1 //===-- InstrProfiling.cpp - Frontend instrumentation based profiling -----===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This pass lowers instrprof_* intrinsics emitted by a frontend for profiling.
11 // It also builds the data structures and initialization code needed for
12 // updating execution counts and emitting the profile at runtime.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "llvm/Transforms/InstrProfiling.h"
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ADT/Triple.h"
21 #include "llvm/ADT/Twine.h"
22 #include "llvm/Analysis/LoopInfo.h"
23 #include "llvm/Analysis/TargetLibraryInfo.h"
24 #include "llvm/IR/Attributes.h"
25 #include "llvm/IR/BasicBlock.h"
26 #include "llvm/IR/Constant.h"
27 #include "llvm/IR/Constants.h"
28 #include "llvm/IR/DerivedTypes.h"
29 #include "llvm/IR/Dominators.h"
30 #include "llvm/IR/Function.h"
31 #include "llvm/IR/GlobalValue.h"
32 #include "llvm/IR/GlobalVariable.h"
33 #include "llvm/IR/IRBuilder.h"
34 #include "llvm/IR/Instruction.h"
35 #include "llvm/IR/Instructions.h"
36 #include "llvm/IR/IntrinsicInst.h"
37 #include "llvm/IR/Module.h"
38 #include "llvm/IR/Type.h"
39 #include "llvm/Pass.h"
40 #include "llvm/ProfileData/InstrProf.h"
41 #include "llvm/Support/Casting.h"
42 #include "llvm/Support/CommandLine.h"
43 #include "llvm/Support/Error.h"
44 #include "llvm/Support/ErrorHandling.h"
45 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
46 #include "llvm/Transforms/Utils/LoopSimplify.h"
47 #include "llvm/Transforms/Utils/ModuleUtils.h"
48 #include "llvm/Transforms/Utils/SSAUpdater.h"
49 #include <algorithm>
50 #include <cassert>
51 #include <cstddef>
52 #include <cstdint>
53 #include <string>
54 
55 using namespace llvm;
56 
57 #define DEBUG_TYPE "instrprof"
58 
59 // The start and end values of precise value profile range for memory
60 // intrinsic sizes
61 cl::opt<std::string> MemOPSizeRange(
62     "memop-size-range",
63     cl::desc("Set the range of size in memory intrinsic calls to be profiled "
64              "precisely, in a format of <start_val>:<end_val>"),
65     cl::init(""));
66 
67 // The value that considered to be large value in  memory intrinsic.
68 cl::opt<unsigned> MemOPSizeLarge(
69     "memop-size-large",
70     cl::desc("Set large value thresthold in memory intrinsic size profiling. "
71              "Value of 0 disables the large value profiling."),
72     cl::init(8192));
73 
74 namespace {
75 
76 cl::opt<bool> DoNameCompression("enable-name-compression",
77                                 cl::desc("Enable name string compression"),
78                                 cl::init(true));
79 
80 cl::opt<bool> DoHashBasedCounterSplit(
81     "hash-based-counter-split",
82     cl::desc("Rename counter variable of a comdat function based on cfg hash"),
83     cl::init(true));
84 
85 cl::opt<bool> ValueProfileStaticAlloc(
86     "vp-static-alloc",
87     cl::desc("Do static counter allocation for value profiler"),
88     cl::init(true));
89 
90 cl::opt<double> NumCountersPerValueSite(
91     "vp-counters-per-site",
92     cl::desc("The average number of profile counters allocated "
93              "per value profiling site."),
94     // This is set to a very small value because in real programs, only
95     // a very small percentage of value sites have non-zero targets, e.g, 1/30.
96     // For those sites with non-zero profile, the average number of targets
97     // is usually smaller than 2.
98     cl::init(1.0));
99 
100 cl::opt<bool> AtomicCounterUpdatePromoted(
101     "atomic-counter-update-promoted", cl::ZeroOrMore,
102     cl::desc("Do counter update using atomic fetch add "
103              " for promoted counters only"),
104     cl::init(false));
105 
106 // If the option is not specified, the default behavior about whether
107 // counter promotion is done depends on how instrumentaiton lowering
108 // pipeline is setup, i.e., the default value of true of this option
109 // does not mean the promotion will be done by default. Explicitly
110 // setting this option can override the default behavior.
111 cl::opt<bool> DoCounterPromotion("do-counter-promotion", cl::ZeroOrMore,
112                                  cl::desc("Do counter register promotion"),
113                                  cl::init(false));
114 cl::opt<unsigned> MaxNumOfPromotionsPerLoop(
115     cl::ZeroOrMore, "max-counter-promotions-per-loop", cl::init(10),
116     cl::desc("Max number counter promotions per loop to avoid"
117              " increasing register pressure too much"));
118 
119 // A debug option
120 cl::opt<int>
121     MaxNumOfPromotions(cl::ZeroOrMore, "max-counter-promotions", cl::init(-1),
122                        cl::desc("Max number of allowed counter promotions"));
123 
124 cl::opt<bool> SpeculativeCounterPromotion(
125     cl::ZeroOrMore, "speculative-counter-promotion", cl::init(false),
126     cl::desc("Allow counter promotion for loops with multiple exiting blocks "
127              " or top-tested loops. "));
128 
129 class InstrProfilingLegacyPass : public ModulePass {
130   InstrProfiling InstrProf;
131 
132 public:
133   static char ID;
134 
135   InstrProfilingLegacyPass() : ModulePass(ID) {}
136   InstrProfilingLegacyPass(const InstrProfOptions &Options)
137       : ModulePass(ID), InstrProf(Options) {}
138 
139   StringRef getPassName() const override {
140     return "Frontend instrumentation-based coverage lowering";
141   }
142 
143   bool runOnModule(Module &M) override {
144     return InstrProf.run(M, getAnalysis<TargetLibraryInfoWrapperPass>().getTLI());
145   }
146 
147   void getAnalysisUsage(AnalysisUsage &AU) const override {
148     AU.setPreservesCFG();
149     AU.addRequired<TargetLibraryInfoWrapperPass>();
150   }
151 };
152 
153 /// A helper class to promote one counter RMW operation in the loop
154 /// into register update.
155 ///
156 /// RWM update for the counter will be sinked out of the loop after
157 /// the transformation.
158 ///
159 class PGOCounterPromoterHelper : public LoadAndStorePromoter {
160 public:
161   PGOCounterPromoterHelper(Instruction *L, Instruction *S, SSAUpdater &SSA,
162                            Value *Init, BasicBlock *PH,
163                            ArrayRef<BasicBlock *> ExitBlocks,
164                            ArrayRef<Instruction *> InsertPts)
165       : LoadAndStorePromoter({L, S}, SSA), Store(S), ExitBlocks(ExitBlocks),
166         InsertPts(InsertPts) {
167     assert(isa<LoadInst>(L));
168     assert(isa<StoreInst>(S));
169     SSA.AddAvailableValue(PH, Init);
170   }
171   void doExtraRewritesBeforeFinalDeletion() const override {
172     for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) {
173       BasicBlock *ExitBlock = ExitBlocks[i];
174       Instruction *InsertPos = InsertPts[i];
175       // Get LiveIn value into the ExitBlock. If there are multiple
176       // predecessors, the value is defined by a PHI node in this
177       // block.
178       Value *LiveInValue = SSA.GetValueInMiddleOfBlock(ExitBlock);
179       Value *Addr = cast<StoreInst>(Store)->getPointerOperand();
180       IRBuilder<> Builder(InsertPos);
181       if (AtomicCounterUpdatePromoted)
182         Builder.CreateAtomicRMW(AtomicRMWInst::Add, Addr, LiveInValue,
183                                 AtomicOrdering::SequentiallyConsistent);
184       else {
185         LoadInst *OldVal = Builder.CreateLoad(Addr, "pgocount.promoted");
186         auto *NewVal = Builder.CreateAdd(OldVal, LiveInValue);
187         Builder.CreateStore(NewVal, Addr);
188       }
189     }
190   }
191 
192 private:
193   Instruction *Store;
194   ArrayRef<BasicBlock *> ExitBlocks;
195   ArrayRef<Instruction *> InsertPts;
196 };
197 
198 /// A helper class to do register promotion for all profile counter
199 /// updates in a loop.
200 ///
201 class PGOCounterPromoter {
202 public:
203   PGOCounterPromoter(ArrayRef<LoadStorePair> Cands, Loop &Loop)
204       : Candidates(Cands), ExitBlocks(), InsertPts(), ParentLoop(Loop) {
205 
206     SmallVector<BasicBlock *, 8> LoopExitBlocks;
207     SmallPtrSet<BasicBlock *, 8> BlockSet;
208     ParentLoop.getExitBlocks(LoopExitBlocks);
209 
210     for (BasicBlock *ExitBlock : LoopExitBlocks) {
211       if (BlockSet.insert(ExitBlock).second) {
212         ExitBlocks.push_back(ExitBlock);
213         InsertPts.push_back(&*ExitBlock->getFirstInsertionPt());
214       }
215     }
216   }
217 
218   bool run(int64_t *NumPromoted) {
219     // We can't insert into a catchswitch.
220     bool HasCatchSwitch = llvm::any_of(ExitBlocks, [](BasicBlock *Exit) {
221       return isa<CatchSwitchInst>(Exit->getTerminator());
222     });
223 
224     if (HasCatchSwitch)
225       return false;
226 
227     if (!ParentLoop.hasDedicatedExits())
228       return false;
229 
230     BasicBlock *PH = ParentLoop.getLoopPreheader();
231     if (!PH)
232       return false;
233 
234     BasicBlock *H = ParentLoop.getHeader();
235     bool TopTested =
236         ((ParentLoop.getBlocks().size() > 1) && ParentLoop.isLoopExiting(H));
237     if (!SpeculativeCounterPromotion &&
238         (TopTested || ParentLoop.getExitingBlock() == nullptr))
239       return false;
240 
241     unsigned Promoted = 0;
242     for (auto &Cand : Candidates) {
243 
244       SmallVector<PHINode *, 4> NewPHIs;
245       SSAUpdater SSA(&NewPHIs);
246       Value *InitVal = ConstantInt::get(Cand.first->getType(), 0);
247       PGOCounterPromoterHelper Promoter(Cand.first, Cand.second, SSA, InitVal,
248                                         PH, ExitBlocks, InsertPts);
249       Promoter.run(SmallVector<Instruction *, 2>({Cand.first, Cand.second}));
250       Promoted++;
251       if (Promoted >= MaxNumOfPromotionsPerLoop)
252         break;
253       (*NumPromoted)++;
254       if (MaxNumOfPromotions != -1 && *NumPromoted >= MaxNumOfPromotions)
255         break;
256     }
257 
258     DEBUG(dbgs() << Promoted << " counters promoted for loop (depth="
259                  << ParentLoop.getLoopDepth() << ")\n");
260     return Promoted != 0;
261   }
262 
263 private:
264   ArrayRef<LoadStorePair> Candidates;
265   SmallVector<BasicBlock *, 8> ExitBlocks;
266   SmallVector<Instruction *, 8> InsertPts;
267   Loop &ParentLoop;
268 };
269 
270 } // end anonymous namespace
271 
272 PreservedAnalyses InstrProfiling::run(Module &M, ModuleAnalysisManager &AM) {
273   auto &TLI = AM.getResult<TargetLibraryAnalysis>(M);
274   if (!run(M, TLI))
275     return PreservedAnalyses::all();
276 
277   return PreservedAnalyses::none();
278 }
279 
280 char InstrProfilingLegacyPass::ID = 0;
281 INITIALIZE_PASS_BEGIN(
282     InstrProfilingLegacyPass, "instrprof",
283     "Frontend instrumentation-based coverage lowering.", false, false)
284 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
285 INITIALIZE_PASS_END(
286     InstrProfilingLegacyPass, "instrprof",
287     "Frontend instrumentation-based coverage lowering.", false, false)
288 
289 ModulePass *
290 llvm::createInstrProfilingLegacyPass(const InstrProfOptions &Options) {
291   return new InstrProfilingLegacyPass(Options);
292 }
293 
294 static InstrProfIncrementInst *castToIncrementInst(Instruction *Instr) {
295   InstrProfIncrementInst *Inc = dyn_cast<InstrProfIncrementInstStep>(Instr);
296   if (Inc)
297     return Inc;
298   return dyn_cast<InstrProfIncrementInst>(Instr);
299 }
300 
301 bool InstrProfiling::lowerIntrinsics(Function *F) {
302   bool MadeChange = false;
303   PromotionCandidates.clear();
304   for (BasicBlock &BB : *F) {
305     for (auto I = BB.begin(), E = BB.end(); I != E;) {
306       auto Instr = I++;
307       InstrProfIncrementInst *Inc = castToIncrementInst(&*Instr);
308       if (Inc) {
309         lowerIncrement(Inc);
310         MadeChange = true;
311       } else if (auto *Ind = dyn_cast<InstrProfValueProfileInst>(Instr)) {
312         lowerValueProfileInst(Ind);
313         MadeChange = true;
314       }
315     }
316   }
317 
318   if (!MadeChange)
319     return false;
320 
321   promoteCounterLoadStores(F);
322   return true;
323 }
324 
325 bool InstrProfiling::isCounterPromotionEnabled() const {
326   if (DoCounterPromotion.getNumOccurrences() > 0)
327     return DoCounterPromotion;
328 
329   return Options.DoCounterPromotion;
330 }
331 
332 void InstrProfiling::promoteCounterLoadStores(Function *F) {
333   if (!isCounterPromotionEnabled())
334     return;
335 
336   DominatorTree DT(*F);
337   LoopInfo LI(DT);
338   DenseMap<Loop *, SmallVector<LoadStorePair, 8>> LoopPromotionCandidates;
339 
340   for (const auto &LoadStore : PromotionCandidates) {
341     auto *CounterLoad = LoadStore.first;
342     auto *CounterStore = LoadStore.second;
343     BasicBlock *BB = CounterLoad->getParent();
344     Loop *ParentLoop = LI.getLoopFor(BB);
345     if (!ParentLoop)
346       continue;
347     LoopPromotionCandidates[ParentLoop].emplace_back(CounterLoad, CounterStore);
348   }
349 
350   SmallVector<Loop *, 4> Loops = LI.getLoopsInPreorder();
351 
352   for (auto *Loop : Loops) {
353     PGOCounterPromoter Promoter(LoopPromotionCandidates[Loop], *Loop);
354     Promoter.run(&TotalCountersPromoted);
355   }
356 }
357 
358 bool InstrProfiling::run(Module &M, const TargetLibraryInfo &TLI) {
359   bool MadeChange = false;
360 
361   this->M = &M;
362   this->TLI = &TLI;
363   NamesVar = nullptr;
364   NamesSize = 0;
365   ProfileDataMap.clear();
366   UsedVars.clear();
367   getMemOPSizeRangeFromOption(MemOPSizeRange, MemOPSizeRangeStart,
368                               MemOPSizeRangeLast);
369   TT = Triple(M.getTargetTriple());
370 
371   // We did not know how many value sites there would be inside
372   // the instrumented function. This is counting the number of instrumented
373   // target value sites to enter it as field in the profile data variable.
374   for (Function &F : M) {
375     InstrProfIncrementInst *FirstProfIncInst = nullptr;
376     for (BasicBlock &BB : F)
377       for (auto I = BB.begin(), E = BB.end(); I != E; I++)
378         if (auto *Ind = dyn_cast<InstrProfValueProfileInst>(I))
379           computeNumValueSiteCounts(Ind);
380         else if (FirstProfIncInst == nullptr)
381           FirstProfIncInst = dyn_cast<InstrProfIncrementInst>(I);
382 
383     // Value profiling intrinsic lowering requires per-function profile data
384     // variable to be created first.
385     if (FirstProfIncInst != nullptr)
386       static_cast<void>(getOrCreateRegionCounters(FirstProfIncInst));
387   }
388 
389   for (Function &F : M)
390     MadeChange |= lowerIntrinsics(&F);
391 
392   if (GlobalVariable *CoverageNamesVar =
393           M.getNamedGlobal(getCoverageUnusedNamesVarName())) {
394     lowerCoverageData(CoverageNamesVar);
395     MadeChange = true;
396   }
397 
398   if (!MadeChange)
399     return false;
400 
401   emitVNodes();
402   emitNameData();
403   emitRegistration();
404   emitRuntimeHook();
405   emitUses();
406   emitInitialization();
407   return true;
408 }
409 
410 static Constant *getOrInsertValueProfilingCall(Module &M,
411                                                const TargetLibraryInfo &TLI,
412                                                bool IsRange = false) {
413   LLVMContext &Ctx = M.getContext();
414   auto *ReturnTy = Type::getVoidTy(M.getContext());
415 
416   Constant *Res;
417   if (!IsRange) {
418     Type *ParamTypes[] = {
419 #define VALUE_PROF_FUNC_PARAM(ParamType, ParamName, ParamLLVMType) ParamLLVMType
420 #include "llvm/ProfileData/InstrProfData.inc"
421     };
422     auto *ValueProfilingCallTy =
423         FunctionType::get(ReturnTy, makeArrayRef(ParamTypes), false);
424     Res = M.getOrInsertFunction(getInstrProfValueProfFuncName(),
425                                 ValueProfilingCallTy);
426   } else {
427     Type *RangeParamTypes[] = {
428 #define VALUE_RANGE_PROF 1
429 #define VALUE_PROF_FUNC_PARAM(ParamType, ParamName, ParamLLVMType) ParamLLVMType
430 #include "llvm/ProfileData/InstrProfData.inc"
431 #undef VALUE_RANGE_PROF
432     };
433     auto *ValueRangeProfilingCallTy =
434         FunctionType::get(ReturnTy, makeArrayRef(RangeParamTypes), false);
435     Res = M.getOrInsertFunction(getInstrProfValueRangeProfFuncName(),
436                                 ValueRangeProfilingCallTy);
437   }
438 
439   if (Function *FunRes = dyn_cast<Function>(Res)) {
440     if (auto AK = TLI.getExtAttrForI32Param(false))
441       FunRes->addParamAttr(2, AK);
442   }
443   return Res;
444 }
445 
446 void InstrProfiling::computeNumValueSiteCounts(InstrProfValueProfileInst *Ind) {
447   GlobalVariable *Name = Ind->getName();
448   uint64_t ValueKind = Ind->getValueKind()->getZExtValue();
449   uint64_t Index = Ind->getIndex()->getZExtValue();
450   auto It = ProfileDataMap.find(Name);
451   if (It == ProfileDataMap.end()) {
452     PerFunctionProfileData PD;
453     PD.NumValueSites[ValueKind] = Index + 1;
454     ProfileDataMap[Name] = PD;
455   } else if (It->second.NumValueSites[ValueKind] <= Index)
456     It->second.NumValueSites[ValueKind] = Index + 1;
457 }
458 
459 void InstrProfiling::lowerValueProfileInst(InstrProfValueProfileInst *Ind) {
460   GlobalVariable *Name = Ind->getName();
461   auto It = ProfileDataMap.find(Name);
462   assert(It != ProfileDataMap.end() && It->second.DataVar &&
463          "value profiling detected in function with no counter incerement");
464 
465   GlobalVariable *DataVar = It->second.DataVar;
466   uint64_t ValueKind = Ind->getValueKind()->getZExtValue();
467   uint64_t Index = Ind->getIndex()->getZExtValue();
468   for (uint32_t Kind = IPVK_First; Kind < ValueKind; ++Kind)
469     Index += It->second.NumValueSites[Kind];
470 
471   IRBuilder<> Builder(Ind);
472   bool IsRange = (Ind->getValueKind()->getZExtValue() ==
473                   llvm::InstrProfValueKind::IPVK_MemOPSize);
474   CallInst *Call = nullptr;
475   if (!IsRange) {
476     Value *Args[3] = {Ind->getTargetValue(),
477                       Builder.CreateBitCast(DataVar, Builder.getInt8PtrTy()),
478                       Builder.getInt32(Index)};
479     Call = Builder.CreateCall(getOrInsertValueProfilingCall(*M, *TLI), Args);
480   } else {
481     Value *Args[6] = {
482         Ind->getTargetValue(),
483         Builder.CreateBitCast(DataVar, Builder.getInt8PtrTy()),
484         Builder.getInt32(Index),
485         Builder.getInt64(MemOPSizeRangeStart),
486         Builder.getInt64(MemOPSizeRangeLast),
487         Builder.getInt64(MemOPSizeLarge == 0 ? INT64_MIN : MemOPSizeLarge)};
488     Call =
489         Builder.CreateCall(getOrInsertValueProfilingCall(*M, *TLI, true), Args);
490   }
491   if (auto AK = TLI->getExtAttrForI32Param(false))
492     Call->addParamAttr(2, AK);
493   Ind->replaceAllUsesWith(Call);
494   Ind->eraseFromParent();
495 }
496 
497 void InstrProfiling::lowerIncrement(InstrProfIncrementInst *Inc) {
498   GlobalVariable *Counters = getOrCreateRegionCounters(Inc);
499 
500   IRBuilder<> Builder(Inc);
501   uint64_t Index = Inc->getIndex()->getZExtValue();
502   Value *Addr = Builder.CreateConstInBoundsGEP2_64(Counters, 0, Index);
503   Value *Load = Builder.CreateLoad(Addr, "pgocount");
504   auto *Count = Builder.CreateAdd(Load, Inc->getStep());
505   auto *Store = Builder.CreateStore(Count, Addr);
506   Inc->replaceAllUsesWith(Store);
507   if (isCounterPromotionEnabled())
508     PromotionCandidates.emplace_back(cast<Instruction>(Load), Store);
509   Inc->eraseFromParent();
510 }
511 
512 void InstrProfiling::lowerCoverageData(GlobalVariable *CoverageNamesVar) {
513   ConstantArray *Names =
514       cast<ConstantArray>(CoverageNamesVar->getInitializer());
515   for (unsigned I = 0, E = Names->getNumOperands(); I < E; ++I) {
516     Constant *NC = Names->getOperand(I);
517     Value *V = NC->stripPointerCasts();
518     assert(isa<GlobalVariable>(V) && "Missing reference to function name");
519     GlobalVariable *Name = cast<GlobalVariable>(V);
520 
521     Name->setLinkage(GlobalValue::PrivateLinkage);
522     ReferencedNames.push_back(Name);
523     NC->dropAllReferences();
524   }
525   CoverageNamesVar->eraseFromParent();
526 }
527 
528 /// Get the name of a profiling variable for a particular function.
529 static std::string getVarName(InstrProfIncrementInst *Inc, StringRef Prefix) {
530   StringRef NamePrefix = getInstrProfNameVarPrefix();
531   StringRef Name = Inc->getName()->getName().substr(NamePrefix.size());
532   Function *F = Inc->getParent()->getParent();
533   Module *M = F->getParent();
534   if (!DoHashBasedCounterSplit || !isIRPGOFlagSet(M) ||
535       !canRenameComdatFunc(*F))
536     return (Prefix + Name).str();
537   uint64_t FuncHash = Inc->getHash()->getZExtValue();
538   SmallVector<char, 24> HashPostfix;
539   if (Name.endswith((Twine(".") + Twine(FuncHash)).toStringRef(HashPostfix)))
540     return (Prefix + Name).str();
541   return (Prefix + Name + "." + Twine(FuncHash)).str();
542 }
543 
544 static inline bool shouldRecordFunctionAddr(Function *F) {
545   // Check the linkage
546   bool HasAvailableExternallyLinkage = F->hasAvailableExternallyLinkage();
547   if (!F->hasLinkOnceLinkage() && !F->hasLocalLinkage() &&
548       !HasAvailableExternallyLinkage)
549     return true;
550 
551   // A function marked 'alwaysinline' with available_externally linkage can't
552   // have its address taken. Doing so would create an undefined external ref to
553   // the function, which would fail to link.
554   if (HasAvailableExternallyLinkage &&
555       F->hasFnAttribute(Attribute::AlwaysInline))
556     return false;
557 
558   // Prohibit function address recording if the function is both internal and
559   // COMDAT. This avoids the profile data variable referencing internal symbols
560   // in COMDAT.
561   if (F->hasLocalLinkage() && F->hasComdat())
562     return false;
563 
564   // Check uses of this function for other than direct calls or invokes to it.
565   // Inline virtual functions have linkeOnceODR linkage. When a key method
566   // exists, the vtable will only be emitted in the TU where the key method
567   // is defined. In a TU where vtable is not available, the function won't
568   // be 'addresstaken'. If its address is not recorded here, the profile data
569   // with missing address may be picked by the linker leading  to missing
570   // indirect call target info.
571   return F->hasAddressTaken() || F->hasLinkOnceLinkage();
572 }
573 
574 static inline Comdat *getOrCreateProfileComdat(Module &M, Function &F,
575                                                InstrProfIncrementInst *Inc) {
576   if (!needsComdatForCounter(F, M))
577     return nullptr;
578 
579   // COFF format requires a COMDAT section to have a key symbol with the same
580   // name. The linker targeting COFF also requires that the COMDAT
581   // a section is associated to must precede the associating section. For this
582   // reason, we must choose the counter var's name as the name of the comdat.
583   StringRef ComdatPrefix = (Triple(M.getTargetTriple()).isOSBinFormatCOFF()
584                                 ? getInstrProfCountersVarPrefix()
585                                 : getInstrProfComdatPrefix());
586   return M.getOrInsertComdat(StringRef(getVarName(Inc, ComdatPrefix)));
587 }
588 
589 static bool needsRuntimeRegistrationOfSectionRange(const Module &M) {
590   // Don't do this for Darwin.  compiler-rt uses linker magic.
591   if (Triple(M.getTargetTriple()).isOSDarwin())
592     return false;
593 
594   // Use linker script magic to get data/cnts/name start/end.
595   if (Triple(M.getTargetTriple()).isOSLinux() ||
596       Triple(M.getTargetTriple()).isOSFreeBSD() ||
597       Triple(M.getTargetTriple()).isPS4CPU())
598     return false;
599 
600   return true;
601 }
602 
603 GlobalVariable *
604 InstrProfiling::getOrCreateRegionCounters(InstrProfIncrementInst *Inc) {
605   GlobalVariable *NamePtr = Inc->getName();
606   auto It = ProfileDataMap.find(NamePtr);
607   PerFunctionProfileData PD;
608   if (It != ProfileDataMap.end()) {
609     if (It->second.RegionCounters)
610       return It->second.RegionCounters;
611     PD = It->second;
612   }
613 
614   // Move the name variable to the right section. Place them in a COMDAT group
615   // if the associated function is a COMDAT. This will make sure that
616   // only one copy of counters of the COMDAT function will be emitted after
617   // linking.
618   Function *Fn = Inc->getParent()->getParent();
619   Comdat *ProfileVarsComdat = nullptr;
620   ProfileVarsComdat = getOrCreateProfileComdat(*M, *Fn, Inc);
621 
622   uint64_t NumCounters = Inc->getNumCounters()->getZExtValue();
623   LLVMContext &Ctx = M->getContext();
624   ArrayType *CounterTy = ArrayType::get(Type::getInt64Ty(Ctx), NumCounters);
625 
626   // Create the counters variable.
627   auto *CounterPtr =
628       new GlobalVariable(*M, CounterTy, false, NamePtr->getLinkage(),
629                          Constant::getNullValue(CounterTy),
630                          getVarName(Inc, getInstrProfCountersVarPrefix()));
631   CounterPtr->setVisibility(NamePtr->getVisibility());
632   CounterPtr->setSection(
633       getInstrProfSectionName(IPSK_cnts, TT.getObjectFormat()));
634   CounterPtr->setAlignment(8);
635   CounterPtr->setComdat(ProfileVarsComdat);
636 
637   auto *Int8PtrTy = Type::getInt8PtrTy(Ctx);
638   // Allocate statically the array of pointers to value profile nodes for
639   // the current function.
640   Constant *ValuesPtrExpr = ConstantPointerNull::get(Int8PtrTy);
641   if (ValueProfileStaticAlloc && !needsRuntimeRegistrationOfSectionRange(*M)) {
642     uint64_t NS = 0;
643     for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
644       NS += PD.NumValueSites[Kind];
645     if (NS) {
646       ArrayType *ValuesTy = ArrayType::get(Type::getInt64Ty(Ctx), NS);
647 
648       auto *ValuesVar =
649           new GlobalVariable(*M, ValuesTy, false, NamePtr->getLinkage(),
650                              Constant::getNullValue(ValuesTy),
651                              getVarName(Inc, getInstrProfValuesVarPrefix()));
652       ValuesVar->setVisibility(NamePtr->getVisibility());
653       ValuesVar->setSection(
654           getInstrProfSectionName(IPSK_vals, TT.getObjectFormat()));
655       ValuesVar->setAlignment(8);
656       ValuesVar->setComdat(ProfileVarsComdat);
657       ValuesPtrExpr =
658           ConstantExpr::getBitCast(ValuesVar, Type::getInt8PtrTy(Ctx));
659     }
660   }
661 
662   // Create data variable.
663   auto *Int16Ty = Type::getInt16Ty(Ctx);
664   auto *Int16ArrayTy = ArrayType::get(Int16Ty, IPVK_Last + 1);
665   Type *DataTypes[] = {
666 #define INSTR_PROF_DATA(Type, LLVMType, Name, Init) LLVMType,
667 #include "llvm/ProfileData/InstrProfData.inc"
668   };
669   auto *DataTy = StructType::get(Ctx, makeArrayRef(DataTypes));
670 
671   Constant *FunctionAddr = shouldRecordFunctionAddr(Fn)
672                                ? ConstantExpr::getBitCast(Fn, Int8PtrTy)
673                                : ConstantPointerNull::get(Int8PtrTy);
674 
675   Constant *Int16ArrayVals[IPVK_Last + 1];
676   for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
677     Int16ArrayVals[Kind] = ConstantInt::get(Int16Ty, PD.NumValueSites[Kind]);
678 
679   Constant *DataVals[] = {
680 #define INSTR_PROF_DATA(Type, LLVMType, Name, Init) Init,
681 #include "llvm/ProfileData/InstrProfData.inc"
682   };
683   auto *Data = new GlobalVariable(*M, DataTy, false, NamePtr->getLinkage(),
684                                   ConstantStruct::get(DataTy, DataVals),
685                                   getVarName(Inc, getInstrProfDataVarPrefix()));
686   Data->setVisibility(NamePtr->getVisibility());
687   Data->setSection(getInstrProfSectionName(IPSK_data, TT.getObjectFormat()));
688   Data->setAlignment(INSTR_PROF_DATA_ALIGNMENT);
689   Data->setComdat(ProfileVarsComdat);
690 
691   PD.RegionCounters = CounterPtr;
692   PD.DataVar = Data;
693   ProfileDataMap[NamePtr] = PD;
694 
695   // Mark the data variable as used so that it isn't stripped out.
696   UsedVars.push_back(Data);
697   // Now that the linkage set by the FE has been passed to the data and counter
698   // variables, reset Name variable's linkage and visibility to private so that
699   // it can be removed later by the compiler.
700   NamePtr->setLinkage(GlobalValue::PrivateLinkage);
701   // Collect the referenced names to be used by emitNameData.
702   ReferencedNames.push_back(NamePtr);
703 
704   return CounterPtr;
705 }
706 
707 void InstrProfiling::emitVNodes() {
708   if (!ValueProfileStaticAlloc)
709     return;
710 
711   // For now only support this on platforms that do
712   // not require runtime registration to discover
713   // named section start/end.
714   if (needsRuntimeRegistrationOfSectionRange(*M))
715     return;
716 
717   size_t TotalNS = 0;
718   for (auto &PD : ProfileDataMap) {
719     for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
720       TotalNS += PD.second.NumValueSites[Kind];
721   }
722 
723   if (!TotalNS)
724     return;
725 
726   uint64_t NumCounters = TotalNS * NumCountersPerValueSite;
727 // Heuristic for small programs with very few total value sites.
728 // The default value of vp-counters-per-site is chosen based on
729 // the observation that large apps usually have a low percentage
730 // of value sites that actually have any profile data, and thus
731 // the average number of counters per site is low. For small
732 // apps with very few sites, this may not be true. Bump up the
733 // number of counters in this case.
734 #define INSTR_PROF_MIN_VAL_COUNTS 10
735   if (NumCounters < INSTR_PROF_MIN_VAL_COUNTS)
736     NumCounters = std::max(INSTR_PROF_MIN_VAL_COUNTS, (int)NumCounters * 2);
737 
738   auto &Ctx = M->getContext();
739   Type *VNodeTypes[] = {
740 #define INSTR_PROF_VALUE_NODE(Type, LLVMType, Name, Init) LLVMType,
741 #include "llvm/ProfileData/InstrProfData.inc"
742   };
743   auto *VNodeTy = StructType::get(Ctx, makeArrayRef(VNodeTypes));
744 
745   ArrayType *VNodesTy = ArrayType::get(VNodeTy, NumCounters);
746   auto *VNodesVar = new GlobalVariable(
747       *M, VNodesTy, false, GlobalValue::PrivateLinkage,
748       Constant::getNullValue(VNodesTy), getInstrProfVNodesVarName());
749   VNodesVar->setSection(
750       getInstrProfSectionName(IPSK_vnodes, TT.getObjectFormat()));
751   UsedVars.push_back(VNodesVar);
752 }
753 
754 void InstrProfiling::emitNameData() {
755   std::string UncompressedData;
756 
757   if (ReferencedNames.empty())
758     return;
759 
760   std::string CompressedNameStr;
761   if (Error E = collectPGOFuncNameStrings(ReferencedNames, CompressedNameStr,
762                                           DoNameCompression)) {
763     report_fatal_error(toString(std::move(E)), false);
764   }
765 
766   auto &Ctx = M->getContext();
767   auto *NamesVal = ConstantDataArray::getString(
768       Ctx, StringRef(CompressedNameStr), false);
769   NamesVar = new GlobalVariable(*M, NamesVal->getType(), true,
770                                 GlobalValue::PrivateLinkage, NamesVal,
771                                 getInstrProfNamesVarName());
772   NamesSize = CompressedNameStr.size();
773   NamesVar->setSection(
774       getInstrProfSectionName(IPSK_name, TT.getObjectFormat()));
775   UsedVars.push_back(NamesVar);
776 
777   for (auto *NamePtr : ReferencedNames)
778     NamePtr->eraseFromParent();
779 }
780 
781 void InstrProfiling::emitRegistration() {
782   if (!needsRuntimeRegistrationOfSectionRange(*M))
783     return;
784 
785   // Construct the function.
786   auto *VoidTy = Type::getVoidTy(M->getContext());
787   auto *VoidPtrTy = Type::getInt8PtrTy(M->getContext());
788   auto *Int64Ty = Type::getInt64Ty(M->getContext());
789   auto *RegisterFTy = FunctionType::get(VoidTy, false);
790   auto *RegisterF = Function::Create(RegisterFTy, GlobalValue::InternalLinkage,
791                                      getInstrProfRegFuncsName(), M);
792   RegisterF->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
793   if (Options.NoRedZone)
794     RegisterF->addFnAttr(Attribute::NoRedZone);
795 
796   auto *RuntimeRegisterTy = FunctionType::get(VoidTy, VoidPtrTy, false);
797   auto *RuntimeRegisterF =
798       Function::Create(RuntimeRegisterTy, GlobalVariable::ExternalLinkage,
799                        getInstrProfRegFuncName(), M);
800 
801   IRBuilder<> IRB(BasicBlock::Create(M->getContext(), "", RegisterF));
802   for (Value *Data : UsedVars)
803     if (Data != NamesVar)
804       IRB.CreateCall(RuntimeRegisterF, IRB.CreateBitCast(Data, VoidPtrTy));
805 
806   if (NamesVar) {
807     Type *ParamTypes[] = {VoidPtrTy, Int64Ty};
808     auto *NamesRegisterTy =
809         FunctionType::get(VoidTy, makeArrayRef(ParamTypes), false);
810     auto *NamesRegisterF =
811         Function::Create(NamesRegisterTy, GlobalVariable::ExternalLinkage,
812                          getInstrProfNamesRegFuncName(), M);
813     IRB.CreateCall(NamesRegisterF, {IRB.CreateBitCast(NamesVar, VoidPtrTy),
814                                     IRB.getInt64(NamesSize)});
815   }
816 
817   IRB.CreateRetVoid();
818 }
819 
820 void InstrProfiling::emitRuntimeHook() {
821   // We expect the linker to be invoked with -u<hook_var> flag for linux,
822   // for which case there is no need to emit the user function.
823   if (Triple(M->getTargetTriple()).isOSLinux())
824     return;
825 
826   // If the module's provided its own runtime, we don't need to do anything.
827   if (M->getGlobalVariable(getInstrProfRuntimeHookVarName()))
828     return;
829 
830   // Declare an external variable that will pull in the runtime initialization.
831   auto *Int32Ty = Type::getInt32Ty(M->getContext());
832   auto *Var =
833       new GlobalVariable(*M, Int32Ty, false, GlobalValue::ExternalLinkage,
834                          nullptr, getInstrProfRuntimeHookVarName());
835 
836   // Make a function that uses it.
837   auto *User = Function::Create(FunctionType::get(Int32Ty, false),
838                                 GlobalValue::LinkOnceODRLinkage,
839                                 getInstrProfRuntimeHookVarUseFuncName(), M);
840   User->addFnAttr(Attribute::NoInline);
841   if (Options.NoRedZone)
842     User->addFnAttr(Attribute::NoRedZone);
843   User->setVisibility(GlobalValue::HiddenVisibility);
844   if (Triple(M->getTargetTriple()).supportsCOMDAT())
845     User->setComdat(M->getOrInsertComdat(User->getName()));
846 
847   IRBuilder<> IRB(BasicBlock::Create(M->getContext(), "", User));
848   auto *Load = IRB.CreateLoad(Var);
849   IRB.CreateRet(Load);
850 
851   // Mark the user variable as used so that it isn't stripped out.
852   UsedVars.push_back(User);
853 }
854 
855 void InstrProfiling::emitUses() {
856   if (!UsedVars.empty())
857     appendToUsed(*M, UsedVars);
858 }
859 
860 void InstrProfiling::emitInitialization() {
861   StringRef InstrProfileOutput = Options.InstrProfileOutput;
862 
863   if (!InstrProfileOutput.empty()) {
864     // Create variable for profile name.
865     Constant *ProfileNameConst =
866         ConstantDataArray::getString(M->getContext(), InstrProfileOutput, true);
867     GlobalVariable *ProfileNameVar = new GlobalVariable(
868         *M, ProfileNameConst->getType(), true, GlobalValue::WeakAnyLinkage,
869         ProfileNameConst, INSTR_PROF_QUOTE(INSTR_PROF_PROFILE_NAME_VAR));
870     if (TT.supportsCOMDAT()) {
871       ProfileNameVar->setLinkage(GlobalValue::ExternalLinkage);
872       ProfileNameVar->setComdat(M->getOrInsertComdat(
873           StringRef(INSTR_PROF_QUOTE(INSTR_PROF_PROFILE_NAME_VAR))));
874     }
875   }
876 
877   Constant *RegisterF = M->getFunction(getInstrProfRegFuncsName());
878   if (!RegisterF)
879     return;
880 
881   // Create the initialization function.
882   auto *VoidTy = Type::getVoidTy(M->getContext());
883   auto *F = Function::Create(FunctionType::get(VoidTy, false),
884                              GlobalValue::InternalLinkage,
885                              getInstrProfInitFuncName(), M);
886   F->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
887   F->addFnAttr(Attribute::NoInline);
888   if (Options.NoRedZone)
889     F->addFnAttr(Attribute::NoRedZone);
890 
891   // Add the basic block and the necessary calls.
892   IRBuilder<> IRB(BasicBlock::Create(M->getContext(), "", F));
893   if (RegisterF)
894     IRB.CreateCall(RegisterF, {});
895   IRB.CreateRetVoid();
896 
897   appendToGlobalCtors(*M, F, 0);
898 }
899