1 ; With reassociation, constant folding can eliminate the 12 and -12 constants.
3 ; RUN: opt < %s -reassociate -gvn -instcombine -S | FileCheck %s
5 define i32 @test1(i32 %arg) {
6 %tmp1 = sub i32 -12, %arg
7 %tmp2 = add i32 %tmp1, 12
10 ; CHECK-NEXT: sub i32 0, %arg
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]
20 ; CHECK-NEXT: add i32 %reg1111, %reg109
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]
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]
39 store i32 %t2, i32* @e
41 store i32 %t4, i32* @f
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]
59 store i32 %t2, i32* @e
61 store i32 %t4, i32* @f
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]
79 store i32 %t2, i32* @e
81 store i32 %t4, i32* @f
94 %tmp.2 = add i32 %tmp.0, %tmp.1
97 %tmp.5 = add i32 %tmp.2, %tmp.4
99 %tmp.8 = add i32 %tmp.0, %tmp.4
101 %tmp.11 = add i32 %tmp.8, %tmp.1
103 %RV = xor i32 %tmp.5, %tmp.11
109 ; This should be one add and two multiplies.
110 define i32 @test7(i32 %A, i32 %B, i32 %C) {
113 %aab = mul i32 %aa, %B
115 %aac = mul i32 %ac, %A
116 %r = add i32 %aab, %aac
119 ; CHECK-NEXT: add i32 %C, %B
120 ; CHECK-NEXT: mul i32
121 ; CHECK-NEXT: mul i32
122 ; CHECK-NEXT: ret i32
126 define i32 @test8(i32 %X, i32 %Y, i32 %Z) {
129 ; (-X)*Y + Z -> Z-X*Y
133 ; CHECK-NEXT: %A = mul i32 %Y, %X
134 ; CHECK-NEXT: %C = sub i32 %Z, %A
135 ; CHECK-NEXT: ret i32 %C
140 define i32 @test9(i32 %X) {
145 ; CHECK-NEXT: mul i32 %X, 94
146 ; CHECK-NEXT: ret i32
149 define i32 @test10(i32 %X) {
154 ; CHECK-NEXT: mul i32 %X, 3
155 ; CHECK-NEXT: ret i32
158 define i32 @test11(i32 %W) {
164 ; CHECK-NEXT: mul i32 %W, 381
165 ; CHECK-NEXT: ret i32
168 define i32 @test12(i32 %X) {
177 ; CHECK-NEXT: mul i32 %X, -3
178 ; CHECK-NEXT: add i32{{.*}}, 6
179 ; CHECK-NEXT: ret i32
182 define i32 @test13(i32 %X1, i32 %X2, i32 %X3) {
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)
189 ; CHECK-NEXT: sub i32 %X3, %X2
190 ; CHECK-NEXT: mul i32 {{.*}}, %X1
191 ; CHECK-NEXT: ret i32
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)
201 ; CHECK-NEXT: sub i32 %X1, %X2
202 ; CHECK-NEXT: mul i32 {{.*}}, 47
203 ; CHECK-NEXT: ret i32
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
211 %D = select i1 %C, i32 %X1, i32 0
214 ; CHECK: and i1 %A, %B