Home | History | Annotate | Download | only in InstCombine
      1 ; Tests to make sure elimination of casts is working correctly
      2 ; RUN: opt < %s -instcombine -S | FileCheck %s
      3 target datalayout = "E-p:64:64:64-p1:32:32:32-p2:64:64:64-p3:64:64:64-a0:0:8-f32:32:32-f64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-v64:64:64-v128:128:128-n8:16:32:64"
      4 
      5 @inbuf = external global [32832 x i8]           ; <[32832 x i8]*> [#uses=1]
      6 
      7 define i32 @test1(i32 %A) {
      8         %c1 = bitcast i32 %A to i32             ; <i32> [#uses=1]
      9         %c2 = bitcast i32 %c1 to i32            ; <i32> [#uses=1]
     10         ret i32 %c2
     11 ; CHECK: ret i32 %A
     12 }
     13 
     14 define i64 @test2(i8 %A) {
     15         %c1 = zext i8 %A to i16         ; <i16> [#uses=1]
     16         %c2 = zext i16 %c1 to i32               ; <i32> [#uses=1]
     17         %Ret = zext i32 %c2 to i64              ; <i64> [#uses=1]
     18         ret i64 %Ret
     19 ; CHECK: %Ret = zext i8 %A to i64
     20 ; CHECK: ret i64 %Ret
     21 }
     22 
     23 ; This function should just use bitwise AND
     24 define i64 @test3(i64 %A) {
     25         %c1 = trunc i64 %A to i8                ; <i8> [#uses=1]
     26         %c2 = zext i8 %c1 to i64                ; <i64> [#uses=1]
     27         ret i64 %c2
     28 ; CHECK: %c2 = and i64 %A, 255
     29 ; CHECK: ret i64 %c2
     30 }
     31 
     32 define i32 @test4(i32 %A, i32 %B) {
     33         %COND = icmp slt i32 %A, %B             ; <i1> [#uses=1]
     34         ; Booleans are unsigned integrals
     35         %c = zext i1 %COND to i8                ; <i8> [#uses=1]
     36         ; for the cast elim purpose
     37         %result = zext i8 %c to i32             ; <i32> [#uses=1]
     38         ret i32 %result
     39 ; CHECK: %COND = icmp slt i32 %A, %B
     40 ; CHECK: %result = zext i1 %COND to i32
     41 ; CHECK: ret i32 %result
     42 }
     43 
     44 define i32 @test5(i1 %B) {
     45         ; This cast should get folded into
     46         %c = zext i1 %B to i8           ; <i8> [#uses=1]
     47         ; this cast        
     48         %result = zext i8 %c to i32             ; <i32> [#uses=1]
     49         ret i32 %result
     50 ; CHECK: %result = zext i1 %B to i32
     51 ; CHECK: ret i32 %result
     52 }
     53 
     54 define i32 @test6(i64 %A) {
     55         %c1 = trunc i64 %A to i32               ; <i32> [#uses=1]
     56         %res = bitcast i32 %c1 to i32           ; <i32> [#uses=1]
     57         ret i32 %res
     58 ; CHECK:  trunc i64 %A to i32
     59 ; CHECK-NEXT: ret i32
     60 }
     61 
     62 define i64 @test7(i1 %A) {
     63         %c1 = zext i1 %A to i32         ; <i32> [#uses=1]
     64         %res = sext i32 %c1 to i64              ; <i64> [#uses=1]
     65         ret i64 %res
     66 ; CHECK: %res = zext i1 %A to i64
     67 ; CHECK: ret i64 %res
     68 }
     69 
     70 define i64 @test8(i8 %A) {
     71         %c1 = sext i8 %A to i64         ; <i64> [#uses=1]
     72         %res = bitcast i64 %c1 to i64           ; <i64> [#uses=1]
     73         ret i64 %res
     74 ; CHECK: = sext i8 %A to i64
     75 ; CHECK-NEXT: ret i64
     76 }
     77 
     78 define i16 @test9(i16 %A) {
     79         %c1 = sext i16 %A to i32                ; <i32> [#uses=1]
     80         %c2 = trunc i32 %c1 to i16              ; <i16> [#uses=1]
     81         ret i16 %c2
     82 ; CHECK: ret i16 %A
     83 }
     84 
     85 define i16 @test10(i16 %A) {
     86         %c1 = sext i16 %A to i32                ; <i32> [#uses=1]
     87         %c2 = trunc i32 %c1 to i16              ; <i16> [#uses=1]
     88         ret i16 %c2
     89 ; CHECK: ret i16 %A
     90 }
     91 
     92 declare void @varargs(i32, ...)
     93 
     94 define void @test11(i32* %P) {
     95         %c = bitcast i32* %P to i16*            ; <i16*> [#uses=1]
     96         call void (i32, ...)* @varargs( i32 5, i16* %c )
     97         ret void
     98 ; CHECK: call void (i32, ...)* @varargs(i32 5, i32* %P)
     99 ; CHECK: ret void
    100 }
    101 
    102 define i8* @test13(i64 %A) {
    103         %c = getelementptr [0 x i8]* bitcast ([32832 x i8]* @inbuf to [0 x i8]*), i64 0, i64 %A             ; <i8*> [#uses=1]
    104         ret i8* %c
    105 ; CHECK: %c = getelementptr [32832 x i8]* @inbuf, i64 0, i64 %A
    106 ; CHECK: ret i8* %c
    107 }
    108 
    109 define i1 @test14(i8 %A) {
    110         %c = bitcast i8 %A to i8                ; <i8> [#uses=1]
    111         %X = icmp ult i8 %c, -128               ; <i1> [#uses=1]
    112         ret i1 %X
    113 ; CHECK: %X = icmp sgt i8 %A, -1
    114 ; CHECK: ret i1 %X
    115 }
    116 
    117 
    118 ; This just won't occur when there's no difference between ubyte and sbyte
    119 ;bool %test15(ubyte %A) {
    120 ;        %c = cast ubyte %A to sbyte
    121 ;        %X = setlt sbyte %c, 0   ; setgt %A, 127
    122 ;        ret bool %X
    123 ;}
    124 
    125 define i1 @test16(i32* %P) {
    126         %c = icmp ne i32* %P, null              ; <i1> [#uses=1]
    127         ret i1 %c
    128 ; CHECK: %c = icmp ne i32* %P, null
    129 ; CHECK: ret i1 %c
    130 }
    131 
    132 define i16 @test17(i1 %tmp3) {
    133         %c = zext i1 %tmp3 to i32               ; <i32> [#uses=1]
    134         %t86 = trunc i32 %c to i16              ; <i16> [#uses=1]
    135         ret i16 %t86
    136 ; CHECK: %t86 = zext i1 %tmp3 to i16
    137 ; CHECK: ret i16 %t86
    138 }
    139 
    140 define i16 @test18(i8 %tmp3) {
    141         %c = sext i8 %tmp3 to i32               ; <i32> [#uses=1]
    142         %t86 = trunc i32 %c to i16              ; <i16> [#uses=1]
    143         ret i16 %t86
    144 ; CHECK: %t86 = sext i8 %tmp3 to i16
    145 ; CHECK: ret i16 %t86
    146 }
    147 
    148 define i1 @test19(i32 %X) {
    149         %c = sext i32 %X to i64         ; <i64> [#uses=1]
    150         %Z = icmp slt i64 %c, 12345             ; <i1> [#uses=1]
    151         ret i1 %Z
    152 ; CHECK: %Z = icmp slt i32 %X, 12345
    153 ; CHECK: ret i1 %Z
    154 }
    155 
    156 define i1 @test20(i1 %B) {
    157         %c = zext i1 %B to i32          ; <i32> [#uses=1]
    158         %D = icmp slt i32 %c, -1                ; <i1> [#uses=1]
    159         ;; false
    160         ret i1 %D
    161 ; CHECK: ret i1 false
    162 }
    163 
    164 define i32 @test21(i32 %X) {
    165         %c1 = trunc i32 %X to i8                ; <i8> [#uses=1]
    166         ;; sext -> zext -> and -> nop
    167         %c2 = sext i8 %c1 to i32                ; <i32> [#uses=1]
    168         %RV = and i32 %c2, 255          ; <i32> [#uses=1]
    169         ret i32 %RV
    170 ; CHECK: %c21 = and i32 %X, 255
    171 ; CHECK: ret i32 %c21
    172 }
    173 
    174 define i32 @test22(i32 %X) {
    175         %c1 = trunc i32 %X to i8                ; <i8> [#uses=1]
    176         ;; sext -> zext -> and -> nop
    177         %c2 = sext i8 %c1 to i32                ; <i32> [#uses=1]
    178         %RV = shl i32 %c2, 24           ; <i32> [#uses=1]
    179         ret i32 %RV
    180 ; CHECK: shl i32 %X, 24
    181 ; CHECK-NEXT: ret i32
    182 }
    183 
    184 define i32 @test23(i32 %X) {
    185         ;; Turn into an AND even though X
    186         %c1 = trunc i32 %X to i16               ; <i16> [#uses=1]
    187         ;; and Z are signed.
    188         %c2 = zext i16 %c1 to i32               ; <i32> [#uses=1]
    189         ret i32 %c2
    190 ; CHECK: %c2 = and i32 %X, 65535
    191 ; CHECK: ret i32 %c2
    192 }
    193 
    194 define i1 @test24(i1 %C) {
    195         %X = select i1 %C, i32 14, i32 1234             ; <i32> [#uses=1]
    196         ;; Fold cast into select
    197         %c = icmp ne i32 %X, 0          ; <i1> [#uses=1]
    198         ret i1 %c
    199 ; CHECK: ret i1 true
    200 }
    201 
    202 define void @test25(i32** %P) {
    203         %c = bitcast i32** %P to float**                ; <float**> [#uses=1]
    204         ;; Fold cast into null
    205         store float* null, float** %c
    206         ret void
    207 ; CHECK: store i32* null, i32** %P
    208 ; CHECK: ret void
    209 }
    210 
    211 define i32 @test26(float %F) {
    212         ;; no need to cast from float->double.
    213         %c = fpext float %F to double           ; <double> [#uses=1]
    214         %D = fptosi double %c to i32            ; <i32> [#uses=1]
    215         ret i32 %D
    216 ; CHECK: %D = fptosi float %F to i32
    217 ; CHECK: ret i32 %D
    218 }
    219 
    220 define [4 x float]* @test27([9 x [4 x float]]* %A) {
    221         %c = bitcast [9 x [4 x float]]* %A to [4 x float]*              ; <[4 x float]*> [#uses=1]
    222         ret [4 x float]* %c
    223 ; CHECK: %c = getelementptr inbounds [9 x [4 x float]]* %A, i64 0, i64 0
    224 ; CHECK: ret [4 x float]* %c
    225 }
    226 
    227 define float* @test28([4 x float]* %A) {
    228         %c = bitcast [4 x float]* %A to float*          ; <float*> [#uses=1]
    229         ret float* %c
    230 ; CHECK: %c = getelementptr inbounds [4 x float]* %A, i64 0, i64 0
    231 ; CHECK: ret float* %c
    232 }
    233 
    234 define i32 @test29(i32 %c1, i32 %c2) {
    235         %tmp1 = trunc i32 %c1 to i8             ; <i8> [#uses=1]
    236         %tmp4.mask = trunc i32 %c2 to i8                ; <i8> [#uses=1]
    237         %tmp = or i8 %tmp4.mask, %tmp1          ; <i8> [#uses=1]
    238         %tmp10 = zext i8 %tmp to i32            ; <i32> [#uses=1]
    239         ret i32 %tmp10
    240 ; CHECK: %tmp2 = or i32 %c2, %c1
    241 ; CHECK: %tmp10 = and i32 %tmp2, 255
    242 ; CHECK: ret i32 %tmp10
    243 }
    244 
    245 define i32 @test30(i32 %c1) {
    246         %c2 = trunc i32 %c1 to i8               ; <i8> [#uses=1]
    247         %c3 = xor i8 %c2, 1             ; <i8> [#uses=1]
    248         %c4 = zext i8 %c3 to i32                ; <i32> [#uses=1]
    249         ret i32 %c4
    250 ; CHECK: %c3 = and i32 %c1, 255
    251 ; CHECK: %c4 = xor i32 %c3, 1
    252 ; CHECK: ret i32 %c4
    253 }
    254 
    255 define i1 @test31(i64 %A) {
    256         %B = trunc i64 %A to i32                ; <i32> [#uses=1]
    257         %C = and i32 %B, 42             ; <i32> [#uses=1]
    258         %D = icmp eq i32 %C, 10         ; <i1> [#uses=1]
    259         ret i1 %D
    260 ; CHECK: %C = and i64 %A, 42
    261 ; CHECK: %D = icmp eq i64 %C, 10
    262 ; CHECK: ret i1 %D
    263 }
    264 
    265 define i32 @test33(i32 %c1) {
    266         %x = bitcast i32 %c1 to float           ; <float> [#uses=1]
    267         %y = bitcast float %x to i32            ; <i32> [#uses=1]
    268         ret i32 %y
    269 ; CHECK: ret i32 %c1
    270 }
    271 
    272 define i16 @test34(i16 %a) {
    273         %c1 = zext i16 %a to i32                ; <i32> [#uses=1]
    274         %tmp21 = lshr i32 %c1, 8                ; <i32> [#uses=1]
    275         %c2 = trunc i32 %tmp21 to i16           ; <i16> [#uses=1]
    276         ret i16 %c2
    277 ; CHECK: %tmp21 = lshr i16 %a, 8
    278 ; CHECK: ret i16 %tmp21
    279 }
    280 
    281 define i16 @test35(i16 %a) {
    282         %c1 = bitcast i16 %a to i16             ; <i16> [#uses=1]
    283         %tmp2 = lshr i16 %c1, 8         ; <i16> [#uses=1]
    284         %c2 = bitcast i16 %tmp2 to i16          ; <i16> [#uses=1]
    285         ret i16 %c2
    286 ; CHECK: %tmp2 = lshr i16 %a, 8
    287 ; CHECK: ret i16 %tmp2
    288 }
    289 
    290 ; icmp sgt i32 %a, -1
    291 ; rdar://6480391
    292 define i1 @test36(i32 %a) {
    293         %b = lshr i32 %a, 31
    294         %c = trunc i32 %b to i8
    295         %d = icmp eq i8 %c, 0
    296         ret i1 %d
    297 ; CHECK: %d = icmp sgt i32 %a, -1
    298 ; CHECK: ret i1 %d
    299 }
    300 
    301 ; ret i1 false
    302 define i1 @test37(i32 %a) {
    303         %b = lshr i32 %a, 31
    304         %c = or i32 %b, 512
    305         %d = trunc i32 %c to i8
    306         %e = icmp eq i8 %d, 11
    307         ret i1 %e
    308 ; CHECK: ret i1 false
    309 }
    310 
    311 define i64 @test38(i32 %a) {
    312 	%1 = icmp eq i32 %a, -2
    313 	%2 = zext i1 %1 to i8
    314 	%3 = xor i8 %2, 1
    315 	%4 = zext i8 %3 to i64
    316         ret i64 %4
    317 ; CHECK: %1 = icmp ne i32 %a, -2
    318 ; CHECK: %2 = zext i1 %1 to i64
    319 ; CHECK: ret i64 %2
    320 }
    321 
    322 define i16 @test39(i16 %a) {
    323         %tmp = zext i16 %a to i32
    324         %tmp21 = lshr i32 %tmp, 8
    325         %tmp5 = shl i32 %tmp, 8
    326         %tmp.upgrd.32 = or i32 %tmp21, %tmp5
    327         %tmp.upgrd.3 = trunc i32 %tmp.upgrd.32 to i16
    328         ret i16 %tmp.upgrd.3
    329 ; CHECK-LABEL: @test39(
    330 ; CHECK: %tmp.upgrd.32 = call i16 @llvm.bswap.i16(i16 %a)
    331 ; CHECK: ret i16 %tmp.upgrd.32
    332 }
    333 
    334 define i16 @test40(i16 %a) {
    335         %tmp = zext i16 %a to i32
    336         %tmp21 = lshr i32 %tmp, 9
    337         %tmp5 = shl i32 %tmp, 8
    338         %tmp.upgrd.32 = or i32 %tmp21, %tmp5
    339         %tmp.upgrd.3 = trunc i32 %tmp.upgrd.32 to i16
    340         ret i16 %tmp.upgrd.3
    341 ; CHECK-LABEL: @test40(
    342 ; CHECK: %tmp21 = lshr i16 %a, 9
    343 ; CHECK: %tmp5 = shl i16 %a, 8
    344 ; CHECK: %tmp.upgrd.32 = or i16 %tmp21, %tmp5
    345 ; CHECK: ret i16 %tmp.upgrd.32
    346 }
    347 
    348 ; PR1263
    349 define i32* @test41(i32* %tmp1) {
    350         %tmp64 = bitcast i32* %tmp1 to { i32 }*
    351         %tmp65 = getelementptr { i32 }* %tmp64, i32 0, i32 0
    352         ret i32* %tmp65
    353 ; CHECK-LABEL: @test41(
    354 ; CHECK: ret i32* %tmp1
    355 }
    356 
    357 define i32 @test42(i32 %X) {
    358         %Y = trunc i32 %X to i8         ; <i8> [#uses=1]
    359         %Z = zext i8 %Y to i32          ; <i32> [#uses=1]
    360         ret i32 %Z
    361 ; CHECK-LABEL: @test42(
    362 ; CHECK: %Z = and i32 %X, 255
    363 }
    364 
    365 ; rdar://6598839
    366 define zeroext i64 @test43(i8 zeroext %on_off) nounwind readonly {
    367 	%A = zext i8 %on_off to i32
    368 	%B = add i32 %A, -1
    369 	%C = sext i32 %B to i64
    370 	ret i64 %C  ;; Should be (add (zext i8 -> i64), -1)
    371 ; CHECK-LABEL: @test43(
    372 ; CHECK-NEXT: %A = zext i8 %on_off to i64
    373 ; CHECK-NEXT: %B = add nsw i64 %A, -1
    374 ; CHECK-NEXT: ret i64 %B
    375 }
    376 
    377 define i64 @test44(i8 %T) {
    378  %A = zext i8 %T to i16
    379  %B = or i16 %A, 1234
    380  %C = zext i16 %B to i64
    381  ret i64 %C
    382 ; CHECK-LABEL: @test44(
    383 ; CHECK-NEXT: %A = zext i8 %T to i64
    384 ; CHECK-NEXT: %B = or i64 %A, 1234
    385 ; CHECK-NEXT: ret i64 %B
    386 }
    387 
    388 define i64 @test45(i8 %A, i64 %Q) {
    389  %D = trunc i64 %Q to i32  ;; should be removed
    390  %B = sext i8 %A to i32
    391  %C = or i32 %B, %D
    392  %E = zext i32 %C to i64 
    393  ret i64 %E
    394 ; CHECK-LABEL: @test45(
    395 ; CHECK-NEXT: %B = sext i8 %A to i64
    396 ; CHECK-NEXT: %C = or i64 %B, %Q
    397 ; CHECK-NEXT: %E = and i64 %C, 4294967295
    398 ; CHECK-NEXT: ret i64 %E
    399 }
    400 
    401 
    402 define i64 @test46(i64 %A) {
    403  %B = trunc i64 %A to i32
    404  %C = and i32 %B, 42
    405  %D = shl i32 %C, 8
    406  %E = zext i32 %D to i64 
    407  ret i64 %E
    408 ; CHECK-LABEL: @test46(
    409 ; CHECK-NEXT: %C = shl i64 %A, 8
    410 ; CHECK-NEXT: %D = and i64 %C, 10752
    411 ; CHECK-NEXT: ret i64 %D
    412 }
    413 
    414 define i64 @test47(i8 %A) {
    415  %B = sext i8 %A to i32
    416  %C = or i32 %B, 42
    417  %E = zext i32 %C to i64 
    418  ret i64 %E
    419 ; CHECK-LABEL: @test47(
    420 ; CHECK-NEXT:   %B = sext i8 %A to i64
    421 ; CHECK-NEXT: %C = and i64 %B, 4294967253
    422 ; CHECK-NEXT:  %E = or i64 %C, 42
    423 ; CHECK-NEXT:  ret i64 %E
    424 }
    425 
    426 define i64 @test48(i8 %A, i8 %a) {
    427   %b = zext i8 %a to i32
    428   %B = zext i8 %A to i32
    429   %C = shl i32 %B, 8
    430   %D = or i32 %C, %b
    431   %E = zext i32 %D to i64
    432   ret i64 %E
    433 ; CHECK-LABEL: @test48(
    434 ; CHECK-NEXT: %b = zext i8 %a to i64
    435 ; CHECK-NEXT: %B = zext i8 %A to i64
    436 ; CHECK-NEXT: %C = shl nuw nsw i64 %B, 8
    437 ; CHECK-NEXT: %D = or i64 %C, %b
    438 ; CHECK-NEXT: ret i64 %D
    439 }
    440 
    441 define i64 @test49(i64 %A) {
    442  %B = trunc i64 %A to i32
    443  %C = or i32 %B, 1
    444  %D = sext i32 %C to i64 
    445  ret i64 %D
    446 ; CHECK-LABEL: @test49(
    447 ; CHECK-NEXT: %C = shl i64 %A, 32
    448 ; CHECK-NEXT: ashr exact i64 %C, 32
    449 ; CHECK-NEXT: %D = or i64 {{.*}}, 1
    450 ; CHECK-NEXT: ret i64 %D
    451 }
    452 
    453 define i64 @test50(i64 %A) {
    454   %a = lshr i64 %A, 2
    455   %B = trunc i64 %a to i32
    456   %D = add i32 %B, -1
    457   %E = sext i32 %D to i64
    458   ret i64 %E
    459 ; CHECK-LABEL: @test50(
    460 ; lshr+shl will be handled by DAGCombine.
    461 ; CHECK-NEXT: lshr i64 %A, 2
    462 ; CHECK-NEXT: shl i64 %a, 32
    463 ; CHECK-NEXT: add i64 {{.*}}, -4294967296
    464 ; CHECK-NEXT: %E = ashr exact i64 {{.*}}, 32
    465 ; CHECK-NEXT: ret i64 %E
    466 }
    467 
    468 define i64 @test51(i64 %A, i1 %cond) {
    469   %B = trunc i64 %A to i32
    470   %C = and i32 %B, -2
    471   %D = or i32 %B, 1
    472   %E = select i1 %cond, i32 %C, i32 %D
    473   %F = sext i32 %E to i64
    474   ret i64 %F
    475 ; CHECK-LABEL: @test51(
    476 ; CHECK-NEXT: %C = and i64 %A, 4294967294
    477 ; CHECK-NEXT: %D = or i64 %A, 1
    478 ; CHECK-NEXT: %E = select i1 %cond, i64 %C, i64 %D
    479 ; CHECK-NEXT: %sext = shl i64 %E, 32
    480 ; CHECK-NEXT: %F = ashr exact i64 %sext, 32
    481 ; CHECK-NEXT: ret i64 %F
    482 }
    483 
    484 define i32 @test52(i64 %A) {
    485   %B = trunc i64 %A to i16
    486   %C = or i16 %B, -32574
    487   %D = and i16 %C, -25350
    488   %E = zext i16 %D to i32
    489   ret i32 %E
    490 ; CHECK-LABEL: @test52(
    491 ; CHECK-NEXT: %B = trunc i64 %A to i32
    492 ; CHECK-NEXT: %C = and i32 %B, 7224
    493 ; CHECK-NEXT: %D = or i32 %C, 32962
    494 ; CHECK-NEXT: ret i32 %D
    495 }
    496 
    497 define i64 @test53(i32 %A) {
    498   %B = trunc i32 %A to i16
    499   %C = or i16 %B, -32574
    500   %D = and i16 %C, -25350
    501   %E = zext i16 %D to i64
    502   ret i64 %E
    503 ; CHECK-LABEL: @test53(
    504 ; CHECK-NEXT: %B = zext i32 %A to i64
    505 ; CHECK-NEXT: %C = and i64 %B, 7224
    506 ; CHECK-NEXT: %D = or i64 %C, 32962
    507 ; CHECK-NEXT: ret i64 %D
    508 }
    509 
    510 define i32 @test54(i64 %A) {
    511   %B = trunc i64 %A to i16
    512   %C = or i16 %B, -32574
    513   %D = and i16 %C, -25350
    514   %E = sext i16 %D to i32
    515   ret i32 %E
    516 ; CHECK-LABEL: @test54(
    517 ; CHECK-NEXT: %B = trunc i64 %A to i32
    518 ; CHECK-NEXT: %C = and i32 %B, 7224
    519 ; CHECK-NEXT: %D = or i32 %C, -32574
    520 ; CHECK-NEXT: ret i32 %D
    521 }
    522 
    523 define i64 @test55(i32 %A) {
    524   %B = trunc i32 %A to i16
    525   %C = or i16 %B, -32574
    526   %D = and i16 %C, -25350
    527   %E = sext i16 %D to i64
    528   ret i64 %E
    529 ; CHECK-LABEL: @test55(
    530 ; CHECK-NEXT: %B = zext i32 %A to i64
    531 ; CHECK-NEXT: %C = and i64 %B, 7224
    532 ; CHECK-NEXT: %D = or i64 %C, -32574
    533 ; CHECK-NEXT: ret i64 %D
    534 }
    535 
    536 define i64 @test56(i16 %A) nounwind {
    537   %tmp353 = sext i16 %A to i32
    538   %tmp354 = lshr i32 %tmp353, 5
    539   %tmp355 = zext i32 %tmp354 to i64
    540   ret i64 %tmp355
    541 ; CHECK-LABEL: @test56(
    542 ; CHECK-NEXT: %tmp353 = sext i16 %A to i64
    543 ; CHECK-NEXT: %tmp354 = lshr i64 %tmp353, 5
    544 ; CHECK-NEXT: %tmp355 = and i64 %tmp354, 134217727
    545 ; CHECK-NEXT: ret i64 %tmp355
    546 }
    547 
    548 define i64 @test57(i64 %A) nounwind {
    549  %B = trunc i64 %A to i32
    550  %C = lshr i32 %B, 8
    551  %E = zext i32 %C to i64
    552  ret i64 %E
    553 ; CHECK-LABEL: @test57(
    554 ; CHECK-NEXT: %C = lshr i64 %A, 8 
    555 ; CHECK-NEXT: %E = and i64 %C, 16777215
    556 ; CHECK-NEXT: ret i64 %E
    557 }
    558 
    559 define i64 @test58(i64 %A) nounwind {
    560  %B = trunc i64 %A to i32
    561  %C = lshr i32 %B, 8
    562  %D = or i32 %C, 128
    563  %E = zext i32 %D to i64
    564  ret i64 %E
    565  
    566 ; CHECK-LABEL: @test58(
    567 ; CHECK-NEXT:   %C = lshr i64 %A, 8
    568 ; CHECK-NEXT:   %D = and i64 %C, 16777087
    569 ; CHECK-NEXT:   %E = or i64 %D, 128
    570 ; CHECK-NEXT:   ret i64 %E
    571 }
    572 
    573 define i64 @test59(i8 %A, i8 %B) nounwind {
    574   %C = zext i8 %A to i32
    575   %D = shl i32 %C, 4
    576   %E = and i32 %D, 48
    577   %F = zext i8 %B to i32
    578   %G = lshr i32 %F, 4
    579   %H = or i32 %G, %E
    580   %I = zext i32 %H to i64
    581   ret i64 %I
    582 ; CHECK-LABEL: @test59(
    583 ; CHECK-NEXT:   %C = zext i8 %A to i64
    584 ; CHECK-NOT: i32
    585 ; CHECK:   %F = zext i8 %B to i64
    586 ; CHECK-NOT: i32
    587 ; CHECK:   ret i64 %H
    588 }
    589 
    590 define <3 x i32> @test60(<4 x i32> %call4) nounwind {
    591   %tmp11 = bitcast <4 x i32> %call4 to i128
    592   %tmp9 = trunc i128 %tmp11 to i96
    593   %tmp10 = bitcast i96 %tmp9 to <3 x i32>
    594   ret <3 x i32> %tmp10
    595   
    596 ; CHECK-LABEL: @test60(
    597 ; CHECK-NEXT: shufflevector
    598 ; CHECK-NEXT: ret
    599 }
    600 
    601 define <4 x i32> @test61(<3 x i32> %call4) nounwind {
    602   %tmp11 = bitcast <3 x i32> %call4 to i96
    603   %tmp9 = zext i96 %tmp11 to i128
    604   %tmp10 = bitcast i128 %tmp9 to <4 x i32>
    605   ret <4 x i32> %tmp10
    606 ; CHECK-LABEL: @test61(
    607 ; CHECK-NEXT: shufflevector
    608 ; CHECK-NEXT: ret
    609 }
    610 
    611 define <4 x i32> @test62(<3 x float> %call4) nounwind {
    612   %tmp11 = bitcast <3 x float> %call4 to i96
    613   %tmp9 = zext i96 %tmp11 to i128
    614   %tmp10 = bitcast i128 %tmp9 to <4 x i32>
    615   ret <4 x i32> %tmp10
    616 ; CHECK-LABEL: @test62(
    617 ; CHECK-NEXT: bitcast
    618 ; CHECK-NEXT: shufflevector
    619 ; CHECK-NEXT: ret
    620 }
    621 
    622 ; PR7311 - Don't create invalid IR on scalar->vector cast.
    623 define <2 x float> @test63(i64 %tmp8) nounwind {
    624 entry:
    625   %a = bitcast i64 %tmp8 to <2 x i32>           
    626   %vcvt.i = uitofp <2 x i32> %a to <2 x float>  
    627   ret <2 x float> %vcvt.i
    628 ; CHECK-LABEL: @test63(
    629 ; CHECK: bitcast
    630 ; CHECK: uitofp
    631 }
    632 
    633 define <4 x float> @test64(<4 x float> %c) nounwind {
    634   %t0 = bitcast <4 x float> %c to <4 x i32>
    635   %t1 = bitcast <4 x i32> %t0 to <4 x float>
    636   ret <4 x float> %t1
    637 ; CHECK-LABEL: @test64(
    638 ; CHECK-NEXT: ret <4 x float> %c
    639 }
    640 
    641 define <4 x float> @test65(<4 x float> %c) nounwind {
    642   %t0 = bitcast <4 x float> %c to <2 x double>
    643   %t1 = bitcast <2 x double> %t0 to <4 x float>
    644   ret <4 x float> %t1
    645 ; CHECK-LABEL: @test65(
    646 ; CHECK-NEXT: ret <4 x float> %c
    647 }
    648 
    649 define <2 x float> @test66(<2 x float> %c) nounwind {
    650   %t0 = bitcast <2 x float> %c to double
    651   %t1 = bitcast double %t0 to <2 x float>
    652   ret <2 x float> %t1
    653 ; CHECK-LABEL: @test66(
    654 ; CHECK-NEXT: ret <2 x float> %c
    655 }
    656 
    657 define float @test2c() {
    658   ret float extractelement (<2 x float> bitcast (double bitcast (<2 x float> <float -1.000000e+00, float -1.000000e+00> to double) to <2 x float>), i32 0)
    659 ; CHECK-LABEL: @test2c(
    660 ; CHECK-NOT: extractelement
    661 }
    662 
    663 define i64 @test_mmx(<2 x i32> %c) nounwind {
    664   %A = bitcast <2 x i32> %c to x86_mmx
    665   %B = bitcast x86_mmx %A to <2 x i32>
    666   %C = bitcast <2 x i32> %B to i64
    667   ret i64 %C
    668 ; CHECK-LABEL: @test_mmx(
    669 ; CHECK-NOT: x86_mmx
    670 }
    671 
    672 define i64 @test_mmx_const(<2 x i32> %c) nounwind {
    673   %A = bitcast <2 x i32> zeroinitializer to x86_mmx
    674   %B = bitcast x86_mmx %A to <2 x i32>
    675   %C = bitcast <2 x i32> %B to i64
    676   ret i64 %C
    677 ; CHECK-LABEL: @test_mmx_const(
    678 ; CHECK-NOT: x86_mmx
    679 }
    680 
    681 ; PR12514
    682 define i1 @test67(i1 %a, i32 %b) {
    683   %tmp2 = zext i1 %a to i32
    684   %conv6 = xor i32 %tmp2, 1
    685   %and = and i32 %b, %conv6
    686   %sext = shl nuw nsw i32 %and, 24
    687   %neg.i = xor i32 %sext, -16777216
    688   %conv.i.i = ashr exact i32 %neg.i, 24
    689   %trunc = trunc i32 %conv.i.i to i8
    690   %tobool.i = icmp eq i8 %trunc, 0
    691   ret i1 %tobool.i
    692 ; CHECK-LABEL: @test67(
    693 ; CHECK: ret i1 false
    694 }
    695 
    696 %s = type { i32, i32, i32 }
    697 
    698 define %s @test68(%s *%p, i64 %i) {
    699 ; CHECK-LABEL: @test68(
    700   %o = mul i64 %i, 12
    701   %q = bitcast %s* %p to i8*
    702   %pp = getelementptr inbounds i8* %q, i64 %o
    703 ; CHECK-NEXT: getelementptr %s*
    704   %r = bitcast i8* %pp to %s*
    705   %l = load %s* %r
    706 ; CHECK-NEXT: load %s*
    707   ret %s %l
    708 ; CHECK-NEXT: ret %s
    709 }
    710 
    711 ; addrspacecasts should be eliminated.
    712 define %s @test68_addrspacecast(%s* %p, i64 %i) {
    713 ; CHECK-LABEL: @test68_addrspacecast(
    714 ; CHECK-NEXT: getelementptr %s*
    715 ; CHECK-NEXT: load %s*
    716 ; CHECK-NEXT: ret %s
    717   %o = mul i64 %i, 12
    718   %q = addrspacecast %s* %p to i8 addrspace(2)*
    719   %pp = getelementptr inbounds i8 addrspace(2)* %q, i64 %o
    720   %r = addrspacecast i8 addrspace(2)* %pp to %s*
    721   %l = load %s* %r
    722   ret %s %l
    723 }
    724 
    725 define %s @test68_addrspacecast_2(%s* %p, i64 %i) {
    726 ; CHECK-LABEL: @test68_addrspacecast_2(
    727 ; CHECK-NEXT: getelementptr %s* %p
    728 ; CHECK-NEXT: addrspacecast
    729 ; CHECK-NEXT: load %s addrspace(1)*
    730 ; CHECK-NEXT: ret %s
    731   %o = mul i64 %i, 12
    732   %q = addrspacecast %s* %p to i8 addrspace(2)*
    733   %pp = getelementptr inbounds i8 addrspace(2)* %q, i64 %o
    734   %r = addrspacecast i8 addrspace(2)* %pp to %s addrspace(1)*
    735   %l = load %s addrspace(1)* %r
    736   ret %s %l
    737 }
    738 
    739 define %s @test68_as1(%s addrspace(1)* %p, i32 %i) {
    740 ; CHECK-LABEL: @test68_as1(
    741   %o = mul i32 %i, 12
    742   %q = bitcast %s addrspace(1)* %p to i8 addrspace(1)*
    743   %pp = getelementptr inbounds i8 addrspace(1)* %q, i32 %o
    744 ; CHECK-NEXT: getelementptr %s addrspace(1)*
    745   %r = bitcast i8 addrspace(1)* %pp to %s addrspace(1)*
    746   %l = load %s addrspace(1)* %r
    747 ; CHECK-NEXT: load %s addrspace(1)*
    748   ret %s %l
    749 ; CHECK-NEXT: ret %s
    750 }
    751 
    752 define double @test69(double *%p, i64 %i) {
    753 ; CHECK-LABEL: @test69(
    754   %o = shl nsw i64 %i, 3
    755   %q = bitcast double* %p to i8*
    756   %pp = getelementptr inbounds i8* %q, i64 %o
    757 ; CHECK-NEXT: getelementptr inbounds double*
    758   %r = bitcast i8* %pp to double*
    759   %l = load double* %r
    760 ; CHECK-NEXT: load double*
    761   ret double %l
    762 ; CHECK-NEXT: ret double
    763 }
    764 
    765 define %s @test70(%s *%p, i64 %i) {
    766 ; CHECK-LABEL: @test70(
    767   %o = mul nsw i64 %i, 36
    768 ; CHECK-NEXT: mul nsw i64 %i, 3
    769   %q = bitcast %s* %p to i8*
    770   %pp = getelementptr inbounds i8* %q, i64 %o
    771 ; CHECK-NEXT: getelementptr inbounds %s*
    772   %r = bitcast i8* %pp to %s*
    773   %l = load %s* %r
    774 ; CHECK-NEXT: load %s*
    775   ret %s %l
    776 ; CHECK-NEXT: ret %s
    777 }
    778 
    779 define double @test71(double *%p, i64 %i) {
    780 ; CHECK-LABEL: @test71(
    781   %o = shl i64 %i, 5
    782 ; CHECK-NEXT: shl i64 %i, 2
    783   %q = bitcast double* %p to i8*
    784   %pp = getelementptr i8* %q, i64 %o
    785 ; CHECK-NEXT: getelementptr double*
    786   %r = bitcast i8* %pp to double*
    787   %l = load double* %r
    788 ; CHECK-NEXT: load double*
    789   ret double %l
    790 ; CHECK-NEXT: ret double
    791 }
    792 
    793 define double @test72(double *%p, i32 %i) {
    794 ; CHECK-LABEL: @test72(
    795   %so = mul nsw i32 %i, 8
    796   %o = sext i32 %so to i64
    797 ; CHECK-NEXT: sext i32 %i to i64
    798   %q = bitcast double* %p to i8*
    799   %pp = getelementptr inbounds i8* %q, i64 %o
    800 ; CHECK-NEXT: getelementptr inbounds double*
    801   %r = bitcast i8* %pp to double*
    802   %l = load double* %r
    803 ; CHECK-NEXT: load double*
    804   ret double %l
    805 ; CHECK-NEXT: ret double
    806 }
    807 
    808 define double @test73(double *%p, i128 %i) {
    809 ; CHECK-LABEL: @test73(
    810   %lo = mul nsw i128 %i, 8
    811   %o = trunc i128 %lo to i64
    812 ; CHECK-NEXT: trunc i128 %i to i64
    813   %q = bitcast double* %p to i8*
    814   %pp = getelementptr inbounds i8* %q, i64 %o
    815 ; CHECK-NEXT: getelementptr double*
    816   %r = bitcast i8* %pp to double*
    817   %l = load double* %r
    818 ; CHECK-NEXT: load double*
    819   ret double %l
    820 ; CHECK-NEXT: ret double
    821 }
    822 
    823 define double @test74(double *%p, i64 %i) {
    824 ; CHECK-LABEL: @test74(
    825   %q = bitcast double* %p to i64*
    826   %pp = getelementptr inbounds i64* %q, i64 %i
    827 ; CHECK-NEXT: getelementptr inbounds double*
    828   %r = bitcast i64* %pp to double*
    829   %l = load double* %r
    830 ; CHECK-NEXT: load double*
    831   ret double %l
    832 ; CHECK-NEXT: ret double
    833 }
    834 
    835 define i32* @test75(i32* %p, i32 %x) {
    836 ; CHECK-LABEL: @test75(
    837   %y = shl i32 %x, 3
    838 ; CHECK-NEXT: shl i32 %x, 3
    839   %z = sext i32 %y to i64
    840 ; CHECK-NEXT: sext i32 %y to i64
    841   %q = bitcast i32* %p to i8*
    842   %r = getelementptr i8* %q, i64 %z
    843   %s = bitcast i8* %r to i32*
    844   ret i32* %s
    845 }
    846 
    847 define %s @test76(%s *%p, i64 %i, i64 %j) {
    848 ; CHECK-LABEL: @test76(
    849   %o = mul i64 %i, 12
    850   %o2 = mul nsw i64 %o, %j
    851 ; CHECK-NEXT: %o2 = mul i64 %i, %j
    852   %q = bitcast %s* %p to i8*
    853   %pp = getelementptr inbounds i8* %q, i64 %o2
    854 ; CHECK-NEXT: getelementptr %s* %p, i64 %o2
    855   %r = bitcast i8* %pp to %s*
    856   %l = load %s* %r
    857 ; CHECK-NEXT: load %s*
    858   ret %s %l
    859 ; CHECK-NEXT: ret %s
    860 }
    861 
    862 define %s @test77(%s *%p, i64 %i, i64 %j) {
    863 ; CHECK-LABEL: @test77(
    864   %o = mul nsw i64 %i, 36
    865   %o2 = mul nsw i64 %o, %j
    866 ; CHECK-NEXT: %o = mul nsw i64 %i, 3
    867 ; CHECK-NEXT: %o2 = mul nsw i64 %o, %j
    868   %q = bitcast %s* %p to i8*
    869   %pp = getelementptr inbounds i8* %q, i64 %o2
    870 ; CHECK-NEXT: getelementptr inbounds %s* %p, i64 %o2
    871   %r = bitcast i8* %pp to %s*
    872   %l = load %s* %r
    873 ; CHECK-NEXT: load %s*
    874   ret %s %l
    875 ; CHECK-NEXT: ret %s
    876 }
    877 
    878 define %s @test78(%s *%p, i64 %i, i64 %j, i32 %k, i32 %l, i128 %m, i128 %n) {
    879 ; CHECK-LABEL: @test78(
    880   %a = mul nsw i32 %k, 36
    881 ; CHECK-NEXT: mul nsw i32 %k, 3
    882   %b = mul nsw i32 %a, %l
    883 ; CHECK-NEXT: mul nsw i32 %a, %l
    884   %c = sext i32 %b to i128
    885 ; CHECK-NEXT: sext i32 %b to i128
    886   %d = mul nsw i128 %c, %m
    887 ; CHECK-NEXT: mul nsw i128 %c, %m
    888   %e = mul i128 %d, %n
    889 ; CHECK-NEXT: mul i128 %d, %n
    890   %f = trunc i128 %e to i64
    891 ; CHECK-NEXT: trunc i128 %e to i64
    892   %g = mul nsw i64 %f, %i
    893 ; CHECK-NEXT: mul i64 %f, %i
    894   %h = mul nsw i64 %g, %j
    895 ; CHECK-NEXT: mul i64 %g, %j
    896   %q = bitcast %s* %p to i8*
    897   %pp = getelementptr inbounds i8* %q, i64 %h
    898 ; CHECK-NEXT: getelementptr %s* %p, i64 %h
    899   %r = bitcast i8* %pp to %s*
    900   %load = load %s* %r
    901 ; CHECK-NEXT: load %s*
    902   ret %s %load
    903 ; CHECK-NEXT: ret %s
    904 }
    905 
    906 define %s @test79(%s *%p, i64 %i, i32 %j) {
    907 ; CHECK-LABEL: @test79(
    908   %a = mul nsw i64 %i, 36
    909 ; CHECK: mul nsw i64 %i, 36
    910   %b = trunc i64 %a to i32
    911   %c = mul i32 %b, %j
    912   %q = bitcast %s* %p to i8*
    913 ; CHECK: bitcast
    914   %pp = getelementptr inbounds i8* %q, i32 %c
    915   %r = bitcast i8* %pp to %s*
    916   %l = load %s* %r
    917   ret %s %l
    918 }
    919 
    920 define double @test80([100 x double]* %p, i32 %i) {
    921 ; CHECK-LABEL: @test80(
    922   %tmp = mul nsw i32 %i, 8
    923 ; CHECK-NEXT: sext i32 %i to i64
    924   %q = bitcast [100 x double]* %p to i8*
    925   %pp = getelementptr i8* %q, i32 %tmp
    926 ; CHECK-NEXT: getelementptr [100 x double]*
    927   %r = bitcast i8* %pp to double*
    928   %l = load double* %r
    929 ; CHECK-NEXT: load double*
    930   ret double %l
    931 ; CHECK-NEXT: ret double
    932 }
    933 
    934 define double @test80_addrspacecast([100 x double] addrspace(1)* %p, i32 %i) {
    935 ; CHECK-LABEL: @test80_addrspacecast(
    936 ; CHECK-NEXT: getelementptr [100 x double] addrspace(1)* %p
    937 ; CHECK-NEXT: load double addrspace(1)*
    938 ; CHECK-NEXT: ret double
    939   %tmp = mul nsw i32 %i, 8
    940   %q = addrspacecast [100 x double] addrspace(1)* %p to i8 addrspace(2)*
    941   %pp = getelementptr i8 addrspace(2)* %q, i32 %tmp
    942   %r = addrspacecast i8 addrspace(2)* %pp to double addrspace(1)*
    943   %l = load double addrspace(1)* %r
    944   ret double %l
    945 }
    946 
    947 define double @test80_addrspacecast_2([100 x double] addrspace(1)* %p, i32 %i) {
    948 ; CHECK-LABEL: @test80_addrspacecast_2(
    949 ; CHECK-NEXT: getelementptr [100 x double] addrspace(1)*
    950 ; CHECK-NEXT: addrspacecast double addrspace(1)*
    951 ; CHECK-NEXT: load double addrspace(3)*
    952 ; CHECK-NEXT: ret double
    953   %tmp = mul nsw i32 %i, 8
    954   %q = addrspacecast [100 x double] addrspace(1)* %p to i8 addrspace(2)*
    955   %pp = getelementptr i8 addrspace(2)* %q, i32 %tmp
    956   %r = addrspacecast i8 addrspace(2)* %pp to double addrspace(3)*
    957   %l = load double addrspace(3)* %r
    958   ret double %l
    959 }
    960 
    961 define double @test80_as1([100 x double] addrspace(1)* %p, i16 %i) {
    962 ; CHECK-LABEL: @test80_as1(
    963   %tmp = mul nsw i16 %i, 8
    964 ; CHECK-NEXT: sext i16 %i to i32
    965   %q = bitcast [100 x double] addrspace(1)* %p to i8 addrspace(1)*
    966   %pp = getelementptr i8 addrspace(1)* %q, i16 %tmp
    967 ; CHECK-NEXT: getelementptr [100 x double] addrspace(1)*
    968   %r = bitcast i8 addrspace(1)* %pp to double addrspace(1)*
    969   %l = load double addrspace(1)* %r
    970 ; CHECK-NEXT: load double addrspace(1)*
    971   ret double %l
    972 ; CHECK-NEXT: ret double
    973 }
    974 
    975 define double @test81(double *%p, float %f) {
    976   %i = fptosi float %f to i64
    977   %q = bitcast double* %p to i8*
    978   %pp = getelementptr i8* %q, i64 %i
    979   %r = bitcast i8* %pp to double*
    980   %l = load double* %r
    981   ret double %l
    982 }
    983 
    984 define i64 @test82(i64 %A) nounwind {
    985   %B = trunc i64 %A to i32
    986   %C = lshr i32 %B, 8
    987   %D = shl i32 %C, 9
    988   %E = zext i32 %D to i64
    989   ret i64 %E
    990 
    991 ; CHECK-LABEL: @test82(
    992 ; CHECK-NEXT:   [[REG:%[0-9]*]] = shl i64 %A, 1
    993 ; CHECK-NEXT:   %E = and i64 [[REG]], 4294966784
    994 ; CHECK-NEXT:   ret i64 %E
    995 }
    996 
    997 ; PR15959
    998 define i64 @test83(i16 %a, i64 %k) {
    999   %conv = sext i16 %a to i32
   1000   %sub = add nsw i64 %k, -1
   1001   %sh_prom = trunc i64 %sub to i32
   1002   %shl = shl i32 %conv, %sh_prom
   1003   %sh_prom1 = zext i32 %shl to i64
   1004   ret i64 %sh_prom1
   1005 
   1006 ; CHECK-LABEL: @test83(
   1007 ; CHECK: %sub = add nsw i64 %k, 4294967295
   1008 ; CHECK: %sh_prom = trunc i64 %sub to i32
   1009 ; CHECK: %shl = shl i32 %conv, %sh_prom
   1010 }
   1011