github.com/filosottile/go@v0.0.0-20170906193555-dbed9972d994/src/cmd/compile/internal/ssa/gen/generic.rules (about) 1 // Copyright 2015 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Simplifications that apply to all backend architectures. As an example, this 6 // Go source code 7 // 8 // y := 0 * x 9 // 10 // can be translated into y := 0 without losing any information, which saves a 11 // pointless multiplication instruction. Other .rules files in this directory 12 // (for example AMD64.rules) contain rules specific to the architecture in the 13 // filename. The rules here apply to every architecture. 14 // 15 // The code for parsing this file lives in rulegen.go; this file generates 16 // ssa/rewritegeneric.go. 17 18 // values are specified using the following format: 19 // (op <type> [auxint] {aux} arg0 arg1 ...) 20 // the type, aux, and auxint fields are optional 21 // on the matching side 22 // - the type, aux, and auxint fields must match if they are specified. 23 // - the first occurrence of a variable defines that variable. Subsequent 24 // uses must match (be == to) the first use. 25 // - v is defined to be the value matched. 26 // - an additional conditional can be provided after the match pattern with "&&". 27 // on the generated side 28 // - the type of the top-level expression is the same as the one on the left-hand side. 29 // - the type of any subexpressions must be specified explicitly (or 30 // be specified in the op's type field). 31 // - auxint will be 0 if not specified. 32 // - aux will be nil if not specified. 33 34 // blocks are specified using the following format: 35 // (kind controlvalue succ0 succ1 ...) 36 // controlvalue must be "nil" or a value expression 37 // succ* fields must be variables 38 // For now, the generated successors must be a permutation of the matched successors. 39 40 // constant folding 41 (Trunc16to8 (Const16 [c])) -> (Const8 [int64(int8(c))]) 42 (Trunc32to8 (Const32 [c])) -> (Const8 [int64(int8(c))]) 43 (Trunc32to16 (Const32 [c])) -> (Const16 [int64(int16(c))]) 44 (Trunc64to8 (Const64 [c])) -> (Const8 [int64(int8(c))]) 45 (Trunc64to16 (Const64 [c])) -> (Const16 [int64(int16(c))]) 46 (Trunc64to32 (Const64 [c])) -> (Const32 [int64(int32(c))]) 47 (Cvt64Fto32F (Const64F [c])) -> (Const32F [f2i(float64(i2f32(c)))]) 48 (Cvt32Fto64F (Const32F [c])) -> (Const64F [c]) // c is already a 64 bit float 49 (Round32F x:(Const32F)) -> x 50 (Round64F x:(Const64F)) -> x 51 52 (Trunc16to8 (ZeroExt8to16 x)) -> x 53 (Trunc32to8 (ZeroExt8to32 x)) -> x 54 (Trunc32to16 (ZeroExt8to32 x)) -> (ZeroExt8to16 x) 55 (Trunc32to16 (ZeroExt16to32 x)) -> x 56 (Trunc64to8 (ZeroExt8to64 x)) -> x 57 (Trunc64to16 (ZeroExt8to64 x)) -> (ZeroExt8to16 x) 58 (Trunc64to16 (ZeroExt16to64 x)) -> x 59 (Trunc64to32 (ZeroExt8to64 x)) -> (ZeroExt8to32 x) 60 (Trunc64to32 (ZeroExt16to64 x)) -> (ZeroExt16to32 x) 61 (Trunc64to32 (ZeroExt32to64 x)) -> x 62 (Trunc16to8 (SignExt8to16 x)) -> x 63 (Trunc32to8 (SignExt8to32 x)) -> x 64 (Trunc32to16 (SignExt8to32 x)) -> (SignExt8to16 x) 65 (Trunc32to16 (SignExt16to32 x)) -> x 66 (Trunc64to8 (SignExt8to64 x)) -> x 67 (Trunc64to16 (SignExt8to64 x)) -> (SignExt8to16 x) 68 (Trunc64to16 (SignExt16to64 x)) -> x 69 (Trunc64to32 (SignExt8to64 x)) -> (SignExt8to32 x) 70 (Trunc64to32 (SignExt16to64 x)) -> (SignExt16to32 x) 71 (Trunc64to32 (SignExt32to64 x)) -> x 72 73 (ZeroExt8to16 (Const8 [c])) -> (Const16 [int64( uint8(c))]) 74 (ZeroExt8to32 (Const8 [c])) -> (Const32 [int64( uint8(c))]) 75 (ZeroExt8to64 (Const8 [c])) -> (Const64 [int64( uint8(c))]) 76 (ZeroExt16to32 (Const16 [c])) -> (Const32 [int64(uint16(c))]) 77 (ZeroExt16to64 (Const16 [c])) -> (Const64 [int64(uint16(c))]) 78 (ZeroExt32to64 (Const32 [c])) -> (Const64 [int64(uint32(c))]) 79 (SignExt8to16 (Const8 [c])) -> (Const16 [int64( int8(c))]) 80 (SignExt8to32 (Const8 [c])) -> (Const32 [int64( int8(c))]) 81 (SignExt8to64 (Const8 [c])) -> (Const64 [int64( int8(c))]) 82 (SignExt16to32 (Const16 [c])) -> (Const32 [int64( int16(c))]) 83 (SignExt16to64 (Const16 [c])) -> (Const64 [int64( int16(c))]) 84 (SignExt32to64 (Const32 [c])) -> (Const64 [int64( int32(c))]) 85 86 (Neg8 (Const8 [c])) -> (Const8 [int64( -int8(c))]) 87 (Neg16 (Const16 [c])) -> (Const16 [int64(-int16(c))]) 88 (Neg32 (Const32 [c])) -> (Const32 [int64(-int32(c))]) 89 (Neg64 (Const64 [c])) -> (Const64 [-c]) 90 (Neg32F (Const32F [c])) && i2f(c) != 0 -> (Const32F [f2i(-i2f(c))]) 91 (Neg64F (Const64F [c])) && i2f(c) != 0 -> (Const64F [f2i(-i2f(c))]) 92 93 (Add8 (Const8 [c]) (Const8 [d])) -> (Const8 [int64(int8(c+d))]) 94 (Add16 (Const16 [c]) (Const16 [d])) -> (Const16 [int64(int16(c+d))]) 95 (Add32 (Const32 [c]) (Const32 [d])) -> (Const32 [int64(int32(c+d))]) 96 (Add64 (Const64 [c]) (Const64 [d])) -> (Const64 [c+d]) 97 (Add32F (Const32F [c]) (Const32F [d])) -> 98 (Const32F [f2i(float64(i2f32(c) + i2f32(d)))]) // ensure we combine the operands with 32 bit precision 99 (Add64F (Const64F [c]) (Const64F [d])) -> (Const64F [f2i(i2f(c) + i2f(d))]) 100 (AddPtr <t> x (Const64 [c])) -> (OffPtr <t> x [c]) 101 (AddPtr <t> x (Const32 [c])) -> (OffPtr <t> x [c]) 102 103 (Sub8 (Const8 [c]) (Const8 [d])) -> (Const8 [int64(int8(c-d))]) 104 (Sub16 (Const16 [c]) (Const16 [d])) -> (Const16 [int64(int16(c-d))]) 105 (Sub32 (Const32 [c]) (Const32 [d])) -> (Const32 [int64(int32(c-d))]) 106 (Sub64 (Const64 [c]) (Const64 [d])) -> (Const64 [c-d]) 107 (Sub32F (Const32F [c]) (Const32F [d])) -> 108 (Const32F [f2i(float64(i2f32(c) - i2f32(d)))]) 109 (Sub64F (Const64F [c]) (Const64F [d])) -> (Const64F [f2i(i2f(c) - i2f(d))]) 110 111 (Mul8 (Const8 [c]) (Const8 [d])) -> (Const8 [int64(int8(c*d))]) 112 (Mul16 (Const16 [c]) (Const16 [d])) -> (Const16 [int64(int16(c*d))]) 113 (Mul32 (Const32 [c]) (Const32 [d])) -> (Const32 [int64(int32(c*d))]) 114 (Mul64 (Const64 [c]) (Const64 [d])) -> (Const64 [c*d]) 115 (Mul32F (Const32F [c]) (Const32F [d])) -> 116 (Const32F [f2i(float64(i2f32(c) * i2f32(d)))]) 117 (Mul64F (Const64F [c]) (Const64F [d])) -> (Const64F [f2i(i2f(c) * i2f(d))]) 118 119 (And8 (Const8 [c]) (Const8 [d])) -> (Const8 [int64(int8(c&d))]) 120 (And16 (Const16 [c]) (Const16 [d])) -> (Const16 [int64(int16(c&d))]) 121 (And32 (Const32 [c]) (Const32 [d])) -> (Const32 [int64(int32(c&d))]) 122 (And64 (Const64 [c]) (Const64 [d])) -> (Const64 [c&d]) 123 124 (Or8 (Const8 [c]) (Const8 [d])) -> (Const8 [int64(int8(c|d))]) 125 (Or16 (Const16 [c]) (Const16 [d])) -> (Const16 [int64(int16(c|d))]) 126 (Or32 (Const32 [c]) (Const32 [d])) -> (Const32 [int64(int32(c|d))]) 127 (Or64 (Const64 [c]) (Const64 [d])) -> (Const64 [c|d]) 128 129 (Xor8 (Const8 [c]) (Const8 [d])) -> (Const8 [int64(int8(c^d))]) 130 (Xor16 (Const16 [c]) (Const16 [d])) -> (Const16 [int64(int16(c^d))]) 131 (Xor32 (Const32 [c]) (Const32 [d])) -> (Const32 [int64(int32(c^d))]) 132 (Xor64 (Const64 [c]) (Const64 [d])) -> (Const64 [c^d]) 133 134 (Div8 (Const8 [c]) (Const8 [d])) && d != 0 -> (Const8 [int64(int8(c)/int8(d))]) 135 (Div16 (Const16 [c]) (Const16 [d])) && d != 0 -> (Const16 [int64(int16(c)/int16(d))]) 136 (Div32 (Const32 [c]) (Const32 [d])) && d != 0 -> (Const32 [int64(int32(c)/int32(d))]) 137 (Div64 (Const64 [c]) (Const64 [d])) && d != 0 -> (Const64 [c/d]) 138 (Div8u (Const8 [c]) (Const8 [d])) && d != 0 -> (Const8 [int64(int8(uint8(c)/uint8(d)))]) 139 (Div16u (Const16 [c]) (Const16 [d])) && d != 0 -> (Const16 [int64(int16(uint16(c)/uint16(d)))]) 140 (Div32u (Const32 [c]) (Const32 [d])) && d != 0 -> (Const32 [int64(int32(uint32(c)/uint32(d)))]) 141 (Div64u (Const64 [c]) (Const64 [d])) && d != 0 -> (Const64 [int64(uint64(c)/uint64(d))]) 142 (Div32F (Const32F [c]) (Const32F [d])) -> (Const32F [f2i(float64(i2f32(c) / i2f32(d)))]) 143 (Div64F (Const64F [c]) (Const64F [d])) -> (Const64F [f2i(i2f(c) / i2f(d))]) 144 145 // Convert x * 1 to x. 146 (Mul8 (Const8 [1]) x) -> x 147 (Mul16 (Const16 [1]) x) -> x 148 (Mul32 (Const32 [1]) x) -> x 149 (Mul64 (Const64 [1]) x) -> x 150 151 // Convert x * -1 to -x. 152 (Mul8 (Const8 [-1]) x) -> (Neg8 x) 153 (Mul16 (Const16 [-1]) x) -> (Neg16 x) 154 (Mul32 (Const32 [-1]) x) -> (Neg32 x) 155 (Mul64 (Const64 [-1]) x) -> (Neg64 x) 156 157 // Convert multiplication by a power of two to a shift. 158 (Mul8 <t> n (Const8 [c])) && isPowerOfTwo(c) -> (Lsh8x64 <t> n (Const64 <typ.UInt64> [log2(c)])) 159 (Mul16 <t> n (Const16 [c])) && isPowerOfTwo(c) -> (Lsh16x64 <t> n (Const64 <typ.UInt64> [log2(c)])) 160 (Mul32 <t> n (Const32 [c])) && isPowerOfTwo(c) -> (Lsh32x64 <t> n (Const64 <typ.UInt64> [log2(c)])) 161 (Mul64 <t> n (Const64 [c])) && isPowerOfTwo(c) -> (Lsh64x64 <t> n (Const64 <typ.UInt64> [log2(c)])) 162 (Mul8 <t> n (Const8 [c])) && t.IsSigned() && isPowerOfTwo(-c) -> (Neg8 (Lsh8x64 <t> n (Const64 <typ.UInt64> [log2(-c)]))) 163 (Mul16 <t> n (Const16 [c])) && t.IsSigned() && isPowerOfTwo(-c) -> (Neg16 (Lsh16x64 <t> n (Const64 <typ.UInt64> [log2(-c)]))) 164 (Mul32 <t> n (Const32 [c])) && t.IsSigned() && isPowerOfTwo(-c) -> (Neg32 (Lsh32x64 <t> n (Const64 <typ.UInt64> [log2(-c)]))) 165 (Mul64 <t> n (Const64 [c])) && t.IsSigned() && isPowerOfTwo(-c) -> (Neg64 (Lsh64x64 <t> n (Const64 <typ.UInt64> [log2(-c)]))) 166 167 (Mod8 (Const8 [c]) (Const8 [d])) && d != 0 -> (Const8 [int64(int8(c % d))]) 168 (Mod16 (Const16 [c]) (Const16 [d])) && d != 0 -> (Const16 [int64(int16(c % d))]) 169 (Mod32 (Const32 [c]) (Const32 [d])) && d != 0 -> (Const32 [int64(int32(c % d))]) 170 (Mod64 (Const64 [c]) (Const64 [d])) && d != 0 -> (Const64 [c % d]) 171 172 (Mod8u (Const8 [c]) (Const8 [d])) && d != 0 -> (Const8 [int64(uint8(c) % uint8(d))]) 173 (Mod16u (Const16 [c]) (Const16 [d])) && d != 0 -> (Const16 [int64(uint16(c) % uint16(d))]) 174 (Mod32u (Const32 [c]) (Const32 [d])) && d != 0 -> (Const32 [int64(uint32(c) % uint32(d))]) 175 (Mod64u (Const64 [c]) (Const64 [d])) && d != 0 -> (Const64 [int64(uint64(c) % uint64(d))]) 176 177 (Lsh64x64 (Const64 [c]) (Const64 [d])) -> (Const64 [c << uint64(d)]) 178 (Rsh64x64 (Const64 [c]) (Const64 [d])) -> (Const64 [c >> uint64(d)]) 179 (Rsh64Ux64 (Const64 [c]) (Const64 [d])) -> (Const64 [int64(uint64(c) >> uint64(d))]) 180 (Lsh32x64 (Const32 [c]) (Const64 [d])) -> (Const32 [int64(int32(c) << uint64(d))]) 181 (Rsh32x64 (Const32 [c]) (Const64 [d])) -> (Const32 [int64(int32(c) >> uint64(d))]) 182 (Rsh32Ux64 (Const32 [c]) (Const64 [d])) -> (Const32 [int64(int32(uint32(c) >> uint64(d)))]) 183 (Lsh16x64 (Const16 [c]) (Const64 [d])) -> (Const16 [int64(int16(c) << uint64(d))]) 184 (Rsh16x64 (Const16 [c]) (Const64 [d])) -> (Const16 [int64(int16(c) >> uint64(d))]) 185 (Rsh16Ux64 (Const16 [c]) (Const64 [d])) -> (Const16 [int64(int16(uint16(c) >> uint64(d)))]) 186 (Lsh8x64 (Const8 [c]) (Const64 [d])) -> (Const8 [int64(int8(c) << uint64(d))]) 187 (Rsh8x64 (Const8 [c]) (Const64 [d])) -> (Const8 [int64(int8(c) >> uint64(d))]) 188 (Rsh8Ux64 (Const8 [c]) (Const64 [d])) -> (Const8 [int64(int8(uint8(c) >> uint64(d)))]) 189 190 // Fold IsInBounds when the range of the index cannot exceed the limit. 191 (IsInBounds (ZeroExt8to32 _) (Const32 [c])) && (1 << 8) <= c -> (ConstBool [1]) 192 (IsInBounds (ZeroExt8to64 _) (Const64 [c])) && (1 << 8) <= c -> (ConstBool [1]) 193 (IsInBounds (ZeroExt16to32 _) (Const32 [c])) && (1 << 16) <= c -> (ConstBool [1]) 194 (IsInBounds (ZeroExt16to64 _) (Const64 [c])) && (1 << 16) <= c -> (ConstBool [1]) 195 (IsInBounds x x) -> (ConstBool [0]) 196 (IsInBounds (And8 (Const8 [c]) _) (Const8 [d])) && 0 <= c && c < d -> (ConstBool [1]) 197 (IsInBounds (ZeroExt8to16 (And8 (Const8 [c]) _)) (Const16 [d])) && 0 <= c && c < d -> (ConstBool [1]) 198 (IsInBounds (ZeroExt8to32 (And8 (Const8 [c]) _)) (Const32 [d])) && 0 <= c && c < d -> (ConstBool [1]) 199 (IsInBounds (ZeroExt8to64 (And8 (Const8 [c]) _)) (Const64 [d])) && 0 <= c && c < d -> (ConstBool [1]) 200 (IsInBounds (And16 (Const16 [c]) _) (Const16 [d])) && 0 <= c && c < d -> (ConstBool [1]) 201 (IsInBounds (ZeroExt16to32 (And16 (Const16 [c]) _)) (Const32 [d])) && 0 <= c && c < d -> (ConstBool [1]) 202 (IsInBounds (ZeroExt16to64 (And16 (Const16 [c]) _)) (Const64 [d])) && 0 <= c && c < d -> (ConstBool [1]) 203 (IsInBounds (And32 (Const32 [c]) _) (Const32 [d])) && 0 <= c && c < d -> (ConstBool [1]) 204 (IsInBounds (ZeroExt32to64 (And32 (Const32 [c]) _)) (Const64 [d])) && 0 <= c && c < d -> (ConstBool [1]) 205 (IsInBounds (And64 (Const64 [c]) _) (Const64 [d])) && 0 <= c && c < d -> (ConstBool [1]) 206 (IsInBounds (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(0 <= c && c < d)]) 207 (IsInBounds (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(0 <= c && c < d)]) 208 // (Mod64u x y) is always between 0 (inclusive) and y (exclusive). 209 (IsInBounds (Mod32u _ y) y) -> (ConstBool [1]) 210 (IsInBounds (Mod64u _ y) y) -> (ConstBool [1]) 211 // Right shifting a unsigned number limits its value. 212 (IsInBounds (ZeroExt8to64 (Rsh8Ux64 _ (Const64 [c]))) (Const64 [d])) && 0 < c && c < 8 && 1<<uint( 8-c)-1 < d -> (ConstBool [1]) 213 (IsInBounds (ZeroExt8to32 (Rsh8Ux64 _ (Const64 [c]))) (Const32 [d])) && 0 < c && c < 8 && 1<<uint( 8-c)-1 < d -> (ConstBool [1]) 214 (IsInBounds (ZeroExt8to16 (Rsh8Ux64 _ (Const64 [c]))) (Const16 [d])) && 0 < c && c < 8 && 1<<uint( 8-c)-1 < d -> (ConstBool [1]) 215 (IsInBounds (Rsh8Ux64 _ (Const64 [c])) (Const64 [d])) && 0 < c && c < 8 && 1<<uint( 8-c)-1 < d -> (ConstBool [1]) 216 (IsInBounds (ZeroExt16to64 (Rsh16Ux64 _ (Const64 [c]))) (Const64 [d])) && 0 < c && c < 16 && 1<<uint(16-c)-1 < d -> (ConstBool [1]) 217 (IsInBounds (ZeroExt16to32 (Rsh16Ux64 _ (Const64 [c]))) (Const64 [d])) && 0 < c && c < 16 && 1<<uint(16-c)-1 < d -> (ConstBool [1]) 218 (IsInBounds (Rsh16Ux64 _ (Const64 [c])) (Const64 [d])) && 0 < c && c < 16 && 1<<uint(16-c)-1 < d -> (ConstBool [1]) 219 (IsInBounds (ZeroExt32to64 (Rsh32Ux64 _ (Const64 [c]))) (Const64 [d])) && 0 < c && c < 32 && 1<<uint(32-c)-1 < d -> (ConstBool [1]) 220 (IsInBounds (Rsh32Ux64 _ (Const64 [c])) (Const64 [d])) && 0 < c && c < 32 && 1<<uint(32-c)-1 < d -> (ConstBool [1]) 221 (IsInBounds (Rsh64Ux64 _ (Const64 [c])) (Const64 [d])) && 0 < c && c < 64 && 1<<uint(64-c)-1 < d -> (ConstBool [1]) 222 223 (IsSliceInBounds x x) -> (ConstBool [1]) 224 (IsSliceInBounds (And32 (Const32 [c]) _) (Const32 [d])) && 0 <= c && c <= d -> (ConstBool [1]) 225 (IsSliceInBounds (And64 (Const64 [c]) _) (Const64 [d])) && 0 <= c && c <= d -> (ConstBool [1]) 226 (IsSliceInBounds (Const32 [0]) _) -> (ConstBool [1]) 227 (IsSliceInBounds (Const64 [0]) _) -> (ConstBool [1]) 228 (IsSliceInBounds (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(0 <= c && c <= d)]) 229 (IsSliceInBounds (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(0 <= c && c <= d)]) 230 (IsSliceInBounds (SliceLen x) (SliceCap x)) -> (ConstBool [1]) 231 232 (Eq64 x x) -> (ConstBool [1]) 233 (Eq32 x x) -> (ConstBool [1]) 234 (Eq16 x x) -> (ConstBool [1]) 235 (Eq8 x x) -> (ConstBool [1]) 236 (EqB (ConstBool [c]) (ConstBool [d])) -> (ConstBool [b2i(c == d)]) 237 (EqB (ConstBool [0]) x) -> (Not x) 238 (EqB (ConstBool [1]) x) -> x 239 240 (Neq64 x x) -> (ConstBool [0]) 241 (Neq32 x x) -> (ConstBool [0]) 242 (Neq16 x x) -> (ConstBool [0]) 243 (Neq8 x x) -> (ConstBool [0]) 244 (NeqB (ConstBool [c]) (ConstBool [d])) -> (ConstBool [b2i(c != d)]) 245 (NeqB (ConstBool [0]) x) -> x 246 (NeqB (ConstBool [1]) x) -> (Not x) 247 (NeqB (Not x) (Not y)) -> (NeqB x y) 248 249 (Eq64 (Const64 <t> [c]) (Add64 (Const64 <t> [d]) x)) -> (Eq64 (Const64 <t> [c-d]) x) 250 (Eq32 (Const32 <t> [c]) (Add32 (Const32 <t> [d]) x)) -> (Eq32 (Const32 <t> [int64(int32(c-d))]) x) 251 (Eq16 (Const16 <t> [c]) (Add16 (Const16 <t> [d]) x)) -> (Eq16 (Const16 <t> [int64(int16(c-d))]) x) 252 (Eq8 (Const8 <t> [c]) (Add8 (Const8 <t> [d]) x)) -> (Eq8 (Const8 <t> [int64(int8(c-d))]) x) 253 254 (Neq64 (Const64 <t> [c]) (Add64 (Const64 <t> [d]) x)) -> (Neq64 (Const64 <t> [c-d]) x) 255 (Neq32 (Const32 <t> [c]) (Add32 (Const32 <t> [d]) x)) -> (Neq32 (Const32 <t> [int64(int32(c-d))]) x) 256 (Neq16 (Const16 <t> [c]) (Add16 (Const16 <t> [d]) x)) -> (Neq16 (Const16 <t> [int64(int16(c-d))]) x) 257 (Neq8 (Const8 <t> [c]) (Add8 (Const8 <t> [d]) x)) -> (Neq8 (Const8 <t> [int64(int8(c-d))]) x) 258 259 // Canonicalize x-const to x+(-const) 260 (Sub64 x (Const64 <t> [c])) && x.Op != OpConst64 -> (Add64 (Const64 <t> [-c]) x) 261 (Sub32 x (Const32 <t> [c])) && x.Op != OpConst32 -> (Add32 (Const32 <t> [int64(int32(-c))]) x) 262 (Sub16 x (Const16 <t> [c])) && x.Op != OpConst16 -> (Add16 (Const16 <t> [int64(int16(-c))]) x) 263 (Sub8 x (Const8 <t> [c])) && x.Op != OpConst8 -> (Add8 (Const8 <t> [int64(int8(-c))]) x) 264 265 // fold negation into comparison operators 266 (Not (Eq64 x y)) -> (Neq64 x y) 267 (Not (Eq32 x y)) -> (Neq32 x y) 268 (Not (Eq16 x y)) -> (Neq16 x y) 269 (Not (Eq8 x y)) -> (Neq8 x y) 270 (Not (EqB x y)) -> (NeqB x y) 271 272 (Not (Neq64 x y)) -> (Eq64 x y) 273 (Not (Neq32 x y)) -> (Eq32 x y) 274 (Not (Neq16 x y)) -> (Eq16 x y) 275 (Not (Neq8 x y)) -> (Eq8 x y) 276 (Not (NeqB x y)) -> (EqB x y) 277 278 (Not (Greater64 x y)) -> (Leq64 x y) 279 (Not (Greater32 x y)) -> (Leq32 x y) 280 (Not (Greater16 x y)) -> (Leq16 x y) 281 (Not (Greater8 x y)) -> (Leq8 x y) 282 283 (Not (Greater64U x y)) -> (Leq64U x y) 284 (Not (Greater32U x y)) -> (Leq32U x y) 285 (Not (Greater16U x y)) -> (Leq16U x y) 286 (Not (Greater8U x y)) -> (Leq8U x y) 287 288 (Not (Geq64 x y)) -> (Less64 x y) 289 (Not (Geq32 x y)) -> (Less32 x y) 290 (Not (Geq16 x y)) -> (Less16 x y) 291 (Not (Geq8 x y)) -> (Less8 x y) 292 293 (Not (Geq64U x y)) -> (Less64U x y) 294 (Not (Geq32U x y)) -> (Less32U x y) 295 (Not (Geq16U x y)) -> (Less16U x y) 296 (Not (Geq8U x y)) -> (Less8U x y) 297 298 (Not (Less64 x y)) -> (Geq64 x y) 299 (Not (Less32 x y)) -> (Geq32 x y) 300 (Not (Less16 x y)) -> (Geq16 x y) 301 (Not (Less8 x y)) -> (Geq8 x y) 302 303 (Not (Less64U x y)) -> (Geq64U x y) 304 (Not (Less32U x y)) -> (Geq32U x y) 305 (Not (Less16U x y)) -> (Geq16U x y) 306 (Not (Less8U x y)) -> (Geq8U x y) 307 308 (Not (Leq64 x y)) -> (Greater64 x y) 309 (Not (Leq32 x y)) -> (Greater32 x y) 310 (Not (Leq16 x y)) -> (Greater16 x y) 311 (Not (Leq8 x y)) -> (Greater8 x y) 312 313 (Not (Leq64U x y)) -> (Greater64U x y) 314 (Not (Leq32U x y)) -> (Greater32U x y) 315 (Not (Leq16U x y)) -> (Greater16U x y) 316 (Not (Leq8U x y)) -> (Greater8U x y) 317 318 // Distribute multiplication c * (d+x) -> c*d + c*x. Useful for: 319 // a[i].b = ...; a[i+1].b = ... 320 (Mul64 (Const64 <t> [c]) (Add64 <t> (Const64 <t> [d]) x)) -> 321 (Add64 (Const64 <t> [c*d]) (Mul64 <t> (Const64 <t> [c]) x)) 322 (Mul32 (Const32 <t> [c]) (Add32 <t> (Const32 <t> [d]) x)) -> 323 (Add32 (Const32 <t> [int64(int32(c*d))]) (Mul32 <t> (Const32 <t> [c]) x)) 324 325 // Rewrite x*y + x*z to x*(y+z) 326 (Add64 <t> (Mul64 x y) (Mul64 x z)) -> (Mul64 x (Add64 <t> y z)) 327 (Add32 <t> (Mul32 x y) (Mul32 x z)) -> (Mul32 x (Add32 <t> y z)) 328 (Add16 <t> (Mul16 x y) (Mul16 x z)) -> (Mul16 x (Add16 <t> y z)) 329 (Add8 <t> (Mul8 x y) (Mul8 x z)) -> (Mul8 x (Add8 <t> y z)) 330 331 // rewrite shifts of 8/16/32 bit consts into 64 bit consts to reduce 332 // the number of the other rewrite rules for const shifts 333 (Lsh64x32 <t> x (Const32 [c])) -> (Lsh64x64 x (Const64 <t> [int64(uint32(c))])) 334 (Lsh64x16 <t> x (Const16 [c])) -> (Lsh64x64 x (Const64 <t> [int64(uint16(c))])) 335 (Lsh64x8 <t> x (Const8 [c])) -> (Lsh64x64 x (Const64 <t> [int64(uint8(c))])) 336 (Rsh64x32 <t> x (Const32 [c])) -> (Rsh64x64 x (Const64 <t> [int64(uint32(c))])) 337 (Rsh64x16 <t> x (Const16 [c])) -> (Rsh64x64 x (Const64 <t> [int64(uint16(c))])) 338 (Rsh64x8 <t> x (Const8 [c])) -> (Rsh64x64 x (Const64 <t> [int64(uint8(c))])) 339 (Rsh64Ux32 <t> x (Const32 [c])) -> (Rsh64Ux64 x (Const64 <t> [int64(uint32(c))])) 340 (Rsh64Ux16 <t> x (Const16 [c])) -> (Rsh64Ux64 x (Const64 <t> [int64(uint16(c))])) 341 (Rsh64Ux8 <t> x (Const8 [c])) -> (Rsh64Ux64 x (Const64 <t> [int64(uint8(c))])) 342 343 (Lsh32x32 <t> x (Const32 [c])) -> (Lsh32x64 x (Const64 <t> [int64(uint32(c))])) 344 (Lsh32x16 <t> x (Const16 [c])) -> (Lsh32x64 x (Const64 <t> [int64(uint16(c))])) 345 (Lsh32x8 <t> x (Const8 [c])) -> (Lsh32x64 x (Const64 <t> [int64(uint8(c))])) 346 (Rsh32x32 <t> x (Const32 [c])) -> (Rsh32x64 x (Const64 <t> [int64(uint32(c))])) 347 (Rsh32x16 <t> x (Const16 [c])) -> (Rsh32x64 x (Const64 <t> [int64(uint16(c))])) 348 (Rsh32x8 <t> x (Const8 [c])) -> (Rsh32x64 x (Const64 <t> [int64(uint8(c))])) 349 (Rsh32Ux32 <t> x (Const32 [c])) -> (Rsh32Ux64 x (Const64 <t> [int64(uint32(c))])) 350 (Rsh32Ux16 <t> x (Const16 [c])) -> (Rsh32Ux64 x (Const64 <t> [int64(uint16(c))])) 351 (Rsh32Ux8 <t> x (Const8 [c])) -> (Rsh32Ux64 x (Const64 <t> [int64(uint8(c))])) 352 353 (Lsh16x32 <t> x (Const32 [c])) -> (Lsh16x64 x (Const64 <t> [int64(uint32(c))])) 354 (Lsh16x16 <t> x (Const16 [c])) -> (Lsh16x64 x (Const64 <t> [int64(uint16(c))])) 355 (Lsh16x8 <t> x (Const8 [c])) -> (Lsh16x64 x (Const64 <t> [int64(uint8(c))])) 356 (Rsh16x32 <t> x (Const32 [c])) -> (Rsh16x64 x (Const64 <t> [int64(uint32(c))])) 357 (Rsh16x16 <t> x (Const16 [c])) -> (Rsh16x64 x (Const64 <t> [int64(uint16(c))])) 358 (Rsh16x8 <t> x (Const8 [c])) -> (Rsh16x64 x (Const64 <t> [int64(uint8(c))])) 359 (Rsh16Ux32 <t> x (Const32 [c])) -> (Rsh16Ux64 x (Const64 <t> [int64(uint32(c))])) 360 (Rsh16Ux16 <t> x (Const16 [c])) -> (Rsh16Ux64 x (Const64 <t> [int64(uint16(c))])) 361 (Rsh16Ux8 <t> x (Const8 [c])) -> (Rsh16Ux64 x (Const64 <t> [int64(uint8(c))])) 362 363 (Lsh8x32 <t> x (Const32 [c])) -> (Lsh8x64 x (Const64 <t> [int64(uint32(c))])) 364 (Lsh8x16 <t> x (Const16 [c])) -> (Lsh8x64 x (Const64 <t> [int64(uint16(c))])) 365 (Lsh8x8 <t> x (Const8 [c])) -> (Lsh8x64 x (Const64 <t> [int64(uint8(c))])) 366 (Rsh8x32 <t> x (Const32 [c])) -> (Rsh8x64 x (Const64 <t> [int64(uint32(c))])) 367 (Rsh8x16 <t> x (Const16 [c])) -> (Rsh8x64 x (Const64 <t> [int64(uint16(c))])) 368 (Rsh8x8 <t> x (Const8 [c])) -> (Rsh8x64 x (Const64 <t> [int64(uint8(c))])) 369 (Rsh8Ux32 <t> x (Const32 [c])) -> (Rsh8Ux64 x (Const64 <t> [int64(uint32(c))])) 370 (Rsh8Ux16 <t> x (Const16 [c])) -> (Rsh8Ux64 x (Const64 <t> [int64(uint16(c))])) 371 (Rsh8Ux8 <t> x (Const8 [c])) -> (Rsh8Ux64 x (Const64 <t> [int64(uint8(c))])) 372 373 // shifts by zero 374 (Lsh64x64 x (Const64 [0])) -> x 375 (Rsh64x64 x (Const64 [0])) -> x 376 (Rsh64Ux64 x (Const64 [0])) -> x 377 (Lsh32x64 x (Const64 [0])) -> x 378 (Rsh32x64 x (Const64 [0])) -> x 379 (Rsh32Ux64 x (Const64 [0])) -> x 380 (Lsh16x64 x (Const64 [0])) -> x 381 (Rsh16x64 x (Const64 [0])) -> x 382 (Rsh16Ux64 x (Const64 [0])) -> x 383 (Lsh8x64 x (Const64 [0])) -> x 384 (Rsh8x64 x (Const64 [0])) -> x 385 (Rsh8Ux64 x (Const64 [0])) -> x 386 387 // zero shifted. 388 (Lsh64x64 (Const64 [0]) _) -> (Const64 [0]) 389 (Lsh64x32 (Const64 [0]) _) -> (Const64 [0]) 390 (Lsh64x16 (Const64 [0]) _) -> (Const64 [0]) 391 (Lsh64x8 (Const64 [0]) _) -> (Const64 [0]) 392 (Rsh64x64 (Const64 [0]) _) -> (Const64 [0]) 393 (Rsh64x32 (Const64 [0]) _) -> (Const64 [0]) 394 (Rsh64x16 (Const64 [0]) _) -> (Const64 [0]) 395 (Rsh64x8 (Const64 [0]) _) -> (Const64 [0]) 396 (Rsh64Ux64 (Const64 [0]) _) -> (Const64 [0]) 397 (Rsh64Ux32 (Const64 [0]) _) -> (Const64 [0]) 398 (Rsh64Ux16 (Const64 [0]) _) -> (Const64 [0]) 399 (Rsh64Ux8 (Const64 [0]) _) -> (Const64 [0]) 400 (Lsh32x64 (Const32 [0]) _) -> (Const32 [0]) 401 (Lsh32x32 (Const32 [0]) _) -> (Const32 [0]) 402 (Lsh32x16 (Const32 [0]) _) -> (Const32 [0]) 403 (Lsh32x8 (Const32 [0]) _) -> (Const32 [0]) 404 (Rsh32x64 (Const32 [0]) _) -> (Const32 [0]) 405 (Rsh32x32 (Const32 [0]) _) -> (Const32 [0]) 406 (Rsh32x16 (Const32 [0]) _) -> (Const32 [0]) 407 (Rsh32x8 (Const32 [0]) _) -> (Const32 [0]) 408 (Rsh32Ux64 (Const32 [0]) _) -> (Const32 [0]) 409 (Rsh32Ux32 (Const32 [0]) _) -> (Const32 [0]) 410 (Rsh32Ux16 (Const32 [0]) _) -> (Const32 [0]) 411 (Rsh32Ux8 (Const32 [0]) _) -> (Const32 [0]) 412 (Lsh16x64 (Const16 [0]) _) -> (Const16 [0]) 413 (Lsh16x32 (Const16 [0]) _) -> (Const16 [0]) 414 (Lsh16x16 (Const16 [0]) _) -> (Const16 [0]) 415 (Lsh16x8 (Const16 [0]) _) -> (Const16 [0]) 416 (Rsh16x64 (Const16 [0]) _) -> (Const16 [0]) 417 (Rsh16x32 (Const16 [0]) _) -> (Const16 [0]) 418 (Rsh16x16 (Const16 [0]) _) -> (Const16 [0]) 419 (Rsh16x8 (Const16 [0]) _) -> (Const16 [0]) 420 (Rsh16Ux64 (Const16 [0]) _) -> (Const16 [0]) 421 (Rsh16Ux32 (Const16 [0]) _) -> (Const16 [0]) 422 (Rsh16Ux16 (Const16 [0]) _) -> (Const16 [0]) 423 (Rsh16Ux8 (Const16 [0]) _) -> (Const16 [0]) 424 (Lsh8x64 (Const8 [0]) _) -> (Const8 [0]) 425 (Lsh8x32 (Const8 [0]) _) -> (Const8 [0]) 426 (Lsh8x16 (Const8 [0]) _) -> (Const8 [0]) 427 (Lsh8x8 (Const8 [0]) _) -> (Const8 [0]) 428 (Rsh8x64 (Const8 [0]) _) -> (Const8 [0]) 429 (Rsh8x32 (Const8 [0]) _) -> (Const8 [0]) 430 (Rsh8x16 (Const8 [0]) _) -> (Const8 [0]) 431 (Rsh8x8 (Const8 [0]) _) -> (Const8 [0]) 432 (Rsh8Ux64 (Const8 [0]) _) -> (Const8 [0]) 433 (Rsh8Ux32 (Const8 [0]) _) -> (Const8 [0]) 434 (Rsh8Ux16 (Const8 [0]) _) -> (Const8 [0]) 435 (Rsh8Ux8 (Const8 [0]) _) -> (Const8 [0]) 436 437 // large left shifts of all values, and right shifts of unsigned values 438 (Lsh64x64 _ (Const64 [c])) && uint64(c) >= 64 -> (Const64 [0]) 439 (Rsh64Ux64 _ (Const64 [c])) && uint64(c) >= 64 -> (Const64 [0]) 440 (Lsh32x64 _ (Const64 [c])) && uint64(c) >= 32 -> (Const32 [0]) 441 (Rsh32Ux64 _ (Const64 [c])) && uint64(c) >= 32 -> (Const32 [0]) 442 (Lsh16x64 _ (Const64 [c])) && uint64(c) >= 16 -> (Const16 [0]) 443 (Rsh16Ux64 _ (Const64 [c])) && uint64(c) >= 16 -> (Const16 [0]) 444 (Lsh8x64 _ (Const64 [c])) && uint64(c) >= 8 -> (Const8 [0]) 445 (Rsh8Ux64 _ (Const64 [c])) && uint64(c) >= 8 -> (Const8 [0]) 446 447 // combine const shifts 448 (Lsh64x64 <t> (Lsh64x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Lsh64x64 x (Const64 <t> [c+d])) 449 (Lsh32x64 <t> (Lsh32x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Lsh32x64 x (Const64 <t> [c+d])) 450 (Lsh16x64 <t> (Lsh16x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Lsh16x64 x (Const64 <t> [c+d])) 451 (Lsh8x64 <t> (Lsh8x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Lsh8x64 x (Const64 <t> [c+d])) 452 453 (Rsh64x64 <t> (Rsh64x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh64x64 x (Const64 <t> [c+d])) 454 (Rsh32x64 <t> (Rsh32x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh32x64 x (Const64 <t> [c+d])) 455 (Rsh16x64 <t> (Rsh16x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh16x64 x (Const64 <t> [c+d])) 456 (Rsh8x64 <t> (Rsh8x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh8x64 x (Const64 <t> [c+d])) 457 458 (Rsh64Ux64 <t> (Rsh64Ux64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh64Ux64 x (Const64 <t> [c+d])) 459 (Rsh32Ux64 <t> (Rsh32Ux64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh32Ux64 x (Const64 <t> [c+d])) 460 (Rsh16Ux64 <t> (Rsh16Ux64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh16Ux64 x (Const64 <t> [c+d])) 461 (Rsh8Ux64 <t> (Rsh8Ux64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh8Ux64 x (Const64 <t> [c+d])) 462 463 // ((x >> c1) << c2) >> c3 464 (Rsh64Ux64 (Lsh64x64 (Rsh64Ux64 x (Const64 [c1])) (Const64 [c2])) (Const64 [c3])) 465 && uint64(c1) >= uint64(c2) && uint64(c3) >= uint64(c2) && !uaddOvf(c1-c2, c3) 466 -> (Rsh64Ux64 x (Const64 <typ.UInt64> [c1-c2+c3])) 467 (Rsh32Ux64 (Lsh32x64 (Rsh32Ux64 x (Const64 [c1])) (Const64 [c2])) (Const64 [c3])) 468 && uint64(c1) >= uint64(c2) && uint64(c3) >= uint64(c2) && !uaddOvf(c1-c2, c3) 469 -> (Rsh32Ux64 x (Const64 <typ.UInt64> [c1-c2+c3])) 470 (Rsh16Ux64 (Lsh16x64 (Rsh16Ux64 x (Const64 [c1])) (Const64 [c2])) (Const64 [c3])) 471 && uint64(c1) >= uint64(c2) && uint64(c3) >= uint64(c2) && !uaddOvf(c1-c2, c3) 472 -> (Rsh16Ux64 x (Const64 <typ.UInt64> [c1-c2+c3])) 473 (Rsh8Ux64 (Lsh8x64 (Rsh8Ux64 x (Const64 [c1])) (Const64 [c2])) (Const64 [c3])) 474 && uint64(c1) >= uint64(c2) && uint64(c3) >= uint64(c2) && !uaddOvf(c1-c2, c3) 475 -> (Rsh8Ux64 x (Const64 <typ.UInt64> [c1-c2+c3])) 476 477 // ((x << c1) >> c2) << c3 478 (Lsh64x64 (Rsh64Ux64 (Lsh64x64 x (Const64 [c1])) (Const64 [c2])) (Const64 [c3])) 479 && uint64(c1) >= uint64(c2) && uint64(c3) >= uint64(c2) && !uaddOvf(c1-c2, c3) 480 -> (Lsh64x64 x (Const64 <typ.UInt64> [c1-c2+c3])) 481 (Lsh32x64 (Rsh32Ux64 (Lsh32x64 x (Const64 [c1])) (Const64 [c2])) (Const64 [c3])) 482 && uint64(c1) >= uint64(c2) && uint64(c3) >= uint64(c2) && !uaddOvf(c1-c2, c3) 483 -> (Lsh32x64 x (Const64 <typ.UInt64> [c1-c2+c3])) 484 (Lsh16x64 (Rsh16Ux64 (Lsh16x64 x (Const64 [c1])) (Const64 [c2])) (Const64 [c3])) 485 && uint64(c1) >= uint64(c2) && uint64(c3) >= uint64(c2) && !uaddOvf(c1-c2, c3) 486 -> (Lsh16x64 x (Const64 <typ.UInt64> [c1-c2+c3])) 487 (Lsh8x64 (Rsh8Ux64 (Lsh8x64 x (Const64 [c1])) (Const64 [c2])) (Const64 [c3])) 488 && uint64(c1) >= uint64(c2) && uint64(c3) >= uint64(c2) && !uaddOvf(c1-c2, c3) 489 -> (Lsh8x64 x (Const64 <typ.UInt64> [c1-c2+c3])) 490 491 // replace shifts with zero extensions 492 (Rsh16Ux64 (Lsh16x64 x (Const64 [8])) (Const64 [8])) -> (ZeroExt8to16 (Trunc16to8 <typ.UInt8> x)) 493 (Rsh32Ux64 (Lsh32x64 x (Const64 [24])) (Const64 [24])) -> (ZeroExt8to32 (Trunc32to8 <typ.UInt8> x)) 494 (Rsh64Ux64 (Lsh64x64 x (Const64 [56])) (Const64 [56])) -> (ZeroExt8to64 (Trunc64to8 <typ.UInt8> x)) 495 (Rsh32Ux64 (Lsh32x64 x (Const64 [16])) (Const64 [16])) -> (ZeroExt16to32 (Trunc32to16 <typ.UInt16> x)) 496 (Rsh64Ux64 (Lsh64x64 x (Const64 [48])) (Const64 [48])) -> (ZeroExt16to64 (Trunc64to16 <typ.UInt16> x)) 497 (Rsh64Ux64 (Lsh64x64 x (Const64 [32])) (Const64 [32])) -> (ZeroExt32to64 (Trunc64to32 <typ.UInt32> x)) 498 499 // replace shifts with sign extensions 500 (Rsh16x64 (Lsh16x64 x (Const64 [8])) (Const64 [8])) -> (SignExt8to16 (Trunc16to8 <typ.Int8> x)) 501 (Rsh32x64 (Lsh32x64 x (Const64 [24])) (Const64 [24])) -> (SignExt8to32 (Trunc32to8 <typ.Int8> x)) 502 (Rsh64x64 (Lsh64x64 x (Const64 [56])) (Const64 [56])) -> (SignExt8to64 (Trunc64to8 <typ.Int8> x)) 503 (Rsh32x64 (Lsh32x64 x (Const64 [16])) (Const64 [16])) -> (SignExt16to32 (Trunc32to16 <typ.Int16> x)) 504 (Rsh64x64 (Lsh64x64 x (Const64 [48])) (Const64 [48])) -> (SignExt16to64 (Trunc64to16 <typ.Int16> x)) 505 (Rsh64x64 (Lsh64x64 x (Const64 [32])) (Const64 [32])) -> (SignExt32to64 (Trunc64to32 <typ.Int32> x)) 506 507 // constant comparisons 508 (Eq64 (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(c == d)]) 509 (Eq32 (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(c == d)]) 510 (Eq16 (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(c == d)]) 511 (Eq8 (Const8 [c]) (Const8 [d])) -> (ConstBool [b2i(c == d)]) 512 513 (Neq64 (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(c != d)]) 514 (Neq32 (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(c != d)]) 515 (Neq16 (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(c != d)]) 516 (Neq8 (Const8 [c]) (Const8 [d])) -> (ConstBool [b2i(c != d)]) 517 518 (Greater64 (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(c > d)]) 519 (Greater32 (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(c > d)]) 520 (Greater16 (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(c > d)]) 521 (Greater8 (Const8 [c]) (Const8 [d])) -> (ConstBool [b2i(c > d)]) 522 523 (Greater64U (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(uint64(c) > uint64(d))]) 524 (Greater32U (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(uint32(c) > uint32(d))]) 525 (Greater16U (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(uint16(c) > uint16(d))]) 526 (Greater8U (Const8 [c]) (Const8 [d])) -> (ConstBool [b2i(uint8(c) > uint8(d))]) 527 528 (Geq64 (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(c >= d)]) 529 (Geq32 (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(c >= d)]) 530 (Geq16 (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(c >= d)]) 531 (Geq8 (Const8 [c]) (Const8 [d])) -> (ConstBool [b2i(c >= d)]) 532 533 (Geq64U (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(uint64(c) >= uint64(d))]) 534 (Geq32U (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(uint32(c) >= uint32(d))]) 535 (Geq16U (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(uint16(c) >= uint16(d))]) 536 (Geq8U (Const8 [c]) (Const8 [d])) -> (ConstBool [b2i(uint8(c) >= uint8(d))]) 537 538 (Less64 (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(c < d)]) 539 (Less32 (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(c < d)]) 540 (Less16 (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(c < d)]) 541 (Less8 (Const8 [c]) (Const8 [d])) -> (ConstBool [b2i(c < d)]) 542 543 (Less64U (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(uint64(c) < uint64(d))]) 544 (Less32U (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(uint32(c) < uint32(d))]) 545 (Less16U (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(uint16(c) < uint16(d))]) 546 (Less8U (Const8 [c]) (Const8 [d])) -> (ConstBool [b2i(uint8(c) < uint8(d))]) 547 548 (Leq64 (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(c <= d)]) 549 (Leq32 (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(c <= d)]) 550 (Leq16 (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(c <= d)]) 551 (Leq8 (Const8 [c]) (Const8 [d])) -> (ConstBool [b2i(c <= d)]) 552 553 (Leq64U (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(uint64(c) <= uint64(d))]) 554 (Leq32U (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(uint32(c) <= uint32(d))]) 555 (Leq16U (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(uint16(c) <= uint16(d))]) 556 (Leq8U (Const8 [c]) (Const8 [d])) -> (ConstBool [b2i(uint8(c) <= uint8(d))]) 557 558 // simplifications 559 (Or64 x x) -> x 560 (Or32 x x) -> x 561 (Or16 x x) -> x 562 (Or8 x x) -> x 563 (Or64 (Const64 [0]) x) -> x 564 (Or32 (Const32 [0]) x) -> x 565 (Or16 (Const16 [0]) x) -> x 566 (Or8 (Const8 [0]) x) -> x 567 (Or64 (Const64 [-1]) _) -> (Const64 [-1]) 568 (Or32 (Const32 [-1]) _) -> (Const32 [-1]) 569 (Or16 (Const16 [-1]) _) -> (Const16 [-1]) 570 (Or8 (Const8 [-1]) _) -> (Const8 [-1]) 571 (And64 x x) -> x 572 (And32 x x) -> x 573 (And16 x x) -> x 574 (And8 x x) -> x 575 (And64 (Const64 [-1]) x) -> x 576 (And32 (Const32 [-1]) x) -> x 577 (And16 (Const16 [-1]) x) -> x 578 (And8 (Const8 [-1]) x) -> x 579 (And64 (Const64 [0]) _) -> (Const64 [0]) 580 (And32 (Const32 [0]) _) -> (Const32 [0]) 581 (And16 (Const16 [0]) _) -> (Const16 [0]) 582 (And8 (Const8 [0]) _) -> (Const8 [0]) 583 (Xor64 x x) -> (Const64 [0]) 584 (Xor32 x x) -> (Const32 [0]) 585 (Xor16 x x) -> (Const16 [0]) 586 (Xor8 x x) -> (Const8 [0]) 587 (Xor64 (Const64 [0]) x) -> x 588 (Xor32 (Const32 [0]) x) -> x 589 (Xor16 (Const16 [0]) x) -> x 590 (Xor8 (Const8 [0]) x) -> x 591 (Add64 (Const64 [0]) x) -> x 592 (Add32 (Const32 [0]) x) -> x 593 (Add16 (Const16 [0]) x) -> x 594 (Add8 (Const8 [0]) x) -> x 595 (Sub64 x x) -> (Const64 [0]) 596 (Sub32 x x) -> (Const32 [0]) 597 (Sub16 x x) -> (Const16 [0]) 598 (Sub8 x x) -> (Const8 [0]) 599 (Mul64 (Const64 [0]) _) -> (Const64 [0]) 600 (Mul32 (Const32 [0]) _) -> (Const32 [0]) 601 (Mul16 (Const16 [0]) _) -> (Const16 [0]) 602 (Mul8 (Const8 [0]) _) -> (Const8 [0]) 603 (Com8 (Com8 x)) -> x 604 (Com16 (Com16 x)) -> x 605 (Com32 (Com32 x)) -> x 606 (Com64 (Com64 x)) -> x 607 (Com8 (Const8 [c])) -> (Const8 [^c]) 608 (Com16 (Const16 [c])) -> (Const16 [^c]) 609 (Com32 (Const32 [c])) -> (Const32 [^c]) 610 (Com64 (Const64 [c])) -> (Const64 [^c]) 611 (Neg8 (Sub8 x y)) -> (Sub8 y x) 612 (Neg16 (Sub16 x y)) -> (Sub16 y x) 613 (Neg32 (Sub32 x y)) -> (Sub32 y x) 614 (Neg64 (Sub64 x y)) -> (Sub64 y x) 615 (Add8 (Const8 [1]) (Com8 x)) -> (Neg8 x) 616 (Add16 (Const16 [1]) (Com16 x)) -> (Neg16 x) 617 (Add32 (Const32 [1]) (Com32 x)) -> (Neg32 x) 618 (Add64 (Const64 [1]) (Com64 x)) -> (Neg64 x) 619 620 (And64 x (And64 x y)) -> (And64 x y) 621 (And32 x (And32 x y)) -> (And32 x y) 622 (And16 x (And16 x y)) -> (And16 x y) 623 (And8 x (And8 x y)) -> (And8 x y) 624 (Or64 x (Or64 x y)) -> (Or64 x y) 625 (Or32 x (Or32 x y)) -> (Or32 x y) 626 (Or16 x (Or16 x y)) -> (Or16 x y) 627 (Or8 x (Or8 x y)) -> (Or8 x y) 628 (Xor64 x (Xor64 x y)) -> y 629 (Xor32 x (Xor32 x y)) -> y 630 (Xor16 x (Xor16 x y)) -> y 631 (Xor8 x (Xor8 x y)) -> y 632 633 // Ands clear bits. Ors set bits. 634 // If a subsequent Or will set all the bits 635 // that an And cleared, we can skip the And. 636 // This happens in bitmasking code like: 637 // x &^= 3 << shift // clear two old bits 638 // x |= v << shift // set two new bits 639 // when shift is a small constant and v ends up a constant 3. 640 (Or8 (And8 x (Const8 [c2])) (Const8 <t> [c1])) && ^(c1 | c2) == 0 -> (Or8 (Const8 <t> [c1]) x) 641 (Or16 (And16 x (Const16 [c2])) (Const16 <t> [c1])) && ^(c1 | c2) == 0 -> (Or16 (Const16 <t> [c1]) x) 642 (Or32 (And32 x (Const32 [c2])) (Const32 <t> [c1])) && ^(c1 | c2) == 0 -> (Or32 (Const32 <t> [c1]) x) 643 (Or64 (And64 x (Const64 [c2])) (Const64 <t> [c1])) && ^(c1 | c2) == 0 -> (Or64 (Const64 <t> [c1]) x) 644 645 (Trunc64to8 (And64 (Const64 [y]) x)) && y&0xFF == 0xFF -> (Trunc64to8 x) 646 (Trunc64to16 (And64 (Const64 [y]) x)) && y&0xFFFF == 0xFFFF -> (Trunc64to16 x) 647 (Trunc64to32 (And64 (Const64 [y]) x)) && y&0xFFFFFFFF == 0xFFFFFFFF -> (Trunc64to32 x) 648 (Trunc32to8 (And32 (Const32 [y]) x)) && y&0xFF == 0xFF -> (Trunc32to8 x) 649 (Trunc32to16 (And32 (Const32 [y]) x)) && y&0xFFFF == 0xFFFF -> (Trunc32to16 x) 650 (Trunc16to8 (And16 (Const16 [y]) x)) && y&0xFF == 0xFF -> (Trunc16to8 x) 651 652 (ZeroExt8to64 (Trunc64to8 x:(Rsh64Ux64 _ (Const64 [s])))) && s >= 56 -> x 653 (ZeroExt16to64 (Trunc64to16 x:(Rsh64Ux64 _ (Const64 [s])))) && s >= 48 -> x 654 (ZeroExt32to64 (Trunc64to32 x:(Rsh64Ux64 _ (Const64 [s])))) && s >= 32 -> x 655 (ZeroExt8to32 (Trunc32to8 x:(Rsh32Ux64 _ (Const64 [s])))) && s >= 24 -> x 656 (ZeroExt16to32 (Trunc32to16 x:(Rsh32Ux64 _ (Const64 [s])))) && s >= 16 -> x 657 (ZeroExt8to16 (Trunc16to8 x:(Rsh16Ux64 _ (Const64 [s])))) && s >= 8 -> x 658 659 (SignExt8to64 (Trunc64to8 x:(Rsh64x64 _ (Const64 [s])))) && s >= 56 -> x 660 (SignExt16to64 (Trunc64to16 x:(Rsh64x64 _ (Const64 [s])))) && s >= 48 -> x 661 (SignExt32to64 (Trunc64to32 x:(Rsh64x64 _ (Const64 [s])))) && s >= 32 -> x 662 (SignExt8to32 (Trunc32to8 x:(Rsh32x64 _ (Const64 [s])))) && s >= 24 -> x 663 (SignExt16to32 (Trunc32to16 x:(Rsh32x64 _ (Const64 [s])))) && s >= 16 -> x 664 (SignExt8to16 (Trunc16to8 x:(Rsh16x64 _ (Const64 [s])))) && s >= 8 -> x 665 666 (Slicemask (Const32 [x])) && x > 0 -> (Const32 [-1]) 667 (Slicemask (Const32 [0])) -> (Const32 [0]) 668 (Slicemask (Const64 [x])) && x > 0 -> (Const64 [-1]) 669 (Slicemask (Const64 [0])) -> (Const64 [0]) 670 671 // Rewrite AND of consts as shifts if possible, slightly faster for 64 bit operands 672 // leading zeros can be shifted left, then right 673 (And64 <t> (Const64 [y]) x) && nlz(y) + nto(y) == 64 && nto(y) >= 32 674 -> (Rsh64Ux64 (Lsh64x64 <t> x (Const64 <t> [nlz(y)])) (Const64 <t> [nlz(y)])) 675 // trailing zeros can be shifted right, then left 676 (And64 <t> (Const64 [y]) x) && nlo(y) + ntz(y) == 64 && ntz(y) >= 32 677 -> (Lsh64x64 (Rsh64Ux64 <t> x (Const64 <t> [ntz(y)])) (Const64 <t> [ntz(y)])) 678 679 // simplifications often used for lengths. e.g. len(s[i:i+5])==5 680 (Sub64 (Add64 x y) x) -> y 681 (Sub64 (Add64 x y) y) -> x 682 (Sub32 (Add32 x y) x) -> y 683 (Sub32 (Add32 x y) y) -> x 684 (Sub16 (Add16 x y) x) -> y 685 (Sub16 (Add16 x y) y) -> x 686 (Sub8 (Add8 x y) x) -> y 687 (Sub8 (Add8 x y) y) -> x 688 689 // basic phi simplifications 690 (Phi (Const8 [c]) (Const8 [c])) -> (Const8 [c]) 691 (Phi (Const16 [c]) (Const16 [c])) -> (Const16 [c]) 692 (Phi (Const32 [c]) (Const32 [c])) -> (Const32 [c]) 693 (Phi (Const64 [c]) (Const64 [c])) -> (Const64 [c]) 694 695 // user nil checks 696 (NeqPtr p (ConstNil)) -> (IsNonNil p) 697 (EqPtr p (ConstNil)) -> (Not (IsNonNil p)) 698 (IsNonNil (ConstNil)) -> (ConstBool [0]) 699 700 // slice and interface comparisons 701 // The frontend ensures that we can only compare against nil, 702 // so we need only compare the first word (interface type or slice ptr). 703 (EqInter x y) -> (EqPtr (ITab x) (ITab y)) 704 (NeqInter x y) -> (NeqPtr (ITab x) (ITab y)) 705 (EqSlice x y) -> (EqPtr (SlicePtr x) (SlicePtr y)) 706 (NeqSlice x y) -> (NeqPtr (SlicePtr x) (SlicePtr y)) 707 708 // Load of store of same address, with compatibly typed value and same size 709 (Load <t1> p1 (Store {t2} p2 x _)) && isSamePtr(p1,p2) && t1.Compare(x.Type) == types.CMPeq && t1.Size() == t2.(*types.Type).Size() -> x 710 711 // Eliminate stores of values that have just been loaded from the same location. 712 // We also handle the common case where there are some intermediate stores to non-overlapping struct fields. 713 (Store {t1} p1 (Load <t2> p2 mem) mem) && 714 isSamePtr(p1, p2) && 715 t2.Size() == t1.(*types.Type).Size() -> mem 716 (Store {t1} (OffPtr [o1] p1) (Load <t2> (OffPtr [o1] p2) oldmem) mem:(Store {t3} (OffPtr [o3] p3) _ oldmem)) && 717 isSamePtr(p1, p2) && 718 isSamePtr(p1, p3) && 719 t2.Size() == t1.(*types.Type).Size() && 720 !overlap(o1, t2.Size(), o3, t3.(*types.Type).Size()) -> mem 721 (Store {t1} (OffPtr [o1] p1) (Load <t2> (OffPtr [o1] p2) oldmem) mem:(Store {t3} (OffPtr [o3] p3) _ (Store {t4} (OffPtr [o4] p4) _ oldmem))) && 722 isSamePtr(p1, p2) && 723 isSamePtr(p1, p3) && 724 isSamePtr(p1, p4) && 725 t2.Size() == t1.(*types.Type).Size() && 726 !overlap(o1, t2.Size(), o3, t3.(*types.Type).Size()) && 727 !overlap(o1, t2.Size(), o4, t4.(*types.Type).Size()) -> mem 728 (Store {t1} (OffPtr [o1] p1) (Load <t2> (OffPtr [o1] p2) oldmem) mem:(Store {t3} (OffPtr [o3] p3) _ (Store {t4} (OffPtr [o4] p4) _ (Store {t5} (OffPtr [o5] p5) _ oldmem)))) && 729 isSamePtr(p1, p2) && 730 isSamePtr(p1, p3) && 731 isSamePtr(p1, p4) && 732 isSamePtr(p1, p5) && 733 t2.Size() == t1.(*types.Type).Size() && 734 !overlap(o1, t2.Size(), o3, t3.(*types.Type).Size()) && 735 !overlap(o1, t2.Size(), o4, t4.(*types.Type).Size()) && 736 !overlap(o1, t2.Size(), o5, t5.(*types.Type).Size()) -> mem 737 738 // Collapse OffPtr 739 (OffPtr (OffPtr p [b]) [a]) -> (OffPtr p [a+b]) 740 (OffPtr p [0]) && v.Type.Compare(p.Type) == types.CMPeq -> p 741 742 // indexing operations 743 // Note: bounds check has already been done 744 (PtrIndex <t> ptr idx) && config.PtrSize == 4 -> (AddPtr ptr (Mul32 <typ.Int> idx (Const32 <typ.Int> [t.ElemType().Size()]))) 745 (PtrIndex <t> ptr idx) && config.PtrSize == 8 -> (AddPtr ptr (Mul64 <typ.Int> idx (Const64 <typ.Int> [t.ElemType().Size()]))) 746 747 // struct operations 748 (StructSelect (StructMake1 x)) -> x 749 (StructSelect [0] (StructMake2 x _)) -> x 750 (StructSelect [1] (StructMake2 _ x)) -> x 751 (StructSelect [0] (StructMake3 x _ _)) -> x 752 (StructSelect [1] (StructMake3 _ x _)) -> x 753 (StructSelect [2] (StructMake3 _ _ x)) -> x 754 (StructSelect [0] (StructMake4 x _ _ _)) -> x 755 (StructSelect [1] (StructMake4 _ x _ _)) -> x 756 (StructSelect [2] (StructMake4 _ _ x _)) -> x 757 (StructSelect [3] (StructMake4 _ _ _ x)) -> x 758 759 (Load <t> _ _) && t.IsStruct() && t.NumFields() == 0 && fe.CanSSA(t) -> 760 (StructMake0) 761 (Load <t> ptr mem) && t.IsStruct() && t.NumFields() == 1 && fe.CanSSA(t) -> 762 (StructMake1 763 (Load <t.FieldType(0)> (OffPtr <t.FieldType(0).PtrTo()> [0] ptr) mem)) 764 (Load <t> ptr mem) && t.IsStruct() && t.NumFields() == 2 && fe.CanSSA(t) -> 765 (StructMake2 766 (Load <t.FieldType(0)> (OffPtr <t.FieldType(0).PtrTo()> [0] ptr) mem) 767 (Load <t.FieldType(1)> (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] ptr) mem)) 768 (Load <t> ptr mem) && t.IsStruct() && t.NumFields() == 3 && fe.CanSSA(t) -> 769 (StructMake3 770 (Load <t.FieldType(0)> (OffPtr <t.FieldType(0).PtrTo()> [0] ptr) mem) 771 (Load <t.FieldType(1)> (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] ptr) mem) 772 (Load <t.FieldType(2)> (OffPtr <t.FieldType(2).PtrTo()> [t.FieldOff(2)] ptr) mem)) 773 (Load <t> ptr mem) && t.IsStruct() && t.NumFields() == 4 && fe.CanSSA(t) -> 774 (StructMake4 775 (Load <t.FieldType(0)> (OffPtr <t.FieldType(0).PtrTo()> [0] ptr) mem) 776 (Load <t.FieldType(1)> (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] ptr) mem) 777 (Load <t.FieldType(2)> (OffPtr <t.FieldType(2).PtrTo()> [t.FieldOff(2)] ptr) mem) 778 (Load <t.FieldType(3)> (OffPtr <t.FieldType(3).PtrTo()> [t.FieldOff(3)] ptr) mem)) 779 780 (StructSelect [i] x:(Load <t> ptr mem)) && !fe.CanSSA(t) -> 781 @x.Block (Load <v.Type> (OffPtr <v.Type.PtrTo()> [t.FieldOff(int(i))] ptr) mem) 782 783 (Store _ (StructMake0) mem) -> mem 784 (Store dst (StructMake1 <t> f0) mem) -> 785 (Store {t.FieldType(0)} (OffPtr <t.FieldType(0).PtrTo()> [0] dst) f0 mem) 786 (Store dst (StructMake2 <t> f0 f1) mem) -> 787 (Store {t.FieldType(1)} 788 (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] dst) 789 f1 790 (Store {t.FieldType(0)} 791 (OffPtr <t.FieldType(0).PtrTo()> [0] dst) 792 f0 mem)) 793 (Store dst (StructMake3 <t> f0 f1 f2) mem) -> 794 (Store {t.FieldType(2)} 795 (OffPtr <t.FieldType(2).PtrTo()> [t.FieldOff(2)] dst) 796 f2 797 (Store {t.FieldType(1)} 798 (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] dst) 799 f1 800 (Store {t.FieldType(0)} 801 (OffPtr <t.FieldType(0).PtrTo()> [0] dst) 802 f0 mem))) 803 (Store dst (StructMake4 <t> f0 f1 f2 f3) mem) -> 804 (Store {t.FieldType(3)} 805 (OffPtr <t.FieldType(3).PtrTo()> [t.FieldOff(3)] dst) 806 f3 807 (Store {t.FieldType(2)} 808 (OffPtr <t.FieldType(2).PtrTo()> [t.FieldOff(2)] dst) 809 f2 810 (Store {t.FieldType(1)} 811 (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] dst) 812 f1 813 (Store {t.FieldType(0)} 814 (OffPtr <t.FieldType(0).PtrTo()> [0] dst) 815 f0 mem)))) 816 817 // Putting struct{*byte} and similar into direct interfaces. 818 (IMake typ (StructMake1 val)) -> (IMake typ val) 819 (StructSelect [0] x:(IData _)) -> x 820 821 // un-SSAable values use mem->mem copies 822 (Store {t} dst (Load src mem) mem) && !fe.CanSSA(t.(*types.Type)) -> 823 (Move {t} [t.(*types.Type).Size()] dst src mem) 824 (Store {t} dst (Load src mem) (VarDef {x} mem)) && !fe.CanSSA(t.(*types.Type)) -> 825 (Move {t} [t.(*types.Type).Size()] dst src (VarDef {x} mem)) 826 827 // array ops 828 (ArraySelect (ArrayMake1 x)) -> x 829 830 (Load <t> _ _) && t.IsArray() && t.NumElem() == 0 -> 831 (ArrayMake0) 832 833 (Load <t> ptr mem) && t.IsArray() && t.NumElem() == 1 && fe.CanSSA(t) -> 834 (ArrayMake1 (Load <t.ElemType()> ptr mem)) 835 836 (Store _ (ArrayMake0) mem) -> mem 837 (Store dst (ArrayMake1 e) mem) -> (Store {e.Type} dst e mem) 838 839 (ArraySelect [0] (Load ptr mem)) -> (Load ptr mem) 840 841 // Putting [1]{*byte} and similar into direct interfaces. 842 (IMake typ (ArrayMake1 val)) -> (IMake typ val) 843 (ArraySelect [0] x:(IData _)) -> x 844 845 // string ops 846 // Decomposing StringMake and lowering of StringPtr and StringLen 847 // happens in a later pass, dec, so that these operations are available 848 // to other passes for optimizations. 849 (StringPtr (StringMake (Const64 <t> [c]) _)) -> (Const64 <t> [c]) 850 (StringLen (StringMake _ (Const64 <t> [c]))) -> (Const64 <t> [c]) 851 (ConstString {s}) && config.PtrSize == 4 && s.(string) == "" -> 852 (StringMake (ConstNil) (Const32 <typ.Int> [0])) 853 (ConstString {s}) && config.PtrSize == 8 && s.(string) == "" -> 854 (StringMake (ConstNil) (Const64 <typ.Int> [0])) 855 (ConstString {s}) && config.PtrSize == 4 && s.(string) != "" -> 856 (StringMake 857 (Addr <typ.BytePtr> {fe.StringData(s.(string))} 858 (SB)) 859 (Const32 <typ.Int> [int64(len(s.(string)))])) 860 (ConstString {s}) && config.PtrSize == 8 && s.(string) != "" -> 861 (StringMake 862 (Addr <typ.BytePtr> {fe.StringData(s.(string))} 863 (SB)) 864 (Const64 <typ.Int> [int64(len(s.(string)))])) 865 866 // slice ops 867 // Only a few slice rules are provided here. See dec.rules for 868 // a more comprehensive set. 869 (SliceLen (SliceMake _ (Const64 <t> [c]) _)) -> (Const64 <t> [c]) 870 (SliceCap (SliceMake _ _ (Const64 <t> [c]))) -> (Const64 <t> [c]) 871 (SliceLen (SliceMake _ (Const32 <t> [c]) _)) -> (Const32 <t> [c]) 872 (SliceCap (SliceMake _ _ (Const32 <t> [c]))) -> (Const32 <t> [c]) 873 (SlicePtr (SliceMake (SlicePtr x) _ _)) -> (SlicePtr x) 874 (SliceLen (SliceMake _ (SliceLen x) _)) -> (SliceLen x) 875 (SliceCap (SliceMake _ _ (SliceCap x))) -> (SliceCap x) 876 (SliceCap (SliceMake _ _ (SliceLen x))) -> (SliceLen x) 877 (ConstSlice) && config.PtrSize == 4 -> 878 (SliceMake 879 (ConstNil <v.Type.ElemType().PtrTo()>) 880 (Const32 <typ.Int> [0]) 881 (Const32 <typ.Int> [0])) 882 (ConstSlice) && config.PtrSize == 8 -> 883 (SliceMake 884 (ConstNil <v.Type.ElemType().PtrTo()>) 885 (Const64 <typ.Int> [0]) 886 (Const64 <typ.Int> [0])) 887 888 // interface ops 889 (ConstInterface) -> 890 (IMake 891 (ConstNil <typ.BytePtr>) 892 (ConstNil <typ.BytePtr>)) 893 894 (NilCheck (GetG mem) mem) -> mem 895 896 (If (Not cond) yes no) -> (If cond no yes) 897 (If (ConstBool [c]) yes no) && c == 1 -> (First nil yes no) 898 (If (ConstBool [c]) yes no) && c == 0 -> (First nil no yes) 899 900 // Get rid of Convert ops for pointer arithmetic on unsafe.Pointer. 901 (Convert (Add64 (Convert ptr mem) off) mem) -> (Add64 ptr off) 902 (Convert (Convert ptr mem) mem) -> ptr 903 904 // Decompose compound argument values 905 (Arg {n} [off]) && v.Type.IsString() -> 906 (StringMake 907 (Arg <typ.BytePtr> {n} [off]) 908 (Arg <typ.Int> {n} [off+config.PtrSize])) 909 910 (Arg {n} [off]) && v.Type.IsSlice() -> 911 (SliceMake 912 (Arg <v.Type.ElemType().PtrTo()> {n} [off]) 913 (Arg <typ.Int> {n} [off+config.PtrSize]) 914 (Arg <typ.Int> {n} [off+2*config.PtrSize])) 915 916 (Arg {n} [off]) && v.Type.IsInterface() -> 917 (IMake 918 (Arg <typ.BytePtr> {n} [off]) 919 (Arg <typ.BytePtr> {n} [off+config.PtrSize])) 920 921 (Arg {n} [off]) && v.Type.IsComplex() && v.Type.Size() == 16 -> 922 (ComplexMake 923 (Arg <typ.Float64> {n} [off]) 924 (Arg <typ.Float64> {n} [off+8])) 925 926 (Arg {n} [off]) && v.Type.IsComplex() && v.Type.Size() == 8 -> 927 (ComplexMake 928 (Arg <typ.Float32> {n} [off]) 929 (Arg <typ.Float32> {n} [off+4])) 930 931 (Arg <t>) && t.IsStruct() && t.NumFields() == 0 && fe.CanSSA(t) -> 932 (StructMake0) 933 (Arg <t> {n} [off]) && t.IsStruct() && t.NumFields() == 1 && fe.CanSSA(t) -> 934 (StructMake1 935 (Arg <t.FieldType(0)> {n} [off+t.FieldOff(0)])) 936 (Arg <t> {n} [off]) && t.IsStruct() && t.NumFields() == 2 && fe.CanSSA(t) -> 937 (StructMake2 938 (Arg <t.FieldType(0)> {n} [off+t.FieldOff(0)]) 939 (Arg <t.FieldType(1)> {n} [off+t.FieldOff(1)])) 940 (Arg <t> {n} [off]) && t.IsStruct() && t.NumFields() == 3 && fe.CanSSA(t) -> 941 (StructMake3 942 (Arg <t.FieldType(0)> {n} [off+t.FieldOff(0)]) 943 (Arg <t.FieldType(1)> {n} [off+t.FieldOff(1)]) 944 (Arg <t.FieldType(2)> {n} [off+t.FieldOff(2)])) 945 (Arg <t> {n} [off]) && t.IsStruct() && t.NumFields() == 4 && fe.CanSSA(t) -> 946 (StructMake4 947 (Arg <t.FieldType(0)> {n} [off+t.FieldOff(0)]) 948 (Arg <t.FieldType(1)> {n} [off+t.FieldOff(1)]) 949 (Arg <t.FieldType(2)> {n} [off+t.FieldOff(2)]) 950 (Arg <t.FieldType(3)> {n} [off+t.FieldOff(3)])) 951 952 (Arg <t>) && t.IsArray() && t.NumElem() == 0 -> 953 (ArrayMake0) 954 (Arg <t> {n} [off]) && t.IsArray() && t.NumElem() == 1 && fe.CanSSA(t) -> 955 (ArrayMake1 (Arg <t.ElemType()> {n} [off])) 956 957 // strength reduction of divide by a constant. 958 // See ../magic.go for a detailed description of these algorithms. 959 960 // Unsigned divide by power of 2. Strength reduce to a shift. 961 (Div8u n (Const8 [c])) && isPowerOfTwo(c&0xff) -> (Rsh8Ux64 n (Const64 <typ.UInt64> [log2(c&0xff)])) 962 (Div16u n (Const16 [c])) && isPowerOfTwo(c&0xffff) -> (Rsh16Ux64 n (Const64 <typ.UInt64> [log2(c&0xffff)])) 963 (Div32u n (Const32 [c])) && isPowerOfTwo(c&0xffffffff) -> (Rsh32Ux64 n (Const64 <typ.UInt64> [log2(c&0xffffffff)])) 964 (Div64u n (Const64 [c])) && isPowerOfTwo(c) -> (Rsh64Ux64 n (Const64 <typ.UInt64> [log2(c)])) 965 (Div64u n (Const64 [-1<<63])) -> (Rsh64Ux64 n (Const64 <typ.UInt64> [63])) 966 967 // Unsigned divide, not a power of 2. Strength reduce to a multiply. 968 // For 8-bit divides, we just do a direct 9-bit by 8-bit multiply. 969 (Div8u x (Const8 [c])) && umagicOK(8, c) -> 970 (Trunc32to8 971 (Rsh32Ux64 <typ.UInt32> 972 (Mul32 <typ.UInt32> 973 (Const32 <typ.UInt32> [int64(1<<8+umagic(8,c).m)]) 974 (ZeroExt8to32 x)) 975 (Const64 <typ.UInt64> [8+umagic(8,c).s]))) 976 977 // For 16-bit divides on 64-bit machines, we do a direct 17-bit by 16-bit multiply. 978 (Div16u x (Const16 [c])) && umagicOK(16, c) && config.RegSize == 8 -> 979 (Trunc64to16 980 (Rsh64Ux64 <typ.UInt64> 981 (Mul64 <typ.UInt64> 982 (Const64 <typ.UInt64> [int64(1<<16+umagic(16,c).m)]) 983 (ZeroExt16to64 x)) 984 (Const64 <typ.UInt64> [16+umagic(16,c).s]))) 985 986 // For 16-bit divides on 32-bit machines 987 (Div16u x (Const16 [c])) && umagicOK(16, c) && config.RegSize == 4 && umagic(16,c).m&1 == 0 -> 988 (Trunc32to16 989 (Rsh32Ux64 <typ.UInt32> 990 (Mul32 <typ.UInt32> 991 (Const32 <typ.UInt32> [int64(1<<15+umagic(16,c).m/2)]) 992 (ZeroExt16to32 x)) 993 (Const64 <typ.UInt64> [16+umagic(16,c).s-1]))) 994 (Div16u x (Const16 [c])) && umagicOK(16, c) && config.RegSize == 4 && c&1 == 0 -> 995 (Trunc32to16 996 (Rsh32Ux64 <typ.UInt32> 997 (Mul32 <typ.UInt32> 998 (Const32 <typ.UInt32> [int64(1<<15+(umagic(16,c).m+1)/2)]) 999 (Rsh32Ux64 <typ.UInt32> (ZeroExt16to32 x) (Const64 <typ.UInt64> [1]))) 1000 (Const64 <typ.UInt64> [16+umagic(16,c).s-2]))) 1001 (Div16u x (Const16 [c])) && umagicOK(16, c) && config.RegSize == 4 -> 1002 (Trunc32to16 1003 (Rsh32Ux64 <typ.UInt32> 1004 (Avg32u 1005 (Lsh32x64 <typ.UInt32> (ZeroExt16to32 x) (Const64 <typ.UInt64> [16])) 1006 (Mul32 <typ.UInt32> 1007 (Const32 <typ.UInt32> [int64(umagic(16,c).m)]) 1008 (ZeroExt16to32 x))) 1009 (Const64 <typ.UInt64> [16+umagic(16,c).s-1]))) 1010 1011 // For 32-bit divides on 32-bit machines 1012 (Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 4 && umagic(32,c).m&1 == 0 -> 1013 (Rsh32Ux64 <typ.UInt32> 1014 (Hmul32u <typ.UInt32> 1015 (Const32 <typ.UInt32> [int64(int32(1<<31+umagic(32,c).m/2))]) 1016 x) 1017 (Const64 <typ.UInt64> [umagic(32,c).s-1])) 1018 (Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 4 && c&1 == 0 -> 1019 (Rsh32Ux64 <typ.UInt32> 1020 (Hmul32u <typ.UInt32> 1021 (Const32 <typ.UInt32> [int64(int32(1<<31+(umagic(32,c).m+1)/2))]) 1022 (Rsh32Ux64 <typ.UInt32> x (Const64 <typ.UInt64> [1]))) 1023 (Const64 <typ.UInt64> [umagic(32,c).s-2])) 1024 (Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 4 -> 1025 (Rsh32Ux64 <typ.UInt32> 1026 (Avg32u 1027 x 1028 (Hmul32u <typ.UInt32> 1029 (Const32 <typ.UInt32> [int64(int32(umagic(32,c).m))]) 1030 x)) 1031 (Const64 <typ.UInt64> [umagic(32,c).s-1])) 1032 1033 // For 32-bit divides on 64-bit machines 1034 // We'll use a regular (non-hi) multiply for this case. 1035 (Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 8 && umagic(32,c).m&1 == 0 -> 1036 (Trunc64to32 1037 (Rsh64Ux64 <typ.UInt64> 1038 (Mul64 <typ.UInt64> 1039 (Const64 <typ.UInt64> [int64(1<<31+umagic(32,c).m/2)]) 1040 (ZeroExt32to64 x)) 1041 (Const64 <typ.UInt64> [32+umagic(32,c).s-1]))) 1042 (Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 8 && c&1 == 0 -> 1043 (Trunc64to32 1044 (Rsh64Ux64 <typ.UInt64> 1045 (Mul64 <typ.UInt64> 1046 (Const64 <typ.UInt64> [int64(1<<31+(umagic(32,c).m+1)/2)]) 1047 (Rsh64Ux64 <typ.UInt64> (ZeroExt32to64 x) (Const64 <typ.UInt64> [1]))) 1048 (Const64 <typ.UInt64> [32+umagic(32,c).s-2]))) 1049 (Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 8 -> 1050 (Trunc64to32 1051 (Rsh64Ux64 <typ.UInt64> 1052 (Avg64u 1053 (Lsh64x64 <typ.UInt64> (ZeroExt32to64 x) (Const64 <typ.UInt64> [32])) 1054 (Mul64 <typ.UInt64> 1055 (Const64 <typ.UInt32> [int64(umagic(32,c).m)]) 1056 (ZeroExt32to64 x))) 1057 (Const64 <typ.UInt64> [32+umagic(32,c).s-1]))) 1058 1059 // For 64-bit divides on 64-bit machines 1060 // (64-bit divides on 32-bit machines are lowered to a runtime call by the walk pass.) 1061 (Div64u x (Const64 [c])) && umagicOK(64, c) && config.RegSize == 8 && umagic(64,c).m&1 == 0 -> 1062 (Rsh64Ux64 <typ.UInt64> 1063 (Hmul64u <typ.UInt64> 1064 (Const64 <typ.UInt64> [int64(1<<63+umagic(64,c).m/2)]) 1065 x) 1066 (Const64 <typ.UInt64> [umagic(64,c).s-1])) 1067 (Div64u x (Const64 [c])) && umagicOK(64, c) && config.RegSize == 8 && c&1 == 0 -> 1068 (Rsh64Ux64 <typ.UInt64> 1069 (Hmul64u <typ.UInt64> 1070 (Const64 <typ.UInt64> [int64(1<<63+(umagic(64,c).m+1)/2)]) 1071 (Rsh64Ux64 <typ.UInt64> x (Const64 <typ.UInt64> [1]))) 1072 (Const64 <typ.UInt64> [umagic(64,c).s-2])) 1073 (Div64u x (Const64 [c])) && umagicOK(64, c) && config.RegSize == 8 -> 1074 (Rsh64Ux64 <typ.UInt64> 1075 (Avg64u 1076 x 1077 (Hmul64u <typ.UInt64> 1078 (Const64 <typ.UInt64> [int64(umagic(64,c).m)]) 1079 x)) 1080 (Const64 <typ.UInt64> [umagic(64,c).s-1])) 1081 1082 // Signed divide by a negative constant. Rewrite to divide by a positive constant. 1083 (Div8 <t> n (Const8 [c])) && c < 0 && c != -1<<7 -> (Neg8 (Div8 <t> n (Const8 <t> [-c]))) 1084 (Div16 <t> n (Const16 [c])) && c < 0 && c != -1<<15 -> (Neg16 (Div16 <t> n (Const16 <t> [-c]))) 1085 (Div32 <t> n (Const32 [c])) && c < 0 && c != -1<<31 -> (Neg32 (Div32 <t> n (Const32 <t> [-c]))) 1086 (Div64 <t> n (Const64 [c])) && c < 0 && c != -1<<63 -> (Neg64 (Div64 <t> n (Const64 <t> [-c]))) 1087 1088 // Dividing by the most-negative number. Result is always 0 except 1089 // if the input is also the most-negative number. 1090 // We can detect that using the sign bit of x & -x. 1091 (Div8 <t> x (Const8 [-1<<7 ])) -> (Rsh8Ux64 (And8 <t> x (Neg8 <t> x)) (Const64 <typ.UInt64> [7 ])) 1092 (Div16 <t> x (Const16 [-1<<15])) -> (Rsh16Ux64 (And16 <t> x (Neg16 <t> x)) (Const64 <typ.UInt64> [15])) 1093 (Div32 <t> x (Const32 [-1<<31])) -> (Rsh32Ux64 (And32 <t> x (Neg32 <t> x)) (Const64 <typ.UInt64> [31])) 1094 (Div64 <t> x (Const64 [-1<<63])) -> (Rsh64Ux64 (And64 <t> x (Neg64 <t> x)) (Const64 <typ.UInt64> [63])) 1095 1096 // Signed divide by power of 2. 1097 // n / c = n >> log(c) if n >= 0 1098 // = (n+c-1) >> log(c) if n < 0 1099 // We conditionally add c-1 by adding n>>63>>(64-log(c)) (first shift signed, second shift unsigned). 1100 (Div8 <t> n (Const8 [c])) && isPowerOfTwo(c) -> 1101 (Rsh8x64 1102 (Add8 <t> n (Rsh8Ux64 <t> (Rsh8x64 <t> n (Const64 <typ.UInt64> [ 7])) (Const64 <typ.UInt64> [ 8-log2(c)]))) 1103 (Const64 <typ.UInt64> [log2(c)])) 1104 (Div16 <t> n (Const16 [c])) && isPowerOfTwo(c) -> 1105 (Rsh16x64 1106 (Add16 <t> n (Rsh16Ux64 <t> (Rsh16x64 <t> n (Const64 <typ.UInt64> [15])) (Const64 <typ.UInt64> [16-log2(c)]))) 1107 (Const64 <typ.UInt64> [log2(c)])) 1108 (Div32 <t> n (Const32 [c])) && isPowerOfTwo(c) -> 1109 (Rsh32x64 1110 (Add32 <t> n (Rsh32Ux64 <t> (Rsh32x64 <t> n (Const64 <typ.UInt64> [31])) (Const64 <typ.UInt64> [32-log2(c)]))) 1111 (Const64 <typ.UInt64> [log2(c)])) 1112 (Div64 <t> n (Const64 [c])) && isPowerOfTwo(c) -> 1113 (Rsh64x64 1114 (Add64 <t> n (Rsh64Ux64 <t> (Rsh64x64 <t> n (Const64 <typ.UInt64> [63])) (Const64 <typ.UInt64> [64-log2(c)]))) 1115 (Const64 <typ.UInt64> [log2(c)])) 1116 1117 // Signed divide, not a power of 2. Strength reduce to a multiply. 1118 (Div8 <t> x (Const8 [c])) && smagicOK(8,c) -> 1119 (Sub8 <t> 1120 (Rsh32x64 <t> 1121 (Mul32 <typ.UInt32> 1122 (Const32 <typ.UInt32> [int64(smagic(8,c).m)]) 1123 (SignExt8to32 x)) 1124 (Const64 <typ.UInt64> [8+smagic(8,c).s])) 1125 (Rsh32x64 <t> 1126 (SignExt8to32 x) 1127 (Const64 <typ.UInt64> [31]))) 1128 (Div16 <t> x (Const16 [c])) && smagicOK(16,c) -> 1129 (Sub16 <t> 1130 (Rsh32x64 <t> 1131 (Mul32 <typ.UInt32> 1132 (Const32 <typ.UInt32> [int64(smagic(16,c).m)]) 1133 (SignExt16to32 x)) 1134 (Const64 <typ.UInt64> [16+smagic(16,c).s])) 1135 (Rsh32x64 <t> 1136 (SignExt16to32 x) 1137 (Const64 <typ.UInt64> [31]))) 1138 (Div32 <t> x (Const32 [c])) && smagicOK(32,c) && config.RegSize == 8 -> 1139 (Sub32 <t> 1140 (Rsh64x64 <t> 1141 (Mul64 <typ.UInt64> 1142 (Const64 <typ.UInt64> [int64(smagic(32,c).m)]) 1143 (SignExt32to64 x)) 1144 (Const64 <typ.UInt64> [32+smagic(32,c).s])) 1145 (Rsh64x64 <t> 1146 (SignExt32to64 x) 1147 (Const64 <typ.UInt64> [63]))) 1148 (Div32 <t> x (Const32 [c])) && smagicOK(32,c) && config.RegSize == 4 && smagic(32,c).m&1 == 0 -> 1149 (Sub32 <t> 1150 (Rsh32x64 <t> 1151 (Hmul32 <t> 1152 (Const32 <typ.UInt32> [int64(int32(smagic(32,c).m/2))]) 1153 x) 1154 (Const64 <typ.UInt64> [smagic(32,c).s-1])) 1155 (Rsh32x64 <t> 1156 x 1157 (Const64 <typ.UInt64> [31]))) 1158 (Div32 <t> x (Const32 [c])) && smagicOK(32,c) && config.RegSize == 4 && smagic(32,c).m&1 != 0 -> 1159 (Sub32 <t> 1160 (Rsh32x64 <t> 1161 (Add32 <t> 1162 (Hmul32 <t> 1163 (Const32 <typ.UInt32> [int64(int32(smagic(32,c).m))]) 1164 x) 1165 x) 1166 (Const64 <typ.UInt64> [smagic(32,c).s])) 1167 (Rsh32x64 <t> 1168 x 1169 (Const64 <typ.UInt64> [31]))) 1170 (Div64 <t> x (Const64 [c])) && smagicOK(64,c) && smagic(64,c).m&1 == 0 -> 1171 (Sub64 <t> 1172 (Rsh64x64 <t> 1173 (Hmul64 <t> 1174 (Const64 <typ.UInt64> [int64(smagic(64,c).m/2)]) 1175 x) 1176 (Const64 <typ.UInt64> [smagic(64,c).s-1])) 1177 (Rsh64x64 <t> 1178 x 1179 (Const64 <typ.UInt64> [63]))) 1180 (Div64 <t> x (Const64 [c])) && smagicOK(64,c) && smagic(64,c).m&1 != 0 -> 1181 (Sub64 <t> 1182 (Rsh64x64 <t> 1183 (Add64 <t> 1184 (Hmul64 <t> 1185 (Const64 <typ.UInt64> [int64(smagic(64,c).m)]) 1186 x) 1187 x) 1188 (Const64 <typ.UInt64> [smagic(64,c).s])) 1189 (Rsh64x64 <t> 1190 x 1191 (Const64 <typ.UInt64> [63]))) 1192 1193 // Unsigned mod by power of 2 constant. 1194 (Mod8u <t> n (Const8 [c])) && isPowerOfTwo(c&0xff) -> (And8 n (Const8 <t> [(c&0xff)-1])) 1195 (Mod16u <t> n (Const16 [c])) && isPowerOfTwo(c&0xffff) -> (And16 n (Const16 <t> [(c&0xffff)-1])) 1196 (Mod32u <t> n (Const32 [c])) && isPowerOfTwo(c&0xffffffff) -> (And32 n (Const32 <t> [(c&0xffffffff)-1])) 1197 (Mod64u <t> n (Const64 [c])) && isPowerOfTwo(c) -> (And64 n (Const64 <t> [c-1])) 1198 (Mod64u <t> n (Const64 [-1<<63])) -> (And64 n (Const64 <t> [1<<63-1])) 1199 1200 // Signed mod by negative constant. 1201 (Mod8 <t> n (Const8 [c])) && c < 0 && c != -1<<7 -> (Mod8 <t> n (Const8 <t> [-c])) 1202 (Mod16 <t> n (Const16 [c])) && c < 0 && c != -1<<15 -> (Mod16 <t> n (Const16 <t> [-c])) 1203 (Mod32 <t> n (Const32 [c])) && c < 0 && c != -1<<31 -> (Mod32 <t> n (Const32 <t> [-c])) 1204 (Mod64 <t> n (Const64 [c])) && c < 0 && c != -1<<63 -> (Mod64 <t> n (Const64 <t> [-c])) 1205 1206 // All other mods by constants, do A%B = A-(A/B*B). 1207 // This implements % with two * and a bunch of ancillary ops. 1208 // One of the * is free if the user's code also computes A/B. 1209 (Mod8 <t> x (Const8 [c])) && x.Op != OpConst8 && (c > 0 || c == -1<<7) 1210 -> (Sub8 x (Mul8 <t> (Div8 <t> x (Const8 <t> [c])) (Const8 <t> [c]))) 1211 (Mod16 <t> x (Const16 [c])) && x.Op != OpConst16 && (c > 0 || c == -1<<15) 1212 -> (Sub16 x (Mul16 <t> (Div16 <t> x (Const16 <t> [c])) (Const16 <t> [c]))) 1213 (Mod32 <t> x (Const32 [c])) && x.Op != OpConst32 && (c > 0 || c == -1<<31) 1214 -> (Sub32 x (Mul32 <t> (Div32 <t> x (Const32 <t> [c])) (Const32 <t> [c]))) 1215 (Mod64 <t> x (Const64 [c])) && x.Op != OpConst64 && (c > 0 || c == -1<<63) 1216 -> (Sub64 x (Mul64 <t> (Div64 <t> x (Const64 <t> [c])) (Const64 <t> [c]))) 1217 (Mod8u <t> x (Const8 [c])) && x.Op != OpConst8 && c > 0 && umagicOK(8 ,c) 1218 -> (Sub8 x (Mul8 <t> (Div8u <t> x (Const8 <t> [c])) (Const8 <t> [c]))) 1219 (Mod16u <t> x (Const16 [c])) && x.Op != OpConst16 && c > 0 && umagicOK(16,c) 1220 -> (Sub16 x (Mul16 <t> (Div16u <t> x (Const16 <t> [c])) (Const16 <t> [c]))) 1221 (Mod32u <t> x (Const32 [c])) && x.Op != OpConst32 && c > 0 && umagicOK(32,c) 1222 -> (Sub32 x (Mul32 <t> (Div32u <t> x (Const32 <t> [c])) (Const32 <t> [c]))) 1223 (Mod64u <t> x (Const64 [c])) && x.Op != OpConst64 && c > 0 && umagicOK(64,c) 1224 -> (Sub64 x (Mul64 <t> (Div64u <t> x (Const64 <t> [c])) (Const64 <t> [c]))) 1225 1226 // Reassociate expressions involving 1227 // constants such that constants come first, 1228 // exposing obvious constant-folding opportunities. 1229 // Reassociate (op (op y C) x) to (op C (op x y)) or similar, where C 1230 // is constant, which pushes constants to the outside 1231 // of the expression. At that point, any constant-folding 1232 // opportunities should be obvious. 1233 1234 // x + (C + z) -> C + (x + z) 1235 (Add64 (Add64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Add64 i (Add64 <t> z x)) 1236 (Add32 (Add32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Add32 i (Add32 <t> z x)) 1237 (Add16 (Add16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Add16 i (Add16 <t> z x)) 1238 (Add8 (Add8 i:(Const8 <t>) z) x) && (z.Op != OpConst8 && x.Op != OpConst8) -> (Add8 i (Add8 <t> z x)) 1239 1240 // x + (C - z) -> C + (x - z) 1241 (Add64 (Sub64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Add64 i (Sub64 <t> x z)) 1242 (Add32 (Sub32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Add32 i (Sub32 <t> x z)) 1243 (Add16 (Sub16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Add16 i (Sub16 <t> x z)) 1244 (Add8 (Sub8 i:(Const8 <t>) z) x) && (z.Op != OpConst8 && x.Op != OpConst8) -> (Add8 i (Sub8 <t> x z)) 1245 (Add64 x (Sub64 i:(Const64 <t>) z)) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Add64 i (Sub64 <t> x z)) 1246 (Add32 x (Sub32 i:(Const32 <t>) z)) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Add32 i (Sub32 <t> x z)) 1247 (Add16 x (Sub16 i:(Const16 <t>) z)) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Add16 i (Sub16 <t> x z)) 1248 (Add8 x (Sub8 i:(Const8 <t>) z)) && (z.Op != OpConst8 && x.Op != OpConst8) -> (Add8 i (Sub8 <t> x z)) 1249 1250 // x + (z - C) -> (x + z) - C 1251 (Add64 (Sub64 z i:(Const64 <t>)) x) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Sub64 (Add64 <t> x z) i) 1252 (Add32 (Sub32 z i:(Const32 <t>)) x) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Sub32 (Add32 <t> x z) i) 1253 (Add16 (Sub16 z i:(Const16 <t>)) x) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Sub16 (Add16 <t> x z) i) 1254 (Add8 (Sub8 z i:(Const8 <t>)) x) && (z.Op != OpConst8 && x.Op != OpConst8) -> (Sub8 (Add8 <t> x z) i) 1255 (Add64 x (Sub64 z i:(Const64 <t>))) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Sub64 (Add64 <t> x z) i) 1256 (Add32 x (Sub32 z i:(Const32 <t>))) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Sub32 (Add32 <t> x z) i) 1257 (Add16 x (Sub16 z i:(Const16 <t>))) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Sub16 (Add16 <t> x z) i) 1258 (Add8 x (Sub8 z i:(Const8 <t>))) && (z.Op != OpConst8 && x.Op != OpConst8) -> (Sub8 (Add8 <t> x z) i) 1259 1260 // x - (C - z) -> x + (z - C) -> (x + z) - C 1261 (Sub64 x (Sub64 i:(Const64 <t>) z)) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Sub64 (Add64 <t> x z) i) 1262 (Sub32 x (Sub32 i:(Const32 <t>) z)) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Sub32 (Add32 <t> x z) i) 1263 (Sub16 x (Sub16 i:(Const16 <t>) z)) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Sub16 (Add16 <t> x z) i) 1264 (Sub8 x (Sub8 i:(Const8 <t>) z)) && (z.Op != OpConst8 && x.Op != OpConst8) -> (Sub8 (Add8 <t> x z) i) 1265 1266 // x - (z - C) -> x + (C - z) -> (x - z) + C 1267 (Sub64 x (Sub64 z i:(Const64 <t>))) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Add64 i (Sub64 <t> x z)) 1268 (Sub32 x (Sub32 z i:(Const32 <t>))) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Add32 i (Sub32 <t> x z)) 1269 (Sub16 x (Sub16 z i:(Const16 <t>))) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Add16 i (Sub16 <t> x z)) 1270 (Sub8 x (Sub8 z i:(Const8 <t>))) && (z.Op != OpConst8 && x.Op != OpConst8) -> (Add8 i (Sub8 <t> x z)) 1271 1272 // x & (C & z) -> C & (x & z) 1273 (And64 (And64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) -> (And64 i (And64 <t> z x)) 1274 (And32 (And32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) -> (And32 i (And32 <t> z x)) 1275 (And16 (And16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) -> (And16 i (And16 <t> z x)) 1276 (And8 (And8 i:(Const8 <t>) z) x) && (z.Op != OpConst8 && x.Op != OpConst8) -> (And8 i (And8 <t> z x)) 1277 1278 // x | (C | z) -> C | (x | z) 1279 (Or64 (Or64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Or64 i (Or64 <t> z x)) 1280 (Or32 (Or32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Or32 i (Or32 <t> z x)) 1281 (Or16 (Or16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Or16 i (Or16 <t> z x)) 1282 (Or8 (Or8 i:(Const8 <t>) z) x) && (z.Op != OpConst8 && x.Op != OpConst8) -> (Or8 i (Or8 <t> z x)) 1283 1284 // x ^ (C ^ z) -> C ^ (x ^ z) 1285 (Xor64 (Xor64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Xor64 i (Xor64 <t> z x)) 1286 (Xor32 (Xor32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Xor32 i (Xor32 <t> z x)) 1287 (Xor16 (Xor16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Xor16 i (Xor16 <t> z x)) 1288 (Xor8 (Xor8 i:(Const8 <t>) z) x) && (z.Op != OpConst8 && x.Op != OpConst8) -> (Xor8 i (Xor8 <t> z x)) 1289 1290 // C + (D + x) -> (C + D) + x 1291 (Add64 (Const64 <t> [c]) (Add64 (Const64 <t> [d]) x)) -> (Add64 (Const64 <t> [c+d]) x) 1292 (Add32 (Const32 <t> [c]) (Add32 (Const32 <t> [d]) x)) -> (Add32 (Const32 <t> [int64(int32(c+d))]) x) 1293 (Add16 (Const16 <t> [c]) (Add16 (Const16 <t> [d]) x)) -> (Add16 (Const16 <t> [int64(int16(c+d))]) x) 1294 (Add8 (Const8 <t> [c]) (Add8 (Const8 <t> [d]) x)) -> (Add8 (Const8 <t> [int64(int8(c+d))]) x) 1295 1296 // C + (D - x) -> (C + D) - x 1297 (Add64 (Const64 <t> [c]) (Sub64 (Const64 <t> [d]) x)) -> (Sub64 (Const64 <t> [c+d]) x) 1298 (Add32 (Const32 <t> [c]) (Sub32 (Const32 <t> [d]) x)) -> (Sub32 (Const32 <t> [int64(int32(c+d))]) x) 1299 (Add16 (Const16 <t> [c]) (Sub16 (Const16 <t> [d]) x)) -> (Sub16 (Const16 <t> [int64(int16(c+d))]) x) 1300 (Add8 (Const8 <t> [c]) (Sub8 (Const8 <t> [d]) x)) -> (Sub8 (Const8 <t> [int64(int8(c+d))]) x) 1301 1302 // C + (x - D) -> (C - D) + x 1303 (Add64 (Const64 <t> [c]) (Sub64 x (Const64 <t> [d]))) -> (Add64 (Const64 <t> [c-d]) x) 1304 (Add32 (Const32 <t> [c]) (Sub32 x (Const32 <t> [d]))) -> (Add32 (Const32 <t> [int64(int32(c-d))]) x) 1305 (Add16 (Const16 <t> [c]) (Sub16 x (Const16 <t> [d]))) -> (Add16 (Const16 <t> [int64(int16(c-d))]) x) 1306 (Add8 (Const8 <t> [c]) (Sub8 x (Const8 <t> [d]))) -> (Add8 (Const8 <t> [int64(int8(c-d))]) x) 1307 1308 // C - (x - D) -> (C + D) - x 1309 (Sub64 (Const64 <t> [c]) (Sub64 x (Const64 <t> [d]))) -> (Sub64 (Const64 <t> [c+d]) x) 1310 (Sub32 (Const32 <t> [c]) (Sub32 x (Const32 <t> [d]))) -> (Sub32 (Const32 <t> [int64(int32(c+d))]) x) 1311 (Sub16 (Const16 <t> [c]) (Sub16 x (Const16 <t> [d]))) -> (Sub16 (Const16 <t> [int64(int16(c+d))]) x) 1312 (Sub8 (Const8 <t> [c]) (Sub8 x (Const8 <t> [d]))) -> (Sub8 (Const8 <t> [int64(int8(c+d))]) x) 1313 1314 // C - (D - x) -> (C - D) + x 1315 (Sub64 (Const64 <t> [c]) (Sub64 (Const64 <t> [d]) x)) -> (Add64 (Const64 <t> [c-d]) x) 1316 (Sub32 (Const32 <t> [c]) (Sub32 (Const32 <t> [d]) x)) -> (Add32 (Const32 <t> [int64(int32(c-d))]) x) 1317 (Sub16 (Const16 <t> [c]) (Sub16 (Const16 <t> [d]) x)) -> (Add16 (Const16 <t> [int64(int16(c-d))]) x) 1318 (Sub8 (Const8 <t> [c]) (Sub8 (Const8 <t> [d]) x)) -> (Add8 (Const8 <t> [int64(int8(c-d))]) x) 1319 1320 // C & (D & x) -> (C & D) & x 1321 (And64 (Const64 <t> [c]) (And64 (Const64 <t> [d]) x)) -> (And64 (Const64 <t> [c&d]) x) 1322 (And32 (Const32 <t> [c]) (And32 (Const32 <t> [d]) x)) -> (And32 (Const32 <t> [int64(int32(c&d))]) x) 1323 (And16 (Const16 <t> [c]) (And16 (Const16 <t> [d]) x)) -> (And16 (Const16 <t> [int64(int16(c&d))]) x) 1324 (And8 (Const8 <t> [c]) (And8 (Const8 <t> [d]) x)) -> (And8 (Const8 <t> [int64(int8(c&d))]) x) 1325 1326 // C | (D | x) -> (C | D) | x 1327 (Or64 (Const64 <t> [c]) (Or64 (Const64 <t> [d]) x)) -> (Or64 (Const64 <t> [c|d]) x) 1328 (Or32 (Const32 <t> [c]) (Or32 (Const32 <t> [d]) x)) -> (Or32 (Const32 <t> [int64(int32(c|d))]) x) 1329 (Or16 (Const16 <t> [c]) (Or16 (Const16 <t> [d]) x)) -> (Or16 (Const16 <t> [int64(int16(c|d))]) x) 1330 (Or8 (Const8 <t> [c]) (Or8 (Const8 <t> [d]) x)) -> (Or8 (Const8 <t> [int64(int8(c|d))]) x) 1331 1332 // C ^ (D ^ x) -> (C ^ D) ^ x 1333 (Xor64 (Const64 <t> [c]) (Xor64 (Const64 <t> [d]) x)) -> (Xor64 (Const64 <t> [c^d]) x) 1334 (Xor32 (Const32 <t> [c]) (Xor32 (Const32 <t> [d]) x)) -> (Xor32 (Const32 <t> [int64(int32(c^d))]) x) 1335 (Xor16 (Const16 <t> [c]) (Xor16 (Const16 <t> [d]) x)) -> (Xor16 (Const16 <t> [int64(int16(c^d))]) x) 1336 (Xor8 (Const8 <t> [c]) (Xor8 (Const8 <t> [d]) x)) -> (Xor8 (Const8 <t> [int64(int8(c^d))]) x) 1337 1338 // C * (D * x) = (C * D) * x 1339 (Mul64 (Const64 <t> [c]) (Mul64 (Const64 <t> [d]) x)) -> (Mul64 (Const64 <t> [c*d]) x) 1340 (Mul32 (Const32 <t> [c]) (Mul32 (Const32 <t> [d]) x)) -> (Mul32 (Const32 <t> [int64(int32(c*d))]) x) 1341 (Mul16 (Const16 <t> [c]) (Mul16 (Const16 <t> [d]) x)) -> (Mul16 (Const16 <t> [int64(int16(c*d))]) x) 1342 (Mul8 (Const8 <t> [c]) (Mul8 (Const8 <t> [d]) x)) -> (Mul8 (Const8 <t> [int64(int8(c*d))]) x) 1343 1344 // floating point optimizations 1345 (Add32F x (Const32F [0])) -> x 1346 (Add64F x (Const64F [0])) -> x 1347 (Sub32F x (Const32F [0])) -> x 1348 (Sub64F x (Const64F [0])) -> x 1349 (Mul32F x (Const32F [f2i(1)])) -> x 1350 (Mul64F x (Const64F [f2i(1)])) -> x 1351 (Mul32F x (Const32F [f2i(-1)])) -> (Neg32F x) 1352 (Mul64F x (Const64F [f2i(-1)])) -> (Neg64F x) 1353 (Mul32F x (Const32F [f2i(2)])) -> (Add32F x x) 1354 (Mul64F x (Const64F [f2i(2)])) -> (Add64F x x) 1355 (Div32F x (Const32F <t> [c])) && reciprocalExact32(float32(i2f(c))) -> (Mul32F x (Const32F <t> [f2i(1/i2f(c))])) 1356 (Div64F x (Const64F <t> [c])) && reciprocalExact64(i2f(c)) -> (Mul64F x (Const64F <t> [f2i(1/i2f(c))])) 1357 1358 (Sqrt (Const64F [c])) -> (Const64F [f2i(math.Sqrt(i2f(c)))]) 1359 1360 // recognize runtime.newobject and don't Zero/Nilcheck it 1361 (Zero (Load (OffPtr [c] (SP)) mem) mem) 1362 && mem.Op == OpStaticCall 1363 && isSameSym(mem.Aux, "runtime.newobject") 1364 && c == config.ctxt.FixedFrameSize() + config.RegSize // offset of return value 1365 -> mem 1366 (Store (Load (OffPtr [c] (SP)) mem) x mem) 1367 && isConstZero(x) 1368 && mem.Op == OpStaticCall 1369 && isSameSym(mem.Aux, "runtime.newobject") 1370 && c == config.ctxt.FixedFrameSize() + config.RegSize // offset of return value 1371 -> mem 1372 (Store (OffPtr (Load (OffPtr [c] (SP)) mem)) x mem) 1373 && isConstZero(x) 1374 && mem.Op == OpStaticCall 1375 && isSameSym(mem.Aux, "runtime.newobject") 1376 && c == config.ctxt.FixedFrameSize() + config.RegSize // offset of return value 1377 -> mem 1378 // nil checks just need to rewrite to something useless. 1379 // they will be deadcode eliminated soon afterwards. 1380 (NilCheck (Load (OffPtr [c] (SP)) (StaticCall {sym} _)) _) 1381 && isSameSym(sym, "runtime.newobject") 1382 && c == config.ctxt.FixedFrameSize() + config.RegSize // offset of return value 1383 && warnRule(fe.Debug_checknil() && v.Pos.Line() > 1, v, "removed nil check") 1384 -> (Invalid) 1385 (NilCheck (OffPtr (Load (OffPtr [c] (SP)) (StaticCall {sym} _))) _) 1386 && isSameSym(sym, "runtime.newobject") 1387 && c == config.ctxt.FixedFrameSize() + config.RegSize // offset of return value 1388 && warnRule(fe.Debug_checknil() && v.Pos.Line() > 1, v, "removed nil check") 1389 -> (Invalid) 1390 1391 // Address comparison shows up in type assertions. 1392 (EqPtr x x) -> (ConstBool [1]) 1393 (EqPtr (Addr {a} x) (Addr {b} x)) -> (ConstBool [b2i(a == b)]) 1394 1395 // De-virtualize interface calls into static calls. 1396 // Note that (ITab (IMake)) doesn't get 1397 // rewritten until after the first opt pass, 1398 // so this rule should trigger reliably. 1399 (InterCall [argsize] (Load (OffPtr [off] (ITab (IMake (Addr {itab} (SB)) _))) _) mem) && devirt(v, itab, off) != nil -> 1400 (StaticCall [argsize] {devirt(v, itab, off)} mem)