github.com/hikaru7719/go@v0.0.0-20181025140707-c8b2ac68906a/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 [auxFrom32F(float32(auxTo64F(c)))]) 48 (Cvt32Fto64F (Const32F [c])) -> (Const64F [c]) // c is already a 64 bit float 49 (Cvt32to32F (Const32 [c])) -> (Const32F [auxFrom32F(float32(int32(c)))]) 50 (Cvt32to64F (Const32 [c])) -> (Const64F [auxFrom64F(float64(int32(c)))]) 51 (Cvt64to32F (Const64 [c])) -> (Const32F [auxFrom32F(float32(c))]) 52 (Cvt64to64F (Const64 [c])) -> (Const64F [auxFrom64F(float64(c))]) 53 (Cvt32Fto32 (Const32F [c])) -> (Const32 [int64(int32(auxTo32F(c)))]) 54 (Cvt32Fto64 (Const32F [c])) -> (Const64 [int64(auxTo32F(c))]) 55 (Cvt64Fto32 (Const64F [c])) -> (Const32 [int64(int32(auxTo64F(c)))]) 56 (Cvt64Fto64 (Const64F [c])) -> (Const64 [int64(auxTo64F(c))]) 57 (Round32F x:(Const32F)) -> x 58 (Round64F x:(Const64F)) -> x 59 60 (Trunc16to8 (ZeroExt8to16 x)) -> x 61 (Trunc32to8 (ZeroExt8to32 x)) -> x 62 (Trunc32to16 (ZeroExt8to32 x)) -> (ZeroExt8to16 x) 63 (Trunc32to16 (ZeroExt16to32 x)) -> x 64 (Trunc64to8 (ZeroExt8to64 x)) -> x 65 (Trunc64to16 (ZeroExt8to64 x)) -> (ZeroExt8to16 x) 66 (Trunc64to16 (ZeroExt16to64 x)) -> x 67 (Trunc64to32 (ZeroExt8to64 x)) -> (ZeroExt8to32 x) 68 (Trunc64to32 (ZeroExt16to64 x)) -> (ZeroExt16to32 x) 69 (Trunc64to32 (ZeroExt32to64 x)) -> x 70 (Trunc16to8 (SignExt8to16 x)) -> x 71 (Trunc32to8 (SignExt8to32 x)) -> x 72 (Trunc32to16 (SignExt8to32 x)) -> (SignExt8to16 x) 73 (Trunc32to16 (SignExt16to32 x)) -> x 74 (Trunc64to8 (SignExt8to64 x)) -> x 75 (Trunc64to16 (SignExt8to64 x)) -> (SignExt8to16 x) 76 (Trunc64to16 (SignExt16to64 x)) -> x 77 (Trunc64to32 (SignExt8to64 x)) -> (SignExt8to32 x) 78 (Trunc64to32 (SignExt16to64 x)) -> (SignExt16to32 x) 79 (Trunc64to32 (SignExt32to64 x)) -> x 80 81 (ZeroExt8to16 (Const8 [c])) -> (Const16 [int64( uint8(c))]) 82 (ZeroExt8to32 (Const8 [c])) -> (Const32 [int64( uint8(c))]) 83 (ZeroExt8to64 (Const8 [c])) -> (Const64 [int64( uint8(c))]) 84 (ZeroExt16to32 (Const16 [c])) -> (Const32 [int64(uint16(c))]) 85 (ZeroExt16to64 (Const16 [c])) -> (Const64 [int64(uint16(c))]) 86 (ZeroExt32to64 (Const32 [c])) -> (Const64 [int64(uint32(c))]) 87 (SignExt8to16 (Const8 [c])) -> (Const16 [int64( int8(c))]) 88 (SignExt8to32 (Const8 [c])) -> (Const32 [int64( int8(c))]) 89 (SignExt8to64 (Const8 [c])) -> (Const64 [int64( int8(c))]) 90 (SignExt16to32 (Const16 [c])) -> (Const32 [int64( int16(c))]) 91 (SignExt16to64 (Const16 [c])) -> (Const64 [int64( int16(c))]) 92 (SignExt32to64 (Const32 [c])) -> (Const64 [int64( int32(c))]) 93 94 (Neg8 (Const8 [c])) -> (Const8 [int64( -int8(c))]) 95 (Neg16 (Const16 [c])) -> (Const16 [int64(-int16(c))]) 96 (Neg32 (Const32 [c])) -> (Const32 [int64(-int32(c))]) 97 (Neg64 (Const64 [c])) -> (Const64 [-c]) 98 (Neg32F (Const32F [c])) && auxTo32F(c) != 0 -> (Const32F [auxFrom32F(-auxTo32F(c))]) 99 (Neg64F (Const64F [c])) && auxTo64F(c) != 0 -> (Const64F [auxFrom64F(-auxTo64F(c))]) 100 101 (Add8 (Const8 [c]) (Const8 [d])) -> (Const8 [int64(int8(c+d))]) 102 (Add16 (Const16 [c]) (Const16 [d])) -> (Const16 [int64(int16(c+d))]) 103 (Add32 (Const32 [c]) (Const32 [d])) -> (Const32 [int64(int32(c+d))]) 104 (Add64 (Const64 [c]) (Const64 [d])) -> (Const64 [c+d]) 105 (Add32F (Const32F [c]) (Const32F [d])) -> (Const32F [auxFrom32F(auxTo32F(c) + auxTo32F(d))]) 106 (Add64F (Const64F [c]) (Const64F [d])) -> (Const64F [auxFrom64F(auxTo64F(c) + auxTo64F(d))]) 107 (AddPtr <t> x (Const64 [c])) -> (OffPtr <t> x [c]) 108 (AddPtr <t> x (Const32 [c])) -> (OffPtr <t> x [c]) 109 110 (Sub8 (Const8 [c]) (Const8 [d])) -> (Const8 [int64(int8(c-d))]) 111 (Sub16 (Const16 [c]) (Const16 [d])) -> (Const16 [int64(int16(c-d))]) 112 (Sub32 (Const32 [c]) (Const32 [d])) -> (Const32 [int64(int32(c-d))]) 113 (Sub64 (Const64 [c]) (Const64 [d])) -> (Const64 [c-d]) 114 (Sub32F (Const32F [c]) (Const32F [d])) -> (Const32F [auxFrom32F(auxTo32F(c) - auxTo32F(d))]) 115 (Sub64F (Const64F [c]) (Const64F [d])) -> (Const64F [auxFrom64F(auxTo64F(c) - auxTo64F(d))]) 116 117 (Mul8 (Const8 [c]) (Const8 [d])) -> (Const8 [int64(int8(c*d))]) 118 (Mul16 (Const16 [c]) (Const16 [d])) -> (Const16 [int64(int16(c*d))]) 119 (Mul32 (Const32 [c]) (Const32 [d])) -> (Const32 [int64(int32(c*d))]) 120 (Mul64 (Const64 [c]) (Const64 [d])) -> (Const64 [c*d]) 121 (Mul32F (Const32F [c]) (Const32F [d])) -> (Const32F [auxFrom32F(auxTo32F(c) * auxTo32F(d))]) 122 (Mul64F (Const64F [c]) (Const64F [d])) -> (Const64F [auxFrom64F(auxTo64F(c) * auxTo64F(d))]) 123 124 (And8 (Const8 [c]) (Const8 [d])) -> (Const8 [int64(int8(c&d))]) 125 (And16 (Const16 [c]) (Const16 [d])) -> (Const16 [int64(int16(c&d))]) 126 (And32 (Const32 [c]) (Const32 [d])) -> (Const32 [int64(int32(c&d))]) 127 (And64 (Const64 [c]) (Const64 [d])) -> (Const64 [c&d]) 128 129 (Or8 (Const8 [c]) (Const8 [d])) -> (Const8 [int64(int8(c|d))]) 130 (Or16 (Const16 [c]) (Const16 [d])) -> (Const16 [int64(int16(c|d))]) 131 (Or32 (Const32 [c]) (Const32 [d])) -> (Const32 [int64(int32(c|d))]) 132 (Or64 (Const64 [c]) (Const64 [d])) -> (Const64 [c|d]) 133 134 (Xor8 (Const8 [c]) (Const8 [d])) -> (Const8 [int64(int8(c^d))]) 135 (Xor16 (Const16 [c]) (Const16 [d])) -> (Const16 [int64(int16(c^d))]) 136 (Xor32 (Const32 [c]) (Const32 [d])) -> (Const32 [int64(int32(c^d))]) 137 (Xor64 (Const64 [c]) (Const64 [d])) -> (Const64 [c^d]) 138 139 (Div8 (Const8 [c]) (Const8 [d])) && d != 0 -> (Const8 [int64(int8(c)/int8(d))]) 140 (Div16 (Const16 [c]) (Const16 [d])) && d != 0 -> (Const16 [int64(int16(c)/int16(d))]) 141 (Div32 (Const32 [c]) (Const32 [d])) && d != 0 -> (Const32 [int64(int32(c)/int32(d))]) 142 (Div64 (Const64 [c]) (Const64 [d])) && d != 0 -> (Const64 [c/d]) 143 (Div8u (Const8 [c]) (Const8 [d])) && d != 0 -> (Const8 [int64(int8(uint8(c)/uint8(d)))]) 144 (Div16u (Const16 [c]) (Const16 [d])) && d != 0 -> (Const16 [int64(int16(uint16(c)/uint16(d)))]) 145 (Div32u (Const32 [c]) (Const32 [d])) && d != 0 -> (Const32 [int64(int32(uint32(c)/uint32(d)))]) 146 (Div64u (Const64 [c]) (Const64 [d])) && d != 0 -> (Const64 [int64(uint64(c)/uint64(d))]) 147 (Div32F (Const32F [c]) (Const32F [d])) -> (Const32F [auxFrom32F(auxTo32F(c) / auxTo32F(d))]) 148 (Div64F (Const64F [c]) (Const64F [d])) -> (Const64F [auxFrom64F(auxTo64F(c) / auxTo64F(d))]) 149 150 (Not (ConstBool [c])) -> (ConstBool [1-c]) 151 152 // Convert x * 1 to x. 153 (Mul(8|16|32|64) (Const(8|16|32|64) [1]) x) -> x 154 155 // Convert x * -1 to -x. 156 (Mul(8|16|32|64) (Const(8|16|32|64) [-1]) x) -> (Neg(8|16|32|64) x) 157 158 // Convert multiplication by a power of two to a shift. 159 (Mul8 <t> n (Const8 [c])) && isPowerOfTwo(c) -> (Lsh8x64 <t> n (Const64 <typ.UInt64> [log2(c)])) 160 (Mul16 <t> n (Const16 [c])) && isPowerOfTwo(c) -> (Lsh16x64 <t> n (Const64 <typ.UInt64> [log2(c)])) 161 (Mul32 <t> n (Const32 [c])) && isPowerOfTwo(c) -> (Lsh32x64 <t> n (Const64 <typ.UInt64> [log2(c)])) 162 (Mul64 <t> n (Const64 [c])) && isPowerOfTwo(c) -> (Lsh64x64 <t> n (Const64 <typ.UInt64> [log2(c)])) 163 (Mul8 <t> n (Const8 [c])) && t.IsSigned() && isPowerOfTwo(-c) -> (Neg8 (Lsh8x64 <t> n (Const64 <typ.UInt64> [log2(-c)]))) 164 (Mul16 <t> n (Const16 [c])) && t.IsSigned() && isPowerOfTwo(-c) -> (Neg16 (Lsh16x64 <t> n (Const64 <typ.UInt64> [log2(-c)]))) 165 (Mul32 <t> n (Const32 [c])) && t.IsSigned() && isPowerOfTwo(-c) -> (Neg32 (Lsh32x64 <t> n (Const64 <typ.UInt64> [log2(-c)]))) 166 (Mul64 <t> n (Const64 [c])) && t.IsSigned() && isPowerOfTwo(-c) -> (Neg64 (Lsh64x64 <t> n (Const64 <typ.UInt64> [log2(-c)]))) 167 168 (Mod8 (Const8 [c]) (Const8 [d])) && d != 0 -> (Const8 [int64(int8(c % d))]) 169 (Mod16 (Const16 [c]) (Const16 [d])) && d != 0 -> (Const16 [int64(int16(c % d))]) 170 (Mod32 (Const32 [c]) (Const32 [d])) && d != 0 -> (Const32 [int64(int32(c % d))]) 171 (Mod64 (Const64 [c]) (Const64 [d])) && d != 0 -> (Const64 [c % d]) 172 173 (Mod8u (Const8 [c]) (Const8 [d])) && d != 0 -> (Const8 [int64(uint8(c) % uint8(d))]) 174 (Mod16u (Const16 [c]) (Const16 [d])) && d != 0 -> (Const16 [int64(uint16(c) % uint16(d))]) 175 (Mod32u (Const32 [c]) (Const32 [d])) && d != 0 -> (Const32 [int64(uint32(c) % uint32(d))]) 176 (Mod64u (Const64 [c]) (Const64 [d])) && d != 0 -> (Const64 [int64(uint64(c) % uint64(d))]) 177 178 (Lsh64x64 (Const64 [c]) (Const64 [d])) -> (Const64 [c << uint64(d)]) 179 (Rsh64x64 (Const64 [c]) (Const64 [d])) -> (Const64 [c >> uint64(d)]) 180 (Rsh64Ux64 (Const64 [c]) (Const64 [d])) -> (Const64 [int64(uint64(c) >> uint64(d))]) 181 (Lsh32x64 (Const32 [c]) (Const64 [d])) -> (Const32 [int64(int32(c) << uint64(d))]) 182 (Rsh32x64 (Const32 [c]) (Const64 [d])) -> (Const32 [int64(int32(c) >> uint64(d))]) 183 (Rsh32Ux64 (Const32 [c]) (Const64 [d])) -> (Const32 [int64(int32(uint32(c) >> uint64(d)))]) 184 (Lsh16x64 (Const16 [c]) (Const64 [d])) -> (Const16 [int64(int16(c) << uint64(d))]) 185 (Rsh16x64 (Const16 [c]) (Const64 [d])) -> (Const16 [int64(int16(c) >> uint64(d))]) 186 (Rsh16Ux64 (Const16 [c]) (Const64 [d])) -> (Const16 [int64(int16(uint16(c) >> uint64(d)))]) 187 (Lsh8x64 (Const8 [c]) (Const64 [d])) -> (Const8 [int64(int8(c) << uint64(d))]) 188 (Rsh8x64 (Const8 [c]) (Const64 [d])) -> (Const8 [int64(int8(c) >> uint64(d))]) 189 (Rsh8Ux64 (Const8 [c]) (Const64 [d])) -> (Const8 [int64(int8(uint8(c) >> uint64(d)))]) 190 191 // Fold IsInBounds when the range of the index cannot exceed the limit. 192 (IsInBounds (ZeroExt8to32 _) (Const32 [c])) && (1 << 8) <= c -> (ConstBool [1]) 193 (IsInBounds (ZeroExt8to64 _) (Const64 [c])) && (1 << 8) <= c -> (ConstBool [1]) 194 (IsInBounds (ZeroExt16to32 _) (Const32 [c])) && (1 << 16) <= c -> (ConstBool [1]) 195 (IsInBounds (ZeroExt16to64 _) (Const64 [c])) && (1 << 16) <= c -> (ConstBool [1]) 196 (IsInBounds x x) -> (ConstBool [0]) 197 (IsInBounds (And8 (Const8 [c]) _) (Const8 [d])) && 0 <= c && c < d -> (ConstBool [1]) 198 (IsInBounds (ZeroExt8to16 (And8 (Const8 [c]) _)) (Const16 [d])) && 0 <= c && c < d -> (ConstBool [1]) 199 (IsInBounds (ZeroExt8to32 (And8 (Const8 [c]) _)) (Const32 [d])) && 0 <= c && c < d -> (ConstBool [1]) 200 (IsInBounds (ZeroExt8to64 (And8 (Const8 [c]) _)) (Const64 [d])) && 0 <= c && c < d -> (ConstBool [1]) 201 (IsInBounds (And16 (Const16 [c]) _) (Const16 [d])) && 0 <= c && c < d -> (ConstBool [1]) 202 (IsInBounds (ZeroExt16to32 (And16 (Const16 [c]) _)) (Const32 [d])) && 0 <= c && c < d -> (ConstBool [1]) 203 (IsInBounds (ZeroExt16to64 (And16 (Const16 [c]) _)) (Const64 [d])) && 0 <= c && c < d -> (ConstBool [1]) 204 (IsInBounds (And32 (Const32 [c]) _) (Const32 [d])) && 0 <= c && c < d -> (ConstBool [1]) 205 (IsInBounds (ZeroExt32to64 (And32 (Const32 [c]) _)) (Const64 [d])) && 0 <= c && c < d -> (ConstBool [1]) 206 (IsInBounds (And64 (Const64 [c]) _) (Const64 [d])) && 0 <= c && c < d -> (ConstBool [1]) 207 (IsInBounds (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(0 <= c && c < d)]) 208 (IsInBounds (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(0 <= c && c < d)]) 209 // (Mod64u x y) is always between 0 (inclusive) and y (exclusive). 210 (IsInBounds (Mod32u _ y) y) -> (ConstBool [1]) 211 (IsInBounds (Mod64u _ y) y) -> (ConstBool [1]) 212 // Right shifting an unsigned number limits its value. 213 (IsInBounds (ZeroExt8to64 (Rsh8Ux64 _ (Const64 [c]))) (Const64 [d])) && 0 < c && c < 8 && 1<<uint( 8-c)-1 < d -> (ConstBool [1]) 214 (IsInBounds (ZeroExt8to32 (Rsh8Ux64 _ (Const64 [c]))) (Const32 [d])) && 0 < c && c < 8 && 1<<uint( 8-c)-1 < d -> (ConstBool [1]) 215 (IsInBounds (ZeroExt8to16 (Rsh8Ux64 _ (Const64 [c]))) (Const16 [d])) && 0 < c && c < 8 && 1<<uint( 8-c)-1 < d -> (ConstBool [1]) 216 (IsInBounds (Rsh8Ux64 _ (Const64 [c])) (Const64 [d])) && 0 < c && c < 8 && 1<<uint( 8-c)-1 < d -> (ConstBool [1]) 217 (IsInBounds (ZeroExt16to64 (Rsh16Ux64 _ (Const64 [c]))) (Const64 [d])) && 0 < c && c < 16 && 1<<uint(16-c)-1 < d -> (ConstBool [1]) 218 (IsInBounds (ZeroExt16to32 (Rsh16Ux64 _ (Const64 [c]))) (Const64 [d])) && 0 < c && c < 16 && 1<<uint(16-c)-1 < d -> (ConstBool [1]) 219 (IsInBounds (Rsh16Ux64 _ (Const64 [c])) (Const64 [d])) && 0 < c && c < 16 && 1<<uint(16-c)-1 < d -> (ConstBool [1]) 220 (IsInBounds (ZeroExt32to64 (Rsh32Ux64 _ (Const64 [c]))) (Const64 [d])) && 0 < c && c < 32 && 1<<uint(32-c)-1 < d -> (ConstBool [1]) 221 (IsInBounds (Rsh32Ux64 _ (Const64 [c])) (Const64 [d])) && 0 < c && c < 32 && 1<<uint(32-c)-1 < d -> (ConstBool [1]) 222 (IsInBounds (Rsh64Ux64 _ (Const64 [c])) (Const64 [d])) && 0 < c && c < 64 && 1<<uint(64-c)-1 < d -> (ConstBool [1]) 223 224 (IsSliceInBounds x x) -> (ConstBool [1]) 225 (IsSliceInBounds (And32 (Const32 [c]) _) (Const32 [d])) && 0 <= c && c <= d -> (ConstBool [1]) 226 (IsSliceInBounds (And64 (Const64 [c]) _) (Const64 [d])) && 0 <= c && c <= d -> (ConstBool [1]) 227 (IsSliceInBounds (Const32 [0]) _) -> (ConstBool [1]) 228 (IsSliceInBounds (Const64 [0]) _) -> (ConstBool [1]) 229 (IsSliceInBounds (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(0 <= c && c <= d)]) 230 (IsSliceInBounds (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(0 <= c && c <= d)]) 231 (IsSliceInBounds (SliceLen x) (SliceCap x)) -> (ConstBool [1]) 232 233 (Eq(64|32|16|8) x x) -> (ConstBool [1]) 234 (EqB (ConstBool [c]) (ConstBool [d])) -> (ConstBool [b2i(c == d)]) 235 (EqB (ConstBool [0]) x) -> (Not x) 236 (EqB (ConstBool [1]) x) -> x 237 238 (Neq(64|32|16|8) x x) -> (ConstBool [0]) 239 (NeqB (ConstBool [c]) (ConstBool [d])) -> (ConstBool [b2i(c != d)]) 240 (NeqB (ConstBool [0]) x) -> x 241 (NeqB (ConstBool [1]) x) -> (Not x) 242 (NeqB (Not x) (Not y)) -> (NeqB x y) 243 244 (Eq64 (Const64 <t> [c]) (Add64 (Const64 <t> [d]) x)) -> (Eq64 (Const64 <t> [c-d]) x) 245 (Eq32 (Const32 <t> [c]) (Add32 (Const32 <t> [d]) x)) -> (Eq32 (Const32 <t> [int64(int32(c-d))]) x) 246 (Eq16 (Const16 <t> [c]) (Add16 (Const16 <t> [d]) x)) -> (Eq16 (Const16 <t> [int64(int16(c-d))]) x) 247 (Eq8 (Const8 <t> [c]) (Add8 (Const8 <t> [d]) x)) -> (Eq8 (Const8 <t> [int64(int8(c-d))]) x) 248 249 (Neq64 (Const64 <t> [c]) (Add64 (Const64 <t> [d]) x)) -> (Neq64 (Const64 <t> [c-d]) x) 250 (Neq32 (Const32 <t> [c]) (Add32 (Const32 <t> [d]) x)) -> (Neq32 (Const32 <t> [int64(int32(c-d))]) x) 251 (Neq16 (Const16 <t> [c]) (Add16 (Const16 <t> [d]) x)) -> (Neq16 (Const16 <t> [int64(int16(c-d))]) x) 252 (Neq8 (Const8 <t> [c]) (Add8 (Const8 <t> [d]) x)) -> (Neq8 (Const8 <t> [int64(int8(c-d))]) x) 253 254 // Canonicalize x-const to x+(-const) 255 (Sub64 x (Const64 <t> [c])) && x.Op != OpConst64 -> (Add64 (Const64 <t> [-c]) x) 256 (Sub32 x (Const32 <t> [c])) && x.Op != OpConst32 -> (Add32 (Const32 <t> [int64(int32(-c))]) x) 257 (Sub16 x (Const16 <t> [c])) && x.Op != OpConst16 -> (Add16 (Const16 <t> [int64(int16(-c))]) x) 258 (Sub8 x (Const8 <t> [c])) && x.Op != OpConst8 -> (Add8 (Const8 <t> [int64(int8(-c))]) x) 259 260 // fold negation into comparison operators 261 (Not (Eq(64|32|16|8|B) x y)) -> (Neq(64|32|16|8|B) x y) 262 (Not (Neq(64|32|16|8|B) x y)) -> (Eq(64|32|16|8|B) x y) 263 264 (Not (Greater(64|32|16|8) x y)) -> (Leq(64|32|16|8) x y) 265 (Not (Greater(64|32|16|8)U x y)) -> (Leq(64|32|16|8)U x y) 266 (Not (Geq(64|32|16|8) x y)) -> (Less(64|32|16|8) x y) 267 (Not (Geq(64|32|16|8)U x y)) -> (Less(64|32|16|8)U x y) 268 269 (Not (Less(64|32|16|8) x y)) -> (Geq(64|32|16|8) x y) 270 (Not (Less(64|32|16|8)U x y)) -> (Geq(64|32|16|8)U x y) 271 (Not (Leq(64|32|16|8) x y)) -> (Greater(64|32|16|8) x y) 272 (Not (Leq(64|32|16|8)U x y)) -> (Greater(64|32|16|8)U x y) 273 274 275 // Distribute multiplication c * (d+x) -> c*d + c*x. Useful for: 276 // a[i].b = ...; a[i+1].b = ... 277 (Mul64 (Const64 <t> [c]) (Add64 <t> (Const64 <t> [d]) x)) -> 278 (Add64 (Const64 <t> [c*d]) (Mul64 <t> (Const64 <t> [c]) x)) 279 (Mul32 (Const32 <t> [c]) (Add32 <t> (Const32 <t> [d]) x)) -> 280 (Add32 (Const32 <t> [int64(int32(c*d))]) (Mul32 <t> (Const32 <t> [c]) x)) 281 282 // Rewrite x*y ± x*z to x*(y±z) 283 (Add(64|32|16|8) <t> (Mul(64|32|16|8) x y) (Mul(64|32|16|8) x z)) 284 -> (Mul(64|32|16|8) x (Add(64|32|16|8) <t> y z)) 285 (Sub(64|32|16|8) <t> (Mul(64|32|16|8) x y) (Mul(64|32|16|8) x z)) 286 -> (Mul(64|32|16|8) x (Sub(64|32|16|8) <t> y z)) 287 288 // rewrite shifts of 8/16/32 bit consts into 64 bit consts to reduce 289 // the number of the other rewrite rules for const shifts 290 (Lsh64x32 <t> x (Const32 [c])) -> (Lsh64x64 x (Const64 <t> [int64(uint32(c))])) 291 (Lsh64x16 <t> x (Const16 [c])) -> (Lsh64x64 x (Const64 <t> [int64(uint16(c))])) 292 (Lsh64x8 <t> x (Const8 [c])) -> (Lsh64x64 x (Const64 <t> [int64(uint8(c))])) 293 (Rsh64x32 <t> x (Const32 [c])) -> (Rsh64x64 x (Const64 <t> [int64(uint32(c))])) 294 (Rsh64x16 <t> x (Const16 [c])) -> (Rsh64x64 x (Const64 <t> [int64(uint16(c))])) 295 (Rsh64x8 <t> x (Const8 [c])) -> (Rsh64x64 x (Const64 <t> [int64(uint8(c))])) 296 (Rsh64Ux32 <t> x (Const32 [c])) -> (Rsh64Ux64 x (Const64 <t> [int64(uint32(c))])) 297 (Rsh64Ux16 <t> x (Const16 [c])) -> (Rsh64Ux64 x (Const64 <t> [int64(uint16(c))])) 298 (Rsh64Ux8 <t> x (Const8 [c])) -> (Rsh64Ux64 x (Const64 <t> [int64(uint8(c))])) 299 300 (Lsh32x32 <t> x (Const32 [c])) -> (Lsh32x64 x (Const64 <t> [int64(uint32(c))])) 301 (Lsh32x16 <t> x (Const16 [c])) -> (Lsh32x64 x (Const64 <t> [int64(uint16(c))])) 302 (Lsh32x8 <t> x (Const8 [c])) -> (Lsh32x64 x (Const64 <t> [int64(uint8(c))])) 303 (Rsh32x32 <t> x (Const32 [c])) -> (Rsh32x64 x (Const64 <t> [int64(uint32(c))])) 304 (Rsh32x16 <t> x (Const16 [c])) -> (Rsh32x64 x (Const64 <t> [int64(uint16(c))])) 305 (Rsh32x8 <t> x (Const8 [c])) -> (Rsh32x64 x (Const64 <t> [int64(uint8(c))])) 306 (Rsh32Ux32 <t> x (Const32 [c])) -> (Rsh32Ux64 x (Const64 <t> [int64(uint32(c))])) 307 (Rsh32Ux16 <t> x (Const16 [c])) -> (Rsh32Ux64 x (Const64 <t> [int64(uint16(c))])) 308 (Rsh32Ux8 <t> x (Const8 [c])) -> (Rsh32Ux64 x (Const64 <t> [int64(uint8(c))])) 309 310 (Lsh16x32 <t> x (Const32 [c])) -> (Lsh16x64 x (Const64 <t> [int64(uint32(c))])) 311 (Lsh16x16 <t> x (Const16 [c])) -> (Lsh16x64 x (Const64 <t> [int64(uint16(c))])) 312 (Lsh16x8 <t> x (Const8 [c])) -> (Lsh16x64 x (Const64 <t> [int64(uint8(c))])) 313 (Rsh16x32 <t> x (Const32 [c])) -> (Rsh16x64 x (Const64 <t> [int64(uint32(c))])) 314 (Rsh16x16 <t> x (Const16 [c])) -> (Rsh16x64 x (Const64 <t> [int64(uint16(c))])) 315 (Rsh16x8 <t> x (Const8 [c])) -> (Rsh16x64 x (Const64 <t> [int64(uint8(c))])) 316 (Rsh16Ux32 <t> x (Const32 [c])) -> (Rsh16Ux64 x (Const64 <t> [int64(uint32(c))])) 317 (Rsh16Ux16 <t> x (Const16 [c])) -> (Rsh16Ux64 x (Const64 <t> [int64(uint16(c))])) 318 (Rsh16Ux8 <t> x (Const8 [c])) -> (Rsh16Ux64 x (Const64 <t> [int64(uint8(c))])) 319 320 (Lsh8x32 <t> x (Const32 [c])) -> (Lsh8x64 x (Const64 <t> [int64(uint32(c))])) 321 (Lsh8x16 <t> x (Const16 [c])) -> (Lsh8x64 x (Const64 <t> [int64(uint16(c))])) 322 (Lsh8x8 <t> x (Const8 [c])) -> (Lsh8x64 x (Const64 <t> [int64(uint8(c))])) 323 (Rsh8x32 <t> x (Const32 [c])) -> (Rsh8x64 x (Const64 <t> [int64(uint32(c))])) 324 (Rsh8x16 <t> x (Const16 [c])) -> (Rsh8x64 x (Const64 <t> [int64(uint16(c))])) 325 (Rsh8x8 <t> x (Const8 [c])) -> (Rsh8x64 x (Const64 <t> [int64(uint8(c))])) 326 (Rsh8Ux32 <t> x (Const32 [c])) -> (Rsh8Ux64 x (Const64 <t> [int64(uint32(c))])) 327 (Rsh8Ux16 <t> x (Const16 [c])) -> (Rsh8Ux64 x (Const64 <t> [int64(uint16(c))])) 328 (Rsh8Ux8 <t> x (Const8 [c])) -> (Rsh8Ux64 x (Const64 <t> [int64(uint8(c))])) 329 330 // shifts by zero 331 (Lsh(64|32|16|8)x64 x (Const64 [0])) -> x 332 (Rsh(64|32|16|8)x64 x (Const64 [0])) -> x 333 (Rsh(64|32|16|8)Ux64 x (Const64 [0])) -> x 334 335 // zero shifted 336 (Lsh64x(64|32|16|8) (Const64 [0]) _) -> (Const64 [0]) 337 (Rsh64x(64|32|16|8) (Const64 [0]) _) -> (Const64 [0]) 338 (Rsh64Ux(64|32|16|8) (Const64 [0]) _) -> (Const64 [0]) 339 (Lsh32x(64|32|16|8) (Const32 [0]) _) -> (Const32 [0]) 340 (Rsh32x(64|32|16|8) (Const32 [0]) _) -> (Const32 [0]) 341 (Rsh32Ux(64|32|16|8) (Const32 [0]) _) -> (Const32 [0]) 342 (Lsh16x(64|32|16|8) (Const16 [0]) _) -> (Const16 [0]) 343 (Rsh16x(64|32|16|8) (Const16 [0]) _) -> (Const16 [0]) 344 (Rsh16Ux(64|32|16|8) (Const16 [0]) _) -> (Const16 [0]) 345 (Lsh8x(64|32|16|8) (Const8 [0]) _) -> (Const8 [0]) 346 (Rsh8x(64|32|16|8) (Const8 [0]) _) -> (Const8 [0]) 347 (Rsh8Ux(64|32|16|8) (Const8 [0]) _) -> (Const8 [0]) 348 349 // large left shifts of all values, and right shifts of unsigned values 350 ((Lsh64|Rsh64U)x64 _ (Const64 [c])) && uint64(c) >= 64 -> (Const64 [0]) 351 ((Lsh32|Rsh32U)x64 _ (Const64 [c])) && uint64(c) >= 32 -> (Const32 [0]) 352 ((Lsh16|Rsh16U)x64 _ (Const64 [c])) && uint64(c) >= 16 -> (Const16 [0]) 353 ((Lsh8|Rsh8U)x64 _ (Const64 [c])) && uint64(c) >= 8 -> (Const8 [0]) 354 355 // combine const shifts 356 (Lsh64x64 <t> (Lsh64x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Lsh64x64 x (Const64 <t> [c+d])) 357 (Lsh32x64 <t> (Lsh32x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Lsh32x64 x (Const64 <t> [c+d])) 358 (Lsh16x64 <t> (Lsh16x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Lsh16x64 x (Const64 <t> [c+d])) 359 (Lsh8x64 <t> (Lsh8x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Lsh8x64 x (Const64 <t> [c+d])) 360 361 (Rsh64x64 <t> (Rsh64x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh64x64 x (Const64 <t> [c+d])) 362 (Rsh32x64 <t> (Rsh32x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh32x64 x (Const64 <t> [c+d])) 363 (Rsh16x64 <t> (Rsh16x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh16x64 x (Const64 <t> [c+d])) 364 (Rsh8x64 <t> (Rsh8x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh8x64 x (Const64 <t> [c+d])) 365 366 (Rsh64Ux64 <t> (Rsh64Ux64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh64Ux64 x (Const64 <t> [c+d])) 367 (Rsh32Ux64 <t> (Rsh32Ux64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh32Ux64 x (Const64 <t> [c+d])) 368 (Rsh16Ux64 <t> (Rsh16Ux64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh16Ux64 x (Const64 <t> [c+d])) 369 (Rsh8Ux64 <t> (Rsh8Ux64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh8Ux64 x (Const64 <t> [c+d])) 370 371 // Remove signed right shift before an unsigned right shift that extracts the sign bit. 372 (Rsh8Ux64 (Rsh8x64 x _) (Const64 <t> [7] )) -> (Rsh8Ux64 x (Const64 <t> [7] )) 373 (Rsh16Ux64 (Rsh16x64 x _) (Const64 <t> [15])) -> (Rsh16Ux64 x (Const64 <t> [15])) 374 (Rsh32Ux64 (Rsh32x64 x _) (Const64 <t> [31])) -> (Rsh32Ux64 x (Const64 <t> [31])) 375 (Rsh64Ux64 (Rsh64x64 x _) (Const64 <t> [63])) -> (Rsh64Ux64 x (Const64 <t> [63])) 376 377 // ((x >> c1) << c2) >> c3 378 (Rsh(64|32|16|8)Ux64 (Lsh(64|32|16|8)x64 (Rsh(64|32|16|8)Ux64 x (Const64 [c1])) (Const64 [c2])) (Const64 [c3])) 379 && uint64(c1) >= uint64(c2) && uint64(c3) >= uint64(c2) && !uaddOvf(c1-c2, c3) 380 -> (Rsh(64|32|16|8)Ux64 x (Const64 <typ.UInt64> [c1-c2+c3])) 381 382 // ((x << c1) >> c2) << c3 383 (Lsh(64|32|16|8)x64 (Rsh(64|32|16|8)Ux64 (Lsh(64|32|16|8)x64 x (Const64 [c1])) (Const64 [c2])) (Const64 [c3])) 384 && uint64(c1) >= uint64(c2) && uint64(c3) >= uint64(c2) && !uaddOvf(c1-c2, c3) 385 -> (Lsh(64|32|16|8)x64 x (Const64 <typ.UInt64> [c1-c2+c3])) 386 387 // (x >> c) & uppermask = 0 388 (And64 (Const64 [m]) (Rsh64Ux64 _ (Const64 [c]))) && c >= 64-ntz(m) -> (Const64 [0]) 389 (And32 (Const32 [m]) (Rsh32Ux64 _ (Const64 [c]))) && c >= 64-ntz(m) -> (Const32 [0]) 390 (And16 (Const16 [m]) (Rsh16Ux64 _ (Const64 [c]))) && c >= 64-ntz(m) -> (Const16 [0]) 391 (And8 (Const8 [m]) (Rsh8Ux64 _ (Const64 [c]))) && c >= 64-ntz(m) -> (Const8 [0]) 392 393 // (x << c) & lowermask = 0 394 (And64 (Const64 [m]) (Lsh64x64 _ (Const64 [c]))) && c >= 64-nlz(m) -> (Const64 [0]) 395 (And32 (Const32 [m]) (Lsh32x64 _ (Const64 [c]))) && c >= 64-nlz(m) -> (Const32 [0]) 396 (And16 (Const16 [m]) (Lsh16x64 _ (Const64 [c]))) && c >= 64-nlz(m) -> (Const16 [0]) 397 (And8 (Const8 [m]) (Lsh8x64 _ (Const64 [c]))) && c >= 64-nlz(m) -> (Const8 [0]) 398 399 // replace shifts with zero extensions 400 (Rsh16Ux64 (Lsh16x64 x (Const64 [8])) (Const64 [8])) -> (ZeroExt8to16 (Trunc16to8 <typ.UInt8> x)) 401 (Rsh32Ux64 (Lsh32x64 x (Const64 [24])) (Const64 [24])) -> (ZeroExt8to32 (Trunc32to8 <typ.UInt8> x)) 402 (Rsh64Ux64 (Lsh64x64 x (Const64 [56])) (Const64 [56])) -> (ZeroExt8to64 (Trunc64to8 <typ.UInt8> x)) 403 (Rsh32Ux64 (Lsh32x64 x (Const64 [16])) (Const64 [16])) -> (ZeroExt16to32 (Trunc32to16 <typ.UInt16> x)) 404 (Rsh64Ux64 (Lsh64x64 x (Const64 [48])) (Const64 [48])) -> (ZeroExt16to64 (Trunc64to16 <typ.UInt16> x)) 405 (Rsh64Ux64 (Lsh64x64 x (Const64 [32])) (Const64 [32])) -> (ZeroExt32to64 (Trunc64to32 <typ.UInt32> x)) 406 407 // replace shifts with sign extensions 408 (Rsh16x64 (Lsh16x64 x (Const64 [8])) (Const64 [8])) -> (SignExt8to16 (Trunc16to8 <typ.Int8> x)) 409 (Rsh32x64 (Lsh32x64 x (Const64 [24])) (Const64 [24])) -> (SignExt8to32 (Trunc32to8 <typ.Int8> x)) 410 (Rsh64x64 (Lsh64x64 x (Const64 [56])) (Const64 [56])) -> (SignExt8to64 (Trunc64to8 <typ.Int8> x)) 411 (Rsh32x64 (Lsh32x64 x (Const64 [16])) (Const64 [16])) -> (SignExt16to32 (Trunc32to16 <typ.Int16> x)) 412 (Rsh64x64 (Lsh64x64 x (Const64 [48])) (Const64 [48])) -> (SignExt16to64 (Trunc64to16 <typ.Int16> x)) 413 (Rsh64x64 (Lsh64x64 x (Const64 [32])) (Const64 [32])) -> (SignExt32to64 (Trunc64to32 <typ.Int32> x)) 414 415 // constant comparisons 416 (Eq(64|32|16|8) (Const(64|32|16|8) [c]) (Const(64|32|16|8) [d])) -> (ConstBool [b2i(c == d)]) 417 (Neq(64|32|16|8) (Const(64|32|16|8) [c]) (Const(64|32|16|8) [d])) -> (ConstBool [b2i(c != d)]) 418 (Greater(64|32|16|8) (Const(64|32|16|8) [c]) (Const(64|32|16|8) [d])) -> (ConstBool [b2i(c > d)]) 419 (Geq(64|32|16|8) (Const(64|32|16|8) [c]) (Const(64|32|16|8) [d])) -> (ConstBool [b2i(c >= d)]) 420 (Less(64|32|16|8) (Const(64|32|16|8) [c]) (Const(64|32|16|8) [d])) -> (ConstBool [b2i(c < d)]) 421 (Leq(64|32|16|8) (Const(64|32|16|8) [c]) (Const(64|32|16|8) [d])) -> (ConstBool [b2i(c <= d)]) 422 423 (Greater64U (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(uint64(c) > uint64(d))]) 424 (Greater32U (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(uint32(c) > uint32(d))]) 425 (Greater16U (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(uint16(c) > uint16(d))]) 426 (Greater8U (Const8 [c]) (Const8 [d])) -> (ConstBool [b2i(uint8(c) > uint8(d))]) 427 428 (Geq64U (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(uint64(c) >= uint64(d))]) 429 (Geq32U (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(uint32(c) >= uint32(d))]) 430 (Geq16U (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(uint16(c) >= uint16(d))]) 431 (Geq8U (Const8 [c]) (Const8 [d])) -> (ConstBool [b2i(uint8(c) >= uint8(d))]) 432 433 (Less64U (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(uint64(c) < uint64(d))]) 434 (Less32U (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(uint32(c) < uint32(d))]) 435 (Less16U (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(uint16(c) < uint16(d))]) 436 (Less8U (Const8 [c]) (Const8 [d])) -> (ConstBool [b2i(uint8(c) < uint8(d))]) 437 438 (Leq64U (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(uint64(c) <= uint64(d))]) 439 (Leq32U (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(uint32(c) <= uint32(d))]) 440 (Leq16U (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(uint16(c) <= uint16(d))]) 441 (Leq8U (Const8 [c]) (Const8 [d])) -> (ConstBool [b2i(uint8(c) <= uint8(d))]) 442 443 // constant floating point comparisons 444 (Eq32F (Const32F [c]) (Const32F [d])) -> (ConstBool [b2i(auxTo32F(c) == auxTo32F(d))]) 445 (Eq64F (Const64F [c]) (Const64F [d])) -> (ConstBool [b2i(auxTo64F(c) == auxTo64F(d))]) 446 (Neq32F (Const32F [c]) (Const32F [d])) -> (ConstBool [b2i(auxTo32F(c) != auxTo32F(d))]) 447 (Neq64F (Const64F [c]) (Const64F [d])) -> (ConstBool [b2i(auxTo64F(c) != auxTo64F(d))]) 448 (Greater32F (Const32F [c]) (Const32F [d])) -> (ConstBool [b2i(auxTo32F(c) > auxTo32F(d))]) 449 (Greater64F (Const64F [c]) (Const64F [d])) -> (ConstBool [b2i(auxTo64F(c) > auxTo64F(d))]) 450 (Geq32F (Const32F [c]) (Const32F [d])) -> (ConstBool [b2i(auxTo32F(c) >= auxTo32F(d))]) 451 (Geq64F (Const64F [c]) (Const64F [d])) -> (ConstBool [b2i(auxTo64F(c) >= auxTo64F(d))]) 452 (Less32F (Const32F [c]) (Const32F [d])) -> (ConstBool [b2i(auxTo32F(c) < auxTo32F(d))]) 453 (Less64F (Const64F [c]) (Const64F [d])) -> (ConstBool [b2i(auxTo64F(c) < auxTo64F(d))]) 454 (Leq32F (Const32F [c]) (Const32F [d])) -> (ConstBool [b2i(auxTo32F(c) <= auxTo32F(d))]) 455 (Leq64F (Const64F [c]) (Const64F [d])) -> (ConstBool [b2i(auxTo64F(c) <= auxTo64F(d))]) 456 457 // simplifications 458 (Or(64|32|16|8) x x) -> x 459 (Or(64|32|16|8) (Const(64|32|16|8) [0]) x) -> x 460 (Or(64|32|16|8) (Const(64|32|16|8) [-1]) _) -> (Const(64|32|16|8) [-1]) 461 462 (And(64|32|16|8) x x) -> x 463 (And(64|32|16|8) (Const(64|32|16|8) [-1]) x) -> x 464 (And(64|32|16|8) (Const(64|32|16|8) [0]) _) -> (Const(64|32|16|8) [0]) 465 466 (Xor(64|32|16|8) x x) -> (Const(64|32|16|8) [0]) 467 (Xor(64|32|16|8) (Const(64|32|16|8) [0]) x) -> x 468 469 (Add(64|32|16|8) (Const(64|32|16|8) [0]) x) -> x 470 (Sub(64|32|16|8) x x) -> (Const(64|32|16|8) [0]) 471 (Mul(64|32|16|8) (Const(64|32|16|8) [0]) _) -> (Const(64|32|16|8) [0]) 472 473 (Com(64|32|16|8) (Com(64|32|16|8) x)) -> x 474 (Com(64|32|16|8) (Const(64|32|16|8) [c])) -> (Const(64|32|16|8) [^c]) 475 476 (Neg(64|32|16|8) (Sub(64|32|16|8) x y)) -> (Sub(64|32|16|8) y x) 477 478 (Add(64|32|16|8) (Const(64|32|16|8) [1]) (Com(64|32|16|8) x)) -> (Neg(64|32|16|8) x) 479 480 (And(64|32|16|8) x (And(64|32|16|8) x y)) -> (And(64|32|16|8) x y) 481 (Or(64|32|16|8) x (Or(64|32|16|8) x y)) -> (Or(64|32|16|8) x y) 482 (Xor(64|32|16|8) x (Xor(64|32|16|8) x y)) -> y 483 484 // Ands clear bits. Ors set bits. 485 // If a subsequent Or will set all the bits 486 // that an And cleared, we can skip the And. 487 // This happens in bitmasking code like: 488 // x &^= 3 << shift // clear two old bits 489 // x |= v << shift // set two new bits 490 // when shift is a small constant and v ends up a constant 3. 491 (Or8 (And8 x (Const8 [c2])) (Const8 <t> [c1])) && ^(c1 | c2) == 0 -> (Or8 (Const8 <t> [c1]) x) 492 (Or16 (And16 x (Const16 [c2])) (Const16 <t> [c1])) && ^(c1 | c2) == 0 -> (Or16 (Const16 <t> [c1]) x) 493 (Or32 (And32 x (Const32 [c2])) (Const32 <t> [c1])) && ^(c1 | c2) == 0 -> (Or32 (Const32 <t> [c1]) x) 494 (Or64 (And64 x (Const64 [c2])) (Const64 <t> [c1])) && ^(c1 | c2) == 0 -> (Or64 (Const64 <t> [c1]) x) 495 496 (Trunc64to8 (And64 (Const64 [y]) x)) && y&0xFF == 0xFF -> (Trunc64to8 x) 497 (Trunc64to16 (And64 (Const64 [y]) x)) && y&0xFFFF == 0xFFFF -> (Trunc64to16 x) 498 (Trunc64to32 (And64 (Const64 [y]) x)) && y&0xFFFFFFFF == 0xFFFFFFFF -> (Trunc64to32 x) 499 (Trunc32to8 (And32 (Const32 [y]) x)) && y&0xFF == 0xFF -> (Trunc32to8 x) 500 (Trunc32to16 (And32 (Const32 [y]) x)) && y&0xFFFF == 0xFFFF -> (Trunc32to16 x) 501 (Trunc16to8 (And16 (Const16 [y]) x)) && y&0xFF == 0xFF -> (Trunc16to8 x) 502 503 (ZeroExt8to64 (Trunc64to8 x:(Rsh64Ux64 _ (Const64 [s])))) && s >= 56 -> x 504 (ZeroExt16to64 (Trunc64to16 x:(Rsh64Ux64 _ (Const64 [s])))) && s >= 48 -> x 505 (ZeroExt32to64 (Trunc64to32 x:(Rsh64Ux64 _ (Const64 [s])))) && s >= 32 -> x 506 (ZeroExt8to32 (Trunc32to8 x:(Rsh32Ux64 _ (Const64 [s])))) && s >= 24 -> x 507 (ZeroExt16to32 (Trunc32to16 x:(Rsh32Ux64 _ (Const64 [s])))) && s >= 16 -> x 508 (ZeroExt8to16 (Trunc16to8 x:(Rsh16Ux64 _ (Const64 [s])))) && s >= 8 -> x 509 510 (SignExt8to64 (Trunc64to8 x:(Rsh64x64 _ (Const64 [s])))) && s >= 56 -> x 511 (SignExt16to64 (Trunc64to16 x:(Rsh64x64 _ (Const64 [s])))) && s >= 48 -> x 512 (SignExt32to64 (Trunc64to32 x:(Rsh64x64 _ (Const64 [s])))) && s >= 32 -> x 513 (SignExt8to32 (Trunc32to8 x:(Rsh32x64 _ (Const64 [s])))) && s >= 24 -> x 514 (SignExt16to32 (Trunc32to16 x:(Rsh32x64 _ (Const64 [s])))) && s >= 16 -> x 515 (SignExt8to16 (Trunc16to8 x:(Rsh16x64 _ (Const64 [s])))) && s >= 8 -> x 516 517 (Slicemask (Const32 [x])) && x > 0 -> (Const32 [-1]) 518 (Slicemask (Const32 [0])) -> (Const32 [0]) 519 (Slicemask (Const64 [x])) && x > 0 -> (Const64 [-1]) 520 (Slicemask (Const64 [0])) -> (Const64 [0]) 521 522 // Rewrite AND of consts as shifts if possible, slightly faster for 64 bit operands 523 // leading zeros can be shifted left, then right 524 (And64 <t> (Const64 [y]) x) && nlz(y) + nto(y) == 64 && nto(y) >= 32 525 -> (Rsh64Ux64 (Lsh64x64 <t> x (Const64 <t> [nlz(y)])) (Const64 <t> [nlz(y)])) 526 // trailing zeros can be shifted right, then left 527 (And64 <t> (Const64 [y]) x) && nlo(y) + ntz(y) == 64 && ntz(y) >= 32 528 -> (Lsh64x64 (Rsh64Ux64 <t> x (Const64 <t> [ntz(y)])) (Const64 <t> [ntz(y)])) 529 530 // simplifications often used for lengths. e.g. len(s[i:i+5])==5 531 (Sub(64|32|16|8) (Add(64|32|16|8) x y) x) -> y 532 (Sub(64|32|16|8) (Add(64|32|16|8) x y) y) -> x 533 534 // basic phi simplifications 535 (Phi (Const8 [c]) (Const8 [c])) -> (Const8 [c]) 536 (Phi (Const16 [c]) (Const16 [c])) -> (Const16 [c]) 537 (Phi (Const32 [c]) (Const32 [c])) -> (Const32 [c]) 538 (Phi (Const64 [c]) (Const64 [c])) -> (Const64 [c]) 539 540 // slice and interface comparisons 541 // The frontend ensures that we can only compare against nil, 542 // so we need only compare the first word (interface type or slice ptr). 543 (EqInter x y) -> (EqPtr (ITab x) (ITab y)) 544 (NeqInter x y) -> (NeqPtr (ITab x) (ITab y)) 545 (EqSlice x y) -> (EqPtr (SlicePtr x) (SlicePtr y)) 546 (NeqSlice x y) -> (NeqPtr (SlicePtr x) (SlicePtr y)) 547 548 // Load of store of same address, with compatibly typed value and same size 549 (Load <t1> p1 (Store {t2} p2 x _)) 550 && isSamePtr(p1, p2) 551 && t1.Compare(x.Type) == types.CMPeq 552 && t1.Size() == sizeof(t2) 553 -> x 554 (Load <t1> p1 (Store {t2} p2 _ (Store {t3} p3 x _))) 555 && isSamePtr(p1, p3) 556 && t1.Compare(x.Type) == types.CMPeq 557 && t1.Size() == sizeof(t2) 558 && disjoint(p3, sizeof(t3), p2, sizeof(t2)) 559 -> x 560 (Load <t1> p1 (Store {t2} p2 _ (Store {t3} p3 _ (Store {t4} p4 x _)))) 561 && isSamePtr(p1, p4) 562 && t1.Compare(x.Type) == types.CMPeq 563 && t1.Size() == sizeof(t2) 564 && disjoint(p4, sizeof(t4), p2, sizeof(t2)) 565 && disjoint(p4, sizeof(t4), p3, sizeof(t3)) 566 -> x 567 (Load <t1> p1 (Store {t2} p2 _ (Store {t3} p3 _ (Store {t4} p4 _ (Store {t5} p5 x _))))) 568 && isSamePtr(p1, p5) 569 && t1.Compare(x.Type) == types.CMPeq 570 && t1.Size() == sizeof(t2) 571 && disjoint(p5, sizeof(t5), p2, sizeof(t2)) 572 && disjoint(p5, sizeof(t5), p3, sizeof(t3)) 573 && disjoint(p5, sizeof(t5), p4, sizeof(t4)) 574 -> x 575 576 // Pass constants through math.Float{32,64}bits and math.Float{32,64}frombits 577 (Load <t1> p1 (Store {t2} p2 (Const64 [x]) _)) && isSamePtr(p1,p2) && sizeof(t2) == 8 && is64BitFloat(t1) -> (Const64F [x]) 578 (Load <t1> p1 (Store {t2} p2 (Const32 [x]) _)) && isSamePtr(p1,p2) && sizeof(t2) == 4 && is32BitFloat(t1) -> (Const32F [auxFrom32F(math.Float32frombits(uint32(x)))]) 579 (Load <t1> p1 (Store {t2} p2 (Const64F [x]) _)) && isSamePtr(p1,p2) && sizeof(t2) == 8 && is64BitInt(t1) -> (Const64 [x]) 580 (Load <t1> p1 (Store {t2} p2 (Const32F [x]) _)) && isSamePtr(p1,p2) && sizeof(t2) == 4 && is32BitInt(t1) -> (Const32 [int64(int32(math.Float32bits(auxTo32F(x))))]) 581 582 // Float Loads up to Zeros so they can be constant folded. 583 (Load <t1> op:(OffPtr [o1] p1) 584 (Store {t2} p2 _ 585 mem:(Zero [n] p3 _))) 586 && o1 >= 0 && o1+t1.Size() <= n && isSamePtr(p1, p3) 587 && fe.CanSSA(t1) 588 && disjoint(op, t1.Size(), p2, sizeof(t2)) 589 -> @mem.Block (Load <t1> (OffPtr <op.Type> [o1] p3) mem) 590 (Load <t1> op:(OffPtr [o1] p1) 591 (Store {t2} p2 _ 592 (Store {t3} p3 _ 593 mem:(Zero [n] p4 _)))) 594 && o1 >= 0 && o1+t1.Size() <= n && isSamePtr(p1, p4) 595 && fe.CanSSA(t1) 596 && disjoint(op, t1.Size(), p2, sizeof(t2)) 597 && disjoint(op, t1.Size(), p3, sizeof(t3)) 598 -> @mem.Block (Load <t1> (OffPtr <op.Type> [o1] p4) mem) 599 (Load <t1> op:(OffPtr [o1] p1) 600 (Store {t2} p2 _ 601 (Store {t3} p3 _ 602 (Store {t4} p4 _ 603 mem:(Zero [n] p5 _))))) 604 && o1 >= 0 && o1+t1.Size() <= n && isSamePtr(p1, p5) 605 && fe.CanSSA(t1) 606 && disjoint(op, t1.Size(), p2, sizeof(t2)) 607 && disjoint(op, t1.Size(), p3, sizeof(t3)) 608 && disjoint(op, t1.Size(), p4, sizeof(t4)) 609 -> @mem.Block (Load <t1> (OffPtr <op.Type> [o1] p5) mem) 610 (Load <t1> op:(OffPtr [o1] p1) 611 (Store {t2} p2 _ 612 (Store {t3} p3 _ 613 (Store {t4} p4 _ 614 (Store {t5} p5 _ 615 mem:(Zero [n] p6 _)))))) 616 && o1 >= 0 && o1+t1.Size() <= n && isSamePtr(p1, p6) 617 && fe.CanSSA(t1) 618 && disjoint(op, t1.Size(), p2, sizeof(t2)) 619 && disjoint(op, t1.Size(), p3, sizeof(t3)) 620 && disjoint(op, t1.Size(), p4, sizeof(t4)) 621 && disjoint(op, t1.Size(), p5, sizeof(t5)) 622 -> @mem.Block (Load <t1> (OffPtr <op.Type> [o1] p6) mem) 623 624 // Zero to Load forwarding. 625 (Load <t1> (OffPtr [o] p1) (Zero [n] p2 _)) 626 && t1.IsBoolean() 627 && isSamePtr(p1, p2) 628 && n >= o + 1 629 -> (ConstBool [0]) 630 (Load <t1> (OffPtr [o] p1) (Zero [n] p2 _)) 631 && is8BitInt(t1) 632 && isSamePtr(p1, p2) 633 && n >= o + 1 634 -> (Const8 [0]) 635 (Load <t1> (OffPtr [o] p1) (Zero [n] p2 _)) 636 && is16BitInt(t1) 637 && isSamePtr(p1, p2) 638 && n >= o + 2 639 -> (Const16 [0]) 640 (Load <t1> (OffPtr [o] p1) (Zero [n] p2 _)) 641 && is32BitInt(t1) 642 && isSamePtr(p1, p2) 643 && n >= o + 4 644 -> (Const32 [0]) 645 (Load <t1> (OffPtr [o] p1) (Zero [n] p2 _)) 646 && is64BitInt(t1) 647 && isSamePtr(p1, p2) 648 && n >= o + 8 649 -> (Const64 [0]) 650 (Load <t1> (OffPtr [o] p1) (Zero [n] p2 _)) 651 && is32BitFloat(t1) 652 && isSamePtr(p1, p2) 653 && n >= o + 4 654 -> (Const32F [0]) 655 (Load <t1> (OffPtr [o] p1) (Zero [n] p2 _)) 656 && is64BitFloat(t1) 657 && isSamePtr(p1, p2) 658 && n >= o + 8 659 -> (Const64F [0]) 660 661 // Eliminate stores of values that have just been loaded from the same location. 662 // We also handle the common case where there are some intermediate stores. 663 (Store {t1} p1 (Load <t2> p2 mem) mem) 664 && isSamePtr(p1, p2) 665 && t2.Size() == sizeof(t1) 666 -> mem 667 (Store {t1} p1 (Load <t2> p2 oldmem) mem:(Store {t3} p3 _ oldmem)) 668 && isSamePtr(p1, p2) 669 && t2.Size() == sizeof(t1) 670 && disjoint(p1, sizeof(t1), p3, sizeof(t3)) 671 -> mem 672 (Store {t1} p1 (Load <t2> p2 oldmem) mem:(Store {t3} p3 _ (Store {t4} p4 _ oldmem))) 673 && isSamePtr(p1, p2) 674 && t2.Size() == sizeof(t1) 675 && disjoint(p1, sizeof(t1), p3, sizeof(t3)) 676 && disjoint(p1, sizeof(t1), p4, sizeof(t4)) 677 -> mem 678 (Store {t1} p1 (Load <t2> p2 oldmem) mem:(Store {t3} p3 _ (Store {t4} p4 _ (Store {t5} p5 _ oldmem)))) 679 && isSamePtr(p1, p2) 680 && t2.Size() == sizeof(t1) 681 && disjoint(p1, sizeof(t1), p3, sizeof(t3)) 682 && disjoint(p1, sizeof(t1), p4, sizeof(t4)) 683 && disjoint(p1, sizeof(t1), p5, sizeof(t5)) 684 -> mem 685 686 // Don't Store zeros to cleared variables. 687 (Store {t} (OffPtr [o] p1) x mem:(Zero [n] p2 _)) 688 && isConstZero(x) 689 && o >= 0 && sizeof(t) + o <= n && isSamePtr(p1, p2) 690 -> mem 691 (Store {t1} op:(OffPtr [o1] p1) x mem:(Store {t2} p2 _ (Zero [n] p3 _))) 692 && isConstZero(x) 693 && o1 >= 0 && sizeof(t1) + o1 <= n && isSamePtr(p1, p3) 694 && disjoint(op, sizeof(t1), p2, sizeof(t2)) 695 -> mem 696 (Store {t1} op:(OffPtr [o1] p1) x mem:(Store {t2} p2 _ (Store {t3} p3 _ (Zero [n] p4 _)))) 697 && isConstZero(x) 698 && o1 >= 0 && sizeof(t1) + o1 <= n && isSamePtr(p1, p4) 699 && disjoint(op, sizeof(t1), p2, sizeof(t2)) 700 && disjoint(op, sizeof(t1), p3, sizeof(t3)) 701 -> mem 702 (Store {t1} op:(OffPtr [o1] p1) x mem:(Store {t2} p2 _ (Store {t3} p3 _ (Store {t4} p4 _ (Zero [n] p5 _))))) 703 && isConstZero(x) 704 && o1 >= 0 && sizeof(t1) + o1 <= n && isSamePtr(p1, p5) 705 && disjoint(op, sizeof(t1), p2, sizeof(t2)) 706 && disjoint(op, sizeof(t1), p3, sizeof(t3)) 707 && disjoint(op, sizeof(t1), p4, sizeof(t4)) 708 -> mem 709 710 // Collapse OffPtr 711 (OffPtr (OffPtr p [b]) [a]) -> (OffPtr p [a+b]) 712 (OffPtr p [0]) && v.Type.Compare(p.Type) == types.CMPeq -> p 713 714 // indexing operations 715 // Note: bounds check has already been done 716 (PtrIndex <t> ptr idx) && config.PtrSize == 4 -> (AddPtr ptr (Mul32 <typ.Int> idx (Const32 <typ.Int> [t.Elem().Size()]))) 717 (PtrIndex <t> ptr idx) && config.PtrSize == 8 -> (AddPtr ptr (Mul64 <typ.Int> idx (Const64 <typ.Int> [t.Elem().Size()]))) 718 719 // struct operations 720 (StructSelect (StructMake1 x)) -> x 721 (StructSelect [0] (StructMake2 x _)) -> x 722 (StructSelect [1] (StructMake2 _ x)) -> x 723 (StructSelect [0] (StructMake3 x _ _)) -> x 724 (StructSelect [1] (StructMake3 _ x _)) -> x 725 (StructSelect [2] (StructMake3 _ _ x)) -> x 726 (StructSelect [0] (StructMake4 x _ _ _)) -> x 727 (StructSelect [1] (StructMake4 _ x _ _)) -> x 728 (StructSelect [2] (StructMake4 _ _ x _)) -> x 729 (StructSelect [3] (StructMake4 _ _ _ x)) -> x 730 731 (Load <t> _ _) && t.IsStruct() && t.NumFields() == 0 && fe.CanSSA(t) -> 732 (StructMake0) 733 (Load <t> ptr mem) && t.IsStruct() && t.NumFields() == 1 && fe.CanSSA(t) -> 734 (StructMake1 735 (Load <t.FieldType(0)> (OffPtr <t.FieldType(0).PtrTo()> [0] ptr) mem)) 736 (Load <t> ptr mem) && t.IsStruct() && t.NumFields() == 2 && fe.CanSSA(t) -> 737 (StructMake2 738 (Load <t.FieldType(0)> (OffPtr <t.FieldType(0).PtrTo()> [0] ptr) mem) 739 (Load <t.FieldType(1)> (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] ptr) mem)) 740 (Load <t> ptr mem) && t.IsStruct() && t.NumFields() == 3 && fe.CanSSA(t) -> 741 (StructMake3 742 (Load <t.FieldType(0)> (OffPtr <t.FieldType(0).PtrTo()> [0] ptr) mem) 743 (Load <t.FieldType(1)> (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] ptr) mem) 744 (Load <t.FieldType(2)> (OffPtr <t.FieldType(2).PtrTo()> [t.FieldOff(2)] ptr) mem)) 745 (Load <t> ptr mem) && t.IsStruct() && t.NumFields() == 4 && fe.CanSSA(t) -> 746 (StructMake4 747 (Load <t.FieldType(0)> (OffPtr <t.FieldType(0).PtrTo()> [0] ptr) mem) 748 (Load <t.FieldType(1)> (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] ptr) mem) 749 (Load <t.FieldType(2)> (OffPtr <t.FieldType(2).PtrTo()> [t.FieldOff(2)] ptr) mem) 750 (Load <t.FieldType(3)> (OffPtr <t.FieldType(3).PtrTo()> [t.FieldOff(3)] ptr) mem)) 751 752 (StructSelect [i] x:(Load <t> ptr mem)) && !fe.CanSSA(t) -> 753 @x.Block (Load <v.Type> (OffPtr <v.Type.PtrTo()> [t.FieldOff(int(i))] ptr) mem) 754 755 (Store _ (StructMake0) mem) -> mem 756 (Store dst (StructMake1 <t> f0) mem) -> 757 (Store {t.FieldType(0)} (OffPtr <t.FieldType(0).PtrTo()> [0] dst) f0 mem) 758 (Store dst (StructMake2 <t> f0 f1) mem) -> 759 (Store {t.FieldType(1)} 760 (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] dst) 761 f1 762 (Store {t.FieldType(0)} 763 (OffPtr <t.FieldType(0).PtrTo()> [0] dst) 764 f0 mem)) 765 (Store dst (StructMake3 <t> f0 f1 f2) mem) -> 766 (Store {t.FieldType(2)} 767 (OffPtr <t.FieldType(2).PtrTo()> [t.FieldOff(2)] dst) 768 f2 769 (Store {t.FieldType(1)} 770 (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] dst) 771 f1 772 (Store {t.FieldType(0)} 773 (OffPtr <t.FieldType(0).PtrTo()> [0] dst) 774 f0 mem))) 775 (Store dst (StructMake4 <t> f0 f1 f2 f3) mem) -> 776 (Store {t.FieldType(3)} 777 (OffPtr <t.FieldType(3).PtrTo()> [t.FieldOff(3)] dst) 778 f3 779 (Store {t.FieldType(2)} 780 (OffPtr <t.FieldType(2).PtrTo()> [t.FieldOff(2)] dst) 781 f2 782 (Store {t.FieldType(1)} 783 (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] dst) 784 f1 785 (Store {t.FieldType(0)} 786 (OffPtr <t.FieldType(0).PtrTo()> [0] dst) 787 f0 mem)))) 788 789 // Putting struct{*byte} and similar into direct interfaces. 790 (IMake typ (StructMake1 val)) -> (IMake typ val) 791 (StructSelect [0] x:(IData _)) -> x 792 793 // un-SSAable values use mem->mem copies 794 (Store {t} dst (Load src mem) mem) && !fe.CanSSA(t.(*types.Type)) -> 795 (Move {t} [sizeof(t)] dst src mem) 796 (Store {t} dst (Load src mem) (VarDef {x} mem)) && !fe.CanSSA(t.(*types.Type)) -> 797 (Move {t} [sizeof(t)] dst src (VarDef {x} mem)) 798 799 // array ops 800 (ArraySelect (ArrayMake1 x)) -> x 801 802 (Load <t> _ _) && t.IsArray() && t.NumElem() == 0 -> 803 (ArrayMake0) 804 805 (Load <t> ptr mem) && t.IsArray() && t.NumElem() == 1 && fe.CanSSA(t) -> 806 (ArrayMake1 (Load <t.Elem()> ptr mem)) 807 808 (Store _ (ArrayMake0) mem) -> mem 809 (Store dst (ArrayMake1 e) mem) -> (Store {e.Type} dst e mem) 810 811 // Putting [1]{*byte} and similar into direct interfaces. 812 (IMake typ (ArrayMake1 val)) -> (IMake typ val) 813 (ArraySelect [0] x:(IData _)) -> x 814 815 // string ops 816 // Decomposing StringMake and lowering of StringPtr and StringLen 817 // happens in a later pass, dec, so that these operations are available 818 // to other passes for optimizations. 819 (StringPtr (StringMake (Addr <t> {s} base) _)) -> (Addr <t> {s} base) 820 (StringLen (StringMake _ (Const64 <t> [c]))) -> (Const64 <t> [c]) 821 (ConstString {s}) && config.PtrSize == 4 && s.(string) == "" -> 822 (StringMake (ConstNil) (Const32 <typ.Int> [0])) 823 (ConstString {s}) && config.PtrSize == 8 && s.(string) == "" -> 824 (StringMake (ConstNil) (Const64 <typ.Int> [0])) 825 (ConstString {s}) && config.PtrSize == 4 && s.(string) != "" -> 826 (StringMake 827 (Addr <typ.BytePtr> {fe.StringData(s.(string))} 828 (SB)) 829 (Const32 <typ.Int> [int64(len(s.(string)))])) 830 (ConstString {s}) && config.PtrSize == 8 && s.(string) != "" -> 831 (StringMake 832 (Addr <typ.BytePtr> {fe.StringData(s.(string))} 833 (SB)) 834 (Const64 <typ.Int> [int64(len(s.(string)))])) 835 836 // slice ops 837 // Only a few slice rules are provided here. See dec.rules for 838 // a more comprehensive set. 839 (SliceLen (SliceMake _ (Const64 <t> [c]) _)) -> (Const64 <t> [c]) 840 (SliceCap (SliceMake _ _ (Const64 <t> [c]))) -> (Const64 <t> [c]) 841 (SliceLen (SliceMake _ (Const32 <t> [c]) _)) -> (Const32 <t> [c]) 842 (SliceCap (SliceMake _ _ (Const32 <t> [c]))) -> (Const32 <t> [c]) 843 (SlicePtr (SliceMake (SlicePtr x) _ _)) -> (SlicePtr x) 844 (SliceLen (SliceMake _ (SliceLen x) _)) -> (SliceLen x) 845 (SliceCap (SliceMake _ _ (SliceCap x))) -> (SliceCap x) 846 (SliceCap (SliceMake _ _ (SliceLen x))) -> (SliceLen x) 847 (ConstSlice) && config.PtrSize == 4 -> 848 (SliceMake 849 (ConstNil <v.Type.Elem().PtrTo()>) 850 (Const32 <typ.Int> [0]) 851 (Const32 <typ.Int> [0])) 852 (ConstSlice) && config.PtrSize == 8 -> 853 (SliceMake 854 (ConstNil <v.Type.Elem().PtrTo()>) 855 (Const64 <typ.Int> [0]) 856 (Const64 <typ.Int> [0])) 857 858 // interface ops 859 (ConstInterface) -> 860 (IMake 861 (ConstNil <typ.Uintptr>) 862 (ConstNil <typ.BytePtr>)) 863 864 (NilCheck (GetG mem) mem) -> mem 865 866 (If (Not cond) yes no) -> (If cond no yes) 867 (If (ConstBool [c]) yes no) && c == 1 -> (First nil yes no) 868 (If (ConstBool [c]) yes no) && c == 0 -> (First nil no yes) 869 870 // Get rid of Convert ops for pointer arithmetic on unsafe.Pointer. 871 (Convert (Add(64|32) (Convert ptr mem) off) mem) -> (Add(64|32) ptr off) 872 (Convert (Convert ptr mem) mem) -> ptr 873 874 // Decompose compound argument values 875 (Arg {n} [off]) && v.Type.IsString() -> 876 (StringMake 877 (Arg <typ.BytePtr> {n} [off]) 878 (Arg <typ.Int> {n} [off+config.PtrSize])) 879 880 (Arg {n} [off]) && v.Type.IsSlice() -> 881 (SliceMake 882 (Arg <v.Type.Elem().PtrTo()> {n} [off]) 883 (Arg <typ.Int> {n} [off+config.PtrSize]) 884 (Arg <typ.Int> {n} [off+2*config.PtrSize])) 885 886 (Arg {n} [off]) && v.Type.IsInterface() -> 887 (IMake 888 (Arg <typ.Uintptr> {n} [off]) 889 (Arg <typ.BytePtr> {n} [off+config.PtrSize])) 890 891 (Arg {n} [off]) && v.Type.IsComplex() && v.Type.Size() == 16 -> 892 (ComplexMake 893 (Arg <typ.Float64> {n} [off]) 894 (Arg <typ.Float64> {n} [off+8])) 895 896 (Arg {n} [off]) && v.Type.IsComplex() && v.Type.Size() == 8 -> 897 (ComplexMake 898 (Arg <typ.Float32> {n} [off]) 899 (Arg <typ.Float32> {n} [off+4])) 900 901 (Arg <t>) && t.IsStruct() && t.NumFields() == 0 && fe.CanSSA(t) -> 902 (StructMake0) 903 (Arg <t> {n} [off]) && t.IsStruct() && t.NumFields() == 1 && fe.CanSSA(t) -> 904 (StructMake1 905 (Arg <t.FieldType(0)> {n} [off+t.FieldOff(0)])) 906 (Arg <t> {n} [off]) && t.IsStruct() && t.NumFields() == 2 && fe.CanSSA(t) -> 907 (StructMake2 908 (Arg <t.FieldType(0)> {n} [off+t.FieldOff(0)]) 909 (Arg <t.FieldType(1)> {n} [off+t.FieldOff(1)])) 910 (Arg <t> {n} [off]) && t.IsStruct() && t.NumFields() == 3 && fe.CanSSA(t) -> 911 (StructMake3 912 (Arg <t.FieldType(0)> {n} [off+t.FieldOff(0)]) 913 (Arg <t.FieldType(1)> {n} [off+t.FieldOff(1)]) 914 (Arg <t.FieldType(2)> {n} [off+t.FieldOff(2)])) 915 (Arg <t> {n} [off]) && t.IsStruct() && t.NumFields() == 4 && fe.CanSSA(t) -> 916 (StructMake4 917 (Arg <t.FieldType(0)> {n} [off+t.FieldOff(0)]) 918 (Arg <t.FieldType(1)> {n} [off+t.FieldOff(1)]) 919 (Arg <t.FieldType(2)> {n} [off+t.FieldOff(2)]) 920 (Arg <t.FieldType(3)> {n} [off+t.FieldOff(3)])) 921 922 (Arg <t>) && t.IsArray() && t.NumElem() == 0 -> 923 (ArrayMake0) 924 (Arg <t> {n} [off]) && t.IsArray() && t.NumElem() == 1 && fe.CanSSA(t) -> 925 (ArrayMake1 (Arg <t.Elem()> {n} [off])) 926 927 // strength reduction of divide by a constant. 928 // See ../magic.go for a detailed description of these algorithms. 929 930 // Unsigned divide by power of 2. Strength reduce to a shift. 931 (Div8u n (Const8 [c])) && isPowerOfTwo(c&0xff) -> (Rsh8Ux64 n (Const64 <typ.UInt64> [log2(c&0xff)])) 932 (Div16u n (Const16 [c])) && isPowerOfTwo(c&0xffff) -> (Rsh16Ux64 n (Const64 <typ.UInt64> [log2(c&0xffff)])) 933 (Div32u n (Const32 [c])) && isPowerOfTwo(c&0xffffffff) -> (Rsh32Ux64 n (Const64 <typ.UInt64> [log2(c&0xffffffff)])) 934 (Div64u n (Const64 [c])) && isPowerOfTwo(c) -> (Rsh64Ux64 n (Const64 <typ.UInt64> [log2(c)])) 935 (Div64u n (Const64 [-1<<63])) -> (Rsh64Ux64 n (Const64 <typ.UInt64> [63])) 936 937 // Signed non-negative divide by power of 2. 938 (Div8 n (Const8 [c])) && isNonNegative(n) && isPowerOfTwo(c&0xff) -> (Rsh8Ux64 n (Const64 <typ.UInt64> [log2(c&0xff)])) 939 (Div16 n (Const16 [c])) && isNonNegative(n) && isPowerOfTwo(c&0xffff) -> (Rsh16Ux64 n (Const64 <typ.UInt64> [log2(c&0xffff)])) 940 (Div32 n (Const32 [c])) && isNonNegative(n) && isPowerOfTwo(c&0xffffffff) -> (Rsh32Ux64 n (Const64 <typ.UInt64> [log2(c&0xffffffff)])) 941 (Div64 n (Const64 [c])) && isNonNegative(n) && isPowerOfTwo(c) -> (Rsh64Ux64 n (Const64 <typ.UInt64> [log2(c)])) 942 (Div64 n (Const64 [-1<<63])) && isNonNegative(n) -> (Const64 [0]) 943 944 // Unsigned divide, not a power of 2. Strength reduce to a multiply. 945 // For 8-bit divides, we just do a direct 9-bit by 8-bit multiply. 946 (Div8u x (Const8 [c])) && umagicOK(8, c) -> 947 (Trunc32to8 948 (Rsh32Ux64 <typ.UInt32> 949 (Mul32 <typ.UInt32> 950 (Const32 <typ.UInt32> [int64(1<<8+umagic(8,c).m)]) 951 (ZeroExt8to32 x)) 952 (Const64 <typ.UInt64> [8+umagic(8,c).s]))) 953 954 // For 16-bit divides on 64-bit machines, we do a direct 17-bit by 16-bit multiply. 955 (Div16u x (Const16 [c])) && umagicOK(16, c) && config.RegSize == 8 -> 956 (Trunc64to16 957 (Rsh64Ux64 <typ.UInt64> 958 (Mul64 <typ.UInt64> 959 (Const64 <typ.UInt64> [int64(1<<16+umagic(16,c).m)]) 960 (ZeroExt16to64 x)) 961 (Const64 <typ.UInt64> [16+umagic(16,c).s]))) 962 963 // For 16-bit divides on 32-bit machines 964 (Div16u x (Const16 [c])) && umagicOK(16, c) && config.RegSize == 4 && umagic(16,c).m&1 == 0 -> 965 (Trunc32to16 966 (Rsh32Ux64 <typ.UInt32> 967 (Mul32 <typ.UInt32> 968 (Const32 <typ.UInt32> [int64(1<<15+umagic(16,c).m/2)]) 969 (ZeroExt16to32 x)) 970 (Const64 <typ.UInt64> [16+umagic(16,c).s-1]))) 971 (Div16u x (Const16 [c])) && umagicOK(16, c) && config.RegSize == 4 && c&1 == 0 -> 972 (Trunc32to16 973 (Rsh32Ux64 <typ.UInt32> 974 (Mul32 <typ.UInt32> 975 (Const32 <typ.UInt32> [int64(1<<15+(umagic(16,c).m+1)/2)]) 976 (Rsh32Ux64 <typ.UInt32> (ZeroExt16to32 x) (Const64 <typ.UInt64> [1]))) 977 (Const64 <typ.UInt64> [16+umagic(16,c).s-2]))) 978 (Div16u x (Const16 [c])) && umagicOK(16, c) && config.RegSize == 4 && config.useAvg -> 979 (Trunc32to16 980 (Rsh32Ux64 <typ.UInt32> 981 (Avg32u 982 (Lsh32x64 <typ.UInt32> (ZeroExt16to32 x) (Const64 <typ.UInt64> [16])) 983 (Mul32 <typ.UInt32> 984 (Const32 <typ.UInt32> [int64(umagic(16,c).m)]) 985 (ZeroExt16to32 x))) 986 (Const64 <typ.UInt64> [16+umagic(16,c).s-1]))) 987 988 // For 32-bit divides on 32-bit machines 989 (Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 4 && umagic(32,c).m&1 == 0 && config.useHmul -> 990 (Rsh32Ux64 <typ.UInt32> 991 (Hmul32u <typ.UInt32> 992 (Const32 <typ.UInt32> [int64(int32(1<<31+umagic(32,c).m/2))]) 993 x) 994 (Const64 <typ.UInt64> [umagic(32,c).s-1])) 995 (Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 4 && c&1 == 0 && config.useHmul -> 996 (Rsh32Ux64 <typ.UInt32> 997 (Hmul32u <typ.UInt32> 998 (Const32 <typ.UInt32> [int64(int32(1<<31+(umagic(32,c).m+1)/2))]) 999 (Rsh32Ux64 <typ.UInt32> x (Const64 <typ.UInt64> [1]))) 1000 (Const64 <typ.UInt64> [umagic(32,c).s-2])) 1001 (Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 4 && config.useAvg && config.useHmul -> 1002 (Rsh32Ux64 <typ.UInt32> 1003 (Avg32u 1004 x 1005 (Hmul32u <typ.UInt32> 1006 (Const32 <typ.UInt32> [int64(int32(umagic(32,c).m))]) 1007 x)) 1008 (Const64 <typ.UInt64> [umagic(32,c).s-1])) 1009 1010 // For 32-bit divides on 64-bit machines 1011 // We'll use a regular (non-hi) multiply for this case. 1012 (Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 8 && umagic(32,c).m&1 == 0 -> 1013 (Trunc64to32 1014 (Rsh64Ux64 <typ.UInt64> 1015 (Mul64 <typ.UInt64> 1016 (Const64 <typ.UInt64> [int64(1<<31+umagic(32,c).m/2)]) 1017 (ZeroExt32to64 x)) 1018 (Const64 <typ.UInt64> [32+umagic(32,c).s-1]))) 1019 (Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 8 && c&1 == 0 -> 1020 (Trunc64to32 1021 (Rsh64Ux64 <typ.UInt64> 1022 (Mul64 <typ.UInt64> 1023 (Const64 <typ.UInt64> [int64(1<<31+(umagic(32,c).m+1)/2)]) 1024 (Rsh64Ux64 <typ.UInt64> (ZeroExt32to64 x) (Const64 <typ.UInt64> [1]))) 1025 (Const64 <typ.UInt64> [32+umagic(32,c).s-2]))) 1026 (Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 8 && config.useAvg -> 1027 (Trunc64to32 1028 (Rsh64Ux64 <typ.UInt64> 1029 (Avg64u 1030 (Lsh64x64 <typ.UInt64> (ZeroExt32to64 x) (Const64 <typ.UInt64> [32])) 1031 (Mul64 <typ.UInt64> 1032 (Const64 <typ.UInt32> [int64(umagic(32,c).m)]) 1033 (ZeroExt32to64 x))) 1034 (Const64 <typ.UInt64> [32+umagic(32,c).s-1]))) 1035 1036 // For 64-bit divides on 64-bit machines 1037 // (64-bit divides on 32-bit machines are lowered to a runtime call by the walk pass.) 1038 (Div64u x (Const64 [c])) && umagicOK(64, c) && config.RegSize == 8 && umagic(64,c).m&1 == 0 && config.useHmul -> 1039 (Rsh64Ux64 <typ.UInt64> 1040 (Hmul64u <typ.UInt64> 1041 (Const64 <typ.UInt64> [int64(1<<63+umagic(64,c).m/2)]) 1042 x) 1043 (Const64 <typ.UInt64> [umagic(64,c).s-1])) 1044 (Div64u x (Const64 [c])) && umagicOK(64, c) && config.RegSize == 8 && c&1 == 0 && config.useHmul -> 1045 (Rsh64Ux64 <typ.UInt64> 1046 (Hmul64u <typ.UInt64> 1047 (Const64 <typ.UInt64> [int64(1<<63+(umagic(64,c).m+1)/2)]) 1048 (Rsh64Ux64 <typ.UInt64> x (Const64 <typ.UInt64> [1]))) 1049 (Const64 <typ.UInt64> [umagic(64,c).s-2])) 1050 (Div64u x (Const64 [c])) && umagicOK(64, c) && config.RegSize == 8 && config.useAvg && config.useHmul -> 1051 (Rsh64Ux64 <typ.UInt64> 1052 (Avg64u 1053 x 1054 (Hmul64u <typ.UInt64> 1055 (Const64 <typ.UInt64> [int64(umagic(64,c).m)]) 1056 x)) 1057 (Const64 <typ.UInt64> [umagic(64,c).s-1])) 1058 1059 // Signed divide by a negative constant. Rewrite to divide by a positive constant. 1060 (Div8 <t> n (Const8 [c])) && c < 0 && c != -1<<7 -> (Neg8 (Div8 <t> n (Const8 <t> [-c]))) 1061 (Div16 <t> n (Const16 [c])) && c < 0 && c != -1<<15 -> (Neg16 (Div16 <t> n (Const16 <t> [-c]))) 1062 (Div32 <t> n (Const32 [c])) && c < 0 && c != -1<<31 -> (Neg32 (Div32 <t> n (Const32 <t> [-c]))) 1063 (Div64 <t> n (Const64 [c])) && c < 0 && c != -1<<63 -> (Neg64 (Div64 <t> n (Const64 <t> [-c]))) 1064 1065 // Dividing by the most-negative number. Result is always 0 except 1066 // if the input is also the most-negative number. 1067 // We can detect that using the sign bit of x & -x. 1068 (Div8 <t> x (Const8 [-1<<7 ])) -> (Rsh8Ux64 (And8 <t> x (Neg8 <t> x)) (Const64 <typ.UInt64> [7 ])) 1069 (Div16 <t> x (Const16 [-1<<15])) -> (Rsh16Ux64 (And16 <t> x (Neg16 <t> x)) (Const64 <typ.UInt64> [15])) 1070 (Div32 <t> x (Const32 [-1<<31])) -> (Rsh32Ux64 (And32 <t> x (Neg32 <t> x)) (Const64 <typ.UInt64> [31])) 1071 (Div64 <t> x (Const64 [-1<<63])) -> (Rsh64Ux64 (And64 <t> x (Neg64 <t> x)) (Const64 <typ.UInt64> [63])) 1072 1073 // Signed divide by power of 2. 1074 // n / c = n >> log(c) if n >= 0 1075 // = (n+c-1) >> log(c) if n < 0 1076 // We conditionally add c-1 by adding n>>63>>(64-log(c)) (first shift signed, second shift unsigned). 1077 (Div8 <t> n (Const8 [c])) && isPowerOfTwo(c) -> 1078 (Rsh8x64 1079 (Add8 <t> n (Rsh8Ux64 <t> (Rsh8x64 <t> n (Const64 <typ.UInt64> [ 7])) (Const64 <typ.UInt64> [ 8-log2(c)]))) 1080 (Const64 <typ.UInt64> [log2(c)])) 1081 (Div16 <t> n (Const16 [c])) && isPowerOfTwo(c) -> 1082 (Rsh16x64 1083 (Add16 <t> n (Rsh16Ux64 <t> (Rsh16x64 <t> n (Const64 <typ.UInt64> [15])) (Const64 <typ.UInt64> [16-log2(c)]))) 1084 (Const64 <typ.UInt64> [log2(c)])) 1085 (Div32 <t> n (Const32 [c])) && isPowerOfTwo(c) -> 1086 (Rsh32x64 1087 (Add32 <t> n (Rsh32Ux64 <t> (Rsh32x64 <t> n (Const64 <typ.UInt64> [31])) (Const64 <typ.UInt64> [32-log2(c)]))) 1088 (Const64 <typ.UInt64> [log2(c)])) 1089 (Div64 <t> n (Const64 [c])) && isPowerOfTwo(c) -> 1090 (Rsh64x64 1091 (Add64 <t> n (Rsh64Ux64 <t> (Rsh64x64 <t> n (Const64 <typ.UInt64> [63])) (Const64 <typ.UInt64> [64-log2(c)]))) 1092 (Const64 <typ.UInt64> [log2(c)])) 1093 1094 // Signed divide, not a power of 2. Strength reduce to a multiply. 1095 (Div8 <t> x (Const8 [c])) && smagicOK(8,c) -> 1096 (Sub8 <t> 1097 (Rsh32x64 <t> 1098 (Mul32 <typ.UInt32> 1099 (Const32 <typ.UInt32> [int64(smagic(8,c).m)]) 1100 (SignExt8to32 x)) 1101 (Const64 <typ.UInt64> [8+smagic(8,c).s])) 1102 (Rsh32x64 <t> 1103 (SignExt8to32 x) 1104 (Const64 <typ.UInt64> [31]))) 1105 (Div16 <t> x (Const16 [c])) && smagicOK(16,c) -> 1106 (Sub16 <t> 1107 (Rsh32x64 <t> 1108 (Mul32 <typ.UInt32> 1109 (Const32 <typ.UInt32> [int64(smagic(16,c).m)]) 1110 (SignExt16to32 x)) 1111 (Const64 <typ.UInt64> [16+smagic(16,c).s])) 1112 (Rsh32x64 <t> 1113 (SignExt16to32 x) 1114 (Const64 <typ.UInt64> [31]))) 1115 (Div32 <t> x (Const32 [c])) && smagicOK(32,c) && config.RegSize == 8 -> 1116 (Sub32 <t> 1117 (Rsh64x64 <t> 1118 (Mul64 <typ.UInt64> 1119 (Const64 <typ.UInt64> [int64(smagic(32,c).m)]) 1120 (SignExt32to64 x)) 1121 (Const64 <typ.UInt64> [32+smagic(32,c).s])) 1122 (Rsh64x64 <t> 1123 (SignExt32to64 x) 1124 (Const64 <typ.UInt64> [63]))) 1125 (Div32 <t> x (Const32 [c])) && smagicOK(32,c) && config.RegSize == 4 && smagic(32,c).m&1 == 0 && config.useHmul -> 1126 (Sub32 <t> 1127 (Rsh32x64 <t> 1128 (Hmul32 <t> 1129 (Const32 <typ.UInt32> [int64(int32(smagic(32,c).m/2))]) 1130 x) 1131 (Const64 <typ.UInt64> [smagic(32,c).s-1])) 1132 (Rsh32x64 <t> 1133 x 1134 (Const64 <typ.UInt64> [31]))) 1135 (Div32 <t> x (Const32 [c])) && smagicOK(32,c) && config.RegSize == 4 && smagic(32,c).m&1 != 0 && config.useHmul -> 1136 (Sub32 <t> 1137 (Rsh32x64 <t> 1138 (Add32 <t> 1139 (Hmul32 <t> 1140 (Const32 <typ.UInt32> [int64(int32(smagic(32,c).m))]) 1141 x) 1142 x) 1143 (Const64 <typ.UInt64> [smagic(32,c).s])) 1144 (Rsh32x64 <t> 1145 x 1146 (Const64 <typ.UInt64> [31]))) 1147 (Div64 <t> x (Const64 [c])) && smagicOK(64,c) && smagic(64,c).m&1 == 0 && config.useHmul -> 1148 (Sub64 <t> 1149 (Rsh64x64 <t> 1150 (Hmul64 <t> 1151 (Const64 <typ.UInt64> [int64(smagic(64,c).m/2)]) 1152 x) 1153 (Const64 <typ.UInt64> [smagic(64,c).s-1])) 1154 (Rsh64x64 <t> 1155 x 1156 (Const64 <typ.UInt64> [63]))) 1157 (Div64 <t> x (Const64 [c])) && smagicOK(64,c) && smagic(64,c).m&1 != 0 && config.useHmul -> 1158 (Sub64 <t> 1159 (Rsh64x64 <t> 1160 (Add64 <t> 1161 (Hmul64 <t> 1162 (Const64 <typ.UInt64> [int64(smagic(64,c).m)]) 1163 x) 1164 x) 1165 (Const64 <typ.UInt64> [smagic(64,c).s])) 1166 (Rsh64x64 <t> 1167 x 1168 (Const64 <typ.UInt64> [63]))) 1169 1170 // Unsigned mod by power of 2 constant. 1171 (Mod8u <t> n (Const8 [c])) && isPowerOfTwo(c&0xff) -> (And8 n (Const8 <t> [(c&0xff)-1])) 1172 (Mod16u <t> n (Const16 [c])) && isPowerOfTwo(c&0xffff) -> (And16 n (Const16 <t> [(c&0xffff)-1])) 1173 (Mod32u <t> n (Const32 [c])) && isPowerOfTwo(c&0xffffffff) -> (And32 n (Const32 <t> [(c&0xffffffff)-1])) 1174 (Mod64u <t> n (Const64 [c])) && isPowerOfTwo(c) -> (And64 n (Const64 <t> [c-1])) 1175 (Mod64u <t> n (Const64 [-1<<63])) -> (And64 n (Const64 <t> [1<<63-1])) 1176 1177 // Signed non-negative mod by power of 2 constant. 1178 (Mod8 <t> n (Const8 [c])) && isNonNegative(n) && isPowerOfTwo(c&0xff) -> (And8 n (Const8 <t> [(c&0xff)-1])) 1179 (Mod16 <t> n (Const16 [c])) && isNonNegative(n) && isPowerOfTwo(c&0xffff) -> (And16 n (Const16 <t> [(c&0xffff)-1])) 1180 (Mod32 <t> n (Const32 [c])) && isNonNegative(n) && isPowerOfTwo(c&0xffffffff) -> (And32 n (Const32 <t> [(c&0xffffffff)-1])) 1181 (Mod64 <t> n (Const64 [c])) && isNonNegative(n) && isPowerOfTwo(c) -> (And64 n (Const64 <t> [c-1])) 1182 (Mod64 n (Const64 [-1<<63])) && isNonNegative(n) -> n 1183 1184 // Signed mod by negative constant. 1185 (Mod8 <t> n (Const8 [c])) && c < 0 && c != -1<<7 -> (Mod8 <t> n (Const8 <t> [-c])) 1186 (Mod16 <t> n (Const16 [c])) && c < 0 && c != -1<<15 -> (Mod16 <t> n (Const16 <t> [-c])) 1187 (Mod32 <t> n (Const32 [c])) && c < 0 && c != -1<<31 -> (Mod32 <t> n (Const32 <t> [-c])) 1188 (Mod64 <t> n (Const64 [c])) && c < 0 && c != -1<<63 -> (Mod64 <t> n (Const64 <t> [-c])) 1189 1190 // All other mods by constants, do A%B = A-(A/B*B). 1191 // This implements % with two * and a bunch of ancillary ops. 1192 // One of the * is free if the user's code also computes A/B. 1193 (Mod8 <t> x (Const8 [c])) && x.Op != OpConst8 && (c > 0 || c == -1<<7) 1194 -> (Sub8 x (Mul8 <t> (Div8 <t> x (Const8 <t> [c])) (Const8 <t> [c]))) 1195 (Mod16 <t> x (Const16 [c])) && x.Op != OpConst16 && (c > 0 || c == -1<<15) 1196 -> (Sub16 x (Mul16 <t> (Div16 <t> x (Const16 <t> [c])) (Const16 <t> [c]))) 1197 (Mod32 <t> x (Const32 [c])) && x.Op != OpConst32 && (c > 0 || c == -1<<31) 1198 -> (Sub32 x (Mul32 <t> (Div32 <t> x (Const32 <t> [c])) (Const32 <t> [c]))) 1199 (Mod64 <t> x (Const64 [c])) && x.Op != OpConst64 && (c > 0 || c == -1<<63) 1200 -> (Sub64 x (Mul64 <t> (Div64 <t> x (Const64 <t> [c])) (Const64 <t> [c]))) 1201 (Mod8u <t> x (Const8 [c])) && x.Op != OpConst8 && c > 0 && umagicOK(8 ,c) 1202 -> (Sub8 x (Mul8 <t> (Div8u <t> x (Const8 <t> [c])) (Const8 <t> [c]))) 1203 (Mod16u <t> x (Const16 [c])) && x.Op != OpConst16 && c > 0 && umagicOK(16,c) 1204 -> (Sub16 x (Mul16 <t> (Div16u <t> x (Const16 <t> [c])) (Const16 <t> [c]))) 1205 (Mod32u <t> x (Const32 [c])) && x.Op != OpConst32 && c > 0 && umagicOK(32,c) 1206 -> (Sub32 x (Mul32 <t> (Div32u <t> x (Const32 <t> [c])) (Const32 <t> [c]))) 1207 (Mod64u <t> x (Const64 [c])) && x.Op != OpConst64 && c > 0 && umagicOK(64,c) 1208 -> (Sub64 x (Mul64 <t> (Div64u <t> x (Const64 <t> [c])) (Const64 <t> [c]))) 1209 1210 (Eq(8|16|32|64) s:(Sub(8|16|32|64) x y) (Const(8|16|32|64) [0])) && s.Uses == 1 -> (Eq(8|16|32|64) x y) 1211 (Neq(8|16|32|64) s:(Sub(8|16|32|64) x y) (Const(8|16|32|64) [0])) && s.Uses == 1 -> (Neq(8|16|32|64) x y) 1212 1213 // Reassociate expressions involving 1214 // constants such that constants come first, 1215 // exposing obvious constant-folding opportunities. 1216 // Reassociate (op (op y C) x) to (op C (op x y)) or similar, where C 1217 // is constant, which pushes constants to the outside 1218 // of the expression. At that point, any constant-folding 1219 // opportunities should be obvious. 1220 1221 // x + (C + z) -> C + (x + z) 1222 (Add64 (Add64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Add64 i (Add64 <t> z x)) 1223 (Add32 (Add32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Add32 i (Add32 <t> z x)) 1224 (Add16 (Add16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Add16 i (Add16 <t> z x)) 1225 (Add8 (Add8 i:(Const8 <t>) z) x) && (z.Op != OpConst8 && x.Op != OpConst8) -> (Add8 i (Add8 <t> z x)) 1226 1227 // x + (C - z) -> C + (x - z) 1228 (Add64 (Sub64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Add64 i (Sub64 <t> x z)) 1229 (Add32 (Sub32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Add32 i (Sub32 <t> x z)) 1230 (Add16 (Sub16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Add16 i (Sub16 <t> x z)) 1231 (Add8 (Sub8 i:(Const8 <t>) z) x) && (z.Op != OpConst8 && x.Op != OpConst8) -> (Add8 i (Sub8 <t> x z)) 1232 (Add64 x (Sub64 i:(Const64 <t>) z)) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Add64 i (Sub64 <t> x z)) 1233 (Add32 x (Sub32 i:(Const32 <t>) z)) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Add32 i (Sub32 <t> x z)) 1234 (Add16 x (Sub16 i:(Const16 <t>) z)) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Add16 i (Sub16 <t> x z)) 1235 (Add8 x (Sub8 i:(Const8 <t>) z)) && (z.Op != OpConst8 && x.Op != OpConst8) -> (Add8 i (Sub8 <t> x z)) 1236 1237 // x + (z - C) -> (x + z) - C 1238 (Add64 (Sub64 z i:(Const64 <t>)) x) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Sub64 (Add64 <t> x z) i) 1239 (Add32 (Sub32 z i:(Const32 <t>)) x) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Sub32 (Add32 <t> x z) i) 1240 (Add16 (Sub16 z i:(Const16 <t>)) x) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Sub16 (Add16 <t> x z) i) 1241 (Add8 (Sub8 z i:(Const8 <t>)) x) && (z.Op != OpConst8 && x.Op != OpConst8) -> (Sub8 (Add8 <t> x z) i) 1242 (Add64 x (Sub64 z i:(Const64 <t>))) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Sub64 (Add64 <t> x z) i) 1243 (Add32 x (Sub32 z i:(Const32 <t>))) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Sub32 (Add32 <t> x z) i) 1244 (Add16 x (Sub16 z i:(Const16 <t>))) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Sub16 (Add16 <t> x z) i) 1245 (Add8 x (Sub8 z i:(Const8 <t>))) && (z.Op != OpConst8 && x.Op != OpConst8) -> (Sub8 (Add8 <t> x z) i) 1246 1247 // x - (C - z) -> x + (z - C) -> (x + z) - C 1248 (Sub64 x (Sub64 i:(Const64 <t>) z)) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Sub64 (Add64 <t> x z) i) 1249 (Sub32 x (Sub32 i:(Const32 <t>) z)) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Sub32 (Add32 <t> x z) i) 1250 (Sub16 x (Sub16 i:(Const16 <t>) z)) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Sub16 (Add16 <t> x z) i) 1251 (Sub8 x (Sub8 i:(Const8 <t>) z)) && (z.Op != OpConst8 && x.Op != OpConst8) -> (Sub8 (Add8 <t> x z) i) 1252 1253 // x - (z - C) -> x + (C - z) -> (x - z) + C 1254 (Sub64 x (Sub64 z i:(Const64 <t>))) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Add64 i (Sub64 <t> x z)) 1255 (Sub32 x (Sub32 z i:(Const32 <t>))) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Add32 i (Sub32 <t> x z)) 1256 (Sub16 x (Sub16 z i:(Const16 <t>))) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Add16 i (Sub16 <t> x z)) 1257 (Sub8 x (Sub8 z i:(Const8 <t>))) && (z.Op != OpConst8 && x.Op != OpConst8) -> (Add8 i (Sub8 <t> x z)) 1258 1259 // x & (C & z) -> C & (x & z) 1260 (And64 (And64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) -> (And64 i (And64 <t> z x)) 1261 (And32 (And32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) -> (And32 i (And32 <t> z x)) 1262 (And16 (And16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) -> (And16 i (And16 <t> z x)) 1263 (And8 (And8 i:(Const8 <t>) z) x) && (z.Op != OpConst8 && x.Op != OpConst8) -> (And8 i (And8 <t> z x)) 1264 1265 // x | (C | z) -> C | (x | z) 1266 (Or64 (Or64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Or64 i (Or64 <t> z x)) 1267 (Or32 (Or32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Or32 i (Or32 <t> z x)) 1268 (Or16 (Or16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Or16 i (Or16 <t> z x)) 1269 (Or8 (Or8 i:(Const8 <t>) z) x) && (z.Op != OpConst8 && x.Op != OpConst8) -> (Or8 i (Or8 <t> z x)) 1270 1271 // x ^ (C ^ z) -> C ^ (x ^ z) 1272 (Xor64 (Xor64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Xor64 i (Xor64 <t> z x)) 1273 (Xor32 (Xor32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Xor32 i (Xor32 <t> z x)) 1274 (Xor16 (Xor16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Xor16 i (Xor16 <t> z x)) 1275 (Xor8 (Xor8 i:(Const8 <t>) z) x) && (z.Op != OpConst8 && x.Op != OpConst8) -> (Xor8 i (Xor8 <t> z x)) 1276 1277 // C + (D + x) -> (C + D) + x 1278 (Add64 (Const64 <t> [c]) (Add64 (Const64 <t> [d]) x)) -> (Add64 (Const64 <t> [c+d]) x) 1279 (Add32 (Const32 <t> [c]) (Add32 (Const32 <t> [d]) x)) -> (Add32 (Const32 <t> [int64(int32(c+d))]) x) 1280 (Add16 (Const16 <t> [c]) (Add16 (Const16 <t> [d]) x)) -> (Add16 (Const16 <t> [int64(int16(c+d))]) x) 1281 (Add8 (Const8 <t> [c]) (Add8 (Const8 <t> [d]) x)) -> (Add8 (Const8 <t> [int64(int8(c+d))]) x) 1282 1283 // C + (D - x) -> (C + D) - x 1284 (Add64 (Const64 <t> [c]) (Sub64 (Const64 <t> [d]) x)) -> (Sub64 (Const64 <t> [c+d]) x) 1285 (Add32 (Const32 <t> [c]) (Sub32 (Const32 <t> [d]) x)) -> (Sub32 (Const32 <t> [int64(int32(c+d))]) x) 1286 (Add16 (Const16 <t> [c]) (Sub16 (Const16 <t> [d]) x)) -> (Sub16 (Const16 <t> [int64(int16(c+d))]) x) 1287 (Add8 (Const8 <t> [c]) (Sub8 (Const8 <t> [d]) x)) -> (Sub8 (Const8 <t> [int64(int8(c+d))]) x) 1288 1289 // C + (x - D) -> (C - D) + x 1290 (Add64 (Const64 <t> [c]) (Sub64 x (Const64 <t> [d]))) -> (Add64 (Const64 <t> [c-d]) x) 1291 (Add32 (Const32 <t> [c]) (Sub32 x (Const32 <t> [d]))) -> (Add32 (Const32 <t> [int64(int32(c-d))]) x) 1292 (Add16 (Const16 <t> [c]) (Sub16 x (Const16 <t> [d]))) -> (Add16 (Const16 <t> [int64(int16(c-d))]) x) 1293 (Add8 (Const8 <t> [c]) (Sub8 x (Const8 <t> [d]))) -> (Add8 (Const8 <t> [int64(int8(c-d))]) x) 1294 1295 // C - (x - D) -> (C + D) - x 1296 (Sub64 (Const64 <t> [c]) (Sub64 x (Const64 <t> [d]))) -> (Sub64 (Const64 <t> [c+d]) x) 1297 (Sub32 (Const32 <t> [c]) (Sub32 x (Const32 <t> [d]))) -> (Sub32 (Const32 <t> [int64(int32(c+d))]) x) 1298 (Sub16 (Const16 <t> [c]) (Sub16 x (Const16 <t> [d]))) -> (Sub16 (Const16 <t> [int64(int16(c+d))]) x) 1299 (Sub8 (Const8 <t> [c]) (Sub8 x (Const8 <t> [d]))) -> (Sub8 (Const8 <t> [int64(int8(c+d))]) x) 1300 1301 // C - (D - x) -> (C - D) + x 1302 (Sub64 (Const64 <t> [c]) (Sub64 (Const64 <t> [d]) x)) -> (Add64 (Const64 <t> [c-d]) x) 1303 (Sub32 (Const32 <t> [c]) (Sub32 (Const32 <t> [d]) x)) -> (Add32 (Const32 <t> [int64(int32(c-d))]) x) 1304 (Sub16 (Const16 <t> [c]) (Sub16 (Const16 <t> [d]) x)) -> (Add16 (Const16 <t> [int64(int16(c-d))]) x) 1305 (Sub8 (Const8 <t> [c]) (Sub8 (Const8 <t> [d]) x)) -> (Add8 (Const8 <t> [int64(int8(c-d))]) x) 1306 1307 // C & (D & x) -> (C & D) & x 1308 (And64 (Const64 <t> [c]) (And64 (Const64 <t> [d]) x)) -> (And64 (Const64 <t> [c&d]) x) 1309 (And32 (Const32 <t> [c]) (And32 (Const32 <t> [d]) x)) -> (And32 (Const32 <t> [int64(int32(c&d))]) x) 1310 (And16 (Const16 <t> [c]) (And16 (Const16 <t> [d]) x)) -> (And16 (Const16 <t> [int64(int16(c&d))]) x) 1311 (And8 (Const8 <t> [c]) (And8 (Const8 <t> [d]) x)) -> (And8 (Const8 <t> [int64(int8(c&d))]) x) 1312 1313 // C | (D | x) -> (C | D) | x 1314 (Or64 (Const64 <t> [c]) (Or64 (Const64 <t> [d]) x)) -> (Or64 (Const64 <t> [c|d]) x) 1315 (Or32 (Const32 <t> [c]) (Or32 (Const32 <t> [d]) x)) -> (Or32 (Const32 <t> [int64(int32(c|d))]) x) 1316 (Or16 (Const16 <t> [c]) (Or16 (Const16 <t> [d]) x)) -> (Or16 (Const16 <t> [int64(int16(c|d))]) x) 1317 (Or8 (Const8 <t> [c]) (Or8 (Const8 <t> [d]) x)) -> (Or8 (Const8 <t> [int64(int8(c|d))]) x) 1318 1319 // C ^ (D ^ x) -> (C ^ D) ^ x 1320 (Xor64 (Const64 <t> [c]) (Xor64 (Const64 <t> [d]) x)) -> (Xor64 (Const64 <t> [c^d]) x) 1321 (Xor32 (Const32 <t> [c]) (Xor32 (Const32 <t> [d]) x)) -> (Xor32 (Const32 <t> [int64(int32(c^d))]) x) 1322 (Xor16 (Const16 <t> [c]) (Xor16 (Const16 <t> [d]) x)) -> (Xor16 (Const16 <t> [int64(int16(c^d))]) x) 1323 (Xor8 (Const8 <t> [c]) (Xor8 (Const8 <t> [d]) x)) -> (Xor8 (Const8 <t> [int64(int8(c^d))]) x) 1324 1325 // C * (D * x) = (C * D) * x 1326 (Mul64 (Const64 <t> [c]) (Mul64 (Const64 <t> [d]) x)) -> (Mul64 (Const64 <t> [c*d]) x) 1327 (Mul32 (Const32 <t> [c]) (Mul32 (Const32 <t> [d]) x)) -> (Mul32 (Const32 <t> [int64(int32(c*d))]) x) 1328 (Mul16 (Const16 <t> [c]) (Mul16 (Const16 <t> [d]) x)) -> (Mul16 (Const16 <t> [int64(int16(c*d))]) x) 1329 (Mul8 (Const8 <t> [c]) (Mul8 (Const8 <t> [d]) x)) -> (Mul8 (Const8 <t> [int64(int8(c*d))]) x) 1330 1331 // floating point optimizations 1332 (Mul(32|64)F x (Const(32|64)F [auxFrom64F(1)])) -> x 1333 (Mul32F x (Const32F [auxFrom32F(-1)])) -> (Neg32F x) 1334 (Mul64F x (Const64F [auxFrom64F(-1)])) -> (Neg64F x) 1335 (Mul32F x (Const32F [auxFrom32F(2)])) -> (Add32F x x) 1336 (Mul64F x (Const64F [auxFrom64F(2)])) -> (Add64F x x) 1337 1338 (Div32F x (Const32F <t> [c])) && reciprocalExact32(auxTo32F(c)) -> (Mul32F x (Const32F <t> [auxFrom32F(1/auxTo32F(c))])) 1339 (Div64F x (Const64F <t> [c])) && reciprocalExact64(auxTo64F(c)) -> (Mul64F x (Const64F <t> [auxFrom64F(1/auxTo64F(c))])) 1340 1341 (Sqrt (Const64F [c])) -> (Const64F [auxFrom64F(math.Sqrt(auxTo64F(c)))]) 1342 1343 // recognize runtime.newobject and don't Zero/Nilcheck it 1344 (Zero (Load (OffPtr [c] (SP)) mem) mem) 1345 && mem.Op == OpStaticCall 1346 && isSameSym(mem.Aux, "runtime.newobject") 1347 && c == config.ctxt.FixedFrameSize() + config.RegSize // offset of return value 1348 -> mem 1349 (Store (Load (OffPtr [c] (SP)) mem) x mem) 1350 && isConstZero(x) 1351 && mem.Op == OpStaticCall 1352 && isSameSym(mem.Aux, "runtime.newobject") 1353 && c == config.ctxt.FixedFrameSize() + config.RegSize // offset of return value 1354 -> mem 1355 (Store (OffPtr (Load (OffPtr [c] (SP)) mem)) x mem) 1356 && isConstZero(x) 1357 && mem.Op == OpStaticCall 1358 && isSameSym(mem.Aux, "runtime.newobject") 1359 && c == config.ctxt.FixedFrameSize() + config.RegSize // offset of return value 1360 -> mem 1361 // nil checks just need to rewrite to something useless. 1362 // they will be deadcode eliminated soon afterwards. 1363 (NilCheck (Load (OffPtr [c] (SP)) (StaticCall {sym} _)) _) 1364 && isSameSym(sym, "runtime.newobject") 1365 && c == config.ctxt.FixedFrameSize() + config.RegSize // offset of return value 1366 && warnRule(fe.Debug_checknil(), v, "removed nil check") 1367 -> (Invalid) 1368 (NilCheck (OffPtr (Load (OffPtr [c] (SP)) (StaticCall {sym} _))) _) 1369 && isSameSym(sym, "runtime.newobject") 1370 && c == config.ctxt.FixedFrameSize() + config.RegSize // offset of return value 1371 && warnRule(fe.Debug_checknil(), v, "removed nil check") 1372 -> (Invalid) 1373 1374 // Evaluate constant address comparisons. 1375 (EqPtr x x) -> (ConstBool [1]) 1376 (NeqPtr x x) -> (ConstBool [0]) 1377 (EqPtr (Addr {a} _) (Addr {b} _)) -> (ConstBool [b2i(a == b)]) 1378 (NeqPtr (Addr {a} _) (Addr {b} _)) -> (ConstBool [b2i(a != b)]) 1379 (EqPtr (LocalAddr {a} _ _) (LocalAddr {b} _ _)) -> (ConstBool [b2i(a == b)]) 1380 (NeqPtr (LocalAddr {a} _ _) (LocalAddr {b} _ _)) -> (ConstBool [b2i(a != b)]) 1381 (EqPtr (OffPtr [o1] p1) p2) && isSamePtr(p1, p2) -> (ConstBool [b2i(o1 == 0)]) 1382 (NeqPtr (OffPtr [o1] p1) p2) && isSamePtr(p1, p2) -> (ConstBool [b2i(o1 != 0)]) 1383 (EqPtr (OffPtr [o1] p1) (OffPtr [o2] p2)) && isSamePtr(p1, p2) -> (ConstBool [b2i(o1 == o2)]) 1384 (NeqPtr (OffPtr [o1] p1) (OffPtr [o2] p2)) && isSamePtr(p1, p2) -> (ConstBool [b2i(o1 != o2)]) 1385 (EqPtr (Const(32|64) [c]) (Const(32|64) [d])) -> (ConstBool [b2i(c == d)]) 1386 (NeqPtr (Const(32|64) [c]) (Const(32|64) [d])) -> (ConstBool [b2i(c != d)]) 1387 1388 (EqPtr (LocalAddr _ _) (Addr _)) -> (ConstBool [0]) 1389 (NeqPtr (LocalAddr _ _) (Addr _)) -> (ConstBool [1]) 1390 (EqPtr (Addr _) (LocalAddr _ _)) -> (ConstBool [0]) 1391 (NeqPtr (Addr _) (LocalAddr _ _)) -> (ConstBool [1]) 1392 1393 // Simplify address comparisons. 1394 (EqPtr (AddPtr p1 o1) p2) && isSamePtr(p1, p2) -> (Not (IsNonNil o1)) 1395 (NeqPtr (AddPtr p1 o1) p2) && isSamePtr(p1, p2) -> (IsNonNil o1) 1396 (EqPtr (Const(32|64) [0]) p) -> (Not (IsNonNil p)) 1397 (NeqPtr (Const(32|64) [0]) p) -> (IsNonNil p) 1398 (EqPtr (ConstNil) p) -> (Not (IsNonNil p)) 1399 (NeqPtr (ConstNil) p) -> (IsNonNil p) 1400 1401 // Evaluate constant user nil checks. 1402 (IsNonNil (ConstNil)) -> (ConstBool [0]) 1403 (IsNonNil (Const(32|64) [c])) -> (ConstBool [b2i(c != 0)]) 1404 (IsNonNil (Addr _)) -> (ConstBool [1]) 1405 (IsNonNil (LocalAddr _ _)) -> (ConstBool [1]) 1406 1407 // Inline small or disjoint runtime.memmove calls with constant length. 1408 (StaticCall {sym} s1:(Store _ (Const(64|32) [sz]) s2:(Store _ src s3:(Store {t} _ dst mem)))) 1409 && isSameSym(sym,"runtime.memmove") 1410 && s1.Uses == 1 && s2.Uses == 1 && s3.Uses == 1 1411 && isInlinableMemmove(dst,src,sz,config) 1412 && clobber(s1) && clobber(s2) && clobber(s3) 1413 -> (Move {t.(*types.Type).Elem()} [sz] dst src mem) 1414 1415 // De-virtualize interface calls into static calls. 1416 // Note that (ITab (IMake)) doesn't get 1417 // rewritten until after the first opt pass, 1418 // so this rule should trigger reliably. 1419 (InterCall [argsize] (Load (OffPtr [off] (ITab (IMake (Addr {itab} (SB)) _))) _) mem) && devirt(v, itab, off) != nil -> 1420 (StaticCall [argsize] {devirt(v, itab, off)} mem) 1421 1422 // Move and Zero optimizations. 1423 // Move source and destination may overlap. 1424 1425 // Convert Moves into Zeros when the source is known to be zeros. 1426 (Move {t} [n] dst1 src mem:(Zero {t} [n] dst2 _)) && isSamePtr(src, dst2) 1427 -> (Zero {t} [n] dst1 mem) 1428 (Move {t} [n] dst1 src mem:(VarDef (Zero {t} [n] dst0 _))) && isSamePtr(src, dst0) 1429 -> (Zero {t} [n] dst1 mem) 1430 1431 // Don't Store to variables that are about to be overwritten by Move/Zero. 1432 (Zero {t1} [n] p1 store:(Store {t2} (OffPtr [o2] p2) _ mem)) 1433 && isSamePtr(p1, p2) && store.Uses == 1 1434 && n >= o2 + sizeof(t2) 1435 && clobber(store) 1436 -> (Zero {t1} [n] p1 mem) 1437 (Move {t1} [n] dst1 src1 store:(Store {t2} op:(OffPtr [o2] dst2) _ mem)) 1438 && isSamePtr(dst1, dst2) && store.Uses == 1 1439 && n >= o2 + sizeof(t2) 1440 && disjoint(src1, n, op, sizeof(t2)) 1441 && clobber(store) 1442 -> (Move {t1} [n] dst1 src1 mem) 1443 1444 // Don't Move to variables that are immediately completely overwritten. 1445 (Zero {t} [n] dst1 move:(Move {t} [n] dst2 _ mem)) 1446 && move.Uses == 1 1447 && isSamePtr(dst1, dst2) 1448 && clobber(move) 1449 -> (Zero {t} [n] dst1 mem) 1450 (Move {t} [n] dst1 src1 move:(Move {t} [n] dst2 _ mem)) 1451 && move.Uses == 1 1452 && isSamePtr(dst1, dst2) && disjoint(src1, n, dst2, n) 1453 && clobber(move) 1454 -> (Move {t} [n] dst1 src1 mem) 1455 (Zero {t} [n] dst1 vardef:(VarDef {x} move:(Move {t} [n] dst2 _ mem))) 1456 && move.Uses == 1 && vardef.Uses == 1 1457 && isSamePtr(dst1, dst2) 1458 && clobber(move) && clobber(vardef) 1459 -> (Zero {t} [n] dst1 (VarDef {x} mem)) 1460 (Move {t} [n] dst1 src1 vardef:(VarDef {x} move:(Move {t} [n] dst2 _ mem))) 1461 && move.Uses == 1 && vardef.Uses == 1 1462 && isSamePtr(dst1, dst2) && disjoint(src1, n, dst2, n) 1463 && clobber(move) && clobber(vardef) 1464 -> (Move {t} [n] dst1 src1 (VarDef {x} mem)) 1465 (Store {t1} op1:(OffPtr [o1] p1) d1 1466 m2:(Store {t2} op2:(OffPtr [0] p2) d2 1467 m3:(Move [n] p3 _ mem))) 1468 && m2.Uses == 1 && m3.Uses == 1 1469 && o1 == sizeof(t2) 1470 && n == sizeof(t2) + sizeof(t1) 1471 && isSamePtr(p1, p2) && isSamePtr(p2, p3) 1472 && clobber(m2) && clobber(m3) 1473 -> (Store {t1} op1 d1 (Store {t2} op2 d2 mem)) 1474 (Store {t1} op1:(OffPtr [o1] p1) d1 1475 m2:(Store {t2} op2:(OffPtr [o2] p2) d2 1476 m3:(Store {t3} op3:(OffPtr [0] p3) d3 1477 m4:(Move [n] p4 _ mem)))) 1478 && m2.Uses == 1 && m3.Uses == 1 && m4.Uses == 1 1479 && o2 == sizeof(t3) 1480 && o1-o2 == sizeof(t2) 1481 && n == sizeof(t3) + sizeof(t2) + sizeof(t1) 1482 && isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) 1483 && clobber(m2) && clobber(m3) && clobber(m4) 1484 -> (Store {t1} op1 d1 (Store {t2} op2 d2 (Store {t3} op3 d3 mem))) 1485 (Store {t1} op1:(OffPtr [o1] p1) d1 1486 m2:(Store {t2} op2:(OffPtr [o2] p2) d2 1487 m3:(Store {t3} op3:(OffPtr [o3] p3) d3 1488 m4:(Store {t4} op4:(OffPtr [0] p4) d4 1489 m5:(Move [n] p5 _ mem))))) 1490 && m2.Uses == 1 && m3.Uses == 1 && m4.Uses == 1 && m5.Uses == 1 1491 && o3 == sizeof(t4) 1492 && o2-o3 == sizeof(t3) 1493 && o1-o2 == sizeof(t2) 1494 && n == sizeof(t4) + sizeof(t3) + sizeof(t2) + sizeof(t1) 1495 && isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5) 1496 && clobber(m2) && clobber(m3) && clobber(m4) && clobber(m5) 1497 -> (Store {t1} op1 d1 (Store {t2} op2 d2 (Store {t3} op3 d3 (Store {t4} op4 d4 mem)))) 1498 1499 // Don't Zero variables that are immediately completely overwritten 1500 // before being accessed. 1501 (Move {t} [n] dst1 src1 zero:(Zero {t} [n] dst2 mem)) 1502 && zero.Uses == 1 1503 && isSamePtr(dst1, dst2) && disjoint(src1, n, dst2, n) 1504 && clobber(zero) 1505 -> (Move {t} [n] dst1 src1 mem) 1506 (Move {t} [n] dst1 src1 vardef:(VarDef {x} zero:(Zero {t} [n] dst2 mem))) 1507 && zero.Uses == 1 && vardef.Uses == 1 1508 && isSamePtr(dst1, dst2) && disjoint(src1, n, dst2, n) 1509 && clobber(zero) && clobber(vardef) 1510 -> (Move {t} [n] dst1 src1 (VarDef {x} mem)) 1511 (Store {t1} op1:(OffPtr [o1] p1) d1 1512 m2:(Store {t2} op2:(OffPtr [0] p2) d2 1513 m3:(Zero [n] p3 mem))) 1514 && m2.Uses == 1 && m3.Uses == 1 1515 && o1 == sizeof(t2) 1516 && n == sizeof(t2) + sizeof(t1) 1517 && isSamePtr(p1, p2) && isSamePtr(p2, p3) 1518 && clobber(m2) && clobber(m3) 1519 -> (Store {t1} op1 d1 (Store {t2} op2 d2 mem)) 1520 (Store {t1} op1:(OffPtr [o1] p1) d1 1521 m2:(Store {t2} op2:(OffPtr [o2] p2) d2 1522 m3:(Store {t3} op3:(OffPtr [0] p3) d3 1523 m4:(Zero [n] p4 mem)))) 1524 && m2.Uses == 1 && m3.Uses == 1 && m4.Uses == 1 1525 && o2 == sizeof(t3) 1526 && o1-o2 == sizeof(t2) 1527 && n == sizeof(t3) + sizeof(t2) + sizeof(t1) 1528 && isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) 1529 && clobber(m2) && clobber(m3) && clobber(m4) 1530 -> (Store {t1} op1 d1 (Store {t2} op2 d2 (Store {t3} op3 d3 mem))) 1531 (Store {t1} op1:(OffPtr [o1] p1) d1 1532 m2:(Store {t2} op2:(OffPtr [o2] p2) d2 1533 m3:(Store {t3} op3:(OffPtr [o3] p3) d3 1534 m4:(Store {t4} op4:(OffPtr [0] p4) d4 1535 m5:(Zero [n] p5 mem))))) 1536 && m2.Uses == 1 && m3.Uses == 1 && m4.Uses == 1 && m5.Uses == 1 1537 && o3 == sizeof(t4) 1538 && o2-o3 == sizeof(t3) 1539 && o1-o2 == sizeof(t2) 1540 && n == sizeof(t4) + sizeof(t3) + sizeof(t2) + sizeof(t1) 1541 && isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5) 1542 && clobber(m2) && clobber(m3) && clobber(m4) && clobber(m5) 1543 -> (Store {t1} op1 d1 (Store {t2} op2 d2 (Store {t3} op3 d3 (Store {t4} op4 d4 mem)))) 1544 1545 // Don't Move from memory if the values are likely to already be 1546 // in registers. 1547 (Move {t1} [n] dst p1 1548 mem:(Store {t2} op2:(OffPtr <tt2> [o2] p2) d1 1549 (Store {t3} op3:(OffPtr <tt3> [0] p3) d2 _))) 1550 && isSamePtr(p1, p2) && isSamePtr(p2, p3) 1551 && alignof(t2) <= alignof(t1) 1552 && alignof(t3) <= alignof(t1) 1553 && registerizable(b, t2) 1554 && registerizable(b, t3) 1555 && o2 == sizeof(t3) 1556 && n == sizeof(t2) + sizeof(t3) 1557 -> (Store {t2} (OffPtr <tt2> [o2] dst) d1 1558 (Store {t3} (OffPtr <tt3> [0] dst) d2 mem)) 1559 (Move {t1} [n] dst p1 1560 mem:(Store {t2} op2:(OffPtr <tt2> [o2] p2) d1 1561 (Store {t3} op3:(OffPtr <tt3> [o3] p3) d2 1562 (Store {t4} op4:(OffPtr <tt4> [0] p4) d3 _)))) 1563 && isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) 1564 && alignof(t2) <= alignof(t1) 1565 && alignof(t3) <= alignof(t1) 1566 && alignof(t4) <= alignof(t1) 1567 && registerizable(b, t2) 1568 && registerizable(b, t3) 1569 && registerizable(b, t4) 1570 && o3 == sizeof(t4) 1571 && o2-o3 == sizeof(t3) 1572 && n == sizeof(t2) + sizeof(t3) + sizeof(t4) 1573 -> (Store {t2} (OffPtr <tt2> [o2] dst) d1 1574 (Store {t3} (OffPtr <tt3> [o3] dst) d2 1575 (Store {t4} (OffPtr <tt4> [0] dst) d3 mem))) 1576 (Move {t1} [n] dst p1 1577 mem:(Store {t2} op2:(OffPtr <tt2> [o2] p2) d1 1578 (Store {t3} op3:(OffPtr <tt3> [o3] p3) d2 1579 (Store {t4} op4:(OffPtr <tt4> [o4] p4) d3 1580 (Store {t5} op5:(OffPtr <tt5> [0] p5) d4 _))))) 1581 && isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5) 1582 && alignof(t2) <= alignof(t1) 1583 && alignof(t3) <= alignof(t1) 1584 && alignof(t4) <= alignof(t1) 1585 && alignof(t5) <= alignof(t1) 1586 && registerizable(b, t2) 1587 && registerizable(b, t3) 1588 && registerizable(b, t4) 1589 && registerizable(b, t5) 1590 && o4 == sizeof(t5) 1591 && o3-o4 == sizeof(t4) 1592 && o2-o3 == sizeof(t3) 1593 && n == sizeof(t2) + sizeof(t3) + sizeof(t4) + sizeof(t5) 1594 -> (Store {t2} (OffPtr <tt2> [o2] dst) d1 1595 (Store {t3} (OffPtr <tt3> [o3] dst) d2 1596 (Store {t4} (OffPtr <tt4> [o4] dst) d3 1597 (Store {t5} (OffPtr <tt5> [0] dst) d4 mem)))) 1598 1599 // Same thing but with VarDef in the middle. 1600 (Move {t1} [n] dst p1 1601 mem:(VarDef 1602 (Store {t2} op2:(OffPtr <tt2> [o2] p2) d1 1603 (Store {t3} op3:(OffPtr <tt3> [0] p3) d2 _)))) 1604 && isSamePtr(p1, p2) && isSamePtr(p2, p3) 1605 && alignof(t2) <= alignof(t1) 1606 && alignof(t3) <= alignof(t1) 1607 && registerizable(b, t2) 1608 && registerizable(b, t3) 1609 && o2 == sizeof(t3) 1610 && n == sizeof(t2) + sizeof(t3) 1611 -> (Store {t2} (OffPtr <tt2> [o2] dst) d1 1612 (Store {t3} (OffPtr <tt3> [0] dst) d2 mem)) 1613 (Move {t1} [n] dst p1 1614 mem:(VarDef 1615 (Store {t2} op2:(OffPtr <tt2> [o2] p2) d1 1616 (Store {t3} op3:(OffPtr <tt3> [o3] p3) d2 1617 (Store {t4} op4:(OffPtr <tt4> [0] p4) d3 _))))) 1618 && isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) 1619 && alignof(t2) <= alignof(t1) 1620 && alignof(t3) <= alignof(t1) 1621 && alignof(t4) <= alignof(t1) 1622 && registerizable(b, t2) 1623 && registerizable(b, t3) 1624 && registerizable(b, t4) 1625 && o3 == sizeof(t4) 1626 && o2-o3 == sizeof(t3) 1627 && n == sizeof(t2) + sizeof(t3) + sizeof(t4) 1628 -> (Store {t2} (OffPtr <tt2> [o2] dst) d1 1629 (Store {t3} (OffPtr <tt3> [o3] dst) d2 1630 (Store {t4} (OffPtr <tt4> [0] dst) d3 mem))) 1631 (Move {t1} [n] dst p1 1632 mem:(VarDef 1633 (Store {t2} op2:(OffPtr <tt2> [o2] p2) d1 1634 (Store {t3} op3:(OffPtr <tt3> [o3] p3) d2 1635 (Store {t4} op4:(OffPtr <tt4> [o4] p4) d3 1636 (Store {t5} op5:(OffPtr <tt5> [0] p5) d4 _)))))) 1637 && isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5) 1638 && alignof(t2) <= alignof(t1) 1639 && alignof(t3) <= alignof(t1) 1640 && alignof(t4) <= alignof(t1) 1641 && alignof(t5) <= alignof(t1) 1642 && registerizable(b, t2) 1643 && registerizable(b, t3) 1644 && registerizable(b, t4) 1645 && registerizable(b, t5) 1646 && o4 == sizeof(t5) 1647 && o3-o4 == sizeof(t4) 1648 && o2-o3 == sizeof(t3) 1649 && n == sizeof(t2) + sizeof(t3) + sizeof(t4) + sizeof(t5) 1650 -> (Store {t2} (OffPtr <tt2> [o2] dst) d1 1651 (Store {t3} (OffPtr <tt3> [o3] dst) d2 1652 (Store {t4} (OffPtr <tt4> [o4] dst) d3 1653 (Store {t5} (OffPtr <tt5> [0] dst) d4 mem)))) 1654 1655 // Prefer to Zero and Store than to Move. 1656 (Move {t1} [n] dst p1 1657 mem:(Store {t2} op2:(OffPtr <tt2> [o2] p2) d1 1658 (Zero {t3} [n] p3 _))) 1659 && isSamePtr(p1, p2) && isSamePtr(p2, p3) 1660 && alignof(t2) <= alignof(t1) 1661 && alignof(t3) <= alignof(t1) 1662 && registerizable(b, t2) 1663 && n >= o2 + sizeof(t2) 1664 -> (Store {t2} (OffPtr <tt2> [o2] dst) d1 1665 (Zero {t1} [n] dst mem)) 1666 (Move {t1} [n] dst p1 1667 mem:(Store {t2} (OffPtr <tt2> [o2] p2) d1 1668 (Store {t3} (OffPtr <tt3> [o3] p3) d2 1669 (Zero {t4} [n] p4 _)))) 1670 && isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) 1671 && alignof(t2) <= alignof(t1) 1672 && alignof(t3) <= alignof(t1) 1673 && alignof(t4) <= alignof(t1) 1674 && registerizable(b, t2) 1675 && registerizable(b, t3) 1676 && n >= o2 + sizeof(t2) 1677 && n >= o3 + sizeof(t3) 1678 -> (Store {t2} (OffPtr <tt2> [o2] dst) d1 1679 (Store {t3} (OffPtr <tt3> [o3] dst) d2 1680 (Zero {t1} [n] dst mem))) 1681 (Move {t1} [n] dst p1 1682 mem:(Store {t2} (OffPtr <tt2> [o2] p2) d1 1683 (Store {t3} (OffPtr <tt3> [o3] p3) d2 1684 (Store {t4} (OffPtr <tt4> [o4] p4) d3 1685 (Zero {t5} [n] p5 _))))) 1686 && isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5) 1687 && alignof(t2) <= alignof(t1) 1688 && alignof(t3) <= alignof(t1) 1689 && alignof(t4) <= alignof(t1) 1690 && alignof(t5) <= alignof(t1) 1691 && registerizable(b, t2) 1692 && registerizable(b, t3) 1693 && registerizable(b, t4) 1694 && n >= o2 + sizeof(t2) 1695 && n >= o3 + sizeof(t3) 1696 && n >= o4 + sizeof(t4) 1697 -> (Store {t2} (OffPtr <tt2> [o2] dst) d1 1698 (Store {t3} (OffPtr <tt3> [o3] dst) d2 1699 (Store {t4} (OffPtr <tt4> [o4] dst) d3 1700 (Zero {t1} [n] dst mem)))) 1701 (Move {t1} [n] dst p1 1702 mem:(Store {t2} (OffPtr <tt2> [o2] p2) d1 1703 (Store {t3} (OffPtr <tt3> [o3] p3) d2 1704 (Store {t4} (OffPtr <tt4> [o4] p4) d3 1705 (Store {t5} (OffPtr <tt5> [o5] p5) d4 1706 (Zero {t6} [n] p6 _)))))) 1707 && isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5) && isSamePtr(p5, p6) 1708 && alignof(t2) <= alignof(t1) 1709 && alignof(t3) <= alignof(t1) 1710 && alignof(t4) <= alignof(t1) 1711 && alignof(t5) <= alignof(t1) 1712 && alignof(t6) <= alignof(t1) 1713 && registerizable(b, t2) 1714 && registerizable(b, t3) 1715 && registerizable(b, t4) 1716 && registerizable(b, t5) 1717 && n >= o2 + sizeof(t2) 1718 && n >= o3 + sizeof(t3) 1719 && n >= o4 + sizeof(t4) 1720 && n >= o5 + sizeof(t5) 1721 -> (Store {t2} (OffPtr <tt2> [o2] dst) d1 1722 (Store {t3} (OffPtr <tt3> [o3] dst) d2 1723 (Store {t4} (OffPtr <tt4> [o4] dst) d3 1724 (Store {t5} (OffPtr <tt5> [o5] dst) d4 1725 (Zero {t1} [n] dst mem))))) 1726 (Move {t1} [n] dst p1 1727 mem:(VarDef 1728 (Store {t2} op2:(OffPtr <tt2> [o2] p2) d1 1729 (Zero {t3} [n] p3 _)))) 1730 && isSamePtr(p1, p2) && isSamePtr(p2, p3) 1731 && alignof(t2) <= alignof(t1) 1732 && alignof(t3) <= alignof(t1) 1733 && registerizable(b, t2) 1734 && n >= o2 + sizeof(t2) 1735 -> (Store {t2} (OffPtr <tt2> [o2] dst) d1 1736 (Zero {t1} [n] dst mem)) 1737 (Move {t1} [n] dst p1 1738 mem:(VarDef 1739 (Store {t2} (OffPtr <tt2> [o2] p2) d1 1740 (Store {t3} (OffPtr <tt3> [o3] p3) d2 1741 (Zero {t4} [n] p4 _))))) 1742 && isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) 1743 && alignof(t2) <= alignof(t1) 1744 && alignof(t3) <= alignof(t1) 1745 && alignof(t4) <= alignof(t1) 1746 && registerizable(b, t2) 1747 && registerizable(b, t3) 1748 && n >= o2 + sizeof(t2) 1749 && n >= o3 + sizeof(t3) 1750 -> (Store {t2} (OffPtr <tt2> [o2] dst) d1 1751 (Store {t3} (OffPtr <tt3> [o3] dst) d2 1752 (Zero {t1} [n] dst mem))) 1753 (Move {t1} [n] dst p1 1754 mem:(VarDef 1755 (Store {t2} (OffPtr <tt2> [o2] p2) d1 1756 (Store {t3} (OffPtr <tt3> [o3] p3) d2 1757 (Store {t4} (OffPtr <tt4> [o4] p4) d3 1758 (Zero {t5} [n] p5 _)))))) 1759 && isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5) 1760 && alignof(t2) <= alignof(t1) 1761 && alignof(t3) <= alignof(t1) 1762 && alignof(t4) <= alignof(t1) 1763 && alignof(t5) <= alignof(t1) 1764 && registerizable(b, t2) 1765 && registerizable(b, t3) 1766 && registerizable(b, t4) 1767 && n >= o2 + sizeof(t2) 1768 && n >= o3 + sizeof(t3) 1769 && n >= o4 + sizeof(t4) 1770 -> (Store {t2} (OffPtr <tt2> [o2] dst) d1 1771 (Store {t3} (OffPtr <tt3> [o3] dst) d2 1772 (Store {t4} (OffPtr <tt4> [o4] dst) d3 1773 (Zero {t1} [n] dst mem)))) 1774 (Move {t1} [n] dst p1 1775 mem:(VarDef 1776 (Store {t2} (OffPtr <tt2> [o2] p2) d1 1777 (Store {t3} (OffPtr <tt3> [o3] p3) d2 1778 (Store {t4} (OffPtr <tt4> [o4] p4) d3 1779 (Store {t5} (OffPtr <tt5> [o5] p5) d4 1780 (Zero {t6} [n] p6 _))))))) 1781 && isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5) && isSamePtr(p5, p6) 1782 && alignof(t2) <= alignof(t1) 1783 && alignof(t3) <= alignof(t1) 1784 && alignof(t4) <= alignof(t1) 1785 && alignof(t5) <= alignof(t1) 1786 && alignof(t6) <= alignof(t1) 1787 && registerizable(b, t2) 1788 && registerizable(b, t3) 1789 && registerizable(b, t4) 1790 && registerizable(b, t5) 1791 && n >= o2 + sizeof(t2) 1792 && n >= o3 + sizeof(t3) 1793 && n >= o4 + sizeof(t4) 1794 && n >= o5 + sizeof(t5) 1795 -> (Store {t2} (OffPtr <tt2> [o2] dst) d1 1796 (Store {t3} (OffPtr <tt3> [o3] dst) d2 1797 (Store {t4} (OffPtr <tt4> [o4] dst) d3 1798 (Store {t5} (OffPtr <tt5> [o5] dst) d4 1799 (Zero {t1} [n] dst mem))))) 1800 1801 (StaticCall {sym} x) && needRaceCleanup(sym,v) -> x 1802 1803 // Collapse moving A -> B -> C into just A -> C. 1804 // Later passes (deadstore, elim unread auto) will remove the A -> B move, if possible. 1805 // This happens most commonly when B is an autotmp inserted earlier 1806 // during compilation to ensure correctness. 1807 (Move {t1} [s1] dst tmp1 midmem:(Move {t2} [s2] tmp2 src _)) 1808 && s1 == s2 1809 && t1.(*types.Type).Compare(t2.(*types.Type)) == types.CMPeq 1810 && isSamePtr(tmp1, tmp2) 1811 -> (Move {t1} [s1] dst src midmem) 1812 1813 // Elide self-moves. This only happens rarely (e.g test/fixedbugs/bug277.go). 1814 // However, this rule is needed to prevent the previous rule from looping forever in such cases. 1815 (Move dst src mem) && isSamePtr(dst, src) -> mem