fixing phi moves (longer move cycles)

Signed-off-by: Jakub Doka <jakub.doka2@gmail.com>
This commit is contained in:
Jakub Doka 2024-11-30 22:33:13 +01:00
parent 19aca050ed
commit 3f6ebdd009
No known key found for this signature in database
GPG key ID: C6E9A89936B8C143
6 changed files with 69 additions and 33 deletions

View file

@ -314,6 +314,7 @@ impl Nodes {
); );
for &block in &*cfg_nodes { for &block in &*cfg_nodes {
debug_assert!(block != VOID);
seen.clear(self.values.len()); seen.clear(self.values.len());
let mut outputs = mem::take(&mut self[block].outputs); let mut outputs = mem::take(&mut self[block].outputs);
self.reschedule_block(block, &mut outputs, buf, seen); self.reschedule_block(block, &mut outputs, buf, seen);
@ -719,7 +720,6 @@ impl Nodes {
self.bind(from, to); self.bind(from, to);
} }
bind_buf.clear();
self[VOID].outputs = self[VOID].outputs =
self[VOID].outputs.iter().filter(|&&n| self[n].kind.is_at_start()).copied().collect(); self[VOID].outputs.iter().filter(|&&n| self[n].kind.is_at_start()).copied().collect();
@ -736,8 +736,8 @@ impl Nodes {
self[shed].outputs.push(n); self[shed].outputs.push(n);
} }
bind_buf.clear();
scratch.clear(); scratch.clear();
visited.clear(self.values.len()); visited.clear(self.values.len());
self.schedule_inside_blocks(bind_buf, scratch, visited); self.schedule_inside_blocks(bind_buf, scratch, visited);
} }

View file

@ -1,9 +1,12 @@
use { use {
crate::{ crate::{
parser, parser, quad_sort,
son::{ son::{
debug_assert_matches, debug_assert_matches,
hbvm::{reg, reg::Reg, HbvmBackend, Nid, Nodes, PLoc}, hbvm::{
reg::{self, Reg},
HbvmBackend, Nid, Nodes, PLoc,
},
Kind, ARG_START, MEM, VOID, Kind, ARG_START, MEM, VOID,
}, },
ty::{self, Arg, Loc}, ty::{self, Arg, Loc},
@ -11,7 +14,7 @@ use {
Sig, Types, Sig, Types,
}, },
alloc::{borrow::ToOwned, vec::Vec}, alloc::{borrow::ToOwned, vec::Vec},
core::{mem, ops::Range}, core::{mem, ops::Range, u8, usize},
hbbytecode::{self as instrs}, hbbytecode::{self as instrs},
}; };
@ -167,32 +170,65 @@ impl HbvmBackend {
moves.len() moves.len()
}); });
moves.sort_unstable_by(|[aa, ab, _], [ba, bb, _]| { let mut graph = [u8::MAX; 256];
if aa == bb && ab == ba { for &[d, s, _] in moves.iter() {
core::cmp::Ordering::Equal graph[d as usize] = s;
} else if aa == bb { }
core::cmp::Ordering::Greater
} else {
core::cmp::Ordering::Less
}
});
moves.dedup_by(|[aa, ab, _], [ba, bb, kind]| { // [3, 2, 0]
if aa == bb && ab == ba {
*kind = 1;
true
} else {
false
}
});
for [dst, src, kind] in moves { // cp 0, 1 = 3
if kind == 0 { // cp 1, 2 = 2
self.emit(instrs::cp(dst, src)); // cp 2, 0 = 1
} else { //
self.emit(instrs::swa(dst, src)); // swp a, b
// swp b, c
// cp a, b
// cp b, c
// cp c, d
// cp d, a
//
// swp a, b
// swp b, c
// swp c, d
'o: for &mut [d, s, ref mut depth] in moves.iter_mut() {
let mut c = s;
loop {
if c == d {
break;
}
c = graph[c as usize];
*depth += 1;
if c == u8::MAX {
continue 'o;
}
}
// cut the cycle
graph[c as usize] = u8::MAX;
// mark cycyle
*depth = u8::MAX;
}
quad_sort(&mut moves, |a, b| a[2].cmp(&b[2]).reverse());
for [mut d, mut s, depth] in moves {
if depth == u8::MAX {
while graph[s as usize] != u8::MAX {
self.emit(instrs::swa(d, s));
d = s;
mem::swap(&mut graph[s as usize], &mut s);
}
graph[s as usize] = s;
} else if graph[s as usize] != s {
self.emit(instrs::cp(d, s));
} }
} }
is_next_block = res.backrefs[nid as usize] as usize == i + 1; is_next_block = res.backrefs[nid as usize] as usize == i + 1;
} }
Kind::Return { .. } => { Kind::Return { .. } => {

View file

@ -14,9 +14,9 @@ main:
CP r35, r0 CP r35, r0
LI64 r36, 30d LI64 r36, 30d
LI64 r37, 100d LI64 r37, 100d
CP r34, r35
CP r33, r35
CP r32, r35 CP r32, r35
CP r33, r35
CP r34, r35
5: JLTU r34, r36, :0 5: JLTU r34, r36, :0
ADDI64 r32, r32, 1d ADDI64 r32, r32, 1d
CP r2, r35 CP r2, r35

View file

@ -172,8 +172,8 @@ push:
ADDI64 r33, r32, 1d ADDI64 r33, r32, 1d
LD r32, r32, 0a, 1h LD r32, r32, 0a, 1h
ST r32, r34, 0a, 1h ST r32, r34, 0a, 1h
CP r34, r36
CP r32, r33 CP r32, r33
CP r34, r36
JMP :7 JMP :7
0: LD r32, r38, 0a, 8h 0: LD r32, r38, 0a, 8h
LD r33, r38, 8a, 8h LD r33, r38, 8a, 8h
@ -228,8 +228,8 @@ push:
ADDI64 r33, r32, 8d ADDI64 r33, r32, 8d
LD r32, r32, 0a, 8h LD r32, r32, 0a, 8h
ST r32, r34, 0a, 8h ST r32, r34, 0a, 8h
CP r34, r36
CP r32, r33 CP r32, r33
CP r34, r36
JMP :7 JMP :7
0: LD r32, r38, 8a, 8h 0: LD r32, r38, 8a, 8h
MULI64 r33, r32, 8d MULI64 r33, r32, 8d

View file

@ -2,8 +2,8 @@ fib:
CP r13, r2 CP r13, r2
LI64 r17, 1d LI64 r17, 1d
CP r15, r0 CP r15, r0
CP r16, r17
CP r14, r15 CP r14, r15
CP r16, r17
2: JNE r13, r15, :0 2: JNE r13, r15, :0
CP r1, r14 CP r1, r14
JMP :1 JMP :1

View file

@ -23,8 +23,8 @@ fib_iter:
CP r13, r2 CP r13, r2
LI64 r17, 1d LI64 r17, 1d
CP r15, r0 CP r15, r0
CP r16, r17
CP r14, r15 CP r14, r15
CP r16, r17
2: JNE r13, r15, :0 2: JNE r13, r15, :0
CP r1, r14 CP r1, r14
JMP :1 JMP :1