[llvm] [IR] Optimize PHINode::removeIncomingValueIf() using two-pointer (PR #171961)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Dec 11 19:46:50 PST 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-ir
Author: Mingjie Xu (Enna1)
<details>
<summary>Changes</summary>
---
Full diff: https://github.com/llvm/llvm-project/pull/171961.diff
1 Files Affected:
- (modified) llvm/lib/IR/Instructions.cpp (+16-17)
``````````diff
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) {
``````````
</details>
https://github.com/llvm/llvm-project/pull/171961
More information about the llvm-commits
mailing list