1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=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 ; Atomic and non-atomic loads should not be combined.
551 define i32 @PR51435(i32* %ptr, i32* %atomic_ptr, i1 %c) {
552 ; CHECK-LABEL: @PR51435(
554 ; CHECK-NEXT: [[NON_ATOMIC:%.*]] = load i32, i32* %ptr, align 4
556 ; CHECK-NEXT: [[ATOMIC:%.*]] = load atomic i32, i32* %atomic_ptr acquire, align 4
558 ; CHECK-NEXT: [[COND:%.*]] = phi i32 [ [[NON_ATOMIC]], %entry ], [ [[ATOMIC]], %if ]
559 ; CHECK-NEXT: ret i32 [[COND]]
561 %x = load i32, i32* %ptr, align 4
562 br i1 %c, label %if, label %end
565 %y = load atomic i32, i32* %atomic_ptr acquire, align 4
569 %cond = phi i32 [ %x, %entry ], [ %y, %if ]
573 define i1 @test18(i1 %cond) {
574 ; CHECK-LABEL: @test18(
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 false
585 br i1 %cond, label %true, label %false
591 %ptr = phi i32* [ %zero, %true ] , [ %one, %false ]
592 %isnull = icmp eq i32* %ptr, null
596 define i1 @test19(i1 %cond, double %x) {
597 ; CHECK-LABEL: @test19(
598 ; CHECK-NEXT: br i1 [[COND:%.*]], label [[TRUE:%.*]], label [[FALSE:%.*]]
600 ; CHECK-NEXT: br label [[RET:%.*]]
602 ; CHECK-NEXT: br label [[RET]]
604 ; CHECK-NEXT: ret i1 true
606 br i1 %cond, label %true, label %false
612 %p = phi double [ %x, %true ], [ 0x7FF0000000000000, %false ]; RHS = +infty
613 %cmp = fcmp ule double %x, %p
617 define i1 @test20(i1 %cond) {
618 ; CHECK-LABEL: @test20(
619 ; CHECK-NEXT: br i1 [[COND:%.*]], label [[TRUE:%.*]], label [[FALSE:%.*]]
621 ; CHECK-NEXT: br label [[RET:%.*]]
623 ; CHECK-NEXT: br label [[RET]]
625 ; CHECK-NEXT: ret i1 false
630 br i1 %cond, label %true, label %false
636 %p = phi i32* [ %a, %true ], [ %b, %false ]
637 %r = icmp eq i32* %p, %c
641 define i1 @test21(i1 %c1, i1 %c2) {
642 ; CHECK-LABEL: @test21(
643 ; CHECK-NEXT: br i1 [[C1:%.*]], label [[TRUE:%.*]], label [[FALSE:%.*]]
645 ; CHECK-NEXT: br label [[LOOP:%.*]]
647 ; CHECK-NEXT: br label [[LOOP]]
649 ; CHECK-NEXT: br i1 [[C2:%.*]], label [[RET:%.*]], label [[LOOP]]
651 ; CHECK-NEXT: ret i1 false
656 br i1 %c1, label %true, label %false
662 %p = phi i32* [ %a, %true ], [ %b, %false ], [ %p, %loop ]
663 %r = icmp eq i32* %p, %c
664 br i1 %c2, label %ret, label %loop
669 define void @test22() {
670 ; CHECK-LABEL: @test22(
672 ; CHECK-NEXT: br label [[LOOP:%.*]]
674 ; CHECK-NEXT: [[PHI:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[Y:%.*]], [[LOOP]] ]
675 ; CHECK-NEXT: [[Y]] = add i32 [[PHI]], 1
676 ; CHECK-NEXT: [[O:%.*]] = or i32 [[Y]], [[PHI]]
677 ; CHECK-NEXT: [[E:%.*]] = icmp eq i32 [[O]], [[Y]]
678 ; CHECK-NEXT: br i1 [[E]], label [[LOOP]], label [[RET:%.*]]
680 ; CHECK-NEXT: ret void
685 %phi = phi i32 [ 0, %entry ], [ %y, %loop ]
688 %e = icmp eq i32 %o, %y
689 br i1 %e, label %loop, label %ret
694 define i32 @test23(i32 %A, i1 %pb, i32 * %P) {
695 ; CHECK-LABEL: @test23(
697 ; CHECK-NEXT: [[PHI_BO:%.*]] = add i32 [[A:%.*]], 19
698 ; CHECK-NEXT: br label [[LOOP:%.*]]
700 ; CHECK-NEXT: [[B:%.*]] = phi i32 [ [[PHI_BO]], [[BB0:%.*]] ], [ 61, [[LOOP]] ]
701 ; CHECK-NEXT: store i32 [[B]], i32* [[P:%.*]], align 4
702 ; CHECK-NEXT: br i1 [[PB:%.*]], label [[LOOP]], label [[EXIT:%.*]]
704 ; CHECK-NEXT: ret i32 [[B]]
709 Loop: ; preds = %Loop, %BB0
710 ; PHI has same value always.
711 %B = phi i32 [ %A, %BB0 ], [ 42, %Loop ]
713 store i32 %D, i32* %P
714 br i1 %pb, label %Loop, label %Exit
716 Exit: ; preds = %Loop
721 define i32 @test24(i32 %A, i1 %cond) {
722 ; CHECK-LABEL: @test24(
724 ; CHECK-NEXT: br i1 [[COND:%.*]], label [[BB1:%.*]], label [[BB2:%.*]]
726 ; CHECK-NEXT: br label [[BB2]]
728 ; CHECK-NEXT: [[C:%.*]] = add nuw i32 [[A:%.*]], 1
729 ; CHECK-NEXT: ret i32 [[C]]
732 %X = add nuw nsw i32 %A, 1
733 br i1 %cond, label %BB1, label %BB2
736 %Y = add nuw i32 %A, 1
740 %C = phi i32 [ %X, %BB0 ], [ %Y, %BB1 ]
744 ; Same as test11, but used to be missed due to a bug.
745 declare i1 @test25a()
747 define i1 @test25() {
748 ; CHECK-LABEL: @test25(
750 ; CHECK-NEXT: [[B:%.*]] = call i1 @test25a()
751 ; CHECK-NEXT: br i1 [[B]], label [[ONE:%.*]], label [[TWO:%.*]]
753 ; CHECK-NEXT: [[C:%.*]] = call i1 @test25a()
754 ; CHECK-NEXT: br i1 [[C]], label [[TWO]], label [[END:%.*]]
756 ; CHECK-NEXT: [[D:%.*]] = call i1 @test25a()
757 ; CHECK-NEXT: br i1 [[D]], label [[ONE]], label [[END]]
759 ; CHECK-NEXT: [[Z:%.*]] = call i1 @test25a()
760 ; CHECK-NEXT: ret i1 [[Z]]
764 %i = ptrtoint i32* %a to i64
765 %b = call i1 @test25a()
766 br i1 %b, label %one, label %two
769 %x = phi i64 [%y, %two], [%i, %entry]
770 %c = call i1 @test25a()
771 br i1 %c, label %two, label %end
774 %y = phi i64 [%x, %one], [%i, %entry]
775 %d = call i1 @test25a()
776 br i1 %d, label %one, label %end
779 %f = phi i64 [ %x, %one], [%y, %two]
780 ; Change the %f to %i, and the optimizer suddenly becomes a lot smarter
781 ; even though %f must equal %i at this point
782 %g = inttoptr i64 %f to i32*
783 store i32 10, i32* %g
784 %z = call i1 @test25a()
788 declare i1 @test26a()
790 define i1 @test26(i32 %n) {
791 ; CHECK-LABEL: @test26(
793 ; CHECK-NEXT: [[B:%.*]] = call i1 @test26a()
794 ; CHECK-NEXT: br label [[ONE:%.*]]
796 ; CHECK-NEXT: [[C:%.*]] = call i1 @test26a()
797 ; CHECK-NEXT: switch i32 [[N:%.*]], label [[END:%.*]] [
798 ; CHECK-NEXT: i32 2, label [[TWO:%.*]]
799 ; CHECK-NEXT: i32 3, label [[THREE:%.*]]
802 ; CHECK-NEXT: [[D:%.*]] = call i1 @test26a()
803 ; CHECK-NEXT: switch i32 [[N]], label [[END]] [
804 ; CHECK-NEXT: i32 10, label [[ONE]]
805 ; CHECK-NEXT: i32 30, label [[THREE]]
808 ; CHECK-NEXT: [[E:%.*]] = call i1 @test26a()
809 ; CHECK-NEXT: br i1 [[E]], label [[ONE]], label [[TWO]]
811 ; CHECK-NEXT: [[Z:%.*]] = call i1 @test26a()
812 ; CHECK-NEXT: ret i1 [[Z]]
816 %i = ptrtoint i32* %a to i64
817 %b = call i1 @test26a()
821 %x = phi i64 [%y, %two], [%w, %three], [%i, %entry]
822 %c = call i1 @test26a()
823 switch i32 %n, label %end [
829 %y = phi i64 [%x, %one], [%w, %three]
830 %d = call i1 @test26a()
831 switch i32 %n, label %end [
837 %w = phi i64 [%y, %two], [%x, %one]
838 %e = call i1 @test26a()
839 br i1 %e, label %one, label %two
842 %f = phi i64 [ %x, %one], [%y, %two]
843 ; Change the %f to %i, and the optimizer suddenly becomes a lot smarter
844 ; even though %f must equal %i at this point
845 %g = inttoptr i64 %f to i32*
846 store i32 10, i32* %g
847 %z = call i1 @test26a()
851 define i32 @test27(i1 %b) {
852 ; CHECK-LABEL: @test27(
854 ; CHECK-NEXT: br label [[DONE:%.*]]
856 ; CHECK-NEXT: ret i32 undef
861 %y = phi i32 [ undef, %entry ]
865 ; We should be able to fold the zexts to the other side of the phi
866 ; even though there's a constant value input to the phi. This is
867 ; because we can shrink that constant to the smaller phi type.
869 define i1 @PR24766(i8 %x1, i8 %x2, i8 %condition) {
870 ; CHECK-LABEL: @PR24766(
872 ; CHECK-NEXT: [[CONV:%.*]] = sext i8 [[CONDITION:%.*]] to i32
873 ; CHECK-NEXT: switch i32 [[CONV]], label [[EPILOG:%.*]] [
874 ; CHECK-NEXT: i32 0, label [[SW1:%.*]]
875 ; CHECK-NEXT: i32 1, label [[SW2:%.*]]
878 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i8 [[X1:%.*]], [[X2:%.*]]
879 ; CHECK-NEXT: br label [[EPILOG]]
881 ; CHECK-NEXT: [[CMP2:%.*]] = icmp sle i8 [[X1]], [[X2]]
882 ; CHECK-NEXT: br label [[EPILOG]]
884 ; CHECK-NEXT: [[CONDITIONMET_SHRUNK:%.*]] = phi i1 [ false, [[ENTRY:%.*]] ], [ [[CMP2]], [[SW2]] ], [ [[CMP1]], [[SW1]] ]
885 ; CHECK-NEXT: ret i1 [[CONDITIONMET_SHRUNK]]
888 %conv = sext i8 %condition to i32
889 switch i32 %conv, label %epilog [
895 %cmp1 = icmp eq i8 %x1, %x2
896 %frombool1 = zext i1 %cmp1 to i8
900 %cmp2 = icmp sle i8 %x1, %x2
901 %frombool2 = zext i1 %cmp2 to i8
905 %conditionMet = phi i8 [ 0, %entry ], [ %frombool2, %sw2 ], [ %frombool1, %sw1 ]
906 %tobool = icmp ne i8 %conditionMet, 0
911 ; Same as above (a phi with more than 2 operands), but no constants
913 define i1 @PR24766_no_constants(i8 %x1, i8 %x2, i8 %condition, i1 %another_condition) {
914 ; CHECK-LABEL: @PR24766_no_constants(
916 ; CHECK-NEXT: [[CONV:%.*]] = sext i8 [[CONDITION:%.*]] to i32
917 ; CHECK-NEXT: switch i32 [[CONV]], label [[EPILOG:%.*]] [
918 ; CHECK-NEXT: i32 0, label [[SW1:%.*]]
919 ; CHECK-NEXT: i32 1, label [[SW2:%.*]]
922 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i8 [[X1:%.*]], [[X2:%.*]]
923 ; CHECK-NEXT: br label [[EPILOG]]
925 ; CHECK-NEXT: [[CMP2:%.*]] = icmp sle i8 [[X1]], [[X2]]
926 ; CHECK-NEXT: br label [[EPILOG]]
928 ; CHECK-NEXT: [[CONDITIONMET_IN:%.*]] = phi i1 [ [[ANOTHER_CONDITION:%.*]], [[ENTRY:%.*]] ], [ [[CMP2]], [[SW2]] ], [ [[CMP1]], [[SW1]] ]
929 ; CHECK-NEXT: ret i1 [[CONDITIONMET_IN]]
932 %frombool0 = zext i1 %another_condition to i8
933 %conv = sext i8 %condition to i32
934 switch i32 %conv, label %epilog [
940 %cmp1 = icmp eq i8 %x1, %x2
941 %frombool1 = zext i1 %cmp1 to i8
945 %cmp2 = icmp sle i8 %x1, %x2
946 %frombool2 = zext i1 %cmp2 to i8
950 %conditionMet = phi i8 [ %frombool0, %entry ], [ %frombool2, %sw2 ], [ %frombool1, %sw1 ]
951 %tobool = icmp ne i8 %conditionMet, 0
956 ; Same as above (a phi with more than 2 operands), but two constants
958 define i1 @PR24766_two_constants(i8 %x1, i8 %x2, i8 %condition) {
959 ; CHECK-LABEL: @PR24766_two_constants(
961 ; CHECK-NEXT: [[CONV:%.*]] = sext i8 [[CONDITION:%.*]] to i32
962 ; CHECK-NEXT: switch i32 [[CONV]], label [[EPILOG:%.*]] [
963 ; CHECK-NEXT: i32 0, label [[SW1:%.*]]
964 ; CHECK-NEXT: i32 1, label [[SW2:%.*]]
967 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i8 [[X1:%.*]], [[X2:%.*]]
968 ; CHECK-NEXT: br label [[EPILOG]]
970 ; CHECK-NEXT: br label [[EPILOG]]
972 ; CHECK-NEXT: [[CONDITIONMET:%.*]] = phi i1 [ false, [[ENTRY:%.*]] ], [ true, [[SW2]] ], [ [[CMP1]], [[SW1]] ]
973 ; CHECK-NEXT: ret i1 [[CONDITIONMET]]
976 %conv = sext i8 %condition to i32
977 switch i32 %conv, label %epilog [
983 %cmp1 = icmp eq i8 %x1, %x2
984 %frombool1 = zext i1 %cmp1 to i8
988 %cmp2 = icmp sle i8 %x1, %x2
989 %frombool2 = zext i1 %cmp2 to i8
993 %conditionMet = phi i8 [ 0, %entry ], [ 1, %sw2 ], [ %frombool1, %sw1 ]
994 %tobool = icmp ne i8 %conditionMet, 0
999 ; Same as above (a phi with more than 2 operands), but two constants and two variables
1001 define i1 @PR24766_two_constants_two_var(i8 %x1, i8 %x2, i8 %condition) {
1002 ; CHECK-LABEL: @PR24766_two_constants_two_var(
1003 ; CHECK-NEXT: entry:
1004 ; CHECK-NEXT: [[CONV:%.*]] = sext i8 [[CONDITION:%.*]] to i32
1005 ; CHECK-NEXT: switch i32 [[CONV]], label [[EPILOG:%.*]] [
1006 ; CHECK-NEXT: i32 0, label [[SW1:%.*]]
1007 ; CHECK-NEXT: i32 1, label [[SW2:%.*]]
1008 ; CHECK-NEXT: i32 2, label [[SW3:%.*]]
1011 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i8 [[X1:%.*]], [[X2:%.*]]
1012 ; CHECK-NEXT: br label [[EPILOG]]
1014 ; CHECK-NEXT: [[CMP2:%.*]] = icmp sle i8 [[X1]], [[X2]]
1015 ; CHECK-NEXT: br label [[EPILOG]]
1017 ; CHECK-NEXT: br label [[EPILOG]]
1019 ; CHECK-NEXT: [[CONDITIONMET_SHRUNK:%.*]] = phi i1 [ false, [[ENTRY:%.*]] ], [ [[CMP2]], [[SW2]] ], [ [[CMP1]], [[SW1]] ], [ true, [[SW3]] ]
1020 ; CHECK-NEXT: ret i1 [[CONDITIONMET_SHRUNK]]
1023 %conv = sext i8 %condition to i32
1024 switch i32 %conv, label %epilog [
1031 %cmp1 = icmp eq i8 %x1, %x2
1032 %frombool1 = zext i1 %cmp1 to i8
1036 %cmp2 = icmp sle i8 %x1, %x2
1037 %frombool2 = zext i1 %cmp2 to i8
1041 %cmp3 = icmp sge i8 %x1, %x2
1042 %frombool3 = zext i1 %cmp3 to i8
1046 %conditionMet = phi i8 [ 0, %entry ], [ %frombool2, %sw2 ], [ %frombool1, %sw1 ], [ 1, %sw3 ]
1047 %tobool = icmp ne i8 %conditionMet, 0
1052 define i1 @phi_allnonzeroconstant(i1 %c, i32 %a, i32 %b) {
1053 ; CHECK-LABEL: @phi_allnonzeroconstant(
1054 ; CHECK-NEXT: entry:
1055 ; CHECK-NEXT: br i1 [[C:%.*]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
1057 ; CHECK-NEXT: br label [[IF_END:%.*]]
1059 ; CHECK-NEXT: call void @dummy()
1060 ; CHECK-NEXT: br label [[IF_END]]
1062 ; CHECK-NEXT: ret i1 false
1065 br i1 %c, label %if.then, label %if.else
1067 if.then: ; preds = %entry
1070 if.else: ; preds = %entry
1075 if.end: ; preds = %if.else, %if.then
1076 %x.0 = phi i32 [ 1, %if.then ], [ 2, %if.else ]
1077 %or = or i32 %x.0, %a
1078 %cmp1 = icmp eq i32 %or, 0
1082 define i1 @phi_allnonzerononconstant(i1 %c, i32 %a, i32* nonnull %b1, i32* nonnull %b2) {
1083 ; CHECK-LABEL: @phi_allnonzerononconstant(
1084 ; CHECK-NEXT: entry:
1085 ; CHECK-NEXT: br i1 [[C:%.*]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
1087 ; CHECK-NEXT: br label [[IF_END:%.*]]
1089 ; CHECK-NEXT: call void @dummy()
1090 ; CHECK-NEXT: br label [[IF_END]]
1092 ; CHECK-NEXT: ret i1 false
1095 br i1 %c, label %if.then, label %if.else
1097 if.then: ; preds = %entry
1100 if.else: ; preds = %entry
1105 if.end: ; preds = %if.else, %if.then
1106 %x.0 = phi i32* [ %b1, %if.then ], [ %b2, %if.else ]
1107 %cmp1 = icmp eq i32* %x.0, null
1111 declare void @dummy()
1113 define i1 @phi_knownnonzero_eq(i32 %n, i32 %s, i32* nocapture readonly %P) {
1114 ; CHECK-LABEL: @phi_knownnonzero_eq(
1115 ; CHECK-NEXT: entry:
1116 ; CHECK-NEXT: [[TOBOOL:%.*]] = icmp slt i32 [[N:%.*]], [[S:%.*]]
1117 ; CHECK-NEXT: br i1 [[TOBOOL]], label [[IF_END:%.*]], label [[IF_THEN:%.*]]
1119 ; CHECK-NEXT: br label [[IF_END]]
1121 ; CHECK-NEXT: [[A_0:%.*]] = phi i32 [ 1, [[IF_THEN]] ], [ [[N]], [[ENTRY:%.*]] ]
1122 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i32 [[A_0]], 0
1123 ; CHECK-NEXT: ret i1 [[CMP1]]
1126 %tobool = icmp slt i32 %n, %s
1127 br i1 %tobool, label %if.end, label %if.then
1129 if.then: ; preds = %entry
1130 %0 = load i32, i32* %P
1131 %cmp = icmp eq i32 %n, %0
1132 %1 = select i1 %cmp, i32 1, i32 2
1135 if.end: ; preds = %entry, %if.then
1136 %a.0 = phi i32 [ %1, %if.then ], [ %n, %entry ]
1137 %cmp1 = icmp eq i32 %a.0, 0
1141 define i1 @phi_knownnonzero_ne(i32 %n, i32 %s, i32* nocapture readonly %P) {
1142 ; CHECK-LABEL: @phi_knownnonzero_ne(
1143 ; CHECK-NEXT: entry:
1144 ; CHECK-NEXT: [[TOBOOL:%.*]] = icmp slt i32 [[N:%.*]], [[S:%.*]]
1145 ; CHECK-NEXT: br i1 [[TOBOOL]], label [[IF_END:%.*]], label [[IF_THEN:%.*]]
1147 ; CHECK-NEXT: br label [[IF_END]]
1149 ; CHECK-NEXT: [[A_0:%.*]] = phi i32 [ 1, [[IF_THEN]] ], [ [[N]], [[ENTRY:%.*]] ]
1150 ; CHECK-NEXT: [[CMP1:%.*]] = icmp ne i32 [[A_0]], 0
1151 ; CHECK-NEXT: ret i1 [[CMP1]]
1154 %tobool = icmp slt i32 %n, %s
1155 br i1 %tobool, label %if.end, label %if.then
1157 if.then: ; preds = %entry
1158 %0 = load i32, i32* %P
1159 %cmp = icmp eq i32 %n, %0
1160 %1 = select i1 %cmp, i32 1, i32 2
1163 if.end: ; preds = %entry, %if.then
1164 %a.0 = phi i32 [ %1, %if.then ], [ %n, %entry ]
1165 %cmp1 = icmp ne i32 %a.0, 0
1169 define i1 @phi_knownnonzero_eq_2(i32 %n, i32 %s, i32* nocapture readonly %P) {
1170 ; CHECK-LABEL: @phi_knownnonzero_eq_2(
1171 ; CHECK-NEXT: entry:
1172 ; CHECK-NEXT: [[TOBOOL:%.*]] = icmp slt i32 [[N:%.*]], [[S:%.*]]
1173 ; CHECK-NEXT: br i1 [[TOBOOL]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
1175 ; CHECK-NEXT: br i1 true, label [[IF_ELSE:%.*]], label [[IF_END]]
1177 ; CHECK-NEXT: br label [[IF_END]]
1179 ; CHECK-NEXT: [[A_0:%.*]] = phi i32 [ 2, [[IF_ELSE]] ], [ [[N]], [[ENTRY:%.*]] ], [ 2, [[IF_THEN]] ]
1180 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i32 [[A_0]], 0
1181 ; CHECK-NEXT: ret i1 [[CMP1]]
1184 %tobool = icmp slt i32 %n, %s
1185 br i1 %tobool, label %if.then, label %if.end
1188 %tobool2 = icmp slt i32 %n, %s
1189 br i1 %tobool2, label %if.else, label %if.end
1191 if.else: ; preds = %entry
1192 %0 = load i32, i32* %P
1193 %cmp = icmp eq i32 %n, %0
1194 %1 = select i1 %cmp, i32 1, i32 2
1197 if.end: ; preds = %entry, %if.then
1198 %a.0 = phi i32 [ %1, %if.else], [ %n, %entry ], [2, %if.then]
1199 %cmp1 = icmp eq i32 %a.0, 0
1203 define i1 @phi_knownnonzero_ne_2(i32 %n, i32 %s, i32* nocapture readonly %P) {
1204 ; CHECK-LABEL: @phi_knownnonzero_ne_2(
1205 ; CHECK-NEXT: entry:
1206 ; CHECK-NEXT: [[TOBOOL:%.*]] = icmp slt i32 [[N:%.*]], [[S:%.*]]
1207 ; CHECK-NEXT: br i1 [[TOBOOL]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
1209 ; CHECK-NEXT: br i1 true, label [[IF_ELSE:%.*]], label [[IF_END]]
1211 ; CHECK-NEXT: br label [[IF_END]]
1213 ; CHECK-NEXT: [[A_0:%.*]] = phi i32 [ 2, [[IF_ELSE]] ], [ [[N]], [[ENTRY:%.*]] ], [ 2, [[IF_THEN]] ]
1214 ; CHECK-NEXT: [[CMP1:%.*]] = icmp ne i32 [[A_0]], 0
1215 ; CHECK-NEXT: ret i1 [[CMP1]]
1218 %tobool = icmp slt i32 %n, %s
1219 br i1 %tobool, label %if.then, label %if.end
1222 %tobool2 = icmp slt i32 %n, %s
1223 br i1 %tobool2, label %if.else, label %if.end
1225 if.else: ; preds = %entry
1226 %0 = load i32, i32* %P
1227 %cmp = icmp eq i32 %n, %0
1228 %1 = select i1 %cmp, i32 1, i32 2
1231 if.end: ; preds = %entry, %if.then
1232 %a.0 = phi i32 [ %1, %if.else], [ %n, %entry ], [2, %if.then]
1233 %cmp1 = icmp ne i32 %a.0, 0
1237 ; This would crash trying to delete an instruction (conv)
1238 ; that still had uses because the user (the phi) was not
1239 ; updated to remove a use from an unreachable block (g.exit).
1241 define void @main(i1 %cond, i16 %x) {
1242 ; CHECK-LABEL: @main(
1243 ; CHECK-NEXT: entry:
1244 ; CHECK-NEXT: br label [[FOR_COND:%.*]]
1246 ; CHECK-NEXT: br i1 [[COND:%.*]], label [[FOR_END:%.*]], label [[FOR_BODY:%.*]]
1248 ; CHECK-NEXT: unreachable
1250 ; CHECK-NEXT: br label [[FOR_COND]]
1252 ; CHECK-NEXT: ret void
1258 %p = phi double [ %conv, %g.exit ], [ undef, %entry ]
1259 br i1 %cond, label %for.end, label %for.body
1262 %conv = sitofp i16 %x to double
1269 store double %p, double* undef