LLVM  7.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"
48 #include <algorithm>
49 #include <cassert>
50 #include <cstddef>
51 #include <cstdint>
52 #include <string>
53 
54 using namespace llvm;
55 
56 #define DEBUG_TYPE "instrprof"
57 
58 // The start and end values of precise value profile range for memory
59 // intrinsic sizes
61  "memop-size-range",
62  cl::desc("Set the range of size in memory intrinsic calls to be profiled "
63  "precisely, in a format of <start_val>:<end_val>"),
64  cl::init(""));
65 
66 // The value that considered to be large value in memory intrinsic.
68  "memop-size-large",
69  cl::desc("Set large value thresthold in memory intrinsic size profiling. "
70  "Value of 0 disables the large value profiling."),
71  cl::init(8192));
72 
73 namespace {
74 
75 cl::opt<bool> DoNameCompression("enable-name-compression",
76  cl::desc("Enable name string compression"),
77  cl::init(true));
78 
79 cl::opt<bool> DoHashBasedCounterSplit(
80  "hash-based-counter-split",
81  cl::desc("Rename counter variable of a comdat function based on cfg hash"),
82  cl::init(true));
83 
84 cl::opt<bool> ValueProfileStaticAlloc(
85  "vp-static-alloc",
86  cl::desc("Do static counter allocation for value profiler"),
87  cl::init(true));
88 
89 cl::opt<double> NumCountersPerValueSite(
90  "vp-counters-per-site",
91  cl::desc("The average number of profile counters allocated "
92  "per value profiling site."),
93  // This is set to a very small value because in real programs, only
94  // a very small percentage of value sites have non-zero targets, e.g, 1/30.
95  // For those sites with non-zero profile, the average number of targets
96  // is usually smaller than 2.
97  cl::init(1.0));
98 
99 cl::opt<bool> AtomicCounterUpdatePromoted(
100  "atomic-counter-update-promoted", cl::ZeroOrMore,
101  cl::desc("Do counter update using atomic fetch add "
102  " for promoted counters only"),
103  cl::init(false));
104 
105 // If the option is not specified, the default behavior about whether
106 // counter promotion is done depends on how instrumentaiton lowering
107 // pipeline is setup, i.e., the default value of true of this option
108 // does not mean the promotion will be done by default. Explicitly
109 // setting this option can override the default behavior.
110 cl::opt<bool> DoCounterPromotion("do-counter-promotion", cl::ZeroOrMore,
111  cl::desc("Do counter register promotion"),
112  cl::init(false));
113 cl::opt<unsigned> MaxNumOfPromotionsPerLoop(
114  cl::ZeroOrMore, "max-counter-promotions-per-loop", cl::init(20),
115  cl::desc("Max number counter promotions per loop to avoid"
116  " increasing register pressure too much"));
117 
118 // A debug option
120  MaxNumOfPromotions(cl::ZeroOrMore, "max-counter-promotions", cl::init(-1),
121  cl::desc("Max number of allowed counter promotions"));
122 
123 cl::opt<unsigned> SpeculativeCounterPromotionMaxExiting(
124  cl::ZeroOrMore, "speculative-counter-promotion-max-exiting", cl::init(3),
125  cl::desc("The max number of exiting blocks of a loop to allow "
126  " speculative counter promotion"));
127 
128 cl::opt<bool> SpeculativeCounterPromotionToLoop(
129  cl::ZeroOrMore, "speculative-counter-promotion-to-loop", cl::init(false),
130  cl::desc("When the option is false, if the target block is in a loop, "
131  "the promotion will be disallowed unless the promoted counter "
132  " update can be further/iteratively promoted into an acyclic "
133  " region."));
134 
135 cl::opt<bool> IterativeCounterPromotion(
136  cl::ZeroOrMore, "iterative-counter-promotion", cl::init(true),
137  cl::desc("Allow counter promotion across the whole loop nest."));
138 
139 class InstrProfilingLegacyPass : public ModulePass {
140  InstrProfiling InstrProf;
141 
142 public:
143  static char ID;
144 
145  InstrProfilingLegacyPass() : ModulePass(ID) {}
146  InstrProfilingLegacyPass(const InstrProfOptions &Options)
147  : ModulePass(ID), InstrProf(Options) {}
148 
149  StringRef getPassName() const override {
150  return "Frontend instrumentation-based coverage lowering";
151  }
152 
153  bool runOnModule(Module &M) override {
154  return InstrProf.run(M, getAnalysis<TargetLibraryInfoWrapperPass>().getTLI());
155  }
156 
157  void getAnalysisUsage(AnalysisUsage &AU) const override {
158  AU.setPreservesCFG();
160  }
161 };
162 
163 ///
164 /// A helper class to promote one counter RMW operation in the loop
165 /// into register update.
166 ///
167 /// RWM update for the counter will be sinked out of the loop after
168 /// the transformation.
169 ///
170 class PGOCounterPromoterHelper : public LoadAndStorePromoter {
171 public:
172  PGOCounterPromoterHelper(
174  BasicBlock *PH, ArrayRef<BasicBlock *> ExitBlocks,
175  ArrayRef<Instruction *> InsertPts,
177  LoopInfo &LI)
178  : LoadAndStorePromoter({L, S}, SSA), Store(S), ExitBlocks(ExitBlocks),
179  InsertPts(InsertPts), LoopToCandidates(LoopToCands), LI(LI) {
180  assert(isa<LoadInst>(L));
181  assert(isa<StoreInst>(S));
182  SSA.AddAvailableValue(PH, Init);
183  }
184 
185  void doExtraRewritesBeforeFinalDeletion() const override {
186  for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) {
187  BasicBlock *ExitBlock = ExitBlocks[i];
188  Instruction *InsertPos = InsertPts[i];
189  // Get LiveIn value into the ExitBlock. If there are multiple
190  // predecessors, the value is defined by a PHI node in this
191  // block.
192  Value *LiveInValue = SSA.GetValueInMiddleOfBlock(ExitBlock);
193  Value *Addr = cast<StoreInst>(Store)->getPointerOperand();
194  IRBuilder<> Builder(InsertPos);
195  if (AtomicCounterUpdatePromoted)
196  // automic update currently can only be promoted across the current
197  // loop, not the whole loop nest.
198  Builder.CreateAtomicRMW(AtomicRMWInst::Add, Addr, LiveInValue,
200  else {
201  LoadInst *OldVal = Builder.CreateLoad(Addr, "pgocount.promoted");
202  auto *NewVal = Builder.CreateAdd(OldVal, LiveInValue);
203  auto *NewStore = Builder.CreateStore(NewVal, Addr);
204 
205  // Now update the parent loop's candidate list:
206  if (IterativeCounterPromotion) {
207  auto *TargetLoop = LI.getLoopFor(ExitBlock);
208  if (TargetLoop)
209  LoopToCandidates[TargetLoop].emplace_back(OldVal, NewStore);
210  }
211  }
212  }
213  }
214 
215 private:
217  ArrayRef<BasicBlock *> ExitBlocks;
218  ArrayRef<Instruction *> InsertPts;
220  LoopInfo &LI;
221 };
222 
223 /// A helper class to do register promotion for all profile counter
224 /// updates in a loop.
225 ///
226 class PGOCounterPromoter {
227 public:
228  PGOCounterPromoter(
230  Loop &CurLoop, LoopInfo &LI)
231  : LoopToCandidates(LoopToCands), ExitBlocks(), InsertPts(), L(CurLoop),
232  LI(LI) {
233 
234  SmallVector<BasicBlock *, 8> LoopExitBlocks;
236  L.getExitBlocks(LoopExitBlocks);
237 
238  for (BasicBlock *ExitBlock : LoopExitBlocks) {
239  if (BlockSet.insert(ExitBlock).second) {
240  ExitBlocks.push_back(ExitBlock);
241  InsertPts.push_back(&*ExitBlock->getFirstInsertionPt());
242  }
243  }
244  }
245 
246  bool run(int64_t *NumPromoted) {
247  // Skip 'infinite' loops:
248  if (ExitBlocks.size() == 0)
249  return false;
250  unsigned MaxProm = getMaxNumOfPromotionsInLoop(&L);
251  if (MaxProm == 0)
252  return false;
253 
254  unsigned Promoted = 0;
255  for (auto &Cand : LoopToCandidates[&L]) {
256 
258  SSAUpdater SSA(&NewPHIs);
259  Value *InitVal = ConstantInt::get(Cand.first->getType(), 0);
260 
261  PGOCounterPromoterHelper Promoter(Cand.first, Cand.second, SSA, InitVal,
262  L.getLoopPreheader(), ExitBlocks,
263  InsertPts, LoopToCandidates, LI);
264  Promoter.run(SmallVector<Instruction *, 2>({Cand.first, Cand.second}));
265  Promoted++;
266  if (Promoted >= MaxProm)
267  break;
268 
269  (*NumPromoted)++;
270  if (MaxNumOfPromotions != -1 && *NumPromoted >= MaxNumOfPromotions)
271  break;
272  }
273 
274  LLVM_DEBUG(dbgs() << Promoted << " counters promoted for loop (depth="
275  << L.getLoopDepth() << ")\n");
276  return Promoted != 0;
277  }
278 
279 private:
280  bool allowSpeculativeCounterPromotion(Loop *LP) {
281  SmallVector<BasicBlock *, 8> ExitingBlocks;
282  L.getExitingBlocks(ExitingBlocks);
283  // Not considierered speculative.
284  if (ExitingBlocks.size() == 1)
285  return true;
286  if (ExitingBlocks.size() > SpeculativeCounterPromotionMaxExiting)
287  return false;
288  return true;
289  }
290 
291  // Returns the max number of Counter Promotions for LP.
292  unsigned getMaxNumOfPromotionsInLoop(Loop *LP) {
293  // We can't insert into a catchswitch.
294  SmallVector<BasicBlock *, 8> LoopExitBlocks;
295  LP->getExitBlocks(LoopExitBlocks);
296  if (llvm::any_of(LoopExitBlocks, [](BasicBlock *Exit) {
297  return isa<CatchSwitchInst>(Exit->getTerminator());
298  }))
299  return 0;
300 
301  if (!LP->hasDedicatedExits())
302  return 0;
303 
304  BasicBlock *PH = LP->getLoopPreheader();
305  if (!PH)
306  return 0;
307 
308  SmallVector<BasicBlock *, 8> ExitingBlocks;
309  LP->getExitingBlocks(ExitingBlocks);
310  // Not considierered speculative.
311  if (ExitingBlocks.size() == 1)
312  return MaxNumOfPromotionsPerLoop;
313 
314  if (ExitingBlocks.size() > SpeculativeCounterPromotionMaxExiting)
315  return 0;
316 
317  // Whether the target block is in a loop does not matter:
318  if (SpeculativeCounterPromotionToLoop)
319  return MaxNumOfPromotionsPerLoop;
320 
321  // Now check the target block:
322  unsigned MaxProm = MaxNumOfPromotionsPerLoop;
323  for (auto *TargetBlock : LoopExitBlocks) {
324  auto *TargetLoop = LI.getLoopFor(TargetBlock);
325  if (!TargetLoop)
326  continue;
327  unsigned MaxPromForTarget = getMaxNumOfPromotionsInLoop(TargetLoop);
328  unsigned PendingCandsInTarget = LoopToCandidates[TargetLoop].size();
329  MaxProm =
330  std::min(MaxProm, std::max(MaxPromForTarget, PendingCandsInTarget) -
331  PendingCandsInTarget);
332  }
333  return MaxProm;
334  }
335 
337  SmallVector<BasicBlock *, 8> ExitBlocks;
339  Loop &L;
340  LoopInfo &LI;
341 };
342 
343 } // end anonymous namespace
344 
346  auto &TLI = AM.getResult<TargetLibraryAnalysis>(M);
347  if (!run(M, TLI))
348  return PreservedAnalyses::all();
349 
350  return PreservedAnalyses::none();
351 }
352 
355  InstrProfilingLegacyPass, "instrprof",
356  "Frontend instrumentation-based coverage lowering.", false, false)
359  InstrProfilingLegacyPass, "instrprof",
360  "Frontend instrumentation-based coverage lowering.", false, false)
361 
362 ModulePass *
364  return new InstrProfilingLegacyPass(Options);
365 }
366 
369  if (Inc)
370  return Inc;
371  return dyn_cast<InstrProfIncrementInst>(Instr);
372 }
373 
374 bool InstrProfiling::lowerIntrinsics(Function *F) {
375  bool MadeChange = false;
376  PromotionCandidates.clear();
377  for (BasicBlock &BB : *F) {
378  for (auto I = BB.begin(), E = BB.end(); I != E;) {
379  auto Instr = I++;
381  if (Inc) {
382  lowerIncrement(Inc);
383  MadeChange = true;
384  } else if (auto *Ind = dyn_cast<InstrProfValueProfileInst>(Instr)) {
385  lowerValueProfileInst(Ind);
386  MadeChange = true;
387  }
388  }
389  }
390 
391  if (!MadeChange)
392  return false;
393 
394  promoteCounterLoadStores(F);
395  return true;
396 }
397 
398 bool InstrProfiling::isCounterPromotionEnabled() const {
399  if (DoCounterPromotion.getNumOccurrences() > 0)
400  return DoCounterPromotion;
401 
402  return Options.DoCounterPromotion;
403 }
404 
405 void InstrProfiling::promoteCounterLoadStores(Function *F) {
406  if (!isCounterPromotionEnabled())
407  return;
408 
409  DominatorTree DT(*F);
410  LoopInfo LI(DT);
411  DenseMap<Loop *, SmallVector<LoadStorePair, 8>> LoopPromotionCandidates;
412 
413  for (const auto &LoadStore : PromotionCandidates) {
414  auto *CounterLoad = LoadStore.first;
415  auto *CounterStore = LoadStore.second;
416  BasicBlock *BB = CounterLoad->getParent();
417  Loop *ParentLoop = LI.getLoopFor(BB);
418  if (!ParentLoop)
419  continue;
420  LoopPromotionCandidates[ParentLoop].emplace_back(CounterLoad, CounterStore);
421  }
422 
424 
425  // Do a post-order traversal of the loops so that counter updates can be
426  // iteratively hoisted outside the loop nest.
427  for (auto *Loop : llvm::reverse(Loops)) {
428  PGOCounterPromoter Promoter(LoopPromotionCandidates, *Loop, LI);
429  Promoter.run(&TotalCountersPromoted);
430  }
431 }
432 
433 /// Check if the module contains uses of any profiling intrinsics.
435  if (auto *F = M.getFunction(
436  Intrinsic::getName(llvm::Intrinsic::instrprof_increment)))
437  if (!F->use_empty())
438  return true;
439  if (auto *F = M.getFunction(
440  Intrinsic::getName(llvm::Intrinsic::instrprof_increment_step)))
441  if (!F->use_empty())
442  return true;
443  if (auto *F = M.getFunction(
444  Intrinsic::getName(llvm::Intrinsic::instrprof_value_profile)))
445  if (!F->use_empty())
446  return true;
447  return false;
448 }
449 
451  this->M = &M;
452  this->TLI = &TLI;
453  NamesVar = nullptr;
454  NamesSize = 0;
455  ProfileDataMap.clear();
456  UsedVars.clear();
457  getMemOPSizeRangeFromOption(MemOPSizeRange, MemOPSizeRangeStart,
458  MemOPSizeRangeLast);
459  TT = Triple(M.getTargetTriple());
460 
461  // Emit the runtime hook even if no counters are present.
462  bool MadeChange = emitRuntimeHook();
463 
464  // Improve compile time by avoiding linear scans when there is no work.
465  GlobalVariable *CoverageNamesVar =
467  if (!containsProfilingIntrinsics(M) && !CoverageNamesVar)
468  return MadeChange;
469 
470  // We did not know how many value sites there would be inside
471  // the instrumented function. This is counting the number of instrumented
472  // target value sites to enter it as field in the profile data variable.
473  for (Function &F : M) {
474  InstrProfIncrementInst *FirstProfIncInst = nullptr;
475  for (BasicBlock &BB : F)
476  for (auto I = BB.begin(), E = BB.end(); I != E; I++)
477  if (auto *Ind = dyn_cast<InstrProfValueProfileInst>(I))
478  computeNumValueSiteCounts(Ind);
479  else if (FirstProfIncInst == nullptr)
480  FirstProfIncInst = dyn_cast<InstrProfIncrementInst>(I);
481 
482  // Value profiling intrinsic lowering requires per-function profile data
483  // variable to be created first.
484  if (FirstProfIncInst != nullptr)
485  static_cast<void>(getOrCreateRegionCounters(FirstProfIncInst));
486  }
487 
488  for (Function &F : M)
489  MadeChange |= lowerIntrinsics(&F);
490 
491  if (CoverageNamesVar) {
492  lowerCoverageData(CoverageNamesVar);
493  MadeChange = true;
494  }
495 
496  if (!MadeChange)
497  return false;
498 
499  emitVNodes();
500  emitNameData();
501  emitRegistration();
502  emitUses();
503  emitInitialization();
504  return true;
505 }
506 
508  const TargetLibraryInfo &TLI,
509  bool IsRange = false) {
510  LLVMContext &Ctx = M.getContext();
511  auto *ReturnTy = Type::getVoidTy(M.getContext());
512 
513  Constant *Res;
514  if (!IsRange) {
515  Type *ParamTypes[] = {
516 #define VALUE_PROF_FUNC_PARAM(ParamType, ParamName, ParamLLVMType) ParamLLVMType
518  };
519  auto *ValueProfilingCallTy =
520  FunctionType::get(ReturnTy, makeArrayRef(ParamTypes), false);
522  ValueProfilingCallTy);
523  } else {
524  Type *RangeParamTypes[] = {
525 #define VALUE_RANGE_PROF 1
526 #define VALUE_PROF_FUNC_PARAM(ParamType, ParamName, ParamLLVMType) ParamLLVMType
528 #undef VALUE_RANGE_PROF
529  };
530  auto *ValueRangeProfilingCallTy =
531  FunctionType::get(ReturnTy, makeArrayRef(RangeParamTypes), false);
533  ValueRangeProfilingCallTy);
534  }
535 
536  if (Function *FunRes = dyn_cast<Function>(Res)) {
537  if (auto AK = TLI.getExtAttrForI32Param(false))
538  FunRes->addParamAttr(2, AK);
539  }
540  return Res;
541 }
542 
543 void InstrProfiling::computeNumValueSiteCounts(InstrProfValueProfileInst *Ind) {
544  GlobalVariable *Name = Ind->getName();
545  uint64_t ValueKind = Ind->getValueKind()->getZExtValue();
546  uint64_t Index = Ind->getIndex()->getZExtValue();
547  auto It = ProfileDataMap.find(Name);
548  if (It == ProfileDataMap.end()) {
549  PerFunctionProfileData PD;
550  PD.NumValueSites[ValueKind] = Index + 1;
551  ProfileDataMap[Name] = PD;
552  } else if (It->second.NumValueSites[ValueKind] <= Index)
553  It->second.NumValueSites[ValueKind] = Index + 1;
554 }
555 
556 void InstrProfiling::lowerValueProfileInst(InstrProfValueProfileInst *Ind) {
557  GlobalVariable *Name = Ind->getName();
558  auto It = ProfileDataMap.find(Name);
559  assert(It != ProfileDataMap.end() && It->second.DataVar &&
560  "value profiling detected in function with no counter incerement");
561 
562  GlobalVariable *DataVar = It->second.DataVar;
563  uint64_t ValueKind = Ind->getValueKind()->getZExtValue();
564  uint64_t Index = Ind->getIndex()->getZExtValue();
565  for (uint32_t Kind = IPVK_First; Kind < ValueKind; ++Kind)
566  Index += It->second.NumValueSites[Kind];
567 
568  IRBuilder<> Builder(Ind);
569  bool IsRange = (Ind->getValueKind()->getZExtValue() ==
570  llvm::InstrProfValueKind::IPVK_MemOPSize);
571  CallInst *Call = nullptr;
572  if (!IsRange) {
573  Value *Args[3] = {Ind->getTargetValue(),
574  Builder.CreateBitCast(DataVar, Builder.getInt8PtrTy()),
575  Builder.getInt32(Index)};
576  Call = Builder.CreateCall(getOrInsertValueProfilingCall(*M, *TLI), Args);
577  } else {
578  Value *Args[6] = {
579  Ind->getTargetValue(),
580  Builder.CreateBitCast(DataVar, Builder.getInt8PtrTy()),
581  Builder.getInt32(Index),
582  Builder.getInt64(MemOPSizeRangeStart),
583  Builder.getInt64(MemOPSizeRangeLast),
584  Builder.getInt64(MemOPSizeLarge == 0 ? INT64_MIN : MemOPSizeLarge)};
585  Call =
586  Builder.CreateCall(getOrInsertValueProfilingCall(*M, *TLI, true), Args);
587  }
588  if (auto AK = TLI->getExtAttrForI32Param(false))
589  Call->addParamAttr(2, AK);
590  Ind->replaceAllUsesWith(Call);
591  Ind->eraseFromParent();
592 }
593 
594 void InstrProfiling::lowerIncrement(InstrProfIncrementInst *Inc) {
595  GlobalVariable *Counters = getOrCreateRegionCounters(Inc);
596 
597  IRBuilder<> Builder(Inc);
598  uint64_t Index = Inc->getIndex()->getZExtValue();
599  Value *Addr = Builder.CreateConstInBoundsGEP2_64(Counters, 0, Index);
600  Value *Load = Builder.CreateLoad(Addr, "pgocount");
601  auto *Count = Builder.CreateAdd(Load, Inc->getStep());
602  auto *Store = Builder.CreateStore(Count, Addr);
604  if (isCounterPromotionEnabled())
605  PromotionCandidates.emplace_back(cast<Instruction>(Load), Store);
606  Inc->eraseFromParent();
607 }
608 
609 void InstrProfiling::lowerCoverageData(GlobalVariable *CoverageNamesVar) {
610  ConstantArray *Names =
611  cast<ConstantArray>(CoverageNamesVar->getInitializer());
612  for (unsigned I = 0, E = Names->getNumOperands(); I < E; ++I) {
613  Constant *NC = Names->getOperand(I);
614  Value *V = NC->stripPointerCasts();
615  assert(isa<GlobalVariable>(V) && "Missing reference to function name");
616  GlobalVariable *Name = cast<GlobalVariable>(V);
617 
619  ReferencedNames.push_back(Name);
620  NC->dropAllReferences();
621  }
622  CoverageNamesVar->eraseFromParent();
623 }
624 
625 /// Get the name of a profiling variable for a particular function.
627  StringRef NamePrefix = getInstrProfNameVarPrefix();
628  StringRef Name = Inc->getName()->getName().substr(NamePrefix.size());
629  Function *F = Inc->getParent()->getParent();
630  Module *M = F->getParent();
631  if (!DoHashBasedCounterSplit || !isIRPGOFlagSet(M) ||
632  !canRenameComdatFunc(*F))
633  return (Prefix + Name).str();
634  uint64_t FuncHash = Inc->getHash()->getZExtValue();
635  SmallVector<char, 24> HashPostfix;
636  if (Name.endswith((Twine(".") + Twine(FuncHash)).toStringRef(HashPostfix)))
637  return (Prefix + Name).str();
638  return (Prefix + Name + "." + Twine(FuncHash)).str();
639 }
640 
641 static inline bool shouldRecordFunctionAddr(Function *F) {
642  // Check the linkage
643  bool HasAvailableExternallyLinkage = F->hasAvailableExternallyLinkage();
644  if (!F->hasLinkOnceLinkage() && !F->hasLocalLinkage() &&
645  !HasAvailableExternallyLinkage)
646  return true;
647 
648  // A function marked 'alwaysinline' with available_externally linkage can't
649  // have its address taken. Doing so would create an undefined external ref to
650  // the function, which would fail to link.
651  if (HasAvailableExternallyLinkage &&
652  F->hasFnAttribute(Attribute::AlwaysInline))
653  return false;
654 
655  // Prohibit function address recording if the function is both internal and
656  // COMDAT. This avoids the profile data variable referencing internal symbols
657  // in COMDAT.
658  if (F->hasLocalLinkage() && F->hasComdat())
659  return false;
660 
661  // Check uses of this function for other than direct calls or invokes to it.
662  // Inline virtual functions have linkeOnceODR linkage. When a key method
663  // exists, the vtable will only be emitted in the TU where the key method
664  // is defined. In a TU where vtable is not available, the function won't
665  // be 'addresstaken'. If its address is not recorded here, the profile data
666  // with missing address may be picked by the linker leading to missing
667  // indirect call target info.
668  return F->hasAddressTaken() || F->hasLinkOnceLinkage();
669 }
670 
672  InstrProfIncrementInst *Inc) {
673  if (!needsComdatForCounter(F, M))
674  return nullptr;
675 
676  // COFF format requires a COMDAT section to have a key symbol with the same
677  // name. The linker targeting COFF also requires that the COMDAT
678  // a section is associated to must precede the associating section. For this
679  // reason, we must choose the counter var's name as the name of the comdat.
680  StringRef ComdatPrefix = (Triple(M.getTargetTriple()).isOSBinFormatCOFF()
683  return M.getOrInsertComdat(StringRef(getVarName(Inc, ComdatPrefix)));
684 }
685 
687  // Don't do this for Darwin. compiler-rt uses linker magic.
688  if (Triple(M.getTargetTriple()).isOSDarwin())
689  return false;
690 
691  // Use linker script magic to get data/cnts/name start/end.
692  if (Triple(M.getTargetTriple()).isOSLinux() ||
693  Triple(M.getTargetTriple()).isOSFreeBSD() ||
694  Triple(M.getTargetTriple()).isPS4CPU())
695  return false;
696 
697  return true;
698 }
699 
701 InstrProfiling::getOrCreateRegionCounters(InstrProfIncrementInst *Inc) {
702  GlobalVariable *NamePtr = Inc->getName();
703  auto It = ProfileDataMap.find(NamePtr);
704  PerFunctionProfileData PD;
705  if (It != ProfileDataMap.end()) {
706  if (It->second.RegionCounters)
707  return It->second.RegionCounters;
708  PD = It->second;
709  }
710 
711  // Move the name variable to the right section. Place them in a COMDAT group
712  // if the associated function is a COMDAT. This will make sure that
713  // only one copy of counters of the COMDAT function will be emitted after
714  // linking.
715  Function *Fn = Inc->getParent()->getParent();
716  Comdat *ProfileVarsComdat = nullptr;
717  ProfileVarsComdat = getOrCreateProfileComdat(*M, *Fn, Inc);
718 
719  uint64_t NumCounters = Inc->getNumCounters()->getZExtValue();
720  LLVMContext &Ctx = M->getContext();
721  ArrayType *CounterTy = ArrayType::get(Type::getInt64Ty(Ctx), NumCounters);
722 
723  // Create the counters variable.
724  auto *CounterPtr =
725  new GlobalVariable(*M, CounterTy, false, NamePtr->getLinkage(),
726  Constant::getNullValue(CounterTy),
728  CounterPtr->setVisibility(NamePtr->getVisibility());
729  CounterPtr->setSection(
730  getInstrProfSectionName(IPSK_cnts, TT.getObjectFormat()));
731  CounterPtr->setAlignment(8);
732  CounterPtr->setComdat(ProfileVarsComdat);
733 
734  auto *Int8PtrTy = Type::getInt8PtrTy(Ctx);
735  // Allocate statically the array of pointers to value profile nodes for
736  // the current function.
737  Constant *ValuesPtrExpr = ConstantPointerNull::get(Int8PtrTy);
738  if (ValueProfileStaticAlloc && !needsRuntimeRegistrationOfSectionRange(*M)) {
739  uint64_t NS = 0;
740  for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
741  NS += PD.NumValueSites[Kind];
742  if (NS) {
743  ArrayType *ValuesTy = ArrayType::get(Type::getInt64Ty(Ctx), NS);
744 
745  auto *ValuesVar =
746  new GlobalVariable(*M, ValuesTy, false, NamePtr->getLinkage(),
747  Constant::getNullValue(ValuesTy),
749  ValuesVar->setVisibility(NamePtr->getVisibility());
750  ValuesVar->setSection(
751  getInstrProfSectionName(IPSK_vals, TT.getObjectFormat()));
752  ValuesVar->setAlignment(8);
753  ValuesVar->setComdat(ProfileVarsComdat);
754  ValuesPtrExpr =
756  }
757  }
758 
759  // Create data variable.
760  auto *Int16Ty = Type::getInt16Ty(Ctx);
761  auto *Int16ArrayTy = ArrayType::get(Int16Ty, IPVK_Last + 1);
762  Type *DataTypes[] = {
763 #define INSTR_PROF_DATA(Type, LLVMType, Name, Init) LLVMType,
765  };
766  auto *DataTy = StructType::get(Ctx, makeArrayRef(DataTypes));
767 
768  Constant *FunctionAddr = shouldRecordFunctionAddr(Fn)
769  ? ConstantExpr::getBitCast(Fn, Int8PtrTy)
770  : ConstantPointerNull::get(Int8PtrTy);
771 
772  Constant *Int16ArrayVals[IPVK_Last + 1];
773  for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
774  Int16ArrayVals[Kind] = ConstantInt::get(Int16Ty, PD.NumValueSites[Kind]);
775 
776  Constant *DataVals[] = {
777 #define INSTR_PROF_DATA(Type, LLVMType, Name, Init) Init,
779  };
780  auto *Data = new GlobalVariable(*M, DataTy, false, NamePtr->getLinkage(),
781  ConstantStruct::get(DataTy, DataVals),
783  Data->setVisibility(NamePtr->getVisibility());
784  Data->setSection(getInstrProfSectionName(IPSK_data, TT.getObjectFormat()));
785  Data->setAlignment(INSTR_PROF_DATA_ALIGNMENT);
786  Data->setComdat(ProfileVarsComdat);
787 
788  PD.RegionCounters = CounterPtr;
789  PD.DataVar = Data;
790  ProfileDataMap[NamePtr] = PD;
791 
792  // Mark the data variable as used so that it isn't stripped out.
793  UsedVars.push_back(Data);
794  // Now that the linkage set by the FE has been passed to the data and counter
795  // variables, reset Name variable's linkage and visibility to private so that
796  // it can be removed later by the compiler.
798  // Collect the referenced names to be used by emitNameData.
799  ReferencedNames.push_back(NamePtr);
800 
801  return CounterPtr;
802 }
803 
804 void InstrProfiling::emitVNodes() {
805  if (!ValueProfileStaticAlloc)
806  return;
807 
808  // For now only support this on platforms that do
809  // not require runtime registration to discover
810  // named section start/end.
812  return;
813 
814  size_t TotalNS = 0;
815  for (auto &PD : ProfileDataMap) {
816  for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
817  TotalNS += PD.second.NumValueSites[Kind];
818  }
819 
820  if (!TotalNS)
821  return;
822 
823  uint64_t NumCounters = TotalNS * NumCountersPerValueSite;
824 // Heuristic for small programs with very few total value sites.
825 // The default value of vp-counters-per-site is chosen based on
826 // the observation that large apps usually have a low percentage
827 // of value sites that actually have any profile data, and thus
828 // the average number of counters per site is low. For small
829 // apps with very few sites, this may not be true. Bump up the
830 // number of counters in this case.
831 #define INSTR_PROF_MIN_VAL_COUNTS 10
832  if (NumCounters < INSTR_PROF_MIN_VAL_COUNTS)
833  NumCounters = std::max(INSTR_PROF_MIN_VAL_COUNTS, (int)NumCounters * 2);
834 
835  auto &Ctx = M->getContext();
836  Type *VNodeTypes[] = {
837 #define INSTR_PROF_VALUE_NODE(Type, LLVMType, Name, Init) LLVMType,
839  };
840  auto *VNodeTy = StructType::get(Ctx, makeArrayRef(VNodeTypes));
841 
842  ArrayType *VNodesTy = ArrayType::get(VNodeTy, NumCounters);
843  auto *VNodesVar = new GlobalVariable(
844  *M, VNodesTy, false, GlobalValue::PrivateLinkage,
846  VNodesVar->setSection(
847  getInstrProfSectionName(IPSK_vnodes, TT.getObjectFormat()));
848  UsedVars.push_back(VNodesVar);
849 }
850 
851 void InstrProfiling::emitNameData() {
852  std::string UncompressedData;
853 
854  if (ReferencedNames.empty())
855  return;
856 
857  std::string CompressedNameStr;
858  if (Error E = collectPGOFuncNameStrings(ReferencedNames, CompressedNameStr,
859  DoNameCompression)) {
860  report_fatal_error(toString(std::move(E)), false);
861  }
862 
863  auto &Ctx = M->getContext();
864  auto *NamesVal = ConstantDataArray::getString(
865  Ctx, StringRef(CompressedNameStr), false);
866  NamesVar = new GlobalVariable(*M, NamesVal->getType(), true,
867  GlobalValue::PrivateLinkage, NamesVal,
869  NamesSize = CompressedNameStr.size();
870  NamesVar->setSection(
871  getInstrProfSectionName(IPSK_name, TT.getObjectFormat()));
872  UsedVars.push_back(NamesVar);
873 
874  for (auto *NamePtr : ReferencedNames)
875  NamePtr->eraseFromParent();
876 }
877 
878 void InstrProfiling::emitRegistration() {
880  return;
881 
882  // Construct the function.
883  auto *VoidTy = Type::getVoidTy(M->getContext());
884  auto *VoidPtrTy = Type::getInt8PtrTy(M->getContext());
885  auto *Int64Ty = Type::getInt64Ty(M->getContext());
886  auto *RegisterFTy = FunctionType::get(VoidTy, false);
887  auto *RegisterF = Function::Create(RegisterFTy, GlobalValue::InternalLinkage,
889  RegisterF->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
890  if (Options.NoRedZone)
891  RegisterF->addFnAttr(Attribute::NoRedZone);
892 
893  auto *RuntimeRegisterTy = FunctionType::get(VoidTy, VoidPtrTy, false);
894  auto *RuntimeRegisterF =
897 
898  IRBuilder<> IRB(BasicBlock::Create(M->getContext(), "", RegisterF));
899  for (Value *Data : UsedVars)
900  if (Data != NamesVar)
901  IRB.CreateCall(RuntimeRegisterF, IRB.CreateBitCast(Data, VoidPtrTy));
902 
903  if (NamesVar) {
904  Type *ParamTypes[] = {VoidPtrTy, Int64Ty};
905  auto *NamesRegisterTy =
906  FunctionType::get(VoidTy, makeArrayRef(ParamTypes), false);
907  auto *NamesRegisterF =
910  IRB.CreateCall(NamesRegisterF, {IRB.CreateBitCast(NamesVar, VoidPtrTy),
911  IRB.getInt64(NamesSize)});
912  }
913 
914  IRB.CreateRetVoid();
915 }
916 
917 bool InstrProfiling::emitRuntimeHook() {
918  // We expect the linker to be invoked with -u<hook_var> flag for linux,
919  // for which case there is no need to emit the user function.
920  if (Triple(M->getTargetTriple()).isOSLinux())
921  return false;
922 
923  // If the module's provided its own runtime, we don't need to do anything.
925  return false;
926 
927  // Declare an external variable that will pull in the runtime initialization.
928  auto *Int32Ty = Type::getInt32Ty(M->getContext());
929  auto *Var =
931  nullptr, getInstrProfRuntimeHookVarName());
932 
933  // Make a function that uses it.
937  User->addFnAttr(Attribute::NoInline);
938  if (Options.NoRedZone)
939  User->addFnAttr(Attribute::NoRedZone);
940  User->setVisibility(GlobalValue::HiddenVisibility);
941  if (Triple(M->getTargetTriple()).supportsCOMDAT())
942  User->setComdat(M->getOrInsertComdat(User->getName()));
943 
945  auto *Load = IRB.CreateLoad(Var);
946  IRB.CreateRet(Load);
947 
948  // Mark the user variable as used so that it isn't stripped out.
949  UsedVars.push_back(User);
950  return true;
951 }
952 
953 void InstrProfiling::emitUses() {
954  if (!UsedVars.empty())
955  appendToUsed(*M, UsedVars);
956 }
957 
958 void InstrProfiling::emitInitialization() {
959  StringRef InstrProfileOutput = Options.InstrProfileOutput;
960 
961  if (!InstrProfileOutput.empty()) {
962  // Create variable for profile name.
963  Constant *ProfileNameConst =
964  ConstantDataArray::getString(M->getContext(), InstrProfileOutput, true);
965  GlobalVariable *ProfileNameVar = new GlobalVariable(
966  *M, ProfileNameConst->getType(), true, GlobalValue::WeakAnyLinkage,
967  ProfileNameConst, INSTR_PROF_QUOTE(INSTR_PROF_PROFILE_NAME_VAR));
968  if (TT.supportsCOMDAT()) {
969  ProfileNameVar->setLinkage(GlobalValue::ExternalLinkage);
970  ProfileNameVar->setComdat(M->getOrInsertComdat(
971  StringRef(INSTR_PROF_QUOTE(INSTR_PROF_PROFILE_NAME_VAR))));
972  }
973  }
974 
975  Constant *RegisterF = M->getFunction(getInstrProfRegFuncsName());
976  if (!RegisterF)
977  return;
978 
979  // Create the initialization function.
980  auto *VoidTy = Type::getVoidTy(M->getContext());
981  auto *F = Function::Create(FunctionType::get(VoidTy, false),
984  F->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
985  F->addFnAttr(Attribute::NoInline);
986  if (Options.NoRedZone)
987  F->addFnAttr(Attribute::NoRedZone);
988 
989  // Add the basic block and the necessary calls.
990  IRBuilder<> IRB(BasicBlock::Create(M->getContext(), "", F));
991  if (RegisterF)
992  IRB.CreateCall(RegisterF, {});
993  IRB.CreateRetVoid();
994 
995  appendToGlobalCtors(*M, F, 0);
996 }
void setVisibility(VisibilityTypes V)
Definition: GlobalValue.h:238
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:68
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:238
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:2479
bool hasLocalLinkage() const
Definition: GlobalValue.h:435
Helper class for SSA formation on a set of values defined in multiple blocks.
Definition: SSAUpdater.h:39
Value * getPointerOperand(Value *V)
A helper function that returns the pointer operand of a load, store or GEP instruction.
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
ConstantInt * getIndex() const
void dropAllReferences()
Drop all references to operands.
Definition: User.h:295
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:137
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:138
static bool containsProfilingIntrinsics(Module &M)
Check if the module contains uses of any profiling intrinsics.
#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:422
bool canRenameComdatFunc(const Function &F, bool CheckAddressTaken=false)
Check if we can safely rename this Comdat function.
Definition: InstrProf.cpp:972
BlockT * getLoopPreheader() const
If there is a preheader for this loop, return it.
Definition: LoopInfoImpl.h:106
const GlobalVariable * getNamedGlobal(StringRef Name) const
Return the global variable in the module with the specified name, of arbitrary type.
Definition: Module.h:388
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:307
GlobalVariable * getGlobalVariable(StringRef Name) const
Look up the specified global variable in the module symbol table.
Definition: Module.h:373
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
void addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind)
Adds the attribute to the indicated argument.
static Constant * getNullValue(Type *Ty)
Constructor to create a &#39;0&#39; constant of arbitrary type.
Definition: Constants.cpp:258
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
StringRef getName(ID id)
Return the LLVM name for an intrinsic, such as "llvm.ppc.altivec.lvx".
Definition: Function.cpp:612
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:684
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
#define INT64_MIN
Definition: DataTypes.h:80
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:242
Instrumentation based profiling lowering pass.
ReturnInst * CreateRet(Value *V)
Create a &#39;ret <val>&#39; instruction.
Definition: IRBuilder.h:817
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:731
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:378
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:962
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:1321
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:381
auto reverse(ContainerTy &&C, typename std::enable_if< has_rbegin< ContainerTy >::value >::type *=nullptr) -> decltype(make_range(C.rbegin(), C.rend()))
Definition: STLExtras.h:237
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1629
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:66
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:450
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:439
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:425
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:142
Value * getOperand(unsigned i) const
Definition: User.h:170
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:952
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:1740
LoadInst * CreateLoad(Value *Ptr, const char *Name)
Provided to resolve &#39;CreateLoad(Ptr, "...")&#39; correctly, instead of converting the string to &#39;bool&#39; fo...
Definition: IRBuilder.h:1305
void getMemOPSizeRangeFromOption(StringRef Str, int64_t &RangeStart, int64_t &RangeLast)
Definition: InstrProf.cpp:995
bool needsComdatForCounter(const Function &F, const Module &M)
Check if we can use Comdat for profile variables.
Definition: InstrProf.cpp:925
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:52
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:410
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:218
static ConstantPointerNull * get(PointerType *T)
Static factory methods - Return objects of the specified value.
Definition: Constants.cpp:1368
VisibilityTypes getVisibility() const
Definition: GlobalValue.h:232
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:354
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:915
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:1011
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:312
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:53
const Constant * stripPointerCasts() const
Definition: Constant.h:169
Comdat * getOrInsertComdat(StringRef Name)
Return the Comdat in the module with the specified name.
Definition: Module.cpp:474
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:192
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:861
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:611
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:286
#define NC
Definition: regutils.h:42
void setLinkage(LinkageTypes LT)
Definition: GlobalValue.h:444
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:133
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:410
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:1514
This represents the llvm.instrprof_increment intrinsic.
ReturnInst * CreateRetVoid()
Create a &#39;ret void&#39; instruction.
Definition: IRBuilder.h:812
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:176
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:445
ConstantInt * getHash() const
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:224
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:1234
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:565
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
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:138
static bool lowerIntrinsics(Module &M)
#define LLVM_DEBUG(X)
Definition: Debug.h:119
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
bool use_empty() const
Definition: Value.h:322
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:67
CallInst * CreateCall(Value *Callee, ArrayRef< Value *> Args=None, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1871