[llvm] [CloneFunction] Fix non-deterministic PHI cleanup using PHINode::removeIncomingValueIf() (PR #173975)

Mingjie Xu via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 30 03:08:09 PST 2025


================
@@ -810,24 +810,17 @@ void llvm::CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
     NumPreds = pred_size(NewBB);
     if (NumPreds != PN->getNumIncomingValues()) {
       assert(NumPreds < PN->getNumIncomingValues());
-      // Count how many times each predecessor comes to this block.
-      std::map<BasicBlock *, unsigned> PredCount;
-      for (BasicBlock *Pred : predecessors(NewBB))
-        --PredCount[Pred];
-
-      // Figure out how many entries to remove from each PHI.
-      for (BasicBlock *Pred : PN->blocks())
-        ++PredCount[Pred];
-
-      // At this point, the excess predecessor entries are positive in the
-      // map.  Loop over all of the PHIs and remove excess predecessor
-      // entries.
+      SmallPtrSet<BasicBlock *, 4> NewBBPreds(llvm::from_range,
+                                              predecessors(NewBB));
+
       BasicBlock::iterator I = NewBB->begin();
       for (; (PN = dyn_cast<PHINode>(I)); ++I) {
-        for (const auto &[Pred, Count] : PredCount) {
-          for ([[maybe_unused]] unsigned _ : llvm::seq<unsigned>(Count))
-            PN->removeIncomingValue(Pred, false);
-        }
+        PN->removeIncomingValueIf(
----------------
Enna1 wrote:

Here is a behavior change compared with previous implementation.

If a block as a predecessor occurs N times in NewBB predecessors list, not sure should the PHI node have exactly N incoming values for that block?

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


More information about the llvm-commits mailing list