From 41e1dffcf0066ab1ee3de6c888454a476c343366 Mon Sep 17 00:00:00 2001 From: Max Kazantsev Date: Wed, 6 Feb 2019 07:56:36 +0000 Subject: [PATCH] [NFC] Factor out detatchment of dead blocks from their erasing git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@353277 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../llvm/Transforms/Utils/BasicBlockUtils.h | 6 +++ lib/Transforms/Utils/BasicBlockUtils.cpp | 44 +++++++++++-------- 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/include/llvm/Transforms/Utils/BasicBlockUtils.h b/include/llvm/Transforms/Utils/BasicBlockUtils.h index a08d2a760a2..fad01f6e8b3 100644 --- a/include/llvm/Transforms/Utils/BasicBlockUtils.h +++ b/include/llvm/Transforms/Utils/BasicBlockUtils.h @@ -39,6 +39,12 @@ class ReturnInst; class TargetLibraryInfo; class Value; +/// Replace contents of every block in \p BBs with single unreachable +/// instruction. If \p Updates is specified, collect all necessary DT updates +/// into this vector. +void DetatchDeadBlocks(ArrayRef BBs, + SmallVectorImpl *Updates); + /// Delete the specified block, which must have no predecessors. void DeleteDeadBlock(BasicBlock *BB, DomTreeUpdater *DTU = nullptr); diff --git a/lib/Transforms/Utils/BasicBlockUtils.cpp b/lib/Transforms/Utils/BasicBlockUtils.cpp index cb8614fcb98..41ad4fefe1f 100644 --- a/lib/Transforms/Utils/BasicBlockUtils.cpp +++ b/lib/Transforms/Utils/BasicBlockUtils.cpp @@ -47,29 +47,17 @@ using namespace llvm; -void llvm::DeleteDeadBlock(BasicBlock *BB, DomTreeUpdater *DTU) { - DeleteDeadBlocks({BB}, DTU); -} - -void llvm::DeleteDeadBlocks(ArrayRef BBs, - DomTreeUpdater *DTU) { -#ifndef NDEBUG - // Make sure that all predecessors of each dead block is also dead. - SmallPtrSet 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 Updates; +void llvm::DetatchDeadBlocks( + ArrayRef BBs, + SmallVectorImpl *Updates) { for (auto *BB : BBs) { // Loop through all of our successors and make sure they know that one // of their predecessors is going away. + SmallPtrSet UniqueSuccessors; for (BasicBlock *Succ : successors(BB)) { Succ->removePredecessor(BB); - if (DTU) - Updates.push_back({DominatorTree::Delete, BB, Succ}); + if (Updates && UniqueSuccessors.insert(Succ).second) + Updates->push_back({DominatorTree::Delete, BB, Succ}); } // Zap all the instructions in the block. @@ -90,6 +78,26 @@ void llvm::DeleteDeadBlocks(ArrayRef BBs, "The successor list of BB isn't empty before " "applying corresponding DTU updates."); } +} + +void llvm::DeleteDeadBlock(BasicBlock *BB, DomTreeUpdater *DTU) { + DeleteDeadBlocks({BB}, DTU); +} + +void llvm::DeleteDeadBlocks(ArrayRef BBs, + DomTreeUpdater *DTU) { +#ifndef NDEBUG + // Make sure that all predecessors of each dead block is also dead. + SmallPtrSet 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 Updates; + DetatchDeadBlocks(BBs, DTU ? &Updates : nullptr); + if (DTU) DTU->applyUpdates(Updates, /*ForceRemoveDuplicates*/ true); -- 2.40.0