#! @file promote.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the fifth pass of the optimizer, which promotes cells to values: a #! cell whose address is only read from, written to, zeroed, or copied in and #! out from its start, at offsets that do not overlap, holds values rather than #! memory. Each word it holds becomes a variable, and the code is rebuilt as it #! was built, each read reading what the variable holds there, which where #! control flow joins is a parameter of the block. The bytes a copy moves that #! nothing reads or writes alone are words of their own, so a value copied from #! one cell to another and on is promoted whole. Inlining leaves many such #! cells, where a function took the address of what it was given or returned. #! A `simd` read and written whole becomes one vector, which an executable holds #! in a vector register, but 32 bytes only in code for processors with AVX. import std::containers; module sakura::compiler; #! A word a cell promoted holds: where it is in the cell, how wide it is, and #! the type of value it holds. struct slot { var cell: u32; #! The cell. var offset: u64; #! Where it begins. var size: u64; #! How many bytes it is. var kind: vt; #! The type of value it holds. var signed: bool; #! Whether it is read as a signed integer narrower than a word, which a copy narrows it as too. var next: u32; #! One more than the next slot of the same cell, or 0. var used: bool; #! Whether anything reads what it holds, or copies it out. } #! What promoting knows of the cells of the code. struct promotion { var escaped: vector; #! Whether each cell's address is used as more than an address read and written through. var extent: vector; #! How many bytes from its start the copies in and out of each cell move, which its slots must cover. var slots: vector; #! The words of the cells, each once. var heads: vector; #! One more than the first slot of each cell, or 0, its others following by `next`. var addresses: cell_table; #! Which cell each value is the address of. var ends: dict; #! The value each slot holds at the end of each block that writes it, by `pair_key(slot, block)`. var starts: dict; #! The value each slot holds at the start of each block asked, by `pair_key(slot, block)`. var zeros: zero_values; #! The zero of each type, made once. var wide: bool; #! Whether 32 bytes of lanes may be a value. var vectors: bool; #! Whether 16 bytes of lanes may be, as an executable's code holds them. var pairs: vector; #! The copies from one cell into another. var copies: dict; #! The read each copy into a cell promoted makes of each slot, by `pair_key(copy, slot)`. var touch: vector; #! The instructions naming a cell, as operands or as what their edges give. var current: vector; #! What each slot holds where the walk of a block is, where the block wrote it. var written: stamped_set; #! The slots the block being rewritten wrote so far, a round each block. var dense: u64; #! How many blocks `ends` and `starts` are held for in `dense_ends` and `dense_starts`, by slot then block, or 0 where they are not. var dense_ends: vector; #! What `ends` holds, where it is dense, 0 for nothing. var dense_starts: vector; #! What `starts` holds, where it is dense. } #! What `promotion::slot_at` gives where a cell has no slot at an offset. const no_slot: u32 = 0xFFFFFFFF; #! The most slots of every block the values they hold at each block's ends are #! held for one after another, rather than by key. const dense_slot_blocks: u64 = 65536; #! The value a slot holds where a block ends, where the block writes it. #! @param this What promoting knows. #! @param s The slot. #! @param block The block. #! @returns The value, or 0. def promotion::end_at(&this, s: u32, block: u32): u32 { return this.dense_ends[s as u64 * this.dense + block as u64] if this.dense != 0; val key := pair_key(s, block); return this.ends.get_or(&key, 0); } #! Notes the value a slot holds where a block ends. #! @param this What promoting knows. #! @param s The slot. #! @param block The block. #! @param value The value. def promotion::set_end(&mut this, s: u32, block: u32, value: u32): void { if this.dense != 0 { this.dense_ends[s as u64 * this.dense + block as u64] = value; return; } this.ends.set(pair_key(s, block), value); } #! The value a slot holds where a block begins, where it is known. #! @param this What promoting knows. #! @param s The slot. #! @param block The block. #! @returns The value, or 0. def promotion::start_at(&this, s: u32, block: u32): u32 { return this.dense_starts[s as u64 * this.dense + block as u64] if this.dense != 0; val key := pair_key(s, block); return this.starts.get_or(&key, 0); } #! Notes the value a slot holds where a block begins. #! @param this What promoting knows. #! @param s The slot. #! @param block The block. #! @param value The value. def promotion::set_start(&mut this, s: u32, block: u32, value: u32): void { if this.dense != 0 { this.dense_starts[s as u64 * this.dense + block as u64] = value; return; } this.starts.set(pair_key(s, block), value); } #! Promotes the cells that can be, as the file says. #! @param this The code. #! @returns Whether any was. pub def code::promote_cells(&mut this): bool { return false if this.cells.length == 0; var state := promotion { escaped = vector::::new(scratch()), extent = vector::::new(scratch()), slots = vector::::new(scratch()), heads = vector::::new(scratch()), addresses = this.cell_table(), ends = dict::::new(scratch()), starts = dict::::new(scratch()), copies = dict::::new(scratch()), pairs = vector::::new(scratch()), touch = vector::::new(scratch()), current = vector::::new(scratch()), written = stamped_set::new(0, scratch()), dense_ends = vector::::new(scratch()), dense_starts = vector::::new(scratch()), wide = this.wide, vectors = this.vectors }; return false if !state.addresses.any; state.escaped.resize(this.cells.length); state.extent.resize(this.cells.length); state.heads.resize(this.cells.length); # A copy from a cell its words cover into another is the reads and writes of # each word, which then promote as any others, so the words of the cells # are found again, as many times as a value is copied on. this.find_touches(&mut state); var rounds := 0; while true { rounds++; this.find_slots(&mut state); break if rounds == 4 || !this.split_pairs(&mut state); for c in 0..this.cells.length { state.escaped[c] = false; state.extent[c] = 0; state.heads[c] = 0; } state.slots.clear(); state.pairs.clear(); } this.settle_pairs(&mut state); var promoted := false; for c in 0..this.cells.length { if !state.escaped[c] { promoted = true; } } return false if !promoted; # Where control flow joins, slots are found in the order of the blocks. if state.slots.length * this.blocks.length <= dense_slot_blocks { state.dense = this.blocks.length; state.dense_ends.resize(state.slots.length * state.dense); state.dense_starts.resize(state.slots.length * state.dense); } var order := vector::::new(scratch()); var number := vector::::new(scratch()); this.block_order(&mut order, &mut number); for &block in order { this.note_ends(block, &mut state); } var done := rewrite::new(); state.current.resize(state.slots.length); state.written = stamped_set::new(state.slots.length, scratch()); for &block in order { state.written.next_round(); this.rewrite_slots(block, &mut state, &mut done); } this.apply(&done); this.drop_unreached(); this.drop_lone_params(); return true; } #! Finds the instructions naming a cell, which are all that finding slots #! walks. #! @param this The code. #! @param state What promoting knows. def code::find_touches(&this, state: &mut promotion): void { state.touch.clear(); var walk := this.walk_all(); var reads := this.reader(); while walk.more() { val at := walk.next(); reads.start(at); while reads.more() { if state.addresses.cell(reads.next()) != 0 { state.touch.append(at); break; } } } } #! Leaves memory the cell each copy between cells neither escaped comes from, #! where the copy was not split, so the copy is one out of memory into the one #! it goes to. #! @param this The code. #! @param state What promoting knows. def code::settle_pairs(&this, state: &mut promotion): void { var changed := false; for © in state.pairs { val to := state.addresses.cell(this.arg(copy, 0)) - 1; val from := state.addresses.cell(this.arg(copy, 1)) - 1; if !state.escaped[to] && !state.escaped[from] { state.escaped[from] = true; changed = true; } } if changed { this.find_uses(state); } } #! Marks the slots of the cells not escaped that something reads, or copies #! out of its cell, so a copy into the cell reads only those from where it #! copies, and a slot read only as a copy moves it into another is used where #! the other's is. #! @param this The code. #! @param state What promoting knows. def code::find_uses(&this, state: &mut promotion): void { for i in 0..state.slots.length { state.slots[i].used = false; } for t in 0..state.touch.length { val at: u32 = state.touch[t]; val held: inst = this.insts[at]; continue if held.kind != op::load && held.kind != op::copy; val operand: u32 = held.kind == op::load ? 0 : 1; val cell := state.addresses.cell(this.arg(at, operand)); val into := held.kind == op::copy ? state.addresses.cell(this.arg(at, 0)) : 0; val moved := into != 0 && !state.escaped[into - 1]; continue if cell == 0 || state.escaped[cell - 1] || moved; var i: u32 = state.heads[cell - 1]; while i != 0 { val each: slot = state.slots[i - 1]; if (held.kind == op::load && each.offset == held.imm) || (held.kind == op::copy && each.offset < held.imm) { state.slots[i - 1].used = true; } i = each.next; } } var changed := true; var rounds := 0; while changed && rounds < 8 { rounds++; changed = false; for p in 0..state.pairs.length { val copy: u32 = state.pairs[p]; val to := state.addresses.cell(this.arg(copy, 0)) - 1; val from := state.addresses.cell(this.arg(copy, 1)) - 1; continue if state.escaped[to] || state.escaped[from]; var i: u32 = state.heads[from]; while i != 0 { val each: slot = state.slots[i - 1]; if !each.used && each.offset < this.insts[copy].imm && state.used_over(to, each.offset, each.size) { state.slots[i - 1].used = true; changed = true; } i = each.next; } } } } #! Finds how each cell's address is used, and the words each cell not escaped #! holds, marking escaped a cell its words do not fit, and which of its words #! are used. Copies between cells are gathered, to be split. #! @param this The code. #! @param state What promoting knows, whose instructions naming cells are #! found. def code::find_slots(&this, state: &mut promotion): void { var reads := this.reader(); for t in 0..state.touch.length { val at: u32 = state.touch[t]; val held: inst = this.insts[at]; reads.start(at); while reads.more() { val cell := state.addresses.cell(reads.next()); continue if cell == 0; val c := cell - 1; val i := reads.operand(); # A cell's address an edge gives escapes. if i == no_operand { state.escaped[c] = true; continue; } match held.kind { op::load => { val how := held.aux as access; state.add_slot(c, held.imm, how.width(), how.value_type(), narrow_signed(how)); } op::store => { if i == 0 { state.add_slot(c, held.imm, (held.aux as access).width(), (held.aux as access).value_type(), false); } else { state.escaped[c] = true; } } # A cell's address is never null, which inlining may check. op::clear => {} op::check => { if held.aux != fault::null_reference as u32 { state.escaped[c] = true; } } op::copy => { # A copy in or out of the cell from its start, between it and memory # no cell promoted is, reads or writes each of the words it moves. if held.imm > this.cells[c].size as u64 { state.escaped[c] = true; } else { if i == 0 && state.addresses.cell(this.arg(at, 1)) != 0 { state.pairs.append(at); } if held.imm > state.extent[c] { state.extent[c] = held.imm; } } } else => state.escaped[c] = true; } } } # A copy between cells moves the words either reads or writes alone, so each # holds the other's, from one to the next along a chain of copies. var rounds := 0; var changed := state.pairs.length != 0; while changed && rounds < 8 { rounds++; changed = false; for p in 0..state.pairs.length { val copy: u32 = state.pairs[p]; val to := state.addresses.cell(this.arg(copy, 0)) - 1; val from := state.addresses.cell(this.arg(copy, 1)) - 1; val size: u64 = this.insts[copy].imm; changed = state.share_slots(from, to, size) || changed; changed = state.share_slots(to, from, size) || changed; } } # Each cell's words must not overlap, and the bytes its copies move that no # word holds are words of their own. for c in 0..this.cells.length { continue if state.escaped[c]; var i: u32 = state.heads[c]; while i != 0 { val each: slot = state.slots[i - 1]; var j: u32 = each.next; while j != 0 { val other: slot = state.slots[j - 1]; if other.offset < each.offset + each.size && each.offset < other.offset + other.size { state.escaped[c] = true; } j = other.next; } i = each.next; } if !state.escaped[c] && state.extent[c] != 0 { state.fill_gaps(c as u32); } } # Nor may a clear or a copy end within a word. for t in 0..state.touch.length { val at: u32 = state.touch[t]; val held: inst = this.insts[at]; continue if held.kind != op::clear && held.kind != op::copy; for k in 0..held.argc { val cell := state.addresses.cell(this.arg(at, k as u32)); continue if cell == 0 || state.escaped[cell - 1]; val c := cell - 1; var i: u32 = state.heads[c]; while i != 0 { val each: slot = state.slots[i - 1]; if each.offset < held.imm && held.imm < each.offset + each.size { state.escaped[c] = true; } i = each.next; } } } this.find_uses(state); } #! Gives a cell the words another holds within the bytes a copy between them #! moves, which it does not hold yet. An escaped cell's words say nothing of #! the values another holds, as an array indexed holds what is read of it one #! lane at a time, so it gives none, and a vector copied into it stays one. #! @param this What promoting knows. #! @param from The cell whose words are given. #! @param to The cell given them. #! @param size How many bytes the copy moves. #! @returns Whether any was new. def promotion::share_slots(&mut this, from: u32, to: u32, size: u64): bool { return false if from == to || this.escaped[to] || this.escaped[from]; var added := false; var i: u32 = this.heads[from]; while i != 0 { val each: slot = this.slots[i - 1]; i = each.next; continue if each.offset >= size || this.slot_at(to, each.offset) != no_slot; this.add_slot(to, each.offset, each.size, each.kind, each.signed); added = true; } return added; } #! Gives the bytes a cell's copies move that no word holds words of their own, #! each as wide as where it begins and the bytes left allow. #! @param this What promoting knows. #! @param cell The cell, whose words do not overlap. def promotion::fill_gaps(&mut this, cell: u32): void { # The words it holds, by where each begins. var held := vector::::new(scratch()); var i: u32 = this.heads[cell]; while i != 0 { val each: slot = this.slots[i - 1]; held.append(pair_key(each.offset as u32, each.size as u32)); i = each.next; } held.sort(); val extent: u64 = this.extent[cell]; var at: u64 = 0; for &key in held { break if at >= extent; val offset := pair_high(key) as u64; this.fill(cell, at, offset < extent ? offset : extent); val end := offset + pair_low(key) as u64; if end > at { at = end; } } this.fill(cell, at, extent); } #! Gives bytes of a cell no word holds words of their own: in an executable's #! code, each 16 bytes from a multiple of 16 a vector, which a copy moves with #! one read and write, or 32 from a multiple of 32 in code for processors with #! AVX, and the rest words. #! @param this What promoting knows. #! @param cell The cell. #! @param from Where the bytes begin. #! @param to Where they end. def promotion::fill(&mut this, cell: u32, from: u64, to: u64): void { var at := from; while this.wide && at % 32 == 0 && at + 32 <= to { this.add_slot(cell, at, 32, vt::v256, false); at += 32; } while this.vectors && at % 16 == 0 && at + 16 <= to { this.add_slot(cell, at, 16, vt::v128, false); at += 16; } while at < to { var width: u64 = 8; while width > 1 && (at % width != 0 || at + width > to) { width = width / 2; } this.add_slot(cell, at, width, vt::word, false); at += width; } } #! Splits each copy from a cell its words cover whole, and not escaped, into #! another into a read of each word and a write of it where it goes. #! @param this The code. #! @param state What promoting knows. #! @returns Whether any copy was split. def code::split_pairs(&mut this, state: &mut promotion): bool { var done := rewrite::new(); var added := vector::::new(scratch()); for p in 0..state.pairs.length { val copy: u32 = state.pairs[p]; val held: inst = this.insts[copy]; val source := state.addresses.cell(this.arg(copy, 1)) - 1; continue if state.escaped[source]; var covered: u64 = 0; var i: u32 = state.heads[source]; while i != 0 { if state.slots[i - 1].offset < held.imm { covered += state.slots[i - 1].size; } i = state.slots[i - 1].next; } continue if covered != held.imm; # Only the words the cell it goes to reads go, where it holds them, each # after the copy, which goes. val target := state.addresses.cell(this.arg(copy, 0)) - 1; var after := copy; i = state.heads[source]; while i != 0 { val each: slot = state.slots[i - 1]; i = each.next; continue if each.offset >= held.imm; val there := state.slot_at(target, each.offset); continue if !state.escaped[target] && there != no_slot && !state.slots[there].used; var from: u32[1] = [this.arg(copy, 1)]; var gives: vt[1] = [each.kind]; val how := slot_access(&each) as u32; val load := this.detached(op::load, from[0:1], gives[0:1], how, each.offset, held.at, held.site); this.link_after(load, after); var to: u32[2] = [this.arg(copy, 0), this.insts[load].result]; val store := this.detached(op::store, to[0:2], span::::empty(), how, each.offset, held.at, held.site); this.link_after(store, load); after = store; added.append(load); added.append(store); } done.remove(copy); } return false if !done.changed; this.apply(&done); # The instructions naming cells are those that did but the copies split, and # the reads and writes they became. var kept: u64 = 0; for t in 0..state.touch.length { val at: u32 = state.touch[t]; if !done.is_removed(at) { state.touch[kept] = at; kept++; } } state.touch.resize(kept); state.touch.extend(added.view()); return true; } #! Adds a word to those a cell holds, where it is new, marking the cell #! escaped where the same offset is read or written as another width or kind, #! or where it is 32 bytes of lanes and the code may run without AVX, or 64. #! @param this What promoting knows. #! @param cell The cell. #! @param offset Where the word begins. #! @param size How many bytes it is. #! @param kind The type of value it holds. #! @param signed Whether it is read as a signed integer narrower than a word. def promotion::add_slot(&mut this, cell: u32, offset: u64, size: u64, kind: vt, signed: bool): void { # 64 bytes of lanes stay in memory, as only code knowing the processor has # AVX-512 holds them. if kind == vt::v512 || (kind == vt::v256 && !this.wide) { this.escaped[cell] = true; return; } var i: u32 = this.heads[cell]; while i != 0 { val each: slot = this.slots[i - 1]; if each.offset == offset { if each.size != size || each.kind != kind { this.escaped[cell] = true; } if signed { this.slots[i - 1].signed = true; } return; } i = each.next; } this.slots.append(slot { cell = cell, offset = offset, size = size, kind = kind, signed = signed, next = this.heads[cell] }); this.heads[cell] = this.slots.length as u32; } #! The slot of a cell at an offset, or `no_slot`. #! @param this What promoting knows. #! @param cell The cell. #! @param offset Where the word begins. #! @returns The slot's index. def promotion::slot_at(&this, cell: u32, offset: u64): u32 { var i: u32 = this.heads[cell]; while i != 0 { return i - 1 if this.slots[i - 1].offset == offset; i = this.slots[i - 1].next; } return no_slot; } #! Whether a slot of a cell that bytes of it lie within is used. The cell copied #! into may hold a word over several of the other's, as a vector over a struct's #! fields where one cell holds each in turn, so each of those is used where the #! word over it is. #! @param this What promoting knows. #! @param cell The cell. #! @param offset Where the bytes begin. #! @param size How many there are. #! @returns Whether one is. def promotion::used_over(&this, cell: u32, offset: u64, size: u64): bool { var i: u32 = this.heads[cell]; while i != 0 { val each: slot = this.slots[i - 1]; return true if each.used && each.offset < offset + size && offset < each.offset + each.size; i = each.next; } return false; } #! Notes the value each slot holds at the end of a block that writes it: what #! it last stores or copies in, or zero where it last clears it. #! @param this The code. #! @param block The block. #! @param state What promoting knows. def code::note_ends(&mut this, block: u32, state: &mut promotion): void { var walk := this.walk(block); while walk.more() { val at := walk.next(); val held: inst = this.insts[at]; continue if held.argc == 0; val target := state.addresses.cell(this.arg(at, 0)); continue if target == 0 || state.escaped[target - 1]; val c := target - 1; match held.kind { op::store => { state.set_end(state.slot_at(c, held.imm), block, this.arg(at, 1)); } op::clear => { var i: u32 = state.heads[c]; while i != 0 { val s := i - 1; val each: slot = state.slots[s]; i = each.next; continue if each.offset >= held.imm; state.set_end(s, block, state.zeros.of(this, each.kind)); } } # A copy in reads each word something reads from where it comes from, # read here, before any block asks what a slot holds. Where nothing reads # any, its first is still read, so an address that cannot be read stops # the program there as the copy did. op::copy => { var reads := !this.checked_address(at, this.arg(at, 1)); var i: u32 = state.heads[c]; while i != 0 { val each: slot = state.slots[i - 1]; if each.offset < held.imm && each.used { reads = false; } i = each.next; } var after := at; i = state.heads[c]; while i != 0 { val s := i - 1; val each: slot = state.slots[s]; i = each.next; continue if each.offset >= held.imm || (!each.used && !(reads && each.offset == 0)); var args: u32[1] = [this.arg(at, 1)]; var gives: vt[1] = [each.kind]; val load := this.detached(op::load, args[0:1], gives[0:1], slot_access(&each) as u32, each.offset, held.at, held.site); this.link_after(load, after); after = load; val value: u32 = this.insts[load].result; state.copies.set(pair_key(at, s), value); state.set_end(s, block, value); } } else => {} } } } #! The value a slot holds at the start of a block: zero at the entry, what it #! holds at the end of the one block going there, or else a parameter the #! block takes, given what it holds at the end of each. #! @param this The code. #! @param s The slot. #! @param block The block. #! @param state What promoting knows. #! @returns The value. def code::slot_start(&mut this, s: u32, block: u32, state: &mut promotion): u32 { val known := state.start_at(s, block); return known if known != 0; val kind := state.slots[s].kind; val preds := this.blocks[block].preds.length; if block == 0 || preds == 0 { val zero := state.zeros.of(this, kind); state.set_start(s, block, zero); return zero; } if preds == 1 { val edge: u32 = this.blocks[block].preds[0]; val made := this.slot_end(s, this.edge_source(edge), state); state.set_start(s, block, made); return made; } # Noted before its arguments are found, which may come back to it. val param := this.add_param(block, kind); state.set_start(s, block, param); var edges := this.blocks[block].preds.copied(scratch()); for &edge in edges { val given := this.slot_end(s, this.edge_source(edge), state); this.edges[edge].args.append(given); } return param; } #! The value a slot holds at the end of a block. #! @param this The code. #! @param s The slot. #! @param block The block. #! @param state What promoting knows. #! @returns The value. def code::slot_end(&mut this, s: u32, block: u32, state: &mut promotion): u32 { val known := state.end_at(s, block); return known if known != 0; return this.slot_start(s, block, state); } #! Notes what a slot holds where the walk of a block is. #! @param this What promoting knows. #! @param s The slot. #! @param value The value. def promotion::hold(&mut this, s: u32, value: u32): void { this.current[s] = value; this.written.put(s); } #! What a slot holds where the walk of a block is: what the block wrote last, #! or what it holds where the block begins. #! @param this The code. #! @param s The slot. #! @param block The block. #! @param state What promoting knows. #! @returns The value. def code::slot_now(&mut this, s: u32, block: u32, state: &mut promotion): u32 { return state.current[s] if state.written.contains(s); return this.slot_start(s, block, state); } #! Rewrites the reads and writes of the slots of a block: a read reads what #! the slot holds there, narrowed as the read narrows, and a write, a clear, #! and a copy in or out go, a copy becoming reads or writes of its words. #! @param this The code. #! @param block The block. #! @param state What promoting knows. #! @param done What the pass changes. def code::rewrite_slots(&mut this, block: u32, state: &mut promotion, done: &mut rewrite): void { var walk := this.walk(block); while walk.more() { val at := walk.next(); val held: inst = this.insts[at]; if held.kind == op::cell { if !state.escaped[held.aux] { done.remove(at); } continue; } continue if held.argc == 0; val first := state.addresses.cell(this.arg(at, 0)); val c := first != 0 && !state.escaped[first - 1] ? first - 1 : no_cell; match held.kind { op::check => { if c != no_cell { done.remove(at); } } op::load => { if c != no_cell { val s := state.slot_at(c, held.imm); val value := this.slot_now(s, block, state); this.read_slot(at, value, done); } } op::store => { if c != no_cell { state.hold(state.slot_at(c, held.imm), this.arg(at, 1)); done.remove(at); } } op::clear => { if c != no_cell { var i: u32 = state.heads[c]; while i != 0 { val each: slot = state.slots[i - 1]; if each.offset < held.imm { state.hold(i - 1, state.zeros.of(this, each.kind)); } i = each.next; } done.remove(at); } } op::copy => { val from := state.addresses.cell(this.arg(at, 1)); if c != no_cell { # Copied in: each word is what was read of where it comes from. var i: u32 = state.heads[c]; while i != 0 { val key := pair_key(at, i - 1); if state.copies.get(&key) is some(got) { state.hold(i - 1, got); } i = state.slots[i - 1].next; } done.remove(at); } else if from != 0 && !state.escaped[from - 1] { # Copied out: each word is written where it goes, after the copy, # which goes. val source := from - 1; var after := at; var i: u32 = state.heads[source]; while i != 0 { val s := i - 1; val each: slot = state.slots[s]; i = each.next; continue if each.offset >= held.imm; val value := this.slot_now(s, block, state); var args: u32[2] = [this.arg(at, 0), value]; val store := this.detached(op::store, args[0:2], span::::empty(), slot_access(&each) as u32, each.offset, held.at, held.site); this.link_after(store, after); after = store; } done.remove(at); } } else => {} } } } #! How a slot is read and written in memory where a copy moves it: a narrow #! integer as it is read, so a field of `i32` copied is narrowed as a read of it #! narrows it again, and the two are one. #! @param each The slot. #! @returns The access. def slot_access(each: &slot): access { return access::v128 if each.kind == vt::v128; return access::v256 if each.kind == vt::v256; return each.kind == vt::real ? (each.size == 4 ? access::f32 : access::f64) : match each.size { 1 => each.signed ? access::i8 : access::u8, 2 => each.signed ? access::i16 : access::u16, 4 => each.signed ? access::i32 : access::u32, else => access::i64 }; } #! Whether an access reads a signed integer narrower than a word. #! @param how The access. #! @returns Whether it does. def narrow_signed(how: access): bool { return how == access::i8 || how == access::i16 || how == access::i32; } #! Makes a read of a slot the value it holds, narrowed as the read narrows. #! @param this The code. #! @param index The `load`. #! @param value What the slot holds. #! @param done What the pass changes. def code::read_slot(&mut this, index: u32, value: u32, done: &mut rewrite): void { val how: access = this.insts[index].aux as access; var args: u32[1] = [value]; match how { access::i64, access::f64, access::v128, access::v256 => { done.replace(this.insts[index].result, value); done.remove(index); } access::f32 => this.become(index, op::fsingle, args[0:1], 0, 0); access::u8 => this.become(index, op::wrap, args[0:1], 8, 0); access::i8 => this.become(index, op::wrap, args[0:1], 8, 1); access::u16 => this.become(index, op::wrap, args[0:1], 16, 0); access::i16 => this.become(index, op::wrap, args[0:1], 16, 1); access::u32 => this.become(index, op::wrap, args[0:1], 32, 0); access::i32 => this.become(index, op::wrap, args[0:1], 32, 1); else => {} } done.changed = true; }