[llvm] [Utils] Hoist ValueMapper out of per-instruction remap loops in CloneFunction (PR #216014)

Antoine Bastide via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 13 04:33:01 PDT 2026


https://github.com/AntoineBastide47 updated https://github.com/llvm/llvm-project/pull/216014

>From d46dd4941c7d9f79199ee26147f2b02fb6bbde8e Mon Sep 17 00:00:00 2001
From: AntoineBastide47 <148970403+AntoineBastide47 at users.noreply.github.com>
Date: Thu, 13 Aug 2026 10:53:52 +0200
Subject: [PATCH] [Utils] Hoist ValueMapper out of per-instruction remap loops
 in CloneFunction

CTMark hot files: instructions -0.6%; cc1 allocations -997K, allocated bytes -7.8% on tramp3d-v4; binary unchanged.
---
 llvm/lib/Transforms/Utils/CloneFunction.cpp | 43 +++++++++------------
 1 file changed, 19 insertions(+), 24 deletions(-)

diff --git a/llvm/lib/Transforms/Utils/CloneFunction.cpp b/llvm/lib/Transforms/Utils/CloneFunction.cpp
index 7896b78a2c208..b53226f6974a8 100644
--- a/llvm/lib/Transforms/Utils/CloneFunction.cpp
+++ b/llvm/lib/Transforms/Utils/CloneFunction.cpp
@@ -289,6 +289,7 @@ void llvm::CloneFunctionBodyInto(Function &NewFunc, const Function &OldFunc,
 
   // Loop over all of the instructions in the new function, fixing up operand
   // references as we go. This uses VMap to do all the hard work.
+  ValueMapper Mapper(VMap, RemapFlag, TypeMapper, Materializer, IdentityMD);
   for (Function::iterator
            BB = cast<BasicBlock>(VMap[&OldFunc.front()])->getIterator(),
            BE = NewFunc.end();
@@ -296,10 +297,8 @@ void llvm::CloneFunctionBodyInto(Function &NewFunc, const Function &OldFunc,
     // Loop over all instructions, fixing each one as we find it, and any
     // attached debug-info records.
     for (Instruction &II : *BB) {
-      RemapInstruction(&II, VMap, RemapFlag, TypeMapper, Materializer,
-                       IdentityMD);
-      RemapDbgRecordRange(II.getModule(), II.getDbgRecordRange(), VMap,
-                          RemapFlag, TypeMapper, Materializer, IdentityMD);
+      Mapper.remapInstruction(II);
+      Mapper.remapDbgRecordRange(II.getModule(), II.getDbgRecordRange());
     }
 }
 
@@ -447,6 +446,7 @@ struct PruningFunctionCloner {
   Function *NewFunc;
   const Function *OldFunc;
   ValueToValueMapTy &VMap;
+  ValueMapper &Remapper;
   bool ModuleLevelChanges;
   const char *NameSuffix;
   ClonedCodeInfo &CodeInfo;
@@ -456,9 +456,10 @@ struct PruningFunctionCloner {
 
 public:
   PruningFunctionCloner(Function *newFunc, const Function *oldFunc,
-                        ValueToValueMapTy &valueMap, bool moduleLevelChanges,
-                        const char *nameSuffix, ClonedCodeInfo &codeInfo)
-      : NewFunc(newFunc), OldFunc(oldFunc), VMap(valueMap),
+                        ValueToValueMapTy &valueMap, ValueMapper &remapper,
+                        bool moduleLevelChanges, const char *nameSuffix,
+                        ClonedCodeInfo &codeInfo)
+      : NewFunc(newFunc), OldFunc(oldFunc), VMap(valueMap), Remapper(remapper),
         ModuleLevelChanges(moduleLevelChanges), NameSuffix(nameSuffix),
         CodeInfo(codeInfo) {
     HostFuncIsStrictFP =
@@ -587,8 +588,7 @@ void PruningFunctionCloner::CloneBlock(
     // Eagerly remap operands to the newly cloned instruction, except for PHI
     // nodes for which we defer processing until we update the CFG.
     if (!isa<PHINode>(NewInst)) {
-      RemapInstruction(NewInst, VMap,
-                       ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges);
+      Remapper.remapInstruction(*NewInst);
 
       // Eagerly constant fold the newly cloned instruction. If successful, add
       // a mapping to the new value. Non-constant operands may be incomplete at
@@ -726,7 +726,10 @@ void llvm::CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
       assert(VMap.count(&II) && "No mapping from source argument specified!");
 #endif
 
-  PruningFunctionCloner PFC(NewFunc, OldFunc, VMap, ModuleLevelChanges,
+  ValueMapper Mapper(VMap,
+                     ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges,
+                     TypeMapper, Materializer);
+  PruningFunctionCloner PFC(NewFunc, OldFunc, VMap, Mapper, ModuleLevelChanges,
                             NameSuffix, CodeInfo);
   const BasicBlock *StartingBB;
   if (StartingInst)
@@ -773,9 +776,7 @@ void llvm::CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
 
     // Finally, remap the terminator instructions, as those can't be remapped
     // until all BBs are mapped.
-    RemapInstruction(NewBB->getTerminator(), VMap,
-                     ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges,
-                     TypeMapper, Materializer);
+    Mapper.remapInstruction(*NewBB->getTerminator());
   }
 
   // Defer PHI resolution until rest of function is resolved, PHI resolution
@@ -796,9 +797,7 @@ void llvm::CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
       for (int64_t pred = NumPreds - 1; pred >= 0; --pred) {
         Value *V = VMap.lookup(PN->getIncomingBlock(pred));
         if (BasicBlock *MappedBlock = cast_or_null<BasicBlock>(V)) {
-          Value *InVal =
-              MapValue(PN->getIncomingValue(pred), VMap,
-                       ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges);
+          Value *InVal = Mapper.mapValue(*PN->getIncomingValue(pred));
           assert(InVal && "Unknown input value?");
           PN->setIncomingValue(pred, InVal);
           PN->setIncomingBlock(pred, MappedBlock);
@@ -904,10 +903,7 @@ void llvm::CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
   Function::iterator Begin = cast<BasicBlock>(VMap[StartingBB])->getIterator();
   for (BasicBlock &BB : make_range(Begin, NewFunc->end())) {
     for (Instruction &I : BB) {
-      RemapDbgRecordRange(I.getModule(), I.getDbgRecordRange(), VMap,
-                          ModuleLevelChanges ? RF_None
-                                             : RF_NoModuleLevelChanges,
-                          TypeMapper, Materializer);
+      Mapper.remapDbgRecordRange(I.getModule(), I.getDbgRecordRange());
     }
   }
 
@@ -1006,12 +1002,11 @@ void llvm::CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc,
 void llvm::remapInstructionsInBlocks(ArrayRef<BasicBlock *> Blocks,
                                      ValueToValueMapTy &VMap) {
   // Rewrite the code to refer to itself.
+  ValueMapper Mapper(VMap, RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);
   for (BasicBlock *BB : Blocks) {
     for (Instruction &Inst : *BB) {
-      RemapDbgRecordRange(Inst.getModule(), Inst.getDbgRecordRange(), VMap,
-                          RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);
-      RemapInstruction(&Inst, VMap,
-                       RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);
+      Mapper.remapDbgRecordRange(Inst.getModule(), Inst.getDbgRecordRange());
+      Mapper.remapInstruction(Inst);
     }
   }
 }



More information about the llvm-commits mailing list