#! @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; #! The value read instead of each value, or 0. var removed: vector; #! 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::::new(scratch()), removed = vector::::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.view().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.view().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::::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; #! 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::::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 { var counts := vector::::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, 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::::empty(), 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::::empty(), 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, gives: span, 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::::empty(), 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::::empty(), 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::::empty(), 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): 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.view() { 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::::empty(), 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; }