1 ; RUN: opt < %s -gvn-sink -simplifycfg -simplifycfg-sink-common=false -S | FileCheck %s
3 define zeroext i1 @test1(i1 zeroext %flag, i32 %blksA, i32 %blksB, i32 %nblks) {
5 br i1 %flag, label %if.then, label %if.else
13 %cmp = icmp uge i32 %blksA, %nblks
14 %frombool1 = zext i1 %cmp to i8
18 %add = add i32 %nblks, %blksB
19 %cmp2 = icmp ule i32 %add, %blksA
20 %frombool3 = zext i1 %cmp2 to i8
24 %obeys.0 = phi i8 [ %frombool1, %if.then ], [ %frombool3, %if.else ]
25 %tobool4 = icmp ne i8 %obeys.0, 0
29 define zeroext i1 @test2(i1 zeroext %flag, i32 %blksA, i32 %blksB, i32 %nblks) {
31 br i1 %flag, label %if.then, label %if.else
39 %cmp = icmp uge i32 %blksA, %nblks
40 %frombool1 = zext i1 %cmp to i8
44 %add = add i32 %nblks, %blksB
45 %cmp2 = icmp uge i32 %blksA, %add
46 %frombool3 = zext i1 %cmp2 to i8
50 %obeys.0 = phi i8 [ %frombool1, %if.then ], [ %frombool3, %if.else ]
51 %tobool4 = icmp ne i8 %obeys.0, 0
55 declare i32 @foo(i32, i32) nounwind readnone
57 ; FIXME: The test failes when the original order of the
58 ; candidates with the same cost is preserved.
60 ;define i32 @test3(i1 zeroext %flag, i32 %x, i32 %y) {
62 ; br i1 %flag, label %if.then, label %if.else
65 ; %x0 = call i32 @foo(i32 %x, i32 0) nounwind readnone
66 ; %y0 = call i32 @foo(i32 %x, i32 1) nounwind readnone
70 ; %x1 = call i32 @foo(i32 %y, i32 0) nounwind readnone
71 ; %y1 = call i32 @foo(i32 %y, i32 1) nounwind readnone
75 ; %xx = phi i32 [ %x0, %if.then ], [ %x1, %if.else ]
76 ; %yy = phi i32 [ %y0, %if.then ], [ %y1, %if.else ]
77 ; %ret = add i32 %xx, %yy
88 define i32 @test4(i1 zeroext %flag, i32 %x, i32* %y) {
90 br i1 %flag, label %if.then, label %if.else
111 define i32 @test5(i1 zeroext %flag, i32 %x, i32* %y) {
113 br i1 %flag, label %if.then, label %if.else
117 store volatile i32 %a, i32* %y
122 store i32 %b, i32* %y
130 ; CHECK: store volatile
133 define i32 @test6(i1 zeroext %flag, i32 %x, i32* %y) {
135 br i1 %flag, label %if.then, label %if.else
139 store volatile i32 %a, i32* %y
144 store volatile i32 %b, i32* %y
153 ; CHECK: store volatile
156 define i32 @test7(i1 zeroext %flag, i32 %x, i32* %y) {
158 br i1 %flag, label %if.then, label %if.else
161 %z = load volatile i32, i32* %y
163 store volatile i32 %a, i32* %y
167 %w = load volatile i32, i32* %y
169 store volatile i32 %b, i32* %y
178 ; CHECK-DAG: load volatile
179 ; CHECK: store volatile
183 ; The extra store in %if.then means %z and %w are not equivalent.
184 define i32 @test9(i1 zeroext %flag, i32 %x, i32* %y, i32* %p) {
186 br i1 %flag, label %if.then, label %if.else
190 %z = load volatile i32, i32* %y
193 store volatile i32 %a, i32* %y
197 %w = load volatile i32, i32* %y
199 store volatile i32 %b, i32* %y
210 %struct.anon = type { i32, i32 }
212 ; The GEP indexes a struct type so cannot have a variable last index.
213 define i32 @test10(i1 zeroext %flag, i32 %x, i32* %y, %struct.anon* %s) {
215 br i1 %flag, label %if.then, label %if.else
218 %dummy = add i32 %x, 5
219 %gepa = getelementptr inbounds %struct.anon, %struct.anon* %s, i32 0, i32 0
220 store volatile i32 %x, i32* %gepa
224 %dummy1 = add i32 %x, 6
225 %gepb = getelementptr inbounds %struct.anon, %struct.anon* %s, i32 0, i32 1
226 store volatile i32 %x, i32* %gepb
233 ; CHECK-LABEL: test10
234 ; CHECK: getelementptr
235 ; CHECK: store volatile
236 ; CHECK: getelementptr
237 ; CHECK: store volatile
239 ; The shufflevector's mask operand cannot be merged in a PHI.
240 define i32 @test11(i1 zeroext %flag, i32 %w, <2 x i32> %x, <2 x i32> %y) {
242 br i1 %flag, label %if.then, label %if.else
245 %dummy = add i32 %w, 5
246 %sv1 = shufflevector <2 x i32> %x, <2 x i32> %y, <2 x i32> <i32 0, i32 1>
250 %dummy1 = add i32 %w, 6
251 %sv2 = shufflevector <2 x i32> %x, <2 x i32> %y, <2 x i32> <i32 1, i32 0>
255 %p = phi <2 x i32> [ %sv1, %if.then ], [ %sv2, %if.else ]
259 ; CHECK-LABEL: test11
260 ; CHECK: shufflevector
261 ; CHECK: shufflevector
263 ; We can't common an intrinsic!
264 define i32 @test12(i1 zeroext %flag, i32 %w, i32 %x, i32 %y) {
266 br i1 %flag, label %if.then, label %if.else
269 %dummy = add i32 %w, 5
270 %sv1 = call i32 @llvm.ctlz.i32(i32 %x)
274 %dummy1 = add i32 %w, 6
275 %sv2 = call i32 @llvm.cttz.i32(i32 %x)
279 %p = phi i32 [ %sv1, %if.then ], [ %sv2, %if.else ]
283 declare i32 @llvm.ctlz.i32(i32 %x) readnone
284 declare i32 @llvm.cttz.i32(i32 %x) readnone
286 ; CHECK-LABEL: test12
287 ; CHECK: call i32 @llvm.ctlz
288 ; CHECK: call i32 @llvm.cttz
290 ; The TBAA metadata should be properly combined.
291 define i32 @test13(i1 zeroext %flag, i32 %x, i32* %y) {
293 br i1 %flag, label %if.then, label %if.else
296 %z = load volatile i32, i32* %y
298 store volatile i32 %a, i32* %y, !tbaa !3
302 %w = load volatile i32, i32* %y
304 store volatile i32 %b, i32* %y, !tbaa !4
311 !0 = !{ !"an example type tree" }
313 !2 = !{ !"float", !0 }
314 !3 = !{ !"const float", !2, i64 0 }
315 !4 = !{ !"special float", !2, i64 1 }
317 ; CHECK-LABEL: test13
319 ; CHECK-DAG: load volatile
320 ; CHECK: store volatile {{.*}}, !tbaa !0
324 ; The call should be commoned.
325 define i32 @test13a(i1 zeroext %flag, i32 %w, i32 %x, i32 %y) {
327 br i1 %flag, label %if.then, label %if.else
330 %sv1 = call i32 @bar(i32 %x)
334 %sv2 = call i32 @bar(i32 %y)
338 %p = phi i32 [ %sv1, %if.then ], [ %sv2, %if.else ]
341 declare i32 @bar(i32)
343 ; CHECK-LABEL: test13a
344 ; CHECK: %[[x:.*]] = select i1 %flag
345 ; CHECK: call i32 @bar(i32 %[[x]])
347 ; The load should be commoned.
348 define i32 @test14(i1 zeroext %flag, i32 %w, i32 %x, i32 %y, %struct.anon* %s) {
350 br i1 %flag, label %if.then, label %if.else
353 %dummy = add i32 %x, 1
354 %gepa = getelementptr inbounds %struct.anon, %struct.anon* %s, i32 0, i32 1
355 %sv1 = load i32, i32* %gepa
356 %cmp1 = icmp eq i32 %sv1, 56
360 %dummy2 = add i32 %x, 4
361 %gepb = getelementptr inbounds %struct.anon, %struct.anon* %s, i32 0, i32 1
362 %sv2 = load i32, i32* %gepb
363 %cmp2 = icmp eq i32 %sv2, 57
367 %p = phi i1 [ %cmp1, %if.then ], [ %cmp2, %if.else ]
371 ; CHECK-LABEL: test14
372 ; CHECK: getelementptr
376 ; The load should be commoned.
377 define i32 @test15(i1 zeroext %flag, i32 %w, i32 %x, i32 %y, %struct.anon* %s) {
379 br i1 %flag, label %if.then, label %if.else
382 %dummy = add i32 %x, 1
383 %gepa = getelementptr inbounds %struct.anon, %struct.anon* %s, i32 0, i32 0
384 %sv1 = load i32, i32* %gepa
385 %ext1 = zext i32 %sv1 to i64
386 %cmp1 = icmp eq i64 %ext1, 56
390 %dummy2 = add i32 %x, 4
391 %gepb = getelementptr inbounds %struct.anon, %struct.anon* %s, i32 0, i32 1
392 %sv2 = load i32, i32* %gepb
393 %ext2 = zext i32 %sv2 to i64
394 %cmp2 = icmp eq i64 %ext2, 56
398 %p = phi i1 [ %cmp1, %if.then ], [ %cmp2, %if.else ]
402 ; CHECK-LABEL: test15
403 ; CHECK: getelementptr
407 define zeroext i1 @test_crash(i1 zeroext %flag, i32* %i4, i32* %m, i32* %n) {
409 br i1 %flag, label %if.then, label %if.else
412 %tmp1 = load i32, i32* %i4
413 %tmp2 = add i32 %tmp1, -1
414 store i32 %tmp2, i32* %i4
418 %tmp3 = load i32, i32* %m
419 %tmp4 = load i32, i32* %n
420 %tmp5 = add i32 %tmp3, %tmp4
421 store i32 %tmp5, i32* %i4
428 ; CHECK-LABEL: test_crash
429 ; No checks for test_crash - just ensure it doesn't crash!
431 define zeroext i1 @test16(i1 zeroext %flag, i1 zeroext %flag2, i32 %blksA, i32 %blksB, i32 %nblks) {
434 br i1 %flag, label %if.then, label %if.else
437 %cmp = icmp uge i32 %blksA, %nblks
438 %frombool1 = zext i1 %cmp to i8
442 br i1 %flag2, label %if.then2, label %if.end
445 %add = add i32 %nblks, %blksB
446 %cmp2 = icmp ule i32 %add, %blksA
447 %frombool3 = zext i1 %cmp2 to i8
451 %obeys.0 = phi i8 [ %frombool1, %if.then ], [ %frombool3, %if.then2 ], [ 0, %if.else ]
452 %tobool4 = icmp ne i8 %obeys.0, 0
456 ; CHECK-LABEL: test16
460 define zeroext i1 @test16a(i1 zeroext %flag, i1 zeroext %flag2, i32 %blksA, i32 %blksB, i32 %nblks, i8* %p) {
463 br i1 %flag, label %if.then, label %if.else
466 %cmp = icmp uge i32 %blksA, %nblks
467 %frombool1 = zext i1 %cmp to i8
468 %b1 = sext i8 %frombool1 to i32
469 %b2 = trunc i32 %b1 to i8
474 br i1 %flag2, label %if.then2, label %if.end
477 %add = add i32 %nblks, %blksB
478 %cmp2 = icmp ule i32 %add, %blksA
479 %frombool3 = zext i1 %cmp2 to i8
480 %a1 = sext i8 %frombool3 to i32
481 %a2 = trunc i32 %a1 to i8
489 ; CHECK-LABEL: test16a
493 define zeroext i1 @test17(i32 %flag, i32 %blksA, i32 %blksB, i32 %nblks) {
495 switch i32 %flag, label %if.end [
496 i32 0, label %if.then
497 i32 1, label %if.then2
501 %cmp = icmp uge i32 %blksA, %nblks
502 %frombool1 = call i8 @i1toi8(i1 %cmp)
503 %a1 = sext i8 %frombool1 to i32
504 %a2 = trunc i32 %a1 to i8
508 %add = add i32 %nblks, %blksB
509 %cmp2 = icmp ule i32 %add, %blksA
510 %frombool3 = call i8 @i1toi8(i1 %cmp2)
511 %b1 = sext i8 %frombool3 to i32
512 %b2 = trunc i32 %b1 to i8
516 %obeys.0 = phi i8 [ %a2, %if.then ], [ %b2, %if.then2 ], [ 0, %entry ]
517 %tobool4 = icmp ne i8 %obeys.0, 0
520 declare i8 @i1toi8(i1)
522 ; FIXME: DISABLED - we don't consider this profitable. We should
523 ; - Consider argument setup/return mov'ing for calls, like InlineCost does.
524 ; - Consider the removal of the %obeys.0 PHI (zero PHI movement overall)
526 ; DISABLED-CHECK-LABEL: test17
527 ; DISABLED-CHECK: if.then:
528 ; DISABLED-CHECK-NEXT: icmp uge
529 ; DISABLED-CHECK-NEXT: br label %[[x:.*]]
531 ; DISABLED-CHECK: if.then2:
532 ; DISABLED-CHECK-NEXT: add
533 ; DISABLED-CHECK-NEXT: icmp ule
534 ; DISABLED-CHECK-NEXT: br label %[[x]]
536 ; DISABLED-CHECK: [[x]]:
537 ; DISABLED-CHECK-NEXT: %[[y:.*]] = phi i1 [ %cmp
538 ; DISABLED-CHECK-NEXT: %[[z:.*]] = call i8 @i1toi8(i1 %[[y]])
539 ; DISABLED-CHECK-NEXT: br label %if.end
541 ; DISABLED-CHECK: if.end:
542 ; DISABLED-CHECK-NEXT: phi i8
543 ; DISABLED-CHECK-DAG: [ %[[z]], %[[x]] ]
544 ; DISABLED-CHECK-DAG: [ 0, %entry ]
546 define zeroext i1 @test18(i32 %flag, i32 %blksA, i32 %blksB, i32 %nblks) {
548 switch i32 %flag, label %if.then3 [
549 i32 0, label %if.then
550 i32 1, label %if.then2
554 %cmp = icmp uge i32 %blksA, %nblks
555 %frombool1 = zext i1 %cmp to i8
559 %add = add i32 %nblks, %blksB
560 %cmp2 = icmp ule i32 %add, %blksA
561 %frombool3 = zext i1 %cmp2 to i8
565 %add2 = add i32 %nblks, %blksA
566 %cmp3 = icmp ule i32 %add2, %blksA
567 %frombool4 = zext i1 %cmp3 to i8
571 %obeys.0 = phi i8 [ %frombool1, %if.then ], [ %frombool3, %if.then2 ], [ %frombool4, %if.then3 ]
572 %tobool4 = icmp ne i8 %obeys.0, 0
576 ; CHECK-LABEL: test18
578 ; CHECK-NEXT: %[[x:.*]] = phi i1
579 ; CHECK-DAG: [ %cmp, %if.then ]
580 ; CHECK-DAG: [ %cmp2, %if.then2 ]
581 ; CHECK-DAG: [ %cmp3, %if.then3 ]
582 ; CHECK-NEXT: zext i1 %[[x]] to i8
584 ; The phi is confusing - both add instructions are used by it, but
585 ; not on their respective unconditional arcs. It should not be
587 define void @test_pr30292(i1 %cond, i1 %cond2, i32 %a, i32 %b) {
589 %add1 = add i32 %a, 1
593 br i1 %cond, label %two, label %succ
597 %add2 = add i32 %a, 1
601 %p = phi i32 [ 0, %entry ], [ %add1, %one ], [ %add2, %two ]
606 ; CHECK-LABEL: test_pr30292
607 ; CHECK: phi i32 [ 0, %entry ], [ %add1, %succ ], [ %add2, %two ]
609 define zeroext i1 @test_pr30244(i1 zeroext %flag, i1 zeroext %flag2, i32 %blksA, i32 %blksB, i32 %nblks) {
613 br i1 %flag, label %if.then, label %if.else
616 %cmp = icmp uge i32 %blksA, %nblks
617 %frombool1 = zext i1 %cmp to i8
618 store i8 %frombool1, i8* %p
622 br i1 %flag2, label %if.then2, label %if.end
625 %add = add i32 %nblks, %blksB
626 %cmp2 = icmp ule i32 %add, %blksA
627 %frombool3 = zext i1 %cmp2 to i8
628 store i8 %frombool3, i8* %p
635 ; CHECK-LABEL: @test_pr30244
639 define i32 @test_pr30373a(i1 zeroext %flag, i32 %x, i32 %y) {
641 br i1 %flag, label %if.then, label %if.else
644 %x0 = call i32 @foo(i32 %x, i32 0) nounwind readnone
645 %y0 = call i32 @foo(i32 %x, i32 1) nounwind readnone
646 %z0 = lshr i32 %y0, 8
650 %x1 = call i32 @foo(i32 %y, i32 0) nounwind readnone
651 %y1 = call i32 @foo(i32 %y, i32 1) nounwind readnone
652 %z1 = lshr exact i32 %y1, 8
656 %xx = phi i32 [ %x0, %if.then ], [ %x1, %if.else ]
657 %yy = phi i32 [ %z0, %if.then ], [ %z1, %if.else ]
658 %ret = add i32 %xx, %yy
662 ; CHECK-LABEL: test_pr30373a
667 define i32 @test_pr30373b(i1 zeroext %flag, i32 %x, i32 %y) {
669 br i1 %flag, label %if.then, label %if.else
672 %x0 = call i32 @foo(i32 %x, i32 0) nounwind readnone
673 %y0 = call i32 @foo(i32 %x, i32 1) nounwind readnone
674 %z0 = lshr exact i32 %y0, 8
678 %x1 = call i32 @foo(i32 %y, i32 0) nounwind readnone
679 %y1 = call i32 @foo(i32 %y, i32 1) nounwind readnone
680 %z1 = lshr i32 %y1, 8
684 %xx = phi i32 [ %x0, %if.then ], [ %x1, %if.else ]
685 %yy = phi i32 [ %z0, %if.then ], [ %z1, %if.else ]
686 %ret = add i32 %xx, %yy
690 ; CHECK-LABEL: test_pr30373b
695 ; CHECK: !0 = !{!1, !1, i64 0}
696 ; CHECK: !1 = !{!"float", !2}
697 ; CHECK: !2 = !{!"an example type tree"}