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   [int8(c)])
42(Trunc32to8  (Const32  [c])) => (Const8   [int8(c)])
43(Trunc32to16 (Const32  [c])) => (Const16  [int16(c)])
44(Trunc64to8  (Const64  [c])) => (Const8   [int8(c)])
45(Trunc64to16 (Const64  [c])) => (Const16  [int16(c)])
46(Trunc64to32 (Const64  [c])) => (Const32  [int32(c)])
47(Cvt64Fto32F (Const64F [c])) => (Const32F [float32(c)])
48(Cvt32Fto64F (Const32F [c])) => (Const64F [float64(c)])
49(Cvt32to32F  (Const32  [c])) => (Const32F [float32(c)])
50(Cvt32to64F  (Const32  [c])) => (Const64F [float64(c)])
51(Cvt64to32F  (Const64  [c])) => (Const32F [float32(c)])
52(Cvt64to64F  (Const64  [c])) => (Const64F [float64(c)])
53(Cvt32Fto32  (Const32F [c])) => (Const32  [int32(c)])
54(Cvt32Fto64  (Const32F [c])) => (Const64  [int64(c)])
55(Cvt64Fto32  (Const64F [c])) => (Const32  [int32(c)])
56(Cvt64Fto64  (Const64F [c])) => (Const64  [int64(c)])
57(Round32F x:(Const32F)) => x
58(Round64F x:(Const64F)) => x
59(CvtBoolToUint8 (ConstBool [false])) => (Const8 [0])
60(CvtBoolToUint8 (ConstBool [true])) => (Const8 [1])
61
62(Trunc16to8  (ZeroExt8to16  x)) => x
63(Trunc32to8  (ZeroExt8to32  x)) => x
64(Trunc32to16 (ZeroExt8to32  x)) => (ZeroExt8to16  x)
65(Trunc32to16 (ZeroExt16to32 x)) => x
66(Trunc64to8  (ZeroExt8to64  x)) => x
67(Trunc64to16 (ZeroExt8to64  x)) => (ZeroExt8to16  x)
68(Trunc64to16 (ZeroExt16to64 x)) => x
69(Trunc64to32 (ZeroExt8to64  x)) => (ZeroExt8to32  x)
70(Trunc64to32 (ZeroExt16to64 x)) => (ZeroExt16to32 x)
71(Trunc64to32 (ZeroExt32to64 x)) => x
72(Trunc16to8  (SignExt8to16  x)) => x
73(Trunc32to8  (SignExt8to32  x)) => x
74(Trunc32to16 (SignExt8to32  x)) => (SignExt8to16  x)
75(Trunc32to16 (SignExt16to32 x)) => x
76(Trunc64to8  (SignExt8to64  x)) => x
77(Trunc64to16 (SignExt8to64  x)) => (SignExt8to16  x)
78(Trunc64to16 (SignExt16to64 x)) => x
79(Trunc64to32 (SignExt8to64  x)) => (SignExt8to32  x)
80(Trunc64to32 (SignExt16to64 x)) => (SignExt16to32 x)
81(Trunc64to32 (SignExt32to64 x)) => x
82
83(ZeroExt8to16  (Const8  [c])) => (Const16 [int16( uint8(c))])
84(ZeroExt8to32  (Const8  [c])) => (Const32 [int32( uint8(c))])
85(ZeroExt8to64  (Const8  [c])) => (Const64 [int64( uint8(c))])
86(ZeroExt16to32 (Const16 [c])) => (Const32 [int32(uint16(c))])
87(ZeroExt16to64 (Const16 [c])) => (Const64 [int64(uint16(c))])
88(ZeroExt32to64 (Const32 [c])) => (Const64 [int64(uint32(c))])
89(SignExt8to16  (Const8  [c])) => (Const16 [int16(c)])
90(SignExt8to32  (Const8  [c])) => (Const32 [int32(c)])
91(SignExt8to64  (Const8  [c])) => (Const64 [int64(c)])
92(SignExt16to32 (Const16 [c])) => (Const32 [int32(c)])
93(SignExt16to64 (Const16 [c])) => (Const64 [int64(c)])
94(SignExt32to64 (Const32 [c])) => (Const64 [int64(c)])
95
96(Neg8   (Const8   [c])) => (Const8   [-c])
97(Neg16  (Const16  [c])) => (Const16  [-c])
98(Neg32  (Const32  [c])) => (Const32  [-c])
99(Neg64  (Const64  [c])) => (Const64  [-c])
100(Neg32F (Const32F [c])) && c != 0 => (Const32F [-c])
101(Neg64F (Const64F [c])) && c != 0 => (Const64F [-c])
102
103(Add8   (Const8 [c])   (Const8 [d]))   => (Const8  [c+d])
104(Add16  (Const16 [c])  (Const16 [d]))  => (Const16 [c+d])
105(Add32  (Const32 [c])  (Const32 [d]))  => (Const32 [c+d])
106(Add64  (Const64 [c])  (Const64 [d]))  => (Const64 [c+d])
107(Add32F (Const32F [c]) (Const32F [d])) && c+d == c+d => (Const32F [c+d])
108(Add64F (Const64F [c]) (Const64F [d])) && c+d == c+d => (Const64F [c+d])
109(AddPtr <t> x (Const64 [c])) => (OffPtr <t> x [c])
110(AddPtr <t> x (Const32 [c])) => (OffPtr <t> x [int64(c)])
111
112(Sub8   (Const8 [c]) (Const8 [d]))     => (Const8 [c-d])
113(Sub16  (Const16 [c]) (Const16 [d]))   => (Const16 [c-d])
114(Sub32  (Const32 [c]) (Const32 [d]))   => (Const32 [c-d])
115(Sub64  (Const64 [c]) (Const64 [d]))   => (Const64 [c-d])
116(Sub32F (Const32F [c]) (Const32F [d])) && c-d == c-d => (Const32F [c-d])
117(Sub64F (Const64F [c]) (Const64F [d])) && c-d == c-d => (Const64F [c-d])
118
119(Mul8   (Const8 [c])   (Const8 [d]))   => (Const8  [c*d])
120(Mul16  (Const16 [c])  (Const16 [d]))  => (Const16 [c*d])
121(Mul32  (Const32 [c])  (Const32 [d]))  => (Const32 [c*d])
122(Mul64  (Const64 [c])  (Const64 [d]))  => (Const64 [c*d])
123(Mul32F (Const32F [c]) (Const32F [d])) && c*d == c*d => (Const32F [c*d])
124(Mul64F (Const64F [c]) (Const64F [d])) && c*d == c*d => (Const64F [c*d])
125
126(And8   (Const8 [c])   (Const8 [d]))   => (Const8  [c&d])
127(And16  (Const16 [c])  (Const16 [d]))  => (Const16 [c&d])
128(And32  (Const32 [c])  (Const32 [d]))  => (Const32 [c&d])
129(And64  (Const64 [c])  (Const64 [d]))  => (Const64 [c&d])
130
131(Or8   (Const8 [c])   (Const8 [d]))   => (Const8  [c|d])
132(Or16  (Const16 [c])  (Const16 [d]))  => (Const16 [c|d])
133(Or32  (Const32 [c])  (Const32 [d]))  => (Const32 [c|d])
134(Or64  (Const64 [c])  (Const64 [d]))  => (Const64 [c|d])
135
136(Xor8   (Const8 [c])   (Const8 [d]))   => (Const8  [c^d])
137(Xor16  (Const16 [c])  (Const16 [d]))  => (Const16 [c^d])
138(Xor32  (Const32 [c])  (Const32 [d]))  => (Const32 [c^d])
139(Xor64  (Const64 [c])  (Const64 [d]))  => (Const64 [c^d])
140
141(Ctz64 (Const64 [c])) && config.PtrSize == 4 => (Const32 [int32(ntz64(c))])
142(Ctz32 (Const32 [c])) && config.PtrSize == 4 => (Const32 [int32(ntz32(c))])
143(Ctz16 (Const16 [c])) && config.PtrSize == 4 => (Const32 [int32(ntz16(c))])
144(Ctz8  (Const8  [c])) && config.PtrSize == 4 => (Const32 [int32(ntz8(c))])
145
146(Ctz64 (Const64 [c])) && config.PtrSize == 8 => (Const64 [int64(ntz64(c))])
147(Ctz32 (Const32 [c])) && config.PtrSize == 8 => (Const64 [int64(ntz32(c))])
148(Ctz16 (Const16 [c])) && config.PtrSize == 8 => (Const64 [int64(ntz16(c))])
149(Ctz8  (Const8  [c])) && config.PtrSize == 8 => (Const64 [int64(ntz8(c))])
150
151(Div8   (Const8  [c])  (Const8  [d])) && d != 0 => (Const8  [c/d])
152(Div16  (Const16 [c])  (Const16 [d])) && d != 0 => (Const16 [c/d])
153(Div32  (Const32 [c])  (Const32 [d])) && d != 0 => (Const32 [c/d])
154(Div64  (Const64 [c])  (Const64 [d])) && d != 0 => (Const64 [c/d])
155(Div8u  (Const8  [c])  (Const8  [d])) && d != 0 => (Const8  [int8(uint8(c)/uint8(d))])
156(Div16u (Const16 [c])  (Const16 [d])) && d != 0 => (Const16 [int16(uint16(c)/uint16(d))])
157(Div32u (Const32 [c])  (Const32 [d])) && d != 0 => (Const32 [int32(uint32(c)/uint32(d))])
158(Div64u (Const64 [c])  (Const64 [d])) && d != 0 => (Const64 [int64(uint64(c)/uint64(d))])
159(Div32F (Const32F [c]) (Const32F [d])) && c/d == c/d => (Const32F [c/d])
160(Div64F (Const64F [c]) (Const64F [d])) && c/d == c/d => (Const64F [c/d])
161(Select0 (Div128u (Const64 [0]) lo y)) => (Div64u lo y)
162(Select1 (Div128u (Const64 [0]) lo y)) => (Mod64u lo y)
163
164(Not (ConstBool [c])) => (ConstBool [!c])
165
166(Floor       (Const64F [c])) => (Const64F [math.Floor(c)])
167(Ceil        (Const64F [c])) => (Const64F [math.Ceil(c)])
168(Trunc       (Const64F [c])) => (Const64F [math.Trunc(c)])
169(RoundToEven (Const64F [c])) => (Const64F [math.RoundToEven(c)])
170
171// Convert x * 1 to x.
172(Mul(8|16|32|64)  (Const(8|16|32|64)  [1]) x) => x
173(Select0 (Mul(32|64)uover (Const(32|64) [1]) x)) => x
174(Select1 (Mul(32|64)uover (Const(32|64) [1]) x)) => (ConstBool [false])
175
176// Convert x * -1 to -x.
177(Mul(8|16|32|64)  (Const(8|16|32|64)  [-1]) x) => (Neg(8|16|32|64)  x)
178
179// DeMorgan's Laws
180(And(8|16|32|64) <t> (Com(8|16|32|64) x) (Com(8|16|32|64) y)) => (Com(8|16|32|64) (Or(8|16|32|64) <t> x y))
181(Or(8|16|32|64) <t> (Com(8|16|32|64) x) (Com(8|16|32|64) y)) => (Com(8|16|32|64) (And(8|16|32|64) <t> x y))
182
183// Convert multiplication by a power of two to a shift.
184(Mul8  <t> n (Const8  [c])) && isPowerOfTwo8(c) => (Lsh8x64  <t> n (Const64 <typ.UInt64> [log8(c)]))
185(Mul16 <t> n (Const16 [c])) && isPowerOfTwo16(c) => (Lsh16x64 <t> n (Const64 <typ.UInt64> [log16(c)]))
186(Mul32 <t> n (Const32 [c])) && isPowerOfTwo32(c) => (Lsh32x64 <t> n (Const64 <typ.UInt64> [log32(c)]))
187(Mul64 <t> n (Const64 [c])) && isPowerOfTwo64(c) => (Lsh64x64 <t> n (Const64 <typ.UInt64> [log64(c)]))
188(Mul8  <t> n (Const8  [c])) && t.IsSigned() && isPowerOfTwo8(-c)  => (Neg8  (Lsh8x64  <t> n (Const64 <typ.UInt64> [log8(-c)])))
189(Mul16 <t> n (Const16 [c])) && t.IsSigned() && isPowerOfTwo16(-c) => (Neg16 (Lsh16x64 <t> n (Const64 <typ.UInt64> [log16(-c)])))
190(Mul32 <t> n (Const32 [c])) && t.IsSigned() && isPowerOfTwo32(-c) => (Neg32 (Lsh32x64 <t> n (Const64 <typ.UInt64> [log32(-c)])))
191(Mul64 <t> n (Const64 [c])) && t.IsSigned() && isPowerOfTwo64(-c) => (Neg64 (Lsh64x64 <t> n (Const64 <typ.UInt64> [log64(-c)])))
192
193(Mod8  (Const8  [c]) (Const8  [d])) && d != 0 => (Const8  [c % d])
194(Mod16 (Const16 [c]) (Const16 [d])) && d != 0 => (Const16 [c % d])
195(Mod32 (Const32 [c]) (Const32 [d])) && d != 0 => (Const32 [c % d])
196(Mod64 (Const64 [c]) (Const64 [d])) && d != 0 => (Const64 [c % d])
197
198(Mod8u  (Const8 [c])  (Const8  [d])) && d != 0 => (Const8  [int8(uint8(c) % uint8(d))])
199(Mod16u (Const16 [c]) (Const16 [d])) && d != 0 => (Const16 [int16(uint16(c) % uint16(d))])
200(Mod32u (Const32 [c]) (Const32 [d])) && d != 0 => (Const32 [int32(uint32(c) % uint32(d))])
201(Mod64u (Const64 [c]) (Const64 [d])) && d != 0 => (Const64 [int64(uint64(c) % uint64(d))])
202
203(Lsh64x64  (Const64 [c]) (Const64 [d])) => (Const64 [c << uint64(d)])
204(Rsh64x64  (Const64 [c]) (Const64 [d])) => (Const64 [c >> uint64(d)])
205(Rsh64Ux64 (Const64 [c]) (Const64 [d])) => (Const64 [int64(uint64(c) >> uint64(d))])
206(Lsh32x64  (Const32 [c]) (Const64 [d])) => (Const32 [c << uint64(d)])
207(Rsh32x64  (Const32 [c]) (Const64 [d])) => (Const32 [c >> uint64(d)])
208(Rsh32Ux64 (Const32 [c]) (Const64 [d])) => (Const32 [int32(uint32(c) >> uint64(d))])
209(Lsh16x64  (Const16 [c]) (Const64 [d])) => (Const16 [c << uint64(d)])
210(Rsh16x64  (Const16 [c]) (Const64 [d])) => (Const16 [c >> uint64(d)])
211(Rsh16Ux64 (Const16 [c]) (Const64 [d])) => (Const16 [int16(uint16(c) >> uint64(d))])
212(Lsh8x64   (Const8  [c]) (Const64 [d])) => (Const8  [c << uint64(d)])
213(Rsh8x64   (Const8  [c]) (Const64 [d])) => (Const8  [c >> uint64(d)])
214(Rsh8Ux64  (Const8  [c]) (Const64 [d])) => (Const8  [int8(uint8(c) >> uint64(d))])
215
216// Fold IsInBounds when the range of the index cannot exceed the limit.
217(IsInBounds (ZeroExt8to32  _) (Const32 [c])) && (1 << 8)  <= c => (ConstBool [true])
218(IsInBounds (ZeroExt8to64  _) (Const64 [c])) && (1 << 8)  <= c => (ConstBool [true])
219(IsInBounds (ZeroExt16to32 _) (Const32 [c])) && (1 << 16) <= c => (ConstBool [true])
220(IsInBounds (ZeroExt16to64 _) (Const64 [c])) && (1 << 16) <= c => (ConstBool [true])
221(IsInBounds x x) => (ConstBool [false])
222(IsInBounds                (And8  (Const8  [c]) _)  (Const8  [d])) && 0 <= c && c < d => (ConstBool [true])
223(IsInBounds (ZeroExt8to16  (And8  (Const8  [c]) _)) (Const16 [d])) && 0 <= c && int16(c) < d => (ConstBool [true])
224(IsInBounds (ZeroExt8to32  (And8  (Const8  [c]) _)) (Const32 [d])) && 0 <= c && int32(c) < d => (ConstBool [true])
225(IsInBounds (ZeroExt8to64  (And8  (Const8  [c]) _)) (Const64 [d])) && 0 <= c && int64(c) < d => (ConstBool [true])
226(IsInBounds                (And16 (Const16 [c]) _)  (Const16 [d])) && 0 <= c && c < d => (ConstBool [true])
227(IsInBounds (ZeroExt16to32 (And16 (Const16 [c]) _)) (Const32 [d])) && 0 <= c && int32(c) < d => (ConstBool [true])
228(IsInBounds (ZeroExt16to64 (And16 (Const16 [c]) _)) (Const64 [d])) && 0 <= c && int64(c) < d => (ConstBool [true])
229(IsInBounds                (And32 (Const32 [c]) _)  (Const32 [d])) && 0 <= c && c < d => (ConstBool [true])
230(IsInBounds (ZeroExt32to64 (And32 (Const32 [c]) _)) (Const64 [d])) && 0 <= c && int64(c) < d => (ConstBool [true])
231(IsInBounds                (And64 (Const64 [c]) _)  (Const64 [d])) && 0 <= c && c < d => (ConstBool [true])
232(IsInBounds (Const32 [c]) (Const32 [d])) => (ConstBool [0 <= c && c < d])
233(IsInBounds (Const64 [c]) (Const64 [d])) => (ConstBool [0 <= c && c < d])
234// (Mod64u x y) is always between 0 (inclusive) and y (exclusive).
235(IsInBounds (Mod32u _ y) y) => (ConstBool [true])
236(IsInBounds (Mod64u _ y) y) => (ConstBool [true])
237// Right shifting an unsigned number limits its value.
238(IsInBounds (ZeroExt8to64  (Rsh8Ux64  _ (Const64 [c]))) (Const64 [d])) && 0 < c && c <  8 && 1<<uint( 8-c)-1 < d => (ConstBool [true])
239(IsInBounds (ZeroExt8to32  (Rsh8Ux64  _ (Const64 [c]))) (Const32 [d])) && 0 < c && c <  8 && 1<<uint( 8-c)-1 < d => (ConstBool [true])
240(IsInBounds (ZeroExt8to16  (Rsh8Ux64  _ (Const64 [c]))) (Const16 [d])) && 0 < c && c <  8 && 1<<uint( 8-c)-1 < d => (ConstBool [true])
241(IsInBounds                (Rsh8Ux64  _ (Const64 [c]))  (Const64 [d])) && 0 < c && c <  8 && 1<<uint( 8-c)-1 < d => (ConstBool [true])
242(IsInBounds (ZeroExt16to64 (Rsh16Ux64 _ (Const64 [c]))) (Const64 [d])) && 0 < c && c < 16 && 1<<uint(16-c)-1 < d => (ConstBool [true])
243(IsInBounds (ZeroExt16to32 (Rsh16Ux64 _ (Const64 [c]))) (Const64 [d])) && 0 < c && c < 16 && 1<<uint(16-c)-1 < d => (ConstBool [true])
244(IsInBounds                (Rsh16Ux64 _ (Const64 [c]))  (Const64 [d])) && 0 < c && c < 16 && 1<<uint(16-c)-1 < d => (ConstBool [true])
245(IsInBounds (ZeroExt32to64 (Rsh32Ux64 _ (Const64 [c]))) (Const64 [d])) && 0 < c && c < 32 && 1<<uint(32-c)-1 < d => (ConstBool [true])
246(IsInBounds                (Rsh32Ux64 _ (Const64 [c]))  (Const64 [d])) && 0 < c && c < 32 && 1<<uint(32-c)-1 < d => (ConstBool [true])
247(IsInBounds                (Rsh64Ux64 _ (Const64 [c]))  (Const64 [d])) && 0 < c && c < 64 && 1<<uint(64-c)-1 < d => (ConstBool [true])
248
249(IsSliceInBounds x x) => (ConstBool [true])
250(IsSliceInBounds (And32 (Const32 [c]) _) (Const32 [d])) && 0 <= c && c <= d => (ConstBool [true])
251(IsSliceInBounds (And64 (Const64 [c]) _) (Const64 [d])) && 0 <= c && c <= d => (ConstBool [true])
252(IsSliceInBounds (Const32 [0]) _) => (ConstBool [true])
253(IsSliceInBounds (Const64 [0]) _) => (ConstBool [true])
254(IsSliceInBounds (Const32 [c]) (Const32 [d])) => (ConstBool [0 <= c && c <= d])
255(IsSliceInBounds (Const64 [c]) (Const64 [d])) => (ConstBool [0 <= c && c <= d])
256(IsSliceInBounds (SliceLen x) (SliceCap x)) => (ConstBool [true])
257
258(Eq(64|32|16|8) x x) => (ConstBool [true])
259(EqB (ConstBool [c]) (ConstBool [d])) => (ConstBool [c == d])
260(EqB (ConstBool [false]) x) => (Not x)
261(EqB (ConstBool [true]) x) => x
262
263(Neq(64|32|16|8) x x) => (ConstBool [false])
264(NeqB (ConstBool [c]) (ConstBool [d])) => (ConstBool [c != d])
265(NeqB (ConstBool [false]) x) => x
266(NeqB (ConstBool [true]) x) => (Not x)
267(NeqB (Not x) (Not y)) => (NeqB x y)
268
269(Eq64 (Const64 <t> [c]) (Add64 (Const64 <t> [d]) x)) => (Eq64 (Const64 <t> [c-d]) x)
270(Eq32 (Const32 <t> [c]) (Add32 (Const32 <t> [d]) x)) => (Eq32 (Const32 <t> [c-d]) x)
271(Eq16 (Const16 <t> [c]) (Add16 (Const16 <t> [d]) x)) => (Eq16 (Const16 <t> [c-d]) x)
272(Eq8  (Const8  <t> [c]) (Add8  (Const8  <t> [d]) x)) => (Eq8  (Const8  <t> [c-d]) x)
273
274(Neq64 (Const64 <t> [c]) (Add64 (Const64 <t> [d]) x)) => (Neq64 (Const64 <t> [c-d]) x)
275(Neq32 (Const32 <t> [c]) (Add32 (Const32 <t> [d]) x)) => (Neq32 (Const32 <t> [c-d]) x)
276(Neq16 (Const16 <t> [c]) (Add16 (Const16 <t> [d]) x)) => (Neq16 (Const16 <t> [c-d]) x)
277(Neq8  (Const8  <t> [c]) (Add8  (Const8  <t> [d]) x)) => (Neq8  (Const8  <t> [c-d]) x)
278
279// signed integer range: ( c <= x && x (<|<=) d ) -> ( unsigned(x-c) (<|<=) unsigned(d-c) )
280(AndB (Leq64 (Const64 [c]) x) ((Less|Leq)64 x (Const64 [d]))) && d >= c => ((Less|Leq)64U (Sub64 <x.Type> x (Const64 <x.Type> [c])) (Const64 <x.Type> [d-c]))
281(AndB (Leq32 (Const32 [c]) x) ((Less|Leq)32 x (Const32 [d]))) && d >= c => ((Less|Leq)32U (Sub32 <x.Type> x (Const32 <x.Type> [c])) (Const32 <x.Type> [d-c]))
282(AndB (Leq16 (Const16 [c]) x) ((Less|Leq)16 x (Const16 [d]))) && d >= c => ((Less|Leq)16U (Sub16 <x.Type> x (Const16 <x.Type> [c])) (Const16 <x.Type> [d-c]))
283(AndB (Leq8  (Const8  [c]) x) ((Less|Leq)8  x (Const8  [d]))) && d >= c => ((Less|Leq)8U  (Sub8  <x.Type> x (Const8  <x.Type> [c])) (Const8  <x.Type> [d-c]))
284
285// signed integer range: ( c < x && x (<|<=) d ) -> ( unsigned(x-(c+1)) (<|<=) unsigned(d-(c+1)) )
286(AndB (Less64 (Const64 [c]) x) ((Less|Leq)64 x (Const64 [d]))) && d >= c+1 && c+1 > c => ((Less|Leq)64U (Sub64 <x.Type> x (Const64 <x.Type> [c+1])) (Const64 <x.Type> [d-c-1]))
287(AndB (Less32 (Const32 [c]) x) ((Less|Leq)32 x (Const32 [d]))) && d >= c+1 && c+1 > c => ((Less|Leq)32U (Sub32 <x.Type> x (Const32 <x.Type> [c+1])) (Const32 <x.Type> [d-c-1]))
288(AndB (Less16 (Const16 [c]) x) ((Less|Leq)16 x (Const16 [d]))) && d >= c+1 && c+1 > c => ((Less|Leq)16U (Sub16 <x.Type> x (Const16 <x.Type> [c+1])) (Const16 <x.Type> [d-c-1]))
289(AndB (Less8  (Const8  [c]) x) ((Less|Leq)8  x (Const8  [d]))) && d >= c+1 && c+1 > c => ((Less|Leq)8U  (Sub8  <x.Type> x (Const8  <x.Type> [c+1])) (Const8  <x.Type> [d-c-1]))
290
291// unsigned integer range: ( c <= x && x (<|<=) d ) -> ( x-c (<|<=) d-c )
292(AndB (Leq64U (Const64 [c]) x) ((Less|Leq)64U x (Const64 [d]))) && uint64(d) >= uint64(c) => ((Less|Leq)64U (Sub64 <x.Type> x (Const64 <x.Type> [c])) (Const64 <x.Type> [d-c]))
293(AndB (Leq32U (Const32 [c]) x) ((Less|Leq)32U x (Const32 [d]))) && uint32(d) >= uint32(c) => ((Less|Leq)32U (Sub32 <x.Type> x (Const32 <x.Type> [c])) (Const32 <x.Type> [d-c]))
294(AndB (Leq16U (Const16 [c]) x) ((Less|Leq)16U x (Const16 [d]))) && uint16(d) >= uint16(c) => ((Less|Leq)16U (Sub16 <x.Type> x (Const16 <x.Type> [c])) (Const16 <x.Type> [d-c]))
295(AndB (Leq8U  (Const8  [c]) x) ((Less|Leq)8U  x (Const8  [d]))) && uint8(d)  >= uint8(c)  => ((Less|Leq)8U  (Sub8  <x.Type> x (Const8  <x.Type> [c])) (Const8  <x.Type> [d-c]))
296
297// unsigned integer range: ( c < x && x (<|<=) d ) -> ( x-(c+1) (<|<=) d-(c+1) )
298(AndB (Less64U (Const64 [c]) x) ((Less|Leq)64U x (Const64 [d]))) && uint64(d) >= uint64(c+1) && uint64(c+1) > uint64(c) => ((Less|Leq)64U (Sub64 <x.Type> x (Const64 <x.Type> [c+1])) (Const64 <x.Type> [d-c-1]))
299(AndB (Less32U (Const32 [c]) x) ((Less|Leq)32U x (Const32 [d]))) && uint32(d) >= uint32(c+1) && uint32(c+1) > uint32(c) => ((Less|Leq)32U (Sub32 <x.Type> x (Const32 <x.Type> [c+1])) (Const32 <x.Type> [d-c-1]))
300(AndB (Less16U (Const16 [c]) x) ((Less|Leq)16U x (Const16 [d]))) && uint16(d) >= uint16(c+1) && uint16(c+1) > uint16(c) => ((Less|Leq)16U (Sub16 <x.Type> x (Const16 <x.Type> [c+1])) (Const16 <x.Type> [d-c-1]))
301(AndB (Less8U  (Const8  [c]) x) ((Less|Leq)8U  x (Const8  [d]))) && uint8(d)  >= uint8(c+1)  && uint8(c+1)  > uint8(c)  => ((Less|Leq)8U  (Sub8  <x.Type> x (Const8  <x.Type> [c+1]))  (Const8  <x.Type> [d-c-1]))
302
303// signed integer range: ( c (<|<=) x || x < d ) -> ( unsigned(c-d) (<|<=) unsigned(x-d) )
304(OrB ((Less|Leq)64 (Const64 [c]) x) (Less64 x (Const64 [d]))) && c >= d => ((Less|Leq)64U (Const64 <x.Type> [c-d]) (Sub64 <x.Type> x (Const64 <x.Type> [d])))
305(OrB ((Less|Leq)32 (Const32 [c]) x) (Less32 x (Const32 [d]))) && c >= d => ((Less|Leq)32U (Const32 <x.Type> [c-d]) (Sub32 <x.Type> x (Const32 <x.Type> [d])))
306(OrB ((Less|Leq)16 (Const16 [c]) x) (Less16 x (Const16 [d]))) && c >= d => ((Less|Leq)16U (Const16 <x.Type> [c-d]) (Sub16 <x.Type> x (Const16 <x.Type> [d])))
307(OrB ((Less|Leq)8  (Const8  [c]) x) (Less8  x (Const8  [d]))) && c >= d => ((Less|Leq)8U  (Const8  <x.Type> [c-d]) (Sub8  <x.Type> x (Const8  <x.Type> [d])))
308
309// signed integer range: ( c (<|<=) x || x <= d ) -> ( unsigned(c-(d+1)) (<|<=) unsigned(x-(d+1)) )
310(OrB ((Less|Leq)64 (Const64 [c]) x) (Leq64 x (Const64 [d]))) && c >= d+1 && d+1 > d => ((Less|Leq)64U (Const64 <x.Type> [c-d-1]) (Sub64 <x.Type> x (Const64 <x.Type> [d+1])))
311(OrB ((Less|Leq)32 (Const32 [c]) x) (Leq32 x (Const32 [d]))) && c >= d+1 && d+1 > d => ((Less|Leq)32U (Const32 <x.Type> [c-d-1]) (Sub32 <x.Type> x (Const32 <x.Type> [d+1])))
312(OrB ((Less|Leq)16 (Const16 [c]) x) (Leq16 x (Const16 [d]))) && c >= d+1 && d+1 > d => ((Less|Leq)16U (Const16 <x.Type> [c-d-1]) (Sub16 <x.Type> x (Const16 <x.Type> [d+1])))
313(OrB ((Less|Leq)8  (Const8  [c]) x) (Leq8  x (Const8  [d]))) && c >= d+1 && d+1 > d => ((Less|Leq)8U  (Const8  <x.Type> [c-d-1]) (Sub8  <x.Type> x (Const8  <x.Type> [d+1])))
314
315// unsigned integer range: ( c (<|<=) x || x < d ) -> ( c-d (<|<=) x-d )
316(OrB ((Less|Leq)64U (Const64 [c]) x) (Less64U x (Const64 [d]))) && uint64(c) >= uint64(d) => ((Less|Leq)64U (Const64 <x.Type> [c-d]) (Sub64 <x.Type> x (Const64 <x.Type> [d])))
317(OrB ((Less|Leq)32U (Const32 [c]) x) (Less32U x (Const32 [d]))) && uint32(c) >= uint32(d) => ((Less|Leq)32U (Const32 <x.Type> [c-d]) (Sub32 <x.Type> x (Const32 <x.Type> [d])))
318(OrB ((Less|Leq)16U (Const16 [c]) x) (Less16U x (Const16 [d]))) && uint16(c) >= uint16(d) => ((Less|Leq)16U (Const16 <x.Type> [c-d]) (Sub16 <x.Type> x (Const16 <x.Type> [d])))
319(OrB ((Less|Leq)8U  (Const8  [c]) x) (Less8U  x (Const8  [d]))) && uint8(c)  >= uint8(d)  => ((Less|Leq)8U  (Const8  <x.Type> [c-d]) (Sub8  <x.Type> x (Const8  <x.Type> [d])))
320
321// unsigned integer range: ( c (<|<=) x || x <= d ) -> ( c-(d+1) (<|<=) x-(d+1) )
322(OrB ((Less|Leq)64U (Const64 [c]) x) (Leq64U x (Const64 [d]))) && uint64(c) >= uint64(d+1) && uint64(d+1) > uint64(d) => ((Less|Leq)64U (Const64 <x.Type> [c-d-1]) (Sub64 <x.Type> x (Const64 <x.Type> [d+1])))
323(OrB ((Less|Leq)32U (Const32 [c]) x) (Leq32U x (Const32 [d]))) && uint32(c) >= uint32(d+1) && uint32(d+1) > uint32(d) => ((Less|Leq)32U (Const32 <x.Type> [c-d-1]) (Sub32 <x.Type> x (Const32 <x.Type> [d+1])))
324(OrB ((Less|Leq)16U (Const16 [c]) x) (Leq16U x (Const16 [d]))) && uint16(c) >= uint16(d+1) && uint16(d+1) > uint16(d) => ((Less|Leq)16U (Const16 <x.Type> [c-d-1]) (Sub16 <x.Type> x (Const16 <x.Type> [d+1])))
325(OrB ((Less|Leq)8U  (Const8  [c]) x) (Leq8U  x (Const8  [d]))) && uint8(c)  >= uint8(d+1)  && uint8(d+1)  > uint8(d)  => ((Less|Leq)8U  (Const8  <x.Type> [c-d-1]) (Sub8  <x.Type> x (Const8  <x.Type> [d+1])))
326
327// Canonicalize x-const to x+(-const)
328(Sub64 x (Const64 <t> [c])) && x.Op != OpConst64 => (Add64 (Const64 <t> [-c]) x)
329(Sub32 x (Const32 <t> [c])) && x.Op != OpConst32 => (Add32 (Const32 <t> [-c]) x)
330(Sub16 x (Const16 <t> [c])) && x.Op != OpConst16 => (Add16 (Const16 <t> [-c]) x)
331(Sub8  x (Const8  <t> [c])) && x.Op != OpConst8  => (Add8  (Const8  <t> [-c]) x)
332
333// fold negation into comparison operators
334(Not (Eq(64|32|16|8|B|Ptr|64F|32F) x y)) => (Neq(64|32|16|8|B|Ptr|64F|32F) x y)
335(Not (Neq(64|32|16|8|B|Ptr|64F|32F) x y)) => (Eq(64|32|16|8|B|Ptr|64F|32F) x y)
336
337(Not (Less(64|32|16|8) x y)) => (Leq(64|32|16|8) y x)
338(Not (Less(64|32|16|8)U x y)) => (Leq(64|32|16|8)U y x)
339(Not (Leq(64|32|16|8) x y)) => (Less(64|32|16|8) y x)
340(Not (Leq(64|32|16|8)U x y)) => (Less(64|32|16|8)U y x)
341
342// Distribute multiplication c * (d+x) -> c*d + c*x. Useful for:
343// a[i].b = ...; a[i+1].b = ...
344(Mul64 (Const64 <t> [c]) (Add64 <t> (Const64 <t> [d]) x)) =>
345  (Add64 (Const64 <t> [c*d]) (Mul64 <t> (Const64 <t> [c]) x))
346(Mul32 (Const32 <t> [c]) (Add32 <t> (Const32 <t> [d]) x)) =>
347  (Add32 (Const32 <t> [c*d]) (Mul32 <t> (Const32 <t> [c]) x))
348
349// Rewrite x*y ± x*z  to  x*(y±z)
350(Add(64|32|16|8) <t> (Mul(64|32|16|8) x y) (Mul(64|32|16|8) x z))
351	=> (Mul(64|32|16|8) x (Add(64|32|16|8) <t> y z))
352(Sub(64|32|16|8) <t> (Mul(64|32|16|8) x y) (Mul(64|32|16|8) x z))
353	=> (Mul(64|32|16|8) x (Sub(64|32|16|8) <t> y z))
354
355// rewrite shifts of 8/16/32 bit consts into 64 bit consts to reduce
356// the number of the other rewrite rules for const shifts
357(Lsh64x32  <t> x (Const32 [c])) => (Lsh64x64  x (Const64 <t> [int64(uint32(c))]))
358(Lsh64x16  <t> x (Const16 [c])) => (Lsh64x64  x (Const64 <t> [int64(uint16(c))]))
359(Lsh64x8   <t> x (Const8  [c])) => (Lsh64x64  x (Const64 <t> [int64(uint8(c))]))
360(Rsh64x32  <t> x (Const32 [c])) => (Rsh64x64  x (Const64 <t> [int64(uint32(c))]))
361(Rsh64x16  <t> x (Const16 [c])) => (Rsh64x64  x (Const64 <t> [int64(uint16(c))]))
362(Rsh64x8   <t> x (Const8  [c])) => (Rsh64x64  x (Const64 <t> [int64(uint8(c))]))
363(Rsh64Ux32 <t> x (Const32 [c])) => (Rsh64Ux64 x (Const64 <t> [int64(uint32(c))]))
364(Rsh64Ux16 <t> x (Const16 [c])) => (Rsh64Ux64 x (Const64 <t> [int64(uint16(c))]))
365(Rsh64Ux8  <t> x (Const8  [c])) => (Rsh64Ux64 x (Const64 <t> [int64(uint8(c))]))
366
367(Lsh32x32  <t> x (Const32 [c])) => (Lsh32x64  x (Const64 <t> [int64(uint32(c))]))
368(Lsh32x16  <t> x (Const16 [c])) => (Lsh32x64  x (Const64 <t> [int64(uint16(c))]))
369(Lsh32x8   <t> x (Const8  [c])) => (Lsh32x64  x (Const64 <t> [int64(uint8(c))]))
370(Rsh32x32  <t> x (Const32 [c])) => (Rsh32x64  x (Const64 <t> [int64(uint32(c))]))
371(Rsh32x16  <t> x (Const16 [c])) => (Rsh32x64  x (Const64 <t> [int64(uint16(c))]))
372(Rsh32x8   <t> x (Const8  [c])) => (Rsh32x64  x (Const64 <t> [int64(uint8(c))]))
373(Rsh32Ux32 <t> x (Const32 [c])) => (Rsh32Ux64 x (Const64 <t> [int64(uint32(c))]))
374(Rsh32Ux16 <t> x (Const16 [c])) => (Rsh32Ux64 x (Const64 <t> [int64(uint16(c))]))
375(Rsh32Ux8  <t> x (Const8  [c])) => (Rsh32Ux64 x (Const64 <t> [int64(uint8(c))]))
376
377(Lsh16x32  <t> x (Const32 [c])) => (Lsh16x64  x (Const64 <t> [int64(uint32(c))]))
378(Lsh16x16  <t> x (Const16 [c])) => (Lsh16x64  x (Const64 <t> [int64(uint16(c))]))
379(Lsh16x8   <t> x (Const8  [c])) => (Lsh16x64  x (Const64 <t> [int64(uint8(c))]))
380(Rsh16x32  <t> x (Const32 [c])) => (Rsh16x64  x (Const64 <t> [int64(uint32(c))]))
381(Rsh16x16  <t> x (Const16 [c])) => (Rsh16x64  x (Const64 <t> [int64(uint16(c))]))
382(Rsh16x8   <t> x (Const8  [c])) => (Rsh16x64  x (Const64 <t> [int64(uint8(c))]))
383(Rsh16Ux32 <t> x (Const32 [c])) => (Rsh16Ux64 x (Const64 <t> [int64(uint32(c))]))
384(Rsh16Ux16 <t> x (Const16 [c])) => (Rsh16Ux64 x (Const64 <t> [int64(uint16(c))]))
385(Rsh16Ux8  <t> x (Const8  [c])) => (Rsh16Ux64 x (Const64 <t> [int64(uint8(c))]))
386
387(Lsh8x32  <t> x (Const32 [c])) => (Lsh8x64  x (Const64 <t> [int64(uint32(c))]))
388(Lsh8x16  <t> x (Const16 [c])) => (Lsh8x64  x (Const64 <t> [int64(uint16(c))]))
389(Lsh8x8   <t> x (Const8  [c])) => (Lsh8x64  x (Const64 <t> [int64(uint8(c))]))
390(Rsh8x32  <t> x (Const32 [c])) => (Rsh8x64  x (Const64 <t> [int64(uint32(c))]))
391(Rsh8x16  <t> x (Const16 [c])) => (Rsh8x64  x (Const64 <t> [int64(uint16(c))]))
392(Rsh8x8   <t> x (Const8  [c])) => (Rsh8x64  x (Const64 <t> [int64(uint8(c))]))
393(Rsh8Ux32 <t> x (Const32 [c])) => (Rsh8Ux64 x (Const64 <t> [int64(uint32(c))]))
394(Rsh8Ux16 <t> x (Const16 [c])) => (Rsh8Ux64 x (Const64 <t> [int64(uint16(c))]))
395(Rsh8Ux8  <t> x (Const8  [c])) => (Rsh8Ux64 x (Const64 <t> [int64(uint8(c))]))
396
397// shifts by zero
398(Lsh(64|32|16|8)x64  x (Const64 [0])) => x
399(Rsh(64|32|16|8)x64  x (Const64 [0])) => x
400(Rsh(64|32|16|8)Ux64 x (Const64 [0])) => x
401
402// rotates by multiples of register width
403(RotateLeft64 x (Const64 [c])) && c%64 == 0 => x
404(RotateLeft32 x (Const32 [c])) && c%32 == 0 => x
405(RotateLeft16 x (Const16 [c])) && c%16 == 0 => x
406(RotateLeft8  x (Const8 [c]))  && c%8  == 0 => x
407
408// zero shifted
409(Lsh64x(64|32|16|8)  (Const64 [0]) _) => (Const64 [0])
410(Rsh64x(64|32|16|8)  (Const64 [0]) _) => (Const64 [0])
411(Rsh64Ux(64|32|16|8) (Const64 [0]) _) => (Const64 [0])
412(Lsh32x(64|32|16|8)  (Const32 [0]) _) => (Const32 [0])
413(Rsh32x(64|32|16|8)  (Const32 [0]) _) => (Const32 [0])
414(Rsh32Ux(64|32|16|8) (Const32 [0]) _) => (Const32 [0])
415(Lsh16x(64|32|16|8)  (Const16 [0]) _) => (Const16 [0])
416(Rsh16x(64|32|16|8)  (Const16 [0]) _) => (Const16 [0])
417(Rsh16Ux(64|32|16|8) (Const16 [0]) _) => (Const16 [0])
418(Lsh8x(64|32|16|8)   (Const8  [0]) _) => (Const8  [0])
419(Rsh8x(64|32|16|8)   (Const8  [0]) _) => (Const8  [0])
420(Rsh8Ux(64|32|16|8)  (Const8  [0]) _) => (Const8  [0])
421
422// large left shifts of all values, and right shifts of unsigned values
423((Lsh64|Rsh64U)x64  _ (Const64 [c])) && uint64(c) >= 64 => (Const64 [0])
424((Lsh32|Rsh32U)x64  _ (Const64 [c])) && uint64(c) >= 32 => (Const32 [0])
425((Lsh16|Rsh16U)x64  _ (Const64 [c])) && uint64(c) >= 16 => (Const16 [0])
426((Lsh8|Rsh8U)x64    _ (Const64 [c])) && uint64(c) >= 8  => (Const8  [0])
427
428// combine const shifts
429(Lsh64x64 <t> (Lsh64x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) => (Lsh64x64 x (Const64 <t> [c+d]))
430(Lsh32x64 <t> (Lsh32x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) => (Lsh32x64 x (Const64 <t> [c+d]))
431(Lsh16x64 <t> (Lsh16x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) => (Lsh16x64 x (Const64 <t> [c+d]))
432(Lsh8x64  <t> (Lsh8x64  x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) => (Lsh8x64  x (Const64 <t> [c+d]))
433
434(Rsh64x64 <t> (Rsh64x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) => (Rsh64x64 x (Const64 <t> [c+d]))
435(Rsh32x64 <t> (Rsh32x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) => (Rsh32x64 x (Const64 <t> [c+d]))
436(Rsh16x64 <t> (Rsh16x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) => (Rsh16x64 x (Const64 <t> [c+d]))
437(Rsh8x64  <t> (Rsh8x64  x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) => (Rsh8x64  x (Const64 <t> [c+d]))
438
439(Rsh64Ux64 <t> (Rsh64Ux64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) => (Rsh64Ux64 x (Const64 <t> [c+d]))
440(Rsh32Ux64 <t> (Rsh32Ux64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) => (Rsh32Ux64 x (Const64 <t> [c+d]))
441(Rsh16Ux64 <t> (Rsh16Ux64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) => (Rsh16Ux64 x (Const64 <t> [c+d]))
442(Rsh8Ux64  <t> (Rsh8Ux64  x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) => (Rsh8Ux64  x (Const64 <t> [c+d]))
443
444// Remove signed right shift before an unsigned right shift that extracts the sign bit.
445(Rsh8Ux64  (Rsh8x64  x _) (Const64 <t> [7] )) => (Rsh8Ux64  x (Const64 <t> [7] ))
446(Rsh16Ux64 (Rsh16x64 x _) (Const64 <t> [15])) => (Rsh16Ux64 x (Const64 <t> [15]))
447(Rsh32Ux64 (Rsh32x64 x _) (Const64 <t> [31])) => (Rsh32Ux64 x (Const64 <t> [31]))
448(Rsh64Ux64 (Rsh64x64 x _) (Const64 <t> [63])) => (Rsh64Ux64 x (Const64 <t> [63]))
449
450// Convert x>>c<<c to x&^(1<<c-1)
451(Lsh64x64 i:(Rsh(64|64U)x64  x (Const64 [c])) (Const64 [c])) && c >= 0 && c < 64 && i.Uses == 1 => (And64 x (Const64 <v.Type> [int64(-1) << c]))
452(Lsh32x64 i:(Rsh(32|32U)x64  x (Const64 [c])) (Const64 [c])) && c >= 0 && c < 32 && i.Uses == 1 => (And32 x (Const32 <v.Type> [int32(-1) << c]))
453(Lsh16x64 i:(Rsh(16|16U)x64  x (Const64 [c])) (Const64 [c])) && c >= 0 && c < 16 && i.Uses == 1 => (And16 x (Const16 <v.Type> [int16(-1) << c]))
454(Lsh8x64  i:(Rsh(8|8U)x64    x (Const64 [c])) (Const64 [c])) && c >= 0 && c < 8  && i.Uses == 1 => (And8  x (Const8  <v.Type> [int8(-1)  << c]))
455// similarly for x<<c>>c
456(Rsh64Ux64 i:(Lsh64x64 x (Const64 [c])) (Const64 [c])) && c >= 0 && c < 64 && i.Uses == 1 => (And64 x (Const64 <v.Type> [int64(^uint64(0)>>c)]))
457(Rsh32Ux64 i:(Lsh32x64 x (Const64 [c])) (Const64 [c])) && c >= 0 && c < 32 && i.Uses == 1 => (And32 x (Const32 <v.Type> [int32(^uint32(0)>>c)]))
458(Rsh16Ux64 i:(Lsh16x64 x (Const64 [c])) (Const64 [c])) && c >= 0 && c < 16 && i.Uses == 1 => (And16 x (Const16 <v.Type> [int16(^uint16(0)>>c)]))
459(Rsh8Ux64  i:(Lsh8x64  x (Const64 [c])) (Const64 [c])) && c >= 0 && c < 8  && i.Uses == 1 => (And8  x (Const8  <v.Type> [int8 (^uint8 (0)>>c)]))
460
461// ((x >> c1) << c2) >> c3
462(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]))
463  && uint64(c1) >= uint64(c2) && uint64(c3) >= uint64(c2) && !uaddOvf(c1-c2, c3)
464  => (Rsh(64|32|16|8)Ux64 x (Const64 <typ.UInt64> [c1-c2+c3]))
465
466// ((x << c1) >> c2) << c3
467(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]))
468  && uint64(c1) >= uint64(c2) && uint64(c3) >= uint64(c2) && !uaddOvf(c1-c2, c3)
469  => (Lsh(64|32|16|8)x64 x (Const64 <typ.UInt64> [c1-c2+c3]))
470
471// (x >> c) & uppermask = 0
472(And64 (Const64 [m]) (Rsh64Ux64 _ (Const64 [c]))) && c >= int64(64-ntz64(m)) => (Const64 [0])
473(And32 (Const32 [m]) (Rsh32Ux64 _ (Const64 [c]))) && c >= int64(32-ntz32(m)) => (Const32 [0])
474(And16 (Const16 [m]) (Rsh16Ux64 _ (Const64 [c]))) && c >= int64(16-ntz16(m)) => (Const16 [0])
475(And8  (Const8  [m]) (Rsh8Ux64  _ (Const64 [c]))) && c >= int64(8-ntz8(m))  => (Const8  [0])
476
477// (x << c) & lowermask = 0
478(And64 (Const64 [m]) (Lsh64x64  _ (Const64 [c]))) && c >= int64(64-nlz64(m)) => (Const64 [0])
479(And32 (Const32 [m]) (Lsh32x64  _ (Const64 [c]))) && c >= int64(32-nlz32(m)) => (Const32 [0])
480(And16 (Const16 [m]) (Lsh16x64  _ (Const64 [c]))) && c >= int64(16-nlz16(m)) => (Const16 [0])
481(And8  (Const8  [m]) (Lsh8x64   _ (Const64 [c]))) && c >= int64(8-nlz8(m))  => (Const8  [0])
482
483// replace shifts with zero extensions
484(Rsh16Ux64 (Lsh16x64 x (Const64  [8])) (Const64  [8])) => (ZeroExt8to16  (Trunc16to8  <typ.UInt8>  x))
485(Rsh32Ux64 (Lsh32x64 x (Const64 [24])) (Const64 [24])) => (ZeroExt8to32  (Trunc32to8  <typ.UInt8>  x))
486(Rsh64Ux64 (Lsh64x64 x (Const64 [56])) (Const64 [56])) => (ZeroExt8to64  (Trunc64to8  <typ.UInt8>  x))
487(Rsh32Ux64 (Lsh32x64 x (Const64 [16])) (Const64 [16])) => (ZeroExt16to32 (Trunc32to16 <typ.UInt16> x))
488(Rsh64Ux64 (Lsh64x64 x (Const64 [48])) (Const64 [48])) => (ZeroExt16to64 (Trunc64to16 <typ.UInt16> x))
489(Rsh64Ux64 (Lsh64x64 x (Const64 [32])) (Const64 [32])) => (ZeroExt32to64 (Trunc64to32 <typ.UInt32> x))
490
491// replace shifts with sign extensions
492(Rsh16x64 (Lsh16x64 x (Const64  [8])) (Const64  [8])) => (SignExt8to16  (Trunc16to8  <typ.Int8>  x))
493(Rsh32x64 (Lsh32x64 x (Const64 [24])) (Const64 [24])) => (SignExt8to32  (Trunc32to8  <typ.Int8>  x))
494(Rsh64x64 (Lsh64x64 x (Const64 [56])) (Const64 [56])) => (SignExt8to64  (Trunc64to8  <typ.Int8>  x))
495(Rsh32x64 (Lsh32x64 x (Const64 [16])) (Const64 [16])) => (SignExt16to32 (Trunc32to16 <typ.Int16> x))
496(Rsh64x64 (Lsh64x64 x (Const64 [48])) (Const64 [48])) => (SignExt16to64 (Trunc64to16 <typ.Int16> x))
497(Rsh64x64 (Lsh64x64 x (Const64 [32])) (Const64 [32])) => (SignExt32to64 (Trunc64to32 <typ.Int32> x))
498
499// constant comparisons
500(Eq(64|32|16|8)   (Const(64|32|16|8) [c]) (Const(64|32|16|8) [d])) => (ConstBool [c == d])
501(Neq(64|32|16|8)  (Const(64|32|16|8) [c]) (Const(64|32|16|8) [d])) => (ConstBool [c != d])
502(Less(64|32|16|8) (Const(64|32|16|8) [c]) (Const(64|32|16|8) [d])) => (ConstBool [c < d])
503(Leq(64|32|16|8)  (Const(64|32|16|8) [c]) (Const(64|32|16|8) [d])) => (ConstBool [c <= d])
504
505(Less64U (Const64 [c]) (Const64 [d])) => (ConstBool [uint64(c) < uint64(d)])
506(Less32U (Const32 [c]) (Const32 [d])) => (ConstBool [uint32(c) < uint32(d)])
507(Less16U (Const16 [c]) (Const16 [d])) => (ConstBool [uint16(c) < uint16(d)])
508(Less8U  (Const8  [c]) (Const8  [d])) => (ConstBool [ uint8(c) <  uint8(d)])
509
510(Leq64U (Const64 [c]) (Const64 [d])) => (ConstBool [uint64(c) <= uint64(d)])
511(Leq32U (Const32 [c]) (Const32 [d])) => (ConstBool [uint32(c) <= uint32(d)])
512(Leq16U (Const16 [c]) (Const16 [d])) => (ConstBool [uint16(c) <= uint16(d)])
513(Leq8U  (Const8  [c]) (Const8  [d])) => (ConstBool [ uint8(c) <=  uint8(d)])
514
515(Leq8  (Const8  [0]) (And8  _ (Const8  [c]))) && c >= 0 => (ConstBool [true])
516(Leq16 (Const16 [0]) (And16 _ (Const16 [c]))) && c >= 0 => (ConstBool [true])
517(Leq32 (Const32 [0]) (And32 _ (Const32 [c]))) && c >= 0 => (ConstBool [true])
518(Leq64 (Const64 [0]) (And64 _ (Const64 [c]))) && c >= 0 => (ConstBool [true])
519
520(Leq8  (Const8  [0]) (Rsh8Ux64  _ (Const64 [c]))) && c > 0 => (ConstBool [true])
521(Leq16 (Const16 [0]) (Rsh16Ux64 _ (Const64 [c]))) && c > 0 => (ConstBool [true])
522(Leq32 (Const32 [0]) (Rsh32Ux64 _ (Const64 [c]))) && c > 0 => (ConstBool [true])
523(Leq64 (Const64 [0]) (Rsh64Ux64 _ (Const64 [c]))) && c > 0 => (ConstBool [true])
524
525// prefer equalities with zero
526(Less(64|32|16|8) (Const(64|32|16|8) <t> [0]) x) && isNonNegative(x) => (Neq(64|32|16|8) (Const(64|32|16|8) <t> [0]) x)
527(Less(64|32|16|8) x (Const(64|32|16|8) <t> [1])) && isNonNegative(x) => (Eq(64|32|16|8) (Const(64|32|16|8) <t> [0]) x)
528(Less(64|32|16|8)U x (Const(64|32|16|8) <t> [1])) => (Eq(64|32|16|8) (Const(64|32|16|8) <t> [0]) x)
529(Leq(64|32|16|8)U (Const(64|32|16|8) <t> [1]) x) => (Neq(64|32|16|8) (Const(64|32|16|8) <t> [0]) x)
530
531// prefer comparisons with zero
532(Less(64|32|16|8) x (Const(64|32|16|8) <t> [1])) => (Leq(64|32|16|8) x (Const(64|32|16|8) <t> [0]))
533(Leq(64|32|16|8) x (Const(64|32|16|8) <t> [-1])) => (Less(64|32|16|8) x (Const(64|32|16|8) <t> [0]))
534(Leq(64|32|16|8) (Const(64|32|16|8) <t> [1]) x) => (Less(64|32|16|8) (Const(64|32|16|8) <t> [0]) x)
535(Less(64|32|16|8) (Const(64|32|16|8) <t> [-1]) x) => (Leq(64|32|16|8) (Const(64|32|16|8) <t> [0]) x)
536
537// constant floating point comparisons
538(Eq32F   (Const32F [c]) (Const32F [d])) => (ConstBool [c == d])
539(Eq64F   (Const64F [c]) (Const64F [d])) => (ConstBool [c == d])
540(Neq32F  (Const32F [c]) (Const32F [d])) => (ConstBool [c != d])
541(Neq64F  (Const64F [c]) (Const64F [d])) => (ConstBool [c != d])
542(Less32F (Const32F [c]) (Const32F [d])) => (ConstBool [c < d])
543(Less64F (Const64F [c]) (Const64F [d])) => (ConstBool [c < d])
544(Leq32F  (Const32F [c]) (Const32F [d])) => (ConstBool [c <= d])
545(Leq64F  (Const64F [c]) (Const64F [d])) => (ConstBool [c <= d])
546
547// simplifications
548(Or(64|32|16|8) x x) => x
549(Or(64|32|16|8) (Const(64|32|16|8)  [0]) x) => x
550(Or(64|32|16|8) (Const(64|32|16|8) [-1]) _) => (Const(64|32|16|8) [-1])
551(Or(64|32|16|8) (Com(64|32|16|8)     x)  x) => (Const(64|32|16|8) [-1])
552
553(And(64|32|16|8) x x) => x
554(And(64|32|16|8) (Const(64|32|16|8) [-1]) x) => x
555(And(64|32|16|8) (Const(64|32|16|8)  [0]) _) => (Const(64|32|16|8) [0])
556(And(64|32|16|8) (Com(64|32|16|8)     x)  x) => (Const(64|32|16|8) [0])
557
558(Xor(64|32|16|8) x x) => (Const(64|32|16|8) [0])
559(Xor(64|32|16|8) (Const(64|32|16|8) [0]) x) => x
560(Xor(64|32|16|8) (Com(64|32|16|8)    x)  x) => (Const(64|32|16|8) [-1])
561
562(Add(64|32|16|8) (Const(64|32|16|8) [0]) x) => x
563(Sub(64|32|16|8) x x) => (Const(64|32|16|8) [0])
564(Mul(64|32|16|8) (Const(64|32|16|8) [0]) _) => (Const(64|32|16|8) [0])
565(Select0 (Mul(64|32)uover (Const(64|32) [0]) x)) => (Const(64|32) [0])
566(Select1 (Mul(64|32)uover (Const(64|32) [0]) x)) => (ConstBool [false])
567
568(Com(64|32|16|8) (Com(64|32|16|8)  x)) => x
569(Com(64|32|16|8) (Const(64|32|16|8) [c])) => (Const(64|32|16|8) [^c])
570
571(Neg(64|32|16|8) (Sub(64|32|16|8) x y)) => (Sub(64|32|16|8) y x)
572(Add(64|32|16|8) x (Neg(64|32|16|8) y)) => (Sub(64|32|16|8) x y)
573
574(Xor(64|32|16|8) (Const(64|32|16|8) [-1]) x) => (Com(64|32|16|8) x)
575
576(Sub(64|32|16|8) (Neg(64|32|16|8) x) (Com(64|32|16|8) x)) => (Const(64|32|16|8) [1])
577(Sub(64|32|16|8) (Com(64|32|16|8) x) (Neg(64|32|16|8) x)) => (Const(64|32|16|8) [-1])
578(Add(64|32|16|8) (Com(64|32|16|8) x)                  x)  => (Const(64|32|16|8) [-1])
579
580// Simplification when involving common integer
581// (t + x) - (t + y) == x - y
582// (t + x) - (y + t) == x - y
583// (x + t) - (y + t) == x - y
584// (x + t) - (t + y) == x - y
585// (x - t) + (t + y) == x + y
586// (x - t) + (y + t) == x + y
587(Sub(64|32|16|8) (Add(64|32|16|8) t x) (Add(64|32|16|8) t y)) => (Sub(64|32|16|8) x y)
588(Add(64|32|16|8) (Sub(64|32|16|8) x t) (Add(64|32|16|8) t y)) => (Add(64|32|16|8) x y)
589
590// ^(x-1) == ^x+1 == -x
591(Add(64|32|16|8) (Const(64|32|16|8) [1]) (Com(64|32|16|8) x)) => (Neg(64|32|16|8) x)
592(Com(64|32|16|8) (Add(64|32|16|8) (Const(64|32|16|8) [-1]) x)) => (Neg(64|32|16|8) x)
593
594// -(-x) == x
595(Neg(64|32|16|8) (Neg(64|32|16|8) x)) => x
596
597// -^x == x+1
598(Neg(64|32|16|8) <t> (Com(64|32|16|8) x)) => (Add(64|32|16|8) (Const(64|32|16|8) <t> [1]) x)
599
600(And(64|32|16|8) x (And(64|32|16|8) x y)) => (And(64|32|16|8) x y)
601(Or(64|32|16|8) x (Or(64|32|16|8) x y)) => (Or(64|32|16|8) x y)
602(Xor(64|32|16|8) x (Xor(64|32|16|8) x y)) => y
603
604// Fold comparisons with numeric bounds
605(Less(64|32|16|8)U _ (Const(64|32|16|8) [0]))  => (ConstBool [false])
606(Leq(64|32|16|8)U (Const(64|32|16|8) [0]) _)   => (ConstBool [true])
607(Less(64|32|16|8)U (Const(64|32|16|8) [-1]) _) => (ConstBool [false])
608(Leq(64|32|16|8)U _ (Const(64|32|16|8) [-1]))  => (ConstBool [true])
609(Less64 _ (Const64 [math.MinInt64])) => (ConstBool [false])
610(Less32 _ (Const32 [math.MinInt32])) => (ConstBool [false])
611(Less16 _ (Const16 [math.MinInt16])) => (ConstBool [false])
612(Less8  _ (Const8  [math.MinInt8 ])) => (ConstBool [false])
613(Leq64 (Const64 [math.MinInt64]) _)  => (ConstBool [true])
614(Leq32 (Const32 [math.MinInt32]) _)  => (ConstBool [true])
615(Leq16 (Const16 [math.MinInt16]) _)  => (ConstBool [true])
616(Leq8  (Const8  [math.MinInt8 ]) _)  => (ConstBool [true])
617(Less64 (Const64 [math.MaxInt64]) _) => (ConstBool [false])
618(Less32 (Const32 [math.MaxInt32]) _) => (ConstBool [false])
619(Less16 (Const16 [math.MaxInt16]) _) => (ConstBool [false])
620(Less8  (Const8  [math.MaxInt8 ]) _) => (ConstBool [false])
621(Leq64 _ (Const64 [math.MaxInt64]))  => (ConstBool [true])
622(Leq32 _ (Const32 [math.MaxInt32]))  => (ConstBool [true])
623(Leq16 _ (Const16 [math.MaxInt16]))  => (ConstBool [true])
624(Leq8  _ (Const8  [math.MaxInt8 ]))  => (ConstBool [true])
625
626// Canonicalize <= on numeric bounds and < near numeric bounds to ==
627(Leq(64|32|16|8)U x c:(Const(64|32|16|8) [0]))     => (Eq(64|32|16|8) x c)
628(Leq(64|32|16|8)U c:(Const(64|32|16|8) [-1]) x)    => (Eq(64|32|16|8) x c)
629(Less(64|32|16|8)U x (Const(64|32|16|8) <t> [1]))  => (Eq(64|32|16|8) x (Const(64|32|16|8) <t> [0]))
630(Less(64|32|16|8)U (Const(64|32|16|8) <t> [-2]) x) => (Eq(64|32|16|8) x (Const(64|32|16|8) <t> [-1]))
631(Leq64 x c:(Const64 [math.MinInt64])) => (Eq64 x c)
632(Leq32 x c:(Const32 [math.MinInt32])) => (Eq32 x c)
633(Leq16 x c:(Const16 [math.MinInt16])) => (Eq16 x c)
634(Leq8  x c:(Const8  [math.MinInt8 ])) => (Eq8  x c)
635(Leq64 c:(Const64 [math.MaxInt64]) x) => (Eq64 x c)
636(Leq32 c:(Const32 [math.MaxInt32]) x) => (Eq32 x c)
637(Leq16 c:(Const16 [math.MaxInt16]) x) => (Eq16 x c)
638(Leq8  c:(Const8  [math.MaxInt8 ]) x) => (Eq8  x c)
639(Less64 x (Const64 <t> [math.MinInt64+1])) => (Eq64 x (Const64 <t> [math.MinInt64]))
640(Less32 x (Const32 <t> [math.MinInt32+1])) => (Eq32 x (Const32 <t> [math.MinInt32]))
641(Less16 x (Const16 <t> [math.MinInt16+1])) => (Eq16 x (Const16 <t> [math.MinInt16]))
642(Less8  x (Const8  <t> [math.MinInt8 +1])) => (Eq8  x (Const8  <t> [math.MinInt8 ]))
643(Less64 (Const64 <t> [math.MaxInt64-1]) x) => (Eq64 x (Const64 <t> [math.MaxInt64]))
644(Less32 (Const32 <t> [math.MaxInt32-1]) x) => (Eq32 x (Const32 <t> [math.MaxInt32]))
645(Less16 (Const16 <t> [math.MaxInt16-1]) x) => (Eq16 x (Const16 <t> [math.MaxInt16]))
646(Less8  (Const8  <t> [math.MaxInt8 -1]) x) => (Eq8  x (Const8  <t> [math.MaxInt8 ]))
647
648// Ands clear bits. Ors set bits.
649// If a subsequent Or will set all the bits
650// that an And cleared, we can skip the And.
651// This happens in bitmasking code like:
652//   x &^= 3 << shift // clear two old bits
653//   x  |= v << shift // set two new bits
654// when shift is a small constant and v ends up a constant 3.
655(Or8  (And8  x (Const8  [c2])) (Const8  <t> [c1])) && ^(c1 | c2) == 0 => (Or8  (Const8  <t> [c1]) x)
656(Or16 (And16 x (Const16 [c2])) (Const16 <t> [c1])) && ^(c1 | c2) == 0 => (Or16 (Const16 <t> [c1]) x)
657(Or32 (And32 x (Const32 [c2])) (Const32 <t> [c1])) && ^(c1 | c2) == 0 => (Or32 (Const32 <t> [c1]) x)
658(Or64 (And64 x (Const64 [c2])) (Const64 <t> [c1])) && ^(c1 | c2) == 0 => (Or64 (Const64 <t> [c1]) x)
659
660(Trunc64to8  (And64 (Const64 [y]) x)) && y&0xFF == 0xFF => (Trunc64to8 x)
661(Trunc64to16 (And64 (Const64 [y]) x)) && y&0xFFFF == 0xFFFF => (Trunc64to16 x)
662(Trunc64to32 (And64 (Const64 [y]) x)) && y&0xFFFFFFFF == 0xFFFFFFFF => (Trunc64to32 x)
663(Trunc32to8  (And32 (Const32 [y]) x)) && y&0xFF == 0xFF => (Trunc32to8 x)
664(Trunc32to16 (And32 (Const32 [y]) x)) && y&0xFFFF == 0xFFFF => (Trunc32to16 x)
665(Trunc16to8  (And16 (Const16 [y]) x)) && y&0xFF == 0xFF => (Trunc16to8 x)
666
667(ZeroExt8to64  (Trunc64to8  x:(Rsh64Ux64 _ (Const64 [s])))) && s >= 56 => x
668(ZeroExt16to64 (Trunc64to16 x:(Rsh64Ux64 _ (Const64 [s])))) && s >= 48 => x
669(ZeroExt32to64 (Trunc64to32 x:(Rsh64Ux64 _ (Const64 [s])))) && s >= 32 => x
670(ZeroExt8to32  (Trunc32to8  x:(Rsh32Ux64 _ (Const64 [s])))) && s >= 24 => x
671(ZeroExt16to32 (Trunc32to16 x:(Rsh32Ux64 _ (Const64 [s])))) && s >= 16 => x
672(ZeroExt8to16  (Trunc16to8  x:(Rsh16Ux64 _ (Const64 [s])))) && s >= 8 => x
673
674(SignExt8to64  (Trunc64to8  x:(Rsh64x64 _ (Const64 [s])))) && s >= 56 => x
675(SignExt16to64 (Trunc64to16 x:(Rsh64x64 _ (Const64 [s])))) && s >= 48 => x
676(SignExt32to64 (Trunc64to32 x:(Rsh64x64 _ (Const64 [s])))) && s >= 32 => x
677(SignExt8to32  (Trunc32to8  x:(Rsh32x64 _ (Const64 [s])))) && s >= 24 => x
678(SignExt16to32 (Trunc32to16 x:(Rsh32x64 _ (Const64 [s])))) && s >= 16 => x
679(SignExt8to16  (Trunc16to8  x:(Rsh16x64 _ (Const64 [s])))) && s >= 8 => x
680
681(Slicemask (Const32 [x])) && x > 0 => (Const32 [-1])
682(Slicemask (Const32 [0]))          => (Const32 [0])
683(Slicemask (Const64 [x])) && x > 0 => (Const64 [-1])
684(Slicemask (Const64 [0]))          => (Const64 [0])
685
686// simplifications often used for lengths.  e.g. len(s[i:i+5])==5
687(Sub(64|32|16|8) (Add(64|32|16|8) x y) x) => y
688(Sub(64|32|16|8) (Add(64|32|16|8) x y) y) => x
689(Sub(64|32|16|8) (Sub(64|32|16|8) x y) x) => (Neg(64|32|16|8) y)
690(Sub(64|32|16|8) x (Add(64|32|16|8) x y)) => (Neg(64|32|16|8) y)
691(Add(64|32|16|8) x (Sub(64|32|16|8) y x)) => y
692(Add(64|32|16|8) x (Add(64|32|16|8) y (Sub(64|32|16|8) z x))) => (Add(64|32|16|8) y z)
693
694// basic phi simplifications
695(Phi (Const8  [c]) (Const8  [c])) => (Const8  [c])
696(Phi (Const16 [c]) (Const16 [c])) => (Const16 [c])
697(Phi (Const32 [c]) (Const32 [c])) => (Const32 [c])
698(Phi (Const64 [c]) (Const64 [c])) => (Const64 [c])
699
700// slice and interface comparisons
701// The frontend ensures that we can only compare against nil,
702// so we need only compare the first word (interface type or slice ptr).
703(EqInter x y)  => (EqPtr  (ITab x) (ITab y))
704(NeqInter x y) => (NeqPtr (ITab x) (ITab y))
705(EqSlice x y)  => (EqPtr  (SlicePtr x) (SlicePtr y))
706(NeqSlice x y) => (NeqPtr (SlicePtr x) (SlicePtr y))
707
708// Load of store of same address, with compatibly typed value and same size
709(Load <t1> p1 (Store {t2} p2 x _))
710	&& isSamePtr(p1, p2)
711	&& t1.Compare(x.Type) == types.CMPeq
712	&& t1.Size() == t2.Size()
713	=> x
714(Load <t1> p1 (Store {t2} p2 _ (Store {t3} p3 x _)))
715	&& isSamePtr(p1, p3)
716	&& t1.Compare(x.Type) == types.CMPeq
717	&& t1.Size() == t2.Size()
718	&& disjoint(p3, t3.Size(), p2, t2.Size())
719	=> x
720(Load <t1> p1 (Store {t2} p2 _ (Store {t3} p3 _ (Store {t4} p4 x _))))
721	&& isSamePtr(p1, p4)
722	&& t1.Compare(x.Type) == types.CMPeq
723	&& t1.Size() == t2.Size()
724	&& disjoint(p4, t4.Size(), p2, t2.Size())
725	&& disjoint(p4, t4.Size(), p3, t3.Size())
726	=> x
727(Load <t1> p1 (Store {t2} p2 _ (Store {t3} p3 _ (Store {t4} p4 _ (Store {t5} p5 x _)))))
728	&& isSamePtr(p1, p5)
729	&& t1.Compare(x.Type) == types.CMPeq
730	&& t1.Size() == t2.Size()
731	&& disjoint(p5, t5.Size(), p2, t2.Size())
732	&& disjoint(p5, t5.Size(), p3, t3.Size())
733	&& disjoint(p5, t5.Size(), p4, t4.Size())
734	=> x
735
736// Pass constants through math.Float{32,64}bits and math.Float{32,64}frombits
737(Load <t1> p1 (Store {t2} p2 (Const64  [x]) _)) && isSamePtr(p1,p2) && sizeof(t2) == 8 && is64BitFloat(t1) && !math.IsNaN(math.Float64frombits(uint64(x))) => (Const64F [math.Float64frombits(uint64(x))])
738(Load <t1> p1 (Store {t2} p2 (Const32  [x]) _)) && isSamePtr(p1,p2) && sizeof(t2) == 4 && is32BitFloat(t1) && !math.IsNaN(float64(math.Float32frombits(uint32(x)))) => (Const32F [math.Float32frombits(uint32(x))])
739(Load <t1> p1 (Store {t2} p2 (Const64F [x]) _)) && isSamePtr(p1,p2) && sizeof(t2) == 8 && is64BitInt(t1)   => (Const64  [int64(math.Float64bits(x))])
740(Load <t1> p1 (Store {t2} p2 (Const32F [x]) _)) && isSamePtr(p1,p2) && sizeof(t2) == 4 && is32BitInt(t1)   => (Const32  [int32(math.Float32bits(x))])
741
742// Float Loads up to Zeros so they can be constant folded.
743(Load <t1> op:(OffPtr [o1] p1)
744	(Store {t2} p2 _
745		mem:(Zero [n] p3 _)))
746	&& o1 >= 0 && o1+t1.Size() <= n && isSamePtr(p1, p3)
747	&& CanSSA(t1)
748	&& disjoint(op, t1.Size(), p2, t2.Size())
749	=> @mem.Block (Load <t1> (OffPtr <op.Type> [o1] p3) mem)
750(Load <t1> op:(OffPtr [o1] p1)
751	(Store {t2} p2 _
752		(Store {t3} p3 _
753			mem:(Zero [n] p4 _))))
754	&& o1 >= 0 && o1+t1.Size() <= n && isSamePtr(p1, p4)
755	&& CanSSA(t1)
756	&& disjoint(op, t1.Size(), p2, t2.Size())
757	&& disjoint(op, t1.Size(), p3, t3.Size())
758	=> @mem.Block (Load <t1> (OffPtr <op.Type> [o1] p4) mem)
759(Load <t1> op:(OffPtr [o1] p1)
760	(Store {t2} p2 _
761		(Store {t3} p3 _
762			(Store {t4} p4 _
763				mem:(Zero [n] p5 _)))))
764	&& o1 >= 0 && o1+t1.Size() <= n && isSamePtr(p1, p5)
765	&& CanSSA(t1)
766	&& disjoint(op, t1.Size(), p2, t2.Size())
767	&& disjoint(op, t1.Size(), p3, t3.Size())
768	&& disjoint(op, t1.Size(), p4, t4.Size())
769	=> @mem.Block (Load <t1> (OffPtr <op.Type> [o1] p5) mem)
770(Load <t1> op:(OffPtr [o1] p1)
771	(Store {t2} p2 _
772		(Store {t3} p3 _
773			(Store {t4} p4 _
774				(Store {t5} p5 _
775					mem:(Zero [n] p6 _))))))
776	&& o1 >= 0 && o1+t1.Size() <= n && isSamePtr(p1, p6)
777	&& CanSSA(t1)
778	&& disjoint(op, t1.Size(), p2, t2.Size())
779	&& disjoint(op, t1.Size(), p3, t3.Size())
780	&& disjoint(op, t1.Size(), p4, t4.Size())
781	&& disjoint(op, t1.Size(), p5, t5.Size())
782	=> @mem.Block (Load <t1> (OffPtr <op.Type> [o1] p6) mem)
783
784// Zero to Load forwarding.
785(Load <t1> (OffPtr [o] p1) (Zero [n] p2 _))
786	&& t1.IsBoolean()
787	&& isSamePtr(p1, p2)
788	&& n >= o + 1
789	=> (ConstBool [false])
790(Load <t1> (OffPtr [o] p1) (Zero [n] p2 _))
791	&& is8BitInt(t1)
792	&& isSamePtr(p1, p2)
793	&& n >= o + 1
794	=> (Const8 [0])
795(Load <t1> (OffPtr [o] p1) (Zero [n] p2 _))
796	&& is16BitInt(t1)
797	&& isSamePtr(p1, p2)
798	&& n >= o + 2
799	=> (Const16 [0])
800(Load <t1> (OffPtr [o] p1) (Zero [n] p2 _))
801	&& is32BitInt(t1)
802	&& isSamePtr(p1, p2)
803	&& n >= o + 4
804	=> (Const32 [0])
805(Load <t1> (OffPtr [o] p1) (Zero [n] p2 _))
806	&& is64BitInt(t1)
807	&& isSamePtr(p1, p2)
808	&& n >= o + 8
809	=> (Const64 [0])
810(Load <t1> (OffPtr [o] p1) (Zero [n] p2 _))
811	&& is32BitFloat(t1)
812	&& isSamePtr(p1, p2)
813	&& n >= o + 4
814	=> (Const32F [0])
815(Load <t1> (OffPtr [o] p1) (Zero [n] p2 _))
816	&& is64BitFloat(t1)
817	&& isSamePtr(p1, p2)
818	&& n >= o + 8
819	=> (Const64F [0])
820
821// Eliminate stores of values that have just been loaded from the same location.
822// We also handle the common case where there are some intermediate stores.
823(Store {t1} p1 (Load <t2> p2 mem) mem)
824	&& isSamePtr(p1, p2)
825	&& t2.Size() == t1.Size()
826	=> mem
827(Store {t1} p1 (Load <t2> p2 oldmem) mem:(Store {t3} p3 _ oldmem))
828	&& isSamePtr(p1, p2)
829	&& t2.Size() == t1.Size()
830	&& disjoint(p1, t1.Size(), p3, t3.Size())
831	=> mem
832(Store {t1} p1 (Load <t2> p2 oldmem) mem:(Store {t3} p3 _ (Store {t4} p4 _ oldmem)))
833	&& isSamePtr(p1, p2)
834	&& t2.Size() == t1.Size()
835	&& disjoint(p1, t1.Size(), p3, t3.Size())
836	&& disjoint(p1, t1.Size(), p4, t4.Size())
837	=> mem
838(Store {t1} p1 (Load <t2> p2 oldmem) mem:(Store {t3} p3 _ (Store {t4} p4 _ (Store {t5} p5 _ oldmem))))
839	&& isSamePtr(p1, p2)
840	&& t2.Size() == t1.Size()
841	&& disjoint(p1, t1.Size(), p3, t3.Size())
842	&& disjoint(p1, t1.Size(), p4, t4.Size())
843	&& disjoint(p1, t1.Size(), p5, t5.Size())
844	=> mem
845
846// Don't Store zeros to cleared variables.
847(Store {t} (OffPtr [o] p1) x mem:(Zero [n] p2 _))
848	&& isConstZero(x)
849	&& o >= 0 && t.Size() + o <= n && isSamePtr(p1, p2)
850	=> mem
851(Store {t1} op:(OffPtr [o1] p1) x mem:(Store {t2} p2 _ (Zero [n] p3 _)))
852	&& isConstZero(x)
853	&& o1 >= 0 && t1.Size() + o1 <= n && isSamePtr(p1, p3)
854	&& disjoint(op, t1.Size(), p2, t2.Size())
855	=> mem
856(Store {t1} op:(OffPtr [o1] p1) x mem:(Store {t2} p2 _ (Store {t3} p3 _ (Zero [n] p4 _))))
857	&& isConstZero(x)
858	&& o1 >= 0 && t1.Size() + o1 <= n && isSamePtr(p1, p4)
859	&& disjoint(op, t1.Size(), p2, t2.Size())
860	&& disjoint(op, t1.Size(), p3, t3.Size())
861	=> mem
862(Store {t1} op:(OffPtr [o1] p1) x mem:(Store {t2} p2 _ (Store {t3} p3 _ (Store {t4} p4 _ (Zero [n] p5 _)))))
863	&& isConstZero(x)
864	&& o1 >= 0 && t1.Size() + o1 <= n && isSamePtr(p1, p5)
865	&& disjoint(op, t1.Size(), p2, t2.Size())
866	&& disjoint(op, t1.Size(), p3, t3.Size())
867	&& disjoint(op, t1.Size(), p4, t4.Size())
868	=> mem
869
870// Collapse OffPtr
871(OffPtr (OffPtr p [y]) [x]) => (OffPtr p [x+y])
872(OffPtr p [0]) && v.Type.Compare(p.Type) == types.CMPeq => p
873
874// indexing operations
875// Note: bounds check has already been done
876(PtrIndex <t> ptr idx) && config.PtrSize == 4 && is32Bit(t.Elem().Size()) => (AddPtr ptr (Mul32 <typ.Int> idx (Const32 <typ.Int> [int32(t.Elem().Size())])))
877(PtrIndex <t> ptr idx) && config.PtrSize == 8 => (AddPtr ptr (Mul64 <typ.Int> idx (Const64 <typ.Int> [t.Elem().Size()])))
878
879// struct operations
880(StructSelect (StructMake1 x)) => x
881(StructSelect [0] (StructMake2 x _)) => x
882(StructSelect [1] (StructMake2 _ x)) => x
883(StructSelect [0] (StructMake3 x _ _)) => x
884(StructSelect [1] (StructMake3 _ x _)) => x
885(StructSelect [2] (StructMake3 _ _ x)) => x
886(StructSelect [0] (StructMake4 x _ _ _)) => x
887(StructSelect [1] (StructMake4 _ x _ _)) => x
888(StructSelect [2] (StructMake4 _ _ x _)) => x
889(StructSelect [3] (StructMake4 _ _ _ x)) => x
890
891(Load <t> _ _) && t.IsStruct() && t.NumFields() == 0 && CanSSA(t) =>
892  (StructMake0)
893(Load <t> ptr mem) && t.IsStruct() && t.NumFields() == 1 && CanSSA(t) =>
894  (StructMake1
895    (Load <t.FieldType(0)> (OffPtr <t.FieldType(0).PtrTo()> [0] ptr) mem))
896(Load <t> ptr mem) && t.IsStruct() && t.NumFields() == 2 && CanSSA(t) =>
897  (StructMake2
898    (Load <t.FieldType(0)> (OffPtr <t.FieldType(0).PtrTo()> [0]             ptr) mem)
899    (Load <t.FieldType(1)> (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] ptr) mem))
900(Load <t> ptr mem) && t.IsStruct() && t.NumFields() == 3 && CanSSA(t) =>
901  (StructMake3
902    (Load <t.FieldType(0)> (OffPtr <t.FieldType(0).PtrTo()> [0]             ptr) mem)
903    (Load <t.FieldType(1)> (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] ptr) mem)
904    (Load <t.FieldType(2)> (OffPtr <t.FieldType(2).PtrTo()> [t.FieldOff(2)] ptr) mem))
905(Load <t> ptr mem) && t.IsStruct() && t.NumFields() == 4 && CanSSA(t) =>
906  (StructMake4
907    (Load <t.FieldType(0)> (OffPtr <t.FieldType(0).PtrTo()> [0]             ptr) mem)
908    (Load <t.FieldType(1)> (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] ptr) mem)
909    (Load <t.FieldType(2)> (OffPtr <t.FieldType(2).PtrTo()> [t.FieldOff(2)] ptr) mem)
910    (Load <t.FieldType(3)> (OffPtr <t.FieldType(3).PtrTo()> [t.FieldOff(3)] ptr) mem))
911
912(StructSelect [i] x:(Load <t> ptr mem)) && !CanSSA(t) =>
913  @x.Block (Load <v.Type> (OffPtr <v.Type.PtrTo()> [t.FieldOff(int(i))] ptr) mem)
914
915(Store _ (StructMake0) mem) => mem
916(Store dst (StructMake1 <t> f0) mem) =>
917  (Store {t.FieldType(0)} (OffPtr <t.FieldType(0).PtrTo()> [0] dst) f0 mem)
918(Store dst (StructMake2 <t> f0 f1) mem) =>
919  (Store {t.FieldType(1)}
920    (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] dst)
921    f1
922    (Store {t.FieldType(0)}
923      (OffPtr <t.FieldType(0).PtrTo()> [0] dst)
924        f0 mem))
925(Store dst (StructMake3 <t> f0 f1 f2) mem) =>
926  (Store {t.FieldType(2)}
927    (OffPtr <t.FieldType(2).PtrTo()> [t.FieldOff(2)] dst)
928    f2
929    (Store {t.FieldType(1)}
930      (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] dst)
931      f1
932      (Store {t.FieldType(0)}
933        (OffPtr <t.FieldType(0).PtrTo()> [0] dst)
934          f0 mem)))
935(Store dst (StructMake4 <t> f0 f1 f2 f3) mem) =>
936  (Store {t.FieldType(3)}
937    (OffPtr <t.FieldType(3).PtrTo()> [t.FieldOff(3)] dst)
938    f3
939    (Store {t.FieldType(2)}
940      (OffPtr <t.FieldType(2).PtrTo()> [t.FieldOff(2)] dst)
941      f2
942      (Store {t.FieldType(1)}
943        (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] dst)
944        f1
945        (Store {t.FieldType(0)}
946          (OffPtr <t.FieldType(0).PtrTo()> [0] dst)
947            f0 mem))))
948
949// Putting struct{*byte} and similar into direct interfaces.
950(IMake _typ (StructMake1 val)) => (IMake _typ val)
951(StructSelect [0] (IData x)) => (IData x)
952
953// un-SSAable values use mem->mem copies
954(Store {t} dst (Load src mem) mem) && !CanSSA(t) =>
955	(Move {t} [t.Size()] dst src mem)
956(Store {t} dst (Load src mem) (VarDef {x} mem)) && !CanSSA(t) =>
957	(Move {t} [t.Size()] dst src (VarDef {x} mem))
958
959// array ops
960(ArraySelect (ArrayMake1 x)) => x
961
962(Load <t> _ _) && t.IsArray() && t.NumElem() == 0 =>
963  (ArrayMake0)
964
965(Load <t> ptr mem) && t.IsArray() && t.NumElem() == 1 && CanSSA(t) =>
966  (ArrayMake1 (Load <t.Elem()> ptr mem))
967
968(Store _ (ArrayMake0) mem) => mem
969(Store dst (ArrayMake1 e) mem) => (Store {e.Type} dst e mem)
970
971// Putting [1]*byte and similar into direct interfaces.
972(IMake _typ (ArrayMake1 val)) => (IMake _typ val)
973(ArraySelect [0] (IData x)) => (IData x)
974
975// string ops
976// Decomposing StringMake and lowering of StringPtr and StringLen
977// happens in a later pass, dec, so that these operations are available
978// to other passes for optimizations.
979(StringPtr (StringMake (Addr <t> {s} base) _)) => (Addr <t> {s} base)
980(StringLen (StringMake _ (Const64 <t> [c]))) => (Const64 <t> [c])
981(ConstString {str}) && config.PtrSize == 4 && str == "" =>
982  (StringMake (ConstNil) (Const32 <typ.Int> [0]))
983(ConstString {str}) && config.PtrSize == 8 && str == "" =>
984  (StringMake (ConstNil) (Const64 <typ.Int> [0]))
985(ConstString {str}) && config.PtrSize == 4 && str != "" =>
986  (StringMake
987    (Addr <typ.BytePtr> {fe.StringData(str)}
988      (SB))
989    (Const32 <typ.Int> [int32(len(str))]))
990(ConstString {str}) && config.PtrSize == 8 && str != "" =>
991  (StringMake
992    (Addr <typ.BytePtr> {fe.StringData(str)}
993      (SB))
994    (Const64 <typ.Int> [int64(len(str))]))
995
996// slice ops
997// Only a few slice rules are provided here.  See dec.rules for
998// a more comprehensive set.
999(SliceLen (SliceMake _ (Const64 <t> [c]) _)) => (Const64 <t> [c])
1000(SliceCap (SliceMake _ _ (Const64 <t> [c]))) => (Const64 <t> [c])
1001(SliceLen (SliceMake _ (Const32 <t> [c]) _)) => (Const32 <t> [c])
1002(SliceCap (SliceMake _ _ (Const32 <t> [c]))) => (Const32 <t> [c])
1003(SlicePtr (SliceMake (SlicePtr x) _ _)) => (SlicePtr x)
1004(SliceLen (SliceMake _ (SliceLen x) _)) => (SliceLen x)
1005(SliceCap (SliceMake _ _ (SliceCap x))) => (SliceCap x)
1006(SliceCap (SliceMake _ _ (SliceLen x))) => (SliceLen x)
1007(ConstSlice) && config.PtrSize == 4 =>
1008  (SliceMake
1009    (ConstNil <v.Type.Elem().PtrTo()>)
1010    (Const32 <typ.Int> [0])
1011    (Const32 <typ.Int> [0]))
1012(ConstSlice) && config.PtrSize == 8 =>
1013  (SliceMake
1014    (ConstNil <v.Type.Elem().PtrTo()>)
1015    (Const64 <typ.Int> [0])
1016    (Const64 <typ.Int> [0]))
1017
1018// interface ops
1019(ConstInterface) =>
1020  (IMake
1021    (ConstNil <typ.Uintptr>)
1022    (ConstNil <typ.BytePtr>))
1023
1024(NilCheck ptr:(GetG mem) mem) => ptr
1025
1026(If (Not cond) yes no) => (If cond no yes)
1027(If (ConstBool [c]) yes no) && c => (First yes no)
1028(If (ConstBool [c]) yes no) && !c => (First no yes)
1029
1030(Phi <t> nx:(Not x) ny:(Not y)) && nx.Uses == 1 && ny.Uses == 1 => (Not (Phi <t> x y))
1031
1032// Get rid of Convert ops for pointer arithmetic on unsafe.Pointer.
1033(Convert (Add(64|32) (Convert ptr mem) off) mem) => (AddPtr ptr off)
1034(Convert (Convert ptr mem) mem) => ptr
1035
1036// strength reduction of divide by a constant.
1037// See ../magic.go for a detailed description of these algorithms.
1038
1039// Unsigned divide by power of 2.  Strength reduce to a shift.
1040(Div8u  n (Const8  [c])) && isPowerOfTwo8(c)  => (Rsh8Ux64  n (Const64 <typ.UInt64> [log8(c)]))
1041(Div16u n (Const16 [c])) && isPowerOfTwo16(c) => (Rsh16Ux64 n (Const64 <typ.UInt64> [log16(c)]))
1042(Div32u n (Const32 [c])) && isPowerOfTwo32(c) => (Rsh32Ux64 n (Const64 <typ.UInt64> [log32(c)]))
1043(Div64u n (Const64 [c])) && isPowerOfTwo64(c) => (Rsh64Ux64 n (Const64 <typ.UInt64> [log64(c)]))
1044(Div64u n (Const64 [-1<<63]))                 => (Rsh64Ux64 n (Const64 <typ.UInt64> [63]))
1045
1046// Signed non-negative divide by power of 2.
1047(Div8  n (Const8  [c])) && isNonNegative(n) && isPowerOfTwo8(c)  => (Rsh8Ux64  n (Const64 <typ.UInt64> [log8(c)]))
1048(Div16 n (Const16 [c])) && isNonNegative(n) && isPowerOfTwo16(c) => (Rsh16Ux64 n (Const64 <typ.UInt64> [log16(c)]))
1049(Div32 n (Const32 [c])) && isNonNegative(n) && isPowerOfTwo32(c) => (Rsh32Ux64 n (Const64 <typ.UInt64> [log32(c)]))
1050(Div64 n (Const64 [c])) && isNonNegative(n) && isPowerOfTwo64(c) => (Rsh64Ux64 n (Const64 <typ.UInt64> [log64(c)]))
1051(Div64 n (Const64 [-1<<63])) && isNonNegative(n)                 => (Const64 [0])
1052
1053// Unsigned divide, not a power of 2.  Strength reduce to a multiply.
1054// For 8-bit divides, we just do a direct 9-bit by 8-bit multiply.
1055(Div8u x (Const8 [c])) && umagicOK8(c) =>
1056  (Trunc32to8
1057    (Rsh32Ux64 <typ.UInt32>
1058      (Mul32 <typ.UInt32>
1059        (Const32 <typ.UInt32> [int32(1<<8+umagic8(c).m)])
1060        (ZeroExt8to32 x))
1061      (Const64 <typ.UInt64> [8+umagic8(c).s])))
1062
1063// For 16-bit divides on 64-bit machines, we do a direct 17-bit by 16-bit multiply.
1064(Div16u x (Const16 [c])) && umagicOK16(c) && config.RegSize == 8 =>
1065  (Trunc64to16
1066    (Rsh64Ux64 <typ.UInt64>
1067      (Mul64 <typ.UInt64>
1068        (Const64 <typ.UInt64> [int64(1<<16+umagic16(c).m)])
1069        (ZeroExt16to64 x))
1070      (Const64 <typ.UInt64> [16+umagic16(c).s])))
1071
1072// For 16-bit divides on 32-bit machines
1073(Div16u x (Const16 [c])) && umagicOK16(c) && config.RegSize == 4 && umagic16(c).m&1 == 0 =>
1074  (Trunc32to16
1075    (Rsh32Ux64 <typ.UInt32>
1076      (Mul32 <typ.UInt32>
1077        (Const32 <typ.UInt32> [int32(1<<15+umagic16(c).m/2)])
1078        (ZeroExt16to32 x))
1079      (Const64 <typ.UInt64> [16+umagic16(c).s-1])))
1080(Div16u x (Const16 [c])) && umagicOK16(c) && config.RegSize == 4 && c&1 == 0 =>
1081  (Trunc32to16
1082    (Rsh32Ux64 <typ.UInt32>
1083      (Mul32 <typ.UInt32>
1084        (Const32 <typ.UInt32> [int32(1<<15+(umagic16(c).m+1)/2)])
1085        (Rsh32Ux64 <typ.UInt32> (ZeroExt16to32 x) (Const64 <typ.UInt64> [1])))
1086      (Const64 <typ.UInt64> [16+umagic16(c).s-2])))
1087(Div16u x (Const16 [c])) && umagicOK16(c) && config.RegSize == 4 && config.useAvg =>
1088  (Trunc32to16
1089    (Rsh32Ux64 <typ.UInt32>
1090      (Avg32u
1091        (Lsh32x64 <typ.UInt32> (ZeroExt16to32 x) (Const64 <typ.UInt64> [16]))
1092        (Mul32 <typ.UInt32>
1093          (Const32 <typ.UInt32> [int32(umagic16(c).m)])
1094          (ZeroExt16to32 x)))
1095      (Const64 <typ.UInt64> [16+umagic16(c).s-1])))
1096
1097// For 32-bit divides on 32-bit machines
1098(Div32u x (Const32 [c])) && umagicOK32(c) && config.RegSize == 4 && umagic32(c).m&1 == 0 && config.useHmul =>
1099  (Rsh32Ux64 <typ.UInt32>
1100    (Hmul32u <typ.UInt32>
1101      (Const32 <typ.UInt32> [int32(1<<31+umagic32(c).m/2)])
1102      x)
1103    (Const64 <typ.UInt64> [umagic32(c).s-1]))
1104(Div32u x (Const32 [c])) && umagicOK32(c) && config.RegSize == 4 && c&1 == 0 && config.useHmul =>
1105  (Rsh32Ux64 <typ.UInt32>
1106    (Hmul32u <typ.UInt32>
1107      (Const32 <typ.UInt32> [int32(1<<31+(umagic32(c).m+1)/2)])
1108      (Rsh32Ux64 <typ.UInt32> x (Const64 <typ.UInt64> [1])))
1109    (Const64 <typ.UInt64> [umagic32(c).s-2]))
1110(Div32u x (Const32 [c])) && umagicOK32(c) && config.RegSize == 4 && config.useAvg && config.useHmul =>
1111  (Rsh32Ux64 <typ.UInt32>
1112    (Avg32u
1113      x
1114      (Hmul32u <typ.UInt32>
1115        (Const32 <typ.UInt32> [int32(umagic32(c).m)])
1116        x))
1117    (Const64 <typ.UInt64> [umagic32(c).s-1]))
1118
1119// For 32-bit divides on 64-bit machines
1120// We'll use a regular (non-hi) multiply for this case.
1121(Div32u x (Const32 [c])) && umagicOK32(c) && config.RegSize == 8 && umagic32(c).m&1 == 0 =>
1122  (Trunc64to32
1123    (Rsh64Ux64 <typ.UInt64>
1124      (Mul64 <typ.UInt64>
1125        (Const64 <typ.UInt64> [int64(1<<31+umagic32(c).m/2)])
1126        (ZeroExt32to64 x))
1127      (Const64 <typ.UInt64> [32+umagic32(c).s-1])))
1128(Div32u x (Const32 [c])) && umagicOK32(c) && config.RegSize == 8 && c&1 == 0 =>
1129  (Trunc64to32
1130    (Rsh64Ux64 <typ.UInt64>
1131      (Mul64 <typ.UInt64>
1132        (Const64 <typ.UInt64> [int64(1<<31+(umagic32(c).m+1)/2)])
1133        (Rsh64Ux64 <typ.UInt64> (ZeroExt32to64 x) (Const64 <typ.UInt64> [1])))
1134      (Const64 <typ.UInt64> [32+umagic32(c).s-2])))
1135(Div32u x (Const32 [c])) && umagicOK32(c) && config.RegSize == 8 && config.useAvg =>
1136  (Trunc64to32
1137    (Rsh64Ux64 <typ.UInt64>
1138      (Avg64u
1139        (Lsh64x64 <typ.UInt64> (ZeroExt32to64 x) (Const64 <typ.UInt64> [32]))
1140        (Mul64 <typ.UInt64>
1141          (Const64 <typ.UInt32> [int64(umagic32(c).m)])
1142          (ZeroExt32to64 x)))
1143      (Const64 <typ.UInt64> [32+umagic32(c).s-1])))
1144
1145// For unsigned 64-bit divides on 32-bit machines,
1146// if the constant fits in 16 bits (so that the last term
1147// fits in 32 bits), convert to three 32-bit divides by a constant.
1148//
1149// If 1<<32 = Q * c + R
1150// and    x = hi << 32 + lo
1151//
1152// Then x = (hi/c*c + hi%c) << 32 + lo
1153//        = hi/c*c<<32 + hi%c<<32 + lo
1154//        = hi/c*c<<32 + (hi%c)*(Q*c+R) + lo/c*c + lo%c
1155//        = hi/c*c<<32 + (hi%c)*Q*c + lo/c*c + (hi%c*R+lo%c)
1156// and x / c = (hi/c)<<32 + (hi%c)*Q + lo/c + (hi%c*R+lo%c)/c
1157(Div64u x (Const64 [c])) && c > 0 && c <= 0xFFFF && umagicOK32(int32(c)) && config.RegSize == 4 && config.useHmul =>
1158  (Add64
1159    (Add64 <typ.UInt64>
1160      (Add64 <typ.UInt64>
1161        (Lsh64x64 <typ.UInt64>
1162          (ZeroExt32to64
1163            (Div32u <typ.UInt32>
1164              (Trunc64to32 <typ.UInt32> (Rsh64Ux64 <typ.UInt64> x (Const64 <typ.UInt64> [32])))
1165              (Const32 <typ.UInt32> [int32(c)])))
1166          (Const64 <typ.UInt64> [32]))
1167        (ZeroExt32to64 (Div32u <typ.UInt32> (Trunc64to32 <typ.UInt32> x) (Const32 <typ.UInt32> [int32(c)]))))
1168      (Mul64 <typ.UInt64>
1169        (ZeroExt32to64 <typ.UInt64>
1170          (Mod32u <typ.UInt32>
1171            (Trunc64to32 <typ.UInt32> (Rsh64Ux64 <typ.UInt64> x (Const64 <typ.UInt64> [32])))
1172            (Const32 <typ.UInt32> [int32(c)])))
1173        (Const64 <typ.UInt64> [int64((1<<32)/c)])))
1174      (ZeroExt32to64
1175        (Div32u <typ.UInt32>
1176          (Add32 <typ.UInt32>
1177            (Mod32u <typ.UInt32> (Trunc64to32 <typ.UInt32> x) (Const32 <typ.UInt32> [int32(c)]))
1178            (Mul32 <typ.UInt32>
1179              (Mod32u <typ.UInt32>
1180                (Trunc64to32 <typ.UInt32> (Rsh64Ux64 <typ.UInt64> x (Const64 <typ.UInt64> [32])))
1181                (Const32 <typ.UInt32> [int32(c)]))
1182              (Const32 <typ.UInt32> [int32((1<<32)%c)])))
1183          (Const32 <typ.UInt32> [int32(c)]))))
1184
1185// For 64-bit divides on 64-bit machines
1186// (64-bit divides on 32-bit machines are lowered to a runtime call by the walk pass.)
1187(Div64u x (Const64 [c])) && umagicOK64(c) && config.RegSize == 8 && umagic64(c).m&1 == 0 && config.useHmul =>
1188  (Rsh64Ux64 <typ.UInt64>
1189    (Hmul64u <typ.UInt64>
1190      (Const64 <typ.UInt64> [int64(1<<63+umagic64(c).m/2)])
1191      x)
1192    (Const64 <typ.UInt64> [umagic64(c).s-1]))
1193(Div64u x (Const64 [c])) && umagicOK64(c) && config.RegSize == 8 && c&1 == 0 && config.useHmul =>
1194  (Rsh64Ux64 <typ.UInt64>
1195    (Hmul64u <typ.UInt64>
1196      (Const64 <typ.UInt64> [int64(1<<63+(umagic64(c).m+1)/2)])
1197      (Rsh64Ux64 <typ.UInt64> x (Const64 <typ.UInt64> [1])))
1198    (Const64 <typ.UInt64> [umagic64(c).s-2]))
1199(Div64u x (Const64 [c])) && umagicOK64(c) && config.RegSize == 8 && config.useAvg && config.useHmul =>
1200  (Rsh64Ux64 <typ.UInt64>
1201    (Avg64u
1202      x
1203      (Hmul64u <typ.UInt64>
1204        (Const64 <typ.UInt64> [int64(umagic64(c).m)])
1205        x))
1206    (Const64 <typ.UInt64> [umagic64(c).s-1]))
1207
1208// Signed divide by a negative constant.  Rewrite to divide by a positive constant.
1209(Div8  <t> n (Const8  [c])) && c < 0 && c != -1<<7  => (Neg8  (Div8  <t> n (Const8  <t> [-c])))
1210(Div16 <t> n (Const16 [c])) && c < 0 && c != -1<<15 => (Neg16 (Div16 <t> n (Const16 <t> [-c])))
1211(Div32 <t> n (Const32 [c])) && c < 0 && c != -1<<31 => (Neg32 (Div32 <t> n (Const32 <t> [-c])))
1212(Div64 <t> n (Const64 [c])) && c < 0 && c != -1<<63 => (Neg64 (Div64 <t> n (Const64 <t> [-c])))
1213
1214// Dividing by the most-negative number.  Result is always 0 except
1215// if the input is also the most-negative number.
1216// We can detect that using the sign bit of x & -x.
1217(Div8  <t> x (Const8  [-1<<7 ])) => (Rsh8Ux64  (And8  <t> x (Neg8  <t> x)) (Const64 <typ.UInt64> [7 ]))
1218(Div16 <t> x (Const16 [-1<<15])) => (Rsh16Ux64 (And16 <t> x (Neg16 <t> x)) (Const64 <typ.UInt64> [15]))
1219(Div32 <t> x (Const32 [-1<<31])) => (Rsh32Ux64 (And32 <t> x (Neg32 <t> x)) (Const64 <typ.UInt64> [31]))
1220(Div64 <t> x (Const64 [-1<<63])) => (Rsh64Ux64 (And64 <t> x (Neg64 <t> x)) (Const64 <typ.UInt64> [63]))
1221
1222// Signed divide by power of 2.
1223// n / c =       n >> log(c) if n >= 0
1224//       = (n+c-1) >> log(c) if n < 0
1225// We conditionally add c-1 by adding n>>63>>(64-log(c)) (first shift signed, second shift unsigned).
1226(Div8  <t> n (Const8  [c])) && isPowerOfTwo8(c) =>
1227  (Rsh8x64
1228    (Add8  <t> n (Rsh8Ux64  <t> (Rsh8x64  <t> n (Const64 <typ.UInt64> [ 7])) (Const64 <typ.UInt64> [int64( 8-log8(c))])))
1229    (Const64 <typ.UInt64> [int64(log8(c))]))
1230(Div16 <t> n (Const16 [c])) && isPowerOfTwo16(c) =>
1231  (Rsh16x64
1232    (Add16 <t> n (Rsh16Ux64 <t> (Rsh16x64 <t> n (Const64 <typ.UInt64> [15])) (Const64 <typ.UInt64> [int64(16-log16(c))])))
1233    (Const64 <typ.UInt64> [int64(log16(c))]))
1234(Div32 <t> n (Const32 [c])) && isPowerOfTwo32(c) =>
1235  (Rsh32x64
1236    (Add32 <t> n (Rsh32Ux64 <t> (Rsh32x64 <t> n (Const64 <typ.UInt64> [31])) (Const64 <typ.UInt64> [int64(32-log32(c))])))
1237    (Const64 <typ.UInt64> [int64(log32(c))]))
1238(Div64 <t> n (Const64 [c])) && isPowerOfTwo64(c) =>
1239  (Rsh64x64
1240    (Add64 <t> n (Rsh64Ux64 <t> (Rsh64x64 <t> n (Const64 <typ.UInt64> [63])) (Const64 <typ.UInt64> [int64(64-log64(c))])))
1241    (Const64 <typ.UInt64> [int64(log64(c))]))
1242
1243// Signed divide, not a power of 2.  Strength reduce to a multiply.
1244(Div8 <t> x (Const8 [c])) && smagicOK8(c) =>
1245  (Sub8 <t>
1246    (Rsh32x64 <t>
1247      (Mul32 <typ.UInt32>
1248        (Const32 <typ.UInt32> [int32(smagic8(c).m)])
1249        (SignExt8to32 x))
1250      (Const64 <typ.UInt64> [8+smagic8(c).s]))
1251    (Rsh32x64 <t>
1252      (SignExt8to32 x)
1253      (Const64 <typ.UInt64> [31])))
1254(Div16 <t> x (Const16 [c])) && smagicOK16(c) =>
1255  (Sub16 <t>
1256    (Rsh32x64 <t>
1257      (Mul32 <typ.UInt32>
1258        (Const32 <typ.UInt32> [int32(smagic16(c).m)])
1259        (SignExt16to32 x))
1260      (Const64 <typ.UInt64> [16+smagic16(c).s]))
1261    (Rsh32x64 <t>
1262      (SignExt16to32 x)
1263      (Const64 <typ.UInt64> [31])))
1264(Div32 <t> x (Const32 [c])) && smagicOK32(c) && config.RegSize == 8 =>
1265  (Sub32 <t>
1266    (Rsh64x64 <t>
1267      (Mul64 <typ.UInt64>
1268        (Const64 <typ.UInt64> [int64(smagic32(c).m)])
1269        (SignExt32to64 x))
1270      (Const64 <typ.UInt64> [32+smagic32(c).s]))
1271    (Rsh64x64 <t>
1272      (SignExt32to64 x)
1273      (Const64 <typ.UInt64> [63])))
1274(Div32 <t> x (Const32 [c])) && smagicOK32(c) && config.RegSize == 4 && smagic32(c).m&1 == 0 && config.useHmul =>
1275  (Sub32 <t>
1276    (Rsh32x64 <t>
1277      (Hmul32 <t>
1278        (Const32 <typ.UInt32> [int32(smagic32(c).m/2)])
1279        x)
1280      (Const64 <typ.UInt64> [smagic32(c).s-1]))
1281    (Rsh32x64 <t>
1282      x
1283      (Const64 <typ.UInt64> [31])))
1284(Div32 <t> x (Const32 [c])) && smagicOK32(c) && config.RegSize == 4 && smagic32(c).m&1 != 0 && config.useHmul =>
1285  (Sub32 <t>
1286    (Rsh32x64 <t>
1287      (Add32 <t>
1288        (Hmul32 <t>
1289          (Const32 <typ.UInt32> [int32(smagic32(c).m)])
1290          x)
1291        x)
1292      (Const64 <typ.UInt64> [smagic32(c).s]))
1293    (Rsh32x64 <t>
1294      x
1295      (Const64 <typ.UInt64> [31])))
1296(Div64 <t> x (Const64 [c])) && smagicOK64(c) && smagic64(c).m&1 == 0 && config.useHmul =>
1297  (Sub64 <t>
1298    (Rsh64x64 <t>
1299      (Hmul64 <t>
1300        (Const64 <typ.UInt64> [int64(smagic64(c).m/2)])
1301        x)
1302      (Const64 <typ.UInt64> [smagic64(c).s-1]))
1303    (Rsh64x64 <t>
1304      x
1305      (Const64 <typ.UInt64> [63])))
1306(Div64 <t> x (Const64 [c])) && smagicOK64(c) && smagic64(c).m&1 != 0 && config.useHmul =>
1307  (Sub64 <t>
1308    (Rsh64x64 <t>
1309      (Add64 <t>
1310        (Hmul64 <t>
1311          (Const64 <typ.UInt64> [int64(smagic64(c).m)])
1312          x)
1313        x)
1314      (Const64 <typ.UInt64> [smagic64(c).s]))
1315    (Rsh64x64 <t>
1316      x
1317      (Const64 <typ.UInt64> [63])))
1318
1319// Unsigned mod by power of 2 constant.
1320(Mod8u  <t> n (Const8  [c])) && isPowerOfTwo8(c)  => (And8  n (Const8  <t> [c-1]))
1321(Mod16u <t> n (Const16 [c])) && isPowerOfTwo16(c) => (And16 n (Const16 <t> [c-1]))
1322(Mod32u <t> n (Const32 [c])) && isPowerOfTwo32(c) => (And32 n (Const32 <t> [c-1]))
1323(Mod64u <t> n (Const64 [c])) && isPowerOfTwo64(c) => (And64 n (Const64 <t> [c-1]))
1324(Mod64u <t> n (Const64 [-1<<63]))                 => (And64 n (Const64 <t> [1<<63-1]))
1325
1326// Signed non-negative mod by power of 2 constant.
1327(Mod8  <t> n (Const8  [c])) && isNonNegative(n) && isPowerOfTwo8(c)  => (And8  n (Const8  <t> [c-1]))
1328(Mod16 <t> n (Const16 [c])) && isNonNegative(n) && isPowerOfTwo16(c) => (And16 n (Const16 <t> [c-1]))
1329(Mod32 <t> n (Const32 [c])) && isNonNegative(n) && isPowerOfTwo32(c) => (And32 n (Const32 <t> [c-1]))
1330(Mod64 <t> n (Const64 [c])) && isNonNegative(n) && isPowerOfTwo64(c) => (And64 n (Const64 <t> [c-1]))
1331(Mod64 n (Const64 [-1<<63])) && isNonNegative(n)                   => n
1332
1333// Signed mod by negative constant.
1334(Mod8  <t> n (Const8  [c])) && c < 0 && c != -1<<7  => (Mod8  <t> n (Const8  <t> [-c]))
1335(Mod16 <t> n (Const16 [c])) && c < 0 && c != -1<<15 => (Mod16 <t> n (Const16 <t> [-c]))
1336(Mod32 <t> n (Const32 [c])) && c < 0 && c != -1<<31 => (Mod32 <t> n (Const32 <t> [-c]))
1337(Mod64 <t> n (Const64 [c])) && c < 0 && c != -1<<63 => (Mod64 <t> n (Const64 <t> [-c]))
1338
1339// All other mods by constants, do A%B = A-(A/B*B).
1340// This implements % with two * and a bunch of ancillary ops.
1341// One of the * is free if the user's code also computes A/B.
1342(Mod8   <t> x (Const8  [c])) && x.Op != OpConst8  && (c > 0 || c == -1<<7)
1343  => (Sub8  x (Mul8  <t> (Div8   <t> x (Const8  <t> [c])) (Const8  <t> [c])))
1344(Mod16  <t> x (Const16 [c])) && x.Op != OpConst16 && (c > 0 || c == -1<<15)
1345  => (Sub16 x (Mul16 <t> (Div16  <t> x (Const16 <t> [c])) (Const16 <t> [c])))
1346(Mod32  <t> x (Const32 [c])) && x.Op != OpConst32 && (c > 0 || c == -1<<31)
1347  => (Sub32 x (Mul32 <t> (Div32  <t> x (Const32 <t> [c])) (Const32 <t> [c])))
1348(Mod64  <t> x (Const64 [c])) && x.Op != OpConst64 && (c > 0 || c == -1<<63)
1349  => (Sub64 x (Mul64 <t> (Div64  <t> x (Const64 <t> [c])) (Const64 <t> [c])))
1350(Mod8u  <t> x (Const8  [c])) && x.Op != OpConst8  && c > 0 && umagicOK8( c)
1351  => (Sub8  x (Mul8  <t> (Div8u  <t> x (Const8  <t> [c])) (Const8  <t> [c])))
1352(Mod16u <t> x (Const16 [c])) && x.Op != OpConst16 && c > 0 && umagicOK16(c)
1353  => (Sub16 x (Mul16 <t> (Div16u <t> x (Const16 <t> [c])) (Const16 <t> [c])))
1354(Mod32u <t> x (Const32 [c])) && x.Op != OpConst32 && c > 0 && umagicOK32(c)
1355  => (Sub32 x (Mul32 <t> (Div32u <t> x (Const32 <t> [c])) (Const32 <t> [c])))
1356(Mod64u <t> x (Const64 [c])) && x.Op != OpConst64 && c > 0 && umagicOK64(c)
1357  => (Sub64 x (Mul64 <t> (Div64u <t> x (Const64 <t> [c])) (Const64 <t> [c])))
1358
1359// For architectures without rotates on less than 32-bits, promote these checks to 32-bit.
1360(Eq8 (Mod8u x (Const8  [c])) (Const8 [0])) && x.Op != OpConst8 && udivisibleOK8(c) && !hasSmallRotate(config) =>
1361	(Eq32 (Mod32u <typ.UInt32> (ZeroExt8to32 <typ.UInt32> x) (Const32 <typ.UInt32> [int32(uint8(c))])) (Const32 <typ.UInt32> [0]))
1362(Eq16 (Mod16u x (Const16  [c])) (Const16 [0])) && x.Op != OpConst16 && udivisibleOK16(c) && !hasSmallRotate(config) =>
1363	(Eq32 (Mod32u <typ.UInt32> (ZeroExt16to32 <typ.UInt32> x) (Const32 <typ.UInt32> [int32(uint16(c))])) (Const32 <typ.UInt32> [0]))
1364(Eq8 (Mod8 x (Const8  [c])) (Const8 [0])) && x.Op != OpConst8 && sdivisibleOK8(c) && !hasSmallRotate(config) =>
1365	(Eq32 (Mod32 <typ.Int32> (SignExt8to32 <typ.Int32> x) (Const32 <typ.Int32> [int32(c)])) (Const32 <typ.Int32> [0]))
1366(Eq16 (Mod16 x (Const16  [c])) (Const16 [0])) && x.Op != OpConst16 && sdivisibleOK16(c) && !hasSmallRotate(config) =>
1367	(Eq32 (Mod32 <typ.Int32> (SignExt16to32 <typ.Int32> x) (Const32 <typ.Int32> [int32(c)])) (Const32 <typ.Int32> [0]))
1368
1369// Divisibility checks x%c == 0 convert to multiply and rotate.
1370// Note, x%c == 0 is rewritten as x == c*(x/c) during the opt pass
1371// where (x/c) is performed using multiplication with magic constants.
1372// To rewrite x%c == 0 requires pattern matching the rewritten expression
1373// and checking that the division by the same constant wasn't already calculated.
1374// This check is made by counting uses of the magic constant multiplication.
1375// Note that if there were an intermediate opt pass, this rule could be applied
1376// directly on the Div op and magic division rewrites could be delayed to late opt.
1377
1378// Unsigned divisibility checks convert to multiply and rotate.
1379(Eq8 x (Mul8 (Const8 [c])
1380  (Trunc32to8
1381    (Rsh32Ux64
1382      mul:(Mul32
1383        (Const32 [m])
1384        (ZeroExt8to32 x))
1385      (Const64 [s])))
1386	)
1387)
1388  && v.Block.Func.pass.name != "opt" && mul.Uses == 1
1389  && m == int32(1<<8+umagic8(c).m) && s == 8+umagic8(c).s
1390  && x.Op != OpConst8 && udivisibleOK8(c)
1391 => (Leq8U
1392			(RotateLeft8 <typ.UInt8>
1393				(Mul8 <typ.UInt8>
1394					(Const8 <typ.UInt8> [int8(udivisible8(c).m)])
1395					x)
1396				(Const8 <typ.UInt8> [int8(8-udivisible8(c).k)])
1397				)
1398			(Const8 <typ.UInt8> [int8(udivisible8(c).max)])
1399		)
1400
1401(Eq16 x (Mul16 (Const16 [c])
1402  (Trunc64to16
1403    (Rsh64Ux64
1404      mul:(Mul64
1405        (Const64 [m])
1406        (ZeroExt16to64 x))
1407      (Const64 [s])))
1408	)
1409)
1410  && v.Block.Func.pass.name != "opt" && mul.Uses == 1
1411  && m == int64(1<<16+umagic16(c).m) && s == 16+umagic16(c).s
1412  && x.Op != OpConst16 && udivisibleOK16(c)
1413 => (Leq16U
1414			(RotateLeft16 <typ.UInt16>
1415				(Mul16 <typ.UInt16>
1416					(Const16 <typ.UInt16> [int16(udivisible16(c).m)])
1417					x)
1418				(Const16 <typ.UInt16> [int16(16-udivisible16(c).k)])
1419				)
1420			(Const16 <typ.UInt16> [int16(udivisible16(c).max)])
1421		)
1422
1423(Eq16 x (Mul16 (Const16 [c])
1424  (Trunc32to16
1425    (Rsh32Ux64
1426      mul:(Mul32
1427        (Const32 [m])
1428        (ZeroExt16to32 x))
1429      (Const64 [s])))
1430	)
1431)
1432  && v.Block.Func.pass.name != "opt" && mul.Uses == 1
1433  && m == int32(1<<15+umagic16(c).m/2) && s == 16+umagic16(c).s-1
1434  && x.Op != OpConst16 && udivisibleOK16(c)
1435 => (Leq16U
1436			(RotateLeft16 <typ.UInt16>
1437				(Mul16 <typ.UInt16>
1438					(Const16 <typ.UInt16> [int16(udivisible16(c).m)])
1439					x)
1440				(Const16 <typ.UInt16> [int16(16-udivisible16(c).k)])
1441				)
1442			(Const16 <typ.UInt16> [int16(udivisible16(c).max)])
1443		)
1444
1445(Eq16 x (Mul16 (Const16 [c])
1446  (Trunc32to16
1447    (Rsh32Ux64
1448      mul:(Mul32
1449        (Const32 [m])
1450        (Rsh32Ux64 (ZeroExt16to32 x) (Const64 [1])))
1451      (Const64 [s])))
1452	)
1453)
1454  && v.Block.Func.pass.name != "opt" && mul.Uses == 1
1455  && m == int32(1<<15+(umagic16(c).m+1)/2) && s == 16+umagic16(c).s-2
1456  && x.Op != OpConst16 && udivisibleOK16(c)
1457 => (Leq16U
1458			(RotateLeft16 <typ.UInt16>
1459				(Mul16 <typ.UInt16>
1460					(Const16 <typ.UInt16> [int16(udivisible16(c).m)])
1461					x)
1462				(Const16 <typ.UInt16> [int16(16-udivisible16(c).k)])
1463				)
1464			(Const16 <typ.UInt16> [int16(udivisible16(c).max)])
1465		)
1466
1467(Eq16 x (Mul16 (Const16 [c])
1468  (Trunc32to16
1469    (Rsh32Ux64
1470      (Avg32u
1471        (Lsh32x64 (ZeroExt16to32 x) (Const64 [16]))
1472        mul:(Mul32
1473          (Const32 [m])
1474          (ZeroExt16to32 x)))
1475      (Const64 [s])))
1476	)
1477)
1478  && v.Block.Func.pass.name != "opt" && mul.Uses == 1
1479  && m == int32(umagic16(c).m) && s == 16+umagic16(c).s-1
1480  && x.Op != OpConst16 && udivisibleOK16(c)
1481 => (Leq16U
1482			(RotateLeft16 <typ.UInt16>
1483				(Mul16 <typ.UInt16>
1484					(Const16 <typ.UInt16> [int16(udivisible16(c).m)])
1485					x)
1486				(Const16 <typ.UInt16> [int16(16-udivisible16(c).k)])
1487				)
1488			(Const16 <typ.UInt16> [int16(udivisible16(c).max)])
1489		)
1490
1491(Eq32 x (Mul32 (Const32 [c])
1492	(Rsh32Ux64
1493		mul:(Hmul32u
1494			(Const32 [m])
1495			x)
1496		(Const64 [s]))
1497	)
1498)
1499  && v.Block.Func.pass.name != "opt" && mul.Uses == 1
1500  && m == int32(1<<31+umagic32(c).m/2) && s == umagic32(c).s-1
1501	&& x.Op != OpConst32 && udivisibleOK32(c)
1502 => (Leq32U
1503			(RotateLeft32 <typ.UInt32>
1504				(Mul32 <typ.UInt32>
1505					(Const32 <typ.UInt32> [int32(udivisible32(c).m)])
1506					x)
1507				(Const32 <typ.UInt32> [int32(32-udivisible32(c).k)])
1508				)
1509			(Const32 <typ.UInt32> [int32(udivisible32(c).max)])
1510		)
1511
1512(Eq32 x (Mul32 (Const32 [c])
1513  (Rsh32Ux64
1514    mul:(Hmul32u
1515      (Const32 <typ.UInt32> [m])
1516      (Rsh32Ux64 x (Const64 [1])))
1517    (Const64 [s]))
1518	)
1519)
1520  && v.Block.Func.pass.name != "opt" && mul.Uses == 1
1521  && m == int32(1<<31+(umagic32(c).m+1)/2) && s == umagic32(c).s-2
1522	&& x.Op != OpConst32 && udivisibleOK32(c)
1523 => (Leq32U
1524			(RotateLeft32 <typ.UInt32>
1525				(Mul32 <typ.UInt32>
1526					(Const32 <typ.UInt32> [int32(udivisible32(c).m)])
1527					x)
1528				(Const32 <typ.UInt32> [int32(32-udivisible32(c).k)])
1529				)
1530			(Const32 <typ.UInt32> [int32(udivisible32(c).max)])
1531		)
1532
1533(Eq32 x (Mul32 (Const32 [c])
1534  (Rsh32Ux64
1535    (Avg32u
1536      x
1537      mul:(Hmul32u
1538        (Const32 [m])
1539        x))
1540    (Const64 [s]))
1541	)
1542)
1543  && v.Block.Func.pass.name != "opt" && mul.Uses == 1
1544  && m == int32(umagic32(c).m) && s == umagic32(c).s-1
1545	&& x.Op != OpConst32 && udivisibleOK32(c)
1546 => (Leq32U
1547			(RotateLeft32 <typ.UInt32>
1548				(Mul32 <typ.UInt32>
1549					(Const32 <typ.UInt32> [int32(udivisible32(c).m)])
1550					x)
1551				(Const32 <typ.UInt32> [int32(32-udivisible32(c).k)])
1552				)
1553			(Const32 <typ.UInt32> [int32(udivisible32(c).max)])
1554		)
1555
1556(Eq32 x (Mul32 (Const32 [c])
1557  (Trunc64to32
1558    (Rsh64Ux64
1559      mul:(Mul64
1560        (Const64 [m])
1561        (ZeroExt32to64 x))
1562      (Const64 [s])))
1563	)
1564)
1565  && v.Block.Func.pass.name != "opt" && mul.Uses == 1
1566  && m == int64(1<<31+umagic32(c).m/2) && s == 32+umagic32(c).s-1
1567	&& x.Op != OpConst32 && udivisibleOK32(c)
1568 => (Leq32U
1569			(RotateLeft32 <typ.UInt32>
1570				(Mul32 <typ.UInt32>
1571					(Const32 <typ.UInt32> [int32(udivisible32(c).m)])
1572					x)
1573				(Const32 <typ.UInt32> [int32(32-udivisible32(c).k)])
1574				)
1575			(Const32 <typ.UInt32> [int32(udivisible32(c).max)])
1576		)
1577
1578(Eq32 x (Mul32 (Const32 [c])
1579  (Trunc64to32
1580    (Rsh64Ux64
1581      mul:(Mul64
1582        (Const64 [m])
1583        (Rsh64Ux64 (ZeroExt32to64 x) (Const64 [1])))
1584      (Const64 [s])))
1585	)
1586)
1587  && v.Block.Func.pass.name != "opt" && mul.Uses == 1
1588  && m == int64(1<<31+(umagic32(c).m+1)/2) && s == 32+umagic32(c).s-2
1589	&& x.Op != OpConst32 && udivisibleOK32(c)
1590 => (Leq32U
1591			(RotateLeft32 <typ.UInt32>
1592				(Mul32 <typ.UInt32>
1593					(Const32 <typ.UInt32> [int32(udivisible32(c).m)])
1594					x)
1595				(Const32 <typ.UInt32> [int32(32-udivisible32(c).k)])
1596				)
1597			(Const32 <typ.UInt32> [int32(udivisible32(c).max)])
1598		)
1599
1600(Eq32 x (Mul32 (Const32 [c])
1601  (Trunc64to32
1602    (Rsh64Ux64
1603      (Avg64u
1604        (Lsh64x64 (ZeroExt32to64 x) (Const64 [32]))
1605        mul:(Mul64
1606          (Const64 [m])
1607          (ZeroExt32to64 x)))
1608      (Const64 [s])))
1609	)
1610)
1611  && v.Block.Func.pass.name != "opt" && mul.Uses == 1
1612  && m == int64(umagic32(c).m) && s == 32+umagic32(c).s-1
1613	&& x.Op != OpConst32 && udivisibleOK32(c)
1614 => (Leq32U
1615			(RotateLeft32 <typ.UInt32>
1616				(Mul32 <typ.UInt32>
1617					(Const32 <typ.UInt32> [int32(udivisible32(c).m)])
1618					x)
1619				(Const32 <typ.UInt32> [int32(32-udivisible32(c).k)])
1620				)
1621			(Const32 <typ.UInt32> [int32(udivisible32(c).max)])
1622		)
1623
1624(Eq64 x (Mul64 (Const64 [c])
1625	(Rsh64Ux64
1626		mul:(Hmul64u
1627			(Const64 [m])
1628			x)
1629		(Const64 [s]))
1630	)
1631) && v.Block.Func.pass.name != "opt" && mul.Uses == 1
1632  && m == int64(1<<63+umagic64(c).m/2) && s == umagic64(c).s-1
1633  && x.Op != OpConst64 && udivisibleOK64(c)
1634 => (Leq64U
1635			(RotateLeft64 <typ.UInt64>
1636				(Mul64 <typ.UInt64>
1637					(Const64 <typ.UInt64> [int64(udivisible64(c).m)])
1638					x)
1639				(Const64 <typ.UInt64> [64-udivisible64(c).k])
1640				)
1641			(Const64 <typ.UInt64> [int64(udivisible64(c).max)])
1642		)
1643(Eq64 x (Mul64 (Const64 [c])
1644	(Rsh64Ux64
1645		mul:(Hmul64u
1646			(Const64 [m])
1647			(Rsh64Ux64 x (Const64 [1])))
1648		(Const64 [s]))
1649	)
1650) && v.Block.Func.pass.name != "opt" && mul.Uses == 1
1651  && m == int64(1<<63+(umagic64(c).m+1)/2) && s == umagic64(c).s-2
1652  && x.Op != OpConst64 && udivisibleOK64(c)
1653 => (Leq64U
1654			(RotateLeft64 <typ.UInt64>
1655				(Mul64 <typ.UInt64>
1656					(Const64 <typ.UInt64> [int64(udivisible64(c).m)])
1657					x)
1658				(Const64 <typ.UInt64> [64-udivisible64(c).k])
1659				)
1660			(Const64 <typ.UInt64> [int64(udivisible64(c).max)])
1661		)
1662(Eq64 x (Mul64 (Const64 [c])
1663	(Rsh64Ux64
1664		(Avg64u
1665			x
1666			mul:(Hmul64u
1667				(Const64 [m])
1668				x))
1669		(Const64 [s]))
1670	)
1671) && v.Block.Func.pass.name != "opt" && mul.Uses == 1
1672  && m == int64(umagic64(c).m) && s == umagic64(c).s-1
1673  && x.Op != OpConst64 && udivisibleOK64(c)
1674 => (Leq64U
1675			(RotateLeft64 <typ.UInt64>
1676				(Mul64 <typ.UInt64>
1677					(Const64 <typ.UInt64> [int64(udivisible64(c).m)])
1678					x)
1679				(Const64 <typ.UInt64> [64-udivisible64(c).k])
1680				)
1681			(Const64 <typ.UInt64> [int64(udivisible64(c).max)])
1682		)
1683
1684// Signed divisibility checks convert to multiply, add and rotate.
1685(Eq8 x (Mul8 (Const8 [c])
1686  (Sub8
1687    (Rsh32x64
1688      mul:(Mul32
1689        (Const32 [m])
1690        (SignExt8to32 x))
1691      (Const64 [s]))
1692    (Rsh32x64
1693      (SignExt8to32 x)
1694      (Const64 [31])))
1695	)
1696)
1697  && v.Block.Func.pass.name != "opt" && mul.Uses == 1
1698  && m == int32(smagic8(c).m) && s == 8+smagic8(c).s
1699	&& x.Op != OpConst8 && sdivisibleOK8(c)
1700 => (Leq8U
1701			(RotateLeft8 <typ.UInt8>
1702				(Add8 <typ.UInt8>
1703					(Mul8 <typ.UInt8>
1704						(Const8 <typ.UInt8> [int8(sdivisible8(c).m)])
1705						x)
1706					(Const8 <typ.UInt8> [int8(sdivisible8(c).a)])
1707				)
1708				(Const8 <typ.UInt8> [int8(8-sdivisible8(c).k)])
1709			)
1710			(Const8 <typ.UInt8> [int8(sdivisible8(c).max)])
1711		)
1712
1713(Eq16 x (Mul16 (Const16 [c])
1714  (Sub16
1715    (Rsh32x64
1716      mul:(Mul32
1717        (Const32 [m])
1718        (SignExt16to32 x))
1719      (Const64 [s]))
1720    (Rsh32x64
1721      (SignExt16to32 x)
1722      (Const64 [31])))
1723	)
1724)
1725  && v.Block.Func.pass.name != "opt" && mul.Uses == 1
1726  && m == int32(smagic16(c).m) && s == 16+smagic16(c).s
1727	&& x.Op != OpConst16 && sdivisibleOK16(c)
1728 => (Leq16U
1729			(RotateLeft16 <typ.UInt16>
1730				(Add16 <typ.UInt16>
1731					(Mul16 <typ.UInt16>
1732						(Const16 <typ.UInt16> [int16(sdivisible16(c).m)])
1733						x)
1734					(Const16 <typ.UInt16> [int16(sdivisible16(c).a)])
1735				)
1736				(Const16 <typ.UInt16> [int16(16-sdivisible16(c).k)])
1737			)
1738			(Const16 <typ.UInt16> [int16(sdivisible16(c).max)])
1739		)
1740
1741(Eq32 x (Mul32 (Const32 [c])
1742  (Sub32
1743    (Rsh64x64
1744      mul:(Mul64
1745        (Const64 [m])
1746        (SignExt32to64 x))
1747      (Const64 [s]))
1748    (Rsh64x64
1749      (SignExt32to64 x)
1750      (Const64 [63])))
1751	)
1752)
1753  && v.Block.Func.pass.name != "opt" && mul.Uses == 1
1754  && m == int64(smagic32(c).m) && s == 32+smagic32(c).s
1755	&& x.Op != OpConst32 && sdivisibleOK32(c)
1756 => (Leq32U
1757			(RotateLeft32 <typ.UInt32>
1758				(Add32 <typ.UInt32>
1759					(Mul32 <typ.UInt32>
1760						(Const32 <typ.UInt32> [int32(sdivisible32(c).m)])
1761						x)
1762					(Const32 <typ.UInt32> [int32(sdivisible32(c).a)])
1763				)
1764				(Const32 <typ.UInt32> [int32(32-sdivisible32(c).k)])
1765			)
1766			(Const32 <typ.UInt32> [int32(sdivisible32(c).max)])
1767		)
1768
1769(Eq32 x (Mul32 (Const32 [c])
1770  (Sub32
1771    (Rsh32x64
1772      mul:(Hmul32
1773        (Const32 [m])
1774        x)
1775      (Const64 [s]))
1776    (Rsh32x64
1777      x
1778      (Const64 [31])))
1779	)
1780)
1781  && v.Block.Func.pass.name != "opt" && mul.Uses == 1
1782  && m == int32(smagic32(c).m/2) && s == smagic32(c).s-1
1783	&& x.Op != OpConst32 && sdivisibleOK32(c)
1784 => (Leq32U
1785			(RotateLeft32 <typ.UInt32>
1786				(Add32 <typ.UInt32>
1787					(Mul32 <typ.UInt32>
1788						(Const32 <typ.UInt32> [int32(sdivisible32(c).m)])
1789						x)
1790					(Const32 <typ.UInt32> [int32(sdivisible32(c).a)])
1791				)
1792				(Const32 <typ.UInt32> [int32(32-sdivisible32(c).k)])
1793			)
1794			(Const32 <typ.UInt32> [int32(sdivisible32(c).max)])
1795		)
1796
1797(Eq32 x (Mul32 (Const32 [c])
1798  (Sub32
1799    (Rsh32x64
1800      (Add32
1801        mul:(Hmul32
1802          (Const32 [m])
1803          x)
1804        x)
1805      (Const64 [s]))
1806    (Rsh32x64
1807      x
1808      (Const64 [31])))
1809	)
1810)
1811  && v.Block.Func.pass.name != "opt" && mul.Uses == 1
1812  && m == int32(smagic32(c).m) && s == smagic32(c).s
1813	&& x.Op != OpConst32 && sdivisibleOK32(c)
1814 => (Leq32U
1815			(RotateLeft32 <typ.UInt32>
1816				(Add32 <typ.UInt32>
1817					(Mul32 <typ.UInt32>
1818						(Const32 <typ.UInt32> [int32(sdivisible32(c).m)])
1819						x)
1820					(Const32 <typ.UInt32> [int32(sdivisible32(c).a)])
1821				)
1822				(Const32 <typ.UInt32> [int32(32-sdivisible32(c).k)])
1823			)
1824			(Const32 <typ.UInt32> [int32(sdivisible32(c).max)])
1825		)
1826
1827(Eq64 x (Mul64 (Const64 [c])
1828  (Sub64
1829    (Rsh64x64
1830      mul:(Hmul64
1831        (Const64 [m])
1832        x)
1833      (Const64 [s]))
1834    (Rsh64x64
1835      x
1836      (Const64 [63])))
1837	)
1838)
1839  && v.Block.Func.pass.name != "opt" && mul.Uses == 1
1840  && m == int64(smagic64(c).m/2) && s == smagic64(c).s-1
1841	&& x.Op != OpConst64 && sdivisibleOK64(c)
1842 => (Leq64U
1843			(RotateLeft64 <typ.UInt64>
1844				(Add64 <typ.UInt64>
1845					(Mul64 <typ.UInt64>
1846						(Const64 <typ.UInt64> [int64(sdivisible64(c).m)])
1847						x)
1848					(Const64 <typ.UInt64> [int64(sdivisible64(c).a)])
1849				)
1850				(Const64 <typ.UInt64> [64-sdivisible64(c).k])
1851			)
1852			(Const64 <typ.UInt64> [int64(sdivisible64(c).max)])
1853		)
1854
1855(Eq64 x (Mul64 (Const64 [c])
1856  (Sub64
1857    (Rsh64x64
1858      (Add64
1859        mul:(Hmul64
1860          (Const64 [m])
1861          x)
1862        x)
1863      (Const64 [s]))
1864    (Rsh64x64
1865      x
1866      (Const64 [63])))
1867	)
1868)
1869  && v.Block.Func.pass.name != "opt" && mul.Uses == 1
1870  && m == int64(smagic64(c).m) && s == smagic64(c).s
1871	&& x.Op != OpConst64 && sdivisibleOK64(c)
1872 => (Leq64U
1873			(RotateLeft64 <typ.UInt64>
1874				(Add64 <typ.UInt64>
1875					(Mul64 <typ.UInt64>
1876						(Const64 <typ.UInt64> [int64(sdivisible64(c).m)])
1877						x)
1878					(Const64 <typ.UInt64> [int64(sdivisible64(c).a)])
1879				)
1880				(Const64 <typ.UInt64> [64-sdivisible64(c).k])
1881			)
1882			(Const64 <typ.UInt64> [int64(sdivisible64(c).max)])
1883		)
1884
1885// Divisibility check for signed integers for power of two constant are simple mask.
1886// However, we must match against the rewritten n%c == 0 -> n - c*(n/c) == 0 -> n == c*(n/c)
1887// where n/c contains fixup code to handle signed n.
1888((Eq8|Neq8) n (Lsh8x64
1889  (Rsh8x64
1890    (Add8  <t> n (Rsh8Ux64  <t> (Rsh8x64  <t> n (Const64 <typ.UInt64> [ 7])) (Const64 <typ.UInt64> [kbar])))
1891    (Const64 <typ.UInt64> [k]))
1892	(Const64 <typ.UInt64> [k]))
1893) && k > 0 && k < 7 && kbar == 8 - k
1894  => ((Eq8|Neq8) (And8 <t> n (Const8 <t> [1<<uint(k)-1])) (Const8 <t> [0]))
1895
1896((Eq16|Neq16) n (Lsh16x64
1897  (Rsh16x64
1898    (Add16 <t> n (Rsh16Ux64 <t> (Rsh16x64 <t> n (Const64 <typ.UInt64> [15])) (Const64 <typ.UInt64> [kbar])))
1899    (Const64 <typ.UInt64> [k]))
1900	(Const64 <typ.UInt64> [k]))
1901) && k > 0 && k < 15 && kbar == 16 - k
1902  => ((Eq16|Neq16) (And16 <t> n (Const16 <t> [1<<uint(k)-1])) (Const16 <t> [0]))
1903
1904((Eq32|Neq32) n (Lsh32x64
1905  (Rsh32x64
1906    (Add32 <t> n (Rsh32Ux64 <t> (Rsh32x64 <t> n (Const64 <typ.UInt64> [31])) (Const64 <typ.UInt64> [kbar])))
1907    (Const64 <typ.UInt64> [k]))
1908	(Const64 <typ.UInt64> [k]))
1909) && k > 0 && k < 31 && kbar == 32 - k
1910  => ((Eq32|Neq32) (And32 <t> n (Const32 <t> [1<<uint(k)-1])) (Const32 <t> [0]))
1911
1912((Eq64|Neq64) n (Lsh64x64
1913  (Rsh64x64
1914    (Add64 <t> n (Rsh64Ux64 <t> (Rsh64x64 <t> n (Const64 <typ.UInt64> [63])) (Const64 <typ.UInt64> [kbar])))
1915    (Const64 <typ.UInt64> [k]))
1916	(Const64 <typ.UInt64> [k]))
1917) && k > 0 && k < 63 && kbar == 64 - k
1918  => ((Eq64|Neq64) (And64 <t> n (Const64 <t> [1<<uint(k)-1])) (Const64 <t> [0]))
1919
1920(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)
1921(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)
1922
1923// Optimize bitsets
1924(Eq8 (And8 <t> x (Const8 <t> [y])) (Const8 <t> [y])) && oneBit8(y)
1925  => (Neq8 (And8 <t> x (Const8 <t> [y])) (Const8 <t> [0]))
1926(Eq16 (And16 <t> x (Const16 <t> [y])) (Const16 <t> [y])) && oneBit16(y)
1927  => (Neq16 (And16 <t> x (Const16 <t> [y])) (Const16 <t> [0]))
1928(Eq32 (And32 <t> x (Const32 <t> [y])) (Const32 <t> [y])) && oneBit32(y)
1929  => (Neq32 (And32 <t> x (Const32 <t> [y])) (Const32 <t> [0]))
1930(Eq64 (And64 <t> x (Const64 <t> [y])) (Const64 <t> [y])) && oneBit64(y)
1931  => (Neq64 (And64 <t> x (Const64 <t> [y])) (Const64 <t> [0]))
1932(Neq8 (And8 <t> x (Const8 <t> [y])) (Const8 <t> [y])) && oneBit8(y)
1933  => (Eq8 (And8 <t> x (Const8 <t> [y])) (Const8 <t> [0]))
1934(Neq16 (And16 <t> x (Const16 <t> [y])) (Const16 <t> [y])) && oneBit16(y)
1935  => (Eq16 (And16 <t> x (Const16 <t> [y])) (Const16 <t> [0]))
1936(Neq32 (And32 <t> x (Const32 <t> [y])) (Const32 <t> [y])) && oneBit32(y)
1937  => (Eq32 (And32 <t> x (Const32 <t> [y])) (Const32 <t> [0]))
1938(Neq64 (And64 <t> x (Const64 <t> [y])) (Const64 <t> [y])) && oneBit64(y)
1939  => (Eq64 (And64 <t> x (Const64 <t> [y])) (Const64 <t> [0]))
1940
1941// Reassociate expressions involving
1942// constants such that constants come first,
1943// exposing obvious constant-folding opportunities.
1944// Reassociate (op (op y C) x) to (op C (op x y)) or similar, where C
1945// is constant, which pushes constants to the outside
1946// of the expression. At that point, any constant-folding
1947// opportunities should be obvious.
1948// Note: don't include AddPtr here! In order to maintain the
1949// invariant that pointers must stay within the pointed-to object,
1950// we can't pull part of a pointer computation above the AddPtr.
1951// See issue 37881.
1952// Note: we don't need to handle any (x-C) cases because we already rewrite
1953// (x-C) to (x+(-C)).
1954
1955// x + (C + z) -> C + (x + z)
1956(Add64 (Add64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) => (Add64 i (Add64 <t> z x))
1957(Add32 (Add32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) => (Add32 i (Add32 <t> z x))
1958(Add16 (Add16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) => (Add16 i (Add16 <t> z x))
1959(Add8  (Add8  i:(Const8  <t>) z) x) && (z.Op != OpConst8  && x.Op != OpConst8)  => (Add8  i (Add8  <t> z x))
1960
1961// x + (C - z) -> C + (x - z)
1962(Add64 (Sub64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) => (Add64 i (Sub64 <t> x z))
1963(Add32 (Sub32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) => (Add32 i (Sub32 <t> x z))
1964(Add16 (Sub16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) => (Add16 i (Sub16 <t> x z))
1965(Add8  (Sub8  i:(Const8  <t>) z) x) && (z.Op != OpConst8  && x.Op != OpConst8)  => (Add8  i (Sub8  <t> x z))
1966
1967// x - (C - z) -> x + (z - C) -> (x + z) - C
1968(Sub64 x (Sub64 i:(Const64 <t>) z)) && (z.Op != OpConst64 && x.Op != OpConst64) => (Sub64 (Add64 <t> x z) i)
1969(Sub32 x (Sub32 i:(Const32 <t>) z)) && (z.Op != OpConst32 && x.Op != OpConst32) => (Sub32 (Add32 <t> x z) i)
1970(Sub16 x (Sub16 i:(Const16 <t>) z)) && (z.Op != OpConst16 && x.Op != OpConst16) => (Sub16 (Add16 <t> x z) i)
1971(Sub8  x (Sub8  i:(Const8  <t>) z)) && (z.Op != OpConst8  && x.Op != OpConst8)  => (Sub8  (Add8  <t> x z) i)
1972
1973// x - (z + C) -> x + (-z - C) -> (x - z) - C
1974(Sub64 x (Add64 z i:(Const64 <t>))) && (z.Op != OpConst64 && x.Op != OpConst64) => (Sub64 (Sub64 <t> x z) i)
1975(Sub32 x (Add32 z i:(Const32 <t>))) && (z.Op != OpConst32 && x.Op != OpConst32) => (Sub32 (Sub32 <t> x z) i)
1976(Sub16 x (Add16 z i:(Const16 <t>))) && (z.Op != OpConst16 && x.Op != OpConst16) => (Sub16 (Sub16 <t> x z) i)
1977(Sub8  x (Add8  z i:(Const8  <t>))) && (z.Op != OpConst8  && x.Op != OpConst8)  => (Sub8 (Sub8  <t> x z) i)
1978
1979// (C - z) - x -> C - (z + x)
1980(Sub64 (Sub64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) => (Sub64 i (Add64 <t> z x))
1981(Sub32 (Sub32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) => (Sub32 i (Add32 <t> z x))
1982(Sub16 (Sub16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) => (Sub16 i (Add16 <t> z x))
1983(Sub8  (Sub8  i:(Const8  <t>) z) x) && (z.Op != OpConst8  && x.Op != OpConst8)  => (Sub8  i (Add8  <t> z x))
1984
1985// (z + C) -x -> C + (z - x)
1986(Sub64 (Add64 z i:(Const64 <t>)) x) && (z.Op != OpConst64 && x.Op != OpConst64) => (Add64 i (Sub64 <t> z x))
1987(Sub32 (Add32 z i:(Const32 <t>)) x) && (z.Op != OpConst32 && x.Op != OpConst32) => (Add32 i (Sub32 <t> z x))
1988(Sub16 (Add16 z i:(Const16 <t>)) x) && (z.Op != OpConst16 && x.Op != OpConst16) => (Add16 i (Sub16 <t> z x))
1989(Sub8  (Add8  z i:(Const8  <t>)) x) && (z.Op != OpConst8  && x.Op != OpConst8)  => (Add8  i (Sub8  <t> z x))
1990
1991// x & (C & z) -> C & (x & z)
1992(And64 (And64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) => (And64 i (And64 <t> z x))
1993(And32 (And32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) => (And32 i (And32 <t> z x))
1994(And16 (And16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) => (And16 i (And16 <t> z x))
1995(And8  (And8  i:(Const8  <t>) z) x) && (z.Op != OpConst8  && x.Op != OpConst8)  => (And8  i (And8  <t> z x))
1996
1997// x | (C | z) -> C | (x | z)
1998(Or64 (Or64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) => (Or64 i (Or64 <t> z x))
1999(Or32 (Or32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) => (Or32 i (Or32 <t> z x))
2000(Or16 (Or16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) => (Or16 i (Or16 <t> z x))
2001(Or8  (Or8  i:(Const8  <t>) z) x) && (z.Op != OpConst8  && x.Op != OpConst8)  => (Or8  i (Or8  <t> z x))
2002
2003// x ^ (C ^ z) -> C ^ (x ^ z)
2004(Xor64 (Xor64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) => (Xor64 i (Xor64 <t> z x))
2005(Xor32 (Xor32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) => (Xor32 i (Xor32 <t> z x))
2006(Xor16 (Xor16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) => (Xor16 i (Xor16 <t> z x))
2007(Xor8  (Xor8  i:(Const8  <t>) z) x) && (z.Op != OpConst8  && x.Op != OpConst8)  => (Xor8  i (Xor8  <t> z x))
2008
2009// x * (D * z) = D * (x * z)
2010(Mul64 (Mul64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) => (Mul64 i (Mul64 <t> x z))
2011(Mul32 (Mul32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) => (Mul32 i (Mul32 <t> x z))
2012(Mul16 (Mul16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) => (Mul16 i (Mul16 <t> x z))
2013(Mul8  (Mul8  i:(Const8  <t>) z) x) && (z.Op != OpConst8  && x.Op != OpConst8)  => (Mul8  i (Mul8  <t> x z))
2014
2015// C + (D + x) -> (C + D) + x
2016(Add64 (Const64 <t> [c]) (Add64 (Const64 <t> [d]) x)) => (Add64 (Const64 <t> [c+d]) x)
2017(Add32 (Const32 <t> [c]) (Add32 (Const32 <t> [d]) x)) => (Add32 (Const32 <t> [c+d]) x)
2018(Add16 (Const16 <t> [c]) (Add16 (Const16 <t> [d]) x)) => (Add16 (Const16 <t> [c+d]) x)
2019(Add8  (Const8  <t> [c]) (Add8  (Const8  <t> [d]) x)) => (Add8  (Const8  <t> [c+d]) x)
2020
2021// C + (D - x) -> (C + D) - x
2022(Add64 (Const64 <t> [c]) (Sub64 (Const64 <t> [d]) x)) => (Sub64 (Const64 <t> [c+d]) x)
2023(Add32 (Const32 <t> [c]) (Sub32 (Const32 <t> [d]) x)) => (Sub32 (Const32 <t> [c+d]) x)
2024(Add16 (Const16 <t> [c]) (Sub16 (Const16 <t> [d]) x)) => (Sub16 (Const16 <t> [c+d]) x)
2025(Add8  (Const8  <t> [c]) (Sub8  (Const8  <t> [d]) x)) => (Sub8  (Const8  <t> [c+d]) x)
2026
2027// C - (D - x) -> (C - D) + x
2028(Sub64 (Const64 <t> [c]) (Sub64 (Const64 <t> [d]) x)) => (Add64 (Const64 <t> [c-d]) x)
2029(Sub32 (Const32 <t> [c]) (Sub32 (Const32 <t> [d]) x)) => (Add32 (Const32 <t> [c-d]) x)
2030(Sub16 (Const16 <t> [c]) (Sub16 (Const16 <t> [d]) x)) => (Add16 (Const16 <t> [c-d]) x)
2031(Sub8  (Const8  <t> [c]) (Sub8  (Const8  <t> [d]) x)) => (Add8  (Const8  <t> [c-d]) x)
2032
2033// C - (D + x) -> (C - D) - x
2034(Sub64 (Const64 <t> [c]) (Add64 (Const64 <t> [d]) x)) => (Sub64 (Const64 <t> [c-d]) x)
2035(Sub32 (Const32 <t> [c]) (Add32 (Const32 <t> [d]) x)) => (Sub32 (Const32 <t> [c-d]) x)
2036(Sub16 (Const16 <t> [c]) (Add16 (Const16 <t> [d]) x)) => (Sub16 (Const16 <t> [c-d]) x)
2037(Sub8  (Const8  <t> [c]) (Add8  (Const8  <t> [d]) x)) => (Sub8  (Const8  <t> [c-d]) x)
2038
2039// C & (D & x) -> (C & D) & x
2040(And64 (Const64 <t> [c]) (And64 (Const64 <t> [d]) x)) => (And64 (Const64 <t> [c&d]) x)
2041(And32 (Const32 <t> [c]) (And32 (Const32 <t> [d]) x)) => (And32 (Const32 <t> [c&d]) x)
2042(And16 (Const16 <t> [c]) (And16 (Const16 <t> [d]) x)) => (And16 (Const16 <t> [c&d]) x)
2043(And8  (Const8  <t> [c]) (And8  (Const8  <t> [d]) x)) => (And8  (Const8  <t> [c&d]) x)
2044
2045// C | (D | x) -> (C | D) | x
2046(Or64 (Const64 <t> [c]) (Or64 (Const64 <t> [d]) x)) => (Or64 (Const64 <t> [c|d]) x)
2047(Or32 (Const32 <t> [c]) (Or32 (Const32 <t> [d]) x)) => (Or32 (Const32 <t> [c|d]) x)
2048(Or16 (Const16 <t> [c]) (Or16 (Const16 <t> [d]) x)) => (Or16 (Const16 <t> [c|d]) x)
2049(Or8  (Const8  <t> [c]) (Or8  (Const8  <t> [d]) x)) => (Or8  (Const8  <t> [c|d]) x)
2050
2051// C ^ (D ^ x) -> (C ^ D) ^ x
2052(Xor64 (Const64 <t> [c]) (Xor64 (Const64 <t> [d]) x)) => (Xor64 (Const64 <t> [c^d]) x)
2053(Xor32 (Const32 <t> [c]) (Xor32 (Const32 <t> [d]) x)) => (Xor32 (Const32 <t> [c^d]) x)
2054(Xor16 (Const16 <t> [c]) (Xor16 (Const16 <t> [d]) x)) => (Xor16 (Const16 <t> [c^d]) x)
2055(Xor8  (Const8  <t> [c]) (Xor8  (Const8  <t> [d]) x)) => (Xor8  (Const8  <t> [c^d]) x)
2056
2057// C * (D * x) = (C * D) * x
2058(Mul64 (Const64 <t> [c]) (Mul64 (Const64 <t> [d]) x)) => (Mul64 (Const64 <t> [c*d]) x)
2059(Mul32 (Const32 <t> [c]) (Mul32 (Const32 <t> [d]) x)) => (Mul32 (Const32 <t> [c*d]) x)
2060(Mul16 (Const16 <t> [c]) (Mul16 (Const16 <t> [d]) x)) => (Mul16 (Const16 <t> [c*d]) x)
2061(Mul8  (Const8  <t> [c]) (Mul8  (Const8  <t> [d]) x)) => (Mul8  (Const8  <t> [c*d]) x)
2062
2063// floating point optimizations
2064(Mul(32|64)F x (Const(32|64)F [1])) => x
2065(Mul32F x (Const32F [-1])) => (Neg32F x)
2066(Mul64F x (Const64F [-1])) => (Neg64F x)
2067(Mul32F x (Const32F [2])) => (Add32F x x)
2068(Mul64F x (Const64F [2])) => (Add64F x x)
2069
2070(Div32F x (Const32F <t> [c])) && reciprocalExact32(c) => (Mul32F x (Const32F <t> [1/c]))
2071(Div64F x (Const64F <t> [c])) && reciprocalExact64(c) => (Mul64F x (Const64F <t> [1/c]))
2072
2073// rewrite single-precision sqrt expression "float32(math.Sqrt(float64(x)))"
2074(Cvt64Fto32F sqrt0:(Sqrt (Cvt32Fto64F x))) && sqrt0.Uses==1 => (Sqrt32 x)
2075
2076(Sqrt (Const64F [c])) && !math.IsNaN(math.Sqrt(c)) => (Const64F [math.Sqrt(c)])
2077
2078// for rewriting results of some late-expanded rewrites (below)
2079(SelectN [0] (MakeResult x ___)) => x
2080(SelectN [1] (MakeResult x y ___)) => y
2081(SelectN [2] (MakeResult x y z ___)) => z
2082
2083// for late-expanded calls, recognize newobject and remove zeroing and nilchecks
2084(Zero (SelectN [0] call:(StaticLECall _ _)) mem:(SelectN [1] call))
2085	&& isSameCall(call.Aux, "runtime.newobject")
2086	=> mem
2087
2088(Store (SelectN [0] call:(StaticLECall _ _)) x mem:(SelectN [1] call))
2089	&& isConstZero(x)
2090	&& isSameCall(call.Aux, "runtime.newobject")
2091	=> mem
2092
2093(Store (OffPtr (SelectN [0] call:(StaticLECall _ _))) x mem:(SelectN [1] call))
2094	&& isConstZero(x)
2095	&& isSameCall(call.Aux, "runtime.newobject")
2096	=> mem
2097
2098(NilCheck ptr:(SelectN [0] call:(StaticLECall _ _)) _)
2099	&& isSameCall(call.Aux, "runtime.newobject")
2100	&& warnRule(fe.Debug_checknil(), v, "removed nil check")
2101	=> ptr
2102
2103(NilCheck ptr:(OffPtr (SelectN [0] call:(StaticLECall _ _))) _)
2104	&& isSameCall(call.Aux, "runtime.newobject")
2105	&& warnRule(fe.Debug_checknil(), v, "removed nil check")
2106	=> ptr
2107
2108// Addresses of globals are always non-nil.
2109(NilCheck          ptr:(Addr {_} (SB))    _) => ptr
2110(NilCheck ptr:(Convert (Addr {_} (SB)) _) _) => ptr
2111
2112// for late-expanded calls, recognize memequal applied to a single constant byte
2113// Support is limited by 1, 2, 4, 8 byte sizes
2114(StaticLECall {callAux} sptr (Addr {scon} (SB)) (Const64 [1]) mem)
2115  && isSameCall(callAux, "runtime.memequal")
2116  && symIsRO(scon)
2117  => (MakeResult (Eq8 (Load <typ.Int8> sptr mem) (Const8 <typ.Int8> [int8(read8(scon,0))])) mem)
2118
2119(StaticLECall {callAux} (Addr {scon} (SB)) sptr (Const64 [1]) mem)
2120  && isSameCall(callAux, "runtime.memequal")
2121  && symIsRO(scon)
2122  => (MakeResult (Eq8 (Load <typ.Int8> sptr mem) (Const8 <typ.Int8> [int8(read8(scon,0))])) mem)
2123
2124(StaticLECall {callAux} sptr (Addr {scon} (SB)) (Const64 [2]) mem)
2125  && isSameCall(callAux, "runtime.memequal")
2126  && symIsRO(scon)
2127  && canLoadUnaligned(config)
2128  => (MakeResult (Eq16 (Load <typ.Int16> sptr mem) (Const16 <typ.Int16> [int16(read16(scon,0,config.ctxt.Arch.ByteOrder))])) mem)
2129
2130(StaticLECall {callAux} (Addr {scon} (SB)) sptr (Const64 [2]) mem)
2131  && isSameCall(callAux, "runtime.memequal")
2132  && symIsRO(scon)
2133  && canLoadUnaligned(config)
2134  => (MakeResult (Eq16 (Load <typ.Int16> sptr mem) (Const16 <typ.Int16> [int16(read16(scon,0,config.ctxt.Arch.ByteOrder))])) mem)
2135
2136(StaticLECall {callAux} sptr (Addr {scon} (SB)) (Const64 [4]) mem)
2137  && isSameCall(callAux, "runtime.memequal")
2138  && symIsRO(scon)
2139  && canLoadUnaligned(config)
2140  => (MakeResult (Eq32 (Load <typ.Int32> sptr mem) (Const32 <typ.Int32> [int32(read32(scon,0,config.ctxt.Arch.ByteOrder))])) mem)
2141
2142(StaticLECall {callAux} (Addr {scon} (SB)) sptr (Const64 [4]) mem)
2143  && isSameCall(callAux, "runtime.memequal")
2144  && symIsRO(scon)
2145  && canLoadUnaligned(config)
2146  => (MakeResult (Eq32 (Load <typ.Int32> sptr mem) (Const32 <typ.Int32> [int32(read32(scon,0,config.ctxt.Arch.ByteOrder))])) mem)
2147
2148(StaticLECall {callAux} sptr (Addr {scon} (SB)) (Const64 [8]) mem)
2149  && isSameCall(callAux, "runtime.memequal")
2150  && symIsRO(scon)
2151  && canLoadUnaligned(config) && config.PtrSize == 8
2152  => (MakeResult (Eq64 (Load <typ.Int64> sptr mem) (Const64 <typ.Int64> [int64(read64(scon,0,config.ctxt.Arch.ByteOrder))])) mem)
2153
2154(StaticLECall {callAux} (Addr {scon} (SB)) sptr (Const64 [8]) mem)
2155  && isSameCall(callAux, "runtime.memequal")
2156  && symIsRO(scon)
2157  && canLoadUnaligned(config) && config.PtrSize == 8
2158  => (MakeResult (Eq64 (Load <typ.Int64> sptr mem) (Const64 <typ.Int64> [int64(read64(scon,0,config.ctxt.Arch.ByteOrder))])) mem)
2159
2160(StaticLECall {callAux} _ _ (Const64 [0]) mem)
2161  && isSameCall(callAux, "runtime.memequal")
2162  => (MakeResult (ConstBool <typ.Bool> [true]) mem)
2163
2164(Static(Call|LECall) {callAux} p q _ mem)
2165  && isSameCall(callAux, "runtime.memequal")
2166  && isSamePtr(p, q)
2167  => (MakeResult (ConstBool <typ.Bool> [true]) mem)
2168
2169// Turn known-size calls to memclrNoHeapPointers into a Zero.
2170// Note that we are using types.Types[types.TUINT8] instead of sptr.Type.Elem() - see issue 55122 and CL 431496 for more details.
2171(SelectN [0] call:(StaticCall {sym} sptr (Const(64|32) [c]) mem))
2172  && isInlinableMemclr(config, int64(c))
2173  && isSameCall(sym, "runtime.memclrNoHeapPointers")
2174  && call.Uses == 1
2175  && clobber(call)
2176  => (Zero {types.Types[types.TUINT8]} [int64(c)] sptr mem)
2177
2178// Recognise make([]T, 0) and replace it with a pointer to the zerobase
2179(StaticLECall {callAux} _ (Const(64|32) [0]) (Const(64|32) [0]) mem)
2180	&& isSameCall(callAux, "runtime.makeslice")
2181	=> (MakeResult (Addr <v.Type.FieldType(0)> {ir.Syms.Zerobase} (SB)) mem)
2182
2183// Evaluate constant address comparisons.
2184(EqPtr  x x) => (ConstBool [true])
2185(NeqPtr x x) => (ConstBool [false])
2186(EqPtr  (Addr {x} _) (Addr {y} _)) => (ConstBool [x == y])
2187(EqPtr  (Addr {x} _) (OffPtr [o] (Addr {y} _))) => (ConstBool [x == y && o == 0])
2188(EqPtr  (OffPtr [o1] (Addr {x} _)) (OffPtr [o2] (Addr {y} _))) => (ConstBool [x == y && o1 == o2])
2189(NeqPtr (Addr {x} _) (Addr {y} _)) => (ConstBool [x != y])
2190(NeqPtr (Addr {x} _) (OffPtr [o] (Addr {y} _))) => (ConstBool [x != y || o != 0])
2191(NeqPtr (OffPtr [o1] (Addr {x} _)) (OffPtr [o2] (Addr {y} _))) => (ConstBool [x != y || o1 != o2])
2192(EqPtr  (LocalAddr {x} _ _) (LocalAddr {y} _ _)) => (ConstBool [x == y])
2193(EqPtr  (LocalAddr {x} _ _) (OffPtr [o] (LocalAddr {y} _ _))) => (ConstBool [x == y && o == 0])
2194(EqPtr  (OffPtr [o1] (LocalAddr {x} _ _)) (OffPtr [o2] (LocalAddr {y} _ _))) => (ConstBool [x == y && o1 == o2])
2195(NeqPtr (LocalAddr {x} _ _) (LocalAddr {y} _ _)) => (ConstBool [x != y])
2196(NeqPtr (LocalAddr {x} _ _) (OffPtr [o] (LocalAddr {y} _ _))) => (ConstBool [x != y || o != 0])
2197(NeqPtr (OffPtr [o1] (LocalAddr {x} _ _)) (OffPtr [o2] (LocalAddr {y} _ _))) => (ConstBool [x != y || o1 != o2])
2198(EqPtr  (OffPtr [o1] p1) p2) && isSamePtr(p1, p2) => (ConstBool [o1 == 0])
2199(NeqPtr (OffPtr [o1] p1) p2) && isSamePtr(p1, p2) => (ConstBool [o1 != 0])
2200(EqPtr  (OffPtr [o1] p1) (OffPtr [o2] p2)) && isSamePtr(p1, p2) => (ConstBool [o1 == o2])
2201(NeqPtr (OffPtr [o1] p1) (OffPtr [o2] p2)) && isSamePtr(p1, p2) => (ConstBool [o1 != o2])
2202(EqPtr  (Const(32|64) [c]) (Const(32|64) [d])) => (ConstBool [c == d])
2203(NeqPtr (Const(32|64) [c]) (Const(32|64) [d])) => (ConstBool [c != d])
2204(EqPtr  (Convert (Addr {x} _) _) (Addr {y} _)) => (ConstBool [x==y])
2205(NeqPtr (Convert (Addr {x} _) _) (Addr {y} _)) => (ConstBool [x!=y])
2206
2207(EqPtr  (LocalAddr _ _) (Addr _)) => (ConstBool [false])
2208(EqPtr  (OffPtr (LocalAddr _ _)) (Addr _)) => (ConstBool [false])
2209(EqPtr  (LocalAddr _ _) (OffPtr (Addr _))) => (ConstBool [false])
2210(EqPtr  (OffPtr (LocalAddr _ _)) (OffPtr (Addr _))) => (ConstBool [false])
2211(NeqPtr (LocalAddr _ _) (Addr _)) => (ConstBool [true])
2212(NeqPtr (OffPtr (LocalAddr _ _)) (Addr _)) => (ConstBool [true])
2213(NeqPtr (LocalAddr _ _) (OffPtr (Addr _))) => (ConstBool [true])
2214(NeqPtr (OffPtr (LocalAddr _ _)) (OffPtr (Addr _))) => (ConstBool [true])
2215
2216// Simplify address comparisons.
2217(EqPtr  (AddPtr p1 o1) p2) && isSamePtr(p1, p2) => (Not (IsNonNil o1))
2218(NeqPtr (AddPtr p1 o1) p2) && isSamePtr(p1, p2) => (IsNonNil o1)
2219(EqPtr  (Const(32|64) [0]) p) => (Not (IsNonNil p))
2220(NeqPtr (Const(32|64) [0]) p) => (IsNonNil p)
2221(EqPtr  (ConstNil) p) => (Not (IsNonNil p))
2222(NeqPtr (ConstNil) p) => (IsNonNil p)
2223
2224// Evaluate constant user nil checks.
2225(IsNonNil (ConstNil)) => (ConstBool [false])
2226(IsNonNil (Const(32|64) [c])) => (ConstBool [c != 0])
2227(IsNonNil          (Addr _)   ) => (ConstBool [true])
2228(IsNonNil (Convert (Addr _) _)) => (ConstBool [true])
2229(IsNonNil (LocalAddr _ _)) => (ConstBool [true])
2230
2231// Inline small or disjoint runtime.memmove calls with constant length.
2232// See the comment in op Move in genericOps.go for discussion of the type.
2233//
2234// Note that we've lost any knowledge of the type and alignment requirements
2235// of the source and destination. We only know the size, and that the type
2236// contains no pointers.
2237// The type of the move is not necessarily v.Args[0].Type().Elem()!
2238// See issue 55122 for details.
2239//
2240// Because expand calls runs after prove, constants useful to this pattern may not appear.
2241// Both versions need to exist; the memory and register variants.
2242//
2243// Match post-expansion calls, memory version.
2244(SelectN [0] call:(StaticCall {sym} s1:(Store _ (Const(64|32) [sz]) s2:(Store  _ src s3:(Store {t} _ dst mem)))))
2245	&& sz >= 0
2246	&& isSameCall(sym, "runtime.memmove")
2247	&& s1.Uses == 1 && s2.Uses == 1 && s3.Uses == 1
2248	&& isInlinableMemmove(dst, src, int64(sz), config)
2249	&& clobber(s1, s2, s3, call)
2250	=> (Move {types.Types[types.TUINT8]} [int64(sz)] dst src mem)
2251
2252// Match post-expansion calls, register version.
2253(SelectN [0] call:(StaticCall {sym} dst src (Const(64|32) [sz]) mem))
2254	&& sz >= 0
2255	&& call.Uses == 1 // this will exclude all calls with results
2256	&& isSameCall(sym, "runtime.memmove")
2257	&& isInlinableMemmove(dst, src, int64(sz), config)
2258	&& clobber(call)
2259	=> (Move {types.Types[types.TUINT8]} [int64(sz)] dst src mem)
2260
2261// Match pre-expansion calls.
2262(SelectN [0] call:(StaticLECall {sym} dst src (Const(64|32) [sz]) mem))
2263	&& sz >= 0
2264	&& call.Uses == 1 // this will exclude all calls with results
2265	&& isSameCall(sym, "runtime.memmove")
2266	&& isInlinableMemmove(dst, src, int64(sz), config)
2267	&& clobber(call)
2268	=> (Move {types.Types[types.TUINT8]} [int64(sz)] dst src mem)
2269
2270// De-virtualize late-expanded interface calls into late-expanded static calls.
2271(InterLECall [argsize] {auxCall} (Addr {fn} (SB)) ___) => devirtLECall(v, fn.(*obj.LSym))
2272
2273// Move and Zero optimizations.
2274// Move source and destination may overlap.
2275
2276// Convert Moves into Zeros when the source is known to be zeros.
2277(Move {t} [n] dst1 src mem:(Zero {t} [n] dst2 _)) && isSamePtr(src, dst2)
2278	=> (Zero {t} [n] dst1 mem)
2279(Move {t} [n] dst1 src mem:(VarDef (Zero {t} [n] dst0 _))) && isSamePtr(src, dst0)
2280	=> (Zero {t} [n] dst1 mem)
2281(Move {t} [n] dst (Addr {sym} (SB)) mem) && symIsROZero(sym) => (Zero {t} [n] dst mem)
2282
2283// Don't Store to variables that are about to be overwritten by Move/Zero.
2284(Zero {t1} [n] p1 store:(Store {t2} (OffPtr [o2] p2) _ mem))
2285	&& isSamePtr(p1, p2) && store.Uses == 1
2286	&& n >= o2 + t2.Size()
2287	&& clobber(store)
2288	=> (Zero {t1} [n] p1 mem)
2289(Move {t1} [n] dst1 src1 store:(Store {t2} op:(OffPtr [o2] dst2) _ mem))
2290	&& isSamePtr(dst1, dst2) && store.Uses == 1
2291	&& n >= o2 + t2.Size()
2292	&& disjoint(src1, n, op, t2.Size())
2293	&& clobber(store)
2294	=> (Move {t1} [n] dst1 src1 mem)
2295
2296// Don't Move to variables that are immediately completely overwritten.
2297(Zero {t} [n] dst1 move:(Move {t} [n] dst2 _ mem))
2298	&& move.Uses == 1
2299	&& isSamePtr(dst1, dst2)
2300	&& clobber(move)
2301	=> (Zero {t} [n] dst1 mem)
2302(Move {t} [n] dst1 src1 move:(Move {t} [n] dst2 _ mem))
2303	&& move.Uses == 1
2304	&& isSamePtr(dst1, dst2) && disjoint(src1, n, dst2, n)
2305	&& clobber(move)
2306	=> (Move {t} [n] dst1 src1 mem)
2307(Zero {t} [n] dst1 vardef:(VarDef {x} move:(Move {t} [n] dst2 _ mem)))
2308	&& move.Uses == 1 && vardef.Uses == 1
2309	&& isSamePtr(dst1, dst2)
2310	&& clobber(move, vardef)
2311	=> (Zero {t} [n] dst1 (VarDef {x} mem))
2312(Move {t} [n] dst1 src1 vardef:(VarDef {x} move:(Move {t} [n] dst2 _ mem)))
2313	&& move.Uses == 1 && vardef.Uses == 1
2314	&& isSamePtr(dst1, dst2) && disjoint(src1, n, dst2, n)
2315	&& clobber(move, vardef)
2316	=> (Move {t} [n] dst1 src1 (VarDef {x} mem))
2317(Store {t1} op1:(OffPtr [o1] p1) d1
2318	m2:(Store {t2} op2:(OffPtr [0] p2) d2
2319		m3:(Move [n] p3 _ mem)))
2320	&& m2.Uses == 1 && m3.Uses == 1
2321	&& o1 == t2.Size()
2322	&& n == t2.Size() + t1.Size()
2323	&& isSamePtr(p1, p2) && isSamePtr(p2, p3)
2324	&& clobber(m2, m3)
2325	=> (Store {t1} op1 d1 (Store {t2} op2 d2 mem))
2326(Store {t1} op1:(OffPtr [o1] p1) d1
2327	m2:(Store {t2} op2:(OffPtr [o2] p2) d2
2328		m3:(Store {t3} op3:(OffPtr [0] p3) d3
2329			m4:(Move [n] p4 _ mem))))
2330	&& m2.Uses == 1 && m3.Uses == 1 && m4.Uses == 1
2331	&& o2 == t3.Size()
2332	&& o1-o2 == t2.Size()
2333	&& n == t3.Size() + t2.Size() + t1.Size()
2334	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4)
2335	&& clobber(m2, m3, m4)
2336	=> (Store {t1} op1 d1 (Store {t2} op2 d2 (Store {t3} op3 d3 mem)))
2337(Store {t1} op1:(OffPtr [o1] p1) d1
2338	m2:(Store {t2} op2:(OffPtr [o2] p2) d2
2339		m3:(Store {t3} op3:(OffPtr [o3] p3) d3
2340			m4:(Store {t4} op4:(OffPtr [0] p4) d4
2341				m5:(Move [n] p5 _ mem)))))
2342	&& m2.Uses == 1 && m3.Uses == 1 && m4.Uses == 1 && m5.Uses == 1
2343	&& o3 == t4.Size()
2344	&& o2-o3 == t3.Size()
2345	&& o1-o2 == t2.Size()
2346	&& n == t4.Size() + t3.Size() + t2.Size() + t1.Size()
2347	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5)
2348	&& clobber(m2, m3, m4, m5)
2349	=> (Store {t1} op1 d1 (Store {t2} op2 d2 (Store {t3} op3 d3 (Store {t4} op4 d4 mem))))
2350
2351// Don't Zero variables that are immediately completely overwritten
2352// before being accessed.
2353(Move {t} [n] dst1 src1 zero:(Zero {t} [n] dst2 mem))
2354	&& zero.Uses == 1
2355	&& isSamePtr(dst1, dst2) && disjoint(src1, n, dst2, n)
2356	&& clobber(zero)
2357	=> (Move {t} [n] dst1 src1 mem)
2358(Move {t} [n] dst1 src1 vardef:(VarDef {x} zero:(Zero {t} [n] dst2 mem)))
2359	&& zero.Uses == 1 && vardef.Uses == 1
2360	&& isSamePtr(dst1, dst2) && disjoint(src1, n, dst2, n)
2361	&& clobber(zero, vardef)
2362	=> (Move {t} [n] dst1 src1 (VarDef {x} mem))
2363(Store {t1} op1:(OffPtr [o1] p1) d1
2364	m2:(Store {t2} op2:(OffPtr [0] p2) d2
2365		m3:(Zero [n] p3 mem)))
2366	&& m2.Uses == 1 && m3.Uses == 1
2367	&& o1 == t2.Size()
2368	&& n == t2.Size() + t1.Size()
2369	&& isSamePtr(p1, p2) && isSamePtr(p2, p3)
2370	&& clobber(m2, m3)
2371	=> (Store {t1} op1 d1 (Store {t2} op2 d2 mem))
2372(Store {t1} op1:(OffPtr [o1] p1) d1
2373	m2:(Store {t2} op2:(OffPtr [o2] p2) d2
2374		m3:(Store {t3} op3:(OffPtr [0] p3) d3
2375			m4:(Zero [n] p4 mem))))
2376	&& m2.Uses == 1 && m3.Uses == 1 && m4.Uses == 1
2377	&& o2 == t3.Size()
2378	&& o1-o2 == t2.Size()
2379	&& n == t3.Size() + t2.Size() + t1.Size()
2380	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4)
2381	&& clobber(m2, m3, m4)
2382	=> (Store {t1} op1 d1 (Store {t2} op2 d2 (Store {t3} op3 d3 mem)))
2383(Store {t1} op1:(OffPtr [o1] p1) d1
2384	m2:(Store {t2} op2:(OffPtr [o2] p2) d2
2385		m3:(Store {t3} op3:(OffPtr [o3] p3) d3
2386			m4:(Store {t4} op4:(OffPtr [0] p4) d4
2387				m5:(Zero [n] p5 mem)))))
2388	&& m2.Uses == 1 && m3.Uses == 1 && m4.Uses == 1 && m5.Uses == 1
2389	&& o3 == t4.Size()
2390	&& o2-o3 == t3.Size()
2391	&& o1-o2 == t2.Size()
2392	&& n == t4.Size() + t3.Size() + t2.Size() + t1.Size()
2393	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5)
2394	&& clobber(m2, m3, m4, m5)
2395	=> (Store {t1} op1 d1 (Store {t2} op2 d2 (Store {t3} op3 d3 (Store {t4} op4 d4 mem))))
2396
2397// Don't Move from memory if the values are likely to already be
2398// in registers.
2399(Move {t1} [n] dst p1
2400	mem:(Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
2401		(Store {t3} op3:(OffPtr <tt3> [0] p3) d2 _)))
2402	&& isSamePtr(p1, p2) && isSamePtr(p2, p3)
2403	&& t2.Alignment() <= t1.Alignment()
2404	&& t3.Alignment() <= t1.Alignment()
2405	&& registerizable(b, t2)
2406	&& registerizable(b, t3)
2407	&& o2 == t3.Size()
2408	&& n == t2.Size() + t3.Size()
2409	=> (Store {t2} (OffPtr <tt2> [o2] dst) d1
2410		(Store {t3} (OffPtr <tt3> [0] dst) d2 mem))
2411(Move {t1} [n] dst p1
2412	mem:(Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
2413		(Store {t3} op3:(OffPtr <tt3> [o3] p3) d2
2414			(Store {t4} op4:(OffPtr <tt4> [0] p4) d3 _))))
2415	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4)
2416	&& t2.Alignment() <= t1.Alignment()
2417	&& t3.Alignment() <= t1.Alignment()
2418	&& t4.Alignment() <= t1.Alignment()
2419	&& registerizable(b, t2)
2420	&& registerizable(b, t3)
2421	&& registerizable(b, t4)
2422	&& o3 == t4.Size()
2423	&& o2-o3 == t3.Size()
2424	&& n == t2.Size() + t3.Size() + t4.Size()
2425	=> (Store {t2} (OffPtr <tt2> [o2] dst) d1
2426		(Store {t3} (OffPtr <tt3> [o3] dst) d2
2427			(Store {t4} (OffPtr <tt4> [0] dst) d3 mem)))
2428(Move {t1} [n] dst p1
2429	mem:(Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
2430		(Store {t3} op3:(OffPtr <tt3> [o3] p3) d2
2431			(Store {t4} op4:(OffPtr <tt4> [o4] p4) d3
2432				(Store {t5} op5:(OffPtr <tt5> [0] p5) d4 _)))))
2433	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5)
2434	&& t2.Alignment() <= t1.Alignment()
2435	&& t3.Alignment() <= t1.Alignment()
2436	&& t4.Alignment() <= t1.Alignment()
2437	&& t5.Alignment() <= t1.Alignment()
2438	&& registerizable(b, t2)
2439	&& registerizable(b, t3)
2440	&& registerizable(b, t4)
2441	&& registerizable(b, t5)
2442	&& o4 == t5.Size()
2443	&& o3-o4 == t4.Size()
2444	&& o2-o3 == t3.Size()
2445	&& n == t2.Size() + t3.Size() + t4.Size() + t5.Size()
2446	=> (Store {t2} (OffPtr <tt2> [o2] dst) d1
2447		(Store {t3} (OffPtr <tt3> [o3] dst) d2
2448			(Store {t4} (OffPtr <tt4> [o4] dst) d3
2449				(Store {t5} (OffPtr <tt5> [0] dst) d4 mem))))
2450
2451// Same thing but with VarDef in the middle.
2452(Move {t1} [n] dst p1
2453	mem:(VarDef
2454		(Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
2455			(Store {t3} op3:(OffPtr <tt3> [0] p3) d2 _))))
2456	&& isSamePtr(p1, p2) && isSamePtr(p2, p3)
2457	&& t2.Alignment() <= t1.Alignment()
2458	&& t3.Alignment() <= t1.Alignment()
2459	&& registerizable(b, t2)
2460	&& registerizable(b, t3)
2461	&& o2 == t3.Size()
2462	&& n == t2.Size() + t3.Size()
2463	=> (Store {t2} (OffPtr <tt2> [o2] dst) d1
2464		(Store {t3} (OffPtr <tt3> [0] dst) d2 mem))
2465(Move {t1} [n] dst p1
2466	mem:(VarDef
2467		(Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
2468			(Store {t3} op3:(OffPtr <tt3> [o3] p3) d2
2469				(Store {t4} op4:(OffPtr <tt4> [0] p4) d3 _)))))
2470	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4)
2471	&& t2.Alignment() <= t1.Alignment()
2472	&& t3.Alignment() <= t1.Alignment()
2473	&& t4.Alignment() <= t1.Alignment()
2474	&& registerizable(b, t2)
2475	&& registerizable(b, t3)
2476	&& registerizable(b, t4)
2477	&& o3 == t4.Size()
2478	&& o2-o3 == t3.Size()
2479	&& n == t2.Size() + t3.Size() + t4.Size()
2480	=> (Store {t2} (OffPtr <tt2> [o2] dst) d1
2481		(Store {t3} (OffPtr <tt3> [o3] dst) d2
2482			(Store {t4} (OffPtr <tt4> [0] dst) d3 mem)))
2483(Move {t1} [n] dst p1
2484	mem:(VarDef
2485		(Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
2486			(Store {t3} op3:(OffPtr <tt3> [o3] p3) d2
2487				(Store {t4} op4:(OffPtr <tt4> [o4] p4) d3
2488					(Store {t5} op5:(OffPtr <tt5> [0] p5) d4 _))))))
2489	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5)
2490	&& t2.Alignment() <= t1.Alignment()
2491	&& t3.Alignment() <= t1.Alignment()
2492	&& t4.Alignment() <= t1.Alignment()
2493	&& t5.Alignment() <= t1.Alignment()
2494	&& registerizable(b, t2)
2495	&& registerizable(b, t3)
2496	&& registerizable(b, t4)
2497	&& registerizable(b, t5)
2498	&& o4 == t5.Size()
2499	&& o3-o4 == t4.Size()
2500	&& o2-o3 == t3.Size()
2501	&& n == t2.Size() + t3.Size() + t4.Size() + t5.Size()
2502	=> (Store {t2} (OffPtr <tt2> [o2] dst) d1
2503		(Store {t3} (OffPtr <tt3> [o3] dst) d2
2504			(Store {t4} (OffPtr <tt4> [o4] dst) d3
2505				(Store {t5} (OffPtr <tt5> [0] dst) d4 mem))))
2506
2507// Prefer to Zero and Store than to Move.
2508(Move {t1} [n] dst p1
2509	mem:(Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
2510		(Zero {t3} [n] p3 _)))
2511	&& isSamePtr(p1, p2) && isSamePtr(p2, p3)
2512	&& t2.Alignment() <= t1.Alignment()
2513	&& t3.Alignment() <= t1.Alignment()
2514	&& registerizable(b, t2)
2515	&& n >= o2 + t2.Size()
2516	=> (Store {t2} (OffPtr <tt2> [o2] dst) d1
2517		(Zero {t1} [n] dst mem))
2518(Move {t1} [n] dst p1
2519	mem:(Store {t2} (OffPtr <tt2> [o2] p2) d1
2520		(Store {t3} (OffPtr <tt3> [o3] p3) d2
2521			(Zero {t4} [n] p4 _))))
2522	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4)
2523	&& t2.Alignment() <= t1.Alignment()
2524	&& t3.Alignment() <= t1.Alignment()
2525	&& t4.Alignment() <= t1.Alignment()
2526	&& registerizable(b, t2)
2527	&& registerizable(b, t3)
2528	&& n >= o2 + t2.Size()
2529	&& n >= o3 + t3.Size()
2530	=> (Store {t2} (OffPtr <tt2> [o2] dst) d1
2531		(Store {t3} (OffPtr <tt3> [o3] dst) d2
2532			(Zero {t1} [n] dst mem)))
2533(Move {t1} [n] dst p1
2534	mem:(Store {t2} (OffPtr <tt2> [o2] p2) d1
2535		(Store {t3} (OffPtr <tt3> [o3] p3) d2
2536			(Store {t4} (OffPtr <tt4> [o4] p4) d3
2537				(Zero {t5} [n] p5 _)))))
2538	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5)
2539	&& t2.Alignment() <= t1.Alignment()
2540	&& t3.Alignment() <= t1.Alignment()
2541	&& t4.Alignment() <= t1.Alignment()
2542	&& t5.Alignment() <= t1.Alignment()
2543	&& registerizable(b, t2)
2544	&& registerizable(b, t3)
2545	&& registerizable(b, t4)
2546	&& n >= o2 + t2.Size()
2547	&& n >= o3 + t3.Size()
2548	&& n >= o4 + t4.Size()
2549	=> (Store {t2} (OffPtr <tt2> [o2] dst) d1
2550		(Store {t3} (OffPtr <tt3> [o3] dst) d2
2551			(Store {t4} (OffPtr <tt4> [o4] dst) d3
2552				(Zero {t1} [n] dst mem))))
2553(Move {t1} [n] dst p1
2554	mem:(Store {t2} (OffPtr <tt2> [o2] p2) d1
2555		(Store {t3} (OffPtr <tt3> [o3] p3) d2
2556			(Store {t4} (OffPtr <tt4> [o4] p4) d3
2557				(Store {t5} (OffPtr <tt5> [o5] p5) d4
2558					(Zero {t6} [n] p6 _))))))
2559	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5) && isSamePtr(p5, p6)
2560	&& t2.Alignment() <= t1.Alignment()
2561	&& t3.Alignment() <= t1.Alignment()
2562	&& t4.Alignment() <= t1.Alignment()
2563	&& t5.Alignment() <= t1.Alignment()
2564	&& t6.Alignment() <= t1.Alignment()
2565	&& registerizable(b, t2)
2566	&& registerizable(b, t3)
2567	&& registerizable(b, t4)
2568	&& registerizable(b, t5)
2569	&& n >= o2 + t2.Size()
2570	&& n >= o3 + t3.Size()
2571	&& n >= o4 + t4.Size()
2572	&& n >= o5 + t5.Size()
2573	=> (Store {t2} (OffPtr <tt2> [o2] dst) d1
2574		(Store {t3} (OffPtr <tt3> [o3] dst) d2
2575			(Store {t4} (OffPtr <tt4> [o4] dst) d3
2576				(Store {t5} (OffPtr <tt5> [o5] dst) d4
2577					(Zero {t1} [n] dst mem)))))
2578(Move {t1} [n] dst p1
2579	mem:(VarDef
2580		(Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
2581			(Zero {t3} [n] p3 _))))
2582	&& isSamePtr(p1, p2) && isSamePtr(p2, p3)
2583	&& t2.Alignment() <= t1.Alignment()
2584	&& t3.Alignment() <= t1.Alignment()
2585	&& registerizable(b, t2)
2586	&& n >= o2 + t2.Size()
2587	=> (Store {t2} (OffPtr <tt2> [o2] dst) d1
2588		(Zero {t1} [n] dst mem))
2589(Move {t1} [n] dst p1
2590	mem:(VarDef
2591		(Store {t2} (OffPtr <tt2> [o2] p2) d1
2592			(Store {t3} (OffPtr <tt3> [o3] p3) d2
2593				(Zero {t4} [n] p4 _)))))
2594	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4)
2595	&& t2.Alignment() <= t1.Alignment()
2596	&& t3.Alignment() <= t1.Alignment()
2597	&& t4.Alignment() <= t1.Alignment()
2598	&& registerizable(b, t2)
2599	&& registerizable(b, t3)
2600	&& n >= o2 + t2.Size()
2601	&& n >= o3 + t3.Size()
2602	=> (Store {t2} (OffPtr <tt2> [o2] dst) d1
2603		(Store {t3} (OffPtr <tt3> [o3] dst) d2
2604			(Zero {t1} [n] dst mem)))
2605(Move {t1} [n] dst p1
2606	mem:(VarDef
2607		(Store {t2} (OffPtr <tt2> [o2] p2) d1
2608			(Store {t3} (OffPtr <tt3> [o3] p3) d2
2609				(Store {t4} (OffPtr <tt4> [o4] p4) d3
2610					(Zero {t5} [n] p5 _))))))
2611	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5)
2612	&& t2.Alignment() <= t1.Alignment()
2613	&& t3.Alignment() <= t1.Alignment()
2614	&& t4.Alignment() <= t1.Alignment()
2615	&& t5.Alignment() <= t1.Alignment()
2616	&& registerizable(b, t2)
2617	&& registerizable(b, t3)
2618	&& registerizable(b, t4)
2619	&& n >= o2 + t2.Size()
2620	&& n >= o3 + t3.Size()
2621	&& n >= o4 + t4.Size()
2622	=> (Store {t2} (OffPtr <tt2> [o2] dst) d1
2623		(Store {t3} (OffPtr <tt3> [o3] dst) d2
2624			(Store {t4} (OffPtr <tt4> [o4] dst) d3
2625				(Zero {t1} [n] dst mem))))
2626(Move {t1} [n] dst p1
2627	mem:(VarDef
2628		(Store {t2} (OffPtr <tt2> [o2] p2) d1
2629			(Store {t3} (OffPtr <tt3> [o3] p3) d2
2630				(Store {t4} (OffPtr <tt4> [o4] p4) d3
2631					(Store {t5} (OffPtr <tt5> [o5] p5) d4
2632						(Zero {t6} [n] p6 _)))))))
2633	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5) && isSamePtr(p5, p6)
2634	&& t2.Alignment() <= t1.Alignment()
2635	&& t3.Alignment() <= t1.Alignment()
2636	&& t4.Alignment() <= t1.Alignment()
2637	&& t5.Alignment() <= t1.Alignment()
2638	&& t6.Alignment() <= t1.Alignment()
2639	&& registerizable(b, t2)
2640	&& registerizable(b, t3)
2641	&& registerizable(b, t4)
2642	&& registerizable(b, t5)
2643	&& n >= o2 + t2.Size()
2644	&& n >= o3 + t3.Size()
2645	&& n >= o4 + t4.Size()
2646	&& n >= o5 + t5.Size()
2647	=> (Store {t2} (OffPtr <tt2> [o2] dst) d1
2648		(Store {t3} (OffPtr <tt3> [o3] dst) d2
2649			(Store {t4} (OffPtr <tt4> [o4] dst) d3
2650				(Store {t5} (OffPtr <tt5> [o5] dst) d4
2651					(Zero {t1} [n] dst mem)))))
2652
2653(SelectN [0] call:(StaticLECall {sym} a x)) && needRaceCleanup(sym, call) && clobber(call) => x
2654(SelectN [0] call:(StaticLECall {sym} x)) && needRaceCleanup(sym, call) && clobber(call) => x
2655
2656// When rewriting append to growslice, we use as the new length the result of
2657// growslice so that we don't have to spill/restore the new length around the growslice call.
2658// The exception here is that if the new length is a constant, avoiding spilling it
2659// is pointless and its constantness is sometimes useful for subsequent optimizations.
2660// See issue 56440.
2661// Note there are 2 rules here, one for the pre-decomposed []T result and one for
2662// the post-decomposed (*T,int,int) result. (The latter is generated after call expansion.)
2663(SliceLen (SelectN [0] (StaticLECall {sym} _ newLen:(Const(64|32)) _ _ _ _))) && isSameCall(sym, "runtime.growslice") => newLen
2664(SelectN [1] (StaticCall {sym} _ newLen:(Const(64|32)) _ _ _ _)) && v.Type.IsInteger() && isSameCall(sym, "runtime.growslice") => newLen
2665
2666// Collapse moving A -> B -> C into just A -> C.
2667// Later passes (deadstore, elim unread auto) will remove the A -> B move, if possible.
2668// This happens most commonly when B is an autotmp inserted earlier
2669// during compilation to ensure correctness.
2670// Take care that overlapping moves are preserved.
2671// Restrict this optimization to the stack, to avoid duplicating loads from the heap;
2672// see CL 145208 for discussion.
2673(Move {t1} [s] dst tmp1 midmem:(Move {t2} [s] tmp2 src _))
2674	&& t1.Compare(t2) == types.CMPeq
2675	&& isSamePtr(tmp1, tmp2)
2676	&& isStackPtr(src) && !isVolatile(src)
2677	&& disjoint(src, s, tmp2, s)
2678	&& (disjoint(src, s, dst, s) || isInlinableMemmove(dst, src, s, config))
2679	=> (Move {t1} [s] dst src midmem)
2680
2681// Same, but for large types that require VarDefs.
2682(Move {t1} [s] dst tmp1 midmem:(VarDef (Move {t2} [s] tmp2 src _)))
2683	&& t1.Compare(t2) == types.CMPeq
2684	&& isSamePtr(tmp1, tmp2)
2685	&& isStackPtr(src) && !isVolatile(src)
2686	&& disjoint(src, s, tmp2, s)
2687	&& (disjoint(src, s, dst, s) || isInlinableMemmove(dst, src, s, config))
2688	=> (Move {t1} [s] dst src midmem)
2689
2690// Don't zero the same bits twice.
2691(Zero {t} [s] dst1 zero:(Zero {t} [s] dst2 _)) && isSamePtr(dst1, dst2) => zero
2692(Zero {t} [s] dst1 vardef:(VarDef (Zero {t} [s] dst2 _))) && isSamePtr(dst1, dst2) => vardef
2693
2694// Elide self-moves. This only happens rarely (e.g test/fixedbugs/bug277.go).
2695// However, this rule is needed to prevent the previous rule from looping forever in such cases.
2696(Move dst src mem) && isSamePtr(dst, src) => mem
2697
2698// Constant rotate detection.
2699((Add64|Or64|Xor64) (Lsh64x64 x z:(Const64 <t> [c])) (Rsh64Ux64 x (Const64 [d]))) && c < 64 && d == 64-c && canRotate(config, 64) => (RotateLeft64 x z)
2700((Add32|Or32|Xor32) (Lsh32x64 x z:(Const64 <t> [c])) (Rsh32Ux64 x (Const64 [d]))) && c < 32 && d == 32-c && canRotate(config, 32) => (RotateLeft32 x z)
2701((Add16|Or16|Xor16) (Lsh16x64 x z:(Const64 <t> [c])) (Rsh16Ux64 x (Const64 [d]))) && c < 16 && d == 16-c && canRotate(config, 16) => (RotateLeft16 x z)
2702((Add8|Or8|Xor8) (Lsh8x64 x z:(Const64 <t> [c])) (Rsh8Ux64 x (Const64 [d]))) && c < 8 && d == 8-c && canRotate(config, 8) => (RotateLeft8 x z)
2703
2704// Non-constant rotate detection.
2705// We use shiftIsBounded to make sure that neither of the shifts are >64.
2706// Note: these rules are subtle when the shift amounts are 0/64, as Go shifts
2707// are different from most native shifts. But it works out.
2708((Add64|Or64|Xor64) left:(Lsh64x64 x y) right:(Rsh64Ux64 x (Sub64 (Const64 [64]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 64) => (RotateLeft64 x y)
2709((Add64|Or64|Xor64) left:(Lsh64x32 x y) right:(Rsh64Ux32 x (Sub32 (Const32 [64]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 64) => (RotateLeft64 x y)
2710((Add64|Or64|Xor64) left:(Lsh64x16 x y) right:(Rsh64Ux16 x (Sub16 (Const16 [64]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 64) => (RotateLeft64 x y)
2711((Add64|Or64|Xor64) left:(Lsh64x8  x y) right:(Rsh64Ux8  x (Sub8  (Const8  [64]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 64) => (RotateLeft64 x y)
2712
2713((Add64|Or64|Xor64) right:(Rsh64Ux64 x y) left:(Lsh64x64 x z:(Sub64 (Const64 [64]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 64) => (RotateLeft64 x z)
2714((Add64|Or64|Xor64) right:(Rsh64Ux32 x y) left:(Lsh64x32 x z:(Sub32 (Const32 [64]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 64) => (RotateLeft64 x z)
2715((Add64|Or64|Xor64) right:(Rsh64Ux16 x y) left:(Lsh64x16 x z:(Sub16 (Const16 [64]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 64) => (RotateLeft64 x z)
2716((Add64|Or64|Xor64) right:(Rsh64Ux8  x y) left:(Lsh64x8  x z:(Sub8  (Const8  [64]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 64) => (RotateLeft64 x z)
2717
2718((Add32|Or32|Xor32) left:(Lsh32x64 x y) right:(Rsh32Ux64 x (Sub64 (Const64 [32]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 32) => (RotateLeft32 x y)
2719((Add32|Or32|Xor32) left:(Lsh32x32 x y) right:(Rsh32Ux32 x (Sub32 (Const32 [32]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 32) => (RotateLeft32 x y)
2720((Add32|Or32|Xor32) left:(Lsh32x16 x y) right:(Rsh32Ux16 x (Sub16 (Const16 [32]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 32) => (RotateLeft32 x y)
2721((Add32|Or32|Xor32) left:(Lsh32x8  x y) right:(Rsh32Ux8  x (Sub8  (Const8  [32]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 32) => (RotateLeft32 x y)
2722
2723((Add32|Or32|Xor32) right:(Rsh32Ux64 x y) left:(Lsh32x64 x z:(Sub64 (Const64 [32]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 32) => (RotateLeft32 x z)
2724((Add32|Or32|Xor32) right:(Rsh32Ux32 x y) left:(Lsh32x32 x z:(Sub32 (Const32 [32]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 32) => (RotateLeft32 x z)
2725((Add32|Or32|Xor32) right:(Rsh32Ux16 x y) left:(Lsh32x16 x z:(Sub16 (Const16 [32]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 32) => (RotateLeft32 x z)
2726((Add32|Or32|Xor32) right:(Rsh32Ux8  x y) left:(Lsh32x8  x z:(Sub8  (Const8  [32]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 32) => (RotateLeft32 x z)
2727
2728((Add16|Or16|Xor16) left:(Lsh16x64 x y) right:(Rsh16Ux64 x (Sub64 (Const64 [16]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 16) => (RotateLeft16 x y)
2729((Add16|Or16|Xor16) left:(Lsh16x32 x y) right:(Rsh16Ux32 x (Sub32 (Const32 [16]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 16) => (RotateLeft16 x y)
2730((Add16|Or16|Xor16) left:(Lsh16x16 x y) right:(Rsh16Ux16 x (Sub16 (Const16 [16]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 16) => (RotateLeft16 x y)
2731((Add16|Or16|Xor16) left:(Lsh16x8  x y) right:(Rsh16Ux8  x (Sub8  (Const8  [16]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 16) => (RotateLeft16 x y)
2732
2733((Add16|Or16|Xor16) right:(Rsh16Ux64 x y) left:(Lsh16x64 x z:(Sub64 (Const64 [16]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 16) => (RotateLeft16 x z)
2734((Add16|Or16|Xor16) right:(Rsh16Ux32 x y) left:(Lsh16x32 x z:(Sub32 (Const32 [16]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 16) => (RotateLeft16 x z)
2735((Add16|Or16|Xor16) right:(Rsh16Ux16 x y) left:(Lsh16x16 x z:(Sub16 (Const16 [16]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 16) => (RotateLeft16 x z)
2736((Add16|Or16|Xor16) right:(Rsh16Ux8  x y) left:(Lsh16x8  x z:(Sub8  (Const8  [16]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 16) => (RotateLeft16 x z)
2737
2738((Add8|Or8|Xor8) left:(Lsh8x64 x y) right:(Rsh8Ux64 x (Sub64 (Const64 [8]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 8) => (RotateLeft8 x y)
2739((Add8|Or8|Xor8) left:(Lsh8x32 x y) right:(Rsh8Ux32 x (Sub32 (Const32 [8]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 8) => (RotateLeft8 x y)
2740((Add8|Or8|Xor8) left:(Lsh8x16 x y) right:(Rsh8Ux16 x (Sub16 (Const16 [8]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 8) => (RotateLeft8 x y)
2741((Add8|Or8|Xor8) left:(Lsh8x8  x y) right:(Rsh8Ux8  x (Sub8  (Const8  [8]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 8) => (RotateLeft8 x y)
2742
2743((Add8|Or8|Xor8) right:(Rsh8Ux64 x y) left:(Lsh8x64 x z:(Sub64 (Const64 [8]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 8) => (RotateLeft8 x z)
2744((Add8|Or8|Xor8) right:(Rsh8Ux32 x y) left:(Lsh8x32 x z:(Sub32 (Const32 [8]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 8) => (RotateLeft8 x z)
2745((Add8|Or8|Xor8) right:(Rsh8Ux16 x y) left:(Lsh8x16 x z:(Sub16 (Const16 [8]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 8) => (RotateLeft8 x z)
2746((Add8|Or8|Xor8) right:(Rsh8Ux8  x y) left:(Lsh8x8  x z:(Sub8  (Const8  [8]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 8) => (RotateLeft8 x z)
2747
2748// Rotating by y&c, with c a mask that doesn't change the bottom bits, is the same as rotating by y.
2749(RotateLeft64 x (And(64|32|16|8) y (Const(64|32|16|8) [c]))) && c&63 == 63 => (RotateLeft64 x y)
2750(RotateLeft32 x (And(64|32|16|8) y (Const(64|32|16|8) [c]))) && c&31 == 31 => (RotateLeft32 x y)
2751(RotateLeft16 x (And(64|32|16|8) y (Const(64|32|16|8) [c]))) && c&15 == 15 => (RotateLeft16 x y)
2752(RotateLeft8  x (And(64|32|16|8) y (Const(64|32|16|8) [c]))) && c&7  == 7  => (RotateLeft8  x y)
2753
2754// Rotating by -(y&c), with c a mask that doesn't change the bottom bits, is the same as rotating by -y.
2755(RotateLeft64 x (Neg(64|32|16|8) (And(64|32|16|8) y (Const(64|32|16|8) [c])))) && c&63 == 63 => (RotateLeft64 x (Neg(64|32|16|8) <y.Type> y))
2756(RotateLeft32 x (Neg(64|32|16|8) (And(64|32|16|8) y (Const(64|32|16|8) [c])))) && c&31 == 31 => (RotateLeft32 x (Neg(64|32|16|8) <y.Type> y))
2757(RotateLeft16 x (Neg(64|32|16|8) (And(64|32|16|8) y (Const(64|32|16|8) [c])))) && c&15 == 15 => (RotateLeft16 x (Neg(64|32|16|8) <y.Type> y))
2758(RotateLeft8  x (Neg(64|32|16|8) (And(64|32|16|8) y (Const(64|32|16|8) [c])))) && c&7  == 7  => (RotateLeft8  x (Neg(64|32|16|8) <y.Type> y))
2759
2760// Rotating by y+c, with c a multiple of the value width, is the same as rotating by y.
2761(RotateLeft64 x (Add(64|32|16|8) y (Const(64|32|16|8) [c]))) && c&63 == 0 => (RotateLeft64 x y)
2762(RotateLeft32 x (Add(64|32|16|8) y (Const(64|32|16|8) [c]))) && c&31 == 0 => (RotateLeft32 x y)
2763(RotateLeft16 x (Add(64|32|16|8) y (Const(64|32|16|8) [c]))) && c&15 == 0 => (RotateLeft16 x y)
2764(RotateLeft8  x (Add(64|32|16|8) y (Const(64|32|16|8) [c]))) && c&7  == 0 => (RotateLeft8  x y)
2765
2766// Rotating by c-y, with c a multiple of the value width, is the same as rotating by -y.
2767(RotateLeft64 x (Sub(64|32|16|8) (Const(64|32|16|8) [c]) y)) && c&63 == 0 => (RotateLeft64 x (Neg(64|32|16|8) <y.Type> y))
2768(RotateLeft32 x (Sub(64|32|16|8) (Const(64|32|16|8) [c]) y)) && c&31 == 0 => (RotateLeft32 x (Neg(64|32|16|8) <y.Type> y))
2769(RotateLeft16 x (Sub(64|32|16|8) (Const(64|32|16|8) [c]) y)) && c&15 == 0 => (RotateLeft16 x (Neg(64|32|16|8) <y.Type> y))
2770(RotateLeft8  x (Sub(64|32|16|8) (Const(64|32|16|8) [c]) y)) && c&7  == 0 => (RotateLeft8  x (Neg(64|32|16|8) <y.Type> y))
2771
2772// Ensure we don't do Const64 rotates in a 32-bit system.
2773(RotateLeft64 x (Const64 <t> [c])) && config.PtrSize == 4 => (RotateLeft64 x (Const32 <t> [int32(c)]))
2774(RotateLeft32 x (Const64 <t> [c])) && config.PtrSize == 4 => (RotateLeft32 x (Const32 <t> [int32(c)]))
2775(RotateLeft16 x (Const64 <t> [c])) && config.PtrSize == 4 => (RotateLeft16 x (Const32 <t> [int32(c)]))
2776(RotateLeft8  x (Const64 <t> [c])) && config.PtrSize == 4 => (RotateLeft8  x (Const32 <t> [int32(c)]))
2777
2778// Rotating by c, then by d, is the same as rotating by c+d.
2779// We're trading a rotate for an add, which seems generally a good choice. It is especially good when c and d are constants.
2780// This rule is a bit tricky as c and d might be different widths. We handle only cases where they are the same width.
2781(RotateLeft(64|32|16|8) (RotateLeft(64|32|16|8) x c) d) && c.Type.Size() == 8 && d.Type.Size() == 8 => (RotateLeft(64|32|16|8) x (Add64 <c.Type> c d))
2782(RotateLeft(64|32|16|8) (RotateLeft(64|32|16|8) x c) d) && c.Type.Size() == 4 && d.Type.Size() == 4 => (RotateLeft(64|32|16|8) x (Add32 <c.Type> c d))
2783(RotateLeft(64|32|16|8) (RotateLeft(64|32|16|8) x c) d) && c.Type.Size() == 2 && d.Type.Size() == 2 => (RotateLeft(64|32|16|8) x (Add16 <c.Type> c d))
2784(RotateLeft(64|32|16|8) (RotateLeft(64|32|16|8) x c) d) && c.Type.Size() == 1 && d.Type.Size() == 1 => (RotateLeft(64|32|16|8) x (Add8  <c.Type> c d))
2785
2786// Loading constant values from dictionaries and itabs.
2787(Load <t> (OffPtr [off]                       (Addr {s} sb)       ) _) && t.IsUintptr() && isFixedSym(s, off) => (Addr {fixedSym(b.Func, s, off)} sb)
2788(Load <t> (OffPtr [off]              (Convert (Addr {s} sb) _)    ) _) && t.IsUintptr() && isFixedSym(s, off) => (Addr {fixedSym(b.Func, s, off)} sb)
2789(Load <t> (OffPtr [off] (ITab (IMake          (Addr {s} sb)    _))) _) && t.IsUintptr() && isFixedSym(s, off) => (Addr {fixedSym(b.Func, s, off)} sb)
2790(Load <t> (OffPtr [off] (ITab (IMake (Convert (Addr {s} sb) _) _))) _) && t.IsUintptr() && isFixedSym(s, off) => (Addr {fixedSym(b.Func, s, off)} sb)
2791
2792// Loading constant values from runtime._type.hash.
2793(Load <t> (OffPtr [off]                       (Addr {sym} _)       ) _) && t.IsInteger() && t.Size() == 4 && isFixed32(config, sym, off) => (Const32 [fixed32(config, sym, off)])
2794(Load <t> (OffPtr [off]              (Convert (Addr {sym} _) _)    ) _) && t.IsInteger() && t.Size() == 4 && isFixed32(config, sym, off) => (Const32 [fixed32(config, sym, off)])
2795(Load <t> (OffPtr [off] (ITab (IMake          (Addr {sym} _)    _))) _) && t.IsInteger() && t.Size() == 4 && isFixed32(config, sym, off) => (Const32 [fixed32(config, sym, off)])
2796(Load <t> (OffPtr [off] (ITab (IMake (Convert (Addr {sym} _) _) _))) _) && t.IsInteger() && t.Size() == 4 && isFixed32(config, sym, off) => (Const32 [fixed32(config, sym, off)])
2797
2798// Calling cmpstring a second time with the same arguments in the
2799// same memory state can reuse the results of the first call.
2800// See issue 61725.
2801// Note that this could pretty easily generalize to any pure function.
2802(SelectN [0] (StaticLECall {f} x y (SelectN [1] c:(StaticLECall {g} x y mem))))
2803  && isSameCall(f, "runtime.cmpstring")
2804  && isSameCall(g, "runtime.cmpstring")
2805=> @c.Block (SelectN [0] <typ.Int> c)
2806
2807// If we don't use the result of cmpstring, might as well not call it.
2808// Note that this could pretty easily generalize to any pure function.
2809(SelectN [1] c:(StaticLECall {f} _ _ mem)) && c.Uses == 1 && isSameCall(f, "runtime.cmpstring") && clobber(c) => mem
2810