[SystemZ] Extend RISBG optimization
[oota-llvm.git] / test / CodeGen / SystemZ / risbg-01.ll
1 ; Test sequences that can use RISBG with a zeroed first operand.
2 ; The tests here assume that RISBLG isn't available.
3 ;
4 ; RUN: llc < %s -mtriple=s390x-linux-gnu -mcpu=z10 | FileCheck %s
5
6 ; Test an extraction of bit 0 from a right-shifted value.
7 define i32 @f1(i32 %foo) {
8 ; CHECK-LABEL: f1:
9 ; CHECK: risbg %r2, %r2, 63, 191, 54
10 ; CHECK: br %r14
11   %shr = lshr i32 %foo, 10
12   %and = and i32 %shr, 1
13   ret i32 %and
14 }
15
16 ; ...and again with i64.
17 define i64 @f2(i64 %foo) {
18 ; CHECK-LABEL: f2:
19 ; CHECK: risbg %r2, %r2, 63, 191, 54
20 ; CHECK: br %r14
21   %shr = lshr i64 %foo, 10
22   %and = and i64 %shr, 1
23   ret i64 %and
24 }
25
26 ; Test an extraction of other bits from a right-shifted value.
27 define i32 @f3(i32 %foo) {
28 ; CHECK-LABEL: f3:
29 ; CHECK: risbg %r2, %r2, 60, 189, 42
30 ; CHECK: br %r14
31   %shr = lshr i32 %foo, 22
32   %and = and i32 %shr, 12
33   ret i32 %and
34 }
35
36 ; ...and again with i64.
37 define i64 @f4(i64 %foo) {
38 ; CHECK-LABEL: f4:
39 ; CHECK: risbg %r2, %r2, 60, 189, 42
40 ; CHECK: br %r14
41   %shr = lshr i64 %foo, 22
42   %and = and i64 %shr, 12
43   ret i64 %and
44 }
45
46 ; Test an extraction of most bits from a right-shifted value.
47 ; The range should be reduced to exclude the zeroed high bits.
48 define i32 @f5(i32 %foo) {
49 ; CHECK-LABEL: f5:
50 ; CHECK: risbg %r2, %r2, 34, 188, 62
51 ; CHECK: br %r14
52   %shr = lshr i32 %foo, 2
53   %and = and i32 %shr, -8
54   ret i32 %and
55 }
56
57 ; ...and again with i64.
58 define i64 @f6(i64 %foo) {
59 ; CHECK-LABEL: f6:
60 ; CHECK: risbg %r2, %r2, 2, 188, 62
61 ; CHECK: br %r14
62   %shr = lshr i64 %foo, 2
63   %and = and i64 %shr, -8
64   ret i64 %and
65 }
66
67 ; Try the next value up (mask ....1111001).  This needs a separate shift
68 ; and mask.
69 define i32 @f7(i32 %foo) {
70 ; CHECK-LABEL: f7:
71 ; CHECK: srl %r2, 2
72 ; CHECK: nill %r2, 65529
73 ; CHECK: br %r14
74   %shr = lshr i32 %foo, 2
75   %and = and i32 %shr, -7
76   ret i32 %and
77 }
78
79 ; ...and again with i64.
80 define i64 @f8(i64 %foo) {
81 ; CHECK-LABEL: f8:
82 ; CHECK: srlg %r2, %r2, 2
83 ; CHECK: nill %r2, 65529
84 ; CHECK: br %r14
85   %shr = lshr i64 %foo, 2
86   %and = and i64 %shr, -7
87   ret i64 %and
88 }
89
90 ; Test an extraction of bits from a left-shifted value.  The range should
91 ; be reduced to exclude the zeroed low bits.
92 define i32 @f9(i32 %foo) {
93 ; CHECK-LABEL: f9:
94 ; CHECK: risbg %r2, %r2, 56, 189, 2
95 ; CHECK: br %r14
96   %shr = shl i32 %foo, 2
97   %and = and i32 %shr, 255
98   ret i32 %and
99 }
100
101 ; ...and again with i64.
102 define i64 @f10(i64 %foo) {
103 ; CHECK-LABEL: f10:
104 ; CHECK: risbg %r2, %r2, 56, 189, 2
105 ; CHECK: br %r14
106   %shr = shl i64 %foo, 2
107   %and = and i64 %shr, 255
108   ret i64 %and
109 }
110
111 ; Try a wrap-around mask (mask ....111100001111).  This needs a separate shift
112 ; and mask.
113 define i32 @f11(i32 %foo) {
114 ; CHECK-LABEL: f11:
115 ; CHECK: sll %r2, 2
116 ; CHECK: nill %r2, 65295
117 ; CHECK: br %r14
118   %shr = shl i32 %foo, 2
119   %and = and i32 %shr, -241
120   ret i32 %and
121 }
122
123 ; ...and again with i64.
124 define i64 @f12(i64 %foo) {
125 ; CHECK-LABEL: f12:
126 ; CHECK: sllg %r2, %r2, 2
127 ; CHECK: nill %r2, 65295
128 ; CHECK: br %r14
129   %shr = shl i64 %foo, 2
130   %and = and i64 %shr, -241
131   ret i64 %and
132 }
133
134 ; Test an extraction from a rotated value, no mask wraparound.
135 ; This is equivalent to the lshr case, because the bits from the
136 ; shl are not used.
137 define i32 @f13(i32 %foo) {
138 ; CHECK-LABEL: f13:
139 ; CHECK: risbg %r2, %r2, 56, 188, 46
140 ; CHECK: br %r14
141   %parta = shl i32 %foo, 14
142   %partb = lshr i32 %foo, 18
143   %rotl = or i32 %parta, %partb
144   %and = and i32 %rotl, 248
145   ret i32 %and
146 }
147
148 ; ...and again with i64.
149 define i64 @f14(i64 %foo) {
150 ; CHECK-LABEL: f14:
151 ; CHECK: risbg %r2, %r2, 56, 188, 14
152 ; CHECK: br %r14
153   %parta = shl i64 %foo, 14
154   %partb = lshr i64 %foo, 50
155   %rotl = or i64 %parta, %partb
156   %and = and i64 %rotl, 248
157   ret i64 %and
158 }
159
160 ; Try a case in which only the bits from the shl are used.
161 define i32 @f15(i32 %foo) {
162 ; CHECK-LABEL: f15:
163 ; CHECK: risbg %r2, %r2, 47, 177, 14
164 ; CHECK: br %r14
165   %parta = shl i32 %foo, 14
166   %partb = lshr i32 %foo, 18
167   %rotl = or i32 %parta, %partb
168   %and = and i32 %rotl, 114688
169   ret i32 %and
170 }
171
172 ; ...and again with i64.
173 define i64 @f16(i64 %foo) {
174 ; CHECK-LABEL: f16:
175 ; CHECK: risbg %r2, %r2, 47, 177, 14
176 ; CHECK: br %r14
177   %parta = shl i64 %foo, 14
178   %partb = lshr i64 %foo, 50
179   %rotl = or i64 %parta, %partb
180   %and = and i64 %rotl, 114688
181   ret i64 %and
182 }
183
184 ; Test a 32-bit rotate in which both parts of the OR are needed.
185 ; This needs a separate shift and mask.
186 define i32 @f17(i32 %foo) {
187 ; CHECK-LABEL: f17:
188 ; CHECK: rll %r2, %r2, 4
189 ; CHECK: nilf %r2, 126
190 ; CHECK: br %r14
191   %parta = shl i32 %foo, 4
192   %partb = lshr i32 %foo, 28
193   %rotl = or i32 %parta, %partb
194   %and = and i32 %rotl, 126
195   ret i32 %and
196 }
197
198 ; ...and for i64, where RISBG should do the rotate too.
199 define i64 @f18(i64 %foo) {
200 ; CHECK-LABEL: f18:
201 ; CHECK: risbg %r2, %r2, 57, 190, 4
202 ; CHECK: br %r14
203   %parta = shl i64 %foo, 4
204   %partb = lshr i64 %foo, 60
205   %rotl = or i64 %parta, %partb
206   %and = and i64 %rotl, 126
207   ret i64 %and
208 }
209
210 ; Test an arithmetic shift right in which some of the sign bits are kept.
211 ; This needs a separate shift and mask.
212 define i32 @f19(i32 %foo) {
213 ; CHECK-LABEL: f19:
214 ; CHECK: sra %r2, 28
215 ; CHECK: nilf %r2, 30
216 ; CHECK: br %r14
217   %shr = ashr i32 %foo, 28
218   %and = and i32 %shr, 30
219   ret i32 %and
220 }
221
222 ; ...and again with i64.  In this case RISBG is the best way of doing the AND.
223 define i64 @f20(i64 %foo) {
224 ; CHECK-LABEL: f20:
225 ; CHECK: srag [[REG:%r[0-5]]], %r2, 60
226 ; CHECK: risbg %r2, [[REG]], 59, 190, 0
227 ; CHECK: br %r14
228   %shr = ashr i64 %foo, 60
229   %and = and i64 %shr, 30
230   ret i64 %and
231 }
232
233 ; Now try an arithmetic right shift in which the sign bits aren't needed.
234 ; Introduce a second use of %shr so that the ashr doesn't decompose to
235 ; an lshr.
236 define i32 @f21(i32 %foo, i32 *%dest) {
237 ; CHECK-LABEL: f21:
238 ; CHECK: risbg %r2, %r2, 60, 190, 36
239 ; CHECK: br %r14
240   %shr = ashr i32 %foo, 28
241   store i32 %shr, i32 *%dest
242   %and = and i32 %shr, 14
243   ret i32 %and
244 }
245
246 ; ...and again with i64.
247 define i64 @f22(i64 %foo, i64 *%dest) {
248 ; CHECK-LABEL: f22:
249 ; CHECK: risbg %r2, %r2, 60, 190, 4
250 ; CHECK: br %r14
251   %shr = ashr i64 %foo, 60
252   store i64 %shr, i64 *%dest
253   %and = and i64 %shr, 14
254   ret i64 %and
255 }
256
257 ; Check that we use RISBG for shifted values even if the AND is a
258 ; natural zero extension.
259 define i64 @f23(i64 %foo) {
260 ; CHECK-LABEL: f23:
261 ; CHECK: risbg %r2, %r2, 56, 191, 62
262 ; CHECK: br %r14
263   %shr = lshr i64 %foo, 2
264   %and = and i64 %shr, 255
265   ret i64 %and
266 }
267
268 ; Test a case where the AND comes before a rotate.  This needs a separate
269 ; mask and rotate.
270 define i32 @f24(i32 %foo) {
271 ; CHECK-LABEL: f24:
272 ; CHECK: nilf %r2, 14
273 ; CHECK: rll %r2, %r2, 3
274 ; CHECK: br %r14
275   %and = and i32 %foo, 14
276   %parta = shl i32 %and, 3
277   %partb = lshr i32 %and, 29
278   %rotl = or i32 %parta, %partb
279   ret i32 %rotl
280 }
281
282 ; ...and again with i64, where a single RISBG is enough.
283 define i64 @f25(i64 %foo) {
284 ; CHECK-LABEL: f25:
285 ; CHECK: risbg %r2, %r2, 57, 187, 3
286 ; CHECK: br %r14
287   %and = and i64 %foo, 14
288   %parta = shl i64 %and, 3
289   %partb = lshr i64 %and, 61
290   %rotl = or i64 %parta, %partb
291   ret i64 %rotl
292 }
293
294 ; Test a wrap-around case in which the AND comes before a rotate.
295 ; This again needs a separate mask and rotate.
296 define i32 @f26(i32 %foo) {
297 ; CHECK-LABEL: f26:
298 ; CHECK: nill %r2, 65487
299 ; CHECK: rll %r2, %r2, 5
300 ; CHECK: br %r14
301   %and = and i32 %foo, -49
302   %parta = shl i32 %and, 5
303   %partb = lshr i32 %and, 27
304   %rotl = or i32 %parta, %partb
305   ret i32 %rotl
306 }
307
308 ; ...and again with i64, where a single RISBG is OK.
309 define i64 @f27(i64 %foo) {
310 ; CHECK-LABEL: f27:
311 ; CHECK: risbg %r2, %r2, 55, 180, 5
312 ; CHECK: br %r14
313   %and = and i64 %foo, -49
314   %parta = shl i64 %and, 5
315   %partb = lshr i64 %and, 59
316   %rotl = or i64 %parta, %partb
317   ret i64 %rotl
318 }
319
320 ; Test a case where the AND comes before a shift left.
321 define i32 @f28(i32 %foo) {
322 ; CHECK-LABEL: f28:
323 ; CHECK: risbg %r2, %r2, 32, 173, 17
324 ; CHECK: br %r14
325   %and = and i32 %foo, 32766
326   %shl = shl i32 %and, 17
327   ret i32 %shl
328 }
329
330 ; ...and again with i64.
331 define i64 @f29(i64 %foo) {
332 ; CHECK-LABEL: f29:
333 ; CHECK: risbg %r2, %r2, 0, 141, 49
334 ; CHECK: br %r14
335   %and = and i64 %foo, 32766
336   %shl = shl i64 %and, 49
337   ret i64 %shl
338 }
339
340 ; Test the next shift up from f28, in which the mask should get shortened.
341 define i32 @f30(i32 %foo) {
342 ; CHECK-LABEL: f30:
343 ; CHECK: risbg %r2, %r2, 32, 172, 18
344 ; CHECK: br %r14
345   %and = and i32 %foo, 32766
346   %shl = shl i32 %and, 18
347   ret i32 %shl
348 }
349
350 ; ...and again with i64.
351 define i64 @f31(i64 %foo) {
352 ; CHECK-LABEL: f31:
353 ; CHECK: risbg %r2, %r2, 0, 140, 50
354 ; CHECK: br %r14
355   %and = and i64 %foo, 32766
356   %shl = shl i64 %and, 50
357   ret i64 %shl
358 }
359
360 ; Test a wrap-around case in which the shift left comes after the AND.
361 ; We can't use RISBG for the shift in that case.
362 define i32 @f32(i32 %foo) {
363 ; CHECK-LABEL: f32:
364 ; CHECK: sll %r2
365 ; CHECK: br %r14
366   %and = and i32 %foo, -7
367   %shl = shl i32 %and, 10
368   ret i32 %shl
369 }
370
371 ; ...and again with i64.
372 define i64 @f33(i64 %foo) {
373 ; CHECK-LABEL: f33:
374 ; CHECK: sllg %r2
375 ; CHECK: br %r14
376   %and = and i64 %foo, -7
377   %shl = shl i64 %and, 10
378   ret i64 %shl
379 }
380
381 ; Test a case where the AND comes before a shift right.
382 define i32 @f34(i32 %foo) {
383 ; CHECK-LABEL: f34:
384 ; CHECK: risbg %r2, %r2, 57, 191, 55
385 ; CHECK: br %r14
386   %and = and i32 %foo, 65535
387   %shl = lshr i32 %and, 9
388   ret i32 %shl
389 }
390
391 ; ...and again with i64.
392 define i64 @f35(i64 %foo) {
393 ; CHECK-LABEL: f35:
394 ; CHECK: risbg %r2, %r2, 57, 191, 55
395 ; CHECK: br %r14
396   %and = and i64 %foo, 65535
397   %shl = lshr i64 %and, 9
398   ret i64 %shl
399 }
400
401 ; Test a wrap-around case where the AND comes before a shift right.
402 ; We can't use RISBG for the shift in that case.
403 define i32 @f36(i32 %foo) {
404 ; CHECK-LABEL: f36:
405 ; CHECK: srl %r2
406 ; CHECK: br %r14
407   %and = and i32 %foo, -25
408   %shl = lshr i32 %and, 1
409   ret i32 %shl
410 }
411
412 ; ...and again with i64.
413 define i64 @f37(i64 %foo) {
414 ; CHECK-LABEL: f37:
415 ; CHECK: srlg %r2
416 ; CHECK: br %r14
417   %and = and i64 %foo, -25
418   %shl = lshr i64 %and, 1
419   ret i64 %shl
420 }
421
422 ; Test a combination involving a large ASHR and a shift left.  We can't
423 ; use RISBG there.
424 define i64 @f38(i64 %foo) {
425 ; CHECK-LABEL: f38:
426 ; CHECK: srag {{%r[0-5]}}
427 ; CHECK: sllg {{%r[0-5]}}
428 ; CHECK: br %r14
429   %ashr = ashr i64 %foo, 32
430   %shl = shl i64 %ashr, 5
431   ret i64 %shl
432 }
433
434 ; Try a similar thing in which no shifted sign bits are kept.
435 define i64 @f39(i64 %foo, i64 *%dest) {
436 ; CHECK-LABEL: f39:
437 ; CHECK: srag [[REG:%r[01345]]], %r2, 35
438 ; CHECK: risbg %r2, %r2, 33, 189, 31
439 ; CHECK: br %r14
440   %ashr = ashr i64 %foo, 35
441   store i64 %ashr, i64 *%dest
442   %shl = shl i64 %ashr, 2
443   %and = and i64 %shl, 2147483647
444   ret i64 %and
445 }
446
447 ; ...and again with the next highest shift value, where one sign bit is kept.
448 define i64 @f40(i64 %foo, i64 *%dest) {
449 ; CHECK-LABEL: f40:
450 ; CHECK: srag [[REG:%r[01345]]], %r2, 36
451 ; CHECK: risbg %r2, [[REG]], 33, 189, 2
452 ; CHECK: br %r14
453   %ashr = ashr i64 %foo, 36
454   store i64 %ashr, i64 *%dest
455   %shl = shl i64 %ashr, 2
456   %and = and i64 %shl, 2147483647
457   ret i64 %and
458 }
459
460 ; Check a case where the result is zero-extended.
461 define i64 @f41(i32 %a) {
462 ; CHECK-LABEL: f41
463 ; CHECK: risbg %r2, %r2, 36, 191, 62
464 ; CHECK: br %r14
465   %shl = shl i32 %a, 2
466   %shr = lshr i32 %shl, 4
467   %ext = zext i32 %shr to i64
468   ret i64 %ext
469 }
470
471 ; In this case the sign extension is converted to a pair of 32-bit shifts,
472 ; which is then extended to 64 bits.  We previously used the wrong bit size
473 ; when testing whether the shifted-in bits of the shift right were significant.
474 define i64 @f42(i1 %x) {
475 ; CHECK-LABEL: f42:
476 ; CHECK: sll %r2, 31
477 ; CHECK: sra %r2, 31
478 ; CHECK: llgcr %r2, %r2
479 ; CHECK: br %r14
480   %ext = sext i1 %x to i8
481   %ext2 = zext i8 %ext to i64
482   ret i64 %ext2
483 }