1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt < %s -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 i32 @test1(i32 %A, i32 %B) {
9        %C = select i1 false, i32 %A, i32 %B
10        ret i32 %C
11; CHECK-LABEL: @test1(
12; CHECK: ret i32 %B
13}
14
15define i32 @test2(i32 %A, i32 %B) {
16        %C = select i1 true, i32 %A, i32 %B
17        ret i32 %C
18; CHECK-LABEL: @test2(
19; CHECK: ret i32 %A
20}
21
22
23define i32 @test3(i1 %C, i32 %I) {
24        ; V = I
25        %V = select i1 %C, i32 %I, i32 %I
26        ret i32 %V
27; CHECK-LABEL: @test3(
28; CHECK: ret i32 %I
29}
30
31define i1 @test4(i1 %C) {
32        ; V = C
33        %V = select i1 %C, i1 true, i1 false
34        ret i1 %V
35; CHECK-LABEL: @test4(
36; CHECK: ret i1 %C
37}
38
39define i1 @test5(i1 %C) {
40        ; V = !C
41        %V = select i1 %C, i1 false, i1 true
42        ret i1 %V
43; CHECK-LABEL: @test5(
44; CHECK: xor i1 %C, true
45; CHECK: ret i1
46}
47
48define i32 @test6(i1 %C) {
49        ; V = cast C to int
50        %V = select i1 %C, i32 1, i32 0
51        ret i32 %V
52; CHECK-LABEL: @test6(
53; CHECK: %V = zext i1 %C to i32
54; CHECK: ret i32 %V
55}
56
57define i1 @test7(i1 %C, i1 %X) {
58; CHECK-LABEL: @test7(
59; CHECK-NEXT:    [[R:%.*]] = or i1 %C, %X
60; CHECK-NEXT:    ret i1 [[R]]
61;
62  %R = select i1 %C, i1 true, i1 %X
63  ret i1 %R
64}
65
66define <2 x i1> @test7vec(<2 x i1> %C, <2 x i1> %X) {
67; CHECK-LABEL: @test7vec(
68; CHECK-NEXT:    [[R:%.*]] = or <2 x i1> %C, %X
69; CHECK-NEXT:    ret <2 x i1> [[R]]
70;
71  %R = select <2 x i1> %C, <2 x i1> <i1 true, i1 true>, <2 x i1> %X
72  ret <2 x i1> %R
73}
74
75define i1 @test8(i1 %C, i1 %X) {
76; CHECK-LABEL: @test8(
77; CHECK-NEXT:    [[R:%.*]] = and i1 %C, %X
78; CHECK-NEXT:    ret i1 [[R]]
79;
80  %R = select i1 %C, i1 %X, i1 false
81  ret i1 %R
82}
83
84define <2 x i1> @test8vec(<2 x i1> %C, <2 x i1> %X) {
85; CHECK-LABEL: @test8vec(
86; CHECK-NEXT:    [[R:%.*]] = and <2 x i1> %C, %X
87; CHECK-NEXT:    ret <2 x i1> [[R]]
88;
89  %R = select <2 x i1> %C, <2 x i1> %X, <2 x i1> <i1 false, i1 false>
90  ret <2 x i1> %R
91}
92
93define i1 @test9(i1 %C, i1 %X) {
94; CHECK-LABEL: @test9(
95; CHECK-NEXT:    [[NOT_C:%.*]] = xor i1 %C, true
96; CHECK-NEXT:    [[R:%.*]] = and i1 [[NOT_C]], %X
97; CHECK-NEXT:    ret i1 [[R]]
98;
99  %R = select i1 %C, i1 false, i1 %X
100  ret i1 %R
101}
102
103define <2 x i1> @test9vec(<2 x i1> %C, <2 x i1> %X) {
104; CHECK-LABEL: @test9vec(
105; CHECK-NEXT:    [[NOT_C:%.*]] = xor <2 x i1> %C, <i1 true, i1 true>
106; CHECK-NEXT:    [[R:%.*]] = and <2 x i1> [[NOT_C]], %X
107; CHECK-NEXT:    ret <2 x i1> [[R]]
108;
109  %R = select <2 x i1> %C, <2 x i1> <i1 false, i1 false>, <2 x i1> %X
110  ret <2 x i1> %R
111}
112
113define i1 @test10(i1 %C, i1 %X) {
114; CHECK-LABEL: @test10(
115; CHECK-NEXT:    [[NOT_C:%.*]] = xor i1 %C, true
116; CHECK-NEXT:    [[R:%.*]] = or i1 [[NOT_C]], %X
117; CHECK-NEXT:    ret i1 [[R]]
118;
119  %R = select i1 %C, i1 %X, i1 true
120  ret i1 %R
121}
122
123define <2 x i1> @test10vec(<2 x i1> %C, <2 x i1> %X) {
124; CHECK-LABEL: @test10vec(
125; CHECK-NEXT:    [[NOT_C:%.*]] = xor <2 x i1> %C, <i1 true, i1 true>
126; CHECK-NEXT:    [[R:%.*]] = or <2 x i1> [[NOT_C]], %X
127; CHECK-NEXT:    ret <2 x i1> [[R]]
128;
129  %R = select <2 x i1> %C, <2 x i1> %X, <2 x i1> <i1 true, i1 true>
130  ret <2 x i1> %R
131}
132
133define i1 @test23(i1 %a, i1 %b) {
134; CHECK-LABEL: @test23(
135; CHECK-NEXT:    [[C:%.*]] = and i1 %a, %b
136; CHECK-NEXT:    ret i1 [[C]]
137;
138  %c = select i1 %a, i1 %b, i1 %a
139  ret i1 %c
140}
141
142define <2 x i1> @test23vec(<2 x i1> %a, <2 x i1> %b) {
143; CHECK-LABEL: @test23vec(
144; CHECK-NEXT:    [[C:%.*]] = and <2 x i1> %a, %b
145; CHECK-NEXT:    ret <2 x i1> [[C]]
146;
147  %c = select <2 x i1> %a, <2 x i1> %b, <2 x i1> %a
148  ret <2 x i1> %c
149}
150
151define i1 @test24(i1 %a, i1 %b) {
152; CHECK-LABEL: @test24(
153; CHECK-NEXT:    [[C:%.*]] = or i1 %a, %b
154; CHECK-NEXT:    ret i1 [[C]]
155;
156  %c = select i1 %a, i1 %a, i1 %b
157  ret i1 %c
158}
159
160define <2 x i1> @test24vec(<2 x i1> %a, <2 x i1> %b) {
161; CHECK-LABEL: @test24vec(
162; CHECK-NEXT:    [[C:%.*]] = or <2 x i1> %a, %b
163; CHECK-NEXT:    ret <2 x i1> [[C]]
164;
165  %c = select <2 x i1> %a, <2 x i1> %a, <2 x i1> %b
166  ret <2 x i1> %c
167}
168
169define i1 @test62(i1 %A, i1 %B) {
170; CHECK-LABEL: @test62(
171; CHECK-NEXT:    [[NOT:%.*]] = xor i1 %A, true
172; CHECK-NEXT:    [[C:%.*]] = and i1 [[NOT]], %B
173; CHECK-NEXT:    ret i1 [[C]]
174;
175  %not = xor i1 %A, true
176  %C = select i1 %A, i1 %not, i1 %B
177  ret i1 %C
178}
179
180define <2 x i1> @test62vec(<2 x i1> %A, <2 x i1> %B) {
181; CHECK-LABEL: @test62vec(
182; CHECK-NEXT:    [[NOT:%.*]] = xor <2 x i1> %A, <i1 true, i1 true>
183; CHECK-NEXT:    [[C:%.*]] = and <2 x i1> [[NOT]], %B
184; CHECK-NEXT:    ret <2 x i1> [[C]]
185;
186  %not = xor <2 x i1> %A, <i1 true, i1 true>
187  %C = select <2 x i1> %A, <2 x i1> %not, <2 x i1> %B
188  ret <2 x i1> %C
189}
190
191define i1 @test63(i1 %A, i1 %B) {
192; CHECK-LABEL: @test63(
193; CHECK-NEXT:    [[NOT:%.*]] = xor i1 %A, true
194; CHECK-NEXT:    [[C:%.*]] = or i1 %B, [[NOT]]
195; CHECK-NEXT:    ret i1 [[C]]
196;
197  %not = xor i1 %A, true
198  %C = select i1 %A, i1 %B, i1 %not
199  ret i1 %C
200}
201
202define <2 x i1> @test63vec(<2 x i1> %A, <2 x i1> %B) {
203; CHECK-LABEL: @test63vec(
204; CHECK-NEXT:    [[NOT:%.*]] = xor <2 x i1> %A, <i1 true, i1 true>
205; CHECK-NEXT:    [[C:%.*]] = or <2 x i1> %B, [[NOT]]
206; CHECK-NEXT:    ret <2 x i1> [[C]]
207;
208  %not = xor <2 x i1> %A, <i1 true, i1 true>
209  %C = select <2 x i1> %A, <2 x i1> %B, <2 x i1> %not
210  ret <2 x i1> %C
211}
212
213define i32 @test11(i32 %a) {
214        %C = icmp eq i32 %a, 0
215        %R = select i1 %C, i32 0, i32 1
216        ret i32 %R
217; CHECK-LABEL: @test11(
218; CHECK: icmp ne i32 %a, 0
219; CHECK: %R = zext i1
220; CHECK: ret i32 %R
221}
222
223define i32 @test12(i1 %cond, i32 %a) {
224        %b = or i32 %a, 1
225        %c = select i1 %cond, i32 %b, i32 %a
226        ret i32 %c
227; CHECK-LABEL: @test12(
228; CHECK: %b = zext i1 %cond to i32
229; CHECK: %c = or i32 %b, %a
230; CHECK: ret i32 %c
231}
232
233define i32 @test12a(i1 %cond, i32 %a) {
234        %b = ashr i32 %a, 1
235        %c = select i1 %cond, i32 %b, i32 %a
236        ret i32 %c
237; CHECK-LABEL: @test12a(
238; CHECK: %b = zext i1 %cond to i32
239; CHECK: %c = ashr i32 %a, %b
240; CHECK: ret i32 %c
241}
242
243define i32 @test12b(i1 %cond, i32 %a) {
244; CHECK-LABEL: @test12b(
245; CHECK-NEXT:    [[NOT_COND:%.*]] = xor i1 %cond, true
246; CHECK-NEXT:    [[B:%.*]] = zext i1 [[NOT_COND]] to i32
247; CHECK-NEXT:    [[D:%.*]] = ashr i32 %a, [[B]]
248; CHECK-NEXT:    ret i32 [[D]]
249;
250  %b = ashr i32 %a, 1
251  %d = select i1 %cond, i32 %a, i32 %b
252  ret i32 %d
253}
254
255define i32 @test13(i32 %a, i32 %b) {
256        %C = icmp eq i32 %a, %b
257        %V = select i1 %C, i32 %a, i32 %b
258        ret i32 %V
259; CHECK-LABEL: @test13(
260; CHECK: ret i32 %b
261}
262
263define i32 @test13a(i32 %a, i32 %b) {
264        %C = icmp ne i32 %a, %b
265        %V = select i1 %C, i32 %a, i32 %b
266        ret i32 %V
267; CHECK-LABEL: @test13a(
268; CHECK: ret i32 %a
269}
270
271define i32 @test13b(i32 %a, i32 %b) {
272        %C = icmp eq i32 %a, %b
273        %V = select i1 %C, i32 %b, i32 %a
274        ret i32 %V
275; CHECK-LABEL: @test13b(
276; CHECK: ret i32 %a
277}
278
279define i1 @test14a(i1 %C, i32 %X) {
280        %V = select i1 %C, i32 %X, i32 0
281        ; (X < 1) | !C
282        %R = icmp slt i32 %V, 1
283        ret i1 %R
284; CHECK-LABEL: @test14a(
285; CHECK: icmp slt i32 %X, 1
286; CHECK: xor i1 %C, true
287; CHECK: or i1
288; CHECK: ret i1 %R
289}
290
291define i1 @test14b(i1 %C, i32 %X) {
292        %V = select i1 %C, i32 0, i32 %X
293        ; (X < 1) | C
294        %R = icmp slt i32 %V, 1
295        ret i1 %R
296; CHECK-LABEL: @test14b(
297; CHECK: icmp slt i32 %X, 1
298; CHECK: or i1
299; CHECK: ret i1 %R
300}
301
302;; Code sequence for (X & 16) ? 16 : 0
303define i32 @test15a(i32 %X) {
304        %t1 = and i32 %X, 16
305        %t2 = icmp eq i32 %t1, 0
306        %t3 = select i1 %t2, i32 0, i32 16
307        ret i32 %t3
308; CHECK-LABEL: @test15a(
309; CHECK: %t1 = and i32 %X, 16
310; CHECK: ret i32 %t1
311}
312
313;; Code sequence for (X & 32) ? 0 : 24
314define i32 @test15b(i32 %X) {
315        %t1 = and i32 %X, 32
316        %t2 = icmp eq i32 %t1, 0
317        %t3 = select i1 %t2, i32 32, i32 0
318        ret i32 %t3
319; CHECK-LABEL: @test15b(
320; CHECK: %t1 = and i32 %X, 32
321; CHECK: xor i32 %t1, 32
322; CHECK: ret i32
323}
324
325;; Alternate code sequence for (X & 16) ? 16 : 0
326define i32 @test15c(i32 %X) {
327        %t1 = and i32 %X, 16
328        %t2 = icmp eq i32 %t1, 16
329        %t3 = select i1 %t2, i32 16, i32 0
330        ret i32 %t3
331; CHECK-LABEL: @test15c(
332; CHECK: %t1 = and i32 %X, 16
333; CHECK: ret i32 %t1
334}
335
336;; Alternate code sequence for (X & 16) ? 16 : 0
337define i32 @test15d(i32 %X) {
338        %t1 = and i32 %X, 16
339        %t2 = icmp ne i32 %t1, 0
340        %t3 = select i1 %t2, i32 16, i32 0
341        ret i32 %t3
342; CHECK-LABEL: @test15d(
343; CHECK: %t1 = and i32 %X, 16
344; CHECK: ret i32 %t1
345}
346
347;; (a & 128) ? 256 : 0
348define i32 @test15e(i32 %X) {
349        %t1 = and i32 %X, 128
350        %t2 = icmp ne i32 %t1, 0
351        %t3 = select i1 %t2, i32 256, i32 0
352        ret i32 %t3
353; CHECK-LABEL: @test15e(
354; CHECK: %t1 = shl i32 %X, 1
355; CHECK: and i32 %t1, 256
356; CHECK: ret i32
357}
358
359;; (a & 128) ? 0 : 256
360define i32 @test15f(i32 %X) {
361        %t1 = and i32 %X, 128
362        %t2 = icmp ne i32 %t1, 0
363        %t3 = select i1 %t2, i32 0, i32 256
364        ret i32 %t3
365; CHECK-LABEL: @test15f(
366; CHECK: %t1 = shl i32 %X, 1
367; CHECK: and i32 %t1, 256
368; CHECK: xor i32 %{{.*}}, 256
369; CHECK: ret i32
370}
371
372;; (a & 8) ? -1 : -9
373define i32 @test15g(i32 %X) {
374        %t1 = and i32 %X, 8
375        %t2 = icmp ne i32 %t1, 0
376        %t3 = select i1 %t2, i32 -1, i32 -9
377        ret i32 %t3
378; CHECK-LABEL: @test15g(
379; CHECK-NEXT: %1 = or i32 %X, -9
380; CHECK-NEXT: ret i32 %1
381}
382
383;; (a & 8) ? -9 : -1
384define i32 @test15h(i32 %X) {
385        %t1 = and i32 %X, 8
386        %t2 = icmp ne i32 %t1, 0
387        %t3 = select i1 %t2, i32 -9, i32 -1
388        ret i32 %t3
389; CHECK-LABEL: @test15h(
390; CHECK-NEXT: %1 = or i32 %X, -9
391; CHECK-NEXT: %2 = xor i32 %1, 8
392; CHECK-NEXT: ret i32 %2
393}
394
395;; (a & 2) ? 577 : 1089
396define i32 @test15i(i32 %X) {
397        %t1 = and i32 %X, 2
398        %t2 = icmp ne i32 %t1, 0
399        %t3 = select i1 %t2, i32 577, i32 1089
400        ret i32 %t3
401; CHECK-LABEL: @test15i(
402; CHECK-NEXT: %t1 = shl i32 %X, 8
403; CHECK-NEXT: %1 = and i32 %t1, 512
404; CHECK-NEXT: %2 = xor i32 %1, 512
405; CHECK-NEXT: %3 = add nuw nsw i32 %2, 577
406; CHECK-NEXT: ret i32 %3
407}
408
409;; (a & 2) ? 1089 : 577
410define i32 @test15j(i32 %X) {
411        %t1 = and i32 %X, 2
412        %t2 = icmp ne i32 %t1, 0
413        %t3 = select i1 %t2, i32 1089, i32 577
414        ret i32 %t3
415; CHECK-LABEL: @test15j(
416; CHECK-NEXT: %t1 = shl i32 %X, 8
417; CHECK-NEXT: %1 = and i32 %t1, 512
418; CHECK-NEXT: %2 = add nuw nsw i32 %1, 577
419; CHECK-NEXT: ret i32 %2
420}
421
422define i32 @test16(i1 %C, i32* %P) {
423        %P2 = select i1 %C, i32* %P, i32* null
424        %V = load i32, i32* %P2
425        ret i32 %V
426; CHECK-LABEL: @test16(
427; CHECK-NEXT: %V = load i32, i32* %P
428; CHECK: ret i32 %V
429}
430
431;; It may be legal to load from a null address in a non-zero address space
432define i32 @test16_neg(i1 %C, i32 addrspace(1)* %P) {
433        %P2 = select i1 %C, i32 addrspace(1)* %P, i32 addrspace(1)* null
434        %V = load i32, i32 addrspace(1)* %P2
435        ret i32 %V
436; CHECK-LABEL: @test16_neg
437; CHECK-NEXT: %P2 = select i1 %C, i32 addrspace(1)* %P, i32 addrspace(1)* null
438; CHECK-NEXT: %V = load i32, i32 addrspace(1)* %P2
439; CHECK: ret i32 %V
440}
441define i32 @test16_neg2(i1 %C, i32 addrspace(1)* %P) {
442        %P2 = select i1 %C, i32 addrspace(1)* null, i32 addrspace(1)* %P
443        %V = load i32, i32 addrspace(1)* %P2
444        ret i32 %V
445; CHECK-LABEL: @test16_neg2
446; CHECK-NEXT: %P2 = select i1 %C, i32 addrspace(1)* null, i32 addrspace(1)* %P
447; CHECK-NEXT: %V = load i32, i32 addrspace(1)* %P2
448; CHECK: ret i32 %V
449}
450
451define i1 @test17(i32* %X, i1 %C) {
452        %R = select i1 %C, i32* %X, i32* null
453        %RV = icmp eq i32* %R, null
454        ret i1 %RV
455; CHECK-LABEL: @test17(
456; CHECK: icmp eq i32* %X, null
457; CHECK: xor i1 %C, true
458; CHECK: %RV = or i1
459; CHECK: ret i1 %RV
460}
461
462define i32 @test18(i32 %X, i32 %Y, i1 %C) {
463        %R = select i1 %C, i32 %X, i32 0
464        %V = sdiv i32 %Y, %R
465        ret i32 %V
466; CHECK-LABEL: @test18(
467; CHECK: %V = sdiv i32 %Y, %X
468; CHECK: ret i32 %V
469}
470
471define i32 @test19(i32 %x) {
472        %tmp = icmp ugt i32 %x, 2147483647
473        %retval = select i1 %tmp, i32 -1, i32 0
474        ret i32 %retval
475; CHECK-LABEL: @test19(
476; CHECK-NEXT: ashr i32 %x, 31
477; CHECK-NEXT: ret i32
478}
479
480define i32 @test20(i32 %x) {
481        %tmp = icmp slt i32 %x, 0
482        %retval = select i1 %tmp, i32 -1, i32 0
483        ret i32 %retval
484; CHECK-LABEL: @test20(
485; CHECK-NEXT: ashr i32 %x, 31
486; CHECK-NEXT: ret i32
487}
488
489define i64 @test21(i32 %x) {
490        %tmp = icmp slt i32 %x, 0
491        %retval = select i1 %tmp, i64 -1, i64 0
492        ret i64 %retval
493; CHECK-LABEL: @test21(
494; CHECK-NEXT: ashr i32 %x, 31
495; CHECK-NEXT: sext i32
496; CHECK-NEXT: ret i64
497}
498
499define i16 @test22(i32 %x) {
500        %tmp = icmp slt i32 %x, 0
501        %retval = select i1 %tmp, i16 -1, i16 0
502        ret i16 %retval
503; CHECK-LABEL: @test22(
504; CHECK-NEXT: ashr i32 %x, 31
505; CHECK-NEXT: trunc i32
506; CHECK-NEXT: ret i16
507}
508
509define i32 @test25(i1 %c)  {
510entry:
511  br i1 %c, label %jump, label %ret
512jump:
513  br label %ret
514ret:
515  %a = phi i1 [true, %jump], [false, %entry]
516  %b = select i1 %a, i32 10, i32 20
517  ret i32 %b
518; CHECK-LABEL: @test25(
519; CHECK: %a = phi i32 [ 10, %jump ], [ 20, %entry ]
520; CHECK-NEXT: ret i32 %a
521}
522
523define i32 @test26(i1 %cond)  {
524entry:
525  br i1 %cond, label %jump, label %ret
526jump:
527  %c = or i1 false, false
528  br label %ret
529ret:
530  %a = phi i1 [true, %entry], [%c, %jump]
531  %b = select i1 %a, i32 20, i32 10
532  ret i32 %b
533; CHECK-LABEL: @test26(
534; CHECK: %a = phi i32 [ 20, %entry ], [ 10, %jump ]
535; CHECK-NEXT: ret i32 %a
536}
537
538define i32 @test27(i1 %c, i32 %A, i32 %B)  {
539entry:
540  br i1 %c, label %jump, label %ret
541jump:
542  br label %ret
543ret:
544  %a = phi i1 [true, %jump], [false, %entry]
545  %b = select i1 %a, i32 %A, i32 %B
546  ret i32 %b
547; CHECK-LABEL: @test27(
548; CHECK: %a = phi i32 [ %A, %jump ], [ %B, %entry ]
549; CHECK-NEXT: ret i32 %a
550}
551
552define i32 @test28(i1 %cond, i32 %A, i32 %B)  {
553entry:
554  br i1 %cond, label %jump, label %ret
555jump:
556  br label %ret
557ret:
558  %c = phi i32 [%A, %jump], [%B, %entry]
559  %a = phi i1 [true, %jump], [false, %entry]
560  %b = select i1 %a, i32 %A, i32 %c
561  ret i32 %b
562; CHECK-LABEL: @test28(
563; CHECK: %a = phi i32 [ %A, %jump ], [ %B, %entry ]
564; CHECK-NEXT: ret i32 %a
565}
566
567define i32 @test29(i1 %cond, i32 %A, i32 %B)  {
568entry:
569  br i1 %cond, label %jump, label %ret
570jump:
571  br label %ret
572ret:
573  %c = phi i32 [%A, %jump], [%B, %entry]
574  %a = phi i1 [true, %jump], [false, %entry]
575  br label %next
576
577next:
578  %b = select i1 %a, i32 %A, i32 %c
579  ret i32 %b
580; CHECK-LABEL: @test29(
581; CHECK: %a = phi i32 [ %A, %jump ], [ %B, %entry ]
582; CHECK: ret i32 %a
583}
584
585
586; SMAX(SMAX(x, y), x) -> SMAX(x, y)
587define i32 @test30(i32 %x, i32 %y) {
588  %cmp = icmp sgt i32 %x, %y
589  %cond = select i1 %cmp, i32 %x, i32 %y
590
591  %cmp5 = icmp sgt i32 %cond, %x
592  %retval = select i1 %cmp5, i32 %cond, i32 %x
593  ret i32 %retval
594; CHECK-LABEL: @test30(
595; CHECK: ret i32 %cond
596}
597
598; UMAX(UMAX(x, y), x) -> UMAX(x, y)
599define i32 @test31(i32 %x, i32 %y) {
600  %cmp = icmp ugt i32 %x, %y
601  %cond = select i1 %cmp, i32 %x, i32 %y
602  %cmp5 = icmp ugt i32 %cond, %x
603  %retval = select i1 %cmp5, i32 %cond, i32 %x
604  ret i32 %retval
605; CHECK-LABEL: @test31(
606; CHECK: ret i32 %cond
607}
608
609; SMIN(SMIN(x, y), x) -> SMIN(x, y)
610define i32 @test32(i32 %x, i32 %y) {
611  %cmp = icmp sgt i32 %x, %y
612  %cond = select i1 %cmp, i32 %y, i32 %x
613  %cmp5 = icmp sgt i32 %cond, %x
614  %retval = select i1 %cmp5, i32 %x, i32 %cond
615  ret i32 %retval
616; CHECK-LABEL: @test32(
617; CHECK: ret i32 %cond
618}
619
620; MAX(MIN(x, y), x) -> x
621define i32 @test33(i32 %x, i32 %y) {
622  %cmp = icmp sgt i32 %x, %y
623  %cond = select i1 %cmp, i32 %y, i32 %x
624  %cmp5 = icmp sgt i32 %cond, %x
625  %retval = select i1 %cmp5, i32 %cond, i32 %x
626  ret i32 %retval
627; CHECK-LABEL: @test33(
628; CHECK: ret i32 %x
629}
630
631; MIN(MAX(x, y), x) -> x
632define i32 @test34(i32 %x, i32 %y) {
633  %cmp = icmp sgt i32 %x, %y
634  %cond = select i1 %cmp, i32 %x, i32 %y
635  %cmp5 = icmp sgt i32 %cond, %x
636  %retval = select i1 %cmp5, i32 %x, i32 %cond
637  ret i32 %retval
638; CHECK-LABEL: @test34(
639; CHECK: ret i32 %x
640}
641
642define i32 @test35(i32 %x) {
643  %cmp = icmp sge i32 %x, 0
644  %cond = select i1 %cmp, i32 60, i32 100
645  ret i32 %cond
646; CHECK-LABEL: @test35(
647; CHECK: ashr i32 %x, 31
648; CHECK: and i32 {{.*}}, 40
649; CHECK: add nuw nsw i32 {{.*}}, 60
650; CHECK: ret
651}
652
653define i32 @test36(i32 %x) {
654  %cmp = icmp slt i32 %x, 0
655  %cond = select i1 %cmp, i32 60, i32 100
656  ret i32 %cond
657; CHECK-LABEL: @test36(
658; CHECK: ashr i32 %x, 31
659; CHECK: and i32 {{.*}}, -40
660; CHECK: add nsw i32 {{.*}}, 100
661; CHECK: ret
662}
663
664define i32 @test37(i32 %x) {
665  %cmp = icmp sgt i32 %x, -1
666  %cond = select i1 %cmp, i32 1, i32 -1
667  ret i32 %cond
668; CHECK-LABEL: @test37(
669; CHECK: ashr i32 %x, 31
670; CHECK: or i32 {{.*}}, 1
671; CHECK: ret
672}
673
674define i1 @test38(i1 %cond) {
675  %zero = alloca i32
676  %one = alloca i32
677  %ptr = select i1 %cond, i32* %zero, i32* %one
678  %isnull = icmp eq i32* %ptr, null
679  ret i1 %isnull
680; CHECK-LABEL: @test38(
681; CHECK: ret i1 false
682}
683
684define i1 @test39(i1 %cond, double %x) {
685  %s = select i1 %cond, double %x, double 0x7FF0000000000000 ; RHS = +infty
686  %cmp = fcmp ule double %x, %s
687  ret i1 %cmp
688; CHECK-LABEL: @test39(
689; CHECK: ret i1 true
690}
691
692define i1 @test40(i1 %cond) {
693  %a = alloca i32
694  %b = alloca i32
695  %c = alloca i32
696  %s = select i1 %cond, i32* %a, i32* %b
697  %r = icmp eq i32* %s, %c
698  ret i1 %r
699; CHECK-LABEL: @test40(
700; CHECK: ret i1 false
701}
702
703define i32 @test41(i1 %cond, i32 %x, i32 %y) {
704  %z = and i32 %x, %y
705  %s = select i1 %cond, i32 %y, i32 %z
706  %r = and i32 %x, %s
707  ret i32 %r
708; CHECK-LABEL: @test41(
709; CHECK-NEXT: and i32 %x, %y
710; CHECK-NEXT: ret i32
711}
712
713define i32 @test42(i32 %x, i32 %y) {
714  %b = add i32 %y, -1
715  %cond = icmp eq i32 %x, 0
716  %c = select i1 %cond, i32 %b, i32 %y
717  ret i32 %c
718; CHECK-LABEL: @test42(
719; CHECK-NEXT: %cond = icmp eq i32 %x, 0
720; CHECK-NEXT: %b = sext i1 %cond to i32
721; CHECK-NEXT: %c = add i32 %b, %y
722; CHECK-NEXT: ret i32 %c
723}
724
725define i64 @test43(i32 %a) nounwind {
726	%a_ext = sext i32 %a to i64
727	%is_a_nonnegative = icmp sgt i32 %a, -1
728	%max = select i1 %is_a_nonnegative, i64 %a_ext, i64 0
729	ret i64 %max
730; CHECK-LABEL: @test43(
731; CHECK-NEXT: %a_ext = sext i32 %a to i64
732; CHECK-NEXT: %is_a_nonnegative = icmp slt i64 %a_ext, 0
733; CHECK-NEXT: %max = select i1 %is_a_nonnegative, i64 0, i64 %a_ext
734; CHECK-NEXT: ret i64 %max
735}
736
737define i64 @test44(i32 %a) nounwind {
738	%a_ext = sext i32 %a to i64
739	%is_a_nonpositive = icmp slt i32 %a, 1
740	%min = select i1 %is_a_nonpositive, i64 %a_ext, i64 0
741	ret i64 %min
742; CHECK-LABEL: @test44(
743; CHECK-NEXT: %a_ext = sext i32 %a to i64
744; CHECK-NEXT: %is_a_nonpositive = icmp sgt i64 %a_ext, 0
745; CHECK-NEXT: %min = select i1 %is_a_nonpositive, i64 0, i64 %a_ext
746; CHECK-NEXT: ret i64 %min
747}
748define i64 @test45(i32 %a) nounwind {
749	%a_ext = zext i32 %a to i64
750	%is_a_nonnegative = icmp ugt i32 %a, 2
751	%max = select i1 %is_a_nonnegative, i64 %a_ext, i64 3
752	ret i64 %max
753; CHECK-LABEL: @test45(
754; CHECK-NEXT: %a_ext = zext i32 %a to i64
755; CHECK-NEXT: %is_a_nonnegative = icmp ult i64 %a_ext, 3
756; CHECK-NEXT: %max = select i1 %is_a_nonnegative, i64 3, i64 %a_ext
757; CHECK-NEXT: ret i64 %max
758}
759
760define i64 @test46(i32 %a) nounwind {
761	%a_ext = zext i32 %a to i64
762	%is_a_nonpositive = icmp ult i32 %a, 3
763	%min = select i1 %is_a_nonpositive, i64 %a_ext, i64 2
764	ret i64 %min
765; CHECK-LABEL: @test46(
766; CHECK-NEXT: %a_ext = zext i32 %a to i64
767; CHECK-NEXT: %is_a_nonpositive = icmp ugt i64 %a_ext, 2
768; CHECK-NEXT: %min = select i1 %is_a_nonpositive, i64 2, i64 %a_ext
769; CHECK-NEXT: ret i64 %min
770}
771define i64 @test47(i32 %a) nounwind {
772	%a_ext = sext i32 %a to i64
773	%is_a_nonnegative = icmp ugt i32 %a, 2
774	%max = select i1 %is_a_nonnegative, i64 %a_ext, i64 3
775	ret i64 %max
776; CHECK-LABEL: @test47(
777; CHECK-NEXT: %a_ext = sext i32 %a to i64
778; CHECK-NEXT: %is_a_nonnegative = icmp ult i64 %a_ext, 3
779; CHECK-NEXT: %max = select i1 %is_a_nonnegative, i64 3, i64 %a_ext
780; CHECK-NEXT: ret i64 %max
781}
782
783define i64 @test48(i32 %a) nounwind {
784	%a_ext = sext i32 %a to i64
785	%is_a_nonpositive = icmp ult i32 %a, 3
786	%min = select i1 %is_a_nonpositive, i64 %a_ext, i64 2
787	ret i64 %min
788; CHECK-LABEL: @test48(
789; CHECK-NEXT: %a_ext = sext i32 %a to i64
790; CHECK-NEXT: %is_a_nonpositive = icmp ugt i64 %a_ext, 2
791; CHECK-NEXT: %min = select i1 %is_a_nonpositive, i64 2, i64 %a_ext
792; CHECK-NEXT: ret i64 %min
793}
794
795define i64 @test49(i32 %a) nounwind {
796	%a_ext = sext i32 %a to i64
797	%is_a_nonpositive = icmp ult i32 %a, 3
798	%min = select i1 %is_a_nonpositive, i64 2, i64 %a_ext
799	ret i64 %min
800; CHECK-LABEL: @test49(
801; CHECK-NEXT: %a_ext = sext i32 %a to i64
802; CHECK-NEXT: %is_a_nonpositive = icmp ugt i64 %a_ext, 2
803; CHECK-NEXT: %min = select i1 %is_a_nonpositive, i64 %a_ext, i64 2
804; CHECK-NEXT: ret i64 %min
805}
806define i64 @test50(i32 %a) nounwind {
807	%is_a_nonpositive = icmp ult i32 %a, 3
808	%a_ext = sext i32 %a to i64
809	%min = select i1 %is_a_nonpositive, i64 2, i64 %a_ext
810	ret i64 %min
811; CHECK-LABEL: @test50(
812; CHECK-NEXT: %a_ext = sext i32 %a to i64
813; CHECK-NEXT: %is_a_nonpositive = icmp ugt i64 %a_ext, 2
814; CHECK-NEXT: %min = select i1 %is_a_nonpositive, i64 %a_ext, i64 2
815; CHECK-NEXT: ret i64 %min
816}
817
818; PR8994
819
820; This select instruction can't be eliminated because trying to do so would
821; change the number of vector elements. This used to assert.
822define i48 @test51(<3 x i1> %icmp, <3 x i16> %tmp) {
823; CHECK-LABEL: @test51(
824  %select = select <3 x i1> %icmp, <3 x i16> zeroinitializer, <3 x i16> %tmp
825; CHECK: select <3 x i1>
826  %tmp2 = bitcast <3 x i16> %select to i48
827  ret i48 %tmp2
828}
829
830; Allow select promotion even if there are multiple uses of bitcasted ops.
831; Hoisting the selects allows later pattern matching to see that these are min/max ops.
832
833define void @min_max_bitcast(<4 x float> %a, <4 x float> %b, <4 x i32>* %ptr1, <4 x i32>* %ptr2) {
834; CHECK-LABEL: @min_max_bitcast(
835; CHECK-NEXT:    [[CMP:%.*]] = fcmp olt <4 x float> %a, %b
836; CHECK-NEXT:    [[SEL1_V:%.*]] = select <4 x i1> [[CMP]], <4 x float> %a, <4 x float> %b
837; CHECK-NEXT:    [[SEL2_V:%.*]] = select <4 x i1> [[CMP]], <4 x float> %b, <4 x float> %a
838; CHECK-NEXT:    [[TMP1:%.*]] = bitcast <4 x i32>* %ptr1 to <4 x float>*
839; CHECK-NEXT:    store <4 x float> [[SEL1_V]], <4 x float>* [[TMP1]], align 16
840; CHECK-NEXT:    [[TMP2:%.*]] = bitcast <4 x i32>* %ptr2 to <4 x float>*
841; CHECK-NEXT:    store <4 x float> [[SEL2_V]], <4 x float>* [[TMP2]], align 16
842; CHECK-NEXT:    ret void
843;
844  %cmp = fcmp olt <4 x float> %a, %b
845  %bc1 = bitcast <4 x float> %a to <4 x i32>
846  %bc2 = bitcast <4 x float> %b to <4 x i32>
847  %sel1 = select <4 x i1> %cmp, <4 x i32> %bc1, <4 x i32> %bc2
848  %sel2 = select <4 x i1> %cmp, <4 x i32> %bc2, <4 x i32> %bc1
849  store <4 x i32> %sel1, <4 x i32>* %ptr1
850  store <4 x i32> %sel2, <4 x i32>* %ptr2
851  ret void
852}
853
854; To avoid potential backend problems, we don't do the same transform for other casts.
855
856define 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) {
857; CHECK-LABEL: @truncs_before_selects(
858; CHECK-NEXT:    [[CMP:%.*]] = fcmp olt <4 x float> %f1, %f2
859; CHECK-NEXT:    [[BC1:%.*]] = trunc <4 x i64> %a to <4 x i32>
860; CHECK-NEXT:    [[BC2:%.*]] = trunc <4 x i64> %b to <4 x i32>
861; CHECK-NEXT:    [[SEL1:%.*]] = select <4 x i1> [[CMP]], <4 x i32> [[BC1]], <4 x i32> [[BC2]]
862; CHECK-NEXT:    [[SEL2:%.*]] = select <4 x i1> [[CMP]], <4 x i32> [[BC2]], <4 x i32> [[BC1]]
863; CHECK-NEXT:    store <4 x i32> [[SEL1]], <4 x i32>* %ptr1, align 16
864; CHECK-NEXT:    store <4 x i32> [[SEL2]], <4 x i32>* %ptr2, align 16
865; CHECK-NEXT:    ret void
866;
867  %cmp = fcmp olt <4 x float> %f1, %f2
868  %bc1 = trunc <4 x i64> %a to <4 x i32>
869  %bc2 = trunc <4 x i64> %b to <4 x i32>
870  %sel1 = select <4 x i1> %cmp, <4 x i32> %bc1, <4 x i32> %bc2
871  %sel2 = select <4 x i1> %cmp, <4 x i32> %bc2, <4 x i32> %bc1
872  store <4 x i32> %sel1, <4 x i32>* %ptr1, align 16
873  store <4 x i32> %sel2, <4 x i32>* %ptr2, align 16
874  ret void
875}
876
877; PR8575
878
879define i32 @test52(i32 %n, i32 %m) nounwind {
880; CHECK-LABEL: @test52(
881  %cmp = icmp sgt i32 %n, %m
882  %. = select i1 %cmp, i32 1, i32 3
883  %add = add nsw i32 %., 3
884  %storemerge = select i1 %cmp, i32 %., i32 %add
885; CHECK: select i1 %cmp, i32 1, i32 6
886  ret i32 %storemerge
887}
888
889; PR9454
890define i32 @test53(i32 %x) nounwind {
891  %and = and i32 %x, 2
892  %cmp = icmp eq i32 %and, %x
893  %sel = select i1 %cmp, i32 2, i32 1
894  ret i32 %sel
895; CHECK-LABEL: @test53(
896; CHECK: select i1 %cmp
897; CHECK: ret
898}
899
900define i32 @test54(i32 %X, i32 %Y) {
901  %A = ashr exact i32 %X, %Y
902  %B = icmp eq i32 %A, 0
903  %C = select i1 %B, i32 %A, i32 1
904  ret i32 %C
905; CHECK-LABEL: @test54(
906; CHECK-NOT: ashr
907; CHECK-NOT: select
908; CHECK: icmp ne i32 %X, 0
909; CHECK: zext
910; CHECK: ret
911}
912
913define i1 @test55(i1 %X, i32 %Y, i32 %Z) {
914  %A = ashr exact i32 %Y, %Z
915  %B = select i1 %X, i32 %Y, i32 %A
916  %C = icmp eq i32 %B, 0
917  ret i1 %C
918; CHECK-LABEL: @test55(
919; CHECK-NOT: ashr
920; CHECK-NOT: select
921; CHECK: icmp eq
922; CHECK: ret i1
923}
924
925define i32 @test56(i16 %x) nounwind {
926  %tobool = icmp eq i16 %x, 0
927  %conv = zext i16 %x to i32
928  %cond = select i1 %tobool, i32 0, i32 %conv
929  ret i32 %cond
930; CHECK-LABEL: @test56(
931; CHECK-NEXT: zext
932; CHECK-NEXT: ret
933}
934
935define i32 @test57(i32 %x, i32 %y) nounwind {
936  %and = and i32 %x, %y
937  %tobool = icmp eq i32 %x, 0
938  %.and = select i1 %tobool, i32 0, i32 %and
939  ret i32 %.and
940; CHECK-LABEL: @test57(
941; CHECK-NEXT: and i32 %x, %y
942; CHECK-NEXT: ret
943}
944
945define i32 @test58(i16 %x) nounwind {
946  %tobool = icmp ne i16 %x, 1
947  %conv = zext i16 %x to i32
948  %cond = select i1 %tobool, i32 %conv, i32 1
949  ret i32 %cond
950; CHECK-LABEL: @test58(
951; CHECK-NEXT: zext
952; CHECK-NEXT: ret
953}
954
955define i32 @test59(i32 %x, i32 %y) nounwind {
956  %and = and i32 %x, %y
957  %tobool = icmp ne i32 %x, %y
958  %.and = select i1 %tobool, i32 %and, i32 %y
959  ret i32 %.and
960; CHECK-LABEL: @test59(
961; CHECK-NEXT: and i32 %x, %y
962; CHECK-NEXT: ret
963}
964
965define i1 @test60(i32 %x, i1* %y) nounwind {
966  %cmp = icmp eq i32 %x, 0
967  %load = load i1, i1* %y, align 1
968  %cmp1 = icmp slt i32 %x, 1
969  %sel = select i1 %cmp, i1 %load, i1 %cmp1
970  ret i1 %sel
971; CHECK-LABEL: @test60(
972; CHECK: select
973}
974
975@glbl = constant i32 10
976define i32 @test61(i32* %ptr) {
977  %A = load i32, i32* %ptr
978  %B = icmp eq i32* %ptr, @glbl
979  %C = select i1 %B, i32 %A, i32 10
980  ret i32 %C
981; CHECK-LABEL: @test61(
982; CHECK: ret i32 10
983}
984
985; PR14131
986define void @test64(i32 %p, i16 %b) noreturn nounwind {
987entry:
988  %p.addr.0.insert.mask = and i32 %p, -65536
989  %conv2 = and i32 %p, 65535
990  br i1 undef, label %lor.rhs, label %lor.end
991
992lor.rhs:
993  %p.addr.0.extract.trunc = trunc i32 %p.addr.0.insert.mask to i16
994  %phitmp = zext i16 %p.addr.0.extract.trunc to i32
995  br label %lor.end
996
997lor.end:
998  %t.1 = phi i32 [ 0, %entry ], [ %phitmp, %lor.rhs ]
999  %conv6 = zext i16 %b to i32
1000  %div = udiv i32 %conv6, %t.1
1001  %tobool8 = icmp eq i32 %div, 0
1002  %cmp = icmp eq i32 %t.1, 0
1003  %cmp12 = icmp ult i32 %conv2, 2
1004  %cmp.sink = select i1 %tobool8, i1 %cmp12, i1 %cmp
1005  br i1 %cmp.sink, label %cond.end17, label %cond.false16
1006
1007cond.false16:
1008  br label %cond.end17
1009
1010cond.end17:
1011  br label %while.body
1012
1013while.body:
1014  br label %while.body
1015; CHECK-LABEL: @test64(
1016; CHECK-NOT: select
1017}
1018
1019; CHECK-LABEL: @select_icmp_eq_and_1_0_or_2(
1020; CHECK-NEXT: [[SHL:%[a-z0-9]+]] = shl i32 %x, 1
1021; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i32 [[SHL]], 2
1022; CHECK-NEXT: [[OR:%[a-z0-9]+]] = or i32 [[AND]], %y
1023; CHECK-NEXT: ret i32 [[OR]]
1024define i32 @select_icmp_eq_and_1_0_or_2(i32 %x, i32 %y) {
1025  %and = and i32 %x, 1
1026  %cmp = icmp eq i32 %and, 0
1027  %or = or i32 %y, 2
1028  %select = select i1 %cmp, i32 %y, i32 %or
1029  ret i32 %select
1030}
1031
1032; CHECK-LABEL: @select_icmp_eq_and_32_0_or_8(
1033; CHECK-NEXT: [[LSHR:%[a-z0-9]+]] = lshr i32 %x, 2
1034; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i32 [[LSHR]], 8
1035; CHECK-NEXT: [[OR:%[a-z0-9]+]] = or i32 [[AND]], %y
1036; CHECK-NEXT: ret i32 [[OR]]
1037define i32 @select_icmp_eq_and_32_0_or_8(i32 %x, i32 %y) {
1038  %and = and i32 %x, 32
1039  %cmp = icmp eq i32 %and, 0
1040  %or = or i32 %y, 8
1041  %select = select i1 %cmp, i32 %y, i32 %or
1042  ret i32 %select
1043}
1044
1045; CHECK-LABEL: @select_icmp_ne_0_and_4096_or_4096(
1046; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i32 %x, 4096
1047; CHECK-NEXT: [[XOR:%[a-z0-9]+]] = xor i32 [[AND]], 4096
1048; CHECK-NEXT: [[OR:%[a-z0-9]+]] = or i32 [[XOR]], %y
1049; CHECK-NEXT: ret i32 [[OR]]
1050define i32 @select_icmp_ne_0_and_4096_or_4096(i32 %x, i32 %y) {
1051  %and = and i32 %x, 4096
1052  %cmp = icmp ne i32 0, %and
1053  %or = or i32 %y, 4096
1054  %select = select i1 %cmp, i32 %y, i32 %or
1055  ret i32 %select
1056}
1057
1058; CHECK-LABEL: @select_icmp_eq_and_4096_0_or_4096(
1059; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i32 %x, 4096
1060; CHECK-NEXT: [[OR:%[a-z0-9]+]] = or i32 [[AND]], %y
1061; CHECK-NEXT: ret i32 [[OR]]
1062define i32 @select_icmp_eq_and_4096_0_or_4096(i32 %x, i32 %y) {
1063  %and = and i32 %x, 4096
1064  %cmp = icmp eq i32 %and, 0
1065  %or = or i32 %y, 4096
1066  %select = select i1 %cmp, i32 %y, i32 %or
1067  ret i32 %select
1068}
1069
1070; CHECK-LABEL: @select_icmp_eq_0_and_1_or_1(
1071; CHECK-NEXT: [[TRUNC:%.+]] = trunc i64 %x to i32
1072; CHECK-NEXT: [[AND:%.+]] = and i32 [[TRUNC]], 1
1073; CHECK-NEXT: [[OR:%.+]] = or i32 [[XOR]], %y
1074; CHECK-NEXT: ret i32 [[OR]]
1075define i32 @select_icmp_eq_0_and_1_or_1(i64 %x, i32 %y) {
1076  %and = and i64 %x, 1
1077  %cmp = icmp eq i64 %and, 0
1078  %or = or i32 %y, 1
1079  %select = select i1 %cmp, i32 %y, i32 %or
1080  ret i32 %select
1081}
1082
1083; CHECK-LABEL: @select_icmp_ne_0_and_4096_or_32(
1084; CHECK-NEXT: [[LSHR:%[a-z0-9]+]] = lshr i32 %x, 7
1085; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i32 [[LSHR]], 32
1086; CHECK-NEXT: [[XOR:%[a-z0-9]+]] = xor i32 [[AND]], 32
1087; CHECK-NEXT: [[OR:%[a-z0-9]+]] = or i32 [[XOR]], %y
1088; CHECK-NEXT: ret i32 [[OR]]
1089define i32 @select_icmp_ne_0_and_4096_or_32(i32 %x, i32 %y) {
1090  %and = and i32 %x, 4096
1091  %cmp = icmp ne i32 0, %and
1092  %or = or i32 %y, 32
1093  %select = select i1 %cmp, i32 %y, i32 %or
1094  ret i32 %select
1095}
1096
1097; CHECK-LABEL: @select_icmp_ne_0_and_32_or_4096(
1098; CHECK-NEXT: [[SHL:%[a-z0-9]+]] = shl i32 %x, 7
1099; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i32 [[SHL]], 4096
1100; CHECK-NEXT: [[XOR:%[a-z0-9]+]] = xor i32 [[AND]], 4096
1101; CHECK-NEXT: [[OR:%[a-z0-9]+]] = or i32 [[XOR]], %y
1102; CHECK-NEXT: ret i32 [[OR]]
1103define i32 @select_icmp_ne_0_and_32_or_4096(i32 %x, i32 %y) {
1104  %and = and i32 %x, 32
1105  %cmp = icmp ne i32 0, %and
1106  %or = or i32 %y, 4096
1107  %select = select i1 %cmp, i32 %y, i32 %or
1108  ret i32 %select
1109}
1110
1111; CHECK-LABEL: @select_icmp_ne_0_and_1073741824_or_8(
1112; CHECK-NEXT: [[LSHR:%.+]] = lshr i32 %x, 27
1113; CHECK-NEXT: [[TRUNC:%.+]] = trunc i32 [[LSHR]] to i8
1114; CHECK-NEXT: [[AND:%.+]] = and i8 [[TRUNC]], 8
1115; CHECK-NEXT: [[XOR:%.+]] = xor i8 [[AND]], 8
1116; CHECK-NEXT: [[OR:%.+]] = or i8 [[XOR]], %y
1117; CHECK-NEXT: ret i8 [[OR]]
1118define i8 @select_icmp_ne_0_and_1073741824_or_8(i32 %x, i8 %y) {
1119  %and = and i32 %x, 1073741824
1120  %cmp = icmp ne i32 0, %and
1121  %or = or i8 %y, 8
1122  %select = select i1 %cmp, i8 %y, i8 %or
1123  ret i8 %select
1124}
1125
1126; CHECK-LABEL: @select_icmp_ne_0_and_8_or_1073741824(
1127; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i8 %x, 8
1128; CHECK-NEXT: [[ZEXT:%[a-z0-9]+]] = zext i8 [[AND]] to i32
1129; CHECK-NEXT: [[SHL:%[a-z0-9]+]] = shl nuw nsw i32 [[ZEXT]], 27
1130; CHECK-NEXT: [[XOR:%[a-z0-9]+]] = xor i32 [[SHL]], 1073741824
1131; CHECK-NEXT: [[OR:%[a-z0-9]+]] = or i32 [[XOR]], %y
1132; CHECK-NEXT: ret i32 [[OR]]
1133define i32 @select_icmp_ne_0_and_8_or_1073741824(i8 %x, i32 %y) {
1134  %and = and i8 %x, 8
1135  %cmp = icmp ne i8 0, %and
1136  %or = or i32 %y, 1073741824
1137  %select = select i1 %cmp, i32 %y, i32 %or
1138  ret i32 %select
1139}
1140
1141; We can't combine here, because the cmp is scalar and the or vector.
1142; Just make sure we don't assert.
1143define <2 x i32> @select_icmp_eq_and_1_0_or_vector_of_2s(i32 %x, <2 x i32> %y) {
1144  %and = and i32 %x, 1
1145  %cmp = icmp eq i32 %and, 0
1146  %or = or <2 x i32> %y, <i32 2, i32 2>
1147  %select = select i1 %cmp, <2 x i32> %y, <2 x i32> %or
1148  ret <2 x i32> %select
1149}
1150
1151; CHECK-LABEL: @select_icmp_and_8_ne_0_xor_8(
1152; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i32 %x, -9
1153; CHECK-NEXT: ret i32 [[AND]]
1154define i32 @select_icmp_and_8_ne_0_xor_8(i32 %x) {
1155  %and = and i32 %x, 8
1156  %cmp = icmp eq i32 %and, 0
1157  %xor = xor i32 %x, 8
1158  %x.xor = select i1 %cmp, i32 %x, i32 %xor
1159  ret i32 %x.xor
1160}
1161
1162; CHECK-LABEL: @select_icmp_and_8_eq_0_xor_8(
1163; CHECK-NEXT: [[OR:%[a-z0-9]+]] = or i32 %x, 8
1164; CHECK-NEXT: ret i32 [[OR]]
1165define i32 @select_icmp_and_8_eq_0_xor_8(i32 %x) {
1166  %and = and i32 %x, 8
1167  %cmp = icmp eq i32 %and, 0
1168  %xor = xor i32 %x, 8
1169  %xor.x = select i1 %cmp, i32 %xor, i32 %x
1170  ret i32 %xor.x
1171}
1172
1173; CHECK-LABEL: @select_icmp_x_and_8_eq_0_y_xor_8(
1174; CHECK: select i1 %cmp, i64 %y, i64 %xor
1175define i64 @select_icmp_x_and_8_eq_0_y_xor_8(i32 %x, i64 %y) {
1176  %and = and i32 %x, 8
1177  %cmp = icmp eq i32 %and, 0
1178  %xor = xor i64 %y, 8
1179  %y.xor = select i1 %cmp, i64 %y, i64 %xor
1180  ret i64 %y.xor
1181}
1182
1183; CHECK-LABEL: @select_icmp_x_and_8_ne_0_y_xor_8(
1184; CHECK: select i1 %cmp, i64 %xor, i64 %y
1185define i64 @select_icmp_x_and_8_ne_0_y_xor_8(i32 %x, i64 %y) {
1186  %and = and i32 %x, 8
1187  %cmp = icmp eq i32 %and, 0
1188  %xor = xor i64 %y, 8
1189  %xor.y = select i1 %cmp, i64 %xor, i64 %y
1190  ret i64 %xor.y
1191}
1192
1193define i64 @select_icmp_x_and_8_ne_0_y_or_8(i32 %x, i64 %y) {
1194; CHECK-LABEL: @select_icmp_x_and_8_ne_0_y_or_8(
1195; CHECK-NEXT:    [[AND:%.*]] = and i32 %x, 8
1196; CHECK-NEXT:    [[TMP1:%.*]] = xor i32 [[AND]], 8
1197; CHECK-NEXT:    [[TMP2:%.*]] = zext i32 [[TMP1]] to i64
1198; CHECK-NEXT:    [[TMP3:%.*]] = or i64 [[TMP2]], %y
1199; CHECK-NEXT:    ret i64 [[TMP3]]
1200;
1201  %and = and i32 %x, 8
1202  %cmp = icmp eq i32 %and, 0
1203  %or = or i64 %y, 8
1204  %or.y = select i1 %cmp, i64 %or, i64 %y
1205  ret i64 %or.y
1206}
1207
1208; CHECK-LABEL: @select_icmp_and_2147483648_ne_0_xor_2147483648(
1209; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i32 %x, 2147483647
1210; CHECK-NEXT: ret i32 [[AND]]
1211define i32 @select_icmp_and_2147483648_ne_0_xor_2147483648(i32 %x) {
1212  %and = and i32 %x, 2147483648
1213  %cmp = icmp eq i32 %and, 0
1214  %xor = xor i32 %x, 2147483648
1215  %x.xor = select i1 %cmp, i32 %x, i32 %xor
1216  ret i32 %x.xor
1217}
1218
1219; CHECK-LABEL: @select_icmp_and_2147483648_eq_0_xor_2147483648(
1220; CHECK-NEXT: [[OR:%[a-z0-9]+]] = or i32 %x, -2147483648
1221; CHECK-NEXT: ret i32 [[OR]]
1222define i32 @select_icmp_and_2147483648_eq_0_xor_2147483648(i32 %x) {
1223  %and = and i32 %x, 2147483648
1224  %cmp = icmp eq i32 %and, 0
1225  %xor = xor i32 %x, 2147483648
1226  %xor.x = select i1 %cmp, i32 %xor, i32 %x
1227  ret i32 %xor.x
1228}
1229
1230; CHECK-LABEL: @select_icmp_x_and_2147483648_ne_0_or_2147483648(
1231; CHECK-NEXT: [[OR:%[a-z0-9]+]] = or i32 %x, -2147483648
1232; CHECK-NEXT: ret i32 [[OR]]
1233define i32 @select_icmp_x_and_2147483648_ne_0_or_2147483648(i32 %x) {
1234  %and = and i32 %x, 2147483648
1235  %cmp = icmp eq i32 %and, 0
1236  %or = or i32 %x, 2147483648
1237  %or.x = select i1 %cmp, i32 %or, i32 %x
1238  ret i32 %or.x
1239}
1240
1241define i32 @test65(i64 %x) {
1242  %1 = and i64 %x, 16
1243  %2 = icmp ne i64 %1, 0
1244  %3 = select i1 %2, i32 40, i32 42
1245  ret i32 %3
1246
1247; CHECK-LABEL: @test65(
1248; CHECK: %[[TRUNC:.*]] = trunc i64 %x to i32
1249; CHECK: %[[LSHR:.*]] = lshr i32 %[[TRUNC]], 3
1250; CHECK: %[[AND:.*]] = and i32 %[[LSHR]], 2
1251; CHECK: %[[XOR:.*]] = xor i32 %[[AND]], 42
1252; CHECK: ret i32 %[[XOR]]
1253}
1254
1255define i32 @test66(i64 %x) {
1256  %1 = and i64 %x, 4294967296
1257  %2 = icmp ne i64 %1, 0
1258  %3 = select i1 %2, i32 40, i32 42
1259  ret i32 %3
1260
1261; CHECK-LABEL: @test66(
1262; CHECK: select
1263}
1264
1265define i32 @test67(i16 %x) {
1266  %1 = and i16 %x, 4
1267  %2 = icmp ne i16 %1, 0
1268  %3 = select i1 %2, i32 40, i32 42
1269  ret i32 %3
1270
1271; CHECK-LABEL: @test67(
1272; CHECK: and i16 %x, 4
1273; CHECK: zext i16 %1 to i32
1274; CHECK: lshr exact i32 %2, 1
1275; CHECK: xor i32 %3, 42
1276}
1277
1278; SMIN(SMIN(X, 11), 92) -> SMIN(X, 11)
1279define i32 @test68(i32 %x) {
1280entry:
1281  %cmp = icmp slt i32 11, %x
1282  %cond = select i1 %cmp, i32 11, i32 %x
1283  %cmp3 = icmp slt i32 92, %cond
1284  %retval = select i1 %cmp3, i32 92, i32 %cond
1285  ret i32 %retval
1286; CHECK-LABEL: @test68(
1287; CHECK: ret i32 %cond
1288}
1289
1290; MIN(MIN(X, 24), 83) -> MIN(X, 24)
1291define i32 @test69(i32 %x) {
1292entry:
1293  %cmp = icmp ult i32 24, %x
1294  %cond = select i1 %cmp, i32 24, i32 %x
1295  %cmp3 = icmp ult i32 83, %cond
1296  %retval = select i1 %cmp3, i32 83, i32 %cond
1297  ret i32 %retval
1298; CHECK-LABEL: @test69(
1299; CHECK: ret i32 %cond
1300}
1301
1302; SMAX(SMAX(X, 75), 36) -> SMAX(X, 75)
1303define i32 @test70(i32 %x) {
1304entry:
1305  %cmp = icmp slt i32 %x, 75
1306  %cond = select i1 %cmp, i32 75, i32 %x
1307  %cmp3 = icmp slt i32 %cond, 36
1308  %retval = select i1 %cmp3, i32 36, i32 %cond
1309  ret i32 %retval
1310; CHECK-LABEL: @test70(
1311; CHECK: ret i32 %cond
1312}
1313
1314; MAX(MAX(X, 68), 47) -> MAX(X, 68)
1315define i32 @test71(i32 %x) {
1316entry:
1317  %cmp = icmp ult i32 %x, 68
1318  %cond = select i1 %cmp, i32 68, i32 %x
1319  %cmp3 = icmp ult i32 %cond, 47
1320  %retval = select i1 %cmp3, i32 47, i32 %cond
1321  ret i32 %retval
1322; CHECK-LABEL: @test71(
1323; CHECK: ret i32 %cond
1324}
1325
1326; SMIN(SMIN(X, 92), 11) -> SMIN(X, 11)
1327define i32 @test72(i32 %x) {
1328  %cmp = icmp sgt i32 %x, 92
1329  %cond = select i1 %cmp, i32 92, i32 %x
1330  %cmp3 = icmp sgt i32 %cond, 11
1331  %retval = select i1 %cmp3, i32 11, i32 %cond
1332  ret i32 %retval
1333; CHECK-LABEL: @test72(
1334; CHECK-NEXT: [[CMP:%[a-z0-9]+]] = icmp sgt i32 %x, 11
1335; CHECK-NEXT: [[SEL:%[a-z0-9]+]] = select i1 [[CMP]], i32 11, i32 %x
1336; CHECK-NEXT: ret i32 [[SEL]]
1337}
1338
1339; MIN(MIN(X, 83), 24) -> MIN(X, 24)
1340define i32 @test73(i32 %x) {
1341  %cmp = icmp ugt i32 %x, 83
1342  %cond = select i1 %cmp, i32 83, i32 %x
1343  %cmp3 = icmp ugt i32 %cond, 24
1344  %retval = select i1 %cmp3, i32 24, i32 %cond
1345  ret i32 %retval
1346; CHECK-LABEL: @test73(
1347; CHECK-NEXT: [[CMP:%[a-z0-9]+]] = icmp ugt i32 %x, 24
1348; CHECK-NEXT: [[SEL:%[a-z0-9]+]] = select i1 [[CMP]], i32 24, i32 %x
1349; CHECK-NEXT: ret i32 [[SEL]]
1350}
1351
1352; SMAX(SMAX(X, 36), 75) -> SMAX(X, 75)
1353define i32 @test74(i32 %x) {
1354  %cmp = icmp slt i32 %x, 36
1355  %cond = select i1 %cmp, i32 36, i32 %x
1356  %cmp3 = icmp slt i32 %cond, 75
1357  %retval = select i1 %cmp3, i32 75, i32 %cond
1358  ret i32 %retval
1359; CHECK-LABEL: @test74(
1360; CHECK-NEXT: [[CMP:%[a-z0-9]+]] = icmp slt i32 %x, 75
1361; CHECK-NEXT: [[SEL:%[a-z0-9]+]] = select i1 [[CMP]], i32 75, i32 %x
1362; CHECK-NEXT: ret i32 [[SEL]]
1363}
1364
1365; MAX(MAX(X, 47), 68) -> MAX(X, 68)
1366define i32 @test75(i32 %x) {
1367  %cmp = icmp ult i32 %x, 47
1368  %cond = select i1 %cmp, i32 47, i32 %x
1369  %cmp3 = icmp ult i32 %cond, 68
1370  %retval = select i1 %cmp3, i32 68, i32 %cond
1371  ret i32 %retval
1372; CHECK-LABEL: @test75(
1373; CHECK-NEXT: [[CMP:%[a-z0-9]+]] = icmp ult i32 %x, 68
1374; CHECK-NEXT: [[SEL:%[a-z0-9]+]] = select i1 [[CMP]], i32 68, i32 %x
1375; CHECK-NEXT: ret i32 [[SEL]]
1376}
1377
1378@under_aligned = external global i32, align 1
1379
1380define i32 @test76(i1 %flag, i32* %x) {
1381; The load here must not be speculated around the select. One side of the
1382; select is trivially dereferencable but may have a lower alignment than the
1383; load does.
1384; CHECK-LABEL: @test76(
1385; CHECK: store i32 0, i32* %x
1386; CHECK: %[[P:.*]] = select i1 %flag, i32* @under_aligned, i32* %x
1387; CHECK: load i32, i32* %[[P]]
1388
1389  store i32 0, i32* %x
1390  %p = select i1 %flag, i32* @under_aligned, i32* %x
1391  %v = load i32, i32* %p
1392  ret i32 %v
1393}
1394
1395declare void @scribble_on_i32(i32*)
1396
1397define i32 @test77(i1 %flag, i32* %x) {
1398; The load here must not be speculated around the select. One side of the
1399; select is trivially dereferencable but may have a lower alignment than the
1400; load does.
1401; CHECK-LABEL: @test77(
1402; CHECK: %[[A:.*]] = alloca i32, align 1
1403; CHECK: call void @scribble_on_i32(i32* nonnull %[[A]])
1404; CHECK: store i32 0, i32* %x
1405; CHECK: %[[P:.*]] = select i1 %flag, i32* %[[A]], i32* %x
1406; CHECK: load i32, i32* %[[P]]
1407
1408  %under_aligned = alloca i32, align 1
1409  call void @scribble_on_i32(i32* %under_aligned)
1410  store i32 0, i32* %x
1411  %p = select i1 %flag, i32* %under_aligned, i32* %x
1412  %v = load i32, i32* %p
1413  ret i32 %v
1414}
1415
1416define i32 @test78(i1 %flag, i32* %x, i32* %y, i32* %z) {
1417; Test that we can speculate the loads around the select even when we can't
1418; fold the load completely away.
1419; CHECK-LABEL: @test78(
1420; CHECK:         %[[V1:.*]] = load i32, i32* %x
1421; CHECK-NEXT:    %[[V2:.*]] = load i32, i32* %y
1422; CHECK-NEXT:    %[[S:.*]] = select i1 %flag, i32 %[[V1]], i32 %[[V2]]
1423; CHECK-NEXT:    ret i32 %[[S]]
1424entry:
1425  store i32 0, i32* %x
1426  store i32 0, i32* %y
1427  ; Block forwarding by storing to %z which could alias either %x or %y.
1428  store i32 42, i32* %z
1429  %p = select i1 %flag, i32* %x, i32* %y
1430  %v = load i32, i32* %p
1431  ret i32 %v
1432}
1433
1434define i32 @test78_deref(i1 %flag, i32* dereferenceable(4) %x, i32* dereferenceable(4) %y, i32* %z) {
1435; Test that we can speculate the loads around the select even when we can't
1436; fold the load completely away.
1437; CHECK-LABEL: @test78_deref(
1438; CHECK:         %[[V1:.*]] = load i32, i32* %x
1439; CHECK-NEXT:    %[[V2:.*]] = load i32, i32* %y
1440; CHECK-NEXT:    %[[S:.*]] = select i1 %flag, i32 %[[V1]], i32 %[[V2]]
1441; CHECK-NEXT:    ret i32 %[[S]]
1442entry:
1443  %p = select i1 %flag, i32* %x, i32* %y
1444  %v = load i32, i32* %p
1445  ret i32 %v
1446}
1447
1448define i32 @test78_neg(i1 %flag, i32* %x, i32* %y, i32* %z) {
1449; The same as @test78 but we can't speculate the load because it can trap
1450; if under-aligned.
1451; CHECK-LABEL: @test78_neg(
1452; CHECK: %p = select i1 %flag, i32* %x, i32* %y
1453; CHECK-NEXT: %v = load i32, i32* %p, align 16
1454; CHECK-NEXT: ret i32 %v
1455entry:
1456  store i32 0, i32* %x
1457  store i32 0, i32* %y
1458  ; Block forwarding by storing to %z which could alias either %x or %y.
1459  store i32 42, i32* %z
1460  %p = select i1 %flag, i32* %x, i32* %y
1461  %v = load i32, i32* %p, align 16
1462  ret i32 %v
1463}
1464
1465define i32 @test78_deref_neg(i1 %flag, i32* dereferenceable(2) %x, i32* dereferenceable(4) %y, i32* %z) {
1466; The same as @test78_deref but we can't speculate the load because
1467; one of the arguments is not sufficiently dereferenceable.
1468; CHECK-LABEL: @test78_deref_neg(
1469; CHECK: %p = select i1 %flag, i32* %x, i32* %y
1470; CHECK-NEXT: %v = load i32, i32* %p
1471; CHECK-NEXT: ret i32 %v
1472entry:
1473  %p = select i1 %flag, i32* %x, i32* %y
1474  %v = load i32, i32* %p
1475  ret i32 %v
1476}
1477
1478define float @test79(i1 %flag, float* %x, i32* %y, i32* %z) {
1479; Test that we can speculate the loads around the select even when we can't
1480; fold the load completely away.
1481; CHECK-LABEL: @test79(
1482; CHECK:         %[[V1:.*]] = load float, float* %x
1483; CHECK-NEXT:    %[[V2:.*]] = load float, float* %y
1484; CHECK-NEXT:    %[[S:.*]] = select i1 %flag, float %[[V1]], float %[[V2]]
1485; CHECK-NEXT:    ret float %[[S]]
1486entry:
1487  %x1 = bitcast float* %x to i32*
1488  %y1 = bitcast i32* %y to float*
1489  store i32 0, i32* %x1
1490  store i32 0, i32* %y
1491  ; Block forwarding by storing to %z which could alias either %x or %y.
1492  store i32 42, i32* %z
1493  %p = select i1 %flag, float* %x, float* %y1
1494  %v = load float, float* %p
1495  ret float %v
1496}
1497
1498define i32 @test80(i1 %flag) {
1499; Test that when we speculate the loads around the select they fold throug
1500; load->load folding and load->store folding.
1501; CHECK-LABEL: @test80(
1502; CHECK:         %[[X:.*]] = alloca i32
1503; CHECK-NEXT:    %[[Y:.*]] = alloca i32
1504; CHECK:         %[[V:.*]] = load i32, i32* %[[X]]
1505; CHECK-NEXT:    store i32 %[[V]], i32* %[[Y]]
1506; CHECK-NEXT:    ret i32 %[[V]]
1507entry:
1508  %x = alloca i32
1509  %y = alloca i32
1510  call void @scribble_on_i32(i32* %x)
1511  call void @scribble_on_i32(i32* %y)
1512  %tmp = load i32, i32* %x
1513  store i32 %tmp, i32* %y
1514  %p = select i1 %flag, i32* %x, i32* %y
1515  %v = load i32, i32* %p
1516  ret i32 %v
1517}
1518
1519define float @test81(i1 %flag) {
1520; Test that we can speculate the load around the select even though they use
1521; differently typed pointers.
1522; CHECK-LABEL: @test81(
1523; CHECK:         %[[X:.*]] = alloca i32
1524; CHECK-NEXT:    %[[Y:.*]] = alloca i32
1525; CHECK:         %[[V:.*]] = load i32, i32* %[[X]]
1526; CHECK-NEXT:    store i32 %[[V]], i32* %[[Y]]
1527; CHECK-NEXT:    %[[C:.*]] = bitcast i32 %[[V]] to float
1528; CHECK-NEXT:    ret float %[[C]]
1529entry:
1530  %x = alloca float
1531  %y = alloca i32
1532  %x1 = bitcast float* %x to i32*
1533  %y1 = bitcast i32* %y to float*
1534  call void @scribble_on_i32(i32* %x1)
1535  call void @scribble_on_i32(i32* %y)
1536  %tmp = load i32, i32* %x1
1537  store i32 %tmp, i32* %y
1538  %p = select i1 %flag, float* %x, float* %y1
1539  %v = load float, float* %p
1540  ret float %v
1541}
1542
1543define i32 @test82(i1 %flag) {
1544; Test that we can speculate the load around the select even though they use
1545; differently typed pointers.
1546; CHECK-LABEL: @test82(
1547; CHECK:         %[[X:.*]] = alloca float
1548; CHECK-NEXT:    %[[Y:.*]] = alloca i32
1549; CHECK-NEXT:    %[[X1:.*]] = bitcast float* %[[X]] to i32*
1550; CHECK-NEXT:    %[[Y1:.*]] = bitcast i32* %[[Y]] to float*
1551; CHECK:         %[[V:.*]] = load float, float* %[[X]]
1552; CHECK-NEXT:    store float %[[V]], float* %[[Y1]]
1553; CHECK-NEXT:    %[[C:.*]] = bitcast float %[[V]] to i32
1554; CHECK-NEXT:    ret i32 %[[C]]
1555entry:
1556  %x = alloca float
1557  %y = alloca i32
1558  %x1 = bitcast float* %x to i32*
1559  %y1 = bitcast i32* %y to float*
1560  call void @scribble_on_i32(i32* %x1)
1561  call void @scribble_on_i32(i32* %y)
1562  %tmp = load float, float* %x
1563  store float %tmp, float* %y1
1564  %p = select i1 %flag, i32* %x1, i32* %y
1565  %v = load i32, i32* %p
1566  ret i32 %v
1567}
1568
1569declare void @scribble_on_i64(i64*)
1570declare void @scribble_on_i128(i128*)
1571
1572define i8* @test83(i1 %flag) {
1573; Test that we can speculate the load around the select even though they use
1574; differently typed pointers and requires inttoptr casts.
1575; CHECK-LABEL: @test83(
1576; CHECK:         %[[X:.*]] = alloca i8*
1577; CHECK-NEXT:    %[[Y:.*]] = alloca i8*
1578; CHECK-DAG:     %[[X2:.*]] = bitcast i8** %[[X]] to i64*
1579; CHECK-DAG:     %[[Y2:.*]] = bitcast i8** %[[Y]] to i64*
1580; CHECK:         %[[V:.*]] = load i64, i64* %[[X2]]
1581; CHECK-NEXT:    store i64 %[[V]], i64* %[[Y2]]
1582; CHECK-NEXT:    %[[C:.*]] = inttoptr i64 %[[V]] to i8*
1583; CHECK-NEXT:    ret i8* %[[S]]
1584entry:
1585  %x = alloca i8*
1586  %y = alloca i64
1587  %x1 = bitcast i8** %x to i64*
1588  %y1 = bitcast i64* %y to i8**
1589  call void @scribble_on_i64(i64* %x1)
1590  call void @scribble_on_i64(i64* %y)
1591  %tmp = load i64, i64* %x1
1592  store i64 %tmp, i64* %y
1593  %p = select i1 %flag, i8** %x, i8** %y1
1594  %v = load i8*, i8** %p
1595  ret i8* %v
1596}
1597
1598define i64 @test84(i1 %flag) {
1599; Test that we can speculate the load around the select even though they use
1600; differently typed pointers and requires a ptrtoint cast.
1601; CHECK-LABEL: @test84(
1602; CHECK:         %[[X:.*]] = alloca i8*
1603; CHECK-NEXT:    %[[Y:.*]] = alloca i8*
1604; CHECK:         %[[V:.*]] = load i8*, i8** %[[X]]
1605; CHECK-NEXT:    store i8* %[[V]], i8** %[[Y]]
1606; CHECK-NEXT:    %[[C:.*]] = ptrtoint i8* %[[V]] to i64
1607; CHECK-NEXT:    ret i64 %[[C]]
1608entry:
1609  %x = alloca i8*
1610  %y = alloca i64
1611  %x1 = bitcast i8** %x to i64*
1612  %y1 = bitcast i64* %y to i8**
1613  call void @scribble_on_i64(i64* %x1)
1614  call void @scribble_on_i64(i64* %y)
1615  %tmp = load i8*, i8** %x
1616  store i8* %tmp, i8** %y1
1617  %p = select i1 %flag, i64* %x1, i64* %y
1618  %v = load i64, i64* %p
1619  ret i64 %v
1620}
1621
1622define i8* @test85(i1 %flag) {
1623; Test that we can't speculate the load around the select. The load of the
1624; pointer doesn't load all of the stored integer bits. We could fix this, but it
1625; would require endianness checks and other nastiness.
1626; CHECK-LABEL: @test85(
1627; CHECK:         %[[T:.*]] = load i128, i128*
1628; CHECK-NEXT:    store i128 %[[T]], i128*
1629; CHECK-NEXT:    %[[X:.*]] = load i8*, i8**
1630; CHECK-NEXT:    %[[Y:.*]] = load i8*, i8**
1631; CHECK-NEXT:    %[[V:.*]] = select i1 %flag, i8* %[[X]], i8* %[[Y]]
1632; CHECK-NEXT:    ret i8* %[[V]]
1633entry:
1634  %x = alloca [2 x i8*]
1635  %y = alloca i128
1636  %x1 = bitcast [2 x i8*]* %x to i8**
1637  %x2 = bitcast i8** %x1 to i128*
1638  %y1 = bitcast i128* %y to i8**
1639  call void @scribble_on_i128(i128* %x2)
1640  call void @scribble_on_i128(i128* %y)
1641  %tmp = load i128, i128* %x2
1642  store i128 %tmp, i128* %y
1643  %p = select i1 %flag, i8** %x1, i8** %y1
1644  %v = load i8*, i8** %p
1645  ret i8* %v
1646}
1647
1648define i128 @test86(i1 %flag) {
1649; Test that we can't speculate the load around the select when the integer size
1650; is larger than the pointer size. The store of the pointer doesn't store to all
1651; the bits of the integer.
1652;
1653; CHECK-LABEL: @test86(
1654; CHECK:         %[[T:.*]] = load i8*, i8**
1655; CHECK-NEXT:    store i8* %[[T]], i8**
1656; CHECK-NEXT:    %[[X:.*]] = load i128, i128*
1657; CHECK-NEXT:    %[[Y:.*]] = load i128, i128*
1658; CHECK-NEXT:    %[[V:.*]] = select i1 %flag, i128 %[[X]], i128 %[[Y]]
1659; CHECK-NEXT:    ret i128 %[[V]]
1660entry:
1661  %x = alloca [2 x i8*]
1662  %y = alloca i128
1663  %x1 = bitcast [2 x i8*]* %x to i8**
1664  %x2 = bitcast i8** %x1 to i128*
1665  %y1 = bitcast i128* %y to i8**
1666  call void @scribble_on_i128(i128* %x2)
1667  call void @scribble_on_i128(i128* %y)
1668  %tmp = load i8*, i8** %x1
1669  store i8* %tmp, i8** %y1
1670  %p = select i1 %flag, i128* %x2, i128* %y
1671  %v = load i128, i128* %p
1672  ret i128 %v
1673}
1674
1675define i32 @test_select_select0(i32 %a, i32 %r0, i32 %r1, i32 %v1, i32 %v2) {
1676  ; CHECK-LABEL: @test_select_select0(
1677  ; CHECK: %[[C0:.*]] = icmp sge i32 %a, %v1
1678  ; CHECK-NEXT: %[[C1:.*]] = icmp slt i32 %a, %v2
1679  ; CHECK-NEXT: %[[C:.*]] = and i1 %[[C1]], %[[C0]]
1680  ; CHECK-NEXT: %[[SEL:.*]] = select i1 %[[C]], i32 %r0, i32 %r1
1681  ; CHECK-NEXT: ret i32 %[[SEL]]
1682  %c0 = icmp sge i32 %a, %v1
1683  %s0 = select i1 %c0, i32 %r0, i32 %r1
1684  %c1 = icmp slt i32 %a, %v2
1685  %s1 = select i1 %c1, i32 %s0, i32 %r1
1686  ret i32 %s1
1687}
1688
1689define i32 @test_select_select1(i32 %a, i32 %r0, i32 %r1, i32 %v1, i32 %v2) {
1690  ; CHECK-LABEL: @test_select_select1(
1691  ; CHECK: %[[C0:.*]] = icmp sge i32 %a, %v1
1692  ; CHECK-NEXT: %[[C1:.*]] = icmp slt i32 %a, %v2
1693  ; CHECK-NEXT: %[[C:.*]] = or i1 %[[C1]], %[[C0]]
1694  ; CHECK-NEXT: %[[SEL:.*]] = select i1 %[[C]], i32 %r0, i32 %r1
1695  ; CHECK-NEXT: ret i32 %[[SEL]]
1696  %c0 = icmp sge i32 %a, %v1
1697  %s0 = select i1 %c0, i32 %r0, i32 %r1
1698  %c1 = icmp slt i32 %a, %v2
1699  %s1 = select i1 %c1, i32 %r0, i32 %s0
1700  ret i32 %s1
1701}
1702
1703define i32 @test_max_of_min(i32 %a) {
1704; MAX(MIN(%a, -1), -1) == -1
1705; CHECK-LABEL: @test_max_of_min(
1706; CHECK: ret i32 -1
1707  %not_a = xor i32 %a, -1
1708  %c0 = icmp sgt i32 %a, 0
1709  %s0 = select i1 %c0, i32 %not_a, i32 -1
1710  %c1 = icmp sgt i32 %s0, -1
1711  %s1 = select i1 %c1, i32 %s0, i32 -1
1712  ret i32 %s1
1713}
1714
1715
1716define i32 @PR23757(i32 %x) {
1717; CHECK-LABEL: @PR23757
1718; CHECK:      %[[cmp:.*]] = icmp eq i32 %x, 2147483647
1719; CHECK-NEXT: %[[add:.*]] = add nsw i32 %x, 1
1720; CHECK-NEXT: %[[sel:.*]] = select i1 %[[cmp]], i32 -2147483648, i32 %[[add]]
1721; CHECK-NEXT: ret i32 %[[sel]]
1722  %cmp = icmp eq i32 %x, 2147483647
1723  %add = add nsw i32 %x, 1
1724  %sel = select i1 %cmp, i32 -2147483648, i32 %add
1725  ret i32 %sel
1726}
1727
1728
1729define i32 @PR27137(i32 %a) {
1730; CHECK-LABEL: @PR27137(
1731; CHECK-NEXT:  %not_a = xor i32 %a, -1
1732; CHECK-NEXT:  %c0 = icmp slt i32 %a, 0
1733; CHECK-NEXT:  %s0 = select i1 %c0, i32 %not_a, i32 -1
1734; CHECK-NEXT:  %c1 = icmp sgt i32 %s0, -1
1735; CHECK-NEXT:  %s1 = select i1 %c1, i32 %s0, i32 -1
1736; CHECK-NEXT:  ret i32 %s1
1737
1738  %not_a = xor i32 %a, -1
1739  %c0 = icmp slt i32 %a, 0
1740  %s0 = select i1 %c0, i32 %not_a, i32 -1
1741  %c1 = icmp sgt i32 %s0, -1
1742  %s1 = select i1 %c1, i32 %s0, i32 -1
1743  ret i32 %s1
1744}
1745
1746define i32 @select_icmp_slt0_xor(i32 %x) {
1747; CHECK-LABEL: @select_icmp_slt0_xor(
1748; CHECK-NEXT:    [[TMP1:%.*]] = or i32 %x, -2147483648
1749; CHECK-NEXT:    ret i32 [[TMP1]]
1750;
1751  %cmp = icmp slt i32 %x, zeroinitializer
1752  %xor = xor i32 %x, 2147483648
1753  %x.xor = select i1 %cmp, i32 %x, i32 %xor
1754  ret i32 %x.xor
1755}
1756
1757define <2 x i32> @select_icmp_slt0_xor_vec(<2 x i32> %x) {
1758; CHECK-LABEL: @select_icmp_slt0_xor_vec(
1759; CHECK-NEXT:    [[TMP1:%.*]] = or <2 x i32> %x, <i32 -2147483648, i32 -2147483648>
1760; CHECK-NEXT:    ret <2 x i32> [[TMP1]]
1761;
1762  %cmp = icmp slt <2 x i32> %x, zeroinitializer
1763  %xor = xor <2 x i32> %x, <i32 2147483648, i32 2147483648>
1764  %x.xor = select <2 x i1> %cmp, <2 x i32> %x, <2 x i32> %xor
1765  ret <2 x i32> %x.xor
1766}
1767
1768; Make sure that undef elements of the select condition are translated into undef elements of the shuffle mask.
1769
1770define <4 x i32> @canonicalize_to_shuffle(<4 x i32> %a, <4 x i32> %b) {
1771; CHECK-LABEL: @canonicalize_to_shuffle(
1772; CHECK-NEXT:    [[SEL:%.*]] = shufflevector <4 x i32> %a, <4 x i32> %b, <4 x i32> <i32 0, i32 undef, i32 6, i32 undef>
1773; CHECK-NEXT:    ret <4 x i32> [[SEL]]
1774;
1775  %sel = select <4 x i1> <i1 true, i1 undef, i1 false, i1 undef>, <4 x i32> %a, <4 x i32> %b
1776  ret <4 x i32> %sel
1777}
1778
1779; Don't die or try if the condition mask is a constant expression or contains a constant expression.
1780
1781@g = global i32 0
1782
1783define <4 x i32> @cannot_canonicalize_to_shuffle1(<4 x i32> %a, <4 x i32> %b) {
1784; CHECK-LABEL: @cannot_canonicalize_to_shuffle1(
1785; 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
1786; CHECK-NEXT:    ret <4 x i32> [[SEL]]
1787;
1788  %sel = select <4 x i1> bitcast (i4 ptrtoint (i32* @g to i4) to <4 x i1>), <4 x i32> %a, <4 x i32> %b
1789  ret <4 x i32> %sel
1790}
1791
1792define <4 x i32> @cannot_canonicalize_to_shuffle2(<4 x i32> %a, <4 x i32> %b) {
1793; CHECK-LABEL: @cannot_canonicalize_to_shuffle2(
1794; 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
1795; CHECK-NEXT:    ret <4 x i32> [[SEL]]
1796;
1797  %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
1798  ret <4 x i32> %sel
1799}
1800
1801