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