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