diff options
author | Gábor Spaits <gaborspaits1@gmail.com> | 2025-09-20 02:00:17 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-09-19 17:00:17 -0700 |
commit | 0a47e8c2fc337b8dc5dac1778c7a46c3219e157c (patch) | |
tree | e9013ab738ed2ec87dfc1450278aab123215143f /llvm/lib/Transforms/Utils/BasicBlockUtils.cpp | |
parent | a38794ff3d47588cb226881eb048cb2333962ab9 (diff) | |
download | llvm-0a47e8c2fc337b8dc5dac1778c7a46c3219e157c.zip llvm-0a47e8c2fc337b8dc5dac1778c7a46c3219e157c.tar.gz llvm-0a47e8c2fc337b8dc5dac1778c7a46c3219e157c.tar.bz2 |
Reland [BasicBlockUtils] Handle funclets when detaching EH pad blocks (#159379)
Fixes #148052 .
Last PR did not account for the scenario, when more than one instruction
used the `catchpad` label.
In that case I have deleted uses, which were already "choosen to be
iterated over" by the early increment iterator. This issue was not
visible in normal release build on x86, but luckily later on the address
sanitizer build it has found it on the buildbot.
Here is the diff from the last version of this PR: #158435
```diff
diff --git a/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp b/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
index 91e245e5e8f5..1dd8cb4ee584 100644
--- a/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
+++ b/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
@@ -106,7 +106,8 @@ void llvm::detachDeadBlocks(ArrayRef<BasicBlock *> BBs,
// 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())) {
+ SmallPtrSet<BasicBlock *, 4> UniqueEHRetBlocksToDelete;
+ for (User *User : 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
@@ -114,33 +115,12 @@ void llvm::detachDeadBlocks(ArrayRef<BasicBlock *> BBs,
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);
+ UniqueEHRetBlocksToDelete.insert(ReturnInstrBB);
}
}
+ for (BasicBlock *EHRetBB :
+ make_early_inc_range(UniqueEHRetBlocksToDelete))
+ emptyAndDetachBlock(EHRetBB, Updates, KeepOneInputPHIs);
}
}
```
Diffstat (limited to 'llvm/lib/Transforms/Utils/BasicBlockUtils.cpp')
-rw-r--r-- | llvm/lib/Transforms/Utils/BasicBlockUtils.cpp | 97 |
1 files changed, 69 insertions, 28 deletions
diff --git a/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp b/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp index cad0b4c..8714741a 100644 --- a/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp +++ b/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp @@ -58,37 +58,78 @@ static cl::opt<unsigned> MaxDeoptOrUnreachableSuccessorCheckDepth( "is followed by a block that either has a terminating " "deoptimizing call or is terminated with an unreachable")); -void llvm::detachDeadBlocks( - ArrayRef<BasicBlock *> BBs, - SmallVectorImpl<DominatorTree::UpdateType> *Updates, - bool KeepOneInputPHIs) { +/// 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) { + SmallPtrSet<BasicBlock *, 4> UniqueEHRetBlocksToDelete; for (auto *BB : BBs) { - // 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}); - } + 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)) { + UniqueEHRetBlocksToDelete.clear(); + + for (User *User : 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(); + UniqueEHRetBlocksToDelete.insert(ReturnInstrBB); + } + } - // 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(); + for (BasicBlock *EHRetBB : UniqueEHRetBlocksToDelete) + emptyAndDetachBlock(EHRetBB, Updates, KeepOneInputPHIs); + } } - 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."); + + UniqueEHRetBlocksToDelete.clear(); + + // Detaching and emptying the current basic block. + emptyAndDetachBlock(BB, Updates, KeepOneInputPHIs); } } |