[llvm] [LoopDeletion] Check for uses in unreachable basic blocks even when there is no exit block. (PR #173428)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Wed Dec 24 01:15:43 PST 2025
================
@@ -608,45 +608,45 @@ void llvm::deleteDeadLoop(Loop *L, DominatorTree *DT, ScalarEvolution *SE,
llvm::SmallDenseSet<DebugVariable, 4> DeadDebugSet;
llvm::SmallVector<DbgVariableRecord *, 4> DeadDbgVariableRecords;
- if (ExitBlock) {
- // Given LCSSA form is satisfied, we should not have users of instructions
- // within the dead loop outside of the loop. However, LCSSA doesn't take
- // unreachable uses into account. We handle them here.
- // We could do it after drop all references (in this case all users in the
- // loop will be already eliminated and we have less work to do but according
- // to API doc of User::dropAllReferences only valid operation after dropping
- // references, is deletion. So let's substitute all usages of
- // instruction from the loop with poison value of corresponding type first.
- for (auto *Block : L->blocks())
- for (Instruction &I : *Block) {
- auto *Poison = PoisonValue::get(I.getType());
- for (Use &U : llvm::make_early_inc_range(I.uses())) {
- if (auto *Usr = dyn_cast<Instruction>(U.getUser()))
- if (L->contains(Usr->getParent()))
- continue;
- // If we have a DT then we can check that uses outside a loop only in
- // unreachable block.
- if (DT)
- assert(!DT->isReachableFromEntry(U) &&
- "Unexpected user in reachable block");
- U.set(Poison);
- }
-
- // For one of each variable encountered, preserve a debug record (set
- // to Poison) and transfer it to the loop exit. This terminates any
- // variable locations that were set during the loop.
- for (DbgVariableRecord &DVR :
- llvm::make_early_inc_range(filterDbgVars(I.getDbgRecordRange()))) {
- DebugVariable Key(DVR.getVariable(), DVR.getExpression(),
- DVR.getDebugLoc().get());
- if (!DeadDebugSet.insert(Key).second)
+ // Given LCSSA form is satisfied, we should not have users of instructions
+ // within the dead loop outside of the loop. However, LCSSA doesn't take
+ // unreachable uses into account. We handle them here.
+ // We could do it after drop all references (in this case all users in the
+ // loop will be already eliminated and we have less work to do but according
+ // to API doc of User::dropAllReferences only valid operation after dropping
+ // references, is deletion. So let's substitute all usages of
+ // instruction from the loop with poison value of corresponding type first.
+ for (auto *Block : L->blocks())
+ for (Instruction &I : *Block) {
+ auto *Poison = PoisonValue::get(I.getType());
+ for (Use &U : llvm::make_early_inc_range(I.uses())) {
+ if (auto *Usr = dyn_cast<Instruction>(U.getUser()))
+ if (L->contains(Usr->getParent()))
continue;
- // Unlinks the DVR from it's container, for later insertion.
- DVR.removeFromParent();
- DeadDbgVariableRecords.push_back(&DVR);
- }
+ // If we have a DT then we can check that uses outside a loop only in
+ // unreachable block.
+ if (DT)
+ assert(!DT->isReachableFromEntry(U) &&
+ "Unexpected user in reachable block");
+ U.set(Poison);
}
+ // For one of each variable encountered, preserve a debug record (set
+ // to Poison) and transfer it to the loop exit. This terminates any
+ // variable locations that were set during the loop.
+ for (DbgVariableRecord &DVR :
----------------
nikic wrote:
I think this block still needs to be guarded by ExitBlock. Otherwise we'll leak these records? (They get unlinked but not deleted.)
https://github.com/llvm/llvm-project/pull/173428
More information about the llvm-commits
mailing list