#! @file bits.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the pass over the bits of words, which works out two things of each #! word as LLVM's known bits and demanded bits do: which of its bits are known #! to be zero, from what makes it, and which of its bits anything reads, from #! what reads it. #! #! What is known to be zero comes first. A narrowing of a word holding nothing #! past the bits it keeps goes, as `wrap(x & 255, 32, signed)` is `x & 255`, and #! so does a mask clearing only bits known to be zero; a comparison those bits #! decide is the constant it gives, as a byte compared with 256 is; and the #! rounding of an `f32` made of a word of fewer than 25 bits goes, as the `f32` #! holds it exactly. What is known of a word is worked out as it is asked for, #! from what made it, a few steps back, as LLVM works it out: a block's #! parameter knows what every edge into it gives, so a loop's counter of `u32` #! stays below 2^32 where each way round narrows it. #! #! Then, of the code that leaves, what is read. The low bits of a sum, a #! difference, or a product are worked out from the low bits of what it takes #! alone, a store of a byte writes only the low 8 bits of its word, and a mask #! reads only the bits it keeps. So a narrowing whose bits past those it keeps #! nothing reads goes, as a sum of `i32`s narrowed, multiplied, and narrowed #! again needs only the last; and a mask or other bits set or flipped where #! nothing reads them goes too. Each word that goes is replaced by one that #! agrees with it on every bit read of it, so what reads it gives what it did #! wherever it is read. module sakura::compiler; #! Every bit of a word. const all_bits: u64 = 0xFFFFFFFFFFFFFFFFu; #! How many steps back from a word what is known of it is looked for. const bits_depth: u32 = 8; #! How many words in all are looked at for what is known of one. const bits_budget: u32 = 48; #! A word of its low bits set. #! @param count How many. #! @returns The word. def low_ones(count: u64): u64 { return count >= 64 ? all_bits : (1u << count) - 1; } #! A word of its high bits set. #! @param count How many. #! @returns The word. def high_ones(count: u64): u64 { return count == 0 ? 0 : (count >= 64 ? all_bits : all_bits << (64 - count)); } #! The bits up to and including the highest bit of a word set, as the low bits #! of a sum or a product read the bits of what it takes. #! @param bits The word. #! @returns The bits. def up_to_highest(bits: u64): u64 { return low_ones(64 - leading_zeros_of(bits)); } #! Simplifies what the bits of the code's words decide, as the file says: first #! by what is known to be zero, then by what is read of what is left. #! @param this The code. #! @returns Whether anything changed. pub def code::trim_bits(&mut this): bool { # Only narrowing, masking, comparing, and rounding are simplified, so code # holding none has nothing to do. var any := false; var scan := this.walk_all(); while scan.more() && !any { match this.insts[scan.next()].kind { op::wrap, op::band, op::bor, op::bxor, op::fsingle, op::eq, op::ne, op::ltu, op::leu, op::lts, op::les => any = true; else => {} } } return false if !any; var order := vector::::new(scratch()); var number := vector::::new(scratch()); this.block_order(&mut order, &mut number); var known := rewrite::new(); var masks := false; for &block in order { var walk := this.walk(block); while walk.more() { masks = this.trim_known(walk.next(), number.span, &mut known) || masks; } } this.apply(&known); return known.changed if !masks; var read := rewrite::new(); val needs := this.needed_bits(order.span, number.span); for &block in order { var walk := this.walk(block); while walk.more() { this.trim_unread(walk.next(), &needs, &mut read); } } this.apply(&read); return known.changed || read.changed; } # What is known to be zero. #! The bits of a word known to be zero, worked out from what made it, and what #! made that in turn, as LLVM's known bits works them out: as far back as #! `bits_depth` steps, and through no more than `bits_budget` words in all, a #! block's parameter knowing what every edge reaching it gives it. Past those, #! a word is taken to be anything, as the function's own parameters are. #! @param this The code. #! @param value The word. #! @param depth How many steps back it is from what was asked. #! @param number Each block's place in the order the blocks run in. #! @param budget How many more words may be looked at. #! @returns The bits. def code::zeros_at(&this, value: u32, depth: u32, number: span, budget: &mut u32): u64 { return 0 if value == 0 || depth >= bits_depth || budget == 0 || this.origins[value].kind != vt::word; budget = budget - 1; val from: origin = this.origins[value]; return this.zeros_of(from.from, depth, number, budget) if !from.param; return 0 if from.from == 0; var bits := all_bits; for &edge in this.blocks[from.from].preds { continue if number[this.edge_source(edge)] == 0xFFFFFFFF; val given: u32 = this.edges[edge].args[from.index]; # A loop giving it back as it took it changes nothing of it. continue if given == value; bits = bits & this.zeros_at(given, depth + 1, number, budget); return 0 if bits == 0; } return bits; } #! The bits known to be zero of the word an instruction gives, from those of #! what it takes. #! @param this The code. #! @param index The instruction. #! @param depth How many steps back it is from what was asked. #! @param number Each block's place in the order the blocks run in. #! @param budget How many more words may be looked at. #! @returns The bits: none where it gives no word. def code::zeros_of(&this, index: u32, depth: u32, number: span, budget: &mut u32): u64 { val held: inst = this.insts[index]; match held.kind { op::iconst => return ~held.imm; op::eq, op::ne, op::lts, op::ltu, op::les, op::leu, op::eqsu, op::ltsu, op::lesu, op::ltus, op::leus, op::feq, op::fne, op::flt, op::fle, op::lnot => return ~1u; op::ones, op::clz, op::ctz => return ~127u; op::load => { return match held.aux as access { access::u8 => ~low_ones(8), access::u16 => ~low_ones(16), access::u32 => ~low_ones(32), else => 0 }; } op::band, op::bor, op::bxor, op::select, op::shl, op::shru, op::shrs, op::add, op::sub, op::mul, op::divu, op::remu, op::wrap => {} else => return 0; } # A shift by a constant past the word's bits is taken to be unknown. var by: u64 = 0; val constant := held.argc == 2 && this.word_constant(this.arg(index, 1), &mut by); if !constant { by = 64; } val a := this.zeros_at(this.arg(index, held.kind == op::select ? 1 : 0), depth + 1, number, budget); var b: u64 = 0; if held.argc >= 2 && held.kind != op::shl && held.kind != op::shru && held.kind != op::shrs && held.kind != op::wrap { b = this.zeros_at(this.arg(index, held.kind == op::select ? 2 : 1), depth + 1, number, budget); } match held.kind { op::band => return a | b; op::bor, op::bxor, op::select => return a & b; op::shl => return by < 64 ? (a << by) | low_ones(by) : 0; op::shru => return by < 64 ? (a >> by) | high_ones(by) : 0; op::shrs => return by < 64 ? ((a as i64) >> by) as u64 : 0; op::add => { # A sum carries one bit past the higher of what it adds. val low := trailing_zeros_of(~a) < trailing_zeros_of(~b) ? trailing_zeros_of(~a) : trailing_zeros_of(~b); val high := leading_zeros_of(~a) < leading_zeros_of(~b) ? leading_zeros_of(~a) : leading_zeros_of(~b); return low_ones(low) | high_ones(high == 0 ? 0 : high - 1); } op::sub => { val low := trailing_zeros_of(~a) < trailing_zeros_of(~b) ? trailing_zeros_of(~a) : trailing_zeros_of(~b); return low_ones(low); } op::mul => { # A product of words of p and q bits holds p + q bits at most. val low := trailing_zeros_of(~a) + trailing_zeros_of(~b); val wide := (64 - leading_zeros_of(~a)) + (64 - leading_zeros_of(~b)); return low_ones(low) | (wide < 64 ? high_ones(64 - wide) : 0); } op::divu => return high_ones(leading_zeros_of(~a)); op::remu => { # What is left is below what divides, and no more than what is divided. var high := leading_zeros_of(~a); if constant && by != 0 { val under := leading_zeros_of(by - 1); high = under > high ? under : high; } return high_ones(high); } op::wrap => { val kept := low_ones(held.aux as u64); return a | ~kept if held.imm == 0 || held.aux == 0; # A narrowing keeping the sign is zero past the bits it keeps where the # highest of them is. val sign := 1u << (held.aux as u64 - 1); return (a & sign) != 0 ? a | ~kept : a & (kept >> 1); } else => return 0; } return 0; } #! The bits of a word known to be zero, as asked where the pass simplifies. #! @param this The code. #! @param value The word. #! @param number Each block's place in the order the blocks run in. #! @returns The bits. def code::known_zeros(&this, value: u32, number: span): u64 { var budget := bits_budget; return this.zeros_at(value, 0, number, &mut budget); } #! Simplifies an instruction by what is known to be zero of the words it takes, #! as the file says, where what it gives is then known exactly. #! @param this The code. #! @param index The instruction. #! @param number Each block's place in the order the blocks run in. #! @param done What the pass changes. #! @returns Whether it is a narrowing or mask by a constant that stays, which #! what is read of what it gives may yet take out. def code::trim_known(&mut this, index: u32, number: span, done: &mut rewrite): bool { val held: inst = this.insts[index]; return false if held.count != 1; match held.kind { op::wrap => { return false if held.aux >= 64 || held.aux == 0; val a := this.arg(index, 0); val kept := low_ones(held.imm != 0 ? held.aux as u64 - 1 : held.aux as u64); if (this.known_zeros(a, number) | kept) == all_bits { this.forward_to(index, a, done); return false; } return true; } op::band, op::bor, op::bxor => { var mask: u64 = 0; return false if !this.word_constant(this.arg(index, 1), &mut mask); val a := this.arg(index, 0); if held.kind == op::band && (this.known_zeros(a, number) | mask) == all_bits { this.forward_to(index, a, done); return false; } return true; } op::fsingle => { # A word of 24 bits or fewer made a real is an `f32` already. val made := this.defining(this.arg(index, 0)); return false if made == 0 || (this.insts[made].kind != op::itof && this.insts[made].kind != op::utof); if (this.known_zeros(this.arg(made, 0), number) | low_ones(24)) == all_bits { this.forward_to(index, this.arg(index, 0), done); } } op::eq, op::ne, op::ltu, op::leu, op::lts, op::les => this.decide_order(index, number, done); else => {} } return false; } #! Decides a comparison of a word with a constant by what is known to be zero #! of the word: an equality where the constant has a bit the word has not, and #! an order where every value the word may hold is on one side. #! @param this The code. #! @param index The comparison. #! @param number Each block's place in the order the blocks run in. #! @param done What the pass changes. def code::decide_order(&mut this, index: u32, number: span, done: &mut rewrite): void { val kind := this.insts[index].kind; val a := this.arg(index, 0); val b := this.arg(index, 1); var c: u64 = 0; var left := false; if this.word_constant(b, &mut c) { left = false; } else if this.word_constant(a, &mut c) { left = true; } else { return; } # The greatest the other may be, which is also its greatest as a signed word # where its sign is known to be zero. val most := ~this.known_zeros(left ? b : a, number); return if most == all_bits; val positive := (most >> 63) == 0; var decided: u64 = 2; match kind { op::eq => decided = (c & ~most) != 0 ? 0 : 2; op::ne => decided = (c & ~most) != 0 ? 1 : 2; op::ltu => decided = left ? (c >= most ? 0 : 2) : (most < c ? 1 : 2); op::leu => decided = left ? (c > most ? 0 : 2) : (most <= c ? 1 : 2); op::lts, op::les => { # A word whose sign is zero is from 0 to the greatest it may be, so above # a negative constant, and not below 0. return if !positive; val strict := kind == op::lts; val negative := (c >> 63) != 0; if left { if negative || (!strict && c == 0) { decided = 1; } else if strict ? c >= most : c > most { decided = 0; } } else { if negative || (strict && c == 0) { decided = 0; } else if strict ? most < c : most <= c { decided = 1; } } } else => {} } return if decided == 2; this.become_word(index, decided); done.changed = true; } # What is read. #! Whether an instruction's operands are read only as what it gives is: an #! operator of words whose bits are worked out from those of what it takes, so #! what of those is read follows from what is read of it. #! @param this The code. #! @param index The instruction. #! @returns Whether they are. def code::passes_bits(&this, index: u32): bool { val held := &this.insts[index]; return false if held.count != 1 || this.origins[held.result].kind != vt::word; return match held.kind { op::add, op::sub, op::mul, op::neg, op::band, op::bor, op::bxor, op::bnot, op::shl, op::shru, op::shrs, op::wrap, op::select => true, else => false }; } #! The bits of each word of the code that anything reads, by value, found from #! what reads words through other than the operators passing bits on, as #! comparisons, calls, and returns do, then through those operators and the #! edges giving blocks' parameters, back to what made each word. #! @param this The code. #! @param order The blocks in the order they run. #! @param number Each block's place in that order. #! @returns The bits, in scratch memory. def code::needed_bits(&this, order: span, number: span): vector { var needs := vector::::new(scratch()); needs.resize(this.origins.length); var todo := vector::::new(scratch()); for &block in order { var walk := this.walk(block); while walk.more() { val at := walk.next(); continue if this.passes_bits(at); val held: inst = this.insts[at]; for i in 0..held.argc { var bits := all_bits; # A store of fewer than 8 bytes writes only the low bits of its word. if held.kind == op::store && i == 1 { match held.aux as access { access::i8, access::u8, access::i16, access::u16, access::i32, access::u32 => { bits = low_ones(8 * (held.aux as access).width()); } else => {} } } need_bits(this.arg(at, i as u32), bits, &mut needs, &mut todo); } } } while todo.length != 0 { val value: u32 = todo.last; todo.truncate(); val bits: u64 = needs[value]; val from: origin = this.origins[value]; if from.param { for &edge in this.blocks[from.from].preds { continue if number[this.edge_source(edge)] == 0xFFFFFFFF; need_bits(this.edges[edge].args[from.index], bits, &mut needs, &mut todo); } continue; } continue if from.from == 0 || !this.passes_bits(from.from); val at := from.from; for i in 0..this.insts[at].argc { need_bits(this.arg(at, i as u32), this.passed_bits(at, i as u32, bits), &mut needs, &mut todo); } } return needs; } #! Notes that bits of a word are read, so what made it reads what those need. #! @param value The word. #! @param bits The bits read. #! @param needs The bits read of each value. #! @param todo The values whose bits read grew, which what made them is #! asked of again for. def need_bits(value: u32, bits: u64, needs: &mut vector, todo: &mut vector): void { return if value == 0; val grown := needs[value] | bits; return if grown == needs[value]; needs[value] = grown; todo.append(value); } #! The bits of an operand an operator passing bits on reads, where some bits of #! what it gives are read. #! @param this The code. #! @param index The instruction. #! @param operand Which operand. #! @param bits The bits read of what it gives. #! @returns The bits of the operand read. def code::passed_bits(&this, index: u32, operand: u32, bits: u64): u64 { val held: inst = this.insts[index]; var other: u64 = 0; val known := held.argc == 2 && this.word_constant(this.arg(index, 1 - operand), &mut other); match held.kind { op::add, op::sub, op::mul, op::neg => return up_to_highest(bits); op::band => return known ? bits & other : bits; op::bor => return known ? bits & ~other : bits; op::bxor, op::bnot => return bits; op::shl => { return all_bits if operand == 1; return known && other < 64 ? bits >> other : up_to_highest(bits); } op::shru => { return all_bits if operand == 1 || !known || other >= 64; return bits << other; } op::shrs => { # What a shift keeping the sign moves in is the sign. return all_bits if operand == 1 || !known || other >= 64; return (bits << other) | ((bits & high_ones(other)) != 0 ? 1u << 63 : 0); } op::select => return operand == 0 ? all_bits : bits; op::wrap => { # What a narrowing keeping the sign gives past its bits is the highest of # them. val kept := low_ones(held.aux as u64); return bits if held.aux >= 64; val sign: u64 = held.imm != 0 && held.aux > 0 && (bits & ~kept) != 0 ? 1u << (held.aux as u64 - 1) : 0; return (bits & kept) | sign; } else => return all_bits; } return all_bits; } #! Simplifies an instruction by what is read of what it gives, as the file #! says: a narrowing, or a mask, setting, or flipping of bits, that changes #! nothing read of it is what it takes. #! @param this The code. #! @param index The instruction. #! @param needs The bits read of each value. #! @param done What the pass changes. def code::trim_unread(&mut this, index: u32, needs: &vector, done: &mut rewrite): void { val held: inst = this.insts[index]; return if held.count != 1 || this.origins[held.result].kind != vt::word; val read: u64 = needs[held.result]; match held.kind { op::wrap => { if held.aux < 64 && (read & ~low_ones(held.aux as u64)) == 0 { this.forward_to(index, this.arg(index, 0), done); } } op::band, op::bor, op::bxor => { var mask: u64 = 0; return if !this.word_constant(this.arg(index, 1), &mut mask); val unchanged := held.kind == op::band ? (read & ~mask) == 0 : (read & mask) == 0; if unchanged { this.forward_to(index, this.arg(index, 0), done); } } else => {} } }