1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -instcombine -S | FileCheck %s
4 target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128:n8:16:32:64"
6 define i32 @test1(i32 %A, i1 %b) {
9 ; CHECK-NEXT: br i1 [[B:%.*]], label [[BB1:%.*]], label [[BB2:%.*]]
11 ; CHECK-NEXT: ret i32 [[A:%.*]]
13 ; CHECK-NEXT: ret i32 [[A]]
16 br i1 %b, label %BB1, label %BB2
19 ; Combine away one argument PHI nodes
20 %B = phi i32 [ %A, %BB0 ]
27 define i32 @test2(i32 %A, i1 %b) {
28 ; CHECK-LABEL: @test2(
30 ; CHECK-NEXT: br i1 [[B:%.*]], label [[BB1:%.*]], label [[BB2:%.*]]
32 ; CHECK-NEXT: br label [[BB2]]
34 ; CHECK-NEXT: ret i32 [[A:%.*]]
37 br i1 %b, label %BB1, label %BB2
43 ; Combine away PHI nodes with same values
44 %B = phi i32 [ %A, %BB0 ], [ %A, %BB1 ]
48 define i32 @test3(i32 %A, i1 %b) {
49 ; CHECK-LABEL: @test3(
51 ; CHECK-NEXT: br label [[LOOP:%.*]]
53 ; CHECK-NEXT: br i1 [[B:%.*]], label [[LOOP]], label [[EXIT:%.*]]
55 ; CHECK-NEXT: ret i32 [[A:%.*]]
61 ; PHI has same value always.
62 %B = phi i32 [ %A, %BB0 ], [ %B, %Loop ]
63 br i1 %b, label %Loop, label %Exit
69 define i32 @test4(i1 %b) {
70 ; CHECK-LABEL: @test4(
72 ; CHECK-NEXT: ret i32 7
74 ; CHECK-NEXT: br i1 [[B:%.*]], label [[L2:%.*]], label [[LOOP:%.*]]
76 ; CHECK-NEXT: br label [[LOOP]]
82 Loop: ; preds = %L2, %Loop
83 ; PHI has same value always.
84 %B = phi i32 [ %B, %L2 ], [ %B, %Loop ]
85 br i1 %b, label %L2, label %Loop
91 define i32 @test5(i32 %A, i1 %b) {
92 ; CHECK-LABEL: @test5(
94 ; CHECK-NEXT: br label [[LOOP:%.*]]
96 ; CHECK-NEXT: br i1 [[B:%.*]], label [[LOOP]], label [[EXIT:%.*]]
98 ; CHECK-NEXT: ret i32 [[A:%.*]]
103 Loop: ; preds = %Loop, %BB0
104 ; PHI has same value always.
105 %B = phi i32 [ %A, %BB0 ], [ undef, %Loop ]
106 br i1 %b, label %Loop, label %Exit
108 Exit: ; preds = %Loop
112 define i32 @test6(i16 %A, i1 %b) {
113 ; CHECK-LABEL: @test6(
115 ; CHECK-NEXT: br i1 [[B:%.*]], label [[BB1:%.*]], label [[BB2:%.*]]
117 ; CHECK-NEXT: br label [[BB2]]
119 ; CHECK-NEXT: [[B:%.*]] = zext i16 [[A:%.*]] to i32
120 ; CHECK-NEXT: ret i32 [[B]]
123 %X = zext i16 %A to i32
124 br i1 %b, label %BB1, label %BB2
127 %Y = zext i16 %A to i32
131 ;; Suck casts into phi
132 %B = phi i32 [ %X, %BB0 ], [ %Y, %BB1 ]
136 define i32 @test7(i32 %A, i1 %b) {
137 ; CHECK-LABEL: @test7(
139 ; CHECK-NEXT: br label [[LOOP:%.*]]
141 ; CHECK-NEXT: br i1 [[B:%.*]], label [[LOOP]], label [[EXIT:%.*]]
143 ; CHECK-NEXT: ret i32 0
148 Loop: ; preds = %Loop, %BB0
150 %B = phi i32 [ %A, %BB0 ], [ %C, %Loop ]
152 br i1 %b, label %Loop, label %Exit
154 Exit: ; preds = %Loop
158 define i32* @test8({ i32, i32 } *%A, i1 %b) {
159 ; CHECK-LABEL: @test8(
161 ; CHECK-NEXT: br i1 [[B:%.*]], label [[BB1:%.*]], label [[BB2:%.*]]
163 ; CHECK-NEXT: br label [[BB2]]
165 ; CHECK-NEXT: [[B:%.*]] = getelementptr { i32, i32 }, { i32, i32 }* [[A:%.*]], i64 0, i32 1
166 ; CHECK-NEXT: ret i32* [[B]]
169 %X = getelementptr inbounds { i32, i32 }, { i32, i32 } *%A, i32 0, i32 1
170 br i1 %b, label %BB1, label %BB2
173 %Y = getelementptr { i32, i32 }, { i32, i32 } *%A, i32 0, i32 1
177 ;; Suck GEPs into phi
178 %B = phi i32* [ %X, %BB0 ], [ %Y, %BB1 ]
182 define i32 @test9(i32* %A, i32* %B) {
183 ; CHECK-LABEL: @test9(
185 ; CHECK-NEXT: [[C:%.*]] = icmp eq i32* [[A:%.*]], null
186 ; CHECK-NEXT: br i1 [[C]], label [[BB1:%.*]], label [[BB:%.*]]
188 ; CHECK-NEXT: br label [[BB2:%.*]]
190 ; CHECK-NEXT: br label [[BB2]]
192 ; CHECK-NEXT: [[E_IN:%.*]] = phi i32* [ [[B:%.*]], [[BB]] ], [ [[A]], [[BB1]] ]
193 ; CHECK-NEXT: [[E:%.*]] = load i32, i32* [[E_IN]], align 1
194 ; CHECK-NEXT: ret i32 [[E]]
197 %c = icmp eq i32* %A, null
198 br i1 %c, label %bb1, label %bb
201 %C = load i32, i32* %B, align 1
205 %D = load i32, i32* %A, align 1
209 %E = phi i32 [ %C, %bb ], [ %D, %bb1 ]
214 define i32 @test10(i32* %A, i32* %B) {
215 ; CHECK-LABEL: @test10(
217 ; CHECK-NEXT: [[C:%.*]] = icmp eq i32* [[A:%.*]], null
218 ; CHECK-NEXT: br i1 [[C]], label [[BB1:%.*]], label [[BB:%.*]]
220 ; CHECK-NEXT: br label [[BB2:%.*]]
222 ; CHECK-NEXT: br label [[BB2]]
224 ; CHECK-NEXT: [[E_IN:%.*]] = phi i32* [ [[B:%.*]], [[BB]] ], [ [[A]], [[BB1]] ]
225 ; CHECK-NEXT: [[E:%.*]] = load i32, i32* [[E_IN]], align 16
226 ; CHECK-NEXT: ret i32 [[E]]
229 %c = icmp eq i32* %A, null
230 br i1 %c, label %bb1, label %bb
233 %C = load i32, i32* %B, align 16
237 %D = load i32, i32* %A, align 32
241 %E = phi i32 [ %C, %bb ], [ %D, %bb1 ]
247 declare i1 @test11a()
249 define i1 @test11() {
250 ; CHECK-LABEL: @test11(
252 ; CHECK-NEXT: [[B:%.*]] = call i1 @test11a()
253 ; CHECK-NEXT: br i1 [[B]], label [[ONE:%.*]], label [[TWO:%.*]]
255 ; CHECK-NEXT: [[C:%.*]] = call i1 @test11a()
256 ; CHECK-NEXT: br i1 [[C]], label [[TWO]], label [[END:%.*]]
258 ; CHECK-NEXT: [[D:%.*]] = call i1 @test11a()
259 ; CHECK-NEXT: br i1 [[D]], label [[ONE]], label [[END]]
261 ; CHECK-NEXT: [[Z:%.*]] = call i1 @test11a()
262 ; CHECK-NEXT: ret i1 [[Z]]
266 %i = ptrtoint i32* %a to i64
267 %b = call i1 @test11a()
268 br i1 %b, label %one, label %two
271 %x = phi i64 [%i, %entry], [%y, %two]
272 %c = call i1 @test11a()
273 br i1 %c, label %two, label %end
276 %y = phi i64 [%i, %entry], [%x, %one]
277 %d = call i1 @test11a()
278 br i1 %d, label %one, label %end
281 %f = phi i64 [ %x, %one], [%y, %two]
282 ; Change the %f to %i, and the optimizer suddenly becomes a lot smarter
283 ; even though %f must equal %i at this point
284 %g = inttoptr i64 %f to i32*
285 store i32 10, i32* %g
286 %z = call i1 @test11a()
291 define i64 @test12(i1 %cond, i8* %Ptr, i64 %Val) {
292 ; CHECK-LABEL: @test12(
294 ; CHECK-NEXT: br i1 [[COND:%.*]], label [[END:%.*]], label [[TWO:%.*]]
296 ; CHECK-NEXT: br label [[END]]
298 ; CHECK-NEXT: [[T869_0_OFF64:%.*]] = phi i64 [ 0, [[ENTRY:%.*]] ], [ [[VAL:%.*]], [[TWO]] ]
299 ; CHECK-NEXT: [[T41:%.*]] = ptrtoint i8* [[PTR:%.*]] to i64
300 ; CHECK-NEXT: [[T2:%.*]] = add i64 [[T869_0_OFF64]], [[T41]]
301 ; CHECK-NEXT: ret i64 [[T2]]
304 %t41 = ptrtoint i8* %Ptr to i64
305 %t42 = zext i64 %t41 to i128
306 br i1 %cond, label %end, label %two
309 %t36 = zext i64 %Val to i128 ; <i128> [#uses=1]
310 %t37 = shl i128 %t36, 64 ; <i128> [#uses=1]
311 %ins39 = or i128 %t42, %t37 ; <i128> [#uses=1]
315 %t869.0 = phi i128 [ %t42, %entry ], [ %ins39, %two ]
316 %t32 = trunc i128 %t869.0 to i64 ; <i64> [#uses=1]
317 %t29 = lshr i128 %t869.0, 64 ; <i128> [#uses=1]
318 %t30 = trunc i128 %t29 to i64 ; <i64> [#uses=1]
320 %t2 = add i64 %t32, %t30
324 declare void @test13f(double, i32)
326 define void @test13(i1 %cond, i32 %V1, double %Vald) {
327 ; CHECK-LABEL: @test13(
329 ; CHECK-NEXT: br i1 [[COND:%.*]], label [[END:%.*]], label [[TWO:%.*]]
331 ; CHECK-NEXT: br label [[END]]
333 ; CHECK-NEXT: [[TMP0:%.*]] = phi double [ 0.000000e+00, [[ENTRY:%.*]] ], [ [[VALD:%.*]], [[TWO]] ]
334 ; CHECK-NEXT: call void @test13f(double [[TMP0]], i32 [[V1:%.*]])
335 ; CHECK-NEXT: ret void
338 %t42 = zext i32 %V1 to i128
339 br i1 %cond, label %end, label %two
342 %Val = bitcast double %Vald to i64
343 %t36 = zext i64 %Val to i128 ; <i128> [#uses=1]
344 %t37 = shl i128 %t36, 64 ; <i128> [#uses=1]
345 %ins39 = or i128 %t42, %t37 ; <i128> [#uses=1]
349 %t869.0 = phi i128 [ %t42, %entry ], [ %ins39, %two ]
350 %t32 = trunc i128 %t869.0 to i32
351 %t29 = lshr i128 %t869.0, 64 ; <i128> [#uses=1]
352 %t30 = trunc i128 %t29 to i64 ; <i64> [#uses=1]
353 %t31 = bitcast i64 %t30 to double
355 call void @test13f(double %t31, i32 %t32)
359 define i640 @test14a(i320 %A, i320 %B, i1 %b1) {
360 ; CHECK-LABEL: @test14a(
362 ; CHECK-NEXT: br label [[LOOP:%.*]]
364 ; CHECK-NEXT: [[C_IN:%.*]] = phi i320 [ [[A:%.*]], [[BB0:%.*]] ], [ [[B:%.*]], [[LOOP]] ]
365 ; CHECK-NEXT: br i1 [[B1:%.*]], label [[LOOP]], label [[EXIT:%.*]]
367 ; CHECK-NEXT: [[C:%.*]] = zext i320 [[C_IN]] to i640
368 ; CHECK-NEXT: ret i640 [[C]]
371 %a = zext i320 %A to i640
372 %b = zext i320 %B to i640
376 %C = phi i640 [ %a, %BB0 ], [ %b, %Loop ]
377 br i1 %b1, label %Loop, label %Exit
379 Exit: ; preds = %Loop
383 define i160 @test14b(i320 %pA, i320 %pB, i1 %b1) {
384 ; CHECK-LABEL: @test14b(
386 ; CHECK-NEXT: [[A:%.*]] = trunc i320 [[PA:%.*]] to i160
387 ; CHECK-NEXT: [[B:%.*]] = trunc i320 [[PB:%.*]] to i160
388 ; CHECK-NEXT: br label [[LOOP:%.*]]
390 ; CHECK-NEXT: [[C:%.*]] = phi i160 [ [[A]], [[BB0:%.*]] ], [ [[B]], [[LOOP]] ]
391 ; CHECK-NEXT: br i1 [[B1:%.*]], label [[LOOP]], label [[EXIT:%.*]]
393 ; CHECK-NEXT: ret i160 [[C]]
396 %a = trunc i320 %pA to i160
397 %b = trunc i320 %pB to i160
401 %C = phi i160 [ %a, %BB0 ], [ %b, %Loop ]
402 br i1 %b1, label %Loop, label %Exit
404 Exit: ; preds = %Loop
408 declare i64 @test15a(i64)
410 define i64 @test15b(i64 %A, i1 %b) {
411 ; CHECK-LABEL: @test15b(
413 ; CHECK-NEXT: br i1 [[B:%.*]], label [[ONE:%.*]], label [[TWO:%.*]]
415 ; CHECK-NEXT: [[X_OFF64:%.*]] = phi i64 [ [[A:%.*]], [[ENTRY:%.*]] ], [ [[Y_OFF64:%.*]], [[TWO]] ]
416 ; CHECK-NEXT: [[C:%.*]] = call i64 @test15a(i64 [[X_OFF64]])
417 ; CHECK-NEXT: br label [[TWO]]
419 ; CHECK-NEXT: [[Y_OFF0:%.*]] = phi i64 [ [[A]], [[ENTRY]] ], [ [[C]], [[ONE]] ]
420 ; CHECK-NEXT: [[Y_OFF64]] = phi i64 [ [[A]], [[ENTRY]] ], [ 0, [[ONE]] ]
421 ; CHECK-NEXT: [[D:%.*]] = call i64 @test15a(i64 [[Y_OFF64]])
422 ; CHECK-NEXT: [[TMP0:%.*]] = and i64 [[D]], 1
423 ; CHECK-NEXT: [[D1_NOT:%.*]] = icmp eq i64 [[TMP0]], 0
424 ; CHECK-NEXT: br i1 [[D1_NOT]], label [[END:%.*]], label [[ONE]]
426 ; CHECK-NEXT: ret i64 [[Y_OFF0]]
429 %i0 = zext i64 %A to i128
430 %i1 = shl i128 %i0, 64
431 %i = or i128 %i1, %i0
432 br i1 %b, label %one, label %two
435 %x = phi i128 [%i, %entry], [%y, %two]
436 %x1 = lshr i128 %x, 64
437 %x2 = trunc i128 %x1 to i64
438 %c = call i64 @test15a(i64 %x2)
439 %c1 = zext i64 %c to i128
444 %y = phi i128 [%i, %entry], [%c1, %one]
445 %y1 = lshr i128 %y, 64
446 %y2 = trunc i128 %y1 to i64
447 %d = call i64 @test15a(i64 %y2)
448 %d1 = trunc i64 %d to i1
449 br i1 %d1, label %one, label %end
453 %g = trunc i128 %y to i64
457 ; PR6512 - Shouldn't merge loads from different addr spaces.
458 define i32 @test16(i32 addrspace(1)* %pointer1, i32 %flag, i32* %pointer2)
459 ; CHECK-LABEL: @test16(
461 ; CHECK-NEXT: [[RETVAL:%.*]] = alloca i32, align 4
462 ; CHECK-NEXT: [[POINTER1_ADDR:%.*]] = alloca i32 addrspace(1)*, align 8
463 ; CHECK-NEXT: [[POINTER2_ADDR:%.*]] = alloca i32*, align 8
464 ; CHECK-NEXT: store i32 addrspace(1)* [[POINTER1:%.*]], i32 addrspace(1)** [[POINTER1_ADDR]], align 8
465 ; CHECK-NEXT: store i32* [[POINTER2:%.*]], i32** [[POINTER2_ADDR]], align 8
466 ; CHECK-NEXT: [[TOBOOL_NOT:%.*]] = icmp eq i32 [[FLAG:%.*]], 0
467 ; CHECK-NEXT: br i1 [[TOBOOL_NOT]], label [[IF_ELSE:%.*]], label [[IF_THEN:%.*]]
469 ; CHECK-NEXT: [[T7:%.*]] = load i32, i32* [[RETVAL]], align 4
470 ; CHECK-NEXT: ret i32 [[T7]]
472 ; CHECK-NEXT: [[STOREMERGE:%.*]] = phi i32 [ [[T5:%.*]], [[IF_ELSE]] ], [ [[T2:%.*]], [[IF_THEN]] ]
473 ; CHECK-NEXT: store i32 [[STOREMERGE]], i32* [[RETVAL]], align 4
474 ; CHECK-NEXT: br label [[RETURN:%.*]]
476 ; CHECK-NEXT: [[T1:%.*]] = load i32 addrspace(1)*, i32 addrspace(1)** [[POINTER1_ADDR]], align 8
477 ; CHECK-NEXT: [[T2]] = load i32, i32 addrspace(1)* [[T1]], align 4
478 ; CHECK-NEXT: br label [[IF_END:%.*]]
480 ; CHECK-NEXT: [[T3:%.*]] = load i32*, i32** [[POINTER2_ADDR]], align 8
481 ; CHECK-NEXT: [[T5]] = load i32, i32* [[T3]], align 4
482 ; CHECK-NEXT: br label [[IF_END]]
486 %retval = alloca i32, align 4 ; <i32*> [#uses=2]
487 %pointer1.addr = alloca i32 addrspace(1)*, align 4 ; <i32 addrspace(1)**>
488 %flag.addr = alloca i32, align 4 ; <i32*> [#uses=2]
489 %pointer2.addr = alloca i32*, align 4 ; <i32**> [#uses=2]
490 %res = alloca i32, align 4 ; <i32*> [#uses=4]
491 store i32 addrspace(1)* %pointer1, i32 addrspace(1)** %pointer1.addr
492 store i32 %flag, i32* %flag.addr
493 store i32* %pointer2, i32** %pointer2.addr
494 store i32 10, i32* %res
495 %t = load i32, i32* %flag.addr ; <i32> [#uses=1]
496 %tobool = icmp ne i32 %t, 0 ; <i1> [#uses=1]
497 br i1 %tobool, label %if.then, label %if.else
499 return: ; preds = %if.end
500 %t7 = load i32, i32* %retval ; <i32> [#uses=1]
503 if.end: ; preds = %if.else, %if.then
504 %t6 = load i32, i32* %res ; <i32> [#uses=1]
505 store i32 %t6, i32* %retval
508 if.then: ; preds = %entry
509 %t1 = load i32 addrspace(1)*, i32 addrspace(1)** %pointer1.addr ; <i32 addrspace(1)*>
510 %arrayidx = getelementptr i32, i32 addrspace(1)* %t1, i32 0 ; <i32 addrspace(1)*> [#uses=1]
511 %t2 = load i32, i32 addrspace(1)* %arrayidx ; <i32> [#uses=1]
512 store i32 %t2, i32* %res
515 if.else: ; preds = %entry
516 %t3 = load i32*, i32** %pointer2.addr ; <i32*> [#uses=1]
517 %arrayidx4 = getelementptr i32, i32* %t3, i32 0 ; <i32*> [#uses=1]
518 %t5 = load i32, i32* %arrayidx4 ; <i32> [#uses=1]
519 store i32 %t5, i32* %res
525 define i32 @test17(i1 %a) {
526 ; CHECK-LABEL: @test17(
528 ; CHECK-NEXT: br i1 [[A:%.*]], label [[BB1:%.*]], label [[BB2:%.*]]
530 ; CHECK-NEXT: [[TMP0:%.*]] = tail call i32 @ext()
531 ; CHECK-NEXT: br label [[BB2]]
533 ; CHECK-NEXT: [[RES:%.*]] = phi i32 [ [[TMP0]], [[BB1]] ], [ 0, [[ENTRY:%.*]] ]
534 ; CHECK-NEXT: ret i32 [[RES]]
537 br i1 %a, label %bb1, label %bb2
539 bb1: ; preds = %entry
540 %0 = tail call i32 @ext() ; <i32> [#uses=1]
543 bb2: ; preds = %bb1, %entry
544 %cond = phi i1 [ true, %bb1 ], [ false, %entry ] ; <i1> [#uses=1]
545 %val = phi i32 [ %0, %bb1 ], [ 0, %entry ] ; <i32> [#uses=1]
546 %res = select i1 %cond, i32 %val, i32 0 ; <i32> [#uses=1]
550 define i1 @test18(i1 %cond) {
551 ; CHECK-LABEL: @test18(
552 ; CHECK-NEXT: br i1 [[COND:%.*]], label [[TRUE:%.*]], label [[FALSE:%.*]]
554 ; CHECK-NEXT: br label [[RET:%.*]]
556 ; CHECK-NEXT: br label [[RET]]
558 ; CHECK-NEXT: ret i1 false
562 br i1 %cond, label %true, label %false
568 %ptr = phi i32* [ %zero, %true ] , [ %one, %false ]
569 %isnull = icmp eq i32* %ptr, null
573 define i1 @test19(i1 %cond, double %x) {
574 ; CHECK-LABEL: @test19(
575 ; CHECK-NEXT: br i1 [[COND:%.*]], label [[TRUE:%.*]], label [[FALSE:%.*]]
577 ; CHECK-NEXT: br label [[RET:%.*]]
579 ; CHECK-NEXT: br label [[RET]]
581 ; CHECK-NEXT: ret i1 true
583 br i1 %cond, label %true, label %false
589 %p = phi double [ %x, %true ], [ 0x7FF0000000000000, %false ]; RHS = +infty
590 %cmp = fcmp ule double %x, %p
594 define i1 @test20(i1 %cond) {
595 ; CHECK-LABEL: @test20(
596 ; CHECK-NEXT: br i1 [[COND:%.*]], label [[TRUE:%.*]], label [[FALSE:%.*]]
598 ; CHECK-NEXT: br label [[RET:%.*]]
600 ; CHECK-NEXT: br label [[RET]]
602 ; CHECK-NEXT: ret i1 false
607 br i1 %cond, label %true, label %false
613 %p = phi i32* [ %a, %true ], [ %b, %false ]
614 %r = icmp eq i32* %p, %c
618 define i1 @test21(i1 %c1, i1 %c2) {
619 ; CHECK-LABEL: @test21(
620 ; CHECK-NEXT: br i1 [[C1:%.*]], label [[TRUE:%.*]], label [[FALSE:%.*]]
622 ; CHECK-NEXT: br label [[LOOP:%.*]]
624 ; CHECK-NEXT: br label [[LOOP]]
626 ; CHECK-NEXT: br i1 [[C2:%.*]], label [[RET:%.*]], label [[LOOP]]
628 ; CHECK-NEXT: ret i1 false
633 br i1 %c1, label %true, label %false
639 %p = phi i32* [ %a, %true ], [ %b, %false ], [ %p, %loop ]
640 %r = icmp eq i32* %p, %c
641 br i1 %c2, label %ret, label %loop
646 define void @test22() {
647 ; CHECK-LABEL: @test22(
649 ; CHECK-NEXT: br label [[LOOP:%.*]]
651 ; CHECK-NEXT: [[PHI:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[Y:%.*]], [[LOOP]] ]
652 ; CHECK-NEXT: [[Y]] = add i32 [[PHI]], 1
653 ; CHECK-NEXT: [[O:%.*]] = or i32 [[Y]], [[PHI]]
654 ; CHECK-NEXT: [[E:%.*]] = icmp eq i32 [[O]], [[Y]]
655 ; CHECK-NEXT: br i1 [[E]], label [[LOOP]], label [[RET:%.*]]
657 ; CHECK-NEXT: ret void
662 %phi = phi i32 [ 0, %entry ], [ %y, %loop ]
665 %e = icmp eq i32 %o, %y
666 br i1 %e, label %loop, label %ret
671 define i32 @test23(i32 %A, i1 %pb, i32 * %P) {
672 ; CHECK-LABEL: @test23(
674 ; CHECK-NEXT: [[PHI_BO:%.*]] = add i32 [[A:%.*]], 19
675 ; CHECK-NEXT: br label [[LOOP:%.*]]
677 ; CHECK-NEXT: [[B:%.*]] = phi i32 [ [[PHI_BO]], [[BB0:%.*]] ], [ 61, [[LOOP]] ]
678 ; CHECK-NEXT: store i32 [[B]], i32* [[P:%.*]], align 4
679 ; CHECK-NEXT: br i1 [[PB:%.*]], label [[LOOP]], label [[EXIT:%.*]]
681 ; CHECK-NEXT: ret i32 [[B]]
686 Loop: ; preds = %Loop, %BB0
687 ; PHI has same value always.
688 %B = phi i32 [ %A, %BB0 ], [ 42, %Loop ]
690 store i32 %D, i32* %P
691 br i1 %pb, label %Loop, label %Exit
693 Exit: ; preds = %Loop
698 define i32 @test24(i32 %A, i1 %cond) {
699 ; CHECK-LABEL: @test24(
701 ; CHECK-NEXT: br i1 [[COND:%.*]], label [[BB1:%.*]], label [[BB2:%.*]]
703 ; CHECK-NEXT: br label [[BB2]]
705 ; CHECK-NEXT: [[C:%.*]] = add nuw i32 [[A:%.*]], 1
706 ; CHECK-NEXT: ret i32 [[C]]
709 %X = add nuw nsw i32 %A, 1
710 br i1 %cond, label %BB1, label %BB2
713 %Y = add nuw i32 %A, 1
717 %C = phi i32 [ %X, %BB0 ], [ %Y, %BB1 ]
721 ; Same as test11, but used to be missed due to a bug.
722 declare i1 @test25a()
724 define i1 @test25() {
725 ; CHECK-LABEL: @test25(
727 ; CHECK-NEXT: [[B:%.*]] = call i1 @test25a()
728 ; CHECK-NEXT: br i1 [[B]], label [[ONE:%.*]], label [[TWO:%.*]]
730 ; CHECK-NEXT: [[C:%.*]] = call i1 @test25a()
731 ; CHECK-NEXT: br i1 [[C]], label [[TWO]], label [[END:%.*]]
733 ; CHECK-NEXT: [[D:%.*]] = call i1 @test25a()
734 ; CHECK-NEXT: br i1 [[D]], label [[ONE]], label [[END]]
736 ; CHECK-NEXT: [[Z:%.*]] = call i1 @test25a()
737 ; CHECK-NEXT: ret i1 [[Z]]
741 %i = ptrtoint i32* %a to i64
742 %b = call i1 @test25a()
743 br i1 %b, label %one, label %two
746 %x = phi i64 [%y, %two], [%i, %entry]
747 %c = call i1 @test25a()
748 br i1 %c, label %two, label %end
751 %y = phi i64 [%x, %one], [%i, %entry]
752 %d = call i1 @test25a()
753 br i1 %d, label %one, label %end
756 %f = phi i64 [ %x, %one], [%y, %two]
757 ; Change the %f to %i, and the optimizer suddenly becomes a lot smarter
758 ; even though %f must equal %i at this point
759 %g = inttoptr i64 %f to i32*
760 store i32 10, i32* %g
761 %z = call i1 @test25a()
765 declare i1 @test26a()
767 define i1 @test26(i32 %n) {
768 ; CHECK-LABEL: @test26(
770 ; CHECK-NEXT: [[B:%.*]] = call i1 @test26a()
771 ; CHECK-NEXT: br label [[ONE:%.*]]
773 ; CHECK-NEXT: [[C:%.*]] = call i1 @test26a()
774 ; CHECK-NEXT: switch i32 [[N:%.*]], label [[END:%.*]] [
775 ; CHECK-NEXT: i32 2, label [[TWO:%.*]]
776 ; CHECK-NEXT: i32 3, label [[THREE:%.*]]
779 ; CHECK-NEXT: [[D:%.*]] = call i1 @test26a()
780 ; CHECK-NEXT: switch i32 [[N]], label [[END]] [
781 ; CHECK-NEXT: i32 10, label [[ONE]]
782 ; CHECK-NEXT: i32 30, label [[THREE]]
785 ; CHECK-NEXT: [[E:%.*]] = call i1 @test26a()
786 ; CHECK-NEXT: br i1 [[E]], label [[ONE]], label [[TWO]]
788 ; CHECK-NEXT: [[Z:%.*]] = call i1 @test26a()
789 ; CHECK-NEXT: ret i1 [[Z]]
793 %i = ptrtoint i32* %a to i64
794 %b = call i1 @test26a()
798 %x = phi i64 [%y, %two], [%w, %three], [%i, %entry]
799 %c = call i1 @test26a()
800 switch i32 %n, label %end [
806 %y = phi i64 [%x, %one], [%w, %three]
807 %d = call i1 @test26a()
808 switch i32 %n, label %end [
814 %w = phi i64 [%y, %two], [%x, %one]
815 %e = call i1 @test26a()
816 br i1 %e, label %one, label %two
819 %f = phi i64 [ %x, %one], [%y, %two]
820 ; Change the %f to %i, and the optimizer suddenly becomes a lot smarter
821 ; even though %f must equal %i at this point
822 %g = inttoptr i64 %f to i32*
823 store i32 10, i32* %g
824 %z = call i1 @test26a()
828 define i32 @test27(i1 %b) {
829 ; CHECK-LABEL: @test27(
831 ; CHECK-NEXT: br label [[DONE:%.*]]
833 ; CHECK-NEXT: ret i32 undef
838 %y = phi i32 [ undef, %entry ]
842 ; We should be able to fold the zexts to the other side of the phi
843 ; even though there's a constant value input to the phi. This is
844 ; because we can shrink that constant to the smaller phi type.
846 define i1 @PR24766(i8 %x1, i8 %x2, i8 %condition) {
847 ; CHECK-LABEL: @PR24766(
849 ; CHECK-NEXT: [[CONV:%.*]] = sext i8 [[CONDITION:%.*]] to i32
850 ; CHECK-NEXT: switch i32 [[CONV]], label [[EPILOG:%.*]] [
851 ; CHECK-NEXT: i32 0, label [[SW1:%.*]]
852 ; CHECK-NEXT: i32 1, label [[SW2:%.*]]
855 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i8 [[X1:%.*]], [[X2:%.*]]
856 ; CHECK-NEXT: br label [[EPILOG]]
858 ; CHECK-NEXT: [[CMP2:%.*]] = icmp sle i8 [[X1]], [[X2]]
859 ; CHECK-NEXT: br label [[EPILOG]]
861 ; CHECK-NEXT: [[CONDITIONMET_SHRUNK:%.*]] = phi i1 [ false, [[ENTRY:%.*]] ], [ [[CMP2]], [[SW2]] ], [ [[CMP1]], [[SW1]] ]
862 ; CHECK-NEXT: ret i1 [[CONDITIONMET_SHRUNK]]
865 %conv = sext i8 %condition to i32
866 switch i32 %conv, label %epilog [
872 %cmp1 = icmp eq i8 %x1, %x2
873 %frombool1 = zext i1 %cmp1 to i8
877 %cmp2 = icmp sle i8 %x1, %x2
878 %frombool2 = zext i1 %cmp2 to i8
882 %conditionMet = phi i8 [ 0, %entry ], [ %frombool2, %sw2 ], [ %frombool1, %sw1 ]
883 %tobool = icmp ne i8 %conditionMet, 0
888 ; Same as above (a phi with more than 2 operands), but no constants
890 define i1 @PR24766_no_constants(i8 %x1, i8 %x2, i8 %condition, i1 %another_condition) {
891 ; CHECK-LABEL: @PR24766_no_constants(
893 ; CHECK-NEXT: [[CONV:%.*]] = sext i8 [[CONDITION:%.*]] to i32
894 ; CHECK-NEXT: switch i32 [[CONV]], label [[EPILOG:%.*]] [
895 ; CHECK-NEXT: i32 0, label [[SW1:%.*]]
896 ; CHECK-NEXT: i32 1, label [[SW2:%.*]]
899 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i8 [[X1:%.*]], [[X2:%.*]]
900 ; CHECK-NEXT: br label [[EPILOG]]
902 ; CHECK-NEXT: [[CMP2:%.*]] = icmp sle i8 [[X1]], [[X2]]
903 ; CHECK-NEXT: br label [[EPILOG]]
905 ; CHECK-NEXT: [[CONDITIONMET_IN:%.*]] = phi i1 [ [[ANOTHER_CONDITION:%.*]], [[ENTRY:%.*]] ], [ [[CMP2]], [[SW2]] ], [ [[CMP1]], [[SW1]] ]
906 ; CHECK-NEXT: ret i1 [[CONDITIONMET_IN]]
909 %frombool0 = zext i1 %another_condition to i8
910 %conv = sext i8 %condition to i32
911 switch i32 %conv, label %epilog [
917 %cmp1 = icmp eq i8 %x1, %x2
918 %frombool1 = zext i1 %cmp1 to i8
922 %cmp2 = icmp sle i8 %x1, %x2
923 %frombool2 = zext i1 %cmp2 to i8
927 %conditionMet = phi i8 [ %frombool0, %entry ], [ %frombool2, %sw2 ], [ %frombool1, %sw1 ]
928 %tobool = icmp ne i8 %conditionMet, 0
933 ; Same as above (a phi with more than 2 operands), but two constants
935 define i1 @PR24766_two_constants(i8 %x1, i8 %x2, i8 %condition) {
936 ; CHECK-LABEL: @PR24766_two_constants(
938 ; CHECK-NEXT: [[CONV:%.*]] = sext i8 [[CONDITION:%.*]] to i32
939 ; CHECK-NEXT: switch i32 [[CONV]], label [[EPILOG:%.*]] [
940 ; CHECK-NEXT: i32 0, label [[SW1:%.*]]
941 ; CHECK-NEXT: i32 1, label [[SW2:%.*]]
944 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i8 [[X1:%.*]], [[X2:%.*]]
945 ; CHECK-NEXT: br label [[EPILOG]]
947 ; CHECK-NEXT: br label [[EPILOG]]
949 ; CHECK-NEXT: [[CONDITIONMET:%.*]] = phi i1 [ false, [[ENTRY:%.*]] ], [ true, [[SW2]] ], [ [[CMP1]], [[SW1]] ]
950 ; CHECK-NEXT: ret i1 [[CONDITIONMET]]
953 %conv = sext i8 %condition to i32
954 switch i32 %conv, label %epilog [
960 %cmp1 = icmp eq i8 %x1, %x2
961 %frombool1 = zext i1 %cmp1 to i8
965 %cmp2 = icmp sle i8 %x1, %x2
966 %frombool2 = zext i1 %cmp2 to i8
970 %conditionMet = phi i8 [ 0, %entry ], [ 1, %sw2 ], [ %frombool1, %sw1 ]
971 %tobool = icmp ne i8 %conditionMet, 0
976 ; Same as above (a phi with more than 2 operands), but two constants and two variables
978 define i1 @PR24766_two_constants_two_var(i8 %x1, i8 %x2, i8 %condition) {
979 ; CHECK-LABEL: @PR24766_two_constants_two_var(
981 ; CHECK-NEXT: [[CONV:%.*]] = sext i8 [[CONDITION:%.*]] to i32
982 ; CHECK-NEXT: switch i32 [[CONV]], label [[EPILOG:%.*]] [
983 ; CHECK-NEXT: i32 0, label [[SW1:%.*]]
984 ; CHECK-NEXT: i32 1, label [[SW2:%.*]]
985 ; CHECK-NEXT: i32 2, label [[SW3:%.*]]
988 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i8 [[X1:%.*]], [[X2:%.*]]
989 ; CHECK-NEXT: br label [[EPILOG]]
991 ; CHECK-NEXT: [[CMP2:%.*]] = icmp sle i8 [[X1]], [[X2]]
992 ; CHECK-NEXT: br label [[EPILOG]]
994 ; CHECK-NEXT: br label [[EPILOG]]
996 ; CHECK-NEXT: [[CONDITIONMET_SHRUNK:%.*]] = phi i1 [ false, [[ENTRY:%.*]] ], [ [[CMP2]], [[SW2]] ], [ [[CMP1]], [[SW1]] ], [ true, [[SW3]] ]
997 ; CHECK-NEXT: ret i1 [[CONDITIONMET_SHRUNK]]
1000 %conv = sext i8 %condition to i32
1001 switch i32 %conv, label %epilog [
1008 %cmp1 = icmp eq i8 %x1, %x2
1009 %frombool1 = zext i1 %cmp1 to i8
1013 %cmp2 = icmp sle i8 %x1, %x2
1014 %frombool2 = zext i1 %cmp2 to i8
1018 %cmp3 = icmp sge i8 %x1, %x2
1019 %frombool3 = zext i1 %cmp3 to i8
1023 %conditionMet = phi i8 [ 0, %entry ], [ %frombool2, %sw2 ], [ %frombool1, %sw1 ], [ 1, %sw3 ]
1024 %tobool = icmp ne i8 %conditionMet, 0
1029 define i1 @phi_allnonzeroconstant(i1 %c, i32 %a, i32 %b) {
1030 ; CHECK-LABEL: @phi_allnonzeroconstant(
1031 ; CHECK-NEXT: entry:
1032 ; CHECK-NEXT: br i1 [[C:%.*]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
1034 ; CHECK-NEXT: br label [[IF_END:%.*]]
1036 ; CHECK-NEXT: call void @dummy()
1037 ; CHECK-NEXT: br label [[IF_END]]
1039 ; CHECK-NEXT: ret i1 false
1042 br i1 %c, label %if.then, label %if.else
1044 if.then: ; preds = %entry
1047 if.else: ; preds = %entry
1052 if.end: ; preds = %if.else, %if.then
1053 %x.0 = phi i32 [ 1, %if.then ], [ 2, %if.else ]
1054 %or = or i32 %x.0, %a
1055 %cmp1 = icmp eq i32 %or, 0
1059 define i1 @phi_allnonzerononconstant(i1 %c, i32 %a, i32* nonnull %b1, i32* nonnull %b2) {
1060 ; CHECK-LABEL: @phi_allnonzerononconstant(
1061 ; CHECK-NEXT: entry:
1062 ; CHECK-NEXT: br i1 [[C:%.*]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
1064 ; CHECK-NEXT: br label [[IF_END:%.*]]
1066 ; CHECK-NEXT: call void @dummy()
1067 ; CHECK-NEXT: br label [[IF_END]]
1069 ; CHECK-NEXT: ret i1 false
1072 br i1 %c, label %if.then, label %if.else
1074 if.then: ; preds = %entry
1077 if.else: ; preds = %entry
1082 if.end: ; preds = %if.else, %if.then
1083 %x.0 = phi i32* [ %b1, %if.then ], [ %b2, %if.else ]
1084 %cmp1 = icmp eq i32* %x.0, null
1088 declare void @dummy()
1090 define i1 @phi_knownnonzero_eq(i32 %n, i32 %s, i32* nocapture readonly %P) {
1091 ; CHECK-LABEL: @phi_knownnonzero_eq(
1092 ; CHECK-NEXT: entry:
1093 ; CHECK-NEXT: [[TOBOOL:%.*]] = icmp slt i32 [[N:%.*]], [[S:%.*]]
1094 ; CHECK-NEXT: br i1 [[TOBOOL]], label [[IF_END:%.*]], label [[IF_THEN:%.*]]
1096 ; CHECK-NEXT: br label [[IF_END]]
1098 ; CHECK-NEXT: [[A_0:%.*]] = phi i32 [ 1, [[IF_THEN]] ], [ [[N]], [[ENTRY:%.*]] ]
1099 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i32 [[A_0]], 0
1100 ; CHECK-NEXT: ret i1 [[CMP1]]
1103 %tobool = icmp slt i32 %n, %s
1104 br i1 %tobool, label %if.end, label %if.then
1106 if.then: ; preds = %entry
1107 %0 = load i32, i32* %P
1108 %cmp = icmp eq i32 %n, %0
1109 %1 = select i1 %cmp, i32 1, i32 2
1112 if.end: ; preds = %entry, %if.then
1113 %a.0 = phi i32 [ %1, %if.then ], [ %n, %entry ]
1114 %cmp1 = icmp eq i32 %a.0, 0
1118 define i1 @phi_knownnonzero_ne(i32 %n, i32 %s, i32* nocapture readonly %P) {
1119 ; CHECK-LABEL: @phi_knownnonzero_ne(
1120 ; CHECK-NEXT: entry:
1121 ; CHECK-NEXT: [[TOBOOL:%.*]] = icmp slt i32 [[N:%.*]], [[S:%.*]]
1122 ; CHECK-NEXT: br i1 [[TOBOOL]], label [[IF_END:%.*]], label [[IF_THEN:%.*]]
1124 ; CHECK-NEXT: br label [[IF_END]]
1126 ; CHECK-NEXT: [[A_0:%.*]] = phi i32 [ 1, [[IF_THEN]] ], [ [[N]], [[ENTRY:%.*]] ]
1127 ; CHECK-NEXT: [[CMP1:%.*]] = icmp ne i32 [[A_0]], 0
1128 ; CHECK-NEXT: ret i1 [[CMP1]]
1131 %tobool = icmp slt i32 %n, %s
1132 br i1 %tobool, label %if.end, label %if.then
1134 if.then: ; preds = %entry
1135 %0 = load i32, i32* %P
1136 %cmp = icmp eq i32 %n, %0
1137 %1 = select i1 %cmp, i32 1, i32 2
1140 if.end: ; preds = %entry, %if.then
1141 %a.0 = phi i32 [ %1, %if.then ], [ %n, %entry ]
1142 %cmp1 = icmp ne i32 %a.0, 0
1146 define i1 @phi_knownnonzero_eq_2(i32 %n, i32 %s, i32* nocapture readonly %P) {
1147 ; CHECK-LABEL: @phi_knownnonzero_eq_2(
1148 ; CHECK-NEXT: entry:
1149 ; CHECK-NEXT: [[TOBOOL:%.*]] = icmp slt i32 [[N:%.*]], [[S:%.*]]
1150 ; CHECK-NEXT: br i1 [[TOBOOL]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
1152 ; CHECK-NEXT: br i1 true, label [[IF_ELSE:%.*]], label [[IF_END]]
1154 ; CHECK-NEXT: br label [[IF_END]]
1156 ; CHECK-NEXT: [[A_0:%.*]] = phi i32 [ 2, [[IF_ELSE]] ], [ [[N]], [[ENTRY:%.*]] ], [ 2, [[IF_THEN]] ]
1157 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i32 [[A_0]], 0
1158 ; CHECK-NEXT: ret i1 [[CMP1]]
1161 %tobool = icmp slt i32 %n, %s
1162 br i1 %tobool, label %if.then, label %if.end
1165 %tobool2 = icmp slt i32 %n, %s
1166 br i1 %tobool2, label %if.else, label %if.end
1168 if.else: ; preds = %entry
1169 %0 = load i32, i32* %P
1170 %cmp = icmp eq i32 %n, %0
1171 %1 = select i1 %cmp, i32 1, i32 2
1174 if.end: ; preds = %entry, %if.then
1175 %a.0 = phi i32 [ %1, %if.else], [ %n, %entry ], [2, %if.then]
1176 %cmp1 = icmp eq i32 %a.0, 0
1180 define i1 @phi_knownnonzero_ne_2(i32 %n, i32 %s, i32* nocapture readonly %P) {
1181 ; CHECK-LABEL: @phi_knownnonzero_ne_2(
1182 ; CHECK-NEXT: entry:
1183 ; CHECK-NEXT: [[TOBOOL:%.*]] = icmp slt i32 [[N:%.*]], [[S:%.*]]
1184 ; CHECK-NEXT: br i1 [[TOBOOL]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
1186 ; CHECK-NEXT: br i1 true, label [[IF_ELSE:%.*]], label [[IF_END]]
1188 ; CHECK-NEXT: br label [[IF_END]]
1190 ; CHECK-NEXT: [[A_0:%.*]] = phi i32 [ 2, [[IF_ELSE]] ], [ [[N]], [[ENTRY:%.*]] ], [ 2, [[IF_THEN]] ]
1191 ; CHECK-NEXT: [[CMP1:%.*]] = icmp ne i32 [[A_0]], 0
1192 ; CHECK-NEXT: ret i1 [[CMP1]]
1195 %tobool = icmp slt i32 %n, %s
1196 br i1 %tobool, label %if.then, label %if.end
1199 %tobool2 = icmp slt i32 %n, %s
1200 br i1 %tobool2, label %if.else, label %if.end
1202 if.else: ; preds = %entry
1203 %0 = load i32, i32* %P
1204 %cmp = icmp eq i32 %n, %0
1205 %1 = select i1 %cmp, i32 1, i32 2
1208 if.end: ; preds = %entry, %if.then
1209 %a.0 = phi i32 [ %1, %if.else], [ %n, %entry ], [2, %if.then]
1210 %cmp1 = icmp ne i32 %a.0, 0
1214 ; This would crash trying to delete an instruction (conv)
1215 ; that still had uses because the user (the phi) was not
1216 ; updated to remove a use from an unreachable block (g.exit).
1218 define void @main(i1 %cond, i16 %x) {
1219 ; CHECK-LABEL: @main(
1220 ; CHECK-NEXT: entry:
1221 ; CHECK-NEXT: br label [[FOR_COND:%.*]]
1223 ; CHECK-NEXT: br i1 [[COND:%.*]], label [[FOR_END:%.*]], label [[FOR_BODY:%.*]]
1225 ; CHECK-NEXT: unreachable
1227 ; CHECK-NEXT: br label [[FOR_COND]]
1229 ; CHECK-NEXT: ret void
1235 %p = phi double [ %conv, %g.exit ], [ undef, %entry ]
1236 br i1 %cond, label %for.end, label %for.body
1239 %conv = sitofp i16 %x to double
1246 store double %p, double* undef