LLVM 22.0.0git
InstrProfiling.cpp
Go to the documentation of this file.
1//===-- InstrProfiling.cpp - Frontend instrumentation based profiling -----===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This pass lowers instrprof_* intrinsics emitted by an instrumentor.
10// It also builds the data structures and initialization code needed for
11// updating execution counts and emitting the profile at runtime.
12//
13//===----------------------------------------------------------------------===//
14
16#include "llvm/ADT/ArrayRef.h"
17#include "llvm/ADT/STLExtras.h"
19#include "llvm/ADT/StringRef.h"
20#include "llvm/ADT/Twine.h"
23#include "llvm/Analysis/CFG.h"
26#include "llvm/IR/Attributes.h"
27#include "llvm/IR/BasicBlock.h"
28#include "llvm/IR/CFG.h"
29#include "llvm/IR/Constant.h"
30#include "llvm/IR/Constants.h"
31#include "llvm/IR/DIBuilder.h"
34#include "llvm/IR/Dominators.h"
35#include "llvm/IR/Function.h"
36#include "llvm/IR/GlobalValue.h"
38#include "llvm/IR/IRBuilder.h"
39#include "llvm/IR/Instruction.h"
42#include "llvm/IR/MDBuilder.h"
43#include "llvm/IR/Module.h"
44#include "llvm/IR/Type.h"
45#include "llvm/Pass.h"
51#include "llvm/Support/Error.h"
59#include <algorithm>
60#include <cassert>
61#include <cstdint>
62#include <string>
63
64using namespace llvm;
65
66#define DEBUG_TYPE "instrprof"
67
68namespace llvm {
69// Command line option to enable vtable value profiling. Defined in
70// ProfileData/InstrProf.cpp: -enable-vtable-value-profiling=
73 "profile-correlate",
74 cl::desc("Use debug info or binary file to correlate profiles."),
77 "No profile correlation"),
79 "Use debug info to correlate"),
81 "Use binary to correlate")));
82} // namespace llvm
83
84namespace {
85
86cl::opt<bool> DoHashBasedCounterSplit(
87 "hash-based-counter-split",
88 cl::desc("Rename counter variable of a comdat function based on cfg hash"),
89 cl::init(true));
90
92 RuntimeCounterRelocation("runtime-counter-relocation",
93 cl::desc("Enable relocating counters at runtime."),
94 cl::init(false));
95
96cl::opt<bool> ValueProfileStaticAlloc(
97 "vp-static-alloc",
98 cl::desc("Do static counter allocation for value profiler"),
99 cl::init(true));
100
101cl::opt<double> NumCountersPerValueSite(
102 "vp-counters-per-site",
103 cl::desc("The average number of profile counters allocated "
104 "per value profiling site."),
105 // This is set to a very small value because in real programs, only
106 // a very small percentage of value sites have non-zero targets, e.g, 1/30.
107 // For those sites with non-zero profile, the average number of targets
108 // is usually smaller than 2.
109 cl::init(1.0));
110
111cl::opt<bool> AtomicCounterUpdateAll(
112 "instrprof-atomic-counter-update-all",
113 cl::desc("Make all profile counter updates atomic (for testing only)"),
114 cl::init(false));
115
116cl::opt<bool> AtomicCounterUpdatePromoted(
117 "atomic-counter-update-promoted",
118 cl::desc("Do counter update using atomic fetch add "
119 " for promoted counters only"),
120 cl::init(false));
121
122cl::opt<bool> AtomicFirstCounter(
123 "atomic-first-counter",
124 cl::desc("Use atomic fetch add for first counter in a function (usually "
125 "the entry counter)"),
126 cl::init(false));
127
128cl::opt<bool> ConditionalCounterUpdate(
129 "conditional-counter-update",
130 cl::desc("Do conditional counter updates in single byte counters mode)"),
131 cl::init(false));
132
133// If the option is not specified, the default behavior about whether
134// counter promotion is done depends on how instrumentation lowering
135// pipeline is setup, i.e., the default value of true of this option
136// does not mean the promotion will be done by default. Explicitly
137// setting this option can override the default behavior.
138cl::opt<bool> DoCounterPromotion("do-counter-promotion",
139 cl::desc("Do counter register promotion"),
140 cl::init(false));
141cl::opt<unsigned> MaxNumOfPromotionsPerLoop(
142 "max-counter-promotions-per-loop", cl::init(20),
143 cl::desc("Max number counter promotions per loop to avoid"
144 " increasing register pressure too much"));
145
146// A debug option
148 MaxNumOfPromotions("max-counter-promotions", cl::init(-1),
149 cl::desc("Max number of allowed counter promotions"));
150
151cl::opt<unsigned> SpeculativeCounterPromotionMaxExiting(
152 "speculative-counter-promotion-max-exiting", cl::init(3),
153 cl::desc("The max number of exiting blocks of a loop to allow "
154 " speculative counter promotion"));
155
156cl::opt<bool> SpeculativeCounterPromotionToLoop(
157 "speculative-counter-promotion-to-loop",
158 cl::desc("When the option is false, if the target block is in a loop, "
159 "the promotion will be disallowed unless the promoted counter "
160 " update can be further/iteratively promoted into an acyclic "
161 " region."));
162
163cl::opt<bool> IterativeCounterPromotion(
164 "iterative-counter-promotion", cl::init(true),
165 cl::desc("Allow counter promotion across the whole loop nest."));
166
167cl::opt<bool> SkipRetExitBlock(
168 "skip-ret-exit-block", cl::init(true),
169 cl::desc("Suppress counter promotion if exit blocks contain ret."));
170
171static cl::opt<bool> SampledInstr("sampled-instrumentation", cl::ZeroOrMore,
172 cl::init(false),
173 cl::desc("Do PGO instrumentation sampling"));
174
175static cl::opt<unsigned> SampledInstrPeriod(
176 "sampled-instr-period",
177 cl::desc("Set the profile instrumentation sample period. A sample period "
178 "of 0 is invalid. For each sample period, a fixed number of "
179 "consecutive samples will be recorded. The number is controlled "
180 "by 'sampled-instr-burst-duration' flag. The default sample "
181 "period of 65536 is optimized for generating efficient code that "
182 "leverages unsigned short integer wrapping in overflow, but this "
183 "is disabled under simple sampling (burst duration = 1)."),
184 cl::init(USHRT_MAX + 1));
185
186static cl::opt<unsigned> SampledInstrBurstDuration(
187 "sampled-instr-burst-duration",
188 cl::desc("Set the profile instrumentation burst duration, which can range "
189 "from 1 to the value of 'sampled-instr-period' (0 is invalid). "
190 "This number of samples will be recorded for each "
191 "'sampled-instr-period' count update. Setting to 1 enables simple "
192 "sampling, in which case it is recommended to set "
193 "'sampled-instr-period' to a prime number."),
194 cl::init(200));
195
196struct SampledInstrumentationConfig {
197 unsigned BurstDuration;
198 unsigned Period;
199 bool UseShort;
200 bool IsSimpleSampling;
201 bool IsFastSampling;
202};
203
204static SampledInstrumentationConfig getSampledInstrumentationConfig() {
205 SampledInstrumentationConfig config;
206 config.BurstDuration = SampledInstrBurstDuration.getValue();
207 config.Period = SampledInstrPeriod.getValue();
208 if (config.BurstDuration > config.Period)
210 "SampledBurstDuration must be less than or equal to SampledPeriod");
211 if (config.Period == 0 || config.BurstDuration == 0)
213 "SampledPeriod and SampledBurstDuration must be greater than 0");
214 config.IsSimpleSampling = (config.BurstDuration == 1);
215 // If (BurstDuration == 1 && Period == 65536), generate the simple sampling
216 // style code.
217 config.IsFastSampling =
218 (!config.IsSimpleSampling && config.Period == USHRT_MAX + 1);
219 config.UseShort = (config.Period <= USHRT_MAX) || config.IsFastSampling;
220 return config;
221}
222
223using LoadStorePair = std::pair<Instruction *, Instruction *>;
224
225static uint64_t getIntModuleFlagOrZero(const Module &M, StringRef Flag) {
226 auto *MD = dyn_cast_or_null<ConstantAsMetadata>(M.getModuleFlag(Flag));
227 if (!MD)
228 return 0;
229
230 // If the flag is a ConstantAsMetadata, it should be an integer representable
231 // in 64-bits.
232 return cast<ConstantInt>(MD->getValue())->getZExtValue();
233}
234
235static bool enablesValueProfiling(const Module &M) {
236 return isIRPGOFlagSet(&M) ||
237 getIntModuleFlagOrZero(M, "EnableValueProfiling") != 0;
238}
239
240// Conservatively returns true if value profiling is enabled.
241static bool profDataReferencedByCode(const Module &M) {
242 return enablesValueProfiling(M);
243}
244
245class InstrLowerer final {
246public:
247 InstrLowerer(Module &M, const InstrProfOptions &Options,
248 std::function<const TargetLibraryInfo &(Function &F)> GetTLI,
249 bool IsCS)
250 : M(M), Options(Options), TT(M.getTargetTriple()), IsCS(IsCS),
251 GetTLI(GetTLI), DataReferencedByCode(profDataReferencedByCode(M)) {}
252
253 bool lower();
254
255private:
256 Module &M;
257 const InstrProfOptions Options;
258 const Triple TT;
259 // Is this lowering for the context-sensitive instrumentation.
260 const bool IsCS;
261
262 std::function<const TargetLibraryInfo &(Function &F)> GetTLI;
263
264 const bool DataReferencedByCode;
265
266 struct PerFunctionProfileData {
267 uint32_t NumValueSites[IPVK_Last + 1] = {};
268 GlobalVariable *RegionCounters = nullptr;
269 GlobalVariable *DataVar = nullptr;
270 GlobalVariable *RegionBitmaps = nullptr;
271 uint32_t NumBitmapBytes = 0;
272
273 PerFunctionProfileData() = default;
274 };
275 DenseMap<GlobalVariable *, PerFunctionProfileData> ProfileDataMap;
276 // Key is virtual table variable, value is 'VTableProfData' in the form of
277 // GlobalVariable.
278 DenseMap<GlobalVariable *, GlobalVariable *> VTableDataMap;
279 /// If runtime relocation is enabled, this maps functions to the load
280 /// instruction that produces the profile relocation bias.
281 DenseMap<const Function *, LoadInst *> FunctionToProfileBiasMap;
282 std::vector<GlobalValue *> CompilerUsedVars;
283 std::vector<GlobalValue *> UsedVars;
284 std::vector<GlobalVariable *> ReferencedNames;
285 // The list of virtual table variables of which the VTableProfData is
286 // collected.
287 std::vector<GlobalVariable *> ReferencedVTables;
288 GlobalVariable *NamesVar = nullptr;
289 size_t NamesSize = 0;
290
291 // vector of counter load/store pairs to be register promoted.
292 std::vector<LoadStorePair> PromotionCandidates;
293
294 int64_t TotalCountersPromoted = 0;
295
296 /// Lower instrumentation intrinsics in the function. Returns true if there
297 /// any lowering.
298 bool lowerIntrinsics(Function *F);
299
300 /// Register-promote counter loads and stores in loops.
301 void promoteCounterLoadStores(Function *F);
302
303 /// Returns true if relocating counters at runtime is enabled.
304 bool isRuntimeCounterRelocationEnabled() const;
305
306 /// Returns true if profile counter update register promotion is enabled.
307 bool isCounterPromotionEnabled() const;
308
309 /// Return true if profile sampling is enabled.
310 bool isSamplingEnabled() const;
311
312 /// Count the number of instrumented value sites for the function.
313 void computeNumValueSiteCounts(InstrProfValueProfileInst *Ins);
314
315 /// Replace instrprof.value.profile with a call to runtime library.
316 void lowerValueProfileInst(InstrProfValueProfileInst *Ins);
317
318 /// Replace instrprof.cover with a store instruction to the coverage byte.
319 void lowerCover(InstrProfCoverInst *Inc);
320
321 /// Replace instrprof.timestamp with a call to
322 /// INSTR_PROF_PROFILE_SET_TIMESTAMP.
323 void lowerTimestamp(InstrProfTimestampInst *TimestampInstruction);
324
325 /// Replace instrprof.increment with an increment of the appropriate value.
326 void lowerIncrement(InstrProfIncrementInst *Inc);
327
328 /// Force emitting of name vars for unused functions.
329 void lowerCoverageData(GlobalVariable *CoverageNamesVar);
330
331 /// Replace instrprof.mcdc.tvbitmask.update with a shift and or instruction
332 /// using the index represented by the a temp value into a bitmap.
333 void lowerMCDCTestVectorBitmapUpdate(InstrProfMCDCTVBitmapUpdate *Ins);
334
335 /// Get the Bias value for data to access mmap-ed area.
336 /// Create it if it hasn't been seen.
337 GlobalVariable *getOrCreateBiasVar(StringRef VarName);
338
339 /// Compute the address of the counter value that this profiling instruction
340 /// acts on.
341 Value *getCounterAddress(InstrProfCntrInstBase *I);
342
343 /// Lower the incremental instructions under profile sampling predicates.
344 void doSampling(Instruction *I);
345
346 /// Get the region counters for an increment, creating them if necessary.
347 ///
348 /// If the counter array doesn't yet exist, the profile data variables
349 /// referring to them will also be created.
350 GlobalVariable *getOrCreateRegionCounters(InstrProfCntrInstBase *Inc);
351
352 /// Create the region counters.
353 GlobalVariable *createRegionCounters(InstrProfCntrInstBase *Inc,
354 StringRef Name,
356
357 /// Compute the address of the test vector bitmap that this profiling
358 /// instruction acts on.
359 Value *getBitmapAddress(InstrProfMCDCTVBitmapUpdate *I);
360
361 /// Get the region bitmaps for an increment, creating them if necessary.
362 ///
363 /// If the bitmap array doesn't yet exist, the profile data variables
364 /// referring to them will also be created.
365 GlobalVariable *getOrCreateRegionBitmaps(InstrProfMCDCBitmapInstBase *Inc);
366
367 /// Create the MC/DC bitmap as a byte-aligned array of bytes associated with
368 /// an MC/DC Decision region. The number of bytes required is indicated by
369 /// the intrinsic used (type InstrProfMCDCBitmapInstBase). This is called
370 /// as part of setupProfileSection() and is conceptually very similar to
371 /// what is done for profile data counters in createRegionCounters().
372 GlobalVariable *createRegionBitmaps(InstrProfMCDCBitmapInstBase *Inc,
373 StringRef Name,
375
376 /// Set Comdat property of GV, if required.
377 void maybeSetComdat(GlobalVariable *GV, GlobalObject *GO, StringRef VarName);
378
379 /// Setup the sections into which counters and bitmaps are allocated.
380 GlobalVariable *setupProfileSection(InstrProfInstBase *Inc,
381 InstrProfSectKind IPSK);
382
383 /// Create INSTR_PROF_DATA variable for counters and bitmaps.
384 void createDataVariable(InstrProfCntrInstBase *Inc);
385
386 /// Get the counters for virtual table values, creating them if necessary.
387 void getOrCreateVTableProfData(GlobalVariable *GV);
388
389 /// Emit the section with compressed function names.
390 void emitNameData();
391
392 /// Emit the section with compressed vtable names.
393 void emitVTableNames();
394
395 /// Emit value nodes section for value profiling.
396 void emitVNodes();
397
398 /// Emit runtime registration functions for each profile data variable.
399 void emitRegistration();
400
401 /// Emit the necessary plumbing to pull in the runtime initialization.
402 /// Returns true if a change was made.
403 bool emitRuntimeHook();
404
405 /// Add uses of our data variables and runtime hook.
406 void emitUses();
407
408 /// Create a static initializer for our data, on platforms that need it,
409 /// and for any profile output file that was specified.
410 void emitInitialization();
411};
412
413///
414/// A helper class to promote one counter RMW operation in the loop
415/// into register update.
416///
417/// RWM update for the counter will be sinked out of the loop after
418/// the transformation.
419///
420class PGOCounterPromoterHelper : public LoadAndStorePromoter {
421public:
422 PGOCounterPromoterHelper(
423 Instruction *L, Instruction *S, SSAUpdater &SSA, Value *Init,
424 BasicBlock *PH, ArrayRef<BasicBlock *> ExitBlocks,
425 ArrayRef<Instruction *> InsertPts,
426 DenseMap<Loop *, SmallVector<LoadStorePair, 8>> &LoopToCands,
427 LoopInfo &LI)
428 : LoadAndStorePromoter({L, S}, SSA), Store(S), ExitBlocks(ExitBlocks),
429 InsertPts(InsertPts), LoopToCandidates(LoopToCands), LI(LI) {
432 SSA.AddAvailableValue(PH, Init);
433 }
434
435 void doExtraRewritesBeforeFinalDeletion() override {
436 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) {
437 BasicBlock *ExitBlock = ExitBlocks[i];
438 Instruction *InsertPos = InsertPts[i];
439 // Get LiveIn value into the ExitBlock. If there are multiple
440 // predecessors, the value is defined by a PHI node in this
441 // block.
442 Value *LiveInValue = SSA.GetValueInMiddleOfBlock(ExitBlock);
443 Value *Addr = cast<StoreInst>(Store)->getPointerOperand();
444 Type *Ty = LiveInValue->getType();
445 IRBuilder<> Builder(InsertPos);
446 if (auto *AddrInst = dyn_cast_or_null<IntToPtrInst>(Addr)) {
447 // If isRuntimeCounterRelocationEnabled() is true then the address of
448 // the store instruction is computed with two instructions in
449 // InstrProfiling::getCounterAddress(). We need to copy those
450 // instructions to this block to compute Addr correctly.
451 // %BiasAdd = add i64 ptrtoint <__profc_>, <__llvm_profile_counter_bias>
452 // %Addr = inttoptr i64 %BiasAdd to i64*
453 auto *OrigBiasInst = dyn_cast<BinaryOperator>(AddrInst->getOperand(0));
454 assert(OrigBiasInst->getOpcode() == Instruction::BinaryOps::Add);
455 Value *BiasInst = Builder.Insert(OrigBiasInst->clone());
456 Addr = Builder.CreateIntToPtr(BiasInst,
457 PointerType::getUnqual(Ty->getContext()));
458 }
459 if (AtomicCounterUpdatePromoted)
460 // automic update currently can only be promoted across the current
461 // loop, not the whole loop nest.
462 Builder.CreateAtomicRMW(AtomicRMWInst::Add, Addr, LiveInValue,
463 MaybeAlign(),
464 AtomicOrdering::SequentiallyConsistent);
465 else {
466 LoadInst *OldVal = Builder.CreateLoad(Ty, Addr, "pgocount.promoted");
467 auto *NewVal = Builder.CreateAdd(OldVal, LiveInValue);
468 auto *NewStore = Builder.CreateStore(NewVal, Addr);
469
470 // Now update the parent loop's candidate list:
471 if (IterativeCounterPromotion) {
472 auto *TargetLoop = LI.getLoopFor(ExitBlock);
473 if (TargetLoop)
474 LoopToCandidates[TargetLoop].emplace_back(OldVal, NewStore);
475 }
476 }
477 }
478 }
479
480private:
481 Instruction *Store;
482 ArrayRef<BasicBlock *> ExitBlocks;
483 ArrayRef<Instruction *> InsertPts;
484 DenseMap<Loop *, SmallVector<LoadStorePair, 8>> &LoopToCandidates;
485 LoopInfo &LI;
486};
487
488/// A helper class to do register promotion for all profile counter
489/// updates in a loop.
490///
491class PGOCounterPromoter {
492public:
493 PGOCounterPromoter(
494 DenseMap<Loop *, SmallVector<LoadStorePair, 8>> &LoopToCands,
495 Loop &CurLoop, LoopInfo &LI, BlockFrequencyInfo *BFI)
496 : LoopToCandidates(LoopToCands), L(CurLoop), LI(LI), BFI(BFI) {
497
498 // Skip collection of ExitBlocks and InsertPts for loops that will not be
499 // able to have counters promoted.
500 SmallVector<BasicBlock *, 8> LoopExitBlocks;
501 SmallPtrSet<BasicBlock *, 8> BlockSet;
502
503 L.getExitBlocks(LoopExitBlocks);
504 if (!isPromotionPossible(&L, LoopExitBlocks))
505 return;
506
507 for (BasicBlock *ExitBlock : LoopExitBlocks) {
508 if (BlockSet.insert(ExitBlock).second &&
509 llvm::none_of(predecessors(ExitBlock), [&](const BasicBlock *Pred) {
510 return llvm::isPresplitCoroSuspendExitEdge(*Pred, *ExitBlock);
511 })) {
512 ExitBlocks.push_back(ExitBlock);
513 InsertPts.push_back(&*ExitBlock->getFirstInsertionPt());
514 }
515 }
516 }
517
518 bool run(int64_t *NumPromoted) {
519 // Skip 'infinite' loops:
520 if (ExitBlocks.size() == 0)
521 return false;
522
523 // Skip if any of the ExitBlocks contains a ret instruction.
524 // This is to prevent dumping of incomplete profile -- if the
525 // the loop is a long running loop and dump is called in the middle
526 // of the loop, the result profile is incomplete.
527 // FIXME: add other heuristics to detect long running loops.
528 if (SkipRetExitBlock) {
529 for (auto *BB : ExitBlocks)
530 if (isa<ReturnInst>(BB->getTerminator()))
531 return false;
532 }
533
534 unsigned MaxProm = getMaxNumOfPromotionsInLoop(&L);
535 if (MaxProm == 0)
536 return false;
537
538 unsigned Promoted = 0;
539 for (auto &Cand : LoopToCandidates[&L]) {
540
542 SSAUpdater SSA(&NewPHIs);
543 Value *InitVal = ConstantInt::get(Cand.first->getType(), 0);
544
545 // If BFI is set, we will use it to guide the promotions.
546 if (BFI) {
547 auto *BB = Cand.first->getParent();
548 auto InstrCount = BFI->getBlockProfileCount(BB);
549 if (!InstrCount)
550 continue;
551 auto PreheaderCount = BFI->getBlockProfileCount(L.getLoopPreheader());
552 // If the average loop trip count is not greater than 1.5, we skip
553 // promotion.
554 if (PreheaderCount && (*PreheaderCount * 3) >= (*InstrCount * 2))
555 continue;
556 }
557
558 PGOCounterPromoterHelper Promoter(Cand.first, Cand.second, SSA, InitVal,
559 L.getLoopPreheader(), ExitBlocks,
560 InsertPts, LoopToCandidates, LI);
561 Promoter.run(SmallVector<Instruction *, 2>({Cand.first, Cand.second}));
562 Promoted++;
563 if (Promoted >= MaxProm)
564 break;
565
566 (*NumPromoted)++;
567 if (MaxNumOfPromotions != -1 && *NumPromoted >= MaxNumOfPromotions)
568 break;
569 }
570
571 LLVM_DEBUG(dbgs() << Promoted << " counters promoted for loop (depth="
572 << L.getLoopDepth() << ")\n");
573 return Promoted != 0;
574 }
575
576private:
577 bool allowSpeculativeCounterPromotion(Loop *LP) {
578 SmallVector<BasicBlock *, 8> ExitingBlocks;
579 L.getExitingBlocks(ExitingBlocks);
580 // Not considierered speculative.
581 if (ExitingBlocks.size() == 1)
582 return true;
583 if (ExitingBlocks.size() > SpeculativeCounterPromotionMaxExiting)
584 return false;
585 return true;
586 }
587
588 // Check whether the loop satisfies the basic conditions needed to perform
589 // Counter Promotions.
590 bool
591 isPromotionPossible(Loop *LP,
592 const SmallVectorImpl<BasicBlock *> &LoopExitBlocks) {
593 // We can't insert into a catchswitch.
594 if (llvm::any_of(LoopExitBlocks, [](BasicBlock *Exit) {
595 return isa<CatchSwitchInst>(Exit->getTerminator());
596 }))
597 return false;
598
599 if (!LP->hasDedicatedExits())
600 return false;
601
602 BasicBlock *PH = LP->getLoopPreheader();
603 if (!PH)
604 return false;
605
606 return true;
607 }
608
609 // Returns the max number of Counter Promotions for LP.
610 unsigned getMaxNumOfPromotionsInLoop(Loop *LP) {
611 SmallVector<BasicBlock *, 8> LoopExitBlocks;
612 LP->getExitBlocks(LoopExitBlocks);
613 if (!isPromotionPossible(LP, LoopExitBlocks))
614 return 0;
615
616 SmallVector<BasicBlock *, 8> ExitingBlocks;
617 LP->getExitingBlocks(ExitingBlocks);
618
619 // If BFI is set, we do more aggressive promotions based on BFI.
620 if (BFI)
621 return (unsigned)-1;
622
623 // Not considierered speculative.
624 if (ExitingBlocks.size() == 1)
625 return MaxNumOfPromotionsPerLoop;
626
627 if (ExitingBlocks.size() > SpeculativeCounterPromotionMaxExiting)
628 return 0;
629
630 // Whether the target block is in a loop does not matter:
631 if (SpeculativeCounterPromotionToLoop)
632 return MaxNumOfPromotionsPerLoop;
633
634 // Now check the target block:
635 unsigned MaxProm = MaxNumOfPromotionsPerLoop;
636 for (auto *TargetBlock : LoopExitBlocks) {
637 auto *TargetLoop = LI.getLoopFor(TargetBlock);
638 if (!TargetLoop)
639 continue;
640 unsigned MaxPromForTarget = getMaxNumOfPromotionsInLoop(TargetLoop);
641 unsigned PendingCandsInTarget = LoopToCandidates[TargetLoop].size();
642 MaxProm =
643 std::min(MaxProm, std::max(MaxPromForTarget, PendingCandsInTarget) -
644 PendingCandsInTarget);
645 }
646 return MaxProm;
647 }
648
649 DenseMap<Loop *, SmallVector<LoadStorePair, 8>> &LoopToCandidates;
650 SmallVector<BasicBlock *, 8> ExitBlocks;
651 SmallVector<Instruction *, 8> InsertPts;
652 Loop &L;
653 LoopInfo &LI;
654 BlockFrequencyInfo *BFI;
655};
656
657enum class ValueProfilingCallType {
658 // Individual values are tracked. Currently used for indiret call target
659 // profiling.
660 Default,
661
662 // MemOp: the memop size value profiling.
663 MemOp
664};
665
666} // end anonymous namespace
667
672 auto GetTLI = [&FAM](Function &F) -> TargetLibraryInfo & {
673 return FAM.getResult<TargetLibraryAnalysis>(F);
674 };
675 InstrLowerer Lowerer(M, Options, GetTLI, IsCS);
676 if (!Lowerer.lower())
677 return PreservedAnalyses::all();
678
680}
681
682//
683// Perform instrumentation sampling.
684//
685// There are 3 favors of sampling:
686// (1) Full burst sampling: We transform:
687// Increment_Instruction;
688// to:
689// if (__llvm_profile_sampling__ <= SampledInstrBurstDuration - 1) {
690// Increment_Instruction;
691// }
692// __llvm_profile_sampling__ += 1;
693// if (__llvm_profile_sampling__ >= SampledInstrPeriod) {
694// __llvm_profile_sampling__ = 0;
695// }
696//
697// "__llvm_profile_sampling__" is a thread-local global shared by all PGO
698// counters (value-instrumentation and edge instrumentation).
699//
700// (2) Fast burst sampling:
701// "__llvm_profile_sampling__" variable is an unsigned type, meaning it will
702// wrap around to zero when overflows. In this case, the second check is
703// unnecessary, so we won't generate check2 when the SampledInstrPeriod is
704// set to 65536 (64K). The code after:
705// if (__llvm_profile_sampling__ <= SampledInstrBurstDuration - 1) {
706// Increment_Instruction;
707// }
708// __llvm_profile_sampling__ += 1;
709//
710// (3) Simple sampling:
711// When SampledInstrBurstDuration is set to 1, we do a simple sampling:
712// __llvm_profile_sampling__ += 1;
713// if (__llvm_profile_sampling__ >= SampledInstrPeriod) {
714// __llvm_profile_sampling__ = 0;
715// Increment_Instruction;
716// }
717//
718// Note that, the code snippet after the transformation can still be counter
719// promoted. However, with sampling enabled, counter updates are expected to
720// be infrequent, making the benefits of counter promotion negligible.
721// Moreover, counter promotion can potentially cause issues in server
722// applications, particularly when the counters are dumped without a clean
723// exit. To mitigate this risk, counter promotion is disabled by default when
724// sampling is enabled. This behavior can be overridden using the internal
725// option.
726void InstrLowerer::doSampling(Instruction *I) {
727 if (!isSamplingEnabled())
728 return;
729
730 SampledInstrumentationConfig config = getSampledInstrumentationConfig();
731 auto GetConstant = [&config](IRBuilder<> &Builder, uint32_t C) {
732 if (config.UseShort)
733 return Builder.getInt16(C);
734 else
735 return Builder.getInt32(C);
736 };
737
738 IntegerType *SamplingVarTy;
739 if (config.UseShort)
740 SamplingVarTy = Type::getInt16Ty(M.getContext());
741 else
742 SamplingVarTy = Type::getInt32Ty(M.getContext());
743 auto *SamplingVar =
745 assert(SamplingVar && "SamplingVar not set properly");
746
747 // Create the condition for checking the burst duration.
748 Instruction *SamplingVarIncr;
749 Value *NewSamplingVarVal;
750 MDBuilder MDB(I->getContext());
751 MDNode *BranchWeight;
752 IRBuilder<> CondBuilder(I);
753 auto *LoadSamplingVar = CondBuilder.CreateLoad(SamplingVarTy, SamplingVar);
754 if (config.IsSimpleSampling) {
755 // For the simple sampling, just create the load and increments.
756 IRBuilder<> IncBuilder(I);
757 NewSamplingVarVal =
758 IncBuilder.CreateAdd(LoadSamplingVar, GetConstant(IncBuilder, 1));
759 SamplingVarIncr = IncBuilder.CreateStore(NewSamplingVarVal, SamplingVar);
760 } else {
761 // For the burst-sampling, create the conditional update.
762 auto *DurationCond = CondBuilder.CreateICmpULE(
763 LoadSamplingVar, GetConstant(CondBuilder, config.BurstDuration - 1));
764 BranchWeight = MDB.createBranchWeights(
765 config.BurstDuration, config.Period - config.BurstDuration);
767 DurationCond, I, /* Unreachable */ false, BranchWeight);
768 IRBuilder<> IncBuilder(I);
769 NewSamplingVarVal =
770 IncBuilder.CreateAdd(LoadSamplingVar, GetConstant(IncBuilder, 1));
771 SamplingVarIncr = IncBuilder.CreateStore(NewSamplingVarVal, SamplingVar);
772 I->moveBefore(ThenTerm->getIterator());
773 }
774
775 if (config.IsFastSampling)
776 return;
777
778 // Create the condition for checking the period.
779 Instruction *ThenTerm, *ElseTerm;
780 IRBuilder<> PeriodCondBuilder(SamplingVarIncr);
781 auto *PeriodCond = PeriodCondBuilder.CreateICmpUGE(
782 NewSamplingVarVal, GetConstant(PeriodCondBuilder, config.Period));
783 BranchWeight = MDB.createBranchWeights(1, config.Period - 1);
784 SplitBlockAndInsertIfThenElse(PeriodCond, SamplingVarIncr, &ThenTerm,
785 &ElseTerm, BranchWeight);
786
787 // For the simple sampling, the counter update happens in sampling var reset.
788 if (config.IsSimpleSampling)
789 I->moveBefore(ThenTerm->getIterator());
790
791 IRBuilder<> ResetBuilder(ThenTerm);
792 ResetBuilder.CreateStore(GetConstant(ResetBuilder, 0), SamplingVar);
793 SamplingVarIncr->moveBefore(ElseTerm->getIterator());
794}
795
796bool InstrLowerer::lowerIntrinsics(Function *F) {
797 bool MadeChange = false;
798 PromotionCandidates.clear();
800
801 // To ensure compatibility with sampling, we save the intrinsics into
802 // a buffer to prevent potential breakage of the iterator (as the
803 // intrinsics will be moved to a different BB).
804 for (BasicBlock &BB : *F) {
805 for (Instruction &Instr : llvm::make_early_inc_range(BB)) {
806 if (auto *IP = dyn_cast<InstrProfInstBase>(&Instr))
807 InstrProfInsts.push_back(IP);
808 }
809 }
810
811 for (auto *Instr : InstrProfInsts) {
812 doSampling(Instr);
813 if (auto *IPIS = dyn_cast<InstrProfIncrementInstStep>(Instr)) {
814 lowerIncrement(IPIS);
815 MadeChange = true;
816 } else if (auto *IPI = dyn_cast<InstrProfIncrementInst>(Instr)) {
817 lowerIncrement(IPI);
818 MadeChange = true;
819 } else if (auto *IPC = dyn_cast<InstrProfTimestampInst>(Instr)) {
820 lowerTimestamp(IPC);
821 MadeChange = true;
822 } else if (auto *IPC = dyn_cast<InstrProfCoverInst>(Instr)) {
823 lowerCover(IPC);
824 MadeChange = true;
825 } else if (auto *IPVP = dyn_cast<InstrProfValueProfileInst>(Instr)) {
826 lowerValueProfileInst(IPVP);
827 MadeChange = true;
828 } else if (auto *IPMP = dyn_cast<InstrProfMCDCBitmapParameters>(Instr)) {
829 IPMP->eraseFromParent();
830 MadeChange = true;
831 } else if (auto *IPBU = dyn_cast<InstrProfMCDCTVBitmapUpdate>(Instr)) {
832 lowerMCDCTestVectorBitmapUpdate(IPBU);
833 MadeChange = true;
834 }
835 }
836
837 if (!MadeChange)
838 return false;
839
840 promoteCounterLoadStores(F);
841 return true;
842}
843
844bool InstrLowerer::isRuntimeCounterRelocationEnabled() const {
845 // Mach-O don't support weak external references.
846 if (TT.isOSBinFormatMachO())
847 return false;
848
849 if (RuntimeCounterRelocation.getNumOccurrences() > 0)
850 return RuntimeCounterRelocation;
851
852 // Fuchsia uses runtime counter relocation by default.
853 return TT.isOSFuchsia();
854}
855
856bool InstrLowerer::isSamplingEnabled() const {
857 if (SampledInstr.getNumOccurrences() > 0)
858 return SampledInstr;
859 return Options.Sampling;
860}
861
862bool InstrLowerer::isCounterPromotionEnabled() const {
863 if (DoCounterPromotion.getNumOccurrences() > 0)
864 return DoCounterPromotion;
865
866 return Options.DoCounterPromotion;
867}
868
869void InstrLowerer::promoteCounterLoadStores(Function *F) {
870 if (!isCounterPromotionEnabled())
871 return;
872
873 DominatorTree DT(*F);
874 LoopInfo LI(DT);
875 DenseMap<Loop *, SmallVector<LoadStorePair, 8>> LoopPromotionCandidates;
876
877 std::unique_ptr<BlockFrequencyInfo> BFI;
878 if (Options.UseBFIInPromotion) {
879 std::unique_ptr<BranchProbabilityInfo> BPI;
880 BPI.reset(new BranchProbabilityInfo(*F, LI, &GetTLI(*F)));
881 BFI.reset(new BlockFrequencyInfo(*F, *BPI, LI));
882 }
883
884 for (const auto &LoadStore : PromotionCandidates) {
885 auto *CounterLoad = LoadStore.first;
886 auto *CounterStore = LoadStore.second;
887 BasicBlock *BB = CounterLoad->getParent();
888 Loop *ParentLoop = LI.getLoopFor(BB);
889 if (!ParentLoop)
890 continue;
891 LoopPromotionCandidates[ParentLoop].emplace_back(CounterLoad, CounterStore);
892 }
893
895
896 // Do a post-order traversal of the loops so that counter updates can be
897 // iteratively hoisted outside the loop nest.
898 for (auto *Loop : llvm::reverse(Loops)) {
899 PGOCounterPromoter Promoter(LoopPromotionCandidates, *Loop, LI, BFI.get());
900 Promoter.run(&TotalCountersPromoted);
901 }
902}
903
905 // On Fuchsia, we only need runtime hook if any counters are present.
906 if (TT.isOSFuchsia())
907 return false;
908
909 return true;
910}
911
912/// Check if the module contains uses of any profiling intrinsics.
914 auto containsIntrinsic = [&](int ID) {
915 if (auto *F = Intrinsic::getDeclarationIfExists(&M, ID))
916 return !F->use_empty();
917 return false;
918 };
919 return containsIntrinsic(Intrinsic::instrprof_cover) ||
920 containsIntrinsic(Intrinsic::instrprof_increment) ||
921 containsIntrinsic(Intrinsic::instrprof_increment_step) ||
922 containsIntrinsic(Intrinsic::instrprof_timestamp) ||
923 containsIntrinsic(Intrinsic::instrprof_value_profile);
924}
925
926bool InstrLowerer::lower() {
927 bool MadeChange = false;
928 bool NeedsRuntimeHook = needsRuntimeHookUnconditionally(TT);
929 if (NeedsRuntimeHook)
930 MadeChange = emitRuntimeHook();
931
932 if (!IsCS && isSamplingEnabled())
934
935 bool ContainsProfiling = containsProfilingIntrinsics(M);
936 GlobalVariable *CoverageNamesVar =
937 M.getNamedGlobal(getCoverageUnusedNamesVarName());
938 // Improve compile time by avoiding linear scans when there is no work.
939 if (!ContainsProfiling && !CoverageNamesVar)
940 return MadeChange;
941
942 // We did not know how many value sites there would be inside
943 // the instrumented function. This is counting the number of instrumented
944 // target value sites to enter it as field in the profile data variable.
945 for (Function &F : M) {
946 InstrProfCntrInstBase *FirstProfInst = nullptr;
947 for (BasicBlock &BB : F) {
948 for (auto I = BB.begin(), E = BB.end(); I != E; I++) {
949 if (auto *Ind = dyn_cast<InstrProfValueProfileInst>(I))
950 computeNumValueSiteCounts(Ind);
951 else {
952 if (FirstProfInst == nullptr &&
954 FirstProfInst = dyn_cast<InstrProfCntrInstBase>(I);
955 // If the MCDCBitmapParameters intrinsic seen, create the bitmaps.
956 if (const auto &Params = dyn_cast<InstrProfMCDCBitmapParameters>(I))
957 static_cast<void>(getOrCreateRegionBitmaps(Params));
958 }
959 }
960 }
961
962 // Use a profile intrinsic to create the region counters and data variable.
963 // Also create the data variable based on the MCDCParams.
964 if (FirstProfInst != nullptr) {
965 static_cast<void>(getOrCreateRegionCounters(FirstProfInst));
966 }
967 }
968
970 for (GlobalVariable &GV : M.globals())
971 // Global variables with type metadata are virtual table variables.
972 if (GV.hasMetadata(LLVMContext::MD_type))
973 getOrCreateVTableProfData(&GV);
974
975 for (Function &F : M)
976 MadeChange |= lowerIntrinsics(&F);
977
978 if (CoverageNamesVar) {
979 lowerCoverageData(CoverageNamesVar);
980 MadeChange = true;
981 }
982
983 if (!MadeChange)
984 return false;
985
986 emitVNodes();
987 emitNameData();
988 emitVTableNames();
989
990 // Emit runtime hook for the cases where the target does not unconditionally
991 // require pulling in profile runtime, and coverage is enabled on code that is
992 // not eliminated by the front-end, e.g. unused functions with internal
993 // linkage.
994 if (!NeedsRuntimeHook && ContainsProfiling)
995 emitRuntimeHook();
996
997 emitRegistration();
998 emitUses();
999 emitInitialization();
1000 return true;
1001}
1002
1004 Module &M, const TargetLibraryInfo &TLI,
1005 ValueProfilingCallType CallType = ValueProfilingCallType::Default) {
1006 LLVMContext &Ctx = M.getContext();
1007 auto *ReturnTy = Type::getVoidTy(M.getContext());
1008
1009 AttributeList AL;
1010 if (auto AK = TLI.getExtAttrForI32Param(false))
1011 AL = AL.addParamAttribute(M.getContext(), 2, AK);
1012
1013 assert((CallType == ValueProfilingCallType::Default ||
1014 CallType == ValueProfilingCallType::MemOp) &&
1015 "Must be Default or MemOp");
1016 Type *ParamTypes[] = {
1017#define VALUE_PROF_FUNC_PARAM(ParamType, ParamName, ParamLLVMType) ParamLLVMType
1019 };
1020 auto *ValueProfilingCallTy =
1021 FunctionType::get(ReturnTy, ArrayRef(ParamTypes), false);
1022 StringRef FuncName = CallType == ValueProfilingCallType::Default
1025 return M.getOrInsertFunction(FuncName, ValueProfilingCallTy, AL);
1026}
1027
1028void InstrLowerer::computeNumValueSiteCounts(InstrProfValueProfileInst *Ind) {
1029 GlobalVariable *Name = Ind->getName();
1030 uint64_t ValueKind = Ind->getValueKind()->getZExtValue();
1031 uint64_t Index = Ind->getIndex()->getZExtValue();
1032 auto &PD = ProfileDataMap[Name];
1033 PD.NumValueSites[ValueKind] =
1034 std::max(PD.NumValueSites[ValueKind], (uint32_t)(Index + 1));
1035}
1036
1037void InstrLowerer::lowerValueProfileInst(InstrProfValueProfileInst *Ind) {
1038 // TODO: Value profiling heavily depends on the data section which is omitted
1039 // in lightweight mode. We need to move the value profile pointer to the
1040 // Counter struct to get this working.
1041 assert(
1043 "Value profiling is not yet supported with lightweight instrumentation");
1044 GlobalVariable *Name = Ind->getName();
1045 auto It = ProfileDataMap.find(Name);
1046 assert(It != ProfileDataMap.end() && It->second.DataVar &&
1047 "value profiling detected in function with no counter increment");
1048
1049 GlobalVariable *DataVar = It->second.DataVar;
1050 uint64_t ValueKind = Ind->getValueKind()->getZExtValue();
1051 uint64_t Index = Ind->getIndex()->getZExtValue();
1052 for (uint32_t Kind = IPVK_First; Kind < ValueKind; ++Kind)
1053 Index += It->second.NumValueSites[Kind];
1054
1055 IRBuilder<> Builder(Ind);
1056 bool IsMemOpSize = (Ind->getValueKind()->getZExtValue() ==
1057 llvm::InstrProfValueKind::IPVK_MemOPSize);
1058 CallInst *Call = nullptr;
1059 auto *TLI = &GetTLI(*Ind->getFunction());
1060 auto *NormalizedDataVarPtr = ConstantExpr::getPointerBitCastOrAddrSpaceCast(
1061 DataVar, PointerType::get(M.getContext(), 0));
1062
1063 // To support value profiling calls within Windows exception handlers, funclet
1064 // information contained within operand bundles needs to be copied over to
1065 // the library call. This is required for the IR to be processed by the
1066 // WinEHPrepare pass.
1068 Ind->getOperandBundlesAsDefs(OpBundles);
1069 if (!IsMemOpSize) {
1070 Value *Args[3] = {Ind->getTargetValue(), NormalizedDataVarPtr,
1071 Builder.getInt32(Index)};
1072 Call = Builder.CreateCall(getOrInsertValueProfilingCall(M, *TLI), Args,
1073 OpBundles);
1074 } else {
1075 Value *Args[3] = {Ind->getTargetValue(), NormalizedDataVarPtr,
1076 Builder.getInt32(Index)};
1077 Call = Builder.CreateCall(
1078 getOrInsertValueProfilingCall(M, *TLI, ValueProfilingCallType::MemOp),
1079 Args, OpBundles);
1080 }
1081 if (auto AK = TLI->getExtAttrForI32Param(false))
1082 Call->addParamAttr(2, AK);
1084 Ind->eraseFromParent();
1085}
1086
1087GlobalVariable *InstrLowerer::getOrCreateBiasVar(StringRef VarName) {
1088 GlobalVariable *Bias = M.getGlobalVariable(VarName);
1089 if (Bias)
1090 return Bias;
1091
1092 Type *Int64Ty = Type::getInt64Ty(M.getContext());
1093
1094 // Compiler must define this variable when runtime counter relocation
1095 // is being used. Runtime has a weak external reference that is used
1096 // to check whether that's the case or not.
1097 Bias = new GlobalVariable(M, Int64Ty, false, GlobalValue::LinkOnceODRLinkage,
1098 Constant::getNullValue(Int64Ty), VarName);
1100 // A definition that's weak (linkonce_odr) without being in a COMDAT
1101 // section wouldn't lead to link errors, but it would lead to a dead
1102 // data word from every TU but one. Putting it in COMDAT ensures there
1103 // will be exactly one data slot in the link.
1104 if (TT.supportsCOMDAT())
1105 Bias->setComdat(M.getOrInsertComdat(VarName));
1106
1107 return Bias;
1108}
1109
1110Value *InstrLowerer::getCounterAddress(InstrProfCntrInstBase *I) {
1111 auto *Counters = getOrCreateRegionCounters(I);
1112 IRBuilder<> Builder(I);
1113
1115 Counters->setAlignment(Align(8));
1116
1117 auto *Addr = Builder.CreateConstInBoundsGEP2_32(
1118 Counters->getValueType(), Counters, 0, I->getIndex()->getZExtValue());
1119
1120 if (!isRuntimeCounterRelocationEnabled())
1121 return Addr;
1122
1123 Type *Int64Ty = Type::getInt64Ty(M.getContext());
1124 Function *Fn = I->getParent()->getParent();
1125 LoadInst *&BiasLI = FunctionToProfileBiasMap[Fn];
1126 if (!BiasLI) {
1127 IRBuilder<> EntryBuilder(&Fn->getEntryBlock().front());
1128 auto *Bias = getOrCreateBiasVar(getInstrProfCounterBiasVarName());
1129 BiasLI = EntryBuilder.CreateLoad(Int64Ty, Bias, "profc_bias");
1130 // Bias doesn't change after startup.
1131 BiasLI->setMetadata(LLVMContext::MD_invariant_load,
1132 MDNode::get(M.getContext(), {}));
1133 }
1134 auto *Add = Builder.CreateAdd(Builder.CreatePtrToInt(Addr, Int64Ty), BiasLI);
1135 return Builder.CreateIntToPtr(Add, Addr->getType());
1136}
1137
1138Value *InstrLowerer::getBitmapAddress(InstrProfMCDCTVBitmapUpdate *I) {
1139 auto *Bitmaps = getOrCreateRegionBitmaps(I);
1140 if (!isRuntimeCounterRelocationEnabled())
1141 return Bitmaps;
1142
1143 // Put BiasLI onto the entry block.
1144 Type *Int64Ty = Type::getInt64Ty(M.getContext());
1145 Function *Fn = I->getFunction();
1146 IRBuilder<> EntryBuilder(&Fn->getEntryBlock().front());
1147 auto *Bias = getOrCreateBiasVar(getInstrProfBitmapBiasVarName());
1148 auto *BiasLI = EntryBuilder.CreateLoad(Int64Ty, Bias, "profbm_bias");
1149 // Assume BiasLI invariant (in the function at least)
1150 BiasLI->setMetadata(LLVMContext::MD_invariant_load,
1151 MDNode::get(M.getContext(), {}));
1152
1153 // Add Bias to Bitmaps and put it before the intrinsic.
1154 IRBuilder<> Builder(I);
1155 return Builder.CreatePtrAdd(Bitmaps, BiasLI, "profbm_addr");
1156}
1157
1158void InstrLowerer::lowerCover(InstrProfCoverInst *CoverInstruction) {
1159 auto *Addr = getCounterAddress(CoverInstruction);
1160 IRBuilder<> Builder(CoverInstruction);
1161 if (ConditionalCounterUpdate) {
1162 Instruction *SplitBefore = CoverInstruction->getNextNode();
1163 auto &Ctx = CoverInstruction->getParent()->getContext();
1164 auto *Int8Ty = llvm::Type::getInt8Ty(Ctx);
1165 Value *Load = Builder.CreateLoad(Int8Ty, Addr, "pgocount");
1166 Value *Cmp = Builder.CreateIsNotNull(Load, "pgocount.ifnonzero");
1167 Instruction *ThenBranch =
1168 SplitBlockAndInsertIfThen(Cmp, SplitBefore, false);
1169 Builder.SetInsertPoint(ThenBranch);
1170 }
1171
1172 // We store zero to represent that this block is covered.
1173 Builder.CreateStore(Builder.getInt8(0), Addr);
1174 CoverInstruction->eraseFromParent();
1175}
1176
1177void InstrLowerer::lowerTimestamp(
1178 InstrProfTimestampInst *TimestampInstruction) {
1179 assert(TimestampInstruction->getIndex()->isZeroValue() &&
1180 "timestamp probes are always the first probe for a function");
1181 auto &Ctx = M.getContext();
1182 auto *TimestampAddr = getCounterAddress(TimestampInstruction);
1183 IRBuilder<> Builder(TimestampInstruction);
1184 auto *CalleeTy =
1185 FunctionType::get(Type::getVoidTy(Ctx), TimestampAddr->getType(), false);
1186 auto Callee = M.getOrInsertFunction(
1188 Builder.CreateCall(Callee, {TimestampAddr});
1189 TimestampInstruction->eraseFromParent();
1190}
1191
1192void InstrLowerer::lowerIncrement(InstrProfIncrementInst *Inc) {
1193 auto *Addr = getCounterAddress(Inc);
1194
1195 IRBuilder<> Builder(Inc);
1196 if (Options.Atomic || AtomicCounterUpdateAll ||
1197 (Inc->getIndex()->isZeroValue() && AtomicFirstCounter)) {
1198 Builder.CreateAtomicRMW(AtomicRMWInst::Add, Addr, Inc->getStep(),
1200 } else {
1201 Value *IncStep = Inc->getStep();
1202 Value *Load = Builder.CreateLoad(IncStep->getType(), Addr, "pgocount");
1203 auto *Count = Builder.CreateAdd(Load, Inc->getStep());
1204 auto *Store = Builder.CreateStore(Count, Addr);
1205 if (isCounterPromotionEnabled())
1206 PromotionCandidates.emplace_back(cast<Instruction>(Load), Store);
1207 }
1208 Inc->eraseFromParent();
1209}
1210
1211void InstrLowerer::lowerCoverageData(GlobalVariable *CoverageNamesVar) {
1212 ConstantArray *Names =
1213 cast<ConstantArray>(CoverageNamesVar->getInitializer());
1214 for (unsigned I = 0, E = Names->getNumOperands(); I < E; ++I) {
1215 Constant *NC = Names->getOperand(I);
1216 Value *V = NC->stripPointerCasts();
1217 assert(isa<GlobalVariable>(V) && "Missing reference to function name");
1219
1220 Name->setLinkage(GlobalValue::PrivateLinkage);
1221 ReferencedNames.push_back(Name);
1222 if (isa<ConstantExpr>(NC))
1223 NC->dropAllReferences();
1224 }
1225 CoverageNamesVar->eraseFromParent();
1226}
1227
1228void InstrLowerer::lowerMCDCTestVectorBitmapUpdate(
1230 auto &Ctx = M.getContext();
1231 IRBuilder<> Builder(Update);
1232 auto *Int8Ty = Type::getInt8Ty(Ctx);
1233 auto *Int32Ty = Type::getInt32Ty(Ctx);
1234 auto *MCDCCondBitmapAddr = Update->getMCDCCondBitmapAddr();
1235 auto *BitmapAddr = getBitmapAddress(Update);
1236
1237 // Load Temp Val + BitmapIdx.
1238 // %mcdc.temp = load i32, ptr %mcdc.addr, align 4
1239 auto *Temp = Builder.CreateAdd(
1240 Builder.CreateLoad(Int32Ty, MCDCCondBitmapAddr, "mcdc.temp"),
1241 Update->getBitmapIndex());
1242
1243 // Calculate byte offset using div8.
1244 // %1 = lshr i32 %mcdc.temp, 3
1245 auto *BitmapByteOffset = Builder.CreateLShr(Temp, 0x3);
1246
1247 // Add byte offset to section base byte address.
1248 // %4 = getelementptr inbounds i8, ptr @__profbm_test, i32 %1
1249 auto *BitmapByteAddr =
1250 Builder.CreateInBoundsPtrAdd(BitmapAddr, BitmapByteOffset);
1251
1252 // Calculate bit offset into bitmap byte by using div8 remainder (AND ~8)
1253 // %5 = and i32 %mcdc.temp, 7
1254 // %6 = trunc i32 %5 to i8
1255 auto *BitToSet = Builder.CreateTrunc(Builder.CreateAnd(Temp, 0x7), Int8Ty);
1256
1257 // Shift bit offset left to form a bitmap.
1258 // %7 = shl i8 1, %6
1259 auto *ShiftedVal = Builder.CreateShl(Builder.getInt8(0x1), BitToSet);
1260
1261 // Load profile bitmap byte.
1262 // %mcdc.bits = load i8, ptr %4, align 1
1263 auto *Bitmap = Builder.CreateLoad(Int8Ty, BitmapByteAddr, "mcdc.bits");
1264
1265 if (Options.Atomic || AtomicCounterUpdateAll) {
1266 // If ((Bitmap & Val) != Val), then execute atomic (Bitmap |= Val).
1267 // Note, just-loaded Bitmap might not be up-to-date. Use it just for
1268 // early testing.
1269 auto *Masked = Builder.CreateAnd(Bitmap, ShiftedVal);
1270 auto *ShouldStore = Builder.CreateICmpNE(Masked, ShiftedVal);
1271
1272 // Assume updating will be rare.
1273 auto *Unlikely = MDBuilder(Ctx).createUnlikelyBranchWeights();
1274 Instruction *ThenBranch =
1275 SplitBlockAndInsertIfThen(ShouldStore, Update, false, Unlikely);
1276
1277 // Execute if (unlikely(ShouldStore)).
1278 Builder.SetInsertPoint(ThenBranch);
1279 Builder.CreateAtomicRMW(AtomicRMWInst::Or, BitmapByteAddr, ShiftedVal,
1281 } else {
1282 // Perform logical OR of profile bitmap byte and shifted bit offset.
1283 // %8 = or i8 %mcdc.bits, %7
1284 auto *Result = Builder.CreateOr(Bitmap, ShiftedVal);
1285
1286 // Store the updated profile bitmap byte.
1287 // store i8 %8, ptr %3, align 1
1288 Builder.CreateStore(Result, BitmapByteAddr);
1289 }
1290
1291 Update->eraseFromParent();
1292}
1293
1294/// Get the name of a profiling variable for a particular function.
1295static std::string getVarName(InstrProfInstBase *Inc, StringRef Prefix,
1296 bool &Renamed) {
1297 StringRef NamePrefix = getInstrProfNameVarPrefix();
1298 StringRef Name = Inc->getName()->getName().substr(NamePrefix.size());
1299 Function *F = Inc->getParent()->getParent();
1300 Module *M = F->getParent();
1301 if (!DoHashBasedCounterSplit || !isIRPGOFlagSet(M) ||
1303 Renamed = false;
1304 return (Prefix + Name).str();
1305 }
1306 Renamed = true;
1308 SmallVector<char, 24> HashPostfix;
1309 if (Name.ends_with((Twine(".") + Twine(FuncHash)).toStringRef(HashPostfix)))
1310 return (Prefix + Name).str();
1311 return (Prefix + Name + "." + Twine(FuncHash)).str();
1312}
1313
1315 // Only record function addresses if IR PGO is enabled or if clang value
1316 // profiling is enabled. Recording function addresses greatly increases object
1317 // file size, because it prevents the inliner from deleting functions that
1318 // have been inlined everywhere.
1319 if (!profDataReferencedByCode(*F->getParent()))
1320 return false;
1321
1322 // Check the linkage
1323 bool HasAvailableExternallyLinkage = F->hasAvailableExternallyLinkage();
1324 if (!F->hasLinkOnceLinkage() && !F->hasLocalLinkage() &&
1325 !HasAvailableExternallyLinkage)
1326 return true;
1327
1328 // A function marked 'alwaysinline' with available_externally linkage can't
1329 // have its address taken. Doing so would create an undefined external ref to
1330 // the function, which would fail to link.
1331 if (HasAvailableExternallyLinkage &&
1332 F->hasFnAttribute(Attribute::AlwaysInline))
1333 return false;
1334
1335 // Prohibit function address recording if the function is both internal and
1336 // COMDAT. This avoids the profile data variable referencing internal symbols
1337 // in COMDAT.
1338 if (F->hasLocalLinkage() && F->hasComdat())
1339 return false;
1340
1341 // Check uses of this function for other than direct calls or invokes to it.
1342 // Inline virtual functions have linkeOnceODR linkage. When a key method
1343 // exists, the vtable will only be emitted in the TU where the key method
1344 // is defined. In a TU where vtable is not available, the function won't
1345 // be 'addresstaken'. If its address is not recorded here, the profile data
1346 // with missing address may be picked by the linker leading to missing
1347 // indirect call target info.
1348 return F->hasAddressTaken() || F->hasLinkOnceLinkage();
1349}
1350
1351static inline bool shouldUsePublicSymbol(Function *Fn) {
1352 // It isn't legal to make an alias of this function at all
1353 if (Fn->isDeclarationForLinker())
1354 return true;
1355
1356 // Symbols with local linkage can just use the symbol directly without
1357 // introducing relocations
1358 if (Fn->hasLocalLinkage())
1359 return true;
1360
1361 // PGO + ThinLTO + CFI cause duplicate symbols to be introduced due to some
1362 // unfavorable interaction between the new alias and the alias renaming done
1363 // in LowerTypeTests under ThinLTO. For comdat functions that would normally
1364 // be deduplicated, but the renaming scheme ends up preventing renaming, since
1365 // it creates unique names for each alias, resulting in duplicated symbols. In
1366 // the future, we should update the CFI related passes to migrate these
1367 // aliases to the same module as the jump-table they refer to will be defined.
1368 if (Fn->hasMetadata(LLVMContext::MD_type))
1369 return true;
1370
1371 // For comdat functions, an alias would need the same linkage as the original
1372 // function and hidden visibility. There is no point in adding an alias with
1373 // identical linkage an visibility to avoid introducing symbolic relocations.
1374 if (Fn->hasComdat() &&
1376 return true;
1377
1378 // its OK to use an alias
1379 return false;
1380}
1381
1383 auto *Int8PtrTy = PointerType::getUnqual(Fn->getContext());
1384 // Store a nullptr in __llvm_profd, if we shouldn't use a real address
1385 if (!shouldRecordFunctionAddr(Fn))
1386 return ConstantPointerNull::get(Int8PtrTy);
1387
1388 // If we can't use an alias, we must use the public symbol, even though this
1389 // may require a symbolic relocation.
1390 if (shouldUsePublicSymbol(Fn))
1391 return Fn;
1392
1393 // When possible use a private alias to avoid symbolic relocations.
1395 Fn->getName() + ".local", Fn);
1396
1397 // When the instrumented function is a COMDAT function, we cannot use a
1398 // private alias. If we did, we would create reference to a local label in
1399 // this function's section. If this version of the function isn't selected by
1400 // the linker, then the metadata would introduce a reference to a discarded
1401 // section. So, for COMDAT functions, we need to adjust the linkage of the
1402 // alias. Using hidden visibility avoids a dynamic relocation and an entry in
1403 // the dynamic symbol table.
1404 //
1405 // Note that this handles COMDAT functions with visibility other than Hidden,
1406 // since that case is covered in shouldUsePublicSymbol()
1407 if (Fn->hasComdat()) {
1408 GA->setLinkage(Fn->getLinkage());
1410 }
1411
1412 // appendToCompilerUsed(*Fn->getParent(), {GA});
1413
1414 return GA;
1415}
1416
1418 // compiler-rt uses linker support to get data/counters/name start/end for
1419 // ELF, COFF, Mach-O, XCOFF, and Wasm.
1420 if (TT.isOSBinFormatELF() || TT.isOSBinFormatCOFF() ||
1421 TT.isOSBinFormatMachO() || TT.isOSBinFormatXCOFF() ||
1422 TT.isOSBinFormatWasm())
1423 return false;
1424
1425 return true;
1426}
1427
1428void InstrLowerer::maybeSetComdat(GlobalVariable *GV, GlobalObject *GO,
1429 StringRef CounterGroupName) {
1430 // Place lowered global variables in a comdat group if the associated function
1431 // or global variable is a COMDAT. This will make sure that only one copy of
1432 // global variable (e.g. function counters) of the COMDAT function will be
1433 // emitted after linking.
1434 bool NeedComdat = needsComdatForCounter(*GO, M);
1435 bool UseComdat = (NeedComdat || TT.isOSBinFormatELF());
1436
1437 if (!UseComdat)
1438 return;
1439
1440 // Keep in mind that this pass may run before the inliner, so we need to
1441 // create a new comdat group (for counters, profiling data, etc). If we use
1442 // the comdat of the parent function, that will result in relocations against
1443 // discarded sections.
1444 //
1445 // If the data variable is referenced by code, non-counter variables (notably
1446 // profiling data) and counters have to be in different comdats for COFF
1447 // because the Visual C++ linker will report duplicate symbol errors if there
1448 // are multiple external symbols with the same name marked
1449 // IMAGE_COMDAT_SELECT_ASSOCIATIVE.
1450 StringRef GroupName = TT.isOSBinFormatCOFF() && DataReferencedByCode
1451 ? GV->getName()
1452 : CounterGroupName;
1453 Comdat *C = M.getOrInsertComdat(GroupName);
1454
1455 if (!NeedComdat) {
1456 // Object file format must be ELF since `UseComdat && !NeedComdat` is true.
1457 //
1458 // For ELF, when not using COMDAT, put counters, data and values into a
1459 // nodeduplicate COMDAT which is lowered to a zero-flag section group. This
1460 // allows -z start-stop-gc to discard the entire group when the function is
1461 // discarded.
1462 C->setSelectionKind(Comdat::NoDeduplicate);
1463 }
1464 GV->setComdat(C);
1465 // COFF doesn't allow the comdat group leader to have private linkage, so
1466 // upgrade private linkage to internal linkage to produce a symbol table
1467 // entry.
1468 if (TT.isOSBinFormatCOFF() && GV->hasPrivateLinkage())
1470}
1471
1473 if (!profDataReferencedByCode(*GV->getParent()))
1474 return false;
1475
1476 if (!GV->hasLinkOnceLinkage() && !GV->hasLocalLinkage() &&
1478 return true;
1479
1480 // This avoids the profile data from referencing internal symbols in
1481 // COMDAT.
1482 if (GV->hasLocalLinkage() && GV->hasComdat())
1483 return false;
1484
1485 return true;
1486}
1487
1488// FIXME: Introduce an internal alias like what's done for functions to reduce
1489// the number of relocation entries.
1491 // Store a nullptr in __profvt_ if a real address shouldn't be used.
1492 if (!shouldRecordVTableAddr(GV))
1494
1495 return GV;
1496}
1497
1498void InstrLowerer::getOrCreateVTableProfData(GlobalVariable *GV) {
1500 "Value profiling is not supported with lightweight instrumentation");
1502 return;
1503
1504 // Skip llvm internal global variable or __prof variables.
1505 if (GV->getName().starts_with("llvm.") ||
1506 GV->getName().starts_with("__llvm") ||
1507 GV->getName().starts_with("__prof"))
1508 return;
1509
1510 // VTableProfData already created
1511 auto It = VTableDataMap.find(GV);
1512 if (It != VTableDataMap.end() && It->second)
1513 return;
1514
1517
1518 // This is to keep consistent with per-function profile data
1519 // for correctness.
1520 if (TT.isOSBinFormatXCOFF()) {
1522 Visibility = GlobalValue::DefaultVisibility;
1523 }
1524
1525 LLVMContext &Ctx = M.getContext();
1526 Type *DataTypes[] = {
1527#define INSTR_PROF_VTABLE_DATA(Type, LLVMType, Name, Init) LLVMType,
1529#undef INSTR_PROF_VTABLE_DATA
1530 };
1531
1532 auto *DataTy = StructType::get(Ctx, ArrayRef(DataTypes));
1533
1534 // Used by INSTR_PROF_VTABLE_DATA MACRO
1535 Constant *VTableAddr = getVTableAddrForProfData(GV);
1536 const std::string PGOVTableName = getPGOName(*GV);
1537 // Record the length of the vtable. This is needed since vtable pointers
1538 // loaded from C++ objects might be from the middle of a vtable definition.
1539 uint32_t VTableSizeVal =
1540 M.getDataLayout().getTypeAllocSize(GV->getValueType());
1541
1542 Constant *DataVals[] = {
1543#define INSTR_PROF_VTABLE_DATA(Type, LLVMType, Name, Init) Init,
1545#undef INSTR_PROF_VTABLE_DATA
1546 };
1547
1548 auto *Data =
1549 new GlobalVariable(M, DataTy, /*constant=*/false, Linkage,
1550 ConstantStruct::get(DataTy, DataVals),
1551 getInstrProfVTableVarPrefix() + PGOVTableName);
1552
1553 Data->setVisibility(Visibility);
1554 Data->setSection(getInstrProfSectionName(IPSK_vtab, TT.getObjectFormat()));
1555 Data->setAlignment(Align(8));
1556
1557 maybeSetComdat(Data, GV, Data->getName());
1558
1559 VTableDataMap[GV] = Data;
1560
1561 ReferencedVTables.push_back(GV);
1562
1563 // VTable <Hash, Addr> is used by runtime but not referenced by other
1564 // sections. Conservatively mark it linker retained.
1565 UsedVars.push_back(Data);
1566}
1567
1568GlobalVariable *InstrLowerer::setupProfileSection(InstrProfInstBase *Inc,
1569 InstrProfSectKind IPSK) {
1570 GlobalVariable *NamePtr = Inc->getName();
1571
1572 // Match the linkage and visibility of the name global.
1573 Function *Fn = Inc->getParent()->getParent();
1575 GlobalValue::VisibilityTypes Visibility = NamePtr->getVisibility();
1576
1577 // Use internal rather than private linkage so the counter variable shows up
1578 // in the symbol table when using debug info for correlation.
1580 TT.isOSBinFormatMachO() && Linkage == GlobalValue::PrivateLinkage)
1582
1583 // Due to the limitation of binder as of 2021/09/28, the duplicate weak
1584 // symbols in the same csect won't be discarded. When there are duplicate weak
1585 // symbols, we can NOT guarantee that the relocations get resolved to the
1586 // intended weak symbol, so we can not ensure the correctness of the relative
1587 // CounterPtr, so we have to use private linkage for counter and data symbols.
1588 if (TT.isOSBinFormatXCOFF()) {
1590 Visibility = GlobalValue::DefaultVisibility;
1591 }
1592 // Move the name variable to the right section.
1593 bool Renamed;
1595 StringRef VarPrefix;
1596 std::string VarName;
1597 if (IPSK == IPSK_cnts) {
1598 VarPrefix = getInstrProfCountersVarPrefix();
1599 VarName = getVarName(Inc, VarPrefix, Renamed);
1601 Ptr = createRegionCounters(CntrIncrement, VarName, Linkage);
1602 } else if (IPSK == IPSK_bitmap) {
1603 VarPrefix = getInstrProfBitmapVarPrefix();
1604 VarName = getVarName(Inc, VarPrefix, Renamed);
1605 InstrProfMCDCBitmapInstBase *BitmapUpdate =
1607 Ptr = createRegionBitmaps(BitmapUpdate, VarName, Linkage);
1608 } else {
1609 llvm_unreachable("Profile Section must be for Counters or Bitmaps");
1610 }
1611
1612 Ptr->setVisibility(Visibility);
1613 // Put the counters and bitmaps in their own sections so linkers can
1614 // remove unneeded sections.
1615 Ptr->setSection(getInstrProfSectionName(IPSK, TT.getObjectFormat()));
1616 Ptr->setLinkage(Linkage);
1617 maybeSetComdat(Ptr, Fn, VarName);
1618 return Ptr;
1619}
1620
1622InstrLowerer::createRegionBitmaps(InstrProfMCDCBitmapInstBase *Inc,
1623 StringRef Name,
1625 uint64_t NumBytes = Inc->getNumBitmapBytes();
1626 auto *BitmapTy = ArrayType::get(Type::getInt8Ty(M.getContext()), NumBytes);
1627 auto GV = new GlobalVariable(M, BitmapTy, false, Linkage,
1628 Constant::getNullValue(BitmapTy), Name);
1629 GV->setAlignment(Align(1));
1630 return GV;
1631}
1632
1634InstrLowerer::getOrCreateRegionBitmaps(InstrProfMCDCBitmapInstBase *Inc) {
1635 GlobalVariable *NamePtr = Inc->getName();
1636 auto &PD = ProfileDataMap[NamePtr];
1637 if (PD.RegionBitmaps)
1638 return PD.RegionBitmaps;
1639
1640 // If RegionBitmaps doesn't already exist, create it by first setting up
1641 // the corresponding profile section.
1642 auto *BitmapPtr = setupProfileSection(Inc, IPSK_bitmap);
1643 PD.RegionBitmaps = BitmapPtr;
1644 PD.NumBitmapBytes = Inc->getNumBitmapBytes();
1645 return PD.RegionBitmaps;
1646}
1647
1649InstrLowerer::createRegionCounters(InstrProfCntrInstBase *Inc, StringRef Name,
1651 uint64_t NumCounters = Inc->getNumCounters()->getZExtValue();
1652 auto &Ctx = M.getContext();
1653 GlobalVariable *GV;
1654 if (isa<InstrProfCoverInst>(Inc)) {
1655 auto *CounterTy = Type::getInt8Ty(Ctx);
1656 auto *CounterArrTy = ArrayType::get(CounterTy, NumCounters);
1657 // TODO: `Constant::getAllOnesValue()` does not yet accept an array type.
1658 std::vector<Constant *> InitialValues(NumCounters,
1659 Constant::getAllOnesValue(CounterTy));
1660 GV = new GlobalVariable(M, CounterArrTy, false, Linkage,
1661 ConstantArray::get(CounterArrTy, InitialValues),
1662 Name);
1663 GV->setAlignment(Align(1));
1664 } else {
1665 auto *CounterTy = ArrayType::get(Type::getInt64Ty(Ctx), NumCounters);
1666 GV = new GlobalVariable(M, CounterTy, false, Linkage,
1667 Constant::getNullValue(CounterTy), Name);
1668 GV->setAlignment(Align(8));
1669 }
1670 return GV;
1671}
1672
1674InstrLowerer::getOrCreateRegionCounters(InstrProfCntrInstBase *Inc) {
1675 GlobalVariable *NamePtr = Inc->getName();
1676 auto &PD = ProfileDataMap[NamePtr];
1677 if (PD.RegionCounters)
1678 return PD.RegionCounters;
1679
1680 // If RegionCounters doesn't already exist, create it by first setting up
1681 // the corresponding profile section.
1682 auto *CounterPtr = setupProfileSection(Inc, IPSK_cnts);
1683 PD.RegionCounters = CounterPtr;
1684
1686 LLVMContext &Ctx = M.getContext();
1687 Function *Fn = Inc->getParent()->getParent();
1688 if (auto *SP = Fn->getSubprogram()) {
1689 DIBuilder DB(M, true, SP->getUnit());
1690 Metadata *FunctionNameAnnotation[] = {
1693 };
1694 Metadata *CFGHashAnnotation[] = {
1697 };
1698 Metadata *NumCountersAnnotation[] = {
1701 };
1702 auto Annotations = DB.getOrCreateArray({
1703 MDNode::get(Ctx, FunctionNameAnnotation),
1704 MDNode::get(Ctx, CFGHashAnnotation),
1705 MDNode::get(Ctx, NumCountersAnnotation),
1706 });
1707 auto *DICounter = DB.createGlobalVariableExpression(
1708 SP, CounterPtr->getName(), /*LinkageName=*/StringRef(), SP->getFile(),
1709 /*LineNo=*/0, DB.createUnspecifiedType("Profile Data Type"),
1710 CounterPtr->hasLocalLinkage(), /*IsDefined=*/true, /*Expr=*/nullptr,
1711 /*Decl=*/nullptr, /*TemplateParams=*/nullptr, /*AlignInBits=*/0,
1712 Annotations);
1713 CounterPtr->addDebugInfo(DICounter);
1714 DB.finalize();
1715 }
1716
1717 // Mark the counter variable as used so that it isn't optimized out.
1718 CompilerUsedVars.push_back(PD.RegionCounters);
1719 }
1720
1721 // Create the data variable (if it doesn't already exist).
1722 createDataVariable(Inc);
1723
1724 return PD.RegionCounters;
1725}
1726
1727void InstrLowerer::createDataVariable(InstrProfCntrInstBase *Inc) {
1728 // When debug information is correlated to profile data, a data variable
1729 // is not needed.
1731 return;
1732
1733 GlobalVariable *NamePtr = Inc->getName();
1734 auto &PD = ProfileDataMap[NamePtr];
1735
1736 // Return if data variable was already created.
1737 if (PD.DataVar)
1738 return;
1739
1740 LLVMContext &Ctx = M.getContext();
1741
1742 Function *Fn = Inc->getParent()->getParent();
1744 GlobalValue::VisibilityTypes Visibility = NamePtr->getVisibility();
1745
1746 // Due to the limitation of binder as of 2021/09/28, the duplicate weak
1747 // symbols in the same csect won't be discarded. When there are duplicate weak
1748 // symbols, we can NOT guarantee that the relocations get resolved to the
1749 // intended weak symbol, so we can not ensure the correctness of the relative
1750 // CounterPtr, so we have to use private linkage for counter and data symbols.
1751 if (TT.isOSBinFormatXCOFF()) {
1753 Visibility = GlobalValue::DefaultVisibility;
1754 }
1755
1756 bool NeedComdat = needsComdatForCounter(*Fn, M);
1757 bool Renamed;
1758
1759 // The Data Variable section is anchored to profile counters.
1760 std::string CntsVarName =
1762 std::string DataVarName =
1763 getVarName(Inc, getInstrProfDataVarPrefix(), Renamed);
1764
1765 auto *Int8PtrTy = PointerType::getUnqual(Ctx);
1766 // Allocate statically the array of pointers to value profile nodes for
1767 // the current function.
1768 Constant *ValuesPtrExpr = ConstantPointerNull::get(Int8PtrTy);
1769 uint64_t NS = 0;
1770 for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
1771 NS += PD.NumValueSites[Kind];
1772 if (NS > 0 && ValueProfileStaticAlloc &&
1774 ArrayType *ValuesTy = ArrayType::get(Type::getInt64Ty(Ctx), NS);
1775 auto *ValuesVar = new GlobalVariable(
1776 M, ValuesTy, false, Linkage, Constant::getNullValue(ValuesTy),
1777 getVarName(Inc, getInstrProfValuesVarPrefix(), Renamed));
1778 ValuesVar->setVisibility(Visibility);
1779 setGlobalVariableLargeSection(TT, *ValuesVar);
1780 ValuesVar->setSection(
1781 getInstrProfSectionName(IPSK_vals, TT.getObjectFormat()));
1782 ValuesVar->setAlignment(Align(8));
1783 maybeSetComdat(ValuesVar, Fn, CntsVarName);
1785 ValuesVar, PointerType::get(Fn->getContext(), 0));
1786 }
1787
1788 uint64_t NumCounters = Inc->getNumCounters()->getZExtValue();
1789 auto *CounterPtr = PD.RegionCounters;
1790
1791 uint64_t NumBitmapBytes = PD.NumBitmapBytes;
1792
1793 // Create data variable.
1794 auto *IntPtrTy = M.getDataLayout().getIntPtrType(M.getContext());
1795 auto *Int16Ty = Type::getInt16Ty(Ctx);
1796 auto *Int16ArrayTy = ArrayType::get(Int16Ty, IPVK_Last + 1);
1797 Type *DataTypes[] = {
1798#define INSTR_PROF_DATA(Type, LLVMType, Name, Init) LLVMType,
1800 };
1801 auto *DataTy = StructType::get(Ctx, ArrayRef(DataTypes));
1802
1803 Constant *FunctionAddr = getFuncAddrForProfData(Fn);
1804
1805 Constant *Int16ArrayVals[IPVK_Last + 1];
1806 for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
1807 Int16ArrayVals[Kind] = ConstantInt::get(Int16Ty, PD.NumValueSites[Kind]);
1808
1809 if (isGPUProfTarget(M)) {
1812 }
1813 // If the data variable is not referenced by code (if we don't emit
1814 // @llvm.instrprof.value.profile, NS will be 0), and the counter keeps the
1815 // data variable live under linker GC, the data variable can be private. This
1816 // optimization applies to ELF.
1817 //
1818 // On COFF, a comdat leader cannot be local so we require DataReferencedByCode
1819 // to be false.
1820 //
1821 // If profd is in a deduplicate comdat, NS==0 with a hash suffix guarantees
1822 // that other copies must have the same CFG and cannot have value profiling.
1823 // If no hash suffix, other profd copies may be referenced by code.
1824 else if (NS == 0 && !(DataReferencedByCode && NeedComdat && !Renamed) &&
1825 (TT.isOSBinFormatELF() ||
1826 (!DataReferencedByCode && TT.isOSBinFormatCOFF()))) {
1828 Visibility = GlobalValue::DefaultVisibility;
1829 }
1830 auto *Data =
1831 new GlobalVariable(M, DataTy, false, Linkage, nullptr, DataVarName);
1832 Constant *RelativeCounterPtr;
1833 GlobalVariable *BitmapPtr = PD.RegionBitmaps;
1834 Constant *RelativeBitmapPtr = ConstantInt::get(IntPtrTy, 0);
1835 InstrProfSectKind DataSectionKind;
1836 // With binary profile correlation, profile data is not loaded into memory.
1837 // profile data must reference profile counter with an absolute relocation.
1839 DataSectionKind = IPSK_covdata;
1840 RelativeCounterPtr = ConstantExpr::getPtrToInt(CounterPtr, IntPtrTy);
1841 if (BitmapPtr != nullptr)
1842 RelativeBitmapPtr = ConstantExpr::getPtrToInt(BitmapPtr, IntPtrTy);
1843 } else {
1844 // Reference the counter variable with a label difference (link-time
1845 // constant).
1846 DataSectionKind = IPSK_data;
1847 RelativeCounterPtr =
1848 ConstantExpr::getSub(ConstantExpr::getPtrToInt(CounterPtr, IntPtrTy),
1849 ConstantExpr::getPtrToInt(Data, IntPtrTy));
1850 if (BitmapPtr != nullptr)
1851 RelativeBitmapPtr =
1853 ConstantExpr::getPtrToInt(Data, IntPtrTy));
1854 }
1855
1856 Constant *DataVals[] = {
1857#define INSTR_PROF_DATA(Type, LLVMType, Name, Init) Init,
1859 };
1860 Data->setInitializer(ConstantStruct::get(DataTy, DataVals));
1861
1862 Data->setVisibility(Visibility);
1863 Data->setSection(
1864 getInstrProfSectionName(DataSectionKind, TT.getObjectFormat()));
1865 Data->setAlignment(Align(INSTR_PROF_DATA_ALIGNMENT));
1866 maybeSetComdat(Data, Fn, CntsVarName);
1867
1868 PD.DataVar = Data;
1869
1870 // Mark the data variable as used so that it isn't stripped out.
1871 CompilerUsedVars.push_back(Data);
1872 // Now that the linkage set by the FE has been passed to the data and counter
1873 // variables, reset Name variable's linkage and visibility to private so that
1874 // it can be removed later by the compiler.
1876 // Collect the referenced names to be used by emitNameData.
1877 ReferencedNames.push_back(NamePtr);
1878}
1879
1880void InstrLowerer::emitVNodes() {
1881 if (!ValueProfileStaticAlloc)
1882 return;
1883
1884 // For now only support this on platforms that do
1885 // not require runtime registration to discover
1886 // named section start/end.
1888 return;
1889
1890 size_t TotalNS = 0;
1891 for (auto &PD : ProfileDataMap) {
1892 for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
1893 TotalNS += PD.second.NumValueSites[Kind];
1894 }
1895
1896 if (!TotalNS)
1897 return;
1898
1899 uint64_t NumCounters = TotalNS * NumCountersPerValueSite;
1900// Heuristic for small programs with very few total value sites.
1901// The default value of vp-counters-per-site is chosen based on
1902// the observation that large apps usually have a low percentage
1903// of value sites that actually have any profile data, and thus
1904// the average number of counters per site is low. For small
1905// apps with very few sites, this may not be true. Bump up the
1906// number of counters in this case.
1907#define INSTR_PROF_MIN_VAL_COUNTS 10
1910
1911 auto &Ctx = M.getContext();
1912 Type *VNodeTypes[] = {
1913#define INSTR_PROF_VALUE_NODE(Type, LLVMType, Name, Init) LLVMType,
1915 };
1916 auto *VNodeTy = StructType::get(Ctx, ArrayRef(VNodeTypes));
1917
1918 ArrayType *VNodesTy = ArrayType::get(VNodeTy, NumCounters);
1919 auto *VNodesVar = new GlobalVariable(
1920 M, VNodesTy, false, GlobalValue::PrivateLinkage,
1922 setGlobalVariableLargeSection(TT, *VNodesVar);
1923 VNodesVar->setSection(
1924 getInstrProfSectionName(IPSK_vnodes, TT.getObjectFormat()));
1925 VNodesVar->setAlignment(M.getDataLayout().getABITypeAlign(VNodesTy));
1926 // VNodesVar is used by runtime but not referenced via relocation by other
1927 // sections. Conservatively make it linker retained.
1928 UsedVars.push_back(VNodesVar);
1929}
1930
1931void InstrLowerer::emitNameData() {
1932 if (ReferencedNames.empty())
1933 return;
1934
1935 std::string CompressedNameStr;
1936 if (Error E = collectPGOFuncNameStrings(ReferencedNames, CompressedNameStr,
1938 report_fatal_error(Twine(toString(std::move(E))), false);
1939 }
1940
1941 auto &Ctx = M.getContext();
1942 auto *NamesVal =
1943 ConstantDataArray::getString(Ctx, StringRef(CompressedNameStr), false);
1944 NamesVar = new GlobalVariable(M, NamesVal->getType(), true,
1947
1948 NamesSize = CompressedNameStr.size();
1949 setGlobalVariableLargeSection(TT, *NamesVar);
1950 NamesVar->setSection(
1952 ? getInstrProfSectionName(IPSK_covname, TT.getObjectFormat())
1953 : getInstrProfSectionName(IPSK_name, TT.getObjectFormat()));
1954 // On COFF, it's important to reduce the alignment down to 1 to prevent the
1955 // linker from inserting padding before the start of the names section or
1956 // between names entries.
1957 NamesVar->setAlignment(Align(1));
1958 // NamesVar is used by runtime but not referenced via relocation by other
1959 // sections. Conservatively make it linker retained.
1960 UsedVars.push_back(NamesVar);
1961
1962 for (auto *NamePtr : ReferencedNames)
1963 NamePtr->eraseFromParent();
1964}
1965
1966void InstrLowerer::emitVTableNames() {
1967 if (!EnableVTableValueProfiling || ReferencedVTables.empty())
1968 return;
1969
1970 // Collect the PGO names of referenced vtables and compress them.
1971 std::string CompressedVTableNames;
1972 if (Error E = collectVTableStrings(ReferencedVTables, CompressedVTableNames,
1974 report_fatal_error(Twine(toString(std::move(E))), false);
1975 }
1976
1977 auto &Ctx = M.getContext();
1978 auto *VTableNamesVal = ConstantDataArray::getString(
1979 Ctx, StringRef(CompressedVTableNames), false /* AddNull */);
1980 GlobalVariable *VTableNamesVar =
1981 new GlobalVariable(M, VTableNamesVal->getType(), true /* constant */,
1982 GlobalValue::PrivateLinkage, VTableNamesVal,
1984 VTableNamesVar->setSection(
1985 getInstrProfSectionName(IPSK_vname, TT.getObjectFormat()));
1986 VTableNamesVar->setAlignment(Align(1));
1987 // Make VTableNames linker retained.
1988 UsedVars.push_back(VTableNamesVar);
1989}
1990
1991void InstrLowerer::emitRegistration() {
1993 return;
1994
1995 // Construct the function.
1996 auto *VoidTy = Type::getVoidTy(M.getContext());
1997 auto *VoidPtrTy = PointerType::getUnqual(M.getContext());
1998 auto *Int64Ty = Type::getInt64Ty(M.getContext());
1999 auto *RegisterFTy = FunctionType::get(VoidTy, false);
2000 auto *RegisterF = Function::Create(RegisterFTy, GlobalValue::InternalLinkage,
2002 RegisterF->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
2003 if (Options.NoRedZone)
2004 RegisterF->addFnAttr(Attribute::NoRedZone);
2005
2006 auto *RuntimeRegisterTy = FunctionType::get(VoidTy, VoidPtrTy, false);
2007 auto *RuntimeRegisterF =
2010
2011 IRBuilder<> IRB(BasicBlock::Create(M.getContext(), "", RegisterF));
2012 for (Value *Data : CompilerUsedVars)
2013 if (!isa<Function>(Data))
2014 // Check for addrspace cast when profiling GPU
2015 IRB.CreateCall(RuntimeRegisterF,
2016 IRB.CreatePointerBitCastOrAddrSpaceCast(Data, VoidPtrTy));
2017 for (Value *Data : UsedVars)
2018 if (Data != NamesVar && !isa<Function>(Data))
2019 IRB.CreateCall(RuntimeRegisterF,
2020 IRB.CreatePointerBitCastOrAddrSpaceCast(Data, VoidPtrTy));
2021
2022 if (NamesVar) {
2023 Type *ParamTypes[] = {VoidPtrTy, Int64Ty};
2024 auto *NamesRegisterTy =
2025 FunctionType::get(VoidTy, ArrayRef(ParamTypes), false);
2026 auto *NamesRegisterF =
2029 IRB.CreateCall(NamesRegisterF, {IRB.CreatePointerBitCastOrAddrSpaceCast(
2030 NamesVar, VoidPtrTy),
2031 IRB.getInt64(NamesSize)});
2032 }
2033
2034 IRB.CreateRetVoid();
2035}
2036
2037bool InstrLowerer::emitRuntimeHook() {
2038 // We expect the linker to be invoked with -u<hook_var> flag for Linux
2039 // in which case there is no need to emit the external variable.
2040 if (TT.isOSLinux() || TT.isOSAIX())
2041 return false;
2042
2043 // If the module's provided its own runtime, we don't need to do anything.
2044 if (M.getGlobalVariable(getInstrProfRuntimeHookVarName()))
2045 return false;
2046
2047 // Declare an external variable that will pull in the runtime initialization.
2048 auto *Int32Ty = Type::getInt32Ty(M.getContext());
2049 auto *Var =
2052 if (isGPUProfTarget(M))
2053 Var->setVisibility(GlobalValue::ProtectedVisibility);
2054 else
2055 Var->setVisibility(GlobalValue::HiddenVisibility);
2056
2057 if (TT.isOSBinFormatELF() && !TT.isPS()) {
2058 // Mark the user variable as used so that it isn't stripped out.
2059 CompilerUsedVars.push_back(Var);
2060 } else {
2061 // Make a function that uses it.
2065 User->addFnAttr(Attribute::NoInline);
2066 if (Options.NoRedZone)
2067 User->addFnAttr(Attribute::NoRedZone);
2068 User->setVisibility(GlobalValue::HiddenVisibility);
2069 if (TT.supportsCOMDAT())
2070 User->setComdat(M.getOrInsertComdat(User->getName()));
2071
2072 IRBuilder<> IRB(BasicBlock::Create(M.getContext(), "", User));
2073 auto *Load = IRB.CreateLoad(Int32Ty, Var);
2074 IRB.CreateRet(Load);
2075
2076 // Mark the function as used so that it isn't stripped out.
2077 CompilerUsedVars.push_back(User);
2078 }
2079 return true;
2080}
2081
2082void InstrLowerer::emitUses() {
2083 // The metadata sections are parallel arrays. Optimizers (e.g.
2084 // GlobalOpt/ConstantMerge) may not discard associated sections as a unit, so
2085 // we conservatively retain all unconditionally in the compiler.
2086 //
2087 // On ELF and Mach-O, the linker can guarantee the associated sections will be
2088 // retained or discarded as a unit, so llvm.compiler.used is sufficient.
2089 // Similarly on COFF, if prof data is not referenced by code we use one comdat
2090 // and ensure this GC property as well. Otherwise, we have to conservatively
2091 // make all of the sections retained by the linker.
2092 if (TT.isOSBinFormatELF() || TT.isOSBinFormatMachO() ||
2093 (TT.isOSBinFormatCOFF() && !DataReferencedByCode))
2094 appendToCompilerUsed(M, CompilerUsedVars);
2095 else
2096 appendToUsed(M, CompilerUsedVars);
2097
2098 // We do not add proper references from used metadata sections to NamesVar and
2099 // VNodesVar, so we have to be conservative and place them in llvm.used
2100 // regardless of the target,
2101 appendToUsed(M, UsedVars);
2102}
2103
2104void InstrLowerer::emitInitialization() {
2105 // Create ProfileFileName variable. Don't don't this for the
2106 // context-sensitive instrumentation lowering: This lowering is after
2107 // LTO/ThinLTO linking. Pass PGOInstrumentationGenCreateVar should
2108 // have already create the variable before LTO/ThinLTO linking.
2109 if (!IsCS)
2110 createProfileFileNameVar(M, Options.InstrProfileOutput);
2111 Function *RegisterF = M.getFunction(getInstrProfRegFuncsName());
2112 if (!RegisterF)
2113 return;
2114
2115 // Create the initialization function.
2116 auto *VoidTy = Type::getVoidTy(M.getContext());
2117 auto *F = Function::Create(FunctionType::get(VoidTy, false),
2120 F->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
2121 F->addFnAttr(Attribute::NoInline);
2122 if (Options.NoRedZone)
2123 F->addFnAttr(Attribute::NoRedZone);
2124
2125 // Add the basic block and the necessary calls.
2126 IRBuilder<> IRB(BasicBlock::Create(M.getContext(), "", F));
2127 IRB.CreateCall(RegisterF, {});
2128 IRB.CreateRetVoid();
2129
2130 appendToGlobalCtors(M, F, 0);
2131}
2132
2133namespace llvm {
2134// Create the variable for profile sampling.
2137 IntegerType *SamplingVarTy;
2138 Constant *ValueZero;
2139 if (getSampledInstrumentationConfig().UseShort) {
2140 SamplingVarTy = Type::getInt16Ty(M.getContext());
2141 ValueZero = Constant::getIntegerValue(SamplingVarTy, APInt(16, 0));
2142 } else {
2143 SamplingVarTy = Type::getInt32Ty(M.getContext());
2144 ValueZero = Constant::getIntegerValue(SamplingVarTy, APInt(32, 0));
2145 }
2146 auto SamplingVar = new GlobalVariable(
2147 M, SamplingVarTy, false, GlobalValue::WeakAnyLinkage, ValueZero, VarName);
2148 SamplingVar->setVisibility(GlobalValue::DefaultVisibility);
2149 SamplingVar->setThreadLocal(true);
2150 Triple TT(M.getTargetTriple());
2151 if (TT.supportsCOMDAT()) {
2152 SamplingVar->setLinkage(GlobalValue::ExternalLinkage);
2153 SamplingVar->setComdat(M.getOrInsertComdat(VarName));
2154 }
2155 appendToCompilerUsed(M, SamplingVar);
2156}
2157} // namespace llvm
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file contains the simple types necessary to represent the attributes associated with functions a...
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
#define LLVM_ABI
Definition Compiler.h:213
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static unsigned InstrCount
DXIL Finalize Linkage
@ Default
Hexagon Hardware Loops
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
Module.h This file contains the declarations for the Module class.
#define INSTR_PROF_QUOTE(x)
#define INSTR_PROF_DATA_ALIGNMENT
#define INSTR_PROF_PROFILE_SET_TIMESTAMP
#define INSTR_PROF_PROFILE_SAMPLING_VAR
static bool shouldRecordVTableAddr(GlobalVariable *GV)
static bool shouldRecordFunctionAddr(Function *F)
static bool needsRuntimeHookUnconditionally(const Triple &TT)
static bool containsProfilingIntrinsics(Module &M)
Check if the module contains uses of any profiling intrinsics.
static std::string getVarName(InstrProfInstBase *Inc, StringRef Prefix, bool &Renamed)
Get the name of a profiling variable for a particular function.
#define INSTR_PROF_MIN_VAL_COUNTS
static Constant * getFuncAddrForProfData(Function *Fn)
static bool shouldUsePublicSymbol(Function *Fn)
static FunctionCallee getOrInsertValueProfilingCall(Module &M, const TargetLibraryInfo &TLI, ValueProfilingCallType CallType=ValueProfilingCallType::Default)
static Constant * getVTableAddrForProfData(GlobalVariable *GV)
static bool needsRuntimeRegistrationOfSectionRange(const Triple &TT)
This file provides the interface for LLVM's PGO Instrumentation lowering pass.
static LVOptions Options
Definition LVOptions.cpp:25
#define F(x, y, z)
Definition MD5.cpp:55
#define I(x, y, z)
Definition MD5.cpp:58
Machine Check Debug Module
Memory SSA
Definition MemorySSA.cpp:72
This file provides the interface for IR based instrumentation passes ( (profile-gen,...
FunctionAnalysisManager FAM
std::unordered_set< BasicBlock * > BlockSet
This file contains some templates that are useful if you are working with the STL at all.
This file defines the SmallVector class.
#define LLVM_DEBUG(...)
Definition Debug.h:114
Class for arbitrary precision integers.
Definition APInt.h:78
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Annotations lets you mark points and ranges inside source code, for tests:
Definition Annotations.h:53
Class to represent array types.
static LLVM_ABI ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
@ Add
*p = old + v
LLVM Basic Block Representation.
Definition BasicBlock.h:62
iterator end()
Definition BasicBlock.h:472
iterator begin()
Instruction iterator methods.
Definition BasicBlock.h:459
LLVM_ABI const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition BasicBlock.h:206
const Instruction & front() const
Definition BasicBlock.h:482
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
Analysis providing branch probability information.
LLVM_ABI void getOperandBundlesAsDefs(SmallVectorImpl< OperandBundleDef > &Defs) const
Return the list of operand bundles attached to this instruction as a vector of OperandBundleDefs.
void addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind)
Adds the attribute to the indicated argument.
This class represents a function call, abstracting a target machine's calling convention.
@ NoDeduplicate
No deduplication is performed.
Definition Comdat.h:40
ConstantArray - Constant Array Declarations.
Definition Constants.h:433
static LLVM_ABI Constant * get(ArrayType *T, ArrayRef< Constant * > V)
static ConstantAsMetadata * get(Constant *C)
Definition Metadata.h:536
static LLVM_ABI Constant * getString(LLVMContext &Context, StringRef Initializer, bool AddNull=true)
This method constructs a CDS and initializes it with a text string.
static LLVM_ABI Constant * getPointerBitCastOrAddrSpaceCast(Constant *C, Type *Ty)
Create a BitCast or AddrSpaceCast for a pointer type depending on the address space.
static LLVM_ABI Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
static LLVM_ABI Constant * getPtrToInt(Constant *C, Type *Ty, bool OnlyIfReduced=false)
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:163
static LLVM_ABI ConstantPointerNull * get(PointerType *T)
Static factory methods - Return objects of the specified value.
static LLVM_ABI Constant * get(StructType *T, ArrayRef< Constant * > V)
This is an important base class in LLVM.
Definition Constant.h:43
static LLVM_ABI Constant * getIntegerValue(Type *Ty, const APInt &V)
Return the value for an integer or pointer constant, or a vector thereof, with the given scalar value...
static LLVM_ABI Constant * getAllOnesValue(Type *Ty)
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
LLVM_ABI bool isZeroValue() const
Return true if the value is negative zero or null value.
Definition Constants.cpp:76
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:165
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
static LLVM_ABI FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
Definition Function.h:166
const BasicBlock & getEntryBlock() const
Definition Function.h:807
DISubprogram * getSubprogram() const
Get the attached subprogram.
const Function & getFunction() const
Definition Function.h:164
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition Function.cpp:359
static LLVM_ABI GlobalAlias * create(Type *Ty, unsigned AddressSpace, LinkageTypes Linkage, const Twine &Name, Constant *Aliasee, Module *Parent)
If a parent module is specified, the alias is automatically inserted into the end of the specified mo...
Definition Globals.cpp:598
bool hasMetadata() const
Return true if this value has any metadata attached to it.
Definition Value.h:602
LLVM_ABI void setComdat(Comdat *C)
Definition Globals.cpp:214
bool hasComdat() const
LLVM_ABI void setSection(StringRef S)
Change the section for this global.
Definition Globals.cpp:275
bool hasLinkOnceLinkage() const
VisibilityTypes getVisibility() const
LLVM_ABI bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition Globals.cpp:328
LinkageTypes getLinkage() const
bool hasLocalLinkage() const
bool hasPrivateLinkage() const
void setLinkage(LinkageTypes LT)
bool isDeclarationForLinker() const
Module * getParent()
Get the module that this global value is contained inside of...
VisibilityTypes
An enumeration for the kinds of visibility of global values.
Definition GlobalValue.h:67
@ DefaultVisibility
The GV is visible.
Definition GlobalValue.h:68
@ HiddenVisibility
The GV is hidden.
Definition GlobalValue.h:69
@ ProtectedVisibility
The GV is protected.
Definition GlobalValue.h:70
void setVisibility(VisibilityTypes V)
bool hasAvailableExternallyLinkage() const
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition GlobalValue.h:52
@ PrivateLinkage
Like Internal, but omit from symbol table.
Definition GlobalValue.h:61
@ InternalLinkage
Rename collisions when linking (static functions).
Definition GlobalValue.h:60
@ ExternalLinkage
Externally visible function.
Definition GlobalValue.h:53
@ WeakAnyLinkage
Keep one copy of named function when linking (weak)
Definition GlobalValue.h:57
@ LinkOnceODRLinkage
Same, but only replaced by something equivalent.
Definition GlobalValue.h:56
Type * getValueType() const
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
LLVM_ABI void eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing module and deletes it.
Definition Globals.cpp:520
void setAlignment(Align Align)
Sets the alignment attribute of the GlobalVariable.
Value * CreateIntToPtr(Value *V, Type *DestTy, const Twine &Name="")
Definition IRBuilder.h:2202
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition IRBuilder.h:1513
ConstantInt * getInt8(uint8_t C)
Get a constant 8-bit value.
Definition IRBuilder.h:512
Value * CreatePtrAdd(Value *Ptr, Value *Offset, const Twine &Name="", GEPNoWrapFlags NW=GEPNoWrapFlags::none())
Definition IRBuilder.h:2039
Value * CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name="")
Definition IRBuilder.h:2336
ConstantInt * getInt32(uint32_t C)
Get a constant 32-bit value.
Definition IRBuilder.h:522
LoadInst * CreateLoad(Type *Ty, Value *Ptr, const char *Name)
Provided to resolve 'CreateLoad(Ty, Ptr, "...")' correctly, instead of converting the string to 'bool...
Definition IRBuilder.h:1850
Value * CreateShl(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition IRBuilder.h:1492
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition IRBuilder.h:1551
Value * CreateConstInBoundsGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0, unsigned Idx1, const Twine &Name="")
Definition IRBuilder.h:1973
StoreInst * CreateStore(Value *Val, Value *Ptr, bool isVolatile=false)
Definition IRBuilder.h:1863
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition IRBuilder.h:1403
Value * CreatePtrToInt(Value *V, Type *DestTy, const Twine &Name="")
Definition IRBuilder.h:2197
Value * CreateIsNotNull(Value *Arg, const Twine &Name="")
Return a boolean value testing if Arg != 0.
Definition IRBuilder.h:2659
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value * > Args={}, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition IRBuilder.h:2511
AtomicRMWInst * CreateAtomicRMW(AtomicRMWInst::BinOp Op, Value *Ptr, Value *Val, MaybeAlign Align, AtomicOrdering Ordering, SyncScope::ID SSID=SyncScope::System)
Definition IRBuilder.h:1914
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="", bool IsNUW=false, bool IsNSW=false)
Definition IRBuilder.h:2071
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block.
Definition IRBuilder.h:207
Value * CreateInBoundsPtrAdd(Value *Ptr, Value *Offset, const Twine &Name="")
Definition IRBuilder.h:2044
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="", bool IsDisjoint=false)
Definition IRBuilder.h:1573
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2788
A base class for all instrprof counter intrinsics.
LLVM_ABI ConstantInt * getIndex() const
LLVM_ABI ConstantInt * getNumCounters() const
static LLVM_ABI const char * FunctionNameAttributeName
static LLVM_ABI const char * CFGHashAttributeName
static LLVM_ABI const char * NumCountersAttributeName
This represents the llvm.instrprof.cover intrinsic.
This represents the llvm.instrprof.increment intrinsic.
LLVM_ABI Value * getStep() const
A base class for all instrprof intrinsics.
GlobalVariable * getName() const
ConstantInt * getHash() const
A base class for instrprof mcdc intrinsics that require global bitmap bytes.
This represents the llvm.instrprof.mcdc.tvbitmap.update intrinsic.
ConstantInt * getBitmapIndex() const
This represents the llvm.instrprof.timestamp intrinsic.
This represents the llvm.instrprof.value.profile intrinsic.
ConstantInt * getIndex() const
ConstantInt * getValueKind() const
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
LLVM_ABI void moveBefore(InstListType::iterator InsertPos)
Unlink this instruction from its current basic block and insert it into the basic block that MovePos ...
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
LLVM_ABI const Function * getFunction() const
Return the function this instruction belongs to.
LLVM_ABI void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
Class to represent integer types.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
Helper class for promoting a collection of loads and stores into SSA Form using the SSAUpdater.
Definition SSAUpdater.h:147
An instruction for reading from memory.
void getExitBlocks(SmallVectorImpl< BlockT * > &ExitBlocks) const
Return all of the successor blocks of this loop.
void getExitingBlocks(SmallVectorImpl< BlockT * > &ExitingBlocks) const
Return all blocks inside the loop that have successors outside of the loop.
BlockT * getLoopPreheader() const
If there is a preheader for this loop, return it.
bool hasDedicatedExits() const
Return true if no exit block for the loop has a predecessor that is outside the loop.
SmallVector< LoopT *, 4 > getLoopsInPreorder() const
Return all of the loops in the function in preorder across the loop nests, with siblings in forward p...
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
LLVM_ABI MDNode * createUnlikelyBranchWeights()
Return metadata containing two branch weights, with significant bias towards false destination.
Definition MDBuilder.cpp:48
Metadata node.
Definition Metadata.h:1078
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1569
static LLVM_ABI MDString * get(LLVMContext &Context, StringRef Str)
Definition Metadata.cpp:608
Root of the metadata hierarchy.
Definition Metadata.h:64
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
static LLVM_ABI PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition Analysis.h:115
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition StringRef.h:573
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition StringRef.h:261
constexpr size_t size() const
size - Get the string size.
Definition StringRef.h:146
static LLVM_ABI 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:414
Analysis pass providing the TargetLibraryInfo.
Provides information about what library functions are available for the current target.
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:47
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
static LLVM_ABI IntegerType * getInt64Ty(LLVMContext &C)
Definition Type.cpp:298
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:297
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
Definition Type.cpp:281
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:295
static LLVM_ABI IntegerType * getInt16Ty(LLVMContext &C)
Definition Type.cpp:296
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition Type.h:128
Value * getOperand(unsigned i) const
Definition User.h:232
unsigned getNumOperands() const
Definition User.h:254
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition Value.cpp:538
LLVM_ABI LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.cpp:1091
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:314
const ParentTy * getParent() const
Definition ilist_node.h:34
self_iterator getIterator()
Definition ilist_node.h:123
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
Definition ilist_node.h:348
CallInst * Call
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
LLVM_ABI Function * getDeclarationIfExists(const Module *M, ID id)
Look up the Function declaration of the intrinsic id in the Module M and return it if it exists.
@ PD
PD - Prefix code for packed double precision vector floating point operations performed in the SSE re...
ValuesClass values(OptsTy... Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
initializer< Ty > init(const Ty &Val)
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
FunctionAddr NumBitmapBytes
Definition InstrProf.h:95
StringRef getInstrProfNameVarPrefix()
Return the name prefix of variables containing instrumented function names.
Definition InstrProf.h:124
StringRef getInstrProfRuntimeHookVarName()
Return the name of the hook variable defined in profile runtime library.
Definition InstrProf.h:195
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
FunctionAddr VTableAddr uintptr_t uintptr_t Int32Ty
Definition InstrProf.h:296
LLVM_ABI void createProfileSamplingVar(Module &M)
StringRef getInstrProfBitmapVarPrefix()
Return the name prefix of profile bitmap variables.
Definition InstrProf.h:136
LLVM_ABI cl::opt< bool > DoInstrProfNameCompression
FuncHash
Definition InstrProf.h:78
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:632
InnerAnalysisManagerProxy< FunctionAnalysisManager, Module > FunctionAnalysisManagerModuleProxy
Provide the FunctionAnalysisManager to Module proxy.
FunctionAddr Int16ArrayTy
Definition InstrProf.h:93
FunctionAddr NumCounters
Definition InstrProf.h:91
StringRef getInstrProfVTableNamesVarName()
Definition InstrProf.h:148
StringRef getInstrProfDataVarPrefix()
Return the name prefix of variables containing per-function control data.
Definition InstrProf.h:130
StringRef getCoverageUnusedNamesVarName()
Return the name of the internal variable recording the array of PGO name vars referenced by the cover...
Definition InstrProf.h:161
LLVM_ABI std::string getInstrProfSectionName(InstrProfSectKind IPSK, Triple::ObjectFormatType OF, bool AddSegmentInfo=true)
Return the name of the profile section corresponding to IPSK.
LLVM_ABI bool needsComdatForCounter(const GlobalObject &GV, const Module &M)
Check if we can use Comdat for profile variables.
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
LLVM_ABI std::string getPGOName(const GlobalVariable &V, bool InLTO=false)
StringRef getInstrProfInitFuncName()
Return the name of the runtime initialization method that is generated by the compiler.
Definition InstrProf.h:190
StringRef getInstrProfValuesVarPrefix()
Return the name prefix of value profile variables.
Definition InstrProf.h:139
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:1732
StringRef getInstrProfCounterBiasVarName()
Definition InstrProf.h:205
auto reverse(ContainerTy &&C)
Definition STLExtras.h:406
StringRef getInstrProfRuntimeHookVarUseFuncName()
Return the name of the compiler generated function that references the runtime hook variable.
Definition InstrProf.h:201
StringRef getInstrProfRegFuncsName()
Return the name of function that registers all the per-function control data at program startup time ...
Definition InstrProf.h:170
LLVM_ABI Error collectPGOFuncNameStrings(ArrayRef< GlobalVariable * > NameVars, std::string &Result, bool doCompression=true)
Produce Result string with the same format described above.
InstrProfSectKind
Definition InstrProf.h:92
LLVM_ABI void SplitBlockAndInsertIfThenElse(Value *Cond, BasicBlock::iterator SplitBefore, Instruction **ThenTerm, Instruction **ElseTerm, MDNode *BranchWeights=nullptr, DomTreeUpdater *DTU=nullptr, LoopInfo *LI=nullptr)
SplitBlockAndInsertIfThenElse is similar to SplitBlockAndInsertIfThen, but also creates the ElseBlock...
StringRef getInstrProfCountersVarPrefix()
Return the name prefix of profile counter variables.
Definition InstrProf.h:133
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1739
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:167
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
LLVM_ABI StringRef getPGOFuncNameVarInitializer(GlobalVariable *NameVar)
Return the initializer in string of the PGO name var NameVar.
StringRef getInstrProfBitmapBiasVarName()
Definition InstrProf.h:209
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
StringRef getInstrProfValueProfMemOpFuncName()
Return the name profile runtime entry point to do memop size value profiling.
Definition InstrProf.h:119
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:189
IRBuilder(LLVMContext &, FolderTy, InserterTy, MDNode *, ArrayRef< OperandBundleDef >) -> IRBuilder< FolderTy, InserterTy >
StringRef getInstrProfNamesRegFuncName()
Return the name of the runtime interface that registers the PGO name strings.
Definition InstrProf.h:182
LLVM_ABI void appendToCompilerUsed(Module &M, ArrayRef< GlobalValue * > Values)
Adds global values to the llvm.compiler.used list.
@ Add
Sum of integers.
LLVM_ABI Error collectVTableStrings(ArrayRef< GlobalVariable * > VTables, std::string &Result, bool doCompression)
LLVM_ABI void setGlobalVariableLargeSection(const Triple &TargetTriple, GlobalVariable &GV)
ArrayRef(const T &OneElt) -> ArrayRef< T >
std::string toString(const APInt &I, unsigned Radix, bool Signed, bool formatAsCLiteral=false, bool UpperCase=true, bool InsertSeparators=false)
LLVM_ABI bool canRenameComdatFunc(const Function &F, bool CheckAddressTaken=false)
Check if we can safely rename this Comdat function.
LLVM_ABI void createProfileFileNameVar(Module &M, StringRef InstrProfileOutput)
LLVM_ABI 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.
LLVM_ABI bool isPresplitCoroSuspendExitEdge(const BasicBlock &Src, const BasicBlock &Dest)
Definition CFG.cpp:361
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
auto predecessors(const MachineBasicBlock *BB)
StringRef getInstrProfValueProfFuncName()
Return the name profile runtime entry point to do value profiling for a given site.
Definition InstrProf.h:113
llvm::cl::opt< llvm::InstrProfCorrelator::ProfCorrelatorKind > ProfileCorrelate
StringRef getInstrProfRegFuncName()
Return the name of the runtime interface that registers per-function control data for one instrumente...
Definition InstrProf.h:176
LLVM_ABI Instruction * SplitBlockAndInsertIfThen(Value *Cond, BasicBlock::iterator SplitBefore, bool Unreachable, MDNode *BranchWeights=nullptr, DomTreeUpdater *DTU=nullptr, LoopInfo *LI=nullptr, BasicBlock *ThenBlock=nullptr)
Split the containing block at the specified instruction - everything before SplitBefore stays in the ...
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
LLVM_ABI void appendToUsed(Module &M, ArrayRef< GlobalValue * > Values)
Adds global values to the llvm.used list.
StringRef getInstrProfNamesVarName()
Return the name of the variable holding the strings (possibly compressed) of all function's PGO names...
Definition InstrProf.h:146
LLVM_ABI bool isGPUProfTarget(const Module &M)
Determines whether module targets a GPU eligable for PGO instrumentation.
LLVM_ABI bool isIRPGOFlagSet(const Module *M)
Check if INSTR_PROF_RAW_VERSION_VAR is defined.
StringRef getInstrProfVNodesVarName()
Return the name of value profile node array variables:
Definition InstrProf.h:142
StringRef toStringRef(bool B)
Construct a string ref from a boolean.
cl::opt< bool > EnableVTableValueProfiling("enable-vtable-value-profiling", cl::init(false), cl::desc("If true, the virtual table address will be instrumented to know " "the types of a C++ pointer. The information is used in indirect " "call promotion to do selective vtable-based comparison."))
StringRef getInstrProfVTableVarPrefix()
Return the name prefix of variables containing virtual table profile data.
Definition InstrProf.h:127
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39
#define NC
Definition regutils.h:42
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition Alignment.h:106