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>
68     RuntimeCounterRelocation("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                                 MaybeAlign(),
209                                 AtomicOrdering::SequentiallyConsistent);
210       else {
211         LoadInst *OldVal = Builder.CreateLoad(Ty, Addr, "pgocount.promoted");
212         auto *NewVal = Builder.CreateAdd(OldVal, LiveInValue);
213         auto *NewStore = Builder.CreateStore(NewVal, Addr);
214 
215         // Now update the parent loop's candidate list:
216         if (IterativeCounterPromotion) {
217           auto *TargetLoop = LI.getLoopFor(ExitBlock);
218           if (TargetLoop)
219             LoopToCandidates[TargetLoop].emplace_back(OldVal, NewStore);
220         }
221       }
222     }
223   }
224 
225 private:
226   Instruction *Store;
227   ArrayRef<BasicBlock *> ExitBlocks;
228   ArrayRef<Instruction *> InsertPts;
229   DenseMap<Loop *, SmallVector<LoadStorePair, 8>> &LoopToCandidates;
230   LoopInfo &LI;
231 };
232 
233 /// A helper class to do register promotion for all profile counter
234 /// updates in a loop.
235 ///
236 class PGOCounterPromoter {
237 public:
238   PGOCounterPromoter(
239       DenseMap<Loop *, SmallVector<LoadStorePair, 8>> &LoopToCands,
240       Loop &CurLoop, LoopInfo &LI, BlockFrequencyInfo *BFI)
241       : LoopToCandidates(LoopToCands), ExitBlocks(), InsertPts(), L(CurLoop),
242         LI(LI), BFI(BFI) {
243 
244     // Skip collection of ExitBlocks and InsertPts for loops that will not be
245     // able to have counters promoted.
246     SmallVector<BasicBlock *, 8> LoopExitBlocks;
247     SmallPtrSet<BasicBlock *, 8> BlockSet;
248 
249     L.getExitBlocks(LoopExitBlocks);
250     if (!isPromotionPossible(&L, LoopExitBlocks))
251       return;
252 
253     for (BasicBlock *ExitBlock : LoopExitBlocks) {
254       if (BlockSet.insert(ExitBlock).second) {
255         ExitBlocks.push_back(ExitBlock);
256         InsertPts.push_back(&*ExitBlock->getFirstInsertionPt());
257       }
258     }
259   }
260 
261   bool run(int64_t *NumPromoted) {
262     // Skip 'infinite' loops:
263     if (ExitBlocks.size() == 0)
264       return false;
265 
266     // Skip if any of the ExitBlocks contains a ret instruction.
267     // This is to prevent dumping of incomplete profile -- if the
268     // the loop is a long running loop and dump is called in the middle
269     // of the loop, the result profile is incomplete.
270     // FIXME: add other heuristics to detect long running loops.
271     if (SkipRetExitBlock) {
272       for (auto BB : ExitBlocks)
273         if (isa<ReturnInst>(BB->getTerminator()))
274           return false;
275     }
276 
277     unsigned MaxProm = getMaxNumOfPromotionsInLoop(&L);
278     if (MaxProm == 0)
279       return false;
280 
281     unsigned Promoted = 0;
282     for (auto &Cand : LoopToCandidates[&L]) {
283 
284       SmallVector<PHINode *, 4> NewPHIs;
285       SSAUpdater SSA(&NewPHIs);
286       Value *InitVal = ConstantInt::get(Cand.first->getType(), 0);
287 
288       // If BFI is set, we will use it to guide the promotions.
289       if (BFI) {
290         auto *BB = Cand.first->getParent();
291         auto InstrCount = BFI->getBlockProfileCount(BB);
292         if (!InstrCount)
293           continue;
294         auto PreheaderCount = BFI->getBlockProfileCount(L.getLoopPreheader());
295         // If the average loop trip count is not greater than 1.5, we skip
296         // promotion.
297         if (PreheaderCount &&
298             (PreheaderCount.getValue() * 3) >= (InstrCount.getValue() * 2))
299           continue;
300       }
301 
302       PGOCounterPromoterHelper Promoter(Cand.first, Cand.second, SSA, InitVal,
303                                         L.getLoopPreheader(), ExitBlocks,
304                                         InsertPts, LoopToCandidates, LI);
305       Promoter.run(SmallVector<Instruction *, 2>({Cand.first, Cand.second}));
306       Promoted++;
307       if (Promoted >= MaxProm)
308         break;
309 
310       (*NumPromoted)++;
311       if (MaxNumOfPromotions != -1 && *NumPromoted >= MaxNumOfPromotions)
312         break;
313     }
314 
315     LLVM_DEBUG(dbgs() << Promoted << " counters promoted for loop (depth="
316                       << L.getLoopDepth() << ")\n");
317     return Promoted != 0;
318   }
319 
320 private:
321   bool allowSpeculativeCounterPromotion(Loop *LP) {
322     SmallVector<BasicBlock *, 8> ExitingBlocks;
323     L.getExitingBlocks(ExitingBlocks);
324     // Not considierered speculative.
325     if (ExitingBlocks.size() == 1)
326       return true;
327     if (ExitingBlocks.size() > SpeculativeCounterPromotionMaxExiting)
328       return false;
329     return true;
330   }
331 
332   // Check whether the loop satisfies the basic conditions needed to perform
333   // Counter Promotions.
334   bool
335   isPromotionPossible(Loop *LP,
336                       const SmallVectorImpl<BasicBlock *> &LoopExitBlocks) {
337     // We can't insert into a catchswitch.
338     if (llvm::any_of(LoopExitBlocks, [](BasicBlock *Exit) {
339           return isa<CatchSwitchInst>(Exit->getTerminator());
340         }))
341       return false;
342 
343     if (!LP->hasDedicatedExits())
344       return false;
345 
346     BasicBlock *PH = LP->getLoopPreheader();
347     if (!PH)
348       return false;
349 
350     return true;
351   }
352 
353   // Returns the max number of Counter Promotions for LP.
354   unsigned getMaxNumOfPromotionsInLoop(Loop *LP) {
355     SmallVector<BasicBlock *, 8> LoopExitBlocks;
356     LP->getExitBlocks(LoopExitBlocks);
357     if (!isPromotionPossible(LP, LoopExitBlocks))
358       return 0;
359 
360     SmallVector<BasicBlock *, 8> ExitingBlocks;
361     LP->getExitingBlocks(ExitingBlocks);
362 
363     // If BFI is set, we do more aggressive promotions based on BFI.
364     if (BFI)
365       return (unsigned)-1;
366 
367     // Not considierered speculative.
368     if (ExitingBlocks.size() == 1)
369       return MaxNumOfPromotionsPerLoop;
370 
371     if (ExitingBlocks.size() > SpeculativeCounterPromotionMaxExiting)
372       return 0;
373 
374     // Whether the target block is in a loop does not matter:
375     if (SpeculativeCounterPromotionToLoop)
376       return MaxNumOfPromotionsPerLoop;
377 
378     // Now check the target block:
379     unsigned MaxProm = MaxNumOfPromotionsPerLoop;
380     for (auto *TargetBlock : LoopExitBlocks) {
381       auto *TargetLoop = LI.getLoopFor(TargetBlock);
382       if (!TargetLoop)
383         continue;
384       unsigned MaxPromForTarget = getMaxNumOfPromotionsInLoop(TargetLoop);
385       unsigned PendingCandsInTarget = LoopToCandidates[TargetLoop].size();
386       MaxProm =
387           std::min(MaxProm, std::max(MaxPromForTarget, PendingCandsInTarget) -
388                                 PendingCandsInTarget);
389     }
390     return MaxProm;
391   }
392 
393   DenseMap<Loop *, SmallVector<LoadStorePair, 8>> &LoopToCandidates;
394   SmallVector<BasicBlock *, 8> ExitBlocks;
395   SmallVector<Instruction *, 8> InsertPts;
396   Loop &L;
397   LoopInfo &LI;
398   BlockFrequencyInfo *BFI;
399 };
400 
401 enum class ValueProfilingCallType {
402   // Individual values are tracked. Currently used for indiret call target
403   // profiling.
404   Default,
405 
406   // MemOp: the memop size value profiling.
407   MemOp
408 };
409 
410 } // end anonymous namespace
411 
412 PreservedAnalyses InstrProfiling::run(Module &M, ModuleAnalysisManager &AM) {
413   FunctionAnalysisManager &FAM =
414       AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
415   auto GetTLI = [&FAM](Function &F) -> TargetLibraryInfo & {
416     return FAM.getResult<TargetLibraryAnalysis>(F);
417   };
418   if (!run(M, GetTLI))
419     return PreservedAnalyses::all();
420 
421   return PreservedAnalyses::none();
422 }
423 
424 char InstrProfilingLegacyPass::ID = 0;
425 INITIALIZE_PASS_BEGIN(InstrProfilingLegacyPass, "instrprof",
426                       "Frontend instrumentation-based coverage lowering.",
427                       false, false)
428 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
429 INITIALIZE_PASS_END(InstrProfilingLegacyPass, "instrprof",
430                     "Frontend instrumentation-based coverage lowering.", false,
431                     false)
432 
433 ModulePass *
434 llvm::createInstrProfilingLegacyPass(const InstrProfOptions &Options,
435                                      bool IsCS) {
436   return new InstrProfilingLegacyPass(Options, IsCS);
437 }
438 
439 static InstrProfIncrementInst *castToIncrementInst(Instruction *Instr) {
440   InstrProfIncrementInst *Inc = dyn_cast<InstrProfIncrementInstStep>(Instr);
441   if (Inc)
442     return Inc;
443   return dyn_cast<InstrProfIncrementInst>(Instr);
444 }
445 
446 bool InstrProfiling::lowerIntrinsics(Function *F) {
447   bool MadeChange = false;
448   PromotionCandidates.clear();
449   for (BasicBlock &BB : *F) {
450     for (Instruction &Instr : llvm::make_early_inc_range(BB)) {
451       InstrProfIncrementInst *Inc = castToIncrementInst(&Instr);
452       if (Inc) {
453         lowerIncrement(Inc);
454         MadeChange = true;
455       } else if (auto *Ind = dyn_cast<InstrProfValueProfileInst>(&Instr)) {
456         lowerValueProfileInst(Ind);
457         MadeChange = true;
458       }
459     }
460   }
461 
462   if (!MadeChange)
463     return false;
464 
465   promoteCounterLoadStores(F);
466   return true;
467 }
468 
469 bool InstrProfiling::isRuntimeCounterRelocationEnabled() const {
470   // Mach-O don't support weak external references.
471   if (TT.isOSBinFormatMachO())
472     return false;
473 
474   if (RuntimeCounterRelocation.getNumOccurrences() > 0)
475     return RuntimeCounterRelocation;
476 
477   // Fuchsia uses runtime counter relocation by default.
478   return TT.isOSFuchsia();
479 }
480 
481 bool InstrProfiling::isCounterPromotionEnabled() const {
482   if (DoCounterPromotion.getNumOccurrences() > 0)
483     return DoCounterPromotion;
484 
485   return Options.DoCounterPromotion;
486 }
487 
488 void InstrProfiling::promoteCounterLoadStores(Function *F) {
489   if (!isCounterPromotionEnabled())
490     return;
491 
492   DominatorTree DT(*F);
493   LoopInfo LI(DT);
494   DenseMap<Loop *, SmallVector<LoadStorePair, 8>> LoopPromotionCandidates;
495 
496   std::unique_ptr<BlockFrequencyInfo> BFI;
497   if (Options.UseBFIInPromotion) {
498     std::unique_ptr<BranchProbabilityInfo> BPI;
499     BPI.reset(new BranchProbabilityInfo(*F, LI, &GetTLI(*F)));
500     BFI.reset(new BlockFrequencyInfo(*F, *BPI, LI));
501   }
502 
503   for (const auto &LoadStore : PromotionCandidates) {
504     auto *CounterLoad = LoadStore.first;
505     auto *CounterStore = LoadStore.second;
506     BasicBlock *BB = CounterLoad->getParent();
507     Loop *ParentLoop = LI.getLoopFor(BB);
508     if (!ParentLoop)
509       continue;
510     LoopPromotionCandidates[ParentLoop].emplace_back(CounterLoad, CounterStore);
511   }
512 
513   SmallVector<Loop *, 4> Loops = LI.getLoopsInPreorder();
514 
515   // Do a post-order traversal of the loops so that counter updates can be
516   // iteratively hoisted outside the loop nest.
517   for (auto *Loop : llvm::reverse(Loops)) {
518     PGOCounterPromoter Promoter(LoopPromotionCandidates, *Loop, LI, BFI.get());
519     Promoter.run(&TotalCountersPromoted);
520   }
521 }
522 
523 static bool needsRuntimeHookUnconditionally(const Triple &TT) {
524   // On Fuchsia, we only need runtime hook if any counters are present.
525   if (TT.isOSFuchsia())
526     return false;
527 
528   return true;
529 }
530 
531 /// Check if the module contains uses of any profiling intrinsics.
532 static bool containsProfilingIntrinsics(Module &M) {
533   if (auto *F = M.getFunction(
534           Intrinsic::getName(llvm::Intrinsic::instrprof_increment)))
535     if (!F->use_empty())
536       return true;
537   if (auto *F = M.getFunction(
538           Intrinsic::getName(llvm::Intrinsic::instrprof_increment_step)))
539     if (!F->use_empty())
540       return true;
541   if (auto *F = M.getFunction(
542           Intrinsic::getName(llvm::Intrinsic::instrprof_value_profile)))
543     if (!F->use_empty())
544       return true;
545   return false;
546 }
547 
548 bool InstrProfiling::run(
549     Module &M, std::function<const TargetLibraryInfo &(Function &F)> GetTLI) {
550   this->M = &M;
551   this->GetTLI = std::move(GetTLI);
552   NamesVar = nullptr;
553   NamesSize = 0;
554   ProfileDataMap.clear();
555   CompilerUsedVars.clear();
556   UsedVars.clear();
557   TT = Triple(M.getTargetTriple());
558 
559   bool MadeChange = false;
560 
561   // Emit the runtime hook even if no counters are present.
562   if (needsRuntimeHookUnconditionally(TT))
563     MadeChange = emitRuntimeHook();
564 
565   // Improve compile time by avoiding linear scans when there is no work.
566   GlobalVariable *CoverageNamesVar =
567       M.getNamedGlobal(getCoverageUnusedNamesVarName());
568   if (!containsProfilingIntrinsics(M) && !CoverageNamesVar)
569     return MadeChange;
570 
571   // We did not know how many value sites there would be inside
572   // the instrumented function. This is counting the number of instrumented
573   // target value sites to enter it as field in the profile data variable.
574   for (Function &F : M) {
575     InstrProfIncrementInst *FirstProfIncInst = nullptr;
576     for (BasicBlock &BB : F)
577       for (auto I = BB.begin(), E = BB.end(); I != E; I++)
578         if (auto *Ind = dyn_cast<InstrProfValueProfileInst>(I))
579           computeNumValueSiteCounts(Ind);
580         else if (FirstProfIncInst == nullptr)
581           FirstProfIncInst = dyn_cast<InstrProfIncrementInst>(I);
582 
583     // Value profiling intrinsic lowering requires per-function profile data
584     // variable to be created first.
585     if (FirstProfIncInst != nullptr)
586       static_cast<void>(getOrCreateRegionCounters(FirstProfIncInst));
587   }
588 
589   for (Function &F : M)
590     MadeChange |= lowerIntrinsics(&F);
591 
592   if (CoverageNamesVar) {
593     lowerCoverageData(CoverageNamesVar);
594     MadeChange = true;
595   }
596 
597   if (!MadeChange)
598     return false;
599 
600   emitVNodes();
601   emitNameData();
602   emitRuntimeHook();
603   emitRegistration();
604   emitUses();
605   emitInitialization();
606   return true;
607 }
608 
609 static FunctionCallee getOrInsertValueProfilingCall(
610     Module &M, const TargetLibraryInfo &TLI,
611     ValueProfilingCallType CallType = ValueProfilingCallType::Default) {
612   LLVMContext &Ctx = M.getContext();
613   auto *ReturnTy = Type::getVoidTy(M.getContext());
614 
615   AttributeList AL;
616   if (auto AK = TLI.getExtAttrForI32Param(false))
617     AL = AL.addParamAttribute(M.getContext(), 2, AK);
618 
619   assert((CallType == ValueProfilingCallType::Default ||
620           CallType == ValueProfilingCallType::MemOp) &&
621          "Must be Default or MemOp");
622   Type *ParamTypes[] = {
623 #define VALUE_PROF_FUNC_PARAM(ParamType, ParamName, ParamLLVMType) ParamLLVMType
624 #include "llvm/ProfileData/InstrProfData.inc"
625   };
626   auto *ValueProfilingCallTy =
627       FunctionType::get(ReturnTy, makeArrayRef(ParamTypes), false);
628   StringRef FuncName = CallType == ValueProfilingCallType::Default
629                            ? getInstrProfValueProfFuncName()
630                            : getInstrProfValueProfMemOpFuncName();
631   return M.getOrInsertFunction(FuncName, ValueProfilingCallTy, AL);
632 }
633 
634 void InstrProfiling::computeNumValueSiteCounts(InstrProfValueProfileInst *Ind) {
635   GlobalVariable *Name = Ind->getName();
636   uint64_t ValueKind = Ind->getValueKind()->getZExtValue();
637   uint64_t Index = Ind->getIndex()->getZExtValue();
638   auto &PD = ProfileDataMap[Name];
639   PD.NumValueSites[ValueKind] =
640       std::max(PD.NumValueSites[ValueKind], (uint32_t)(Index + 1));
641 }
642 
643 void InstrProfiling::lowerValueProfileInst(InstrProfValueProfileInst *Ind) {
644   GlobalVariable *Name = Ind->getName();
645   auto It = ProfileDataMap.find(Name);
646   assert(It != ProfileDataMap.end() && It->second.DataVar &&
647          "value profiling detected in function with no counter incerement");
648 
649   GlobalVariable *DataVar = It->second.DataVar;
650   uint64_t ValueKind = Ind->getValueKind()->getZExtValue();
651   uint64_t Index = Ind->getIndex()->getZExtValue();
652   for (uint32_t Kind = IPVK_First; Kind < ValueKind; ++Kind)
653     Index += It->second.NumValueSites[Kind];
654 
655   IRBuilder<> Builder(Ind);
656   bool IsMemOpSize = (Ind->getValueKind()->getZExtValue() ==
657                       llvm::InstrProfValueKind::IPVK_MemOPSize);
658   CallInst *Call = nullptr;
659   auto *TLI = &GetTLI(*Ind->getFunction());
660 
661   // To support value profiling calls within Windows exception handlers, funclet
662   // information contained within operand bundles needs to be copied over to
663   // the library call. This is required for the IR to be processed by the
664   // WinEHPrepare pass.
665   SmallVector<OperandBundleDef, 1> OpBundles;
666   Ind->getOperandBundlesAsDefs(OpBundles);
667   if (!IsMemOpSize) {
668     Value *Args[3] = {Ind->getTargetValue(),
669                       Builder.CreateBitCast(DataVar, Builder.getInt8PtrTy()),
670                       Builder.getInt32(Index)};
671     Call = Builder.CreateCall(getOrInsertValueProfilingCall(*M, *TLI), Args,
672                               OpBundles);
673   } else {
674     Value *Args[3] = {Ind->getTargetValue(),
675                       Builder.CreateBitCast(DataVar, Builder.getInt8PtrTy()),
676                       Builder.getInt32(Index)};
677     Call = Builder.CreateCall(
678         getOrInsertValueProfilingCall(*M, *TLI, ValueProfilingCallType::MemOp),
679         Args, OpBundles);
680   }
681   if (auto AK = TLI->getExtAttrForI32Param(false))
682     Call->addParamAttr(2, AK);
683   Ind->replaceAllUsesWith(Call);
684   Ind->eraseFromParent();
685 }
686 
687 void InstrProfiling::lowerIncrement(InstrProfIncrementInst *Inc) {
688   GlobalVariable *Counters = getOrCreateRegionCounters(Inc);
689 
690   IRBuilder<> Builder(Inc);
691   uint64_t Index = Inc->getIndex()->getZExtValue();
692   Value *Addr = Builder.CreateConstInBoundsGEP2_32(Counters->getValueType(),
693                                                    Counters, 0, Index);
694 
695   if (isRuntimeCounterRelocationEnabled()) {
696     Type *Int64Ty = Type::getInt64Ty(M->getContext());
697     Type *Int64PtrTy = Type::getInt64PtrTy(M->getContext());
698     Function *Fn = Inc->getParent()->getParent();
699     Instruction &I = Fn->getEntryBlock().front();
700     LoadInst *LI = dyn_cast<LoadInst>(&I);
701     if (!LI) {
702       IRBuilder<> Builder(&I);
703       GlobalVariable *Bias =
704           M->getGlobalVariable(getInstrProfCounterBiasVarName());
705       if (!Bias) {
706         // Compiler must define this variable when runtime counter relocation
707         // is being used. Runtime has a weak external reference that is used
708         // to check whether that's the case or not.
709         Bias = new GlobalVariable(
710             *M, Int64Ty, false, GlobalValue::LinkOnceODRLinkage,
711             Constant::getNullValue(Int64Ty), getInstrProfCounterBiasVarName());
712         Bias->setVisibility(GlobalVariable::HiddenVisibility);
713         // A definition that's weak (linkonce_odr) without being in a COMDAT
714         // section wouldn't lead to link errors, but it would lead to a dead
715         // data word from every TU but one. Putting it in COMDAT ensures there
716         // will be exactly one data slot in the link.
717         if (TT.supportsCOMDAT())
718           Bias->setComdat(M->getOrInsertComdat(Bias->getName()));
719       }
720       LI = Builder.CreateLoad(Int64Ty, Bias);
721     }
722     auto *Add = Builder.CreateAdd(Builder.CreatePtrToInt(Addr, Int64Ty), LI);
723     Addr = Builder.CreateIntToPtr(Add, Int64PtrTy);
724   }
725 
726   if (Options.Atomic || AtomicCounterUpdateAll ||
727       (Index == 0 && AtomicFirstCounter)) {
728     Builder.CreateAtomicRMW(AtomicRMWInst::Add, Addr, Inc->getStep(),
729                             MaybeAlign(), AtomicOrdering::Monotonic);
730   } else {
731     Value *IncStep = Inc->getStep();
732     Value *Load = Builder.CreateLoad(IncStep->getType(), Addr, "pgocount");
733     auto *Count = Builder.CreateAdd(Load, Inc->getStep());
734     auto *Store = Builder.CreateStore(Count, Addr);
735     if (isCounterPromotionEnabled())
736       PromotionCandidates.emplace_back(cast<Instruction>(Load), Store);
737   }
738   Inc->eraseFromParent();
739 }
740 
741 void InstrProfiling::lowerCoverageData(GlobalVariable *CoverageNamesVar) {
742   ConstantArray *Names =
743       cast<ConstantArray>(CoverageNamesVar->getInitializer());
744   for (unsigned I = 0, E = Names->getNumOperands(); I < E; ++I) {
745     Constant *NC = Names->getOperand(I);
746     Value *V = NC->stripPointerCasts();
747     assert(isa<GlobalVariable>(V) && "Missing reference to function name");
748     GlobalVariable *Name = cast<GlobalVariable>(V);
749 
750     Name->setLinkage(GlobalValue::PrivateLinkage);
751     ReferencedNames.push_back(Name);
752     NC->dropAllReferences();
753   }
754   CoverageNamesVar->eraseFromParent();
755 }
756 
757 /// Get the name of a profiling variable for a particular function.
758 static std::string getVarName(InstrProfIncrementInst *Inc, StringRef Prefix,
759                               bool &Renamed) {
760   StringRef NamePrefix = getInstrProfNameVarPrefix();
761   StringRef Name = Inc->getName()->getName().substr(NamePrefix.size());
762   Function *F = Inc->getParent()->getParent();
763   Module *M = F->getParent();
764   if (!DoHashBasedCounterSplit || !isIRPGOFlagSet(M) ||
765       !canRenameComdatFunc(*F)) {
766     Renamed = false;
767     return (Prefix + Name).str();
768   }
769   Renamed = true;
770   uint64_t FuncHash = Inc->getHash()->getZExtValue();
771   SmallVector<char, 24> HashPostfix;
772   if (Name.endswith((Twine(".") + Twine(FuncHash)).toStringRef(HashPostfix)))
773     return (Prefix + Name).str();
774   return (Prefix + Name + "." + Twine(FuncHash)).str();
775 }
776 
777 static uint64_t getIntModuleFlagOrZero(const Module &M, StringRef Flag) {
778   auto *MD = dyn_cast_or_null<ConstantAsMetadata>(M.getModuleFlag(Flag));
779   if (!MD)
780     return 0;
781 
782   // If the flag is a ConstantAsMetadata, it should be an integer representable
783   // in 64-bits.
784   return cast<ConstantInt>(MD->getValue())->getZExtValue();
785 }
786 
787 static bool enablesValueProfiling(const Module &M) {
788   return isIRPGOFlagSet(&M) ||
789          getIntModuleFlagOrZero(M, "EnableValueProfiling") != 0;
790 }
791 
792 // Conservatively returns true if data variables may be referenced by code.
793 static bool profDataReferencedByCode(const Module &M) {
794   return enablesValueProfiling(M);
795 }
796 
797 static inline bool shouldRecordFunctionAddr(Function *F) {
798   // Only record function addresses if IR PGO is enabled or if clang value
799   // profiling is enabled. Recording function addresses greatly increases object
800   // file size, because it prevents the inliner from deleting functions that
801   // have been inlined everywhere.
802   if (!profDataReferencedByCode(*F->getParent()))
803     return false;
804 
805   // Check the linkage
806   bool HasAvailableExternallyLinkage = F->hasAvailableExternallyLinkage();
807   if (!F->hasLinkOnceLinkage() && !F->hasLocalLinkage() &&
808       !HasAvailableExternallyLinkage)
809     return true;
810 
811   // A function marked 'alwaysinline' with available_externally linkage can't
812   // have its address taken. Doing so would create an undefined external ref to
813   // the function, which would fail to link.
814   if (HasAvailableExternallyLinkage &&
815       F->hasFnAttribute(Attribute::AlwaysInline))
816     return false;
817 
818   // Prohibit function address recording if the function is both internal and
819   // COMDAT. This avoids the profile data variable referencing internal symbols
820   // in COMDAT.
821   if (F->hasLocalLinkage() && F->hasComdat())
822     return false;
823 
824   // Check uses of this function for other than direct calls or invokes to it.
825   // Inline virtual functions have linkeOnceODR linkage. When a key method
826   // exists, the vtable will only be emitted in the TU where the key method
827   // is defined. In a TU where vtable is not available, the function won't
828   // be 'addresstaken'. If its address is not recorded here, the profile data
829   // with missing address may be picked by the linker leading  to missing
830   // indirect call target info.
831   return F->hasAddressTaken() || F->hasLinkOnceLinkage();
832 }
833 
834 static bool needsRuntimeRegistrationOfSectionRange(const Triple &TT) {
835   // Don't do this for Darwin.  compiler-rt uses linker magic.
836   if (TT.isOSDarwin())
837     return false;
838   // Use linker script magic to get data/cnts/name start/end.
839   if (TT.isOSLinux() || TT.isOSFreeBSD() || TT.isOSNetBSD() ||
840       TT.isOSSolaris() || TT.isOSFuchsia() || TT.isPS4CPU() || TT.isOSWindows())
841     return false;
842 
843   return true;
844 }
845 
846 GlobalVariable *
847 InstrProfiling::getOrCreateRegionCounters(InstrProfIncrementInst *Inc) {
848   GlobalVariable *NamePtr = Inc->getName();
849   auto &PD = ProfileDataMap[NamePtr];
850   if (PD.RegionCounters)
851     return PD.RegionCounters;
852 
853   // Match the linkage and visibility of the name global.
854   Function *Fn = Inc->getParent()->getParent();
855   GlobalValue::LinkageTypes Linkage = NamePtr->getLinkage();
856   GlobalValue::VisibilityTypes Visibility = NamePtr->getVisibility();
857 
858   // Due to the limitation of binder as of 2021/09/28, the duplicate weak
859   // symbols in the same csect won't be discarded. When there are duplicate weak
860   // symbols, we can NOT guarantee that the relocations get resolved to the
861   // intended weak symbol, so we can not ensure the correctness of the relative
862   // CounterPtr, so we have to use private linkage for counter and data symbols.
863   if (TT.isOSBinFormatXCOFF()) {
864     Linkage = GlobalValue::PrivateLinkage;
865     Visibility = GlobalValue::DefaultVisibility;
866   }
867   // Move the name variable to the right section. Place them in a COMDAT group
868   // if the associated function is a COMDAT. This will make sure that only one
869   // copy of counters of the COMDAT function will be emitted after linking. Keep
870   // in mind that this pass may run before the inliner, so we need to create a
871   // new comdat group for the counters and profiling data. If we use the comdat
872   // of the parent function, that will result in relocations against discarded
873   // sections.
874   //
875   // If the data variable is referenced by code,  counters and data have to be
876   // in different comdats for COFF because the Visual C++ linker will report
877   // duplicate symbol errors if there are multiple external symbols with the
878   // same name marked IMAGE_COMDAT_SELECT_ASSOCIATIVE.
879   //
880   // For ELF, when not using COMDAT, put counters, data and values into a
881   // nodeduplicate COMDAT which is lowered to a zero-flag section group. This
882   // allows -z start-stop-gc to discard the entire group when the function is
883   // discarded.
884   bool DataReferencedByCode = profDataReferencedByCode(*M);
885   bool NeedComdat = needsComdatForCounter(*Fn, *M);
886   bool Renamed;
887   std::string CntsVarName =
888       getVarName(Inc, getInstrProfCountersVarPrefix(), Renamed);
889   std::string DataVarName =
890       getVarName(Inc, getInstrProfDataVarPrefix(), Renamed);
891   auto MaybeSetComdat = [&](GlobalVariable *GV) {
892     bool UseComdat = (NeedComdat || TT.isOSBinFormatELF());
893     if (UseComdat) {
894       StringRef GroupName = TT.isOSBinFormatCOFF() && DataReferencedByCode
895                                 ? GV->getName()
896                                 : CntsVarName;
897       Comdat *C = M->getOrInsertComdat(GroupName);
898       if (!NeedComdat)
899         C->setSelectionKind(Comdat::NoDeduplicate);
900       GV->setComdat(C);
901     }
902   };
903 
904   uint64_t NumCounters = Inc->getNumCounters()->getZExtValue();
905   LLVMContext &Ctx = M->getContext();
906   ArrayType *CounterTy = ArrayType::get(Type::getInt64Ty(Ctx), NumCounters);
907 
908   // Create the counters variable.
909   auto *CounterPtr =
910       new GlobalVariable(*M, CounterTy, false, Linkage,
911                          Constant::getNullValue(CounterTy), CntsVarName);
912   CounterPtr->setVisibility(Visibility);
913   CounterPtr->setSection(
914       getInstrProfSectionName(IPSK_cnts, TT.getObjectFormat()));
915   CounterPtr->setAlignment(Align(8));
916   MaybeSetComdat(CounterPtr);
917   CounterPtr->setLinkage(Linkage);
918   PD.RegionCounters = CounterPtr;
919 
920   auto *Int8PtrTy = Type::getInt8PtrTy(Ctx);
921   // Allocate statically the array of pointers to value profile nodes for
922   // the current function.
923   Constant *ValuesPtrExpr = ConstantPointerNull::get(Int8PtrTy);
924   uint64_t NS = 0;
925   for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
926     NS += PD.NumValueSites[Kind];
927   if (NS > 0 && ValueProfileStaticAlloc &&
928       !needsRuntimeRegistrationOfSectionRange(TT)) {
929     ArrayType *ValuesTy = ArrayType::get(Type::getInt64Ty(Ctx), NS);
930     auto *ValuesVar = new GlobalVariable(
931         *M, ValuesTy, false, Linkage, Constant::getNullValue(ValuesTy),
932         getVarName(Inc, getInstrProfValuesVarPrefix(), Renamed));
933     ValuesVar->setVisibility(Visibility);
934     ValuesVar->setSection(
935         getInstrProfSectionName(IPSK_vals, TT.getObjectFormat()));
936     ValuesVar->setAlignment(Align(8));
937     MaybeSetComdat(ValuesVar);
938     ValuesPtrExpr =
939         ConstantExpr::getBitCast(ValuesVar, Type::getInt8PtrTy(Ctx));
940   }
941 
942   // Create data variable.
943   auto *IntPtrTy = M->getDataLayout().getIntPtrType(M->getContext());
944   auto *Int16Ty = Type::getInt16Ty(Ctx);
945   auto *Int16ArrayTy = ArrayType::get(Int16Ty, IPVK_Last + 1);
946   Type *DataTypes[] = {
947 #define INSTR_PROF_DATA(Type, LLVMType, Name, Init) LLVMType,
948 #include "llvm/ProfileData/InstrProfData.inc"
949   };
950   auto *DataTy = StructType::get(Ctx, makeArrayRef(DataTypes));
951 
952   Constant *FunctionAddr = shouldRecordFunctionAddr(Fn)
953                                ? ConstantExpr::getBitCast(Fn, Int8PtrTy)
954                                : ConstantPointerNull::get(Int8PtrTy);
955 
956   Constant *Int16ArrayVals[IPVK_Last + 1];
957   for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
958     Int16ArrayVals[Kind] = ConstantInt::get(Int16Ty, PD.NumValueSites[Kind]);
959 
960   // If the data variable is not referenced by code (if we don't emit
961   // @llvm.instrprof.value.profile, NS will be 0), and the counter keeps the
962   // data variable live under linker GC, the data variable can be private. This
963   // optimization applies to ELF.
964   //
965   // On COFF, a comdat leader cannot be local so we require DataReferencedByCode
966   // to be false.
967   //
968   // If profd is in a deduplicate comdat, NS==0 with a hash suffix guarantees
969   // that other copies must have the same CFG and cannot have value profiling.
970   // If no hash suffix, other profd copies may be referenced by code.
971   if (NS == 0 && !(DataReferencedByCode && NeedComdat && !Renamed) &&
972       (TT.isOSBinFormatELF() ||
973        (!DataReferencedByCode && TT.isOSBinFormatCOFF()))) {
974     Linkage = GlobalValue::PrivateLinkage;
975     Visibility = GlobalValue::DefaultVisibility;
976   }
977   auto *Data =
978       new GlobalVariable(*M, DataTy, false, Linkage, nullptr, DataVarName);
979   // Reference the counter variable with a label difference (link-time
980   // constant).
981   auto *RelativeCounterPtr =
982       ConstantExpr::getSub(ConstantExpr::getPtrToInt(CounterPtr, IntPtrTy),
983                            ConstantExpr::getPtrToInt(Data, IntPtrTy));
984 
985   Constant *DataVals[] = {
986 #define INSTR_PROF_DATA(Type, LLVMType, Name, Init) Init,
987 #include "llvm/ProfileData/InstrProfData.inc"
988   };
989   Data->setInitializer(ConstantStruct::get(DataTy, DataVals));
990 
991   Data->setVisibility(Visibility);
992   Data->setSection(getInstrProfSectionName(IPSK_data, TT.getObjectFormat()));
993   Data->setAlignment(Align(INSTR_PROF_DATA_ALIGNMENT));
994   MaybeSetComdat(Data);
995   Data->setLinkage(Linkage);
996 
997   PD.DataVar = Data;
998 
999   // Mark the data variable as used so that it isn't stripped out.
1000   CompilerUsedVars.push_back(Data);
1001   // Now that the linkage set by the FE has been passed to the data and counter
1002   // variables, reset Name variable's linkage and visibility to private so that
1003   // it can be removed later by the compiler.
1004   NamePtr->setLinkage(GlobalValue::PrivateLinkage);
1005   // Collect the referenced names to be used by emitNameData.
1006   ReferencedNames.push_back(NamePtr);
1007 
1008   return PD.RegionCounters;
1009 }
1010 
1011 void InstrProfiling::emitVNodes() {
1012   if (!ValueProfileStaticAlloc)
1013     return;
1014 
1015   // For now only support this on platforms that do
1016   // not require runtime registration to discover
1017   // named section start/end.
1018   if (needsRuntimeRegistrationOfSectionRange(TT))
1019     return;
1020 
1021   size_t TotalNS = 0;
1022   for (auto &PD : ProfileDataMap) {
1023     for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
1024       TotalNS += PD.second.NumValueSites[Kind];
1025   }
1026 
1027   if (!TotalNS)
1028     return;
1029 
1030   uint64_t NumCounters = TotalNS * NumCountersPerValueSite;
1031 // Heuristic for small programs with very few total value sites.
1032 // The default value of vp-counters-per-site is chosen based on
1033 // the observation that large apps usually have a low percentage
1034 // of value sites that actually have any profile data, and thus
1035 // the average number of counters per site is low. For small
1036 // apps with very few sites, this may not be true. Bump up the
1037 // number of counters in this case.
1038 #define INSTR_PROF_MIN_VAL_COUNTS 10
1039   if (NumCounters < INSTR_PROF_MIN_VAL_COUNTS)
1040     NumCounters = std::max(INSTR_PROF_MIN_VAL_COUNTS, (int)NumCounters * 2);
1041 
1042   auto &Ctx = M->getContext();
1043   Type *VNodeTypes[] = {
1044 #define INSTR_PROF_VALUE_NODE(Type, LLVMType, Name, Init) LLVMType,
1045 #include "llvm/ProfileData/InstrProfData.inc"
1046   };
1047   auto *VNodeTy = StructType::get(Ctx, makeArrayRef(VNodeTypes));
1048 
1049   ArrayType *VNodesTy = ArrayType::get(VNodeTy, NumCounters);
1050   auto *VNodesVar = new GlobalVariable(
1051       *M, VNodesTy, false, GlobalValue::PrivateLinkage,
1052       Constant::getNullValue(VNodesTy), getInstrProfVNodesVarName());
1053   VNodesVar->setSection(
1054       getInstrProfSectionName(IPSK_vnodes, TT.getObjectFormat()));
1055   // VNodesVar is used by runtime but not referenced via relocation by other
1056   // sections. Conservatively make it linker retained.
1057   UsedVars.push_back(VNodesVar);
1058 }
1059 
1060 void InstrProfiling::emitNameData() {
1061   std::string UncompressedData;
1062 
1063   if (ReferencedNames.empty())
1064     return;
1065 
1066   std::string CompressedNameStr;
1067   if (Error E = collectPGOFuncNameStrings(ReferencedNames, CompressedNameStr,
1068                                           DoInstrProfNameCompression)) {
1069     report_fatal_error(Twine(toString(std::move(E))), false);
1070   }
1071 
1072   auto &Ctx = M->getContext();
1073   auto *NamesVal =
1074       ConstantDataArray::getString(Ctx, StringRef(CompressedNameStr), false);
1075   NamesVar = new GlobalVariable(*M, NamesVal->getType(), true,
1076                                 GlobalValue::PrivateLinkage, NamesVal,
1077                                 getInstrProfNamesVarName());
1078   NamesSize = CompressedNameStr.size();
1079   NamesVar->setSection(
1080       getInstrProfSectionName(IPSK_name, TT.getObjectFormat()));
1081   // On COFF, it's important to reduce the alignment down to 1 to prevent the
1082   // linker from inserting padding before the start of the names section or
1083   // between names entries.
1084   NamesVar->setAlignment(Align(1));
1085   // NamesVar is used by runtime but not referenced via relocation by other
1086   // sections. Conservatively make it linker retained.
1087   UsedVars.push_back(NamesVar);
1088 
1089   for (auto *NamePtr : ReferencedNames)
1090     NamePtr->eraseFromParent();
1091 }
1092 
1093 void InstrProfiling::emitRegistration() {
1094   if (!needsRuntimeRegistrationOfSectionRange(TT))
1095     return;
1096 
1097   // Construct the function.
1098   auto *VoidTy = Type::getVoidTy(M->getContext());
1099   auto *VoidPtrTy = Type::getInt8PtrTy(M->getContext());
1100   auto *Int64Ty = Type::getInt64Ty(M->getContext());
1101   auto *RegisterFTy = FunctionType::get(VoidTy, false);
1102   auto *RegisterF = Function::Create(RegisterFTy, GlobalValue::InternalLinkage,
1103                                      getInstrProfRegFuncsName(), M);
1104   RegisterF->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
1105   if (Options.NoRedZone)
1106     RegisterF->addFnAttr(Attribute::NoRedZone);
1107 
1108   auto *RuntimeRegisterTy = FunctionType::get(VoidTy, VoidPtrTy, false);
1109   auto *RuntimeRegisterF =
1110       Function::Create(RuntimeRegisterTy, GlobalVariable::ExternalLinkage,
1111                        getInstrProfRegFuncName(), M);
1112 
1113   IRBuilder<> IRB(BasicBlock::Create(M->getContext(), "", RegisterF));
1114   for (Value *Data : CompilerUsedVars)
1115     if (!isa<Function>(Data))
1116       IRB.CreateCall(RuntimeRegisterF, IRB.CreateBitCast(Data, VoidPtrTy));
1117   for (Value *Data : UsedVars)
1118     if (Data != NamesVar && !isa<Function>(Data))
1119       IRB.CreateCall(RuntimeRegisterF, IRB.CreateBitCast(Data, VoidPtrTy));
1120 
1121   if (NamesVar) {
1122     Type *ParamTypes[] = {VoidPtrTy, Int64Ty};
1123     auto *NamesRegisterTy =
1124         FunctionType::get(VoidTy, makeArrayRef(ParamTypes), false);
1125     auto *NamesRegisterF =
1126         Function::Create(NamesRegisterTy, GlobalVariable::ExternalLinkage,
1127                          getInstrProfNamesRegFuncName(), M);
1128     IRB.CreateCall(NamesRegisterF, {IRB.CreateBitCast(NamesVar, VoidPtrTy),
1129                                     IRB.getInt64(NamesSize)});
1130   }
1131 
1132   IRB.CreateRetVoid();
1133 }
1134 
1135 bool InstrProfiling::emitRuntimeHook() {
1136   // We expect the linker to be invoked with -u<hook_var> flag for Linux
1137   // in which case there is no need to emit the external variable.
1138   if (TT.isOSLinux())
1139     return false;
1140 
1141   // If the module's provided its own runtime, we don't need to do anything.
1142   if (M->getGlobalVariable(getInstrProfRuntimeHookVarName()))
1143     return false;
1144 
1145   // Declare an external variable that will pull in the runtime initialization.
1146   auto *Int32Ty = Type::getInt32Ty(M->getContext());
1147   auto *Var =
1148       new GlobalVariable(*M, Int32Ty, false, GlobalValue::ExternalLinkage,
1149                          nullptr, getInstrProfRuntimeHookVarName());
1150 
1151   if (TT.isOSBinFormatELF()) {
1152     // Mark the user variable as used so that it isn't stripped out.
1153     CompilerUsedVars.push_back(Var);
1154   } else {
1155     // Make a function that uses it.
1156     auto *User = Function::Create(FunctionType::get(Int32Ty, false),
1157                                   GlobalValue::LinkOnceODRLinkage,
1158                                   getInstrProfRuntimeHookVarUseFuncName(), M);
1159     User->addFnAttr(Attribute::NoInline);
1160     if (Options.NoRedZone)
1161       User->addFnAttr(Attribute::NoRedZone);
1162     User->setVisibility(GlobalValue::HiddenVisibility);
1163     if (TT.supportsCOMDAT())
1164       User->setComdat(M->getOrInsertComdat(User->getName()));
1165 
1166     IRBuilder<> IRB(BasicBlock::Create(M->getContext(), "", User));
1167     auto *Load = IRB.CreateLoad(Int32Ty, Var);
1168     IRB.CreateRet(Load);
1169 
1170     // Mark the function as used so that it isn't stripped out.
1171     CompilerUsedVars.push_back(User);
1172   }
1173   return true;
1174 }
1175 
1176 void InstrProfiling::emitUses() {
1177   // The metadata sections are parallel arrays. Optimizers (e.g.
1178   // GlobalOpt/ConstantMerge) may not discard associated sections as a unit, so
1179   // we conservatively retain all unconditionally in the compiler.
1180   //
1181   // On ELF and Mach-O, the linker can guarantee the associated sections will be
1182   // retained or discarded as a unit, so llvm.compiler.used is sufficient.
1183   // Similarly on COFF, if prof data is not referenced by code we use one comdat
1184   // and ensure this GC property as well. Otherwise, we have to conservatively
1185   // make all of the sections retained by the linker.
1186   if (TT.isOSBinFormatELF() || TT.isOSBinFormatMachO() ||
1187       (TT.isOSBinFormatCOFF() && !profDataReferencedByCode(*M)))
1188     appendToCompilerUsed(*M, CompilerUsedVars);
1189   else
1190     appendToUsed(*M, CompilerUsedVars);
1191 
1192   // We do not add proper references from used metadata sections to NamesVar and
1193   // VNodesVar, so we have to be conservative and place them in llvm.used
1194   // regardless of the target,
1195   appendToUsed(*M, UsedVars);
1196 }
1197 
1198 void InstrProfiling::emitInitialization() {
1199   // Create ProfileFileName variable. Don't don't this for the
1200   // context-sensitive instrumentation lowering: This lowering is after
1201   // LTO/ThinLTO linking. Pass PGOInstrumentationGenCreateVar should
1202   // have already create the variable before LTO/ThinLTO linking.
1203   if (!IsCS)
1204     createProfileFileNameVar(*M, Options.InstrProfileOutput);
1205   Function *RegisterF = M->getFunction(getInstrProfRegFuncsName());
1206   if (!RegisterF)
1207     return;
1208 
1209   // Create the initialization function.
1210   auto *VoidTy = Type::getVoidTy(M->getContext());
1211   auto *F = Function::Create(FunctionType::get(VoidTy, false),
1212                              GlobalValue::InternalLinkage,
1213                              getInstrProfInitFuncName(), M);
1214   F->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
1215   F->addFnAttr(Attribute::NoInline);
1216   if (Options.NoRedZone)
1217     F->addFnAttr(Attribute::NoRedZone);
1218 
1219   // Add the basic block and the necessary calls.
1220   IRBuilder<> IRB(BasicBlock::Create(M->getContext(), "", F));
1221   IRB.CreateCall(RegisterF, {});
1222   IRB.CreateRetVoid();
1223 
1224   appendToGlobalCtors(*M, F, 0);
1225 }
1226