diff options
author | Jeremy Morse <jeremy.morse@sony.com> | 2023-11-23 12:24:15 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-23 12:24:15 +0000 |
commit | ce1b24330efb242172324e39099668dcc15c21f0 (patch) | |
tree | 2678a76b53ae63e56a778814836b53dc46595efa /llvm/lib/Transforms/Utils/LoopUtils.cpp | |
parent | 6248c24876d81d83544af02399d46813dbea869c (diff) | |
download | llvm-ce1b24330efb242172324e39099668dcc15c21f0.zip llvm-ce1b24330efb242172324e39099668dcc15c21f0.tar.gz llvm-ce1b24330efb242172324e39099668dcc15c21f0.tar.bz2 |
[DebugInfo][RemoveDIs] Instrument loop-deletion for DPValues (#73042)
Loop deletion identifies dbg.value intrinsics in the loop, sets them to
undef/poison, and sinks them to the exit of the loop, to ensure that any
variable assignments that happen in a deleted loop are "optimised out".
This needs to be replicated for DPValues, the non-instruction
replacement for dbg.value intrinsics.
The movement API for DPValues is (deliberately) more limited than
dbg.values, which is tricky because inserting the collection of
dbg.values at an arbitrary iterator can insert a dbg.value in the middle
of a sequence of dbg.values. A big no-no for DPValues. This patch
replicates the order by inserting DPValues in reverse at the
head-iterator of the block, to ensure the same output as dbg.value mode.
Technically the order isn't important, but we're trying to ensure
identical outputs from optimisation passes right now.
Add more CHECK lines for dbg.values in diundef.ll to ensure that we
don't create any spurious dbg.values, and to ensure that sequences of
dbg.values come out of the optimisation in the correct order.
Diffstat (limited to 'llvm/lib/Transforms/Utils/LoopUtils.cpp')
-rw-r--r-- | llvm/lib/Transforms/Utils/LoopUtils.cpp | 35 |
1 files changed, 32 insertions, 3 deletions
diff --git a/llvm/lib/Transforms/Utils/LoopUtils.cpp b/llvm/lib/Transforms/Utils/LoopUtils.cpp index acce8d1..5948512 100644 --- a/llvm/lib/Transforms/Utils/LoopUtils.cpp +++ b/llvm/lib/Transforms/Utils/LoopUtils.cpp @@ -604,6 +604,7 @@ void llvm::deleteDeadLoop(Loop *L, DominatorTree *DT, ScalarEvolution *SE, // Use a map to unique and a vector to guarantee deterministic ordering. llvm::SmallDenseSet<DebugVariable, 4> DeadDebugSet; llvm::SmallVector<DbgVariableIntrinsic *, 4> DeadDebugInst; + llvm::SmallVector<DPValue *, 4> DeadDPValues; if (ExitBlock) { // Given LCSSA form is satisfied, we should not have users of instructions @@ -628,6 +629,24 @@ void llvm::deleteDeadLoop(Loop *L, DominatorTree *DT, ScalarEvolution *SE, "Unexpected user in reachable block"); U.set(Poison); } + + // RemoveDIs: do the same as below for DPValues. + if (Block->IsNewDbgInfoFormat) { + for (DPValue &DPV : + llvm::make_early_inc_range(I.getDbgValueRange())) { + DebugVariable Key(DPV.getVariable(), DPV.getExpression(), + DPV.getDebugLoc().get()); + if (!DeadDebugSet.insert(Key).second) + continue; + // Unlinks the DPV from it's container, for later insertion. + DPV.removeFromParent(); + DeadDPValues.push_back(&DPV); + } + } + + // For one of each variable encountered, preserve a debug intrinsic (set + // to Poison) and transfer it to the loop exit. This terminates any + // variable locations that were set during the loop. auto *DVI = dyn_cast<DbgVariableIntrinsic>(&I); if (!DVI) continue; @@ -642,12 +661,22 @@ void llvm::deleteDeadLoop(Loop *L, DominatorTree *DT, ScalarEvolution *SE, // be be replaced with undef. Loop invariant values will still be available. // Move dbg.values out the loop so that earlier location ranges are still // terminated and loop invariant assignments are preserved. - Instruction *InsertDbgValueBefore = ExitBlock->getFirstNonPHI(); - assert(InsertDbgValueBefore && + DIBuilder DIB(*ExitBlock->getModule()); + BasicBlock::iterator InsertDbgValueBefore = + ExitBlock->getFirstInsertionPt(); + assert(InsertDbgValueBefore != ExitBlock->end() && "There should be a non-PHI instruction in exit block, else these " "instructions will have no parent."); + for (auto *DVI : DeadDebugInst) - DVI->moveBefore(InsertDbgValueBefore); + DVI->moveBefore(*ExitBlock, InsertDbgValueBefore); + + // Due to the "head" bit in BasicBlock::iterator, we're going to insert + // each DPValue right at the start of the block, wheras dbg.values would be + // repeatedly inserted before the first instruction. To replicate this + // behaviour, do it backwards. + for (DPValue *DPV : llvm::reverse(DeadDPValues)) + ExitBlock->insertDPValueBefore(DPV, InsertDbgValueBefore); } // Remove the block from the reference counting scheme, so that we can |