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