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: [[CMP9_NOT_1:%.*]] = icmp eq i32 [[X:%.*]], [[Y:%.*]]
975 ; CHECK-NEXT: [[CMP15:%.*]] = icmp slt i32 [[X]], [[Y]]
976 ; CHECK-NEXT: [[SPEC_SELECT39:%.*]] = select i1 [[CMP9_NOT_1]], i1 undef, i1 [[CMP15]]
977 ; CHECK-NEXT: [[SPEC_SELECT40:%.*]] = xor i1 [[CMP9_NOT_1]], true
978 ; CHECK-NEXT: [[SPEC_SELECT:%.*]] = and i1 [[SPEC_SELECT39]], [[SPEC_SELECT40]]
979 ; CHECK-NEXT: ret i1 [[SPEC_SELECT]]
981 %cmp9.not.1 = icmp eq i32 %x, %y
982 %cmp15 = icmp slt i32 %x, %y
983 %spec.select39 = select i1 %cmp9.not.1, i1 undef, i1 %cmp15
984 %spec.select40 = xor i1 %cmp9.not.1, 1
985 %spec.select = and i1 %spec.select39, %spec.select40
989 define i32 @pr47322_more_poisonous_replacement(i32 %arg) {
990 ; CHECK-LABEL: @pr47322_more_poisonous_replacement(
991 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[ARG:%.*]], 0
992 ; CHECK-NEXT: [[TRAILING:%.*]] = call i32 @llvm.cttz.i32(i32 [[ARG]], i1 immarg true)
993 ; CHECK-NEXT: [[SHIFTED:%.*]] = lshr i32 [[ARG]], [[TRAILING]]
994 ; CHECK-NEXT: [[R1_SROA_0_1:%.*]] = select i1 [[CMP]], i32 0, i32 [[SHIFTED]]
995 ; CHECK-NEXT: ret i32 [[R1_SROA_0_1]]
997 %cmp = icmp eq i32 %arg, 0
998 %trailing = call i32 @llvm.cttz.i32(i32 %arg, i1 immarg true)
999 %shifted = lshr i32 %arg, %trailing
1000 %r1.sroa.0.1 = select i1 %cmp, i32 0, i32 %shifted
1001 ret i32 %r1.sroa.0.1
1003 declare i32 @llvm.cttz.i32(i32, i1 immarg)
1005 ; Partial undef scalable vectors should be ignored.
1006 define <vscale x 2 x i1> @ignore_scalable_undef(<vscale x 2 x i1> %cond) {
1007 ; CHECK-LABEL: @ignore_scalable_undef(
1008 ; CHECK-NEXT: ret <vscale x 2 x i1> insertelement (<vscale x 2 x i1> undef, i1 true, i32 0)
1010 %vec = insertelement <vscale x 2 x i1> undef, i1 true, i32 0
1011 %s = select <vscale x 2 x i1> %cond, <vscale x 2 x i1> undef, <vscale x 2 x i1> %vec
1012 ret <vscale x 2 x i1> %s
1015 define i32 @select_neutral_add_rhs(i32 %x, i32 %y) {
1016 ; CHECK-LABEL: @select_neutral_add_rhs(
1017 ; CHECK-NEXT: [[ADD:%.*]] = add i32 [[X:%.*]], [[Y:%.*]]
1018 ; CHECK-NEXT: ret i32 [[ADD]]
1020 %cmp = icmp ne i32 %y, 0
1021 %add = add i32 %x, %y
1022 %sel = select i1 %cmp, i32 %add, i32 %x
1026 define i32 @select_neutral_add_lhs(i32 %x, i32 %y) {
1027 ; CHECK-LABEL: @select_neutral_add_lhs(
1028 ; CHECK-NEXT: [[ADD:%.*]] = add i32 [[Y:%.*]], [[X:%.*]]
1029 ; CHECK-NEXT: ret i32 [[ADD]]
1031 %cmp = icmp ne i32 %y, 0
1032 %add = add i32 %y, %x
1033 %sel = select i1 %cmp, i32 %add, i32 %x
1037 define <2 x i32> @select_neutral_add_rhs_vec(<2 x i32> %x, <2 x i32> %y) {
1038 ; CHECK-LABEL: @select_neutral_add_rhs_vec(
1039 ; CHECK-NEXT: [[ADD:%.*]] = add <2 x i32> [[X:%.*]], [[Y:%.*]]
1040 ; CHECK-NEXT: ret <2 x i32> [[ADD]]
1042 %cmp = icmp ne <2 x i32> %y, zeroinitializer
1043 %add = add <2 x i32> %x, %y
1044 %sel = select <2 x i1> %cmp, <2 x i32> %add, <2 x i32> %x
1048 define <2 x i32> @select_neutral_add_lhs_vec(<2 x i32> %x, <2 x i32> %y) {
1049 ; CHECK-LABEL: @select_neutral_add_lhs_vec(
1050 ; CHECK-NEXT: [[ADD:%.*]] = add <2 x i32> [[Y:%.*]], [[X:%.*]]
1051 ; CHECK-NEXT: ret <2 x i32> [[ADD]]
1053 %cmp = icmp ne <2 x i32> %y, zeroinitializer
1054 %add = add <2 x i32> %y, %x
1055 %sel = select <2 x i1> %cmp, <2 x i32> %add, <2 x i32> %x
1059 define i32 @select_neutral_sub_rhs(i32 %x, i32 %y) {
1060 ; CHECK-LABEL: @select_neutral_sub_rhs(
1061 ; CHECK-NEXT: [[ADD:%.*]] = sub i32 [[X:%.*]], [[Y:%.*]]
1062 ; CHECK-NEXT: ret i32 [[ADD]]
1064 %cmp = icmp ne i32 %y, 0
1065 %add = sub i32 %x, %y
1066 %sel = select i1 %cmp, i32 %add, i32 %x
1070 define i32 @select_neutral_sub_lhs(i32 %x, i32 %y) {
1071 ; CHECK-LABEL: @select_neutral_sub_lhs(
1072 ; CHECK-NEXT: [[CMP:%.*]] = icmp ne i32 [[Y:%.*]], 0
1073 ; CHECK-NEXT: [[ADD:%.*]] = sub i32 [[Y]], [[X:%.*]]
1074 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i32 [[ADD]], i32 [[X]]
1075 ; CHECK-NEXT: ret i32 [[SEL]]
1077 %cmp = icmp ne i32 %y, 0
1078 %add = sub i32 %y, %x
1079 %sel = select i1 %cmp, i32 %add, i32 %x
1083 define i32 @select_ctpop_zero(i32 %x) {
1084 ; CHECK-LABEL: @select_ctpop_zero(
1085 ; CHECK-NEXT: [[T1:%.*]] = call i32 @llvm.ctpop.i32(i32 [[X:%.*]])
1086 ; CHECK-NEXT: ret i32 [[T1]]
1088 %t0 = icmp eq i32 %x, 0
1089 %t1 = call i32 @llvm.ctpop.i32(i32 %x)
1090 %sel = select i1 %t0, i32 0, i32 %t1
1094 ; FIXME: This is safe to fold.
1095 define <2 x i32> @select_ctpop_zero_vec(<2 x i32> %x) {
1096 ; CHECK-LABEL: @select_ctpop_zero_vec(
1097 ; CHECK-NEXT: [[T0:%.*]] = icmp eq <2 x i32> [[X:%.*]], zeroinitializer
1098 ; CHECK-NEXT: [[T1:%.*]] = call <2 x i32> @llvm.ctpop.v2i32(<2 x i32> [[X]])
1099 ; CHECK-NEXT: [[SEL:%.*]] = select <2 x i1> [[T0]], <2 x i32> zeroinitializer, <2 x i32> [[T1]]
1100 ; CHECK-NEXT: ret <2 x i32> [[SEL]]
1102 %t0 = icmp eq <2 x i32> %x, zeroinitializer
1103 %t1 = call <2 x i32> @llvm.ctpop.v2i32(<2 x i32> %x)
1104 %sel = select <2 x i1> %t0, <2 x i32> zeroinitializer, <2 x i32> %t1
1108 ; Negative test: Cannot fold due to cross-lane intrinsic.
1109 define <2 x i32> @select_vector_reverse(<2 x i32> %x) {
1110 ; CHECK-LABEL: @select_vector_reverse(
1111 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i32> [[X:%.*]], zeroinitializer
1112 ; CHECK-NEXT: [[REV:%.*]] = call <2 x i32> @llvm.experimental.vector.reverse.v2i32(<2 x i32> [[X]])
1113 ; CHECK-NEXT: [[SEL:%.*]] = select <2 x i1> [[CMP]], <2 x i32> zeroinitializer, <2 x i32> [[REV]]
1114 ; CHECK-NEXT: ret <2 x i32> [[SEL]]
1116 %cmp = icmp eq <2 x i32> %x, zeroinitializer
1117 %rev = call <2 x i32> @llvm.experimental.vector.reverse.v2i32(<2 x i32> %x)
1118 %sel = select <2 x i1> %cmp, <2 x i32> zeroinitializer, <2 x i32> %rev
1122 declare i32 @llvm.ctpop.i32(i32)
1123 declare <2 x i32> @llvm.ctpop.v2i32(<2 x i32>)
1124 declare <2 x i32> @llvm.experimental.vector.reverse.v2i32(<2 x i32>)
1126 define <2 x i32> @vec_select_no_equivalence(<2 x i32> %x, <2 x i32> %y) {
1127 ; CHECK-LABEL: @vec_select_no_equivalence(
1128 ; CHECK-NEXT: [[X10:%.*]] = shufflevector <2 x i32> [[X:%.*]], <2 x i32> undef, <2 x i32> <i32 1, i32 0>
1129 ; CHECK-NEXT: [[COND:%.*]] = icmp eq <2 x i32> [[X]], zeroinitializer
1130 ; CHECK-NEXT: [[S:%.*]] = select <2 x i1> [[COND]], <2 x i32> [[X10]], <2 x i32> zeroinitializer
1131 ; CHECK-NEXT: ret <2 x i32> [[S]]
1133 %x10 = shufflevector <2 x i32> %x, <2 x i32> undef, <2 x i32> <i32 1, i32 0>
1134 %cond = icmp eq <2 x i32> %x, zeroinitializer
1135 %s = select <2 x i1> %cond, <2 x i32> %x10, <2 x i32> zeroinitializer
1139 define i8 @select_eq_xor_recursive(i8 %a, i8 %b) {
1140 ; CHECK-LABEL: @select_eq_xor_recursive(
1141 ; CHECK-NEXT: [[XOR:%.*]] = xor i8 [[A:%.*]], [[B:%.*]]
1142 ; CHECK-NEXT: [[INV:%.*]] = xor i8 [[XOR]], -1
1143 ; CHECK-NEXT: ret i8 [[INV]]
1145 %xor = xor i8 %a, %b
1146 %inv = xor i8 %xor, -1
1147 %cmp = icmp eq i8 %a, %b
1148 %sel = select i1 %cmp, i8 -1, i8 %inv
1152 define i8 @select_eq_xor_recursive2(i8 %a, i8 %b) {
1153 ; CHECK-LABEL: @select_eq_xor_recursive2(
1154 ; CHECK-NEXT: [[XOR:%.*]] = xor i8 [[A:%.*]], [[B:%.*]]
1155 ; CHECK-NEXT: [[INV:%.*]] = xor i8 [[XOR]], -1
1156 ; CHECK-NEXT: [[ADD:%.*]] = add i8 [[INV]], 10
1157 ; CHECK-NEXT: ret i8 [[ADD]]
1159 %xor = xor i8 %a, %b
1160 %inv = xor i8 %xor, -1
1161 %add = add i8 %inv, 10
1162 %cmp = icmp eq i8 %a, %b
1163 %sel = select i1 %cmp, i8 9, i8 %add
1167 define i8 @select_eq_xor_recursive3(i8 %a, i8 %b) {
1168 ; CHECK-LABEL: @select_eq_xor_recursive3(
1169 ; CHECK-NEXT: [[XOR:%.*]] = xor i8 [[A:%.*]], [[B:%.*]]
1170 ; CHECK-NEXT: [[INV:%.*]] = xor i8 [[XOR]], -1
1171 ; CHECK-NEXT: [[ADD:%.*]] = add i8 [[INV]], 10
1172 ; CHECK-NEXT: [[MUL:%.*]] = mul i8 [[ADD]], 3
1173 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[A]], [[B]]
1174 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i8 9, i8 [[MUL]]
1175 ; CHECK-NEXT: ret i8 [[SEL]]
1177 %xor = xor i8 %a, %b
1178 %inv = xor i8 %xor, -1
1179 %add = add i8 %inv, 10
1180 %mul = mul i8 %add, 3
1181 %cmp = icmp eq i8 %a, %b
1182 %sel = select i1 %cmp, i8 9, i8 %mul
1186 ; Cannot drop select, because this would propagate poison from %a.
1187 define i8 @select_eq_xor_recursive_propagates_poison(i8 %a, i8 %b) {
1188 ; CHECK-LABEL: @select_eq_xor_recursive_propagates_poison(
1189 ; CHECK-NEXT: [[XOR1:%.*]] = add i8 [[A:%.*]], [[B:%.*]]
1190 ; CHECK-NEXT: [[XOR2:%.*]] = xor i8 [[A]], [[XOR1]]
1191 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[B]], 0
1192 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i8 0, i8 [[XOR2]]
1193 ; CHECK-NEXT: ret i8 [[SEL]]
1195 %xor1 = add i8 %a, %b
1196 %xor2 = xor i8 %a, %xor1
1197 %cmp = icmp eq i8 %b, 0
1198 %sel = select i1 %cmp, i8 0, i8 %xor2
1202 define i8 @select_eq_and_recursive(i8 %a) {
1203 ; CHECK-LABEL: @select_eq_and_recursive(
1204 ; CHECK-NEXT: [[NEG:%.*]] = sub i8 0, [[A:%.*]]
1205 ; CHECK-NEXT: [[AND:%.*]] = and i8 [[NEG]], [[A]]
1206 ; CHECK-NEXT: [[ADD:%.*]] = add i8 [[AND]], 1
1207 ; CHECK-NEXT: ret i8 [[ADD]]
1210 %and = and i8 %neg, %a
1211 %add = add i8 %and, 1
1212 %cmp = icmp eq i8 %a, 0
1213 %sel = select i1 %cmp, i8 1, i8 %add
1217 ; Cannot drop select, because this would propagate poison from %b.
1218 define i8 @select_eq_and_recursive_propagates_poison(i8 %a, i8 %b) {
1219 ; CHECK-LABEL: @select_eq_and_recursive_propagates_poison(
1220 ; CHECK-NEXT: [[NEG:%.*]] = sub i8 [[B:%.*]], [[A:%.*]]
1221 ; CHECK-NEXT: [[AND:%.*]] = and i8 [[NEG]], [[A]]
1222 ; CHECK-NEXT: [[ADD:%.*]] = add i8 [[AND]], 1
1223 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[A]], 0
1224 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i8 1, i8 [[ADD]]
1225 ; CHECK-NEXT: ret i8 [[SEL]]
1227 %neg = sub i8 %b, %a
1228 %and = and i8 %neg, %a
1229 %add = add i8 %and, 1
1230 %cmp = icmp eq i8 %a, 0
1231 %sel = select i1 %cmp, i8 1, i8 %add
1235 define i8 @select_eq_xor_recursive_allow_refinement(i8 %a, i8 %b) {
1236 ; CHECK-LABEL: @select_eq_xor_recursive_allow_refinement(
1237 ; CHECK-NEXT: ret i8 0
1239 %xor1 = add i8 %a, %b
1240 %xor2 = xor i8 %a, %xor1
1241 %cmp = icmp eq i8 %b, 0
1242 %sel = select i1 %cmp, i8 %xor2, i8 0
1246 define i8 @select_eq_mul_absorber(i8 %x, i8 noundef %y) {
1247 ; CHECK-LABEL: @select_eq_mul_absorber(
1248 ; CHECK-NEXT: [[ADD:%.*]] = add i8 [[X:%.*]], -1
1249 ; CHECK-NEXT: [[MUL:%.*]] = mul i8 [[ADD]], [[Y:%.*]]
1250 ; CHECK-NEXT: ret i8 [[MUL]]
1252 %cmp = icmp eq i8 %x, 1
1253 %add = add i8 %x, -1
1254 %mul = mul i8 %add, %y
1255 %sel = select i1 %cmp, i8 0, i8 %mul
1259 define i8 @select_eq_mul_not_absorber(i8 %x, i8 noundef %y) {
1260 ; CHECK-LABEL: @select_eq_mul_not_absorber(
1261 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[X:%.*]], 0
1262 ; CHECK-NEXT: [[ADD:%.*]] = add i8 [[X]], -1
1263 ; CHECK-NEXT: [[MUL:%.*]] = mul i8 [[ADD]], [[Y:%.*]]
1264 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i8 0, i8 [[MUL]]
1265 ; CHECK-NEXT: ret i8 [[SEL]]
1267 %cmp = icmp eq i8 %x, 0
1268 %add = add i8 %x, -1
1269 %mul = mul i8 %add, %y
1270 %sel = select i1 %cmp, i8 0, i8 %mul
1274 ; Vector to scalar options should be treated as lane-crossing.
1275 define <2 x i8> @select_eq_vector_insert_extract(<2 x i8> %a, <2 x i8> %b) {
1276 ; CHECK-LABEL: @select_eq_vector_insert_extract(
1277 ; CHECK-NEXT: [[EXTRACT0:%.*]] = extractelement <2 x i8> [[A:%.*]], i64 0
1278 ; CHECK-NEXT: [[EXTRACT1:%.*]] = extractelement <2 x i8> [[A]], i64 1
1279 ; CHECK-NEXT: [[INSERT0:%.*]] = insertelement <2 x i8> poison, i8 [[EXTRACT1]], i64 0
1280 ; CHECK-NEXT: [[INSERT1:%.*]] = insertelement <2 x i8> [[INSERT0]], i8 [[EXTRACT0]], i64 1
1281 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i8> [[A]], zeroinitializer
1282 ; CHECK-NEXT: [[SEL:%.*]] = select <2 x i1> [[CMP]], <2 x i8> [[INSERT1]], <2 x i8> zeroinitializer
1283 ; CHECK-NEXT: ret <2 x i8> [[SEL]]
1285 %extract0 = extractelement <2 x i8> %a, i64 0
1286 %extract1 = extractelement <2 x i8> %a, i64 1
1287 %insert0 = insertelement <2 x i8> poison, i8 %extract1, i64 0
1288 %insert1 = insertelement <2 x i8> %insert0, i8 %extract0, i64 1
1289 %cmp = icmp eq <2 x i8> %a, zeroinitializer
1290 %sel = select <2 x i1> %cmp, <2 x i8> %insert1, <2 x i8> zeroinitializer
1294 define i32 @poison(i32 %x, i32 %y) {
1295 ; CHECK-LABEL: @poison(
1296 ; CHECK-NEXT: ret i32 [[X:%.*]]
1298 %v = select i1 undef, i32 %x, i32 %y
1302 define i32 @poison2(i1 %cond, i32 %x) {
1303 ; CHECK-LABEL: @poison2(
1304 ; CHECK-NEXT: ret i32 [[X:%.*]]
1306 %v = select i1 %cond, i32 poison, i32 %x
1310 define i32 @poison3(i1 %cond, i32 %x) {
1311 ; CHECK-LABEL: @poison3(
1312 ; CHECK-NEXT: ret i32 [[X:%.*]]
1314 %v = select i1 %cond, i32 %x, i32 poison
1318 define <2 x i32> @poison4(<2 x i1> %cond, <2 x i32> %x) {
1319 ; CHECK-LABEL: @poison4(
1320 ; CHECK-NEXT: ret <2 x i32> [[X:%.*]]
1322 %v = select <2 x i1> %cond, <2 x i32> %x, <2 x i32> poison
1326 ; 0 is the absorber constant for 'and'.
1327 ; The 'select' can't block extra poison because both sides of 'and' have 'x' operand.
1329 define i8 @replace_false_op_eq_neg_and(i8 %x) {
1330 ; CHECK-LABEL: @replace_false_op_eq_neg_and(
1331 ; CHECK-NEXT: [[NEG:%.*]] = sub i8 0, [[X:%.*]]
1332 ; CHECK-NEXT: [[AND:%.*]] = and i8 [[NEG]], [[X]]
1333 ; CHECK-NEXT: ret i8 [[AND]]
1335 %eq0 = icmp eq i8 %x, 0
1337 %and = and i8 %neg, %x
1338 %sel = select i1 %eq0, i8 0, i8 %and
1342 ; same as above, but commute 'and'
1344 define i8 @replace_false_op_eq_neg_and_commute(i8 %x) {
1345 ; CHECK-LABEL: @replace_false_op_eq_neg_and_commute(
1346 ; CHECK-NEXT: [[NEG:%.*]] = sub i8 0, [[X:%.*]]
1347 ; CHECK-NEXT: [[AND:%.*]] = and i8 [[X]], [[NEG]]
1348 ; CHECK-NEXT: ret i8 [[AND]]
1350 %eq0 = icmp eq i8 %x, 0
1352 %and = and i8 %x, %neg
1353 %sel = select i1 %eq0, i8 0, i8 %and
1357 ; same as above, but swap 'select'
1359 define i8 @replace_false_op_ne_neg_and(i8 %x) {
1360 ; CHECK-LABEL: @replace_false_op_ne_neg_and(
1361 ; CHECK-NEXT: [[NEG:%.*]] = sub i8 0, [[X:%.*]]
1362 ; CHECK-NEXT: [[AND:%.*]] = and i8 [[NEG]], [[X]]
1363 ; CHECK-NEXT: ret i8 [[AND]]
1365 %ne0 = icmp ne i8 %x, 0
1367 %and = and i8 %neg, %x
1368 %sel = select i1 %ne0, i8 %and, i8 0
1372 ; same as above, but commute 'and' and swap 'select'
1374 define i8 @replace_false_op_ne_neg_and_commute(i8 %x) {
1375 ; CHECK-LABEL: @replace_false_op_ne_neg_and_commute(
1376 ; CHECK-NEXT: [[NEG:%.*]] = sub i8 0, [[X:%.*]]
1377 ; CHECK-NEXT: [[AND:%.*]] = and i8 [[X]], [[NEG]]
1378 ; CHECK-NEXT: ret i8 [[AND]]
1380 %ne0 = icmp ne i8 %x, 0
1382 %and = and i8 %x, %neg
1383 %sel = select i1 %ne0, i8 %and, i8 0
1387 ; the first binop can be anything as long as it has the common operand
1389 define i8 @replace_false_op_eq_dec_and(i8 %x) {
1390 ; CHECK-LABEL: @replace_false_op_eq_dec_and(
1391 ; CHECK-NEXT: [[DEC:%.*]] = add i8 [[X:%.*]], -1
1392 ; CHECK-NEXT: [[AND:%.*]] = and i8 [[DEC]], [[X]]
1393 ; CHECK-NEXT: ret i8 [[AND]]
1395 %eq0 = icmp eq i8 %x, 0
1396 %dec = add i8 %x, -1
1397 %and = and i8 %dec, %x
1398 %sel = select i1 %eq0, i8 0, i8 %and
1402 ; mul has the same absorber constant - "0"
1404 define i8 @replace_false_op_eq_add_mul(i8 %x) {
1405 ; CHECK-LABEL: @replace_false_op_eq_add_mul(
1406 ; CHECK-NEXT: [[ADD:%.*]] = add i8 [[X:%.*]], 42
1407 ; CHECK-NEXT: [[MUL:%.*]] = mul i8 [[ADD]], [[X]]
1408 ; CHECK-NEXT: ret i8 [[MUL]]
1410 %eq0 = icmp eq i8 %x, 0
1411 %add = add i8 %x, 42
1412 %mul = mul i8 %add, %x
1413 %sel = select i1 %eq0, i8 0, i8 %mul
1417 ; or has a different absorber constant = "-1"
1419 define i8 @replace_false_op_eq_shl_or(i8 %x) {
1420 ; CHECK-LABEL: @replace_false_op_eq_shl_or(
1421 ; CHECK-NEXT: [[SHL:%.*]] = shl i8 [[X:%.*]], 3
1422 ; CHECK-NEXT: [[OR:%.*]] = or i8 [[X]], [[SHL]]
1423 ; CHECK-NEXT: ret i8 [[OR]]
1425 %eq0 = icmp eq i8 %x, -1
1427 %or = or i8 %x, %shl
1428 %sel = select i1 %eq0, i8 -1, i8 %or
1432 define i8 @replace_false_op_eq_shl_or_disjoint(i8 %x) {
1433 ; CHECK-LABEL: @replace_false_op_eq_shl_or_disjoint(
1434 ; CHECK-NEXT: [[EQ0:%.*]] = icmp eq i8 [[X:%.*]], -1
1435 ; CHECK-NEXT: [[SHL:%.*]] = shl i8 [[X]], 3
1436 ; CHECK-NEXT: [[OR:%.*]] = or disjoint i8 [[X]], [[SHL]]
1437 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[EQ0]], i8 -1, i8 [[OR]]
1438 ; CHECK-NEXT: ret i8 [[SEL]]
1440 %eq0 = icmp eq i8 %x, -1
1442 %or = or disjoint i8 %x, %shl
1443 %sel = select i1 %eq0, i8 -1, i8 %or
1447 ; negative test - wrong cmp predicate
1449 define i8 @replace_false_op_sgt_neg_and(i8 %x) {
1450 ; CHECK-LABEL: @replace_false_op_sgt_neg_and(
1451 ; CHECK-NEXT: [[EQ0:%.*]] = icmp sgt i8 [[X:%.*]], 0
1452 ; CHECK-NEXT: [[NEG:%.*]] = sub i8 0, [[X]]
1453 ; CHECK-NEXT: [[AND:%.*]] = and i8 [[NEG]], [[X]]
1454 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[EQ0]], i8 0, i8 [[AND]]
1455 ; CHECK-NEXT: ret i8 [[SEL]]
1457 %eq0 = icmp sgt i8 %x, 0
1459 %and = and i8 %neg, %x
1460 %sel = select i1 %eq0, i8 0, i8 %and
1464 ; negative test - the binop must use a compare operand
1466 define i8 @replace_false_op_eq_shl_or_wrong_cmp_op(i8 %x, i8 %y) {
1467 ; CHECK-LABEL: @replace_false_op_eq_shl_or_wrong_cmp_op(
1468 ; CHECK-NEXT: [[EQ0:%.*]] = icmp eq i8 [[Y:%.*]], -1
1469 ; CHECK-NEXT: [[SHL:%.*]] = shl i8 [[X:%.*]], 3
1470 ; CHECK-NEXT: [[OR:%.*]] = or i8 [[X]], [[SHL]]
1471 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[EQ0]], i8 -1, i8 [[OR]]
1472 ; CHECK-NEXT: ret i8 [[SEL]]
1474 %eq0 = icmp eq i8 %y, -1
1476 %or = or i8 %x, %shl
1477 %sel = select i1 %eq0, i8 -1, i8 %or
1481 ; negative test - can't have extra source of potential poison
1483 define i8 @replace_false_op_eq_neg_and_leak1(i8 %x, i8 %y) {
1484 ; CHECK-LABEL: @replace_false_op_eq_neg_and_leak1(
1485 ; CHECK-NEXT: [[EQ0:%.*]] = icmp eq i8 [[X:%.*]], 0
1486 ; CHECK-NEXT: [[NEG:%.*]] = sub i8 0, [[Y:%.*]]
1487 ; CHECK-NEXT: [[AND:%.*]] = and i8 [[NEG]], [[X]]
1488 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[EQ0]], i8 0, i8 [[AND]]
1489 ; CHECK-NEXT: ret i8 [[SEL]]
1491 %eq0 = icmp eq i8 %x, 0
1493 %and = and i8 %neg, %x
1494 %sel = select i1 %eq0, i8 0, i8 %and
1498 ; negative test - can't have extra source of potential poison
1500 define i8 @replace_false_op_eq_neg_and_leak2(i8 %x, i8 %y) {
1501 ; CHECK-LABEL: @replace_false_op_eq_neg_and_leak2(
1502 ; CHECK-NEXT: [[EQ0:%.*]] = icmp eq i8 [[X:%.*]], 0
1503 ; CHECK-NEXT: [[NEG:%.*]] = sub i8 0, [[X]]
1504 ; CHECK-NEXT: [[AND:%.*]] = and i8 [[NEG]], [[Y:%.*]]
1505 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[EQ0]], i8 0, i8 [[AND]]
1506 ; CHECK-NEXT: ret i8 [[SEL]]
1508 %eq0 = icmp eq i8 %x, 0
1510 %and = and i8 %neg, %y
1511 %sel = select i1 %eq0, i8 0, i8 %and
1515 ; negative test - can't have extra source of potential poison
1517 define i8 @replace_false_op_eq_add_mul_leak3(i8 %x, i8 %y) {
1518 ; CHECK-LABEL: @replace_false_op_eq_add_mul_leak3(
1519 ; CHECK-NEXT: [[EQ0:%.*]] = icmp eq i8 [[X:%.*]], 0
1520 ; CHECK-NEXT: [[ADD:%.*]] = add i8 [[X]], [[Y:%.*]]
1521 ; CHECK-NEXT: [[MUL:%.*]] = mul i8 [[ADD]], [[X]]
1522 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[EQ0]], i8 0, i8 [[MUL]]
1523 ; CHECK-NEXT: ret i8 [[SEL]]
1525 %eq0 = icmp eq i8 %x, 0
1526 %add = add i8 %x, %y
1527 %mul = mul i8 %add, %x
1528 %sel = select i1 %eq0, i8 0, i8 %mul
1532 ; negative test - can't have extra source of potential poison
1534 define i8 @replace_false_op_eq_shl_or_leak4(i8 %x, i8 %y) {
1535 ; CHECK-LABEL: @replace_false_op_eq_shl_or_leak4(
1536 ; CHECK-NEXT: [[EQ0:%.*]] = icmp eq i8 [[X:%.*]], -1
1537 ; CHECK-NEXT: [[SHL:%.*]] = shl i8 [[Y:%.*]], [[X]]
1538 ; CHECK-NEXT: [[OR:%.*]] = or i8 [[X]], [[SHL]]
1539 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[EQ0]], i8 -1, i8 [[OR]]
1540 ; CHECK-NEXT: ret i8 [[SEL]]
1542 %eq0 = icmp eq i8 %x, -1
1543 %shl = shl i8 %y, %x
1544 %or = or i8 %x, %shl
1545 %sel = select i1 %eq0, i8 -1, i8 %or
1549 ; negative test - wrong cmp constant
1551 define i8 @replace_false_op_eq42_neg_and(i8 %x) {
1552 ; CHECK-LABEL: @replace_false_op_eq42_neg_and(
1553 ; CHECK-NEXT: [[EQ42:%.*]] = icmp eq i8 [[X:%.*]], 42
1554 ; CHECK-NEXT: [[NEG:%.*]] = sub i8 0, [[X]]
1555 ; CHECK-NEXT: [[AND:%.*]] = and i8 [[NEG]], [[X]]
1556 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[EQ42]], i8 0, i8 [[AND]]
1557 ; CHECK-NEXT: ret i8 [[SEL]]
1559 %eq42 = icmp eq i8 %x, 42
1561 %and = and i8 %neg, %x
1562 %sel = select i1 %eq42, i8 0, i8 %and
1566 define ptr @select_op_replacement_in_phi(ptr %head) {
1567 ; CHECK-LABEL: @select_op_replacement_in_phi(
1568 ; CHECK-NEXT: entry:
1569 ; CHECK-NEXT: br label [[LOOP:%.*]]
1571 ; CHECK-NEXT: [[CURRENT:%.*]] = phi ptr [ [[HEAD:%.*]], [[ENTRY:%.*]] ], [ [[NEXT:%.*]], [[LATCH:%.*]] ]
1572 ; CHECK-NEXT: [[PREV:%.*]] = phi ptr [ null, [[ENTRY]] ], [ [[CURRENT]], [[LATCH]] ]
1573 ; CHECK-NEXT: [[CURRENT_NULL:%.*]] = icmp eq ptr [[CURRENT]], null
1574 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CURRENT_NULL]], ptr [[PREV]], ptr null
1575 ; CHECK-NEXT: br i1 [[CURRENT_NULL]], label [[EXIT:%.*]], label [[LATCH]]
1577 ; CHECK-NEXT: [[NEXT]] = load ptr, ptr [[CURRENT]], align 8
1578 ; CHECK-NEXT: br label [[LOOP]]
1580 ; CHECK-NEXT: ret ptr [[SEL]]
1586 %current = phi ptr [ %head, %entry ], [ %next, %latch ]
1587 %prev = phi ptr [ null, %entry ], [ %current, %latch ]
1588 %current.null = icmp eq ptr %current, null
1589 %sel = select i1 %current.null, ptr %prev, ptr null
1590 br i1 %current.null, label %exit, label %latch
1593 %next = load ptr, ptr %current
1600 define i8 @select_sub_cmp(i8 %0, i8 %1) {
1601 ; CHECK-LABEL: @select_sub_cmp(
1602 ; CHECK-NEXT: [[TMP3:%.*]] = sub nsw i8 [[TMP1:%.*]], [[TMP0:%.*]]
1603 ; CHECK-NEXT: ret i8 [[TMP3]]
1605 %3 = icmp eq i8 %1, %0
1606 %4 = sub nsw i8 %1, %0
1607 %5 = select i1 %3, i8 0, i8 %4
1611 define <2 x i8> @select_sub_cmp_vec(<2 x i8> %0, <2 x i8> %1) {
1612 ; CHECK-LABEL: @select_sub_cmp_vec(
1613 ; CHECK-NEXT: [[TMP3:%.*]] = sub nsw <2 x i8> [[TMP1:%.*]], [[TMP0:%.*]]
1614 ; CHECK-NEXT: ret <2 x i8> [[TMP3]]
1616 %3 = icmp eq <2 x i8> %1, %0
1617 %4 = sub nsw <2 x i8> %1, %0
1618 %5 = select <2 x i1> %3, <2 x i8> <i8 0, i8 0>, <2 x i8> %4
1622 define i8 @select_sub_cmp_swap(i8 %0, i8 %1) {
1623 ; CHECK-LABEL: @select_sub_cmp_swap(
1624 ; CHECK-NEXT: [[TMP3:%.*]] = sub nsw i8 [[TMP0:%.*]], [[TMP1:%.*]]
1625 ; CHECK-NEXT: ret i8 [[TMP3]]
1627 %3 = icmp eq i8 %1, %0
1628 %4 = sub nsw i8 %0, %1
1629 %5 = select i1 %3, i8 0, i8 %4
1633 define <2 x i8> @select_sub_cmp_vec_swap(<2 x i8> %0, <2 x i8> %1) {
1634 ; CHECK-LABEL: @select_sub_cmp_vec_swap(
1635 ; CHECK-NEXT: [[TMP3:%.*]] = sub nsw <2 x i8> [[TMP0:%.*]], [[TMP1:%.*]]
1636 ; CHECK-NEXT: ret <2 x i8> [[TMP3]]
1638 %3 = icmp eq <2 x i8> %1, %0
1639 %4 = sub nsw <2 x i8> %0, %1
1640 %5 = select <2 x i1> %3, <2 x i8> <i8 0, i8 0>, <2 x i8> %4
1645 define i8 @select_sub_cmp_nonzero(i8 %0, i8 %1) {
1646 ; CHECK-LABEL: @select_sub_cmp_nonzero(
1647 ; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i8 [[TMP1:%.*]], [[TMP0:%.*]]
1648 ; CHECK-NEXT: [[TMP4:%.*]] = sub nsw i8 [[TMP1]], [[TMP0]]
1649 ; CHECK-NEXT: [[TMP5:%.*]] = select i1 [[TMP3]], i8 42, i8 [[TMP4]]
1650 ; CHECK-NEXT: ret i8 [[TMP5]]
1652 %3 = icmp eq i8 %1, %0
1653 %4 = sub nsw i8 %1, %0
1654 %5 = select i1 %3, i8 42, i8 %4
1658 ; X == Y ? 0 : X ^ Y --> X ^ Y, https://alive2.llvm.org/ce/z/cykffE
1659 define i8 @select_xor_cmp(i8 %0, i8 %1) {
1660 ; CHECK-LABEL: @select_xor_cmp(
1661 ; CHECK-NEXT: [[TMP3:%.*]] = xor i8 [[TMP1:%.*]], [[TMP0:%.*]]
1662 ; CHECK-NEXT: ret i8 [[TMP3]]
1664 %3 = icmp eq i8 %1, %0
1666 %5 = select i1 %3, i8 0, i8 %4
1670 define <2 x i8> @select_xor_cmp_vec(<2 x i8> %0, <2 x i8> %1) {
1671 ; CHECK-LABEL: @select_xor_cmp_vec(
1672 ; CHECK-NEXT: [[TMP3:%.*]] = xor <2 x i8> [[TMP1:%.*]], [[TMP0:%.*]]
1673 ; CHECK-NEXT: ret <2 x i8> [[TMP3]]
1675 %3 = icmp eq <2 x i8> %1, %0
1676 %4 = xor <2 x i8> %1, %0
1677 %5 = select <2 x i1> %3, <2 x i8> <i8 0, i8 0>, <2 x i8> %4
1681 define i8 @select_xor_cmp_swap(i8 %0, i8 %1) {
1682 ; CHECK-LABEL: @select_xor_cmp_swap(
1683 ; CHECK-NEXT: [[TMP3:%.*]] = xor i8 [[TMP0:%.*]], [[TMP1:%.*]]
1684 ; CHECK-NEXT: ret i8 [[TMP3]]
1686 %3 = icmp eq i8 %1, %0
1688 %5 = select i1 %3, i8 0, i8 %4
1692 define <2 x i8> @select_xor_cmp_vec_swap(<2 x i8> %0, <2 x i8> %1) {
1693 ; CHECK-LABEL: @select_xor_cmp_vec_swap(
1694 ; CHECK-NEXT: [[TMP3:%.*]] = xor <2 x i8> [[TMP0:%.*]], [[TMP1:%.*]]
1695 ; CHECK-NEXT: ret <2 x i8> [[TMP3]]
1697 %3 = icmp eq <2 x i8> %1, %0
1698 %4 = xor <2 x i8> %0, %1
1699 %5 = select <2 x i1> %3, <2 x i8> <i8 0, i8 0>, <2 x i8> %4
1703 ; Negative test: the xor operands are not %0 and %1
1704 define i8 @select_xor_cmp_unmatched_operands(i8 %0, i8 %1, i8 %c) {
1705 ; CHECK-LABEL: @select_xor_cmp_unmatched_operands(
1706 ; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i8 [[TMP1:%.*]], [[TMP0:%.*]]
1707 ; CHECK-NEXT: [[TMP4:%.*]] = xor i8 [[TMP1]], [[C:%.*]]
1708 ; CHECK-NEXT: [[TMP5:%.*]] = select i1 [[TMP3]], i8 0, i8 [[TMP4]]
1709 ; CHECK-NEXT: ret i8 [[TMP5]]
1711 %3 = icmp eq i8 %1, %0
1713 %5 = select i1 %3, i8 0, i8 %4
1717 define i8 @select_or_eq(i8 %x, i8 %y) {
1718 ; CHECK-LABEL: @select_or_eq(
1719 ; CHECK-NEXT: [[OR:%.*]] = or i8 [[X:%.*]], [[Y:%.*]]
1720 ; CHECK-NEXT: ret i8 [[OR]]
1722 %cmp = icmp eq i8 %x, %y
1724 %sel = select i1 %cmp, i8 %x, i8 %or
1728 define i8 @select_or_disjoint_eq(i8 %x, i8 %y) {
1729 ; CHECK-LABEL: @select_or_disjoint_eq(
1730 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[X:%.*]], [[Y:%.*]]
1731 ; CHECK-NEXT: [[OR:%.*]] = or disjoint i8 [[X]], [[Y]]
1732 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i8 [[X]], i8 [[OR]]
1733 ; CHECK-NEXT: ret i8 [[SEL]]
1735 %cmp = icmp eq i8 %x, %y
1736 %or = or disjoint i8 %x, %y
1737 %sel = select i1 %cmp, i8 %x, i8 %or