[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:42 PDT 2026


================
@@ -79,44 +88,65 @@ WebAssemblyDebugValueManager::getSinkableDebugValues(
     MachineInstr *Insert) const {
   if (DbgValues.empty())
     return {};
-  // DBG_VALUEs between Def and Insert
+
+  // If Def and Insert are in different BBs, we only handle a simple case in
+  // which Insert's BB is a successor of Def's BB.
+  if (Def->getParent() != Insert->getParent() &&
+      !Def->getParent()->isSuccessor(Insert->getParent()))
+    return {};
+
+  SmallDenseSet<std::pair<const DILocalVariable *, const DILocation *>, 4>
+      OurVars;
+  for (MachineInstr *DV : DbgValues)
+    OurVars.insert({DV->getDebugVariable(), DV->getDebugLoc()->getInlinedAt()});
+  auto IsRelevantDbgValue = [&](const MachineInstr &MI) {
+    return MI.isDebugValue() &&
+           OurVars.count(
+               {MI.getDebugVariable(), MI.getDebugLoc()->getInlinedAt()});
+  };
+
   SmallVector<MachineInstr *, 8> DbgValuesInBetween;
 
   if (Def->getParent() == Insert->getParent()) {
     // When Def and Insert are within the same BB, check if Insert comes after
     // Def, because we only support sinking.
+    MachineBasicBlock::iterator Down = std::next(Def->getIterator()),
+                                DownEnd = Def->getParent()->end(),
+                                Up = Def->getIterator(),
+                                UpBegin = Def->getParent()->begin();
     bool DefFirst = false;
-    for (MachineBasicBlock::iterator MI = std::next(Def->getIterator()),
-                                     ME = Def->getParent()->end();
-         MI != ME; ++MI) {
-      if (&*MI == Insert) {
-        DefFirst = true;
-        break;
+    while (Down != DownEnd || Up != UpBegin) {
+      if (Down != DownEnd) {
+        if (&*Down == Insert) {
+          DefFirst = true;
+          break;
+        }
+        if (IsRelevantDbgValue(*Down))
+          DbgValuesInBetween.push_back(&*Down);
+        ++Down;
+      }
+      if (Up != UpBegin) {
+        --Up;
+        if (&*Up == Insert)
+          break;
       }
-      if (MI->isDebugValue())
-        DbgValuesInBetween.push_back(&*MI);
     }
----------------
aheejin wrote:

We need some comments here too

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


More information about the llvm-commits mailing list