InstCombine: Squash an icmp+select into bitwise arithmetic
[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_ne_0_xor_8(
1001 ; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i32 %x, -9
1002 ; CHECK-NEXT: ret i32 [[AND]]
1003 define i32 @select_icmp_and_8_ne_0_xor_8(i32 %x) {
1004   %and = and i32 %x, 8
1005   %cmp = icmp eq i32 %and, 0
1006   %xor = xor i32 %x, 8
1007   %x.xor = select i1 %cmp, i32 %x, i32 %xor
1008   ret i32 %x.xor
1009 }
1010
1011 ; CHECK-LABEL: @select_icmp_and_8_eq_0_xor_8(
1012 ; CHECK-NEXT: [[OR:%[a-z0-9]+]] = or i32 %x, 8
1013 ; CHECK-NEXT: ret i32 [[OR]]
1014 define i32 @select_icmp_and_8_eq_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   %xor.x = select i1 %cmp, i32 %xor, i32 %x
1019   ret i32 %xor.x
1020 }
1021
1022 ; CHECK-LABEL: @select_icmp_x_and_8_eq_0_y_xor_8(
1023 ; CHECK: select i1 %cmp, i64 %y, i64 %xor
1024 define i64 @select_icmp_x_and_8_eq_0_y_xor_8(i32 %x, i64 %y) {
1025   %and = and i32 %x, 8
1026   %cmp = icmp eq i32 %and, 0
1027   %xor = xor i64 %y, 8
1028   %y.xor = select i1 %cmp, i64 %y, i64 %xor
1029   ret i64 %y.xor
1030 }
1031
1032 ; CHECK-LABEL: @select_icmp_x_and_8_ne_0_y_xor_8(
1033 ; CHECK: select i1 %cmp, i64 %xor, i64 %y
1034 define i64 @select_icmp_x_and_8_ne_0_y_xor_8(i32 %x, i64 %y) {
1035   %and = and i32 %x, 8
1036   %cmp = icmp eq i32 %and, 0
1037   %xor = xor i64 %y, 8
1038   %xor.y = select i1 %cmp, i64 %xor, i64 %y
1039   ret i64 %xor.y
1040 }
1041
1042 ; CHECK-LABEL: @select_icmp_x_and_8_ne_0_y_or_8(
1043 ; CHECK: xor i64 %1, 8
1044 ; CHECK: or i64 %2, %y
1045 define i64 @select_icmp_x_and_8_ne_0_y_or_8(i32 %x, i64 %y) {
1046   %and = and i32 %x, 8
1047   %cmp = icmp eq i32 %and, 0
1048   %or = or i64 %y, 8
1049   %or.y = select i1 %cmp, i64 %or, i64 %y
1050   ret i64 %or.y
1051 }
1052
1053 ; CHECK-LABEL: @select_icmp_and_2147483648_ne_0_xor_2147483648(
1054 ; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i32 %x, 2147483647
1055 ; CHECK-NEXT: ret i32 [[AND]]
1056 define i32 @select_icmp_and_2147483648_ne_0_xor_2147483648(i32 %x) {
1057   %and = and i32 %x, 2147483648
1058   %cmp = icmp eq i32 %and, 0
1059   %xor = xor i32 %x, 2147483648
1060   %x.xor = select i1 %cmp, i32 %x, i32 %xor
1061   ret i32 %x.xor
1062 }
1063
1064 ; CHECK-LABEL: @select_icmp_and_2147483648_eq_0_xor_2147483648(
1065 ; CHECK-NEXT: [[OR:%[a-z0-9]+]] = or i32 %x, -2147483648
1066 ; CHECK-NEXT: ret i32 [[OR]]
1067 define i32 @select_icmp_and_2147483648_eq_0_xor_2147483648(i32 %x) {
1068   %and = and i32 %x, 2147483648
1069   %cmp = icmp eq i32 %and, 0
1070   %xor = xor i32 %x, 2147483648
1071   %xor.x = select i1 %cmp, i32 %xor, i32 %x
1072   ret i32 %xor.x
1073 }
1074
1075 ; CHECK-LABEL: @select_icmp_x_and_2147483648_ne_0_or_2147483648(
1076 ; CHECK: xor i32 %1, -2147483648
1077 ; CHECK: or i32 %2, %x
1078 define i32 @select_icmp_x_and_2147483648_ne_0_or_2147483648(i32 %x) {
1079   %and = and i32 %x, 2147483648
1080   %cmp = icmp eq i32 %and, 0
1081   %or = or i32 %x, 2147483648
1082   %or.x = select i1 %cmp, i32 %or, i32 %x
1083   ret i32 %or.x
1084 }
1085
1086 define i32 @test65(i64 %x) {
1087   %1 = and i64 %x, 16
1088   %2 = icmp ne i64 %1, 0
1089   %3 = select i1 %2, i32 40, i32 42
1090   ret i32 %3
1091
1092 ; CHECK-LABEL: @test65(
1093 ; CHECK: %[[TRUNC:.*]] = trunc i64 %x to i32
1094 ; CHECK: %[[LSHR:.*]] = lshr i32 %[[TRUNC]], 3
1095 ; CHECK: %[[AND:.*]] = and i32 %[[LSHR]], 2
1096 ; CHECK: %[[XOR:.*]] = xor i32 %[[AND]], 42
1097 ; CHECK: ret i32 %[[XOR]]
1098 }
1099
1100 define i32 @test66(i64 %x) {
1101   %1 = and i64 %x, 4294967296
1102   %2 = icmp ne i64 %1, 0
1103   %3 = select i1 %2, i32 40, i32 42
1104   ret i32 %3
1105
1106 ; CHECK-LABEL: @test66(
1107 ; CHECK: select
1108 }
1109
1110 define i32 @test67(i16 %x) {
1111   %1 = and i16 %x, 4
1112   %2 = icmp ne i16 %1, 0
1113   %3 = select i1 %2, i32 40, i32 42
1114   ret i32 %3
1115
1116 ; CHECK-LABEL: @test67(
1117 ; CHECK: and i16 %x, 4
1118 ; CHECK: zext i16 %1 to i32
1119 ; CHECK: lshr exact i32 %2, 1
1120 ; CHECK: xor i32 %3, 42
1121 }
1122
1123 ; SMIN(SMIN(X, 11), 92) -> SMIN(X, 11)
1124 define i32 @test68(i32 %x) {
1125 entry:
1126   %cmp = icmp slt i32 11, %x
1127   %cond = select i1 %cmp, i32 11, i32 %x
1128   %cmp3 = icmp slt i32 92, %cond
1129   %retval = select i1 %cmp3, i32 92, i32 %cond
1130   ret i32 %retval
1131 ; CHECK-LABEL: @test68(
1132 ; CHECK: ret i32 %cond
1133 }
1134
1135 ; MIN(MIN(X, 24), 83) -> MIN(X, 24)
1136 define i32 @test69(i32 %x) {
1137 entry:
1138   %cmp = icmp ult i32 24, %x
1139   %cond = select i1 %cmp, i32 24, i32 %x
1140   %cmp3 = icmp ult i32 83, %cond
1141   %retval = select i1 %cmp3, i32 83, i32 %cond
1142   ret i32 %retval
1143 ; CHECK-LABEL: @test69(
1144 ; CHECK: ret i32 %cond
1145 }
1146
1147 ; SMAX(SMAX(X, 75), 36) -> SMAX(X, 75)
1148 define i32 @test70(i32 %x) {
1149 entry:
1150   %cmp = icmp slt i32 %x, 75
1151   %cond = select i1 %cmp, i32 75, i32 %x
1152   %cmp3 = icmp slt i32 %cond, 36
1153   %retval = select i1 %cmp3, i32 36, i32 %cond
1154   ret i32 %retval
1155 ; CHECK-LABEL: @test70(
1156 ; CHECK: ret i32 %cond
1157 }
1158
1159 ; MAX(MAX(X, 68), 47) -> MAX(X, 68)
1160 define i32 @test71(i32 %x) {
1161 entry:
1162   %cmp = icmp ult i32 %x, 68
1163   %cond = select i1 %cmp, i32 68, i32 %x
1164   %cmp3 = icmp ult i32 %cond, 47
1165   %retval = select i1 %cmp3, i32 47, i32 %cond
1166   ret i32 %retval
1167 ; CHECK-LABEL: @test71(
1168 ; CHECK: ret i32 %cond
1169 }
1170
1171 ; SMIN(SMIN(X, 92), 11) -> SMIN(X, 11)
1172 define i32 @test72(i32 %x) {
1173   %cmp = icmp sgt i32 %x, 92
1174   %cond = select i1 %cmp, i32 92, i32 %x
1175   %cmp3 = icmp sgt i32 %cond, 11
1176   %retval = select i1 %cmp3, i32 11, i32 %cond
1177   ret i32 %retval
1178 ; CHECK-LABEL: @test72(
1179 ; CHECK-NEXT: [[CMP:%[a-z0-9]+]] = icmp sgt i32 %x, 11
1180 ; CHECK-NEXT: [[SEL:%[a-z0-9]+]] = select i1 [[CMP]], i32 11, i32 %x
1181 ; CHECK-NEXT: ret i32 [[SEL]]
1182 }
1183
1184 ; MIN(MIN(X, 83), 24) -> MIN(X, 24)
1185 define i32 @test73(i32 %x) {
1186   %cmp = icmp ugt i32 %x, 83
1187   %cond = select i1 %cmp, i32 83, i32 %x
1188   %cmp3 = icmp ugt i32 %cond, 24
1189   %retval = select i1 %cmp3, i32 24, i32 %cond
1190   ret i32 %retval
1191 ; CHECK-LABEL: @test73(
1192 ; CHECK-NEXT: [[CMP:%[a-z0-9]+]] = icmp ugt i32 %x, 24
1193 ; CHECK-NEXT: [[SEL:%[a-z0-9]+]] = select i1 [[CMP]], i32 24, i32 %x
1194 ; CHECK-NEXT: ret i32 [[SEL]]
1195 }
1196
1197 ; SMAX(SMAX(X, 36), 75) -> SMAX(X, 75)
1198 define i32 @test74(i32 %x) {
1199   %cmp = icmp slt i32 %x, 36
1200   %cond = select i1 %cmp, i32 36, i32 %x
1201   %cmp3 = icmp slt i32 %cond, 75
1202   %retval = select i1 %cmp3, i32 75, i32 %cond
1203   ret i32 %retval
1204 ; CHECK-LABEL: @test74(
1205 ; CHECK-NEXT: [[CMP:%[a-z0-9]+]] = icmp slt i32 %x, 75
1206 ; CHECK-NEXT: [[SEL:%[a-z0-9]+]] = select i1 [[CMP]], i32 75, i32 %x
1207 ; CHECK-NEXT: ret i32 [[SEL]]
1208 }
1209
1210 ; MAX(MAX(X, 47), 68) -> MAX(X, 68)
1211 define i32 @test75(i32 %x) {
1212   %cmp = icmp ult i32 %x, 47
1213   %cond = select i1 %cmp, i32 47, i32 %x
1214   %cmp3 = icmp ult i32 %cond, 68
1215   %retval = select i1 %cmp3, i32 68, i32 %cond
1216   ret i32 %retval
1217 ; CHECK-LABEL: @test75(
1218 ; CHECK-NEXT: [[CMP:%[a-z0-9]+]] = icmp ult i32 %x, 68
1219 ; CHECK-NEXT: [[SEL:%[a-z0-9]+]] = select i1 [[CMP]], i32 68, i32 %x
1220 ; CHECK-NEXT: ret i32 [[SEL]]
1221 }
1222
1223 @under_aligned = external global i32, align 1
1224
1225 define i32 @test76(i1 %flag, i32* %x) {
1226 ; The load here must not be speculated around the select. One side of the
1227 ; select is trivially dereferencable but may have a lower alignment than the
1228 ; load does.
1229 ; CHECK-LABEL: @test76(
1230 ; CHECK: store i32 0, i32* %x
1231 ; CHECK: %[[P:.*]] = select i1 %flag, i32* @under_aligned, i32* %x
1232 ; CHECK: load i32* %[[P]]
1233
1234   store i32 0, i32* %x
1235   %p = select i1 %flag, i32* @under_aligned, i32* %x
1236   %v = load i32* %p
1237   ret i32 %v
1238 }
1239
1240 declare void @scribble_on_i32(i32*)
1241
1242 define i32 @test77(i1 %flag, i32* %x) {
1243 ; The load here must not be speculated around the select. One side of the
1244 ; select is trivially dereferencable but may have a lower alignment than the
1245 ; load does.
1246 ; CHECK-LABEL: @test77(
1247 ; CHECK: %[[A:.*]] = alloca i32, align 1
1248 ; CHECK: call void @scribble_on_i32(i32* %[[A]])
1249 ; CHECK: store i32 0, i32* %x
1250 ; CHECK: %[[P:.*]] = select i1 %flag, i32* %[[A]], i32* %x
1251 ; CHECK: load i32* %[[P]]
1252
1253   %under_aligned = alloca i32, align 1
1254   call void @scribble_on_i32(i32* %under_aligned)
1255   store i32 0, i32* %x
1256   %p = select i1 %flag, i32* %under_aligned, i32* %x
1257   %v = load i32* %p
1258   ret i32 %v
1259 }
1260
1261 define i32 @test78(i1 %flag, i32* %x, i32* %y, i32* %z) {
1262 ; Test that we can speculate the loads around the select even when we can't
1263 ; fold the load completely away.
1264 ; CHECK-LABEL: @test78(
1265 ; CHECK:         %[[V1:.*]] = load i32* %x
1266 ; CHECK-NEXT:    %[[V2:.*]] = load i32* %y
1267 ; CHECK-NEXT:    %[[S:.*]] = select i1 %flag, i32 %[[V1]], i32 %[[V2]]
1268 ; CHECK-NEXT:    ret i32 %[[S]]
1269 entry:
1270   store i32 0, i32* %x
1271   store i32 0, i32* %y
1272   ; Block forwarding by storing to %z which could alias either %x or %y.
1273   store i32 42, i32* %z
1274   %p = select i1 %flag, i32* %x, i32* %y
1275   %v = load i32* %p
1276   ret i32 %v
1277 }
1278
1279 define float @test79(i1 %flag, float* %x, i32* %y, i32* %z) {
1280 ; Test that we can speculate the loads around the select even when we can't
1281 ; fold the load completely away.
1282 ; CHECK-LABEL: @test79(
1283 ; CHECK:         %[[V1:.*]] = load float* %x
1284 ; CHECK-NEXT:    %[[V2:.*]] = load float* %y
1285 ; CHECK-NEXT:    %[[S:.*]] = select i1 %flag, float %[[V1]], float %[[V2]]
1286 ; CHECK-NEXT:    ret float %[[S]]
1287 entry:
1288   %x1 = bitcast float* %x to i32*
1289   %y1 = bitcast i32* %y to float*
1290   store i32 0, i32* %x1
1291   store i32 0, i32* %y
1292   ; Block forwarding by storing to %z which could alias either %x or %y.
1293   store i32 42, i32* %z
1294   %p = select i1 %flag, float* %x, float* %y1
1295   %v = load float* %p
1296   ret float %v
1297 }
1298
1299 define i32 @test80(i1 %flag) {
1300 ; Test that when we speculate the loads around the select they fold throug
1301 ; load->load folding and load->store folding.
1302 ; CHECK-LABEL: @test80(
1303 ; CHECK:         %[[X:.*]] = alloca i32
1304 ; CHECK-NEXT:    %[[Y:.*]] = alloca i32
1305 ; CHECK:         %[[V:.*]] = load i32* %[[X]]
1306 ; CHECK-NEXT:    store i32 %[[V]], i32* %[[Y]]
1307 ; CHECK-NEXT:    ret i32 %[[V]]
1308 entry:
1309   %x = alloca i32
1310   %y = alloca i32
1311   call void @scribble_on_i32(i32* %x)
1312   call void @scribble_on_i32(i32* %y)
1313   %tmp = load i32* %x
1314   store i32 %tmp, i32* %y
1315   %p = select i1 %flag, i32* %x, i32* %y
1316   %v = load i32* %p
1317   ret i32 %v
1318 }
1319
1320 define float @test81(i1 %flag) {
1321 ; Test that we can speculate the load around the select even though they use
1322 ; differently typed pointers.
1323 ; CHECK-LABEL: @test81(
1324 ; CHECK:         %[[X:.*]] = alloca i32
1325 ; CHECK-NEXT:    %[[Y:.*]] = alloca i32
1326 ; CHECK:         %[[V:.*]] = load i32* %[[X]]
1327 ; CHECK-NEXT:    store i32 %[[V]], i32* %[[Y]]
1328 ; CHECK-NEXT:    %[[C:.*]] = bitcast i32 %[[V]] to float
1329 ; CHECK-NEXT:    ret float %[[C]]
1330 entry:
1331   %x = alloca float
1332   %y = alloca i32
1333   %x1 = bitcast float* %x to i32*
1334   %y1 = bitcast i32* %y to float*
1335   call void @scribble_on_i32(i32* %x1)
1336   call void @scribble_on_i32(i32* %y)
1337   %tmp = load i32* %x1
1338   store i32 %tmp, i32* %y
1339   %p = select i1 %flag, float* %x, float* %y1
1340   %v = load float* %p
1341   ret float %v
1342 }
1343
1344 define i32 @test82(i1 %flag) {
1345 ; Test that we can speculate the load around the select even though they use
1346 ; differently typed pointers.
1347 ; CHECK-LABEL: @test82(
1348 ; CHECK:         %[[X:.*]] = alloca float
1349 ; CHECK-NEXT:    %[[Y:.*]] = alloca i32
1350 ; CHECK-NEXT:    %[[X1:.*]] = bitcast float* %[[X]] to i32*
1351 ; CHECK-NEXT:    %[[Y1:.*]] = bitcast i32* %[[Y]] to float*
1352 ; CHECK:         %[[V:.*]] = load float* %[[X]]
1353 ; CHECK-NEXT:    store float %[[V]], float* %[[Y1]]
1354 ; CHECK-NEXT:    %[[C:.*]] = bitcast float %[[V]] to i32
1355 ; CHECK-NEXT:    ret i32 %[[C]]
1356 entry:
1357   %x = alloca float
1358   %y = alloca i32
1359   %x1 = bitcast float* %x to i32*
1360   %y1 = bitcast i32* %y to float*
1361   call void @scribble_on_i32(i32* %x1)
1362   call void @scribble_on_i32(i32* %y)
1363   %tmp = load float* %x
1364   store float %tmp, float* %y1
1365   %p = select i1 %flag, i32* %x1, i32* %y
1366   %v = load i32* %p
1367   ret i32 %v
1368 }
1369
1370 declare void @scribble_on_i64(i64*)
1371 declare void @scribble_on_i128(i128*)
1372
1373 define i8* @test83(i1 %flag) {
1374 ; Test that we can speculate the load around the select even though they use
1375 ; differently typed pointers and requires inttoptr casts.
1376 ; CHECK-LABEL: @test83(
1377 ; CHECK:         %[[X:.*]] = alloca i8*
1378 ; CHECK-NEXT:    %[[Y:.*]] = alloca i8*
1379 ; CHECK-DAG:     %[[X2:.*]] = bitcast i8** %[[X]] to i64*
1380 ; CHECK-DAG:     %[[Y2:.*]] = bitcast i8** %[[Y]] to i64*
1381 ; CHECK:         %[[V:.*]] = load i64* %[[X2]]
1382 ; CHECK-NEXT:    store i64 %[[V]], i64* %[[Y2]]
1383 ; CHECK-NEXT:    %[[C:.*]] = inttoptr i64 %[[V]] to i8*
1384 ; CHECK-NEXT:    ret i8* %[[S]]
1385 entry:
1386   %x = alloca i8*
1387   %y = alloca i64
1388   %x1 = bitcast i8** %x to i64*
1389   %y1 = bitcast i64* %y to i8**
1390   call void @scribble_on_i64(i64* %x1)
1391   call void @scribble_on_i64(i64* %y)
1392   %tmp = load i64* %x1
1393   store i64 %tmp, i64* %y
1394   %p = select i1 %flag, i8** %x, i8** %y1
1395   %v = load i8** %p
1396   ret i8* %v
1397 }
1398
1399 define i64 @test84(i1 %flag) {
1400 ; Test that we can speculate the load around the select even though they use
1401 ; differently typed pointers and requires a ptrtoint cast.
1402 ; CHECK-LABEL: @test84(
1403 ; CHECK:         %[[X:.*]] = alloca i8*
1404 ; CHECK-NEXT:    %[[Y:.*]] = alloca i8*
1405 ; CHECK:         %[[V:.*]] = load i8** %[[X]]
1406 ; CHECK-NEXT:    store i8* %[[V]], i8** %[[Y]]
1407 ; CHECK-NEXT:    %[[C:.*]] = ptrtoint i8* %[[V]] to i64
1408 ; CHECK-NEXT:    ret i64 %[[C]]
1409 entry:
1410   %x = alloca i8*
1411   %y = alloca i64
1412   %x1 = bitcast i8** %x to i64*
1413   %y1 = bitcast i64* %y to i8**
1414   call void @scribble_on_i64(i64* %x1)
1415   call void @scribble_on_i64(i64* %y)
1416   %tmp = load i8** %x
1417   store i8* %tmp, i8** %y1
1418   %p = select i1 %flag, i64* %x1, i64* %y
1419   %v = load i64* %p
1420   ret i64 %v
1421 }
1422
1423 define i8* @test85(i1 %flag) {
1424 ; Test that we can't speculate the load around the select. The load of the
1425 ; pointer doesn't load all of the stored integer bits. We could fix this, but it
1426 ; would require endianness checks and other nastiness.
1427 ; CHECK-LABEL: @test85(
1428 ; CHECK:         %[[T:.*]] = load i128*
1429 ; CHECK-NEXT:    store i128 %[[T]], i128*
1430 ; CHECK-NEXT:    %[[X:.*]] = load i8**
1431 ; CHECK-NEXT:    %[[Y:.*]] = load i8**
1432 ; CHECK-NEXT:    %[[V:.*]] = select i1 %flag, i8* %[[X]], i8* %[[Y]]
1433 ; CHECK-NEXT:    ret i8* %[[V]]
1434 entry:
1435   %x = alloca [2 x i8*]
1436   %y = alloca i128
1437   %x1 = bitcast [2 x i8*]* %x to i8**
1438   %x2 = bitcast i8** %x1 to i128*
1439   %y1 = bitcast i128* %y to i8**
1440   call void @scribble_on_i128(i128* %x2)
1441   call void @scribble_on_i128(i128* %y)
1442   %tmp = load i128* %x2
1443   store i128 %tmp, i128* %y
1444   %p = select i1 %flag, i8** %x1, i8** %y1
1445   %v = load i8** %p
1446   ret i8* %v
1447 }
1448
1449 define i128 @test86(i1 %flag) {
1450 ; Test that we can't speculate the load around the select when the integer size
1451 ; is larger than the pointer size. The store of the pointer doesn't store to all
1452 ; the bits of the integer.
1453 ;
1454 ; CHECK-LABEL: @test86(
1455 ; CHECK:         %[[T:.*]] = load i8**
1456 ; CHECK-NEXT:    store i8* %[[T]], i8**
1457 ; CHECK-NEXT:    %[[X:.*]] = load i128*
1458 ; CHECK-NEXT:    %[[Y:.*]] = load i128*
1459 ; CHECK-NEXT:    %[[V:.*]] = select i1 %flag, i128 %[[X]], i128 %[[Y]]
1460 ; CHECK-NEXT:    ret i128 %[[V]]
1461 entry:
1462   %x = alloca [2 x i8*]
1463   %y = alloca i128
1464   %x1 = bitcast [2 x i8*]* %x to i8**
1465   %x2 = bitcast i8** %x1 to i128*
1466   %y1 = bitcast i128* %y to i8**
1467   call void @scribble_on_i128(i128* %x2)
1468   call void @scribble_on_i128(i128* %y)
1469   %tmp = load i8** %x1
1470   store i8* %tmp, i8** %y1
1471   %p = select i1 %flag, i128* %x2, i128* %y
1472   %v = load i128* %p
1473   ret i128 %v
1474 }