#! @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; #! Whether each cell's address goes where it may be read or written without the code saying. var place: vector; #! The place of each cell among those followed, or `unfollowed`. var cells: vector; #! The cell followed at each place. var exposed: vector; #! The places of the escaped cells followed. var zero: vector; #! The words known zero of each cell followed, where the walk is, a bit each. var clears: vector; #! Of each cell followed, the clear of it nothing has read since, or 0. var cleared: vector; #! The words that clear zeroes. var covered: vector; #! The words of it written whole since. var ends: vector; #! The words known zero where each block ends, a word for each place, block after block. var walked: vector; #! 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): bool { return false if this.cells.length == 0; var state := memory_known { addresses = this.cell_table(), escaped = vector::::new(scratch()), place = vector::::new(scratch()), cells = vector::::new(scratch()), exposed = vector::::new(scratch()), zero = vector::::new(scratch()), clears = vector::::new(scratch()), cleared = vector::::new(scratch()), covered = vector::::new(scratch()), ends = vector::::new(scratch()), walked = vector::::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::::new(scratch()); var number := vector::::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, 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, 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); } }