aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
diff options
context:
space:
mode:
authorGábor Spaits <gaborspaits1@gmail.com>2025-09-17 11:44:17 +0200
committerGitHub <noreply@github.com>2025-09-17 09:44:17 +0000
commit88c64f76ed2ca226da99b99f60d316b1519fc7d8 (patch)
tree0c5dfa18ae140377427286faf22b3edbf1c7f2c9 /llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
parentb7f68cb08ca6e6d4a935640e8065d54b1590d144 (diff)
downloadllvm-88c64f76ed2ca226da99b99f60d316b1519fc7d8.zip
llvm-88c64f76ed2ca226da99b99f60d316b1519fc7d8.tar.gz
llvm-88c64f76ed2ca226da99b99f60d316b1519fc7d8.tar.bz2
Revert "Reland "[BasicBlockUtils] Handle funclets when detaching EH p… (#159292)
…ad blocks" (#158435)" This reverts commit 41cef78227eb909181cb9360099b2d92de8d649f.
Diffstat (limited to 'llvm/lib/Transforms/Utils/BasicBlockUtils.cpp')
-rw-r--r--llvm/lib/Transforms/Utils/BasicBlockUtils.cpp113
1 files changed, 28 insertions, 85 deletions
diff --git a/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp b/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
index 91e245e..cad0b4c 100644
--- a/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
+++ b/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
@@ -58,94 +58,37 @@ static cl::opt<unsigned> MaxDeoptOrUnreachableSuccessorCheckDepth(
"is followed by a block that either has a terminating "
"deoptimizing call or is terminated with an unreachable"));
-/// Zap all the instructions in the block and replace them with an unreachable
-/// instruction and notify the basic block's successors that one of their
-/// predecessors is going away.
-static void
-emptyAndDetachBlock(BasicBlock *BB,
- SmallVectorImpl<DominatorTree::UpdateType> *Updates,
- bool KeepOneInputPHIs) {
- // Loop through all of our successors and make sure they know that one
- // of their predecessors is going away.
- SmallPtrSet<BasicBlock *, 4> UniqueSuccessors;
- for (BasicBlock *Succ : successors(BB)) {
- Succ->removePredecessor(BB, KeepOneInputPHIs);
- if (Updates && UniqueSuccessors.insert(Succ).second)
- Updates->push_back({DominatorTree::Delete, BB, Succ});
- }
-
- // Zap all the instructions in the block.
- while (!BB->empty()) {
- Instruction &I = BB->back();
- // 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
- // value with. Note that since this block is unreachable, and all values
- // contained within it must dominate their uses, that all uses will
- // eventually be removed (they are themselves dead).
- if (!I.use_empty())
- I.replaceAllUsesWith(PoisonValue::get(I.getType()));
- BB->back().eraseFromParent();
- }
- new UnreachableInst(BB->getContext(), BB);
- assert(BB->size() == 1 && isa<UnreachableInst>(BB->getTerminator()) &&
- "The successor list of BB isn't empty before "
- "applying corresponding DTU updates.");
-}
-
-void llvm::detachDeadBlocks(ArrayRef<BasicBlock *> BBs,
- SmallVectorImpl<DominatorTree::UpdateType> *Updates,
- bool KeepOneInputPHIs) {
+void llvm::detachDeadBlocks(
+ ArrayRef<BasicBlock *> BBs,
+ SmallVectorImpl<DominatorTree::UpdateType> *Updates,
+ bool KeepOneInputPHIs) {
for (auto *BB : BBs) {
- auto NonFirstPhiIt = BB->getFirstNonPHIIt();
- if (NonFirstPhiIt != BB->end()) {
- Instruction &I = *NonFirstPhiIt;
- // Exception handling funclets need to be explicitly addressed.
- // These funclets must begin with cleanuppad or catchpad and end with
- // cleanupred or catchret. The return instructions can be in different
- // basic blocks than the pad instruction. If we would only delete the
- // first block, the we would have possible cleanupret and catchret
- // instructions with poison arguments, which wouldn't be valid.
- if (isa<FuncletPadInst>(I)) {
- for (User *User : make_early_inc_range(I.users())) {
- Instruction *ReturnInstr = dyn_cast<Instruction>(User);
- // If we have a cleanupret or catchret block, replace it with just an
- // unreachable. The other alternative, that may use a catchpad is a
- // catchswitch. That does not need special handling for now.
- if (isa<CatchReturnInst>(ReturnInstr) ||
- isa<CleanupReturnInst>(ReturnInstr)) {
- BasicBlock *ReturnInstrBB = ReturnInstr->getParent();
- // This catchret or catchpad basic block is detached now. Let the
- // successors know it.
- // This basic block also may have some predecessors too. For
- // example the following LLVM-IR is valid:
- //
- // [cleanuppad_block]
- // |
- // [regular_block]
- // |
- // [cleanupret_block]
- //
- // The IR after the cleanup will look like this:
- //
- // [cleanuppad_block]
- // |
- // [regular_block]
- // |
- // [unreachable]
- //
- // So regular_block will lead to an unreachable block, which is also
- // valid. There is no need to replace regular_block with unreachable
- // in this context now.
- // On the other hand, the cleanupret/catchret block's successors
- // need to know about the deletion of their predecessors.
- emptyAndDetachBlock(ReturnInstrBB, Updates, KeepOneInputPHIs);
- }
- }
- }
+ // Loop through all of our successors and make sure they know that one
+ // of their predecessors is going away.
+ SmallPtrSet<BasicBlock *, 4> UniqueSuccessors;
+ for (BasicBlock *Succ : successors(BB)) {
+ Succ->removePredecessor(BB, KeepOneInputPHIs);
+ if (Updates && UniqueSuccessors.insert(Succ).second)
+ Updates->push_back({DominatorTree::Delete, BB, Succ});
}
- // Detaching and emptying the current basic block.
- emptyAndDetachBlock(BB, Updates, KeepOneInputPHIs);
+ // Zap all the instructions in the block.
+ while (!BB->empty()) {
+ Instruction &I = BB->back();
+ // 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
+ // value with. Note that since this block is unreachable, and all values
+ // contained within it must dominate their uses, that all uses will
+ // eventually be removed (they are themselves dead).
+ if (!I.use_empty())
+ I.replaceAllUsesWith(PoisonValue::get(I.getType()));
+ BB->back().eraseFromParent();
+ }
+ new UnreachableInst(BB->getContext(), BB);
+ assert(BB->size() == 1 &&
+ isa<UnreachableInst>(BB->getTerminator()) &&
+ "The successor list of BB isn't empty before "
+ "applying corresponding DTU updates.");
}
}