diff --git a/include/llvm/Transforms/Utils/Local.h b/include/llvm/Transforms/Utils/Local.h index 8172afa1ea2..32a05eba857 100644 --- a/include/llvm/Transforms/Utils/Local.h +++ b/include/llvm/Transforms/Utils/Local.h @@ -295,7 +295,7 @@ unsigned removeAllNonTerminatorAndEHPadInstructions(BasicBlock *BB); /// \brief Insert an unreachable instruction before the specified /// instruction, making it and the rest of the code in the block dead. -void changeToUnreachable(Instruction *I, bool UseLLVMTrap); +unsigned changeToUnreachable(Instruction *I, bool UseLLVMTrap); /// Replace 'BB's terminator with one that does not have an unwind successor /// block. Rewrites `invoke` to `call`, etc. Updates any PHIs in unwind diff --git a/lib/Transforms/Scalar/SCCP.cpp b/lib/Transforms/Scalar/SCCP.cpp index 902fbdfc295..d1b0d2547e2 100644 --- a/lib/Transforms/Scalar/SCCP.cpp +++ b/lib/Transforms/Scalar/SCCP.cpp @@ -1564,14 +1564,6 @@ FunctionPass *llvm::createSCCPPass() { return new SCCP(); } -static void DeleteInstructionInBlock(BasicBlock *BB) { - DEBUG(dbgs() << " BasicBlock Dead:" << *BB); - ++NumDeadBlocks; - - unsigned NumRemovedInBB = removeAllNonTerminatorAndEHPadInstructions(BB); - NumInstRemoved += NumRemovedInBB; -} - // runOnFunction() - Run the Sparse Conditional Constant Propagation algorithm, // and return true if the function was modified. // @@ -1608,7 +1600,11 @@ bool SCCP::runOnFunction(Function &F) { for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) { if (!Solver.isBlockExecutable(&*BB)) { - DeleteInstructionInBlock(&*BB); + DEBUG(dbgs() << " BasicBlock Dead:" << *BB); + + ++NumDeadBlocks; + NumInstRemoved += removeAllNonTerminatorAndEHPadInstructions(BB); + MadeChanges = true; continue; } @@ -1806,18 +1802,13 @@ bool IPSCCP::runOnModule(Module &M) { for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { if (!Solver.isBlockExecutable(&*BB)) { - DeleteInstructionInBlock(&*BB); - MadeChanges = true; + DEBUG(dbgs() << " BasicBlock Dead:" << *BB); - TerminatorInst *TI = BB->getTerminator(); - for (BasicBlock *Succ : TI->successors()) { - if (!Succ->empty() && isa(Succ->begin())) - Succ->removePredecessor(&*BB); - } - if (!TI->use_empty()) - TI->replaceAllUsesWith(UndefValue::get(TI->getType())); - TI->eraseFromParent(); - new UnreachableInst(M.getContext(), &*BB); + ++NumDeadBlocks; + NumInstRemoved += + changeToUnreachable(&*BB->begin(), /*UseLLVMTrap=*/false); + + MadeChanges = true; if (&*BB != &F->front()) BlocksToErase.push_back(&*BB); diff --git a/lib/Transforms/Utils/Local.cpp b/lib/Transforms/Utils/Local.cpp index d2ac9890b62..fd9c7f6f886 100644 --- a/lib/Transforms/Utils/Local.cpp +++ b/lib/Transforms/Utils/Local.cpp @@ -1243,7 +1243,7 @@ unsigned llvm::removeAllNonTerminatorAndEHPadInstructions(BasicBlock *BB) { return NumDeadInst; } -void llvm::changeToUnreachable(Instruction *I, bool UseLLVMTrap) { +unsigned llvm::changeToUnreachable(Instruction *I, bool UseLLVMTrap) { BasicBlock *BB = I->getParent(); // Loop over all of the successors, removing BB's entry from any PHI // nodes. @@ -1261,12 +1261,15 @@ void llvm::changeToUnreachable(Instruction *I, bool UseLLVMTrap) { new UnreachableInst(I->getContext(), I); // All instructions after this are dead. + unsigned NumInstrsRemoved = 0; BasicBlock::iterator BBI = I->getIterator(), BBE = BB->end(); while (BBI != BBE) { if (!BBI->use_empty()) BBI->replaceAllUsesWith(UndefValue::get(BBI->getType())); BB->getInstList().erase(BBI++); + ++NumInstrsRemoved; } + return NumInstrsRemoved; } /// changeToCall - Convert the specified invoke into a normal call.