Home | History | Annotate | Download | only in InstSimplify
      1 ; NOTE: Assertions have been autogenerated by update_test_checks.py
      2 ; RUN: opt < %s -instsimplify -S | FileCheck %s
      3 
      4 define i32 @add1(i32 %x) {
      5 ; CHECK-LABEL: @add1(
      6 ; CHECK:         ret i32 %x
      7 ;
      8 ; (X + -1) + 1 -> X
      9   %l = add i32 %x, -1
     10   %r = add i32 %l, 1
     11   ret i32 %r
     12 }
     13 
     14 define i32 @and1(i32 %x, i32 %y) {
     15 ; CHECK-LABEL: @and1(
     16 ; CHECK:         [[L:%.*]] = and i32 %x, %y
     17 ; CHECK-NEXT:    ret i32 [[L]]
     18 ;
     19 ; (X & Y) & X -> X & Y
     20   %l = and i32 %x, %y
     21   %r = and i32 %l, %x
     22   ret i32 %r
     23 }
     24 
     25 define i32 @and2(i32 %x, i32 %y) {
     26 ; CHECK-LABEL: @and2(
     27 ; CHECK:         [[R:%.*]] = and i32 %x, %y
     28 ; CHECK-NEXT:    ret i32 [[R]]
     29 ;
     30 ; X & (X & Y) -> X & Y
     31   %r = and i32 %x, %y
     32   %l = and i32 %x, %r
     33   ret i32 %l
     34 }
     35 
     36 define i32 @or1(i32 %x, i32 %y) {
     37 ; CHECK-LABEL: @or1(
     38 ; CHECK:         [[L:%.*]] = or i32 %x, %y
     39 ; CHECK-NEXT:    ret i32 [[L]]
     40 ;
     41 ; (X | Y) | X -> X | Y
     42   %l = or i32 %x, %y
     43   %r = or i32 %l, %x
     44   ret i32 %r
     45 }
     46 
     47 define i32 @or2(i32 %x, i32 %y) {
     48 ; CHECK-LABEL: @or2(
     49 ; CHECK:         [[R:%.*]] = or i32 %x, %y
     50 ; CHECK-NEXT:    ret i32 [[R]]
     51 ;
     52 ; X | (X | Y) -> X | Y
     53   %r = or i32 %x, %y
     54   %l = or i32 %x, %r
     55   ret i32 %l
     56 }
     57 
     58 define i32 @xor1(i32 %x, i32 %y) {
     59 ; CHECK-LABEL: @xor1(
     60 ; CHECK:         ret i32 %y
     61 ;
     62 ; (X ^ Y) ^ X = Y
     63   %l = xor i32 %x, %y
     64   %r = xor i32 %l, %x
     65   ret i32 %r
     66 }
     67 
     68 define i32 @xor2(i32 %x, i32 %y) {
     69 ; CHECK-LABEL: @xor2(
     70 ; CHECK:         ret i32 %y
     71 ;
     72 ; X ^ (X ^ Y) = Y
     73   %r = xor i32 %x, %y
     74   %l = xor i32 %x, %r
     75   ret i32 %l
     76 }
     77 
     78 define i32 @sub1(i32 %x, i32 %y) {
     79 ; CHECK-LABEL: @sub1(
     80 ; CHECK:         ret i32 %y
     81 ;
     82   %d = sub i32 %x, %y
     83   %r = sub i32 %x, %d
     84   ret i32 %r
     85 }
     86 
     87 define i32 @sub2(i32 %x) {
     88 ; CHECK-LABEL: @sub2(
     89 ; CHECK:         ret i32 -1
     90 ;
     91 ; X - (X + 1) -> -1
     92   %xp1 = add i32 %x, 1
     93   %r = sub i32 %x, %xp1
     94   ret i32 %r
     95 }
     96 
     97 define i32 @sub3(i32 %x, i32 %y) {
     98 ; CHECK-LABEL: @sub3(
     99 ; CHECK:         ret i32 %x
    100 ;
    101 ; ((X + 1) + Y) - (Y + 1) -> X
    102   %xp1 = add i32 %x, 1
    103   %lhs = add i32 %xp1, %y
    104   %rhs = add i32 %y, 1
    105   %r = sub i32 %lhs, %rhs
    106   ret i32 %r
    107 }
    108 
    109 define i32 @sdiv1(i32 %x, i32 %y) {
    110 ; CHECK-LABEL: @sdiv1(
    111 ; CHECK:         ret i32 %x
    112 ;
    113 ; (no overflow X * Y) / Y -> X
    114   %mul = mul nsw i32 %x, %y
    115   %r = sdiv i32 %mul, %y
    116   ret i32 %r
    117 }
    118 
    119 define i32 @sdiv2(i32 %x, i32 %y) {
    120 ; CHECK-LABEL: @sdiv2(
    121 ; CHECK:         [[DIV:%.*]] = sdiv i32 %x, %y
    122 ; CHECK-NEXT:    ret i32 [[DIV]]
    123 ;
    124 ; (((X / Y) * Y) / Y) -> X / Y
    125   %div = sdiv i32 %x, %y
    126   %mul = mul i32 %div, %y
    127   %r = sdiv i32 %mul, %y
    128   ret i32 %r
    129 }
    130 
    131 define i32 @sdiv3(i32 %x, i32 %y) {
    132 ; CHECK-LABEL: @sdiv3(
    133 ; CHECK:         ret i32 0
    134 ;
    135 ; (X rem Y) / Y -> 0
    136   %rem = srem i32 %x, %y
    137   %div = sdiv i32 %rem, %y
    138   ret i32 %div
    139 }
    140 
    141 define i32 @sdiv4(i32 %x, i32 %y) {
    142 ; CHECK-LABEL: @sdiv4(
    143 ; CHECK:         ret i32 %x
    144 ;
    145 ; (X / Y) * Y -> X if the division is exact
    146   %div = sdiv exact i32 %x, %y
    147   %mul = mul i32 %div, %y
    148   ret i32 %mul
    149 }
    150 
    151 define i32 @sdiv5(i32 %x, i32 %y) {
    152 ; CHECK-LABEL: @sdiv5(
    153 ; CHECK:         ret i32 %x
    154 ;
    155 ; Y * (X / Y) -> X if the division is exact
    156   %div = sdiv exact i32 %x, %y
    157   %mul = mul i32 %y, %div
    158   ret i32 %mul
    159 }
    160 
    161 
    162 define i32 @udiv1(i32 %x, i32 %y) {
    163 ; CHECK-LABEL: @udiv1(
    164 ; CHECK:         ret i32 %x
    165 ;
    166 ; (no overflow X * Y) / Y -> X
    167   %mul = mul nuw i32 %x, %y
    168   %r = udiv i32 %mul, %y
    169   ret i32 %r
    170 }
    171 
    172 define i32 @udiv2(i32 %x, i32 %y) {
    173 ; CHECK-LABEL: @udiv2(
    174 ; CHECK:         [[DIV:%.*]] = udiv i32 %x, %y
    175 ; CHECK-NEXT:    ret i32 [[DIV]]
    176 ;
    177 ; (((X / Y) * Y) / Y) -> X / Y
    178   %div = udiv i32 %x, %y
    179   %mul = mul i32 %div, %y
    180   %r = udiv i32 %mul, %y
    181   ret i32 %r
    182 }
    183 
    184 define i32 @udiv3(i32 %x, i32 %y) {
    185 ; CHECK-LABEL: @udiv3(
    186 ; CHECK:         ret i32 0
    187 ;
    188 ; (X rem Y) / Y -> 0
    189   %rem = urem i32 %x, %y
    190   %div = udiv i32 %rem, %y
    191   ret i32 %div
    192 }
    193 
    194 define i32 @udiv4(i32 %x, i32 %y) {
    195 ; CHECK-LABEL: @udiv4(
    196 ; CHECK:         ret i32 %x
    197 ;
    198 ; (X / Y) * Y -> X if the division is exact
    199   %div = udiv exact i32 %x, %y
    200   %mul = mul i32 %div, %y
    201   ret i32 %mul
    202 }
    203 
    204 define i32 @udiv5(i32 %x, i32 %y) {
    205 ; CHECK-LABEL: @udiv5(
    206 ; CHECK:         ret i32 %x
    207 ;
    208 ; Y * (X / Y) -> X if the division is exact
    209   %div = udiv exact i32 %x, %y
    210   %mul = mul i32 %y, %div
    211   ret i32 %mul
    212 }
    213 
    214 define i16 @trunc1(i32 %x) {
    215 ; CHECK-LABEL: @trunc1(
    216 ; CHECK:         ret i16 1
    217 ;
    218   %y = add i32 %x, 1
    219   %tx = trunc i32 %x to i16
    220   %ty = trunc i32 %y to i16
    221   %d = sub i16 %ty, %tx
    222   ret i16 %d
    223 }
    224