Files
sakurac/native/registers.sakura
T

1686 lines
60 KiB
Plaintext
Raw Normal View History

2026-10-26 10:14:11 -05:00
#! @file registers.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the register allocator, which says where each value of a
#! function's code is held from where it is made to where it is last read: a
#! register, its slot of the frame, or nowhere, for a constant or an address
#! made again wherever it is read.
#!
#! The blocks are laid out in reverse postorder, and every value is live over
#! ranges of that order, as Wimmer and Franz's "Linear Scan Register Allocation
#! on SSA Form" builds them, with holes where it is not live. A linear scan
#! gives each value a register for as long as one is free, and splits it where
#! none is, or where a call is made: a call keeps only `rbx` and `r12` to `r15`,
#! which every function saves where it uses them, as C's conventions keep them
#! too, so a value live across a call is in one of those or in its slot. A
#! value with a slot is written to it once, where it is made, so moving it
#! there later costs nothing, and only reading it back into a register is a
#! move.
#!
#! 64 bytes of lanes are never in a register, but in their slot, as only code
#! knowing the processor has AVX-512 may hold them in one.
#!
#! Where no register is free, a value takes one from another read no more
#! often than it, counting its edges' arguments, so what a loop reads in every
#! arm keeps its register rather than making way for what one arm reads once.
#! Between values living in no loop, where the order is the order the code runs
#! in, the one read last makes way, whichever it is, so a value read at once is
#! never written to its slot only to be read back.
#! A value living across a call takes a register a call keeps at once where it
#! can, rather than another until the call, and one first read only after its
#! register would be taken back goes to its slot, so a loop's head holds each
#! value where its arms do, and its edges move nothing.
#!
#! Each instruction has four positions: its gap, where moves go before it, the
#! read of its operands, the clobber of a call, and the write of its results.
#! Each block begins with a place for its parameters and ends with one for the
#! arguments its edges give.
import std::containers;
import sakura::compiler;
import binary::amd64;
module sakura::native;
#! What stands for no position, interval, or slot.
pub const never: u32 = 0xFFFFFFFF;
#! What stands for no register.
pub const no_reg: u8 = 0xFF;
#! Where a value is held.
pub enum place : u8 {
none, #! Nowhere: it is never read.
gpr, #! A general register.
xmm, #! An SSE register.
slot, #! The slot of the frame of the value `value`.
remade, #! Made again where it is read, as the value `value` is.
out, #! The word `value` of the arguments a call passes on the stack.
incoming, #! The word `value` of the arguments the function was called with, where it begins.
}
#! Where a value is held at some position.
pub struct loc {
var kind: place; #! What holds it.
var number: u8; #! The register, of a register.
var value: u32; #! The value whose slot it is, which is made again, or the word of the arguments.
}
#! Whether two places are the same.
#! @param this One place.
#! @param other The other.
#! @returns Whether they are.
pub def loc::same(&this, other: &loc): bool {
return this.kind == other.kind && this.number == other.number && this.value == other.value;
}
#! A general register as a place.
#! @param r The register.
#! @returns The place.
pub def in_gpr(r: reg): loc {
return loc { kind = place::gpr, number = r as u8 };
}
#! An SSE register as a place.
#! @param x The register's number.
#! @returns The place.
pub def in_xmm(x: u8): loc {
return loc { kind = place::xmm, number = x };
}
#! A value's slot as a place.
#! @param value The value.
#! @returns The place.
pub def in_slot(value: u32): loc {
return loc { kind = place::slot, value = value };
}
#! A move of a value from one place to another.
pub struct transfer {
var from: loc; #! Where it is.
var to: loc; #! Where it goes.
var bytes: u8; #! How many bytes it moves: 16, 32, or 64 for a vector, and 8, or 0 meaning 8, otherwise.
}
#! The general registers values are given, in the order they are chosen in:
#! those a call clobbers, in the order arguments are passed in, then those it
#! keeps.
pub const gprs: reg[11] = [
reg::rdi, reg::rsi, reg::r8, reg::r9, reg::r10, reg::r11, reg::rbx, reg::r12, reg::r13, reg::r14, reg::r15
];
#! How many general registers values are given.
pub const gpr_count: u64 = 11;
#! The general registers a call keeps, as both of C's conventions keep them: a
#! function saves each it uses as it begins, and gives it back as it returns.
pub const kept_gprs: reg[5] = [reg::rbx, reg::r12, reg::r13, reg::r14, reg::r15];
#! The SSE registers values are given: all but `xmm0` to `xmm4`, which code
#! computes in, the operations on lanes all of them. A real is held in a
#! register's low 8 bytes, and a vector in its 16 or 32. A call keeps none.
pub const xmms: u8[11] = [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
#! How many arguments a call passes in registers, the first of `gprs`, none of
#! them a register a call keeps; the rest it passes on the stack.
pub const arg_count: u64 = 6;
#! Whether a call keeps a general register.
#! @param r The register's number.
#! @returns Whether it does.
pub def keeps(r: u8): bool {
return r == reg::rbx as u8 || (r >= reg::r12 as u8 && r <= reg::r15 as u8);
}
#! Where moves before a position's instruction go: its gap.
#! @param at The position.
#! @returns The gap.
pub def gap_of(at: u32): u32 {
return at & 0xFFFFFFFC;
}
#! A part of a value's life, held in one place.
struct interval {
var value: u32; #! The value.
var first: u32; #! Its first range, in `froms` and `tos`.
var last: u32; #! Its last range.
var start: u32; #! Where it begins, within its first range.
var end: u32; #! Where it ends, within its last range.
var use_lo: u32; #! Its first read, among its value's in `reads`.
var use_hi: u32; #! One past its last read.
var reg: u8; #! Its register, or `no_reg`.
var stacked: bool; #! Whether it is in its value's slot.
var next: u32; #! The part of the same value after it, or `never`.
var cursor: u32; #! The range the scan last found it in.
}
#! Where each value of a function's code is held, and the moves between.
pub struct placing {
# The order of the code.
var order: vector<u32>; #! The blocks, in the order they are laid out.
var number: vector<u32>; #! Each block's place in that order.
var start_slots: vector<u32>; #! The place of each block's parameters.
var end_slots: vector<u32>; #! The place of each block's edges.
var slots: vector<u32>; #! The place of each instruction.
var folded: vector<bool>; #! Whether each instruction is folded into what reads it, giving no value: a comparison into a branch, a shift into the sum scaling it, or a sum into the loads and stores it addresses.
var remade: vector<bool>; #! Whether each value is made again where it is read.
var memory_params: vector<bool>; #! Whether each value is a parameter held in memory, the address of the function's own copy.
var clobbers: vector<u32>; #! The positions of the calls, where only the registers a call keeps keep what they held.
var loops: vector<u64>; #! The places each loop spans, from its head to the edge going back to it, by `pair_key(first, last)`.
var looped: vector<bool>; #! Whether each value lives within a loop: whether any range of its meets a loop's places.
var places: u32; #! How many places there are.
# Reads, by value.
var reads: groups<u32>; #! The positions of each value's reads, in order.
var counts: vector<u32>; #! How many times each value is read, its edges' arguments included.
var def_at: vector<u32>; #! Where each value is made.
var real: vector<bool>; #! Whether each value is in the SSE registers: a real or a vector.
var bytes: vector<u8>; #! How many bytes each value is: 8, or 16, 32, or 64 for a vector.
# Life.
var froms: vector<u32>; #! Where each range begins.
var tos: vector<u32>; #! Where each range ends, past its last position.
var intervals: vector<interval>;
var heads: vector<u32>; #! The first interval of each value, or `never`.
var liveins: groups<u32>; #! The values live where each block begins, but its parameters, by block.
# What was chosen.
var slot_of: vector<u32>; #! The slot of each value that has one, or `never`.
var slot_count: u32; #! How many slots the values take.
var child_first: vector<u32>; #! Where each value's parts begin in `child_starts` and the rest, by value, and one more.
var child_starts: vector<u32>;
var child_ends: vector<u32>;
var child_locs: vector<loc>;
var gap_moves: groups<transfer>; #! The moves in the gap of each place, by place, the stores of values made just before it first.
var edge_first: vector<u32>; #! Where each edge's moves begin in `edge_moves`, by edge, and one more.
var edge_moves: vector<transfer>; #! The moves along each edge, made all at once.
# Hints, which save moves where they are taken.
var hint: vector<u8>; #! The register each value is passed in, as an argument of a call or of the function, or `no_reg`.
var partners: groups<u32>; #! The values each would best share a register with, by value: the parameters it is given as and the arguments given as it, and the operand it is made from in place.
}
#! Says where each value of a function's code is held.
#! @param body The code.
#! @param memory Whether each parameter of the entry is held in memory, the
#! address of the function's own copy of it.
#! @returns Where.
pub def place_values(body: &code, memory: span<bool>): placing {
var made := placing {
order = vector::<u32>::new(scratch()),
number = vector::<u32>::new(scratch()),
start_slots = vector::<u32>::new(scratch()),
end_slots = vector::<u32>::new(scratch()),
slots = vector::<u32>::new(scratch()),
folded = vector::<bool>::new(scratch()),
remade = vector::<bool>::new(scratch()),
memory_params = vector::<bool>::new(scratch()),
clobbers = vector::<u32>::new(scratch()),
loops = vector::<u64>::new(scratch()),
looped = vector::<bool>::new(scratch()),
reads = groups::<u32>::new(body.origins.length, scratch()),
counts = vector::<u32>::new(scratch()),
def_at = vector::<u32>::new(scratch()),
real = vector::<bool>::new(scratch()),
bytes = vector::<u8>::new(scratch()),
froms = vector::<u32>::new(scratch()),
tos = vector::<u32>::new(scratch()),
intervals = vector::<interval>::new(scratch()),
heads = vector::<u32>::new(scratch()),
liveins = groups::<u32>::new(body.blocks.length, scratch()),
slot_of = vector::<u32>::new(scratch()),
child_first = vector::<u32>::new(scratch()),
child_starts = vector::<u32>::new(scratch()),
child_ends = vector::<u32>::new(scratch()),
child_locs = vector::<loc>::new(scratch()),
gap_moves = groups::<transfer>::new(0, scratch()),
edge_first = vector::<u32>::new(scratch()),
edge_moves = vector::<transfer>::new(scratch()),
hint = vector::<u8>::new(scratch()),
partners = groups::<u32>::new(body.origins.length, scratch())
};
made.lay_out(body, memory);
made.count_reads(body);
made.read_all(body);
made.live(body);
made.find_looped();
made.find_hints(body);
made.scan(false);
made.scan(true);
made.choose(body);
made.resolve(body);
return made;
}
# Laying out.
#! Whether an operation calls, so only the registers a call keeps keep what
#! they held across it. A safe point calls only where a signal came, and keeps
#! every register itself.
#! @param kind The operation.
#! @returns Whether it does.
pub def calls(kind: op): bool {
return match kind {
op::call, op::call_trait, op::call_value, op::call_c, op::allocate, op::release, op::resize,
op::equal, op::syscall, op::catch, op::exit, op::abort, op::fault, op::format, op::format_text,
op::format_value, op::format_bytes, op::format_owned, op::start, op::clone => true,
else => false
};
}
#! Whether an operation's value is cheap enough to make again wherever it is
#! read rather than hold: a constant, or an address the code knows.
#! @param kind The operation.
#! @returns Whether it is.
pub def remakes(kind: op): bool {
return match kind {
op::iconst, op::fconst, op::cell, op::global, op::datum, op::image, op::callback, op::frame, op::c_address => true,
else => false
};
}
#! Whether an operation compares two values in a way one condition of the
#! flags tests, as a branch or a check may test for itself.
#! @param kind The operation.
#! @returns Whether it does.
pub def compares(kind: op): bool {
return match kind {
op::eq, op::ne, op::lts, op::ltu, op::les, op::leu, op::flt, op::fle => true,
else => false
};
}
#! Whether an instruction asks whether a real is a number, as `x != x` or
#! `x == x` do, which `ucomisd` of it with itself answers in the parity flag
#! alone, as a branch or check may test.
#! @param body The code.
#! @param index The instruction.
#! @returns Whether it does.
pub def nan_test(body: &code, index: u32): bool {
val kind := body.insts[index].kind;
return (kind == op::feq || kind == op::fne) && body.arg(index, 0) == body.arg(index, 1);
}
#! Lays the blocks every way on from which stops the program after the rest,
#! each in the order it had, as where a contract fails makes its report: so
#! the code that runs is together, and the branch to such a block is taken
#! only where it stops. Each block is cold where it ends unreached past a
#! fault, a trap, or a call that never returns, or where every block it goes to
#! is, which a reverse walk of the order finds, as a loop never is.
#! @param this The placing.
#! @param body The code.
def placing::cold_last(&mut this, body: &code): void {
var cold := vector::<bool>::new(scratch());
cold.resize(body.blocks.length);
var any := false;
var k := this.order.length;
while k > 0 {
k--;
val block: u32 = this.order[k];
val last: u32 = body.blocks[block].last;
continue if last == 0;
if body.edge_count(last) == 0 {
val kind := body.insts[last].kind;
cold[block] = kind == op::unreachable || kind == op::trap;
} else {
var all := true;
var edges := body.edges_of(last);
while edges.more() {
if !cold[body.edges[edges.next()].target] {
all = false;
break;
}
}
cold[block] = all;
}
any = any || cold[block];
}
return if !any || cold[this.order[0]];
var laid := vector::<u32>::new(scratch());
for &each in this.order {
if !cold[each] {
laid.append(each);
}
}
for &each in this.order {
if cold[each] {
laid.append(each);
}
}
for i in 0..laid.length {
this.order[i] = laid[i];
this.number[laid[i]] = i as u32;
}
}
#! Lays the blocks out, gives each instruction its place, and finds what is
#! made again.
#! @param this The placing.
#! @param body The code.
#! @param memory Whether each parameter of the entry is held in memory.
def placing::lay_out(&mut this, body: &code, memory: span<bool>): void {
body.copy_order(&mut this.order, &mut this.number);
this.cold_last(body);
this.start_slots.resize(body.blocks.length);
this.end_slots.resize(body.blocks.length);
this.slots.resize(body.insts.length);
this.folded.resize(body.insts.length);
val values := body.origins.length;
this.remade.resize(values);
this.memory_params.resize(values);
this.real.resize(values);
this.bytes.resize(values);
this.def_at.resize(values);
for v in 0..values {
val kind := body.origins[v].kind;
this.real[v] = kind == vt::real || kind.lanes();
this.bytes[v] = kind == vt::v512 ? 64 : kind == vt::v256 ? 32 : kind == vt::v128 ? 16 : 8;
}
var slot: u32 = 0;
for k in 0..this.order.length {
val block: u32 = this.order[k];
this.start_slots[block] = slot;
for &param in body.blocks[block].params {
this.def_at[param] = 4 * slot + 3;
}
slot++;
var walk := body.walk(block);
while walk.more() {
val at := walk.next();
val held: inst = body.insts[at];
this.slots[at] = slot;
for r in 0..held.count {
this.def_at[held.result as u64 + r as u64] = 4 * slot + 3;
this.remade[held.result as u64 + r as u64] = remakes(held.kind);
}
if calls(held.kind) {
this.clobbers.append(4 * slot + 2);
}
slot++;
}
this.end_slots[block] = slot;
slot++;
}
this.places = slot;
# An edge to a block laid out at or before its own goes back round a loop.
for k in 0..this.order.length {
val block: u32 = this.order[k];
val last: u32 = body.blocks[block].last;
continue if last == 0;
for e in 0..body.edge_count(last) {
val target: u32 = body.edges[body.insts[last].aux + e as u32].target;
if this.number[target] <= k as u32 {
this.loops.append(pair_key(this.start_slots[target], this.end_slots[block]));
}
}
}
# A parameter held in memory is the address of the function's own copy,
# which is made again where it is read.
val entry := &body.blocks[0];
for i in 0..entry.params.length {
if i < memory.length && memory[i] {
val value: u32 = entry.params[i];
this.remade[value] = true;
this.memory_params[value] = true;
}
}
}
#! Counts the reads of each value, then folds each comparison only the branch
#! or check after it reads into it, and the sums and shifts addresses are made
#! of into what reads them.
#! @param this The placing.
#! @param body The code.
def placing::count_reads(&mut this, body: &code): void {
this.counts.resize(body.origins.length);
var reads := body.reader();
for k in 0..this.order.length {
var walk := body.walk(this.order[k]);
while walk.more() {
reads.start(walk.next());
while reads.more() {
val value := reads.next();
this.counts[value] = this.counts[value] + 1;
}
}
}
for k in 0..this.order.length {
var walk := body.walk(this.order[k]);
while walk.more() {
val at := walk.next();
this.folded[at] = this.foldable(body, at);
}
}
this.fold_addresses(body);
}
#! Folds what addresses are made of as x86 addresses memory, `[base + index *
#! scale + displacement]`: each sum read only as the address of one load or
#! store into it, as its base and index, and each shift left by one to three
#! read only by a sum into the sum, as its index's scale, which the sum
#! otherwise makes with `lea`. What is folded gives no value, and what reads it
#! reads what it is made of instead. A sum several accesses read stays, as
#! holding its base and index to each would hold two values for one.
#! @param this The placing.
#! @param body The code.
def placing::fold_addresses(&mut this, body: &code): void {
# How many times each value is read as the address of a load or store whose
# reach needs no test of it for null, which a fault makes.
var addressed := vector::<u32>::new(scratch());
addressed.resize(body.origins.length);
for k in 0..this.order.length {
var walk := body.walk(this.order[k]);
while walk.more() {
val held: inst = body.insts[walk.next()];
continue if held.kind != op::load && held.kind != op::store;
continue if held.imm + (held.aux as access).width() > null_page;
val address := body.operands[held.args as u64];
addressed[address] = addressed[address] + 1;
}
}
for k in 0..this.order.length {
var walk := body.walk(this.order[k]);
while walk.more() {
val at := walk.next();
val held: inst = body.insts[at];
continue if held.kind != op::add || this.real[held.result];
for a in 0..2u {
val shift := this.scaling(body, body.arg(at, a as u32));
if shift != 0 {
this.folded[shift] = true;
break;
}
}
val result := held.result;
if this.counts[result] == 1 && addressed[result] == 1 && this.addresses(body, at) {
this.folded[at] = true;
}
}
}
}
#! The shift a value is, of a word left by one to three, read only once, as a
#! sum reads it to scale its index, or 0.
#! @param this The placing.
#! @param body The code.
#! @param value The value.
#! @returns The shift.
def placing::scaling(&this, body: &code, value: u32): u32 {
val made := body.defining(value);
return 0 if made == 0 || this.counts[value] != 1;
val held: inst = body.insts[made];
return 0 if held.kind != op::shl;
val by := body.arg(made, 1);
val count := body.defining(by);
return 0 if count == 0 || body.insts[count].kind != op::iconst;
val bits: u64 = body.insts[count].imm;
val shifted := body.arg(made, 0);
return 0 if bits < 1 || bits > 3 || this.remade[shifted] || this.real[shifted];
return made;
}
#! Whether a sum may be the address memory is reached at: neither operand a
#! constant, which is a displacement instead, and one of them held, rather
#! than made again, as its index.
#! @param this The placing.
#! @param body The code.
#! @param index The sum.
#! @returns Whether it may.
def placing::addresses(&this, body: &code, index: u32): bool {
var held_index := false;
for a in 0..2u {
val operand := body.arg(index, a as u32);
val made := body.defining(operand);
if made != 0 {
val kind: op = body.insts[made].kind;
return false if kind == op::iconst || kind == op::fconst;
}
held_index = held_index || !this.remade[operand];
}
return held_index;
}
#! Whether an instruction is a comparison read only by the branch, check, or
#! choice after it, which compares for itself, so the comparison gives no
#! value: a choice of words by any, and of reals by one of reals, which it
#! makes with no branch.
#! @param this The placing.
#! @param body The code.
#! @param index The instruction.
#! @returns Whether it is.
def placing::foldable(&this, body: &code, index: u32): bool {
val held: inst = body.insts[index];
return false if !(compares(held.kind) || nan_test(body, index)) || held.next == 0 || this.counts[held.result] != 1;
val next: inst = body.insts[held.next];
val reals := held.kind == op::flt || held.kind == op::fle;
val tests := next.kind == op::branch || next.kind == op::check || (next.kind == op::select && (!this.real[next.result] || reals));
return tests && next.argc != 0 && body.operands[next.args as u64] == held.result;
}
#! The instruction whose operands an instruction reads where it is: its own,
#! or for a branch, check, or select a comparison is folded into, the
#! comparison, though a select reads what it chooses between too.
#! @param this The placing.
#! @param body The code.
#! @param index The instruction.
#! @returns The instruction whose operands are read.
pub def placing::reader_of(&this, body: &code, index: u32): u32 {
val held: inst = body.insts[index];
if (held.kind == op::branch || held.kind == op::check || held.kind == op::select) && held.argc != 0 {
val tested := body.operands[held.args as u64];
val origin: origin = body.origins[tested];
if !origin.param && this.folded[origin.from] {
return origin.from;
}
}
return index;
}
#! The values an instruction reads where it is: its operands, or for one a
#! comparison is folded into, the comparison's, and for a select so folded what
#! it chooses between too, where each operand folded into it is what it is made
#! of.
#! @param this The placing.
#! @param body The code.
#! @param index The instruction.
#! @param into Where the values are written, emptied first.
pub def placing::read_by(&this, body: &code, index: u32, into: &mut vector<u32>): void {
into.clear();
val reader := this.reader_of(body, index);
val read: inst = body.insts[reader];
for i in 0..read.argc {
this.read_through(body, body.operands[read.args as u64 + i as u64], into);
}
if reader != index && body.insts[index].kind == op::select {
this.read_through(body, body.arg(index, 1), into);
this.read_through(body, body.arg(index, 2), into);
}
}
#! Adds what reading a value reads: the value, or what it is made of where it
#! is folded into what reads it, the sum of an address its operands and a
#! shift the word it scales.
#! @param this The placing.
#! @param body The code.
#! @param value The value.
#! @param into Where the values are written.
def placing::read_through(&this, body: &code, value: u32, into: &mut vector<u32>): void {
val made := body.defining(value);
if made != 0 && this.folded[made] {
val kind: op = body.insts[made].kind;
if kind == op::add {
this.read_through(body, body.arg(made, 0), into);
this.read_through(body, body.arg(made, 1), into);
return;
}
if kind == op::shl {
into.append(body.arg(made, 0));
return;
}
}
into.append(value);
}
#! Finds where every value is read by an instruction: what it reads where it
#! is, as `read_by` says. Arguments of edges are not kept as reads, since a
#! move reads them from anywhere, though they keep a value live.
#! @param this The placing.
#! @param body The code.
def placing::read_all(&mut this, body: &code): void {
this.each_read(body);
this.reads.seal();
this.each_read(body);
}
#! Walks the reads of the instructions, in the order laid out, counting them
#! for each value, or, once `reads` is sealed, putting their positions.
#! @param this The placing.
#! @param body The code.
def placing::each_read(&mut this, body: &code): void {
val fill := this.reads.is_sealed;
var operands := vector::<u32>::new(scratch());
for k in 0..this.order.length {
var walk := body.walk(this.order[k]);
while walk.more() {
val index := walk.next();
continue if this.folded[index];
val position := 4 * this.slots[index] + 1;
this.read_by(body, index, &mut operands);
for &value in operands {
if fill {
this.reads.put(value, position);
} else {
this.reads.count(value);
}
}
}
}
}
# Life.
#! Finds the ranges each value is live over, and the values live where each
#! block begins: walking back from each read to the block making the value, as
#! the machine's assembler does.
#! @param this The placing.
#! @param body The code.
def placing::live(&mut this, body: &code): void {
val values := body.origins.length;
val blocks := body.blocks.length;
this.heads.resize(values, never);
# Each block's reads by value: the arguments of edges, read where a block
# ends, as well as the operands. These are gathered value by value.
var args := groups::<u32>::new(values, scratch());
var given := body.reader();
for pass in 0..2u {
if pass == 1 {
args.seal();
}
for k in 0..this.order.length {
val block: u32 = this.order[k];
val last: u32 = body.blocks[block].last;
continue if last == 0;
given.start(last);
while given.more() {
val value := given.next();
# Only what its edges give, as its operands are read where it is.
continue if given.operand() != no_operand;
if pass == 0 {
args.count(value);
} else {
args.put(value, block);
}
}
}
}
# The block of each read, which an instruction's place gives.
var block_of_slot := vector::<u32>::new(scratch());
block_of_slot.resize(this.places as u64);
for k in 0..this.order.length {
val block: u32 = this.order[k];
for s in this.start_slots[block]..this.end_slots[block] + 1 {
block_of_slot[s as u64] = block;
}
}
var walk := live_walk::new(blocks, scratch());
var keys := vector::<u64>::new(scratch());
var livein_pairs := vector::<u64>::new(scratch());
for v in 1..values {
continue if this.remade[v] || !this.made_somewhere(body, v as u32);
continue if this.reads.length(v) == 0 && args.length(v) == 0;
val home := body.home_of(v as u32);
continue if this.number[home] == never;
# Each read: in its block up to where it is, and live into that block
# where it is not where the value is made, and back from there over the
# blocks laid out.
walk.begin(v as u32, home);
for &position in this.reads.of(v) {
walk.read(block_of_slot[(position / 4) as u64], position);
}
for &block in args.of(v) {
walk.read_out(block);
}
walk.walk(body, &this.number, true);
# The ranges, in the order the blocks are laid out.
keys.clear();
for &block in walk.touched {
keys.append(pair_key(this.number[block], block));
}
keys.sort();
val first := this.froms.length as u32;
for &key in keys {
val block := pair_low(key);
val held: block_reach = walk.reaches[block];
val from := block == home ? this.def_at[v] : 4 * this.start_slots[block];
val to := held.out ? 4 * this.end_slots[block] + 4 : held.ends;
continue if to <= from;
if held.into && block != home {
livein_pairs.append(pair_key(block, v as u32));
}
val count := this.froms.length;
if count > first as u64 && this.tos[count - 1] == from {
this.tos[count - 1] = to;
continue;
}
this.froms.append(from);
this.tos.append(to);
}
continue if this.froms.length == first as u64;
val last := (this.froms.length - 1) as u32;
this.heads[v] = this.intervals.length as u32;
this.intervals.append(interval {
value = v as u32,
first = first,
last = last,
start = this.froms[first as u64],
end = this.tos[last as u64],
use_lo = 0,
use_hi = this.reads.length(v) as u32,
reg = no_reg,
next = never,
cursor = first
});
}
# The values live where each block begins, block by block, each block's in
# the order of the values, as they were found.
for &pair in livein_pairs {
this.liveins.count(pair_high(pair));
}
this.liveins.seal();
for &pair in livein_pairs {
this.liveins.put(pair_high(pair), pair_low(pair));
}
}
#! Whether a value is made by any instruction or block the layout holds.
#! @param this The placing.
#! @param body The code.
#! @param value The value.
#! @returns Whether it is.
def placing::made_somewhere(&this, body: &code, value: u32): bool {
return this.number[body.home_of(value)] != never;
}
#! The moves along an edge, made all at once.
#! @param this The placing.
#! @param edge The edge.
#! @returns The moves.
pub def placing::edge_moves_of(&this, edge: u32): span<transfer> {
val first: u32 = this.edge_first[edge as u64];
return this.edge_moves[first as u64:(this.edge_first[edge as u64 + 1] - first) as u64];
}
# Ranges.
#! Where a range of an interval begins, as far as the interval holds it.
#! @param this The placing.
#! @param it The interval.
#! @param at The range.
#! @returns Where.
def placing::from_of(&this, it: &interval, at: u32): u32 {
return at == it.first ? it.start : this.froms[at as u64];
}
#! Where a range of an interval ends, as far as the interval holds it.
#! @param this The placing.
#! @param it The interval.
#! @param at The range.
#! @returns Where.
def placing::to_of(&this, it: &interval, at: u32): u32 {
return at == it.last ? it.end : this.tos[at as u64];
}
#! Whether an interval holds its value at a position the scan has reached,
#! moving its cursor on, since the scan's position only grows.
#! @param this The placing.
#! @param id The interval.
#! @param at The position.
#! @returns Whether it does.
def placing::covers_now(&mut this, id: u32, at: u32): bool {
val it: interval = this.intervals[id];
var k := it.cursor;
while k < it.last && this.to_of(&it, k) <= at {
k++;
}
this.intervals[id].cursor = k;
return this.from_of(&it, k) <= at && at < this.to_of(&it, k);
}
#! The first position two intervals both hold, or `never`.
#! @param this The placing.
#! @param a One interval.
#! @param b The other.
#! @returns The position.
def placing::intersection(&this, a: u32, b: u32): u32 {
val x: interval = this.intervals[a];
val y: interval = this.intervals[b];
var i := x.first;
var j := y.first;
while i <= x.last && j <= y.last {
val xf := this.from_of(&x, i);
val xt := this.to_of(&x, i);
val yf := this.from_of(&y, j);
val yt := this.to_of(&y, j);
val lo := xf > yf ? xf : yf;
val hi := xt < yt ? xt : yt;
if lo < hi {
return lo;
}
if xt <= yt {
i++;
} else {
j++;
}
}
return never;
}
#! The first read of an interval at or after a position, or `never`.
#! @param this The placing.
#! @param id The interval.
#! @param at The position.
#! @returns Where.
def placing::next_use(&this, id: u32, at: u32): u32 {
val it: interval = this.intervals[id];
val reads := this.reads.of(it.value)[it.use_lo:(it.use_hi - it.use_lo) as u64];
val next := reads.lower_bound(&at);
return next < reads.length ? reads[next] : never;
}
#! The first call an interval holds its value across, at or after a position,
#! or `never`.
#! @param this The placing.
#! @param id The interval.
#! @param at The position.
#! @returns The call's clobber.
def placing::next_clobber(&this, id: u32, at: u32): u32 {
val it: interval = this.intervals[id];
val count := this.clobbers.length;
return never if count == 0;
for k in it.first..it.last + 1 {
val to := this.to_of(&it, k);
continue if to <= at;
val from := this.from_of(&it, k);
val lo := from > at ? from : at;
val low := this.clobbers.span.lower_bound(&lo);
if low < count && this.clobbers[low] < to {
return this.clobbers[low];
}
}
return never;
}
#! Splits an interval at a position, the part from there on becoming one of
#! its own, which comes after it among its value's parts.
#! @param this The placing.
#! @param id The interval, which holds positions before and after the
#! position.
#! @param at The position.
#! @returns The part split off.
def placing::split(&mut this, id: u32, at: u32): u32 {
val it: interval = this.intervals[id];
var k := it.first;
while k < it.last && this.to_of(&it, k) <= at {
k++;
}
var child := interval {
value = it.value,
first = k,
last = it.last,
end = it.end,
reg = no_reg,
next = it.next,
cursor = k
};
if this.from_of(&it, k) < at {
child.start = at;
this.intervals[id].last = k;
this.intervals[id].end = at;
} else {
child.start = this.from_of(&it, k);
this.intervals[id].last = k - 1;
this.intervals[id].end = this.to_of(&it, k - 1);
}
if this.intervals[id].cursor > this.intervals[id].last {
this.intervals[id].cursor = this.intervals[id].last;
}
# The reads before the part split off stay.
val reads := this.reads.of(it.value);
var mid := it.use_lo;
while mid < it.use_hi && reads[mid as u64] < child.start {
mid++;
}
child.use_lo = mid;
child.use_hi = it.use_hi;
this.intervals[id].use_hi = mid;
val made := this.intervals.length as u32;
this.intervals[id].next = made;
this.intervals.append(child);
return made;
}
# Hints.
#! Works out the hints: the register each value is passed in, as an argument
#! of a call or of the function itself, and the values each would best share a
#! register with, so a move between them goes.
#! @param this The placing.
#! @param body The code.
def placing::find_hints(&mut this, body: &code): void {
val values := body.origins.length;
this.hint.resize(values, no_reg);
val entry := &body.blocks[0].params;
for i in 0..entry.length {
break if i >= arg_count;
this.hint[entry[i]] = gprs[i] as u8;
}
# The pairs, each both ways round.
var pairs := vector::<u64>::new(scratch());
for k in 0..this.order.length {
var walk := body.walk(this.order[k]);
while walk.more() {
val at := walk.next();
continue if this.folded[at];
val held: inst = body.insts[at];
match held.kind {
# A function value's two words are not among the arguments it is
# passed in registers.
op::call, op::call_trait, op::call_value => {
val skip: u32 = held.kind == op::call_value ? 2 : 0;
for a in skip..held.argc {
break if (a - skip) as u64 >= arg_count;
val given := body.arg(at, a);
if this.hint[given] == no_reg && !this.real[given] {
this.hint[given] = gprs[(a - skip) as u64] as u8;
}
}
}
op::add, op::sub, op::band, op::bor, op::bxor, op::mul, op::shl, op::shrs, op::shru,
op::fadd, op::fsub, op::fmul, op::fdiv => {
pairs.append(pair_key(held.result, body.arg(at, 0)));
val commutes := held.kind == op::add || held.kind == op::band || held.kind == op::bor
|| held.kind == op::bxor || held.kind == op::mul || held.kind == op::fadd || held.kind == op::fmul;
if commutes {
pairs.append(pair_key(held.result, body.arg(at, 1)));
}
}
else => {}
}
for e in 0..body.edge_count(at) {
val edge := held.aux + e as u32;
val params := &body.blocks[body.edges[edge].target].params;
val args := &body.edges[edge].args;
for i in 0..args.length {
val param: u32 = params[i];
val arg: u32 = args[i];
continue if param == arg;
pairs.append(pair_key(param, arg));
pairs.append(pair_key(arg, param));
}
}
}
}
# Each value's partners, in the order they were found.
for &each in pairs {
this.partners.count(pair_high(each));
}
this.partners.seal();
for &each in pairs {
this.partners.put(pair_high(each), pair_low(each));
}
}
# The scan.
#! What the scan of one class of registers keeps.
struct scanning {
var heap: heap<u64>; #! The intervals not yet given a place, each its start and itself as a pair's key, least first.
var active: vector<u32>; #! Those holding a register where the scan is.
var inactive: vector<u32>; #! Those holding a register, in a hole where the scan is.
var real: bool; #! Whether the class is the SSE registers.
}
#! Gives each value of one class a place, a register where one is free and its
#! slot otherwise, in the order the values begin.
#! @param this The placing.
#! @param real Whether the class is the reals, which SSE registers hold.
def placing::scan(&mut this, real: bool): void {
var work := scanning {
heap = heap::<u64>::new(scratch()),
active = vector::<u32>::new(scratch()),
inactive = vector::<u32>::new(scratch()),
real = real
};
for v in 0..this.heads.length {
val head: u32 = this.heads[v];
continue if head == never || this.real[v] != real;
work.heap.push(pair_key(this.intervals[head].start, head));
}
while work.heap.length != 0 {
val id := pair_low(work.heap.peek());
work.heap.pop();
val position: u32 = this.intervals[id].start;
# Those that ended go, those in a hole wait, and those out of one hold
# their registers again.
this.sweep(&mut work.active, &mut work.inactive, position, false);
this.sweep(&mut work.inactive, &mut work.active, position, true);
this.assign(id, &mut work);
}
}
#! Takes out of a list of intervals holding registers those that ended before
#! a position, and moves to another those whose cover of it is as asked.
#! @param this The placing.
#! @param from The intervals swept.
#! @param to Where those moved go.
#! @param position The position.
#! @param covered Whether those covering the position move, rather than
#! those in a hole there.
def placing::sweep(&mut this, from: &mut vector<u32>, to: &mut vector<u32>, position: u32, covered: bool): void {
var i: u64 = 0;
while i < from.length {
val held: u32 = from[i];
val ended := this.intervals[held].end <= position;
if ended || this.covers_now(held, position) == covered {
if !ended {
to.append(held);
}
from[i] = from.last;
from.truncate();
continue;
}
i++;
}
}
#! Gives an interval a place: a register for as long as one is free, or the
#! register held by what is read last, or its slot. Across a call, only a
#! register the call keeps holds it.
#! @param this The placing.
#! @param id The interval.
#! @param work What the scan keeps.
def placing::assign(&mut this, id: u32, work: &mut scanning): void {
val position: u32 = this.intervals[id].start;
val end: u32 = this.intervals[id].end;
# 64 bytes of lanes are held in their slot, as the instructions working them
# load and store them there.
if this.bytes[this.intervals[id].value] == 64 {
this.spill(id, work);
return;
}
# Across a call in the place it begins, it can only be in a register the call
# keeps, which no SSE register is, or its slot.
val clobber := this.next_clobber(id, position);
val at_call := clobber != never && gap_of(clobber) <= position;
if at_call && work.real {
this.spill(id, work);
return;
}
# Each register is free until another holds it, and one a call clobbers
# until the call.
var free: u32[16];
val counted: u64 = 11;
for k in 0..counted {
val r := this.register_at(work.real, k);
free[r as u64] = this.call_free(work.real, r, clobber, at_call);
}
for &held in work.active {
free[this.intervals[held].reg as u64] = 0;
}
for &held in work.inactive {
val r := this.intervals[held].reg as u64;
continue if free[r] == 0;
val meets := this.intersection(held, id);
if meets < free[r] {
free[r] = meets;
}
}
# A register a hint names, free for the whole interval, saves a move.
var wishes: u8[8];
val wished := this.wishes_of(id, work.real, &mut wishes);
for w in 0..wished {
val r := wishes[w];
if free[r as u64] >= end && free[r as u64] > position {
this.intervals[id].reg = r;
work.active.append(id);
return;
}
}
# A register free for the whole interval, of those a call clobbers first, as
# the function saves one a call keeps, and the one free longest of them.
for pass in 0..2u {
var fits := no_reg;
for k in 0..counted {
val r := this.register_at(work.real, k);
continue if (!work.real && keeps(r)) != (pass == 1);
if free[r as u64] >= end && (fits == no_reg || free[r as u64] > free[fits as u64]) {
fits = r;
}
}
if fits != no_reg {
this.intervals[id].reg = fits;
work.active.append(id);
return;
}
}
# Across a call, a register the call keeps holds it whole where what holds
# one now is read after it, so a value read in a loop holding a call keeps
# one register throughout, rather than another at the loop's head than in
# its body, with moves between on every edge.
if !work.real && clobber != never && clobber < end && this.take_kept(id, position, work) {
return;
}
# Otherwise the one free longest holds it until it is not.
var best := this.register_at(work.real, 0);
for k in 1..counted {
val r := this.register_at(work.real, k);
if free[r as u64] > free[best as u64] {
best = r;
}
}
# Unless it is first read only after that, where it would only be moved to
# its slot: a block's parameter would then be moved into the register on
# every edge going to the block, as a loop's is on every way round it.
val until := free[best as u64];
if until > position {
val at := gap_of(until);
if at > position && this.next_use(id, position) < at {
work.heap.push(pair_key(at, this.split(id, at)));
this.intervals[id].reg = best;
work.active.append(id);
return;
}
}
# Every register is held: of those whose holders are read no more often than
# this, within a loop, or of all of them otherwise, the one whose holder is
# read last is taken, unless this is read later still and its holder no less
# often. So a value read in every arm of a loop keeps its register, rather
# than making way for one read once.
val looping := this.in_loop(position) || this.loop_live(id);
var used: u32[16];
var heavy: u32[16];
var loopy: bool[16];
for k in 0..counted {
val r := this.register_at(work.real, k);
used[r as u64] = this.call_free(work.real, r, clobber, at_call);
heavy[r as u64] = 0;
loopy[r as u64] = false;
}
this.holders(id, position, work, &mut used, &mut heavy, &mut loopy);
val mine := this.weight(id);
var taken := no_reg;
for k in 0..counted {
val r := this.register_at(work.real, k);
continue if (looping || loopy[r as u64]) && heavy[r as u64] > mine;
if taken == no_reg || used[r as u64] > used[taken as u64] {
taken = r;
}
}
# A holder read less makes way even where it is read first, but a call does
# not, nor a register it clobbers before this is read.
val first := this.next_use(id, position);
val lighter := looping && taken != no_reg && heavy[taken as u64] < mine
&& this.call_free(work.real, taken, clobber, at_call) > first;
if first == never || taken == no_reg || (used[taken as u64] <= first && !lighter) {
this.spill(id, work);
return;
}
this.take(id, taken, position, work);
if clobber != never && clobber < this.intervals[id].end && (work.real || !keeps(taken)) {
val at := gap_of(clobber);
work.heap.push(pair_key(at, this.split(id, at)));
}
work.active.append(id);
}
#! Gives an interval living across a call a register the call keeps, held now,
#! where what holds it is read no more often than it, and is next read after
#! it, or less often: of those, the one whose holder is read last.
#! @param this The placing.
#! @param id The interval.
#! @param position Where it begins.
#! @param work What the scan keeps.
#! @returns Whether it was given one.
def placing::take_kept(&mut this, id: u32, position: u32, work: &mut scanning): bool {
val first := this.next_use(id, position);
return false if first == never;
var used: u32[16];
var heavy: u32[16];
var loopy: bool[16];
for r in 0..16u {
used[r] = never;
heavy[r] = 0;
}
this.holders(id, position, work, &mut used, &mut heavy, &mut loopy);
val mine := this.weight(id);
var taken := no_reg;
for &r in kept_gprs {
continue if heavy[r as u64] > mine;
if taken == no_reg || used[r as u64] > used[taken as u64] {
taken = r as u8;
}
}
return false if taken == no_reg || (used[taken as u64] <= first && heavy[taken as u64] >= mine);
this.take(id, taken, position, work);
work.active.append(id);
return true;
}
#! Whether a block is the head of a loop, which an edge goes back to.
#! @param this The placing.
#! @param block The block.
#! @returns Whether it is.
pub def placing::heads_loop(&this, block: u32): bool {
val first := this.start_slots[block];
for &span in this.loops {
return true if pair_high(span) == first;
}
return false;
}
#! Whether a place is within a loop: between a loop's head and the edge going
#! back to it.
#! @param this The placing.
#! @param at The position.
#! @returns Whether it is.
def placing::in_loop(&this, at: u32): bool {
val place := at / 4;
for &span in this.loops {
return true if pair_high(span) <= place && place <= pair_low(span);
}
return false;
}
#! Finds which values live within a loop, as `looped` says.
#! @param this The placing.
def placing::find_looped(&mut this): void {
this.looped.resize(this.heads.length);
return if this.loops.length == 0;
for v in 0..this.heads.length {
var part: u32 = this.heads[v];
while part != never && !this.looped[v] {
val it: interval = this.intervals[part];
for k in it.first..it.last + 1 {
val from := this.froms[k as u64] / 4;
val to := (this.tos[k as u64] + 3) / 4;
for &span in this.loops {
if from <= pair_low(span) && pair_high(span) <= to {
this.looped[v] = true;
}
}
}
part = it.next;
}
}
}
#! Whether an interval's value lives within a loop.
#! @param this The placing.
#! @param id The interval.
#! @returns Whether it does.
def placing::loop_live(&this, id: u32): bool {
return this.looped[this.intervals[id].value];
}
#! How often an interval's value is read, its edges' arguments included: what
#! a value read less makes way for in a register.
#! @param this The placing.
#! @param id The interval.
#! @returns How often.
def placing::weight(&this, id: u32): u32 {
return this.counts[this.intervals[id].value];
}
#! Of each register, when what holds it, now or where it meets an interval, is
#! next read, how often the most read of those is, and whether any lives in a
#! loop.
#! @param this The placing.
#! @param id The interval.
#! @param position Where it begins.
#! @param work What the scan keeps.
#! @param used Each register's next read, lowered to its holders'.
#! @param heavy Each register's weight, raised to its holders'.
#! @param loopy Whether a holder of each lives in a loop.
def placing::holders(&this, id: u32, position: u32, work: &scanning, used: &mut u32[16], heavy: &mut u32[16], loopy: &mut bool[16]): void {
for &held in work.active {
val r := this.intervals[held].reg as u64;
val next := this.next_use(held, position);
if next < used[r] {
used[r] = next;
}
val weight := this.weight(held);
if weight > heavy[r] {
heavy[r] = weight;
}
loopy[r] = loopy[r] || this.loop_live(held);
}
for &held in work.inactive {
val r := this.intervals[held].reg as u64;
continue if this.intersection(held, id) == never;
val next := this.next_use(held, position);
if next < used[r] {
used[r] = next;
}
val weight := this.weight(held);
if weight > heavy[r] {
heavy[r] = weight;
}
loopy[r] = loopy[r] || this.loop_live(held);
}
}
#! Gives an interval a register, moving what held it to its slot: what holds
#! it now, and what holds it later where the two meet.
#! @param this The placing.
#! @param id The interval.
#! @param taken The register.
#! @param position Where the interval begins.
#! @param work What the scan keeps.
def placing::take(&mut this, id: u32, taken: u8, position: u32, work: &mut scanning): void {
this.intervals[id].reg = taken;
var i: u64 = 0;
while i < work.active.length {
val held: u32 = work.active[i];
if this.intervals[held].reg != taken {
i++;
continue;
}
work.active[i] = work.active.last;
work.active.truncate();
this.evict(held, position, work);
}
i = 0;
while i < work.inactive.length {
val held: u32 = work.inactive[i];
val meets := this.intervals[held].reg == taken ? this.intersection(held, id) : never;
if meets == never {
i++;
continue;
}
# What is left of it in the register, before where they meet, may still
# hold it past here, so it stays.
this.evict(held, meets, work);
if this.intervals[held].reg == taken && this.intervals[held].end > position {
i++;
continue;
}
work.inactive[i] = work.inactive.last;
work.inactive.truncate();
}
}
#! Until where a register may hold an interval, as far as calls go: past every
#! call for one a call keeps, and for another until the first call the
#! interval lives across, or not at all where it begins there.
#! @param this The placing.
#! @param real Whether the register is an SSE register.
#! @param r The register.
#! @param clobber The first call the interval lives across, or `never`.
#! @param at_call Whether the interval begins at that call.
#! @returns Where.
def placing::call_free(&this, real: bool, r: u8, clobber: u32, at_call: bool): u32 {
return never if !real && keeps(r);
return at_call ? 0 : clobber;
}
#! The registers an interval would best be in, the best first: that of the
#! part of its value before it, the one its value is passed in, and those its
#! partners were given.
#! @param this The placing.
#! @param id The interval.
#! @param real Whether its class is the SSE registers.
#! @param wishes Where the registers are written.
#! @returns How many.
def placing::wishes_of(&this, id: u32, real: bool, wishes: &mut u8[8]): u64 {
var count: u64 = 0;
val value: u32 = this.intervals[id].value;
var part: u32 = this.heads[value];
while part != never && part != id {
if this.intervals[part].next == id && this.intervals[part].reg != no_reg {
wishes[count] = this.intervals[part].reg;
count++;
}
part = this.intervals[part].next;
}
if !real && this.hint[value] != no_reg {
wishes[count] = this.hint[value];
count++;
}
for &partner in this.partners.of(value) {
break if count == 8;
continue if this.real[partner] != real;
var each: u32 = this.heads[partner];
while each != never && this.intervals[each].reg == no_reg {
each = this.intervals[each].next;
}
continue if each == never;
wishes[count] = this.intervals[each].reg;
count++;
}
return count;
}
#! The register of a class at a place in the order registers are chosen in.
#! @param this The placing.
#! @param real Whether the class is the SSE registers.
#! @param k The place.
#! @returns The register's number.
def placing::register_at(&this, real: bool, k: u64): u8 {
return real ? xmms[k] : gprs[k] as u8;
}
#! Puts an interval in its value's slot, until the first read past where it
#! begins, from which the rest is given a place of its own.
#! @param this The placing.
#! @param id The interval.
#! @param work What the scan keeps.
def placing::spill(&mut this, id: u32, work: &mut scanning): void {
this.intervals[id].reg = no_reg;
this.intervals[id].stacked = true;
val it: interval = this.intervals[id];
val begins := gap_of(it.start);
val reads := this.reads.of(it.value);
for u in it.use_lo..it.use_hi {
val at := gap_of(reads[u as u64]);
continue if at <= begins;
if at > it.start && at < it.end {
work.heap.push(pair_key(at, this.split(id, at)));
}
return;
}
}
#! Takes the register from an interval from a position on: the part from the
#! gap of that position goes to its value's slot, and its reads after are
#! given places of their own.
#! @param this The placing.
#! @param id The interval.
#! @param at The position.
#! @param work What the scan keeps.
def placing::evict(&mut this, id: u32, at: u32, work: &mut scanning): void {
val begins := gap_of(at);
if begins <= this.intervals[id].start {
this.spill(id, work);
return;
}
val child := this.split(id, begins);
this.spill(child, work);
}
# What was chosen.
#! Gives each value with a part in its slot a slot, and lists each value's
#! parts, in order, with where each is.
#! @param this The placing.
#! @param body The code.
def placing::choose(&mut this, body: &code): void {
val values := body.origins.length;
this.slot_of.resize(values);
this.child_first.resize(values + 1);
for v in 0..values {
this.slot_of[v] = never;
this.child_first[v] = this.child_starts.length as u32;
var id: u32 = this.heads[v];
while id != never {
val it: interval = this.intervals[id];
var held_in := loc { kind = place::none };
if it.stacked {
if this.slot_of[v] == never {
this.slot_of[v] = this.slot_count;
this.slot_count += (this.bytes[v] / 8) as u32;
}
held_in = in_slot(v as u32);
} else if it.reg != no_reg {
held_in = this.real[v] ? in_xmm(it.reg) : loc { kind = place::gpr, number = it.reg };
}
this.child_starts.append(it.start);
this.child_ends.append(it.end);
this.child_locs.append(held_in);
id = it.next;
}
}
this.child_first[values] = this.child_starts.length as u32;
}
#! Where a value is at a position.
#! @param this The placing.
#! @param value The value.
#! @param at The position.
#! @returns Where: made again, the part holding it there, or nowhere.
pub def placing::located(&this, value: u32, at: u32): loc {
if this.remade[value] {
return loc { kind = place::remade, value = value };
}
val first: u32 = this.child_first[value];
val last: u32 = this.child_first[value + 1];
return loc { kind = place::none } if first == last;
# The last part beginning at or before the position, or the first.
val starts := this.child_starts[first:(last - first) as u64];
val after := starts.upper_bound(&at);
return this.child_locs[first as u64 + (after == 0 ? 0 : after - 1)];
}
#! Where a value is read at an instruction.
#! @param this The placing.
#! @param value The value.
#! @param index The instruction.
#! @returns Where.
pub def placing::read_at(&this, value: u32, index: u32): loc {
return this.located(value, 4 * this.slots[index] + 1);
}
#! Where an instruction's result goes.
#! @param this The placing.
#! @param value The result.
#! @returns Where: nowhere for one never read.
pub def placing::made_at(&this, value: u32): loc {
return this.located(value, this.def_at[value]);
}
# Moves.
#! Finds the moves: where a value's part ends and the next begins within a
#! block, where a value made in a register is stored to its slot, and along
#! each edge, into what the block it goes to holds where it begins.
#! @param this The placing.
#! @param body The code.
def placing::resolve(&mut this, body: &code): void {
# The places that begin blocks, for telling the moves along edges from those
# within blocks.
var starts := vector::<bool>::new(scratch());
starts.resize(this.places as u64);
for k in 0..this.order.length {
starts[this.start_slots[this.order[k]] as u64] = true;
}
# Moves within blocks, gathered as the key of the pair of the place and the
# index of each move, then put in order of place, the stores first.
var keyed := vector::<u64>::new(scratch());
var found := vector::<transfer>::new(scratch());
val values := body.origins.length;
for v in 0..values {
val first: u32 = this.child_first[v];
val last: u32 = this.child_first[v + 1];
continue if first == last;
val own := in_slot(v as u32);
# A value with a slot is stored to it where it is made, unless it is made
# there.
if this.slot_of[v] != never {
val made: loc = this.child_locs[first as u64];
if made.kind != place::slot {
val at := this.def_at[v] / 4 + 1;
keyed.append(pair_key(at, found.length as u32));
found.append(transfer { from = made, to = own, bytes = this.bytes[v] });
}
}
for c in first + 1..last {
val before: u32 = c - 1;
val at: u32 = this.child_starts[c as u64];
continue if this.child_ends[before as u64] != at || starts[(at / 4) as u64];
val to: loc = this.child_locs[c as u64];
val from: loc = this.child_locs[before as u64];
continue if to.same(&own) || to.same(&from);
# Reloads come after the stores of the same gap, which are keyed first.
keyed.append(pair_key(at / 4, 0x80000000 | found.length as u32));
found.append(transfer { from = from, to = to, bytes = this.bytes[v] });
}
}
# Each place's moves follow one another, its stores first, then its reloads,
# each in the order they were found.
this.gap_moves.reset(this.places as u64);
for &key in keyed {
this.gap_moves.count(pair_high(key));
}
this.gap_moves.seal();
for reloads in 0..2u {
for &key in keyed {
continue if ((key & 0x80000000) != 0) != (reloads == 1);
this.gap_moves.put(pair_high(key), found[pair_low(key) & 0x7FFFFFFF]);
}
}
# Moves along each edge: each parameter of the block gone to takes its
# argument, and each value live into it goes where it is there.
this.edge_first.resize(body.edges.length + 1);
for e in 0..body.edges.length {
this.edge_first[e] = this.edge_moves.length as u32;
val edge: u32 = e as u32;
val target: u32 = body.edges[e].target;
val source := body.edge_source(edge);
continue if this.number[target] == never || this.number[source] == never;
val leaves := 4 * this.end_slots[source] + 1;
val enters := 4 * this.start_slots[target];
val params := &body.blocks[target].params;
for i in 0..params.length {
break if i >= body.edges[e].args.length;
val param: u32 = params[i];
val to := this.located(param, enters + 3);
continue if to.kind == place::none;
val arg: u32 = body.edges[e].args[i];
val from := this.located(arg, leaves);
continue if from.same(&to);
this.edge_moves.append(transfer { from = from, to = to, bytes = this.bytes[arg] });
}
for &value in this.liveins.of(target) {
val to := this.located(value, enters);
val from := this.located(value, leaves);
continue if to.same(&from) || to.same(&in_slot(value));
this.edge_moves.append(transfer { from = from, to = to, bytes = this.bytes[value] });
}
}
this.edge_first[body.edges.length] = this.edge_moves.length as u32;
}