LLVM 24.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/CycleInfo.h"
32#include "llvm/IR/DIBuilder.h"
35#include "llvm/IR/Function.h"
36#include "llvm/IR/GlobalAlias.h"
37#include "llvm/IR/GlobalValue.h"
39#include "llvm/IR/IRBuilder.h"
41#include "llvm/IR/Instruction.h"
44#include "llvm/IR/Intrinsics.h"
45#include "llvm/IR/MDBuilder.h"
46#include "llvm/IR/Module.h"
48#include "llvm/IR/Type.h"
49#include "llvm/Pass.h"
55#include "llvm/Support/Error.h"
63#include <algorithm>
64#include <cassert>
65#include <cstdint>
66#include <string>
67
68using namespace llvm;
69
70#define DEBUG_TYPE "instrprof"
71
72namespace llvm {
73// Command line option to enable vtable value profiling. Defined in
74// ProfileData/InstrProf.cpp: -enable-vtable-value-profiling=
77 "profile-correlate",
78 cl::desc("Use debug info or binary file to correlate profiles."),
81 "No profile correlation"),
83 "Use debug info to correlate"),
85 "Use binary to correlate")));
86} // namespace llvm
87
88namespace {
89
90cl::opt<bool> DoHashBasedCounterSplit(
91 "hash-based-counter-split",
92 cl::desc("Rename counter variable of a comdat function based on cfg hash"),
93 cl::init(true));
94
96 RuntimeCounterRelocation("runtime-counter-relocation",
97 cl::desc("Enable relocating counters at runtime."),
98 cl::init(false));
99
100cl::opt<bool> ValueProfileStaticAlloc(
101 "vp-static-alloc",
102 cl::desc("Do static counter allocation for value profiler"),
103 cl::init(true));
104
105cl::opt<double> NumCountersPerValueSite(
106 "vp-counters-per-site",
107 cl::desc("The average number of profile counters allocated "
108 "per value profiling site."),
109 // This is set to a very small value because in real programs, only
110 // a very small percentage of value sites have non-zero targets, e.g, 1/30.
111 // For those sites with non-zero profile, the average number of targets
112 // is usually smaller than 2.
113 cl::init(1.0));
114
115cl::opt<bool> AtomicCounterUpdateAll(
116 "instrprof-atomic-counter-update-all",
117 cl::desc("Make all profile counter updates atomic (for testing only)"),
118 cl::init(false));
119
120cl::opt<bool> VerifyAtomicPromotion(
121 "verify-atomic-counter-promoted",
122 cl::desc("Check that all profile counter updates were made atomic; no-op "
123 "if atomic updates are not requested (-fprofile-update=atomic)"),
124 cl::init(false));
125
126cl::opt<bool> AtomicCounterUpdatePromoted(
127 "atomic-counter-update-promoted",
128 cl::desc("Do counter update using atomic fetch add "
129 " for promoted counters only"),
130 cl::init(false));
131
132cl::opt<bool> AtomicFirstCounter(
133 "atomic-first-counter",
134 cl::desc("Use atomic fetch add for first counter in a function (usually "
135 "the entry counter)"),
136 cl::init(false));
137
138cl::opt<bool> ConditionalCounterUpdate(
139 "conditional-counter-update",
140 cl::desc("Do conditional counter updates in single byte counters mode)"),
141 cl::init(false));
142
143// If the option is not specified, the default behavior about whether
144// counter promotion is done depends on how instrumentation lowering
145// pipeline is setup, i.e., the default value of true of this option
146// does not mean the promotion will be done by default. Explicitly
147// setting this option can override the default behavior.
148cl::opt<bool> DoCounterPromotion("do-counter-promotion",
149 cl::desc("Do counter register promotion"),
150 cl::init(false));
151cl::opt<unsigned> MaxNumOfPromotionsPerLoop(
152 "max-counter-promotions-per-loop", cl::init(20),
153 cl::desc("Max number counter promotions per loop to avoid"
154 " increasing register pressure too much"));
155
156// A debug option
158 MaxNumOfPromotions("max-counter-promotions", cl::init(-1),
159 cl::desc("Max number of allowed counter promotions"));
160
161cl::opt<unsigned> SpeculativeCounterPromotionMaxExiting(
162 "speculative-counter-promotion-max-exiting", cl::init(3),
163 cl::desc("The max number of exiting blocks of a loop to allow "
164 " speculative counter promotion"));
165
166cl::opt<bool> SpeculativeCounterPromotionToLoop(
167 "speculative-counter-promotion-to-loop",
168 cl::desc("When the option is false, if the target block is in a loop, "
169 "the promotion will be disallowed unless the promoted counter "
170 " update can be further/iteratively promoted into an acyclic "
171 " region."));
172
173static cl::opt<unsigned> OffloadPGOSampling(
174 "offload-pgo-sampling",
175 cl::desc("Log2 of the sampling period for offload PGO instrumentation. "
176 "Only 1 in every 2^N blocks is instrumented. "
177 "0 = all blocks, 1 = 50%, 2 = 25%, 3 = 12.5% (default). "
178 "Higher values reduce overhead at the cost of sparser profiles."),
179 cl::init(3));
180
181cl::opt<bool> IterativeCounterPromotion(
182 "iterative-counter-promotion", cl::init(true),
183 cl::desc("Allow counter promotion across the whole loop nest."));
184
185cl::opt<bool> SkipRetExitBlock(
186 "skip-ret-exit-block", cl::init(true),
187 cl::desc("Suppress counter promotion if exit blocks contain ret."));
188
189static cl::opt<bool> SampledInstr("sampled-instrumentation",
190 cl::desc("Do PGO instrumentation sampling"));
191
192static cl::opt<unsigned> SampledInstrPeriod(
193 "sampled-instr-period",
194 cl::desc("Set the profile instrumentation sample period. A sample period "
195 "of 0 is invalid. For each sample period, a fixed number of "
196 "consecutive samples will be recorded. The number is controlled "
197 "by 'sampled-instr-burst-duration' flag. The default sample "
198 "period of 65536 is optimized for generating efficient code that "
199 "leverages unsigned short integer wrapping in overflow, but this "
200 "is disabled under simple sampling (burst duration = 1)."),
201 cl::init(USHRT_MAX + 1));
202
203static cl::opt<unsigned> SampledInstrBurstDuration(
204 "sampled-instr-burst-duration",
205 cl::desc("Set the profile instrumentation burst duration, which can range "
206 "from 1 to the value of 'sampled-instr-period' (0 is invalid). "
207 "This number of samples will be recorded for each "
208 "'sampled-instr-period' count update. Setting to 1 enables simple "
209 "sampling, in which case it is recommended to set "
210 "'sampled-instr-period' to a prime number."),
211 cl::init(200));
212
213struct SampledInstrumentationConfig {
214 unsigned BurstDuration;
215 unsigned Period;
216 bool UseShort;
217 bool IsSimpleSampling;
218 bool IsFastSampling;
219};
220
221static SampledInstrumentationConfig getSampledInstrumentationConfig() {
222 SampledInstrumentationConfig config;
223 config.BurstDuration = SampledInstrBurstDuration.getValue();
224 config.Period = SampledInstrPeriod.getValue();
225 if (config.BurstDuration > config.Period)
227 "SampledBurstDuration must be less than or equal to SampledPeriod");
228 if (config.Period == 0 || config.BurstDuration == 0)
230 "SampledPeriod and SampledBurstDuration must be greater than 0");
231 config.IsSimpleSampling = (config.BurstDuration == 1);
232 // If (BurstDuration == 1 && Period == 65536), generate the simple sampling
233 // style code.
234 config.IsFastSampling =
235 (!config.IsSimpleSampling && config.Period == USHRT_MAX + 1);
236 config.UseShort = (config.Period <= USHRT_MAX) || config.IsFastSampling;
237 return config;
238}
239
240using LoadStorePair = std::pair<Instruction *, Instruction *>;
241
242static void makeAtomic(Instruction *Load, Instruction *Store) {
243 auto *Addition = dyn_cast<BinaryOperator>(Store->getOperand(0));
244 assert(Addition && Addition->getOpcode() == Instruction::BinaryOps::Add);
245 auto *Addend = Addition->getOperand(1);
246
247 IRBuilder<> Builder(Load);
248 Builder.CreateAtomicRMW(AtomicRMWInst::Add, Store->getOperand(1), Addend,
250 Store->eraseFromParent();
251 Addition->eraseFromParent();
252 Load->eraseFromParent();
253}
254
255static uint64_t getIntModuleFlagOrZero(const Module &M, StringRef Flag) {
256 auto *MD = dyn_cast_or_null<ConstantAsMetadata>(M.getModuleFlag(Flag));
257 if (!MD)
258 return 0;
259
260 // If the flag is a ConstantAsMetadata, it should be an integer representable
261 // in 64-bits.
262 return cast<ConstantInt>(MD->getValue())->getZExtValue();
263}
264
265static bool enablesValueProfiling(const Module &M) {
266 return isIRPGOFlagSet(&M) ||
267 getIntModuleFlagOrZero(M, "EnableValueProfiling") != 0;
268}
269
270// Conservatively returns true if value profiling is enabled.
271static bool profDataReferencedByCode(const Module &M) {
272 return enablesValueProfiling(M);
273}
274
275class InstrLowerer final {
276public:
277 InstrLowerer(Module &M, const InstrProfOptions &Options,
278 std::function<const TargetLibraryInfo &(Function &F)> GetTLI,
279 bool IsCS)
280 : M(M), Options(Options), TT(M.getTargetTriple()), IsCS(IsCS),
281 GetTLI(GetTLI), DataReferencedByCode(profDataReferencedByCode(M)) {}
282
283 bool lower();
284
285private:
286 Module &M;
287 const InstrProfOptions Options;
288 const Triple TT;
289 // Is this lowering for the context-sensitive instrumentation.
290 const bool IsCS;
291
292 std::function<const TargetLibraryInfo &(Function &F)> GetTLI;
293
294 const bool DataReferencedByCode;
295
296 struct PerFunctionProfileData {
297 uint32_t NumValueSites[IPVK_Last + 1] = {};
298 GlobalVariable *RegionCounters = nullptr;
299 GlobalVariable *UniformCounters =
300 nullptr; // Per-block uniform-entry counters
301 GlobalVariable *DataVar = nullptr;
302 GlobalVariable *RegionBitmaps = nullptr;
303 uint32_t NumBitmapBytes = 0;
304
305 PerFunctionProfileData() = default;
306 };
307 DenseMap<GlobalVariable *, PerFunctionProfileData> ProfileDataMap;
308 // Key is virtual table variable, value is 'VTableProfData' in the form of
309 // GlobalVariable.
310 DenseMap<GlobalVariable *, GlobalVariable *> VTableDataMap;
311 /// If runtime relocation is enabled, this maps functions to the load
312 /// instruction that produces the profile relocation bias.
313 DenseMap<const Function *, LoadInst *> FunctionToProfileBiasMap;
314 std::vector<GlobalValue *> CompilerUsedVars;
315 std::vector<GlobalValue *> UsedVars;
316 std::vector<GlobalVariable *> ReferencedNames;
317 // The list of virtual table variables of which the VTableProfData is
318 // collected.
319 std::vector<GlobalVariable *> ReferencedVTables;
320 GlobalVariable *NamesVar = nullptr;
321 size_t NamesSize = 0;
322
323 StructType *ProfileDataTy = nullptr;
324
325 // vector of counter load/store pairs to be register promoted.
326 std::vector<LoadStorePair> PromotionCandidates;
327
328 int64_t TotalCountersPromoted = 0;
329
330 // Per-function cache of invariant values for GPU PGO instrumentation.
331 // Computed once at the function entry and reused across all instrumentation
332 // points to avoid redundant IR and help the optimizer.
333 struct GPUPGOInvariants {
334 Value *Matched = nullptr;
335 bool WaveSizeStored = false;
336 };
337 DenseMap<Function *, GPUPGOInvariants> GPUInvariantsCache;
338
339 /// Emit invariant PGO values at the function entry block and cache them.
340 GPUPGOInvariants &getOrCreateGPUInvariants(Function *F);
341
342 /// Lower instrumentation intrinsics in the function. Returns true if there
343 /// any lowering.
344 bool lowerIntrinsics(Function *F);
345
346 /// Register-promote counter loads and stores in loops.
347 void promoteCounterLoadStores(Function *F);
348
349 /// Returns true if relocating counters at runtime is enabled.
350 bool isRuntimeCounterRelocationEnabled() const;
351
352 /// Returns true if profile counter update register promotion is enabled.
353 bool isCounterPromotionEnabled() const;
354
355 /// Returns true if profile counter updates should be atomic.
356 bool isAtomic() const;
357
358 /// Return true if profile sampling is enabled.
359 bool isSamplingEnabled() const;
360
361 /// Count the number of instrumented value sites for the function.
362 void computeNumValueSiteCounts(InstrProfValueProfileInst *Ins);
363
364 /// Replace instrprof.value.profile with a call to runtime library.
365 void lowerValueProfileInst(InstrProfValueProfileInst *Ins);
366
367 /// Replace instrprof.cover with a store instruction to the coverage byte.
368 void lowerCover(InstrProfCoverInst *Inc);
369
370 /// Replace instrprof.timestamp with a call to
371 /// INSTR_PROF_PROFILE_SET_TIMESTAMP.
372 void lowerTimestamp(InstrProfTimestampInst *TimestampInstruction);
373
374 /// Replace instrprof.increment with an increment of the appropriate value.
375 void lowerIncrement(InstrProfIncrementInst *Inc);
376
377 /// Force emitting of name vars for unused functions.
378 void lowerCoverageData(GlobalVariable *CoverageNamesVar);
379
380 /// Replace instrprof.mcdc.tvbitmask.update with a shift and or instruction
381 /// using the index represented by the a temp value into a bitmap.
382 void lowerMCDCTestVectorBitmapUpdate(InstrProfMCDCTVBitmapUpdate *Ins);
383
384 /// Get the Bias value for data to access mmap-ed area.
385 /// Create it if it hasn't been seen.
386 GlobalVariable *getOrCreateBiasVar(StringRef VarName);
387
388 /// Compute the address of the counter value that this profiling instruction
389 /// acts on.
390 Value *getCounterAddress(InstrProfCntrInstBase *I);
391
392 /// Lower the incremental instructions under profile sampling predicates.
393 void doSampling(Instruction *I);
394
395 /// Get the region counters for an increment, creating them if necessary.
396 ///
397 /// If the counter array doesn't yet exist, the profile data variables
398 /// referring to them will also be created.
399 GlobalVariable *getOrCreateRegionCounters(InstrProfCntrInstBase *Inc);
400
401 /// Get the uniform entry counters for GPU divergence tracking.
402 /// These counters track how often blocks are entered with all lanes active.
403 GlobalVariable *getOrCreateUniformCounters(InstrProfCntrInstBase *Inc);
404
405 /// Create the region counters.
406 GlobalVariable *createRegionCounters(InstrProfCntrInstBase *Inc,
407 StringRef Name,
409
410 /// Compute the address of the test vector bitmap that this profiling
411 /// instruction acts on.
412 Value *getBitmapAddress(InstrProfMCDCTVBitmapUpdate *I);
413
414 /// Get the region bitmaps for an increment, creating them if necessary.
415 ///
416 /// If the bitmap array doesn't yet exist, the profile data variables
417 /// referring to them will also be created.
418 GlobalVariable *getOrCreateRegionBitmaps(InstrProfMCDCBitmapInstBase *Inc);
419
420 /// Create the MC/DC bitmap as a byte-aligned array of bytes associated with
421 /// an MC/DC Decision region. The number of bytes required is indicated by
422 /// the intrinsic used (type InstrProfMCDCBitmapInstBase). This is called
423 /// as part of setupProfileSection() and is conceptually very similar to
424 /// what is done for profile data counters in createRegionCounters().
425 GlobalVariable *createRegionBitmaps(InstrProfMCDCBitmapInstBase *Inc,
426 StringRef Name,
428
429 /// Set Comdat property of GV, if required.
430 void maybeSetComdat(GlobalVariable *GV, GlobalObject *GO, StringRef VarName);
431
432 /// Setup the sections into which counters and bitmaps are allocated.
433 GlobalVariable *setupProfileSection(InstrProfInstBase *Inc,
434 InstrProfSectKind IPSK);
435
436 /// Create INSTR_PROF_DATA variable for counters and bitmaps.
437 void createDataVariable(InstrProfCntrInstBase *Inc);
438
439 /// Get the counters for virtual table values, creating them if necessary.
440 void getOrCreateVTableProfData(GlobalVariable *GV);
441
442 /// Emit the section with compressed function names.
443 void emitNameData();
444
445 /// Emit the section with compressed vtable names.
446 void emitVTableNames();
447
448 /// Emit value nodes section for value profiling.
449 void emitVNodes();
450
451 /// Emit runtime registration functions for each profile data variable.
452 void emitRegistration();
453
454 /// Emit the necessary plumbing to pull in the runtime initialization.
455 /// Returns true if a change was made.
456 bool emitRuntimeHook();
457
458 /// Add uses of our data variables and runtime hook.
459 void emitUses();
460
461 /// Create a static initializer for our data, on platforms that need it,
462 /// and for any profile output file that was specified.
463 void emitInitialization();
464
465 /// Return the __llvm_profile_data struct type.
466 StructType *getProfileDataTy();
467};
468
469///
470/// A helper class to promote one counter RMW operation in the loop
471/// into register update.
472///
473/// RWM update for the counter will be sinked out of the loop after
474/// the transformation.
475///
476class PGOCounterPromoterHelper : public LoadAndStorePromoter {
477public:
478 PGOCounterPromoterHelper(
479 Instruction *L, Instruction *S, SSAUpdater &SSA, Value *Init,
480 BasicBlock *PH, ArrayRef<BasicBlock *> ExitBlocks,
481 ArrayRef<Instruction *> InsertPts,
482 DenseMap<Loop *, SmallVector<LoadStorePair, 8>> &LoopToCands,
483 LoopInfo &LI, bool IsAtomic)
484 : LoadAndStorePromoter({L, S}, SSA), Store(S), ExitBlocks(ExitBlocks),
485 InsertPts(InsertPts), LoopToCandidates(LoopToCands), LI(LI),
486 IsAtomic(IsAtomic) {
489 SSA.AddAvailableValue(PH, Init);
490 }
491
492 void doExtraRewritesBeforeFinalDeletion() override {
493 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) {
494 BasicBlock *ExitBlock = ExitBlocks[i];
495 Instruction *InsertPos = InsertPts[i];
496 // Get LiveIn value into the ExitBlock. If there are multiple
497 // predecessors, the value is defined by a PHI node in this
498 // block.
499 Value *LiveInValue = SSA.GetValueInMiddleOfBlock(ExitBlock);
500 Value *Addr = cast<StoreInst>(Store)->getPointerOperand();
501 Type *Ty = LiveInValue->getType();
502 IRBuilder<> Builder(InsertPos);
503 if (auto *AddrInst = dyn_cast_or_null<IntToPtrInst>(Addr)) {
504 // If isRuntimeCounterRelocationEnabled() is true then the address of
505 // the store instruction is computed with two instructions in
506 // InstrProfiling::getCounterAddress(). We need to copy those
507 // instructions to this block to compute Addr correctly.
508 // %BiasAdd = add i64 ptrtoint <__profc_>, <__llvm_profile_counter_bias>
509 // %Addr = inttoptr i64 %BiasAdd to i64*
510 auto *OrigBiasInst = dyn_cast<BinaryOperator>(AddrInst->getOperand(0));
511 assert(OrigBiasInst->getOpcode() == Instruction::BinaryOps::Add);
512 Value *BiasInst = Builder.Insert(OrigBiasInst->clone());
513 Addr = Builder.CreateIntToPtr(BiasInst,
514 PointerType::getUnqual(Ty->getContext()));
515 }
516 auto *TargetLoop =
517 IterativeCounterPromotion ? LI.getLoopFor(ExitBlock) : nullptr;
518 // Generate the relaxed atomic RMW if we've asked for it and no more
519 // promotion is possible.
520 if ((IsAtomic && !TargetLoop) || AtomicCounterUpdatePromoted)
521 Builder.CreateAtomicRMW(AtomicRMWInst::Add, Addr, LiveInValue,
522 MaybeAlign(), AtomicOrdering::Monotonic);
523 else {
524 LoadInst *OldVal = Builder.CreateLoad(Ty, Addr, "pgocount.promoted");
525 auto *NewVal = Builder.CreateAdd(OldVal, LiveInValue);
526 auto *NewStore = Builder.CreateStore(NewVal, Addr);
527
528 // Now update the parent loop's candidate list:
529 if (TargetLoop)
530 LoopToCandidates[TargetLoop].emplace_back(OldVal, NewStore);
531 }
532 }
533 }
534
535private:
536 Instruction *Store;
537 ArrayRef<BasicBlock *> ExitBlocks;
538 ArrayRef<Instruction *> InsertPts;
539 DenseMap<Loop *, SmallVector<LoadStorePair, 8>> &LoopToCandidates;
540 LoopInfo &LI;
541 const bool IsAtomic;
542};
543
544/// A helper class to do register promotion for all profile counter
545/// updates in a loop.
546///
547class PGOCounterPromoter {
548public:
549 PGOCounterPromoter(
550 DenseMap<Loop *, SmallVector<LoadStorePair, 8>> &LoopToCands,
551 Loop &CurLoop, LoopInfo &LI, BlockFrequencyInfo *BFI, bool IsAtomic)
552 : LoopToCandidates(LoopToCands), L(CurLoop), LI(LI), BFI(BFI),
553 IsAtomic(IsAtomic) {
554
555 // Skip collection of ExitBlocks and InsertPts for loops that will not be
556 // able to have counters promoted.
557 SmallVector<BasicBlock *, 8> LoopExitBlocks;
558 SmallPtrSet<BasicBlock *, 8> BlockSet;
559
560 L.getExitBlocks(LoopExitBlocks);
561 if (!isPromotionPossible(&L, LoopExitBlocks))
562 return;
563
564 for (BasicBlock *ExitBlock : LoopExitBlocks) {
565 if (BlockSet.insert(ExitBlock).second &&
566 llvm::none_of(predecessors(ExitBlock), [&](const BasicBlock *Pred) {
567 return llvm::isPresplitCoroSuspendExitEdge(*Pred, *ExitBlock);
568 })) {
569 ExitBlocks.push_back(ExitBlock);
570 InsertPts.push_back(&*ExitBlock->getFirstInsertionPt());
571 }
572 }
573 }
574
575 bool run(int64_t *NumPromoted) {
576 bool RC = promoteCandidates(NumPromoted);
577 // In certain case, e.g. with -fprofile-update=atomic, we want to generate
578 // atomic updates of the PGO counters, but also perform promotion of these
579 // updates out of loops to reduce train time. The strategy is:
580 // 1) generate non-atomic load-increment-store sequence of instructions
581 // during lowerIntrinsics phase,
582 // 2) perform the promotion (in promoteCandidates function), then
583 // 3) convert all (promoted and unpromotable) updates to atomicRMW.
584 // This requires that promoted candidates are set to nullptr in the
585 // LoopToCandidates[&L] array by the promoteCandidates() function.
586 if (IsAtomic)
587 for (auto &Cand : LoopToCandidates[&L])
588 if (Cand.first != nullptr && Cand.second != nullptr)
589 makeAtomic(Cand.first, Cand.second);
590 return RC;
591 }
592
593private:
594 bool promoteCandidates(int64_t *NumPromoted) {
595 // Skip 'infinite' loops:
596 if (ExitBlocks.size() == 0)
597 return false;
598
599 // Skip if any of the ExitBlocks contains a ret instruction.
600 // This is to prevent dumping of incomplete profile -- if the
601 // the loop is a long running loop and dump is called in the middle
602 // of the loop, the result profile is incomplete.
603 // FIXME: add other heuristics to detect long running loops.
604 if (SkipRetExitBlock) {
605 for (auto *BB : ExitBlocks)
606 if (isa<ReturnInst>(BB->getTerminator()))
607 return false;
608 }
609
610 unsigned MaxProm = getMaxNumOfPromotionsInLoop(&L);
611 if (MaxProm == 0)
612 return false;
613
614 [[maybe_unused]] auto *Ptr = LoopToCandidates.getPointerIntoBucketsArray();
615 unsigned Promoted = 0;
616 for (auto &Cand : LoopToCandidates[&L]) {
618 SSAUpdater SSA(&NewPHIs);
619 Value *InitVal = ConstantInt::get(Cand.first->getType(), 0);
620
621 // If BFI is set, we will use it to guide the promotions.
622 if (BFI) {
623 auto *BB = Cand.first->getParent();
624 auto InstrCount = BFI->getBlockProfileCount(BB);
625 if (!InstrCount)
626 continue;
627 auto PreheaderCount = BFI->getBlockProfileCount(L.getLoopPreheader());
628 // If the average loop trip count is not greater than 1.5, we skip
629 // promotion.
630 if (PreheaderCount && (*PreheaderCount * 3) >= (*InstrCount * 2))
631 continue;
632 }
633
634 PGOCounterPromoterHelper Promoter(
635 Cand.first, Cand.second, SSA, InitVal, L.getLoopPreheader(),
636 ExitBlocks, InsertPts, LoopToCandidates, LI, IsAtomic);
637 Promoter.run(SmallVector<Instruction *, 2>({Cand.first, Cand.second}));
638
639 assert(LoopToCandidates.isPointerIntoBucketsArray(Ptr) &&
640 "References into LoopToCandidates might be invalid");
641 Cand = {nullptr, nullptr};
642
643 Promoted++;
644 if (Promoted >= MaxProm)
645 break;
646
647 (*NumPromoted)++;
648 if (MaxNumOfPromotions != -1 && *NumPromoted >= MaxNumOfPromotions)
649 break;
650 }
651
652 LLVM_DEBUG(dbgs() << Promoted << " counters promoted for loop (depth="
653 << L.getLoopDepth() << ")\n");
654 return Promoted != 0;
655 }
656
657private:
658 bool allowSpeculativeCounterPromotion(Loop *LP) {
659 SmallVector<BasicBlock *, 8> ExitingBlocks;
660 L.getExitingBlocks(ExitingBlocks);
661 // Not considierered speculative.
662 if (ExitingBlocks.size() == 1)
663 return true;
664 if (ExitingBlocks.size() > SpeculativeCounterPromotionMaxExiting)
665 return false;
666 return true;
667 }
668
669 // Check whether the loop satisfies the basic conditions needed to perform
670 // Counter Promotions.
671 bool
672 isPromotionPossible(Loop *LP,
673 const SmallVectorImpl<BasicBlock *> &LoopExitBlocks) {
674 // We can't insert into a catchswitch.
675 if (llvm::any_of(LoopExitBlocks, [](BasicBlock *Exit) {
676 return isa<CatchSwitchInst>(Exit->getTerminator());
677 }))
678 return false;
679
680 if (!LP->hasDedicatedExits())
681 return false;
682
683 BasicBlock *PH = LP->getLoopPreheader();
684 if (!PH)
685 return false;
686
687 return true;
688 }
689
690 // Returns the max number of Counter Promotions for LP.
691 unsigned getMaxNumOfPromotionsInLoop(Loop *LP) {
692 SmallVector<BasicBlock *, 8> LoopExitBlocks;
693 LP->getExitBlocks(LoopExitBlocks);
694 if (!isPromotionPossible(LP, LoopExitBlocks))
695 return 0;
696
697 SmallVector<BasicBlock *, 8> ExitingBlocks;
698 LP->getExitingBlocks(ExitingBlocks);
699
700 // If BFI is set, we do more aggressive promotions based on BFI.
701 if (BFI)
702 return (unsigned)-1;
703
704 // Not considierered speculative.
705 if (ExitingBlocks.size() == 1)
706 return MaxNumOfPromotionsPerLoop;
707
708 if (ExitingBlocks.size() > SpeculativeCounterPromotionMaxExiting)
709 return 0;
710
711 // Whether the target block is in a loop does not matter:
712 if (SpeculativeCounterPromotionToLoop)
713 return MaxNumOfPromotionsPerLoop;
714
715 // Now check the target block:
716 unsigned MaxProm = MaxNumOfPromotionsPerLoop;
717 for (auto *TargetBlock : LoopExitBlocks) {
718 auto *TargetLoop = LI.getLoopFor(TargetBlock);
719 if (!TargetLoop)
720 continue;
721 unsigned MaxPromForTarget = getMaxNumOfPromotionsInLoop(TargetLoop);
722 unsigned PendingCandsInTarget = LoopToCandidates[TargetLoop].size();
723 MaxProm =
724 std::min(MaxProm, std::max(MaxPromForTarget, PendingCandsInTarget) -
725 PendingCandsInTarget);
726 }
727 return MaxProm;
728 }
729
730 DenseMap<Loop *, SmallVector<LoadStorePair, 8>> &LoopToCandidates;
731 SmallVector<BasicBlock *, 8> ExitBlocks;
732 SmallVector<Instruction *, 8> InsertPts;
733 Loop &L;
734 LoopInfo &LI;
735 BlockFrequencyInfo *BFI;
736 const bool IsAtomic; // Whether to convert counter updates to atomics.
737};
738
739enum class ValueProfilingCallType {
740 // Individual values are tracked. Currently used for indiret call target
741 // profiling.
742 Default,
743
744 // MemOp: the memop size value profiling.
745 MemOp
746};
747
748} // end anonymous namespace
749
754 auto GetTLI = [&FAM](Function &F) -> TargetLibraryInfo & {
755 return FAM.getResult<TargetLibraryAnalysis>(F);
756 };
757 InstrLowerer Lowerer(M, Options, GetTLI, IsCS);
758 if (!Lowerer.lower())
759 return PreservedAnalyses::all();
760
762}
763
764//
765// Perform instrumentation sampling.
766//
767// There are 3 favors of sampling:
768// (1) Full burst sampling: We transform:
769// Increment_Instruction;
770// to:
771// if (__llvm_profile_sampling__ <= SampledInstrBurstDuration - 1) {
772// Increment_Instruction;
773// }
774// __llvm_profile_sampling__ += 1;
775// if (__llvm_profile_sampling__ >= SampledInstrPeriod) {
776// __llvm_profile_sampling__ = 0;
777// }
778//
779// "__llvm_profile_sampling__" is a thread-local global shared by all PGO
780// counters (value-instrumentation and edge instrumentation).
781//
782// (2) Fast burst sampling:
783// "__llvm_profile_sampling__" variable is an unsigned type, meaning it will
784// wrap around to zero when overflows. In this case, the second check is
785// unnecessary, so we won't generate check2 when the SampledInstrPeriod is
786// set to 65536 (64K). The code after:
787// if (__llvm_profile_sampling__ <= SampledInstrBurstDuration - 1) {
788// Increment_Instruction;
789// }
790// __llvm_profile_sampling__ += 1;
791//
792// (3) Simple sampling:
793// When SampledInstrBurstDuration is set to 1, we do a simple sampling:
794// __llvm_profile_sampling__ += 1;
795// if (__llvm_profile_sampling__ >= SampledInstrPeriod) {
796// __llvm_profile_sampling__ = 0;
797// Increment_Instruction;
798// }
799//
800// Note that, the code snippet after the transformation can still be counter
801// promoted. However, with sampling enabled, counter updates are expected to
802// be infrequent, making the benefits of counter promotion negligible.
803// Moreover, counter promotion can potentially cause issues in server
804// applications, particularly when the counters are dumped without a clean
805// exit. To mitigate this risk, counter promotion is disabled by default when
806// sampling is enabled. This behavior can be overridden using the internal
807// option.
808void InstrLowerer::doSampling(Instruction *I) {
809 if (!isSamplingEnabled())
810 return;
811
812 SampledInstrumentationConfig config = getSampledInstrumentationConfig();
813 auto GetConstant = [&config](IRBuilder<> &Builder, uint32_t C) {
814 if (config.UseShort)
815 return Builder.getInt16(C);
816 else
817 return Builder.getInt32(C);
818 };
819
820 IntegerType *SamplingVarTy;
821 if (config.UseShort)
822 SamplingVarTy = Type::getInt16Ty(M.getContext());
823 else
824 SamplingVarTy = Type::getInt32Ty(M.getContext());
825 auto *SamplingVar =
827 assert(SamplingVar && "SamplingVar not set properly");
828
829 // Create the condition for checking the burst duration.
830 Instruction *SamplingVarIncr;
831 Value *NewSamplingVarVal;
832 MDBuilder MDB(I->getContext());
833 MDNode *BranchWeight;
834 IRBuilder<> CondBuilder(I);
835 auto *LoadSamplingVar = CondBuilder.CreateLoad(SamplingVarTy, SamplingVar);
836 if (config.IsSimpleSampling) {
837 // For the simple sampling, just create the load and increments.
838 IRBuilder<> IncBuilder(I);
839 NewSamplingVarVal =
840 IncBuilder.CreateAdd(LoadSamplingVar, GetConstant(IncBuilder, 1));
841 SamplingVarIncr = IncBuilder.CreateStore(NewSamplingVarVal, SamplingVar);
842 } else {
843 // For the burst-sampling, create the conditional update.
844 auto *DurationCond = CondBuilder.CreateICmpULE(
845 LoadSamplingVar, GetConstant(CondBuilder, config.BurstDuration - 1));
846 BranchWeight = MDB.createBranchWeights(
847 config.BurstDuration, config.Period - config.BurstDuration);
849 DurationCond, I, /* Unreachable */ false, BranchWeight);
850 IRBuilder<> IncBuilder(I);
851 NewSamplingVarVal =
852 IncBuilder.CreateAdd(LoadSamplingVar, GetConstant(IncBuilder, 1));
853 SamplingVarIncr = IncBuilder.CreateStore(NewSamplingVarVal, SamplingVar);
854 I->moveBefore(ThenTerm->getIterator());
855 }
856
857 if (config.IsFastSampling)
858 return;
859
860 // Create the condition for checking the period.
861 Instruction *ThenTerm, *ElseTerm;
862 IRBuilder<> PeriodCondBuilder(SamplingVarIncr);
863 auto *PeriodCond = PeriodCondBuilder.CreateICmpUGE(
864 NewSamplingVarVal, GetConstant(PeriodCondBuilder, config.Period));
865 BranchWeight = MDB.createBranchWeights(1, config.Period - 1);
866 SplitBlockAndInsertIfThenElse(PeriodCond, SamplingVarIncr, &ThenTerm,
867 &ElseTerm, BranchWeight);
868
869 // For the simple sampling, the counter update happens in sampling var reset.
870 if (config.IsSimpleSampling)
871 I->moveBefore(ThenTerm->getIterator());
872
873 IRBuilder<> ResetBuilder(ThenTerm);
874 ResetBuilder.CreateStore(GetConstant(ResetBuilder, 0), SamplingVar);
875 SamplingVarIncr->moveBefore(ElseTerm->getIterator());
876}
877
878bool InstrLowerer::lowerIntrinsics(Function *F) {
879 bool MadeChange = false;
880 PromotionCandidates.clear();
882
883 // To ensure compatibility with sampling, we save the intrinsics into
884 // a buffer to prevent potential breakage of the iterator (as the
885 // intrinsics will be moved to a different BB).
886 for (BasicBlock &BB : *F) {
887 for (Instruction &Instr : llvm::make_early_inc_range(BB)) {
888 if (auto *IP = dyn_cast<InstrProfInstBase>(&Instr))
889 InstrProfInsts.push_back(IP);
890 }
891 }
892
893 for (auto *Instr : InstrProfInsts) {
894 doSampling(Instr);
895 if (auto *IPIS = dyn_cast<InstrProfIncrementInstStep>(Instr)) {
896 lowerIncrement(IPIS);
897 MadeChange = true;
898 } else if (auto *IPI = dyn_cast<InstrProfIncrementInst>(Instr)) {
899 lowerIncrement(IPI);
900 MadeChange = true;
901 } else if (auto *IPC = dyn_cast<InstrProfTimestampInst>(Instr)) {
902 lowerTimestamp(IPC);
903 MadeChange = true;
904 } else if (auto *IPC = dyn_cast<InstrProfCoverInst>(Instr)) {
905 lowerCover(IPC);
906 MadeChange = true;
907 } else if (auto *IPVP = dyn_cast<InstrProfValueProfileInst>(Instr)) {
908 lowerValueProfileInst(IPVP);
909 MadeChange = true;
910 } else if (auto *IPMP = dyn_cast<InstrProfMCDCBitmapParameters>(Instr)) {
911 IPMP->eraseFromParent();
912 MadeChange = true;
913 } else if (auto *IPBU = dyn_cast<InstrProfMCDCTVBitmapUpdate>(Instr)) {
914 lowerMCDCTestVectorBitmapUpdate(IPBU);
915 MadeChange = true;
916 }
917 }
918
919 if (!MadeChange)
920 return false;
921
922 promoteCounterLoadStores(F);
923 return true;
924}
925
926bool InstrLowerer::isRuntimeCounterRelocationEnabled() const {
927 // Mach-O don't support weak external references.
928 if (TT.isOSBinFormatMachO())
929 return false;
930
931 if (RuntimeCounterRelocation.getNumOccurrences() > 0)
932 return RuntimeCounterRelocation;
933
934 // Fuchsia uses runtime counter relocation by default.
935 return TT.isOSFuchsia();
936}
937
938bool InstrLowerer::isSamplingEnabled() const {
939 if (SampledInstr.getNumOccurrences() > 0)
940 return SampledInstr;
941 return Options.Sampling;
942}
943
944bool InstrLowerer::isCounterPromotionEnabled() const {
945 if (DoCounterPromotion.getNumOccurrences() > 0)
946 return DoCounterPromotion;
947 return Options.DoCounterPromotion;
948}
949
950bool InstrLowerer::isAtomic() const {
951 return Options.Atomic || AtomicCounterUpdateAll;
952}
953
954static void doAtomicCheck(Function *F) {
955 for (const llvm::Instruction &I : llvm::instructions(F)) {
956 const Value *Addr = nullptr;
957 if (const LoadInst *LI = dyn_cast<LoadInst>(&I))
958 Addr = LI->getOperand(0);
959 else if (const StoreInst *LI = dyn_cast<StoreInst>(&I))
960 Addr = LI->getOperand(1);
961
962 if (Addr && Addr->stripInBoundsOffsets()->getName().starts_with(
964 LLVM_DEBUG(dbgs() << "Missed candidate: "; I.dump());
965 report_fatal_error("Candidate load/store not converted to atomic");
966 }
967 }
968}
969
970void InstrLowerer::promoteCounterLoadStores(Function *F) {
971 if (!isCounterPromotionEnabled())
972 return;
973
974 CycleInfo CI;
975 CI.compute(*F);
976 LoopInfo LI;
977 LI.analyze(F);
978 DenseMap<Loop *, SmallVector<LoadStorePair, 8>> LoopPromotionCandidates;
979
980 std::unique_ptr<BlockFrequencyInfo> BFI;
981 if (Options.UseBFIInPromotion) {
982 std::unique_ptr<BranchProbabilityInfo> BPI;
983 BPI.reset(new BranchProbabilityInfo(*F, CI, &GetTLI(*F)));
984 BFI.reset(new BlockFrequencyInfo(*F, *BPI, CI));
985 }
986
987 for (const auto &LoadStore : PromotionCandidates) {
988 auto *CounterLoad = LoadStore.first;
989 auto *CounterStore = LoadStore.second;
990 BasicBlock *BB = CounterLoad->getParent();
991 Loop *ParentLoop = LI.getLoopFor(BB);
992 if (!ParentLoop) {
993 if (isAtomic())
994 makeAtomic(CounterLoad, CounterStore);
995 continue;
996 }
997 LoopPromotionCandidates[ParentLoop].emplace_back(CounterLoad, CounterStore);
998 }
999
1001
1002 // Do a post-order traversal of the loops so that counter updates can be
1003 // iteratively hoisted outside the loop nest.
1004 for (auto *Loop : llvm::reverse(Loops)) {
1005 PGOCounterPromoter Promoter(LoopPromotionCandidates, *Loop, LI, BFI.get(),
1006 isAtomic());
1007 Promoter.run(&TotalCountersPromoted);
1008 }
1009
1010 if (isAtomic() && VerifyAtomicPromotion)
1012}
1013
1015 // On Fuchsia, we only need runtime hook if any counters are present.
1016 if (TT.isOSFuchsia())
1017 return false;
1018
1019 return true;
1020}
1021
1022/// Check if the module contains uses of any profiling intrinsics.
1024 auto containsIntrinsic = [&](int ID) {
1025 if (auto *F = Intrinsic::getDeclarationIfExists(&M, ID))
1026 return !F->use_empty();
1027 return false;
1028 };
1029 return containsIntrinsic(Intrinsic::instrprof_cover) ||
1030 containsIntrinsic(Intrinsic::instrprof_increment) ||
1031 containsIntrinsic(Intrinsic::instrprof_increment_step) ||
1032 containsIntrinsic(Intrinsic::instrprof_timestamp) ||
1033 containsIntrinsic(Intrinsic::instrprof_value_profile);
1034}
1035
1036bool InstrLowerer::lower() {
1037 bool MadeChange = false;
1038 bool NeedsRuntimeHook = needsRuntimeHookUnconditionally(TT);
1039 if (NeedsRuntimeHook)
1040 MadeChange = emitRuntimeHook();
1041
1042 if (!IsCS && isSamplingEnabled())
1044
1045 bool ContainsProfiling = containsProfilingIntrinsics(M);
1046 GlobalVariable *CoverageNamesVar =
1047 M.getNamedGlobal(getCoverageUnusedNamesVarName());
1048 // Improve compile time by avoiding linear scans when there is no work.
1049 if (!ContainsProfiling && !CoverageNamesVar)
1050 return MadeChange;
1051
1052 // We did not know how many value sites there would be inside
1053 // the instrumented function. This is counting the number of instrumented
1054 // target value sites to enter it as field in the profile data variable.
1055 for (Function &F : M) {
1056 InstrProfCntrInstBase *FirstProfInst = nullptr;
1057 for (BasicBlock &BB : F) {
1058 for (auto I = BB.begin(), E = BB.end(); I != E; I++) {
1059 if (auto *Ind = dyn_cast<InstrProfValueProfileInst>(I))
1060 computeNumValueSiteCounts(Ind);
1061 else {
1062 if (FirstProfInst == nullptr &&
1064 FirstProfInst = dyn_cast<InstrProfCntrInstBase>(I);
1065 // If the MCDCBitmapParameters intrinsic seen, create the bitmaps.
1066 if (const auto &Params = dyn_cast<InstrProfMCDCBitmapParameters>(I))
1067 static_cast<void>(getOrCreateRegionBitmaps(Params));
1068 }
1069 }
1070 }
1071
1072 // Use a profile intrinsic to create the region counters and data variable.
1073 // Also create the data variable based on the MCDCParams.
1074 if (FirstProfInst != nullptr) {
1075 static_cast<void>(getOrCreateRegionCounters(FirstProfInst));
1076 }
1077 }
1078
1080 for (GlobalVariable &GV : M.globals())
1081 // Global variables with type metadata are virtual table variables.
1082 if (GV.hasMetadata(LLVMContext::MD_type))
1083 getOrCreateVTableProfData(&GV);
1084
1085 for (Function &F : M)
1086 MadeChange |= lowerIntrinsics(&F);
1087
1088 if (CoverageNamesVar) {
1089 lowerCoverageData(CoverageNamesVar);
1090 MadeChange = true;
1091 }
1092
1093 if (!MadeChange)
1094 return false;
1095
1096 emitVNodes();
1097 emitNameData();
1098 emitVTableNames();
1099
1100 // Emit runtime hook for the cases where the target does not unconditionally
1101 // require pulling in profile runtime, and coverage is enabled on code that is
1102 // not eliminated by the front-end, e.g. unused functions with internal
1103 // linkage.
1104 if (!NeedsRuntimeHook && ContainsProfiling)
1105 emitRuntimeHook();
1106
1107 emitRegistration();
1108 emitUses();
1109 emitInitialization();
1110 return true;
1111}
1112
1114 Module &M, const TargetLibraryInfo &TLI,
1115 ValueProfilingCallType CallType = ValueProfilingCallType::Default) {
1116 LLVMContext &Ctx = M.getContext();
1117 auto *ReturnTy = Type::getVoidTy(M.getContext());
1118
1119 AttributeList AL;
1120 if (auto AK = TLI.getExtAttrForI32Param(false))
1121 AL = AL.addParamAttribute(M.getContext(), 2, AK);
1122
1123 assert((CallType == ValueProfilingCallType::Default ||
1124 CallType == ValueProfilingCallType::MemOp) &&
1125 "Must be Default or MemOp");
1126 Type *ParamTypes[] = {
1127#define VALUE_PROF_FUNC_PARAM(ParamType, ParamName, ParamLLVMType) ParamLLVMType
1129 };
1130 auto *ValueProfilingCallTy =
1131 FunctionType::get(ReturnTy, ArrayRef(ParamTypes), false);
1132 StringRef FuncName = CallType == ValueProfilingCallType::Default
1135 return M.getOrInsertFunction(FuncName, ValueProfilingCallTy, AL);
1136}
1137
1138void InstrLowerer::computeNumValueSiteCounts(InstrProfValueProfileInst *Ind) {
1139 GlobalVariable *Name = Ind->getName();
1141 uint64_t Index = Ind->getIndex()->getZExtValue();
1142 auto &PD = ProfileDataMap[Name];
1143 PD.NumValueSites[ValueKind] =
1144 std::max(PD.NumValueSites[ValueKind], (uint32_t)(Index + 1));
1145}
1146
1147void InstrLowerer::lowerValueProfileInst(InstrProfValueProfileInst *Ind) {
1148 // TODO: Value profiling heavily depends on the data section which is omitted
1149 // in lightweight mode. We need to move the value profile pointer to the
1150 // Counter struct to get this working.
1151 assert(
1153 "Value profiling is not yet supported with lightweight instrumentation");
1154 GlobalVariable *Name = Ind->getName();
1155 auto It = ProfileDataMap.find(Name);
1156 assert(It != ProfileDataMap.end() && It->second.DataVar &&
1157 "value profiling detected in function with no counter increment");
1158
1159 GlobalVariable *DataVar = It->second.DataVar;
1161 uint64_t Index = Ind->getIndex()->getZExtValue();
1162 for (uint32_t Kind = IPVK_First; Kind < ValueKind; ++Kind)
1163 Index += It->second.NumValueSites[Kind];
1164
1165 IRBuilder<> Builder(Ind);
1166 bool IsMemOpSize = (Ind->getValueKind()->getZExtValue() ==
1167 llvm::InstrProfValueKind::IPVK_MemOPSize);
1168 CallInst *Call = nullptr;
1169 auto *TLI = &GetTLI(*Ind->getFunction());
1170 auto *NormalizedDataVarPtr = ConstantExpr::getPointerBitCastOrAddrSpaceCast(
1171 DataVar, PointerType::get(M.getContext(), 0));
1172
1173 // To support value profiling calls within Windows exception handlers, funclet
1174 // information contained within operand bundles needs to be copied over to
1175 // the library call. This is required for the IR to be processed by the
1176 // WinEHPrepare pass.
1178 Ind->getOperandBundlesAsDefs(OpBundles);
1179 if (!IsMemOpSize) {
1180 Value *Args[3] = {Ind->getTargetValue(), NormalizedDataVarPtr,
1181 Builder.getInt32(Index)};
1182 Call = Builder.CreateCall(getOrInsertValueProfilingCall(M, *TLI), Args,
1183 OpBundles);
1184 } else {
1185 Value *Args[3] = {Ind->getTargetValue(), NormalizedDataVarPtr,
1186 Builder.getInt32(Index)};
1187 Call = Builder.CreateCall(
1188 getOrInsertValueProfilingCall(M, *TLI, ValueProfilingCallType::MemOp),
1189 Args, OpBundles);
1190 }
1191 if (auto AK = TLI->getExtAttrForI32Param(false))
1192 Call->addParamAttr(2, AK);
1194 Ind->eraseFromParent();
1195}
1196
1197GlobalVariable *InstrLowerer::getOrCreateBiasVar(StringRef VarName) {
1198 GlobalVariable *Bias = M.getGlobalVariable(VarName);
1199 if (Bias)
1200 return Bias;
1201
1202 Type *Int64Ty = Type::getInt64Ty(M.getContext());
1203
1204 // Compiler must define this variable when runtime counter relocation
1205 // is being used. Runtime has a weak external reference that is used
1206 // to check whether that's the case or not.
1207 Bias = new GlobalVariable(M, Int64Ty, false, GlobalValue::LinkOnceODRLinkage,
1208 Constant::getNullValue(Int64Ty), VarName);
1210 // A definition that's weak (linkonce_odr) without being in a COMDAT
1211 // section wouldn't lead to link errors, but it would lead to a dead
1212 // data word from every TU but one. Putting it in COMDAT ensures there
1213 // will be exactly one data slot in the link.
1214 if (TT.supportsCOMDAT())
1215 Bias->setComdat(M.getOrInsertComdat(VarName));
1216
1217 return Bias;
1218}
1219
1220Value *InstrLowerer::getCounterAddress(InstrProfCntrInstBase *I) {
1221 auto *Counters = getOrCreateRegionCounters(I);
1222 IRBuilder<> Builder(I);
1223
1225 Counters->setAlignment(Align(8));
1226
1227 auto *Addr = Builder.CreateConstInBoundsGEP2_32(
1228 Counters->getValueType(), Counters, 0, I->getIndex()->getZExtValue());
1229
1230 if (!isRuntimeCounterRelocationEnabled())
1231 return Addr;
1232
1233 Type *Int64Ty = Type::getInt64Ty(M.getContext());
1234 Function *Fn = I->getParent()->getParent();
1235 LoadInst *&BiasLI = FunctionToProfileBiasMap[Fn];
1236 if (!BiasLI) {
1237 IRBuilder<> EntryBuilder(&Fn->getEntryBlock().front());
1238 auto *Bias = getOrCreateBiasVar(getInstrProfCounterBiasVarName());
1239 BiasLI = EntryBuilder.CreateLoad(Int64Ty, Bias, "profc_bias");
1240 // Bias doesn't change after startup.
1241 BiasLI->setMetadata(LLVMContext::MD_invariant_load,
1242 MDNode::get(M.getContext(), {}));
1243 }
1244 auto *Add = Builder.CreateAdd(Builder.CreatePtrToInt(Addr, Int64Ty), BiasLI);
1245 return Builder.CreateIntToPtr(Add, Addr->getType());
1246}
1247
1248Value *InstrLowerer::getBitmapAddress(InstrProfMCDCTVBitmapUpdate *I) {
1249 auto *Bitmaps = getOrCreateRegionBitmaps(I);
1250 if (!isRuntimeCounterRelocationEnabled())
1251 return Bitmaps;
1252
1253 // Put BiasLI onto the entry block.
1254 Type *Int64Ty = Type::getInt64Ty(M.getContext());
1255 Function *Fn = I->getFunction();
1256 IRBuilder<> EntryBuilder(&Fn->getEntryBlock().front());
1257 auto *Bias = getOrCreateBiasVar(getInstrProfBitmapBiasVarName());
1258 auto *BiasLI = EntryBuilder.CreateLoad(Int64Ty, Bias, "profbm_bias");
1259 // Assume BiasLI invariant (in the function at least)
1260 BiasLI->setMetadata(LLVMContext::MD_invariant_load,
1261 MDNode::get(M.getContext(), {}));
1262
1263 // Add Bias to Bitmaps and put it before the intrinsic.
1264 IRBuilder<> Builder(I);
1265 return Builder.CreatePtrAdd(Bitmaps, BiasLI, "profbm_addr");
1266}
1267
1268void InstrLowerer::lowerCover(InstrProfCoverInst *CoverInstruction) {
1269 auto *Addr = getCounterAddress(CoverInstruction);
1270 IRBuilder<> Builder(CoverInstruction);
1271 if (ConditionalCounterUpdate) {
1272 Instruction *SplitBefore = CoverInstruction->getNextNode();
1273 auto &Ctx = CoverInstruction->getParent()->getContext();
1274 auto *Int8Ty = llvm::Type::getInt8Ty(Ctx);
1275 Value *Load = Builder.CreateLoad(Int8Ty, Addr, "pgocount");
1276 Value *Cmp = Builder.CreateIsNotNull(Load, "pgocount.ifnonzero");
1277 Instruction *ThenBranch =
1278 SplitBlockAndInsertIfThen(Cmp, SplitBefore, false);
1279 Builder.SetInsertPoint(ThenBranch);
1280 }
1281
1282 // We store zero to represent that this block is covered.
1283 Builder.CreateStore(Builder.getInt8(0), Addr);
1284 CoverInstruction->eraseFromParent();
1285}
1286
1287void InstrLowerer::lowerTimestamp(
1288 InstrProfTimestampInst *TimestampInstruction) {
1289 assert(TimestampInstruction->getIndex()->isNullValue() &&
1290 "timestamp probes are always the first probe for a function");
1291 auto &Ctx = M.getContext();
1292 auto *TimestampAddr = getCounterAddress(TimestampInstruction);
1293 IRBuilder<> Builder(TimestampInstruction);
1294 auto *CalleeTy =
1295 FunctionType::get(Type::getVoidTy(Ctx), TimestampAddr->getType(), false);
1296 auto Callee = M.getOrInsertFunction(
1298 Builder.CreateCall(Callee, {TimestampAddr});
1299 TimestampInstruction->eraseFromParent();
1300}
1301
1302InstrLowerer::GPUPGOInvariants &
1303InstrLowerer::getOrCreateGPUInvariants(Function *F) {
1304 auto It = GPUInvariantsCache.find(F);
1305 if (It != GPUInvariantsCache.end())
1306 return It->second;
1307
1308 LLVMContext &Context = M.getContext();
1309 auto *Int32Ty = Type::getInt32Ty(Context);
1310
1311 BasicBlock &EntryBB = F->getEntryBlock();
1312 IRBuilder<> Builder(&*EntryBB.getFirstInsertionPt());
1313
1315 if (OffloadPGOSampling > 0) {
1316 FunctionCallee IsSampledFn =
1318 RTLIB::impl___llvm_profile_sampling_gpu),
1319 Int32Ty, Int32Ty);
1320 Value *SampledInt = Builder.CreateCall(
1321 IsSampledFn, {ConstantInt::get(Int32Ty, OffloadPGOSampling)},
1322 "pgo.sampled");
1323 Matched = Builder.CreateICmpNE(SampledInt, ConstantInt::get(Int32Ty, 0),
1324 "pgo.matched");
1325 }
1326
1327 auto &Inv = GPUInvariantsCache[F];
1328 Inv.Matched = Matched;
1329 return Inv;
1330}
1331
1332void InstrLowerer::lowerIncrement(InstrProfIncrementInst *Inc) {
1333 IRBuilder<> Builder(Inc);
1334 if (isGPUProfTarget(M)) {
1335 Function *F = Inc->getFunction();
1336 auto &Inv = getOrCreateGPUInvariants(F);
1337
1338 LLVMContext &Context = M.getContext();
1339 auto *Int64Ty = Type::getInt64Ty(Context);
1340 auto *PtrTy = PointerType::getUnqual(Context);
1341
1342 auto *Addr = getCounterAddress(Inc);
1343
1344 // Store the device wave/warp size into the profile data struct once per
1345 // function. AMDGPU folds llvm.amdgcn.wavefrontsize to the subtarget's
1346 // constant; other GPUs use their fixed warp size.
1347 if (!Inv.WaveSizeStored) {
1348 Inv.WaveSizeStored = true;
1349 GlobalVariable *NamePtr = Inc->getName();
1350 auto &PD = ProfileDataMap[NamePtr];
1351 if (PD.DataVar) {
1352 IRBuilder<> EntryBuilder(&*F->getEntryBlock().getFirstInsertionPt());
1353 Value *WaveSize16 = nullptr;
1354 // Look the intrinsic up by name so this target-agnostic pass does not
1355 // pull in IntrinsicsAMDGPU.h. AMDGPU folds the intrinsic to the
1356 // subtarget's wavefront size; other GPUs fall back to a 32-lane warp.
1357 if (TT.isAMDGPU()) {
1358 Intrinsic::ID WaveSizeID =
1359 Intrinsic::lookupIntrinsicID("llvm.amdgcn.wavefrontsize");
1360 if (WaveSizeID != Intrinsic::not_intrinsic) {
1361 Function *WaveSizeFn =
1362 Intrinsic::getOrInsertDeclaration(&M, WaveSizeID);
1363 Value *WaveSize = EntryBuilder.CreateCall(WaveSizeFn);
1364 WaveSize16 = EntryBuilder.CreateTrunc(
1365 WaveSize, Type::getInt16Ty(Context), "wavesize.i16");
1366 }
1367 }
1368 if (!WaveSize16)
1369 WaveSize16 = ConstantInt::get(Type::getInt16Ty(Context), 32);
1370 Value *WaveSizeAddr = EntryBuilder.CreateStructGEP(
1371 PD.DataVar->getValueType(), PD.DataVar, 9, "profd.wavesize");
1372 EntryBuilder.CreateStore(WaveSize16, WaveSizeAddr);
1373 }
1374 }
1375
1376 GlobalVariable *UniformCounters = getOrCreateUniformCounters(Inc);
1377 Value *UniformAddrArg = ConstantPointerNull::get(PtrTy);
1378 if (UniformCounters) {
1379 Value *UniformIndices[] = {Builder.getInt32(0), Inc->getIndex()};
1380 Value *UniformAddr = Builder.CreateInBoundsGEP(
1381 UniformCounters->getValueType(), UniformCounters, UniformIndices,
1382 "unifctr.addr");
1383 UniformAddrArg =
1384 Builder.CreatePointerBitCastOrAddrSpaceCast(UniformAddr, PtrTy);
1385 }
1386 Value *CastAddr = Builder.CreatePointerBitCastOrAddrSpaceCast(Addr, PtrTy);
1387 Value *StepI64 =
1388 Builder.CreateZExtOrTrunc(Inc->getStep(), Int64Ty, "step.i64");
1389
1390 auto *CalleeTy = FunctionType::get(Type::getVoidTy(Context),
1391 {PtrTy, PtrTy, Int64Ty}, false);
1394 RTLIB::impl___llvm_profile_instrument_gpu),
1395 CalleeTy);
1396
1397 if (OffloadPGOSampling > 0) {
1398 BasicBlock *CurBB = Builder.GetInsertBlock();
1399 BasicBlock *ContBB =
1400 CurBB->splitBasicBlock(BasicBlock::iterator(Inc), "po_cont");
1401 BasicBlock *ThenBB = BasicBlock::Create(Context, "po_then", F);
1402
1403 CurBB->getTerminator()->eraseFromParent();
1404 IRBuilder<> HeadBuilder(CurBB);
1405 HeadBuilder.CreateCondBr(Inv.Matched, ThenBB, ContBB);
1406
1407 IRBuilder<> ThenBuilder(ThenBB);
1408 ThenBuilder.CreateCall(Callee, {CastAddr, UniformAddrArg, StepI64});
1409 ThenBuilder.CreateBr(ContBB);
1410 } else {
1411 Builder.CreateCall(Callee, {CastAddr, UniformAddrArg, StepI64});
1412 }
1413 Inc->eraseFromParent();
1414 return;
1415 }
1416
1417 auto *Addr = getCounterAddress(Inc);
1418 // If promotion is enabled then delay generating atomic updates until
1419 // after promotion is done.
1420 if ((!isCounterPromotionEnabled() && isAtomic()) ||
1421 (Inc->getIndex()->isNullValue() && AtomicFirstCounter)) {
1422 Builder.CreateAtomicRMW(AtomicRMWInst::Add, Addr, Inc->getStep(),
1424 } else {
1425 Value *IncStep = Inc->getStep();
1426 Value *Load = Builder.CreateLoad(IncStep->getType(), Addr, "pgocount");
1427 auto *Count = Builder.CreateAdd(Load, Inc->getStep());
1428 auto *Store = Builder.CreateStore(Count, Addr);
1429 if (isCounterPromotionEnabled())
1430 PromotionCandidates.emplace_back(cast<Instruction>(Load), Store);
1431 }
1432 Inc->eraseFromParent();
1433}
1434
1435void InstrLowerer::lowerCoverageData(GlobalVariable *CoverageNamesVar) {
1436 ConstantArray *Names =
1437 cast<ConstantArray>(CoverageNamesVar->getInitializer());
1438 for (unsigned I = 0, E = Names->getNumOperands(); I < E; ++I) {
1439 Constant *NC = Names->getOperand(I);
1440 Value *V = NC->stripPointerCasts();
1441 assert(isa<GlobalVariable>(V) && "Missing reference to function name");
1443
1444 Name->setLinkage(GlobalValue::PrivateLinkage);
1445 ReferencedNames.push_back(Name);
1446 if (isa<ConstantExpr>(NC))
1447 NC->dropAllReferences();
1448 }
1449 CoverageNamesVar->eraseFromParent();
1450}
1451
1452void InstrLowerer::lowerMCDCTestVectorBitmapUpdate(
1454 auto &Ctx = M.getContext();
1455 IRBuilder<> Builder(Update);
1456 auto *Int8Ty = Type::getInt8Ty(Ctx);
1457 auto *Int32Ty = Type::getInt32Ty(Ctx);
1458 auto *MCDCCondBitmapAddr = Update->getMCDCCondBitmapAddr();
1459 auto *BitmapAddr = getBitmapAddress(Update);
1460
1461 // Load Temp Val + BitmapIdx.
1462 // %mcdc.temp = load i32, ptr %mcdc.addr, align 4
1463 auto *Temp = Builder.CreateAdd(
1464 Builder.CreateLoad(Int32Ty, MCDCCondBitmapAddr, "mcdc.temp"),
1465 Update->getBitmapIndex());
1466
1467 // Calculate byte offset using div8.
1468 // %1 = lshr i32 %mcdc.temp, 3
1469 auto *BitmapByteOffset = Builder.CreateLShr(Temp, 0x3);
1470
1471 // Add byte offset to section base byte address.
1472 // %4 = getelementptr inbounds i8, ptr @__profbm_test, i32 %1
1473 auto *BitmapByteAddr =
1474 Builder.CreateInBoundsPtrAdd(BitmapAddr, BitmapByteOffset);
1475
1476 // Calculate bit offset into bitmap byte by using div8 remainder (AND ~8)
1477 // %5 = and i32 %mcdc.temp, 7
1478 // %6 = trunc i32 %5 to i8
1479 auto *BitToSet = Builder.CreateTrunc(Builder.CreateAnd(Temp, 0x7), Int8Ty);
1480
1481 // Shift bit offset left to form a bitmap.
1482 // %7 = shl i8 1, %6
1483 auto *ShiftedVal = Builder.CreateShl(Builder.getInt8(0x1), BitToSet);
1484
1485 // Load profile bitmap byte.
1486 // %mcdc.bits = load i8, ptr %4, align 1
1487 auto *Bitmap = Builder.CreateLoad(Int8Ty, BitmapByteAddr, "mcdc.bits");
1488
1489 if (isAtomic()) {
1490 // If ((Bitmap & Val) != Val), then execute atomic (Bitmap |= Val).
1491 // Note, just-loaded Bitmap might not be up-to-date. Use it just for
1492 // early testing.
1493 auto *Masked = Builder.CreateAnd(Bitmap, ShiftedVal);
1494 auto *ShouldStore = Builder.CreateICmpNE(Masked, ShiftedVal);
1495
1496 // Assume updating will be rare.
1497 auto *Unlikely = MDBuilder(Ctx).createUnlikelyBranchWeights();
1498 Instruction *ThenBranch =
1499 SplitBlockAndInsertIfThen(ShouldStore, Update, false, Unlikely);
1500
1501 // Execute if (unlikely(ShouldStore)).
1502 Builder.SetInsertPoint(ThenBranch);
1503 Builder.CreateAtomicRMW(AtomicRMWInst::Or, BitmapByteAddr, ShiftedVal,
1505 } else {
1506 // Perform logical OR of profile bitmap byte and shifted bit offset.
1507 // %8 = or i8 %mcdc.bits, %7
1508 auto *Result = Builder.CreateOr(Bitmap, ShiftedVal);
1509
1510 // Store the updated profile bitmap byte.
1511 // store i8 %8, ptr %3, align 1
1512 Builder.CreateStore(Result, BitmapByteAddr);
1513 }
1514
1515 Update->eraseFromParent();
1516}
1517
1518/// Get the name of a profiling variable for a particular function.
1519static std::string getVarName(InstrProfInstBase *Inc, StringRef Prefix,
1520 bool &Renamed) {
1521 StringRef NamePrefix = getInstrProfNameVarPrefix();
1522 StringRef Name = Inc->getName()->getName().substr(NamePrefix.size());
1523 Function *F = Inc->getParent()->getParent();
1524 Module *M = F->getParent();
1525 if (!DoHashBasedCounterSplit || !isIRPGOFlagSet(M) ||
1527 Renamed = false;
1528 return (Prefix + Name).str();
1529 }
1530 Renamed = true;
1532 SmallVector<char, 24> HashPostfix;
1533 if (Name.ends_with((Twine(".") + Twine(FuncHash)).toStringRef(HashPostfix)))
1534 return (Prefix + Name).str();
1535 return (Prefix + Name + "." + Twine(FuncHash)).str();
1536}
1537
1539 // Only record function addresses if IR PGO is enabled or if clang value
1540 // profiling is enabled. Recording function addresses greatly increases object
1541 // file size, because it prevents the inliner from deleting functions that
1542 // have been inlined everywhere.
1543 if (!profDataReferencedByCode(*F->getParent()))
1544 return false;
1545
1546 // Check the linkage
1547 bool HasAvailableExternallyLinkage = F->hasAvailableExternallyLinkage();
1548 if (!F->hasLinkOnceLinkage() && !F->hasLocalLinkage() &&
1549 !HasAvailableExternallyLinkage)
1550 return true;
1551
1552 // A function marked 'alwaysinline' with available_externally linkage can't
1553 // have its address taken. Doing so would create an undefined external ref to
1554 // the function, which would fail to link.
1555 if (HasAvailableExternallyLinkage &&
1556 F->hasFnAttribute(Attribute::AlwaysInline))
1557 return false;
1558
1559 // Prohibit function address recording if the function is both internal and
1560 // COMDAT. This avoids the profile data variable referencing internal symbols
1561 // in COMDAT.
1562 if (F->hasLocalLinkage() && F->hasComdat())
1563 return false;
1564
1565 // Check uses of this function for other than direct calls or invokes to it.
1566 // Inline virtual functions have linkeOnceODR linkage. When a key method
1567 // exists, the vtable will only be emitted in the TU where the key method
1568 // is defined. In a TU where vtable is not available, the function won't
1569 // be 'addresstaken'. If its address is not recorded here, the profile data
1570 // with missing address may be picked by the linker leading to missing
1571 // indirect call target info.
1572 return F->hasAddressTaken() || F->hasLinkOnceLinkage();
1573}
1574
1575static inline bool shouldUsePublicSymbol(Function *Fn) {
1576 // It isn't legal to make an alias of this function at all
1577 if (Fn->isDeclarationForLinker())
1578 return true;
1579
1580 // Symbols with local linkage can just use the symbol directly without
1581 // introducing relocations
1582 if (Fn->hasLocalLinkage())
1583 return true;
1584
1585 // PGO + ThinLTO + CFI cause duplicate symbols to be introduced due to some
1586 // unfavorable interaction between the new alias and the alias renaming done
1587 // in LowerTypeTests under ThinLTO. For comdat functions that would normally
1588 // be deduplicated, but the renaming scheme ends up preventing renaming, since
1589 // it creates unique names for each alias, resulting in duplicated symbols. In
1590 // the future, we should update the CFI related passes to migrate these
1591 // aliases to the same module as the jump-table they refer to will be defined.
1592 if (Fn->hasMetadata(LLVMContext::MD_type))
1593 return true;
1594
1595 // For comdat functions, an alias would need the same linkage as the original
1596 // function and hidden visibility. There is no point in adding an alias with
1597 // identical linkage an visibility to avoid introducing symbolic relocations.
1598 if (Fn->hasComdat() &&
1600 return true;
1601
1602 // its OK to use an alias
1603 return false;
1604}
1605
1607 auto *Int8PtrTy = PointerType::getUnqual(Fn->getContext());
1608 // Store a nullptr in __llvm_profd, if we shouldn't use a real address
1609 if (!shouldRecordFunctionAddr(Fn))
1610 return ConstantPointerNull::get(Int8PtrTy);
1611
1612 // If we can't use an alias, we must use the public symbol, even though this
1613 // may require a symbolic relocation.
1614 if (shouldUsePublicSymbol(Fn))
1615 return Fn;
1616
1617 // For GPU targets, weak functions cannot use private aliases because
1618 // LTO may pick a different TU's copy, leaving the alias undefined
1619 if (isGPUProfTarget(*Fn->getParent()) &&
1621 return Fn;
1622
1623 // When possible use a private alias to avoid symbolic relocations.
1625 Fn->getName() + ".local", Fn);
1626
1627 // When the instrumented function is a COMDAT function, we cannot use a
1628 // private alias. If we did, we would create reference to a local label in
1629 // this function's section. If this version of the function isn't selected by
1630 // the linker, then the metadata would introduce a reference to a discarded
1631 // section. So, for COMDAT functions, we need to adjust the linkage of the
1632 // alias. Using hidden visibility avoids a dynamic relocation and an entry in
1633 // the dynamic symbol table.
1634 //
1635 // Note that this handles COMDAT functions with visibility other than Hidden,
1636 // since that case is covered in shouldUsePublicSymbol()
1637 if (Fn->hasComdat()) {
1638 GA->setLinkage(Fn->getLinkage());
1640 }
1641
1642 // appendToCompilerUsed(*Fn->getParent(), {GA});
1643
1644 return GA;
1645}
1646
1648 // NVPTX is an ELF target but PTX does not expose sections or linker symbols.
1649 if (TT.isNVPTX())
1650 return true;
1651
1652 // compiler-rt uses linker support to get data/counters/name start/end for
1653 // ELF, COFF, Mach-O, XCOFF, and Wasm.
1654 if (TT.isOSBinFormatELF() || TT.isOSBinFormatCOFF() ||
1655 TT.isOSBinFormatMachO() || TT.isOSBinFormatXCOFF() ||
1656 TT.isOSBinFormatWasm())
1657 return false;
1658
1659 return true;
1660}
1661
1662void InstrLowerer::maybeSetComdat(GlobalVariable *GV, GlobalObject *GO,
1663 StringRef CounterGroupName) {
1664 // Place lowered global variables in a comdat group if the associated function
1665 // or global variable is a COMDAT. This will make sure that only one copy of
1666 // global variable (e.g. function counters) of the COMDAT function will be
1667 // emitted after linking.
1668 bool NeedComdat = needsComdatForCounter(*GO, M);
1669 bool UseComdat = (NeedComdat || TT.isOSBinFormatELF());
1670
1671 if (!UseComdat)
1672 return;
1673
1674 // Keep in mind that this pass may run before the inliner, so we need to
1675 // create a new comdat group (for counters, profiling data, etc). If we use
1676 // the comdat of the parent function, that will result in relocations against
1677 // discarded sections.
1678 //
1679 // If the data variable is referenced by code, non-counter variables (notably
1680 // profiling data) and counters have to be in different comdats for COFF
1681 // because the Visual C++ linker will report duplicate symbol errors if there
1682 // are multiple external symbols with the same name marked
1683 // IMAGE_COMDAT_SELECT_ASSOCIATIVE.
1684 StringRef GroupName = TT.isOSBinFormatCOFF() && DataReferencedByCode
1685 ? GV->getName()
1686 : CounterGroupName;
1687 Comdat *C = M.getOrInsertComdat(GroupName);
1688
1689 if (!NeedComdat) {
1690 // Object file format must be ELF since `UseComdat && !NeedComdat` is true.
1691 //
1692 // For ELF, when not using COMDAT, put counters, data and values into a
1693 // nodeduplicate COMDAT which is lowered to a zero-flag section group. This
1694 // allows -z start-stop-gc to discard the entire group when the function is
1695 // discarded.
1696 C->setSelectionKind(Comdat::NoDeduplicate);
1697 }
1698 GV->setComdat(C);
1699 // COFF doesn't allow the comdat group leader to have private linkage, so
1700 // upgrade private linkage to internal linkage to produce a symbol table
1701 // entry.
1702 if (TT.isOSBinFormatCOFF() && GV->hasPrivateLinkage())
1704}
1705
1707 if (!profDataReferencedByCode(*GV->getParent()))
1708 return false;
1709
1710 if (!GV->hasLinkOnceLinkage() && !GV->hasLocalLinkage() &&
1712 return true;
1713
1714 // This avoids the profile data from referencing internal symbols in
1715 // COMDAT.
1716 if (GV->hasLocalLinkage() && GV->hasComdat())
1717 return false;
1718
1719 return true;
1720}
1721
1722// FIXME: Introduce an internal alias like what's done for functions to reduce
1723// the number of relocation entries.
1725 // Store a nullptr in __profvt_ if a real address shouldn't be used.
1726 if (!shouldRecordVTableAddr(GV))
1728
1729 return GV;
1730}
1731
1732void InstrLowerer::getOrCreateVTableProfData(GlobalVariable *GV) {
1734 "Value profiling is not supported with lightweight instrumentation");
1736 return;
1737
1738 // Skip llvm internal global variable or __prof variables.
1739 if (GV->getName().starts_with("llvm.") ||
1740 GV->getName().starts_with("__llvm") ||
1741 GV->getName().starts_with("__prof"))
1742 return;
1743
1744 // VTableProfData already created
1745 auto It = VTableDataMap.find(GV);
1746 if (It != VTableDataMap.end() && It->second)
1747 return;
1748
1751
1752 // This is to keep consistent with per-function profile data
1753 // for correctness.
1754 if (TT.isOSBinFormatXCOFF()) {
1756 Visibility = GlobalValue::DefaultVisibility;
1757 }
1758
1759 LLVMContext &Ctx = M.getContext();
1760 Type *DataTypes[] = {
1761#define INSTR_PROF_VTABLE_DATA(Type, LLVMType, Name, Init) LLVMType,
1763#undef INSTR_PROF_VTABLE_DATA
1764 };
1765
1766 auto *DataTy = StructType::get(Ctx, ArrayRef(DataTypes));
1767
1768 // Used by INSTR_PROF_VTABLE_DATA MACRO
1769 Constant *VTableAddr = getVTableAddrForProfData(GV);
1770 const std::string PGOVTableName = getPGOName(*GV);
1771 // Record the length of the vtable. This is needed since vtable pointers
1772 // loaded from C++ objects might be from the middle of a vtable definition.
1773 uint32_t VTableSizeVal = GV->getGlobalSize(M.getDataLayout());
1774
1775 Constant *DataVals[] = {
1776#define INSTR_PROF_VTABLE_DATA(Type, LLVMType, Name, Init) Init,
1778#undef INSTR_PROF_VTABLE_DATA
1779 };
1780
1781 auto *Data =
1782 new GlobalVariable(M, DataTy, /*constant=*/false, Linkage,
1783 ConstantStruct::get(DataTy, DataVals),
1784 getInstrProfVTableVarPrefix() + PGOVTableName);
1785
1786 Data->setVisibility(Visibility);
1787 Data->setSection(getInstrProfSectionName(IPSK_vtab, TT.getObjectFormat()));
1788 Data->setAlignment(Align(8));
1789
1790 maybeSetComdat(Data, GV, Data->getName());
1791
1792 VTableDataMap[GV] = Data;
1793
1794 ReferencedVTables.push_back(GV);
1795
1796 // VTable <Hash, Addr> is used by runtime but not referenced by other
1797 // sections. Conservatively mark it linker retained.
1798 UsedVars.push_back(Data);
1799}
1800
1801GlobalVariable *InstrLowerer::setupProfileSection(InstrProfInstBase *Inc,
1802 InstrProfSectKind IPSK) {
1803 GlobalVariable *NamePtr = Inc->getName();
1804
1805 // Match the linkage and visibility of the name global.
1806 Function *Fn = Inc->getParent()->getParent();
1808 GlobalValue::VisibilityTypes Visibility = NamePtr->getVisibility();
1809
1810 // Use internal rather than private linkage so the counter variable shows up
1811 // in the symbol table when using debug info for correlation.
1813 TT.isOSBinFormatMachO() && Linkage == GlobalValue::PrivateLinkage)
1815
1816 // Due to the limitation of binder as of 2021/09/28, the duplicate weak
1817 // symbols in the same csect won't be discarded. When there are duplicate weak
1818 // symbols, we can NOT guarantee that the relocations get resolved to the
1819 // intended weak symbol, so we can not ensure the correctness of the relative
1820 // CounterPtr, so we have to use private linkage for counter and data symbols.
1821 if (TT.isOSBinFormatXCOFF()) {
1823 Visibility = GlobalValue::DefaultVisibility;
1824 }
1825 // Move the name variable to the right section.
1826 bool Renamed;
1827 GlobalVariable *Ptr;
1828 StringRef VarPrefix;
1829 std::string VarName;
1830 if (IPSK == IPSK_cnts) {
1831 VarPrefix = getInstrProfCountersVarPrefix();
1832 VarName = getVarName(Inc, VarPrefix, Renamed);
1834 Ptr = createRegionCounters(CntrIncrement, VarName, Linkage);
1835 } else if (IPSK == IPSK_bitmap) {
1836 VarPrefix = getInstrProfBitmapVarPrefix();
1837 VarName = getVarName(Inc, VarPrefix, Renamed);
1838 InstrProfMCDCBitmapInstBase *BitmapUpdate =
1840 Ptr = createRegionBitmaps(BitmapUpdate, VarName, Linkage);
1841 } else {
1842 llvm_unreachable("Profile Section must be for Counters or Bitmaps");
1843 }
1844
1845 Ptr->setVisibility(Visibility);
1846 Ptr->setSection(getInstrProfSectionName(IPSK, TT.getObjectFormat()));
1847 Ptr->setLinkage(Linkage);
1848 if (isGPUProfTarget(M) && !Ptr->hasComdat()) {
1849 Ptr->setComdat(M.getOrInsertComdat(VarName));
1852 } else {
1853 maybeSetComdat(Ptr, Fn, VarName);
1854 }
1855 return Ptr;
1856}
1857
1859InstrLowerer::createRegionBitmaps(InstrProfMCDCBitmapInstBase *Inc,
1860 StringRef Name,
1862 uint64_t NumBytes = Inc->getNumBitmapBytes();
1863 auto *BitmapTy = ArrayType::get(Type::getInt8Ty(M.getContext()), NumBytes);
1864 auto GV = new GlobalVariable(M, BitmapTy, false, Linkage,
1865 Constant::getNullValue(BitmapTy), Name);
1866 GV->setAlignment(Align(1));
1867 return GV;
1868}
1869
1871InstrLowerer::getOrCreateRegionBitmaps(InstrProfMCDCBitmapInstBase *Inc) {
1872 GlobalVariable *NamePtr = Inc->getName();
1873 auto &PD = ProfileDataMap[NamePtr];
1874 if (PD.RegionBitmaps)
1875 return PD.RegionBitmaps;
1876
1877 // If RegionBitmaps doesn't already exist, create it by first setting up
1878 // the corresponding profile section.
1879 auto *BitmapPtr = setupProfileSection(Inc, IPSK_bitmap);
1880 PD.RegionBitmaps = BitmapPtr;
1881 PD.NumBitmapBytes = Inc->getNumBitmapBytes();
1882
1883 if (PD.NumBitmapBytes &&
1885 LLVMContext &Ctx = M.getContext();
1886 Function *Fn = Inc->getParent()->getParent();
1887 if (auto *SP = Fn->getSubprogram()) {
1888 DIBuilder DB(M, true, SP->getUnit());
1889 Metadata *FunctionNameAnnotation[] = {
1892 };
1893 Metadata *NumBitmapBitsAnnotation[] = {
1896 };
1897 auto Annotations = DB.getOrCreateArray({
1898 MDNode::get(Ctx, FunctionNameAnnotation),
1899 MDNode::get(Ctx, NumBitmapBitsAnnotation),
1900 });
1901 auto *DICounter = DB.createGlobalVariableExpression(
1902 SP, BitmapPtr->getName(), /*LinkageName=*/StringRef(), SP->getFile(),
1903 /*LineNo=*/0, DB.createUnspecifiedType("Profile Bitmap Type"),
1904 BitmapPtr->hasLocalLinkage(), /*IsDefined=*/true, /*Expr=*/nullptr,
1905 /*Decl=*/nullptr, /*TemplateParams=*/nullptr, /*AlignInBits=*/0,
1906 Annotations);
1907 BitmapPtr->addDebugInfo(DICounter);
1908 DB.finalizeSubprogram(SP);
1909 DB.finalize();
1910 }
1911
1912 // Mark the bitmap variable as used so that it isn't optimized out.
1913 CompilerUsedVars.push_back(PD.RegionBitmaps);
1914 }
1915
1916 return PD.RegionBitmaps;
1917}
1918
1920InstrLowerer::createRegionCounters(InstrProfCntrInstBase *Inc, StringRef Name,
1922 uint64_t NumCounters = Inc->getNumCounters()->getZExtValue();
1923 auto &Ctx = M.getContext();
1924 GlobalVariable *GV;
1925 if (isa<InstrProfCoverInst>(Inc)) {
1926 auto *CounterTy = Type::getInt8Ty(Ctx);
1927 auto *CounterArrTy = ArrayType::get(CounterTy, NumCounters);
1928 // TODO: `Constant::getAllOnesValue()` does not yet accept an array type.
1929 std::vector<Constant *> InitialValues(NumCounters,
1930 Constant::getAllOnesValue(CounterTy));
1931 GV = new GlobalVariable(M, CounterArrTy, false, Linkage,
1932 ConstantArray::get(CounterArrTy, InitialValues),
1933 Name);
1934 GV->setAlignment(Align(1));
1935 } else {
1936 auto *CounterTy = ArrayType::get(Type::getInt64Ty(Ctx), NumCounters);
1937 GV = new GlobalVariable(M, CounterTy, false, Linkage,
1938 Constant::getNullValue(CounterTy), Name);
1939 GV->setAlignment(Align(8));
1940 }
1941 return GV;
1942}
1943
1945InstrLowerer::getOrCreateRegionCounters(InstrProfCntrInstBase *Inc) {
1946 GlobalVariable *NamePtr = Inc->getName();
1947 auto &PD = ProfileDataMap[NamePtr];
1948 if (PD.RegionCounters)
1949 return PD.RegionCounters;
1950
1951 // If RegionCounters doesn't already exist, create it by first setting up
1952 // the corresponding profile section.
1953 auto *CounterPtr = setupProfileSection(Inc, IPSK_cnts);
1954 PD.RegionCounters = CounterPtr;
1955
1957 LLVMContext &Ctx = M.getContext();
1958 Function *Fn = Inc->getParent()->getParent();
1959 if (auto *SP = Fn->getSubprogram()) {
1960 DIBuilder DB(M, true, SP->getUnit());
1961 Metadata *FunctionNameAnnotation[] = {
1964 };
1965 Metadata *CFGHashAnnotation[] = {
1968 };
1969 Metadata *NumCountersAnnotation[] = {
1972 };
1973 auto Annotations = DB.getOrCreateArray({
1974 MDNode::get(Ctx, FunctionNameAnnotation),
1975 MDNode::get(Ctx, CFGHashAnnotation),
1976 MDNode::get(Ctx, NumCountersAnnotation),
1977 });
1978 auto *DICounter = DB.createGlobalVariableExpression(
1979 SP, CounterPtr->getName(), /*LinkageName=*/StringRef(), SP->getFile(),
1980 /*LineNo=*/0, DB.createUnspecifiedType("Profile Data Type"),
1981 CounterPtr->hasLocalLinkage(), /*IsDefined=*/true, /*Expr=*/nullptr,
1982 /*Decl=*/nullptr, /*TemplateParams=*/nullptr, /*AlignInBits=*/0,
1983 Annotations);
1984 CounterPtr->addDebugInfo(DICounter);
1985 DB.finalizeSubprogram(SP);
1986 DB.finalize();
1987 }
1988
1989 // Mark the counter variable as used so that it isn't optimized out.
1990 CompilerUsedVars.push_back(PD.RegionCounters);
1991 }
1992
1993 // Create uniform counters before the data variable so that
1994 // UniformCounterPtr can reference them in createDataVariable().
1995 getOrCreateUniformCounters(Inc);
1996
1997 // Create the data variable (if it doesn't already exist).
1998 createDataVariable(Inc);
1999
2000 return PD.RegionCounters;
2001}
2002
2004InstrLowerer::getOrCreateUniformCounters(InstrProfCntrInstBase *Inc) {
2005 // Uniform counters are only meaningful for GPU profile targets.
2006 if (!isGPUProfTarget(M))
2007 return nullptr;
2008
2009 GlobalVariable *NamePtr = Inc->getName();
2010 auto &PD = ProfileDataMap[NamePtr];
2011 if (PD.UniformCounters)
2012 return PD.UniformCounters;
2013
2014 assert(PD.RegionCounters && "region counters must be created first");
2015
2016 uint64_t NumCounters = Inc->getNumCounters()->getZExtValue();
2017
2018 LLVMContext &Ctx = M.getContext();
2019 ArrayType *CounterTy = ArrayType::get(Type::getInt64Ty(Ctx), NumCounters);
2020
2021 bool Renamed;
2022 std::string VarName = getVarName(Inc, "__llvm_prf_unifcnt_", Renamed);
2023
2024 auto *GV = new GlobalVariable(M, CounterTy, false, NamePtr->getLinkage(),
2025 Constant::getNullValue(CounterTy), VarName);
2026 GV->setAlignment(Align(8));
2027
2028 GV->setSection(getInstrProfSectionName(IPSK_ucnts, TT.getObjectFormat()));
2029
2030 GV->setComdat(M.getOrInsertComdat(VarName));
2033
2034 PD.UniformCounters = GV;
2035 CompilerUsedVars.push_back(GV);
2036
2037 return PD.UniformCounters;
2038}
2039
2040void InstrLowerer::createDataVariable(InstrProfCntrInstBase *Inc) {
2041 // When debug information is correlated to profile data, a data variable
2042 // is not needed.
2044 return;
2045
2046 GlobalVariable *NamePtr = Inc->getName();
2047 auto &PD = ProfileDataMap[NamePtr];
2048
2049 // Return if data variable was already created.
2050 if (PD.DataVar)
2051 return;
2052
2053 LLVMContext &Ctx = M.getContext();
2054
2055 Function *Fn = Inc->getParent()->getParent();
2057 GlobalValue::VisibilityTypes Visibility = NamePtr->getVisibility();
2058
2059 // Due to the limitation of binder as of 2021/09/28, the duplicate weak
2060 // symbols in the same csect won't be discarded. When there are duplicate weak
2061 // symbols, we can NOT guarantee that the relocations get resolved to the
2062 // intended weak symbol, so we can not ensure the correctness of the relative
2063 // CounterPtr, so we have to use private linkage for counter and data symbols.
2064 if (TT.isOSBinFormatXCOFF()) {
2066 Visibility = GlobalValue::DefaultVisibility;
2067 }
2068
2069 bool NeedComdat = needsComdatForCounter(*Fn, M);
2070 bool Renamed;
2071
2072 // The Data Variable section is anchored to profile counters.
2073 std::string CntsVarName =
2075 std::string DataVarName =
2076 getVarName(Inc, getInstrProfDataVarPrefix(), Renamed);
2077
2078 auto *Int8PtrTy = PointerType::getUnqual(Ctx);
2079 // Allocate statically the array of pointers to value profile nodes for
2080 // the current function.
2081 Constant *ValuesPtrExpr = ConstantPointerNull::get(Int8PtrTy);
2082 uint64_t NS = 0;
2083 for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
2084 NS += PD.NumValueSites[Kind];
2085 if (NS > 0 && ValueProfileStaticAlloc &&
2087 ArrayType *ValuesTy = ArrayType::get(Type::getInt64Ty(Ctx), NS);
2088 auto *ValuesVar = new GlobalVariable(
2089 M, ValuesTy, false, Linkage, Constant::getNullValue(ValuesTy),
2090 getVarName(Inc, getInstrProfValuesVarPrefix(), Renamed));
2091 ValuesVar->setVisibility(Visibility);
2092 setGlobalVariableLargeSection(TT, *ValuesVar);
2093 ValuesVar->setSection(
2094 getInstrProfSectionName(IPSK_vals, TT.getObjectFormat()));
2095 ValuesVar->setAlignment(Align(8));
2096 maybeSetComdat(ValuesVar, Fn, CntsVarName);
2098 ValuesVar, PointerType::get(Fn->getContext(), 0));
2099 }
2100
2101 uint64_t NumCounters = Inc->getNumCounters()->getZExtValue();
2102
2103 Constant *CounterPtr = PD.RegionCounters;
2104 Constant *UniformCounterPtr = PD.UniformCounters;
2105
2106 uint64_t NumBitmapBytes = PD.NumBitmapBytes;
2107
2108 // Create data variable.
2109 auto *IntPtrTy = M.getDataLayout().getIntPtrType(M.getContext());
2110 auto *Int16Ty = Type::getInt16Ty(Ctx);
2111 auto *Int16ArrayTy = ArrayType::get(Int16Ty, IPVK_Last + 1);
2112 auto *DataTy = getProfileDataTy();
2113
2114 Constant *FunctionAddr = getFuncAddrForProfData(Fn);
2115
2116 Constant *Int16ArrayVals[IPVK_Last + 1];
2117 for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
2118 Int16ArrayVals[Kind] = ConstantInt::get(Int16Ty, PD.NumValueSites[Kind]);
2119
2120 uint16_t OffloadDeviceWaveSizeVal = 0;
2121
2122 if (isGPUProfTarget(M)) {
2123 // For GPU targets, weak functions need weak linkage for their profile data
2124 // aliases to allow linker deduplication across TUs
2126 Linkage = Fn->getLinkage();
2127 else
2130 }
2131 // If the data variable is not referenced by code (if we don't emit
2132 // @llvm.instrprof.value.profile, NS will be 0), and the counter keeps the
2133 // data variable live under linker GC, the data variable can be private. This
2134 // optimization applies to ELF.
2135 //
2136 // On COFF, a comdat leader cannot be local so we require DataReferencedByCode
2137 // to be false.
2138 //
2139 // If profd is in a deduplicate comdat, NS==0 with a hash suffix guarantees
2140 // that other copies must have the same CFG and cannot have value profiling.
2141 // If no hash suffix, other profd copies may be referenced by code.
2142 if (!isGPUProfTarget(M) && NS == 0 &&
2143 !(DataReferencedByCode && NeedComdat && !Renamed) &&
2144 (TT.isOSBinFormatELF() ||
2145 (!DataReferencedByCode && TT.isOSBinFormatCOFF()))) {
2147 Visibility = GlobalValue::DefaultVisibility;
2148 }
2149 // GPU-target ELF objects are always ET_DYN, so non-local symbols with
2150 // default visibility are preemptible. The CounterPtr label difference
2151 // emits a REL32 relocation that lld rejects against preemptible targets.
2152 if (TT.isGPU() && TT.isOSBinFormatELF() &&
2155 auto *Data =
2156 new GlobalVariable(M, DataTy, false, Linkage, nullptr, DataVarName);
2157
2158 Constant *RelativeCounterPtr;
2159 Constant *RelativeUniformCounterPtr = ConstantInt::get(IntPtrTy, 0);
2160 GlobalVariable *BitmapPtr = PD.RegionBitmaps;
2161 Constant *RelativeBitmapPtr = ConstantInt::get(IntPtrTy, 0);
2162 InstrProfSectKind DataSectionKind;
2163 // With binary profile correlation, profile data is not loaded into memory.
2164 // profile data must reference profile counter with an absolute relocation.
2166 DataSectionKind = IPSK_covdata;
2167 RelativeCounterPtr = ConstantExpr::getPtrToInt(CounterPtr, IntPtrTy);
2168 if (BitmapPtr != nullptr)
2169 RelativeBitmapPtr = ConstantExpr::getPtrToInt(BitmapPtr, IntPtrTy);
2170 if (UniformCounterPtr != nullptr)
2171 RelativeUniformCounterPtr =
2173 } else if (TT.isNVPTX()) {
2174 // The NVPTX target cannot handle self-referencing constant expressions in
2175 // global initializers at all. Use absolute pointers and have the runtime
2176 // registration convert them to relative offsets.
2177 DataSectionKind = IPSK_data;
2178 RelativeCounterPtr = ConstantExpr::getPtrToInt(CounterPtr, IntPtrTy);
2179 } else {
2180 // Reference the counter variable with a label difference (link-time
2181 // constant).
2182 DataSectionKind = IPSK_data;
2183 RelativeCounterPtr =
2186 if (BitmapPtr != nullptr)
2187 RelativeBitmapPtr =
2190 if (UniformCounterPtr != nullptr)
2191 RelativeUniformCounterPtr = ConstantExpr::getSub(
2194 }
2195
2196 Constant *DataVals[] = {
2197#define INSTR_PROF_DATA(Type, LLVMType, Name, Init) Init,
2199 };
2200 Data->setInitializer(ConstantStruct::get(DataTy, DataVals));
2201
2202 Data->setVisibility(Visibility);
2203 Data->setSection(
2204 getInstrProfSectionName(DataSectionKind, TT.getObjectFormat()));
2205 Data->setAlignment(Align(INSTR_PROF_DATA_ALIGNMENT));
2206 if (isGPUProfTarget(M) && !Data->hasComdat()) {
2207 Data->setComdat(M.getOrInsertComdat(CntsVarName));
2209 } else {
2210 maybeSetComdat(Data, Fn, CntsVarName);
2211 }
2212
2213 PD.DataVar = Data;
2214
2215 // Mark the data variable as used so that it isn't stripped out.
2216 CompilerUsedVars.push_back(Data);
2217 // Now that the linkage set by the FE has been passed to the data and counter
2218 // variables, reset Name variable's linkage and visibility to private so that
2219 // it can be removed later by the compiler.
2221 // Collect the referenced names to be used by emitNameData.
2222 ReferencedNames.push_back(NamePtr);
2223}
2224
2225void InstrLowerer::emitVNodes() {
2226 if (!ValueProfileStaticAlloc)
2227 return;
2228
2229 // For now only support this on platforms that do
2230 // not require runtime registration to discover
2231 // named section start/end.
2233 return;
2234
2235 size_t TotalNS = 0;
2236 for (auto &PD : ProfileDataMap) {
2237 for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
2238 TotalNS += PD.second.NumValueSites[Kind];
2239 }
2240
2241 if (!TotalNS)
2242 return;
2243
2244 uint64_t NumCounters = TotalNS * NumCountersPerValueSite;
2245// Heuristic for small programs with very few total value sites.
2246// The default value of vp-counters-per-site is chosen based on
2247// the observation that large apps usually have a low percentage
2248// of value sites that actually have any profile data, and thus
2249// the average number of counters per site is low. For small
2250// apps with very few sites, this may not be true. Bump up the
2251// number of counters in this case.
2252#define INSTR_PROF_MIN_VAL_COUNTS 10
2253 if (NumCounters < INSTR_PROF_MIN_VAL_COUNTS)
2254 NumCounters = std::max(INSTR_PROF_MIN_VAL_COUNTS, (int)NumCounters * 2);
2255
2256 auto &Ctx = M.getContext();
2257 Type *VNodeTypes[] = {
2258#define INSTR_PROF_VALUE_NODE(Type, LLVMType, Name, Init) LLVMType,
2260 };
2261 auto *VNodeTy = StructType::get(Ctx, ArrayRef(VNodeTypes));
2262
2263 ArrayType *VNodesTy = ArrayType::get(VNodeTy, NumCounters);
2264 auto *VNodesVar = new GlobalVariable(
2265 M, VNodesTy, false, GlobalValue::PrivateLinkage,
2267 setGlobalVariableLargeSection(TT, *VNodesVar);
2268 VNodesVar->setSection(
2269 getInstrProfSectionName(IPSK_vnodes, TT.getObjectFormat()));
2270 VNodesVar->setAlignment(M.getDataLayout().getABITypeAlign(VNodesTy));
2271 // VNodesVar is used by runtime but not referenced via relocation by other
2272 // sections. Conservatively make it linker retained.
2273 UsedVars.push_back(VNodesVar);
2274}
2275
2276// Build the per-TU device-PGO sections struct: section start/stop bounds for
2277// names/counters/data/uniform-counters plus the raw version. Returns null if it
2278// already exists.
2280 StringRef CUIDPostfix) {
2281 std::string Name = ("__llvm_profile_sections" + CUIDPostfix).str();
2282 if (M.getNamedValue(Name))
2283 return nullptr;
2284
2285 LLVMContext &Ctx = M.getContext();
2286 unsigned AS = M.getDataLayout().getDefaultGlobalsAddressSpace();
2287 auto Extern = [&](StringRef Sym, Type *Ty, bool IsConst,
2289 GlobalVariable *GV = M.getNamedGlobal(Sym);
2290 if (!GV) {
2291 GV = new GlobalVariable(M, Ty, IsConst, GlobalValue::ExternalLinkage,
2292 nullptr, Sym, nullptr,
2294 GV->setVisibility(Vis);
2295 }
2296 return GV;
2297 };
2298 // Section bounds are hidden i8 markers; raw_version is an i64 constant.
2299 auto *I8 = Type::getInt8Ty(Ctx);
2300 auto Hidden = GlobalValue::HiddenVisibility;
2301 Constant *Fields[] = {Extern("__start___llvm_prf_names", I8, false, Hidden),
2302 Extern("__stop___llvm_prf_names", I8, false, Hidden),
2303 Extern("__start___llvm_prf_cnts", I8, false, Hidden),
2304 Extern("__stop___llvm_prf_cnts", I8, false, Hidden),
2305 Extern("__start___llvm_prf_data", I8, false, Hidden),
2306 Extern("__stop___llvm_prf_data", I8, false, Hidden),
2307 Extern("__start___llvm_prf_ucnts", I8, false, Hidden),
2308 Extern("__stop___llvm_prf_ucnts", I8, false, Hidden),
2309 Extern("__llvm_profile_raw_version",
2310 Type::getInt64Ty(Ctx), true,
2312 auto *PtrTy = PointerType::get(Ctx, AS);
2313 auto *STy = StructType::get(
2314 Ctx, {PtrTy, PtrTy, PtrTy, PtrTy, PtrTy, PtrTy, PtrTy, PtrTy, PtrTy});
2315 auto *GV = new GlobalVariable(M, STy, /*isConstant=*/true,
2317 ConstantStruct::get(STy, Fields), Name, nullptr,
2319 GV->setVisibility(GlobalValue::ProtectedVisibility);
2320 return GV;
2321}
2322
2323void InstrLowerer::emitNameData() {
2324 if (ReferencedNames.empty())
2325 return;
2326
2327 std::string CompressedNameStr;
2328 if (Error E = collectPGOFuncNameStrings(ReferencedNames, CompressedNameStr,
2330 report_fatal_error(Twine(toString(std::move(E))), false);
2331 }
2332
2333 auto &Ctx = M.getContext();
2334 auto *NamesVal =
2335 ConstantDataArray::getString(Ctx, StringRef(CompressedNameStr), false);
2336 std::string NamesVarName = std::string(getInstrProfNamesVarName());
2339 std::string GPUCUIDPostfix;
2340 if (isGPUProfTarget(M)) {
2341 if (auto *GV = M.getNamedGlobal(getInstrProfNamesVarPostfixVarName())) {
2342 if (auto *Init =
2344 if (Init->isCString()) {
2345 GPUCUIDPostfix = Init->getAsCString().str();
2346 NamesVarName += GPUCUIDPostfix;
2347 NamesLinkage = GlobalValue::ExternalLinkage;
2348 NamesVisibility = GlobalValue::ProtectedVisibility;
2350 M, [GV](Constant *C) { return C->stripPointerCasts() == GV; });
2351 GV->eraseFromParent();
2352 }
2353 }
2354 }
2355 }
2356 NamesVar = new GlobalVariable(M, NamesVal->getType(), true, NamesLinkage,
2357 NamesVal, NamesVarName);
2358 NamesVar->setVisibility(NamesVisibility);
2359
2360 NamesSize = CompressedNameStr.size();
2361 setGlobalVariableLargeSection(TT, *NamesVar);
2362 std::string NamesSectionName =
2364 ? getInstrProfSectionName(IPSK_covname, TT.getObjectFormat())
2365 : getInstrProfSectionName(IPSK_name, TT.getObjectFormat());
2366 NamesVar->setSection(NamesSectionName);
2367 // On COFF, it's important to reduce the alignment down to 1 to prevent the
2368 // linker from inserting padding before the start of the names section or
2369 // between names entries.
2370 NamesVar->setAlignment(Align(1));
2371 // NamesVar is used by runtime but not referenced via relocation by other
2372 // sections. Conservatively make it linker retained.
2373 UsedVars.push_back(NamesVar);
2374
2375 for (auto *NamePtr : ReferencedNames)
2376 NamePtr->eraseFromParent();
2377
2378 // Emit the device sections struct only when this TU produced profile data, so
2379 // its section start/stop references are backed by a real section.
2380 bool HasData = llvm::any_of(ProfileDataMap,
2381 [](const auto &KV) { return KV.second.DataVar; });
2382 if (!GPUCUIDPostfix.empty() && HasData)
2383 if (GlobalVariable *GV = emitGPUOffloadSectionsStruct(M, GPUCUIDPostfix))
2384 CompilerUsedVars.push_back(GV);
2385}
2386
2387void InstrLowerer::emitVTableNames() {
2388 if (!EnableVTableValueProfiling || ReferencedVTables.empty())
2389 return;
2390
2391 // Collect the PGO names of referenced vtables and compress them.
2392 std::string CompressedVTableNames;
2393 if (Error E = collectVTableStrings(ReferencedVTables, CompressedVTableNames,
2395 report_fatal_error(Twine(toString(std::move(E))), false);
2396 }
2397
2398 auto &Ctx = M.getContext();
2399 auto *VTableNamesVal = ConstantDataArray::getString(
2400 Ctx, StringRef(CompressedVTableNames), false /* AddNull */);
2401 GlobalVariable *VTableNamesVar =
2402 new GlobalVariable(M, VTableNamesVal->getType(), true /* constant */,
2403 GlobalValue::PrivateLinkage, VTableNamesVal,
2405 VTableNamesVar->setSection(
2406 getInstrProfSectionName(IPSK_vname, TT.getObjectFormat()));
2407 VTableNamesVar->setAlignment(Align(1));
2408 // Make VTableNames linker retained.
2409 UsedVars.push_back(VTableNamesVar);
2410}
2411
2412void InstrLowerer::emitRegistration() {
2414 return;
2415
2416 // Construct the function.
2417 auto *VoidTy = Type::getVoidTy(M.getContext());
2418 auto *VoidPtrTy = PointerType::getUnqual(M.getContext());
2419 auto *Int64Ty = Type::getInt64Ty(M.getContext());
2420 auto *RegisterFTy = FunctionType::get(VoidTy, false);
2421 auto *RegisterF = Function::Create(RegisterFTy, GlobalValue::InternalLinkage,
2423 RegisterF->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
2424 if (Options.NoRedZone)
2425 RegisterF->addFnAttr(Attribute::NoRedZone);
2426
2427 auto *RuntimeRegisterTy = FunctionType::get(VoidTy, VoidPtrTy, false);
2428 auto *RuntimeRegisterF =
2431
2432 IRBuilder<> IRB(BasicBlock::Create(M.getContext(), "", RegisterF));
2433 for (Value *Data : CompilerUsedVars)
2434 if (!isa<Function>(Data))
2435 // Check for addrspace cast when profiling GPU
2436 IRB.CreateCall(RuntimeRegisterF,
2437 IRB.CreatePointerBitCastOrAddrSpaceCast(Data, VoidPtrTy));
2438 for (Value *Data : UsedVars)
2439 if (Data != NamesVar && !isa<Function>(Data))
2440 IRB.CreateCall(RuntimeRegisterF,
2441 IRB.CreatePointerBitCastOrAddrSpaceCast(Data, VoidPtrTy));
2442
2443 if (NamesVar) {
2444 Type *ParamTypes[] = {VoidPtrTy, Int64Ty};
2445 auto *NamesRegisterTy =
2446 FunctionType::get(VoidTy, ArrayRef(ParamTypes), false);
2447 auto *NamesRegisterF =
2450 IRB.CreateCall(NamesRegisterF, {IRB.CreatePointerBitCastOrAddrSpaceCast(
2451 NamesVar, VoidPtrTy),
2452 IRB.getInt64(NamesSize)});
2453 }
2454
2455 IRB.CreateRetVoid();
2456}
2457
2458bool InstrLowerer::emitRuntimeHook() {
2459 // GPU profiling data is read directly by the host offload runtime. We do not
2460 // need the standard runtime hook.
2461 if (TT.isGPU())
2462 return false;
2463
2464 // We expect the linker to be invoked with -u<hook_var> flag for Linux
2465 // in which case there is no need to emit the external variable.
2466 if (TT.isOSLinux() || TT.isOSAIX())
2467 return false;
2468
2469 // If the module's provided its own runtime, we don't need to do anything.
2470 if (M.getGlobalVariable(getInstrProfRuntimeHookVarName()))
2471 return false;
2472
2473 // Declare an external variable that will pull in the runtime initialization.
2474 auto *Int32Ty = Type::getInt32Ty(M.getContext());
2475 auto *Var =
2476 new GlobalVariable(M, Int32Ty, false, GlobalValue::ExternalLinkage,
2478 Var->setVisibility(GlobalValue::HiddenVisibility);
2479
2480 if (TT.isOSBinFormatELF() && !TT.isPS()) {
2481 // Mark the user variable as used so that it isn't stripped out.
2482 CompilerUsedVars.push_back(Var);
2483 } else {
2484 // Make a function that uses it.
2485 auto *User = Function::Create(FunctionType::get(Int32Ty, false),
2488 User->addFnAttr(Attribute::NoInline);
2489 if (Options.NoRedZone)
2490 User->addFnAttr(Attribute::NoRedZone);
2491 User->setVisibility(GlobalValue::HiddenVisibility);
2492 if (TT.supportsCOMDAT())
2493 User->setComdat(M.getOrInsertComdat(User->getName()));
2494 // Explicitly mark this function as cold since it is never called.
2495 User->setEntryCount(0);
2496
2497 IRBuilder<> IRB(BasicBlock::Create(M.getContext(), "", User));
2498 auto *Load = IRB.CreateLoad(Int32Ty, Var);
2499 IRB.CreateRet(Load);
2500
2501 // Mark the function as used so that it isn't stripped out.
2502 CompilerUsedVars.push_back(User);
2503 }
2504 return true;
2505}
2506
2507void InstrLowerer::emitUses() {
2508 // The metadata sections are parallel arrays. Optimizers (e.g.
2509 // GlobalOpt/ConstantMerge) may not discard associated sections as a unit, so
2510 // we conservatively retain all unconditionally in the compiler.
2511 //
2512 // On ELF and Mach-O, the linker can guarantee the associated sections will be
2513 // retained or discarded as a unit, so llvm.compiler.used is sufficient.
2514 // Similarly on COFF, if prof data is not referenced by code we use one comdat
2515 // and ensure this GC property as well. Otherwise, we have to conservatively
2516 // make all of the sections retained by the linker.
2517 if (TT.isOSBinFormatELF() || TT.isOSBinFormatMachO() ||
2518 (TT.isOSBinFormatCOFF() && !DataReferencedByCode))
2519 appendToCompilerUsed(M, CompilerUsedVars);
2520 else
2521 appendToUsed(M, CompilerUsedVars);
2522
2523 // We do not add proper references from used metadata sections to NamesVar and
2524 // VNodesVar, so we have to be conservative and place them in llvm.used
2525 // regardless of the target,
2526 appendToUsed(M, UsedVars);
2527}
2528
2529void InstrLowerer::emitInitialization() {
2530 // Create ProfileFileName variable. Don't don't this for the
2531 // context-sensitive instrumentation lowering: This lowering is after
2532 // LTO/ThinLTO linking. Pass PGOInstrumentationGenCreateVar should
2533 // have already create the variable before LTO/ThinLTO linking.
2534 if (!IsCS)
2535 createProfileFileNameVar(M, Options.InstrProfileOutput);
2536 Function *RegisterF = M.getFunction(getInstrProfRegFuncsName());
2537 if (!RegisterF)
2538 return;
2539
2540 // Create the initialization function.
2541 auto *VoidTy = Type::getVoidTy(M.getContext());
2542 auto *F = Function::Create(FunctionType::get(VoidTy, false),
2545 F->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
2546 F->addFnAttr(Attribute::NoInline);
2547 if (Options.NoRedZone)
2548 F->addFnAttr(Attribute::NoRedZone);
2549
2550 // Add the basic block and the necessary calls.
2551 IRBuilder<> IRB(BasicBlock::Create(M.getContext(), "", F));
2552 IRB.CreateCall(RegisterF, {});
2553 IRB.CreateRetVoid();
2554
2555 appendToGlobalCtors(M, F, 0);
2556}
2557
2558namespace llvm {
2559// Create the variable for profile sampling.
2562 IntegerType *SamplingVarTy;
2563 Constant *ValueZero;
2564 if (getSampledInstrumentationConfig().UseShort) {
2565 SamplingVarTy = Type::getInt16Ty(M.getContext());
2566 ValueZero = Constant::getIntegerValue(SamplingVarTy, APInt(16, 0));
2567 } else {
2568 SamplingVarTy = Type::getInt32Ty(M.getContext());
2569 ValueZero = Constant::getIntegerValue(SamplingVarTy, APInt(32, 0));
2570 }
2571 auto SamplingVar = new GlobalVariable(
2572 M, SamplingVarTy, false, GlobalValue::WeakAnyLinkage, ValueZero, VarName);
2573 SamplingVar->setVisibility(GlobalValue::DefaultVisibility);
2574 SamplingVar->setThreadLocal(true);
2575 Triple TT(M.getTargetTriple());
2576 if (TT.supportsCOMDAT()) {
2577 SamplingVar->setLinkage(GlobalValue::ExternalLinkage);
2578 SamplingVar->setComdat(M.getOrInsertComdat(VarName));
2579 }
2580 appendToCompilerUsed(M, SamplingVar);
2581}
2582} // namespace llvm
2583
2584// For GPU targets: Allocate contiguous arrays for all profile data.
2585// This solves the linker reordering problem by using ONE symbol per section
2586// type, so there's nothing for the linker to reorder.
2587StructType *InstrLowerer::getProfileDataTy() {
2588 if (ProfileDataTy)
2589 return ProfileDataTy;
2590
2591 auto &Ctx = M.getContext();
2592 auto *IntPtrTy = M.getDataLayout().getIntPtrType(M.getContext());
2593 auto *Int16Ty = Type::getInt16Ty(Ctx);
2594 auto *Int16ArrayTy = ArrayType::get(Int16Ty, IPVK_Last + 1);
2595 Type *DataTypes[] = {
2596#define INSTR_PROF_DATA(Type, LLVMType, Name, Init) LLVMType,
2598 };
2599 ProfileDataTy = StructType::get(Ctx, ArrayRef(DataTypes));
2600 return ProfileDataTy;
2601}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
unsigned uint64_t
This file contains the simple types necessary to represent the attributes associated with functions a...
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
#define LLVM_ABI
Definition Compiler.h:215
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file declares the LLVM IR specialization of the GenericCycle templates.
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 void doAtomicCheck(Function *F)
static GlobalVariable * emitGPUOffloadSectionsStruct(Module &M, StringRef CUIDPostfix)
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:54
#define I(x, y, z)
Definition MD5.cpp:57
Memory SSA
Definition MemorySSA.cpp:73
This file provides the interface for IR based instrumentation passes ( (profile-gen,...
FunctionAnalysisManager FAM
if(PassOpts->AAPipeline)
SmallPtrSet< BasicBlock *, 0 > 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:119
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:67
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:459
iterator begin()
Instruction iterator methods.
Definition BasicBlock.h:446
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...
LLVM_ABI BasicBlock * splitBasicBlock(iterator I, const Twine &BBName="")
Split the basic block into two basic blocks at the specified instruction.
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:469
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction; assumes that the block is well-formed.
Definition BasicBlock.h:237
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:590
static LLVM_ABI Constant * get(ArrayType *T, ArrayRef< Constant * > V)
static ConstantAsMetadata * get(Constant *C)
Definition Metadata.h:537
static LLVM_ABI Constant * getString(LLVMContext &Context, StringRef Initializer, bool AddNull=true, bool ByteString=false)
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)
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
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:168
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...
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition Constant.h:64
static LLVM_ABI Constant * getAllOnesValue(Type *Ty)
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
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:168
const BasicBlock & getEntryBlock() const
Definition Function.h:793
DISubprogram * getSubprogram() const
Get the attached subprogram.
const Function & getFunction() const
Definition Function.h:166
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition Function.cpp:353
void compute(FunctionT &F)
Compute the cycle info for a function.
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:692
bool hasMetadata() const
Return true if this GlobalObject has any metadata attached to it.
LLVM_ABI void setComdat(Comdat *C)
Definition Globals.cpp:287
bool hasComdat() const
LLVM_ABI void setSection(StringRef S)
Change the section for this global.
Definition Globals.cpp:348
bool hasLinkOnceLinkage() const
VisibilityTypes getVisibility() const
static bool isLocalLinkage(LinkageTypes Linkage)
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:408
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)
static bool isWeakForLinker(LinkageTypes Linkage)
Whether the definition of this global may be replaced at link time.
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 uint64_t getGlobalSize(const DataLayout &DL) const
Get the size of this global variable in bytes.
Definition Globals.cpp:640
LLVM_ABI void eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing module and deletes it.
Definition Globals.cpp:609
void setAlignment(Align Align)
Sets the alignment attribute of the GlobalVariable.
Value * CreateZExtOrTrunc(Value *V, Type *DestTy, const Twine &Name="")
Create a ZExt or Trunc from the integer value V to DestTy.
Definition IRBuilder.h:2149
Value * CreateIntToPtr(Value *V, Type *DestTy, const Twine &Name="")
Definition IRBuilder.h:2248
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition IRBuilder.h:1542
ConstantInt * getInt8(uint8_t C)
Get a constant 8-bit value.
Definition IRBuilder.h:467
Value * CreatePtrAdd(Value *Ptr, Value *Offset, const Twine &Name="", GEPNoWrapFlags NW=GEPNoWrapFlags::none())
Definition IRBuilder.h:2102
BasicBlock * GetInsertBlock() const
Definition IRBuilder.h:175
Value * CreateInBoundsGEP(Type *Ty, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &Name="")
Definition IRBuilder.h:2029
Value * CreatePointerBitCastOrAddrSpaceCast(Value *V, Type *DestTy, const Twine &Name="")
Definition IRBuilder.h:2312
Value * CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name="")
Definition IRBuilder.h:2389
ConstantInt * getInt32(uint32_t C)
Get a constant 32-bit value.
Definition IRBuilder.h:477
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:1916
Value * CreateShl(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition IRBuilder.h:1521
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition IRBuilder.h:1580
Value * CreateConstInBoundsGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0, unsigned Idx1, const Twine &Name="")
Definition IRBuilder.h:2056
StoreInst * CreateStore(Value *Val, Value *Ptr, bool isVolatile=false)
Definition IRBuilder.h:1935
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition IRBuilder.h:1432
Value * CreatePtrToInt(Value *V, Type *DestTy, const Twine &Name="")
Definition IRBuilder.h:2243
Value * CreateIsNotNull(Value *Arg, const Twine &Name="")
Return a boolean value testing if Arg != 0.
Definition IRBuilder.h:2752
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value * > Args={}, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition IRBuilder.h:2564
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="", bool IsNUW=false, bool IsNSW=false)
Definition IRBuilder.h:2117
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block.
Definition IRBuilder.h:181
Value * CreateInBoundsPtrAdd(Value *Ptr, Value *Offset, const Twine &Name="")
Definition IRBuilder.h:2107
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="", bool IsDisjoint=false)
Definition IRBuilder.h:1602
AtomicRMWInst * CreateAtomicRMW(AtomicRMWInst::BinOp Op, Value *Ptr, Value *Val, MaybeAlign Align, AtomicOrdering Ordering, SyncScope::ID SSID=SyncScope::System, bool Elementwise=false)
Definition IRBuilder.h:1991
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2903
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
static LLVM_ABI const char * NumBitmapBitsAttributeName
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.
ConstantInt * getNumBitmapBits() const
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:149
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...
void analyze(ParentT F)
Create the loop forest for a function.
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:1069
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1567
static LLVM_ABI MDString * get(LLVMContext &Context, StringRef Str)
Definition Metadata.cpp:615
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(LLVMContext &C)
This constructs an opaque pointer to an object in the default address space (address space zero).
static LLVM_ABI PointerType * get(LLVMContext &C, unsigned AddressSpace)
This constructs an opaque pointer to an object in a numbered address space.
Definition Type.cpp:911
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
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
An instruction for storing to memory.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition StringRef.h:597
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition StringRef.h:258
constexpr size_t size() const
Get the string size.
Definition StringRef.h:144
Class to represent struct types.
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:477
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:48
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:46
static LLVM_ABI IntegerType * getInt64Ty(LLVMContext &C)
Definition Type.cpp:310
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:309
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
Definition Type.cpp:282
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:307
static LLVM_ABI IntegerType * getInt16Ty(LLVMContext &C)
Definition Type.cpp:308
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition Type.h:130
Value * getOperand(unsigned i) const
Definition User.h:207
unsigned getNumOperands() const
Definition User.h:229
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:255
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition Value.cpp:553
LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.h:258
LLVM_ABI const Value * stripInBoundsOffsets(function_ref< void(const Value *)> Func=[](const Value *) {}) const
Strip off pointer casts and inbounds GEPs.
Definition Value.cpp:828
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:319
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.
@ 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.
LLVM_ABI Function * getOrInsertDeclaration(Module *M, ID id, ArrayRef< Type * > OverloadTys={})
Look up the Function declaration of the intrinsic id in the Module M.
LLVM_ABI ID lookupIntrinsicID(StringRef Name)
This does the actual lookup of an intrinsic ID which matches the given function name.
constexpr bool isAtomic(const T &...O)
Definition SIDefines.h:396
@ 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)
DXILDebugInfoMap run(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.
StringRef getInstrProfNameVarPrefix()
Return the name prefix of variables containing instrumented function names.
Definition InstrProf.h:131
RelativeUniformCounterPtr ValuesPtrExpr NumBitmapBytes
Definition InstrProf.h:101
StringRef getInstrProfRuntimeHookVarName()
Return the name of the hook variable defined in profile runtime library.
Definition InstrProf.h:206
UniformCounterPtr
Definition InstrProf.h:82
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI void createProfileSamplingVar(Module &M)
@ Load
The value being inserted comes from a load (InsertElement only).
@ Store
The extracted value is stored (ExtractElement only).
StringRef getInstrProfBitmapVarPrefix()
Return the name prefix of profile bitmap variables.
Definition InstrProf.h:143
LLVM_ABI cl::opt< bool > DoInstrProfNameCompression
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:633
InnerAnalysisManagerProxy< FunctionAnalysisManager, Module > FunctionAnalysisManagerModuleProxy
Provide the FunctionAnalysisManager to Module proxy.
StringRef getInstrProfVTableNamesVarName()
Definition InstrProf.h:159
StringRef getInstrProfDataVarPrefix()
Return the name prefix of variables containing per-function control data.
Definition InstrProf.h:137
RelativeUniformCounterPtr ValuesPtrExpr Int16ArrayTy
Definition InstrProf.h:95
StringRef getCoverageUnusedNamesVarName()
Return the name of the internal variable recording the array of PGO name vars referenced by the cover...
Definition InstrProf.h:172
LLVM_ABI std::string getInstrProfSectionName(InstrProfSectKind IPSK, Triple::ObjectFormatType OF, bool AddSegmentInfo=true)
Return the name of the profile section corresponding to IPSK.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
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
FuncHash
Definition InstrProf.h:78
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:201
StringRef getInstrProfValuesVarPrefix()
Return the name prefix of value profile variables.
Definition InstrProf.h:146
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:1746
StringRef getInstrProfCounterBiasVarName()
Definition InstrProf.h:216
auto reverse(ContainerTy &&C)
Definition STLExtras.h:407
StringRef getInstrProfRuntimeHookVarUseFuncName()
Return the name of the compiler generated function that references the runtime hook variable.
Definition InstrProf.h:212
StringRef getInstrProfRegFuncsName()
Return the name of function that registers all the per-function control data at program startup time ...
Definition InstrProf.h:181
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:91
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:140
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
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:1753
inst_range instructions(Function *F)
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
LLVM_ABI StringRef getPGOFuncNameVarInitializer(GlobalVariable *NameVar)
Return the initializer in string of the PGO name var NameVar.
StringRef getInstrProfBitmapBiasVarName()
Definition InstrProf.h:220
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:118
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
LLVM_ABI void removeFromUsedLists(Module &M, function_ref< bool(Constant *)> ShouldRemove)
Removes global values from the llvm.used and llvm.compiler.used arrays.
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:193
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)
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
IntPtrTy
Definition InstrProf.h:82
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)
StringRef getInstrProfNamesVarPostfixVarName()
Definition InstrProf.h:155
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:424
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:112
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:187
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:153
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:149
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."))
@ Extern
Replace returns with jump to thunk, don't emit thunk.
Definition CodeGen.h:230
StringRef getInstrProfVTableVarPrefix()
Return the name prefix of variables containing virtual table profile data.
Definition InstrProf.h:134
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
static StringRef getLibcallImplName(RTLIB::LibcallImpl CallImpl)
Get the libcall routine name for the specified libcall implementation.