Skip to content

Commit

Permalink
[SCEV] Fix exit condition for recursive loop guard collection (llvm#1…
Browse files Browse the repository at this point in the history
…20442)

When assumptions are present `Terms.size()` does not actually count the
number of conditions collected from dominating branches; introduce a
separate counter.

Fixes llvm#120237
  • Loading branch information
juliannagele authored Dec 20, 2024
1 parent 9e33387 commit acfd26a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
6 changes: 4 additions & 2 deletions llvm/lib/Analysis/ScalarEvolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15753,6 +15753,7 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
// predecessors that can be found that have unique successors leading to the
// original header.
// TODO: share this logic with isLoopEntryGuardedByCond.
unsigned NumCollectedConditions = 0;
std::pair<const BasicBlock *, const BasicBlock *> Pair(Pred, Block);
for (; Pair.first;
Pair = SE.getPredecessorWithUniqueSuccessorForBB(Pair.first)) {
Expand All @@ -15764,10 +15765,11 @@ void ScalarEvolution::LoopGuards::collectFromBlock(

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

// If we are recursively collecting guards stop after 2
// predecessors to limit compile-time impact for now.
if (Depth > 0 && Terms.size() == 2)
// conditions to limit compile-time impact for now.
if (Depth > 0 && NumCollectedConditions == 2)
break;
}
// Finally, if we stopped climbing the predecessor chain because
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,36 @@ epilogue:
exit:
ret void
}

declare void @llvm.assume(i1)

; Checks that the presence of assumptions does not interfere with
; exiting loop guard collection via following loop predecessors.
define void @pr120442(i1 %c.1, i1 %c.2) {
; CHECK-LABEL: 'pr120442'
; CHECK-NEXT: Determining loop execution counts for: @pr120442
; CHECK-NEXT: Loop %inner.header: backedge-taken count is i32 0
; CHECK-NEXT: Loop %inner.header: constant max backedge-taken count is i32 0
; CHECK-NEXT: Loop %inner.header: symbolic max backedge-taken count is i32 0
; CHECK-NEXT: Loop %inner.header: Trip multiple is 1
entry:
call void @llvm.assume(i1 %c.1)
call void @llvm.assume(i1 %c.2)
br label %outer.header

outer.header:
%phi7 = phi i32 [ 0, %bb ], [ 0, %entry ]
br label %inner.header

bb:
br i1 false, label %outer.header, label %bb

inner.header:
%phi = phi i32 [ %add, %inner.header ], [ 0, %outer.header ]
%add = add i32 %phi, 1
%icmp = icmp ugt i32 %add, 0
br i1 %icmp, label %exit, label %inner.header

exit:
ret void
}

0 comments on commit acfd26a

Please sign in to comment.