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