Files
sakurac/compiler/intermediate/liveness.sakura
T
2026-10-28 22:13:49 -05:00

134 lines
5.2 KiB
Plaintext

#! @file liveness.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains what the machine's assembler and the native backend's register
#! allocator share of finding where each value of a function's code is live:
#! the block a value is made in, and the walk from each block a value is read
#! in back over the edges into it, to the block making it. A value read in a
#! block other than its own is live into that block, and out of every block an
#! edge into it leaves, and so on back to its own, as a value read in a loop is
#! live around it.
module sakura::compiler;
#! Where the value being found is live in a block.
pub struct block_reach {
pub var stamp: u32; #! The value it is for, or another, where it is stale.
pub var ends: u32; #! Past the last read in the block.
pub var out: bool; #! Whether it is live where the block ends.
pub var into: bool; #! Whether it is live where the block begins.
}
#! The walk finding the blocks a value is live in, from the reads of it back to
#! the block making it. The reach of each block is kept for the value being
#! found, and is stale for any other, so the values of a function are found one
#! after another in the same memory.
pub struct live_walk {
pub var reaches: vector<block_reach>; #! The reach of each block.
pub var touched: vector<u32>; #! The blocks the value reaches, each once.
var todo: vector<u32>; #! The blocks it is live into whose edges in are not walked yet.
var reach: slice<block_reach>; #! A view of `reaches`, which never grows, indexed without a call.
var value: u32; #! The value being found.
var home: u32; #! The block making it.
}
#! Makes a walk of the blocks of a function's code, finding no value yet.
#! @param blocks How many blocks the code has.
#! @param alloc The optional allocator for the memory backing of the walk.
#! @returns The walk.
pub def live_walk::new(blocks: u64, alloc: maybe<&mut allocator>): live_walk {
var made := live_walk {
reaches = vector::<block_reach>::new(alloc),
touched = vector::<u32>::new(alloc),
todo = vector::<u32>::new(alloc),
value = 0xFFFFFFFF
};
made.reaches.resize(blocks, block_reach { stamp = 0xFFFFFFFF });
made.reach = made.reaches.slice();
return made;
}
#! Begins finding where a value is live.
#! @param this The walk.
#! @param value The value, which no reach is stamped with yet.
#! @param home The block making it.
pub def live_walk::begin(&mut this, value: u32, home: u32): void {
this.touched.clear();
this.todo.clear();
this.value = value;
this.home = home;
}
#! Readies a block's reach for the value being found, where it is stale.
#! @param this The walk.
#! @param block The block.
pub def live_walk::touch(&mut this, block: u32): void {
return if this.reach[block].stamp == this.value;
this.reach[block] = block_reach { stamp = this.value };
this.touched.append(block);
}
# Each way the value is found live in a block notes it live into the block too,
# which is then walked back from, unless the block makes the value, or it was
# noted already.
#! Notes a read of the value in a block, at a position: the block holds it up
#! to there, and it is live into the block, unless the block makes it.
#! @param this The walk.
#! @param block The block.
#! @param position Where the read is.
pub def live_walk::read(&mut this, block: u32, position: u32): void {
this.touch(block);
if position + 1 > this.reach[block].ends {
this.reach[block].ends = position + 1;
}
if block != this.home && !this.reach[block].into {
this.reach[block].into = true;
this.todo.append(block);
}
}
#! Notes a read of the value where a block ends, as the argument of an edge
#! leaving it: it is live out of the block, and into it, unless the block makes
#! it.
#! @param this The walk.
#! @param block The block.
pub def live_walk::read_out(&mut this, block: u32): void {
this.touch(block);
this.reach[block].out = true;
if block != this.home && !this.reach[block].into {
this.reach[block].into = true;
this.todo.append(block);
}
}
#! Walks back from each block the value is live into, over each edge into it:
#! the value is live out of the block the edge leaves, and into that block too,
#! unless it makes the value. The block making it is reached last of all.
#! @param this The walk.
#! @param body The code.
#! @param number Each block's place in the order laid out, or `0xFFFFFFFF`
#! for one not laid out.
#! @param laid Whether the edges leaving blocks not laid out are passed
#! over, rather than walked as any other.
pub def live_walk::walk(&mut this, body: &code, number: &vector<u32>, laid: bool): void {
while this.todo.length != 0 {
val block: u32 = this.todo.last();
this.todo.truncate();
for &edge in body.blocks[block].preds {
val from := body.edge_source(edge);
continue if laid && number[from] == 0xFFFFFFFF;
this.touch(from);
this.reach[from].out = true;
if from != this.home && !this.reach[from].into {
this.reach[from].into = true;
this.todo.append(from);
}
}
}
this.touch(this.home);
}