1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-25 12:12:47 +01:00

[BasicBlockUtils] Generalize DeleteDeadBlock to deal with multiple dead blocks

Utility function `DeleteDeadBlock` expects that all predecessors of a block being
deleted are already deleted, with the exception of single-block loop. It makes it
hard to use for deletion of a set of blocks that may contain cyclic dependencies.
The is no correct order of invocations of this function that does not produce
dangling pointers on already deleted blocks.

This patch introduces a generalized version of this function `DeleteDeadBlocks`
that allows us to remove multiple blocks at once, even if there are cycles among
them. The only requirement is that no block being deleted should have a predecessor
that is not being deleted. 

The logic of `DeleteDeadBlocks` is following:
  for each block
    create relevant DT updates;
    remove all instructions (replace with undef if needed);
    replace terminator with unreacheable;
  apply DT updates;
  for each block
    delete block;

Therefore, `DeleteDeadBlock` becomes a particular case of
the general algorithm called for a single block.

Differential Revision: https://reviews.llvm.org/D56120
Reviewed By: skatkov

llvm-svn: 351045
This commit is contained in:
Max Kazantsev 2019-01-14 10:26:26 +00:00
parent d7fc122a32
commit 2415664d75
2 changed files with 55 additions and 37 deletions

View File

@ -43,6 +43,13 @@ class Value;
/// Delete the specified block, which must have no predecessors.
void DeleteDeadBlock(BasicBlock *BB, DomTreeUpdater *DTU = nullptr);
/// Delete the specified blocks from \p BB. The set of deleted blocks must have
/// no predecessors that are not being deleted themselves. \p BBs must have no
/// duplicating blocks. If there are loops among this set of blocks, all
/// relevant loop info updates should be done before this function is called.
void DeleteDeadBlocks(SmallVectorImpl <BasicBlock *> &BBs,
DomTreeUpdater *DTU = nullptr);
/// We know that BB has one predecessor. If there are any single-entry PHI nodes
/// in it, fold them away. This handles the case when all entries to the PHI
/// nodes in a block are guaranteed equal, such as when the block has exactly

View File

@ -49,17 +49,26 @@
using namespace llvm;
void llvm::DeleteDeadBlock(BasicBlock *BB, DomTreeUpdater *DTU) {
assert((pred_begin(BB) == pred_end(BB) ||
// Can delete self loop.
BB->getSinglePredecessor() == BB) && "Block is not dead!");
Instruction *BBTerm = BB->getTerminator();
std::vector<DominatorTree::UpdateType> Updates;
SmallVector<BasicBlock *, 1> BBs = {BB};
DeleteDeadBlocks(BBs, DTU);
}
void llvm::DeleteDeadBlocks(SmallVectorImpl <BasicBlock *> &BBs,
DomTreeUpdater *DTU) {
#ifndef NDEBUG
// Make sure that all predecessors of each dead block is also dead.
SmallPtrSet<BasicBlock *, 4> Dead(BBs.begin(), BBs.end());
assert(Dead.size() == BBs.size() && "Duplicating blocks?");
for (auto *BB : Dead)
for (BasicBlock *Pred : predecessors(BB))
assert(Dead.count(Pred) && "All predecessors must be dead!");
#endif
SmallVector<DominatorTree::UpdateType, 4> Updates;
for (auto *BB : BBs) {
// Loop through all of our successors and make sure they know that one
// of their predecessors is going away.
if (DTU)
Updates.reserve(BBTerm->getNumSuccessors());
for (BasicBlock *Succ : successors(BBTerm)) {
for (BasicBlock *Succ : successors(BB)) {
Succ->removePredecessor(BB);
if (DTU)
Updates.push_back({DominatorTree::Delete, BB, Succ});
@ -82,13 +91,15 @@ void llvm::DeleteDeadBlock(BasicBlock *BB, DomTreeUpdater *DTU) {
isa<UnreachableInst>(BB->getTerminator()) &&
"The successor list of BB isn't empty before "
"applying corresponding DTU updates.");
if (DTU) {
DTU->applyUpdates(Updates, /*ForceRemoveDuplicates*/ true);
DTU->deleteBB(BB);
} else {
BB->eraseFromParent(); // Zap the block!
}
if (DTU)
DTU->applyUpdates(Updates, /*ForceRemoveDuplicates*/ true);
for (BasicBlock *BB : BBs)
if (DTU)
DTU->deleteBB(BB);
else
BB->eraseFromParent();
}
void llvm::FoldSingleEntryPHINodes(BasicBlock *BB,