Skip to content

Commit c571bd9

Browse files
authored
[cherrypick][SCEV] Pick fixes for recursive loop guard collection. (#9797)
* [SCEV] Fix exit condition for recursive loop guard collection (llvm#120442) 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 (cherry picked from commit acfd26a) * [SCEV] Make sure starting block is marked as visited when recursively collecting loop guards. (llvm#120749) When `collectFromBlock` is called without a predecessor (in particular for loops that don't have a unique predecessor outside the loop) we never start climbing the predecessor chain, and thus don't mark the starting block as visited. Fixes llvm#120615. (cherry picked from commit f035351)
1 parent 06a77c5 commit c571bd9

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15532,6 +15532,8 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1553215532
// predecessors that can be found that have unique successors leading to the
1553315533
// original header.
1553415534
// TODO: share this logic with isLoopEntryGuardedByCond.
15535+
unsigned NumCollectedConditions = 0;
15536+
VisitedBlocks.insert(Block);
1553515537
std::pair<const BasicBlock *, const BasicBlock *> Pair(Pred, Block);
1553615538
for (; Pair.first;
1553715539
Pair = SE.getPredecessorWithUniqueSuccessorForBB(Pair.first)) {
@@ -15543,10 +15545,11 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1554315545

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

1554715550
// If we are recursively collecting guards stop after 2
15548-
// predecessors to limit compile-time impact for now.
15549-
if (Depth > 0 && Terms.size() == 2)
15551+
// conditions to limit compile-time impact for now.
15552+
if (Depth > 0 && NumCollectedConditions == 2)
1555015553
break;
1555115554
}
1555215555
// Finally, if we stopped climbing the predecessor chain because

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,3 +277,62 @@ 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+
}
313+
314+
; Checks correct traversal for loops without a unique predecessor
315+
; outside the loop.
316+
define void @pr120615() {
317+
; CHECK-LABEL: pr120615
318+
; CHECK-NEXT: Determining loop execution counts for: @pr120615
319+
; CHECK-NEXT: Loop %header: backedge-taken count is i32 0
320+
; CHECK-NEXT: Loop %header: constant max backedge-taken count is i32 0
321+
; CHECK-NEXT: Loop %header: symbolic max backedge-taken count is i32 0
322+
; CHECK-NEXT: Loop %header: Trip multiple is 1
323+
entry:
324+
br label %header
325+
326+
bb:
327+
br label %header
328+
329+
header:
330+
%0 = phi i32 [ %1, %header ], [ 0, %bb ], [ 0, %entry ]
331+
%1 = add i32 %0, 1
332+
%icmp = icmp slt i32 %0, 0
333+
br i1 %icmp, label %header, label %exit
334+
335+
exit:
336+
ret void
337+
338+
}

0 commit comments

Comments
 (0)