Changeset View
Changeset View
Standalone View
Standalone View
llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
Show All 39 Lines | |||||
#include <cassert> | #include <cassert> | ||||
#include <cstdint> | #include <cstdint> | ||||
#include <string> | #include <string> | ||||
#include <utility> | #include <utility> | ||||
#include <vector> | #include <vector> | ||||
using namespace llvm; | using namespace llvm; | ||||
void llvm::DeleteDeadBlock(BasicBlock *BB) { | void llvm::DeleteDeadBlock(BasicBlock *BB, DeferredDominance *DDT) { | ||||
assert((pred_begin(BB) == pred_end(BB) || | assert((pred_begin(BB) == pred_end(BB) || | ||||
// Can delete self loop. | // Can delete self loop. | ||||
BB->getSinglePredecessor() == BB) && "Block is not dead!"); | BB->getSinglePredecessor() == BB) && "Block is not dead!"); | ||||
TerminatorInst *BBTerm = BB->getTerminator(); | TerminatorInst *BBTerm = BB->getTerminator(); | ||||
std::vector<DominatorTree::UpdateType> Updates; | |||||
// Loop through all of our successors and make sure they know that one | // Loop through all of our successors and make sure they know that one | ||||
// of their predecessors is going away. | // of their predecessors is going away. | ||||
for (BasicBlock *Succ : BBTerm->successors()) | if (DDT) | ||||
Updates.reserve(BBTerm->getNumSuccessors()); | |||||
for (BasicBlock *Succ : BBTerm->successors()) { | |||||
Succ->removePredecessor(BB); | Succ->removePredecessor(BB); | ||||
if (DDT) | |||||
Updates.push_back({DominatorTree::Delete, BB, Succ}); | |||||
} | |||||
// Zap all the instructions in the block. | // Zap all the instructions in the block. | ||||
while (!BB->empty()) { | while (!BB->empty()) { | ||||
Instruction &I = BB->back(); | Instruction &I = BB->back(); | ||||
// If this instruction is used, replace uses with an arbitrary value. | // If this instruction is used, replace uses with an arbitrary value. | ||||
// Because control flow can't get here, we don't care what we replace the | // Because control flow can't get here, we don't care what we replace the | ||||
// value with. Note that since this block is unreachable, and all values | // value with. Note that since this block is unreachable, and all values | ||||
// contained within it must dominate their uses, that all uses will | // contained within it must dominate their uses, that all uses will | ||||
// eventually be removed (they are themselves dead). | // eventually be removed (they are themselves dead). | ||||
if (!I.use_empty()) | if (!I.use_empty()) | ||||
I.replaceAllUsesWith(UndefValue::get(I.getType())); | I.replaceAllUsesWith(UndefValue::get(I.getType())); | ||||
BB->getInstList().pop_back(); | BB->getInstList().pop_back(); | ||||
} | } | ||||
// Zap the block! | if (DDT) { | ||||
BB->eraseFromParent(); | DDT->applyUpdates(Updates); | ||||
DDT->deleteBB(BB); // Deferred deletion of BB. | |||||
} else { | |||||
BB->eraseFromParent(); // Zap the block! | |||||
} | |||||
} | } | ||||
void llvm::FoldSingleEntryPHINodes(BasicBlock *BB, | void llvm::FoldSingleEntryPHINodes(BasicBlock *BB, | ||||
MemoryDependenceResults *MemDep) { | MemoryDependenceResults *MemDep) { | ||||
if (!isa<PHINode>(BB->begin())) return; | if (!isa<PHINode>(BB->begin())) return; | ||||
while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) { | while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) { | ||||
if (PN->getIncomingValue(0) != PN) | if (PN->getIncomingValue(0) != PN) | ||||
▲ Show 20 Lines • Show All 724 Lines • Show Last 20 Lines |