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