1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt < %s -passes=instcombine -S | FileCheck %s
3
4; PR1822
5
6target datalayout = "e-p:64:64-p1:16:16-p2:32:32:32-p3:64:64:64"
7
8define i1 @test5(i1 %C) {
9; CHECK-LABEL: @test5(
10; CHECK-NEXT:    [[NOT_C:%.*]] = xor i1 [[C:%.*]], true
11; CHECK-NEXT:    ret i1 [[NOT_C]]
12;
13  %V = select i1 %C, i1 false, i1 true
14  ret i1 %V
15}
16
17define i32 @test6(i1 %C) {
18; CHECK-LABEL: @test6(
19; CHECK-NEXT:    [[V:%.*]] = zext i1 [[C:%.*]] to i32
20; CHECK-NEXT:    ret i32 [[V]]
21;
22  %V = select i1 %C, i32 1, i32 0
23  ret i32 %V
24}
25
26define 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]]
30;
31  %R = select i1 %C, i1 true, i1 %X
32  ret i1 %R
33}
34
35define <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]]
39;
40  %R = select <2 x i1> %C, <2 x i1> <i1 true, i1 true>, <2 x i1> %X
41  ret <2 x i1> %R
42}
43
44define <2 x i1> @trueval_is_true_vec_undef_elt(<2 x i1> %C, <2 x i1> %X) {
45; CHECK-LABEL: @trueval_is_true_vec_undef_elt(
46; CHECK-NEXT:    [[R:%.*]] = select <2 x i1> [[C:%.*]], <2 x i1> <i1 undef, i1 true>, <2 x i1> [[X:%.*]]
47; CHECK-NEXT:    ret <2 x i1> [[R]]
48;
49  %R = select <2 x i1> %C, <2 x i1> <i1 undef, i1 true>, <2 x i1> %X
50  ret <2 x i1> %R
51}
52
53define 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]]
57;
58  %R = select i1 %C, i1 %X, i1 false
59  ret i1 %R
60}
61
62define <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]]
66;
67  %R = select <2 x i1> %C, <2 x i1> %X, <2 x i1> <i1 false, i1 false>
68  ret <2 x i1> %R
69}
70
71define <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]]
75;
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
78}
79
80define 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]]
85;
86  %R = select i1 %C, i1 false, i1 %X
87  ret i1 %R
88}
89
90define <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]]
95;
96  %R = select <2 x i1> %C, <2 x i1> <i1 false, i1 false>, <2 x i1> %X
97  ret <2 x i1> %R
98}
99
100define <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]]
105;
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
108}
109
110define 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]]
115;
116  %R = select i1 %C, i1 %X, i1 true
117  ret i1 %R
118}
119
120define <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]]
125;
126  %R = select <2 x i1> %C, <2 x i1> %X, <2 x i1> <i1 true, i1 true>
127  ret <2 x i1> %R
128}
129
130define 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]]
134;
135  %c = select i1 %a, i1 %b, i1 %a
136  ret i1 %c
137}
138
139define <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]]
143;
144  %c = select <2 x i1> %a, <2 x i1> %b, <2 x i1> %a
145  ret <2 x i1> %c
146}
147
148define 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]]
152;
153  %c = select i1 %a, i1 %a, i1 %b
154  ret i1 %c
155}
156
157define <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]]
161;
162  %c = select <2 x i1> %a, <2 x i1> %a, <2 x i1> %b
163  ret <2 x i1> %c
164}
165
166define 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]]
171;
172  %not = xor i1 %A, true
173  %C = select i1 %A, i1 %not, i1 %B
174  ret i1 %C
175}
176
177define <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]]
182;
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
186}
187
188define 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]]
193;
194  %not = xor i1 %A, true
195  %C = select i1 %A, i1 %B, i1 %not
196  ret i1 %C
197}
198
199define <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]]
204;
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
208}
209
210define 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]]
215;
216  %C = icmp eq i32 %a, 0
217  %R = select i1 %C, i32 0, i32 1
218  ret i32 %R
219}
220
221define 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]]
226;
227  %b = or i32 %a, 1
228  %c = select i1 %cond, i32 %b, i32 %a
229  ret i32 %c
230}
231
232define <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]]
237;
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
241}
242
243define 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]]
248;
249  %b = ashr i32 %a, 1
250  %c = select i1 %cond, i32 %b, i32 %a
251  ret i32 %c
252}
253
254define <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]]
259;
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
263}
264
265define 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]]
271;
272  %b = ashr i32 %a, 1
273  %d = select i1 %cond, i32 %a, i32 %b
274  ret i32 %d
275}
276
277define <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]]
283;
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
287}
288
289define i32 @test13(i32 %a, i32 %b) {
290; CHECK-LABEL: @test13(
291; CHECK-NEXT:    ret i32 [[B:%.*]]
292;
293  %C = icmp eq i32 %a, %b
294  %V = select i1 %C, i32 %a, i32 %b
295  ret i32 %V
296}
297
298define i32 @test13a(i32 %a, i32 %b) {
299; CHECK-LABEL: @test13a(
300; CHECK-NEXT:    ret i32 [[A:%.*]]
301;
302  %C = icmp ne i32 %a, %b
303  %V = select i1 %C, i32 %a, i32 %b
304  ret i32 %V
305}
306
307define i32 @test13b(i32 %a, i32 %b) {
308; CHECK-LABEL: @test13b(
309; CHECK-NEXT:    ret i32 [[A:%.*]]
310;
311  %C = icmp eq i32 %a, %b
312  %V = select i1 %C, i32 %b, i32 %a
313  ret i32 %V
314}
315
316define 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]]
322;
323  %V = select i1 %C, i32 %X, i32 0
324  ; (X < 1) | !C
325  %R = icmp slt i32 %V, 1
326  ret i1 %R
327}
328
329define 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]]
334;
335  %V = select i1 %C, i32 0, i32 %X
336  ; (X < 1) | C
337  %R = icmp slt i32 %V, 1
338  ret i1 %R
339}
340
341define 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]]
345;
346  %P2 = select i1 %C, i32* %P, i32* null
347  %V = load i32, i32* %P2
348  ret i32 %V
349}
350
351;; It may be legal to load from a null address in a non-zero address space
352define 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]]
357;
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
361}
362
363define 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]]
368;
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
372}
373
374;; It may be legal to load from a null address with null pointer valid attribute.
375define 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]]
380;
381  %P2 = select i1 %C, i32* %P, i32* null
382  %V = load i32, i32* %P2
383  ret i32 %V
384}
385
386define 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]]
391;
392  %P2 = select i1 %C, i32* null, i32* %P
393  %V = load i32, i32* %P2
394  ret i32 %V
395}
396
397attributes #0 = { null_pointer_is_valid }
398
399define 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]]
405;
406  %R = select i1 %C, i32* %X, i32* null
407  %RV = icmp eq i32* %R, null
408  ret i1 %RV
409}
410
411define 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]]
415;
416  %R = select i1 %C, i32 %X, i32 0
417  %V = sdiv i32 %Y, %R
418  ret i32 %V
419}
420
421define i32 @test19(i32 %x) {
422; CHECK-LABEL: @test19(
423; CHECK-NEXT:    [[X_LOBIT:%.*]] = ashr i32 [[X:%.*]], 31
424; CHECK-NEXT:    ret i32 [[X_LOBIT]]
425;
426  %t = icmp ugt i32 %x, 2147483647
427  %retval = select i1 %t, i32 -1, i32 0
428  ret i32 %retval
429}
430
431define i32 @test20(i32 %x) {
432; CHECK-LABEL: @test20(
433; CHECK-NEXT:    [[X_LOBIT:%.*]] = ashr i32 [[X:%.*]], 31
434; CHECK-NEXT:    ret i32 [[X_LOBIT]]
435;
436  %t = icmp slt i32 %x, 0
437  %retval = select i1 %t, i32 -1, i32 0
438  ret i32 %retval
439}
440
441define 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]]
446;
447  %t = icmp slt i32 %x, 0
448  %retval = select i1 %t, i64 -1, i64 0
449  ret i64 %retval
450}
451
452define 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]]
457;
458  %t = icmp slt i32 %x, 0
459  %retval = select i1 %t, i16 -1, i16 0
460  ret i16 %retval
461}
462
463define 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]]
472;
473entry:
474  br i1 %c, label %jump, label %ret
475jump:
476  br label %ret
477ret:
478  %a = phi i1 [true, %jump], [false, %entry]
479  %b = select i1 %a, i32 10, i32 20
480  ret i32 %b
481}
482
483define 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]]
492;
493entry:
494  br i1 %cond, label %jump, label %ret
495jump:
496  %c = or i1 false, false
497  br label %ret
498ret:
499  %a = phi i1 [true, %entry], [%c, %jump]
500  %b = select i1 %a, i32 20, i32 10
501  ret i32 %b
502}
503
504define 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]]
513;
514entry:
515  br i1 %cond, label %jump, label %ret
516jump:
517  %c = select i1 false, i1 true, i1 false
518  br label %ret
519ret:
520  %a = phi i1 [true, %entry], [%c, %jump]
521  %b = select i1 %a, i32 20, i32 10
522  ret i32 %b
523}
524
525define 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]]
534;
535entry:
536  br i1 %c, label %jump, label %ret
537jump:
538  br label %ret
539ret:
540  %p = phi i1 [true, %jump], [false, %entry]
541  %s = select i1 %p, i32 %A, i32 %B
542  ret i32 %s
543}
544
545define 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]]
554;
555entry:
556  br i1 %cond, label %jump, label %ret
557jump:
558  br label %ret
559ret:
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
564}
565
566define 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]]
577;
578entry:
579  br i1 %cond, label %jump, label %ret
580jump:
581  br label %ret
582ret:
583  %c = phi i32 [%A, %jump], [%B, %entry]
584  %p = phi i1 [true, %jump], [false, %entry]
585  br label %next
586
587next:
588  %s = select i1 %p, i32 %A, i32 %c
589  ret i32 %s
590}
591
592; SMAX(SMAX(x, y), x) -> SMAX(x, y)
593define 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]]
597;
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
603}
604
605; UMAX(UMAX(x, y), x) -> UMAX(x, y)
606define 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]]
610;
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
616}
617
618; SMIN(SMIN(x, y), x) -> SMIN(x, y)
619define 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]]
623;
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
629}
630
631; MAX(MIN(x, y), x) -> x
632define i32 @test33(i32 %x, i32 %y) {
633; CHECK-LABEL: @test33(
634; CHECK-NEXT:    ret i32 [[X:%.*]]
635;
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
641}
642
643; MIN(MAX(x, y), x) -> x
644define i32 @test34(i32 %x, i32 %y) {
645; CHECK-LABEL: @test34(
646; CHECK-NEXT:    ret i32 [[X:%.*]]
647;
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
653}
654
655define i1 @test38(i1 %cond) {
656; CHECK-LABEL: @test38(
657; CHECK-NEXT:    ret i1 false
658;
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
664}
665
666define i1 @test39(i1 %cond, double %x) {
667; CHECK-LABEL: @test39(
668; CHECK-NEXT:    ret i1 true
669;
670  %s = select i1 %cond, double %x, double 0x7FF0000000000000 ; RHS = +infty
671  %cmp = fcmp ule double %x, %s
672  ret i1 %cmp
673}
674
675define i1 @test40(i1 %cond) {
676; CHECK-LABEL: @test40(
677; CHECK-NEXT:    ret i1 false
678;
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
685}
686
687define 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]]
691;
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
696}
697
698define 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]]
704;
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
709}
710
711define <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]]
717;
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
722}
723
724; PR8994
725
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.
728define 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]]
733;
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
737}
738
739define <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]]
744;
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
749}
750
751define 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
758;
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
765}
766
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.
769
770define 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
780;
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
789}
790
791define 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
801;
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
810}
811
812; To avoid potential backend problems, we don't do the same transform for other casts.
813
814define 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
824;
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
833}
834
835; PR8575
836
837define 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]]
842;
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
848}
849
850; PR9454
851
852define 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]]
858;
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
863}
864
865define 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]]
870;
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
875}
876
877define 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]]
881;
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
886}
887
888define i32 @test56(i16 %x) {
889; CHECK-LABEL: @test56(
890; CHECK-NEXT:    [[CONV:%.*]] = zext i16 [[X:%.*]] to i32
891; CHECK-NEXT:    ret i32 [[CONV]]
892;
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
897}
898
899define 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]]
905;
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
910}
911
912define i32 @test58(i16 %x) {
913; CHECK-LABEL: @test58(
914; CHECK-NEXT:    [[CONV:%.*]] = zext i16 [[X:%.*]] to i32
915; CHECK-NEXT:    ret i32 [[CONV]]
916;
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
921}
922
923define i32 @test59(i32 %x, i32 %y) {
924; CHECK-LABEL: @test59(
925; CHECK-NEXT:    [[AND:%.*]] = and i32 [[X:%.*]], [[Y:%.*]]
926; CHECK-NEXT:    ret i32 [[AND]]
927;
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
932}
933
934define 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]]
941;
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
947}
948
949@glbl = constant i32 10
950define i32 @test61(i32* %ptr) {
951; CHECK-LABEL: @test61(
952; CHECK-NEXT:    ret i32 10
953;
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
958}
959
960; PR14131
961define void @test64(i32 %p, i16 %b) noreturn {
962; CHECK-LABEL: @test64(
963; CHECK-NEXT:  entry:
964; CHECK-NEXT:    br i1 undef, 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]]
975;
976entry:
977  %p.addr.0.insert.mask = and i32 %p, -65536
978  %conv2 = and i32 %p, 65535
979  br i1 undef, label %lor.rhs, label %lor.end
980
981lor.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
985
986lor.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
995
996cond.false16:
997  br label %cond.end17
998
999cond.end17:
1000  br label %while.body
1001
1002while.body:
1003  br label %while.body
1004}
1005
1006@under_aligned = external global i32, align 1
1007
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.
1011define 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]]
1017;
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
1022}
1023
1024declare void @scribble_on_i32(i32*)
1025
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.
1029
1030define 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]]
1038;
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
1045}
1046
1047define 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]]
1059;
1060entry:
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
1068}
1069
1070; Test that we can speculate the loads around the select even when we can't
1071; fold the load completely away.
1072define 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]]
1078;
1079  %p = select i1 %flag, i32* %x, i32* %y
1080  %v = load i32, i32* %p
1081  ret i32 %v
1082}
1083
1084; The same as @test78 but we can't speculate the load because it can trap
1085; if under-aligned.
1086define 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]]
1094;
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
1102}
1103
1104; The same as @test78_deref but we can't speculate the load because
1105; one of the arguments is not sufficiently dereferenceable.
1106define 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]]
1111;
1112  %p = select i1 %flag, i32* %x, i32* %y
1113  %v = load i32, i32* %p
1114  ret i32 %v
1115}
1116
1117; Test that we can speculate the loads around the select even when we can't
1118; fold the load completely away.
1119define 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]]
1130;
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
1140}
1141
1142; Test that when we speculate the loads around the select they fold throug
1143; load->load folding and load->store folding.
1144define 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]]
1153;
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
1163}
1164
1165; Test that we can speculate the load around the select even though they use
1166; differently typed pointers.
1167define 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]]
1177;
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
1189}
1190
1191; Test that we can speculate the load around the select even though they use
1192; differently typed pointers.
1193define 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]]
1205;
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
1217}
1218
1219declare void @scribble_on_i64(i64*)
1220declare void @scribble_on_i128(i128*)
1221
1222; Test that we can speculate the load around the select even though they use
1223; differently typed pointers and requires inttoptr casts.
1224define 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]]
1236;
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
1248}
1249
1250; Test that we can speculate the load around the select even though they use
1251; differently typed pointers and requires a ptrtoint cast.
1252define 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]]
1264;
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
1276}
1277
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.
1281define 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]]
1296;
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
1309}
1310
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.
1314define 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]]
1329;
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
1342}
1343
1344define 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]]
1351;
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
1357}
1358
1359define 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]]
1366;
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
1372}
1373
1374define i32 @PR23757(i32 %x) {
1375; CHECK-LABEL: @PR23757(
1376; CHECK-NEXT:    [[ADD:%.*]] = add i32 [[X:%.*]], 1
1377; CHECK-NEXT:    ret i32 [[ADD]]
1378;
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
1383}
1384
1385define i32 @PR23757_swapped(i32 %x) {
1386; CHECK-LABEL: @PR23757_swapped(
1387; CHECK-NEXT:    ret i32 -2147483648
1388;
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
1393}
1394
1395define 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
1400;
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
1406}
1407
1408define 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]]
1414;
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
1420}
1421
1422; max(max(~a, -1), -1) --> ~min(a, 0)
1423
1424define 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]]
1429;
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
1436}
1437
1438; ub-safe negation pattern
1439define i32 @PR27817(i32 %x) {
1440; CHECK-LABEL: @PR27817(
1441; CHECK-NEXT:    [[SUB:%.*]] = sub i32 0, [[X:%.*]]
1442; CHECK-NEXT:    ret i32 [[SUB]]
1443;
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
1448}
1449
1450define i32 @PR27817_nsw(i32 %x) {
1451; CHECK-LABEL: @PR27817_nsw(
1452; CHECK-NEXT:    [[SUB:%.*]] = sub i32 0, [[X:%.*]]
1453; CHECK-NEXT:    ret i32 [[SUB]]
1454;
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
1459}
1460
1461define 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]]
1465;
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
1470}
1471
1472define <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]]
1476;
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
1481}
1482
1483define <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]]
1487;
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
1490}
1491
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
1495
1496define <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]]
1500;
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
1503}
1504
1505; Don't die or try if the condition mask is a constant expression or contains a constant expression.
1506
1507@g = global i32 0
1508
1509define <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]]
1513;
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
1516}
1517
1518define <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]]
1522;
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
1525}
1526
1527declare void @llvm.assume(i1)
1528
1529define 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:%.*]]
1533;
1534  call void @llvm.assume(i1 %cond)
1535  %sel = select i1 %cond, i8 %x, i8 %y
1536  ret i8 %sel
1537}
1538
1539; computeKnownBitsFromAssume() understands the 'not' of an assumed condition.
1540
1541define 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:%.*]]
1546;
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
1551}
1552
1553; Test case to make sure we don't consider an all ones float values for converting the select into a sext.
1554define <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]]
1560;
1561entry:
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
1565}
1566
1567; select(C, binop(select(C, X, Y), W), Z) -> select(C, binop(X, W), Z)
1568define 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]]
1573;
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
1578}
1579
1580; select(C, binop(select(C, X, Y), W), Z) -> select(C, Z, binop(Y, W))
1581define 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]]
1586;
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
1591}
1592
1593; select(C, Z, binop(W, select(C, X, Y))) -> select(C, binop(X, W), Z)
1594define 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]]
1599;
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
1604}
1605
1606; select(C, Z, binop(W, select(C, X, Y))) -> select(C, Z, binop(W, Y))
1607define 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]]
1612;
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
1617}
1618
1619define 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]]
1623;
1624  %r = select i1 %t, i32 4, i32 0
1625  ret i32 %r
1626}
1627
1628define <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]]
1632;
1633  %r = select <2 x i1> %t, <2 x i32> <i32 8, i32 8>, <2 x i32> zeroinitializer
1634  ret <2 x i32> %r
1635}
1636
1637define <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]]
1641;
1642  %r = select <2 x i1> %t, <2 x i32> <i32 4, i32 8>, <2 x i32> zeroinitializer
1643  ret <2 x i32> %r
1644}
1645
1646define 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]]
1650;
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
1655}
1656
1657define 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]]
1661;
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
1666}
1667
1668define <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]]
1673;
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
1678}
1679
1680define 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]]
1685;
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
1690}
1691
1692; TODO: Allow undefs when matching vectors.
1693
1694define <2 x float> @copysign4(<2 x float> %x) {
1695; CHECK-LABEL: @copysign4(
1696; CHECK-NEXT:    [[I:%.*]] = bitcast <2 x float> [[X:%.*]] to <2 x i32>
1697; CHECK-NEXT:    [[ISNEG:%.*]] = icmp slt <2 x i32> [[I]], zeroinitializer
1698; CHECK-NEXT:    [[R:%.*]] = select nnan arcp <2 x i1> [[ISNEG]], <2 x float> <float 4.200000e+01, float undef>, <2 x float> <float -4.200000e+01, float -4.200000e+01>
1699; CHECK-NEXT:    ret <2 x float> [[R]]
1700;
1701  %i = bitcast <2 x float> %x to <2 x i32>
1702  %isneg = icmp ugt <2 x i32> %i, <i32 2147483647, i32 2147483647>
1703  %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>
1704  ret <2 x float> %r
1705}
1706
1707declare void @use1(i1)
1708
1709; Negative test
1710
1711define float @copysign_extra_use(float %x) {
1712; CHECK-LABEL: @copysign_extra_use(
1713; CHECK-NEXT:    [[I:%.*]] = bitcast float [[X:%.*]] to i32
1714; CHECK-NEXT:    [[ISNEG:%.*]] = icmp slt i32 [[I]], 0
1715; CHECK-NEXT:    call void @use1(i1 [[ISNEG]])
1716; CHECK-NEXT:    [[R:%.*]] = select i1 [[ISNEG]], float -4.400000e+01, float 4.400000e+01
1717; CHECK-NEXT:    ret float [[R]]
1718;
1719  %i = bitcast float %x to i32
1720  %isneg = icmp ugt i32 %i, 2147483647
1721  call void @use1(i1 %isneg)
1722  %r = select i1 %isneg, float -44.0, float 44.0
1723  ret float %r
1724}
1725
1726; Negative test
1727
1728define float @copysign_type_mismatch(double %x) {
1729; CHECK-LABEL: @copysign_type_mismatch(
1730; CHECK-NEXT:    [[I:%.*]] = bitcast double [[X:%.*]] to i64
1731; CHECK-NEXT:    [[ISPOS:%.*]] = icmp sgt i64 [[I]], -1
1732; CHECK-NEXT:    [[R:%.*]] = select i1 [[ISPOS]], float 1.000000e+00, float -1.000000e+00
1733; CHECK-NEXT:    ret float [[R]]
1734;
1735  %i = bitcast double %x to i64
1736  %ispos = icmp sgt i64 %i, -1
1737  %r = select i1 %ispos, float 1.0, float -1.0
1738  ret float %r
1739}
1740
1741; Negative test
1742
1743define float @copysign_wrong_cmp(float %x) {
1744; CHECK-LABEL: @copysign_wrong_cmp(
1745; CHECK-NEXT:    [[I:%.*]] = bitcast float [[X:%.*]] to i32
1746; CHECK-NEXT:    [[ISPOS:%.*]] = icmp sgt i32 [[I]], 0
1747; CHECK-NEXT:    [[R:%.*]] = select i1 [[ISPOS]], float 1.000000e+00, float -1.000000e+00
1748; CHECK-NEXT:    ret float [[R]]
1749;
1750  %i = bitcast float %x to i32
1751  %ispos = icmp sgt i32 %i, 0
1752  %r = select i1 %ispos, float 1.0, float -1.0
1753  ret float %r
1754}
1755
1756; Negative test
1757
1758define float @copysign_wrong_const(float %x) {
1759; CHECK-LABEL: @copysign_wrong_const(
1760; CHECK-NEXT:    [[I:%.*]] = bitcast float [[X:%.*]] to i32
1761; CHECK-NEXT:    [[ISPOS:%.*]] = icmp sgt i32 [[I]], -1
1762; CHECK-NEXT:    [[R:%.*]] = select i1 [[ISPOS]], float 2.000000e+00, float -1.000000e+00
1763; CHECK-NEXT:    ret float [[R]]
1764;
1765  %i = bitcast float %x to i32
1766  %ispos = icmp sgt i32 %i, -1
1767  %r = select i1 %ispos, float 2.0, float -1.0
1768  ret float %r
1769}
1770
1771; TODO: we can replace select with a Phi.
1772define i32 @select_dominating_cond(i1 %cond, i32 %x, i32 %y) {
1773; CHECK-LABEL: @select_dominating_cond(
1774; CHECK-NEXT:  entry:
1775; CHECK-NEXT:    br i1 [[COND:%.*]], label [[IF_TRUE:%.*]], label [[IF_FALSE:%.*]]
1776; CHECK:       if.true:
1777; CHECK-NEXT:    br label [[MERGE:%.*]]
1778; CHECK:       if.false:
1779; CHECK-NEXT:    br label [[MERGE]]
1780; CHECK:       merge:
1781; CHECK-NEXT:    [[S:%.*]] = phi i32 [ [[Y:%.*]], [[IF_FALSE]] ], [ [[X:%.*]], [[IF_TRUE]] ]
1782; CHECK-NEXT:    ret i32 [[S]]
1783;
1784entry:
1785  br i1 %cond, label %if.true, label %if.false
1786
1787if.true:
1788  br label %merge
1789
1790if.false:
1791  br label %merge
1792
1793merge:
1794  %s = select i1 %cond, i32 %x, i32 %y
1795  ret i32 %s
1796}
1797
1798define i32 @select_dominating_inverted(i1 %cond, i32 %x, i32 %y) {
1799; CHECK-LABEL: @select_dominating_inverted(
1800; CHECK-NEXT:  entry:
1801; CHECK-NEXT:    br i1 [[COND:%.*]], label [[IF_FALSE:%.*]], label [[IF_TRUE:%.*]]
1802; CHECK:       if.true:
1803; CHECK-NEXT:    br label [[MERGE:%.*]]
1804; CHECK:       if.false:
1805; CHECK-NEXT:    br label [[MERGE]]
1806; CHECK:       merge:
1807; CHECK-NEXT:    [[S:%.*]] = phi i32 [ [[X:%.*]], [[IF_FALSE]] ], [ [[Y:%.*]], [[IF_TRUE]] ]
1808; CHECK-NEXT:    ret i32 [[S]]
1809;
1810entry:
1811  %inverted = xor i1 %cond, 1
1812  br i1 %inverted, label %if.true, label %if.false
1813
1814if.true:
1815  br label %merge
1816
1817if.false:
1818  br label %merge
1819
1820merge:
1821  %s = select i1 %cond, i32 %x, i32 %y
1822  ret i32 %s
1823}
1824
1825; More complex CFG: the block with select has multiple predecessors.
1826define i32 @select_dominating_cond_multiple_preds(i1 %cond, i1 %cond2, i1 %cond3, i32 %x, i32 %y) {
1827; CHECK-LABEL: @select_dominating_cond_multiple_preds(
1828; CHECK-NEXT:  entry:
1829; CHECK-NEXT:    br i1 [[COND:%.*]], label [[IF_TRUE:%.*]], label [[IF_FALSE:%.*]]
1830; CHECK:       if.true:
1831; CHECK-NEXT:    br i1 [[COND2:%.*]], label [[IF_TRUE_1:%.*]], label [[IF_TRUE_2:%.*]]
1832; CHECK:       if.true.1:
1833; CHECK-NEXT:    br label [[MERGE:%.*]]
1834; CHECK:       if.true.2:
1835; CHECK-NEXT:    br label [[MERGE]]
1836; CHECK:       if.false:
1837; CHECK-NEXT:    br i1 [[COND3:%.*]], label [[IF_FALSE_1:%.*]], label [[EXIT:%.*]]
1838; CHECK:       if.false.1:
1839; CHECK-NEXT:    br label [[MERGE]]
1840; CHECK:       merge:
1841; CHECK-NEXT:    [[S:%.*]] = phi i32 [ [[Y:%.*]], [[IF_FALSE_1]] ], [ [[X:%.*]], [[IF_TRUE_2]] ], [ [[X]], [[IF_TRUE_1]] ]
1842; CHECK-NEXT:    ret i32 [[S]]
1843; CHECK:       exit:
1844; CHECK-NEXT:    ret i32 0
1845;
1846entry:
1847  br i1 %cond, label %if.true, label %if.false
1848
1849if.true:
1850  br i1 %cond2, label %if.true.1, label %if.true.2
1851
1852if.true.1:
1853  br label %merge
1854
1855if.true.2:
1856  br label %merge
1857
1858if.false:
1859  br i1 %cond3, label %if.false.1, label %exit
1860
1861if.false.1:
1862  br label %merge
1863
1864merge:
1865  %s = select i1 %cond, i32 %x, i32 %y
1866  ret i32 %s
1867
1868exit:
1869  ret i32 0
1870}
1871
1872; More complex CFG for inverted case: the block with select has multiple predecessors.
1873define i32 @select_dominating_cond_inverted_multiple_preds(i1 %cond, i1 %cond2, i1 %cond3, i32 %x, i32 %y) {
1874; CHECK-LABEL: @select_dominating_cond_inverted_multiple_preds(
1875; CHECK-NEXT:  entry:
1876; CHECK-NEXT:    br i1 [[COND:%.*]], label [[IF_FALSE:%.*]], label [[IF_TRUE:%.*]]
1877; CHECK:       if.true:
1878; CHECK-NEXT:    br i1 [[COND2:%.*]], label [[IF_TRUE_1:%.*]], label [[IF_TRUE_2:%.*]]
1879; CHECK:       if.true.1:
1880; CHECK-NEXT:    br label [[MERGE:%.*]]
1881; CHECK:       if.true.2:
1882; CHECK-NEXT:    br label [[MERGE]]
1883; CHECK:       if.false:
1884; CHECK-NEXT:    br i1 [[COND3:%.*]], label [[IF_FALSE_1:%.*]], label [[EXIT:%.*]]
1885; CHECK:       if.false.1:
1886; CHECK-NEXT:    br label [[MERGE]]
1887; CHECK:       merge:
1888; CHECK-NEXT:    [[S:%.*]] = phi i32 [ [[X:%.*]], [[IF_FALSE_1]] ], [ [[Y:%.*]], [[IF_TRUE_2]] ], [ [[Y]], [[IF_TRUE_1]] ]
1889; CHECK-NEXT:    ret i32 [[S]]
1890; CHECK:       exit:
1891; CHECK-NEXT:    ret i32 0
1892;
1893entry:
1894  %inverted = xor i1 %cond, 1
1895  br i1 %inverted, label %if.true, label %if.false
1896
1897if.true:
1898  br i1 %cond2, label %if.true.1, label %if.true.2
1899
1900if.true.1:
1901  br label %merge
1902
1903if.true.2:
1904  br label %merge
1905
1906if.false:
1907  br i1 %cond3, label %if.false.1, label %exit
1908
1909if.false.1:
1910  br label %merge
1911
1912merge:
1913  %s = select i1 %cond, i32 %x, i32 %y
1914  ret i32 %s
1915
1916exit:
1917  ret i32 0
1918}
1919
1920; More complex CFG for inverted case: the block with select has multiple predecessors that can duplicate.
1921define i32 @select_dominating_cond_inverted_multiple_duplicating_preds(i1 %cond, i32 %cond2, i1 %cond3, i32 %x, i32 %y) {
1922; CHECK-LABEL: @select_dominating_cond_inverted_multiple_duplicating_preds(
1923; CHECK-NEXT:  entry:
1924; CHECK-NEXT:    br i1 [[COND:%.*]], label [[IF_FALSE:%.*]], label [[IF_TRUE:%.*]]
1925; CHECK:       if.true:
1926; CHECK-NEXT:    switch i32 [[COND2:%.*]], label [[SWITCH_CASE_1:%.*]] [
1927; CHECK-NEXT:    i32 1, label [[MERGE:%.*]]
1928; CHECK-NEXT:    i32 2, label [[MERGE]]
1929; CHECK-NEXT:    i32 3, label [[MERGE]]
1930; CHECK-NEXT:    ]
1931; CHECK:       switch.case.1:
1932; CHECK-NEXT:    br label [[MERGE]]
1933; CHECK:       if.false:
1934; CHECK-NEXT:    br i1 [[COND3:%.*]], label [[IF_FALSE_1:%.*]], label [[EXIT:%.*]]
1935; CHECK:       if.false.1:
1936; CHECK-NEXT:    br label [[MERGE]]
1937; CHECK:       merge:
1938; CHECK-NEXT:    [[S:%.*]] = phi i32 [ [[X:%.*]], [[IF_FALSE_1]] ], [ [[Y:%.*]], [[SWITCH_CASE_1]] ], [ [[Y]], [[IF_TRUE]] ], [ [[Y]], [[IF_TRUE]] ], [ [[Y]], [[IF_TRUE]] ]
1939; CHECK-NEXT:    ret i32 [[S]]
1940; CHECK:       exit:
1941; CHECK-NEXT:    ret i32 0
1942;
1943entry:
1944  %inverted = xor i1 %cond, 1
1945  br i1 %inverted, label %if.true, label %if.false
1946
1947if.true:
1948  switch i32 %cond2, label %switch.case.1 [
1949  i32 1, label %merge
1950  i32 2, label %merge
1951  i32 3, label %merge
1952  ]
1953
1954switch.case.1:
1955  br label %merge
1956
1957if.false:
1958  br i1 %cond3, label %if.false.1, label %exit
1959
1960if.false.1:
1961  br label %merge
1962
1963merge:
1964  %s = select i1 %cond, i32 %x, i32 %y
1965  ret i32 %s
1966
1967exit:
1968  ret i32 0
1969}
1970
1971; Negative test: currently we take condition from IDom, but might be willing to expand it in the future.
1972define i32 @select_not_imm_dominating_cond_neg(i1 %cond, i32 %x, i32 %y) {
1973; CHECK-LABEL: @select_not_imm_dominating_cond_neg(
1974; CHECK-NEXT:  entry:
1975; CHECK-NEXT:    br i1 [[COND:%.*]], label [[IF_TRUE:%.*]], label [[IF_FALSE:%.*]]
1976; CHECK:       if.true:
1977; CHECK-NEXT:    br label [[MERGE:%.*]]
1978; CHECK:       if.false:
1979; CHECK-NEXT:    br label [[MERGE]]
1980; CHECK:       merge:
1981; CHECK-NEXT:    br label [[EXIT:%.*]]
1982; CHECK:       exit:
1983; CHECK-NEXT:    [[S:%.*]] = select i1 [[COND]], i32 [[X:%.*]], i32 [[Y:%.*]]
1984; CHECK-NEXT:    ret i32 [[S]]
1985;
1986entry:
1987  br i1 %cond, label %if.true, label %if.false
1988
1989if.true:
1990  br label %merge
1991
1992if.false:
1993  br label %merge
1994
1995merge:
1996  br label %exit
1997
1998exit:
1999  %s = select i1 %cond, i32 %x, i32 %y
2000  ret i32 %s
2001}
2002
2003; Shows how we can leverage dominance to eliminate duplicating selects.
2004define i32 @select_dominance_chain(i1 %cond, i32 %x, i32 %y) {
2005; CHECK-LABEL: @select_dominance_chain(
2006; CHECK-NEXT:  entry:
2007; CHECK-NEXT:    br i1 [[COND:%.*]], label [[IF_TRUE_1:%.*]], label [[IF_FALSE_1:%.*]]
2008; CHECK:       if.true.1:
2009; CHECK-NEXT:    br label [[MERGE_1:%.*]]
2010; CHECK:       if.false.1:
2011; CHECK-NEXT:    br label [[MERGE_1]]
2012; CHECK:       merge.1:
2013; CHECK-NEXT:    br i1 [[COND]], label [[IF_TRUE_2:%.*]], label [[IF_FALSE_2:%.*]]
2014; CHECK:       if.true.2:
2015; CHECK-NEXT:    br label [[MERGE_2:%.*]]
2016; CHECK:       if.false.2:
2017; CHECK-NEXT:    br label [[MERGE_2]]
2018; CHECK:       merge.2:
2019; CHECK-NEXT:    br i1 [[COND]], label [[IF_TRUE_3:%.*]], label [[IF_FALSE_3:%.*]]
2020; CHECK:       if.true.3:
2021; CHECK-NEXT:    br label [[MERGE_3:%.*]]
2022; CHECK:       if.false.3:
2023; CHECK-NEXT:    br label [[MERGE_3]]
2024; CHECK:       merge.3:
2025; CHECK-NEXT:    [[S_1:%.*]] = phi i32 [ [[Y:%.*]], [[IF_FALSE_3]] ], [ [[X:%.*]], [[IF_TRUE_3]] ]
2026; CHECK-NEXT:    [[SUM_2:%.*]] = mul i32 [[S_1]], 3
2027; CHECK-NEXT:    ret i32 [[SUM_2]]
2028;
2029entry:
2030  br i1 %cond, label %if.true.1, label %if.false.1
2031
2032if.true.1:
2033  br label %merge.1
2034
2035if.false.1:
2036  br label %merge.1
2037
2038merge.1:
2039  %s.1 = select i1 %cond, i32 %x, i32 %y
2040  br i1 %cond, label %if.true.2, label %if.false.2
2041
2042if.true.2:
2043  br label %merge.2
2044
2045if.false.2:
2046  br label %merge.2
2047
2048merge.2:
2049  %s.2 = select i1 %cond, i32 %x, i32 %y
2050  br i1 %cond, label %if.true.3, label %if.false.3
2051
2052if.true.3:
2053  br label %merge.3
2054
2055if.false.3:
2056  br label %merge.3
2057
2058merge.3:
2059  %s.3 = select i1 %cond, i32 %x, i32 %y
2060  %sum.1 = add i32 %s.1, %s.2
2061  %sum.2 = add i32 %sum.1, %s.3
2062  ret i32 %sum.2
2063}
2064
2065; TODO: We can replace select with a Phi and then sink a and b to respective
2066; branches.
2067define i32 @select_dominating_cond_and_sink(i1 %cond, i32 %x, i32 %y) {
2068; CHECK-LABEL: @select_dominating_cond_and_sink(
2069; CHECK-NEXT:  entry:
2070; CHECK-NEXT:    br i1 [[COND:%.*]], label [[IF_TRUE:%.*]], label [[IF_FALSE:%.*]]
2071; CHECK:       if.true:
2072; CHECK-NEXT:    br label [[MERGE:%.*]]
2073; CHECK:       if.false:
2074; CHECK-NEXT:    br label [[MERGE]]
2075; CHECK:       merge:
2076; CHECK-NEXT:    [[B:%.*]] = mul i32 [[X:%.*]], [[Y:%.*]]
2077; CHECK-NEXT:    [[A:%.*]] = add i32 [[X]], [[Y]]
2078; CHECK-NEXT:    [[S:%.*]] = select i1 [[COND]], i32 [[A]], i32 [[B]]
2079; CHECK-NEXT:    ret i32 [[S]]
2080;
2081entry:
2082  %a = add i32 %x, %y
2083  %b = mul i32 %x, %y
2084  br i1 %cond, label %if.true, label %if.false
2085
2086if.true:
2087  br label %merge
2088
2089if.false:
2090  br label %merge
2091
2092merge:
2093  %s = select i1 %cond, i32 %a, i32 %b
2094  ret i32 %s
2095}
2096
2097define i32 @select_dominating_cond_same_labels(i1 %cond) {
2098; CHECK-LABEL: @select_dominating_cond_same_labels(
2099; CHECK-NEXT:  entry:
2100; CHECK-NEXT:    br i1 false, label [[EXIT:%.*]], label [[EXIT]]
2101; CHECK:       exit:
2102; CHECK-NEXT:    [[RESULT:%.*]] = select i1 [[COND:%.*]], i32 123, i32 456
2103; CHECK-NEXT:    ret i32 [[RESULT]]
2104;
2105entry:
2106  %result = select i1 %cond, i32 123, i32 456
2107  br i1 %cond, label %exit, label %exit
2108exit:
2109  ret i32 %result
2110}
2111
2112define i32 @select_phi_same_condition(i1 %cond, i32 %x, i32 %y, i32 %z) {
2113; CHECK-LABEL: @select_phi_same_condition(
2114; CHECK-NEXT:  entry:
2115; CHECK-NEXT:    br i1 [[COND:%.*]], label [[IF_TRUE:%.*]], label [[IF_FALSE:%.*]]
2116; CHECK:       if.true:
2117; CHECK-NEXT:    br label [[MERGE:%.*]]
2118; CHECK:       if.false:
2119; CHECK-NEXT:    br label [[MERGE]]
2120; CHECK:       merge:
2121; CHECK-NEXT:    [[S:%.*]] = phi i32 [ [[Z:%.*]], [[IF_FALSE]] ], [ [[X:%.*]], [[IF_TRUE]] ]
2122; CHECK-NEXT:    ret i32 [[S]]
2123;
2124entry:
2125  br i1 %cond, label %if.true, label %if.false
2126
2127if.true:
2128  br label %merge
2129
2130if.false:
2131  br label %merge
2132
2133merge:
2134  %phi = phi i32 [0, %if.true], [%z, %if.false]
2135  %s = select i1 %cond, i32 %x, i32 %phi
2136  ret i32 %s
2137}
2138
2139
2140; TODO: Replace with phi[a, c] and sink them to respective branches.
2141define i32 @select_phi_same_condition_sink(i1 %cond, i32 %x, i32 %y, i32 %z) {
2142; CHECK-LABEL: @select_phi_same_condition_sink(
2143; CHECK-NEXT:  entry:
2144; CHECK-NEXT:    br i1 [[COND:%.*]], label [[IF_TRUE:%.*]], label [[IF_FALSE:%.*]]
2145; CHECK:       if.true:
2146; CHECK-NEXT:    br label [[MERGE:%.*]]
2147; CHECK:       if.false:
2148; CHECK-NEXT:    [[B:%.*]] = mul i32 [[X:%.*]], [[Z:%.*]]
2149; CHECK-NEXT:    br label [[MERGE]]
2150; CHECK:       merge:
2151; CHECK-NEXT:    [[PHI:%.*]] = phi i32 [ 0, [[IF_TRUE]] ], [ [[B]], [[IF_FALSE]] ]
2152; CHECK-NEXT:    [[A:%.*]] = add i32 [[X]], [[Y:%.*]]
2153; CHECK-NEXT:    [[S:%.*]] = select i1 [[COND]], i32 [[A]], i32 [[PHI]]
2154; CHECK-NEXT:    ret i32 [[S]]
2155;
2156entry:
2157  %a = add i32 %x, %y
2158  %b = mul i32 %x, %z
2159  br i1 %cond, label %if.true, label %if.false
2160
2161if.true:
2162  br label %merge
2163
2164if.false:
2165  br label %merge
2166
2167merge:
2168  %phi = phi i32 [0, %if.true], [%b, %if.false]
2169  %s = select i1 %cond, i32 %a, i32 %phi
2170  ret i32 %s
2171}
2172
2173declare i32 @__gxx_personality_v0(...)
2174declare i1 @foo()
2175
2176define i32 @test_invoke_neg(i32 %x, i32 %y) nounwind uwtable ssp personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
2177; CHECK-LABEL: @test_invoke_neg(
2178; CHECK-NEXT:  entry:
2179; CHECK-NEXT:    [[COND:%.*]] = invoke i1 @foo()
2180; CHECK-NEXT:    to label [[INVOKE_CONT:%.*]] unwind label [[LPAD:%.*]]
2181; CHECK:       invoke.cont:
2182; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[COND]], i32 [[X:%.*]], i32 [[Y:%.*]]
2183; CHECK-NEXT:    ret i32 [[SEL]]
2184; CHECK:       lpad:
2185; CHECK-NEXT:    [[LP:%.*]] = landingpad { i1, i32 }
2186; CHECK-NEXT:    filter [0 x i1] zeroinitializer
2187; CHECK-NEXT:    unreachable
2188;
2189entry:
2190  %cond = invoke i1 @foo()
2191  to label %invoke.cont unwind label %lpad
2192
2193invoke.cont:
2194  %sel = select i1 %cond, i32 %x, i32 %y
2195  ret i32 %sel
2196
2197lpad:
2198  %lp = landingpad { i1, i32 }
2199  filter [0 x i1] zeroinitializer
2200  unreachable
2201}
2202
2203declare i32 @bar()
2204
2205define i32 @test_invoke_2_neg(i1 %cond, i32 %x, i32 %y) nounwind uwtable ssp personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
2206; CHECK-LABEL: @test_invoke_2_neg(
2207; CHECK-NEXT:  entry:
2208; CHECK-NEXT:    br i1 [[COND:%.*]], label [[IF_TRUE:%.*]], label [[IF_FALSE:%.*]]
2209; CHECK:       if.true:
2210; CHECK-NEXT:    br label [[MERGE:%.*]]
2211; CHECK:       if.false:
2212; CHECK-NEXT:    [[RESULT:%.*]] = invoke i32 @bar()
2213; CHECK-NEXT:    to label [[MERGE]] unwind label [[LPAD:%.*]]
2214; CHECK:       merge:
2215; CHECK-NEXT:    [[PHI:%.*]] = phi i32 [ 0, [[IF_TRUE]] ], [ [[RESULT]], [[IF_FALSE]] ]
2216; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[COND]], i32 1, i32 [[PHI]]
2217; CHECK-NEXT:    ret i32 [[SEL]]
2218; CHECK:       lpad:
2219; CHECK-NEXT:    [[LP:%.*]] = landingpad { i1, i32 }
2220; CHECK-NEXT:    filter [0 x i1] zeroinitializer
2221; CHECK-NEXT:    unreachable
2222;
2223entry:
2224  br i1 %cond, label %if.true, label %if.false
2225
2226if.true:
2227  br label %merge
2228
2229if.false:
2230  %result = invoke i32 @bar()
2231  to label %merge unwind label %lpad
2232
2233merge:
2234  %phi = phi i32 [ 0, %if.true ], [ %result, %if.false ]
2235  %sel = select i1 %cond, i32 1, i32 %phi
2236  ret i32 %sel
2237
2238lpad:
2239  %lp = landingpad { i1, i32 }
2240  filter [0 x i1] zeroinitializer
2241  unreachable
2242}
2243
2244define i32 @select_phi_same_condition_switch(i1 %cond, i32 %x, i32 %y) {
2245; CHECK-LABEL: @select_phi_same_condition_switch(
2246; CHECK-NEXT:  entry:
2247; CHECK-NEXT:    br i1 [[COND:%.*]], label [[IF_TRUE:%.*]], label [[IF_FALSE:%.*]]
2248; CHECK:       if.true:
2249; CHECK-NEXT:    switch i32 [[X:%.*]], label [[EXIT:%.*]] [
2250; CHECK-NEXT:    i32 1, label [[MERGE:%.*]]
2251; CHECK-NEXT:    i32 2, label [[MERGE]]
2252; CHECK-NEXT:    ]
2253; CHECK:       exit:
2254; CHECK-NEXT:    ret i32 0
2255; CHECK:       if.false:
2256; CHECK-NEXT:    br label [[MERGE]]
2257; CHECK:       merge:
2258; CHECK-NEXT:    [[S:%.*]] = phi i32 [ [[Y:%.*]], [[IF_FALSE]] ], [ [[X]], [[IF_TRUE]] ], [ [[X]], [[IF_TRUE]] ]
2259; CHECK-NEXT:    ret i32 [[S]]
2260;
2261entry:
2262  br i1 %cond, label %if.true, label %if.false
2263
2264if.true:
2265  switch i32 %x, label %exit [
2266  i32 1, label %merge
2267  i32 2, label %merge
2268  ]
2269
2270exit:
2271  ret i32 0
2272
2273if.false:
2274  br label %merge
2275
2276merge:
2277  %phi = phi i32 [0, %if.true], [0, %if.true], [%y, %if.false]
2278  %s = select i1 %cond, i32 %x, i32 %phi
2279  ret i32 %s
2280}
2281
2282define i32 @transit_different_values_through_phi(i1 %cond, i1 %cond2) {
2283; CHECK-LABEL: @transit_different_values_through_phi(
2284; CHECK-NEXT:  entry:
2285; CHECK-NEXT:    br i1 [[COND:%.*]], label [[IF_TRUE:%.*]], label [[IF_FALSE:%.*]]
2286; CHECK:       if.true:
2287; CHECK-NEXT:    br i1 [[COND2:%.*]], label [[IF_TRUE_1:%.*]], label [[IF_TRUE_2:%.*]]
2288; CHECK:       if.true.1:
2289; CHECK-NEXT:    br label [[MERGE:%.*]]
2290; CHECK:       if.true.2:
2291; CHECK-NEXT:    br label [[MERGE]]
2292; CHECK:       if.false:
2293; CHECK-NEXT:    br label [[MERGE]]
2294; CHECK:       merge:
2295; CHECK-NEXT:    [[S:%.*]] = phi i32 [ 3, [[IF_FALSE]] ], [ 2, [[IF_TRUE_2]] ], [ 1, [[IF_TRUE_1]] ]
2296; CHECK-NEXT:    ret i32 [[S]]
2297; CHECK:       exit:
2298; CHECK-NEXT:    ret i32 0
2299;
2300entry:
2301  br i1 %cond, label %if.true, label %if.false
2302
2303if.true:
2304  br i1 %cond2, label %if.true.1, label %if.true.2
2305
2306if.true.1:
2307  br label %merge
2308
2309if.true.2:
2310  br label %merge
2311
2312if.false:
2313  br label %merge
2314
2315merge:
2316  %p = phi i32 [ 1, %if.true.1 ], [ 2, %if.true.2 ], [ 4, %if.false ]
2317  %s = select i1 %cond, i32 %p, i32 3
2318  ret i32 %s
2319
2320exit:
2321  ret i32 0
2322}
2323
2324define i32 @select_phi_degenerate(i1 %cond, i1 %cond2) {
2325; CHECK-LABEL: @select_phi_degenerate(
2326; CHECK-NEXT:  entry:
2327; CHECK-NEXT:    br i1 [[COND:%.*]], label [[LOOP:%.*]], label [[EXIT:%.*]]
2328; CHECK:       loop:
2329; CHECK-NEXT:    [[SELECT:%.*]] = phi i32 [ [[IV_INC:%.*]], [[LOOP]] ], [ 0, [[ENTRY:%.*]] ]
2330; CHECK-NEXT:    [[IV_INC]] = add i32 [[SELECT]], 1
2331; CHECK-NEXT:    br i1 [[COND2:%.*]], label [[LOOP]], label [[EXIT2:%.*]]
2332; CHECK:       exit:
2333; CHECK-NEXT:    ret i32 0
2334; CHECK:       exit2:
2335; CHECK-NEXT:    ret i32 [[IV_INC]]
2336;
2337entry:
2338  br i1 %cond, label %loop, label %exit
2339
2340loop:
2341  %iv = phi i32 [ 0, %entry ], [ %iv.inc, %loop ]
2342  %select = select i1 %cond, i32 %iv, i32 -1
2343  %iv.inc = add i32 %select, 1
2344  br i1 %cond2, label %loop, label %exit2
2345
2346exit:
2347  ret i32 0
2348
2349exit2:
2350  ret i32 %iv.inc
2351}
2352
2353define i32 @test_select_into_phi_not_idom(i1 %cond, i32 %A, i32 %B)  {
2354; CHECK-LABEL: @test_select_into_phi_not_idom(
2355; CHECK-NEXT:  entry:
2356; CHECK-NEXT:    br i1 [[COND:%.*]], label [[IF_TRUE:%.*]], label [[IF_FALSE:%.*]]
2357; CHECK:       if.true:
2358; CHECK-NEXT:    br label [[MERGE:%.*]]
2359; CHECK:       if.false:
2360; CHECK-NEXT:    br label [[MERGE]]
2361; CHECK:       merge:
2362; CHECK-NEXT:    br label [[EXIT:%.*]]
2363; CHECK:       exit:
2364; CHECK-NEXT:    ret i32 [[A:%.*]]
2365;
2366entry:
2367  br i1 %cond, label %if.true, label %if.false
2368
2369if.true:
2370  br label %merge
2371
2372if.false:
2373  br label %merge
2374
2375merge:
2376  %phi = phi i32 [%A, %if.true], [%B, %if.false]
2377  br label %exit
2378
2379exit:
2380  %sel = select i1 %cond, i32 %phi, i32 %A
2381  ret i32 %sel
2382}
2383
2384define i32 @test_select_into_phi_not_idom_2(i1 %cond, i32 %A, i32 %B)  {
2385; CHECK-LABEL: @test_select_into_phi_not_idom_2(
2386; CHECK-NEXT:  entry:
2387; CHECK-NEXT:    br i1 [[COND:%.*]], label [[IF_TRUE:%.*]], label [[IF_FALSE:%.*]]
2388; CHECK:       if.true:
2389; CHECK-NEXT:    br label [[MERGE:%.*]]
2390; CHECK:       if.false:
2391; CHECK-NEXT:    br label [[MERGE]]
2392; CHECK:       merge:
2393; CHECK-NEXT:    br label [[EXIT:%.*]]
2394; CHECK:       exit:
2395; CHECK-NEXT:    ret i32 [[B:%.*]]
2396;
2397entry:
2398  br i1 %cond, label %if.true, label %if.false
2399
2400if.true:
2401  br label %merge
2402
2403if.false:
2404  br label %merge
2405
2406merge:
2407  %phi = phi i32 [%A, %if.true], [%B, %if.false]
2408  br label %exit
2409
2410exit:
2411  %sel = select i1 %cond, i32 %B, i32 %phi
2412  ret i32 %sel
2413}
2414
2415define i32 @test_select_into_phi_not_idom_inverted(i1 %cond, i32 %A, i32 %B)  {
2416; CHECK-LABEL: @test_select_into_phi_not_idom_inverted(
2417; CHECK-NEXT:  entry:
2418; CHECK-NEXT:    br i1 [[COND:%.*]], label [[IF_FALSE:%.*]], label [[IF_TRUE:%.*]]
2419; CHECK:       if.true:
2420; CHECK-NEXT:    br label [[MERGE:%.*]]
2421; CHECK:       if.false:
2422; CHECK-NEXT:    br label [[MERGE]]
2423; CHECK:       merge:
2424; CHECK-NEXT:    [[SEL:%.*]] = phi i32 [ [[B:%.*]], [[IF_FALSE]] ], [ [[A:%.*]], [[IF_TRUE]] ]
2425; CHECK-NEXT:    br label [[EXIT:%.*]]
2426; CHECK:       exit:
2427; CHECK-NEXT:    ret i32 [[SEL]]
2428;
2429entry:
2430  %inverted = xor i1 %cond, 1
2431  br i1 %inverted, label %if.true, label %if.false
2432
2433if.true:
2434  br label %merge
2435
2436if.false:
2437  br label %merge
2438
2439merge:
2440  %phi = phi i32 [%A, %if.true], [%B, %if.false]
2441  br label %exit
2442
2443exit:
2444  %sel = select i1 %cond, i32 %phi, i32 %A
2445  ret i32 %sel
2446}
2447
2448define i32 @test_select_into_phi_not_idom_inverted_2(i1 %cond, i32 %A, i32 %B)  {
2449; CHECK-LABEL: @test_select_into_phi_not_idom_inverted_2(
2450; CHECK-NEXT:  entry:
2451; CHECK-NEXT:    br i1 [[COND:%.*]], label [[IF_FALSE:%.*]], label [[IF_TRUE:%.*]]
2452; CHECK:       if.true:
2453; CHECK-NEXT:    br label [[MERGE:%.*]]
2454; CHECK:       if.false:
2455; CHECK-NEXT:    br label [[MERGE]]
2456; CHECK:       merge:
2457; CHECK-NEXT:    [[SEL:%.*]] = phi i32 [ [[B:%.*]], [[IF_FALSE]] ], [ [[A:%.*]], [[IF_TRUE]] ]
2458; CHECK-NEXT:    br label [[EXIT:%.*]]
2459; CHECK:       exit:
2460; CHECK-NEXT:    ret i32 [[SEL]]
2461;
2462entry:
2463  %inverted = xor i1 %cond, 1
2464  br i1 %inverted, label %if.true, label %if.false
2465
2466if.true:
2467  br label %merge
2468
2469if.false:
2470  br label %merge
2471
2472merge:
2473  %phi = phi i32 [%A, %if.true], [%B, %if.false]
2474  br label %exit
2475
2476exit:
2477  %sel = select i1 %cond, i32 %B, i32 %phi
2478  ret i32 %sel
2479}
2480
2481define i32 @test_select_into_phi_not_idom_no_dom_input_1(i1 %cond, i32 %A, i32 %B, i32 *%p)  {
2482; CHECK-LABEL: @test_select_into_phi_not_idom_no_dom_input_1(
2483; CHECK-NEXT:  entry:
2484; CHECK-NEXT:    br i1 [[COND:%.*]], label [[IF_TRUE:%.*]], label [[IF_FALSE:%.*]]
2485; CHECK:       if.true:
2486; CHECK-NEXT:    [[C:%.*]] = load i32, i32* [[P:%.*]], align 4
2487; CHECK-NEXT:    br label [[MERGE:%.*]]
2488; CHECK:       if.false:
2489; CHECK-NEXT:    br label [[MERGE]]
2490; CHECK:       merge:
2491; CHECK-NEXT:    [[SEL:%.*]] = phi i32 [ [[A:%.*]], [[IF_FALSE]] ], [ [[C]], [[IF_TRUE]] ]
2492; CHECK-NEXT:    br label [[EXIT:%.*]]
2493; CHECK:       exit:
2494; CHECK-NEXT:    ret i32 [[SEL]]
2495;
2496entry:
2497  br i1 %cond, label %if.true, label %if.false
2498
2499if.true:
2500  %C = load i32, i32* %p
2501  br label %merge
2502
2503if.false:
2504  br label %merge
2505
2506merge:
2507  %phi = phi i32 [%C, %if.true], [%B, %if.false]
2508  br label %exit
2509
2510exit:
2511  %sel = select i1 %cond, i32 %phi, i32 %A
2512  ret i32 %sel
2513}
2514
2515define i32 @test_select_into_phi_not_idom_no_dom_input_2(i1 %cond, i32 %A, i32 %B, i32 *%p)  {
2516; CHECK-LABEL: @test_select_into_phi_not_idom_no_dom_input_2(
2517; CHECK-NEXT:  entry:
2518; CHECK-NEXT:    br i1 [[COND:%.*]], label [[IF_TRUE:%.*]], label [[IF_FALSE:%.*]]
2519; CHECK:       if.true:
2520; CHECK-NEXT:    br label [[MERGE:%.*]]
2521; CHECK:       if.false:
2522; CHECK-NEXT:    [[C:%.*]] = load i32, i32* [[P:%.*]], align 4
2523; CHECK-NEXT:    br label [[MERGE]]
2524; CHECK:       merge:
2525; CHECK-NEXT:    [[SEL:%.*]] = phi i32 [ [[C]], [[IF_FALSE]] ], [ [[B:%.*]], [[IF_TRUE]] ]
2526; CHECK-NEXT:    br label [[EXIT:%.*]]
2527; CHECK:       exit:
2528; CHECK-NEXT:    ret i32 [[SEL]]
2529;
2530entry:
2531  br i1 %cond, label %if.true, label %if.false
2532
2533if.true:
2534  br label %merge
2535
2536if.false:
2537  %C = load i32, i32* %p
2538  br label %merge
2539
2540merge:
2541  %phi = phi i32 [%A, %if.true], [%C, %if.false]
2542  br label %exit
2543
2544exit:
2545  %sel = select i1 %cond, i32 %B, i32 %phi
2546  ret i32 %sel
2547}
2548
2549; Negative tests to ensure we don't remove selects with undef true/false values.
2550; See https://bugs.llvm.org/show_bug.cgi?id=31633
2551; https://lists.llvm.org/pipermail/llvm-dev/2016-October/106182.html
2552; https://reviews.llvm.org/D83360
2553define i32 @false_undef(i1 %cond, i32 %x) {
2554; CHECK-LABEL: @false_undef(
2555; CHECK-NEXT:    [[S:%.*]] = select i1 [[COND:%.*]], i32 [[X:%.*]], i32 undef
2556; CHECK-NEXT:    ret i32 [[S]]
2557;
2558  %s = select i1 %cond, i32 %x, i32 undef
2559  ret i32 %s
2560}
2561
2562define i32 @true_undef(i1 %cond, i32 %x) {
2563; CHECK-LABEL: @true_undef(
2564; CHECK-NEXT:    [[S:%.*]] = select i1 [[COND:%.*]], i32 undef, i32 [[X:%.*]]
2565; CHECK-NEXT:    ret i32 [[S]]
2566;
2567  %s = select i1 %cond, i32 undef, i32 %x
2568  ret i32 %s
2569}
2570
2571define <2 x i32> @false_undef_vec(i1 %cond, <2 x i32> %x) {
2572; CHECK-LABEL: @false_undef_vec(
2573; CHECK-NEXT:    [[S:%.*]] = select i1 [[COND:%.*]], <2 x i32> [[X:%.*]], <2 x i32> undef
2574; CHECK-NEXT:    ret <2 x i32> [[S]]
2575;
2576  %s = select i1 %cond, <2 x i32> %x, <2 x i32> undef
2577  ret <2 x i32> %s
2578}
2579
2580define <2 x i32> @true_undef_vec(i1 %cond, <2 x i32> %x) {
2581; CHECK-LABEL: @true_undef_vec(
2582; CHECK-NEXT:    [[S:%.*]] = select i1 [[COND:%.*]], <2 x i32> undef, <2 x i32> [[X:%.*]]
2583; CHECK-NEXT:    ret <2 x i32> [[S]]
2584;
2585  %s = select i1 %cond, <2 x i32> undef, <2 x i32> %x
2586  ret <2 x i32> %s
2587}
2588
2589define i8 @cond_freeze(i8 %x, i8 %y) {
2590; CHECK-LABEL: @cond_freeze(
2591; CHECK-NEXT:    ret i8 [[Y:%.*]]
2592;
2593  %cond.fr = freeze i1 undef
2594  %s = select i1 %cond.fr, i8 %x, i8 %y
2595  ret i8 %s
2596}
2597
2598define i8 @cond_freeze_constant_false_val(i8 %x) {
2599; CHECK-LABEL: @cond_freeze_constant_false_val(
2600; CHECK-NEXT:    ret i8 1
2601;
2602  %cond.fr = freeze i1 undef
2603  %s = select i1 %cond.fr, i8 %x, i8 1
2604  ret i8 %s
2605}
2606
2607define i8 @cond_freeze_constant_true_val(i8 %x) {
2608; CHECK-LABEL: @cond_freeze_constant_true_val(
2609; CHECK-NEXT:    ret i8 1
2610;
2611  %cond.fr = freeze i1 undef
2612  %s = select i1 %cond.fr, i8 1, i8 %x
2613  ret i8 %s
2614}
2615
2616define i8 @cond_freeze_both_arms_constant() {
2617; CHECK-LABEL: @cond_freeze_both_arms_constant(
2618; CHECK-NEXT:    ret i8 42
2619;
2620  %cond.fr = freeze i1 undef
2621  %s = select i1 %cond.fr, i8 42, i8 3
2622  ret i8 %s
2623}
2624
2625define <2 x i8> @cond_freeze_constant_true_val_vec(<2 x i8> %x) {
2626; CHECK-LABEL: @cond_freeze_constant_true_val_vec(
2627; CHECK-NEXT:    ret <2 x i8> <i8 1, i8 2>
2628;
2629  %cond.fr = freeze <2 x i1> <i1 undef, i1 undef>
2630  %s = select <2 x i1> %cond.fr, <2 x i8> <i8 1, i8 2>, <2 x i8> %x
2631  ret <2 x i8> %s
2632}
2633
2634define <2 x i8> @partial_cond_freeze_constant_true_val_vec(<2 x i8> %x) {
2635; CHECK-LABEL: @partial_cond_freeze_constant_true_val_vec(
2636; CHECK-NEXT:    ret <2 x i8> <i8 1, i8 2>
2637;
2638  %cond.fr = freeze <2 x i1> <i1 true, i1 undef>
2639  %s = select <2 x i1> %cond.fr, <2 x i8> <i8 1, i8 2>, <2 x i8> %x
2640  ret <2 x i8> %s
2641}
2642
2643define <2 x i8> @partial_cond_freeze_constant_false_val_vec(<2 x i8> %x) {
2644; CHECK-LABEL: @partial_cond_freeze_constant_false_val_vec(
2645; CHECK-NEXT:    [[S1:%.*]] = insertelement <2 x i8> [[X:%.*]], i8 2, i64 1
2646; CHECK-NEXT:    ret <2 x i8> [[S1]]
2647;
2648  %cond.fr = freeze <2 x i1> <i1 true, i1 undef>
2649  %s = select <2 x i1> %cond.fr, <2 x i8> %x, <2 x i8> <i8 1, i8 2>
2650  ret <2 x i8> %s
2651}
2652
2653define <2 x i8> @partial_cond_freeze_both_arms_constant_vec() {
2654; CHECK-LABEL: @partial_cond_freeze_both_arms_constant_vec(
2655; CHECK-NEXT:    ret <2 x i8> <i8 42, i8 2>
2656;
2657  %cond.fr = freeze <2 x i1> <i1 false, i1 undef>
2658  %s = select <2 x i1> %cond.fr, <2 x i8> <i8 1, i8 2>, <2 x i8> <i8 42, i8 43>
2659  ret <2 x i8> %s
2660}
2661
2662declare void @foo2(i8, i8)
2663
2664define void @cond_freeze_multipleuses(i8 %x, i8 %y) {
2665; CHECK-LABEL: @cond_freeze_multipleuses(
2666; CHECK-NEXT:    call void @foo2(i8 [[Y:%.*]], i8 [[X:%.*]])
2667; CHECK-NEXT:    ret void
2668;
2669  %cond.fr = freeze i1 undef
2670  %s = select i1 %cond.fr, i8 %x, i8 %y
2671  %s2 = select i1 %cond.fr, i8 %y, i8 %x
2672  call void @foo2(i8 %s, i8 %s2)
2673  ret void
2674}
2675
2676define i32 @select_freeze_icmp_eq(i32 %x, i32 %y) {
2677; CHECK-LABEL: @select_freeze_icmp_eq(
2678; CHECK-NEXT:    ret i32 [[Y:%.*]]
2679;
2680  %c = icmp eq i32 %x, %y
2681  %c.fr = freeze i1 %c
2682  %v = select i1 %c.fr, i32 %x, i32 %y
2683  ret i32 %v
2684}
2685
2686define i32 @select_freeze_icmp_ne(i32 %x, i32 %y) {
2687; CHECK-LABEL: @select_freeze_icmp_ne(
2688; CHECK-NEXT:    ret i32 [[X:%.*]]
2689;
2690  %c = icmp ne i32 %x, %y
2691  %c.fr = freeze i1 %c
2692  %v = select i1 %c.fr, i32 %x, i32 %y
2693  ret i32 %v
2694}
2695
2696define i32 @select_freeze_icmp_else(i32 %x, i32 %y) {
2697; CHECK-LABEL: @select_freeze_icmp_else(
2698; CHECK-NEXT:    [[C:%.*]] = icmp ult i32 [[X:%.*]], [[Y:%.*]]
2699; CHECK-NEXT:    [[C_FR:%.*]] = freeze i1 [[C]]
2700; CHECK-NEXT:    [[V:%.*]] = select i1 [[C_FR]], i32 [[X]], i32 [[Y]]
2701; CHECK-NEXT:    ret i32 [[V]]
2702;
2703  %c = icmp ult i32 %x, %y
2704  %c.fr = freeze i1 %c
2705  %v = select i1 %c.fr, i32 %x, i32 %y
2706  ret i32 %v
2707}
2708
2709declare void @use_i1_i32(i1, i32)
2710
2711define void @select_freeze_icmp_multuses(i32 %x, i32 %y) {
2712; CHECK-LABEL: @select_freeze_icmp_multuses(
2713; CHECK-NEXT:    [[C:%.*]] = icmp ne i32 [[X:%.*]], [[Y:%.*]]
2714; CHECK-NEXT:    [[C_FR:%.*]] = freeze i1 [[C]]
2715; CHECK-NEXT:    [[V:%.*]] = select i1 [[C_FR]], i32 [[X]], i32 [[Y]]
2716; CHECK-NEXT:    call void @use_i1_i32(i1 [[C_FR]], i32 [[V]])
2717; CHECK-NEXT:    ret void
2718;
2719  %c = icmp ne i32 %x, %y
2720  %c.fr = freeze i1 %c
2721  %v = select i1 %c.fr, i32 %x, i32 %y
2722  call void @use_i1_i32(i1 %c.fr, i32 %v)
2723  ret void
2724}
2725
2726define i32 @pr47322_more_poisonous_replacement(i32 %arg) {
2727; CHECK-LABEL: @pr47322_more_poisonous_replacement(
2728; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[ARG:%.*]], 0
2729; CHECK-NEXT:    [[TRAILING:%.*]] = call i32 @llvm.cttz.i32(i32 [[ARG]], i1 immarg true), !range [[RNG0:![0-9]+]]
2730; CHECK-NEXT:    [[SHIFTED:%.*]] = lshr i32 [[ARG]], [[TRAILING]]
2731; CHECK-NEXT:    [[R1_SROA_0_1:%.*]] = select i1 [[CMP]], i32 0, i32 [[SHIFTED]]
2732; CHECK-NEXT:    ret i32 [[R1_SROA_0_1]]
2733;
2734  %cmp = icmp eq i32 %arg, 0
2735  %trailing = call i32 @llvm.cttz.i32(i32 %arg, i1 immarg true)
2736  %shifted = lshr i32 %arg, %trailing
2737  %r1.sroa.0.1 = select i1 %cmp, i32 0, i32 %shifted
2738  ret i32 %r1.sroa.0.1
2739}
2740
2741define i8 @select_replacement_add_eq(i8 %x, i8 %y) {
2742; CHECK-LABEL: @select_replacement_add_eq(
2743; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i8 [[X:%.*]], 1
2744; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[CMP]], i8 2, i8 [[Y:%.*]]
2745; CHECK-NEXT:    ret i8 [[SEL]]
2746;
2747  %cmp = icmp eq i8 %x, 1
2748  %add = add i8 %x, 1
2749  %sel = select i1 %cmp, i8 %add, i8 %y
2750  ret i8 %sel
2751}
2752
2753define i8 @select_replacement_add_ne(i8 %x, i8 %y) {
2754; CHECK-LABEL: @select_replacement_add_ne(
2755; CHECK-NEXT:    [[CMP:%.*]] = icmp ne i8 [[X:%.*]], 1
2756; CHECK-NEXT:    call void @use(i1 [[CMP]])
2757; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[CMP]], i8 [[Y:%.*]], i8 2
2758; CHECK-NEXT:    ret i8 [[SEL]]
2759;
2760  %cmp = icmp ne i8 %x, 1
2761  call void @use(i1 %cmp)
2762  %add = add i8 %x, 1
2763  %sel = select i1 %cmp, i8 %y, i8 %add
2764  ret i8 %sel
2765}
2766
2767define i8 @select_replacement_add_nuw(i8 %x, i8 %y) {
2768; CHECK-LABEL: @select_replacement_add_nuw(
2769; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i8 [[X:%.*]], 1
2770; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[CMP]], i8 2, i8 [[Y:%.*]]
2771; CHECK-NEXT:    ret i8 [[SEL]]
2772;
2773  %cmp = icmp eq i8 %x, 1
2774  %add = add nuw i8 %x, 1
2775  %sel = select i1 %cmp, i8 %add, i8 %y
2776  ret i8 %sel
2777}
2778
2779define i8 @select_replacement_sub_noundef(i8 %x, i8 noundef %y, i8 %z) {
2780; CHECK-LABEL: @select_replacement_sub_noundef(
2781; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i8 [[X:%.*]], [[Y:%.*]]
2782; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[CMP]], i8 0, i8 [[Z:%.*]]
2783; CHECK-NEXT:    ret i8 [[SEL]]
2784;
2785  %cmp = icmp eq i8 %x, %y
2786  %sub = sub i8 %x, %y
2787  %sel = select i1 %cmp, i8 %sub, i8 %z
2788  ret i8 %sel
2789}
2790
2791; TODO: The transform is also safe without noundef.
2792define i8 @select_replacement_sub(i8 %x, i8 %y, i8 %z) {
2793; CHECK-LABEL: @select_replacement_sub(
2794; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i8 [[X:%.*]], [[Y:%.*]]
2795; CHECK-NEXT:    [[SUB:%.*]] = sub i8 [[X]], [[Y]]
2796; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[CMP]], i8 [[SUB]], i8 [[Z:%.*]]
2797; CHECK-NEXT:    ret i8 [[SEL]]
2798;
2799  %cmp = icmp eq i8 %x, %y
2800  %sub = sub i8 %x, %y
2801  %sel = select i1 %cmp, i8 %sub, i8 %z
2802  ret i8 %sel
2803}
2804
2805define i8 @select_replacement_shift_noundef(i8 %x, i8 %y, i8 %z) {
2806; CHECK-LABEL: @select_replacement_shift_noundef(
2807; CHECK-NEXT:    [[SHR:%.*]] = lshr exact i8 [[X:%.*]], 1
2808; CHECK-NEXT:    call void @use_i8(i8 noundef [[SHR]])
2809; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i8 [[SHR]], [[Y:%.*]]
2810; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[CMP]], i8 [[X]], i8 [[Z:%.*]]
2811; CHECK-NEXT:    ret i8 [[SEL]]
2812;
2813  %shr = lshr exact i8 %x, 1
2814  call void @use_i8(i8 noundef %shr)
2815  %cmp = icmp eq i8 %shr, %y
2816  %shl = shl i8 %y, 1
2817  %sel = select i1 %cmp, i8 %shl, i8 %z
2818  ret i8 %sel
2819}
2820
2821; TODO: The transform is also safe without noundef.
2822define i8 @select_replacement_shift(i8 %x, i8 %y, i8 %z) {
2823; CHECK-LABEL: @select_replacement_shift(
2824; CHECK-NEXT:    [[SHR:%.*]] = lshr exact i8 [[X:%.*]], 1
2825; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i8 [[SHR]], [[Y:%.*]]
2826; CHECK-NEXT:    [[SHL:%.*]] = shl i8 [[Y]], 1
2827; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[CMP]], i8 [[SHL]], i8 [[Z:%.*]]
2828; CHECK-NEXT:    ret i8 [[SEL]]
2829;
2830  %shr = lshr exact i8 %x, 1
2831  %cmp = icmp eq i8 %shr, %y
2832  %shl = shl i8 %y, 1
2833  %sel = select i1 %cmp, i8 %shl, i8 %z
2834  ret i8 %sel
2835}
2836
2837define i8 @select_replacement_loop(i8 %x, i8 %y, i8 %z) {
2838; CHECK-LABEL: @select_replacement_loop(
2839; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i8 [[X:%.*]], [[Y:%.*]]
2840; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[CMP]], i8 [[X]], i8 [[Z:%.*]]
2841; CHECK-NEXT:    ret i8 [[SEL]]
2842;
2843  %cmp = icmp eq i8 %x, %y
2844  %sel = select i1 %cmp, i8 %x, i8 %z
2845  ret i8 %sel
2846}
2847
2848define i32 @select_replacement_loop2(i32 %arg, i32 %arg2) {
2849; CHECK-LABEL: @select_replacement_loop2(
2850; CHECK-NEXT:    [[DIV:%.*]] = udiv i32 [[ARG:%.*]], [[ARG2:%.*]]
2851; CHECK-NEXT:    [[MUL:%.*]] = mul nsw i32 [[DIV]], [[ARG2]]
2852; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[MUL]], [[ARG]]
2853; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[CMP]], i32 [[DIV]], i32 undef
2854; CHECK-NEXT:    ret i32 [[SEL]]
2855;
2856  %div = udiv i32 %arg, %arg2
2857  %mul = mul nsw i32 %div, %arg2
2858  %cmp = icmp eq i32 %mul, %arg
2859  %sel = select i1 %cmp, i32 %div, i32 undef
2860  ret i32 %sel
2861}
2862
2863; TODO: Dropping the inbounds flag should not be necessary for this fold.
2864define i8* @select_replacement_gep_inbounds(i8* %base, i64 %offset) {
2865; CHECK-LABEL: @select_replacement_gep_inbounds(
2866; CHECK-NEXT:    [[GEP:%.*]] = getelementptr i8, i8* [[BASE:%.*]], i64 [[OFFSET:%.*]]
2867; CHECK-NEXT:    ret i8* [[GEP]]
2868;
2869  %cmp = icmp eq i64 %offset, 0
2870  %gep = getelementptr inbounds i8, i8* %base, i64 %offset
2871  %sel = select i1 %cmp, i8* %base, i8* %gep
2872  ret i8* %sel
2873}
2874
2875define <2 x i1> @partial_true_undef_condval(<2 x i1> %x) {
2876; CHECK-LABEL: @partial_true_undef_condval(
2877; CHECK-NEXT:    ret <2 x i1> <i1 true, i1 poison>
2878;
2879  %r = select <2 x i1> <i1 true, i1 poison>, <2 x i1> <i1 true, i1 poison>, <2 x i1> %x
2880  ret <2 x i1> %r
2881}
2882
2883define <2 x i1> @partial_false_undef_condval(<2 x i1> %x) {
2884; CHECK-LABEL: @partial_false_undef_condval(
2885; CHECK-NEXT:    ret <2 x i1> <i1 false, i1 poison>
2886;
2887  %r = select <2 x i1> <i1 false, i1 poison>, <2 x i1> %x, <2 x i1> <i1 false, i1 poison>
2888  ret <2 x i1> %r
2889}
2890
2891; select (x == 0), 0, x * y --> freeze(y) * x
2892define i32 @mul_select_eq_zero(i32 %x, i32 %y) {
2893; CHECK-LABEL: @mul_select_eq_zero(
2894; CHECK-NEXT:    [[Y_FR:%.*]] = freeze i32 [[Y:%.*]]
2895; CHECK-NEXT:    [[M:%.*]] = mul i32 [[Y_FR]], [[X:%.*]]
2896; CHECK-NEXT:    ret i32 [[M]]
2897;
2898  %c = icmp eq i32 %x, 0
2899  %m = mul i32 %x, %y
2900  %r = select i1 %c, i32 0, i32 %m
2901  ret i32 %r
2902}
2903
2904; select (y == 0), 0, x * y --> freeze(x) * y
2905define i32 @mul_select_eq_zero_commute(i32 %x, i32 %y) {
2906; CHECK-LABEL: @mul_select_eq_zero_commute(
2907; CHECK-NEXT:    [[X_FR:%.*]] = freeze i32 [[X:%.*]]
2908; CHECK-NEXT:    [[M:%.*]] = mul i32 [[X_FR]], [[Y:%.*]]
2909; CHECK-NEXT:    ret i32 [[M]]
2910;
2911  %c = icmp eq i32 %y, 0
2912  %m = mul i32 %x, %y
2913  %r = select i1 %c, i32 0, i32 %m
2914  ret i32 %r
2915}
2916
2917; Check that mul's flags preserved during the transformation.
2918define i32 @mul_select_eq_zero_copy_flags(i32 %x, i32 %y) {
2919; CHECK-LABEL: @mul_select_eq_zero_copy_flags(
2920; CHECK-NEXT:    [[Y_FR:%.*]] = freeze i32 [[Y:%.*]]
2921; CHECK-NEXT:    [[M:%.*]] = mul nuw nsw i32 [[Y_FR]], [[X:%.*]]
2922; CHECK-NEXT:    ret i32 [[M]]
2923;
2924  %c = icmp eq i32 %x, 0
2925  %m = mul nuw nsw i32 %x, %y
2926  %r = select i1 %c, i32 0, i32 %m
2927  ret i32 %r
2928}
2929
2930; Check that the transformation could be applied after condition's inversion.
2931; select (x != 0), x * y, 0 --> freeze(y) * x
2932define i32 @mul_select_ne_zero(i32 %x, i32 %y) {
2933; CHECK-LABEL: @mul_select_ne_zero(
2934; CHECK-NEXT:    [[C:%.*]] = icmp ne i32 [[X:%.*]], 0
2935; CHECK-NEXT:    [[Y_FR:%.*]] = freeze i32 [[Y:%.*]]
2936; CHECK-NEXT:    [[M:%.*]] = mul i32 [[Y_FR]], [[X]]
2937; CHECK-NEXT:    call void @use(i1 [[C]])
2938; CHECK-NEXT:    ret i32 [[M]]
2939;
2940  %c = icmp ne i32 %x, 0
2941  %m = mul i32 %x, %y
2942  %r = select i1 %c, i32 %m, i32 0
2943  call void @use(i1 %c)
2944  ret i32 %r
2945}
2946
2947; Check that if one of a select's branches returns undef then
2948; an expression could be folded into mul as if there was a 0 instead of undef.
2949; select (x == 0), undef, x * y --> freeze(y) * x
2950define i32 @mul_select_eq_zero_sel_undef(i32 %x, i32 %y) {
2951; CHECK-LABEL: @mul_select_eq_zero_sel_undef(
2952; CHECK-NEXT:    [[Y_FR:%.*]] = freeze i32 [[Y:%.*]]
2953; CHECK-NEXT:    [[M:%.*]] = mul i32 [[Y_FR]], [[X:%.*]]
2954; CHECK-NEXT:    ret i32 [[M]]
2955;
2956  %c = icmp eq i32 %x, 0
2957  %m = mul i32 %x, %y
2958  %r = select i1 %c, i32 undef, i32 %m
2959  ret i32 %r
2960}
2961
2962; Check that the transformation is applied disregard to a number
2963; of expression's users.
2964define i32 @mul_select_eq_zero_multiple_users(i32 %x, i32 %y) {
2965; CHECK-LABEL: @mul_select_eq_zero_multiple_users(
2966; CHECK-NEXT:    [[Y_FR:%.*]] = freeze i32 [[Y:%.*]]
2967; CHECK-NEXT:    [[M:%.*]] = mul i32 [[Y_FR]], [[X:%.*]]
2968; CHECK-NEXT:    call void @use_i32(i32 [[M]])
2969; CHECK-NEXT:    call void @use_i32(i32 [[M]])
2970; CHECK-NEXT:    call void @use_i32(i32 [[M]])
2971; CHECK-NEXT:    ret i32 [[M]]
2972;
2973  %m = mul i32 %x, %y
2974  call void @use_i32(i32 %m)
2975  %c = icmp eq i32 %x, 0
2976  %r = select i1 %c, i32 0, i32 %m
2977  call void @use_i32(i32 %m)
2978  call void @use_i32(i32 %r)
2979  ret i32 %r
2980}
2981
2982; Negative test: select's condition is unrelated to multiplied values,
2983; so the transformation should not be applied.
2984define i32 @mul_select_eq_zero_unrelated_condition(i32 %x, i32 %y, i32 %z) {
2985; CHECK-LABEL: @mul_select_eq_zero_unrelated_condition(
2986; CHECK-NEXT:    [[C:%.*]] = icmp eq i32 [[Z:%.*]], 0
2987; CHECK-NEXT:    [[M:%.*]] = mul i32 [[X:%.*]], [[Y:%.*]]
2988; CHECK-NEXT:    [[R:%.*]] = select i1 [[C]], i32 0, i32 [[M]]
2989; CHECK-NEXT:    ret i32 [[R]]
2990;
2991  %c = icmp eq i32 %z, 0
2992  %m = mul i32 %x, %y
2993  %r = select i1 %c, i32 0, i32 %m
2994  ret i32 %r
2995}
2996
2997; select (<k x elt> x == 0), <k x elt> 0, <k x elt> x * y --> freeze(y) * x
2998define <4 x i32> @mul_select_eq_zero_vector(<4 x i32> %x, <4 x i32> %y) {
2999; CHECK-LABEL: @mul_select_eq_zero_vector(
3000; CHECK-NEXT:    [[Y_FR:%.*]] = freeze <4 x i32> [[Y:%.*]]
3001; CHECK-NEXT:    [[M:%.*]] = mul <4 x i32> [[Y_FR]], [[X:%.*]]
3002; CHECK-NEXT:    ret <4 x i32> [[M]]
3003;
3004  %c = icmp eq <4 x i32> %x, zeroinitializer
3005  %m = mul <4 x i32> %x, %y
3006  %r = select <4 x i1> %c, <4 x i32> zeroinitializer, <4 x i32> %m
3007  ret <4 x i32> %r
3008}
3009
3010; Check that a select is folded into multiplication if condition's operand
3011; is a vector consisting of zeros and undefs.
3012; select (<k x elt> x == {0, undef, ...}), <k x elt> 0, <k x elt> x * y --> freeze(y) * x
3013define <2 x i32> @mul_select_eq_undef_vector(<2 x i32> %x, <2 x i32> %y) {
3014; CHECK-LABEL: @mul_select_eq_undef_vector(
3015; CHECK-NEXT:    [[Y_FR:%.*]] = freeze <2 x i32> [[Y:%.*]]
3016; CHECK-NEXT:    [[M:%.*]] = mul <2 x i32> [[Y_FR]], [[X:%.*]]
3017; CHECK-NEXT:    ret <2 x i32> [[M]]
3018;
3019  %c = icmp eq <2 x i32> %x, <i32 0, i32 undef>
3020  %m = mul <2 x i32> %x, %y
3021  %r = select <2 x i1> %c, <2 x i32> <i32 0, i32 42>, <2 x i32> %m
3022  ret <2 x i32> %r
3023}
3024
3025; Check that a select is folded into multiplication if other select's operand
3026; is a vector consisting of zeros and undefs.
3027; select (<k x elt> x == 0), <k x elt> {0, undef, ...}, <k x elt> x * y --> freeze(y) * x
3028define <2 x i32> @mul_select_eq_zero_sel_undef_vector(<2 x i32> %x, <2 x i32> %y) {
3029; CHECK-LABEL: @mul_select_eq_zero_sel_undef_vector(
3030; CHECK-NEXT:    [[Y_FR:%.*]] = freeze <2 x i32> [[Y:%.*]]
3031; CHECK-NEXT:    [[M:%.*]] = mul <2 x i32> [[Y_FR]], [[X:%.*]]
3032; CHECK-NEXT:    ret <2 x i32> [[M]]
3033;
3034  %c = icmp eq <2 x i32> %x, zeroinitializer
3035  %m = mul <2 x i32> %x, %y
3036  %r = select <2 x i1> %c, <2 x i32> <i32 0, i32 undef>, <2 x i32> %m
3037  ret <2 x i32> %r
3038}
3039
3040; Negative test: select should not be folded into mul because
3041; condition's operand and select's operand do not merge into zero vector.
3042define <2 x i32> @mul_select_eq_undef_vector_not_merging_to_zero(<2 x i32> %x, <2 x i32> %y) {
3043; CHECK-LABEL: @mul_select_eq_undef_vector_not_merging_to_zero(
3044; CHECK-NEXT:    [[C:%.*]] = icmp eq <2 x i32> [[X:%.*]], <i32 0, i32 undef>
3045; CHECK-NEXT:    [[M:%.*]] = mul <2 x i32> [[X]], [[Y:%.*]]
3046; CHECK-NEXT:    [[R:%.*]] = select <2 x i1> [[C]], <2 x i32> <i32 1, i32 0>, <2 x i32> [[M]]
3047; CHECK-NEXT:    ret <2 x i32> [[R]]
3048;
3049  %c = icmp eq <2 x i32> %x, <i32 0, i32 undef>
3050  %m = mul <2 x i32> %x, %y
3051  %r = select <2 x i1> %c, <2 x i32> <i32 1, i32 0>, <2 x i32> %m
3052  ret <2 x i32> %r
3053}
3054
3055define i8 @ne0_is_all_ones(i8 %x) {
3056; CHECK-LABEL: @ne0_is_all_ones(
3057; CHECK-NEXT:    [[TMP1:%.*]] = icmp ne i8 [[X:%.*]], 0
3058; CHECK-NEXT:    [[R:%.*]] = sext i1 [[TMP1]] to i8
3059; CHECK-NEXT:    ret i8 [[R]]
3060;
3061  %negx = sub i8 0, %x
3062  %ugt1 = icmp ugt i8 %x, 1
3063  %r = select i1 %ugt1, i8 -1, i8 %negx
3064  ret i8 %r
3065}
3066
3067define i8 @ne0_is_all_ones_use1(i8 %x) {
3068; CHECK-LABEL: @ne0_is_all_ones_use1(
3069; CHECK-NEXT:    [[NEGX:%.*]] = sub i8 0, [[X:%.*]]
3070; CHECK-NEXT:    call void @use_i8(i8 [[NEGX]])
3071; CHECK-NEXT:    [[TMP1:%.*]] = icmp ne i8 [[X]], 0
3072; CHECK-NEXT:    [[R:%.*]] = sext i1 [[TMP1]] to i8
3073; CHECK-NEXT:    ret i8 [[R]]
3074;
3075  %negx = sub i8 0, %x
3076  call void @use_i8(i8 %negx)
3077  %ugt1 = icmp ugt i8 %x, 1
3078  %r = select i1 %ugt1, i8 -1, i8 %negx
3079  ret i8 %r
3080}
3081
3082; negative test
3083
3084define i8 @ne0_is_all_ones_use2(i8 %x) {
3085; CHECK-LABEL: @ne0_is_all_ones_use2(
3086; CHECK-NEXT:    [[NEGX:%.*]] = sub i8 0, [[X:%.*]]
3087; CHECK-NEXT:    [[UGT1:%.*]] = icmp ugt i8 [[X]], 1
3088; CHECK-NEXT:    call void @use(i1 [[UGT1]])
3089; CHECK-NEXT:    [[R:%.*]] = select i1 [[UGT1]], i8 -1, i8 [[NEGX]]
3090; CHECK-NEXT:    ret i8 [[R]]
3091;
3092  %negx = sub i8 0, %x
3093  %ugt1 = icmp ugt i8 %x, 1
3094  call void @use(i1 %ugt1)
3095  %r = select i1 %ugt1, i8 -1, i8 %negx
3096  ret i8 %r
3097}
3098
3099; negative test
3100
3101define i8 @ne0_is_all_ones_wrong_pred(i8 %x) {
3102; CHECK-LABEL: @ne0_is_all_ones_wrong_pred(
3103; CHECK-NEXT:    [[NEGX:%.*]] = sub i8 0, [[X:%.*]]
3104; CHECK-NEXT:    [[UGT1:%.*]] = icmp sgt i8 [[X]], 2
3105; CHECK-NEXT:    [[R:%.*]] = select i1 [[UGT1]], i8 -1, i8 [[NEGX]]
3106; CHECK-NEXT:    ret i8 [[R]]
3107;
3108  %negx = sub i8 0, %x
3109  %ugt1 = icmp sgt i8 %x, 2
3110  %r = select i1 %ugt1, i8 -1, i8 %negx
3111  ret i8 %r
3112}
3113
3114; negative test
3115
3116define i8 @ne0_is_all_ones_wrong_cmp(i8 %x) {
3117; CHECK-LABEL: @ne0_is_all_ones_wrong_cmp(
3118; CHECK-NEXT:    [[NEGX:%.*]] = sub i8 0, [[X:%.*]]
3119; CHECK-NEXT:    [[UGT1:%.*]] = icmp ugt i8 [[X]], 2
3120; CHECK-NEXT:    [[R:%.*]] = select i1 [[UGT1]], i8 -1, i8 [[NEGX]]
3121; CHECK-NEXT:    ret i8 [[R]]
3122;
3123  %negx = sub i8 0, %x
3124  %ugt1 = icmp ugt i8 %x, 2
3125  %r = select i1 %ugt1, i8 -1, i8 %negx
3126  ret i8 %r
3127}
3128
3129; negative test
3130
3131define i8 @ne0_is_all_ones_wrong_sel(i8 %x) {
3132; CHECK-LABEL: @ne0_is_all_ones_wrong_sel(
3133; CHECK-NEXT:    [[NEGX:%.*]] = sub i8 0, [[X:%.*]]
3134; CHECK-NEXT:    [[UGT1:%.*]] = icmp ugt i8 [[X]], 2
3135; CHECK-NEXT:    [[R:%.*]] = select i1 [[UGT1]], i8 1, i8 [[NEGX]]
3136; CHECK-NEXT:    ret i8 [[R]]
3137;
3138  %negx = sub i8 0, %x
3139  %ugt1 = icmp ugt i8 %x, 2
3140  %r = select i1 %ugt1, i8 1, i8 %negx
3141  ret i8 %r
3142}
3143
3144define <2 x i8> @ne0_is_all_ones_swap_vec(<2 x i8> %x) {
3145; CHECK-LABEL: @ne0_is_all_ones_swap_vec(
3146; CHECK-NEXT:    [[TMP1:%.*]] = icmp ne <2 x i8> [[X:%.*]], zeroinitializer
3147; CHECK-NEXT:    [[R:%.*]] = sext <2 x i1> [[TMP1]] to <2 x i8>
3148; CHECK-NEXT:    ret <2 x i8> [[R]]
3149;
3150  %negx = sub <2 x i8> zeroinitializer, %x
3151  %ult2 = icmp ult <2 x i8> %x, <i8 2, i8 2>
3152  %r = select <2 x i1> %ult2, <2 x i8> %negx, <2 x i8> <i8 -1, i8 -1>
3153  ret <2 x i8> %r
3154}
3155
3156define <2 x i8> @ne0_is_all_ones_swap_vec_poison(<2 x i8> %x) {
3157; CHECK-LABEL: @ne0_is_all_ones_swap_vec_poison(
3158; CHECK-NEXT:    [[TMP1:%.*]] = icmp ne <2 x i8> [[X:%.*]], zeroinitializer
3159; CHECK-NEXT:    [[R:%.*]] = sext <2 x i1> [[TMP1]] to <2 x i8>
3160; CHECK-NEXT:    ret <2 x i8> [[R]]
3161;
3162  %negx = sub <2 x i8> <i8 0, i8 poison>, %x
3163  %ult2 = icmp ult <2 x i8> %x, <i8 2, i8 poison>
3164  %r = select <2 x i1> %ult2, <2 x i8> %negx, <2 x i8> <i8 -1, i8 poison>
3165  ret <2 x i8> %r
3166}
3167
3168declare void @use(i1)
3169declare void @use_i8(i8)
3170declare void @use_i32(i32)
3171declare i32 @llvm.cttz.i32(i32, i1 immarg)
3172