<div dir="ltr">Is it possible the unreachable block has instructions that are used by its original successors?<div><br clear="all"><div><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature">~Craig</div></div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Aug 20, 2021 at 3:28 AM Roman Lebedev via llvm-commits <<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><br>
Author: Roman Lebedev<br>
Date: 2021-08-20T13:28:08+03:00<br>
New Revision: 5d4f37e895487408e61498ec42e545ec5f778b5b<br>
<br>
URL: <a href="https://github.com/llvm/llvm-project/commit/5d4f37e895487408e61498ec42e545ec5f778b5b" rel="noreferrer" target="_blank">https://github.com/llvm/llvm-project/commit/5d4f37e895487408e61498ec42e545ec5f778b5b</a><br>
DIFF: <a href="https://github.com/llvm/llvm-project/commit/5d4f37e895487408e61498ec42e545ec5f778b5b.diff" rel="noreferrer" target="_blank">https://github.com/llvm/llvm-project/commit/5d4f37e895487408e61498ec42e545ec5f778b5b.diff</a><br>
<br>
LOG: [NFCI][SimplifyCFG] Rewrite `createUnreachableSwitchDefault()`<br>
<br>
The only thing that function should do as per it's semantic,<br>
is to ensure that the switch's default is a block consisting only of<br>
an `unreachable` terminator.<br>
<br>
So let's just create such a block and update switch's default<br>
to point to it. There should be no need for all this weird dance<br>
around predecessors/successors.<br>
<br>
Added: <br>
<br>
<br>
Modified: <br>
llvm/lib/Transforms/Utils/SimplifyCFG.cpp<br>
llvm/test/Transforms/SimplifyCFG/switch-dead-default.ll<br>
<br>
Removed: <br>
<br>
<br>
<br>
################################################################################<br>
diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp<br>
index 847fdd760d2fe..ba3129f5581fe 100644<br>
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp<br>
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp<br>
@@ -4747,23 +4747,20 @@ static void createUnreachableSwitchDefault(SwitchInst *Switch,<br>
DomTreeUpdater *DTU) {<br>
LLVM_DEBUG(dbgs() << "SimplifyCFG: switch default is dead.\n");<br>
auto *BB = Switch->getParent();<br>
- BasicBlock *NewDefaultBlock = SplitBlockPredecessors(<br>
- Switch->getDefaultDest(), Switch->getParent(), "", DTU);<br>
auto *OrigDefaultBlock = Switch->getDefaultDest();<br>
+ OrigDefaultBlock->removePredecessor(BB);<br>
+ BasicBlock *NewDefaultBlock = BasicBlock::Create(<br>
+ BB->getContext(), BB->getName() + ".unreachabledefault", BB->getParent(),<br>
+ OrigDefaultBlock);<br>
+ new UnreachableInst(Switch->getContext(), NewDefaultBlock);<br>
Switch->setDefaultDest(&*NewDefaultBlock);<br>
- if (DTU)<br>
- DTU->applyUpdates({{DominatorTree::Insert, BB, &*NewDefaultBlock},<br>
- {DominatorTree::Delete, BB, OrigDefaultBlock}});<br>
- SplitBlock(&*NewDefaultBlock, &NewDefaultBlock->front(), DTU);<br>
- SmallVector<DominatorTree::UpdateType, 2> Updates;<br>
- if (DTU)<br>
- for (auto *Successor : successors(NewDefaultBlock))<br>
- Updates.push_back({DominatorTree::Delete, NewDefaultBlock, Successor});<br>
- auto *NewTerminator = NewDefaultBlock->getTerminator();<br>
- new UnreachableInst(Switch->getContext(), NewTerminator);<br>
- EraseTerminatorAndDCECond(NewTerminator);<br>
- if (DTU)<br>
+ if (DTU) {<br>
+ SmallVector<DominatorTree::UpdateType, 2> Updates;<br>
+ Updates.push_back({DominatorTree::Insert, BB, &*NewDefaultBlock});<br>
+ if (!is_contained(successors(BB), OrigDefaultBlock))<br>
+ Updates.push_back({DominatorTree::Delete, BB, &*OrigDefaultBlock});<br>
DTU->applyUpdates(Updates);<br>
+ }<br>
}<br>
<br>
/// Turn a switch with two reachable destinations into an integer range<br>
<br>
diff --git a/llvm/test/Transforms/SimplifyCFG/switch-dead-default.ll b/llvm/test/Transforms/SimplifyCFG/switch-dead-default.ll<br>
index b42024ee48995..154ecb2166310 100644<br>
--- a/llvm/test/Transforms/SimplifyCFG/switch-dead-default.ll<br>
+++ b/llvm/test/Transforms/SimplifyCFG/switch-dead-default.ll<br>
@@ -33,7 +33,7 @@ default:<br>
<br>
define void @test2(i2 %a) {<br>
; CHECK-LABEL: @test2(<br>
-; CHECK-NEXT: switch i2 [[A:%.*]], label [[DEFAULT1:%.*]] [<br>
+; CHECK-NEXT: switch i2 [[A:%.*]], label [[DOTUNREACHABLEDEFAULT:%.*]] [<br>
; CHECK-NEXT: i2 0, label [[CASE0:%.*]]<br>
; CHECK-NEXT: i2 1, label [[CASE1:%.*]]<br>
; CHECK-NEXT: i2 -2, label [[CASE2:%.*]]<br>
@@ -53,7 +53,7 @@ define void @test2(i2 %a) {<br>
; CHECK: case3:<br>
; CHECK-NEXT: call void @foo(i32 3)<br>
; CHECK-NEXT: br label [[COMMON_RET]]<br>
-; CHECK: default1:<br>
+; CHECK: .unreachabledefault:<br>
; CHECK-NEXT: unreachable<br>
;<br>
switch i2 %a, label %default [i2 0, label %case0<br>
<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote></div>