Home | History | Annotate | Download | only in InstCombine
      1 ; This test makes sure that these instructions are properly eliminated.
      2 ; PR1822
      3 
      4 ; RUN: opt < %s -instcombine -S | FileCheck %s
      5 
      6 define i32 @test1(i32 %A, i32 %B) {
      7         %C = select i1 false, i32 %A, i32 %B            
      8         ret i32 %C
      9 ; CHECK-LABEL: @test1(
     10 ; CHECK: ret i32 %B
     11 }
     12 
     13 define i32 @test2(i32 %A, i32 %B) {
     14         %C = select i1 true, i32 %A, i32 %B             
     15         ret i32 %C
     16 ; CHECK-LABEL: @test2(
     17 ; CHECK: ret i32 %A
     18 }
     19 
     20 
     21 define i32 @test3(i1 %C, i32 %I) {
     22         ; V = I
     23         %V = select i1 %C, i32 %I, i32 %I               
     24         ret i32 %V
     25 ; CHECK-LABEL: @test3(
     26 ; CHECK: ret i32 %I
     27 }
     28 
     29 define i1 @test4(i1 %C) {
     30         ; V = C
     31         %V = select i1 %C, i1 true, i1 false            
     32         ret i1 %V
     33 ; CHECK-LABEL: @test4(
     34 ; CHECK: ret i1 %C
     35 }
     36 
     37 define i1 @test5(i1 %C) {
     38         ; V = !C
     39         %V = select i1 %C, i1 false, i1 true            
     40         ret i1 %V
     41 ; CHECK-LABEL: @test5(
     42 ; CHECK: xor i1 %C, true
     43 ; CHECK: ret i1
     44 }
     45 
     46 define i32 @test6(i1 %C) { 
     47         ; V = cast C to int
     48         %V = select i1 %C, i32 1, i32 0         
     49         ret i32 %V
     50 ; CHECK-LABEL: @test6(
     51 ; CHECK: %V = zext i1 %C to i32
     52 ; CHECK: ret i32 %V
     53 }
     54 
     55 define i1 @test7(i1 %C, i1 %X) {
     56         ; R = or C, X       
     57         %R = select i1 %C, i1 true, i1 %X               
     58         ret i1 %R
     59 ; CHECK-LABEL: @test7(
     60 ; CHECK: %R = or i1 %C, %X
     61 ; CHECK: ret i1 %R
     62 }
     63 
     64 define i1 @test8(i1 %C, i1 %X) {
     65         ; R = and C, X
     66         %R = select i1 %C, i1 %X, i1 false              
     67         ret i1 %R
     68 ; CHECK-LABEL: @test8(
     69 ; CHECK: %R = and i1 %C, %X
     70 ; CHECK: ret i1 %R
     71 }
     72 
     73 define i1 @test9(i1 %C, i1 %X) {
     74         ; R = and !C, X
     75         %R = select i1 %C, i1 false, i1 %X              
     76         ret i1 %R
     77 ; CHECK-LABEL: @test9(
     78 ; CHECK: xor i1 %C, true
     79 ; CHECK: %R = and i1
     80 ; CHECK: ret i1 %R
     81 }
     82 
     83 define i1 @test10(i1 %C, i1 %X) {
     84         ; R = or !C, X
     85         %R = select i1 %C, i1 %X, i1 true               
     86         ret i1 %R
     87 ; CHECK-LABEL: @test10(
     88 ; CHECK: xor i1 %C, true
     89 ; CHECK: %R = or i1
     90 ; CHECK: ret i1 %R
     91 }
     92 
     93 define i32 @test11(i32 %a) {
     94         %C = icmp eq i32 %a, 0          
     95         %R = select i1 %C, i32 0, i32 1         
     96         ret i32 %R
     97 ; CHECK-LABEL: @test11(
     98 ; CHECK: icmp ne i32 %a, 0
     99 ; CHECK: %R = zext i1
    100 ; CHECK: ret i32 %R
    101 }
    102 
    103 define i32 @test12(i1 %cond, i32 %a) {
    104         %b = or i32 %a, 1               
    105         %c = select i1 %cond, i32 %b, i32 %a            
    106         ret i32 %c
    107 ; CHECK-LABEL: @test12(
    108 ; CHECK: %b = zext i1 %cond to i32
    109 ; CHECK: %c = or i32 %b, %a
    110 ; CHECK: ret i32 %c
    111 }
    112 
    113 define i32 @test12a(i1 %cond, i32 %a) {
    114         %b = ashr i32 %a, 1             
    115         %c = select i1 %cond, i32 %b, i32 %a            
    116         ret i32 %c
    117 ; CHECK-LABEL: @test12a(
    118 ; CHECK: %b = zext i1 %cond to i32
    119 ; CHECK: %c = ashr i32 %a, %b
    120 ; CHECK: ret i32 %c
    121 }
    122 
    123 define i32 @test12b(i1 %cond, i32 %a) {
    124         %b = ashr i32 %a, 1             
    125         %c = select i1 %cond, i32 %a, i32 %b            
    126         ret i32 %c
    127 ; CHECK-LABEL: @test12b(
    128 ; CHECK: zext i1 %cond to i32
    129 ; CHECK: %b = xor i32
    130 ; CHECK: %c = ashr i32 %a, %b
    131 ; CHECK: ret i32 %c
    132 }
    133 
    134 define i32 @test13(i32 %a, i32 %b) {
    135         %C = icmp eq i32 %a, %b         
    136         %V = select i1 %C, i32 %a, i32 %b               
    137         ret i32 %V
    138 ; CHECK-LABEL: @test13(
    139 ; CHECK: ret i32 %b
    140 }
    141 
    142 define i32 @test13a(i32 %a, i32 %b) {
    143         %C = icmp ne i32 %a, %b         
    144         %V = select i1 %C, i32 %a, i32 %b               
    145         ret i32 %V
    146 ; CHECK-LABEL: @test13a(
    147 ; CHECK: ret i32 %a
    148 }
    149 
    150 define i32 @test13b(i32 %a, i32 %b) {
    151         %C = icmp eq i32 %a, %b         
    152         %V = select i1 %C, i32 %b, i32 %a               
    153         ret i32 %V
    154 ; CHECK-LABEL: @test13b(
    155 ; CHECK: ret i32 %a
    156 }
    157 
    158 define i1 @test14a(i1 %C, i32 %X) {
    159         %V = select i1 %C, i32 %X, i32 0                
    160         ; (X < 1) | !C
    161         %R = icmp slt i32 %V, 1         
    162         ret i1 %R
    163 ; CHECK-LABEL: @test14a(
    164 ; CHECK: icmp slt i32 %X, 1
    165 ; CHECK: xor i1 %C, true
    166 ; CHECK: or i1
    167 ; CHECK: ret i1 %R
    168 }
    169 
    170 define i1 @test14b(i1 %C, i32 %X) {
    171         %V = select i1 %C, i32 0, i32 %X                
    172         ; (X < 1) | C
    173         %R = icmp slt i32 %V, 1         
    174         ret i1 %R
    175 ; CHECK-LABEL: @test14b(
    176 ; CHECK: icmp slt i32 %X, 1
    177 ; CHECK: or i1
    178 ; CHECK: ret i1 %R
    179 }
    180 
    181 ;; Code sequence for (X & 16) ? 16 : 0
    182 define i32 @test15a(i32 %X) {
    183         %t1 = and i32 %X, 16            
    184         %t2 = icmp eq i32 %t1, 0                
    185         %t3 = select i1 %t2, i32 0, i32 16              
    186         ret i32 %t3
    187 ; CHECK-LABEL: @test15a(
    188 ; CHECK: %t1 = and i32 %X, 16
    189 ; CHECK: ret i32 %t1
    190 }
    191 
    192 ;; Code sequence for (X & 32) ? 0 : 24
    193 define i32 @test15b(i32 %X) {
    194         %t1 = and i32 %X, 32            
    195         %t2 = icmp eq i32 %t1, 0                
    196         %t3 = select i1 %t2, i32 32, i32 0              
    197         ret i32 %t3
    198 ; CHECK-LABEL: @test15b(
    199 ; CHECK: %t1 = and i32 %X, 32
    200 ; CHECK: xor i32 %t1, 32
    201 ; CHECK: ret i32
    202 }
    203 
    204 ;; Alternate code sequence for (X & 16) ? 16 : 0
    205 define i32 @test15c(i32 %X) {
    206         %t1 = and i32 %X, 16            
    207         %t2 = icmp eq i32 %t1, 16               
    208         %t3 = select i1 %t2, i32 16, i32 0              
    209         ret i32 %t3
    210 ; CHECK-LABEL: @test15c(
    211 ; CHECK: %t1 = and i32 %X, 16
    212 ; CHECK: ret i32 %t1
    213 }
    214 
    215 ;; Alternate code sequence for (X & 16) ? 16 : 0
    216 define i32 @test15d(i32 %X) {
    217         %t1 = and i32 %X, 16            
    218         %t2 = icmp ne i32 %t1, 0                
    219         %t3 = select i1 %t2, i32 16, i32 0              
    220         ret i32 %t3
    221 ; CHECK-LABEL: @test15d(
    222 ; CHECK: %t1 = and i32 %X, 16
    223 ; CHECK: ret i32 %t1
    224 }
    225 
    226 ;; (a & 128) ? 256 : 0
    227 define i32 @test15e(i32 %X) {
    228         %t1 = and i32 %X, 128
    229         %t2 = icmp ne i32 %t1, 0
    230         %t3 = select i1 %t2, i32 256, i32 0
    231         ret i32 %t3
    232 ; CHECK-LABEL: @test15e(
    233 ; CHECK: %t1 = shl i32 %X, 1
    234 ; CHECK: and i32 %t1, 256
    235 ; CHECK: ret i32
    236 }
    237 
    238 ;; (a & 128) ? 0 : 256
    239 define i32 @test15f(i32 %X) {
    240         %t1 = and i32 %X, 128
    241         %t2 = icmp ne i32 %t1, 0
    242         %t3 = select i1 %t2, i32 0, i32 256
    243         ret i32 %t3
    244 ; CHECK-LABEL: @test15f(
    245 ; CHECK: %t1 = shl i32 %X, 1
    246 ; CHECK: and i32 %t1, 256
    247 ; CHECK: xor i32 %{{.*}}, 256
    248 ; CHECK: ret i32
    249 }
    250 
    251 ;; (a & 8) ? -1 : -9
    252 define i32 @test15g(i32 %X) {
    253         %t1 = and i32 %X, 8
    254         %t2 = icmp ne i32 %t1, 0
    255         %t3 = select i1 %t2, i32 -1, i32 -9
    256         ret i32 %t3
    257 ; CHECK-LABEL: @test15g(
    258 ; CHECK-NEXT: %1 = or i32 %X, -9
    259 ; CHECK-NEXT: ret i32 %1
    260 }
    261 
    262 ;; (a & 8) ? -9 : -1
    263 define i32 @test15h(i32 %X) {
    264         %t1 = and i32 %X, 8
    265         %t2 = icmp ne i32 %t1, 0
    266         %t3 = select i1 %t2, i32 -9, i32 -1
    267         ret i32 %t3
    268 ; CHECK-LABEL: @test15h(
    269 ; CHECK-NEXT: %1 = or i32 %X, -9
    270 ; CHECK-NEXT: %2 = xor i32 %1, 8
    271 ; CHECK-NEXT: ret i32 %2
    272 }
    273 
    274 ;; (a & 2) ? 577 : 1089
    275 define i32 @test15i(i32 %X) {
    276         %t1 = and i32 %X, 2
    277         %t2 = icmp ne i32 %t1, 0
    278         %t3 = select i1 %t2, i32 577, i32 1089
    279         ret i32 %t3
    280 ; CHECK-LABEL: @test15i(
    281 ; CHECK-NEXT: %t1 = shl i32 %X, 8
    282 ; CHECK-NEXT: %1 = and i32 %t1, 512
    283 ; CHECK-NEXT: %2 = xor i32 %1, 512
    284 ; CHECK-NEXT: %3 = add i32 %2, 577
    285 ; CHECK-NEXT: ret i32 %3
    286 }
    287 
    288 ;; (a & 2) ? 1089 : 577
    289 define i32 @test15j(i32 %X) {
    290         %t1 = and i32 %X, 2
    291         %t2 = icmp ne i32 %t1, 0
    292         %t3 = select i1 %t2, i32 1089, i32 577
    293         ret i32 %t3
    294 ; CHECK-LABEL: @test15j(
    295 ; CHECK-NEXT: %t1 = shl i32 %X, 8
    296 ; CHECK-NEXT: %1 = and i32 %t1, 512
    297 ; CHECK-NEXT: %2 = add i32 %1, 577
    298 ; CHECK-NEXT: ret i32 %2
    299 }
    300 
    301 define i32 @test16(i1 %C, i32* %P) {
    302         %P2 = select i1 %C, i32* %P, i32* null          
    303         %V = load i32* %P2              
    304         ret i32 %V
    305 ; CHECK-LABEL: @test16(
    306 ; CHECK-NEXT: %V = load i32* %P
    307 ; CHECK: ret i32 %V
    308 }
    309 
    310 define i1 @test17(i32* %X, i1 %C) {
    311         %R = select i1 %C, i32* %X, i32* null           
    312         %RV = icmp eq i32* %R, null             
    313         ret i1 %RV
    314 ; CHECK-LABEL: @test17(
    315 ; CHECK: icmp eq i32* %X, null
    316 ; CHECK: xor i1 %C, true
    317 ; CHECK: %RV = or i1
    318 ; CHECK: ret i1 %RV
    319 }
    320 
    321 define i32 @test18(i32 %X, i32 %Y, i1 %C) {
    322         %R = select i1 %C, i32 %X, i32 0                
    323         %V = sdiv i32 %Y, %R            
    324         ret i32 %V
    325 ; CHECK-LABEL: @test18(
    326 ; CHECK: %V = sdiv i32 %Y, %X
    327 ; CHECK: ret i32 %V
    328 }
    329 
    330 define i32 @test19(i32 %x) {
    331         %tmp = icmp ugt i32 %x, 2147483647              
    332         %retval = select i1 %tmp, i32 -1, i32 0         
    333         ret i32 %retval
    334 ; CHECK-LABEL: @test19(
    335 ; CHECK-NEXT: ashr i32 %x, 31
    336 ; CHECK-NEXT: ret i32 
    337 }
    338 
    339 define i32 @test20(i32 %x) {
    340         %tmp = icmp slt i32 %x, 0               
    341         %retval = select i1 %tmp, i32 -1, i32 0         
    342         ret i32 %retval
    343 ; CHECK-LABEL: @test20(
    344 ; CHECK-NEXT: ashr i32 %x, 31
    345 ; CHECK-NEXT: ret i32 
    346 }
    347 
    348 define i64 @test21(i32 %x) {
    349         %tmp = icmp slt i32 %x, 0               
    350         %retval = select i1 %tmp, i64 -1, i64 0         
    351         ret i64 %retval
    352 ; CHECK-LABEL: @test21(
    353 ; CHECK-NEXT: ashr i32 %x, 31
    354 ; CHECK-NEXT: sext i32 
    355 ; CHECK-NEXT: ret i64
    356 }
    357 
    358 define i16 @test22(i32 %x) {
    359         %tmp = icmp slt i32 %x, 0               
    360         %retval = select i1 %tmp, i16 -1, i16 0         
    361         ret i16 %retval
    362 ; CHECK-LABEL: @test22(
    363 ; CHECK-NEXT: ashr i32 %x, 31
    364 ; CHECK-NEXT: trunc i32 
    365 ; CHECK-NEXT: ret i16
    366 }
    367 
    368 define i1 @test23(i1 %a, i1 %b) {
    369         %c = select i1 %a, i1 %b, i1 %a         
    370         ret i1 %c
    371 ; CHECK-LABEL: @test23(
    372 ; CHECK-NEXT: %c = and i1 %a, %b
    373 ; CHECK-NEXT: ret i1 %c
    374 }
    375 
    376 define i1 @test24(i1 %a, i1 %b) {
    377         %c = select i1 %a, i1 %a, i1 %b         
    378         ret i1 %c
    379 ; CHECK-LABEL: @test24(
    380 ; CHECK-NEXT: %c = or i1 %a, %b
    381 ; CHECK-NEXT: ret i1 %c
    382 }
    383 
    384 define i32 @test25(i1 %c)  {
    385 entry:
    386   br i1 %c, label %jump, label %ret
    387 jump:
    388   br label %ret 
    389 ret:
    390   %a = phi i1 [true, %jump], [false, %entry]
    391   %b = select i1 %a, i32 10, i32 20
    392   ret i32 %b
    393 ; CHECK-LABEL: @test25(
    394 ; CHECK: %a = phi i32 [ 10, %jump ], [ 20, %entry ]
    395 ; CHECK-NEXT: ret i32 %a
    396 }
    397 
    398 define i32 @test26(i1 %cond)  {
    399 entry:
    400   br i1 %cond, label %jump, label %ret
    401 jump:
    402   %c = or i1 false, false
    403   br label %ret 
    404 ret:
    405   %a = phi i1 [true, %jump], [%c, %entry]
    406   %b = select i1 %a, i32 10, i32 20
    407   ret i32 %b
    408 ; CHECK-LABEL: @test26(
    409 ; CHECK: %a = phi i32 [ 10, %jump ], [ 20, %entry ]
    410 ; CHECK-NEXT: ret i32 %a
    411 }
    412 
    413 define i32 @test27(i1 %c, i32 %A, i32 %B)  {
    414 entry:
    415   br i1 %c, label %jump, label %ret
    416 jump:
    417   br label %ret 
    418 ret:
    419   %a = phi i1 [true, %jump], [false, %entry]
    420   %b = select i1 %a, i32 %A, i32 %B
    421   ret i32 %b
    422 ; CHECK-LABEL: @test27(
    423 ; CHECK: %a = phi i32 [ %A, %jump ], [ %B, %entry ]
    424 ; CHECK-NEXT: ret i32 %a
    425 }
    426 
    427 define i32 @test28(i1 %cond, i32 %A, i32 %B)  {
    428 entry:
    429   br i1 %cond, label %jump, label %ret
    430 jump:
    431   br label %ret 
    432 ret:
    433   %c = phi i32 [%A, %jump], [%B, %entry]
    434   %a = phi i1 [true, %jump], [false, %entry]
    435   %b = select i1 %a, i32 %A, i32 %c
    436   ret i32 %b
    437 ; CHECK-LABEL: @test28(
    438 ; CHECK: %a = phi i32 [ %A, %jump ], [ %B, %entry ]
    439 ; CHECK-NEXT: ret i32 %a
    440 }
    441 
    442 define i32 @test29(i1 %cond, i32 %A, i32 %B)  {
    443 entry:
    444   br i1 %cond, label %jump, label %ret
    445 jump:
    446   br label %ret 
    447 ret:
    448   %c = phi i32 [%A, %jump], [%B, %entry]
    449   %a = phi i1 [true, %jump], [false, %entry]
    450   br label %next
    451   
    452 next:
    453   %b = select i1 %a, i32 %A, i32 %c
    454   ret i32 %b
    455 ; CHECK-LABEL: @test29(
    456 ; CHECK: %a = phi i32 [ %A, %jump ], [ %B, %entry ]
    457 ; CHECK: ret i32 %a
    458 }
    459 
    460 
    461 ; SMAX(SMAX(x, y), x) -> SMAX(x, y)
    462 define i32 @test30(i32 %x, i32 %y) {
    463   %cmp = icmp sgt i32 %x, %y
    464   %cond = select i1 %cmp, i32 %x, i32 %y
    465   
    466   %cmp5 = icmp sgt i32 %cond, %x
    467   %retval = select i1 %cmp5, i32 %cond, i32 %x
    468   ret i32 %retval
    469 ; CHECK-LABEL: @test30(
    470 ; CHECK: ret i32 %cond
    471 }
    472 
    473 ; UMAX(UMAX(x, y), x) -> UMAX(x, y)
    474 define i32 @test31(i32 %x, i32 %y) {
    475   %cmp = icmp ugt i32 %x, %y 
    476   %cond = select i1 %cmp, i32 %x, i32 %y
    477   %cmp5 = icmp ugt i32 %cond, %x
    478   %retval = select i1 %cmp5, i32 %cond, i32 %x
    479   ret i32 %retval
    480 ; CHECK-LABEL: @test31(
    481 ; CHECK: ret i32 %cond
    482 }
    483 
    484 ; SMIN(SMIN(x, y), x) -> SMIN(x, y)
    485 define i32 @test32(i32 %x, i32 %y) {
    486   %cmp = icmp sgt i32 %x, %y
    487   %cond = select i1 %cmp, i32 %y, i32 %x
    488   %cmp5 = icmp sgt i32 %cond, %x
    489   %retval = select i1 %cmp5, i32 %x, i32 %cond
    490   ret i32 %retval
    491 ; CHECK-LABEL: @test32(
    492 ; CHECK: ret i32 %cond
    493 }
    494 
    495 ; MAX(MIN(x, y), x) -> x
    496 define i32 @test33(i32 %x, i32 %y) {
    497   %cmp = icmp sgt i32 %x, %y
    498   %cond = select i1 %cmp, i32 %y, i32 %x
    499   %cmp5 = icmp sgt i32 %cond, %x
    500   %retval = select i1 %cmp5, i32 %cond, i32 %x
    501   ret i32 %retval
    502 ; CHECK-LABEL: @test33(
    503 ; CHECK: ret i32 %x
    504 }
    505 
    506 ; MIN(MAX(x, y), x) -> x
    507 define i32 @test34(i32 %x, i32 %y) {
    508   %cmp = icmp sgt i32 %x, %y
    509   %cond = select i1 %cmp, i32 %x, i32 %y
    510   %cmp5 = icmp sgt i32 %cond, %x
    511   %retval = select i1 %cmp5, i32 %x, i32 %cond
    512   ret i32 %retval
    513 ; CHECK-LABEL: @test34(
    514 ; CHECK: ret i32 %x
    515 }
    516 
    517 define i32 @test35(i32 %x) {
    518   %cmp = icmp sge i32 %x, 0
    519   %cond = select i1 %cmp, i32 60, i32 100
    520   ret i32 %cond
    521 ; CHECK-LABEL: @test35(
    522 ; CHECK: ashr i32 %x, 31
    523 ; CHECK: and i32 {{.*}}, 40
    524 ; CHECK: add i32 {{.*}}, 60
    525 ; CHECK: ret
    526 }
    527 
    528 define i32 @test36(i32 %x) {
    529   %cmp = icmp slt i32 %x, 0
    530   %cond = select i1 %cmp, i32 60, i32 100
    531   ret i32 %cond
    532 ; CHECK-LABEL: @test36(
    533 ; CHECK: ashr i32 %x, 31
    534 ; CHECK: and i32 {{.*}}, -40
    535 ; CHECK: add i32 {{.*}}, 100
    536 ; CHECK: ret
    537 }
    538 
    539 define i32 @test37(i32 %x) {
    540   %cmp = icmp sgt i32 %x, -1
    541   %cond = select i1 %cmp, i32 1, i32 -1
    542   ret i32 %cond
    543 ; CHECK-LABEL: @test37(
    544 ; CHECK: ashr i32 %x, 31
    545 ; CHECK: or i32 {{.*}}, 1
    546 ; CHECK: ret
    547 }
    548 
    549 define i1 @test38(i1 %cond) {
    550   %zero = alloca i32
    551   %one = alloca i32
    552   %ptr = select i1 %cond, i32* %zero, i32* %one
    553   %isnull = icmp eq i32* %ptr, null
    554   ret i1 %isnull
    555 ; CHECK-LABEL: @test38(
    556 ; CHECK: ret i1 false
    557 }
    558 
    559 define i1 @test39(i1 %cond, double %x) {
    560   %s = select i1 %cond, double %x, double 0x7FF0000000000000 ; RHS = +infty
    561   %cmp = fcmp ule double %x, %s
    562   ret i1 %cmp
    563 ; CHECK-LABEL: @test39(
    564 ; CHECK: ret i1 true
    565 }
    566 
    567 define i1 @test40(i1 %cond) {
    568   %a = alloca i32
    569   %b = alloca i32
    570   %c = alloca i32
    571   %s = select i1 %cond, i32* %a, i32* %b
    572   %r = icmp eq i32* %s, %c
    573   ret i1 %r
    574 ; CHECK-LABEL: @test40(
    575 ; CHECK: ret i1 false
    576 }
    577 
    578 define i32 @test41(i1 %cond, i32 %x, i32 %y) {
    579   %z = and i32 %x, %y
    580   %s = select i1 %cond, i32 %y, i32 %z
    581   %r = and i32 %x, %s
    582   ret i32 %r
    583 ; CHECK-LABEL: @test41(
    584 ; CHECK-NEXT: and i32 %x, %y
    585 ; CHECK-NEXT: ret i32
    586 }
    587 
    588 define i32 @test42(i32 %x, i32 %y) {
    589   %b = add i32 %y, -1
    590   %cond = icmp eq i32 %x, 0
    591   %c = select i1 %cond, i32 %b, i32 %y
    592   ret i32 %c
    593 ; CHECK-LABEL: @test42(
    594 ; CHECK-NEXT: %cond = icmp eq i32 %x, 0
    595 ; CHECK-NEXT: %b = sext i1 %cond to i32
    596 ; CHECK-NEXT: %c = add i32 %b, %y
    597 ; CHECK-NEXT: ret i32 %c
    598 }
    599 
    600 define i64 @test43(i32 %a) nounwind {
    601 	%a_ext = sext i32 %a to i64
    602 	%is_a_nonnegative = icmp sgt i32 %a, -1
    603 	%max = select i1 %is_a_nonnegative, i64 %a_ext, i64 0
    604 	ret i64 %max
    605 ; CHECK-LABEL: @test43(
    606 ; CHECK-NEXT: %a_ext = sext i32 %a to i64
    607 ; CHECK-NEXT: %is_a_nonnegative = icmp slt i64 %a_ext, 0
    608 ; CHECK-NEXT: %max = select i1 %is_a_nonnegative, i64 0, i64 %a_ext
    609 ; CHECK-NEXT: ret i64 %max
    610 }
    611 
    612 define i64 @test44(i32 %a) nounwind {
    613 	%a_ext = sext i32 %a to i64
    614 	%is_a_nonpositive = icmp slt i32 %a, 1
    615 	%min = select i1 %is_a_nonpositive, i64 %a_ext, i64 0
    616 	ret i64 %min
    617 ; CHECK-LABEL: @test44(
    618 ; CHECK-NEXT: %a_ext = sext i32 %a to i64
    619 ; CHECK-NEXT: %is_a_nonpositive = icmp sgt i64 %a_ext, 0
    620 ; CHECK-NEXT: %min = select i1 %is_a_nonpositive, i64 0, i64 %a_ext
    621 ; CHECK-NEXT: ret i64 %min
    622 }
    623 define i64 @test45(i32 %a) nounwind {
    624 	%a_ext = zext i32 %a to i64
    625 	%is_a_nonnegative = icmp ugt i32 %a, 2
    626 	%max = select i1 %is_a_nonnegative, i64 %a_ext, i64 3
    627 	ret i64 %max
    628 ; CHECK-LABEL: @test45(
    629 ; CHECK-NEXT: %a_ext = zext i32 %a to i64
    630 ; CHECK-NEXT: %is_a_nonnegative = icmp ult i64 %a_ext, 3
    631 ; CHECK-NEXT: %max = select i1 %is_a_nonnegative, i64 3, i64 %a_ext
    632 ; CHECK-NEXT: ret i64 %max
    633 }
    634 
    635 define i64 @test46(i32 %a) nounwind {
    636 	%a_ext = zext i32 %a to i64
    637 	%is_a_nonpositive = icmp ult i32 %a, 3
    638 	%min = select i1 %is_a_nonpositive, i64 %a_ext, i64 2
    639 	ret i64 %min
    640 ; CHECK-LABEL: @test46(
    641 ; CHECK-NEXT: %a_ext = zext i32 %a to i64
    642 ; CHECK-NEXT: %is_a_nonpositive = icmp ugt i64 %a_ext, 2
    643 ; CHECK-NEXT: %min = select i1 %is_a_nonpositive, i64 2, i64 %a_ext
    644 ; CHECK-NEXT: ret i64 %min
    645 }
    646 define i64 @test47(i32 %a) nounwind {
    647 	%a_ext = sext i32 %a to i64
    648 	%is_a_nonnegative = icmp ugt i32 %a, 2
    649 	%max = select i1 %is_a_nonnegative, i64 %a_ext, i64 3
    650 	ret i64 %max
    651 ; CHECK-LABEL: @test47(
    652 ; CHECK-NEXT: %a_ext = sext i32 %a to i64
    653 ; CHECK-NEXT: %is_a_nonnegative = icmp ult i64 %a_ext, 3
    654 ; CHECK-NEXT: %max = select i1 %is_a_nonnegative, i64 3, i64 %a_ext
    655 ; CHECK-NEXT: ret i64 %max
    656 }
    657 
    658 define i64 @test48(i32 %a) nounwind {
    659 	%a_ext = sext i32 %a to i64
    660 	%is_a_nonpositive = icmp ult i32 %a, 3
    661 	%min = select i1 %is_a_nonpositive, i64 %a_ext, i64 2
    662 	ret i64 %min
    663 ; CHECK-LABEL: @test48(
    664 ; CHECK-NEXT: %a_ext = sext i32 %a to i64
    665 ; CHECK-NEXT: %is_a_nonpositive = icmp ugt i64 %a_ext, 2
    666 ; CHECK-NEXT: %min = select i1 %is_a_nonpositive, i64 2, i64 %a_ext
    667 ; CHECK-NEXT: ret i64 %min
    668 }
    669 
    670 define i64 @test49(i32 %a) nounwind {
    671 	%a_ext = sext i32 %a to i64
    672 	%is_a_nonpositive = icmp ult i32 %a, 3
    673 	%min = select i1 %is_a_nonpositive, i64 2, i64 %a_ext
    674 	ret i64 %min
    675 ; CHECK-LABEL: @test49(
    676 ; CHECK-NEXT: %a_ext = sext i32 %a to i64
    677 ; CHECK-NEXT: %is_a_nonpositive = icmp ugt i64 %a_ext, 2
    678 ; CHECK-NEXT: %min = select i1 %is_a_nonpositive, i64 %a_ext, i64 2
    679 ; CHECK-NEXT: ret i64 %min
    680 }
    681 define i64 @test50(i32 %a) nounwind {
    682 	%is_a_nonpositive = icmp ult i32 %a, 3
    683 	%a_ext = sext i32 %a to i64
    684 	%min = select i1 %is_a_nonpositive, i64 2, i64 %a_ext
    685 	ret i64 %min
    686 ; CHECK-LABEL: @test50(
    687 ; CHECK-NEXT: %a_ext = sext i32 %a to i64
    688 ; CHECK-NEXT: %is_a_nonpositive = icmp ugt i64 %a_ext, 2
    689 ; CHECK-NEXT: %min = select i1 %is_a_nonpositive, i64 %a_ext, i64 2
    690 ; CHECK-NEXT: ret i64 %min
    691 }
    692 
    693 ; PR8994
    694 
    695 ; This select instruction can't be eliminated because trying to do so would
    696 ; change the number of vector elements. This used to assert.
    697 define i48 @test51(<3 x i1> %icmp, <3 x i16> %tmp) {
    698 ; CHECK-LABEL: @test51(
    699   %select = select <3 x i1> %icmp, <3 x i16> zeroinitializer, <3 x i16> %tmp
    700 ; CHECK: select <3 x i1>
    701   %tmp2 = bitcast <3 x i16> %select to i48
    702   ret i48 %tmp2
    703 }
    704 
    705 ; PR8575
    706 
    707 define i32 @test52(i32 %n, i32 %m) nounwind {
    708 ; CHECK-LABEL: @test52(
    709   %cmp = icmp sgt i32 %n, %m
    710   %. = select i1 %cmp, i32 1, i32 3
    711   %add = add nsw i32 %., 3
    712   %storemerge = select i1 %cmp, i32 %., i32 %add
    713 ; CHECK: select i1 %cmp, i32 1, i32 6
    714   ret i32 %storemerge
    715 }
    716 
    717 ; PR9454
    718 define i32 @test53(i32 %x) nounwind {
    719   %and = and i32 %x, 2
    720   %cmp = icmp eq i32 %and, %x
    721   %sel = select i1 %cmp, i32 2, i32 1
    722   ret i32 %sel
    723 ; CHECK-LABEL: @test53(
    724 ; CHECK: select i1 %cmp
    725 ; CHECK: ret
    726 }
    727 
    728 define i32 @test54(i32 %X, i32 %Y) {
    729   %A = ashr exact i32 %X, %Y
    730   %B = icmp eq i32 %A, 0
    731   %C = select i1 %B, i32 %A, i32 1
    732   ret i32 %C
    733 ; CHECK-LABEL: @test54(
    734 ; CHECK-NOT: ashr
    735 ; CHECK-NOT: select
    736 ; CHECK: icmp ne i32 %X, 0
    737 ; CHECK: zext 
    738 ; CHECK: ret
    739 }
    740 
    741 define i1 @test55(i1 %X, i32 %Y, i32 %Z) {
    742   %A = ashr exact i32 %Y, %Z
    743   %B = select i1 %X, i32 %Y, i32 %A
    744   %C = icmp eq i32 %B, 0
    745   ret i1 %C
    746 ; CHECK-LABEL: @test55(
    747 ; CHECK-NOT: ashr
    748 ; CHECK-NOT: select
    749 ; CHECK: icmp eq
    750 ; CHECK: ret i1
    751 }
    752 
    753 define i32 @test56(i16 %x) nounwind {
    754   %tobool = icmp eq i16 %x, 0
    755   %conv = zext i16 %x to i32
    756   %cond = select i1 %tobool, i32 0, i32 %conv
    757   ret i32 %cond
    758 ; CHECK-LABEL: @test56(
    759 ; CHECK-NEXT: zext
    760 ; CHECK-NEXT: ret
    761 }
    762 
    763 define i32 @test57(i32 %x, i32 %y) nounwind {
    764   %and = and i32 %x, %y
    765   %tobool = icmp eq i32 %x, 0
    766   %.and = select i1 %tobool, i32 0, i32 %and
    767   ret i32 %.and
    768 ; CHECK-LABEL: @test57(
    769 ; CHECK-NEXT: and i32 %x, %y
    770 ; CHECK-NEXT: ret
    771 }
    772 
    773 define i32 @test58(i16 %x) nounwind {
    774   %tobool = icmp ne i16 %x, 1
    775   %conv = zext i16 %x to i32
    776   %cond = select i1 %tobool, i32 %conv, i32 1
    777   ret i32 %cond
    778 ; CHECK-LABEL: @test58(
    779 ; CHECK-NEXT: zext
    780 ; CHECK-NEXT: ret
    781 }
    782 
    783 define i32 @test59(i32 %x, i32 %y) nounwind {
    784   %and = and i32 %x, %y
    785   %tobool = icmp ne i32 %x, %y
    786   %.and = select i1 %tobool, i32 %and, i32 %y
    787   ret i32 %.and
    788 ; CHECK-LABEL: @test59(
    789 ; CHECK-NEXT: and i32 %x, %y
    790 ; CHECK-NEXT: ret
    791 }
    792 
    793 define i1 @test60(i32 %x, i1* %y) nounwind {
    794   %cmp = icmp eq i32 %x, 0
    795   %load = load i1* %y, align 1
    796   %cmp1 = icmp slt i32 %x, 1
    797   %sel = select i1 %cmp, i1 %load, i1 %cmp1
    798   ret i1 %sel
    799 ; CHECK-LABEL: @test60(
    800 ; CHECK: select
    801 }
    802 
    803 @glbl = constant i32 10
    804 define i32 @test61(i32* %ptr) {
    805   %A = load i32* %ptr
    806   %B = icmp eq i32* %ptr, @glbl
    807   %C = select i1 %B, i32 %A, i32 10
    808   ret i32 %C
    809 ; CHECK-LABEL: @test61(
    810 ; CHECK: ret i32 10
    811 }
    812 
    813 define i1 @test62(i1 %A, i1 %B) {
    814         %not = xor i1 %A, true
    815         %C = select i1 %A, i1 %not, i1 %B             
    816         ret i1 %C
    817 ; CHECK-LABEL: @test62(
    818 ; CHECK: %not = xor i1 %A, true
    819 ; CHECK: %C = and i1 %not, %B
    820 ; CHECK: ret i1 %C
    821 }
    822 
    823 define i1 @test63(i1 %A, i1 %B) {
    824         %not = xor i1 %A, true
    825         %C = select i1 %A, i1 %B, i1 %not         
    826         ret i1 %C
    827 ; CHECK-LABEL: @test63(
    828 ; CHECK: %not = xor i1 %A, true
    829 ; CHECK: %C = or i1 %B, %not
    830 ; CHECK: ret i1 %C
    831 }
    832 
    833 ; PR14131
    834 define void @test64(i32 %p, i16 %b) noreturn nounwind {
    835 entry:
    836   %p.addr.0.insert.mask = and i32 %p, -65536
    837   %conv2 = and i32 %p, 65535
    838   br i1 undef, label %lor.rhs, label %lor.end
    839 
    840 lor.rhs:
    841   %p.addr.0.extract.trunc = trunc i32 %p.addr.0.insert.mask to i16
    842   %phitmp = zext i16 %p.addr.0.extract.trunc to i32
    843   br label %lor.end
    844 
    845 lor.end:
    846   %t.1 = phi i32 [ 0, %entry ], [ %phitmp, %lor.rhs ]
    847   %conv6 = zext i16 %b to i32
    848   %div = udiv i32 %conv6, %t.1
    849   %tobool8 = icmp eq i32 %div, 0
    850   %cmp = icmp eq i32 %t.1, 0
    851   %cmp12 = icmp ult i32 %conv2, 2
    852   %cmp.sink = select i1 %tobool8, i1 %cmp12, i1 %cmp
    853   br i1 %cmp.sink, label %cond.end17, label %cond.false16
    854 
    855 cond.false16:
    856   br label %cond.end17
    857 
    858 cond.end17:
    859   br label %while.body
    860 
    861 while.body:
    862   br label %while.body
    863 ; CHECK-LABEL: @test64(
    864 ; CHECK-NOT: select
    865 }
    866 
    867 ; CHECK-LABEL: @select_icmp_eq_and_1_0_or_2(
    868 ; CHECK-NEXT: [[SHL:%[a-z0-9]+]] = shl i32 %x, 1
    869 ; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i32 [[SHL]], 2
    870 ; CHECK-NEXT: [[OR:%[a-z0-9]+]] = or i32 [[AND]], %y
    871 ; CHECK-NEXT: ret i32 [[OR]]
    872 define i32 @select_icmp_eq_and_1_0_or_2(i32 %x, i32 %y) {
    873   %and = and i32 %x, 1
    874   %cmp = icmp eq i32 %and, 0
    875   %or = or i32 %y, 2
    876   %select = select i1 %cmp, i32 %y, i32 %or
    877   ret i32 %select
    878 }
    879 
    880 ; CHECK-LABEL: @select_icmp_eq_and_32_0_or_8(
    881 ; CHECK-NEXT: [[LSHR:%[a-z0-9]+]] = lshr i32 %x, 2
    882 ; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i32 [[LSHR]], 8
    883 ; CHECK-NEXT: [[OR:%[a-z0-9]+]] = or i32 [[AND]], %y
    884 ; CHECK-NEXT: ret i32 [[OR]]
    885 define i32 @select_icmp_eq_and_32_0_or_8(i32 %x, i32 %y) {
    886   %and = and i32 %x, 32
    887   %cmp = icmp eq i32 %and, 0
    888   %or = or i32 %y, 8
    889   %select = select i1 %cmp, i32 %y, i32 %or
    890   ret i32 %select
    891 }
    892 
    893 ; CHECK-LABEL: @select_icmp_ne_0_and_4096_or_4096(
    894 ; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i32 %x, 4096
    895 ; CHECK-NEXT: [[XOR:%[a-z0-9]+]] = xor i32 [[AND]], 4096
    896 ; CHECK-NEXT: [[OR:%[a-z0-9]+]] = or i32 [[XOR]], %y
    897 ; CHECK-NEXT: ret i32 [[OR]]
    898 define i32 @select_icmp_ne_0_and_4096_or_4096(i32 %x, i32 %y) {
    899   %and = and i32 %x, 4096
    900   %cmp = icmp ne i32 0, %and
    901   %or = or i32 %y, 4096
    902   %select = select i1 %cmp, i32 %y, i32 %or
    903   ret i32 %select
    904 }
    905 
    906 ; CHECK-LABEL: @select_icmp_eq_and_4096_0_or_4096(
    907 ; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i32 %x, 4096
    908 ; CHECK-NEXT: [[OR:%[a-z0-9]+]] = or i32 [[AND]], %y
    909 ; CHECK-NEXT: ret i32 [[OR]]
    910 define i32 @select_icmp_eq_and_4096_0_or_4096(i32 %x, i32 %y) {
    911   %and = and i32 %x, 4096
    912   %cmp = icmp eq i32 %and, 0
    913   %or = or i32 %y, 4096
    914   %select = select i1 %cmp, i32 %y, i32 %or
    915   ret i32 %select
    916 }
    917 
    918 ; CHECK-LABEL: @select_icmp_eq_0_and_1_or_1(
    919 ; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i64 %x, 1
    920 ; CHECK-NEXT: [[ZEXT:%[a-z0-9]+]] = trunc i64 [[AND]] to i32
    921 ; CHECK-NEXT: [[OR:%[a-z0-9]+]] = or i32 [[XOR]], %y
    922 ; CHECK-NEXT: ret i32 [[OR]]
    923 define i32 @select_icmp_eq_0_and_1_or_1(i64 %x, i32 %y) {
    924   %and = and i64 %x, 1
    925   %cmp = icmp eq i64 %and, 0
    926   %or = or i32 %y, 1
    927   %select = select i1 %cmp, i32 %y, i32 %or
    928   ret i32 %select
    929 }
    930 
    931 ; CHECK-LABEL: @select_icmp_ne_0_and_4096_or_32(
    932 ; CHECK-NEXT: [[LSHR:%[a-z0-9]+]] = lshr i32 %x, 7
    933 ; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i32 [[LSHR]], 32
    934 ; CHECK-NEXT: [[XOR:%[a-z0-9]+]] = xor i32 [[AND]], 32
    935 ; CHECK-NEXT: [[OR:%[a-z0-9]+]] = or i32 [[XOR]], %y
    936 ; CHECK-NEXT: ret i32 [[OR]]
    937 define i32 @select_icmp_ne_0_and_4096_or_32(i32 %x, i32 %y) {
    938   %and = and i32 %x, 4096
    939   %cmp = icmp ne i32 0, %and
    940   %or = or i32 %y, 32
    941   %select = select i1 %cmp, i32 %y, i32 %or
    942   ret i32 %select
    943 }
    944 
    945 ; CHECK-LABEL: @select_icmp_ne_0_and_32_or_4096(
    946 ; CHECK-NEXT: [[SHL:%[a-z0-9]+]] = shl i32 %x, 7
    947 ; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i32 [[SHL]], 4096
    948 ; CHECK-NEXT: [[XOR:%[a-z0-9]+]] = xor i32 [[AND]], 4096
    949 ; CHECK-NEXT: [[OR:%[a-z0-9]+]] = or i32 [[XOR]], %y
    950 ; CHECK-NEXT: ret i32 [[OR]]
    951 define i32 @select_icmp_ne_0_and_32_or_4096(i32 %x, i32 %y) {
    952   %and = and i32 %x, 32
    953   %cmp = icmp ne i32 0, %and
    954   %or = or i32 %y, 4096
    955   %select = select i1 %cmp, i32 %y, i32 %or
    956   ret i32 %select
    957 }
    958 
    959 ; CHECK-LABEL: @select_icmp_ne_0_and_1073741824_or_8(
    960 ; CHECK-NEXT: [[LSHR:%[a-z0-9]+]] = lshr i32 %x, 27
    961 ; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i32 [[LSHR]], 8
    962 ; CHECK-NEXT: [[TRUNC:%[a-z0-9]+]] = trunc i32 [[AND]] to i8
    963 ; CHECK-NEXT: [[XOR:%[a-z0-9]+]] = xor i8 [[TRUNC]], 8
    964 ; CHECK-NEXT: [[OR:%[a-z0-9]+]] = or i8 [[XOR]], %y
    965 ; CHECK-NEXT: ret i8 [[OR]]
    966 define i8 @select_icmp_ne_0_and_1073741824_or_8(i32 %x, i8 %y) {
    967   %and = and i32 %x, 1073741824
    968   %cmp = icmp ne i32 0, %and
    969   %or = or i8 %y, 8
    970   %select = select i1 %cmp, i8 %y, i8 %or
    971   ret i8 %select
    972 }
    973 
    974 ; CHECK-LABEL: @select_icmp_ne_0_and_8_or_1073741824(
    975 ; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i8 %x, 8
    976 ; CHECK-NEXT: [[ZEXT:%[a-z0-9]+]] = zext i8 [[AND]] to i32
    977 ; CHECK-NEXT: [[SHL:%[a-z0-9]+]] = shl nuw nsw i32 [[ZEXT]], 27
    978 ; CHECK-NEXT: [[XOR:%[a-z0-9]+]] = xor i32 [[SHL]], 1073741824
    979 ; CHECK-NEXT: [[OR:%[a-z0-9]+]] = or i32 [[XOR]], %y
    980 ; CHECK-NEXT: ret i32 [[OR]]
    981 define i32 @select_icmp_ne_0_and_8_or_1073741824(i8 %x, i32 %y) {
    982   %and = and i8 %x, 8
    983   %cmp = icmp ne i8 0, %and
    984   %or = or i32 %y, 1073741824
    985   %select = select i1 %cmp, i32 %y, i32 %or
    986   ret i32 %select
    987 }
    988 
    989 define i32 @test65(i64 %x) {
    990   %1 = and i64 %x, 16
    991   %2 = icmp ne i64 %1, 0
    992   %3 = select i1 %2, i32 40, i32 42
    993   ret i32 %3
    994 
    995 ; CHECK-LABEL: @test65(
    996 ; CHECK: and i64 %x, 16
    997 ; CHECK: trunc i64 %1 to i32
    998 ; CHECK: lshr exact i32 %2, 3
    999 ; CHECK: xor i32 %3, 42
   1000 }
   1001 
   1002 define i32 @test66(i64 %x) {
   1003   %1 = and i64 %x, 4294967296
   1004   %2 = icmp ne i64 %1, 0
   1005   %3 = select i1 %2, i32 40, i32 42
   1006   ret i32 %3
   1007 
   1008 ; CHECK-LABEL: @test66(
   1009 ; CHECK: select
   1010 }
   1011 
   1012 define i32 @test67(i16 %x) {
   1013   %1 = and i16 %x, 4
   1014   %2 = icmp ne i16 %1, 0
   1015   %3 = select i1 %2, i32 40, i32 42
   1016   ret i32 %3
   1017 
   1018 ; CHECK-LABEL: @test67(
   1019 ; CHECK: and i16 %x, 4
   1020 ; CHECK: zext i16 %1 to i32
   1021 ; CHECK: lshr exact i32 %2, 1
   1022 ; CHECK: xor i32 %3, 42
   1023 }
   1024