[llvm] r301064 - LowerSwitch: Fix producing invalid IR on unreachable code

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 21 16:54:13 PDT 2017


Author: arsenm
Date: Fri Apr 21 18:54:12 2017
New Revision: 301064

URL: http://llvm.org/viewvc/llvm-project?rev=301064&view=rev
Log:
LowerSwitch: Fix producing invalid IR on unreachable code

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

Added:
    llvm/trunk/test/Transforms/LowerSwitch/phi-in-dead-block.ll
Modified:
    llvm/trunk/lib/Transforms/Utils/LowerSwitch.cpp

Modified: llvm/trunk/lib/Transforms/Utils/LowerSwitch.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/LowerSwitch.cpp?rev=301064&r1=301063&r2=301064&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/LowerSwitch.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/LowerSwitch.cpp Fri Apr 21 18:54:12 2017
@@ -403,6 +403,14 @@ void LowerSwitch::processSwitchInst(Swit
   Value *Val = SI->getCondition();  // The value we are switching on...
   BasicBlock* Default = SI->getDefaultDest();
 
+  // Don't handle unreachable blocks. If there are successors with phis, this
+  // would leave them behind with missing predecessors.
+  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);

Added: llvm/trunk/test/Transforms/LowerSwitch/phi-in-dead-block.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/LowerSwitch/phi-in-dead-block.ll?rev=301064&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/LowerSwitch/phi-in-dead-block.ll (added)
+++ llvm/trunk/test/Transforms/LowerSwitch/phi-in-dead-block.ll Fri Apr 21 18:54:12 2017
@@ -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
+}




More information about the llvm-commits mailing list