Skip to content

Commit bbe951f

Browse files
committed
dead code elimination: Fixed a crash when trying to get the immediate post-dominating block in an infinite loop.
SR-7805 rdar://problem/40650953
1 parent 2f0c281 commit bbe951f

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

lib/SILOptimizer/Transforms/DeadCodeElimination.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -419,10 +419,12 @@ void DCE::propagateLiveness(SILInstruction *I) {
419419
SILBasicBlock *DCE::nearestUsefulPostDominator(SILBasicBlock *Block) {
420420
// Find the nearest post-dominator that has useful instructions.
421421
auto *PostDomNode = PDT->getNode(Block)->getIDom();
422-
while (!LiveBlocks.count(PostDomNode->getBlock()))
422+
while (PostDomNode && !LiveBlocks.count(PostDomNode->getBlock()))
423423
PostDomNode = PostDomNode->getIDom();
424424

425-
return PostDomNode->getBlock();
425+
if (PostDomNode)
426+
return PostDomNode->getBlock();
427+
return nullptr;
426428
}
427429

428430
// Replace the given conditional branching instruction with a plain
@@ -485,8 +487,11 @@ bool DCE::removeDead(SILFunction &F) {
485487
// We want to replace dead terminators with unconditional branches to
486488
// the nearest post-dominator that has useful instructions.
487489
if (isa<TermInst>(Inst)) {
488-
replaceBranchWithJump(Inst,
489-
nearestUsefulPostDominator(Inst->getParent()));
490+
SILBasicBlock *postDom = nearestUsefulPostDominator(Inst->getParent());
491+
if (!postDom)
492+
continue;
493+
494+
replaceBranchWithJump(Inst, postDom);
490495
Inst->eraseFromParent();
491496
BranchesChanged = true;
492497
Changed = true;

test/SILOptimizer/dead_code_elimination.sil

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,3 +243,19 @@ bb0(%0 : $*Container):
243243
return %90 : $()
244244
}
245245

246+
// Check that DCE does not crash with an infinite loop through a cond_br.
247+
// CHECK-LABEL: sil @deal_with_infinite_loop
248+
// CHECK: cond_br
249+
sil @deal_with_infinite_loop : $@convention(thin) () -> () {
250+
bb0:
251+
br bb1
252+
253+
bb1:
254+
cond_br undef, bb2, bb3
255+
256+
bb2:
257+
br bb1
258+
259+
bb3:
260+
br bb1
261+
}

0 commit comments

Comments
 (0)