[llvm] [DebugInfo][RemoveDIs] Instrument loop-deletion for DPValues (PR #73042)

Jeremy Morse via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 23 04:16:06 PST 2023


================
@@ -642,12 +661,21 @@ 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);
----------------
jmorse wrote:

Alas this is inherent in dbg.values having an absolute ordering and us trying to have completely identical outputs -- the reason why this reversal exists is that it can have a meaningful effect on output when building real codebases :(, and we've had to fix it up. This is something we can fix, but as part of a future "what happens when you jam two blocks of debug-info together" refactor.

https://github.com/llvm/llvm-project/pull/73042


More information about the llvm-commits mailing list