1 ; RUN: opt < %s -passes=nary-reassociate -S | FileCheck %s
2 ; RUN: opt < %s -passes='nary-reassociate' -S | FileCheck %s
4 target datalayout = "e-i64:64-v16:16-v32:32-n16:32:64"
14 define void @left_reassociate(i32 %a, i32 %b, i32 %c) {
15 ; CHECK-LABEL: @left_reassociate(
17 ; CHECK: [[BASE:%[a-zA-Z0-9]+]] = add i32 %a, %c
18 call void @foo(i32 %1)
21 ; CHECK: [[RESULT:%[a-zA-Z0-9]+]] = add i32 [[BASE]], %b
22 call void @foo(i32 %3)
23 ; CHECK-NEXT: call void @foo(i32 [[RESULT]])
33 define void @right_reassociate(i32 %a, i32 %b, i32 %c) {
34 ; CHECK-LABEL: @right_reassociate(
36 ; CHECK: [[BASE:%[a-zA-Z0-9]+]] = add i32 %a, %c
37 call void @foo(i32 %1)
40 ; CHECK: [[RESULT:%[a-zA-Z0-9]+]] = add i32 [[BASE]], %b
41 call void @foo(i32 %3)
42 ; CHECK-NEXT: call void @foo(i32 [[RESULT]])
53 ; Do not rewrite t3 into t1 + b because t2 is used elsewhere and is likely free.
54 define void @no_reassociate(i32 %a, i32 %b, i32 %c) {
55 ; CHECK-LABEL: @no_reassociate(
57 ; CHECK: add i32 %a, %c
58 call void @foo(i32 %1)
60 ; CHECK: add i32 %a, %b
61 call void @foo(i32 %2)
63 ; CHECK: add i32 %2, %c
64 call void @foo(i32 %3)
75 ; No action because (a + c) does not dominate ((a + b) + c).
76 define void @conditional(i1 %p1, i1 %p2, i1 %p3, i32 %a, i32 %b, i32 %c) {
77 ; CHECK-LABEL: @conditional(
79 br i1 %p1, label %then1, label %branch1
83 ; CHECK: add i32 %a, %c
84 call void @foo(i32 %0)
88 br i1 %p2, label %then2, label %branch2
92 ; CHECK: add i32 %a, %c
93 call void @foo(i32 %1)
97 br i1 %p3, label %then3, label %return
101 ; CHECK: %2 = add i32 %a, %b
103 ; CHECK: add i32 %2, %c
104 call void @foo(i32 %3)
111 ; This test involves more conditional reassociation candidates. It exercises
112 ; the stack optimization in tryReassociatedAdd that pops the candidates that
113 ; do not dominate the current instruction.
128 ; NaryReassociate should match use1 with def3, and use2 with def1.
129 define void @conditional2(i32 %a, i32 %b, i32 %c, i1 %cond1, i1 %cond2, i1 %cond3) {
131 %def1 = add i32 %a, %b
132 br i1 %cond1, label %bb1, label %bb6
134 br i1 %cond2, label %bb2, label %bb3
136 %def2 = add i32 %a, %b
137 call void @foo(i32 %def2)
140 %def3 = add i32 %a, %b
141 br i1 %cond3, label %bb4, label %bb5
143 %def4 = add i32 %a, %b
144 call void @foo(i32 %def4)
149 ; CHECK: [[t1:%[0-9]+]] = add i32 %def3, %c
150 call void @foo(i32 %1) ; foo((a + c) + b);
151 ; CHECK-NEXT: call void @foo(i32 [[t1]])
156 ; CHECK: [[t2:%[0-9]+]] = add i32 %def1, %c
157 call void @foo(i32 %3) ; foo((a + c) + b);
158 ; CHECK-NEXT: call void @foo(i32 [[t2]])
163 ; foo(((a + d) + b) + c)
168 define void @quaternary(i32 %a, i32 %b, i32 %c, i32 %d) {
169 ; CHECK-LABEL: @quaternary(
172 call void @foo(i32 %2)
173 ; CHECK: call void @foo(i32 [[TMP1:%[a-zA-Z0-9]]])
177 ; CHECK: [[TMP2:%[a-zA-Z0-9]]] = add i32 [[TMP1]], %d
178 call void @foo(i32 %5)
179 ; CHECK: call void @foo(i32 [[TMP2]]
183 define void @iterative(i32 %a, i32 %b, i32 %c) {
185 %abc = add i32 %ab, %c
186 call void @foo(i32 %abc)
188 %ab2 = add i32 %ab, %b
189 %ab2c = add i32 %ab2, %c
190 ; CHECK: %ab2c = add i32 %abc, %b
191 call void @foo(i32 %ab2c)
192 ; CHECK-NEXT: call void @foo(i32 %ab2c)
194 %ab3 = add i32 %ab2, %b
195 %ab3c = add i32 %ab3, %c
196 ; CHECK-NEXT: %ab3c = add i32 %ab2c, %b
197 call void @foo(i32 %ab3c)
198 ; CHECK-NEXT: call void @foo(i32 %ab3c)
203 define void @avoid_infinite_loop(i32 %a, i32 %b) {
204 ; CHECK-LABEL: @avoid_infinite_loop
207 %ab2 = add i32 %ab, %b
209 call void @foo(i32 %ab2)
210 ; CHECK-NEXT: @foo(i32 %ab2)