generic.rules 69.7 KB
Newer Older
1 2 3 4
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

5 6 7 8 9 10 11 12 13 14 15 16 17
// Simplifications that apply to all backend architectures. As an example, this
// Go source code
//
// y := 0 * x
//
// can be translated into y := 0 without losing any information, which saves a
// pointless multiplication instruction. Other .rules files in this directory
// (for example AMD64.rules) contain rules specific to the architecture in the
// filename. The rules here apply to every architecture.
//
// The code for parsing this file lives in rulegen.go; this file generates
// ssa/rewritegeneric.go.

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 24 25 26
//  - the first occurrence of a variable defines that variable.  Subsequent
//    uses must match (be == to) the first use.
//  - v is defined to be the value matched.
//  - an additional conditional can be provided after the match pattern with "&&".
27 28
// on the generated side
//  - the type of the top-level expression is the same as the one on the left-hand side.
29 30
//  - the type of any subexpressions must be specified explicitly (or
//    be specified in the op's type field).
31
//  - auxint will be 0 if not specified.
32 33 34 35 36 37 38 39
//  - aux will be nil if not specified.

// blocks are specified using the following format:
// (kind controlvalue succ0 succ1 ...)
// controlvalue must be "nil" or a value expression
// succ* fields must be variables
// For now, the generated successors must be a permutation of the matched successors.

40
// constant folding
41 42 43 44 45 46
(Trunc16to8  (Const16  [c])) -> (Const8   [int64(int8(c))])
(Trunc32to8  (Const32  [c])) -> (Const8   [int64(int8(c))])
(Trunc32to16 (Const32  [c])) -> (Const16  [int64(int16(c))])
(Trunc64to8  (Const64  [c])) -> (Const8   [int64(int8(c))])
(Trunc64to16 (Const64  [c])) -> (Const16  [int64(int16(c))])
(Trunc64to32 (Const64  [c])) -> (Const32  [int64(int32(c))])
47 48
(Cvt64Fto32F (Const64F [c])) -> (Const32F [f2i(float64(i2f32(c)))])
(Cvt32Fto64F (Const32F [c])) -> (Const64F [c]) // c is already a 64 bit float
49 50 51 52 53 54 55
(Cvt32to32F  (Const32  [c])) -> (Const32F [f2i(float64(float32(int32(c))))])
(Cvt32to64F  (Const32  [c])) -> (Const64F [f2i(float64(int32(c)))])
(Cvt64to32F  (Const64  [c])) -> (Const32F [f2i(float64(float32(c)))])
(Cvt64to64F  (Const64  [c])) -> (Const64F [f2i(float64(c))])
(Cvt32Fto32  (Const32F [c])) -> (Const32  [int64(int32(i2f(c)))])
(Cvt32Fto64  (Const32F [c])) -> (Const64  [int64(i2f(c))])
(Cvt64Fto32  (Const64F [c])) -> (Const32  [int64(int32(i2f(c)))])
56
(Cvt64Fto64  (Const64F [c])) -> (Const64  [int64(i2f(c))])
57 58
(Round32F x:(Const32F)) -> x
(Round64F x:(Const64F)) -> x
59

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
(Trunc16to8  (ZeroExt8to16  x)) -> x
(Trunc32to8  (ZeroExt8to32  x)) -> x
(Trunc32to16 (ZeroExt8to32  x)) -> (ZeroExt8to16  x)
(Trunc32to16 (ZeroExt16to32 x)) -> x
(Trunc64to8  (ZeroExt8to64  x)) -> x
(Trunc64to16 (ZeroExt8to64  x)) -> (ZeroExt8to16  x)
(Trunc64to16 (ZeroExt16to64 x)) -> x
(Trunc64to32 (ZeroExt8to64  x)) -> (ZeroExt8to32  x)
(Trunc64to32 (ZeroExt16to64 x)) -> (ZeroExt16to32 x)
(Trunc64to32 (ZeroExt32to64 x)) -> x
(Trunc16to8  (SignExt8to16  x)) -> x
(Trunc32to8  (SignExt8to32  x)) -> x
(Trunc32to16 (SignExt8to32  x)) -> (SignExt8to16  x)
(Trunc32to16 (SignExt16to32 x)) -> x
(Trunc64to8  (SignExt8to64  x)) -> x
(Trunc64to16 (SignExt8to64  x)) -> (SignExt8to16  x)
(Trunc64to16 (SignExt16to64 x)) -> x
(Trunc64to32 (SignExt8to64  x)) -> (SignExt8to32  x)
(Trunc64to32 (SignExt16to64 x)) -> (SignExt16to32 x)
(Trunc64to32 (SignExt32to64 x)) -> x

81 82 83 84 85 86 87 88 89 90 91 92 93
(ZeroExt8to16  (Const8  [c])) -> (Const16 [int64( uint8(c))])
(ZeroExt8to32  (Const8  [c])) -> (Const32 [int64( uint8(c))])
(ZeroExt8to64  (Const8  [c])) -> (Const64 [int64( uint8(c))])
(ZeroExt16to32 (Const16 [c])) -> (Const32 [int64(uint16(c))])
(ZeroExt16to64 (Const16 [c])) -> (Const64 [int64(uint16(c))])
(ZeroExt32to64 (Const32 [c])) -> (Const64 [int64(uint32(c))])
(SignExt8to16  (Const8  [c])) -> (Const16 [int64(  int8(c))])
(SignExt8to32  (Const8  [c])) -> (Const32 [int64(  int8(c))])
(SignExt8to64  (Const8  [c])) -> (Const64 [int64(  int8(c))])
(SignExt16to32 (Const16 [c])) -> (Const32 [int64( int16(c))])
(SignExt16to64 (Const16 [c])) -> (Const64 [int64( int16(c))])
(SignExt32to64 (Const32 [c])) -> (Const64 [int64( int32(c))])

94 95 96 97 98 99
(Neg8   (Const8   [c])) -> (Const8   [int64( -int8(c))])
(Neg16  (Const16  [c])) -> (Const16  [int64(-int16(c))])
(Neg32  (Const32  [c])) -> (Const32  [int64(-int32(c))])
(Neg64  (Const64  [c])) -> (Const64  [-c])
(Neg32F (Const32F [c])) && i2f(c) != 0 -> (Const32F [f2i(-i2f(c))])
(Neg64F (Const64F [c])) && i2f(c) != 0 -> (Const64F [f2i(-i2f(c))])
100

101 102 103 104
(Add8   (Const8 [c])   (Const8 [d]))   -> (Const8  [int64(int8(c+d))])
(Add16  (Const16 [c])  (Const16 [d]))  -> (Const16 [int64(int16(c+d))])
(Add32  (Const32 [c])  (Const32 [d]))  -> (Const32 [int64(int32(c+d))])
(Add64  (Const64 [c])  (Const64 [d]))  -> (Const64 [c+d])
105
(Add32F (Const32F [c]) (Const32F [d])) ->
106 107
        (Const32F [f2i(float64(i2f32(c) + i2f32(d)))]) // ensure we combine the operands with 32 bit precision
(Add64F (Const64F [c]) (Const64F [d])) -> (Const64F [f2i(i2f(c) + i2f(d))])
108
(AddPtr <t> x (Const64 [c])) -> (OffPtr <t> x [c])
109
(AddPtr <t> x (Const32 [c])) -> (OffPtr <t> x [c])
110

111 112 113 114
(Sub8   (Const8 [c]) (Const8 [d]))     -> (Const8 [int64(int8(c-d))])
(Sub16  (Const16 [c]) (Const16 [d]))   -> (Const16 [int64(int16(c-d))])
(Sub32  (Const32 [c]) (Const32 [d]))   -> (Const32 [int64(int32(c-d))])
(Sub64  (Const64 [c]) (Const64 [d]))   -> (Const64 [c-d])
115
(Sub32F (Const32F [c]) (Const32F [d])) ->
116 117
        (Const32F [f2i(float64(i2f32(c) - i2f32(d)))])
(Sub64F (Const64F [c]) (Const64F [d])) -> (Const64F [f2i(i2f(c) - i2f(d))])
118

119 120 121 122
(Mul8   (Const8 [c])   (Const8 [d]))   -> (Const8  [int64(int8(c*d))])
(Mul16  (Const16 [c])  (Const16 [d]))  -> (Const16 [int64(int16(c*d))])
(Mul32  (Const32 [c])  (Const32 [d]))  -> (Const32 [int64(int32(c*d))])
(Mul64  (Const64 [c])  (Const64 [d]))  -> (Const64 [c*d])
123
(Mul32F (Const32F [c]) (Const32F [d])) ->
124 125
        (Const32F [f2i(float64(i2f32(c) * i2f32(d)))])
(Mul64F (Const64F [c]) (Const64F [d])) -> (Const64F [f2i(i2f(c) * i2f(d))])
126

127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
(And8   (Const8 [c])   (Const8 [d]))   -> (Const8  [int64(int8(c&d))])
(And16  (Const16 [c])  (Const16 [d]))  -> (Const16 [int64(int16(c&d))])
(And32  (Const32 [c])  (Const32 [d]))  -> (Const32 [int64(int32(c&d))])
(And64  (Const64 [c])  (Const64 [d]))  -> (Const64 [c&d])

(Or8   (Const8 [c])   (Const8 [d]))   -> (Const8  [int64(int8(c|d))])
(Or16  (Const16 [c])  (Const16 [d]))  -> (Const16 [int64(int16(c|d))])
(Or32  (Const32 [c])  (Const32 [d]))  -> (Const32 [int64(int32(c|d))])
(Or64  (Const64 [c])  (Const64 [d]))  -> (Const64 [c|d])

(Xor8   (Const8 [c])   (Const8 [d]))   -> (Const8  [int64(int8(c^d))])
(Xor16  (Const16 [c])  (Const16 [d]))  -> (Const16 [int64(int16(c^d))])
(Xor32  (Const32 [c])  (Const32 [d]))  -> (Const32 [int64(int32(c^d))])
(Xor64  (Const64 [c])  (Const64 [d]))  -> (Const64 [c^d])

142 143 144 145 146 147 148 149 150 151 152
(Div8   (Const8  [c])  (Const8  [d])) && d != 0 -> (Const8  [int64(int8(c)/int8(d))])
(Div16  (Const16 [c])  (Const16 [d])) && d != 0 -> (Const16 [int64(int16(c)/int16(d))])
(Div32  (Const32 [c])  (Const32 [d])) && d != 0 -> (Const32 [int64(int32(c)/int32(d))])
(Div64  (Const64 [c])  (Const64 [d])) && d != 0 -> (Const64 [c/d])
(Div8u  (Const8  [c])  (Const8  [d])) && d != 0 -> (Const8  [int64(int8(uint8(c)/uint8(d)))])
(Div16u (Const16 [c])  (Const16 [d])) && d != 0 -> (Const16 [int64(int16(uint16(c)/uint16(d)))])
(Div32u (Const32 [c])  (Const32 [d])) && d != 0 -> (Const32 [int64(int32(uint32(c)/uint32(d)))])
(Div64u (Const64 [c])  (Const64 [d])) && d != 0 -> (Const64 [int64(uint64(c)/uint64(d))])
(Div32F (Const32F [c]) (Const32F [d])) -> (Const32F [f2i(float64(i2f32(c) / i2f32(d)))])
(Div64F (Const64F [c]) (Const64F [d])) -> (Const64F [f2i(i2f(c) / i2f(d))])

153 154
(Not (ConstBool [c])) -> (ConstBool [1-c])

155 156 157 158 159 160 161
// Convert x * 1 to x.
(Mul8  (Const8  [1]) x) -> x
(Mul16 (Const16 [1]) x) -> x
(Mul32 (Const32 [1]) x) -> x
(Mul64 (Const64 [1]) x) -> x

// Convert x * -1 to -x.
162 163 164 165 166
(Mul8  (Const8  [-1]) x) -> (Neg8  x)
(Mul16 (Const16 [-1]) x) -> (Neg16 x)
(Mul32 (Const32 [-1]) x) -> (Neg32 x)
(Mul64 (Const64 [-1]) x) -> (Neg64 x)

167
// Convert multiplication by a power of two to a shift.
168 169 170 171 172 173 174 175
(Mul8  <t> n (Const8  [c])) && isPowerOfTwo(c) -> (Lsh8x64  <t> n (Const64 <typ.UInt64> [log2(c)]))
(Mul16 <t> n (Const16 [c])) && isPowerOfTwo(c) -> (Lsh16x64 <t> n (Const64 <typ.UInt64> [log2(c)]))
(Mul32 <t> n (Const32 [c])) && isPowerOfTwo(c) -> (Lsh32x64 <t> n (Const64 <typ.UInt64> [log2(c)]))
(Mul64 <t> n (Const64 [c])) && isPowerOfTwo(c) -> (Lsh64x64 <t> n (Const64 <typ.UInt64> [log2(c)]))
(Mul8  <t> n (Const8  [c])) && t.IsSigned() && isPowerOfTwo(-c) -> (Neg8  (Lsh8x64  <t> n (Const64 <typ.UInt64> [log2(-c)])))
(Mul16 <t> n (Const16 [c])) && t.IsSigned() && isPowerOfTwo(-c) -> (Neg16 (Lsh16x64 <t> n (Const64 <typ.UInt64> [log2(-c)])))
(Mul32 <t> n (Const32 [c])) && t.IsSigned() && isPowerOfTwo(-c) -> (Neg32 (Lsh32x64 <t> n (Const64 <typ.UInt64> [log2(-c)])))
(Mul64 <t> n (Const64 [c])) && t.IsSigned() && isPowerOfTwo(-c) -> (Neg64 (Lsh64x64 <t> n (Const64 <typ.UInt64> [log2(-c)])))
176

177 178 179 180
(Mod8  (Const8  [c]) (Const8  [d])) && d != 0 -> (Const8  [int64(int8(c % d))])
(Mod16 (Const16 [c]) (Const16 [d])) && d != 0 -> (Const16 [int64(int16(c % d))])
(Mod32 (Const32 [c]) (Const32 [d])) && d != 0 -> (Const32 [int64(int32(c % d))])
(Mod64 (Const64 [c]) (Const64 [d])) && d != 0 -> (Const64 [c % d])
181

182 183 184 185
(Mod8u  (Const8 [c])  (Const8  [d])) && d != 0 -> (Const8  [int64(uint8(c) % uint8(d))])
(Mod16u (Const16 [c]) (Const16 [d])) && d != 0 -> (Const16 [int64(uint16(c) % uint16(d))])
(Mod32u (Const32 [c]) (Const32 [d])) && d != 0 -> (Const32 [int64(uint32(c) % uint32(d))])
(Mod64u (Const64 [c]) (Const64 [d])) && d != 0 -> (Const64 [int64(uint64(c) % uint64(d))])
186

187 188 189 190 191
(Lsh64x64  (Const64 [c]) (Const64 [d])) -> (Const64 [c << uint64(d)])
(Rsh64x64  (Const64 [c]) (Const64 [d])) -> (Const64 [c >> uint64(d)])
(Rsh64Ux64 (Const64 [c]) (Const64 [d])) -> (Const64 [int64(uint64(c) >> uint64(d))])
(Lsh32x64  (Const32 [c]) (Const64 [d])) -> (Const32 [int64(int32(c) << uint64(d))])
(Rsh32x64  (Const32 [c]) (Const64 [d])) -> (Const32 [int64(int32(c) >> uint64(d))])
192
(Rsh32Ux64 (Const32 [c]) (Const64 [d])) -> (Const32 [int64(int32(uint32(c) >> uint64(d)))])
193 194
(Lsh16x64  (Const16 [c]) (Const64 [d])) -> (Const16 [int64(int16(c) << uint64(d))])
(Rsh16x64  (Const16 [c]) (Const64 [d])) -> (Const16 [int64(int16(c) >> uint64(d))])
195
(Rsh16Ux64 (Const16 [c]) (Const64 [d])) -> (Const16 [int64(int16(uint16(c) >> uint64(d)))])
196 197
(Lsh8x64   (Const8  [c]) (Const64 [d])) -> (Const8  [int64(int8(c) << uint64(d))])
(Rsh8x64   (Const8  [c]) (Const64 [d])) -> (Const8  [int64(int8(c) >> uint64(d))])
198
(Rsh8Ux64  (Const8  [c]) (Const64 [d])) -> (Const8  [int64(int8(uint8(c) >> uint64(d)))])
199

200
// Fold IsInBounds when the range of the index cannot exceed the limit.
201 202
(IsInBounds (ZeroExt8to32  _) (Const32 [c])) && (1 << 8)  <= c -> (ConstBool [1])
(IsInBounds (ZeroExt8to64  _) (Const64 [c])) && (1 << 8)  <= c -> (ConstBool [1])
203
(IsInBounds (ZeroExt16to32 _) (Const32 [c])) && (1 << 16) <= c -> (ConstBool [1])
204
(IsInBounds (ZeroExt16to64 _) (Const64 [c])) && (1 << 16) <= c -> (ConstBool [1])
205
(IsInBounds x x) -> (ConstBool [0])
206 207 208 209 210 211 212 213 214 215
(IsInBounds                (And8  (Const8  [c]) _)  (Const8  [d])) && 0 <= c && c < d -> (ConstBool [1])
(IsInBounds (ZeroExt8to16  (And8  (Const8  [c]) _)) (Const16 [d])) && 0 <= c && c < d -> (ConstBool [1])
(IsInBounds (ZeroExt8to32  (And8  (Const8  [c]) _)) (Const32 [d])) && 0 <= c && c < d -> (ConstBool [1])
(IsInBounds (ZeroExt8to64  (And8  (Const8  [c]) _)) (Const64 [d])) && 0 <= c && c < d -> (ConstBool [1])
(IsInBounds                (And16 (Const16 [c]) _)  (Const16 [d])) && 0 <= c && c < d -> (ConstBool [1])
(IsInBounds (ZeroExt16to32 (And16 (Const16 [c]) _)) (Const32 [d])) && 0 <= c && c < d -> (ConstBool [1])
(IsInBounds (ZeroExt16to64 (And16 (Const16 [c]) _)) (Const64 [d])) && 0 <= c && c < d -> (ConstBool [1])
(IsInBounds                (And32 (Const32 [c]) _)  (Const32 [d])) && 0 <= c && c < d -> (ConstBool [1])
(IsInBounds (ZeroExt32to64 (And32 (Const32 [c]) _)) (Const64 [d])) && 0 <= c && c < d -> (ConstBool [1])
(IsInBounds                (And64 (Const64 [c]) _)  (Const64 [d])) && 0 <= c && c < d -> (ConstBool [1])
216 217
(IsInBounds (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(0 <= c && c < d)])
(IsInBounds (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(0 <= c && c < d)])
218 219 220
// (Mod64u x y) is always between 0 (inclusive) and y (exclusive).
(IsInBounds (Mod32u _ y) y) -> (ConstBool [1])
(IsInBounds (Mod64u _ y) y) -> (ConstBool [1])
221 222 223 224 225 226 227 228 229 230 231
// Right shifting a unsigned number limits its value.
(IsInBounds (ZeroExt8to64  (Rsh8Ux64  _ (Const64 [c]))) (Const64 [d])) && 0 < c && c <  8 && 1<<uint( 8-c)-1 < d -> (ConstBool [1])
(IsInBounds (ZeroExt8to32  (Rsh8Ux64  _ (Const64 [c]))) (Const32 [d])) && 0 < c && c <  8 && 1<<uint( 8-c)-1 < d -> (ConstBool [1])
(IsInBounds (ZeroExt8to16  (Rsh8Ux64  _ (Const64 [c]))) (Const16 [d])) && 0 < c && c <  8 && 1<<uint( 8-c)-1 < d -> (ConstBool [1])
(IsInBounds                (Rsh8Ux64  _ (Const64 [c]))  (Const64 [d])) && 0 < c && c <  8 && 1<<uint( 8-c)-1 < d -> (ConstBool [1])
(IsInBounds (ZeroExt16to64 (Rsh16Ux64 _ (Const64 [c]))) (Const64 [d])) && 0 < c && c < 16 && 1<<uint(16-c)-1 < d -> (ConstBool [1])
(IsInBounds (ZeroExt16to32 (Rsh16Ux64 _ (Const64 [c]))) (Const64 [d])) && 0 < c && c < 16 && 1<<uint(16-c)-1 < d -> (ConstBool [1])
(IsInBounds                (Rsh16Ux64 _ (Const64 [c]))  (Const64 [d])) && 0 < c && c < 16 && 1<<uint(16-c)-1 < d -> (ConstBool [1])
(IsInBounds (ZeroExt32to64 (Rsh32Ux64 _ (Const64 [c]))) (Const64 [d])) && 0 < c && c < 32 && 1<<uint(32-c)-1 < d -> (ConstBool [1])
(IsInBounds                (Rsh32Ux64 _ (Const64 [c]))  (Const64 [d])) && 0 < c && c < 32 && 1<<uint(32-c)-1 < d -> (ConstBool [1])
(IsInBounds                (Rsh64Ux64 _ (Const64 [c]))  (Const64 [d])) && 0 < c && c < 64 && 1<<uint(64-c)-1 < d -> (ConstBool [1])
232

233
(IsSliceInBounds x x) -> (ConstBool [1])
234 235
(IsSliceInBounds (And32 (Const32 [c]) _) (Const32 [d])) && 0 <= c && c <= d -> (ConstBool [1])
(IsSliceInBounds (And64 (Const64 [c]) _) (Const64 [d])) && 0 <= c && c <= d -> (ConstBool [1])
236 237
(IsSliceInBounds (Const32 [0]) _) -> (ConstBool [1])
(IsSliceInBounds (Const64 [0]) _) -> (ConstBool [1])
238 239
(IsSliceInBounds (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(0 <= c && c <= d)])
(IsSliceInBounds (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(0 <= c && c <= d)])
240
(IsSliceInBounds (SliceLen x) (SliceCap x)) -> (ConstBool [1])
241

242 243 244
(Eq64 x x) -> (ConstBool [1])
(Eq32 x x) -> (ConstBool [1])
(Eq16 x x) -> (ConstBool [1])
245
(Eq8  x x) -> (ConstBool [1])
246 247 248
(EqB (ConstBool [c]) (ConstBool [d])) -> (ConstBool [b2i(c == d)])
(EqB (ConstBool [0]) x) -> (Not x)
(EqB (ConstBool [1]) x) -> x
249

250 251 252
(Neq64 x x) -> (ConstBool [0])
(Neq32 x x) -> (ConstBool [0])
(Neq16 x x) -> (ConstBool [0])
253
(Neq8  x x) -> (ConstBool [0])
254 255 256
(NeqB (ConstBool [c]) (ConstBool [d])) -> (ConstBool [b2i(c != d)])
(NeqB (ConstBool [0]) x) -> x
(NeqB (ConstBool [1]) x) -> (Not x)
257
(NeqB (Not x) (Not y)) -> (NeqB x y)
258

259
(Eq64 (Const64 <t> [c]) (Add64 (Const64 <t> [d]) x)) -> (Eq64 (Const64 <t> [c-d]) x)
260 261
(Eq32 (Const32 <t> [c]) (Add32 (Const32 <t> [d]) x)) -> (Eq32 (Const32 <t> [int64(int32(c-d))]) x)
(Eq16 (Const16 <t> [c]) (Add16 (Const16 <t> [d]) x)) -> (Eq16 (Const16 <t> [int64(int16(c-d))]) x)
262
(Eq8  (Const8  <t> [c]) (Add8  (Const8  <t> [d]) x)) -> (Eq8  (Const8 <t> [int64(int8(c-d))]) x)
263 264

(Neq64 (Const64 <t> [c]) (Add64 (Const64 <t> [d]) x)) -> (Neq64 (Const64 <t> [c-d]) x)
265 266
(Neq32 (Const32 <t> [c]) (Add32 (Const32 <t> [d]) x)) -> (Neq32 (Const32 <t> [int64(int32(c-d))]) x)
(Neq16 (Const16 <t> [c]) (Add16 (Const16 <t> [d]) x)) -> (Neq16 (Const16 <t> [int64(int16(c-d))]) x)
267
(Neq8  (Const8  <t> [c]) (Add8  (Const8  <t> [d]) x)) -> (Neq8 (Const8 <t> [int64(int8(c-d))]) x)
268

269
// Canonicalize x-const to x+(-const)
270
(Sub64 x (Const64 <t> [c])) && x.Op != OpConst64 -> (Add64 (Const64 <t> [-c]) x)
271 272
(Sub32 x (Const32 <t> [c])) && x.Op != OpConst32 -> (Add32 (Const32 <t> [int64(int32(-c))]) x)
(Sub16 x (Const16 <t> [c])) && x.Op != OpConst16 -> (Add16 (Const16 <t> [int64(int16(-c))]) x)
273
(Sub8  x (Const8  <t> [c])) && x.Op != OpConst8  -> (Add8  (Const8  <t> [int64(int8(-c))]) x)
274

275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
// fold negation into comparison operators
(Not (Eq64 x y)) -> (Neq64 x y)
(Not (Eq32 x y)) -> (Neq32 x y)
(Not (Eq16 x y)) -> (Neq16 x y)
(Not (Eq8  x y)) -> (Neq8  x y)
(Not (EqB  x y)) -> (NeqB  x y)

(Not (Neq64 x y)) -> (Eq64 x y)
(Not (Neq32 x y)) -> (Eq32 x y)
(Not (Neq16 x y)) -> (Eq16 x y)
(Not (Neq8  x y)) -> (Eq8  x y)
(Not (NeqB  x y)) -> (EqB  x y)

(Not (Greater64 x y)) -> (Leq64 x y)
(Not (Greater32 x y)) -> (Leq32 x y)
(Not (Greater16 x y)) -> (Leq16 x y)
(Not (Greater8  x y)) -> (Leq8  x y)

(Not (Greater64U x y)) -> (Leq64U x y)
(Not (Greater32U x y)) -> (Leq32U x y)
(Not (Greater16U x y)) -> (Leq16U x y)
(Not (Greater8U  x y)) -> (Leq8U  x y)

(Not (Geq64 x y)) -> (Less64 x y)
(Not (Geq32 x y)) -> (Less32 x y)
(Not (Geq16 x y)) -> (Less16 x y)
(Not (Geq8  x y)) -> (Less8  x y)

(Not (Geq64U x y)) -> (Less64U x y)
(Not (Geq32U x y)) -> (Less32U x y)
(Not (Geq16U x y)) -> (Less16U x y)
(Not (Geq8U  x y)) -> (Less8U  x y)

(Not (Less64 x y)) -> (Geq64 x y)
(Not (Less32 x y)) -> (Geq32 x y)
(Not (Less16 x y)) -> (Geq16 x y)
(Not (Less8  x y)) -> (Geq8  x y)

(Not (Less64U x y)) -> (Geq64U x y)
(Not (Less32U x y)) -> (Geq32U x y)
(Not (Less16U x y)) -> (Geq16U x y)
(Not (Less8U  x y)) -> (Geq8U  x y)

(Not (Leq64 x y)) -> (Greater64 x y)
(Not (Leq32 x y)) -> (Greater32 x y)
(Not (Leq16 x y)) -> (Greater16 x y)
(Not (Leq8  x y)) -> (Greater8 x y)

(Not (Leq64U x y)) -> (Greater64U x y)
(Not (Leq32U x y)) -> (Greater32U x y)
(Not (Leq16U x y)) -> (Greater16U x y)
(Not (Leq8U  x y)) -> (Greater8U  x y)

328 329
// Distribute multiplication c * (d+x) -> c*d + c*x. Useful for:
// a[i].b = ...; a[i+1].b = ...
330 331 332 333
(Mul64 (Const64 <t> [c]) (Add64 <t> (Const64 <t> [d]) x)) ->
  (Add64 (Const64 <t> [c*d]) (Mul64 <t> (Const64 <t> [c]) x))
(Mul32 (Const32 <t> [c]) (Add32 <t> (Const32 <t> [d]) x)) ->
  (Add32 (Const32 <t> [int64(int32(c*d))]) (Mul32 <t> (Const32 <t> [c]) x))
334

335 336 337 338 339 340
// Rewrite x*y + x*z  to  x*(y+z)
(Add64 <t> (Mul64 x y) (Mul64 x z)) -> (Mul64 x (Add64 <t> y z))
(Add32 <t> (Mul32 x y) (Mul32 x z)) -> (Mul32 x (Add32 <t> y z))
(Add16 <t> (Mul16 x y) (Mul16 x z)) -> (Mul16 x (Add16 <t> y z))
(Add8  <t> (Mul8  x y) (Mul8  x z)) -> (Mul8  x (Add8  <t> y z))

341 342 343 344 345 346
// Rewrite x*y - x*z  to  x*(y-z)
(Sub64 <t> (Mul64 x y) (Mul64 x z)) -> (Mul64 x (Sub64 <t> y z))
(Sub32 <t> (Mul32 x y) (Mul32 x z)) -> (Mul32 x (Sub32 <t> y z))
(Sub16 <t> (Mul16 x y) (Mul16 x z)) -> (Mul16 x (Sub16 <t> y z))
(Sub8  <t> (Mul8  x y) (Mul8  x z)) -> (Mul8  x (Sub8  <t> y z))

347 348 349 350
// rewrite shifts of 8/16/32 bit consts into 64 bit consts to reduce
// the number of the other rewrite rules for const shifts
(Lsh64x32  <t> x (Const32 [c])) -> (Lsh64x64  x (Const64 <t> [int64(uint32(c))]))
(Lsh64x16  <t> x (Const16 [c])) -> (Lsh64x64  x (Const64 <t> [int64(uint16(c))]))
351
(Lsh64x8   <t> x (Const8  [c])) -> (Lsh64x64  x (Const64 <t> [int64(uint8(c))]))
352 353
(Rsh64x32  <t> x (Const32 [c])) -> (Rsh64x64  x (Const64 <t> [int64(uint32(c))]))
(Rsh64x16  <t> x (Const16 [c])) -> (Rsh64x64  x (Const64 <t> [int64(uint16(c))]))
354
(Rsh64x8   <t> x (Const8  [c])) -> (Rsh64x64  x (Const64 <t> [int64(uint8(c))]))
355 356
(Rsh64Ux32 <t> x (Const32 [c])) -> (Rsh64Ux64 x (Const64 <t> [int64(uint32(c))]))
(Rsh64Ux16 <t> x (Const16 [c])) -> (Rsh64Ux64 x (Const64 <t> [int64(uint16(c))]))
357
(Rsh64Ux8  <t> x (Const8  [c])) -> (Rsh64Ux64 x (Const64 <t> [int64(uint8(c))]))
358 359 360

(Lsh32x32  <t> x (Const32 [c])) -> (Lsh32x64  x (Const64 <t> [int64(uint32(c))]))
(Lsh32x16  <t> x (Const16 [c])) -> (Lsh32x64  x (Const64 <t> [int64(uint16(c))]))
361
(Lsh32x8   <t> x (Const8  [c])) -> (Lsh32x64  x (Const64 <t> [int64(uint8(c))]))
362 363
(Rsh32x32  <t> x (Const32 [c])) -> (Rsh32x64  x (Const64 <t> [int64(uint32(c))]))
(Rsh32x16  <t> x (Const16 [c])) -> (Rsh32x64  x (Const64 <t> [int64(uint16(c))]))
364
(Rsh32x8   <t> x (Const8  [c])) -> (Rsh32x64  x (Const64 <t> [int64(uint8(c))]))
365 366
(Rsh32Ux32 <t> x (Const32 [c])) -> (Rsh32Ux64 x (Const64 <t> [int64(uint32(c))]))
(Rsh32Ux16 <t> x (Const16 [c])) -> (Rsh32Ux64 x (Const64 <t> [int64(uint16(c))]))
367
(Rsh32Ux8  <t> x (Const8  [c])) -> (Rsh32Ux64 x (Const64 <t> [int64(uint8(c))]))
368 369 370

(Lsh16x32  <t> x (Const32 [c])) -> (Lsh16x64  x (Const64 <t> [int64(uint32(c))]))
(Lsh16x16  <t> x (Const16 [c])) -> (Lsh16x64  x (Const64 <t> [int64(uint16(c))]))
371
(Lsh16x8   <t> x (Const8  [c])) -> (Lsh16x64  x (Const64 <t> [int64(uint8(c))]))
372 373
(Rsh16x32  <t> x (Const32 [c])) -> (Rsh16x64  x (Const64 <t> [int64(uint32(c))]))
(Rsh16x16  <t> x (Const16 [c])) -> (Rsh16x64  x (Const64 <t> [int64(uint16(c))]))
374
(Rsh16x8   <t> x (Const8  [c])) -> (Rsh16x64  x (Const64 <t> [int64(uint8(c))]))
375 376
(Rsh16Ux32 <t> x (Const32 [c])) -> (Rsh16Ux64 x (Const64 <t> [int64(uint32(c))]))
(Rsh16Ux16 <t> x (Const16 [c])) -> (Rsh16Ux64 x (Const64 <t> [int64(uint16(c))]))
377
(Rsh16Ux8  <t> x (Const8  [c])) -> (Rsh16Ux64 x (Const64 <t> [int64(uint8(c))]))
378 379 380

(Lsh8x32  <t> x (Const32 [c])) -> (Lsh8x64  x (Const64 <t> [int64(uint32(c))]))
(Lsh8x16  <t> x (Const16 [c])) -> (Lsh8x64  x (Const64 <t> [int64(uint16(c))]))
381
(Lsh8x8   <t> x (Const8  [c])) -> (Lsh8x64  x (Const64 <t> [int64(uint8(c))]))
382 383
(Rsh8x32  <t> x (Const32 [c])) -> (Rsh8x64  x (Const64 <t> [int64(uint32(c))]))
(Rsh8x16  <t> x (Const16 [c])) -> (Rsh8x64  x (Const64 <t> [int64(uint16(c))]))
384
(Rsh8x8   <t> x (Const8  [c])) -> (Rsh8x64  x (Const64 <t> [int64(uint8(c))]))
385 386
(Rsh8Ux32 <t> x (Const32 [c])) -> (Rsh8Ux64 x (Const64 <t> [int64(uint32(c))]))
(Rsh8Ux16 <t> x (Const16 [c])) -> (Rsh8Ux64 x (Const64 <t> [int64(uint16(c))]))
387
(Rsh8Ux8  <t> x (Const8  [c])) -> (Rsh8Ux64 x (Const64 <t> [int64(uint8(c))]))
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402

// shifts by zero
(Lsh64x64  x (Const64 [0])) -> x
(Rsh64x64  x (Const64 [0])) -> x
(Rsh64Ux64 x (Const64 [0])) -> x
(Lsh32x64  x (Const64 [0])) -> x
(Rsh32x64  x (Const64 [0])) -> x
(Rsh32Ux64 x (Const64 [0])) -> x
(Lsh16x64  x (Const64 [0])) -> x
(Rsh16x64  x (Const64 [0])) -> x
(Rsh16Ux64 x (Const64 [0])) -> x
(Lsh8x64   x (Const64 [0])) -> x
(Rsh8x64   x (Const64 [0])) -> x
(Rsh8Ux64  x (Const64 [0])) -> x

403
// zero shifted.
404 405 406 407 408 409 410 411 412 413 414 415
(Lsh64x(64|32|16|8)  (Const64 [0]) _) -> (Const64 [0])
(Rsh64x(64|32|16|8)  (Const64 [0]) _) -> (Const64 [0])
(Rsh64Ux(64|32|16|8) (Const64 [0]) _) -> (Const64 [0])
(Lsh32x(64|32|16|8)  (Const32 [0]) _) -> (Const32 [0])
(Rsh32x(64|32|16|8)  (Const32 [0]) _) -> (Const32 [0])
(Rsh32Ux(64|32|16|8) (Const32 [0]) _) -> (Const32 [0])
(Lsh16x(64|32|16|8)  (Const16 [0]) _) -> (Const16 [0])
(Rsh16x(64|32|16|8)  (Const16 [0]) _) -> (Const16 [0])
(Rsh16Ux(64|32|16|8) (Const16 [0]) _) -> (Const16 [0])
(Lsh8x(64|32|16|8)   (Const8  [0]) _) -> (Const8  [0])
(Rsh8x(64|32|16|8)   (Const8  [0]) _) -> (Const8  [0])
(Rsh8Ux(64|32|16|8)  (Const8  [0]) _) -> (Const8  [0])
416

417
// large left shifts of all values, and right shifts of unsigned values
418 419 420 421
((Lsh64|Rsh64U)x64  _ (Const64 [c])) && uint64(c) >= 64 -> (Const64 [0])
((Lsh32|Rsh32U)x64  _ (Const64 [c])) && uint64(c) >= 32 -> (Const32 [0])
((Lsh16|Rsh16U)x64  _ (Const64 [c])) && uint64(c) >= 16 -> (Const16 [0])
((Lsh8|Rsh8U)x64    _ (Const64 [c])) && uint64(c) >= 8  -> (Const8  [0])
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438

// combine const shifts
(Lsh64x64 <t> (Lsh64x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Lsh64x64 x (Const64 <t> [c+d]))
(Lsh32x64 <t> (Lsh32x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Lsh32x64 x (Const64 <t> [c+d]))
(Lsh16x64 <t> (Lsh16x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Lsh16x64 x (Const64 <t> [c+d]))
(Lsh8x64  <t> (Lsh8x64  x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Lsh8x64  x (Const64 <t> [c+d]))

(Rsh64x64 <t> (Rsh64x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh64x64 x (Const64 <t> [c+d]))
(Rsh32x64 <t> (Rsh32x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh32x64 x (Const64 <t> [c+d]))
(Rsh16x64 <t> (Rsh16x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh16x64 x (Const64 <t> [c+d]))
(Rsh8x64  <t> (Rsh8x64  x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh8x64  x (Const64 <t> [c+d]))

(Rsh64Ux64 <t> (Rsh64Ux64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh64Ux64 x (Const64 <t> [c+d]))
(Rsh32Ux64 <t> (Rsh32Ux64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh32Ux64 x (Const64 <t> [c+d]))
(Rsh16Ux64 <t> (Rsh16Ux64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh16Ux64 x (Const64 <t> [c+d]))
(Rsh8Ux64  <t> (Rsh8Ux64  x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh8Ux64  x (Const64 <t> [c+d]))

439
// ((x >> c1) << c2) >> c3
440
(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]))
441
  && uint64(c1) >= uint64(c2) && uint64(c3) >= uint64(c2) && !uaddOvf(c1-c2, c3)
442
  -> (Rsh(64|32|16|8)Ux64 x (Const64 <typ.UInt64> [c1-c2+c3]))
443 444

// ((x << c1) >> c2) << c3
445
(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]))
446
  && uint64(c1) >= uint64(c2) && uint64(c3) >= uint64(c2) && !uaddOvf(c1-c2, c3)
447
  -> (Lsh(64|32|16|8)x64 x (Const64 <typ.UInt64> [c1-c2+c3]))
448

449
// replace shifts with zero extensions
450 451 452 453 454 455
(Rsh16Ux64 (Lsh16x64 x (Const64  [8])) (Const64  [8])) -> (ZeroExt8to16  (Trunc16to8  <typ.UInt8>  x))
(Rsh32Ux64 (Lsh32x64 x (Const64 [24])) (Const64 [24])) -> (ZeroExt8to32  (Trunc32to8  <typ.UInt8>  x))
(Rsh64Ux64 (Lsh64x64 x (Const64 [56])) (Const64 [56])) -> (ZeroExt8to64  (Trunc64to8  <typ.UInt8>  x))
(Rsh32Ux64 (Lsh32x64 x (Const64 [16])) (Const64 [16])) -> (ZeroExt16to32 (Trunc32to16 <typ.UInt16> x))
(Rsh64Ux64 (Lsh64x64 x (Const64 [48])) (Const64 [48])) -> (ZeroExt16to64 (Trunc64to16 <typ.UInt16> x))
(Rsh64Ux64 (Lsh64x64 x (Const64 [32])) (Const64 [32])) -> (ZeroExt32to64 (Trunc64to32 <typ.UInt32> x))
456 457

// replace shifts with sign extensions
458 459 460 461 462 463
(Rsh16x64 (Lsh16x64 x (Const64  [8])) (Const64  [8])) -> (SignExt8to16  (Trunc16to8  <typ.Int8>  x))
(Rsh32x64 (Lsh32x64 x (Const64 [24])) (Const64 [24])) -> (SignExt8to32  (Trunc32to8  <typ.Int8>  x))
(Rsh64x64 (Lsh64x64 x (Const64 [56])) (Const64 [56])) -> (SignExt8to64  (Trunc64to8  <typ.Int8>  x))
(Rsh32x64 (Lsh32x64 x (Const64 [16])) (Const64 [16])) -> (SignExt16to32 (Trunc32to16 <typ.Int16> x))
(Rsh64x64 (Lsh64x64 x (Const64 [48])) (Const64 [48])) -> (SignExt16to64 (Trunc64to16 <typ.Int16> x))
(Rsh64x64 (Lsh64x64 x (Const64 [32])) (Const64 [32])) -> (SignExt32to64 (Trunc64to32 <typ.Int32> x))
464

465
// constant comparisons
466 467 468 469 470 471
(Eq(64|32|16|8)      (Const(64|32|16|8) [c]) (Const(64|32|16|8) [d])) -> (ConstBool [b2i(c == d)])
(Neq(64|32|16|8)     (Const(64|32|16|8) [c]) (Const(64|32|16|8) [d])) -> (ConstBool [b2i(c != d)])
(Greater(64|32|16|8) (Const(64|32|16|8) [c]) (Const(64|32|16|8) [d])) -> (ConstBool [b2i(c > d)])
(Geq(64|32|16|8)     (Const(64|32|16|8) [c]) (Const(64|32|16|8) [d])) -> (ConstBool [b2i(c >= d)])
(Less(64|32|16|8)    (Const(64|32|16|8) [c]) (Const(64|32|16|8) [d])) -> (ConstBool [b2i(c < d)])
(Leq(64|32|16|8)     (Const(64|32|16|8) [c]) (Const(64|32|16|8) [d])) -> (ConstBool [b2i(c <= d)])
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492

(Greater64U (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(uint64(c) > uint64(d))])
(Greater32U (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(uint32(c) > uint32(d))])
(Greater16U (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(uint16(c) > uint16(d))])
(Greater8U  (Const8  [c]) (Const8  [d])) -> (ConstBool [b2i(uint8(c)  > uint8(d))])

(Geq64U (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(uint64(c) >= uint64(d))])
(Geq32U (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(uint32(c) >= uint32(d))])
(Geq16U (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(uint16(c) >= uint16(d))])
(Geq8U  (Const8  [c]) (Const8  [d])) -> (ConstBool [b2i(uint8(c)  >= uint8(d))])

(Less64U (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(uint64(c) < uint64(d))])
(Less32U (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(uint32(c) < uint32(d))])
(Less16U (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(uint16(c) < uint16(d))])
(Less8U  (Const8  [c]) (Const8  [d])) -> (ConstBool [b2i(uint8(c)  < uint8(d))])

(Leq64U (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(uint64(c) <= uint64(d))])
(Leq32U (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(uint32(c) <= uint32(d))])
(Leq16U (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(uint16(c) <= uint16(d))])
(Leq8U  (Const8  [c]) (Const8  [d])) -> (ConstBool [b2i(uint8(c)  <= uint8(d))])

493
// constant floating point comparisons
494 495 496 497 498 499
(Eq(64|32)F      (Const(64|32)F [c]) (Const(64|32)F [d])) -> (ConstBool [b2i(i2f(c) == i2f(d))])
(Neq(64|32)F     (Const(64|32)F [c]) (Const(64|32)F [d])) -> (ConstBool [b2i(i2f(c) != i2f(d))])
(Greater(64|32)F (Const(64|32)F [c]) (Const(64|32)F [d])) -> (ConstBool [b2i(i2f(c) > i2f(d))])
(Geq(64|32)F     (Const(64|32)F [c]) (Const(64|32)F [d])) -> (ConstBool [b2i(i2f(c) >= i2f(d))])
(Less(64|32)F    (Const(64|32)F [c]) (Const(64|32)F [d])) -> (ConstBool [b2i(i2f(c) < i2f(d))])
(Leq(64|32)F     (Const(64|32)F [c]) (Const(64|32)F [d])) -> (ConstBool [b2i(i2f(c) <= i2f(d))])
500

501
// simplifications
502 503
(Or(64|32|16|8) x x) -> x
(Or(64|32|16|8) (Const(64|32|16|8) [0]) x) -> x
504 505 506
(Or64 (Const64 [-1]) _) -> (Const64 [-1])
(Or32 (Const32 [-1]) _) -> (Const32 [-1])
(Or16 (Const16 [-1]) _) -> (Const16 [-1])
507
(Or8  (Const8  [-1]) _) -> (Const8  [-1])
508 509
(And(64|32|16|8) x x) -> x
(And(64|32|16|8) (Const(64|32|16|8) [-1]) x) -> x
510 511 512
(And64 (Const64 [0]) _) -> (Const64 [0])
(And32 (Const32 [0]) _) -> (Const32 [0])
(And16 (Const16 [0]) _) -> (Const16 [0])
513
(And8  (Const8  [0]) _) -> (Const8  [0])
514 515 516
(Xor64 x x) -> (Const64 [0])
(Xor32 x x) -> (Const32 [0])
(Xor16 x x) -> (Const16 [0])
517
(Xor8  x x) -> (Const8  [0])
518 519
(Xor(64|32|16|8) (Const(64|32|16|8) [0]) x) -> x
(Add(64|32|16|8) (Const(64|32|16|8) [0]) x) -> x
520 521 522
(Sub64 x x) -> (Const64 [0])
(Sub32 x x) -> (Const32 [0])
(Sub16 x x) -> (Const16 [0])
523
(Sub8  x x) -> (Const8  [0])
524 525 526
(Mul64 (Const64 [0]) _) -> (Const64 [0])
(Mul32 (Const32 [0]) _) -> (Const32 [0])
(Mul16 (Const16 [0]) _) -> (Const16 [0])
527
(Mul8  (Const8  [0]) _) -> (Const8  [0])
528
(Com(64|32|16|8) (Com(64|32|16|8)  x)) -> x
529 530 531 532
(Com8  (Const8  [c])) -> (Const8  [^c])
(Com16 (Const16 [c])) -> (Const16 [^c])
(Com32 (Const32 [c])) -> (Const32 [^c])
(Com64 (Const64 [c])) -> (Const64 [^c])
533
(Neg8  (Sub8  x y)) -> (Sub8  y x)
534 535 536
(Neg16 (Sub16 x y)) -> (Sub16 y x)
(Neg32 (Sub32 x y)) -> (Sub32 y x)
(Neg64 (Sub64 x y)) -> (Sub64 y x)
537 538 539 540
(Add8  (Const8  [1]) (Com8  x)) -> (Neg8  x)
(Add16 (Const16 [1]) (Com16 x)) -> (Neg16 x)
(Add32 (Const32 [1]) (Com32 x)) -> (Neg32 x)
(Add64 (Const64 [1]) (Com64 x)) -> (Neg64 x)
541

542 543 544 545 546 547 548 549
(And64 x (And64 x y)) -> (And64 x y)
(And32 x (And32 x y)) -> (And32 x y)
(And16 x (And16 x y)) -> (And16 x y)
(And8  x (And8  x y)) -> (And8  x y)
(Or64 x (Or64 x y)) -> (Or64 x y)
(Or32 x (Or32 x y)) -> (Or32 x y)
(Or16 x (Or16 x y)) -> (Or16 x y)
(Or8  x (Or8  x y)) -> (Or8  x y)
550
(Xor(64|32|16|8) x (Xor(64|32|16|8) x y)) -> y
551

552 553 554 555 556 557 558 559 560 561 562 563
// Ands clear bits. Ors set bits.
// If a subsequent Or will set all the bits
// that an And cleared, we can skip the And.
// This happens in bitmasking code like:
//   x &^= 3 << shift // clear two old bits
//   x  |= v << shift // set two new bits
// when shift is a small constant and v ends up a constant 3.
(Or8  (And8  x (Const8  [c2])) (Const8  <t> [c1])) && ^(c1 | c2) == 0 -> (Or8  (Const8  <t> [c1]) x)
(Or16 (And16 x (Const16 [c2])) (Const16 <t> [c1])) && ^(c1 | c2) == 0 -> (Or16 (Const16 <t> [c1]) x)
(Or32 (And32 x (Const32 [c2])) (Const32 <t> [c1])) && ^(c1 | c2) == 0 -> (Or32 (Const32 <t> [c1]) x)
(Or64 (And64 x (Const64 [c2])) (Const64 <t> [c1])) && ^(c1 | c2) == 0 -> (Or64 (Const64 <t> [c1]) x)

564
(Trunc64to8  (And64 (Const64 [y]) x)) && y&0xFF == 0xFF -> (Trunc64to8 x)
565 566
(Trunc64to16 (And64 (Const64 [y]) x)) && y&0xFFFF == 0xFFFF -> (Trunc64to16 x)
(Trunc64to32 (And64 (Const64 [y]) x)) && y&0xFFFFFFFF == 0xFFFFFFFF -> (Trunc64to32 x)
567
(Trunc32to8  (And32 (Const32 [y]) x)) && y&0xFF == 0xFF -> (Trunc32to8 x)
568
(Trunc32to16 (And32 (Const32 [y]) x)) && y&0xFFFF == 0xFFFF -> (Trunc32to16 x)
569
(Trunc16to8  (And16 (Const16 [y]) x)) && y&0xFF == 0xFF -> (Trunc16to8 x)
570

571 572 573 574 575 576 577 578 579 580 581 582 583 584
(ZeroExt8to64  (Trunc64to8  x:(Rsh64Ux64 _ (Const64 [s])))) && s >= 56 -> x
(ZeroExt16to64 (Trunc64to16 x:(Rsh64Ux64 _ (Const64 [s])))) && s >= 48 -> x
(ZeroExt32to64 (Trunc64to32 x:(Rsh64Ux64 _ (Const64 [s])))) && s >= 32 -> x
(ZeroExt8to32  (Trunc32to8  x:(Rsh32Ux64 _ (Const64 [s])))) && s >= 24 -> x
(ZeroExt16to32 (Trunc32to16 x:(Rsh32Ux64 _ (Const64 [s])))) && s >= 16 -> x
(ZeroExt8to16  (Trunc16to8  x:(Rsh16Ux64 _ (Const64 [s])))) && s >= 8 -> x

(SignExt8to64  (Trunc64to8  x:(Rsh64x64 _ (Const64 [s])))) && s >= 56 -> x
(SignExt16to64 (Trunc64to16 x:(Rsh64x64 _ (Const64 [s])))) && s >= 48 -> x
(SignExt32to64 (Trunc64to32 x:(Rsh64x64 _ (Const64 [s])))) && s >= 32 -> x
(SignExt8to32  (Trunc32to8  x:(Rsh32x64 _ (Const64 [s])))) && s >= 24 -> x
(SignExt16to32 (Trunc32to16 x:(Rsh32x64 _ (Const64 [s])))) && s >= 16 -> x
(SignExt8to16  (Trunc16to8  x:(Rsh16x64 _ (Const64 [s])))) && s >= 8 -> x

585 586 587 588 589
(Slicemask (Const32 [x])) && x > 0 -> (Const32 [-1])
(Slicemask (Const32 [0]))          -> (Const32 [0])
(Slicemask (Const64 [x])) && x > 0 -> (Const64 [-1])
(Slicemask (Const64 [0]))          -> (Const64 [0])

590
// Rewrite AND of consts as shifts if possible, slightly faster for 64 bit operands
591
// leading zeros can be shifted left, then right
592 593
(And64 <t> (Const64 [y]) x) && nlz(y) + nto(y) == 64 && nto(y) >= 32
  -> (Rsh64Ux64 (Lsh64x64 <t> x (Const64 <t> [nlz(y)])) (Const64 <t> [nlz(y)]))
594
// trailing zeros can be shifted right, then left
595 596
(And64 <t> (Const64 [y]) x) && nlo(y) + ntz(y) == 64 && ntz(y) >= 32
  -> (Lsh64x64 (Rsh64Ux64 <t> x (Const64 <t> [ntz(y)])) (Const64 <t> [ntz(y)]))
597

598
// simplifications often used for lengths.  e.g. len(s[i:i+5])==5
599 600
(Sub(64|32|16|8) (Add(64|32|16|8) x y) x) -> y
(Sub(64|32|16|8) (Add(64|32|16|8) x y) y) -> x
601

602
// basic phi simplifications
603
(Phi (Const8  [c]) (Const8  [c])) -> (Const8  [c])
604 605
(Phi (Const16 [c]) (Const16 [c])) -> (Const16 [c])
(Phi (Const32 [c]) (Const32 [c])) -> (Const32 [c])
606 607
(Phi (Const64 [c]) (Const64 [c])) -> (Const64 [c])

608 609 610
// user nil checks
(NeqPtr p (ConstNil)) -> (IsNonNil p)
(EqPtr p (ConstNil)) -> (Not (IsNonNil p))
611
(IsNonNil (ConstNil)) -> (ConstBool [0])
612

613
// slice and interface comparisons
614 615 616 617 618 619
// The frontend ensures that we can only compare against nil,
// so we need only compare the first word (interface type or slice ptr).
(EqInter x y)  -> (EqPtr  (ITab x) (ITab y))
(NeqInter x y) -> (NeqPtr (ITab x) (ITab y))
(EqSlice x y)  -> (EqPtr  (SlicePtr x) (SlicePtr y))
(NeqSlice x y) -> (NeqPtr (SlicePtr x) (SlicePtr y))
620

621
// Load of store of same address, with compatibly typed value and same size
622
(Load <t1> p1 (Store {t2} p2 x _)) && isSamePtr(p1,p2) && t1.Compare(x.Type) == types.CMPeq && t1.Size() == t2.(*types.Type).Size() -> x
623

624 625 626 627 628 629
// Pass constants through math.Float{32,64}bits and math.Float{32,64}frombits
(Load <t1> p1 (Store {t2} p2 (Const64  [x]) _)) && isSamePtr(p1,p2) && t2.(*types.Type).Size() == 8 && is64BitFloat(t1) -> (Const64F [x])
(Load <t1> p1 (Store {t2} p2 (Const32  [x]) _)) && isSamePtr(p1,p2) && t2.(*types.Type).Size() == 4 && is32BitFloat(t1) -> (Const32F [f2i(float64(math.Float32frombits(uint32(x))))])
(Load <t1> p1 (Store {t2} p2 (Const64F [x]) _)) && isSamePtr(p1,p2) && t2.(*types.Type).Size() == 8 && is64BitInt(t1)   -> (Const64  [x])
(Load <t1> p1 (Store {t2} p2 (Const32F [x]) _)) && isSamePtr(p1,p2) && t2.(*types.Type).Size() == 4 && is32BitInt(t1)   -> (Const32  [int64(int32(math.Float32bits(float32(i2f(x)))))])

630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
// Eliminate stores of values that have just been loaded from the same location.
// We also handle the common case where there are some intermediate stores to non-overlapping struct fields.
(Store {t1} p1 (Load <t2> p2 mem) mem) &&
	isSamePtr(p1, p2) &&
	t2.Size() == t1.(*types.Type).Size() -> mem
(Store {t1} (OffPtr [o1] p1) (Load <t2> (OffPtr [o1] p2) oldmem) mem:(Store {t3} (OffPtr [o3] p3) _ oldmem)) &&
	isSamePtr(p1, p2) &&
	isSamePtr(p1, p3) &&
	t2.Size() == t1.(*types.Type).Size() &&
	!overlap(o1, t2.Size(), o3, t3.(*types.Type).Size()) -> mem
(Store {t1} (OffPtr [o1] p1) (Load <t2> (OffPtr [o1] p2) oldmem) mem:(Store {t3} (OffPtr [o3] p3) _ (Store {t4} (OffPtr [o4] p4) _ oldmem))) &&
	isSamePtr(p1, p2) &&
	isSamePtr(p1, p3) &&
	isSamePtr(p1, p4) &&
	t2.Size() == t1.(*types.Type).Size() &&
	!overlap(o1, t2.Size(), o3, t3.(*types.Type).Size()) &&
	!overlap(o1, t2.Size(), o4, t4.(*types.Type).Size()) -> mem
(Store {t1} (OffPtr [o1] p1) (Load <t2> (OffPtr [o1] p2) oldmem) mem:(Store {t3} (OffPtr [o3] p3) _ (Store {t4} (OffPtr [o4] p4) _ (Store {t5} (OffPtr [o5] p5) _ oldmem)))) &&
	isSamePtr(p1, p2) &&
	isSamePtr(p1, p3) &&
	isSamePtr(p1, p4) &&
	isSamePtr(p1, p5) &&
	t2.Size() == t1.(*types.Type).Size() &&
	!overlap(o1, t2.Size(), o3, t3.(*types.Type).Size()) &&
	!overlap(o1, t2.Size(), o4, t4.(*types.Type).Size()) &&
	!overlap(o1, t2.Size(), o5, t5.(*types.Type).Size()) -> mem

657 658
// Collapse OffPtr
(OffPtr (OffPtr p [b]) [a]) -> (OffPtr p [a+b])
659
(OffPtr p [0]) && v.Type.Compare(p.Type) == types.CMPeq -> p
660

661
// indexing operations
662
// Note: bounds check has already been done
663 664
(PtrIndex <t> ptr idx) && config.PtrSize == 4 -> (AddPtr ptr (Mul32 <typ.Int> idx (Const32 <typ.Int> [t.ElemType().Size()])))
(PtrIndex <t> ptr idx) && config.PtrSize == 8 -> (AddPtr ptr (Mul64 <typ.Int> idx (Const64 <typ.Int> [t.ElemType().Size()])))
665 666 667 668 669 670 671 672 673 674 675 676 677

// struct operations
(StructSelect (StructMake1 x)) -> x
(StructSelect [0] (StructMake2 x _)) -> x
(StructSelect [1] (StructMake2 _ x)) -> x
(StructSelect [0] (StructMake3 x _ _)) -> x
(StructSelect [1] (StructMake3 _ x _)) -> x
(StructSelect [2] (StructMake3 _ _ x)) -> x
(StructSelect [0] (StructMake4 x _ _ _)) -> x
(StructSelect [1] (StructMake4 _ x _ _)) -> x
(StructSelect [2] (StructMake4 _ _ x _)) -> x
(StructSelect [3] (StructMake4 _ _ _ x)) -> x

678
(Load <t> _ _) && t.IsStruct() && t.NumFields() == 0 && fe.CanSSA(t) ->
679
  (StructMake0)
680
(Load <t> ptr mem) && t.IsStruct() && t.NumFields() == 1 && fe.CanSSA(t) ->
681
  (StructMake1
682
    (Load <t.FieldType(0)> (OffPtr <t.FieldType(0).PtrTo()> [0] ptr) mem))
683
(Load <t> ptr mem) && t.IsStruct() && t.NumFields() == 2 && fe.CanSSA(t) ->
684
  (StructMake2
685
    (Load <t.FieldType(0)> (OffPtr <t.FieldType(0).PtrTo()> [0]             ptr) mem)
686
    (Load <t.FieldType(1)> (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] ptr) mem))
687
(Load <t> ptr mem) && t.IsStruct() && t.NumFields() == 3 && fe.CanSSA(t) ->
688
  (StructMake3
689
    (Load <t.FieldType(0)> (OffPtr <t.FieldType(0).PtrTo()> [0]             ptr) mem)
690 691
    (Load <t.FieldType(1)> (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] ptr) mem)
    (Load <t.FieldType(2)> (OffPtr <t.FieldType(2).PtrTo()> [t.FieldOff(2)] ptr) mem))
692
(Load <t> ptr mem) && t.IsStruct() && t.NumFields() == 4 && fe.CanSSA(t) ->
693
  (StructMake4
694
    (Load <t.FieldType(0)> (OffPtr <t.FieldType(0).PtrTo()> [0]             ptr) mem)
695 696 697 698
    (Load <t.FieldType(1)> (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] ptr) mem)
    (Load <t.FieldType(2)> (OffPtr <t.FieldType(2).PtrTo()> [t.FieldOff(2)] ptr) mem)
    (Load <t.FieldType(3)> (OffPtr <t.FieldType(3).PtrTo()> [t.FieldOff(3)] ptr) mem))

699
(StructSelect [i] x:(Load <t> ptr mem)) && !fe.CanSSA(t) ->
700
  @x.Block (Load <v.Type> (OffPtr <v.Type.PtrTo()> [t.FieldOff(int(i))] ptr) mem)
701 702 703

(Store _ (StructMake0) mem) -> mem
(Store dst (StructMake1 <t> f0) mem) ->
704
  (Store {t.FieldType(0)} (OffPtr <t.FieldType(0).PtrTo()> [0] dst) f0 mem)
705
(Store dst (StructMake2 <t> f0 f1) mem) ->
706
  (Store {t.FieldType(1)}
707 708
    (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] dst)
    f1
709
    (Store {t.FieldType(0)}
710
      (OffPtr <t.FieldType(0).PtrTo()> [0] dst)
711
        f0 mem))
712
(Store dst (StructMake3 <t> f0 f1 f2) mem) ->
713
  (Store {t.FieldType(2)}
714 715
    (OffPtr <t.FieldType(2).PtrTo()> [t.FieldOff(2)] dst)
    f2
716
    (Store {t.FieldType(1)}
717 718
      (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] dst)
      f1
719
      (Store {t.FieldType(0)}
720 721
        (OffPtr <t.FieldType(0).PtrTo()> [0] dst)
          f0 mem)))
722
(Store dst (StructMake4 <t> f0 f1 f2 f3) mem) ->
723
  (Store {t.FieldType(3)}
724 725
    (OffPtr <t.FieldType(3).PtrTo()> [t.FieldOff(3)] dst)
    f3
726
    (Store {t.FieldType(2)}
727 728
      (OffPtr <t.FieldType(2).PtrTo()> [t.FieldOff(2)] dst)
      f2
729
      (Store {t.FieldType(1)}
730 731
        (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] dst)
        f1
732
        (Store {t.FieldType(0)}
733 734
          (OffPtr <t.FieldType(0).PtrTo()> [0] dst)
            f0 mem))))
735

736
// Putting struct{*byte} and similar into direct interfaces.
737
(IMake typ (StructMake1 val)) -> (IMake typ val)
738
(StructSelect [0] x:(IData _)) -> x
739

740
// un-SSAable values use mem->mem copies
741 742 743 744
(Store {t} dst (Load src mem) mem) && !fe.CanSSA(t.(*types.Type)) ->
	(Move {t} [t.(*types.Type).Size()] dst src mem)
(Store {t} dst (Load src mem) (VarDef {x} mem)) && !fe.CanSSA(t.(*types.Type)) ->
	(Move {t} [t.(*types.Type).Size()] dst src (VarDef {x} mem))
745

746 747 748 749 750 751
// array ops
(ArraySelect (ArrayMake1 x)) -> x

(Load <t> _ _) && t.IsArray() && t.NumElem() == 0 ->
  (ArrayMake0)

752
(Load <t> ptr mem) && t.IsArray() && t.NumElem() == 1 && fe.CanSSA(t) ->
753 754 755
  (ArrayMake1 (Load <t.ElemType()> ptr mem))

(Store _ (ArrayMake0) mem) -> mem
756
(Store dst (ArrayMake1 e) mem) -> (Store {e.Type} dst e mem)
757

758
// Putting [1]{*byte} and similar into direct interfaces.
759
(IMake typ (ArrayMake1 val)) -> (IMake typ val)
760
(ArraySelect [0] x:(IData _)) -> x
761

762
// string ops
763 764
// Decomposing StringMake and lowering of StringPtr and StringLen
// happens in a later pass, dec, so that these operations are available
765
// to other passes for optimizations.
766 767
(StringPtr (StringMake (Const64 <t> [c]) _)) -> (Const64 <t> [c])
(StringLen (StringMake _ (Const64 <t> [c]))) -> (Const64 <t> [c])
768
(ConstString {s}) && config.PtrSize == 4 && s.(string) == "" ->
769
  (StringMake (ConstNil) (Const32 <typ.Int> [0]))
770
(ConstString {s}) && config.PtrSize == 8 && s.(string) == "" ->
771
  (StringMake (ConstNil) (Const64 <typ.Int> [0]))
772
(ConstString {s}) && config.PtrSize == 4 && s.(string) != "" ->
773
  (StringMake
774
    (Addr <typ.BytePtr> {fe.StringData(s.(string))}
775
      (SB))
776
    (Const32 <typ.Int> [int64(len(s.(string)))]))
777
(ConstString {s}) && config.PtrSize == 8 && s.(string) != "" ->
778
  (StringMake
779
    (Addr <typ.BytePtr> {fe.StringData(s.(string))}
780
      (SB))
781
    (Const64 <typ.Int> [int64(len(s.(string)))]))
782 783

// slice ops
784 785
// Only a few slice rules are provided here.  See dec.rules for
// a more comprehensive set.
786 787
(SliceLen (SliceMake _ (Const64 <t> [c]) _)) -> (Const64 <t> [c])
(SliceCap (SliceMake _ _ (Const64 <t> [c]))) -> (Const64 <t> [c])
788 789
(SliceLen (SliceMake _ (Const32 <t> [c]) _)) -> (Const32 <t> [c])
(SliceCap (SliceMake _ _ (Const32 <t> [c]))) -> (Const32 <t> [c])
790 791 792
(SlicePtr (SliceMake (SlicePtr x) _ _)) -> (SlicePtr x)
(SliceLen (SliceMake _ (SliceLen x) _)) -> (SliceLen x)
(SliceCap (SliceMake _ _ (SliceCap x))) -> (SliceCap x)
793
(SliceCap (SliceMake _ _ (SliceLen x))) -> (SliceLen x)
794 795
(ConstSlice) && config.PtrSize == 4 ->
  (SliceMake
796
    (ConstNil <v.Type.ElemType().PtrTo()>)
797 798
    (Const32 <typ.Int> [0])
    (Const32 <typ.Int> [0]))
799
(ConstSlice) && config.PtrSize == 8 ->
800
  (SliceMake
801
    (ConstNil <v.Type.ElemType().PtrTo()>)
802 803
    (Const64 <typ.Int> [0])
    (Const64 <typ.Int> [0]))
804 805 806 807

// interface ops
(ConstInterface) ->
  (IMake
808 809
    (ConstNil <typ.BytePtr>)
    (ConstNil <typ.BytePtr>))
810

Keith Randall's avatar
Keith Randall committed
811
(NilCheck (GetG mem) mem) -> mem
812

813
(If (Not cond) yes no) -> (If cond no yes)
814 815
(If (ConstBool [c]) yes no) && c == 1 -> (First nil yes no)
(If (ConstBool [c]) yes no) && c == 0 -> (First nil no yes)
816 817

// Get rid of Convert ops for pointer arithmetic on unsafe.Pointer.
818 819
(Convert (Add64 (Convert ptr mem) off) mem) -> (Add64 ptr off)
(Convert (Convert ptr mem) mem) -> ptr
820 821 822 823

// Decompose compound argument values
(Arg {n} [off]) && v.Type.IsString() ->
  (StringMake
824 825
    (Arg <typ.BytePtr> {n} [off])
    (Arg <typ.Int> {n} [off+config.PtrSize]))
826 827 828

(Arg {n} [off]) && v.Type.IsSlice() ->
  (SliceMake
829
    (Arg <v.Type.ElemType().PtrTo()> {n} [off])
830 831
    (Arg <typ.Int> {n} [off+config.PtrSize])
    (Arg <typ.Int> {n} [off+2*config.PtrSize]))
832 833 834

(Arg {n} [off]) && v.Type.IsInterface() ->
  (IMake
835 836
    (Arg <typ.BytePtr> {n} [off])
    (Arg <typ.BytePtr> {n} [off+config.PtrSize]))
837

838
(Arg {n} [off]) && v.Type.IsComplex() && v.Type.Size() == 16 ->
839
  (ComplexMake
840 841
    (Arg <typ.Float64> {n} [off])
    (Arg <typ.Float64> {n} [off+8]))
842

843
(Arg {n} [off]) && v.Type.IsComplex() && v.Type.Size() == 8 ->
844
  (ComplexMake
845 846
    (Arg <typ.Float32> {n} [off])
    (Arg <typ.Float32> {n} [off+4]))
847

848
(Arg <t>) && t.IsStruct() && t.NumFields() == 0 && fe.CanSSA(t) ->
849
  (StructMake0)
850
(Arg <t> {n} [off]) && t.IsStruct() && t.NumFields() == 1 && fe.CanSSA(t) ->
851 852
  (StructMake1
    (Arg <t.FieldType(0)> {n} [off+t.FieldOff(0)]))
853
(Arg <t> {n} [off]) && t.IsStruct() && t.NumFields() == 2 && fe.CanSSA(t) ->
854 855 856
  (StructMake2
    (Arg <t.FieldType(0)> {n} [off+t.FieldOff(0)])
    (Arg <t.FieldType(1)> {n} [off+t.FieldOff(1)]))
857
(Arg <t> {n} [off]) && t.IsStruct() && t.NumFields() == 3 && fe.CanSSA(t) ->
858 859 860 861
  (StructMake3
    (Arg <t.FieldType(0)> {n} [off+t.FieldOff(0)])
    (Arg <t.FieldType(1)> {n} [off+t.FieldOff(1)])
    (Arg <t.FieldType(2)> {n} [off+t.FieldOff(2)]))
862
(Arg <t> {n} [off]) && t.IsStruct() && t.NumFields() == 4 && fe.CanSSA(t) ->
863 864 865 866 867
  (StructMake4
    (Arg <t.FieldType(0)> {n} [off+t.FieldOff(0)])
    (Arg <t.FieldType(1)> {n} [off+t.FieldOff(1)])
    (Arg <t.FieldType(2)> {n} [off+t.FieldOff(2)])
    (Arg <t.FieldType(3)> {n} [off+t.FieldOff(3)]))
868

869 870
(Arg <t>) && t.IsArray() && t.NumElem() == 0 ->
  (ArrayMake0)
871
(Arg <t> {n} [off]) && t.IsArray() && t.NumElem() == 1 && fe.CanSSA(t) ->
872 873
  (ArrayMake1 (Arg <t.ElemType()> {n} [off]))

874
// strength reduction of divide by a constant.
875
// See ../magic.go for a detailed description of these algorithms.
876

877
// Unsigned divide by power of 2.  Strength reduce to a shift.
878 879 880 881
(Div8u  n (Const8  [c])) && isPowerOfTwo(c&0xff)       -> (Rsh8Ux64 n  (Const64 <typ.UInt64> [log2(c&0xff)]))
(Div16u n (Const16 [c])) && isPowerOfTwo(c&0xffff)     -> (Rsh16Ux64 n (Const64 <typ.UInt64> [log2(c&0xffff)]))
(Div32u n (Const32 [c])) && isPowerOfTwo(c&0xffffffff) -> (Rsh32Ux64 n (Const64 <typ.UInt64> [log2(c&0xffffffff)]))
(Div64u n (Const64 [c])) && isPowerOfTwo(c)            -> (Rsh64Ux64 n (Const64 <typ.UInt64> [log2(c)]))
882
(Div64u n (Const64 [-1<<63]))                          -> (Rsh64Ux64 n (Const64 <typ.UInt64> [63]))
883

884 885 886 887 888 889 890
// Signed non-negative divide by power of 2.
(Div8  n (Const8  [c])) && isNonNegative(n) && isPowerOfTwo(c&0xff)       -> (Rsh8Ux64 n  (Const64 <typ.UInt64> [log2(c&0xff)]))
(Div16 n (Const16 [c])) && isNonNegative(n) && isPowerOfTwo(c&0xffff)     -> (Rsh16Ux64 n (Const64 <typ.UInt64> [log2(c&0xffff)]))
(Div32 n (Const32 [c])) && isNonNegative(n) && isPowerOfTwo(c&0xffffffff) -> (Rsh32Ux64 n (Const64 <typ.UInt64> [log2(c&0xffffffff)]))
(Div64 n (Const64 [c])) && isNonNegative(n) && isPowerOfTwo(c)            -> (Rsh64Ux64 n (Const64 <typ.UInt64> [log2(c)]))
(Div64 n (Const64 [-1<<63])) && isNonNegative(n)                          -> (Const64 [0])

891
// Unsigned divide, not a power of 2.  Strength reduce to a multiply.
892 893 894
// For 8-bit divides, we just do a direct 9-bit by 8-bit multiply.
(Div8u x (Const8 [c])) && umagicOK(8, c) ->
  (Trunc32to8
895 896 897
    (Rsh32Ux64 <typ.UInt32>
      (Mul32 <typ.UInt32>
        (Const32 <typ.UInt32> [int64(1<<8+umagic(8,c).m)])
898
        (ZeroExt8to32 x))
899
      (Const64 <typ.UInt64> [8+umagic(8,c).s])))
900 901 902 903

// For 16-bit divides on 64-bit machines, we do a direct 17-bit by 16-bit multiply.
(Div16u x (Const16 [c])) && umagicOK(16, c) && config.RegSize == 8 ->
  (Trunc64to16
904 905 906
    (Rsh64Ux64 <typ.UInt64>
      (Mul64 <typ.UInt64>
        (Const64 <typ.UInt64> [int64(1<<16+umagic(16,c).m)])
907
        (ZeroExt16to64 x))
908
      (Const64 <typ.UInt64> [16+umagic(16,c).s])))
909 910 911 912

// For 16-bit divides on 32-bit machines
(Div16u x (Const16 [c])) && umagicOK(16, c) && config.RegSize == 4 && umagic(16,c).m&1 == 0 ->
  (Trunc32to16
913 914 915
    (Rsh32Ux64 <typ.UInt32>
      (Mul32 <typ.UInt32>
        (Const32 <typ.UInt32> [int64(1<<15+umagic(16,c).m/2)])
916
        (ZeroExt16to32 x))
917
      (Const64 <typ.UInt64> [16+umagic(16,c).s-1])))
918 919
(Div16u x (Const16 [c])) && umagicOK(16, c) && config.RegSize == 4 && c&1 == 0 ->
  (Trunc32to16
920 921 922 923 924
    (Rsh32Ux64 <typ.UInt32>
      (Mul32 <typ.UInt32>
        (Const32 <typ.UInt32> [int64(1<<15+(umagic(16,c).m+1)/2)])
        (Rsh32Ux64 <typ.UInt32> (ZeroExt16to32 x) (Const64 <typ.UInt64> [1])))
      (Const64 <typ.UInt64> [16+umagic(16,c).s-2])))
925 926
(Div16u x (Const16 [c])) && umagicOK(16, c) && config.RegSize == 4 ->
  (Trunc32to16
927
    (Rsh32Ux64 <typ.UInt32>
928
      (Avg32u
929 930 931
        (Lsh32x64 <typ.UInt32> (ZeroExt16to32 x) (Const64 <typ.UInt64> [16]))
        (Mul32 <typ.UInt32>
          (Const32 <typ.UInt32> [int64(umagic(16,c).m)])
932
          (ZeroExt16to32 x)))
933
      (Const64 <typ.UInt64> [16+umagic(16,c).s-1])))
934 935 936

// For 32-bit divides on 32-bit machines
(Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 4 && umagic(32,c).m&1 == 0 ->
937 938 939
  (Rsh32Ux64 <typ.UInt32>
    (Hmul32u <typ.UInt32>
      (Const32 <typ.UInt32> [int64(int32(1<<31+umagic(32,c).m/2))])
940
      x)
941
    (Const64 <typ.UInt64> [umagic(32,c).s-1]))
942
(Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 4 && c&1 == 0 ->
943 944 945 946 947
  (Rsh32Ux64 <typ.UInt32>
    (Hmul32u <typ.UInt32>
      (Const32 <typ.UInt32> [int64(int32(1<<31+(umagic(32,c).m+1)/2))])
      (Rsh32Ux64 <typ.UInt32> x (Const64 <typ.UInt64> [1])))
    (Const64 <typ.UInt64> [umagic(32,c).s-2]))
948
(Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 4 ->
949
  (Rsh32Ux64 <typ.UInt32>
950 951
    (Avg32u
      x
952 953
      (Hmul32u <typ.UInt32>
        (Const32 <typ.UInt32> [int64(int32(umagic(32,c).m))])
954
        x))
955
    (Const64 <typ.UInt64> [umagic(32,c).s-1]))
956 957 958 959 960

// For 32-bit divides on 64-bit machines
// We'll use a regular (non-hi) multiply for this case.
(Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 8 && umagic(32,c).m&1 == 0 ->
  (Trunc64to32
961 962 963
    (Rsh64Ux64 <typ.UInt64>
      (Mul64 <typ.UInt64>
        (Const64 <typ.UInt64> [int64(1<<31+umagic(32,c).m/2)])
964
        (ZeroExt32to64 x))
965
      (Const64 <typ.UInt64> [32+umagic(32,c).s-1])))
966 967
(Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 8 && c&1 == 0 ->
  (Trunc64to32
968 969 970 971 972
    (Rsh64Ux64 <typ.UInt64>
      (Mul64 <typ.UInt64>
        (Const64 <typ.UInt64> [int64(1<<31+(umagic(32,c).m+1)/2)])
        (Rsh64Ux64 <typ.UInt64> (ZeroExt32to64 x) (Const64 <typ.UInt64> [1])))
      (Const64 <typ.UInt64> [32+umagic(32,c).s-2])))
973 974
(Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 8 ->
  (Trunc64to32
975
    (Rsh64Ux64 <typ.UInt64>
976
      (Avg64u
977 978 979
        (Lsh64x64 <typ.UInt64> (ZeroExt32to64 x) (Const64 <typ.UInt64> [32]))
        (Mul64 <typ.UInt64>
          (Const64 <typ.UInt32> [int64(umagic(32,c).m)])
980
          (ZeroExt32to64 x)))
981
      (Const64 <typ.UInt64> [32+umagic(32,c).s-1])))
982 983 984 985

// For 64-bit divides on 64-bit machines
// (64-bit divides on 32-bit machines are lowered to a runtime call by the walk pass.)
(Div64u x (Const64 [c])) && umagicOK(64, c) && config.RegSize == 8 && umagic(64,c).m&1 == 0 ->
986 987 988
  (Rsh64Ux64 <typ.UInt64>
    (Hmul64u <typ.UInt64>
      (Const64 <typ.UInt64> [int64(1<<63+umagic(64,c).m/2)])
989
      x)
990
    (Const64 <typ.UInt64> [umagic(64,c).s-1]))
991
(Div64u x (Const64 [c])) && umagicOK(64, c) && config.RegSize == 8 && c&1 == 0 ->
992 993 994 995 996
  (Rsh64Ux64 <typ.UInt64>
    (Hmul64u <typ.UInt64>
      (Const64 <typ.UInt64> [int64(1<<63+(umagic(64,c).m+1)/2)])
      (Rsh64Ux64 <typ.UInt64> x (Const64 <typ.UInt64> [1])))
    (Const64 <typ.UInt64> [umagic(64,c).s-2]))
997
(Div64u x (Const64 [c])) && umagicOK(64, c) && config.RegSize == 8 ->
998
  (Rsh64Ux64 <typ.UInt64>
999 1000
    (Avg64u
      x
1001 1002
      (Hmul64u <typ.UInt64>
        (Const64 <typ.UInt64> [int64(umagic(64,c).m)])
1003
        x))
1004
    (Const64 <typ.UInt64> [umagic(64,c).s-1]))
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014

// Signed divide by a negative constant.  Rewrite to divide by a positive constant.
(Div8  <t> n (Const8  [c])) && c < 0 && c != -1<<7  -> (Neg8  (Div8  <t> n (Const8  <t> [-c])))
(Div16 <t> n (Const16 [c])) && c < 0 && c != -1<<15 -> (Neg16 (Div16 <t> n (Const16 <t> [-c])))
(Div32 <t> n (Const32 [c])) && c < 0 && c != -1<<31 -> (Neg32 (Div32 <t> n (Const32 <t> [-c])))
(Div64 <t> n (Const64 [c])) && c < 0 && c != -1<<63 -> (Neg64 (Div64 <t> n (Const64 <t> [-c])))

// Dividing by the most-negative number.  Result is always 0 except
// if the input is also the most-negative number.
// We can detect that using the sign bit of x & -x.
1015 1016 1017 1018
(Div8  <t> x (Const8  [-1<<7 ])) -> (Rsh8Ux64  (And8  <t> x (Neg8  <t> x)) (Const64 <typ.UInt64> [7 ]))
(Div16 <t> x (Const16 [-1<<15])) -> (Rsh16Ux64 (And16 <t> x (Neg16 <t> x)) (Const64 <typ.UInt64> [15]))
(Div32 <t> x (Const32 [-1<<31])) -> (Rsh32Ux64 (And32 <t> x (Neg32 <t> x)) (Const64 <typ.UInt64> [31]))
(Div64 <t> x (Const64 [-1<<63])) -> (Rsh64Ux64 (And64 <t> x (Neg64 <t> x)) (Const64 <typ.UInt64> [63]))
1019 1020 1021 1022 1023 1024 1025

// Signed divide by power of 2.
// n / c =       n >> log(c) if n >= 0
//       = (n+c-1) >> log(c) if n < 0
// We conditionally add c-1 by adding n>>63>>(64-log(c)) (first shift signed, second shift unsigned).
(Div8  <t> n (Const8  [c])) && isPowerOfTwo(c) ->
  (Rsh8x64
1026 1027
    (Add8  <t> n (Rsh8Ux64  <t> (Rsh8x64  <t> n (Const64 <typ.UInt64> [ 7])) (Const64 <typ.UInt64> [ 8-log2(c)])))
    (Const64 <typ.UInt64> [log2(c)]))
1028 1029
(Div16 <t> n (Const16 [c])) && isPowerOfTwo(c) ->
  (Rsh16x64
1030 1031
    (Add16 <t> n (Rsh16Ux64 <t> (Rsh16x64 <t> n (Const64 <typ.UInt64> [15])) (Const64 <typ.UInt64> [16-log2(c)])))
    (Const64 <typ.UInt64> [log2(c)]))
1032 1033
(Div32 <t> n (Const32 [c])) && isPowerOfTwo(c) ->
  (Rsh32x64
1034 1035
    (Add32 <t> n (Rsh32Ux64 <t> (Rsh32x64 <t> n (Const64 <typ.UInt64> [31])) (Const64 <typ.UInt64> [32-log2(c)])))
    (Const64 <typ.UInt64> [log2(c)]))
1036 1037
(Div64 <t> n (Const64 [c])) && isPowerOfTwo(c) ->
  (Rsh64x64
1038 1039
    (Add64 <t> n (Rsh64Ux64 <t> (Rsh64x64 <t> n (Const64 <typ.UInt64> [63])) (Const64 <typ.UInt64> [64-log2(c)])))
    (Const64 <typ.UInt64> [log2(c)]))
1040 1041

// Signed divide, not a power of 2.  Strength reduce to a multiply.
1042 1043 1044
(Div8 <t> x (Const8 [c])) && smagicOK(8,c) ->
  (Sub8 <t>
    (Rsh32x64 <t>
1045 1046
      (Mul32 <typ.UInt32>
        (Const32 <typ.UInt32> [int64(smagic(8,c).m)])
1047
        (SignExt8to32 x))
1048
      (Const64 <typ.UInt64> [8+smagic(8,c).s]))
1049 1050
    (Rsh32x64 <t>
      (SignExt8to32 x)
1051
      (Const64 <typ.UInt64> [31])))
1052 1053 1054
(Div16 <t> x (Const16 [c])) && smagicOK(16,c) ->
  (Sub16 <t>
    (Rsh32x64 <t>
1055 1056
      (Mul32 <typ.UInt32>
        (Const32 <typ.UInt32> [int64(smagic(16,c).m)])
1057
        (SignExt16to32 x))
1058
      (Const64 <typ.UInt64> [16+smagic(16,c).s]))
1059 1060
    (Rsh32x64 <t>
      (SignExt16to32 x)
1061
      (Const64 <typ.UInt64> [31])))
1062 1063 1064
(Div32 <t> x (Const32 [c])) && smagicOK(32,c) && config.RegSize == 8 ->
  (Sub32 <t>
    (Rsh64x64 <t>
1065 1066
      (Mul64 <typ.UInt64>
        (Const64 <typ.UInt64> [int64(smagic(32,c).m)])
1067
        (SignExt32to64 x))
1068
      (Const64 <typ.UInt64> [32+smagic(32,c).s]))
1069 1070
    (Rsh64x64 <t>
      (SignExt32to64 x)
1071
      (Const64 <typ.UInt64> [63])))
1072 1073 1074 1075
(Div32 <t> x (Const32 [c])) && smagicOK(32,c) && config.RegSize == 4 && smagic(32,c).m&1 == 0 ->
  (Sub32 <t>
    (Rsh32x64 <t>
      (Hmul32 <t>
1076
        (Const32 <typ.UInt32> [int64(int32(smagic(32,c).m/2))])
1077
        x)
1078
      (Const64 <typ.UInt64> [smagic(32,c).s-1]))
1079 1080
    (Rsh32x64 <t>
      x
1081
      (Const64 <typ.UInt64> [31])))
1082 1083 1084 1085 1086
(Div32 <t> x (Const32 [c])) && smagicOK(32,c) && config.RegSize == 4 && smagic(32,c).m&1 != 0 ->
  (Sub32 <t>
    (Rsh32x64 <t>
      (Add32 <t>
        (Hmul32 <t>
1087
          (Const32 <typ.UInt32> [int64(int32(smagic(32,c).m))])
1088 1089
          x)
        x)
1090
      (Const64 <typ.UInt64> [smagic(32,c).s]))
1091 1092
    (Rsh32x64 <t>
      x
1093
      (Const64 <typ.UInt64> [31])))
1094
(Div64 <t> x (Const64 [c])) && smagicOK(64,c) && smagic(64,c).m&1 == 0 ->
1095 1096 1097
  (Sub64 <t>
    (Rsh64x64 <t>
      (Hmul64 <t>
1098
        (Const64 <typ.UInt64> [int64(smagic(64,c).m/2)])
1099
        x)
1100
      (Const64 <typ.UInt64> [smagic(64,c).s-1]))
1101 1102
    (Rsh64x64 <t>
      x
1103
      (Const64 <typ.UInt64> [63])))
1104
(Div64 <t> x (Const64 [c])) && smagicOK(64,c) && smagic(64,c).m&1 != 0 ->
1105 1106 1107 1108
  (Sub64 <t>
    (Rsh64x64 <t>
      (Add64 <t>
        (Hmul64 <t>
1109
          (Const64 <typ.UInt64> [int64(smagic(64,c).m)])
1110 1111
          x)
        x)
1112
      (Const64 <typ.UInt64> [smagic(64,c).s]))
1113 1114
    (Rsh64x64 <t>
      x
1115
      (Const64 <typ.UInt64> [63])))
1116 1117 1118 1119 1120 1121

// Unsigned mod by power of 2 constant.
(Mod8u  <t> n (Const8  [c])) && isPowerOfTwo(c&0xff)       -> (And8 n (Const8 <t> [(c&0xff)-1]))
(Mod16u <t> n (Const16 [c])) && isPowerOfTwo(c&0xffff)     -> (And16 n (Const16 <t> [(c&0xffff)-1]))
(Mod32u <t> n (Const32 [c])) && isPowerOfTwo(c&0xffffffff) -> (And32 n (Const32 <t> [(c&0xffffffff)-1]))
(Mod64u <t> n (Const64 [c])) && isPowerOfTwo(c)            -> (And64 n (Const64 <t> [c-1]))
1122
(Mod64u <t> n (Const64 [-1<<63]))                          -> (And64 n (Const64 <t> [1<<63-1]))
1123

1124 1125 1126 1127 1128 1129 1130
// Signed non-negative mod by power of 2 constant.
(Mod8  <t> n (Const8  [c])) && isNonNegative(n) && isPowerOfTwo(c&0xff)       -> (And8 n (Const8 <t> [(c&0xff)-1]))
(Mod16 <t> n (Const16 [c])) && isNonNegative(n) && isPowerOfTwo(c&0xffff)     -> (And16 n (Const16 <t> [(c&0xffff)-1]))
(Mod32 <t> n (Const32 [c])) && isNonNegative(n) && isPowerOfTwo(c&0xffffffff) -> (And32 n (Const32 <t> [(c&0xffffffff)-1]))
(Mod64 <t> n (Const64 [c])) && isNonNegative(n) && isPowerOfTwo(c)            -> (And64 n (Const64 <t> [c-1]))
(Mod64 n (Const64 [-1<<63])) && isNonNegative(n)                              -> n

1131 1132 1133 1134 1135
// Signed mod by negative constant.
(Mod8  <t> n (Const8  [c])) && c < 0 && c != -1<<7  -> (Mod8  <t> n (Const8  <t> [-c]))
(Mod16 <t> n (Const16 [c])) && c < 0 && c != -1<<15 -> (Mod16 <t> n (Const16 <t> [-c]))
(Mod32 <t> n (Const32 [c])) && c < 0 && c != -1<<31 -> (Mod32 <t> n (Const32 <t> [-c]))
(Mod64 <t> n (Const64 [c])) && c < 0 && c != -1<<63 -> (Mod64 <t> n (Const64 <t> [-c]))
1136

1137
// All other mods by constants, do A%B = A-(A/B*B).
1138 1139
// This implements % with two * and a bunch of ancillary ops.
// One of the * is free if the user's code also computes A/B.
1140 1141 1142 1143 1144 1145 1146
(Mod8   <t> x (Const8  [c])) && x.Op != OpConst8  && (c > 0 || c == -1<<7)
  -> (Sub8  x (Mul8  <t> (Div8   <t> x (Const8  <t> [c])) (Const8  <t> [c])))
(Mod16  <t> x (Const16 [c])) && x.Op != OpConst16 && (c > 0 || c == -1<<15)
  -> (Sub16 x (Mul16 <t> (Div16  <t> x (Const16 <t> [c])) (Const16 <t> [c])))
(Mod32  <t> x (Const32 [c])) && x.Op != OpConst32 && (c > 0 || c == -1<<31)
  -> (Sub32 x (Mul32 <t> (Div32  <t> x (Const32 <t> [c])) (Const32 <t> [c])))
(Mod64  <t> x (Const64 [c])) && x.Op != OpConst64 && (c > 0 || c == -1<<63)
1147
  -> (Sub64 x (Mul64 <t> (Div64  <t> x (Const64 <t> [c])) (Const64 <t> [c])))
1148 1149 1150 1151 1152 1153 1154
(Mod8u  <t> x (Const8  [c])) && x.Op != OpConst8  && c > 0 && umagicOK(8 ,c)
  -> (Sub8  x (Mul8  <t> (Div8u  <t> x (Const8  <t> [c])) (Const8  <t> [c])))
(Mod16u <t> x (Const16 [c])) && x.Op != OpConst16 && c > 0 && umagicOK(16,c)
  -> (Sub16 x (Mul16 <t> (Div16u <t> x (Const16 <t> [c])) (Const16 <t> [c])))
(Mod32u <t> x (Const32 [c])) && x.Op != OpConst32 && c > 0 && umagicOK(32,c)
  -> (Sub32 x (Mul32 <t> (Div32u <t> x (Const32 <t> [c])) (Const32 <t> [c])))
(Mod64u <t> x (Const64 [c])) && x.Op != OpConst64 && c > 0 && umagicOK(64,c)
1155
  -> (Sub64 x (Mul64 <t> (Div64u <t> x (Const64 <t> [c])) (Const64 <t> [c])))
1156

1157 1158 1159
// Reassociate expressions involving
// constants such that constants come first,
// exposing obvious constant-folding opportunities.
1160
// Reassociate (op (op y C) x) to (op C (op x y)) or similar, where C
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
// is constant, which pushes constants to the outside
// of the expression. At that point, any constant-folding
// opportunities should be obvious.

// x + (C + z) -> C + (x + z)
(Add64 (Add64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Add64 i (Add64 <t> z x))
(Add32 (Add32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Add32 i (Add32 <t> z x))
(Add16 (Add16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Add16 i (Add16 <t> z x))
(Add8  (Add8  i:(Const8  <t>) z) x) && (z.Op != OpConst8  && x.Op != OpConst8)  -> (Add8  i (Add8  <t> z x))

// x + (C - z) -> C + (x - z)
(Add64 (Sub64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Add64 i (Sub64 <t> x z))
(Add32 (Sub32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Add32 i (Sub32 <t> x z))
(Add16 (Sub16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Add16 i (Sub16 <t> x z))
(Add8  (Sub8  i:(Const8  <t>) z) x) && (z.Op != OpConst8  && x.Op != OpConst8)  -> (Add8  i (Sub8  <t> x z))
(Add64 x (Sub64 i:(Const64 <t>) z)) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Add64 i (Sub64 <t> x z))
(Add32 x (Sub32 i:(Const32 <t>) z)) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Add32 i (Sub32 <t> x z))
(Add16 x (Sub16 i:(Const16 <t>) z)) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Add16 i (Sub16 <t> x z))
(Add8  x (Sub8  i:(Const8  <t>) z)) && (z.Op != OpConst8  && x.Op != OpConst8)  -> (Add8  i (Sub8  <t> x z))

// x + (z - C) -> (x + z) - C
(Add64 (Sub64 z i:(Const64 <t>)) x) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Sub64 (Add64 <t> x z) i)
(Add32 (Sub32 z i:(Const32 <t>)) x) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Sub32 (Add32 <t> x z) i)
(Add16 (Sub16 z i:(Const16 <t>)) x) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Sub16 (Add16 <t> x z) i)
(Add8  (Sub8  z i:(Const8  <t>)) x) && (z.Op != OpConst8  && x.Op != OpConst8)  -> (Sub8  (Add8  <t> x z) i)
(Add64 x (Sub64 z i:(Const64 <t>))) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Sub64 (Add64 <t> x z) i)
(Add32 x (Sub32 z i:(Const32 <t>))) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Sub32 (Add32 <t> x z) i)
(Add16 x (Sub16 z i:(Const16 <t>))) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Sub16 (Add16 <t> x z) i)
(Add8  x (Sub8  z i:(Const8  <t>))) && (z.Op != OpConst8  && x.Op != OpConst8)  -> (Sub8  (Add8  <t> x z) i)

// x - (C - z) -> x + (z - C) -> (x + z) - C
(Sub64 x (Sub64 i:(Const64 <t>) z)) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Sub64 (Add64 <t> x z) i)
(Sub32 x (Sub32 i:(Const32 <t>) z)) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Sub32 (Add32 <t> x z) i)
(Sub16 x (Sub16 i:(Const16 <t>) z)) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Sub16 (Add16 <t> x z) i)
(Sub8  x (Sub8  i:(Const8  <t>) z)) && (z.Op != OpConst8  && x.Op != OpConst8)  -> (Sub8  (Add8  <t> x z) i)

// x - (z - C) -> x + (C - z) -> (x - z) + C
(Sub64 x (Sub64 z i:(Const64 <t>))) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Add64 i (Sub64 <t> x z))
(Sub32 x (Sub32 z i:(Const32 <t>))) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Add32 i (Sub32 <t> x z))
(Sub16 x (Sub16 z i:(Const16 <t>))) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Add16 i (Sub16 <t> x z))
(Sub8  x (Sub8  z i:(Const8  <t>))) && (z.Op != OpConst8  && x.Op != OpConst8)  -> (Add8  i (Sub8  <t> x z))

// x & (C & z) -> C & (x & z)
(And64 (And64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) -> (And64 i (And64 <t> z x))
(And32 (And32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) -> (And32 i (And32 <t> z x))
(And16 (And16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) -> (And16 i (And16 <t> z x))
(And8  (And8  i:(Const8  <t>) z) x) && (z.Op != OpConst8  && x.Op != OpConst8)  -> (And8  i (And8  <t> z x))

// x | (C | z) -> C | (x | z)
(Or64 (Or64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Or64 i (Or64 <t> z x))
(Or32 (Or32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Or32 i (Or32 <t> z x))
(Or16 (Or16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Or16 i (Or16 <t> z x))
(Or8  (Or8  i:(Const8  <t>) z) x) && (z.Op != OpConst8  && x.Op != OpConst8)  -> (Or8  i (Or8  <t> z x))

// x ^ (C ^ z) -> C ^ (x ^ z)
(Xor64 (Xor64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Xor64 i (Xor64 <t> z x))
(Xor32 (Xor32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Xor32 i (Xor32 <t> z x))
(Xor16 (Xor16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Xor16 i (Xor16 <t> z x))
(Xor8  (Xor8  i:(Const8  <t>) z) x) && (z.Op != OpConst8  && x.Op != OpConst8)  -> (Xor8  i (Xor8  <t> z x))

// C + (D + x) -> (C + D) + x
(Add64 (Const64 <t> [c]) (Add64 (Const64 <t> [d]) x)) -> (Add64 (Const64 <t> [c+d]) x)
(Add32 (Const32 <t> [c]) (Add32 (Const32 <t> [d]) x)) -> (Add32 (Const32 <t> [int64(int32(c+d))]) x)
(Add16 (Const16 <t> [c]) (Add16 (Const16 <t> [d]) x)) -> (Add16 (Const16 <t> [int64(int16(c+d))]) x)
(Add8  (Const8  <t> [c]) (Add8  (Const8  <t> [d]) x)) -> (Add8  (Const8  <t> [int64(int8(c+d))]) x)

// C + (D - x) -> (C + D) - x
(Add64 (Const64 <t> [c]) (Sub64 (Const64 <t> [d]) x)) -> (Sub64 (Const64 <t> [c+d]) x)
(Add32 (Const32 <t> [c]) (Sub32 (Const32 <t> [d]) x)) -> (Sub32 (Const32 <t> [int64(int32(c+d))]) x)
(Add16 (Const16 <t> [c]) (Sub16 (Const16 <t> [d]) x)) -> (Sub16 (Const16 <t> [int64(int16(c+d))]) x)
(Add8  (Const8  <t> [c]) (Sub8  (Const8  <t> [d]) x)) -> (Sub8  (Const8  <t> [int64(int8(c+d))]) x)

// C + (x - D) -> (C - D) + x
(Add64 (Const64 <t> [c]) (Sub64 x (Const64 <t> [d]))) -> (Add64 (Const64 <t> [c-d]) x)
(Add32 (Const32 <t> [c]) (Sub32 x (Const32 <t> [d]))) -> (Add32 (Const32 <t> [int64(int32(c-d))]) x)
(Add16 (Const16 <t> [c]) (Sub16 x (Const16 <t> [d]))) -> (Add16 (Const16 <t> [int64(int16(c-d))]) x)
(Add8  (Const8  <t> [c]) (Sub8  x (Const8  <t> [d]))) -> (Add8  (Const8  <t> [int64(int8(c-d))]) x)

// C - (x - D) -> (C + D) - x
(Sub64 (Const64 <t> [c]) (Sub64 x (Const64 <t> [d]))) -> (Sub64 (Const64 <t> [c+d]) x)
(Sub32 (Const32 <t> [c]) (Sub32 x (Const32 <t> [d]))) -> (Sub32 (Const32 <t> [int64(int32(c+d))]) x)
(Sub16 (Const16 <t> [c]) (Sub16 x (Const16 <t> [d]))) -> (Sub16 (Const16 <t> [int64(int16(c+d))]) x)
(Sub8  (Const8  <t> [c]) (Sub8  x (Const8  <t> [d]))) -> (Sub8  (Const8  <t> [int64(int8(c+d))]) x)

// C - (D - x) -> (C - D) + x
(Sub64 (Const64 <t> [c]) (Sub64 (Const64 <t> [d]) x)) -> (Add64 (Const64 <t> [c-d]) x)
(Sub32 (Const32 <t> [c]) (Sub32 (Const32 <t> [d]) x)) -> (Add32 (Const32 <t> [int64(int32(c-d))]) x)
(Sub16 (Const16 <t> [c]) (Sub16 (Const16 <t> [d]) x)) -> (Add16 (Const16 <t> [int64(int16(c-d))]) x)
(Sub8  (Const8  <t> [c]) (Sub8  (Const8  <t> [d]) x)) -> (Add8  (Const8  <t> [int64(int8(c-d))]) x)

// C & (D & x) -> (C & D) & x
(And64 (Const64 <t> [c]) (And64 (Const64 <t> [d]) x)) -> (And64 (Const64 <t> [c&d]) x)
(And32 (Const32 <t> [c]) (And32 (Const32 <t> [d]) x)) -> (And32 (Const32 <t> [int64(int32(c&d))]) x)
(And16 (Const16 <t> [c]) (And16 (Const16 <t> [d]) x)) -> (And16 (Const16 <t> [int64(int16(c&d))]) x)
(And8  (Const8  <t> [c]) (And8  (Const8  <t> [d]) x)) -> (And8  (Const8  <t> [int64(int8(c&d))]) x)

// C | (D | x) -> (C | D) | x
(Or64 (Const64 <t> [c]) (Or64 (Const64 <t> [d]) x)) -> (Or64 (Const64 <t> [c|d]) x)
(Or32 (Const32 <t> [c]) (Or32 (Const32 <t> [d]) x)) -> (Or32 (Const32 <t> [int64(int32(c|d))]) x)
(Or16 (Const16 <t> [c]) (Or16 (Const16 <t> [d]) x)) -> (Or16 (Const16 <t> [int64(int16(c|d))]) x)
(Or8  (Const8  <t> [c]) (Or8  (Const8  <t> [d]) x)) -> (Or8  (Const8  <t> [int64(int8(c|d))]) x)

// C ^ (D ^ x) -> (C ^ D) ^ x
(Xor64 (Const64 <t> [c]) (Xor64 (Const64 <t> [d]) x)) -> (Xor64 (Const64 <t> [c^d]) x)
(Xor32 (Const32 <t> [c]) (Xor32 (Const32 <t> [d]) x)) -> (Xor32 (Const32 <t> [int64(int32(c^d))]) x)
(Xor16 (Const16 <t> [c]) (Xor16 (Const16 <t> [d]) x)) -> (Xor16 (Const16 <t> [int64(int16(c^d))]) x)
(Xor8  (Const8  <t> [c]) (Xor8  (Const8  <t> [d]) x)) -> (Xor8  (Const8  <t> [int64(int8(c^d))]) x)

// C * (D * x) = (C * D) * x
(Mul64 (Const64 <t> [c]) (Mul64 (Const64 <t> [d]) x)) -> (Mul64 (Const64 <t> [c*d]) x)
(Mul32 (Const32 <t> [c]) (Mul32 (Const32 <t> [d]) x)) -> (Mul32 (Const32 <t> [int64(int32(c*d))]) x)
(Mul16 (Const16 <t> [c]) (Mul16 (Const16 <t> [d]) x)) -> (Mul16 (Const16 <t> [int64(int16(c*d))]) x)
(Mul8  (Const8  <t> [c]) (Mul8  (Const8  <t> [d]) x)) -> (Mul8  (Const8  <t> [int64(int8(c*d))]) x)

1275 1276 1277 1278 1279 1280 1281 1282 1283
// floating point optimizations
(Add32F x (Const32F [0])) -> x
(Add64F x (Const64F [0])) -> x
(Sub32F x (Const32F [0])) -> x
(Sub64F x (Const64F [0])) -> x
(Mul32F x (Const32F [f2i(1)])) -> x
(Mul64F x (Const64F [f2i(1)])) -> x
(Mul32F x (Const32F [f2i(-1)])) -> (Neg32F x)
(Mul64F x (Const64F [f2i(-1)])) -> (Neg64F x)
1284 1285 1286 1287
(Mul32F x (Const32F [f2i(2)])) -> (Add32F x x)
(Mul64F x (Const64F [f2i(2)])) -> (Add64F x x)
(Div32F x (Const32F <t> [c])) && reciprocalExact32(float32(i2f(c))) -> (Mul32F x (Const32F <t> [f2i(1/i2f(c))]))
(Div64F x (Const64F <t> [c])) && reciprocalExact64(i2f(c))          -> (Mul64F x (Const64F <t> [f2i(1/i2f(c))]))
1288 1289

(Sqrt (Const64F [c])) -> (Const64F [f2i(math.Sqrt(i2f(c)))])
1290 1291

// recognize runtime.newobject and don't Zero/Nilcheck it
Keith Randall's avatar
Keith Randall committed
1292
(Zero (Load (OffPtr [c] (SP)) mem) mem)
1293
	&& mem.Op == OpStaticCall
Keith Randall's avatar
Keith Randall committed
1294
	&& isSameSym(mem.Aux, "runtime.newobject")
1295
	&& c == config.ctxt.FixedFrameSize() + config.RegSize // offset of return value
1296
	-> mem
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
(Store (Load (OffPtr [c] (SP)) mem) x mem)
	&& isConstZero(x)
	&& mem.Op == OpStaticCall
	&& isSameSym(mem.Aux, "runtime.newobject")
	&& c == config.ctxt.FixedFrameSize() + config.RegSize // offset of return value
	-> mem
(Store (OffPtr (Load (OffPtr [c] (SP)) mem)) x mem)
	&& isConstZero(x)
	&& mem.Op == OpStaticCall
	&& isSameSym(mem.Aux, "runtime.newobject")
	&& c == config.ctxt.FixedFrameSize() + config.RegSize // offset of return value
	-> mem
Keith Randall's avatar
Keith Randall committed
1309 1310
// nil checks just need to rewrite to something useless.
// they will be deadcode eliminated soon afterwards.
1311 1312
(NilCheck (Load (OffPtr [c] (SP)) (StaticCall {sym} _)) _)
	&& isSameSym(sym, "runtime.newobject")
1313
	&& c == config.ctxt.FixedFrameSize() + config.RegSize // offset of return value
1314
	&& warnRule(fe.Debug_checknil() && v.Pos.Line() > 1, v, "removed nil check")
1315
	-> (Invalid)
1316 1317
(NilCheck (OffPtr (Load (OffPtr [c] (SP)) (StaticCall {sym} _))) _)
	&& isSameSym(sym, "runtime.newobject")
1318
	&& c == config.ctxt.FixedFrameSize() + config.RegSize // offset of return value
1319
	&& warnRule(fe.Debug_checknil() && v.Pos.Line() > 1, v, "removed nil check")
1320
	-> (Invalid)
1321

1322 1323 1324 1325
// Address comparison shows up in type assertions.
(EqPtr x x) -> (ConstBool [1])
(EqPtr (Addr {a} x) (Addr {b} x)) -> (ConstBool [b2i(a == b)])

1326 1327 1328 1329 1330 1331 1332 1333
// Inline small runtime.memmove calls with constant length.
(StaticCall {sym} s1:(Store _ (Const64 [sz]) s2:(Store  _ src s3:(Store {t} _ dst mem))))
     && isSameSym(sym,"runtime.memmove") && s1.Uses == 1 && s2.Uses == 1 && s3.Uses == 1 && isInlinableMemmoveSize(sz,config)
     -> (Move {t.(*types.Type).Elem()} [sz] dst src mem)
(StaticCall {sym} s1:(Store _ (Const32 [sz]) s2:(Store  _ src s3:(Store {t} _ dst mem))))
     && isSameSym(sym,"runtime.memmove") && s1.Uses == 1 && s2.Uses == 1 && s3.Uses == 1 && isInlinableMemmoveSize(sz,config)
     -> (Move {t.(*types.Type).Elem()} [sz] dst src mem)

1334 1335 1336 1337 1338 1339
// De-virtualize interface calls into static calls.
// Note that (ITab (IMake)) doesn't get
// rewritten until after the first opt pass,
// so this rule should trigger reliably.
(InterCall [argsize] (Load (OffPtr [off] (ITab (IMake (Addr {itab} (SB)) _))) _) mem) && devirt(v, itab, off) != nil ->
	(StaticCall [argsize] {devirt(v, itab, off)} mem)