Skip to content

Commit 137d706

Browse files
authored
[SCEV] Do not attempt to collect loop guards for loops without predecessor. (#123662)
Attempting to collect loop guards for loops without a predecessor can lead to non-terminating recursion trying to construct a SCEV. Fixes #122913.
1 parent 195a1fc commit 137d706

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15328,6 +15328,8 @@ ScalarEvolution::LoopGuards::collect(const Loop *L, ScalarEvolution &SE) {
1532815328
BasicBlock *Header = L->getHeader();
1532915329
BasicBlock *Pred = L->getLoopPredecessor();
1533015330
LoopGuards Guards(SE);
15331+
if (!Pred)
15332+
return Guards;
1533115333
SmallPtrSet<const BasicBlock *, 8> VisitedBlocks;
1533215334
collectFromBlock(SE, Guards, Header, Pred, VisitedBlocks);
1533315335
return Guards;

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,3 +336,31 @@ exit:
336336
ret void
337337

338338
}
339+
340+
; Checks correct traversal for loops without a unique predecessor
341+
; outside the loop.
342+
define void @pr122913() {
343+
; CHECK-LABEL: pr122913
344+
; CHECK-NEXT: Determining loop execution counts for: @pr122913
345+
; CHECK-NEXT: Loop %header: backedge-taken count is i1 false
346+
; CHECK-NEXT: Loop %header: constant max backedge-taken count is i1 false
347+
; CHECK-NEXT: Loop %header: symbolic max backedge-taken count is i1 false
348+
; CHECK-NEXT: Loop %header: Trip multiple is 1
349+
entry:
350+
br i1 1, label %bb, label %header
351+
352+
bb:
353+
br i1 1, label %exit, label %header
354+
355+
header:
356+
%0 = phi i32 [ %1, %body ], [ 0, %bb ], [ 0, %entry ]
357+
br label %body
358+
359+
body:
360+
%1 = add i32 %0, 1
361+
%2 = icmp ult i32 %1, 0
362+
br i1 %2, label %header, label %exit
363+
364+
exit:
365+
ret void
366+
}

0 commit comments

Comments
 (0)