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 (Less(64|32|16|8) (Const(64|32|16|8) <t> [0]) x) && isNonNegative(x) => (Neq(64|32|16|8) (Const(64|32|16|8) <t> [0]) x)
504 (Less(64|32|16|8) x (Const(64|32|16|8) <t> [1])) && isNonNegative(x) => (Eq(64|32|16|8) (Const(64|32|16|8) <t> [0]) x)
505
506 // constant floating point comparisons
507 (Eq32F (Const32F [c]) (Const32F [d])) => (ConstBool [c == d])
508 (Eq64F (Const64F [c]) (Const64F [d])) => (ConstBool [c == d])
509 (Neq32F (Const32F [c]) (Const32F [d])) => (ConstBool [c != d])
510 (Neq64F (Const64F [c]) (Const64F [d])) => (ConstBool [c != d])
511 (Less32F (Const32F [c]) (Const32F [d])) => (ConstBool [c < d])
512 (Less64F (Const64F [c]) (Const64F [d])) => (ConstBool [c < d])
513 (Leq32F (Const32F [c]) (Const32F [d])) => (ConstBool [c <= d])
514 (Leq64F (Const64F [c]) (Const64F [d])) => (ConstBool [c <= d])
515
516 // simplifications
517 (Or(64|32|16|8) x x) => x
518 (Or(64|32|16|8) (Const(64|32|16|8) [0]) x) => x
519 (Or(64|32|16|8) (Const(64|32|16|8) [-1]) _) => (Const(64|32|16|8) [-1])
520
521 (And(64|32|16|8) x x) => x
522 (And(64|32|16|8) (Const(64|32|16|8) [-1]) x) => x
523 (And(64|32|16|8) (Const(64|32|16|8) [0]) _) => (Const(64|32|16|8) [0])
524
525 (Xor(64|32|16|8) x x) => (Const(64|32|16|8) [0])
526 (Xor(64|32|16|8) (Const(64|32|16|8) [0]) x) => x
527
528 (Add(64|32|16|8) (Const(64|32|16|8) [0]) x) => x
529 (Sub(64|32|16|8) x x) => (Const(64|32|16|8) [0])
530 (Mul(64|32|16|8) (Const(64|32|16|8) [0]) _) => (Const(64|32|16|8) [0])
531
532 (Com(64|32|16|8) (Com(64|32|16|8) x)) => x
533 (Com(64|32|16|8) (Const(64|32|16|8) [c])) => (Const(64|32|16|8) [^c])
534
535 (Neg(64|32|16|8) (Sub(64|32|16|8) x y)) => (Sub(64|32|16|8) y x)
536
537 // ^(x-1) == ^x+1 == -x
538 (Add(64|32|16|8) (Const(64|32|16|8) [1]) (Com(64|32|16|8) x)) => (Neg(64|32|16|8) x)
539 (Com(64|32|16|8) (Add(64|32|16|8) (Const(64|32|16|8) [-1]) x)) => (Neg(64|32|16|8) x)
540
541 // -(-x) == x
542 (Neg(64|32|16|8) (Neg(64|32|16|8) x)) => x
543
544 // -^x == x+1
545 (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)
546
547 (And(64|32|16|8) x (And(64|32|16|8) x y)) => (And(64|32|16|8) x y)
548 (Or(64|32|16|8) x (Or(64|32|16|8) x y)) => (Or(64|32|16|8) x y)
549 (Xor(64|32|16|8) x (Xor(64|32|16|8) x y)) => y
550
551 // Unsigned comparisons to zero.
552 (Less(64U|32U|16U|8U) _ (Const(64|32|16|8) [0])) => (ConstBool [false])
553 (Leq(64U|32U|16U|8U) (Const(64|32|16|8) [0]) _) => (ConstBool [true])
554
555 // Ands clear bits. Ors set bits.
556 // If a subsequent Or will set all the bits
557 // that an And cleared, we can skip the And.
558 // This happens in bitmasking code like:
559 // x &^= 3 << shift // clear two old bits
560 // x |= v << shift // set two new bits
561 // when shift is a small constant and v ends up a constant 3.
562 (Or8 (And8 x (Const8 [c2])) (Const8 <t> [c1])) && ^(c1 | c2) == 0 => (Or8 (Const8 <t> [c1]) x)
563 (Or16 (And16 x (Const16 [c2])) (Const16 <t> [c1])) && ^(c1 | c2) == 0 => (Or16 (Const16 <t> [c1]) x)
564 (Or32 (And32 x (Const32 [c2])) (Const32 <t> [c1])) && ^(c1 | c2) == 0 => (Or32 (Const32 <t> [c1]) x)
565 (Or64 (And64 x (Const64 [c2])) (Const64 <t> [c1])) && ^(c1 | c2) == 0 => (Or64 (Const64 <t> [c1]) x)
566
567 (Trunc64to8 (And64 (Const64 [y]) x)) && y&0xFF == 0xFF => (Trunc64to8 x)
568 (Trunc64to16 (And64 (Const64 [y]) x)) && y&0xFFFF == 0xFFFF => (Trunc64to16 x)
569 (Trunc64to32 (And64 (Const64 [y]) x)) && y&0xFFFFFFFF == 0xFFFFFFFF => (Trunc64to32 x)
570 (Trunc32to8 (And32 (Const32 [y]) x)) && y&0xFF == 0xFF => (Trunc32to8 x)
571 (Trunc32to16 (And32 (Const32 [y]) x)) && y&0xFFFF == 0xFFFF => (Trunc32to16 x)
572 (Trunc16to8 (And16 (Const16 [y]) x)) && y&0xFF == 0xFF => (Trunc16to8 x)
573
574 (ZeroExt8to64 (Trunc64to8 x:(Rsh64Ux64 _ (Const64 [s])))) && s >= 56 => x
575 (ZeroExt16to64 (Trunc64to16 x:(Rsh64Ux64 _ (Const64 [s])))) && s >= 48 => x
576 (ZeroExt32to64 (Trunc64to32 x:(Rsh64Ux64 _ (Const64 [s])))) && s >= 32 => x
577 (ZeroExt8to32 (Trunc32to8 x:(Rsh32Ux64 _ (Const64 [s])))) && s >= 24 => x
578 (ZeroExt16to32 (Trunc32to16 x:(Rsh32Ux64 _ (Const64 [s])))) && s >= 16 => x
579 (ZeroExt8to16 (Trunc16to8 x:(Rsh16Ux64 _ (Const64 [s])))) && s >= 8 => x
580
581 (SignExt8to64 (Trunc64to8 x:(Rsh64x64 _ (Const64 [s])))) && s >= 56 => x
582 (SignExt16to64 (Trunc64to16 x:(Rsh64x64 _ (Const64 [s])))) && s >= 48 => x
583 (SignExt32to64 (Trunc64to32 x:(Rsh64x64 _ (Const64 [s])))) && s >= 32 => x
584 (SignExt8to32 (Trunc32to8 x:(Rsh32x64 _ (Const64 [s])))) && s >= 24 => x
585 (SignExt16to32 (Trunc32to16 x:(Rsh32x64 _ (Const64 [s])))) && s >= 16 => x
586 (SignExt8to16 (Trunc16to8 x:(Rsh16x64 _ (Const64 [s])))) && s >= 8 => x
587
588 (Slicemask (Const32 [x])) && x > 0 => (Const32 [-1])
589 (Slicemask (Const32 [0])) => (Const32 [0])
590 (Slicemask (Const64 [x])) && x > 0 => (Const64 [-1])
591 (Slicemask (Const64 [0])) => (Const64 [0])
592
593 // simplifications often used for lengths. e.g. len(s[i:i+5])==5
594 (Sub(64|32|16|8) (Add(64|32|16|8) x y) x) => y
595 (Sub(64|32|16|8) (Add(64|32|16|8) x y) y) => x
596 (Sub(64|32|16|8) (Sub(64|32|16|8) x y) x) => (Neg(64|32|16|8) y)
597 (Sub(64|32|16|8) x (Add(64|32|16|8) x y)) => (Neg(64|32|16|8) y)
598 (Add(64|32|16|8) x (Sub(64|32|16|8) y x)) => y
599 (Add(64|32|16|8) x (Add(64|32|16|8) y (Sub(64|32|16|8) z x))) => (Add(64|32|16|8) y z)
600
601 // basic phi simplifications
602 (Phi (Const8 [c]) (Const8 [c])) => (Const8 [c])
603 (Phi (Const16 [c]) (Const16 [c])) => (Const16 [c])
604 (Phi (Const32 [c]) (Const32 [c])) => (Const32 [c])
605 (Phi (Const64 [c]) (Const64 [c])) => (Const64 [c])
606
607 // slice and interface comparisons
608 // The frontend ensures that we can only compare against nil,
609 // so we need only compare the first word (interface type or slice ptr).
610 (EqInter x y) => (EqPtr (ITab x) (ITab y))
611 (NeqInter x y) => (NeqPtr (ITab x) (ITab y))
612 (EqSlice x y) => (EqPtr (SlicePtr x) (SlicePtr y))
613 (NeqSlice x y) => (NeqPtr (SlicePtr x) (SlicePtr y))
614
615 // Load of store of same address, with compatibly typed value and same size
616 (Load <t1> p1 (Store {t2} p2 x _))
617 && isSamePtr(p1, p2)
618 && t1.Compare(x.Type) == types.CMPeq
619 && t1.Size() == t2.Size()
620 => x
621 (Load <t1> p1 (Store {t2} p2 _ (Store {t3} p3 x _)))
622 && isSamePtr(p1, p3)
623 && t1.Compare(x.Type) == types.CMPeq
624 && t1.Size() == t2.Size()
625 && disjoint(p3, t3.Size(), p2, t2.Size())
626 => x
627 (Load <t1> p1 (Store {t2} p2 _ (Store {t3} p3 _ (Store {t4} p4 x _))))
628 && isSamePtr(p1, p4)
629 && t1.Compare(x.Type) == types.CMPeq
630 && t1.Size() == t2.Size()
631 && disjoint(p4, t4.Size(), p2, t2.Size())
632 && disjoint(p4, t4.Size(), p3, t3.Size())
633 => x
634 (Load <t1> p1 (Store {t2} p2 _ (Store {t3} p3 _ (Store {t4} p4 _ (Store {t5} p5 x _)))))
635 && isSamePtr(p1, p5)
636 && t1.Compare(x.Type) == types.CMPeq
637 && t1.Size() == t2.Size()
638 && disjoint(p5, t5.Size(), p2, t2.Size())
639 && disjoint(p5, t5.Size(), p3, t3.Size())
640 && disjoint(p5, t5.Size(), p4, t4.Size())
641 => x
642
643 // Pass constants through math.Float{32,64}bits and math.Float{32,64}frombits
644 (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))])
645 (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))])
646 (Load <t1> p1 (Store {t2} p2 (Const64F [x]) _)) && isSamePtr(p1,p2) && sizeof(t2) == 8 && is64BitInt(t1) => (Const64 [int64(math.Float64bits(x))])
647 (Load <t1> p1 (Store {t2} p2 (Const32F [x]) _)) && isSamePtr(p1,p2) && sizeof(t2) == 4 && is32BitInt(t1) => (Const32 [int32(math.Float32bits(x))])
648
649 // Float Loads up to Zeros so they can be constant folded.
650 (Load <t1> op:(OffPtr [o1] p1)
651 (Store {t2} p2 _
652 mem:(Zero [n] p3 _)))
653 && o1 >= 0 && o1+t1.Size() <= n && isSamePtr(p1, p3)
654 && fe.CanSSA(t1)
655 && disjoint(op, t1.Size(), p2, t2.Size())
656 => @mem.Block (Load <t1> (OffPtr <op.Type> [o1] p3) mem)
657 (Load <t1> op:(OffPtr [o1] p1)
658 (Store {t2} p2 _
659 (Store {t3} p3 _
660 mem:(Zero [n] p4 _))))
661 && o1 >= 0 && o1+t1.Size() <= n && isSamePtr(p1, p4)
662 && fe.CanSSA(t1)
663 && disjoint(op, t1.Size(), p2, t2.Size())
664 && disjoint(op, t1.Size(), p3, t3.Size())
665 => @mem.Block (Load <t1> (OffPtr <op.Type> [o1] p4) mem)
666 (Load <t1> op:(OffPtr [o1] p1)
667 (Store {t2} p2 _
668 (Store {t3} p3 _
669 (Store {t4} p4 _
670 mem:(Zero [n] p5 _)))))
671 && o1 >= 0 && o1+t1.Size() <= n && isSamePtr(p1, p5)
672 && fe.CanSSA(t1)
673 && disjoint(op, t1.Size(), p2, t2.Size())
674 && disjoint(op, t1.Size(), p3, t3.Size())
675 && disjoint(op, t1.Size(), p4, t4.Size())
676 => @mem.Block (Load <t1> (OffPtr <op.Type> [o1] p5) mem)
677 (Load <t1> op:(OffPtr [o1] p1)
678 (Store {t2} p2 _
679 (Store {t3} p3 _
680 (Store {t4} p4 _
681 (Store {t5} p5 _
682 mem:(Zero [n] p6 _))))))
683 && o1 >= 0 && o1+t1.Size() <= n && isSamePtr(p1, p6)
684 && fe.CanSSA(t1)
685 && disjoint(op, t1.Size(), p2, t2.Size())
686 && disjoint(op, t1.Size(), p3, t3.Size())
687 && disjoint(op, t1.Size(), p4, t4.Size())
688 && disjoint(op, t1.Size(), p5, t5.Size())
689 => @mem.Block (Load <t1> (OffPtr <op.Type> [o1] p6) mem)
690
691 // Zero to Load forwarding.
692 (Load <t1> (OffPtr [o] p1) (Zero [n] p2 _))
693 && t1.IsBoolean()
694 && isSamePtr(p1, p2)
695 && n >= o + 1
696 => (ConstBool [false])
697 (Load <t1> (OffPtr [o] p1) (Zero [n] p2 _))
698 && is8BitInt(t1)
699 && isSamePtr(p1, p2)
700 && n >= o + 1
701 => (Const8 [0])
702 (Load <t1> (OffPtr [o] p1) (Zero [n] p2 _))
703 && is16BitInt(t1)
704 && isSamePtr(p1, p2)
705 && n >= o + 2
706 => (Const16 [0])
707 (Load <t1> (OffPtr [o] p1) (Zero [n] p2 _))
708 && is32BitInt(t1)
709 && isSamePtr(p1, p2)
710 && n >= o + 4
711 => (Const32 [0])
712 (Load <t1> (OffPtr [o] p1) (Zero [n] p2 _))
713 && is64BitInt(t1)
714 && isSamePtr(p1, p2)
715 && n >= o + 8
716 => (Const64 [0])
717 (Load <t1> (OffPtr [o] p1) (Zero [n] p2 _))
718 && is32BitFloat(t1)
719 && isSamePtr(p1, p2)
720 && n >= o + 4
721 => (Const32F [0])
722 (Load <t1> (OffPtr [o] p1) (Zero [n] p2 _))
723 && is64BitFloat(t1)
724 && isSamePtr(p1, p2)
725 && n >= o + 8
726 => (Const64F [0])
727
728 // Eliminate stores of values that have just been loaded from the same location.
729 // We also handle the common case where there are some intermediate stores.
730 (Store {t1} p1 (Load <t2> p2 mem) mem)
731 && isSamePtr(p1, p2)
732 && t2.Size() == t1.Size()
733 => mem
734 (Store {t1} p1 (Load <t2> p2 oldmem) mem:(Store {t3} p3 _ oldmem))
735 && isSamePtr(p1, p2)
736 && t2.Size() == t1.Size()
737 && disjoint(p1, t1.Size(), p3, t3.Size())
738 => mem
739 (Store {t1} p1 (Load <t2> p2 oldmem) mem:(Store {t3} p3 _ (Store {t4} p4 _ oldmem)))
740 && isSamePtr(p1, p2)
741 && t2.Size() == t1.Size()
742 && disjoint(p1, t1.Size(), p3, t3.Size())
743 && disjoint(p1, t1.Size(), p4, t4.Size())
744 => mem
745 (Store {t1} p1 (Load <t2> p2 oldmem) mem:(Store {t3} p3 _ (Store {t4} p4 _ (Store {t5} p5 _ oldmem))))
746 && isSamePtr(p1, p2)
747 && t2.Size() == t1.Size()
748 && disjoint(p1, t1.Size(), p3, t3.Size())
749 && disjoint(p1, t1.Size(), p4, t4.Size())
750 && disjoint(p1, t1.Size(), p5, t5.Size())
751 => mem
752
753 // Don't Store zeros to cleared variables.
754 (Store {t} (OffPtr [o] p1) x mem:(Zero [n] p2 _))
755 && isConstZero(x)
756 && o >= 0 && t.Size() + o <= n && isSamePtr(p1, p2)
757 => mem
758 (Store {t1} op:(OffPtr [o1] p1) x mem:(Store {t2} p2 _ (Zero [n] p3 _)))
759 && isConstZero(x)
760 && o1 >= 0 && t1.Size() + o1 <= n && isSamePtr(p1, p3)
761 && disjoint(op, t1.Size(), p2, t2.Size())
762 => mem
763 (Store {t1} op:(OffPtr [o1] p1) x mem:(Store {t2} p2 _ (Store {t3} p3 _ (Zero [n] p4 _))))
764 && isConstZero(x)
765 && o1 >= 0 && t1.Size() + o1 <= n && isSamePtr(p1, p4)
766 && disjoint(op, t1.Size(), p2, t2.Size())
767 && disjoint(op, t1.Size(), p3, t3.Size())
768 => mem
769 (Store {t1} op:(OffPtr [o1] p1) x mem:(Store {t2} p2 _ (Store {t3} p3 _ (Store {t4} p4 _ (Zero [n] p5 _)))))
770 && isConstZero(x)
771 && o1 >= 0 && t1.Size() + o1 <= n && isSamePtr(p1, p5)
772 && disjoint(op, t1.Size(), p2, t2.Size())
773 && disjoint(op, t1.Size(), p3, t3.Size())
774 && disjoint(op, t1.Size(), p4, t4.Size())
775 => mem
776
777 // Collapse OffPtr
778 (OffPtr (OffPtr p [y]) [x]) => (OffPtr p [x+y])
779 (OffPtr p [0]) && v.Type.Compare(p.Type) == types.CMPeq => p
780
781 // indexing operations
782 // Note: bounds check has already been done
783 (PtrIndex <t> ptr idx) && config.PtrSize == 4 && is32Bit(t.Elem().Size()) => (AddPtr ptr (Mul32 <typ.Int> idx (Const32 <typ.Int> [int32(t.Elem().Size())])))
784 (PtrIndex <t> ptr idx) && config.PtrSize == 8 => (AddPtr ptr (Mul64 <typ.Int> idx (Const64 <typ.Int> [t.Elem().Size()])))
785
786 // struct operations
787 (StructSelect (StructMake1 x)) => x
788 (StructSelect [0] (StructMake2 x _)) => x
789 (StructSelect [1] (StructMake2 _ x)) => x
790 (StructSelect [0] (StructMake3 x _ _)) => x
791 (StructSelect [1] (StructMake3 _ x _)) => x
792 (StructSelect [2] (StructMake3 _ _ x)) => x
793 (StructSelect [0] (StructMake4 x _ _ _)) => x
794 (StructSelect [1] (StructMake4 _ x _ _)) => x
795 (StructSelect [2] (StructMake4 _ _ x _)) => x
796 (StructSelect [3] (StructMake4 _ _ _ x)) => x
797
798 (Load <t> _ _) && t.IsStruct() && t.NumFields() == 0 && fe.CanSSA(t) =>
799 (StructMake0)
800 (Load <t> ptr mem) && t.IsStruct() && t.NumFields() == 1 && fe.CanSSA(t) =>
801 (StructMake1
802 (Load <t.FieldType(0)> (OffPtr <t.FieldType(0).PtrTo()> [0] ptr) mem))
803 (Load <t> ptr mem) && t.IsStruct() && t.NumFields() == 2 && fe.CanSSA(t) =>
804 (StructMake2
805 (Load <t.FieldType(0)> (OffPtr <t.FieldType(0).PtrTo()> [0] ptr) mem)
806 (Load <t.FieldType(1)> (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] ptr) mem))
807 (Load <t> ptr mem) && t.IsStruct() && t.NumFields() == 3 && fe.CanSSA(t) =>
808 (StructMake3
809 (Load <t.FieldType(0)> (OffPtr <t.FieldType(0).PtrTo()> [0] ptr) mem)
810 (Load <t.FieldType(1)> (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] ptr) mem)
811 (Load <t.FieldType(2)> (OffPtr <t.FieldType(2).PtrTo()> [t.FieldOff(2)] ptr) mem))
812 (Load <t> ptr mem) && t.IsStruct() && t.NumFields() == 4 && fe.CanSSA(t) =>
813 (StructMake4
814 (Load <t.FieldType(0)> (OffPtr <t.FieldType(0).PtrTo()> [0] ptr) mem)
815 (Load <t.FieldType(1)> (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] ptr) mem)
816 (Load <t.FieldType(2)> (OffPtr <t.FieldType(2).PtrTo()> [t.FieldOff(2)] ptr) mem)
817 (Load <t.FieldType(3)> (OffPtr <t.FieldType(3).PtrTo()> [t.FieldOff(3)] ptr) mem))
818
819 (StructSelect [i] x:(Load <t> ptr mem)) && !fe.CanSSA(t) =>
820 @x.Block (Load <v.Type> (OffPtr <v.Type.PtrTo()> [t.FieldOff(int(i))] ptr) mem)
821
822 (Store _ (StructMake0) mem) => mem
823 (Store dst (StructMake1 <t> f0) mem) =>
824 (Store {t.FieldType(0)} (OffPtr <t.FieldType(0).PtrTo()> [0] dst) f0 mem)
825 (Store dst (StructMake2 <t> f0 f1) mem) =>
826 (Store {t.FieldType(1)}
827 (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] dst)
828 f1
829 (Store {t.FieldType(0)}
830 (OffPtr <t.FieldType(0).PtrTo()> [0] dst)
831 f0 mem))
832 (Store dst (StructMake3 <t> f0 f1 f2) mem) =>
833 (Store {t.FieldType(2)}
834 (OffPtr <t.FieldType(2).PtrTo()> [t.FieldOff(2)] dst)
835 f2
836 (Store {t.FieldType(1)}
837 (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] dst)
838 f1
839 (Store {t.FieldType(0)}
840 (OffPtr <t.FieldType(0).PtrTo()> [0] dst)
841 f0 mem)))
842 (Store dst (StructMake4 <t> f0 f1 f2 f3) mem) =>
843 (Store {t.FieldType(3)}
844 (OffPtr <t.FieldType(3).PtrTo()> [t.FieldOff(3)] dst)
845 f3
846 (Store {t.FieldType(2)}
847 (OffPtr <t.FieldType(2).PtrTo()> [t.FieldOff(2)] dst)
848 f2
849 (Store {t.FieldType(1)}
850 (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] dst)
851 f1
852 (Store {t.FieldType(0)}
853 (OffPtr <t.FieldType(0).PtrTo()> [0] dst)
854 f0 mem))))
855
856 // Putting struct{*byte} and similar into direct interfaces.
857 (IMake _typ (StructMake1 val)) => (IMake _typ val)
858 (StructSelect [0] (IData x)) => (IData x)
859
860 // un-SSAable values use mem->mem copies
861 (Store {t} dst (Load src mem) mem) && !fe.CanSSA(t) =>
862 (Move {t} [t.Size()] dst src mem)
863 (Store {t} dst (Load src mem) (VarDef {x} mem)) && !fe.CanSSA(t) =>
864 (Move {t} [t.Size()] dst src (VarDef {x} mem))
865
866 // array ops
867 (ArraySelect (ArrayMake1 x)) => x
868
869 (Load <t> _ _) && t.IsArray() && t.NumElem() == 0 =>
870 (ArrayMake0)
871
872 (Load <t> ptr mem) && t.IsArray() && t.NumElem() == 1 && fe.CanSSA(t) =>
873 (ArrayMake1 (Load <t.Elem()> ptr mem))
874
875 (Store _ (ArrayMake0) mem) => mem
876 (Store dst (ArrayMake1 e) mem) => (Store {e.Type} dst e mem)
877
878 // Putting [1]*byte and similar into direct interfaces.
879 (IMake _typ (ArrayMake1 val)) => (IMake _typ val)
880 (ArraySelect [0] (IData x)) => (IData x)
881
882 // string ops
883 // Decomposing StringMake and lowering of StringPtr and StringLen
884 // happens in a later pass, dec, so that these operations are available
885 // to other passes for optimizations.
886 (StringPtr (StringMake (Addr <t> {s} base) _)) => (Addr <t> {s} base)
887 (StringLen (StringMake _ (Const64 <t> [c]))) => (Const64 <t> [c])
888 (ConstString {str}) && config.PtrSize == 4 && str == "" =>
889 (StringMake (ConstNil) (Const32 <typ.Int> [0]))
890 (ConstString {str}) && config.PtrSize == 8 && str == "" =>
891 (StringMake (ConstNil) (Const64 <typ.Int> [0]))
892 (ConstString {str}) && config.PtrSize == 4 && str != "" =>
893 (StringMake
894 (Addr <typ.BytePtr> {fe.StringData(str)}
895 (SB))
896 (Const32 <typ.Int> [int32(len(str))]))
897 (ConstString {str}) && config.PtrSize == 8 && str != "" =>
898 (StringMake
899 (Addr <typ.BytePtr> {fe.StringData(str)}
900 (SB))
901 (Const64 <typ.Int> [int64(len(str))]))
902
903 // slice ops
904 // Only a few slice rules are provided here. See dec.rules for
905 // a more comprehensive set.
906 (SliceLen (SliceMake _ (Const64 <t> [c]) _)) => (Const64 <t> [c])
907 (SliceCap (SliceMake _ _ (Const64 <t> [c]))) => (Const64 <t> [c])
908 (SliceLen (SliceMake _ (Const32 <t> [c]) _)) => (Const32 <t> [c])
909 (SliceCap (SliceMake _ _ (Const32 <t> [c]))) => (Const32 <t> [c])
910 (SlicePtr (SliceMake (SlicePtr x) _ _)) => (SlicePtr x)
911 (SliceLen (SliceMake _ (SliceLen x) _)) => (SliceLen x)
912 (SliceCap (SliceMake _ _ (SliceCap x))) => (SliceCap x)
913 (SliceCap (SliceMake _ _ (SliceLen x))) => (SliceLen x)
914 (ConstSlice) && config.PtrSize == 4 =>
915 (SliceMake
916 (ConstNil <v.Type.Elem().PtrTo()>)
917 (Const32 <typ.Int> [0])
918 (Const32 <typ.Int> [0]))
919 (ConstSlice) && config.PtrSize == 8 =>
920 (SliceMake
921 (ConstNil <v.Type.Elem().PtrTo()>)
922 (Const64 <typ.Int> [0])
923 (Const64 <typ.Int> [0]))
924
925 // interface ops
926 (ConstInterface) =>
927 (IMake
928 (ConstNil <typ.Uintptr>)
929 (ConstNil <typ.BytePtr>))
930
931 (NilCheck (GetG mem) mem) => mem
932
933 (If (Not cond) yes no) => (If cond no yes)
934 (If (ConstBool [c]) yes no) && c => (First yes no)
935 (If (ConstBool [c]) yes no) && !c => (First no yes)
936
937 // Get rid of Convert ops for pointer arithmetic on unsafe.Pointer.
938 (Convert (Add(64|32) (Convert ptr mem) off) mem) => (AddPtr ptr off)
939 (Convert (Convert ptr mem) mem) => ptr
940
941 // strength reduction of divide by a constant.
942 // See ../magic.go for a detailed description of these algorithms.
943
944 // Unsigned divide by power of 2. Strength reduce to a shift.
945 (Div8u n (Const8 [c])) && isPowerOfTwo8(c) => (Rsh8Ux64 n (Const64 <typ.UInt64> [log8(c)]))
946 (Div16u n (Const16 [c])) && isPowerOfTwo16(c) => (Rsh16Ux64 n (Const64 <typ.UInt64> [log16(c)]))
947 (Div32u n (Const32 [c])) && isPowerOfTwo32(c) => (Rsh32Ux64 n (Const64 <typ.UInt64> [log32(c)]))
948 (Div64u n (Const64 [c])) && isPowerOfTwo64(c) => (Rsh64Ux64 n (Const64 <typ.UInt64> [log64(c)]))
949 (Div64u n (Const64 [-1<<63])) => (Rsh64Ux64 n (Const64 <typ.UInt64> [63]))
950
951 // Signed non-negative divide by power of 2.
952 (Div8 n (Const8 [c])) && isNonNegative(n) && isPowerOfTwo8(c) => (Rsh8Ux64 n (Const64 <typ.UInt64> [log8(c)]))
953 (Div16 n (Const16 [c])) && isNonNegative(n) && isPowerOfTwo16(c) => (Rsh16Ux64 n (Const64 <typ.UInt64> [log16(c)]))
954 (Div32 n (Const32 [c])) && isNonNegative(n) && isPowerOfTwo32(c) => (Rsh32Ux64 n (Const64 <typ.UInt64> [log32(c)]))
955 (Div64 n (Const64 [c])) && isNonNegative(n) && isPowerOfTwo64(c) => (Rsh64Ux64 n (Const64 <typ.UInt64> [log64(c)]))
956 (Div64 n (Const64 [-1<<63])) && isNonNegative(n) => (Const64 [0])
957
958 // Unsigned divide, not a power of 2. Strength reduce to a multiply.
959 // For 8-bit divides, we just do a direct 9-bit by 8-bit multiply.
960 (Div8u x (Const8 [c])) && umagicOK8(c) =>
961 (Trunc32to8
962 (Rsh32Ux64 <typ.UInt32>
963 (Mul32 <typ.UInt32>
964 (Const32 <typ.UInt32> [int32(1<<8+umagic8(c).m)])
965 (ZeroExt8to32 x))
966 (Const64 <typ.UInt64> [8+umagic8(c).s])))
967
968 // For 16-bit divides on 64-bit machines, we do a direct 17-bit by 16-bit multiply.
969 (Div16u x (Const16 [c])) && umagicOK16(c) && config.RegSize == 8 =>
970 (Trunc64to16
971 (Rsh64Ux64 <typ.UInt64>
972 (Mul64 <typ.UInt64>
973 (Const64 <typ.UInt64> [int64(1<<16+umagic16(c).m)])
974 (ZeroExt16to64 x))
975 (Const64 <typ.UInt64> [16+umagic16(c).s])))
976
977 // For 16-bit divides on 32-bit machines
978 (Div16u x (Const16 [c])) && umagicOK16(c) && config.RegSize == 4 && umagic16(c).m&1 == 0 =>
979 (Trunc32to16
980 (Rsh32Ux64 <typ.UInt32>
981 (Mul32 <typ.UInt32>
982 (Const32 <typ.UInt32> [int32(1<<15+umagic16(c).m/2)])
983 (ZeroExt16to32 x))
984 (Const64 <typ.UInt64> [16+umagic16(c).s-1])))
985 (Div16u x (Const16 [c])) && umagicOK16(c) && config.RegSize == 4 && c&1 == 0 =>
986 (Trunc32to16
987 (Rsh32Ux64 <typ.UInt32>
988 (Mul32 <typ.UInt32>
989 (Const32 <typ.UInt32> [int32(1<<15+(umagic16(c).m+1)/2)])
990 (Rsh32Ux64 <typ.UInt32> (ZeroExt16to32 x) (Const64 <typ.UInt64> [1])))
991 (Const64 <typ.UInt64> [16+umagic16(c).s-2])))
992 (Div16u x (Const16 [c])) && umagicOK16(c) && config.RegSize == 4 && config.useAvg =>
993 (Trunc32to16
994 (Rsh32Ux64 <typ.UInt32>
995 (Avg32u
996 (Lsh32x64 <typ.UInt32> (ZeroExt16to32 x) (Const64 <typ.UInt64> [16]))
997 (Mul32 <typ.UInt32>
998 (Const32 <typ.UInt32> [int32(umagic16(c).m)])
999 (ZeroExt16to32 x)))
1000 (Const64 <typ.UInt64> [16+umagic16(c).s-1])))
1001
1002 // For 32-bit divides on 32-bit machines
1003 (Div32u x (Const32 [c])) && umagicOK32(c) && config.RegSize == 4 && umagic32(c).m&1 == 0 && config.useHmul =>
1004 (Rsh32Ux64 <typ.UInt32>
1005 (Hmul32u <typ.UInt32>
1006 (Const32 <typ.UInt32> [int32(1<<31+umagic32(c).m/2)])
1007 x)
1008 (Const64 <typ.UInt64> [umagic32(c).s-1]))
1009 (Div32u x (Const32 [c])) && umagicOK32(c) && config.RegSize == 4 && c&1 == 0 && config.useHmul =>
1010 (Rsh32Ux64 <typ.UInt32>
1011 (Hmul32u <typ.UInt32>
1012 (Const32 <typ.UInt32> [int32(1<<31+(umagic32(c).m+1)/2)])
1013 (Rsh32Ux64 <typ.UInt32> x (Const64 <typ.UInt64> [1])))
1014 (Const64 <typ.UInt64> [umagic32(c).s-2]))
1015 (Div32u x (Const32 [c])) && umagicOK32(c) && config.RegSize == 4 && config.useAvg && config.useHmul =>
1016 (Rsh32Ux64 <typ.UInt32>
1017 (Avg32u
1018 x
1019 (Hmul32u <typ.UInt32>
1020 (Const32 <typ.UInt32> [int32(umagic32(c).m)])
1021 x))
1022 (Const64 <typ.UInt64> [umagic32(c).s-1]))
1023
1024 // For 32-bit divides on 64-bit machines
1025 // We'll use a regular (non-hi) multiply for this case.
1026 (Div32u x (Const32 [c])) && umagicOK32(c) && config.RegSize == 8 && umagic32(c).m&1 == 0 =>
1027 (Trunc64to32
1028 (Rsh64Ux64 <typ.UInt64>
1029 (Mul64 <typ.UInt64>
1030 (Const64 <typ.UInt64> [int64(1<<31+umagic32(c).m/2)])
1031 (ZeroExt32to64 x))
1032 (Const64 <typ.UInt64> [32+umagic32(c).s-1])))
1033 (Div32u x (Const32 [c])) && umagicOK32(c) && config.RegSize == 8 && c&1 == 0 =>
1034 (Trunc64to32
1035 (Rsh64Ux64 <typ.UInt64>
1036 (Mul64 <typ.UInt64>
1037 (Const64 <typ.UInt64> [int64(1<<31+(umagic32(c).m+1)/2)])
1038 (Rsh64Ux64 <typ.UInt64> (ZeroExt32to64 x) (Const64 <typ.UInt64> [1])))
1039 (Const64 <typ.UInt64> [32+umagic32(c).s-2])))
1040 (Div32u x (Const32 [c])) && umagicOK32(c) && config.RegSize == 8 && config.useAvg =>
1041 (Trunc64to32
1042 (Rsh64Ux64 <typ.UInt64>
1043 (Avg64u
1044 (Lsh64x64 <typ.UInt64> (ZeroExt32to64 x) (Const64 <typ.UInt64> [32]))
1045 (Mul64 <typ.UInt64>
1046 (Const64 <typ.UInt32> [int64(umagic32(c).m)])
1047 (ZeroExt32to64 x)))
1048 (Const64 <typ.UInt64> [32+umagic32(c).s-1])))
1049
1050 // For unsigned 64-bit divides on 32-bit machines,
1051 // if the constant fits in 16 bits (so that the last term
1052 // fits in 32 bits), convert to three 32-bit divides by a constant.
1053 //
1054 // If 1<<32 = Q * c + R
1055 // and x = hi << 32 + lo
1056 //
1057 // Then x = (hi/c*c + hi%c) << 32 + lo
1058 // = hi/c*c<<32 + hi%c<<32 + lo
1059 // = hi/c*c<<32 + (hi%c)*(Q*c+R) + lo/c*c + lo%c
1060 // = hi/c*c<<32 + (hi%c)*Q*c + lo/c*c + (hi%c*R+lo%c)
1061 // and x / c = (hi/c)<<32 + (hi%c)*Q + lo/c + (hi%c*R+lo%c)/c
1062 (Div64u x (Const64 [c])) && c > 0 && c <= 0xFFFF && umagicOK32(int32(c)) && config.RegSize == 4 && config.useHmul =>
1063 (Add64
1064 (Add64 <typ.UInt64>
1065 (Add64 <typ.UInt64>
1066 (Lsh64x64 <typ.UInt64>
1067 (ZeroExt32to64
1068 (Div32u <typ.UInt32>
1069 (Trunc64to32 <typ.UInt32> (Rsh64Ux64 <typ.UInt64> x (Const64 <typ.UInt64> [32])))
1070 (Const32 <typ.UInt32> [int32(c)])))
1071 (Const64 <typ.UInt64> [32]))
1072 (ZeroExt32to64 (Div32u <typ.UInt32> (Trunc64to32 <typ.UInt32> x) (Const32 <typ.UInt32> [int32(c)]))))
1073 (Mul64 <typ.UInt64>
1074 (ZeroExt32to64 <typ.UInt64>
1075 (Mod32u <typ.UInt32>
1076 (Trunc64to32 <typ.UInt32> (Rsh64Ux64 <typ.UInt64> x (Const64 <typ.UInt64> [32])))
1077 (Const32 <typ.UInt32> [int32(c)])))
1078 (Const64 <typ.UInt64> [int64((1<<32)/c)])))
1079 (ZeroExt32to64
1080 (Div32u <typ.UInt32>
1081 (Add32 <typ.UInt32>
1082 (Mod32u <typ.UInt32> (Trunc64to32 <typ.UInt32> x) (Const32 <typ.UInt32> [int32(c)]))
1083 (Mul32 <typ.UInt32>
1084 (Mod32u <typ.UInt32>
1085 (Trunc64to32 <typ.UInt32> (Rsh64Ux64 <typ.UInt64> x (Const64 <typ.UInt64> [32])))
1086 (Const32 <typ.UInt32> [int32(c)]))
1087 (Const32 <typ.UInt32> [int32((1<<32)%c)])))
1088 (Const32 <typ.UInt32> [int32(c)]))))
1089
1090 // For 64-bit divides on 64-bit machines
1091 // (64-bit divides on 32-bit machines are lowered to a runtime call by the walk pass.)
1092 (Div64u x (Const64 [c])) && umagicOK64(c) && config.RegSize == 8 && umagic64(c).m&1 == 0 && config.useHmul =>
1093 (Rsh64Ux64 <typ.UInt64>
1094 (Hmul64u <typ.UInt64>
1095 (Const64 <typ.UInt64> [int64(1<<63+umagic64(c).m/2)])
1096 x)
1097 (Const64 <typ.UInt64> [umagic64(c).s-1]))
1098 (Div64u x (Const64 [c])) && umagicOK64(c) && config.RegSize == 8 && c&1 == 0 && config.useHmul =>
1099 (Rsh64Ux64 <typ.UInt64>
1100 (Hmul64u <typ.UInt64>
1101 (Const64 <typ.UInt64> [int64(1<<63+(umagic64(c).m+1)/2)])
1102 (Rsh64Ux64 <typ.UInt64> x (Const64 <typ.UInt64> [1])))
1103 (Const64 <typ.UInt64> [umagic64(c).s-2]))
1104 (Div64u x (Const64 [c])) && umagicOK64(c) && config.RegSize == 8 && config.useAvg && config.useHmul =>
1105 (Rsh64Ux64 <typ.UInt64>
1106 (Avg64u
1107 x
1108 (Hmul64u <typ.UInt64>
1109 (Const64 <typ.UInt64> [int64(umagic64(c).m)])
1110 x))
1111 (Const64 <typ.UInt64> [umagic64(c).s-1]))
1112
1113 // Signed divide by a negative constant. Rewrite to divide by a positive constant.
1114 (Div8 <t> n (Const8 [c])) && c < 0 && c != -1<<7 => (Neg8 (Div8 <t> n (Const8 <t> [-c])))
1115 (Div16 <t> n (Const16 [c])) && c < 0 && c != -1<<15 => (Neg16 (Div16 <t> n (Const16 <t> [-c])))
1116 (Div32 <t> n (Const32 [c])) && c < 0 && c != -1<<31 => (Neg32 (Div32 <t> n (Const32 <t> [-c])))
1117 (Div64 <t> n (Const64 [c])) && c < 0 && c != -1<<63 => (Neg64 (Div64 <t> n (Const64 <t> [-c])))
1118
1119 // Dividing by the most-negative number. Result is always 0 except
1120 // if the input is also the most-negative number.
1121 // We can detect that using the sign bit of x & -x.
1122 (Div8 <t> x (Const8 [-1<<7 ])) => (Rsh8Ux64 (And8 <t> x (Neg8 <t> x)) (Const64 <typ.UInt64> [7 ]))
1123 (Div16 <t> x (Const16 [-1<<15])) => (Rsh16Ux64 (And16 <t> x (Neg16 <t> x)) (Const64 <typ.UInt64> [15]))
1124 (Div32 <t> x (Const32 [-1<<31])) => (Rsh32Ux64 (And32 <t> x (Neg32 <t> x)) (Const64 <typ.UInt64> [31]))
1125 (Div64 <t> x (Const64 [-1<<63])) => (Rsh64Ux64 (And64 <t> x (Neg64 <t> x)) (Const64 <typ.UInt64> [63]))
1126
1127 // Signed divide by power of 2.
1128 // n / c = n >> log(c) if n >= 0
1129 // = (n+c-1) >> log(c) if n < 0
1130 // We conditionally add c-1 by adding n>>63>>(64-log(c)) (first shift signed, second shift unsigned).
1131 (Div8 <t> n (Const8 [c])) && isPowerOfTwo8(c) =>
1132 (Rsh8x64
1133 (Add8 <t> n (Rsh8Ux64 <t> (Rsh8x64 <t> n (Const64 <typ.UInt64> [ 7])) (Const64 <typ.UInt64> [int64( 8-log8(c))])))
1134 (Const64 <typ.UInt64> [int64(log8(c))]))
1135 (Div16 <t> n (Const16 [c])) && isPowerOfTwo16(c) =>
1136 (Rsh16x64
1137 (Add16 <t> n (Rsh16Ux64 <t> (Rsh16x64 <t> n (Const64 <typ.UInt64> [15])) (Const64 <typ.UInt64> [int64(16-log16(c))])))
1138 (Const64 <typ.UInt64> [int64(log16(c))]))
1139 (Div32 <t> n (Const32 [c])) && isPowerOfTwo32(c) =>
1140 (Rsh32x64
1141 (Add32 <t> n (Rsh32Ux64 <t> (Rsh32x64 <t> n (Const64 <typ.UInt64> [31])) (Const64 <typ.UInt64> [int64(32-log32(c))])))
1142 (Const64 <typ.UInt64> [int64(log32(c))]))
1143 (Div64 <t> n (Const64 [c])) && isPowerOfTwo64(c) =>
1144 (Rsh64x64
1145 (Add64 <t> n (Rsh64Ux64 <t> (Rsh64x64 <t> n (Const64 <typ.UInt64> [63])) (Const64 <typ.UInt64> [int64(64-log64(c))])))
1146 (Const64 <typ.UInt64> [int64(log64(c))]))
1147
1148 // Signed divide, not a power of 2. Strength reduce to a multiply.
1149 (Div8 <t> x (Const8 [c])) && smagicOK8(c) =>
1150 (Sub8 <t>
1151 (Rsh32x64 <t>
1152 (Mul32 <typ.UInt32>
1153 (Const32 <typ.UInt32> [int32(smagic8(c).m)])
1154 (SignExt8to32 x))
1155 (Const64 <typ.UInt64> [8+smagic8(c).s]))
1156 (Rsh32x64 <t>
1157 (SignExt8to32 x)
1158 (Const64 <typ.UInt64> [31])))
1159 (Div16 <t> x (Const16 [c])) && smagicOK16(c) =>
1160 (Sub16 <t>
1161 (Rsh32x64 <t>
1162 (Mul32 <typ.UInt32>
1163 (Const32 <typ.UInt32> [int32(smagic16(c).m)])
1164 (SignExt16to32 x))
1165 (Const64 <typ.UInt64> [16+smagic16(c).s]))
1166 (Rsh32x64 <t>
1167 (SignExt16to32 x)
1168 (Const64 <typ.UInt64> [31])))
1169 (Div32 <t> x (Const32 [c])) && smagicOK32(c) && config.RegSize == 8 =>
1170 (Sub32 <t>
1171 (Rsh64x64 <t>
1172 (Mul64 <typ.UInt64>
1173 (Const64 <typ.UInt64> [int64(smagic32(c).m)])
1174 (SignExt32to64 x))
1175 (Const64 <typ.UInt64> [32+smagic32(c).s]))
1176 (Rsh64x64 <t>
1177 (SignExt32to64 x)
1178 (Const64 <typ.UInt64> [63])))
1179 (Div32 <t> x (Const32 [c])) && smagicOK32(c) && config.RegSize == 4 && smagic32(c).m&1 == 0 && config.useHmul =>
1180 (Sub32 <t>
1181 (Rsh32x64 <t>
1182 (Hmul32 <t>
1183 (Const32 <typ.UInt32> [int32(smagic32(c).m/2)])
1184 x)
1185 (Const64 <typ.UInt64> [smagic32(c).s-1]))
1186 (Rsh32x64 <t>
1187 x
1188 (Const64 <typ.UInt64> [31])))
1189 (Div32 <t> x (Const32 [c])) && smagicOK32(c) && config.RegSize == 4 && smagic32(c).m&1 != 0 && config.useHmul =>
1190 (Sub32 <t>
1191 (Rsh32x64 <t>
1192 (Add32 <t>
1193 (Hmul32 <t>
1194 (Const32 <typ.UInt32> [int32(smagic32(c).m)])
1195 x)
1196 x)
1197 (Const64 <typ.UInt64> [smagic32(c).s]))
1198 (Rsh32x64 <t>
1199 x
1200 (Const64 <typ.UInt64> [31])))
1201 (Div64 <t> x (Const64 [c])) && smagicOK64(c) && smagic64(c).m&1 == 0 && config.useHmul =>
1202 (Sub64 <t>
1203 (Rsh64x64 <t>
1204 (Hmul64 <t>
1205 (Const64 <typ.UInt64> [int64(smagic64(c).m/2)])
1206 x)
1207 (Const64 <typ.UInt64> [smagic64(c).s-1]))
1208 (Rsh64x64 <t>
1209 x
1210 (Const64 <typ.UInt64> [63])))
1211 (Div64 <t> x (Const64 [c])) && smagicOK64(c) && smagic64(c).m&1 != 0 && config.useHmul =>
1212 (Sub64 <t>
1213 (Rsh64x64 <t>
1214 (Add64 <t>
1215 (Hmul64 <t>
1216 (Const64 <typ.UInt64> [int64(smagic64(c).m)])
1217 x)
1218 x)
1219 (Const64 <typ.UInt64> [smagic64(c).s]))
1220 (Rsh64x64 <t>
1221 x
1222 (Const64 <typ.UInt64> [63])))
1223
1224 // Unsigned mod by power of 2 constant.
1225 (Mod8u <t> n (Const8 [c])) && isPowerOfTwo8(c) => (And8 n (Const8 <t> [c-1]))
1226 (Mod16u <t> n (Const16 [c])) && isPowerOfTwo16(c) => (And16 n (Const16 <t> [c-1]))
1227 (Mod32u <t> n (Const32 [c])) && isPowerOfTwo32(c) => (And32 n (Const32 <t> [c-1]))
1228 (Mod64u <t> n (Const64 [c])) && isPowerOfTwo64(c) => (And64 n (Const64 <t> [c-1]))
1229 (Mod64u <t> n (Const64 [-1<<63])) => (And64 n (Const64 <t> [1<<63-1]))
1230
1231 // Signed non-negative mod by power of 2 constant.
1232 (Mod8 <t> n (Const8 [c])) && isNonNegative(n) && isPowerOfTwo8(c) => (And8 n (Const8 <t> [c-1]))
1233 (Mod16 <t> n (Const16 [c])) && isNonNegative(n) && isPowerOfTwo16(c) => (And16 n (Const16 <t> [c-1]))
1234 (Mod32 <t> n (Const32 [c])) && isNonNegative(n) && isPowerOfTwo32(c) => (And32 n (Const32 <t> [c-1]))
1235 (Mod64 <t> n (Const64 [c])) && isNonNegative(n) && isPowerOfTwo64(c) => (And64 n (Const64 <t> [c-1]))
1236 (Mod64 n (Const64 [-1<<63])) && isNonNegative(n) => n
1237
1238 // Signed mod by negative constant.
1239 (Mod8 <t> n (Const8 [c])) && c < 0 && c != -1<<7 => (Mod8 <t> n (Const8 <t> [-c]))
1240 (Mod16 <t> n (Const16 [c])) && c < 0 && c != -1<<15 => (Mod16 <t> n (Const16 <t> [-c]))
1241 (Mod32 <t> n (Const32 [c])) && c < 0 && c != -1<<31 => (Mod32 <t> n (Const32 <t> [-c]))
1242 (Mod64 <t> n (Const64 [c])) && c < 0 && c != -1<<63 => (Mod64 <t> n (Const64 <t> [-c]))
1243
1244 // All other mods by constants, do A%B = A-(A/B*B).
1245 // This implements % with two * and a bunch of ancillary ops.
1246 // One of the * is free if the user's code also computes A/B.
1247 (Mod8 <t> x (Const8 [c])) && x.Op != OpConst8 && (c > 0 || c == -1<<7)
1248 => (Sub8 x (Mul8 <t> (Div8 <t> x (Const8 <t> [c])) (Const8 <t> [c])))
1249 (Mod16 <t> x (Const16 [c])) && x.Op != OpConst16 && (c > 0 || c == -1<<15)
1250 => (Sub16 x (Mul16 <t> (Div16 <t> x (Const16 <t> [c])) (Const16 <t> [c])))
1251 (Mod32 <t> x (Const32 [c])) && x.Op != OpConst32 && (c > 0 || c == -1<<31)
1252 => (Sub32 x (Mul32 <t> (Div32 <t> x (Const32 <t> [c])) (Const32 <t> [c])))
1253 (Mod64 <t> x (Const64 [c])) && x.Op != OpConst64 && (c > 0 || c == -1<<63)
1254 => (Sub64 x (Mul64 <t> (Div64 <t> x (Const64 <t> [c])) (Const64 <t> [c])))
1255 (Mod8u <t> x (Const8 [c])) && x.Op != OpConst8 && c > 0 && umagicOK8( c)
1256 => (Sub8 x (Mul8 <t> (Div8u <t> x (Const8 <t> [c])) (Const8 <t> [c])))
1257 (Mod16u <t> x (Const16 [c])) && x.Op != OpConst16 && c > 0 && umagicOK16(c)
1258 => (Sub16 x (Mul16 <t> (Div16u <t> x (Const16 <t> [c])) (Const16 <t> [c])))
1259 (Mod32u <t> x (Const32 [c])) && x.Op != OpConst32 && c > 0 && umagicOK32(c)
1260 => (Sub32 x (Mul32 <t> (Div32u <t> x (Const32 <t> [c])) (Const32 <t> [c])))
1261 (Mod64u <t> x (Const64 [c])) && x.Op != OpConst64 && c > 0 && umagicOK64(c)
1262 => (Sub64 x (Mul64 <t> (Div64u <t> x (Const64 <t> [c])) (Const64 <t> [c])))
1263
1264 // For architectures without rotates on less than 32-bits, promote these checks to 32-bit.
1265 (Eq8 (Mod8u x (Const8 [c])) (Const8 [0])) && x.Op != OpConst8 && udivisibleOK8(c) && !hasSmallRotate(config) =>
1266 (Eq32 (Mod32u <typ.UInt32> (ZeroExt8to32 <typ.UInt32> x) (Const32 <typ.UInt32> [int32(uint8(c))])) (Const32 <typ.UInt32> [0]))
1267 (Eq16 (Mod16u x (Const16 [c])) (Const16 [0])) && x.Op != OpConst16 && udivisibleOK16(c) && !hasSmallRotate(config) =>
1268 (Eq32 (Mod32u <typ.UInt32> (ZeroExt16to32 <typ.UInt32> x) (Const32 <typ.UInt32> [int32(uint16(c))])) (Const32 <typ.UInt32> [0]))
1269 (Eq8 (Mod8 x (Const8 [c])) (Const8 [0])) && x.Op != OpConst8 && sdivisibleOK8(c) && !hasSmallRotate(config) =>
1270 (Eq32 (Mod32 <typ.Int32> (SignExt8to32 <typ.Int32> x) (Const32 <typ.Int32> [int32(c)])) (Const32 <typ.Int32> [0]))
1271 (Eq16 (Mod16 x (Const16 [c])) (Const16 [0])) && x.Op != OpConst16 && sdivisibleOK16(c) && !hasSmallRotate(config) =>
1272 (Eq32 (Mod32 <typ.Int32> (SignExt16to32 <typ.Int32> x) (Const32 <typ.Int32> [int32(c)])) (Const32 <typ.Int32> [0]))
1273
1274 // Divisibility checks x%c == 0 convert to multiply and rotate.
1275 // Note, x%c == 0 is rewritten as x == c*(x/c) during the opt pass
1276 // where (x/c) is performed using multiplication with magic constants.
1277 // To rewrite x%c == 0 requires pattern matching the rewritten expression
1278 // and checking that the division by the same constant wasn't already calculated.
1279 // This check is made by counting uses of the magic constant multiplication.
1280 // Note that if there were an intermediate opt pass, this rule could be applied
1281 // directly on the Div op and magic division rewrites could be delayed to late opt.
1282
1283 // Unsigned divisibility checks convert to multiply and rotate.
1284 (Eq8 x (Mul8 (Const8 [c])
1285 (Trunc32to8
1286 (Rsh32Ux64
1287 mul:(Mul32
1288 (Const32 [m])
1289 (ZeroExt8to32 x))
1290 (Const64 [s])))
1291 )
1292 )
1293 && v.Block.Func.pass.name != "opt" && mul.Uses == 1
1294 && m == int32(1<<8+umagic8(c).m) && s == 8+umagic8(c).s
1295 && x.Op != OpConst8 && udivisibleOK8(c)
1296 => (Leq8U
1297 (RotateLeft8 <typ.UInt8>
1298 (Mul8 <typ.UInt8>
1299 (Const8 <typ.UInt8> [int8(udivisible8(c).m)])
1300 x)
1301 (Const8 <typ.UInt8> [int8(8-udivisible8(c).k)])
1302 )
1303 (Const8 <typ.UInt8> [int8(udivisible8(c).max)])
1304 )
1305
1306 (Eq16 x (Mul16 (Const16 [c])
1307 (Trunc64to16
1308 (Rsh64Ux64
1309 mul:(Mul64
1310 (Const64 [m])
1311 (ZeroExt16to64 x))
1312 (Const64 [s])))
1313 )
1314 )
1315 && v.Block.Func.pass.name != "opt" && mul.Uses == 1
1316 && m == int64(1<<16+umagic16(c).m) && s == 16+umagic16(c).s
1317 && x.Op != OpConst16 && udivisibleOK16(c)
1318 => (Leq16U
1319 (RotateLeft16 <typ.UInt16>
1320 (Mul16 <typ.UInt16>
1321 (Const16 <typ.UInt16> [int16(udivisible16(c).m)])
1322 x)
1323 (Const16 <typ.UInt16> [int16(16-udivisible16(c).k)])
1324 )
1325 (Const16 <typ.UInt16> [int16(udivisible16(c).max)])
1326 )
1327
1328 (Eq16 x (Mul16 (Const16 [c])
1329 (Trunc32to16
1330 (Rsh32Ux64
1331 mul:(Mul32
1332 (Const32 [m])
1333 (ZeroExt16to32 x))
1334 (Const64 [s])))
1335 )
1336 )
1337 && v.Block.Func.pass.name != "opt" && mul.Uses == 1
1338 && m == int32(1<<15+umagic16(c).m/2) && s == 16+umagic16(c).s-1
1339 && x.Op != OpConst16 && udivisibleOK16(c)
1340 => (Leq16U
1341 (RotateLeft16 <typ.UInt16>
1342 (Mul16 <typ.UInt16>
1343 (Const16 <typ.UInt16> [int16(udivisible16(c).m)])
1344 x)
1345 (Const16 <typ.UInt16> [int16(16-udivisible16(c).k)])
1346 )
1347 (Const16 <typ.UInt16> [int16(udivisible16(c).max)])
1348 )
1349
1350 (Eq16 x (Mul16 (Const16 [c])
1351 (Trunc32to16
1352 (Rsh32Ux64
1353 mul:(Mul32
1354 (Const32 [m])
1355 (Rsh32Ux64 (ZeroExt16to32 x) (Const64 [1])))
1356 (Const64 [s])))
1357 )
1358 )
1359 && v.Block.Func.pass.name != "opt" && mul.Uses == 1
1360 && m == int32(1<<15+(umagic16(c).m+1)/2) && s == 16+umagic16(c).s-2
1361 && x.Op != OpConst16 && udivisibleOK16(c)
1362 => (Leq16U
1363 (RotateLeft16 <typ.UInt16>
1364 (Mul16 <typ.UInt16>
1365 (Const16 <typ.UInt16> [int16(udivisible16(c).m)])
1366 x)
1367 (Const16 <typ.UInt16> [int16(16-udivisible16(c).k)])
1368 )
1369 (Const16 <typ.UInt16> [int16(udivisible16(c).max)])
1370 )
1371
1372 (Eq16 x (Mul16 (Const16 [c])
1373 (Trunc32to16
1374 (Rsh32Ux64
1375 (Avg32u
1376 (Lsh32x64 (ZeroExt16to32 x) (Const64 [16]))
1377 mul:(Mul32
1378 (Const32 [m])
1379 (ZeroExt16to32 x)))
1380 (Const64 [s])))
1381 )
1382 )
1383 && v.Block.Func.pass.name != "opt" && mul.Uses == 1
1384 && m == int32(umagic16(c).m) && s == 16+umagic16(c).s-1
1385 && x.Op != OpConst16 && udivisibleOK16(c)
1386 => (Leq16U
1387 (RotateLeft16 <typ.UInt16>
1388 (Mul16 <typ.UInt16>
1389 (Const16 <typ.UInt16> [int16(udivisible16(c).m)])
1390 x)
1391 (Const16 <typ.UInt16> [int16(16-udivisible16(c).k)])
1392 )
1393 (Const16 <typ.UInt16> [int16(udivisible16(c).max)])
1394 )
1395
1396 (Eq32 x (Mul32 (Const32 [c])
1397 (Rsh32Ux64
1398 mul:(Hmul32u
1399 (Const32 [m])
1400 x)
1401 (Const64 [s]))
1402 )
1403 )
1404 && v.Block.Func.pass.name != "opt" && mul.Uses == 1
1405 && m == int32(1<<31+umagic32(c).m/2) && s == umagic32(c).s-1
1406 && x.Op != OpConst32 && udivisibleOK32(c)
1407 => (Leq32U
1408 (RotateLeft32 <typ.UInt32>
1409 (Mul32 <typ.UInt32>
1410 (Const32 <typ.UInt32> [int32(udivisible32(c).m)])
1411 x)
1412 (Const32 <typ.UInt32> [int32(32-udivisible32(c).k)])
1413 )
1414 (Const32 <typ.UInt32> [int32(udivisible32(c).max)])
1415 )
1416
1417 (Eq32 x (Mul32 (Const32 [c])
1418 (Rsh32Ux64
1419 mul:(Hmul32u
1420 (Const32 <typ.UInt32> [m])
1421 (Rsh32Ux64 x (Const64 [1])))
1422 (Const64 [s]))
1423 )
1424 )
1425 && v.Block.Func.pass.name != "opt" && mul.Uses == 1
1426 && m == int32(1<<31+(umagic32(c).m+1)/2) && s == umagic32(c).s-2
1427 && x.Op != OpConst32 && udivisibleOK32(c)
1428 => (Leq32U
1429 (RotateLeft32 <typ.UInt32>
1430 (Mul32 <typ.UInt32>
1431 (Const32 <typ.UInt32> [int32(udivisible32(c).m)])
1432 x)
1433 (Const32 <typ.UInt32> [int32(32-udivisible32(c).k)])
1434 )
1435 (Const32 <typ.UInt32> [int32(udivisible32(c).max)])
1436 )
1437
1438 (Eq32 x (Mul32 (Const32 [c])
1439 (Rsh32Ux64
1440 (Avg32u
1441 x
1442 mul:(Hmul32u
1443 (Const32 [m])
1444 x))
1445 (Const64 [s]))
1446 )
1447 )
1448 && v.Block.Func.pass.name != "opt" && mul.Uses == 1
1449 && m == int32(umagic32(c).m) && s == umagic32(c).s-1
1450 && x.Op != OpConst32 && udivisibleOK32(c)
1451 => (Leq32U
1452 (RotateLeft32 <typ.UInt32>
1453 (Mul32 <typ.UInt32>
1454 (Const32 <typ.UInt32> [int32(udivisible32(c).m)])
1455 x)
1456 (Const32 <typ.UInt32> [int32(32-udivisible32(c).k)])
1457 )
1458 (Const32 <typ.UInt32> [int32(udivisible32(c).max)])
1459 )
1460
1461 (Eq32 x (Mul32 (Const32 [c])
1462 (Trunc64to32
1463 (Rsh64Ux64
1464 mul:(Mul64
1465 (Const64 [m])
1466 (ZeroExt32to64 x))
1467 (Const64 [s])))
1468 )
1469 )
1470 && v.Block.Func.pass.name != "opt" && mul.Uses == 1
1471 && m == int64(1<<31+umagic32(c).m/2) && s == 32+umagic32(c).s-1
1472 && x.Op != OpConst32 && udivisibleOK32(c)
1473 => (Leq32U
1474 (RotateLeft32 <typ.UInt32>
1475 (Mul32 <typ.UInt32>
1476 (Const32 <typ.UInt32> [int32(udivisible32(c).m)])
1477 x)
1478 (Const32 <typ.UInt32> [int32(32-udivisible32(c).k)])
1479 )
1480 (Const32 <typ.UInt32> [int32(udivisible32(c).max)])
1481 )
1482
1483 (Eq32 x (Mul32 (Const32 [c])
1484 (Trunc64to32
1485 (Rsh64Ux64
1486 mul:(Mul64
1487 (Const64 [m])
1488 (Rsh64Ux64 (ZeroExt32to64 x) (Const64 [1])))
1489 (Const64 [s])))
1490 )
1491 )
1492 && v.Block.Func.pass.name != "opt" && mul.Uses == 1
1493 && m == int64(1<<31+(umagic32(c).m+1)/2) && s == 32+umagic32(c).s-2
1494 && x.Op != OpConst32 && udivisibleOK32(c)
1495 => (Leq32U
1496 (RotateLeft32 <typ.UInt32>
1497 (Mul32 <typ.UInt32>
1498 (Const32 <typ.UInt32> [int32(udivisible32(c).m)])
1499 x)
1500 (Const32 <typ.UInt32> [int32(32-udivisible32(c).k)])
1501 )
1502 (Const32 <typ.UInt32> [int32(udivisible32(c).max)])
1503 )
1504
1505 (Eq32 x (Mul32 (Const32 [c])
1506 (Trunc64to32
1507 (Rsh64Ux64
1508 (Avg64u
1509 (Lsh64x64 (ZeroExt32to64 x) (Const64 [32]))
1510 mul:(Mul64
1511 (Const64 [m])
1512 (ZeroExt32to64 x)))
1513 (Const64 [s])))
1514 )
1515 )
1516 && v.Block.Func.pass.name != "opt" && mul.Uses == 1
1517 && m == int64(umagic32(c).m) && s == 32+umagic32(c).s-1
1518 && x.Op != OpConst32 && udivisibleOK32(c)
1519 => (Leq32U
1520 (RotateLeft32 <typ.UInt32>
1521 (Mul32 <typ.UInt32>
1522 (Const32 <typ.UInt32> [int32(udivisible32(c).m)])
1523 x)
1524 (Const32 <typ.UInt32> [int32(32-udivisible32(c).k)])
1525 )
1526 (Const32 <typ.UInt32> [int32(udivisible32(c).max)])
1527 )
1528
1529 (Eq64 x (Mul64 (Const64 [c])
1530 (Rsh64Ux64
1531 mul:(Hmul64u
1532 (Const64 [m])
1533 x)
1534 (Const64 [s]))
1535 )
1536 ) && v.Block.Func.pass.name != "opt" && mul.Uses == 1
1537 && m == int64(1<<63+umagic64(c).m/2) && s == umagic64(c).s-1
1538 && x.Op != OpConst64 && udivisibleOK64(c)
1539 => (Leq64U
1540 (RotateLeft64 <typ.UInt64>
1541 (Mul64 <typ.UInt64>
1542 (Const64 <typ.UInt64> [int64(udivisible64(c).m)])
1543 x)
1544 (Const64 <typ.UInt64> [64-udivisible64(c).k])
1545 )
1546 (Const64 <typ.UInt64> [int64(udivisible64(c).max)])
1547 )
1548 (Eq64 x (Mul64 (Const64 [c])
1549 (Rsh64Ux64
1550 mul:(Hmul64u
1551 (Const64 [m])
1552 (Rsh64Ux64 x (Const64 [1])))
1553 (Const64 [s]))
1554 )
1555 ) && v.Block.Func.pass.name != "opt" && mul.Uses == 1
1556 && m == int64(1<<63+(umagic64(c).m+1)/2) && s == umagic64(c).s-2
1557 && x.Op != OpConst64 && udivisibleOK64(c)
1558 => (Leq64U
1559 (RotateLeft64 <typ.UInt64>
1560 (Mul64 <typ.UInt64>
1561 (Const64 <typ.UInt64> [int64(udivisible64(c).m)])
1562 x)
1563 (Const64 <typ.UInt64> [64-udivisible64(c).k])
1564 )
1565 (Const64 <typ.UInt64> [int64(udivisible64(c).max)])
1566 )
1567 (Eq64 x (Mul64 (Const64 [c])
1568 (Rsh64Ux64
1569 (Avg64u
1570 x
1571 mul:(Hmul64u
1572 (Const64 [m])
1573 x))
1574 (Const64 [s]))
1575 )
1576 ) && v.Block.Func.pass.name != "opt" && mul.Uses == 1
1577 && m == int64(umagic64(c).m) && s == umagic64(c).s-1
1578 && x.Op != OpConst64 && udivisibleOK64(c)
1579 => (Leq64U
1580 (RotateLeft64 <typ.UInt64>
1581 (Mul64 <typ.UInt64>
1582 (Const64 <typ.UInt64> [int64(udivisible64(c).m)])
1583 x)
1584 (Const64 <typ.UInt64> [64-udivisible64(c).k])
1585 )
1586 (Const64 <typ.UInt64> [int64(udivisible64(c).max)])
1587 )
1588
1589 // Signed divisibility checks convert to multiply, add and rotate.
1590 (Eq8 x (Mul8 (Const8 [c])
1591 (Sub8
1592 (Rsh32x64
1593 mul:(Mul32
1594 (Const32 [m])
1595 (SignExt8to32 x))
1596 (Const64 [s]))
1597 (Rsh32x64
1598 (SignExt8to32 x)
1599 (Const64 [31])))
1600 )
1601 )
1602 && v.Block.Func.pass.name != "opt" && mul.Uses == 1
1603 && m == int32(smagic8(c).m) && s == 8+smagic8(c).s
1604 && x.Op != OpConst8 && sdivisibleOK8(c)
1605 => (Leq8U
1606 (RotateLeft8 <typ.UInt8>
1607 (Add8 <typ.UInt8>
1608 (Mul8 <typ.UInt8>
1609 (Const8 <typ.UInt8> [int8(sdivisible8(c).m)])
1610 x)
1611 (Const8 <typ.UInt8> [int8(sdivisible8(c).a)])
1612 )
1613 (Const8 <typ.UInt8> [int8(8-sdivisible8(c).k)])
1614 )
1615 (Const8 <typ.UInt8> [int8(sdivisible8(c).max)])
1616 )
1617
1618 (Eq16 x (Mul16 (Const16 [c])
1619 (Sub16
1620 (Rsh32x64
1621 mul:(Mul32
1622 (Const32 [m])
1623 (SignExt16to32 x))
1624 (Const64 [s]))
1625 (Rsh32x64
1626 (SignExt16to32 x)
1627 (Const64 [31])))
1628 )
1629 )
1630 && v.Block.Func.pass.name != "opt" && mul.Uses == 1
1631 && m == int32(smagic16(c).m) && s == 16+smagic16(c).s
1632 && x.Op != OpConst16 && sdivisibleOK16(c)
1633 => (Leq16U
1634 (RotateLeft16 <typ.UInt16>
1635 (Add16 <typ.UInt16>
1636 (Mul16 <typ.UInt16>
1637 (Const16 <typ.UInt16> [int16(sdivisible16(c).m)])
1638 x)
1639 (Const16 <typ.UInt16> [int16(sdivisible16(c).a)])
1640 )
1641 (Const16 <typ.UInt16> [int16(16-sdivisible16(c).k)])
1642 )
1643 (Const16 <typ.UInt16> [int16(sdivisible16(c).max)])
1644 )
1645
1646 (Eq32 x (Mul32 (Const32 [c])
1647 (Sub32
1648 (Rsh64x64
1649 mul:(Mul64
1650 (Const64 [m])
1651 (SignExt32to64 x))
1652 (Const64 [s]))
1653 (Rsh64x64
1654 (SignExt32to64 x)
1655 (Const64 [63])))
1656 )
1657 )
1658 && v.Block.Func.pass.name != "opt" && mul.Uses == 1
1659 && m == int64(smagic32(c).m) && s == 32+smagic32(c).s
1660 && x.Op != OpConst32 && sdivisibleOK32(c)
1661 => (Leq32U
1662 (RotateLeft32 <typ.UInt32>
1663 (Add32 <typ.UInt32>
1664 (Mul32 <typ.UInt32>
1665 (Const32 <typ.UInt32> [int32(sdivisible32(c).m)])
1666 x)
1667 (Const32 <typ.UInt32> [int32(sdivisible32(c).a)])
1668 )
1669 (Const32 <typ.UInt32> [int32(32-sdivisible32(c).k)])
1670 )
1671 (Const32 <typ.UInt32> [int32(sdivisible32(c).max)])
1672 )
1673
1674 (Eq32 x (Mul32 (Const32 [c])
1675 (Sub32
1676 (Rsh32x64
1677 mul:(Hmul32
1678 (Const32 [m])
1679 x)
1680 (Const64 [s]))
1681 (Rsh32x64
1682 x
1683 (Const64 [31])))
1684 )
1685 )
1686 && v.Block.Func.pass.name != "opt" && mul.Uses == 1
1687 && m == int32(smagic32(c).m/2) && s == smagic32(c).s-1
1688 && x.Op != OpConst32 && sdivisibleOK32(c)
1689 => (Leq32U
1690 (RotateLeft32 <typ.UInt32>
1691 (Add32 <typ.UInt32>
1692 (Mul32 <typ.UInt32>
1693 (Const32 <typ.UInt32> [int32(sdivisible32(c).m)])
1694 x)
1695 (Const32 <typ.UInt32> [int32(sdivisible32(c).a)])
1696 )
1697 (Const32 <typ.UInt32> [int32(32-sdivisible32(c).k)])
1698 )
1699 (Const32 <typ.UInt32> [int32(sdivisible32(c).max)])
1700 )
1701
1702 (Eq32 x (Mul32 (Const32 [c])
1703 (Sub32
1704 (Rsh32x64
1705 (Add32
1706 mul:(Hmul32
1707 (Const32 [m])
1708 x)
1709 x)
1710 (Const64 [s]))
1711 (Rsh32x64
1712 x
1713 (Const64 [31])))
1714 )
1715 )
1716 && v.Block.Func.pass.name != "opt" && mul.Uses == 1
1717 && m == int32(smagic32(c).m) && s == smagic32(c).s
1718 && x.Op != OpConst32 && sdivisibleOK32(c)
1719 => (Leq32U
1720 (RotateLeft32 <typ.UInt32>
1721 (Add32 <typ.UInt32>
1722 (Mul32 <typ.UInt32>
1723 (Const32 <typ.UInt32> [int32(sdivisible32(c).m)])
1724 x)
1725 (Const32 <typ.UInt32> [int32(sdivisible32(c).a)])
1726 )
1727 (Const32 <typ.UInt32> [int32(32-sdivisible32(c).k)])
1728 )
1729 (Const32 <typ.UInt32> [int32(sdivisible32(c).max)])
1730 )
1731
1732 (Eq64 x (Mul64 (Const64 [c])
1733 (Sub64
1734 (Rsh64x64
1735 mul:(Hmul64
1736 (Const64 [m])
1737 x)
1738 (Const64 [s]))
1739 (Rsh64x64
1740 x
1741 (Const64 [63])))
1742 )
1743 )
1744 && v.Block.Func.pass.name != "opt" && mul.Uses == 1
1745 && m == int64(smagic64(c).m/2) && s == smagic64(c).s-1
1746 && x.Op != OpConst64 && sdivisibleOK64(c)
1747 => (Leq64U
1748 (RotateLeft64 <typ.UInt64>
1749 (Add64 <typ.UInt64>
1750 (Mul64 <typ.UInt64>
1751 (Const64 <typ.UInt64> [int64(sdivisible64(c).m)])
1752 x)
1753 (Const64 <typ.UInt64> [int64(sdivisible64(c).a)])
1754 )
1755 (Const64 <typ.UInt64> [64-sdivisible64(c).k])
1756 )
1757 (Const64 <typ.UInt64> [int64(sdivisible64(c).max)])
1758 )
1759
1760 (Eq64 x (Mul64 (Const64 [c])
1761 (Sub64
1762 (Rsh64x64
1763 (Add64
1764 mul:(Hmul64
1765 (Const64 [m])
1766 x)
1767 x)
1768 (Const64 [s]))
1769 (Rsh64x64
1770 x
1771 (Const64 [63])))
1772 )
1773 )
1774 && v.Block.Func.pass.name != "opt" && mul.Uses == 1
1775 && m == int64(smagic64(c).m) && s == smagic64(c).s
1776 && x.Op != OpConst64 && sdivisibleOK64(c)
1777 => (Leq64U
1778 (RotateLeft64 <typ.UInt64>
1779 (Add64 <typ.UInt64>
1780 (Mul64 <typ.UInt64>
1781 (Const64 <typ.UInt64> [int64(sdivisible64(c).m)])
1782 x)
1783 (Const64 <typ.UInt64> [int64(sdivisible64(c).a)])
1784 )
1785 (Const64 <typ.UInt64> [64-sdivisible64(c).k])
1786 )
1787 (Const64 <typ.UInt64> [int64(sdivisible64(c).max)])
1788 )
1789
1790 // Divisibility check for signed integers for power of two constant are simple mask.
1791 // However, we must match against the rewritten n%c == 0 -> n - c*(n/c) == 0 -> n == c*(n/c)
1792 // where n/c contains fixup code to handle signed n.
1793 ((Eq8|Neq8) n (Lsh8x64
1794 (Rsh8x64
1795 (Add8 <t> n (Rsh8Ux64 <t> (Rsh8x64 <t> n (Const64 <typ.UInt64> [ 7])) (Const64 <typ.UInt64> [kbar])))
1796 (Const64 <typ.UInt64> [k]))
1797 (Const64 <typ.UInt64> [k]))
1798 ) && k > 0 && k < 7 && kbar == 8 - k
1799 => ((Eq8|Neq8) (And8 <t> n (Const8 <t> [1<<uint(k)-1])) (Const8 <t> [0]))
1800
1801 ((Eq16|Neq16) n (Lsh16x64
1802 (Rsh16x64
1803 (Add16 <t> n (Rsh16Ux64 <t> (Rsh16x64 <t> n (Const64 <typ.UInt64> [15])) (Const64 <typ.UInt64> [kbar])))
1804 (Const64 <typ.UInt64> [k]))
1805 (Const64 <typ.UInt64> [k]))
1806 ) && k > 0 && k < 15 && kbar == 16 - k
1807 => ((Eq16|Neq16) (And16 <t> n (Const16 <t> [1<<uint(k)-1])) (Const16 <t> [0]))
1808
1809 ((Eq32|Neq32) n (Lsh32x64
1810 (Rsh32x64
1811 (Add32 <t> n (Rsh32Ux64 <t> (Rsh32x64 <t> n (Const64 <typ.UInt64> [31])) (Const64 <typ.UInt64> [kbar])))
1812 (Const64 <typ.UInt64> [k]))
1813 (Const64 <typ.UInt64> [k]))
1814 ) && k > 0 && k < 31 && kbar == 32 - k
1815 => ((Eq32|Neq32) (And32 <t> n (Const32 <t> [1<<uint(k)-1])) (Const32 <t> [0]))
1816
1817 ((Eq64|Neq64) n (Lsh64x64
1818 (Rsh64x64
1819 (Add64 <t> n (Rsh64Ux64 <t> (Rsh64x64 <t> n (Const64 <typ.UInt64> [63])) (Const64 <typ.UInt64> [kbar])))
1820 (Const64 <typ.UInt64> [k]))
1821 (Const64 <typ.UInt64> [k]))
1822 ) && k > 0 && k < 63 && kbar == 64 - k
1823 => ((Eq64|Neq64) (And64 <t> n (Const64 <t> [1<<uint(k)-1])) (Const64 <t> [0]))
1824
1825 (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)
1826 (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)
1827
1828 // Optimize bitsets
1829 (Eq8 (And8 <t> x (Const8 <t> [y])) (Const8 <t> [y])) && oneBit8(y)
1830 => (Neq8 (And8 <t> x (Const8 <t> [y])) (Const8 <t> [0]))
1831 (Eq16 (And16 <t> x (Const16 <t> [y])) (Const16 <t> [y])) && oneBit16(y)
1832 => (Neq16 (And16 <t> x (Const16 <t> [y])) (Const16 <t> [0]))
1833 (Eq32 (And32 <t> x (Const32 <t> [y])) (Const32 <t> [y])) && oneBit32(y)
1834 => (Neq32 (And32 <t> x (Const32 <t> [y])) (Const32 <t> [0]))
1835 (Eq64 (And64 <t> x (Const64 <t> [y])) (Const64 <t> [y])) && oneBit64(y)
1836 => (Neq64 (And64 <t> x (Const64 <t> [y])) (Const64 <t> [0]))
1837 (Neq8 (And8 <t> x (Const8 <t> [y])) (Const8 <t> [y])) && oneBit8(y)
1838 => (Eq8 (And8 <t> x (Const8 <t> [y])) (Const8 <t> [0]))
1839 (Neq16 (And16 <t> x (Const16 <t> [y])) (Const16 <t> [y])) && oneBit16(y)
1840 => (Eq16 (And16 <t> x (Const16 <t> [y])) (Const16 <t> [0]))
1841 (Neq32 (And32 <t> x (Const32 <t> [y])) (Const32 <t> [y])) && oneBit32(y)
1842 => (Eq32 (And32 <t> x (Const32 <t> [y])) (Const32 <t> [0]))
1843 (Neq64 (And64 <t> x (Const64 <t> [y])) (Const64 <t> [y])) && oneBit64(y)
1844 => (Eq64 (And64 <t> x (Const64 <t> [y])) (Const64 <t> [0]))
1845
1846 // Reassociate expressions involving
1847 // constants such that constants come first,
1848 // exposing obvious constant-folding opportunities.
1849 // Reassociate (op (op y C) x) to (op C (op x y)) or similar, where C
1850 // is constant, which pushes constants to the outside
1851 // of the expression. At that point, any constant-folding
1852 // opportunities should be obvious.
1853 // Note: don't include AddPtr here! In order to maintain the
1854 // invariant that pointers must stay within the pointed-to object,
1855 // we can't pull part of a pointer computation above the AddPtr.
1856 // See issue 37881.
1857 // Note: we don't need to handle any (x-C) cases because we already rewrite
1858 // (x-C) to (x+(-C)).
1859
1860 // x + (C + z) -> C + (x + z)
1861 (Add64 (Add64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) => (Add64 i (Add64 <t> z x))
1862 (Add32 (Add32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) => (Add32 i (Add32 <t> z x))
1863 (Add16 (Add16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) => (Add16 i (Add16 <t> z x))
1864 (Add8 (Add8 i:(Const8 <t>) z) x) && (z.Op != OpConst8 && x.Op != OpConst8) => (Add8 i (Add8 <t> z x))
1865
1866 // x + (C - z) -> C + (x - z)
1867 (Add64 (Sub64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) => (Add64 i (Sub64 <t> x z))
1868 (Add32 (Sub32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) => (Add32 i (Sub32 <t> x z))
1869 (Add16 (Sub16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) => (Add16 i (Sub16 <t> x z))
1870 (Add8 (Sub8 i:(Const8 <t>) z) x) && (z.Op != OpConst8 && x.Op != OpConst8) => (Add8 i (Sub8 <t> x z))
1871
1872 // x - (C - z) -> x + (z - C) -> (x + z) - C
1873 (Sub64 x (Sub64 i:(Const64 <t>) z)) && (z.Op != OpConst64 && x.Op != OpConst64) => (Sub64 (Add64 <t> x z) i)
1874 (Sub32 x (Sub32 i:(Const32 <t>) z)) && (z.Op != OpConst32 && x.Op != OpConst32) => (Sub32 (Add32 <t> x z) i)
1875 (Sub16 x (Sub16 i:(Const16 <t>) z)) && (z.Op != OpConst16 && x.Op != OpConst16) => (Sub16 (Add16 <t> x z) i)
1876 (Sub8 x (Sub8 i:(Const8 <t>) z)) && (z.Op != OpConst8 && x.Op != OpConst8) => (Sub8 (Add8 <t> x z) i)
1877
1878 // x - (z + C) -> x + (-z - C) -> (x - z) - C
1879 (Sub64 x (Add64 z i:(Const64 <t>))) && (z.Op != OpConst64 && x.Op != OpConst64) => (Sub64 (Sub64 <t> x z) i)
1880 (Sub32 x (Add32 z i:(Const32 <t>))) && (z.Op != OpConst32 && x.Op != OpConst32) => (Sub32 (Sub32 <t> x z) i)
1881 (Sub16 x (Add16 z i:(Const16 <t>))) && (z.Op != OpConst16 && x.Op != OpConst16) => (Sub16 (Sub16 <t> x z) i)
1882 (Sub8 x (Add8 z i:(Const8 <t>))) && (z.Op != OpConst8 && x.Op != OpConst8) => (Sub8 (Sub8 <t> x z) i)
1883
1884 // (C - z) - x -> C - (z + x)
1885 (Sub64 (Sub64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) => (Sub64 i (Add64 <t> z x))
1886 (Sub32 (Sub32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) => (Sub32 i (Add32 <t> z x))
1887 (Sub16 (Sub16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) => (Sub16 i (Add16 <t> z x))
1888 (Sub8 (Sub8 i:(Const8 <t>) z) x) && (z.Op != OpConst8 && x.Op != OpConst8) => (Sub8 i (Add8 <t> z x))
1889
1890 // (z + C) -x -> C + (z - x)
1891 (Sub64 (Add64 z i:(Const64 <t>)) x) && (z.Op != OpConst64 && x.Op != OpConst64) => (Add64 i (Sub64 <t> z x))
1892 (Sub32 (Add32 z i:(Const32 <t>)) x) && (z.Op != OpConst32 && x.Op != OpConst32) => (Add32 i (Sub32 <t> z x))
1893 (Sub16 (Add16 z i:(Const16 <t>)) x) && (z.Op != OpConst16 && x.Op != OpConst16) => (Add16 i (Sub16 <t> z x))
1894 (Sub8 (Add8 z i:(Const8 <t>)) x) && (z.Op != OpConst8 && x.Op != OpConst8) => (Add8 i (Sub8 <t> z x))
1895
1896 // x & (C & z) -> C & (x & z)
1897 (And64 (And64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) => (And64 i (And64 <t> z x))
1898 (And32 (And32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) => (And32 i (And32 <t> z x))
1899 (And16 (And16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) => (And16 i (And16 <t> z x))
1900 (And8 (And8 i:(Const8 <t>) z) x) && (z.Op != OpConst8 && x.Op != OpConst8) => (And8 i (And8 <t> z x))
1901
1902 // x | (C | z) -> C | (x | z)
1903 (Or64 (Or64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) => (Or64 i (Or64 <t> z x))
1904 (Or32 (Or32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) => (Or32 i (Or32 <t> z x))
1905 (Or16 (Or16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) => (Or16 i (Or16 <t> z x))
1906 (Or8 (Or8 i:(Const8 <t>) z) x) && (z.Op != OpConst8 && x.Op != OpConst8) => (Or8 i (Or8 <t> z x))
1907
1908 // x ^ (C ^ z) -> C ^ (x ^ z)
1909 (Xor64 (Xor64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) => (Xor64 i (Xor64 <t> z x))
1910 (Xor32 (Xor32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) => (Xor32 i (Xor32 <t> z x))
1911 (Xor16 (Xor16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) => (Xor16 i (Xor16 <t> z x))
1912 (Xor8 (Xor8 i:(Const8 <t>) z) x) && (z.Op != OpConst8 && x.Op != OpConst8) => (Xor8 i (Xor8 <t> z x))
1913
1914 // x * (D * z) = D * (x * z)
1915 (Mul64 (Mul64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) => (Mul64 i (Mul64 <t> x z))
1916 (Mul32 (Mul32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) => (Mul32 i (Mul32 <t> x z))
1917 (Mul16 (Mul16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) => (Mul16 i (Mul16 <t> x z))
1918 (Mul8 (Mul8 i:(Const8 <t>) z) x) && (z.Op != OpConst8 && x.Op != OpConst8) => (Mul8 i (Mul8 <t> x z))
1919
1920 // C + (D + x) -> (C + D) + x
1921 (Add64 (Const64 <t> [c]) (Add64 (Const64 <t> [d]) x)) => (Add64 (Const64 <t> [c+d]) x)
1922 (Add32 (Const32 <t> [c]) (Add32 (Const32 <t> [d]) x)) => (Add32 (Const32 <t> [c+d]) x)
1923 (Add16 (Const16 <t> [c]) (Add16 (Const16 <t> [d]) x)) => (Add16 (Const16 <t> [c+d]) x)
1924 (Add8 (Const8 <t> [c]) (Add8 (Const8 <t> [d]) x)) => (Add8 (Const8 <t> [c+d]) x)
1925
1926 // C + (D - x) -> (C + D) - x
1927 (Add64 (Const64 <t> [c]) (Sub64 (Const64 <t> [d]) x)) => (Sub64 (Const64 <t> [c+d]) x)
1928 (Add32 (Const32 <t> [c]) (Sub32 (Const32 <t> [d]) x)) => (Sub32 (Const32 <t> [c+d]) x)
1929 (Add16 (Const16 <t> [c]) (Sub16 (Const16 <t> [d]) x)) => (Sub16 (Const16 <t> [c+d]) x)
1930 (Add8 (Const8 <t> [c]) (Sub8 (Const8 <t> [d]) x)) => (Sub8 (Const8 <t> [c+d]) x)
1931
1932 // C - (D - x) -> (C - D) + x
1933 (Sub64 (Const64 <t> [c]) (Sub64 (Const64 <t> [d]) x)) => (Add64 (Const64 <t> [c-d]) x)
1934 (Sub32 (Const32 <t> [c]) (Sub32 (Const32 <t> [d]) x)) => (Add32 (Const32 <t> [c-d]) x)
1935 (Sub16 (Const16 <t> [c]) (Sub16 (Const16 <t> [d]) x)) => (Add16 (Const16 <t> [c-d]) x)
1936 (Sub8 (Const8 <t> [c]) (Sub8 (Const8 <t> [d]) x)) => (Add8 (Const8 <t> [c-d]) x)
1937
1938 // C - (D + x) -> (C - D) - x
1939 (Sub64 (Const64 <t> [c]) (Add64 (Const64 <t> [d]) x)) => (Sub64 (Const64 <t> [c-d]) x)
1940 (Sub32 (Const32 <t> [c]) (Add32 (Const32 <t> [d]) x)) => (Sub32 (Const32 <t> [c-d]) x)
1941 (Sub16 (Const16 <t> [c]) (Add16 (Const16 <t> [d]) x)) => (Sub16 (Const16 <t> [c-d]) x)
1942 (Sub8 (Const8 <t> [c]) (Add8 (Const8 <t> [d]) x)) => (Sub8 (Const8 <t> [c-d]) x)
1943
1944 // C & (D & x) -> (C & D) & x
1945 (And64 (Const64 <t> [c]) (And64 (Const64 <t> [d]) x)) => (And64 (Const64 <t> [c&d]) x)
1946 (And32 (Const32 <t> [c]) (And32 (Const32 <t> [d]) x)) => (And32 (Const32 <t> [c&d]) x)
1947 (And16 (Const16 <t> [c]) (And16 (Const16 <t> [d]) x)) => (And16 (Const16 <t> [c&d]) x)
1948 (And8 (Const8 <t> [c]) (And8 (Const8 <t> [d]) x)) => (And8 (Const8 <t> [c&d]) x)
1949
1950 // C | (D | x) -> (C | D) | x
1951 (Or64 (Const64 <t> [c]) (Or64 (Const64 <t> [d]) x)) => (Or64 (Const64 <t> [c|d]) x)
1952 (Or32 (Const32 <t> [c]) (Or32 (Const32 <t> [d]) x)) => (Or32 (Const32 <t> [c|d]) x)
1953 (Or16 (Const16 <t> [c]) (Or16 (Const16 <t> [d]) x)) => (Or16 (Const16 <t> [c|d]) x)
1954 (Or8 (Const8 <t> [c]) (Or8 (Const8 <t> [d]) x)) => (Or8 (Const8 <t> [c|d]) x)
1955
1956 // C ^ (D ^ x) -> (C ^ D) ^ x
1957 (Xor64 (Const64 <t> [c]) (Xor64 (Const64 <t> [d]) x)) => (Xor64 (Const64 <t> [c^d]) x)
1958 (Xor32 (Const32 <t> [c]) (Xor32 (Const32 <t> [d]) x)) => (Xor32 (Const32 <t> [c^d]) x)
1959 (Xor16 (Const16 <t> [c]) (Xor16 (Const16 <t> [d]) x)) => (Xor16 (Const16 <t> [c^d]) x)
1960 (Xor8 (Const8 <t> [c]) (Xor8 (Const8 <t> [d]) x)) => (Xor8 (Const8 <t> [c^d]) x)
1961
1962 // C * (D * x) = (C * D) * x
1963 (Mul64 (Const64 <t> [c]) (Mul64 (Const64 <t> [d]) x)) => (Mul64 (Const64 <t> [c*d]) x)
1964 (Mul32 (Const32 <t> [c]) (Mul32 (Const32 <t> [d]) x)) => (Mul32 (Const32 <t> [c*d]) x)
1965 (Mul16 (Const16 <t> [c]) (Mul16 (Const16 <t> [d]) x)) => (Mul16 (Const16 <t> [c*d]) x)
1966 (Mul8 (Const8 <t> [c]) (Mul8 (Const8 <t> [d]) x)) => (Mul8 (Const8 <t> [c*d]) x)
1967
1968 // floating point optimizations
1969 (Mul(32|64)F x (Const(32|64)F [1])) => x
1970 (Mul32F x (Const32F [-1])) => (Neg32F x)
1971 (Mul64F x (Const64F [-1])) => (Neg64F x)
1972 (Mul32F x (Const32F [2])) => (Add32F x x)
1973 (Mul64F x (Const64F [2])) => (Add64F x x)
1974
1975 (Div32F x (Const32F <t> [c])) && reciprocalExact32(c) => (Mul32F x (Const32F <t> [1/c]))
1976 (Div64F x (Const64F <t> [c])) && reciprocalExact64(c) => (Mul64F x (Const64F <t> [1/c]))
1977
1978 // rewrite single-precision sqrt expression "float32(math.Sqrt(float64(x)))"
1979 (Cvt64Fto32F sqrt0:(Sqrt (Cvt32Fto64F x))) && sqrt0.Uses==1 => (Sqrt32 x)
1980
1981 (Sqrt (Const64F [c])) && !math.IsNaN(math.Sqrt(c)) => (Const64F [math.Sqrt(c)])
1982
1983 // for rewriting results of some late-expanded rewrites (below)
1984 (SelectN [0] (MakeResult x ___)) => x
1985 (SelectN [1] (MakeResult x y ___)) => y
1986 (SelectN [2] (MakeResult x y z ___)) => z
1987
1988 // for late-expanded calls, recognize newobject and remove zeroing and nilchecks
1989 (Zero (SelectN [0] call:(StaticLECall _ _)) mem:(SelectN [1] call))
1990 && isSameCall(call.Aux, "runtime.newobject")
1991 => mem
1992
1993 (Store (SelectN [0] call:(StaticLECall _ _)) x mem:(SelectN [1] call))
1994 && isConstZero(x)
1995 && isSameCall(call.Aux, "runtime.newobject")
1996 => mem
1997
1998 (Store (OffPtr (SelectN [0] call:(StaticLECall _ _))) x mem:(SelectN [1] call))
1999 && isConstZero(x)
2000 && isSameCall(call.Aux, "runtime.newobject")
2001 => mem
2002
2003 (NilCheck (SelectN [0] call:(StaticLECall _ _)) _)
2004 && isSameCall(call.Aux, "runtime.newobject")
2005 && warnRule(fe.Debug_checknil(), v, "removed nil check")
2006 => (Invalid)
2007
2008 (NilCheck (OffPtr (SelectN [0] call:(StaticLECall _ _))) _)
2009 && isSameCall(call.Aux, "runtime.newobject")
2010 && warnRule(fe.Debug_checknil(), v, "removed nil check")
2011 => (Invalid)
2012
2013 // for late-expanded calls, recognize memequal applied to a single constant byte
2014 // Support is limited by 1, 2, 4, 8 byte sizes
2015 (StaticLECall {callAux} sptr (Addr {scon} (SB)) (Const64 [1]) mem)
2016 && isSameCall(callAux, "runtime.memequal")
2017 && symIsRO(scon)
2018 => (MakeResult (Eq8 (Load <typ.Int8> sptr mem) (Const8 <typ.Int8> [int8(read8(scon,0))])) mem)
2019
2020 (StaticLECall {callAux} sptr (Addr {scon} (SB)) (Const64 [2]) mem)
2021 && isSameCall(callAux, "runtime.memequal")
2022 && symIsRO(scon)
2023 && canLoadUnaligned(config)
2024 => (MakeResult (Eq16 (Load <typ.Int16> sptr mem) (Const16 <typ.Int16> [int16(read16(scon,0,config.ctxt.Arch.ByteOrder))])) mem)
2025
2026 (StaticLECall {callAux} sptr (Addr {scon} (SB)) (Const64 [4]) mem)
2027 && isSameCall(callAux, "runtime.memequal")
2028 && symIsRO(scon)
2029 && canLoadUnaligned(config)
2030 => (MakeResult (Eq32 (Load <typ.Int32> sptr mem) (Const32 <typ.Int32> [int32(read32(scon,0,config.ctxt.Arch.ByteOrder))])) mem)
2031
2032 (StaticLECall {callAux} sptr (Addr {scon} (SB)) (Const64 [8]) mem)
2033 && isSameCall(callAux, "runtime.memequal")
2034 && symIsRO(scon)
2035 && canLoadUnaligned(config) && config.PtrSize == 8
2036 => (MakeResult (Eq64 (Load <typ.Int64> sptr mem) (Const64 <typ.Int64> [int64(read64(scon,0,config.ctxt.Arch.ByteOrder))])) mem)
2037
2038 // Evaluate constant address comparisons.
2039 (EqPtr x x) => (ConstBool [true])
2040 (NeqPtr x x) => (ConstBool [false])
2041 (EqPtr (Addr {x} _) (Addr {y} _)) => (ConstBool [x == y])
2042 (EqPtr (Addr {x} _) (OffPtr [o] (Addr {y} _))) => (ConstBool [x == y && o == 0])
2043 (EqPtr (OffPtr [o1] (Addr {x} _)) (OffPtr [o2] (Addr {y} _))) => (ConstBool [x == y && o1 == o2])
2044 (NeqPtr (Addr {x} _) (Addr {y} _)) => (ConstBool [x != y])
2045 (NeqPtr (Addr {x} _) (OffPtr [o] (Addr {y} _))) => (ConstBool [x != y || o != 0])
2046 (NeqPtr (OffPtr [o1] (Addr {x} _)) (OffPtr [o2] (Addr {y} _))) => (ConstBool [x != y || o1 != o2])
2047 (EqPtr (LocalAddr {x} _ _) (LocalAddr {y} _ _)) => (ConstBool [x == y])
2048 (EqPtr (LocalAddr {x} _ _) (OffPtr [o] (LocalAddr {y} _ _))) => (ConstBool [x == y && o == 0])
2049 (EqPtr (OffPtr [o1] (LocalAddr {x} _ _)) (OffPtr [o2] (LocalAddr {y} _ _))) => (ConstBool [x == y && o1 == o2])
2050 (NeqPtr (LocalAddr {x} _ _) (LocalAddr {y} _ _)) => (ConstBool [x != y])
2051 (NeqPtr (LocalAddr {x} _ _) (OffPtr [o] (LocalAddr {y} _ _))) => (ConstBool [x != y || o != 0])
2052 (NeqPtr (OffPtr [o1] (LocalAddr {x} _ _)) (OffPtr [o2] (LocalAddr {y} _ _))) => (ConstBool [x != y || o1 != o2])
2053 (EqPtr (OffPtr [o1] p1) p2) && isSamePtr(p1, p2) => (ConstBool [o1 == 0])
2054 (NeqPtr (OffPtr [o1] p1) p2) && isSamePtr(p1, p2) => (ConstBool [o1 != 0])
2055 (EqPtr (OffPtr [o1] p1) (OffPtr [o2] p2)) && isSamePtr(p1, p2) => (ConstBool [o1 == o2])
2056 (NeqPtr (OffPtr [o1] p1) (OffPtr [o2] p2)) && isSamePtr(p1, p2) => (ConstBool [o1 != o2])
2057 (EqPtr (Const(32|64) [c]) (Const(32|64) [d])) => (ConstBool [c == d])
2058 (NeqPtr (Const(32|64) [c]) (Const(32|64) [d])) => (ConstBool [c != d])
2059
2060 (EqPtr (LocalAddr _ _) (Addr _)) => (ConstBool [false])
2061 (EqPtr (OffPtr (LocalAddr _ _)) (Addr _)) => (ConstBool [false])
2062 (EqPtr (LocalAddr _ _) (OffPtr (Addr _))) => (ConstBool [false])
2063 (EqPtr (OffPtr (LocalAddr _ _)) (OffPtr (Addr _))) => (ConstBool [false])
2064 (NeqPtr (LocalAddr _ _) (Addr _)) => (ConstBool [true])
2065 (NeqPtr (OffPtr (LocalAddr _ _)) (Addr _)) => (ConstBool [true])
2066 (NeqPtr (LocalAddr _ _) (OffPtr (Addr _))) => (ConstBool [true])
2067 (NeqPtr (OffPtr (LocalAddr _ _)) (OffPtr (Addr _))) => (ConstBool [true])
2068
2069 // Simplify address comparisons.
2070 (EqPtr (AddPtr p1 o1) p2) && isSamePtr(p1, p2) => (Not (IsNonNil o1))
2071 (NeqPtr (AddPtr p1 o1) p2) && isSamePtr(p1, p2) => (IsNonNil o1)
2072 (EqPtr (Const(32|64) [0]) p) => (Not (IsNonNil p))
2073 (NeqPtr (Const(32|64) [0]) p) => (IsNonNil p)
2074 (EqPtr (ConstNil) p) => (Not (IsNonNil p))
2075 (NeqPtr (ConstNil) p) => (IsNonNil p)
2076
2077 // Evaluate constant user nil checks.
2078 (IsNonNil (ConstNil)) => (ConstBool [false])
2079 (IsNonNil (Const(32|64) [c])) => (ConstBool [c != 0])
2080 (IsNonNil (Addr _)) => (ConstBool [true])
2081 (IsNonNil (LocalAddr _ _)) => (ConstBool [true])
2082
2083 // Inline small or disjoint runtime.memmove calls with constant length.
2084 // See the comment in op Move in genericOps.go for discussion of the type.
2085
2086 // Because expand calls runs after prove, constants useful to this pattern may not appear.
2087 // Both versions need to exist; the memory and register variants.
2088 //
2089 // Match post-expansion calls, memory version.
2090 (SelectN [0] call:(StaticCall {sym} s1:(Store _ (Const(64|32) [sz]) s2:(Store _ src s3:(Store {t} _ dst mem)))))
2091 && sz >= 0
2092 && isSameCall(sym, "runtime.memmove")
2093 && t.IsPtr() // avoids TUNSAFEPTR, see issue 30061
2094 && s1.Uses == 1 && s2.Uses == 1 && s3.Uses == 1
2095 && isInlinableMemmove(dst, src, int64(sz), config)
2096 && clobber(s1, s2, s3, call)
2097 => (Move {t.Elem()} [int64(sz)] dst src mem)
2098
2099 // Match post-expansion calls, register version.
2100 (SelectN [0] call:(StaticCall {sym} dst src (Const(64|32) [sz]) mem))
2101 && sz >= 0
2102 && call.Uses == 1 // this will exclude all calls with results
2103 && isSameCall(sym, "runtime.memmove")
2104 && dst.Type.IsPtr() // avoids TUNSAFEPTR, see issue 30061
2105 && isInlinableMemmove(dst, src, int64(sz), config)
2106 && clobber(call)
2107 => (Move {dst.Type.Elem()} [int64(sz)] dst src mem)
2108
2109 // Match pre-expansion calls.
2110 (SelectN [0] call:(StaticLECall {sym} dst src (Const(64|32) [sz]) mem))
2111 && sz >= 0
2112 && call.Uses == 1 // this will exclude all calls with results
2113 && isSameCall(sym, "runtime.memmove")
2114 && dst.Type.IsPtr() // avoids TUNSAFEPTR, see issue 30061
2115 && isInlinableMemmove(dst, src, int64(sz), config)
2116 && clobber(call)
2117 => (Move {dst.Type.Elem()} [int64(sz)] dst src mem)
2118
2119 // De-virtualize late-expanded interface calls into late-expanded static calls.
2120 // Note that (ITab (IMake)) doesn't get rewritten until after the first opt pass,
2121 // so this rule should trigger reliably.
2122 // devirtLECall removes the first argument, adds the devirtualized symbol to the AuxCall, and changes the opcode
2123 (InterLECall [argsize] {auxCall} (Load (OffPtr [off] (ITab (IMake (Addr {itab} (SB)) _))) _) ___) && devirtLESym(v, auxCall, itab, off) !=
2124 nil => devirtLECall(v, devirtLESym(v, auxCall, itab, off))
2125
2126 // Move and Zero optimizations.
2127 // Move source and destination may overlap.
2128
2129 // Convert Moves into Zeros when the source is known to be zeros.
2130 (Move {t} [n] dst1 src mem:(Zero {t} [n] dst2 _)) && isSamePtr(src, dst2)
2131 => (Zero {t} [n] dst1 mem)
2132 (Move {t} [n] dst1 src mem:(VarDef (Zero {t} [n] dst0 _))) && isSamePtr(src, dst0)
2133 => (Zero {t} [n] dst1 mem)
2134 (Move {t} [n] dst (Addr {sym} (SB)) mem) && symIsROZero(sym) => (Zero {t} [n] dst mem)
2135
2136 // Don't Store to variables that are about to be overwritten by Move/Zero.
2137 (Zero {t1} [n] p1 store:(Store {t2} (OffPtr [o2] p2) _ mem))
2138 && isSamePtr(p1, p2) && store.Uses == 1
2139 && n >= o2 + t2.Size()
2140 && clobber(store)
2141 => (Zero {t1} [n] p1 mem)
2142 (Move {t1} [n] dst1 src1 store:(Store {t2} op:(OffPtr [o2] dst2) _ mem))
2143 && isSamePtr(dst1, dst2) && store.Uses == 1
2144 && n >= o2 + t2.Size()
2145 && disjoint(src1, n, op, t2.Size())
2146 && clobber(store)
2147 => (Move {t1} [n] dst1 src1 mem)
2148
2149 // Don't Move to variables that are immediately completely overwritten.
2150 (Zero {t} [n] dst1 move:(Move {t} [n] dst2 _ mem))
2151 && move.Uses == 1
2152 && isSamePtr(dst1, dst2)
2153 && clobber(move)
2154 => (Zero {t} [n] dst1 mem)
2155 (Move {t} [n] dst1 src1 move:(Move {t} [n] dst2 _ mem))
2156 && move.Uses == 1
2157 && isSamePtr(dst1, dst2) && disjoint(src1, n, dst2, n)
2158 && clobber(move)
2159 => (Move {t} [n] dst1 src1 mem)
2160 (Zero {t} [n] dst1 vardef:(VarDef {x} move:(Move {t} [n] dst2 _ mem)))
2161 && move.Uses == 1 && vardef.Uses == 1
2162 && isSamePtr(dst1, dst2)
2163 && clobber(move, vardef)
2164 => (Zero {t} [n] dst1 (VarDef {x} mem))
2165 (Move {t} [n] dst1 src1 vardef:(VarDef {x} move:(Move {t} [n] dst2 _ mem)))
2166 && move.Uses == 1 && vardef.Uses == 1
2167 && isSamePtr(dst1, dst2) && disjoint(src1, n, dst2, n)
2168 && clobber(move, vardef)
2169 => (Move {t} [n] dst1 src1 (VarDef {x} mem))
2170 (Store {t1} op1:(OffPtr [o1] p1) d1
2171 m2:(Store {t2} op2:(OffPtr [0] p2) d2
2172 m3:(Move [n] p3 _ mem)))
2173 && m2.Uses == 1 && m3.Uses == 1
2174 && o1 == t2.Size()
2175 && n == t2.Size() + t1.Size()
2176 && isSamePtr(p1, p2) && isSamePtr(p2, p3)
2177 && clobber(m2, m3)
2178 => (Store {t1} op1 d1 (Store {t2} op2 d2 mem))
2179 (Store {t1} op1:(OffPtr [o1] p1) d1
2180 m2:(Store {t2} op2:(OffPtr [o2] p2) d2
2181 m3:(Store {t3} op3:(OffPtr [0] p3) d3
2182 m4:(Move [n] p4 _ mem))))
2183 && m2.Uses == 1 && m3.Uses == 1 && m4.Uses == 1
2184 && o2 == t3.Size()
2185 && o1-o2 == t2.Size()
2186 && n == t3.Size() + t2.Size() + t1.Size()
2187 && isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4)
2188 && clobber(m2, m3, m4)
2189 => (Store {t1} op1 d1 (Store {t2} op2 d2 (Store {t3} op3 d3 mem)))
2190 (Store {t1} op1:(OffPtr [o1] p1) d1
2191 m2:(Store {t2} op2:(OffPtr [o2] p2) d2
2192 m3:(Store {t3} op3:(OffPtr [o3] p3) d3
2193 m4:(Store {t4} op4:(OffPtr [0] p4) d4
2194 m5:(Move [n] p5 _ mem)))))
2195 && m2.Uses == 1 && m3.Uses == 1 && m4.Uses == 1 && m5.Uses == 1
2196 && o3 == t4.Size()
2197 && o2-o3 == t3.Size()
2198 && o1-o2 == t2.Size()
2199 && n == t4.Size() + t3.Size() + t2.Size() + t1.Size()
2200 && isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5)
2201 && clobber(m2, m3, m4, m5)
2202 => (Store {t1} op1 d1 (Store {t2} op2 d2 (Store {t3} op3 d3 (Store {t4} op4 d4 mem))))
2203
2204 // Don't Zero variables that are immediately completely overwritten
2205 // before being accessed.
2206 (Move {t} [n] dst1 src1 zero:(Zero {t} [n] dst2 mem))
2207 && zero.Uses == 1
2208 && isSamePtr(dst1, dst2) && disjoint(src1, n, dst2, n)
2209 && clobber(zero)
2210 => (Move {t} [n] dst1 src1 mem)
2211 (Move {t} [n] dst1 src1 vardef:(VarDef {x} zero:(Zero {t} [n] dst2 mem)))
2212 && zero.Uses == 1 && vardef.Uses == 1
2213 && isSamePtr(dst1, dst2) && disjoint(src1, n, dst2, n)
2214 && clobber(zero, vardef)
2215 => (Move {t} [n] dst1 src1 (VarDef {x} mem))
2216 (Store {t1} op1:(OffPtr [o1] p1) d1
2217 m2:(Store {t2} op2:(OffPtr [0] p2) d2
2218 m3:(Zero [n] p3 mem)))
2219 && m2.Uses == 1 && m3.Uses == 1
2220 && o1 == t2.Size()
2221 && n == t2.Size() + t1.Size()
2222 && isSamePtr(p1, p2) && isSamePtr(p2, p3)
2223 && clobber(m2, m3)
2224 => (Store {t1} op1 d1 (Store {t2} op2 d2 mem))
2225 (Store {t1} op1:(OffPtr [o1] p1) d1
2226 m2:(Store {t2} op2:(OffPtr [o2] p2) d2
2227 m3:(Store {t3} op3:(OffPtr [0] p3) d3
2228 m4:(Zero [n] p4 mem))))
2229 && m2.Uses == 1 && m3.Uses == 1 && m4.Uses == 1
2230 && o2 == t3.Size()
2231 && o1-o2 == t2.Size()
2232 && n == t3.Size() + t2.Size() + t1.Size()
2233 && isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4)
2234 && clobber(m2, m3, m4)
2235 => (Store {t1} op1 d1 (Store {t2} op2 d2 (Store {t3} op3 d3 mem)))
2236 (Store {t1} op1:(OffPtr [o1] p1) d1
2237 m2:(Store {t2} op2:(OffPtr [o2] p2) d2
2238 m3:(Store {t3} op3:(OffPtr [o3] p3) d3
2239 m4:(Store {t4} op4:(OffPtr [0] p4) d4
2240 m5:(Zero [n] p5 mem)))))
2241 && m2.Uses == 1 && m3.Uses == 1 && m4.Uses == 1 && m5.Uses == 1
2242 && o3 == t4.Size()
2243 && o2-o3 == t3.Size()
2244 && o1-o2 == t2.Size()
2245 && n == t4.Size() + t3.Size() + t2.Size() + t1.Size()
2246 && isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5)
2247 && clobber(m2, m3, m4, m5)
2248 => (Store {t1} op1 d1 (Store {t2} op2 d2 (Store {t3} op3 d3 (Store {t4} op4 d4 mem))))
2249
2250 // Don't Move from memory if the values are likely to already be
2251 // in registers.
2252 (Move {t1} [n] dst p1
2253 mem:(Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
2254 (Store {t3} op3:(OffPtr <tt3> [0] p3) d2 _)))
2255 && isSamePtr(p1, p2) && isSamePtr(p2, p3)
2256 && t2.Alignment() <= t1.Alignment()
2257 && t3.Alignment() <= t1.Alignment()
2258 && registerizable(b, t2)
2259 && registerizable(b, t3)
2260 && o2 == t3.Size()
2261 && n == t2.Size() + t3.Size()
2262 => (Store {t2} (OffPtr <tt2> [o2] dst) d1
2263 (Store {t3} (OffPtr <tt3> [0] dst) d2 mem))
2264 (Move {t1} [n] dst p1
2265 mem:(Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
2266 (Store {t3} op3:(OffPtr <tt3> [o3] p3) d2
2267 (Store {t4} op4:(OffPtr <tt4> [0] p4) d3 _))))
2268 && isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4)
2269 && t2.Alignment() <= t1.Alignment()
2270 && t3.Alignment() <= t1.Alignment()
2271 && t4.Alignment() <= t1.Alignment()
2272 && registerizable(b, t2)
2273 && registerizable(b, t3)
2274 && registerizable(b, t4)
2275 && o3 == t4.Size()
2276 && o2-o3 == t3.Size()
2277 && n == t2.Size() + t3.Size() + t4.Size()
2278 => (Store {t2} (OffPtr <tt2> [o2] dst) d1
2279 (Store {t3} (OffPtr <tt3> [o3] dst) d2
2280 (Store {t4} (OffPtr <tt4> [0] dst) d3 mem)))
2281 (Move {t1} [n] dst p1
2282 mem:(Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
2283 (Store {t3} op3:(OffPtr <tt3> [o3] p3) d2
2284 (Store {t4} op4:(OffPtr <tt4> [o4] p4) d3
2285 (Store {t5} op5:(OffPtr <tt5> [0] p5) d4 _)))))
2286 && isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5)
2287 && t2.Alignment() <= t1.Alignment()
2288 && t3.Alignment() <= t1.Alignment()
2289 && t4.Alignment() <= t1.Alignment()
2290 && t5.Alignment() <= t1.Alignment()
2291 && registerizable(b, t2)
2292 && registerizable(b, t3)
2293 && registerizable(b, t4)
2294 && registerizable(b, t5)
2295 && o4 == t5.Size()
2296 && o3-o4 == t4.Size()
2297 && o2-o3 == t3.Size()
2298 && n == t2.Size() + t3.Size() + t4.Size() + t5.Size()
2299 => (Store {t2} (OffPtr <tt2> [o2] dst) d1
2300 (Store {t3} (OffPtr <tt3> [o3] dst) d2
2301 (Store {t4} (OffPtr <tt4> [o4] dst) d3
2302 (Store {t5} (OffPtr <tt5> [0] dst) d4 mem))))
2303
2304 // Same thing but with VarDef in the middle.
2305 (Move {t1} [n] dst p1
2306 mem:(VarDef
2307 (Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
2308 (Store {t3} op3:(OffPtr <tt3> [0] p3) d2 _))))
2309 && isSamePtr(p1, p2) && isSamePtr(p2, p3)
2310 && t2.Alignment() <= t1.Alignment()
2311 && t3.Alignment() <= t1.Alignment()
2312 && registerizable(b, t2)
2313 && registerizable(b, t3)
2314 && o2 == t3.Size()
2315 && n == t2.Size() + t3.Size()
2316 => (Store {t2} (OffPtr <tt2> [o2] dst) d1
2317 (Store {t3} (OffPtr <tt3> [0] dst) d2 mem))
2318 (Move {t1} [n] dst p1
2319 mem:(VarDef
2320 (Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
2321 (Store {t3} op3:(OffPtr <tt3> [o3] p3) d2
2322 (Store {t4} op4:(OffPtr <tt4> [0] p4) d3 _)))))
2323 && isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4)
2324 && t2.Alignment() <= t1.Alignment()
2325 && t3.Alignment() <= t1.Alignment()
2326 && t4.Alignment() <= t1.Alignment()
2327 && registerizable(b, t2)
2328 && registerizable(b, t3)
2329 && registerizable(b, t4)
2330 && o3 == t4.Size()
2331 && o2-o3 == t3.Size()
2332 && n == t2.Size() + t3.Size() + t4.Size()
2333 => (Store {t2} (OffPtr <tt2> [o2] dst) d1
2334 (Store {t3} (OffPtr <tt3> [o3] dst) d2
2335 (Store {t4} (OffPtr <tt4> [0] dst) d3 mem)))
2336 (Move {t1} [n] dst p1
2337 mem:(VarDef
2338 (Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
2339 (Store {t3} op3:(OffPtr <tt3> [o3] p3) d2
2340 (Store {t4} op4:(OffPtr <tt4> [o4] p4) d3
2341 (Store {t5} op5:(OffPtr <tt5> [0] p5) d4 _))))))
2342 && isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5)
2343 && t2.Alignment() <= t1.Alignment()
2344 && t3.Alignment() <= t1.Alignment()
2345 && t4.Alignment() <= t1.Alignment()
2346 && t5.Alignment() <= t1.Alignment()
2347 && registerizable(b, t2)
2348 && registerizable(b, t3)
2349 && registerizable(b, t4)
2350 && registerizable(b, t5)
2351 && o4 == t5.Size()
2352 && o3-o4 == t4.Size()
2353 && o2-o3 == t3.Size()
2354 && n == t2.Size() + t3.Size() + t4.Size() + t5.Size()
2355 => (Store {t2} (OffPtr <tt2> [o2] dst) d1
2356 (Store {t3} (OffPtr <tt3> [o3] dst) d2
2357 (Store {t4} (OffPtr <tt4> [o4] dst) d3
2358 (Store {t5} (OffPtr <tt5> [0] dst) d4 mem))))
2359
2360 // Prefer to Zero and Store than to Move.
2361 (Move {t1} [n] dst p1
2362 mem:(Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
2363 (Zero {t3} [n] p3 _)))
2364 && isSamePtr(p1, p2) && isSamePtr(p2, p3)
2365 && t2.Alignment() <= t1.Alignment()
2366 && t3.Alignment() <= t1.Alignment()
2367 && registerizable(b, t2)
2368 && n >= o2 + t2.Size()
2369 => (Store {t2} (OffPtr <tt2> [o2] dst) d1
2370 (Zero {t1} [n] dst mem))
2371 (Move {t1} [n] dst p1
2372 mem:(Store {t2} (OffPtr <tt2> [o2] p2) d1
2373 (Store {t3} (OffPtr <tt3> [o3] p3) d2
2374 (Zero {t4} [n] p4 _))))
2375 && isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4)
2376 && t2.Alignment() <= t1.Alignment()
2377 && t3.Alignment() <= t1.Alignment()
2378 && t4.Alignment() <= t1.Alignment()
2379 && registerizable(b, t2)
2380 && registerizable(b, t3)
2381 && n >= o2 + t2.Size()
2382 && n >= o3 + t3.Size()
2383 => (Store {t2} (OffPtr <tt2> [o2] dst) d1
2384 (Store {t3} (OffPtr <tt3> [o3] dst) d2
2385 (Zero {t1} [n] dst mem)))
2386 (Move {t1} [n] dst p1
2387 mem:(Store {t2} (OffPtr <tt2> [o2] p2) d1
2388 (Store {t3} (OffPtr <tt3> [o3] p3) d2
2389 (Store {t4} (OffPtr <tt4> [o4] p4) d3
2390 (Zero {t5} [n] p5 _)))))
2391 && isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5)
2392 && t2.Alignment() <= t1.Alignment()
2393 && t3.Alignment() <= t1.Alignment()
2394 && t4.Alignment() <= t1.Alignment()
2395 && t5.Alignment() <= t1.Alignment()
2396 && registerizable(b, t2)
2397 && registerizable(b, t3)
2398 && registerizable(b, t4)
2399 && n >= o2 + t2.Size()
2400 && n >= o3 + t3.Size()
2401 && n >= o4 + t4.Size()
2402 => (Store {t2} (OffPtr <tt2> [o2] dst) d1
2403 (Store {t3} (OffPtr <tt3> [o3] dst) d2
2404 (Store {t4} (OffPtr <tt4> [o4] dst) d3
2405 (Zero {t1} [n] dst mem))))
2406 (Move {t1} [n] dst p1
2407 mem:(Store {t2} (OffPtr <tt2> [o2] p2) d1
2408 (Store {t3} (OffPtr <tt3> [o3] p3) d2
2409 (Store {t4} (OffPtr <tt4> [o4] p4) d3
2410 (Store {t5} (OffPtr <tt5> [o5] p5) d4
2411 (Zero {t6} [n] p6 _))))))
2412 && isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5) && isSamePtr(p5, p6)
2413 && t2.Alignment() <= t1.Alignment()
2414 && t3.Alignment() <= t1.Alignment()
2415 && t4.Alignment() <= t1.Alignment()
2416 && t5.Alignment() <= t1.Alignment()
2417 && t6.Alignment() <= t1.Alignment()
2418 && registerizable(b, t2)
2419 && registerizable(b, t3)
2420 && registerizable(b, t4)
2421 && registerizable(b, t5)
2422 && n >= o2 + t2.Size()
2423 && n >= o3 + t3.Size()
2424 && n >= o4 + t4.Size()
2425 && n >= o5 + t5.Size()
2426 => (Store {t2} (OffPtr <tt2> [o2] dst) d1
2427 (Store {t3} (OffPtr <tt3> [o3] dst) d2
2428 (Store {t4} (OffPtr <tt4> [o4] dst) d3
2429 (Store {t5} (OffPtr <tt5> [o5] dst) d4
2430 (Zero {t1} [n] dst mem)))))
2431 (Move {t1} [n] dst p1
2432 mem:(VarDef
2433 (Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
2434 (Zero {t3} [n] p3 _))))
2435 && isSamePtr(p1, p2) && isSamePtr(p2, p3)
2436 && t2.Alignment() <= t1.Alignment()
2437 && t3.Alignment() <= t1.Alignment()
2438 && registerizable(b, t2)
2439 && n >= o2 + t2.Size()
2440 => (Store {t2} (OffPtr <tt2> [o2] dst) d1
2441 (Zero {t1} [n] dst mem))
2442 (Move {t1} [n] dst p1
2443 mem:(VarDef
2444 (Store {t2} (OffPtr <tt2> [o2] p2) d1
2445 (Store {t3} (OffPtr <tt3> [o3] p3) d2
2446 (Zero {t4} [n] p4 _)))))
2447 && isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4)
2448 && t2.Alignment() <= t1.Alignment()
2449 && t3.Alignment() <= t1.Alignment()
2450 && t4.Alignment() <= t1.Alignment()
2451 && registerizable(b, t2)
2452 && registerizable(b, t3)
2453 && n >= o2 + t2.Size()
2454 && n >= o3 + t3.Size()
2455 => (Store {t2} (OffPtr <tt2> [o2] dst) d1
2456 (Store {t3} (OffPtr <tt3> [o3] dst) d2
2457 (Zero {t1} [n] dst mem)))
2458 (Move {t1} [n] dst p1
2459 mem:(VarDef
2460 (Store {t2} (OffPtr <tt2> [o2] p2) d1
2461 (Store {t3} (OffPtr <tt3> [o3] p3) d2
2462 (Store {t4} (OffPtr <tt4> [o4] p4) d3
2463 (Zero {t5} [n] p5 _))))))
2464 && isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5)
2465 && t2.Alignment() <= t1.Alignment()
2466 && t3.Alignment() <= t1.Alignment()
2467 && t4.Alignment() <= t1.Alignment()
2468 && t5.Alignment() <= t1.Alignment()
2469 && registerizable(b, t2)
2470 && registerizable(b, t3)
2471 && registerizable(b, t4)
2472 && n >= o2 + t2.Size()
2473 && n >= o3 + t3.Size()
2474 && n >= o4 + t4.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 (Zero {t1} [n] dst mem))))
2479 (Move {t1} [n] dst p1
2480 mem:(VarDef
2481 (Store {t2} (OffPtr <tt2> [o2] p2) d1
2482 (Store {t3} (OffPtr <tt3> [o3] p3) d2
2483 (Store {t4} (OffPtr <tt4> [o4] p4) d3
2484 (Store {t5} (OffPtr <tt5> [o5] p5) d4
2485 (Zero {t6} [n] p6 _)))))))
2486 && isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5) && isSamePtr(p5, p6)
2487 && t2.Alignment() <= t1.Alignment()
2488 && t3.Alignment() <= t1.Alignment()
2489 && t4.Alignment() <= t1.Alignment()
2490 && t5.Alignment() <= t1.Alignment()
2491 && t6.Alignment() <= t1.Alignment()
2492 && registerizable(b, t2)
2493 && registerizable(b, t3)
2494 && registerizable(b, t4)
2495 && registerizable(b, t5)
2496 && n >= o2 + t2.Size()
2497 && n >= o3 + t3.Size()
2498 && n >= o4 + t4.Size()
2499 && n >= o5 + t5.Size()
2500 => (Store {t2} (OffPtr <tt2> [o2] dst) d1
2501 (Store {t3} (OffPtr <tt3> [o3] dst) d2
2502 (Store {t4} (OffPtr <tt4> [o4] dst) d3
2503 (Store {t5} (OffPtr <tt5> [o5] dst) d4
2504 (Zero {t1} [n] dst mem)))))
2505
2506 (SelectN [0] call:(StaticLECall {sym} a x)) && needRaceCleanup(sym, call) && clobber(call) => x
2507 (SelectN [0] call:(StaticLECall {sym} x)) && needRaceCleanup(sym, call) && clobber(call) => x
2508
2509 // Collapse moving A -> B -> C into just A -> C.
2510 // Later passes (deadstore, elim unread auto) will remove the A -> B move, if possible.
2511 // This happens most commonly when B is an autotmp inserted earlier
2512 // during compilation to ensure correctness.
2513 // Take care that overlapping moves are preserved.
2514 // Restrict this optimization to the stack, to avoid duplicating loads from the heap;
2515 // see CL 145208 for discussion.
2516 (Move {t1} [s] dst tmp1 midmem:(Move {t2} [s] tmp2 src _))
2517 && t1.Compare(t2) == types.CMPeq
2518 && isSamePtr(tmp1, tmp2)
2519 && isStackPtr(src) && !isVolatile(src)
2520 && disjoint(src, s, tmp2, s)
2521 && (disjoint(src, s, dst, s) || isInlinableMemmove(dst, src, s, config))
2522 => (Move {t1} [s] dst src midmem)
2523
2524 // Same, but for large types that require VarDefs.
2525 (Move {t1} [s] dst tmp1 midmem:(VarDef (Move {t2} [s] tmp2 src _)))
2526 && t1.Compare(t2) == types.CMPeq
2527 && isSamePtr(tmp1, tmp2)
2528 && isStackPtr(src) && !isVolatile(src)
2529 && disjoint(src, s, tmp2, s)
2530 && (disjoint(src, s, dst, s) || isInlinableMemmove(dst, src, s, config))
2531 => (Move {t1} [s] dst src midmem)
2532
2533 // Don't zero the same bits twice.
2534 (Zero {t} [s] dst1 zero:(Zero {t} [s] dst2 _)) && isSamePtr(dst1, dst2) => zero
2535 (Zero {t} [s] dst1 vardef:(VarDef (Zero {t} [s] dst2 _))) && isSamePtr(dst1, dst2) => vardef
2536
2537 // Elide self-moves. This only happens rarely (e.g test/fixedbugs/bug277.go).
2538 // However, this rule is needed to prevent the previous rule from looping forever in such cases.
2539 (Move dst src mem) && isSamePtr(dst, src) => mem
2540
View as plain text