1 //===-- InstrProfiling.cpp - Frontend instrumentation based profiling -----===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This pass lowers instrprof_* intrinsics emitted by a frontend for profiling.
10 // It also builds the data structures and initialization code needed for
11 // updating execution counts and emitting the profile at runtime.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/Transforms/Instrumentation/InstrProfiling.h"
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/Triple.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/Analysis/BlockFrequencyInfo.h"
22 #include "llvm/Analysis/BranchProbabilityInfo.h"
23 #include "llvm/Analysis/LoopInfo.h"
24 #include "llvm/Analysis/TargetLibraryInfo.h"
25 #include "llvm/IR/Attributes.h"
26 #include "llvm/IR/BasicBlock.h"
27 #include "llvm/IR/Constant.h"
28 #include "llvm/IR/Constants.h"
29 #include "llvm/IR/DerivedTypes.h"
30 #include "llvm/IR/Dominators.h"
31 #include "llvm/IR/Function.h"
32 #include "llvm/IR/GlobalValue.h"
33 #include "llvm/IR/GlobalVariable.h"
34 #include "llvm/IR/IRBuilder.h"
35 #include "llvm/IR/Instruction.h"
36 #include "llvm/IR/Instructions.h"
37 #include "llvm/IR/IntrinsicInst.h"
38 #include "llvm/IR/Module.h"
39 #include "llvm/IR/Type.h"
40 #include "llvm/InitializePasses.h"
41 #include "llvm/Pass.h"
42 #include "llvm/ProfileData/InstrProf.h"
43 #include "llvm/Support/Casting.h"
44 #include "llvm/Support/CommandLine.h"
45 #include "llvm/Support/Error.h"
46 #include "llvm/Support/ErrorHandling.h"
47 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
48 #include "llvm/Transforms/Utils/ModuleUtils.h"
49 #include "llvm/Transforms/Utils/SSAUpdater.h"
50 #include <algorithm>
51 #include <cassert>
52 #include <cstddef>
53 #include <cstdint>
54 #include <string>
55 
56 using namespace llvm;
57 
58 #define DEBUG_TYPE "instrprof"
59 
60 namespace {
61 
62 cl::opt<bool> DoHashBasedCounterSplit(
63     "hash-based-counter-split",
64     cl::desc("Rename counter variable of a comdat function based on cfg hash"),
65     cl::init(true));
66 
67 cl::opt<bool> RuntimeCounterRelocation(
68     "runtime-counter-relocation",
69     cl::desc("Enable relocating counters at runtime."),
70     cl::init(false));
71 
72 cl::opt<bool> ValueProfileStaticAlloc(
73     "vp-static-alloc",
74     cl::desc("Do static counter allocation for value profiler"),
75     cl::init(true));
76 
77 cl::opt<double> NumCountersPerValueSite(
78     "vp-counters-per-site",
79     cl::desc("The average number of profile counters allocated "
80              "per value profiling site."),
81     // This is set to a very small value because in real programs, only
82     // a very small percentage of value sites have non-zero targets, e.g, 1/30.
83     // For those sites with non-zero profile, the average number of targets
84     // is usually smaller than 2.
85     cl::init(1.0));
86 
87 cl::opt<bool> AtomicCounterUpdateAll(
88     "instrprof-atomic-counter-update-all", cl::ZeroOrMore,
89     cl::desc("Make all profile counter updates atomic (for testing only)"),
90     cl::init(false));
91 
92 cl::opt<bool> AtomicCounterUpdatePromoted(
93     "atomic-counter-update-promoted", cl::ZeroOrMore,
94     cl::desc("Do counter update using atomic fetch add "
95              " for promoted counters only"),
96     cl::init(false));
97 
98 cl::opt<bool> AtomicFirstCounter(
99     "atomic-first-counter", cl::ZeroOrMore,
100     cl::desc("Use atomic fetch add for first counter in a function (usually "
101              "the entry counter)"),
102     cl::init(false));
103 
104 // If the option is not specified, the default behavior about whether
105 // counter promotion is done depends on how instrumentaiton lowering
106 // pipeline is setup, i.e., the default value of true of this option
107 // does not mean the promotion will be done by default. Explicitly
108 // setting this option can override the default behavior.
109 cl::opt<bool> DoCounterPromotion("do-counter-promotion", cl::ZeroOrMore,
110                                  cl::desc("Do counter register promotion"),
111                                  cl::init(false));
112 cl::opt<unsigned> MaxNumOfPromotionsPerLoop(
113     cl::ZeroOrMore, "max-counter-promotions-per-loop", cl::init(20),
114     cl::desc("Max number counter promotions per loop to avoid"
115              " increasing register pressure too much"));
116 
117 // A debug option
118 cl::opt<int>
119     MaxNumOfPromotions(cl::ZeroOrMore, "max-counter-promotions", cl::init(-1),
120                        cl::desc("Max number of allowed counter promotions"));
121 
122 cl::opt<unsigned> SpeculativeCounterPromotionMaxExiting(
123     cl::ZeroOrMore, "speculative-counter-promotion-max-exiting", cl::init(3),
124     cl::desc("The max number of exiting blocks of a loop to allow "
125              " speculative counter promotion"));
126 
127 cl::opt<bool> SpeculativeCounterPromotionToLoop(
128     cl::ZeroOrMore, "speculative-counter-promotion-to-loop", cl::init(false),
129     cl::desc("When the option is false, if the target block is in a loop, "
130              "the promotion will be disallowed unless the promoted counter "
131              " update can be further/iteratively promoted into an acyclic "
132              " region."));
133 
134 cl::opt<bool> IterativeCounterPromotion(
135     cl::ZeroOrMore, "iterative-counter-promotion", cl::init(true),
136     cl::desc("Allow counter promotion across the whole loop nest."));
137 
138 cl::opt<bool> SkipRetExitBlock(
139     cl::ZeroOrMore, "skip-ret-exit-block", cl::init(true),
140     cl::desc("Suppress counter promotion if exit blocks contain ret."));
141 
142 class InstrProfilingLegacyPass : public ModulePass {
143   InstrProfiling InstrProf;
144 
145 public:
146   static char ID;
147 
148   InstrProfilingLegacyPass() : ModulePass(ID) {}
149   InstrProfilingLegacyPass(const InstrProfOptions &Options, bool IsCS = false)
150       : ModulePass(ID), InstrProf(Options, IsCS) {
151     initializeInstrProfilingLegacyPassPass(*PassRegistry::getPassRegistry());
152   }
153 
154   StringRef getPassName() const override {
155     return "Frontend instrumentation-based coverage lowering";
156   }
157 
158   bool runOnModule(Module &M) override {
159     auto GetTLI = [this](Function &F) -> TargetLibraryInfo & {
160       return this->getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
161     };
162     return InstrProf.run(M, GetTLI);
163   }
164 
165   void getAnalysisUsage(AnalysisUsage &AU) const override {
166     AU.setPreservesCFG();
167     AU.addRequired<TargetLibraryInfoWrapperPass>();
168   }
169 };
170 
171 ///
172 /// A helper class to promote one counter RMW operation in the loop
173 /// into register update.
174 ///
175 /// RWM update for the counter will be sinked out of the loop after
176 /// the transformation.
177 ///
178 class PGOCounterPromoterHelper : public LoadAndStorePromoter {
179 public:
180   PGOCounterPromoterHelper(
181       Instruction *L, Instruction *S, SSAUpdater &SSA, Value *Init,
182       BasicBlock *PH, ArrayRef<BasicBlock *> ExitBlocks,
183       ArrayRef<Instruction *> InsertPts,
184       DenseMap<Loop *, SmallVector<LoadStorePair, 8>> &LoopToCands,
185       LoopInfo &LI)
186       : LoadAndStorePromoter({L, S}, SSA), Store(S), ExitBlocks(ExitBlocks),
187         InsertPts(InsertPts), LoopToCandidates(LoopToCands), LI(LI) {
188     assert(isa<LoadInst>(L));
189     assert(isa<StoreInst>(S));
190     SSA.AddAvailableValue(PH, Init);
191   }
192 
193   void doExtraRewritesBeforeFinalDeletion() override {
194     for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) {
195       BasicBlock *ExitBlock = ExitBlocks[i];
196       Instruction *InsertPos = InsertPts[i];
197       // Get LiveIn value into the ExitBlock. If there are multiple
198       // predecessors, the value is defined by a PHI node in this
199       // block.
200       Value *LiveInValue = SSA.GetValueInMiddleOfBlock(ExitBlock);
201       Value *Addr = cast<StoreInst>(Store)->getPointerOperand();
202       Type *Ty = LiveInValue->getType();
203       IRBuilder<> Builder(InsertPos);
204       if (AtomicCounterUpdatePromoted)
205         // automic update currently can only be promoted across the current
206         // loop, not the whole loop nest.
207         Builder.CreateAtomicRMW(AtomicRMWInst::Add, Addr, LiveInValue,
208                                 AtomicOrdering::SequentiallyConsistent);
209       else {
210         LoadInst *OldVal = Builder.CreateLoad(Ty, Addr, "pgocount.promoted");
211         auto *NewVal = Builder.CreateAdd(OldVal, LiveInValue);
212         auto *NewStore = Builder.CreateStore(NewVal, Addr);
213 
214         // Now update the parent loop's candidate list:
215         if (IterativeCounterPromotion) {
216           auto *TargetLoop = LI.getLoopFor(ExitBlock);
217           if (TargetLoop)
218             LoopToCandidates[TargetLoop].emplace_back(OldVal, NewStore);
219         }
220       }
221     }
222   }
223 
224 private:
225   Instruction *Store;
226   ArrayRef<BasicBlock *> ExitBlocks;
227   ArrayRef<Instruction *> InsertPts;
228   DenseMap<Loop *, SmallVector<LoadStorePair, 8>> &LoopToCandidates;
229   LoopInfo &LI;
230 };
231 
232 /// A helper class to do register promotion for all profile counter
233 /// updates in a loop.
234 ///
235 class PGOCounterPromoter {
236 public:
237   PGOCounterPromoter(
238       DenseMap<Loop *, SmallVector<LoadStorePair, 8>> &LoopToCands,
239       Loop &CurLoop, LoopInfo &LI, BlockFrequencyInfo *BFI)
240       : LoopToCandidates(LoopToCands), ExitBlocks(), InsertPts(), L(CurLoop),
241         LI(LI), BFI(BFI) {
242 
243     // Skip collection of ExitBlocks and InsertPts for loops that will not be
244     // able to have counters promoted.
245     SmallVector<BasicBlock *, 8> LoopExitBlocks;
246     SmallPtrSet<BasicBlock *, 8> BlockSet;
247 
248     L.getExitBlocks(LoopExitBlocks);
249     if (!isPromotionPossible(&L, LoopExitBlocks))
250       return;
251 
252     for (BasicBlock *ExitBlock : LoopExitBlocks) {
253       if (BlockSet.insert(ExitBlock).second) {
254         ExitBlocks.push_back(ExitBlock);
255         InsertPts.push_back(&*ExitBlock->getFirstInsertionPt());
256       }
257     }
258   }
259 
260   bool run(int64_t *NumPromoted) {
261     // Skip 'infinite' loops:
262     if (ExitBlocks.size() == 0)
263       return false;
264 
265     // Skip if any of the ExitBlocks contains a ret instruction.
266     // This is to prevent dumping of incomplete profile -- if the
267     // the loop is a long running loop and dump is called in the middle
268     // of the loop, the result profile is incomplete.
269     // FIXME: add other heuristics to detect long running loops.
270     if (SkipRetExitBlock) {
271       for (auto BB : ExitBlocks)
272         if (isa<ReturnInst>(BB->getTerminator()))
273           return false;
274     }
275 
276     unsigned MaxProm = getMaxNumOfPromotionsInLoop(&L);
277     if (MaxProm == 0)
278       return false;
279 
280     unsigned Promoted = 0;
281     for (auto &Cand : LoopToCandidates[&L]) {
282 
283       SmallVector<PHINode *, 4> NewPHIs;
284       SSAUpdater SSA(&NewPHIs);
285       Value *InitVal = ConstantInt::get(Cand.first->getType(), 0);
286 
287       // If BFI is set, we will use it to guide the promotions.
288       if (BFI) {
289         auto *BB = Cand.first->getParent();
290         auto InstrCount = BFI->getBlockProfileCount(BB);
291         if (!InstrCount)
292           continue;
293         auto PreheaderCount = BFI->getBlockProfileCount(L.getLoopPreheader());
294         // If the average loop trip count is not greater than 1.5, we skip
295         // promotion.
296         if (PreheaderCount &&
297             (PreheaderCount.getValue() * 3) >= (InstrCount.getValue() * 2))
298           continue;
299       }
300 
301       PGOCounterPromoterHelper Promoter(Cand.first, Cand.second, SSA, InitVal,
302                                         L.getLoopPreheader(), ExitBlocks,
303                                         InsertPts, LoopToCandidates, LI);
304       Promoter.run(SmallVector<Instruction *, 2>({Cand.first, Cand.second}));
305       Promoted++;
306       if (Promoted >= MaxProm)
307         break;
308 
309       (*NumPromoted)++;
310       if (MaxNumOfPromotions != -1 && *NumPromoted >= MaxNumOfPromotions)
311         break;
312     }
313 
314     LLVM_DEBUG(dbgs() << Promoted << " counters promoted for loop (depth="
315                       << L.getLoopDepth() << ")\n");
316     return Promoted != 0;
317   }
318 
319 private:
320   bool allowSpeculativeCounterPromotion(Loop *LP) {
321     SmallVector<BasicBlock *, 8> ExitingBlocks;
322     L.getExitingBlocks(ExitingBlocks);
323     // Not considierered speculative.
324     if (ExitingBlocks.size() == 1)
325       return true;
326     if (ExitingBlocks.size() > SpeculativeCounterPromotionMaxExiting)
327       return false;
328     return true;
329   }
330 
331   // Check whether the loop satisfies the basic conditions needed to perform
332   // Counter Promotions.
333   bool isPromotionPossible(Loop *LP,
334                            const SmallVectorImpl<BasicBlock *> &LoopExitBlocks) {
335     // We can't insert into a catchswitch.
336     if (llvm::any_of(LoopExitBlocks, [](BasicBlock *Exit) {
337           return isa<CatchSwitchInst>(Exit->getTerminator());
338         }))
339       return false;
340 
341     if (!LP->hasDedicatedExits())
342       return false;
343 
344     BasicBlock *PH = LP->getLoopPreheader();
345     if (!PH)
346       return false;
347 
348     return true;
349   }
350 
351   // Returns the max number of Counter Promotions for LP.
352   unsigned getMaxNumOfPromotionsInLoop(Loop *LP) {
353     SmallVector<BasicBlock *, 8> LoopExitBlocks;
354     LP->getExitBlocks(LoopExitBlocks);
355     if (!isPromotionPossible(LP, LoopExitBlocks))
356       return 0;
357 
358     SmallVector<BasicBlock *, 8> ExitingBlocks;
359     LP->getExitingBlocks(ExitingBlocks);
360 
361     // If BFI is set, we do more aggressive promotions based on BFI.
362     if (BFI)
363       return (unsigned)-1;
364 
365     // Not considierered speculative.
366     if (ExitingBlocks.size() == 1)
367       return MaxNumOfPromotionsPerLoop;
368 
369     if (ExitingBlocks.size() > SpeculativeCounterPromotionMaxExiting)
370       return 0;
371 
372     // Whether the target block is in a loop does not matter:
373     if (SpeculativeCounterPromotionToLoop)
374       return MaxNumOfPromotionsPerLoop;
375 
376     // Now check the target block:
377     unsigned MaxProm = MaxNumOfPromotionsPerLoop;
378     for (auto *TargetBlock : LoopExitBlocks) {
379       auto *TargetLoop = LI.getLoopFor(TargetBlock);
380       if (!TargetLoop)
381         continue;
382       unsigned MaxPromForTarget = getMaxNumOfPromotionsInLoop(TargetLoop);
383       unsigned PendingCandsInTarget = LoopToCandidates[TargetLoop].size();
384       MaxProm =
385           std::min(MaxProm, std::max(MaxPromForTarget, PendingCandsInTarget) -
386                                 PendingCandsInTarget);
387     }
388     return MaxProm;
389   }
390 
391   DenseMap<Loop *, SmallVector<LoadStorePair, 8>> &LoopToCandidates;
392   SmallVector<BasicBlock *, 8> ExitBlocks;
393   SmallVector<Instruction *, 8> InsertPts;
394   Loop &L;
395   LoopInfo &LI;
396   BlockFrequencyInfo *BFI;
397 };
398 
399 enum class ValueProfilingCallType {
400   // Individual values are tracked. Currently used for indiret call target
401   // profiling.
402   Default,
403 
404   // MemOp: the memop size value profiling.
405   MemOp
406 };
407 
408 } // end anonymous namespace
409 
410 PreservedAnalyses InstrProfiling::run(Module &M, ModuleAnalysisManager &AM) {
411   FunctionAnalysisManager &FAM =
412       AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
413   auto GetTLI = [&FAM](Function &F) -> TargetLibraryInfo & {
414     return FAM.getResult<TargetLibraryAnalysis>(F);
415   };
416   if (!run(M, GetTLI))
417     return PreservedAnalyses::all();
418 
419   return PreservedAnalyses::none();
420 }
421 
422 char InstrProfilingLegacyPass::ID = 0;
423 INITIALIZE_PASS_BEGIN(
424     InstrProfilingLegacyPass, "instrprof",
425     "Frontend instrumentation-based coverage lowering.", false, false)
426 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
427 INITIALIZE_PASS_END(
428     InstrProfilingLegacyPass, "instrprof",
429     "Frontend instrumentation-based coverage lowering.", false, false)
430 
431 ModulePass *
432 llvm::createInstrProfilingLegacyPass(const InstrProfOptions &Options,
433                                      bool IsCS) {
434   return new InstrProfilingLegacyPass(Options, IsCS);
435 }
436 
437 static InstrProfIncrementInst *castToIncrementInst(Instruction *Instr) {
438   InstrProfIncrementInst *Inc = dyn_cast<InstrProfIncrementInstStep>(Instr);
439   if (Inc)
440     return Inc;
441   return dyn_cast<InstrProfIncrementInst>(Instr);
442 }
443 
444 bool InstrProfiling::lowerIntrinsics(Function *F) {
445   bool MadeChange = false;
446   PromotionCandidates.clear();
447   for (BasicBlock &BB : *F) {
448     for (auto I = BB.begin(), E = BB.end(); I != E;) {
449       auto Instr = I++;
450       InstrProfIncrementInst *Inc = castToIncrementInst(&*Instr);
451       if (Inc) {
452         lowerIncrement(Inc);
453         MadeChange = true;
454       } else if (auto *Ind = dyn_cast<InstrProfValueProfileInst>(Instr)) {
455         lowerValueProfileInst(Ind);
456         MadeChange = true;
457       }
458     }
459   }
460 
461   if (!MadeChange)
462     return false;
463 
464   promoteCounterLoadStores(F);
465   return true;
466 }
467 
468 bool InstrProfiling::isRuntimeCounterRelocationEnabled() const {
469   if (RuntimeCounterRelocation.getNumOccurrences() > 0)
470     return RuntimeCounterRelocation;
471 
472   return TT.isOSFuchsia();
473 }
474 
475 bool InstrProfiling::isCounterPromotionEnabled() const {
476   if (DoCounterPromotion.getNumOccurrences() > 0)
477     return DoCounterPromotion;
478 
479   return Options.DoCounterPromotion;
480 }
481 
482 void InstrProfiling::promoteCounterLoadStores(Function *F) {
483   if (!isCounterPromotionEnabled())
484     return;
485 
486   DominatorTree DT(*F);
487   LoopInfo LI(DT);
488   DenseMap<Loop *, SmallVector<LoadStorePair, 8>> LoopPromotionCandidates;
489 
490   std::unique_ptr<BlockFrequencyInfo> BFI;
491   if (Options.UseBFIInPromotion) {
492     std::unique_ptr<BranchProbabilityInfo> BPI;
493     BPI.reset(new BranchProbabilityInfo(*F, LI, &GetTLI(*F)));
494     BFI.reset(new BlockFrequencyInfo(*F, *BPI, LI));
495   }
496 
497   for (const auto &LoadStore : PromotionCandidates) {
498     auto *CounterLoad = LoadStore.first;
499     auto *CounterStore = LoadStore.second;
500     BasicBlock *BB = CounterLoad->getParent();
501     Loop *ParentLoop = LI.getLoopFor(BB);
502     if (!ParentLoop)
503       continue;
504     LoopPromotionCandidates[ParentLoop].emplace_back(CounterLoad, CounterStore);
505   }
506 
507   SmallVector<Loop *, 4> Loops = LI.getLoopsInPreorder();
508 
509   // Do a post-order traversal of the loops so that counter updates can be
510   // iteratively hoisted outside the loop nest.
511   for (auto *Loop : llvm::reverse(Loops)) {
512     PGOCounterPromoter Promoter(LoopPromotionCandidates, *Loop, LI, BFI.get());
513     Promoter.run(&TotalCountersPromoted);
514   }
515 }
516 
517 /// Check if the module contains uses of any profiling intrinsics.
518 static bool containsProfilingIntrinsics(Module &M) {
519   if (auto *F = M.getFunction(
520           Intrinsic::getName(llvm::Intrinsic::instrprof_increment)))
521     if (!F->use_empty())
522       return true;
523   if (auto *F = M.getFunction(
524           Intrinsic::getName(llvm::Intrinsic::instrprof_increment_step)))
525     if (!F->use_empty())
526       return true;
527   if (auto *F = M.getFunction(
528           Intrinsic::getName(llvm::Intrinsic::instrprof_value_profile)))
529     if (!F->use_empty())
530       return true;
531   return false;
532 }
533 
534 bool InstrProfiling::run(
535     Module &M, std::function<const TargetLibraryInfo &(Function &F)> GetTLI) {
536   this->M = &M;
537   this->GetTLI = std::move(GetTLI);
538   NamesVar = nullptr;
539   NamesSize = 0;
540   ProfileDataMap.clear();
541   UsedVars.clear();
542   TT = Triple(M.getTargetTriple());
543 
544   // Emit the runtime hook even if no counters are present.
545   bool MadeChange = emitRuntimeHook();
546 
547   // Improve compile time by avoiding linear scans when there is no work.
548   GlobalVariable *CoverageNamesVar =
549       M.getNamedGlobal(getCoverageUnusedNamesVarName());
550   if (!containsProfilingIntrinsics(M) && !CoverageNamesVar)
551     return MadeChange;
552 
553   // We did not know how many value sites there would be inside
554   // the instrumented function. This is counting the number of instrumented
555   // target value sites to enter it as field in the profile data variable.
556   for (Function &F : M) {
557     InstrProfIncrementInst *FirstProfIncInst = nullptr;
558     for (BasicBlock &BB : F)
559       for (auto I = BB.begin(), E = BB.end(); I != E; I++)
560         if (auto *Ind = dyn_cast<InstrProfValueProfileInst>(I))
561           computeNumValueSiteCounts(Ind);
562         else if (FirstProfIncInst == nullptr)
563           FirstProfIncInst = dyn_cast<InstrProfIncrementInst>(I);
564 
565     // Value profiling intrinsic lowering requires per-function profile data
566     // variable to be created first.
567     if (FirstProfIncInst != nullptr)
568       static_cast<void>(getOrCreateRegionCounters(FirstProfIncInst));
569   }
570 
571   for (Function &F : M)
572     MadeChange |= lowerIntrinsics(&F);
573 
574   if (CoverageNamesVar) {
575     lowerCoverageData(CoverageNamesVar);
576     MadeChange = true;
577   }
578 
579   if (!MadeChange)
580     return false;
581 
582   emitVNodes();
583   emitNameData();
584   emitRegistration();
585   emitUses();
586   emitInitialization();
587   return true;
588 }
589 
590 static FunctionCallee getOrInsertValueProfilingCall(
591     Module &M, const TargetLibraryInfo &TLI,
592     ValueProfilingCallType CallType = ValueProfilingCallType::Default) {
593   LLVMContext &Ctx = M.getContext();
594   auto *ReturnTy = Type::getVoidTy(M.getContext());
595 
596   AttributeList AL;
597   if (auto AK = TLI.getExtAttrForI32Param(false))
598     AL = AL.addParamAttribute(M.getContext(), 2, AK);
599 
600   assert((CallType == ValueProfilingCallType::Default ||
601           CallType == ValueProfilingCallType::MemOp) &&
602          "Must be Default or MemOp");
603   Type *ParamTypes[] = {
604 #define VALUE_PROF_FUNC_PARAM(ParamType, ParamName, ParamLLVMType) ParamLLVMType
605 #include "llvm/ProfileData/InstrProfData.inc"
606   };
607   auto *ValueProfilingCallTy =
608       FunctionType::get(ReturnTy, makeArrayRef(ParamTypes), false);
609   StringRef FuncName = CallType == ValueProfilingCallType::Default
610                            ? getInstrProfValueProfFuncName()
611                            : getInstrProfValueProfMemOpFuncName();
612   return M.getOrInsertFunction(FuncName, ValueProfilingCallTy, AL);
613 }
614 
615 void InstrProfiling::computeNumValueSiteCounts(InstrProfValueProfileInst *Ind) {
616   GlobalVariable *Name = Ind->getName();
617   uint64_t ValueKind = Ind->getValueKind()->getZExtValue();
618   uint64_t Index = Ind->getIndex()->getZExtValue();
619   auto It = ProfileDataMap.find(Name);
620   if (It == ProfileDataMap.end()) {
621     PerFunctionProfileData PD;
622     PD.NumValueSites[ValueKind] = Index + 1;
623     ProfileDataMap[Name] = PD;
624   } else if (It->second.NumValueSites[ValueKind] <= Index)
625     It->second.NumValueSites[ValueKind] = Index + 1;
626 }
627 
628 void InstrProfiling::lowerValueProfileInst(InstrProfValueProfileInst *Ind) {
629   GlobalVariable *Name = Ind->getName();
630   auto It = ProfileDataMap.find(Name);
631   assert(It != ProfileDataMap.end() && It->second.DataVar &&
632          "value profiling detected in function with no counter incerement");
633 
634   GlobalVariable *DataVar = It->second.DataVar;
635   uint64_t ValueKind = Ind->getValueKind()->getZExtValue();
636   uint64_t Index = Ind->getIndex()->getZExtValue();
637   for (uint32_t Kind = IPVK_First; Kind < ValueKind; ++Kind)
638     Index += It->second.NumValueSites[Kind];
639 
640   IRBuilder<> Builder(Ind);
641   bool IsMemOpSize = (Ind->getValueKind()->getZExtValue() ==
642                       llvm::InstrProfValueKind::IPVK_MemOPSize);
643   CallInst *Call = nullptr;
644   auto *TLI = &GetTLI(*Ind->getFunction());
645 
646   // To support value profiling calls within Windows exception handlers, funclet
647   // information contained within operand bundles needs to be copied over to
648   // the library call. This is required for the IR to be processed by the
649   // WinEHPrepare pass.
650   SmallVector<OperandBundleDef, 1> OpBundles;
651   Ind->getOperandBundlesAsDefs(OpBundles);
652   if (!IsMemOpSize) {
653     Value *Args[3] = {Ind->getTargetValue(),
654                       Builder.CreateBitCast(DataVar, Builder.getInt8PtrTy()),
655                       Builder.getInt32(Index)};
656     Call = Builder.CreateCall(getOrInsertValueProfilingCall(*M, *TLI), Args,
657                               OpBundles);
658   } else {
659     Value *Args[3] = {Ind->getTargetValue(),
660                       Builder.CreateBitCast(DataVar, Builder.getInt8PtrTy()),
661                       Builder.getInt32(Index)};
662     Call = Builder.CreateCall(
663         getOrInsertValueProfilingCall(*M, *TLI, ValueProfilingCallType::MemOp),
664         Args, OpBundles);
665   }
666   if (auto AK = TLI->getExtAttrForI32Param(false))
667     Call->addParamAttr(2, AK);
668   Ind->replaceAllUsesWith(Call);
669   Ind->eraseFromParent();
670 }
671 
672 void InstrProfiling::lowerIncrement(InstrProfIncrementInst *Inc) {
673   GlobalVariable *Counters = getOrCreateRegionCounters(Inc);
674 
675   IRBuilder<> Builder(Inc);
676   uint64_t Index = Inc->getIndex()->getZExtValue();
677   Value *Addr = Builder.CreateConstInBoundsGEP2_64(Counters->getValueType(),
678                                                    Counters, 0, Index);
679 
680   if (isRuntimeCounterRelocationEnabled()) {
681     Type *Int64Ty = Type::getInt64Ty(M->getContext());
682     Type *Int64PtrTy = Type::getInt64PtrTy(M->getContext());
683     Function *Fn = Inc->getParent()->getParent();
684     Instruction &I = Fn->getEntryBlock().front();
685     LoadInst *LI = dyn_cast<LoadInst>(&I);
686     if (!LI) {
687       IRBuilder<> Builder(&I);
688       Type *Int64Ty = Type::getInt64Ty(M->getContext());
689       GlobalVariable *Bias = M->getGlobalVariable(getInstrProfCounterBiasVarName());
690       if (!Bias) {
691         Bias = new GlobalVariable(*M, Int64Ty, false, GlobalValue::LinkOnceODRLinkage,
692                                   Constant::getNullValue(Int64Ty),
693                                   getInstrProfCounterBiasVarName());
694         Bias->setVisibility(GlobalVariable::HiddenVisibility);
695       }
696       LI = Builder.CreateLoad(Int64Ty, Bias);
697     }
698     auto *Add = Builder.CreateAdd(Builder.CreatePtrToInt(Addr, Int64Ty), LI);
699     Addr = Builder.CreateIntToPtr(Add, Int64PtrTy);
700   }
701 
702   if (Options.Atomic || AtomicCounterUpdateAll ||
703       (Index == 0 && AtomicFirstCounter)) {
704     Builder.CreateAtomicRMW(AtomicRMWInst::Add, Addr, Inc->getStep(),
705                             AtomicOrdering::Monotonic);
706   } else {
707     Value *IncStep = Inc->getStep();
708     Value *Load = Builder.CreateLoad(IncStep->getType(), Addr, "pgocount");
709     auto *Count = Builder.CreateAdd(Load, Inc->getStep());
710     auto *Store = Builder.CreateStore(Count, Addr);
711     if (isCounterPromotionEnabled())
712       PromotionCandidates.emplace_back(cast<Instruction>(Load), Store);
713   }
714   Inc->eraseFromParent();
715 }
716 
717 void InstrProfiling::lowerCoverageData(GlobalVariable *CoverageNamesVar) {
718   ConstantArray *Names =
719       cast<ConstantArray>(CoverageNamesVar->getInitializer());
720   for (unsigned I = 0, E = Names->getNumOperands(); I < E; ++I) {
721     Constant *NC = Names->getOperand(I);
722     Value *V = NC->stripPointerCasts();
723     assert(isa<GlobalVariable>(V) && "Missing reference to function name");
724     GlobalVariable *Name = cast<GlobalVariable>(V);
725 
726     Name->setLinkage(GlobalValue::PrivateLinkage);
727     ReferencedNames.push_back(Name);
728     NC->dropAllReferences();
729   }
730   CoverageNamesVar->eraseFromParent();
731 }
732 
733 /// Get the name of a profiling variable for a particular function.
734 static std::string getVarName(InstrProfIncrementInst *Inc, StringRef Prefix) {
735   StringRef NamePrefix = getInstrProfNameVarPrefix();
736   StringRef Name = Inc->getName()->getName().substr(NamePrefix.size());
737   Function *F = Inc->getParent()->getParent();
738   Module *M = F->getParent();
739   if (!DoHashBasedCounterSplit || !isIRPGOFlagSet(M) ||
740       !canRenameComdatFunc(*F))
741     return (Prefix + Name).str();
742   uint64_t FuncHash = Inc->getHash()->getZExtValue();
743   SmallVector<char, 24> HashPostfix;
744   if (Name.endswith((Twine(".") + Twine(FuncHash)).toStringRef(HashPostfix)))
745     return (Prefix + Name).str();
746   return (Prefix + Name + "." + Twine(FuncHash)).str();
747 }
748 
749 static inline bool shouldRecordFunctionAddr(Function *F) {
750   // Check the linkage
751   bool HasAvailableExternallyLinkage = F->hasAvailableExternallyLinkage();
752   if (!F->hasLinkOnceLinkage() && !F->hasLocalLinkage() &&
753       !HasAvailableExternallyLinkage)
754     return true;
755 
756   // A function marked 'alwaysinline' with available_externally linkage can't
757   // have its address taken. Doing so would create an undefined external ref to
758   // the function, which would fail to link.
759   if (HasAvailableExternallyLinkage &&
760       F->hasFnAttribute(Attribute::AlwaysInline))
761     return false;
762 
763   // Prohibit function address recording if the function is both internal and
764   // COMDAT. This avoids the profile data variable referencing internal symbols
765   // in COMDAT.
766   if (F->hasLocalLinkage() && F->hasComdat())
767     return false;
768 
769   // Check uses of this function for other than direct calls or invokes to it.
770   // Inline virtual functions have linkeOnceODR linkage. When a key method
771   // exists, the vtable will only be emitted in the TU where the key method
772   // is defined. In a TU where vtable is not available, the function won't
773   // be 'addresstaken'. If its address is not recorded here, the profile data
774   // with missing address may be picked by the linker leading  to missing
775   // indirect call target info.
776   return F->hasAddressTaken() || F->hasLinkOnceLinkage();
777 }
778 
779 static bool needsRuntimeRegistrationOfSectionRange(const Triple &TT) {
780   // Don't do this for Darwin.  compiler-rt uses linker magic.
781   if (TT.isOSDarwin())
782     return false;
783   // Use linker script magic to get data/cnts/name start/end.
784   if (TT.isOSLinux() || TT.isOSFreeBSD() || TT.isOSNetBSD() ||
785       TT.isOSSolaris() || TT.isOSFuchsia() || TT.isPS4CPU() ||
786       TT.isOSWindows())
787     return false;
788 
789   return true;
790 }
791 
792 GlobalVariable *
793 InstrProfiling::getOrCreateRegionCounters(InstrProfIncrementInst *Inc) {
794   GlobalVariable *NamePtr = Inc->getName();
795   auto It = ProfileDataMap.find(NamePtr);
796   PerFunctionProfileData PD;
797   if (It != ProfileDataMap.end()) {
798     if (It->second.RegionCounters)
799       return It->second.RegionCounters;
800     PD = It->second;
801   }
802 
803   // Match the linkage and visibility of the name global. COFF supports using
804   // comdats with internal symbols, so do that if we can.
805   Function *Fn = Inc->getParent()->getParent();
806   GlobalValue::LinkageTypes Linkage = NamePtr->getLinkage();
807   GlobalValue::VisibilityTypes Visibility = NamePtr->getVisibility();
808   if (TT.isOSBinFormatCOFF()) {
809     Linkage = GlobalValue::InternalLinkage;
810     Visibility = GlobalValue::DefaultVisibility;
811   }
812 
813   // Move the name variable to the right section. Place them in a COMDAT group
814   // if the associated function is a COMDAT. This will make sure that only one
815   // copy of counters of the COMDAT function will be emitted after linking. Keep
816   // in mind that this pass may run before the inliner, so we need to create a
817   // new comdat group for the counters and profiling data. If we use the comdat
818   // of the parent function, that will result in relocations against discarded
819   // sections.
820   bool NeedComdat = needsComdatForCounter(*Fn, *M);
821   if (NeedComdat) {
822     if (TT.isOSBinFormatCOFF()) {
823       // For COFF, put the counters, data, and values each into their own
824       // comdats. We can't use a group because the Visual C++ linker will
825       // report duplicate symbol errors if there are multiple external symbols
826       // with the same name marked IMAGE_COMDAT_SELECT_ASSOCIATIVE.
827       Linkage = GlobalValue::LinkOnceODRLinkage;
828       Visibility = GlobalValue::HiddenVisibility;
829     }
830   }
831   std::string DataVarName = getVarName(Inc, getInstrProfDataVarPrefix());
832   auto MaybeSetComdat = [=](GlobalVariable *GV) {
833     if (NeedComdat)
834       GV->setComdat(M->getOrInsertComdat(TT.isOSBinFormatCOFF() ? GV->getName()
835                                                                 : DataVarName));
836   };
837 
838   uint64_t NumCounters = Inc->getNumCounters()->getZExtValue();
839   LLVMContext &Ctx = M->getContext();
840   ArrayType *CounterTy = ArrayType::get(Type::getInt64Ty(Ctx), NumCounters);
841 
842   // Create the counters variable.
843   auto *CounterPtr =
844       new GlobalVariable(*M, CounterTy, false, Linkage,
845                          Constant::getNullValue(CounterTy),
846                          getVarName(Inc, getInstrProfCountersVarPrefix()));
847   CounterPtr->setVisibility(Visibility);
848   CounterPtr->setSection(
849       getInstrProfSectionName(IPSK_cnts, TT.getObjectFormat()));
850   CounterPtr->setAlignment(Align(8));
851   MaybeSetComdat(CounterPtr);
852   CounterPtr->setLinkage(Linkage);
853 
854   auto *Int8PtrTy = Type::getInt8PtrTy(Ctx);
855   // Allocate statically the array of pointers to value profile nodes for
856   // the current function.
857   Constant *ValuesPtrExpr = ConstantPointerNull::get(Int8PtrTy);
858   if (ValueProfileStaticAlloc && !needsRuntimeRegistrationOfSectionRange(TT)) {
859     uint64_t NS = 0;
860     for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
861       NS += PD.NumValueSites[Kind];
862     if (NS) {
863       ArrayType *ValuesTy = ArrayType::get(Type::getInt64Ty(Ctx), NS);
864 
865       auto *ValuesVar =
866           new GlobalVariable(*M, ValuesTy, false, Linkage,
867                              Constant::getNullValue(ValuesTy),
868                              getVarName(Inc, getInstrProfValuesVarPrefix()));
869       ValuesVar->setVisibility(Visibility);
870       ValuesVar->setSection(
871           getInstrProfSectionName(IPSK_vals, TT.getObjectFormat()));
872       ValuesVar->setAlignment(Align(8));
873       MaybeSetComdat(ValuesVar);
874       ValuesPtrExpr =
875           ConstantExpr::getBitCast(ValuesVar, Type::getInt8PtrTy(Ctx));
876     }
877   }
878 
879   // Create data variable.
880   auto *Int16Ty = Type::getInt16Ty(Ctx);
881   auto *Int16ArrayTy = ArrayType::get(Int16Ty, IPVK_Last + 1);
882   Type *DataTypes[] = {
883 #define INSTR_PROF_DATA(Type, LLVMType, Name, Init) LLVMType,
884 #include "llvm/ProfileData/InstrProfData.inc"
885   };
886   auto *DataTy = StructType::get(Ctx, makeArrayRef(DataTypes));
887 
888   Constant *FunctionAddr = shouldRecordFunctionAddr(Fn)
889                                ? ConstantExpr::getBitCast(Fn, Int8PtrTy)
890                                : ConstantPointerNull::get(Int8PtrTy);
891 
892   Constant *Int16ArrayVals[IPVK_Last + 1];
893   for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
894     Int16ArrayVals[Kind] = ConstantInt::get(Int16Ty, PD.NumValueSites[Kind]);
895 
896   Constant *DataVals[] = {
897 #define INSTR_PROF_DATA(Type, LLVMType, Name, Init) Init,
898 #include "llvm/ProfileData/InstrProfData.inc"
899   };
900   auto *Data =
901       new GlobalVariable(*M, DataTy, false, Linkage,
902                          ConstantStruct::get(DataTy, DataVals), DataVarName);
903   Data->setVisibility(Visibility);
904   Data->setSection(getInstrProfSectionName(IPSK_data, TT.getObjectFormat()));
905   Data->setAlignment(Align(INSTR_PROF_DATA_ALIGNMENT));
906   MaybeSetComdat(Data);
907   Data->setLinkage(Linkage);
908 
909   PD.RegionCounters = CounterPtr;
910   PD.DataVar = Data;
911   ProfileDataMap[NamePtr] = PD;
912 
913   // Mark the data variable as used so that it isn't stripped out.
914   UsedVars.push_back(Data);
915   // Now that the linkage set by the FE has been passed to the data and counter
916   // variables, reset Name variable's linkage and visibility to private so that
917   // it can be removed later by the compiler.
918   NamePtr->setLinkage(GlobalValue::PrivateLinkage);
919   // Collect the referenced names to be used by emitNameData.
920   ReferencedNames.push_back(NamePtr);
921 
922   return CounterPtr;
923 }
924 
925 void InstrProfiling::emitVNodes() {
926   if (!ValueProfileStaticAlloc)
927     return;
928 
929   // For now only support this on platforms that do
930   // not require runtime registration to discover
931   // named section start/end.
932   if (needsRuntimeRegistrationOfSectionRange(TT))
933     return;
934 
935   size_t TotalNS = 0;
936   for (auto &PD : ProfileDataMap) {
937     for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
938       TotalNS += PD.second.NumValueSites[Kind];
939   }
940 
941   if (!TotalNS)
942     return;
943 
944   uint64_t NumCounters = TotalNS * NumCountersPerValueSite;
945 // Heuristic for small programs with very few total value sites.
946 // The default value of vp-counters-per-site is chosen based on
947 // the observation that large apps usually have a low percentage
948 // of value sites that actually have any profile data, and thus
949 // the average number of counters per site is low. For small
950 // apps with very few sites, this may not be true. Bump up the
951 // number of counters in this case.
952 #define INSTR_PROF_MIN_VAL_COUNTS 10
953   if (NumCounters < INSTR_PROF_MIN_VAL_COUNTS)
954     NumCounters = std::max(INSTR_PROF_MIN_VAL_COUNTS, (int)NumCounters * 2);
955 
956   auto &Ctx = M->getContext();
957   Type *VNodeTypes[] = {
958 #define INSTR_PROF_VALUE_NODE(Type, LLVMType, Name, Init) LLVMType,
959 #include "llvm/ProfileData/InstrProfData.inc"
960   };
961   auto *VNodeTy = StructType::get(Ctx, makeArrayRef(VNodeTypes));
962 
963   ArrayType *VNodesTy = ArrayType::get(VNodeTy, NumCounters);
964   auto *VNodesVar = new GlobalVariable(
965       *M, VNodesTy, false, GlobalValue::PrivateLinkage,
966       Constant::getNullValue(VNodesTy), getInstrProfVNodesVarName());
967   VNodesVar->setSection(
968       getInstrProfSectionName(IPSK_vnodes, TT.getObjectFormat()));
969   UsedVars.push_back(VNodesVar);
970 }
971 
972 void InstrProfiling::emitNameData() {
973   std::string UncompressedData;
974 
975   if (ReferencedNames.empty())
976     return;
977 
978   std::string CompressedNameStr;
979   if (Error E = collectPGOFuncNameStrings(ReferencedNames, CompressedNameStr,
980                                           DoInstrProfNameCompression)) {
981     report_fatal_error(toString(std::move(E)), false);
982   }
983 
984   auto &Ctx = M->getContext();
985   auto *NamesVal = ConstantDataArray::getString(
986       Ctx, StringRef(CompressedNameStr), false);
987   NamesVar = new GlobalVariable(*M, NamesVal->getType(), true,
988                                 GlobalValue::PrivateLinkage, NamesVal,
989                                 getInstrProfNamesVarName());
990   NamesSize = CompressedNameStr.size();
991   NamesVar->setSection(
992       getInstrProfSectionName(IPSK_name, TT.getObjectFormat()));
993   // On COFF, it's important to reduce the alignment down to 1 to prevent the
994   // linker from inserting padding before the start of the names section or
995   // between names entries.
996   NamesVar->setAlignment(Align(1));
997   UsedVars.push_back(NamesVar);
998 
999   for (auto *NamePtr : ReferencedNames)
1000     NamePtr->eraseFromParent();
1001 }
1002 
1003 void InstrProfiling::emitRegistration() {
1004   if (!needsRuntimeRegistrationOfSectionRange(TT))
1005     return;
1006 
1007   // Construct the function.
1008   auto *VoidTy = Type::getVoidTy(M->getContext());
1009   auto *VoidPtrTy = Type::getInt8PtrTy(M->getContext());
1010   auto *Int64Ty = Type::getInt64Ty(M->getContext());
1011   auto *RegisterFTy = FunctionType::get(VoidTy, false);
1012   auto *RegisterF = Function::Create(RegisterFTy, GlobalValue::InternalLinkage,
1013                                      getInstrProfRegFuncsName(), M);
1014   RegisterF->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
1015   if (Options.NoRedZone)
1016     RegisterF->addFnAttr(Attribute::NoRedZone);
1017 
1018   auto *RuntimeRegisterTy = FunctionType::get(VoidTy, VoidPtrTy, false);
1019   auto *RuntimeRegisterF =
1020       Function::Create(RuntimeRegisterTy, GlobalVariable::ExternalLinkage,
1021                        getInstrProfRegFuncName(), M);
1022 
1023   IRBuilder<> IRB(BasicBlock::Create(M->getContext(), "", RegisterF));
1024   for (Value *Data : UsedVars)
1025     if (Data != NamesVar && !isa<Function>(Data))
1026       IRB.CreateCall(RuntimeRegisterF, IRB.CreateBitCast(Data, VoidPtrTy));
1027 
1028   if (NamesVar) {
1029     Type *ParamTypes[] = {VoidPtrTy, Int64Ty};
1030     auto *NamesRegisterTy =
1031         FunctionType::get(VoidTy, makeArrayRef(ParamTypes), false);
1032     auto *NamesRegisterF =
1033         Function::Create(NamesRegisterTy, GlobalVariable::ExternalLinkage,
1034                          getInstrProfNamesRegFuncName(), M);
1035     IRB.CreateCall(NamesRegisterF, {IRB.CreateBitCast(NamesVar, VoidPtrTy),
1036                                     IRB.getInt64(NamesSize)});
1037   }
1038 
1039   IRB.CreateRetVoid();
1040 }
1041 
1042 bool InstrProfiling::emitRuntimeHook() {
1043   // We expect the linker to be invoked with -u<hook_var> flag for Linux or
1044   // Fuchsia, in which case there is no need to emit the user function.
1045   if (TT.isOSLinux() || TT.isOSFuchsia())
1046     return false;
1047 
1048   // If the module's provided its own runtime, we don't need to do anything.
1049   if (M->getGlobalVariable(getInstrProfRuntimeHookVarName()))
1050     return false;
1051 
1052   // Declare an external variable that will pull in the runtime initialization.
1053   auto *Int32Ty = Type::getInt32Ty(M->getContext());
1054   auto *Var =
1055       new GlobalVariable(*M, Int32Ty, false, GlobalValue::ExternalLinkage,
1056                          nullptr, getInstrProfRuntimeHookVarName());
1057 
1058   // Make a function that uses it.
1059   auto *User = Function::Create(FunctionType::get(Int32Ty, false),
1060                                 GlobalValue::LinkOnceODRLinkage,
1061                                 getInstrProfRuntimeHookVarUseFuncName(), M);
1062   User->addFnAttr(Attribute::NoInline);
1063   if (Options.NoRedZone)
1064     User->addFnAttr(Attribute::NoRedZone);
1065   User->setVisibility(GlobalValue::HiddenVisibility);
1066   if (TT.supportsCOMDAT())
1067     User->setComdat(M->getOrInsertComdat(User->getName()));
1068 
1069   IRBuilder<> IRB(BasicBlock::Create(M->getContext(), "", User));
1070   auto *Load = IRB.CreateLoad(Int32Ty, Var);
1071   IRB.CreateRet(Load);
1072 
1073   // Mark the user variable as used so that it isn't stripped out.
1074   UsedVars.push_back(User);
1075   return true;
1076 }
1077 
1078 void InstrProfiling::emitUses() {
1079   if (!UsedVars.empty())
1080     appendToUsed(*M, UsedVars);
1081 }
1082 
1083 void InstrProfiling::emitInitialization() {
1084   // Create ProfileFileName variable. Don't don't this for the
1085   // context-sensitive instrumentation lowering: This lowering is after
1086   // LTO/ThinLTO linking. Pass PGOInstrumentationGenCreateVar should
1087   // have already create the variable before LTO/ThinLTO linking.
1088   if (!IsCS)
1089     createProfileFileNameVar(*M, Options.InstrProfileOutput);
1090   Function *RegisterF = M->getFunction(getInstrProfRegFuncsName());
1091   if (!RegisterF)
1092     return;
1093 
1094   // Create the initialization function.
1095   auto *VoidTy = Type::getVoidTy(M->getContext());
1096   auto *F = Function::Create(FunctionType::get(VoidTy, false),
1097                              GlobalValue::InternalLinkage,
1098                              getInstrProfInitFuncName(), M);
1099   F->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
1100   F->addFnAttr(Attribute::NoInline);
1101   if (Options.NoRedZone)
1102     F->addFnAttr(Attribute::NoRedZone);
1103 
1104   // Add the basic block and the necessary calls.
1105   IRBuilder<> IRB(BasicBlock::Create(M->getContext(), "", F));
1106   IRB.CreateCall(RegisterF, {});
1107   IRB.CreateRetVoid();
1108 
1109   appendToGlobalCtors(*M, F, 0);
1110 }
1111