1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=instsimplify -S | FileCheck %s
4 define i1 @bool_true_or_false(i1 %cond) {
5 ; CHECK-LABEL: @bool_true_or_false(
6 ; CHECK-NEXT: ret i1 [[COND:%.*]]
8 %s = select i1 %cond, i1 true, i1 false
12 define <2 x i1> @bool_true_or_false_vec(<2 x i1> %cond) {
13 ; CHECK-LABEL: @bool_true_or_false_vec(
14 ; CHECK-NEXT: ret <2 x i1> [[COND:%.*]]
16 %s = select <2 x i1> %cond, <2 x i1> <i1 true, i1 true>, <2 x i1> zeroinitializer
20 define <2 x i1> @bool_true_or_false_vec_undef(<2 x i1> %cond) {
21 ; CHECK-LABEL: @bool_true_or_false_vec_undef(
22 ; CHECK-NEXT: ret <2 x i1> [[COND:%.*]]
24 %s = select <2 x i1> %cond, <2 x i1> <i1 undef, i1 true>, <2 x i1> <i1 false, i1 undef>
28 define i32 @cond_is_false(i32 %A, i32 %B) {
29 ; CHECK-LABEL: @cond_is_false(
30 ; CHECK-NEXT: ret i32 [[B:%.*]]
32 %C = select i1 false, i32 %A, i32 %B
36 define i32 @cond_is_true(i32 %A, i32 %B) {
37 ; CHECK-LABEL: @cond_is_true(
38 ; CHECK-NEXT: ret i32 [[A:%.*]]
40 %C = select i1 true, i32 %A, i32 %B
44 define i32 @equal_arms(i1 %cond, i32 %x) {
45 ; CHECK-LABEL: @equal_arms(
46 ; CHECK-NEXT: ret i32 [[X:%.*]]
48 %V = select i1 %cond, i32 %x, i32 %x
52 define <2 x i32> @equal_arms_vec(<2 x i1> %cond, <2 x i32> %x) {
53 ; CHECK-LABEL: @equal_arms_vec(
54 ; CHECK-NEXT: ret <2 x i32> [[X:%.*]]
56 %V = select <2 x i1> %cond, <2 x i32> %x, <2 x i32> %x
60 define <2 x i32> @equal_arms_vec_undef(<2 x i1> %cond) {
61 ; CHECK-LABEL: @equal_arms_vec_undef(
62 ; CHECK-NEXT: ret <2 x i32> <i32 42, i32 42>
64 %V = select <2 x i1> %cond, <2 x i32> <i32 42, i32 undef>, <2 x i32> <i32 undef, i32 42>
68 define <3 x float> @equal_arms_vec_less_undef(<3 x i1> %cond) {
69 ; CHECK-LABEL: @equal_arms_vec_less_undef(
70 ; CHECK-NEXT: ret <3 x float> <float 4.200000e+01, float 4.200000e+01, float 4.300000e+01>
72 %V = select <3 x i1> %cond, <3 x float> <float 42.0, float undef, float 43.0>, <3 x float> <float 42.0, float 42.0, float 43.0>
76 define <3 x float> @equal_arms_vec_more_undef(<3 x i1> %cond) {
77 ; CHECK-LABEL: @equal_arms_vec_more_undef(
78 ; CHECK-NEXT: ret <3 x float> <float 4.200000e+01, float undef, float 4.300000e+01>
80 %V = select <3 x i1> %cond, <3 x float> <float 42.0, float undef, float undef>, <3 x float> <float undef, float undef, float 43.0>
84 define <2 x i8> @vsel_tvec(<2 x i8> %x, <2 x i8> %y) {
85 ; CHECK-LABEL: @vsel_tvec(
86 ; CHECK-NEXT: ret <2 x i8> [[X:%.*]]
88 %s = select <2 x i1><i1 true, i1 true>, <2 x i8> %x, <2 x i8> %y
92 define <2 x i8> @vsel_fvec(<2 x i8> %x, <2 x i8> %y) {
93 ; CHECK-LABEL: @vsel_fvec(
94 ; CHECK-NEXT: ret <2 x i8> [[Y:%.*]]
96 %s = select <2 x i1><i1 false, i1 false>, <2 x i8> %x, <2 x i8> %y
100 define <2 x i8> @vsel_mixedvec() {
101 ; CHECK-LABEL: @vsel_mixedvec(
102 ; CHECK-NEXT: ret <2 x i8> <i8 0, i8 3>
104 %s = select <2 x i1><i1 true, i1 false>, <2 x i8> <i8 0, i8 1>, <2 x i8> <i8 2, i8 3>
108 define <3 x i8> @vsel_undef_true_op(<3 x i8> %x, <3 x i8> %y) {
109 ; CHECK-LABEL: @vsel_undef_true_op(
110 ; CHECK-NEXT: ret <3 x i8> [[X:%.*]]
112 %s = select <3 x i1><i1 1, i1 undef, i1 1>, <3 x i8> %x, <3 x i8> %y
116 define <3 x i4> @vsel_undef_false_op(<3 x i4> %x, <3 x i4> %y) {
117 ; CHECK-LABEL: @vsel_undef_false_op(
118 ; CHECK-NEXT: ret <3 x i4> [[Y:%.*]]
120 %s = select <3 x i1><i1 0, i1 undef, i1 undef>, <3 x i4> %x, <3 x i4> %y
124 define i32 @test1(i32 %x) {
125 ; CHECK-LABEL: @test1(
126 ; CHECK-NEXT: ret i32 [[X:%.*]]
129 %cmp = icmp eq i32 %and, 0
130 %and1 = and i32 %x, -2
131 %and1.x = select i1 %cmp, i32 %and1, i32 %x
135 define i32 @test2(i32 %x) {
136 ; CHECK-LABEL: @test2(
137 ; CHECK-NEXT: ret i32 [[X:%.*]]
140 %cmp = icmp ne i32 %and, 0
141 %and1 = and i32 %x, -2
142 %and1.x = select i1 %cmp, i32 %x, i32 %and1
146 define i32 @test3(i32 %x) {
147 ; CHECK-LABEL: @test3(
148 ; CHECK-NEXT: [[AND1:%.*]] = and i32 [[X:%.*]], -2
149 ; CHECK-NEXT: ret i32 [[AND1]]
152 %cmp = icmp ne i32 %and, 0
153 %and1 = and i32 %x, -2
154 %and1.x = select i1 %cmp, i32 %and1, i32 %x
158 define i32 @test4(i32 %X) {
159 ; CHECK-LABEL: @test4(
160 ; CHECK-NEXT: [[OR:%.*]] = or i32 [[X:%.*]], -2147483648
161 ; CHECK-NEXT: ret i32 [[OR]]
163 %cmp = icmp slt i32 %X, 0
164 %or = or i32 %X, -2147483648
165 %cond = select i1 %cmp, i32 %X, i32 %or
169 ; Same as above, but the compare isn't canonical
170 define i32 @test4noncanon(i32 %X) {
171 ; CHECK-LABEL: @test4noncanon(
172 ; CHECK-NEXT: [[OR:%.*]] = or i32 [[X:%.*]], -2147483648
173 ; CHECK-NEXT: ret i32 [[OR]]
175 %cmp = icmp sle i32 %X, -1
176 %or = or i32 %X, -2147483648
177 %cond = select i1 %cmp, i32 %X, i32 %or
181 define i32 @test5(i32 %X) {
182 ; CHECK-LABEL: @test5(
183 ; CHECK-NEXT: ret i32 [[X:%.*]]
185 %cmp = icmp slt i32 %X, 0
186 %or = or i32 %X, -2147483648
187 %cond = select i1 %cmp, i32 %or, i32 %X
191 define i32 @test6(i32 %X) {
192 ; CHECK-LABEL: @test6(
193 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 2147483647
194 ; CHECK-NEXT: ret i32 [[AND]]
196 %cmp = icmp slt i32 %X, 0
197 %and = and i32 %X, 2147483647
198 %cond = select i1 %cmp, i32 %and, i32 %X
202 define i32 @test7(i32 %X) {
203 ; CHECK-LABEL: @test7(
204 ; CHECK-NEXT: ret i32 [[X:%.*]]
206 %cmp = icmp slt i32 %X, 0
207 %and = and i32 %X, 2147483647
208 %cond = select i1 %cmp, i32 %X, i32 %and
212 define i32 @test8(i32 %X) {
213 ; CHECK-LABEL: @test8(
214 ; CHECK-NEXT: ret i32 [[X:%.*]]
216 %cmp = icmp sgt i32 %X, -1
217 %or = or i32 %X, -2147483648
218 %cond = select i1 %cmp, i32 %X, i32 %or
222 define i32 @test9(i32 %X) {
223 ; CHECK-LABEL: @test9(
224 ; CHECK-NEXT: [[OR:%.*]] = or i32 [[X:%.*]], -2147483648
225 ; CHECK-NEXT: ret i32 [[OR]]
227 %cmp = icmp sgt i32 %X, -1
228 %or = or i32 %X, -2147483648
229 %cond = select i1 %cmp, i32 %or, i32 %X
233 ; Same as above, but the compare isn't canonical
234 define i32 @test9noncanon(i32 %X) {
235 ; CHECK-LABEL: @test9noncanon(
236 ; CHECK-NEXT: [[OR:%.*]] = or i32 [[X:%.*]], -2147483648
237 ; CHECK-NEXT: ret i32 [[OR]]
239 %cmp = icmp sge i32 %X, 0
240 %or = or i32 %X, -2147483648
241 %cond = select i1 %cmp, i32 %or, i32 %X
245 define i32 @test10(i32 %X) {
246 ; CHECK-LABEL: @test10(
247 ; CHECK-NEXT: ret i32 [[X:%.*]]
249 %cmp = icmp sgt i32 %X, -1
250 %and = and i32 %X, 2147483647
251 %cond = select i1 %cmp, i32 %and, i32 %X
255 define i32 @test11(i32 %X) {
256 ; CHECK-LABEL: @test11(
257 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 2147483647
258 ; CHECK-NEXT: ret i32 [[AND]]
260 %cmp = icmp sgt i32 %X, -1
261 %and = and i32 %X, 2147483647
262 %cond = select i1 %cmp, i32 %X, i32 %and
266 define <2 x i8> @test11vec(<2 x i8> %X) {
267 ; CHECK-LABEL: @test11vec(
268 ; CHECK-NEXT: [[AND:%.*]] = and <2 x i8> [[X:%.*]], <i8 127, i8 127>
269 ; CHECK-NEXT: ret <2 x i8> [[AND]]
271 %cmp = icmp sgt <2 x i8> %X, <i8 -1, i8 -1>
272 %and = and <2 x i8> %X, <i8 127, i8 127>
273 %sel = select <2 x i1> %cmp, <2 x i8> %X, <2 x i8> %and
277 define i32 @test12(i32 %X) {
278 ; CHECK-LABEL: @test12(
279 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 3
280 ; CHECK-NEXT: ret i32 [[AND]]
282 %cmp = icmp ult i32 %X, 4
284 %cond = select i1 %cmp, i32 %X, i32 %and
288 ; Same as above, but the compare isn't canonical
289 define i32 @test12noncanon(i32 %X) {
290 ; CHECK-LABEL: @test12noncanon(
291 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 3
292 ; CHECK-NEXT: ret i32 [[AND]]
294 %cmp = icmp ule i32 %X, 3
296 %cond = select i1 %cmp, i32 %X, i32 %and
300 define i32 @test13(i32 %X) {
301 ; CHECK-LABEL: @test13(
302 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 3
303 ; CHECK-NEXT: ret i32 [[AND]]
305 %cmp = icmp ugt i32 %X, 3
307 %cond = select i1 %cmp, i32 %and, i32 %X
311 ; Same as above, but the compare isn't canonical
312 define i32 @test13noncanon(i32 %X) {
313 ; CHECK-LABEL: @test13noncanon(
314 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 3
315 ; CHECK-NEXT: ret i32 [[AND]]
317 %cmp = icmp uge i32 %X, 4
319 %cond = select i1 %cmp, i32 %and, i32 %X
323 define i32 @select_icmp_and_8_eq_0_or_8(i32 %x) {
324 ; CHECK-LABEL: @select_icmp_and_8_eq_0_or_8(
325 ; CHECK-NEXT: [[OR:%.*]] = or i32 [[X:%.*]], 8
326 ; CHECK-NEXT: ret i32 [[OR]]
329 %cmp = icmp eq i32 %and, 0
331 %sel = select i1 %cmp, i32 %or, i32 %x
335 define i32 @select_icmp_and_8_eq_0_or_8_alt(i32 %x) {
336 ; CHECK-LABEL: @select_icmp_and_8_eq_0_or_8_alt(
337 ; CHECK-NEXT: [[OR:%.*]] = or i32 [[X:%.*]], 8
338 ; CHECK-NEXT: ret i32 [[OR]]
341 %cmp = icmp ne i32 %and, 0
343 %sel = select i1 %cmp, i32 %x, i32 %or
347 define i32 @select_icmp_and_8_ne_0_or_8(i32 %x) {
348 ; CHECK-LABEL: @select_icmp_and_8_ne_0_or_8(
349 ; CHECK-NEXT: ret i32 [[X:%.*]]
352 %cmp = icmp ne i32 %and, 0
354 %sel = select i1 %cmp, i32 %or, i32 %x
358 define i32 @select_icmp_and_8_ne_0_or_8_alt(i32 %x) {
359 ; CHECK-LABEL: @select_icmp_and_8_ne_0_or_8_alt(
360 ; CHECK-NEXT: ret i32 [[X:%.*]]
363 %cmp = icmp eq i32 %and, 0
365 %sel = select i1 %cmp, i32 %x, i32 %or
369 define i32 @select_icmp_and_8_eq_0_and_not_8(i32 %x) {
370 ; CHECK-LABEL: @select_icmp_and_8_eq_0_and_not_8(
371 ; CHECK-NEXT: [[AND1:%.*]] = and i32 [[X:%.*]], -9
372 ; CHECK-NEXT: ret i32 [[AND1]]
375 %cmp = icmp eq i32 %and, 0
376 %and1 = and i32 %x, -9
377 %sel = select i1 %cmp, i32 %x, i32 %and1
381 define i32 @select_icmp_and_8_eq_0_and_not_8_alt(i32 %x) {
382 ; CHECK-LABEL: @select_icmp_and_8_eq_0_and_not_8_alt(
383 ; CHECK-NEXT: [[AND1:%.*]] = and i32 [[X:%.*]], -9
384 ; CHECK-NEXT: ret i32 [[AND1]]
387 %cmp = icmp ne i32 %and, 0
388 %and1 = and i32 %x, -9
389 %sel = select i1 %cmp, i32 %and1, i32 %x
393 define i32 @select_icmp_and_8_ne_0_and_not_8(i32 %x) {
394 ; CHECK-LABEL: @select_icmp_and_8_ne_0_and_not_8(
395 ; CHECK-NEXT: ret i32 [[X:%.*]]
398 %cmp = icmp ne i32 %and, 0
399 %and1 = and i32 %x, -9
400 %sel = select i1 %cmp, i32 %x, i32 %and1
404 define i32 @select_icmp_and_8_ne_0_and_not_8_alt(i32 %x) {
405 ; CHECK-LABEL: @select_icmp_and_8_ne_0_and_not_8_alt(
406 ; CHECK-NEXT: ret i32 [[X:%.*]]
409 %cmp = icmp eq i32 %and, 0
410 %and1 = and i32 %x, -9
411 %sel = select i1 %cmp, i32 %and1, i32 %x
415 ; PR28466: https://llvm.org/bugs/show_bug.cgi?id=28466
416 ; Each of the previous 8 patterns has a variant that replaces the
417 ; 'and' with a 'trunc' and the icmp eq/ne with icmp slt/sgt.
419 define i32 @select_icmp_trunc_8_ne_0_or_128(i32 %x) {
420 ; CHECK-LABEL: @select_icmp_trunc_8_ne_0_or_128(
421 ; CHECK-NEXT: [[OR:%.*]] = or i32 [[X:%.*]], 128
422 ; CHECK-NEXT: ret i32 [[OR]]
424 %trunc = trunc i32 %x to i8
425 %cmp = icmp sgt i8 %trunc, -1
427 %sel = select i1 %cmp, i32 %or, i32 %x
431 define i32 @select_icmp_trunc_8_ne_0_or_128_alt(i32 %x) {
432 ; CHECK-LABEL: @select_icmp_trunc_8_ne_0_or_128_alt(
433 ; CHECK-NEXT: [[OR:%.*]] = or i32 [[X:%.*]], 128
434 ; CHECK-NEXT: ret i32 [[OR]]
436 %trunc = trunc i32 %x to i8
437 %cmp = icmp slt i8 %trunc, 0
439 %sel = select i1 %cmp, i32 %x, i32 %or
443 define i32 @select_icmp_trunc_8_eq_0_or_128(i32 %x) {
444 ; CHECK-LABEL: @select_icmp_trunc_8_eq_0_or_128(
445 ; CHECK-NEXT: ret i32 [[X:%.*]]
447 %trunc = trunc i32 %x to i8
448 %cmp = icmp slt i8 %trunc, 0
450 %sel = select i1 %cmp, i32 %or, i32 %x
454 define i32 @select_icmp_trunc_8_eq_0_or_128_alt(i32 %x) {
455 ; CHECK-LABEL: @select_icmp_trunc_8_eq_0_or_128_alt(
456 ; CHECK-NEXT: ret i32 [[X:%.*]]
458 %trunc = trunc i32 %x to i8
459 %cmp = icmp sgt i8 %trunc, -1
461 %sel = select i1 %cmp, i32 %x, i32 %or
465 define i32 @select_icmp_trunc_8_eq_0_and_not_8(i32 %x) {
466 ; CHECK-LABEL: @select_icmp_trunc_8_eq_0_and_not_8(
467 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], -9
468 ; CHECK-NEXT: ret i32 [[AND]]
470 %trunc = trunc i32 %x to i4
471 %cmp = icmp sgt i4 %trunc, -1
472 %and = and i32 %x, -9
473 %sel = select i1 %cmp, i32 %x, i32 %and
477 define i32 @select_icmp_trunc_8_eq_0_and_not_8_alt(i32 %x) {
478 ; CHECK-LABEL: @select_icmp_trunc_8_eq_0_and_not_8_alt(
479 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], -9
480 ; CHECK-NEXT: ret i32 [[AND]]
482 %trunc = trunc i32 %x to i4
483 %cmp = icmp slt i4 %trunc, 0
484 %and = and i32 %x, -9
485 %sel = select i1 %cmp, i32 %and, i32 %x
489 define i32 @select_icmp_trunc_8_ne_0_and_not_8(i32 %x) {
490 ; CHECK-LABEL: @select_icmp_trunc_8_ne_0_and_not_8(
491 ; CHECK-NEXT: ret i32 [[X:%.*]]
493 %trunc = trunc i32 %x to i4
494 %cmp = icmp slt i4 %trunc, 0
495 %and = and i32 %x, -9
496 %sel = select i1 %cmp, i32 %x, i32 %and
500 define i32 @select_icmp_trunc_8_ne_0_and_not_8_alt(i32 %x) {
501 ; CHECK-LABEL: @select_icmp_trunc_8_ne_0_and_not_8_alt(
502 ; CHECK-NEXT: ret i32 [[X:%.*]]
504 %trunc = trunc i32 %x to i4
505 %cmp = icmp sgt i4 %trunc, -1
506 %and = and i32 %x, -9
507 %sel = select i1 %cmp, i32 %and, i32 %x
511 ; Make sure that at least a few of the same patterns are repeated with vector types.
513 define <2 x i32> @select_icmp_and_8_ne_0_and_not_8_vec(<2 x i32> %x) {
514 ; CHECK-LABEL: @select_icmp_and_8_ne_0_and_not_8_vec(
515 ; CHECK-NEXT: ret <2 x i32> [[X:%.*]]
517 %and = and <2 x i32> %x, <i32 8, i32 8>
518 %cmp = icmp ne <2 x i32> %and, zeroinitializer
519 %and1 = and <2 x i32> %x, <i32 -9, i32 -9>
520 %sel = select <2 x i1> %cmp, <2 x i32> %x, <2 x i32> %and1
524 define <2 x i32> @select_icmp_trunc_8_ne_0_and_not_8_alt_vec(<2 x i32> %x) {
525 ; CHECK-LABEL: @select_icmp_trunc_8_ne_0_and_not_8_alt_vec(
526 ; CHECK-NEXT: ret <2 x i32> [[X:%.*]]
528 %trunc = trunc <2 x i32> %x to <2 x i4>
529 %cmp = icmp sgt <2 x i4> %trunc, <i4 -1, i4 -1>
530 %and = and <2 x i32> %x, <i32 -9, i32 -9>
531 %sel = select <2 x i1> %cmp, <2 x i32> %and, <2 x i32> %x
535 ; Insert a bit from x into y? This should be possible in InstCombine, but not InstSimplify?
537 define i32 @select_icmp_x_and_8_eq_0_y_and_not_8(i32 %x, i32 %y) {
538 ; CHECK-LABEL: @select_icmp_x_and_8_eq_0_y_and_not_8(
539 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 8
540 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[AND]], 0
541 ; CHECK-NEXT: [[AND1:%.*]] = and i32 [[Y:%.*]], -9
542 ; CHECK-NEXT: [[Y_AND1:%.*]] = select i1 [[CMP]], i32 [[Y]], i32 [[AND1]]
543 ; CHECK-NEXT: ret i32 [[Y_AND1]]
546 %cmp = icmp eq i32 %and, 0
547 %and1 = and i32 %y, -9
548 %y.and1 = select i1 %cmp, i32 %y, i32 %and1
552 define i64 @select_icmp_x_and_8_eq_0_y64_and_not_8(i32 %x, i64 %y) {
553 ; CHECK-LABEL: @select_icmp_x_and_8_eq_0_y64_and_not_8(
554 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 8
555 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[AND]], 0
556 ; CHECK-NEXT: [[AND1:%.*]] = and i64 [[Y:%.*]], -9
557 ; CHECK-NEXT: [[Y_AND1:%.*]] = select i1 [[CMP]], i64 [[Y]], i64 [[AND1]]
558 ; CHECK-NEXT: ret i64 [[Y_AND1]]
561 %cmp = icmp eq i32 %and, 0
562 %and1 = and i64 %y, -9
563 %y.and1 = select i1 %cmp, i64 %y, i64 %and1
567 define i64 @select_icmp_x_and_8_ne_0_y64_and_not_8(i32 %x, i64 %y) {
568 ; CHECK-LABEL: @select_icmp_x_and_8_ne_0_y64_and_not_8(
569 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 8
570 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[AND]], 0
571 ; CHECK-NEXT: [[AND1:%.*]] = and i64 [[Y:%.*]], -9
572 ; CHECK-NEXT: [[AND1_Y:%.*]] = select i1 [[CMP]], i64 [[AND1]], i64 [[Y]]
573 ; CHECK-NEXT: ret i64 [[AND1_Y]]
576 %cmp = icmp eq i32 %and, 0
577 %and1 = and i64 %y, -9
578 %and1.y = select i1 %cmp, i64 %and1, i64 %y
582 ; Don't crash on a pointer or aggregate type.
584 define ptr @select_icmp_pointers(ptr %x, ptr %y) {
585 ; CHECK-LABEL: @select_icmp_pointers(
586 ; CHECK-NEXT: [[CMP:%.*]] = icmp slt ptr [[X:%.*]], null
587 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], ptr [[X]], ptr [[Y:%.*]]
588 ; CHECK-NEXT: ret ptr [[SEL]]
590 %cmp = icmp slt ptr %x, null
591 %sel = select i1 %cmp, ptr %x, ptr %y
595 ; If the condition is known, we don't need to select, but we're not
596 ; doing this fold here to avoid compile-time cost.
598 declare void @llvm.assume(i1)
600 define i8 @assume_sel_cond(i1 %cond, i8 %x, i8 %y) {
601 ; CHECK-LABEL: @assume_sel_cond(
602 ; CHECK-NEXT: call void @llvm.assume(i1 [[COND:%.*]])
603 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[COND]], i8 [[X:%.*]], i8 [[Y:%.*]]
604 ; CHECK-NEXT: ret i8 [[SEL]]
606 call void @llvm.assume(i1 %cond)
607 %sel = select i1 %cond, i8 %x, i8 %y
611 define i8 @do_not_assume_sel_cond(i1 %cond, i8 %x, i8 %y) {
612 ; CHECK-LABEL: @do_not_assume_sel_cond(
613 ; CHECK-NEXT: [[NOTCOND:%.*]] = icmp eq i1 [[COND:%.*]], false
614 ; CHECK-NEXT: call void @llvm.assume(i1 [[NOTCOND]])
615 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[COND]], i8 [[X:%.*]], i8 [[Y:%.*]]
616 ; CHECK-NEXT: ret i8 [[SEL]]
618 %notcond = icmp eq i1 %cond, false
619 call void @llvm.assume(i1 %notcond)
620 %sel = select i1 %cond, i8 %x, i8 %y
624 define ptr @select_icmp_eq_0_gep_operand(ptr %base, i64 %n) {
625 ; CHECK-LABEL: @select_icmp_eq_0_gep_operand(
626 ; CHECK-NEXT: [[GEP:%.*]] = getelementptr i32, ptr [[BASE:%.*]], i64 [[N:%.*]]
627 ; CHECK-NEXT: ret ptr [[GEP]]
629 %cond = icmp eq i64 %n, 0
630 %gep = getelementptr i32, ptr %base, i64 %n
631 %r = select i1 %cond, ptr %base, ptr %gep
635 define ptr @select_icmp_ne_0_gep_operand(ptr %base, i64 %n) {
636 ; CHECK-LABEL: @select_icmp_ne_0_gep_operand(
637 ; CHECK-NEXT: [[GEP:%.*]] = getelementptr i32, ptr [[BASE:%.*]], i64 [[N:%.*]]
638 ; CHECK-NEXT: ret ptr [[GEP]]
640 %cond = icmp ne i64 %n, 0
641 %gep = getelementptr i32, ptr %base, i64 %n
642 %r = select i1 %cond, ptr %gep, ptr %base
646 define i1 @and_cmps(i32 %x) {
647 ; CHECK-LABEL: @and_cmps(
648 ; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i32 [[X:%.*]], 92
649 ; CHECK-NEXT: [[CMP2:%.*]] = icmp slt i32 [[X]], 11
650 ; CHECK-NEXT: [[R:%.*]] = select i1 [[CMP1]], i1 [[CMP2]], i1 false
651 ; CHECK-NEXT: ret i1 [[R]]
653 %cmp1 = icmp slt i32 %x, 92
654 %cmp2 = icmp slt i32 %x, 11
655 %r = select i1 %cmp1, i1 %cmp2, i1 false
659 define <2 x i1> @and_cmps_vector(<2 x i32> %x) {
660 ; CHECK-LABEL: @and_cmps_vector(
661 ; CHECK-NEXT: [[CMP1:%.*]] = icmp slt <2 x i32> [[X:%.*]], <i32 92, i32 92>
662 ; CHECK-NEXT: [[CMP2:%.*]] = icmp slt <2 x i32> [[X]], <i32 11, i32 11>
663 ; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[CMP1]], <2 x i1> [[CMP2]], <2 x i1> zeroinitializer
664 ; CHECK-NEXT: ret <2 x i1> [[R]]
666 %cmp1 = icmp slt <2 x i32> %x, <i32 92, i32 92>
667 %cmp2 = icmp slt <2 x i32> %x, <i32 11, i32 11>
668 %r = select <2 x i1> %cmp1, <2 x i1> %cmp2, <2 x i1> <i1 false, i1 false>
672 define i1 @or_cmps(float %x) {
673 ; CHECK-LABEL: @or_cmps(
674 ; CHECK-NEXT: [[CMP1:%.*]] = fcmp uno float [[X:%.*]], 4.200000e+01
675 ; CHECK-NEXT: [[CMP2:%.*]] = fcmp uno float [[X]], 5.200000e+01
676 ; CHECK-NEXT: [[R:%.*]] = select i1 [[CMP1]], i1 true, i1 [[CMP2]]
677 ; CHECK-NEXT: ret i1 [[R]]
679 %cmp1 = fcmp uno float %x, 42.0
680 %cmp2 = fcmp uno float %x, 52.0
681 %r = select i1 %cmp1, i1 true, i1 %cmp2
685 define <2 x i1> @or_logic_vector(<2 x i1> %x, <2 x i1> %y) {
686 ; CHECK-LABEL: @or_logic_vector(
687 ; CHECK-NEXT: ret <2 x i1> [[X:%.*]]
689 %a = and <2 x i1> %x, %y
690 %r = select <2 x i1> %x, <2 x i1> <i1 true, i1 true>, <2 x i1> %a
694 define i1 @and_not_cmps(i32 %x) {
695 ; CHECK-LABEL: @and_not_cmps(
696 ; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i32 [[X:%.*]], 92
697 ; CHECK-NEXT: [[CMP2:%.*]] = icmp slt i32 [[X]], 11
698 ; CHECK-NEXT: [[R:%.*]] = select i1 [[CMP1]], i1 false, i1 [[CMP2]]
699 ; CHECK-NEXT: ret i1 [[R]]
701 %cmp1 = icmp slt i32 %x, 92
702 %cmp2 = icmp slt i32 %x, 11
703 %r = select i1 %cmp1, i1 false, i1 %cmp2
707 define i1 @or_not_cmps(i32 %x) {
708 ; CHECK-LABEL: @or_not_cmps(
709 ; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i32 [[X:%.*]], 92
710 ; CHECK-NEXT: [[CMP2:%.*]] = icmp slt i32 [[X]], 11
711 ; CHECK-NEXT: [[R:%.*]] = select i1 [[CMP1]], i1 [[CMP2]], i1 true
712 ; CHECK-NEXT: ret i1 [[R]]
714 %cmp1 = icmp slt i32 %x, 92
715 %cmp2 = icmp slt i32 %x, 11
716 %r = select i1 %cmp1, i1 %cmp2, i1 true
720 define i8 @and_cmps_wrong_type(i32 %x) {
721 ; CHECK-LABEL: @and_cmps_wrong_type(
722 ; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i32 [[X:%.*]], 92
723 ; CHECK-NEXT: [[CMP2:%.*]] = icmp slt i32 [[X]], 11
724 ; CHECK-NEXT: [[S:%.*]] = sext i1 [[CMP2]] to i8
725 ; CHECK-NEXT: [[R:%.*]] = select i1 [[CMP1]], i8 [[S]], i8 0
726 ; CHECK-NEXT: ret i8 [[R]]
728 %cmp1 = icmp slt i32 %x, 92
729 %cmp2 = icmp slt i32 %x, 11
730 %s = sext i1 %cmp2 to i8
731 %r = select i1 %cmp1, i8 %s, i8 0
735 define i1 @y_might_be_poison(float %x, float %y) {
736 ; CHECK-LABEL: @y_might_be_poison(
737 ; CHECK-NEXT: [[C1:%.*]] = fcmp ord float 0.000000e+00, [[X:%.*]]
738 ; CHECK-NEXT: [[C2:%.*]] = fcmp ord float [[X]], [[Y:%.*]]
739 ; CHECK-NEXT: [[C3:%.*]] = select i1 [[C1]], i1 [[C2]], i1 false
740 ; CHECK-NEXT: ret i1 [[C3]]
742 %c1 = fcmp ord float 0.0, %x
743 %c2 = fcmp ord float %x, %y
744 %c3 = select i1 %c1, i1 %c2, i1 false
748 ; Negative tests to ensure we don't remove selects with undef true/false values.
749 ; See https://bugs.llvm.org/show_bug.cgi?id=31633
750 ; https://lists.llvm.org/pipermail/llvm-dev/2016-October/106182.html
751 ; https://reviews.llvm.org/D83360
752 define i32 @false_undef(i1 %cond, i32 %x) {
753 ; CHECK-LABEL: @false_undef(
754 ; CHECK-NEXT: [[S:%.*]] = select i1 [[COND:%.*]], i32 [[X:%.*]], i32 undef
755 ; CHECK-NEXT: ret i32 [[S]]
757 %s = select i1 %cond, i32 %x, i32 undef
761 define i32 @true_undef(i1 %cond, i32 %x) {
762 ; CHECK-LABEL: @true_undef(
763 ; CHECK-NEXT: [[S:%.*]] = select i1 [[COND:%.*]], i32 undef, i32 [[X:%.*]]
764 ; CHECK-NEXT: ret i32 [[S]]
766 %s = select i1 %cond, i32 undef, i32 %x
770 define <2 x i32> @false_undef_vec(i1 %cond, <2 x i32> %x) {
771 ; CHECK-LABEL: @false_undef_vec(
772 ; CHECK-NEXT: [[S:%.*]] = select i1 [[COND:%.*]], <2 x i32> [[X:%.*]], <2 x i32> undef
773 ; CHECK-NEXT: ret <2 x i32> [[S]]
775 %s = select i1 %cond, <2 x i32> %x, <2 x i32> undef
779 define <2 x i32> @true_undef_vec(i1 %cond, <2 x i32> %x) {
780 ; CHECK-LABEL: @true_undef_vec(
781 ; CHECK-NEXT: [[S:%.*]] = select i1 [[COND:%.*]], <2 x i32> undef, <2 x i32> [[X:%.*]]
782 ; CHECK-NEXT: ret <2 x i32> [[S]]
784 %s = select i1 %cond, <2 x i32> undef, <2 x i32> %x
788 ; These can be folded because the other value is guaranteed not to be poison.
789 define i32 @false_undef_true_constant(i1 %cond) {
790 ; CHECK-LABEL: @false_undef_true_constant(
791 ; CHECK-NEXT: ret i32 10
793 %s = select i1 %cond, i32 10, i32 undef
797 define i32 @true_undef_false_constant(i1 %cond) {
798 ; CHECK-LABEL: @true_undef_false_constant(
799 ; CHECK-NEXT: ret i32 20
801 %s = select i1 %cond, i32 undef, i32 20
805 define <2 x i32> @false_undef_true_constant_vec(i1 %cond) {
806 ; CHECK-LABEL: @false_undef_true_constant_vec(
807 ; CHECK-NEXT: ret <2 x i32> <i32 42, i32 -42>
809 %s = select i1 %cond, <2 x i32> <i32 42, i32 -42>, <2 x i32> undef
813 define <2 x i32> @true_undef_false_constant_vec(i1 %cond) {
814 ; CHECK-LABEL: @true_undef_false_constant_vec(
815 ; CHECK-NEXT: ret <2 x i32> <i32 -42, i32 42>
817 %s = select i1 %cond, <2 x i32> undef, <2 x i32> <i32 -42, i32 42>
821 ; If one input is undef and the other is freeze, we can fold it to the freeze.
822 define i32 @false_undef_true_freeze(i1 %cond, i32 %x) {
823 ; CHECK-LABEL: @false_undef_true_freeze(
824 ; CHECK-NEXT: [[XF:%.*]] = freeze i32 [[X:%.*]]
825 ; CHECK-NEXT: ret i32 [[XF]]
828 %s = select i1 %cond, i32 %xf, i32 undef
832 define i32 @false_undef_false_freeze(i1 %cond, i32 %x) {
833 ; CHECK-LABEL: @false_undef_false_freeze(
834 ; CHECK-NEXT: [[XF:%.*]] = freeze i32 [[X:%.*]]
835 ; CHECK-NEXT: ret i32 [[XF]]
838 %s = select i1 %cond, i32 undef, i32 %xf
842 @g = external global i32, align 1
844 define <2 x i32> @false_undef_true_constextpr_vec(i1 %cond) {
845 ; CHECK-LABEL: @false_undef_true_constextpr_vec(
846 ; CHECK-NEXT: ret <2 x i32> <i32 ptrtoint (ptr @g to i32), i32 ptrtoint (ptr @g to i32)>
848 %s = select i1 %cond, <2 x i32> <i32 undef, i32 ptrtoint (ptr @g to i32)>, <2 x i32> <i32 ptrtoint (ptr @g to i32), i32 undef>
852 define i32 @all_constant_true_undef() {
853 ; CHECK-LABEL: @all_constant_true_undef(
854 ; CHECK-NEXT: ret i32 1
856 %s = select i1 ptrtoint (ptr @all_constant_true_undef to i1), i32 undef, i32 1
860 define float @all_constant_false_undef() {
861 ; CHECK-LABEL: @all_constant_false_undef(
862 ; CHECK-NEXT: ret float 1.000000e+00
864 %s = select i1 ptrtoint (ptr @all_constant_false_undef to i1), float undef, float 1.0
868 define <2 x i32> @all_constant_true_undef_vec() {
869 ; CHECK-LABEL: @all_constant_true_undef_vec(
870 ; CHECK-NEXT: ret <2 x i32> <i32 1, i32 -1>
872 %s = select i1 ptrtoint (ptr @all_constant_true_undef_vec to i1), <2 x i32> undef, <2 x i32> <i32 1, i32 -1>
876 define <2 x float> @all_constant_false_undef_vec() {
877 ; CHECK-LABEL: @all_constant_false_undef_vec(
878 ; CHECK-NEXT: ret <2 x float> <float 1.000000e+00, float -1.000000e+00>
880 %s = select i1 ptrtoint (ptr @all_constant_false_undef_vec to i1), <2 x float> undef, <2 x float> <float 1.0, float -1.0>
884 @a = external global [3 x ptr]
885 define i32 @all_constant_false_undef_true_poison_gen_constexpr(i1 %a) {
886 ; CHECK-LABEL: @all_constant_false_undef_true_poison_gen_constexpr(
887 ; CHECK-NEXT: [[S:%.*]] = select i1 [[A:%.*]], i32 ptrtoint (ptr getelementptr inbounds ([3 x ptr], ptr @a, i64 2) to i32), i32 undef
888 ; CHECK-NEXT: ret i32 [[S]]
890 %s = select i1 %a, i32 ptrtoint (ptr getelementptr inbounds ([3 x ptr], ptr @a, i64 2) to i32), i32 undef
894 define i32 @all_constant_false_undef_true_constexpr() {
895 ; CHECK-LABEL: @all_constant_false_undef_true_constexpr(
896 ; CHECK-NEXT: ret i32 ptrtoint (ptr @all_constant_false_undef_true_constexpr to i32)
898 %s = select i1 ptrtoint (ptr @all_constant_false_undef_true_constexpr to i1), i32 ptrtoint (ptr @all_constant_false_undef_true_constexpr to i32), i32 undef
902 define i32 @all_constant_true_undef_false_constexpr() {
903 ; CHECK-LABEL: @all_constant_true_undef_false_constexpr(
904 ; CHECK-NEXT: ret i32 ptrtoint (ptr @all_constant_true_undef_false_constexpr to i32)
906 %s = select i1 ptrtoint (ptr @all_constant_true_undef_false_constexpr to i1), i32 undef, i32 ptrtoint (ptr @all_constant_true_undef_false_constexpr to i32)
910 ; Negative tests. Don't fold if the non-undef operand is a vector containing a constexpr.
911 define <2 x i32> @all_constant_false_undef_true_constexpr_vec() {
912 ; CHECK-LABEL: @all_constant_false_undef_true_constexpr_vec(
913 ; CHECK-NEXT: ret <2 x i32> <i32 ptrtoint (ptr @all_constant_false_undef_true_constexpr_vec to i32), i32 -1>
915 %s = select i1 ptrtoint (ptr @all_constant_false_undef_true_constexpr_vec to i1), <2 x i32> <i32 ptrtoint (ptr @all_constant_false_undef_true_constexpr_vec to i32), i32 -1>, <2 x i32> undef
919 define <2 x i32> @all_constant_true_undef_false_constexpr_vec() {
920 ; CHECK-LABEL: @all_constant_true_undef_false_constexpr_vec(
921 ; CHECK-NEXT: ret <2 x i32> <i32 -1, i32 ptrtoint (ptr @all_constant_true_undef_false_constexpr_vec to i32)>
923 %s = select i1 ptrtoint (ptr @all_constant_true_undef_false_constexpr_vec to i1), <2 x i32> undef, <2 x i32><i32 -1, i32 ptrtoint (ptr @all_constant_true_undef_false_constexpr_vec to i32)>
927 define i1 @expand_binop_undef(i32 %x, i32 %y) {
928 ; CHECK-LABEL: @expand_binop_undef(
929 ; CHECK-NEXT: [[CMP9_NOT_1:%.*]] = icmp eq i32 [[X:%.*]], [[Y:%.*]]
930 ; CHECK-NEXT: [[CMP15:%.*]] = icmp slt i32 [[X]], [[Y]]
931 ; CHECK-NEXT: [[SPEC_SELECT39:%.*]] = select i1 [[CMP9_NOT_1]], i1 undef, i1 [[CMP15]]
932 ; CHECK-NEXT: [[SPEC_SELECT40:%.*]] = xor i1 [[CMP9_NOT_1]], true
933 ; CHECK-NEXT: [[SPEC_SELECT:%.*]] = and i1 [[SPEC_SELECT39]], [[SPEC_SELECT40]]
934 ; CHECK-NEXT: ret i1 [[SPEC_SELECT]]
936 %cmp9.not.1 = icmp eq i32 %x, %y
937 %cmp15 = icmp slt i32 %x, %y
938 %spec.select39 = select i1 %cmp9.not.1, i1 undef, i1 %cmp15
939 %spec.select40 = xor i1 %cmp9.not.1, 1
940 %spec.select = and i1 %spec.select39, %spec.select40
944 define i32 @pr47322_more_poisonous_replacement(i32 %arg) {
945 ; CHECK-LABEL: @pr47322_more_poisonous_replacement(
946 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[ARG:%.*]], 0
947 ; CHECK-NEXT: [[TRAILING:%.*]] = call i32 @llvm.cttz.i32(i32 [[ARG]], i1 immarg true)
948 ; CHECK-NEXT: [[SHIFTED:%.*]] = lshr i32 [[ARG]], [[TRAILING]]
949 ; CHECK-NEXT: [[R1_SROA_0_1:%.*]] = select i1 [[CMP]], i32 0, i32 [[SHIFTED]]
950 ; CHECK-NEXT: ret i32 [[R1_SROA_0_1]]
952 %cmp = icmp eq i32 %arg, 0
953 %trailing = call i32 @llvm.cttz.i32(i32 %arg, i1 immarg true)
954 %shifted = lshr i32 %arg, %trailing
955 %r1.sroa.0.1 = select i1 %cmp, i32 0, i32 %shifted
958 declare i32 @llvm.cttz.i32(i32, i1 immarg)
960 ; Partial undef scalable vectors should be ignored.
961 define <vscale x 2 x i1> @ignore_scalable_undef(<vscale x 2 x i1> %cond) {
962 ; CHECK-LABEL: @ignore_scalable_undef(
963 ; CHECK-NEXT: [[S:%.*]] = select <vscale x 2 x i1> [[COND:%.*]], <vscale x 2 x i1> undef, <vscale x 2 x i1> insertelement (<vscale x 2 x i1> poison, i1 true, i32 0)
964 ; CHECK-NEXT: ret <vscale x 2 x i1> [[S]]
966 %vec = insertelement <vscale x 2 x i1> poison, i1 true, i32 0
967 %s = select <vscale x 2 x i1> %cond, <vscale x 2 x i1> undef, <vscale x 2 x i1> %vec
968 ret <vscale x 2 x i1> %s
971 define i32 @poison(i32 %x, i32 %y) {
972 ; CHECK-LABEL: @poison(
973 ; CHECK-NEXT: ret i32 [[X:%.*]]
975 %v = select i1 undef, i32 %x, i32 %y
979 define i32 @poison2(i1 %cond, i32 %x) {
980 ; CHECK-LABEL: @poison2(
981 ; CHECK-NEXT: ret i32 [[X:%.*]]
983 %v = select i1 %cond, i32 poison, i32 %x
987 define i32 @poison3(i1 %cond, i32 %x) {
988 ; CHECK-LABEL: @poison3(
989 ; CHECK-NEXT: ret i32 [[X:%.*]]
991 %v = select i1 %cond, i32 %x, i32 poison
995 define <2 x i32> @poison4(<2 x i1> %cond, <2 x i32> %x) {
996 ; CHECK-LABEL: @poison4(
997 ; CHECK-NEXT: ret <2 x i32> [[X:%.*]]
999 %v = select <2 x i1> %cond, <2 x i32> %x, <2 x i32> poison