1 ; RUN: opt < %s -instcombine -S | FileCheck %s
2 ; This test makes sure that these instructions are properly eliminated.
5 target datalayout = "e-p:64:64-p1:16:16-p2:32:32:32-p3:64:64:64"
7 define i32 @test1(i32 %A, i32 %B) {
8 %C = select i1 false, i32 %A, i32 %B
10 ; CHECK-LABEL: @test1(
14 define i32 @test2(i32 %A, i32 %B) {
15 %C = select i1 true, i32 %A, i32 %B
17 ; CHECK-LABEL: @test2(
22 define i32 @test3(i1 %C, i32 %I) {
24 %V = select i1 %C, i32 %I, i32 %I
26 ; CHECK-LABEL: @test3(
30 define i1 @test4(i1 %C) {
32 %V = select i1 %C, i1 true, i1 false
34 ; CHECK-LABEL: @test4(
38 define i1 @test5(i1 %C) {
40 %V = select i1 %C, i1 false, i1 true
42 ; CHECK-LABEL: @test5(
43 ; CHECK: xor i1 %C, true
47 define i32 @test6(i1 %C) {
49 %V = select i1 %C, i32 1, i32 0
51 ; CHECK-LABEL: @test6(
52 ; CHECK: %V = zext i1 %C to i32
56 define i1 @test7(i1 %C, i1 %X) {
58 %R = select i1 %C, i1 true, i1 %X
60 ; CHECK-LABEL: @test7(
61 ; CHECK: %R = or i1 %C, %X
65 define i1 @test8(i1 %C, i1 %X) {
67 %R = select i1 %C, i1 %X, i1 false
69 ; CHECK-LABEL: @test8(
70 ; CHECK: %R = and i1 %C, %X
74 define i1 @test9(i1 %C, i1 %X) {
76 %R = select i1 %C, i1 false, i1 %X
78 ; CHECK-LABEL: @test9(
79 ; CHECK: xor i1 %C, true
84 define i1 @test10(i1 %C, i1 %X) {
86 %R = select i1 %C, i1 %X, i1 true
88 ; CHECK-LABEL: @test10(
89 ; CHECK: xor i1 %C, true
94 define i32 @test11(i32 %a) {
95 %C = icmp eq i32 %a, 0
96 %R = select i1 %C, i32 0, i32 1
98 ; CHECK-LABEL: @test11(
99 ; CHECK: icmp ne i32 %a, 0
100 ; CHECK: %R = zext i1
104 define i32 @test12(i1 %cond, i32 %a) {
106 %c = select i1 %cond, i32 %b, i32 %a
108 ; CHECK-LABEL: @test12(
109 ; CHECK: %b = zext i1 %cond to i32
110 ; CHECK: %c = or i32 %b, %a
114 define i32 @test12a(i1 %cond, i32 %a) {
116 %c = select i1 %cond, i32 %b, i32 %a
118 ; CHECK-LABEL: @test12a(
119 ; CHECK: %b = zext i1 %cond to i32
120 ; CHECK: %c = ashr i32 %a, %b
124 define i32 @test12b(i1 %cond, i32 %a) {
126 %c = select i1 %cond, i32 %a, i32 %b
128 ; CHECK-LABEL: @test12b(
129 ; CHECK: zext i1 %cond to i32
130 ; CHECK: %b = xor i32
131 ; CHECK: %c = ashr i32 %a, %b
135 define i32 @test13(i32 %a, i32 %b) {
136 %C = icmp eq i32 %a, %b
137 %V = select i1 %C, i32 %a, i32 %b
139 ; CHECK-LABEL: @test13(
143 define i32 @test13a(i32 %a, i32 %b) {
144 %C = icmp ne i32 %a, %b
145 %V = select i1 %C, i32 %a, i32 %b
147 ; CHECK-LABEL: @test13a(
151 define i32 @test13b(i32 %a, i32 %b) {
152 %C = icmp eq i32 %a, %b
153 %V = select i1 %C, i32 %b, i32 %a
155 ; CHECK-LABEL: @test13b(
159 define i1 @test14a(i1 %C, i32 %X) {
160 %V = select i1 %C, i32 %X, i32 0
162 %R = icmp slt i32 %V, 1
164 ; CHECK-LABEL: @test14a(
165 ; CHECK: icmp slt i32 %X, 1
166 ; CHECK: xor i1 %C, true
171 define i1 @test14b(i1 %C, i32 %X) {
172 %V = select i1 %C, i32 0, i32 %X
174 %R = icmp slt i32 %V, 1
176 ; CHECK-LABEL: @test14b(
177 ; CHECK: icmp slt i32 %X, 1
182 ;; Code sequence for (X & 16) ? 16 : 0
183 define i32 @test15a(i32 %X) {
185 %t2 = icmp eq i32 %t1, 0
186 %t3 = select i1 %t2, i32 0, i32 16
188 ; CHECK-LABEL: @test15a(
189 ; CHECK: %t1 = and i32 %X, 16
193 ;; Code sequence for (X & 32) ? 0 : 24
194 define i32 @test15b(i32 %X) {
196 %t2 = icmp eq i32 %t1, 0
197 %t3 = select i1 %t2, i32 32, i32 0
199 ; CHECK-LABEL: @test15b(
200 ; CHECK: %t1 = and i32 %X, 32
201 ; CHECK: xor i32 %t1, 32
205 ;; Alternate code sequence for (X & 16) ? 16 : 0
206 define i32 @test15c(i32 %X) {
208 %t2 = icmp eq i32 %t1, 16
209 %t3 = select i1 %t2, i32 16, i32 0
211 ; CHECK-LABEL: @test15c(
212 ; CHECK: %t1 = and i32 %X, 16
216 ;; Alternate code sequence for (X & 16) ? 16 : 0
217 define i32 @test15d(i32 %X) {
219 %t2 = icmp ne i32 %t1, 0
220 %t3 = select i1 %t2, i32 16, i32 0
222 ; CHECK-LABEL: @test15d(
223 ; CHECK: %t1 = and i32 %X, 16
227 ;; (a & 128) ? 256 : 0
228 define i32 @test15e(i32 %X) {
229 %t1 = and i32 %X, 128
230 %t2 = icmp ne i32 %t1, 0
231 %t3 = select i1 %t2, i32 256, i32 0
233 ; CHECK-LABEL: @test15e(
234 ; CHECK: %t1 = shl i32 %X, 1
235 ; CHECK: and i32 %t1, 256
239 ;; (a & 128) ? 0 : 256
240 define i32 @test15f(i32 %X) {
241 %t1 = and i32 %X, 128
242 %t2 = icmp ne i32 %t1, 0
243 %t3 = select i1 %t2, i32 0, i32 256
245 ; CHECK-LABEL: @test15f(
246 ; CHECK: %t1 = shl i32 %X, 1
247 ; CHECK: and i32 %t1, 256
248 ; CHECK: xor i32 %{{.*}}, 256
253 define i32 @test15g(i32 %X) {
255 %t2 = icmp ne i32 %t1, 0
256 %t3 = select i1 %t2, i32 -1, i32 -9
258 ; CHECK-LABEL: @test15g(
259 ; CHECK-NEXT: %1 = or i32 %X, -9
260 ; CHECK-NEXT: ret i32 %1
264 define i32 @test15h(i32 %X) {
266 %t2 = icmp ne i32 %t1, 0
267 %t3 = select i1 %t2, i32 -9, i32 -1
269 ; CHECK-LABEL: @test15h(
270 ; CHECK-NEXT: %1 = or i32 %X, -9
271 ; CHECK-NEXT: %2 = xor i32 %1, 8
272 ; CHECK-NEXT: ret i32 %2
275 ;; (a & 2) ? 577 : 1089
276 define i32 @test15i(i32 %X) {
278 %t2 = icmp ne i32 %t1, 0
279 %t3 = select i1 %t2, i32 577, i32 1089
281 ; CHECK-LABEL: @test15i(
282 ; CHECK-NEXT: %t1 = shl i32 %X, 8
283 ; CHECK-NEXT: %1 = and i32 %t1, 512
284 ; CHECK-NEXT: %2 = xor i32 %1, 512
285 ; CHECK-NEXT: %3 = add nuw nsw i32 %2, 577
286 ; CHECK-NEXT: ret i32 %3
289 ;; (a & 2) ? 1089 : 577
290 define i32 @test15j(i32 %X) {
292 %t2 = icmp ne i32 %t1, 0
293 %t3 = select i1 %t2, i32 1089, i32 577
295 ; CHECK-LABEL: @test15j(
296 ; CHECK-NEXT: %t1 = shl i32 %X, 8
297 ; CHECK-NEXT: %1 = and i32 %t1, 512
298 ; CHECK-NEXT: %2 = add nuw nsw i32 %1, 577
299 ; CHECK-NEXT: ret i32 %2
302 define i32 @test16(i1 %C, i32* %P) {
303 %P2 = select i1 %C, i32* %P, i32* null
304 %V = load i32, i32* %P2
306 ; CHECK-LABEL: @test16(
307 ; CHECK-NEXT: %V = load i32, i32* %P
311 ;; It may be legal to load from a null address in a non-zero address space
312 define i32 @test16_neg(i1 %C, i32 addrspace(1)* %P) {
313 %P2 = select i1 %C, i32 addrspace(1)* %P, i32 addrspace(1)* null
314 %V = load i32, i32 addrspace(1)* %P2
316 ; CHECK-LABEL: @test16_neg
317 ; CHECK-NEXT: %P2 = select i1 %C, i32 addrspace(1)* %P, i32 addrspace(1)* null
318 ; CHECK-NEXT: %V = load i32, i32 addrspace(1)* %P2
321 define i32 @test16_neg2(i1 %C, i32 addrspace(1)* %P) {
322 %P2 = select i1 %C, i32 addrspace(1)* null, i32 addrspace(1)* %P
323 %V = load i32, i32 addrspace(1)* %P2
325 ; CHECK-LABEL: @test16_neg2
326 ; CHECK-NEXT: %P2 = select i1 %C, i32 addrspace(1)* null, i32 addrspace(1)* %P
327 ; CHECK-NEXT: %V = load i32, i32 addrspace(1)* %P2
331 define i1 @test17(i32* %X, i1 %C) {
332 %R = select i1 %C, i32* %X, i32* null
333 %RV = icmp eq i32* %R, null
335 ; CHECK-LABEL: @test17(
336 ; CHECK: icmp eq i32* %X, null
337 ; CHECK: xor i1 %C, true
342 define i32 @test18(i32 %X, i32 %Y, i1 %C) {
343 %R = select i1 %C, i32 %X, i32 0
346 ; CHECK-LABEL: @test18(
347 ; CHECK: %V = sdiv i32 %Y, %X
351 define i32 @test19(i32 %x) {
352 %tmp = icmp ugt i32 %x, 2147483647
353 %retval = select i1 %tmp, i32 -1, i32 0
355 ; CHECK-LABEL: @test19(
356 ; CHECK-NEXT: ashr i32 %x, 31
357 ; CHECK-NEXT: ret i32
360 define i32 @test20(i32 %x) {
361 %tmp = icmp slt i32 %x, 0
362 %retval = select i1 %tmp, i32 -1, i32 0
364 ; CHECK-LABEL: @test20(
365 ; CHECK-NEXT: ashr i32 %x, 31
366 ; CHECK-NEXT: ret i32
369 define i64 @test21(i32 %x) {
370 %tmp = icmp slt i32 %x, 0
371 %retval = select i1 %tmp, i64 -1, i64 0
373 ; CHECK-LABEL: @test21(
374 ; CHECK-NEXT: ashr i32 %x, 31
375 ; CHECK-NEXT: sext i32
376 ; CHECK-NEXT: ret i64
379 define i16 @test22(i32 %x) {
380 %tmp = icmp slt i32 %x, 0
381 %retval = select i1 %tmp, i16 -1, i16 0
383 ; CHECK-LABEL: @test22(
384 ; CHECK-NEXT: ashr i32 %x, 31
385 ; CHECK-NEXT: trunc i32
386 ; CHECK-NEXT: ret i16
389 define i1 @test23(i1 %a, i1 %b) {
390 %c = select i1 %a, i1 %b, i1 %a
392 ; CHECK-LABEL: @test23(
393 ; CHECK-NEXT: %c = and i1 %a, %b
394 ; CHECK-NEXT: ret i1 %c
397 define i1 @test24(i1 %a, i1 %b) {
398 %c = select i1 %a, i1 %a, i1 %b
400 ; CHECK-LABEL: @test24(
401 ; CHECK-NEXT: %c = or i1 %a, %b
402 ; CHECK-NEXT: ret i1 %c
405 define i32 @test25(i1 %c) {
407 br i1 %c, label %jump, label %ret
411 %a = phi i1 [true, %jump], [false, %entry]
412 %b = select i1 %a, i32 10, i32 20
414 ; CHECK-LABEL: @test25(
415 ; CHECK: %a = phi i32 [ 10, %jump ], [ 20, %entry ]
416 ; CHECK-NEXT: ret i32 %a
419 define i32 @test26(i1 %cond) {
421 br i1 %cond, label %jump, label %ret
423 %c = or i1 false, false
426 %a = phi i1 [true, %entry], [%c, %jump]
427 %b = select i1 %a, i32 20, i32 10
429 ; CHECK-LABEL: @test26(
430 ; CHECK: %a = phi i32 [ 20, %entry ], [ 10, %jump ]
431 ; CHECK-NEXT: ret i32 %a
434 define i32 @test27(i1 %c, i32 %A, i32 %B) {
436 br i1 %c, label %jump, label %ret
440 %a = phi i1 [true, %jump], [false, %entry]
441 %b = select i1 %a, i32 %A, i32 %B
443 ; CHECK-LABEL: @test27(
444 ; CHECK: %a = phi i32 [ %A, %jump ], [ %B, %entry ]
445 ; CHECK-NEXT: ret i32 %a
448 define i32 @test28(i1 %cond, i32 %A, i32 %B) {
450 br i1 %cond, label %jump, label %ret
454 %c = phi i32 [%A, %jump], [%B, %entry]
455 %a = phi i1 [true, %jump], [false, %entry]
456 %b = select i1 %a, i32 %A, i32 %c
458 ; CHECK-LABEL: @test28(
459 ; CHECK: %a = phi i32 [ %A, %jump ], [ %B, %entry ]
460 ; CHECK-NEXT: ret i32 %a
463 define i32 @test29(i1 %cond, i32 %A, i32 %B) {
465 br i1 %cond, label %jump, label %ret
469 %c = phi i32 [%A, %jump], [%B, %entry]
470 %a = phi i1 [true, %jump], [false, %entry]
474 %b = select i1 %a, i32 %A, i32 %c
476 ; CHECK-LABEL: @test29(
477 ; CHECK: %a = phi i32 [ %A, %jump ], [ %B, %entry ]
482 ; SMAX(SMAX(x, y), x) -> SMAX(x, y)
483 define i32 @test30(i32 %x, i32 %y) {
484 %cmp = icmp sgt i32 %x, %y
485 %cond = select i1 %cmp, i32 %x, i32 %y
487 %cmp5 = icmp sgt i32 %cond, %x
488 %retval = select i1 %cmp5, i32 %cond, i32 %x
490 ; CHECK-LABEL: @test30(
491 ; CHECK: ret i32 %cond
494 ; UMAX(UMAX(x, y), x) -> UMAX(x, y)
495 define i32 @test31(i32 %x, i32 %y) {
496 %cmp = icmp ugt i32 %x, %y
497 %cond = select i1 %cmp, i32 %x, i32 %y
498 %cmp5 = icmp ugt i32 %cond, %x
499 %retval = select i1 %cmp5, i32 %cond, i32 %x
501 ; CHECK-LABEL: @test31(
502 ; CHECK: ret i32 %cond
505 ; SMIN(SMIN(x, y), x) -> SMIN(x, y)
506 define i32 @test32(i32 %x, i32 %y) {
507 %cmp = icmp sgt i32 %x, %y
508 %cond = select i1 %cmp, i32 %y, i32 %x
509 %cmp5 = icmp sgt i32 %cond, %x
510 %retval = select i1 %cmp5, i32 %x, i32 %cond
512 ; CHECK-LABEL: @test32(
513 ; CHECK: ret i32 %cond
516 ; MAX(MIN(x, y), x) -> x
517 define i32 @test33(i32 %x, i32 %y) {
518 %cmp = icmp sgt i32 %x, %y
519 %cond = select i1 %cmp, i32 %y, i32 %x
520 %cmp5 = icmp sgt i32 %cond, %x
521 %retval = select i1 %cmp5, i32 %cond, i32 %x
523 ; CHECK-LABEL: @test33(
527 ; MIN(MAX(x, y), x) -> x
528 define i32 @test34(i32 %x, i32 %y) {
529 %cmp = icmp sgt i32 %x, %y
530 %cond = select i1 %cmp, i32 %x, i32 %y
531 %cmp5 = icmp sgt i32 %cond, %x
532 %retval = select i1 %cmp5, i32 %x, i32 %cond
534 ; CHECK-LABEL: @test34(
538 define i32 @test35(i32 %x) {
539 %cmp = icmp sge i32 %x, 0
540 %cond = select i1 %cmp, i32 60, i32 100
542 ; CHECK-LABEL: @test35(
543 ; CHECK: ashr i32 %x, 31
544 ; CHECK: and i32 {{.*}}, 40
545 ; CHECK: add nuw nsw i32 {{.*}}, 60
549 define i32 @test36(i32 %x) {
550 %cmp = icmp slt i32 %x, 0
551 %cond = select i1 %cmp, i32 60, i32 100
553 ; CHECK-LABEL: @test36(
554 ; CHECK: ashr i32 %x, 31
555 ; CHECK: and i32 {{.*}}, -40
556 ; CHECK: add nsw i32 {{.*}}, 100
560 define i32 @test37(i32 %x) {
561 %cmp = icmp sgt i32 %x, -1
562 %cond = select i1 %cmp, i32 1, i32 -1
564 ; CHECK-LABEL: @test37(
565 ; CHECK: ashr i32 %x, 31
566 ; CHECK: or i32 {{.*}}, 1
570 define i1 @test38(i1 %cond) {
573 %ptr = select i1 %cond, i32* %zero, i32* %one
574 %isnull = icmp eq i32* %ptr, null
576 ; CHECK-LABEL: @test38(
577 ; CHECK: ret i1 false
580 define i1 @test39(i1 %cond, double %x) {
581 %s = select i1 %cond, double %x, double 0x7FF0000000000000 ; RHS = +infty
582 %cmp = fcmp ule double %x, %s
584 ; CHECK-LABEL: @test39(
588 define i1 @test40(i1 %cond) {
592 %s = select i1 %cond, i32* %a, i32* %b
593 %r = icmp eq i32* %s, %c
595 ; CHECK-LABEL: @test40(
596 ; CHECK: ret i1 false
599 define i32 @test41(i1 %cond, i32 %x, i32 %y) {
601 %s = select i1 %cond, i32 %y, i32 %z
604 ; CHECK-LABEL: @test41(
605 ; CHECK-NEXT: and i32 %x, %y
606 ; CHECK-NEXT: ret i32
609 define i32 @test42(i32 %x, i32 %y) {
611 %cond = icmp eq i32 %x, 0
612 %c = select i1 %cond, i32 %b, i32 %y
614 ; CHECK-LABEL: @test42(
615 ; CHECK-NEXT: %cond = icmp eq i32 %x, 0
616 ; CHECK-NEXT: %b = sext i1 %cond to i32
617 ; CHECK-NEXT: %c = add i32 %b, %y
618 ; CHECK-NEXT: ret i32 %c
621 define i64 @test43(i32 %a) nounwind {
622 %a_ext = sext i32 %a to i64
623 %is_a_nonnegative = icmp sgt i32 %a, -1
624 %max = select i1 %is_a_nonnegative, i64 %a_ext, i64 0
626 ; CHECK-LABEL: @test43(
627 ; CHECK-NEXT: %a_ext = sext i32 %a to i64
628 ; CHECK-NEXT: %is_a_nonnegative = icmp slt i64 %a_ext, 0
629 ; CHECK-NEXT: %max = select i1 %is_a_nonnegative, i64 0, i64 %a_ext
630 ; CHECK-NEXT: ret i64 %max
633 define i64 @test44(i32 %a) nounwind {
634 %a_ext = sext i32 %a to i64
635 %is_a_nonpositive = icmp slt i32 %a, 1
636 %min = select i1 %is_a_nonpositive, i64 %a_ext, i64 0
638 ; CHECK-LABEL: @test44(
639 ; CHECK-NEXT: %a_ext = sext i32 %a to i64
640 ; CHECK-NEXT: %is_a_nonpositive = icmp sgt i64 %a_ext, 0
641 ; CHECK-NEXT: %min = select i1 %is_a_nonpositive, i64 0, i64 %a_ext
642 ; CHECK-NEXT: ret i64 %min
644 define i64 @test45(i32 %a) nounwind {
645 %a_ext = zext i32 %a to i64
646 %is_a_nonnegative = icmp ugt i32 %a, 2
647 %max = select i1 %is_a_nonnegative, i64 %a_ext, i64 3
649 ; CHECK-LABEL: @test45(
650 ; CHECK-NEXT: %a_ext = zext i32 %a to i64
651 ; CHECK-NEXT: %is_a_nonnegative = icmp ult i64 %a_ext, 3
652 ; CHECK-NEXT: %max = select i1 %is_a_nonnegative, i64 3, i64 %a_ext
653 ; CHECK-NEXT: ret i64 %max
656 define i64 @test46(i32 %a) nounwind {
657 %a_ext = zext i32 %a to i64
658 %is_a_nonpositive = icmp ult i32 %a, 3
659 %min = select i1 %is_a_nonpositive, i64 %a_ext, i64 2
661 ; CHECK-LABEL: @test46(
662 ; CHECK-NEXT: %a_ext = zext i32 %a to i64
663 ; CHECK-NEXT: %is_a_nonpositive = icmp ugt i64 %a_ext, 2
664 ; CHECK-NEXT: %min = select i1 %is_a_nonpositive, i64 2, i64 %a_ext
665 ; CHECK-NEXT: ret i64 %min
667 define i64 @test47(i32 %a) nounwind {
668 %a_ext = sext i32 %a to i64
669 %is_a_nonnegative = icmp ugt i32 %a, 2
670 %max = select i1 %is_a_nonnegative, i64 %a_ext, i64 3
672 ; CHECK-LABEL: @test47(
673 ; CHECK-NEXT: %a_ext = sext i32 %a to i64
674 ; CHECK-NEXT: %is_a_nonnegative = icmp ult i64 %a_ext, 3
675 ; CHECK-NEXT: %max = select i1 %is_a_nonnegative, i64 3, i64 %a_ext
676 ; CHECK-NEXT: ret i64 %max
679 define i64 @test48(i32 %a) nounwind {
680 %a_ext = sext i32 %a to i64
681 %is_a_nonpositive = icmp ult i32 %a, 3
682 %min = select i1 %is_a_nonpositive, i64 %a_ext, i64 2
684 ; CHECK-LABEL: @test48(
685 ; CHECK-NEXT: %a_ext = sext i32 %a to i64
686 ; CHECK-NEXT: %is_a_nonpositive = icmp ugt i64 %a_ext, 2
687 ; CHECK-NEXT: %min = select i1 %is_a_nonpositive, i64 2, i64 %a_ext
688 ; CHECK-NEXT: ret i64 %min
691 define i64 @test49(i32 %a) nounwind {
692 %a_ext = sext i32 %a to i64
693 %is_a_nonpositive = icmp ult i32 %a, 3
694 %min = select i1 %is_a_nonpositive, i64 2, i64 %a_ext
696 ; CHECK-LABEL: @test49(
697 ; CHECK-NEXT: %a_ext = sext i32 %a to i64
698 ; CHECK-NEXT: %is_a_nonpositive = icmp ugt i64 %a_ext, 2
699 ; CHECK-NEXT: %min = select i1 %is_a_nonpositive, i64 %a_ext, i64 2
700 ; CHECK-NEXT: ret i64 %min
702 define i64 @test50(i32 %a) nounwind {
703 %is_a_nonpositive = icmp ult i32 %a, 3
704 %a_ext = sext i32 %a to i64
705 %min = select i1 %is_a_nonpositive, i64 2, i64 %a_ext
707 ; CHECK-LABEL: @test50(
708 ; CHECK-NEXT: %a_ext = sext i32 %a to i64
709 ; CHECK-NEXT: %is_a_nonpositive = icmp ugt i64 %a_ext, 2
710 ; CHECK-NEXT: %min = select i1 %is_a_nonpositive, i64 %a_ext, i64 2
711 ; CHECK-NEXT: ret i64 %min
716 ; This select instruction can't be eliminated because trying to do so would
717 ; change the number of vector elements. This used to assert.
718 define i48 @test51(<3 x i1> %icmp, <3 x i16> %tmp) {
719 ; CHECK-LABEL: @test51(
720 %select = select <3 x i1> %icmp, <3 x i16> zeroinitializer, <3 x i16> %tmp
721 ; CHECK: select <3 x i1>
722 %tmp2 = bitcast <3 x i16> %select to i48
728 define i32 @test52(i32 %n, i32 %m) nounwind {
729 ; CHECK-LABEL: @test52(
730 %cmp = icmp sgt i32 %n, %m
731 %. = select i1 %cmp, i32 1, i32 3
732 %add = add nsw i32 %., 3
733 %storemerge = select i1 %cmp, i32 %., i32 %add
734 ; CHECK: select i1 %cmp, i32 1, i32 6
739 define i32 @test53(i32 %x) nounwind {
741 %cmp = icmp eq i32 %and, %x
742 %sel = select i1 %cmp, i32 2, i32 1
744 ; CHECK-LABEL: @test53(
745 ; CHECK: select i1 %cmp
749 define i32 @test54(i32 %X, i32 %Y) {
750 %A = ashr exact i32 %X, %Y
751 %B = icmp eq i32 %A, 0
752 %C = select i1 %B, i32 %A, i32 1
754 ; CHECK-LABEL: @test54(
757 ; CHECK: icmp ne i32 %X, 0
762 define i1 @test55(i1 %X, i32 %Y, i32 %Z) {
763 %A = ashr exact i32 %Y, %Z
764 %B = select i1 %X, i32 %Y, i32 %A
765 %C = icmp eq i32 %B, 0
767 ; CHECK-LABEL: @test55(
774 define i32 @test56(i16 %x) nounwind {
775 %tobool = icmp eq i16 %x, 0
776 %conv = zext i16 %x to i32
777 %cond = select i1 %tobool, i32 0, i32 %conv
779 ; CHECK-LABEL: @test56(
784 define i32 @test57(i32 %x, i32 %y) nounwind {
785 %and = and i32 %x, %y
786 %tobool = icmp eq i32 %x, 0
787 %.and = select i1 %tobool, i32 0, i32 %and
789 ; CHECK-LABEL: @test57(
790 ; CHECK-NEXT: and i32 %x, %y
794 define i32 @test58(i16 %x) nounwind {
795 %tobool = icmp ne i16 %x, 1
796 %conv = zext i16 %x to i32
797 %cond = select i1 %tobool, i32 %conv, i32 1
799 ; CHECK-LABEL: @test58(
804 define i32 @test59(i32 %x, i32 %y) nounwind {
805 %and = and i32 %x, %y
806 %tobool = icmp ne i32 %x, %y
807 %.and = select i1 %tobool, i32 %and, i32 %y
809 ; CHECK-LABEL: @test59(
810 ; CHECK-NEXT: and i32 %x, %y
814 define i1 @test60(i32 %x, i1* %y) nounwind {
815 %cmp = icmp eq i32 %x, 0
816 %load = load i1, i1* %y, align 1
817 %cmp1 = icmp slt i32 %x, 1
818 %sel = select i1 %cmp, i1 %load, i1 %cmp1
820 ; CHECK-LABEL: @test60(
824 @glbl = constant i32 10
825 define i32 @test61(i32* %ptr) {
826 %A = load i32, i32* %ptr
827 %B = icmp eq i32* %ptr, @glbl
828 %C = select i1 %B, i32 %A, i32 10
830 ; CHECK-LABEL: @test61(
834 define i1 @test62(i1 %A, i1 %B) {
835 %not = xor i1 %A, true
836 %C = select i1 %A, i1 %not, i1 %B
838 ; CHECK-LABEL: @test62(
839 ; CHECK: %not = xor i1 %A, true
840 ; CHECK: %C = and i1 %not, %B
844 define i1 @test63(i1 %A, i1 %B) {
845 %not = xor i1 %A, true
846 %C = select i1 %A, i1 %B, i1 %not
848 ; CHECK-LABEL: @test63(
849 ; CHECK: %not = xor i1 %A, true
850 ; CHECK: %C = or i1 %B, %not
855 define void @test64(i32 %p, i16 %b) noreturn nounwind {
857 %p.addr.0.insert.mask = and i32 %p, -65536
858 %conv2 = and i32 %p, 65535
859 br i1 undef, label %lor.rhs, label %lor.end
862 %p.addr.0.extract.trunc = trunc i32 %p.addr.0.insert.mask to i16
863 %phitmp = zext i16 %p.addr.0.extract.trunc to i32
867 %t.1 = phi i32 [ 0, %entry ], [ %phitmp, %lor.rhs ]
868 %conv6 = zext i16 %b to i32
869 %div = udiv i32 %conv6, %t.1
870 %tobool8 = icmp eq i32 %div, 0
871 %cmp = icmp eq i32 %t.1, 0
872 %cmp12 = icmp ult i32 %conv2, 2
873 %cmp.sink = select i1 %tobool8, i1 %cmp12, i1 %cmp
874 br i1 %cmp.sink, label %cond.end17, label %cond.false16
884 ; CHECK-LABEL: @test64(
888 ; CHECK-LABEL: @select_icmp_eq_and_1_0_or_2(
889 ; CHECK-NEXT: [[SHL:%[a-z0-9]+]] = shl i32 %x, 1
890 ; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i32 [[SHL]], 2
891 ; CHECK-NEXT: [[OR:%[a-z0-9]+]] = or i32 [[AND]], %y
892 ; CHECK-NEXT: ret i32 [[OR]]
893 define i32 @select_icmp_eq_and_1_0_or_2(i32 %x, i32 %y) {
895 %cmp = icmp eq i32 %and, 0
897 %select = select i1 %cmp, i32 %y, i32 %or
901 ; CHECK-LABEL: @select_icmp_eq_and_32_0_or_8(
902 ; CHECK-NEXT: [[LSHR:%[a-z0-9]+]] = lshr i32 %x, 2
903 ; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i32 [[LSHR]], 8
904 ; CHECK-NEXT: [[OR:%[a-z0-9]+]] = or i32 [[AND]], %y
905 ; CHECK-NEXT: ret i32 [[OR]]
906 define i32 @select_icmp_eq_and_32_0_or_8(i32 %x, i32 %y) {
907 %and = and i32 %x, 32
908 %cmp = icmp eq i32 %and, 0
910 %select = select i1 %cmp, i32 %y, i32 %or
914 ; CHECK-LABEL: @select_icmp_ne_0_and_4096_or_4096(
915 ; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i32 %x, 4096
916 ; CHECK-NEXT: [[XOR:%[a-z0-9]+]] = xor i32 [[AND]], 4096
917 ; CHECK-NEXT: [[OR:%[a-z0-9]+]] = or i32 [[XOR]], %y
918 ; CHECK-NEXT: ret i32 [[OR]]
919 define i32 @select_icmp_ne_0_and_4096_or_4096(i32 %x, i32 %y) {
920 %and = and i32 %x, 4096
921 %cmp = icmp ne i32 0, %and
922 %or = or i32 %y, 4096
923 %select = select i1 %cmp, i32 %y, i32 %or
927 ; CHECK-LABEL: @select_icmp_eq_and_4096_0_or_4096(
928 ; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i32 %x, 4096
929 ; CHECK-NEXT: [[OR:%[a-z0-9]+]] = or i32 [[AND]], %y
930 ; CHECK-NEXT: ret i32 [[OR]]
931 define i32 @select_icmp_eq_and_4096_0_or_4096(i32 %x, i32 %y) {
932 %and = and i32 %x, 4096
933 %cmp = icmp eq i32 %and, 0
934 %or = or i32 %y, 4096
935 %select = select i1 %cmp, i32 %y, i32 %or
939 ; CHECK-LABEL: @select_icmp_eq_0_and_1_or_1(
940 ; CHECK-NEXT: [[TRUNC:%.+]] = trunc i64 %x to i32
941 ; CHECK-NEXT: [[AND:%.+]] = and i32 [[TRUNC]], 1
942 ; CHECK-NEXT: [[OR:%.+]] = or i32 [[XOR]], %y
943 ; CHECK-NEXT: ret i32 [[OR]]
944 define i32 @select_icmp_eq_0_and_1_or_1(i64 %x, i32 %y) {
946 %cmp = icmp eq i64 %and, 0
948 %select = select i1 %cmp, i32 %y, i32 %or
952 ; CHECK-LABEL: @select_icmp_ne_0_and_4096_or_32(
953 ; CHECK-NEXT: [[LSHR:%[a-z0-9]+]] = lshr i32 %x, 7
954 ; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i32 [[LSHR]], 32
955 ; CHECK-NEXT: [[XOR:%[a-z0-9]+]] = xor i32 [[AND]], 32
956 ; CHECK-NEXT: [[OR:%[a-z0-9]+]] = or i32 [[XOR]], %y
957 ; CHECK-NEXT: ret i32 [[OR]]
958 define i32 @select_icmp_ne_0_and_4096_or_32(i32 %x, i32 %y) {
959 %and = and i32 %x, 4096
960 %cmp = icmp ne i32 0, %and
962 %select = select i1 %cmp, i32 %y, i32 %or
966 ; CHECK-LABEL: @select_icmp_ne_0_and_32_or_4096(
967 ; CHECK-NEXT: [[SHL:%[a-z0-9]+]] = shl i32 %x, 7
968 ; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i32 [[SHL]], 4096
969 ; CHECK-NEXT: [[XOR:%[a-z0-9]+]] = xor i32 [[AND]], 4096
970 ; CHECK-NEXT: [[OR:%[a-z0-9]+]] = or i32 [[XOR]], %y
971 ; CHECK-NEXT: ret i32 [[OR]]
972 define i32 @select_icmp_ne_0_and_32_or_4096(i32 %x, i32 %y) {
973 %and = and i32 %x, 32
974 %cmp = icmp ne i32 0, %and
975 %or = or i32 %y, 4096
976 %select = select i1 %cmp, i32 %y, i32 %or
980 ; CHECK-LABEL: @select_icmp_ne_0_and_1073741824_or_8(
981 ; CHECK-NEXT: [[LSHR:%.+]] = lshr i32 %x, 27
982 ; CHECK-NEXT: [[TRUNC:%.+]] = trunc i32 [[LSHR]] to i8
983 ; CHECK-NEXT: [[AND:%.+]] = and i8 [[TRUNC]], 8
984 ; CHECK-NEXT: [[XOR:%.+]] = xor i8 [[AND]], 8
985 ; CHECK-NEXT: [[OR:%.+]] = or i8 [[XOR]], %y
986 ; CHECK-NEXT: ret i8 [[OR]]
987 define i8 @select_icmp_ne_0_and_1073741824_or_8(i32 %x, i8 %y) {
988 %and = and i32 %x, 1073741824
989 %cmp = icmp ne i32 0, %and
991 %select = select i1 %cmp, i8 %y, i8 %or
995 ; CHECK-LABEL: @select_icmp_ne_0_and_8_or_1073741824(
996 ; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i8 %x, 8
997 ; CHECK-NEXT: [[ZEXT:%[a-z0-9]+]] = zext i8 [[AND]] to i32
998 ; CHECK-NEXT: [[SHL:%[a-z0-9]+]] = shl nuw nsw i32 [[ZEXT]], 27
999 ; CHECK-NEXT: [[XOR:%[a-z0-9]+]] = xor i32 [[SHL]], 1073741824
1000 ; CHECK-NEXT: [[OR:%[a-z0-9]+]] = or i32 [[XOR]], %y
1001 ; CHECK-NEXT: ret i32 [[OR]]
1002 define i32 @select_icmp_ne_0_and_8_or_1073741824(i8 %x, i32 %y) {
1004 %cmp = icmp ne i8 0, %and
1005 %or = or i32 %y, 1073741824
1006 %select = select i1 %cmp, i32 %y, i32 %or
1010 ; We can't combine here, because the cmp is scalar and the or vector.
1011 ; Just make sure we don't assert.
1012 define <2 x i32> @select_icmp_eq_and_1_0_or_vector_of_2s(i32 %x, <2 x i32> %y) {
1013 %and = and i32 %x, 1
1014 %cmp = icmp eq i32 %and, 0
1015 %or = or <2 x i32> %y, <i32 2, i32 2>
1016 %select = select i1 %cmp, <2 x i32> %y, <2 x i32> %or
1017 ret <2 x i32> %select
1020 ; CHECK-LABEL: @select_icmp_and_8_ne_0_xor_8(
1021 ; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i32 %x, -9
1022 ; CHECK-NEXT: ret i32 [[AND]]
1023 define i32 @select_icmp_and_8_ne_0_xor_8(i32 %x) {
1024 %and = and i32 %x, 8
1025 %cmp = icmp eq i32 %and, 0
1026 %xor = xor i32 %x, 8
1027 %x.xor = select i1 %cmp, i32 %x, i32 %xor
1031 ; CHECK-LABEL: @select_icmp_and_8_eq_0_xor_8(
1032 ; CHECK-NEXT: [[OR:%[a-z0-9]+]] = or i32 %x, 8
1033 ; CHECK-NEXT: ret i32 [[OR]]
1034 define i32 @select_icmp_and_8_eq_0_xor_8(i32 %x) {
1035 %and = and i32 %x, 8
1036 %cmp = icmp eq i32 %and, 0
1037 %xor = xor i32 %x, 8
1038 %xor.x = select i1 %cmp, i32 %xor, i32 %x
1042 ; CHECK-LABEL: @select_icmp_x_and_8_eq_0_y_xor_8(
1043 ; CHECK: select i1 %cmp, i64 %y, i64 %xor
1044 define i64 @select_icmp_x_and_8_eq_0_y_xor_8(i32 %x, i64 %y) {
1045 %and = and i32 %x, 8
1046 %cmp = icmp eq i32 %and, 0
1047 %xor = xor i64 %y, 8
1048 %y.xor = select i1 %cmp, i64 %y, i64 %xor
1052 ; CHECK-LABEL: @select_icmp_x_and_8_ne_0_y_xor_8(
1053 ; CHECK: select i1 %cmp, i64 %xor, i64 %y
1054 define i64 @select_icmp_x_and_8_ne_0_y_xor_8(i32 %x, i64 %y) {
1055 %and = and i32 %x, 8
1056 %cmp = icmp eq i32 %and, 0
1057 %xor = xor i64 %y, 8
1058 %xor.y = select i1 %cmp, i64 %xor, i64 %y
1062 ; CHECK-LABEL: @select_icmp_x_and_8_ne_0_y_or_8(
1063 ; CHECK: xor i64 %1, 8
1064 ; CHECK: or i64 %2, %y
1065 define i64 @select_icmp_x_and_8_ne_0_y_or_8(i32 %x, i64 %y) {
1066 %and = and i32 %x, 8
1067 %cmp = icmp eq i32 %and, 0
1069 %or.y = select i1 %cmp, i64 %or, i64 %y
1073 ; CHECK-LABEL: @select_icmp_and_2147483648_ne_0_xor_2147483648(
1074 ; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i32 %x, 2147483647
1075 ; CHECK-NEXT: ret i32 [[AND]]
1076 define i32 @select_icmp_and_2147483648_ne_0_xor_2147483648(i32 %x) {
1077 %and = and i32 %x, 2147483648
1078 %cmp = icmp eq i32 %and, 0
1079 %xor = xor i32 %x, 2147483648
1080 %x.xor = select i1 %cmp, i32 %x, i32 %xor
1084 ; CHECK-LABEL: @select_icmp_and_2147483648_eq_0_xor_2147483648(
1085 ; CHECK-NEXT: [[OR:%[a-z0-9]+]] = or i32 %x, -2147483648
1086 ; CHECK-NEXT: ret i32 [[OR]]
1087 define i32 @select_icmp_and_2147483648_eq_0_xor_2147483648(i32 %x) {
1088 %and = and i32 %x, 2147483648
1089 %cmp = icmp eq i32 %and, 0
1090 %xor = xor i32 %x, 2147483648
1091 %xor.x = select i1 %cmp, i32 %xor, i32 %x
1095 ; CHECK-LABEL: @select_icmp_x_and_2147483648_ne_0_or_2147483648(
1096 ; CHECK-NEXT: [[OR:%[a-z0-9]+]] = or i32 %x, -2147483648
1097 ; CHECK-NEXT: ret i32 [[OR]]
1098 define i32 @select_icmp_x_and_2147483648_ne_0_or_2147483648(i32 %x) {
1099 %and = and i32 %x, 2147483648
1100 %cmp = icmp eq i32 %and, 0
1101 %or = or i32 %x, 2147483648
1102 %or.x = select i1 %cmp, i32 %or, i32 %x
1106 define i32 @test65(i64 %x) {
1108 %2 = icmp ne i64 %1, 0
1109 %3 = select i1 %2, i32 40, i32 42
1112 ; CHECK-LABEL: @test65(
1113 ; CHECK: %[[TRUNC:.*]] = trunc i64 %x to i32
1114 ; CHECK: %[[LSHR:.*]] = lshr i32 %[[TRUNC]], 3
1115 ; CHECK: %[[AND:.*]] = and i32 %[[LSHR]], 2
1116 ; CHECK: %[[XOR:.*]] = xor i32 %[[AND]], 42
1117 ; CHECK: ret i32 %[[XOR]]
1120 define i32 @test66(i64 %x) {
1121 %1 = and i64 %x, 4294967296
1122 %2 = icmp ne i64 %1, 0
1123 %3 = select i1 %2, i32 40, i32 42
1126 ; CHECK-LABEL: @test66(
1130 define i32 @test67(i16 %x) {
1132 %2 = icmp ne i16 %1, 0
1133 %3 = select i1 %2, i32 40, i32 42
1136 ; CHECK-LABEL: @test67(
1137 ; CHECK: and i16 %x, 4
1138 ; CHECK: zext i16 %1 to i32
1139 ; CHECK: lshr exact i32 %2, 1
1140 ; CHECK: xor i32 %3, 42
1143 ; SMIN(SMIN(X, 11), 92) -> SMIN(X, 11)
1144 define i32 @test68(i32 %x) {
1146 %cmp = icmp slt i32 11, %x
1147 %cond = select i1 %cmp, i32 11, i32 %x
1148 %cmp3 = icmp slt i32 92, %cond
1149 %retval = select i1 %cmp3, i32 92, i32 %cond
1151 ; CHECK-LABEL: @test68(
1152 ; CHECK: ret i32 %cond
1155 ; MIN(MIN(X, 24), 83) -> MIN(X, 24)
1156 define i32 @test69(i32 %x) {
1158 %cmp = icmp ult i32 24, %x
1159 %cond = select i1 %cmp, i32 24, i32 %x
1160 %cmp3 = icmp ult i32 83, %cond
1161 %retval = select i1 %cmp3, i32 83, i32 %cond
1163 ; CHECK-LABEL: @test69(
1164 ; CHECK: ret i32 %cond
1167 ; SMAX(SMAX(X, 75), 36) -> SMAX(X, 75)
1168 define i32 @test70(i32 %x) {
1170 %cmp = icmp slt i32 %x, 75
1171 %cond = select i1 %cmp, i32 75, i32 %x
1172 %cmp3 = icmp slt i32 %cond, 36
1173 %retval = select i1 %cmp3, i32 36, i32 %cond
1175 ; CHECK-LABEL: @test70(
1176 ; CHECK: ret i32 %cond
1179 ; MAX(MAX(X, 68), 47) -> MAX(X, 68)
1180 define i32 @test71(i32 %x) {
1182 %cmp = icmp ult i32 %x, 68
1183 %cond = select i1 %cmp, i32 68, i32 %x
1184 %cmp3 = icmp ult i32 %cond, 47
1185 %retval = select i1 %cmp3, i32 47, i32 %cond
1187 ; CHECK-LABEL: @test71(
1188 ; CHECK: ret i32 %cond
1191 ; SMIN(SMIN(X, 92), 11) -> SMIN(X, 11)
1192 define i32 @test72(i32 %x) {
1193 %cmp = icmp sgt i32 %x, 92
1194 %cond = select i1 %cmp, i32 92, i32 %x
1195 %cmp3 = icmp sgt i32 %cond, 11
1196 %retval = select i1 %cmp3, i32 11, i32 %cond
1198 ; CHECK-LABEL: @test72(
1199 ; CHECK-NEXT: [[CMP:%[a-z0-9]+]] = icmp sgt i32 %x, 11
1200 ; CHECK-NEXT: [[SEL:%[a-z0-9]+]] = select i1 [[CMP]], i32 11, i32 %x
1201 ; CHECK-NEXT: ret i32 [[SEL]]
1204 ; MIN(MIN(X, 83), 24) -> MIN(X, 24)
1205 define i32 @test73(i32 %x) {
1206 %cmp = icmp ugt i32 %x, 83
1207 %cond = select i1 %cmp, i32 83, i32 %x
1208 %cmp3 = icmp ugt i32 %cond, 24
1209 %retval = select i1 %cmp3, i32 24, i32 %cond
1211 ; CHECK-LABEL: @test73(
1212 ; CHECK-NEXT: [[CMP:%[a-z0-9]+]] = icmp ugt i32 %x, 24
1213 ; CHECK-NEXT: [[SEL:%[a-z0-9]+]] = select i1 [[CMP]], i32 24, i32 %x
1214 ; CHECK-NEXT: ret i32 [[SEL]]
1217 ; SMAX(SMAX(X, 36), 75) -> SMAX(X, 75)
1218 define i32 @test74(i32 %x) {
1219 %cmp = icmp slt i32 %x, 36
1220 %cond = select i1 %cmp, i32 36, i32 %x
1221 %cmp3 = icmp slt i32 %cond, 75
1222 %retval = select i1 %cmp3, i32 75, i32 %cond
1224 ; CHECK-LABEL: @test74(
1225 ; CHECK-NEXT: [[CMP:%[a-z0-9]+]] = icmp slt i32 %x, 75
1226 ; CHECK-NEXT: [[SEL:%[a-z0-9]+]] = select i1 [[CMP]], i32 75, i32 %x
1227 ; CHECK-NEXT: ret i32 [[SEL]]
1230 ; MAX(MAX(X, 47), 68) -> MAX(X, 68)
1231 define i32 @test75(i32 %x) {
1232 %cmp = icmp ult i32 %x, 47
1233 %cond = select i1 %cmp, i32 47, i32 %x
1234 %cmp3 = icmp ult i32 %cond, 68
1235 %retval = select i1 %cmp3, i32 68, i32 %cond
1237 ; CHECK-LABEL: @test75(
1238 ; CHECK-NEXT: [[CMP:%[a-z0-9]+]] = icmp ult i32 %x, 68
1239 ; CHECK-NEXT: [[SEL:%[a-z0-9]+]] = select i1 [[CMP]], i32 68, i32 %x
1240 ; CHECK-NEXT: ret i32 [[SEL]]
1243 @under_aligned = external global i32, align 1
1245 define i32 @test76(i1 %flag, i32* %x) {
1246 ; The load here must not be speculated around the select. One side of the
1247 ; select is trivially dereferencable but may have a lower alignment than the
1249 ; CHECK-LABEL: @test76(
1250 ; CHECK: store i32 0, i32* %x
1251 ; CHECK: %[[P:.*]] = select i1 %flag, i32* @under_aligned, i32* %x
1252 ; CHECK: load i32, i32* %[[P]]
1254 store i32 0, i32* %x
1255 %p = select i1 %flag, i32* @under_aligned, i32* %x
1256 %v = load i32, i32* %p
1260 declare void @scribble_on_i32(i32*)
1262 define i32 @test77(i1 %flag, i32* %x) {
1263 ; The load here must not be speculated around the select. One side of the
1264 ; select is trivially dereferencable but may have a lower alignment than the
1266 ; CHECK-LABEL: @test77(
1267 ; CHECK: %[[A:.*]] = alloca i32, align 1
1268 ; CHECK: call void @scribble_on_i32(i32* %[[A]])
1269 ; CHECK: store i32 0, i32* %x
1270 ; CHECK: %[[P:.*]] = select i1 %flag, i32* %[[A]], i32* %x
1271 ; CHECK: load i32, i32* %[[P]]
1273 %under_aligned = alloca i32, align 1
1274 call void @scribble_on_i32(i32* %under_aligned)
1275 store i32 0, i32* %x
1276 %p = select i1 %flag, i32* %under_aligned, i32* %x
1277 %v = load i32, i32* %p
1281 define i32 @test78(i1 %flag, i32* %x, i32* %y, i32* %z) {
1282 ; Test that we can speculate the loads around the select even when we can't
1283 ; fold the load completely away.
1284 ; CHECK-LABEL: @test78(
1285 ; CHECK: %[[V1:.*]] = load i32, i32* %x
1286 ; CHECK-NEXT: %[[V2:.*]] = load i32, i32* %y
1287 ; CHECK-NEXT: %[[S:.*]] = select i1 %flag, i32 %[[V1]], i32 %[[V2]]
1288 ; CHECK-NEXT: ret i32 %[[S]]
1290 store i32 0, i32* %x
1291 store i32 0, i32* %y
1292 ; Block forwarding by storing to %z which could alias either %x or %y.
1293 store i32 42, i32* %z
1294 %p = select i1 %flag, i32* %x, i32* %y
1295 %v = load i32, i32* %p
1299 define float @test79(i1 %flag, float* %x, i32* %y, i32* %z) {
1300 ; Test that we can speculate the loads around the select even when we can't
1301 ; fold the load completely away.
1302 ; CHECK-LABEL: @test79(
1303 ; CHECK: %[[V1:.*]] = load float, float* %x
1304 ; CHECK-NEXT: %[[V2:.*]] = load float, float* %y
1305 ; CHECK-NEXT: %[[S:.*]] = select i1 %flag, float %[[V1]], float %[[V2]]
1306 ; CHECK-NEXT: ret float %[[S]]
1308 %x1 = bitcast float* %x to i32*
1309 %y1 = bitcast i32* %y to float*
1310 store i32 0, i32* %x1
1311 store i32 0, i32* %y
1312 ; Block forwarding by storing to %z which could alias either %x or %y.
1313 store i32 42, i32* %z
1314 %p = select i1 %flag, float* %x, float* %y1
1315 %v = load float, float* %p
1319 define i32 @test80(i1 %flag) {
1320 ; Test that when we speculate the loads around the select they fold throug
1321 ; load->load folding and load->store folding.
1322 ; CHECK-LABEL: @test80(
1323 ; CHECK: %[[X:.*]] = alloca i32
1324 ; CHECK-NEXT: %[[Y:.*]] = alloca i32
1325 ; CHECK: %[[V:.*]] = load i32, i32* %[[X]]
1326 ; CHECK-NEXT: store i32 %[[V]], i32* %[[Y]]
1327 ; CHECK-NEXT: ret i32 %[[V]]
1331 call void @scribble_on_i32(i32* %x)
1332 call void @scribble_on_i32(i32* %y)
1333 %tmp = load i32, i32* %x
1334 store i32 %tmp, i32* %y
1335 %p = select i1 %flag, i32* %x, i32* %y
1336 %v = load i32, i32* %p
1340 define float @test81(i1 %flag) {
1341 ; Test that we can speculate the load around the select even though they use
1342 ; differently typed pointers.
1343 ; CHECK-LABEL: @test81(
1344 ; CHECK: %[[X:.*]] = alloca i32
1345 ; CHECK-NEXT: %[[Y:.*]] = alloca i32
1346 ; CHECK: %[[V:.*]] = load i32, i32* %[[X]]
1347 ; CHECK-NEXT: store i32 %[[V]], i32* %[[Y]]
1348 ; CHECK-NEXT: %[[C:.*]] = bitcast i32 %[[V]] to float
1349 ; CHECK-NEXT: ret float %[[C]]
1353 %x1 = bitcast float* %x to i32*
1354 %y1 = bitcast i32* %y to float*
1355 call void @scribble_on_i32(i32* %x1)
1356 call void @scribble_on_i32(i32* %y)
1357 %tmp = load i32, i32* %x1
1358 store i32 %tmp, i32* %y
1359 %p = select i1 %flag, float* %x, float* %y1
1360 %v = load float, float* %p
1364 define i32 @test82(i1 %flag) {
1365 ; Test that we can speculate the load around the select even though they use
1366 ; differently typed pointers.
1367 ; CHECK-LABEL: @test82(
1368 ; CHECK: %[[X:.*]] = alloca float
1369 ; CHECK-NEXT: %[[Y:.*]] = alloca i32
1370 ; CHECK-NEXT: %[[X1:.*]] = bitcast float* %[[X]] to i32*
1371 ; CHECK-NEXT: %[[Y1:.*]] = bitcast i32* %[[Y]] to float*
1372 ; CHECK: %[[V:.*]] = load float, float* %[[X]]
1373 ; CHECK-NEXT: store float %[[V]], float* %[[Y1]]
1374 ; CHECK-NEXT: %[[C:.*]] = bitcast float %[[V]] to i32
1375 ; CHECK-NEXT: ret i32 %[[C]]
1379 %x1 = bitcast float* %x to i32*
1380 %y1 = bitcast i32* %y to float*
1381 call void @scribble_on_i32(i32* %x1)
1382 call void @scribble_on_i32(i32* %y)
1383 %tmp = load float, float* %x
1384 store float %tmp, float* %y1
1385 %p = select i1 %flag, i32* %x1, i32* %y
1386 %v = load i32, i32* %p
1390 declare void @scribble_on_i64(i64*)
1391 declare void @scribble_on_i128(i128*)
1393 define i8* @test83(i1 %flag) {
1394 ; Test that we can speculate the load around the select even though they use
1395 ; differently typed pointers and requires inttoptr casts.
1396 ; CHECK-LABEL: @test83(
1397 ; CHECK: %[[X:.*]] = alloca i8*
1398 ; CHECK-NEXT: %[[Y:.*]] = alloca i8*
1399 ; CHECK-DAG: %[[X2:.*]] = bitcast i8** %[[X]] to i64*
1400 ; CHECK-DAG: %[[Y2:.*]] = bitcast i8** %[[Y]] to i64*
1401 ; CHECK: %[[V:.*]] = load i64, i64* %[[X2]]
1402 ; CHECK-NEXT: store i64 %[[V]], i64* %[[Y2]]
1403 ; CHECK-NEXT: %[[C:.*]] = inttoptr i64 %[[V]] to i8*
1404 ; CHECK-NEXT: ret i8* %[[S]]
1408 %x1 = bitcast i8** %x to i64*
1409 %y1 = bitcast i64* %y to i8**
1410 call void @scribble_on_i64(i64* %x1)
1411 call void @scribble_on_i64(i64* %y)
1412 %tmp = load i64, i64* %x1
1413 store i64 %tmp, i64* %y
1414 %p = select i1 %flag, i8** %x, i8** %y1
1415 %v = load i8*, i8** %p
1419 define i64 @test84(i1 %flag) {
1420 ; Test that we can speculate the load around the select even though they use
1421 ; differently typed pointers and requires a ptrtoint cast.
1422 ; CHECK-LABEL: @test84(
1423 ; CHECK: %[[X:.*]] = alloca i8*
1424 ; CHECK-NEXT: %[[Y:.*]] = alloca i8*
1425 ; CHECK: %[[V:.*]] = load i8*, i8** %[[X]]
1426 ; CHECK-NEXT: store i8* %[[V]], i8** %[[Y]]
1427 ; CHECK-NEXT: %[[C:.*]] = ptrtoint i8* %[[V]] to i64
1428 ; CHECK-NEXT: ret i64 %[[C]]
1432 %x1 = bitcast i8** %x to i64*
1433 %y1 = bitcast i64* %y to i8**
1434 call void @scribble_on_i64(i64* %x1)
1435 call void @scribble_on_i64(i64* %y)
1436 %tmp = load i8*, i8** %x
1437 store i8* %tmp, i8** %y1
1438 %p = select i1 %flag, i64* %x1, i64* %y
1439 %v = load i64, i64* %p
1443 define i8* @test85(i1 %flag) {
1444 ; Test that we can't speculate the load around the select. The load of the
1445 ; pointer doesn't load all of the stored integer bits. We could fix this, but it
1446 ; would require endianness checks and other nastiness.
1447 ; CHECK-LABEL: @test85(
1448 ; CHECK: %[[T:.*]] = load i128, i128*
1449 ; CHECK-NEXT: store i128 %[[T]], i128*
1450 ; CHECK-NEXT: %[[X:.*]] = load i8*, i8**
1451 ; CHECK-NEXT: %[[Y:.*]] = load i8*, i8**
1452 ; CHECK-NEXT: %[[V:.*]] = select i1 %flag, i8* %[[X]], i8* %[[Y]]
1453 ; CHECK-NEXT: ret i8* %[[V]]
1455 %x = alloca [2 x i8*]
1457 %x1 = bitcast [2 x i8*]* %x to i8**
1458 %x2 = bitcast i8** %x1 to i128*
1459 %y1 = bitcast i128* %y to i8**
1460 call void @scribble_on_i128(i128* %x2)
1461 call void @scribble_on_i128(i128* %y)
1462 %tmp = load i128, i128* %x2
1463 store i128 %tmp, i128* %y
1464 %p = select i1 %flag, i8** %x1, i8** %y1
1465 %v = load i8*, i8** %p
1469 define i128 @test86(i1 %flag) {
1470 ; Test that we can't speculate the load around the select when the integer size
1471 ; is larger than the pointer size. The store of the pointer doesn't store to all
1472 ; the bits of the integer.
1474 ; CHECK-LABEL: @test86(
1475 ; CHECK: %[[T:.*]] = load i8*, i8**
1476 ; CHECK-NEXT: store i8* %[[T]], i8**
1477 ; CHECK-NEXT: %[[X:.*]] = load i128, i128*
1478 ; CHECK-NEXT: %[[Y:.*]] = load i128, i128*
1479 ; CHECK-NEXT: %[[V:.*]] = select i1 %flag, i128 %[[X]], i128 %[[Y]]
1480 ; CHECK-NEXT: ret i128 %[[V]]
1482 %x = alloca [2 x i8*]
1484 %x1 = bitcast [2 x i8*]* %x to i8**
1485 %x2 = bitcast i8** %x1 to i128*
1486 %y1 = bitcast i128* %y to i8**
1487 call void @scribble_on_i128(i128* %x2)
1488 call void @scribble_on_i128(i128* %y)
1489 %tmp = load i8*, i8** %x1
1490 store i8* %tmp, i8** %y1
1491 %p = select i1 %flag, i128* %x2, i128* %y
1492 %v = load i128, i128* %p
1496 define i32 @test_select_select0(i32 %a, i32 %r0, i32 %r1, i32 %v1, i32 %v2) {
1497 ; CHECK-LABEL: @test_select_select0(
1498 ; CHECK: %[[C0:.*]] = icmp sge i32 %a, %v1
1499 ; CHECK-NEXT: %[[C1:.*]] = icmp slt i32 %a, %v2
1500 ; CHECK-NEXT: %[[C:.*]] = and i1 %[[C1]], %[[C0]]
1501 ; CHECK-NEXT: %[[SEL:.*]] = select i1 %[[C]], i32 %r0, i32 %r1
1502 ; CHECK-NEXT: ret i32 %[[SEL]]
1503 %c0 = icmp sge i32 %a, %v1
1504 %s0 = select i1 %c0, i32 %r0, i32 %r1
1505 %c1 = icmp slt i32 %a, %v2
1506 %s1 = select i1 %c1, i32 %s0, i32 %r1
1510 define i32 @test_select_select1(i32 %a, i32 %r0, i32 %r1, i32 %v1, i32 %v2) {
1511 ; CHECK-LABEL: @test_select_select1(
1512 ; CHECK: %[[C0:.*]] = icmp sge i32 %a, %v1
1513 ; CHECK-NEXT: %[[C1:.*]] = icmp slt i32 %a, %v2
1514 ; CHECK-NEXT: %[[C:.*]] = or i1 %[[C1]], %[[C0]]
1515 ; CHECK-NEXT: %[[SEL:.*]] = select i1 %[[C]], i32 %r0, i32 %r1
1516 ; CHECK-NEXT: ret i32 %[[SEL]]
1517 %c0 = icmp sge i32 %a, %v1
1518 %s0 = select i1 %c0, i32 %r0, i32 %r1
1519 %c1 = icmp slt i32 %a, %v2
1520 %s1 = select i1 %c1, i32 %r0, i32 %s0
1524 define i32 @test_max_of_min(i32 %a) {
1525 ; MAX(MIN(%a, -1), -1) == -1
1526 ; CHECK-LABEL: @test_max_of_min(
1528 %not_a = xor i32 %a, -1
1529 %c0 = icmp sgt i32 %a, 0
1530 %s0 = select i1 %c0, i32 %not_a, i32 -1
1531 %c1 = icmp sgt i32 %s0, -1
1532 %s1 = select i1 %c1, i32 %s0, i32 -1
1537 define i32 @PR23757(i32 %x) {
1538 ; CHECK-LABEL: @PR23757
1539 ; CHECK: %[[cmp:.*]] = icmp eq i32 %x, 2147483647
1540 ; CHECK-NEXT: %[[add:.*]] = add nsw i32 %x, 1
1541 ; CHECK-NEXT: %[[sel:.*]] = select i1 %[[cmp]], i32 -2147483648, i32 %[[add]]
1542 ; CHECK-NEXT: ret i32 %[[sel]]
1543 %cmp = icmp eq i32 %x, 2147483647
1544 %add = add nsw i32 %x, 1
1545 %sel = select i1 %cmp, i32 -2147483648, i32 %add