#! @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; #! The place of each cell among those split, or `unfollowed`. var cells: vector; #! The cell at each place. var extent: vector; #! Of each place, how many bytes from its start its uses touch. var begins: vector; #! Of each instruction, whether a value of the cell it writes, its first operand's, begins there. var round: vector; #! Of each place, the block it was last followed in, as one more than its place in the order, or 0. var pending: vector; #! Of each place, the first of the writes since it was last read, or 0. var covered: vector; #! Of each place, the bytes those writes cover, a bit each. var heads: vector; #! Of each place, one more than the last use of the value begun in the block, among `uses`, or 0. var uses: vector; #! The operands naming the cells, each as its instruction and its place among its operands, by `pair_key`. var links: vector; #! 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; #! 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::::new(scratch()), cells = vector::::new(scratch()), extent = vector::::new(scratch()), begins = vector::::new(scratch()), round = vector::::new(scratch()), pending = vector::::new(scratch()), covered = vector::::new(scratch()), heads = vector::::new(scratch()), uses = vector::::new(scratch()), links = vector::::new(scratch()), live_out = vector::::new(scratch()) }; return false if !state.addresses.any; return false if !this.find_splittable(&mut state); var order := vector::::new(scratch()); var number := vector::::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::::new(scratch()); refused.resize(count); var reach := vector::::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, state: &mut splitting): void { val words := state.words; val blocks := this.blocks.length; var exposed := vector::::new(scratch()); var killed := vector::::new(scratch()); var live_in := vector::::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::::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::::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; }