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