Skip to content

Commit 5d4f37e

Browse files
committed
[NFCI][SimplifyCFG] Rewrite createUnreachableSwitchDefault()
The only thing that function should do as per it's semantic, is to ensure that the switch's default is a block consisting only of an `unreachable` terminator. So let's just create such a block and update switch's default to point to it. There should be no need for all this weird dance around predecessors/successors.
1 parent 94c4952 commit 5d4f37e

File tree

2 files changed

+13
-16
lines changed

2 files changed

+13
-16
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

+11-14
Original file line numberDiff line numberDiff line change
@@ -4747,23 +4747,20 @@ static void createUnreachableSwitchDefault(SwitchInst *Switch,
47474747
DomTreeUpdater *DTU) {
47484748
LLVM_DEBUG(dbgs() << "SimplifyCFG: switch default is dead.\n");
47494749
auto *BB = Switch->getParent();
4750-
BasicBlock *NewDefaultBlock = SplitBlockPredecessors(
4751-
Switch->getDefaultDest(), Switch->getParent(), "", DTU);
47524750
auto *OrigDefaultBlock = Switch->getDefaultDest();
4751+
OrigDefaultBlock->removePredecessor(BB);
4752+
BasicBlock *NewDefaultBlock = BasicBlock::Create(
4753+
BB->getContext(), BB->getName() + ".unreachabledefault", BB->getParent(),
4754+
OrigDefaultBlock);
4755+
new UnreachableInst(Switch->getContext(), NewDefaultBlock);
47534756
Switch->setDefaultDest(&*NewDefaultBlock);
4754-
if (DTU)
4755-
DTU->applyUpdates({{DominatorTree::Insert, BB, &*NewDefaultBlock},
4756-
{DominatorTree::Delete, BB, OrigDefaultBlock}});
4757-
SplitBlock(&*NewDefaultBlock, &NewDefaultBlock->front(), DTU);
4758-
SmallVector<DominatorTree::UpdateType, 2> Updates;
4759-
if (DTU)
4760-
for (auto *Successor : successors(NewDefaultBlock))
4761-
Updates.push_back({DominatorTree::Delete, NewDefaultBlock, Successor});
4762-
auto *NewTerminator = NewDefaultBlock->getTerminator();
4763-
new UnreachableInst(Switch->getContext(), NewTerminator);
4764-
EraseTerminatorAndDCECond(NewTerminator);
4765-
if (DTU)
4757+
if (DTU) {
4758+
SmallVector<DominatorTree::UpdateType, 2> Updates;
4759+
Updates.push_back({DominatorTree::Insert, BB, &*NewDefaultBlock});
4760+
if (!is_contained(successors(BB), OrigDefaultBlock))
4761+
Updates.push_back({DominatorTree::Delete, BB, &*OrigDefaultBlock});
47664762
DTU->applyUpdates(Updates);
4763+
}
47674764
}
47684765

47694766
/// Turn a switch with two reachable destinations into an integer range

llvm/test/Transforms/SimplifyCFG/switch-dead-default.ll

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ default:
3333

3434
define void @test2(i2 %a) {
3535
; CHECK-LABEL: @test2(
36-
; CHECK-NEXT: switch i2 [[A:%.*]], label [[DEFAULT1:%.*]] [
36+
; CHECK-NEXT: switch i2 [[A:%.*]], label [[DOTUNREACHABLEDEFAULT:%.*]] [
3737
; CHECK-NEXT: i2 0, label [[CASE0:%.*]]
3838
; CHECK-NEXT: i2 1, label [[CASE1:%.*]]
3939
; CHECK-NEXT: i2 -2, label [[CASE2:%.*]]
@@ -53,7 +53,7 @@ define void @test2(i2 %a) {
5353
; CHECK: case3:
5454
; CHECK-NEXT: call void @foo(i32 3)
5555
; CHECK-NEXT: br label [[COMMON_RET]]
56-
; CHECK: default1:
56+
; CHECK: .unreachabledefault:
5757
; CHECK-NEXT: unreachable
5858
;
5959
switch i2 %a, label %default [i2 0, label %case0

0 commit comments

Comments
 (0)