1 ; RUN: opt < %s -simplifycfg -sink-common-insts -S | FileCheck -enable-var-scope %s
2 ; RUN: opt < %s -passes='simplify-cfg<sink-common-insts>' -S | FileCheck -enable-var-scope %s
4 define zeroext i1 @test1(i1 zeroext %flag, i32 %blksA, i32 %blksB, i32 %nblks) {
6 br i1 %flag, label %if.then, label %if.else
14 %cmp = icmp uge i32 %blksA, %nblks
15 %frombool1 = zext i1 %cmp to i8
19 %add = add i32 %nblks, %blksB
20 %cmp2 = icmp ule i32 %add, %blksA
21 %frombool3 = zext i1 %cmp2 to i8
25 %obeys.0 = phi i8 [ %frombool1, %if.then ], [ %frombool3, %if.else ]
26 %tobool4 = icmp ne i8 %obeys.0, 0
30 define zeroext i1 @test2(i1 zeroext %flag, i32 %blksA, i32 %blksB, i32 %nblks) {
32 br i1 %flag, label %if.then, label %if.else
40 %cmp = icmp uge i32 %blksA, %nblks
41 %frombool1 = zext i1 %cmp to i8
45 %add = add i32 %nblks, %blksB
46 %cmp2 = icmp uge i32 %blksA, %add
47 %frombool3 = zext i1 %cmp2 to i8
51 %obeys.0 = phi i8 [ %frombool1, %if.then ], [ %frombool3, %if.else ]
52 %tobool4 = icmp ne i8 %obeys.0, 0
56 declare i32 @foo(i32, i32) nounwind readnone
58 define i32 @test3(i1 zeroext %flag, i32 %x, i32 %y) {
60 br i1 %flag, label %if.then, label %if.else
63 %x0 = call i32 @foo(i32 %x, i32 0) nounwind readnone
64 %y0 = call i32 @foo(i32 %x, i32 1) nounwind readnone
68 %x1 = call i32 @foo(i32 %y, i32 0) nounwind readnone
69 %y1 = call i32 @foo(i32 %y, i32 1) nounwind readnone
73 %xx = phi i32 [ %x0, %if.then ], [ %x1, %if.else ]
74 %yy = phi i32 [ %y0, %if.then ], [ %y1, %if.else ]
75 %ret = add i32 %xx, %yy
86 define i32 @test4(i1 zeroext %flag, i32 %x, i32* %y) {
88 br i1 %flag, label %if.then, label %if.else
109 define i32 @test5(i1 zeroext %flag, i32 %x, i32* %y) {
111 br i1 %flag, label %if.then, label %if.else
115 store volatile i32 %a, i32* %y
120 store i32 %b, i32* %y
128 ; CHECK: store volatile
131 define i32 @test6(i1 zeroext %flag, i32 %x, i32* %y) {
133 br i1 %flag, label %if.then, label %if.else
137 store volatile i32 %a, i32* %y
142 store volatile i32 %b, i32* %y
151 ; CHECK: store volatile
154 define i32 @test7(i1 zeroext %flag, i32 %x, i32* %y) {
156 br i1 %flag, label %if.then, label %if.else
159 %z = load volatile i32, i32* %y
161 store volatile i32 %a, i32* %y
165 %w = load volatile i32, i32* %y
167 store volatile i32 %b, i32* %y
176 ; CHECK-DAG: load volatile
177 ; CHECK: store volatile
181 ; %z and %w are in different blocks. We shouldn't sink the add because
182 ; there may be intervening memory instructions.
183 define i32 @test8(i1 zeroext %flag, i32 %x, i32* %y) {
185 %z = load volatile i32, i32* %y
186 br i1 %flag, label %if.then, label %if.else
190 store volatile i32 %a, i32* %y
194 %w = load volatile i32, i32* %y
196 store volatile i32 %b, i32* %y
207 ; The extra store in %if.then means %z and %w are not equivalent.
208 define i32 @test9(i1 zeroext %flag, i32 %x, i32* %y, i32* %p) {
210 br i1 %flag, label %if.then, label %if.else
214 %z = load volatile i32, i32* %y
217 store volatile i32 %a, i32* %y
221 %w = load volatile i32, i32* %y
223 store volatile i32 %b, i32* %y
234 %struct.anon = type { i32, i32 }
236 ; The GEP indexes a struct type so cannot have a variable last index.
237 define i32 @test10(i1 zeroext %flag, i32 %x, i32* %y, %struct.anon* %s) {
239 br i1 %flag, label %if.then, label %if.else
242 %dummy = add i32 %x, 5
243 %gepa = getelementptr inbounds %struct.anon, %struct.anon* %s, i32 0, i32 0
244 store volatile i32 %x, i32* %gepa
248 %dummy1 = add i32 %x, 6
249 %gepb = getelementptr inbounds %struct.anon, %struct.anon* %s, i32 0, i32 1
250 store volatile i32 %x, i32* %gepb
257 ; CHECK-LABEL: test10
258 ; CHECK: getelementptr
259 ; CHECK: getelementptr
261 ; CHECK: store volatile
263 ; The shufflevector's mask operand cannot be merged in a PHI.
264 define i32 @test11(i1 zeroext %flag, i32 %w, <2 x i32> %x, <2 x i32> %y) {
266 br i1 %flag, label %if.then, label %if.else
269 %dummy = add i32 %w, 5
270 %sv1 = shufflevector <2 x i32> %x, <2 x i32> %y, <2 x i32> <i32 0, i32 1>
274 %dummy1 = add i32 %w, 6
275 %sv2 = shufflevector <2 x i32> %x, <2 x i32> %y, <2 x i32> <i32 1, i32 0>
279 %p = phi <2 x i32> [ %sv1, %if.then ], [ %sv2, %if.else ]
283 ; CHECK-LABEL: test11
284 ; CHECK: shufflevector
285 ; CHECK: shufflevector
287 ; We can't common an intrinsic!
288 define i32 @test12(i1 zeroext %flag, i32 %w, i32 %x, i32 %y) {
290 br i1 %flag, label %if.then, label %if.else
293 %dummy = add i32 %w, 5
294 %sv1 = call i32 @llvm.ctlz.i32(i32 %x)
298 %dummy1 = add i32 %w, 6
299 %sv2 = call i32 @llvm.cttz.i32(i32 %x)
303 %p = phi i32 [ %sv1, %if.then ], [ %sv2, %if.else ]
307 declare i32 @llvm.ctlz.i32(i32 %x) readnone
308 declare i32 @llvm.cttz.i32(i32 %x) readnone
310 ; CHECK-LABEL: test12
311 ; CHECK: call i32 @llvm.ctlz
312 ; CHECK: call i32 @llvm.cttz
314 ; The TBAA metadata should be properly combined.
315 define i32 @test13(i1 zeroext %flag, i32 %x, i32* %y) {
317 br i1 %flag, label %if.then, label %if.else
320 %z = load volatile i32, i32* %y
322 store volatile i32 %a, i32* %y, !tbaa !3
326 %w = load volatile i32, i32* %y
328 store volatile i32 %b, i32* %y, !tbaa !4
335 !0 = !{ !"an example type tree" }
337 !2 = !{ !"float", !0 }
338 !3 = !{ !"const float", !2, i64 0 }
339 !4 = !{ !"special float", !2, i64 1 }
341 ; CHECK-LABEL: test13
343 ; CHECK-DAG: load volatile
344 ; CHECK: store volatile {{.*}}, !tbaa ![[$TBAA:[0-9]]]
348 ; The call should be commoned.
349 define i32 @test13a(i1 zeroext %flag, i32 %w, i32 %x, i32 %y) {
351 br i1 %flag, label %if.then, label %if.else
354 %sv1 = call i32 @bar(i32 %x)
358 %sv2 = call i32 @bar(i32 %y)
362 %p = phi i32 [ %sv1, %if.then ], [ %sv2, %if.else ]
365 declare i32 @bar(i32)
367 ; CHECK-LABEL: test13a
368 ; CHECK: %[[x:.*]] = select i1 %flag
369 ; CHECK: call i32 @bar(i32 %[[x]])
371 ; The load should be commoned.
372 define i32 @test14(i1 zeroext %flag, i32 %w, i32 %x, i32 %y, %struct.anon* %s) {
374 br i1 %flag, label %if.then, label %if.else
377 %dummy = add i32 %x, 1
378 %gepa = getelementptr inbounds %struct.anon, %struct.anon* %s, i32 0, i32 1
379 %sv1 = load i32, i32* %gepa
380 %cmp1 = icmp eq i32 %sv1, 56
384 %dummy2 = add i32 %x, 4
385 %gepb = getelementptr inbounds %struct.anon, %struct.anon* %s, i32 0, i32 1
386 %sv2 = load i32, i32* %gepb
387 %cmp2 = icmp eq i32 %sv2, 57
388 call void @llvm.dbg.value(metadata i32 0, metadata !9, metadata !DIExpression()), !dbg !11
392 %p = phi i1 [ %cmp1, %if.then ], [ %cmp2, %if.else ]
396 declare void @llvm.dbg.value(metadata, metadata, metadata)
397 !llvm.module.flags = !{!5, !6}
400 !5 = !{i32 2, !"Dwarf Version", i32 4}
401 !6 = !{i32 2, !"Debug Info Version", i32 3}
402 !7 = distinct !DICompileUnit(language: DW_LANG_C99, file: !10)
403 !8 = distinct !DISubprogram(name: "foo", unit: !7)
404 !9 = !DILocalVariable(name: "b", line: 1, arg: 2, scope: !8)
405 !10 = !DIFile(filename: "a.c", directory: "a/b")
406 !11 = !DILocation(line: 1, column: 14, scope: !8)
408 ; CHECK-LABEL: test14
409 ; CHECK: getelementptr
413 ; The load should be commoned.
414 define i32 @test15(i1 zeroext %flag, i32 %w, i32 %x, i32 %y, %struct.anon* %s) {
416 br i1 %flag, label %if.then, label %if.else
419 %dummy = add i32 %x, 1
420 %gepa = getelementptr inbounds %struct.anon, %struct.anon* %s, i32 0, i32 0
421 %sv1 = load i32, i32* %gepa
422 %ext1 = zext i32 %sv1 to i64
423 %cmp1 = icmp eq i64 %ext1, 56
427 %dummy2 = add i32 %x, 4
428 %gepb = getelementptr inbounds %struct.anon, %struct.anon* %s, i32 0, i32 1
429 %sv2 = load i32, i32* %gepb
430 %ext2 = zext i32 %sv2 to i64
431 %cmp2 = icmp eq i64 %ext2, 57
435 %p = phi i1 [ %cmp1, %if.then ], [ %cmp2, %if.else ]
439 ; CHECK-LABEL: test15
440 ; CHECK: getelementptr
444 define zeroext i1 @test_crash(i1 zeroext %flag, i32* %i4, i32* %m, i32* %n) {
446 br i1 %flag, label %if.then, label %if.else
449 %tmp1 = load i32, i32* %i4
450 %tmp2 = add i32 %tmp1, -1
451 store i32 %tmp2, i32* %i4
455 %tmp3 = load i32, i32* %m
456 %tmp4 = load i32, i32* %n
457 %tmp5 = add i32 %tmp3, %tmp4
458 store i32 %tmp5, i32* %i4
465 ; CHECK-LABEL: test_crash
466 ; No checks for test_crash - just ensure it doesn't crash!
468 define zeroext i1 @test16(i1 zeroext %flag, i1 zeroext %flag2, i32 %blksA, i32 %blksB, i32 %nblks) {
471 br i1 %flag, label %if.then, label %if.else
474 %cmp = icmp uge i32 %blksA, %nblks
475 %frombool1 = zext i1 %cmp to i8
479 br i1 %flag2, label %if.then2, label %if.end
482 %add = add i32 %nblks, %blksB
483 %cmp2 = icmp ule i32 %add, %blksA
484 %frombool3 = zext i1 %cmp2 to i8
488 %obeys.0 = phi i8 [ %frombool1, %if.then ], [ %frombool3, %if.then2 ], [ 0, %if.else ]
489 %tobool4 = icmp ne i8 %obeys.0, 0
493 ; CHECK-LABEL: test16
497 define zeroext i1 @test16a(i1 zeroext %flag, i1 zeroext %flag2, i32 %blksA, i32 %blksB, i32 %nblks, i8* %p) {
500 br i1 %flag, label %if.then, label %if.else
503 %cmp = icmp uge i32 %blksA, %nblks
504 %frombool1 = zext i1 %cmp to i8
505 store i8 %frombool1, i8* %p
509 br i1 %flag2, label %if.then2, label %if.end
512 %add = add i32 %nblks, %blksB
513 %cmp2 = icmp ule i32 %add, %blksA
514 %frombool3 = zext i1 %cmp2 to i8
515 store i8 %frombool3, i8* %p
522 ; CHECK-LABEL: test16a
526 define zeroext i1 @test17(i32 %flag, i32 %blksA, i32 %blksB, i32 %nblks) {
528 switch i32 %flag, label %if.end [
529 i32 0, label %if.then
530 i32 1, label %if.then2
534 %cmp = icmp uge i32 %blksA, %nblks
535 %frombool1 = call i8 @i1toi8(i1 %cmp)
539 %add = add i32 %nblks, %blksB
540 %cmp2 = icmp ule i32 %add, %blksA
541 %frombool3 = call i8 @i1toi8(i1 %cmp2)
545 %obeys.0 = phi i8 [ %frombool1, %if.then ], [ %frombool3, %if.then2 ], [ 0, %entry ]
546 %tobool4 = icmp ne i8 %obeys.0, 0
549 declare i8 @i1toi8(i1)
551 ; CHECK-LABEL: test17
553 ; CHECK-NEXT: icmp uge
554 ; CHECK-NEXT: br label %[[x:.*]]
558 ; CHECK-NEXT: icmp ule
559 ; CHECK-NEXT: br label %[[x]]
562 ; CHECK-NEXT: %[[y:.*]] = phi i1 [ %cmp
563 ; CHECK-NEXT: %[[z:.*]] = call i8 @i1toi8(i1 %[[y]])
564 ; CHECK-NEXT: br label %if.end
568 ; CHECK-DAG: [ %[[z]], %[[x]] ]
569 ; CHECK-DAG: [ 0, %entry ]
571 define zeroext i1 @test18(i32 %flag, i32 %blksA, i32 %blksB, i32 %nblks) {
573 switch i32 %flag, label %if.then3 [
574 i32 0, label %if.then
575 i32 1, label %if.then2
579 %cmp = icmp uge i32 %blksA, %nblks
580 %frombool1 = zext i1 %cmp to i8
584 %add = add i32 %nblks, %blksB
585 %cmp2 = icmp ule i32 %add, %blksA
586 %frombool3 = zext i1 %cmp2 to i8
590 %add2 = add i32 %nblks, %blksA
591 %cmp3 = icmp ule i32 %add2, %blksA
592 %frombool4 = zext i1 %cmp3 to i8
596 %obeys.0 = phi i8 [ %frombool1, %if.then ], [ %frombool3, %if.then2 ], [ %frombool4, %if.then3 ]
597 %tobool4 = icmp ne i8 %obeys.0, 0
601 ; CHECK-LABEL: test18
603 ; CHECK-NEXT: %[[x:.*]] = phi i1
604 ; CHECK-DAG: [ %cmp, %if.then ]
605 ; CHECK-DAG: [ %cmp2, %if.then2 ]
606 ; CHECK-DAG: [ %cmp3, %if.then3 ]
607 ; CHECK-NEXT: zext i1 %[[x]] to i8
609 define i32 @test_pr30188(i1 zeroext %flag, i32 %x) {
613 br i1 %flag, label %if.then, label %if.else
616 store i32 %x, i32* %y
620 store i32 %x, i32* %z
627 ; CHECK-LABEL: test_pr30188
632 define i32 @test_pr30188a(i1 zeroext %flag, i32 %x) {
636 br i1 %flag, label %if.then, label %if.else
640 %one = load i32, i32* %y
641 %two = add i32 %one, 2
642 store i32 %two, i32* %y
646 %three = load i32, i32* %z
647 %four = add i32 %three, 2
648 store i32 %four, i32* %y
655 ; CHECK-LABEL: test_pr30188a
661 ; The phi is confusing - both add instructions are used by it, but
662 ; not on their respective unconditional arcs. It should not be
664 define void @test_pr30292(i1 %cond, i1 %cond2, i32 %a, i32 %b) {
666 %add1 = add i32 %a, 1
670 br i1 %cond, label %two, label %succ
674 %add2 = add i32 %a, 1
678 %p = phi i32 [ 0, %entry ], [ %add1, %one ], [ %add2, %two ]
683 ; CHECK-LABEL: test_pr30292
684 ; CHECK: phi i32 [ 0, %entry ], [ %add1, %succ ], [ %add2, %two ]
686 define zeroext i1 @test_pr30244(i1 zeroext %flag, i1 zeroext %flag2, i32 %blksA, i32 %blksB, i32 %nblks) {
690 br i1 %flag, label %if.then, label %if.else
693 %cmp = icmp uge i32 %blksA, %nblks
694 %frombool1 = zext i1 %cmp to i8
695 store i8 %frombool1, i8* %p
699 br i1 %flag2, label %if.then2, label %if.end
702 %add = add i32 %nblks, %blksB
703 %cmp2 = icmp ule i32 %add, %blksA
704 %frombool3 = zext i1 %cmp2 to i8
705 store i8 %frombool3, i8* %p
712 ; CHECK-LABEL: @test_pr30244
716 define i32 @test_pr30373a(i1 zeroext %flag, i32 %x, i32 %y) {
718 br i1 %flag, label %if.then, label %if.else
721 %x0 = call i32 @foo(i32 %x, i32 0) nounwind readnone
722 %y0 = call i32 @foo(i32 %x, i32 1) nounwind readnone
723 %z0 = lshr i32 %y0, 8
727 %x1 = call i32 @foo(i32 %y, i32 0) nounwind readnone
728 %y1 = call i32 @foo(i32 %y, i32 1) nounwind readnone
729 %z1 = lshr exact i32 %y1, 8
733 %xx = phi i32 [ %x0, %if.then ], [ %x1, %if.else ]
734 %yy = phi i32 [ %z0, %if.then ], [ %z1, %if.else ]
735 %ret = add i32 %xx, %yy
739 ; CHECK-LABEL: test_pr30373a
744 define i32 @test_pr30373b(i1 zeroext %flag, i32 %x, i32 %y) {
746 br i1 %flag, label %if.then, label %if.else
749 %x0 = call i32 @foo(i32 %x, i32 0) nounwind readnone
750 %y0 = call i32 @foo(i32 %x, i32 1) nounwind readnone
751 %z0 = lshr exact i32 %y0, 8
755 %x1 = call i32 @foo(i32 %y, i32 0) nounwind readnone
756 %y1 = call i32 @foo(i32 %y, i32 1) nounwind readnone
757 %z1 = lshr i32 %y1, 8
761 %xx = phi i32 [ %x0, %if.then ], [ %x1, %if.else ]
762 %yy = phi i32 [ %z0, %if.then ], [ %z1, %if.else ]
763 %ret = add i32 %xx, %yy
767 ; CHECK-LABEL: test_pr30373b
772 ; Check that simplifycfg doesn't sink and merge inline-asm instructions.
774 define i32 @test_inline_asm1(i32 %c, i32 %r6) {
776 %tobool = icmp eq i32 %c, 0
777 br i1 %tobool, label %if.else, label %if.then
780 %0 = call i32 asm "rorl $2, $0", "=&r,0,n,~{dirflag},~{fpsr},~{flags}"(i32 %r6, i32 8)
784 %1 = call i32 asm "rorl $2, $0", "=&r,0,n,~{dirflag},~{fpsr},~{flags}"(i32 %r6, i32 6)
788 %r6.addr.0 = phi i32 [ %0, %if.then ], [ %1, %if.else ]
792 ; CHECK-LABEL: @test_inline_asm1(
793 ; CHECK: call i32 asm "rorl $2, $0", "=&r,0,n,~{dirflag},~{fpsr},~{flags}"(i32 %r6, i32 8)
794 ; CHECK: call i32 asm "rorl $2, $0", "=&r,0,n,~{dirflag},~{fpsr},~{flags}"(i32 %r6, i32 6)
796 declare i32 @call_target()
798 define void @test_operand_bundles(i1 %cond, i32* %ptr) {
800 br i1 %cond, label %left, label %right
803 %val0 = call i32 @call_target() [ "deopt"(i32 10) ]
804 store i32 %val0, i32* %ptr
808 %val1 = call i32 @call_target() [ "deopt"(i32 20) ]
809 store i32 %val1, i32* %ptr
816 ; CHECK-LABEL: @test_operand_bundles(
818 ; CHECK-NEXT: %val0 = call i32 @call_target() [ "deopt"(i32 10) ]
820 ; CHECK-NEXT: %val1 = call i32 @call_target() [ "deopt"(i32 20) ]
824 define i32 @test_insertvalue(i1 zeroext %flag, %T %P) {
826 br i1 %flag, label %if.then, label %if.else
829 %t1 = insertvalue %T %P, i32 0, 0
833 %t2 = insertvalue %T %P, i32 1, 0
837 %t = phi %T [%t1, %if.then], [%t2, %if.else]
841 ; CHECK-LABEL: @test_insertvalue
844 ; CHECK-NOT: insertvalue
847 declare void @baz(i32)
849 define void @test_sink_void_calls(i32 %x) {
851 switch i32 %x, label %default [
859 call void @baz(i32 12)
862 call void @baz(i32 34)
865 call void @baz(i32 56)
868 call void @baz(i32 78)
871 call void @baz(i32 90)
878 ; Check that the calls get sunk to the return block.
879 ; We would previously not sink calls without uses, see PR41259.
880 ; CHECK-LABEL: @test_sink_void_calls
882 ; CHECK-LABEL: return:
890 ; CHECK: ![[$TBAA]] = !{![[TYPE:[0-9]]], ![[TYPE]], i64 0}
891 ; CHECK: ![[TYPE]] = !{!"float", ![[TEXT:[0-9]]]}
892 ; CHECK: ![[TEXT]] = !{!"an example type tree"}