[PATCH] D11852: [LowerSwitch] Fix a bug when LowerSwitch deletes the default block

Chen Li via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 10 15:24:21 PDT 2015


chenli updated this revision to Diff 31735.
chenli added a comment.

Update patch w.r.t Hans' comments.


http://reviews.llvm.org/D11852

Files:
  lib/Transforms/Utils/LowerSwitch.cpp
  test/Transforms/LowerSwitch/delete-default-block-crash.ll

Index: test/Transforms/LowerSwitch/delete-default-block-crash.ll
===================================================================
--- /dev/null
+++ test/Transforms/LowerSwitch/delete-default-block-crash.ll
@@ -0,0 +1,27 @@
+; RUN: opt < %s -lowerswitch -disable-output
+
+; This test verify -lowerswitch does not crash after deleting the default block.
+
+declare i32 @f(i32)
+
+define i32 @unreachable(i32 %x) {
+
+entry:
+  switch i32 %x, label %unreachable [
+    i32 5, label %a
+    i32 6, label %a
+    i32 7, label %a
+    i32 10, label %b
+    i32 20, label %b
+    i32 30, label %b
+    i32 40, label %b
+  ]
+unreachable:
+  unreachable
+a:
+  %0 = call i32 @f(i32 0)
+  ret i32 %0
+b:
+  %1 = call i32 @f(i32 1)
+  ret i32 %1
+}
Index: lib/Transforms/Utils/LowerSwitch.cpp
===================================================================
--- lib/Transforms/Utils/LowerSwitch.cpp
+++ lib/Transforms/Utils/LowerSwitch.cpp
@@ -120,6 +120,14 @@
   for (Function::iterator I = F.begin(), E = F.end(); I != E; ) {
     BasicBlock *Cur = I++; // Advance over block so we don't traverse new blocks
 
+    // If the block has no more predecessors, it should be a Default block which
+    // was erased in the previous processSwitchInst() or dead block resulted from
+    // other passes. Delete it and move on to the next iteration.
+    if (Cur != &F.getEntryBlock() && pred_begin(Cur) == pred_end(Cur)) {
+      DeleteDeadBlock(Cur);
+      continue;
+    }
+
     if (SwitchInst *SI = dyn_cast<SwitchInst>(Cur->getTerminator())) {
       Changed = true;
       processSwitchInst(SI);
@@ -515,10 +523,5 @@
   BranchInst::Create(SwitchBlock, OrigBlock);
 
   // We are now done with the switch instruction, delete it.
-  BasicBlock *OldDefault = SI->getDefaultDest();
   CurBlock->getInstList().erase(SI);
-
-  // If the Default block has no more predecessors just remove it.
-  if (pred_begin(OldDefault) == pred_end(OldDefault))
-    DeleteDeadBlock(OldDefault);
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D11852.31735.patch
Type: text/x-patch
Size: 1977 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150810/14a055ed/attachment.bin>


More information about the llvm-commits mailing list