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 @@ -56,6 +56,9 @@ } }; DenseMap ProfileDataMap; + /// If runtime relocation is enabled, this maps functions to the load + /// instruction that produces the profile relocation bias. + DenseMap FunctionToProfileBiasMap; 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 @@ -703,10 +703,9 @@ Type *Int64Ty = Type::getInt64Ty(M->getContext()); Function *Fn = I->getParent()->getParent(); - Instruction &EntryI = Fn->getEntryBlock().front(); - LoadInst *LI = dyn_cast(&EntryI); - if (!LI) { - IRBuilder<> EntryBuilder(&EntryI); + LoadInst *&BiasLI = FunctionToProfileBiasMap[Fn]; + if (!BiasLI) { + IRBuilder<> EntryBuilder(&Fn->getEntryBlock().front()); auto *Bias = M->getGlobalVariable(getInstrProfCounterBiasVarName()); if (!Bias) { // Compiler must define this variable when runtime counter relocation @@ -723,9 +722,9 @@ if (TT.supportsCOMDAT()) Bias->setComdat(M->getOrInsertComdat(Bias->getName())); } - LI = EntryBuilder.CreateLoad(Int64Ty, Bias); + BiasLI = EntryBuilder.CreateLoad(Int64Ty, Bias); } - auto *Add = Builder.CreateAdd(Builder.CreatePtrToInt(Addr, Int64Ty), LI); + auto *Add = Builder.CreateAdd(Builder.CreatePtrToInt(Addr, Int64Ty), BiasLI); return Builder.CreateIntToPtr(Add, Addr->getType()); }