[PATCH] D30916: LowerSwitch: Fix producing invalid IR on unreachable code
Matt Arsenault via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Mon Mar 13 15:51:33 PDT 2017
arsenm updated this revision to Diff 91639.
arsenm added a comment.
Handle branch to self also
https://reviews.llvm.org/D30916
Files:
lib/Transforms/Utils/LowerSwitch.cpp
test/Transforms/LowerSwitch/phi-in-dead-block.ll
Index: test/Transforms/LowerSwitch/phi-in-dead-block.ll
===================================================================
--- /dev/null
+++ test/Transforms/LowerSwitch/phi-in-dead-block.ll
@@ -0,0 +1,40 @@
+; RUN: opt -S -lowerswitch %s | FileCheck %s
+
+; CHECK-LABEL: @phi_in_dead_block(
+; CHECK-NOT: switch
+define void @phi_in_dead_block() {
+bb:
+ br i1 undef, label %bb2, label %bb3
+
+bb1: ; No predecessors!
+ switch i32 undef, label %bb2 [
+ i32 9, label %bb3
+ ]
+
+bb2: ; preds = %bb1, %bb
+ %tmp = phi i64 [ undef, %bb1 ], [ undef, %bb ]
+ unreachable
+
+bb3: ; preds = %bb1, %bb
+ unreachable
+}
+
+; CHECK-LABEL: @phi_in_dead_block_br_to_self(
+; CHECK-NOT: switch
+define void @phi_in_dead_block_br_to_self() {
+bb:
+ br i1 undef, label %bb2, label %bb3
+
+bb1: ; No predecessors!
+ switch i32 undef, label %bb2 [
+ i32 9, label %bb3
+ i32 10, label %bb1
+ ]
+
+bb2: ; preds = %bb1, %bb
+ %tmp = phi i64 [ undef, %bb1 ], [ undef, %bb ]
+ unreachable
+
+bb3: ; preds = %bb1, %bb
+ unreachable
+}
Index: lib/Transforms/Utils/LowerSwitch.cpp
===================================================================
--- lib/Transforms/Utils/LowerSwitch.cpp
+++ lib/Transforms/Utils/LowerSwitch.cpp
@@ -403,6 +403,12 @@
Value *Val = SI->getCondition(); // The value we are switching on...
BasicBlock* Default = SI->getDefaultDest();
+ if ((CurBlock != &F->getEntryBlock() && pred_empty(CurBlock)) ||
+ CurBlock->getSinglePredecessor() == CurBlock) {
+ DeleteList.insert(CurBlock);
+ return;
+ }
+
// If there is only the default destination, just branch.
if (!SI->getNumCases()) {
BranchInst::Create(Default, CurBlock);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D30916.91639.patch
Type: text/x-patch
Size: 1952 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170313/702d5fb2/attachment.bin>
More information about the llvm-commits
mailing list