Home | History | Annotate | Download | only in InstCombine
      1 ; RUN: opt < %s -instcombine -S | FileCheck %s
      2 
      3 define i1 @test1(i16 %tmp2) {
      4 ; CHECK-LABEL: @test1
      5 ; CHECK-NEXT:  %tmp = icmp eq i16 %tmp2, 256
      6 ; CHECK-NEXT:  ret i1 %tmp
      7         %tmp10 = call i16 @llvm.bswap.i16( i16 %tmp2 )
      8         %tmp = icmp eq i16 %tmp10, 1
      9         ret i1 %tmp
     10 }
     11 
     12 define i1 @test2(i32 %tmp) {
     13 ; CHECK-LABEL: @test2
     14 ; CHECK-NEXT:  %tmp.upgrd.1 = icmp eq i32 %tmp, 16777216
     15 ; CHECK-NEXT:  ret i1 %tmp.upgrd.1
     16         %tmp34 = tail call i32 @llvm.bswap.i32( i32 %tmp )
     17         %tmp.upgrd.1 = icmp eq i32 %tmp34, 1
     18         ret i1 %tmp.upgrd.1
     19 }
     20 
     21 define i1 @test3(i64 %tmp) {
     22 ; CHECK-LABEL: @test3
     23 ; CHECK-NEXT:  %tmp.upgrd.2 = icmp eq i64 %tmp, 72057594037927936
     24 ; CHECK-NEXT:  ret i1 %tmp.upgrd.2
     25         %tmp34 = tail call i64 @llvm.bswap.i64( i64 %tmp )
     26         %tmp.upgrd.2 = icmp eq i64 %tmp34, 1
     27         ret i1 %tmp.upgrd.2
     28 }
     29 
     30 ; rdar://5992453
     31 ; A & 255
     32 define i32 @test4(i32 %a) nounwind  {
     33 ; CHECK-LABEL: @test4
     34 ; CHECK-NEXT:  %tmp2 = and i32 %a, 255
     35 ; CHECK-NEXT:  ret i32 %tmp2
     36 	%tmp2 = tail call i32 @llvm.bswap.i32( i32 %a )
     37 	%tmp4 = lshr i32 %tmp2, 24
     38 	ret i32 %tmp4
     39 }
     40 
     41 ; A
     42 define i32 @test5(i32 %a) nounwind {
     43 ; CHECK-LABEL: @test5
     44 ; CHECK-NEXT:  ret i32 %a
     45 	%tmp2 = tail call i32 @llvm.bswap.i32( i32 %a )
     46 	%tmp4 = tail call i32 @llvm.bswap.i32( i32 %tmp2 )
     47 	ret i32 %tmp4
     48 }
     49 
     50 ; a >> 24
     51 define i32 @test6(i32 %a) nounwind {
     52 ; CHECK-LABEL: @test6
     53 ; CHECK-NEXT:  %tmp2 = lshr i32 %a, 24
     54 ; CHECK-NEXT:  ret i32 %tmp2
     55 	%tmp2 = tail call i32 @llvm.bswap.i32( i32 %a )
     56 	%tmp4 = and i32 %tmp2, 255
     57 	ret i32 %tmp4
     58 }
     59 
     60 ; PR5284
     61 define i16 @test7(i32 %A) {
     62 ; CHECK-LABEL: @test7
     63 ; CHECK-NEXT:  %1 = lshr i32 %A, 16
     64 ; CHECK-NEXT:  %D = trunc i32 %1 to i16
     65 ; CHECK-NEXT:  ret i16 %D
     66   %B = tail call i32 @llvm.bswap.i32(i32 %A) nounwind
     67   %C = trunc i32 %B to i16
     68   %D = tail call i16 @llvm.bswap.i16(i16 %C) nounwind
     69   ret i16 %D
     70 }
     71 
     72 define i16 @test8(i64 %A) {
     73 ; CHECK-LABEL: @test8
     74 ; CHECK-NEXT:  %1 = lshr i64 %A, 48
     75 ; CHECK-NEXT:  %D = trunc i64 %1 to i16
     76 ; CHECK-NEXT:  ret i16 %D
     77   %B = tail call i64 @llvm.bswap.i64(i64 %A) nounwind 
     78   %C = trunc i64 %B to i16
     79   %D = tail call i16 @llvm.bswap.i16(i16 %C) nounwind
     80   ret i16 %D
     81 }
     82 
     83 ; Misc: Fold bswap(undef) to undef.
     84 define i64 @foo() {
     85 ; CHECK-LABEL: @foo
     86 ; CHECK-NEXT: ret i64 undef
     87   %a = call i64 @llvm.bswap.i64(i64 undef)
     88   ret i64 %a
     89 }
     90 
     91 ; PR15782
     92 ; Fold: OP( BSWAP(x), BSWAP(y) ) -> BSWAP( OP(x, y) )
     93 ; Fold: OP( BSWAP(x), CONSTANT ) -> BSWAP( OP(x, BSWAP(CONSTANT) ) )
     94 define i16 @bs_and16i(i16 %a, i16 %b) #0 {
     95 ; CHECK-LABEL: @bs_and16i
     96 ; CHECK-NEXT:  %1 = and i16 %a, 4391
     97 ; CHECK-NEXT:  %2 = call i16 @llvm.bswap.i16(i16 %1)
     98 ; CHECK-NEXT:  ret i16 %2
     99   %1 = tail call i16 @llvm.bswap.i16(i16 %a)
    100   %2 = and i16 %1, 10001
    101   ret i16 %2
    102 }
    103 
    104 define i16 @bs_and16(i16 %a, i16 %b) #0 {
    105 ; CHECK-LABEL: @bs_and16
    106 ; CHECK-NEXT:  %1 = and i16 %a, %b
    107 ; CHECK-NEXT:  %2 = call i16 @llvm.bswap.i16(i16 %1)
    108 ; CHECK-NEXT:  ret i16 %2
    109   %tmp1 = tail call i16 @llvm.bswap.i16(i16 %a)
    110   %tmp2 = tail call i16 @llvm.bswap.i16(i16 %b)
    111   %tmp3 = and i16 %tmp1, %tmp2
    112   ret i16 %tmp3
    113 }
    114 
    115 define i16 @bs_or16(i16 %a, i16 %b) #0 {
    116 ; CHECK-LABEL: @bs_or16
    117 ; CHECK-NEXT:  %1 = or i16 %a, %b
    118 ; CHECK-NEXT:  %2 = call i16 @llvm.bswap.i16(i16 %1)
    119 ; CHECK-NEXT:  ret i16 %2
    120   %tmp1 = tail call i16 @llvm.bswap.i16(i16 %a)
    121   %tmp2 = tail call i16 @llvm.bswap.i16(i16 %b)
    122   %tmp3 = or i16 %tmp1, %tmp2
    123   ret i16 %tmp3
    124 }
    125 
    126 define i16 @bs_xor16(i16 %a, i16 %b) #0 {
    127 ; CHECK-LABEL: @bs_xor16
    128 ; CHECK-NEXT:  %1 = xor i16 %a, %b
    129 ; CHECK-NEXT:  %2 = call i16 @llvm.bswap.i16(i16 %1)
    130 ; CHECK-NEXT:  ret i16 %2
    131   %tmp1 = tail call i16 @llvm.bswap.i16(i16 %a)
    132   %tmp2 = tail call i16 @llvm.bswap.i16(i16 %b)
    133   %tmp3 = xor i16 %tmp1, %tmp2
    134   ret i16 %tmp3
    135 }
    136 
    137 define i32 @bs_and32i(i32 %a, i32 %b) #0 {
    138 ; CHECK-LABEL: @bs_and32i
    139 ; CHECK-NEXT:  %1 = and i32 %a, -1585053440
    140 ; CHECK-NEXT:  %2 = call i32 @llvm.bswap.i32(i32 %1)
    141 ; CHECK-NEXT:  ret i32 %2
    142   %tmp1 = tail call i32 @llvm.bswap.i32(i32 %a)
    143   %tmp2 = and i32 %tmp1, 100001
    144   ret i32 %tmp2
    145 }
    146 
    147 define i32 @bs_and32(i32 %a, i32 %b) #0 {
    148 ; CHECK-LABEL: @bs_and32
    149 ; CHECK-NEXT:  %1 = and i32 %a, %b
    150 ; CHECK-NEXT:  %2 = call i32 @llvm.bswap.i32(i32 %1)
    151 ; CHECK-NEXT:  ret i32 %2
    152   %tmp1 = tail call i32 @llvm.bswap.i32(i32 %a)
    153   %tmp2 = tail call i32 @llvm.bswap.i32(i32 %b)
    154   %tmp3 = and i32 %tmp1, %tmp2
    155   ret i32 %tmp3
    156 }
    157 
    158 define i32 @bs_or32(i32 %a, i32 %b) #0 {
    159 ; CHECK-LABEL: @bs_or32
    160 ; CHECK-NEXT:  %1 = or i32 %a, %b
    161 ; CHECK-NEXT:  %2 = call i32 @llvm.bswap.i32(i32 %1)
    162 ; CHECK-NEXT:  ret i32 %2
    163   %tmp1 = tail call i32 @llvm.bswap.i32(i32 %a)
    164   %tmp2 = tail call i32 @llvm.bswap.i32(i32 %b)
    165   %tmp3 = or i32 %tmp1, %tmp2
    166   ret i32 %tmp3
    167 }
    168 
    169 define i32 @bs_xor32(i32 %a, i32 %b) #0 {
    170 ; CHECK-LABEL: @bs_xor32
    171 ; CHECK-NEXT:  %1 = xor i32 %a, %b
    172 ; CHECK-NEXT:  %2 = call i32 @llvm.bswap.i32(i32 %1)
    173 ; CHECK-NEXT:  ret i32 %2
    174   %tmp1 = tail call i32 @llvm.bswap.i32(i32 %a)
    175   %tmp2 = tail call i32 @llvm.bswap.i32(i32 %b)
    176   %tmp3 = xor i32 %tmp1, %tmp2
    177   ret i32 %tmp3
    178 }
    179 
    180 define i64 @bs_and64i(i64 %a, i64 %b) #0 {
    181 ; CHECK-LABEL: @bs_and64i
    182 ; CHECK-NEXT:  %1 = and i64 %a, 129085117527228416
    183 ; CHECK-NEXT:  %2 = call i64 @llvm.bswap.i64(i64 %1)
    184 ; CHECK-NEXT:  ret i64 %2
    185   %tmp1 = tail call i64 @llvm.bswap.i64(i64 %a)
    186   %tmp2 = and i64 %tmp1, 1000000001
    187   ret i64 %tmp2
    188 }
    189 
    190 define i64 @bs_and64(i64 %a, i64 %b) #0 {
    191 ; CHECK-LABEL: @bs_and64
    192 ; CHECK-NEXT:  %1 = and i64 %a, %b
    193 ; CHECK-NEXT:  %2 = call i64 @llvm.bswap.i64(i64 %1)
    194 ; CHECK-NEXT:  ret i64 %2
    195   %tmp1 = tail call i64 @llvm.bswap.i64(i64 %a)
    196   %tmp2 = tail call i64 @llvm.bswap.i64(i64 %b)
    197   %tmp3 = and i64 %tmp1, %tmp2
    198   ret i64 %tmp3
    199 }
    200 
    201 define i64 @bs_or64(i64 %a, i64 %b) #0 {
    202 ; CHECK-LABEL: @bs_or64
    203 ; CHECK-NEXT:  %1 = or i64 %a, %b
    204 ; CHECK-NEXT:  %2 = call i64 @llvm.bswap.i64(i64 %1)
    205 ; CHECK-NEXT:  ret i64 %2
    206   %tmp1 = tail call i64 @llvm.bswap.i64(i64 %a)
    207   %tmp2 = tail call i64 @llvm.bswap.i64(i64 %b)
    208   %tmp3 = or i64 %tmp1, %tmp2
    209   ret i64 %tmp3
    210 }
    211 
    212 define i64 @bs_xor64(i64 %a, i64 %b) #0 {
    213 ; CHECK-LABEL: @bs_xor64
    214 ; CHECK-NEXT:  %1 = xor i64 %a, %b
    215 ; CHECK-NEXT:  %2 = call i64 @llvm.bswap.i64(i64 %1)
    216 ; CHECK-NEXT:  ret i64 %2
    217   %tmp1 = tail call i64 @llvm.bswap.i64(i64 %a)
    218   %tmp2 = tail call i64 @llvm.bswap.i64(i64 %b)
    219   %tmp3 = xor i64 %tmp1, %tmp2
    220   ret i64 %tmp3
    221 }
    222 
    223 declare i16 @llvm.bswap.i16(i16)
    224 declare i32 @llvm.bswap.i32(i32)
    225 declare i64 @llvm.bswap.i64(i64)
    226