diff --git a/llvm/include/llvm/Transforms/Instrumentation/InstrProfiling.h b/llvm/include/llvm/Transforms/Instrumentation/InstrProfiling.h --- a/llvm/include/llvm/Transforms/Instrumentation/InstrProfiling.h +++ b/llvm/include/llvm/Transforms/Instrumentation/InstrProfiling.h @@ -57,6 +57,9 @@ } }; DenseMap ProfileDataMap; + /// If runtime relocation is enabled, this maps functions to the load + /// instruction that produces the profile relocation bias. + DenseMap FunctionToProfileBiasLI; std::vector CompilerUsedVars; std::vector UsedVars; std::vector ReferencedNames; diff --git a/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp b/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp --- a/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp +++ b/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp @@ -700,10 +700,9 @@ Type *Int64Ty = Type::getInt64Ty(M->getContext()); Type *Int64PtrTy = Type::getInt64PtrTy(M->getContext()); Function *Fn = Inc->getParent()->getParent(); - Instruction &I = Fn->getEntryBlock().front(); - LoadInst *LI = dyn_cast(&I); - if (!LI) { - IRBuilder<> Builder(&I); + auto *&BiasLI = FunctionToProfileBiasLI[Fn]; + if (!BiasLI) { + IRBuilder<> Builder(&Fn->getEntryBlock().front()); GlobalVariable *Bias = M->getGlobalVariable(getInstrProfCounterBiasVarName()); if (!Bias) { @@ -721,9 +720,10 @@ if (TT.supportsCOMDAT()) Bias->setComdat(M->getOrInsertComdat(Bias->getName())); } - LI = Builder.CreateLoad(Int64Ty, Bias); + BiasLI = Builder.CreateLoad(Int64Ty, Bias); } - auto *Add = Builder.CreateAdd(Builder.CreatePtrToInt(Addr, Int64Ty), LI); + auto *Add = + Builder.CreateAdd(Builder.CreatePtrToInt(Addr, Int64Ty), BiasLI); Addr = Builder.CreateIntToPtr(Add, Int64PtrTy); }