[llvm] [MSSAUpdater] Replace recursion with worklist and cap it. (PR #150543)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 25 01:06:29 PDT 2025


================
@@ -230,9 +223,61 @@ MemoryAccess *MemorySSAUpdater::tryRemoveTrivialPhi(MemoryPhi *Phi,
     removeMemoryAccess(Phi);
   }
 
-  // We should only end up recursing in case we replaced something, in which
-  // case, we may have made other Phis trivial.
-  return recursePhi(Same);
+  // Continue traversal in a DFS worklist approach, in case we might find
+  // other trivial Phis.
+  if (!Same)
+    return nullptr;
+
+  TrackingVH<MemoryAccess> Result(Same);
+
+  // Worklist approach to recursively removing trivial Phis.
+  SmallVector<TrackingVH<Value>, 5> Worklist;
+
+  for (auto U : Same->users()) {
+    if (dyn_cast<MemoryPhi>(&*U))
+      Worklist.push_back(TrackingVH<Value>(U));
+  }
+
+  while (!Worklist.empty() && Worklist.size() < TrivialPhiProcessingLimit) {
+    MemoryPhi *RecPhi = dyn_cast<MemoryPhi>(&*(Worklist[Worklist.size() - 1]));
+    Worklist.pop_back();
+
+    if (!RecPhi)
+      continue;
+    auto RecOperands = RecPhi->operands();
+
+    // Repeat above algorithm.
----------------
nikic wrote:

Instead of repeating it, can we handle the base case by starting with a one element worklist?

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


More information about the llvm-commits mailing list