[llvm] [WebAssembly] Avoid scanning unrelated debug values (NFCI) (PR #218378)

Heejin Ahn via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 24 21:51:43 PDT 2026


================
@@ -35,26 +37,33 @@ WebAssemblyDebugValueManager::WebAssemblyDebugValueManager(MachineInstr *Def)
 
   // Collect all the uses of this def.
   MachineRegisterInfo &MRI = Def->getMF()->getRegInfo();
-  SmallVector<MachineInstr *, 2> Candidates;
-  for (MachineInstr &MI : MRI.use_instructions(CurrentReg)) {
-    if (MI.isDebugValue() && MI.getParent() == Def->getParent())
-      Candidates.push_back(&MI);
-  }
-  if (Candidates.empty())
+  MachineBasicBlock *MBB = Def->getParent();
+  unsigned RemainingUses = 0;
+  for (MachineInstr &MI : MRI.use_instructions(CurrentReg))
+    if (MI.isDebugValue() && MI.getParent() == MBB)
+      ++RemainingUses;
+  if (RemainingUses == 0)
     return;
 
   // To preserve the order of DBG_VALUEs and correctly handle non-SSA cases,
   // we scan the BB as far as needed to find all candidates.
-  for (MachineBasicBlock::iterator MI = std::next(Def->getIterator()),
-                                   ME = Def->getParent()->end();
-       MI != ME; ++MI) {
-    // If another definition appears, stop
-    if (MI->definesRegister(CurrentReg, /*TRI=*/nullptr))
+  MachineBasicBlock::iterator Down = std::next(Def->getIterator()),
+                              DownEnd = MBB->end(), Up = Def->getIterator(),
+                              UpBegin = MBB->begin();
+  while (RemainingUses > 0 && Down != DownEnd) {
+    if (Down->isDebugValue()) {
+      if (Down->hasDebugOperandForReg(CurrentReg)) {
+        DbgValues.push_back(&*Down);
+        --RemainingUses;
+      }
+    } else if (Down->definesRegister(CurrentReg, /*TRI=*/nullptr)) {
       break;
-    if (MI->isDebugValue() && MI->hasDebugOperandForReg(CurrentReg)) {
-      DbgValues.push_back(&*MI);
-      if (DbgValues.size() == Candidates.size())
-        break;
+    }
+    ++Down;
+    if (Up != UpBegin) {
+      --Up;
+      if (Up->isDebugValue() && Up->hasDebugOperandForReg(CurrentReg))
+        --RemainingUses;
     }
   }
----------------
aheejin wrote:

Can you add some comments on why we do this here? The description of the PR has some description but there is no comment for why we are doing this. Also why we do this one upward/downward traversal per iteration too, which can be confusing at first glance. Also it would be helpful to clarify that we don't actually gather debug values backwards.

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


More information about the llvm-commits mailing list