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