1 | //===- Transforms/Instrumentation.h - Instrumentation passes ----*- C++ -*-===// |
2 | // |
3 | // The LLVM Compiler Infrastructure |
4 | // |
5 | // This file is distributed under the University of Illinois Open Source |
6 | // License. See LICENSE.TXT for details. |
7 | // |
8 | //===----------------------------------------------------------------------===// |
9 | // |
10 | // This file defines constructor functions for instrumentation passes. |
11 | // |
12 | //===----------------------------------------------------------------------===// |
13 | |
14 | #ifndef LLVM_TRANSFORMS_INSTRUMENTATION_H |
15 | #define LLVM_TRANSFORMS_INSTRUMENTATION_H |
16 | |
17 | #include "llvm/ADT/StringRef.h" |
18 | #include "llvm/IR/BasicBlock.h" |
19 | #include <cassert> |
20 | #include <cstdint> |
21 | #include <limits> |
22 | #include <string> |
23 | #include <vector> |
24 | |
25 | namespace llvm { |
26 | |
27 | class Triple; |
28 | class FunctionPass; |
29 | class ModulePass; |
30 | class ; |
31 | class Comdat; |
32 | |
33 | /// Instrumentation passes often insert conditional checks into entry blocks. |
34 | /// Call this function before splitting the entry block to move instructions |
35 | /// that must remain in the entry block up before the split point. Static |
36 | /// allocas and llvm.localescape calls, for example, must remain in the entry |
37 | /// block. |
38 | BasicBlock::iterator PrepareToSplitEntryBlock(BasicBlock &BB, |
39 | BasicBlock::iterator IP); |
40 | |
41 | // Create a constant for Str so that we can pass it to the run-time lib. |
42 | GlobalVariable *createPrivateGlobalForString(Module &M, StringRef Str, |
43 | bool AllowMerging, |
44 | const char *NamePrefix = "" ); |
45 | |
46 | // Returns F.getComdat() if it exists. |
47 | // Otherwise creates a new comdat, sets F's comdat, and returns it. |
48 | // Returns nullptr on failure. |
49 | Comdat *GetOrCreateFunctionComdat(Function &F, Triple &T, |
50 | const std::string &ModuleId); |
51 | |
52 | // Insert GCOV profiling instrumentation |
53 | struct GCOVOptions { |
54 | static GCOVOptions getDefault(); |
55 | |
56 | // Specify whether to emit .gcno files. |
57 | bool EmitNotes; |
58 | |
59 | // Specify whether to modify the program to emit .gcda files when run. |
60 | bool EmitData; |
61 | |
62 | // A four-byte version string. The meaning of a version string is described in |
63 | // gcc's gcov-io.h |
64 | char Version[4]; |
65 | |
66 | // Emit a "cfg checksum" that follows the "line number checksum" of a |
67 | // function. This affects both .gcno and .gcda files. |
68 | bool UseCfgChecksum; |
69 | |
70 | // Add the 'noredzone' attribute to added runtime library calls. |
71 | bool NoRedZone; |
72 | |
73 | // Emit the name of the function in the .gcda files. This is redundant, as |
74 | // the function identifier can be used to find the name from the .gcno file. |
75 | bool FunctionNamesInData; |
76 | |
77 | // Emit the exit block immediately after the start block, rather than after |
78 | // all of the function body's blocks. |
79 | bool ExitBlockBeforeBody; |
80 | |
81 | // Regexes separated by a semi-colon to filter the files to instrument. |
82 | std::string Filter; |
83 | |
84 | // Regexes separated by a semi-colon to filter the files to not instrument. |
85 | std::string Exclude; |
86 | }; |
87 | |
88 | ModulePass *createGCOVProfilerPass(const GCOVOptions &Options = |
89 | GCOVOptions::getDefault()); |
90 | |
91 | // PGO Instrumention |
92 | ModulePass *createPGOInstrumentationGenLegacyPass(); |
93 | ModulePass * |
94 | createPGOInstrumentationUseLegacyPass(StringRef Filename = StringRef("" )); |
95 | ModulePass *createPGOIndirectCallPromotionLegacyPass(bool InLTO = false, |
96 | bool SamplePGO = false); |
97 | FunctionPass *createPGOMemOPSizeOptLegacyPass(); |
98 | |
99 | // The pgo-specific indirect call promotion function declared below is used by |
100 | // the pgo-driven indirect call promotion and sample profile passes. It's a |
101 | // wrapper around llvm::promoteCall, et al. that additionally computes !prof |
102 | // metadata. We place it in a pgo namespace so it's not confused with the |
103 | // generic utilities. |
104 | namespace pgo { |
105 | |
106 | // Helper function that transforms Inst (either an indirect-call instruction, or |
107 | // an invoke instruction , to a conditional call to F. This is like: |
108 | // if (Inst.CalledValue == F) |
109 | // F(...); |
110 | // else |
111 | // Inst(...); |
112 | // end |
113 | // TotalCount is the profile count value that the instruction executes. |
114 | // Count is the profile count value that F is the target function. |
115 | // These two values are used to update the branch weight. |
116 | // If \p AttachProfToDirectCall is true, a prof metadata is attached to the |
117 | // new direct call to contain \p Count. |
118 | // Returns the promoted direct call instruction. |
119 | Instruction *(Instruction *Inst, Function *F, uint64_t Count, |
120 | uint64_t TotalCount, |
121 | bool AttachProfToDirectCall, |
122 | OptimizationRemarkEmitter *ORE); |
123 | } // namespace pgo |
124 | |
125 | /// Options for the frontend instrumentation based profiling pass. |
126 | struct InstrProfOptions { |
127 | // Add the 'noredzone' attribute to added runtime library calls. |
128 | bool NoRedZone = false; |
129 | |
130 | // Do counter register promotion |
131 | bool DoCounterPromotion = false; |
132 | |
133 | // Use atomic profile counter increments. |
134 | bool Atomic = false; |
135 | |
136 | // Name of the profile file to use as output |
137 | std::string InstrProfileOutput; |
138 | |
139 | InstrProfOptions() = default; |
140 | }; |
141 | |
142 | /// Insert frontend instrumentation based profiling. |
143 | ModulePass *createInstrProfilingLegacyPass( |
144 | const InstrProfOptions &Options = InstrProfOptions()); |
145 | |
146 | // Insert AddressSanitizer (address sanity checking) instrumentation |
147 | FunctionPass *createAddressSanitizerFunctionPass(bool CompileKernel = false, |
148 | bool Recover = false, |
149 | bool UseAfterScope = false); |
150 | ModulePass *createAddressSanitizerModulePass(bool CompileKernel = false, |
151 | bool Recover = false, |
152 | bool UseGlobalsGC = true, |
153 | bool UseOdrIndicator = true); |
154 | |
155 | FunctionPass *createHWAddressSanitizerPass(bool CompileKernel = false, |
156 | bool Recover = false); |
157 | |
158 | // Insert DataFlowSanitizer (dynamic data flow analysis) instrumentation |
159 | ModulePass *createDataFlowSanitizerPass( |
160 | const std::vector<std::string> &ABIListFiles = std::vector<std::string>(), |
161 | void *(*getArgTLS)() = nullptr, void *(*getRetValTLS)() = nullptr); |
162 | |
163 | // Options for EfficiencySanitizer sub-tools. |
164 | struct EfficiencySanitizerOptions { |
165 | enum Type { |
166 | ESAN_None = 0, |
167 | ESAN_CacheFrag, |
168 | ESAN_WorkingSet, |
169 | } ToolType = ESAN_None; |
170 | |
171 | EfficiencySanitizerOptions() = default; |
172 | }; |
173 | |
174 | // Insert EfficiencySanitizer instrumentation. |
175 | ModulePass *createEfficiencySanitizerPass( |
176 | const EfficiencySanitizerOptions &Options = EfficiencySanitizerOptions()); |
177 | |
178 | // Options for sanitizer coverage instrumentation. |
179 | struct SanitizerCoverageOptions { |
180 | enum Type { |
181 | SCK_None = 0, |
182 | SCK_Function, |
183 | SCK_BB, |
184 | SCK_Edge |
185 | } CoverageType = SCK_None; |
186 | bool IndirectCalls = false; |
187 | bool TraceBB = false; |
188 | bool TraceCmp = false; |
189 | bool TraceDiv = false; |
190 | bool TraceGep = false; |
191 | bool Use8bitCounters = false; |
192 | bool TracePC = false; |
193 | bool TracePCGuard = false; |
194 | bool Inline8bitCounters = false; |
195 | bool PCTable = false; |
196 | bool NoPrune = false; |
197 | bool StackDepth = false; |
198 | |
199 | SanitizerCoverageOptions() = default; |
200 | }; |
201 | |
202 | // Insert SanitizerCoverage instrumentation. |
203 | ModulePass *createSanitizerCoverageModulePass( |
204 | const SanitizerCoverageOptions &Options = SanitizerCoverageOptions()); |
205 | |
206 | /// Calculate what to divide by to scale counts. |
207 | /// |
208 | /// Given the maximum count, calculate a divisor that will scale all the |
209 | /// weights to strictly less than std::numeric_limits<uint32_t>::max(). |
210 | static inline uint64_t calculateCountScale(uint64_t MaxCount) { |
211 | return MaxCount < std::numeric_limits<uint32_t>::max() |
212 | ? 1 |
213 | : MaxCount / std::numeric_limits<uint32_t>::max() + 1; |
214 | } |
215 | |
216 | /// Scale an individual branch count. |
217 | /// |
218 | /// Scale a 64-bit weight down to 32-bits using \c Scale. |
219 | /// |
220 | static inline uint32_t scaleBranchCount(uint64_t Count, uint64_t Scale) { |
221 | uint64_t Scaled = Count / Scale; |
222 | assert(Scaled <= std::numeric_limits<uint32_t>::max() && "overflow 32-bits" ); |
223 | return Scaled; |
224 | } |
225 | } // end namespace llvm |
226 | |
227 | #endif // LLVM_TRANSFORMS_INSTRUMENTATION_H |
228 | |