github.com/hlts2/go@v0.0.0-20170904000733-812b34efaed8/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 x*y - x*z to x*(y-z) 332 (Sub64 <t> (Mul64 x y) (Mul64 x z)) -> (Mul64 x (Sub64 <t> y z)) 333 (Sub32 <t> (Mul32 x y) (Mul32 x z)) -> (Mul32 x (Sub32 <t> y z)) 334 (Sub16 <t> (Mul16 x y) (Mul16 x z)) -> (Mul16 x (Sub16 <t> y z)) 335 (Sub8 <t> (Mul8 x y) (Mul8 x z)) -> (Mul8 x (Sub8 <t> y z)) 336 337 // rewrite shifts of 8/16/32 bit consts into 64 bit consts to reduce 338 // the number of the other rewrite rules for const shifts 339 (Lsh64x32 <t> x (Const32 [c])) -> (Lsh64x64 x (Const64 <t> [int64(uint32(c))])) 340 (Lsh64x16 <t> x (Const16 [c])) -> (Lsh64x64 x (Const64 <t> [int64(uint16(c))])) 341 (Lsh64x8 <t> x (Const8 [c])) -> (Lsh64x64 x (Const64 <t> [int64(uint8(c))])) 342 (Rsh64x32 <t> x (Const32 [c])) -> (Rsh64x64 x (Const64 <t> [int64(uint32(c))])) 343 (Rsh64x16 <t> x (Const16 [c])) -> (Rsh64x64 x (Const64 <t> [int64(uint16(c))])) 344 (Rsh64x8 <t> x (Const8 [c])) -> (Rsh64x64 x (Const64 <t> [int64(uint8(c))])) 345 (Rsh64Ux32 <t> x (Const32 [c])) -> (Rsh64Ux64 x (Const64 <t> [int64(uint32(c))])) 346 (Rsh64Ux16 <t> x (Const16 [c])) -> (Rsh64Ux64 x (Const64 <t> [int64(uint16(c))])) 347 (Rsh64Ux8 <t> x (Const8 [c])) -> (Rsh64Ux64 x (Const64 <t> [int64(uint8(c))])) 348 349 (Lsh32x32 <t> x (Const32 [c])) -> (Lsh32x64 x (Const64 <t> [int64(uint32(c))])) 350 (Lsh32x16 <t> x (Const16 [c])) -> (Lsh32x64 x (Const64 <t> [int64(uint16(c))])) 351 (Lsh32x8 <t> x (Const8 [c])) -> (Lsh32x64 x (Const64 <t> [int64(uint8(c))])) 352 (Rsh32x32 <t> x (Const32 [c])) -> (Rsh32x64 x (Const64 <t> [int64(uint32(c))])) 353 (Rsh32x16 <t> x (Const16 [c])) -> (Rsh32x64 x (Const64 <t> [int64(uint16(c))])) 354 (Rsh32x8 <t> x (Const8 [c])) -> (Rsh32x64 x (Const64 <t> [int64(uint8(c))])) 355 (Rsh32Ux32 <t> x (Const32 [c])) -> (Rsh32Ux64 x (Const64 <t> [int64(uint32(c))])) 356 (Rsh32Ux16 <t> x (Const16 [c])) -> (Rsh32Ux64 x (Const64 <t> [int64(uint16(c))])) 357 (Rsh32Ux8 <t> x (Const8 [c])) -> (Rsh32Ux64 x (Const64 <t> [int64(uint8(c))])) 358 359 (Lsh16x32 <t> x (Const32 [c])) -> (Lsh16x64 x (Const64 <t> [int64(uint32(c))])) 360 (Lsh16x16 <t> x (Const16 [c])) -> (Lsh16x64 x (Const64 <t> [int64(uint16(c))])) 361 (Lsh16x8 <t> x (Const8 [c])) -> (Lsh16x64 x (Const64 <t> [int64(uint8(c))])) 362 (Rsh16x32 <t> x (Const32 [c])) -> (Rsh16x64 x (Const64 <t> [int64(uint32(c))])) 363 (Rsh16x16 <t> x (Const16 [c])) -> (Rsh16x64 x (Const64 <t> [int64(uint16(c))])) 364 (Rsh16x8 <t> x (Const8 [c])) -> (Rsh16x64 x (Const64 <t> [int64(uint8(c))])) 365 (Rsh16Ux32 <t> x (Const32 [c])) -> (Rsh16Ux64 x (Const64 <t> [int64(uint32(c))])) 366 (Rsh16Ux16 <t> x (Const16 [c])) -> (Rsh16Ux64 x (Const64 <t> [int64(uint16(c))])) 367 (Rsh16Ux8 <t> x (Const8 [c])) -> (Rsh16Ux64 x (Const64 <t> [int64(uint8(c))])) 368 369 (Lsh8x32 <t> x (Const32 [c])) -> (Lsh8x64 x (Const64 <t> [int64(uint32(c))])) 370 (Lsh8x16 <t> x (Const16 [c])) -> (Lsh8x64 x (Const64 <t> [int64(uint16(c))])) 371 (Lsh8x8 <t> x (Const8 [c])) -> (Lsh8x64 x (Const64 <t> [int64(uint8(c))])) 372 (Rsh8x32 <t> x (Const32 [c])) -> (Rsh8x64 x (Const64 <t> [int64(uint32(c))])) 373 (Rsh8x16 <t> x (Const16 [c])) -> (Rsh8x64 x (Const64 <t> [int64(uint16(c))])) 374 (Rsh8x8 <t> x (Const8 [c])) -> (Rsh8x64 x (Const64 <t> [int64(uint8(c))])) 375 (Rsh8Ux32 <t> x (Const32 [c])) -> (Rsh8Ux64 x (Const64 <t> [int64(uint32(c))])) 376 (Rsh8Ux16 <t> x (Const16 [c])) -> (Rsh8Ux64 x (Const64 <t> [int64(uint16(c))])) 377 (Rsh8Ux8 <t> x (Const8 [c])) -> (Rsh8Ux64 x (Const64 <t> [int64(uint8(c))])) 378 379 // shifts by zero 380 (Lsh64x64 x (Const64 [0])) -> x 381 (Rsh64x64 x (Const64 [0])) -> x 382 (Rsh64Ux64 x (Const64 [0])) -> x 383 (Lsh32x64 x (Const64 [0])) -> x 384 (Rsh32x64 x (Const64 [0])) -> x 385 (Rsh32Ux64 x (Const64 [0])) -> x 386 (Lsh16x64 x (Const64 [0])) -> x 387 (Rsh16x64 x (Const64 [0])) -> x 388 (Rsh16Ux64 x (Const64 [0])) -> x 389 (Lsh8x64 x (Const64 [0])) -> x 390 (Rsh8x64 x (Const64 [0])) -> x 391 (Rsh8Ux64 x (Const64 [0])) -> x 392 393 // zero shifted. 394 (Lsh64x64 (Const64 [0]) _) -> (Const64 [0]) 395 (Lsh64x32 (Const64 [0]) _) -> (Const64 [0]) 396 (Lsh64x16 (Const64 [0]) _) -> (Const64 [0]) 397 (Lsh64x8 (Const64 [0]) _) -> (Const64 [0]) 398 (Rsh64x64 (Const64 [0]) _) -> (Const64 [0]) 399 (Rsh64x32 (Const64 [0]) _) -> (Const64 [0]) 400 (Rsh64x16 (Const64 [0]) _) -> (Const64 [0]) 401 (Rsh64x8 (Const64 [0]) _) -> (Const64 [0]) 402 (Rsh64Ux64 (Const64 [0]) _) -> (Const64 [0]) 403 (Rsh64Ux32 (Const64 [0]) _) -> (Const64 [0]) 404 (Rsh64Ux16 (Const64 [0]) _) -> (Const64 [0]) 405 (Rsh64Ux8 (Const64 [0]) _) -> (Const64 [0]) 406 (Lsh32x64 (Const32 [0]) _) -> (Const32 [0]) 407 (Lsh32x32 (Const32 [0]) _) -> (Const32 [0]) 408 (Lsh32x16 (Const32 [0]) _) -> (Const32 [0]) 409 (Lsh32x8 (Const32 [0]) _) -> (Const32 [0]) 410 (Rsh32x64 (Const32 [0]) _) -> (Const32 [0]) 411 (Rsh32x32 (Const32 [0]) _) -> (Const32 [0]) 412 (Rsh32x16 (Const32 [0]) _) -> (Const32 [0]) 413 (Rsh32x8 (Const32 [0]) _) -> (Const32 [0]) 414 (Rsh32Ux64 (Const32 [0]) _) -> (Const32 [0]) 415 (Rsh32Ux32 (Const32 [0]) _) -> (Const32 [0]) 416 (Rsh32Ux16 (Const32 [0]) _) -> (Const32 [0]) 417 (Rsh32Ux8 (Const32 [0]) _) -> (Const32 [0]) 418 (Lsh16x64 (Const16 [0]) _) -> (Const16 [0]) 419 (Lsh16x32 (Const16 [0]) _) -> (Const16 [0]) 420 (Lsh16x16 (Const16 [0]) _) -> (Const16 [0]) 421 (Lsh16x8 (Const16 [0]) _) -> (Const16 [0]) 422 (Rsh16x64 (Const16 [0]) _) -> (Const16 [0]) 423 (Rsh16x32 (Const16 [0]) _) -> (Const16 [0]) 424 (Rsh16x16 (Const16 [0]) _) -> (Const16 [0]) 425 (Rsh16x8 (Const16 [0]) _) -> (Const16 [0]) 426 (Rsh16Ux64 (Const16 [0]) _) -> (Const16 [0]) 427 (Rsh16Ux32 (Const16 [0]) _) -> (Const16 [0]) 428 (Rsh16Ux16 (Const16 [0]) _) -> (Const16 [0]) 429 (Rsh16Ux8 (Const16 [0]) _) -> (Const16 [0]) 430 (Lsh8x64 (Const8 [0]) _) -> (Const8 [0]) 431 (Lsh8x32 (Const8 [0]) _) -> (Const8 [0]) 432 (Lsh8x16 (Const8 [0]) _) -> (Const8 [0]) 433 (Lsh8x8 (Const8 [0]) _) -> (Const8 [0]) 434 (Rsh8x64 (Const8 [0]) _) -> (Const8 [0]) 435 (Rsh8x32 (Const8 [0]) _) -> (Const8 [0]) 436 (Rsh8x16 (Const8 [0]) _) -> (Const8 [0]) 437 (Rsh8x8 (Const8 [0]) _) -> (Const8 [0]) 438 (Rsh8Ux64 (Const8 [0]) _) -> (Const8 [0]) 439 (Rsh8Ux32 (Const8 [0]) _) -> (Const8 [0]) 440 (Rsh8Ux16 (Const8 [0]) _) -> (Const8 [0]) 441 (Rsh8Ux8 (Const8 [0]) _) -> (Const8 [0]) 442 443 // large left shifts of all values, and right shifts of unsigned values 444 (Lsh64x64 _ (Const64 [c])) && uint64(c) >= 64 -> (Const64 [0]) 445 (Rsh64Ux64 _ (Const64 [c])) && uint64(c) >= 64 -> (Const64 [0]) 446 (Lsh32x64 _ (Const64 [c])) && uint64(c) >= 32 -> (Const32 [0]) 447 (Rsh32Ux64 _ (Const64 [c])) && uint64(c) >= 32 -> (Const32 [0]) 448 (Lsh16x64 _ (Const64 [c])) && uint64(c) >= 16 -> (Const16 [0]) 449 (Rsh16Ux64 _ (Const64 [c])) && uint64(c) >= 16 -> (Const16 [0]) 450 (Lsh8x64 _ (Const64 [c])) && uint64(c) >= 8 -> (Const8 [0]) 451 (Rsh8Ux64 _ (Const64 [c])) && uint64(c) >= 8 -> (Const8 [0]) 452 453 // combine const shifts 454 (Lsh64x64 <t> (Lsh64x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Lsh64x64 x (Const64 <t> [c+d])) 455 (Lsh32x64 <t> (Lsh32x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Lsh32x64 x (Const64 <t> [c+d])) 456 (Lsh16x64 <t> (Lsh16x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Lsh16x64 x (Const64 <t> [c+d])) 457 (Lsh8x64 <t> (Lsh8x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Lsh8x64 x (Const64 <t> [c+d])) 458 459 (Rsh64x64 <t> (Rsh64x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh64x64 x (Const64 <t> [c+d])) 460 (Rsh32x64 <t> (Rsh32x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh32x64 x (Const64 <t> [c+d])) 461 (Rsh16x64 <t> (Rsh16x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh16x64 x (Const64 <t> [c+d])) 462 (Rsh8x64 <t> (Rsh8x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh8x64 x (Const64 <t> [c+d])) 463 464 (Rsh64Ux64 <t> (Rsh64Ux64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh64Ux64 x (Const64 <t> [c+d])) 465 (Rsh32Ux64 <t> (Rsh32Ux64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh32Ux64 x (Const64 <t> [c+d])) 466 (Rsh16Ux64 <t> (Rsh16Ux64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh16Ux64 x (Const64 <t> [c+d])) 467 (Rsh8Ux64 <t> (Rsh8Ux64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh8Ux64 x (Const64 <t> [c+d])) 468 469 // ((x >> c1) << c2) >> c3 470 (Rsh64Ux64 (Lsh64x64 (Rsh64Ux64 x (Const64 [c1])) (Const64 [c2])) (Const64 [c3])) 471 && uint64(c1) >= uint64(c2) && uint64(c3) >= uint64(c2) && !uaddOvf(c1-c2, c3) 472 -> (Rsh64Ux64 x (Const64 <typ.UInt64> [c1-c2+c3])) 473 (Rsh32Ux64 (Lsh32x64 (Rsh32Ux64 x (Const64 [c1])) (Const64 [c2])) (Const64 [c3])) 474 && uint64(c1) >= uint64(c2) && uint64(c3) >= uint64(c2) && !uaddOvf(c1-c2, c3) 475 -> (Rsh32Ux64 x (Const64 <typ.UInt64> [c1-c2+c3])) 476 (Rsh16Ux64 (Lsh16x64 (Rsh16Ux64 x (Const64 [c1])) (Const64 [c2])) (Const64 [c3])) 477 && uint64(c1) >= uint64(c2) && uint64(c3) >= uint64(c2) && !uaddOvf(c1-c2, c3) 478 -> (Rsh16Ux64 x (Const64 <typ.UInt64> [c1-c2+c3])) 479 (Rsh8Ux64 (Lsh8x64 (Rsh8Ux64 x (Const64 [c1])) (Const64 [c2])) (Const64 [c3])) 480 && uint64(c1) >= uint64(c2) && uint64(c3) >= uint64(c2) && !uaddOvf(c1-c2, c3) 481 -> (Rsh8Ux64 x (Const64 <typ.UInt64> [c1-c2+c3])) 482 483 // ((x << c1) >> c2) << c3 484 (Lsh64x64 (Rsh64Ux64 (Lsh64x64 x (Const64 [c1])) (Const64 [c2])) (Const64 [c3])) 485 && uint64(c1) >= uint64(c2) && uint64(c3) >= uint64(c2) && !uaddOvf(c1-c2, c3) 486 -> (Lsh64x64 x (Const64 <typ.UInt64> [c1-c2+c3])) 487 (Lsh32x64 (Rsh32Ux64 (Lsh32x64 x (Const64 [c1])) (Const64 [c2])) (Const64 [c3])) 488 && uint64(c1) >= uint64(c2) && uint64(c3) >= uint64(c2) && !uaddOvf(c1-c2, c3) 489 -> (Lsh32x64 x (Const64 <typ.UInt64> [c1-c2+c3])) 490 (Lsh16x64 (Rsh16Ux64 (Lsh16x64 x (Const64 [c1])) (Const64 [c2])) (Const64 [c3])) 491 && uint64(c1) >= uint64(c2) && uint64(c3) >= uint64(c2) && !uaddOvf(c1-c2, c3) 492 -> (Lsh16x64 x (Const64 <typ.UInt64> [c1-c2+c3])) 493 (Lsh8x64 (Rsh8Ux64 (Lsh8x64 x (Const64 [c1])) (Const64 [c2])) (Const64 [c3])) 494 && uint64(c1) >= uint64(c2) && uint64(c3) >= uint64(c2) && !uaddOvf(c1-c2, c3) 495 -> (Lsh8x64 x (Const64 <typ.UInt64> [c1-c2+c3])) 496 497 // replace shifts with zero extensions 498 (Rsh16Ux64 (Lsh16x64 x (Const64 [8])) (Const64 [8])) -> (ZeroExt8to16 (Trunc16to8 <typ.UInt8> x)) 499 (Rsh32Ux64 (Lsh32x64 x (Const64 [24])) (Const64 [24])) -> (ZeroExt8to32 (Trunc32to8 <typ.UInt8> x)) 500 (Rsh64Ux64 (Lsh64x64 x (Const64 [56])) (Const64 [56])) -> (ZeroExt8to64 (Trunc64to8 <typ.UInt8> x)) 501 (Rsh32Ux64 (Lsh32x64 x (Const64 [16])) (Const64 [16])) -> (ZeroExt16to32 (Trunc32to16 <typ.UInt16> x)) 502 (Rsh64Ux64 (Lsh64x64 x (Const64 [48])) (Const64 [48])) -> (ZeroExt16to64 (Trunc64to16 <typ.UInt16> x)) 503 (Rsh64Ux64 (Lsh64x64 x (Const64 [32])) (Const64 [32])) -> (ZeroExt32to64 (Trunc64to32 <typ.UInt32> x)) 504 505 // replace shifts with sign extensions 506 (Rsh16x64 (Lsh16x64 x (Const64 [8])) (Const64 [8])) -> (SignExt8to16 (Trunc16to8 <typ.Int8> x)) 507 (Rsh32x64 (Lsh32x64 x (Const64 [24])) (Const64 [24])) -> (SignExt8to32 (Trunc32to8 <typ.Int8> x)) 508 (Rsh64x64 (Lsh64x64 x (Const64 [56])) (Const64 [56])) -> (SignExt8to64 (Trunc64to8 <typ.Int8> x)) 509 (Rsh32x64 (Lsh32x64 x (Const64 [16])) (Const64 [16])) -> (SignExt16to32 (Trunc32to16 <typ.Int16> x)) 510 (Rsh64x64 (Lsh64x64 x (Const64 [48])) (Const64 [48])) -> (SignExt16to64 (Trunc64to16 <typ.Int16> x)) 511 (Rsh64x64 (Lsh64x64 x (Const64 [32])) (Const64 [32])) -> (SignExt32to64 (Trunc64to32 <typ.Int32> x)) 512 513 // constant comparisons 514 (Eq64 (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(c == d)]) 515 (Eq32 (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(c == d)]) 516 (Eq16 (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(c == d)]) 517 (Eq8 (Const8 [c]) (Const8 [d])) -> (ConstBool [b2i(c == d)]) 518 519 (Neq64 (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(c != d)]) 520 (Neq32 (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(c != d)]) 521 (Neq16 (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(c != d)]) 522 (Neq8 (Const8 [c]) (Const8 [d])) -> (ConstBool [b2i(c != d)]) 523 524 (Greater64 (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(c > d)]) 525 (Greater32 (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(c > d)]) 526 (Greater16 (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(c > d)]) 527 (Greater8 (Const8 [c]) (Const8 [d])) -> (ConstBool [b2i(c > d)]) 528 529 (Greater64U (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(uint64(c) > uint64(d))]) 530 (Greater32U (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(uint32(c) > uint32(d))]) 531 (Greater16U (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(uint16(c) > uint16(d))]) 532 (Greater8U (Const8 [c]) (Const8 [d])) -> (ConstBool [b2i(uint8(c) > uint8(d))]) 533 534 (Geq64 (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(c >= d)]) 535 (Geq32 (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(c >= d)]) 536 (Geq16 (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(c >= d)]) 537 (Geq8 (Const8 [c]) (Const8 [d])) -> (ConstBool [b2i(c >= d)]) 538 539 (Geq64U (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(uint64(c) >= uint64(d))]) 540 (Geq32U (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(uint32(c) >= uint32(d))]) 541 (Geq16U (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(uint16(c) >= uint16(d))]) 542 (Geq8U (Const8 [c]) (Const8 [d])) -> (ConstBool [b2i(uint8(c) >= uint8(d))]) 543 544 (Less64 (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(c < d)]) 545 (Less32 (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(c < d)]) 546 (Less16 (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(c < d)]) 547 (Less8 (Const8 [c]) (Const8 [d])) -> (ConstBool [b2i(c < d)]) 548 549 (Less64U (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(uint64(c) < uint64(d))]) 550 (Less32U (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(uint32(c) < uint32(d))]) 551 (Less16U (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(uint16(c) < uint16(d))]) 552 (Less8U (Const8 [c]) (Const8 [d])) -> (ConstBool [b2i(uint8(c) < uint8(d))]) 553 554 (Leq64 (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(c <= d)]) 555 (Leq32 (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(c <= d)]) 556 (Leq16 (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(c <= d)]) 557 (Leq8 (Const8 [c]) (Const8 [d])) -> (ConstBool [b2i(c <= d)]) 558 559 (Leq64U (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(uint64(c) <= uint64(d))]) 560 (Leq32U (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(uint32(c) <= uint32(d))]) 561 (Leq16U (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(uint16(c) <= uint16(d))]) 562 (Leq8U (Const8 [c]) (Const8 [d])) -> (ConstBool [b2i(uint8(c) <= uint8(d))]) 563 564 // simplifications 565 (Or64 x x) -> x 566 (Or32 x x) -> x 567 (Or16 x x) -> x 568 (Or8 x x) -> x 569 (Or64 (Const64 [0]) x) -> x 570 (Or32 (Const32 [0]) x) -> x 571 (Or16 (Const16 [0]) x) -> x 572 (Or8 (Const8 [0]) x) -> x 573 (Or64 (Const64 [-1]) _) -> (Const64 [-1]) 574 (Or32 (Const32 [-1]) _) -> (Const32 [-1]) 575 (Or16 (Const16 [-1]) _) -> (Const16 [-1]) 576 (Or8 (Const8 [-1]) _) -> (Const8 [-1]) 577 (And64 x x) -> x 578 (And32 x x) -> x 579 (And16 x x) -> x 580 (And8 x x) -> x 581 (And64 (Const64 [-1]) x) -> x 582 (And32 (Const32 [-1]) x) -> x 583 (And16 (Const16 [-1]) x) -> x 584 (And8 (Const8 [-1]) x) -> x 585 (And64 (Const64 [0]) _) -> (Const64 [0]) 586 (And32 (Const32 [0]) _) -> (Const32 [0]) 587 (And16 (Const16 [0]) _) -> (Const16 [0]) 588 (And8 (Const8 [0]) _) -> (Const8 [0]) 589 (Xor64 x x) -> (Const64 [0]) 590 (Xor32 x x) -> (Const32 [0]) 591 (Xor16 x x) -> (Const16 [0]) 592 (Xor8 x x) -> (Const8 [0]) 593 (Xor64 (Const64 [0]) x) -> x 594 (Xor32 (Const32 [0]) x) -> x 595 (Xor16 (Const16 [0]) x) -> x 596 (Xor8 (Const8 [0]) x) -> x 597 (Add64 (Const64 [0]) x) -> x 598 (Add32 (Const32 [0]) x) -> x 599 (Add16 (Const16 [0]) x) -> x 600 (Add8 (Const8 [0]) x) -> x 601 (Sub64 x x) -> (Const64 [0]) 602 (Sub32 x x) -> (Const32 [0]) 603 (Sub16 x x) -> (Const16 [0]) 604 (Sub8 x x) -> (Const8 [0]) 605 (Mul64 (Const64 [0]) _) -> (Const64 [0]) 606 (Mul32 (Const32 [0]) _) -> (Const32 [0]) 607 (Mul16 (Const16 [0]) _) -> (Const16 [0]) 608 (Mul8 (Const8 [0]) _) -> (Const8 [0]) 609 (Com8 (Com8 x)) -> x 610 (Com16 (Com16 x)) -> x 611 (Com32 (Com32 x)) -> x 612 (Com64 (Com64 x)) -> x 613 (Com8 (Const8 [c])) -> (Const8 [^c]) 614 (Com16 (Const16 [c])) -> (Const16 [^c]) 615 (Com32 (Const32 [c])) -> (Const32 [^c]) 616 (Com64 (Const64 [c])) -> (Const64 [^c]) 617 (Neg8 (Sub8 x y)) -> (Sub8 y x) 618 (Neg16 (Sub16 x y)) -> (Sub16 y x) 619 (Neg32 (Sub32 x y)) -> (Sub32 y x) 620 (Neg64 (Sub64 x y)) -> (Sub64 y x) 621 (Add8 (Const8 [1]) (Com8 x)) -> (Neg8 x) 622 (Add16 (Const16 [1]) (Com16 x)) -> (Neg16 x) 623 (Add32 (Const32 [1]) (Com32 x)) -> (Neg32 x) 624 (Add64 (Const64 [1]) (Com64 x)) -> (Neg64 x) 625 626 (And64 x (And64 x y)) -> (And64 x y) 627 (And32 x (And32 x y)) -> (And32 x y) 628 (And16 x (And16 x y)) -> (And16 x y) 629 (And8 x (And8 x y)) -> (And8 x y) 630 (Or64 x (Or64 x y)) -> (Or64 x y) 631 (Or32 x (Or32 x y)) -> (Or32 x y) 632 (Or16 x (Or16 x y)) -> (Or16 x y) 633 (Or8 x (Or8 x y)) -> (Or8 x y) 634 (Xor64 x (Xor64 x y)) -> y 635 (Xor32 x (Xor32 x y)) -> y 636 (Xor16 x (Xor16 x y)) -> y 637 (Xor8 x (Xor8 x y)) -> y 638 639 // Ands clear bits. Ors set bits. 640 // If a subsequent Or will set all the bits 641 // that an And cleared, we can skip the And. 642 // This happens in bitmasking code like: 643 // x &^= 3 << shift // clear two old bits 644 // x |= v << shift // set two new bits 645 // when shift is a small constant and v ends up a constant 3. 646 (Or8 (And8 x (Const8 [c2])) (Const8 <t> [c1])) && ^(c1 | c2) == 0 -> (Or8 (Const8 <t> [c1]) x) 647 (Or16 (And16 x (Const16 [c2])) (Const16 <t> [c1])) && ^(c1 | c2) == 0 -> (Or16 (Const16 <t> [c1]) x) 648 (Or32 (And32 x (Const32 [c2])) (Const32 <t> [c1])) && ^(c1 | c2) == 0 -> (Or32 (Const32 <t> [c1]) x) 649 (Or64 (And64 x (Const64 [c2])) (Const64 <t> [c1])) && ^(c1 | c2) == 0 -> (Or64 (Const64 <t> [c1]) x) 650 651 (Trunc64to8 (And64 (Const64 [y]) x)) && y&0xFF == 0xFF -> (Trunc64to8 x) 652 (Trunc64to16 (And64 (Const64 [y]) x)) && y&0xFFFF == 0xFFFF -> (Trunc64to16 x) 653 (Trunc64to32 (And64 (Const64 [y]) x)) && y&0xFFFFFFFF == 0xFFFFFFFF -> (Trunc64to32 x) 654 (Trunc32to8 (And32 (Const32 [y]) x)) && y&0xFF == 0xFF -> (Trunc32to8 x) 655 (Trunc32to16 (And32 (Const32 [y]) x)) && y&0xFFFF == 0xFFFF -> (Trunc32to16 x) 656 (Trunc16to8 (And16 (Const16 [y]) x)) && y&0xFF == 0xFF -> (Trunc16to8 x) 657 658 (ZeroExt8to64 (Trunc64to8 x:(Rsh64Ux64 _ (Const64 [s])))) && s >= 56 -> x 659 (ZeroExt16to64 (Trunc64to16 x:(Rsh64Ux64 _ (Const64 [s])))) && s >= 48 -> x 660 (ZeroExt32to64 (Trunc64to32 x:(Rsh64Ux64 _ (Const64 [s])))) && s >= 32 -> x 661 (ZeroExt8to32 (Trunc32to8 x:(Rsh32Ux64 _ (Const64 [s])))) && s >= 24 -> x 662 (ZeroExt16to32 (Trunc32to16 x:(Rsh32Ux64 _ (Const64 [s])))) && s >= 16 -> x 663 (ZeroExt8to16 (Trunc16to8 x:(Rsh16Ux64 _ (Const64 [s])))) && s >= 8 -> x 664 665 (SignExt8to64 (Trunc64to8 x:(Rsh64x64 _ (Const64 [s])))) && s >= 56 -> x 666 (SignExt16to64 (Trunc64to16 x:(Rsh64x64 _ (Const64 [s])))) && s >= 48 -> x 667 (SignExt32to64 (Trunc64to32 x:(Rsh64x64 _ (Const64 [s])))) && s >= 32 -> x 668 (SignExt8to32 (Trunc32to8 x:(Rsh32x64 _ (Const64 [s])))) && s >= 24 -> x 669 (SignExt16to32 (Trunc32to16 x:(Rsh32x64 _ (Const64 [s])))) && s >= 16 -> x 670 (SignExt8to16 (Trunc16to8 x:(Rsh16x64 _ (Const64 [s])))) && s >= 8 -> x 671 672 (Slicemask (Const32 [x])) && x > 0 -> (Const32 [-1]) 673 (Slicemask (Const32 [0])) -> (Const32 [0]) 674 (Slicemask (Const64 [x])) && x > 0 -> (Const64 [-1]) 675 (Slicemask (Const64 [0])) -> (Const64 [0]) 676 677 // Rewrite AND of consts as shifts if possible, slightly faster for 64 bit operands 678 // leading zeros can be shifted left, then right 679 (And64 <t> (Const64 [y]) x) && nlz(y) + nto(y) == 64 && nto(y) >= 32 680 -> (Rsh64Ux64 (Lsh64x64 <t> x (Const64 <t> [nlz(y)])) (Const64 <t> [nlz(y)])) 681 // trailing zeros can be shifted right, then left 682 (And64 <t> (Const64 [y]) x) && nlo(y) + ntz(y) == 64 && ntz(y) >= 32 683 -> (Lsh64x64 (Rsh64Ux64 <t> x (Const64 <t> [ntz(y)])) (Const64 <t> [ntz(y)])) 684 685 // simplifications often used for lengths. e.g. len(s[i:i+5])==5 686 (Sub64 (Add64 x y) x) -> y 687 (Sub64 (Add64 x y) y) -> x 688 (Sub32 (Add32 x y) x) -> y 689 (Sub32 (Add32 x y) y) -> x 690 (Sub16 (Add16 x y) x) -> y 691 (Sub16 (Add16 x y) y) -> x 692 (Sub8 (Add8 x y) x) -> y 693 (Sub8 (Add8 x y) y) -> x 694 695 // basic phi simplifications 696 (Phi (Const8 [c]) (Const8 [c])) -> (Const8 [c]) 697 (Phi (Const16 [c]) (Const16 [c])) -> (Const16 [c]) 698 (Phi (Const32 [c]) (Const32 [c])) -> (Const32 [c]) 699 (Phi (Const64 [c]) (Const64 [c])) -> (Const64 [c]) 700 701 // user nil checks 702 (NeqPtr p (ConstNil)) -> (IsNonNil p) 703 (EqPtr p (ConstNil)) -> (Not (IsNonNil p)) 704 (IsNonNil (ConstNil)) -> (ConstBool [0]) 705 706 // slice and interface comparisons 707 // The frontend ensures that we can only compare against nil, 708 // so we need only compare the first word (interface type or slice ptr). 709 (EqInter x y) -> (EqPtr (ITab x) (ITab y)) 710 (NeqInter x y) -> (NeqPtr (ITab x) (ITab y)) 711 (EqSlice x y) -> (EqPtr (SlicePtr x) (SlicePtr y)) 712 (NeqSlice x y) -> (NeqPtr (SlicePtr x) (SlicePtr y)) 713 714 // Load of store of same address, with compatibly typed value and same size 715 (Load <t1> p1 (Store {t2} p2 x _)) && isSamePtr(p1,p2) && t1.Compare(x.Type) == types.CMPeq && t1.Size() == t2.(*types.Type).Size() -> x 716 717 // Eliminate stores of values that have just been loaded from the same location. 718 // We also handle the common case where there are some intermediate stores to non-overlapping struct fields. 719 (Store {t1} p1 (Load <t2> p2 mem) mem) && 720 isSamePtr(p1, p2) && 721 t2.Size() == t1.(*types.Type).Size() -> mem 722 (Store {t1} (OffPtr [o1] p1) (Load <t2> (OffPtr [o1] p2) oldmem) mem:(Store {t3} (OffPtr [o3] p3) _ oldmem)) && 723 isSamePtr(p1, p2) && 724 isSamePtr(p1, p3) && 725 t2.Size() == t1.(*types.Type).Size() && 726 !overlap(o1, t2.Size(), o3, t3.(*types.Type).Size()) -> mem 727 (Store {t1} (OffPtr [o1] p1) (Load <t2> (OffPtr [o1] p2) oldmem) mem:(Store {t3} (OffPtr [o3] p3) _ (Store {t4} (OffPtr [o4] p4) _ oldmem))) && 728 isSamePtr(p1, p2) && 729 isSamePtr(p1, p3) && 730 isSamePtr(p1, p4) && 731 t2.Size() == t1.(*types.Type).Size() && 732 !overlap(o1, t2.Size(), o3, t3.(*types.Type).Size()) && 733 !overlap(o1, t2.Size(), o4, t4.(*types.Type).Size()) -> mem 734 (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)))) && 735 isSamePtr(p1, p2) && 736 isSamePtr(p1, p3) && 737 isSamePtr(p1, p4) && 738 isSamePtr(p1, p5) && 739 t2.Size() == t1.(*types.Type).Size() && 740 !overlap(o1, t2.Size(), o3, t3.(*types.Type).Size()) && 741 !overlap(o1, t2.Size(), o4, t4.(*types.Type).Size()) && 742 !overlap(o1, t2.Size(), o5, t5.(*types.Type).Size()) -> mem 743 744 // Collapse OffPtr 745 (OffPtr (OffPtr p [b]) [a]) -> (OffPtr p [a+b]) 746 (OffPtr p [0]) && v.Type.Compare(p.Type) == types.CMPeq -> p 747 748 // indexing operations 749 // Note: bounds check has already been done 750 (PtrIndex <t> ptr idx) && config.PtrSize == 4 -> (AddPtr ptr (Mul32 <typ.Int> idx (Const32 <typ.Int> [t.ElemType().Size()]))) 751 (PtrIndex <t> ptr idx) && config.PtrSize == 8 -> (AddPtr ptr (Mul64 <typ.Int> idx (Const64 <typ.Int> [t.ElemType().Size()]))) 752 753 // struct operations 754 (StructSelect (StructMake1 x)) -> x 755 (StructSelect [0] (StructMake2 x _)) -> x 756 (StructSelect [1] (StructMake2 _ x)) -> x 757 (StructSelect [0] (StructMake3 x _ _)) -> x 758 (StructSelect [1] (StructMake3 _ x _)) -> x 759 (StructSelect [2] (StructMake3 _ _ x)) -> x 760 (StructSelect [0] (StructMake4 x _ _ _)) -> x 761 (StructSelect [1] (StructMake4 _ x _ _)) -> x 762 (StructSelect [2] (StructMake4 _ _ x _)) -> x 763 (StructSelect [3] (StructMake4 _ _ _ x)) -> x 764 765 (Load <t> _ _) && t.IsStruct() && t.NumFields() == 0 && fe.CanSSA(t) -> 766 (StructMake0) 767 (Load <t> ptr mem) && t.IsStruct() && t.NumFields() == 1 && fe.CanSSA(t) -> 768 (StructMake1 769 (Load <t.FieldType(0)> (OffPtr <t.FieldType(0).PtrTo()> [0] ptr) mem)) 770 (Load <t> ptr mem) && t.IsStruct() && t.NumFields() == 2 && fe.CanSSA(t) -> 771 (StructMake2 772 (Load <t.FieldType(0)> (OffPtr <t.FieldType(0).PtrTo()> [0] ptr) mem) 773 (Load <t.FieldType(1)> (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] ptr) mem)) 774 (Load <t> ptr mem) && t.IsStruct() && t.NumFields() == 3 && fe.CanSSA(t) -> 775 (StructMake3 776 (Load <t.FieldType(0)> (OffPtr <t.FieldType(0).PtrTo()> [0] ptr) mem) 777 (Load <t.FieldType(1)> (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] ptr) mem) 778 (Load <t.FieldType(2)> (OffPtr <t.FieldType(2).PtrTo()> [t.FieldOff(2)] ptr) mem)) 779 (Load <t> ptr mem) && t.IsStruct() && t.NumFields() == 4 && fe.CanSSA(t) -> 780 (StructMake4 781 (Load <t.FieldType(0)> (OffPtr <t.FieldType(0).PtrTo()> [0] ptr) mem) 782 (Load <t.FieldType(1)> (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] ptr) mem) 783 (Load <t.FieldType(2)> (OffPtr <t.FieldType(2).PtrTo()> [t.FieldOff(2)] ptr) mem) 784 (Load <t.FieldType(3)> (OffPtr <t.FieldType(3).PtrTo()> [t.FieldOff(3)] ptr) mem)) 785 786 (StructSelect [i] x:(Load <t> ptr mem)) && !fe.CanSSA(t) -> 787 @x.Block (Load <v.Type> (OffPtr <v.Type.PtrTo()> [t.FieldOff(int(i))] ptr) mem) 788 789 (Store _ (StructMake0) mem) -> mem 790 (Store dst (StructMake1 <t> f0) mem) -> 791 (Store {t.FieldType(0)} (OffPtr <t.FieldType(0).PtrTo()> [0] dst) f0 mem) 792 (Store dst (StructMake2 <t> f0 f1) mem) -> 793 (Store {t.FieldType(1)} 794 (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] dst) 795 f1 796 (Store {t.FieldType(0)} 797 (OffPtr <t.FieldType(0).PtrTo()> [0] dst) 798 f0 mem)) 799 (Store dst (StructMake3 <t> f0 f1 f2) mem) -> 800 (Store {t.FieldType(2)} 801 (OffPtr <t.FieldType(2).PtrTo()> [t.FieldOff(2)] dst) 802 f2 803 (Store {t.FieldType(1)} 804 (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] dst) 805 f1 806 (Store {t.FieldType(0)} 807 (OffPtr <t.FieldType(0).PtrTo()> [0] dst) 808 f0 mem))) 809 (Store dst (StructMake4 <t> f0 f1 f2 f3) mem) -> 810 (Store {t.FieldType(3)} 811 (OffPtr <t.FieldType(3).PtrTo()> [t.FieldOff(3)] dst) 812 f3 813 (Store {t.FieldType(2)} 814 (OffPtr <t.FieldType(2).PtrTo()> [t.FieldOff(2)] dst) 815 f2 816 (Store {t.FieldType(1)} 817 (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] dst) 818 f1 819 (Store {t.FieldType(0)} 820 (OffPtr <t.FieldType(0).PtrTo()> [0] dst) 821 f0 mem)))) 822 823 // Putting struct{*byte} and similar into direct interfaces. 824 (IMake typ (StructMake1 val)) -> (IMake typ val) 825 (StructSelect [0] x:(IData _)) -> x 826 827 // un-SSAable values use mem->mem copies 828 (Store {t} dst (Load src mem) mem) && !fe.CanSSA(t.(*types.Type)) -> 829 (Move {t} [t.(*types.Type).Size()] dst src mem) 830 (Store {t} dst (Load src mem) (VarDef {x} mem)) && !fe.CanSSA(t.(*types.Type)) -> 831 (Move {t} [t.(*types.Type).Size()] dst src (VarDef {x} mem)) 832 833 // array ops 834 (ArraySelect (ArrayMake1 x)) -> x 835 836 (Load <t> _ _) && t.IsArray() && t.NumElem() == 0 -> 837 (ArrayMake0) 838 839 (Load <t> ptr mem) && t.IsArray() && t.NumElem() == 1 && fe.CanSSA(t) -> 840 (ArrayMake1 (Load <t.ElemType()> ptr mem)) 841 842 (Store _ (ArrayMake0) mem) -> mem 843 (Store dst (ArrayMake1 e) mem) -> (Store {e.Type} dst e mem) 844 845 (ArraySelect [0] (Load ptr mem)) -> (Load ptr mem) 846 847 // Putting [1]{*byte} and similar into direct interfaces. 848 (IMake typ (ArrayMake1 val)) -> (IMake typ val) 849 (ArraySelect [0] x:(IData _)) -> x 850 851 // string ops 852 // Decomposing StringMake and lowering of StringPtr and StringLen 853 // happens in a later pass, dec, so that these operations are available 854 // to other passes for optimizations. 855 (StringPtr (StringMake (Const64 <t> [c]) _)) -> (Const64 <t> [c]) 856 (StringLen (StringMake _ (Const64 <t> [c]))) -> (Const64 <t> [c]) 857 (ConstString {s}) && config.PtrSize == 4 && s.(string) == "" -> 858 (StringMake (ConstNil) (Const32 <typ.Int> [0])) 859 (ConstString {s}) && config.PtrSize == 8 && s.(string) == "" -> 860 (StringMake (ConstNil) (Const64 <typ.Int> [0])) 861 (ConstString {s}) && config.PtrSize == 4 && s.(string) != "" -> 862 (StringMake 863 (Addr <typ.BytePtr> {fe.StringData(s.(string))} 864 (SB)) 865 (Const32 <typ.Int> [int64(len(s.(string)))])) 866 (ConstString {s}) && config.PtrSize == 8 && s.(string) != "" -> 867 (StringMake 868 (Addr <typ.BytePtr> {fe.StringData(s.(string))} 869 (SB)) 870 (Const64 <typ.Int> [int64(len(s.(string)))])) 871 872 // slice ops 873 // Only a few slice rules are provided here. See dec.rules for 874 // a more comprehensive set. 875 (SliceLen (SliceMake _ (Const64 <t> [c]) _)) -> (Const64 <t> [c]) 876 (SliceCap (SliceMake _ _ (Const64 <t> [c]))) -> (Const64 <t> [c]) 877 (SliceLen (SliceMake _ (Const32 <t> [c]) _)) -> (Const32 <t> [c]) 878 (SliceCap (SliceMake _ _ (Const32 <t> [c]))) -> (Const32 <t> [c]) 879 (SlicePtr (SliceMake (SlicePtr x) _ _)) -> (SlicePtr x) 880 (SliceLen (SliceMake _ (SliceLen x) _)) -> (SliceLen x) 881 (SliceCap (SliceMake _ _ (SliceCap x))) -> (SliceCap x) 882 (SliceCap (SliceMake _ _ (SliceLen x))) -> (SliceLen x) 883 (ConstSlice) && config.PtrSize == 4 -> 884 (SliceMake 885 (ConstNil <v.Type.ElemType().PtrTo()>) 886 (Const32 <typ.Int> [0]) 887 (Const32 <typ.Int> [0])) 888 (ConstSlice) && config.PtrSize == 8 -> 889 (SliceMake 890 (ConstNil <v.Type.ElemType().PtrTo()>) 891 (Const64 <typ.Int> [0]) 892 (Const64 <typ.Int> [0])) 893 894 // interface ops 895 (ConstInterface) -> 896 (IMake 897 (ConstNil <typ.BytePtr>) 898 (ConstNil <typ.BytePtr>)) 899 900 (NilCheck (GetG mem) mem) -> mem 901 902 (If (Not cond) yes no) -> (If cond no yes) 903 (If (ConstBool [c]) yes no) && c == 1 -> (First nil yes no) 904 (If (ConstBool [c]) yes no) && c == 0 -> (First nil no yes) 905 906 // Get rid of Convert ops for pointer arithmetic on unsafe.Pointer. 907 (Convert (Add64 (Convert ptr mem) off) mem) -> (Add64 ptr off) 908 (Convert (Convert ptr mem) mem) -> ptr 909 910 // Decompose compound argument values 911 (Arg {n} [off]) && v.Type.IsString() -> 912 (StringMake 913 (Arg <typ.BytePtr> {n} [off]) 914 (Arg <typ.Int> {n} [off+config.PtrSize])) 915 916 (Arg {n} [off]) && v.Type.IsSlice() -> 917 (SliceMake 918 (Arg <v.Type.ElemType().PtrTo()> {n} [off]) 919 (Arg <typ.Int> {n} [off+config.PtrSize]) 920 (Arg <typ.Int> {n} [off+2*config.PtrSize])) 921 922 (Arg {n} [off]) && v.Type.IsInterface() -> 923 (IMake 924 (Arg <typ.BytePtr> {n} [off]) 925 (Arg <typ.BytePtr> {n} [off+config.PtrSize])) 926 927 (Arg {n} [off]) && v.Type.IsComplex() && v.Type.Size() == 16 -> 928 (ComplexMake 929 (Arg <typ.Float64> {n} [off]) 930 (Arg <typ.Float64> {n} [off+8])) 931 932 (Arg {n} [off]) && v.Type.IsComplex() && v.Type.Size() == 8 -> 933 (ComplexMake 934 (Arg <typ.Float32> {n} [off]) 935 (Arg <typ.Float32> {n} [off+4])) 936 937 (Arg <t>) && t.IsStruct() && t.NumFields() == 0 && fe.CanSSA(t) -> 938 (StructMake0) 939 (Arg <t> {n} [off]) && t.IsStruct() && t.NumFields() == 1 && fe.CanSSA(t) -> 940 (StructMake1 941 (Arg <t.FieldType(0)> {n} [off+t.FieldOff(0)])) 942 (Arg <t> {n} [off]) && t.IsStruct() && t.NumFields() == 2 && fe.CanSSA(t) -> 943 (StructMake2 944 (Arg <t.FieldType(0)> {n} [off+t.FieldOff(0)]) 945 (Arg <t.FieldType(1)> {n} [off+t.FieldOff(1)])) 946 (Arg <t> {n} [off]) && t.IsStruct() && t.NumFields() == 3 && fe.CanSSA(t) -> 947 (StructMake3 948 (Arg <t.FieldType(0)> {n} [off+t.FieldOff(0)]) 949 (Arg <t.FieldType(1)> {n} [off+t.FieldOff(1)]) 950 (Arg <t.FieldType(2)> {n} [off+t.FieldOff(2)])) 951 (Arg <t> {n} [off]) && t.IsStruct() && t.NumFields() == 4 && fe.CanSSA(t) -> 952 (StructMake4 953 (Arg <t.FieldType(0)> {n} [off+t.FieldOff(0)]) 954 (Arg <t.FieldType(1)> {n} [off+t.FieldOff(1)]) 955 (Arg <t.FieldType(2)> {n} [off+t.FieldOff(2)]) 956 (Arg <t.FieldType(3)> {n} [off+t.FieldOff(3)])) 957 958 (Arg <t>) && t.IsArray() && t.NumElem() == 0 -> 959 (ArrayMake0) 960 (Arg <t> {n} [off]) && t.IsArray() && t.NumElem() == 1 && fe.CanSSA(t) -> 961 (ArrayMake1 (Arg <t.ElemType()> {n} [off])) 962 963 // strength reduction of divide by a constant. 964 // See ../magic.go for a detailed description of these algorithms. 965 966 // Unsigned divide by power of 2. Strength reduce to a shift. 967 (Div8u n (Const8 [c])) && isPowerOfTwo(c&0xff) -> (Rsh8Ux64 n (Const64 <typ.UInt64> [log2(c&0xff)])) 968 (Div16u n (Const16 [c])) && isPowerOfTwo(c&0xffff) -> (Rsh16Ux64 n (Const64 <typ.UInt64> [log2(c&0xffff)])) 969 (Div32u n (Const32 [c])) && isPowerOfTwo(c&0xffffffff) -> (Rsh32Ux64 n (Const64 <typ.UInt64> [log2(c&0xffffffff)])) 970 (Div64u n (Const64 [c])) && isPowerOfTwo(c) -> (Rsh64Ux64 n (Const64 <typ.UInt64> [log2(c)])) 971 (Div64u n (Const64 [-1<<63])) -> (Rsh64Ux64 n (Const64 <typ.UInt64> [63])) 972 973 // Unsigned divide, not a power of 2. Strength reduce to a multiply. 974 // For 8-bit divides, we just do a direct 9-bit by 8-bit multiply. 975 (Div8u x (Const8 [c])) && umagicOK(8, c) -> 976 (Trunc32to8 977 (Rsh32Ux64 <typ.UInt32> 978 (Mul32 <typ.UInt32> 979 (Const32 <typ.UInt32> [int64(1<<8+umagic(8,c).m)]) 980 (ZeroExt8to32 x)) 981 (Const64 <typ.UInt64> [8+umagic(8,c).s]))) 982 983 // For 16-bit divides on 64-bit machines, we do a direct 17-bit by 16-bit multiply. 984 (Div16u x (Const16 [c])) && umagicOK(16, c) && config.RegSize == 8 -> 985 (Trunc64to16 986 (Rsh64Ux64 <typ.UInt64> 987 (Mul64 <typ.UInt64> 988 (Const64 <typ.UInt64> [int64(1<<16+umagic(16,c).m)]) 989 (ZeroExt16to64 x)) 990 (Const64 <typ.UInt64> [16+umagic(16,c).s]))) 991 992 // For 16-bit divides on 32-bit machines 993 (Div16u x (Const16 [c])) && umagicOK(16, c) && config.RegSize == 4 && umagic(16,c).m&1 == 0 -> 994 (Trunc32to16 995 (Rsh32Ux64 <typ.UInt32> 996 (Mul32 <typ.UInt32> 997 (Const32 <typ.UInt32> [int64(1<<15+umagic(16,c).m/2)]) 998 (ZeroExt16to32 x)) 999 (Const64 <typ.UInt64> [16+umagic(16,c).s-1]))) 1000 (Div16u x (Const16 [c])) && umagicOK(16, c) && config.RegSize == 4 && c&1 == 0 -> 1001 (Trunc32to16 1002 (Rsh32Ux64 <typ.UInt32> 1003 (Mul32 <typ.UInt32> 1004 (Const32 <typ.UInt32> [int64(1<<15+(umagic(16,c).m+1)/2)]) 1005 (Rsh32Ux64 <typ.UInt32> (ZeroExt16to32 x) (Const64 <typ.UInt64> [1]))) 1006 (Const64 <typ.UInt64> [16+umagic(16,c).s-2]))) 1007 (Div16u x (Const16 [c])) && umagicOK(16, c) && config.RegSize == 4 -> 1008 (Trunc32to16 1009 (Rsh32Ux64 <typ.UInt32> 1010 (Avg32u 1011 (Lsh32x64 <typ.UInt32> (ZeroExt16to32 x) (Const64 <typ.UInt64> [16])) 1012 (Mul32 <typ.UInt32> 1013 (Const32 <typ.UInt32> [int64(umagic(16,c).m)]) 1014 (ZeroExt16to32 x))) 1015 (Const64 <typ.UInt64> [16+umagic(16,c).s-1]))) 1016 1017 // For 32-bit divides on 32-bit machines 1018 (Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 4 && umagic(32,c).m&1 == 0 -> 1019 (Rsh32Ux64 <typ.UInt32> 1020 (Hmul32u <typ.UInt32> 1021 (Const32 <typ.UInt32> [int64(int32(1<<31+umagic(32,c).m/2))]) 1022 x) 1023 (Const64 <typ.UInt64> [umagic(32,c).s-1])) 1024 (Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 4 && c&1 == 0 -> 1025 (Rsh32Ux64 <typ.UInt32> 1026 (Hmul32u <typ.UInt32> 1027 (Const32 <typ.UInt32> [int64(int32(1<<31+(umagic(32,c).m+1)/2))]) 1028 (Rsh32Ux64 <typ.UInt32> x (Const64 <typ.UInt64> [1]))) 1029 (Const64 <typ.UInt64> [umagic(32,c).s-2])) 1030 (Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 4 -> 1031 (Rsh32Ux64 <typ.UInt32> 1032 (Avg32u 1033 x 1034 (Hmul32u <typ.UInt32> 1035 (Const32 <typ.UInt32> [int64(int32(umagic(32,c).m))]) 1036 x)) 1037 (Const64 <typ.UInt64> [umagic(32,c).s-1])) 1038 1039 // For 32-bit divides on 64-bit machines 1040 // We'll use a regular (non-hi) multiply for this case. 1041 (Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 8 && umagic(32,c).m&1 == 0 -> 1042 (Trunc64to32 1043 (Rsh64Ux64 <typ.UInt64> 1044 (Mul64 <typ.UInt64> 1045 (Const64 <typ.UInt64> [int64(1<<31+umagic(32,c).m/2)]) 1046 (ZeroExt32to64 x)) 1047 (Const64 <typ.UInt64> [32+umagic(32,c).s-1]))) 1048 (Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 8 && c&1 == 0 -> 1049 (Trunc64to32 1050 (Rsh64Ux64 <typ.UInt64> 1051 (Mul64 <typ.UInt64> 1052 (Const64 <typ.UInt64> [int64(1<<31+(umagic(32,c).m+1)/2)]) 1053 (Rsh64Ux64 <typ.UInt64> (ZeroExt32to64 x) (Const64 <typ.UInt64> [1]))) 1054 (Const64 <typ.UInt64> [32+umagic(32,c).s-2]))) 1055 (Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 8 -> 1056 (Trunc64to32 1057 (Rsh64Ux64 <typ.UInt64> 1058 (Avg64u 1059 (Lsh64x64 <typ.UInt64> (ZeroExt32to64 x) (Const64 <typ.UInt64> [32])) 1060 (Mul64 <typ.UInt64> 1061 (Const64 <typ.UInt32> [int64(umagic(32,c).m)]) 1062 (ZeroExt32to64 x))) 1063 (Const64 <typ.UInt64> [32+umagic(32,c).s-1]))) 1064 1065 // For 64-bit divides on 64-bit machines 1066 // (64-bit divides on 32-bit machines are lowered to a runtime call by the walk pass.) 1067 (Div64u x (Const64 [c])) && umagicOK(64, c) && config.RegSize == 8 && umagic(64,c).m&1 == 0 -> 1068 (Rsh64Ux64 <typ.UInt64> 1069 (Hmul64u <typ.UInt64> 1070 (Const64 <typ.UInt64> [int64(1<<63+umagic(64,c).m/2)]) 1071 x) 1072 (Const64 <typ.UInt64> [umagic(64,c).s-1])) 1073 (Div64u x (Const64 [c])) && umagicOK(64, c) && config.RegSize == 8 && c&1 == 0 -> 1074 (Rsh64Ux64 <typ.UInt64> 1075 (Hmul64u <typ.UInt64> 1076 (Const64 <typ.UInt64> [int64(1<<63+(umagic(64,c).m+1)/2)]) 1077 (Rsh64Ux64 <typ.UInt64> x (Const64 <typ.UInt64> [1]))) 1078 (Const64 <typ.UInt64> [umagic(64,c).s-2])) 1079 (Div64u x (Const64 [c])) && umagicOK(64, c) && config.RegSize == 8 -> 1080 (Rsh64Ux64 <typ.UInt64> 1081 (Avg64u 1082 x 1083 (Hmul64u <typ.UInt64> 1084 (Const64 <typ.UInt64> [int64(umagic(64,c).m)]) 1085 x)) 1086 (Const64 <typ.UInt64> [umagic(64,c).s-1])) 1087 1088 // Signed divide by a negative constant. Rewrite to divide by a positive constant. 1089 (Div8 <t> n (Const8 [c])) && c < 0 && c != -1<<7 -> (Neg8 (Div8 <t> n (Const8 <t> [-c]))) 1090 (Div16 <t> n (Const16 [c])) && c < 0 && c != -1<<15 -> (Neg16 (Div16 <t> n (Const16 <t> [-c]))) 1091 (Div32 <t> n (Const32 [c])) && c < 0 && c != -1<<31 -> (Neg32 (Div32 <t> n (Const32 <t> [-c]))) 1092 (Div64 <t> n (Const64 [c])) && c < 0 && c != -1<<63 -> (Neg64 (Div64 <t> n (Const64 <t> [-c]))) 1093 1094 // Dividing by the most-negative number. Result is always 0 except 1095 // if the input is also the most-negative number. 1096 // We can detect that using the sign bit of x & -x. 1097 (Div8 <t> x (Const8 [-1<<7 ])) -> (Rsh8Ux64 (And8 <t> x (Neg8 <t> x)) (Const64 <typ.UInt64> [7 ])) 1098 (Div16 <t> x (Const16 [-1<<15])) -> (Rsh16Ux64 (And16 <t> x (Neg16 <t> x)) (Const64 <typ.UInt64> [15])) 1099 (Div32 <t> x (Const32 [-1<<31])) -> (Rsh32Ux64 (And32 <t> x (Neg32 <t> x)) (Const64 <typ.UInt64> [31])) 1100 (Div64 <t> x (Const64 [-1<<63])) -> (Rsh64Ux64 (And64 <t> x (Neg64 <t> x)) (Const64 <typ.UInt64> [63])) 1101 1102 // Signed divide by power of 2. 1103 // n / c = n >> log(c) if n >= 0 1104 // = (n+c-1) >> log(c) if n < 0 1105 // We conditionally add c-1 by adding n>>63>>(64-log(c)) (first shift signed, second shift unsigned). 1106 (Div8 <t> n (Const8 [c])) && isPowerOfTwo(c) -> 1107 (Rsh8x64 1108 (Add8 <t> n (Rsh8Ux64 <t> (Rsh8x64 <t> n (Const64 <typ.UInt64> [ 7])) (Const64 <typ.UInt64> [ 8-log2(c)]))) 1109 (Const64 <typ.UInt64> [log2(c)])) 1110 (Div16 <t> n (Const16 [c])) && isPowerOfTwo(c) -> 1111 (Rsh16x64 1112 (Add16 <t> n (Rsh16Ux64 <t> (Rsh16x64 <t> n (Const64 <typ.UInt64> [15])) (Const64 <typ.UInt64> [16-log2(c)]))) 1113 (Const64 <typ.UInt64> [log2(c)])) 1114 (Div32 <t> n (Const32 [c])) && isPowerOfTwo(c) -> 1115 (Rsh32x64 1116 (Add32 <t> n (Rsh32Ux64 <t> (Rsh32x64 <t> n (Const64 <typ.UInt64> [31])) (Const64 <typ.UInt64> [32-log2(c)]))) 1117 (Const64 <typ.UInt64> [log2(c)])) 1118 (Div64 <t> n (Const64 [c])) && isPowerOfTwo(c) -> 1119 (Rsh64x64 1120 (Add64 <t> n (Rsh64Ux64 <t> (Rsh64x64 <t> n (Const64 <typ.UInt64> [63])) (Const64 <typ.UInt64> [64-log2(c)]))) 1121 (Const64 <typ.UInt64> [log2(c)])) 1122 1123 // Signed divide, not a power of 2. Strength reduce to a multiply. 1124 (Div8 <t> x (Const8 [c])) && smagicOK(8,c) -> 1125 (Sub8 <t> 1126 (Rsh32x64 <t> 1127 (Mul32 <typ.UInt32> 1128 (Const32 <typ.UInt32> [int64(smagic(8,c).m)]) 1129 (SignExt8to32 x)) 1130 (Const64 <typ.UInt64> [8+smagic(8,c).s])) 1131 (Rsh32x64 <t> 1132 (SignExt8to32 x) 1133 (Const64 <typ.UInt64> [31]))) 1134 (Div16 <t> x (Const16 [c])) && smagicOK(16,c) -> 1135 (Sub16 <t> 1136 (Rsh32x64 <t> 1137 (Mul32 <typ.UInt32> 1138 (Const32 <typ.UInt32> [int64(smagic(16,c).m)]) 1139 (SignExt16to32 x)) 1140 (Const64 <typ.UInt64> [16+smagic(16,c).s])) 1141 (Rsh32x64 <t> 1142 (SignExt16to32 x) 1143 (Const64 <typ.UInt64> [31]))) 1144 (Div32 <t> x (Const32 [c])) && smagicOK(32,c) && config.RegSize == 8 -> 1145 (Sub32 <t> 1146 (Rsh64x64 <t> 1147 (Mul64 <typ.UInt64> 1148 (Const64 <typ.UInt64> [int64(smagic(32,c).m)]) 1149 (SignExt32to64 x)) 1150 (Const64 <typ.UInt64> [32+smagic(32,c).s])) 1151 (Rsh64x64 <t> 1152 (SignExt32to64 x) 1153 (Const64 <typ.UInt64> [63]))) 1154 (Div32 <t> x (Const32 [c])) && smagicOK(32,c) && config.RegSize == 4 && smagic(32,c).m&1 == 0 -> 1155 (Sub32 <t> 1156 (Rsh32x64 <t> 1157 (Hmul32 <t> 1158 (Const32 <typ.UInt32> [int64(int32(smagic(32,c).m/2))]) 1159 x) 1160 (Const64 <typ.UInt64> [smagic(32,c).s-1])) 1161 (Rsh32x64 <t> 1162 x 1163 (Const64 <typ.UInt64> [31]))) 1164 (Div32 <t> x (Const32 [c])) && smagicOK(32,c) && config.RegSize == 4 && smagic(32,c).m&1 != 0 -> 1165 (Sub32 <t> 1166 (Rsh32x64 <t> 1167 (Add32 <t> 1168 (Hmul32 <t> 1169 (Const32 <typ.UInt32> [int64(int32(smagic(32,c).m))]) 1170 x) 1171 x) 1172 (Const64 <typ.UInt64> [smagic(32,c).s])) 1173 (Rsh32x64 <t> 1174 x 1175 (Const64 <typ.UInt64> [31]))) 1176 (Div64 <t> x (Const64 [c])) && smagicOK(64,c) && smagic(64,c).m&1 == 0 -> 1177 (Sub64 <t> 1178 (Rsh64x64 <t> 1179 (Hmul64 <t> 1180 (Const64 <typ.UInt64> [int64(smagic(64,c).m/2)]) 1181 x) 1182 (Const64 <typ.UInt64> [smagic(64,c).s-1])) 1183 (Rsh64x64 <t> 1184 x 1185 (Const64 <typ.UInt64> [63]))) 1186 (Div64 <t> x (Const64 [c])) && smagicOK(64,c) && smagic(64,c).m&1 != 0 -> 1187 (Sub64 <t> 1188 (Rsh64x64 <t> 1189 (Add64 <t> 1190 (Hmul64 <t> 1191 (Const64 <typ.UInt64> [int64(smagic(64,c).m)]) 1192 x) 1193 x) 1194 (Const64 <typ.UInt64> [smagic(64,c).s])) 1195 (Rsh64x64 <t> 1196 x 1197 (Const64 <typ.UInt64> [63]))) 1198 1199 // Unsigned mod by power of 2 constant. 1200 (Mod8u <t> n (Const8 [c])) && isPowerOfTwo(c&0xff) -> (And8 n (Const8 <t> [(c&0xff)-1])) 1201 (Mod16u <t> n (Const16 [c])) && isPowerOfTwo(c&0xffff) -> (And16 n (Const16 <t> [(c&0xffff)-1])) 1202 (Mod32u <t> n (Const32 [c])) && isPowerOfTwo(c&0xffffffff) -> (And32 n (Const32 <t> [(c&0xffffffff)-1])) 1203 (Mod64u <t> n (Const64 [c])) && isPowerOfTwo(c) -> (And64 n (Const64 <t> [c-1])) 1204 (Mod64u <t> n (Const64 [-1<<63])) -> (And64 n (Const64 <t> [1<<63-1])) 1205 1206 // Signed mod by negative constant. 1207 (Mod8 <t> n (Const8 [c])) && c < 0 && c != -1<<7 -> (Mod8 <t> n (Const8 <t> [-c])) 1208 (Mod16 <t> n (Const16 [c])) && c < 0 && c != -1<<15 -> (Mod16 <t> n (Const16 <t> [-c])) 1209 (Mod32 <t> n (Const32 [c])) && c < 0 && c != -1<<31 -> (Mod32 <t> n (Const32 <t> [-c])) 1210 (Mod64 <t> n (Const64 [c])) && c < 0 && c != -1<<63 -> (Mod64 <t> n (Const64 <t> [-c])) 1211 1212 // All other mods by constants, do A%B = A-(A/B*B). 1213 // This implements % with two * and a bunch of ancillary ops. 1214 // One of the * is free if the user's code also computes A/B. 1215 (Mod8 <t> x (Const8 [c])) && x.Op != OpConst8 && (c > 0 || c == -1<<7) 1216 -> (Sub8 x (Mul8 <t> (Div8 <t> x (Const8 <t> [c])) (Const8 <t> [c]))) 1217 (Mod16 <t> x (Const16 [c])) && x.Op != OpConst16 && (c > 0 || c == -1<<15) 1218 -> (Sub16 x (Mul16 <t> (Div16 <t> x (Const16 <t> [c])) (Const16 <t> [c]))) 1219 (Mod32 <t> x (Const32 [c])) && x.Op != OpConst32 && (c > 0 || c == -1<<31) 1220 -> (Sub32 x (Mul32 <t> (Div32 <t> x (Const32 <t> [c])) (Const32 <t> [c]))) 1221 (Mod64 <t> x (Const64 [c])) && x.Op != OpConst64 && (c > 0 || c == -1<<63) 1222 -> (Sub64 x (Mul64 <t> (Div64 <t> x (Const64 <t> [c])) (Const64 <t> [c]))) 1223 (Mod8u <t> x (Const8 [c])) && x.Op != OpConst8 && c > 0 && umagicOK(8 ,c) 1224 -> (Sub8 x (Mul8 <t> (Div8u <t> x (Const8 <t> [c])) (Const8 <t> [c]))) 1225 (Mod16u <t> x (Const16 [c])) && x.Op != OpConst16 && c > 0 && umagicOK(16,c) 1226 -> (Sub16 x (Mul16 <t> (Div16u <t> x (Const16 <t> [c])) (Const16 <t> [c]))) 1227 (Mod32u <t> x (Const32 [c])) && x.Op != OpConst32 && c > 0 && umagicOK(32,c) 1228 -> (Sub32 x (Mul32 <t> (Div32u <t> x (Const32 <t> [c])) (Const32 <t> [c]))) 1229 (Mod64u <t> x (Const64 [c])) && x.Op != OpConst64 && c > 0 && umagicOK(64,c) 1230 -> (Sub64 x (Mul64 <t> (Div64u <t> x (Const64 <t> [c])) (Const64 <t> [c]))) 1231 1232 // Reassociate expressions involving 1233 // constants such that constants come first, 1234 // exposing obvious constant-folding opportunities. 1235 // Reassociate (op (op y C) x) to (op C (op x y)) or similar, where C 1236 // is constant, which pushes constants to the outside 1237 // of the expression. At that point, any constant-folding 1238 // opportunities should be obvious. 1239 1240 // x + (C + z) -> C + (x + z) 1241 (Add64 (Add64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Add64 i (Add64 <t> z x)) 1242 (Add32 (Add32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Add32 i (Add32 <t> z x)) 1243 (Add16 (Add16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Add16 i (Add16 <t> z x)) 1244 (Add8 (Add8 i:(Const8 <t>) z) x) && (z.Op != OpConst8 && x.Op != OpConst8) -> (Add8 i (Add8 <t> z x)) 1245 1246 // x + (C - z) -> C + (x - z) 1247 (Add64 (Sub64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Add64 i (Sub64 <t> x z)) 1248 (Add32 (Sub32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Add32 i (Sub32 <t> x z)) 1249 (Add16 (Sub16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Add16 i (Sub16 <t> x z)) 1250 (Add8 (Sub8 i:(Const8 <t>) z) x) && (z.Op != OpConst8 && x.Op != OpConst8) -> (Add8 i (Sub8 <t> x z)) 1251 (Add64 x (Sub64 i:(Const64 <t>) z)) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Add64 i (Sub64 <t> x z)) 1252 (Add32 x (Sub32 i:(Const32 <t>) z)) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Add32 i (Sub32 <t> x z)) 1253 (Add16 x (Sub16 i:(Const16 <t>) z)) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Add16 i (Sub16 <t> x z)) 1254 (Add8 x (Sub8 i:(Const8 <t>) z)) && (z.Op != OpConst8 && x.Op != OpConst8) -> (Add8 i (Sub8 <t> x z)) 1255 1256 // x + (z - C) -> (x + z) - C 1257 (Add64 (Sub64 z i:(Const64 <t>)) x) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Sub64 (Add64 <t> x z) i) 1258 (Add32 (Sub32 z i:(Const32 <t>)) x) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Sub32 (Add32 <t> x z) i) 1259 (Add16 (Sub16 z i:(Const16 <t>)) x) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Sub16 (Add16 <t> x z) i) 1260 (Add8 (Sub8 z i:(Const8 <t>)) x) && (z.Op != OpConst8 && x.Op != OpConst8) -> (Sub8 (Add8 <t> x z) i) 1261 (Add64 x (Sub64 z i:(Const64 <t>))) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Sub64 (Add64 <t> x z) i) 1262 (Add32 x (Sub32 z i:(Const32 <t>))) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Sub32 (Add32 <t> x z) i) 1263 (Add16 x (Sub16 z i:(Const16 <t>))) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Sub16 (Add16 <t> x z) i) 1264 (Add8 x (Sub8 z i:(Const8 <t>))) && (z.Op != OpConst8 && x.Op != OpConst8) -> (Sub8 (Add8 <t> x z) i) 1265 1266 // x - (C - z) -> x + (z - C) -> (x + z) - C 1267 (Sub64 x (Sub64 i:(Const64 <t>) z)) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Sub64 (Add64 <t> x z) i) 1268 (Sub32 x (Sub32 i:(Const32 <t>) z)) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Sub32 (Add32 <t> x z) i) 1269 (Sub16 x (Sub16 i:(Const16 <t>) z)) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Sub16 (Add16 <t> x z) i) 1270 (Sub8 x (Sub8 i:(Const8 <t>) z)) && (z.Op != OpConst8 && x.Op != OpConst8) -> (Sub8 (Add8 <t> x z) i) 1271 1272 // x - (z - C) -> x + (C - z) -> (x - z) + C 1273 (Sub64 x (Sub64 z i:(Const64 <t>))) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Add64 i (Sub64 <t> x z)) 1274 (Sub32 x (Sub32 z i:(Const32 <t>))) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Add32 i (Sub32 <t> x z)) 1275 (Sub16 x (Sub16 z i:(Const16 <t>))) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Add16 i (Sub16 <t> x z)) 1276 (Sub8 x (Sub8 z i:(Const8 <t>))) && (z.Op != OpConst8 && x.Op != OpConst8) -> (Add8 i (Sub8 <t> x z)) 1277 1278 // x & (C & z) -> C & (x & z) 1279 (And64 (And64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) -> (And64 i (And64 <t> z x)) 1280 (And32 (And32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) -> (And32 i (And32 <t> z x)) 1281 (And16 (And16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) -> (And16 i (And16 <t> z x)) 1282 (And8 (And8 i:(Const8 <t>) z) x) && (z.Op != OpConst8 && x.Op != OpConst8) -> (And8 i (And8 <t> z x)) 1283 1284 // x | (C | z) -> C | (x | z) 1285 (Or64 (Or64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Or64 i (Or64 <t> z x)) 1286 (Or32 (Or32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Or32 i (Or32 <t> z x)) 1287 (Or16 (Or16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Or16 i (Or16 <t> z x)) 1288 (Or8 (Or8 i:(Const8 <t>) z) x) && (z.Op != OpConst8 && x.Op != OpConst8) -> (Or8 i (Or8 <t> z x)) 1289 1290 // x ^ (C ^ z) -> C ^ (x ^ z) 1291 (Xor64 (Xor64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Xor64 i (Xor64 <t> z x)) 1292 (Xor32 (Xor32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Xor32 i (Xor32 <t> z x)) 1293 (Xor16 (Xor16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Xor16 i (Xor16 <t> z x)) 1294 (Xor8 (Xor8 i:(Const8 <t>) z) x) && (z.Op != OpConst8 && x.Op != OpConst8) -> (Xor8 i (Xor8 <t> z x)) 1295 1296 // C + (D + x) -> (C + D) + x 1297 (Add64 (Const64 <t> [c]) (Add64 (Const64 <t> [d]) x)) -> (Add64 (Const64 <t> [c+d]) x) 1298 (Add32 (Const32 <t> [c]) (Add32 (Const32 <t> [d]) x)) -> (Add32 (Const32 <t> [int64(int32(c+d))]) x) 1299 (Add16 (Const16 <t> [c]) (Add16 (Const16 <t> [d]) x)) -> (Add16 (Const16 <t> [int64(int16(c+d))]) x) 1300 (Add8 (Const8 <t> [c]) (Add8 (Const8 <t> [d]) x)) -> (Add8 (Const8 <t> [int64(int8(c+d))]) x) 1301 1302 // C + (D - x) -> (C + D) - x 1303 (Add64 (Const64 <t> [c]) (Sub64 (Const64 <t> [d]) x)) -> (Sub64 (Const64 <t> [c+d]) x) 1304 (Add32 (Const32 <t> [c]) (Sub32 (Const32 <t> [d]) x)) -> (Sub32 (Const32 <t> [int64(int32(c+d))]) x) 1305 (Add16 (Const16 <t> [c]) (Sub16 (Const16 <t> [d]) x)) -> (Sub16 (Const16 <t> [int64(int16(c+d))]) x) 1306 (Add8 (Const8 <t> [c]) (Sub8 (Const8 <t> [d]) x)) -> (Sub8 (Const8 <t> [int64(int8(c+d))]) x) 1307 1308 // C + (x - D) -> (C - D) + x 1309 (Add64 (Const64 <t> [c]) (Sub64 x (Const64 <t> [d]))) -> (Add64 (Const64 <t> [c-d]) x) 1310 (Add32 (Const32 <t> [c]) (Sub32 x (Const32 <t> [d]))) -> (Add32 (Const32 <t> [int64(int32(c-d))]) x) 1311 (Add16 (Const16 <t> [c]) (Sub16 x (Const16 <t> [d]))) -> (Add16 (Const16 <t> [int64(int16(c-d))]) x) 1312 (Add8 (Const8 <t> [c]) (Sub8 x (Const8 <t> [d]))) -> (Add8 (Const8 <t> [int64(int8(c-d))]) x) 1313 1314 // C - (x - D) -> (C + D) - x 1315 (Sub64 (Const64 <t> [c]) (Sub64 x (Const64 <t> [d]))) -> (Sub64 (Const64 <t> [c+d]) x) 1316 (Sub32 (Const32 <t> [c]) (Sub32 x (Const32 <t> [d]))) -> (Sub32 (Const32 <t> [int64(int32(c+d))]) x) 1317 (Sub16 (Const16 <t> [c]) (Sub16 x (Const16 <t> [d]))) -> (Sub16 (Const16 <t> [int64(int16(c+d))]) x) 1318 (Sub8 (Const8 <t> [c]) (Sub8 x (Const8 <t> [d]))) -> (Sub8 (Const8 <t> [int64(int8(c+d))]) x) 1319 1320 // C - (D - x) -> (C - D) + x 1321 (Sub64 (Const64 <t> [c]) (Sub64 (Const64 <t> [d]) x)) -> (Add64 (Const64 <t> [c-d]) x) 1322 (Sub32 (Const32 <t> [c]) (Sub32 (Const32 <t> [d]) x)) -> (Add32 (Const32 <t> [int64(int32(c-d))]) x) 1323 (Sub16 (Const16 <t> [c]) (Sub16 (Const16 <t> [d]) x)) -> (Add16 (Const16 <t> [int64(int16(c-d))]) x) 1324 (Sub8 (Const8 <t> [c]) (Sub8 (Const8 <t> [d]) x)) -> (Add8 (Const8 <t> [int64(int8(c-d))]) x) 1325 1326 // C & (D & x) -> (C & D) & x 1327 (And64 (Const64 <t> [c]) (And64 (Const64 <t> [d]) x)) -> (And64 (Const64 <t> [c&d]) x) 1328 (And32 (Const32 <t> [c]) (And32 (Const32 <t> [d]) x)) -> (And32 (Const32 <t> [int64(int32(c&d))]) x) 1329 (And16 (Const16 <t> [c]) (And16 (Const16 <t> [d]) x)) -> (And16 (Const16 <t> [int64(int16(c&d))]) x) 1330 (And8 (Const8 <t> [c]) (And8 (Const8 <t> [d]) x)) -> (And8 (Const8 <t> [int64(int8(c&d))]) x) 1331 1332 // C | (D | x) -> (C | D) | x 1333 (Or64 (Const64 <t> [c]) (Or64 (Const64 <t> [d]) x)) -> (Or64 (Const64 <t> [c|d]) x) 1334 (Or32 (Const32 <t> [c]) (Or32 (Const32 <t> [d]) x)) -> (Or32 (Const32 <t> [int64(int32(c|d))]) x) 1335 (Or16 (Const16 <t> [c]) (Or16 (Const16 <t> [d]) x)) -> (Or16 (Const16 <t> [int64(int16(c|d))]) x) 1336 (Or8 (Const8 <t> [c]) (Or8 (Const8 <t> [d]) x)) -> (Or8 (Const8 <t> [int64(int8(c|d))]) x) 1337 1338 // C ^ (D ^ x) -> (C ^ D) ^ x 1339 (Xor64 (Const64 <t> [c]) (Xor64 (Const64 <t> [d]) x)) -> (Xor64 (Const64 <t> [c^d]) x) 1340 (Xor32 (Const32 <t> [c]) (Xor32 (Const32 <t> [d]) x)) -> (Xor32 (Const32 <t> [int64(int32(c^d))]) x) 1341 (Xor16 (Const16 <t> [c]) (Xor16 (Const16 <t> [d]) x)) -> (Xor16 (Const16 <t> [int64(int16(c^d))]) x) 1342 (Xor8 (Const8 <t> [c]) (Xor8 (Const8 <t> [d]) x)) -> (Xor8 (Const8 <t> [int64(int8(c^d))]) x) 1343 1344 // C * (D * x) = (C * D) * x 1345 (Mul64 (Const64 <t> [c]) (Mul64 (Const64 <t> [d]) x)) -> (Mul64 (Const64 <t> [c*d]) x) 1346 (Mul32 (Const32 <t> [c]) (Mul32 (Const32 <t> [d]) x)) -> (Mul32 (Const32 <t> [int64(int32(c*d))]) x) 1347 (Mul16 (Const16 <t> [c]) (Mul16 (Const16 <t> [d]) x)) -> (Mul16 (Const16 <t> [int64(int16(c*d))]) x) 1348 (Mul8 (Const8 <t> [c]) (Mul8 (Const8 <t> [d]) x)) -> (Mul8 (Const8 <t> [int64(int8(c*d))]) x) 1349 1350 // floating point optimizations 1351 (Add32F x (Const32F [0])) -> x 1352 (Add64F x (Const64F [0])) -> x 1353 (Sub32F x (Const32F [0])) -> x 1354 (Sub64F x (Const64F [0])) -> x 1355 (Mul32F x (Const32F [f2i(1)])) -> x 1356 (Mul64F x (Const64F [f2i(1)])) -> x 1357 (Mul32F x (Const32F [f2i(-1)])) -> (Neg32F x) 1358 (Mul64F x (Const64F [f2i(-1)])) -> (Neg64F x) 1359 (Mul32F x (Const32F [f2i(2)])) -> (Add32F x x) 1360 (Mul64F x (Const64F [f2i(2)])) -> (Add64F x x) 1361 (Div32F x (Const32F <t> [c])) && reciprocalExact32(float32(i2f(c))) -> (Mul32F x (Const32F <t> [f2i(1/i2f(c))])) 1362 (Div64F x (Const64F <t> [c])) && reciprocalExact64(i2f(c)) -> (Mul64F x (Const64F <t> [f2i(1/i2f(c))])) 1363 1364 (Sqrt (Const64F [c])) -> (Const64F [f2i(math.Sqrt(i2f(c)))]) 1365 1366 // recognize runtime.newobject and don't Zero/Nilcheck it 1367 (Zero (Load (OffPtr [c] (SP)) mem) mem) 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 (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 (Store (OffPtr (Load (OffPtr [c] (SP)) mem)) x mem) 1379 && isConstZero(x) 1380 && mem.Op == OpStaticCall 1381 && isSameSym(mem.Aux, "runtime.newobject") 1382 && c == config.ctxt.FixedFrameSize() + config.RegSize // offset of return value 1383 -> mem 1384 // nil checks just need to rewrite to something useless. 1385 // they will be deadcode eliminated soon afterwards. 1386 (NilCheck (Load (OffPtr [c] (SP)) (StaticCall {sym} _)) _) 1387 && isSameSym(sym, "runtime.newobject") 1388 && c == config.ctxt.FixedFrameSize() + config.RegSize // offset of return value 1389 && warnRule(fe.Debug_checknil() && v.Pos.Line() > 1, v, "removed nil check") 1390 -> (Invalid) 1391 (NilCheck (OffPtr (Load (OffPtr [c] (SP)) (StaticCall {sym} _))) _) 1392 && isSameSym(sym, "runtime.newobject") 1393 && c == config.ctxt.FixedFrameSize() + config.RegSize // offset of return value 1394 && warnRule(fe.Debug_checknil() && v.Pos.Line() > 1, v, "removed nil check") 1395 -> (Invalid) 1396 1397 // Address comparison shows up in type assertions. 1398 (EqPtr x x) -> (ConstBool [1]) 1399 (EqPtr (Addr {a} x) (Addr {b} x)) -> (ConstBool [b2i(a == b)]) 1400 1401 // De-virtualize interface calls into static calls. 1402 // Note that (ITab (IMake)) doesn't get 1403 // rewritten until after the first opt pass, 1404 // so this rule should trigger reliably. 1405 (InterCall [argsize] (Load (OffPtr [off] (ITab (IMake (Addr {itab} (SB)) _))) _) mem) && devirt(v, itab, off) != nil -> 1406 (StaticCall [argsize] {devirt(v, itab, off)} mem)