• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1; This test makes sure that these instructions are properly eliminated.
2;
3; RUN: opt < %s -instcombine -S | FileCheck %s
4
5target 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
7define i32 @test1(i32 %A, i1 %b) {
8BB0:
9        br i1 %b, label %BB1, label %BB2
10
11BB1:
12        ; Combine away one argument PHI nodes
13        %B = phi i32 [ %A, %BB0 ]
14        ret i32 %B
15
16BB2:
17        ret i32 %A
18; CHECK-LABEL: @test1(
19; CHECK: BB1:
20; CHECK-NEXT: ret i32 %A
21}
22
23define i32 @test2(i32 %A, i1 %b) {
24BB0:
25        br i1 %b, label %BB1, label %BB2
26
27BB1:
28        br label %BB2
29
30BB2:
31        ; Combine away PHI nodes with same values
32        %B = phi i32 [ %A, %BB0 ], [ %A, %BB1 ]
33        ret i32 %B
34; CHECK-LABEL: @test2(
35; CHECK: BB2:
36; CHECK-NEXT: ret i32 %A
37}
38
39define i32 @test3(i32 %A, i1 %b) {
40BB0:
41        br label %Loop
42
43Loop:
44        ; PHI has same value always.
45        %B = phi i32 [ %A, %BB0 ], [ %B, %Loop ]
46        br i1 %b, label %Loop, label %Exit
47
48Exit:
49        ret i32 %B
50; CHECK-LABEL: @test3(
51; CHECK: Exit:
52; CHECK-NEXT: ret i32 %A
53}
54
55define i32 @test4(i1 %b) {
56BB0:
57        ; Loop is unreachable
58        ret i32 7
59
60Loop:           ; preds = %L2, %Loop
61        ; PHI has same value always.
62        %B = phi i32 [ %B, %L2 ], [ %B, %Loop ]
63        br i1 %b, label %L2, label %Loop
64
65L2:             ; preds = %Loop
66        br label %Loop
67; CHECK-LABEL: @test4(
68; CHECK: Loop:
69; CHECK-NEXT: br i1 %b
70}
71
72define i32 @test5(i32 %A, i1 %b) {
73BB0:
74        br label %Loop
75
76Loop:           ; preds = %Loop, %BB0
77        ; PHI has same value always.
78        %B = phi i32 [ %A, %BB0 ], [ undef, %Loop ]
79        br i1 %b, label %Loop, label %Exit
80
81Exit:           ; preds = %Loop
82        ret i32 %B
83; CHECK-LABEL: @test5(
84; CHECK: Loop:
85; CHECK-NEXT: br i1 %b
86; CHECK: Exit:
87; CHECK-NEXT: ret i32 %A
88}
89
90define i32 @test6(i16 %A, i1 %b) {
91BB0:
92        %X = zext i16 %A to i32
93        br i1 %b, label %BB1, label %BB2
94
95BB1:
96        %Y = zext i16 %A to i32
97        br label %BB2
98
99BB2:
100        ;; Suck casts into phi
101        %B = phi i32 [ %X, %BB0 ], [ %Y, %BB1 ]
102        ret i32 %B
103; CHECK-LABEL: @test6(
104; CHECK: BB2:
105; CHECK: zext i16 %A to i32
106; CHECK-NEXT: ret i32
107}
108
109define i32 @test7(i32 %A, i1 %b) {
110BB0:
111        br label %Loop
112
113Loop:           ; preds = %Loop, %BB0
114        ; PHI is dead.
115        %B = phi i32 [ %A, %BB0 ], [ %C, %Loop ]
116        %C = add i32 %B, 123
117        br i1 %b, label %Loop, label %Exit
118
119Exit:           ; preds = %Loop
120        ret i32 0
121; CHECK-LABEL: @test7(
122; CHECK: Loop:
123; CHECK-NEXT: br i1 %b
124}
125
126define i32* @test8({ i32, i32 } *%A, i1 %b) {
127BB0:
128        %X = getelementptr inbounds { i32, i32 }, { i32, i32 } *%A, i32 0, i32 1
129        br i1 %b, label %BB1, label %BB2
130
131BB1:
132        %Y = getelementptr { i32, i32 }, { i32, i32 } *%A, i32 0, i32 1
133        br label %BB2
134
135BB2:
136        ;; Suck GEPs into phi
137        %B = phi i32* [ %X, %BB0 ], [ %Y, %BB1 ]
138        ret i32* %B
139; CHECK-LABEL: @test8(
140; CHECK-NOT: phi
141; CHECK: BB2:
142; CHECK-NEXT: %B = getelementptr { i32, i32 }, { i32, i32 }* %A
143; CHECK-NEXT: ret i32* %B
144}
145
146define i32 @test9(i32* %A, i32* %B) {
147entry:
148  %c = icmp eq i32* %A, null
149  br i1 %c, label %bb1, label %bb
150
151bb:
152  %C = load i32, i32* %B, align 1
153  br label %bb2
154
155bb1:
156  %D = load i32, i32* %A, align 1
157  br label %bb2
158
159bb2:
160  %E = phi i32 [ %C, %bb ], [ %D, %bb1 ]
161  ret i32 %E
162; CHECK-LABEL: @test9(
163; CHECK:       bb2:
164; CHECK-NEXT:        phi i32* [ %B, %bb ], [ %A, %bb1 ]
165; CHECK-NEXT:   %E = load i32, i32* %{{[^,]*}}, align 1
166; CHECK-NEXT:   ret i32 %E
167
168}
169
170define i32 @test10(i32* %A, i32* %B) {
171entry:
172  %c = icmp eq i32* %A, null
173  br i1 %c, label %bb1, label %bb
174
175bb:
176  %C = load i32, i32* %B, align 16
177  br label %bb2
178
179bb1:
180  %D = load i32, i32* %A, align 32
181  br label %bb2
182
183bb2:
184  %E = phi i32 [ %C, %bb ], [ %D, %bb1 ]
185  ret i32 %E
186; CHECK-LABEL: @test10(
187; CHECK:       bb2:
188; CHECK-NEXT:        phi i32* [ %B, %bb ], [ %A, %bb1 ]
189; CHECK-NEXT:   %E = load i32, i32* %{{[^,]*}}, align 16
190; CHECK-NEXT:   ret i32 %E
191}
192
193
194; PR1777
195declare i1 @test11a()
196
197define i1 @test11() {
198entry:
199  %a = alloca i32
200  %i = ptrtoint i32* %a to i64
201  %b = call i1 @test11a()
202  br i1 %b, label %one, label %two
203
204one:
205  %x = phi i64 [%i, %entry], [%y, %two]
206  %c = call i1 @test11a()
207  br i1 %c, label %two, label %end
208
209two:
210  %y = phi i64 [%i, %entry], [%x, %one]
211  %d = call i1 @test11a()
212  br i1 %d, label %one, label %end
213
214end:
215  %f = phi i64 [ %x, %one], [%y, %two]
216  ; Change the %f to %i, and the optimizer suddenly becomes a lot smarter
217  ; even though %f must equal %i at this point
218  %g = inttoptr i64 %f to i32*
219  store i32 10, i32* %g
220  %z = call i1 @test11a()
221  ret i1 %z
222; CHECK-LABEL: @test11(
223; CHECK-NOT: phi i32
224; CHECK: ret i1 %z
225}
226
227
228define i64 @test12(i1 %cond, i8* %Ptr, i64 %Val) {
229entry:
230  %tmp41 = ptrtoint i8* %Ptr to i64
231  %tmp42 = zext i64 %tmp41 to i128
232  br i1 %cond, label %end, label %two
233
234two:
235  %tmp36 = zext i64 %Val to i128            ; <i128> [#uses=1]
236  %tmp37 = shl i128 %tmp36, 64                    ; <i128> [#uses=1]
237  %ins39 = or i128 %tmp42, %tmp37                 ; <i128> [#uses=1]
238  br label %end
239
240end:
241  %tmp869.0 = phi i128 [ %tmp42, %entry ], [ %ins39, %two ]
242  %tmp32 = trunc i128 %tmp869.0 to i64            ; <i64> [#uses=1]
243  %tmp29 = lshr i128 %tmp869.0, 64                ; <i128> [#uses=1]
244  %tmp30 = trunc i128 %tmp29 to i64               ; <i64> [#uses=1]
245
246  %tmp2 = add i64 %tmp32, %tmp30
247  ret i64 %tmp2
248; CHECK-LABEL: @test12(
249; CHECK-NOT: zext
250; CHECK: end:
251; CHECK-NEXT: phi i64 [ 0, %entry ], [ %Val, %two ]
252; CHECK-NOT: phi
253; CHECK: ret i64
254}
255
256declare void @test13f(double, i32)
257
258define void @test13(i1 %cond, i32 %V1, double %Vald) {
259entry:
260  %tmp42 = zext i32 %V1 to i128
261  br i1 %cond, label %end, label %two
262
263two:
264  %Val = bitcast double %Vald to i64
265  %tmp36 = zext i64 %Val to i128            ; <i128> [#uses=1]
266  %tmp37 = shl i128 %tmp36, 64                    ; <i128> [#uses=1]
267  %ins39 = or i128 %tmp42, %tmp37                 ; <i128> [#uses=1]
268  br label %end
269
270end:
271  %tmp869.0 = phi i128 [ %tmp42, %entry ], [ %ins39, %two ]
272  %tmp32 = trunc i128 %tmp869.0 to i32
273  %tmp29 = lshr i128 %tmp869.0, 64                ; <i128> [#uses=1]
274  %tmp30 = trunc i128 %tmp29 to i64               ; <i64> [#uses=1]
275  %tmp31 = bitcast i64 %tmp30 to double
276
277  call void @test13f(double %tmp31, i32 %tmp32)
278  ret void
279; CHECK-LABEL: @test13(
280; CHECK-NOT: zext
281; CHECK: end:
282; CHECK-NEXT: phi double [ 0.000000e+00, %entry ], [ %Vald, %two ]
283; CHECK-NEXT: call void @test13f(double {{[^,]*}}, i32 %V1)
284; CHECK: ret void
285}
286
287define i640 @test14a(i320 %A, i320 %B, i1 %b1) {
288BB0:
289        %a = zext i320 %A to i640
290        %b = zext i320 %B to i640
291        br label %Loop
292
293Loop:
294        %C = phi i640 [ %a, %BB0 ], [ %b, %Loop ]
295        br i1 %b1, label %Loop, label %Exit
296
297Exit:           ; preds = %Loop
298        ret i640 %C
299; CHECK-LABEL: @test14a(
300; CHECK: Loop:
301; CHECK-NEXT: phi i320
302}
303
304define i160 @test14b(i320 %A, i320 %B, i1 %b1) {
305BB0:
306        %a = trunc i320 %A to i160
307        %b = trunc i320 %B to i160
308        br label %Loop
309
310Loop:
311        %C = phi i160 [ %a, %BB0 ], [ %b, %Loop ]
312        br i1 %b1, label %Loop, label %Exit
313
314Exit:           ; preds = %Loop
315        ret i160 %C
316; CHECK-LABEL: @test14b(
317; CHECK: Loop:
318; CHECK-NEXT: phi i160
319}
320
321declare i64 @test15a(i64)
322
323define i64 @test15b(i64 %A, i1 %b) {
324; CHECK-LABEL: @test15b(
325entry:
326  %i0 = zext i64 %A to i128
327  %i1 = shl i128 %i0, 64
328  %i = or i128 %i1, %i0
329  br i1 %b, label %one, label %two
330; CHECK: entry:
331; CHECK-NEXT: br i1 %b
332
333one:
334  %x = phi i128 [%i, %entry], [%y, %two]
335  %x1 = lshr i128 %x, 64
336  %x2 = trunc i128 %x1 to i64
337  %c = call i64 @test15a(i64 %x2)
338  %c1 = zext i64 %c to i128
339  br label %two
340
341; CHECK: one:
342; CHECK-NEXT: phi i64
343; CHECK-NEXT: %c = call i64 @test15a
344
345two:
346  %y = phi i128 [%i, %entry], [%c1, %one]
347  %y1 = lshr i128 %y, 64
348  %y2 = trunc i128 %y1 to i64
349  %d = call i64 @test15a(i64 %y2)
350  %d1 = trunc i64 %d to i1
351  br i1 %d1, label %one, label %end
352
353; CHECK: two:
354; CHECK-NEXT: phi i64
355; CHECK-NEXT: phi i64
356; CHECK-NEXT: %d = call i64 @test15a
357
358end:
359  %g = trunc i128 %y to i64
360  ret i64 %g
361; CHECK: end:
362; CHECK-NEXT: ret i64
363}
364
365; PR6512 - Shouldn't merge loads from different addr spaces.
366define i32 @test16(i32 addrspace(1)* %pointer1, i32 %flag, i32* %pointer2)
367nounwind {
368entry:
369  %retval = alloca i32, align 4                   ; <i32*> [#uses=2]
370  %pointer1.addr = alloca i32 addrspace(1)*, align 4 ; <i32 addrspace(1)**>
371  %flag.addr = alloca i32, align 4                ; <i32*> [#uses=2]
372  %pointer2.addr = alloca i32*, align 4           ; <i32**> [#uses=2]
373  %res = alloca i32, align 4                      ; <i32*> [#uses=4]
374  store i32 addrspace(1)* %pointer1, i32 addrspace(1)** %pointer1.addr
375  store i32 %flag, i32* %flag.addr
376  store i32* %pointer2, i32** %pointer2.addr
377  store i32 10, i32* %res
378  %tmp = load i32, i32* %flag.addr                     ; <i32> [#uses=1]
379  %tobool = icmp ne i32 %tmp, 0                   ; <i1> [#uses=1]
380  br i1 %tobool, label %if.then, label %if.else
381
382return:                                           ; preds = %if.end
383  %tmp7 = load i32, i32* %retval                       ; <i32> [#uses=1]
384  ret i32 %tmp7
385
386if.end:                                           ; preds = %if.else, %if.then
387  %tmp6 = load i32, i32* %res                          ; <i32> [#uses=1]
388  store i32 %tmp6, i32* %retval
389  br label %return
390
391if.then:                                          ; preds = %entry
392  %tmp1 = load i32 addrspace(1)*, i32 addrspace(1)** %pointer1.addr  ; <i32 addrspace(1)*>
393  %arrayidx = getelementptr i32, i32 addrspace(1)* %tmp1, i32 0 ; <i32 addrspace(1)*> [#uses=1]
394  %tmp2 = load i32, i32 addrspace(1)* %arrayidx        ; <i32> [#uses=1]
395  store i32 %tmp2, i32* %res
396  br label %if.end
397
398if.else:                                          ; preds = %entry
399  %tmp3 = load i32*, i32** %pointer2.addr               ; <i32*> [#uses=1]
400  %arrayidx4 = getelementptr i32, i32* %tmp3, i32 0    ; <i32*> [#uses=1]
401  %tmp5 = load i32, i32* %arrayidx4                    ; <i32> [#uses=1]
402  store i32 %tmp5, i32* %res
403  br label %if.end
404}
405
406; PR4413
407declare i32 @ext()
408; CHECK-LABEL: @test17(
409define i32 @test17(i1 %a) {
410entry:
411    br i1 %a, label %bb1, label %bb2
412
413bb1:        ; preds = %entry
414    %0 = tail call i32 @ext()        ; <i32> [#uses=1]
415    br label %bb2
416
417bb2:        ; preds = %bb1, %entry
418    %cond = phi i1 [ true, %bb1 ], [ false, %entry ]        ; <i1> [#uses=1]
419; CHECK-NOT: %val = phi i32 [ %0, %bb1 ], [ 0, %entry ]
420    %val = phi i32 [ %0, %bb1 ], [ 0, %entry ]        ; <i32> [#uses=1]
421    %res = select i1 %cond, i32 %val, i32 0        ; <i32> [#uses=1]
422; CHECK: ret i32 %cond
423    ret i32 %res
424}
425
426define i1 @test18(i1 %cond) {
427  %zero = alloca i32
428  %one = alloca i32
429  br i1 %cond, label %true, label %false
430true:
431  br label %ret
432false:
433  br label %ret
434ret:
435  %ptr = phi i32* [ %zero, %true ] , [ %one, %false ]
436  %isnull = icmp eq i32* %ptr, null
437  ret i1 %isnull
438; CHECK-LABEL: @test18(
439; CHECK: ret i1 false
440}
441
442define i1 @test19(i1 %cond, double %x) {
443  br i1 %cond, label %true, label %false
444true:
445  br label %ret
446false:
447  br label %ret
448ret:
449  %p = phi double [ %x, %true ], [ 0x7FF0000000000000, %false ]; RHS = +infty
450  %cmp = fcmp ule double %x, %p
451  ret i1 %cmp
452; CHECK-LABEL: @test19(
453; CHECK: ret i1 true
454}
455
456define i1 @test20(i1 %cond) {
457  %a = alloca i32
458  %b = alloca i32
459  %c = alloca i32
460  br i1 %cond, label %true, label %false
461true:
462  br label %ret
463false:
464  br label %ret
465ret:
466  %p = phi i32* [ %a, %true ], [ %b, %false ]
467  %r = icmp eq i32* %p, %c
468  ret i1 %r
469; CHECK-LABEL: @test20(
470; CHECK: ret i1 false
471}
472
473define i1 @test21(i1 %c1, i1 %c2) {
474  %a = alloca i32
475  %b = alloca i32
476  %c = alloca i32
477  br i1 %c1, label %true, label %false
478true:
479  br label %loop
480false:
481  br label %loop
482loop:
483  %p = phi i32* [ %a, %true ], [ %b, %false ], [ %p, %loop ]
484  %r = icmp eq i32* %p, %c
485  br i1 %c2, label %ret, label %loop
486ret:
487  ret i1 %r
488; CHECK-LABEL: @test21(
489; CHECK: ret i1 false
490}
491
492define void @test22() {
493; CHECK-LABEL: @test22(
494entry:
495  br label %loop
496loop:
497  %phi = phi i32 [ 0, %entry ], [ %y, %loop ]
498  %y = add i32 %phi, 1
499  %o = or i32 %y, %phi
500  %e = icmp eq i32 %o, %y
501  br i1 %e, label %loop, label %ret
502; CHECK: br i1 %e
503ret:
504  ret void
505}
506
507define i32 @test23(i32 %A, i1 %b, i32 * %P) {
508BB0:
509        br label %Loop
510
511Loop:           ; preds = %Loop, %BB0
512        ; PHI has same value always.
513        %B = phi i32 [ %A, %BB0 ], [ 42, %Loop ]
514        %D = add i32 %B, 19
515        store i32 %D, i32* %P
516        br i1 %b, label %Loop, label %Exit
517
518Exit:           ; preds = %Loop
519        %E = add i32 %B, 19
520        ret i32 %E
521; CHECK-LABEL: @test23(
522; CHECK: %phitmp = add i32 %A, 19
523; CHECK: Loop:
524; CHECK-NEXT: %B = phi i32 [ %phitmp, %BB0 ], [ 61, %Loop ]
525; CHECK: Exit:
526; CHECK-NEXT: ret i32 %B
527}
528
529define i32 @test24(i32 %A, i1 %cond) {
530BB0:
531        %X = add nuw nsw i32 %A, 1
532        br i1 %cond, label %BB1, label %BB2
533
534BB1:
535        %Y = add nuw i32 %A, 1
536        br label %BB2
537
538BB2:
539        %C = phi i32 [ %X, %BB0 ], [ %Y, %BB1 ]
540        ret i32 %C
541; CHECK-LABEL: @test24(
542; CHECK-NOT: phi
543; CHECK: BB2:
544; CHECK-NEXT: %C = add nuw i32 %A, 1
545; CHECK-NEXT: ret i32 %C
546}
547
548; Same as test11, but used to be missed due to a bug.
549declare i1 @test25a()
550
551define i1 @test25() {
552entry:
553  %a = alloca i32
554  %i = ptrtoint i32* %a to i64
555  %b = call i1 @test25a()
556  br i1 %b, label %one, label %two
557
558one:
559  %x = phi i64 [%y, %two], [%i, %entry]
560  %c = call i1 @test25a()
561  br i1 %c, label %two, label %end
562
563two:
564  %y = phi i64 [%x, %one], [%i, %entry]
565  %d = call i1 @test25a()
566  br i1 %d, label %one, label %end
567
568end:
569  %f = phi i64 [ %x, %one], [%y, %two]
570  ; Change the %f to %i, and the optimizer suddenly becomes a lot smarter
571  ; even though %f must equal %i at this point
572  %g = inttoptr i64 %f to i32*
573  store i32 10, i32* %g
574  %z = call i1 @test25a()
575  ret i1 %z
576; CHECK-LABEL: @test25(
577; CHECK-NOT: phi i32
578; CHECK: ret i1 %z
579}
580
581declare i1 @test26a()
582
583define i1 @test26(i32 %n) {
584entry:
585  %a = alloca i32
586  %i = ptrtoint i32* %a to i64
587  %b = call i1 @test26a()
588  br label %one
589
590one:
591  %x = phi i64 [%y, %two], [%w, %three], [%i, %entry]
592  %c = call i1 @test26a()
593  switch i32 %n, label %end [
594          i32 2, label %two
595          i32 3, label %three
596  ]
597
598two:
599  %y = phi i64 [%x, %one], [%w, %three]
600  %d = call i1 @test26a()
601  switch i32 %n, label %end [
602          i32 10, label %one
603          i32 30, label %three
604  ]
605
606three:
607  %w = phi i64 [%y, %two], [%x, %one]
608  %e = call i1 @test26a()
609  br i1 %e, label %one, label %two
610
611end:
612  %f = phi i64 [ %x, %one], [%y, %two]
613  ; Change the %f to %i, and the optimizer suddenly becomes a lot smarter
614  ; even though %f must equal %i at this point
615  %g = inttoptr i64 %f to i32*
616  store i32 10, i32* %g
617  %z = call i1 @test26a()
618  ret i1 %z
619; CHECK-LABEL: @test26(
620; CHECK-NOT: phi i32
621; CHECK: ret i1 %z
622}
623
624; CHECK-LABEL: @test27(
625; CHECK: ret i32 undef
626define i32 @test27(i1 %b) {
627entry:
628  br label %done
629done:
630  %y = phi i32 [ undef, %entry ]
631  ret i32 %y
632}
633
634; We should be able to fold the zexts to the other side of the phi
635; even though there's a constant value input to the phi. This is
636; because we can shrink that constant to the smaller phi type.
637
638define i1 @PR24766(i8 %x1, i8 %x2, i8 %condition) {
639entry:
640  %conv = sext i8 %condition to i32
641  switch i32 %conv, label %epilog [
642    i32 0, label %sw1
643    i32 1, label %sw2
644  ]
645
646sw1:
647  %cmp1 = icmp eq i8 %x1, %x2
648  %frombool1 = zext i1 %cmp1 to i8
649  br label %epilog
650
651sw2:
652  %cmp2 = icmp sle i8 %x1, %x2
653  %frombool2 = zext i1 %cmp2 to i8
654  br label %epilog
655
656epilog:
657  %conditionMet = phi i8 [ 0, %entry ], [ %frombool2, %sw2 ], [ %frombool1, %sw1 ]
658  %tobool = icmp ne i8 %conditionMet, 0
659  ret i1 %tobool
660
661; CHECK-LABEL: @PR24766(
662; CHECK: %[[RES:.*]] = phi i1 [ false, %entry ], [ %cmp2, %sw2 ], [ %cmp1, %sw1 ]
663; CHECK-NEXT: ret i1 %[[RES]]
664}
665
666; Same as above (a phi with more than 2 operands), but no constants
667
668define i1 @PR24766_no_constants(i8 %x1, i8 %x2, i8 %condition, i1 %another_condition) {
669entry:
670  %frombool0 = zext i1 %another_condition to i8
671  %conv = sext i8 %condition to i32
672  switch i32 %conv, label %epilog [
673    i32 0, label %sw1
674    i32 1, label %sw2
675  ]
676
677sw1:
678  %cmp1 = icmp eq i8 %x1, %x2
679  %frombool1 = zext i1 %cmp1 to i8
680  br label %epilog
681
682sw2:
683  %cmp2 = icmp sle i8 %x1, %x2
684  %frombool2 = zext i1 %cmp2 to i8
685  br label %epilog
686
687epilog:
688  %conditionMet = phi i8 [ %frombool0, %entry ], [ %frombool2, %sw2 ], [ %frombool1, %sw1 ]
689  %tobool = icmp ne i8 %conditionMet, 0
690  ret i1 %tobool
691
692; CHECK-LABEL: @PR24766_no_constants(
693; CHECK: %[[RES:.*]] = phi i1 [ %another_condition, %entry ], [ %cmp2, %sw2 ], [ %cmp1, %sw1 ]
694; CHECK-NEXT: ret i1 %[[RES]]
695}
696
697; Same as above (a phi with more than 2 operands), but two constants
698
699define i1 @PR24766_two_constants(i8 %x1, i8 %x2, i8 %condition) {
700entry:
701  %conv = sext i8 %condition to i32
702  switch i32 %conv, label %epilog [
703    i32 0, label %sw1
704    i32 1, label %sw2
705  ]
706
707sw1:
708  %cmp1 = icmp eq i8 %x1, %x2
709  %frombool1 = zext i1 %cmp1 to i8
710  br label %epilog
711
712sw2:
713  %cmp2 = icmp sle i8 %x1, %x2
714  %frombool2 = zext i1 %cmp2 to i8
715  br label %epilog
716
717epilog:
718  %conditionMet = phi i8 [ 0, %entry ], [ 1, %sw2 ], [ %frombool1, %sw1 ]
719  %tobool = icmp ne i8 %conditionMet, 0
720  ret i1 %tobool
721
722; CHECK-LABEL: @PR24766_two_constants(
723; CHECK: %[[RES:.*]] = phi i1 [ false, %entry ], [ true, %sw2 ], [ %cmp1, %sw1 ]
724; CHECK-NEXT: ret i1 %[[RES]]
725}
726
727; Same as above (a phi with more than 2 operands), but two constants and two variables
728
729define i1 @PR24766_two_constants_two_var(i8 %x1, i8 %x2, i8 %condition) {
730entry:
731  %conv = sext i8 %condition to i32
732  switch i32 %conv, label %epilog [
733    i32 0, label %sw1
734    i32 1, label %sw2
735    i32 2, label %sw3
736  ]
737
738sw1:
739  %cmp1 = icmp eq i8 %x1, %x2
740  %frombool1 = zext i1 %cmp1 to i8
741  br label %epilog
742
743sw2:
744  %cmp2 = icmp sle i8 %x1, %x2
745  %frombool2 = zext i1 %cmp2 to i8
746  br label %epilog
747
748sw3:
749  %cmp3 = icmp sge i8 %x1, %x2
750  %frombool3 = zext i1 %cmp3 to i8
751  br label %epilog
752
753epilog:
754  %conditionMet = phi i8 [ 0, %entry ], [ %frombool2, %sw2 ], [ %frombool1, %sw1 ], [ 1, %sw3 ]
755  %tobool = icmp ne i8 %conditionMet, 0
756  ret i1 %tobool
757
758; CHECK-LABEL: @PR24766_two_constants_two_var(
759; CHECK: %[[RES:.*]] = phi i1 [ false, %entry ], [ %cmp2, %sw2 ], [ %cmp1, %sw1 ], [ true, %sw3 ]
760; CHECK-NEXT: ret i1 %[[RES]]
761}
762
763; CHECK-LABEL: phi_allnonzeroconstant
764; CHECK-NOT: phi i32
765; CHECK: ret i1 false
766define i1 @phi_allnonzeroconstant(i1 %c, i32 %a, i32 %b) {
767entry:
768  br i1 %c, label %if.then, label %if.else
769
770if.then:                                          ; preds = %entry
771  br label %if.end
772
773if.else:                                          ; preds = %entry
774  call void @dummy()
775
776  br label %if.end
777
778if.end:                                           ; preds = %if.else, %if.then
779  %x.0 = phi i32 [ 1, %if.then ], [ 2, %if.else ]
780  %or = or i32 %x.0, %a
781  %cmp1 = icmp eq i32 %or, 0
782  ret i1 %cmp1
783}
784
785declare void @dummy()
786
787; CHECK-LABEL: @phi_knownnonzero_eq
788; CHECK-LABEL: if.then:
789; CHECK-NOT: select
790; CHECK-LABEL: if.end:
791; CHECK: phi i32 [ 1, %if.then ]
792define i1 @phi_knownnonzero_eq(i32 %n, i32 %s, i32* nocapture readonly %P) {
793entry:
794  %tobool = icmp slt  i32 %n, %s
795  br i1 %tobool, label %if.end, label %if.then
796
797if.then:                                          ; preds = %entry
798  %0 = load i32, i32* %P
799  %cmp = icmp eq i32 %n, %0
800  %1 = select i1 %cmp, i32 1, i32 2
801  br label %if.end
802
803if.end:                                           ; preds = %entry, %if.then
804  %a.0 = phi i32 [ %1,  %if.then ], [ %n, %entry ]
805  %cmp1 = icmp eq i32 %a.0, 0
806  ret i1  %cmp1
807}
808
809; CHECK-LABEL: @phi_knownnonzero_ne
810; CHECK-LABEL: if.then:
811; CHECK-NOT: select
812; CHECK-LABEL: if.end:
813; CHECK: phi i32 [ 1, %if.then ]
814define i1 @phi_knownnonzero_ne(i32 %n, i32 %s, i32* nocapture readonly %P) {
815entry:
816  %tobool = icmp slt  i32 %n, %s
817  br i1 %tobool, label %if.end, label %if.then
818
819if.then:                                          ; preds = %entry
820  %0 = load i32, i32* %P
821  %cmp = icmp eq i32 %n, %0
822  %1 = select i1 %cmp, i32 1, i32 2
823  br label %if.end
824
825if.end:                                           ; preds = %entry, %if.then
826  %a.0 = phi i32 [ %1,  %if.then ], [ %n, %entry ]
827  %cmp1 = icmp ne i32 %a.0, 0
828  ret i1  %cmp1
829}
830
831; CHECK-LABEL: @phi_knownnonzero_eq_2
832; CHECK-LABEL: if.then:
833; CHECK-NOT: select
834; CHECK-LABEL: if.end:
835; CHECK: phi i32 [ 2, %if.else ]
836define i1 @phi_knownnonzero_eq_2(i32 %n, i32 %s, i32* nocapture readonly %P) {
837entry:
838  %tobool = icmp slt  i32 %n, %s
839  br i1 %tobool, label %if.then, label %if.end
840
841if.then:
842  %tobool2 = icmp slt  i32 %n, %s
843  br i1 %tobool2, label %if.else, label %if.end
844
845if.else:                                          ; preds = %entry
846  %0 = load i32, i32* %P
847  %cmp = icmp eq i32 %n, %0
848  %1 = select i1 %cmp, i32 1, i32 2
849  br label %if.end
850
851if.end:                                           ; preds = %entry, %if.then
852  %a.0 = phi i32 [ %1,  %if.else], [ %n, %entry ], [2, %if.then]
853  %cmp1 = icmp eq i32 %a.0, 0
854  ret i1  %cmp1
855}
856
857; CHECK-LABEL: @phi_knownnonzero_ne_2
858; CHECK-LABEL: if.then:
859; CHECK-NOT: select
860; CHECK-LABEL: if.end:
861; CHECK: phi i32 [ 2, %if.else ]
862define i1 @phi_knownnonzero_ne_2(i32 %n, i32 %s, i32* nocapture readonly %P) {
863entry:
864  %tobool = icmp slt  i32 %n, %s
865  br i1 %tobool, label %if.then, label %if.end
866
867if.then:
868  %tobool2 = icmp slt  i32 %n, %s
869  br i1 %tobool2, label %if.else, label %if.end
870
871if.else:                                          ; preds = %entry
872  %0 = load i32, i32* %P
873  %cmp = icmp eq i32 %n, %0
874  %1 = select i1 %cmp, i32 1, i32 2
875  br label %if.end
876
877if.end:                                           ; preds = %entry, %if.then
878  %a.0 = phi i32 [ %1,  %if.else], [ %n, %entry ], [2, %if.then]
879  %cmp1 = icmp ne i32 %a.0, 0
880  ret i1  %cmp1
881}
882