208 lines
8.1 KiB
Plaintext
208 lines
8.1 KiB
Plaintext
#! @file lift.sakura
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
|
#! @copyright Malunal Studios, LLC.
|
|
#! @license MIT
|
|
#!
|
|
#! Contains code lifting: what both ways out of a branch work out alike is
|
|
#! worked out once, before the branch, as LLVM's SimplifyCFG lifts what the two
|
|
#! arms of an `if` begin with alike, and its GVNHoist what they work out alike
|
|
#! anywhere. A branch whose two ways each go into a block it alone goes into
|
|
#! has each instruction of the first that the second works out too, from the
|
|
#! same values made before the branch or lifted already, moved to before the
|
|
#! branch, and the second's read it instead. What only works out its value
|
|
#! moves from anywhere in its block; a read of memory, a division, and a check,
|
|
#! which may stop the program, only from among what each block does first,
|
|
#! before anything else of it that may stop the program, or that writes
|
|
#! memory or calls, so it reads what it would have and stops the program where
|
|
#! it would have. Then where the two ways join, what works out again what both
|
|
#! worked out is numbered as what the branch's block now does.
|
|
#!
|
|
#! Blocks are lifted from after those they lead to, so what an inner `if`'s
|
|
#! arms lifted into it may be lifted again into the `if` holding it.
|
|
|
|
module sakura::compiler;
|
|
|
|
|
|
#! The most instructions of the second block looked through for one alike.
|
|
const lift_window: u64 = 64;
|
|
|
|
#! The most instructions of the first block looked through.
|
|
const lift_reach: u32 = 128;
|
|
|
|
#! Lifts what both ways out of each branch work out alike, as the file says.
|
|
#! @param this The code.
|
|
#! @returns Whether anything moved.
|
|
pub def code::lift_code(&mut this): bool {
|
|
var order := vector::<u32>::new(scratch());
|
|
var number := vector::<u32>::new(scratch());
|
|
this.block_order(&mut order, &mut number);
|
|
var done := rewrite::new();
|
|
var k := order.length;
|
|
while k > 0 {
|
|
k--;
|
|
val block: u32 = order[k];
|
|
val last: u32 = this.blocks[block].last;
|
|
continue if last == 0 || this.insts[last].kind != op::branch;
|
|
val one: u32 = this.insts[last].aux;
|
|
val first: u32 = this.edges[one].target;
|
|
val second: u32 = this.edges[one + 1].target;
|
|
continue if first == second || first == block || second == block;
|
|
continue if this.blocks[first].preds.length != 1 || this.blocks[second].preds.length != 1;
|
|
this.lift_pair(block, first, second, &mut done);
|
|
}
|
|
this.apply(&done);
|
|
return done.changed;
|
|
}
|
|
|
|
#! Whether an instruction may be lifted wherever it is in its block: it only
|
|
#! works out its value from what it takes, reading no memory, which what is
|
|
#! before it in its block may have written, and cannot stop the program.
|
|
#! @param this The code.
|
|
#! @param index The instruction.
|
|
#! @returns Whether it may.
|
|
def code::quiet(&this, index: u32): bool {
|
|
return this.removable(index) && this.insts[index].count == 1 && (this.insts[index].kind.traits() & op_reads) == 0;
|
|
}
|
|
|
|
#! Whether an instruction may be lifted from among what its block does first:
|
|
#! a read of memory, a division, or a check, which may stop the program but
|
|
#! write nothing.
|
|
#! @param this The code.
|
|
#! @param index The instruction.
|
|
#! @returns Whether it may.
|
|
def code::liftable_first(&this, index: u32): bool {
|
|
return match this.insts[index].kind {
|
|
op::load, op::check, op::divs, op::divu, op::rems, op::remu => true,
|
|
else => false
|
|
};
|
|
}
|
|
|
|
#! Lifts what two blocks a branch alone goes into work out alike, as the file
|
|
#! says.
|
|
#! @param this The code.
|
|
#! @param block The block ending in the branch.
|
|
#! @param first The block one way goes into.
|
|
#! @param second The block the other way goes into.
|
|
#! @param done What the pass changes: each instruction of the second block
|
|
#! lifted is taken out, what it gave read from the first's.
|
|
def code::lift_pair(&mut this, block: u32, first: u32, second: u32, done: &mut rewrite): void {
|
|
# The instructions of the second block that may be alike, in order.
|
|
var others := vector::<u32>::new(scratch());
|
|
var gather := this.walk(second);
|
|
while gather.more() && others.length < lift_window {
|
|
val at := gather.next();
|
|
break if this.insts[at].kind.ends();
|
|
others.append(at);
|
|
}
|
|
return if others.length == 0;
|
|
|
|
var prior: u32 = 0;
|
|
var tail: u32 = 0;
|
|
var opening := true;
|
|
var looked: u32 = 0;
|
|
var walk := this.walk(first);
|
|
while walk.more() && looked < lift_reach {
|
|
val at := walk.next();
|
|
looked++;
|
|
break if this.insts[at].kind.ends();
|
|
val quiet := this.quiet(at);
|
|
val early := !quiet && opening && this.liftable_first(at);
|
|
var lifted := false;
|
|
if (quiet || early) && !this.insts[at].kind.made_anywhere() && this.made_before(at, first, done) {
|
|
val other := this.alike_in(at, second, &others, early, done);
|
|
if other != 0 {
|
|
# It leaves the first block for the end of the branch's, after what
|
|
# was lifted before it.
|
|
val next: u32 = this.insts[at].next;
|
|
if prior == 0 {
|
|
this.blocks[first].first = next;
|
|
} else {
|
|
this.insts[prior].next = next;
|
|
}
|
|
if tail == 0 {
|
|
this.link_before(at, this.blocks[block].last);
|
|
} else {
|
|
this.link_after(at, tail);
|
|
}
|
|
tail = at;
|
|
if this.insts[at].count == 1 {
|
|
done.replace(this.insts[other].result, this.insts[at].result);
|
|
}
|
|
done.remove(other);
|
|
lifted = true;
|
|
}
|
|
}
|
|
if !lifted {
|
|
opening = opening && quiet;
|
|
prior = at;
|
|
}
|
|
}
|
|
}
|
|
|
|
#! Whether what an instruction reads is made before the block it is in, or by
|
|
#! what was lifted out of it, so it may be worked out before the block.
|
|
#! @param this The code.
|
|
#! @param index The instruction.
|
|
#! @param home Its block.
|
|
#! @param done What the pass changes.
|
|
#! @returns Whether it is.
|
|
def code::made_before(&this, index: u32, home: u32, done: &rewrite): bool {
|
|
val held: inst = this.insts[index];
|
|
for i in 0..held.argc {
|
|
return false if this.home_of(done.resolve(this.arg(index, i as u32))) == home;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
#! Finds the instruction of the second block alike to one of the first: of the
|
|
#! same operation, working it out of the same values, those of the second
|
|
#! block read as what they were replaced by, all made before it. Where the one
|
|
#! of the first may stop the program, the one of the second must be among what
|
|
#! its block does first too.
|
|
#! @param this The code.
|
|
#! @param index The instruction of the first block.
|
|
#! @param second The second block.
|
|
#! @param others The instructions of the second block that may be alike.
|
|
#! @param early Whether the one of the first may stop the program.
|
|
#! @param done What the pass changes.
|
|
#! @returns The instruction alike, or 0.
|
|
def code::alike_in(&this, index: u32, second: u32, others: &vector<u32>, early: bool, done: &rewrite): u32 {
|
|
val held: inst = this.insts[index];
|
|
var opening := true;
|
|
for &other in others.span {
|
|
continue if done.is_removed(other);
|
|
val them: inst = this.insts[other];
|
|
if them.kind == held.kind && them.aux == held.aux && them.imm == held.imm && them.argc == held.argc
|
|
&& them.count == held.count && (!early || opening)
|
|
&& (held.count == 0 || this.origins[them.result].kind == this.origins[held.result].kind)
|
|
&& this.reads_alike(index, other, done) && this.made_before(other, second, done) {
|
|
return other;
|
|
}
|
|
opening = opening && this.quiet(other);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
#! Whether two instructions of one operation work it out of the same values,
|
|
#! each read as what replaced it, in either order where the operation does not
|
|
#! mind which.
|
|
#! @param this The code.
|
|
#! @param a The first instruction.
|
|
#! @param b The second.
|
|
#! @param done What the pass changes.
|
|
#! @returns Whether they do.
|
|
def code::reads_alike(&this, a: u32, b: u32, done: &rewrite): bool {
|
|
val count := this.insts[a].argc;
|
|
if count == 2 && commutes(this.insts[a].kind) {
|
|
val a0 := done.resolve(this.arg(a, 0));
|
|
val a1 := done.resolve(this.arg(a, 1));
|
|
val b0 := done.resolve(this.arg(b, 0));
|
|
val b1 := done.resolve(this.arg(b, 1));
|
|
return (a0 == b0 && a1 == b1) || (a0 == b1 && a1 == b0);
|
|
}
|
|
for i in 0..count {
|
|
return false if done.resolve(this.arg(a, i as u32)) != done.resolve(this.arg(b, i as u32));
|
|
}
|
|
return true;
|
|
}
|