[SLP] Add cost model for `llvm.powi.*` intrinsics
[llvm-project.git] / llvm / test / Transforms / InstCombine / select.ll
blob0af3fe523f476bedbadbf63d292ee0a80bcebc20
1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=instcombine -S | FileCheck %s
4 ; PR1822
6 target datalayout = "e-p:64:64-p1:16:16-p2:32:32:32-p3:64:64:64"
8 define i1 @test5(i1 %C) {
9 ; CHECK-LABEL: @test5(
10 ; CHECK-NEXT:    [[NOT_C:%.*]] = xor i1 [[C:%.*]], true
11 ; CHECK-NEXT:    ret i1 [[NOT_C]]
13   %V = select i1 %C, i1 false, i1 true
14   ret i1 %V
17 define i32 @test6(i1 %C) {
18 ; CHECK-LABEL: @test6(
19 ; CHECK-NEXT:    [[V:%.*]] = zext i1 [[C:%.*]] to i32
20 ; CHECK-NEXT:    ret i32 [[V]]
22   %V = select i1 %C, i32 1, i32 0
23   ret i32 %V
26 define i1 @trueval_is_true(i1 %C, i1 %X) {
27 ; CHECK-LABEL: @trueval_is_true(
28 ; CHECK-NEXT:    [[R:%.*]] = select i1 [[C:%.*]], i1 true, i1 [[X:%.*]]
29 ; CHECK-NEXT:    ret i1 [[R]]
31   %R = select i1 %C, i1 true, i1 %X
32   ret i1 %R
35 define <2 x i1> @trueval_is_true_vec(<2 x i1> %C, <2 x i1> %X) {
36 ; CHECK-LABEL: @trueval_is_true_vec(
37 ; CHECK-NEXT:    [[R:%.*]] = select <2 x i1> [[C:%.*]], <2 x i1> <i1 true, i1 true>, <2 x i1> [[X:%.*]]
38 ; CHECK-NEXT:    ret <2 x i1> [[R]]
40   %R = select <2 x i1> %C, <2 x i1> <i1 true, i1 true>, <2 x i1> %X
41   ret <2 x i1> %R
44 define <2 x i1> @trueval_is_true_vec_poison_elt(<2 x i1> %C, <2 x i1> %X) {
45 ; CHECK-LABEL: @trueval_is_true_vec_poison_elt(
46 ; CHECK-NEXT:    [[R:%.*]] = select <2 x i1> [[C:%.*]], <2 x i1> <i1 poison, i1 true>, <2 x i1> [[X:%.*]]
47 ; CHECK-NEXT:    ret <2 x i1> [[R]]
49   %R = select <2 x i1> %C, <2 x i1> <i1 poison, i1 true>, <2 x i1> %X
50   ret <2 x i1> %R
53 define i1 @test8(i1 %C, i1 %X) {
54 ; CHECK-LABEL: @test8(
55 ; CHECK-NEXT:    [[R:%.*]] = select i1 [[C:%.*]], i1 [[X:%.*]], i1 false
56 ; CHECK-NEXT:    ret i1 [[R]]
58   %R = select i1 %C, i1 %X, i1 false
59   ret i1 %R
62 define <2 x i1> @test8vec(<2 x i1> %C, <2 x i1> %X) {
63 ; CHECK-LABEL: @test8vec(
64 ; CHECK-NEXT:    [[R:%.*]] = select <2 x i1> [[C:%.*]], <2 x i1> [[X:%.*]], <2 x i1> zeroinitializer
65 ; CHECK-NEXT:    ret <2 x i1> [[R]]
67   %R = select <2 x i1> %C, <2 x i1> %X, <2 x i1> <i1 false, i1 false>
68   ret <2 x i1> %R
71 define <vscale x 2 x i1> @test8vvec(<vscale x 2 x i1> %C, <vscale x 2 x i1> %X) {
72 ; CHECK-LABEL: @test8vvec(
73 ; CHECK-NEXT:    [[R:%.*]] = select <vscale x 2 x i1> [[C:%.*]], <vscale x 2 x i1> [[X:%.*]], <vscale x 2 x i1> zeroinitializer
74 ; CHECK-NEXT:    ret <vscale x 2 x i1> [[R]]
76   %R = select <vscale x 2 x i1> %C, <vscale x 2 x i1> %X, <vscale x 2 x i1> zeroinitializer
77   ret <vscale x 2 x i1> %R
80 define i1 @test9(i1 %C, i1 %X) {
81 ; CHECK-LABEL: @test9(
82 ; CHECK-NEXT:    [[NOT_C:%.*]] = xor i1 [[C:%.*]], true
83 ; CHECK-NEXT:    [[R:%.*]] = select i1 [[NOT_C]], i1 [[X:%.*]], i1 false
84 ; CHECK-NEXT:    ret i1 [[R]]
86   %R = select i1 %C, i1 false, i1 %X
87   ret i1 %R
90 define <2 x i1> @test9vec(<2 x i1> %C, <2 x i1> %X) {
91 ; CHECK-LABEL: @test9vec(
92 ; CHECK-NEXT:    [[NOT_C:%.*]] = xor <2 x i1> [[C:%.*]], <i1 true, i1 true>
93 ; CHECK-NEXT:    [[R:%.*]] = select <2 x i1> [[NOT_C]], <2 x i1> [[X:%.*]], <2 x i1> zeroinitializer
94 ; CHECK-NEXT:    ret <2 x i1> [[R]]
96   %R = select <2 x i1> %C, <2 x i1> <i1 false, i1 false>, <2 x i1> %X
97   ret <2 x i1> %R
100 define <vscale x 2 x i1> @test9vvec(<vscale x 2 x i1> %C, <vscale x 2 x i1> %X) {
101 ; CHECK-LABEL: @test9vvec(
102 ; CHECK-NEXT:    [[NOT_C:%.*]] = xor <vscale x 2 x i1> [[C:%.*]], shufflevector (<vscale x 2 x i1> insertelement (<vscale x 2 x i1> poison, i1 true, i32 0), <vscale x 2 x i1> poison, <vscale x 2 x i32> zeroinitializer)
103 ; CHECK-NEXT:    [[R:%.*]] = select <vscale x 2 x i1> [[NOT_C]], <vscale x 2 x i1> [[X:%.*]], <vscale x 2 x i1> zeroinitializer
104 ; CHECK-NEXT:    ret <vscale x 2 x i1> [[R]]
106   %R = select <vscale x 2 x i1> %C, <vscale x 2 x i1> zeroinitializer, <vscale x 2 x i1> %X
107   ret <vscale x 2 x i1> %R
110 define i1 @test10(i1 %C, i1 %X) {
111 ; CHECK-LABEL: @test10(
112 ; CHECK-NEXT:    [[NOT_C:%.*]] = xor i1 [[C:%.*]], true
113 ; CHECK-NEXT:    [[R:%.*]] = select i1 [[NOT_C]], i1 true, i1 [[X:%.*]]
114 ; CHECK-NEXT:    ret i1 [[R]]
116   %R = select i1 %C, i1 %X, i1 true
117   ret i1 %R
120 define <2 x i1> @test10vec(<2 x i1> %C, <2 x i1> %X) {
121 ; CHECK-LABEL: @test10vec(
122 ; CHECK-NEXT:    [[NOT_C:%.*]] = xor <2 x i1> [[C:%.*]], <i1 true, i1 true>
123 ; CHECK-NEXT:    [[R:%.*]] = select <2 x i1> [[NOT_C]], <2 x i1> <i1 true, i1 true>, <2 x i1> [[X:%.*]]
124 ; CHECK-NEXT:    ret <2 x i1> [[R]]
126   %R = select <2 x i1> %C, <2 x i1> %X, <2 x i1> <i1 true, i1 true>
127   ret <2 x i1> %R
130 define i1 @test23(i1 %a, i1 %b) {
131 ; CHECK-LABEL: @test23(
132 ; CHECK-NEXT:    [[C:%.*]] = select i1 [[A:%.*]], i1 [[B:%.*]], i1 false
133 ; CHECK-NEXT:    ret i1 [[C]]
135   %c = select i1 %a, i1 %b, i1 %a
136   ret i1 %c
139 define <2 x i1> @test23vec(<2 x i1> %a, <2 x i1> %b) {
140 ; CHECK-LABEL: @test23vec(
141 ; CHECK-NEXT:    [[C:%.*]] = select <2 x i1> [[A:%.*]], <2 x i1> [[B:%.*]], <2 x i1> zeroinitializer
142 ; CHECK-NEXT:    ret <2 x i1> [[C]]
144   %c = select <2 x i1> %a, <2 x i1> %b, <2 x i1> %a
145   ret <2 x i1> %c
148 define i1 @test24(i1 %a, i1 %b) {
149 ; CHECK-LABEL: @test24(
150 ; CHECK-NEXT:    [[C:%.*]] = select i1 [[A:%.*]], i1 true, i1 [[B:%.*]]
151 ; CHECK-NEXT:    ret i1 [[C]]
153   %c = select i1 %a, i1 %a, i1 %b
154   ret i1 %c
157 define <2 x i1> @test24vec(<2 x i1> %a, <2 x i1> %b) {
158 ; CHECK-LABEL: @test24vec(
159 ; CHECK-NEXT:    [[C:%.*]] = select <2 x i1> [[A:%.*]], <2 x i1> <i1 true, i1 true>, <2 x i1> [[B:%.*]]
160 ; CHECK-NEXT:    ret <2 x i1> [[C]]
162   %c = select <2 x i1> %a, <2 x i1> %a, <2 x i1> %b
163   ret <2 x i1> %c
166 define i1 @test62(i1 %A, i1 %B) {
167 ; CHECK-LABEL: @test62(
168 ; CHECK-NEXT:    [[NOT:%.*]] = xor i1 [[A:%.*]], true
169 ; CHECK-NEXT:    [[C:%.*]] = select i1 [[NOT]], i1 [[B:%.*]], i1 false
170 ; CHECK-NEXT:    ret i1 [[C]]
172   %not = xor i1 %A, true
173   %C = select i1 %A, i1 %not, i1 %B
174   ret i1 %C
177 define <2 x i1> @test62vec(<2 x i1> %A, <2 x i1> %B) {
178 ; CHECK-LABEL: @test62vec(
179 ; CHECK-NEXT:    [[NOT:%.*]] = xor <2 x i1> [[A:%.*]], <i1 true, i1 true>
180 ; CHECK-NEXT:    [[C:%.*]] = select <2 x i1> [[NOT]], <2 x i1> [[B:%.*]], <2 x i1> zeroinitializer
181 ; CHECK-NEXT:    ret <2 x i1> [[C]]
183   %not = xor <2 x i1> %A, <i1 true, i1 true>
184   %C = select <2 x i1> %A, <2 x i1> %not, <2 x i1> %B
185   ret <2 x i1> %C
188 define i1 @test63(i1 %A, i1 %B) {
189 ; CHECK-LABEL: @test63(
190 ; CHECK-NEXT:    [[NOT:%.*]] = xor i1 [[A:%.*]], true
191 ; CHECK-NEXT:    [[C:%.*]] = select i1 [[NOT]], i1 true, i1 [[B:%.*]]
192 ; CHECK-NEXT:    ret i1 [[C]]
194   %not = xor i1 %A, true
195   %C = select i1 %A, i1 %B, i1 %not
196   ret i1 %C
199 define <2 x i1> @test63vec(<2 x i1> %A, <2 x i1> %B) {
200 ; CHECK-LABEL: @test63vec(
201 ; CHECK-NEXT:    [[NOT:%.*]] = xor <2 x i1> [[A:%.*]], <i1 true, i1 true>
202 ; CHECK-NEXT:    [[C:%.*]] = select <2 x i1> [[NOT]], <2 x i1> <i1 true, i1 true>, <2 x i1> [[B:%.*]]
203 ; CHECK-NEXT:    ret <2 x i1> [[C]]
205   %not = xor <2 x i1> %A, <i1 true, i1 true>
206   %C = select <2 x i1> %A, <2 x i1> %B, <2 x i1> %not
207   ret <2 x i1> %C
210 define i32 @test11(i32 %a) {
211 ; CHECK-LABEL: @test11(
212 ; CHECK-NEXT:    [[C:%.*]] = icmp ne i32 [[A:%.*]], 0
213 ; CHECK-NEXT:    [[R:%.*]] = zext i1 [[C]] to i32
214 ; CHECK-NEXT:    ret i32 [[R]]
216   %C = icmp eq i32 %a, 0
217   %R = select i1 %C, i32 0, i32 1
218   ret i32 %R
221 define i32 @test12(i1 %cond, i32 %a) {
222 ; CHECK-LABEL: @test12(
223 ; CHECK-NEXT:    [[B:%.*]] = zext i1 [[COND:%.*]] to i32
224 ; CHECK-NEXT:    [[C:%.*]] = or i32 [[B]], [[A:%.*]]
225 ; CHECK-NEXT:    ret i32 [[C]]
227   %b = or i32 %a, 1
228   %c = select i1 %cond, i32 %b, i32 %a
229   ret i32 %c
232 define <2 x i32> @test12vec(<2 x i1> %cond, <2 x i32> %a) {
233 ; CHECK-LABEL: @test12vec(
234 ; CHECK-NEXT:    [[B:%.*]] = zext <2 x i1> [[COND:%.*]] to <2 x i32>
235 ; CHECK-NEXT:    [[C:%.*]] = or <2 x i32> [[B]], [[A:%.*]]
236 ; CHECK-NEXT:    ret <2 x i32> [[C]]
238   %b = or <2 x i32> %a, <i32 1, i32 1>
239   %c = select <2 x i1> %cond, <2 x i32> %b, <2 x i32> %a
240   ret <2 x i32> %c
243 define i32 @test12a(i1 %cond, i32 %a) {
244 ; CHECK-LABEL: @test12a(
245 ; CHECK-NEXT:    [[B:%.*]] = zext i1 [[COND:%.*]] to i32
246 ; CHECK-NEXT:    [[C:%.*]] = ashr i32 [[A:%.*]], [[B]]
247 ; CHECK-NEXT:    ret i32 [[C]]
249   %b = ashr i32 %a, 1
250   %c = select i1 %cond, i32 %b, i32 %a
251   ret i32 %c
254 define <2 x i32> @test12avec(<2 x i1> %cond, <2 x i32> %a) {
255 ; CHECK-LABEL: @test12avec(
256 ; CHECK-NEXT:    [[B:%.*]] = zext <2 x i1> [[COND:%.*]] to <2 x i32>
257 ; CHECK-NEXT:    [[C:%.*]] = ashr <2 x i32> [[A:%.*]], [[B]]
258 ; CHECK-NEXT:    ret <2 x i32> [[C]]
260   %b = ashr <2 x i32> %a, <i32 1, i32 1>
261   %c = select <2 x i1> %cond, <2 x i32> %b, <2 x i32> %a
262   ret <2 x i32> %c
265 define i32 @test12b(i1 %cond, i32 %a) {
266 ; CHECK-LABEL: @test12b(
267 ; CHECK-NEXT:    [[NOT_COND:%.*]] = xor i1 [[COND:%.*]], true
268 ; CHECK-NEXT:    [[B:%.*]] = zext i1 [[NOT_COND]] to i32
269 ; CHECK-NEXT:    [[D:%.*]] = ashr i32 [[A:%.*]], [[B]]
270 ; CHECK-NEXT:    ret i32 [[D]]
272   %b = ashr i32 %a, 1
273   %d = select i1 %cond, i32 %a, i32 %b
274   ret i32 %d
277 define <2 x i32> @test12bvec(<2 x i1> %cond, <2 x i32> %a) {
278 ; CHECK-LABEL: @test12bvec(
279 ; CHECK-NEXT:    [[NOT_COND:%.*]] = xor <2 x i1> [[COND:%.*]], <i1 true, i1 true>
280 ; CHECK-NEXT:    [[B:%.*]] = zext <2 x i1> [[NOT_COND]] to <2 x i32>
281 ; CHECK-NEXT:    [[D:%.*]] = ashr <2 x i32> [[A:%.*]], [[B]]
282 ; CHECK-NEXT:    ret <2 x i32> [[D]]
284   %b = ashr <2 x i32> %a, <i32 1, i32 1>
285   %d = select <2 x i1> %cond, <2 x i32> %a, <2 x i32> %b
286   ret <2 x i32> %d
289 define i32 @test13(i32 %a, i32 %b) {
290 ; CHECK-LABEL: @test13(
291 ; CHECK-NEXT:    ret i32 [[B:%.*]]
293   %C = icmp eq i32 %a, %b
294   %V = select i1 %C, i32 %a, i32 %b
295   ret i32 %V
298 define i32 @test13a(i32 %a, i32 %b) {
299 ; CHECK-LABEL: @test13a(
300 ; CHECK-NEXT:    ret i32 [[A:%.*]]
302   %C = icmp ne i32 %a, %b
303   %V = select i1 %C, i32 %a, i32 %b
304   ret i32 %V
307 define i32 @test13b(i32 %a, i32 %b) {
308 ; CHECK-LABEL: @test13b(
309 ; CHECK-NEXT:    ret i32 [[A:%.*]]
311   %C = icmp eq i32 %a, %b
312   %V = select i1 %C, i32 %b, i32 %a
313   ret i32 %V
316 define i1 @test14a(i1 %C, i32 %X) {
317 ; CHECK-LABEL: @test14a(
318 ; CHECK-NEXT:    [[R1:%.*]] = icmp slt i32 [[X:%.*]], 1
319 ; CHECK-NEXT:    [[NOT_C:%.*]] = xor i1 [[C:%.*]], true
320 ; CHECK-NEXT:    [[R:%.*]] = select i1 [[NOT_C]], i1 true, i1 [[R1]]
321 ; CHECK-NEXT:    ret i1 [[R]]
323   %V = select i1 %C, i32 %X, i32 0
324   ; (X < 1) | !C
325   %R = icmp slt i32 %V, 1
326   ret i1 %R
329 define i1 @test14b(i1 %C, i32 %X) {
330 ; CHECK-LABEL: @test14b(
331 ; CHECK-NEXT:    [[R1:%.*]] = icmp slt i32 [[X:%.*]], 1
332 ; CHECK-NEXT:    [[R:%.*]] = select i1 [[C:%.*]], i1 true, i1 [[R1]]
333 ; CHECK-NEXT:    ret i1 [[R]]
335   %V = select i1 %C, i32 0, i32 %X
336   ; (X < 1) | C
337   %R = icmp slt i32 %V, 1
338   ret i1 %R
341 define i32 @test16(i1 %C, i32* %P) {
342 ; CHECK-LABEL: @test16(
343 ; CHECK-NEXT:    [[V:%.*]] = load i32, i32* [[P:%.*]], align 4
344 ; CHECK-NEXT:    ret i32 [[V]]
346   %P2 = select i1 %C, i32* %P, i32* null
347   %V = load i32, i32* %P2
348   ret i32 %V
351 ;; It may be legal to load from a null address in a non-zero address space
352 define i32 @test16_neg(i1 %C, i32 addrspace(1)* %P) {
353 ; CHECK-LABEL: @test16_neg(
354 ; CHECK-NEXT:    [[P2:%.*]] = select i1 [[C:%.*]], i32 addrspace(1)* [[P:%.*]], i32 addrspace(1)* null
355 ; CHECK-NEXT:    [[V:%.*]] = load i32, i32 addrspace(1)* [[P2]], align 4
356 ; CHECK-NEXT:    ret i32 [[V]]
358   %P2 = select i1 %C, i32 addrspace(1)* %P, i32 addrspace(1)* null
359   %V = load i32, i32 addrspace(1)* %P2
360   ret i32 %V
363 define i32 @test16_neg2(i1 %C, i32 addrspace(1)* %P) {
364 ; CHECK-LABEL: @test16_neg2(
365 ; CHECK-NEXT:    [[P2:%.*]] = select i1 [[C:%.*]], i32 addrspace(1)* null, i32 addrspace(1)* [[P:%.*]]
366 ; CHECK-NEXT:    [[V:%.*]] = load i32, i32 addrspace(1)* [[P2]], align 4
367 ; CHECK-NEXT:    ret i32 [[V]]
369   %P2 = select i1 %C, i32 addrspace(1)* null, i32 addrspace(1)* %P
370   %V = load i32, i32 addrspace(1)* %P2
371   ret i32 %V
374 ;; It may be legal to load from a null address with null pointer valid attribute.
375 define i32 @test16_no_null_opt(i1 %C, i32* %P) #0 {
376 ; CHECK-LABEL: @test16_no_null_opt(
377 ; CHECK-NEXT:    [[P2:%.*]] = select i1 [[C:%.*]], i32* [[P:%.*]], i32* null
378 ; CHECK-NEXT:    [[V:%.*]] = load i32, i32* [[P2]], align 4
379 ; CHECK-NEXT:    ret i32 [[V]]
381   %P2 = select i1 %C, i32* %P, i32* null
382   %V = load i32, i32* %P2
383   ret i32 %V
386 define i32 @test16_no_null_opt_2(i1 %C, i32* %P) #0 {
387 ; CHECK-LABEL: @test16_no_null_opt_2(
388 ; CHECK-NEXT:    [[P2:%.*]] = select i1 [[C:%.*]], i32* null, i32* [[P:%.*]]
389 ; CHECK-NEXT:    [[V:%.*]] = load i32, i32* [[P2]], align 4
390 ; CHECK-NEXT:    ret i32 [[V]]
392   %P2 = select i1 %C, i32* null, i32* %P
393   %V = load i32, i32* %P2
394   ret i32 %V
397 attributes #0 = { null_pointer_is_valid }
399 define i1 @test17(i32* %X, i1 %C) {
400 ; CHECK-LABEL: @test17(
401 ; CHECK-NEXT:    [[RV1:%.*]] = icmp eq i32* [[X:%.*]], null
402 ; CHECK-NEXT:    [[NOT_C:%.*]] = xor i1 [[C:%.*]], true
403 ; CHECK-NEXT:    [[RV:%.*]] = select i1 [[NOT_C]], i1 true, i1 [[RV1]]
404 ; CHECK-NEXT:    ret i1 [[RV]]
406   %R = select i1 %C, i32* %X, i32* null
407   %RV = icmp eq i32* %R, null
408   ret i1 %RV
411 define i32 @test18(i32 %X, i32 %Y, i1 %C) {
412 ; CHECK-LABEL: @test18(
413 ; CHECK-NEXT:    [[V:%.*]] = sdiv i32 [[Y:%.*]], [[X:%.*]]
414 ; CHECK-NEXT:    ret i32 [[V]]
416   %R = select i1 %C, i32 %X, i32 0
417   %V = sdiv i32 %Y, %R
418   ret i32 %V
421 define i32 @test19(i32 %x) {
422 ; CHECK-LABEL: @test19(
423 ; CHECK-NEXT:    [[X_LOBIT:%.*]] = ashr i32 [[X:%.*]], 31
424 ; CHECK-NEXT:    ret i32 [[X_LOBIT]]
426   %t = icmp ugt i32 %x, 2147483647
427   %retval = select i1 %t, i32 -1, i32 0
428   ret i32 %retval
431 define i32 @test20(i32 %x) {
432 ; CHECK-LABEL: @test20(
433 ; CHECK-NEXT:    [[X_LOBIT:%.*]] = ashr i32 [[X:%.*]], 31
434 ; CHECK-NEXT:    ret i32 [[X_LOBIT]]
436   %t = icmp slt i32 %x, 0
437   %retval = select i1 %t, i32 -1, i32 0
438   ret i32 %retval
441 define i64 @test21(i32 %x) {
442 ; CHECK-LABEL: @test21(
443 ; CHECK-NEXT:    [[X_LOBIT:%.*]] = ashr i32 [[X:%.*]], 31
444 ; CHECK-NEXT:    [[TMP1:%.*]] = sext i32 [[X_LOBIT]] to i64
445 ; CHECK-NEXT:    ret i64 [[TMP1]]
447   %t = icmp slt i32 %x, 0
448   %retval = select i1 %t, i64 -1, i64 0
449   ret i64 %retval
452 define i16 @test22(i32 %x) {
453 ; CHECK-LABEL: @test22(
454 ; CHECK-NEXT:    [[X_LOBIT:%.*]] = ashr i32 [[X:%.*]], 31
455 ; CHECK-NEXT:    [[TMP1:%.*]] = trunc i32 [[X_LOBIT]] to i16
456 ; CHECK-NEXT:    ret i16 [[TMP1]]
458   %t = icmp slt i32 %x, 0
459   %retval = select i1 %t, i16 -1, i16 0
460   ret i16 %retval
463 define i32 @test25(i1 %c)  {
464 ; CHECK-LABEL: @test25(
465 ; CHECK-NEXT:  entry:
466 ; CHECK-NEXT:    br i1 [[C:%.*]], label [[JUMP:%.*]], label [[RET:%.*]]
467 ; CHECK:       jump:
468 ; CHECK-NEXT:    br label [[RET]]
469 ; CHECK:       ret:
470 ; CHECK-NEXT:    [[B:%.*]] = phi i32 [ 10, [[JUMP]] ], [ 20, [[ENTRY:%.*]] ]
471 ; CHECK-NEXT:    ret i32 [[B]]
473 entry:
474   br i1 %c, label %jump, label %ret
475 jump:
476   br label %ret
477 ret:
478   %a = phi i1 [true, %jump], [false, %entry]
479   %b = select i1 %a, i32 10, i32 20
480   ret i32 %b
483 define i32 @test26(i1 %cond)  {
484 ; CHECK-LABEL: @test26(
485 ; CHECK-NEXT:  entry:
486 ; CHECK-NEXT:    br i1 [[COND:%.*]], label [[JUMP:%.*]], label [[RET:%.*]]
487 ; CHECK:       jump:
488 ; CHECK-NEXT:    br label [[RET]]
489 ; CHECK:       ret:
490 ; CHECK-NEXT:    [[B:%.*]] = phi i32 [ 10, [[JUMP]] ], [ 20, [[ENTRY:%.*]] ]
491 ; CHECK-NEXT:    ret i32 [[B]]
493 entry:
494   br i1 %cond, label %jump, label %ret
495 jump:
496   %c = or i1 false, false
497   br label %ret
498 ret:
499   %a = phi i1 [true, %entry], [%c, %jump]
500   %b = select i1 %a, i32 20, i32 10
501   ret i32 %b
504 define i32 @test26_logical(i1 %cond)  {
505 ; CHECK-LABEL: @test26_logical(
506 ; CHECK-NEXT:  entry:
507 ; CHECK-NEXT:    br i1 [[COND:%.*]], label [[JUMP:%.*]], label [[RET:%.*]]
508 ; CHECK:       jump:
509 ; CHECK-NEXT:    br label [[RET]]
510 ; CHECK:       ret:
511 ; CHECK-NEXT:    [[B:%.*]] = phi i32 [ 10, [[JUMP]] ], [ 20, [[ENTRY:%.*]] ]
512 ; CHECK-NEXT:    ret i32 [[B]]
514 entry:
515   br i1 %cond, label %jump, label %ret
516 jump:
517   %c = select i1 false, i1 true, i1 false
518   br label %ret
519 ret:
520   %a = phi i1 [true, %entry], [%c, %jump]
521   %b = select i1 %a, i32 20, i32 10
522   ret i32 %b
525 define i32 @test27(i1 %c, i32 %A, i32 %B)  {
526 ; CHECK-LABEL: @test27(
527 ; CHECK-NEXT:  entry:
528 ; CHECK-NEXT:    br i1 [[C:%.*]], label [[JUMP:%.*]], label [[RET:%.*]]
529 ; CHECK:       jump:
530 ; CHECK-NEXT:    br label [[RET]]
531 ; CHECK:       ret:
532 ; CHECK-NEXT:    [[S:%.*]] = phi i32 [ [[A:%.*]], [[JUMP]] ], [ [[B:%.*]], [[ENTRY:%.*]] ]
533 ; CHECK-NEXT:    ret i32 [[S]]
535 entry:
536   br i1 %c, label %jump, label %ret
537 jump:
538   br label %ret
539 ret:
540   %p = phi i1 [true, %jump], [false, %entry]
541   %s = select i1 %p, i32 %A, i32 %B
542   ret i32 %s
545 define i32 @test28(i1 %cond, i32 %A, i32 %B)  {
546 ; CHECK-LABEL: @test28(
547 ; CHECK-NEXT:  entry:
548 ; CHECK-NEXT:    br i1 [[COND:%.*]], label [[JUMP:%.*]], label [[RET:%.*]]
549 ; CHECK:       jump:
550 ; CHECK-NEXT:    br label [[RET]]
551 ; CHECK:       ret:
552 ; CHECK-NEXT:    [[S:%.*]] = phi i32 [ [[A:%.*]], [[JUMP]] ], [ [[B:%.*]], [[ENTRY:%.*]] ]
553 ; CHECK-NEXT:    ret i32 [[S]]
555 entry:
556   br i1 %cond, label %jump, label %ret
557 jump:
558   br label %ret
559 ret:
560   %c = phi i32 [%A, %jump], [%B, %entry]
561   %p = phi i1 [true, %jump], [false, %entry]
562   %s = select i1 %p, i32 %A, i32 %c
563   ret i32 %s
566 define i32 @test29(i1 %cond, i32 %A, i32 %B)  {
567 ; CHECK-LABEL: @test29(
568 ; CHECK-NEXT:  entry:
569 ; CHECK-NEXT:    br i1 [[COND:%.*]], label [[JUMP:%.*]], label [[RET:%.*]]
570 ; CHECK:       jump:
571 ; CHECK-NEXT:    br label [[RET]]
572 ; CHECK:       ret:
573 ; CHECK-NEXT:    [[S:%.*]] = phi i32 [ [[A:%.*]], [[JUMP]] ], [ [[B:%.*]], [[ENTRY:%.*]] ]
574 ; CHECK-NEXT:    br label [[NEXT:%.*]]
575 ; CHECK:       next:
576 ; CHECK-NEXT:    ret i32 [[S]]
578 entry:
579   br i1 %cond, label %jump, label %ret
580 jump:
581   br label %ret
582 ret:
583   %c = phi i32 [%A, %jump], [%B, %entry]
584   %p = phi i1 [true, %jump], [false, %entry]
585   br label %next
587 next:
588   %s = select i1 %p, i32 %A, i32 %c
589   ret i32 %s
592 ; SMAX(SMAX(x, y), x) -> SMAX(x, y)
593 define i32 @test30(i32 %x, i32 %y) {
594 ; CHECK-LABEL: @test30(
595 ; CHECK-NEXT:    [[TMP1:%.*]] = call i32 @llvm.smax.i32(i32 [[X:%.*]], i32 [[Y:%.*]])
596 ; CHECK-NEXT:    ret i32 [[TMP1]]
598   %cmp = icmp sgt i32 %x, %y
599   %cond = select i1 %cmp, i32 %x, i32 %y
600   %cmp5 = icmp sgt i32 %cond, %x
601   %retval = select i1 %cmp5, i32 %cond, i32 %x
602   ret i32 %retval
605 ; UMAX(UMAX(x, y), x) -> UMAX(x, y)
606 define i32 @test31(i32 %x, i32 %y) {
607 ; CHECK-LABEL: @test31(
608 ; CHECK-NEXT:    [[TMP1:%.*]] = call i32 @llvm.umax.i32(i32 [[X:%.*]], i32 [[Y:%.*]])
609 ; CHECK-NEXT:    ret i32 [[TMP1]]
611   %cmp = icmp ugt i32 %x, %y
612   %cond = select i1 %cmp, i32 %x, i32 %y
613   %cmp5 = icmp ugt i32 %cond, %x
614   %retval = select i1 %cmp5, i32 %cond, i32 %x
615   ret i32 %retval
618 ; SMIN(SMIN(x, y), x) -> SMIN(x, y)
619 define i32 @test32(i32 %x, i32 %y) {
620 ; CHECK-LABEL: @test32(
621 ; CHECK-NEXT:    [[TMP1:%.*]] = call i32 @llvm.smin.i32(i32 [[X:%.*]], i32 [[Y:%.*]])
622 ; CHECK-NEXT:    ret i32 [[TMP1]]
624   %cmp = icmp sgt i32 %x, %y
625   %cond = select i1 %cmp, i32 %y, i32 %x
626   %cmp5 = icmp sgt i32 %cond, %x
627   %retval = select i1 %cmp5, i32 %x, i32 %cond
628   ret i32 %retval
631 ; MAX(MIN(x, y), x) -> x
632 define i32 @test33(i32 %x, i32 %y) {
633 ; CHECK-LABEL: @test33(
634 ; CHECK-NEXT:    ret i32 [[X:%.*]]
636   %cmp = icmp sgt i32 %x, %y
637   %cond = select i1 %cmp, i32 %y, i32 %x
638   %cmp5 = icmp sgt i32 %cond, %x
639   %retval = select i1 %cmp5, i32 %cond, i32 %x
640   ret i32 %retval
643 ; MIN(MAX(x, y), x) -> x
644 define i32 @test34(i32 %x, i32 %y) {
645 ; CHECK-LABEL: @test34(
646 ; CHECK-NEXT:    ret i32 [[X:%.*]]
648   %cmp = icmp sgt i32 %x, %y
649   %cond = select i1 %cmp, i32 %x, i32 %y
650   %cmp5 = icmp sgt i32 %cond, %x
651   %retval = select i1 %cmp5, i32 %x, i32 %cond
652   ret i32 %retval
655 define i1 @test38(i1 %cond) {
656 ; CHECK-LABEL: @test38(
657 ; CHECK-NEXT:    ret i1 false
659   %zero = alloca i32
660   %one = alloca i32
661   %ptr = select i1 %cond, i32* %zero, i32* %one
662   %isnull = icmp eq i32* %ptr, null
663   ret i1 %isnull
666 define i1 @test39(i1 %cond, double %x) {
667 ; CHECK-LABEL: @test39(
668 ; CHECK-NEXT:    ret i1 true
670   %s = select i1 %cond, double %x, double 0x7FF0000000000000 ; RHS = +infty
671   %cmp = fcmp ule double %x, %s
672   ret i1 %cmp
675 define i1 @test40(i1 %cond) {
676 ; CHECK-LABEL: @test40(
677 ; CHECK-NEXT:    ret i1 false
679   %a = alloca i32
680   %b = alloca i32
681   %c = alloca i32
682   %s = select i1 %cond, i32* %a, i32* %b
683   %r = icmp eq i32* %s, %c
684   ret i1 %r
687 define i32 @test41(i1 %cond, i32 %x, i32 %y) {
688 ; CHECK-LABEL: @test41(
689 ; CHECK-NEXT:    [[R:%.*]] = and i32 [[X:%.*]], [[Y:%.*]]
690 ; CHECK-NEXT:    ret i32 [[R]]
692   %z = and i32 %x, %y
693   %s = select i1 %cond, i32 %y, i32 %z
694   %r = and i32 %x, %s
695   ret i32 %r
698 define i32 @test42(i32 %x, i32 %y) {
699 ; CHECK-LABEL: @test42(
700 ; CHECK-NEXT:    [[COND:%.*]] = icmp eq i32 [[X:%.*]], 0
701 ; CHECK-NEXT:    [[B:%.*]] = sext i1 [[COND]] to i32
702 ; CHECK-NEXT:    [[C:%.*]] = add i32 [[B]], [[Y:%.*]]
703 ; CHECK-NEXT:    ret i32 [[C]]
705   %b = add i32 %y, -1
706   %cond = icmp eq i32 %x, 0
707   %c = select i1 %cond, i32 %b, i32 %y
708   ret i32 %c
711 define <2 x i32> @test42vec(<2 x i32> %x, <2 x i32> %y) {
712 ; CHECK-LABEL: @test42vec(
713 ; CHECK-NEXT:    [[COND:%.*]] = icmp eq <2 x i32> [[X:%.*]], zeroinitializer
714 ; CHECK-NEXT:    [[B:%.*]] = sext <2 x i1> [[COND]] to <2 x i32>
715 ; CHECK-NEXT:    [[C:%.*]] = add <2 x i32> [[B]], [[Y:%.*]]
716 ; CHECK-NEXT:    ret <2 x i32> [[C]]
718   %b = add <2 x i32> %y, <i32 -1, i32 -1>
719   %cond = icmp eq <2 x i32> %x, zeroinitializer
720   %c = select <2 x i1> %cond, <2 x i32> %b, <2 x i32> %y
721   ret <2 x i32> %c
724 ; PR8994
726 ; This select instruction can't be eliminated because trying to do so would
727 ; change the number of vector elements. This used to assert.
728 define i48 @test51(<3 x i1> %icmp, <3 x i16> %t) {
729 ; CHECK-LABEL: @test51(
730 ; CHECK-NEXT:    [[SELECT:%.*]] = select <3 x i1> [[ICMP:%.*]], <3 x i16> zeroinitializer, <3 x i16> [[T:%.*]]
731 ; CHECK-NEXT:    [[T2:%.*]] = bitcast <3 x i16> [[SELECT]] to i48
732 ; CHECK-NEXT:    ret i48 [[T2]]
734   %select = select <3 x i1> %icmp, <3 x i16> zeroinitializer, <3 x i16> %t
735   %t2 = bitcast <3 x i16> %select to i48
736   ret i48 %t2
739 define <vscale x 4 x float> @bitcast_select_bitcast(<vscale x 4 x i1> %icmp, <vscale x 4 x i32> %a, <vscale x 4 x float> %b) {
740 ; CHECK-LABEL: @bitcast_select_bitcast(
741 ; CHECK-NEXT:    [[TMP1:%.*]] = bitcast <vscale x 4 x i32> [[A:%.*]] to <vscale x 4 x float>
742 ; CHECK-NEXT:    [[BC2:%.*]] = select <vscale x 4 x i1> [[ICMP:%.*]], <vscale x 4 x float> [[B:%.*]], <vscale x 4 x float> [[TMP1]]
743 ; CHECK-NEXT:    ret <vscale x 4 x float> [[BC2]]
745   %bc1 = bitcast <vscale x 4 x float> %b to <vscale x 4 x i32>
746   %select = select <vscale x 4 x i1> %icmp, <vscale x 4 x i32> %bc1, <vscale x 4 x i32> %a
747   %bc2 = bitcast <vscale x 4 x i32> %select to <vscale x 4 x float>
748   ret <vscale x 4 x float> %bc2
751 define void @select_oneuse_bitcast(<vscale x 4 x float> %a, <vscale x 4 x float> %b, <vscale x 4 x i32> %c, <vscale x 4 x i32> %d, <vscale x 4 x i32>* %ptr1) {
752 ; CHECK-LABEL: @select_oneuse_bitcast(
753 ; CHECK-NEXT:    [[CMP:%.*]] = icmp ult <vscale x 4 x i32> [[C:%.*]], [[D:%.*]]
754 ; CHECK-NEXT:    [[SEL1_V:%.*]] = select <vscale x 4 x i1> [[CMP]], <vscale x 4 x float> [[A:%.*]], <vscale x 4 x float> [[B:%.*]]
755 ; CHECK-NEXT:    [[TMP1:%.*]] = bitcast <vscale x 4 x i32>* [[PTR1:%.*]] to <vscale x 4 x float>*
756 ; CHECK-NEXT:    store <vscale x 4 x float> [[SEL1_V]], <vscale x 4 x float>* [[TMP1]], align 16
757 ; CHECK-NEXT:    ret void
759   %cmp = icmp ult <vscale x 4 x i32> %c, %d
760   %bc1 = bitcast <vscale x 4 x float> %a to <vscale x 4 x i32>
761   %bc2 = bitcast <vscale x 4 x float> %b to <vscale x 4 x i32>
762   %sel1 = select <vscale x 4 x i1> %cmp, <vscale x 4 x i32> %bc1, <vscale x 4 x i32> %bc2
763   store <vscale x 4 x i32> %sel1, <vscale x 4 x i32>* %ptr1
764   ret void
767 ; Allow select promotion even if there are multiple uses of bitcasted ops.
768 ; Hoisting the selects allows later pattern matching to see that these are min/max ops.
770 define void @min_max_bitcast(<4 x float> %a, <4 x float> %b, <4 x i32>* %ptr1, <4 x i32>* %ptr2) {
771 ; CHECK-LABEL: @min_max_bitcast(
772 ; CHECK-NEXT:    [[CMP:%.*]] = fcmp olt <4 x float> [[A:%.*]], [[B:%.*]]
773 ; CHECK-NEXT:    [[SEL1_V:%.*]] = select <4 x i1> [[CMP]], <4 x float> [[A]], <4 x float> [[B]]
774 ; CHECK-NEXT:    [[SEL2_V:%.*]] = select <4 x i1> [[CMP]], <4 x float> [[B]], <4 x float> [[A]]
775 ; CHECK-NEXT:    [[TMP1:%.*]] = bitcast <4 x i32>* [[PTR1:%.*]] to <4 x float>*
776 ; CHECK-NEXT:    store <4 x float> [[SEL1_V]], <4 x float>* [[TMP1]], align 16
777 ; CHECK-NEXT:    [[TMP2:%.*]] = bitcast <4 x i32>* [[PTR2:%.*]] to <4 x float>*
778 ; CHECK-NEXT:    store <4 x float> [[SEL2_V]], <4 x float>* [[TMP2]], align 16
779 ; CHECK-NEXT:    ret void
781   %cmp = fcmp olt <4 x float> %a, %b
782   %bc1 = bitcast <4 x float> %a to <4 x i32>
783   %bc2 = bitcast <4 x float> %b to <4 x i32>
784   %sel1 = select <4 x i1> %cmp, <4 x i32> %bc1, <4 x i32> %bc2
785   %sel2 = select <4 x i1> %cmp, <4 x i32> %bc2, <4 x i32> %bc1
786   store <4 x i32> %sel1, <4 x i32>* %ptr1
787   store <4 x i32> %sel2, <4 x i32>* %ptr2
788   ret void
791 define void @min_max_bitcast1(<vscale x 4 x float> %a, <vscale x 4 x float> %b, <vscale x 4 x i32>* %ptr1, <vscale x 4 x i32>* %ptr2) {
792 ; CHECK-LABEL: @min_max_bitcast1(
793 ; CHECK-NEXT:    [[CMP:%.*]] = fcmp olt <vscale x 4 x float> [[A:%.*]], [[B:%.*]]
794 ; CHECK-NEXT:    [[SEL1_V:%.*]] = select <vscale x 4 x i1> [[CMP]], <vscale x 4 x float> [[A]], <vscale x 4 x float> [[B]]
795 ; CHECK-NEXT:    [[SEL2_V:%.*]] = select <vscale x 4 x i1> [[CMP]], <vscale x 4 x float> [[B]], <vscale x 4 x float> [[A]]
796 ; CHECK-NEXT:    [[TMP1:%.*]] = bitcast <vscale x 4 x i32>* [[PTR1:%.*]] to <vscale x 4 x float>*
797 ; CHECK-NEXT:    store <vscale x 4 x float> [[SEL1_V]], <vscale x 4 x float>* [[TMP1]], align 16
798 ; CHECK-NEXT:    [[TMP2:%.*]] = bitcast <vscale x 4 x i32>* [[PTR2:%.*]] to <vscale x 4 x float>*
799 ; CHECK-NEXT:    store <vscale x 4 x float> [[SEL2_V]], <vscale x 4 x float>* [[TMP2]], align 16
800 ; CHECK-NEXT:    ret void
802   %cmp = fcmp olt <vscale x 4 x float> %a, %b
803   %bc1 = bitcast <vscale x 4 x float> %a to <vscale x 4 x i32>
804   %bc2 = bitcast <vscale x 4 x float> %b to <vscale x 4 x i32>
805   %sel1 = select <vscale x 4 x i1> %cmp, <vscale x 4 x i32> %bc1, <vscale x 4 x i32> %bc2
806   %sel2 = select <vscale x 4 x i1> %cmp, <vscale x 4 x i32> %bc2, <vscale x 4 x i32> %bc1
807   store <vscale x 4 x i32> %sel1, <vscale x 4 x i32>* %ptr1
808   store <vscale x 4 x i32> %sel2, <vscale x 4 x i32>* %ptr2
809   ret void
812 ; To avoid potential backend problems, we don't do the same transform for other casts.
814 define void @truncs_before_selects(<4 x float> %f1, <4 x float> %f2, <4 x i64> %a, <4 x i64> %b, <4 x i32>* %ptr1, <4 x i32>* %ptr2) {
815 ; CHECK-LABEL: @truncs_before_selects(
816 ; CHECK-NEXT:    [[CMP:%.*]] = fcmp olt <4 x float> [[F1:%.*]], [[F2:%.*]]
817 ; CHECK-NEXT:    [[BC1:%.*]] = trunc <4 x i64> [[A:%.*]] to <4 x i32>
818 ; CHECK-NEXT:    [[BC2:%.*]] = trunc <4 x i64> [[B:%.*]] to <4 x i32>
819 ; CHECK-NEXT:    [[SEL1:%.*]] = select <4 x i1> [[CMP]], <4 x i32> [[BC1]], <4 x i32> [[BC2]]
820 ; CHECK-NEXT:    [[SEL2:%.*]] = select <4 x i1> [[CMP]], <4 x i32> [[BC2]], <4 x i32> [[BC1]]
821 ; CHECK-NEXT:    store <4 x i32> [[SEL1]], <4 x i32>* [[PTR1:%.*]], align 16
822 ; CHECK-NEXT:    store <4 x i32> [[SEL2]], <4 x i32>* [[PTR2:%.*]], align 16
823 ; CHECK-NEXT:    ret void
825   %cmp = fcmp olt <4 x float> %f1, %f2
826   %bc1 = trunc <4 x i64> %a to <4 x i32>
827   %bc2 = trunc <4 x i64> %b to <4 x i32>
828   %sel1 = select <4 x i1> %cmp, <4 x i32> %bc1, <4 x i32> %bc2
829   %sel2 = select <4 x i1> %cmp, <4 x i32> %bc2, <4 x i32> %bc1
830   store <4 x i32> %sel1, <4 x i32>* %ptr1, align 16
831   store <4 x i32> %sel2, <4 x i32>* %ptr2, align 16
832   ret void
835 ; PR8575
837 define i32 @test52(i32 %n, i32 %m) {
838 ; CHECK-LABEL: @test52(
839 ; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt i32 [[N:%.*]], [[M:%.*]]
840 ; CHECK-NEXT:    [[STOREMERGE:%.*]] = select i1 [[CMP]], i32 1, i32 6
841 ; CHECK-NEXT:    ret i32 [[STOREMERGE]]
843   %cmp = icmp sgt i32 %n, %m
844   %. = select i1 %cmp, i32 1, i32 3
845   %add = add nsw i32 %., 3
846   %storemerge = select i1 %cmp, i32 %., i32 %add
847   ret i32 %storemerge
850 ; PR9454
852 define i32 @test53(i32 %x) {
853 ; CHECK-LABEL: @test53(
854 ; CHECK-NEXT:    [[AND:%.*]] = and i32 [[X:%.*]], 2
855 ; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[AND]], [[X]]
856 ; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[CMP]], i32 2, i32 1
857 ; CHECK-NEXT:    ret i32 [[SEL]]
859   %and = and i32 %x, 2
860   %cmp = icmp eq i32 %and, %x
861   %sel = select i1 %cmp, i32 2, i32 1
862   ret i32 %sel
865 define i32 @test54(i32 %X, i32 %Y) {
866 ; CHECK-LABEL: @test54(
867 ; CHECK-NEXT:    [[B:%.*]] = icmp ne i32 [[X:%.*]], 0
868 ; CHECK-NEXT:    [[C:%.*]] = zext i1 [[B]] to i32
869 ; CHECK-NEXT:    ret i32 [[C]]
871   %A = ashr exact i32 %X, %Y
872   %B = icmp eq i32 %A, 0
873   %C = select i1 %B, i32 %A, i32 1
874   ret i32 %C
877 define i1 @test55(i1 %X, i32 %Y, i32 %Z) {
878 ; CHECK-LABEL: @test55(
879 ; CHECK-NEXT:    [[C:%.*]] = icmp eq i32 [[Y:%.*]], 0
880 ; CHECK-NEXT:    ret i1 [[C]]
882   %A = ashr exact i32 %Y, %Z
883   %B = select i1 %X, i32 %Y, i32 %A
884   %C = icmp eq i32 %B, 0
885   ret i1 %C
888 define i32 @test56(i16 %x) {
889 ; CHECK-LABEL: @test56(
890 ; CHECK-NEXT:    [[CONV:%.*]] = zext i16 [[X:%.*]] to i32
891 ; CHECK-NEXT:    ret i32 [[CONV]]
893   %tobool = icmp eq i16 %x, 0
894   %conv = zext i16 %x to i32
895   %cond = select i1 %tobool, i32 0, i32 %conv
896   ret i32 %cond
899 define i32 @test57(i32 %x, i32 %y) {
900 ; CHECK-LABEL: @test57(
901 ; CHECK-NEXT:    [[AND:%.*]] = and i32 [[X:%.*]], [[Y:%.*]]
902 ; CHECK-NEXT:    [[TOBOOL:%.*]] = icmp eq i32 [[X]], 0
903 ; CHECK-NEXT:    [[DOTAND:%.*]] = select i1 [[TOBOOL]], i32 0, i32 [[AND]]
904 ; CHECK-NEXT:    ret i32 [[DOTAND]]
906   %and = and i32 %x, %y
907   %tobool = icmp eq i32 %x, 0
908   %.and = select i1 %tobool, i32 0, i32 %and
909   ret i32 %.and
912 define i32 @test58(i16 %x) {
913 ; CHECK-LABEL: @test58(
914 ; CHECK-NEXT:    [[CONV:%.*]] = zext i16 [[X:%.*]] to i32
915 ; CHECK-NEXT:    ret i32 [[CONV]]
917   %tobool = icmp ne i16 %x, 1
918   %conv = zext i16 %x to i32
919   %cond = select i1 %tobool, i32 %conv, i32 1
920   ret i32 %cond
923 define i32 @test59(i32 %x, i32 %y) {
924 ; CHECK-LABEL: @test59(
925 ; CHECK-NEXT:    [[AND:%.*]] = and i32 [[X:%.*]], [[Y:%.*]]
926 ; CHECK-NEXT:    ret i32 [[AND]]
928   %and = and i32 %x, %y
929   %tobool = icmp ne i32 %x, %y
930   %.and = select i1 %tobool, i32 %and, i32 %y
931   ret i32 %.and
934 define i1 @test60(i32 %x, i1* %y) {
935 ; CHECK-LABEL: @test60(
936 ; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[X:%.*]], 0
937 ; CHECK-NEXT:    [[LOAD:%.*]] = load i1, i1* [[Y:%.*]], align 1
938 ; CHECK-NEXT:    [[CMP1:%.*]] = icmp slt i32 [[X]], 1
939 ; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[CMP]], i1 [[LOAD]], i1 [[CMP1]]
940 ; CHECK-NEXT:    ret i1 [[SEL]]
942   %cmp = icmp eq i32 %x, 0
943   %load = load i1, i1* %y, align 1
944   %cmp1 = icmp slt i32 %x, 1
945   %sel = select i1 %cmp, i1 %load, i1 %cmp1
946   ret i1 %sel
949 @glbl = constant i32 10
950 define i32 @test61(i32* %ptr) {
951 ; CHECK-LABEL: @test61(
952 ; CHECK-NEXT:    ret i32 10
954   %A = load i32, i32* %ptr
955   %B = icmp eq i32* %ptr, @glbl
956   %C = select i1 %B, i32 %A, i32 10
957   ret i32 %C
960 ; PR14131
961 define void @test64(i32 %p, i16 %b, i1 %c1) noreturn {
962 ; CHECK-LABEL: @test64(
963 ; CHECK-NEXT:  entry:
964 ; CHECK-NEXT:    br i1 [[C1:%.*]], label [[LOR_RHS:%.*]], label [[LOR_END:%.*]]
965 ; CHECK:       lor.rhs:
966 ; CHECK-NEXT:    br label [[LOR_END]]
967 ; CHECK:       lor.end:
968 ; CHECK-NEXT:    br i1 true, label [[COND_END17:%.*]], label [[COND_FALSE16:%.*]]
969 ; CHECK:       cond.false16:
970 ; CHECK-NEXT:    br label [[COND_END17]]
971 ; CHECK:       cond.end17:
972 ; CHECK-NEXT:    br label [[WHILE_BODY:%.*]]
973 ; CHECK:       while.body:
974 ; CHECK-NEXT:    br label [[WHILE_BODY]]
976 entry:
977   %p.addr.0.insert.mask = and i32 %p, -65536
978   %conv2 = and i32 %p, 65535
979   br i1 %c1, label %lor.rhs, label %lor.end
981 lor.rhs:
982   %p.addr.0.extract.trunc = trunc i32 %p.addr.0.insert.mask to i16
983   %phitmp = zext i16 %p.addr.0.extract.trunc to i32
984   br label %lor.end
986 lor.end:
987   %t.1 = phi i32 [ 0, %entry ], [ %phitmp, %lor.rhs ]
988   %conv6 = zext i16 %b to i32
989   %div = udiv i32 %conv6, %t.1
990   %tobool8 = icmp eq i32 %div, 0
991   %cmp = icmp eq i32 %t.1, 0
992   %cmp12 = icmp ult i32 %conv2, 2
993   %cmp.sink = select i1 %tobool8, i1 %cmp12, i1 %cmp
994   br i1 %cmp.sink, label %cond.end17, label %cond.false16
996 cond.false16:
997   br label %cond.end17
999 cond.end17:
1000   br label %while.body
1002 while.body:
1003   br label %while.body
1006 @under_aligned = external global i32, align 1
1008 ; The load here must not be speculated around the select. One side of the
1009 ; select is trivially dereferenceable but may have a lower alignment than the
1010 ; load does.
1011 define i32 @test76(i1 %flag, i32* %x) {
1012 ; CHECK-LABEL: @test76(
1013 ; CHECK-NEXT:    store i32 0, i32* [[X:%.*]], align 4
1014 ; CHECK-NEXT:    [[P:%.*]] = select i1 [[FLAG:%.*]], i32* @under_aligned, i32* [[X]]
1015 ; CHECK-NEXT:    [[V:%.*]] = load i32, i32* [[P]], align 4
1016 ; CHECK-NEXT:    ret i32 [[V]]
1018   store i32 0, i32* %x
1019   %p = select i1 %flag, i32* @under_aligned, i32* %x
1020   %v = load i32, i32* %p
1021   ret i32 %v
1024 declare void @scribble_on_i32(i32*)
1026 ; The load here must not be speculated around the select. One side of the
1027 ; select is trivially dereferenceable but may have a lower alignment than the
1028 ; load does.
1030 define i32 @test77(i1 %flag, i32* %x) {
1031 ; CHECK-LABEL: @test77(
1032 ; CHECK-NEXT:    [[UNDER_ALIGNED:%.*]] = alloca i32, align 1
1033 ; CHECK-NEXT:    call void @scribble_on_i32(i32* nonnull [[UNDER_ALIGNED]])
1034 ; CHECK-NEXT:    store i32 0, i32* [[X:%.*]], align 4
1035 ; CHECK-NEXT:    [[P:%.*]] = select i1 [[FLAG:%.*]], i32* [[UNDER_ALIGNED]], i32* [[X]]
1036 ; CHECK-NEXT:    [[V:%.*]] = load i32, i32* [[P]], align 4
1037 ; CHECK-NEXT:    ret i32 [[V]]
1039   %under_aligned = alloca i32, align 1
1040   call void @scribble_on_i32(i32* %under_aligned)
1041   store i32 0, i32* %x
1042   %p = select i1 %flag, i32* %under_aligned, i32* %x
1043   %v = load i32, i32* %p
1044   ret i32 %v
1047 define i32 @test78(i1 %flag, i32* %x, i32* %y, i32* %z) {
1048 ; Test that we can speculate the loads around the select even when we can't
1049 ; fold the load completely away.
1050 ; CHECK-LABEL: @test78(
1051 ; CHECK-NEXT:  entry:
1052 ; CHECK-NEXT:    store i32 0, i32* [[X:%.*]], align 4
1053 ; CHECK-NEXT:    store i32 0, i32* [[Y:%.*]], align 4
1054 ; CHECK-NEXT:    store i32 42, i32* [[Z:%.*]], align 4
1055 ; CHECK-NEXT:    [[X_VAL:%.*]] = load i32, i32* [[X]], align 4
1056 ; CHECK-NEXT:    [[Y_VAL:%.*]] = load i32, i32* [[Y]], align 4
1057 ; CHECK-NEXT:    [[V:%.*]] = select i1 [[FLAG:%.*]], i32 [[X_VAL]], i32 [[Y_VAL]]
1058 ; CHECK-NEXT:    ret i32 [[V]]
1060 entry:
1061   store i32 0, i32* %x
1062   store i32 0, i32* %y
1063   ; Block forwarding by storing to %z which could alias either %x or %y.
1064   store i32 42, i32* %z
1065   %p = select i1 %flag, i32* %x, i32* %y
1066   %v = load i32, i32* %p
1067   ret i32 %v
1070 ; Test that we can speculate the loads around the select even when we can't
1071 ; fold the load completely away.
1072 define i32 @test78_deref(i1 %flag, i32* dereferenceable(4) align 4 %x, i32* dereferenceable(4) align 4 %y, i32* %z) nofree nosync {
1073 ; CHECK-LABEL: @test78_deref(
1074 ; CHECK-NEXT:    [[X_VAL:%.*]] = load i32, i32* [[X:%.*]], align 4
1075 ; CHECK-NEXT:    [[Y_VAL:%.*]] = load i32, i32* [[Y:%.*]], align 4
1076 ; CHECK-NEXT:    [[V:%.*]] = select i1 [[FLAG:%.*]], i32 [[X_VAL]], i32 [[Y_VAL]]
1077 ; CHECK-NEXT:    ret i32 [[V]]
1079   %p = select i1 %flag, i32* %x, i32* %y
1080   %v = load i32, i32* %p
1081   ret i32 %v
1084 ; The same as @test78 but we can't speculate the load because it can trap
1085 ; if under-aligned.
1086 define i32 @test78_neg(i1 %flag, i32* %x, i32* %y, i32* %z) {
1087 ; CHECK-LABEL: @test78_neg(
1088 ; CHECK-NEXT:    store i32 0, i32* [[X:%.*]], align 4
1089 ; CHECK-NEXT:    store i32 0, i32* [[Y:%.*]], align 4
1090 ; CHECK-NEXT:    store i32 42, i32* [[Z:%.*]], align 4
1091 ; CHECK-NEXT:    [[P:%.*]] = select i1 [[FLAG:%.*]], i32* [[X]], i32* [[Y]]
1092 ; CHECK-NEXT:    [[V:%.*]] = load i32, i32* [[P]], align 16
1093 ; CHECK-NEXT:    ret i32 [[V]]
1095   store i32 0, i32* %x
1096   store i32 0, i32* %y
1097   ; Block forwarding by storing to %z which could alias either %x or %y.
1098   store i32 42, i32* %z
1099   %p = select i1 %flag, i32* %x, i32* %y
1100   %v = load i32, i32* %p, align 16
1101   ret i32 %v
1104 ; The same as @test78_deref but we can't speculate the load because
1105 ; one of the arguments is not sufficiently dereferenceable.
1106 define i32 @test78_deref_neg(i1 %flag, i32* dereferenceable(2) %x, i32* dereferenceable(4) %y, i32* %z) nofree nosync {
1107 ; CHECK-LABEL: @test78_deref_neg(
1108 ; CHECK-NEXT:    [[P:%.*]] = select i1 [[FLAG:%.*]], i32* [[X:%.*]], i32* [[Y:%.*]]
1109 ; CHECK-NEXT:    [[V:%.*]] = load i32, i32* [[P]], align 4
1110 ; CHECK-NEXT:    ret i32 [[V]]
1112   %p = select i1 %flag, i32* %x, i32* %y
1113   %v = load i32, i32* %p
1114   ret i32 %v
1117 ; Test that we can speculate the loads around the select even when we can't
1118 ; fold the load completely away.
1119 define float @test79(i1 %flag, float* %x, i32* %y, i32* %z) {
1120 ; CHECK-LABEL: @test79(
1121 ; CHECK-NEXT:    [[X1:%.*]] = bitcast float* [[X:%.*]] to i32*
1122 ; CHECK-NEXT:    [[Y1:%.*]] = bitcast i32* [[Y:%.*]] to float*
1123 ; CHECK-NEXT:    store i32 0, i32* [[X1]], align 4
1124 ; CHECK-NEXT:    store i32 0, i32* [[Y]], align 4
1125 ; CHECK-NEXT:    store i32 42, i32* [[Z:%.*]], align 4
1126 ; CHECK-NEXT:    [[X_VAL:%.*]] = load float, float* [[X]], align 4
1127 ; CHECK-NEXT:    [[Y1_VAL:%.*]] = load float, float* [[Y1]], align 4
1128 ; CHECK-NEXT:    [[V:%.*]] = select i1 [[FLAG:%.*]], float [[X_VAL]], float [[Y1_VAL]]
1129 ; CHECK-NEXT:    ret float [[V]]
1131   %x1 = bitcast float* %x to i32*
1132   %y1 = bitcast i32* %y to float*
1133   store i32 0, i32* %x1
1134   store i32 0, i32* %y
1135   ; Block forwarding by storing to %z which could alias either %x or %y.
1136   store i32 42, i32* %z
1137   %p = select i1 %flag, float* %x, float* %y1
1138   %v = load float, float* %p
1139   ret float %v
1142 ; Test that when we speculate the loads around the select they fold throug
1143 ; load->load folding and load->store folding.
1144 define i32 @test80(i1 %flag) {
1145 ; CHECK-LABEL: @test80(
1146 ; CHECK-NEXT:    [[X:%.*]] = alloca i32, align 4
1147 ; CHECK-NEXT:    [[Y:%.*]] = alloca i32, align 4
1148 ; CHECK-NEXT:    call void @scribble_on_i32(i32* nonnull [[X]])
1149 ; CHECK-NEXT:    call void @scribble_on_i32(i32* nonnull [[Y]])
1150 ; CHECK-NEXT:    [[T:%.*]] = load i32, i32* [[X]], align 4
1151 ; CHECK-NEXT:    store i32 [[T]], i32* [[Y]], align 4
1152 ; CHECK-NEXT:    ret i32 [[T]]
1154   %x = alloca i32
1155   %y = alloca i32
1156   call void @scribble_on_i32(i32* %x)
1157   call void @scribble_on_i32(i32* %y)
1158   %t = load i32, i32* %x
1159   store i32 %t, i32* %y
1160   %p = select i1 %flag, i32* %x, i32* %y
1161   %v = load i32, i32* %p
1162   ret i32 %v
1165 ; Test that we can speculate the load around the select even though they use
1166 ; differently typed pointers.
1167 define float @test81(i1 %flag) {
1168 ; CHECK-LABEL: @test81(
1169 ; CHECK-NEXT:    [[X:%.*]] = alloca i32, align 4
1170 ; CHECK-NEXT:    [[Y:%.*]] = alloca i32, align 4
1171 ; CHECK-NEXT:    call void @scribble_on_i32(i32* nonnull [[X]])
1172 ; CHECK-NEXT:    call void @scribble_on_i32(i32* nonnull [[Y]])
1173 ; CHECK-NEXT:    [[T:%.*]] = load i32, i32* [[X]], align 4
1174 ; CHECK-NEXT:    store i32 [[T]], i32* [[Y]], align 4
1175 ; CHECK-NEXT:    [[V:%.*]] = bitcast i32 [[T]] to float
1176 ; CHECK-NEXT:    ret float [[V]]
1178   %x = alloca float
1179   %y = alloca i32
1180   %x1 = bitcast float* %x to i32*
1181   %y1 = bitcast i32* %y to float*
1182   call void @scribble_on_i32(i32* %x1)
1183   call void @scribble_on_i32(i32* %y)
1184   %t = load i32, i32* %x1
1185   store i32 %t, i32* %y
1186   %p = select i1 %flag, float* %x, float* %y1
1187   %v = load float, float* %p
1188   ret float %v
1191 ; Test that we can speculate the load around the select even though they use
1192 ; differently typed pointers.
1193 define i32 @test82(i1 %flag) {
1194 ; CHECK-LABEL: @test82(
1195 ; CHECK-NEXT:    [[X:%.*]] = alloca float, align 4
1196 ; CHECK-NEXT:    [[Y:%.*]] = alloca i32, align 4
1197 ; CHECK-NEXT:    [[X1:%.*]] = bitcast float* [[X]] to i32*
1198 ; CHECK-NEXT:    [[Y1:%.*]] = bitcast i32* [[Y]] to float*
1199 ; CHECK-NEXT:    call void @scribble_on_i32(i32* nonnull [[X1]])
1200 ; CHECK-NEXT:    call void @scribble_on_i32(i32* nonnull [[Y]])
1201 ; CHECK-NEXT:    [[T:%.*]] = load float, float* [[X]], align 4
1202 ; CHECK-NEXT:    store float [[T]], float* [[Y1]], align 4
1203 ; CHECK-NEXT:    [[V:%.*]] = bitcast float [[T]] to i32
1204 ; CHECK-NEXT:    ret i32 [[V]]
1206   %x = alloca float
1207   %y = alloca i32
1208   %x1 = bitcast float* %x to i32*
1209   %y1 = bitcast i32* %y to float*
1210   call void @scribble_on_i32(i32* %x1)
1211   call void @scribble_on_i32(i32* %y)
1212   %t = load float, float* %x
1213   store float %t, float* %y1
1214   %p = select i1 %flag, i32* %x1, i32* %y
1215   %v = load i32, i32* %p
1216   ret i32 %v
1219 declare void @scribble_on_i64(i64*)
1220 declare void @scribble_on_i128(i128*)
1222 ; Test that we can speculate the load around the select even though they use
1223 ; differently typed pointers and requires inttoptr casts.
1224 define i8* @test83(i1 %flag) {
1225 ; CHECK-LABEL: @test83(
1226 ; CHECK-NEXT:    [[X:%.*]] = alloca i8*, align 8
1227 ; CHECK-NEXT:    [[Y:%.*]] = alloca i8*, align 8
1228 ; CHECK-NEXT:    [[TMPCAST:%.*]] = bitcast i8** [[Y]] to i64*
1229 ; CHECK-NEXT:    [[X1:%.*]] = bitcast i8** [[X]] to i64*
1230 ; CHECK-NEXT:    call void @scribble_on_i64(i64* nonnull [[X1]])
1231 ; CHECK-NEXT:    call void @scribble_on_i64(i64* nonnull [[TMPCAST]])
1232 ; CHECK-NEXT:    [[T:%.*]] = load i64, i64* [[X1]], align 8
1233 ; CHECK-NEXT:    store i64 [[T]], i64* [[TMPCAST]], align 8
1234 ; CHECK-NEXT:    [[V:%.*]] = inttoptr i64 [[T]] to i8*
1235 ; CHECK-NEXT:    ret i8* [[V]]
1237   %x = alloca i8*
1238   %y = alloca i64
1239   %x1 = bitcast i8** %x to i64*
1240   %y1 = bitcast i64* %y to i8**
1241   call void @scribble_on_i64(i64* %x1)
1242   call void @scribble_on_i64(i64* %y)
1243   %t = load i64, i64* %x1
1244   store i64 %t, i64* %y
1245   %p = select i1 %flag, i8** %x, i8** %y1
1246   %v = load i8*, i8** %p
1247   ret i8* %v
1250 ; Test that we can speculate the load around the select even though they use
1251 ; differently typed pointers and requires a ptrtoint cast.
1252 define i64 @test84(i1 %flag) {
1253 ; CHECK-LABEL: @test84(
1254 ; CHECK-NEXT:    [[X:%.*]] = alloca i8*, align 8
1255 ; CHECK-NEXT:    [[Y:%.*]] = alloca i8*, align 8
1256 ; CHECK-NEXT:    [[TMPCAST:%.*]] = bitcast i8** [[Y]] to i64*
1257 ; CHECK-NEXT:    [[X1:%.*]] = bitcast i8** [[X]] to i64*
1258 ; CHECK-NEXT:    call void @scribble_on_i64(i64* nonnull [[X1]])
1259 ; CHECK-NEXT:    call void @scribble_on_i64(i64* nonnull [[TMPCAST]])
1260 ; CHECK-NEXT:    [[T:%.*]] = load i8*, i8** [[X]], align 8
1261 ; CHECK-NEXT:    store i8* [[T]], i8** [[Y]], align 8
1262 ; CHECK-NEXT:    [[V:%.*]] = ptrtoint i8* [[T]] to i64
1263 ; CHECK-NEXT:    ret i64 [[V]]
1265   %x = alloca i8*
1266   %y = alloca i64
1267   %x1 = bitcast i8** %x to i64*
1268   %y1 = bitcast i64* %y to i8**
1269   call void @scribble_on_i64(i64* %x1)
1270   call void @scribble_on_i64(i64* %y)
1271   %t = load i8*, i8** %x
1272   store i8* %t, i8** %y1
1273   %p = select i1 %flag, i64* %x1, i64* %y
1274   %v = load i64, i64* %p
1275   ret i64 %v
1278 ; Test that we can't speculate the load around the select. The load of the
1279 ; pointer doesn't load all of the stored integer bits. We could fix this, but it
1280 ; would require endianness checks and other nastiness.
1281 define i8* @test85(i1 %flag) {
1282 ; CHECK-LABEL: @test85(
1283 ; CHECK-NEXT:    [[X1:%.*]] = alloca [2 x i8*], align 8
1284 ; CHECK-NEXT:    [[Y:%.*]] = alloca i128, align 8
1285 ; CHECK-NEXT:    [[X1_SUB:%.*]] = getelementptr inbounds [2 x i8*], [2 x i8*]* [[X1]], i64 0, i64 0
1286 ; CHECK-NEXT:    [[X2:%.*]] = bitcast [2 x i8*]* [[X1]] to i128*
1287 ; CHECK-NEXT:    [[Y1:%.*]] = bitcast i128* [[Y]] to i8**
1288 ; CHECK-NEXT:    call void @scribble_on_i128(i128* nonnull [[X2]])
1289 ; CHECK-NEXT:    call void @scribble_on_i128(i128* nonnull [[Y]])
1290 ; CHECK-NEXT:    [[T:%.*]] = load i128, i128* [[X2]], align 8
1291 ; CHECK-NEXT:    store i128 [[T]], i128* [[Y]], align 8
1292 ; CHECK-NEXT:    [[X1_SUB_VAL:%.*]] = load i8*, i8** [[X1_SUB]], align 8
1293 ; CHECK-NEXT:    [[Y1_VAL:%.*]] = load i8*, i8** [[Y1]], align 8
1294 ; CHECK-NEXT:    [[V:%.*]] = select i1 [[FLAG:%.*]], i8* [[X1_SUB_VAL]], i8* [[Y1_VAL]]
1295 ; CHECK-NEXT:    ret i8* [[V]]
1297   %x = alloca [2 x i8*]
1298   %y = alloca i128
1299   %x1 = bitcast [2 x i8*]* %x to i8**
1300   %x2 = bitcast i8** %x1 to i128*
1301   %y1 = bitcast i128* %y to i8**
1302   call void @scribble_on_i128(i128* %x2)
1303   call void @scribble_on_i128(i128* %y)
1304   %t = load i128, i128* %x2
1305   store i128 %t, i128* %y
1306   %p = select i1 %flag, i8** %x1, i8** %y1
1307   %v = load i8*, i8** %p
1308   ret i8* %v
1311 ; Test that we can't speculate the load around the select when the integer size
1312 ; is larger than the pointer size. The store of the pointer doesn't store to all
1313 ; the bits of the integer.
1314 define i128 @test86(i1 %flag) {
1315 ; CHECK-LABEL: @test86(
1316 ; CHECK-NEXT:    [[X1:%.*]] = alloca [2 x i8*], align 8
1317 ; CHECK-NEXT:    [[Y:%.*]] = alloca i128, align 8
1318 ; CHECK-NEXT:    [[X1_SUB:%.*]] = getelementptr inbounds [2 x i8*], [2 x i8*]* [[X1]], i64 0, i64 0
1319 ; CHECK-NEXT:    [[X2:%.*]] = bitcast [2 x i8*]* [[X1]] to i128*
1320 ; CHECK-NEXT:    [[Y1:%.*]] = bitcast i128* [[Y]] to i8**
1321 ; CHECK-NEXT:    call void @scribble_on_i128(i128* nonnull [[X2]])
1322 ; CHECK-NEXT:    call void @scribble_on_i128(i128* nonnull [[Y]])
1323 ; CHECK-NEXT:    [[T:%.*]] = load i8*, i8** [[X1_SUB]], align 8
1324 ; CHECK-NEXT:    store i8* [[T]], i8** [[Y1]], align 8
1325 ; CHECK-NEXT:    [[X2_VAL:%.*]] = load i128, i128* [[X2]], align 8
1326 ; CHECK-NEXT:    [[Y_VAL:%.*]] = load i128, i128* [[Y]], align 8
1327 ; CHECK-NEXT:    [[V:%.*]] = select i1 [[FLAG:%.*]], i128 [[X2_VAL]], i128 [[Y_VAL]]
1328 ; CHECK-NEXT:    ret i128 [[V]]
1330   %x = alloca [2 x i8*]
1331   %y = alloca i128
1332   %x1 = bitcast [2 x i8*]* %x to i8**
1333   %x2 = bitcast i8** %x1 to i128*
1334   %y1 = bitcast i128* %y to i8**
1335   call void @scribble_on_i128(i128* %x2)
1336   call void @scribble_on_i128(i128* %y)
1337   %t = load i8*, i8** %x1
1338   store i8* %t, i8** %y1
1339   %p = select i1 %flag, i128* %x2, i128* %y
1340   %v = load i128, i128* %p
1341   ret i128 %v
1344 define i32 @test_select_select0(i32 %a, i32 %r0, i32 %r1, i32 %v1, i32 %v2) {
1345 ; CHECK-LABEL: @test_select_select0(
1346 ; CHECK-NEXT:    [[C0_NOT:%.*]] = icmp slt i32 [[A:%.*]], [[V1:%.*]]
1347 ; CHECK-NEXT:    [[S0:%.*]] = select i1 [[C0_NOT]], i32 [[R1:%.*]], i32 [[R0:%.*]]
1348 ; CHECK-NEXT:    [[C1:%.*]] = icmp slt i32 [[A]], [[V2:%.*]]
1349 ; CHECK-NEXT:    [[S1:%.*]] = select i1 [[C1]], i32 [[S0]], i32 [[R1]]
1350 ; CHECK-NEXT:    ret i32 [[S1]]
1352   %c0 = icmp sge i32 %a, %v1
1353   %s0 = select i1 %c0, i32 %r0, i32 %r1
1354   %c1 = icmp slt i32 %a, %v2
1355   %s1 = select i1 %c1, i32 %s0, i32 %r1
1356   ret i32 %s1
1359 define i32 @test_select_select1(i32 %a, i32 %r0, i32 %r1, i32 %v1, i32 %v2) {
1360 ; CHECK-LABEL: @test_select_select1(
1361 ; CHECK-NEXT:    [[C0_NOT:%.*]] = icmp slt i32 [[A:%.*]], [[V1:%.*]]
1362 ; CHECK-NEXT:    [[S0:%.*]] = select i1 [[C0_NOT]], i32 [[R1:%.*]], i32 [[R0:%.*]]
1363 ; CHECK-NEXT:    [[C1:%.*]] = icmp slt i32 [[A]], [[V2:%.*]]
1364 ; CHECK-NEXT:    [[S1:%.*]] = select i1 [[C1]], i32 [[R0]], i32 [[S0]]
1365 ; CHECK-NEXT:    ret i32 [[S1]]
1367   %c0 = icmp sge i32 %a, %v1
1368   %s0 = select i1 %c0, i32 %r0, i32 %r1
1369   %c1 = icmp slt i32 %a, %v2
1370   %s1 = select i1 %c1, i32 %r0, i32 %s0
1371   ret i32 %s1
1374 define i32 @PR23757(i32 %x) {
1375 ; CHECK-LABEL: @PR23757(
1376 ; CHECK-NEXT:    [[ADD:%.*]] = add i32 [[X:%.*]], 1
1377 ; CHECK-NEXT:    ret i32 [[ADD]]
1379   %cmp = icmp eq i32 %x, 2147483647
1380   %add = add nsw i32 %x, 1
1381   %sel = select i1 %cmp, i32 -2147483648, i32 %add
1382   ret i32 %sel
1385 define i32 @PR23757_swapped(i32 %x) {
1386 ; CHECK-LABEL: @PR23757_swapped(
1387 ; CHECK-NEXT:    ret i32 -2147483648
1389   %cmp = icmp eq i32 %x, 2147483647
1390   %add = add nsw i32 %x, 1
1391   %sel = select i1 %cmp, i32 %add, i32 -2147483648
1392   ret i32 %sel
1395 define i32 @PR23757_ne(i32 %x, i1* %p) {
1396 ; CHECK-LABEL: @PR23757_ne(
1397 ; CHECK-NEXT:    [[CMP:%.*]] = icmp ne i32 [[X:%.*]], 2147483647
1398 ; CHECK-NEXT:    store i1 [[CMP]], i1* [[P:%.*]], align 1
1399 ; CHECK-NEXT:    ret i32 -2147483648
1401   %cmp = icmp ne i32 %x, 2147483647
1402   store i1 %cmp, i1* %p ; thwart predicate canonicalization
1403   %add = add nsw i32 %x, 1
1404   %sel = select i1 %cmp, i32 -2147483648, i32 %add
1405   ret i32 %sel
1408 define i32 @PR23757_ne_swapped(i32 %x, i1* %p) {
1409 ; CHECK-LABEL: @PR23757_ne_swapped(
1410 ; CHECK-NEXT:    [[CMP:%.*]] = icmp ne i32 [[X:%.*]], 2147483647
1411 ; CHECK-NEXT:    store i1 [[CMP]], i1* [[P:%.*]], align 1
1412 ; CHECK-NEXT:    [[ADD:%.*]] = add i32 [[X]], 1
1413 ; CHECK-NEXT:    ret i32 [[ADD]]
1415   %cmp = icmp ne i32 %x, 2147483647
1416   store i1 %cmp, i1* %p ; thwart predicate canonicalization
1417   %add = add nsw i32 %x, 1
1418   %sel = select i1 %cmp, i32 %add, i32 -2147483648
1419   ret i32 %sel
1422 ; max(max(~a, -1), -1) --> ~min(a, 0)
1424 define i32 @PR27137(i32 %a) {
1425 ; CHECK-LABEL: @PR27137(
1426 ; CHECK-NEXT:    [[TMP1:%.*]] = call i32 @llvm.smin.i32(i32 [[A:%.*]], i32 0)
1427 ; CHECK-NEXT:    [[TMP2:%.*]] = xor i32 [[TMP1]], -1
1428 ; CHECK-NEXT:    ret i32 [[TMP2]]
1430   %not_a = xor i32 %a, -1
1431   %c0 = icmp slt i32 %a, 0
1432   %s0 = select i1 %c0, i32 %not_a, i32 -1
1433   %c1 = icmp sgt i32 %s0, -1
1434   %s1 = select i1 %c1, i32 %s0, i32 -1
1435   ret i32 %s1
1438 ; ub-safe negation pattern
1439 define i32 @PR27817(i32 %x) {
1440 ; CHECK-LABEL: @PR27817(
1441 ; CHECK-NEXT:    [[SUB:%.*]] = sub i32 0, [[X:%.*]]
1442 ; CHECK-NEXT:    ret i32 [[SUB]]
1444   %cmp = icmp eq i32 %x, -2147483648
1445   %sub = sub i32 0, %x
1446   %sel = select i1 %cmp, i32 -2147483648, i32 %sub
1447   ret i32 %sel
1450 define i32 @PR27817_nsw(i32 %x) {
1451 ; CHECK-LABEL: @PR27817_nsw(
1452 ; CHECK-NEXT:    [[SUB:%.*]] = sub i32 0, [[X:%.*]]
1453 ; CHECK-NEXT:    ret i32 [[SUB]]
1455   %cmp = icmp eq i32 %x, -2147483648
1456   %sub = sub nsw i32 0, %x
1457   %sel = select i1 %cmp, i32 -2147483648, i32 %sub
1458   ret i32 %sel
1461 define i32 @select_icmp_slt0_xor(i32 %x) {
1462 ; CHECK-LABEL: @select_icmp_slt0_xor(
1463 ; CHECK-NEXT:    [[TMP1:%.*]] = or i32 [[X:%.*]], -2147483648
1464 ; CHECK-NEXT:    ret i32 [[TMP1]]
1466   %cmp = icmp slt i32 %x, zeroinitializer
1467   %xor = xor i32 %x, 2147483648
1468   %x.xor = select i1 %cmp, i32 %x, i32 %xor
1469   ret i32 %x.xor
1472 define <2 x i32> @select_icmp_slt0_xor_vec(<2 x i32> %x) {
1473 ; CHECK-LABEL: @select_icmp_slt0_xor_vec(
1474 ; CHECK-NEXT:    [[TMP1:%.*]] = or <2 x i32> [[X:%.*]], <i32 -2147483648, i32 -2147483648>
1475 ; CHECK-NEXT:    ret <2 x i32> [[TMP1]]
1477   %cmp = icmp slt <2 x i32> %x, zeroinitializer
1478   %xor = xor <2 x i32> %x, <i32 2147483648, i32 2147483648>
1479   %x.xor = select <2 x i1> %cmp, <2 x i32> %x, <2 x i32> %xor
1480   ret <2 x i32> %x.xor
1483 define <4 x i32> @canonicalize_to_shuffle(<4 x i32> %a, <4 x i32> %b) {
1484 ; CHECK-LABEL: @canonicalize_to_shuffle(
1485 ; CHECK-NEXT:    [[SEL:%.*]] = shufflevector <4 x i32> [[A:%.*]], <4 x i32> [[B:%.*]], <4 x i32> <i32 0, i32 5, i32 6, i32 3>
1486 ; CHECK-NEXT:    ret <4 x i32> [[SEL]]
1488   %sel = select <4 x i1> <i1 true, i1 false, i1 false, i1 true>, <4 x i32> %a, <4 x i32> %b
1489   ret <4 x i32> %sel
1492 ; Undef elements of the select condition may not be translated into undef elements of a shuffle mask
1493 ; because undef in a shuffle mask means we can return anything, not just one of the selected values.
1494 ; https://bugs.llvm.org/show_bug.cgi?id=32486
1496 define <4 x i32> @undef_elts_in_condition(<4 x i32> %a, <4 x i32> %b) {
1497 ; CHECK-LABEL: @undef_elts_in_condition(
1498 ; CHECK-NEXT:    [[SEL:%.*]] = select <4 x i1> <i1 true, i1 undef, i1 false, i1 undef>, <4 x i32> [[A:%.*]], <4 x i32> [[B:%.*]]
1499 ; CHECK-NEXT:    ret <4 x i32> [[SEL]]
1501   %sel = select <4 x i1> <i1 true, i1 undef, i1 false, i1 undef>, <4 x i32> %a, <4 x i32> %b
1502   ret <4 x i32> %sel
1505 ; Don't die or try if the condition mask is a constant expression or contains a constant expression.
1507 @g = global i32 0
1509 define <4 x i32> @cannot_canonicalize_to_shuffle1(<4 x i32> %a, <4 x i32> %b) {
1510 ; CHECK-LABEL: @cannot_canonicalize_to_shuffle1(
1511 ; CHECK-NEXT:    [[SEL:%.*]] = select <4 x i1> bitcast (i4 ptrtoint (i32* @g to i4) to <4 x i1>), <4 x i32> [[A:%.*]], <4 x i32> [[B:%.*]]
1512 ; CHECK-NEXT:    ret <4 x i32> [[SEL]]
1514   %sel = select <4 x i1> bitcast (i4 ptrtoint (i32* @g to i4) to <4 x i1>), <4 x i32> %a, <4 x i32> %b
1515   ret <4 x i32> %sel
1518 define <4 x i32> @cannot_canonicalize_to_shuffle2(<4 x i32> %a, <4 x i32> %b) {
1519 ; CHECK-LABEL: @cannot_canonicalize_to_shuffle2(
1520 ; CHECK-NEXT:    [[SEL:%.*]] = select <4 x i1> <i1 true, i1 undef, i1 false, i1 icmp sle (i16 ptrtoint (i32* @g to i16), i16 4)>, <4 x i32> [[A:%.*]], <4 x i32> [[B:%.*]]
1521 ; CHECK-NEXT:    ret <4 x i32> [[SEL]]
1523   %sel = select <4 x i1> <i1 true, i1 undef, i1 false, i1 icmp sle (i16 ptrtoint (i32* @g to i16), i16 4)>, <4 x i32> %a, <4 x i32> %b
1524   ret <4 x i32> %sel
1527 declare void @llvm.assume(i1)
1529 define i8 @assume_cond_true(i1 %cond, i8 %x, i8 %y) {
1530 ; CHECK-LABEL: @assume_cond_true(
1531 ; CHECK-NEXT:    call void @llvm.assume(i1 [[COND:%.*]])
1532 ; CHECK-NEXT:    ret i8 [[X:%.*]]
1534   call void @llvm.assume(i1 %cond)
1535   %sel = select i1 %cond, i8 %x, i8 %y
1536   ret i8 %sel
1539 ; computeKnownBitsFromAssume() understands the 'not' of an assumed condition.
1541 define i8 @assume_cond_false(i1 %cond, i8 %x, i8 %y) {
1542 ; CHECK-LABEL: @assume_cond_false(
1543 ; CHECK-NEXT:    [[NOTCOND:%.*]] = xor i1 [[COND:%.*]], true
1544 ; CHECK-NEXT:    call void @llvm.assume(i1 [[NOTCOND]])
1545 ; CHECK-NEXT:    ret i8 [[Y:%.*]]
1547   %notcond = xor i1 %cond, true
1548   call void @llvm.assume(i1 %notcond)
1549   %sel = select i1 %cond, i8 %x, i8 %y
1550   ret i8 %sel
1553 ; Test case to make sure we don't consider an all ones float values for converting the select into a sext.
1554 define <4 x float> @PR33721(<4 x float> %w) {
1555 ; CHECK-LABEL: @PR33721(
1556 ; CHECK-NEXT:  entry:
1557 ; CHECK-NEXT:    [[TMP0:%.*]] = fcmp ole <4 x float> [[W:%.*]], zeroinitializer
1558 ; CHECK-NEXT:    [[TMP1:%.*]] = select <4 x i1> [[TMP0]], <4 x float> <float 0xFFFFFFFFE0000000, float 0xFFFFFFFFE0000000, float 0xFFFFFFFFE0000000, float 0xFFFFFFFFE0000000>, <4 x float> zeroinitializer
1559 ; CHECK-NEXT:    ret <4 x float> [[TMP1]]
1561 entry:
1562   %0 = fcmp ole <4 x float> %w, zeroinitializer
1563   %1 = select <4 x i1> %0, <4 x float> <float 0xFFFFFFFFE0000000, float 0xFFFFFFFFE0000000, float 0xFFFFFFFFE0000000, float 0xFFFFFFFFE0000000>, <4 x float> zeroinitializer
1564   ret <4 x float> %1
1567 ; select(C, binop(select(C, X, Y), W), Z) -> select(C, binop(X, W), Z)
1568 define i8 @test87(i1 %cond, i8 %w, i8 %x, i8 %y, i8 %z) {
1569 ; CHECK-LABEL: @test87(
1570 ; CHECK-NEXT:    [[B:%.*]] = add i8 [[X:%.*]], [[W:%.*]]
1571 ; CHECK-NEXT:    [[C:%.*]] = select i1 [[COND:%.*]], i8 [[B]], i8 [[Z:%.*]]
1572 ; CHECK-NEXT:    ret i8 [[C]]
1574   %a = select i1 %cond, i8 %x, i8 %y
1575   %b = add i8 %a, %w
1576   %c = select i1 %cond, i8 %b, i8 %z
1577   ret i8 %c
1580 ; select(C, binop(select(C, X, Y), W), Z) -> select(C, Z, binop(Y, W))
1581 define i8 @test88(i1 %cond, i8 %w, i8 %x, i8 %y, i8 %z) {
1582 ; CHECK-LABEL: @test88(
1583 ; CHECK-NEXT:    [[B:%.*]] = sub i8 [[Y:%.*]], [[W:%.*]]
1584 ; CHECK-NEXT:    [[C:%.*]] = select i1 [[COND:%.*]], i8 [[Z:%.*]], i8 [[B]]
1585 ; CHECK-NEXT:    ret i8 [[C]]
1587   %a = select i1 %cond, i8 %x, i8 %y
1588   %b = sub i8 %a, %w
1589   %c = select i1 %cond, i8 %z, i8 %b
1590   ret i8 %c
1593 ; select(C, Z, binop(W, select(C, X, Y))) -> select(C, binop(X, W), Z)
1594 define i8 @test89(i1 %cond, i8 %w, i8 %x, i8 %y, i8 %z) {
1595 ; CHECK-LABEL: @test89(
1596 ; CHECK-NEXT:    [[B:%.*]] = and i8 [[X:%.*]], [[W:%.*]]
1597 ; CHECK-NEXT:    [[C:%.*]] = select i1 [[COND:%.*]], i8 [[B]], i8 [[Z:%.*]]
1598 ; CHECK-NEXT:    ret i8 [[C]]
1600   %a = select i1 %cond, i8 %x, i8 %y
1601   %b = and i8 %w, %a
1602   %c = select i1 %cond, i8 %b, i8 %z
1603   ret i8 %c
1606 ; select(C, Z, binop(W, select(C, X, Y))) -> select(C, Z, binop(W, Y))
1607 define i8 @test90(i1 %cond, i8 %w, i8 %x, i8 %y, i8 %z) {
1608 ; CHECK-LABEL: @test90(
1609 ; CHECK-NEXT:    [[B:%.*]] = or i8 [[Y:%.*]], [[W:%.*]]
1610 ; CHECK-NEXT:    [[C:%.*]] = select i1 [[COND:%.*]], i8 [[Z:%.*]], i8 [[B]]
1611 ; CHECK-NEXT:    ret i8 [[C]]
1613   %a = select i1 %cond, i8 %x, i8 %y
1614   %b = or i8 %w, %a
1615   %c = select i1 %cond, i8 %z, i8 %b
1616   ret i8 %c
1619 define i32 @test_shl_zext_bool(i1 %t) {
1620 ; CHECK-LABEL: @test_shl_zext_bool(
1621 ; CHECK-NEXT:    [[R:%.*]] = select i1 [[T:%.*]], i32 4, i32 0
1622 ; CHECK-NEXT:    ret i32 [[R]]
1624   %r = select i1 %t, i32 4, i32 0
1625   ret i32 %r
1628 define <2 x i32> @test_shl_zext_bool_splat(<2 x i1> %t) {
1629 ; CHECK-LABEL: @test_shl_zext_bool_splat(
1630 ; CHECK-NEXT:    [[R:%.*]] = select <2 x i1> [[T:%.*]], <2 x i32> <i32 8, i32 8>, <2 x i32> zeroinitializer
1631 ; CHECK-NEXT:    ret <2 x i32> [[R]]
1633   %r = select <2 x i1> %t, <2 x i32> <i32 8, i32 8>, <2 x i32> zeroinitializer
1634   ret <2 x i32> %r
1637 define <2 x i32> @test_shl_zext_bool_vec(<2 x i1> %t) {
1638 ; CHECK-LABEL: @test_shl_zext_bool_vec(
1639 ; CHECK-NEXT:    [[R:%.*]] = select <2 x i1> [[T:%.*]], <2 x i32> <i32 4, i32 8>, <2 x i32> zeroinitializer
1640 ; CHECK-NEXT:    ret <2 x i32> [[R]]
1642   %r = select <2 x i1> %t, <2 x i32> <i32 4, i32 8>, <2 x i32> zeroinitializer
1643   ret <2 x i32> %r
1646 define float @copysign1(float %x) {
1647 ; CHECK-LABEL: @copysign1(
1648 ; CHECK-NEXT:    [[R:%.*]] = call float @llvm.copysign.f32(float 1.000000e+00, float [[X:%.*]])
1649 ; CHECK-NEXT:    ret float [[R]]
1651   %i = bitcast float %x to i32
1652   %ispos = icmp sgt i32 %i, -1
1653   %r = select i1 %ispos, float 1.0, float -1.0
1654   ret float %r
1657 define float @copysign1_fmf(float %x) {
1658 ; CHECK-LABEL: @copysign1_fmf(
1659 ; CHECK-NEXT:    [[R:%.*]] = call float @llvm.copysign.f32(float 1.000000e+00, float [[X:%.*]])
1660 ; CHECK-NEXT:    ret float [[R]]
1662   %i = bitcast float %x to i32
1663   %ispos = icmp sgt i32 %i, -1
1664   %r = select nsz ninf i1 %ispos, float 1.0, float -1.0
1665   ret float %r
1668 define <2 x float> @copysign2(<2 x float> %x) {
1669 ; CHECK-LABEL: @copysign2(
1670 ; CHECK-NEXT:    [[TMP1:%.*]] = fneg <2 x float> [[X:%.*]]
1671 ; CHECK-NEXT:    [[R:%.*]] = call <2 x float> @llvm.copysign.v2f32(<2 x float> <float 4.200000e+01, float 4.200000e+01>, <2 x float> [[TMP1]])
1672 ; CHECK-NEXT:    ret <2 x float> [[R]]
1674   %i = bitcast <2 x float> %x to <2 x i32>
1675   %isneg = icmp slt <2 x i32> %i, zeroinitializer
1676   %r = select nsz <2 x i1> %isneg, <2 x float> <float 42.0, float 42.0>, <2 x float> <float -42.0, float -42.0>
1677   ret <2 x float> %r
1680 define float @copysign3(float %x) {
1681 ; CHECK-LABEL: @copysign3(
1682 ; CHECK-NEXT:    [[TMP1:%.*]] = fneg float [[X:%.*]]
1683 ; CHECK-NEXT:    [[R:%.*]] = call float @llvm.copysign.f32(float 4.300000e+01, float [[TMP1]])
1684 ; CHECK-NEXT:    ret float [[R]]
1686   %i = bitcast float %x to i32
1687   %ispos = icmp ult i32 %i, 2147483648
1688   %r = select fast i1 %ispos, float -43.0, float 43.0
1689   ret float %r
1692 define <2 x float> @copysign_vec_undef(<2 x float> %x) {
1693 ; CHECK-LABEL: @copysign_vec_undef(
1694 ; CHECK-NEXT:    [[TMP1:%.*]] = fneg <2 x float> [[X:%.*]]
1695 ; CHECK-NEXT:    [[R:%.*]] = call <2 x float> @llvm.copysign.v2f32(<2 x float> <float 4.200000e+01, float 4.200000e+01>, <2 x float> [[TMP1]])
1696 ; CHECK-NEXT:    ret <2 x float> [[R]]
1698   %i = bitcast <2 x float> %x to <2 x i32>
1699   %isneg = icmp ugt <2 x i32> %i, <i32 2147483647, i32 2147483647>
1700   %r = select arcp nnan <2 x i1> %isneg, <2 x float> <float 42.0, float undef>, <2 x float> <float -42.0, float -42.0>
1701   ret <2 x float> %r
1704 define <2 x float> @copysign_vec_undef1(<2 x float> %x) {
1705 ; CHECK-LABEL: @copysign_vec_undef1(
1706 ; CHECK-NEXT:    [[R:%.*]] = call <2 x float> @llvm.copysign.v2f32(<2 x float> <float 4.200000e+01, float 4.200000e+01>, <2 x float> [[X:%.*]])
1707 ; CHECK-NEXT:    ret <2 x float> [[R]]
1709   %i = bitcast <2 x float> %x to <2 x i32>
1710   %isneg = icmp ult <2 x i32> %i, <i32 2147483648, i32 2147483648>
1711   %r = select arcp nnan <2 x i1> %isneg, <2 x float> <float 42.0, float 42.0>, <2 x float> <float undef, float -42.0>
1712   ret <2 x float> %r
1715 define <2 x float> @copysign_vec_undef3(<2 x float> %x) {
1716 ; CHECK-LABEL: @copysign_vec_undef3(
1717 ; CHECK-NEXT:    [[R:%.*]] = call <2 x float> @llvm.copysign.v2f32(<2 x float> <float 4.200000e+01, float 4.200000e+01>, <2 x float> [[X:%.*]])
1718 ; CHECK-NEXT:    ret <2 x float> [[R]]
1720   %i = bitcast <2 x float> %x to <2 x i32>
1721   %isneg = icmp ugt <2 x i32> %i, <i32 2147483647, i32 2147483647>
1722   %r = select arcp nnan <2 x i1> %isneg, <2 x float> <float -42.0, float undef>, <2 x float> <float +42.0, float undef>
1723   ret <2 x float> %r
1726 declare void @use1(i1)
1728 ; Negative test
1730 define float @copysign_extra_use(float %x) {
1731 ; CHECK-LABEL: @copysign_extra_use(
1732 ; CHECK-NEXT:    [[I:%.*]] = bitcast float [[X:%.*]] to i32
1733 ; CHECK-NEXT:    [[ISNEG:%.*]] = icmp slt i32 [[I]], 0
1734 ; CHECK-NEXT:    call void @use1(i1 [[ISNEG]])
1735 ; CHECK-NEXT:    [[R:%.*]] = select i1 [[ISNEG]], float -4.400000e+01, float 4.400000e+01
1736 ; CHECK-NEXT:    ret float [[R]]
1738   %i = bitcast float %x to i32
1739   %isneg = icmp ugt i32 %i, 2147483647
1740   call void @use1(i1 %isneg)
1741   %r = select i1 %isneg, float -44.0, float 44.0
1742   ret float %r
1745 ; Negative test
1747 define float @copysign_type_mismatch(double %x) {
1748 ; CHECK-LABEL: @copysign_type_mismatch(
1749 ; CHECK-NEXT:    [[I:%.*]] = bitcast double [[X:%.*]] to i64
1750 ; CHECK-NEXT:    [[ISPOS:%.*]] = icmp sgt i64 [[I]], -1
1751 ; CHECK-NEXT:    [[R:%.*]] = select i1 [[ISPOS]], float 1.000000e+00, float -1.000000e+00
1752 ; CHECK-NEXT:    ret float [[R]]
1754   %i = bitcast double %x to i64
1755   %ispos = icmp sgt i64 %i, -1
1756   %r = select i1 %ispos, float 1.0, float -1.0
1757   ret float %r
1760 ; Negative test
1762 define float @copysign_wrong_cmp(float %x) {
1763 ; CHECK-LABEL: @copysign_wrong_cmp(
1764 ; CHECK-NEXT:    [[I:%.*]] = bitcast float [[X:%.*]] to i32
1765 ; CHECK-NEXT:    [[ISPOS:%.*]] = icmp sgt i32 [[I]], 0
1766 ; CHECK-NEXT:    [[R:%.*]] = select i1 [[ISPOS]], float 1.000000e+00, float -1.000000e+00
1767 ; CHECK-NEXT:    ret float [[R]]
1769   %i = bitcast float %x to i32
1770   %ispos = icmp sgt i32 %i, 0
1771   %r = select i1 %ispos, float 1.0, float -1.0
1772   ret float %r
1775 ; Negative test
1777 define float @copysign_wrong_const(float %x) {
1778 ; CHECK-LABEL: @copysign_wrong_const(
1779 ; CHECK-NEXT:    [[I:%.*]] = bitcast float [[X:%.*]] to i32
1780 ; CHECK-NEXT:    [[ISPOS:%.*]] = icmp sgt i32 [[I]], -1
1781 ; CHECK-NEXT:    [[R:%.*]] = select i1 [[ISPOS]], float 2.000000e+00, float -1.000000e+00
1782 ; CHECK-NEXT:    ret float [[R]]
1784   %i = bitcast float %x to i32
1785   %ispos = icmp sgt i32 %i, -1
1786   %r = select i1 %ispos, float 2.0, float -1.0
1787   ret float %r
1790 ; TODO: we can replace select with a Phi.
1791 define i32 @select_dominating_cond(i1 %cond, i32 %x, i32 %y) {
1792 ; CHECK-LABEL: @select_dominating_cond(
1793 ; CHECK-NEXT:  entry:
1794 ; CHECK-NEXT:    br i1 [[COND:%.*]], label [[IF_TRUE:%.*]], label [[IF_FALSE:%.*]]
1795 ; CHECK:       if.true:
1796 ; CHECK-NEXT:    br label [[MERGE:%.*]]
1797 ; CHECK:       if.false:
1798 ; CHECK-NEXT:    br label [[MERGE]]
1799 ; CHECK:       merge:
1800 ; CHECK-NEXT:    [[S:%.*]] = phi i32 [ [[Y:%.*]], [[IF_FALSE]] ], [ [[X:%.*]], [[IF_TRUE]] ]
1801 ; CHECK-NEXT:    ret i32 [[S]]
1803 entry:
1804   br i1 %cond, label %if.true, label %if.false
1806 if.true:
1807   br label %merge
1809 if.false:
1810   br label %merge
1812 merge:
1813   %s = select i1 %cond, i32 %x, i32 %y
1814   ret i32 %s
1817 define i32 @select_dominating_inverted(i1 %cond, i32 %x, i32 %y) {
1818 ; CHECK-LABEL: @select_dominating_inverted(
1819 ; CHECK-NEXT:  entry:
1820 ; CHECK-NEXT:    br i1 [[COND:%.*]], label [[IF_FALSE:%.*]], label [[IF_TRUE:%.*]]
1821 ; CHECK:       if.true:
1822 ; CHECK-NEXT:    br label [[MERGE:%.*]]
1823 ; CHECK:       if.false:
1824 ; CHECK-NEXT:    br label [[MERGE]]
1825 ; CHECK:       merge:
1826 ; CHECK-NEXT:    [[S:%.*]] = phi i32 [ [[X:%.*]], [[IF_FALSE]] ], [ [[Y:%.*]], [[IF_TRUE]] ]
1827 ; CHECK-NEXT:    ret i32 [[S]]
1829 entry:
1830   %inverted = xor i1 %cond, 1
1831   br i1 %inverted, label %if.true, label %if.false
1833 if.true:
1834   br label %merge
1836 if.false:
1837   br label %merge
1839 merge:
1840   %s = select i1 %cond, i32 %x, i32 %y
1841   ret i32 %s
1844 ; More complex CFG: the block with select has multiple predecessors.
1845 define i32 @select_dominating_cond_multiple_preds(i1 %cond, i1 %cond2, i1 %cond3, i32 %x, i32 %y) {
1846 ; CHECK-LABEL: @select_dominating_cond_multiple_preds(
1847 ; CHECK-NEXT:  entry:
1848 ; CHECK-NEXT:    br i1 [[COND:%.*]], label [[IF_TRUE:%.*]], label [[IF_FALSE:%.*]]
1849 ; CHECK:       if.true:
1850 ; CHECK-NEXT:    br i1 [[COND2:%.*]], label [[IF_TRUE_1:%.*]], label [[IF_TRUE_2:%.*]]
1851 ; CHECK:       if.true.1:
1852 ; CHECK-NEXT:    br label [[MERGE:%.*]]
1853 ; CHECK:       if.true.2:
1854 ; CHECK-NEXT:    br label [[MERGE]]
1855 ; CHECK:       if.false:
1856 ; CHECK-NEXT:    br i1 [[COND3:%.*]], label [[IF_FALSE_1:%.*]], label [[EXIT:%.*]]
1857 ; CHECK:       if.false.1:
1858 ; CHECK-NEXT:    br label [[MERGE]]
1859 ; CHECK:       merge:
1860 ; CHECK-NEXT:    [[S:%.*]] = phi i32 [ [[Y:%.*]], [[IF_FALSE_1]] ], [ [[X:%.*]], [[IF_TRUE_2]] ], [ [[X]], [[IF_TRUE_1]] ]
1861 ; CHECK-NEXT:    ret i32 [[S]]
1862 ; CHECK:       exit:
1863 ; CHECK-NEXT:    ret i32 0
1865 entry:
1866   br i1 %cond, label %if.true, label %if.false
1868 if.true:
1869   br i1 %cond2, label %if.true.1, label %if.true.2
1871 if.true.1:
1872   br label %merge
1874 if.true.2:
1875   br label %merge
1877 if.false:
1878   br i1 %cond3, label %if.false.1, label %exit
1880 if.false.1:
1881   br label %merge
1883 merge:
1884   %s = select i1 %cond, i32 %x, i32 %y
1885   ret i32 %s
1887 exit:
1888   ret i32 0
1891 ; More complex CFG for inverted case: the block with select has multiple predecessors.
1892 define i32 @select_dominating_cond_inverted_multiple_preds(i1 %cond, i1 %cond2, i1 %cond3, i32 %x, i32 %y) {
1893 ; CHECK-LABEL: @select_dominating_cond_inverted_multiple_preds(
1894 ; CHECK-NEXT:  entry:
1895 ; CHECK-NEXT:    br i1 [[COND:%.*]], label [[IF_FALSE:%.*]], label [[IF_TRUE:%.*]]
1896 ; CHECK:       if.true:
1897 ; CHECK-NEXT:    br i1 [[COND2:%.*]], label [[IF_TRUE_1:%.*]], label [[IF_TRUE_2:%.*]]
1898 ; CHECK:       if.true.1:
1899 ; CHECK-NEXT:    br label [[MERGE:%.*]]
1900 ; CHECK:       if.true.2:
1901 ; CHECK-NEXT:    br label [[MERGE]]
1902 ; CHECK:       if.false:
1903 ; CHECK-NEXT:    br i1 [[COND3:%.*]], label [[IF_FALSE_1:%.*]], label [[EXIT:%.*]]
1904 ; CHECK:       if.false.1:
1905 ; CHECK-NEXT:    br label [[MERGE]]
1906 ; CHECK:       merge:
1907 ; CHECK-NEXT:    [[S:%.*]] = phi i32 [ [[X:%.*]], [[IF_FALSE_1]] ], [ [[Y:%.*]], [[IF_TRUE_2]] ], [ [[Y]], [[IF_TRUE_1]] ]
1908 ; CHECK-NEXT:    ret i32 [[S]]
1909 ; CHECK:       exit:
1910 ; CHECK-NEXT:    ret i32 0
1912 entry:
1913   %inverted = xor i1 %cond, 1
1914   br i1 %inverted, label %if.true, label %if.false
1916 if.true:
1917   br i1 %cond2, label %if.true.1, label %if.true.2
1919 if.true.1:
1920   br label %merge
1922 if.true.2:
1923   br label %merge
1925 if.false:
1926   br i1 %cond3, label %if.false.1, label %exit
1928 if.false.1:
1929   br label %merge
1931 merge:
1932   %s = select i1 %cond, i32 %x, i32 %y
1933   ret i32 %s
1935 exit:
1936   ret i32 0
1939 ; More complex CFG for inverted case: the block with select has multiple predecessors that can duplicate.
1940 define i32 @select_dominating_cond_inverted_multiple_duplicating_preds(i1 %cond, i32 %cond2, i1 %cond3, i32 %x, i32 %y) {
1941 ; CHECK-LABEL: @select_dominating_cond_inverted_multiple_duplicating_preds(
1942 ; CHECK-NEXT:  entry:
1943 ; CHECK-NEXT:    br i1 [[COND:%.*]], label [[IF_FALSE:%.*]], label [[IF_TRUE:%.*]]
1944 ; CHECK:       if.true:
1945 ; CHECK-NEXT:    switch i32 [[COND2:%.*]], label [[SWITCH_CASE_1:%.*]] [
1946 ; CHECK-NEXT:    i32 1, label [[MERGE:%.*]]
1947 ; CHECK-NEXT:    i32 2, label [[MERGE]]
1948 ; CHECK-NEXT:    i32 3, label [[MERGE]]
1949 ; CHECK-NEXT:    ]
1950 ; CHECK:       switch.case.1:
1951 ; CHECK-NEXT:    br label [[MERGE]]
1952 ; CHECK:       if.false:
1953 ; CHECK-NEXT:    br i1 [[COND3:%.*]], label [[IF_FALSE_1:%.*]], label [[EXIT:%.*]]
1954 ; CHECK:       if.false.1:
1955 ; CHECK-NEXT:    br label [[MERGE]]
1956 ; CHECK:       merge:
1957 ; CHECK-NEXT:    [[S:%.*]] = phi i32 [ [[X:%.*]], [[IF_FALSE_1]] ], [ [[Y:%.*]], [[SWITCH_CASE_1]] ], [ [[Y]], [[IF_TRUE]] ], [ [[Y]], [[IF_TRUE]] ], [ [[Y]], [[IF_TRUE]] ]
1958 ; CHECK-NEXT:    ret i32 [[S]]
1959 ; CHECK:       exit:
1960 ; CHECK-NEXT:    ret i32 0
1962 entry:
1963   %inverted = xor i1 %cond, 1
1964   br i1 %inverted, label %if.true, label %if.false
1966 if.true:
1967   switch i32 %cond2, label %switch.case.1 [
1968   i32 1, label %merge
1969   i32 2, label %merge
1970   i32 3, label %merge
1971   ]
1973 switch.case.1:
1974   br label %merge
1976 if.false:
1977   br i1 %cond3, label %if.false.1, label %exit
1979 if.false.1:
1980   br label %merge
1982 merge:
1983   %s = select i1 %cond, i32 %x, i32 %y
1984   ret i32 %s
1986 exit:
1987   ret i32 0
1990 ; Negative test: currently we take condition from IDom, but might be willing to expand it in the future.
1991 define i32 @select_not_imm_dominating_cond_neg(i1 %cond, i32 %x, i32 %y) {
1992 ; CHECK-LABEL: @select_not_imm_dominating_cond_neg(
1993 ; CHECK-NEXT:  entry:
1994 ; CHECK-NEXT:    br i1 [[COND:%.*]], label [[IF_TRUE:%.*]], label [[IF_FALSE:%.*]]
1995 ; CHECK:       if.true:
1996 ; CHECK-NEXT:    br label [[MERGE:%.*]]
1997 ; CHECK:       if.false:
1998 ; CHECK-NEXT:    br label [[MERGE]]
1999 ; CHECK:       merge:
2000 ; CHECK-NEXT:    br label [[EXIT:%.*]]
2001 ; CHECK:       exit:
2002 ; CHECK-NEXT:    [[S:%.*]] = select i1 [[COND]], i32 [[X:%.*]], i32 [[Y:%.*]]
2003 ; CHECK-NEXT:    ret i32 [[S]]
2005 entry:
2006   br i1 %cond, label %if.true, label %if.false
2008 if.true:
2009   br label %merge
2011 if.false:
2012   br label %merge
2014 merge:
2015   br label %exit
2017 exit:
2018   %s = select i1 %cond, i32 %x, i32 %y
2019   ret i32 %s
2022 ; Shows how we can leverage dominance to eliminate duplicating selects.
2023 define i32 @select_dominance_chain(i1 %cond, i32 %x, i32 %y) {
2024 ; CHECK-LABEL: @select_dominance_chain(
2025 ; CHECK-NEXT:  entry:
2026 ; CHECK-NEXT:    br i1 [[COND:%.*]], label [[IF_TRUE_1:%.*]], label [[IF_FALSE_1:%.*]]
2027 ; CHECK:       if.true.1:
2028 ; CHECK-NEXT:    br label [[MERGE_1:%.*]]
2029 ; CHECK:       if.false.1:
2030 ; CHECK-NEXT:    br label [[MERGE_1]]
2031 ; CHECK:       merge.1:
2032 ; CHECK-NEXT:    br i1 [[COND]], label [[IF_TRUE_2:%.*]], label [[IF_FALSE_2:%.*]]
2033 ; CHECK:       if.true.2:
2034 ; CHECK-NEXT:    br label [[MERGE_2:%.*]]
2035 ; CHECK:       if.false.2:
2036 ; CHECK-NEXT:    br label [[MERGE_2]]
2037 ; CHECK:       merge.2:
2038 ; CHECK-NEXT:    br i1 [[COND]], label [[IF_TRUE_3:%.*]], label [[IF_FALSE_3:%.*]]
2039 ; CHECK:       if.true.3:
2040 ; CHECK-NEXT:    br label [[MERGE_3:%.*]]
2041 ; CHECK:       if.false.3:
2042 ; CHECK-NEXT:    br label [[MERGE_3]]
2043 ; CHECK:       merge.3:
2044 ; CHECK-NEXT:    [[S_1:%.*]] = phi i32 [ [[Y:%.*]], [[IF_FALSE_3]] ], [ [[X:%.*]], [[IF_TRUE_3]] ]
2045 ; CHECK-NEXT:    [[SUM_2:%.*]] = mul i32 [[S_1]], 3
2046 ; CHECK-NEXT:    ret i32 [[SUM_2]]
2048 entry:
2049   br i1 %cond, label %if.true.1, label %if.false.1
2051 if.true.1:
2052   br label %merge.1
2054 if.false.1:
2055   br label %merge.1
2057 merge.1:
2058   %s.1 = select i1 %cond, i32 %x, i32 %y
2059   br i1 %cond, label %if.true.2, label %if.false.2
2061 if.true.2:
2062   br label %merge.2
2064 if.false.2:
2065   br label %merge.2
2067 merge.2:
2068   %s.2 = select i1 %cond, i32 %x, i32 %y
2069   br i1 %cond, label %if.true.3, label %if.false.3
2071 if.true.3:
2072   br label %merge.3
2074 if.false.3:
2075   br label %merge.3
2077 merge.3:
2078   %s.3 = select i1 %cond, i32 %x, i32 %y
2079   %sum.1 = add i32 %s.1, %s.2
2080   %sum.2 = add i32 %sum.1, %s.3
2081   ret i32 %sum.2
2084 ; TODO: We can replace select with a Phi and then sink a and b to respective
2085 ; branches.
2086 define i32 @select_dominating_cond_and_sink(i1 %cond, i32 %x, i32 %y) {
2087 ; CHECK-LABEL: @select_dominating_cond_and_sink(
2088 ; CHECK-NEXT:  entry:
2089 ; CHECK-NEXT:    br i1 [[COND:%.*]], label [[IF_TRUE:%.*]], label [[IF_FALSE:%.*]]
2090 ; CHECK:       if.true:
2091 ; CHECK-NEXT:    br label [[MERGE:%.*]]
2092 ; CHECK:       if.false:
2093 ; CHECK-NEXT:    br label [[MERGE]]
2094 ; CHECK:       merge:
2095 ; CHECK-NEXT:    [[B:%.*]] = mul i32 [[X:%.*]], [[Y:%.*]]
2096 ; CHECK-NEXT:    [[A:%.*]] = add i32 [[X]], [[Y]]
2097 ; CHECK-NEXT:    [[S:%.*]] = select i1 [[COND]], i32 [[A]], i32 [[B]]
2098 ; CHECK-NEXT:    ret i32 [[S]]
2100 entry:
2101   %a = add i32 %x, %y
2102   %b = mul i32 %x, %y
2103   br i1 %cond, label %if.true, label %if.false
2105 if.true:
2106   br label %merge
2108 if.false:
2109   br label %merge
2111 merge:
2112   %s = select i1 %cond, i32 %a, i32 %b
2113   ret i32 %s
2116 define i32 @select_dominating_cond_same_labels(i1 %cond) {
2117 ; CHECK-LABEL: @select_dominating_cond_same_labels(
2118 ; CHECK-NEXT:  entry:
2119 ; CHECK-NEXT:    br i1 false, label [[EXIT:%.*]], label [[EXIT]]
2120 ; CHECK:       exit:
2121 ; CHECK-NEXT:    [[RESULT:%.*]] = select i1 [[COND:%.*]], i32 123, i32 456
2122 ; CHECK-NEXT:    ret i32 [[RESULT]]
2124 entry:
2125   %result = select i1 %cond, i32 123, i32 456
2126   br i1 %cond, label %exit, label %exit
2127 exit:
2128   ret i32 %result
2131 define i32 @select_phi_same_condition(i1 %cond, i32 %x, i32 %y, i32 %z) {
2132 ; CHECK-LABEL: @select_phi_same_condition(
2133 ; CHECK-NEXT:  entry:
2134 ; CHECK-NEXT:    br i1 [[COND:%.*]], label [[IF_TRUE:%.*]], label [[IF_FALSE:%.*]]
2135 ; CHECK:       if.true:
2136 ; CHECK-NEXT:    br label [[MERGE:%.*]]
2137 ; CHECK:       if.false:
2138 ; CHECK-NEXT:    br label [[MERGE]]
2139 ; CHECK:       merge:
2140 ; CHECK-NEXT:    [[S:%.*]] = phi i32 [ [[Z:%.*]], [[IF_FALSE]] ], [ [[X:%.*]], [[IF_TRUE]] ]
2141 ; CHECK-NEXT:    ret i32 [[S]]
2143 entry:
2144   br i1 %cond, label %if.true, label %if.false
2146 if.true:
2147   br label %merge
2149 if.false:
2150   br label %merge
2152 merge:
2153   %phi = phi i32 [0, %if.true], [%z, %if.false]
2154   %s = select i1 %cond, i32 %x, i32 %phi
2155   ret i32 %s
2159 ; TODO: Replace with phi[a, c] and sink them to respective branches.
2160 define i32 @select_phi_same_condition_sink(i1 %cond, i32 %x, i32 %y, i32 %z) {
2161 ; CHECK-LABEL: @select_phi_same_condition_sink(
2162 ; CHECK-NEXT:  entry:
2163 ; CHECK-NEXT:    br i1 [[COND:%.*]], label [[IF_TRUE:%.*]], label [[IF_FALSE:%.*]]
2164 ; CHECK:       if.true:
2165 ; CHECK-NEXT:    br label [[MERGE:%.*]]
2166 ; CHECK:       if.false:
2167 ; CHECK-NEXT:    [[B:%.*]] = mul i32 [[X:%.*]], [[Z:%.*]]
2168 ; CHECK-NEXT:    br label [[MERGE]]
2169 ; CHECK:       merge:
2170 ; CHECK-NEXT:    [[PHI:%.*]] = phi i32 [ 0, [[IF_TRUE]] ], [ [[B]], [[IF_FALSE]] ]
2171 ; CHECK-NEXT:    [[A:%.*]] = add i32 [[X]], [[Y:%.*]]
2172 ; CHECK-NEXT:    [[S:%.*]] = select i1 [[COND]], i32 [[A]], i32 [[PHI]]
2173 ; CHECK-NEXT:    ret i32 [[S]]
2175 entry:
2176   %a = add i32 %x, %y
2177   %b = mul i32 %x, %z
2178   br i1 %cond, label %if.true, label %if.false
2180 if.true:
2181   br label %merge
2183 if.false:
2184   br label %merge
2186 merge:
2187   %phi = phi i32 [0, %if.true], [%b, %if.false]
2188   %s = select i1 %cond, i32 %a, i32 %phi
2189   ret i32 %s
2192 declare i32 @__gxx_personality_v0(...)
2193 declare i1 @foo()
2195 define i32 @test_invoke_neg(i32 %x, i32 %y) nounwind uwtable ssp personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
2196 ; CHECK-LABEL: @test_invoke_neg(
2197 ; CHECK-NEXT:  entry:
2198 ; CHECK-NEXT:    [[COND:%.*]] = invoke i1 @foo()
2199 ; CHECK-NEXT:    to label [[INVOKE_CONT:%.*]] unwind label [[LPAD:%.*]]
2200 ; CHECK:       invoke.cont:
2201 ; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[COND]], i32 [[X:%.*]], i32 [[Y:%.*]]
2202 ; CHECK-NEXT:    ret i32 [[SEL]]
2203 ; CHECK:       lpad:
2204 ; CHECK-NEXT:    [[LP:%.*]] = landingpad { i1, i32 }
2205 ; CHECK-NEXT:    filter [0 x i1] zeroinitializer
2206 ; CHECK-NEXT:    unreachable
2208 entry:
2209   %cond = invoke i1 @foo()
2210   to label %invoke.cont unwind label %lpad
2212 invoke.cont:
2213   %sel = select i1 %cond, i32 %x, i32 %y
2214   ret i32 %sel
2216 lpad:
2217   %lp = landingpad { i1, i32 }
2218   filter [0 x i1] zeroinitializer
2219   unreachable
2222 declare i32 @bar()
2224 define i32 @test_invoke_2_neg(i1 %cond, i32 %x, i32 %y) nounwind uwtable ssp personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
2225 ; CHECK-LABEL: @test_invoke_2_neg(
2226 ; CHECK-NEXT:  entry:
2227 ; CHECK-NEXT:    br i1 [[COND:%.*]], label [[IF_TRUE:%.*]], label [[IF_FALSE:%.*]]
2228 ; CHECK:       if.true:
2229 ; CHECK-NEXT:    br label [[MERGE:%.*]]
2230 ; CHECK:       if.false:
2231 ; CHECK-NEXT:    [[RESULT:%.*]] = invoke i32 @bar()
2232 ; CHECK-NEXT:    to label [[MERGE]] unwind label [[LPAD:%.*]]
2233 ; CHECK:       merge:
2234 ; CHECK-NEXT:    [[PHI:%.*]] = phi i32 [ 0, [[IF_TRUE]] ], [ [[RESULT]], [[IF_FALSE]] ]
2235 ; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[COND]], i32 1, i32 [[PHI]]
2236 ; CHECK-NEXT:    ret i32 [[SEL]]
2237 ; CHECK:       lpad:
2238 ; CHECK-NEXT:    [[LP:%.*]] = landingpad { i1, i32 }
2239 ; CHECK-NEXT:    filter [0 x i1] zeroinitializer
2240 ; CHECK-NEXT:    unreachable
2242 entry:
2243   br i1 %cond, label %if.true, label %if.false
2245 if.true:
2246   br label %merge
2248 if.false:
2249   %result = invoke i32 @bar()
2250   to label %merge unwind label %lpad
2252 merge:
2253   %phi = phi i32 [ 0, %if.true ], [ %result, %if.false ]
2254   %sel = select i1 %cond, i32 1, i32 %phi
2255   ret i32 %sel
2257 lpad:
2258   %lp = landingpad { i1, i32 }
2259   filter [0 x i1] zeroinitializer
2260   unreachable
2263 define i32 @select_phi_same_condition_switch(i1 %cond, i32 %x, i32 %y) {
2264 ; CHECK-LABEL: @select_phi_same_condition_switch(
2265 ; CHECK-NEXT:  entry:
2266 ; CHECK-NEXT:    br i1 [[COND:%.*]], label [[IF_TRUE:%.*]], label [[IF_FALSE:%.*]]
2267 ; CHECK:       if.true:
2268 ; CHECK-NEXT:    switch i32 [[X:%.*]], label [[EXIT:%.*]] [
2269 ; CHECK-NEXT:    i32 1, label [[MERGE:%.*]]
2270 ; CHECK-NEXT:    i32 2, label [[MERGE]]
2271 ; CHECK-NEXT:    ]
2272 ; CHECK:       exit:
2273 ; CHECK-NEXT:    ret i32 0
2274 ; CHECK:       if.false:
2275 ; CHECK-NEXT:    br label [[MERGE]]
2276 ; CHECK:       merge:
2277 ; CHECK-NEXT:    [[S:%.*]] = phi i32 [ [[Y:%.*]], [[IF_FALSE]] ], [ [[X]], [[IF_TRUE]] ], [ [[X]], [[IF_TRUE]] ]
2278 ; CHECK-NEXT:    ret i32 [[S]]
2280 entry:
2281   br i1 %cond, label %if.true, label %if.false
2283 if.true:
2284   switch i32 %x, label %exit [
2285   i32 1, label %merge
2286   i32 2, label %merge
2287   ]
2289 exit:
2290   ret i32 0
2292 if.false:
2293   br label %merge
2295 merge:
2296   %phi = phi i32 [0, %if.true], [0, %if.true], [%y, %if.false]
2297   %s = select i1 %cond, i32 %x, i32 %phi
2298   ret i32 %s
2301 define i32 @transit_different_values_through_phi(i1 %cond, i1 %cond2) {
2302 ; CHECK-LABEL: @transit_different_values_through_phi(
2303 ; CHECK-NEXT:  entry:
2304 ; CHECK-NEXT:    br i1 [[COND:%.*]], label [[IF_TRUE:%.*]], label [[IF_FALSE:%.*]]
2305 ; CHECK:       if.true:
2306 ; CHECK-NEXT:    br i1 [[COND2:%.*]], label [[IF_TRUE_1:%.*]], label [[IF_TRUE_2:%.*]]
2307 ; CHECK:       if.true.1:
2308 ; CHECK-NEXT:    br label [[MERGE:%.*]]
2309 ; CHECK:       if.true.2:
2310 ; CHECK-NEXT:    br label [[MERGE]]
2311 ; CHECK:       if.false:
2312 ; CHECK-NEXT:    br label [[MERGE]]
2313 ; CHECK:       merge:
2314 ; CHECK-NEXT:    [[S:%.*]] = phi i32 [ 3, [[IF_FALSE]] ], [ 2, [[IF_TRUE_2]] ], [ 1, [[IF_TRUE_1]] ]
2315 ; CHECK-NEXT:    ret i32 [[S]]
2316 ; CHECK:       exit:
2317 ; CHECK-NEXT:    ret i32 0
2319 entry:
2320   br i1 %cond, label %if.true, label %if.false
2322 if.true:
2323   br i1 %cond2, label %if.true.1, label %if.true.2
2325 if.true.1:
2326   br label %merge
2328 if.true.2:
2329   br label %merge
2331 if.false:
2332   br label %merge
2334 merge:
2335   %p = phi i32 [ 1, %if.true.1 ], [ 2, %if.true.2 ], [ 4, %if.false ]
2336   %s = select i1 %cond, i32 %p, i32 3
2337   ret i32 %s
2339 exit:
2340   ret i32 0
2343 define i32 @select_phi_degenerate(i1 %cond, i1 %cond2) {
2344 ; CHECK-LABEL: @select_phi_degenerate(
2345 ; CHECK-NEXT:  entry:
2346 ; CHECK-NEXT:    br i1 [[COND:%.*]], label [[LOOP:%.*]], label [[EXIT:%.*]]
2347 ; CHECK:       loop:
2348 ; CHECK-NEXT:    [[SELECT:%.*]] = phi i32 [ [[IV_INC:%.*]], [[LOOP]] ], [ 0, [[ENTRY:%.*]] ]
2349 ; CHECK-NEXT:    [[IV_INC]] = add i32 [[SELECT]], 1
2350 ; CHECK-NEXT:    br i1 [[COND2:%.*]], label [[LOOP]], label [[EXIT2:%.*]]
2351 ; CHECK:       exit:
2352 ; CHECK-NEXT:    ret i32 0
2353 ; CHECK:       exit2:
2354 ; CHECK-NEXT:    ret i32 [[IV_INC]]
2356 entry:
2357   br i1 %cond, label %loop, label %exit
2359 loop:
2360   %iv = phi i32 [ 0, %entry ], [ %iv.inc, %loop ]
2361   %select = select i1 %cond, i32 %iv, i32 -1
2362   %iv.inc = add i32 %select, 1
2363   br i1 %cond2, label %loop, label %exit2
2365 exit:
2366   ret i32 0
2368 exit2:
2369   ret i32 %iv.inc
2372 define i32 @test_select_into_phi_not_idom(i1 %cond, i32 %A, i32 %B)  {
2373 ; CHECK-LABEL: @test_select_into_phi_not_idom(
2374 ; CHECK-NEXT:  entry:
2375 ; CHECK-NEXT:    br i1 [[COND:%.*]], label [[IF_TRUE:%.*]], label [[IF_FALSE:%.*]]
2376 ; CHECK:       if.true:
2377 ; CHECK-NEXT:    br label [[MERGE:%.*]]
2378 ; CHECK:       if.false:
2379 ; CHECK-NEXT:    br label [[MERGE]]
2380 ; CHECK:       merge:
2381 ; CHECK-NEXT:    br label [[EXIT:%.*]]
2382 ; CHECK:       exit:
2383 ; CHECK-NEXT:    ret i32 [[A:%.*]]
2385 entry:
2386   br i1 %cond, label %if.true, label %if.false
2388 if.true:
2389   br label %merge
2391 if.false:
2392   br label %merge
2394 merge:
2395   %phi = phi i32 [%A, %if.true], [%B, %if.false]
2396   br label %exit
2398 exit:
2399   %sel = select i1 %cond, i32 %phi, i32 %A
2400   ret i32 %sel
2403 define i32 @test_select_into_phi_not_idom_2(i1 %cond, i32 %A, i32 %B)  {
2404 ; CHECK-LABEL: @test_select_into_phi_not_idom_2(
2405 ; CHECK-NEXT:  entry:
2406 ; CHECK-NEXT:    br i1 [[COND:%.*]], label [[IF_TRUE:%.*]], label [[IF_FALSE:%.*]]
2407 ; CHECK:       if.true:
2408 ; CHECK-NEXT:    br label [[MERGE:%.*]]
2409 ; CHECK:       if.false:
2410 ; CHECK-NEXT:    br label [[MERGE]]
2411 ; CHECK:       merge:
2412 ; CHECK-NEXT:    br label [[EXIT:%.*]]
2413 ; CHECK:       exit:
2414 ; CHECK-NEXT:    ret i32 [[B:%.*]]
2416 entry:
2417   br i1 %cond, label %if.true, label %if.false
2419 if.true:
2420   br label %merge
2422 if.false:
2423   br label %merge
2425 merge:
2426   %phi = phi i32 [%A, %if.true], [%B, %if.false]
2427   br label %exit
2429 exit:
2430   %sel = select i1 %cond, i32 %B, i32 %phi
2431   ret i32 %sel
2434 define i32 @test_select_into_phi_not_idom_inverted(i1 %cond, i32 %A, i32 %B)  {
2435 ; CHECK-LABEL: @test_select_into_phi_not_idom_inverted(
2436 ; CHECK-NEXT:  entry:
2437 ; CHECK-NEXT:    br i1 [[COND:%.*]], label [[IF_FALSE:%.*]], label [[IF_TRUE:%.*]]
2438 ; CHECK:       if.true:
2439 ; CHECK-NEXT:    br label [[MERGE:%.*]]
2440 ; CHECK:       if.false:
2441 ; CHECK-NEXT:    br label [[MERGE]]
2442 ; CHECK:       merge:
2443 ; CHECK-NEXT:    [[SEL:%.*]] = phi i32 [ [[B:%.*]], [[IF_FALSE]] ], [ [[A:%.*]], [[IF_TRUE]] ]
2444 ; CHECK-NEXT:    br label [[EXIT:%.*]]
2445 ; CHECK:       exit:
2446 ; CHECK-NEXT:    ret i32 [[SEL]]
2448 entry:
2449   %inverted = xor i1 %cond, 1
2450   br i1 %inverted, label %if.true, label %if.false
2452 if.true:
2453   br label %merge
2455 if.false:
2456   br label %merge
2458 merge:
2459   %phi = phi i32 [%A, %if.true], [%B, %if.false]
2460   br label %exit
2462 exit:
2463   %sel = select i1 %cond, i32 %phi, i32 %A
2464   ret i32 %sel
2467 define i32 @test_select_into_phi_not_idom_inverted_2(i1 %cond, i32 %A, i32 %B)  {
2468 ; CHECK-LABEL: @test_select_into_phi_not_idom_inverted_2(
2469 ; CHECK-NEXT:  entry:
2470 ; CHECK-NEXT:    br i1 [[COND:%.*]], label [[IF_FALSE:%.*]], label [[IF_TRUE:%.*]]
2471 ; CHECK:       if.true:
2472 ; CHECK-NEXT:    br label [[MERGE:%.*]]
2473 ; CHECK:       if.false:
2474 ; CHECK-NEXT:    br label [[MERGE]]
2475 ; CHECK:       merge:
2476 ; CHECK-NEXT:    [[SEL:%.*]] = phi i32 [ [[B:%.*]], [[IF_FALSE]] ], [ [[A:%.*]], [[IF_TRUE]] ]
2477 ; CHECK-NEXT:    br label [[EXIT:%.*]]
2478 ; CHECK:       exit:
2479 ; CHECK-NEXT:    ret i32 [[SEL]]
2481 entry:
2482   %inverted = xor i1 %cond, 1
2483   br i1 %inverted, label %if.true, label %if.false
2485 if.true:
2486   br label %merge
2488 if.false:
2489   br label %merge
2491 merge:
2492   %phi = phi i32 [%A, %if.true], [%B, %if.false]
2493   br label %exit
2495 exit:
2496   %sel = select i1 %cond, i32 %B, i32 %phi
2497   ret i32 %sel
2500 define i32 @test_select_into_phi_not_idom_no_dom_input_1(i1 %cond, i32 %A, i32 %B, i32 *%p)  {
2501 ; CHECK-LABEL: @test_select_into_phi_not_idom_no_dom_input_1(
2502 ; CHECK-NEXT:  entry:
2503 ; CHECK-NEXT:    br i1 [[COND:%.*]], label [[IF_TRUE:%.*]], label [[IF_FALSE:%.*]]
2504 ; CHECK:       if.true:
2505 ; CHECK-NEXT:    [[C:%.*]] = load i32, i32* [[P:%.*]], align 4
2506 ; CHECK-NEXT:    br label [[MERGE:%.*]]
2507 ; CHECK:       if.false:
2508 ; CHECK-NEXT:    br label [[MERGE]]
2509 ; CHECK:       merge:
2510 ; CHECK-NEXT:    [[SEL:%.*]] = phi i32 [ [[A:%.*]], [[IF_FALSE]] ], [ [[C]], [[IF_TRUE]] ]
2511 ; CHECK-NEXT:    br label [[EXIT:%.*]]
2512 ; CHECK:       exit:
2513 ; CHECK-NEXT:    ret i32 [[SEL]]
2515 entry:
2516   br i1 %cond, label %if.true, label %if.false
2518 if.true:
2519   %C = load i32, i32* %p
2520   br label %merge
2522 if.false:
2523   br label %merge
2525 merge:
2526   %phi = phi i32 [%C, %if.true], [%B, %if.false]
2527   br label %exit
2529 exit:
2530   %sel = select i1 %cond, i32 %phi, i32 %A
2531   ret i32 %sel
2534 define i32 @test_select_into_phi_not_idom_no_dom_input_2(i1 %cond, i32 %A, i32 %B, i32 *%p)  {
2535 ; CHECK-LABEL: @test_select_into_phi_not_idom_no_dom_input_2(
2536 ; CHECK-NEXT:  entry:
2537 ; CHECK-NEXT:    br i1 [[COND:%.*]], label [[IF_TRUE:%.*]], label [[IF_FALSE:%.*]]
2538 ; CHECK:       if.true:
2539 ; CHECK-NEXT:    br label [[MERGE:%.*]]
2540 ; CHECK:       if.false:
2541 ; CHECK-NEXT:    [[C:%.*]] = load i32, i32* [[P:%.*]], align 4
2542 ; CHECK-NEXT:    br label [[MERGE]]
2543 ; CHECK:       merge:
2544 ; CHECK-NEXT:    [[SEL:%.*]] = phi i32 [ [[C]], [[IF_FALSE]] ], [ [[B:%.*]], [[IF_TRUE]] ]
2545 ; CHECK-NEXT:    br label [[EXIT:%.*]]
2546 ; CHECK:       exit:
2547 ; CHECK-NEXT:    ret i32 [[SEL]]
2549 entry:
2550   br i1 %cond, label %if.true, label %if.false
2552 if.true:
2553   br label %merge
2555 if.false:
2556   %C = load i32, i32* %p
2557   br label %merge
2559 merge:
2560   %phi = phi i32 [%A, %if.true], [%C, %if.false]
2561   br label %exit
2563 exit:
2564   %sel = select i1 %cond, i32 %B, i32 %phi
2565   ret i32 %sel
2568 ; Negative tests to ensure we don't remove selects with undef true/false values.
2569 ; See https://bugs.llvm.org/show_bug.cgi?id=31633
2570 ; https://lists.llvm.org/pipermail/llvm-dev/2016-October/106182.html
2571 ; https://reviews.llvm.org/D83360
2572 define i32 @false_undef(i1 %cond, i32 %x) {
2573 ; CHECK-LABEL: @false_undef(
2574 ; CHECK-NEXT:    [[S:%.*]] = select i1 [[COND:%.*]], i32 [[X:%.*]], i32 undef
2575 ; CHECK-NEXT:    ret i32 [[S]]
2577   %s = select i1 %cond, i32 %x, i32 undef
2578   ret i32 %s
2581 define i32 @true_undef(i1 %cond, i32 %x) {
2582 ; CHECK-LABEL: @true_undef(
2583 ; CHECK-NEXT:    [[S:%.*]] = select i1 [[COND:%.*]], i32 undef, i32 [[X:%.*]]
2584 ; CHECK-NEXT:    ret i32 [[S]]
2586   %s = select i1 %cond, i32 undef, i32 %x
2587   ret i32 %s
2590 define <2 x i32> @false_undef_vec(i1 %cond, <2 x i32> %x) {
2591 ; CHECK-LABEL: @false_undef_vec(
2592 ; CHECK-NEXT:    [[S:%.*]] = select i1 [[COND:%.*]], <2 x i32> [[X:%.*]], <2 x i32> undef
2593 ; CHECK-NEXT:    ret <2 x i32> [[S]]
2595   %s = select i1 %cond, <2 x i32> %x, <2 x i32> undef
2596   ret <2 x i32> %s
2599 define <2 x i32> @true_undef_vec(i1 %cond, <2 x i32> %x) {
2600 ; CHECK-LABEL: @true_undef_vec(
2601 ; CHECK-NEXT:    [[S:%.*]] = select i1 [[COND:%.*]], <2 x i32> undef, <2 x i32> [[X:%.*]]
2602 ; CHECK-NEXT:    ret <2 x i32> [[S]]
2604   %s = select i1 %cond, <2 x i32> undef, <2 x i32> %x
2605   ret <2 x i32> %s
2608 define i8 @cond_freeze(i8 %x, i8 %y) {
2609 ; CHECK-LABEL: @cond_freeze(
2610 ; CHECK-NEXT:    ret i8 [[Y:%.*]]
2612   %cond.fr = freeze i1 undef
2613   %s = select i1 %cond.fr, i8 %x, i8 %y
2614   ret i8 %s
2617 define i8 @cond_freeze_constant_false_val(i8 %x) {
2618 ; CHECK-LABEL: @cond_freeze_constant_false_val(
2619 ; CHECK-NEXT:    ret i8 1
2621   %cond.fr = freeze i1 undef
2622   %s = select i1 %cond.fr, i8 %x, i8 1
2623   ret i8 %s
2626 define i8 @cond_freeze_constant_true_val(i8 %x) {
2627 ; CHECK-LABEL: @cond_freeze_constant_true_val(
2628 ; CHECK-NEXT:    ret i8 1
2630   %cond.fr = freeze i1 undef
2631   %s = select i1 %cond.fr, i8 1, i8 %x
2632   ret i8 %s
2635 define i8 @cond_freeze_both_arms_constant() {
2636 ; CHECK-LABEL: @cond_freeze_both_arms_constant(
2637 ; CHECK-NEXT:    ret i8 42
2639   %cond.fr = freeze i1 undef
2640   %s = select i1 %cond.fr, i8 42, i8 3
2641   ret i8 %s
2644 define <2 x i8> @cond_freeze_constant_true_val_vec(<2 x i8> %x) {
2645 ; CHECK-LABEL: @cond_freeze_constant_true_val_vec(
2646 ; CHECK-NEXT:    ret <2 x i8> <i8 1, i8 2>
2648   %cond.fr = freeze <2 x i1> <i1 undef, i1 undef>
2649   %s = select <2 x i1> %cond.fr, <2 x i8> <i8 1, i8 2>, <2 x i8> %x
2650   ret <2 x i8> %s
2653 define <2 x i8> @partial_cond_freeze_constant_true_val_vec(<2 x i8> %x) {
2654 ; CHECK-LABEL: @partial_cond_freeze_constant_true_val_vec(
2655 ; CHECK-NEXT:    ret <2 x i8> <i8 1, i8 2>
2657   %cond.fr = freeze <2 x i1> <i1 true, i1 undef>
2658   %s = select <2 x i1> %cond.fr, <2 x i8> <i8 1, i8 2>, <2 x i8> %x
2659   ret <2 x i8> %s
2662 define <2 x i8> @partial_cond_freeze_constant_false_val_vec(<2 x i8> %x) {
2663 ; CHECK-LABEL: @partial_cond_freeze_constant_false_val_vec(
2664 ; CHECK-NEXT:    [[S1:%.*]] = insertelement <2 x i8> [[X:%.*]], i8 2, i64 1
2665 ; CHECK-NEXT:    ret <2 x i8> [[S1]]
2667   %cond.fr = freeze <2 x i1> <i1 true, i1 undef>
2668   %s = select <2 x i1> %cond.fr, <2 x i8> %x, <2 x i8> <i8 1, i8 2>
2669   ret <2 x i8> %s
2672 define <2 x i8> @partial_cond_freeze_both_arms_constant_vec() {
2673 ; CHECK-LABEL: @partial_cond_freeze_both_arms_constant_vec(
2674 ; CHECK-NEXT:    ret <2 x i8> <i8 42, i8 2>
2676   %cond.fr = freeze <2 x i1> <i1 false, i1 undef>
2677   %s = select <2 x i1> %cond.fr, <2 x i8> <i8 1, i8 2>, <2 x i8> <i8 42, i8 43>
2678   ret <2 x i8> %s
2681 declare void @foo2(i8, i8)
2683 define void @cond_freeze_multipleuses(i8 %x, i8 %y) {
2684 ; CHECK-LABEL: @cond_freeze_multipleuses(
2685 ; CHECK-NEXT:    call void @foo2(i8 [[Y:%.*]], i8 [[X:%.*]])
2686 ; CHECK-NEXT:    ret void
2688   %cond.fr = freeze i1 undef
2689   %s = select i1 %cond.fr, i8 %x, i8 %y
2690   %s2 = select i1 %cond.fr, i8 %y, i8 %x
2691   call void @foo2(i8 %s, i8 %s2)
2692   ret void
2695 define i32 @select_freeze_icmp_eq(i32 %x, i32 %y) {
2696 ; CHECK-LABEL: @select_freeze_icmp_eq(
2697 ; CHECK-NEXT:    ret i32 [[Y:%.*]]
2699   %c = icmp eq i32 %x, %y
2700   %c.fr = freeze i1 %c
2701   %v = select i1 %c.fr, i32 %x, i32 %y
2702   ret i32 %v
2705 define i32 @select_freeze_icmp_ne(i32 %x, i32 %y) {
2706 ; CHECK-LABEL: @select_freeze_icmp_ne(
2707 ; CHECK-NEXT:    ret i32 [[X:%.*]]
2709   %c = icmp ne i32 %x, %y
2710   %c.fr = freeze i1 %c
2711   %v = select i1 %c.fr, i32 %x, i32 %y
2712   ret i32 %v
2715 define i32 @select_freeze_icmp_else(i32 %x, i32 %y) {
2716 ; CHECK-LABEL: @select_freeze_icmp_else(
2717 ; CHECK-NEXT:    [[C:%.*]] = icmp ult i32 [[X:%.*]], [[Y:%.*]]
2718 ; CHECK-NEXT:    [[C_FR:%.*]] = freeze i1 [[C]]
2719 ; CHECK-NEXT:    [[V:%.*]] = select i1 [[C_FR]], i32 [[X]], i32 [[Y]]
2720 ; CHECK-NEXT:    ret i32 [[V]]
2722   %c = icmp ult i32 %x, %y
2723   %c.fr = freeze i1 %c
2724   %v = select i1 %c.fr, i32 %x, i32 %y
2725   ret i32 %v
2728 declare void @use_i1_i32(i1, i32)
2730 define void @select_freeze_icmp_multuses(i32 %x, i32 %y) {
2731 ; CHECK-LABEL: @select_freeze_icmp_multuses(
2732 ; CHECK-NEXT:    [[C:%.*]] = icmp ne i32 [[X:%.*]], [[Y:%.*]]
2733 ; CHECK-NEXT:    [[C_FR:%.*]] = freeze i1 [[C]]
2734 ; CHECK-NEXT:    [[V:%.*]] = select i1 [[C_FR]], i32 [[X]], i32 [[Y]]
2735 ; CHECK-NEXT:    call void @use_i1_i32(i1 [[C_FR]], i32 [[V]])
2736 ; CHECK-NEXT:    ret void
2738   %c = icmp ne i32 %x, %y
2739   %c.fr = freeze i1 %c
2740   %v = select i1 %c.fr, i32 %x, i32 %y
2741   call void @use_i1_i32(i1 %c.fr, i32 %v)
2742   ret void
2745 define i32 @pr47322_more_poisonous_replacement(i32 %arg) {
2746 ; CHECK-LABEL: @pr47322_more_poisonous_replacement(
2747 ; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[ARG:%.*]], 0
2748 ; CHECK-NEXT:    [[TRAILING:%.*]] = call i32 @llvm.cttz.i32(i32 [[ARG]], i1 immarg true), !range [[RNG0:![0-9]+]]
2749 ; CHECK-NEXT:    [[SHIFTED:%.*]] = lshr i32 [[ARG]], [[TRAILING]]
2750 ; CHECK-NEXT:    [[R1_SROA_0_1:%.*]] = select i1 [[CMP]], i32 0, i32 [[SHIFTED]]
2751 ; CHECK-NEXT:    ret i32 [[R1_SROA_0_1]]
2753   %cmp = icmp eq i32 %arg, 0
2754   %trailing = call i32 @llvm.cttz.i32(i32 %arg, i1 immarg true)
2755   %shifted = lshr i32 %arg, %trailing
2756   %r1.sroa.0.1 = select i1 %cmp, i32 0, i32 %shifted
2757   ret i32 %r1.sroa.0.1
2760 define i8 @select_replacement_add_eq(i8 %x, i8 %y) {
2761 ; CHECK-LABEL: @select_replacement_add_eq(
2762 ; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i8 [[X:%.*]], 1
2763 ; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[CMP]], i8 2, i8 [[Y:%.*]]
2764 ; CHECK-NEXT:    ret i8 [[SEL]]
2766   %cmp = icmp eq i8 %x, 1
2767   %add = add i8 %x, 1
2768   %sel = select i1 %cmp, i8 %add, i8 %y
2769   ret i8 %sel
2772 define i8 @select_replacement_add_ne(i8 %x, i8 %y) {
2773 ; CHECK-LABEL: @select_replacement_add_ne(
2774 ; CHECK-NEXT:    [[CMP:%.*]] = icmp ne i8 [[X:%.*]], 1
2775 ; CHECK-NEXT:    call void @use(i1 [[CMP]])
2776 ; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[CMP]], i8 [[Y:%.*]], i8 2
2777 ; CHECK-NEXT:    ret i8 [[SEL]]
2779   %cmp = icmp ne i8 %x, 1
2780   call void @use(i1 %cmp)
2781   %add = add i8 %x, 1
2782   %sel = select i1 %cmp, i8 %y, i8 %add
2783   ret i8 %sel
2786 define i8 @select_replacement_add_nuw(i8 %x, i8 %y) {
2787 ; CHECK-LABEL: @select_replacement_add_nuw(
2788 ; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i8 [[X:%.*]], 1
2789 ; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[CMP]], i8 2, i8 [[Y:%.*]]
2790 ; CHECK-NEXT:    ret i8 [[SEL]]
2792   %cmp = icmp eq i8 %x, 1
2793   %add = add nuw i8 %x, 1
2794   %sel = select i1 %cmp, i8 %add, i8 %y
2795   ret i8 %sel
2798 define i8 @select_replacement_sub_noundef(i8 %x, i8 noundef %y, i8 %z) {
2799 ; CHECK-LABEL: @select_replacement_sub_noundef(
2800 ; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i8 [[X:%.*]], [[Y:%.*]]
2801 ; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[CMP]], i8 0, i8 [[Z:%.*]]
2802 ; CHECK-NEXT:    ret i8 [[SEL]]
2804   %cmp = icmp eq i8 %x, %y
2805   %sub = sub i8 %x, %y
2806   %sel = select i1 %cmp, i8 %sub, i8 %z
2807   ret i8 %sel
2810 ; TODO: The transform is also safe without noundef.
2811 define i8 @select_replacement_sub(i8 %x, i8 %y, i8 %z) {
2812 ; CHECK-LABEL: @select_replacement_sub(
2813 ; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i8 [[X:%.*]], [[Y:%.*]]
2814 ; CHECK-NEXT:    [[SUB:%.*]] = sub i8 [[X]], [[Y]]
2815 ; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[CMP]], i8 [[SUB]], i8 [[Z:%.*]]
2816 ; CHECK-NEXT:    ret i8 [[SEL]]
2818   %cmp = icmp eq i8 %x, %y
2819   %sub = sub i8 %x, %y
2820   %sel = select i1 %cmp, i8 %sub, i8 %z
2821   ret i8 %sel
2824 define i8 @select_replacement_shift_noundef(i8 %x, i8 %y, i8 %z) {
2825 ; CHECK-LABEL: @select_replacement_shift_noundef(
2826 ; CHECK-NEXT:    [[SHR:%.*]] = lshr exact i8 [[X:%.*]], 1
2827 ; CHECK-NEXT:    call void @use_i8(i8 noundef [[SHR]])
2828 ; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i8 [[SHR]], [[Y:%.*]]
2829 ; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[CMP]], i8 [[X]], i8 [[Z:%.*]]
2830 ; CHECK-NEXT:    ret i8 [[SEL]]
2832   %shr = lshr exact i8 %x, 1
2833   call void @use_i8(i8 noundef %shr)
2834   %cmp = icmp eq i8 %shr, %y
2835   %shl = shl i8 %y, 1
2836   %sel = select i1 %cmp, i8 %shl, i8 %z
2837   ret i8 %sel
2840 ; TODO: The transform is also safe without noundef.
2841 define i8 @select_replacement_shift(i8 %x, i8 %y, i8 %z) {
2842 ; CHECK-LABEL: @select_replacement_shift(
2843 ; CHECK-NEXT:    [[SHR:%.*]] = lshr exact i8 [[X:%.*]], 1
2844 ; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i8 [[SHR]], [[Y:%.*]]
2845 ; CHECK-NEXT:    [[SHL:%.*]] = shl i8 [[Y]], 1
2846 ; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[CMP]], i8 [[SHL]], i8 [[Z:%.*]]
2847 ; CHECK-NEXT:    ret i8 [[SEL]]
2849   %shr = lshr exact i8 %x, 1
2850   %cmp = icmp eq i8 %shr, %y
2851   %shl = shl i8 %y, 1
2852   %sel = select i1 %cmp, i8 %shl, i8 %z
2853   ret i8 %sel
2856 define i8 @select_replacement_loop(i8 %x, i8 %y, i8 %z) {
2857 ; CHECK-LABEL: @select_replacement_loop(
2858 ; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i8 [[X:%.*]], [[Y:%.*]]
2859 ; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[CMP]], i8 [[X]], i8 [[Z:%.*]]
2860 ; CHECK-NEXT:    ret i8 [[SEL]]
2862   %cmp = icmp eq i8 %x, %y
2863   %sel = select i1 %cmp, i8 %x, i8 %z
2864   ret i8 %sel
2867 define i32 @select_replacement_loop2(i32 %arg, i32 %arg2) {
2868 ; CHECK-LABEL: @select_replacement_loop2(
2869 ; CHECK-NEXT:    [[DIV:%.*]] = udiv i32 [[ARG:%.*]], [[ARG2:%.*]]
2870 ; CHECK-NEXT:    [[MUL:%.*]] = mul nsw i32 [[DIV]], [[ARG2]]
2871 ; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[MUL]], [[ARG]]
2872 ; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[CMP]], i32 [[DIV]], i32 undef
2873 ; CHECK-NEXT:    ret i32 [[SEL]]
2875   %div = udiv i32 %arg, %arg2
2876   %mul = mul nsw i32 %div, %arg2
2877   %cmp = icmp eq i32 %mul, %arg
2878   %sel = select i1 %cmp, i32 %div, i32 undef
2879   ret i32 %sel
2882 ; TODO: Dropping the inbounds flag should not be necessary for this fold.
2883 define i8* @select_replacement_gep_inbounds(i8* %base, i64 %offset) {
2884 ; CHECK-LABEL: @select_replacement_gep_inbounds(
2885 ; CHECK-NEXT:    [[GEP:%.*]] = getelementptr i8, i8* [[BASE:%.*]], i64 [[OFFSET:%.*]]
2886 ; CHECK-NEXT:    ret i8* [[GEP]]
2888   %cmp = icmp eq i64 %offset, 0
2889   %gep = getelementptr inbounds i8, i8* %base, i64 %offset
2890   %sel = select i1 %cmp, i8* %base, i8* %gep
2891   ret i8* %sel
2894 define <2 x i1> @partial_true_undef_condval(<2 x i1> %x) {
2895 ; CHECK-LABEL: @partial_true_undef_condval(
2896 ; CHECK-NEXT:    ret <2 x i1> <i1 true, i1 poison>
2898   %r = select <2 x i1> <i1 true, i1 poison>, <2 x i1> <i1 true, i1 poison>, <2 x i1> %x
2899   ret <2 x i1> %r
2902 define <2 x i1> @partial_false_undef_condval(<2 x i1> %x) {
2903 ; CHECK-LABEL: @partial_false_undef_condval(
2904 ; CHECK-NEXT:    ret <2 x i1> <i1 false, i1 poison>
2906   %r = select <2 x i1> <i1 false, i1 poison>, <2 x i1> %x, <2 x i1> <i1 false, i1 poison>
2907   ret <2 x i1> %r
2910 ; select (x == 0), 0, x * y --> freeze(y) * x
2911 define i32 @mul_select_eq_zero(i32 %x, i32 %y) {
2912 ; CHECK-LABEL: @mul_select_eq_zero(
2913 ; CHECK-NEXT:    [[Y_FR:%.*]] = freeze i32 [[Y:%.*]]
2914 ; CHECK-NEXT:    [[M:%.*]] = mul i32 [[Y_FR]], [[X:%.*]]
2915 ; CHECK-NEXT:    ret i32 [[M]]
2917   %c = icmp eq i32 %x, 0
2918   %m = mul i32 %x, %y
2919   %r = select i1 %c, i32 0, i32 %m
2920   ret i32 %r
2923 ; select (y == 0), 0, x * y --> freeze(x) * y
2924 define i32 @mul_select_eq_zero_commute(i32 %x, i32 %y) {
2925 ; CHECK-LABEL: @mul_select_eq_zero_commute(
2926 ; CHECK-NEXT:    [[X_FR:%.*]] = freeze i32 [[X:%.*]]
2927 ; CHECK-NEXT:    [[M:%.*]] = mul i32 [[X_FR]], [[Y:%.*]]
2928 ; CHECK-NEXT:    ret i32 [[M]]
2930   %c = icmp eq i32 %y, 0
2931   %m = mul i32 %x, %y
2932   %r = select i1 %c, i32 0, i32 %m
2933   ret i32 %r
2936 ; Check that mul's flags preserved during the transformation.
2937 define i32 @mul_select_eq_zero_copy_flags(i32 %x, i32 %y) {
2938 ; CHECK-LABEL: @mul_select_eq_zero_copy_flags(
2939 ; CHECK-NEXT:    [[Y_FR:%.*]] = freeze i32 [[Y:%.*]]
2940 ; CHECK-NEXT:    [[M:%.*]] = mul nuw nsw i32 [[Y_FR]], [[X:%.*]]
2941 ; CHECK-NEXT:    ret i32 [[M]]
2943   %c = icmp eq i32 %x, 0
2944   %m = mul nuw nsw i32 %x, %y
2945   %r = select i1 %c, i32 0, i32 %m
2946   ret i32 %r
2949 ; Check that the transformation could be applied after condition's inversion.
2950 ; select (x != 0), x * y, 0 --> freeze(y) * x
2951 define i32 @mul_select_ne_zero(i32 %x, i32 %y) {
2952 ; CHECK-LABEL: @mul_select_ne_zero(
2953 ; CHECK-NEXT:    [[C:%.*]] = icmp ne i32 [[X:%.*]], 0
2954 ; CHECK-NEXT:    [[Y_FR:%.*]] = freeze i32 [[Y:%.*]]
2955 ; CHECK-NEXT:    [[M:%.*]] = mul i32 [[Y_FR]], [[X]]
2956 ; CHECK-NEXT:    call void @use(i1 [[C]])
2957 ; CHECK-NEXT:    ret i32 [[M]]
2959   %c = icmp ne i32 %x, 0
2960   %m = mul i32 %x, %y
2961   %r = select i1 %c, i32 %m, i32 0
2962   call void @use(i1 %c)
2963   ret i32 %r
2966 ; Check that if one of a select's branches returns undef then
2967 ; an expression could be folded into mul as if there was a 0 instead of undef.
2968 ; select (x == 0), undef, x * y --> freeze(y) * x
2969 define i32 @mul_select_eq_zero_sel_undef(i32 %x, i32 %y) {
2970 ; CHECK-LABEL: @mul_select_eq_zero_sel_undef(
2971 ; CHECK-NEXT:    [[Y_FR:%.*]] = freeze i32 [[Y:%.*]]
2972 ; CHECK-NEXT:    [[M:%.*]] = mul i32 [[Y_FR]], [[X:%.*]]
2973 ; CHECK-NEXT:    ret i32 [[M]]
2975   %c = icmp eq i32 %x, 0
2976   %m = mul i32 %x, %y
2977   %r = select i1 %c, i32 undef, i32 %m
2978   ret i32 %r
2981 ; Check that the transformation is applied disregard to a number
2982 ; of expression's users.
2983 define i32 @mul_select_eq_zero_multiple_users(i32 %x, i32 %y) {
2984 ; CHECK-LABEL: @mul_select_eq_zero_multiple_users(
2985 ; CHECK-NEXT:    [[Y_FR:%.*]] = freeze i32 [[Y:%.*]]
2986 ; CHECK-NEXT:    [[M:%.*]] = mul i32 [[Y_FR]], [[X:%.*]]
2987 ; CHECK-NEXT:    call void @use_i32(i32 [[M]])
2988 ; CHECK-NEXT:    call void @use_i32(i32 [[M]])
2989 ; CHECK-NEXT:    call void @use_i32(i32 [[M]])
2990 ; CHECK-NEXT:    ret i32 [[M]]
2992   %m = mul i32 %x, %y
2993   call void @use_i32(i32 %m)
2994   %c = icmp eq i32 %x, 0
2995   %r = select i1 %c, i32 0, i32 %m
2996   call void @use_i32(i32 %m)
2997   call void @use_i32(i32 %r)
2998   ret i32 %r
3001 ; Negative test: select's condition is unrelated to multiplied values,
3002 ; so the transformation should not be applied.
3003 define i32 @mul_select_eq_zero_unrelated_condition(i32 %x, i32 %y, i32 %z) {
3004 ; CHECK-LABEL: @mul_select_eq_zero_unrelated_condition(
3005 ; CHECK-NEXT:    [[C:%.*]] = icmp eq i32 [[Z:%.*]], 0
3006 ; CHECK-NEXT:    [[M:%.*]] = mul i32 [[X:%.*]], [[Y:%.*]]
3007 ; CHECK-NEXT:    [[R:%.*]] = select i1 [[C]], i32 0, i32 [[M]]
3008 ; CHECK-NEXT:    ret i32 [[R]]
3010   %c = icmp eq i32 %z, 0
3011   %m = mul i32 %x, %y
3012   %r = select i1 %c, i32 0, i32 %m
3013   ret i32 %r
3016 ; select (<k x elt> x == 0), <k x elt> 0, <k x elt> x * y --> freeze(y) * x
3017 define <4 x i32> @mul_select_eq_zero_vector(<4 x i32> %x, <4 x i32> %y) {
3018 ; CHECK-LABEL: @mul_select_eq_zero_vector(
3019 ; CHECK-NEXT:    [[Y_FR:%.*]] = freeze <4 x i32> [[Y:%.*]]
3020 ; CHECK-NEXT:    [[M:%.*]] = mul <4 x i32> [[Y_FR]], [[X:%.*]]
3021 ; CHECK-NEXT:    ret <4 x i32> [[M]]
3023   %c = icmp eq <4 x i32> %x, zeroinitializer
3024   %m = mul <4 x i32> %x, %y
3025   %r = select <4 x i1> %c, <4 x i32> zeroinitializer, <4 x i32> %m
3026   ret <4 x i32> %r
3029 ; Check that a select is folded into multiplication if condition's operand
3030 ; is a vector consisting of zeros and undefs.
3031 ; select (<k x elt> x == {0, undef, ...}), <k x elt> 0, <k x elt> x * y --> freeze(y) * x
3032 define <2 x i32> @mul_select_eq_undef_vector(<2 x i32> %x, <2 x i32> %y) {
3033 ; CHECK-LABEL: @mul_select_eq_undef_vector(
3034 ; CHECK-NEXT:    [[Y_FR:%.*]] = freeze <2 x i32> [[Y:%.*]]
3035 ; CHECK-NEXT:    [[M:%.*]] = mul <2 x i32> [[Y_FR]], [[X:%.*]]
3036 ; CHECK-NEXT:    ret <2 x i32> [[M]]
3038   %c = icmp eq <2 x i32> %x, <i32 0, i32 undef>
3039   %m = mul <2 x i32> %x, %y
3040   %r = select <2 x i1> %c, <2 x i32> <i32 0, i32 42>, <2 x i32> %m
3041   ret <2 x i32> %r
3044 ; Check that a select is folded into multiplication if other select's operand
3045 ; is a vector consisting of zeros and undefs.
3046 ; select (<k x elt> x == 0), <k x elt> {0, undef, ...}, <k x elt> x * y --> freeze(y) * x
3047 define <2 x i32> @mul_select_eq_zero_sel_undef_vector(<2 x i32> %x, <2 x i32> %y) {
3048 ; CHECK-LABEL: @mul_select_eq_zero_sel_undef_vector(
3049 ; CHECK-NEXT:    [[Y_FR:%.*]] = freeze <2 x i32> [[Y:%.*]]
3050 ; CHECK-NEXT:    [[M:%.*]] = mul <2 x i32> [[Y_FR]], [[X:%.*]]
3051 ; CHECK-NEXT:    ret <2 x i32> [[M]]
3053   %c = icmp eq <2 x i32> %x, zeroinitializer
3054   %m = mul <2 x i32> %x, %y
3055   %r = select <2 x i1> %c, <2 x i32> <i32 0, i32 undef>, <2 x i32> %m
3056   ret <2 x i32> %r
3059 ; Negative test: select should not be folded into mul because
3060 ; condition's operand and select's operand do not merge into zero vector.
3061 define <2 x i32> @mul_select_eq_undef_vector_not_merging_to_zero(<2 x i32> %x, <2 x i32> %y) {
3062 ; CHECK-LABEL: @mul_select_eq_undef_vector_not_merging_to_zero(
3063 ; CHECK-NEXT:    [[C:%.*]] = icmp eq <2 x i32> [[X:%.*]], <i32 0, i32 undef>
3064 ; CHECK-NEXT:    [[M:%.*]] = mul <2 x i32> [[X]], [[Y:%.*]]
3065 ; CHECK-NEXT:    [[R:%.*]] = select <2 x i1> [[C]], <2 x i32> <i32 1, i32 0>, <2 x i32> [[M]]
3066 ; CHECK-NEXT:    ret <2 x i32> [[R]]
3068   %c = icmp eq <2 x i32> %x, <i32 0, i32 undef>
3069   %m = mul <2 x i32> %x, %y
3070   %r = select <2 x i1> %c, <2 x i32> <i32 1, i32 0>, <2 x i32> %m
3071   ret <2 x i32> %r
3074 define i8 @ne0_is_all_ones(i8 %x) {
3075 ; CHECK-LABEL: @ne0_is_all_ones(
3076 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp ne i8 [[X:%.*]], 0
3077 ; CHECK-NEXT:    [[R:%.*]] = sext i1 [[TMP1]] to i8
3078 ; CHECK-NEXT:    ret i8 [[R]]
3080   %negx = sub i8 0, %x
3081   %ugt1 = icmp ugt i8 %x, 1
3082   %r = select i1 %ugt1, i8 -1, i8 %negx
3083   ret i8 %r
3086 define i8 @ne0_is_all_ones_use1(i8 %x) {
3087 ; CHECK-LABEL: @ne0_is_all_ones_use1(
3088 ; CHECK-NEXT:    [[NEGX:%.*]] = sub i8 0, [[X:%.*]]
3089 ; CHECK-NEXT:    call void @use_i8(i8 [[NEGX]])
3090 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp ne i8 [[X]], 0
3091 ; CHECK-NEXT:    [[R:%.*]] = sext i1 [[TMP1]] to i8
3092 ; CHECK-NEXT:    ret i8 [[R]]
3094   %negx = sub i8 0, %x
3095   call void @use_i8(i8 %negx)
3096   %ugt1 = icmp ugt i8 %x, 1
3097   %r = select i1 %ugt1, i8 -1, i8 %negx
3098   ret i8 %r
3101 ; negative test
3103 define i8 @ne0_is_all_ones_use2(i8 %x) {
3104 ; CHECK-LABEL: @ne0_is_all_ones_use2(
3105 ; CHECK-NEXT:    [[NEGX:%.*]] = sub i8 0, [[X:%.*]]
3106 ; CHECK-NEXT:    [[UGT1:%.*]] = icmp ugt i8 [[X]], 1
3107 ; CHECK-NEXT:    call void @use(i1 [[UGT1]])
3108 ; CHECK-NEXT:    [[R:%.*]] = select i1 [[UGT1]], i8 -1, i8 [[NEGX]]
3109 ; CHECK-NEXT:    ret i8 [[R]]
3111   %negx = sub i8 0, %x
3112   %ugt1 = icmp ugt i8 %x, 1
3113   call void @use(i1 %ugt1)
3114   %r = select i1 %ugt1, i8 -1, i8 %negx
3115   ret i8 %r
3118 ; negative test
3120 define i8 @ne0_is_all_ones_wrong_pred(i8 %x) {
3121 ; CHECK-LABEL: @ne0_is_all_ones_wrong_pred(
3122 ; CHECK-NEXT:    [[NEGX:%.*]] = sub i8 0, [[X:%.*]]
3123 ; CHECK-NEXT:    [[UGT1:%.*]] = icmp sgt i8 [[X]], 2
3124 ; CHECK-NEXT:    [[R:%.*]] = select i1 [[UGT1]], i8 -1, i8 [[NEGX]]
3125 ; CHECK-NEXT:    ret i8 [[R]]
3127   %negx = sub i8 0, %x
3128   %ugt1 = icmp sgt i8 %x, 2
3129   %r = select i1 %ugt1, i8 -1, i8 %negx
3130   ret i8 %r
3133 ; negative test
3135 define i8 @ne0_is_all_ones_wrong_cmp(i8 %x) {
3136 ; CHECK-LABEL: @ne0_is_all_ones_wrong_cmp(
3137 ; CHECK-NEXT:    [[NEGX:%.*]] = sub i8 0, [[X:%.*]]
3138 ; CHECK-NEXT:    [[UGT1:%.*]] = icmp ugt i8 [[X]], 2
3139 ; CHECK-NEXT:    [[R:%.*]] = select i1 [[UGT1]], i8 -1, i8 [[NEGX]]
3140 ; CHECK-NEXT:    ret i8 [[R]]
3142   %negx = sub i8 0, %x
3143   %ugt1 = icmp ugt i8 %x, 2
3144   %r = select i1 %ugt1, i8 -1, i8 %negx
3145   ret i8 %r
3148 ; negative test
3150 define i8 @ne0_is_all_ones_wrong_sel(i8 %x) {
3151 ; CHECK-LABEL: @ne0_is_all_ones_wrong_sel(
3152 ; CHECK-NEXT:    [[NEGX:%.*]] = sub i8 0, [[X:%.*]]
3153 ; CHECK-NEXT:    [[UGT1:%.*]] = icmp ugt i8 [[X]], 2
3154 ; CHECK-NEXT:    [[R:%.*]] = select i1 [[UGT1]], i8 1, i8 [[NEGX]]
3155 ; CHECK-NEXT:    ret i8 [[R]]
3157   %negx = sub i8 0, %x
3158   %ugt1 = icmp ugt i8 %x, 2
3159   %r = select i1 %ugt1, i8 1, i8 %negx
3160   ret i8 %r
3163 define <2 x i8> @ne0_is_all_ones_swap_vec(<2 x i8> %x) {
3164 ; CHECK-LABEL: @ne0_is_all_ones_swap_vec(
3165 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp ne <2 x i8> [[X:%.*]], zeroinitializer
3166 ; CHECK-NEXT:    [[R:%.*]] = sext <2 x i1> [[TMP1]] to <2 x i8>
3167 ; CHECK-NEXT:    ret <2 x i8> [[R]]
3169   %negx = sub <2 x i8> zeroinitializer, %x
3170   %ult2 = icmp ult <2 x i8> %x, <i8 2, i8 2>
3171   %r = select <2 x i1> %ult2, <2 x i8> %negx, <2 x i8> <i8 -1, i8 -1>
3172   ret <2 x i8> %r
3175 define <2 x i8> @ne0_is_all_ones_swap_vec_poison(<2 x i8> %x) {
3176 ; CHECK-LABEL: @ne0_is_all_ones_swap_vec_poison(
3177 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp ne <2 x i8> [[X:%.*]], zeroinitializer
3178 ; CHECK-NEXT:    [[R:%.*]] = sext <2 x i1> [[TMP1]] to <2 x i8>
3179 ; CHECK-NEXT:    ret <2 x i8> [[R]]
3181   %negx = sub <2 x i8> <i8 0, i8 poison>, %x
3182   %ult2 = icmp ult <2 x i8> %x, <i8 2, i8 poison>
3183   %r = select <2 x i1> %ult2, <2 x i8> %negx, <2 x i8> <i8 -1, i8 poison>
3184   ret <2 x i8> %r
3187 declare void @use(i1)
3188 declare void @use_i8(i8)
3189 declare void @use_i32(i32)
3190 declare i32 @llvm.cttz.i32(i32, i1 immarg)