Files
sakurac/compiler/optimizer/sink.sakura
T
2026-11-19 04:54:17 -06:00

272 lines
10 KiB
Plaintext

#! @file sink.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the tenth pass of the optimizer, code sinking: an instruction whose
#! values are read only past one way out of its block moves into the block
#! that way goes to, where nothing else goes there, so what it works out is
#! worked out only where it is read. What moves only works out its values and
#! cannot stop the program, or reads memory. A read moves only where nothing
#! after it in its block may write what it reads, as alias analysis tells, and
#! where it cannot stop the program but as it would have: its address is read
#! or written through, or checked, before it, or after it in its block before
#! anything the program would be seen to do. A block one way alone goes into
#! heads no loop, so nothing moves into a loop it was not in. Blocks are walked
#! before those they dominate, each from its end, so what moved may move on
#! again.
import std::containers;
module sakura::compiler;
#! What sinking knows of the code as it goes. What reads ask of is found the
#! first time a read may move, as most code has none that may.
struct sinking {
var idom: vector<u32>; #! The immediate dominator of each block.
var readers: groups<u32>; #! The instructions reading each value, by value, once each time they read it.
var reached: dict<u64, u32>; #! How many instructions read or write through each address, or check it, in each block, by `pair_key(address, block)`.
var list: vector<u32>; #! The instructions of the block walked, in order, each that moved out 0.
var aliases: aliases; #! What may write what.
var writes: span<u64>; #! Of each instruction, the bytes a call writes what it returns to at its last operand, or 0.
var ready: bool; #! Whether `reached` and `aliases` are found.
}
#! Sinks the code of a function, knowing what each call writes what it
#! returns to.
#! @param this The compiler.
#! @param index The function.
#! @returns Whether anything moved.
def lowerer::sink_code(&mut this, index: u32): bool {
val writes := this.result_writes(index);
return this.program.functions[index].body.sink_code(writes.span);
}
#! Sinks the code, as the file says.
#! @param this The code.
#! @param writes Of each instruction, the bytes a call writes what it returns
#! to at its last operand, or 0.
#! @returns Whether anything moved.
pub def code::sink_code(&mut this, writes: span<u64>): bool {
# Only a block going more than one way has anything to move out of it.
var ways := false;
for b in 0..this.blocks.length {
if !this.blocks[b].gone && this.edge_count(this.blocks[b].last) >= 2 {
ways = true;
break;
}
}
return false if !ways;
var order := vector::<u32>::new(scratch());
var number := vector::<u32>::new(scratch());
this.block_order(&mut order, &mut number);
var state := sinking {
idom = this.block_dominators(),
readers = groups::<u32>::new(this.origins.length, scratch()),
reached = dict::<u64, u32>::new(scratch()),
list = vector::<u32>::new(scratch()),
writes = writes
};
# What reads each value, counted then put.
var walk := this.walk_all();
var reads := this.reader();
while walk.more() {
reads.start(walk.next());
while reads.more() {
state.readers.count(reads.next());
}
}
state.readers.seal();
var again := this.walk_all();
while again.more() {
val at := again.next();
reads.start(at);
while reads.more() {
state.readers.put(reads.next(), at);
}
}
var changed := false;
for &block in order {
val last: u32 = this.blocks[block].last;
continue if this.edge_count(last) < 2;
changed = this.sink_block(block, &mut state) || changed;
}
return changed;
}
#! Finds what reads moving ask of the code, where it is not yet found: what
#! may write what, and what reaches through each address in each block, as the
#! code is now.
#! @param this The code.
#! @param state What sinking knows.
def code::ready_sinking(&this, state: &mut sinking): void {
return if state.ready;
state.ready = true;
state.aliases = this.aliases(state.writes, state.idom.span);
var walk := this.walk_all();
while walk.more() {
val at := walk.next();
val held: inst = this.insts[at];
match held.kind {
op::load, op::store, op::clear, op::copy, op::check => {
val first := this.arg(at, 0);
if this.reaches(at, first) {
val key := pair_key(first, held.block);
state.reached.set(key, state.reached.get_or(&key, 0) + 1);
}
if held.kind == op::copy && held.imm != 0 && this.arg(at, 1) != first {
val key := pair_key(this.arg(at, 1), held.block);
state.reached.set(key, state.reached.get_or(&key, 0) + 1);
}
}
else => {}
}
}
}
#! Whether an instruction reads or writes memory through an address, or checks
#! it is not null, so that it stops the program where the address is null.
#! @param this The code.
#! @param index The instruction.
#! @param address The address.
#! @returns Whether it does.
def code::reaches(&this, index: u32, address: u32): bool {
val held := &this.insts[index];
return match held.kind {
op::load, op::store => this.arg(index, 0) == address,
op::clear => held.imm != 0 && this.arg(index, 0) == address,
op::copy => held.imm != 0 && (this.arg(index, 0) == address || this.arg(index, 1) == address),
op::check => held.aux == fault::null_reference as u32 && this.arg(index, 0) == address,
else => false
};
}
#! Moves what a block works out only for one way out of it into the block
#! that way goes to, from the block's end.
#! @param this The code.
#! @param block The block, which ends going more than one way.
#! @param state What sinking knows.
#! @returns Whether anything moved.
def code::sink_block(&mut this, block: u32, state: &mut sinking): bool {
state.list.clear();
var walk := this.walk(block);
while walk.more() {
state.list.append(walk.next());
}
var changed := false;
var k := state.list.length - 1;
while k > 0 {
k--;
val index: u32 = state.list[k];
val into := this.sink_target(index, block, state);
continue if into == no_block;
val load := this.insts[index].kind == op::load;
if load {
this.ready_sinking(state);
}
continue if load && !this.load_sinks(index, k, block, state);
# Those before it are all still in the block.
val prior: u32 = k == 0 ? 0 : state.list[k - 1];
if prior == 0 {
this.blocks[block].first = this.insts[index].next;
} else {
this.insts[prior].next = this.insts[index].next;
}
this.put_first(into, index);
state.list[k] = 0;
changed = true;
if load {
val address := this.arg(index, 0);
val from := pair_key(address, block);
state.reached.set(from, state.reached.get_or(&from, 1) - 1);
val to := pair_key(address, into);
state.reached.set(to, state.reached.get_or(&to, 0) + 1);
}
}
return changed;
}
#! Where an instruction of a block may move: the block one way out of it goes
#! to, where nothing else goes, past which everything reading its values is.
#! @param this The code.
#! @param index The instruction.
#! @param block Its block.
#! @param state What sinking knows.
#! @returns The block, or `no_block` where it stays.
def code::sink_target(&this, index: u32, block: u32, state: &sinking): u32 {
val held: inst = this.insts[index];
return no_block if held.count == 0 || held.kind.made_anywhere();
return no_block if held.kind != op::load && !this.removable(index);
var target := no_block;
for r in 0..held.count {
for &reader in state.readers.of(held.result + r as u32) {
# The block it dominates most nearly that dominates the reader's.
var at: u32 = this.insts[reader].block;
return no_block if at == block;
var steps := 0;
while true {
val up: u32 = state.idom[at];
break if up == block;
return no_block if up == no_block || up == at || steps > 256;
at = up;
steps++;
}
return no_block if target != no_block && target != at;
target = at;
}
}
return no_block if target == no_block;
val preds := &this.blocks[target].preds;
return no_block if preds.length != 1 || this.edge_source(preds[0]) != block;
return target;
}
#! Whether a read may move to after the end of its block: nothing after it in
#! the block may write what it reads, and its address is not null there, or
#! the program would have stopped as it does now. That it is read or written
#! through, or checked, before the read, in the block or one dominating it,
#! says so, as does its being so after the read in the block, before anything
#! the program would be seen to do.
#! @param this The code.
#! @param index The `load`.
#! @param k Its place among the block's instructions.
#! @param block Its block.
#! @param state What sinking knows.
#! @returns Whether it may.
def code::load_sinks(&this, index: u32, k: u64, block: u32, state: &sinking): bool {
val address := this.arg(index, 0);
val place := state.aliases.region_of(this, index, 0);
var safe := this.never_zero(address);
var quiet := true;
for j in k + 1..state.list.length {
val other: u32 = state.list[j];
continue if other == 0;
val kind := this.insts[other].kind;
return false if kind != op::load && (kind.traits() & op_write) != 0 && state.aliases.clobbers(this, other, &place);
if quiet && this.reaches(other, address) {
safe = true;
}
val stops := kind == op::load ? !this.never_zero(this.arg(other, 0)) : (kind.traits() & op_traps) != 0 && !this.removable(other);
if stops {
quiet = false;
}
}
return true if safe;
for j in 0..k {
return true if this.reaches(state.list[j], address);
}
val home := this.home_of(address);
var at := block;
var steps := 0;
while at != home && at != 0 && steps < 64 {
at = state.idom[at];
val key := pair_key(address, at);
return true if state.reached.get_or(&key, 0) != 0;
steps++;
}
return false;
}