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