LLVM 18.0.0git
InstrProfiling.h
Go to the documentation of this file.
1//===- Transforms/Instrumentation/InstrProfiling.h --------------*- C++ -*-===//
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/// \file
9/// This file provides the interface for LLVM's PGO Instrumentation lowering
10/// pass.
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_TRANSFORMS_INSTRUMENTATION_INSTRPROFILING_H
14#define LLVM_TRANSFORMS_INSTRUMENTATION_INSTRPROFILING_H
15
16#include "llvm/ADT/DenseMap.h"
17#include "llvm/ADT/StringRef.h"
19#include "llvm/IR/PassManager.h"
22#include <cstdint>
23#include <cstring>
24#include <vector>
25
26namespace llvm {
27
28class TargetLibraryInfo;
29using LoadStorePair = std::pair<Instruction *, Instruction *>;
30
31/// Instrumentation based profiling lowering pass. This pass lowers
32/// the profile instrumented code generated by FE or the IR based
33/// instrumentation pass.
34class InstrProfiling : public PassInfoMixin<InstrProfiling> {
35public:
36 InstrProfiling() : IsCS(false) {}
37 InstrProfiling(const InstrProfOptions &Options, bool IsCS = false)
38 : Options(Options), IsCS(IsCS) {}
39
41 bool run(Module &M,
42 std::function<const TargetLibraryInfo &(Function &F)> GetTLI);
43
44private:
45 InstrProfOptions Options;
46 Module *M;
47 Triple TT;
48 std::function<const TargetLibraryInfo &(Function &F)> GetTLI;
49 struct PerFunctionProfileData {
50 uint32_t NumValueSites[IPVK_Last + 1];
51 GlobalVariable *RegionCounters = nullptr;
52 GlobalVariable *DataVar = nullptr;
53
54 PerFunctionProfileData() {
55 memset(NumValueSites, 0, sizeof(uint32_t) * (IPVK_Last + 1));
56 }
57 };
58 DenseMap<GlobalVariable *, PerFunctionProfileData> ProfileDataMap;
59 /// If runtime relocation is enabled, this maps functions to the load
60 /// instruction that produces the profile relocation bias.
61 DenseMap<const Function *, LoadInst *> FunctionToProfileBiasMap;
62 std::vector<GlobalValue *> CompilerUsedVars;
63 std::vector<GlobalValue *> UsedVars;
64 std::vector<GlobalVariable *> ReferencedNames;
65 GlobalVariable *NamesVar;
66 size_t NamesSize;
67
68 // Is this lowering for the context-sensitive instrumentation.
69 bool IsCS;
70
71 // vector of counter load/store pairs to be register promoted.
72 std::vector<LoadStorePair> PromotionCandidates;
73
74 int64_t TotalCountersPromoted = 0;
75
76 /// Lower instrumentation intrinsics in the function. Returns true if there
77 /// any lowering.
78 bool lowerIntrinsics(Function *F);
79
80 /// Register-promote counter loads and stores in loops.
81 void promoteCounterLoadStores(Function *F);
82
83 /// Returns true if relocating counters at runtime is enabled.
84 bool isRuntimeCounterRelocationEnabled() const;
85
86 /// Returns true if profile counter update register promotion is enabled.
87 bool isCounterPromotionEnabled() const;
88
89 /// Count the number of instrumented value sites for the function.
90 void computeNumValueSiteCounts(InstrProfValueProfileInst *Ins);
91
92 /// Replace instrprof.value.profile with a call to runtime library.
93 void lowerValueProfileInst(InstrProfValueProfileInst *Ins);
94
95 /// Replace instrprof.cover with a store instruction to the coverage byte.
96 void lowerCover(InstrProfCoverInst *Inc);
97
98 /// Replace instrprof.timestamp with a call to
99 /// INSTR_PROF_PROFILE_SET_TIMESTAMP.
100 void lowerTimestamp(InstrProfTimestampInst *TimestampInstruction);
101
102 /// Replace instrprof.increment with an increment of the appropriate value.
103 void lowerIncrement(InstrProfIncrementInst *Inc);
104
105 /// Force emitting of name vars for unused functions.
106 void lowerCoverageData(GlobalVariable *CoverageNamesVar);
107
108 /// Compute the address of the counter value that this profiling instruction
109 /// acts on.
110 Value *getCounterAddress(InstrProfInstBase *I);
111
112 /// Get the region counters for an increment, creating them if necessary.
113 ///
114 /// If the counter array doesn't yet exist, the profile data variables
115 /// referring to them will also be created.
116 GlobalVariable *getOrCreateRegionCounters(InstrProfInstBase *Inc);
117
118 /// Create the region counters.
119 GlobalVariable *createRegionCounters(InstrProfInstBase *Inc, StringRef Name,
121
122 /// Emit the section with compressed function names.
123 void emitNameData();
124
125 /// Emit value nodes section for value profiling.
126 void emitVNodes();
127
128 /// Emit runtime registration functions for each profile data variable.
129 void emitRegistration();
130
131 /// Emit the necessary plumbing to pull in the runtime initialization.
132 /// Returns true if a change was made.
133 bool emitRuntimeHook();
134
135 /// Add uses of our data variables and runtime hook.
136 void emitUses();
137
138 /// Create a static initializer for our data, on platforms that need it,
139 /// and for any profile output file that was specified.
140 void emitInitialization();
141};
142
143} // end namespace llvm
144
145#endif // LLVM_TRANSFORMS_INSTRUMENTATION_INSTRPROFILING_H
This file defines the DenseMap class.
std::string Name
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
const char LLVMTargetMachineRef LLVMPassBuilderOptionsRef Options
This header defines various interfaces for pass management in LLVM.
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:620
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition: GlobalValue.h:47
Instrumentation based profiling lowering pass.
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
InstrProfiling(const InstrProfOptions &Options, bool IsCS=false)
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:152
Provides information about what library functions are available for the current target.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::pair< Instruction *, Instruction * > LoadStorePair
Options for the frontend instrumentation based profiling pass.
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:371