1 // Copyright 2016 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|Ptr) ...) => (ADD ...)
7 (Add(32|16|8) ...) => (ADDW ...)
8 (Add32F x y) => (Select0 (FADDS x y))
9 (Add64F x y) => (Select0 (FADD x y))
10
11 (Sub(64|Ptr) ...) => (SUB ...)
12 (Sub(32|16|8) ...) => (SUBW ...)
13 (Sub32F x y) => (Select0 (FSUBS x y))
14 (Sub64F x y) => (Select0 (FSUB x y))
15
16 (Mul64 ...) => (MULLD ...)
17 (Mul(32|16|8) ...) => (MULLW ...)
18 (Mul32F ...) => (FMULS ...)
19 (Mul64F ...) => (FMUL ...)
20 (Mul64uhilo ...) => (MLGR ...)
21
22 (Div32F ...) => (FDIVS ...)
23 (Div64F ...) => (FDIV ...)
24
25 (Div64 x y) => (DIVD x y)
26 (Div64u ...) => (DIVDU ...)
27 // DIVW/DIVWU has a 64-bit dividend and a 32-bit divisor,
28 // so a sign/zero extension of the dividend is required.
29 (Div32 x y) => (DIVW (MOVWreg x) y)
30 (Div32u x y) => (DIVWU (MOVWZreg x) y)
31 (Div16 x y) => (DIVW (MOVHreg x) (MOVHreg y))
32 (Div16u x y) => (DIVWU (MOVHZreg x) (MOVHZreg y))
33 (Div8 x y) => (DIVW (MOVBreg x) (MOVBreg y))
34 (Div8u x y) => (DIVWU (MOVBZreg x) (MOVBZreg y))
35
36 (Hmul(64|64u) ...) => (MULH(D|DU) ...)
37 (Hmul32 x y) => (SRDconst [32] (MULLD (MOVWreg x) (MOVWreg y)))
38 (Hmul32u x y) => (SRDconst [32] (MULLD (MOVWZreg x) (MOVWZreg y)))
39
40 (Mod64 x y) => (MODD x y)
41 (Mod64u ...) => (MODDU ...)
42 // MODW/MODWU has a 64-bit dividend and a 32-bit divisor,
43 // so a sign/zero extension of the dividend is required.
44 (Mod32 x y) => (MODW (MOVWreg x) y)
45 (Mod32u x y) => (MODWU (MOVWZreg x) y)
46 (Mod16 x y) => (MODW (MOVHreg x) (MOVHreg y))
47 (Mod16u x y) => (MODWU (MOVHZreg x) (MOVHZreg y))
48 (Mod8 x y) => (MODW (MOVBreg x) (MOVBreg y))
49 (Mod8u x y) => (MODWU (MOVBZreg x) (MOVBZreg y))
50
51 // (x + y) / 2 with x>=y -> (x - y) / 2 + y
52 (Avg64u <t> x y) => (ADD (SRDconst <t> (SUB <t> x y) [1]) y)
53
54 (And64 ...) => (AND ...)
55 (And(32|16|8) ...) => (ANDW ...)
56
57 (Or64 ...) => (OR ...)
58 (Or(32|16|8) ...) => (ORW ...)
59
60 (Xor64 ...) => (XOR ...)
61 (Xor(32|16|8) ...) => (XORW ...)
62
63 (Neg64 ...) => (NEG ...)
64 (Neg(32|16|8) ...) => (NEGW ...)
65 (Neg32F ...) => (FNEGS ...)
66 (Neg64F ...) => (FNEG ...)
67
68 (Com64 ...) => (NOT ...)
69 (Com(32|16|8) ...) => (NOTW ...)
70 (NOT x) => (XOR (MOVDconst [-1]) x)
71 (NOTW x) => (XORWconst [-1] x)
72
73 // Lowering boolean ops
74 (AndB ...) => (ANDW ...)
75 (OrB ...) => (ORW ...)
76 (Not x) => (XORWconst [1] x)
77
78 // Lowering pointer arithmetic
79 (OffPtr [off] ptr:(SP)) => (MOVDaddr [int32(off)] ptr)
80 (OffPtr [off] ptr) && ssa.Is32Bit(off) => (ADDconst [int32(off)] ptr)
81 (OffPtr [off] ptr) => (ADD (MOVDconst [off]) ptr)
82
83 (Ctz64NonZero ...) => (Ctz64 ...)
84 // Extend argument to ensure subsequent calculations have 64 correct bits.
85 (Ctz(32|16|8)NonZero x) => (Ctz64 (ZeroExt(32|16|8)to64 x))
86
87 // Ctz(x) = 64 - findLeftmostOne((x-1)&^x)
88 (Ctz64 <t> x) => (SUB (MOVDconst [64]) (FLOGR (AND <t> (SUBconst <t> [1] x) (NOT <t> x))))
89 (Ctz32 x) => (SUB (MOVDconst [64]) (FLOGR (MOVWZreg (ANDW <typ.UInt32> (SUBWconst <typ.UInt32> [1] x) (NOTW <typ.UInt32> x)))))
90 (Ctz16 x) => (Ctz64 (Or64 <typ.UInt64> x (MOVDconst [1<<16])))
91 (Ctz8 x) => (Ctz64 (Or64 <typ.UInt64> x (MOVDconst [1<<8])))
92
93 (BitLen64 x) => (SUB (MOVDconst [64]) (FLOGR x))
94 (BitLen(32|16|8) x) => (BitLen64 (ZeroExt(32|16|8)to64 x))
95
96 // POPCNT treats the input register as a vector of 8 bytes, producing
97 // a population count for each individual byte. For inputs larger than
98 // a single byte we therefore need to sum the individual bytes produced
99 // by the POPCNT instruction. For example, the following instruction
100 // sequence could be used to calculate the population count of a 4-byte
101 // value:
102 //
103 // MOVD $0x12345678, R1 // R1=0x12345678 <-- input
104 // POPCNT R1, R2 // R2=0x02030404
105 // SRW $16, R2, R3 // R3=0x00000203
106 // ADDW R2, R3, R4 // R4=0x02030607
107 // SRW $8, R4, R5 // R5=0x00020306
108 // ADDW R4, R5, R6 // R6=0x0205090d
109 // MOVBZ R6, R7 // R7=0x0000000d <-- result is 13
110 //
111 (PopCount8 x) => (POPCNT (MOVBZreg x))
112 (PopCount16 x) => (MOVBZreg (SumBytes2 (POPCNT <typ.UInt16> x)))
113 (PopCount32 x) => (MOVBZreg (SumBytes4 (POPCNT <typ.UInt32> x)))
114 (PopCount64 x) => (MOVBZreg (SumBytes8 (POPCNT <typ.UInt64> x)))
115
116 // SumBytes{2,4,8} pseudo operations sum the values of the rightmost
117 // 2, 4 or 8 bytes respectively. The result is a single byte however
118 // other bytes might contain junk so a zero extension is required if
119 // the desired output type is larger than 1 byte.
120 (SumBytes2 x) => (ADDW (SRWconst <typ.UInt8> x [8]) x)
121 (SumBytes4 x) => (SumBytes2 (ADDW <typ.UInt16> (SRWconst <typ.UInt16> x [16]) x))
122 (SumBytes8 x) => (SumBytes4 (ADDW <typ.UInt32> (SRDconst <typ.UInt32> x [32]) x))
123
124 (Bswap64 ...) => (MOVDBR ...)
125 (Bswap32 ...) => (MOVWBR ...)
126
127 // add with carry
128 (Select0 (Add64carry x y c))
129 => (Select0 <typ.UInt64> (ADDE x y (Select1 <types.TypeFlags> (ADDCconst c [-1]))))
130 (Select1 (Add64carry x y c))
131 => (Select0 <typ.UInt64> (ADDE (MOVDconst [0]) (MOVDconst [0]) (Select1 <types.TypeFlags> (ADDE x y (Select1 <types.TypeFlags> (ADDCconst c [-1]))))))
132
133 // subtract with borrow
134 (Select0 (Sub64borrow x y c))
135 => (Select0 <typ.UInt64> (SUBE x y (Select1 <types.TypeFlags> (SUBC (MOVDconst [0]) c))))
136 (Select1 (Sub64borrow x y c))
137 => (NEG (Select0 <typ.UInt64> (SUBE (MOVDconst [0]) (MOVDconst [0]) (Select1 <types.TypeFlags> (SUBE x y (Select1 <types.TypeFlags> (SUBC (MOVDconst [0]) c)))))))
138
139 // math package intrinsics
140 (Sqrt ...) => (FSQRT ...)
141 (Floor x) => (FIDBR [7] x)
142 (Ceil x) => (FIDBR [6] x)
143 (Trunc x) => (FIDBR [5] x)
144 (RoundToEven x) => (FIDBR [4] x)
145 (Round x) => (FIDBR [1] x)
146 (FMA x y z) => (FMADD z x y)
147 (FMA32 x y z) => (FMADDS z x y)
148
149 (Sqrt32 ...) => (FSQRTS ...)
150
151 (Max(64|32)F ...) => (WFMAX(D|S)B ...)
152 (Min(64|32)F ...) => (WFMIN(D|S)B ...)
153
154 // Atomic loads and stores.
155 // The SYNC instruction (fast-BCR-serialization) prevents store-load
156 // reordering. Other sequences of memory operations (load-load,
157 // store-store and load-store) are already guaranteed not to be reordered.
158 (AtomicLoad(8|32|Acq32|64|Ptr) ptr mem) => (MOV(BZ|WZ|WZ|D|D)atomicload ptr mem)
159 (AtomicStore(8|32|64|PtrNoWB) ptr val mem) => (SYNC (MOV(B|W|D|D)atomicstore ptr val mem))
160
161 // Store-release doesn't require store-load ordering.
162 (AtomicStoreRel32 ptr val mem) => (MOVWatomicstore ptr val mem)
163
164 // Atomic adds.
165 (AtomicAdd32 ptr val mem) => (AddTupleFirst32 val (LAA ptr val mem))
166 (AtomicAdd64 ptr val mem) => (AddTupleFirst64 val (LAAG ptr val mem))
167 (Select0 <t> (AddTupleFirst32 val tuple)) => (ADDW val (Select0 <t> tuple))
168 (Select1 (AddTupleFirst32 _ tuple)) => (Select1 tuple)
169 (Select0 <t> (AddTupleFirst64 val tuple)) => (ADD val (Select0 <t> tuple))
170 (Select1 (AddTupleFirst64 _ tuple)) => (Select1 tuple)
171
172 // Atomic exchanges.
173 (AtomicExchange32 ptr val mem) => (LoweredAtomicExchange32 ptr val mem)
174 (AtomicExchange64 ptr val mem) => (LoweredAtomicExchange64 ptr val mem)
175
176 // Atomic compare and swap.
177 (AtomicCompareAndSwap32 ptr old new_ mem) => (LoweredAtomicCas32 ptr old new_ mem)
178 (AtomicCompareAndSwap64 ptr old new_ mem) => (LoweredAtomicCas64 ptr old new_ mem)
179
180 // Atomic and: *(*uint8)(ptr) &= val
181 //
182 // Round pointer down to nearest word boundary and pad value with ones before
183 // applying atomic AND operation to target word.
184 //
185 // *(*uint32)(ptr &^ 3) &= rotateleft(uint32(val) | 0xffffff00, ((3 << 3) ^ ((ptr & 3) << 3))
186 //
187 (AtomicAnd8 ptr val mem)
188 => (LANfloor
189 ptr
190 (RLL <typ.UInt32>
191 (ORWconst <typ.UInt32> val [-1<<8])
192 (RXSBG <typ.UInt32> {s390x.NewRotateParams(59, 60, 3)} (MOVDconst [3<<3]) ptr))
193 mem)
194
195 // Atomic or: *(*uint8)(ptr) |= val
196 //
197 // Round pointer down to nearest word boundary and pad value with zeros before
198 // applying atomic OR operation to target word.
199 //
200 // *(*uint32)(ptr &^ 3) |= uint32(val) << ((3 << 3) ^ ((ptr & 3) << 3))
201 //
202 (AtomicOr8 ptr val mem)
203 => (LAOfloor
204 ptr
205 (SLW <typ.UInt32>
206 (MOVBZreg <typ.UInt32> val)
207 (RXSBG <typ.UInt32> {s390x.NewRotateParams(59, 60, 3)} (MOVDconst [3<<3]) ptr))
208 mem)
209
210 (AtomicAnd32 ...) => (LAN ...)
211 (AtomicOr32 ...) => (LAO ...)
212
213 // Lowering extension
214 // Note: we always extend to 64 bits even though some ops don't need that many result bits.
215 (SignExt8to(16|32|64) ...) => (MOVBreg ...)
216 (SignExt16to(32|64) ...) => (MOVHreg ...)
217 (SignExt32to64 ...) => (MOVWreg ...)
218
219 (ZeroExt8to(16|32|64) ...) => (MOVBZreg ...)
220 (ZeroExt16to(32|64) ...) => (MOVHZreg ...)
221 (ZeroExt32to64 ...) => (MOVWZreg ...)
222
223 (Slicemask <t> x) => (SRADconst (NEG <t> x) [63])
224
225 // Lowering truncation
226 // Because we ignore high parts of registers, truncates are just copies.
227 (Trunc(16|32|64)to8 ...) => (Copy ...)
228 (Trunc(32|64)to16 ...) => (Copy ...)
229 (Trunc64to32 ...) => (Copy ...)
230
231 // Lowering float <-> int
232 (Cvt32to32F ...) => (CEFBRA ...)
233 (Cvt32to64F ...) => (CDFBRA ...)
234 (Cvt64to32F ...) => (CEGBRA ...)
235 (Cvt64to64F ...) => (CDGBRA ...)
236
237 (Cvt32Fto32 ...) => (CFEBRA ...)
238 (Cvt32Fto64 ...) => (CGEBRA ...)
239 (Cvt64Fto32 ...) => (CFDBRA ...)
240 (Cvt64Fto64 ...) => (CGDBRA ...)
241
242 // Lowering float <-> uint
243 (Cvt32Uto32F ...) => (CELFBR ...)
244 (Cvt32Uto64F ...) => (CDLFBR ...)
245 (Cvt64Uto32F ...) => (CELGBR ...)
246 (Cvt64Uto64F ...) => (CDLGBR ...)
247
248 (Cvt32Fto32U ...) => (CLFEBR ...)
249 (Cvt32Fto64U ...) => (CLGEBR ...)
250 (Cvt64Fto32U ...) => (CLFDBR ...)
251 (Cvt64Fto64U ...) => (CLGDBR ...)
252
253 // Lowering float32 <-> float64
254 (Cvt32Fto64F ...) => (LDEBR ...)
255 (Cvt64Fto32F ...) => (LEDBR ...)
256
257 (CvtBoolToUint8 ...) => (Copy ...)
258
259 (Round(32|64)F ...) => (LoweredRound(32|64)F ...)
260
261 // Lowering shifts
262
263 // Lower bounded shifts first. No need to check shift value.
264 (Lsh64x(64|32|16|8) x y) && ssa.ShiftIsBounded(v) => (SLD x y)
265 (Lsh32x(64|32|16|8) x y) && ssa.ShiftIsBounded(v) => (SLW x y)
266 (Lsh16x(64|32|16|8) x y) && ssa.ShiftIsBounded(v) => (SLW x y)
267 (Lsh8x(64|32|16|8) x y) && ssa.ShiftIsBounded(v) => (SLW x y)
268 (Rsh64Ux(64|32|16|8) x y) && ssa.ShiftIsBounded(v) => (SRD x y)
269 (Rsh32Ux(64|32|16|8) x y) && ssa.ShiftIsBounded(v) => (SRW x y)
270 (Rsh16Ux(64|32|16|8) x y) && ssa.ShiftIsBounded(v) => (SRW (MOVHZreg x) y)
271 (Rsh8Ux(64|32|16|8) x y) && ssa.ShiftIsBounded(v) => (SRW (MOVBZreg x) y)
272 (Rsh64x(64|32|16|8) x y) && ssa.ShiftIsBounded(v) => (SRAD x y)
273 (Rsh32x(64|32|16|8) x y) && ssa.ShiftIsBounded(v) => (SRAW x y)
274 (Rsh16x(64|32|16|8) x y) && ssa.ShiftIsBounded(v) => (SRAW (MOVHreg x) y)
275 (Rsh8x(64|32|16|8) x y) && ssa.ShiftIsBounded(v) => (SRAW (MOVBreg x) y)
276
277 // Unsigned shifts need to return 0 if shift amount is >= width of shifted value.
278 // result = shift >= 64 ? 0 : arg << shift
279 (Lsh(64|32|16|8)x64 <t> x y) => (LOCGR {s390x.GreaterOrEqual} <t> (SL(D|W|W|W) <t> x y) (MOVDconst [0]) (CMPUconst y [64]))
280 (Lsh(64|32|16|8)x32 <t> x y) => (LOCGR {s390x.GreaterOrEqual} <t> (SL(D|W|W|W) <t> x y) (MOVDconst [0]) (CMPWUconst y [64]))
281 (Lsh(64|32|16|8)x16 <t> x y) => (LOCGR {s390x.GreaterOrEqual} <t> (SL(D|W|W|W) <t> x y) (MOVDconst [0]) (CMPWUconst (MOVHZreg y) [64]))
282 (Lsh(64|32|16|8)x8 <t> x y) => (LOCGR {s390x.GreaterOrEqual} <t> (SL(D|W|W|W) <t> x y) (MOVDconst [0]) (CMPWUconst (MOVBZreg y) [64]))
283
284 (Rsh(64|32)Ux64 <t> x y) => (LOCGR {s390x.GreaterOrEqual} <t> (SR(D|W) <t> x y) (MOVDconst [0]) (CMPUconst y [64]))
285 (Rsh(64|32)Ux32 <t> x y) => (LOCGR {s390x.GreaterOrEqual} <t> (SR(D|W) <t> x y) (MOVDconst [0]) (CMPWUconst y [64]))
286 (Rsh(64|32)Ux16 <t> x y) => (LOCGR {s390x.GreaterOrEqual} <t> (SR(D|W) <t> x y) (MOVDconst [0]) (CMPWUconst (MOVHZreg y) [64]))
287 (Rsh(64|32)Ux8 <t> x y) => (LOCGR {s390x.GreaterOrEqual} <t> (SR(D|W) <t> x y) (MOVDconst [0]) (CMPWUconst (MOVBZreg y) [64]))
288
289 (Rsh(16|8)Ux64 <t> x y) => (LOCGR {s390x.GreaterOrEqual} <t> (SRW <t> (MOV(H|B)Zreg x) y) (MOVDconst [0]) (CMPUconst y [64]))
290 (Rsh(16|8)Ux32 <t> x y) => (LOCGR {s390x.GreaterOrEqual} <t> (SRW <t> (MOV(H|B)Zreg x) y) (MOVDconst [0]) (CMPWUconst y [64]))
291 (Rsh(16|8)Ux16 <t> x y) => (LOCGR {s390x.GreaterOrEqual} <t> (SRW <t> (MOV(H|B)Zreg x) y) (MOVDconst [0]) (CMPWUconst (MOVHZreg y) [64]))
292 (Rsh(16|8)Ux8 <t> x y) => (LOCGR {s390x.GreaterOrEqual} <t> (SRW <t> (MOV(H|B)Zreg x) y) (MOVDconst [0]) (CMPWUconst (MOVBZreg y) [64]))
293
294 // Signed right shift needs to return 0/-1 if shift amount is >= width of shifted value.
295 // We implement this by setting the shift value to 63 (all ones) if the shift value is more than 63.
296 // result = arg >> (shift >= 64 ? 63 : shift)
297 (Rsh(64|32)x64 x y) => (SRA(D|W) x (LOCGR {s390x.GreaterOrEqual} <y.Type> y (MOVDconst <y.Type> [63]) (CMPUconst y [64])))
298 (Rsh(64|32)x32 x y) => (SRA(D|W) x (LOCGR {s390x.GreaterOrEqual} <y.Type> y (MOVDconst <y.Type> [63]) (CMPWUconst y [64])))
299 (Rsh(64|32)x16 x y) => (SRA(D|W) x (LOCGR {s390x.GreaterOrEqual} <y.Type> y (MOVDconst <y.Type> [63]) (CMPWUconst (MOVHZreg y) [64])))
300 (Rsh(64|32)x8 x y) => (SRA(D|W) x (LOCGR {s390x.GreaterOrEqual} <y.Type> y (MOVDconst <y.Type> [63]) (CMPWUconst (MOVBZreg y) [64])))
301
302 (Rsh(16|8)x64 x y) => (SRAW (MOV(H|B)reg x) (LOCGR {s390x.GreaterOrEqual} <y.Type> y (MOVDconst <y.Type> [63]) (CMPUconst y [64])))
303 (Rsh(16|8)x32 x y) => (SRAW (MOV(H|B)reg x) (LOCGR {s390x.GreaterOrEqual} <y.Type> y (MOVDconst <y.Type> [63]) (CMPWUconst y [64])))
304 (Rsh(16|8)x16 x y) => (SRAW (MOV(H|B)reg x) (LOCGR {s390x.GreaterOrEqual} <y.Type> y (MOVDconst <y.Type> [63]) (CMPWUconst (MOVHZreg y) [64])))
305 (Rsh(16|8)x8 x y) => (SRAW (MOV(H|B)reg x) (LOCGR {s390x.GreaterOrEqual} <y.Type> y (MOVDconst <y.Type> [63]) (CMPWUconst (MOVBZreg y) [64])))
306
307 // Lowering rotates
308 (RotateLeft8 <t> x (MOVDconst [c])) => (Or8 (Lsh8x64 <t> x (MOVDconst [c&7])) (Rsh8Ux64 <t> x (MOVDconst [-c&7])))
309 (RotateLeft16 <t> x (MOVDconst [c])) => (Or16 (Lsh16x64 <t> x (MOVDconst [c&15])) (Rsh16Ux64 <t> x (MOVDconst [-c&15])))
310 (RotateLeft32 ...) => (RLL ...)
311 (RotateLeft64 ...) => (RLLG ...)
312
313 // Lowering comparisons
314 (Less64 x y) => (LOCGR {s390x.Less} (MOVDconst [0]) (MOVDconst [1]) (CMP x y))
315 (Less32 x y) => (LOCGR {s390x.Less} (MOVDconst [0]) (MOVDconst [1]) (CMPW x y))
316 (Less(16|8) x y) => (LOCGR {s390x.Less} (MOVDconst [0]) (MOVDconst [1]) (CMPW (MOV(H|B)reg x) (MOV(H|B)reg y)))
317 (Less64U x y) => (LOCGR {s390x.Less} (MOVDconst [0]) (MOVDconst [1]) (CMPU x y))
318 (Less32U x y) => (LOCGR {s390x.Less} (MOVDconst [0]) (MOVDconst [1]) (CMPWU x y))
319 (Less(16|8)U x y) => (LOCGR {s390x.Less} (MOVDconst [0]) (MOVDconst [1]) (CMPWU (MOV(H|B)Zreg x) (MOV(H|B)Zreg y)))
320 (Less64F x y) => (LOCGR {s390x.Less} (MOVDconst [0]) (MOVDconst [1]) (FCMP x y))
321 (Less32F x y) => (LOCGR {s390x.Less} (MOVDconst [0]) (MOVDconst [1]) (FCMPS x y))
322
323 (Leq64 x y) => (LOCGR {s390x.LessOrEqual} (MOVDconst [0]) (MOVDconst [1]) (CMP x y))
324 (Leq32 x y) => (LOCGR {s390x.LessOrEqual} (MOVDconst [0]) (MOVDconst [1]) (CMPW x y))
325 (Leq(16|8) x y) => (LOCGR {s390x.LessOrEqual} (MOVDconst [0]) (MOVDconst [1]) (CMPW (MOV(H|B)reg x) (MOV(H|B)reg y)))
326 (Leq64U x y) => (LOCGR {s390x.LessOrEqual} (MOVDconst [0]) (MOVDconst [1]) (CMPU x y))
327 (Leq32U x y) => (LOCGR {s390x.LessOrEqual} (MOVDconst [0]) (MOVDconst [1]) (CMPWU x y))
328 (Leq(16|8)U x y) => (LOCGR {s390x.LessOrEqual} (MOVDconst [0]) (MOVDconst [1]) (CMPWU (MOV(H|B)Zreg x) (MOV(H|B)Zreg y)))
329 (Leq64F x y) => (LOCGR {s390x.LessOrEqual} (MOVDconst [0]) (MOVDconst [1]) (FCMP x y))
330 (Leq32F x y) => (LOCGR {s390x.LessOrEqual} (MOVDconst [0]) (MOVDconst [1]) (FCMPS x y))
331
332 (Eq(64|Ptr) x y) => (LOCGR {s390x.Equal} (MOVDconst [0]) (MOVDconst [1]) (CMP x y))
333 (Eq32 x y) => (LOCGR {s390x.Equal} (MOVDconst [0]) (MOVDconst [1]) (CMPW x y))
334 (Eq(16|8|B) x y) => (LOCGR {s390x.Equal} (MOVDconst [0]) (MOVDconst [1]) (CMPW (MOV(H|B|B)reg x) (MOV(H|B|B)reg y)))
335 (Eq64F x y) => (LOCGR {s390x.Equal} (MOVDconst [0]) (MOVDconst [1]) (FCMP x y))
336 (Eq32F x y) => (LOCGR {s390x.Equal} (MOVDconst [0]) (MOVDconst [1]) (FCMPS x y))
337
338 (Neq(64|Ptr) x y) => (LOCGR {s390x.NotEqual} (MOVDconst [0]) (MOVDconst [1]) (CMP x y))
339 (Neq32 x y) => (LOCGR {s390x.NotEqual} (MOVDconst [0]) (MOVDconst [1]) (CMPW x y))
340 (Neq(16|8|B) x y) => (LOCGR {s390x.NotEqual} (MOVDconst [0]) (MOVDconst [1]) (CMPW (MOV(H|B|B)reg x) (MOV(H|B|B)reg y)))
341 (Neq64F x y) => (LOCGR {s390x.NotEqual} (MOVDconst [0]) (MOVDconst [1]) (FCMP x y))
342 (Neq32F x y) => (LOCGR {s390x.NotEqual} (MOVDconst [0]) (MOVDconst [1]) (FCMPS x y))
343
344 // Lowering loads
345 (Load <t> ptr mem) && (ssa.Is64BitInt(t) ||ssa.IsPtr(t)) => (MOVDload ptr mem)
346 (Load <t> ptr mem) && ssa.Is32BitInt(t) && t.IsSigned() => (MOVWload ptr mem)
347 (Load <t> ptr mem) && ssa.Is32BitInt(t) && !t.IsSigned() => (MOVWZload ptr mem)
348 (Load <t> ptr mem) && ssa.Is16BitInt(t) && t.IsSigned() => (MOVHload ptr mem)
349 (Load <t> ptr mem) && ssa.Is16BitInt(t) && !t.IsSigned() => (MOVHZload ptr mem)
350 (Load <t> ptr mem) && ssa.Is8BitInt(t) && t.IsSigned() => (MOVBload ptr mem)
351 (Load <t> ptr mem) && (t.IsBoolean() || (ssa.Is8BitInt(t) && !t.IsSigned())) => (MOVBZload ptr mem)
352 (Load <t> ptr mem) && ssa.Is32BitFloat(t) => (FMOVSload ptr mem)
353 (Load <t> ptr mem) && ssa.Is64BitFloat(t) => (FMOVDload ptr mem)
354
355 // Lowering stores
356 (Store {t} ptr val mem) && t.Size() == 8 && t.IsFloat() => (FMOVDstore ptr val mem)
357 (Store {t} ptr val mem) && t.Size() == 4 && t.IsFloat() => (FMOVSstore ptr val mem)
358 (Store {t} ptr val mem) && t.Size() == 8 && !t.IsFloat() => (MOVDstore ptr val mem)
359 (Store {t} ptr val mem) && t.Size() == 4 && !t.IsFloat() => (MOVWstore ptr val mem)
360 (Store {t} ptr val mem) && t.Size() == 2 => (MOVHstore ptr val mem)
361 (Store {t} ptr val mem) && t.Size() == 1 => (MOVBstore ptr val mem)
362
363 // Lowering moves
364
365 // Load and store for small copies.
366 (Move [0] _ _ mem) => mem
367 (Move [1] dst src mem) => (MOVBstore dst (MOVBZload src mem) mem)
368 (Move [2] dst src mem) => (MOVHstore dst (MOVHZload src mem) mem)
369 (Move [4] dst src mem) => (MOVWstore dst (MOVWZload src mem) mem)
370 (Move [8] dst src mem) => (MOVDstore dst (MOVDload src mem) mem)
371 (Move [16] dst src mem) =>
372 (MOVDstore [8] dst (MOVDload [8] src mem)
373 (MOVDstore dst (MOVDload src mem) mem))
374 (Move [24] dst src mem) =>
375 (MOVDstore [16] dst (MOVDload [16] src mem)
376 (MOVDstore [8] dst (MOVDload [8] src mem)
377 (MOVDstore dst (MOVDload src mem) mem)))
378 (Move [3] dst src mem) =>
379 (MOVBstore [2] dst (MOVBZload [2] src mem)
380 (MOVHstore dst (MOVHZload src mem) mem))
381 (Move [5] dst src mem) =>
382 (MOVBstore [4] dst (MOVBZload [4] src mem)
383 (MOVWstore dst (MOVWZload src mem) mem))
384 (Move [6] dst src mem) =>
385 (MOVHstore [4] dst (MOVHZload [4] src mem)
386 (MOVWstore dst (MOVWZload src mem) mem))
387 (Move [7] dst src mem) =>
388 (MOVBstore [6] dst (MOVBZload [6] src mem)
389 (MOVHstore [4] dst (MOVHZload [4] src mem)
390 (MOVWstore dst (MOVWZload src mem) mem)))
391
392 // MVC for other moves. Use up to 4 instructions (sizes up to 1024 bytes).
393 (Move [s] dst src mem) && s > 0 && s <= 256 && ssa.LogLargeCopyValue(v, s) =>
394 (MVC [ssa.MakeValAndOff(int32(s), 0)] dst src mem)
395 (Move [s] dst src mem) && s > 256 && s <= 512 && ssa.LogLargeCopyValue(v, s) =>
396 (MVC [ssa.MakeValAndOff(int32(s)-256, 256)] dst src (MVC [ssa.MakeValAndOff(256, 0)] dst src mem))
397 (Move [s] dst src mem) && s > 512 && s <= 768 && ssa.LogLargeCopyValue(v, s) =>
398 (MVC [ssa.MakeValAndOff(int32(s)-512, 512)] dst src (MVC [ssa.MakeValAndOff(256, 256)] dst src (MVC [ssa.MakeValAndOff(256, 0)] dst src mem)))
399 (Move [s] dst src mem) && s > 768 && s <= 1024 && ssa.LogLargeCopyValue(v, s) =>
400 (MVC [ssa.MakeValAndOff(int32(s)-768, 768)] dst src (MVC [ssa.MakeValAndOff(256, 512)] dst src (MVC [ssa.MakeValAndOff(256, 256)] dst src (MVC [ssa.MakeValAndOff(256, 0)] dst src mem))))
401
402 // Move more than 1024 bytes using a loop.
403 (Move [s] dst src mem) && s > 1024 && ssa.LogLargeCopyValue(v, s) =>
404 (LoweredMove [s%256] dst src (ADD <src.Type> src (MOVDconst [(s/256-1)*256])) mem)
405
406 // Lowering Zero instructions
407 (Zero [0] _ mem) => mem
408 (Zero [1] destptr mem) => (MOVBstoreconst [0] destptr mem)
409 (Zero [2] destptr mem) => (MOVHstoreconst [0] destptr mem)
410 (Zero [4] destptr mem) => (MOVWstoreconst [0] destptr mem)
411 (Zero [8] destptr mem) => (MOVDstoreconst [0] destptr mem)
412 (Zero [3] destptr mem) =>
413 (MOVBstoreconst [ssa.MakeValAndOff(0,2)] destptr
414 (MOVHstoreconst [0] destptr mem))
415 (Zero [5] destptr mem) =>
416 (MOVBstoreconst [ssa.MakeValAndOff(0,4)] destptr
417 (MOVWstoreconst [0] destptr mem))
418 (Zero [6] destptr mem) =>
419 (MOVHstoreconst [ssa.MakeValAndOff(0,4)] destptr
420 (MOVWstoreconst [0] destptr mem))
421 (Zero [7] destptr mem) =>
422 (MOVWstoreconst [ssa.MakeValAndOff(0,3)] destptr
423 (MOVWstoreconst [0] destptr mem))
424
425 (Zero [s] destptr mem) && s > 0 && s <= 1024 =>
426 (CLEAR [ssa.MakeValAndOff(int32(s), 0)] destptr mem)
427
428 // Zero more than 1024 bytes using a loop.
429 (Zero [s] destptr mem) && s > 1024 =>
430 (LoweredZero [s%256] destptr (ADDconst <destptr.Type> destptr [(int32(s)/256-1)*256]) mem)
431
432 // Lowering constants
433 (Const(64|32|16|8) [val]) => (MOVDconst [int64(val)])
434 (Const(32|64)F ...) => (FMOV(S|D)const ...)
435 (ConstNil) => (MOVDconst [0])
436 (ConstBool [t]) => (MOVDconst [ssa.B2i(t)])
437
438 // Lowering calls
439 (StaticCall ...) => (CALLstatic ...)
440 (ClosureCall ...) => (CALLclosure ...)
441 (InterCall ...) => (CALLinter ...)
442 (TailCall ...) => (CALLtail ...)
443 (TailCallInter ...) => (CALLtailinter ...)
444
445 // Miscellaneous
446 (IsNonNil p) => (LOCGR {s390x.NotEqual} (MOVDconst [0]) (MOVDconst [1]) (CMPconst p [0]))
447 (IsInBounds idx len) => (LOCGR {s390x.Less} (MOVDconst [0]) (MOVDconst [1]) (CMPU idx len))
448 (IsSliceInBounds idx len) => (LOCGR {s390x.LessOrEqual} (MOVDconst [0]) (MOVDconst [1]) (CMPU idx len))
449 (NilCheck ...) => (LoweredNilCheck ...)
450 (GetG ...) => (LoweredGetG ...)
451 (GetClosurePtr ...) => (LoweredGetClosurePtr ...)
452 (GetCallerSP ...) => (LoweredGetCallerSP ...)
453 (GetCallerPC ...) => (LoweredGetCallerPC ...)
454 (Addr {sym} base) => (MOVDaddr {sym} base)
455 (LocalAddr <t> {sym} base mem) && t.Elem().HasPointers() => (MOVDaddr {sym} (SPanchored base mem))
456 (LocalAddr <t> {sym} base _) && !t.Elem().HasPointers() => (MOVDaddr {sym} base)
457 (ITab (Load ptr mem)) => (MOVDload ptr mem)
458
459 // block rewrites
460 (If cond yes no) => (CLIJ {s390x.LessOrGreater} (MOVBZreg <typ.Bool> cond) [0] yes no)
461
462 // Write barrier.
463 (WB ...) => (LoweredWB ...)
464
465 (PanicBounds ...) => (LoweredPanicBoundsRR ...)
466 (LoweredPanicBoundsRR [kind] x (MOVDconst [c]) mem) => (LoweredPanicBoundsRC [kind] x {ssa.PanicBoundsC{C:c}} mem)
467 (LoweredPanicBoundsRR [kind] (MOVDconst [c]) y mem) => (LoweredPanicBoundsCR [kind] {ssa.PanicBoundsC{C:c}} y mem)
468 (LoweredPanicBoundsRC [kind] {p} (MOVDconst [c]) mem) => (LoweredPanicBoundsCC [kind] {ssa.PanicBoundsCC{Cx:c, Cy:p.C}} mem)
469 (LoweredPanicBoundsCR [kind] {p} (MOVDconst [c]) mem) => (LoweredPanicBoundsCC [kind] {ssa.PanicBoundsCC{Cx:p.C, Cy:c}} mem)
470
471 // ***************************
472 // Above: lowering rules
473 // Below: optimizations
474 // ***************************
475 // TODO: Should the optimizations be a separate pass?
476
477 // Note: when removing unnecessary sign/zero extensions.
478 //
479 // After a value is spilled it is restored using a sign- or zero-extension
480 // to register-width as appropriate for its type. For example, a uint8 will
481 // be restored using a MOVBZ (llgc) instruction which will zero extend the
482 // 8-bit value to 64-bits.
483 //
484 // This is a hazard when folding sign- and zero-extensions since we need to
485 // ensure not only that the value in the argument register is correctly
486 // extended but also that it will still be correctly extended if it is
487 // spilled and restored.
488 //
489 // In general this means we need type checks when the RHS of a rule is an
490 // OpCopy (i.e. "(... x:(...) ...) -> x").
491
492 // Merge double extensions.
493 (MOV(H|HZ)reg e:(MOV(B|BZ)reg x)) && ssa.ClobberIfDead(e) => (MOV(B|BZ)reg x)
494 (MOV(W|WZ)reg e:(MOV(B|BZ)reg x)) && ssa.ClobberIfDead(e) => (MOV(B|BZ)reg x)
495 (MOV(W|WZ)reg e:(MOV(H|HZ)reg x)) && ssa.ClobberIfDead(e) => (MOV(H|HZ)reg x)
496
497 // Bypass redundant sign extensions.
498 (MOV(B|BZ)reg e:(MOVBreg x)) && ssa.ClobberIfDead(e) => (MOV(B|BZ)reg x)
499 (MOV(B|BZ)reg e:(MOVHreg x)) && ssa.ClobberIfDead(e) => (MOV(B|BZ)reg x)
500 (MOV(B|BZ)reg e:(MOVWreg x)) && ssa.ClobberIfDead(e) => (MOV(B|BZ)reg x)
501 (MOV(H|HZ)reg e:(MOVHreg x)) && ssa.ClobberIfDead(e) => (MOV(H|HZ)reg x)
502 (MOV(H|HZ)reg e:(MOVWreg x)) && ssa.ClobberIfDead(e) => (MOV(H|HZ)reg x)
503 (MOV(W|WZ)reg e:(MOVWreg x)) && ssa.ClobberIfDead(e) => (MOV(W|WZ)reg x)
504
505 // Bypass redundant zero extensions.
506 (MOV(B|BZ)reg e:(MOVBZreg x)) && ssa.ClobberIfDead(e) => (MOV(B|BZ)reg x)
507 (MOV(B|BZ)reg e:(MOVHZreg x)) && ssa.ClobberIfDead(e) => (MOV(B|BZ)reg x)
508 (MOV(B|BZ)reg e:(MOVWZreg x)) && ssa.ClobberIfDead(e) => (MOV(B|BZ)reg x)
509 (MOV(H|HZ)reg e:(MOVHZreg x)) && ssa.ClobberIfDead(e) => (MOV(H|HZ)reg x)
510 (MOV(H|HZ)reg e:(MOVWZreg x)) && ssa.ClobberIfDead(e) => (MOV(H|HZ)reg x)
511 (MOV(W|WZ)reg e:(MOVWZreg x)) && ssa.ClobberIfDead(e) => (MOV(W|WZ)reg x)
512
513 // Remove zero extensions after zero extending load.
514 // Note: take care that if x is spilled it is restored correctly.
515 (MOV(B|H|W)Zreg x:(MOVBZload _ _)) && (!x.Type.IsSigned() || x.Type.Size() > 1) => x
516 (MOV(H|W)Zreg x:(MOVHZload _ _)) && (!x.Type.IsSigned() || x.Type.Size() > 2) => x
517 (MOVWZreg x:(MOVWZload _ _)) && (!x.Type.IsSigned() || x.Type.Size() > 4) => x
518
519 // Remove sign extensions after sign extending load.
520 // Note: take care that if x is spilled it is restored correctly.
521 (MOV(B|H|W)reg x:(MOVBload _ _)) && (x.Type.IsSigned() || x.Type.Size() == 8) => x
522 (MOV(H|W)reg x:(MOVHload _ _)) && (x.Type.IsSigned() || x.Type.Size() == 8) => x
523 (MOVWreg x:(MOVWload _ _)) && (x.Type.IsSigned() || x.Type.Size() == 8) => x
524
525 // Remove sign extensions after zero extending load.
526 // These type checks are probably unnecessary but do them anyway just in case.
527 (MOV(H|W)reg x:(MOVBZload _ _)) && (!x.Type.IsSigned() || x.Type.Size() > 1) => x
528 (MOVWreg x:(MOVHZload _ _)) && (!x.Type.IsSigned() || x.Type.Size() > 2) => x
529
530 // Fold sign and zero extensions into loads.
531 //
532 // Note: The combined instruction must end up in the same block
533 // as the original load. If not, we end up making a value with
534 // memory type live in two different blocks, which can lead to
535 // multiple memory values alive simultaneously.
536 //
537 // Make sure we don't combine these ops if the load has another use.
538 // This prevents a single load from being split into multiple loads
539 // which then might return different values. See test/atomicload.go.
540 (MOV(B|H|W)Zreg <t> x:(MOV(B|H|W)load [o] {s} p mem))
541 && x.Uses == 1
542 && ssa.Clobber(x)
543 => @x.Block (MOV(B|H|W)Zload <t> [o] {s} p mem)
544 (MOV(B|H|W)reg <t> x:(MOV(B|H|W)Zload [o] {s} p mem))
545 && x.Uses == 1
546 && ssa.Clobber(x)
547 => @x.Block (MOV(B|H|W)load <t> [o] {s} p mem)
548
549 // Remove zero extensions after argument load.
550 (MOVBZreg x:(Arg <t>)) && !t.IsSigned() && t.Size() == 1 => x
551 (MOVHZreg x:(Arg <t>)) && !t.IsSigned() && t.Size() <= 2 => x
552 (MOVWZreg x:(Arg <t>)) && !t.IsSigned() && t.Size() <= 4 => x
553
554 // Remove sign extensions after argument load.
555 (MOVBreg x:(Arg <t>)) && t.IsSigned() && t.Size() == 1 => x
556 (MOVHreg x:(Arg <t>)) && t.IsSigned() && t.Size() <= 2 => x
557 (MOVWreg x:(Arg <t>)) && t.IsSigned() && t.Size() <= 4 => x
558
559 // Fold zero extensions into constants.
560 (MOVBZreg (MOVDconst [c])) => (MOVDconst [int64( uint8(c))])
561 (MOVHZreg (MOVDconst [c])) => (MOVDconst [int64(uint16(c))])
562 (MOVWZreg (MOVDconst [c])) => (MOVDconst [int64(uint32(c))])
563
564 // Fold sign extensions into constants.
565 (MOVBreg (MOVDconst [c])) => (MOVDconst [int64( int8(c))])
566 (MOVHreg (MOVDconst [c])) => (MOVDconst [int64(int16(c))])
567 (MOVWreg (MOVDconst [c])) => (MOVDconst [int64(int32(c))])
568
569 // Remove zero extension of conditional move.
570 // Note: only for MOVBZreg for now since it is added as part of 'if' statement lowering.
571 (MOVBZreg x:(LOCGR (MOVDconst [c]) (MOVDconst [d]) _))
572 && int64(uint8(c)) == c
573 && int64(uint8(d)) == d
574 && (!x.Type.IsSigned() || x.Type.Size() > 1)
575 => x
576
577 // Fold boolean tests into blocks.
578 // Note: this must match If statement lowering.
579 (CLIJ {s390x.LessOrGreater} (LOCGR {d} (MOVDconst [0]) (MOVDconst [x]) cmp) [0] yes no)
580 && int32(x) != 0
581 => (BRC {d} cmp yes no)
582
583 // Canonicalize BRC condition code mask by removing impossible conditions.
584 // Integer comparisons cannot generate the unordered condition.
585 (BRC {c} x:((CMP|CMPW|CMPU|CMPWU) _ _) yes no) && c&s390x.Unordered != 0 => (BRC {c&^s390x.Unordered} x yes no)
586 (BRC {c} x:((CMP|CMPW|CMPU|CMPWU)const _) yes no) && c&s390x.Unordered != 0 => (BRC {c&^s390x.Unordered} x yes no)
587
588 // Compare-and-branch.
589 // Note: bit 3 (unordered) must not be set so we mask out s390x.Unordered.
590 (BRC {c} (CMP x y) yes no) => (CGRJ {c&^s390x.Unordered} x y yes no)
591 (BRC {c} (CMPW x y) yes no) => (CRJ {c&^s390x.Unordered} x y yes no)
592 (BRC {c} (CMPU x y) yes no) => (CLGRJ {c&^s390x.Unordered} x y yes no)
593 (BRC {c} (CMPWU x y) yes no) => (CLRJ {c&^s390x.Unordered} x y yes no)
594
595 // Compare-and-branch (immediate).
596 // Note: bit 3 (unordered) must not be set so we mask out s390x.Unordered.
597 (BRC {c} (CMPconst x [y]) yes no) && y == int32( int8(y)) => (CGIJ {c&^s390x.Unordered} x [ int8(y)] yes no)
598 (BRC {c} (CMPWconst x [y]) yes no) && y == int32( int8(y)) => (CIJ {c&^s390x.Unordered} x [ int8(y)] yes no)
599 (BRC {c} (CMPUconst x [y]) yes no) && y == int32(uint8(y)) => (CLGIJ {c&^s390x.Unordered} x [uint8(y)] yes no)
600 (BRC {c} (CMPWUconst x [y]) yes no) && y == int32(uint8(y)) => (CLIJ {c&^s390x.Unordered} x [uint8(y)] yes no)
601
602 // Absorb immediate into compare-and-branch.
603 (C(R|GR)J {c} x (MOVDconst [y]) yes no) && is8Bit(y) => (C(I|GI)J {c} x [ int8(y)] yes no)
604 (CL(R|GR)J {c} x (MOVDconst [y]) yes no) && isU8Bit(y) => (CL(I|GI)J {c} x [uint8(y)] yes no)
605 (C(R|GR)J {c} (MOVDconst [x]) y yes no) && is8Bit(x) => (C(I|GI)J {c.ReverseComparison()} y [ int8(x)] yes no)
606 (CL(R|GR)J {c} (MOVDconst [x]) y yes no) && isU8Bit(x) => (CL(I|GI)J {c.ReverseComparison()} y [uint8(x)] yes no)
607
608 // Prefer comparison with immediate to compare-and-branch.
609 (CGRJ {c} x (MOVDconst [y]) yes no) && !is8Bit(y) && ssa.Is32Bit(y) => (BRC {c} (CMPconst x [int32(y)]) yes no)
610 (CRJ {c} x (MOVDconst [y]) yes no) && !is8Bit(y) && ssa.Is32Bit(y) => (BRC {c} (CMPWconst x [int32(y)]) yes no)
611 (CLGRJ {c} x (MOVDconst [y]) yes no) && !isU8Bit(y) && ssa.IsU32Bit(y) => (BRC {c} (CMPUconst x [int32(y)]) yes no)
612 (CLRJ {c} x (MOVDconst [y]) yes no) && !isU8Bit(y) && ssa.IsU32Bit(y) => (BRC {c} (CMPWUconst x [int32(y)]) yes no)
613 (CGRJ {c} (MOVDconst [x]) y yes no) && !is8Bit(x) && ssa.Is32Bit(x) => (BRC {c.ReverseComparison()} (CMPconst y [int32(x)]) yes no)
614 (CRJ {c} (MOVDconst [x]) y yes no) && !is8Bit(x) && ssa.Is32Bit(x) => (BRC {c.ReverseComparison()} (CMPWconst y [int32(x)]) yes no)
615 (CLGRJ {c} (MOVDconst [x]) y yes no) && !isU8Bit(x) && ssa.IsU32Bit(x) => (BRC {c.ReverseComparison()} (CMPUconst y [int32(x)]) yes no)
616 (CLRJ {c} (MOVDconst [x]) y yes no) && !isU8Bit(x) && ssa.IsU32Bit(x) => (BRC {c.ReverseComparison()} (CMPWUconst y [int32(x)]) yes no)
617
618 // Absorb sign/zero extensions into 32-bit compare-and-branch.
619 (CIJ {c} (MOV(W|WZ)reg x) [y] yes no) => (CIJ {c} x [y] yes no)
620 (CLIJ {c} (MOV(W|WZ)reg x) [y] yes no) => (CLIJ {c} x [y] yes no)
621
622 // Bring out-of-range signed immediates into range by varying branch condition.
623 (BRC {s390x.Less} (CMPconst x [ 128]) yes no) => (CGIJ {s390x.LessOrEqual} x [ 127] yes no)
624 (BRC {s390x.Less} (CMPWconst x [ 128]) yes no) => (CIJ {s390x.LessOrEqual} x [ 127] yes no)
625 (BRC {s390x.LessOrEqual} (CMPconst x [-129]) yes no) => (CGIJ {s390x.Less} x [-128] yes no)
626 (BRC {s390x.LessOrEqual} (CMPWconst x [-129]) yes no) => (CIJ {s390x.Less} x [-128] yes no)
627 (BRC {s390x.Greater} (CMPconst x [-129]) yes no) => (CGIJ {s390x.GreaterOrEqual} x [-128] yes no)
628 (BRC {s390x.Greater} (CMPWconst x [-129]) yes no) => (CIJ {s390x.GreaterOrEqual} x [-128] yes no)
629 (BRC {s390x.GreaterOrEqual} (CMPconst x [ 128]) yes no) => (CGIJ {s390x.Greater} x [ 127] yes no)
630 (BRC {s390x.GreaterOrEqual} (CMPWconst x [ 128]) yes no) => (CIJ {s390x.Greater} x [ 127] yes no)
631
632 // Bring out-of-range unsigned immediates into range by varying branch condition.
633 (BRC {s390x.Less} (CMP(WU|U)const x [256]) yes no) => (C(L|LG)IJ {s390x.LessOrEqual} x [255] yes no)
634 (BRC {s390x.GreaterOrEqual} (CMP(WU|U)const x [256]) yes no) => (C(L|LG)IJ {s390x.Greater} x [255] yes no)
635
636 // Bring out-of-range immediates into range by switching signedness (only == and !=).
637 (BRC {c} (CMPconst x [y]) yes no) && y == int32(uint8(y)) && (c == s390x.Equal || c == s390x.LessOrGreater) => (CLGIJ {c} x [uint8(y)] yes no)
638 (BRC {c} (CMPWconst x [y]) yes no) && y == int32(uint8(y)) && (c == s390x.Equal || c == s390x.LessOrGreater) => (CLIJ {c} x [uint8(y)] yes no)
639 (BRC {c} (CMPUconst x [y]) yes no) && y == int32( int8(y)) && (c == s390x.Equal || c == s390x.LessOrGreater) => (CGIJ {c} x [ int8(y)] yes no)
640 (BRC {c} (CMPWUconst x [y]) yes no) && y == int32( int8(y)) && (c == s390x.Equal || c == s390x.LessOrGreater) => (CIJ {c} x [ int8(y)] yes no)
641
642 // Fold constants into instructions.
643 (ADD x (MOVDconst <t> [c])) && ssa.Is32Bit(c) && !t.IsPtr() => (ADDconst [int32(c)] x)
644 (ADDW x (MOVDconst [c])) => (ADDWconst [int32(c)] x)
645
646 (SUB x (MOVDconst [c])) && ssa.Is32Bit(c) => (SUBconst x [int32(c)])
647 (SUB (MOVDconst [c]) x) && ssa.Is32Bit(c) => (NEG (SUBconst <v.Type> x [int32(c)]))
648 (SUBW x (MOVDconst [c])) => (SUBWconst x [int32(c)])
649 (SUBW (MOVDconst [c]) x) => (NEGW (SUBWconst <v.Type> x [int32(c)]))
650
651 (MULLD x (MOVDconst [c])) && ssa.Is32Bit(c) => (MULLDconst [int32(c)] x)
652 (MULLW x (MOVDconst [c])) => (MULLWconst [int32(c)] x)
653
654 // NILF instructions leave the high 32 bits unchanged which is
655 // equivalent to the leftmost 32 bits being set.
656 // TODO(mundaym): modify the assembler to accept 64-bit values
657 // and use ssa.IsU32Bit(^c).
658 (AND x (MOVDconst [c]))
659 && s390x.NewRotateParams(0, 63, 0).OutMerge(uint64(c)) != nil
660 => (RISBGZ x {*s390x.NewRotateParams(0, 63, 0).OutMerge(uint64(c))})
661 (AND x (MOVDconst [c]))
662 && ssa.Is32Bit(c)
663 && c < 0
664 => (ANDconst [c] x)
665 (AND x (MOVDconst [c]))
666 && ssa.Is32Bit(c)
667 && c >= 0
668 => (MOVWZreg (ANDWconst <typ.UInt32> [int32(c)] x))
669
670 (ANDW x (MOVDconst [c])) => (ANDWconst [int32(c)] x)
671
672 ((AND|ANDW)const [c] ((AND|ANDW)const [d] x)) => ((AND|ANDW)const [c&d] x)
673
674 ((OR|XOR) x (MOVDconst [c])) && ssa.IsU32Bit(c) => ((OR|XOR)const [c] x)
675 ((OR|XOR)W x (MOVDconst [c])) => ((OR|XOR)Wconst [int32(c)] x)
676
677 // Constant shifts.
678 (S(LD|RD|RAD) x (MOVDconst [c])) => (S(LD|RD|RAD)const x [uint8(c&63)])
679 (S(LW|RW|RAW) x (MOVDconst [c])) && c&32 == 0 => (S(LW|RW|RAW)const x [uint8(c&31)])
680 (S(LW|RW) _ (MOVDconst [c])) && c&32 != 0 => (MOVDconst [0])
681 (SRAW x (MOVDconst [c])) && c&32 != 0 => (SRAWconst x [31])
682
683 // Shifts only use the rightmost 6 bits of the shift value.
684 (S(LD|RD|RAD|LW|RW|RAW) x (RISBGZ y {r}))
685 && r.Amount == 0
686 && r.OutMask()&63 == 63
687 => (S(LD|RD|RAD|LW|RW|RAW) x y)
688 (S(LD|RD|RAD|LW|RW|RAW) x (AND (MOVDconst [c]) y))
689 => (S(LD|RD|RAD|LW|RW|RAW) x (ANDWconst <typ.UInt32> [int32(c&63)] y))
690 (S(LD|RD|RAD|LW|RW|RAW) x (ANDWconst [c] y)) && c&63 == 63
691 => (S(LD|RD|RAD|LW|RW|RAW) x y)
692 (SLD x (MOV(W|H|B|WZ|HZ|BZ)reg y)) => (SLD x y)
693 (SRD x (MOV(W|H|B|WZ|HZ|BZ)reg y)) => (SRD x y)
694 (SRAD x (MOV(W|H|B|WZ|HZ|BZ)reg y)) => (SRAD x y)
695 (SLW x (MOV(W|H|B|WZ|HZ|BZ)reg y)) => (SLW x y)
696 (SRW x (MOV(W|H|B|WZ|HZ|BZ)reg y)) => (SRW x y)
697 (SRAW x (MOV(W|H|B|WZ|HZ|BZ)reg y)) => (SRAW x y)
698
699 // Match rotate by constant.
700 (RLLG x (MOVDconst [c])) => (RISBGZ x {s390x.NewRotateParams(0, 63, uint8(c&63))})
701 (RLL x (MOVDconst [c])) => (RLLconst x [uint8(c&31)])
702
703 // Signed 64-bit comparison with immediate.
704 (CMP x (MOVDconst [c])) && ssa.Is32Bit(c) => (CMPconst x [int32(c)])
705 (CMP (MOVDconst [c]) x) && ssa.Is32Bit(c) => (InvertFlags (CMPconst x [int32(c)]))
706
707 // Unsigned 64-bit comparison with immediate.
708 (CMPU x (MOVDconst [c])) && ssa.IsU32Bit(c) => (CMPUconst x [int32(c)])
709 (CMPU (MOVDconst [c]) x) && ssa.IsU32Bit(c) => (InvertFlags (CMPUconst x [int32(c)]))
710
711 // Signed and unsigned 32-bit comparison with immediate.
712 (CMP(W|WU) x (MOVDconst [c])) => (CMP(W|WU)const x [int32(c)])
713 (CMP(W|WU) (MOVDconst [c]) x) => (InvertFlags (CMP(W|WU)const x [int32(c)]))
714
715 // Match (x >> c) << d to 'rotate then insert selected bits [into zero]'.
716 (SLDconst (SRDconst x [c]) [d]) => (RISBGZ x {s390x.NewRotateParams(uint8(max(0, int8(c-d))), 63-d, uint8(int8(d-c)&63))})
717
718 // Match (x << c) >> d to 'rotate then insert selected bits [into zero]'.
719 (SRDconst (SLDconst x [c]) [d]) => (RISBGZ x {s390x.NewRotateParams(d, uint8(min(63, int8(63-c+d))), uint8(int8(c-d)&63))})
720
721 // Absorb input zero extension into 'rotate then insert selected bits [into zero]'.
722 (RISBGZ (MOVWZreg x) {r}) && r.InMerge(0xffffffff) != nil => (RISBGZ x {*r.InMerge(0xffffffff)})
723 (RISBGZ (MOVHZreg x) {r}) && r.InMerge(0x0000ffff) != nil => (RISBGZ x {*r.InMerge(0x0000ffff)})
724 (RISBGZ (MOVBZreg x) {r}) && r.InMerge(0x000000ff) != nil => (RISBGZ x {*r.InMerge(0x000000ff)})
725
726 // Absorb 'rotate then insert selected bits [into zero]' into zero extension.
727 (MOVWZreg (RISBGZ x {r})) && r.OutMerge(0xffffffff) != nil => (RISBGZ x {*r.OutMerge(0xffffffff)})
728 (MOVHZreg (RISBGZ x {r})) && r.OutMerge(0x0000ffff) != nil => (RISBGZ x {*r.OutMerge(0x0000ffff)})
729 (MOVBZreg (RISBGZ x {r})) && r.OutMerge(0x000000ff) != nil => (RISBGZ x {*r.OutMerge(0x000000ff)})
730
731 // Absorb shift into 'rotate then insert selected bits [into zero]'.
732 //
733 // Any unsigned shift can be represented as a rotate and mask operation:
734 //
735 // x << c => RotateLeft64(x, c) & (^uint64(0) << c)
736 // x >> c => RotateLeft64(x, -c) & (^uint64(0) >> c)
737 //
738 // Therefore when a shift is used as the input to a rotate then insert
739 // selected bits instruction we can merge the two together. We just have
740 // to be careful that the resultant mask is representable (non-zero and
741 // contiguous). For example, assuming that x is variable and c, y and m
742 // are constants, a shift followed by a rotate then insert selected bits
743 // could be represented as:
744 //
745 // RotateLeft64(RotateLeft64(x, c) & (^uint64(0) << c), y) & m
746 //
747 // We can split the rotation by y into two, one rotate for x and one for
748 // the mask:
749 //
750 // RotateLeft64(RotateLeft64(x, c), y) & (RotateLeft64(^uint64(0) << c, y)) & m
751 //
752 // The rotations of x by c followed by y can then be combined:
753 //
754 // RotateLeft64(x, c+y) & (RotateLeft64(^uint64(0) << c, y)) & m
755 // ^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
756 // rotate mask
757 //
758 // To perform this optimization we therefore just need to check that it
759 // is valid to merge the shift mask (^(uint64(0)<<c)) into the selected
760 // bits mask (i.e. that the resultant mask is non-zero and contiguous).
761 //
762 (RISBGZ (SLDconst x [c]) {r}) && r.InMerge(^uint64(0)<<c) != nil => (RISBGZ x {(*r.InMerge(^uint64(0)<<c)).RotateLeft(c)})
763 (RISBGZ (SRDconst x [c]) {r}) && r.InMerge(^uint64(0)>>c) != nil => (RISBGZ x {(*r.InMerge(^uint64(0)>>c)).RotateLeft(-c)})
764
765 // Absorb 'rotate then insert selected bits [into zero]' into left shift.
766 (SLDconst (RISBGZ x {r}) [c])
767 && s390x.NewRotateParams(0, 63-c, c).InMerge(r.OutMask()) != nil
768 => (RISBGZ x {(*s390x.NewRotateParams(0, 63-c, c).InMerge(r.OutMask())).RotateLeft(r.Amount)})
769
770 // Absorb 'rotate then insert selected bits [into zero]' into right shift.
771 (SRDconst (RISBGZ x {r}) [c])
772 && s390x.NewRotateParams(c, 63, -c&63).InMerge(r.OutMask()) != nil
773 => (RISBGZ x {(*s390x.NewRotateParams(c, 63, -c&63).InMerge(r.OutMask())).RotateLeft(r.Amount)})
774
775 // Merge 'rotate then insert selected bits [into zero]' instructions together.
776 (RISBGZ (RISBGZ x {y}) {z})
777 && z.InMerge(y.OutMask()) != nil
778 => (RISBGZ x {(*z.InMerge(y.OutMask())).RotateLeft(y.Amount)})
779
780 // Convert RISBGZ into 64-bit shift (helps CSE).
781 (RISBGZ x {r}) && r.End == 63 && r.Start == -r.Amount&63 => (SRDconst x [-r.Amount&63])
782 (RISBGZ x {r}) && r.Start == 0 && r.End == 63-r.Amount => (SLDconst x [r.Amount])
783
784 // Optimize single bit isolation when it is known to be equivalent to
785 // the most significant bit due to mask produced by arithmetic shift.
786 // Simply isolate the most significant bit itself and place it in the
787 // correct position.
788 //
789 // Example: (int64(x) >> 63) & 0x8 -> RISBGZ $60, $60, $4, Rsrc, Rdst
790 (RISBGZ (SRADconst x [c]) {r})
791 && r.Start == r.End // single bit selected
792 && (r.Start+r.Amount)&63 <= c // equivalent to most significant bit of x
793 => (RISBGZ x {s390x.NewRotateParams(r.Start, r.Start, -r.Start&63)})
794
795 // Canonicalize the order of arguments to comparisons - helps with CSE.
796 ((CMP|CMPW|CMPU|CMPWU) x y) && ssa.CanonLessThan(x,y) => (InvertFlags ((CMP|CMPW|CMPU|CMPWU) y x))
797
798 // Use sign/zero extend instead of RISBGZ.
799 (RISBGZ x {r}) && r == s390x.NewRotateParams(56, 63, 0) => (MOVBZreg x)
800 (RISBGZ x {r}) && r == s390x.NewRotateParams(48, 63, 0) => (MOVHZreg x)
801 (RISBGZ x {r}) && r == s390x.NewRotateParams(32, 63, 0) => (MOVWZreg x)
802
803 // Use sign/zero extend instead of ANDW.
804 (ANDWconst [0x00ff] x) => (MOVBZreg x)
805 (ANDWconst [0xffff] x) => (MOVHZreg x)
806
807 // Strength reduce multiplication to the sum (or difference) of two powers of two.
808 //
809 // Examples:
810 // 5x -> 4x + 1x
811 // 10x -> 8x + 2x
812 // 120x -> 128x - 8x
813 // -120x -> 8x - 128x
814 //
815 // We know that the rightmost bit of any positive value, once isolated, must either
816 // be a power of 2 (because it is a single bit) or 0 (if the original value is 0).
817 // In all of these rules we use a rightmost bit calculation to determine one operand
818 // for the addition or subtraction. We then just need to calculate if the other
819 // operand is a valid power of 2 before we can match the rule.
820 //
821 // Notes:
822 // - the generic rules have already matched single powers of two so we ignore them here
823 // - isPowerOfTwo asserts that its argument is greater than 0
824 // - c&(c-1) = clear rightmost bit
825 // - c&^(c-1) = isolate rightmost bit
826
827 // c = 2ˣ + 2ʸ => c - 2ˣ = 2ʸ
828 (MULL(D|W)const <t> x [c]) && ssa.IsPowerOfTwo(c&(c-1))
829 => ((ADD|ADDW) (SL(D|W)const <t> x [uint8(ssa.Log32(c&(c-1)))])
830 (SL(D|W)const <t> x [uint8(ssa.Log32(c&^(c-1)))]))
831
832 // c = 2ʸ - 2ˣ => c + 2ˣ = 2ʸ
833 (MULL(D|W)const <t> x [c]) && ssa.IsPowerOfTwo(c+(c&^(c-1)))
834 => ((SUB|SUBW) (SL(D|W)const <t> x [uint8(ssa.Log32(c+(c&^(c-1))))])
835 (SL(D|W)const <t> x [uint8(ssa.Log32(c&^(c-1)))]))
836
837 // c = 2ˣ - 2ʸ => -c + 2ˣ = 2ʸ
838 (MULL(D|W)const <t> x [c]) && ssa.IsPowerOfTwo(-c+(-c&^(-c-1)))
839 => ((SUB|SUBW) (SL(D|W)const <t> x [uint8(ssa.Log32(-c&^(-c-1)))])
840 (SL(D|W)const <t> x [uint8(ssa.Log32(-c+(-c&^(-c-1))))]))
841
842 // Fold ADD into MOVDaddr. Odd offsets from SB shouldn't be folded (LARL can't handle them).
843 (ADDconst [c] (MOVDaddr [d] {s} x:(SB))) && ((c+d)&1 == 0) && ssa.Is32Bit(int64(c)+int64(d)) => (MOVDaddr [c+d] {s} x)
844 (ADDconst [c] (MOVDaddr [d] {s} x)) && x.Op != ssaop.OpSB && ssa.Is20Bit(int64(c)+int64(d)) => (MOVDaddr [c+d] {s} x)
845 (ADD idx (MOVDaddr [c] {s} ptr)) && ptr.Op != ssaop.OpSB => (MOVDaddridx [c] {s} ptr idx)
846
847 // fold ADDconst into MOVDaddrx
848 (ADDconst [c] (MOVDaddridx [d] {s} x y)) && ssa.Is20Bit(int64(c)+int64(d)) => (MOVDaddridx [c+d] {s} x y)
849 (MOVDaddridx [c] {s} (ADDconst [d] x) y) && ssa.Is20Bit(int64(c)+int64(d)) => (MOVDaddridx [c+d] {s} x y)
850 (MOVDaddridx [c] {s} x (ADDconst [d] y)) && ssa.Is20Bit(int64(c)+int64(d)) => (MOVDaddridx [c+d] {s} x y)
851
852 // reverse ordering of compare instruction
853 (LOCGR {c} x y (InvertFlags cmp)) => (LOCGR {c.ReverseComparison()} x y cmp)
854
855 // replace load from same location as preceding store with copy
856 (MOVDload [off] {sym} ptr1 (MOVDstore [off] {sym} ptr2 x _)) && ssa.IsSamePtr(ptr1, ptr2) => x
857 (MOVWload [off] {sym} ptr1 (MOVWstore [off] {sym} ptr2 x _)) && ssa.IsSamePtr(ptr1, ptr2) => (MOVWreg x)
858 (MOVHload [off] {sym} ptr1 (MOVHstore [off] {sym} ptr2 x _)) && ssa.IsSamePtr(ptr1, ptr2) => (MOVHreg x)
859 (MOVBload [off] {sym} ptr1 (MOVBstore [off] {sym} ptr2 x _)) && ssa.IsSamePtr(ptr1, ptr2) => (MOVBreg x)
860 (MOVWZload [off] {sym} ptr1 (MOVWstore [off] {sym} ptr2 x _)) && ssa.IsSamePtr(ptr1, ptr2) => (MOVWZreg x)
861 (MOVHZload [off] {sym} ptr1 (MOVHstore [off] {sym} ptr2 x _)) && ssa.IsSamePtr(ptr1, ptr2) => (MOVHZreg x)
862 (MOVBZload [off] {sym} ptr1 (MOVBstore [off] {sym} ptr2 x _)) && ssa.IsSamePtr(ptr1, ptr2) => (MOVBZreg x)
863 (MOVDload [off] {sym} ptr1 (FMOVDstore [off] {sym} ptr2 x _)) && ssa.IsSamePtr(ptr1, ptr2) => (LGDR x)
864 (FMOVDload [off] {sym} ptr1 (MOVDstore [off] {sym} ptr2 x _)) && ssa.IsSamePtr(ptr1, ptr2) => (LDGR x)
865 (FMOVDload [off] {sym} ptr1 (FMOVDstore [off] {sym} ptr2 x _)) && ssa.IsSamePtr(ptr1, ptr2) => x
866 (FMOVSload [off] {sym} ptr1 (FMOVSstore [off] {sym} ptr2 x _)) && ssa.IsSamePtr(ptr1, ptr2) => x
867
868 // prefer FPR <-> GPR moves over combined load ops
869 (MULLDload <t> [off] {sym} x ptr1 (FMOVDstore [off] {sym} ptr2 y _)) && ssa.IsSamePtr(ptr1, ptr2) => (MULLD x (LGDR <t> y))
870 (ADDload <t> [off] {sym} x ptr1 (FMOVDstore [off] {sym} ptr2 y _)) && ssa.IsSamePtr(ptr1, ptr2) => (ADD x (LGDR <t> y))
871 (SUBload <t> [off] {sym} x ptr1 (FMOVDstore [off] {sym} ptr2 y _)) && ssa.IsSamePtr(ptr1, ptr2) => (SUB x (LGDR <t> y))
872 (ORload <t> [off] {sym} x ptr1 (FMOVDstore [off] {sym} ptr2 y _)) && ssa.IsSamePtr(ptr1, ptr2) => (OR x (LGDR <t> y))
873 (ANDload <t> [off] {sym} x ptr1 (FMOVDstore [off] {sym} ptr2 y _)) && ssa.IsSamePtr(ptr1, ptr2) => (AND x (LGDR <t> y))
874 (XORload <t> [off] {sym} x ptr1 (FMOVDstore [off] {sym} ptr2 y _)) && ssa.IsSamePtr(ptr1, ptr2) => (XOR x (LGDR <t> y))
875
876 // detect attempts to set/clear the sign bit
877 // may need to be reworked when NIHH/OIHH are added
878 (RISBGZ (LGDR <t> x) {r}) && r == s390x.NewRotateParams(1, 63, 0) => (LGDR <t> (LPDFR <x.Type> x))
879 (LDGR <t> (RISBGZ x {r})) && r == s390x.NewRotateParams(1, 63, 0) => (LPDFR (LDGR <t> x))
880 (OR (MOVDconst [-1<<63]) (LGDR <t> x)) => (LGDR <t> (LNDFR <x.Type> x))
881 (LDGR <t> (OR (MOVDconst [-1<<63]) x)) => (LNDFR (LDGR <t> x))
882
883 // detect attempts to set the sign bit with load
884 (LDGR <t> x:(ORload <t1> [off] {sym} (MOVDconst [-1<<63]) ptr mem)) && x.Uses == 1 && ssa.Clobber(x) => @x.Block (LNDFR <t> (LDGR <t> (MOVDload <t1> [off] {sym} ptr mem)))
885
886 // detect copysign
887 (OR (RISBGZ (LGDR x) {r}) (LGDR (LPDFR <t> y)))
888 && r == s390x.NewRotateParams(0, 0, 0)
889 => (LGDR (CPSDR <t> y x))
890 (OR (RISBGZ (LGDR x) {r}) (MOVDconst [c]))
891 && c >= 0
892 && r == s390x.NewRotateParams(0, 0, 0)
893 => (LGDR (CPSDR <x.Type> (FMOVDconst <x.Type> [math.Float64frombits(uint64(c))]) x))
894 (CPSDR y (FMOVDconst [c])) && !math.Signbit(c) => (LPDFR y)
895 (CPSDR y (FMOVDconst [c])) && math.Signbit(c) => (LNDFR y)
896
897 // absorb negations into set/clear sign bit
898 (FNEG (LPDFR x)) => (LNDFR x)
899 (FNEG (LNDFR x)) => (LPDFR x)
900 (FNEGS (LPDFR x)) => (LNDFR x)
901 (FNEGS (LNDFR x)) => (LPDFR x)
902
903 // no need to convert float32 to float64 to set/clear sign bit
904 (LEDBR (LPDFR (LDEBR x))) => (LPDFR x)
905 (LEDBR (LNDFR (LDEBR x))) => (LNDFR x)
906
907 // remove unnecessary FPR <-> GPR moves
908 (LDGR (LGDR x)) => x
909 (LGDR (LDGR x)) => x
910
911 // Don't extend before storing
912 (MOVWstore [off] {sym} ptr (MOVWreg x) mem) => (MOVWstore [off] {sym} ptr x mem)
913 (MOVHstore [off] {sym} ptr (MOVHreg x) mem) => (MOVHstore [off] {sym} ptr x mem)
914 (MOVBstore [off] {sym} ptr (MOVBreg x) mem) => (MOVBstore [off] {sym} ptr x mem)
915 (MOVWstore [off] {sym} ptr (MOVWZreg x) mem) => (MOVWstore [off] {sym} ptr x mem)
916 (MOVHstore [off] {sym} ptr (MOVHZreg x) mem) => (MOVHstore [off] {sym} ptr x mem)
917 (MOVBstore [off] {sym} ptr (MOVBZreg x) mem) => (MOVBstore [off] {sym} ptr x mem)
918
919 // Fold constants into memory operations.
920 // Note that this is not always a good idea because if not all the uses of
921 // the ADDconst get eliminated, we still have to compute the ADDconst and we now
922 // have potentially two live values (ptr and (ADDconst [off] ptr)) instead of one.
923 // Nevertheless, let's do it!
924 (MOVDload [off1] {sym} (ADDconst [off2] ptr) mem) && ssa.Is20Bit(int64(off1)+int64(off2)) => (MOVDload [off1+off2] {sym} ptr mem)
925 (MOVWload [off1] {sym} (ADDconst [off2] ptr) mem) && ssa.Is20Bit(int64(off1)+int64(off2)) => (MOVWload [off1+off2] {sym} ptr mem)
926 (MOVHload [off1] {sym} (ADDconst [off2] ptr) mem) && ssa.Is20Bit(int64(off1)+int64(off2)) => (MOVHload [off1+off2] {sym} ptr mem)
927 (MOVBload [off1] {sym} (ADDconst [off2] ptr) mem) && ssa.Is20Bit(int64(off1)+int64(off2)) => (MOVBload [off1+off2] {sym} ptr mem)
928 (MOVWZload [off1] {sym} (ADDconst [off2] ptr) mem) && ssa.Is20Bit(int64(off1)+int64(off2)) => (MOVWZload [off1+off2] {sym} ptr mem)
929 (MOVHZload [off1] {sym} (ADDconst [off2] ptr) mem) && ssa.Is20Bit(int64(off1)+int64(off2)) => (MOVHZload [off1+off2] {sym} ptr mem)
930 (MOVBZload [off1] {sym} (ADDconst [off2] ptr) mem) && ssa.Is20Bit(int64(off1)+int64(off2)) => (MOVBZload [off1+off2] {sym} ptr mem)
931 (FMOVSload [off1] {sym} (ADDconst [off2] ptr) mem) && ssa.Is20Bit(int64(off1)+int64(off2)) => (FMOVSload [off1+off2] {sym} ptr mem)
932 (FMOVDload [off1] {sym} (ADDconst [off2] ptr) mem) && ssa.Is20Bit(int64(off1)+int64(off2)) => (FMOVDload [off1+off2] {sym} ptr mem)
933
934 (MOVDstore [off1] {sym} (ADDconst [off2] ptr) val mem) && ssa.Is20Bit(int64(off1)+int64(off2)) => (MOVDstore [off1+off2] {sym} ptr val mem)
935 (MOVWstore [off1] {sym} (ADDconst [off2] ptr) val mem) && ssa.Is20Bit(int64(off1)+int64(off2)) => (MOVWstore [off1+off2] {sym} ptr val mem)
936 (MOVHstore [off1] {sym} (ADDconst [off2] ptr) val mem) && ssa.Is20Bit(int64(off1)+int64(off2)) => (MOVHstore [off1+off2] {sym} ptr val mem)
937 (MOVBstore [off1] {sym} (ADDconst [off2] ptr) val mem) && ssa.Is20Bit(int64(off1)+int64(off2)) => (MOVBstore [off1+off2] {sym} ptr val mem)
938 (FMOVSstore [off1] {sym} (ADDconst [off2] ptr) val mem) && ssa.Is20Bit(int64(off1)+int64(off2)) => (FMOVSstore [off1+off2] {sym} ptr val mem)
939 (FMOVDstore [off1] {sym} (ADDconst [off2] ptr) val mem) && ssa.Is20Bit(int64(off1)+int64(off2)) => (FMOVDstore [off1+off2] {sym} ptr val mem)
940
941 (ADDload [off1] {sym} x (ADDconst [off2] ptr) mem) && ptr.Op != ssaop.OpSB && ssa.Is20Bit(int64(off1)+int64(off2)) => (ADDload [off1+off2] {sym} x ptr mem)
942 (ADDWload [off1] {sym} x (ADDconst [off2] ptr) mem) && ptr.Op != ssaop.OpSB && ssa.Is20Bit(int64(off1)+int64(off2)) => (ADDWload [off1+off2] {sym} x ptr mem)
943 (MULLDload [off1] {sym} x (ADDconst [off2] ptr) mem) && ptr.Op != ssaop.OpSB && ssa.Is20Bit(int64(off1)+int64(off2)) => (MULLDload [off1+off2] {sym} x ptr mem)
944 (MULLWload [off1] {sym} x (ADDconst [off2] ptr) mem) && ptr.Op != ssaop.OpSB && ssa.Is20Bit(int64(off1)+int64(off2)) => (MULLWload [off1+off2] {sym} x ptr mem)
945 (SUBload [off1] {sym} x (ADDconst [off2] ptr) mem) && ptr.Op != ssaop.OpSB && ssa.Is20Bit(int64(off1)+int64(off2)) => (SUBload [off1+off2] {sym} x ptr mem)
946 (SUBWload [off1] {sym} x (ADDconst [off2] ptr) mem) && ptr.Op != ssaop.OpSB && ssa.Is20Bit(int64(off1)+int64(off2)) => (SUBWload [off1+off2] {sym} x ptr mem)
947
948 (ANDload [off1] {sym} x (ADDconst [off2] ptr) mem) && ptr.Op != ssaop.OpSB && ssa.Is20Bit(int64(off1)+int64(off2)) => (ANDload [off1+off2] {sym} x ptr mem)
949 (ANDWload [off1] {sym} x (ADDconst [off2] ptr) mem) && ptr.Op != ssaop.OpSB && ssa.Is20Bit(int64(off1)+int64(off2)) => (ANDWload [off1+off2] {sym} x ptr mem)
950 (ORload [off1] {sym} x (ADDconst [off2] ptr) mem) && ptr.Op != ssaop.OpSB && ssa.Is20Bit(int64(off1)+int64(off2)) => (ORload [off1+off2] {sym} x ptr mem)
951 (ORWload [off1] {sym} x (ADDconst [off2] ptr) mem) && ptr.Op != ssaop.OpSB && ssa.Is20Bit(int64(off1)+int64(off2)) => (ORWload [off1+off2] {sym} x ptr mem)
952 (XORload [off1] {sym} x (ADDconst [off2] ptr) mem) && ptr.Op != ssaop.OpSB && ssa.Is20Bit(int64(off1)+int64(off2)) => (XORload [off1+off2] {sym} x ptr mem)
953 (XORWload [off1] {sym} x (ADDconst [off2] ptr) mem) && ptr.Op != ssaop.OpSB && ssa.Is20Bit(int64(off1)+int64(off2)) => (XORWload [off1+off2] {sym} x ptr mem)
954
955 // Fold constants into stores.
956 (MOVDstore [off] {sym} ptr (MOVDconst [c]) mem) && ssa.Is16Bit(c) && isU12Bit(int64(off)) && ptr.Op != ssaop.OpSB =>
957 (MOVDstoreconst [ssa.MakeValAndOff(int32(c),off)] {sym} ptr mem)
958 (MOVWstore [off] {sym} ptr (MOVDconst [c]) mem) && ssa.Is16Bit(c) && isU12Bit(int64(off)) && ptr.Op != ssaop.OpSB =>
959 (MOVWstoreconst [ssa.MakeValAndOff(int32(c),off)] {sym} ptr mem)
960 (MOVHstore [off] {sym} ptr (MOVDconst [c]) mem) && isU12Bit(int64(off)) && ptr.Op != ssaop.OpSB =>
961 (MOVHstoreconst [ssa.MakeValAndOff(int32(int16(c)),off)] {sym} ptr mem)
962 (MOVBstore [off] {sym} ptr (MOVDconst [c]) mem) && ssa.Is20Bit(int64(off)) && ptr.Op != ssaop.OpSB =>
963 (MOVBstoreconst [ssa.MakeValAndOff(int32(int8(c)),off)] {sym} ptr mem)
964
965 // Fold address offsets into constant stores.
966 (MOVDstoreconst [sc] {s} (ADDconst [off] ptr) mem) && isU12Bit(sc.Off64()+int64(off)) =>
967 (MOVDstoreconst [sc.AddOffset32(off)] {s} ptr mem)
968 (MOVWstoreconst [sc] {s} (ADDconst [off] ptr) mem) && isU12Bit(sc.Off64()+int64(off)) =>
969 (MOVWstoreconst [sc.AddOffset32(off)] {s} ptr mem)
970 (MOVHstoreconst [sc] {s} (ADDconst [off] ptr) mem) && isU12Bit(sc.Off64()+int64(off)) =>
971 (MOVHstoreconst [sc.AddOffset32(off)] {s} ptr mem)
972 (MOVBstoreconst [sc] {s} (ADDconst [off] ptr) mem) && ssa.Is20Bit(sc.Off64()+int64(off)) =>
973 (MOVBstoreconst [sc.AddOffset32(off)] {s} ptr mem)
974
975 // Merge address calculations into loads and stores.
976 // Offsets from SB must not be merged into unaligned memory accesses because
977 // loads/stores using PC-relative addressing directly must be aligned to the
978 // size of the target.
979 (MOVDload [off1] {sym1} (MOVDaddr <t> [off2] {sym2} base) mem) && ssa.Is32Bit(int64(off1)+int64(off2)) && ssa.CanMergeSym(sym1, sym2) && (base.Op != ssaop.OpSB || (t.IsPtr() && t.Elem().Alignment()%8 == 0 && (off1+off2)%8 == 0)) =>
980 (MOVDload [off1+off2] {ssa.MergeSym(sym1,sym2)} base mem)
981 (MOVWZload [off1] {sym1} (MOVDaddr <t> [off2] {sym2} base) mem) && ssa.Is32Bit(int64(off1)+int64(off2)) && ssa.CanMergeSym(sym1, sym2) && (base.Op != ssaop.OpSB || (t.IsPtr() && t.Elem().Alignment()%4 == 0 && (off1+off2)%4 == 0)) =>
982 (MOVWZload [off1+off2] {ssa.MergeSym(sym1,sym2)} base mem)
983 (MOVHZload [off1] {sym1} (MOVDaddr <t> [off2] {sym2} base) mem) && ssa.Is32Bit(int64(off1)+int64(off2)) && ssa.CanMergeSym(sym1, sym2) && (base.Op != ssaop.OpSB || (t.IsPtr() && t.Elem().Alignment()%2 == 0 && (off1+off2)%2 == 0)) =>
984 (MOVHZload [off1+off2] {ssa.MergeSym(sym1,sym2)} base mem)
985 (MOVBZload [off1] {sym1} (MOVDaddr [off2] {sym2} base) mem) && ssa.Is32Bit(int64(off1)+int64(off2)) && ssa.CanMergeSym(sym1, sym2) =>
986 (MOVBZload [off1+off2] {ssa.MergeSym(sym1,sym2)} base mem)
987 (FMOVSload [off1] {sym1} (MOVDaddr [off2] {sym2} base) mem) && ssa.Is32Bit(int64(off1)+int64(off2)) && ssa.CanMergeSym(sym1, sym2) =>
988 (FMOVSload [off1+off2] {ssa.MergeSym(sym1,sym2)} base mem)
989 (FMOVDload [off1] {sym1} (MOVDaddr [off2] {sym2} base) mem) && ssa.Is32Bit(int64(off1)+int64(off2)) && ssa.CanMergeSym(sym1, sym2) =>
990 (FMOVDload [off1+off2] {ssa.MergeSym(sym1,sym2)} base mem)
991
992 (MOVWload [off1] {sym1} (MOVDaddr <t> [off2] {sym2} base) mem) && ssa.Is32Bit(int64(off1)+int64(off2)) && ssa.CanMergeSym(sym1, sym2) && (base.Op != ssaop.OpSB || (t.IsPtr() && t.Elem().Alignment()%4 == 0 && (off1+off2)%4 == 0)) =>
993 (MOVWload [off1+off2] {ssa.MergeSym(sym1,sym2)} base mem)
994 (MOVHload [off1] {sym1} (MOVDaddr <t> [off2] {sym2} base) mem) && ssa.Is32Bit(int64(off1)+int64(off2)) && ssa.CanMergeSym(sym1, sym2) && (base.Op != ssaop.OpSB || (t.IsPtr() && t.Elem().Alignment()%2 == 0 && (off1+off2)%2 == 0)) =>
995 (MOVHload [off1+off2] {ssa.MergeSym(sym1,sym2)} base mem)
996 (MOVBload [off1] {sym1} (MOVDaddr [off2] {sym2} base) mem) && ssa.Is32Bit(int64(off1)+int64(off2)) && ssa.CanMergeSym(sym1, sym2) =>
997 (MOVBload [off1+off2] {ssa.MergeSym(sym1,sym2)} base mem)
998
999 (MOVDstore [off1] {sym1} (MOVDaddr <t> [off2] {sym2} base) val mem) && ssa.Is32Bit(int64(off1)+int64(off2)) && ssa.CanMergeSym(sym1, sym2) && (base.Op != ssaop.OpSB || (t.IsPtr() && t.Elem().Alignment()%8 == 0 && (off1+off2)%8 == 0)) =>
1000 (MOVDstore [off1+off2] {ssa.MergeSym(sym1,sym2)} base val mem)
1001 (MOVWstore [off1] {sym1} (MOVDaddr <t> [off2] {sym2} base) val mem) && ssa.Is32Bit(int64(off1)+int64(off2)) && ssa.CanMergeSym(sym1, sym2) && (base.Op != ssaop.OpSB || (t.IsPtr() && t.Elem().Alignment()%4 == 0 && (off1+off2)%4 == 0)) =>
1002 (MOVWstore [off1+off2] {ssa.MergeSym(sym1,sym2)} base val mem)
1003 (MOVHstore [off1] {sym1} (MOVDaddr <t> [off2] {sym2} base) val mem) && ssa.Is32Bit(int64(off1)+int64(off2)) && ssa.CanMergeSym(sym1, sym2) && (base.Op != ssaop.OpSB || (t.IsPtr() && t.Elem().Alignment()%2 == 0 && (off1+off2)%2 == 0)) =>
1004 (MOVHstore [off1+off2] {ssa.MergeSym(sym1,sym2)} base val mem)
1005 (MOVBstore [off1] {sym1} (MOVDaddr [off2] {sym2} base) val mem) && ssa.Is32Bit(int64(off1)+int64(off2)) && ssa.CanMergeSym(sym1, sym2) =>
1006 (MOVBstore [off1+off2] {ssa.MergeSym(sym1,sym2)} base val mem)
1007 (FMOVSstore [off1] {sym1} (MOVDaddr [off2] {sym2} base) val mem) && ssa.Is32Bit(int64(off1)+int64(off2)) && ssa.CanMergeSym(sym1, sym2) =>
1008 (FMOVSstore [off1+off2] {ssa.MergeSym(sym1,sym2)} base val mem)
1009 (FMOVDstore [off1] {sym1} (MOVDaddr [off2] {sym2} base) val mem) && ssa.Is32Bit(int64(off1)+int64(off2)) && ssa.CanMergeSym(sym1, sym2) =>
1010 (FMOVDstore [off1+off2] {ssa.MergeSym(sym1,sym2)} base val mem)
1011
1012 (ADDload [o1] {s1} x (MOVDaddr [o2] {s2} ptr) mem) && ptr.Op != ssaop.OpSB && ssa.Is20Bit(int64(o1)+int64(o2)) && ssa.CanMergeSym(s1, s2) => (ADDload [o1+o2] {ssa.MergeSym(s1, s2)} x ptr mem)
1013 (ADDWload [o1] {s1} x (MOVDaddr [o2] {s2} ptr) mem) && ptr.Op != ssaop.OpSB && ssa.Is20Bit(int64(o1)+int64(o2)) && ssa.CanMergeSym(s1, s2) => (ADDWload [o1+o2] {ssa.MergeSym(s1, s2)} x ptr mem)
1014 (MULLDload [o1] {s1} x (MOVDaddr [o2] {s2} ptr) mem) && ptr.Op != ssaop.OpSB && ssa.Is20Bit(int64(o1)+int64(o2)) && ssa.CanMergeSym(s1, s2) => (MULLDload [o1+o2] {ssa.MergeSym(s1, s2)} x ptr mem)
1015 (MULLWload [o1] {s1} x (MOVDaddr [o2] {s2} ptr) mem) && ptr.Op != ssaop.OpSB && ssa.Is20Bit(int64(o1)+int64(o2)) && ssa.CanMergeSym(s1, s2) => (MULLWload [o1+o2] {ssa.MergeSym(s1, s2)} x ptr mem)
1016 (SUBload [o1] {s1} x (MOVDaddr [o2] {s2} ptr) mem) && ptr.Op != ssaop.OpSB && ssa.Is20Bit(int64(o1)+int64(o2)) && ssa.CanMergeSym(s1, s2) => (SUBload [o1+o2] {ssa.MergeSym(s1, s2)} x ptr mem)
1017 (SUBWload [o1] {s1} x (MOVDaddr [o2] {s2} ptr) mem) && ptr.Op != ssaop.OpSB && ssa.Is20Bit(int64(o1)+int64(o2)) && ssa.CanMergeSym(s1, s2) => (SUBWload [o1+o2] {ssa.MergeSym(s1, s2)} x ptr mem)
1018
1019 (ANDload [o1] {s1} x (MOVDaddr [o2] {s2} ptr) mem) && ptr.Op != ssaop.OpSB && ssa.Is20Bit(int64(o1)+int64(o2)) && ssa.CanMergeSym(s1, s2) => (ANDload [o1+o2] {ssa.MergeSym(s1, s2)} x ptr mem)
1020 (ANDWload [o1] {s1} x (MOVDaddr [o2] {s2} ptr) mem) && ptr.Op != ssaop.OpSB && ssa.Is20Bit(int64(o1)+int64(o2)) && ssa.CanMergeSym(s1, s2) => (ANDWload [o1+o2] {ssa.MergeSym(s1, s2)} x ptr mem)
1021 (ORload [o1] {s1} x (MOVDaddr [o2] {s2} ptr) mem) && ptr.Op != ssaop.OpSB && ssa.Is20Bit(int64(o1)+int64(o2)) && ssa.CanMergeSym(s1, s2) => (ORload [o1+o2] {ssa.MergeSym(s1, s2)} x ptr mem)
1022 (ORWload [o1] {s1} x (MOVDaddr [o2] {s2} ptr) mem) && ptr.Op != ssaop.OpSB && ssa.Is20Bit(int64(o1)+int64(o2)) && ssa.CanMergeSym(s1, s2) => (ORWload [o1+o2] {ssa.MergeSym(s1, s2)} x ptr mem)
1023 (XORload [o1] {s1} x (MOVDaddr [o2] {s2} ptr) mem) && ptr.Op != ssaop.OpSB && ssa.Is20Bit(int64(o1)+int64(o2)) && ssa.CanMergeSym(s1, s2) => (XORload [o1+o2] {ssa.MergeSym(s1, s2)} x ptr mem)
1024 (XORWload [o1] {s1} x (MOVDaddr [o2] {s2} ptr) mem) && ptr.Op != ssaop.OpSB && ssa.Is20Bit(int64(o1)+int64(o2)) && ssa.CanMergeSym(s1, s2) => (XORWload [o1+o2] {ssa.MergeSym(s1, s2)} x ptr mem)
1025
1026 // Cannot store constant to SB directly (no 'move relative long immediate' instructions).
1027 (MOVDstoreconst [sc] {sym1} (MOVDaddr [off] {sym2} ptr) mem) && ptr.Op != ssaop.OpSB && ssa.CanMergeSym(sym1, sym2) && sc.CanAdd32(off) =>
1028 (MOVDstoreconst [sc.AddOffset32(off)] {ssa.MergeSym(sym1, sym2)} ptr mem)
1029 (MOVWstoreconst [sc] {sym1} (MOVDaddr [off] {sym2} ptr) mem) && ptr.Op != ssaop.OpSB && ssa.CanMergeSym(sym1, sym2) && sc.CanAdd32(off) =>
1030 (MOVWstoreconst [sc.AddOffset32(off)] {ssa.MergeSym(sym1, sym2)} ptr mem)
1031 (MOVHstoreconst [sc] {sym1} (MOVDaddr [off] {sym2} ptr) mem) && ptr.Op != ssaop.OpSB && ssa.CanMergeSym(sym1, sym2) && sc.CanAdd32(off) =>
1032 (MOVHstoreconst [sc.AddOffset32(off)] {ssa.MergeSym(sym1, sym2)} ptr mem)
1033 (MOVBstoreconst [sc] {sym1} (MOVDaddr [off] {sym2} ptr) mem) && ptr.Op != ssaop.OpSB && ssa.CanMergeSym(sym1, sym2) && sc.CanAdd32(off) =>
1034 (MOVBstoreconst [sc.AddOffset32(off)] {ssa.MergeSym(sym1, sym2)} ptr mem)
1035
1036 // MOVDaddr into MOVDaddridx
1037 (MOVDaddridx [off1] {sym1} (MOVDaddr [off2] {sym2} x) y) && ssa.Is32Bit(int64(off1)+int64(off2)) && ssa.CanMergeSym(sym1, sym2) && x.Op != ssaop.OpSB =>
1038 (MOVDaddridx [off1+off2] {ssa.MergeSym(sym1,sym2)} x y)
1039 (MOVDaddridx [off1] {sym1} x (MOVDaddr [off2] {sym2} y)) && ssa.Is32Bit(int64(off1)+int64(off2)) && ssa.CanMergeSym(sym1, sym2) && y.Op != ssaop.OpSB =>
1040 (MOVDaddridx [off1+off2] {ssa.MergeSym(sym1,sym2)} x y)
1041
1042 // Absorb InvertFlags into branches.
1043 (BRC {c} (InvertFlags cmp) yes no) => (BRC {c.ReverseComparison()} cmp yes no)
1044
1045 // Constant comparisons.
1046 (CMPconst (MOVDconst [x]) [y]) && x==int64(y) => (FlagEQ)
1047 (CMPconst (MOVDconst [x]) [y]) && x<int64(y) => (FlagLT)
1048 (CMPconst (MOVDconst [x]) [y]) && x>int64(y) => (FlagGT)
1049 (CMPUconst (MOVDconst [x]) [y]) && uint64(x)==uint64(y) => (FlagEQ)
1050 (CMPUconst (MOVDconst [x]) [y]) && uint64(x)<uint64(y) => (FlagLT)
1051 (CMPUconst (MOVDconst [x]) [y]) && uint64(x)>uint64(y) => (FlagGT)
1052
1053 (CMPWconst (MOVDconst [x]) [y]) && int32(x)==int32(y) => (FlagEQ)
1054 (CMPWconst (MOVDconst [x]) [y]) && int32(x)<int32(y) => (FlagLT)
1055 (CMPWconst (MOVDconst [x]) [y]) && int32(x)>int32(y) => (FlagGT)
1056 (CMPWUconst (MOVDconst [x]) [y]) && uint32(x)==uint32(y) => (FlagEQ)
1057 (CMPWUconst (MOVDconst [x]) [y]) && uint32(x)<uint32(y) => (FlagLT)
1058 (CMPWUconst (MOVDconst [x]) [y]) && uint32(x)>uint32(y) => (FlagGT)
1059
1060 (CMP(W|WU)const (MOVBZreg _) [c]) && 0xff < c => (FlagLT)
1061 (CMP(W|WU)const (MOVHZreg _) [c]) && 0xffff < c => (FlagLT)
1062
1063 (CMPconst (SRDconst _ [c]) [n]) && c > 0 && n < 0 => (FlagGT)
1064 (CMPWconst (SRWconst _ [c]) [n]) && c > 0 && n < 0 => (FlagGT)
1065
1066 (CMPUconst (SRDconst _ [c]) [n]) && c > 0 && c < 64 && (1<<uint(64-c)) <= uint64(uint32(n)) => (FlagLT)
1067 (CMPWUconst (SRWconst _ [c]) [n]) && c > 0 && c < 32 && (1<<uint(32-c)) <= uint32(n) => (FlagLT)
1068
1069 (CMPWconst (ANDWconst _ [m]) [n]) && int32(m) >= 0 && int32(m) < int32(n) => (FlagLT)
1070 (CMPWUconst (ANDWconst _ [m]) [n]) && uint32(m) < uint32(n) => (FlagLT)
1071
1072 (CMPconst (RISBGZ x {r}) [c]) && c > 0 && r.OutMask() < uint64(c) => (FlagLT)
1073 (CMPUconst (RISBGZ x {r}) [c]) && r.OutMask() < uint64(uint32(c)) => (FlagLT)
1074
1075 // Constant compare-and-branch with immediate.
1076 (CGIJ {c} (MOVDconst [x]) [y] yes no) && c&s390x.Equal != 0 && int64(x) == int64(y) => (First yes no)
1077 (CGIJ {c} (MOVDconst [x]) [y] yes no) && c&s390x.Less != 0 && int64(x) < int64(y) => (First yes no)
1078 (CGIJ {c} (MOVDconst [x]) [y] yes no) && c&s390x.Greater != 0 && int64(x) > int64(y) => (First yes no)
1079 (CIJ {c} (MOVDconst [x]) [y] yes no) && c&s390x.Equal != 0 && int32(x) == int32(y) => (First yes no)
1080 (CIJ {c} (MOVDconst [x]) [y] yes no) && c&s390x.Less != 0 && int32(x) < int32(y) => (First yes no)
1081 (CIJ {c} (MOVDconst [x]) [y] yes no) && c&s390x.Greater != 0 && int32(x) > int32(y) => (First yes no)
1082 (CLGIJ {c} (MOVDconst [x]) [y] yes no) && c&s390x.Equal != 0 && uint64(x) == uint64(y) => (First yes no)
1083 (CLGIJ {c} (MOVDconst [x]) [y] yes no) && c&s390x.Less != 0 && uint64(x) < uint64(y) => (First yes no)
1084 (CLGIJ {c} (MOVDconst [x]) [y] yes no) && c&s390x.Greater != 0 && uint64(x) > uint64(y) => (First yes no)
1085 (CLIJ {c} (MOVDconst [x]) [y] yes no) && c&s390x.Equal != 0 && uint32(x) == uint32(y) => (First yes no)
1086 (CLIJ {c} (MOVDconst [x]) [y] yes no) && c&s390x.Less != 0 && uint32(x) < uint32(y) => (First yes no)
1087 (CLIJ {c} (MOVDconst [x]) [y] yes no) && c&s390x.Greater != 0 && uint32(x) > uint32(y) => (First yes no)
1088 (CGIJ {c} (MOVDconst [x]) [y] yes no) && c&s390x.Equal == 0 && int64(x) == int64(y) => (First no yes)
1089 (CGIJ {c} (MOVDconst [x]) [y] yes no) && c&s390x.Less == 0 && int64(x) < int64(y) => (First no yes)
1090 (CGIJ {c} (MOVDconst [x]) [y] yes no) && c&s390x.Greater == 0 && int64(x) > int64(y) => (First no yes)
1091 (CIJ {c} (MOVDconst [x]) [y] yes no) && c&s390x.Equal == 0 && int32(x) == int32(y) => (First no yes)
1092 (CIJ {c} (MOVDconst [x]) [y] yes no) && c&s390x.Less == 0 && int32(x) < int32(y) => (First no yes)
1093 (CIJ {c} (MOVDconst [x]) [y] yes no) && c&s390x.Greater == 0 && int32(x) > int32(y) => (First no yes)
1094 (CLGIJ {c} (MOVDconst [x]) [y] yes no) && c&s390x.Equal == 0 && uint64(x) == uint64(y) => (First no yes)
1095 (CLGIJ {c} (MOVDconst [x]) [y] yes no) && c&s390x.Less == 0 && uint64(x) < uint64(y) => (First no yes)
1096 (CLGIJ {c} (MOVDconst [x]) [y] yes no) && c&s390x.Greater == 0 && uint64(x) > uint64(y) => (First no yes)
1097 (CLIJ {c} (MOVDconst [x]) [y] yes no) && c&s390x.Equal == 0 && uint32(x) == uint32(y) => (First no yes)
1098 (CLIJ {c} (MOVDconst [x]) [y] yes no) && c&s390x.Less == 0 && uint32(x) < uint32(y) => (First no yes)
1099 (CLIJ {c} (MOVDconst [x]) [y] yes no) && c&s390x.Greater == 0 && uint32(x) > uint32(y) => (First no yes)
1100
1101 // Constant compare-and-branch with immediate when unsigned comparison with zero.
1102 (C(L|LG)IJ {s390x.GreaterOrEqual} _ [0] yes no) => (First yes no)
1103 (C(L|LG)IJ {s390x.Less} _ [0] yes no) => (First no yes)
1104
1105 // Constant compare-and-branch when operands match.
1106 (C(GR|R|LGR|LR)J {c} x y yes no) && x == y && c&s390x.Equal != 0 => (First yes no)
1107 (C(GR|R|LGR|LR)J {c} x y yes no) && x == y && c&s390x.Equal == 0 => (First no yes)
1108
1109 // Convert 64-bit comparisons to 32-bit comparisons and signed comparisons
1110 // to unsigned comparisons.
1111 // Helps simplify constant comparison detection.
1112 (CM(P|PU)const (MOV(W|WZ)reg x) [c]) => (CMP(W|WU)const x [c])
1113 (CM(P|P|PU)const x:(MOV(H|HZ|HZ)reg _) [c]) => (CMP(W|W|WU)const x [c])
1114 // A sign-extended value compared unsigned with a 64-bit compare is huge when negative;
1115 // the 32-bit compare agrees only if the zero-extended immediate is below 1<<31.
1116 (CMPUconst x:(MOVHreg _) [c]) && c >= 0 => (CMPWUconst x [c])
1117 (CM(P|P|PU)const x:(MOV(B|BZ|BZ)reg _) [c]) => (CMP(W|W|WU)const x [c])
1118 (CMPUconst x:(MOVBreg _) [c]) && c >= 0 => (CMPWUconst x [c])
1119 (CMPconst (MOV(WZ|W)reg x:(ANDWconst [m] _)) [c]) && int32(m) >= 0 && c >= 0 => (CMPWUconst x [c])
1120 (CMPUconst (MOV(WZ|W)reg x:(ANDWconst [m] _)) [c]) && int32(m) >= 0 => (CMPWUconst x [c])
1121 (CMPconst x:(SRDconst _ [c]) [n]) && c > 0 && n >= 0 => (CMPUconst x [n])
1122 (CMPWconst x:(SRWconst _ [c]) [n]) && c > 0 && n >= 0 => (CMPWUconst x [n])
1123
1124 // Absorb sign and zero extensions into 32-bit comparisons.
1125 (CMP(W|W|WU|WU) x (MOV(W|WZ|W|WZ)reg y)) => (CMP(W|W|WU|WU) x y)
1126 (CMP(W|W|WU|WU) (MOV(W|WZ|W|WZ)reg x) y) => (CMP(W|W|WU|WU) x y)
1127 (CMP(W|W|WU|WU)const (MOV(W|WZ|W|WZ)reg x) [c]) => (CMP(W|W|WU|WU)const x [c])
1128
1129 // Absorb flag constants into branches.
1130 (BRC {c} (FlagEQ) yes no) && c&s390x.Equal != 0 => (First yes no)
1131 (BRC {c} (FlagLT) yes no) && c&s390x.Less != 0 => (First yes no)
1132 (BRC {c} (FlagGT) yes no) && c&s390x.Greater != 0 => (First yes no)
1133 (BRC {c} (FlagOV) yes no) && c&s390x.Unordered != 0 => (First yes no)
1134
1135 (BRC {c} (FlagEQ) yes no) && c&s390x.Equal == 0 => (First no yes)
1136 (BRC {c} (FlagLT) yes no) && c&s390x.Less == 0 => (First no yes)
1137 (BRC {c} (FlagGT) yes no) && c&s390x.Greater == 0 => (First no yes)
1138 (BRC {c} (FlagOV) yes no) && c&s390x.Unordered == 0 => (First no yes)
1139
1140 // Absorb flag constants into SETxx ops.
1141 (LOCGR {c} _ x (FlagEQ)) && c&s390x.Equal != 0 => x
1142 (LOCGR {c} _ x (FlagLT)) && c&s390x.Less != 0 => x
1143 (LOCGR {c} _ x (FlagGT)) && c&s390x.Greater != 0 => x
1144 (LOCGR {c} _ x (FlagOV)) && c&s390x.Unordered != 0 => x
1145
1146 (LOCGR {c} x _ (FlagEQ)) && c&s390x.Equal == 0 => x
1147 (LOCGR {c} x _ (FlagLT)) && c&s390x.Less == 0 => x
1148 (LOCGR {c} x _ (FlagGT)) && c&s390x.Greater == 0 => x
1149 (LOCGR {c} x _ (FlagOV)) && c&s390x.Unordered == 0 => x
1150
1151 // Remove redundant *const ops
1152 (ADDconst [0] x) => x
1153 (ADDWconst [c] x) && int32(c)==0 => x
1154 (SUBconst [0] x) => x
1155 (SUBWconst [c] x) && int32(c) == 0 => x
1156 (ANDconst [0] _) => (MOVDconst [0])
1157 (ANDWconst [c] _) && int32(c)==0 => (MOVDconst [0])
1158 (ANDconst [-1] x) => x
1159 (ANDWconst [c] x) && int32(c)==-1 => x
1160 (ORconst [0] x) => x
1161 (ORWconst [c] x) && int32(c)==0 => x
1162 (ORconst [-1] _) => (MOVDconst [-1])
1163 (ORWconst [c] _) && int32(c)==-1 => (MOVDconst [-1])
1164 (XORconst [0] x) => x
1165 (XORWconst [c] x) && int32(c)==0 => x
1166
1167 // Shifts by zero (may be inserted during multiplication strength reduction).
1168 ((SLD|SLW|SRD|SRW|SRAD|SRAW)const x [0]) => x
1169
1170 // Convert constant subtracts to constant adds.
1171 (SUBconst [c] x) && c != -(1<<31) => (ADDconst [-c] x)
1172 (SUBWconst [c] x) => (ADDWconst [-int32(c)] x)
1173
1174 // generic constant folding
1175 // TODO: more of this
1176 (ADDconst [c] (MOVDconst [d])) => (MOVDconst [int64(c)+d])
1177 (ADDWconst [c] (MOVDconst [d])) => (MOVDconst [int64(c)+d])
1178 (ADDconst [c] (ADDconst [d] x)) && ssa.Is32Bit(int64(c)+int64(d)) => (ADDconst [c+d] x)
1179 (ADDWconst [c] (ADDWconst [d] x)) => (ADDWconst [int32(c+d)] x)
1180 (SUBconst (MOVDconst [d]) [c]) => (MOVDconst [d-int64(c)])
1181 (SUBconst (SUBconst x [d]) [c]) && ssa.Is32Bit(-int64(c)-int64(d)) => (ADDconst [-c-d] x)
1182 (SRADconst [c] (MOVDconst [d])) => (MOVDconst [d>>uint64(c)])
1183 (SRAWconst [c] (MOVDconst [d])) => (MOVDconst [int64(int32(d))>>uint64(c)])
1184 (NEG (MOVDconst [c])) => (MOVDconst [-c])
1185 (NEGW (MOVDconst [c])) => (MOVDconst [int64(int32(-c))])
1186 (MULLDconst [c] (MOVDconst [d])) => (MOVDconst [int64(c)*d])
1187 (MULLWconst [c] (MOVDconst [d])) => (MOVDconst [int64(c*int32(d))])
1188 (AND (MOVDconst [c]) (MOVDconst [d])) => (MOVDconst [c&d])
1189 (ANDconst [c] (MOVDconst [d])) => (MOVDconst [c&d])
1190 (ANDWconst [c] (MOVDconst [d])) => (MOVDconst [int64(c)&d])
1191 (OR (MOVDconst [c]) (MOVDconst [d])) => (MOVDconst [c|d])
1192 (ORconst [c] (MOVDconst [d])) => (MOVDconst [c|d])
1193 (ORWconst [c] (MOVDconst [d])) => (MOVDconst [int64(c)|d])
1194 (XOR (MOVDconst [c]) (MOVDconst [d])) => (MOVDconst [c^d])
1195 (XORconst [c] (MOVDconst [d])) => (MOVDconst [c^d])
1196 (XORWconst [c] (MOVDconst [d])) => (MOVDconst [int64(c)^d])
1197 (LoweredRound32F x:(FMOVSconst)) => x
1198 (LoweredRound64F x:(FMOVDconst)) => x
1199
1200 // generic simplifications
1201 // TODO: more of this
1202 (ADD x (NEG y)) => (SUB x y)
1203 (ADDW x (NEGW y)) => (SUBW x y)
1204 (SUB x (NEG y)) => (ADD x y)
1205 (SUBW x (NEGW y)) => (ADDW x y)
1206 (SUB x x) => (MOVDconst [0])
1207 (SUBW x x) => (MOVDconst [0])
1208 (AND x x) => x
1209 (ANDW x x) => x
1210 (OR x x) => x
1211 (ORW x x) => x
1212 (XOR x x) => (MOVDconst [0])
1213 (XORW x x) => (MOVDconst [0])
1214 (NEG (NEG x)) => x
1215 (NEG (ADDconst [c] (NEG x))) && c != -(1<<31) => (ADDconst [-c] x)
1216 (MOVBZreg (ANDWconst [m] x)) => (MOVWZreg (ANDWconst <typ.UInt32> [int32( uint8(m))] x))
1217 (MOVHZreg (ANDWconst [m] x)) => (MOVWZreg (ANDWconst <typ.UInt32> [int32(uint16(m))] x))
1218 (MOVBreg (ANDWconst [m] x)) && int8(m) >= 0 => (MOVWZreg (ANDWconst <typ.UInt32> [int32( uint8(m))] x))
1219 (MOVHreg (ANDWconst [m] x)) && int16(m) >= 0 => (MOVWZreg (ANDWconst <typ.UInt32> [int32(uint16(m))] x))
1220
1221 // carry flag generation
1222 // (only constant fold carry of zero)
1223 (Select1 (ADDCconst (MOVDconst [c]) [d]))
1224 && uint64(c+int64(d)) >= uint64(c) && c+int64(d) == 0
1225 => (FlagEQ)
1226 (Select1 (ADDCconst (MOVDconst [c]) [d]))
1227 && uint64(c+int64(d)) >= uint64(c) && c+int64(d) != 0
1228 => (FlagLT)
1229
1230 // borrow flag generation
1231 // (only constant fold borrow of zero)
1232 (Select1 (SUBC (MOVDconst [c]) (MOVDconst [d])))
1233 && uint64(d) <= uint64(c) && c-d == 0
1234 => (FlagGT)
1235 (Select1 (SUBC (MOVDconst [c]) (MOVDconst [d])))
1236 && uint64(d) <= uint64(c) && c-d != 0
1237 => (FlagOV)
1238
1239 // add with carry
1240 (ADDE x y (FlagEQ)) => (ADDC x y)
1241 (ADDE x y (FlagLT)) => (ADDC x y)
1242 (ADDC x (MOVDconst [c])) && ssa.Is16Bit(c) => (ADDCconst x [int16(c)])
1243 (Select0 (ADDCconst (MOVDconst [c]) [d])) => (MOVDconst [c+int64(d)])
1244
1245 // subtract with borrow
1246 (SUBE x y (FlagGT)) => (SUBC x y)
1247 (SUBE x y (FlagOV)) => (SUBC x y)
1248 (Select0 (SUBC (MOVDconst [c]) (MOVDconst [d]))) => (MOVDconst [c-d])
1249
1250 // collapse carry chain
1251 (ADDE x y (Select1 (ADDCconst [-1] (Select0 (ADDE (MOVDconst [0]) (MOVDconst [0]) c)))))
1252 => (ADDE x y c)
1253
1254 // collapse borrow chain
1255 (SUBE x y (Select1 (SUBC (MOVDconst [0]) (NEG (Select0 (SUBE (MOVDconst [0]) (MOVDconst [0]) c))))))
1256 => (SUBE x y c)
1257
1258 // branch on carry
1259 (C(G|LG)IJ {s390x.Equal} (Select0 (ADDE (MOVDconst [0]) (MOVDconst [0]) carry)) [0]) => (BRC {s390x.NoCarry} carry)
1260 (C(G|LG)IJ {s390x.Equal} (Select0 (ADDE (MOVDconst [0]) (MOVDconst [0]) carry)) [1]) => (BRC {s390x.Carry} carry)
1261 (C(G|LG)IJ {s390x.LessOrGreater} (Select0 (ADDE (MOVDconst [0]) (MOVDconst [0]) carry)) [0]) => (BRC {s390x.Carry} carry)
1262 (C(G|LG)IJ {s390x.LessOrGreater} (Select0 (ADDE (MOVDconst [0]) (MOVDconst [0]) carry)) [1]) => (BRC {s390x.NoCarry} carry)
1263 (C(G|LG)IJ {s390x.Greater} (Select0 (ADDE (MOVDconst [0]) (MOVDconst [0]) carry)) [0]) => (BRC {s390x.Carry} carry)
1264
1265 // branch on borrow
1266 (C(G|LG)IJ {s390x.Equal} (NEG (Select0 (SUBE (MOVDconst [0]) (MOVDconst [0]) borrow))) [0]) => (BRC {s390x.NoBorrow} borrow)
1267 (C(G|LG)IJ {s390x.Equal} (NEG (Select0 (SUBE (MOVDconst [0]) (MOVDconst [0]) borrow))) [1]) => (BRC {s390x.Borrow} borrow)
1268 (C(G|LG)IJ {s390x.LessOrGreater} (NEG (Select0 (SUBE (MOVDconst [0]) (MOVDconst [0]) borrow))) [0]) => (BRC {s390x.Borrow} borrow)
1269 (C(G|LG)IJ {s390x.LessOrGreater} (NEG (Select0 (SUBE (MOVDconst [0]) (MOVDconst [0]) borrow))) [1]) => (BRC {s390x.NoBorrow} borrow)
1270 (C(G|LG)IJ {s390x.Greater} (NEG (Select0 (SUBE (MOVDconst [0]) (MOVDconst [0]) borrow))) [0]) => (BRC {s390x.Borrow} borrow)
1271
1272 // fused multiply-add
1273 (Select0 (F(ADD|SUB) (FMUL y z) x)) && x.Block.Func.UseFMA(v) => (FM(ADD|SUB) x y z)
1274 (Select0 (F(ADDS|SUBS) (FMULS y z) x)) && x.Block.Func.UseFMA(v) => (FM(ADDS|SUBS) x y z)
1275
1276 // Convert floating point comparisons against zero into 'load and test' instructions.
1277 (F(CMP|CMPS) x (FMOV(D|S)const [0.0])) => (LT(D|E)BR x)
1278 (F(CMP|CMPS) (FMOV(D|S)const [0.0]) x) => (InvertFlags (LT(D|E)BR <v.Type> x))
1279
1280 // FSUB, FSUBS, FADD, FADDS now produce a condition code representing the
1281 // comparison of the result with 0.0. If a compare with zero instruction
1282 // (e.g. LTDBR) is following one of those instructions, we can use the
1283 // generated flag and remove the comparison instruction.
1284 // Note: when inserting Select1 ops we need to ensure they are in the
1285 // same block as their argument. We could also use @x.Block for this
1286 // but moving the flag generating value to a different block seems to
1287 // increase the likelihood that the flags value will have to be regenerated
1288 // by flagalloc which is not what we want.
1289 (LTDBR (Select0 x:(F(ADD|SUB) _ _))) && b == x.Block => (Select1 x)
1290 (LTEBR (Select0 x:(F(ADDS|SUBS) _ _))) && b == x.Block => (Select1 x)
1291
1292 // Fold memory operations into operations.
1293 // Exclude global data (SB) because these instructions cannot handle relative addresses.
1294 // TODO(mundaym): indexed versions of these?
1295 ((ADD|SUB|MULLD|AND|OR|XOR) <t> x g:(MOVDload [off] {sym} ptr mem))
1296 && ptr.Op != ssaop.OpSB
1297 && ssa.Is20Bit(int64(off))
1298 && ssa.CanMergeLoadClobber(v, g, x)
1299 && ssa.Clobber(g)
1300 => ((ADD|SUB|MULLD|AND|OR|XOR)load <t> [off] {sym} x ptr mem)
1301 ((ADD|SUB|MULL|AND|OR|XOR)W <t> x g:(MOVWload [off] {sym} ptr mem))
1302 && ptr.Op != ssaop.OpSB
1303 && ssa.Is20Bit(int64(off))
1304 && ssa.CanMergeLoadClobber(v, g, x)
1305 && ssa.Clobber(g)
1306 => ((ADD|SUB|MULL|AND|OR|XOR)Wload <t> [off] {sym} x ptr mem)
1307 ((ADD|SUB|MULL|AND|OR|XOR)W <t> x g:(MOVWZload [off] {sym} ptr mem))
1308 && ptr.Op != ssaop.OpSB
1309 && ssa.Is20Bit(int64(off))
1310 && ssa.CanMergeLoadClobber(v, g, x)
1311 && ssa.Clobber(g)
1312 => ((ADD|SUB|MULL|AND|OR|XOR)Wload <t> [off] {sym} x ptr mem)
1313
1314 // Combine stores into store multiples.
1315 // 32-bit
1316 (MOVWstore [i] {s} p w1 x:(MOVWstore [i-4] {s} p w0 mem))
1317 && p.Op != ssaop.OpSB
1318 && x.Uses == 1
1319 && ssa.Is20Bit(int64(i)-4)
1320 && ssa.SetPos(v, x.Pos)
1321 && ssa.Clobber(x)
1322 => (STM2 [i-4] {s} p w0 w1 mem)
1323 (MOVWstore [i] {s} p w2 x:(STM2 [i-8] {s} p w0 w1 mem))
1324 && x.Uses == 1
1325 && ssa.Is20Bit(int64(i)-8)
1326 && ssa.SetPos(v, x.Pos)
1327 && ssa.Clobber(x)
1328 => (STM3 [i-8] {s} p w0 w1 w2 mem)
1329 (MOVWstore [i] {s} p w3 x:(STM3 [i-12] {s} p w0 w1 w2 mem))
1330 && x.Uses == 1
1331 && ssa.Is20Bit(int64(i)-12)
1332 && ssa.SetPos(v, x.Pos)
1333 && ssa.Clobber(x)
1334 => (STM4 [i-12] {s} p w0 w1 w2 w3 mem)
1335 (STM2 [i] {s} p w2 w3 x:(STM2 [i-8] {s} p w0 w1 mem))
1336 && x.Uses == 1
1337 && ssa.Is20Bit(int64(i)-8)
1338 && ssa.SetPos(v, x.Pos)
1339 && ssa.Clobber(x)
1340 => (STM4 [i-8] {s} p w0 w1 w2 w3 mem)
1341 // 64-bit
1342 (MOVDstore [i] {s} p w1 x:(MOVDstore [i-8] {s} p w0 mem))
1343 && p.Op != ssaop.OpSB
1344 && x.Uses == 1
1345 && ssa.Is20Bit(int64(i)-8)
1346 && ssa.SetPos(v, x.Pos)
1347 && ssa.Clobber(x)
1348 => (STMG2 [i-8] {s} p w0 w1 mem)
1349 (MOVDstore [i] {s} p w2 x:(STMG2 [i-16] {s} p w0 w1 mem))
1350 && x.Uses == 1
1351 && ssa.Is20Bit(int64(i)-16)
1352 && ssa.SetPos(v, x.Pos)
1353 && ssa.Clobber(x)
1354 => (STMG3 [i-16] {s} p w0 w1 w2 mem)
1355 (MOVDstore [i] {s} p w3 x:(STMG3 [i-24] {s} p w0 w1 w2 mem))
1356 && x.Uses == 1
1357 && ssa.Is20Bit(int64(i)-24)
1358 && ssa.SetPos(v, x.Pos)
1359 && ssa.Clobber(x)
1360 => (STMG4 [i-24] {s} p w0 w1 w2 w3 mem)
1361 (STMG2 [i] {s} p w2 w3 x:(STMG2 [i-16] {s} p w0 w1 mem))
1362 && x.Uses == 1
1363 && ssa.Is20Bit(int64(i)-16)
1364 && ssa.SetPos(v, x.Pos)
1365 && ssa.Clobber(x)
1366 => (STMG4 [i-16] {s} p w0 w1 w2 w3 mem)
1367
1368 // Convert 32-bit store multiples into 64-bit stores.
1369 (STM2 [i] {s} p (SRDconst [32] x) x mem) => (MOVDstore [i] {s} p x mem)
1370
1371 // Fold bit reversal into loads.
1372 (MOVWBR x:(MOVWZload [off] {sym} ptr mem)) && x.Uses == 1 => @x.Block (MOVWZreg (MOVWBRload [off] {sym} ptr mem)) // need zero extension?
1373 (MOVWBR x:(MOVWZloadidx [off] {sym} ptr idx mem)) && x.Uses == 1 => @x.Block (MOVWZreg (MOVWBRloadidx [off] {sym} ptr idx mem)) // need zero extension?
1374 (MOVDBR x:(MOVDload [off] {sym} ptr mem)) && x.Uses == 1 => @x.Block (MOVDBRload [off] {sym} ptr mem)
1375 (MOVDBR x:(MOVDloadidx [off] {sym} ptr idx mem)) && x.Uses == 1 => @x.Block (MOVDBRloadidx [off] {sym} ptr idx mem)
1376
1377 // Fold bit reversal into stores.
1378 (MOV(D|W)store [off] {sym} ptr r:(MOV(D|W)BR x) mem) && r.Uses == 1 => (MOV(D|W)BRstore [off] {sym} ptr x mem)
1379 (MOV(D|W)storeidx [off] {sym} ptr idx r:(MOV(D|W)BR x) mem) && r.Uses == 1 => (MOV(D|W)BRstoreidx [off] {sym} ptr idx x mem)
1380
1381 // Special bswap16 rules
1382 (Bswap16 x:(MOVHZload [off] {sym} ptr mem)) => @x.Block (MOVHZreg (MOVHBRload [off] {sym} ptr mem))
1383 (Bswap16 x:(MOVHZloadidx [off] {sym} ptr idx mem)) => @x.Block (MOVHZreg (MOVHBRloadidx [off] {sym} ptr idx mem))
1384 (MOVHstore [off] {sym} ptr (Bswap16 val) mem) => (MOVHBRstore [off] {sym} ptr val mem)
1385 (MOVHstoreidx [off] {sym} ptr idx (Bswap16 val) mem) => (MOVHBRstoreidx [off] {sym} ptr idx val mem)
1386
View as plain text