Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6885950

Browse files
authored
[SCEV] Fix a hang introduced by collectForPHI (llvm#158153)
If we have a phi where one of it's source blocks is an unreachable block, we don't want to traverse back into the unreachable region. Doing so allows e.g. finding a trivial self loop when walking back the predecessor chain.
1 parent 04d38be commit 6885950

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15457,6 +15457,12 @@ void ScalarEvolution::LoopGuards::collectFromPHI(
1545715457
const BasicBlock *InBlock = Phi.getIncomingBlock(IncomingIdx);
1545815458
if (!VisitedBlocks.insert(InBlock).second)
1545915459
return {nullptr, scCouldNotCompute};
15460+
15461+
// Avoid analyzing unreachable blocks so that we don't get trapped
15462+
// traversing cycles with ill-formed dominance or infinite cycles
15463+
if (!SE.DT.isReachableFromEntry(InBlock))
15464+
return {nullptr, scCouldNotCompute};
15465+
1546015466
auto [G, Inserted] = IncomingGuards.try_emplace(InBlock, LoopGuards(SE));
1546115467
if (Inserted)
1546215468
collectFromBlock(SE, G->second, Phi.getParent(), InBlock, VisitedBlocks,
@@ -15511,6 +15517,9 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1551115517
ScalarEvolution &SE, ScalarEvolution::LoopGuards &Guards,
1551215518
const BasicBlock *Block, const BasicBlock *Pred,
1551315519
SmallPtrSetImpl<const BasicBlock *> &VisitedBlocks, unsigned Depth) {
15520+
15521+
assert(SE.DT.isReachableFromEntry(Block) && SE.DT.isReachableFromEntry(Pred));
15522+
1551415523
SmallVector<const SCEV *> ExprsToRewrite;
1551515524
auto CollectCondition = [&](ICmpInst::Predicate Predicate, const SCEV *LHS,
1551615525
const SCEV *RHS,

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,3 +364,29 @@ body:
364364
exit:
365365
ret void
366366
}
367+
368+
define void @hang_due_to_unreachable_phi_inblock() personality ptr null {
369+
bb:
370+
br label %bb6
371+
372+
self-loop: ; preds = %self-loop
373+
%dead = invoke ptr null()
374+
to label %self-loop unwind label %bb4
375+
376+
bb4: ; preds = %self-loop
377+
%i5 = landingpad { ptr, i32 }
378+
cleanup
379+
br label %bb6
380+
381+
bb6: ; preds = %bb4, %bb
382+
%i7 = phi ptr [ null, %bb4 ], [ null, %bb ]
383+
br label %bb8
384+
385+
bb8: ; preds = %bb8, %bb6
386+
%i9 = phi ptr [ null, %bb8 ], [ null, %bb6 ]
387+
%i11 = icmp eq ptr %i9, null
388+
br i1 %i11, label %bb12, label %bb8
389+
390+
bb12: ; preds = %bb8, %bb6
391+
ret void
392+
}

0 commit comments

Comments
 (0)