LLVM  6.0.0svn
InstrProfiling.cpp
Go to the documentation of this file.
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 
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"
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"
41 #include "llvm/Support/Casting.h"
43 #include "llvm/Support/Error.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
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.
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(20),
116  cl::desc("Max number counter promotions per loop to avoid"
117  " increasing register pressure too much"));
118 
119 // A debug option
121  MaxNumOfPromotions(cl::ZeroOrMore, "max-counter-promotions", cl::init(-1),
122  cl::desc("Max number of allowed counter promotions"));
123 
124 cl::opt<unsigned> SpeculativeCounterPromotionMaxExiting(
125  cl::ZeroOrMore, "speculative-counter-promotion-max-exiting", cl::init(3),
126  cl::desc("The max number of exiting blocks of a loop to allow "
127  " speculative counter promotion"));
128 
129 cl::opt<bool> SpeculativeCounterPromotionToLoop(
130  cl::ZeroOrMore, "speculative-counter-promotion-to-loop", cl::init(false),
131  cl::desc("When the option is false, if the target block is in a loop, "
132  "the promotion will be disallowed unless the promoted counter "
133  " update can be further/iteratively promoted into an acyclic "
134  " region."));
135 
136 cl::opt<bool> IterativeCounterPromotion(
137  cl::ZeroOrMore, "iterative-counter-promotion", cl::init(true),
138  cl::desc("Allow counter promotion across the whole loop nest."));
139 
140 class InstrProfilingLegacyPass : public ModulePass {
141  InstrProfiling InstrProf;
142 
143 public:
144  static char ID;
145 
146  InstrProfilingLegacyPass() : ModulePass(ID) {}
147  InstrProfilingLegacyPass(const InstrProfOptions &Options)
148  : ModulePass(ID), InstrProf(Options) {}
149 
150  StringRef getPassName() const override {
151  return "Frontend instrumentation-based coverage lowering";
152  }
153 
154  bool runOnModule(Module &M) override {
155  return InstrProf.run(M, getAnalysis<TargetLibraryInfoWrapperPass>().getTLI());
156  }
157 
158  void getAnalysisUsage(AnalysisUsage &AU) const override {
159  AU.setPreservesCFG();
161  }
162 };
163 
164 ///
165 /// A helper class to promote one counter RMW operation in the loop
166 /// into register update.
167 ///
168 /// RWM update for the counter will be sinked out of the loop after
169 /// the transformation.
170 ///
171 class PGOCounterPromoterHelper : public LoadAndStorePromoter {
172 public:
173  PGOCounterPromoterHelper(
175  BasicBlock *PH, ArrayRef<BasicBlock *> ExitBlocks,
176  ArrayRef<Instruction *> InsertPts,
178  LoopInfo &LI)
179  : LoadAndStorePromoter({L, S}, SSA), Store(S), ExitBlocks(ExitBlocks),
180  InsertPts(InsertPts), LoopToCandidates(LoopToCands), LI(LI) {
181  assert(isa<LoadInst>(L));
182  assert(isa<StoreInst>(S));
183  SSA.AddAvailableValue(PH, Init);
184  }
185 
186  void doExtraRewritesBeforeFinalDeletion() const override {
187  for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) {
188  BasicBlock *ExitBlock = ExitBlocks[i];
189  Instruction *InsertPos = InsertPts[i];
190  // Get LiveIn value into the ExitBlock. If there are multiple
191  // predecessors, the value is defined by a PHI node in this
192  // block.
193  Value *LiveInValue = SSA.GetValueInMiddleOfBlock(ExitBlock);
194  Value *Addr = cast<StoreInst>(Store)->getPointerOperand();
195  IRBuilder<> Builder(InsertPos);
196  if (AtomicCounterUpdatePromoted)
197  // automic update currently can only be promoted across the current
198  // loop, not the whole loop nest.
199  Builder.CreateAtomicRMW(AtomicRMWInst::Add, Addr, LiveInValue,
201  else {
202  LoadInst *OldVal = Builder.CreateLoad(Addr, "pgocount.promoted");
203  auto *NewVal = Builder.CreateAdd(OldVal, LiveInValue);
204  auto *NewStore = Builder.CreateStore(NewVal, Addr);
205 
206  // Now update the parent loop's candidate list:
207  if (IterativeCounterPromotion) {
208  auto *TargetLoop = LI.getLoopFor(ExitBlock);
209  if (TargetLoop)
210  LoopToCandidates[TargetLoop].emplace_back(OldVal, NewStore);
211  }
212  }
213  }
214  }
215 
216 private:
218  ArrayRef<BasicBlock *> ExitBlocks;
219  ArrayRef<Instruction *> InsertPts;
221  LoopInfo &LI;
222 };
223 
224 /// A helper class to do register promotion for all profile counter
225 /// updates in a loop.
226 ///
227 class PGOCounterPromoter {
228 public:
229  PGOCounterPromoter(
231  Loop &CurLoop, LoopInfo &LI)
232  : LoopToCandidates(LoopToCands), ExitBlocks(), InsertPts(), L(CurLoop),
233  LI(LI) {
234 
235  SmallVector<BasicBlock *, 8> LoopExitBlocks;
237  L.getExitBlocks(LoopExitBlocks);
238 
239  for (BasicBlock *ExitBlock : LoopExitBlocks) {
240  if (BlockSet.insert(ExitBlock).second) {
241  ExitBlocks.push_back(ExitBlock);
242  InsertPts.push_back(&*ExitBlock->getFirstInsertionPt());
243  }
244  }
245  }
246 
247  bool run(int64_t *NumPromoted) {
248  unsigned MaxProm = getMaxNumOfPromotionsInLoop(&L);
249  if (MaxProm == 0)
250  return false;
251 
252  unsigned Promoted = 0;
253  for (auto &Cand : LoopToCandidates[&L]) {
254 
256  SSAUpdater SSA(&NewPHIs);
257  Value *InitVal = ConstantInt::get(Cand.first->getType(), 0);
258 
259  PGOCounterPromoterHelper Promoter(Cand.first, Cand.second, SSA, InitVal,
260  L.getLoopPreheader(), ExitBlocks,
261  InsertPts, LoopToCandidates, LI);
262  Promoter.run(SmallVector<Instruction *, 2>({Cand.first, Cand.second}));
263  Promoted++;
264  if (Promoted >= MaxProm)
265  break;
266 
267  (*NumPromoted)++;
268  if (MaxNumOfPromotions != -1 && *NumPromoted >= MaxNumOfPromotions)
269  break;
270  }
271 
272  DEBUG(dbgs() << Promoted << " counters promoted for loop (depth="
273  << L.getLoopDepth() << ")\n");
274  return Promoted != 0;
275  }
276 
277 private:
278  bool allowSpeculativeCounterPromotion(Loop *LP) {
279  SmallVector<BasicBlock *, 8> ExitingBlocks;
280  L.getExitingBlocks(ExitingBlocks);
281  // Not considierered speculative.
282  if (ExitingBlocks.size() == 1)
283  return true;
284  if (ExitingBlocks.size() > SpeculativeCounterPromotionMaxExiting)
285  return false;
286  return true;
287  }
288 
289  // Returns the max number of Counter Promotions for LP.
290  unsigned getMaxNumOfPromotionsInLoop(Loop *LP) {
291  // We can't insert into a catchswitch.
292  SmallVector<BasicBlock *, 8> LoopExitBlocks;
293  LP->getExitBlocks(LoopExitBlocks);
294  if (llvm::any_of(LoopExitBlocks, [](BasicBlock *Exit) {
295  return isa<CatchSwitchInst>(Exit->getTerminator());
296  }))
297  return 0;
298 
299  if (!LP->hasDedicatedExits())
300  return 0;
301 
302  BasicBlock *PH = LP->getLoopPreheader();
303  if (!PH)
304  return 0;
305 
306  SmallVector<BasicBlock *, 8> ExitingBlocks;
307  LP->getExitingBlocks(ExitingBlocks);
308  // Not considierered speculative.
309  if (ExitingBlocks.size() == 1)
310  return MaxNumOfPromotionsPerLoop;
311 
312  if (ExitingBlocks.size() > SpeculativeCounterPromotionMaxExiting)
313  return 0;
314 
315  // Whether the target block is in a loop does not matter:
316  if (SpeculativeCounterPromotionToLoop)
317  return MaxNumOfPromotionsPerLoop;
318 
319  // Now check the target block:
320  unsigned MaxProm = MaxNumOfPromotionsPerLoop;
321  for (auto *TargetBlock : LoopExitBlocks) {
322  auto *TargetLoop = LI.getLoopFor(TargetBlock);
323  if (!TargetLoop)
324  continue;
325  unsigned MaxPromForTarget = getMaxNumOfPromotionsInLoop(TargetLoop);
326  unsigned PendingCandsInTarget = LoopToCandidates[TargetLoop].size();
327  MaxProm =
328  std::min(MaxProm, std::max(MaxPromForTarget, PendingCandsInTarget) -
329  PendingCandsInTarget);
330  }
331  return MaxProm;
332  }
333 
335  SmallVector<BasicBlock *, 8> ExitBlocks;
337  Loop &L;
338  LoopInfo &LI;
339 };
340 
341 } // end anonymous namespace
342 
344  auto &TLI = AM.getResult<TargetLibraryAnalysis>(M);
345  if (!run(M, TLI))
346  return PreservedAnalyses::all();
347 
348  return PreservedAnalyses::none();
349 }
350 
353  InstrProfilingLegacyPass, "instrprof",
354  "Frontend instrumentation-based coverage lowering.", false, false)
357  InstrProfilingLegacyPass, "instrprof",
358  "Frontend instrumentation-based coverage lowering.", false, false)
359 
360 ModulePass *
362  return new InstrProfilingLegacyPass(Options);
363 }
364 
367  if (Inc)
368  return Inc;
369  return dyn_cast<InstrProfIncrementInst>(Instr);
370 }
371 
372 bool InstrProfiling::lowerIntrinsics(Function *F) {
373  bool MadeChange = false;
374  PromotionCandidates.clear();
375  for (BasicBlock &BB : *F) {
376  for (auto I = BB.begin(), E = BB.end(); I != E;) {
377  auto Instr = I++;
379  if (Inc) {
380  lowerIncrement(Inc);
381  MadeChange = true;
382  } else if (auto *Ind = dyn_cast<InstrProfValueProfileInst>(Instr)) {
383  lowerValueProfileInst(Ind);
384  MadeChange = true;
385  }
386  }
387  }
388 
389  if (!MadeChange)
390  return false;
391 
392  promoteCounterLoadStores(F);
393  return true;
394 }
395 
396 bool InstrProfiling::isCounterPromotionEnabled() const {
397  if (DoCounterPromotion.getNumOccurrences() > 0)
398  return DoCounterPromotion;
399 
400  return Options.DoCounterPromotion;
401 }
402 
403 void InstrProfiling::promoteCounterLoadStores(Function *F) {
404  if (!isCounterPromotionEnabled())
405  return;
406 
407  DominatorTree DT(*F);
408  LoopInfo LI(DT);
409  DenseMap<Loop *, SmallVector<LoadStorePair, 8>> LoopPromotionCandidates;
410 
411  for (const auto &LoadStore : PromotionCandidates) {
412  auto *CounterLoad = LoadStore.first;
413  auto *CounterStore = LoadStore.second;
414  BasicBlock *BB = CounterLoad->getParent();
415  Loop *ParentLoop = LI.getLoopFor(BB);
416  if (!ParentLoop)
417  continue;
418  LoopPromotionCandidates[ParentLoop].emplace_back(CounterLoad, CounterStore);
419  }
420 
422 
423  // Do a post-order traversal of the loops so that counter updates can be
424  // iteratively hoisted outside the loop nest.
425  for (auto *Loop : llvm::reverse(Loops)) {
426  PGOCounterPromoter Promoter(LoopPromotionCandidates, *Loop, LI);
427  Promoter.run(&TotalCountersPromoted);
428  }
429 }
430 
432  bool MadeChange = false;
433 
434  this->M = &M;
435  this->TLI = &TLI;
436  NamesVar = nullptr;
437  NamesSize = 0;
438  ProfileDataMap.clear();
439  UsedVars.clear();
440  getMemOPSizeRangeFromOption(MemOPSizeRange, MemOPSizeRangeStart,
441  MemOPSizeRangeLast);
442  TT = Triple(M.getTargetTriple());
443 
444  // We did not know how many value sites there would be inside
445  // the instrumented function. This is counting the number of instrumented
446  // target value sites to enter it as field in the profile data variable.
447  for (Function &F : M) {
448  InstrProfIncrementInst *FirstProfIncInst = nullptr;
449  for (BasicBlock &BB : F)
450  for (auto I = BB.begin(), E = BB.end(); I != E; I++)
451  if (auto *Ind = dyn_cast<InstrProfValueProfileInst>(I))
452  computeNumValueSiteCounts(Ind);
453  else if (FirstProfIncInst == nullptr)
454  FirstProfIncInst = dyn_cast<InstrProfIncrementInst>(I);
455 
456  // Value profiling intrinsic lowering requires per-function profile data
457  // variable to be created first.
458  if (FirstProfIncInst != nullptr)
459  static_cast<void>(getOrCreateRegionCounters(FirstProfIncInst));
460  }
461 
462  for (Function &F : M)
463  MadeChange |= lowerIntrinsics(&F);
464 
465  if (GlobalVariable *CoverageNamesVar =
466  M.getNamedGlobal(getCoverageUnusedNamesVarName())) {
467  lowerCoverageData(CoverageNamesVar);
468  MadeChange = true;
469  }
470 
471  if (!MadeChange)
472  return false;
473 
474  emitVNodes();
475  emitNameData();
476  emitRegistration();
477  emitRuntimeHook();
478  emitUses();
479  emitInitialization();
480  return true;
481 }
482 
484  const TargetLibraryInfo &TLI,
485  bool IsRange = false) {
486  LLVMContext &Ctx = M.getContext();
487  auto *ReturnTy = Type::getVoidTy(M.getContext());
488 
489  Constant *Res;
490  if (!IsRange) {
491  Type *ParamTypes[] = {
492 #define VALUE_PROF_FUNC_PARAM(ParamType, ParamName, ParamLLVMType) ParamLLVMType
494  };
495  auto *ValueProfilingCallTy =
496  FunctionType::get(ReturnTy, makeArrayRef(ParamTypes), false);
498  ValueProfilingCallTy);
499  } else {
500  Type *RangeParamTypes[] = {
501 #define VALUE_RANGE_PROF 1
502 #define VALUE_PROF_FUNC_PARAM(ParamType, ParamName, ParamLLVMType) ParamLLVMType
504 #undef VALUE_RANGE_PROF
505  };
506  auto *ValueRangeProfilingCallTy =
507  FunctionType::get(ReturnTy, makeArrayRef(RangeParamTypes), false);
509  ValueRangeProfilingCallTy);
510  }
511 
512  if (Function *FunRes = dyn_cast<Function>(Res)) {
513  if (auto AK = TLI.getExtAttrForI32Param(false))
514  FunRes->addParamAttr(2, AK);
515  }
516  return Res;
517 }
518 
519 void InstrProfiling::computeNumValueSiteCounts(InstrProfValueProfileInst *Ind) {
520  GlobalVariable *Name = Ind->getName();
521  uint64_t ValueKind = Ind->getValueKind()->getZExtValue();
522  uint64_t Index = Ind->getIndex()->getZExtValue();
523  auto It = ProfileDataMap.find(Name);
524  if (It == ProfileDataMap.end()) {
525  PerFunctionProfileData PD;
526  PD.NumValueSites[ValueKind] = Index + 1;
527  ProfileDataMap[Name] = PD;
528  } else if (It->second.NumValueSites[ValueKind] <= Index)
529  It->second.NumValueSites[ValueKind] = Index + 1;
530 }
531 
532 void InstrProfiling::lowerValueProfileInst(InstrProfValueProfileInst *Ind) {
533  GlobalVariable *Name = Ind->getName();
534  auto It = ProfileDataMap.find(Name);
535  assert(It != ProfileDataMap.end() && It->second.DataVar &&
536  "value profiling detected in function with no counter incerement");
537 
538  GlobalVariable *DataVar = It->second.DataVar;
539  uint64_t ValueKind = Ind->getValueKind()->getZExtValue();
540  uint64_t Index = Ind->getIndex()->getZExtValue();
541  for (uint32_t Kind = IPVK_First; Kind < ValueKind; ++Kind)
542  Index += It->second.NumValueSites[Kind];
543 
544  IRBuilder<> Builder(Ind);
545  bool IsRange = (Ind->getValueKind()->getZExtValue() ==
546  llvm::InstrProfValueKind::IPVK_MemOPSize);
547  CallInst *Call = nullptr;
548  if (!IsRange) {
549  Value *Args[3] = {Ind->getTargetValue(),
550  Builder.CreateBitCast(DataVar, Builder.getInt8PtrTy()),
551  Builder.getInt32(Index)};
552  Call = Builder.CreateCall(getOrInsertValueProfilingCall(*M, *TLI), Args);
553  } else {
554  Value *Args[6] = {
555  Ind->getTargetValue(),
556  Builder.CreateBitCast(DataVar, Builder.getInt8PtrTy()),
557  Builder.getInt32(Index),
558  Builder.getInt64(MemOPSizeRangeStart),
559  Builder.getInt64(MemOPSizeRangeLast),
560  Builder.getInt64(MemOPSizeLarge == 0 ? INT64_MIN : MemOPSizeLarge)};
561  Call =
562  Builder.CreateCall(getOrInsertValueProfilingCall(*M, *TLI, true), Args);
563  }
564  if (auto AK = TLI->getExtAttrForI32Param(false))
565  Call->addParamAttr(2, AK);
566  Ind->replaceAllUsesWith(Call);
567  Ind->eraseFromParent();
568 }
569 
570 void InstrProfiling::lowerIncrement(InstrProfIncrementInst *Inc) {
571  GlobalVariable *Counters = getOrCreateRegionCounters(Inc);
572 
573  IRBuilder<> Builder(Inc);
574  uint64_t Index = Inc->getIndex()->getZExtValue();
575  Value *Addr = Builder.CreateConstInBoundsGEP2_64(Counters, 0, Index);
576  Value *Load = Builder.CreateLoad(Addr, "pgocount");
577  auto *Count = Builder.CreateAdd(Load, Inc->getStep());
578  auto *Store = Builder.CreateStore(Count, Addr);
580  if (isCounterPromotionEnabled())
581  PromotionCandidates.emplace_back(cast<Instruction>(Load), Store);
582  Inc->eraseFromParent();
583 }
584 
585 void InstrProfiling::lowerCoverageData(GlobalVariable *CoverageNamesVar) {
586  ConstantArray *Names =
587  cast<ConstantArray>(CoverageNamesVar->getInitializer());
588  for (unsigned I = 0, E = Names->getNumOperands(); I < E; ++I) {
589  Constant *NC = Names->getOperand(I);
590  Value *V = NC->stripPointerCasts();
591  assert(isa<GlobalVariable>(V) && "Missing reference to function name");
592  GlobalVariable *Name = cast<GlobalVariable>(V);
593 
595  ReferencedNames.push_back(Name);
596  NC->dropAllReferences();
597  }
598  CoverageNamesVar->eraseFromParent();
599 }
600 
601 /// Get the name of a profiling variable for a particular function.
603  StringRef NamePrefix = getInstrProfNameVarPrefix();
604  StringRef Name = Inc->getName()->getName().substr(NamePrefix.size());
605  Function *F = Inc->getParent()->getParent();
606  Module *M = F->getParent();
607  if (!DoHashBasedCounterSplit || !isIRPGOFlagSet(M) ||
608  !canRenameComdatFunc(*F))
609  return (Prefix + Name).str();
610  uint64_t FuncHash = Inc->getHash()->getZExtValue();
611  SmallVector<char, 24> HashPostfix;
612  if (Name.endswith((Twine(".") + Twine(FuncHash)).toStringRef(HashPostfix)))
613  return (Prefix + Name).str();
614  return (Prefix + Name + "." + Twine(FuncHash)).str();
615 }
616 
617 static inline bool shouldRecordFunctionAddr(Function *F) {
618  // Check the linkage
619  bool HasAvailableExternallyLinkage = F->hasAvailableExternallyLinkage();
620  if (!F->hasLinkOnceLinkage() && !F->hasLocalLinkage() &&
621  !HasAvailableExternallyLinkage)
622  return true;
623 
624  // A function marked 'alwaysinline' with available_externally linkage can't
625  // have its address taken. Doing so would create an undefined external ref to
626  // the function, which would fail to link.
627  if (HasAvailableExternallyLinkage &&
628  F->hasFnAttribute(Attribute::AlwaysInline))
629  return false;
630 
631  // Prohibit function address recording if the function is both internal and
632  // COMDAT. This avoids the profile data variable referencing internal symbols
633  // in COMDAT.
634  if (F->hasLocalLinkage() && F->hasComdat())
635  return false;
636 
637  // Check uses of this function for other than direct calls or invokes to it.
638  // Inline virtual functions have linkeOnceODR linkage. When a key method
639  // exists, the vtable will only be emitted in the TU where the key method
640  // is defined. In a TU where vtable is not available, the function won't
641  // be 'addresstaken'. If its address is not recorded here, the profile data
642  // with missing address may be picked by the linker leading to missing
643  // indirect call target info.
644  return F->hasAddressTaken() || F->hasLinkOnceLinkage();
645 }
646 
648  InstrProfIncrementInst *Inc) {
649  if (!needsComdatForCounter(F, M))
650  return nullptr;
651 
652  // COFF format requires a COMDAT section to have a key symbol with the same
653  // name. The linker targeting COFF also requires that the COMDAT
654  // a section is associated to must precede the associating section. For this
655  // reason, we must choose the counter var's name as the name of the comdat.
656  StringRef ComdatPrefix = (Triple(M.getTargetTriple()).isOSBinFormatCOFF()
659  return M.getOrInsertComdat(StringRef(getVarName(Inc, ComdatPrefix)));
660 }
661 
663  // Don't do this for Darwin. compiler-rt uses linker magic.
664  if (Triple(M.getTargetTriple()).isOSDarwin())
665  return false;
666 
667  // Use linker script magic to get data/cnts/name start/end.
668  if (Triple(M.getTargetTriple()).isOSLinux() ||
669  Triple(M.getTargetTriple()).isOSFreeBSD() ||
670  Triple(M.getTargetTriple()).isPS4CPU())
671  return false;
672 
673  return true;
674 }
675 
677 InstrProfiling::getOrCreateRegionCounters(InstrProfIncrementInst *Inc) {
678  GlobalVariable *NamePtr = Inc->getName();
679  auto It = ProfileDataMap.find(NamePtr);
680  PerFunctionProfileData PD;
681  if (It != ProfileDataMap.end()) {
682  if (It->second.RegionCounters)
683  return It->second.RegionCounters;
684  PD = It->second;
685  }
686 
687  // Move the name variable to the right section. Place them in a COMDAT group
688  // if the associated function is a COMDAT. This will make sure that
689  // only one copy of counters of the COMDAT function will be emitted after
690  // linking.
691  Function *Fn = Inc->getParent()->getParent();
692  Comdat *ProfileVarsComdat = nullptr;
693  ProfileVarsComdat = getOrCreateProfileComdat(*M, *Fn, Inc);
694 
695  uint64_t NumCounters = Inc->getNumCounters()->getZExtValue();
696  LLVMContext &Ctx = M->getContext();
697  ArrayType *CounterTy = ArrayType::get(Type::getInt64Ty(Ctx), NumCounters);
698 
699  // Create the counters variable.
700  auto *CounterPtr =
701  new GlobalVariable(*M, CounterTy, false, NamePtr->getLinkage(),
702  Constant::getNullValue(CounterTy),
704  CounterPtr->setVisibility(NamePtr->getVisibility());
705  CounterPtr->setSection(
706  getInstrProfSectionName(IPSK_cnts, TT.getObjectFormat()));
707  CounterPtr->setAlignment(8);
708  CounterPtr->setComdat(ProfileVarsComdat);
709 
710  auto *Int8PtrTy = Type::getInt8PtrTy(Ctx);
711  // Allocate statically the array of pointers to value profile nodes for
712  // the current function.
713  Constant *ValuesPtrExpr = ConstantPointerNull::get(Int8PtrTy);
714  if (ValueProfileStaticAlloc && !needsRuntimeRegistrationOfSectionRange(*M)) {
715  uint64_t NS = 0;
716  for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
717  NS += PD.NumValueSites[Kind];
718  if (NS) {
719  ArrayType *ValuesTy = ArrayType::get(Type::getInt64Ty(Ctx), NS);
720 
721  auto *ValuesVar =
722  new GlobalVariable(*M, ValuesTy, false, NamePtr->getLinkage(),
723  Constant::getNullValue(ValuesTy),
725  ValuesVar->setVisibility(NamePtr->getVisibility());
726  ValuesVar->setSection(
727  getInstrProfSectionName(IPSK_vals, TT.getObjectFormat()));
728  ValuesVar->setAlignment(8);
729  ValuesVar->setComdat(ProfileVarsComdat);
730  ValuesPtrExpr =
732  }
733  }
734 
735  // Create data variable.
736  auto *Int16Ty = Type::getInt16Ty(Ctx);
737  auto *Int16ArrayTy = ArrayType::get(Int16Ty, IPVK_Last + 1);
738  Type *DataTypes[] = {
739 #define INSTR_PROF_DATA(Type, LLVMType, Name, Init) LLVMType,
741  };
742  auto *DataTy = StructType::get(Ctx, makeArrayRef(DataTypes));
743 
744  Constant *FunctionAddr = shouldRecordFunctionAddr(Fn)
745  ? ConstantExpr::getBitCast(Fn, Int8PtrTy)
746  : ConstantPointerNull::get(Int8PtrTy);
747 
748  Constant *Int16ArrayVals[IPVK_Last + 1];
749  for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
750  Int16ArrayVals[Kind] = ConstantInt::get(Int16Ty, PD.NumValueSites[Kind]);
751 
752  Constant *DataVals[] = {
753 #define INSTR_PROF_DATA(Type, LLVMType, Name, Init) Init,
755  };
756  auto *Data = new GlobalVariable(*M, DataTy, false, NamePtr->getLinkage(),
757  ConstantStruct::get(DataTy, DataVals),
759  Data->setVisibility(NamePtr->getVisibility());
760  Data->setSection(getInstrProfSectionName(IPSK_data, TT.getObjectFormat()));
761  Data->setAlignment(INSTR_PROF_DATA_ALIGNMENT);
762  Data->setComdat(ProfileVarsComdat);
763 
764  PD.RegionCounters = CounterPtr;
765  PD.DataVar = Data;
766  ProfileDataMap[NamePtr] = PD;
767 
768  // Mark the data variable as used so that it isn't stripped out.
769  UsedVars.push_back(Data);
770  // Now that the linkage set by the FE has been passed to the data and counter
771  // variables, reset Name variable's linkage and visibility to private so that
772  // it can be removed later by the compiler.
774  // Collect the referenced names to be used by emitNameData.
775  ReferencedNames.push_back(NamePtr);
776 
777  return CounterPtr;
778 }
779 
780 void InstrProfiling::emitVNodes() {
781  if (!ValueProfileStaticAlloc)
782  return;
783 
784  // For now only support this on platforms that do
785  // not require runtime registration to discover
786  // named section start/end.
788  return;
789 
790  size_t TotalNS = 0;
791  for (auto &PD : ProfileDataMap) {
792  for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
793  TotalNS += PD.second.NumValueSites[Kind];
794  }
795 
796  if (!TotalNS)
797  return;
798 
799  uint64_t NumCounters = TotalNS * NumCountersPerValueSite;
800 // Heuristic for small programs with very few total value sites.
801 // The default value of vp-counters-per-site is chosen based on
802 // the observation that large apps usually have a low percentage
803 // of value sites that actually have any profile data, and thus
804 // the average number of counters per site is low. For small
805 // apps with very few sites, this may not be true. Bump up the
806 // number of counters in this case.
807 #define INSTR_PROF_MIN_VAL_COUNTS 10
808  if (NumCounters < INSTR_PROF_MIN_VAL_COUNTS)
809  NumCounters = std::max(INSTR_PROF_MIN_VAL_COUNTS, (int)NumCounters * 2);
810 
811  auto &Ctx = M->getContext();
812  Type *VNodeTypes[] = {
813 #define INSTR_PROF_VALUE_NODE(Type, LLVMType, Name, Init) LLVMType,
815  };
816  auto *VNodeTy = StructType::get(Ctx, makeArrayRef(VNodeTypes));
817 
818  ArrayType *VNodesTy = ArrayType::get(VNodeTy, NumCounters);
819  auto *VNodesVar = new GlobalVariable(
820  *M, VNodesTy, false, GlobalValue::PrivateLinkage,
822  VNodesVar->setSection(
823  getInstrProfSectionName(IPSK_vnodes, TT.getObjectFormat()));
824  UsedVars.push_back(VNodesVar);
825 }
826 
827 void InstrProfiling::emitNameData() {
828  std::string UncompressedData;
829 
830  if (ReferencedNames.empty())
831  return;
832 
833  std::string CompressedNameStr;
834  if (Error E = collectPGOFuncNameStrings(ReferencedNames, CompressedNameStr,
835  DoNameCompression)) {
836  report_fatal_error(toString(std::move(E)), false);
837  }
838 
839  auto &Ctx = M->getContext();
840  auto *NamesVal = ConstantDataArray::getString(
841  Ctx, StringRef(CompressedNameStr), false);
842  NamesVar = new GlobalVariable(*M, NamesVal->getType(), true,
843  GlobalValue::PrivateLinkage, NamesVal,
845  NamesSize = CompressedNameStr.size();
846  NamesVar->setSection(
847  getInstrProfSectionName(IPSK_name, TT.getObjectFormat()));
848  UsedVars.push_back(NamesVar);
849 
850  for (auto *NamePtr : ReferencedNames)
851  NamePtr->eraseFromParent();
852 }
853 
854 void InstrProfiling::emitRegistration() {
856  return;
857 
858  // Construct the function.
859  auto *VoidTy = Type::getVoidTy(M->getContext());
860  auto *VoidPtrTy = Type::getInt8PtrTy(M->getContext());
861  auto *Int64Ty = Type::getInt64Ty(M->getContext());
862  auto *RegisterFTy = FunctionType::get(VoidTy, false);
863  auto *RegisterF = Function::Create(RegisterFTy, GlobalValue::InternalLinkage,
865  RegisterF->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
866  if (Options.NoRedZone)
867  RegisterF->addFnAttr(Attribute::NoRedZone);
868 
869  auto *RuntimeRegisterTy = FunctionType::get(VoidTy, VoidPtrTy, false);
870  auto *RuntimeRegisterF =
873 
874  IRBuilder<> IRB(BasicBlock::Create(M->getContext(), "", RegisterF));
875  for (Value *Data : UsedVars)
876  if (Data != NamesVar)
877  IRB.CreateCall(RuntimeRegisterF, IRB.CreateBitCast(Data, VoidPtrTy));
878 
879  if (NamesVar) {
880  Type *ParamTypes[] = {VoidPtrTy, Int64Ty};
881  auto *NamesRegisterTy =
882  FunctionType::get(VoidTy, makeArrayRef(ParamTypes), false);
883  auto *NamesRegisterF =
886  IRB.CreateCall(NamesRegisterF, {IRB.CreateBitCast(NamesVar, VoidPtrTy),
887  IRB.getInt64(NamesSize)});
888  }
889 
890  IRB.CreateRetVoid();
891 }
892 
893 void InstrProfiling::emitRuntimeHook() {
894  // We expect the linker to be invoked with -u<hook_var> flag for linux,
895  // for which case there is no need to emit the user function.
896  if (Triple(M->getTargetTriple()).isOSLinux())
897  return;
898 
899  // If the module's provided its own runtime, we don't need to do anything.
901  return;
902 
903  // Declare an external variable that will pull in the runtime initialization.
904  auto *Int32Ty = Type::getInt32Ty(M->getContext());
905  auto *Var =
907  nullptr, getInstrProfRuntimeHookVarName());
908 
909  // Make a function that uses it.
913  User->addFnAttr(Attribute::NoInline);
914  if (Options.NoRedZone)
915  User->addFnAttr(Attribute::NoRedZone);
916  User->setVisibility(GlobalValue::HiddenVisibility);
917  if (Triple(M->getTargetTriple()).supportsCOMDAT())
918  User->setComdat(M->getOrInsertComdat(User->getName()));
919 
921  auto *Load = IRB.CreateLoad(Var);
922  IRB.CreateRet(Load);
923 
924  // Mark the user variable as used so that it isn't stripped out.
925  UsedVars.push_back(User);
926 }
927 
928 void InstrProfiling::emitUses() {
929  if (!UsedVars.empty())
930  appendToUsed(*M, UsedVars);
931 }
932 
933 void InstrProfiling::emitInitialization() {
934  StringRef InstrProfileOutput = Options.InstrProfileOutput;
935 
936  if (!InstrProfileOutput.empty()) {
937  // Create variable for profile name.
938  Constant *ProfileNameConst =
939  ConstantDataArray::getString(M->getContext(), InstrProfileOutput, true);
940  GlobalVariable *ProfileNameVar = new GlobalVariable(
941  *M, ProfileNameConst->getType(), true, GlobalValue::WeakAnyLinkage,
942  ProfileNameConst, INSTR_PROF_QUOTE(INSTR_PROF_PROFILE_NAME_VAR));
943  if (TT.supportsCOMDAT()) {
944  ProfileNameVar->setLinkage(GlobalValue::ExternalLinkage);
945  ProfileNameVar->setComdat(M->getOrInsertComdat(
946  StringRef(INSTR_PROF_QUOTE(INSTR_PROF_PROFILE_NAME_VAR))));
947  }
948  }
949 
950  Constant *RegisterF = M->getFunction(getInstrProfRegFuncsName());
951  if (!RegisterF)
952  return;
953 
954  // Create the initialization function.
955  auto *VoidTy = Type::getVoidTy(M->getContext());
956  auto *F = Function::Create(FunctionType::get(VoidTy, false),
959  F->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
960  F->addFnAttr(Attribute::NoInline);
961  if (Options.NoRedZone)
962  F->addFnAttr(Attribute::NoRedZone);
963 
964  // Add the basic block and the necessary calls.
965  IRBuilder<> IRB(BasicBlock::Create(M->getContext(), "", F));
966  if (RegisterF)
967  IRB.CreateCall(RegisterF, {});
968  IRB.CreateRetVoid();
969 
970  appendToGlobalCtors(*M, F, 0);
971 }
void setVisibility(VisibilityTypes V)
Definition: GlobalValue.h:231
cl::opt< std::string > MemOPSizeRange("memop-size-range", cl::desc("Set the range of size in memory intrinsic calls to be profiled " "precisely, in a format of <start_val>:<end_val>"), cl::init(""))
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks &#39;this&#39; from the containing basic block and deletes it.
Definition: Instruction.cpp:69
StringRef getInstrProfRuntimeHookVarUseFuncName()
Return the name of the compiler generated function that references the runtime hook variable...
Definition: InstrProf.h:157
const std::string & getTargetTriple() const
Get the target triple which is a string describing the target host.
Definition: Module.h:233
static Constant * getString(LLVMContext &Context, StringRef Initializer, bool AddNull=true)
This method constructs a CDS and initializes it with a text string.
Definition: Constants.cpp:2451
bool hasLocalLinkage() const
Definition: GlobalValue.h:427
Helper class for SSA formation on a set of values defined in multiple blocks.
Definition: SSAUpdater.h:39
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
ConstantInt * getIndex() const
void dropAllReferences()
Drop all references to operands.
Definition: User.h:279
StringRef getInstrProfNameVarPrefix()
Return the name prefix of variables containing instrumented function names.
Definition: InstrProf.h:83
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:687
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:115
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
cl::opt< unsigned > MemOPSizeLarge("memop-size-large", cl::desc("Set large value thresthold in memory intrinsic size profiling. " "Value of 0 disables the large value profiling."), cl::init(8192))
Constant * getOrInsertFunction(StringRef Name, FunctionType *T, AttributeList AttributeList)
Look up the specified function in the module symbol table.
Definition: Module.cpp:142
StringRef getInstrProfRegFuncsName()
Return the name of function that registers all the per-function control data at program startup time ...
Definition: InstrProf.h:127
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:136
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:138
#define INSTR_PROF_MIN_VAL_COUNTS
This class represents a function call, abstracting a target machine&#39;s calling convention.
bool hasAvailableExternallyLinkage() const
Definition: GlobalValue.h:414
bool canRenameComdatFunc(const Function &F, bool CheckAddressTaken=false)
Check if we can safely rename this Comdat function.
Definition: InstrProf.cpp:971
BlockT * getLoopPreheader() const
If there is a preheader for this loop, return it.
Definition: LoopInfoImpl.h:106
Like Internal, but omit from symbol table.
Definition: GlobalValue.h:57
Externally visible function.
Definition: GlobalValue.h:49
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.h:262
GlobalVariable * getGlobalVariable(StringRef Name) const
Look up the specified global variable in the module symbol table.
Definition: Module.h:368
This file provides the interface for LLVM&#39;s PGO Instrumentation lowering pass.
std::string getInstrProfSectionName(InstrProfSectKind IPSK, Triple::ObjectFormatType OF, bool AddSegmentInfo=true)
Return the name of the profile section corresponding to IPSK.
Definition: InstrProf.cpp:165
F(f)
An instruction for reading from memory.
Definition: Instructions.h:164
static IntegerType * getInt64Ty(LLVMContext &C)
Definition: Type.cpp:177
ModulePass * createInstrProfilingLegacyPass(const InstrProfOptions &Options=InstrProfOptions())
Insert frontend instrumentation based profiling.
static IntegerType * getInt16Ty(LLVMContext &C)
Definition: Type.cpp:175
StringRef getInstrProfValuesVarPrefix()
Return the name prefix of value profile variables.
Definition: InstrProf.h:92
static Constant * getNullValue(Type *Ty)
Constructor to create a &#39;0&#39; constant of arbitrary type.
Definition: Constants.cpp:207
Attribute::AttrKind getExtAttrForI32Param(bool Signed=true) const
Returns extension attribute kind to be used for i32 parameters corresponding to C-level int or unsign...
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool endswith(StringRef Suffix) const
Check if this string ends with the given Suffix.
Definition: StringRef.h:279
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:51
Hexagon Hardware Loops
StringRef getInstrProfInitFuncName()
Return the name of the runtime initialization method that is generated by the compiler.
Definition: InstrProf.h:146
std::string toString(Error E)
Write all error messages (if any) in E to a string.
Definition: Error.h:947
static Value * getPointerOperand(Instruction &Inst)
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
Definition: LoopInfo.h:678
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:451
StringRef getInstrProfDataVarPrefix()
Return the name prefix of variables containing per-function control data.
Definition: InstrProf.h:86
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:237
Instrumentation based profiling lowering pass.
ReturnInst * CreateRet(Value *V)
Create a &#39;ret <val>&#39; instruction.
Definition: IRBuilder.h:754
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:668
This file contains the simple types necessary to represent the attributes associated with functions a...
Error collectPGOFuncNameStrings(ArrayRef< std::string > NameStrs, bool doCompression, std::string &Result)
Given a vector of strings (function PGO names) NameStrs, the method generates a combined string Resul...
Definition: InstrProf.cpp:363
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:893
StringRef getCoverageUnusedNamesVarName()
Return the name of the internal variable recording the array of PGO name vars referenced by the cover...
Definition: InstrProf.h:118
static StructType * get(LLVMContext &Context, ArrayRef< Type *> Elements, bool isPacked=false)
This static method is the primary way to create a literal StructType.
Definition: Type.cpp:336
StringRef getInstrProfNamesRegFuncName()
Return the name of the runtime interface that registers the PGO name strings.
Definition: InstrProf.h:138
StoreInst * CreateStore(Value *Val, Value *Ptr, bool isVolatile=false)
Definition: IRBuilder.h:1180
void getExitBlocks(SmallVectorImpl< BlockT *> &ExitBlocks) const
Return all of the successor blocks of this loop.
Definition: LoopInfoImpl.h:63
bool hasDedicatedExits() const
Return true if no exit block for the loop has a predecessor that is outside the loop.
Definition: LoopInfo.cpp:380
auto reverse(ContainerTy &&C, typename std::enable_if< has_rbegin< ContainerTy >::value >::type *=nullptr) -> decltype(make_range(C.rbegin(), C.rend()))
Definition: STLExtras.h:232
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1448
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:245
void appendToUsed(Module &M, ArrayRef< GlobalValue *> Values)
Adds global values to the llvm.used list.
Class to represent array types.
Definition: DerivedTypes.h:369
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
Memory SSA
Definition: MemorySSA.cpp:65
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
LinkageTypes getLinkage() const
Definition: GlobalValue.h:441
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:430
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:598
bool hasLinkOnceLinkage() const
Definition: GlobalValue.h:417
GlobalVariable * getName() const
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:140
Value * getOperand(unsigned i) const
Definition: User.h:154
ConstantInt * getNumCounters() const
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:156
bool isIRPGOFlagSet(const Module *M)
Check if INSTR_PROF_RAW_VERSION_VAR is defined.
Definition: InstrProf.cpp:951
static std::string getVarName(InstrProfIncrementInst *Inc, StringRef Prefix)
Get the name of a profiling variable for a particular function.
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:1678
LoadInst * CreateLoad(Value *Ptr, const char *Name)
Definition: IRBuilder.h:1168
void getMemOPSizeRangeFromOption(StringRef Str, int64_t &RangeStart, int64_t &RangeLast)
Definition: InstrProf.cpp:994
bool needsComdatForCounter(const Function &F, const Module &M)
Check if we can use Comdat for profile variables.
Definition: InstrProf.cpp:924
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:52
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:406
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition: Constants.h:149
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:153
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
Definition: BasicBlock.cpp:200
static ConstantPointerNull * get(PointerType *T)
Static factory methods - Return objects of the specified value.
Definition: Constants.cpp:1306
VisibilityTypes getVisibility() const
Definition: GlobalValue.h:225
ConstantInt * getIndex() const
static Constant * getOrInsertValueProfilingCall(Module &M, const TargetLibraryInfo &TLI, bool IsRange=false)
LLVM Basic Block Representation.
Definition: BasicBlock.h:59
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:69
StringRef getInstrProfValueProfFuncName()
Return the name profile runtime entry point to do value profiling for a given site.
Definition: InstrProf.h:73
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This is an important base class in LLVM.
Definition: Constant.h:42
ValueKind
Value kinds.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:371
StringRef getInstrProfNamesVarName()
Return the name of the variable holding the strings (possibly compressed) of all function&#39;s PGO names...
Definition: InstrProf.h:103
void eraseFromParent()
eraseFromParent - This method unlinks &#39;this&#39; from the containing module and deletes it...
Definition: Globals.cpp:336
Represent the analysis usage information of a pass.
static Type * getVoidTy(LLVMContext &C)
Definition: Type.cpp:161
bool any_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:820
static bool shouldRecordFunctionAddr(Function *F)
static FunctionType * get(Type *Result, ArrayRef< Type *> Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
Definition: Type.cpp:297
static Constant * get(StructType *T, ArrayRef< Constant *> V)
Definition: Constants.cpp:949
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition: BasicBlock.h:101
ConstantInt * getInt64(uint64_t C)
Get a constant 64-bit value.
Definition: IRBuilder.h:313
void getExitingBlocks(SmallVectorImpl< BlockT *> &ExitingBlocks) const
Return all blocks inside the loop that have successors outside of the loop.
Definition: LoopInfoImpl.h:35
StringRef toStringRef(bool B)
Construct a string ref from a boolean.
Definition: StringExtras.h:43
const Constant * stripPointerCasts() const
Definition: Constant.h:153
Comdat * getOrInsertComdat(StringRef Name)
Return the Comdat in the module with the specified name.
Definition: Module.cpp:467
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:159
static bool needsRuntimeRegistrationOfSectionRange(const Module &M)
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:220
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
StringRef getInstrProfCountersVarPrefix()
Return the name prefix of profile counter variables.
Definition: InstrProf.h:89
unsigned getNumOperands() const
Definition: User.h:176
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:418
Helper class for promoting a collection of loads and stores into SSA Form using the SSAUpdater...
Definition: SSAUpdater.h:133
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:864
Module.h This file contains the declarations for the Module class.
Provides information about what library functions are available for the current target.
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:560
void appendToGlobalCtors(Module &M, Function *F, int Priority, Constant *Data=nullptr)
Append F to the list of global ctors of module M with the given Priority.
Definition: ModuleUtils.cpp:84
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:285
#define NC
Definition: regutils.h:42
void setLinkage(LinkageTypes LT)
Definition: GlobalValue.h:436
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
Definition: Module.cpp:172
ConstantArray - Constant Array Declarations.
Definition: Constants.h:405
StringRef getInstrProfRuntimeHookVarName()
Return the name of the hook variable defined in profile runtime library.
Definition: InstrProf.h:151
StringRef getInstrProfRegFuncName()
Return the name of the runtime interface that registers per-function control data for one instrumente...
Definition: InstrProf.h:133
Options for the frontend instrumentation based profiling pass.
bool hasComdat() const
Definition: GlobalObject.h:100
Value * CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1, const Twine &Name="")
Definition: IRBuilder.h:1352
This represents the llvm.instrprof_increment intrinsic.
ReturnInst * CreateRetVoid()
Create a &#39;ret void&#39; instruction.
Definition: IRBuilder.h:749
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:176
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:439
ConstantInt * getHash() const
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:220
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:108
#define I(x, y, z)
Definition: MD5.cpp:58
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:225
static ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
Definition: Type.cpp:568
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:323
Keep one copy of named function when linking (weak)
Definition: GlobalValue.h:53
Rename collisions when linking (static functions).
Definition: GlobalValue.h:56
SmallVector< LoopT *, 4 > getLoopsInPreorder()
Return all of the loops in the function in preorder across the loop nests, with siblings in forward p...
Definition: LoopInfoImpl.h:512
Analysis pass providing the TargetLibraryInfo.
const unsigned Kind
bool hasAddressTaken(const User **=nullptr) const
hasAddressTaken - returns true if there are any uses of this function other than direct calls or invo...
Definition: Function.cpp:1213
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
StringRef getInstrProfValueRangeProfFuncName()
Return the name profile runtime entry point to do value range profiling.
Definition: InstrProf.h:78
aarch64 promote const
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:556
LLVM Value Representation.
Definition: Value.h:73
StringRef getInstrProfVNodesVarName()
Return the name of value profile node array variables:
Definition: InstrProf.h:95
GlobalVariable * getName() const
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
ConstantInt * getValueKind() const
#define DEBUG(X)
Definition: Debug.h:118
static Comdat * getOrCreateProfileComdat(Module &M, Function &F, InstrProfIncrementInst *Inc)
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
A container for analyses that lazily runs them and caches their results.
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, const Twine &N="", Module *M=nullptr)
Definition: Function.h:136
StringRef getInstrProfComdatPrefix()
Return the name prefix of the COMDAT group for instrumentation variables associated with a COMDAT fun...
Definition: InstrProf.h:99
const TerminatorInst * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.cpp:120
static bool lowerIntrinsics(Module &M)
This represents the llvm.instrprof_value_profile intrinsic.
static InstrProfIncrementInst * castToIncrementInst(Instruction *Instr)
void setSection(StringRef S)
Change the section for this global.
Definition: Globals.cpp:184
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
IntegerType * Int32Ty
INITIALIZE_PASS_BEGIN(InstrProfilingLegacyPass, "instrprof", "Frontend instrumentation-based coverage lowering.", false, false) INITIALIZE_PASS_END(InstrProfilingLegacyPass
const BasicBlock * getParent() const
Definition: Instruction.h:66
CallInst * CreateCall(Value *Callee, ArrayRef< Value *> Args=None, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1663
void addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind)
Adds the attribute to the indicated argument.