Files
sakurac/compiler/optimizer/dead.sakura
T

224 lines
7.3 KiB
Plaintext
Raw Permalink Normal View History

2026-10-26 10:14:11 -05:00
#! @file dead.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the second pass of the optimizer, which takes out what nothing
#! needs: an instruction whose results nothing reads, where taking it out
#! changes nothing else, a parameter of a block that nothing reads, and a
#! cell whose address nothing takes, which leaves the frame smaller. What
#! is needed is found from what must stay, as a store or a call: what it
#! reads is needed, then what that reads in turn, and a parameter needed
#! needs the argument each edge into its block gives it. So a value carried
#! around a loop only to be carried around again goes too.
module sakura::compiler;
#! Takes out what nothing needs, as the file says.
#! @param this The code.
#! @returns Whether anything changed.
pub def code::remove_dead(&mut this): bool {
var live := worklist::new(this.origins.length, scratch());
var reached := vector::<bool>::new(scratch());
reached.resize(this.insts.length);
# What must stay needs what it reads, but for writes to cells nothing reads.
val read := this.read_cells();
var walk := this.walk_all();
while walk.more() {
val at := walk.next();
if !this.removable(at) && !this.unread_write(at, &read) {
reached[at] = true;
this.need_operands(at, &mut live);
}
}
# The entry's parameters are what the function takes, which stay.
for &each in this.blocks[0].params {
live.push_new(each);
}
while !live.empty {
val value := live.pop();
val held: origin = this.origins[value];
if held.param {
for &edge in this.blocks[held.from].preds {
val given: u32 = this.edges[edge].args[held.index];
live.push_new(given);
}
continue;
}
if !reached[held.from] {
reached[held.from] = true;
this.need_operands(held.from, &mut live);
}
}
var done := rewrite::new();
var sweep := this.walk_all();
while sweep.more() {
val at := sweep.next();
if !reached[at] {
done.remove(at);
}
}
this.apply(&done);
# Each parameter nothing needs goes, with what each edge gives it.
var changed := done.changed;
var keeps := vector::<bool>::new(scratch());
for b in 1..this.blocks.length {
continue if this.blocks[b].gone;
keeps.clear();
for &param in this.blocks[b].params.span {
keeps.append(live.has(param));
}
continue if !keeps.span.contains(&false);
changed = true;
this.drop_params(b as u32, keeps.span);
}
return this.drop_cells() || changed;
}
#! Takes out the cells whose address nothing takes, numbering those left in
#! the order they were.
#! @param this The code.
#! @returns Whether any was taken out.
def code::drop_cells(&mut this): bool {
return false if this.cells.length == 0;
var used := vector::<bool>::new(scratch());
used.resize(this.cells.length);
var walk := this.walk_all();
while walk.more() {
val at := walk.next();
if this.insts[at].kind == op::cell {
used[this.insts[at].aux] = true;
}
}
var renumbered := vector::<u32>::new(scratch());
var kept := vector::<cell>::new();
for c in 0..this.cells.length {
renumbered.append(kept.length as u32);
if used[c] {
kept.append(this.cells[c]);
}
}
return false if kept.length == this.cells.length;
var again := this.walk_all();
while again.more() {
val at := again.next();
if this.insts[at].kind == op::cell {
this.insts[at].aux = renumbered[this.insts[at].aux];
}
}
this.cells = move(&mut kept);
for i in 0..this.variables.length {
val held: u32 = this.variables[i].cell;
continue if held == 0;
this.variables[i].cell = used[held - 1] ? renumbered[held - 1] + 1 : 0;
}
return true;
}
#! Which cells something reads: by loading from them, copying out of them,
#! or anything else their address is given to, which may read them.
#! @param this The code.
#! @returns Whether each cell is read.
def code::read_cells(&this): vector<bool> {
var read := vector::<bool>::new(scratch());
return read if this.cells.length == 0;
read.resize(this.cells.length);
var walk := this.walk_all();
var reads := this.reader();
while walk.more() {
val at := walk.next();
# Writing a cell at its address does not read it.
val kind := this.insts[at].kind;
val writes := kind == op::store || kind == op::clear || kind == op::copy;
reads.start(at);
while reads.more() {
val cell := this.cell_of(reads.next());
if cell != no_cell && !(writes && reads.operand() == 0) {
read[cell] = true;
}
}
}
return read;
}
#! Whether an instruction only writes a cell nothing reads, which goes: a store
#! or a clear of it, or a copy into it from an address that cannot be null.
#! @param this The code.
#! @param index The instruction.
#! @param read Which cells something reads.
#! @returns Whether it does.
def code::unread_write(&this, index: u32, read: &vector<bool>): bool {
val held: inst = this.insts[index];
return false if held.kind != op::store && held.kind != op::clear && held.kind != op::copy;
val cell := this.cell_of(this.arg(index, 0));
return false if cell == no_cell || read[cell];
return held.kind != op::copy || this.checked_address(index, this.arg(index, 1));
}
#! Whether an address an instruction reads cannot be null there: one never
#! zero, or a step from a pointer a check before it in its block found not
#! null.
#! @param this The code.
#! @param index The instruction.
#! @param address The address.
#! @returns Whether it cannot.
pub def code::checked_address(&this, index: u32, address: u32): bool {
return true if this.never_zero(address);
var base := address;
var steps := 0;
while steps < 8 {
val made := this.defining(base);
break if made == 0 || this.insts[made].kind != op::add;
base = this.arg(made, 0);
steps++;
}
var walk := this.walk(this.insts[index].block);
while walk.more() {
val at := walk.next();
break if at == index;
val held := &this.insts[at];
if held.kind == op::check && held.aux == fault::null_reference as u32 && this.arg(at, 0) == base {
return true;
}
}
return false;
}
#! Marks what an instruction reads as needed.
#! @param this The code.
#! @param index The instruction.
#! @param live The values needed, those newly needed waiting for their own
#! needs to be marked.
def code::need_operands(&this, index: u32, live: &mut worklist): void {
val held := &this.insts[index];
for i in 0..held.argc {
live.push_new(this.operands[held.args as u64 + i as u64]);
}
}
#! Whether an instruction may be taken out where nothing reads what it gives:
#! one that only works out its results, a division by a constant other than
#! zero, which never stops the program, and a read of an address never zero.
#! @param this The code.
#! @param index The instruction.
#! @returns Whether it may.
pub def code::removable(&this, index: u32): bool {
val held: inst = this.insts[index];
return false if held.count == 0 || held.kind.ends();
return true if (held.kind.traits() & op_pure) != 0;
match held.kind {
op::divs, op::divu, op::rems, op::remu => {
var bits: u64 = 0;
return this.word_constant(this.arg(index, 1), &mut bits) && bits != 0;
}
op::load => return this.never_zero(this.arg(index, 0));
else => return false;
}
return false;
}