1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=reassociate,gvn,instcombine -S | FileCheck %s
3 ; RUN: opt < %s -passes='reassociate,gvn,instcombine' -S | FileCheck %s
5 define i32 @test1(i32 %arg) {
7 ; CHECK-NEXT: [[ARG_NEG:%.*]] = sub i32 0, [[ARG:%.*]]
8 ; CHECK-NEXT: ret i32 [[ARG_NEG]]
10 %tmp1 = sub i32 -12, %arg
11 %tmp2 = add i32 %tmp1, 12
15 define i32 @test2(i32 %reg109, i32 %reg1111) {
16 ; CHECK-LABEL: @test2(
17 ; CHECK-NEXT: [[REG117:%.*]] = add i32 [[REG1111:%.*]], [[REG109:%.*]]
18 ; CHECK-NEXT: ret i32 [[REG117]]
20 %reg115 = add i32 %reg109, -30
21 %reg116 = add i32 %reg115, %reg1111
22 %reg117 = add i32 %reg116, 30
26 @e = external global i32
27 @a = external global i32
28 @b = external global i32
29 @c = external global i32
30 @f = external global i32
32 define void @test3() {
33 ; CHECK-LABEL: @test3(
34 ; CHECK-NEXT: [[A:%.*]] = load i32, ptr @a, align 4
35 ; CHECK-NEXT: [[B:%.*]] = load i32, ptr @b, align 4
36 ; CHECK-NEXT: [[C:%.*]] = load i32, ptr @c, align 4
37 ; CHECK-NEXT: [[T1:%.*]] = add i32 [[B]], [[A]]
38 ; CHECK-NEXT: [[T2:%.*]] = add i32 [[T1]], [[C]]
39 ; CHECK-NEXT: store i32 [[T2]], ptr @e, align 4
40 ; CHECK-NEXT: store i32 [[T2]], ptr @f, align 4
41 ; CHECK-NEXT: ret void
57 define void @test4() {
58 ; CHECK-LABEL: @test4(
59 ; CHECK-NEXT: [[A:%.*]] = load i32, ptr @a, align 4
60 ; CHECK-NEXT: [[B:%.*]] = load i32, ptr @b, align 4
61 ; CHECK-NEXT: [[C:%.*]] = load i32, ptr @c, align 4
62 ; CHECK-NEXT: [[T1:%.*]] = add i32 [[B]], [[A]]
63 ; CHECK-NEXT: [[T2:%.*]] = add i32 [[T1]], [[C]]
64 ; CHECK-NEXT: store i32 [[T2]], ptr @e, align 4
65 ; CHECK-NEXT: store i32 [[T2]], ptr @f, align 4
66 ; CHECK-NEXT: ret void
82 define void @test5() {
83 ; CHECK-LABEL: @test5(
84 ; CHECK-NEXT: [[A:%.*]] = load i32, ptr @a, align 4
85 ; CHECK-NEXT: [[B:%.*]] = load i32, ptr @b, align 4
86 ; CHECK-NEXT: [[C:%.*]] = load i32, ptr @c, align 4
87 ; CHECK-NEXT: [[T1:%.*]] = add i32 [[B]], [[A]]
88 ; CHECK-NEXT: [[T2:%.*]] = add i32 [[T1]], [[C]]
89 ; CHECK-NEXT: store i32 [[T2]], ptr @e, align 4
90 ; CHECK-NEXT: store i32 [[T2]], ptr @f, align 4
91 ; CHECK-NEXT: ret void
101 store i32 %t2, ptr @e
103 store i32 %t4, ptr @f
107 define i32 @test6() {
108 ; CHECK-LABEL: @test6(
109 ; CHECK-NEXT: ret i32 0
111 %tmp.0 = load i32, ptr @a
112 %tmp.1 = load i32, ptr @b
114 %tmp.2 = add i32 %tmp.0, %tmp.1
115 %tmp.4 = load i32, ptr @c
117 %tmp.5 = add i32 %tmp.2, %tmp.4
119 %tmp.8 = add i32 %tmp.0, %tmp.4
121 %tmp.11 = add i32 %tmp.8, %tmp.1
123 %RV = xor i32 %tmp.5, %tmp.11
127 ; This should be one add and two multiplies.
130 define i32 @test7(i32 %A, i32 %B, i32 %C) {
131 ; CHECK-LABEL: @test7(
132 ; CHECK-NEXT: [[REASS_ADD1:%.*]] = add i32 [[C:%.*]], [[B:%.*]]
133 ; CHECK-NEXT: [[REASS_MUL2:%.*]] = mul i32 [[A:%.*]], [[A]]
134 ; CHECK-NEXT: [[REASS_MUL:%.*]] = mul i32 [[REASS_MUL2]], [[REASS_ADD1]]
135 ; CHECK-NEXT: ret i32 [[REASS_MUL]]
138 %aab = mul i32 %aa, %B
140 %aac = mul i32 %ac, %A
141 %r = add i32 %aab, %aac
145 define i32 @test8(i32 %X, i32 %Y, i32 %Z) {
146 ; CHECK-LABEL: @test8(
147 ; CHECK-NEXT: [[A:%.*]] = mul i32 [[Y:%.*]], [[X:%.*]]
148 ; CHECK-NEXT: [[C:%.*]] = sub i32 [[Z:%.*]], [[A]]
149 ; CHECK-NEXT: ret i32 [[C]]
153 ; (-X)*Y + Z -> Z-X*Y
160 define i32 @test9(i32 %X) {
161 ; CHECK-LABEL: @test9(
162 ; CHECK-NEXT: [[FACTOR:%.*]] = mul i32 [[X:%.*]], 94
163 ; CHECK-NEXT: ret i32 [[FACTOR]]
170 define i32 @test10(i32 %X) {
171 ; CHECK-LABEL: @test10(
172 ; CHECK-NEXT: [[FACTOR:%.*]] = mul i32 [[X:%.*]], 3
173 ; CHECK-NEXT: ret i32 [[FACTOR]]
180 define i32 @test11(i32 %W) {
181 ; CHECK-LABEL: @test11(
182 ; CHECK-NEXT: [[FACTOR:%.*]] = mul i32 [[W:%.*]], 381
183 ; CHECK-NEXT: ret i32 [[FACTOR]]
191 declare void @mumble(i32)
193 define i32 @test12(i32 %X) {
194 ; CHECK-LABEL: @test12(
195 ; CHECK-NEXT: [[X_NEG:%.*]] = sub i32 0, [[X:%.*]]
196 ; CHECK-NEXT: call void @mumble(i32 [[X_NEG]])
197 ; CHECK-NEXT: [[FACTOR:%.*]] = mul i32 [[X]], -3
198 ; CHECK-NEXT: [[Z:%.*]] = add i32 [[FACTOR]], 6
199 ; CHECK-NEXT: ret i32 [[Z]]
201 %X.neg = sub nsw nuw i32 0, %X
202 call void @mumble(i32 %X.neg)
211 define i32 @test13(i32 %X1, i32 %X2, i32 %X3) {
212 ; CHECK-LABEL: @test13(
213 ; CHECK-NEXT: [[REASS_ADD:%.*]] = sub i32 [[X3:%.*]], [[X2:%.*]]
214 ; CHECK-NEXT: [[REASS_MUL:%.*]] = mul i32 [[REASS_ADD]], [[X1:%.*]]
215 ; CHECK-NEXT: ret i32 [[REASS_MUL]]
218 %B = mul i32 %A, %X2 ; -X1*X2
219 %C = mul i32 %X1, %X3 ; X1*X3
220 %D = add i32 %B, %C ; -X1*X2 + X1*X3 -> X1*(X3-X2)
226 define i32 @test14(i32 %X1, i32 %X2) {
227 ; CHECK-LABEL: @test14(
228 ; CHECK-NEXT: [[REASS_ADD:%.*]] = sub i32 [[X1:%.*]], [[X2:%.*]]
229 ; CHECK-NEXT: [[REASS_MUL:%.*]] = mul i32 [[REASS_ADD]], 47
230 ; CHECK-NEXT: ret i32 [[REASS_MUL]]
232 %B = mul i32 %X1, 47 ; X1*47
233 %C = mul i32 %X2, -47 ; X2*-47
234 %D = add i32 %B, %C ; X1*47 + X2*-47 -> 47*(X1-X2)
238 ; Do not reassociate expressions of type i1
240 define i32 @test15(i32 %X1, i32 %X2, i32 %X3) {
241 ; CHECK-LABEL: @test15(
242 ; CHECK-NEXT: [[B:%.*]] = icmp slt i32 [[X2:%.*]], [[X3:%.*]]
243 ; CHECK-NEXT: [[D:%.*]] = select i1 [[B]], i32 [[X1:%.*]], i32 0
244 ; CHECK-NEXT: ret i32 [[D]]
246 %A = icmp ne i32 %X1, 0
247 %B = icmp slt i32 %X2, %X3
249 %D = select i1 %C, i32 %X1, i32 0
253 ; PR30256 - previously this asserted.
255 define i64 @test16(i1 %cmp, i64 %a, i64 %b) {
256 ; CHECK-LABEL: @test16(
258 ; CHECK-NEXT: br i1 [[CMP:%.*]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
260 ; CHECK-NEXT: [[TMP0:%.*]] = shl i64 [[A:%.*]], 2
261 ; CHECK-NEXT: [[ADD2:%.*]] = sub i64 [[B:%.*]], [[TMP0]]
262 ; CHECK-NEXT: ret i64 [[ADD2]]
264 ; CHECK-NEXT: ret i64 0
268 %shl.neg = sub i64 0, %shl
269 br i1 %cmp, label %if.then, label %if.end
272 %add1 = add i64 %shl.neg, %shl.neg
273 %add2 = add i64 %add1, %b
280 define i32 @test17(i32 %X1, i32 %X2, i32 %X3, i32 %X4) {
281 ; CHECK-LABEL: @test17(
282 ; CHECK-NEXT: [[A:%.*]] = mul i32 [[X4:%.*]], [[X3:%.*]]
283 ; CHECK-NEXT: [[C:%.*]] = mul i32 [[A]], [[X1:%.*]]
284 ; CHECK-NEXT: [[D:%.*]] = mul i32 [[A]], [[X2:%.*]]
285 ; CHECK-NEXT: [[E:%.*]] = xor i32 [[C]], [[D]]
286 ; CHECK-NEXT: ret i32 [[E]]
288 %A = mul i32 %X3, %X1
289 %B = mul i32 %X3, %X2