llvm.org GIT mirror llvm / e413590
[SCEV] Fix sorting order for AddRecExprs The existing sorting order in defined CompareSCEVComplexity sorts AddRecExprs by loop depth, but does not pay attention to dominance of loops. This can lead us to the following buggy situation: for (...) { // loop1 op1 = {A,+,B} } for (...) { // loop2 op2 = {A,+,B} S = add op1, op2 } In this case there is no guarantee that in operand list of S the op2 comes before op1 (loop depth is the same, so they will be sorted just lexicographically), so we can incorrectly treat S as a recurrence of loop1, which is wrong. This patch changes the sorting logic so that it places the dominated recs before the dominating recs. This ensures that when we pick the first recurrency in the operands order, it will be the bottom-most in terms of domination tree. The attached test set includes some tests that produce incorrect SCEV estimations and crashes with oldlogic. Reviewers: sanjoy, reames, apilipenko, anna Reviewed By: sanjoy Subscribers: llvm-commits, mzolotukhin Differential Revision: https://reviews.llvm.org/D33121 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@303148 91177308-0d34-0410-b5e6-96231b3b80d8 Max Kazantsev 2 years ago
2 changed file(s) with 485 addition(s) and 16 deletion(s). Raw diff Collapse all Expand all
583583 static int CompareSCEVComplexity(
584584 SmallSet, 8> &EqCacheSCEV,
585585 const LoopInfo *const LI, const SCEV *LHS, const SCEV *RHS,
586 unsigned Depth = 0) {
586 DominatorTree &DT, unsigned Depth = 0) {
587587 // Fast-path: SCEVs are uniqued so we can do a quick equality check.
588588 if (LHS == RHS)
589589 return 0;
628628 const SCEVAddRecExpr *LA = cast(LHS);
629629 const SCEVAddRecExpr *RA = cast(RHS);
630630
631 // Compare addrec loop depths.
631 // If there is a dominance relationship between the loops, sort by the
632 // dominance. Otherwise, sort by depth. We require such order in getAddExpr.
632633 const Loop *LLoop = LA->getLoop(), *RLoop = RA->getLoop();
633634 if (LLoop != RLoop) {
635 const BasicBlock *LHead = LLoop->getHeader(), *RHead = RLoop->getHeader();
636 assert(LHead != RHead && "Two loops share the same header?");
637 if (DT.dominates(LHead, RHead))
638 return 1;
639 else if (DT.dominates(RHead, LHead))
640 return -1;
634641 unsigned LDepth = LLoop->getLoopDepth(), RDepth = RLoop->getLoopDepth();
635642 if (LDepth != RDepth)
636643 return (int)LDepth - (int)RDepth;
644651 // Lexicographically compare.
645652 for (unsigned i = 0; i != LNumOps; ++i) {
646653 int X = CompareSCEVComplexity(EqCacheSCEV, LI, LA->getOperand(i),
647 RA->getOperand(i), Depth + 1);
654 RA->getOperand(i), DT, Depth + 1);
648655 if (X != 0)
649656 return X;
650657 }
668675 if (i >= RNumOps)
669676 return 1;
670677 int X = CompareSCEVComplexity(EqCacheSCEV, LI, LC->getOperand(i),
671 RC->getOperand(i), Depth + 1);
678 RC->getOperand(i), DT, Depth + 1);
672679 if (X != 0)
673680 return X;
674681 }
682689
683690 // Lexicographically compare udiv expressions.
684691 int X = CompareSCEVComplexity(EqCacheSCEV, LI, LC->getLHS(), RC->getLHS(),
685 Depth + 1);
692 DT, Depth + 1);
686693 if (X != 0)
687694 return X;
688 X = CompareSCEVComplexity(EqCacheSCEV, LI, LC->getRHS(), RC->getRHS(),
695 X = CompareSCEVComplexity(EqCacheSCEV, LI, LC->getRHS(), RC->getRHS(), DT,
689696 Depth + 1);
690697 if (X == 0)
691698 EqCacheSCEV.insert({LHS, RHS});
700707
701708 // Compare cast expressions by operand.
702709 int X = CompareSCEVComplexity(EqCacheSCEV, LI, LC->getOperand(),
703 RC->getOperand(), Depth + 1);
710 RC->getOperand(), DT, Depth + 1);
704711 if (X == 0)
705712 EqCacheSCEV.insert({LHS, RHS});
706713 return X;
723730 /// land in memory.
724731 ///
725732 static void GroupByComplexity(SmallVectorImpl &Ops,
726 LoopInfo *LI) {
733 LoopInfo *LI, DominatorTree &DT) {
727734 if (Ops.size() < 2) return; // Noop
728735
729736 SmallSet, 8> EqCache;
731738 // This is the common case, which also happens to be trivially simple.
732739 // Special case it.
733740 const SCEV *&LHS = Ops[0], *&RHS = Ops[1];
734 if (CompareSCEVComplexity(EqCache, LI, RHS, LHS) < 0)
741 if (CompareSCEVComplexity(EqCache, LI, RHS, LHS, DT) < 0)
735742 std::swap(LHS, RHS);
736743 return;
737744 }
738745
739746 // Do the rough sort by complexity.
740747 std::stable_sort(Ops.begin(), Ops.end(),
741 [&EqCache, LI](const SCEV *LHS, const SCEV *RHS) {
742 return CompareSCEVComplexity(EqCache, LI, LHS, RHS) < 0;
748 [&EqCache, LI, &DT](const SCEV *LHS, const SCEV *RHS) {
749 return
750 CompareSCEVComplexity(EqCache, LI, LHS, RHS, DT) < 0;
743751 });
744752
745753 // Now that we are sorted by complexity, group elements of the same
21852193 #endif
21862194
21872195 // Sort by complexity, this groups all similar expression types together.
2188 GroupByComplexity(Ops, &LI);
2196 GroupByComplexity(Ops, &LI, DT);
21892197
21902198 Flags = StrengthenNoWrapFlags(this, scAddExpr, Ops, Flags);
21912199
24912499 // added together. If so, we can fold them.
24922500 for (unsigned OtherIdx = Idx+1;
24932501 OtherIdx < Ops.size() && isa(Ops[OtherIdx]);
2494 ++OtherIdx)
2502 ++OtherIdx) {
2503 // We expect the AddRecExpr's to be sorted in reverse dominance order,
2504 // so that the 1st found AddRecExpr is dominated by all others.
2505 assert(DT.dominates(
2506 cast(Ops[OtherIdx])->getLoop()->getHeader(),
2507 AddRec->getLoop()->getHeader()) &&
2508 "AddRecExprs are not sorted in reverse dominance order?");
24952509 if (AddRecLoop == cast(Ops[OtherIdx])->getLoop()) {
24962510 // Other + {A,+,B} + {C,+,D} --> Other + {A+C,+,B+D}
24972511 SmallVector AddRecOps(AddRec->op_begin(),
25172531 Ops[Idx] = getAddRecExpr(AddRecOps, AddRecLoop, SCEV::FlagAnyWrap);
25182532 return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
25192533 }
2534 }
25202535
25212536 // Otherwise couldn't fold anything into this recurrence. Move onto the
25222537 // next one.
26132628 #endif
26142629
26152630 // Sort by complexity, this groups all similar expression types together.
2616 GroupByComplexity(Ops, &LI);
2631 GroupByComplexity(Ops, &LI, DT);
26172632
26182633 Flags = StrengthenNoWrapFlags(this, scMulExpr, Ops, Flags);
26192634
32103225 #endif
32113226
32123227 // Sort by complexity, this groups all similar expression types together.
3213 GroupByComplexity(Ops, &LI);
3228 GroupByComplexity(Ops, &LI, DT);
32143229
32153230 // If there are any constants, fold them together.
32163231 unsigned Idx = 0;
33113326 #endif
33123327
33133328 // Sort by complexity, this groups all similar expression types together.
3314 GroupByComplexity(Ops, &LI);
3329 GroupByComplexity(Ops, &LI, DT);
33153330
33163331 // If there are any constants, fold them together.
33173332 unsigned Idx = 0;
0 ; RUN: opt -analyze -scalar-evolution < %s | FileCheck %s
1
2 ; This test set ensures that we can correctly operate with recurrencies from
3 ; different loops.
4
5 ; Check that we can evaluate a sum of phis from two different loops in any
6 ; order.
7
8 define void @test_00() {
9
10 ; CHECK-LABEL: Classifying expressions for: @test_00
11 ; CHECK: %sum1 = add i32 %phi1, %phi2
12 ; CHECK-NEXT: --> {14,+,3}<%loop1>
13 ; CHECK: %sum2 = add i32 %sum1, %phi3
14 ; CHECK-NEXT: --> {20,+,6}<%loop1>
15 ; CHECK: %sum3 = add i32 %phi4, %phi5
16 ; CHECK-NEXT: --> {116,+,3}<%loop2>
17 ; CHECK: %sum4 = add i32 %sum3, %phi6
18 ; CHECK-NEXT: --> {159,+,6}<%loop2>
19 ; CHECK: %s1 = add i32 %phi1, %phi4
20 ; CHECK-NEXT: --> {{{{}}73,+,1}<%loop1>,+,1}<%loop2>
21 ; CHECK: %s2 = add i32 %phi5, %phi2
22 ; CHECK-NEXT: --> {{{{}}57,+,2}<%loop1>,+,2}<%loop2>
23 ; CHECK: %s3 = add i32 %sum1, %sum3
24 ; CHECK-NEXT: --> {{{{}}130,+,3}<%loop1>,+,3}<%loop2>
25 ; CHECK: %s4 = add i32 %sum4, %sum2
26 ; CHECK-NEXT: --> {{{{}}179,+,6}<%loop1>,+,6}<%loop2>
27 ; CHECK: %s5 = add i32 %phi3, %sum3
28 ; CHECK-NEXT: --> {{{{}}122,+,3}<%loop1>,+,3}<%loop2>
29 ; CHECK: %s6 = add i32 %sum2, %phi6
30 ; CHECK-NEXT: --> {{{{}}63,+,6}<%loop1>,+,3}<%loop2>
31
32 entry:
33 br label %loop1
34
35 loop1:
36 %phi1 = phi i32 [ 10, %entry ], [ %phi1.inc, %loop1 ]
37 %phi2 = phi i32 [ 4, %entry ], [ %phi2.inc, %loop1 ]
38 %phi3 = phi i32 [ 6, %entry ], [ %phi3.inc, %loop1 ]
39 %phi1.inc = add i32 %phi1, 1
40 %phi2.inc = add i32 %phi2, 2
41 %phi3.inc = add i32 %phi3, 3
42 %sum1 = add i32 %phi1, %phi2
43 %sum2 = add i32 %sum1, %phi3
44 %cond1 = icmp ult i32 %sum2, 1000
45 br i1 %cond1, label %loop1, label %loop2
46
47 loop2:
48 %phi4 = phi i32 [ 63, %loop1 ], [ %phi4.inc, %loop2 ]
49 %phi5 = phi i32 [ 53, %loop1 ], [ %phi5.inc, %loop2 ]
50 %phi6 = phi i32 [ 43, %loop1 ], [ %phi6.inc, %loop2 ]
51 %phi4.inc = add i32 %phi4, 1
52 %phi5.inc = add i32 %phi5, 2
53 %phi6.inc = add i32 %phi6, 3
54 %sum3 = add i32 %phi4, %phi5
55 %sum4 = add i32 %sum3, %phi6
56 %cond2 = icmp ult i32 %sum4, 1000
57 br i1 %cond2, label %loop2, label %exit
58
59 exit:
60 %s1 = add i32 %phi1, %phi4
61 %s2 = add i32 %phi5, %phi2
62 %s3 = add i32 %sum1, %sum3
63 %s4 = add i32 %sum4, %sum2
64 %s5 = add i32 %phi3, %sum3
65 %s6 = add i32 %sum2, %phi6
66 ret void
67 }
68
69 ; Check that we can evaluate a sum of phis+invariants from two different loops
70 ; in any order.
71
72 define void @test_01(i32 %a, i32 %b) {
73
74 ; CHECK-LABEL: Classifying expressions for: @test_01
75 ; CHECK: %sum1 = add i32 %phi1, %phi2
76 ; CHECK-NEXT: --> {(%a + %b),+,3}<%loop1>
77 ; CHECK: %sum2 = add i32 %sum1, %phi3
78 ; CHECK-NEXT: --> {(6 + %a + %b),+,6}<%loop1>
79 ; CHECK: %is1 = add i32 %sum2, %a
80 ; CHECK-NEXT: --> {(6 + (2 * %a) + %b),+,6}<%loop1>
81 ; CHECK: %sum3 = add i32 %phi4, %phi5
82 ; CHECK-NEXT: --> {116,+,3}<%loop2>
83 ; CHECK: %sum4 = add i32 %sum3, %phi6
84 ; CHECK-NEXT: --> {159,+,6}<%loop2>
85 ; CHECK: %is2 = add i32 %sum4, %b
86 ; CHECK-NEXT: --> {(159 + %b),+,6}<%loop2>
87 ; CHECK: %ec2 = add i32 %is1, %is2
88 ; CHECK-NEXT: --> {{{{}}(165 + (2 * %a) + (2 * %b)),+,6}<%loop1>,+,6}<%loop2>
89 ; CHECK: %s1 = add i32 %phi1, %is1
90 ; CHECK-NEXT: --> {(6 + (3 * %a) + %b),+,7}<%loop1>
91 ; CHECK: %s2 = add i32 %is2, %phi4
92 ; CHECK-NEXT: --> {(222 + %b),+,7}<%loop2>
93 ; CHECK: %s3 = add i32 %is1, %phi5
94 ; CHECK-NEXT: --> {{{{}}(59 + (2 * %a) + %b),+,6}<%loop1>,+,2}<%loop2>
95 ; CHECK: %s4 = add i32 %phi2, %is2
96 ; CHECK-NEXT: --> {{{{}}(159 + (2 * %b)),+,2}<%loop1>,+,6}<%loop2>
97 ; CHECK: %s5 = add i32 %is1, %is2
98 ; CHECK-NEXT: --> {{{{}}(165 + (2 * %a) + (2 * %b)),+,6}<%loop1>,+,6}<%loop2>
99 ; CHECK: %s6 = add i32 %is2, %is1
100 ; CHECK-NEXT: --> {{{{}}(165 + (2 * %a) + (2 * %b)),+,6}<%loop1>,+,6}<%loop2>
101
102 entry:
103 br label %loop1
104
105 loop1:
106 %phi1 = phi i32 [ %a, %entry ], [ %phi1.inc, %loop1 ]
107 %phi2 = phi i32 [ %b, %entry ], [ %phi2.inc, %loop1 ]
108 %phi3 = phi i32 [ 6, %entry ], [ %phi3.inc, %loop1 ]
109 %phi1.inc = add i32 %phi1, 1
110 %phi2.inc = add i32 %phi2, 2
111 %phi3.inc = add i32 %phi3, 3
112 %sum1 = add i32 %phi1, %phi2
113 %sum2 = add i32 %sum1, %phi3
114 %is1 = add i32 %sum2, %a
115 %cond1 = icmp ult i32 %is1, 1000
116 br i1 %cond1, label %loop1, label %loop2
117
118 loop2:
119 %phi4 = phi i32 [ 63, %loop1 ], [ %phi4.inc, %loop2 ]
120 %phi5 = phi i32 [ 53, %loop1 ], [ %phi5.inc, %loop2 ]
121 %phi6 = phi i32 [ 43, %loop1 ], [ %phi6.inc, %loop2 ]
122 %phi4.inc = add i32 %phi4, 1
123 %phi5.inc = add i32 %phi5, 2
124 %phi6.inc = add i32 %phi6, 3
125 %sum3 = add i32 %phi4, %phi5
126 %sum4 = add i32 %sum3, %phi6
127 %is2 = add i32 %sum4, %b
128 %ec2 = add i32 %is1, %is2
129 %cond2 = icmp ult i32 %ec2, 1000
130 br i1 %cond2, label %loop2, label %exit
131
132 exit:
133 %s1 = add i32 %phi1, %is1
134 %s2 = add i32 %is2, %phi4
135 %s3 = add i32 %is1, %phi5
136 %s4 = add i32 %phi2, %is2
137 %s5 = add i32 %is1, %is2
138 %s6 = add i32 %is2, %is1
139 ret void
140 }
141
142 ; Check that we can correctly evaluate a sum of phis+variants from two different
143 ; loops in any order.
144
145 define void @test_02(i32 %a, i32 %b, i32* %p) {
146
147 ; CHECK-LABEL: Classifying expressions for: @test_02
148 ; CHECK: %sum1 = add i32 %phi1, %phi2
149 ; CHECK-NEXT: --> {(%a + %b),+,3}<%loop1>
150 ; CHECK: %sum2 = add i32 %sum1, %phi3
151 ; CHECK-NEXT: --> {(6 + %a + %b),+,6}<%loop1>
152 ; CHECK: %is1 = add i32 %sum2, %v1
153 ; CHECK-NEXT: --> ({(6 + %a + %b),+,6}<%loop1> + %v1)
154 ; CHECK: %sum3 = add i32 %phi4, %phi5
155 ; CHECK-NEXT: --> {(%a + %b),+,3}<%loop2>
156 ; CHECK: %sum4 = add i32 %sum3, %phi6
157 ; CHECK-NEXT: --> {(43 + %a + %b),+,6}<%loop2>
158 ; CHECK: %is2 = add i32 %sum4, %v2
159 ; CHECK-NEXT: --> ({(43 + %a + %b),+,6}<%loop2> + %v2)
160 ; CHECK: %is3 = add i32 %v1, %sum2
161 ; CHECK-NEXT: --> ({(6 + %a + %b),+,6}<%loop1> + %v1)
162 ; CHECK: %ec2 = add i32 %is1, %is3
163 ; CHECK-NEXT: --> (2 * ({(6 + %a + %b),+,6}<%loop1> + %v1))
164 ; CHECK: %s1 = add i32 %phi1, %is1
165 ; CHECK-NEXT: --> ({(6 + (2 * %a) + %b),+,7}<%loop1> + %v1)
166 ; CHECK: %s2 = add i32 %is2, %phi4
167 ; CHECK-NEXT: --> ({(43 + (2 * %a) + %b),+,7}<%loop2> + %v2)
168 ; CHECK: %s3 = add i32 %is1, %phi5
169 ; CHECK-NEXT: --> {({(6 + (2 * %b) + %a),+,6}<%loop1> + %v1),+,2}<%loop2>
170 ; CHECK: %s4 = add i32 %phi2, %is2
171 ; CHECK-NEXT: --> ({{{{}}(43 + (2 * %b) + %a),+,2}<%loop1>,+,6}<%loop2> + %v2)
172 ; CHECK: %s5 = add i32 %is1, %is2
173 ; CHECK-NEXT: --> ({({(49 + (2 * %a) + (2 * %b)),+,6}<%loop1> + %v1),+,6}<%loop2> + %v2)
174 ; CHECK: %s6 = add i32 %is2, %is1
175 ; CHECK-NEXT: --> ({({(49 + (2 * %a) + (2 * %b)),+,6}<%loop1> + %v1),+,6}<%loop2> + %v2)
176
177 entry:
178 br label %loop1
179
180 loop1:
181 %phi1 = phi i32 [ %a, %entry ], [ %phi1.inc, %loop1 ]
182 %phi2 = phi i32 [ %b, %entry ], [ %phi2.inc, %loop1 ]
183 %phi3 = phi i32 [ 6, %entry ], [ %phi3.inc, %loop1 ]
184 %phi1.inc = add i32 %phi1, 1
185 %phi2.inc = add i32 %phi2, 2
186 %phi3.inc = add i32 %phi3, 3
187 %v1 = load i32, i32* %p
188 %sum1 = add i32 %phi1, %phi2
189 %sum2 = add i32 %sum1, %phi3
190 %is1 = add i32 %sum2, %v1
191 %cond1 = icmp ult i32 %is1, 1000
192 br i1 %cond1, label %loop1, label %loop2
193
194 loop2:
195 %phi4 = phi i32 [ %a, %loop1 ], [ %phi4.inc, %loop2 ]
196 %phi5 = phi i32 [ %b, %loop1 ], [ %phi5.inc, %loop2 ]
197 %phi6 = phi i32 [ 43, %loop1 ], [ %phi6.inc, %loop2 ]
198 %phi4.inc = add i32 %phi4, 1
199 %phi5.inc = add i32 %phi5, 2
200 %phi6.inc = add i32 %phi6, 3
201 %v2 = load i32, i32* %p
202 %sum3 = add i32 %phi4, %phi5
203 %sum4 = add i32 %sum3, %phi6
204 %is2 = add i32 %sum4, %v2
205 %is3 = add i32 %v1, %sum2
206 %ec2 = add i32 %is1, %is3
207 %cond2 = icmp ult i32 %ec2, 1000
208 br i1 %cond2, label %loop2, label %exit
209
210 exit:
211 %s1 = add i32 %phi1, %is1
212 %s2 = add i32 %is2, %phi4
213 %s3 = add i32 %is1, %phi5
214 %s4 = add i32 %phi2, %is2
215 %s5 = add i32 %is1, %is2
216 %s6 = add i32 %is2, %is1
217 ret void
218 }
219
220 ; Mix of previous use cases that demonstrates %s3 can be incorrectly treated as
221 ; a recurrence of loop1 because of operands order if we pick recurrencies in an
222 ; incorrect order.
223
224 define void @test_03(i32 %a, i32 %b, i32 %c, i32* %p) {
225
226 ; CHECK-LABEL: Classifying expressions for: @test_03
227 ; CHECK: %v1 = load i32, i32* %p
228 ; CHECK-NEXT: --> %v1
229 ; CHECK: %s1 = add i32 %phi1, %v1
230 ; CHECK-NEXT: --> {(%a + %v1),+,1}<%loop1>
231 ; CHECK: %s2 = add i32 %s1, %b
232 ; CHECK-NEXT: --> {(%a + %b + %v1),+,1}<%loop1>
233 ; CHECK: %s3 = add i32 %s2, %phi2
234 ; CHECK-NEXT: --> ({{{{}}((2 * %a) + %b),+,1}<%loop1>,+,2}<%loop2> + %v1)
235
236 entry:
237 br label %loop1
238
239 loop1:
240 %phi1 = phi i32 [ %a, %entry ], [ %phi1.inc, %loop1 ]
241 %phi1.inc = add i32 %phi1, 1
242 %cond1 = icmp ult i32 %phi1, %c
243 br i1 %cond1, label %loop1, label %loop2
244
245 loop2:
246 %phi2 = phi i32 [ %a, %loop1 ], [ %phi2.inc, %loop2 ]
247 %phi2.inc = add i32 %phi2, 2
248 %v1 = load i32, i32* %p
249 %s1 = add i32 %phi1, %v1
250 %s2 = add i32 %s1, %b
251 %s3 = add i32 %s2, %phi2
252 %cond2 = icmp ult i32 %s3, %c
253 br i1 %cond2, label %loop2, label %exit
254
255 exit:
256
257 ret void
258 }
259
260 ; Another mix of previous use cases that demonstrates that incorrect picking of
261 ; a loop for a recurrence may cause a crash of SCEV analysis.
262 define void @test_04() {
263
264 ; CHECK-LABEL: Classifying expressions for: @test_04
265 ; CHECK: %tmp = phi i64 [ 2, %bb ], [ %tmp4, %bb3 ]
266 ; CHECK-NEXT: --> {2,+,1}<%loop1>
267 ; CHECK: %tmp2 = trunc i64 %tmp to i32
268 ; CHECK-NEXT: --> {2,+,1}<%loop1>
269 ; CHECK: %tmp4 = add nuw nsw i64 %tmp, 1
270 ; CHECK-NEXT: --> {3,+,1}<%loop1>
271 ; CHECK: %tmp7 = phi i64 [ %tmp15, %loop2 ], [ 2, %loop1 ]
272 ; CHECK-NEXT: --> {2,+,1}<%loop2>
273 ; CHECK: %tmp10 = sub i64 %tmp9, %tmp7
274 ; CHECK-NEXT: --> ((sext i8 %tmp8 to i64) + {-2,+,-1}<%loop2>)
275 ; CHECK: %tmp11 = add i64 %tmp10, undef
276 ; CHECK-NEXT: --> ((sext i8 %tmp8 to i64) + {(-2 + undef),+,-1}<%loop2>)
277 ; CHECK: %tmp13 = trunc i64 %tmp11 to i32
278 ; CHECK-NEXT: --> ((sext i8 %tmp8 to i32) + {(trunc i64 (-2 + undef) to i32),+,-1}<%loop2>)
279 ; CHECK: %tmp14 = sub i32 %tmp13, %tmp2
280 ; CHECK-NEXT: --> ((sext i8 %tmp8 to i32) + {{{{}}(-2 + (trunc i64 (-2 + undef) to i32)),+,-1}<%loop1>,+,-1}<%loop2>)
281 ; CHECK: %tmp15 = add nuw nsw i64 %tmp7, 1
282 ; CHECK-NEXT: --> {3,+,1}<%loop2>
283
284 bb:
285 br label %loop1
286
287 loop1:
288 %tmp = phi i64 [ 2, %bb ], [ %tmp4, %bb3 ]
289 %tmp2 = trunc i64 %tmp to i32
290 br i1 undef, label %loop2, label %bb3
291
292 bb3:
293 %tmp4 = add nuw nsw i64 %tmp, 1
294 br label %loop1
295
296 bb5:
297 ret void
298
299 loop2:
300 %tmp7 = phi i64 [ %tmp15, %loop2 ], [ 2, %loop1 ]
301 %tmp8 = load i8, i8 addrspace(1)* undef, align 1
302 %tmp9 = sext i8 %tmp8 to i64
303 %tmp10 = sub i64 %tmp9, %tmp7
304 %tmp11 = add i64 %tmp10, undef
305 %tmp13 = trunc i64 %tmp11 to i32
306 %tmp14 = sub i32 %tmp13, %tmp2
307 %tmp15 = add nuw nsw i64 %tmp7, 1
308 %tmp16 = icmp slt i64 %tmp15, %tmp
309 br i1 %tmp16, label %loop2, label %bb5
310 }
311
312 @A = weak global [1000 x i32] zeroinitializer, align 32
313
314 ; Demonstrate a situation when we can add two recs with different degrees from
315 ; the same loop.
316 define void @test_05(i32 %N) {
317
318 ; CHECK-LABEL: Classifying expressions for: @test_05
319 ; CHECK: %SQ = mul i32 %i.0, %i.0
320 ; CHECK-NEXT: --> {4,+,5,+,2}<%bb3>
321 ; CHECK: %tmp4 = mul i32 %i.0, 2
322 ; CHECK-NEXT: --> {4,+,2}<%bb3>
323 ; CHECK: %tmp5 = sub i32 %SQ, %tmp4
324 ; CHECK-NEXT: --> {0,+,3,+,2}<%bb3>
325
326 entry:
327 %"alloca point" = bitcast i32 0 to i32 ; [#uses=0]
328 br label %bb3
329
330 bb: ; preds = %bb3
331 %tmp = getelementptr [1000 x i32], [1000 x i32]* @A, i32 0, i32 %i.0 ; [#uses=1]
332 store i32 123, i32* %tmp
333 %tmp2 = add i32 %i.0, 1 ; [#uses=1]
334 br label %bb3
335
336 bb3: ; preds = %bb, %entry
337 %i.0 = phi i32 [ 2, %entry ], [ %tmp2, %bb ] ; [#uses=3]
338 %SQ = mul i32 %i.0, %i.0
339 %tmp4 = mul i32 %i.0, 2
340 %tmp5 = sub i32 %SQ, %tmp4
341 %tmp3 = icmp sle i32 %tmp5, 9999 ; [#uses=1]
342 br i1 %tmp3, label %bb, label %bb5
343
344 bb5: ; preds = %bb3
345 br label %return
346
347 return: ; preds = %bb5
348 ret void
349 }
350
351 ; Check that we can add Phis from different loops with different nesting, nested
352 ; loop comes first.
353 define void @test_06() {
354
355 ; CHECK-LABEL: Classifying expressions for: @test_06
356 ; CHECK: %s1 = add i32 %phi1, %phi2
357 ; CHECK-NEXT: --> {{{{}}30,+,1}<%loop1>,+,2}<%loop2>
358 ; CHECK: %s2 = add i32 %phi2, %phi1
359 ; CHECK-NEXT: --> {{{{}}30,+,1}<%loop1>,+,2}<%loop2>
360 ; CHECK: %s3 = add i32 %phi1, %phi3
361 ; CHECK-NEXT: --> {{{{}}40,+,1}<%loop1>,+,3}<%loop3>
362 ; CHECK: %s4 = add i32 %phi3, %phi1
363 ; CHECK-NEXT: --> {{{{}}40,+,1}<%loop1>,+,3}<%loop3>
364 ; CHECK: %s5 = add i32 %phi2, %phi3
365 ; CHECK-NEXT: --> {{{{}}50,+,2}<%loop2>,+,3}<%loop3>
366 ; CHECK: %s6 = add i32 %phi3, %phi2
367 ; CHECK-NEXT: --> {{{{}}50,+,2}<%loop2>,+,3}<%loop3>
368
369 entry:
370 br label %loop1
371
372 loop1:
373 %phi1 = phi i32 [ 10, %entry ], [ %phi1.inc, %loop1.exit ]
374 br label %loop2
375
376 loop2:
377 %phi2 = phi i32 [ 20, %loop1 ], [ %phi2.inc, %loop2 ]
378 %phi2.inc = add i32 %phi2, 2
379 %cond2 = icmp ult i32 %phi2.inc, 1000
380 br i1 %cond2, label %loop2, label %loop1.exit
381
382 loop1.exit:
383 %phi1.inc = add i32 %phi1, 1
384 %cond1 = icmp ult i32 %phi1.inc, 1000
385 br i1 %cond1, label %loop1, label %loop3
386
387 loop3:
388 %phi3 = phi i32 [ 30, %loop1.exit ], [ %phi3.inc, %loop3 ]
389 %phi3.inc = add i32 %phi3, 3
390 %cond3 = icmp ult i32 %phi3.inc, 1000
391 br i1 %cond3, label %loop3, label %exit
392
393 exit:
394 %s1 = add i32 %phi1, %phi2
395 %s2 = add i32 %phi2, %phi1
396 %s3 = add i32 %phi1, %phi3
397 %s4 = add i32 %phi3, %phi1
398 %s5 = add i32 %phi2, %phi3
399 %s6 = add i32 %phi3, %phi2
400 ret void
401 }
402
403 ; Check that we can add Phis from different loops with different nesting, nested
404 ; loop comes second.
405 define void @test_07() {
406
407 ; CHECK-LABEL: Classifying expressions for: @test_07
408 ; CHECK: %s1 = add i32 %phi1, %phi2
409 ; CHECK-NEXT: --> {{{{}}30,+,1}<%loop1>,+,2}<%loop2>
410 ; CHECK: %s2 = add i32 %phi2, %phi1
411 ; CHECK-NEXT: --> {{{{}}30,+,1}<%loop1>,+,2}<%loop2>
412 ; CHECK: %s3 = add i32 %phi1, %phi3
413 ; CHECK-NEXT: --> {{{{}}40,+,3}<%loop3>,+,1}<%loop1>
414 ; CHECK: %s4 = add i32 %phi3, %phi1
415 ; CHECK-NEXT: --> {{{{}}40,+,3}<%loop3>,+,1}<%loop1>
416 ; CHECK: %s5 = add i32 %phi2, %phi3
417 ; CHECK-NEXT: --> {{{{}}50,+,3}<%loop3>,+,2}<%loop2>
418 ; CHECK: %s6 = add i32 %phi3, %phi2
419 ; CHECK-NEXT: --> {{{{}}50,+,3}<%loop3>,+,2}<%loop2>
420
421 entry:
422 br label %loop3
423
424 loop3:
425 %phi3 = phi i32 [ 30, %entry ], [ %phi3.inc, %loop3 ]
426 %phi3.inc = add i32 %phi3, 3
427 %cond3 = icmp ult i32 %phi3.inc, 1000
428 br i1 %cond3, label %loop3, label %loop1
429
430 loop1:
431 %phi1 = phi i32 [ 10, %loop3 ], [ %phi1.inc, %loop1.exit ]
432 br label %loop2
433
434 loop2:
435 %phi2 = phi i32 [ 20, %loop1 ], [ %phi2.inc, %loop2 ]
436 %phi2.inc = add i32 %phi2, 2
437 %cond2 = icmp ult i32 %phi2.inc, 1000
438 br i1 %cond2, label %loop2, label %loop1.exit
439
440 loop1.exit:
441 %phi1.inc = add i32 %phi1, 1
442 %cond1 = icmp ult i32 %phi1.inc, 1000
443 br i1 %cond1, label %exit, label %loop1
444
445 exit:
446 %s1 = add i32 %phi1, %phi2
447 %s2 = add i32 %phi2, %phi1
448 %s3 = add i32 %phi1, %phi3
449 %s4 = add i32 %phi3, %phi1
450 %s5 = add i32 %phi2, %phi3
451 %s6 = add i32 %phi3, %phi2
452 ret void
453 }