[llvm] 5850c44 - [InstCombine] Improve `foldDeadPhiWeb` compile time (#158057)

via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 14 19:28:44 PDT 2025


Author: Mingjie Xu
Date: 2025-09-15T10:28:40+08:00
New Revision: 5850c44288cba01c75f8afb7cfcf3a2d47e2d5a4

URL: https://github.com/llvm/llvm-project/commit/5850c44288cba01c75f8afb7cfcf3a2d47e2d5a4
DIFF: https://github.com/llvm/llvm-project/commit/5850c44288cba01c75f8afb7cfcf3a2d47e2d5a4.diff

LOG: [InstCombine] Improve `foldDeadPhiWeb` compile time (#158057)

The foldDeadPhiWeb function identifies and removes small dead PHI webs,
it bails out if the web size exceeds threshold (16).

In the current implementation, when there is a phi node has large number
of users that most of them are phi nodes, we still push them on the
`Stack` even if the number of phi nodes user exceeds the threshold.

This patch checks the early stop condition when we push an unvisited phi
node on the `Stack`.
With this change, the wall duration of total instcombine pass decreased
from 523,649.276 ms to 208,687.042 ms in an our internal case.

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp b/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp
index ed9a0be6981fa..15e7172c6ce12 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp
@@ -60,17 +60,18 @@ bool InstCombinerImpl::foldDeadPhiWeb(PHINode &PN) {
   SmallVector<PHINode *, 16> Stack;
   SmallPtrSet<PHINode *, 16> Visited;
   Stack.push_back(&PN);
+  Visited.insert(&PN);
   while (!Stack.empty()) {
     PHINode *Phi = Stack.pop_back_val();
-    if (!Visited.insert(Phi).second)
-      continue;
-    // Early stop if the set of PHIs is large
-    if (Visited.size() == 16)
-      return false;
     for (User *Use : Phi->users()) {
-      if (PHINode *PhiUse = dyn_cast<PHINode>(Use))
+      if (PHINode *PhiUse = dyn_cast<PHINode>(Use)) {
+        if (!Visited.insert(PhiUse).second)
+          continue;
+        // Early stop if the set of PHIs is large
+        if (Visited.size() >= 16)
+          return false;
         Stack.push_back(PhiUse);
-      else
+      } else
         return false;
     }
   }


        


More information about the llvm-commits mailing list