1 ; RUN: opt < %s -nary-reassociate -S | FileCheck %s
3 target datalayout = "e-i64:64-v16:16-v32:32-n16:32:64"
13 define void @left_reassociate(i32 %a, i32 %b, i32 %c) {
14 ; CHECK-LABEL: @left_reassociate(
16 ; CHECK: [[BASE:%[a-zA-Z0-9]+]] = add i32 %a, %c
17 call void @foo(i32 %1)
20 ; CHECK: [[RESULT:%[a-zA-Z0-9]+]] = add i32 [[BASE]], %b
21 call void @foo(i32 %3)
22 ; CHECK-NEXT: call void @foo(i32 [[RESULT]])
32 define void @right_reassociate(i32 %a, i32 %b, i32 %c) {
33 ; CHECK-LABEL: @right_reassociate(
35 ; CHECK: [[BASE:%[a-zA-Z0-9]+]] = add i32 %a, %c
36 call void @foo(i32 %1)
39 ; CHECK: [[RESULT:%[a-zA-Z0-9]+]] = add i32 [[BASE]], %b
40 call void @foo(i32 %3)
41 ; CHECK-NEXT: call void @foo(i32 [[RESULT]])
52 ; Do not rewrite t3 into t1 + b because t2 is used elsewhere and is likely free.
53 define void @no_reassociate(i32 %a, i32 %b, i32 %c) {
54 ; CHECK-LABEL: @no_reassociate(
56 ; CHECK: add i32 %a, %c
57 call void @foo(i32 %1)
59 ; CHECK: add i32 %a, %b
60 call void @foo(i32 %2)
62 ; CHECK: add i32 %2, %c
63 call void @foo(i32 %3)
74 ; No action because (a + c) does not dominate ((a + b) + c).
75 define void @conditional(i1 %p1, i1 %p2, i1 %p3, i32 %a, i32 %b, i32 %c) {
76 ; CHECK-LABEL: @conditional(
78 br i1 %p1, label %then1, label %branch1
82 ; CHECK: add i32 %a, %c
83 call void @foo(i32 %0)
87 br i1 %p2, label %then2, label %branch2
91 ; CHECK: add i32 %a, %c
92 call void @foo(i32 %1)
96 br i1 %p3, label %then3, label %return
100 ; CHECK: %2 = add i32 %a, %b
102 ; CHECK: add i32 %2, %c
103 call void @foo(i32 %3)
110 ; This test involves more conditional reassociation candidates. It exercises
111 ; the stack optimization in tryReassociatedAdd that pops the candidates that
112 ; do not dominate the current instruction.
127 ; NaryReassociate should match use1 with def3, and use2 with def1.
128 define void @conditional2(i32 %a, i32 %b, i32 %c, i1 %cond1, i1 %cond2, i1 %cond3) {
130 %def1 = add i32 %a, %b
131 br i1 %cond1, label %bb1, label %bb6
133 br i1 %cond2, label %bb2, label %bb3
135 %def2 = add i32 %a, %b
136 call void @foo(i32 %def2)
139 %def3 = add i32 %a, %b
140 br i1 %cond3, label %bb4, label %bb5
142 %def4 = add i32 %a, %b
143 call void @foo(i32 %def4)
148 ; CHECK: [[t1:%[0-9]+]] = add i32 %def3, %c
149 call void @foo(i32 %1) ; foo((a + c) + b);
150 ; CHECK-NEXT: call void @foo(i32 [[t1]])
155 ; CHECK: [[t2:%[0-9]+]] = add i32 %def1, %c
156 call void @foo(i32 %3) ; foo((a + c) + b);
157 ; CHECK-NEXT: call void @foo(i32 [[t2]])
162 ; foo(((a + d) + b) + c)
167 define void @quaternary(i32 %a, i32 %b, i32 %c, i32 %d) {
168 ; CHECK-LABEL: @quaternary(
171 call void @foo(i32 %2)
172 ; CHECK: call void @foo(i32 [[TMP1:%[a-zA-Z0-9]]])
176 ; CHECK: [[TMP2:%[a-zA-Z0-9]]] = add i32 [[TMP1]], %d
177 call void @foo(i32 %5)
178 ; CHECK: call void @foo(i32 [[TMP2]]
182 define void @iterative(i32 %a, i32 %b, i32 %c) {
184 %abc = add i32 %ab, %c
185 call void @foo(i32 %abc)
187 %ab2 = add i32 %ab, %b
188 %ab2c = add i32 %ab2, %c
189 ; CHECK: %ab2c = add i32 %abc, %b
190 call void @foo(i32 %ab2c)
191 ; CHECK-NEXT: call void @foo(i32 %ab2c)
193 %ab3 = add i32 %ab2, %b
194 %ab3c = add i32 %ab3, %c
195 ; CHECK-NEXT: %ab3c = add i32 %ab2c, %b
196 call void @foo(i32 %ab3c)
197 ; CHECK-NEXT: call void @foo(i32 %ab3c)
202 define void @avoid_infinite_loop(i32 %a, i32 %b) {
203 ; CHECK-LABEL: @avoid_infinite_loop
206 %ab2 = add i32 %ab, %b
208 call void @foo(i32 %ab2)
209 ; CHECK-NEXT: @foo(i32 %ab2)