Skip to content

Commit 0acd02d

Browse files
authored
Merge pull request #16913 from eeckstein/fix-dce-4.2
2 parents 8f58361 + bbe951f commit 0acd02d

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
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: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// RUN: %target-sil-opt -assume-parsing-unqualified-ownership-sil -enable-sil-verify-all -dce %s | %FileCheck %s
22

3-
// REQUIRES: rdar34013900
4-
53
sil_stage canonical
64

75
import Builtin
@@ -59,17 +57,15 @@ bb3:
5957
return %18 : $()
6058
}
6159

62-
// We currently bail out on functions that have structurally infinite
63-
// loops.
6460
// CHECK-LABEL: sil @dead3
6561
sil @dead3 : $@convention(thin) () -> () {
6662
bb0:
6763
// CHECK: bb0
6864
br bb1
6965
// CHECK: bb1
7066
bb1:
71-
// CHECK: integer_literal $Builtin.Int32, 0
72-
// CHECK: br bb1
67+
// CHECK: bb1:
68+
// CHECK-NEXT: br bb1
7369
%0 = integer_literal $Builtin.Int32, 0
7470
br bb1
7571
}
@@ -247,3 +243,19 @@ bb0(%0 : $*Container):
247243
return %90 : $()
248244
}
249245

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)