1 ; Test that compares are omitted if CC already has the right value
4 ; RUN: llc < %s -mtriple=s390x-linux-gnu -mcpu=z10 -no-integrated-as \
5 ; RUN: -verify-machineinstrs| FileCheck %s
9 ; Addition provides enough for comparisons with zero if we know no
10 ; signed overflow happens, which is when the "nsw" flag is set.
11 ; First test the EQ case.
12 define i32 @f1(i32 %a, i32 %b, ptr %dest) {
14 ; CHECK: afi %r2, 1000000
15 ; CHECK-NEXT: ber %r14
18 %res = add nsw i32 %a, 1000000
19 %cmp = icmp eq i32 %res, 0
20 br i1 %cmp, label %exit, label %store
23 store i32 %b, ptr %dest
30 ; ...and again with NE.
31 define i32 @f2(i32 %a, i32 %b, ptr %dest) {
33 ; CHECK: afi %r2, 1000000
34 ; CHECK-NEXT: blhr %r14
37 %res = add nsw i32 %a, 1000000
38 %cmp = icmp ne i32 %res, 0
39 br i1 %cmp, label %exit, label %store
42 store i32 %b, ptr %dest
49 ; ...and again with SLT.
50 define i32 @f3(i32 %a, i32 %b, ptr %dest) {
52 ; CHECK: afi %r2, 1000000
53 ; CHECK-NEXT: blr %r14
55 %res = add nsw i32 %a, 1000000
56 %cmp = icmp slt i32 %res, 0
57 br i1 %cmp, label %exit, label %store
60 store i32 %b, ptr %dest
67 ; ...and again with SLE.
68 define i32 @f4(i32 %a, i32 %b, ptr %dest) {
70 ; CHECK: afi %r2, 1000000
71 ; CHECK-NEXT: bler %r14
73 %res = add nsw i32 %a, 1000000
74 %cmp = icmp sle i32 %res, 0
75 br i1 %cmp, label %exit, label %store
78 store i32 %b, ptr %dest
85 ; ...and again with SGT.
86 define i32 @f5(i32 %a, i32 %b, ptr %dest) {
88 ; CHECK: afi %r2, 1000000
89 ; CHECK-NEXT: bhr %r14
91 %res = add nsw i32 %a, 1000000
92 %cmp = icmp sgt i32 %res, 0
93 br i1 %cmp, label %exit, label %store
96 store i32 %b, ptr %dest
103 ; ...and again with SGE.
104 define i32 @f6(i32 %a, i32 %b, ptr %dest) {
106 ; CHECK: afi %r2, 1000000
107 ; CHECK-NEXT: bher %r14
109 %res = add nsw i32 %a, 1000000
110 %cmp = icmp sge i32 %res, 0
111 br i1 %cmp, label %exit, label %store
114 store i32 %b, ptr %dest
121 ; Subtraction provides in addition also enough for equality comparisons with
122 ; zero even without "nsw".
123 define i32 @f7(i32 %a, i32 %b, ptr %dest) {
125 ; CHECK: s %r2, 0(%r4)
126 ; CHECK-NEXT: bner %r14
129 %cur = load i32, ptr %dest
130 %res = sub i32 %a, %cur
131 %cmp = icmp ne i32 %res, 0
132 br i1 %cmp, label %exit, label %store
135 store i32 %b, ptr %dest
142 ; ...and again with SLT.
143 define i32 @f8(i32 %a, i32 %b, ptr %dest) {
145 ; CHECK: s %r2, 0(%r4)
146 ; CHECK-NEXT: blr %r14
148 %cur = load i32, ptr %dest
149 %res = sub nsw i32 %a, %cur
150 %cmp = icmp slt i32 %res, 0
151 br i1 %cmp, label %exit, label %store
154 store i32 %b, ptr %dest
161 ; Logic register-register instructions also provide enough for equality
162 ; comparisons with zero.
163 define i32 @f9(i32 %a, i32 %b, ptr %dest) {
166 ; CHECK-NEXT: blr %r14
169 %res = and i32 %a, %b
170 %cmp = icmp ne i32 %res, 0
171 br i1 %cmp, label %exit, label %store
174 store i32 %b, ptr %dest
181 ; ...but not for ordered comparisons.
182 define i32 @f10(i32 %a, i32 %b, ptr %dest) {
185 ; CHECK-NEXT: cibl %r2, 0, 0(%r14)
188 %res = and i32 %a, %b
189 %cmp = icmp slt i32 %res, 0
190 br i1 %cmp, label %exit, label %store
193 store i32 %b, ptr %dest
200 ; Logic register-immediate instructions also provide enough for equality
201 ; comparisons with zero if the immediate covers the whole register.
202 define i32 @f11(i32 %a, i32 %b, ptr %dest) {
204 ; CHECK: nilf %r2, 100000001
205 ; CHECK-NEXT: blr %r14
208 %res = and i32 %a, 100000001
209 %cmp = icmp ne i32 %res, 0
210 br i1 %cmp, label %exit, label %store
213 store i32 %b, ptr %dest
220 ; Partial logic register-immediate instructions do not provide simple
222 define i32 @f12(i32 %a, i32 %b, ptr %dest) {
224 ; CHECK: nill %r2, 65436
225 ; CHECK-NEXT: ciblh %r2, 0, 0(%r14)
228 %res = and i32 %a, -100
229 %cmp = icmp ne i32 %res, 0
230 br i1 %cmp, label %exit, label %store
233 store i32 %b, ptr %dest
240 ; SRA provides the same CC result as a comparison with zero.
241 define i32 @f13(i32 %a, i32 %b, ptr %dest) {
243 ; CHECK: sra %r2, 0(%r3)
244 ; CHECK-NEXT: ber %r14
247 %res = ashr i32 %a, %b
248 %cmp = icmp eq i32 %res, 0
249 br i1 %cmp, label %exit, label %store
252 store i32 %b, ptr %dest
259 ; ...and again with NE.
260 define i32 @f14(i32 %a, i32 %b, ptr %dest) {
262 ; CHECK: sra %r2, 0(%r3)
263 ; CHECK-NEXT: blhr %r14
266 %res = ashr i32 %a, %b
267 %cmp = icmp ne i32 %res, 0
268 br i1 %cmp, label %exit, label %store
271 store i32 %b, ptr %dest
279 define i32 @f15(i32 %a, i32 %b, ptr %dest) {
281 ; CHECK: sra %r2, 0(%r3)
282 ; CHECK-NEXT: blr %r14
285 %res = ashr i32 %a, %b
286 %cmp = icmp slt i32 %res, 0
287 br i1 %cmp, label %exit, label %store
290 store i32 %b, ptr %dest
298 define i32 @f16(i32 %a, i32 %b, ptr %dest) {
300 ; CHECK: sra %r2, 0(%r3)
301 ; CHECK-NEXT: bler %r14
304 %res = ashr i32 %a, %b
305 %cmp = icmp sle i32 %res, 0
306 br i1 %cmp, label %exit, label %store
309 store i32 %b, ptr %dest
317 define i32 @f17(i32 %a, i32 %b, ptr %dest) {
319 ; CHECK: sra %r2, 0(%r3)
320 ; CHECK-NEXT: bhr %r14
323 %res = ashr i32 %a, %b
324 %cmp = icmp sgt i32 %res, 0
325 br i1 %cmp, label %exit, label %store
328 store i32 %b, ptr %dest
336 define i32 @f18(i32 %a, i32 %b, ptr %dest) {
338 ; CHECK: sra %r2, 0(%r3)
339 ; CHECK-NEXT: bher %r14
342 %res = ashr i32 %a, %b
343 %cmp = icmp sge i32 %res, 0
344 br i1 %cmp, label %exit, label %store
347 store i32 %b, ptr %dest
354 ; RISBG provides the same result as a comparison against zero.
356 define i64 @f19(i64 %a, i64 %b, ptr %dest) {
358 ; CHECK: risbg %r2, %r3, 0, 190, 0
359 ; CHECK-NEXT: ber %r14
362 %res = and i64 %b, -2
363 %cmp = icmp eq i64 %res, 0
364 br i1 %cmp, label %exit, label %store
367 store i64 %b, ptr %dest
374 ; ...and the SLT case.
375 define i64 @f20(i64 %a, i64 %b, ptr %dest) {
377 ; CHECK: risbg %r2, %r3, 0, 190, 0
378 ; CHECK-NEXT: blr %r14
381 %res = and i64 %b, -2
382 %cmp = icmp slt i64 %res, 0
383 br i1 %cmp, label %exit, label %store
386 store i64 %b, ptr %dest
393 ; Test a case where the register we're testing is set by a non-CC-clobbering
395 define i32 @f21(i32 %a, i32 %b, ptr %dest) {
397 ; CHECK: afi %r2, 1000000
399 ; CHECK-NEXT: blah %r2
400 ; CHECK-NEXT: #NO_APP
401 ; CHECK-NEXT: cibe %r2, 0, 0(%r14)
404 %add = add i32 %a, 1000000
405 %res = call i32 asm "blah $0", "=r,0" (i32 %add)
406 %cmp = icmp eq i32 %res, 0
407 br i1 %cmp, label %exit, label %store
410 store i32 %b, ptr %dest
417 ; ...and again with a CC-clobbering instruction.
418 define i32 @f22(i32 %a, i32 %b, ptr %dest) {
420 ; CHECK: afi %r2, 1000000
422 ; CHECK-NEXT: blah %r2
423 ; CHECK-NEXT: #NO_APP
424 ; CHECK-NEXT: cibe %r2, 0, 0(%r14)
427 %add = add i32 %a, 1000000
428 %res = call i32 asm "blah $0", "=r,0,~{cc}" (i32 %add)
429 %cmp = icmp eq i32 %res, 0
430 br i1 %cmp, label %exit, label %store
433 store i32 %b, ptr %dest
440 ; Check that stores do not interfere.
441 define i32 @f23(i32 %a, i32 %b, ptr %dest1, ptr %dest2) {
443 ; CHECK: afi %r2, 1000000
444 ; CHECK-NEXT: st %r2, 0(%r4)
445 ; CHECK-NEXT: blhr %r14
448 %res = add nsw i32 %a, 1000000
449 store i32 %res, ptr %dest1
450 %cmp = icmp ne i32 %res, 0
451 br i1 %cmp, label %exit, label %store
454 store i32 %b, ptr %dest2
461 ; Check that calls do interfere.
462 define void @f24(ptr %ptr) {
464 ; CHECK: afi [[REG:%r[0-9]+]], 1000000
465 ; CHECK-NEXT: brasl %r14, foo@PLT
466 ; CHECK-NEXT: cijlh [[REG]], 0, .L{{.*}}
469 %val = load i32, ptr %ptr
470 %xor = xor i32 %val, 1
471 %add = add i32 %xor, 1000000
473 %cmp = icmp eq i32 %add, 0
474 br i1 %cmp, label %store, label %exit, !prof !1
477 store i32 %add, ptr %ptr
484 ; Check that inline asms don't interfere if they don't clobber CC.
485 define void @f25(i32 %a, ptr %ptr) {
487 ; CHECK: afi %r2, 1000000
490 ; CHECK-NEXT: #NO_APP
491 ; CHECK-NEXT: blhr %r14
494 %add = add nsw i32 %a, 1000000
495 call void asm sideeffect "blah", "r"(i32 %add)
496 %cmp = icmp ne i32 %add, 0
497 br i1 %cmp, label %exit, label %store
500 store i32 %add, ptr %ptr
507 ; ...but do interfere if they do clobber CC.
508 define void @f26(i32 %a, ptr %ptr) {
510 ; CHECK: afi %r2, 1000000
513 ; CHECK-NEXT: #NO_APP
514 ; CHECK-NEXT: ciblh %r2, 0, 0(%r14)
517 %add = add i32 %a, 1000000
518 call void asm sideeffect "blah", "r,~{cc}"(i32 %add)
519 %cmp = icmp ne i32 %add, 0
520 br i1 %cmp, label %exit, label %store
523 store i32 %add, ptr %ptr
530 ; Test a case where CC is set based on a different register from the
532 define i32 @f27(i32 %a, i32 %b, ptr %dest1, ptr %dest2) {
534 ; CHECK: afi %r2, 1000000
535 ; CHECK-NEXT: sr %r3, %r2
536 ; CHECK-NEXT: st %r3, 0(%r4)
537 ; CHECK-NEXT: cibe %r2, 0, 0(%r14)
540 %add = add nsw i32 %a, 1000000
541 %sub = sub i32 %b, %add
542 store i32 %sub, ptr %dest1
543 %cmp = icmp eq i32 %add, 0
544 br i1 %cmp, label %exit, label %store
547 store i32 %sub, ptr %dest2
554 ; Make sure that we don't confuse a base register for a destination.
555 define void @f28(i64 %a, ptr %dest) {
557 ; CHECK: xi 0(%r2), 15
558 ; CHECK: cgibe %r2, 0, 0(%r14)
561 %ptr = inttoptr i64 %a to ptr
562 %val = load i8, ptr %ptr
563 %xor = xor i8 %val, 15
564 store i8 %xor, ptr %ptr
565 %cmp = icmp eq i64 %a, 0
566 br i1 %cmp, label %exit, label %store
569 store i64 %a, ptr %dest
576 ; Test that L gets converted to LT where useful.
577 define i32 @f29(i64 %base, i64 %index, ptr %dest) {
579 ; CHECK: lt %r2, 0({{%r2,%r3|%r3,%r2}})
580 ; CHECK-NEXT: bler %r14
583 %add = add i64 %base, %index
584 %ptr = inttoptr i64 %add to ptr
585 %res = load i32, ptr %ptr
586 %cmp = icmp sle i32 %res, 0
587 br i1 %cmp, label %exit, label %store
590 store i32 %res, ptr %dest
597 ; Test that LY gets converted to LT where useful.
598 define i32 @f30(i64 %base, i64 %index, ptr %dest) {
600 ; CHECK: lt %r2, 100000({{%r2,%r3|%r3,%r2}})
601 ; CHECK-NEXT: bler %r14
604 %add1 = add i64 %base, %index
605 %add2 = add i64 %add1, 100000
606 %ptr = inttoptr i64 %add2 to ptr
607 %res = load i32, ptr %ptr
608 %cmp = icmp sle i32 %res, 0
609 br i1 %cmp, label %exit, label %store
612 store i32 %res, ptr %dest
619 ; Test that LG gets converted to LTG where useful.
620 define i64 @f31(i64 %base, i64 %index, ptr %dest) {
622 ; CHECK: ltg %r2, 0({{%r2,%r3|%r3,%r2}})
623 ; CHECK-NEXT: bher %r14
626 %add = add i64 %base, %index
627 %ptr = inttoptr i64 %add to ptr
628 %res = load i64, ptr %ptr
629 %cmp = icmp sge i64 %res, 0
630 br i1 %cmp, label %exit, label %store
633 store i64 %res, ptr %dest
640 ; Test that LGF gets converted to LTGF where useful.
641 define i64 @f32(i64 %base, i64 %index, ptr %dest) {
643 ; CHECK: ltgf %r2, 0({{%r2,%r3|%r3,%r2}})
644 ; CHECK-NEXT: bhr %r14
647 %add = add i64 %base, %index
648 %ptr = inttoptr i64 %add to ptr
649 %val = load i32, ptr %ptr
650 %res = sext i32 %val to i64
651 %cmp = icmp sgt i64 %res, 0
652 br i1 %cmp, label %exit, label %store
655 store i64 %res, ptr %dest
662 ; Test that LR gets converted to LTR where useful.
663 define i32 @f33(i32 %dummy, i32 %val, ptr %dest) {
665 ; CHECK: ltr %r2, %r3
667 ; CHECK-NEXT: blah %r2
668 ; CHECK-NEXT: #NO_APP
669 ; CHECK-NEXT: blr %r14
672 call void asm sideeffect "blah $0", "{r2}"(i32 %val)
673 %cmp = icmp slt i32 %val, 0
674 br i1 %cmp, label %exit, label %store
677 store i32 %val, ptr %dest
684 ; Test that LGR gets converted to LTGR where useful.
685 define i64 @f34(i64 %dummy, i64 %val, ptr %dest) {
687 ; CHECK: ltgr %r2, %r3
689 ; CHECK-NEXT: blah %r2
690 ; CHECK-NEXT: #NO_APP
691 ; CHECK-NEXT: bhr %r14
694 call void asm sideeffect "blah $0", "{r2}"(i64 %val)
695 %cmp = icmp sgt i64 %val, 0
696 br i1 %cmp, label %exit, label %store
699 store i64 %val, ptr %dest
706 ; Test that LGFR gets converted to LTGFR where useful.
707 define i64 @f35(i64 %dummy, i32 %val, ptr %dest) {
709 ; CHECK: ltgfr %r2, %r3
711 ; CHECK-NEXT: blah %r2
712 ; CHECK-NEXT: #NO_APP
713 ; CHECK-NEXT: bhr %r14
716 %ext = sext i32 %val to i64
717 call void asm sideeffect "blah $0", "{r2}"(i64 %ext)
718 %cmp = icmp sgt i64 %ext, 0
719 br i1 %cmp, label %exit, label %store
722 store i64 %ext, ptr %dest
729 ; Test a case where it is the source rather than destination of LR that
731 define i32 @f36(i32 %val, i32 %dummy, ptr %dest) {
733 ; CHECK: ltr %r3, %r2
735 ; CHECK-NEXT: blah %r3
736 ; CHECK-NEXT: #NO_APP
737 ; CHECK-NEXT: blr %r14
740 call void asm sideeffect "blah $0", "{r3}"(i32 %val)
741 %cmp = icmp slt i32 %val, 0
742 br i1 %cmp, label %exit, label %store
745 store i32 %val, ptr %dest
752 ; Test a case where it is the source rather than destination of LGR that
754 define i64 @f37(i64 %val, i64 %dummy, ptr %dest) {
756 ; CHECK: ltgr %r3, %r2
758 ; CHECK-NEXT: blah %r3
759 ; CHECK-NEXT: #NO_APP
760 ; CHECK-NEXT: blr %r14
763 call void asm sideeffect "blah $0", "{r3}"(i64 %val)
764 %cmp = icmp slt i64 %val, 0
765 br i1 %cmp, label %exit, label %store
768 store i64 %val, ptr %dest
775 ; Test a case where it is the source rather than destination of LGFR that
777 define i32 @f38(i32 %val, i64 %dummy, ptr %dest) {
779 ; CHECK: ltgfr %r3, %r2
781 ; CHECK-NEXT: blah %r3
782 ; CHECK-NEXT: #NO_APP
783 ; CHECK-NEXT: blr %r14
786 %ext = sext i32 %val to i64
787 call void asm sideeffect "blah $0", "{r3}"(i64 %ext)
788 %cmp = icmp slt i32 %val, 0
789 br i1 %cmp, label %exit, label %store
792 store i32 %val, ptr %dest
799 ; Test f35 for in-register extensions.
800 define i64 @f39(i64 %dummy, i64 %a, ptr %dest) {
802 ; CHECK: ltgfr %r2, %r3
804 ; CHECK-NEXT: blah %r2
805 ; CHECK-NEXT: #NO_APP
806 ; CHECK-NEXT: bhr %r14
809 %val = trunc i64 %a to i32
810 %ext = sext i32 %val to i64
811 call void asm sideeffect "blah $0", "{r2}"(i64 %ext)
812 %cmp = icmp sgt i64 %ext, 0
813 br i1 %cmp, label %exit, label %store
816 store i64 %ext, ptr %dest
823 ; ...and again with what InstCombine would produce for f40.
824 define i64 @f40(i64 %dummy, i64 %a, ptr %dest) {
826 ; CHECK: ltgfr %r2, %r3
828 ; CHECK-NEXT: blah %r2
829 ; CHECK-NEXT: #NO_APP
830 ; CHECK-NEXT: bhr %r14
833 %shl = shl i64 %a, 32
834 %ext = ashr i64 %shl, 32
835 call void asm sideeffect "blah $0", "{r2}"(i64 %ext)
836 %cmp = icmp sgt i64 %shl, 0
837 br i1 %cmp, label %exit, label %store
840 store i64 %ext, ptr %dest
847 ; Try a form of f7 in which the subtraction operands are compared directly.
848 define i32 @f41(i32 %a, i32 %b, ptr %dest) {
850 ; CHECK: s %r2, 0(%r4)
851 ; CHECK-NEXT: bner %r14
854 %cur = load i32, ptr %dest
855 %res = sub i32 %a, %cur
856 %cmp = icmp ne i32 %a, %cur
857 br i1 %cmp, label %exit, label %store
860 store i32 %b, ptr %dest
867 ; A version of f32 that tests the unextended value.
868 define i64 @f42(i64 %base, i64 %index, ptr %dest) {
870 ; CHECK: ltgf %r2, 0({{%r2,%r3|%r3,%r2}})
871 ; CHECK-NEXT: bhr %r14
874 %add = add i64 %base, %index
875 %ptr = inttoptr i64 %add to ptr
876 %val = load i32, ptr %ptr
877 %res = sext i32 %val to i64
878 %cmp = icmp sgt i32 %val, 0
879 br i1 %cmp, label %exit, label %store
882 store i64 %res, ptr %dest
889 !1 = !{!"branch_weights", i32 2, i32 1}