1;; Rewrites for `band`, `bnot`, `bor`, `bxor` 2 3;; x | 0 == x | x == x. 4(rule (simplify (bor ty 5 x 6 (iconst_u ty 0))) 7 (subsume x)) 8(rule (simplify (bor ty x x)) 9 (subsume x)) 10 11;; x ^ 0 == x. 12(rule (simplify (bxor ty 13 x 14 (iconst_u ty 0))) 15 (subsume x)) 16 17;; x ^ x == 0. 18(rule (simplify (bxor (fits_in_64 (ty_int ty)) x x)) 19 (subsume (iconst_u ty 0))) 20 21;; x ^ not(x) == not(x) ^ x == x | not(x) == not(x) | x == -1. 22;; This identity also holds for non-integer types, vectors, and wider types. 23;; But `iconst` is only valid for integers up to 64 bits wide. 24(rule (simplify (bxor (fits_in_64 (ty_int ty)) x (bnot ty x))) (subsume (iconst ty (imm64 (ty_mask ty))))) 25(rule (simplify (bxor (fits_in_64 (ty_int ty)) (bnot ty x) x)) (subsume (iconst ty (imm64 (ty_mask ty))))) 26(rule (simplify (bor (fits_in_64 (ty_int ty)) x (bnot ty x))) (subsume (iconst ty (imm64 (ty_mask ty))))) 27(rule (simplify (bor (fits_in_64 (ty_int ty)) (bnot ty x) x)) (subsume (iconst ty (imm64 (ty_mask ty))))) 28 29;; x & x == x & -1 == x. 30(rule (simplify (band ty x x)) (subsume x)) 31(rule (simplify (band ty x (iconst_s ty -1))) 32 (subsume x)) 33 34;; x & 0 == x & not(x) == not(x) & x == 0. 35(rule (simplify (band ty _ zero @ (iconst_u ty 0))) (subsume zero)) 36(rule (simplify (band (fits_in_64 (ty_int ty)) x (bnot ty x))) (subsume (iconst_u ty 0))) 37(rule (simplify (band (fits_in_64 (ty_int ty)) (bnot ty x) x)) (subsume (iconst_u ty 0))) 38 39;; not(not(x)) == x. 40(rule (simplify (bnot ty (bnot ty x))) (subsume x)) 41 42;; DeMorgan's rule (two versions): 43;; bnot(bor(x, y)) == band(bnot(x), bnot(y)) 44(rule (simplify (bnot ty (bor ty x y))) 45 (band ty (bnot ty x) (bnot ty y))) 46;; bnot(band(x, y)) == bor(bnot(x), bnot(y)) 47(rule (simplify (bnot ty (band t x y))) 48 (bor ty (bnot ty x) (bnot ty y))) 49 50;; `or(and(x, y), not(y)) == or(x, not(y))` 51(rule (simplify (bor ty 52 (band ty x y) 53 z @ (bnot ty y))) 54 (bor ty x z)) 55;; Duplicate the rule but swap the `bor` operands because `bor` is 56;; commutative. We could, of course, add a `simplify` rule to do the commutative 57;; swap for all `bor`s but this will bloat the e-graph with many e-nodes. It is 58;; cheaper to have additional rules, rather than additional e-nodes, because we 59;; amortize their cost via ISLE's smart codegen. 60(rule (simplify (bor ty 61 z @ (bnot ty y) 62 (band ty x y))) 63 (bor ty x z)) 64 65;; `or(and(x, y), not(y)) == or(x, not(y))` specialized for constants, since 66;; otherwise we may not know that `z == not(y)` since we don't generally expand 67;; constants in the e-graph. 68;; 69;; (No need to duplicate for commutative `bor` for this constant version because 70;; we move constants to the right.) 71(rule (simplify (bor ty 72 (band ty x (iconst ty (u64_from_imm64 y))) 73 z @ (iconst ty (u64_from_imm64 zk)))) 74 (if-let $true (u64_eq (u64_and (ty_mask ty) zk) 75 (u64_and (ty_mask ty) (u64_not y)))) 76 (bor ty x z)) 77 78;; (x ^ -1) can be replaced with the `bnot` instruction 79(rule (simplify (bxor ty x (iconst_s ty -1))) 80 (bnot ty x)) 81 82;; sshr((x | -x), N) == bmask(x) where N = ty_bits(ty) - 1. 83;; 84;; (x | -x) sets the sign bit to 1 if x is nonzero, and 0 if x is zero. sshr propagates 85;; the sign bit to the rest of the value. 86(rule (simplify (sshr ty (bor ty x (ineg ty x)) (iconst ty (u64_from_imm64 shift_amt)))) 87 (if-let $true (u64_eq shift_amt (ty_shift_mask ty))) 88 (bmask ty x)) 89 90(rule (simplify (sshr ty (bor ty (ineg ty x) x) (iconst ty (u64_from_imm64 shift_amt)))) 91 (if-let $true (u64_eq shift_amt (ty_shift_mask ty))) 92 (bmask ty x)) 93 94;; Matches any expressions that preserve "truthiness". 95;; i.e. If the input is zero it remains zero, and if it is nonzero it can have 96;; a different value as long as it is still nonzero. 97(decl pure multi truthy (Value) Value) 98(rule (truthy (sextend _ x)) x) 99(rule (truthy (uextend _ x)) x) 100(rule (truthy (bmask _ x)) x) 101(rule (truthy (ineg _ x)) x) 102(rule (truthy (bswap _ x)) x) 103(rule (truthy (bitrev _ x)) x) 104(rule (truthy (popcnt _ x)) x) 105(rule (truthy (rotl _ x _)) x) 106(rule (truthy (rotr _ x _)) x) 107(rule (truthy (select _ x (iconst _ (u64_from_imm64 (u64_nonzero _))) (iconst _ (u64_from_imm64 0)))) x) 108;; (ne ty (iconst 0) v) is also canonicalized into this form via another rule 109(rule (truthy (ne _ x (iconst_u _ 0))) x) 110 111;; All of these expressions don't care about their input as long as it is truthy. 112;; so we can remove expressions that preserve that property from the input. 113(rule (simplify (bmask ty v)) (if-let x (truthy v)) (bmask ty x)) 114(rule (simplify (select ty v t f)) (if-let c (truthy v)) (select ty c t f)) 115;; (ne ty (iconst 0) v) is also canonicalized into this form via another rule 116(rule (simplify (ne cty v (iconst_u _ 0))) 117 (if-let c (truthy v)) 118 (if-let (value_type (ty_int_ref_scalar_64 ty)) c) 119 (ne cty c (iconst_u ty 0))) 120 121 122 123;; (sextend (bmask x)) can be replaced with (bmask x) since bmask 124;; supports any size of output type, regardless of input. 125;; Same with `ireduce` 126(rule (simplify (sextend ty (bmask _ x))) (bmask ty x)) 127(rule (simplify (ireduce ty (bmask _ x))) (bmask ty x)) 128 129;; (bswap (bswap x)) == x 130(rule (simplify (bswap ty (bswap ty x))) (subsume x)) 131 132;; (bitrev (bitrev x)) == x 133(rule (simplify (bitrev ty (bitrev ty x))) (subsume x)) 134 135;; WebAssembly doesn't have a native byte-swapping instruction at this time so 136;; languages which have a byte-swapping operation will compile it down to bit 137;; shifting and twiddling. This attempts to pattern match what LLVM currently 138;; generates today for the Rust code `a.swap_bytes()`. This might be a bit 139;; brittle over time and/or with other possible LLVM backend optimizations, but 140;; it's at least one way to generate a byte swap. 141;; 142;; Technically this could be permuted quite a few ways and currently there's no 143;; easy way to match all of them, so only one is matched here. 144(rule (simplify (bor ty @ $I32 145 (bor ty 146 (ishl ty x (iconst_u ty 24)) 147 (ishl ty 148 (band ty x (iconst_u ty 0xff00)) 149 (iconst_u ty 8))) 150 (bor ty 151 (band ty 152 (ushr ty x (iconst_u ty 8)) 153 (iconst_u ty 0xff00)) 154 (ushr ty x (iconst_u ty 24))))) 155 (bswap ty x)) 156 157(rule (simplify (bor ty @ $I64 158 (bor ty 159 (bor ty 160 (ishl ty x (iconst_u ty 56)) 161 (ishl ty 162 (band ty x (iconst_u ty 0xff00)) 163 (iconst_u ty 40))) 164 (bor ty 165 (ishl ty 166 (band ty x (iconst_u ty 0xff_0000)) 167 (iconst_u ty 24)) 168 (ishl ty 169 (band ty x (iconst_u ty 0xff00_0000)) 170 (iconst_u ty 8)))) 171 (bor ty 172 (bor ty 173 (band ty 174 (ushr ty x (iconst_u ty 8)) 175 (iconst_u ty 0xff00_0000)) 176 (band ty 177 (ushr ty x (iconst_u ty 24)) 178 (iconst_u ty 0xff_0000))) 179 (bor ty 180 (band ty 181 (ushr ty x (iconst_u ty 40)) 182 (iconst_u ty 0xff00)) 183 (ushr ty x (iconst_u ty 56)))))) 184 (bswap ty x)) 185