]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmd/compile/internal/ssa/_gen/AMD64.rules
cmd/compile: don't use BTS when OR works, add direct memory BTS operations
[gostls13.git] / src / cmd / compile / internal / ssa / _gen / AMD64.rules
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 // Lowering arithmetic
6 (Add(64|32|16|8) ...) => (ADD(Q|L|L|L) ...)
7 (AddPtr ...) => (ADDQ ...)
8 (Add(32|64)F ...) => (ADDS(S|D) ...)
9
10 (Sub(64|32|16|8) ...) => (SUB(Q|L|L|L) ...)
11 (SubPtr ...) => (SUBQ ...)
12 (Sub(32|64)F ...) => (SUBS(S|D) ...)
13
14 (Mul(64|32|16|8) ...) => (MUL(Q|L|L|L) ...)
15 (Mul(32|64)F ...) => (MULS(S|D) ...)
16
17 (Select0 (Mul64uover x y)) => (Select0 <typ.UInt64> (MULQU x y))
18 (Select0 (Mul32uover x y)) => (Select0 <typ.UInt32> (MULLU x y))
19 (Select1 (Mul(64|32)uover x y)) => (SETO (Select1 <types.TypeFlags> (MUL(Q|L)U x y)))
20
21 (Hmul(64|32) ...) => (HMUL(Q|L) ...)
22 (Hmul(64|32)u ...) => (HMUL(Q|L)U ...)
23
24 (Div(64|32|16) [a] x y) => (Select0 (DIV(Q|L|W) [a] x y))
25 (Div8  x y) => (Select0 (DIVW  (SignExt8to16 x) (SignExt8to16 y)))
26 (Div(64|32|16)u x y) => (Select0 (DIV(Q|L|W)U x y))
27 (Div8u x y) => (Select0 (DIVWU (ZeroExt8to16 x) (ZeroExt8to16 y)))
28 (Div(32|64)F ...) => (DIVS(S|D) ...)
29
30 (Select0 (Add64carry x y c)) =>
31         (Select0 <typ.UInt64> (ADCQ x y (Select1 <types.TypeFlags> (NEGLflags c))))
32 (Select1 (Add64carry x y c)) =>
33         (NEGQ <typ.UInt64> (SBBQcarrymask <typ.UInt64> (Select1 <types.TypeFlags> (ADCQ x y (Select1 <types.TypeFlags> (NEGLflags c))))))
34 (Select0 (Sub64borrow x y c)) =>
35         (Select0 <typ.UInt64> (SBBQ x y (Select1 <types.TypeFlags> (NEGLflags c))))
36 (Select1 (Sub64borrow x y c)) =>
37         (NEGQ <typ.UInt64> (SBBQcarrymask <typ.UInt64> (Select1 <types.TypeFlags> (SBBQ x y (Select1 <types.TypeFlags> (NEGLflags c))))))
38
39 // Optimize ADCQ and friends
40 (ADCQ x (MOVQconst [c]) carry) && is32Bit(c) => (ADCQconst x [int32(c)] carry)
41 (ADCQ x y (FlagEQ)) => (ADDQcarry x y)
42 (ADCQconst x [c] (FlagEQ)) => (ADDQconstcarry x [c])
43 (ADDQcarry x (MOVQconst [c])) && is32Bit(c) => (ADDQconstcarry x [int32(c)])
44 (SBBQ x (MOVQconst [c]) borrow) && is32Bit(c) => (SBBQconst x [int32(c)] borrow)
45 (SBBQ x y (FlagEQ)) => (SUBQborrow x y)
46 (SBBQconst x [c] (FlagEQ)) => (SUBQconstborrow x [c])
47 (SUBQborrow x (MOVQconst [c])) && is32Bit(c) => (SUBQconstborrow x [int32(c)])
48 (Select1 (NEGLflags (MOVQconst [0]))) => (FlagEQ)
49 (Select1 (NEGLflags (NEGQ (SBBQcarrymask x)))) => x
50
51
52 (Mul64uhilo ...) => (MULQU2 ...)
53 (Div128u ...) => (DIVQU2 ...)
54
55 (Avg64u ...) => (AVGQU ...)
56
57 (Mod(64|32|16) [a] x y) => (Select1 (DIV(Q|L|W) [a] x y))
58 (Mod8  x y) => (Select1 (DIVW  (SignExt8to16 x) (SignExt8to16 y)))
59 (Mod(64|32|16)u x y) => (Select1 (DIV(Q|L|W)U x y))
60 (Mod8u x y) => (Select1 (DIVWU (ZeroExt8to16 x) (ZeroExt8to16 y)))
61
62 (And(64|32|16|8) ...) => (AND(Q|L|L|L) ...)
63 (Or(64|32|16|8) ...) => (OR(Q|L|L|L) ...)
64 (Xor(64|32|16|8) ...) => (XOR(Q|L|L|L) ...)
65 (Com(64|32|16|8) ...) => (NOT(Q|L|L|L) ...)
66
67 (Neg(64|32|16|8) ...) => (NEG(Q|L|L|L) ...)
68 (Neg32F x) => (PXOR x (MOVSSconst <typ.Float32> [float32(math.Copysign(0, -1))]))
69 (Neg64F x) => (PXOR x (MOVSDconst <typ.Float64> [math.Copysign(0, -1)]))
70
71 // Lowering boolean ops
72 (AndB ...) => (ANDL ...)
73 (OrB ...) => (ORL ...)
74 (Not x) => (XORLconst [1] x)
75
76 // Lowering pointer arithmetic
77 (OffPtr [off] ptr) && is32Bit(off) => (ADDQconst [int32(off)] ptr)
78 (OffPtr [off] ptr) => (ADDQ (MOVQconst [off]) ptr)
79
80 // Lowering other arithmetic
81 (Ctz64 x)     && buildcfg.GOAMD64 >= 3 => (TZCNTQ x)
82 (Ctz32 x)     && buildcfg.GOAMD64 >= 3 => (TZCNTL x)
83 (Ctz64 <t> x) && buildcfg.GOAMD64 <  3 => (CMOVQEQ (Select0 <t> (BSFQ x)) (MOVQconst <t> [64]) (Select1 <types.TypeFlags> (BSFQ x)))
84 (Ctz32 x)     && buildcfg.GOAMD64 <  3 => (Select0 (BSFQ (BTSQconst <typ.UInt64> [32] x)))
85 (Ctz16 x) => (BSFL (ORLconst <typ.UInt32> [1<<16] x))
86 (Ctz8  x) => (BSFL (ORLconst <typ.UInt32> [1<<8 ] x))
87
88 (Ctz64NonZero x) && buildcfg.GOAMD64 >= 3 => (TZCNTQ x)
89 (Ctz32NonZero x) && buildcfg.GOAMD64 >= 3 => (TZCNTL x)
90 (Ctz16NonZero x) && buildcfg.GOAMD64 >= 3 => (TZCNTL x)
91 (Ctz8NonZero  x) && buildcfg.GOAMD64 >= 3 => (TZCNTL x)
92 (Ctz64NonZero x) && buildcfg.GOAMD64 <  3 => (Select0 (BSFQ x))
93 (Ctz32NonZero x) && buildcfg.GOAMD64 <  3 => (BSFL x)
94 (Ctz16NonZero x) && buildcfg.GOAMD64 <  3 => (BSFL x)
95 (Ctz8NonZero  x) && buildcfg.GOAMD64 <  3 => (BSFL x)
96
97 // BitLen64 of a 64 bit value x requires checking whether x == 0, since BSRQ is undefined when x == 0.
98 // However, for zero-extended values, we can cheat a bit, and calculate
99 // BSR(x<<1 + 1), which is guaranteed to be non-zero, and which conveniently
100 // places the index of the highest set bit where we want it.
101 // For GOAMD64>=3, BitLen can be calculated by OperandSize - LZCNT(x).
102 (BitLen64 <t> x) && buildcfg.GOAMD64 < 3 => (ADDQconst [1] (CMOVQEQ <t> (Select0 <t> (BSRQ x)) (MOVQconst <t> [-1]) (Select1 <types.TypeFlags> (BSRQ x))))
103 (BitLen32 x) && buildcfg.GOAMD64 <  3 => (Select0 (BSRQ (LEAQ1 <typ.UInt64> [1] (MOVLQZX <typ.UInt64> x) (MOVLQZX <typ.UInt64> x))))
104 (BitLen16 x) && buildcfg.GOAMD64 <  3 => (BSRL (LEAL1 <typ.UInt32> [1] (MOVWQZX <typ.UInt32> x) (MOVWQZX <typ.UInt32> x)))
105 (BitLen8  x) && buildcfg.GOAMD64 <  3 => (BSRL (LEAL1 <typ.UInt32> [1] (MOVBQZX <typ.UInt32> x) (MOVBQZX <typ.UInt32> x)))
106 (BitLen64 <t> x)        && buildcfg.GOAMD64 >= 3 => (NEGQ (ADDQconst <t> [-64] (LZCNTQ x)))
107 // Use 64-bit version to allow const-fold remove unnecessary arithmetic.
108 (BitLen32 <t> x) && buildcfg.GOAMD64 >= 3 => (NEGQ (ADDQconst <t> [-32] (LZCNTL x)))
109 (BitLen16 <t> x) && buildcfg.GOAMD64 >= 3 => (NEGQ (ADDQconst <t> [-32] (LZCNTL (MOVWQZX <x.Type> x))))
110 (BitLen8 <t> x) && buildcfg.GOAMD64 >= 3 => (NEGQ (ADDQconst <t> [-32] (LZCNTL (MOVBQZX <x.Type> x))))
111
112 (Bswap(64|32) ...) => (BSWAP(Q|L) ...)
113 (Bswap16 x) => (ROLWconst [8] x)
114
115 (PopCount(64|32) ...) => (POPCNT(Q|L) ...)
116 (PopCount16 x) => (POPCNTL (MOVWQZX <typ.UInt32> x))
117 (PopCount8 x) => (POPCNTL (MOVBQZX <typ.UInt32> x))
118
119 (Sqrt ...) => (SQRTSD ...)
120 (Sqrt32 ...) => (SQRTSS ...)
121
122 (RoundToEven x) => (ROUNDSD [0] x)
123 (Floor x)       => (ROUNDSD [1] x)
124 (Ceil x)        => (ROUNDSD [2] x)
125 (Trunc x)       => (ROUNDSD [3] x)
126
127 (FMA x y z) => (VFMADD231SD z x y)
128
129 // Lowering extension
130 // Note: we always extend to 64 bits even though some ops don't need that many result bits.
131 (SignExt8to16  ...) => (MOVBQSX ...)
132 (SignExt8to32  ...) => (MOVBQSX ...)
133 (SignExt8to64  ...) => (MOVBQSX ...)
134 (SignExt16to32 ...) => (MOVWQSX ...)
135 (SignExt16to64 ...) => (MOVWQSX ...)
136 (SignExt32to64 ...) => (MOVLQSX ...)
137
138 (ZeroExt8to16  ...) => (MOVBQZX ...)
139 (ZeroExt8to32  ...) => (MOVBQZX ...)
140 (ZeroExt8to64  ...) => (MOVBQZX ...)
141 (ZeroExt16to32 ...) => (MOVWQZX ...)
142 (ZeroExt16to64 ...) => (MOVWQZX ...)
143 (ZeroExt32to64 ...) => (MOVLQZX ...)
144
145 (Slicemask <t> x) => (SARQconst (NEGQ <t> x) [63])
146
147 (SpectreIndex <t> x y) => (CMOVQCC x (MOVQconst [0]) (CMPQ x y))
148 (SpectreSliceIndex <t> x y) => (CMOVQHI x (MOVQconst [0]) (CMPQ x y))
149
150 // Lowering truncation
151 // Because we ignore high parts of registers, truncates are just copies.
152 (Trunc16to8  ...) => (Copy ...)
153 (Trunc32to8  ...) => (Copy ...)
154 (Trunc32to16 ...) => (Copy ...)
155 (Trunc64to8  ...) => (Copy ...)
156 (Trunc64to16 ...) => (Copy ...)
157 (Trunc64to32 ...) => (Copy ...)
158
159 // Lowering float <-> int
160 (Cvt32to32F ...) => (CVTSL2SS ...)
161 (Cvt32to64F ...) => (CVTSL2SD ...)
162 (Cvt64to32F ...) => (CVTSQ2SS ...)
163 (Cvt64to64F ...) => (CVTSQ2SD ...)
164
165 (Cvt32Fto32 ...) => (CVTTSS2SL ...)
166 (Cvt32Fto64 ...) => (CVTTSS2SQ ...)
167 (Cvt64Fto32 ...) => (CVTTSD2SL ...)
168 (Cvt64Fto64 ...) => (CVTTSD2SQ ...)
169
170 (Cvt32Fto64F ...) => (CVTSS2SD ...)
171 (Cvt64Fto32F ...) => (CVTSD2SS ...)
172
173 (Round(32|64)F ...) => (Copy ...)
174
175 // Floating-point min is tricky, as the hardware op isn't right for various special
176 // cases (-0 and NaN). We use two hardware ops organized just right to make the
177 // result come out how we want it. See https://github.com/golang/go/issues/59488#issuecomment-1553493207
178 // (although that comment isn't exactly right, as the value overwritten is not simulated correctly).
179 //    t1 = MINSD x, y   => incorrect if x==NaN or x==-0,y==+0
180 //    t2 = MINSD t1, x  => fixes x==NaN case
181 //   res = POR t1, t2   => fixes x==-0,y==+0 case
182 // Note that this trick depends on the special property that (NaN OR x) produces a NaN (although
183 // it might not produce the same NaN as the input).
184 (Min(64|32)F <t> x y) => (POR (MINS(D|S) <t> (MINS(D|S) <t> x y) x) (MINS(D|S) <t> x y))
185 // Floating-point max is even trickier. Punt to using min instead.
186 // max(x,y) == -min(-x,-y)
187 (Max(64|32)F <t> x y) => (Neg(64|32)F <t> (Min(64|32)F <t> (Neg(64|32)F <t> x) (Neg(64|32)F <t> y)))
188
189 (CvtBoolToUint8 ...) => (Copy ...)
190
191 // Lowering shifts
192 // Unsigned shifts need to return 0 if shift amount is >= width of shifted value.
193 //   result = (arg << shift) & (shift >= argbits ? 0 : 0xffffffffffffffff)
194 (Lsh64x(64|32|16|8) <t> x y) && !shiftIsBounded(v) => (ANDQ (SHLQ <t> x y) (SBBQcarrymask <t> (CMP(Q|L|W|B)const y [64])))
195 (Lsh32x(64|32|16|8) <t> x y) && !shiftIsBounded(v) => (ANDL (SHLL <t> x y) (SBBLcarrymask <t> (CMP(Q|L|W|B)const y [32])))
196 (Lsh16x(64|32|16|8) <t> x y) && !shiftIsBounded(v) => (ANDL (SHLL <t> x y) (SBBLcarrymask <t> (CMP(Q|L|W|B)const y [32])))
197 (Lsh8x(64|32|16|8)  <t> x y) && !shiftIsBounded(v) => (ANDL (SHLL <t> x y) (SBBLcarrymask <t> (CMP(Q|L|W|B)const y [32])))
198
199 (Lsh64x(64|32|16|8) x y) && shiftIsBounded(v) => (SHLQ x y)
200 (Lsh32x(64|32|16|8) x y) && shiftIsBounded(v) => (SHLL x y)
201 (Lsh16x(64|32|16|8) x y) && shiftIsBounded(v) => (SHLL x y)
202 (Lsh8x(64|32|16|8)  x y) && shiftIsBounded(v) => (SHLL x y)
203
204 (Rsh64Ux(64|32|16|8) <t> x y) && !shiftIsBounded(v) => (ANDQ (SHRQ <t> x y) (SBBQcarrymask <t> (CMP(Q|L|W|B)const y [64])))
205 (Rsh32Ux(64|32|16|8) <t> x y) && !shiftIsBounded(v) => (ANDL (SHRL <t> x y) (SBBLcarrymask <t> (CMP(Q|L|W|B)const y [32])))
206 (Rsh16Ux(64|32|16|8) <t> x y) && !shiftIsBounded(v) => (ANDL (SHRW <t> x y) (SBBLcarrymask <t> (CMP(Q|L|W|B)const y [16])))
207 (Rsh8Ux(64|32|16|8)  <t> x y) && !shiftIsBounded(v) => (ANDL (SHRB <t> x y) (SBBLcarrymask <t> (CMP(Q|L|W|B)const y [8])))
208
209 (Rsh64Ux(64|32|16|8) x y) && shiftIsBounded(v) => (SHRQ x y)
210 (Rsh32Ux(64|32|16|8) x y) && shiftIsBounded(v) => (SHRL x y)
211 (Rsh16Ux(64|32|16|8) x y) && shiftIsBounded(v) => (SHRW x y)
212 (Rsh8Ux(64|32|16|8)  x y) && shiftIsBounded(v) => (SHRB x y)
213
214 // Signed right shift needs to return 0/-1 if shift amount is >= width of shifted value.
215 // We implement this by setting the shift value to -1 (all ones) if the shift value is >= width.
216 (Rsh64x(64|32|16|8) <t> x y) && !shiftIsBounded(v) => (SARQ <t> x (OR(Q|L|L|L) <y.Type> y (NOT(Q|L|L|L) <y.Type> (SBB(Q|L|L|L)carrymask <y.Type> (CMP(Q|L|W|B)const y [64])))))
217 (Rsh32x(64|32|16|8) <t> x y) && !shiftIsBounded(v) => (SARL <t> x (OR(Q|L|L|L) <y.Type> y (NOT(Q|L|L|L) <y.Type> (SBB(Q|L|L|L)carrymask <y.Type> (CMP(Q|L|W|B)const y [32])))))
218 (Rsh16x(64|32|16|8) <t> x y) && !shiftIsBounded(v) => (SARW <t> x (OR(Q|L|L|L) <y.Type> y (NOT(Q|L|L|L) <y.Type> (SBB(Q|L|L|L)carrymask <y.Type> (CMP(Q|L|W|B)const y [16])))))
219 (Rsh8x(64|32|16|8)  <t> x y) && !shiftIsBounded(v) => (SARB <t> x (OR(Q|L|L|L) <y.Type> y (NOT(Q|L|L|L) <y.Type> (SBB(Q|L|L|L)carrymask <y.Type> (CMP(Q|L|W|B)const y [8])))))
220
221 (Rsh64x(64|32|16|8) x y) && shiftIsBounded(v) => (SARQ x y)
222 (Rsh32x(64|32|16|8) x y) && shiftIsBounded(v) => (SARL x y)
223 (Rsh16x(64|32|16|8) x y) && shiftIsBounded(v) => (SARW x y)
224 (Rsh8x(64|32|16|8) x y)  && shiftIsBounded(v) => (SARB x y)
225
226 // Lowering integer comparisons
227 (Less(64|32|16|8)      x y) => (SETL  (CMP(Q|L|W|B)     x y))
228 (Less(64|32|16|8)U     x y) => (SETB  (CMP(Q|L|W|B)     x y))
229 (Leq(64|32|16|8)       x y) => (SETLE (CMP(Q|L|W|B)     x y))
230 (Leq(64|32|16|8)U      x y) => (SETBE (CMP(Q|L|W|B)     x y))
231 (Eq(Ptr|64|32|16|8|B)  x y) => (SETEQ (CMP(Q|Q|L|W|B|B) x y))
232 (Neq(Ptr|64|32|16|8|B) x y) => (SETNE (CMP(Q|Q|L|W|B|B) x y))
233
234 // Lowering floating point comparisons
235 // Note Go assembler gets UCOMISx operand order wrong, but it is right here
236 // and the operands are reversed when generating assembly language.
237 (Eq(32|64)F   x y) => (SETEQF (UCOMIS(S|D) x y))
238 (Neq(32|64)F  x y) => (SETNEF (UCOMIS(S|D) x y))
239 // Use SETGF/SETGEF with reversed operands to dodge NaN case.
240 (Less(32|64)F x y) => (SETGF  (UCOMIS(S|D) y x))
241 (Leq(32|64)F  x y) => (SETGEF (UCOMIS(S|D) y x))
242
243 // Lowering loads
244 (Load <t> ptr mem) && (is64BitInt(t) || isPtr(t)) => (MOVQload ptr mem)
245 (Load <t> ptr mem) && is32BitInt(t) => (MOVLload ptr mem)
246 (Load <t> ptr mem) && is16BitInt(t) => (MOVWload ptr mem)
247 (Load <t> ptr mem) && (t.IsBoolean() || is8BitInt(t)) => (MOVBload ptr mem)
248 (Load <t> ptr mem) && is32BitFloat(t) => (MOVSSload ptr mem)
249 (Load <t> ptr mem) && is64BitFloat(t) => (MOVSDload ptr mem)
250
251 // Lowering stores
252 (Store {t} ptr val mem) && t.Size() == 8 &&  t.IsFloat() => (MOVSDstore ptr val mem)
253 (Store {t} ptr val mem) && t.Size() == 4 &&  t.IsFloat() => (MOVSSstore ptr val mem)
254 (Store {t} ptr val mem) && t.Size() == 8 && !t.IsFloat() => (MOVQstore ptr val mem)
255 (Store {t} ptr val mem) && t.Size() == 4 && !t.IsFloat() => (MOVLstore ptr val mem)
256 (Store {t} ptr val mem) && t.Size() == 2 => (MOVWstore ptr val mem)
257 (Store {t} ptr val mem) && t.Size() == 1 => (MOVBstore ptr val mem)
258
259 // Lowering moves
260 (Move [0] _ _ mem) => mem
261 (Move [1] dst src mem) => (MOVBstore dst (MOVBload src mem) mem)
262 (Move [2] dst src mem) => (MOVWstore dst (MOVWload src mem) mem)
263 (Move [4] dst src mem) => (MOVLstore dst (MOVLload src mem) mem)
264 (Move [8] dst src mem) => (MOVQstore dst (MOVQload src mem) mem)
265 (Move [16] dst src mem) && config.useSSE => (MOVOstore dst (MOVOload src mem) mem)
266 (Move [16] dst src mem) && !config.useSSE =>
267         (MOVQstore [8] dst (MOVQload [8] src mem)
268                 (MOVQstore dst (MOVQload src mem) mem))
269
270 (Move [32] dst src mem) =>
271         (Move [16]
272                 (OffPtr <dst.Type> dst [16])
273                 (OffPtr <src.Type> src [16])
274                 (Move [16] dst src mem))
275
276 (Move [48] dst src mem) && config.useSSE =>
277         (Move [32]
278                 (OffPtr <dst.Type> dst [16])
279                 (OffPtr <src.Type> src [16])
280                 (Move [16] dst src mem))
281
282 (Move [64] dst src mem) && config.useSSE =>
283         (Move [32]
284                 (OffPtr <dst.Type> dst [32])
285                 (OffPtr <src.Type> src [32])
286                 (Move [32] dst src mem))
287
288 (Move [3] dst src mem) =>
289         (MOVBstore [2] dst (MOVBload [2] src mem)
290                 (MOVWstore dst (MOVWload src mem) mem))
291 (Move [5] dst src mem) =>
292         (MOVBstore [4] dst (MOVBload [4] src mem)
293                 (MOVLstore dst (MOVLload src mem) mem))
294 (Move [6] dst src mem) =>
295         (MOVWstore [4] dst (MOVWload [4] src mem)
296                 (MOVLstore dst (MOVLload src mem) mem))
297 (Move [7] dst src mem) =>
298         (MOVLstore [3] dst (MOVLload [3] src mem)
299                 (MOVLstore dst (MOVLload src mem) mem))
300 (Move [9] dst src mem) =>
301         (MOVBstore [8] dst (MOVBload [8] src mem)
302                 (MOVQstore dst (MOVQload src mem) mem))
303 (Move [10] dst src mem) =>
304         (MOVWstore [8] dst (MOVWload [8] src mem)
305                 (MOVQstore dst (MOVQload src mem) mem))
306 (Move [12] dst src mem) =>
307         (MOVLstore [8] dst (MOVLload [8] src mem)
308                 (MOVQstore dst (MOVQload src mem) mem))
309 (Move [s] dst src mem) && s == 11 || s >= 13 && s <= 15 =>
310         (MOVQstore [int32(s-8)] dst (MOVQload [int32(s-8)] src mem)
311                 (MOVQstore dst (MOVQload src mem) mem))
312
313 // Adjust moves to be a multiple of 16 bytes.
314 (Move [s] dst src mem)
315         && s > 16 && s%16 != 0 && s%16 <= 8 =>
316         (Move [s-s%16]
317                 (OffPtr <dst.Type> dst [s%16])
318                 (OffPtr <src.Type> src [s%16])
319                 (MOVQstore dst (MOVQload src mem) mem))
320 (Move [s] dst src mem)
321         && s > 16 && s%16 != 0 && s%16 > 8 && config.useSSE =>
322         (Move [s-s%16]
323                 (OffPtr <dst.Type> dst [s%16])
324                 (OffPtr <src.Type> src [s%16])
325                 (MOVOstore dst (MOVOload src mem) mem))
326 (Move [s] dst src mem)
327         && s > 16 && s%16 != 0 && s%16 > 8 && !config.useSSE =>
328         (Move [s-s%16]
329                 (OffPtr <dst.Type> dst [s%16])
330                 (OffPtr <src.Type> src [s%16])
331                 (MOVQstore [8] dst (MOVQload [8] src mem)
332                         (MOVQstore dst (MOVQload src mem) mem)))
333
334 // Medium copying uses a duff device.
335 (Move [s] dst src mem)
336         && s > 64 && s <= 16*64 && s%16 == 0
337         && !config.noDuffDevice && logLargeCopy(v, s) =>
338         (DUFFCOPY [s] dst src mem)
339
340 // Large copying uses REP MOVSQ.
341 (Move [s] dst src mem) && (s > 16*64 || config.noDuffDevice) && s%8 == 0 && logLargeCopy(v, s) =>
342         (REPMOVSQ dst src (MOVQconst [s/8]) mem)
343
344 // Lowering Zero instructions
345 (Zero [0] _ mem) => mem
346 (Zero [1] destptr mem) => (MOVBstoreconst [makeValAndOff(0,0)] destptr mem)
347 (Zero [2] destptr mem) => (MOVWstoreconst [makeValAndOff(0,0)] destptr mem)
348 (Zero [4] destptr mem) => (MOVLstoreconst [makeValAndOff(0,0)] destptr mem)
349 (Zero [8] destptr mem) => (MOVQstoreconst [makeValAndOff(0,0)] destptr mem)
350
351 (Zero [3] destptr mem) =>
352         (MOVBstoreconst [makeValAndOff(0,2)] destptr
353                 (MOVWstoreconst [makeValAndOff(0,0)] destptr mem))
354 (Zero [5] destptr mem) =>
355         (MOVBstoreconst [makeValAndOff(0,4)] destptr
356                 (MOVLstoreconst [makeValAndOff(0,0)] destptr mem))
357 (Zero [6] destptr mem) =>
358         (MOVWstoreconst [makeValAndOff(0,4)] destptr
359                 (MOVLstoreconst [makeValAndOff(0,0)] destptr mem))
360 (Zero [7] destptr mem) =>
361         (MOVLstoreconst [makeValAndOff(0,3)] destptr
362                 (MOVLstoreconst [makeValAndOff(0,0)] destptr mem))
363
364 // Strip off any fractional word zeroing.
365 (Zero [s] destptr mem) && s%8 != 0 && s > 8 && !config.useSSE =>
366         (Zero [s-s%8] (OffPtr <destptr.Type> destptr [s%8])
367                 (MOVQstoreconst [makeValAndOff(0,0)] destptr mem))
368
369 // Zero small numbers of words directly.
370 (Zero [16] destptr mem) && !config.useSSE =>
371         (MOVQstoreconst [makeValAndOff(0,8)] destptr
372                 (MOVQstoreconst [makeValAndOff(0,0)] destptr mem))
373 (Zero [24] destptr mem) && !config.useSSE =>
374         (MOVQstoreconst [makeValAndOff(0,16)] destptr
375                 (MOVQstoreconst [makeValAndOff(0,8)] destptr
376                         (MOVQstoreconst [makeValAndOff(0,0)] destptr mem)))
377 (Zero [32] destptr mem) && !config.useSSE =>
378         (MOVQstoreconst [makeValAndOff(0,24)] destptr
379                 (MOVQstoreconst [makeValAndOff(0,16)] destptr
380                         (MOVQstoreconst [makeValAndOff(0,8)] destptr
381                                 (MOVQstoreconst [makeValAndOff(0,0)] destptr mem))))
382
383 (Zero [s] destptr mem) && s > 8 && s < 16 && config.useSSE =>
384         (MOVQstoreconst [makeValAndOff(0,int32(s-8))] destptr
385                 (MOVQstoreconst [makeValAndOff(0,0)] destptr mem))
386
387 // Adjust zeros to be a multiple of 16 bytes.
388 (Zero [s] destptr mem) && s%16 != 0 && s > 16 && s%16 > 8 && config.useSSE =>
389         (Zero [s-s%16] (OffPtr <destptr.Type> destptr [s%16])
390                 (MOVOstoreconst [makeValAndOff(0,0)] destptr mem))
391
392 (Zero [s] destptr mem) && s%16 != 0 && s > 16 && s%16 <= 8 && config.useSSE =>
393         (Zero [s-s%16] (OffPtr <destptr.Type> destptr [s%16])
394                 (MOVOstoreconst [makeValAndOff(0,0)] destptr mem))
395
396 (Zero [16] destptr mem) && config.useSSE =>
397         (MOVOstoreconst [makeValAndOff(0,0)] destptr mem)
398 (Zero [32] destptr mem) && config.useSSE =>
399         (MOVOstoreconst [makeValAndOff(0,16)] destptr
400                 (MOVOstoreconst [makeValAndOff(0,0)] destptr mem))
401 (Zero [48] destptr mem) && config.useSSE =>
402         (MOVOstoreconst [makeValAndOff(0,32)] destptr
403                 (MOVOstoreconst [makeValAndOff(0,16)] destptr
404                         (MOVOstoreconst [makeValAndOff(0,0)] destptr mem)))
405 (Zero [64] destptr mem) && config.useSSE =>
406         (MOVOstoreconst [makeValAndOff(0,48)] destptr
407                 (MOVOstoreconst [makeValAndOff(0,32)] destptr
408                         (MOVOstoreconst [makeValAndOff(0,16)] destptr
409                                 (MOVOstoreconst [makeValAndOff(0,0)] destptr mem))))
410
411 // Medium zeroing uses a duff device.
412 (Zero [s] destptr mem)
413         && s > 64 && s <= 1024 && s%16 == 0 && !config.noDuffDevice =>
414         (DUFFZERO [s] destptr mem)
415
416 // Large zeroing uses REP STOSQ.
417 (Zero [s] destptr mem)
418         && (s > 1024 || (config.noDuffDevice && s > 64 || !config.useSSE && s > 32))
419         && s%8 == 0 =>
420         (REPSTOSQ destptr (MOVQconst [s/8]) (MOVQconst [0]) mem)
421
422 // Lowering constants
423 (Const8   [c]) => (MOVLconst [int32(c)])
424 (Const16  [c]) => (MOVLconst [int32(c)])
425 (Const32  ...) => (MOVLconst ...)
426 (Const64  ...) => (MOVQconst ...)
427 (Const32F ...) => (MOVSSconst ...)
428 (Const64F ...) => (MOVSDconst ...)
429 (ConstNil    ) => (MOVQconst [0])
430 (ConstBool [c]) => (MOVLconst [b2i32(c)])
431
432 // Lowering calls
433 (StaticCall ...) => (CALLstatic ...)
434 (ClosureCall ...) => (CALLclosure ...)
435 (InterCall ...) => (CALLinter ...)
436 (TailCall ...) => (CALLtail ...)
437
438 // Lowering conditional moves
439 // If the condition is a SETxx, we can just run a CMOV from the comparison that was
440 // setting the flags.
441 // Legend: HI=unsigned ABOVE, CS=unsigned BELOW, CC=unsigned ABOVE EQUAL, LS=unsigned BELOW EQUAL
442 (CondSelect <t> x y (SET(EQ|NE|L|G|LE|GE|A|B|AE|BE|EQF|NEF|GF|GEF) cond)) && (is64BitInt(t) || isPtr(t))
443     => (CMOVQ(EQ|NE|LT|GT|LE|GE|HI|CS|CC|LS|EQF|NEF|GTF|GEF) y x cond)
444 (CondSelect <t> x y (SET(EQ|NE|L|G|LE|GE|A|B|AE|BE|EQF|NEF|GF|GEF) cond)) && is32BitInt(t)
445     => (CMOVL(EQ|NE|LT|GT|LE|GE|HI|CS|CC|LS|EQF|NEF|GTF|GEF) y x cond)
446 (CondSelect <t> x y (SET(EQ|NE|L|G|LE|GE|A|B|AE|BE|EQF|NEF|GF|GEF) cond)) && is16BitInt(t)
447     => (CMOVW(EQ|NE|LT|GT|LE|GE|HI|CS|CC|LS|EQF|NEF|GTF|GEF) y x cond)
448
449 // If the condition does not set the flags, we need to generate a comparison.
450 (CondSelect <t> x y check) && !check.Type.IsFlags() && check.Type.Size() == 1
451     => (CondSelect <t> x y (MOVBQZX <typ.UInt64> check))
452 (CondSelect <t> x y check) && !check.Type.IsFlags() && check.Type.Size() == 2
453     => (CondSelect <t> x y (MOVWQZX <typ.UInt64> check))
454 (CondSelect <t> x y check) && !check.Type.IsFlags() && check.Type.Size() == 4
455     => (CondSelect <t> x y (MOVLQZX <typ.UInt64> check))
456
457 (CondSelect <t> x y check) && !check.Type.IsFlags() && check.Type.Size() == 8 && (is64BitInt(t) || isPtr(t))
458     => (CMOVQNE y x (CMPQconst [0] check))
459 (CondSelect <t> x y check) && !check.Type.IsFlags() && check.Type.Size() == 8 && is32BitInt(t)
460     => (CMOVLNE y x (CMPQconst [0] check))
461 (CondSelect <t> x y check) && !check.Type.IsFlags() && check.Type.Size() == 8 && is16BitInt(t)
462     => (CMOVWNE y x (CMPQconst [0] check))
463
464 // Absorb InvertFlags
465 (CMOVQ(EQ|NE|LT|GT|LE|GE|HI|CS|CC|LS) x y (InvertFlags cond))
466     => (CMOVQ(EQ|NE|GT|LT|GE|LE|CS|HI|LS|CC) x y cond)
467 (CMOVL(EQ|NE|LT|GT|LE|GE|HI|CS|CC|LS) x y (InvertFlags cond))
468     => (CMOVL(EQ|NE|GT|LT|GE|LE|CS|HI|LS|CC) x y cond)
469 (CMOVW(EQ|NE|LT|GT|LE|GE|HI|CS|CC|LS) x y (InvertFlags cond))
470     => (CMOVW(EQ|NE|GT|LT|GE|LE|CS|HI|LS|CC) x y cond)
471
472 // Absorb constants generated during lower
473 (CMOV(QEQ|QLE|QGE|QCC|QLS|LEQ|LLE|LGE|LCC|LLS|WEQ|WLE|WGE|WCC|WLS) _ x (FlagEQ)) => x
474 (CMOV(QNE|QLT|QGT|QCS|QHI|LNE|LLT|LGT|LCS|LHI|WNE|WLT|WGT|WCS|WHI) y _ (FlagEQ)) => y
475 (CMOV(QNE|QGT|QGE|QHI|QCC|LNE|LGT|LGE|LHI|LCC|WNE|WGT|WGE|WHI|WCC) _ x (FlagGT_UGT)) => x
476 (CMOV(QEQ|QLE|QLT|QLS|QCS|LEQ|LLE|LLT|LLS|LCS|WEQ|WLE|WLT|WLS|WCS) y _ (FlagGT_UGT)) => y
477 (CMOV(QNE|QGT|QGE|QLS|QCS|LNE|LGT|LGE|LLS|LCS|WNE|WGT|WGE|WLS|WCS) _ x (FlagGT_ULT)) => x
478 (CMOV(QEQ|QLE|QLT|QHI|QCC|LEQ|LLE|LLT|LHI|LCC|WEQ|WLE|WLT|WHI|WCC) y _ (FlagGT_ULT)) => y
479 (CMOV(QNE|QLT|QLE|QCS|QLS|LNE|LLT|LLE|LCS|LLS|WNE|WLT|WLE|WCS|WLS) _ x (FlagLT_ULT)) => x
480 (CMOV(QEQ|QGT|QGE|QHI|QCC|LEQ|LGT|LGE|LHI|LCC|WEQ|WGT|WGE|WHI|WCC) y _ (FlagLT_ULT)) => y
481 (CMOV(QNE|QLT|QLE|QHI|QCC|LNE|LLT|LLE|LHI|LCC|WNE|WLT|WLE|WHI|WCC) _ x (FlagLT_UGT)) => x
482 (CMOV(QEQ|QGT|QGE|QCS|QLS|LEQ|LGT|LGE|LCS|LLS|WEQ|WGT|WGE|WCS|WLS) y _ (FlagLT_UGT)) => y
483
484 // Miscellaneous
485 (IsNonNil p) => (SETNE (TESTQ p p))
486 (IsInBounds idx len) => (SETB (CMPQ idx len))
487 (IsSliceInBounds idx len) => (SETBE (CMPQ idx len))
488 (NilCheck ...) => (LoweredNilCheck ...)
489 (GetG mem) && v.Block.Func.OwnAux.Fn.ABI() != obj.ABIInternal => (LoweredGetG mem) // only lower in old ABI. in new ABI we have a G register.
490 (GetClosurePtr ...) => (LoweredGetClosurePtr ...)
491 (GetCallerPC ...) => (LoweredGetCallerPC ...)
492 (GetCallerSP ...) => (LoweredGetCallerSP ...)
493
494 (HasCPUFeature {s}) => (SETNE (CMPLconst [0] (LoweredHasCPUFeature {s})))
495 (Addr {sym} base) => (LEAQ {sym} base)
496 (LocalAddr <t> {sym} base mem) && t.Elem().HasPointers() => (LEAQ {sym} (SPanchored base mem))
497 (LocalAddr <t> {sym} base _)  && !t.Elem().HasPointers() => (LEAQ {sym} base)
498
499 (MOVBstore [off] {sym} ptr y:(SETL x) mem) && y.Uses == 1 => (SETLstore [off] {sym} ptr x mem)
500 (MOVBstore [off] {sym} ptr y:(SETLE x) mem) && y.Uses == 1 => (SETLEstore [off] {sym} ptr x mem)
501 (MOVBstore [off] {sym} ptr y:(SETG x) mem) && y.Uses == 1 => (SETGstore [off] {sym} ptr x mem)
502 (MOVBstore [off] {sym} ptr y:(SETGE x) mem) && y.Uses == 1 => (SETGEstore [off] {sym} ptr x mem)
503 (MOVBstore [off] {sym} ptr y:(SETEQ x) mem) && y.Uses == 1 => (SETEQstore [off] {sym} ptr x mem)
504 (MOVBstore [off] {sym} ptr y:(SETNE x) mem) && y.Uses == 1 => (SETNEstore [off] {sym} ptr x mem)
505 (MOVBstore [off] {sym} ptr y:(SETB x) mem) && y.Uses == 1 => (SETBstore [off] {sym} ptr x mem)
506 (MOVBstore [off] {sym} ptr y:(SETBE x) mem) && y.Uses == 1 => (SETBEstore [off] {sym} ptr x mem)
507 (MOVBstore [off] {sym} ptr y:(SETA x) mem) && y.Uses == 1 => (SETAstore [off] {sym} ptr x mem)
508 (MOVBstore [off] {sym} ptr y:(SETAE x) mem) && y.Uses == 1 => (SETAEstore [off] {sym} ptr x mem)
509
510 // block rewrites
511 (If (SETL  cmp) yes no) => (LT  cmp yes no)
512 (If (SETLE cmp) yes no) => (LE  cmp yes no)
513 (If (SETG  cmp) yes no) => (GT  cmp yes no)
514 (If (SETGE cmp) yes no) => (GE  cmp yes no)
515 (If (SETEQ cmp) yes no) => (EQ  cmp yes no)
516 (If (SETNE cmp) yes no) => (NE  cmp yes no)
517 (If (SETB  cmp) yes no) => (ULT cmp yes no)
518 (If (SETBE cmp) yes no) => (ULE cmp yes no)
519 (If (SETA  cmp) yes no) => (UGT cmp yes no)
520 (If (SETAE cmp) yes no) => (UGE cmp yes no)
521 (If (SETO cmp) yes no) => (OS cmp yes no)
522
523 // Special case for floating point - LF/LEF not generated
524 (If (SETGF  cmp) yes no) => (UGT  cmp yes no)
525 (If (SETGEF cmp) yes no) => (UGE  cmp yes no)
526 (If (SETEQF cmp) yes no) => (EQF  cmp yes no)
527 (If (SETNEF cmp) yes no) => (NEF  cmp yes no)
528
529 (If cond yes no) => (NE (TESTB cond cond) yes no)
530
531 (JumpTable idx) => (JUMPTABLE {makeJumpTableSym(b)} idx (LEAQ <typ.Uintptr> {makeJumpTableSym(b)} (SB)))
532
533 // Atomic loads.  Other than preserving their ordering with respect to other loads, nothing special here.
534 (AtomicLoad8 ptr mem) => (MOVBatomicload ptr mem)
535 (AtomicLoad32 ptr mem) => (MOVLatomicload ptr mem)
536 (AtomicLoad64 ptr mem) => (MOVQatomicload ptr mem)
537 (AtomicLoadPtr ptr mem) => (MOVQatomicload ptr mem)
538
539 // Atomic stores.  We use XCHG to prevent the hardware reordering a subsequent load.
540 // TODO: most runtime uses of atomic stores don't need that property.  Use normal stores for those?
541 (AtomicStore8 ptr val mem) => (Select1 (XCHGB <types.NewTuple(typ.UInt8,types.TypeMem)> val ptr mem))
542 (AtomicStore32 ptr val mem) => (Select1 (XCHGL <types.NewTuple(typ.UInt32,types.TypeMem)> val ptr mem))
543 (AtomicStore64 ptr val mem) => (Select1 (XCHGQ <types.NewTuple(typ.UInt64,types.TypeMem)> val ptr mem))
544 (AtomicStorePtrNoWB ptr val mem) => (Select1 (XCHGQ <types.NewTuple(typ.BytePtr,types.TypeMem)> val ptr mem))
545
546 // Atomic exchanges.
547 (AtomicExchange32 ptr val mem) => (XCHGL val ptr mem)
548 (AtomicExchange64 ptr val mem) => (XCHGQ val ptr mem)
549
550 // Atomic adds.
551 (AtomicAdd32 ptr val mem) => (AddTupleFirst32 val (XADDLlock val ptr mem))
552 (AtomicAdd64 ptr val mem) => (AddTupleFirst64 val (XADDQlock val ptr mem))
553 (Select0 <t> (AddTupleFirst32 val tuple)) => (ADDL val (Select0 <t> tuple))
554 (Select1     (AddTupleFirst32   _ tuple)) => (Select1 tuple)
555 (Select0 <t> (AddTupleFirst64 val tuple)) => (ADDQ val (Select0 <t> tuple))
556 (Select1     (AddTupleFirst64   _ tuple)) => (Select1 tuple)
557
558 // Atomic compare and swap.
559 (AtomicCompareAndSwap32 ptr old new_ mem) => (CMPXCHGLlock ptr old new_ mem)
560 (AtomicCompareAndSwap64 ptr old new_ mem) => (CMPXCHGQlock ptr old new_ mem)
561
562 // Atomic memory updates.
563 (AtomicAnd8  ptr val mem) => (ANDBlock ptr val mem)
564 (AtomicAnd32 ptr val mem) => (ANDLlock ptr val mem)
565 (AtomicOr8   ptr val mem) => (ORBlock  ptr val mem)
566 (AtomicOr32  ptr val mem) => (ORLlock  ptr val mem)
567
568 // Write barrier.
569 (WB ...) => (LoweredWB ...)
570
571 (PanicBounds [kind] x y mem) && boundsABI(kind) == 0 => (LoweredPanicBoundsA [kind] x y mem)
572 (PanicBounds [kind] x y mem) && boundsABI(kind) == 1 => (LoweredPanicBoundsB [kind] x y mem)
573 (PanicBounds [kind] x y mem) && boundsABI(kind) == 2 => (LoweredPanicBoundsC [kind] x y mem)
574
575 // lowering rotates
576 (RotateLeft8  ...) => (ROLB ...)
577 (RotateLeft16 ...) => (ROLW ...)
578 (RotateLeft32 ...) => (ROLL ...)
579 (RotateLeft64 ...) => (ROLQ ...)
580
581 // ***************************
582 // Above: lowering rules
583 // Below: optimizations
584 // ***************************
585 // TODO: Should the optimizations be a separate pass?
586
587 // Fold boolean tests into blocks
588 (NE (TESTB (SETL  cmp) (SETL  cmp)) yes no) => (LT  cmp yes no)
589 (NE (TESTB (SETLE cmp) (SETLE cmp)) yes no) => (LE  cmp yes no)
590 (NE (TESTB (SETG  cmp) (SETG  cmp)) yes no) => (GT  cmp yes no)
591 (NE (TESTB (SETGE cmp) (SETGE cmp)) yes no) => (GE  cmp yes no)
592 (NE (TESTB (SETEQ cmp) (SETEQ cmp)) yes no) => (EQ  cmp yes no)
593 (NE (TESTB (SETNE cmp) (SETNE cmp)) yes no) => (NE  cmp yes no)
594 (NE (TESTB (SETB  cmp) (SETB  cmp)) yes no) => (ULT cmp yes no)
595 (NE (TESTB (SETBE cmp) (SETBE cmp)) yes no) => (ULE cmp yes no)
596 (NE (TESTB (SETA  cmp) (SETA  cmp)) yes no) => (UGT cmp yes no)
597 (NE (TESTB (SETAE cmp) (SETAE cmp)) yes no) => (UGE cmp yes no)
598 (NE (TESTB (SETO cmp) (SETO cmp)) yes no) => (OS cmp yes no)
599
600 // Unsigned comparisons to 0/1
601 (ULT (TEST(Q|L|W|B) x x) yes no) => (First no yes)
602 (UGE (TEST(Q|L|W|B) x x) yes no) => (First yes no)
603 (SETB (TEST(Q|L|W|B) x x)) => (ConstBool [false])
604 (SETAE (TEST(Q|L|W|B) x x)) => (ConstBool [true])
605
606 // x & 1 != 0 -> x & 1
607 (SETNE (TEST(B|W)const [1] x)) => (AND(L|L)const [1] x)
608 (SETB (BT(L|Q)const [0] x)) => (AND(L|Q)const [1] x)
609
610 // Recognize bit tests: a&(1<<b) != 0 for b suitably bounded
611 // Note that BTx instructions use the carry bit, so we need to convert tests for zero flag
612 // into tests for carry flags.
613 // ULT and SETB check the carry flag; they are identical to CS and SETCS. Same, mutatis
614 // mutandis, for UGE and SETAE, and CC and SETCC.
615 ((NE|EQ) (TESTL (SHLL (MOVLconst [1]) x) y)) => ((ULT|UGE) (BTL x y))
616 ((NE|EQ) (TESTQ (SHLQ (MOVQconst [1]) x) y)) => ((ULT|UGE) (BTQ x y))
617 ((NE|EQ) (TESTLconst [c] x)) && isUint32PowerOfTwo(int64(c))
618     => ((ULT|UGE) (BTLconst [int8(log32(c))] x))
619 ((NE|EQ) (TESTQconst [c] x)) && isUint64PowerOfTwo(int64(c))
620     => ((ULT|UGE) (BTQconst [int8(log32(c))] x))
621 ((NE|EQ) (TESTQ (MOVQconst [c]) x)) && isUint64PowerOfTwo(c)
622     => ((ULT|UGE) (BTQconst [int8(log64(c))] x))
623 (SET(NE|EQ) (TESTL (SHLL (MOVLconst [1]) x) y)) => (SET(B|AE)  (BTL x y))
624 (SET(NE|EQ) (TESTQ (SHLQ (MOVQconst [1]) x) y)) => (SET(B|AE)  (BTQ x y))
625 (SET(NE|EQ) (TESTLconst [c] x)) && isUint32PowerOfTwo(int64(c))
626     => (SET(B|AE)  (BTLconst [int8(log32(c))] x))
627 (SET(NE|EQ) (TESTQconst [c] x)) && isUint64PowerOfTwo(int64(c))
628     => (SET(B|AE)  (BTQconst [int8(log32(c))] x))
629 (SET(NE|EQ) (TESTQ (MOVQconst [c]) x)) && isUint64PowerOfTwo(c)
630     => (SET(B|AE)  (BTQconst [int8(log64(c))] x))
631 // SET..store variant
632 (SET(NE|EQ)store [off] {sym} ptr (TESTL (SHLL (MOVLconst [1]) x) y) mem)
633     => (SET(B|AE)store  [off] {sym} ptr (BTL x y) mem)
634 (SET(NE|EQ)store [off] {sym} ptr (TESTQ (SHLQ (MOVQconst [1]) x) y) mem)
635     => (SET(B|AE)store  [off] {sym} ptr (BTQ x y) mem)
636 (SET(NE|EQ)store [off] {sym} ptr (TESTLconst [c] x) mem) && isUint32PowerOfTwo(int64(c))
637     => (SET(B|AE)store  [off] {sym} ptr (BTLconst [int8(log32(c))] x) mem)
638 (SET(NE|EQ)store [off] {sym} ptr (TESTQconst [c] x) mem) && isUint64PowerOfTwo(int64(c))
639     => (SET(B|AE)store  [off] {sym} ptr (BTQconst [int8(log32(c))] x) mem)
640 (SET(NE|EQ)store [off] {sym} ptr (TESTQ (MOVQconst [c]) x) mem) && isUint64PowerOfTwo(c)
641     => (SET(B|AE)store  [off] {sym} ptr (BTQconst [int8(log64(c))] x) mem)
642
643 // Handle bit-testing in the form (a>>b)&1 != 0 by building the above rules
644 // and further combining shifts.
645 (BT(Q|L)const [c] (SHRQconst [d] x)) && (c+d)<64 => (BTQconst [c+d] x)
646 (BT(Q|L)const [c] (SHLQconst [d] x)) && c>d      => (BT(Q|L)const [c-d] x)
647 (BT(Q|L)const [0] s:(SHRQ x y)) => (BTQ y x)
648 (BTLconst [c] (SHRLconst [d] x)) && (c+d)<32 => (BTLconst [c+d] x)
649 (BTLconst [c] (SHLLconst [d] x)) && c>d      => (BTLconst [c-d] x)
650 (BTLconst [0] s:(SHR(L|XL) x y)) => (BTL y x)
651
652 // Rewrite a & 1 != 1 into a & 1 == 0.
653 // Among other things, this lets us turn (a>>b)&1 != 1 into a bit test.
654 (SET(NE|EQ) (CMPLconst [1] s:(ANDLconst [1] _))) => (SET(EQ|NE) (CMPLconst [0] s))
655 (SET(NE|EQ)store [off] {sym} ptr (CMPLconst [1] s:(ANDLconst [1] _)) mem) => (SET(EQ|NE)store [off] {sym} ptr (CMPLconst [0] s) mem)
656 (SET(NE|EQ) (CMPQconst [1] s:(ANDQconst [1] _))) => (SET(EQ|NE) (CMPQconst [0] s))
657 (SET(NE|EQ)store [off] {sym} ptr (CMPQconst [1] s:(ANDQconst [1] _)) mem) => (SET(EQ|NE)store [off] {sym} ptr (CMPQconst [0] s) mem)
658
659 // Recognize bit setting (a |= 1<<b) and toggling (a ^= 1<<b)
660 (OR(Q|L) (SHL(Q|L) (MOV(Q|L)const [1]) y) x) => (BTS(Q|L) x y)
661 (XOR(Q|L) (SHL(Q|L) (MOV(Q|L)const [1]) y) x) => (BTC(Q|L) x y)
662 // Note: only convert OR/XOR to BTS/BTC if the constant wouldn't fit in
663 // the constant field of the OR/XOR instruction. See issue 61694.
664 ((OR|XOR)Q (MOVQconst [c]) x) && isUint64PowerOfTwo(c) && uint64(c) >= 1<<31 => (BT(S|C)Qconst [int8(log64(c))] x)
665
666 // Recognize bit clearing: a &^= 1<<b
667 (AND(Q|L) (NOT(Q|L) (SHL(Q|L) (MOV(Q|L)const [1]) y)) x) => (BTR(Q|L) x y)
668 (ANDN(Q|L) x (SHL(Q|L) (MOV(Q|L)const [1]) y)) => (BTR(Q|L) x y)
669 // Note: only convert AND to BTR if the constant wouldn't fit in
670 // the constant field of the AND instruction. See issue 61694.
671 (ANDQ (MOVQconst [c]) x) && isUint64PowerOfTwo(^c) && uint64(^c) >= 1<<31 => (BTRQconst [int8(log64(^c))] x)
672
673 // Special-case bit patterns on first/last bit.
674 // generic.rules changes ANDs of high-part/low-part masks into a couple of shifts,
675 // for instance:
676 //    x & 0xFFFF0000 -> (x >> 16) << 16
677 //    x & 0x80000000 -> (x >> 31) << 31
678 //
679 // In case the mask is just one bit (like second example above), it conflicts
680 // with the above rules to detect bit-testing / bit-clearing of first/last bit.
681 // We thus special-case them, by detecting the shift patterns.
682
683 // Special case resetting first/last bit
684 (SHL(L|Q)const [1] (SHR(L|Q)const [1] x))
685         => (AND(L|Q)const [-2] x)
686 (SHRLconst [1] (SHLLconst [1] x))
687         => (ANDLconst [0x7fffffff] x)
688 (SHRQconst [1] (SHLQconst [1] x))
689         => (BTRQconst [63] x)
690
691 // Special case testing first/last bit (with double-shift generated by generic.rules)
692 ((SETNE|SETEQ|NE|EQ) (TESTQ z1:(SHLQconst [63] (SHRQconst [63] x)) z2)) && z1==z2
693     => ((SETB|SETAE|ULT|UGE) (BTQconst [63] x))
694 ((SETNE|SETEQ|NE|EQ) (TESTL z1:(SHLLconst [31] (SHRQconst [31] x)) z2)) && z1==z2
695     => ((SETB|SETAE|ULT|UGE) (BTQconst [31] x))
696 (SET(NE|EQ)store [off] {sym} ptr (TESTQ z1:(SHLQconst [63] (SHRQconst [63] x)) z2) mem) && z1==z2
697     => (SET(B|AE)store [off] {sym} ptr (BTQconst [63] x) mem)
698 (SET(NE|EQ)store [off] {sym} ptr (TESTL z1:(SHLLconst [31] (SHRLconst [31] x)) z2) mem) && z1==z2
699     => (SET(B|AE)store [off] {sym} ptr (BTLconst [31] x) mem)
700
701 ((SETNE|SETEQ|NE|EQ) (TESTQ z1:(SHRQconst [63] (SHLQconst [63] x)) z2)) && z1==z2
702     => ((SETB|SETAE|ULT|UGE)  (BTQconst [0] x))
703 ((SETNE|SETEQ|NE|EQ) (TESTL z1:(SHRLconst [31] (SHLLconst [31] x)) z2)) && z1==z2
704     => ((SETB|SETAE|ULT|UGE)  (BTLconst [0] x))
705 (SET(NE|EQ)store [off] {sym} ptr (TESTQ z1:(SHRQconst [63] (SHLQconst [63] x)) z2) mem) && z1==z2
706     => (SET(B|AE)store [off] {sym} ptr (BTQconst [0] x) mem)
707 (SET(NE|EQ)store [off] {sym} ptr (TESTL z1:(SHRLconst [31] (SHLLconst [31] x)) z2) mem) && z1==z2
708     => (SET(B|AE)store [off] {sym} ptr (BTLconst [0] x) mem)
709
710 // Special-case manually testing last bit with "a>>63 != 0" (without "&1")
711 ((SETNE|SETEQ|NE|EQ) (TESTQ z1:(SHRQconst [63] x) z2)) && z1==z2
712     => ((SETB|SETAE|ULT|UGE) (BTQconst [63] x))
713 ((SETNE|SETEQ|NE|EQ) (TESTL z1:(SHRLconst [31] x) z2)) && z1==z2
714     => ((SETB|SETAE|ULT|UGE) (BTLconst [31] x))
715 (SET(NE|EQ)store [off] {sym} ptr (TESTQ z1:(SHRQconst [63] x) z2) mem) && z1==z2
716     => (SET(B|AE)store [off] {sym} ptr (BTQconst [63] x) mem)
717 (SET(NE|EQ)store [off] {sym} ptr (TESTL z1:(SHRLconst [31] x) z2) mem) && z1==z2
718     => (SET(B|AE)store [off] {sym} ptr (BTLconst [31] x) mem)
719
720 // Fold combinations of bit ops on same bit. An example is math.Copysign(c,-1)
721 (BTSQconst [c] (BTRQconst [c] x)) => (BTSQconst [c] x)
722 (BTSQconst [c] (BTCQconst [c] x)) => (BTSQconst [c] x)
723 (BTRQconst [c] (BTSQconst [c] x)) => (BTRQconst [c] x)
724 (BTRQconst [c] (BTCQconst [c] x)) => (BTRQconst [c] x)
725
726 // Fold boolean negation into SETcc.
727 (XORLconst [1] (SETNE x)) => (SETEQ x)
728 (XORLconst [1] (SETEQ x)) => (SETNE x)
729 (XORLconst [1] (SETL  x)) => (SETGE x)
730 (XORLconst [1] (SETGE x)) => (SETL  x)
731 (XORLconst [1] (SETLE x)) => (SETG  x)
732 (XORLconst [1] (SETG  x)) => (SETLE x)
733 (XORLconst [1] (SETB  x)) => (SETAE x)
734 (XORLconst [1] (SETAE x)) => (SETB  x)
735 (XORLconst [1] (SETBE x)) => (SETA  x)
736 (XORLconst [1] (SETA  x)) => (SETBE x)
737
738 // Special case for floating point - LF/LEF not generated
739 (NE (TESTB (SETGF  cmp) (SETGF  cmp)) yes no) => (UGT  cmp yes no)
740 (NE (TESTB (SETGEF cmp) (SETGEF cmp)) yes no) => (UGE  cmp yes no)
741 (NE (TESTB (SETEQF cmp) (SETEQF cmp)) yes no) => (EQF  cmp yes no)
742 (NE (TESTB (SETNEF cmp) (SETNEF cmp)) yes no) => (NEF  cmp yes no)
743
744 // Disabled because it interferes with the pattern match above and makes worse code.
745 // (SETNEF x) => (ORQ (SETNE <typ.Int8> x) (SETNAN <typ.Int8> x))
746 // (SETEQF x) => (ANDQ (SETEQ <typ.Int8> x) (SETORD <typ.Int8> x))
747
748 // fold constants into instructions
749 (ADDQ x (MOVQconst <t> [c])) && is32Bit(c) && !t.IsPtr() => (ADDQconst [int32(c)] x)
750 (ADDQ x (MOVLconst [c])) => (ADDQconst [c] x)
751 (ADDL x (MOVLconst [c])) => (ADDLconst [c] x)
752
753 (SUBQ x (MOVQconst [c])) && is32Bit(c) => (SUBQconst x [int32(c)])
754 (SUBQ (MOVQconst [c]) x) && is32Bit(c) => (NEGQ (SUBQconst <v.Type> x [int32(c)]))
755 (SUBL x (MOVLconst [c])) => (SUBLconst x [c])
756 (SUBL (MOVLconst [c]) x) => (NEGL (SUBLconst <v.Type> x [c]))
757
758 (MULQ x (MOVQconst [c])) && is32Bit(c) => (MULQconst [int32(c)] x)
759 (MULL x (MOVLconst [c])) => (MULLconst [c] x)
760
761 (ANDQ x (MOVQconst [c])) && is32Bit(c) => (ANDQconst [int32(c)] x)
762 (ANDL x (MOVLconst [c])) => (ANDLconst [c] x)
763
764 (AND(L|Q)const [c] (AND(L|Q)const [d] x)) => (AND(L|Q)const [c & d] x)
765 (XOR(L|Q)const [c] (XOR(L|Q)const [d] x)) => (XOR(L|Q)const [c ^ d] x)
766 (OR(L|Q)const  [c] (OR(L|Q)const  [d] x)) => (OR(L|Q)const  [c | d] x)
767
768 (MULLconst [c] (MULLconst [d] x)) => (MULLconst [c * d] x)
769 (MULQconst [c] (MULQconst [d] x)) && is32Bit(int64(c)*int64(d)) => (MULQconst [c * d] x)
770
771 (ORQ x (MOVQconst [c])) && is32Bit(c) => (ORQconst [int32(c)] x)
772 (ORQ x (MOVLconst [c])) => (ORQconst [c] x)
773 (ORL x (MOVLconst [c])) => (ORLconst [c] x)
774
775 (XORQ x (MOVQconst [c])) && is32Bit(c) => (XORQconst [int32(c)] x)
776 (XORL x (MOVLconst [c])) => (XORLconst [c] x)
777
778 (SHLQ x (MOV(Q|L)const [c])) => (SHLQconst [int8(c&63)] x)
779 (SHLL x (MOV(Q|L)const [c])) => (SHLLconst [int8(c&31)] x)
780
781 (SHRQ x (MOV(Q|L)const [c])) => (SHRQconst [int8(c&63)] x)
782 (SHRL x (MOV(Q|L)const [c])) => (SHRLconst [int8(c&31)] x)
783 (SHRW x (MOV(Q|L)const [c])) && c&31 < 16 => (SHRWconst [int8(c&31)] x)
784 (SHRW _ (MOV(Q|L)const [c])) && c&31 >= 16 => (MOVLconst [0])
785 (SHRB x (MOV(Q|L)const [c])) && c&31 < 8 => (SHRBconst [int8(c&31)] x)
786 (SHRB _ (MOV(Q|L)const [c])) && c&31 >= 8 => (MOVLconst [0])
787
788 (SARQ x (MOV(Q|L)const [c])) => (SARQconst [int8(c&63)] x)
789 (SARL x (MOV(Q|L)const [c])) => (SARLconst [int8(c&31)] x)
790 (SARW x (MOV(Q|L)const [c])) => (SARWconst [int8(min(int64(c)&31,15))] x)
791 (SARB x (MOV(Q|L)const [c])) => (SARBconst [int8(min(int64(c)&31,7))] x)
792
793 // Operations which don't affect the low 6/5 bits of the shift amount are NOPs.
794 ((SHLQ|SHRQ|SARQ) x (ADDQconst [c] y)) && c & 63 == 0  => ((SHLQ|SHRQ|SARQ) x y)
795 ((SHLQ|SHRQ|SARQ) x (NEGQ <t> (ADDQconst [c] y))) && c & 63 == 0  => ((SHLQ|SHRQ|SARQ) x (NEGQ <t> y))
796 ((SHLQ|SHRQ|SARQ) x (ANDQconst [c] y)) && c & 63 == 63 => ((SHLQ|SHRQ|SARQ) x y)
797 ((SHLQ|SHRQ|SARQ) x (NEGQ <t> (ANDQconst [c] y))) && c & 63 == 63 => ((SHLQ|SHRQ|SARQ) x (NEGQ <t> y))
798
799 ((SHLL|SHRL|SARL) x (ADDQconst [c] y)) && c & 31 == 0  => ((SHLL|SHRL|SARL) x y)
800 ((SHLL|SHRL|SARL) x (NEGQ <t> (ADDQconst [c] y))) && c & 31 == 0  => ((SHLL|SHRL|SARL) x (NEGQ <t> y))
801 ((SHLL|SHRL|SARL) x (ANDQconst [c] y)) && c & 31 == 31 => ((SHLL|SHRL|SARL) x y)
802 ((SHLL|SHRL|SARL) x (NEGQ <t> (ANDQconst [c] y))) && c & 31 == 31 => ((SHLL|SHRL|SARL) x (NEGQ <t> y))
803
804 ((SHLQ|SHRQ|SARQ) x (ADDLconst [c] y)) && c & 63 == 0  => ((SHLQ|SHRQ|SARQ) x y)
805 ((SHLQ|SHRQ|SARQ) x (NEGL <t> (ADDLconst [c] y))) && c & 63 == 0  => ((SHLQ|SHRQ|SARQ) x (NEGL <t> y))
806 ((SHLQ|SHRQ|SARQ) x (ANDLconst [c] y)) && c & 63 == 63 => ((SHLQ|SHRQ|SARQ) x y)
807 ((SHLQ|SHRQ|SARQ) x (NEGL <t> (ANDLconst [c] y))) && c & 63 == 63 => ((SHLQ|SHRQ|SARQ) x (NEGL <t> y))
808
809 ((SHLL|SHRL|SARL) x (ADDLconst [c] y)) && c & 31 == 0  => ((SHLL|SHRL|SARL) x y)
810 ((SHLL|SHRL|SARL) x (NEGL <t> (ADDLconst [c] y))) && c & 31 == 0  => ((SHLL|SHRL|SARL) x (NEGL <t> y))
811 ((SHLL|SHRL|SARL) x (ANDLconst [c] y)) && c & 31 == 31 => ((SHLL|SHRL|SARL) x y)
812 ((SHLL|SHRL|SARL) x (NEGL <t> (ANDLconst [c] y))) && c & 31 == 31 => ((SHLL|SHRL|SARL) x (NEGL <t> y))
813
814 // rotate left negative = rotate right
815 (ROLQ x (NEG(Q|L) y)) => (RORQ x y)
816 (ROLL x (NEG(Q|L) y)) => (RORL x y)
817 (ROLW x (NEG(Q|L) y)) => (RORW x y)
818 (ROLB x (NEG(Q|L) y)) => (RORB x y)
819
820 // rotate right negative = rotate left
821 (RORQ x (NEG(Q|L) y)) => (ROLQ x y)
822 (RORL x (NEG(Q|L) y)) => (ROLL x y)
823 (RORW x (NEG(Q|L) y)) => (ROLW x y)
824 (RORB x (NEG(Q|L) y)) => (ROLB x y)
825
826 // rotate by constants
827 (ROLQ x (MOV(Q|L)const [c])) => (ROLQconst [int8(c&63)] x)
828 (ROLL x (MOV(Q|L)const [c])) => (ROLLconst [int8(c&31)] x)
829 (ROLW x (MOV(Q|L)const [c])) => (ROLWconst [int8(c&15)] x)
830 (ROLB x (MOV(Q|L)const [c])) => (ROLBconst [int8(c&7) ] x)
831
832 (RORQ x (MOV(Q|L)const [c])) => (ROLQconst [int8((-c)&63)] x)
833 (RORL x (MOV(Q|L)const [c])) => (ROLLconst [int8((-c)&31)] x)
834 (RORW x (MOV(Q|L)const [c])) => (ROLWconst [int8((-c)&15)] x)
835 (RORB x (MOV(Q|L)const [c])) => (ROLBconst [int8((-c)&7) ] x)
836
837 // Constant shift simplifications
838 ((SHLQ|SHRQ|SARQ)const      x [0]) => x
839 ((SHLL|SHRL|SARL)const      x [0]) => x
840 ((SHRW|SARW)const           x [0]) => x
841 ((SHRB|SARB)const           x [0]) => x
842 ((ROLQ|ROLL|ROLW|ROLB)const x [0]) => x
843
844 // Multi-register shifts
845 (ORQ (SH(R|L)Q lo bits) (SH(L|R)Q hi (NEGQ bits))) => (SH(R|L)DQ lo hi bits)
846 (ORQ (SH(R|L)XQ lo bits) (SH(L|R)XQ hi (NEGQ bits))) => (SH(R|L)DQ lo hi bits)
847
848 // Note: the word and byte shifts keep the low 5 bits (not the low 4 or 3 bits)
849 // because the x86 instructions are defined to use all 5 bits of the shift even
850 // for the small shifts. I don't think we'll ever generate a weird shift (e.g.
851 // (SHRW x (MOVLconst [24])), but just in case.
852
853 (CMPQ x (MOVQconst [c])) && is32Bit(c) => (CMPQconst x [int32(c)])
854 (CMPQ (MOVQconst [c]) x) && is32Bit(c) => (InvertFlags (CMPQconst x [int32(c)]))
855 (CMPL x (MOVLconst [c])) => (CMPLconst x [c])
856 (CMPL (MOVLconst [c]) x) => (InvertFlags (CMPLconst x [c]))
857 (CMPW x (MOVLconst [c])) => (CMPWconst x [int16(c)])
858 (CMPW (MOVLconst [c]) x) => (InvertFlags (CMPWconst x [int16(c)]))
859 (CMPB x (MOVLconst [c])) => (CMPBconst x [int8(c)])
860 (CMPB (MOVLconst [c]) x) => (InvertFlags (CMPBconst x [int8(c)]))
861
862 // Canonicalize the order of arguments to comparisons - helps with CSE.
863 (CMP(Q|L|W|B) x y) && canonLessThan(x,y) => (InvertFlags (CMP(Q|L|W|B) y x))
864
865 // Using MOVZX instead of AND is cheaper.
866 (AND(Q|L)const [  0xFF] x) => (MOVBQZX x)
867 (AND(Q|L)const [0xFFFF] x) => (MOVWQZX x)
868 // This rule is currently invalid because 0xFFFFFFFF is not representable by a signed int32.
869 // Commenting out for now, because it also can't trigger because of the is32bit guard on the
870 // ANDQconst lowering-rule, above, prevents 0xFFFFFFFF from matching (for the same reason)
871 // Using an alternate form of this rule segfaults some binaries because of
872 // adverse interactions with other passes.
873 // (ANDQconst [0xFFFFFFFF] x) => (MOVLQZX x)
874
875 // strength reduction
876 // Assumes that the following costs from https://gmplib.org/~tege/x86-timing.pdf:
877 //    1 - addq, shlq, leaq, negq, subq
878 //    3 - imulq
879 // This limits the rewrites to two instructions.
880 // Note that negq always operates in-place,
881 // which can require a register-register move
882 // to preserve the original value,
883 // so it must be used with care.
884 (MUL(Q|L)const [-9] x) => (NEG(Q|L) (LEA(Q|L)8 <v.Type> x x))
885 (MUL(Q|L)const [-5] x) => (NEG(Q|L) (LEA(Q|L)4 <v.Type> x x))
886 (MUL(Q|L)const [-3] x) => (NEG(Q|L) (LEA(Q|L)2 <v.Type> x x))
887 (MUL(Q|L)const [-1] x) => (NEG(Q|L) x)
888 (MUL(Q|L)const [ 0] _) => (MOV(Q|L)const [0])
889 (MUL(Q|L)const [ 1] x) => x
890 (MUL(Q|L)const [ 3] x) => (LEA(Q|L)2 x x)
891 (MUL(Q|L)const [ 5] x) => (LEA(Q|L)4 x x)
892 (MUL(Q|L)const [ 7] x) => (LEA(Q|L)2 x (LEA(Q|L)2 <v.Type> x x))
893 (MUL(Q|L)const [ 9] x) => (LEA(Q|L)8 x x)
894 (MUL(Q|L)const [11] x) => (LEA(Q|L)2 x (LEA(Q|L)4 <v.Type> x x))
895 (MUL(Q|L)const [13] x) => (LEA(Q|L)4 x (LEA(Q|L)2 <v.Type> x x))
896 (MUL(Q|L)const [19] x) => (LEA(Q|L)2 x (LEA(Q|L)8 <v.Type> x x))
897 (MUL(Q|L)const [21] x) => (LEA(Q|L)4 x (LEA(Q|L)4 <v.Type> x x))
898 (MUL(Q|L)const [25] x) => (LEA(Q|L)8 x (LEA(Q|L)2 <v.Type> x x))
899 (MUL(Q|L)const [27] x) => (LEA(Q|L)8 (LEA(Q|L)2 <v.Type> x x) (LEA(Q|L)2 <v.Type> x x))
900 (MUL(Q|L)const [37] x) => (LEA(Q|L)4 x (LEA(Q|L)8 <v.Type> x x))
901 (MUL(Q|L)const [41] x) => (LEA(Q|L)8 x (LEA(Q|L)4 <v.Type> x x))
902 (MUL(Q|L)const [45] x) => (LEA(Q|L)8 (LEA(Q|L)4 <v.Type> x x) (LEA(Q|L)4 <v.Type> x x))
903 (MUL(Q|L)const [73] x) => (LEA(Q|L)8 x (LEA(Q|L)8 <v.Type> x x))
904 (MUL(Q|L)const [81] x) => (LEA(Q|L)8 (LEA(Q|L)8 <v.Type> x x) (LEA(Q|L)8 <v.Type> x x))
905
906 (MUL(Q|L)const [c] x) && isPowerOfTwo64(int64(c)+1) && c >=  15 => (SUB(Q|L)  (SHL(Q|L)const <v.Type> [int8(log64(int64(c)+1))] x) x)
907 (MUL(Q|L)const [c] x) && isPowerOfTwo32(c-1) && c >=  17 => (LEA(Q|L)1 (SHL(Q|L)const <v.Type> [int8(log32(c-1))] x) x)
908 (MUL(Q|L)const [c] x) && isPowerOfTwo32(c-2) && c >=  34 => (LEA(Q|L)2 (SHL(Q|L)const <v.Type> [int8(log32(c-2))] x) x)
909 (MUL(Q|L)const [c] x) && isPowerOfTwo32(c-4) && c >=  68 => (LEA(Q|L)4 (SHL(Q|L)const <v.Type> [int8(log32(c-4))] x) x)
910 (MUL(Q|L)const [c] x) && isPowerOfTwo32(c-8) && c >= 136 => (LEA(Q|L)8 (SHL(Q|L)const <v.Type> [int8(log32(c-8))] x) x)
911 (MUL(Q|L)const [c] x) && c%3 == 0 && isPowerOfTwo32(c/3) => (SHL(Q|L)const [int8(log32(c/3))] (LEA(Q|L)2 <v.Type> x x))
912 (MUL(Q|L)const [c] x) && c%5 == 0 && isPowerOfTwo32(c/5) => (SHL(Q|L)const [int8(log32(c/5))] (LEA(Q|L)4 <v.Type> x x))
913 (MUL(Q|L)const [c] x) && c%9 == 0 && isPowerOfTwo32(c/9) => (SHL(Q|L)const [int8(log32(c/9))] (LEA(Q|L)8 <v.Type> x x))
914
915 // combine add/shift into LEAQ/LEAL
916 (ADD(L|Q) x (SHL(L|Q)const [3] y)) => (LEA(L|Q)8 x y)
917 (ADD(L|Q) x (SHL(L|Q)const [2] y)) => (LEA(L|Q)4 x y)
918 (ADD(L|Q) x (SHL(L|Q)const [1] y)) => (LEA(L|Q)2 x y)
919 (ADD(L|Q) x (ADD(L|Q) y y))        => (LEA(L|Q)2 x y)
920 (ADD(L|Q) x (ADD(L|Q) x y))        => (LEA(L|Q)2 y x)
921
922 // combine ADDQ/ADDQconst into LEAQ1/LEAL1
923 (ADD(Q|L)const [c] (ADD(Q|L) x y)) => (LEA(Q|L)1 [c] x y)
924 (ADD(Q|L) (ADD(Q|L)const [c] x) y) => (LEA(Q|L)1 [c] x y)
925 (ADD(Q|L)const [c] (SHL(Q|L)const [1] x)) => (LEA(Q|L)1 [c] x x)
926
927 // fold ADDQ/ADDL into LEAQ/LEAL
928 (ADD(Q|L)const [c] (LEA(Q|L) [d] {s} x)) && is32Bit(int64(c)+int64(d)) => (LEA(Q|L) [c+d] {s} x)
929 (LEA(Q|L) [c] {s} (ADD(Q|L)const [d] x)) && is32Bit(int64(c)+int64(d)) => (LEA(Q|L) [c+d] {s} x)
930 (LEA(Q|L) [c] {s} (ADD(Q|L) x y)) && x.Op != OpSB && y.Op != OpSB => (LEA(Q|L)1 [c] {s} x y)
931 (ADD(Q|L) x (LEA(Q|L) [c] {s} y)) && x.Op != OpSB && y.Op != OpSB => (LEA(Q|L)1 [c] {s} x y)
932
933 // fold ADDQconst/ADDLconst into LEAQx/LEALx
934 (ADD(Q|L)const [c] (LEA(Q|L)1 [d] {s} x y)) && is32Bit(int64(c)+int64(d)) => (LEA(Q|L)1 [c+d] {s} x y)
935 (ADD(Q|L)const [c] (LEA(Q|L)2 [d] {s} x y)) && is32Bit(int64(c)+int64(d)) => (LEA(Q|L)2 [c+d] {s} x y)
936 (ADD(Q|L)const [c] (LEA(Q|L)4 [d] {s} x y)) && is32Bit(int64(c)+int64(d)) => (LEA(Q|L)4 [c+d] {s} x y)
937 (ADD(Q|L)const [c] (LEA(Q|L)8 [d] {s} x y)) && is32Bit(int64(c)+int64(d)) => (LEA(Q|L)8 [c+d] {s} x y)
938 (LEA(Q|L)1 [c] {s} (ADD(Q|L)const [d] x) y) && is32Bit(int64(c)+int64(d))   && x.Op != OpSB => (LEA(Q|L)1 [c+d] {s} x y)
939 (LEA(Q|L)2 [c] {s} (ADD(Q|L)const [d] x) y) && is32Bit(int64(c)+int64(d))   && x.Op != OpSB => (LEA(Q|L)2 [c+d] {s} x y)
940 (LEA(Q|L)2 [c] {s} x (ADD(Q|L)const [d] y)) && is32Bit(int64(c)+2*int64(d)) && y.Op != OpSB => (LEA(Q|L)2 [c+2*d] {s} x y)
941 (LEA(Q|L)4 [c] {s} (ADD(Q|L)const [d] x) y) && is32Bit(int64(c)+int64(d))   && x.Op != OpSB => (LEA(Q|L)4 [c+d] {s} x y)
942 (LEA(Q|L)4 [c] {s} x (ADD(Q|L)const [d] y)) && is32Bit(int64(c)+4*int64(d)) && y.Op != OpSB => (LEA(Q|L)4 [c+4*d] {s} x y)
943 (LEA(Q|L)8 [c] {s} (ADD(Q|L)const [d] x) y) && is32Bit(int64(c)+int64(d))   && x.Op != OpSB => (LEA(Q|L)8 [c+d] {s} x y)
944 (LEA(Q|L)8 [c] {s} x (ADD(Q|L)const [d] y)) && is32Bit(int64(c)+8*int64(d)) && y.Op != OpSB => (LEA(Q|L)8 [c+8*d] {s} x y)
945
946 // fold shifts into LEAQx/LEALx
947 (LEA(Q|L)1 [c] {s} x (SHL(Q|L)const [1] y)) => (LEA(Q|L)2 [c] {s} x y)
948 (LEA(Q|L)1 [c] {s} x (SHL(Q|L)const [2] y)) => (LEA(Q|L)4 [c] {s} x y)
949 (LEA(Q|L)1 [c] {s} x (SHL(Q|L)const [3] y)) => (LEA(Q|L)8 [c] {s} x y)
950 (LEA(Q|L)2 [c] {s} x (SHL(Q|L)const [1] y)) => (LEA(Q|L)4 [c] {s} x y)
951 (LEA(Q|L)2 [c] {s} x (SHL(Q|L)const [2] y)) => (LEA(Q|L)8 [c] {s} x y)
952 (LEA(Q|L)4 [c] {s} x (SHL(Q|L)const [1] y)) => (LEA(Q|L)8 [c] {s} x y)
953
954 // reverse ordering of compare instruction
955 (SETL (InvertFlags x)) => (SETG x)
956 (SETG (InvertFlags x)) => (SETL x)
957 (SETB (InvertFlags x)) => (SETA x)
958 (SETA (InvertFlags x)) => (SETB x)
959 (SETLE (InvertFlags x)) => (SETGE x)
960 (SETGE (InvertFlags x)) => (SETLE x)
961 (SETBE (InvertFlags x)) => (SETAE x)
962 (SETAE (InvertFlags x)) => (SETBE x)
963 (SETEQ (InvertFlags x)) => (SETEQ x)
964 (SETNE (InvertFlags x)) => (SETNE x)
965
966 (SETLstore [off] {sym} ptr (InvertFlags x) mem) => (SETGstore [off] {sym} ptr x mem)
967 (SETGstore [off] {sym} ptr (InvertFlags x) mem) => (SETLstore [off] {sym} ptr x mem)
968 (SETBstore [off] {sym} ptr (InvertFlags x) mem) => (SETAstore [off] {sym} ptr x mem)
969 (SETAstore [off] {sym} ptr (InvertFlags x) mem) => (SETBstore [off] {sym} ptr x mem)
970 (SETLEstore [off] {sym} ptr (InvertFlags x) mem) => (SETGEstore [off] {sym} ptr x mem)
971 (SETGEstore [off] {sym} ptr (InvertFlags x) mem) => (SETLEstore [off] {sym} ptr x mem)
972 (SETBEstore [off] {sym} ptr (InvertFlags x) mem) => (SETAEstore [off] {sym} ptr x mem)
973 (SETAEstore [off] {sym} ptr (InvertFlags x) mem) => (SETBEstore [off] {sym} ptr x mem)
974 (SETEQstore [off] {sym} ptr (InvertFlags x) mem) => (SETEQstore [off] {sym} ptr x mem)
975 (SETNEstore [off] {sym} ptr (InvertFlags x) mem) => (SETNEstore [off] {sym} ptr x mem)
976
977 // sign extended loads
978 // Note: The combined instruction must end up in the same block
979 // as the original load. If not, we end up making a value with
980 // memory type live in two different blocks, which can lead to
981 // multiple memory values alive simultaneously.
982 // Make sure we don't combine these ops if the load has another use.
983 // This prevents a single load from being split into multiple loads
984 // which then might return different values.  See test/atomicload.go.
985 (MOVBQSX x:(MOVBload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) => @x.Block (MOVBQSXload <v.Type> [off] {sym} ptr mem)
986 (MOVBQSX x:(MOVWload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) => @x.Block (MOVBQSXload <v.Type> [off] {sym} ptr mem)
987 (MOVBQSX x:(MOVLload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) => @x.Block (MOVBQSXload <v.Type> [off] {sym} ptr mem)
988 (MOVBQSX x:(MOVQload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) => @x.Block (MOVBQSXload <v.Type> [off] {sym} ptr mem)
989 (MOVBQZX x:(MOVBload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) => @x.Block (MOVBload <v.Type> [off] {sym} ptr mem)
990 (MOVBQZX x:(MOVWload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) => @x.Block (MOVBload <v.Type> [off] {sym} ptr mem)
991 (MOVBQZX x:(MOVLload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) => @x.Block (MOVBload <v.Type> [off] {sym} ptr mem)
992 (MOVBQZX x:(MOVQload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) => @x.Block (MOVBload <v.Type> [off] {sym} ptr mem)
993 (MOVWQSX x:(MOVWload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) => @x.Block (MOVWQSXload <v.Type> [off] {sym} ptr mem)
994 (MOVWQSX x:(MOVLload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) => @x.Block (MOVWQSXload <v.Type> [off] {sym} ptr mem)
995 (MOVWQSX x:(MOVQload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) => @x.Block (MOVWQSXload <v.Type> [off] {sym} ptr mem)
996 (MOVWQZX x:(MOVWload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) => @x.Block (MOVWload <v.Type> [off] {sym} ptr mem)
997 (MOVWQZX x:(MOVLload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) => @x.Block (MOVWload <v.Type> [off] {sym} ptr mem)
998 (MOVWQZX x:(MOVQload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) => @x.Block (MOVWload <v.Type> [off] {sym} ptr mem)
999 (MOVLQSX x:(MOVLload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) => @x.Block (MOVLQSXload <v.Type> [off] {sym} ptr mem)
1000 (MOVLQSX x:(MOVQload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) => @x.Block (MOVLQSXload <v.Type> [off] {sym} ptr mem)
1001 (MOVLQZX x:(MOVLload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) => @x.Block (MOVLload <v.Type> [off] {sym} ptr mem)
1002 (MOVLQZX x:(MOVQload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) => @x.Block (MOVLload <v.Type> [off] {sym} ptr mem)
1003
1004 (MOVLQZX x) && zeroUpper32Bits(x,3) => x
1005 (MOVWQZX x) && zeroUpper48Bits(x,3) => x
1006 (MOVBQZX x) && zeroUpper56Bits(x,3) => x
1007
1008 // replace load from same location as preceding store with zero/sign extension (or copy in case of full width)
1009 (MOVBload [off] {sym} ptr (MOVBstore [off2] {sym2} ptr2 x _)) && sym == sym2 && off == off2 && isSamePtr(ptr, ptr2) => (MOVBQZX x)
1010 (MOVWload [off] {sym} ptr (MOVWstore [off2] {sym2} ptr2 x _)) && sym == sym2 && off == off2 && isSamePtr(ptr, ptr2) => (MOVWQZX x)
1011 (MOVLload [off] {sym} ptr (MOVLstore [off2] {sym2} ptr2 x _)) && sym == sym2 && off == off2 && isSamePtr(ptr, ptr2) => (MOVLQZX x)
1012 (MOVQload [off] {sym} ptr (MOVQstore [off2] {sym2} ptr2 x _)) && sym == sym2 && off == off2 && isSamePtr(ptr, ptr2) => x
1013 (MOVBQSXload [off] {sym} ptr (MOVBstore [off2] {sym2} ptr2 x _)) && sym == sym2 && off == off2 && isSamePtr(ptr, ptr2) => (MOVBQSX x)
1014 (MOVWQSXload [off] {sym} ptr (MOVWstore [off2] {sym2} ptr2 x _)) && sym == sym2 && off == off2 && isSamePtr(ptr, ptr2) => (MOVWQSX x)
1015 (MOVLQSXload [off] {sym} ptr (MOVLstore [off2] {sym2} ptr2 x _)) && sym == sym2 && off == off2 && isSamePtr(ptr, ptr2) => (MOVLQSX x)
1016
1017 // Fold extensions and ANDs together.
1018 (MOVBQZX (ANDLconst [c] x)) => (ANDLconst [c & 0xff] x)
1019 (MOVWQZX (ANDLconst [c] x)) => (ANDLconst [c & 0xffff] x)
1020 (MOVLQZX (ANDLconst [c] x)) => (ANDLconst [c] x)
1021 (MOVBQSX (ANDLconst [c] x)) && c & 0x80 == 0 => (ANDLconst [c & 0x7f] x)
1022 (MOVWQSX (ANDLconst [c] x)) && c & 0x8000 == 0 => (ANDLconst [c & 0x7fff] x)
1023 (MOVLQSX (ANDLconst [c] x)) && uint32(c) & 0x80000000 == 0 => (ANDLconst [c & 0x7fffffff] x)
1024
1025 // Don't extend before storing
1026 (MOVLstore [off] {sym} ptr (MOVLQSX x) mem) => (MOVLstore [off] {sym} ptr x mem)
1027 (MOVWstore [off] {sym} ptr (MOVWQSX x) mem) => (MOVWstore [off] {sym} ptr x mem)
1028 (MOVBstore [off] {sym} ptr (MOVBQSX x) mem) => (MOVBstore [off] {sym} ptr x mem)
1029 (MOVLstore [off] {sym} ptr (MOVLQZX x) mem) => (MOVLstore [off] {sym} ptr x mem)
1030 (MOVWstore [off] {sym} ptr (MOVWQZX x) mem) => (MOVWstore [off] {sym} ptr x mem)
1031 (MOVBstore [off] {sym} ptr (MOVBQZX x) mem) => (MOVBstore [off] {sym} ptr x mem)
1032
1033 // fold constants into memory operations
1034 // Note that this is not always a good idea because if not all the uses of
1035 // the ADDQconst get eliminated, we still have to compute the ADDQconst and we now
1036 // have potentially two live values (ptr and (ADDQconst [off] ptr)) instead of one.
1037 // Nevertheless, let's do it!
1038 (MOV(Q|L|W|B|SS|SD|O)load  [off1] {sym} (ADDQconst [off2] ptr) mem) && is32Bit(int64(off1)+int64(off2)) =>
1039     (MOV(Q|L|W|B|SS|SD|O)load  [off1+off2] {sym} ptr mem)
1040 (MOV(Q|L|W|B|SS|SD|O)store  [off1] {sym} (ADDQconst [off2] ptr) val mem) && is32Bit(int64(off1)+int64(off2)) =>
1041         (MOV(Q|L|W|B|SS|SD|O)store  [off1+off2] {sym} ptr val mem)
1042 (SET(L|G|B|A|LE|GE|BE|AE|EQ|NE)store [off1] {sym} (ADDQconst [off2] base) val mem) && is32Bit(int64(off1)+int64(off2)) =>
1043         (SET(L|G|B|A|LE|GE|BE|AE|EQ|NE)store [off1+off2] {sym} base val mem)
1044 ((ADD|SUB|AND|OR|XOR)Qload [off1] {sym} val (ADDQconst [off2] base) mem) && is32Bit(int64(off1)+int64(off2)) =>
1045         ((ADD|SUB|AND|OR|XOR)Qload [off1+off2] {sym} val base mem)
1046 ((ADD|SUB|AND|OR|XOR)Lload [off1] {sym} val (ADDQconst [off2] base) mem) && is32Bit(int64(off1)+int64(off2)) =>
1047         ((ADD|SUB|AND|OR|XOR)Lload [off1+off2] {sym} val base mem)
1048 (CMP(Q|L|W|B)load [off1] {sym} (ADDQconst [off2] base) val mem) && is32Bit(int64(off1)+int64(off2)) =>
1049         (CMP(Q|L|W|B)load [off1+off2] {sym} base val mem)
1050 (CMP(Q|L|W|B)constload [valoff1] {sym} (ADDQconst [off2] base) mem) && ValAndOff(valoff1).canAdd32(off2) =>
1051         (CMP(Q|L|W|B)constload [ValAndOff(valoff1).addOffset32(off2)] {sym} base mem)
1052
1053 ((ADD|SUB|MUL|DIV)SSload [off1] {sym} val (ADDQconst [off2] base) mem) && is32Bit(int64(off1)+int64(off2)) =>
1054         ((ADD|SUB|MUL|DIV)SSload [off1+off2] {sym} val base mem)
1055 ((ADD|SUB|MUL|DIV)SDload [off1] {sym} val (ADDQconst [off2] base) mem) && is32Bit(int64(off1)+int64(off2)) =>
1056         ((ADD|SUB|MUL|DIV)SDload [off1+off2] {sym} val base mem)
1057 ((ADD|AND|OR|XOR)Qconstmodify [valoff1] {sym} (ADDQconst [off2] base) mem) && ValAndOff(valoff1).canAdd32(off2) =>
1058         ((ADD|AND|OR|XOR)Qconstmodify [ValAndOff(valoff1).addOffset32(off2)] {sym} base mem)
1059 ((ADD|AND|OR|XOR)Lconstmodify [valoff1] {sym} (ADDQconst [off2] base) mem) && ValAndOff(valoff1).canAdd32(off2) =>
1060         ((ADD|AND|OR|XOR)Lconstmodify [ValAndOff(valoff1).addOffset32(off2)] {sym} base mem)
1061 ((ADD|SUB|AND|OR|XOR)Qmodify [off1] {sym} (ADDQconst [off2] base) val mem) && is32Bit(int64(off1)+int64(off2)) =>
1062         ((ADD|SUB|AND|OR|XOR)Qmodify [off1+off2] {sym} base val mem)
1063 ((ADD|SUB|AND|OR|XOR)Lmodify [off1] {sym} (ADDQconst [off2] base) val mem) && is32Bit(int64(off1)+int64(off2)) =>
1064         ((ADD|SUB|AND|OR|XOR)Lmodify [off1+off2] {sym} base val mem)
1065
1066 // Fold constants into stores.
1067 (MOVQstore [off] {sym} ptr (MOVQconst [c]) mem) && validVal(c) =>
1068         (MOVQstoreconst [makeValAndOff(int32(c),off)] {sym} ptr mem)
1069 (MOVLstore [off] {sym} ptr (MOV(L|Q)const [c]) mem) =>
1070         (MOVLstoreconst [makeValAndOff(int32(c),off)] {sym} ptr mem)
1071 (MOVWstore [off] {sym} ptr (MOV(L|Q)const [c]) mem) =>
1072         (MOVWstoreconst [makeValAndOff(int32(int16(c)),off)] {sym} ptr mem)
1073 (MOVBstore [off] {sym} ptr (MOV(L|Q)const [c]) mem) =>
1074         (MOVBstoreconst [makeValAndOff(int32(int8(c)),off)] {sym} ptr mem)
1075
1076 // Fold address offsets into constant stores.
1077 (MOV(Q|L|W|B|O)storeconst [sc] {s} (ADDQconst [off] ptr) mem) && ValAndOff(sc).canAdd32(off) =>
1078         (MOV(Q|L|W|B|O)storeconst [ValAndOff(sc).addOffset32(off)] {s} ptr mem)
1079
1080 // We need to fold LEAQ into the MOVx ops so that the live variable analysis knows
1081 // what variables are being read/written by the ops.
1082 (MOV(Q|L|W|B|SS|SD|O|BQSX|WQSX|LQSX)load [off1] {sym1} (LEAQ [off2] {sym2} base) mem)
1083         && is32Bit(int64(off1)+int64(off2)) && canMergeSym(sym1, sym2) =>
1084         (MOV(Q|L|W|B|SS|SD|O|BQSX|WQSX|LQSX)load [off1+off2] {mergeSym(sym1,sym2)} base mem)
1085 (MOV(Q|L|W|B|SS|SD|O)store [off1] {sym1} (LEAQ [off2] {sym2} base) val mem)
1086         && is32Bit(int64(off1)+int64(off2)) && canMergeSym(sym1, sym2) =>
1087         (MOV(Q|L|W|B|SS|SD|O)store [off1+off2] {mergeSym(sym1,sym2)} base val mem)
1088 (MOV(Q|L|W|B|O)storeconst [sc] {sym1} (LEAQ [off] {sym2} ptr) mem) && canMergeSym(sym1, sym2) && ValAndOff(sc).canAdd32(off) =>
1089         (MOV(Q|L|W|B|O)storeconst [ValAndOff(sc).addOffset32(off)] {mergeSym(sym1, sym2)} ptr mem)
1090 (SET(L|G|B|A|LE|GE|BE|AE|EQ|NE)store [off1] {sym1} (LEAQ [off2] {sym2} base) val mem)
1091         && is32Bit(int64(off1)+int64(off2)) && canMergeSym(sym1, sym2) =>
1092         (SET(L|G|B|A|LE|GE|BE|AE|EQ|NE)store [off1+off2] {mergeSym(sym1,sym2)} base val mem)
1093 ((ADD|SUB|AND|OR|XOR)Qload [off1] {sym1} val (LEAQ [off2] {sym2} base) mem)
1094         && is32Bit(int64(off1)+int64(off2)) && canMergeSym(sym1, sym2) =>
1095         ((ADD|SUB|AND|OR|XOR)Qload [off1+off2] {mergeSym(sym1,sym2)} val base mem)
1096 ((ADD|SUB|AND|OR|XOR)Lload [off1] {sym1} val (LEAQ [off2] {sym2} base) mem)
1097         && is32Bit(int64(off1)+int64(off2)) && canMergeSym(sym1, sym2) =>
1098         ((ADD|SUB|AND|OR|XOR)Lload [off1+off2] {mergeSym(sym1,sym2)} val base mem)
1099 (CMP(Q|L|W|B)load [off1] {sym1} (LEAQ [off2] {sym2} base) val mem)
1100         && is32Bit(int64(off1)+int64(off2)) && canMergeSym(sym1, sym2) =>
1101         (CMP(Q|L|W|B)load [off1+off2] {mergeSym(sym1,sym2)} base val mem)
1102 (CMP(Q|L|W|B)constload [valoff1] {sym1} (LEAQ [off2] {sym2} base) mem)
1103         && ValAndOff(valoff1).canAdd32(off2) && canMergeSym(sym1, sym2) =>
1104         (CMP(Q|L|W|B)constload [ValAndOff(valoff1).addOffset32(off2)] {mergeSym(sym1,sym2)} base mem)
1105
1106 ((ADD|SUB|MUL|DIV)SSload [off1] {sym1} val (LEAQ [off2] {sym2} base) mem)
1107         && is32Bit(int64(off1)+int64(off2)) && canMergeSym(sym1, sym2) =>
1108         ((ADD|SUB|MUL|DIV)SSload [off1+off2] {mergeSym(sym1,sym2)} val base mem)
1109 ((ADD|SUB|MUL|DIV)SDload [off1] {sym1} val (LEAQ [off2] {sym2} base) mem)
1110         && is32Bit(int64(off1)+int64(off2)) && canMergeSym(sym1, sym2) =>
1111         ((ADD|SUB|MUL|DIV)SDload [off1+off2] {mergeSym(sym1,sym2)} val base mem)
1112 ((ADD|AND|OR|XOR)Qconstmodify [valoff1] {sym1} (LEAQ [off2] {sym2} base) mem)
1113         && ValAndOff(valoff1).canAdd32(off2) && canMergeSym(sym1, sym2) =>
1114         ((ADD|AND|OR|XOR)Qconstmodify [ValAndOff(valoff1).addOffset32(off2)] {mergeSym(sym1,sym2)} base mem)
1115 ((ADD|AND|OR|XOR)Lconstmodify [valoff1] {sym1} (LEAQ [off2] {sym2} base) mem)
1116         && ValAndOff(valoff1).canAdd32(off2) && canMergeSym(sym1, sym2) =>
1117         ((ADD|AND|OR|XOR)Lconstmodify [ValAndOff(valoff1).addOffset32(off2)] {mergeSym(sym1,sym2)} base mem)
1118 ((ADD|SUB|AND|OR|XOR)Qmodify [off1] {sym1} (LEAQ [off2] {sym2} base) val mem)
1119         && is32Bit(int64(off1)+int64(off2)) && canMergeSym(sym1, sym2) =>
1120         ((ADD|SUB|AND|OR|XOR)Qmodify [off1+off2] {mergeSym(sym1,sym2)} base val mem)
1121 ((ADD|SUB|AND|OR|XOR)Lmodify [off1] {sym1} (LEAQ [off2] {sym2} base) val mem)
1122         && is32Bit(int64(off1)+int64(off2)) && canMergeSym(sym1, sym2) =>
1123         ((ADD|SUB|AND|OR|XOR)Lmodify [off1+off2] {mergeSym(sym1,sym2)} base val mem)
1124
1125 // fold LEAQs together
1126 (LEAQ [off1] {sym1} (LEAQ [off2] {sym2} x)) && is32Bit(int64(off1)+int64(off2)) && canMergeSym(sym1, sym2) =>
1127       (LEAQ [off1+off2] {mergeSym(sym1,sym2)} x)
1128
1129 // LEAQ into LEAQ1
1130 (LEAQ1 [off1] {sym1} (LEAQ [off2] {sym2} x) y) && is32Bit(int64(off1)+int64(off2)) && canMergeSym(sym1, sym2) && x.Op != OpSB =>
1131        (LEAQ1 [off1+off2] {mergeSym(sym1,sym2)} x y)
1132
1133 // LEAQ1 into LEAQ
1134 (LEAQ [off1] {sym1} (LEAQ1 [off2] {sym2} x y)) && is32Bit(int64(off1)+int64(off2)) && canMergeSym(sym1, sym2) =>
1135        (LEAQ1 [off1+off2] {mergeSym(sym1,sym2)} x y)
1136
1137 // LEAQ into LEAQ[248]
1138 (LEAQ2 [off1] {sym1} (LEAQ [off2] {sym2} x) y) && is32Bit(int64(off1)+int64(off2)) && canMergeSym(sym1, sym2) && x.Op != OpSB =>
1139        (LEAQ2 [off1+off2] {mergeSym(sym1,sym2)} x y)
1140 (LEAQ4 [off1] {sym1} (LEAQ [off2] {sym2} x) y) && is32Bit(int64(off1)+int64(off2)) && canMergeSym(sym1, sym2) && x.Op != OpSB =>
1141        (LEAQ4 [off1+off2] {mergeSym(sym1,sym2)} x y)
1142 (LEAQ8 [off1] {sym1} (LEAQ [off2] {sym2} x) y) && is32Bit(int64(off1)+int64(off2)) && canMergeSym(sym1, sym2) && x.Op != OpSB =>
1143        (LEAQ8 [off1+off2] {mergeSym(sym1,sym2)} x y)
1144
1145 // LEAQ[248] into LEAQ
1146 (LEAQ [off1] {sym1} (LEAQ2 [off2] {sym2} x y)) && is32Bit(int64(off1)+int64(off2)) && canMergeSym(sym1, sym2) =>
1147       (LEAQ2 [off1+off2] {mergeSym(sym1,sym2)} x y)
1148 (LEAQ [off1] {sym1} (LEAQ4 [off2] {sym2} x y)) && is32Bit(int64(off1)+int64(off2)) && canMergeSym(sym1, sym2) =>
1149       (LEAQ4 [off1+off2] {mergeSym(sym1,sym2)} x y)
1150 (LEAQ [off1] {sym1} (LEAQ8 [off2] {sym2} x y)) && is32Bit(int64(off1)+int64(off2)) && canMergeSym(sym1, sym2) =>
1151       (LEAQ8 [off1+off2] {mergeSym(sym1,sym2)} x y)
1152
1153 // LEAQ[1248] into LEAQ[1248]. Only some such merges are possible.
1154 (LEAQ1 [off1] {sym1} x (LEAQ1 [off2] {sym2} y y)) && is32Bit(int64(off1)+int64(off2)) && canMergeSym(sym1, sym2) =>
1155       (LEAQ2 [off1+off2] {mergeSym(sym1, sym2)} x y)
1156 (LEAQ1 [off1] {sym1} x (LEAQ1 [off2] {sym2} x y)) && is32Bit(int64(off1)+int64(off2)) && canMergeSym(sym1, sym2) =>
1157       (LEAQ2 [off1+off2] {mergeSym(sym1, sym2)} y x)
1158 (LEAQ2 [off1] {sym1} x (LEAQ1 [off2] {sym2} y y)) && is32Bit(int64(off1)+2*int64(off2)) && sym2 == nil =>
1159       (LEAQ4 [off1+2*off2] {sym1} x y)
1160 (LEAQ4 [off1] {sym1} x (LEAQ1 [off2] {sym2} y y)) && is32Bit(int64(off1)+4*int64(off2)) && sym2 == nil =>
1161       (LEAQ8 [off1+4*off2] {sym1} x y)
1162 // TODO: more?
1163
1164 // Lower LEAQ2/4/8 when the offset is a constant
1165 (LEAQ2 [off] {sym} x (MOV(Q|L)const [scale])) && is32Bit(int64(off)+int64(scale)*2) =>
1166         (LEAQ [off+int32(scale)*2] {sym} x)
1167 (LEAQ4 [off] {sym} x (MOV(Q|L)const [scale])) && is32Bit(int64(off)+int64(scale)*4) =>
1168         (LEAQ [off+int32(scale)*4] {sym} x)
1169 (LEAQ8 [off] {sym} x (MOV(Q|L)const [scale])) && is32Bit(int64(off)+int64(scale)*8) =>
1170         (LEAQ [off+int32(scale)*8] {sym} x)
1171
1172 // Absorb InvertFlags into branches.
1173 (LT (InvertFlags cmp) yes no) => (GT cmp yes no)
1174 (GT (InvertFlags cmp) yes no) => (LT cmp yes no)
1175 (LE (InvertFlags cmp) yes no) => (GE cmp yes no)
1176 (GE (InvertFlags cmp) yes no) => (LE cmp yes no)
1177 (ULT (InvertFlags cmp) yes no) => (UGT cmp yes no)
1178 (UGT (InvertFlags cmp) yes no) => (ULT cmp yes no)
1179 (ULE (InvertFlags cmp) yes no) => (UGE cmp yes no)
1180 (UGE (InvertFlags cmp) yes no) => (ULE cmp yes no)
1181 (EQ (InvertFlags cmp) yes no) => (EQ cmp yes no)
1182 (NE (InvertFlags cmp) yes no) => (NE cmp yes no)
1183
1184 // Constant comparisons.
1185 (CMPQconst (MOVQconst [x]) [y]) && x==int64(y) => (FlagEQ)
1186 (CMPQconst (MOVQconst [x]) [y]) && x<int64(y) && uint64(x)<uint64(int64(y)) => (FlagLT_ULT)
1187 (CMPQconst (MOVQconst [x]) [y]) && x<int64(y) && uint64(x)>uint64(int64(y)) => (FlagLT_UGT)
1188 (CMPQconst (MOVQconst [x]) [y]) && x>int64(y) && uint64(x)<uint64(int64(y)) => (FlagGT_ULT)
1189 (CMPQconst (MOVQconst [x]) [y]) && x>int64(y) && uint64(x)>uint64(int64(y)) => (FlagGT_UGT)
1190 (CMPLconst (MOVLconst [x]) [y]) && x==y => (FlagEQ)
1191 (CMPLconst (MOVLconst [x]) [y]) && x<y && uint32(x)<uint32(y) => (FlagLT_ULT)
1192 (CMPLconst (MOVLconst [x]) [y]) && x<y && uint32(x)>uint32(y) => (FlagLT_UGT)
1193 (CMPLconst (MOVLconst [x]) [y]) && x>y && uint32(x)<uint32(y) => (FlagGT_ULT)
1194 (CMPLconst (MOVLconst [x]) [y]) && x>y && uint32(x)>uint32(y) => (FlagGT_UGT)
1195 (CMPWconst (MOVLconst [x]) [y]) && int16(x)==y => (FlagEQ)
1196 (CMPWconst (MOVLconst [x]) [y]) && int16(x)<y && uint16(x)<uint16(y) => (FlagLT_ULT)
1197 (CMPWconst (MOVLconst [x]) [y]) && int16(x)<y && uint16(x)>uint16(y) => (FlagLT_UGT)
1198 (CMPWconst (MOVLconst [x]) [y]) && int16(x)>y && uint16(x)<uint16(y) => (FlagGT_ULT)
1199 (CMPWconst (MOVLconst [x]) [y]) && int16(x)>y && uint16(x)>uint16(y) => (FlagGT_UGT)
1200 (CMPBconst (MOVLconst [x]) [y]) && int8(x)==y => (FlagEQ)
1201 (CMPBconst (MOVLconst [x]) [y]) && int8(x)<y && uint8(x)<uint8(y) => (FlagLT_ULT)
1202 (CMPBconst (MOVLconst [x]) [y]) && int8(x)<y && uint8(x)>uint8(y) => (FlagLT_UGT)
1203 (CMPBconst (MOVLconst [x]) [y]) && int8(x)>y && uint8(x)<uint8(y) => (FlagGT_ULT)
1204 (CMPBconst (MOVLconst [x]) [y]) && int8(x)>y && uint8(x)>uint8(y) => (FlagGT_UGT)
1205
1206 // CMPQconst requires a 32 bit const, but we can still constant-fold 64 bit consts.
1207 // In theory this applies to any of the simplifications above,
1208 // but CMPQ is the only one I've actually seen occur.
1209 (CMPQ (MOVQconst [x]) (MOVQconst [y])) && x==y => (FlagEQ)
1210 (CMPQ (MOVQconst [x]) (MOVQconst [y])) && x<y && uint64(x)<uint64(y) => (FlagLT_ULT)
1211 (CMPQ (MOVQconst [x]) (MOVQconst [y])) && x<y && uint64(x)>uint64(y) => (FlagLT_UGT)
1212 (CMPQ (MOVQconst [x]) (MOVQconst [y])) && x>y && uint64(x)<uint64(y) => (FlagGT_ULT)
1213 (CMPQ (MOVQconst [x]) (MOVQconst [y])) && x>y && uint64(x)>uint64(y) => (FlagGT_UGT)
1214
1215 // Other known comparisons.
1216 (CMPQconst (MOVBQZX _) [c]) && 0xFF < c => (FlagLT_ULT)
1217 (CMPQconst (MOVWQZX _) [c]) && 0xFFFF < c => (FlagLT_ULT)
1218 (CMPLconst (SHRLconst _ [c]) [n]) && 0 <= n && 0 < c && c <= 32 && (1<<uint64(32-c)) <= uint64(n) => (FlagLT_ULT)
1219 (CMPQconst (SHRQconst _ [c]) [n]) && 0 <= n && 0 < c && c <= 64 && (1<<uint64(64-c)) <= uint64(n) => (FlagLT_ULT)
1220 (CMPQconst (ANDQconst _ [m]) [n]) && 0 <= m && m < n => (FlagLT_ULT)
1221 (CMPQconst (ANDLconst _ [m]) [n]) && 0 <= m && m < n => (FlagLT_ULT)
1222 (CMPLconst (ANDLconst _ [m]) [n]) && 0 <= m && m < n => (FlagLT_ULT)
1223 (CMPWconst (ANDLconst _ [m]) [n]) && 0 <= int16(m) && int16(m) < n => (FlagLT_ULT)
1224 (CMPBconst (ANDLconst _ [m]) [n]) && 0 <= int8(m)  && int8(m)  < n => (FlagLT_ULT)
1225
1226 // TESTQ c c sets flags like CMPQ c 0.
1227 (TESTQconst [c] (MOVQconst [d])) && int64(c) == d && c == 0 => (FlagEQ)
1228 (TESTLconst [c] (MOVLconst [c])) && c == 0 => (FlagEQ)
1229 (TESTQconst [c] (MOVQconst [d])) && int64(c) == d && c < 0  => (FlagLT_UGT)
1230 (TESTLconst [c] (MOVLconst [c])) && c < 0  => (FlagLT_UGT)
1231 (TESTQconst [c] (MOVQconst [d])) && int64(c) == d && c > 0  => (FlagGT_UGT)
1232 (TESTLconst [c] (MOVLconst [c])) && c > 0  => (FlagGT_UGT)
1233
1234 // TODO: DIVxU also.
1235
1236 // Absorb flag constants into SBB ops.
1237 (SBBQcarrymask (FlagEQ))     => (MOVQconst [0])
1238 (SBBQcarrymask (FlagLT_ULT)) => (MOVQconst [-1])
1239 (SBBQcarrymask (FlagLT_UGT)) => (MOVQconst [0])
1240 (SBBQcarrymask (FlagGT_ULT)) => (MOVQconst [-1])
1241 (SBBQcarrymask (FlagGT_UGT)) => (MOVQconst [0])
1242 (SBBLcarrymask (FlagEQ))     => (MOVLconst [0])
1243 (SBBLcarrymask (FlagLT_ULT)) => (MOVLconst [-1])
1244 (SBBLcarrymask (FlagLT_UGT)) => (MOVLconst [0])
1245 (SBBLcarrymask (FlagGT_ULT)) => (MOVLconst [-1])
1246 (SBBLcarrymask (FlagGT_UGT)) => (MOVLconst [0])
1247
1248 // Absorb flag constants into branches.
1249 ((EQ|LE|GE|ULE|UGE) (FlagEQ) yes no)     => (First yes no)
1250 ((NE|LT|GT|ULT|UGT) (FlagEQ) yes no)     => (First no yes)
1251 ((NE|LT|LE|ULT|ULE) (FlagLT_ULT) yes no) => (First yes no)
1252 ((EQ|GT|GE|UGT|UGE) (FlagLT_ULT) yes no) => (First no yes)
1253 ((NE|LT|LE|UGT|UGE) (FlagLT_UGT) yes no) => (First yes no)
1254 ((EQ|GT|GE|ULT|ULE) (FlagLT_UGT) yes no) => (First no yes)
1255 ((NE|GT|GE|ULT|ULE) (FlagGT_ULT) yes no) => (First yes no)
1256 ((EQ|LT|LE|UGT|UGE) (FlagGT_ULT) yes no) => (First no yes)
1257 ((NE|GT|GE|UGT|UGE) (FlagGT_UGT) yes no) => (First yes no)
1258 ((EQ|LT|LE|ULT|ULE) (FlagGT_UGT) yes no) => (First no yes)
1259
1260 // Absorb flag constants into SETxx ops.
1261 ((SETEQ|SETLE|SETGE|SETBE|SETAE) (FlagEQ))     => (MOVLconst [1])
1262 ((SETNE|SETL|SETG|SETB|SETA)     (FlagEQ))     => (MOVLconst [0])
1263 ((SETNE|SETL|SETLE|SETB|SETBE)   (FlagLT_ULT)) => (MOVLconst [1])
1264 ((SETEQ|SETG|SETGE|SETA|SETAE)   (FlagLT_ULT)) => (MOVLconst [0])
1265 ((SETNE|SETL|SETLE|SETA|SETAE)   (FlagLT_UGT)) => (MOVLconst [1])
1266 ((SETEQ|SETG|SETGE|SETB|SETBE)   (FlagLT_UGT)) => (MOVLconst [0])
1267 ((SETNE|SETG|SETGE|SETB|SETBE)   (FlagGT_ULT)) => (MOVLconst [1])
1268 ((SETEQ|SETL|SETLE|SETA|SETAE)   (FlagGT_ULT)) => (MOVLconst [0])
1269 ((SETNE|SETG|SETGE|SETA|SETAE)   (FlagGT_UGT)) => (MOVLconst [1])
1270 ((SETEQ|SETL|SETLE|SETB|SETBE)   (FlagGT_UGT)) => (MOVLconst [0])
1271
1272 (SETEQstore [off] {sym} ptr (FlagEQ)     mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [1]) mem)
1273 (SETEQstore [off] {sym} ptr (FlagLT_ULT) mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [0]) mem)
1274 (SETEQstore [off] {sym} ptr (FlagLT_UGT) mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [0]) mem)
1275 (SETEQstore [off] {sym} ptr (FlagGT_ULT) mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [0]) mem)
1276 (SETEQstore [off] {sym} ptr (FlagGT_UGT) mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [0]) mem)
1277
1278 (SETNEstore [off] {sym} ptr (FlagEQ)     mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [0]) mem)
1279 (SETNEstore [off] {sym} ptr (FlagLT_ULT) mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [1]) mem)
1280 (SETNEstore [off] {sym} ptr (FlagLT_UGT) mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [1]) mem)
1281 (SETNEstore [off] {sym} ptr (FlagGT_ULT) mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [1]) mem)
1282 (SETNEstore [off] {sym} ptr (FlagGT_UGT) mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [1]) mem)
1283
1284 (SETLstore  [off] {sym} ptr (FlagEQ)     mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [0]) mem)
1285 (SETLstore  [off] {sym} ptr (FlagLT_ULT) mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [1]) mem)
1286 (SETLstore  [off] {sym} ptr (FlagLT_UGT) mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [1]) mem)
1287 (SETLstore  [off] {sym} ptr (FlagGT_ULT) mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [0]) mem)
1288 (SETLstore  [off] {sym} ptr (FlagGT_UGT) mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [0]) mem)
1289
1290 (SETLEstore [off] {sym} ptr (FlagEQ)     mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [1]) mem)
1291 (SETLEstore [off] {sym} ptr (FlagLT_ULT) mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [1]) mem)
1292 (SETLEstore [off] {sym} ptr (FlagLT_UGT) mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [1]) mem)
1293 (SETLEstore [off] {sym} ptr (FlagGT_ULT) mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [0]) mem)
1294 (SETLEstore [off] {sym} ptr (FlagGT_UGT) mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [0]) mem)
1295
1296 (SETGstore  [off] {sym} ptr (FlagEQ)     mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [0]) mem)
1297 (SETGstore  [off] {sym} ptr (FlagLT_ULT) mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [0]) mem)
1298 (SETGstore  [off] {sym} ptr (FlagLT_UGT) mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [0]) mem)
1299 (SETGstore  [off] {sym} ptr (FlagGT_ULT) mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [1]) mem)
1300 (SETGstore  [off] {sym} ptr (FlagGT_UGT) mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [1]) mem)
1301
1302 (SETGEstore [off] {sym} ptr (FlagEQ)     mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [1]) mem)
1303 (SETGEstore [off] {sym} ptr (FlagLT_ULT) mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [0]) mem)
1304 (SETGEstore [off] {sym} ptr (FlagLT_UGT) mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [0]) mem)
1305 (SETGEstore [off] {sym} ptr (FlagGT_ULT) mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [1]) mem)
1306 (SETGEstore [off] {sym} ptr (FlagGT_UGT) mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [1]) mem)
1307
1308 (SETBstore  [off] {sym} ptr (FlagEQ)     mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [0]) mem)
1309 (SETBstore  [off] {sym} ptr (FlagLT_ULT) mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [1]) mem)
1310 (SETBstore  [off] {sym} ptr (FlagLT_UGT) mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [0]) mem)
1311 (SETBstore  [off] {sym} ptr (FlagGT_ULT) mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [1]) mem)
1312 (SETBstore  [off] {sym} ptr (FlagGT_UGT) mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [0]) mem)
1313
1314 (SETBEstore [off] {sym} ptr (FlagEQ)     mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [1]) mem)
1315 (SETBEstore [off] {sym} ptr (FlagLT_ULT) mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [1]) mem)
1316 (SETBEstore [off] {sym} ptr (FlagLT_UGT) mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [0]) mem)
1317 (SETBEstore [off] {sym} ptr (FlagGT_ULT) mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [1]) mem)
1318 (SETBEstore [off] {sym} ptr (FlagGT_UGT) mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [0]) mem)
1319
1320 (SETAstore  [off] {sym} ptr (FlagEQ)     mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [0]) mem)
1321 (SETAstore  [off] {sym} ptr (FlagLT_ULT) mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [0]) mem)
1322 (SETAstore  [off] {sym} ptr (FlagLT_UGT) mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [1]) mem)
1323 (SETAstore  [off] {sym} ptr (FlagGT_ULT) mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [0]) mem)
1324 (SETAstore  [off] {sym} ptr (FlagGT_UGT) mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [1]) mem)
1325
1326 (SETAEstore [off] {sym} ptr (FlagEQ)     mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [1]) mem)
1327 (SETAEstore [off] {sym} ptr (FlagLT_ULT) mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [0]) mem)
1328 (SETAEstore [off] {sym} ptr (FlagLT_UGT) mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [1]) mem)
1329 (SETAEstore [off] {sym} ptr (FlagGT_ULT) mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [0]) mem)
1330 (SETAEstore [off] {sym} ptr (FlagGT_UGT) mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [1]) mem)
1331
1332 // Remove redundant *const ops
1333 (ADDQconst [0] x)          => x
1334 (ADDLconst [c] x) && c==0  => x
1335 (SUBQconst [0] x)          => x
1336 (SUBLconst [c] x) && c==0  => x
1337 (ANDQconst [0] _)          => (MOVQconst [0])
1338 (ANDLconst [c] _) && c==0  => (MOVLconst [0])
1339 (ANDQconst [-1] x)         => x
1340 (ANDLconst [c] x) && c==-1 => x
1341 (ORQconst [0] x)           => x
1342 (ORLconst [c] x)  && c==0  => x
1343 (ORQconst [-1] _)          => (MOVQconst [-1])
1344 (ORLconst [c] _)  && c==-1 => (MOVLconst [-1])
1345 (XORQconst [0] x)          => x
1346 (XORLconst [c] x) && c==0  => x
1347 // TODO: since we got rid of the W/B versions, we might miss
1348 // things like (ANDLconst [0x100] x) which were formerly
1349 // (ANDBconst [0] x).  Probably doesn't happen very often.
1350 // If we cared, we might do:
1351 //  (ANDLconst <t> [c] x) && t.Size()==1 && int8(x)==0 -> (MOVLconst [0])
1352
1353 // Remove redundant ops
1354 // Not in generic rules, because they may appear after lowering e. g. Slicemask
1355 (NEG(Q|L) (NEG(Q|L) x)) => x
1356 (NEG(Q|L) s:(SUB(Q|L) x y)) && s.Uses == 1 => (SUB(Q|L) y x)
1357
1358 // Convert constant subtracts to constant adds
1359 (SUBQconst [c] x) && c != -(1<<31) => (ADDQconst [-c] x)
1360 (SUBLconst [c] x) => (ADDLconst [-c] x)
1361
1362 // generic constant folding
1363 // TODO: more of this
1364 (ADDQconst [c] (MOVQconst [d])) => (MOVQconst [int64(c)+d])
1365 (ADDLconst [c] (MOVLconst [d])) => (MOVLconst [c+d])
1366 (ADDQconst [c] (ADDQconst [d] x)) && is32Bit(int64(c)+int64(d)) => (ADDQconst [c+d] x)
1367 (ADDLconst [c] (ADDLconst [d] x)) => (ADDLconst [c+d] x)
1368 (SUBQconst (MOVQconst [d]) [c]) => (MOVQconst [d-int64(c)])
1369 (SUBQconst (SUBQconst x [d]) [c]) && is32Bit(int64(-c)-int64(d)) => (ADDQconst [-c-d] x)
1370 (SARQconst [c] (MOVQconst [d])) => (MOVQconst [d>>uint64(c)])
1371 (SARLconst [c] (MOVQconst [d])) => (MOVQconst [int64(int32(d))>>uint64(c)])
1372 (SARWconst [c] (MOVQconst [d])) => (MOVQconst [int64(int16(d))>>uint64(c)])
1373 (SARBconst [c] (MOVQconst [d])) => (MOVQconst [int64(int8(d))>>uint64(c)])
1374 (NEGQ (MOVQconst [c])) => (MOVQconst [-c])
1375 (NEGL (MOVLconst [c])) => (MOVLconst [-c])
1376 (MULQconst [c] (MOVQconst [d])) => (MOVQconst [int64(c)*d])
1377 (MULLconst [c] (MOVLconst [d])) => (MOVLconst [c*d])
1378 (ANDQconst [c] (MOVQconst [d])) => (MOVQconst [int64(c)&d])
1379 (ANDLconst [c] (MOVLconst [d])) => (MOVLconst [c&d])
1380 (ORQconst [c] (MOVQconst [d])) => (MOVQconst [int64(c)|d])
1381 (ORLconst [c] (MOVLconst [d])) => (MOVLconst [c|d])
1382 (XORQconst [c] (MOVQconst [d])) => (MOVQconst [int64(c)^d])
1383 (XORLconst [c] (MOVLconst [d])) => (MOVLconst [c^d])
1384 (NOTQ (MOVQconst [c])) => (MOVQconst [^c])
1385 (NOTL (MOVLconst [c])) => (MOVLconst [^c])
1386 (BTSQconst [c] (MOVQconst [d])) => (MOVQconst [d|(1<<uint32(c))])
1387 (BTRQconst [c] (MOVQconst [d])) => (MOVQconst [d&^(1<<uint32(c))])
1388 (BTCQconst [c] (MOVQconst [d])) => (MOVQconst [d^(1<<uint32(c))])
1389
1390 // If c or d doesn't fit into 32 bits, then we can't construct ORQconst,
1391 // but we can still constant-fold.
1392 // In theory this applies to any of the simplifications above,
1393 // but ORQ is the only one I've actually seen occur.
1394 (ORQ (MOVQconst [c]) (MOVQconst [d])) => (MOVQconst [c|d])
1395
1396 // generic simplifications
1397 // TODO: more of this
1398 (ADDQ x (NEGQ y)) => (SUBQ x y)
1399 (ADDL x (NEGL y)) => (SUBL x y)
1400 (SUBQ x x) => (MOVQconst [0])
1401 (SUBL x x) => (MOVLconst [0])
1402 (ANDQ x x) => x
1403 (ANDL x x) => x
1404 (ORQ x x)  => x
1405 (ORL x x)  => x
1406 (XORQ x x) => (MOVQconst [0])
1407 (XORL x x) => (MOVLconst [0])
1408
1409 (SHLLconst [d] (MOVLconst [c])) => (MOVLconst [c << uint64(d)])
1410 (SHLQconst [d] (MOVQconst [c])) => (MOVQconst [c << uint64(d)])
1411 (SHLQconst [d] (MOVLconst [c])) => (MOVQconst [int64(c) << uint64(d)])
1412
1413 // Fold NEG into ADDconst/MULconst. Take care to keep c in 32 bit range.
1414 (NEGQ (ADDQconst [c] (NEGQ x))) && c != -(1<<31) => (ADDQconst [-c] x)
1415 (MULQconst [c] (NEGQ x)) && c != -(1<<31) => (MULQconst [-c] x)
1416
1417 // checking AND against 0.
1418 (CMPQconst a:(ANDQ x y) [0]) && a.Uses == 1 => (TESTQ x y)
1419 (CMPLconst a:(ANDL x y) [0]) && a.Uses == 1 => (TESTL x y)
1420 (CMPWconst a:(ANDL x y) [0]) && a.Uses == 1 => (TESTW x y)
1421 (CMPBconst a:(ANDL x y) [0]) && a.Uses == 1 => (TESTB x y)
1422 (CMPQconst a:(ANDQconst [c] x) [0]) && a.Uses == 1 => (TESTQconst [c] x)
1423 (CMPLconst a:(ANDLconst [c] x) [0]) && a.Uses == 1 => (TESTLconst [c] x)
1424 (CMPWconst a:(ANDLconst [c] x) [0]) && a.Uses == 1 => (TESTWconst [int16(c)] x)
1425 (CMPBconst a:(ANDLconst [c] x) [0]) && a.Uses == 1 => (TESTBconst [int8(c)] x)
1426
1427 // Convert TESTx to TESTxconst if possible.
1428 (TESTQ (MOVQconst [c]) x) && is32Bit(c) => (TESTQconst [int32(c)] x)
1429 (TESTL (MOVLconst [c]) x) => (TESTLconst [c] x)
1430 (TESTW (MOVLconst [c]) x) => (TESTWconst [int16(c)] x)
1431 (TESTB (MOVLconst [c]) x) => (TESTBconst [int8(c)] x)
1432
1433 // TEST %reg,%reg is shorter than CMP
1434 (CMPQconst x [0]) => (TESTQ x x)
1435 (CMPLconst x [0]) => (TESTL x x)
1436 (CMPWconst x [0]) => (TESTW x x)
1437 (CMPBconst x [0]) => (TESTB x x)
1438 (TESTQconst [-1] x) && x.Op != OpAMD64MOVQconst => (TESTQ x x)
1439 (TESTLconst [-1] x) && x.Op != OpAMD64MOVLconst => (TESTL x x)
1440 (TESTWconst [-1] x) && x.Op != OpAMD64MOVLconst => (TESTW x x)
1441 (TESTBconst [-1] x) && x.Op != OpAMD64MOVLconst => (TESTB x x)
1442
1443 // Convert LEAQ1 back to ADDQ if we can
1444 (LEAQ1 [0] x y) && v.Aux == nil => (ADDQ x y)
1445
1446 (MOVQstoreconst [c] {s} p1 x:(MOVQstoreconst [a] {s} p0 mem))
1447   && config.useSSE
1448   && x.Uses == 1
1449   && sequentialAddresses(p0, p1, int64(a.Off()+8-c.Off()))
1450   && a.Val() == 0
1451   && c.Val() == 0
1452   && clobber(x)
1453   => (MOVOstoreconst [makeValAndOff(0,a.Off())] {s} p0 mem)
1454 (MOVQstoreconst [a] {s} p0 x:(MOVQstoreconst [c] {s} p1 mem))
1455   && config.useSSE
1456   && x.Uses == 1
1457   && sequentialAddresses(p0, p1, int64(a.Off()+8-c.Off()))
1458   && a.Val() == 0
1459   && c.Val() == 0
1460   && clobber(x)
1461   => (MOVOstoreconst [makeValAndOff(0,a.Off())] {s} p0 mem)
1462
1463 // Merge load and op
1464 // TODO: add indexed variants?
1465 ((ADD|SUB|AND|OR|XOR)Q x l:(MOVQload [off] {sym} ptr mem)) && canMergeLoadClobber(v, l, x) && clobber(l) => ((ADD|SUB|AND|OR|XOR)Qload x [off] {sym} ptr mem)
1466 ((ADD|SUB|AND|OR|XOR)L x l:(MOVLload [off] {sym} ptr mem)) && canMergeLoadClobber(v, l, x) && clobber(l) => ((ADD|SUB|AND|OR|XOR)Lload x [off] {sym} ptr mem)
1467 ((ADD|SUB|MUL|DIV)SD x l:(MOVSDload [off] {sym} ptr mem)) && canMergeLoadClobber(v, l, x) && clobber(l) => ((ADD|SUB|MUL|DIV)SDload x [off] {sym} ptr mem)
1468 ((ADD|SUB|MUL|DIV)SS x l:(MOVSSload [off] {sym} ptr mem)) && canMergeLoadClobber(v, l, x) && clobber(l) => ((ADD|SUB|MUL|DIV)SSload x [off] {sym} ptr mem)
1469 (MOVLstore {sym} [off] ptr y:((ADD|AND|OR|XOR)Lload x [off] {sym} ptr mem) mem) && y.Uses==1 && clobber(y) => ((ADD|AND|OR|XOR)Lmodify [off] {sym} ptr x mem)
1470 (MOVLstore {sym} [off] ptr y:((ADD|SUB|AND|OR|XOR)L l:(MOVLload [off] {sym} ptr mem) x) mem) && y.Uses==1 && l.Uses==1 && clobber(y, l) =>
1471         ((ADD|SUB|AND|OR|XOR)Lmodify [off] {sym} ptr x mem)
1472 (MOVQstore {sym} [off] ptr y:((ADD|AND|OR|XOR)Qload x [off] {sym} ptr mem) mem) && y.Uses==1 && clobber(y) => ((ADD|AND|OR|XOR)Qmodify [off] {sym} ptr x mem)
1473 (MOVQstore {sym} [off] ptr y:((ADD|SUB|AND|OR|XOR)Q l:(MOVQload [off] {sym} ptr mem) x) mem) && y.Uses==1 && l.Uses==1 && clobber(y, l) =>
1474         ((ADD|SUB|AND|OR|XOR)Qmodify [off] {sym} ptr x mem)
1475 (MOVQstore {sym} [off] ptr x:(BT(S|R|C)Qconst [c] l:(MOVQload {sym} [off] ptr mem)) mem) && x.Uses == 1 && l.Uses == 1 && clobber(x, l) =>
1476         (BT(S|R|C)Qconstmodify {sym} [makeValAndOff(int32(c),off)] ptr mem)
1477
1478 // Merge ADDQconst and LEAQ into atomic loads.
1479 (MOV(Q|L|B)atomicload [off1] {sym} (ADDQconst [off2] ptr) mem) && is32Bit(int64(off1)+int64(off2)) =>
1480         (MOV(Q|L|B)atomicload [off1+off2] {sym} ptr mem)
1481 (MOV(Q|L|B)atomicload [off1] {sym1} (LEAQ [off2] {sym2} ptr) mem) && is32Bit(int64(off1)+int64(off2)) && canMergeSym(sym1, sym2) =>
1482         (MOV(Q|L|B)atomicload [off1+off2] {mergeSym(sym1, sym2)} ptr mem)
1483
1484 // Merge ADDQconst and LEAQ into atomic stores.
1485 (XCHGQ [off1] {sym} val (ADDQconst [off2] ptr) mem) && is32Bit(int64(off1)+int64(off2)) =>
1486         (XCHGQ [off1+off2] {sym} val ptr mem)
1487 (XCHGQ [off1] {sym1} val (LEAQ [off2] {sym2} ptr) mem) && is32Bit(int64(off1)+int64(off2)) && canMergeSym(sym1, sym2) && ptr.Op != OpSB =>
1488         (XCHGQ [off1+off2] {mergeSym(sym1,sym2)} val ptr mem)
1489 (XCHGL [off1] {sym} val (ADDQconst [off2] ptr) mem) && is32Bit(int64(off1)+int64(off2)) =>
1490         (XCHGL [off1+off2] {sym} val ptr mem)
1491 (XCHGL [off1] {sym1} val (LEAQ [off2] {sym2} ptr) mem) && is32Bit(int64(off1)+int64(off2)) && canMergeSym(sym1, sym2) && ptr.Op != OpSB =>
1492         (XCHGL [off1+off2] {mergeSym(sym1,sym2)} val ptr mem)
1493
1494 // Merge ADDQconst into atomic adds.
1495 // TODO: merging LEAQ doesn't work, assembler doesn't like the resulting instructions.
1496 (XADDQlock [off1] {sym} val (ADDQconst [off2] ptr) mem) && is32Bit(int64(off1)+int64(off2)) =>
1497         (XADDQlock [off1+off2] {sym} val ptr mem)
1498 (XADDLlock [off1] {sym} val (ADDQconst [off2] ptr) mem) && is32Bit(int64(off1)+int64(off2)) =>
1499         (XADDLlock [off1+off2] {sym} val ptr mem)
1500
1501 // Merge ADDQconst into atomic compare and swaps.
1502 // TODO: merging LEAQ doesn't work, assembler doesn't like the resulting instructions.
1503 (CMPXCHGQlock [off1] {sym} (ADDQconst [off2] ptr) old new_ mem) && is32Bit(int64(off1)+int64(off2)) =>
1504         (CMPXCHGQlock [off1+off2] {sym} ptr old new_ mem)
1505 (CMPXCHGLlock [off1] {sym} (ADDQconst [off2] ptr) old new_ mem) && is32Bit(int64(off1)+int64(off2)) =>
1506         (CMPXCHGLlock [off1+off2] {sym} ptr old new_ mem)
1507
1508 // We don't need the conditional move if we know the arg of BSF is not zero.
1509 (CMOVQEQ x _ (Select1 (BS(F|R)Q (ORQconst [c] _)))) && c != 0 => x
1510 // Extension is unnecessary for trailing zeros.
1511 (BSFQ (ORQconst <t> [1<<8] (MOVBQZX x))) => (BSFQ (ORQconst <t> [1<<8] x))
1512 (BSFQ (ORQconst <t> [1<<16] (MOVWQZX x))) => (BSFQ (ORQconst <t> [1<<16] x))
1513
1514 // Redundant sign/zero extensions
1515 // Note: see issue 21963. We have to make sure we use the right type on
1516 // the resulting extension (the outer type, not the inner type).
1517 (MOVLQSX (MOVLQSX x)) => (MOVLQSX x)
1518 (MOVLQSX (MOVWQSX x)) => (MOVWQSX x)
1519 (MOVLQSX (MOVBQSX x)) => (MOVBQSX x)
1520 (MOVWQSX (MOVWQSX x)) => (MOVWQSX x)
1521 (MOVWQSX (MOVBQSX x)) => (MOVBQSX x)
1522 (MOVBQSX (MOVBQSX x)) => (MOVBQSX x)
1523 (MOVLQZX (MOVLQZX x)) => (MOVLQZX x)
1524 (MOVLQZX (MOVWQZX x)) => (MOVWQZX x)
1525 (MOVLQZX (MOVBQZX x)) => (MOVBQZX x)
1526 (MOVWQZX (MOVWQZX x)) => (MOVWQZX x)
1527 (MOVWQZX (MOVBQZX x)) => (MOVBQZX x)
1528 (MOVBQZX (MOVBQZX x)) => (MOVBQZX x)
1529
1530 (MOVQstore [off] {sym} ptr a:((ADD|AND|OR|XOR)Qconst [c] l:(MOVQload [off] {sym} ptr2 mem)) mem)
1531         && isSamePtr(ptr, ptr2) && a.Uses == 1 && l.Uses == 1 && clobber(l, a) =>
1532         ((ADD|AND|OR|XOR)Qconstmodify {sym} [makeValAndOff(int32(c),off)] ptr mem)
1533 (MOVLstore [off] {sym} ptr a:((ADD|AND|OR|XOR)Lconst [c] l:(MOVLload [off] {sym} ptr2 mem)) mem)
1534         && isSamePtr(ptr, ptr2) && a.Uses == 1 && l.Uses == 1 && clobber(l, a) =>
1535         ((ADD|AND|OR|XOR)Lconstmodify {sym} [makeValAndOff(int32(c),off)] ptr mem)
1536
1537 // float <-> int register moves, with no conversion.
1538 // These come up when compiling math.{Float{32,64}bits,Float{32,64}frombits}.
1539 (MOVQload  [off] {sym} ptr (MOVSDstore [off] {sym} ptr val _)) => (MOVQf2i val)
1540 (MOVLload  [off] {sym} ptr (MOVSSstore [off] {sym} ptr val _)) => (MOVLf2i val)
1541 (MOVSDload [off] {sym} ptr (MOVQstore  [off] {sym} ptr val _)) => (MOVQi2f val)
1542 (MOVSSload [off] {sym} ptr (MOVLstore  [off] {sym} ptr val _)) => (MOVLi2f val)
1543
1544 // Other load-like ops.
1545 (ADDQload x [off] {sym} ptr (MOVSDstore [off] {sym} ptr y _)) => (ADDQ x (MOVQf2i y))
1546 (ADDLload x [off] {sym} ptr (MOVSSstore [off] {sym} ptr y _)) => (ADDL x (MOVLf2i y))
1547 (SUBQload x [off] {sym} ptr (MOVSDstore [off] {sym} ptr y _)) => (SUBQ x (MOVQf2i y))
1548 (SUBLload x [off] {sym} ptr (MOVSSstore [off] {sym} ptr y _)) => (SUBL x (MOVLf2i y))
1549 (ANDQload x [off] {sym} ptr (MOVSDstore [off] {sym} ptr y _)) => (ANDQ x (MOVQf2i y))
1550 (ANDLload x [off] {sym} ptr (MOVSSstore [off] {sym} ptr y _)) => (ANDL x (MOVLf2i y))
1551 ( ORQload x [off] {sym} ptr (MOVSDstore [off] {sym} ptr y _)) => ( ORQ x (MOVQf2i y))
1552 ( ORLload x [off] {sym} ptr (MOVSSstore [off] {sym} ptr y _)) => ( ORL x (MOVLf2i y))
1553 (XORQload x [off] {sym} ptr (MOVSDstore [off] {sym} ptr y _)) => (XORQ x (MOVQf2i y))
1554 (XORLload x [off] {sym} ptr (MOVSSstore [off] {sym} ptr y _)) => (XORL x (MOVLf2i y))
1555
1556 (ADDSDload x [off] {sym} ptr (MOVQstore [off] {sym} ptr y _)) => (ADDSD x (MOVQi2f y))
1557 (ADDSSload x [off] {sym} ptr (MOVLstore [off] {sym} ptr y _)) => (ADDSS x (MOVLi2f y))
1558 (SUBSDload x [off] {sym} ptr (MOVQstore [off] {sym} ptr y _)) => (SUBSD x (MOVQi2f y))
1559 (SUBSSload x [off] {sym} ptr (MOVLstore [off] {sym} ptr y _)) => (SUBSS x (MOVLi2f y))
1560 (MULSDload x [off] {sym} ptr (MOVQstore [off] {sym} ptr y _)) => (MULSD x (MOVQi2f y))
1561 (MULSSload x [off] {sym} ptr (MOVLstore [off] {sym} ptr y _)) => (MULSS x (MOVLi2f y))
1562
1563 // Redirect stores to use the other register set.
1564 (MOVQstore  [off] {sym} ptr (MOVQf2i val) mem) => (MOVSDstore [off] {sym} ptr val mem)
1565 (MOVLstore  [off] {sym} ptr (MOVLf2i val) mem) => (MOVSSstore [off] {sym} ptr val mem)
1566 (MOVSDstore [off] {sym} ptr (MOVQi2f val) mem) => (MOVQstore  [off] {sym} ptr val mem)
1567 (MOVSSstore [off] {sym} ptr (MOVLi2f val) mem) => (MOVLstore  [off] {sym} ptr val mem)
1568
1569 // Load args directly into the register class where it will be used.
1570 // We do this by just modifying the type of the Arg.
1571 (MOVQf2i <t> (Arg <u> [off] {sym})) && t.Size() == u.Size() => @b.Func.Entry (Arg <t> [off] {sym})
1572 (MOVLf2i <t> (Arg <u> [off] {sym})) && t.Size() == u.Size() => @b.Func.Entry (Arg <t> [off] {sym})
1573 (MOVQi2f <t> (Arg <u> [off] {sym})) && t.Size() == u.Size() => @b.Func.Entry (Arg <t> [off] {sym})
1574 (MOVLi2f <t> (Arg <u> [off] {sym})) && t.Size() == u.Size() => @b.Func.Entry (Arg <t> [off] {sym})
1575
1576 // LEAQ is rematerializeable, so this helps to avoid register spill.
1577 // See issue 22947 for details
1578 (ADD(Q|L)const [off] x:(SP)) => (LEA(Q|L) [off] x)
1579
1580 // HMULx is commutative, but its first argument must go in AX.
1581 // If possible, put a rematerializeable value in the first argument slot,
1582 // to reduce the odds that another value will be have to spilled
1583 // specifically to free up AX.
1584 (HMUL(Q|L)  x y) && !x.rematerializeable() && y.rematerializeable() => (HMUL(Q|L)  y x)
1585 (HMUL(Q|L)U x y) && !x.rematerializeable() && y.rematerializeable() => (HMUL(Q|L)U y x)
1586
1587 // Fold loads into compares
1588 // Note: these may be undone by the flagalloc pass.
1589 (CMP(Q|L|W|B) l:(MOV(Q|L|W|B)load {sym} [off] ptr mem) x) && canMergeLoad(v, l) && clobber(l) => (CMP(Q|L|W|B)load {sym} [off] ptr x mem)
1590 (CMP(Q|L|W|B) x l:(MOV(Q|L|W|B)load {sym} [off] ptr mem)) && canMergeLoad(v, l) && clobber(l) => (InvertFlags (CMP(Q|L|W|B)load {sym} [off] ptr x mem))
1591
1592 (CMP(Q|L)const l:(MOV(Q|L)load {sym} [off] ptr mem) [c])
1593         && l.Uses == 1
1594         && clobber(l) =>
1595 @l.Block (CMP(Q|L)constload {sym} [makeValAndOff(c,off)] ptr mem)
1596 (CMP(W|B)const l:(MOV(W|B)load {sym} [off] ptr mem) [c])
1597         && l.Uses == 1
1598         && clobber(l) =>
1599 @l.Block (CMP(W|B)constload {sym} [makeValAndOff(int32(c),off)] ptr mem)
1600
1601 (CMPQload {sym} [off] ptr (MOVQconst [c]) mem) && validVal(c) => (CMPQconstload {sym} [makeValAndOff(int32(c),off)] ptr mem)
1602 (CMPLload {sym} [off] ptr (MOVLconst [c]) mem) => (CMPLconstload {sym} [makeValAndOff(c,off)] ptr mem)
1603 (CMPWload {sym} [off] ptr (MOVLconst [c]) mem) => (CMPWconstload {sym} [makeValAndOff(int32(int16(c)),off)] ptr mem)
1604 (CMPBload {sym} [off] ptr (MOVLconst [c]) mem) => (CMPBconstload {sym} [makeValAndOff(int32(int8(c)),off)] ptr mem)
1605
1606 (TEST(Q|L|W|B)  l:(MOV(Q|L|W|B)load {sym} [off] ptr mem) l2)
1607         && l == l2
1608         && l.Uses == 2
1609         && clobber(l) =>
1610   @l.Block (CMP(Q|L|W|B)constload {sym} [makeValAndOff(0, off)] ptr mem)
1611
1612 // Convert ANDload to MOVload when we can do the AND in a containing TEST op.
1613 // Only do when it's within the same block, so we don't have flags live across basic block boundaries.
1614 // See issue 44228.
1615 (TEST(Q|L) a:(AND(Q|L)load [off] {sym} x ptr mem) a) && a.Uses == 2 && a.Block == v.Block && clobber(a) => (TEST(Q|L) (MOV(Q|L)load <a.Type> [off] {sym} ptr mem) x)
1616
1617 (MOVBload [off] {sym} (SB) _) && symIsRO(sym) => (MOVLconst [int32(read8(sym, int64(off)))])
1618 (MOVWload [off] {sym} (SB) _) && symIsRO(sym) => (MOVLconst [int32(read16(sym, int64(off), config.ctxt.Arch.ByteOrder))])
1619 (MOVLload [off] {sym} (SB) _) && symIsRO(sym) => (MOVQconst [int64(read32(sym, int64(off), config.ctxt.Arch.ByteOrder))])
1620 (MOVQload [off] {sym} (SB) _) && symIsRO(sym) => (MOVQconst [int64(read64(sym, int64(off), config.ctxt.Arch.ByteOrder))])
1621 (MOVOstore [dstOff] {dstSym} ptr (MOVOload [srcOff] {srcSym} (SB) _) mem) && symIsRO(srcSym) =>
1622   (MOVQstore [dstOff+8] {dstSym} ptr (MOVQconst [int64(read64(srcSym, int64(srcOff)+8, config.ctxt.Arch.ByteOrder))])
1623     (MOVQstore [dstOff] {dstSym} ptr (MOVQconst [int64(read64(srcSym, int64(srcOff), config.ctxt.Arch.ByteOrder))]) mem))
1624
1625 // Arch-specific inlining for small or disjoint runtime.memmove
1626 // Match post-lowering calls, memory version.
1627 (SelectN [0] call:(CALLstatic {sym} s1:(MOVQstoreconst _ [sc] s2:(MOVQstore _ src s3:(MOVQstore _ dst mem)))))
1628         && sc.Val64() >= 0
1629         && isSameCall(sym, "runtime.memmove")
1630         && s1.Uses == 1 && s2.Uses == 1 && s3.Uses == 1
1631         && isInlinableMemmove(dst, src, sc.Val64(), config)
1632         && clobber(s1, s2, s3, call)
1633         => (Move [sc.Val64()] dst src mem)
1634
1635 // Match post-lowering calls, register version.
1636 (SelectN [0] call:(CALLstatic {sym} dst src (MOVQconst [sz]) mem))
1637         && sz >= 0
1638         && isSameCall(sym, "runtime.memmove")
1639         && call.Uses == 1
1640         && isInlinableMemmove(dst, src, sz, config)
1641         && clobber(call)
1642         => (Move [sz] dst src mem)
1643
1644 // Prefetch instructions
1645 (PrefetchCache ...)   => (PrefetchT0 ...)
1646 (PrefetchCacheStreamed ...) => (PrefetchNTA ...)
1647
1648 // CPUID feature: BMI1.
1649 (AND(Q|L) x (NOT(Q|L) y))               && buildcfg.GOAMD64 >= 3 => (ANDN(Q|L) x y)
1650 (AND(Q|L) x (NEG(Q|L) x))               && buildcfg.GOAMD64 >= 3 => (BLSI(Q|L) x)
1651 (XOR(Q|L) x (ADD(Q|L)const [-1] x))     && buildcfg.GOAMD64 >= 3 => (BLSMSK(Q|L) x)
1652 (AND(Q|L) <t> x (ADD(Q|L)const [-1] x)) && buildcfg.GOAMD64 >= 3 => (Select0 <t> (BLSR(Q|L) x))
1653 // eliminate TEST instruction in classical "isPowerOfTwo" check
1654 (SETEQ       (TEST(Q|L) s:(Select0 blsr:(BLSR(Q|L) _)) s))        => (SETEQ       (Select1 <types.TypeFlags> blsr))
1655 (CMOVQEQ x y (TEST(Q|L) s:(Select0 blsr:(BLSR(Q|L) _)) s))        => (CMOVQEQ x y (Select1 <types.TypeFlags> blsr))
1656 (CMOVLEQ x y (TEST(Q|L) s:(Select0 blsr:(BLSR(Q|L) _)) s))        => (CMOVLEQ x y (Select1 <types.TypeFlags> blsr))
1657 (EQ          (TEST(Q|L) s:(Select0 blsr:(BLSR(Q|L) _)) s) yes no) => (EQ          (Select1 <types.TypeFlags> blsr) yes no)
1658 (SETNE       (TEST(Q|L) s:(Select0 blsr:(BLSR(Q|L) _)) s))        => (SETNE       (Select1 <types.TypeFlags> blsr))
1659 (CMOVQNE x y (TEST(Q|L) s:(Select0 blsr:(BLSR(Q|L) _)) s))        => (CMOVQNE x y (Select1 <types.TypeFlags> blsr))
1660 (CMOVLNE x y (TEST(Q|L) s:(Select0 blsr:(BLSR(Q|L) _)) s))        => (CMOVLNE x y (Select1 <types.TypeFlags> blsr))
1661 (NE          (TEST(Q|L) s:(Select0 blsr:(BLSR(Q|L) _)) s) yes no) => (NE          (Select1 <types.TypeFlags> blsr) yes no)
1662
1663 (BSWAP(Q|L) (BSWAP(Q|L) p)) => p
1664
1665 // CPUID feature: MOVBE.
1666 (MOV(Q|L)store   [i] {s} p x:(BSWAP(Q|L) w) mem) && x.Uses == 1 && buildcfg.GOAMD64 >= 3 => (MOVBE(Q|L)store [i] {s} p w mem)
1667 (MOVBE(Q|L)store [i] {s} p x:(BSWAP(Q|L) w) mem) && x.Uses == 1                          => (MOV(Q|L)store   [i] {s} p w mem)
1668 (BSWAP(Q|L) x:(MOV(Q|L)load   [i] {s} p mem))  && x.Uses == 1 && buildcfg.GOAMD64 >= 3 => @x.Block (MOVBE(Q|L)load [i] {s} p mem)
1669 (BSWAP(Q|L) x:(MOVBE(Q|L)load [i] {s} p mem))  && x.Uses == 1                          => @x.Block (MOV(Q|L)load   [i] {s} p mem)
1670 (MOVWstore [i] {s} p x:(ROLWconst [8] w) mem)   && x.Uses == 1 && buildcfg.GOAMD64 >= 3 => (MOVBEWstore [i] {s} p w mem)
1671 (MOVBEWstore [i] {s} p x:(ROLWconst [8] w) mem) && x.Uses == 1 => (MOVWstore [i] {s} p w mem)
1672
1673 (SAR(Q|L) l:(MOV(Q|L)load [off] {sym} ptr mem) x) && buildcfg.GOAMD64 >= 3 && canMergeLoad(v, l) && clobber(l) => (SARX(Q|L)load [off] {sym} ptr x mem)
1674 (SHL(Q|L) l:(MOV(Q|L)load [off] {sym} ptr mem) x) && buildcfg.GOAMD64 >= 3 && canMergeLoad(v, l) && clobber(l) => (SHLX(Q|L)load [off] {sym} ptr x mem)
1675 (SHR(Q|L) l:(MOV(Q|L)load [off] {sym} ptr mem) x) && buildcfg.GOAMD64 >= 3 && canMergeLoad(v, l) && clobber(l) => (SHRX(Q|L)load [off] {sym} ptr x mem)
1676
1677 ((SHL|SHR|SAR)XQload [off] {sym} ptr (MOVQconst [c]) mem) => ((SHL|SHR|SAR)Qconst [int8(c&63)] (MOVQload [off] {sym} ptr mem))
1678 ((SHL|SHR|SAR)XQload [off] {sym} ptr (MOVLconst [c]) mem) => ((SHL|SHR|SAR)Qconst [int8(c&63)] (MOVQload [off] {sym} ptr mem))
1679 ((SHL|SHR|SAR)XLload [off] {sym} ptr (MOVLconst [c]) mem) => ((SHL|SHR|SAR)Lconst [int8(c&31)] (MOVLload [off] {sym} ptr mem))