Teach the load analysis to allow finding available values which require
[oota-llvm.git] / test / Transforms / InstCombine / select.ll
1 ; RUN: opt < %s -instcombine -S | FileCheck %s
2 ; This test makes sure that these instructions are properly eliminated.
3 ; PR1822
4
5 target datalayout = "e-p:64:64-p1:16:16-p2:32:32:32-p3:64:64:64"
6
7 define i32 @test1(i32 %A, i32 %B) {
8         %C = select i1 false, i32 %A, i32 %B            
9         ret i32 %C
10 ; CHECK-LABEL: @test1(
11 ; CHECK: ret i32 %B
12 }
13
14 define i32 @test2(i32 %A, i32 %B) {
15         %C = select i1 true, i32 %A, i32 %B             
16         ret i32 %C
17 ; CHECK-LABEL: @test2(
18 ; CHECK: ret i32 %A
19 }
20
21
22 define i32 @test3(i1 %C, i32 %I) {
23         ; V = I
24         %V = select i1 %C, i32 %I, i32 %I               
25         ret i32 %V
26 ; CHECK-LABEL: @test3(
27 ; CHECK: ret i32 %I
28 }
29
30 define i1 @test4(i1 %C) {
31         ; V = C
32         %V = select i1 %C, i1 true, i1 false            
33         ret i1 %V
34 ; CHECK-LABEL: @test4(
35 ; CHECK: ret i1 %C
36 }
37
38 define i1 @test5(i1 %C) {
39         ; V = !C
40         %V = select i1 %C, i1 false, i1 true            
41         ret i1 %V
42 ; CHECK-LABEL: @test5(
43 ; CHECK: xor i1 %C, true
44 ; CHECK: ret i1
45 }
46
47 define i32 @test6(i1 %C) { 
48         ; V = cast C to int
49         %V = select i1 %C, i32 1, i32 0         
50         ret i32 %V
51 ; CHECK-LABEL: @test6(
52 ; CHECK: %V = zext i1 %C to i32
53 ; CHECK: ret i32 %V
54 }
55
56 define i1 @test7(i1 %C, i1 %X) {
57         ; R = or C, X       
58         %R = select i1 %C, i1 true, i1 %X               
59         ret i1 %R
60 ; CHECK-LABEL: @test7(
61 ; CHECK: %R = or i1 %C, %X
62 ; CHECK: ret i1 %R
63 }
64
65 define i1 @test8(i1 %C, i1 %X) {
66         ; R = and C, X
67         %R = select i1 %C, i1 %X, i1 false              
68         ret i1 %R
69 ; CHECK-LABEL: @test8(
70 ; CHECK: %R = and i1 %C, %X
71 ; CHECK: ret i1 %R
72 }
73
74 define i1 @test9(i1 %C, i1 %X) {
75         ; R = and !C, X
76         %R = select i1 %C, i1 false, i1 %X              
77         ret i1 %R
78 ; CHECK-LABEL: @test9(
79 ; CHECK: xor i1 %C, true
80 ; CHECK: %R = and i1
81 ; CHECK: ret i1 %R
82 }
83
84 define i1 @test10(i1 %C, i1 %X) {
85         ; R = or !C, X
86         %R = select i1 %C, i1 %X, i1 true               
87         ret i1 %R
88 ; CHECK-LABEL: @test10(
89 ; CHECK: xor i1 %C, true
90 ; CHECK: %R = or i1
91 ; CHECK: ret i1 %R
92 }
93
94 define i32 @test11(i32 %a) {
95         %C = icmp eq i32 %a, 0          
96         %R = select i1 %C, i32 0, i32 1         
97         ret i32 %R
98 ; CHECK-LABEL: @test11(
99 ; CHECK: icmp ne i32 %a, 0
100 ; CHECK: %R = zext i1
101 ; CHECK: ret i32 %R
102 }
103
104 define i32 @test12(i1 %cond, i32 %a) {
105         %b = or i32 %a, 1               
106         %c = select i1 %cond, i32 %b, i32 %a            
107         ret i32 %c
108 ; CHECK-LABEL: @test12(
109 ; CHECK: %b = zext i1 %cond to i32
110 ; CHECK: %c = or i32 %b, %a
111 ; CHECK: ret i32 %c
112 }
113
114 define i32 @test12a(i1 %cond, i32 %a) {
115         %b = ashr i32 %a, 1             
116         %c = select i1 %cond, i32 %b, i32 %a            
117         ret i32 %c
118 ; CHECK-LABEL: @test12a(
119 ; CHECK: %b = zext i1 %cond to i32
120 ; CHECK: %c = ashr i32 %a, %b
121 ; CHECK: ret i32 %c
122 }
123
124 define i32 @test12b(i1 %cond, i32 %a) {
125         %b = ashr i32 %a, 1             
126         %c = select i1 %cond, i32 %a, i32 %b            
127         ret i32 %c
128 ; CHECK-LABEL: @test12b(
129 ; CHECK: zext i1 %cond to i32
130 ; CHECK: %b = xor i32
131 ; CHECK: %c = ashr i32 %a, %b
132 ; CHECK: ret i32 %c
133 }
134
135 define i32 @test13(i32 %a, i32 %b) {
136         %C = icmp eq i32 %a, %b         
137         %V = select i1 %C, i32 %a, i32 %b               
138         ret i32 %V
139 ; CHECK-LABEL: @test13(
140 ; CHECK: ret i32 %b
141 }
142
143 define i32 @test13a(i32 %a, i32 %b) {
144         %C = icmp ne i32 %a, %b         
145         %V = select i1 %C, i32 %a, i32 %b               
146         ret i32 %V
147 ; CHECK-LABEL: @test13a(
148 ; CHECK: ret i32 %a
149 }
150
151 define i32 @test13b(i32 %a, i32 %b) {
152         %C = icmp eq i32 %a, %b         
153         %V = select i1 %C, i32 %b, i32 %a               
154         ret i32 %V
155 ; CHECK-LABEL: @test13b(
156 ; CHECK: ret i32 %a
157 }
158
159 define i1 @test14a(i1 %C, i32 %X) {
160         %V = select i1 %C, i32 %X, i32 0                
161         ; (X < 1) | !C
162         %R = icmp slt i32 %V, 1         
163         ret i1 %R
164 ; CHECK-LABEL: @test14a(
165 ; CHECK: icmp slt i32 %X, 1
166 ; CHECK: xor i1 %C, true
167 ; CHECK: or i1
168 ; CHECK: ret i1 %R
169 }
170
171 define i1 @test14b(i1 %C, i32 %X) {
172         %V = select i1 %C, i32 0, i32 %X                
173         ; (X < 1) | C
174         %R = icmp slt i32 %V, 1         
175         ret i1 %R
176 ; CHECK-LABEL: @test14b(
177 ; CHECK: icmp slt i32 %X, 1
178 ; CHECK: or i1
179 ; CHECK: ret i1 %R
180 }
181
182 ;; Code sequence for (X & 16) ? 16 : 0
183 define i32 @test15a(i32 %X) {
184         %t1 = and i32 %X, 16            
185         %t2 = icmp eq i32 %t1, 0                
186         %t3 = select i1 %t2, i32 0, i32 16              
187         ret i32 %t3
188 ; CHECK-LABEL: @test15a(
189 ; CHECK: %t1 = and i32 %X, 16
190 ; CHECK: ret i32 %t1
191 }
192
193 ;; Code sequence for (X & 32) ? 0 : 24
194 define i32 @test15b(i32 %X) {
195         %t1 = and i32 %X, 32            
196         %t2 = icmp eq i32 %t1, 0                
197         %t3 = select i1 %t2, i32 32, i32 0              
198         ret i32 %t3
199 ; CHECK-LABEL: @test15b(
200 ; CHECK: %t1 = and i32 %X, 32
201 ; CHECK: xor i32 %t1, 32
202 ; CHECK: ret i32
203 }
204
205 ;; Alternate code sequence for (X & 16) ? 16 : 0
206 define i32 @test15c(i32 %X) {
207         %t1 = and i32 %X, 16            
208         %t2 = icmp eq i32 %t1, 16               
209         %t3 = select i1 %t2, i32 16, i32 0              
210         ret i32 %t3
211 ; CHECK-LABEL: @test15c(
212 ; CHECK: %t1 = and i32 %X, 16
213 ; CHECK: ret i32 %t1
214 }
215
216 ;; Alternate code sequence for (X & 16) ? 16 : 0
217 define i32 @test15d(i32 %X) {
218         %t1 = and i32 %X, 16            
219         %t2 = icmp ne i32 %t1, 0                
220         %t3 = select i1 %t2, i32 16, i32 0              
221         ret i32 %t3
222 ; CHECK-LABEL: @test15d(
223 ; CHECK: %t1 = and i32 %X, 16
224 ; CHECK: ret i32 %t1
225 }
226
227 ;; (a & 128) ? 256 : 0
228 define i32 @test15e(i32 %X) {
229         %t1 = and i32 %X, 128
230         %t2 = icmp ne i32 %t1, 0
231         %t3 = select i1 %t2, i32 256, i32 0
232         ret i32 %t3
233 ; CHECK-LABEL: @test15e(
234 ; CHECK: %t1 = shl i32 %X, 1
235 ; CHECK: and i32 %t1, 256
236 ; CHECK: ret i32
237 }
238
239 ;; (a & 128) ? 0 : 256
240 define i32 @test15f(i32 %X) {
241         %t1 = and i32 %X, 128
242         %t2 = icmp ne i32 %t1, 0
243         %t3 = select i1 %t2, i32 0, i32 256
244         ret i32 %t3
245 ; CHECK-LABEL: @test15f(
246 ; CHECK: %t1 = shl i32 %X, 1
247 ; CHECK: and i32 %t1, 256
248 ; CHECK: xor i32 %{{.*}}, 256
249 ; CHECK: ret i32
250 }
251
252 ;; (a & 8) ? -1 : -9
253 define i32 @test15g(i32 %X) {
254         %t1 = and i32 %X, 8
255         %t2 = icmp ne i32 %t1, 0
256         %t3 = select i1 %t2, i32 -1, i32 -9
257         ret i32 %t3
258 ; CHECK-LABEL: @test15g(
259 ; CHECK-NEXT: %1 = or i32 %X, -9
260 ; CHECK-NEXT: ret i32 %1
261 }
262
263 ;; (a & 8) ? -9 : -1
264 define i32 @test15h(i32 %X) {
265         %t1 = and i32 %X, 8
266         %t2 = icmp ne i32 %t1, 0
267         %t3 = select i1 %t2, i32 -9, i32 -1
268         ret i32 %t3
269 ; CHECK-LABEL: @test15h(
270 ; CHECK-NEXT: %1 = or i32 %X, -9
271 ; CHECK-NEXT: %2 = xor i32 %1, 8
272 ; CHECK-NEXT: ret i32 %2
273 }
274
275 ;; (a & 2) ? 577 : 1089
276 define i32 @test15i(i32 %X) {
277         %t1 = and i32 %X, 2
278         %t2 = icmp ne i32 %t1, 0
279         %t3 = select i1 %t2, i32 577, i32 1089
280         ret i32 %t3
281 ; CHECK-LABEL: @test15i(
282 ; CHECK-NEXT: %t1 = shl i32 %X, 8
283 ; CHECK-NEXT: %1 = and i32 %t1, 512
284 ; CHECK-NEXT: %2 = xor i32 %1, 512
285 ; CHECK-NEXT: %3 = add nuw nsw i32 %2, 577
286 ; CHECK-NEXT: ret i32 %3
287 }
288
289 ;; (a & 2) ? 1089 : 577
290 define i32 @test15j(i32 %X) {
291         %t1 = and i32 %X, 2
292         %t2 = icmp ne i32 %t1, 0
293         %t3 = select i1 %t2, i32 1089, i32 577
294         ret i32 %t3
295 ; CHECK-LABEL: @test15j(
296 ; CHECK-NEXT: %t1 = shl i32 %X, 8
297 ; CHECK-NEXT: %1 = and i32 %t1, 512
298 ; CHECK-NEXT: %2 = add nuw nsw i32 %1, 577
299 ; CHECK-NEXT: ret i32 %2
300 }
301
302 define i32 @test16(i1 %C, i32* %P) {
303         %P2 = select i1 %C, i32* %P, i32* null          
304         %V = load i32* %P2              
305         ret i32 %V
306 ; CHECK-LABEL: @test16(
307 ; CHECK-NEXT: %V = load i32* %P
308 ; CHECK: ret i32 %V
309 }
310
311 define i1 @test17(i32* %X, i1 %C) {
312         %R = select i1 %C, i32* %X, i32* null           
313         %RV = icmp eq i32* %R, null             
314         ret i1 %RV
315 ; CHECK-LABEL: @test17(
316 ; CHECK: icmp eq i32* %X, null
317 ; CHECK: xor i1 %C, true
318 ; CHECK: %RV = or i1
319 ; CHECK: ret i1 %RV
320 }
321
322 define i32 @test18(i32 %X, i32 %Y, i1 %C) {
323         %R = select i1 %C, i32 %X, i32 0                
324         %V = sdiv i32 %Y, %R            
325         ret i32 %V
326 ; CHECK-LABEL: @test18(
327 ; CHECK: %V = sdiv i32 %Y, %X
328 ; CHECK: ret i32 %V
329 }
330
331 define i32 @test19(i32 %x) {
332         %tmp = icmp ugt i32 %x, 2147483647              
333         %retval = select i1 %tmp, i32 -1, i32 0         
334         ret i32 %retval
335 ; CHECK-LABEL: @test19(
336 ; CHECK-NEXT: ashr i32 %x, 31
337 ; CHECK-NEXT: ret i32 
338 }
339
340 define i32 @test20(i32 %x) {
341         %tmp = icmp slt i32 %x, 0               
342         %retval = select i1 %tmp, i32 -1, i32 0         
343         ret i32 %retval
344 ; CHECK-LABEL: @test20(
345 ; CHECK-NEXT: ashr i32 %x, 31
346 ; CHECK-NEXT: ret i32 
347 }
348
349 define i64 @test21(i32 %x) {
350         %tmp = icmp slt i32 %x, 0               
351         %retval = select i1 %tmp, i64 -1, i64 0         
352         ret i64 %retval
353 ; CHECK-LABEL: @test21(
354 ; CHECK-NEXT: ashr i32 %x, 31
355 ; CHECK-NEXT: sext i32 
356 ; CHECK-NEXT: ret i64
357 }
358
359 define i16 @test22(i32 %x) {
360         %tmp = icmp slt i32 %x, 0               
361         %retval = select i1 %tmp, i16 -1, i16 0         
362         ret i16 %retval
363 ; CHECK-LABEL: @test22(
364 ; CHECK-NEXT: ashr i32 %x, 31
365 ; CHECK-NEXT: trunc i32 
366 ; CHECK-NEXT: ret i16
367 }
368
369 define i1 @test23(i1 %a, i1 %b) {
370         %c = select i1 %a, i1 %b, i1 %a         
371         ret i1 %c
372 ; CHECK-LABEL: @test23(
373 ; CHECK-NEXT: %c = and i1 %a, %b
374 ; CHECK-NEXT: ret i1 %c
375 }
376
377 define i1 @test24(i1 %a, i1 %b) {
378         %c = select i1 %a, i1 %a, i1 %b         
379         ret i1 %c
380 ; CHECK-LABEL: @test24(
381 ; CHECK-NEXT: %c = or i1 %a, %b
382 ; CHECK-NEXT: ret i1 %c
383 }
384
385 define i32 @test25(i1 %c)  {
386 entry:
387   br i1 %c, label %jump, label %ret
388 jump:
389   br label %ret 
390 ret:
391   %a = phi i1 [true, %jump], [false, %entry]
392   %b = select i1 %a, i32 10, i32 20
393   ret i32 %b
394 ; CHECK-LABEL: @test25(
395 ; CHECK: %a = phi i32 [ 10, %jump ], [ 20, %entry ]
396 ; CHECK-NEXT: ret i32 %a
397 }
398
399 define i32 @test26(i1 %cond)  {
400 entry:
401   br i1 %cond, label %jump, label %ret
402 jump:
403   %c = or i1 false, false
404   br label %ret 
405 ret:
406   %a = phi i1 [true, %jump], [%c, %entry]
407   %b = select i1 %a, i32 10, i32 20
408   ret i32 %b
409 ; CHECK-LABEL: @test26(
410 ; CHECK: %a = phi i32 [ 10, %jump ], [ 20, %entry ]
411 ; CHECK-NEXT: ret i32 %a
412 }
413
414 define i32 @test27(i1 %c, i32 %A, i32 %B)  {
415 entry:
416   br i1 %c, label %jump, label %ret
417 jump:
418   br label %ret 
419 ret:
420   %a = phi i1 [true, %jump], [false, %entry]
421   %b = select i1 %a, i32 %A, i32 %B
422   ret i32 %b
423 ; CHECK-LABEL: @test27(
424 ; CHECK: %a = phi i32 [ %A, %jump ], [ %B, %entry ]
425 ; CHECK-NEXT: ret i32 %a
426 }
427
428 define i32 @test28(i1 %cond, i32 %A, i32 %B)  {
429 entry:
430   br i1 %cond, label %jump, label %ret
431 jump:
432   br label %ret 
433 ret:
434   %c = phi i32 [%A, %jump], [%B, %entry]
435   %a = phi i1 [true, %jump], [false, %entry]
436   %b = select i1 %a, i32 %A, i32 %c
437   ret i32 %b
438 ; CHECK-LABEL: @test28(
439 ; CHECK: %a = phi i32 [ %A, %jump ], [ %B, %entry ]
440 ; CHECK-NEXT: ret i32 %a
441 }
442
443 define i32 @test29(i1 %cond, i32 %A, i32 %B)  {
444 entry:
445   br i1 %cond, label %jump, label %ret
446 jump:
447   br label %ret 
448 ret:
449   %c = phi i32 [%A, %jump], [%B, %entry]
450   %a = phi i1 [true, %jump], [false, %entry]
451   br label %next
452   
453 next:
454   %b = select i1 %a, i32 %A, i32 %c
455   ret i32 %b
456 ; CHECK-LABEL: @test29(
457 ; CHECK: %a = phi i32 [ %A, %jump ], [ %B, %entry ]
458 ; CHECK: ret i32 %a
459 }
460
461
462 ; SMAX(SMAX(x, y), x) -> SMAX(x, y)
463 define i32 @test30(i32 %x, i32 %y) {
464   %cmp = icmp sgt i32 %x, %y
465   %cond = select i1 %cmp, i32 %x, i32 %y
466   
467   %cmp5 = icmp sgt i32 %cond, %x
468   %retval = select i1 %cmp5, i32 %cond, i32 %x
469   ret i32 %retval
470 ; CHECK-LABEL: @test30(
471 ; CHECK: ret i32 %cond
472 }
473
474 ; UMAX(UMAX(x, y), x) -> UMAX(x, y)
475 define i32 @test31(i32 %x, i32 %y) {
476   %cmp = icmp ugt i32 %x, %y 
477   %cond = select i1 %cmp, i32 %x, i32 %y
478   %cmp5 = icmp ugt i32 %cond, %x
479   %retval = select i1 %cmp5, i32 %cond, i32 %x
480   ret i32 %retval
481 ; CHECK-LABEL: @test31(
482 ; CHECK: ret i32 %cond
483 }
484
485 ; SMIN(SMIN(x, y), x) -> SMIN(x, y)
486 define i32 @test32(i32 %x, i32 %y) {
487   %cmp = icmp sgt i32 %x, %y
488   %cond = select i1 %cmp, i32 %y, i32 %x
489   %cmp5 = icmp sgt i32 %cond, %x
490   %retval = select i1 %cmp5, i32 %x, i32 %cond
491   ret i32 %retval
492 ; CHECK-LABEL: @test32(
493 ; CHECK: ret i32 %cond
494 }
495
496 ; MAX(MIN(x, y), x) -> x
497 define i32 @test33(i32 %x, i32 %y) {
498   %cmp = icmp sgt i32 %x, %y
499   %cond = select i1 %cmp, i32 %y, i32 %x
500   %cmp5 = icmp sgt i32 %cond, %x
501   %retval = select i1 %cmp5, i32 %cond, i32 %x
502   ret i32 %retval
503 ; CHECK-LABEL: @test33(
504 ; CHECK: ret i32 %x
505 }
506
507 ; MIN(MAX(x, y), x) -> x
508 define i32 @test34(i32 %x, i32 %y) {
509   %cmp = icmp sgt i32 %x, %y
510   %cond = select i1 %cmp, i32 %x, i32 %y
511   %cmp5 = icmp sgt i32 %cond, %x
512   %retval = select i1 %cmp5, i32 %x, i32 %cond
513   ret i32 %retval
514 ; CHECK-LABEL: @test34(
515 ; CHECK: ret i32 %x
516 }
517
518 define i32 @test35(i32 %x) {
519   %cmp = icmp sge i32 %x, 0
520   %cond = select i1 %cmp, i32 60, i32 100
521   ret i32 %cond
522 ; CHECK-LABEL: @test35(
523 ; CHECK: ashr i32 %x, 31
524 ; CHECK: and i32 {{.*}}, 40
525 ; CHECK: add nuw nsw i32 {{.*}}, 60
526 ; CHECK: ret
527 }
528
529 define i32 @test36(i32 %x) {
530   %cmp = icmp slt i32 %x, 0
531   %cond = select i1 %cmp, i32 60, i32 100
532   ret i32 %cond
533 ; CHECK-LABEL: @test36(
534 ; CHECK: ashr i32 %x, 31
535 ; CHECK: and i32 {{.*}}, -40
536 ; CHECK: add nsw i32 {{.*}}, 100
537 ; CHECK: ret
538 }
539
540 define i32 @test37(i32 %x) {
541   %cmp = icmp sgt i32 %x, -1
542   %cond = select i1 %cmp, i32 1, i32 -1
543   ret i32 %cond
544 ; CHECK-LABEL: @test37(
545 ; CHECK: ashr i32 %x, 31
546 ; CHECK: or i32 {{.*}}, 1
547 ; CHECK: ret
548 }
549
550 define i1 @test38(i1 %cond) {
551   %zero = alloca i32
552   %one = alloca i32
553   %ptr = select i1 %cond, i32* %zero, i32* %one
554   %isnull = icmp eq i32* %ptr, null
555   ret i1 %isnull
556 ; CHECK-LABEL: @test38(
557 ; CHECK: ret i1 false
558 }
559
560 define i1 @test39(i1 %cond, double %x) {
561   %s = select i1 %cond, double %x, double 0x7FF0000000000000 ; RHS = +infty
562   %cmp = fcmp ule double %x, %s
563   ret i1 %cmp
564 ; CHECK-LABEL: @test39(
565 ; CHECK: ret i1 true
566 }
567
568 define i1 @test40(i1 %cond) {
569   %a = alloca i32
570   %b = alloca i32
571   %c = alloca i32
572   %s = select i1 %cond, i32* %a, i32* %b
573   %r = icmp eq i32* %s, %c
574   ret i1 %r
575 ; CHECK-LABEL: @test40(
576 ; CHECK: ret i1 false
577 }
578
579 define i32 @test41(i1 %cond, i32 %x, i32 %y) {
580   %z = and i32 %x, %y
581   %s = select i1 %cond, i32 %y, i32 %z
582   %r = and i32 %x, %s
583   ret i32 %r
584 ; CHECK-LABEL: @test41(
585 ; CHECK-NEXT: and i32 %x, %y
586 ; CHECK-NEXT: ret i32
587 }
588
589 define i32 @test42(i32 %x, i32 %y) {
590   %b = add i32 %y, -1
591   %cond = icmp eq i32 %x, 0
592   %c = select i1 %cond, i32 %b, i32 %y
593   ret i32 %c
594 ; CHECK-LABEL: @test42(
595 ; CHECK-NEXT: %cond = icmp eq i32 %x, 0
596 ; CHECK-NEXT: %b = sext i1 %cond to i32
597 ; CHECK-NEXT: %c = add i32 %b, %y
598 ; CHECK-NEXT: ret i32 %c
599 }
600
601 define i64 @test43(i32 %a) nounwind {
602         %a_ext = sext i32 %a to i64
603         %is_a_nonnegative = icmp sgt i32 %a, -1
604         %max = select i1 %is_a_nonnegative, i64 %a_ext, i64 0
605         ret i64 %max
606 ; CHECK-LABEL: @test43(
607 ; CHECK-NEXT: %a_ext = sext i32 %a to i64
608 ; CHECK-NEXT: %is_a_nonnegative = icmp slt i64 %a_ext, 0
609 ; CHECK-NEXT: %max = select i1 %is_a_nonnegative, i64 0, i64 %a_ext
610 ; CHECK-NEXT: ret i64 %max
611 }
612
613 define i64 @test44(i32 %a) nounwind {
614         %a_ext = sext i32 %a to i64
615         %is_a_nonpositive = icmp slt i32 %a, 1
616         %min = select i1 %is_a_nonpositive, i64 %a_ext, i64 0
617         ret i64 %min
618 ; CHECK-LABEL: @test44(
619 ; CHECK-NEXT: %a_ext = sext i32 %a to i64
620 ; CHECK-NEXT: %is_a_nonpositive = icmp sgt i64 %a_ext, 0
621 ; CHECK-NEXT: %min = select i1 %is_a_nonpositive, i64 0, i64 %a_ext
622 ; CHECK-NEXT: ret i64 %min
623 }
624 define i64 @test45(i32 %a) nounwind {
625         %a_ext = zext i32 %a to i64
626         %is_a_nonnegative = icmp ugt i32 %a, 2
627         %max = select i1 %is_a_nonnegative, i64 %a_ext, i64 3
628         ret i64 %max
629 ; CHECK-LABEL: @test45(
630 ; CHECK-NEXT: %a_ext = zext i32 %a to i64
631 ; CHECK-NEXT: %is_a_nonnegative = icmp ult i64 %a_ext, 3
632 ; CHECK-NEXT: %max = select i1 %is_a_nonnegative, i64 3, i64 %a_ext
633 ; CHECK-NEXT: ret i64 %max
634 }
635
636 define i64 @test46(i32 %a) nounwind {
637         %a_ext = zext i32 %a to i64
638         %is_a_nonpositive = icmp ult i32 %a, 3
639         %min = select i1 %is_a_nonpositive, i64 %a_ext, i64 2
640         ret i64 %min
641 ; CHECK-LABEL: @test46(
642 ; CHECK-NEXT: %a_ext = zext i32 %a to i64
643 ; CHECK-NEXT: %is_a_nonpositive = icmp ugt i64 %a_ext, 2
644 ; CHECK-NEXT: %min = select i1 %is_a_nonpositive, i64 2, i64 %a_ext
645 ; CHECK-NEXT: ret i64 %min
646 }
647 define i64 @test47(i32 %a) nounwind {
648         %a_ext = sext i32 %a to i64
649         %is_a_nonnegative = icmp ugt i32 %a, 2
650         %max = select i1 %is_a_nonnegative, i64 %a_ext, i64 3
651         ret i64 %max
652 ; CHECK-LABEL: @test47(
653 ; CHECK-NEXT: %a_ext = sext i32 %a to i64
654 ; CHECK-NEXT: %is_a_nonnegative = icmp ult i64 %a_ext, 3
655 ; CHECK-NEXT: %max = select i1 %is_a_nonnegative, i64 3, i64 %a_ext
656 ; CHECK-NEXT: ret i64 %max
657 }
658
659 define i64 @test48(i32 %a) nounwind {
660         %a_ext = sext i32 %a to i64
661         %is_a_nonpositive = icmp ult i32 %a, 3
662         %min = select i1 %is_a_nonpositive, i64 %a_ext, i64 2
663         ret i64 %min
664 ; CHECK-LABEL: @test48(
665 ; CHECK-NEXT: %a_ext = sext i32 %a to i64
666 ; CHECK-NEXT: %is_a_nonpositive = icmp ugt i64 %a_ext, 2
667 ; CHECK-NEXT: %min = select i1 %is_a_nonpositive, i64 2, i64 %a_ext
668 ; CHECK-NEXT: ret i64 %min
669 }
670
671 define i64 @test49(i32 %a) nounwind {
672         %a_ext = sext i32 %a to i64
673         %is_a_nonpositive = icmp ult i32 %a, 3
674         %min = select i1 %is_a_nonpositive, i64 2, i64 %a_ext
675         ret i64 %min
676 ; CHECK-LABEL: @test49(
677 ; CHECK-NEXT: %a_ext = sext i32 %a to i64
678 ; CHECK-NEXT: %is_a_nonpositive = icmp ugt i64 %a_ext, 2
679 ; CHECK-NEXT: %min = select i1 %is_a_nonpositive, i64 %a_ext, i64 2
680 ; CHECK-NEXT: ret i64 %min
681 }
682 define i64 @test50(i32 %a) nounwind {
683         %is_a_nonpositive = icmp ult i32 %a, 3
684         %a_ext = sext i32 %a to i64
685         %min = select i1 %is_a_nonpositive, i64 2, i64 %a_ext
686         ret i64 %min
687 ; CHECK-LABEL: @test50(
688 ; CHECK-NEXT: %a_ext = sext i32 %a to i64
689 ; CHECK-NEXT: %is_a_nonpositive = icmp ugt i64 %a_ext, 2
690 ; CHECK-NEXT: %min = select i1 %is_a_nonpositive, i64 %a_ext, i64 2
691 ; CHECK-NEXT: ret i64 %min
692 }
693
694 ; PR8994
695
696 ; This select instruction can't be eliminated because trying to do so would
697 ; change the number of vector elements. This used to assert.
698 define i48 @test51(<3 x i1> %icmp, <3 x i16> %tmp) {
699 ; CHECK-LABEL: @test51(
700   %select = select <3 x i1> %icmp, <3 x i16> zeroinitializer, <3 x i16> %tmp
701 ; CHECK: select <3 x i1>
702   %tmp2 = bitcast <3 x i16> %select to i48
703   ret i48 %tmp2
704 }
705
706 ; PR8575
707
708 define i32 @test52(i32 %n, i32 %m) nounwind {
709 ; CHECK-LABEL: @test52(
710   %cmp = icmp sgt i32 %n, %m
711   %. = select i1 %cmp, i32 1, i32 3
712   %add = add nsw i32 %., 3
713   %storemerge = select i1 %cmp, i32 %., i32 %add
714 ; CHECK: select i1 %cmp, i32 1, i32 6
715   ret i32 %storemerge
716 }
717
718 ; PR9454
719 define i32 @test53(i32 %x) nounwind {
720   %and = and i32 %x, 2
721   %cmp = icmp eq i32 %and, %x
722   %sel = select i1 %cmp, i32 2, i32 1
723   ret i32 %sel
724 ; CHECK-LABEL: @test53(
725 ; CHECK: select i1 %cmp
726 ; CHECK: ret
727 }
728
729 define i32 @test54(i32 %X, i32 %Y) {
730   %A = ashr exact i32 %X, %Y
731   %B = icmp eq i32 %A, 0
732   %C = select i1 %B, i32 %A, i32 1
733   ret i32 %C
734 ; CHECK-LABEL: @test54(
735 ; CHECK-NOT: ashr
736 ; CHECK-NOT: select
737 ; CHECK: icmp ne i32 %X, 0
738 ; CHECK: zext 
739 ; CHECK: ret
740 }
741
742 define i1 @test55(i1 %X, i32 %Y, i32 %Z) {
743   %A = ashr exact i32 %Y, %Z
744   %B = select i1 %X, i32 %Y, i32 %A
745   %C = icmp eq i32 %B, 0
746   ret i1 %C
747 ; CHECK-LABEL: @test55(
748 ; CHECK-NOT: ashr
749 ; CHECK-NOT: select
750 ; CHECK: icmp eq
751 ; CHECK: ret i1
752 }
753
754 define i32 @test56(i16 %x) nounwind {
755   %tobool = icmp eq i16 %x, 0
756   %conv = zext i16 %x to i32
757   %cond = select i1 %tobool, i32 0, i32 %conv
758   ret i32 %cond
759 ; CHECK-LABEL: @test56(
760 ; CHECK-NEXT: zext
761 ; CHECK-NEXT: ret
762 }
763
764 define i32 @test57(i32 %x, i32 %y) nounwind {
765   %and = and i32 %x, %y
766   %tobool = icmp eq i32 %x, 0
767   %.and = select i1 %tobool, i32 0, i32 %and
768   ret i32 %.and
769 ; CHECK-LABEL: @test57(
770 ; CHECK-NEXT: and i32 %x, %y
771 ; CHECK-NEXT: ret
772 }
773
774 define i32 @test58(i16 %x) nounwind {
775   %tobool = icmp ne i16 %x, 1
776   %conv = zext i16 %x to i32
777   %cond = select i1 %tobool, i32 %conv, i32 1
778   ret i32 %cond
779 ; CHECK-LABEL: @test58(
780 ; CHECK-NEXT: zext
781 ; CHECK-NEXT: ret
782 }
783
784 define i32 @test59(i32 %x, i32 %y) nounwind {
785   %and = and i32 %x, %y
786   %tobool = icmp ne i32 %x, %y
787   %.and = select i1 %tobool, i32 %and, i32 %y
788   ret i32 %.and
789 ; CHECK-LABEL: @test59(
790 ; CHECK-NEXT: and i32 %x, %y
791 ; CHECK-NEXT: ret
792 }
793
794 define i1 @test60(i32 %x, i1* %y) nounwind {
795   %cmp = icmp eq i32 %x, 0
796   %load = load i1* %y, align 1
797   %cmp1 = icmp slt i32 %x, 1
798   %sel = select i1 %cmp, i1 %load, i1 %cmp1
799   ret i1 %sel
800 ; CHECK-LABEL: @test60(
801 ; CHECK: select
802 }
803
804 @glbl = constant i32 10
805 define i32 @test61(i32* %ptr) {
806   %A = load i32* %ptr
807   %B = icmp eq i32* %ptr, @glbl
808   %C = select i1 %B, i32 %A, i32 10
809   ret i32 %C
810 ; CHECK-LABEL: @test61(
811 ; CHECK: ret i32 10
812 }
813
814 define i1 @test62(i1 %A, i1 %B) {
815         %not = xor i1 %A, true
816         %C = select i1 %A, i1 %not, i1 %B             
817         ret i1 %C
818 ; CHECK-LABEL: @test62(
819 ; CHECK: %not = xor i1 %A, true
820 ; CHECK: %C = and i1 %not, %B
821 ; CHECK: ret i1 %C
822 }
823
824 define i1 @test63(i1 %A, i1 %B) {
825         %not = xor i1 %A, true
826         %C = select i1 %A, i1 %B, i1 %not         
827         ret i1 %C
828 ; CHECK-LABEL: @test63(
829 ; CHECK: %not = xor i1 %A, true
830 ; CHECK: %C = or i1 %B, %not
831 ; CHECK: ret i1 %C
832 }
833
834 ; PR14131
835 define void @test64(i32 %p, i16 %b) noreturn nounwind {
836 entry:
837   %p.addr.0.insert.mask = and i32 %p, -65536
838   %conv2 = and i32 %p, 65535
839   br i1 undef, label %lor.rhs, label %lor.end
840
841 lor.rhs:
842   %p.addr.0.extract.trunc = trunc i32 %p.addr.0.insert.mask to i16
843   %phitmp = zext i16 %p.addr.0.extract.trunc to i32
844   br label %lor.end
845
846 lor.end:
847   %t.1 = phi i32 [ 0, %entry ], [ %phitmp, %lor.rhs ]
848   %conv6 = zext i16 %b to i32
849   %div = udiv i32 %conv6, %t.1
850   %tobool8 = icmp eq i32 %div, 0
851   %cmp = icmp eq i32 %t.1, 0
852   %cmp12 = icmp ult i32 %conv2, 2
853   %cmp.sink = select i1 %tobool8, i1 %cmp12, i1 %cmp
854   br i1 %cmp.sink, label %cond.end17, label %cond.false16
855
856 cond.false16:
857   br label %cond.end17
858
859 cond.end17:
860   br label %while.body
861
862 while.body:
863   br label %while.body
864 ; CHECK-LABEL: @test64(
865 ; CHECK-NOT: select
866 }
867
868 ; CHECK-LABEL: @select_icmp_eq_and_1_0_or_2(
869 ; CHECK-NEXT: [[SHL:%[a-z0-9]+]] = shl i32 %x, 1
870 ; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i32 [[SHL]], 2
871 ; CHECK-NEXT: [[OR:%[a-z0-9]+]] = or i32 [[AND]], %y
872 ; CHECK-NEXT: ret i32 [[OR]]
873 define i32 @select_icmp_eq_and_1_0_or_2(i32 %x, i32 %y) {
874   %and = and i32 %x, 1
875   %cmp = icmp eq i32 %and, 0
876   %or = or i32 %y, 2
877   %select = select i1 %cmp, i32 %y, i32 %or
878   ret i32 %select
879 }
880
881 ; CHECK-LABEL: @select_icmp_eq_and_32_0_or_8(
882 ; CHECK-NEXT: [[LSHR:%[a-z0-9]+]] = lshr i32 %x, 2
883 ; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i32 [[LSHR]], 8
884 ; CHECK-NEXT: [[OR:%[a-z0-9]+]] = or i32 [[AND]], %y
885 ; CHECK-NEXT: ret i32 [[OR]]
886 define i32 @select_icmp_eq_and_32_0_or_8(i32 %x, i32 %y) {
887   %and = and i32 %x, 32
888   %cmp = icmp eq i32 %and, 0
889   %or = or i32 %y, 8
890   %select = select i1 %cmp, i32 %y, i32 %or
891   ret i32 %select
892 }
893
894 ; CHECK-LABEL: @select_icmp_ne_0_and_4096_or_4096(
895 ; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i32 %x, 4096
896 ; CHECK-NEXT: [[XOR:%[a-z0-9]+]] = xor i32 [[AND]], 4096
897 ; CHECK-NEXT: [[OR:%[a-z0-9]+]] = or i32 [[XOR]], %y
898 ; CHECK-NEXT: ret i32 [[OR]]
899 define i32 @select_icmp_ne_0_and_4096_or_4096(i32 %x, i32 %y) {
900   %and = and i32 %x, 4096
901   %cmp = icmp ne i32 0, %and
902   %or = or i32 %y, 4096
903   %select = select i1 %cmp, i32 %y, i32 %or
904   ret i32 %select
905 }
906
907 ; CHECK-LABEL: @select_icmp_eq_and_4096_0_or_4096(
908 ; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i32 %x, 4096
909 ; CHECK-NEXT: [[OR:%[a-z0-9]+]] = or i32 [[AND]], %y
910 ; CHECK-NEXT: ret i32 [[OR]]
911 define i32 @select_icmp_eq_and_4096_0_or_4096(i32 %x, i32 %y) {
912   %and = and i32 %x, 4096
913   %cmp = icmp eq i32 %and, 0
914   %or = or i32 %y, 4096
915   %select = select i1 %cmp, i32 %y, i32 %or
916   ret i32 %select
917 }
918
919 ; CHECK-LABEL: @select_icmp_eq_0_and_1_or_1(
920 ; CHECK-NEXT: [[TRUNC:%.+]] = trunc i64 %x to i32
921 ; CHECK-NEXT: [[AND:%.+]] = and i32 [[TRUNC]], 1
922 ; CHECK-NEXT: [[OR:%.+]] = or i32 [[XOR]], %y
923 ; CHECK-NEXT: ret i32 [[OR]]
924 define i32 @select_icmp_eq_0_and_1_or_1(i64 %x, i32 %y) {
925   %and = and i64 %x, 1
926   %cmp = icmp eq i64 %and, 0
927   %or = or i32 %y, 1
928   %select = select i1 %cmp, i32 %y, i32 %or
929   ret i32 %select
930 }
931
932 ; CHECK-LABEL: @select_icmp_ne_0_and_4096_or_32(
933 ; CHECK-NEXT: [[LSHR:%[a-z0-9]+]] = lshr i32 %x, 7
934 ; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i32 [[LSHR]], 32
935 ; CHECK-NEXT: [[XOR:%[a-z0-9]+]] = xor i32 [[AND]], 32
936 ; CHECK-NEXT: [[OR:%[a-z0-9]+]] = or i32 [[XOR]], %y
937 ; CHECK-NEXT: ret i32 [[OR]]
938 define i32 @select_icmp_ne_0_and_4096_or_32(i32 %x, i32 %y) {
939   %and = and i32 %x, 4096
940   %cmp = icmp ne i32 0, %and
941   %or = or i32 %y, 32
942   %select = select i1 %cmp, i32 %y, i32 %or
943   ret i32 %select
944 }
945
946 ; CHECK-LABEL: @select_icmp_ne_0_and_32_or_4096(
947 ; CHECK-NEXT: [[SHL:%[a-z0-9]+]] = shl i32 %x, 7
948 ; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i32 [[SHL]], 4096
949 ; CHECK-NEXT: [[XOR:%[a-z0-9]+]] = xor i32 [[AND]], 4096
950 ; CHECK-NEXT: [[OR:%[a-z0-9]+]] = or i32 [[XOR]], %y
951 ; CHECK-NEXT: ret i32 [[OR]]
952 define i32 @select_icmp_ne_0_and_32_or_4096(i32 %x, i32 %y) {
953   %and = and i32 %x, 32
954   %cmp = icmp ne i32 0, %and
955   %or = or i32 %y, 4096
956   %select = select i1 %cmp, i32 %y, i32 %or
957   ret i32 %select
958 }
959
960 ; CHECK-LABEL: @select_icmp_ne_0_and_1073741824_or_8(
961 ; CHECK-NEXT: [[LSHR:%.+]] = lshr i32 %x, 27
962 ; CHECK-NEXT: [[TRUNC:%.+]] = trunc i32 [[LSHR]] to i8
963 ; CHECK-NEXT: [[AND:%.+]] = and i8 [[TRUNC]], 8
964 ; CHECK-NEXT: [[XOR:%.+]] = xor i8 [[AND]], 8
965 ; CHECK-NEXT: [[OR:%.+]] = or i8 [[XOR]], %y
966 ; CHECK-NEXT: ret i8 [[OR]]
967 define i8 @select_icmp_ne_0_and_1073741824_or_8(i32 %x, i8 %y) {
968   %and = and i32 %x, 1073741824
969   %cmp = icmp ne i32 0, %and
970   %or = or i8 %y, 8
971   %select = select i1 %cmp, i8 %y, i8 %or
972   ret i8 %select
973 }
974
975 ; CHECK-LABEL: @select_icmp_ne_0_and_8_or_1073741824(
976 ; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i8 %x, 8
977 ; CHECK-NEXT: [[ZEXT:%[a-z0-9]+]] = zext i8 [[AND]] to i32
978 ; CHECK-NEXT: [[SHL:%[a-z0-9]+]] = shl nuw nsw i32 [[ZEXT]], 27
979 ; CHECK-NEXT: [[XOR:%[a-z0-9]+]] = xor i32 [[SHL]], 1073741824
980 ; CHECK-NEXT: [[OR:%[a-z0-9]+]] = or i32 [[XOR]], %y
981 ; CHECK-NEXT: ret i32 [[OR]]
982 define i32 @select_icmp_ne_0_and_8_or_1073741824(i8 %x, i32 %y) {
983   %and = and i8 %x, 8
984   %cmp = icmp ne i8 0, %and
985   %or = or i32 %y, 1073741824
986   %select = select i1 %cmp, i32 %y, i32 %or
987   ret i32 %select
988 }
989
990 ; We can't combine here, because the cmp is scalar and the or vector.
991 ; Just make sure we don't assert.
992 define <2 x i32> @select_icmp_eq_and_1_0_or_vector_of_2s(i32 %x, <2 x i32> %y) {
993   %and = and i32 %x, 1
994   %cmp = icmp eq i32 %and, 0
995   %or = or <2 x i32> %y, <i32 2, i32 2>
996   %select = select i1 %cmp, <2 x i32> %y, <2 x i32> %or
997   ret <2 x i32> %select
998 }
999
1000 ; CHECK-LABEL: @select_icmp_and_8_eq_0_or_8(
1001 ; CHECK-NEXT: [[OR:%[a-z0-9]+]] = or i32 %x, 8
1002 ; CHECK-NEXT: ret i32 [[OR]]
1003 define i32 @select_icmp_and_8_eq_0_or_8(i32 %x) {
1004   %and = and i32 %x, 8
1005   %cmp = icmp eq i32 %and, 0
1006   %or = or i32 %x, 8
1007   %or.x = select i1 %cmp, i32 %or, i32 %x
1008   ret i32 %or.x
1009 }
1010
1011 ; CHECK-LABEL: @select_icmp_and_8_ne_0_xor_8(
1012 ; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i32 %x, -9
1013 ; CHECK-NEXT: ret i32 [[AND]]
1014 define i32 @select_icmp_and_8_ne_0_xor_8(i32 %x) {
1015   %and = and i32 %x, 8
1016   %cmp = icmp eq i32 %and, 0
1017   %xor = xor i32 %x, 8
1018   %x.xor = select i1 %cmp, i32 %x, i32 %xor
1019   ret i32 %x.xor
1020 }
1021
1022 ; CHECK-LABEL: @select_icmp_and_8_eq_0_xor_8(
1023 ; CHECK-NEXT: [[OR:%[a-z0-9]+]] = or i32 %x, 8
1024 ; CHECK-NEXT: ret i32 [[OR]]
1025 define i32 @select_icmp_and_8_eq_0_xor_8(i32 %x) {
1026   %and = and i32 %x, 8
1027   %cmp = icmp eq i32 %and, 0
1028   %xor = xor i32 %x, 8
1029   %xor.x = select i1 %cmp, i32 %xor, i32 %x
1030   ret i32 %xor.x
1031 }
1032
1033 ; CHECK-LABEL: @select_icmp_and_8_ne_0_and_not_8(
1034 ; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i32 %x, -9
1035 ; CHECK-NEXT: ret i32 [[AND]]
1036 define i32 @select_icmp_and_8_ne_0_and_not_8(i32 %x) {
1037   %and = and i32 %x, 8
1038   %cmp = icmp eq i32 %and, 0
1039   %and1 = and i32 %x, -9
1040   %x.and1 = select i1 %cmp, i32 %x, i32 %and1
1041   ret i32 %x.and1
1042 }
1043
1044 ; CHECK-LABEL: @select_icmp_and_8_eq_0_and_not_8(
1045 ; CHECK-NEXT: ret i32 %x
1046 define i32 @select_icmp_and_8_eq_0_and_not_8(i32 %x) {
1047   %and = and i32 %x, 8
1048   %cmp = icmp eq i32 %and, 0
1049   %and1 = and i32 %x, -9
1050   %and1.x = select i1 %cmp, i32 %and1, i32 %x
1051   ret i32 %and1.x
1052 }
1053
1054 ; CHECK-LABEL: @select_icmp_x_and_8_eq_0_y_xor_8(
1055 ; CHECK: select i1 %cmp, i64 %y, i64 %xor
1056 define i64 @select_icmp_x_and_8_eq_0_y_xor_8(i32 %x, i64 %y) {
1057   %and = and i32 %x, 8
1058   %cmp = icmp eq i32 %and, 0
1059   %xor = xor i64 %y, 8
1060   %y.xor = select i1 %cmp, i64 %y, i64 %xor
1061   ret i64 %y.xor
1062 }
1063
1064 ; CHECK-LABEL: @select_icmp_x_and_8_eq_0_y_and_not_8(
1065 ; CHECK: select i1 %cmp, i64 %y, i64 %and1
1066 define i64 @select_icmp_x_and_8_eq_0_y_and_not_8(i32 %x, i64 %y) {
1067   %and = and i32 %x, 8
1068   %cmp = icmp eq i32 %and, 0
1069   %and1 = and i64 %y, -9
1070   %y.and1 = select i1 %cmp, i64 %y, i64 %and1
1071   ret i64 %y.and1
1072 }
1073
1074 ; CHECK-LABEL: @select_icmp_x_and_8_ne_0_y_xor_8(
1075 ; CHECK: select i1 %cmp, i64 %xor, i64 %y
1076 define i64 @select_icmp_x_and_8_ne_0_y_xor_8(i32 %x, i64 %y) {
1077   %and = and i32 %x, 8
1078   %cmp = icmp eq i32 %and, 0
1079   %xor = xor i64 %y, 8
1080   %xor.y = select i1 %cmp, i64 %xor, i64 %y
1081   ret i64 %xor.y
1082 }
1083
1084 ; CHECK-LABEL: @select_icmp_x_and_8_ne_0_y_and_not_8(
1085 ; CHECK: select i1 %cmp, i64 %and1, i64 %y
1086 define i64 @select_icmp_x_and_8_ne_0_y_and_not_8(i32 %x, i64 %y) {
1087   %and = and i32 %x, 8
1088   %cmp = icmp eq i32 %and, 0
1089   %and1 = and i64 %y, -9
1090   %and1.y = select i1 %cmp, i64 %and1, i64 %y
1091   ret i64 %and1.y
1092 }
1093
1094 ; CHECK-LABEL: @select_icmp_x_and_8_ne_0_y_or_8(
1095 ; CHECK: xor i64 %1, 8
1096 ; CHECK: or i64 %2, %y
1097 define i64 @select_icmp_x_and_8_ne_0_y_or_8(i32 %x, i64 %y) {
1098   %and = and i32 %x, 8
1099   %cmp = icmp eq i32 %and, 0
1100   %or = or i64 %y, 8
1101   %or.y = select i1 %cmp, i64 %or, i64 %y
1102   ret i64 %or.y
1103 }
1104
1105 define i32 @test65(i64 %x) {
1106   %1 = and i64 %x, 16
1107   %2 = icmp ne i64 %1, 0
1108   %3 = select i1 %2, i32 40, i32 42
1109   ret i32 %3
1110
1111 ; CHECK-LABEL: @test65(
1112 ; CHECK: %[[TRUNC:.*]] = trunc i64 %x to i32
1113 ; CHECK: %[[LSHR:.*]] = lshr i32 %[[TRUNC]], 3
1114 ; CHECK: %[[AND:.*]] = and i32 %[[LSHR]], 2
1115 ; CHECK: %[[XOR:.*]] = xor i32 %[[AND]], 42
1116 ; CHECK: ret i32 %[[XOR]]
1117 }
1118
1119 define i32 @test66(i64 %x) {
1120   %1 = and i64 %x, 4294967296
1121   %2 = icmp ne i64 %1, 0
1122   %3 = select i1 %2, i32 40, i32 42
1123   ret i32 %3
1124
1125 ; CHECK-LABEL: @test66(
1126 ; CHECK: select
1127 }
1128
1129 define i32 @test67(i16 %x) {
1130   %1 = and i16 %x, 4
1131   %2 = icmp ne i16 %1, 0
1132   %3 = select i1 %2, i32 40, i32 42
1133   ret i32 %3
1134
1135 ; CHECK-LABEL: @test67(
1136 ; CHECK: and i16 %x, 4
1137 ; CHECK: zext i16 %1 to i32
1138 ; CHECK: lshr exact i32 %2, 1
1139 ; CHECK: xor i32 %3, 42
1140 }
1141
1142 ; SMIN(SMIN(X, 11), 92) -> SMIN(X, 11)
1143 define i32 @test68(i32 %x) {
1144 entry:
1145   %cmp = icmp slt i32 11, %x
1146   %cond = select i1 %cmp, i32 11, i32 %x
1147   %cmp3 = icmp slt i32 92, %cond
1148   %retval = select i1 %cmp3, i32 92, i32 %cond
1149   ret i32 %retval
1150 ; CHECK-LABEL: @test68(
1151 ; CHECK: ret i32 %cond
1152 }
1153
1154 ; MIN(MIN(X, 24), 83) -> MIN(X, 24)
1155 define i32 @test69(i32 %x) {
1156 entry:
1157   %cmp = icmp ult i32 24, %x
1158   %cond = select i1 %cmp, i32 24, i32 %x
1159   %cmp3 = icmp ult i32 83, %cond
1160   %retval = select i1 %cmp3, i32 83, i32 %cond
1161   ret i32 %retval
1162 ; CHECK-LABEL: @test69(
1163 ; CHECK: ret i32 %cond
1164 }
1165
1166 ; SMAX(SMAX(X, 75), 36) -> SMAX(X, 75)
1167 define i32 @test70(i32 %x) {
1168 entry:
1169   %cmp = icmp slt i32 %x, 75
1170   %cond = select i1 %cmp, i32 75, i32 %x
1171   %cmp3 = icmp slt i32 %cond, 36
1172   %retval = select i1 %cmp3, i32 36, i32 %cond
1173   ret i32 %retval
1174 ; CHECK-LABEL: @test70(
1175 ; CHECK: ret i32 %cond
1176 }
1177
1178 ; MAX(MAX(X, 68), 47) -> MAX(X, 68)
1179 define i32 @test71(i32 %x) {
1180 entry:
1181   %cmp = icmp ult i32 %x, 68
1182   %cond = select i1 %cmp, i32 68, i32 %x
1183   %cmp3 = icmp ult i32 %cond, 47
1184   %retval = select i1 %cmp3, i32 47, i32 %cond
1185   ret i32 %retval
1186 ; CHECK-LABEL: @test71(
1187 ; CHECK: ret i32 %cond
1188 }
1189
1190 ; SMIN(SMIN(X, 92), 11) -> SMIN(X, 11)
1191 define i32 @test72(i32 %x) {
1192   %cmp = icmp sgt i32 %x, 92
1193   %cond = select i1 %cmp, i32 92, i32 %x
1194   %cmp3 = icmp sgt i32 %cond, 11
1195   %retval = select i1 %cmp3, i32 11, i32 %cond
1196   ret i32 %retval
1197 ; CHECK-LABEL: @test72(
1198 ; CHECK-NEXT: [[CMP:%[a-z0-9]+]] = icmp sgt i32 %x, 11
1199 ; CHECK-NEXT: [[SEL:%[a-z0-9]+]] = select i1 [[CMP]], i32 11, i32 %x
1200 ; CHECK-NEXT: ret i32 [[SEL]]
1201 }
1202
1203 ; MIN(MIN(X, 83), 24) -> MIN(X, 24)
1204 define i32 @test73(i32 %x) {
1205   %cmp = icmp ugt i32 %x, 83
1206   %cond = select i1 %cmp, i32 83, i32 %x
1207   %cmp3 = icmp ugt i32 %cond, 24
1208   %retval = select i1 %cmp3, i32 24, i32 %cond
1209   ret i32 %retval
1210 ; CHECK-LABEL: @test73(
1211 ; CHECK-NEXT: [[CMP:%[a-z0-9]+]] = icmp ugt i32 %x, 24
1212 ; CHECK-NEXT: [[SEL:%[a-z0-9]+]] = select i1 [[CMP]], i32 24, i32 %x
1213 ; CHECK-NEXT: ret i32 [[SEL]]
1214 }
1215
1216 ; SMAX(SMAX(X, 36), 75) -> SMAX(X, 75)
1217 define i32 @test74(i32 %x) {
1218   %cmp = icmp slt i32 %x, 36
1219   %cond = select i1 %cmp, i32 36, i32 %x
1220   %cmp3 = icmp slt i32 %cond, 75
1221   %retval = select i1 %cmp3, i32 75, i32 %cond
1222   ret i32 %retval
1223 ; CHECK-LABEL: @test74(
1224 ; CHECK-NEXT: [[CMP:%[a-z0-9]+]] = icmp slt i32 %x, 75
1225 ; CHECK-NEXT: [[SEL:%[a-z0-9]+]] = select i1 [[CMP]], i32 75, i32 %x
1226 ; CHECK-NEXT: ret i32 [[SEL]]
1227 }
1228
1229 ; MAX(MAX(X, 47), 68) -> MAX(X, 68)
1230 define i32 @test75(i32 %x) {
1231   %cmp = icmp ult i32 %x, 47
1232   %cond = select i1 %cmp, i32 47, i32 %x
1233   %cmp3 = icmp ult i32 %cond, 68
1234   %retval = select i1 %cmp3, i32 68, i32 %cond
1235   ret i32 %retval
1236 ; CHECK-LABEL: @test75(
1237 ; CHECK-NEXT: [[CMP:%[a-z0-9]+]] = icmp ult i32 %x, 68
1238 ; CHECK-NEXT: [[SEL:%[a-z0-9]+]] = select i1 [[CMP]], i32 68, i32 %x
1239 ; CHECK-NEXT: ret i32 [[SEL]]
1240 }
1241
1242 @under_aligned = external global i32, align 1
1243
1244 define i32 @test76(i1 %flag, i32* %x) {
1245 ; The load here must not be speculated around the select. One side of the
1246 ; select is trivially dereferencable but may have a lower alignment than the
1247 ; load does.
1248 ; CHECK-LABEL: @test76(
1249 ; CHECK: store i32 0, i32* %x
1250 ; CHECK: %[[P:.*]] = select i1 %flag, i32* @under_aligned, i32* %x
1251 ; CHECK: load i32* %[[P]]
1252
1253   store i32 0, i32* %x
1254   %p = select i1 %flag, i32* @under_aligned, i32* %x
1255   %v = load i32* %p
1256   ret i32 %v
1257 }
1258
1259 declare void @scribble_on_i32(i32*)
1260
1261 define i32 @test77(i1 %flag, i32* %x) {
1262 ; The load here must not be speculated around the select. One side of the
1263 ; select is trivially dereferencable but may have a lower alignment than the
1264 ; load does.
1265 ; CHECK-LABEL: @test77(
1266 ; CHECK: %[[A:.*]] = alloca i32, align 1
1267 ; CHECK: call void @scribble_on_i32(i32* %[[A]])
1268 ; CHECK: store i32 0, i32* %x
1269 ; CHECK: %[[P:.*]] = select i1 %flag, i32* %[[A]], i32* %x
1270 ; CHECK: load i32* %[[P]]
1271
1272   %under_aligned = alloca i32, align 1
1273   call void @scribble_on_i32(i32* %under_aligned)
1274   store i32 0, i32* %x
1275   %p = select i1 %flag, i32* %under_aligned, i32* %x
1276   %v = load i32* %p
1277   ret i32 %v
1278 }
1279
1280 define i32 @test78(i1 %flag, i32* %x, i32* %y, i32* %z) {
1281 ; Test that we can speculate the loads around the select even when we can't
1282 ; fold the load completely away.
1283 ; CHECK-LABEL: @test78(
1284 ; CHECK:         %[[V1:.*]] = load i32* %x
1285 ; CHECK-NEXT:    %[[V2:.*]] = load i32* %y
1286 ; CHECK-NEXT:    %[[S:.*]] = select i1 %flag, i32 %[[V1]], i32 %[[V2]]
1287 ; CHECK-NEXT:    ret i32 %[[S]]
1288 entry:
1289   store i32 0, i32* %x
1290   store i32 0, i32* %y
1291   ; Block forwarding by storing to %z which could alias either %x or %y.
1292   store i32 42, i32* %z
1293   %p = select i1 %flag, i32* %x, i32* %y
1294   %v = load i32* %p
1295   ret i32 %v
1296 }
1297
1298 define float @test79(i1 %flag, float* %x, i32* %y, i32* %z) {
1299 ; Test that we can speculate the loads around the select even when we can't
1300 ; fold the load completely away.
1301 ; CHECK-LABEL: @test79(
1302 ; CHECK:         %[[V1:.*]] = load float* %x
1303 ; CHECK-NEXT:    %[[V2:.*]] = load float* %y
1304 ; CHECK-NEXT:    %[[S:.*]] = select i1 %flag, float %[[V1]], float %[[V2]]
1305 ; CHECK-NEXT:    ret float %[[S]]
1306 entry:
1307   %x1 = bitcast float* %x to i32*
1308   %y1 = bitcast i32* %y to float*
1309   store i32 0, i32* %x1
1310   store i32 0, i32* %y
1311   ; Block forwarding by storing to %z which could alias either %x or %y.
1312   store i32 42, i32* %z
1313   %p = select i1 %flag, float* %x, float* %y1
1314   %v = load float* %p
1315   ret float %v
1316 }
1317
1318 define i32 @test80(i1 %flag) {
1319 ; Test that when we speculate the loads around the select they fold throug
1320 ; load->load folding and load->store folding.
1321 ; CHECK-LABEL: @test80(
1322 ; CHECK:         %[[X:.*]] = alloca i32
1323 ; CHECK-NEXT:    %[[Y:.*]] = alloca i32
1324 ; CHECK:         %[[V:.*]] = load i32* %[[X]]
1325 ; CHECK-NEXT:    store i32 %[[V]], i32* %[[Y]]
1326 ; CHECK-NEXT:    ret i32 %[[V]]
1327 entry:
1328   %x = alloca i32
1329   %y = alloca i32
1330   call void @scribble_on_i32(i32* %x)
1331   call void @scribble_on_i32(i32* %y)
1332   %tmp = load i32* %x
1333   store i32 %tmp, i32* %y
1334   %p = select i1 %flag, i32* %x, i32* %y
1335   %v = load i32* %p
1336   ret i32 %v
1337 }
1338
1339 define float @test81(i1 %flag) {
1340 ; Test that we can speculate the load around the select even though they use
1341 ; differently typed pointers.
1342 ; CHECK-LABEL: @test81(
1343 ; CHECK:         %[[X:.*]] = alloca i32
1344 ; CHECK-NEXT:    %[[Y:.*]] = alloca i32
1345 ; CHECK:         %[[V:.*]] = load i32* %[[X]]
1346 ; CHECK-NEXT:    store i32 %[[V]], i32* %[[Y]]
1347 ; CHECK-NEXT:    %[[C:.*]] = bitcast i32 %[[V]] to float
1348 ; CHECK-NEXT:    ret float %[[C]]
1349 entry:
1350   %x = alloca float
1351   %y = alloca i32
1352   %x1 = bitcast float* %x to i32*
1353   %y1 = bitcast i32* %y to float*
1354   call void @scribble_on_i32(i32* %x1)
1355   call void @scribble_on_i32(i32* %y)
1356   %tmp = load i32* %x1
1357   store i32 %tmp, i32* %y
1358   %p = select i1 %flag, float* %x, float* %y1
1359   %v = load float* %p
1360   ret float %v
1361 }
1362
1363 define i32 @test82(i1 %flag) {
1364 ; Test that we can speculate the load around the select even though they use
1365 ; differently typed pointers.
1366 ; CHECK-LABEL: @test82(
1367 ; CHECK:         %[[X:.*]] = alloca float
1368 ; CHECK-NEXT:    %[[Y:.*]] = alloca i32
1369 ; CHECK-NEXT:    %[[X1:.*]] = bitcast float* %[[X]] to i32*
1370 ; CHECK-NEXT:    %[[Y1:.*]] = bitcast i32* %[[Y]] to float*
1371 ; CHECK:         %[[V:.*]] = load float* %[[X]]
1372 ; CHECK-NEXT:    store float %[[V]], float* %[[Y1]]
1373 ; CHECK-NEXT:    %[[C:.*]] = bitcast float %[[V]] to i32
1374 ; CHECK-NEXT:    ret i32 %[[C]]
1375 entry:
1376   %x = alloca float
1377   %y = alloca i32
1378   %x1 = bitcast float* %x to i32*
1379   %y1 = bitcast i32* %y to float*
1380   call void @scribble_on_i32(i32* %x1)
1381   call void @scribble_on_i32(i32* %y)
1382   %tmp = load float* %x
1383   store float %tmp, float* %y1
1384   %p = select i1 %flag, i32* %x1, i32* %y
1385   %v = load i32* %p
1386   ret i32 %v
1387 }
1388
1389 declare void @scribble_on_i64(i64*)
1390
1391 define i8* @test83(i1 %flag) {
1392 ; Test that we can speculate the load around the select even though they use
1393 ; differently typed pointers and requires inttoptr casts.
1394 ; CHECK-LABEL: @test83(
1395 ; CHECK:         %[[X:.*]] = alloca i8*
1396 ; CHECK-NEXT:    %[[Y:.*]] = alloca i8*
1397 ; CHECK:         %[[V:.*]] = load i64* %[[X]]
1398 ; CHECK-NEXT:    %[[C1:.*]] = inttoptr i64 %[[V]] to i8*
1399 ; CHECK-NEXT:    store i8* %[[C1]], i8** %[[Y]]
1400 ; CHECK-NEXT:    %[[C2:.*]] = inttoptr i64 %[[V]] to i8*
1401 ; CHECK-NEXT:    %[[S:.*]] = select i1 %flag, i8* %[[C2]], i8* %[[C1]]
1402 ; CHECK-NEXT:    ret i8* %[[S]]
1403 entry:
1404   %x = alloca i8*
1405   %y = alloca i64
1406   %x1 = bitcast i8** %x to i64*
1407   %y1 = bitcast i64* %y to i8**
1408   call void @scribble_on_i64(i64* %x1)
1409   call void @scribble_on_i64(i64* %y)
1410   %tmp = load i64* %x1
1411   store i64 %tmp, i64* %y
1412   %p = select i1 %flag, i8** %x, i8** %y1
1413   %v = load i8** %p
1414   ret i8* %v
1415 }
1416
1417 define i64 @test84(i1 %flag) {
1418 ; Test that we can speculate the load around the select even though they use
1419 ; differently typed pointers and requires a ptrtoint cast.
1420 ; CHECK-LABEL: @test84(
1421 ; CHECK:         %[[X:.*]] = alloca i8*
1422 ; CHECK-NEXT:    %[[Y:.*]] = alloca i8*
1423 ; CHECK:         %[[V:.*]] = load i8** %[[X]]
1424 ; CHECK-NEXT:    store i8* %[[V]], i8** %[[Y]]
1425 ; CHECK-NEXT:    %[[C:.*]] = ptrtoint i8* %[[V]] to i64
1426 ; CHECK-NEXT:    ret i64 %[[C]]
1427 entry:
1428   %x = alloca i8*
1429   %y = alloca i64
1430   %x1 = bitcast i8** %x to i64*
1431   %y1 = bitcast i64* %y to i8**
1432   call void @scribble_on_i64(i64* %x1)
1433   call void @scribble_on_i64(i64* %y)
1434   %tmp = load i8** %x
1435   store i8* %tmp, i8** %y1
1436   %p = select i1 %flag, i64* %x1, i64* %y
1437   %v = load i64* %p
1438   ret i64 %v
1439 }