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