[llvm] [JumpThreading] Add fast path for single-pred blocks in redirectValuesFromPredecessorsToPhi (PR #173596)

via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 25 22:37:43 PST 2025


https://github.com/int-zjt created https://github.com/llvm/llvm-project/pull/173596

This patch adds a fast path to `redirectValuesFromPredecessorsToPhi` to skip the `gatherIncomingValuesToPhi` call when possible.

Currently, `gatherIncomingValuesToPhi` populates a `SmallDenseMap` with all incoming values of the PHI node. When the PHI node has a large number of incoming values, this process triggers frequent resizing and rehashing of the map, leading to significant compile-time overhead.

However, if the block `BB` has exactly one predecessor and the incoming value used by `PN` is not defined within `BB`, we don't need to resolve any value mappings. We can simply propagate the existing value. This fast path completely avoids the O(N) map construction cost for this common scenario.


>From 9958963e59d13b90179c87151d3ef23c3ab3a465 Mon Sep 17 00:00:00 2001
From: "zhangjiatong.0" <zhangjiatong.0 at bytedance.com>
Date: Thu, 11 Dec 2025 19:48:27 +0800
Subject: [PATCH] [JumpThreading] Add fast path for single-pred blocks in
 redirectValuesFromPredecessorsToPhi

---
 llvm/lib/Transforms/Utils/Local.cpp | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp
index f7842a235b20d..9e5115d0b0795 100644
--- a/llvm/lib/Transforms/Utils/Local.cpp
+++ b/llvm/lib/Transforms/Utils/Local.cpp
@@ -1077,6 +1077,19 @@ static void redirectValuesFromPredecessorsToPhi(BasicBlock *BB,
   Value *OldVal = PN->removeIncomingValue(BB, false);
   assert(OldVal && "No entry in PHI for Pred BB!");
 
+  // Fast path: If BB has a single predecessor and the incoming value is not
+  // defined in BB itself, we can directly redirect the edge.
+  //
+  // Note: We rely on TryToSimplifyUncondBranchFromEmptyBlock (the caller) to
+  // have already verified via CanPropagatePredecessorsForPHIs that merging
+  // blocks won't introduce value conflicts for any common predecessors.
+  auto *BBSinglePred = BB->getSinglePredecessor();
+  Instruction *OldInst = dyn_cast<Instruction>(OldVal);
+  if (BBSinglePred && OldInst && OldInst->getParent() != BB) {
+    PN->addIncoming(OldVal, BBSinglePred);
+    return;
+  }
+
   IncomingValueMap IncomingValues;
 
   // We are merging two blocks - BB, and the block containing PN - and



More information about the llvm-commits mailing list