[llvm] 1ea9f44 - [IR] Optimize PHINode::removeIncomingValueIf() using two-pointer (#171961)

via llvm-commits llvm-commits at lists.llvm.org
Sat Dec 13 18:27:13 PST 2025


Author: Mingjie Xu
Date: 2025-12-14T10:27:09+08:00
New Revision: 1ea9f44f29f87496c2fa1df5cfa3cb28fd0aebe9

URL: https://github.com/llvm/llvm-project/commit/1ea9f44f29f87496c2fa1df5cfa3cb28fd0aebe9
DIFF: https://github.com/llvm/llvm-project/commit/1ea9f44f29f87496c2fa1df5cfa3cb28fd0aebe9.diff

LOG: [IR] Optimize PHINode::removeIncomingValueIf() using two-pointer (#171961)

Added: 
    

Modified: 
    llvm/lib/IR/Instructions.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/IR/Instructions.cpp b/llvm/lib/IR/Instructions.cpp
index db0d5af83655f..aa46cbfc59f6b 100644
--- a/llvm/lib/IR/Instructions.cpp
+++ b/llvm/lib/IR/Instructions.cpp
@@ -161,28 +161,27 @@ Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
 
 void PHINode::removeIncomingValueIf(function_ref<bool(unsigned)> Predicate,
                                     bool DeletePHIIfEmpty) {
-  SmallDenseSet<unsigned> RemoveIndices;
-  for (unsigned Idx = 0; Idx < getNumIncomingValues(); ++Idx)
+  unsigned NumOps = getNumIncomingValues();
+  unsigned NewNumOps = 0;
+  for (unsigned Idx = 0; Idx < NumOps; ++Idx) {
     if (Predicate(Idx))
-      RemoveIndices.insert(Idx);
+      continue;
 
-  if (RemoveIndices.empty())
+    if (Idx != NewNumOps) {
+      setIncomingValue(NewNumOps, getIncomingValue(Idx));
+      setIncomingBlock(NewNumOps, getIncomingBlock(Idx));
+    }
+    ++NewNumOps;
+  }
+
+  if (NewNumOps == NumOps)
     return;
 
   // Remove operands.
-  auto NewOpEnd = remove_if(operands(), [&](Use &U) {
-    return RemoveIndices.contains(U.getOperandNo());
-  });
-  for (Use &U : make_range(NewOpEnd, op_end()))
-    U.set(nullptr);
-
-  // Remove incoming blocks.
-  (void)std::remove_if(const_cast<block_iterator>(block_begin()),
-                 const_cast<block_iterator>(block_end()), [&](BasicBlock *&BB) {
-                   return RemoveIndices.contains(&BB - block_begin());
-                 });
-
-  setNumHungOffUseOperands(getNumOperands() - RemoveIndices.size());
+  for (unsigned Idx = NewNumOps; Idx < NumOps; ++Idx)
+    getOperandUse(Idx).set(nullptr);
+
+  setNumHungOffUseOperands(NewNumOps);
 
   // If the PHI node is dead, because it has zero entries, nuke it now.
   if (getNumOperands() == 0 && DeletePHIIfEmpty) {


        


More information about the llvm-commits mailing list