[llvm] [JumpThreading] Refractor redirectValuesFromPredecessorsToPhi: avoid unnecessary map resizing in gatherIncomingValuesToPhi (PR #173596)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 8 08:02:34 PST 2026


================
@@ -925,37 +925,48 @@ using IncomingValueMap = SmallDenseMap<BasicBlock *, Value *, 16>;
 /// \returns the selected value.
 static Value *selectIncomingValueForBlock(Value *OldVal, BasicBlock *BB,
                                           IncomingValueMap &IncomingValues) {
+  IncomingValueMap::const_iterator It = IncomingValues.find(BB);
   if (!isa<UndefValue>(OldVal)) {
-    assert((!IncomingValues.count(BB) ||
-            IncomingValues.find(BB)->second == OldVal) &&
+    assert((It != IncomingValues.end() &&
+            (!(It->second) || It->second == OldVal)) &&
            "Expected OldVal to match incoming value from BB!");
 
-    IncomingValues.insert(std::make_pair(BB, OldVal));
+    IncomingValues.insert_or_assign(BB, OldVal);
     return OldVal;
   }
 
-  IncomingValueMap::const_iterator It = IncomingValues.find(BB);
-  if (It != IncomingValues.end()) return It->second;
+  if (It != IncomingValues.end() && It->second)
+    return It->second;
 
   return OldVal;
 }
 
 /// Create a map from block to value for the operands of a
 /// given phi.
 ///
-/// Create a map from block to value for each non-undef value flowing
-/// into \p PN.
+/// This function initializes the map with UndefValue for all predecessors
+/// in BBPreds, and then updates the map with concrete non-undef values
+/// found in the PHI node.
 ///
 /// \param PN The phi we are collecting the map for.
+/// \param BBPreds The list of all predecessor blocks to initialize with Undef.
 /// \param IncomingValues [out] The map from block to value for this phi.
-static void gatherIncomingValuesToPhi(PHINode *PN,
-                                      IncomingValueMap &IncomingValues) {
+static void gatherIncomingValuesToPhi(llvm::PHINode *PN,
+                                      IncomingValueMap &IncomingValues,
+                                      const PredBlockVector &BBPreds) {
+  for (llvm::BasicBlock *Pred : BBPreds) {
----------------
nikic wrote:

Remove all the `llvm::` qualifiers.

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


More information about the llvm-commits mailing list