Home | History | Annotate | Download | only in Reassociate
      1 ; RUN: opt < %s -reassociate -S | FileCheck %s
      2 
      3 ; Check that a*c+b*c is turned into (a+b)*c
      4 define <4 x float> @test1(<4 x float> %a, <4 x float> %b, <4 x float> %c) {
      5 ; CHECK-LABEL: @test1
      6 ; CHECK-NEXT: %tmp = fadd fast <4 x float> %b, %a
      7 ; CHECK-NEXT: %tmp1 = fmul fast <4 x float> %tmp, %c
      8 ; CHECK-NEXT: ret <4 x float> %tmp1
      9 
     10   %mul = fmul fast <4 x float> %a, %c
     11   %mul1 = fmul fast <4 x float> %b, %c
     12   %add = fadd fast <4 x float> %mul, %mul1
     13   ret <4 x float> %add
     14 }
     15 
     16 ; Check that a*a*b+a*a*c is turned into a*(a*(b+c)).
     17 define <2 x float> @test2(<2 x float> %a, <2 x float> %b, <2 x float> %c) {
     18 ; CHECK-LABEL: @test2
     19 ; CHECK-NEXT: [[TMP1:%tmp.*]] = fadd fast <2 x float> %c, %b
     20 ; CHECK-NEXT: [[TMP2:%tmp.*]] = fmul fast <2 x float> %a, %a
     21 ; CHECK-NEXT: fmul fast <2 x float> [[TMP2]], [[TMP1]]
     22 ; CHECK-NEXT: ret <2 x float>
     23 
     24   %t0 = fmul fast <2 x float> %a, %b
     25   %t1 = fmul fast <2 x float> %a, %t0
     26   %t2 = fmul fast <2 x float> %a, %c
     27   %t3 = fmul fast <2 x float> %a, %t2
     28   %t4 = fadd fast <2 x float> %t1, %t3
     29   ret <2 x float> %t4
     30 }
     31 
     32 ; Check that a*b+a*c+d is turned into a*(b+c)+d.
     33 define <2 x double> @test3(<2 x double> %a, <2 x double> %b, <2 x double> %c, <2 x double> %d) {
     34 ; CHECK-LABEL: @test3
     35 ; CHECK-NEXT: fadd fast <2 x double> %c, %b
     36 ; CHECK-NEXT: fmul fast <2 x double> %tmp, %a
     37 ; CHECK-NEXT: fadd fast <2 x double> %tmp1, %d
     38 ; CHECK-NEXT: ret <2 x double>
     39 
     40   %t0 = fmul fast <2 x double> %a, %b
     41   %t1 = fmul fast <2 x double> %a, %c
     42   %t2 = fadd fast <2 x double> %t1, %d
     43   %t3 = fadd fast <2 x double> %t0, %t2
     44   ret <2 x double> %t3
     45 }
     46 
     47 ; No fast-math.
     48 define <2 x float> @test4(<2 x float> %A) {
     49 ; CHECK-LABEL: @test4
     50 ; CHECK-NEXT: %X = fadd <2 x float> %A, <float 1.000000e+00, float 1.000000e+00>
     51 ; CHECK-NEXT: %Y = fadd <2 x float> %A, <float 1.000000e+00, float 1.000000e+00>
     52 ; CHECK-NEXT: %R = fsub <2 x float> %X, %Y
     53 ; CHECK-NEXT: ret <2 x float> %R
     54 
     55   %X = fadd <2 x float> %A, < float 1.000000e+00, float 1.000000e+00 >
     56   %Y = fadd <2 x float> %A, < float 1.000000e+00, float 1.000000e+00 >
     57   %R = fsub <2 x float> %X, %Y
     58   ret <2 x float> %R
     59 }
     60 
     61 ; Check 47*X + 47*X -> 94*X.
     62 define <2 x float> @test5(<2 x float> %X) {
     63 ; CHECK-LABEL: @test5
     64 ; CHECK-NEXT: fmul fast <2 x float> %X, <float 9.400000e+01, float 9.400000e+01>
     65 ; CHECK-NEXT: ret <2 x float>
     66 
     67   %Y = fmul fast <2 x float> %X, <float 4.700000e+01, float 4.700000e+01>
     68   %Z = fadd fast <2 x float> %Y, %Y
     69   ret <2 x float> %Z
     70 }
     71 
     72 ; Check X+X+X -> 3*X.
     73 define <2 x float> @test6(<2 x float> %X) {
     74 ; CHECK-LABEL: @test6
     75 ; CHECK-NEXT: fmul fast <2 x float> %X, <float 3.000000e+00, float 3.000000e+00>
     76 ; CHECK-NEXT: ret <2 x float>
     77 
     78   %Y = fadd fast <2 x float> %X ,%X
     79   %Z = fadd fast <2 x float> %Y, %X
     80   ret <2 x float> %Z
     81 }
     82 
     83 ; Check 127*W+50*W -> 177*W.
     84 define <2 x double> @test7(<2 x double> %W) {
     85 ; CHECK-LABEL: @test7
     86 ; CHECK-NEXT: fmul fast <2 x double> %W, <double 1.770000e+02, double 1.770000e+02>
     87 ; CHECK-NEXT: ret <2 x double>
     88 
     89   %X = fmul fast <2 x double> %W, <double 127.0, double 127.0>
     90   %Y = fmul fast <2 x double> %W, <double 50.0, double 50.0>
     91   %Z = fadd fast <2 x double> %Y, %X
     92   ret <2 x double> %Z
     93 }
     94 
     95 ; Check X*12*12 -> X*144.
     96 define <2 x float> @test8(<2 x float> %arg) {
     97 ; CHECK-LABEL: @test8
     98 ; CHECK: fmul fast <2 x float> %arg, <float 1.440000e+02, float 1.440000e+02>
     99 ; CHECK-NEXT: ret <2 x float> %tmp2
    100 
    101   %tmp1 = fmul fast <2 x float> <float 1.200000e+01, float 1.200000e+01>, %arg
    102   %tmp2 = fmul fast <2 x float> %tmp1, <float 1.200000e+01, float 1.200000e+01>
    103   ret <2 x float> %tmp2
    104 }
    105 
    106 ; Check (b+(a+1234))+-a -> b+1234.
    107 define <2 x double> @test9(<2 x double> %b, <2 x double> %a) {
    108 ; CHECK-LABEL: @test9
    109 ; CHECK: fadd fast <2 x double> %b, <double 1.234000e+03, double 1.234000e+03>
    110 ; CHECK-NEXT: ret <2 x double>
    111 
    112   %1 = fadd fast <2 x double> %a, <double 1.234000e+03, double 1.234000e+03>
    113   %2 = fadd fast <2 x double> %b, %1
    114   %3 = fsub fast <2 x double> <double 0.000000e+00, double 0.000000e+00>, %a
    115   %4 = fadd fast <2 x double> %2, %3
    116   ret <2 x double> %4
    117 }
    118 
    119 ; Check -(-(z*40)*a) -> a*40*z.
    120 define <2 x float> @test10(<2 x float> %a, <2 x float> %b, <2 x float> %z) {
    121 ; CHECK-LABEL: @test10
    122 ; CHECK: fmul fast <2 x float> %a, <float 4.000000e+01, float 4.000000e+01>
    123 ; CHECK-NEXT: fmul fast <2 x float> %e, %z
    124 ; CHECK-NEXT: ret <2 x float>
    125 
    126   %d = fmul fast <2 x float> %z, <float 4.000000e+01, float 4.000000e+01>
    127   %c = fsub fast <2 x float> <float 0.000000e+00, float 0.000000e+00>, %d
    128   %e = fmul fast <2 x float> %a, %c
    129   %f = fsub fast <2 x float> <float 0.000000e+00, float 0.000000e+00>, %e
    130   ret <2 x float> %f
    131 }
    132 
    133 ; Check x*y+y*x -> x*y*2.
    134 define <2 x double> @test11(<2 x double> %x, <2 x double> %y) {
    135 ; CHECK-LABEL: @test11
    136 ; CHECK-NEXT: %factor = fmul fast <2 x double> %x, <double 2.000000e+00, double 2.000000e+00>
    137 ; CHECK-NEXT: %tmp1 = fmul fast <2 x double> %factor, %y
    138 ; CHECK-NEXT: ret <2 x double> %tmp1
    139 
    140   %1 = fmul fast <2 x double> %x, %y
    141   %2 = fmul fast <2 x double> %y, %x
    142   %3 = fadd fast <2 x double> %1, %2
    143   ret <2 x double> %3
    144 }
    145 
    146 ; FIXME: shifts should be converted to mul to assist further reassociation.
    147 define <2 x i64> @test12(<2 x i64> %b, <2 x i64> %c) {
    148 ; CHECK-LABEL: @test12
    149 ; CHECK-NEXT:  %mul = mul <2 x i64> %c, %b
    150 ; CHECK-NEXT:  %shl = shl <2 x i64> %mul, <i64 5, i64 5>
    151 ; CHECK-NEXT:  ret <2 x i64> %shl
    152 
    153   %mul = mul <2 x i64> %c, %b
    154   %shl = shl <2 x i64> %mul, <i64 5, i64 5>
    155   ret <2 x i64> %shl
    156 }
    157 
    158 ; FIXME: expressions with a negative const should be canonicalized to assist
    159 ; further reassociation.
    160 ; We would expect (-5*b)+a -> a-(5*b) but only the constant operand is commuted.
    161 define <4 x float> @test13(<4 x float> %a, <4 x float> %b) {
    162 ; CHECK-LABEL: @test13
    163 ; CHECK-NEXT:  %mul = fmul fast <4 x float> %b, <float -5.000000e+00, float -5.000000e+00, float -5.000000e+00, float -5.000000e+00>
    164 ; CHECK-NEXT:  %add = fadd fast <4 x float> %mul, %a
    165 ; CHECK-NEXT:  ret <4 x float> %add
    166 
    167   %mul = fmul fast <4 x float> <float -5.000000e+00, float -5.000000e+00, float -5.000000e+00, float -5.000000e+00>, %b
    168   %add = fadd fast <4 x float> %mul, %a
    169   ret <4 x float> %add
    170 }
    171 
    172 ; Break up subtract to assist further reassociation.
    173 ; Check a+b-c -> a+b+-c.
    174 define <2 x i64> @test14(<2 x i64> %a, <2 x i64> %b, <2 x i64> %c) {
    175 ; CHECK-LABEL: @test14
    176 ; CHECK-NEXT: %add = add <2 x i64> %b, %a
    177 ; CHECK-NEXT: %c.neg = sub <2 x i64> zeroinitializer, %c
    178 ; CHECK-NEXT: %sub = add <2 x i64> %add, %c.neg
    179 ; CHECK-NEXT: ret <2 x i64> %sub
    180 
    181   %add = add <2 x i64> %b, %a
    182   %sub = sub <2 x i64> %add, %c
    183   ret <2 x i64> %sub
    184 }
    185 
    186 define <2 x i32> @test15(<2 x i32> %x, <2 x i32> %y) {
    187 ; CHECK-LABEL: test15
    188 ; CHECK-NEXT: %tmp3 = and <2 x i32> %y, %x
    189 ; CHECK-NEXT: ret <2 x i32> %tmp3
    190 
    191   %tmp1 = and <2 x i32> %x, %y
    192   %tmp2 = and <2 x i32> %y, %x
    193   %tmp3 = and <2 x i32> %tmp1, %tmp2
    194   ret <2 x i32> %tmp3
    195 }
    196 
    197 define <2 x i32> @test16(<2 x i32> %x, <2 x i32> %y) {
    198 ; CHECK-LABEL: test16
    199 ; CHECK-NEXT: %tmp3 = or <2 x i32> %y, %x
    200 ; CHECK-NEXT: ret <2 x i32> %tmp3
    201 
    202   %tmp1 = or <2 x i32> %x, %y
    203   %tmp2 = or <2 x i32> %y, %x
    204   %tmp3 = or <2 x i32> %tmp1, %tmp2
    205   ret <2 x i32> %tmp3
    206 }
    207 
    208 ; FIXME: Optimize vector xor.  Currently only commute operands.
    209 define <2 x i32> @test17(<2 x i32> %x, <2 x i32> %y) {
    210 ; CHECK-LABEL: test17
    211 ; CHECK-NEXT: %tmp1 = xor <2 x i32> %x, %y
    212 ; CHECK-NEXT: %tmp2 = xor <2 x i32> %x, %y
    213 ; CHECK-NEXT: %tmp3 = xor <2 x i32> %tmp1, %tmp2
    214 
    215   %tmp1 = xor <2 x i32> %x, %y
    216   %tmp2 = xor <2 x i32> %y, %x
    217   %tmp3 = xor <2 x i32> %tmp1, %tmp2
    218   ret <2 x i32> %tmp3
    219 }
    220