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