[llvm] [InstCombine] Iterative replacement in PtrReplacer (PR #137215)

Jay Foad via llvm-commits llvm-commits at lists.llvm.org
Tue May 27 02:36:50 PDT 2025


================
@@ -365,13 +408,18 @@ void PointerReplacer::replace(Instruction *I) {
     // replacement (new value).
     WorkMap[NewI] = NewI;
   } else if (auto *PHI = dyn_cast<PHINode>(I)) {
-    Type *NewTy = getReplacement(PHI->getIncomingValue(0))->getType();
-    auto *NewPHI = PHINode::Create(NewTy, PHI->getNumIncomingValues(),
-                                   PHI->getName(), PHI->getIterator());
+    // Create a new PHI by replacing any incoming value that is a user of the
+    // root pointer and has a replacement.
+    auto GetReplacementForInValue = [&](Value *V) {
+      return WorkMap.contains(V) ? WorkMap[V] : V;
----------------
jayfoad wrote:

To explain: you are looking up the same value in the same map twice, once with `contains` and once with `[]`. This is wasteful. You can avoid it with something like `I = WorkMap.find(V); return I == WorkMap.end() ? V : I->second;`

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


More information about the llvm-commits mailing list