Files
sakurac/compiler/optimizer/alias.sakura
T
2026-12-06 13:52:57 -06:00

394 lines
16 KiB
Plaintext

#! @file alias.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains alias analysis, which the passes of the optimizer ask whether an
#! instruction writing memory may write what a read reads, or what they know
#! memory holds. An access touches a region: bytes from a base, which its
#! address steps from by constants, as `add`s of constants and the steps loads
#! and stores take give them. An address stepping by what is not known from one
#! within a cell, the globals, a datum, or memory made, as indexing an array
#! does, is anywhere within the same.
#!
#! Two regions may hold a byte in common but where it is known they do not.
#! Regions of one base, of one cell, or both of the globals, do not where their
#! bytes do not overlap, and two cells do not. A cell that is not escaped, its
#! address going nowhere but to the instructions reading, writing, copying, and
#! clearing it, and to calls as where to write what they return, as the memory
#! pass finds, is touched by nothing reaching through another address, nor by
#! a call, or anything else writing memory the code does not name, but where it
#! writes what it returns. No cell is what an address the function was given,
#! the globals, or the heap holds, none of which is the frame. Memory a `box` or
#! `allocate` made is not what an address the function was given, the globals,
#! other memory made so, or an address read before it was made holds, as none
#! could point at what was not yet there. And nothing writes a datum.
#!
#! Sakura has no rule that memory of one type is not read or written as
#! another: std and the runtime reach through pointers cast to other types, as
#! `float_bits` is written as an `f64` and read as a `u64`, and loads and stores say only how they read and
#! write, not what they reach. So no region is told apart from another by the
#! type of what it holds.
module sakura::compiler;
#! What the base of a region is.
pub enum base_kind : u8 {
other, #! A value nothing more is known of.
cell, #! The address of a cell of the frame.
global, #! The start of the globals, a global's address stepping from it.
datum, #! The address of a datum, which nothing writes.
fresh, #! The address of memory a `box` or `allocate` made.
given, #! A parameter of the entry: an address the function was given.
loaded, #! An address read from memory.
}
#! What a region's size is where it is not known.
pub const unknown_size: u64 = 0xFFFFFFFFFFFFFFFF;
#! The bytes an access touches: a base, how far past it they begin, and how
#! many there are.
pub struct region {
pub var base: u32; #! The value the address steps from by constants.
pub var offset: u64; #! How far past the base the bytes begin, wrapping as addresses do; for a global, past the start of the globals.
pub var size: u64; #! How many bytes, or `unknown_size`.
pub var kind: base_kind; #! What the base is.
pub var cell: u32; #! The cell, where the base is a cell's address.
}
#! What alias analysis knows of code: which cell each value is the address
#! of, which cells are escaped, the bytes each call writes what it returns to,
#! and which block dominates which, where that is known.
pub struct aliases {
var addresses: cell_table; #! Which cell each value is the address of.
var escaped: vector<bool>; #! Whether each cell is escaped.
var writes: vector<u64>; #! Of each instruction, the bytes a call writes what it returns to at its last operand, or 0.
var idom: vector<u32>; #! The immediate dominator of each block, or nothing where it is not known.
var found: bool; #! Whether which cells are escaped is found.
}
#! Finds what alias analysis knows of the code.
#! @param this The code.
#! @param writes Of each instruction, the bytes a call writes what it returns
#! to at its last operand, or 0.
#! @param idom The immediate dominator of each block, or nothing, where
#! memory made after an address was read is not told apart
#! from what it points at, but within a block.
#! @returns What it knows, in scratch memory.
pub def code::aliases(&this, writes: span<u64>, idom: span<u32>): aliases {
var made := this.aliases_unfound(writes, idom);
made.find_escaped(this);
return made;
}
#! Begins what alias analysis knows of the code, but for which cells are
#! escaped, which `aliases::find_escaped` finds before anything asks whether
#! one region may hold a byte of another, and before anything changes which
#! cells are escaped.
#! @param this The code.
#! @param writes Of each instruction, the bytes a call writes what it returns
#! to at its last operand, or 0.
#! @param idom The immediate dominator of each block, or nothing.
#! @returns What it knows, in scratch memory.
pub def code::aliases_unfound(&this, writes: span<u64>, idom: span<u32>): aliases {
var made := aliases {
escaped = vector::<bool>::new(scratch()),
writes = vector::<u64>::new(scratch()),
idom = vector::<u32>::new(scratch())
};
made.writes.extend(writes);
made.idom.extend(idom);
return made;
}
#! Finds which cells are escaped, where that is not found yet.
#! @param this What alias analysis knows.
#! @param body The code.
pub def aliases::find_escaped(&mut this, body: &code): void {
return if this.found;
this.found = true;
this.addresses = body.cell_table();
this.escaped = body.escaped_cells(&this.addresses, this.writes.span);
}
#! Which cells are escaped: their address goes anywhere but to the instructions
#! reading, writing, copying, and clearing them, to `ret`, or to a call as
#! where to write what it returns.
#! @param this The code.
#! @param addresses Which cell each value is the address of.
#! @param writes Of each instruction, the bytes a call writes what it
#! returns to at its last operand, or 0.
#! @returns Whether each cell is, in scratch memory.
pub def code::escaped_cells(&this, addresses: &cell_table, writes: span<u64>): vector<bool> {
var escaped := vector::<bool>::new(scratch());
escaped.resize(this.cells.length);
return escaped if !addresses.any;
var walk := this.walk_all();
var reads := this.reader();
while walk.more() {
val at := walk.next();
val held := &this.insts[at];
reads.start(at);
while reads.more() {
val cell := addresses.cell(reads.next());
continue if cell == 0;
# A cell's address an edge gives escapes.
val i := reads.operand();
val direct := i != no_operand && match held.kind {
op::load, op::store, op::clear => i == 0,
op::copy => i < 2,
op::ret => true,
else => i + 1 == held.argc && (at as u64) < writes.length && writes[at] != 0
};
if !direct {
escaped[cell - 1] = true;
}
}
}
return escaped;
}
#! The region an access touches.
#! @param this What alias analysis knows.
#! @param body The code.
#! @param index The `load`, `store`, `copy`, or `clear`.
#! @param operand Which address: of a `copy`, 0 for where it writes, 1 for
#! where it reads.
#! @returns The region.
pub def aliases::region_of(&this, body: &code, index: u32, operand: u32): region {
val held: inst = body.insts[index];
if held.kind == op::load || held.kind == op::store {
val how: access = held.aux as access;
return this.region_at(body, body.arg(index, 0), held.imm, how == access::bytes ? unknown_size : how.width());
}
return this.region_at(body, body.arg(index, operand), 0, held.imm);
}
#! The region of bytes past an address.
#! @param this What alias analysis knows.
#! @param body The code.
#! @param address The address.
#! @param offset How far past it the bytes begin.
#! @param size How many there are, or `unknown_size`.
#! @returns The region.
pub def aliases::region_at(&this, body: &code, address: u32, offset: u64, size: u64): region {
var made := region { offset = offset, size = size };
this.find_base(body, address, &mut made, 0);
return made;
}
#! Finds the base of a region at an address, and how far past it the address
#! is, adding that to the region's offset. An address stepping by what is not
#! known from one within a cell, the globals, a datum, or memory made, and from
#! nothing else such, is somewhere within the same, as indexing an array is,
#! so the region is any of it.
#! @param this What alias analysis knows.
#! @param body The code.
#! @param address The address.
#! @param made The region.
#! @param depth How many such steps were followed to the address.
def aliases::find_base(&this, body: &code, address: u32, made: &mut region, depth: u32): void {
var at := address;
var steps := 0;
while steps < 8 {
val index := body.defining(at);
break if index == 0 || body.insts[index].kind != op::add;
var step: u64 = 0;
if body.word_constant(body.arg(index, 1), &mut step) {
at = body.arg(index, 0);
} else if body.word_constant(body.arg(index, 0), &mut step) {
at = body.arg(index, 1);
} else {
break if depth >= 2;
var one := region {};
var two := region {};
this.find_base(body, body.arg(index, 0), &mut one, depth + 1);
this.find_base(body, body.arg(index, 1), &mut two, depth + 1);
break if one.within_object() == two.within_object();
val inside: region = one.within_object() ? one : two;
made.base = inside.base;
made.kind = inside.kind;
made.cell = inside.cell;
made.offset = 0;
made.size = unknown_size;
return;
}
made.offset = made.offset + step;
steps++;
}
made.base = at;
val held: origin = body.origins[at];
if held.param {
made.kind = held.from == 0 ? base_kind::given : base_kind::other;
return;
}
val from: inst = body.insts[held.from];
match from.kind {
op::cell => {
made.kind = base_kind::cell;
made.cell = from.aux;
}
op::global => {
made.kind = base_kind::global;
made.offset = made.offset + from.imm;
}
op::datum => made.kind = base_kind::datum;
op::allocate => made.kind = base_kind::fresh;
op::load => made.kind = base_kind::loaded;
else => made.kind = base_kind::other;
}
}
#! Whether a region's base is within a cell, the globals, a datum, or memory
#! made, which a number never is.
#! @param this The region.
#! @returns Whether it is.
def region::within_object(&this): bool {
return match this.kind {
base_kind::cell, base_kind::global, base_kind::datum, base_kind::fresh => true,
else => false
};
}
#! Whether two regions may hold a byte in common, as the file says.
#! @param this What alias analysis knows.
#! @param body The code.
#! @param a One region.
#! @param b The other.
#! @returns Whether they may.
pub def aliases::may_alias(&this, body: &code, a: &region, b: &region): bool {
return false if a.size == 0 || b.size == 0;
return false if a.kind == base_kind::datum || b.kind == base_kind::datum;
if a.kind == base_kind::cell && b.kind == base_kind::cell {
return a.cell == b.cell && overlaps(a, b);
}
if a.kind == base_kind::cell {
return this.escaped[a.cell] && !b.outside_frame();
}
if b.kind == base_kind::cell {
return this.escaped[b.cell] && !a.outside_frame();
}
if a.kind == base_kind::global && b.kind == base_kind::global {
return overlaps(a, b);
}
return overlaps(a, b) if a.base == b.base;
if a.kind == base_kind::fresh {
return this.may_point_at(body, b, a.base);
}
if b.kind == base_kind::fresh {
return this.may_point_at(body, a, b.base);
}
return true;
}
#! Whether a region may hold bytes of memory a `box` or `allocate` made, its
#! base being other than that memory's address.
#! @param this What alias analysis knows.
#! @param body The code.
#! @param other The region.
#! @param fresh The address of the memory made.
#! @returns Whether it may.
def aliases::may_point_at(&this, body: &code, other: &region, fresh: u32): bool {
return match other.kind {
base_kind::fresh, base_kind::given, base_kind::global => false,
base_kind::loaded => !this.read_before(body, other.base, fresh),
else => true
};
}
#! Whether an address was read before memory was made, wherever both are
#! known: the read is before the `box` or `allocate` in its block, or in a
#! block dominating its block.
#! @param this What alias analysis knows.
#! @param body The code.
#! @param loaded The address read.
#! @param fresh The address of the memory made.
#! @returns Whether it was.
def aliases::read_before(&this, body: &code, loaded: u32, fresh: u32): bool {
val read := body.defining(loaded);
val made := body.defining(fresh);
val at: u32 = body.insts[read].block;
val home: u32 = body.insts[made].block;
if at == home {
var walk := body.walk(at);
while walk.more() {
val each := walk.next();
return true if each == read;
return false if each == made;
}
return false;
}
return (home as u64) < this.idom.length && dominates(&this.idom, at, home);
}
#! Whether the bytes of two regions of one base overlap.
#! @param a One region.
#! @param b The other.
#! @returns Whether they do.
def overlaps(a: &region, b: &region): bool {
return b.offset - a.offset < a.size || a.offset - b.offset < b.size;
}
#! Whether a region is outside the frame, so within no cell: the globals, the
#! heap, or what an address the function was given points at.
#! @param this The region.
#! @returns Whether it is.
def region::outside_frame(&this): bool {
return this.kind == base_kind::global || this.kind == base_kind::fresh || this.kind == base_kind::given;
}
#! What an instruction writes, as alias analysis tells it.
pub struct writing {
pub var place: region; #! The region a store, copy, or clear writes, or that a call writes what it returns to, or none of no bytes.
pub var wide: bool; #! Whether it may write any memory but an unescaped cell's, as a call does.
}
#! What an instruction writes: a store, copy, or clear its region; a call or
#! anything else writing memory the code does not name any but an unescaped
#! cell's, and where it writes what it returns; and anything else nothing.
#! @param this What alias analysis knows.
#! @param body The code.
#! @param index The instruction.
#! @returns What it writes.
pub def aliases::writes_of(&this, body: &code, index: u32): writing {
val held: inst = body.insts[index];
var made := writing {};
if held.kind == op::store || held.kind == op::clear || held.kind == op::copy {
made.place = this.region_of(body, index, 0);
return made;
}
return made if held.kind == op::load || (held.kind.traits() & op_write) == 0;
made.wide = true;
val result: u64 = (index as u64) < this.writes.length ? this.writes[index] : 0;
if result != 0 {
made.place = this.region_at(body, body.arg(index, held.argc - 1), 0, result);
}
return made;
}
#! Whether what an instruction writes may hold a byte of a region.
#! @param this What alias analysis knows.
#! @param body The code.
#! @param written What the instruction writes.
#! @param place The region.
#! @returns Whether it may.
pub def aliases::touches(&this, body: &code, written: &writing, place: &region): bool {
if written.wide {
return false if place.kind == base_kind::datum;
return true if place.kind != base_kind::cell || this.escaped[place.cell];
}
return this.may_alias(body, &written.place, place);
}
#! Whether an instruction may write a byte of a region.
#! @param this What alias analysis knows.
#! @param body The code.
#! @param index The instruction.
#! @param place The region.
#! @returns Whether it may.
pub def aliases::clobbers(&this, body: &code, index: u32, place: &region): bool {
val written := this.writes_of(body, index);
return this.touches(body, &written, place);
}