1 //===- Transforms/Instrumentation.h - Instrumentation passes ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines constructor functions for instrumentation passes.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_TRANSFORMS_INSTRUMENTATION_H
15 #define LLVM_TRANSFORMS_INSTRUMENTATION_H
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/IR/BasicBlock.h"
25 #if defined(__GNUC__) && defined(__linux__) && !defined(ANDROID)
26 inline void *getDFSanArgTLSPtrForJIT() {
27 extern __thread __attribute__((tls_model("initial-exec")))
28 void *__dfsan_arg_tls;
29 return (void *)&__dfsan_arg_tls;
32 inline void *getDFSanRetValTLSPtrForJIT() {
33 extern __thread __attribute__((tls_model("initial-exec")))
34 void *__dfsan_retval_tls;
35 return (void *)&__dfsan_retval_tls;
44 /// Instrumentation passes often insert conditional checks into entry blocks.
45 /// Call this function before splitting the entry block to move instructions
46 /// that must remain in the entry block up before the split point. Static
47 /// allocas and llvm.localescape calls, for example, must remain in the entry
49 BasicBlock::iterator PrepareToSplitEntryBlock(BasicBlock &BB,
50 BasicBlock::iterator IP);
52 // Insert GCOV profiling instrumentation
54 static GCOVOptions getDefault();
56 // Specify whether to emit .gcno files.
59 // Specify whether to modify the program to emit .gcda files when run.
62 // A four-byte version string. The meaning of a version string is described in
66 // Emit a "cfg checksum" that follows the "line number checksum" of a
67 // function. This affects both .gcno and .gcda files.
70 // Add the 'noredzone' attribute to added runtime library calls.
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;
77 // Emit the exit block immediately after the start block, rather than after
78 // all of the function body's blocks.
79 bool ExitBlockBeforeBody;
82 ModulePass *createGCOVProfilerPass(const GCOVOptions &Options =
83 GCOVOptions::getDefault());
86 ModulePass *createPGOInstrumentationGenLegacyPass();
88 createPGOInstrumentationUseLegacyPass(StringRef Filename = StringRef(""));
89 ModulePass *createPGOIndirectCallPromotionLegacyPass(bool InLTO = false,
90 bool SamplePGO = false);
91 FunctionPass *createPGOMemOPSizeOptLegacyPass();
93 // Helper function to check if it is legal to promote indirect call \p Inst
94 // to a direct call of function \p F. Stores the reason in \p Reason.
95 bool isLegalToPromote(Instruction *Inst, Function *F, const char **Reason);
97 // Helper function that transforms Inst (either an indirect-call instruction, or
98 // an invoke instruction , to a conditional call to F. This is like:
99 // if (Inst.CalledValue == F)
104 // TotalCount is the profile count value that the instruction executes.
105 // Count is the profile count value that F is the target function.
106 // These two values are used to update the branch weight.
107 // If \p AttachProfToDirectCall is true, a prof metadata is attached to the
108 // new direct call to contain \p Count.
109 // Returns the promoted direct call instruction.
110 Instruction *promoteIndirectCall(Instruction *Inst, Function *F, uint64_t Count,
112 bool AttachProfToDirectCall);
114 /// Options for the frontend instrumentation based profiling pass.
115 struct InstrProfOptions {
116 // Add the 'noredzone' attribute to added runtime library calls.
117 bool NoRedZone = false;
119 // Name of the profile file to use as output
120 std::string InstrProfileOutput;
122 InstrProfOptions() = default;
125 /// Insert frontend instrumentation based profiling.
126 ModulePass *createInstrProfilingLegacyPass(
127 const InstrProfOptions &Options = InstrProfOptions());
129 // Insert AddressSanitizer (address sanity checking) instrumentation
130 FunctionPass *createAddressSanitizerFunctionPass(bool CompileKernel = false,
131 bool Recover = false,
132 bool UseAfterScope = false);
133 ModulePass *createAddressSanitizerModulePass(bool CompileKernel = false,
134 bool Recover = false);
136 // Insert MemorySanitizer instrumentation (detection of uninitialized reads)
137 FunctionPass *createMemorySanitizerPass(int TrackOrigins = 0,
138 bool Recover = false);
140 // Insert ThreadSanitizer (race detection) instrumentation
141 FunctionPass *createThreadSanitizerPass();
143 // Insert DataFlowSanitizer (dynamic data flow analysis) instrumentation
144 ModulePass *createDataFlowSanitizerPass(
145 const std::vector<std::string> &ABIListFiles = std::vector<std::string>(),
146 void *(*getArgTLS)() = nullptr, void *(*getRetValTLS)() = nullptr);
148 // Options for EfficiencySanitizer sub-tools.
149 struct EfficiencySanitizerOptions {
154 } ToolType = ESAN_None;
156 EfficiencySanitizerOptions() = default;
159 // Insert EfficiencySanitizer instrumentation.
160 ModulePass *createEfficiencySanitizerPass(
161 const EfficiencySanitizerOptions &Options = EfficiencySanitizerOptions());
163 // Options for sanitizer coverage instrumentation.
164 struct SanitizerCoverageOptions {
170 } CoverageType = SCK_None;
171 bool IndirectCalls = false;
172 bool TraceBB = false;
173 bool TraceCmp = false;
174 bool TraceDiv = false;
175 bool TraceGep = false;
176 bool Use8bitCounters = false;
177 bool TracePC = false;
178 bool TracePCGuard = false;
180 SanitizerCoverageOptions() = default;
183 // Insert SanitizerCoverage instrumentation.
184 ModulePass *createSanitizerCoverageModulePass(
185 const SanitizerCoverageOptions &Options = SanitizerCoverageOptions());
187 #if defined(__GNUC__) && defined(__linux__) && !defined(ANDROID)
188 inline ModulePass *createDataFlowSanitizerPassForJIT(
189 const std::vector<std::string> &ABIListFiles = std::vector<std::string>()) {
190 return createDataFlowSanitizerPass(ABIListFiles, getDFSanArgTLSPtrForJIT,
191 getDFSanRetValTLSPtrForJIT);
195 // BoundsChecking - This pass instruments the code to perform run-time bounds
196 // checking on loads, stores, and other memory intrinsics.
197 FunctionPass *createBoundsCheckingPass();
199 /// \brief Calculate what to divide by to scale counts.
201 /// Given the maximum count, calculate a divisor that will scale all the
202 /// weights to strictly less than std::numeric_limits<uint32_t>::max().
203 static inline uint64_t calculateCountScale(uint64_t MaxCount) {
204 return MaxCount < std::numeric_limits<uint32_t>::max()
206 : MaxCount / std::numeric_limits<uint32_t>::max() + 1;
209 /// \brief Scale an individual branch count.
211 /// Scale a 64-bit weight down to 32-bits using \c Scale.
213 static inline uint32_t scaleBranchCount(uint64_t Count, uint64_t Scale) {
214 uint64_t Scaled = Count / Scale;
215 assert(Scaled <= std::numeric_limits<uint32_t>::max() && "overflow 32-bits");
219 } // end namespace llvm
221 #endif // LLVM_TRANSFORMS_INSTRUMENTATION_H