Home | History | Annotate | Download | only in Reassociate
      1 ; RUN: opt < %s -reassociate -gvn -instcombine -S | FileCheck %s
      2 
      3 define i32 @test1(i32 %arg) {
      4   %tmp1 = sub i32 -12, %arg
      5   %tmp2 = add i32 %tmp1, 12
      6   ret i32 %tmp2
      7 
      8 ; CHECK-LABEL: @test1
      9 ; CHECK-NEXT: sub i32 0, %arg
     10 ; CHECK-NEXT: ret i32
     11 }
     12 
     13 define i32 @test2(i32 %reg109, i32 %reg1111) {
     14   %reg115 = add i32 %reg109, -30
     15   %reg116 = add i32 %reg115, %reg1111
     16   %reg117 = add i32 %reg116, 30
     17   ret i32 %reg117
     18 
     19 ; CHECK-LABEL: @test2
     20 ; CHECK-NEXT: %reg117 = add i32 %reg1111, %reg109
     21 ; CHECK-NEXT: ret i32 %reg117
     22 }
     23 
     24 @e = external global i32
     25 @a = external global i32
     26 @b = external global i32
     27 @c = external global i32
     28 @f = external global i32
     29 
     30 define void @test3() {
     31   %A = load i32* @a
     32   %B = load i32* @b
     33   %C = load i32* @c
     34   %t1 = add i32 %A, %B
     35   %t2 = add i32 %t1, %C
     36   %t3 = add i32 %C, %A
     37   %t4 = add i32 %t3, %B
     38   ; e = (a+b)+c;
     39   store i32 %t2, i32* @e
     40   ; f = (a+c)+b
     41   store i32 %t4, i32* @f
     42   ret void
     43 
     44 ; CHECK-LABEL: @test3
     45 ; CHECK: add i32
     46 ; CHECK: add i32
     47 ; CHECK-NOT: add i32
     48 ; CHECK: ret void
     49 }
     50 
     51 define void @test4() {
     52   %A = load i32* @a
     53   %B = load i32* @b
     54   %C = load i32* @c
     55   %t1 = add i32 %A, %B
     56   %t2 = add i32 %t1, %C
     57   %t3 = add i32 %C, %A
     58   %t4 = add i32 %t3, %B
     59   ; e = c+(a+b)
     60   store i32 %t2, i32* @e
     61   ; f = (c+a)+b
     62   store i32 %t4, i32* @f
     63   ret void
     64 
     65 ; CHECK-LABEL: @test4
     66 ; CHECK: add i32
     67 ; CHECK: add i32
     68 ; CHECK-NOT: add i32
     69 ; CHECK: ret void
     70 }
     71 
     72 define void @test5() {
     73   %A = load i32* @a
     74   %B = load i32* @b
     75   %C = load i32* @c
     76   %t1 = add i32 %B, %A
     77   %t2 = add i32 %t1, %C
     78   %t3 = add i32 %C, %A
     79   %t4 = add i32 %t3, %B
     80   ; e = c+(b+a)
     81   store i32 %t2, i32* @e
     82   ; f = (c+a)+b
     83   store i32 %t4, i32* @f
     84   ret void
     85 
     86 ; CHECK-LABEL: @test5
     87 ; CHECK: add i32
     88 ; CHECK: add i32
     89 ; CHECK-NOT: add i32
     90 ; CHECK: ret void
     91 }
     92 
     93 define i32 @test6() {
     94   %tmp.0 = load i32* @a
     95   %tmp.1 = load i32* @b
     96   ; (a+b)
     97   %tmp.2 = add i32 %tmp.0, %tmp.1
     98   %tmp.4 = load i32* @c
     99   ; (a+b)+c
    100   %tmp.5 = add i32 %tmp.2, %tmp.4
    101   ; (a+c)
    102   %tmp.8 = add i32 %tmp.0, %tmp.4
    103   ; (a+c)+b
    104   %tmp.11 = add i32 %tmp.8, %tmp.1
    105   ; X ^ X = 0
    106   %RV = xor i32 %tmp.5, %tmp.11
    107   ret i32 %RV
    108 
    109 ; CHECK-LABEL: @test6
    110 ; CHECK: ret i32 0
    111 }
    112 
    113 ; This should be one add and two multiplies.
    114 define i32 @test7(i32 %A, i32 %B, i32 %C) {
    115   ; A*A*B + A*C*A
    116   %aa = mul i32 %A, %A
    117   %aab = mul i32 %aa, %B
    118   %ac = mul i32 %A, %C
    119   %aac = mul i32 %ac, %A
    120   %r = add i32 %aab, %aac
    121   ret i32 %r
    122 
    123 ; CHECK-LABEL: @test7
    124 ; CHECK-NEXT: add i32 %C, %B
    125 ; CHECK-NEXT: mul i32 
    126 ; CHECK-NEXT: mul i32 
    127 ; CHECK-NEXT: ret i32 
    128 }
    129 
    130 define i32 @test8(i32 %X, i32 %Y, i32 %Z) {
    131   %A = sub i32 0, %X
    132   %B = mul i32 %A, %Y
    133   ; (-X)*Y + Z -> Z-X*Y
    134   %C = add i32 %B, %Z
    135   ret i32 %C
    136 
    137 ; CHECK-LABEL: @test8
    138 ; CHECK-NEXT: %A = mul i32 %Y, %X
    139 ; CHECK-NEXT: %C = sub i32 %Z, %A
    140 ; CHECK-NEXT: ret i32 %C
    141 }
    142 
    143 ; PR5458
    144 define i32 @test9(i32 %X) {
    145   %Y = mul i32 %X, 47
    146   %Z = add i32 %Y, %Y
    147   ret i32 %Z
    148 ; CHECK-LABEL: @test9
    149 ; CHECK-NEXT: mul i32 %X, 94
    150 ; CHECK-NEXT: ret i32
    151 }
    152 
    153 define i32 @test10(i32 %X) {
    154   %Y = add i32 %X ,%X
    155   %Z = add i32 %Y, %X
    156   ret i32 %Z
    157 ; CHECK-LABEL: @test10
    158 ; CHECK-NEXT: mul i32 %X, 3
    159 ; CHECK-NEXT: ret i32
    160 }
    161 
    162 define i32 @test11(i32 %W) {
    163   %X = mul i32 %W, 127
    164   %Y = add i32 %X ,%X
    165   %Z = add i32 %Y, %X
    166   ret i32 %Z
    167 ; CHECK-LABEL: @test11
    168 ; CHECK-NEXT: mul i32 %W, 381
    169 ; CHECK-NEXT: ret i32
    170 }
    171 
    172 define i32 @test12(i32 %X) {
    173   %A = sub i32 1, %X
    174   %B = sub i32 2, %X
    175   %C = sub i32 3, %X
    176   %Y = add i32 %A ,%B
    177   %Z = add i32 %Y, %C
    178   ret i32 %Z
    179 ; CHECK-LABEL: @test12
    180 ; CHECK-NEXT: mul i32 %X, -3
    181 ; CHECK-NEXT: add i32{{.*}}, 6
    182 ; CHECK-NEXT: ret i32
    183 }
    184 
    185 define i32 @test13(i32 %X1, i32 %X2, i32 %X3) {
    186   %A = sub i32 0, %X1
    187   %B = mul i32 %A, %X2   ; -X1*X2
    188   %C = mul i32 %X1, %X3  ; X1*X3
    189   %D = add i32 %B, %C    ; -X1*X2 + X1*X3 -> X1*(X3-X2)
    190   ret i32 %D
    191 ; CHECK-LABEL: @test13
    192 ; CHECK-NEXT: sub i32 %X3, %X2
    193 ; CHECK-NEXT: mul i32 {{.*}}, %X1
    194 ; CHECK-NEXT: ret i32
    195 }
    196 
    197 ; PR5359
    198 define i32 @test14(i32 %X1, i32 %X2) {
    199   %B = mul i32 %X1, 47   ; X1*47
    200   %C = mul i32 %X2, -47  ; X2*-47
    201   %D = add i32 %B, %C    ; X1*47 + X2*-47 -> 47*(X1-X2)
    202   ret i32 %D
    203 
    204 ; CHECK-LABEL: @test14
    205 ; CHECK-NEXT: sub i32 %X1, %X2
    206 ; CHECK-NEXT: mul i32 %tmp, 47
    207 ; CHECK-NEXT: ret i32
    208 }
    209 
    210 ; Do not reassociate expressions of type i1
    211 define i32 @test15(i32 %X1, i32 %X2, i32 %X3) {
    212   %A = icmp ne i32 %X1, 0
    213   %B = icmp slt i32 %X2, %X3
    214   %C = and i1 %A, %B
    215   %D = select i1 %C, i32 %X1, i32 0
    216   ret i32 %D
    217 ; CHECK-LABEL: @test15
    218 ; CHECK: and i1 %A, %B
    219 }
    220