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 i1 @cond_constexpr_bool_true_or_false(i1 %cond) {
13 ; CHECK-LABEL: @cond_constexpr_bool_true_or_false(
14 ; CHECK-NEXT: ret i1 ptrtoint (ptr @cond_constexpr_bool_true_or_false to i1)
16 %s = select i1 ptrtoint (ptr @cond_constexpr_bool_true_or_false to i1), i1 true, i1 false
20 define <2 x i1> @bool_true_or_false_vec(<2 x i1> %cond) {
21 ; CHECK-LABEL: @bool_true_or_false_vec(
22 ; CHECK-NEXT: ret <2 x i1> [[COND:%.*]]
24 %s = select <2 x i1> %cond, <2 x i1> <i1 true, i1 true>, <2 x i1> zeroinitializer
28 define <2 x i1> @bool_true_or_false_vec_undef(<2 x i1> %cond) {
29 ; CHECK-LABEL: @bool_true_or_false_vec_undef(
30 ; CHECK-NEXT: ret <2 x i1> [[COND:%.*]]
32 %s = select <2 x i1> %cond, <2 x i1> <i1 undef, i1 true>, <2 x i1> <i1 false, i1 undef>
36 define i32 @cond_is_false(i32 %A, i32 %B) {
37 ; CHECK-LABEL: @cond_is_false(
38 ; CHECK-NEXT: ret i32 [[B:%.*]]
40 %C = select i1 false, i32 %A, i32 %B
44 define i32 @cond_is_true(i32 %A, i32 %B) {
45 ; CHECK-LABEL: @cond_is_true(
46 ; CHECK-NEXT: ret i32 [[A:%.*]]
48 %C = select i1 true, i32 %A, i32 %B
52 define i32 @equal_arms(i1 %cond, i32 %x) {
53 ; CHECK-LABEL: @equal_arms(
54 ; CHECK-NEXT: ret i32 [[X:%.*]]
56 %V = select i1 %cond, i32 %x, i32 %x
60 define <2 x i32> @equal_arms_vec(<2 x i1> %cond, <2 x i32> %x) {
61 ; CHECK-LABEL: @equal_arms_vec(
62 ; CHECK-NEXT: ret <2 x i32> [[X:%.*]]
64 %V = select <2 x i1> %cond, <2 x i32> %x, <2 x i32> %x
68 define <2 x i32> @equal_arms_vec_undef(<2 x i1> %cond) {
69 ; CHECK-LABEL: @equal_arms_vec_undef(
70 ; CHECK-NEXT: ret <2 x i32> <i32 42, i32 42>
72 %V = select <2 x i1> %cond, <2 x i32> <i32 42, i32 undef>, <2 x i32> <i32 undef, i32 42>
76 define <3 x float> @equal_arms_vec_less_undef(<3 x i1> %cond) {
77 ; CHECK-LABEL: @equal_arms_vec_less_undef(
78 ; CHECK-NEXT: ret <3 x float> <float 4.200000e+01, float 4.200000e+01, float 4.300000e+01>
80 %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>
84 define <3 x float> @equal_arms_vec_more_undef(<3 x i1> %cond) {
85 ; CHECK-LABEL: @equal_arms_vec_more_undef(
86 ; CHECK-NEXT: ret <3 x float> <float 4.200000e+01, float undef, float 4.300000e+01>
88 %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>
92 define <2 x i8> @vsel_tvec(<2 x i8> %x, <2 x i8> %y) {
93 ; CHECK-LABEL: @vsel_tvec(
94 ; CHECK-NEXT: ret <2 x i8> [[X:%.*]]
96 %s = select <2 x i1><i1 true, i1 true>, <2 x i8> %x, <2 x i8> %y
100 define <2 x i8> @vsel_fvec(<2 x i8> %x, <2 x i8> %y) {
101 ; CHECK-LABEL: @vsel_fvec(
102 ; CHECK-NEXT: ret <2 x i8> [[Y:%.*]]
104 %s = select <2 x i1><i1 false, i1 false>, <2 x i8> %x, <2 x i8> %y
108 define <2 x i8> @vsel_mixedvec() {
109 ; CHECK-LABEL: @vsel_mixedvec(
110 ; CHECK-NEXT: ret <2 x i8> <i8 0, i8 3>
112 %s = select <2 x i1><i1 true, i1 false>, <2 x i8> <i8 0, i8 1>, <2 x i8> <i8 2, i8 3>
116 define <3 x i8> @vsel_undef_true_op(<3 x i8> %x, <3 x i8> %y) {
117 ; CHECK-LABEL: @vsel_undef_true_op(
118 ; CHECK-NEXT: ret <3 x i8> [[X:%.*]]
120 %s = select <3 x i1><i1 1, i1 undef, i1 1>, <3 x i8> %x, <3 x i8> %y
124 define <3 x i4> @vsel_undef_false_op(<3 x i4> %x, <3 x i4> %y) {
125 ; CHECK-LABEL: @vsel_undef_false_op(
126 ; CHECK-NEXT: ret <3 x i4> [[Y:%.*]]
128 %s = select <3 x i1><i1 0, i1 undef, i1 undef>, <3 x i4> %x, <3 x i4> %y
132 define i32 @test1(i32 %x) {
133 ; CHECK-LABEL: @test1(
134 ; CHECK-NEXT: ret i32 [[X:%.*]]
137 %cmp = icmp eq i32 %and, 0
138 %and1 = and i32 %x, -2
139 %and1.x = select i1 %cmp, i32 %and1, i32 %x
143 define i32 @test2(i32 %x) {
144 ; CHECK-LABEL: @test2(
145 ; CHECK-NEXT: ret i32 [[X:%.*]]
148 %cmp = icmp ne i32 %and, 0
149 %and1 = and i32 %x, -2
150 %and1.x = select i1 %cmp, i32 %x, i32 %and1
154 define i32 @test3(i32 %x) {
155 ; CHECK-LABEL: @test3(
156 ; CHECK-NEXT: [[AND1:%.*]] = and i32 [[X:%.*]], -2
157 ; CHECK-NEXT: ret i32 [[AND1]]
160 %cmp = icmp ne i32 %and, 0
161 %and1 = and i32 %x, -2
162 %and1.x = select i1 %cmp, i32 %and1, i32 %x
166 define i32 @test4(i32 %X) {
167 ; CHECK-LABEL: @test4(
168 ; CHECK-NEXT: [[OR:%.*]] = or i32 [[X:%.*]], -2147483648
169 ; CHECK-NEXT: ret i32 [[OR]]
171 %cmp = icmp slt i32 %X, 0
172 %or = or i32 %X, -2147483648
173 %cond = select i1 %cmp, i32 %X, i32 %or
177 define i32 @test4_disjoint(i32 %X) {
178 ; CHECK-LABEL: @test4_disjoint(
179 ; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[X:%.*]], 0
180 ; CHECK-NEXT: [[OR:%.*]] = or disjoint i32 [[X]], -2147483648
181 ; CHECK-NEXT: [[COND:%.*]] = select i1 [[CMP]], i32 [[X]], i32 [[OR]]
182 ; CHECK-NEXT: ret i32 [[COND]]
184 %cmp = icmp slt i32 %X, 0
185 %or = or disjoint i32 %X, -2147483648
186 %cond = select i1 %cmp, i32 %X, i32 %or
190 ; Same as above, but the compare isn't canonical
191 define i32 @test4noncanon(i32 %X) {
192 ; CHECK-LABEL: @test4noncanon(
193 ; CHECK-NEXT: [[OR:%.*]] = or i32 [[X:%.*]], -2147483648
194 ; CHECK-NEXT: ret i32 [[OR]]
196 %cmp = icmp sle i32 %X, -1
197 %or = or i32 %X, -2147483648
198 %cond = select i1 %cmp, i32 %X, i32 %or
202 define i32 @test5(i32 %X) {
203 ; CHECK-LABEL: @test5(
204 ; CHECK-NEXT: ret i32 [[X:%.*]]
206 %cmp = icmp slt i32 %X, 0
207 %or = or i32 %X, -2147483648
208 %cond = select i1 %cmp, i32 %or, i32 %X
212 define i32 @test5_disjoint(i32 %X) {
213 ; CHECK-LABEL: @test5_disjoint(
214 ; CHECK-NEXT: ret i32 [[X:%.*]]
216 %cmp = icmp slt i32 %X, 0
217 %or = or disjoint i32 %X, -2147483648
218 %cond = select i1 %cmp, i32 %or, i32 %X
222 define i32 @test6(i32 %X) {
223 ; CHECK-LABEL: @test6(
224 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 2147483647
225 ; CHECK-NEXT: ret i32 [[AND]]
227 %cmp = icmp slt i32 %X, 0
228 %and = and i32 %X, 2147483647
229 %cond = select i1 %cmp, i32 %and, i32 %X
233 define i32 @test7(i32 %X) {
234 ; CHECK-LABEL: @test7(
235 ; CHECK-NEXT: ret i32 [[X:%.*]]
237 %cmp = icmp slt i32 %X, 0
238 %and = and i32 %X, 2147483647
239 %cond = select i1 %cmp, i32 %X, i32 %and
243 define i32 @test8(i32 %X) {
244 ; CHECK-LABEL: @test8(
245 ; CHECK-NEXT: ret i32 [[X:%.*]]
247 %cmp = icmp sgt i32 %X, -1
248 %or = or i32 %X, -2147483648
249 %cond = select i1 %cmp, i32 %X, i32 %or
253 define i32 @test8_disjoint(i32 %X) {
254 ; CHECK-LABEL: @test8_disjoint(
255 ; CHECK-NEXT: ret i32 [[X:%.*]]
257 %cmp = icmp sgt i32 %X, -1
258 %or = or disjoint i32 %X, -2147483648
259 %cond = select i1 %cmp, i32 %X, i32 %or
263 define i32 @test9(i32 %X) {
264 ; CHECK-LABEL: @test9(
265 ; CHECK-NEXT: [[OR:%.*]] = or i32 [[X:%.*]], -2147483648
266 ; CHECK-NEXT: ret i32 [[OR]]
268 %cmp = icmp sgt i32 %X, -1
269 %or = or i32 %X, -2147483648
270 %cond = select i1 %cmp, i32 %or, i32 %X
274 define i32 @test9_disjoint(i32 %X) {
275 ; CHECK-LABEL: @test9_disjoint(
276 ; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[X:%.*]], -1
277 ; CHECK-NEXT: [[OR:%.*]] = or disjoint i32 [[X]], -2147483648
278 ; CHECK-NEXT: [[COND:%.*]] = select i1 [[CMP]], i32 [[OR]], i32 [[X]]
279 ; CHECK-NEXT: ret i32 [[COND]]
281 %cmp = icmp sgt i32 %X, -1
282 %or = or disjoint i32 %X, -2147483648
283 %cond = select i1 %cmp, i32 %or, i32 %X
287 ; Same as above, but the compare isn't canonical
288 define i32 @test9noncanon(i32 %X) {
289 ; CHECK-LABEL: @test9noncanon(
290 ; CHECK-NEXT: [[OR:%.*]] = or i32 [[X:%.*]], -2147483648
291 ; CHECK-NEXT: ret i32 [[OR]]
293 %cmp = icmp sge i32 %X, 0
294 %or = or i32 %X, -2147483648
295 %cond = select i1 %cmp, i32 %or, i32 %X
299 define i32 @test10(i32 %X) {
300 ; CHECK-LABEL: @test10(
301 ; CHECK-NEXT: ret i32 [[X:%.*]]
303 %cmp = icmp sgt i32 %X, -1
304 %and = and i32 %X, 2147483647
305 %cond = select i1 %cmp, i32 %and, i32 %X
309 define i32 @test11(i32 %X) {
310 ; CHECK-LABEL: @test11(
311 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 2147483647
312 ; CHECK-NEXT: ret i32 [[AND]]
314 %cmp = icmp sgt i32 %X, -1
315 %and = and i32 %X, 2147483647
316 %cond = select i1 %cmp, i32 %X, i32 %and
320 define <2 x i8> @test11vec(<2 x i8> %X) {
321 ; CHECK-LABEL: @test11vec(
322 ; CHECK-NEXT: [[AND:%.*]] = and <2 x i8> [[X:%.*]], <i8 127, i8 127>
323 ; CHECK-NEXT: ret <2 x i8> [[AND]]
325 %cmp = icmp sgt <2 x i8> %X, <i8 -1, i8 -1>
326 %and = and <2 x i8> %X, <i8 127, i8 127>
327 %sel = select <2 x i1> %cmp, <2 x i8> %X, <2 x i8> %and
331 define i32 @test12(i32 %X) {
332 ; CHECK-LABEL: @test12(
333 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 3
334 ; CHECK-NEXT: ret i32 [[AND]]
336 %cmp = icmp ult i32 %X, 4
338 %cond = select i1 %cmp, i32 %X, i32 %and
342 ; Same as above, but the compare isn't canonical
343 define i32 @test12noncanon(i32 %X) {
344 ; CHECK-LABEL: @test12noncanon(
345 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 3
346 ; CHECK-NEXT: ret i32 [[AND]]
348 %cmp = icmp ule i32 %X, 3
350 %cond = select i1 %cmp, i32 %X, i32 %and
354 define i32 @test13(i32 %X) {
355 ; CHECK-LABEL: @test13(
356 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 3
357 ; CHECK-NEXT: ret i32 [[AND]]
359 %cmp = icmp ugt i32 %X, 3
361 %cond = select i1 %cmp, i32 %and, i32 %X
365 ; Same as above, but the compare isn't canonical
366 define i32 @test13noncanon(i32 %X) {
367 ; CHECK-LABEL: @test13noncanon(
368 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 3
369 ; CHECK-NEXT: ret i32 [[AND]]
371 %cmp = icmp uge i32 %X, 4
373 %cond = select i1 %cmp, i32 %and, i32 %X
377 define i32 @select_icmp_and_8_eq_0_or_8(i32 %x) {
378 ; CHECK-LABEL: @select_icmp_and_8_eq_0_or_8(
379 ; CHECK-NEXT: [[OR:%.*]] = or i32 [[X:%.*]], 8
380 ; CHECK-NEXT: ret i32 [[OR]]
383 %cmp = icmp eq i32 %and, 0
385 %sel = select i1 %cmp, i32 %or, i32 %x
389 define i32 @select_icmp_and_8_eq_0_or_8_alt(i32 %x) {
390 ; CHECK-LABEL: @select_icmp_and_8_eq_0_or_8_alt(
391 ; CHECK-NEXT: [[OR:%.*]] = or i32 [[X:%.*]], 8
392 ; CHECK-NEXT: ret i32 [[OR]]
395 %cmp = icmp ne i32 %and, 0
397 %sel = select i1 %cmp, i32 %x, i32 %or
401 define i32 @select_icmp_and_8_ne_0_or_8(i32 %x) {
402 ; CHECK-LABEL: @select_icmp_and_8_ne_0_or_8(
403 ; CHECK-NEXT: ret i32 [[X:%.*]]
406 %cmp = icmp ne i32 %and, 0
408 %sel = select i1 %cmp, i32 %or, i32 %x
412 define i32 @select_icmp_and_8_ne_0_or_8_alt(i32 %x) {
413 ; CHECK-LABEL: @select_icmp_and_8_ne_0_or_8_alt(
414 ; CHECK-NEXT: ret i32 [[X:%.*]]
417 %cmp = icmp eq i32 %and, 0
419 %sel = select i1 %cmp, i32 %x, i32 %or
423 define i32 @select_icmp_and_8_eq_0_and_not_8(i32 %x) {
424 ; CHECK-LABEL: @select_icmp_and_8_eq_0_and_not_8(
425 ; CHECK-NEXT: [[AND1:%.*]] = and i32 [[X:%.*]], -9
426 ; CHECK-NEXT: ret i32 [[AND1]]
429 %cmp = icmp eq i32 %and, 0
430 %and1 = and i32 %x, -9
431 %sel = select i1 %cmp, i32 %x, i32 %and1
435 define i32 @select_icmp_and_8_eq_0_and_not_8_alt(i32 %x) {
436 ; CHECK-LABEL: @select_icmp_and_8_eq_0_and_not_8_alt(
437 ; CHECK-NEXT: [[AND1:%.*]] = and i32 [[X:%.*]], -9
438 ; CHECK-NEXT: ret i32 [[AND1]]
441 %cmp = icmp ne i32 %and, 0
442 %and1 = and i32 %x, -9
443 %sel = select i1 %cmp, i32 %and1, i32 %x
447 define i32 @select_icmp_and_8_ne_0_and_not_8(i32 %x) {
448 ; CHECK-LABEL: @select_icmp_and_8_ne_0_and_not_8(
449 ; CHECK-NEXT: ret i32 [[X:%.*]]
452 %cmp = icmp ne i32 %and, 0
453 %and1 = and i32 %x, -9
454 %sel = select i1 %cmp, i32 %x, i32 %and1
458 define i32 @select_icmp_and_8_ne_0_and_not_8_alt(i32 %x) {
459 ; CHECK-LABEL: @select_icmp_and_8_ne_0_and_not_8_alt(
460 ; CHECK-NEXT: ret i32 [[X:%.*]]
463 %cmp = icmp eq i32 %and, 0
464 %and1 = and i32 %x, -9
465 %sel = select i1 %cmp, i32 %and1, i32 %x
469 ; PR28466: https://llvm.org/bugs/show_bug.cgi?id=28466
470 ; Each of the previous 8 patterns has a variant that replaces the
471 ; 'and' with a 'trunc' and the icmp eq/ne with icmp slt/sgt.
473 define i32 @select_icmp_trunc_8_ne_0_or_128(i32 %x) {
474 ; CHECK-LABEL: @select_icmp_trunc_8_ne_0_or_128(
475 ; CHECK-NEXT: [[OR:%.*]] = or i32 [[X:%.*]], 128
476 ; CHECK-NEXT: ret i32 [[OR]]
478 %trunc = trunc i32 %x to i8
479 %cmp = icmp sgt i8 %trunc, -1
481 %sel = select i1 %cmp, i32 %or, i32 %x
485 define i32 @select_icmp_trunc_8_ne_0_or_128_alt(i32 %x) {
486 ; CHECK-LABEL: @select_icmp_trunc_8_ne_0_or_128_alt(
487 ; CHECK-NEXT: [[OR:%.*]] = or i32 [[X:%.*]], 128
488 ; CHECK-NEXT: ret i32 [[OR]]
490 %trunc = trunc i32 %x to i8
491 %cmp = icmp slt i8 %trunc, 0
493 %sel = select i1 %cmp, i32 %x, i32 %or
497 define i32 @select_icmp_trunc_8_eq_0_or_128(i32 %x) {
498 ; CHECK-LABEL: @select_icmp_trunc_8_eq_0_or_128(
499 ; CHECK-NEXT: ret i32 [[X:%.*]]
501 %trunc = trunc i32 %x to i8
502 %cmp = icmp slt i8 %trunc, 0
504 %sel = select i1 %cmp, i32 %or, i32 %x
508 define i32 @select_icmp_trunc_8_eq_0_or_128_alt(i32 %x) {
509 ; CHECK-LABEL: @select_icmp_trunc_8_eq_0_or_128_alt(
510 ; CHECK-NEXT: ret i32 [[X:%.*]]
512 %trunc = trunc i32 %x to i8
513 %cmp = icmp sgt i8 %trunc, -1
515 %sel = select i1 %cmp, i32 %x, i32 %or
519 define i32 @select_icmp_trunc_8_eq_0_and_not_8(i32 %x) {
520 ; CHECK-LABEL: @select_icmp_trunc_8_eq_0_and_not_8(
521 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], -9
522 ; CHECK-NEXT: ret i32 [[AND]]
524 %trunc = trunc i32 %x to i4
525 %cmp = icmp sgt i4 %trunc, -1
526 %and = and i32 %x, -9
527 %sel = select i1 %cmp, i32 %x, i32 %and
531 define i32 @select_icmp_trunc_8_eq_0_and_not_8_alt(i32 %x) {
532 ; CHECK-LABEL: @select_icmp_trunc_8_eq_0_and_not_8_alt(
533 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], -9
534 ; CHECK-NEXT: ret i32 [[AND]]
536 %trunc = trunc i32 %x to i4
537 %cmp = icmp slt i4 %trunc, 0
538 %and = and i32 %x, -9
539 %sel = select i1 %cmp, i32 %and, i32 %x
543 define i32 @select_icmp_trunc_8_ne_0_and_not_8(i32 %x) {
544 ; CHECK-LABEL: @select_icmp_trunc_8_ne_0_and_not_8(
545 ; CHECK-NEXT: ret i32 [[X:%.*]]
547 %trunc = trunc i32 %x to i4
548 %cmp = icmp slt i4 %trunc, 0
549 %and = and i32 %x, -9
550 %sel = select i1 %cmp, i32 %x, i32 %and
554 define i32 @select_icmp_trunc_8_ne_0_and_not_8_alt(i32 %x) {
555 ; CHECK-LABEL: @select_icmp_trunc_8_ne_0_and_not_8_alt(
556 ; CHECK-NEXT: ret i32 [[X:%.*]]
558 %trunc = trunc i32 %x to i4
559 %cmp = icmp sgt i4 %trunc, -1
560 %and = and i32 %x, -9
561 %sel = select i1 %cmp, i32 %and, i32 %x
565 ; Make sure that at least a few of the same patterns are repeated with vector types.
567 define <2 x i32> @select_icmp_and_8_ne_0_and_not_8_vec(<2 x i32> %x) {
568 ; CHECK-LABEL: @select_icmp_and_8_ne_0_and_not_8_vec(
569 ; CHECK-NEXT: ret <2 x i32> [[X:%.*]]
571 %and = and <2 x i32> %x, <i32 8, i32 8>
572 %cmp = icmp ne <2 x i32> %and, zeroinitializer
573 %and1 = and <2 x i32> %x, <i32 -9, i32 -9>
574 %sel = select <2 x i1> %cmp, <2 x i32> %x, <2 x i32> %and1
578 define <2 x i32> @select_icmp_trunc_8_ne_0_and_not_8_alt_vec(<2 x i32> %x) {
579 ; CHECK-LABEL: @select_icmp_trunc_8_ne_0_and_not_8_alt_vec(
580 ; CHECK-NEXT: ret <2 x i32> [[X:%.*]]
582 %trunc = trunc <2 x i32> %x to <2 x i4>
583 %cmp = icmp sgt <2 x i4> %trunc, <i4 -1, i4 -1>
584 %and = and <2 x i32> %x, <i32 -9, i32 -9>
585 %sel = select <2 x i1> %cmp, <2 x i32> %and, <2 x i32> %x
589 ; Insert a bit from x into y? This should be possible in InstCombine, but not InstSimplify?
591 define i32 @select_icmp_x_and_8_eq_0_y_and_not_8(i32 %x, i32 %y) {
592 ; CHECK-LABEL: @select_icmp_x_and_8_eq_0_y_and_not_8(
593 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 8
594 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[AND]], 0
595 ; CHECK-NEXT: [[AND1:%.*]] = and i32 [[Y:%.*]], -9
596 ; CHECK-NEXT: [[Y_AND1:%.*]] = select i1 [[CMP]], i32 [[Y]], i32 [[AND1]]
597 ; CHECK-NEXT: ret i32 [[Y_AND1]]
600 %cmp = icmp eq i32 %and, 0
601 %and1 = and i32 %y, -9
602 %y.and1 = select i1 %cmp, i32 %y, i32 %and1
606 define i64 @select_icmp_x_and_8_eq_0_y64_and_not_8(i32 %x, i64 %y) {
607 ; CHECK-LABEL: @select_icmp_x_and_8_eq_0_y64_and_not_8(
608 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 8
609 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[AND]], 0
610 ; CHECK-NEXT: [[AND1:%.*]] = and i64 [[Y:%.*]], -9
611 ; CHECK-NEXT: [[Y_AND1:%.*]] = select i1 [[CMP]], i64 [[Y]], i64 [[AND1]]
612 ; CHECK-NEXT: ret i64 [[Y_AND1]]
615 %cmp = icmp eq i32 %and, 0
616 %and1 = and i64 %y, -9
617 %y.and1 = select i1 %cmp, i64 %y, i64 %and1
621 define i64 @select_icmp_x_and_8_ne_0_y64_and_not_8(i32 %x, i64 %y) {
622 ; CHECK-LABEL: @select_icmp_x_and_8_ne_0_y64_and_not_8(
623 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 8
624 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[AND]], 0
625 ; CHECK-NEXT: [[AND1:%.*]] = and i64 [[Y:%.*]], -9
626 ; CHECK-NEXT: [[AND1_Y:%.*]] = select i1 [[CMP]], i64 [[AND1]], i64 [[Y]]
627 ; CHECK-NEXT: ret i64 [[AND1_Y]]
630 %cmp = icmp eq i32 %and, 0
631 %and1 = and i64 %y, -9
632 %and1.y = select i1 %cmp, i64 %and1, i64 %y
636 ; Don't crash on a pointer or aggregate type.
638 define ptr @select_icmp_pointers(ptr %x, ptr %y) {
639 ; CHECK-LABEL: @select_icmp_pointers(
640 ; CHECK-NEXT: [[CMP:%.*]] = icmp slt ptr [[X:%.*]], null
641 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], ptr [[X]], ptr [[Y:%.*]]
642 ; CHECK-NEXT: ret ptr [[SEL]]
644 %cmp = icmp slt ptr %x, null
645 %sel = select i1 %cmp, ptr %x, ptr %y
649 ; If the condition is known, we don't need to select, but we're not
650 ; doing this fold here to avoid compile-time cost.
652 declare void @llvm.assume(i1)
654 define i8 @assume_sel_cond(i1 %cond, i8 %x, i8 %y) {
655 ; CHECK-LABEL: @assume_sel_cond(
656 ; CHECK-NEXT: call void @llvm.assume(i1 [[COND:%.*]])
657 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[COND]], i8 [[X:%.*]], i8 [[Y:%.*]]
658 ; CHECK-NEXT: ret i8 [[SEL]]
660 call void @llvm.assume(i1 %cond)
661 %sel = select i1 %cond, i8 %x, i8 %y
665 define i8 @do_not_assume_sel_cond(i1 %cond, i8 %x, i8 %y) {
666 ; CHECK-LABEL: @do_not_assume_sel_cond(
667 ; CHECK-NEXT: [[NOTCOND:%.*]] = icmp eq i1 [[COND:%.*]], false
668 ; CHECK-NEXT: call void @llvm.assume(i1 [[NOTCOND]])
669 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[COND]], i8 [[X:%.*]], i8 [[Y:%.*]]
670 ; CHECK-NEXT: ret i8 [[SEL]]
672 %notcond = icmp eq i1 %cond, false
673 call void @llvm.assume(i1 %notcond)
674 %sel = select i1 %cond, i8 %x, i8 %y
678 define ptr @select_icmp_eq_0_gep_operand(ptr %base, i64 %n) {
679 ; CHECK-LABEL: @select_icmp_eq_0_gep_operand(
680 ; CHECK-NEXT: [[GEP:%.*]] = getelementptr i32, ptr [[BASE:%.*]], i64 [[N:%.*]]
681 ; CHECK-NEXT: ret ptr [[GEP]]
683 %cond = icmp eq i64 %n, 0
684 %gep = getelementptr i32, ptr %base, i64 %n
685 %r = select i1 %cond, ptr %base, ptr %gep
689 define ptr @select_icmp_ne_0_gep_operand(ptr %base, i64 %n) {
690 ; CHECK-LABEL: @select_icmp_ne_0_gep_operand(
691 ; CHECK-NEXT: [[GEP:%.*]] = getelementptr i32, ptr [[BASE:%.*]], i64 [[N:%.*]]
692 ; CHECK-NEXT: ret ptr [[GEP]]
694 %cond = icmp ne i64 %n, 0
695 %gep = getelementptr i32, ptr %base, i64 %n
696 %r = select i1 %cond, ptr %gep, ptr %base
700 define i1 @and_cmps(i32 %x) {
701 ; CHECK-LABEL: @and_cmps(
702 ; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i32 [[X:%.*]], 92
703 ; CHECK-NEXT: [[CMP2:%.*]] = icmp slt i32 [[X]], 11
704 ; CHECK-NEXT: [[R:%.*]] = select i1 [[CMP1]], i1 [[CMP2]], i1 false
705 ; CHECK-NEXT: ret i1 [[R]]
707 %cmp1 = icmp slt i32 %x, 92
708 %cmp2 = icmp slt i32 %x, 11
709 %r = select i1 %cmp1, i1 %cmp2, i1 false
713 define <2 x i1> @and_cmps_vector(<2 x i32> %x) {
714 ; CHECK-LABEL: @and_cmps_vector(
715 ; CHECK-NEXT: [[CMP1:%.*]] = icmp slt <2 x i32> [[X:%.*]], <i32 92, i32 92>
716 ; CHECK-NEXT: [[CMP2:%.*]] = icmp slt <2 x i32> [[X]], <i32 11, i32 11>
717 ; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[CMP1]], <2 x i1> [[CMP2]], <2 x i1> zeroinitializer
718 ; CHECK-NEXT: ret <2 x i1> [[R]]
720 %cmp1 = icmp slt <2 x i32> %x, <i32 92, i32 92>
721 %cmp2 = icmp slt <2 x i32> %x, <i32 11, i32 11>
722 %r = select <2 x i1> %cmp1, <2 x i1> %cmp2, <2 x i1> <i1 false, i1 false>
726 define i1 @or_cmps(float %x) {
727 ; CHECK-LABEL: @or_cmps(
728 ; CHECK-NEXT: [[CMP1:%.*]] = fcmp uno float [[X:%.*]], 4.200000e+01
729 ; CHECK-NEXT: [[CMP2:%.*]] = fcmp uno float [[X]], 5.200000e+01
730 ; CHECK-NEXT: [[R:%.*]] = select i1 [[CMP1]], i1 true, i1 [[CMP2]]
731 ; CHECK-NEXT: ret i1 [[R]]
733 %cmp1 = fcmp uno float %x, 42.0
734 %cmp2 = fcmp uno float %x, 52.0
735 %r = select i1 %cmp1, i1 true, i1 %cmp2
739 define <2 x i1> @or_logic_vector(<2 x i1> %x, <2 x i1> %y) {
740 ; CHECK-LABEL: @or_logic_vector(
741 ; CHECK-NEXT: ret <2 x i1> [[X:%.*]]
743 %a = and <2 x i1> %x, %y
744 %r = select <2 x i1> %x, <2 x i1> <i1 true, i1 true>, <2 x i1> %a
748 define i1 @and_not_cmps(i32 %x) {
749 ; CHECK-LABEL: @and_not_cmps(
750 ; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i32 [[X:%.*]], 92
751 ; CHECK-NEXT: [[CMP2:%.*]] = icmp slt i32 [[X]], 11
752 ; CHECK-NEXT: [[R:%.*]] = select i1 [[CMP1]], i1 false, i1 [[CMP2]]
753 ; CHECK-NEXT: ret i1 [[R]]
755 %cmp1 = icmp slt i32 %x, 92
756 %cmp2 = icmp slt i32 %x, 11
757 %r = select i1 %cmp1, i1 false, i1 %cmp2
761 define i1 @or_not_cmps(i32 %x) {
762 ; CHECK-LABEL: @or_not_cmps(
763 ; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i32 [[X:%.*]], 92
764 ; CHECK-NEXT: [[CMP2:%.*]] = icmp slt i32 [[X]], 11
765 ; CHECK-NEXT: [[R:%.*]] = select i1 [[CMP1]], i1 [[CMP2]], i1 true
766 ; CHECK-NEXT: ret i1 [[R]]
768 %cmp1 = icmp slt i32 %x, 92
769 %cmp2 = icmp slt i32 %x, 11
770 %r = select i1 %cmp1, i1 %cmp2, i1 true
774 define i8 @and_cmps_wrong_type(i32 %x) {
775 ; CHECK-LABEL: @and_cmps_wrong_type(
776 ; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i32 [[X:%.*]], 92
777 ; CHECK-NEXT: [[CMP2:%.*]] = icmp slt i32 [[X]], 11
778 ; CHECK-NEXT: [[S:%.*]] = sext i1 [[CMP2]] to i8
779 ; CHECK-NEXT: [[R:%.*]] = select i1 [[CMP1]], i8 [[S]], i8 0
780 ; CHECK-NEXT: ret i8 [[R]]
782 %cmp1 = icmp slt i32 %x, 92
783 %cmp2 = icmp slt i32 %x, 11
784 %s = sext i1 %cmp2 to i8
785 %r = select i1 %cmp1, i8 %s, i8 0
789 define i1 @y_might_be_poison(float %x, float %y) {
790 ; CHECK-LABEL: @y_might_be_poison(
791 ; CHECK-NEXT: [[C1:%.*]] = fcmp ord float 0.000000e+00, [[X:%.*]]
792 ; CHECK-NEXT: [[C2:%.*]] = fcmp ord float [[X]], [[Y:%.*]]
793 ; CHECK-NEXT: [[C3:%.*]] = select i1 [[C1]], i1 [[C2]], i1 false
794 ; CHECK-NEXT: ret i1 [[C3]]
796 %c1 = fcmp ord float 0.0, %x
797 %c2 = fcmp ord float %x, %y
798 %c3 = select i1 %c1, i1 %c2, i1 false
802 ; Negative tests to ensure we don't remove selects with undef true/false values.
803 ; See https://bugs.llvm.org/show_bug.cgi?id=31633
804 ; https://lists.llvm.org/pipermail/llvm-dev/2016-October/106182.html
805 ; https://reviews.llvm.org/D83360
806 define i32 @false_undef(i1 %cond, i32 %x) {
807 ; CHECK-LABEL: @false_undef(
808 ; CHECK-NEXT: [[S:%.*]] = select i1 [[COND:%.*]], i32 [[X:%.*]], i32 undef
809 ; CHECK-NEXT: ret i32 [[S]]
811 %s = select i1 %cond, i32 %x, i32 undef
815 define i32 @true_undef(i1 %cond, i32 %x) {
816 ; CHECK-LABEL: @true_undef(
817 ; CHECK-NEXT: [[S:%.*]] = select i1 [[COND:%.*]], i32 undef, i32 [[X:%.*]]
818 ; CHECK-NEXT: ret i32 [[S]]
820 %s = select i1 %cond, i32 undef, i32 %x
824 define <2 x i32> @false_undef_vec(i1 %cond, <2 x i32> %x) {
825 ; CHECK-LABEL: @false_undef_vec(
826 ; CHECK-NEXT: [[S:%.*]] = select i1 [[COND:%.*]], <2 x i32> [[X:%.*]], <2 x i32> undef
827 ; CHECK-NEXT: ret <2 x i32> [[S]]
829 %s = select i1 %cond, <2 x i32> %x, <2 x i32> undef
833 define <2 x i32> @true_undef_vec(i1 %cond, <2 x i32> %x) {
834 ; CHECK-LABEL: @true_undef_vec(
835 ; CHECK-NEXT: [[S:%.*]] = select i1 [[COND:%.*]], <2 x i32> undef, <2 x i32> [[X:%.*]]
836 ; CHECK-NEXT: ret <2 x i32> [[S]]
838 %s = select i1 %cond, <2 x i32> undef, <2 x i32> %x
842 ; These can be folded because the other value is guaranteed not to be poison.
843 define i32 @false_undef_true_constant(i1 %cond) {
844 ; CHECK-LABEL: @false_undef_true_constant(
845 ; CHECK-NEXT: ret i32 10
847 %s = select i1 %cond, i32 10, i32 undef
851 define i32 @true_undef_false_constant(i1 %cond) {
852 ; CHECK-LABEL: @true_undef_false_constant(
853 ; CHECK-NEXT: ret i32 20
855 %s = select i1 %cond, i32 undef, i32 20
859 define <2 x i32> @false_undef_true_constant_vec(i1 %cond) {
860 ; CHECK-LABEL: @false_undef_true_constant_vec(
861 ; CHECK-NEXT: ret <2 x i32> <i32 42, i32 -42>
863 %s = select i1 %cond, <2 x i32> <i32 42, i32 -42>, <2 x i32> undef
867 define <2 x i32> @true_undef_false_constant_vec(i1 %cond) {
868 ; CHECK-LABEL: @true_undef_false_constant_vec(
869 ; CHECK-NEXT: ret <2 x i32> <i32 -42, i32 42>
871 %s = select i1 %cond, <2 x i32> undef, <2 x i32> <i32 -42, i32 42>
875 ; If one input is undef and the other is freeze, we can fold it to the freeze.
876 define i32 @false_undef_true_freeze(i1 %cond, i32 %x) {
877 ; CHECK-LABEL: @false_undef_true_freeze(
878 ; CHECK-NEXT: [[XF:%.*]] = freeze i32 [[X:%.*]]
879 ; CHECK-NEXT: ret i32 [[XF]]
882 %s = select i1 %cond, i32 %xf, i32 undef
886 define i32 @false_undef_false_freeze(i1 %cond, i32 %x) {
887 ; CHECK-LABEL: @false_undef_false_freeze(
888 ; CHECK-NEXT: [[XF:%.*]] = freeze i32 [[X:%.*]]
889 ; CHECK-NEXT: ret i32 [[XF]]
892 %s = select i1 %cond, i32 undef, i32 %xf
896 @g = external global i32, align 1
898 define <2 x i32> @false_undef_true_constextpr_vec(i1 %cond) {
899 ; CHECK-LABEL: @false_undef_true_constextpr_vec(
900 ; CHECK-NEXT: ret <2 x i32> <i32 ptrtoint (ptr @g to i32), i32 ptrtoint (ptr @g to i32)>
902 %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>
906 define i32 @all_constant_true_undef() {
907 ; CHECK-LABEL: @all_constant_true_undef(
908 ; CHECK-NEXT: ret i32 1
910 %s = select i1 ptrtoint (ptr @all_constant_true_undef to i1), i32 undef, i32 1
914 define float @all_constant_false_undef() {
915 ; CHECK-LABEL: @all_constant_false_undef(
916 ; CHECK-NEXT: ret float 1.000000e+00
918 %s = select i1 ptrtoint (ptr @all_constant_false_undef to i1), float undef, float 1.0
922 define <2 x i32> @all_constant_true_undef_vec() {
923 ; CHECK-LABEL: @all_constant_true_undef_vec(
924 ; CHECK-NEXT: ret <2 x i32> <i32 1, i32 -1>
926 %s = select i1 ptrtoint (ptr @all_constant_true_undef_vec to i1), <2 x i32> undef, <2 x i32> <i32 1, i32 -1>
930 define <2 x float> @all_constant_false_undef_vec() {
931 ; CHECK-LABEL: @all_constant_false_undef_vec(
932 ; CHECK-NEXT: ret <2 x float> <float 1.000000e+00, float -1.000000e+00>
934 %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>
938 ; Negative tests. Don't fold if the non-undef operand is a constexpr.
939 define i32 @all_constant_false_undef_true_constexpr() {
940 ; CHECK-LABEL: @all_constant_false_undef_true_constexpr(
941 ; CHECK-NEXT: ret i32 ptrtoint (ptr @all_constant_false_undef_true_constexpr to i32)
943 %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
947 define i32 @all_constant_true_undef_false_constexpr() {
948 ; CHECK-LABEL: @all_constant_true_undef_false_constexpr(
949 ; CHECK-NEXT: ret i32 ptrtoint (ptr @all_constant_true_undef_false_constexpr to i32)
951 %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)
955 ; Negative tests. Don't fold if the non-undef operand is a vector containing a constexpr.
956 define <2 x i32> @all_constant_false_undef_true_constexpr_vec() {
957 ; CHECK-LABEL: @all_constant_false_undef_true_constexpr_vec(
958 ; CHECK-NEXT: ret <2 x i32> <i32 ptrtoint (ptr @all_constant_false_undef_true_constexpr_vec to i32), i32 -1>
960 %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
964 define <2 x i32> @all_constant_true_undef_false_constexpr_vec() {
965 ; CHECK-LABEL: @all_constant_true_undef_false_constexpr_vec(
966 ; CHECK-NEXT: ret <2 x i32> <i32 -1, i32 ptrtoint (ptr @all_constant_true_undef_false_constexpr_vec to i32)>
968 %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)>
972 define i1 @expand_binop_undef(i32 %x, i32 %y) {
973 ; CHECK-LABEL: @expand_binop_undef(
974 ; CHECK-NEXT: [[CMP15:%.*]] = icmp slt i32 [[X:%.*]], [[Y:%.*]]
975 ; CHECK-NEXT: ret i1 [[CMP15]]
977 %cmp9.not.1 = icmp eq i32 %x, %y
978 %cmp15 = icmp slt i32 %x, %y
979 %spec.select39 = select i1 %cmp9.not.1, i1 undef, i1 %cmp15
980 %spec.select40 = xor i1 %cmp9.not.1, 1
981 %spec.select = and i1 %spec.select39, %spec.select40
985 define i32 @pr47322_more_poisonous_replacement(i32 %arg) {
986 ; CHECK-LABEL: @pr47322_more_poisonous_replacement(
987 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[ARG:%.*]], 0
988 ; CHECK-NEXT: [[TRAILING:%.*]] = call i32 @llvm.cttz.i32(i32 [[ARG]], i1 immarg true)
989 ; CHECK-NEXT: [[SHIFTED:%.*]] = lshr i32 [[ARG]], [[TRAILING]]
990 ; CHECK-NEXT: [[R1_SROA_0_1:%.*]] = select i1 [[CMP]], i32 0, i32 [[SHIFTED]]
991 ; CHECK-NEXT: ret i32 [[R1_SROA_0_1]]
993 %cmp = icmp eq i32 %arg, 0
994 %trailing = call i32 @llvm.cttz.i32(i32 %arg, i1 immarg true)
995 %shifted = lshr i32 %arg, %trailing
996 %r1.sroa.0.1 = select i1 %cmp, i32 0, i32 %shifted
999 declare i32 @llvm.cttz.i32(i32, i1 immarg)
1001 ; Partial undef scalable vectors should be ignored.
1002 define <vscale x 2 x i1> @ignore_scalable_undef(<vscale x 2 x i1> %cond) {
1003 ; CHECK-LABEL: @ignore_scalable_undef(
1004 ; CHECK-NEXT: ret <vscale x 2 x i1> insertelement (<vscale x 2 x i1> undef, i1 true, i32 0)
1006 %vec = insertelement <vscale x 2 x i1> undef, i1 true, i32 0
1007 %s = select <vscale x 2 x i1> %cond, <vscale x 2 x i1> undef, <vscale x 2 x i1> %vec
1008 ret <vscale x 2 x i1> %s
1011 define i32 @select_neutral_add_rhs(i32 %x, i32 %y) {
1012 ; CHECK-LABEL: @select_neutral_add_rhs(
1013 ; CHECK-NEXT: [[ADD:%.*]] = add i32 [[X:%.*]], [[Y:%.*]]
1014 ; CHECK-NEXT: ret i32 [[ADD]]
1016 %cmp = icmp ne i32 %y, 0
1017 %add = add i32 %x, %y
1018 %sel = select i1 %cmp, i32 %add, i32 %x
1022 define i32 @select_neutral_add_lhs(i32 %x, i32 %y) {
1023 ; CHECK-LABEL: @select_neutral_add_lhs(
1024 ; CHECK-NEXT: [[ADD:%.*]] = add i32 [[Y:%.*]], [[X:%.*]]
1025 ; CHECK-NEXT: ret i32 [[ADD]]
1027 %cmp = icmp ne i32 %y, 0
1028 %add = add i32 %y, %x
1029 %sel = select i1 %cmp, i32 %add, i32 %x
1033 define <2 x i32> @select_neutral_add_rhs_vec(<2 x i32> %x, <2 x i32> %y) {
1034 ; CHECK-LABEL: @select_neutral_add_rhs_vec(
1035 ; CHECK-NEXT: [[ADD:%.*]] = add <2 x i32> [[X:%.*]], [[Y:%.*]]
1036 ; CHECK-NEXT: ret <2 x i32> [[ADD]]
1038 %cmp = icmp ne <2 x i32> %y, zeroinitializer
1039 %add = add <2 x i32> %x, %y
1040 %sel = select <2 x i1> %cmp, <2 x i32> %add, <2 x i32> %x
1044 define <2 x i32> @select_neutral_add_lhs_vec(<2 x i32> %x, <2 x i32> %y) {
1045 ; CHECK-LABEL: @select_neutral_add_lhs_vec(
1046 ; CHECK-NEXT: [[ADD:%.*]] = add <2 x i32> [[Y:%.*]], [[X:%.*]]
1047 ; CHECK-NEXT: ret <2 x i32> [[ADD]]
1049 %cmp = icmp ne <2 x i32> %y, zeroinitializer
1050 %add = add <2 x i32> %y, %x
1051 %sel = select <2 x i1> %cmp, <2 x i32> %add, <2 x i32> %x
1055 define i32 @select_neutral_sub_rhs(i32 %x, i32 %y) {
1056 ; CHECK-LABEL: @select_neutral_sub_rhs(
1057 ; CHECK-NEXT: [[ADD:%.*]] = sub i32 [[X:%.*]], [[Y:%.*]]
1058 ; CHECK-NEXT: ret i32 [[ADD]]
1060 %cmp = icmp ne i32 %y, 0
1061 %add = sub i32 %x, %y
1062 %sel = select i1 %cmp, i32 %add, i32 %x
1066 define i32 @select_neutral_sub_lhs(i32 %x, i32 %y) {
1067 ; CHECK-LABEL: @select_neutral_sub_lhs(
1068 ; CHECK-NEXT: [[CMP:%.*]] = icmp ne i32 [[Y:%.*]], 0
1069 ; CHECK-NEXT: [[ADD:%.*]] = sub i32 [[Y]], [[X:%.*]]
1070 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i32 [[ADD]], i32 [[X]]
1071 ; CHECK-NEXT: ret i32 [[SEL]]
1073 %cmp = icmp ne i32 %y, 0
1074 %add = sub i32 %y, %x
1075 %sel = select i1 %cmp, i32 %add, i32 %x
1079 define i32 @select_ctpop_zero(i32 %x) {
1080 ; CHECK-LABEL: @select_ctpop_zero(
1081 ; CHECK-NEXT: [[T1:%.*]] = call i32 @llvm.ctpop.i32(i32 [[X:%.*]])
1082 ; CHECK-NEXT: ret i32 [[T1]]
1084 %t0 = icmp eq i32 %x, 0
1085 %t1 = call i32 @llvm.ctpop.i32(i32 %x)
1086 %sel = select i1 %t0, i32 0, i32 %t1
1090 ; FIXME: This is safe to fold.
1091 define <2 x i32> @select_ctpop_zero_vec(<2 x i32> %x) {
1092 ; CHECK-LABEL: @select_ctpop_zero_vec(
1093 ; CHECK-NEXT: [[T0:%.*]] = icmp eq <2 x i32> [[X:%.*]], zeroinitializer
1094 ; CHECK-NEXT: [[T1:%.*]] = call <2 x i32> @llvm.ctpop.v2i32(<2 x i32> [[X]])
1095 ; CHECK-NEXT: [[SEL:%.*]] = select <2 x i1> [[T0]], <2 x i32> zeroinitializer, <2 x i32> [[T1]]
1096 ; CHECK-NEXT: ret <2 x i32> [[SEL]]
1098 %t0 = icmp eq <2 x i32> %x, zeroinitializer
1099 %t1 = call <2 x i32> @llvm.ctpop.v2i32(<2 x i32> %x)
1100 %sel = select <2 x i1> %t0, <2 x i32> zeroinitializer, <2 x i32> %t1
1104 ; Negative test: Cannot fold due to cross-lane intrinsic.
1105 define <2 x i32> @select_vector_reverse(<2 x i32> %x) {
1106 ; CHECK-LABEL: @select_vector_reverse(
1107 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i32> [[X:%.*]], zeroinitializer
1108 ; CHECK-NEXT: [[REV:%.*]] = call <2 x i32> @llvm.experimental.vector.reverse.v2i32(<2 x i32> [[X]])
1109 ; CHECK-NEXT: [[SEL:%.*]] = select <2 x i1> [[CMP]], <2 x i32> zeroinitializer, <2 x i32> [[REV]]
1110 ; CHECK-NEXT: ret <2 x i32> [[SEL]]
1112 %cmp = icmp eq <2 x i32> %x, zeroinitializer
1113 %rev = call <2 x i32> @llvm.experimental.vector.reverse.v2i32(<2 x i32> %x)
1114 %sel = select <2 x i1> %cmp, <2 x i32> zeroinitializer, <2 x i32> %rev
1118 declare i32 @llvm.ctpop.i32(i32)
1119 declare <2 x i32> @llvm.ctpop.v2i32(<2 x i32>)
1120 declare <2 x i32> @llvm.experimental.vector.reverse.v2i32(<2 x i32>)
1122 define <2 x i32> @vec_select_no_equivalence(<2 x i32> %x, <2 x i32> %y) {
1123 ; CHECK-LABEL: @vec_select_no_equivalence(
1124 ; CHECK-NEXT: [[X10:%.*]] = shufflevector <2 x i32> [[X:%.*]], <2 x i32> undef, <2 x i32> <i32 1, i32 0>
1125 ; CHECK-NEXT: [[COND:%.*]] = icmp eq <2 x i32> [[X]], zeroinitializer
1126 ; CHECK-NEXT: [[S:%.*]] = select <2 x i1> [[COND]], <2 x i32> [[X10]], <2 x i32> zeroinitializer
1127 ; CHECK-NEXT: ret <2 x i32> [[S]]
1129 %x10 = shufflevector <2 x i32> %x, <2 x i32> undef, <2 x i32> <i32 1, i32 0>
1130 %cond = icmp eq <2 x i32> %x, zeroinitializer
1131 %s = select <2 x i1> %cond, <2 x i32> %x10, <2 x i32> zeroinitializer
1135 define i8 @select_eq_xor_recursive(i8 %a, i8 %b) {
1136 ; CHECK-LABEL: @select_eq_xor_recursive(
1137 ; CHECK-NEXT: [[XOR:%.*]] = xor i8 [[A:%.*]], [[B:%.*]]
1138 ; CHECK-NEXT: [[INV:%.*]] = xor i8 [[XOR]], -1
1139 ; CHECK-NEXT: ret i8 [[INV]]
1141 %xor = xor i8 %a, %b
1142 %inv = xor i8 %xor, -1
1143 %cmp = icmp eq i8 %a, %b
1144 %sel = select i1 %cmp, i8 -1, i8 %inv
1148 define i8 @select_eq_xor_recursive2(i8 %a, i8 %b) {
1149 ; CHECK-LABEL: @select_eq_xor_recursive2(
1150 ; CHECK-NEXT: [[XOR:%.*]] = xor i8 [[A:%.*]], [[B:%.*]]
1151 ; CHECK-NEXT: [[INV:%.*]] = xor i8 [[XOR]], -1
1152 ; CHECK-NEXT: [[ADD:%.*]] = add i8 [[INV]], 10
1153 ; CHECK-NEXT: ret i8 [[ADD]]
1155 %xor = xor i8 %a, %b
1156 %inv = xor i8 %xor, -1
1157 %add = add i8 %inv, 10
1158 %cmp = icmp eq i8 %a, %b
1159 %sel = select i1 %cmp, i8 9, i8 %add
1163 define i8 @select_eq_xor_recursive3(i8 %a, i8 %b) {
1164 ; CHECK-LABEL: @select_eq_xor_recursive3(
1165 ; CHECK-NEXT: [[XOR:%.*]] = xor i8 [[A:%.*]], [[B:%.*]]
1166 ; CHECK-NEXT: [[INV:%.*]] = xor i8 [[XOR]], -1
1167 ; CHECK-NEXT: [[ADD:%.*]] = add i8 [[INV]], 10
1168 ; CHECK-NEXT: [[MUL:%.*]] = mul i8 [[ADD]], 3
1169 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[A]], [[B]]
1170 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i8 9, i8 [[MUL]]
1171 ; CHECK-NEXT: ret i8 [[SEL]]
1173 %xor = xor i8 %a, %b
1174 %inv = xor i8 %xor, -1
1175 %add = add i8 %inv, 10
1176 %mul = mul i8 %add, 3
1177 %cmp = icmp eq i8 %a, %b
1178 %sel = select i1 %cmp, i8 9, i8 %mul
1182 ; Cannot drop select, because this would propagate poison from %a.
1183 define i8 @select_eq_xor_recursive_propagates_poison(i8 %a, i8 %b) {
1184 ; CHECK-LABEL: @select_eq_xor_recursive_propagates_poison(
1185 ; CHECK-NEXT: [[XOR1:%.*]] = add i8 [[A:%.*]], [[B:%.*]]
1186 ; CHECK-NEXT: [[XOR2:%.*]] = xor i8 [[A]], [[XOR1]]
1187 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[B]], 0
1188 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i8 0, i8 [[XOR2]]
1189 ; CHECK-NEXT: ret i8 [[SEL]]
1191 %xor1 = add i8 %a, %b
1192 %xor2 = xor i8 %a, %xor1
1193 %cmp = icmp eq i8 %b, 0
1194 %sel = select i1 %cmp, i8 0, i8 %xor2
1198 define i8 @select_eq_and_recursive(i8 %a) {
1199 ; CHECK-LABEL: @select_eq_and_recursive(
1200 ; CHECK-NEXT: [[NEG:%.*]] = sub i8 0, [[A:%.*]]
1201 ; CHECK-NEXT: [[AND:%.*]] = and i8 [[NEG]], [[A]]
1202 ; CHECK-NEXT: [[ADD:%.*]] = add i8 [[AND]], 1
1203 ; CHECK-NEXT: ret i8 [[ADD]]
1206 %and = and i8 %neg, %a
1207 %add = add i8 %and, 1
1208 %cmp = icmp eq i8 %a, 0
1209 %sel = select i1 %cmp, i8 1, i8 %add
1213 ; Cannot drop select, because this would propagate poison from %b.
1214 define i8 @select_eq_and_recursive_propagates_poison(i8 %a, i8 %b) {
1215 ; CHECK-LABEL: @select_eq_and_recursive_propagates_poison(
1216 ; CHECK-NEXT: [[NEG:%.*]] = sub i8 [[B:%.*]], [[A:%.*]]
1217 ; CHECK-NEXT: [[AND:%.*]] = and i8 [[NEG]], [[A]]
1218 ; CHECK-NEXT: [[ADD:%.*]] = add i8 [[AND]], 1
1219 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[A]], 0
1220 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i8 1, i8 [[ADD]]
1221 ; CHECK-NEXT: ret i8 [[SEL]]
1223 %neg = sub i8 %b, %a
1224 %and = and i8 %neg, %a
1225 %add = add i8 %and, 1
1226 %cmp = icmp eq i8 %a, 0
1227 %sel = select i1 %cmp, i8 1, i8 %add
1231 define i8 @select_eq_xor_recursive_allow_refinement(i8 %a, i8 %b) {
1232 ; CHECK-LABEL: @select_eq_xor_recursive_allow_refinement(
1233 ; CHECK-NEXT: ret i8 0
1235 %xor1 = add i8 %a, %b
1236 %xor2 = xor i8 %a, %xor1
1237 %cmp = icmp eq i8 %b, 0
1238 %sel = select i1 %cmp, i8 %xor2, i8 0
1242 define i8 @select_eq_mul_absorber(i8 %x, i8 noundef %y) {
1243 ; CHECK-LABEL: @select_eq_mul_absorber(
1244 ; CHECK-NEXT: [[ADD:%.*]] = add i8 [[X:%.*]], -1
1245 ; CHECK-NEXT: [[MUL:%.*]] = mul i8 [[ADD]], [[Y:%.*]]
1246 ; CHECK-NEXT: ret i8 [[MUL]]
1248 %cmp = icmp eq i8 %x, 1
1249 %add = add i8 %x, -1
1250 %mul = mul i8 %add, %y
1251 %sel = select i1 %cmp, i8 0, i8 %mul
1255 define i8 @select_eq_mul_not_absorber(i8 %x, i8 noundef %y) {
1256 ; CHECK-LABEL: @select_eq_mul_not_absorber(
1257 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[X:%.*]], 0
1258 ; CHECK-NEXT: [[ADD:%.*]] = add i8 [[X]], -1
1259 ; CHECK-NEXT: [[MUL:%.*]] = mul i8 [[ADD]], [[Y:%.*]]
1260 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i8 0, i8 [[MUL]]
1261 ; CHECK-NEXT: ret i8 [[SEL]]
1263 %cmp = icmp eq i8 %x, 0
1264 %add = add i8 %x, -1
1265 %mul = mul i8 %add, %y
1266 %sel = select i1 %cmp, i8 0, i8 %mul
1270 ; Vector to scalar options should be treated as lane-crossing.
1271 define <2 x i8> @select_eq_vector_insert_extract(<2 x i8> %a, <2 x i8> %b) {
1272 ; CHECK-LABEL: @select_eq_vector_insert_extract(
1273 ; CHECK-NEXT: [[EXTRACT0:%.*]] = extractelement <2 x i8> [[A:%.*]], i64 0
1274 ; CHECK-NEXT: [[EXTRACT1:%.*]] = extractelement <2 x i8> [[A]], i64 1
1275 ; CHECK-NEXT: [[INSERT0:%.*]] = insertelement <2 x i8> poison, i8 [[EXTRACT1]], i64 0
1276 ; CHECK-NEXT: [[INSERT1:%.*]] = insertelement <2 x i8> [[INSERT0]], i8 [[EXTRACT0]], i64 1
1277 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i8> [[A]], zeroinitializer
1278 ; CHECK-NEXT: [[SEL:%.*]] = select <2 x i1> [[CMP]], <2 x i8> [[INSERT1]], <2 x i8> zeroinitializer
1279 ; CHECK-NEXT: ret <2 x i8> [[SEL]]
1281 %extract0 = extractelement <2 x i8> %a, i64 0
1282 %extract1 = extractelement <2 x i8> %a, i64 1
1283 %insert0 = insertelement <2 x i8> poison, i8 %extract1, i64 0
1284 %insert1 = insertelement <2 x i8> %insert0, i8 %extract0, i64 1
1285 %cmp = icmp eq <2 x i8> %a, zeroinitializer
1286 %sel = select <2 x i1> %cmp, <2 x i8> %insert1, <2 x i8> zeroinitializer
1290 define i32 @poison(i32 %x, i32 %y) {
1291 ; CHECK-LABEL: @poison(
1292 ; CHECK-NEXT: ret i32 [[X:%.*]]
1294 %v = select i1 undef, i32 %x, i32 %y
1298 define i32 @poison2(i1 %cond, i32 %x) {
1299 ; CHECK-LABEL: @poison2(
1300 ; CHECK-NEXT: ret i32 [[X:%.*]]
1302 %v = select i1 %cond, i32 poison, i32 %x
1306 define i32 @poison3(i1 %cond, i32 %x) {
1307 ; CHECK-LABEL: @poison3(
1308 ; CHECK-NEXT: ret i32 [[X:%.*]]
1310 %v = select i1 %cond, i32 %x, i32 poison
1314 define <2 x i32> @poison4(<2 x i1> %cond, <2 x i32> %x) {
1315 ; CHECK-LABEL: @poison4(
1316 ; CHECK-NEXT: ret <2 x i32> [[X:%.*]]
1318 %v = select <2 x i1> %cond, <2 x i32> %x, <2 x i32> poison
1322 ; 0 is the absorber constant for 'and'.
1323 ; The 'select' can't block extra poison because both sides of 'and' have 'x' operand.
1325 define i8 @replace_false_op_eq_neg_and(i8 %x) {
1326 ; CHECK-LABEL: @replace_false_op_eq_neg_and(
1327 ; CHECK-NEXT: [[NEG:%.*]] = sub i8 0, [[X:%.*]]
1328 ; CHECK-NEXT: [[AND:%.*]] = and i8 [[NEG]], [[X]]
1329 ; CHECK-NEXT: ret i8 [[AND]]
1331 %eq0 = icmp eq i8 %x, 0
1333 %and = and i8 %neg, %x
1334 %sel = select i1 %eq0, i8 0, i8 %and
1338 ; same as above, but commute 'and'
1340 define i8 @replace_false_op_eq_neg_and_commute(i8 %x) {
1341 ; CHECK-LABEL: @replace_false_op_eq_neg_and_commute(
1342 ; CHECK-NEXT: [[NEG:%.*]] = sub i8 0, [[X:%.*]]
1343 ; CHECK-NEXT: [[AND:%.*]] = and i8 [[X]], [[NEG]]
1344 ; CHECK-NEXT: ret i8 [[AND]]
1346 %eq0 = icmp eq i8 %x, 0
1348 %and = and i8 %x, %neg
1349 %sel = select i1 %eq0, i8 0, i8 %and
1353 ; same as above, but swap 'select'
1355 define i8 @replace_false_op_ne_neg_and(i8 %x) {
1356 ; CHECK-LABEL: @replace_false_op_ne_neg_and(
1357 ; CHECK-NEXT: [[NEG:%.*]] = sub i8 0, [[X:%.*]]
1358 ; CHECK-NEXT: [[AND:%.*]] = and i8 [[NEG]], [[X]]
1359 ; CHECK-NEXT: ret i8 [[AND]]
1361 %ne0 = icmp ne i8 %x, 0
1363 %and = and i8 %neg, %x
1364 %sel = select i1 %ne0, i8 %and, i8 0
1368 ; same as above, but commute 'and' and swap 'select'
1370 define i8 @replace_false_op_ne_neg_and_commute(i8 %x) {
1371 ; CHECK-LABEL: @replace_false_op_ne_neg_and_commute(
1372 ; CHECK-NEXT: [[NEG:%.*]] = sub i8 0, [[X:%.*]]
1373 ; CHECK-NEXT: [[AND:%.*]] = and i8 [[X]], [[NEG]]
1374 ; CHECK-NEXT: ret i8 [[AND]]
1376 %ne0 = icmp ne i8 %x, 0
1378 %and = and i8 %x, %neg
1379 %sel = select i1 %ne0, i8 %and, i8 0
1383 ; the first binop can be anything as long as it has the common operand
1385 define i8 @replace_false_op_eq_dec_and(i8 %x) {
1386 ; CHECK-LABEL: @replace_false_op_eq_dec_and(
1387 ; CHECK-NEXT: [[DEC:%.*]] = add i8 [[X:%.*]], -1
1388 ; CHECK-NEXT: [[AND:%.*]] = and i8 [[DEC]], [[X]]
1389 ; CHECK-NEXT: ret i8 [[AND]]
1391 %eq0 = icmp eq i8 %x, 0
1392 %dec = add i8 %x, -1
1393 %and = and i8 %dec, %x
1394 %sel = select i1 %eq0, i8 0, i8 %and
1398 ; mul has the same absorber constant - "0"
1400 define i8 @replace_false_op_eq_add_mul(i8 %x) {
1401 ; CHECK-LABEL: @replace_false_op_eq_add_mul(
1402 ; CHECK-NEXT: [[ADD:%.*]] = add i8 [[X:%.*]], 42
1403 ; CHECK-NEXT: [[MUL:%.*]] = mul i8 [[ADD]], [[X]]
1404 ; CHECK-NEXT: ret i8 [[MUL]]
1406 %eq0 = icmp eq i8 %x, 0
1407 %add = add i8 %x, 42
1408 %mul = mul i8 %add, %x
1409 %sel = select i1 %eq0, i8 0, i8 %mul
1413 ; or has a different absorber constant = "-1"
1415 define i8 @replace_false_op_eq_shl_or(i8 %x) {
1416 ; CHECK-LABEL: @replace_false_op_eq_shl_or(
1417 ; CHECK-NEXT: [[SHL:%.*]] = shl i8 [[X:%.*]], 3
1418 ; CHECK-NEXT: [[OR:%.*]] = or i8 [[X]], [[SHL]]
1419 ; CHECK-NEXT: ret i8 [[OR]]
1421 %eq0 = icmp eq i8 %x, -1
1423 %or = or i8 %x, %shl
1424 %sel = select i1 %eq0, i8 -1, i8 %or
1428 define i8 @replace_false_op_eq_shl_or_disjoint(i8 %x) {
1429 ; CHECK-LABEL: @replace_false_op_eq_shl_or_disjoint(
1430 ; CHECK-NEXT: [[EQ0:%.*]] = icmp eq i8 [[X:%.*]], -1
1431 ; CHECK-NEXT: [[SHL:%.*]] = shl i8 [[X]], 3
1432 ; CHECK-NEXT: [[OR:%.*]] = or disjoint i8 [[X]], [[SHL]]
1433 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[EQ0]], i8 -1, i8 [[OR]]
1434 ; CHECK-NEXT: ret i8 [[SEL]]
1436 %eq0 = icmp eq i8 %x, -1
1438 %or = or disjoint i8 %x, %shl
1439 %sel = select i1 %eq0, i8 -1, i8 %or
1443 ; negative test - wrong cmp predicate
1445 define i8 @replace_false_op_sgt_neg_and(i8 %x) {
1446 ; CHECK-LABEL: @replace_false_op_sgt_neg_and(
1447 ; CHECK-NEXT: [[EQ0:%.*]] = icmp sgt i8 [[X:%.*]], 0
1448 ; CHECK-NEXT: [[NEG:%.*]] = sub i8 0, [[X]]
1449 ; CHECK-NEXT: [[AND:%.*]] = and i8 [[NEG]], [[X]]
1450 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[EQ0]], i8 0, i8 [[AND]]
1451 ; CHECK-NEXT: ret i8 [[SEL]]
1453 %eq0 = icmp sgt i8 %x, 0
1455 %and = and i8 %neg, %x
1456 %sel = select i1 %eq0, i8 0, i8 %and
1460 ; negative test - the binop must use a compare operand
1462 define i8 @replace_false_op_eq_shl_or_wrong_cmp_op(i8 %x, i8 %y) {
1463 ; CHECK-LABEL: @replace_false_op_eq_shl_or_wrong_cmp_op(
1464 ; CHECK-NEXT: [[EQ0:%.*]] = icmp eq i8 [[Y:%.*]], -1
1465 ; CHECK-NEXT: [[SHL:%.*]] = shl i8 [[X:%.*]], 3
1466 ; CHECK-NEXT: [[OR:%.*]] = or i8 [[X]], [[SHL]]
1467 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[EQ0]], i8 -1, i8 [[OR]]
1468 ; CHECK-NEXT: ret i8 [[SEL]]
1470 %eq0 = icmp eq i8 %y, -1
1472 %or = or i8 %x, %shl
1473 %sel = select i1 %eq0, i8 -1, i8 %or
1477 ; negative test - can't have extra source of potential poison
1479 define i8 @replace_false_op_eq_neg_and_leak1(i8 %x, i8 %y) {
1480 ; CHECK-LABEL: @replace_false_op_eq_neg_and_leak1(
1481 ; CHECK-NEXT: [[EQ0:%.*]] = icmp eq i8 [[X:%.*]], 0
1482 ; CHECK-NEXT: [[NEG:%.*]] = sub i8 0, [[Y:%.*]]
1483 ; CHECK-NEXT: [[AND:%.*]] = and i8 [[NEG]], [[X]]
1484 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[EQ0]], i8 0, i8 [[AND]]
1485 ; CHECK-NEXT: ret i8 [[SEL]]
1487 %eq0 = icmp eq i8 %x, 0
1489 %and = and i8 %neg, %x
1490 %sel = select i1 %eq0, i8 0, i8 %and
1494 ; negative test - can't have extra source of potential poison
1496 define i8 @replace_false_op_eq_neg_and_leak2(i8 %x, i8 %y) {
1497 ; CHECK-LABEL: @replace_false_op_eq_neg_and_leak2(
1498 ; CHECK-NEXT: [[EQ0:%.*]] = icmp eq i8 [[X:%.*]], 0
1499 ; CHECK-NEXT: [[NEG:%.*]] = sub i8 0, [[X]]
1500 ; CHECK-NEXT: [[AND:%.*]] = and i8 [[NEG]], [[Y:%.*]]
1501 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[EQ0]], i8 0, i8 [[AND]]
1502 ; CHECK-NEXT: ret i8 [[SEL]]
1504 %eq0 = icmp eq i8 %x, 0
1506 %and = and i8 %neg, %y
1507 %sel = select i1 %eq0, i8 0, i8 %and
1511 ; negative test - can't have extra source of potential poison
1513 define i8 @replace_false_op_eq_add_mul_leak3(i8 %x, i8 %y) {
1514 ; CHECK-LABEL: @replace_false_op_eq_add_mul_leak3(
1515 ; CHECK-NEXT: [[EQ0:%.*]] = icmp eq i8 [[X:%.*]], 0
1516 ; CHECK-NEXT: [[ADD:%.*]] = add i8 [[X]], [[Y:%.*]]
1517 ; CHECK-NEXT: [[MUL:%.*]] = mul i8 [[ADD]], [[X]]
1518 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[EQ0]], i8 0, i8 [[MUL]]
1519 ; CHECK-NEXT: ret i8 [[SEL]]
1521 %eq0 = icmp eq i8 %x, 0
1522 %add = add i8 %x, %y
1523 %mul = mul i8 %add, %x
1524 %sel = select i1 %eq0, i8 0, i8 %mul
1528 ; negative test - can't have extra source of potential poison
1530 define i8 @replace_false_op_eq_shl_or_leak4(i8 %x, i8 %y) {
1531 ; CHECK-LABEL: @replace_false_op_eq_shl_or_leak4(
1532 ; CHECK-NEXT: [[EQ0:%.*]] = icmp eq i8 [[X:%.*]], -1
1533 ; CHECK-NEXT: [[SHL:%.*]] = shl i8 [[Y:%.*]], [[X]]
1534 ; CHECK-NEXT: [[OR:%.*]] = or i8 [[X]], [[SHL]]
1535 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[EQ0]], i8 -1, i8 [[OR]]
1536 ; CHECK-NEXT: ret i8 [[SEL]]
1538 %eq0 = icmp eq i8 %x, -1
1539 %shl = shl i8 %y, %x
1540 %or = or i8 %x, %shl
1541 %sel = select i1 %eq0, i8 -1, i8 %or
1545 ; negative test - wrong cmp constant
1547 define i8 @replace_false_op_eq42_neg_and(i8 %x) {
1548 ; CHECK-LABEL: @replace_false_op_eq42_neg_and(
1549 ; CHECK-NEXT: [[EQ42:%.*]] = icmp eq i8 [[X:%.*]], 42
1550 ; CHECK-NEXT: [[NEG:%.*]] = sub i8 0, [[X]]
1551 ; CHECK-NEXT: [[AND:%.*]] = and i8 [[NEG]], [[X]]
1552 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[EQ42]], i8 0, i8 [[AND]]
1553 ; CHECK-NEXT: ret i8 [[SEL]]
1555 %eq42 = icmp eq i8 %x, 42
1557 %and = and i8 %neg, %x
1558 %sel = select i1 %eq42, i8 0, i8 %and
1562 define ptr @select_op_replacement_in_phi(ptr %head) {
1563 ; CHECK-LABEL: @select_op_replacement_in_phi(
1564 ; CHECK-NEXT: entry:
1565 ; CHECK-NEXT: br label [[LOOP:%.*]]
1567 ; CHECK-NEXT: [[CURRENT:%.*]] = phi ptr [ [[HEAD:%.*]], [[ENTRY:%.*]] ], [ [[NEXT:%.*]], [[LATCH:%.*]] ]
1568 ; CHECK-NEXT: [[PREV:%.*]] = phi ptr [ null, [[ENTRY]] ], [ [[CURRENT]], [[LATCH]] ]
1569 ; CHECK-NEXT: [[CURRENT_NULL:%.*]] = icmp eq ptr [[CURRENT]], null
1570 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CURRENT_NULL]], ptr [[PREV]], ptr null
1571 ; CHECK-NEXT: br i1 [[CURRENT_NULL]], label [[EXIT:%.*]], label [[LATCH]]
1573 ; CHECK-NEXT: [[NEXT]] = load ptr, ptr [[CURRENT]], align 8
1574 ; CHECK-NEXT: br label [[LOOP]]
1576 ; CHECK-NEXT: ret ptr [[SEL]]
1582 %current = phi ptr [ %head, %entry ], [ %next, %latch ]
1583 %prev = phi ptr [ null, %entry ], [ %current, %latch ]
1584 %current.null = icmp eq ptr %current, null
1585 %sel = select i1 %current.null, ptr %prev, ptr null
1586 br i1 %current.null, label %exit, label %latch
1589 %next = load ptr, ptr %current
1596 define i8 @select_sub_cmp(i8 %0, i8 %1) {
1597 ; CHECK-LABEL: @select_sub_cmp(
1598 ; CHECK-NEXT: [[TMP3:%.*]] = sub nsw i8 [[TMP1:%.*]], [[TMP0:%.*]]
1599 ; CHECK-NEXT: ret i8 [[TMP3]]
1601 %3 = icmp eq i8 %1, %0
1602 %4 = sub nsw i8 %1, %0
1603 %5 = select i1 %3, i8 0, i8 %4
1607 define <2 x i8> @select_sub_cmp_vec(<2 x i8> %0, <2 x i8> %1) {
1608 ; CHECK-LABEL: @select_sub_cmp_vec(
1609 ; CHECK-NEXT: [[TMP3:%.*]] = sub nsw <2 x i8> [[TMP1:%.*]], [[TMP0:%.*]]
1610 ; CHECK-NEXT: ret <2 x i8> [[TMP3]]
1612 %3 = icmp eq <2 x i8> %1, %0
1613 %4 = sub nsw <2 x i8> %1, %0
1614 %5 = select <2 x i1> %3, <2 x i8> <i8 0, i8 0>, <2 x i8> %4
1618 define i8 @select_sub_cmp_swap(i8 %0, i8 %1) {
1619 ; CHECK-LABEL: @select_sub_cmp_swap(
1620 ; CHECK-NEXT: [[TMP3:%.*]] = sub nsw i8 [[TMP0:%.*]], [[TMP1:%.*]]
1621 ; CHECK-NEXT: ret i8 [[TMP3]]
1623 %3 = icmp eq i8 %1, %0
1624 %4 = sub nsw i8 %0, %1
1625 %5 = select i1 %3, i8 0, i8 %4
1629 define <2 x i8> @select_sub_cmp_vec_swap(<2 x i8> %0, <2 x i8> %1) {
1630 ; CHECK-LABEL: @select_sub_cmp_vec_swap(
1631 ; CHECK-NEXT: [[TMP3:%.*]] = sub nsw <2 x i8> [[TMP0:%.*]], [[TMP1:%.*]]
1632 ; CHECK-NEXT: ret <2 x i8> [[TMP3]]
1634 %3 = icmp eq <2 x i8> %1, %0
1635 %4 = sub nsw <2 x i8> %0, %1
1636 %5 = select <2 x i1> %3, <2 x i8> <i8 0, i8 0>, <2 x i8> %4
1641 define i8 @select_sub_cmp_nonzero(i8 %0, i8 %1) {
1642 ; CHECK-LABEL: @select_sub_cmp_nonzero(
1643 ; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i8 [[TMP1:%.*]], [[TMP0:%.*]]
1644 ; CHECK-NEXT: [[TMP4:%.*]] = sub nsw i8 [[TMP1]], [[TMP0]]
1645 ; CHECK-NEXT: [[TMP5:%.*]] = select i1 [[TMP3]], i8 42, i8 [[TMP4]]
1646 ; CHECK-NEXT: ret i8 [[TMP5]]
1648 %3 = icmp eq i8 %1, %0
1649 %4 = sub nsw i8 %1, %0
1650 %5 = select i1 %3, i8 42, i8 %4
1654 ; X == Y ? 0 : X ^ Y --> X ^ Y, https://alive2.llvm.org/ce/z/cykffE
1655 define i8 @select_xor_cmp(i8 %0, i8 %1) {
1656 ; CHECK-LABEL: @select_xor_cmp(
1657 ; CHECK-NEXT: [[TMP3:%.*]] = xor i8 [[TMP1:%.*]], [[TMP0:%.*]]
1658 ; CHECK-NEXT: ret i8 [[TMP3]]
1660 %3 = icmp eq i8 %1, %0
1662 %5 = select i1 %3, i8 0, i8 %4
1666 define <2 x i8> @select_xor_cmp_vec(<2 x i8> %0, <2 x i8> %1) {
1667 ; CHECK-LABEL: @select_xor_cmp_vec(
1668 ; CHECK-NEXT: [[TMP3:%.*]] = xor <2 x i8> [[TMP1:%.*]], [[TMP0:%.*]]
1669 ; CHECK-NEXT: ret <2 x i8> [[TMP3]]
1671 %3 = icmp eq <2 x i8> %1, %0
1672 %4 = xor <2 x i8> %1, %0
1673 %5 = select <2 x i1> %3, <2 x i8> <i8 0, i8 0>, <2 x i8> %4
1677 define i8 @select_xor_cmp_swap(i8 %0, i8 %1) {
1678 ; CHECK-LABEL: @select_xor_cmp_swap(
1679 ; CHECK-NEXT: [[TMP3:%.*]] = xor i8 [[TMP0:%.*]], [[TMP1:%.*]]
1680 ; CHECK-NEXT: ret i8 [[TMP3]]
1682 %3 = icmp eq i8 %1, %0
1684 %5 = select i1 %3, i8 0, i8 %4
1688 define <2 x i8> @select_xor_cmp_vec_swap(<2 x i8> %0, <2 x i8> %1) {
1689 ; CHECK-LABEL: @select_xor_cmp_vec_swap(
1690 ; CHECK-NEXT: [[TMP3:%.*]] = xor <2 x i8> [[TMP0:%.*]], [[TMP1:%.*]]
1691 ; CHECK-NEXT: ret <2 x i8> [[TMP3]]
1693 %3 = icmp eq <2 x i8> %1, %0
1694 %4 = xor <2 x i8> %0, %1
1695 %5 = select <2 x i1> %3, <2 x i8> <i8 0, i8 0>, <2 x i8> %4
1699 ; Negative test: the xor operands are not %0 and %1
1700 define i8 @select_xor_cmp_unmatched_operands(i8 %0, i8 %1, i8 %c) {
1701 ; CHECK-LABEL: @select_xor_cmp_unmatched_operands(
1702 ; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i8 [[TMP1:%.*]], [[TMP0:%.*]]
1703 ; CHECK-NEXT: [[TMP4:%.*]] = xor i8 [[TMP1]], [[C:%.*]]
1704 ; CHECK-NEXT: [[TMP5:%.*]] = select i1 [[TMP3]], i8 0, i8 [[TMP4]]
1705 ; CHECK-NEXT: ret i8 [[TMP5]]
1707 %3 = icmp eq i8 %1, %0
1709 %5 = select i1 %3, i8 0, i8 %4
1713 define i8 @select_or_eq(i8 %x, i8 %y) {
1714 ; CHECK-LABEL: @select_or_eq(
1715 ; CHECK-NEXT: [[OR:%.*]] = or i8 [[X:%.*]], [[Y:%.*]]
1716 ; CHECK-NEXT: ret i8 [[OR]]
1718 %cmp = icmp eq i8 %x, %y
1720 %sel = select i1 %cmp, i8 %x, i8 %or
1724 define i8 @select_or_disjoint_eq(i8 %x, i8 %y) {
1725 ; CHECK-LABEL: @select_or_disjoint_eq(
1726 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[X:%.*]], [[Y:%.*]]
1727 ; CHECK-NEXT: [[OR:%.*]] = or disjoint i8 [[X]], [[Y]]
1728 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i8 [[X]], i8 [[OR]]
1729 ; CHECK-NEXT: ret i8 [[SEL]]
1731 %cmp = icmp eq i8 %x, %y
1732 %or = or disjoint i8 %x, %y
1733 %sel = select i1 %cmp, i8 %x, i8 %or
1737 define <4 x i32> @select_vector_cmp_with_bitcasts(<2 x i64> %x, <4 x i32> %y) {
1738 ; CHECK-LABEL: @select_vector_cmp_with_bitcasts(
1739 ; CHECK-NEXT: [[X_BC:%.*]] = bitcast <2 x i64> [[X:%.*]] to <4 x i32>
1740 ; CHECK-NEXT: [[Y_BC:%.*]] = bitcast <4 x i32> [[Y:%.*]] to <2 x i64>
1741 ; CHECK-NEXT: [[SUB:%.*]] = sub <2 x i64> [[X]], [[Y_BC]]
1742 ; CHECK-NEXT: [[SUB_BC:%.*]] = bitcast <2 x i64> [[SUB]] to <4 x i32>
1743 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq <4 x i32> [[Y]], [[X_BC]]
1744 ; CHECK-NEXT: [[SEL:%.*]] = select <4 x i1> [[CMP]], <4 x i32> [[SUB_BC]], <4 x i32> zeroinitializer
1745 ; CHECK-NEXT: ret <4 x i32> [[SEL]]
1747 %x.bc = bitcast <2 x i64> %x to <4 x i32>
1748 %y.bc = bitcast <4 x i32> %y to <2 x i64>
1749 %sub = sub <2 x i64> %x, %y.bc
1750 %sub.bc = bitcast <2 x i64> %sub to <4 x i32>
1751 %cmp = icmp eq <4 x i32> %y, %x.bc
1752 %sel = select <4 x i1> %cmp, <4 x i32> %sub.bc, <4 x i32> zeroinitializer