Initial Commit
This commit is contained in:
782 files changed
+145156
No files matched your search
@@ -0,0 +1,393 @@
|
||||
#! @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: ®ion, b: ®ion): 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: ®ion, 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: ®ion, b: ®ion): 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: ®ion): 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: ®ion): bool {
|
||||
val written := this.writes_of(body, index);
|
||||
return this.touches(body, &written, place);
|
||||
}
|
||||
@@ -0,0 +1,496 @@
|
||||
#! @file bits.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the pass over the bits of words, which works out two things of each
|
||||
#! word as LLVM's known bits and demanded bits do: which of its bits are known
|
||||
#! to be zero, from what makes it, and which of its bits anything reads, from
|
||||
#! what reads it.
|
||||
#!
|
||||
#! What is known to be zero comes first. A narrowing of a word holding nothing
|
||||
#! past the bits it keeps goes, as `wrap(x & 255, 32, signed)` is `x & 255`, and
|
||||
#! so does a mask clearing only bits known to be zero; a comparison those bits
|
||||
#! decide is the constant it gives, as a byte compared with 256 is; and the
|
||||
#! rounding of an `f32` made of a word of fewer than 25 bits goes, as the `f32`
|
||||
#! holds it exactly. What is known of a word is worked out as it is asked for,
|
||||
#! from what made it, a few steps back, as LLVM works it out: a block's
|
||||
#! parameter knows what every edge into it gives, so a loop's counter of `u32`
|
||||
#! stays below 2^32 where each way round narrows it.
|
||||
#!
|
||||
#! Then, of the code that leaves, what is read. The low bits of a sum, a
|
||||
#! difference, or a product are worked out from the low bits of what it takes
|
||||
#! alone, a store of a byte writes only the low 8 bits of its word, and a mask
|
||||
#! reads only the bits it keeps. So a narrowing whose bits past those it keeps
|
||||
#! nothing reads goes, as a sum of `i32`s narrowed, multiplied, and narrowed
|
||||
#! again needs only the last; and a mask or other bits set or flipped where
|
||||
#! nothing reads them goes too. Each word that goes is replaced by one that
|
||||
#! agrees with it on every bit read of it, so what reads it gives what it did
|
||||
#! wherever it is read.
|
||||
|
||||
module sakura::compiler;
|
||||
|
||||
|
||||
#! Every bit of a word.
|
||||
const all_bits: u64 = 0xFFFFFFFFFFFFFFFFu;
|
||||
|
||||
#! How many steps back from a word what is known of it is looked for.
|
||||
const bits_depth: u32 = 8;
|
||||
|
||||
#! How many words in all are looked at for what is known of one.
|
||||
const bits_budget: u32 = 48;
|
||||
|
||||
#! A word of its low bits set.
|
||||
#! @param count How many.
|
||||
#! @returns The word.
|
||||
def low_ones(count: u64): u64 {
|
||||
return count >= 64 ? all_bits : (1u << count) - 1;
|
||||
}
|
||||
|
||||
#! A word of its high bits set.
|
||||
#! @param count How many.
|
||||
#! @returns The word.
|
||||
def high_ones(count: u64): u64 {
|
||||
return count == 0 ? 0 : (count >= 64 ? all_bits : all_bits << (64 - count));
|
||||
}
|
||||
|
||||
#! The bits up to and including the highest bit of a word set, as the low bits
|
||||
#! of a sum or a product read the bits of what it takes.
|
||||
#! @param bits The word.
|
||||
#! @returns The bits.
|
||||
def up_to_highest(bits: u64): u64 {
|
||||
return low_ones(64 - leading_zeros_of(bits));
|
||||
}
|
||||
|
||||
#! Simplifies what the bits of the code's words decide, as the file says: first
|
||||
#! by what is known to be zero, then by what is read of what is left.
|
||||
#! @param this The code.
|
||||
#! @returns Whether anything changed.
|
||||
pub def code::trim_bits(&mut this): bool {
|
||||
# Only narrowing, masking, comparing, and rounding are simplified, so code
|
||||
# holding none has nothing to do.
|
||||
var any := false;
|
||||
var scan := this.walk_all();
|
||||
while scan.more() && !any {
|
||||
match this.insts[scan.next()].kind {
|
||||
op::wrap, op::band, op::bor, op::bxor, op::fsingle,
|
||||
op::eq, op::ne, op::ltu, op::leu, op::lts, op::les => any = true;
|
||||
else => {}
|
||||
}
|
||||
}
|
||||
return false if !any;
|
||||
|
||||
var order := vector::<u32>::new(scratch());
|
||||
var number := vector::<u32>::new(scratch());
|
||||
this.block_order(&mut order, &mut number);
|
||||
var known := rewrite::new();
|
||||
var masks := false;
|
||||
for &block in order {
|
||||
var walk := this.walk(block);
|
||||
while walk.more() {
|
||||
masks = this.trim_known(walk.next(), number.span, &mut known) || masks;
|
||||
}
|
||||
}
|
||||
this.apply(&known);
|
||||
return known.changed if !masks;
|
||||
|
||||
var read := rewrite::new();
|
||||
val needs := this.needed_bits(order.span, number.span);
|
||||
for &block in order {
|
||||
var walk := this.walk(block);
|
||||
while walk.more() {
|
||||
this.trim_unread(walk.next(), &needs, &mut read);
|
||||
}
|
||||
}
|
||||
this.apply(&read);
|
||||
return known.changed || read.changed;
|
||||
}
|
||||
|
||||
|
||||
# What is known to be zero.
|
||||
|
||||
#! The bits of a word known to be zero, worked out from what made it, and what
|
||||
#! made that in turn, as LLVM's known bits works them out: as far back as
|
||||
#! `bits_depth` steps, and through no more than `bits_budget` words in all, a
|
||||
#! block's parameter knowing what every edge reaching it gives it. Past those,
|
||||
#! a word is taken to be anything, as the function's own parameters are.
|
||||
#! @param this The code.
|
||||
#! @param value The word.
|
||||
#! @param depth How many steps back it is from what was asked.
|
||||
#! @param number Each block's place in the order the blocks run in.
|
||||
#! @param budget How many more words may be looked at.
|
||||
#! @returns The bits.
|
||||
def code::zeros_at(&this, value: u32, depth: u32, number: span<u32>, budget: &mut u32): u64 {
|
||||
return 0 if value == 0 || depth >= bits_depth || budget == 0 || this.origins[value].kind != vt::word;
|
||||
budget = budget - 1;
|
||||
val from: origin = this.origins[value];
|
||||
return this.zeros_of(from.from, depth, number, budget) if !from.param;
|
||||
return 0 if from.from == 0;
|
||||
var bits := all_bits;
|
||||
for &edge in this.blocks[from.from].preds {
|
||||
continue if number[this.edge_source(edge)] == 0xFFFFFFFF;
|
||||
val given: u32 = this.edges[edge].args[from.index];
|
||||
# A loop giving it back as it took it changes nothing of it.
|
||||
continue if given == value;
|
||||
bits = bits & this.zeros_at(given, depth + 1, number, budget);
|
||||
return 0 if bits == 0;
|
||||
}
|
||||
return bits;
|
||||
}
|
||||
|
||||
#! The bits known to be zero of the word an instruction gives, from those of
|
||||
#! what it takes.
|
||||
#! @param this The code.
|
||||
#! @param index The instruction.
|
||||
#! @param depth How many steps back it is from what was asked.
|
||||
#! @param number Each block's place in the order the blocks run in.
|
||||
#! @param budget How many more words may be looked at.
|
||||
#! @returns The bits: none where it gives no word.
|
||||
def code::zeros_of(&this, index: u32, depth: u32, number: span<u32>, budget: &mut u32): u64 {
|
||||
val held: inst = this.insts[index];
|
||||
match held.kind {
|
||||
op::iconst => return ~held.imm;
|
||||
op::eq, op::ne, op::lts, op::ltu, op::les, op::leu, op::eqsu, op::ltsu, op::lesu, op::ltus, op::leus,
|
||||
op::feq, op::fne, op::flt, op::fle, op::lnot => return ~1u;
|
||||
op::ones, op::clz, op::ctz => return ~127u;
|
||||
op::load => {
|
||||
return match held.aux as access {
|
||||
access::u8 => ~low_ones(8),
|
||||
access::u16 => ~low_ones(16),
|
||||
access::u32 => ~low_ones(32),
|
||||
else => 0
|
||||
};
|
||||
}
|
||||
op::band, op::bor, op::bxor, op::select, op::shl, op::shru, op::shrs,
|
||||
op::add, op::sub, op::mul, op::divu, op::remu, op::wrap => {}
|
||||
else => return 0;
|
||||
}
|
||||
# A shift by a constant past the word's bits is taken to be unknown.
|
||||
var by: u64 = 0;
|
||||
val constant := held.argc == 2 && this.word_constant(this.arg(index, 1), &mut by);
|
||||
if !constant {
|
||||
by = 64;
|
||||
}
|
||||
val a := this.zeros_at(this.arg(index, held.kind == op::select ? 1 : 0), depth + 1, number, budget);
|
||||
var b: u64 = 0;
|
||||
if held.argc >= 2 && held.kind != op::shl && held.kind != op::shru && held.kind != op::shrs && held.kind != op::wrap {
|
||||
b = this.zeros_at(this.arg(index, held.kind == op::select ? 2 : 1), depth + 1, number, budget);
|
||||
}
|
||||
match held.kind {
|
||||
op::band => return a | b;
|
||||
op::bor, op::bxor, op::select => return a & b;
|
||||
op::shl => return by < 64 ? (a << by) | low_ones(by) : 0;
|
||||
op::shru => return by < 64 ? (a >> by) | high_ones(by) : 0;
|
||||
op::shrs => return by < 64 ? ((a as i64) >> by) as u64 : 0;
|
||||
op::add => {
|
||||
# A sum carries one bit past the higher of what it adds.
|
||||
val low := trailing_zeros_of(~a) < trailing_zeros_of(~b) ? trailing_zeros_of(~a) : trailing_zeros_of(~b);
|
||||
val high := leading_zeros_of(~a) < leading_zeros_of(~b) ? leading_zeros_of(~a) : leading_zeros_of(~b);
|
||||
return low_ones(low) | high_ones(high == 0 ? 0 : high - 1);
|
||||
}
|
||||
op::sub => {
|
||||
val low := trailing_zeros_of(~a) < trailing_zeros_of(~b) ? trailing_zeros_of(~a) : trailing_zeros_of(~b);
|
||||
return low_ones(low);
|
||||
}
|
||||
op::mul => {
|
||||
# A product of words of p and q bits holds p + q bits at most.
|
||||
val low := trailing_zeros_of(~a) + trailing_zeros_of(~b);
|
||||
val wide := (64 - leading_zeros_of(~a)) + (64 - leading_zeros_of(~b));
|
||||
return low_ones(low) | (wide < 64 ? high_ones(64 - wide) : 0);
|
||||
}
|
||||
op::divu => return high_ones(leading_zeros_of(~a));
|
||||
op::remu => {
|
||||
# What is left is below what divides, and no more than what is divided.
|
||||
var high := leading_zeros_of(~a);
|
||||
if constant && by != 0 {
|
||||
val under := leading_zeros_of(by - 1);
|
||||
high = under > high ? under : high;
|
||||
}
|
||||
return high_ones(high);
|
||||
}
|
||||
op::wrap => {
|
||||
val kept := low_ones(held.aux as u64);
|
||||
return a | ~kept if held.imm == 0 || held.aux == 0;
|
||||
# A narrowing keeping the sign is zero past the bits it keeps where the
|
||||
# highest of them is.
|
||||
val sign := 1u << (held.aux as u64 - 1);
|
||||
return (a & sign) != 0 ? a | ~kept : a & (kept >> 1);
|
||||
}
|
||||
else => return 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#! The bits of a word known to be zero, as asked where the pass simplifies.
|
||||
#! @param this The code.
|
||||
#! @param value The word.
|
||||
#! @param number Each block's place in the order the blocks run in.
|
||||
#! @returns The bits.
|
||||
def code::known_zeros(&this, value: u32, number: span<u32>): u64 {
|
||||
var budget := bits_budget;
|
||||
return this.zeros_at(value, 0, number, &mut budget);
|
||||
}
|
||||
|
||||
#! Simplifies an instruction by what is known to be zero of the words it takes,
|
||||
#! as the file says, where what it gives is then known exactly.
|
||||
#! @param this The code.
|
||||
#! @param index The instruction.
|
||||
#! @param number Each block's place in the order the blocks run in.
|
||||
#! @param done What the pass changes.
|
||||
#! @returns Whether it is a narrowing or mask by a constant that stays, which
|
||||
#! what is read of what it gives may yet take out.
|
||||
def code::trim_known(&mut this, index: u32, number: span<u32>, done: &mut rewrite): bool {
|
||||
val held: inst = this.insts[index];
|
||||
return false if held.count != 1;
|
||||
match held.kind {
|
||||
op::wrap => {
|
||||
return false if held.aux >= 64 || held.aux == 0;
|
||||
val a := this.arg(index, 0);
|
||||
val kept := low_ones(held.imm != 0 ? held.aux as u64 - 1 : held.aux as u64);
|
||||
if (this.known_zeros(a, number) | kept) == all_bits {
|
||||
this.forward_to(index, a, done);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
op::band, op::bor, op::bxor => {
|
||||
var mask: u64 = 0;
|
||||
return false if !this.word_constant(this.arg(index, 1), &mut mask);
|
||||
val a := this.arg(index, 0);
|
||||
if held.kind == op::band && (this.known_zeros(a, number) | mask) == all_bits {
|
||||
this.forward_to(index, a, done);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
op::fsingle => {
|
||||
# A word of 24 bits or fewer made a real is an `f32` already.
|
||||
val made := this.defining(this.arg(index, 0));
|
||||
return false if made == 0 || (this.insts[made].kind != op::itof && this.insts[made].kind != op::utof);
|
||||
if (this.known_zeros(this.arg(made, 0), number) | low_ones(24)) == all_bits {
|
||||
this.forward_to(index, this.arg(index, 0), done);
|
||||
}
|
||||
}
|
||||
op::eq, op::ne, op::ltu, op::leu, op::lts, op::les => this.decide_order(index, number, done);
|
||||
else => {}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#! Decides a comparison of a word with a constant by what is known to be zero
|
||||
#! of the word: an equality where the constant has a bit the word has not, and
|
||||
#! an order where every value the word may hold is on one side.
|
||||
#! @param this The code.
|
||||
#! @param index The comparison.
|
||||
#! @param number Each block's place in the order the blocks run in.
|
||||
#! @param done What the pass changes.
|
||||
def code::decide_order(&mut this, index: u32, number: span<u32>, done: &mut rewrite): void {
|
||||
val kind := this.insts[index].kind;
|
||||
val a := this.arg(index, 0);
|
||||
val b := this.arg(index, 1);
|
||||
var c: u64 = 0;
|
||||
var left := false;
|
||||
if this.word_constant(b, &mut c) {
|
||||
left = false;
|
||||
} else if this.word_constant(a, &mut c) {
|
||||
left = true;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
# The greatest the other may be, which is also its greatest as a signed word
|
||||
# where its sign is known to be zero.
|
||||
val most := ~this.known_zeros(left ? b : a, number);
|
||||
return if most == all_bits;
|
||||
val positive := (most >> 63) == 0;
|
||||
var decided: u64 = 2;
|
||||
match kind {
|
||||
op::eq => decided = (c & ~most) != 0 ? 0 : 2;
|
||||
op::ne => decided = (c & ~most) != 0 ? 1 : 2;
|
||||
op::ltu => decided = left ? (c >= most ? 0 : 2) : (most < c ? 1 : 2);
|
||||
op::leu => decided = left ? (c > most ? 0 : 2) : (most <= c ? 1 : 2);
|
||||
op::lts, op::les => {
|
||||
# A word whose sign is zero is from 0 to the greatest it may be, so above
|
||||
# a negative constant, and not below 0.
|
||||
return if !positive;
|
||||
val strict := kind == op::lts;
|
||||
val negative := (c >> 63) != 0;
|
||||
if left {
|
||||
if negative || (!strict && c == 0) {
|
||||
decided = 1;
|
||||
} else if strict ? c >= most : c > most {
|
||||
decided = 0;
|
||||
}
|
||||
} else {
|
||||
if negative || (strict && c == 0) {
|
||||
decided = 0;
|
||||
} else if strict ? most < c : most <= c {
|
||||
decided = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
else => {}
|
||||
}
|
||||
return if decided == 2;
|
||||
this.become_word(index, decided);
|
||||
done.changed = true;
|
||||
}
|
||||
|
||||
|
||||
# What is read.
|
||||
|
||||
#! Whether an instruction's operands are read only as what it gives is: an
|
||||
#! operator of words whose bits are worked out from those of what it takes, so
|
||||
#! what of those is read follows from what is read of it.
|
||||
#! @param this The code.
|
||||
#! @param index The instruction.
|
||||
#! @returns Whether they are.
|
||||
def code::passes_bits(&this, index: u32): bool {
|
||||
val held := &this.insts[index];
|
||||
return false if held.count != 1 || this.origins[held.result].kind != vt::word;
|
||||
return match held.kind {
|
||||
op::add, op::sub, op::mul, op::neg, op::band, op::bor, op::bxor, op::bnot,
|
||||
op::shl, op::shru, op::shrs, op::wrap, op::select => true,
|
||||
else => false
|
||||
};
|
||||
}
|
||||
|
||||
#! The bits of each word of the code that anything reads, by value, found from
|
||||
#! what reads words through other than the operators passing bits on, as
|
||||
#! comparisons, calls, and returns do, then through those operators and the
|
||||
#! edges giving blocks' parameters, back to what made each word.
|
||||
#! @param this The code.
|
||||
#! @param order The blocks in the order they run.
|
||||
#! @param number Each block's place in that order.
|
||||
#! @returns The bits, in scratch memory.
|
||||
def code::needed_bits(&this, order: span<u32>, number: span<u32>): vector<u64> {
|
||||
var needs := vector::<u64>::new(scratch());
|
||||
needs.resize(this.origins.length);
|
||||
var todo := vector::<u32>::new(scratch());
|
||||
for &block in order {
|
||||
var walk := this.walk(block);
|
||||
while walk.more() {
|
||||
val at := walk.next();
|
||||
continue if this.passes_bits(at);
|
||||
val held: inst = this.insts[at];
|
||||
for i in 0..held.argc {
|
||||
var bits := all_bits;
|
||||
# A store of fewer than 8 bytes writes only the low bits of its word.
|
||||
if held.kind == op::store && i == 1 {
|
||||
match held.aux as access {
|
||||
access::i8, access::u8, access::i16, access::u16, access::i32, access::u32 => {
|
||||
bits = low_ones(8 * (held.aux as access).width());
|
||||
}
|
||||
else => {}
|
||||
}
|
||||
}
|
||||
need_bits(this.arg(at, i as u32), bits, &mut needs, &mut todo);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
while todo.length != 0 {
|
||||
val value: u32 = todo.last;
|
||||
todo.truncate();
|
||||
val bits: u64 = needs[value];
|
||||
val from: origin = this.origins[value];
|
||||
if from.param {
|
||||
for &edge in this.blocks[from.from].preds {
|
||||
continue if number[this.edge_source(edge)] == 0xFFFFFFFF;
|
||||
need_bits(this.edges[edge].args[from.index], bits, &mut needs, &mut todo);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
continue if from.from == 0 || !this.passes_bits(from.from);
|
||||
val at := from.from;
|
||||
for i in 0..this.insts[at].argc {
|
||||
need_bits(this.arg(at, i as u32), this.passed_bits(at, i as u32, bits), &mut needs, &mut todo);
|
||||
}
|
||||
}
|
||||
return needs;
|
||||
}
|
||||
|
||||
#! Notes that bits of a word are read, so what made it reads what those need.
|
||||
#! @param value The word.
|
||||
#! @param bits The bits read.
|
||||
#! @param needs The bits read of each value.
|
||||
#! @param todo The values whose bits read grew, which what made them is
|
||||
#! asked of again for.
|
||||
def need_bits(value: u32, bits: u64, needs: &mut vector<u64>, todo: &mut vector<u32>): void {
|
||||
return if value == 0;
|
||||
val grown := needs[value] | bits;
|
||||
return if grown == needs[value];
|
||||
needs[value] = grown;
|
||||
todo.append(value);
|
||||
}
|
||||
|
||||
#! The bits of an operand an operator passing bits on reads, where some bits of
|
||||
#! what it gives are read.
|
||||
#! @param this The code.
|
||||
#! @param index The instruction.
|
||||
#! @param operand Which operand.
|
||||
#! @param bits The bits read of what it gives.
|
||||
#! @returns The bits of the operand read.
|
||||
def code::passed_bits(&this, index: u32, operand: u32, bits: u64): u64 {
|
||||
val held: inst = this.insts[index];
|
||||
var other: u64 = 0;
|
||||
val known := held.argc == 2 && this.word_constant(this.arg(index, 1 - operand), &mut other);
|
||||
match held.kind {
|
||||
op::add, op::sub, op::mul, op::neg => return up_to_highest(bits);
|
||||
op::band => return known ? bits & other : bits;
|
||||
op::bor => return known ? bits & ~other : bits;
|
||||
op::bxor, op::bnot => return bits;
|
||||
op::shl => {
|
||||
return all_bits if operand == 1;
|
||||
return known && other < 64 ? bits >> other : up_to_highest(bits);
|
||||
}
|
||||
op::shru => {
|
||||
return all_bits if operand == 1 || !known || other >= 64;
|
||||
return bits << other;
|
||||
}
|
||||
op::shrs => {
|
||||
# What a shift keeping the sign moves in is the sign.
|
||||
return all_bits if operand == 1 || !known || other >= 64;
|
||||
return (bits << other) | ((bits & high_ones(other)) != 0 ? 1u << 63 : 0);
|
||||
}
|
||||
op::select => return operand == 0 ? all_bits : bits;
|
||||
op::wrap => {
|
||||
# What a narrowing keeping the sign gives past its bits is the highest of
|
||||
# them.
|
||||
val kept := low_ones(held.aux as u64);
|
||||
return bits if held.aux >= 64;
|
||||
val sign: u64 = held.imm != 0 && held.aux > 0 && (bits & ~kept) != 0 ? 1u << (held.aux as u64 - 1) : 0;
|
||||
return (bits & kept) | sign;
|
||||
}
|
||||
else => return all_bits;
|
||||
}
|
||||
return all_bits;
|
||||
}
|
||||
|
||||
#! Simplifies an instruction by what is read of what it gives, as the file
|
||||
#! says: a narrowing, or a mask, setting, or flipping of bits, that changes
|
||||
#! nothing read of it is what it takes.
|
||||
#! @param this The code.
|
||||
#! @param index The instruction.
|
||||
#! @param needs The bits read of each value.
|
||||
#! @param done What the pass changes.
|
||||
def code::trim_unread(&mut this, index: u32, needs: &vector<u64>, done: &mut rewrite): void {
|
||||
val held: inst = this.insts[index];
|
||||
return if held.count != 1 || this.origins[held.result].kind != vt::word;
|
||||
val read: u64 = needs[held.result];
|
||||
match held.kind {
|
||||
op::wrap => {
|
||||
if held.aux < 64 && (read & ~low_ones(held.aux as u64)) == 0 {
|
||||
this.forward_to(index, this.arg(index, 0), done);
|
||||
}
|
||||
}
|
||||
op::band, op::bor, op::bxor => {
|
||||
var mask: u64 = 0;
|
||||
return if !this.word_constant(this.arg(index, 1), &mut mask);
|
||||
val unchanged := held.kind == op::band ? (read & ~mask) == 0 : (read & mask) == 0;
|
||||
if unchanged {
|
||||
this.forward_to(index, this.arg(index, 0), done);
|
||||
}
|
||||
}
|
||||
else => {}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,224 @@
|
||||
#! @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 ¶m 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;
|
||||
}
|
||||
@@ -0,0 +1,592 @@
|
||||
#! @file edit.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains what the passes of the optimizer share to change code: a rewrite,
|
||||
#! which gathers the values a pass replaces and the instructions it takes out
|
||||
#! and applies them in one sweep once the pass is done, and the changes made
|
||||
#! in place, as an instruction becoming another that gives the same values, an
|
||||
#! instruction put first in a block or before another, and a branch whose ways
|
||||
#! out are swapped. Also what the passes ask of code: the constant a value is,
|
||||
#! whether a value is never zero, and how often each value is used.
|
||||
|
||||
module sakura::compiler;
|
||||
|
||||
|
||||
#! What a pass changes as it goes: the value each use of another reads instead,
|
||||
#! and the instructions taken out, which `code::apply` applies in one sweep.
|
||||
pub struct rewrite {
|
||||
var forward: vector<u32>; #! The value read instead of each value, or 0.
|
||||
var removed: vector<bool>; #! Whether each instruction is taken out.
|
||||
var changed: bool; #! Whether the pass changed anything.
|
||||
}
|
||||
|
||||
#! Makes a rewrite that changes nothing yet.
|
||||
#! @returns The rewrite.
|
||||
pub def rewrite::new(): rewrite {
|
||||
return rewrite { forward = vector::<u32>::new(scratch()), removed = vector::<bool>::new(scratch()) };
|
||||
}
|
||||
|
||||
#! The value read where a value is, following each value replaced to what
|
||||
#! replaces it.
|
||||
#! @param this The rewrite.
|
||||
#! @param value The value.
|
||||
#! @returns The value read.
|
||||
pub def rewrite::resolve(&this, value: u32): u32 {
|
||||
return follow_replaced(this.forward.span.data, this.forward.length, value);
|
||||
}
|
||||
|
||||
#! The value read where a value is, following each value replaced to what
|
||||
#! replaces it, until one not replaced, or a million steps, past which the
|
||||
#! replacements are taken to go round.
|
||||
#! @param forward The value replacing each value, by index, or 0 where it is
|
||||
#! not replaced.
|
||||
#! @param count How many values `forward` holds, past which none is
|
||||
#! replaced.
|
||||
#! @param value The value.
|
||||
#! @returns The value read.
|
||||
pub def follow_replaced(forward: *u32, count: u64, value: u32): u32 {
|
||||
var at := value;
|
||||
var steps := 0;
|
||||
while (at as u64) < count && forward[at] != 0 && steps < 1000000 {
|
||||
at = forward[at];
|
||||
steps++;
|
||||
}
|
||||
return at;
|
||||
}
|
||||
|
||||
#! Makes each use of a value read another.
|
||||
#! @param this The rewrite.
|
||||
#! @param from The value replaced.
|
||||
#! @param to The value read instead.
|
||||
pub def rewrite::replace(&mut this, from: u32, to: u32): void {
|
||||
return if from == to || this.resolve(to) == from;
|
||||
if this.forward.length <= from as u64 {
|
||||
this.forward.resize(from as u64 + 1);
|
||||
}
|
||||
this.forward[from] = to;
|
||||
this.changed = true;
|
||||
}
|
||||
|
||||
#! Takes an instruction out of its block.
|
||||
#! @param this The rewrite.
|
||||
#! @param index The instruction.
|
||||
pub def rewrite::remove(&mut this, index: u32): void {
|
||||
if this.removed.length <= index as u64 {
|
||||
this.removed.resize(index as u64 + 1);
|
||||
}
|
||||
this.removed[index] = true;
|
||||
this.changed = true;
|
||||
}
|
||||
|
||||
#! Whether an instruction is taken out.
|
||||
#! @param this The rewrite.
|
||||
#! @param index The instruction.
|
||||
#! @returns Whether it is.
|
||||
pub def rewrite::is_removed(&this, index: u32): bool {
|
||||
return (index as u64) < this.removed.length && this.removed[index];
|
||||
}
|
||||
|
||||
#! Applies a rewrite: each operand and argument reads what replaces the value
|
||||
#! it named, and the instructions taken out leave their blocks.
|
||||
#! @param this The code.
|
||||
#! @param done The rewrite.
|
||||
pub def code::apply(&mut this, done: &rewrite): void {
|
||||
val count := done.forward.length;
|
||||
if count != 0 {
|
||||
# Read and written where the elements are, as this runs over every operand.
|
||||
val forward := done.forward.span.data;
|
||||
val operands := this.operands.slice.data;
|
||||
for i in 0..this.operands.length {
|
||||
operands[i] = follow_replaced(forward, count, operands[i]);
|
||||
}
|
||||
for e in 0..this.edges.length {
|
||||
val args := this.edges[e].args.slice.data;
|
||||
for a in 0..this.edges[e].args.length {
|
||||
args[a] = follow_replaced(forward, count, args[a]);
|
||||
}
|
||||
}
|
||||
# A variable takes what replaces a value it took.
|
||||
for i in 0..this.bindings.length {
|
||||
this.bindings[i].value = follow_replaced(forward, count, this.bindings[i].value);
|
||||
}
|
||||
}
|
||||
this.take_out(done);
|
||||
}
|
||||
|
||||
#! Takes the instructions a rewrite takes out out of their blocks.
|
||||
#! @param this The code.
|
||||
#! @param done The rewrite.
|
||||
pub def code::take_out(&mut this, done: &rewrite): void {
|
||||
return if done.removed.length == 0;
|
||||
|
||||
# Only the blocks holding what is taken out are linked anew.
|
||||
var touched := vector::<bool>::new(scratch());
|
||||
touched.resize(this.blocks.length);
|
||||
for i in 0..done.removed.length {
|
||||
if done.removed[i] && (this.insts[i].block as u64) < touched.length {
|
||||
touched[this.insts[i].block] = true;
|
||||
}
|
||||
}
|
||||
for b in 0..this.blocks.length {
|
||||
continue if this.blocks[b].gone || !touched[b];
|
||||
var walk := this.walk(b as u32);
|
||||
var kept: u32 = 0;
|
||||
this.blocks[b].first = 0;
|
||||
this.blocks[b].last = 0;
|
||||
while walk.more() {
|
||||
val at := walk.next();
|
||||
continue if done.is_removed(at);
|
||||
if kept == 0 {
|
||||
this.blocks[b].first = at;
|
||||
} else {
|
||||
this.insts[kept].next = at;
|
||||
}
|
||||
kept = at;
|
||||
}
|
||||
if kept != 0 {
|
||||
this.insts[kept].next = 0;
|
||||
}
|
||||
this.blocks[b].last = kept;
|
||||
}
|
||||
}
|
||||
|
||||
#! The instruction giving a value, or 0 for one a block takes.
|
||||
#! @param this The code.
|
||||
#! @param value The value.
|
||||
#! @returns The instruction.
|
||||
pub def code::defining(&this, value: u32): u32 {
|
||||
return 0 if value == 0 || (value as u64) >= this.origins.length;
|
||||
val held: origin = this.origins[value];
|
||||
return held.param ? 0 : held.from;
|
||||
}
|
||||
|
||||
#! The block a value is made in: the block taking it, or holding the
|
||||
#! instruction giving it.
|
||||
#! @param this The code.
|
||||
#! @param value The value.
|
||||
#! @returns The block.
|
||||
pub def code::home_of(&this, value: u32): u32 {
|
||||
val held: origin = this.origins[value];
|
||||
return held.param ? held.from : this.insts[held.from].block;
|
||||
}
|
||||
|
||||
#! What `code::cell_of` gives for a value that is the address of no cell.
|
||||
pub const no_cell: u32 = 0xFFFFFFFF;
|
||||
|
||||
#! The cell a value is the address of, as the instruction giving it says.
|
||||
#! @param this The code.
|
||||
#! @param value The value.
|
||||
#! @returns The cell, or `no_cell`.
|
||||
pub def code::cell_of(&this, value: u32): u32 {
|
||||
val index := this.defining(value);
|
||||
return no_cell if index == 0 || this.insts[index].kind != op::cell;
|
||||
return this.insts[index].aux;
|
||||
}
|
||||
|
||||
#! Which cell each value of code is the address of, found once from the `cell`
|
||||
#! instructions of the blocks not removed, for a pass that asks it of many
|
||||
#! values.
|
||||
pub struct cell_table {
|
||||
var of: vector<u32>; #! One more than the cell each value is the address of, or 0.
|
||||
var any: bool; #! Whether any value is.
|
||||
}
|
||||
|
||||
#! Finds which cell each value is the address of.
|
||||
#! @param this The code.
|
||||
#! @returns The table, in scratch memory.
|
||||
pub def code::cell_table(&this): cell_table {
|
||||
var made := cell_table { of = vector::<u32>::new(scratch()) };
|
||||
# Code with no cells has no value that is the address of one.
|
||||
return made if this.cells.length == 0;
|
||||
made.of.resize(this.origins.length);
|
||||
var walk := this.walk_all();
|
||||
while walk.more() {
|
||||
val held := &this.insts[walk.next()];
|
||||
if held.kind == op::cell {
|
||||
made.of[held.result] = held.aux + 1;
|
||||
made.any = true;
|
||||
}
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! The cell a value is the address of, as the table found it.
|
||||
#! @param this The table.
|
||||
#! @param value The value.
|
||||
#! @returns One more than the cell, or 0, as for a value made after the table.
|
||||
pub def cell_table::cell(&this, value: u32): u32 {
|
||||
return (value as u64) < this.of.length ? this.of[value] : 0;
|
||||
}
|
||||
|
||||
#! Whether a value is a constant word, and its bits.
|
||||
#! @param this The code.
|
||||
#! @param value The value.
|
||||
#! @param bits Where its bits are written, where it is one.
|
||||
#! @returns Whether it is.
|
||||
pub def code::word_constant(&this, value: u32, bits: &mut u64): bool {
|
||||
val index := this.defining(value);
|
||||
return false if index == 0 || this.insts[index].kind != op::iconst;
|
||||
bits = this.insts[index].imm;
|
||||
return true;
|
||||
}
|
||||
|
||||
#! Whether a value is a constant real, and its bits.
|
||||
#! @param this The code.
|
||||
#! @param value The value.
|
||||
#! @param bits Where the bits of the `f64` are written, where it is one.
|
||||
#! @returns Whether it is.
|
||||
pub def code::real_constant(&this, value: u32, bits: &mut u64): bool {
|
||||
val index := this.defining(value);
|
||||
return false if index == 0 || this.insts[index].kind != op::fconst;
|
||||
bits = this.insts[index].imm;
|
||||
return true;
|
||||
}
|
||||
|
||||
#! The type of the constant a value is, and its bits, read where the
|
||||
#! instruction making it lies, taken out of its block or not, as a binding
|
||||
#! naming the value keeps the constant once that instruction goes.
|
||||
#! @param this The code.
|
||||
#! @param value The value.
|
||||
#! @param bits Where its bits are written, where it is one.
|
||||
#! @returns Its type, or `none` where it is no constant.
|
||||
pub def code::constant_kind(&this, value: u32, bits: &mut u64): vt {
|
||||
return vt::word if this.word_constant(value, bits);
|
||||
return vt::real if this.real_constant(value, bits);
|
||||
return vt::none;
|
||||
}
|
||||
|
||||
#! Whether a word is never zero: the address of a cell, a global, or a datum,
|
||||
#! or a small step past one.
|
||||
#! @param this The code.
|
||||
#! @param value The word.
|
||||
#! @returns Whether it is never zero.
|
||||
pub def code::never_zero(&this, value: u32): bool {
|
||||
var at := value;
|
||||
var steps := 0;
|
||||
while steps < 8 {
|
||||
val index := this.defining(at);
|
||||
return false if index == 0;
|
||||
val held: inst = this.insts[index];
|
||||
match held.kind {
|
||||
op::cell, op::global, op::datum => return true;
|
||||
op::iconst => return held.imm != 0;
|
||||
op::add => {
|
||||
var step: u64 = 0;
|
||||
return false if !this.word_constant(this.arg(index, 1), &mut step) || step >= 0x100000000u;
|
||||
at = this.arg(index, 0);
|
||||
}
|
||||
else => return false;
|
||||
}
|
||||
steps++;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#! How many times each value is read, by operands and by the arguments of
|
||||
#! edges, in the blocks not removed.
|
||||
#! @param this The code.
|
||||
#! @returns The count of each value.
|
||||
pub def code::use_counts(&this): vector<u32> {
|
||||
var counts := vector::<u32>::new(scratch());
|
||||
counts.resize(this.origins.length);
|
||||
var walk := this.walk_all();
|
||||
var reads := this.reader();
|
||||
while walk.more() {
|
||||
reads.start(walk.next());
|
||||
while reads.more() {
|
||||
val value := reads.next();
|
||||
counts[value] = counts[value] + 1;
|
||||
}
|
||||
}
|
||||
return counts;
|
||||
}
|
||||
|
||||
#! Makes an instruction another that gives the same values, in place, so what
|
||||
#! reads them reads what it gives now.
|
||||
#! @param this The code.
|
||||
#! @param index The instruction.
|
||||
#! @param kind What it does now.
|
||||
#! @param args Its operands now.
|
||||
#! @param aux What its kind says `aux` holds.
|
||||
#! @param imm What its kind says `imm` holds.
|
||||
pub def code::become(&mut this, index: u32, kind: op, args: span<u32>, aux: u32, imm: u64): void {
|
||||
# Operands fit where its old ones were, or else go after every other.
|
||||
var first: u32 = this.insts[index].args;
|
||||
if args.length > this.insts[index].argc as u64 {
|
||||
first = this.operands.length as u32;
|
||||
this.operands.resize(this.operands.length + args.length);
|
||||
}
|
||||
for i in 0..args.length {
|
||||
this.operands[first as u64 + i] = args[i];
|
||||
}
|
||||
this.insts[index].kind = kind;
|
||||
this.insts[index].args = first;
|
||||
this.insts[index].argc = args.length as u32;
|
||||
this.insts[index].aux = aux;
|
||||
this.insts[index].imm = imm;
|
||||
}
|
||||
|
||||
#! Makes an instruction the constant word it gives.
|
||||
#! @param this The code.
|
||||
#! @param index The instruction.
|
||||
#! @param bits The word's bits.
|
||||
pub def code::become_word(&mut this, index: u32, bits: u64): void {
|
||||
this.become(index, op::iconst, span::<u32>::blank(), 0, bits);
|
||||
this.origins[this.insts[index].result].kind = vt::word;
|
||||
}
|
||||
|
||||
#! Makes an instruction the constant real it gives.
|
||||
#! @param this The code.
|
||||
#! @param index The instruction.
|
||||
#! @param bits The bits of the `f64` it is.
|
||||
pub def code::become_real(&mut this, index: u32, bits: u64): void {
|
||||
this.become(index, op::fconst, span::<u32>::blank(), 0, bits);
|
||||
}
|
||||
|
||||
#! Adds an instruction, in no block yet.
|
||||
#! @param this The code.
|
||||
#! @param kind What it does.
|
||||
#! @param args Its operands.
|
||||
#! @param gives The types of its results, in order.
|
||||
#! @param aux What its kind says `aux` holds.
|
||||
#! @param imm What its kind says `imm` holds.
|
||||
#! @param pos Where in the source it is from.
|
||||
#! @param site One more than the site of the function it was inlined from,
|
||||
#! or 0.
|
||||
#! @returns Its index.
|
||||
pub def code::detached(
|
||||
&mut this,
|
||||
kind: op,
|
||||
args: span<u32>,
|
||||
gives: span<vt>,
|
||||
aux: u32,
|
||||
imm: u64,
|
||||
pos: u64,
|
||||
site: u32
|
||||
): u32 {
|
||||
val index := this.insts.length as u32;
|
||||
val first := this.operands.length as u32;
|
||||
# Appended one by one, which is inlined, where `extend` is a call for each
|
||||
# instruction.
|
||||
for &each in args {
|
||||
this.operands.append(each);
|
||||
}
|
||||
val result := this.origins.length as u32;
|
||||
for i in 0..gives.length {
|
||||
this.origins.append(origin { kind = gives[i], from = index, index = i as u32 });
|
||||
}
|
||||
this.insts.append(inst {
|
||||
kind = kind,
|
||||
count = gives.length as u8,
|
||||
aux = aux,
|
||||
imm = imm,
|
||||
args = first,
|
||||
argc = args.length as u32,
|
||||
result = gives.length == 0 ? 0 : result,
|
||||
at = pos,
|
||||
site = site
|
||||
});
|
||||
return index;
|
||||
}
|
||||
|
||||
#! Puts an instruction made in no block before another in its block.
|
||||
#! @param this The code.
|
||||
#! @param index The instruction.
|
||||
#! @param before The instruction it goes before.
|
||||
pub def code::link_before(&mut this, index: u32, before: u32): void {
|
||||
val block: u32 = this.insts[before].block;
|
||||
val prior := this.before(before);
|
||||
this.insts[index].block = block;
|
||||
this.insts[index].next = before;
|
||||
if prior == 0 {
|
||||
this.blocks[block].first = index;
|
||||
} else {
|
||||
this.insts[prior].next = index;
|
||||
}
|
||||
}
|
||||
|
||||
#! The instruction before another in its block.
|
||||
#! @param this The code.
|
||||
#! @param index The instruction, which its block holds.
|
||||
#! @returns The instruction before it, or 0 where it is the first.
|
||||
pub def code::before(&this, index: u32): u32 {
|
||||
var prior: u32 = 0;
|
||||
var walk := this.walk(this.insts[index].block);
|
||||
while walk.more() {
|
||||
val at := walk.next();
|
||||
break if at == index;
|
||||
prior = at;
|
||||
}
|
||||
return prior;
|
||||
}
|
||||
|
||||
#! Puts an instruction made in no block after another in its block.
|
||||
#! @param this The code.
|
||||
#! @param index The instruction.
|
||||
#! @param after The instruction it goes after.
|
||||
pub def code::link_after(&mut this, index: u32, after: u32): void {
|
||||
val block: u32 = this.insts[after].block;
|
||||
this.insts[index].block = block;
|
||||
this.insts[index].next = this.insts[after].next;
|
||||
this.insts[after].next = index;
|
||||
if this.blocks[block].last == after {
|
||||
this.blocks[block].last = index;
|
||||
}
|
||||
}
|
||||
|
||||
#! A constant word, made first in the entry, where it stands before everything
|
||||
#! that may read it.
|
||||
#! @param this The code.
|
||||
#! @param bits Its bits.
|
||||
#! @returns The value.
|
||||
pub def code::word_value(&mut this, bits: u64): u32 {
|
||||
var gives: vt[1] = [vt::word];
|
||||
val index := this.detached(op::iconst, span::<u32>::blank(), gives[0:1], 0, bits, 0, 0);
|
||||
this.put_first(0, index);
|
||||
return this.insts[index].result;
|
||||
}
|
||||
|
||||
#! A constant real, made first in the entry.
|
||||
#! @param this The code.
|
||||
#! @param bits The bits of the `f64` it is.
|
||||
#! @returns The value.
|
||||
pub def code::real_value(&mut this, bits: u64): u32 {
|
||||
var gives: vt[1] = [vt::real];
|
||||
val index := this.detached(op::fconst, span::<u32>::blank(), gives[0:1], 0, bits, 0, 0);
|
||||
this.put_first(0, index);
|
||||
return this.insts[index].result;
|
||||
}
|
||||
|
||||
#! The zero of a type of value, made first in the entry: the word 0, the real
|
||||
#! 0, or a vector of zero bytes.
|
||||
#! @param this The code.
|
||||
#! @param kind The type.
|
||||
#! @returns The value.
|
||||
pub def code::zero_value(&mut this, kind: vt): u32 {
|
||||
return this.real_value(0) if kind == vt::real;
|
||||
return this.word_value(0) if !kind.lanes();
|
||||
var gives: vt[1] = [kind];
|
||||
val index := this.detached(op::vzero, span::<u32>::blank(), gives[0:1], 0, 0, 0, 0);
|
||||
this.put_first(0, index);
|
||||
return this.insts[index].result;
|
||||
}
|
||||
|
||||
#! The zero of each type of value a pass asks for, each made once, as
|
||||
#! `code::zero_value` makes it.
|
||||
pub struct zero_values {
|
||||
var held: u32[vt::count as u64]; #! The zero of each type made, or 0.
|
||||
}
|
||||
|
||||
#! The zero of a type of value, made first in the entry the first time it is
|
||||
#! asked for.
|
||||
#! @param this The zeros.
|
||||
#! @param body The code.
|
||||
#! @param kind The type.
|
||||
#! @returns The value.
|
||||
pub def zero_values::of(&mut this, body: &mut code, kind: vt): u32 {
|
||||
val at := kind as u64;
|
||||
if this.held[at] == 0 {
|
||||
this.held[at] = body.zero_value(kind);
|
||||
}
|
||||
return this.held[at];
|
||||
}
|
||||
|
||||
#! Takes an edge out of the edges into its block.
|
||||
#! @param this The code.
|
||||
#! @param edge The edge.
|
||||
pub def code::drop_pred(&mut this, edge: u32): void {
|
||||
this.forget_order();
|
||||
val target: u32 = this.edges[edge].target;
|
||||
var kept := ids {};
|
||||
for &each in this.blocks[target].preds {
|
||||
if each != edge {
|
||||
kept.append(each);
|
||||
}
|
||||
}
|
||||
this.blocks[target].preds = move(&mut kept);
|
||||
}
|
||||
|
||||
#! Takes out the parameters of a block that do not stay, with the argument each
|
||||
#! edge into the block gives them, numbering those left in the order they were.
|
||||
#! @param this The code.
|
||||
#! @param block The block.
|
||||
#! @param keeps Whether each of its parameters stays, by its position.
|
||||
pub def code::drop_params(&mut this, block: u32, keeps: span<bool>): void {
|
||||
var kept := ids {};
|
||||
for p in 0..keeps.length {
|
||||
continue if !keeps[p];
|
||||
val param: u32 = this.blocks[block].params[p];
|
||||
this.origins[param].index = kept.length as u32;
|
||||
kept.append(param);
|
||||
}
|
||||
for &edge in this.blocks[block].preds.span {
|
||||
var args := ids {};
|
||||
for p in 0..keeps.length {
|
||||
if keeps[p] {
|
||||
args.append(this.edges[edge].args[p]);
|
||||
}
|
||||
}
|
||||
this.edges[edge].args = move(&mut args);
|
||||
}
|
||||
this.blocks[block].params = move(&mut kept);
|
||||
}
|
||||
|
||||
#! Makes an instruction ending a block go along one of its edges only, as a
|
||||
#! `jump`, taking the others out of the blocks they went to.
|
||||
#! @param this The code.
|
||||
#! @param index The instruction.
|
||||
#! @param kept The edge it goes along.
|
||||
pub def code::jump_only(&mut this, index: u32, kept: u32): void {
|
||||
this.forget_order();
|
||||
var outs := this.edges_of(index);
|
||||
while outs.more() {
|
||||
val edge := outs.next();
|
||||
if edge != kept {
|
||||
this.drop_pred(edge);
|
||||
}
|
||||
}
|
||||
this.become(index, op::jump, span::<u32>::blank(), kept, 0);
|
||||
}
|
||||
|
||||
#! Swaps where a `branch` goes, so it goes where it went where its condition
|
||||
#! was zero where it is not, as a branch on a condition's opposite does.
|
||||
#! @param this The code.
|
||||
#! @param index The `branch`.
|
||||
pub def code::swap_branch(&mut this, index: u32): void {
|
||||
this.forget_order();
|
||||
val one: u32 = this.insts[index].aux;
|
||||
val two := one + 1;
|
||||
val first: u32 = this.edges[one].target;
|
||||
val second: u32 = this.edges[two].target;
|
||||
var args := move(&mut this.edges[one].args);
|
||||
this.edges[one].args = move(&mut this.edges[two].args);
|
||||
this.edges[two].args = move(&mut args);
|
||||
this.edges[one].target = second;
|
||||
this.edges[two].target = first;
|
||||
return if first == second;
|
||||
for i in 0..this.blocks[first].preds.length {
|
||||
if this.blocks[first].preds[i] == one {
|
||||
this.blocks[first].preds[i] = two;
|
||||
}
|
||||
}
|
||||
for i in 0..this.blocks[second].preds.length {
|
||||
if this.blocks[second].preds[i] == two {
|
||||
this.blocks[second].preds[i] = one;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#! How many instructions the code holds, in the blocks not removed.
|
||||
#! @param this The code.
|
||||
#! @returns How many.
|
||||
pub def code::size(&this): u32 {
|
||||
var count: u32 = 0;
|
||||
var walk := this.walk_all();
|
||||
while walk.more() {
|
||||
walk.next();
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,207 @@
|
||||
#! @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;
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,500 @@
|
||||
#! @file memory.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the eighth pass of the optimizer, which cleans up what the code does
|
||||
#! with the memory of its cells, of which building values and inlining leave
|
||||
#! much. Where each cell is known to hold zero is followed through the blocks,
|
||||
#! from what the frame zeroes and what the blocks before each leave: clearing
|
||||
#! what is zero already, or storing zero to it, goes; copying from what is zero
|
||||
#! clears instead; and reading it reads the zero. A clear whose bytes are all
|
||||
#! written before anything reads them goes. Then a copy into the memory a
|
||||
#! function returns, right before it returns, is returned from where it was
|
||||
#! copied from.
|
||||
#!
|
||||
#! A cell whose address goes anywhere but to the instructions reading, writing,
|
||||
#! copying, and clearing it, or to a call as where to write what it returns, is
|
||||
#! escaped, as alias analysis finds: anything that may read or write memory the
|
||||
#! code does not name may read or write it, but for what reaches through an
|
||||
#! address outside the frame.
|
||||
|
||||
module sakura::compiler;
|
||||
|
||||
|
||||
#! The most words of a cell the pass follows, a bit each.
|
||||
const memory_words: u64 = 64;
|
||||
|
||||
#! What stands for a cell the pass does not follow.
|
||||
const unfollowed: u32 = 0xFFFFFFFF;
|
||||
|
||||
#! The most words the ends of the blocks may take, past which no block begins
|
||||
#! knowing what the blocks before it left.
|
||||
const memory_ends_limit: u64 = 1048576;
|
||||
|
||||
#! What the pass knows of the cells of the code.
|
||||
struct memory_known {
|
||||
var addresses: cell_table; #! Which cell each value is the address of.
|
||||
var escaped: vector<bool>; #! Whether each cell's address goes where it may be read or written without the code saying.
|
||||
var place: vector<u32>; #! The place of each cell among those followed, or `unfollowed`.
|
||||
var cells: vector<u32>; #! The cell followed at each place.
|
||||
var exposed: vector<u32>; #! The places of the escaped cells followed.
|
||||
var zero: vector<u64>; #! The words known zero of each cell followed, where the walk is, a bit each.
|
||||
var clears: vector<u32>; #! Of each cell followed, the clear of it nothing has read since, or 0.
|
||||
var cleared: vector<u64>; #! The words that clear zeroes.
|
||||
var covered: vector<u64>; #! The words of it written whole since.
|
||||
var ends: vector<u64>; #! The words known zero where each block ends, a word for each place, block after block.
|
||||
var walked: vector<bool>; #! Whether each block was walked.
|
||||
var zeros: zero_values; #! The zero of each type, made once.
|
||||
}
|
||||
|
||||
#! The place among those followed of the cell an address is.
|
||||
#! @param this What the pass knows.
|
||||
#! @param value The address.
|
||||
#! @returns The place, or `unfollowed`.
|
||||
def memory_known::followed(&this, value: u32): u32 {
|
||||
val cell := this.addresses.cell(value);
|
||||
return cell == 0 ? unfollowed : this.place[cell - 1];
|
||||
}
|
||||
|
||||
#! Says a cell was read, so the clear of it nothing had read stays.
|
||||
#! @param this What the pass knows.
|
||||
#! @param place The cell's place.
|
||||
def memory_known::read(&mut this, place: u32): void {
|
||||
this.clears[place] = 0;
|
||||
}
|
||||
|
||||
#! Says memory the code does not name was read, which may be any escaped cell.
|
||||
#! @param this What the pass knows.
|
||||
def memory_known::read_escaped(&mut this): void {
|
||||
for &k in this.exposed {
|
||||
this.clears[k] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
#! Says memory the code does not name was written, which may be any escaped
|
||||
#! cell, so none is known zero.
|
||||
#! @param this What the pass knows.
|
||||
def memory_known::forget_escaped(&mut this): void {
|
||||
for &k in this.exposed {
|
||||
this.zero[k] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
#! Says words of a cell were written whole, taking out the clear of it they
|
||||
#! write over all of.
|
||||
#! @param this What the pass knows.
|
||||
#! @param place The cell's place.
|
||||
#! @param words The words, a bit each.
|
||||
#! @param done What the pass changes.
|
||||
def memory_known::cover(&mut this, place: u32, words: u64, done: &mut rewrite): void {
|
||||
return if this.clears[place] == 0;
|
||||
this.covered[place] = this.covered[place] | words;
|
||||
if (this.covered[place] & this.cleared[place]) == this.cleared[place] {
|
||||
done.remove(this.clears[place]);
|
||||
this.clears[place] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
#! Says a cell was cleared by an instruction that stays.
|
||||
#! @param this What the pass knows.
|
||||
#! @param place The cell's place.
|
||||
#! @param index The clear.
|
||||
#! @param size How many bytes it clears.
|
||||
#! @param done What the pass changes.
|
||||
def memory_known::clear(&mut this, place: u32, index: u32, size: u64, done: &mut rewrite): void {
|
||||
val whole := whole_words(0, size);
|
||||
this.cover(place, whole, done);
|
||||
this.clears[place] = index;
|
||||
this.cleared[place] = touched_words(0, size);
|
||||
this.covered[place] = 0;
|
||||
this.zero[place] = this.zero[place] | whole;
|
||||
}
|
||||
|
||||
#! The words bytes touch, a bit each.
|
||||
#! @param offset Where they begin.
|
||||
#! @param size How many there are.
|
||||
#! @returns The words.
|
||||
def touched_words(offset: u64, size: u64): u64 {
|
||||
return 0 if size == 0;
|
||||
return 0xFFFFFFFFFFFFFFFFu if offset + size > memory_words * word_size;
|
||||
val first := offset / word_size;
|
||||
val last := (offset + size - 1) / word_size;
|
||||
return words_between(first, last + 1);
|
||||
}
|
||||
|
||||
#! The words bytes cover whole, a bit each.
|
||||
#! @param offset Where they begin.
|
||||
#! @param size How many there are.
|
||||
#! @returns The words.
|
||||
def whole_words(offset: u64, size: u64): u64 {
|
||||
return 0 if offset + size > memory_words * word_size;
|
||||
val first := (offset + word_size - 1) / word_size;
|
||||
val end := (offset + size) / word_size;
|
||||
return first >= end ? 0 : words_between(first, end);
|
||||
}
|
||||
|
||||
#! The words from one to before another, a bit each.
|
||||
#! @param first The first.
|
||||
#! @param end The one after the last.
|
||||
#! @returns The words.
|
||||
def words_between(first: u64, end: u64): u64 {
|
||||
val high := end >= 64 ? 0xFFFFFFFFFFFFFFFFu : (1u << end) - 1;
|
||||
return high & ~((1u << first) - 1);
|
||||
}
|
||||
|
||||
#! Whether an address is within no cell: a step from a global, a datum, what
|
||||
#! the program holds on the heap, or an address the function was given, which
|
||||
#! points outside its frame.
|
||||
#! @param this The code.
|
||||
#! @param address The address.
|
||||
#! @returns Whether it is.
|
||||
def code::outside_cells(&this, address: u32): bool {
|
||||
var at := address;
|
||||
for _ in 0..8 {
|
||||
val index := this.defining(at);
|
||||
if index == 0 {
|
||||
val held: origin = this.origins[at];
|
||||
return held.param && held.from == 0;
|
||||
}
|
||||
match this.insts[index].kind {
|
||||
op::global, op::datum, op::allocate => return true;
|
||||
op::add => at = this.arg(index, 0);
|
||||
else => return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#! Whether a value is a word or real all of whose bits are zero.
|
||||
#! @param this The code.
|
||||
#! @param value The value.
|
||||
#! @returns Whether it is.
|
||||
def code::zero_bits(&this, value: u32): bool {
|
||||
var bits: u64 = 1;
|
||||
val origin: origin = this.origins[value];
|
||||
return true if !origin.param && this.insts[origin.from].kind == op::vzero;
|
||||
return (this.word_constant(value, &mut bits) || this.real_constant(value, &mut bits)) && bits == 0;
|
||||
}
|
||||
|
||||
#! Cleans up the memory of the cells, 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 changed.
|
||||
pub def code::clean_memory(&mut this, writes: span<u64>): bool {
|
||||
return false if this.cells.length == 0;
|
||||
var state := memory_known {
|
||||
addresses = this.cell_table(),
|
||||
escaped = vector::<bool>::new(scratch()),
|
||||
place = vector::<u32>::new(scratch()),
|
||||
cells = vector::<u32>::new(scratch()),
|
||||
exposed = vector::<u32>::new(scratch()),
|
||||
zero = vector::<u64>::new(scratch()),
|
||||
clears = vector::<u32>::new(scratch()),
|
||||
cleared = vector::<u64>::new(scratch()),
|
||||
covered = vector::<u64>::new(scratch()),
|
||||
ends = vector::<u64>::new(scratch()),
|
||||
walked = vector::<bool>::new(scratch())
|
||||
};
|
||||
return false if !state.addresses.any;
|
||||
state.escaped = this.escaped_cells(&state.addresses, writes);
|
||||
|
||||
state.place.resize(this.cells.length, unfollowed);
|
||||
for c in 0..this.cells.length {
|
||||
continue if this.cells[c].size as u64 > memory_words * word_size;
|
||||
state.place[c] = state.cells.length as u32;
|
||||
if state.escaped[c] {
|
||||
state.exposed.append(state.cells.length as u32);
|
||||
}
|
||||
state.cells.append(c as u32);
|
||||
}
|
||||
val count := state.cells.length;
|
||||
state.zero.resize(count);
|
||||
state.clears.resize(count);
|
||||
state.cleared.resize(count);
|
||||
state.covered.resize(count);
|
||||
|
||||
var done := rewrite::new();
|
||||
var order := vector::<u32>::new(scratch());
|
||||
var number := vector::<u32>::new(scratch());
|
||||
this.block_order(&mut order, &mut number);
|
||||
val inherits := count != 0 && this.blocks.length * count <= memory_ends_limit;
|
||||
if inherits {
|
||||
state.ends.resize(this.blocks.length * count);
|
||||
state.walked.resize(this.blocks.length);
|
||||
}
|
||||
for &block in order {
|
||||
continue if count == 0;
|
||||
this.begin_memory(block, &mut state, inherits);
|
||||
this.walk_memory(block, &mut state, writes, &mut done);
|
||||
if inherits {
|
||||
for k in 0..count {
|
||||
state.ends[block as u64 * count + k] = state.zero[k];
|
||||
}
|
||||
state.walked[block] = true;
|
||||
}
|
||||
}
|
||||
this.apply(&done);
|
||||
|
||||
var returns := rewrite::new();
|
||||
this.forward_returns(&mut returns);
|
||||
this.apply(&returns);
|
||||
return done.changed || returns.changed;
|
||||
}
|
||||
|
||||
#! Begins what is known zero where a block begins: every word of each cell the
|
||||
#! frame zeroes at the entry, and elsewhere what every block going there left,
|
||||
#! where each was walked.
|
||||
#! @param this The code.
|
||||
#! @param block The block.
|
||||
#! @param state What the pass knows.
|
||||
#! @param inherits Whether what the blocks before it left is known.
|
||||
def code::begin_memory(&this, block: u32, state: &mut memory_known, inherits: bool): void {
|
||||
val count := state.cells.length;
|
||||
for k in 0..count {
|
||||
state.zero[k] = 0;
|
||||
state.clears[k] = 0;
|
||||
}
|
||||
val preds := &this.blocks[block].preds;
|
||||
if block == 0 && preds.length == 0 {
|
||||
for k in 0..count {
|
||||
val held: cell = this.cells[state.cells[k]];
|
||||
if held.kind == ck::temporary || held.kind == ck::local || held.kind == ck::environment {
|
||||
state.zero[k] = touched_words(0, held.size as u64);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
return if !inherits || preds.length == 0;
|
||||
for &edge in preds.span {
|
||||
return if !state.walked[this.edge_source(edge)];
|
||||
}
|
||||
for k in 0..count {
|
||||
state.zero[k] = 0xFFFFFFFFFFFFFFFFu;
|
||||
}
|
||||
for &edge in preds.span {
|
||||
val from := this.edge_source(edge) as u64;
|
||||
for k in 0..count {
|
||||
state.zero[k] = state.zero[k] & state.ends[from * count + k];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#! Walks the instructions of a block, as the file says.
|
||||
#! @param this The code.
|
||||
#! @param block The block.
|
||||
#! @param state What the pass knows.
|
||||
#! @param writes Of each instruction, the bytes a call writes what it returns
|
||||
#! to at its last operand, or 0.
|
||||
#! @param done What the pass changes.
|
||||
def code::walk_memory(&mut this, block: u32, state: &mut memory_known, writes: span<u64>, done: &mut rewrite): void {
|
||||
var walk := this.walk(block);
|
||||
while walk.more() {
|
||||
val at := walk.next();
|
||||
val held: inst = this.insts[at];
|
||||
match held.kind {
|
||||
op::load => this.memory_load(at, &held, state, done);
|
||||
op::store => this.memory_store(at, &held, state, done);
|
||||
op::clear => this.memory_clear(at, &held, state, done);
|
||||
op::copy => this.memory_copy(at, &held, state, done);
|
||||
else => this.memory_other(at, &held, state, writes, done);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#! Follows a read: of a cell's words known zero, it is that zero.
|
||||
#! @param this The code.
|
||||
#! @param index The `load`.
|
||||
#! @param held The instruction.
|
||||
#! @param state What the pass knows.
|
||||
#! @param done What the pass changes.
|
||||
def code::memory_load(&mut this, index: u32, held: &inst, state: &mut memory_known, done: &mut rewrite): void {
|
||||
val address := this.arg(index, 0);
|
||||
if state.addresses.cell(address) == 0 {
|
||||
if !this.outside_cells(address) {
|
||||
state.read_escaped();
|
||||
}
|
||||
return;
|
||||
}
|
||||
val k := state.followed(address);
|
||||
return if k == unfollowed;
|
||||
state.read(k);
|
||||
val words := touched_words(held.imm, (held.aux as access).width());
|
||||
return if (state.zero[k] & words) != words;
|
||||
done.replace(held.result, state.zeros.of(this, this.type_of(held.result)));
|
||||
done.remove(index);
|
||||
}
|
||||
|
||||
#! Follows a write of a word: zero to words known zero goes.
|
||||
#! @param this The code.
|
||||
#! @param index The `store`.
|
||||
#! @param held The instruction.
|
||||
#! @param state What the pass knows.
|
||||
#! @param done What the pass changes.
|
||||
def code::memory_store(&mut this, index: u32, held: &inst, state: &mut memory_known, done: &mut rewrite): void {
|
||||
val address := this.arg(index, 0);
|
||||
if state.addresses.cell(address) == 0 {
|
||||
if !this.outside_cells(address) {
|
||||
state.forget_escaped();
|
||||
}
|
||||
return;
|
||||
}
|
||||
val k := state.followed(address);
|
||||
return if k == unfollowed;
|
||||
val width := (held.aux as access).width();
|
||||
val words := touched_words(held.imm, width);
|
||||
val whole := whole_words(held.imm, width);
|
||||
if this.zero_bits(this.arg(index, 1)) {
|
||||
if (state.zero[k] & words) == words {
|
||||
done.remove(index);
|
||||
return;
|
||||
}
|
||||
state.zero[k] = state.zero[k] | whole;
|
||||
} else {
|
||||
state.zero[k] = state.zero[k] & ~words;
|
||||
}
|
||||
state.cover(k, whole, done);
|
||||
}
|
||||
|
||||
#! Follows a clear: of words known zero already, it goes.
|
||||
#! @param this The code.
|
||||
#! @param index The `clear`.
|
||||
#! @param held The instruction.
|
||||
#! @param state What the pass knows.
|
||||
#! @param done What the pass changes.
|
||||
def code::memory_clear(&mut this, index: u32, held: &inst, state: &mut memory_known, done: &mut rewrite): void {
|
||||
val address := this.arg(index, 0);
|
||||
if state.addresses.cell(address) == 0 {
|
||||
if !this.outside_cells(address) {
|
||||
state.forget_escaped();
|
||||
}
|
||||
return;
|
||||
}
|
||||
val k := state.followed(address);
|
||||
return if k == unfollowed;
|
||||
val words := touched_words(0, held.imm);
|
||||
if (state.zero[k] & words) == words {
|
||||
done.remove(index);
|
||||
return;
|
||||
}
|
||||
state.clear(k, index, held.imm, done);
|
||||
}
|
||||
|
||||
#! Follows a copy: from words known zero, it clears instead, or goes where what
|
||||
#! it writes is known zero already.
|
||||
#! @param this The code.
|
||||
#! @param index The `copy`.
|
||||
#! @param held The instruction.
|
||||
#! @param state What the pass knows.
|
||||
#! @param done What the pass changes.
|
||||
def code::memory_copy(&mut this, index: u32, held: &inst, state: &mut memory_known, done: &mut rewrite): void {
|
||||
val to := this.arg(index, 0);
|
||||
val from := this.arg(index, 1);
|
||||
val words := touched_words(0, held.imm);
|
||||
var zero := false;
|
||||
if state.addresses.cell(from) == 0 {
|
||||
if !this.outside_cells(from) {
|
||||
state.read_escaped();
|
||||
}
|
||||
} else {
|
||||
val s := state.followed(from);
|
||||
if s != unfollowed {
|
||||
zero = (state.zero[s] & words) == words;
|
||||
if !zero {
|
||||
state.read(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if zero {
|
||||
val d := state.followed(to);
|
||||
if d != unfollowed && (state.zero[d] & words) == words {
|
||||
done.remove(index);
|
||||
return;
|
||||
}
|
||||
var args: u32[1] = [to];
|
||||
this.become(index, op::clear, args[0:1], 0, held.imm);
|
||||
done.changed = true;
|
||||
if d != unfollowed {
|
||||
state.clear(d, index, held.imm, done);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if state.addresses.cell(to) == 0 {
|
||||
if !this.outside_cells(to) {
|
||||
state.forget_escaped();
|
||||
}
|
||||
return;
|
||||
}
|
||||
val d := state.followed(to);
|
||||
return if d == unfollowed || zero;
|
||||
state.zero[d] = state.zero[d] & ~words;
|
||||
state.cover(d, whole_words(0, held.imm), done);
|
||||
}
|
||||
|
||||
#! Follows any other instruction: one reading or writing memory the code does
|
||||
#! not name may read or write any escaped cell, and a call writes what it
|
||||
#! returns to the cell its last operand is.
|
||||
#! @param this The code.
|
||||
#! @param index The instruction.
|
||||
#! @param held The instruction.
|
||||
#! @param state What the pass knows.
|
||||
#! @param writes Of each instruction, the bytes a call writes what it returns
|
||||
#! to at its last operand, or 0.
|
||||
#! @param done What the pass changes.
|
||||
def code::memory_other(&mut this, index: u32, held: &inst, state: &mut memory_known, writes: span<u64>, done: &mut rewrite): void {
|
||||
# What a cell's address is given to may read it, and as an operand write it,
|
||||
# but for where a call writes what it returns, which is followed below.
|
||||
val written: u64 = (index as u64) < writes.length ? writes[index] : 0;
|
||||
var reads := this.reads_of(index);
|
||||
while reads.more() {
|
||||
val k := state.followed(reads.next());
|
||||
continue if k == unfollowed;
|
||||
val i := reads.operand();
|
||||
if i == no_operand {
|
||||
state.read(k);
|
||||
continue;
|
||||
}
|
||||
continue if written != 0 && i + 1 == held.argc;
|
||||
state.read(k);
|
||||
state.zero[k] = 0;
|
||||
}
|
||||
val traits := held.kind.traits();
|
||||
if (traits & op_reads) != 0 {
|
||||
state.read_escaped();
|
||||
}
|
||||
if (traits & op_write) != 0 {
|
||||
state.forget_escaped();
|
||||
}
|
||||
if written != 0 {
|
||||
val k := state.followed(this.arg(index, held.argc - 1));
|
||||
if k != unfollowed {
|
||||
state.zero[k] = 0;
|
||||
state.cover(k, whole_words(0, written), done);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#! Returns from where a copy into the memory a function returns, right before
|
||||
#! it returns, was copied from.
|
||||
#! @param this The code.
|
||||
#! @param done What the pass changes.
|
||||
def code::forward_returns(&mut this, done: &mut rewrite): void {
|
||||
return if this.returned == 0;
|
||||
for b in 0..this.blocks.length {
|
||||
continue if this.blocks[b].gone;
|
||||
val last: u32 = this.blocks[b].last;
|
||||
continue if last == 0 || this.insts[last].kind != op::ret || this.insts[last].argc != 1;
|
||||
val before := this.before(last);
|
||||
continue if before == 0 || this.insts[before].kind != op::copy;
|
||||
val returned := this.arg(last, 0);
|
||||
val to := this.arg(before, 0);
|
||||
val from := this.arg(before, 1);
|
||||
continue if this.insts[before].imm < this.returned as u64 || from == returned;
|
||||
continue if to != returned && (this.cell_of(to) == no_cell || this.cell_of(to) != this.cell_of(returned));
|
||||
var args: u32[1] = [from];
|
||||
this.become(last, op::ret, args[0:1], this.insts[last].aux, this.insts[last].imm);
|
||||
done.remove(before);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
#! @file merge.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the merging of the stores filling 16 bytes of a cell into one, of a
|
||||
#! vector built in a register, which an executable's code is given once it is
|
||||
#! optimized. A load reading several stores waits until each reaches the cache,
|
||||
#! where one store of all its bytes is forwarded to it at once, and a struct of
|
||||
#! four `f32`s or two words, as a `vec3` holding its fourth, is copied out 16
|
||||
#! bytes at a time, as it is returned: built a field at a time, the copy waited
|
||||
#! on every field.
|
||||
#!
|
||||
#! The 16 bytes from a multiple of 16 in a cell the frame zeroes are merged
|
||||
#! where every instruction writing them in the code is a store of 4 or 8 bytes,
|
||||
#! all reals or all words, each in one block, and nothing reads them from the
|
||||
#! first to the last; what none writes is zero. The cell's address goes only to
|
||||
#! what reads, writes, copies, clears, and returns it. It is done once nothing
|
||||
#! more is inlined, so a function copying the code holds its fields as values
|
||||
#! still, as promoting holds them, and only in an executable's code, which holds
|
||||
#! vectors.
|
||||
|
||||
module sakura::compiler;
|
||||
|
||||
|
||||
#! What merging knows of a write or read of a cell.
|
||||
struct touch {
|
||||
var at: u32; #! The instruction.
|
||||
var offset: u64; #! The first byte.
|
||||
var size: u64; #! How many bytes.
|
||||
var writes: bool; #! Whether it writes them.
|
||||
var how: access; #! For a store, how it writes them; `access::bytes` for any other.
|
||||
}
|
||||
|
||||
#! Merges the stores filling 16 bytes of a cell, as the file says.
|
||||
#! @param this The code.
|
||||
#! @returns Whether anything changed.
|
||||
pub def code::merge_stores(&mut this): bool {
|
||||
return false if !this.vectors || this.cells.length == 0;
|
||||
val addresses := this.cell_table();
|
||||
return false if !addresses.any;
|
||||
|
||||
# What each cell's address goes to, where it goes only to what reads and
|
||||
# writes it, and the order of each instruction within its block.
|
||||
var touches := vector::<vector<touch>>::new(scratch());
|
||||
touches.resize(this.cells.length);
|
||||
var whole := vector::<bool>::new(scratch());
|
||||
whole.resize(this.cells.length, true);
|
||||
var place := vector::<u32>::new(scratch());
|
||||
place.resize(this.insts.length);
|
||||
for b in 0..this.blocks.length {
|
||||
continue if this.blocks[b].gone;
|
||||
var number: u32 = 0;
|
||||
var walk := this.walk(b as u32);
|
||||
while walk.more() {
|
||||
place[walk.next()] = number;
|
||||
number++;
|
||||
}
|
||||
}
|
||||
var walk := this.walk_all();
|
||||
var reads := this.reader();
|
||||
while walk.more() {
|
||||
val at := walk.next();
|
||||
val held: inst = this.insts[at];
|
||||
reads.start(at);
|
||||
while reads.more() {
|
||||
val cell := addresses.cell(reads.next());
|
||||
continue if cell == 0;
|
||||
val c := (cell - 1) as u64;
|
||||
val i := reads.operand();
|
||||
if i == no_operand {
|
||||
whole[c] = false;
|
||||
} else if held.kind == op::load && i == 0 {
|
||||
touches[c].append(touch { at = at, offset = held.imm, size = (held.aux as access).width(), how = access::bytes });
|
||||
} else if held.kind == op::store && i == 0 {
|
||||
val how := held.aux as access;
|
||||
touches[c].append(touch { at = at, offset = held.imm, size = how.width(), writes = true, how = how });
|
||||
} else if held.kind == op::clear && i == 0 {
|
||||
touches[c].append(touch { at = at, size = held.imm, writes = true, how = access::bytes });
|
||||
} else if held.kind == op::copy && i < 2 {
|
||||
touches[c].append(touch { at = at, size = held.imm, writes = i == 0, how = access::bytes });
|
||||
} else if held.kind == op::ret {
|
||||
touches[c].append(touch { at = at, size = this.cells[c].size as u64, how = access::bytes });
|
||||
} else {
|
||||
whole[c] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var done := rewrite::new();
|
||||
var zeros := zero_values {};
|
||||
for c in 0..this.cells.length {
|
||||
val held: cell = this.cells[c];
|
||||
continue if !whole[c] || (held.kind != ck::temporary && held.kind != ck::local && held.kind != ck::environment);
|
||||
var start: u64 = 0;
|
||||
while start + 16 <= held.size as u64 {
|
||||
this.merge_region(c as u32, start, touches[c].span, place.span, &mut zeros, &mut done);
|
||||
start += 16;
|
||||
}
|
||||
}
|
||||
this.apply(&done);
|
||||
return done.changed;
|
||||
}
|
||||
|
||||
#! Merges the stores filling the 16 bytes of a cell from an offset, where they
|
||||
#! may be merged, as the file says: the last becomes the store of the vector
|
||||
#! of them all, built before it, and the rest go.
|
||||
#! @param this The code.
|
||||
#! @param cell The cell.
|
||||
#! @param start Where the bytes begin.
|
||||
#! @param touches What reads and writes the cell.
|
||||
#! @param place The order of each instruction within its block.
|
||||
#! @param zeros The zero of each type, made once.
|
||||
#! @param done What the pass changes.
|
||||
def code::merge_region(&mut this, cell: u32, start: u64, touches: span<touch>, place: span<u32>, zeros: &mut zero_values, done: &mut rewrite): void {
|
||||
val end := start + 16;
|
||||
var stores := vector::<touch>::new(scratch());
|
||||
var block: u32 = 0;
|
||||
for &each in touches {
|
||||
continue if !each.writes || each.offset >= end || each.offset + each.size <= start;
|
||||
return if each.how == access::bytes || each.offset < start || each.offset + each.size > end;
|
||||
val one: inst = this.insts[each.at];
|
||||
if stores.length == 0 {
|
||||
block = one.block;
|
||||
}
|
||||
# One block's stores, each of a word or a real of the same width, at an
|
||||
# offset of its own, as a struct's fields are written.
|
||||
return if one.block != block || each.size != stores_width(&stores, each.size) || (each.size != 4 && each.size != 8);
|
||||
return if stores.length != 0 && (each.how.value_type() != stores[0].how.value_type());
|
||||
for &other in stores {
|
||||
return if other.offset == each.offset;
|
||||
}
|
||||
stores.append(each);
|
||||
}
|
||||
return if stores.length < 2;
|
||||
var first: u32 = 0xFFFFFFFF;
|
||||
var last: u32 = 0;
|
||||
var last_at: u32 = 0;
|
||||
for &each in stores {
|
||||
val p := place[each.at];
|
||||
first = first < p ? first : p;
|
||||
if p >= last {
|
||||
last = p;
|
||||
last_at = each.at;
|
||||
}
|
||||
}
|
||||
# Nothing reads the bytes between the first store and the last.
|
||||
for &each in touches {
|
||||
continue if each.writes || each.offset >= end || each.offset + each.size <= start;
|
||||
val p := place[each.at];
|
||||
return if this.insts[each.at].block == block && p > first && p < last;
|
||||
}
|
||||
|
||||
val width := stores[0].size;
|
||||
val real := stores[0].how.value_type() == vt::real;
|
||||
val lane: u32 = real ? (width == 4 ? lane_f32 : lane_f64) : (width == 4 ? lane_u32 : lane_u64);
|
||||
val count := 16 / width;
|
||||
var lanes := vector::<u32>::new(scratch());
|
||||
lanes.resize(count, 0);
|
||||
for &each in stores {
|
||||
lanes[(each.offset - start) / width] = this.arg(each.at, 1);
|
||||
}
|
||||
for k in 0..count {
|
||||
if lanes[k] == 0 {
|
||||
lanes[k] = zeros.of(this, real ? vt::real : vt::word);
|
||||
}
|
||||
}
|
||||
val held: inst = this.insts[last_at];
|
||||
var gives: vt[1] = [vt::v128];
|
||||
val pack := this.detached(op::lanes, lanes.span, gives[0:1], lanes_pack, lane as u64 | 16 << 8, held.at, held.site);
|
||||
this.link_before(pack, last_at);
|
||||
var args: u32[2] = [this.arg(last_at, 0), this.insts[pack].result];
|
||||
this.become(last_at, op::store, args[0:2], access::v128 as u32, start);
|
||||
for &each in stores {
|
||||
if each.at != last_at {
|
||||
done.remove(each.at);
|
||||
}
|
||||
}
|
||||
done.changed = true;
|
||||
}
|
||||
|
||||
# The width the stores of a region share: that of the first, or the one given
|
||||
# where there is none yet.
|
||||
def stores_width(stores: &vector<touch>, size: u64): u64 {
|
||||
return stores.length == 0 ? size : stores[0].size;
|
||||
}
|
||||
@@ -0,0 +1,764 @@
|
||||
#! @file numbering.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the sixth pass of the optimizer, value numbering: an instruction
|
||||
#! working out what one before it already worked out, in a block that block
|
||||
#! dominates, reads that instead, as two reads of `v[i]` share one bounds
|
||||
#! check's comparison. A check of a condition a dominating check or branch
|
||||
#! already proved goes, as does a branch's question where it was answered, and
|
||||
#! a `select` on it is what it chooses. Along the way a branch goes where a word
|
||||
#! equals a constant, or a switch goes for a case, the word is read as the
|
||||
#! constant, as LLVM's numbering learns it, so what is worked out of it folds.
|
||||
#! And a read of memory read or written
|
||||
#! before, with nothing that may write it between, as alias analysis tells,
|
||||
#! reads what was read or written: within a block, and from the block that
|
||||
#! dominates it, but for what anything on the way between may write.
|
||||
|
||||
import std::containers;
|
||||
module sakura::compiler;
|
||||
|
||||
|
||||
#! What numbering knows where it is in the walk: the instruction giving each
|
||||
#! value worked out so far, by its hash, and each condition known, with what
|
||||
#! each block added, which leaving it takes back.
|
||||
struct numbering {
|
||||
var table: scoped_dict<u64, u32>; #! The instruction giving each value worked out, by its hash.
|
||||
var facts: scoped_dict<u32, u64>; #! What each condition known is.
|
||||
var equal: scoped_dict<u32, u64>; #! The bits of the constant each word known to be one is, where the way a branch or switch went says so.
|
||||
var constants: dict<u64, u32>; #! The value made first in the entry for each constant a word known to be one was read as.
|
||||
var loads: vector<known_load>; #! The reads and writes remembered in the block.
|
||||
var copies: vector<known_copy>; #! The copies into cells remembered in the block.
|
||||
var writers: runs<u32>; #! The instructions of each block writing memory, in order, a run each.
|
||||
var writer_runs: vector<u32>; #! The run of `writers` of each block, by block, 0 where none writes, or `no_run` where they are not yet listed.
|
||||
var kept: runs<known_load>; #! What each block knew of memory where it ended, a run each.
|
||||
var kept_runs: vector<u32>; #! The run of `kept` each block knew, by block, or 0 where it knew nothing or dominates no block.
|
||||
var held_run: u32; #! The run of `kept` the reads and writes remembered are, where they were inherited whole and nothing changed them since, or `no_run`.
|
||||
var aliases: aliases; #! What may write what.
|
||||
}
|
||||
|
||||
#! A read or write of memory numbering remembers: what is at an address, read
|
||||
#! as an access reads it.
|
||||
struct known_load {
|
||||
var address: u32; #! The address.
|
||||
var how: u32; #! The access.
|
||||
var value: u32; #! What is there.
|
||||
var offset: u64; #! How many bytes past the address it is.
|
||||
var place: region; #! The bytes it is, as alias analysis tells.
|
||||
}
|
||||
|
||||
#! A copy into a cell numbering remembers: the cell holds what is where it
|
||||
#! copied from, until something writes.
|
||||
struct known_copy {
|
||||
var to: u32; #! The cell's address.
|
||||
var from: u32; #! The address it copied from.
|
||||
var size: u64; #! How many bytes it copied.
|
||||
var to_place: region; #! The bytes it wrote, as alias analysis tells.
|
||||
var from_place: region; #! The bytes it read.
|
||||
}
|
||||
|
||||
#! What `numbering::held_run` holds where what is remembered of memory is no
|
||||
#! run kept.
|
||||
const no_run: u32 = 0xFFFFFFFF;
|
||||
|
||||
#! A block on numbering's walk down the tree of which dominates which.
|
||||
struct numbered_block {
|
||||
var table: u64; #! How many changes the table had before the block, which leaving it takes back.
|
||||
var facts: u64; #! How many changes the conditions known had before it.
|
||||
var equal: u64; #! How many changes the words known to be constants had before it.
|
||||
var next: u32; #! The next of the blocks it immediately dominates to walk, or `no_block`.
|
||||
}
|
||||
|
||||
#! Forgets what numbering remembers of memory, as what writes it does.
|
||||
#! @param this What numbering knows.
|
||||
def numbering::forget_memory(&mut this): void {
|
||||
this.loads.clear();
|
||||
this.copies.clear();
|
||||
this.held_run = 0;
|
||||
}
|
||||
|
||||
#! Numbers the values of a function's code, knowing what each call writes
|
||||
#! what it returns to.
|
||||
#! @param this The compiler.
|
||||
#! @param index The function.
|
||||
#! @returns Whether anything changed.
|
||||
def lowerer::number_values(&mut this, index: u32): bool {
|
||||
val writes := this.result_writes(index);
|
||||
return this.program.functions[index].body.number_values(writes.span);
|
||||
}
|
||||
|
||||
#! Numbers the values of the code, as the file says, walking the blocks down
|
||||
#! the tree of which dominates which.
|
||||
#! @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 changed.
|
||||
pub def code::number_values(&mut this, writes: span<u64>): bool {
|
||||
var order := vector::<u32>::new(scratch());
|
||||
var number := vector::<u32>::new(scratch());
|
||||
this.block_order(&mut order, &mut number);
|
||||
val idom := this.block_dominators();
|
||||
|
||||
# The blocks each block immediately dominates.
|
||||
var first := vector::<u32>::new(scratch());
|
||||
var after := vector::<u32>::new(scratch());
|
||||
first.resize(this.blocks.length, no_block);
|
||||
after.resize(this.blocks.length, no_block);
|
||||
var k := order.length;
|
||||
while k > 1 {
|
||||
k--;
|
||||
val block: u32 = order[k];
|
||||
val parent: u32 = idom[block];
|
||||
after[block] = first[parent];
|
||||
first[parent] = block;
|
||||
}
|
||||
|
||||
var state := numbering {
|
||||
table = scoped_dict::<u64, u32>::new(scratch()),
|
||||
facts = scoped_dict::<u32, u64>::new(scratch()),
|
||||
equal = scoped_dict::<u32, u64>::new(scratch()),
|
||||
constants = dict::<u64, u32>::new(scratch()),
|
||||
loads = vector::<known_load>::new(scratch()),
|
||||
copies = vector::<known_copy>::new(scratch()),
|
||||
writers = runs::<u32>::new(scratch()),
|
||||
writer_runs = vector::<u32>::new(scratch()),
|
||||
kept = runs::<known_load>::new(scratch()),
|
||||
kept_runs = vector::<u32>::new(scratch()),
|
||||
held_run = 0,
|
||||
aliases = this.aliases_unfound(writes, idom.span)
|
||||
};
|
||||
state.writer_runs.resize(this.blocks.length, no_run);
|
||||
state.kept_runs.resize(this.blocks.length);
|
||||
var done := rewrite::new();
|
||||
|
||||
# The blocks on the walk, each with where its changes begin.
|
||||
var stack := vector::<numbered_block>::new(scratch());
|
||||
stack.append(numbered_block { next = first[0] });
|
||||
this.number_block(0, &mut state, &mut done);
|
||||
if first[0] != no_block {
|
||||
this.keep_memory(0, &mut state);
|
||||
}
|
||||
while stack.length != 0 {
|
||||
# The top is written before a block is pushed, which may move it.
|
||||
val top := &mut stack.last;
|
||||
val child := top.next;
|
||||
if child != no_block {
|
||||
top.next = after[child];
|
||||
stack.append(numbered_block { table = state.table.mark(), facts = state.facts.mark(), equal = state.equal.mark(), next = first[child] });
|
||||
this.learn_entry(child, &mut state);
|
||||
this.inherit_memory(child, idom[child], &mut state);
|
||||
this.number_block(child, &mut state, &mut done);
|
||||
# Only the blocks a block dominates ask what it knew where it ended.
|
||||
if first[child] != no_block {
|
||||
this.keep_memory(child, &mut state);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
# Leaving a block takes back what it added.
|
||||
state.table.rewind(top.table);
|
||||
state.facts.rewind(top.facts);
|
||||
state.equal.rewind(top.equal);
|
||||
stack.truncate();
|
||||
}
|
||||
|
||||
# Numbering a block had what it reads read what replaces it, and what the
|
||||
# edges leaving it give; so do those of the blocks the entry does not reach,
|
||||
# never numbered, and the variables bound.
|
||||
if done.forward.length != 0 {
|
||||
for b in 0..this.blocks.length {
|
||||
continue if this.blocks[b].gone || number[b] != 0xFFFFFFFF;
|
||||
var walk := this.walk(b as u32);
|
||||
while walk.more() {
|
||||
val at := walk.next();
|
||||
val held: inst = this.insts[at];
|
||||
for i in 0..held.argc {
|
||||
val slot := held.args as u64 + i as u64;
|
||||
this.operands[slot] = done.resolve(this.operands[slot]);
|
||||
}
|
||||
}
|
||||
this.resolve_edges(b as u32, &done);
|
||||
}
|
||||
for i in 0..this.bindings.length {
|
||||
this.bindings[i].value = done.resolve(this.bindings[i].value);
|
||||
}
|
||||
}
|
||||
this.take_out(&done);
|
||||
return done.changed;
|
||||
}
|
||||
|
||||
#! Has the edges leaving a block give what replaces what they give.
|
||||
#! @param this The code.
|
||||
#! @param block The block.
|
||||
#! @param done What the pass changes.
|
||||
def code::resolve_edges(&mut this, block: u32, done: &rewrite): void {
|
||||
var outs := this.edges_of(this.blocks[block].last);
|
||||
while outs.more() {
|
||||
val args := this.edges[outs.next()].args.slice;
|
||||
for a in 0..args.length {
|
||||
args[a] = done.resolve(args[a]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#! Learns what the branch or switch into a block says where only it goes there:
|
||||
#! a branch's condition is not zero along the way it goes where it is not, and
|
||||
#! is zero along the other; along the way it goes where a word equals a
|
||||
#! constant, the word is that constant, as it is along the way a switch goes
|
||||
#! for a case, as LLVM's numbering learns it; and the word is read as the
|
||||
#! constant within, so what is worked out of it folds.
|
||||
#! @param this The code.
|
||||
#! @param block The block.
|
||||
#! @param state What numbering knows.
|
||||
def code::learn_entry(&mut this, block: u32, state: &mut numbering): void {
|
||||
return if this.blocks[block].preds.length != 1;
|
||||
val edge: u32 = this.blocks[block].preds[0];
|
||||
val from: u32 = this.edges[edge].from;
|
||||
if this.insts[from].kind == op::switch {
|
||||
# The last way out is the one for every other word.
|
||||
if edge + 1 < this.insts[from].aux + this.insts[from].imm as u32 {
|
||||
this.learn_equal(this.arg(from, 0), this.edges[edge].case, state);
|
||||
}
|
||||
return;
|
||||
}
|
||||
return if this.insts[from].kind != op::branch;
|
||||
val cond := this.arg(from, 0);
|
||||
val taken := edge == this.insts[from].aux;
|
||||
val made := this.defining(cond);
|
||||
if made != 0 && ((this.insts[made].kind == op::eq && taken) || (this.insts[made].kind == op::ne && !taken)) {
|
||||
var bits: u64 = 0;
|
||||
if this.word_constant(this.arg(made, 1), &mut bits) {
|
||||
this.learn_equal(this.arg(made, 0), bits, state);
|
||||
} else if this.word_constant(this.arg(made, 0), &mut bits) {
|
||||
this.learn_equal(this.arg(made, 1), bits, state);
|
||||
}
|
||||
}
|
||||
return if state.facts.contains(&cond);
|
||||
state.facts.set(cond, taken ? 1 : 0);
|
||||
}
|
||||
|
||||
#! Learns that a word is a constant where the walk is, but for a constant
|
||||
#! itself, or one known already.
|
||||
#! @param this The code.
|
||||
#! @param value The word.
|
||||
#! @param bits The constant.
|
||||
#! @param state What numbering knows.
|
||||
def code::learn_equal(&this, value: u32, bits: u64, state: &mut numbering): void {
|
||||
var held: u64 = 0;
|
||||
return if this.word_constant(value, &mut held) || this.type_of(value) != vt::word || state.equal.contains(&value);
|
||||
state.equal.set(value, bits);
|
||||
}
|
||||
|
||||
#! Reads a value as the constant it is known to be where the walk is, if it
|
||||
#! is known to be one: a constant made first in the entry, once for each.
|
||||
#! @param this The code.
|
||||
#! @param value The value.
|
||||
#! @param state What numbering knows.
|
||||
#! @param done What the pass changes.
|
||||
#! @returns The constant's value, or the value.
|
||||
def code::known_as(&mut this, value: u32, state: &mut numbering, done: &mut rewrite): u32 {
|
||||
return value if !state.equal.contains(&value);
|
||||
val bits := state.equal.get_or(&value, 0);
|
||||
done.changed = true;
|
||||
if state.constants.get(&bits) is some(made) {
|
||||
return made;
|
||||
}
|
||||
val made := this.word_value(bits);
|
||||
state.constants.set(bits, made);
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Begins what numbering knows of memory where a block begins: what the block
|
||||
#! dominating it knew where it ended, but for what anything on a way from
|
||||
#! there to the block may write, and nothing where the ways pass too many
|
||||
#! blocks.
|
||||
#! @param this The code.
|
||||
#! @param block The block.
|
||||
#! @param parent The block dominating it.
|
||||
#! @param state What numbering knows.
|
||||
def code::inherit_memory(&this, block: u32, parent: u32, state: &mut numbering): void {
|
||||
state.forget_memory();
|
||||
val run: u32 = state.kept_runs[parent];
|
||||
return if run == 0;
|
||||
var between := vector::<u32>::new(scratch());
|
||||
return if !this.blocks_between(parent, block, &mut between);
|
||||
state.loads.extend(state.kept.view(run));
|
||||
state.held_run = run;
|
||||
for &each in between {
|
||||
this.list_writers(each, state);
|
||||
for &at in state.writers.view(state.writer_runs[each]) {
|
||||
this.forget_written(at, state);
|
||||
return if state.loads.length == 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#! Lists the instructions of a block writing memory, where numbering it did not
|
||||
#! list them already.
|
||||
#! @param this The code.
|
||||
#! @param block The block.
|
||||
#! @param state What numbering knows.
|
||||
def code::list_writers(&this, block: u32, state: &mut numbering): void {
|
||||
return if state.writer_runs[block] != no_run;
|
||||
var walk := this.walk(block);
|
||||
while walk.more() {
|
||||
val at := walk.next();
|
||||
val kind := this.insts[at].kind;
|
||||
if kind != op::load && (kind.traits() & op_write) != 0 {
|
||||
state.writers.push(at);
|
||||
}
|
||||
}
|
||||
state.writer_runs[block] = state.writers.end();
|
||||
}
|
||||
|
||||
#! Finds the blocks on the ways from where a block ends to where a block it
|
||||
#! dominates begins. A way going round a loop through the block it ends at
|
||||
#! passes that block whole, so it is among them; one coming back through the
|
||||
#! dominating block begins again where that block ends, which knows what it
|
||||
#! knows anew. Only so many blocks are looked through.
|
||||
#! @param this The code.
|
||||
#! @param parent The dominating block.
|
||||
#! @param block The block it dominates.
|
||||
#! @param between Where the blocks are written.
|
||||
#! @returns Whether they are few enough to be looked through.
|
||||
def code::blocks_between(&this, parent: u32, block: u32, between: &mut vector<u32>): bool {
|
||||
val preds := &this.blocks[block].preds;
|
||||
if preds.length == 1 && this.edge_source(preds[0]) == parent {
|
||||
return true;
|
||||
}
|
||||
var todo := vector::<u32>::new(scratch());
|
||||
for &edge in preds.span {
|
||||
todo.append(this.edge_source(edge));
|
||||
}
|
||||
while todo.length != 0 {
|
||||
val at: u32 = todo.last;
|
||||
todo.truncate();
|
||||
continue if at == parent || between.span.contains(&at);
|
||||
return false if between.length >= 32;
|
||||
between.append(at);
|
||||
for &edge in this.blocks[at].preds.span {
|
||||
todo.append(this.edge_source(edge));
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#! Forgets what numbering remembers of memory that an instruction may write.
|
||||
#! @param this The code.
|
||||
#! @param index The instruction.
|
||||
#! @param state What numbering knows.
|
||||
def code::forget_written(&this, index: u32, state: &mut numbering): void {
|
||||
return if state.loads.length == 0 && state.copies.length == 0;
|
||||
val written := state.aliases.writes_of(this, index);
|
||||
this.forget_writing(&written, state);
|
||||
}
|
||||
|
||||
#! Forgets what numbering remembers of memory that what an instruction writes
|
||||
#! may hold.
|
||||
#! @param this The code.
|
||||
#! @param written What the instruction writes.
|
||||
#! @param state What numbering knows.
|
||||
def code::forget_writing(&this, written: &writing, state: &mut numbering): void {
|
||||
return if state.loads.length == 0 && state.copies.length == 0;
|
||||
state.aliases.find_escaped(this);
|
||||
var kept: u64 = 0;
|
||||
for i in 0..state.loads.length {
|
||||
continue if state.aliases.touches(this, written, &state.loads[i].place);
|
||||
if kept != i {
|
||||
state.loads[kept] = state.loads[i];
|
||||
}
|
||||
kept++;
|
||||
}
|
||||
if kept != state.loads.length {
|
||||
state.loads.resize(kept);
|
||||
state.held_run = no_run;
|
||||
}
|
||||
var held: u64 = 0;
|
||||
for i in 0..state.copies.length {
|
||||
val each: known_copy = state.copies[i];
|
||||
continue if state.aliases.touches(this, written, &each.to_place) || state.aliases.touches(this, written, &each.from_place);
|
||||
state.copies[held] = each;
|
||||
held++;
|
||||
}
|
||||
state.copies.resize(held);
|
||||
}
|
||||
|
||||
#! Keeps what numbering knows of memory where a block ends, for the blocks it
|
||||
#! dominates: the run it inherited, where nothing changed it.
|
||||
#! @param this The code.
|
||||
#! @param block The block.
|
||||
#! @param state What numbering knows.
|
||||
def code::keep_memory(&this, block: u32, state: &mut numbering): void {
|
||||
state.kept_runs[block] = state.held_run != no_run ? state.held_run : state.kept.add(state.loads.span);
|
||||
}
|
||||
|
||||
#! Numbers the instructions of a block, in order, from what is known of memory
|
||||
#! where it begins, which then holds what is known where it ends.
|
||||
#! @param this The code.
|
||||
#! @param block The block.
|
||||
#! @param state What numbering knows.
|
||||
#! @param done What the pass changes.
|
||||
def code::number_block(&mut this, block: u32, state: &mut numbering, done: &mut rewrite): void {
|
||||
# What writes memory is listed as it is met, where it is not listed yet.
|
||||
val listing := state.writer_runs[block] == no_run;
|
||||
val constants := state.equal.map.length != 0;
|
||||
var walk := this.walk(block);
|
||||
while walk.more() {
|
||||
val at := walk.next();
|
||||
val held: inst = this.insts[at];
|
||||
for i in 0..held.argc {
|
||||
val slot := held.args as u64 + i as u64;
|
||||
this.operands[slot] = done.resolve(this.operands[slot]);
|
||||
if constants {
|
||||
this.operands[slot] = this.known_as(this.operands[slot], state, done);
|
||||
}
|
||||
}
|
||||
|
||||
match held.kind {
|
||||
op::check => {
|
||||
val cond := this.arg(at, 0);
|
||||
if state.facts.get(&cond) is some(known) {
|
||||
if known != 0 {
|
||||
done.remove(at);
|
||||
}
|
||||
} else {
|
||||
state.facts.set(cond, 1);
|
||||
}
|
||||
}
|
||||
op::branch => {
|
||||
val cond := this.arg(at, 0);
|
||||
if state.facts.get(&cond) is some(known) {
|
||||
# Which cells are escaped is found as the code was, before the
|
||||
# branch no longer reads its condition.
|
||||
state.aliases.find_escaped(this);
|
||||
val bits: u64 = known;
|
||||
this.operands[held.args] = this.word_value(bits != 0 ? 1 : 0);
|
||||
done.changed = true;
|
||||
}
|
||||
}
|
||||
op::load => {
|
||||
this.forward_copy(at, 0, (held.aux as access).width() + held.imm, state, done);
|
||||
this.number_load(at, state, done);
|
||||
}
|
||||
op::copy => this.forward_copy(at, 1, held.imm, state, done);
|
||||
op::select => this.number_select(at, state, done);
|
||||
else => {
|
||||
if this.removable(at) && held.count == 1 {
|
||||
this.number_inst(at, state, done);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# What writes memory makes what was read of what it may write unknown,
|
||||
# but for what a store of a whole word writes.
|
||||
if held.kind != op::load && (held.kind.traits() & op_write) != 0 {
|
||||
if listing {
|
||||
state.writers.push(at);
|
||||
}
|
||||
if held.kind == op::store && (held.aux == access::i64 as u32 || held.aux == access::f64 as u32) {
|
||||
# What a store writes is the region it then holds.
|
||||
val written := state.aliases.writes_of(this, at);
|
||||
this.forget_writing(&written, state);
|
||||
state.held_run = no_run;
|
||||
var base: u32 = 0;
|
||||
val offset := this.address_base(this.arg(at, 0), held.imm, &mut base);
|
||||
state.loads.append(known_load {
|
||||
address = base,
|
||||
how = held.aux,
|
||||
value = this.arg(at, 1),
|
||||
offset = offset,
|
||||
place = written.place
|
||||
});
|
||||
} else {
|
||||
this.forget_written(at, state);
|
||||
}
|
||||
# A cell a copy filled holds what it copied, until something writes.
|
||||
if held.kind == op::copy && this.cell_of(this.arg(at, 0)) != no_cell
|
||||
&& this.cell_of(this.arg(at, 0)) != this.cell_of(this.arg(at, 1)) {
|
||||
val to := this.arg(at, 0);
|
||||
val from := this.arg(at, 1);
|
||||
state.copies.append(known_copy {
|
||||
to = to,
|
||||
from = from,
|
||||
size = held.imm,
|
||||
to_place = state.aliases.region_at(this, to, 0, held.imm),
|
||||
from_place = state.aliases.region_at(this, from, 0, held.imm)
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
if listing {
|
||||
state.writer_runs[block] = state.writers.end();
|
||||
}
|
||||
this.resolve_edges(block, done);
|
||||
if constants {
|
||||
var outs := this.edges_of(this.blocks[block].last);
|
||||
while outs.more() {
|
||||
val edge := outs.next();
|
||||
for a in 0..this.edges[edge].args.length {
|
||||
val known := this.known_as(this.edges[edge].args[a], state, done);
|
||||
var args := this.edges[edge].args.slice;
|
||||
args[a] = known;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#! Numbers a `select`: one on a condition known where it is, as a dominating
|
||||
#! check or branch made it, is what it chooses, and otherwise it is numbered as
|
||||
#! any instruction working out its value is.
|
||||
#! @param this The code.
|
||||
#! @param index The `select`.
|
||||
#! @param state What numbering knows.
|
||||
#! @param done What the pass changes.
|
||||
def code::number_select(&mut this, index: u32, state: &mut numbering, done: &mut rewrite): void {
|
||||
val cond := this.arg(index, 0);
|
||||
if state.facts.get(&cond) is some(known) {
|
||||
val bits: u64 = known;
|
||||
done.replace(this.insts[index].result, this.arg(index, bits != 0 ? 1 : 2));
|
||||
done.remove(index);
|
||||
return;
|
||||
}
|
||||
this.number_inst(index, state, done);
|
||||
}
|
||||
|
||||
#! Reads what a cell a copy filled holds from where it was copied from
|
||||
#! instead, where nothing wrote since: a load from it, or a copy out of it into
|
||||
#! another cell.
|
||||
#! @param this The code.
|
||||
#! @param index The `load` or `copy`.
|
||||
#! @param operand The operand reading the cell.
|
||||
#! @param reach How far past the cell's address it reads.
|
||||
#! @param state What numbering knows.
|
||||
#! @param done What the pass changes.
|
||||
def code::forward_copy(&mut this, index: u32, operand: u32, reach: u64, state: &mut numbering, done: &mut rewrite): void {
|
||||
val address := this.arg(index, operand);
|
||||
for i in 0..state.copies.length {
|
||||
val each := &state.copies[i];
|
||||
continue if each.to != address || reach > each.size;
|
||||
val from := each.from;
|
||||
# A copy out of it goes on only into a cell, which nothing else overlaps.
|
||||
if operand == 1 {
|
||||
val into := this.cell_of(this.arg(index, 0));
|
||||
return if into == no_cell || into == this.cell_of(from);
|
||||
}
|
||||
this.operands[this.insts[index].args as u64 + operand as u64] = from;
|
||||
done.changed = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
#! Numbers a read of memory: one of what the block read or wrote before, with
|
||||
#! nothing writing between, reads that instead, though one reached through a
|
||||
#! step past the address the other was.
|
||||
#! @param this The code.
|
||||
#! @param index The `load`.
|
||||
#! @param state What numbering knows.
|
||||
#! @param done What the pass changes.
|
||||
def code::number_load(&mut this, index: u32, state: &mut numbering, done: &mut rewrite): void {
|
||||
val held: inst = this.insts[index];
|
||||
var address: u32 = 0;
|
||||
val offset := this.address_base(this.arg(index, 0), held.imm, &mut address);
|
||||
for i in 0..state.loads.length {
|
||||
val each := &state.loads[i];
|
||||
continue if each.address != address || each.offset != offset || each.how != held.aux;
|
||||
done.replace(held.result, each.value);
|
||||
done.remove(index);
|
||||
return;
|
||||
}
|
||||
state.held_run = no_run;
|
||||
state.loads.append(known_load {
|
||||
address = address,
|
||||
how = held.aux,
|
||||
value = held.result,
|
||||
offset = offset,
|
||||
place = state.aliases.region_of(this, index, 0)
|
||||
});
|
||||
}
|
||||
|
||||
#! Where a load or store reaches, as a value and how many bytes past it: a step
|
||||
#! of a constant past another value reaches as far past that one, so the bytes
|
||||
#! it reaches are known as the same as those reached from it directly.
|
||||
#! @param this The code.
|
||||
#! @param address The address the load or store is given.
|
||||
#! @param offset How many bytes past it the load or store reaches.
|
||||
#! @param base Where the value it reaches past is written.
|
||||
#! @returns How many bytes past that value it reaches.
|
||||
def code::address_base(&this, address: u32, offset: u64, base: &mut u32): u64 {
|
||||
base = address;
|
||||
val index := this.defining(address);
|
||||
return offset if index == 0 || this.insts[index].kind != op::add;
|
||||
var bits: u64 = 0;
|
||||
return offset if !this.word_constant(this.arg(index, 1), &mut bits);
|
||||
base = this.arg(index, 0);
|
||||
return offset + bits;
|
||||
}
|
||||
|
||||
#! Numbers an instruction that only works out its value: one working out what
|
||||
#! one dominating it did reads that instead.
|
||||
#! @param this The code.
|
||||
#! @param index The instruction.
|
||||
#! @param state What numbering knows.
|
||||
#! @param done What the pass changes.
|
||||
def code::number_inst(&mut this, index: u32, state: &mut numbering, done: &mut rewrite): void {
|
||||
val hash := this.hash_inst(index);
|
||||
if state.table.get(&hash) is some(got) {
|
||||
val other: u32 = got;
|
||||
if this.same_inst(index, other) {
|
||||
done.replace(this.insts[index].result, this.insts[other].result);
|
||||
done.remove(index);
|
||||
return;
|
||||
}
|
||||
}
|
||||
state.table.set(hash, index);
|
||||
}
|
||||
|
||||
#! The hash of what an instruction works out: its operation, what its `aux`
|
||||
#! and `imm` hold, the type of its value, and its operands, in either order
|
||||
#! where the operation does not mind which.
|
||||
#! @param this The code.
|
||||
#! @param index The instruction.
|
||||
#! @returns The hash.
|
||||
def code::hash_inst(&this, index: u32): u64 {
|
||||
val held: inst = this.insts[index];
|
||||
var hash := mix_hash(mix_hash(mix_hash(0xCBF29CE484222325u, held.kind as u64), held.aux as u64), held.imm);
|
||||
hash = mix_hash(hash, this.origins[held.result].kind as u64);
|
||||
if held.argc == 2 && commutes(held.kind) {
|
||||
val a := this.arg(index, 0);
|
||||
val b := this.arg(index, 1);
|
||||
hash = mix_hash(mix_hash(hash, (a < b ? a : b) as u64), (a < b ? b : a) as u64);
|
||||
return hash;
|
||||
}
|
||||
for i in 0..held.argc {
|
||||
hash = mix_hash(hash, this.arg(index, i as u32) as u64);
|
||||
}
|
||||
return mix_hash(hash, held.argc as u64);
|
||||
}
|
||||
|
||||
#! Whether two instructions work out the same. Their values are of one type,
|
||||
#! as a `vzero` of 16 bytes and one of 32 are not the same.
|
||||
#! @param this The code.
|
||||
#! @param a One instruction.
|
||||
#! @param b The other.
|
||||
#! @returns Whether they do.
|
||||
def code::same_inst(&this, a: u32, b: u32): bool {
|
||||
val x: inst = this.insts[a];
|
||||
val y: inst = this.insts[b];
|
||||
return false if x.kind != y.kind || x.aux != y.aux || x.imm != y.imm || x.argc != y.argc;
|
||||
return false if this.origins[x.result].kind != this.origins[y.result].kind;
|
||||
if x.argc == 2 && commutes(x.kind) {
|
||||
val a0 := this.arg(a, 0);
|
||||
val a1 := this.arg(a, 1);
|
||||
val b0 := this.arg(b, 0);
|
||||
val b1 := this.arg(b, 1);
|
||||
return (a0 == b0 && a1 == b1) || (a0 == b1 && a1 == b0);
|
||||
}
|
||||
for i in 0..x.argc {
|
||||
return false if this.arg(a, i as u32) != this.arg(b, i as u32);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#! Mixes a word into a hash.
|
||||
#! @param hash The hash so far.
|
||||
#! @param value The word.
|
||||
#! @returns The hash.
|
||||
def mix_hash(hash: u64, value: u64): u64 {
|
||||
return (hash ^ value) * 0x100000001B3u;
|
||||
}
|
||||
|
||||
#! Moves each constant made within a loop first into the entry, one of each,
|
||||
#! so the machine makes it once a call rather than each time the loop goes
|
||||
#! round, as it does the address of a cell, a global, or a datum. One made
|
||||
#! outside every loop stays, since making it at the entry would make it where
|
||||
#! nothing reads it.
|
||||
#! @param this The code.
|
||||
#! @returns Whether anything changed.
|
||||
pub def code::hoist_constants(&mut this): bool {
|
||||
var order := vector::<u32>::new(scratch());
|
||||
var number := vector::<u32>::new(scratch());
|
||||
this.block_order(&mut order, &mut number);
|
||||
val looped := this.loop_blocks(order.span, number.span);
|
||||
|
||||
var done := rewrite::new();
|
||||
var words := dict::<u64, u32>::new(scratch());
|
||||
var reals := dict::<u64, u32>::new(scratch());
|
||||
var moved := vector::<u32>::new(scratch());
|
||||
for &block in order {
|
||||
continue if block != 0 && !looped[block];
|
||||
var walk := this.walk(block);
|
||||
while walk.more() {
|
||||
val at := walk.next();
|
||||
val held: inst = this.insts[at];
|
||||
if block != 0 && (held.kind == op::cell || held.kind == op::global || held.kind == op::datum) {
|
||||
done.remove(at);
|
||||
moved.append(at);
|
||||
} else if held.kind == op::iconst || held.kind == op::fconst {
|
||||
val seen := held.kind == op::iconst ? &mut words : &mut reals;
|
||||
if seen.get(&held.imm) is some(got) {
|
||||
done.replace(held.result, got);
|
||||
done.remove(at);
|
||||
} else {
|
||||
seen.set(held.imm, held.result);
|
||||
if block != 0 {
|
||||
done.remove(at);
|
||||
moved.append(at);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
this.apply(&done);
|
||||
var i := moved.length;
|
||||
while i > 0 {
|
||||
i--;
|
||||
this.put_first(0, moved[i]);
|
||||
}
|
||||
return done.changed;
|
||||
}
|
||||
|
||||
#! Which blocks are within a loop: those from which an edge going back to a
|
||||
#! block dominating them is reached, without passing that block, and that
|
||||
#! block.
|
||||
#! @param this The code.
|
||||
#! @param order The blocks in reverse postorder.
|
||||
#! @param number Each block's place in that order.
|
||||
#! @returns Whether each block is within a loop.
|
||||
pub def code::loop_blocks(&mut this, order: span<u32>, number: span<u32>): vector<bool> {
|
||||
var looped := vector::<bool>::new(scratch());
|
||||
looped.resize(this.blocks.length);
|
||||
|
||||
# The dominators are asked for only where an edge may go back.
|
||||
var back := false;
|
||||
for &block in order {
|
||||
for &edge in this.blocks[block].preds {
|
||||
back = back || number[this.edge_source(edge)] >= number[block];
|
||||
}
|
||||
}
|
||||
return looped if !back;
|
||||
val idom := this.block_dominators();
|
||||
var todo := vector::<u32>::new(scratch());
|
||||
for &block in order {
|
||||
for &edge in this.blocks[block].preds {
|
||||
val from := this.edge_source(edge);
|
||||
continue if number[from] < number[block] || !dominates(&idom, block, from);
|
||||
# A loop headed by the block: walk back from where it goes back.
|
||||
looped[block] = true;
|
||||
todo.append(from);
|
||||
while todo.length != 0 {
|
||||
val at: u32 = todo.last;
|
||||
todo.truncate();
|
||||
continue if at == block || looped[at] && at != from;
|
||||
looped[at] = true;
|
||||
for &inner in this.blocks[at].preds {
|
||||
val back := this.edge_source(inner);
|
||||
if back != block && !looped[back] {
|
||||
todo.append(back);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return looped;
|
||||
}
|
||||
@@ -0,0 +1,601 @@
|
||||
#! @file optimizer.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the optimizer, which runs passes over the code of each function
|
||||
#! once the program is lowered. Each pass is small, in a file of its own, and
|
||||
#! leaves the code well formed, which the verifier checks after each where
|
||||
#! lowering verifies. `-O0` runs none, `-O1` simplification, control flow
|
||||
#! cleanup, value numbering, dead code removal, code lifting, bit tracking,
|
||||
#! and the passes over loops and memory, and `-O2` those with inlining and the
|
||||
#! promotion of cells to values. Below `-O2`, calls of
|
||||
#! `@inline` functions are inlined all the same, and nothing else is, so at
|
||||
#! `-O0` the code is as written but for those, each local of the code copied
|
||||
#! still in a cell of its own. The slots the passes leave are compacted away
|
||||
#! at the end, which the backends and the functions inlining the code read.
|
||||
#! Functions are optimized after the functions they call, so a function
|
||||
#! inlined is already optimized.
|
||||
#! At `-O2`, a function of an executable asking which vector instructions the
|
||||
#! processor has is given a version for x86-64-v3's, as `versions.sakura` says.
|
||||
|
||||
import std::containers;
|
||||
module sakura::compiler;
|
||||
|
||||
|
||||
#! The passes, as a problem a pass leaves names them.
|
||||
pub enum pass : u8 {
|
||||
simplify, #! Folding constants and simplifying operations.
|
||||
dead, #! Taking out what nothing needs.
|
||||
control, #! Cleaning up how control flows.
|
||||
inline, #! Inlining calls of small functions.
|
||||
promote, #! Promoting cells to values.
|
||||
numbering, #! Numbering values.
|
||||
hoist, #! Moving constants into the entry.
|
||||
memory, #! Cleaning up the memory of cells.
|
||||
loops, #! Rotating loops and moving out of them what they work out the same each time round.
|
||||
sink, #! Moving what is read past one way out of a block into that way.
|
||||
bits, #! Simplifying by the bits of words known to be zero, and those read.
|
||||
lift, #! Moving what both ways out of a branch work out alike before it.
|
||||
count, #! How many passes there are; not a pass itself.
|
||||
}
|
||||
|
||||
#! The name of a pass, as a problem names it.
|
||||
#! @param this The pass.
|
||||
#! @returns Its name.
|
||||
pub def pass::name(&this): strview {
|
||||
return match this {
|
||||
pass::simplify => "simplification",
|
||||
pass::dead => "dead code removal",
|
||||
pass::control => "control flow cleanup",
|
||||
pass::inline => "inlining",
|
||||
pass::promote => "promotion",
|
||||
pass::numbering => "value numbering",
|
||||
pass::hoist => "constant hoisting",
|
||||
pass::memory => "memory cleanup",
|
||||
pass::loops => "loop optimization",
|
||||
pass::sink => "code sinking",
|
||||
pass::bits => "bit tracking",
|
||||
pass::lift => "code lifting",
|
||||
else => "a pass"
|
||||
};
|
||||
}
|
||||
|
||||
#! Optimizes every function lowered and not yet optimized, as its level says,
|
||||
#! each after those it calls, but for those calling each other, which go in the
|
||||
#! order met. The order is worked out first, walking the calls, and a function
|
||||
#! inlines only those before it, so what each is made into is the same however
|
||||
#! many threads optimize them: as many as the options say work at once, each
|
||||
#! taking a function once those before it that it calls are done. A function
|
||||
#! the one calling it alone copies whole is optimized there, so only the passes
|
||||
#! up to inlining run on it before, and the rest after, where something still
|
||||
#! reaches it, as a cold call does. The versions of those asking which
|
||||
#! instructions the processor has are made after, in the order, as each reads
|
||||
#! what came before its function.
|
||||
#! @param this The compiler.
|
||||
pub def lowerer::optimize(&mut this): void {
|
||||
return if this.options.level == 0 && !this.copies_marked();
|
||||
var plan := this.plan_optimizing();
|
||||
val count := plan.order.length;
|
||||
|
||||
# What verifying each function finds, kept by its place in the order, so it
|
||||
# is noted in that order whichever thread found it.
|
||||
var found := vector::<vector<string>>::new();
|
||||
found.resize(count);
|
||||
val compiler := &mut this;
|
||||
share_out_ordered(count, this.options.threads, &plan.waiting, def(place: u64): void {
|
||||
compiler.optimize_at(&plan, place, &mut found[place]);
|
||||
});
|
||||
|
||||
# Those left for the function calling them that something still reaches, as
|
||||
# one whose call is cold, have the rest of their passes run, each on its own,
|
||||
# whichever optimizing left them.
|
||||
var finishing := this.left_unfinished();
|
||||
var alone := groups::<u32>::new(0);
|
||||
alone.reset(finishing.length);
|
||||
alone.seal();
|
||||
var noted := vector::<vector<string>>::new();
|
||||
noted.resize(finishing.length);
|
||||
share_out_ordered(finishing.length, this.options.threads, &alone, def(k: u64): void {
|
||||
compiler.finish_unfinished(finishing[k], &plan.ranks, &mut noted[k]);
|
||||
});
|
||||
for &each in found {
|
||||
for &text in each {
|
||||
this.problems.append(string::copy_of(&text));
|
||||
}
|
||||
}
|
||||
for &each in noted {
|
||||
for &text in each {
|
||||
this.problems.append(string::copy_of(&text));
|
||||
}
|
||||
}
|
||||
|
||||
if this.versions_made() {
|
||||
for place in 0..count {
|
||||
this.make_version(plan.order[place], &mut plan.ranks, (2 * place + 3) as u32);
|
||||
this.program.functions[plan.order[place]].unoptimized = code{};
|
||||
}
|
||||
}
|
||||
this.merge_all();
|
||||
}
|
||||
|
||||
#! Merges the stores filling 16 bytes of a cell, in each function of an
|
||||
#! executable optimized, once nothing more is inlined, as `merge.sakura` says.
|
||||
#! @param this The compiler.
|
||||
def lowerer::merge_all(&mut this): void {
|
||||
for i in 0..this.program.functions.length {
|
||||
val function := &mut this.program.functions[i];
|
||||
continue if !function.optimized || !function.body.vectors;
|
||||
val point := mark_scratch();
|
||||
if function.body.merge_stores() {
|
||||
function.body.compact();
|
||||
if this.options.verify {
|
||||
val verdict := function.body.verify();
|
||||
for &each in verdict.problems {
|
||||
this.problems.append($"{function.name}: after merging stores: {each}");
|
||||
}
|
||||
}
|
||||
}
|
||||
release_scratch(point);
|
||||
}
|
||||
}
|
||||
|
||||
#! Whether calls of `@inline` functions are inlined below `-O2`, which inlines
|
||||
#! nothing else: but where the program evaluates constants, or runs with its
|
||||
#! memory checked, as each function's locals end where its frame does then.
|
||||
#! @param this The compiler.
|
||||
#! @returns Whether they are.
|
||||
def lowerer::inlines_marked(&this): bool {
|
||||
return !this.options.evaluating && !this.options.checks;
|
||||
}
|
||||
|
||||
#! Whether `-O0`, which optimizes nothing, has calls to inline all the same:
|
||||
#! where a function lowered is `@inline`.
|
||||
#! @param this The compiler.
|
||||
#! @returns Whether it has.
|
||||
def lowerer::copies_marked(&this): bool {
|
||||
return false if !this.inlines_marked();
|
||||
for &each in this.program.functions {
|
||||
return true if each.inline && each.lowered;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#! Where a function never optimized is in the order functions are optimized in.
|
||||
const unranked: u32 = 0xFFFFFFFF;
|
||||
|
||||
#! The order functions are optimized in, and what each waits for.
|
||||
struct optimizing {
|
||||
var order: vector<u32>; #! The functions, each after those it calls, but for those calling each other.
|
||||
var ranks: vector<u32>; #! Of each function, `2k + 2` where it is the `k`th in the order, `2k + 3` where it is the version of that one, 0 where it was optimized before, and `unranked` where it is not optimized.
|
||||
var waiting: groups<u32>; #! The places in the order of those waiting on each, by its place: each calls it, and comes after it.
|
||||
}
|
||||
|
||||
#! A function on the walk of the calls that orders functions to be optimized.
|
||||
struct calling {
|
||||
var function: u32; #! The function.
|
||||
var begin: u64; #! Where the functions it calls begin among the calls of those on the walk.
|
||||
var end: u64; #! Where they end.
|
||||
var next: u64; #! The next of them to walk.
|
||||
}
|
||||
|
||||
#! Works out the order functions are optimized in: walking the calls from each
|
||||
#! function lowered and not yet optimized, each once those it calls are, but
|
||||
#! for those calling each other, which go in the order met. Each waits for the
|
||||
#! functions before it that it calls.
|
||||
#! @param this The compiler.
|
||||
#! @returns The order.
|
||||
def lowerer::plan_optimizing(&mut this): optimizing {
|
||||
val count := this.program.functions.length;
|
||||
var plan := optimizing {
|
||||
order = vector::<u32>::new(),
|
||||
ranks = vector::<u32>::new(),
|
||||
waiting = groups::<u32>::new(0)
|
||||
};
|
||||
var seen := vector::<bool>::new();
|
||||
seen.resize(count);
|
||||
plan.ranks.resize(count);
|
||||
for i in 0..count {
|
||||
if this.program.functions[i].optimized {
|
||||
seen[i] = true;
|
||||
this.ready_to_inline(i as u32);
|
||||
} else {
|
||||
plan.ranks[i] = unranked;
|
||||
}
|
||||
}
|
||||
|
||||
# The functions on the walk, each with the functions it calls in `calls`.
|
||||
# The callees of each function in the order are kept in `callees`, a run of
|
||||
# each.
|
||||
var stack := vector::<calling>::new();
|
||||
var calls := vector::<u32>::new();
|
||||
var callees := runs::<u32>::new();
|
||||
var callee_runs := vector::<u32>::new();
|
||||
for root in 0..count {
|
||||
continue if seen[root] || !this.program.functions[root].lowered;
|
||||
this.walk_into(root as u32, &mut seen, &mut stack, &mut calls);
|
||||
while stack.length != 0 {
|
||||
# The top is written before a callee is pushed, which may move it.
|
||||
val top := &mut stack.last;
|
||||
if top.next < top.end {
|
||||
val callee: u32 = calls[top.next];
|
||||
top.next++;
|
||||
if !seen[callee] && this.program.functions[callee].lowered {
|
||||
this.walk_into(callee, &mut seen, &mut stack, &mut calls);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
callee_runs.append(callees.add(calls[top.begin:top.end - top.begin]));
|
||||
plan.ranks[top.function] = (2 * plan.order.length + 2) as u32;
|
||||
plan.order.append(top.function);
|
||||
calls.resize(top.begin);
|
||||
stack.truncate();
|
||||
}
|
||||
}
|
||||
|
||||
# Each function waits for those before it that it calls, each once, as the
|
||||
# functions counted for it say.
|
||||
val placed := plan.order.length;
|
||||
var counted := stamped_set::new(placed, nothing {});
|
||||
var pairs := vector::<u64>::new();
|
||||
plan.waiting.reset(placed);
|
||||
for p in 0..placed {
|
||||
counted.next_round();
|
||||
for &callee in callees.view(callee_runs[p]) {
|
||||
val rank: u32 = plan.ranks[callee];
|
||||
continue if rank == 0 || rank == unranked;
|
||||
val q := (rank - 2) / 2;
|
||||
continue if q as u64 >= p || !counted.insert(q);
|
||||
pairs.append(pair_key(q, p as u32));
|
||||
plan.waiting.count(q);
|
||||
}
|
||||
}
|
||||
plan.waiting.seal();
|
||||
for &pair in pairs {
|
||||
plan.waiting.put(pair_high(pair), pair_low(pair));
|
||||
}
|
||||
this.find_sole_calls(&plan.order);
|
||||
|
||||
# A call of a function this one waits for is settled, and stays so where it is
|
||||
# inlined into another, which waited for this one.
|
||||
for p in 0..placed {
|
||||
val function: u32 = plan.order[p];
|
||||
val body := &mut this.program.functions[function].body;
|
||||
var walk := body.walk_all();
|
||||
while walk.more() {
|
||||
val at := walk.next();
|
||||
continue if body.insts[at].kind != op::call;
|
||||
val rank: u32 = plan.ranks[body.insts[at].aux];
|
||||
body.insts[at].imm = rank != 0 && rank != unranked && rank < plan.ranks[function] ? call_settled : 0;
|
||||
}
|
||||
}
|
||||
return plan;
|
||||
}
|
||||
|
||||
#! Puts a function on the walk, with the functions it calls.
|
||||
#! @param this The compiler.
|
||||
#! @param function The function.
|
||||
#! @param seen Whether each function was put on the walk.
|
||||
#! @param stack The functions on the walk.
|
||||
#! @param calls The callees of the functions on the walk.
|
||||
def lowerer::walk_into(&mut this, function: u32, seen: &mut vector<bool>, stack: &mut vector<calling>, calls: &mut vector<u32>): void {
|
||||
seen[function] = true;
|
||||
val begin := calls.length;
|
||||
val body := &this.program.functions[function].body;
|
||||
var walk := body.walk_all();
|
||||
while walk.more() {
|
||||
val at := walk.next();
|
||||
if body.insts[at].kind == op::call {
|
||||
calls.append(body.insts[at].aux);
|
||||
}
|
||||
}
|
||||
# A function named as a value may be called directly once the value is known,
|
||||
# so it is waited for as one called is.
|
||||
for &named in this.program.functions[function].named.span {
|
||||
calls.append(named);
|
||||
}
|
||||
stack.append(calling { function = function, begin = begin, end = calls.length, next = begin });
|
||||
}
|
||||
|
||||
#! Optimizes the function of a place in the order, then readies it to be
|
||||
#! inlined.
|
||||
#! @param this The compiler.
|
||||
#! @param plan The order.
|
||||
#! @param place The place.
|
||||
#! @param found Where what verifying it finds is noted.
|
||||
def lowerer::optimize_at(&mut this, plan: &optimizing, place: u64, found: &mut vector<string>): void {
|
||||
val function: u32 = plan.order[place];
|
||||
# Its version, where it is given one, is optimized from its code as lowered,
|
||||
# as optimizing for processors without AVX splits 32 bytes of lanes in two.
|
||||
if this.versions_made() {
|
||||
this.program.functions[function].unoptimized = this.program.functions[function].body.duplicate();
|
||||
}
|
||||
this.optimize_function(function, &plan.ranks, (2 * place + 2) as u32, found);
|
||||
this.program.functions[function].optimized = true;
|
||||
this.ready_to_inline(function);
|
||||
}
|
||||
|
||||
#! Finds the functions left unfinished that something still reaches.
|
||||
#! @param this The compiler.
|
||||
#! @returns The functions, in order.
|
||||
def lowerer::left_unfinished(&mut this): vector<u32> {
|
||||
var left := vector::<u32>::new();
|
||||
val count := this.program.functions.length;
|
||||
var any := false;
|
||||
for i in 0..count {
|
||||
any = any || this.program.functions[i].unfinished;
|
||||
}
|
||||
return left if !any;
|
||||
val point := mark_scratch();
|
||||
var reached := worklist::new(count, scratch());
|
||||
this.reach_functions(&mut reached);
|
||||
for i in 0..count {
|
||||
if this.program.functions[i].unfinished && reached.has(i as u32) {
|
||||
left.append(i as u32);
|
||||
}
|
||||
}
|
||||
release_scratch(point);
|
||||
return left;
|
||||
}
|
||||
|
||||
#! Runs the rest of the passes on a function left unfinished, then readies it
|
||||
#! to be inlined again, as its code changed.
|
||||
#! @param this The compiler.
|
||||
#! @param function The function.
|
||||
#! @param ranks Where each function is in the order functions are
|
||||
#! optimized in, which none of the passes left inlines by.
|
||||
#! @param found Where what verifying it finds is noted.
|
||||
def lowerer::finish_unfinished(&mut this, function: u32, ranks: &vector<u32>, found: &mut vector<string>): void {
|
||||
var sweep := passes { ranks = ranks, rank = 0, found = found, compacted = 0xFFFFFFFF };
|
||||
for i in 0..pass::count as u64 {
|
||||
sweep.clean[i] = 0xFFFFFFFF;
|
||||
}
|
||||
this.finish_function(function, &mut sweep);
|
||||
val held := &mut this.program.functions[function];
|
||||
held.unfinished = false;
|
||||
held.inlining = inline_facts { sole = held.inlining.sole };
|
||||
held.order.clear();
|
||||
this.ready_to_inline(function);
|
||||
}
|
||||
|
||||
#! Works out what inlining an optimized function asks of it, as its code no
|
||||
#! longer changes, rather than leave that to the first function inlining it,
|
||||
#! as several may at once on threads of their own: what inlining knows of it,
|
||||
#! and where it may be copied, the order its blocks are copied in.
|
||||
#! @param this The compiler.
|
||||
#! @param function The function.
|
||||
def lowerer::ready_to_inline(&mut this, function: u32): void {
|
||||
# Below `-O2`, only `@inline` functions are.
|
||||
return if this.options.level < 2 && !this.program.functions[function].inline;
|
||||
val point := mark_scratch();
|
||||
if this.inline_facts(function).fits {
|
||||
this.order_blocks(function);
|
||||
}
|
||||
release_scratch(point);
|
||||
}
|
||||
|
||||
#! Optimizes one function, as its level says. A pass that found nothing to
|
||||
#! change is not run again until another changes something.
|
||||
#! @param this The compiler.
|
||||
#! @param index The function.
|
||||
#! @param ranks Where each function is in the order functions are optimized
|
||||
#! in, as `optimizing` says: those before it are inlined.
|
||||
#! @param rank Where the function is.
|
||||
#! @param found Where what verifying it finds is noted.
|
||||
def lowerer::optimize_function(&mut this, index: u32, ranks: &vector<u32>, rank: u32, found: &mut vector<string>): void {
|
||||
var sweep := passes { ranks = ranks, rank = rank, found = found, compacted = 0xFFFFFFFF };
|
||||
for i in 0..pass::count as u64 {
|
||||
sweep.clean[i] = 0xFFFFFFFF;
|
||||
}
|
||||
# `-O0` inlines the calls of `@inline` functions, and changes nothing else.
|
||||
if this.options.level == 0 {
|
||||
this.sweep(index, &mut sweep, pass::inline);
|
||||
if sweep.changes != 0 {
|
||||
this.compact_function(index, &mut sweep);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
this.sweep(index, &mut sweep, pass::simplify);
|
||||
this.sweep(index, &mut sweep, pass::control);
|
||||
if this.options.level < 2 && this.inlines_marked() {
|
||||
this.sweep(index, &mut sweep, pass::inline);
|
||||
}
|
||||
if this.options.level >= 2 {
|
||||
this.sweep(index, &mut sweep, pass::dead);
|
||||
this.sweep(index, &mut sweep, pass::inline);
|
||||
this.sweep(index, &mut sweep, pass::promote);
|
||||
this.sweep(index, &mut sweep, pass::simplify);
|
||||
this.sweep(index, &mut sweep, pass::control);
|
||||
# Promoting may show which function a value calls, which is then called and
|
||||
# inlined as any other.
|
||||
this.sweep(index, &mut sweep, pass::inline);
|
||||
this.sweep(index, &mut sweep, pass::promote);
|
||||
this.sweep(index, &mut sweep, pass::simplify);
|
||||
this.sweep(index, &mut sweep, pass::control);
|
||||
# The function calling it copies it whole and optimizes it there, so the
|
||||
# rest is left until then, and run after only where it is still called.
|
||||
if this.awaits_caller(index) {
|
||||
this.program.functions[index].unfinished = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
this.finish_function(index, &mut sweep);
|
||||
}
|
||||
|
||||
#! Runs the passes after inlining on a function's code, then compacts it.
|
||||
#! @param this The compiler.
|
||||
#! @param index The function.
|
||||
#! @param sweep What is known of the passes run on it.
|
||||
def lowerer::finish_function(&mut this, index: u32, sweep: &mut passes): void {
|
||||
# What both ways out of a branch work out alike is worked out before it, so
|
||||
# numbering finds it worked out where they join.
|
||||
this.sweep(index, sweep, pass::lift);
|
||||
this.sweep(index, sweep, pass::numbering);
|
||||
this.sweep(index, sweep, pass::memory);
|
||||
this.sweep(index, sweep, pass::dead);
|
||||
# What numbering and cleaning up memory left a value copied whole between
|
||||
# cells may be promoted now, as a vector a version's tests no longer split.
|
||||
if this.options.level >= 2 {
|
||||
this.sweep(index, sweep, pass::promote);
|
||||
this.sweep(index, sweep, pass::simplify);
|
||||
}
|
||||
this.sweep(index, sweep, pass::loops);
|
||||
this.sweep(index, sweep, pass::numbering);
|
||||
this.sweep(index, sweep, pass::simplify);
|
||||
# A narrowing or a mask whose bits are zero already, or never read, goes,
|
||||
# and what that leaves is simplified again.
|
||||
this.sweep(index, sweep, pass::bits);
|
||||
this.sweep(index, sweep, pass::simplify);
|
||||
this.sweep(index, sweep, pass::control);
|
||||
this.sweep(index, sweep, pass::sink);
|
||||
# What sinking moved past a branch, as a `select` on the condition the branch
|
||||
# tests, is numbered again where the branch decided it.
|
||||
this.sweep(index, sweep, pass::numbering);
|
||||
this.sweep(index, sweep, pass::dead);
|
||||
this.sweep(index, sweep, pass::hoist);
|
||||
this.compact_function(index, sweep);
|
||||
}
|
||||
|
||||
#! Whether a function, its calls inlined, is left for the function calling it
|
||||
#! to optimize: that alone calls it, and copies it whole, as it may be copied,
|
||||
#! asks nothing of the processor, and leaves that function room to grow.
|
||||
#! @param this The compiler.
|
||||
#! @param index The function.
|
||||
#! @returns Whether it is.
|
||||
def lowerer::awaits_caller(&this, index: u32): bool {
|
||||
val held := &this.program.functions[index];
|
||||
return false if held.inlining.sole == 0 || held.closure || held.evaluation != 0;
|
||||
var size: u32 = 0;
|
||||
var walk := held.body.walk_all();
|
||||
while walk.more() {
|
||||
match held.body.insts[walk.next()].kind {
|
||||
op::frame, op::ready, op::reach, op::same, op::cpu => { return false; }
|
||||
else => {}
|
||||
}
|
||||
size++;
|
||||
}
|
||||
return size <= sole_growth;
|
||||
}
|
||||
|
||||
#! What optimizing one function knows of the passes run on it.
|
||||
struct passes {
|
||||
var ranks: &vector<u32>; #! Where each function is in the order functions are optimized in.
|
||||
var rank: u32; #! Where the function is.
|
||||
var found: &mut vector<string>; #! What verifying it finds.
|
||||
var changes: u32; #! How many passes changed something.
|
||||
var compacted: u32; #! How many passes had changed something when the code was last compacted, or `0xFFFFFFFF` before it was.
|
||||
var clean: u32[pass::count as u64]; #! How many passes had changed something when each last found nothing to change.
|
||||
}
|
||||
|
||||
#! Runs a pass over a function's code, but where it found nothing to change
|
||||
#! since any pass last changed something.
|
||||
#! @param this The compiler.
|
||||
#! @param index The function.
|
||||
#! @param sweep What is known of the passes run on it.
|
||||
#! @param which The pass.
|
||||
def lowerer::sweep(&mut this, index: u32, sweep: &mut passes, which: pass): void {
|
||||
return if sweep.clean[which as u64] == sweep.changes;
|
||||
if this.run_pass(index, which, sweep) {
|
||||
sweep.changes++;
|
||||
} else {
|
||||
sweep.clean[which as u64] = sweep.changes;
|
||||
}
|
||||
}
|
||||
|
||||
#! Compacts a function's code where a pass changed it since it was last
|
||||
#! compacted, then verifies it where lowering verifies.
|
||||
#! @param this The compiler.
|
||||
#! @param index The function.
|
||||
#! @param sweep What is known of the passes run on it.
|
||||
def lowerer::compact_function(&mut this, index: u32, sweep: &mut passes): void {
|
||||
return if sweep.compacted == sweep.changes;
|
||||
sweep.compacted = sweep.changes;
|
||||
val point := mark_scratch();
|
||||
this.program.functions[index].body.compact();
|
||||
if this.options.verify {
|
||||
val verdict := this.program.functions[index].body.verify();
|
||||
for &each in verdict.problems {
|
||||
sweep.found.append($"{this.program.functions[index].name}: after compacting: {each}");
|
||||
}
|
||||
}
|
||||
release_scratch(point);
|
||||
}
|
||||
|
||||
#! Cleans up the memory of the cells of a function's code, knowing what each
|
||||
#! call writes what it returns to.
|
||||
#! @param this The compiler.
|
||||
#! @param index The function.
|
||||
#! @returns Whether anything changed.
|
||||
def lowerer::clean_memory(&mut this, index: u32): bool {
|
||||
val writes := this.result_writes(index);
|
||||
return this.program.functions[index].body.clean_memory(writes.span);
|
||||
}
|
||||
|
||||
#! How many bytes each call of a function's code writes what it returns to, at
|
||||
#! the address its last operand gives, as `emitter::call_with` passes it. Each
|
||||
#! instruction is looked at where it lies, those taken out too, which nothing
|
||||
#! asks of.
|
||||
#! @param this The compiler.
|
||||
#! @param index The function.
|
||||
#! @returns The bytes, by instruction, or 0 for what writes none.
|
||||
pub def lowerer::result_writes(&this, index: u32): vector<u64> {
|
||||
val body := &this.program.functions[index].body;
|
||||
var writes := vector::<u64>::new(scratch());
|
||||
writes.resize(body.insts.length);
|
||||
for at in 1..body.insts.length {
|
||||
val kind := body.insts[at].kind;
|
||||
val aux: u32 = body.insts[at].aux;
|
||||
var signature: u32 = no_function;
|
||||
if kind == op::call {
|
||||
signature = this.program.functions[aux].signature;
|
||||
} else if kind == op::call_trait || kind == op::call_value || kind == op::call_c {
|
||||
signature = aux;
|
||||
}
|
||||
continue if signature == no_function;
|
||||
val shape := this.layout_at(this.program.signatures[signature].result);
|
||||
if returned_in_memory(&shape) {
|
||||
writes[at] = shape.size;
|
||||
}
|
||||
}
|
||||
return writes;
|
||||
}
|
||||
|
||||
#! Runs a pass over a function's code, then verifies it where lowering
|
||||
#! verifies, noting what is wrong with what the pass left.
|
||||
#! @param this The compiler.
|
||||
#! @param index The function.
|
||||
#! @param which The pass.
|
||||
#! @param sweep What is known of the passes run on it.
|
||||
#! @returns Whether the pass changed anything.
|
||||
def lowerer::run_pass(&mut this, index: u32, which: pass, sweep: &mut passes): bool {
|
||||
# What the pass and the verifier work out is freed as the pass ends.
|
||||
val point := mark_scratch();
|
||||
val body := &mut this.program.functions[index].body;
|
||||
val changed := match which {
|
||||
pass::simplify => body.simplify(this.program.data.span),
|
||||
pass::dead => body.remove_dead(),
|
||||
pass::control => body.clean_control(),
|
||||
pass::inline => this.inline_calls(index, sweep.ranks, sweep.rank),
|
||||
pass::promote => body.promote_cells(),
|
||||
pass::numbering => this.number_values(index),
|
||||
pass::hoist => body.hoist_constants(),
|
||||
pass::memory => this.clean_memory(index),
|
||||
pass::loops => this.optimize_loops(index),
|
||||
pass::sink => this.sink_code(index),
|
||||
pass::bits => body.trim_bits(),
|
||||
pass::lift => body.lift_code(),
|
||||
else => false
|
||||
};
|
||||
# Each pass forgets the order and dominators the code keeps where it changes
|
||||
# where blocks lead, or works them out anew, as pruning does after inlining,
|
||||
# so they hold for the code it leaves.
|
||||
if this.options.verify && changed {
|
||||
val verdict := this.program.functions[index].body.verify();
|
||||
for &each in verdict.problems {
|
||||
sweep.found.append($"{this.program.functions[index].name}: after {which.name()}: {each}");
|
||||
}
|
||||
}
|
||||
release_scratch(point);
|
||||
return changed;
|
||||
}
|
||||
@@ -0,0 +1,895 @@
|
||||
#! @file promote.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the fifth pass of the optimizer, which promotes cells to values: a
|
||||
#! cell whose address is only read from, written to, zeroed, or copied in and
|
||||
#! out from its start, at offsets that do not overlap, holds values rather than
|
||||
#! memory. Each word it holds becomes a variable, and the code is rebuilt as it
|
||||
#! was built, each read reading what the variable holds there, which where
|
||||
#! control flow joins is a parameter of the block. The bytes a copy moves that
|
||||
#! nothing reads or writes alone are words of their own, so a value copied from
|
||||
#! one cell to another and on is promoted whole. Inlining leaves many such
|
||||
#! cells, where a function took the address of what it was given or returned.
|
||||
#! A `simd` read and written whole becomes one vector, which an executable holds
|
||||
#! in a vector register, but 32 bytes only in code for processors with AVX.
|
||||
#! First, a cell holding several values one after another, as a statement's
|
||||
#! temporary taken again by the next does, is split into a cell for each, as
|
||||
#! `split.sakura` says.
|
||||
|
||||
import std::containers;
|
||||
module sakura::compiler;
|
||||
|
||||
|
||||
#! A word a cell promoted holds: where it is in the cell, how wide it is, and
|
||||
#! the type of value it holds.
|
||||
struct slot {
|
||||
var cell: u32; #! The cell.
|
||||
var offset: u64; #! Where it begins.
|
||||
var size: u64; #! How many bytes it is.
|
||||
var kind: vt; #! The type of value it holds.
|
||||
var signed: bool; #! Whether it is read as a signed integer narrower than a word, which a copy narrows it as too.
|
||||
var next: u32; #! One more than the next slot of the same cell, or 0.
|
||||
var used: bool; #! Whether anything reads what it holds, or copies it out.
|
||||
}
|
||||
|
||||
#! What promoting knows of the cells of the code.
|
||||
struct promotion {
|
||||
var escaped: vector<bool>; #! Whether each cell's address is used as more than an address read and written through.
|
||||
var extent: vector<u64>; #! How many bytes from its start the copies in and out of each cell move, which its slots must cover.
|
||||
var slots: vector<slot>; #! The words of the cells, each once.
|
||||
var heads: vector<u32>; #! One more than the first slot of each cell, or 0, its others following by `next`.
|
||||
var addresses: cell_table; #! Which cell each value is the address of.
|
||||
var ends: dict<u64, u32>; #! The value each slot holds at the end of each block that writes it, by `pair_key(slot, block)`.
|
||||
var starts: dict<u64, u32>; #! The value each slot holds at the start of each block asked, by `pair_key(slot, block)`.
|
||||
var zeros: zero_values; #! The zero of each type, made once.
|
||||
var wide: bool; #! Whether 32 bytes of lanes may be a value.
|
||||
var vectors: bool; #! Whether 16 bytes of lanes may be, as an executable's code holds them.
|
||||
var pairs: vector<u32>; #! The copies from one cell into another.
|
||||
var copies: dict<u64, u32>; #! The read each copy into a cell promoted makes of each slot, by `pair_key(copy, slot)`.
|
||||
var touch: vector<u32>; #! The instructions naming a cell, as operands or as what their edges give.
|
||||
var current: vector<u32>; #! What each slot holds where the walk of a block is, where the block wrote it.
|
||||
var written: stamped_set; #! The slots the block being rewritten wrote so far, a round each block.
|
||||
var dense: u64; #! How many blocks `ends` and `starts` are held for in `dense_ends` and `dense_starts`, by slot then block, or 0 where they are not.
|
||||
var dense_ends: vector<u32>; #! What `ends` holds, where it is dense, 0 for nothing.
|
||||
var dense_starts: vector<u32>; #! What `starts` holds, where it is dense.
|
||||
}
|
||||
|
||||
#! What `promotion::slot_at` gives where a cell has no slot at an offset.
|
||||
const no_slot: u32 = 0xFFFFFFFF;
|
||||
|
||||
#! The most slots of every block the values they hold at each block's ends are
|
||||
#! held for one after another, rather than by key.
|
||||
const dense_slot_blocks: u64 = 65536;
|
||||
|
||||
#! The value a slot holds where a block ends, where the block writes it.
|
||||
#! @param this What promoting knows.
|
||||
#! @param s The slot.
|
||||
#! @param block The block.
|
||||
#! @returns The value, or 0.
|
||||
def promotion::end_at(&this, s: u32, block: u32): u32 {
|
||||
return this.dense_ends[s as u64 * this.dense + block as u64] if this.dense != 0;
|
||||
val key := pair_key(s, block);
|
||||
return this.ends.get_or(&key, 0);
|
||||
}
|
||||
|
||||
#! Notes the value a slot holds where a block ends.
|
||||
#! @param this What promoting knows.
|
||||
#! @param s The slot.
|
||||
#! @param block The block.
|
||||
#! @param value The value.
|
||||
def promotion::set_end(&mut this, s: u32, block: u32, value: u32): void {
|
||||
if this.dense != 0 {
|
||||
this.dense_ends[s as u64 * this.dense + block as u64] = value;
|
||||
return;
|
||||
}
|
||||
this.ends.set(pair_key(s, block), value);
|
||||
}
|
||||
|
||||
#! The value a slot holds where a block begins, where it is known.
|
||||
#! @param this What promoting knows.
|
||||
#! @param s The slot.
|
||||
#! @param block The block.
|
||||
#! @returns The value, or 0.
|
||||
def promotion::start_at(&this, s: u32, block: u32): u32 {
|
||||
return this.dense_starts[s as u64 * this.dense + block as u64] if this.dense != 0;
|
||||
val key := pair_key(s, block);
|
||||
return this.starts.get_or(&key, 0);
|
||||
}
|
||||
|
||||
#! Notes the value a slot holds where a block begins.
|
||||
#! @param this What promoting knows.
|
||||
#! @param s The slot.
|
||||
#! @param block The block.
|
||||
#! @param value The value.
|
||||
def promotion::set_start(&mut this, s: u32, block: u32, value: u32): void {
|
||||
if this.dense != 0 {
|
||||
this.dense_starts[s as u64 * this.dense + block as u64] = value;
|
||||
return;
|
||||
}
|
||||
this.starts.set(pair_key(s, block), value);
|
||||
}
|
||||
|
||||
#! Promotes the cells that can be, as the file says.
|
||||
#! @param this The code.
|
||||
#! @returns Whether any was.
|
||||
pub def code::promote_cells(&mut this): bool {
|
||||
return false if this.cells.length == 0;
|
||||
# Each value a cell holds in turn is promoted or not by its own uses, and a
|
||||
# vector read or written where its lanes are too is so a lane at a time.
|
||||
val split := this.split_cells();
|
||||
val united := this.unite_widths();
|
||||
val reshaped := split || united;
|
||||
var state := promotion {
|
||||
escaped = vector::<bool>::new(scratch()),
|
||||
extent = vector::<u64>::new(scratch()),
|
||||
slots = vector::<slot>::new(scratch()),
|
||||
heads = vector::<u32>::new(scratch()),
|
||||
addresses = this.cell_table(),
|
||||
ends = dict::<u64, u32>::new(scratch()),
|
||||
starts = dict::<u64, u32>::new(scratch()),
|
||||
copies = dict::<u64, u32>::new(scratch()),
|
||||
pairs = vector::<u32>::new(scratch()),
|
||||
touch = vector::<u32>::new(scratch()),
|
||||
current = vector::<u32>::new(scratch()),
|
||||
written = stamped_set::new(0, scratch()),
|
||||
dense_ends = vector::<u32>::new(scratch()),
|
||||
dense_starts = vector::<u32>::new(scratch()),
|
||||
wide = this.wide,
|
||||
vectors = this.vectors
|
||||
};
|
||||
return reshaped if !state.addresses.any;
|
||||
state.escaped.resize(this.cells.length);
|
||||
state.extent.resize(this.cells.length);
|
||||
state.heads.resize(this.cells.length);
|
||||
|
||||
# A copy from a cell its words cover into another is the reads and writes of
|
||||
# each word, which then promote as any others, so the words of the cells
|
||||
# are found again, as many times as a value is copied on.
|
||||
this.find_touches(&mut state);
|
||||
var rounds := 0;
|
||||
while true {
|
||||
rounds++;
|
||||
this.find_slots(&mut state);
|
||||
break if rounds == 4 || !this.split_pairs(&mut state);
|
||||
for c in 0..this.cells.length {
|
||||
state.escaped[c] = false;
|
||||
state.extent[c] = 0;
|
||||
state.heads[c] = 0;
|
||||
}
|
||||
state.slots.clear();
|
||||
state.pairs.clear();
|
||||
}
|
||||
this.settle_pairs(&mut state);
|
||||
var promoted := false;
|
||||
for c in 0..this.cells.length {
|
||||
if !state.escaped[c] {
|
||||
promoted = true;
|
||||
}
|
||||
}
|
||||
return reshaped if !promoted;
|
||||
|
||||
# Where control flow joins, slots are found in the order of the blocks.
|
||||
if state.slots.length * this.blocks.length <= dense_slot_blocks {
|
||||
state.dense = this.blocks.length;
|
||||
state.dense_ends.resize(state.slots.length * state.dense);
|
||||
state.dense_starts.resize(state.slots.length * state.dense);
|
||||
}
|
||||
var order := vector::<u32>::new(scratch());
|
||||
var number := vector::<u32>::new(scratch());
|
||||
this.block_order(&mut order, &mut number);
|
||||
for &block in order {
|
||||
this.note_ends(block, &mut state);
|
||||
}
|
||||
var done := rewrite::new();
|
||||
state.current.resize(state.slots.length);
|
||||
state.written = stamped_set::new(state.slots.length, scratch());
|
||||
for &block in order {
|
||||
state.written.next_round();
|
||||
this.rewrite_slots(block, &mut state, &mut done);
|
||||
}
|
||||
this.apply(&done);
|
||||
this.drop_unreached();
|
||||
this.drop_lone_params();
|
||||
return true;
|
||||
}
|
||||
|
||||
#! Finds the instructions naming a cell, which are all that finding slots
|
||||
#! walks.
|
||||
#! @param this The code.
|
||||
#! @param state What promoting knows.
|
||||
def code::find_touches(&this, state: &mut promotion): void {
|
||||
state.touch.clear();
|
||||
var walk := this.walk_all();
|
||||
var reads := this.reader();
|
||||
while walk.more() {
|
||||
val at := walk.next();
|
||||
reads.start(at);
|
||||
while reads.more() {
|
||||
if state.addresses.cell(reads.next()) != 0 {
|
||||
state.touch.append(at);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#! Leaves memory the cell each copy between cells neither escaped comes from,
|
||||
#! where the copy was not split, so the copy is one out of memory into the one
|
||||
#! it goes to.
|
||||
#! @param this The code.
|
||||
#! @param state What promoting knows.
|
||||
def code::settle_pairs(&this, state: &mut promotion): void {
|
||||
var changed := false;
|
||||
for © in state.pairs {
|
||||
val to := state.addresses.cell(this.arg(copy, 0)) - 1;
|
||||
val from := state.addresses.cell(this.arg(copy, 1)) - 1;
|
||||
if !state.escaped[to] && !state.escaped[from] {
|
||||
state.escaped[from] = true;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
if changed {
|
||||
this.find_uses(state);
|
||||
}
|
||||
}
|
||||
|
||||
#! Marks the slots of the cells not escaped that something reads, or copies
|
||||
#! out of its cell, so a copy into the cell reads only those from where it
|
||||
#! copies, and a slot read only as a copy moves it into another is used where
|
||||
#! the other's is.
|
||||
#! @param this The code.
|
||||
#! @param state What promoting knows.
|
||||
def code::find_uses(&this, state: &mut promotion): void {
|
||||
for i in 0..state.slots.length {
|
||||
state.slots[i].used = false;
|
||||
}
|
||||
for t in 0..state.touch.length {
|
||||
val at: u32 = state.touch[t];
|
||||
val held: inst = this.insts[at];
|
||||
continue if held.kind != op::load && held.kind != op::copy;
|
||||
val operand: u32 = held.kind == op::load ? 0 : 1;
|
||||
val cell := state.addresses.cell(this.arg(at, operand));
|
||||
val into := held.kind == op::copy ? state.addresses.cell(this.arg(at, 0)) : 0;
|
||||
val moved := into != 0 && !state.escaped[into - 1];
|
||||
continue if cell == 0 || state.escaped[cell - 1] || moved;
|
||||
var i: u32 = state.heads[cell - 1];
|
||||
while i != 0 {
|
||||
val each: slot = state.slots[i - 1];
|
||||
if (held.kind == op::load && each.offset == held.imm) || (held.kind == op::copy && each.offset < held.imm) {
|
||||
state.slots[i - 1].used = true;
|
||||
}
|
||||
i = each.next;
|
||||
}
|
||||
}
|
||||
|
||||
var changed := true;
|
||||
var rounds := 0;
|
||||
while changed && rounds < 8 {
|
||||
rounds++;
|
||||
changed = false;
|
||||
for p in 0..state.pairs.length {
|
||||
val copy: u32 = state.pairs[p];
|
||||
val to := state.addresses.cell(this.arg(copy, 0)) - 1;
|
||||
val from := state.addresses.cell(this.arg(copy, 1)) - 1;
|
||||
continue if state.escaped[to] || state.escaped[from];
|
||||
var i: u32 = state.heads[from];
|
||||
while i != 0 {
|
||||
val each: slot = state.slots[i - 1];
|
||||
if !each.used && each.offset < this.insts[copy].imm && state.used_over(to, each.offset, each.size) {
|
||||
state.slots[i - 1].used = true;
|
||||
changed = true;
|
||||
}
|
||||
i = each.next;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#! Finds how each cell's address is used, and the words each cell not escaped
|
||||
#! holds, marking escaped a cell its words do not fit, and which of its words
|
||||
#! are used. Copies between cells are gathered, to be split.
|
||||
#! @param this The code.
|
||||
#! @param state What promoting knows, whose instructions naming cells are
|
||||
#! found.
|
||||
def code::find_slots(&this, state: &mut promotion): void {
|
||||
var reads := this.reader();
|
||||
for t in 0..state.touch.length {
|
||||
val at: u32 = state.touch[t];
|
||||
val held: inst = this.insts[at];
|
||||
reads.start(at);
|
||||
while reads.more() {
|
||||
val cell := state.addresses.cell(reads.next());
|
||||
continue if cell == 0;
|
||||
val c := cell - 1;
|
||||
val i := reads.operand();
|
||||
# A cell's address an edge gives escapes.
|
||||
if i == no_operand {
|
||||
state.escaped[c] = true;
|
||||
continue;
|
||||
}
|
||||
match held.kind {
|
||||
op::load => {
|
||||
val how := held.aux as access;
|
||||
state.add_slot(c, held.imm, how.width(), how.value_type(), narrow_signed(how));
|
||||
}
|
||||
op::store => {
|
||||
if i == 0 {
|
||||
state.add_slot(c, held.imm, (held.aux as access).width(), (held.aux as access).value_type(), false);
|
||||
} else {
|
||||
state.escaped[c] = true;
|
||||
}
|
||||
}
|
||||
# A cell's address is never null, which inlining may check.
|
||||
op::clear => {}
|
||||
op::check => {
|
||||
if held.aux != fault::null_reference as u32 {
|
||||
state.escaped[c] = true;
|
||||
}
|
||||
}
|
||||
op::copy => {
|
||||
# A copy in or out of the cell from its start, between it and memory
|
||||
# no cell promoted is, reads or writes each of the words it moves.
|
||||
if held.imm > this.cells[c].size as u64 {
|
||||
state.escaped[c] = true;
|
||||
} else {
|
||||
if i == 0 && state.addresses.cell(this.arg(at, 1)) != 0 {
|
||||
state.pairs.append(at);
|
||||
}
|
||||
if held.imm > state.extent[c] {
|
||||
state.extent[c] = held.imm;
|
||||
}
|
||||
}
|
||||
}
|
||||
else => state.escaped[c] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# A copy between cells moves the words either reads or writes alone, so each
|
||||
# holds the other's, from one to the next along a chain of copies.
|
||||
var rounds := 0;
|
||||
var changed := state.pairs.length != 0;
|
||||
while changed && rounds < 8 {
|
||||
rounds++;
|
||||
changed = false;
|
||||
for p in 0..state.pairs.length {
|
||||
val copy: u32 = state.pairs[p];
|
||||
val to := state.addresses.cell(this.arg(copy, 0)) - 1;
|
||||
val from := state.addresses.cell(this.arg(copy, 1)) - 1;
|
||||
val size: u64 = this.insts[copy].imm;
|
||||
changed = state.share_slots(from, to, size) || changed;
|
||||
changed = state.share_slots(to, from, size) || changed;
|
||||
}
|
||||
}
|
||||
|
||||
# Each cell's words must not overlap, and the bytes its copies move that no
|
||||
# word holds are words of their own.
|
||||
for c in 0..this.cells.length {
|
||||
continue if state.escaped[c];
|
||||
var i: u32 = state.heads[c];
|
||||
while i != 0 {
|
||||
val each: slot = state.slots[i - 1];
|
||||
var j: u32 = each.next;
|
||||
while j != 0 {
|
||||
val other: slot = state.slots[j - 1];
|
||||
if other.offset < each.offset + each.size && each.offset < other.offset + other.size {
|
||||
state.escaped[c] = true;
|
||||
}
|
||||
j = other.next;
|
||||
}
|
||||
i = each.next;
|
||||
}
|
||||
if !state.escaped[c] && state.extent[c] != 0 {
|
||||
state.fill_gaps(c as u32);
|
||||
}
|
||||
}
|
||||
|
||||
# Nor may a clear or a copy end within a word.
|
||||
for t in 0..state.touch.length {
|
||||
val at: u32 = state.touch[t];
|
||||
val held: inst = this.insts[at];
|
||||
continue if held.kind != op::clear && held.kind != op::copy;
|
||||
for k in 0..held.argc {
|
||||
val cell := state.addresses.cell(this.arg(at, k as u32));
|
||||
continue if cell == 0 || state.escaped[cell - 1];
|
||||
val c := cell - 1;
|
||||
var i: u32 = state.heads[c];
|
||||
while i != 0 {
|
||||
val each: slot = state.slots[i - 1];
|
||||
if each.offset < held.imm && held.imm < each.offset + each.size {
|
||||
state.escaped[c] = true;
|
||||
}
|
||||
i = each.next;
|
||||
}
|
||||
}
|
||||
}
|
||||
this.find_uses(state);
|
||||
}
|
||||
|
||||
#! Gives a cell the words another holds within the bytes a copy between them
|
||||
#! moves, which it does not hold yet. An escaped cell's words say nothing of
|
||||
#! the values another holds, as an array indexed holds what is read of it one
|
||||
#! lane at a time, so it gives none, and a vector copied into it stays one.
|
||||
#! @param this What promoting knows.
|
||||
#! @param from The cell whose words are given.
|
||||
#! @param to The cell given them.
|
||||
#! @param size How many bytes the copy moves.
|
||||
#! @returns Whether any was new.
|
||||
def promotion::share_slots(&mut this, from: u32, to: u32, size: u64): bool {
|
||||
return false if from == to || this.escaped[to] || this.escaped[from];
|
||||
var added := false;
|
||||
var i: u32 = this.heads[from];
|
||||
while i != 0 {
|
||||
val each: slot = this.slots[i - 1];
|
||||
i = each.next;
|
||||
continue if each.offset >= size || this.slot_at(to, each.offset) != no_slot;
|
||||
this.add_slot(to, each.offset, each.size, each.kind, each.signed);
|
||||
added = true;
|
||||
}
|
||||
return added;
|
||||
}
|
||||
|
||||
#! Gives the bytes a cell's copies move that no word holds words of their own,
|
||||
#! each as wide as where it begins and the bytes left allow.
|
||||
#! @param this What promoting knows.
|
||||
#! @param cell The cell, whose words do not overlap.
|
||||
def promotion::fill_gaps(&mut this, cell: u32): void {
|
||||
# The words it holds, by where each begins.
|
||||
var held := vector::<u64>::new(scratch());
|
||||
var i: u32 = this.heads[cell];
|
||||
while i != 0 {
|
||||
val each: slot = this.slots[i - 1];
|
||||
held.append(pair_key(each.offset as u32, each.size as u32));
|
||||
i = each.next;
|
||||
}
|
||||
held.sort();
|
||||
|
||||
val extent: u64 = this.extent[cell];
|
||||
var at: u64 = 0;
|
||||
for &key in held {
|
||||
break if at >= extent;
|
||||
val offset := pair_high(key) as u64;
|
||||
this.fill(cell, at, offset < extent ? offset : extent);
|
||||
val end := offset + pair_low(key) as u64;
|
||||
if end > at {
|
||||
at = end;
|
||||
}
|
||||
}
|
||||
this.fill(cell, at, extent);
|
||||
}
|
||||
|
||||
#! Gives bytes of a cell no word holds words of their own: in an executable's
|
||||
#! code, each 16 bytes from a multiple of 16 a vector, which a copy moves with
|
||||
#! one read and write, or 32 from a multiple of 32 in code for processors with
|
||||
#! AVX, and the rest words.
|
||||
#! @param this What promoting knows.
|
||||
#! @param cell The cell.
|
||||
#! @param from Where the bytes begin.
|
||||
#! @param to Where they end.
|
||||
def promotion::fill(&mut this, cell: u32, from: u64, to: u64): void {
|
||||
var at := from;
|
||||
while this.wide && at % 32 == 0 && at + 32 <= to {
|
||||
this.add_slot(cell, at, 32, vt::v256, false);
|
||||
at += 32;
|
||||
}
|
||||
while this.vectors && at % 16 == 0 && at + 16 <= to {
|
||||
this.add_slot(cell, at, 16, vt::v128, false);
|
||||
at += 16;
|
||||
}
|
||||
while at < to {
|
||||
var width: u64 = 8;
|
||||
while width > 1 && (at % width != 0 || at + width > to) {
|
||||
width = width / 2;
|
||||
}
|
||||
this.add_slot(cell, at, width, vt::word, false);
|
||||
at += width;
|
||||
}
|
||||
}
|
||||
|
||||
#! Splits each copy from a cell its words cover whole, and not escaped, into
|
||||
#! another into a read of each word and a write of it where it goes.
|
||||
#! @param this The code.
|
||||
#! @param state What promoting knows.
|
||||
#! @returns Whether any copy was split.
|
||||
def code::split_pairs(&mut this, state: &mut promotion): bool {
|
||||
var done := rewrite::new();
|
||||
var added := vector::<u32>::new(scratch());
|
||||
for p in 0..state.pairs.length {
|
||||
val copy: u32 = state.pairs[p];
|
||||
val held: inst = this.insts[copy];
|
||||
val source := state.addresses.cell(this.arg(copy, 1)) - 1;
|
||||
continue if state.escaped[source];
|
||||
var covered: u64 = 0;
|
||||
var i: u32 = state.heads[source];
|
||||
while i != 0 {
|
||||
if state.slots[i - 1].offset < held.imm {
|
||||
covered += state.slots[i - 1].size;
|
||||
}
|
||||
i = state.slots[i - 1].next;
|
||||
}
|
||||
continue if covered != held.imm;
|
||||
|
||||
# Only the words the cell it goes to reads go, where it holds them, each
|
||||
# after the copy, which goes.
|
||||
val target := state.addresses.cell(this.arg(copy, 0)) - 1;
|
||||
var after := copy;
|
||||
i = state.heads[source];
|
||||
while i != 0 {
|
||||
val each: slot = state.slots[i - 1];
|
||||
i = each.next;
|
||||
continue if each.offset >= held.imm;
|
||||
val there := state.slot_at(target, each.offset);
|
||||
continue if !state.escaped[target] && there != no_slot && !state.slots[there].used;
|
||||
var from: u32[1] = [this.arg(copy, 1)];
|
||||
var gives: vt[1] = [each.kind];
|
||||
val how := slot_access(&each) as u32;
|
||||
val load := this.detached(op::load, from[0:1], gives[0:1], how, each.offset, held.at, held.site);
|
||||
this.link_after(load, after);
|
||||
var to: u32[2] = [this.arg(copy, 0), this.insts[load].result];
|
||||
val store := this.detached(op::store, to[0:2], span::<vt>::blank(), how, each.offset, held.at, held.site);
|
||||
this.link_after(store, load);
|
||||
after = store;
|
||||
added.append(load);
|
||||
added.append(store);
|
||||
}
|
||||
done.remove(copy);
|
||||
}
|
||||
return false if !done.changed;
|
||||
this.apply(&done);
|
||||
|
||||
# The instructions naming cells are those that did but the copies split, and
|
||||
# the reads and writes they became.
|
||||
var kept: u64 = 0;
|
||||
for t in 0..state.touch.length {
|
||||
val at: u32 = state.touch[t];
|
||||
if !done.is_removed(at) {
|
||||
state.touch[kept] = at;
|
||||
kept++;
|
||||
}
|
||||
}
|
||||
state.touch.resize(kept);
|
||||
state.touch.extend(added.span);
|
||||
return true;
|
||||
}
|
||||
|
||||
#! Adds a word to those a cell holds, where it is new, marking the cell
|
||||
#! escaped where the same offset is read or written as another width or kind,
|
||||
#! or where it is 32 bytes of lanes and the code may run without AVX, or 64.
|
||||
#! @param this What promoting knows.
|
||||
#! @param cell The cell.
|
||||
#! @param offset Where the word begins.
|
||||
#! @param size How many bytes it is.
|
||||
#! @param kind The type of value it holds.
|
||||
#! @param signed Whether it is read as a signed integer narrower than a word.
|
||||
def promotion::add_slot(&mut this, cell: u32, offset: u64, size: u64, kind: vt, signed: bool): void {
|
||||
# 64 bytes of lanes stay in memory, as only code knowing the processor has
|
||||
# AVX-512 holds them.
|
||||
if kind == vt::v512 || (kind == vt::v256 && !this.wide) {
|
||||
this.escaped[cell] = true;
|
||||
return;
|
||||
}
|
||||
var i: u32 = this.heads[cell];
|
||||
while i != 0 {
|
||||
val each: slot = this.slots[i - 1];
|
||||
if each.offset == offset {
|
||||
if each.size != size || each.kind != kind {
|
||||
this.escaped[cell] = true;
|
||||
}
|
||||
if signed {
|
||||
this.slots[i - 1].signed = true;
|
||||
}
|
||||
return;
|
||||
}
|
||||
i = each.next;
|
||||
}
|
||||
this.slots.append(slot { cell = cell, offset = offset, size = size, kind = kind, signed = signed, next = this.heads[cell] });
|
||||
this.heads[cell] = this.slots.length as u32;
|
||||
}
|
||||
|
||||
#! The slot of a cell at an offset, or `no_slot`.
|
||||
#! @param this What promoting knows.
|
||||
#! @param cell The cell.
|
||||
#! @param offset Where the word begins.
|
||||
#! @returns The slot's index.
|
||||
def promotion::slot_at(&this, cell: u32, offset: u64): u32 {
|
||||
var i: u32 = this.heads[cell];
|
||||
while i != 0 {
|
||||
return i - 1 if this.slots[i - 1].offset == offset;
|
||||
i = this.slots[i - 1].next;
|
||||
}
|
||||
return no_slot;
|
||||
}
|
||||
|
||||
#! Whether a slot of a cell that bytes of it lie within is used. The cell copied
|
||||
#! into may hold a word over several of the other's, as a vector over a struct's
|
||||
#! fields where one cell holds each in turn, so each of those is used where the
|
||||
#! word over it is.
|
||||
#! @param this What promoting knows.
|
||||
#! @param cell The cell.
|
||||
#! @param offset Where the bytes begin.
|
||||
#! @param size How many there are.
|
||||
#! @returns Whether one is.
|
||||
def promotion::used_over(&this, cell: u32, offset: u64, size: u64): bool {
|
||||
var i: u32 = this.heads[cell];
|
||||
while i != 0 {
|
||||
val each: slot = this.slots[i - 1];
|
||||
return true if each.used && each.offset < offset + size && offset < each.offset + each.size;
|
||||
i = each.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#! Notes the value each slot holds at the end of a block that writes it: what
|
||||
#! it last stores or copies in, or zero where it last clears it.
|
||||
#! @param this The code.
|
||||
#! @param block The block.
|
||||
#! @param state What promoting knows.
|
||||
def code::note_ends(&mut this, block: u32, state: &mut promotion): void {
|
||||
var walk := this.walk(block);
|
||||
while walk.more() {
|
||||
val at := walk.next();
|
||||
val held: inst = this.insts[at];
|
||||
continue if held.argc == 0;
|
||||
val target := state.addresses.cell(this.arg(at, 0));
|
||||
continue if target == 0 || state.escaped[target - 1];
|
||||
val c := target - 1;
|
||||
match held.kind {
|
||||
op::store => {
|
||||
state.set_end(state.slot_at(c, held.imm), block, this.arg(at, 1));
|
||||
}
|
||||
op::clear => {
|
||||
var i: u32 = state.heads[c];
|
||||
while i != 0 {
|
||||
val s := i - 1;
|
||||
val each: slot = state.slots[s];
|
||||
i = each.next;
|
||||
continue if each.offset >= held.imm;
|
||||
state.set_end(s, block, state.zeros.of(this, each.kind));
|
||||
}
|
||||
}
|
||||
# A copy in reads each word something reads from where it comes from,
|
||||
# read here, before any block asks what a slot holds. Where nothing reads
|
||||
# any, its first is still read, so an address that cannot be read stops
|
||||
# the program there as the copy did.
|
||||
op::copy => {
|
||||
var reads := !this.checked_address(at, this.arg(at, 1));
|
||||
var i: u32 = state.heads[c];
|
||||
while i != 0 {
|
||||
val each: slot = state.slots[i - 1];
|
||||
if each.offset < held.imm && each.used {
|
||||
reads = false;
|
||||
}
|
||||
i = each.next;
|
||||
}
|
||||
var after := at;
|
||||
i = state.heads[c];
|
||||
while i != 0 {
|
||||
val s := i - 1;
|
||||
val each: slot = state.slots[s];
|
||||
i = each.next;
|
||||
continue if each.offset >= held.imm || (!each.used && !(reads && each.offset == 0));
|
||||
var args: u32[1] = [this.arg(at, 1)];
|
||||
var gives: vt[1] = [each.kind];
|
||||
val load := this.detached(op::load, args[0:1], gives[0:1], slot_access(&each) as u32, each.offset, held.at, held.site);
|
||||
this.link_after(load, after);
|
||||
after = load;
|
||||
val value: u32 = this.insts[load].result;
|
||||
state.copies.set(pair_key(at, s), value);
|
||||
state.set_end(s, block, value);
|
||||
}
|
||||
}
|
||||
else => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#! The value a slot holds at the start of a block: zero at the entry, what it
|
||||
#! holds at the end of the one block going there, or else a parameter the
|
||||
#! block takes, given what it holds at the end of each.
|
||||
#! @param this The code.
|
||||
#! @param s The slot.
|
||||
#! @param block The block.
|
||||
#! @param state What promoting knows.
|
||||
#! @returns The value.
|
||||
def code::slot_start(&mut this, s: u32, block: u32, state: &mut promotion): u32 {
|
||||
val known := state.start_at(s, block);
|
||||
return known if known != 0;
|
||||
val kind := state.slots[s].kind;
|
||||
val preds := this.blocks[block].preds.length;
|
||||
if block == 0 || preds == 0 {
|
||||
val zero := state.zeros.of(this, kind);
|
||||
state.set_start(s, block, zero);
|
||||
return zero;
|
||||
}
|
||||
if preds == 1 {
|
||||
val edge: u32 = this.blocks[block].preds[0];
|
||||
val made := this.slot_end(s, this.edge_source(edge), state);
|
||||
state.set_start(s, block, made);
|
||||
return made;
|
||||
}
|
||||
|
||||
# Noted before its arguments are found, which may come back to it.
|
||||
val param := this.add_param(block, kind);
|
||||
state.set_start(s, block, param);
|
||||
var edges := this.blocks[block].preds.copied(scratch());
|
||||
for &edge in edges {
|
||||
val given := this.slot_end(s, this.edge_source(edge), state);
|
||||
this.edges[edge].args.append(given);
|
||||
}
|
||||
return param;
|
||||
}
|
||||
|
||||
#! The value a slot holds at the end of a block.
|
||||
#! @param this The code.
|
||||
#! @param s The slot.
|
||||
#! @param block The block.
|
||||
#! @param state What promoting knows.
|
||||
#! @returns The value.
|
||||
def code::slot_end(&mut this, s: u32, block: u32, state: &mut promotion): u32 {
|
||||
val known := state.end_at(s, block);
|
||||
return known if known != 0;
|
||||
return this.slot_start(s, block, state);
|
||||
}
|
||||
|
||||
#! Notes what a slot holds where the walk of a block is.
|
||||
#! @param this What promoting knows.
|
||||
#! @param s The slot.
|
||||
#! @param value The value.
|
||||
def promotion::hold(&mut this, s: u32, value: u32): void {
|
||||
this.current[s] = value;
|
||||
this.written.put(s);
|
||||
}
|
||||
|
||||
#! What a slot holds where the walk of a block is: what the block wrote last,
|
||||
#! or what it holds where the block begins.
|
||||
#! @param this The code.
|
||||
#! @param s The slot.
|
||||
#! @param block The block.
|
||||
#! @param state What promoting knows.
|
||||
#! @returns The value.
|
||||
def code::slot_now(&mut this, s: u32, block: u32, state: &mut promotion): u32 {
|
||||
return state.current[s] if state.written.contains(s);
|
||||
return this.slot_start(s, block, state);
|
||||
}
|
||||
|
||||
#! Rewrites the reads and writes of the slots of a block: a read reads what
|
||||
#! the slot holds there, narrowed as the read narrows, and a write, a clear,
|
||||
#! and a copy in or out go, a copy becoming reads or writes of its words.
|
||||
#! @param this The code.
|
||||
#! @param block The block.
|
||||
#! @param state What promoting knows.
|
||||
#! @param done What the pass changes.
|
||||
def code::rewrite_slots(&mut this, block: u32, state: &mut promotion, done: &mut rewrite): void {
|
||||
var walk := this.walk(block);
|
||||
while walk.more() {
|
||||
val at := walk.next();
|
||||
val held: inst = this.insts[at];
|
||||
if held.kind == op::cell {
|
||||
if !state.escaped[held.aux] {
|
||||
done.remove(at);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
continue if held.argc == 0;
|
||||
|
||||
val first := state.addresses.cell(this.arg(at, 0));
|
||||
val c := first != 0 && !state.escaped[first - 1] ? first - 1 : no_cell;
|
||||
match held.kind {
|
||||
op::check => {
|
||||
if c != no_cell {
|
||||
done.remove(at);
|
||||
}
|
||||
}
|
||||
op::load => {
|
||||
if c != no_cell {
|
||||
val s := state.slot_at(c, held.imm);
|
||||
val value := this.slot_now(s, block, state);
|
||||
this.read_slot(at, value, done);
|
||||
}
|
||||
}
|
||||
op::store => {
|
||||
if c != no_cell {
|
||||
state.hold(state.slot_at(c, held.imm), this.arg(at, 1));
|
||||
done.remove(at);
|
||||
}
|
||||
}
|
||||
op::clear => {
|
||||
if c != no_cell {
|
||||
var i: u32 = state.heads[c];
|
||||
while i != 0 {
|
||||
val each: slot = state.slots[i - 1];
|
||||
if each.offset < held.imm {
|
||||
state.hold(i - 1, state.zeros.of(this, each.kind));
|
||||
}
|
||||
i = each.next;
|
||||
}
|
||||
done.remove(at);
|
||||
}
|
||||
}
|
||||
op::copy => {
|
||||
val from := state.addresses.cell(this.arg(at, 1));
|
||||
if c != no_cell {
|
||||
# Copied in: each word is what was read of where it comes from.
|
||||
var i: u32 = state.heads[c];
|
||||
while i != 0 {
|
||||
val key := pair_key(at, i - 1);
|
||||
if state.copies.get(&key) is some(got) {
|
||||
state.hold(i - 1, got);
|
||||
}
|
||||
i = state.slots[i - 1].next;
|
||||
}
|
||||
done.remove(at);
|
||||
} else if from != 0 && !state.escaped[from - 1] {
|
||||
# Copied out: each word is written where it goes, after the copy,
|
||||
# which goes.
|
||||
val source := from - 1;
|
||||
var after := at;
|
||||
var i: u32 = state.heads[source];
|
||||
while i != 0 {
|
||||
val s := i - 1;
|
||||
val each: slot = state.slots[s];
|
||||
i = each.next;
|
||||
continue if each.offset >= held.imm;
|
||||
val value := this.slot_now(s, block, state);
|
||||
var args: u32[2] = [this.arg(at, 0), value];
|
||||
val store := this.detached(op::store, args[0:2], span::<vt>::blank(), slot_access(&each) as u32, each.offset, held.at, held.site);
|
||||
this.link_after(store, after);
|
||||
after = store;
|
||||
}
|
||||
done.remove(at);
|
||||
}
|
||||
}
|
||||
else => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#! How a slot is read and written in memory where a copy moves it: a narrow
|
||||
#! integer as it is read, so a field of `i32` copied is narrowed as a read of it
|
||||
#! narrows it again, and the two are one.
|
||||
#! @param each The slot.
|
||||
#! @returns The access.
|
||||
def slot_access(each: &slot): access {
|
||||
return access::v128 if each.kind == vt::v128;
|
||||
return access::v256 if each.kind == vt::v256;
|
||||
return each.kind == vt::real ? (each.size == 4 ? access::f32 : access::f64) : match each.size {
|
||||
1 => each.signed ? access::i8 : access::u8,
|
||||
2 => each.signed ? access::i16 : access::u16,
|
||||
4 => each.signed ? access::i32 : access::u32,
|
||||
else => access::i64
|
||||
};
|
||||
}
|
||||
|
||||
#! Whether an access reads a signed integer narrower than a word.
|
||||
#! @param how The access.
|
||||
#! @returns Whether it does.
|
||||
def narrow_signed(how: access): bool {
|
||||
return how == access::i8 || how == access::i16 || how == access::i32;
|
||||
}
|
||||
|
||||
#! Makes a read of a slot the value it holds, narrowed as the read narrows.
|
||||
#! @param this The code.
|
||||
#! @param index The `load`.
|
||||
#! @param value What the slot holds.
|
||||
#! @param done What the pass changes.
|
||||
def code::read_slot(&mut this, index: u32, value: u32, done: &mut rewrite): void {
|
||||
val how: access = this.insts[index].aux as access;
|
||||
var args: u32[1] = [value];
|
||||
match how {
|
||||
access::i64, access::f64, access::v128, access::v256 => {
|
||||
done.replace(this.insts[index].result, value);
|
||||
done.remove(index);
|
||||
}
|
||||
access::f32 => this.become(index, op::fsingle, args[0:1], 0, 0);
|
||||
access::u8 => this.become(index, op::wrap, args[0:1], 8, 0);
|
||||
access::i8 => this.become(index, op::wrap, args[0:1], 8, 1);
|
||||
access::u16 => this.become(index, op::wrap, args[0:1], 16, 0);
|
||||
access::i16 => this.become(index, op::wrap, args[0:1], 16, 1);
|
||||
access::u32 => this.become(index, op::wrap, args[0:1], 32, 0);
|
||||
access::i32 => this.become(index, op::wrap, args[0:1], 32, 1);
|
||||
else => {}
|
||||
}
|
||||
done.changed = true;
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,271 @@
|
||||
#! @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;
|
||||
}
|
||||
@@ -0,0 +1,394 @@
|
||||
#! @file split.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains what promotion does first: splitting a cell that holds several
|
||||
#! values one after another into a cell for each. A statement's temporary cell
|
||||
#! is taken again by the statements after it, and inlining copies whole the
|
||||
#! cells of what it inlines, so one cell holds many values, each written whole
|
||||
#! before it is read. One use of the cell that keeps it in memory, as a call
|
||||
#! given its address does, would keep all of them there, and a value of one
|
||||
#! width read where another of another width was would too. So each value the
|
||||
#! cell holds is given a cell of its own, which promotes or not by its own
|
||||
#! uses.
|
||||
#!
|
||||
#! A cell's value begins where the cell is written whole, by one write or by
|
||||
#! writes one after another that nothing reads between, over every byte any of
|
||||
#! its uses touches, and lasts to where the next begins. A value that begins
|
||||
#! and ends within a block, or that begins there and is read in no block after,
|
||||
#! is split off; the value a block begins with, and the one it ends with that
|
||||
#! a block after reads, stay in the cell. A cell is split only where its
|
||||
#! address goes nowhere but to what reads, writes, copies, and clears it, or,
|
||||
#! of a statement's temporary cell, which nothing points into past the
|
||||
#! statement, to a call: an address given on as a value, kept in memory, or
|
||||
#! given a block, may be read through anywhere.
|
||||
|
||||
module sakura::compiler;
|
||||
|
||||
|
||||
#! The most bytes of a cell whose writes are followed a byte at a time, so that
|
||||
#! writes of its parts one after another may write it whole.
|
||||
const split_bytes: u64 = 64;
|
||||
|
||||
#! What splitting knows of the cells of the code.
|
||||
struct splitting {
|
||||
var addresses: cell_table; #! Which cell each value is the address of.
|
||||
var place: vector<u32>; #! The place of each cell among those split, or `unfollowed`.
|
||||
var cells: vector<u32>; #! The cell at each place.
|
||||
var extent: vector<u64>; #! Of each place, how many bytes from its start its uses touch.
|
||||
var begins: vector<bool>; #! Of each instruction, whether a value of the cell it writes, its first operand's, begins there.
|
||||
var round: vector<u32>; #! Of each place, the block it was last followed in, as one more than its place in the order, or 0.
|
||||
var pending: vector<u32>; #! Of each place, the first of the writes since it was last read, or 0.
|
||||
var covered: vector<u64>; #! Of each place, the bytes those writes cover, a bit each.
|
||||
var heads: vector<u32>; #! Of each place, one more than the last use of the value begun in the block, among `uses`, or 0.
|
||||
var uses: vector<u64>; #! The operands naming the cells, each as its instruction and its place among its operands, by `pair_key`.
|
||||
var links: vector<u32>; #! Of each use, one more than the use of the same value before it, or 0.
|
||||
var words: u64; #! The words of a block's bits of the places.
|
||||
var live_out: vector<u64>; #! Of each block, the places whose value a block after it reads, a bit each.
|
||||
}
|
||||
|
||||
#! Splits the cells that hold several values into a cell for each, as the file
|
||||
#! says.
|
||||
#! @param this The code.
|
||||
#! @returns Whether any was.
|
||||
pub def code::split_cells(&mut this): bool {
|
||||
return false if this.cells.length == 0;
|
||||
var state := splitting {
|
||||
addresses = this.cell_table(),
|
||||
place = vector::<u32>::new(scratch()),
|
||||
cells = vector::<u32>::new(scratch()),
|
||||
extent = vector::<u64>::new(scratch()),
|
||||
begins = vector::<bool>::new(scratch()),
|
||||
round = vector::<u32>::new(scratch()),
|
||||
pending = vector::<u32>::new(scratch()),
|
||||
covered = vector::<u64>::new(scratch()),
|
||||
heads = vector::<u32>::new(scratch()),
|
||||
uses = vector::<u64>::new(scratch()),
|
||||
links = vector::<u32>::new(scratch()),
|
||||
live_out = vector::<u64>::new(scratch())
|
||||
};
|
||||
return false if !state.addresses.any;
|
||||
return false if !this.find_splittable(&mut state);
|
||||
|
||||
var order := vector::<u32>::new(scratch());
|
||||
var number := vector::<u32>::new(scratch());
|
||||
this.block_order(&mut order, &mut number);
|
||||
val count := state.cells.length;
|
||||
state.begins.resize(this.insts.length);
|
||||
state.round.resize(count);
|
||||
state.pending.resize(count);
|
||||
state.covered.resize(count);
|
||||
state.heads.resize(count);
|
||||
for k in 0..order.length {
|
||||
this.find_beginnings(order[k], (k + 1) as u32, &mut state);
|
||||
}
|
||||
this.find_live_out(order.span, &mut state);
|
||||
|
||||
for k in 0..count {
|
||||
state.round[k] = 0;
|
||||
}
|
||||
var changed := false;
|
||||
for k in 0..order.length {
|
||||
changed = this.split_block(order[k], (k + 1) as u32, &mut state) || changed;
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
|
||||
#! Finds the cells that may be split, and how many bytes each one's uses touch.
|
||||
#! @param this The code.
|
||||
#! @param state What splitting knows.
|
||||
#! @returns Whether any may.
|
||||
def code::find_splittable(&this, state: &mut splitting): bool {
|
||||
val count := this.cells.length;
|
||||
state.place.resize(count, unfollowed);
|
||||
var refused := vector::<bool>::new(scratch());
|
||||
refused.resize(count);
|
||||
var reach := vector::<u64>::new(scratch());
|
||||
reach.resize(count);
|
||||
for c in 0..count {
|
||||
val held: cell = this.cells[c];
|
||||
refused[c] = held.name != 0 || (held.kind != ck::temporary && held.kind != ck::inlined);
|
||||
}
|
||||
|
||||
var reads := this.reader();
|
||||
var walk := this.walk_all();
|
||||
while walk.more() {
|
||||
val at := walk.next();
|
||||
val held: inst = this.insts[at];
|
||||
reads.start(at);
|
||||
while reads.more() {
|
||||
val cell := state.addresses.cell(reads.next());
|
||||
continue if cell == 0;
|
||||
val c := cell - 1;
|
||||
continue if refused[c];
|
||||
val a := reads.operand();
|
||||
var touched: u64 = 0;
|
||||
var known := a != no_operand;
|
||||
if known {
|
||||
match held.kind {
|
||||
op::load => { known = a == 0; touched = held.imm + (held.aux as access).width(); }
|
||||
op::store => { known = a == 0; touched = held.imm + (held.aux as access).width(); }
|
||||
op::clear => { known = a == 0; touched = held.imm; }
|
||||
op::copy => {
|
||||
known = this.arg(at, 0) != this.arg(at, 1);
|
||||
touched = held.imm;
|
||||
}
|
||||
op::check, op::forget => {}
|
||||
op::call, op::call_trait, op::call_value, op::call_c, op::equal, op::format_value => {
|
||||
known = this.cells[c].kind == ck::temporary;
|
||||
touched = this.cells[c].size as u64;
|
||||
}
|
||||
else => known = false;
|
||||
}
|
||||
}
|
||||
if !known {
|
||||
refused[c] = true;
|
||||
continue;
|
||||
}
|
||||
if touched > reach[c] {
|
||||
reach[c] = touched;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for c in 0..count {
|
||||
continue if refused[c] || reach[c] == 0;
|
||||
state.place[c] = state.cells.length as u32;
|
||||
state.cells.append(c as u32);
|
||||
state.extent.append(reach[c]);
|
||||
}
|
||||
state.words = (state.cells.length as u64 + 63) / 64;
|
||||
return state.cells.length != 0;
|
||||
}
|
||||
|
||||
#! The bits of the bytes from one offset to another, of a cell followed a byte
|
||||
#! at a time.
|
||||
#! @param from Where they begin.
|
||||
#! @param to Where they end.
|
||||
#! @returns The bits.
|
||||
def split_mask(from: u64, to: u64): u64 {
|
||||
return 0 if from >= to || from >= split_bytes;
|
||||
val end := to > split_bytes ? split_bytes : to;
|
||||
val high := end >= 64 ? 0xFFFFFFFFFFFFFFFFu : (1u << end) - 1;
|
||||
return high & ~((1u << from) - 1);
|
||||
}
|
||||
|
||||
#! The place among those split of the cell a value is the address of.
|
||||
#! @param this What splitting knows.
|
||||
#! @param value The value.
|
||||
#! @returns The place, or `unfollowed`.
|
||||
def splitting::place_of(&this, value: u32): u32 {
|
||||
val cell := this.addresses.cell(value);
|
||||
return cell == 0 ? unfollowed : this.place[cell - 1];
|
||||
}
|
||||
|
||||
#! Begins following a place in a block, where it was not yet.
|
||||
#! @param this What splitting knows.
|
||||
#! @param k The place.
|
||||
#! @param round The block's round.
|
||||
def splitting::enter(&mut this, k: u32, round: u32): void {
|
||||
return if this.round[k] == round;
|
||||
this.round[k] = round;
|
||||
this.pending[k] = 0;
|
||||
this.covered[k] = 0;
|
||||
this.heads[k] = 0;
|
||||
}
|
||||
|
||||
#! Finds where the values of the cells begin in a block: at a write of a cell
|
||||
#! that covers every byte its uses touch, or at the first of writes one after
|
||||
#! another that do, nothing reading the cell between.
|
||||
#! @param this The code.
|
||||
#! @param block The block.
|
||||
#! @param round One more than its place in the order.
|
||||
#! @param state What splitting knows.
|
||||
def code::find_beginnings(&this, block: u32, round: u32, state: &mut splitting): void {
|
||||
var reads := this.reader();
|
||||
var walk := this.walk(block);
|
||||
while walk.more() {
|
||||
val at := walk.next();
|
||||
val held: inst = this.insts[at];
|
||||
var written: u32 = unfollowed;
|
||||
var from: u64 = 0;
|
||||
var to: u64 = 0;
|
||||
match held.kind {
|
||||
op::store => {
|
||||
written = state.place_of(this.arg(at, 0));
|
||||
from = held.imm;
|
||||
to = held.imm + (held.aux as access).width();
|
||||
}
|
||||
op::clear, op::copy => {
|
||||
written = state.place_of(this.arg(at, 0));
|
||||
to = held.imm;
|
||||
}
|
||||
else => {}
|
||||
}
|
||||
# Every other use of a cell reads it.
|
||||
reads.start(at);
|
||||
while reads.more() {
|
||||
val k := state.place_of(reads.next());
|
||||
continue if k == unfollowed || (k == written && reads.operand() == 0);
|
||||
state.enter(k, round);
|
||||
state.pending[k] = 0;
|
||||
state.covered[k] = 0;
|
||||
}
|
||||
continue if written == unfollowed;
|
||||
state.enter(written, round);
|
||||
val extent: u64 = state.extent[written];
|
||||
if from == 0 && to >= extent {
|
||||
state.begins[at] = true;
|
||||
state.pending[written] = 0;
|
||||
state.covered[written] = 0;
|
||||
continue;
|
||||
}
|
||||
continue if extent > split_bytes;
|
||||
if state.pending[written] == 0 {
|
||||
state.pending[written] = at;
|
||||
}
|
||||
state.covered[written] = state.covered[written] | split_mask(from, to);
|
||||
if state.covered[written] == split_mask(0, extent) {
|
||||
state.begins[state.pending[written]] = true;
|
||||
state.pending[written] = 0;
|
||||
state.covered[written] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#! Finds which values of the cells each block ends with a block after it reads:
|
||||
#! where some block it goes to reads the cell before a value of it begins
|
||||
#! there, or goes on without one beginning to a block that does.
|
||||
#! @param this The code.
|
||||
#! @param order The blocks, in reverse postorder.
|
||||
#! @param state What splitting knows.
|
||||
def code::find_live_out(&this, order: span<u32>, state: &mut splitting): void {
|
||||
val words := state.words;
|
||||
val blocks := this.blocks.length;
|
||||
var exposed := vector::<u64>::new(scratch());
|
||||
var killed := vector::<u64>::new(scratch());
|
||||
var live_in := vector::<u64>::new(scratch());
|
||||
exposed.resize(blocks * words);
|
||||
killed.resize(blocks * words);
|
||||
live_in.resize(blocks * words);
|
||||
state.live_out.resize(blocks * words);
|
||||
|
||||
var reads := this.reader();
|
||||
for &block in order {
|
||||
val base := block as u64 * words;
|
||||
var walk := this.walk(block);
|
||||
while walk.more() {
|
||||
val at := walk.next();
|
||||
val begun := state.begins[at] ? state.place_of(this.arg(at, 0)) : unfollowed;
|
||||
if begun != unfollowed {
|
||||
val bit := 1u << (begun as u64 % 64);
|
||||
val w := base + begun as u64 / 64;
|
||||
if (exposed[w] & bit) == 0 {
|
||||
killed[w] = killed[w] | bit;
|
||||
}
|
||||
}
|
||||
reads.start(at);
|
||||
while reads.more() {
|
||||
val k := state.place_of(reads.next());
|
||||
continue if k == unfollowed || k == begun;
|
||||
val bit := 1u << (k as u64 % 64);
|
||||
val w := base + k as u64 / 64;
|
||||
if (killed[w] & bit) == 0 {
|
||||
exposed[w] = exposed[w] | bit;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Backward, until nothing changes.
|
||||
var changed := true;
|
||||
while changed {
|
||||
changed = false;
|
||||
var k := order.length;
|
||||
while k > 0 {
|
||||
k--;
|
||||
val block: u32 = order[k];
|
||||
val base := block as u64 * words;
|
||||
val last: u32 = this.blocks[block].last;
|
||||
if last != 0 {
|
||||
var edges := this.edges_of(last);
|
||||
while edges.more() {
|
||||
val target := this.edges[edges.next()].target as u64;
|
||||
for w in 0..words {
|
||||
state.live_out[base + w] = state.live_out[base + w] | live_in[target * words + w];
|
||||
}
|
||||
}
|
||||
}
|
||||
for w in 0..words {
|
||||
val now := exposed[base + w] | (state.live_out[base + w] & ~killed[base + w]);
|
||||
if now != live_in[base + w] {
|
||||
live_in[base + w] = now;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#! Splits off each value of the cells that begins in a block and ends there, or
|
||||
#! that no block after reads, into a cell of its own.
|
||||
#! @param this The code.
|
||||
#! @param block The block.
|
||||
#! @param round One more than its place in the order.
|
||||
#! @param state What splitting knows.
|
||||
#! @returns Whether any was.
|
||||
def code::split_block(&mut this, block: u32, round: u32, state: &mut splitting): bool {
|
||||
# The places of the values begun in the block, each once.
|
||||
var begun := vector::<u32>::new(scratch());
|
||||
var changed := false;
|
||||
var walk := this.walk(block);
|
||||
while walk.more() {
|
||||
val at := walk.next();
|
||||
val argc: u32 = this.insts[at].argc;
|
||||
if state.begins[at] {
|
||||
val k := state.place_of(this.arg(at, 0));
|
||||
if state.round[k] == round {
|
||||
changed = this.split_value(state, k) || changed;
|
||||
} else {
|
||||
state.round[k] = round;
|
||||
begun.append(k);
|
||||
}
|
||||
state.heads[k] = 0;
|
||||
}
|
||||
for a in 0..argc {
|
||||
val k := state.place_of(this.arg(at, a));
|
||||
continue if k == unfollowed || state.round[k] != round;
|
||||
state.uses.append(pair_key(at, a));
|
||||
state.links.append(state.heads[k]);
|
||||
state.heads[k] = state.uses.length as u32;
|
||||
}
|
||||
}
|
||||
val base := block as u64 * state.words;
|
||||
for &k in begun {
|
||||
continue if (state.live_out[base + k as u64 / 64] & (1u << (k as u64 % 64))) != 0;
|
||||
changed = this.split_value(state, k) || changed;
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
|
||||
#! Gives the value of a cell begun in the block being split a cell of its own: a
|
||||
#! copy of the cell, whose address is made first in the entry, named by each
|
||||
#! operand that named the cell.
|
||||
#! @param this The code.
|
||||
#! @param state What splitting knows.
|
||||
#! @param k The place of the cell.
|
||||
#! @returns Whether it was.
|
||||
def code::split_value(&mut this, state: &mut splitting, k: u32): bool {
|
||||
return false if state.heads[k] == 0;
|
||||
val held: cell = this.cells[state.cells[k]];
|
||||
val made := this.add_cell(held.size, held.align, held.kind, held.name);
|
||||
var gives: vt[1] = [vt::word];
|
||||
val index := this.detached(op::cell, span::<u32>::blank(), gives[0:1], made, 0, 0, 0);
|
||||
this.put_first(0, index);
|
||||
val address: u32 = this.insts[index].result;
|
||||
var i: u32 = state.heads[k];
|
||||
while i != 0 {
|
||||
val each: u64 = state.uses[i - 1];
|
||||
this.operands[this.insts[pair_high(each)].args as u64 + pair_low(each) as u64] = address;
|
||||
i = state.links[i - 1];
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
#! @file unused.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the dropping of the functions nothing reaches once the program is
|
||||
#! optimized, as those whose every call inlining copied. What is reached is
|
||||
#! found from what is reached other than by the code's calls: `main`, the code
|
||||
#! initializing the globals, the function safe points call, each function a
|
||||
#! vtable holds, each C calls, and each the program takes as a value or the
|
||||
#! backend calls by name. Each function reached then reaches what its code
|
||||
#! calls, calls at safe points, and gives C to call, and its version. The rest
|
||||
#! are dropped as though never lowered, so no code is made of them, each
|
||||
#! keeping its index, and its name, which a trace of the code inlined from it
|
||||
#! still gives.
|
||||
|
||||
module sakura::compiler;
|
||||
|
||||
|
||||
#! Drops the functions nothing reaches, as the file says, once the program is
|
||||
#! optimized and nothing more is lowered, but where every function is asked
|
||||
#! for, or the program evaluates constants.
|
||||
#! @param this The compiler.
|
||||
pub def lowerer::drop_unreached_functions(&mut this): void {
|
||||
return if this.options.everything || this.options.evaluating;
|
||||
val point := mark_scratch();
|
||||
val count := this.program.functions.length;
|
||||
var reached := worklist::new(count, scratch());
|
||||
this.reach_functions(&mut reached);
|
||||
for i in 0..count {
|
||||
if !reached.has(i as u32) {
|
||||
this.program.functions[i].lowered = false;
|
||||
}
|
||||
}
|
||||
release_scratch(point);
|
||||
}
|
||||
|
||||
#! Finds the functions reached, as the file says.
|
||||
#! @param this The compiler.
|
||||
#! @param reached Where the functions reached are put, empty and as long as
|
||||
#! the functions are; each is in it after.
|
||||
pub def lowerer::reach_functions(&this, reached: &mut worklist): void {
|
||||
val count := this.program.functions.length;
|
||||
for i in 0..count {
|
||||
if this.program.functions[i].taken || this.program.functions[i].callback {
|
||||
reached.push_new(i as u32);
|
||||
}
|
||||
}
|
||||
val roots: u32[3] = [this.program.main, this.program.init, this.dispatch];
|
||||
for r in 0..3u {
|
||||
if roots[r] != no_function {
|
||||
reached.push_new(roots[r]);
|
||||
}
|
||||
}
|
||||
for &each in this.program.vtables {
|
||||
for &function in each.functions {
|
||||
if function != no_function {
|
||||
reached.push_new(function);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
while !reached.empty {
|
||||
val function := reached.pop();
|
||||
val held := &this.program.functions[function];
|
||||
if held.version != 0 {
|
||||
reached.push_new(held.version - 1);
|
||||
}
|
||||
continue if !held.lowered;
|
||||
var walk := held.body.walk_all();
|
||||
while walk.more() {
|
||||
val at := walk.next();
|
||||
val kind := held.body.insts[at].kind;
|
||||
if kind == op::call || kind == op::callback || kind == op::safepoint {
|
||||
reached.push_new(held.body.insts[at].aux);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,171 @@
|
||||
#! @file versions.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the versions of functions for processors with x86-64-v3's
|
||||
#! instructions. A function of an executable that asks while running which
|
||||
#! vector instructions the processor has, as `core::simd`'s operations do, or
|
||||
#! counts a word's bits, is optimized again as a version knowing it has those of
|
||||
#! x86-64-v3: SSE through SSE4.2, AVX, AVX2, FMA3, `popcnt`, `lzcnt`, and BMI1's
|
||||
#! `tzcnt`. There, every operation on 16 or 32 bytes of lanes is worked with
|
||||
#! instructions, so the lanes written a lane at a time go, 32 bytes of lanes are
|
||||
#! values held in registers, and a count of bits is one instruction; what asks
|
||||
#! for more, as 64 bytes ask for AVX-512, still asks. The function itself goes
|
||||
#! to its version as it begins, where the processor has them, and its version
|
||||
#! calls the versions of the functions it calls.
|
||||
#!
|
||||
#! A function calling one whose version is small enough to inline is given a
|
||||
#! version too, though it asks nothing itself, so that its version copies the
|
||||
#! other's: a loop calling `fma` works it with the instruction, rather than
|
||||
#! calling a function that asks the processor each time.
|
||||
#!
|
||||
#! A version is optimized from the function's code as lowered, kept as it
|
||||
#! began optimizing, so what it calls is inlined from their versions, and
|
||||
#! its 32 bytes of lanes are promoted as the values they are, where the
|
||||
#! function's own code, optimized for any processor, holds them in memory a
|
||||
#! half at a time.
|
||||
|
||||
module sakura::compiler;
|
||||
|
||||
|
||||
#! The instruction sets of x86-64-v3, as the executable keeps them.
|
||||
pub const cpu_v3: u64 = cpu_sse2 | cpu_ssse3 | cpu_sse41 | cpu_sse42 | cpu_avx | cpu_avx2 | cpu_fma | cpu_popcnt
|
||||
| cpu_lzcnt | cpu_bmi1;
|
||||
|
||||
#! Whether code asks which instructions the processor has, or counts a word's
|
||||
#! bits, which x86-64-v3 has instructions for. The code is compacted, as each
|
||||
#! function is once it is optimized, so each instruction it holds is in a block,
|
||||
#! and is looked at where it lies.
|
||||
#! @param this The code.
|
||||
#! @returns Whether it does.
|
||||
def code::asks_processor(&this): bool {
|
||||
for i in 1..this.insts.length {
|
||||
val kind := this.insts[i].kind;
|
||||
return true if kind == op::cpu || kind == op::ones || kind == op::clz || kind == op::ctz;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#! Copies code whole, sharing no memory with it.
|
||||
#! @param this The code.
|
||||
#! @returns The copy.
|
||||
pub def code::duplicate(&this): code {
|
||||
var made := code::new();
|
||||
made.insts.clear();
|
||||
made.origins.clear();
|
||||
made.blocks.clear();
|
||||
made.insts.extend(this.insts.span);
|
||||
made.operands.extend(this.operands.span);
|
||||
made.origins.extend(this.origins.span);
|
||||
for &each in this.blocks {
|
||||
made.blocks.append(block { first = each.first, last = each.last, params = ids::copy_of(&each.params), preds = ids::copy_of(&each.preds), gone = each.gone });
|
||||
}
|
||||
for &each in this.edges {
|
||||
made.edges.append(edge { from = each.from, target = each.target, args = ids::copy_of(&each.args), case = each.case });
|
||||
}
|
||||
made.cells.extend(this.cells.span);
|
||||
made.results.extend(this.results.span);
|
||||
made.sites.extend(this.sites.span);
|
||||
made.variables.extend(this.variables.span);
|
||||
made.bindings.extend(this.bindings.span);
|
||||
made.returned = this.returned;
|
||||
made.vectors = this.vectors;
|
||||
made.wide = this.wide;
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Whether a function calls one with a version small enough to be inlined
|
||||
#! whole, or `@inline`, which its own version would copy.
|
||||
#! @param this The compiler.
|
||||
#! @param index The function.
|
||||
#! @returns Whether it does.
|
||||
def lowerer::calls_small_version(&mut this, index: u32): bool {
|
||||
val limit := this.options.inline_limit != 0 ? this.options.inline_limit : inline_limit;
|
||||
val body := &this.program.functions[index].body;
|
||||
for i in 1..body.insts.length {
|
||||
continue if body.insts[i].kind != op::call;
|
||||
val version: u32 = this.program.functions[body.insts[i].aux].version;
|
||||
continue if version == 0;
|
||||
val facts := this.inline_facts(version - 1);
|
||||
return true if facts.fits && (facts.cost <= limit || this.program.functions[version - 1].inline);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#! Whether the program's functions are given versions: in an executable
|
||||
#! optimized at all.
|
||||
#! @param this The compiler.
|
||||
#! @returns Whether they are.
|
||||
def lowerer::versions_made(&this): bool {
|
||||
return this.options.level >= 1 && !this.options.evaluating
|
||||
&& this.checker.names.text(this.checker.target.backend) == "native";
|
||||
}
|
||||
|
||||
#! Makes the version of a function optimized for x86-64-v3, where it asks which
|
||||
#! instructions the processor has, and optimizes it: what the processor has is
|
||||
#! known to hold those, and each call goes to the version of what it calls,
|
||||
#! where there is one.
|
||||
#! @param this The compiler.
|
||||
#! @param index The function, optimized.
|
||||
#! @param ranks Where each function is in the order functions are optimized
|
||||
#! in, as `optimizing` says, which the version is added to.
|
||||
#! @param rank Where the version is: after the function, and before the
|
||||
#! next in the order.
|
||||
pub def lowerer::make_version(&mut this, index: u32, ranks: &mut vector<u32>, rank: u32): void {
|
||||
return if !this.versions_made() || this.program.functions[index].body.wide;
|
||||
# One left unfinished is not compacted, and nothing reaches it, so it is
|
||||
# dropped rather than given a version.
|
||||
return if this.program.functions[index].unfinished;
|
||||
return if !this.program.functions[index].body.asks_processor() && !this.calls_small_version(index);
|
||||
|
||||
# Made from its code as lowered, where that was kept, so its lanes are
|
||||
# promoted as values whole rather than as the halves its optimized code holds.
|
||||
var body := code{};
|
||||
if this.program.functions[index].unoptimized.insts.length != 0 {
|
||||
body = move(&mut this.program.functions[index].unoptimized);
|
||||
} else {
|
||||
body = this.program.functions[index].body.duplicate();
|
||||
}
|
||||
body.wide = true;
|
||||
for i in 0..body.insts.length {
|
||||
val kind := body.insts[i].kind;
|
||||
if kind == op::cpu {
|
||||
body.insts[i].imm = cpu_v3;
|
||||
} else if kind == op::call {
|
||||
val version: u32 = this.program.functions[body.insts[i].aux].version;
|
||||
if version != 0 {
|
||||
body.insts[i].aux = version - 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val original := &this.program.functions[index];
|
||||
this.program.functions.append(function {
|
||||
name = string::from(original.name.span, nothing {}),
|
||||
decl = original.decl,
|
||||
instance = original.instance,
|
||||
node = original.node,
|
||||
outer = original.outer,
|
||||
table = original.table,
|
||||
file = original.file,
|
||||
offset = original.offset,
|
||||
signature = original.signature,
|
||||
body = move(&mut body),
|
||||
compiled = bytecode::new(),
|
||||
lowered = true,
|
||||
order = vector::<u32>::new(),
|
||||
closure = original.closure,
|
||||
callback = original.callback,
|
||||
inline = original.inline
|
||||
});
|
||||
val made := (this.program.functions.length - 1) as u32;
|
||||
this.program.functions[index].version = made + 1;
|
||||
ranks.append(rank);
|
||||
var found := vector::<string>::new();
|
||||
this.optimize_function(made, ranks, rank, &mut found);
|
||||
this.program.functions[made].optimized = true;
|
||||
for &each in found {
|
||||
this.problems.append(string::copy_of(&each));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,283 @@
|
||||
#! @file widths.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains what promotion does second, in an executable's code: a cell whose
|
||||
#! bytes are read or written whole as a vector of 4-byte lanes in one place and
|
||||
#! 4 bytes at a time in another, as a struct of `f32`s built by its fields and
|
||||
#! then read as lanes, lanes written and then read by field, or an array a
|
||||
#! vector's lanes were put in, holds words of two widths, which promotion leaves
|
||||
#! in memory; and where four writes of 4 bytes are read back by one of 16, the
|
||||
#! processor waits for them to reach the cache. So a cell whose `f32`s are
|
||||
#! written alone and read as a vector of 16 bytes has each read of the vector
|
||||
#! made reads of its four lanes, packed into one, a 4-byte integer it writes
|
||||
#! there being written as an `f32` of the same bits, where it is zero; and a
|
||||
#! cell written as vectors and read 4 bytes at a time has each of those reads
|
||||
#! made a read of the vector holding it, the lane taken from it. Either way the
|
||||
#! cell then holds words of one width, which promote, and what stays in memory
|
||||
#! is read as it was written. Vectors of 32 bytes are so only in code for
|
||||
#! processors with AVX, where they may be values.
|
||||
|
||||
module sakura::compiler;
|
||||
|
||||
|
||||
#! What reading a cell's vectors and lanes alike knows of a cell, its 4-byte
|
||||
#! lanes a bit each by their offset over 4.
|
||||
struct cell_widths {
|
||||
var refused: bool; #! Whether it is not rewritten: its address goes elsewhere, a word of it is of another width, or its vectors overlap.
|
||||
var starts: u64; #! The lanes where the vectors it reads or writes begin.
|
||||
var wides: u64; #! Of those, where vectors of 32 bytes begin, rather than of 16.
|
||||
var covered: u64; #! The lanes within its vectors.
|
||||
var stored: u64; #! The lanes within the vectors it writes.
|
||||
var vector_loads: bool; #! Whether it reads a vector.
|
||||
var vector_stores: bool; #! Whether it writes a vector.
|
||||
var single_loads: u64; #! The lanes it reads as `f32`s.
|
||||
var single_stores: u64; #! The lanes it writes as `f32`s.
|
||||
var word_loads: u64; #! The lanes it reads as 4-byte integers.
|
||||
var word_stores: u64; #! The lanes it writes as 4-byte integers.
|
||||
var other_words: u64; #! Of those, the lanes written anything but zero.
|
||||
var mode: u8; #! 1 where its vectors' reads become its lanes', 2 where its lanes' reads become its vectors', or 0.
|
||||
}
|
||||
|
||||
#! The bytes of a cell whose widths are followed, of 64 lanes.
|
||||
const widths_bytes: u64 = 256;
|
||||
|
||||
#! The lanes from one, as many as there are.
|
||||
#! @param first The first.
|
||||
#! @param count How many.
|
||||
#! @returns The lanes, a bit each.
|
||||
def widths_run(first: u64, count: u64): u64 {
|
||||
return 0 if first >= 64;
|
||||
val end := first + count > 64 ? 64 : first + count;
|
||||
val high := end >= 64 ? 0xFFFFFFFFFFFFFFFFu : (1u << end) - 1;
|
||||
return high & ~((1u << first) - 1);
|
||||
}
|
||||
|
||||
#! The type of lane a read or write of 4 bytes is, as `lanes` has it.
|
||||
#! @param how The access.
|
||||
#! @returns The type of lane, or `lane_none`.
|
||||
def widths_lane(how: access): u32 {
|
||||
return match how {
|
||||
access::f32 => lane_f32,
|
||||
access::u32 => lane_u32,
|
||||
access::i32 => lane_i32,
|
||||
else => lane_none
|
||||
};
|
||||
}
|
||||
|
||||
#! Notes a vector a cell reads or writes, refusing the cell where it overlaps
|
||||
#! another or is of 32 bytes where code may run without AVX.
|
||||
#! @param this What is known of the cell.
|
||||
#! @param offset Where the vector begins.
|
||||
#! @param bytes How many bytes it is: 16 or 32.
|
||||
#! @param store Whether it is written.
|
||||
#! @param wide Whether the code runs only where the processor has AVX.
|
||||
def cell_widths::note_vector(&mut this, offset: u64, bytes: u64, store: bool, wide: bool): void {
|
||||
if offset % 4 != 0 || (bytes == 32 && !wide) {
|
||||
this.refused = true;
|
||||
return;
|
||||
}
|
||||
val first := offset / 4;
|
||||
val lanes := widths_run(first, bytes / 4);
|
||||
val start := 1u << first;
|
||||
val long := bytes == 32;
|
||||
if (this.starts & start) != 0 {
|
||||
if ((this.wides & start) != 0) != long {
|
||||
this.refused = true;
|
||||
}
|
||||
} else if (this.covered & lanes) != 0 {
|
||||
this.refused = true;
|
||||
}
|
||||
this.starts = this.starts | start;
|
||||
if long {
|
||||
this.wides = this.wides | start;
|
||||
}
|
||||
this.covered = this.covered | lanes;
|
||||
if store {
|
||||
this.stored = this.stored | lanes;
|
||||
this.vector_stores = true;
|
||||
} else {
|
||||
this.vector_loads = true;
|
||||
}
|
||||
}
|
||||
|
||||
#! Reads a cell's vectors and their lanes alike, as the file says.
|
||||
#! @param this The code.
|
||||
#! @returns Whether anything changed.
|
||||
pub def code::unite_widths(&mut this): bool {
|
||||
return false if !this.vectors || this.cells.length == 0;
|
||||
val addresses := this.cell_table();
|
||||
return false if !addresses.any;
|
||||
var cells := vector::<cell_widths>::new(scratch());
|
||||
cells.resize(this.cells.length);
|
||||
for c in 0..this.cells.length {
|
||||
cells[c].refused = this.cells[c].size as u64 > widths_bytes;
|
||||
}
|
||||
|
||||
var reads := this.reader();
|
||||
var walk := this.walk_all();
|
||||
while walk.more() {
|
||||
val at := walk.next();
|
||||
val held: inst = this.insts[at];
|
||||
reads.start(at);
|
||||
while reads.more() {
|
||||
val cell := addresses.cell(reads.next());
|
||||
continue if cell == 0;
|
||||
val c := cell - 1;
|
||||
val a := reads.operand();
|
||||
val known := a == 0 && (held.kind == op::load || held.kind == op::store);
|
||||
if !known {
|
||||
# A copy moves its words, whichever they are; anything else keeps the
|
||||
# cell as it is.
|
||||
if held.kind != op::copy || a == no_operand {
|
||||
cells[c].refused = true;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
val how := held.aux as access;
|
||||
val store := held.kind == op::store;
|
||||
if how == access::v128 || how == access::v256 {
|
||||
cells[c].note_vector(held.imm, how == access::v256 ? 32 : 16, store, this.wide);
|
||||
continue;
|
||||
}
|
||||
val lane := widths_lane(how);
|
||||
if lane == lane_none || held.imm % 4 != 0 {
|
||||
cells[c].refused = true;
|
||||
continue;
|
||||
}
|
||||
val bit := 1u << (held.imm / 4);
|
||||
if lane == lane_f32 {
|
||||
if store {
|
||||
cells[c].single_stores = cells[c].single_stores | bit;
|
||||
} else {
|
||||
cells[c].single_loads = cells[c].single_loads | bit;
|
||||
}
|
||||
} else if store {
|
||||
cells[c].word_stores = cells[c].word_stores | bit;
|
||||
if !this.zero_bits(this.arg(at, 1)) {
|
||||
cells[c].other_words = cells[c].other_words | bit;
|
||||
}
|
||||
} else {
|
||||
cells[c].word_loads = cells[c].word_loads | bit;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Its vectors' reads become its lanes' where it writes lanes within vectors
|
||||
# of 16 bytes it only reads, each an `f32` or zero, and reads no integer
|
||||
# there; and its lanes' reads become its vectors' where it reads lanes within
|
||||
# vectors it writes, but writes no lane within any vector.
|
||||
var any := false;
|
||||
for c in 0..this.cells.length {
|
||||
val held: cell_widths = cells[c];
|
||||
continue if held.refused || held.starts == 0;
|
||||
val lanes_written := held.single_stores | held.word_stores;
|
||||
if !held.vector_stores && held.wides == 0 && (lanes_written & held.covered) != 0
|
||||
&& ((held.other_words | held.word_loads) & held.covered) == 0 {
|
||||
cells[c].mode = 1;
|
||||
} else if held.vector_stores && (lanes_written & held.covered) == 0
|
||||
&& ((held.single_loads | held.word_loads) & held.stored) != 0 {
|
||||
cells[c].mode = 2;
|
||||
}
|
||||
any = any || cells[c].mode != 0;
|
||||
}
|
||||
return false if !any;
|
||||
|
||||
var found := vector::<u32>::new(scratch());
|
||||
var walking := this.walk_all();
|
||||
while walking.more() {
|
||||
val at := walking.next();
|
||||
val held: inst = this.insts[at];
|
||||
continue if held.kind != op::load && held.kind != op::store;
|
||||
val cell := addresses.cell(this.arg(at, 0));
|
||||
continue if cell == 0 || cells[cell - 1].mode == 0;
|
||||
val how := held.aux as access;
|
||||
val vector := how == access::v128 || how == access::v256;
|
||||
val bit := !vector && held.imm % 4 == 0 && held.imm < widths_bytes ? 1u << (held.imm / 4) : 0u;
|
||||
if cells[cell - 1].mode == 1 {
|
||||
val zeroed := held.kind == op::store && widths_lane(how) != lane_f32 && (cells[cell - 1].covered & bit) != 0;
|
||||
if (held.kind == op::load && vector) || zeroed {
|
||||
found.append(at);
|
||||
}
|
||||
} else if held.kind == op::load && (cells[cell - 1].stored & bit) != 0 {
|
||||
found.append(at);
|
||||
}
|
||||
}
|
||||
|
||||
var done := rewrite::new();
|
||||
for &at in found.span {
|
||||
val held: inst = this.insts[at];
|
||||
if held.kind == op::store {
|
||||
this.write_zero_lane(at);
|
||||
} else if (held.aux as access) == access::v128 {
|
||||
this.read_lanes(at, &mut done);
|
||||
} else {
|
||||
val cell := addresses.cell(this.arg(at, 0)) - 1;
|
||||
this.read_lane(at, &cells[cell], &mut done);
|
||||
}
|
||||
}
|
||||
this.apply(&done);
|
||||
return true;
|
||||
}
|
||||
|
||||
#! Makes a read of a vector of four `f32`s reads of its lanes, packed.
|
||||
#! @param this The code.
|
||||
#! @param index The `load`.
|
||||
#! @param done What the pass changes.
|
||||
def code::read_lanes(&mut this, index: u32, done: &mut rewrite): void {
|
||||
val held: inst = this.insts[index];
|
||||
var after := index;
|
||||
var lanes: u32[4] = [0, 0, 0, 0];
|
||||
var gives: vt[1] = [vt::real];
|
||||
var from: u32[1] = [this.arg(index, 0)];
|
||||
for k in 0..4u {
|
||||
val load := this.detached(op::load, from[0:1], gives[0:1], access::f32 as u32, held.imm + 4 * k, held.at, held.site);
|
||||
this.link_after(load, after);
|
||||
after = load;
|
||||
lanes[k] = this.insts[load].result;
|
||||
}
|
||||
var vector: vt[1] = [vt::v128];
|
||||
val pack := this.detached(op::lanes, lanes[0:4], vector[0:1], lanes_pack, lane_f32 as u64 | (16u << 8), held.at, held.site);
|
||||
this.link_after(pack, after);
|
||||
done.replace(held.result, this.insts[pack].result);
|
||||
done.remove(index);
|
||||
}
|
||||
|
||||
#! Makes a write of a 4-byte integer of zero within a vector read whole a write
|
||||
#! of the `f32` of the same bits, as the vector's other lanes are written.
|
||||
#! @param this The code.
|
||||
#! @param index The `store`.
|
||||
def code::write_zero_lane(&mut this, index: u32): void {
|
||||
val zero := this.real_value(0);
|
||||
var args: u32[2] = [this.arg(index, 0), zero];
|
||||
this.become(index, op::store, args[0:2], access::f32 as u32, this.insts[index].imm);
|
||||
}
|
||||
|
||||
#! Makes a read of 4 bytes within a vector the cell writes a read of the
|
||||
#! vector, the lane taken from it.
|
||||
#! @param this The code.
|
||||
#! @param index The `load`.
|
||||
#! @param cell What is known of the cell.
|
||||
#! @param done What the pass changes.
|
||||
def code::read_lane(&mut this, index: u32, cell: &cell_widths, done: &mut rewrite): void {
|
||||
val held: inst = this.insts[index];
|
||||
# The vector holding it begins at the nearest start at or before it.
|
||||
var first := held.imm / 4;
|
||||
while (cell.starts & (1u << first)) == 0 {
|
||||
first--;
|
||||
}
|
||||
val bytes: u64 = (cell.wides & (1u << first)) != 0 ? 32 : 16;
|
||||
var vector: vt[1] = [bytes == 32 ? vt::v256 : vt::v128];
|
||||
var from: u32[1] = [this.arg(index, 0)];
|
||||
val load := this.detached(op::load, from[0:1], vector[0:1], (bytes == 32 ? access::v256 : access::v128) as u32, 4 * first, held.at, held.site);
|
||||
this.link_after(load, index);
|
||||
val lane := widths_lane(held.aux as access);
|
||||
var gives: vt[1] = [lane == lane_f32 ? vt::real : vt::word];
|
||||
var of: u32[1] = [this.insts[load].result];
|
||||
val k := (held.imm / 4 - first) as u32;
|
||||
val taken := this.detached(op::lanes, of[0:1], gives[0:1], lanes_lane | (k << 8), lane as u64 | (bytes << 8), held.at, held.site);
|
||||
this.link_after(taken, load);
|
||||
done.replace(held.result, this.insts[taken].result);
|
||||
done.remove(index);
|
||||
}
|
||||
@@ -0,0 +1,429 @@
|
||||
#! @file worth.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains what a call is worth inlining, worked out for each call rather
|
||||
#! than once for the function, as LLVM's inliner works out a call's cost and
|
||||
#! threshold. A function too large to inline at every call may be small at
|
||||
#! one: the copy is costed as it would be once optimized there, which is the
|
||||
#! function's code walked from its entry with what the call gives it.
|
||||
#!
|
||||
#! - What the call gives as constants folds: an operator of constants is the
|
||||
#! constant it gives, a branch or switch on one goes one way, and what only
|
||||
#! the ways not gone lead to is never copied, so costs nothing.
|
||||
#! - A parameter that is an address only read, written, copied, and cleared
|
||||
#! through, directly or at steps of constants from it, reaches a cell of the
|
||||
#! caller's where it is taken as memory, which the copy copies into a cell of
|
||||
#! its own, or where the call gives the address of a cell: promoting the cell
|
||||
#! then holds what it reaches as values, so those reads and writes cost
|
||||
#! nothing, as LLVM counts what its SROA takes out.
|
||||
#!
|
||||
#! The threshold the cost is held to grows where the call is worth more: by
|
||||
#! half within a loop, where the call runs again and again, and by a quarter
|
||||
#! where the copy is one block, whose code the caller's block then simply
|
||||
#! holds. Only functions within a few times the limit are walked, so a large
|
||||
#! function costs nothing to turn down.
|
||||
|
||||
module sakura::compiler;
|
||||
|
||||
|
||||
#! How many times the limit a function's cost may be for a call of it to be
|
||||
#! walked for what it is worth there.
|
||||
pub const worth_reach: u32 = 4;
|
||||
|
||||
#! The most vectors of lanes living through a loop where a call within it may
|
||||
#! be inlined past the limit. The backend gives values 11 vector registers, so
|
||||
#! past this, what a copy promotes out of memory into values would only be
|
||||
#! spilled to the stack again, a value at a time, which costs more than the
|
||||
#! call did.
|
||||
const vector_room: u32 = 6;
|
||||
|
||||
#! What a copy of a function at a call costs.
|
||||
struct call_worth {
|
||||
var cost: u32; #! Its instructions, counted as `inline_cost` counts them, but for those that fold or promote away.
|
||||
var blocks: u32; #! How many of its blocks the call reaches.
|
||||
}
|
||||
|
||||
#! Whether a call of a function too large to be inlined everywhere is worth
|
||||
#! inlining where it is, as the file says.
|
||||
#! @param this The compiler.
|
||||
#! @param caller The function calling.
|
||||
#! @param call The call.
|
||||
#! @param callee The function called.
|
||||
#! @param facts What inlining knows of the function called.
|
||||
#! @param limit The most a function inlined everywhere may cost.
|
||||
#! @param looped 0 where the call is in no loop, and otherwise one more than
|
||||
#! the vectors of lanes living through the innermost loop it is
|
||||
#! in, as `loop_pressure` counts them.
|
||||
#! @returns Whether it is.
|
||||
def lowerer::worth_inlining(&mut this, caller: u32, call: u32, callee: u32, facts: &inline_facts, limit: u32, looped: u32): bool {
|
||||
return false if looped > vector_room + 1;
|
||||
# Where nothing the call gives folds, the copy costs no less than what the
|
||||
# function does but for its reads and writes through what it takes, so
|
||||
# where that is past the most it may cost, it is not walked.
|
||||
val most := limit + limit / 4 + (looped != 0 ? limit / 2 : 0);
|
||||
return false if facts.cost > most + facts.reaches && !this.program.functions[caller].body.gives_constants(call);
|
||||
var threshold := limit;
|
||||
val worth := this.call_cost(caller, call, callee, facts);
|
||||
if worth.blocks == 1 {
|
||||
threshold += limit / 4;
|
||||
if looped != 0 {
|
||||
threshold += limit / 2;
|
||||
}
|
||||
}
|
||||
return worth.cost <= threshold;
|
||||
}
|
||||
|
||||
#! Whether a call gives any constant.
|
||||
#! @param this The code.
|
||||
#! @param call The call.
|
||||
#! @returns Whether it does.
|
||||
def code::gives_constants(&this, call: u32): bool {
|
||||
for i in 0..this.insts[call].argc {
|
||||
val made := this.defining(this.arg(call, i as u32));
|
||||
return true if made != 0 && (this.insts[made].kind == op::iconst || this.insts[made].kind == op::fconst);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#! Works out what a copy of a function at a call costs, as the file says.
|
||||
#! @param this The compiler.
|
||||
#! @param caller The function calling.
|
||||
#! @param call The call.
|
||||
#! @param callee The function called.
|
||||
#! @param facts What inlining knows of the function called.
|
||||
#! @returns What it costs: what the function costs everywhere, where the
|
||||
#! function's blocks were never ordered to be copied.
|
||||
def lowerer::call_cost(&mut this, caller: u32, call: u32, callee: u32, facts: &inline_facts): call_worth {
|
||||
var worth := call_worth { cost = facts.cost, blocks = 2 };
|
||||
var memory := vector::<bool>::new(scratch());
|
||||
var layouts := vector::<u32>::new(scratch());
|
||||
this.entry_values(this.program.functions[callee].signature, &mut memory, &mut layouts);
|
||||
val code := &this.program.functions[callee].body;
|
||||
val body := &this.program.functions[caller].body;
|
||||
val order := &this.program.functions[callee].order;
|
||||
val takes := code.blocks[0].params.length;
|
||||
return worth if order.length == 0 || memory.length != takes || takes > body.insts[call].argc as u64;
|
||||
|
||||
# What each value is known to be: a constant's bits, or an address whose
|
||||
# reads and writes promote.
|
||||
var walking := worth_walk {
|
||||
known = vector::<u64>::new(scratch()),
|
||||
fixed = vector::<bool>::new(scratch()),
|
||||
free = vector::<bool>::new(scratch()),
|
||||
taken = vector::<bool>::new(scratch())
|
||||
};
|
||||
walking.known.resize(code.origins.length);
|
||||
walking.fixed.resize(code.origins.length);
|
||||
walking.free.resize(code.origins.length);
|
||||
walking.taken.resize(code.edges.length);
|
||||
for i in 0..takes {
|
||||
val param: u32 = code.blocks[0].params[i];
|
||||
val arg := body.arg(call, i as u32);
|
||||
val contained := i < 64 && (facts.contained & (1u << i)) != 0;
|
||||
if memory[i] {
|
||||
walking.free[param] = contained;
|
||||
continue;
|
||||
}
|
||||
var bits: u64 = 0;
|
||||
if body.word_constant(arg, &mut bits) || body.real_constant(arg, &mut bits) {
|
||||
walking.known[param] = bits;
|
||||
walking.fixed[param] = true;
|
||||
} else if contained && body.cell_of(arg) != no_cell {
|
||||
walking.free[param] = true;
|
||||
}
|
||||
}
|
||||
|
||||
# The blocks in the order they are copied in, which is the order they run
|
||||
# in, each walked where a way gone reaches it.
|
||||
var place := vector::<u32>::new(scratch());
|
||||
place.resize(code.blocks.length, 0xFFFFFFFF);
|
||||
for k in 0..order.length {
|
||||
place[order[k]] = k as u32;
|
||||
}
|
||||
var live := vector::<bool>::new(scratch());
|
||||
live.resize(code.blocks.length);
|
||||
live[0] = true;
|
||||
worth.cost = 0;
|
||||
worth.blocks = 0;
|
||||
for &b in order.span {
|
||||
continue if !live[b];
|
||||
worth.blocks++;
|
||||
if b != 0 {
|
||||
code.join_known(b, &place, &live, &mut walking);
|
||||
}
|
||||
var walk := code.walk(b);
|
||||
while walk.more() {
|
||||
worth.cost += code.cost_at(walk.next(), &mut walking);
|
||||
}
|
||||
var outs := code.edges_of(code.blocks[b].last);
|
||||
while outs.more() {
|
||||
val edge := outs.next();
|
||||
if walking.taken[edge] {
|
||||
live[code.edges[edge].target] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return worth;
|
||||
}
|
||||
|
||||
#! What walking a function's code for what a call is worth knows.
|
||||
struct worth_walk {
|
||||
var known: vector<u64>; #! The bits of each value known to be a constant.
|
||||
var fixed: vector<bool>; #! Whether each value is known to be a constant.
|
||||
var free: vector<bool>; #! Whether each value is an address whose reads and writes promote.
|
||||
var taken: vector<bool>; #! Whether each edge is gone along.
|
||||
}
|
||||
|
||||
#! Learns which parameters of a block are known constants: those every edge
|
||||
#! gone into it gives the same constant, where no edge comes back into it from
|
||||
#! a block not walked yet, as round a loop.
|
||||
#! @param this The code.
|
||||
#! @param block The block.
|
||||
#! @param place Each block's place in the order they are walked in.
|
||||
#! @param live Whether each block is reached.
|
||||
#! @param walking What the walk knows.
|
||||
def code::join_known(&this, block: u32, place: &vector<u32>, live: &vector<bool>, walking: &mut worth_walk): void {
|
||||
val held := &this.blocks[block];
|
||||
for p in 0..held.params.length {
|
||||
var bits: u64 = 0;
|
||||
var any := false;
|
||||
var same := true;
|
||||
for &edge in held.preds {
|
||||
val from := this.edge_source(edge);
|
||||
if place[from] >= place[block] {
|
||||
same = false;
|
||||
break;
|
||||
}
|
||||
continue if !live[from] || !walking.taken[edge];
|
||||
val arg: u32 = this.edges[edge].args[p];
|
||||
if !walking.fixed[arg] || (any && walking.known[arg] != bits) {
|
||||
same = false;
|
||||
break;
|
||||
}
|
||||
bits = walking.known[arg];
|
||||
any = true;
|
||||
}
|
||||
if same && any {
|
||||
val param: u32 = held.params[p];
|
||||
walking.known[param] = bits;
|
||||
walking.fixed[param] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#! What an instruction of a copy costs, as the file says, noting what it gives
|
||||
#! where that is known, and which ways out of its block it goes where it ends
|
||||
#! one.
|
||||
#! @param this The code.
|
||||
#! @param index The instruction.
|
||||
#! @param walking What the walk knows.
|
||||
#! @returns What it costs.
|
||||
def code::cost_at(&this, index: u32, walking: &mut worth_walk): u32 {
|
||||
val held: inst = this.insts[index];
|
||||
val kind := held.kind;
|
||||
match kind {
|
||||
op::jump => {
|
||||
walking.taken[held.aux] = true;
|
||||
return 0;
|
||||
}
|
||||
op::branch => {
|
||||
val cond := this.arg(index, 0);
|
||||
if walking.fixed[cond] {
|
||||
walking.taken[walking.known[cond] != 0 ? held.aux : held.aux + 1] = true;
|
||||
return 0;
|
||||
}
|
||||
walking.taken[held.aux] = true;
|
||||
walking.taken[held.aux + 1] = true;
|
||||
return 1;
|
||||
}
|
||||
op::switch => {
|
||||
val word := this.arg(index, 0);
|
||||
val count := held.imm as u32;
|
||||
if walking.fixed[word] {
|
||||
# The last way out is the one for every other word.
|
||||
var chosen := held.aux + count - 1;
|
||||
for e in held.aux..(held.aux + count - 1) {
|
||||
if this.edges[e].case == walking.known[word] {
|
||||
chosen = e;
|
||||
break;
|
||||
}
|
||||
}
|
||||
walking.taken[chosen] = true;
|
||||
return 0;
|
||||
}
|
||||
for e in held.aux..(held.aux + count) {
|
||||
walking.taken[e] = true;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
op::iconst, op::fconst => {
|
||||
walking.known[held.result] = held.imm;
|
||||
walking.fixed[held.result] = true;
|
||||
return 0;
|
||||
}
|
||||
else => {}
|
||||
}
|
||||
|
||||
# What only works out a constant from constants folds.
|
||||
if held.count == 1 && held.argc >= 1 {
|
||||
var all := true;
|
||||
for i in 0..held.argc {
|
||||
all = all && walking.fixed[this.arg(index, i as u32)];
|
||||
}
|
||||
if all {
|
||||
var made: u64 = 0;
|
||||
if fold_known(kind, &held, this, walking, &mut made) {
|
||||
walking.known[held.result] = made;
|
||||
walking.fixed[held.result] = true;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
if kind == op::select && walking.fixed[this.arg(index, 0)] {
|
||||
val chosen := this.arg(index, walking.known[this.arg(index, 0)] != 0 ? 1 : 2);
|
||||
if walking.fixed[chosen] {
|
||||
walking.known[held.result] = walking.known[chosen];
|
||||
walking.fixed[held.result] = true;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
# What reaches a cell that promotes costs nothing, nor do the steps to it.
|
||||
match kind {
|
||||
op::add => {
|
||||
val a := this.arg(index, 0);
|
||||
val b := this.arg(index, 1);
|
||||
if (walking.free[a] && walking.fixed[b]) || (walking.free[b] && walking.fixed[a]) {
|
||||
walking.free[held.result] = true;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
op::load, op::store, op::clear => {
|
||||
return 0 if walking.free[this.arg(index, 0)];
|
||||
}
|
||||
op::copy => {
|
||||
return 0 if walking.free[this.arg(index, 0)] && walking.free[this.arg(index, 1)];
|
||||
}
|
||||
else => {}
|
||||
}
|
||||
return this.inline_cost(index);
|
||||
}
|
||||
|
||||
#! What an operator of constants gives, as the machine works it out.
|
||||
#! @param kind The operator.
|
||||
#! @param held The instruction.
|
||||
#! @param body The code.
|
||||
#! @param walking What the walk knows, which knows each operand.
|
||||
#! @param made Where what it gives is written.
|
||||
#! @returns Whether it gives anything: not where it divides by zero, or is
|
||||
#! not an operator worked out so.
|
||||
def fold_known(kind: op, held: &inst, body: &code, walking: &worth_walk, made: &mut u64): bool {
|
||||
val number := kind as u32;
|
||||
val x: u64 = walking.known[body.operands[held.args]];
|
||||
var y: u64 = 0;
|
||||
if held.argc >= 2 {
|
||||
y = walking.known[body.operands[held.args as u64 + 1]];
|
||||
}
|
||||
return fold_word(kind, x, y, made) if number >= op::add as u32 && number <= op::leus as u32;
|
||||
return fold_real(kind, x, y, made) if number >= op::fadd as u32 && number <= op::fle as u32;
|
||||
return fold_unary(kind, x, made) if number >= op::neg as u32 && number <= op::bswap as u32;
|
||||
if kind == op::wrap {
|
||||
made = wrap_word(x, held.aux, held.imm != 0);
|
||||
return true;
|
||||
}
|
||||
if kind == op::select {
|
||||
made = x != 0 ? y : walking.known[body.operands[held.args as u64 + 2]];
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#! Which of code's first 64 parameters are addresses read, written, copied,
|
||||
#! and cleared through alone, directly or at steps of constants from them:
|
||||
#! nothing else learns them, nor what they reach, so where what they reach is
|
||||
#! a cell of the caller's, a copy of the code leaves the cell to promote.
|
||||
#! @param this The code.
|
||||
#! @param reaches Where how many reads, writes, copies, and clears are
|
||||
#! through those parameters is written.
|
||||
#! @returns The parameters, one bit each.
|
||||
pub def code::contained_params(&this, reaches: &mut u32): u64 {
|
||||
reaches = 0;
|
||||
val params := &this.blocks[0].params;
|
||||
val count := params.length < 64 ? params.length : 64;
|
||||
# One more than the parameter each value steps from, or 0.
|
||||
var root := vector::<u32>::new(scratch());
|
||||
root.resize(this.origins.length);
|
||||
var any := false;
|
||||
for p in 0..count {
|
||||
val param: u32 = params[p];
|
||||
if this.type_of(param) == vt::word {
|
||||
root[param] = p as u32 + 1;
|
||||
any = true;
|
||||
}
|
||||
}
|
||||
return 0 if !any;
|
||||
|
||||
# The steps of constants from each, until no more are found.
|
||||
var grew := true;
|
||||
while grew {
|
||||
grew = false;
|
||||
var walk := this.walk_all();
|
||||
while walk.more() {
|
||||
val at := walk.next();
|
||||
val held: inst = this.insts[at];
|
||||
continue if held.kind != op::add || held.argc != 2 || root[held.result] != 0;
|
||||
var bits: u64 = 0;
|
||||
val a := this.arg(at, 0);
|
||||
val b := this.arg(at, 1);
|
||||
if root[a] != 0 && this.word_constant(b, &mut bits) {
|
||||
root[held.result] = root[a];
|
||||
grew = true;
|
||||
} else if root[b] != 0 && this.word_constant(a, &mut bits) {
|
||||
root[held.result] = root[b];
|
||||
grew = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Any other read of one, as a call's argument, a value stored, or what an
|
||||
# edge gives, lets it out.
|
||||
var out: u64 = 0;
|
||||
var accesses: u32[64];
|
||||
var walk := this.walk_all();
|
||||
var reads := this.reader();
|
||||
while walk.more() {
|
||||
val at := walk.next();
|
||||
val held: inst = this.insts[at];
|
||||
val stepping := held.kind == op::add && root[held.result] != 0;
|
||||
reads.start(at);
|
||||
while reads.more() {
|
||||
val from: u32 = root[reads.next()];
|
||||
continue if from == 0;
|
||||
val i := reads.operand();
|
||||
var direct := false;
|
||||
if i != no_operand {
|
||||
match held.kind {
|
||||
op::load, op::store, op::clear => direct = i == 0;
|
||||
op::copy => direct = i < 2;
|
||||
op::add => direct = stepping;
|
||||
else => {}
|
||||
}
|
||||
}
|
||||
if !direct {
|
||||
out = out | (1u << (from - 1) as u64);
|
||||
} else if held.kind != op::add {
|
||||
accesses[from - 1] = accesses[from - 1] + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
var all: u64 = 0;
|
||||
for p in 0..count {
|
||||
if root[params[p]] != 0 && (out & (1u << p as u64)) == 0 {
|
||||
all = all | (1u << p as u64);
|
||||
reaches = reaches + accesses[p];
|
||||
}
|
||||
}
|
||||
return all;
|
||||
}
|
||||
Reference in new issue
Block a user