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(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); } 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 +}