[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:28:40 PDT 2017


arsenm created this revision.
Herald added a subscriber: wdng.

If a switch was an unreachable block that branched to an unreachable block, it would leave phis with missing predecessors.


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,20 @@
+; 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
+}
Index: lib/Transforms/Utils/LowerSwitch.cpp
===================================================================
--- lib/Transforms/Utils/LowerSwitch.cpp
+++ lib/Transforms/Utils/LowerSwitch.cpp
@@ -403,6 +403,11 @@
   Value *Val = SI->getCondition();  // The value we are switching on...
   BasicBlock* Default = SI->getDefaultDest();
 
+  if (CurBlock != &F->getEntryBlock() && pred_empty(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.91630.patch
Type: text/x-patch
Size: 1347 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170313/dd00f3c8/attachment.bin>


More information about the llvm-commits mailing list