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 // The load at offset zero is written without an OffPtr, so it needs a rule
868 // of its own. Without it, a group of loads covering the copy would be split
869 // between the source and the destination, which stops them from being
870 // recombined into one wider load.
871 (Load <t1> op1:(OffPtr [o1] p1) move:(Move [n] p2 src mem))
872 && o1 >= 0 && o1+t1.Size() <= n && ssa.IsSamePtr(p1, p2)
873 && !ssa.IsVolatile(src)
874 => @move.Block (Load <t1> (OffPtr <op1.Type> [o1] src) mem)
875 (Load <t1> p1 move:(Move [n] p2 src mem))
876 && t1.Size() <= n && ssa.IsSamePtr(p1, p2)
877 && !ssa.IsVolatile(src)
878 => @move.Block (Load <t1> src mem)
879
880 // Pass constants through math.Float{32,64}bits and math.Float{32,64}frombits
881 (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))])
882 (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))])
883 (Load <t1> p1 (Store {t2} p2 (Const64F [x]) _)) && ssa.IsSamePtr(p1,p2) && t2.Size() == 8 && ssa.Is64BitInt(t1) => (Const64 [int64(math.Float64bits(x))])
884 (Load <t1> p1 (Store {t2} p2 (Const32F [x]) _)) && ssa.IsSamePtr(p1,p2) && t2.Size() == 4 && ssa.Is32BitInt(t1) => (Const32 [int32(math.Float32bits(x))])
885
886 // Float Loads up to Zeros so they can be constant folded.
887 (Load <t1> op1:(OffPtr [o1] p1)
888 (Store {t2} p2 _
889 mem:(Zero [n] p3 _)))
890 && o1 >= 0 && o1+t1.Size() <= n && ssa.IsSamePtr(p1, p3)
891 && ssa.CanSSA(t1)
892 && ssa.Disjoint(op1, t1, p2, t2)
893 => @mem.Block (Load <t1> (OffPtr <op1.Type> [o1] p3) mem)
894 (Load <t1> op1:(OffPtr [o1] p1)
895 (Store {t2} p2 _
896 (Store {t3} p3 _
897 mem:(Zero [n] p4 _))))
898 && o1 >= 0 && o1+t1.Size() <= n && ssa.IsSamePtr(p1, p4)
899 && ssa.CanSSA(t1)
900 && ssa.Disjoint(op1, t1, p2, t2)
901 && ssa.Disjoint(op1, t1, p3, t3)
902 => @mem.Block (Load <t1> (OffPtr <op1.Type> [o1] p4) mem)
903 (Load <t1> op1:(OffPtr [o1] p1)
904 (Store {t2} p2 _
905 (Store {t3} p3 _
906 (Store {t4} p4 _
907 mem:(Zero [n] p5 _)))))
908 && o1 >= 0 && o1+t1.Size() <= n && ssa.IsSamePtr(p1, p5)
909 && ssa.CanSSA(t1)
910 && ssa.Disjoint(op1, t1, p2, t2)
911 && ssa.Disjoint(op1, t1, p3, t3)
912 && ssa.Disjoint(op1, t1, p4, t4)
913 => @mem.Block (Load <t1> (OffPtr <op1.Type> [o1] p5) mem)
914 (Load <t1> op1:(OffPtr [o1] p1)
915 (Store {t2} p2 _
916 (Store {t3} p3 _
917 (Store {t4} p4 _
918 (Store {t5} p5 _
919 mem:(Zero [n] p6 _))))))
920 && o1 >= 0 && o1+t1.Size() <= n && ssa.IsSamePtr(p1, p6)
921 && ssa.CanSSA(t1)
922 && ssa.Disjoint(op1, t1, p2, t2)
923 && ssa.Disjoint(op1, t1, p3, t3)
924 && ssa.Disjoint(op1, t1, p4, t4)
925 && ssa.Disjoint(op1, t1, p5, t5)
926 => @mem.Block (Load <t1> (OffPtr <op1.Type> [o1] p6) mem)
927
928 // Zero to Load forwarding.
929 (Load <t1> (OffPtr [o] p1) (Zero [n] p2 _))
930 && t1.IsBoolean()
931 && ssa.IsSamePtr(p1, p2)
932 && n >= o + 1
933 => (ConstBool [false])
934 (Load <t1> (OffPtr [o] p1) (Zero [n] p2 _))
935 && ssa.Is8BitInt(t1)
936 && ssa.IsSamePtr(p1, p2)
937 && n >= o + 1
938 => (Const8 [0])
939 (Load <t1> (OffPtr [o] p1) (Zero [n] p2 _))
940 && ssa.Is16BitInt(t1)
941 && ssa.IsSamePtr(p1, p2)
942 && n >= o + 2
943 => (Const16 [0])
944 (Load <t1> (OffPtr [o] p1) (Zero [n] p2 _))
945 && ssa.Is32BitInt(t1)
946 && ssa.IsSamePtr(p1, p2)
947 && n >= o + 4
948 => (Const32 [0])
949 (Load <t1> (OffPtr [o] p1) (Zero [n] p2 _))
950 && ssa.Is64BitInt(t1)
951 && ssa.IsSamePtr(p1, p2)
952 && n >= o + 8
953 => (Const64 [0])
954 (Load <t1> (OffPtr [o] p1) (Zero [n] p2 _))
955 && ssa.Is32BitFloat(t1)
956 && ssa.IsSamePtr(p1, p2)
957 && n >= o + 4
958 => (Const32F [0])
959 (Load <t1> (OffPtr [o] p1) (Zero [n] p2 _))
960 && ssa.Is64BitFloat(t1)
961 && ssa.IsSamePtr(p1, p2)
962 && n >= o + 8
963 => (Const64F [0])
964
965 // Eliminate stores of values that have just been loaded from the same location.
966 // We also handle the common case where there are some intermediate stores.
967 (Store {t1} p1 (Load <t2> p2 mem) mem)
968 && ssa.IsSamePtr(p1, p2)
969 && t2.Size() == t1.Size()
970 => mem
971 (Store {t1} p1 (Load <t2> p2 oldmem) mem:(Store {t3} p3 _ oldmem))
972 && ssa.IsSamePtr(p1, p2)
973 && t2.Size() == t1.Size()
974 && ssa.Disjoint(p1, t1, p3, t3)
975 => mem
976 (Store {t1} p1 (Load <t2> p2 oldmem) mem:(Store {t3} p3 _ (Store {t4} p4 _ oldmem)))
977 && ssa.IsSamePtr(p1, p2)
978 && t2.Size() == t1.Size()
979 && ssa.Disjoint(p1, t1, p3, t3)
980 && ssa.Disjoint(p1, t1, p4, t4)
981 => mem
982 (Store {t1} p1 (Load <t2> p2 oldmem) mem:(Store {t3} p3 _ (Store {t4} p4 _ (Store {t5} p5 _ oldmem))))
983 && ssa.IsSamePtr(p1, p2)
984 && t2.Size() == t1.Size()
985 && ssa.Disjoint(p1, t1, p3, t3)
986 && ssa.Disjoint(p1, t1, p4, t4)
987 && ssa.Disjoint(p1, t1, p5, t5)
988 => mem
989
990 // Don't Store zeros to cleared variables.
991 (Store {t} (OffPtr [o] p1) x mem:(Zero [n] p2 _))
992 && ssa.IsConstZero(x)
993 && o >= 0 && t.Size() + o <= n && ssa.IsSamePtr(p1, p2)
994 => mem
995 (Store {t1} op1:(OffPtr [o1] p1) x mem:(Store {t2} p2 _ (Zero [n] p3 _)))
996 && ssa.IsConstZero(x)
997 && o1 >= 0 && t1.Size() + o1 <= n && ssa.IsSamePtr(p1, p3)
998 && ssa.Disjoint(op1, t1, p2, t2)
999 => mem
1000 (Store {t1} op1:(OffPtr [o1] p1) x mem:(Store {t2} p2 _ (Store {t3} p3 _ (Zero [n] p4 _))))
1001 && ssa.IsConstZero(x)
1002 && o1 >= 0 && t1.Size() + o1 <= n && ssa.IsSamePtr(p1, p4)
1003 && ssa.Disjoint(op1, t1, p2, t2)
1004 && ssa.Disjoint(op1, t1, p3, t3)
1005 => mem
1006 (Store {t1} op1:(OffPtr [o1] p1) x mem:(Store {t2} p2 _ (Store {t3} p3 _ (Store {t4} p4 _ (Zero [n] p5 _)))))
1007 && ssa.IsConstZero(x)
1008 && o1 >= 0 && t1.Size() + o1 <= n && ssa.IsSamePtr(p1, p5)
1009 && ssa.Disjoint(op1, t1, p2, t2)
1010 && ssa.Disjoint(op1, t1, p3, t3)
1011 && ssa.Disjoint(op1, t1, p4, t4)
1012 => mem
1013
1014 // Collapse OffPtr
1015 (OffPtr (OffPtr p [y]) [x]) => (OffPtr p [x+y])
1016 (OffPtr p [0]) && v.Type.Compare(p.Type) == types.CMPeq => p
1017
1018 // indexing operations
1019 // Note: bounds check has already been done
1020 (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())])))
1021 (PtrIndex <t> ptr idx) && config.PtrSize == 8 => (AddPtr ptr (Mul64 <typ.Int> idx (Const64 <typ.Int> [t.Elem().Size()])))
1022
1023 // struct operations
1024 (StructSelect [i] x:(StructMake ___)) => x.Args[i]
1025 (Load <t> _ _) && t.IsStruct() && t.Size() > 0 && ssa.CanSSA(t) && !t.IsSIMD() => rewriteStructLoad(v)
1026 (Store _ (StructMake ___) _) => ssa.RewriteStructStore(v)
1027
1028 (StructSelect [i] x:(Load <t> ptr mem)) && !ssa.CanSSA(t) =>
1029 @x.Block (Load <v.Type> (OffPtr <v.Type.PtrTo()> [t.FieldOff(int(i))] ptr) mem)
1030
1031 // Putting struct{*byte} and similar into direct interfaces.
1032 (IMake _typ (StructMake ___)) => ssa.ImakeOfStructMake(v)
1033 (StructSelect (IData x)) && v.Type.Size() > 0 => (IData x)
1034 (StructSelect (IData x)) && v.Type.Size() == 0 => (Empty)
1035
1036 // un-SSAable values use mem->mem copies
1037 (Store {t} dst (Load src mem) mem) && !ssa.CanSSA(t) =>
1038 (Move {t} [t.Size()] dst src mem)
1039 (Store {t} dst (Load src mem) (VarDef {x} mem)) && !ssa.CanSSA(t) =>
1040 (Move {t} [t.Size()] dst src (VarDef {x} mem))
1041
1042 // array ops
1043 (ArraySelect (ArrayMake1 x)) => x
1044
1045 (Load <t> ptr mem) && t.IsArray() && t.NumElem() == 1 && t.Size() > 0 && ssa.CanSSA(t) =>
1046 (ArrayMake1 (Load <t.Elem()> ptr mem))
1047
1048 (Store dst (ArrayMake1 e) mem) => (Store {e.Type} dst e mem)
1049
1050 // Putting [1]*byte and similar into direct interfaces.
1051 (IMake _typ (ArrayMake1 val)) => (IMake _typ val)
1052 (ArraySelect [0] (IData x)) => (IData x)
1053
1054 // zero-sized values.
1055 (Load <t> _ _) && t.Size() == 0 => (Empty)
1056 (Store _ (Empty) mem) => mem
1057
1058 // string ops
1059 // Decomposing StringMake and lowering of StringPtr and StringLen
1060 // happens in a later pass, dec, so that these operations are available
1061 // to other passes for optimizations.
1062 (StringPtr (StringMake (Addr <t> {s} base) _)) => (Addr <t> {s} base)
1063 (StringLen (StringMake _ (Const64 <t> [c]))) => (Const64 <t> [c])
1064 (ConstString {str}) && config.PtrSize == 4 && str == "" =>
1065 (StringMake (ConstNil) (Const32 <typ.Int> [0]))
1066 (ConstString {str}) && config.PtrSize == 8 && str == "" =>
1067 (StringMake (ConstNil) (Const64 <typ.Int> [0]))
1068 (ConstString {str}) && config.PtrSize == 4 && str != "" =>
1069 (StringMake
1070 (Addr <typ.BytePtr> {fe.StringData(str)}
1071 (SB))
1072 (Const32 <typ.Int> [int32(len(str))]))
1073 (ConstString {str}) && config.PtrSize == 8 && str != "" =>
1074 (StringMake
1075 (Addr <typ.BytePtr> {fe.StringData(str)}
1076 (SB))
1077 (Const64 <typ.Int> [int64(len(str))]))
1078
1079 // slice ops
1080 // Only a few slice rules are provided here. See dec.rules for
1081 // a more comprehensive set.
1082 (SliceLen (SliceMake _ (Const64 <t> [c]) _)) => (Const64 <t> [c])
1083 (SliceCap (SliceMake _ _ (Const64 <t> [c]))) => (Const64 <t> [c])
1084 (SliceLen (SliceMake _ (Const32 <t> [c]) _)) => (Const32 <t> [c])
1085 (SliceCap (SliceMake _ _ (Const32 <t> [c]))) => (Const32 <t> [c])
1086 (SlicePtr (SliceMake (SlicePtr x) _ _)) => (SlicePtr x)
1087 (SliceLen (SliceMake _ (SliceLen x) _)) => (SliceLen x)
1088 (SliceCap (SliceMake _ _ (SliceCap x))) => (SliceCap x)
1089 (SliceCap (SliceMake _ _ (SliceLen x))) => (SliceLen x)
1090 (ConstSlice) && config.PtrSize == 4 =>
1091 (SliceMake
1092 (ConstNil <v.Type.Elem().PtrTo()>)
1093 (Const32 <typ.Int> [0])
1094 (Const32 <typ.Int> [0]))
1095 (ConstSlice) && config.PtrSize == 8 =>
1096 (SliceMake
1097 (ConstNil <v.Type.Elem().PtrTo()>)
1098 (Const64 <typ.Int> [0])
1099 (Const64 <typ.Int> [0]))
1100 (SliceLen (Phi (SliceMake _ x _) (SliceMake _ x _))) => x
1101 (SliceCap (Phi (SliceMake _ _ x) (SliceMake _ _ x))) => x
1102
1103 // Special rule to help constant slicing; len > 0 implies cap > 0 implies Slicemask is all 1
1104 (SliceMake (AddPtr <t> x (And64 y (Slicemask _))) w:(Const64 [c]) z) && c > 0 => (SliceMake (AddPtr <t> x y) w z)
1105 (SliceMake (AddPtr <t> x (And32 y (Slicemask _))) w:(Const32 [c]) z) && c > 0 => (SliceMake (AddPtr <t> x y) w z)
1106
1107 // interface ops
1108 (ConstInterface) =>
1109 (IMake
1110 (ConstNil <typ.Uintptr>)
1111 (ConstNil <typ.BytePtr>))
1112
1113 (NilCheck ptr:(GetG mem) mem) => ptr
1114
1115 (If (Not cond) yes no) => (If cond no yes)
1116 (If (ConstBool [c]) yes no) && c => (First yes no)
1117 (If (ConstBool [c]) yes no) && !c => (First no yes)
1118
1119 (Phi <t> nx:(Not x) ny:(Not y)) && nx.Uses == 1 && ny.Uses == 1 => (Not (Phi <t> x y))
1120
1121 // Get rid of Convert ops for pointer arithmetic on unsafe.Pointer.
1122 (Convert (Add(64|32) (Convert ptr mem) off) mem) => (AddPtr ptr off)
1123 (Convert (Convert ptr mem) mem) => ptr
1124 // Note: it is important that the target rewrite is ptr+(off1+off2), not (ptr+off1)+off2.
1125 // We must ensure that no intermediate computations are invalid pointers.
1126 (Convert a:(Add(64|32) (Add(64|32) (Convert ptr mem) off1) off2) mem) => (AddPtr ptr (Add(64|32) <a.Type> off1 off2))
1127
1128 // Simplification of divisions.
1129 // Only trivial, easily analyzed (by prove) rewrites here.
1130 // Strength reduction of div to mul is delayed to divmod.rules.
1131
1132 // Signed divide by a negative constant. Rewrite to divide by a positive constant.
1133 (Div8 <t> n (Const8 [c])) && c < 0 && c != -1<<7 => (Neg8 (Div8 <t> n (Const8 <t> [-c])))
1134 (Div16 <t> n (Const16 [c])) && c < 0 && c != -1<<15 => (Neg16 (Div16 <t> n (Const16 <t> [-c])))
1135 (Div32 <t> n (Const32 [c])) && c < 0 && c != -1<<31 => (Neg32 (Div32 <t> n (Const32 <t> [-c])))
1136 (Div64 <t> n (Const64 [c])) && c < 0 && c != -1<<63 => (Neg64 (Div64 <t> n (Const64 <t> [-c])))
1137
1138 // Dividing by the most-negative number. Result is always 0 except
1139 // if the input is also the most-negative number.
1140 // We can detect that using the sign bit of x & -x.
1141 (Div64 x (Const64 [-1<<63])) && isNonNegative(x) => (Const64 [0])
1142 (Div8 <t> x (Const8 [-1<<7 ])) => (Rsh8Ux64 (And8 <t> x (Neg8 <t> x)) (Const64 <typ.UInt64> [7 ]))
1143 (Div16 <t> x (Const16 [-1<<15])) => (Rsh16Ux64 (And16 <t> x (Neg16 <t> x)) (Const64 <typ.UInt64> [15]))
1144 (Div32 <t> x (Const32 [-1<<31])) => (Rsh32Ux64 (And32 <t> x (Neg32 <t> x)) (Const64 <typ.UInt64> [31]))
1145 (Div64 <t> x (Const64 [-1<<63])) => (Rsh64Ux64 (And64 <t> x (Neg64 <t> x)) (Const64 <typ.UInt64> [63]))
1146
1147 // Unsigned divide by power of 2. Strength reduce to a shift.
1148 (Div8u n (Const8 [c])) && ssa.IsPowerOfTwo(uint8(c)) => (Rsh8Ux64 n (Const64 <typ.UInt64> [ssa.Log8u(uint8(c))]))
1149 (Div16u n (Const16 [c])) && ssa.IsPowerOfTwo(uint16(c)) => (Rsh16Ux64 n (Const64 <typ.UInt64> [ssa.Log16u(uint16(c))]))
1150 (Div32u n (Const32 [c])) && ssa.IsPowerOfTwo(uint32(c)) => (Rsh32Ux64 n (Const64 <typ.UInt64> [ssa.Log32u(uint32(c))]))
1151 (Div64u n (Const64 [c])) && ssa.IsPowerOfTwo(uint64(c)) => (Rsh64Ux64 n (Const64 <typ.UInt64> [ssa.Log64u(uint64(c))]))
1152
1153 // Strength reduce multiplication by a power of two to a shift.
1154 // Excluded from early opt so that prove can recognize mod
1155 // by the x - (x/d)*d pattern.
1156 // (Runs during "middle opt" and "late opt".)
1157 (Mul8 <t> x (Const8 [c])) && ssa.IsPowerOfTwo(uint8(c)) && v.Block.Func.Pass.Name != "opt" =>
1158 (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 (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 (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 (Lsh64x64 <t> x (Const64 <typ.UInt64> [ssa.Log64u(uint64(c))]))
1165 (Mul8 <t> x (Const8 [c])) && ssa.IsPowerOfTwo(uint8(-c)) && v.Block.Func.Pass.Name != "opt" =>
1166 (Neg8 (Lsh8x64 <t> x (Const64 <typ.UInt64> [ssa.Log8u(uint8(-c))])))
1167 (Mul16 <t> x (Const16 [c])) && ssa.IsPowerOfTwo(uint16(-c)) && v.Block.Func.Pass.Name != "opt" =>
1168 (Neg16 (Lsh16x64 <t> x (Const64 <typ.UInt64> [ssa.Log16u(uint16(-c))])))
1169 (Mul32 <t> x (Const32 [c])) && ssa.IsPowerOfTwo(uint32(-c)) && v.Block.Func.Pass.Name != "opt" =>
1170 (Neg32 (Lsh32x64 <t> x (Const64 <typ.UInt64> [ssa.Log32u(uint32(-c))])))
1171 (Mul64 <t> x (Const64 [c])) && ssa.IsPowerOfTwo(uint64(-c)) && v.Block.Func.Pass.Name != "opt" =>
1172 (Neg64 (Lsh64x64 <t> x (Const64 <typ.UInt64> [ssa.Log64u(uint64(-c))])))
1173
1174 // Strength reduction of mod to div.
1175 // Strength reduction of div to mul is delayed to divmod.rules.
1176
1177 // Unsigned mod by power of 2 constant.
1178 (Mod8u <t> n (Const8 [c])) && ssa.IsPowerOfTwo(uint8(c)) => (And8 n (Const8 <t> [c-1]))
1179 (Mod16u <t> n (Const16 [c])) && ssa.IsPowerOfTwo(uint16(c)) => (And16 n (Const16 <t> [c-1]))
1180 (Mod32u <t> n (Const32 [c])) && ssa.IsPowerOfTwo(uint32(c)) => (And32 n (Const32 <t> [c-1]))
1181 (Mod64u <t> n (Const64 [c])) && ssa.IsPowerOfTwo(uint64(c)) => (And64 n (Const64 <t> [c-1]))
1182
1183 // Signed non-negative mod by power of 2 constant.
1184 // TODO: Replace ModN with ModNu in prove.
1185 (Mod8 <t> n (Const8 [c])) && isNonNegative(n) && ssa.IsPowerOfTwo(c) => (And8 n (Const8 <t> [c-1]))
1186 (Mod16 <t> n (Const16 [c])) && isNonNegative(n) && ssa.IsPowerOfTwo(c) => (And16 n (Const16 <t> [c-1]))
1187 (Mod32 <t> n (Const32 [c])) && isNonNegative(n) && ssa.IsPowerOfTwo(c) => (And32 n (Const32 <t> [c-1]))
1188 (Mod64 <t> n (Const64 [c])) && isNonNegative(n) && ssa.IsPowerOfTwo(c) => (And64 n (Const64 <t> [c-1]))
1189 (Mod64 n (Const64 [-1<<63])) && isNonNegative(n) => n
1190
1191 // Signed mod by negative constant.
1192 (Mod8 <t> n (Const8 [c])) && c < 0 && c != -1<<7 => (Mod8 <t> n (Const8 <t> [-c]))
1193 (Mod16 <t> n (Const16 [c])) && c < 0 && c != -1<<15 => (Mod16 <t> n (Const16 <t> [-c]))
1194 (Mod32 <t> n (Const32 [c])) && c < 0 && c != -1<<31 => (Mod32 <t> n (Const32 <t> [-c]))
1195 (Mod64 <t> n (Const64 [c])) && c < 0 && c != -1<<63 => (Mod64 <t> n (Const64 <t> [-c]))
1196
1197 // All other mods by constants, do A%B = A-(A/B*B).
1198 // This implements % with two * and a bunch of ancillary ops.
1199 // One of the * is free if the user's code also computes A/B.
1200 (Mod8 <t> x (Const8 [c])) && x.Op != ssaop.OpConst8 && (c > 0 || c == -1<<7)
1201 => (Sub8 x (Mul8 <t> (Div8 <t> x (Const8 <t> [c])) (Const8 <t> [c])))
1202 (Mod16 <t> x (Const16 [c])) && x.Op != ssaop.OpConst16 && (c > 0 || c == -1<<15)
1203 => (Sub16 x (Mul16 <t> (Div16 <t> x (Const16 <t> [c])) (Const16 <t> [c])))
1204 (Mod32 <t> x (Const32 [c])) && x.Op != ssaop.OpConst32 && (c > 0 || c == -1<<31)
1205 => (Sub32 x (Mul32 <t> (Div32 <t> x (Const32 <t> [c])) (Const32 <t> [c])))
1206 (Mod64 <t> x (Const64 [c])) && x.Op != ssaop.OpConst64 && (c > 0 || c == -1<<63)
1207 => (Sub64 x (Mul64 <t> (Div64 <t> x (Const64 <t> [c])) (Const64 <t> [c])))
1208 (Mod8u <t> x (Const8 [c])) && x.Op != ssaop.OpConst8 && c != 0
1209 => (Sub8 x (Mul8 <t> (Div8u <t> x (Const8 <t> [c])) (Const8 <t> [c])))
1210 (Mod16u <t> x (Const16 [c])) && x.Op != ssaop.OpConst16 && c != 0
1211 => (Sub16 x (Mul16 <t> (Div16u <t> x (Const16 <t> [c])) (Const16 <t> [c])))
1212 (Mod32u <t> x (Const32 [c])) && x.Op != ssaop.OpConst32 && c != 0
1213 => (Sub32 x (Mul32 <t> (Div32u <t> x (Const32 <t> [c])) (Const32 <t> [c])))
1214 (Mod64u <t> x (Const64 [c])) && x.Op != ssaop.OpConst64 && c != 0
1215 => (Sub64 x (Mul64 <t> (Div64u <t> x (Const64 <t> [c])) (Const64 <t> [c])))
1216
1217 // Set up for mod->mul+rot optimization in genericlateopt.rules.
1218 // For architectures without rotates on less than 32-bits, promote to 32-bit.
1219 // TODO: Also != 0 case?
1220 (Eq8 (Mod8u x (Const8 [c])) (Const8 [0])) && x.Op != ssaop.OpConst8 && ssa.UdivisibleOK8(c) && !hasSmallRotate(config) =>
1221 (Eq32 (Mod32u <typ.UInt32> (ZeroExt8to32 <typ.UInt32> x) (Const32 <typ.UInt32> [int32(uint8(c))])) (Const32 <typ.UInt32> [0]))
1222 (Eq16 (Mod16u x (Const16 [c])) (Const16 [0])) && x.Op != ssaop.OpConst16 && ssa.UdivisibleOK16(c) && !hasSmallRotate(config) =>
1223 (Eq32 (Mod32u <typ.UInt32> (ZeroExt16to32 <typ.UInt32> x) (Const32 <typ.UInt32> [int32(uint16(c))])) (Const32 <typ.UInt32> [0]))
1224 (Eq8 (Mod8 x (Const8 [c])) (Const8 [0])) && x.Op != ssaop.OpConst8 && ssa.SdivisibleOK8(c) && !hasSmallRotate(config) =>
1225 (Eq32 (Mod32 <typ.Int32> (SignExt8to32 <typ.Int32> x) (Const32 <typ.Int32> [int32(c)])) (Const32 <typ.Int32> [0]))
1226 (Eq16 (Mod16 x (Const16 [c])) (Const16 [0])) && x.Op != ssaop.OpConst16 && ssa.SdivisibleOK16(c) && !hasSmallRotate(config) =>
1227 (Eq32 (Mod32 <typ.Int32> (SignExt16to32 <typ.Int32> x) (Const32 <typ.Int32> [int32(c)])) (Const32 <typ.Int32> [0]))
1228
1229 (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)
1230 (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)
1231
1232 // Optimize bitsets
1233 (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)
1234 => (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]))
1235 (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)
1236 => (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]))
1237
1238 // Mark newly generated bounded shifts as bounded, for opt passes after prove.
1239 (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)
1240 (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)
1241 (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)
1242 (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)
1243 (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)
1244 (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)
1245 (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)
1246 (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)
1247 (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)
1248 (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)
1249 (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)
1250 (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)
1251
1252 // Reassociate expressions involving
1253 // constants such that constants come first,
1254 // exposing obvious constant-folding opportunities.
1255 // Reassociate (op (op y C) x) to (op C (op x y)) or similar, where C
1256 // is constant, which pushes constants to the outside
1257 // of the expression. At that point, any constant-folding
1258 // opportunities should be obvious.
1259 // Note: don't include AddPtr here! In order to maintain the
1260 // invariant that pointers must stay within the pointed-to object,
1261 // we can't pull part of a pointer computation above the AddPtr.
1262 // See issue 37881.
1263 // Note: we don't need to handle any (x-C) cases because we already rewrite
1264 // (x-C) to (x+(-C)).
1265
1266 // x + (C + z) -> C + (x + z)
1267 (Add64 (Add64 i:(Const64 <t>) z) x) && (z.Op != ssaop.OpConst64 && x.Op != ssaop.OpConst64) => (Add64 i (Add64 <t> z x))
1268 (Add32 (Add32 i:(Const32 <t>) z) x) && (z.Op != ssaop.OpConst32 && x.Op != ssaop.OpConst32) => (Add32 i (Add32 <t> z x))
1269 (Add16 (Add16 i:(Const16 <t>) z) x) && (z.Op != ssaop.OpConst16 && x.Op != ssaop.OpConst16) => (Add16 i (Add16 <t> z x))
1270 (Add8 (Add8 i:(Const8 <t>) z) x) && (z.Op != ssaop.OpConst8 && x.Op != ssaop.OpConst8) => (Add8 i (Add8 <t> z x))
1271
1272 // x + (C - z) -> C + (x - z)
1273 (Add64 (Sub64 i:(Const64 <t>) z) x) && (z.Op != ssaop.OpConst64 && x.Op != ssaop.OpConst64) => (Add64 i (Sub64 <t> x z))
1274 (Add32 (Sub32 i:(Const32 <t>) z) x) && (z.Op != ssaop.OpConst32 && x.Op != ssaop.OpConst32) => (Add32 i (Sub32 <t> x z))
1275 (Add16 (Sub16 i:(Const16 <t>) z) x) && (z.Op != ssaop.OpConst16 && x.Op != ssaop.OpConst16) => (Add16 i (Sub16 <t> x z))
1276 (Add8 (Sub8 i:(Const8 <t>) z) x) && (z.Op != ssaop.OpConst8 && x.Op != ssaop.OpConst8) => (Add8 i (Sub8 <t> x z))
1277
1278 // x - (C - z) -> x + (z - C) -> (x + z) - C
1279 (Sub64 x (Sub64 i:(Const64 <t>) z)) && (z.Op != ssaop.OpConst64 && x.Op != ssaop.OpConst64) => (Sub64 (Add64 <t> x z) i)
1280 (Sub32 x (Sub32 i:(Const32 <t>) z)) && (z.Op != ssaop.OpConst32 && x.Op != ssaop.OpConst32) => (Sub32 (Add32 <t> x z) i)
1281 (Sub16 x (Sub16 i:(Const16 <t>) z)) && (z.Op != ssaop.OpConst16 && x.Op != ssaop.OpConst16) => (Sub16 (Add16 <t> x z) i)
1282 (Sub8 x (Sub8 i:(Const8 <t>) z)) && (z.Op != ssaop.OpConst8 && x.Op != ssaop.OpConst8) => (Sub8 (Add8 <t> x z) i)
1283
1284 // x - (z + C) -> x + (-z - C) -> (x - z) - C
1285 (Sub64 x (Add64 z i:(Const64 <t>))) && (z.Op != ssaop.OpConst64 && x.Op != ssaop.OpConst64) => (Sub64 (Sub64 <t> x z) i)
1286 (Sub32 x (Add32 z i:(Const32 <t>))) && (z.Op != ssaop.OpConst32 && x.Op != ssaop.OpConst32) => (Sub32 (Sub32 <t> x z) i)
1287 (Sub16 x (Add16 z i:(Const16 <t>))) && (z.Op != ssaop.OpConst16 && x.Op != ssaop.OpConst16) => (Sub16 (Sub16 <t> x z) i)
1288 (Sub8 x (Add8 z i:(Const8 <t>))) && (z.Op != ssaop.OpConst8 && x.Op != ssaop.OpConst8) => (Sub8 (Sub8 <t> x z) i)
1289
1290 // (C - z) - x -> C - (z + x)
1291 (Sub64 (Sub64 i:(Const64 <t>) z) x) && (z.Op != ssaop.OpConst64 && x.Op != ssaop.OpConst64) => (Sub64 i (Add64 <t> z x))
1292 (Sub32 (Sub32 i:(Const32 <t>) z) x) && (z.Op != ssaop.OpConst32 && x.Op != ssaop.OpConst32) => (Sub32 i (Add32 <t> z x))
1293 (Sub16 (Sub16 i:(Const16 <t>) z) x) && (z.Op != ssaop.OpConst16 && x.Op != ssaop.OpConst16) => (Sub16 i (Add16 <t> z x))
1294 (Sub8 (Sub8 i:(Const8 <t>) z) x) && (z.Op != ssaop.OpConst8 && x.Op != ssaop.OpConst8) => (Sub8 i (Add8 <t> z x))
1295
1296 // (z + C) -x -> C + (z - x)
1297 (Sub64 (Add64 z i:(Const64 <t>)) x) && (z.Op != ssaop.OpConst64 && x.Op != ssaop.OpConst64) => (Add64 i (Sub64 <t> z x))
1298 (Sub32 (Add32 z i:(Const32 <t>)) x) && (z.Op != ssaop.OpConst32 && x.Op != ssaop.OpConst32) => (Add32 i (Sub32 <t> z x))
1299 (Sub16 (Add16 z i:(Const16 <t>)) x) && (z.Op != ssaop.OpConst16 && x.Op != ssaop.OpConst16) => (Add16 i (Sub16 <t> z x))
1300 (Sub8 (Add8 z i:(Const8 <t>)) x) && (z.Op != ssaop.OpConst8 && x.Op != ssaop.OpConst8) => (Add8 i (Sub8 <t> z x))
1301
1302 // x & (C & z) -> C & (x & z)
1303 (And64 (And64 i:(Const64 <t>) z) x) && (z.Op != ssaop.OpConst64 && x.Op != ssaop.OpConst64) => (And64 i (And64 <t> z x))
1304 (And32 (And32 i:(Const32 <t>) z) x) && (z.Op != ssaop.OpConst32 && x.Op != ssaop.OpConst32) => (And32 i (And32 <t> z x))
1305 (And16 (And16 i:(Const16 <t>) z) x) && (z.Op != ssaop.OpConst16 && x.Op != ssaop.OpConst16) => (And16 i (And16 <t> z x))
1306 (And8 (And8 i:(Const8 <t>) z) x) && (z.Op != ssaop.OpConst8 && x.Op != ssaop.OpConst8) => (And8 i (And8 <t> z x))
1307
1308 // x | (C | z) -> C | (x | z)
1309 (Or64 (Or64 i:(Const64 <t>) z) x) && (z.Op != ssaop.OpConst64 && x.Op != ssaop.OpConst64) => (Or64 i (Or64 <t> z x))
1310 (Or32 (Or32 i:(Const32 <t>) z) x) && (z.Op != ssaop.OpConst32 && x.Op != ssaop.OpConst32) => (Or32 i (Or32 <t> z x))
1311 (Or16 (Or16 i:(Const16 <t>) z) x) && (z.Op != ssaop.OpConst16 && x.Op != ssaop.OpConst16) => (Or16 i (Or16 <t> z x))
1312 (Or8 (Or8 i:(Const8 <t>) z) x) && (z.Op != ssaop.OpConst8 && x.Op != ssaop.OpConst8) => (Or8 i (Or8 <t> z x))
1313
1314 // x ^ (C ^ z) -> C ^ (x ^ z)
1315 (Xor64 (Xor64 i:(Const64 <t>) z) x) && (z.Op != ssaop.OpConst64 && x.Op != ssaop.OpConst64) => (Xor64 i (Xor64 <t> z x))
1316 (Xor32 (Xor32 i:(Const32 <t>) z) x) && (z.Op != ssaop.OpConst32 && x.Op != ssaop.OpConst32) => (Xor32 i (Xor32 <t> z x))
1317 (Xor16 (Xor16 i:(Const16 <t>) z) x) && (z.Op != ssaop.OpConst16 && x.Op != ssaop.OpConst16) => (Xor16 i (Xor16 <t> z x))
1318 (Xor8 (Xor8 i:(Const8 <t>) z) x) && (z.Op != ssaop.OpConst8 && x.Op != ssaop.OpConst8) => (Xor8 i (Xor8 <t> z x))
1319
1320 // x * (D * z) = D * (x * z)
1321 (Mul64 (Mul64 i:(Const64 <t>) z) x) && (z.Op != ssaop.OpConst64 && x.Op != ssaop.OpConst64) => (Mul64 i (Mul64 <t> x z))
1322 (Mul32 (Mul32 i:(Const32 <t>) z) x) && (z.Op != ssaop.OpConst32 && x.Op != ssaop.OpConst32) => (Mul32 i (Mul32 <t> x z))
1323 (Mul16 (Mul16 i:(Const16 <t>) z) x) && (z.Op != ssaop.OpConst16 && x.Op != ssaop.OpConst16) => (Mul16 i (Mul16 <t> x z))
1324 (Mul8 (Mul8 i:(Const8 <t>) z) x) && (z.Op != ssaop.OpConst8 && x.Op != ssaop.OpConst8) => (Mul8 i (Mul8 <t> x z))
1325
1326 // C + (D + x) -> (C + D) + x
1327 (Add64 (Const64 <t> [c]) (Add64 (Const64 <t> [d]) x)) => (Add64 (Const64 <t> [c+d]) x)
1328 (Add32 (Const32 <t> [c]) (Add32 (Const32 <t> [d]) x)) => (Add32 (Const32 <t> [c+d]) x)
1329 (Add16 (Const16 <t> [c]) (Add16 (Const16 <t> [d]) x)) => (Add16 (Const16 <t> [c+d]) x)
1330 (Add8 (Const8 <t> [c]) (Add8 (Const8 <t> [d]) x)) => (Add8 (Const8 <t> [c+d]) x)
1331
1332 // C + (D - x) -> (C + D) - x
1333 (Add64 (Const64 <t> [c]) (Sub64 (Const64 <t> [d]) x)) => (Sub64 (Const64 <t> [c+d]) x)
1334 (Add32 (Const32 <t> [c]) (Sub32 (Const32 <t> [d]) x)) => (Sub32 (Const32 <t> [c+d]) x)
1335 (Add16 (Const16 <t> [c]) (Sub16 (Const16 <t> [d]) x)) => (Sub16 (Const16 <t> [c+d]) x)
1336 (Add8 (Const8 <t> [c]) (Sub8 (Const8 <t> [d]) x)) => (Sub8 (Const8 <t> [c+d]) x)
1337
1338 // C - (D - x) -> (C - D) + x
1339 (Sub64 (Const64 <t> [c]) (Sub64 (Const64 <t> [d]) x)) => (Add64 (Const64 <t> [c-d]) x)
1340 (Sub32 (Const32 <t> [c]) (Sub32 (Const32 <t> [d]) x)) => (Add32 (Const32 <t> [c-d]) x)
1341 (Sub16 (Const16 <t> [c]) (Sub16 (Const16 <t> [d]) x)) => (Add16 (Const16 <t> [c-d]) x)
1342 (Sub8 (Const8 <t> [c]) (Sub8 (Const8 <t> [d]) x)) => (Add8 (Const8 <t> [c-d]) x)
1343
1344 // C - (D + x) -> (C - D) - x
1345 (Sub64 (Const64 <t> [c]) (Add64 (Const64 <t> [d]) x)) => (Sub64 (Const64 <t> [c-d]) x)
1346 (Sub32 (Const32 <t> [c]) (Add32 (Const32 <t> [d]) x)) => (Sub32 (Const32 <t> [c-d]) x)
1347 (Sub16 (Const16 <t> [c]) (Add16 (Const16 <t> [d]) x)) => (Sub16 (Const16 <t> [c-d]) x)
1348 (Sub8 (Const8 <t> [c]) (Add8 (Const8 <t> [d]) x)) => (Sub8 (Const8 <t> [c-d]) x)
1349
1350 // C & (D & x) -> (C & D) & x
1351 (And64 (Const64 <t> [c]) (And64 (Const64 <t> [d]) x)) => (And64 (Const64 <t> [c&d]) x)
1352 (And32 (Const32 <t> [c]) (And32 (Const32 <t> [d]) x)) => (And32 (Const32 <t> [c&d]) x)
1353 (And16 (Const16 <t> [c]) (And16 (Const16 <t> [d]) x)) => (And16 (Const16 <t> [c&d]) x)
1354 (And8 (Const8 <t> [c]) (And8 (Const8 <t> [d]) x)) => (And8 (Const8 <t> [c&d]) x)
1355
1356 // C | (D | x) -> (C | D) | x
1357 (Or64 (Const64 <t> [c]) (Or64 (Const64 <t> [d]) x)) => (Or64 (Const64 <t> [c|d]) x)
1358 (Or32 (Const32 <t> [c]) (Or32 (Const32 <t> [d]) x)) => (Or32 (Const32 <t> [c|d]) x)
1359 (Or16 (Const16 <t> [c]) (Or16 (Const16 <t> [d]) x)) => (Or16 (Const16 <t> [c|d]) x)
1360 (Or8 (Const8 <t> [c]) (Or8 (Const8 <t> [d]) x)) => (Or8 (Const8 <t> [c|d]) x)
1361
1362 // C ^ (D ^ x) -> (C ^ D) ^ x
1363 (Xor64 (Const64 <t> [c]) (Xor64 (Const64 <t> [d]) x)) => (Xor64 (Const64 <t> [c^d]) x)
1364 (Xor32 (Const32 <t> [c]) (Xor32 (Const32 <t> [d]) x)) => (Xor32 (Const32 <t> [c^d]) x)
1365 (Xor16 (Const16 <t> [c]) (Xor16 (Const16 <t> [d]) x)) => (Xor16 (Const16 <t> [c^d]) x)
1366 (Xor8 (Const8 <t> [c]) (Xor8 (Const8 <t> [d]) x)) => (Xor8 (Const8 <t> [c^d]) x)
1367
1368 // C * (D * x) = (C * D) * x
1369 // Allow constant folding for single uses while preserving shared power-of-2 multiplies for scaled-index addressing.
1370 // See issue 80639 for additional details.
1371 (Mul64 (Const64 <t> [c]) m:(Mul64 (Const64 <t> [d]) x)) && (!ssa.IsPowerOfTwo(c) || m.Uses == 1) => (Mul64 (Const64 <t> [c*d]) x)
1372 (Mul32 (Const32 <t> [c]) m:(Mul32 (Const32 <t> [d]) x)) && (!ssa.IsPowerOfTwo(c) || m.Uses == 1) => (Mul32 (Const32 <t> [c*d]) x)
1373 (Mul16 (Const16 <t> [c]) m:(Mul16 (Const16 <t> [d]) x)) && (!ssa.IsPowerOfTwo(c) || m.Uses == 1) => (Mul16 (Const16 <t> [c*d]) x)
1374 (Mul8 (Const8 <t> [c]) m:(Mul8 (Const8 <t> [d]) x)) && (!ssa.IsPowerOfTwo(c) || m.Uses == 1) => (Mul8 (Const8 <t> [c*d]) x)
1375
1376
1377 // floating point optimizations
1378 (Mul(32|64)F x (Const(32|64)F [1])) => x
1379 (Mul32F x (Const32F [-1])) => (Neg32F x)
1380 (Mul64F x (Const64F [-1])) => (Neg64F x)
1381 (Mul32F x (Const32F [2])) => (Add32F x x)
1382 (Mul64F x (Const64F [2])) => (Add64F x x)
1383
1384 (Div32F x (Const32F <t> [c])) && reciprocalExact32(c) => (Mul32F x (Const32F <t> [1/c]))
1385 (Div64F x (Const64F <t> [c])) && reciprocalExact64(c) => (Mul64F x (Const64F <t> [1/c]))
1386
1387 // rewrite single-precision sqrt expression "float32(math.Sqrt(float64(x)))"
1388 (Cvt64Fto32F sqrt0:(Sqrt (Cvt32Fto64F x))) && sqrt0.Uses==1 => (Sqrt32 x)
1389
1390 (Sqrt (Const64F [c])) && !math.IsNaN(math.Sqrt(c)) => (Const64F [math.Sqrt(c)])
1391
1392 // for rewriting constant folded math/bits ops
1393 (Select0 (MakeTuple x y)) => x
1394 (Select1 (MakeTuple x y)) => y
1395
1396 // for rewriting results of some late-expanded rewrites (below)
1397 (SelectN [n] m:(MakeResult ___)) => m.Args[n]
1398
1399 // TODO(matloob): Try out having non-zeroing mallocs for prointerless
1400 // memory, and leaving the zeroing here. Then the compiler can remove
1401 // the zeroing if the user has explicit writes to the whole object.
1402
1403 // for late-expanded calls, recognize newobject and remove zeroing and nilchecks
1404 (Zero (SelectN [0] call:(StaticLECall ___)) mem:(SelectN [1] call))
1405 && isMalloc(call.Aux)
1406 => mem
1407
1408 (Store (SelectN [0] call:(StaticLECall ___)) x mem:(SelectN [1] call))
1409 && ssa.IsConstZero(x)
1410 && isMalloc(call.Aux)
1411 => mem
1412
1413 (Store (OffPtr (SelectN [0] call:(StaticLECall ___))) x mem:(SelectN [1] call))
1414 && ssa.IsConstZero(x)
1415 && isMalloc(call.Aux)
1416 => mem
1417
1418 (NilCheck ptr:(SelectN [0] call:(StaticLECall ___)) _)
1419 && isMalloc(call.Aux)
1420 && warnRule(fe.Debug_checknil(), v, "removed nil check")
1421 => ptr
1422
1423 (NilCheck ptr:(OffPtr (SelectN [0] call:(StaticLECall ___))) _)
1424 && isMalloc(call.Aux)
1425 && warnRule(fe.Debug_checknil(), v, "removed nil check")
1426 => ptr
1427
1428 // Addresses of globals are always non-nil.
1429 (NilCheck ptr:(Addr {_} (SB)) _) => ptr
1430 (NilCheck ptr:(Convert (Addr {_} (SB)) _) _) => ptr
1431
1432 // Addresses of locals are always non-nil.
1433 (NilCheck ptr:(LocalAddr _ _) _)
1434 && warnRule(fe.Debug_checknil(), v, "removed nil check")
1435 => ptr
1436
1437 // .dict args are always non-nil.
1438 (NilCheck ptr:(Arg {sym}) _) && isDictArgSym(sym) => ptr
1439
1440 // Nil checks of nil checks are redundant.
1441 // See comment at the end of https://go-review.googlesource.com/c/go/+/537775.
1442 (NilCheck ptr:(NilCheck _ _) _ ) => ptr
1443
1444 // for late-expanded calls, recognize memequal applied to a single constant byte
1445 // Support is limited by [1-8] byte sizes
1446 (StaticLECall {callAux} sptr (Addr {scon} (SB)) (Const(32|64) [1]) mem)
1447 && ssa.IsSameCall(callAux, "runtime.memequal")
1448 && ssa.SymIsRO(scon)
1449 => (MakeResult (Eq8 (Load <typ.Int8> sptr mem) (Const8 <typ.Int8> [int8(ssa.Read8(scon,0))])) mem)
1450
1451 (StaticLECall {callAux} (Addr {scon} (SB)) sptr (Const(32|64) [1]) mem)
1452 && ssa.IsSameCall(callAux, "runtime.memequal")
1453 && ssa.SymIsRO(scon)
1454 => (MakeResult (Eq8 (Load <typ.Int8> sptr mem) (Const8 <typ.Int8> [int8(ssa.Read8(scon,0))])) mem)
1455
1456 (StaticLECall {callAux} sptr (Addr {scon} (SB)) (Const(32|64) [2]) mem)
1457 && ssa.IsSameCall(callAux, "runtime.memequal")
1458 && ssa.SymIsRO(scon)
1459 && canLoadUnaligned(config)
1460 => (MakeResult (Eq16 (Load <typ.Int16> sptr mem) (Const16 <typ.Int16> [int16(ssa.Read16(scon,0,config.Ctxt.Arch.ByteOrder))])) mem)
1461
1462 (StaticLECall {callAux} (Addr {scon} (SB)) sptr (Const(32|64) [2]) mem)
1463 && ssa.IsSameCall(callAux, "runtime.memequal")
1464 && ssa.SymIsRO(scon)
1465 && canLoadUnaligned(config)
1466 => (MakeResult (Eq16 (Load <typ.Int16> sptr mem) (Const16 <typ.Int16> [int16(ssa.Read16(scon,0,config.Ctxt.Arch.ByteOrder))])) mem)
1467
1468 (StaticLECall {callAux} sptr (Addr {scon} (SB)) (Const(32|64) [4]) mem)
1469 && ssa.IsSameCall(callAux, "runtime.memequal")
1470 && ssa.SymIsRO(scon)
1471 && canLoadUnaligned(config)
1472 => (MakeResult (Eq32 (Load <typ.Int32> sptr mem) (Const32 <typ.Int32> [int32(ssa.Read32(scon,0,config.Ctxt.Arch.ByteOrder))])) mem)
1473
1474 (StaticLECall {callAux} (Addr {scon} (SB)) sptr (Const(32|64) [4]) mem)
1475 && ssa.IsSameCall(callAux, "runtime.memequal")
1476 && ssa.SymIsRO(scon)
1477 && canLoadUnaligned(config)
1478 => (MakeResult (Eq32 (Load <typ.Int32> sptr mem) (Const32 <typ.Int32> [int32(ssa.Read32(scon,0,config.Ctxt.Arch.ByteOrder))])) mem)
1479
1480 (StaticLECall {callAux} sptr (Addr {scon} (SB)) (Const(32|64) [8]) mem)
1481 && ssa.IsSameCall(callAux, "runtime.memequal")
1482 && ssa.SymIsRO(scon)
1483 && canLoadUnaligned(config) && config.RegSize == 8
1484 => (MakeResult (Eq64 (Load <typ.Int64> sptr mem) (Const64 <typ.Int64> [int64(ssa.Read64(scon,0,config.Ctxt.Arch.ByteOrder))])) mem)
1485
1486 (StaticLECall {callAux} (Addr {scon} (SB)) sptr (Const(32|64) [8]) mem)
1487 && ssa.IsSameCall(callAux, "runtime.memequal")
1488 && ssa.SymIsRO(scon)
1489 && canLoadUnaligned(config) && config.RegSize == 8
1490 => (MakeResult (Eq64 (Load <typ.Int64> sptr mem) (Const64 <typ.Int64> [int64(ssa.Read64(scon,0,config.Ctxt.Arch.ByteOrder))])) mem)
1491
1492 (StaticLECall {callAux} sptr (Addr {scon} (SB)) (Const(32|64) [3]) mem)
1493 && ssa.IsSameCall(callAux, "runtime.memequal")
1494 && ssa.SymIsRO(scon)
1495 && canLoadUnaligned(config) =>
1496 (MakeResult
1497 (Eq32
1498 (Or32 <typ.Int32>
1499 (ZeroExt16to32 <typ.Int32> (Load <typ.Int16> sptr mem))
1500 (Lsh32x32 <typ.Int32>
1501 (ZeroExt8to32 <typ.Int32> (Load <typ.Int8> (OffPtr <typ.BytePtr> [2] sptr) mem))
1502 (Const32 <typ.Int32> [16])))
1503 (Const32 <typ.Int32> [int32(uint32(ssa.Read16(scon,0,config.Ctxt.Arch.ByteOrder))|(uint32(ssa.Read8(scon,2))<<16))]))
1504 mem)
1505
1506 (StaticLECall {callAux} (Addr {scon} (SB)) sptr (Const(32|64) [3]) mem)
1507 && ssa.IsSameCall(callAux, "runtime.memequal")
1508 && ssa.SymIsRO(scon)
1509 && canLoadUnaligned(config) =>
1510 (MakeResult
1511 (Eq32
1512 (Or32 <typ.Int32>
1513 (ZeroExt16to32 <typ.Int32> (Load <typ.Int16> sptr mem))
1514 (Lsh32x32 <typ.Int32>
1515 (ZeroExt8to32 <typ.Int32> (Load <typ.Int8> (OffPtr <typ.BytePtr> [2] sptr) mem))
1516 (Const32 <typ.Int32> [16])))
1517 (Const32 <typ.Int32> [int32(uint32(ssa.Read16(scon,0,config.Ctxt.Arch.ByteOrder))|(uint32(ssa.Read8(scon,2))<<16))]))
1518 mem)
1519
1520 (StaticLECall {callAux} sptr (Addr {scon} (SB)) (Const(32|64) [5]) mem)
1521 && ssa.IsSameCall(callAux, "runtime.memequal")
1522 && ssa.SymIsRO(scon)
1523 && canLoadUnaligned(config) && config.RegSize == 8 =>
1524 (MakeResult
1525 (Eq64
1526 (Or64 <typ.Int64>
1527 (ZeroExt32to64 <typ.Int64> (Load <typ.Int32> sptr mem))
1528 (Lsh64x64 <typ.Int64>
1529 (ZeroExt8to64 <typ.Int64> (Load <typ.Int8> (OffPtr <typ.BytePtr> [4] sptr) mem))
1530 (Const64 <typ.Int64> [32])))
1531 (Const64 <typ.Int64> [int64(uint64(ssa.Read32(scon,0,config.Ctxt.Arch.ByteOrder))|(uint64(ssa.Read8(scon,4))<<32))]))
1532 mem)
1533
1534 (StaticLECall {callAux} (Addr {scon} (SB)) sptr (Const(32|64) [5]) mem)
1535 && ssa.IsSameCall(callAux, "runtime.memequal")
1536 && ssa.SymIsRO(scon)
1537 && canLoadUnaligned(config) && config.RegSize == 8 =>
1538 (MakeResult
1539 (Eq64
1540 (Or64 <typ.Int64>
1541 (ZeroExt32to64 <typ.Int64> (Load <typ.Int32> sptr mem))
1542 (Lsh64x64 <typ.Int64>
1543 (ZeroExt8to64 <typ.Int64> (Load <typ.Int8> (OffPtr <typ.BytePtr> [4] sptr) mem))
1544 (Const64 <typ.Int64> [32])))
1545 (Const64 <typ.Int64> [int64(uint64(ssa.Read32(scon,0,config.Ctxt.Arch.ByteOrder))|(uint64(ssa.Read8(scon,4))<<32))]))
1546 mem)
1547
1548 (StaticLECall {callAux} sptr (Addr {scon} (SB)) (Const(32|64) [6]) mem)
1549 && ssa.IsSameCall(callAux, "runtime.memequal")
1550 && ssa.SymIsRO(scon)
1551 && canLoadUnaligned(config) && config.RegSize == 8 =>
1552 (MakeResult
1553 (Eq64
1554 (Or64 <typ.Int64>
1555 (ZeroExt32to64 <typ.Int64> (Load <typ.Int32> sptr mem))
1556 (Lsh64x64 <typ.Int64>
1557 (ZeroExt16to64 <typ.Int64> (Load <typ.Int16> (OffPtr <typ.BytePtr> [4] sptr) mem))
1558 (Const64 <typ.Int64> [32])))
1559 (Const64 <typ.Int64> [int64(uint64(ssa.Read32(scon,0,config.Ctxt.Arch.ByteOrder))|(uint64(ssa.Read16(scon,4,config.Ctxt.Arch.ByteOrder))<<32))]))
1560 mem)
1561
1562 (StaticLECall {callAux} (Addr {scon} (SB)) sptr (Const(32|64) [6]) mem)
1563 && ssa.IsSameCall(callAux, "runtime.memequal")
1564 && ssa.SymIsRO(scon)
1565 && canLoadUnaligned(config) && config.RegSize == 8 =>
1566 (MakeResult
1567 (Eq64
1568 (Or64 <typ.Int64>
1569 (ZeroExt32to64 <typ.Int64> (Load <typ.Int32> sptr mem))
1570 (Lsh64x64 <typ.Int64>
1571 (ZeroExt16to64 <typ.Int64> (Load <typ.Int16> (OffPtr <typ.BytePtr> [4] sptr) mem))
1572 (Const64 <typ.Int64> [32])))
1573 (Const64 <typ.Int64> [int64(uint64(ssa.Read32(scon,0,config.Ctxt.Arch.ByteOrder))|(uint64(ssa.Read16(scon,4,config.Ctxt.Arch.ByteOrder))<<32))]))
1574 mem)
1575
1576 (StaticLECall {callAux} sptr (Addr {scon} (SB)) (Const(32|64) [7]) mem)
1577 && ssa.IsSameCall(callAux, "runtime.memequal")
1578 && ssa.SymIsRO(scon)
1579 && canLoadUnaligned(config) && config.RegSize == 8 =>
1580 (MakeResult
1581 (Eq64
1582 (Or64 <typ.Int64>
1583 (ZeroExt32to64 <typ.Int64> (Load <typ.Int32> sptr mem))
1584 (Lsh64x64 <typ.Int64>
1585 (ZeroExt32to64 <typ.Int64> (Load <typ.Int32> (OffPtr <typ.BytePtr> [3] sptr) mem))
1586 (Const64 <typ.Int64> [32])))
1587 (Const64 <typ.Int64> [int64(uint64(ssa.Read32(scon,0,config.Ctxt.Arch.ByteOrder))|(uint64(ssa.Read32(scon,3,config.Ctxt.Arch.ByteOrder))<<32))]))
1588 mem)
1589
1590 (StaticLECall {callAux} (Addr {scon} (SB)) sptr (Const(32|64) [7]) mem)
1591 && ssa.IsSameCall(callAux, "runtime.memequal")
1592 && ssa.SymIsRO(scon)
1593 && canLoadUnaligned(config) && config.RegSize == 8 =>
1594 (MakeResult
1595 (Eq64
1596 (Or64 <typ.Int64>
1597 (ZeroExt32to64 <typ.Int64> (Load <typ.Int32> sptr mem))
1598 (Lsh64x64 <typ.Int64>
1599 (ZeroExt32to64 <typ.Int64> (Load <typ.Int32> (OffPtr <typ.BytePtr> [3] sptr) mem))
1600 (Const64 <typ.Int64> [32])))
1601 (Const64 <typ.Int64> [int64(uint64(ssa.Read32(scon,0,config.Ctxt.Arch.ByteOrder))|(uint64(ssa.Read32(scon,3,config.Ctxt.Arch.ByteOrder))<<32))]))
1602 mem)
1603
1604 (StaticLECall {callAux} _ _ (Const(32|64) [0]) mem)
1605 && ssa.IsSameCall(callAux, "runtime.memequal")
1606 => (MakeResult (ConstBool <typ.Bool> [true]) mem)
1607
1608 (Static(Call|LECall) {callAux} p q _ mem)
1609 && ssa.IsSameCall(callAux, "runtime.memequal")
1610 && ssa.IsSamePtr(p, q)
1611 => (MakeResult (ConstBool <typ.Bool> [true]) mem)
1612
1613 (MemEq sptr tptr (Const64 [1]) mem)
1614 => (Eq8 (Load <typ.Int8> sptr mem) (Load <typ.Int8> tptr mem))
1615
1616 (Load <typ.Int8> sptr:(Addr {scon} (SB)) mem)
1617 && ssa.SymIsRO(scon)
1618 => (Const8 <typ.Int8> [int8(ssa.Read8(scon,0))])
1619
1620 (MemEq sptr tptr (Const64 [2]) mem)
1621 && canLoadUnaligned(config)
1622 => (Eq16 (Load <typ.Int16> sptr mem) (Load <typ.Int16> tptr mem))
1623
1624 (Load <typ.Int16> sptr:(Addr {scon} (SB)) mem)
1625 && ssa.SymIsRO(scon)
1626 => (Const16 <typ.Int16> [int16(ssa.Read16(scon,0,config.Ctxt.Arch.ByteOrder))])
1627
1628 (MemEq sptr tptr (Const64 [4]) mem)
1629 && canLoadUnaligned(config)
1630 => (Eq32 (Load <typ.Int32> sptr mem) (Load <typ.Int32> tptr mem))
1631
1632 (Load <typ.Int32> sptr:(Addr {scon} (SB)) mem)
1633 && ssa.SymIsRO(scon)
1634 => (Const32 <typ.Int32> [int32(ssa.Read32(scon,0,config.Ctxt.Arch.ByteOrder))])
1635
1636 (MemEq sptr tptr (Const64 [8]) mem)
1637 && canLoadUnaligned(config) && config.RegSize == 8
1638 => (Eq64 (Load <typ.Int64> sptr mem) (Load <typ.Int64> tptr mem))
1639
1640 (Load <typ.Int64> sptr:(Addr {scon} (SB)) mem)
1641 && ssa.SymIsRO(scon)
1642 => (Const64 <typ.Int64> [int64(ssa.Read64(scon,0,config.Ctxt.Arch.ByteOrder))])
1643
1644 (MemEq _ _ (Const64 [0]) _) => (ConstBool <typ.Bool> [true])
1645
1646 (MemEq p q _ _) && ssa.IsSamePtr(p, q) => (ConstBool <typ.Bool> [true])
1647
1648 // 3-32 bytes memeq (enabled only with support of unaligned loads and 8-byte max word size)
1649
1650 (MemEq p q (Const64 [c]) mem)
1651 && (c == 3 || c == 5 || c == 9 || c == 17)
1652 && canLoadUnaligned(config)
1653 && config.RegSize == 8
1654 => (AndB (MemEq p q (Const64 <typ.Int64> [c-1]) mem)
1655 (Eq8 (Load <typ.Int8> (OffPtr <p.Type> p [c-1]) mem) (Load <typ.Int8> (OffPtr <q.Type> q [c-1]) mem)))
1656
1657 (MemEq p q (Const64 [c]) mem)
1658 && (c == 6 || c == 10 || c == 18)
1659 && canLoadUnaligned(config)
1660 && config.RegSize == 8
1661 => (AndB (MemEq p q (Const64 <typ.Int64> [c-2]) mem)
1662 (Eq16 (Load <typ.Int16> (OffPtr <p.Type> p [c-2]) mem) (Load <typ.Int16> (OffPtr <q.Type> q [c-2]) mem)))
1663
1664 (MemEq p q (Const64 [c]) mem)
1665 && (c == 7 || c == 11 || c == 19 || c == 20)
1666 && canLoadUnaligned(config)
1667 && config.RegSize == 8
1668 => (AndB (MemEq p q (Const64 <typ.Int64> [min(c-3,16)]) mem)
1669 (Eq32 (Load <typ.Int32> (OffPtr <p.Type> p [c-4]) mem) (Load <typ.Int32> (OffPtr <q.Type> q [c-4]) mem)))
1670
1671 (MemEq p q (Const64 [c]) mem)
1672 && ((c >= 12 && c <= 16) || (c >= 21 && c <= 24))
1673 && canLoadUnaligned(config)
1674 && config.RegSize == 8
1675 => (AndB (MemEq p q (Const64 <typ.Int64> [8 + int64(bool2int(c>16))*8]) mem)
1676 (Eq64 (Load <typ.Int64> (OffPtr <p.Type> p [c-8]) mem) (Load <typ.Int64> (OffPtr <q.Type> q [c-8]) mem)))
1677
1678 (MemEq p q (Const64 [c]) mem)
1679 && c >= 25 && c <= 32
1680 && canLoadUnaligned(config)
1681 && config.RegSize == 8
1682 => (AndB (MemEq p q (Const64 <typ.Int64> [16]) mem)
1683 (MemEq (OffPtr <p.Type> p [16]) (OffPtr <q.Type> q [16]) (Const64 <typ.Int64> [c-16]) mem))
1684
1685 // Turn known-size calls to memclrNoHeapPointers into a Zero.
1686 // Note that we are using types.Types[types.TUINT8] instead of sptr.Type.Elem() - see issue 55122 and CL 431496 for more details.
1687 (SelectN [0] call:(StaticCall {sym} sptr (Const(64|32) [c]) mem))
1688 && isInlinableMemclr(config, int64(c))
1689 && ssa.IsSameCall(sym, "runtime.memclrNoHeapPointers")
1690 && call.Uses == 1
1691 && ssa.Clobber(call)
1692 => (Zero {types.Types[types.TUINT8]} [int64(c)] sptr mem)
1693
1694 // Recognise make([]T, 0) and replace it with a pointer to the zerobase
1695 (StaticLECall {callAux} _ (Const(64|32) [0]) (Const(64|32) [0]) mem)
1696 && ssa.IsSameCall(callAux, "runtime.makeslice")
1697 => (MakeResult (Addr <v.Type.FieldType(0)> {ir.Syms.Zerobase} (SB)) mem)
1698
1699 // Evaluate constant address comparisons.
1700 (EqPtr x x) => (ConstBool [true])
1701 (NeqPtr x x) => (ConstBool [false])
1702 (EqPtr (Addr {x} _) (Addr {y} _)) => (ConstBool [x == y])
1703 (EqPtr (Addr {x} _) (OffPtr [o] (Addr {y} _))) => (ConstBool [x == y && o == 0])
1704 (EqPtr (OffPtr [o1] (Addr {x} _)) (OffPtr [o2] (Addr {y} _))) => (ConstBool [x == y && o1 == o2])
1705 (NeqPtr (Addr {x} _) (Addr {y} _)) => (ConstBool [x != y])
1706 (NeqPtr (Addr {x} _) (OffPtr [o] (Addr {y} _))) => (ConstBool [x != y || o != 0])
1707 (NeqPtr (OffPtr [o1] (Addr {x} _)) (OffPtr [o2] (Addr {y} _))) => (ConstBool [x != y || o1 != o2])
1708 (EqPtr (LocalAddr {x} _ _) (LocalAddr {y} _ _)) => (ConstBool [x == y])
1709 (EqPtr (LocalAddr {x} _ _) (OffPtr [o] (LocalAddr {y} _ _))) => (ConstBool [x == y && o == 0])
1710 (EqPtr (OffPtr [o1] (LocalAddr {x} _ _)) (OffPtr [o2] (LocalAddr {y} _ _))) => (ConstBool [x == y && o1 == o2])
1711 (NeqPtr (LocalAddr {x} _ _) (LocalAddr {y} _ _)) => (ConstBool [x != y])
1712 (NeqPtr (LocalAddr {x} _ _) (OffPtr [o] (LocalAddr {y} _ _))) => (ConstBool [x != y || o != 0])
1713 (NeqPtr (OffPtr [o1] (LocalAddr {x} _ _)) (OffPtr [o2] (LocalAddr {y} _ _))) => (ConstBool [x != y || o1 != o2])
1714 (EqPtr (OffPtr [o1] p1) p2) && ssa.IsSamePtr(p1, p2) => (ConstBool [o1 == 0])
1715 (NeqPtr (OffPtr [o1] p1) p2) && ssa.IsSamePtr(p1, p2) => (ConstBool [o1 != 0])
1716 (EqPtr (OffPtr [o1] p1) (OffPtr [o2] p2)) && ssa.IsSamePtr(p1, p2) => (ConstBool [o1 == o2])
1717 (NeqPtr (OffPtr [o1] p1) (OffPtr [o2] p2)) && ssa.IsSamePtr(p1, p2) => (ConstBool [o1 != o2])
1718 (EqPtr (Const(32|64) [c]) (Const(32|64) [d])) => (ConstBool [c == d])
1719 (NeqPtr (Const(32|64) [c]) (Const(32|64) [d])) => (ConstBool [c != d])
1720 (EqPtr (Convert (Addr {x} _) _) (Addr {y} _)) => (ConstBool [x==y])
1721 (NeqPtr (Convert (Addr {x} _) _) (Addr {y} _)) => (ConstBool [x!=y])
1722
1723 (EqPtr (LocalAddr _ _) (Addr _)) => (ConstBool [false])
1724 (EqPtr (OffPtr (LocalAddr _ _)) (Addr _)) => (ConstBool [false])
1725 (EqPtr (LocalAddr _ _) (OffPtr (Addr _))) => (ConstBool [false])
1726 (EqPtr (OffPtr (LocalAddr _ _)) (OffPtr (Addr _))) => (ConstBool [false])
1727 (NeqPtr (LocalAddr _ _) (Addr _)) => (ConstBool [true])
1728 (NeqPtr (OffPtr (LocalAddr _ _)) (Addr _)) => (ConstBool [true])
1729 (NeqPtr (LocalAddr _ _) (OffPtr (Addr _))) => (ConstBool [true])
1730 (NeqPtr (OffPtr (LocalAddr _ _)) (OffPtr (Addr _))) => (ConstBool [true])
1731
1732 // Simplify address comparisons.
1733 (EqPtr (AddPtr p1 o1) p2) && ssa.IsSamePtr(p1, p2) => (Not (IsNonNil o1))
1734 (NeqPtr (AddPtr p1 o1) p2) && ssa.IsSamePtr(p1, p2) => (IsNonNil o1)
1735 (EqPtr (Const(32|64) [0]) p) => (Not (IsNonNil p))
1736 (NeqPtr (Const(32|64) [0]) p) => (IsNonNil p)
1737 (EqPtr (ConstNil) p) => (Not (IsNonNil p))
1738 (NeqPtr (ConstNil) p) => (IsNonNil p)
1739
1740 // Evaluate constant user nil checks.
1741 (IsNonNil (ConstNil)) => (ConstBool [false])
1742 (IsNonNil (Const(32|64) [c])) => (ConstBool [c != 0])
1743 (IsNonNil (Addr _) ) => (ConstBool [true])
1744 (IsNonNil (Convert (Addr _) _)) => (ConstBool [true])
1745 (IsNonNil (LocalAddr _ _)) => (ConstBool [true])
1746
1747 // Inline small or disjoint runtime.memmove calls with constant length.
1748 // See the comment in op Move in genericOps.go for discussion of the type.
1749 //
1750 // Note that we've lost any knowledge of the type and alignment requirements
1751 // of the source and destination. We only know the size, and that the type
1752 // contains no pointers.
1753 // The type of the move is not necessarily v.Args[0].Type().Elem()!
1754 // See issue 55122 for details.
1755 //
1756 // Because expand calls runs after prove, constants useful to this pattern may not appear.
1757 // Both versions need to exist; the memory and register variants.
1758 //
1759 // Match post-expansion calls, memory version.
1760 (SelectN [0] call:(StaticCall {sym} s1:(Store _ (Const(64|32) [sz]) s2:(Store _ src s3:(Store {t} _ dst mem)))))
1761 && sz >= 0
1762 && ssa.IsSameCall(sym, "runtime.memmove")
1763 && s1.Uses == 1 && s2.Uses == 1 && s3.Uses == 1
1764 && ssa.IsInlinableMemmove(dst, src, int64(sz), config)
1765 && ssa.Clobber(s1, s2, s3, call)
1766 => (Move {types.Types[types.TUINT8]} [int64(sz)] dst src mem)
1767
1768 // Match post-expansion calls, register version.
1769 (SelectN [0] call:(StaticCall {sym} dst src (Const(64|32) [sz]) mem))
1770 && sz >= 0
1771 && call.Uses == 1 // this will exclude all calls with results
1772 && ssa.IsSameCall(sym, "runtime.memmove")
1773 && ssa.IsInlinableMemmove(dst, src, int64(sz), config)
1774 && ssa.Clobber(call)
1775 => (Move {types.Types[types.TUINT8]} [int64(sz)] dst src mem)
1776
1777 // Match pre-expansion calls.
1778 (SelectN [0] call:(StaticLECall {sym} dst src (Const(64|32) [sz]) mem))
1779 && sz >= 0
1780 && call.Uses == 1 // this will exclude all calls with results
1781 && ssa.IsSameCall(sym, "runtime.memmove")
1782 && ssa.IsInlinableMemmove(dst, src, int64(sz), config)
1783 && ssa.Clobber(call)
1784 => (Move {types.Types[types.TUINT8]} [int64(sz)] dst src mem)
1785
1786 // De-virtualize late-expanded interface calls into late-expanded static calls.
1787 (InterLECall [argsize] {auxCall} (Addr {fn} (SB)) ___) => devirtLECall(v, fn.(*obj.LSym))
1788
1789 // Function handles are table indices, so resolve their targets at calls.
1790 (InterLECall [argsize] {auxCall} (Load (OffPtr [off] (Addr {sym} (SB))) _) ___)
1791 && config.Arch == "wasm32" && funcHandleSym(sym, off) != nil => devirtLECall(v, funcHandleSym(sym, off))
1792 (InterLECall [argsize] {auxCall} (Load (OffPtr [off] (ITab (IMake (Addr {sym} (SB)) _))) _) ___)
1793 && config.Arch == "wasm32" && funcHandleSym(sym, off) != nil => devirtLECall(v, funcHandleSym(sym, off))
1794 (InterLECall [argsize] {auxCall} (Load (OffPtr [off] (ITab (IMake (Convert (Addr {sym} (SB)) _) _))) _) ___)
1795 && config.Arch == "wasm32" && funcHandleSym(sym, off) != nil => devirtLECall(v, funcHandleSym(sym, off))
1796
1797 // Move and Zero optimizations.
1798 // Move source and destination may overlap.
1799
1800 // Convert Moves into Zeros when the source is known to be zeros.
1801 (Move {t} [n] dst1 src mem:(Zero {t} [n] dst2 _)) && ssa.IsSamePtr(src, dst2)
1802 => (Zero {t} [n] dst1 mem)
1803 (Move {t} [n] dst1 src mem:(VarDef (Zero {t} [n] dst0 _))) && ssa.IsSamePtr(src, dst0)
1804 => (Zero {t} [n] dst1 mem)
1805 (Move {t} [n] dst (Addr {sym} (SB)) mem) && symIsROZero(sym) => (Zero {t} [n] dst mem)
1806
1807 // Don't Store to variables that are about to be overwritten by Move/Zero.
1808 (Zero {t1} [n] p1 store:(Store {t2} (OffPtr [o2] p2) _ mem))
1809 && ssa.IsSamePtr(p1, p2) && store.Uses == 1
1810 && n >= o2 + t2.Size()
1811 && ssa.Clobber(store)
1812 => (Zero {t1} [n] p1 mem)
1813 (Move {t1} [n] dst1 src1 store:(Store {t2} op1:(OffPtr [o2] dst2) _ mem))
1814 && ssa.IsSamePtr(dst1, dst2) && store.Uses == 1
1815 && n >= o2 + t2.Size()
1816 && ssa.Disjoint1(src1, n, op1, t2.Size())
1817 && ssa.Clobber(store)
1818 => (Move {t1} [n] dst1 src1 mem)
1819
1820 // Don't Move to variables that are immediately completely overwritten.
1821 (Zero {t} [n] dst1 move:(Move {t} [n] dst2 _ mem))
1822 && move.Uses == 1
1823 && ssa.IsSamePtr(dst1, dst2)
1824 && ssa.Clobber(move)
1825 => (Zero {t} [n] dst1 mem)
1826 (Move {t} [n] dst1 src1 move:(Move {t} [n] dst2 _ mem))
1827 && move.Uses == 1
1828 && ssa.IsSamePtr(dst1, dst2) && ssa.Disjoint1(src1, n, dst2, n)
1829 && ssa.Clobber(move)
1830 => (Move {t} [n] dst1 src1 mem)
1831 (Zero {t} [n] dst1 vardef:(VarDef {x} move:(Move {t} [n] dst2 _ mem)))
1832 && move.Uses == 1 && vardef.Uses == 1
1833 && ssa.IsSamePtr(dst1, dst2)
1834 && ssa.Clobber(move, vardef)
1835 => (Zero {t} [n] dst1 (VarDef {x} mem))
1836 (Move {t} [n] dst1 src1 vardef:(VarDef {x} move:(Move {t} [n] dst2 _ mem)))
1837 && move.Uses == 1 && vardef.Uses == 1
1838 && ssa.IsSamePtr(dst1, dst2) && ssa.Disjoint1(src1, n, dst2, n)
1839 && ssa.Clobber(move, vardef)
1840 => (Move {t} [n] dst1 src1 (VarDef {x} mem))
1841 (Store {t1} op1:(OffPtr [o1] p1) d1
1842 m2:(Store {t2} op2:(OffPtr [0] p2) d2
1843 m3:(Move [n] p3 _ mem)))
1844 && m2.Uses == 1 && m3.Uses == 1
1845 && o1 == t2.Size()
1846 && n == t2.Size() + t1.Size()
1847 && ssa.IsSamePtr(p1, p2) && ssa.IsSamePtr(p2, p3)
1848 && ssa.Clobber(m2, m3)
1849 => (Store {t1} op1 d1 (Store {t2} op2 d2 mem))
1850 (Store {t1} op1:(OffPtr [o1] p1) d1
1851 m2:(Store {t2} op2:(OffPtr [o2] p2) d2
1852 m3:(Store {t3} op3:(OffPtr [0] p3) d3
1853 m4:(Move [n] p4 _ mem))))
1854 && m2.Uses == 1 && m3.Uses == 1 && m4.Uses == 1
1855 && o2 == t3.Size()
1856 && o1-o2 == t2.Size()
1857 && n == t3.Size() + t2.Size() + t1.Size()
1858 && ssa.IsSamePtr(p1, p2) && ssa.IsSamePtr(p2, p3) && ssa.IsSamePtr(p3, p4)
1859 && ssa.Clobber(m2, m3, m4)
1860 => (Store {t1} op1 d1 (Store {t2} op2 d2 (Store {t3} op3 d3 mem)))
1861 (Store {t1} op1:(OffPtr [o1] p1) d1
1862 m2:(Store {t2} op2:(OffPtr [o2] p2) d2
1863 m3:(Store {t3} op3:(OffPtr [o3] p3) d3
1864 m4:(Store {t4} op4:(OffPtr [0] p4) d4
1865 m5:(Move [n] p5 _ mem)))))
1866 && m2.Uses == 1 && m3.Uses == 1 && m4.Uses == 1 && m5.Uses == 1
1867 && o3 == t4.Size()
1868 && o2-o3 == t3.Size()
1869 && o1-o2 == t2.Size()
1870 && n == t4.Size() + t3.Size() + t2.Size() + t1.Size()
1871 && ssa.IsSamePtr(p1, p2) && ssa.IsSamePtr(p2, p3) && ssa.IsSamePtr(p3, p4) && ssa.IsSamePtr(p4, p5)
1872 && ssa.Clobber(m2, m3, m4, m5)
1873 => (Store {t1} op1 d1 (Store {t2} op2 d2 (Store {t3} op3 d3 (Store {t4} op4 d4 mem))))
1874
1875 // Don't Zero variables that are immediately completely overwritten
1876 // before being accessed.
1877 (Move {t} [n] dst1 src1 zero:(Zero {t} [n] dst2 mem))
1878 && zero.Uses == 1
1879 && ssa.IsSamePtr(dst1, dst2) && ssa.Disjoint1(src1, n, dst2, n)
1880 && ssa.Clobber(zero)
1881 => (Move {t} [n] dst1 src1 mem)
1882 (Move {t} [n] dst1 src1 vardef:(VarDef {x} zero:(Zero {t} [n] dst2 mem)))
1883 && zero.Uses == 1 && vardef.Uses == 1
1884 && ssa.IsSamePtr(dst1, dst2) && ssa.Disjoint1(src1, n, dst2, n)
1885 && ssa.Clobber(zero, vardef)
1886 => (Move {t} [n] dst1 src1 (VarDef {x} mem))
1887 (Store {t1} op1:(OffPtr [o1] p1) d1
1888 m2:(Store {t2} op2:(OffPtr [0] p2) d2
1889 m3:(Zero [n] p3 mem)))
1890 && m2.Uses == 1 && m3.Uses == 1
1891 && o1 == t2.Size()
1892 && n == t2.Size() + t1.Size()
1893 && ssa.IsSamePtr(p1, p2) && ssa.IsSamePtr(p2, p3)
1894 && ssa.Clobber(m2, m3)
1895 => (Store {t1} op1 d1 (Store {t2} op2 d2 mem))
1896 (Store {t1} op1:(OffPtr [o1] p1) d1
1897 m2:(Store {t2} op2:(OffPtr [o2] p2) d2
1898 m3:(Store {t3} op3:(OffPtr [0] p3) d3
1899 m4:(Zero [n] p4 mem))))
1900 && m2.Uses == 1 && m3.Uses == 1 && m4.Uses == 1
1901 && o2 == t3.Size()
1902 && o1-o2 == t2.Size()
1903 && n == t3.Size() + t2.Size() + t1.Size()
1904 && ssa.IsSamePtr(p1, p2) && ssa.IsSamePtr(p2, p3) && ssa.IsSamePtr(p3, p4)
1905 && ssa.Clobber(m2, m3, m4)
1906 => (Store {t1} op1 d1 (Store {t2} op2 d2 (Store {t3} op3 d3 mem)))
1907 (Store {t1} op1:(OffPtr [o1] p1) d1
1908 m2:(Store {t2} op2:(OffPtr [o2] p2) d2
1909 m3:(Store {t3} op3:(OffPtr [o3] p3) d3
1910 m4:(Store {t4} op4:(OffPtr [0] p4) d4
1911 m5:(Zero [n] p5 mem)))))
1912 && m2.Uses == 1 && m3.Uses == 1 && m4.Uses == 1 && m5.Uses == 1
1913 && o3 == t4.Size()
1914 && o2-o3 == t3.Size()
1915 && o1-o2 == t2.Size()
1916 && n == t4.Size() + t3.Size() + t2.Size() + t1.Size()
1917 && ssa.IsSamePtr(p1, p2) && ssa.IsSamePtr(p2, p3) && ssa.IsSamePtr(p3, p4) && ssa.IsSamePtr(p4, p5)
1918 && ssa.Clobber(m2, m3, m4, m5)
1919 => (Store {t1} op1 d1 (Store {t2} op2 d2 (Store {t3} op3 d3 (Store {t4} op4 d4 mem))))
1920
1921 // Don't Move from memory if the values are likely to already be
1922 // in registers.
1923 (Move {t1} [n] dst p1
1924 mem:(Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
1925 (Store {t3} op3:(OffPtr <tt3> [0] p3) d2 _)))
1926 && ssa.IsSamePtr(p1, p2) && ssa.IsSamePtr(p2, p3)
1927 && t2.Alignment() <= t1.Alignment()
1928 && t3.Alignment() <= t1.Alignment()
1929 && registerizable(b, t2)
1930 && registerizable(b, t3)
1931 && o2 == t3.Size()
1932 && n == t2.Size() + t3.Size()
1933 => (Store {t2} (OffPtr <tt2> [o2] dst) d1
1934 (Store {t3} (OffPtr <tt3> [0] dst) d2 mem))
1935 (Move {t1} [n] dst p1
1936 mem:(Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
1937 (Store {t3} op3:(OffPtr <tt3> [o3] p3) d2
1938 (Store {t4} op4:(OffPtr <tt4> [0] p4) d3 _))))
1939 && ssa.IsSamePtr(p1, p2) && ssa.IsSamePtr(p2, p3) && ssa.IsSamePtr(p3, p4)
1940 && t2.Alignment() <= t1.Alignment()
1941 && t3.Alignment() <= t1.Alignment()
1942 && t4.Alignment() <= t1.Alignment()
1943 && registerizable(b, t2)
1944 && registerizable(b, t3)
1945 && registerizable(b, t4)
1946 && o3 == t4.Size()
1947 && o2-o3 == t3.Size()
1948 && n == t2.Size() + t3.Size() + t4.Size()
1949 => (Store {t2} (OffPtr <tt2> [o2] dst) d1
1950 (Store {t3} (OffPtr <tt3> [o3] dst) d2
1951 (Store {t4} (OffPtr <tt4> [0] dst) d3 mem)))
1952 (Move {t1} [n] dst p1
1953 mem:(Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
1954 (Store {t3} op3:(OffPtr <tt3> [o3] p3) d2
1955 (Store {t4} op4:(OffPtr <tt4> [o4] p4) d3
1956 (Store {t5} op5:(OffPtr <tt5> [0] p5) d4 _)))))
1957 && ssa.IsSamePtr(p1, p2) && ssa.IsSamePtr(p2, p3) && ssa.IsSamePtr(p3, p4) && ssa.IsSamePtr(p4, p5)
1958 && t2.Alignment() <= t1.Alignment()
1959 && t3.Alignment() <= t1.Alignment()
1960 && t4.Alignment() <= t1.Alignment()
1961 && t5.Alignment() <= t1.Alignment()
1962 && registerizable(b, t2)
1963 && registerizable(b, t3)
1964 && registerizable(b, t4)
1965 && registerizable(b, t5)
1966 && o4 == t5.Size()
1967 && o3-o4 == t4.Size()
1968 && o2-o3 == t3.Size()
1969 && n == t2.Size() + t3.Size() + t4.Size() + t5.Size()
1970 => (Store {t2} (OffPtr <tt2> [o2] dst) d1
1971 (Store {t3} (OffPtr <tt3> [o3] dst) d2
1972 (Store {t4} (OffPtr <tt4> [o4] dst) d3
1973 (Store {t5} (OffPtr <tt5> [0] dst) d4 mem))))
1974
1975 // Same thing but with VarDef in the middle.
1976 (Move {t1} [n] dst p1
1977 mem:(VarDef
1978 (Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
1979 (Store {t3} op3:(OffPtr <tt3> [0] p3) d2 _))))
1980 && ssa.IsSamePtr(p1, p2) && ssa.IsSamePtr(p2, p3)
1981 && t2.Alignment() <= t1.Alignment()
1982 && t3.Alignment() <= t1.Alignment()
1983 && registerizable(b, t2)
1984 && registerizable(b, t3)
1985 && o2 == t3.Size()
1986 && n == t2.Size() + t3.Size()
1987 => (Store {t2} (OffPtr <tt2> [o2] dst) d1
1988 (Store {t3} (OffPtr <tt3> [0] dst) d2 mem))
1989 (Move {t1} [n] dst p1
1990 mem:(VarDef
1991 (Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
1992 (Store {t3} op3:(OffPtr <tt3> [o3] p3) d2
1993 (Store {t4} op4:(OffPtr <tt4> [0] p4) d3 _)))))
1994 && ssa.IsSamePtr(p1, p2) && ssa.IsSamePtr(p2, p3) && ssa.IsSamePtr(p3, p4)
1995 && t2.Alignment() <= t1.Alignment()
1996 && t3.Alignment() <= t1.Alignment()
1997 && t4.Alignment() <= t1.Alignment()
1998 && registerizable(b, t2)
1999 && registerizable(b, t3)
2000 && registerizable(b, t4)
2001 && o3 == t4.Size()
2002 && o2-o3 == t3.Size()
2003 && n == t2.Size() + t3.Size() + t4.Size()
2004 => (Store {t2} (OffPtr <tt2> [o2] dst) d1
2005 (Store {t3} (OffPtr <tt3> [o3] dst) d2
2006 (Store {t4} (OffPtr <tt4> [0] dst) d3 mem)))
2007 (Move {t1} [n] dst p1
2008 mem:(VarDef
2009 (Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
2010 (Store {t3} op3:(OffPtr <tt3> [o3] p3) d2
2011 (Store {t4} op4:(OffPtr <tt4> [o4] p4) d3
2012 (Store {t5} op5:(OffPtr <tt5> [0] p5) d4 _))))))
2013 && ssa.IsSamePtr(p1, p2) && ssa.IsSamePtr(p2, p3) && ssa.IsSamePtr(p3, p4) && ssa.IsSamePtr(p4, p5)
2014 && t2.Alignment() <= t1.Alignment()
2015 && t3.Alignment() <= t1.Alignment()
2016 && t4.Alignment() <= t1.Alignment()
2017 && t5.Alignment() <= t1.Alignment()
2018 && registerizable(b, t2)
2019 && registerizable(b, t3)
2020 && registerizable(b, t4)
2021 && registerizable(b, t5)
2022 && o4 == t5.Size()
2023 && o3-o4 == t4.Size()
2024 && o2-o3 == t3.Size()
2025 && n == t2.Size() + t3.Size() + t4.Size() + t5.Size()
2026 => (Store {t2} (OffPtr <tt2> [o2] dst) d1
2027 (Store {t3} (OffPtr <tt3> [o3] dst) d2
2028 (Store {t4} (OffPtr <tt4> [o4] dst) d3
2029 (Store {t5} (OffPtr <tt5> [0] dst) d4 mem))))
2030
2031 // Prefer to Zero and Store than to Move.
2032 (Move {t1} [n] dst p1
2033 mem:(Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
2034 (Zero {t3} [n] p3 _)))
2035 && ssa.IsSamePtr(p1, p2) && ssa.IsSamePtr(p2, p3)
2036 && t2.Alignment() <= t1.Alignment()
2037 && t3.Alignment() <= t1.Alignment()
2038 && registerizable(b, t2)
2039 && n >= o2 + t2.Size()
2040 => (Store {t2} (OffPtr <tt2> [o2] dst) d1
2041 (Zero {t1} [n] dst mem))
2042 (Move {t1} [n] dst p1
2043 mem:(Store {t2} (OffPtr <tt2> [o2] p2) d1
2044 (Store {t3} (OffPtr <tt3> [o3] p3) d2
2045 (Zero {t4} [n] p4 _))))
2046 && ssa.IsSamePtr(p1, p2) && ssa.IsSamePtr(p2, p3) && ssa.IsSamePtr(p3, p4)
2047 && t2.Alignment() <= t1.Alignment()
2048 && t3.Alignment() <= t1.Alignment()
2049 && t4.Alignment() <= t1.Alignment()
2050 && registerizable(b, t2)
2051 && registerizable(b, t3)
2052 && n >= o2 + t2.Size()
2053 && n >= o3 + t3.Size()
2054 => (Store {t2} (OffPtr <tt2> [o2] dst) d1
2055 (Store {t3} (OffPtr <tt3> [o3] dst) d2
2056 (Zero {t1} [n] dst mem)))
2057 (Move {t1} [n] dst p1
2058 mem:(Store {t2} (OffPtr <tt2> [o2] p2) d1
2059 (Store {t3} (OffPtr <tt3> [o3] p3) d2
2060 (Store {t4} (OffPtr <tt4> [o4] p4) d3
2061 (Zero {t5} [n] p5 _)))))
2062 && ssa.IsSamePtr(p1, p2) && ssa.IsSamePtr(p2, p3) && ssa.IsSamePtr(p3, p4) && ssa.IsSamePtr(p4, p5)
2063 && t2.Alignment() <= t1.Alignment()
2064 && t3.Alignment() <= t1.Alignment()
2065 && t4.Alignment() <= t1.Alignment()
2066 && t5.Alignment() <= t1.Alignment()
2067 && registerizable(b, t2)
2068 && registerizable(b, t3)
2069 && registerizable(b, t4)
2070 && n >= o2 + t2.Size()
2071 && n >= o3 + t3.Size()
2072 && n >= o4 + t4.Size()
2073 => (Store {t2} (OffPtr <tt2> [o2] dst) d1
2074 (Store {t3} (OffPtr <tt3> [o3] dst) d2
2075 (Store {t4} (OffPtr <tt4> [o4] dst) d3
2076 (Zero {t1} [n] dst mem))))
2077 (Move {t1} [n] dst p1
2078 mem:(Store {t2} (OffPtr <tt2> [o2] p2) d1
2079 (Store {t3} (OffPtr <tt3> [o3] p3) d2
2080 (Store {t4} (OffPtr <tt4> [o4] p4) d3
2081 (Store {t5} (OffPtr <tt5> [o5] p5) d4
2082 (Zero {t6} [n] p6 _))))))
2083 && ssa.IsSamePtr(p1, p2) && ssa.IsSamePtr(p2, p3) && ssa.IsSamePtr(p3, p4) && ssa.IsSamePtr(p4, p5) && ssa.IsSamePtr(p5, p6)
2084 && t2.Alignment() <= t1.Alignment()
2085 && t3.Alignment() <= t1.Alignment()
2086 && t4.Alignment() <= t1.Alignment()
2087 && t5.Alignment() <= t1.Alignment()
2088 && t6.Alignment() <= t1.Alignment()
2089 && registerizable(b, t2)
2090 && registerizable(b, t3)
2091 && registerizable(b, t4)
2092 && registerizable(b, t5)
2093 && n >= o2 + t2.Size()
2094 && n >= o3 + t3.Size()
2095 && n >= o4 + t4.Size()
2096 && n >= o5 + t5.Size()
2097 => (Store {t2} (OffPtr <tt2> [o2] dst) d1
2098 (Store {t3} (OffPtr <tt3> [o3] dst) d2
2099 (Store {t4} (OffPtr <tt4> [o4] dst) d3
2100 (Store {t5} (OffPtr <tt5> [o5] dst) d4
2101 (Zero {t1} [n] dst mem)))))
2102 (Move {t1} [n] dst p1
2103 mem:(VarDef
2104 (Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
2105 (Zero {t3} [n] p3 _))))
2106 && ssa.IsSamePtr(p1, p2) && ssa.IsSamePtr(p2, p3)
2107 && t2.Alignment() <= t1.Alignment()
2108 && t3.Alignment() <= t1.Alignment()
2109 && registerizable(b, t2)
2110 && n >= o2 + t2.Size()
2111 => (Store {t2} (OffPtr <tt2> [o2] dst) d1
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 (Zero {t4} [n] p4 _)))))
2118 && ssa.IsSamePtr(p1, p2) && ssa.IsSamePtr(p2, p3) && ssa.IsSamePtr(p3, p4)
2119 && t2.Alignment() <= t1.Alignment()
2120 && t3.Alignment() <= t1.Alignment()
2121 && t4.Alignment() <= t1.Alignment()
2122 && registerizable(b, t2)
2123 && registerizable(b, t3)
2124 && n >= o2 + t2.Size()
2125 && n >= o3 + t3.Size()
2126 => (Store {t2} (OffPtr <tt2> [o2] dst) d1
2127 (Store {t3} (OffPtr <tt3> [o3] dst) d2
2128 (Zero {t1} [n] dst mem)))
2129 (Move {t1} [n] dst p1
2130 mem:(VarDef
2131 (Store {t2} (OffPtr <tt2> [o2] p2) d1
2132 (Store {t3} (OffPtr <tt3> [o3] p3) d2
2133 (Store {t4} (OffPtr <tt4> [o4] p4) d3
2134 (Zero {t5} [n] p5 _))))))
2135 && ssa.IsSamePtr(p1, p2) && ssa.IsSamePtr(p2, p3) && ssa.IsSamePtr(p3, p4) && ssa.IsSamePtr(p4, p5)
2136 && t2.Alignment() <= t1.Alignment()
2137 && t3.Alignment() <= t1.Alignment()
2138 && t4.Alignment() <= t1.Alignment()
2139 && t5.Alignment() <= t1.Alignment()
2140 && registerizable(b, t2)
2141 && registerizable(b, t3)
2142 && registerizable(b, t4)
2143 && n >= o2 + t2.Size()
2144 && n >= o3 + t3.Size()
2145 && n >= o4 + t4.Size()
2146 => (Store {t2} (OffPtr <tt2> [o2] dst) d1
2147 (Store {t3} (OffPtr <tt3> [o3] dst) d2
2148 (Store {t4} (OffPtr <tt4> [o4] dst) d3
2149 (Zero {t1} [n] dst mem))))
2150 (Move {t1} [n] dst p1
2151 mem:(VarDef
2152 (Store {t2} (OffPtr <tt2> [o2] p2) d1
2153 (Store {t3} (OffPtr <tt3> [o3] p3) d2
2154 (Store {t4} (OffPtr <tt4> [o4] p4) d3
2155 (Store {t5} (OffPtr <tt5> [o5] p5) d4
2156 (Zero {t6} [n] p6 _)))))))
2157 && ssa.IsSamePtr(p1, p2) && ssa.IsSamePtr(p2, p3) && ssa.IsSamePtr(p3, p4) && ssa.IsSamePtr(p4, p5) && ssa.IsSamePtr(p5, p6)
2158 && t2.Alignment() <= t1.Alignment()
2159 && t3.Alignment() <= t1.Alignment()
2160 && t4.Alignment() <= t1.Alignment()
2161 && t5.Alignment() <= t1.Alignment()
2162 && t6.Alignment() <= t1.Alignment()
2163 && registerizable(b, t2)
2164 && registerizable(b, t3)
2165 && registerizable(b, t4)
2166 && registerizable(b, t5)
2167 && n >= o2 + t2.Size()
2168 && n >= o3 + t3.Size()
2169 && n >= o4 + t4.Size()
2170 && n >= o5 + t5.Size()
2171 => (Store {t2} (OffPtr <tt2> [o2] dst) d1
2172 (Store {t3} (OffPtr <tt3> [o3] dst) d2
2173 (Store {t4} (OffPtr <tt4> [o4] dst) d3
2174 (Store {t5} (OffPtr <tt5> [o5] dst) d4
2175 (Zero {t1} [n] dst mem)))))
2176
2177 (SelectN [0] call:(StaticLECall {sym} a x)) && needRaceCleanup(sym, call) && ssa.Clobber(call) => x
2178 (SelectN [0] call:(StaticLECall {sym} x)) && needRaceCleanup(sym, call) && ssa.Clobber(call) => x
2179
2180 // When rewriting append to growslice, we use as the new length the result of
2181 // growslice so that we don't have to spill/restore the new length around the growslice call.
2182 // The exception here is that if the new length is a constant, avoiding spilling it
2183 // is pointless and its constantness is sometimes useful for subsequent optimizations.
2184 // See issue 56440.
2185 // Note there are 2 rules here, one for the pre-decomposed []T result and one for
2186 // the post-decomposed (*T,int,int) result. (The latter is generated after call expansion.)
2187 // TODO(thepudds): we probably need the new growsliceBuf and growsliceBufNoAlias here as well?
2188 (SliceLen (SelectN [0] (StaticLECall {sym} _ newLen:(Const(64|32)) _ _ _ _)))
2189 && (ssa.IsSameCall(sym, "runtime.growslice") || ssa.IsSameCall(sym, "runtime.growsliceNoAlias"))
2190 => newLen
2191 (SelectN [1] (StaticCall {sym} _ newLen:(Const(64|32)) _ _ _ _)) && v.Type.IsInteger()
2192 && (ssa.IsSameCall(sym, "runtime.growslice") || ssa.IsSameCall(sym, "runtime.growsliceNoAlias"))
2193 => newLen
2194
2195 // Collapse moving A -> B -> C into just A -> C.
2196 // Later passes (deadstore, elim unread auto) will remove the A -> B move, if possible.
2197 // This happens most commonly when B is an autotmp inserted earlier
2198 // during compilation to ensure correctness.
2199 // Take care that overlapping moves are preserved.
2200 // Restrict this optimization to the stack, to avoid duplicating loads from the heap;
2201 // see CL 145208 for discussion.
2202 (Move {t1} [s] dst tmp1 midmem:(Move {t2} [s] tmp2 src _))
2203 && t1.Compare(t2) == types.CMPeq
2204 && ssa.IsSamePtr(tmp1, tmp2)
2205 && isStackPtr(src) && !ssa.IsVolatile(src)
2206 && ssa.Disjoint1(src, s, tmp2, s)
2207 && (ssa.Disjoint1(src, s, dst, s) || ssa.IsInlinableMemmove(dst, src, s, config))
2208 => (Move {t1} [s] dst src midmem)
2209
2210 // Same, but for large types that require VarDefs.
2211 (Move {t1} [s] dst tmp1 midmem:(VarDef (Move {t2} [s] tmp2 src _)))
2212 && t1.Compare(t2) == types.CMPeq
2213 && ssa.IsSamePtr(tmp1, tmp2)
2214 && isStackPtr(src) && !ssa.IsVolatile(src)
2215 && ssa.Disjoint1(src, s, tmp2, s)
2216 && (ssa.Disjoint1(src, s, dst, s) || ssa.IsInlinableMemmove(dst, src, s, config))
2217 => (Move {t1} [s] dst src midmem)
2218
2219 // Don't zero the same bits twice.
2220 (Zero {t} [s] dst1 zero:(Zero {t} [s] dst2 _)) && ssa.IsSamePtr(dst1, dst2) => zero
2221 (Zero {t} [s] dst1 vardef:(VarDef (Zero {t} [s] dst2 _))) && ssa.IsSamePtr(dst1, dst2) => vardef
2222
2223 // Elide self-moves. This only happens rarely (e.g test/fixedbugs/bug277.go).
2224 // However, this rule is needed to prevent the previous rule from looping forever in such cases.
2225 (Move dst src mem) && ssa.IsSamePtr(dst, src) => mem
2226
2227 // Constant rotate detection.
2228 ((Add64|Or64|Xor64) (Lsh64x64 x z:(Const64 <t> [c])) (Rsh64Ux64 x (Const64 [d]))) && c < 64 && d == 64-c && canRotate(config, 64) => (RotateLeft64 x z)
2229 ((Add32|Or32|Xor32) (Lsh32x64 x z:(Const64 <t> [c])) (Rsh32Ux64 x (Const64 [d]))) && c < 32 && d == 32-c && canRotate(config, 32) => (RotateLeft32 x z)
2230 ((Add16|Or16|Xor16) (Lsh16x64 x z:(Const64 <t> [c])) (Rsh16Ux64 x (Const64 [d]))) && c < 16 && d == 16-c && canRotate(config, 16) => (RotateLeft16 x z)
2231 ((Add8|Or8|Xor8) (Lsh8x64 x z:(Const64 <t> [c])) (Rsh8Ux64 x (Const64 [d]))) && c < 8 && d == 8-c && canRotate(config, 8) => (RotateLeft8 x z)
2232
2233 // Non-constant rotate detection.
2234 // We use shiftIsBounded to make sure that neither of the shifts are >64.
2235 // Note: these rules are subtle when the shift amounts are 0/64, as Go shifts
2236 // are different from most native shifts. But it works out.
2237 ((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)
2238 ((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)
2239 ((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)
2240 ((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)
2241
2242 ((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)
2243 ((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)
2244 ((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)
2245 ((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)
2246
2247 ((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)
2248 ((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)
2249 ((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)
2250 ((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)
2251
2252 ((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)
2253 ((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)
2254 ((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)
2255 ((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)
2256
2257 ((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)
2258 ((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)
2259 ((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)
2260 ((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)
2261
2262 ((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)
2263 ((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)
2264 ((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)
2265 ((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)
2266
2267 ((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)
2268 ((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)
2269 ((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)
2270 ((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)
2271
2272 ((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)
2273 ((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)
2274 ((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)
2275 ((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)
2276
2277 // Rotating by y&c, with c a mask that doesn't change the bottom bits, is the same as rotating by y.
2278 (RotateLeft64 x (And(64|32|16|8) y (Const(64|32|16|8) [c]))) && c&63 == 63 => (RotateLeft64 x y)
2279 (RotateLeft32 x (And(64|32|16|8) y (Const(64|32|16|8) [c]))) && c&31 == 31 => (RotateLeft32 x y)
2280 (RotateLeft16 x (And(64|32|16|8) y (Const(64|32|16|8) [c]))) && c&15 == 15 => (RotateLeft16 x y)
2281 (RotateLeft8 x (And(64|32|16|8) y (Const(64|32|16|8) [c]))) && c&7 == 7 => (RotateLeft8 x y)
2282
2283 // Rotating by -(y&c), with c a mask that doesn't change the bottom bits, is the same as rotating by -y.
2284 (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))
2285 (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))
2286 (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))
2287 (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))
2288
2289 // Rotating by y+c, with c a multiple of the value width, is the same as rotating by y.
2290 (RotateLeft64 x (Add(64|32|16|8) y (Const(64|32|16|8) [c]))) && c&63 == 0 => (RotateLeft64 x y)
2291 (RotateLeft32 x (Add(64|32|16|8) y (Const(64|32|16|8) [c]))) && c&31 == 0 => (RotateLeft32 x y)
2292 (RotateLeft16 x (Add(64|32|16|8) y (Const(64|32|16|8) [c]))) && c&15 == 0 => (RotateLeft16 x y)
2293 (RotateLeft8 x (Add(64|32|16|8) y (Const(64|32|16|8) [c]))) && c&7 == 0 => (RotateLeft8 x y)
2294
2295 // Rotating by c-y, with c a multiple of the value width, is the same as rotating by -y.
2296 (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))
2297 (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))
2298 (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))
2299 (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))
2300
2301 // Ensure we don't do Const64 rotates in a 32-bit system.
2302 (RotateLeft(64|32|16|8) x (Const64 [c])) && config.RegSize == 4 => (RotateLeft(64|32|16|8) x (Const32 <typ.UInt32> [int32(c)]))
2303
2304 // Rotating by c, then by d, is the same as rotating by c+d.
2305 // We're trading a rotate for an add, which seems generally a good choice. It is especially good when c and d are constants.
2306 // This rule is a bit tricky as c and d might be different widths. We handle only cases where they are the same width.
2307 (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))
2308 (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))
2309 (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))
2310 (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))
2311
2312 // Loading fixed addresses and constants.
2313 (Load (Addr {s} sb) _) && isFixedLoad(v, s, 0) => rewriteFixedLoad(v, s, sb, 0)
2314 (Load (Convert (Addr {s} sb) _) _) && isFixedLoad(v, s, 0) => rewriteFixedLoad(v, s, sb, 0)
2315 (Load (ITab (IMake (Addr {s} sb) _)) _) && isFixedLoad(v, s, 0) => rewriteFixedLoad(v, s, sb, 0)
2316 (Load (ITab (IMake (Convert (Addr {s} sb) _) _)) _) && isFixedLoad(v, s, 0) => rewriteFixedLoad(v, s, sb, 0)
2317 (Load (OffPtr [off] (Addr {s} sb) ) _) && isFixedLoad(v, s, off) => rewriteFixedLoad(v, s, sb, off)
2318 (Load (OffPtr [off] (Convert (Addr {s} sb) _) ) _) && isFixedLoad(v, s, off) => rewriteFixedLoad(v, s, sb, off)
2319 (Load (OffPtr [off] (ITab (IMake (Addr {s} sb) _))) _) && isFixedLoad(v, s, off) => rewriteFixedLoad(v, s, sb, off)
2320 (Load (OffPtr [off] (ITab (IMake (Convert (Addr {s} sb) _) _))) _) && isFixedLoad(v, s, off) => rewriteFixedLoad(v, s, sb, off)
2321
2322 // Calling cmpstring a second time with the same arguments in the
2323 // same memory state can reuse the results of the first call.
2324 // See issue 61725.
2325 // Note that this could pretty easily generalize to any pure function.
2326 (SelectN [0] (StaticLECall {f} x y (SelectN [1] c:(StaticLECall {g} x y mem))))
2327 && ssa.IsSameCall(f, "runtime.cmpstring")
2328 && ssa.IsSameCall(g, "runtime.cmpstring")
2329 => @c.Block (SelectN [0] <typ.Int> c)
2330
2331 // If we don't use the result of cmpstring, might as well not call it.
2332 // Note that this could pretty easily generalize to any pure function.
2333 (SelectN [1] c:(StaticLECall {f} _ _ mem)) && c.Uses == 1 && ssa.IsSameCall(f, "runtime.cmpstring") && ssa.Clobber(c) => mem
2334
2335 // We can easily compute the result of efaceeq if
2336 // we know the underlying type is pointer-ish.
2337 (StaticLECall {f} typ_ x y mem)
2338 && ssa.IsSameCall(f, "runtime.efaceeq")
2339 && isDirectAndComparableType(typ_)
2340 && ssa.Clobber(v)
2341 => (MakeResult (EqPtr x y) mem)
2342
2343 // We can easily compute the result of ifaceeq if
2344 // we know the underlying type is pointer-ish.
2345 (StaticLECall {f} itab x y mem)
2346 && ssa.IsSameCall(f, "runtime.ifaceeq")
2347 && isDirectAndComparableIface(itab)
2348 && ssa.Clobber(v)
2349 => (MakeResult (EqPtr x y) mem)
2350
2351 // If we use the result of slicebytetostring in a map lookup operation,
2352 // then we don't need to actually do the []byte->string conversion.
2353 // We can just use the ptr/len of the byte slice directly as a (temporary) string.
2354 //
2355 // Note that this does not handle some obscure cases like
2356 // m[[2]string{string(b1), string(b2)}]. There is code in ../walk/order.go
2357 // which handles some of those cases.
2358 (StaticLECall {f} [argsize] typ_ map_ key:(SelectN [0] sbts:(StaticLECall {g} _ ptr len mem)) m:(SelectN [1] sbts))
2359 && (ssa.IsSameCall(f, "runtime.mapaccess1_faststr")
2360 || ssa.IsSameCall(f, "runtime.mapaccess2_faststr")
2361 || ssa.IsSameCall(f, "runtime.mapdelete_faststr"))
2362 && ssa.IsSameCall(g, "runtime.slicebytetostring")
2363 && key.Uses == 1
2364 && sbts.Uses == 2
2365 && resetCopy(m, mem)
2366 && ssa.Clobber(sbts)
2367 && ssa.Clobber(key)
2368 => (StaticLECall {f} [argsize] typ_ map_ (StringMake <typ.String> ptr len) mem)
2369
2370 // Similarly to map lookups, also handle unique.Make for strings, which unique.Make will clone.
2371 (StaticLECall {f} [argsize] dict_ key:(SelectN [0] sbts:(StaticLECall {g} _ ptr len mem)) m:(SelectN [1] sbts))
2372 && ssa.IsSameCall(f, "unique.Make[go.shape.string]")
2373 && ssa.IsSameCall(g, "runtime.slicebytetostring")
2374 && key.Uses == 1
2375 && sbts.Uses == 2
2376 && resetCopy(m, mem)
2377 && ssa.Clobber(sbts)
2378 && ssa.Clobber(key)
2379 => (StaticLECall {f} [argsize] dict_ (StringMake <typ.String> ptr len) mem)
2380
2381 // Transform some CondSelect into math operations.
2382 // if b { x += c } => x += b * c
2383 (CondSelect op1:(Add8 <t> x c:(Const8)) x bool) &&
2384 rewriteCondSelectIntoMath(config, op1.Op, c.AuxInt) =>
2385 (Add8 x (Mul8 <t> c (CvtBoolToUint8 <t> bool)))
2386 (CondSelect op1:(Add(64|32|16) <t> x c:(Const(64|32|16))) x bool) &&
2387 rewriteCondSelectIntoMath(config, op1.Op, c.AuxInt) =>
2388 (Add(64|32|16) x (Mul(64|32|16) <t> c (ZeroExt8to(64|32|16) <t> (CvtBoolToUint8 <types.Types[types.TUINT8]> bool))))
2389 // if !b { x += c } => x += !b * c
2390 (CondSelect x op1:(Add8 <t> x c:(Const8)) bool) &&
2391 rewriteCondSelectIntoMath(config, op1.Op, c.AuxInt) =>
2392 (Add8 x (Mul8 <t> c (CvtBoolToUint8 <t> (Not <bool.Type> bool))))
2393 (CondSelect x op1:(Add(64|32|16) <t> x c:(Const(64|32|16))) bool) &&
2394 rewriteCondSelectIntoMath(config, op1.Op, c.AuxInt) =>
2395 (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)))))
2396
2397 // if b { x += c } => x -= b * -c // looks redundant but useful if the arch can inline materialize -c but not c
2398 (CondSelect op1:(Add8 <t> x c:(Const8 [consT])) x bool) &&
2399 !rewriteCondSelectIntoMath(config, op1.Op, c.AuxInt) &&
2400 rewriteCondSelectIntoMath(config, addToSub(op1.Op), -c.AuxInt) =>
2401 (Sub8 x (Mul8 <t> (Const8 <t> [-consT]) (CvtBoolToUint8 <t> bool)))
2402 (CondSelect op1:(Add(64|32|16) <t> x c:(Const(64|32|16) [consT])) x bool) &&
2403 !rewriteCondSelectIntoMath(config, op1.Op, c.AuxInt) &&
2404 rewriteCondSelectIntoMath(config, addToSub(op1.Op), -c.AuxInt) =>
2405 (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))))
2406 // if !b { x += c } => x -= !b * -c // looks redundant but useful if the arch can inline materialize -c but not c
2407 (CondSelect x op1:(Add8 <t> x c:(Const8 [consT])) bool) &&
2408 !rewriteCondSelectIntoMath(config, op1.Op, c.AuxInt) &&
2409 rewriteCondSelectIntoMath(config, addToSub(op1.Op), -c.AuxInt) =>
2410 (Sub8 x (Mul8 <t> (Const8 <t> [-consT]) (CvtBoolToUint8 <t> (Not <bool.Type> bool))))
2411 (CondSelect x op1:(Add(64|32|16) <t> x c:(Const(64|32|16) [consT])) bool) &&
2412 !rewriteCondSelectIntoMath(config, op1.Op, c.AuxInt) &&
2413 rewriteCondSelectIntoMath(config, addToSub(op1.Op), -c.AuxInt) =>
2414 (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)))))
2415
2416 // if b { x <<= 1 } => x <<= b
2417 (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))
2418 // if !b { x <<= 1 } => x <<= !b
2419 (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)))
2420
2421 // if b { x >>= 1 } => x >>= b
2422 (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))
2423 (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))
2424 // if !b { x >>= 1 } => x >>= !b
2425 (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)))
2426 (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)))
2427
2428 // if b { x |= c } => x |= b * c
2429 (CondSelect op1:(Or8 <t> x c:(Const8)) x bool) &&
2430 rewriteCondSelectIntoMath(config, op1.Op, c.AuxInt) =>
2431 (Or8 x (Mul8 <t> c (CvtBoolToUint8 <t> bool)))
2432 (CondSelect op1:(Or(64|32|16) <t> x c:(Const(64|32|16))) x bool) &&
2433 rewriteCondSelectIntoMath(config, op1.Op, c.AuxInt) =>
2434 (Or(64|32|16) x (Mul(64|32|16) <t> c (ZeroExt8to(64|32|16) <t> (CvtBoolToUint8 <types.Types[types.TUINT8]> bool))))
2435 // if !b { x |= c } => x |= !b * c
2436 (CondSelect x op1:(Or8 <t> x c:(Const8)) bool) &&
2437 rewriteCondSelectIntoMath(config, op1.Op, c.AuxInt) =>
2438 (Or8 x (Mul8 <t> c (CvtBoolToUint8 <t> (Not <bool.Type> bool))))
2439 (CondSelect x op1:(Or(64|32|16) <t> x c:(Const(64|32|16))) bool) &&
2440 rewriteCondSelectIntoMath(config, op1.Op, c.AuxInt) =>
2441 (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)))))
2442
2443 // if b { x ^= c } => x ^= b * c
2444 (CondSelect op1:(Xor8 <t> x c:(Const8)) x bool) &&
2445 rewriteCondSelectIntoMath(config, op1.Op, c.AuxInt) =>
2446 (Xor8 x (Mul8 <t> c (CvtBoolToUint8 <t> bool)))
2447 (CondSelect op1:(Xor(64|32|16) <t> x c:(Const(64|32|16))) x bool) &&
2448 rewriteCondSelectIntoMath(config, op1.Op, c.AuxInt) =>
2449 (Xor(64|32|16) x (Mul(64|32|16) <t> c (ZeroExt8to(64|32|16) <t> (CvtBoolToUint8 <types.Types[types.TUINT8]> bool))))
2450 // if !b { x ^= c } => x ^= !b * c
2451 (CondSelect x op1:(Xor8 <t> x c:(Const8)) bool) &&
2452 rewriteCondSelectIntoMath(config, op1.Op, c.AuxInt) =>
2453 (Xor8 x (Mul8 <t> c (CvtBoolToUint8 <t> (Not <bool.Type> bool))))
2454 (CondSelect x op1:(Xor(64|32|16) <t> x c:(Const(64|32|16))) bool) &&
2455 rewriteCondSelectIntoMath(config, op1.Op, c.AuxInt) =>
2456 (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)))))
2457
2458 // bool(int(x)) => x
2459 (Neq8 (CvtBoolToUint8 x) (Const8 [0])) => x
2460 (Neq8 (CvtBoolToUint8 x) (Const8 [1])) => (Not x)
2461 (Eq8 (CvtBoolToUint8 x) (Const8 [1])) => x
2462 (Eq8 (CvtBoolToUint8 x) (Const8 [0])) => (Not x)
2463 (Neq(64|32|16) (ZeroExt8to(64|32|16) (CvtBoolToUint8 x)) (Const(64|32|16) [0])) => x
2464 (Neq(64|32|16) (ZeroExt8to(64|32|16) (CvtBoolToUint8 x)) (Const(64|32|16) [1])) => (Not x)
2465 (Eq(64|32|16) (ZeroExt8to(64|32|16) (CvtBoolToUint8 x)) (Const(64|32|16) [1])) => x
2466 (Eq(64|32|16) (ZeroExt8to(64|32|16) (CvtBoolToUint8 x)) (Const(64|32|16) [0])) => (Not x)
2467
2468 // Canonicalize sext(int(bool)) => zext(int(bool))
2469 (SignExt8to(64|32|16) cvt:(CvtBoolToUint8 bool)) => (ZeroExt8to(64|32|16) cvt)
2470
2471 // int(bool)^1 => int(!bool)
2472 (Xor8 (CvtBoolToUint8 bool) (Const8 [1])) && invertibleBool(bool.Op) => (CvtBoolToUint8 (Not <bool.Type> bool))
2473 (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)))
2474
2475 // int(!bool)^c => int(bool)^(c^1)
2476 (Xor8 (CvtBoolToUint8 <cvtT> (Not bool)) (Const8 <constT> [c])) && c != 1 => (Xor8 (CvtBoolToUint8 <cvtT> bool) (Const8 <constT> [c^1]))
2477 (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]))
2478
View as plain text