Skip to content

Commit acfd26a

Browse files
authored
[SCEV] Fix exit condition for recursive loop guard collection (#120442)
When assumptions are present `Terms.size()` does not actually count the number of conditions collected from dominating branches; introduce a separate counter. Fixes #120237
1 parent 9e33387 commit acfd26a

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15753,6 +15753,7 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1575315753
// predecessors that can be found that have unique successors leading to the
1575415754
// original header.
1575515755
// TODO: share this logic with isLoopEntryGuardedByCond.
15756+
unsigned NumCollectedConditions = 0;
1575615757
std::pair<const BasicBlock *, const BasicBlock *> Pair(Pred, Block);
1575715758
for (; Pair.first;
1575815759
Pair = SE.getPredecessorWithUniqueSuccessorForBB(Pair.first)) {
@@ -15764,10 +15765,11 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1576415765

1576515766
Terms.emplace_back(LoopEntryPredicate->getCondition(),
1576615767
LoopEntryPredicate->getSuccessor(0) == Pair.second);
15768+
NumCollectedConditions++;
1576715769

1576815770
// If we are recursively collecting guards stop after 2
15769-
// predecessors to limit compile-time impact for now.
15770-
if (Depth > 0 && Terms.size() == 2)
15771+
// conditions to limit compile-time impact for now.
15772+
if (Depth > 0 && NumCollectedConditions == 2)
1577115773
break;
1577215774
}
1577315775
// Finally, if we stopped climbing the predecessor chain because

llvm/test/Analysis/ScalarEvolution/backedge-taken-count-guard-info-with-multiple-predecessors.ll

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,3 +277,36 @@ epilogue:
277277
exit:
278278
ret void
279279
}
280+
281+
declare void @llvm.assume(i1)
282+
283+
; Checks that the presence of assumptions does not interfere with
284+
; exiting loop guard collection via following loop predecessors.
285+
define void @pr120442(i1 %c.1, i1 %c.2) {
286+
; CHECK-LABEL: 'pr120442'
287+
; CHECK-NEXT: Determining loop execution counts for: @pr120442
288+
; CHECK-NEXT: Loop %inner.header: backedge-taken count is i32 0
289+
; CHECK-NEXT: Loop %inner.header: constant max backedge-taken count is i32 0
290+
; CHECK-NEXT: Loop %inner.header: symbolic max backedge-taken count is i32 0
291+
; CHECK-NEXT: Loop %inner.header: Trip multiple is 1
292+
entry:
293+
call void @llvm.assume(i1 %c.1)
294+
call void @llvm.assume(i1 %c.2)
295+
br label %outer.header
296+
297+
outer.header:
298+
%phi7 = phi i32 [ 0, %bb ], [ 0, %entry ]
299+
br label %inner.header
300+
301+
bb:
302+
br i1 false, label %outer.header, label %bb
303+
304+
inner.header:
305+
%phi = phi i32 [ %add, %inner.header ], [ 0, %outer.header ]
306+
%add = add i32 %phi, 1
307+
%icmp = icmp ugt i32 %add, 0
308+
br i1 %icmp, label %exit, label %inner.header
309+
310+
exit:
311+
ret void
312+
}

0 commit comments

Comments
 (0)