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"
29 class OptimizationRemarkEmitter;
31 /// Instrumentation passes often insert conditional checks into entry blocks.
32 /// Call this function before splitting the entry block to move instructions
33 /// that must remain in the entry block up before the split point. Static
34 /// allocas and llvm.localescape calls, for example, must remain in the entry
36 BasicBlock::iterator PrepareToSplitEntryBlock(BasicBlock &BB,
37 BasicBlock::iterator IP);
39 // Insert GCOV profiling instrumentation
41 static GCOVOptions getDefault();
43 // Specify whether to emit .gcno files.
46 // Specify whether to modify the program to emit .gcda files when run.
49 // A four-byte version string. The meaning of a version string is described in
53 // Emit a "cfg checksum" that follows the "line number checksum" of a
54 // function. This affects both .gcno and .gcda files.
57 // Add the 'noredzone' attribute to added runtime library calls.
60 // Emit the name of the function in the .gcda files. This is redundant, as
61 // the function identifier can be used to find the name from the .gcno file.
62 bool FunctionNamesInData;
64 // Emit the exit block immediately after the start block, rather than after
65 // all of the function body's blocks.
66 bool ExitBlockBeforeBody;
69 ModulePass *createGCOVProfilerPass(const GCOVOptions &Options =
70 GCOVOptions::getDefault());
73 ModulePass *createPGOInstrumentationGenLegacyPass();
75 createPGOInstrumentationUseLegacyPass(StringRef Filename = StringRef(""));
76 ModulePass *createPGOIndirectCallPromotionLegacyPass(bool InLTO = false,
77 bool SamplePGO = false);
78 FunctionPass *createPGOMemOPSizeOptLegacyPass();
80 // The pgo-specific indirect call promotion function declared below is used by
81 // the pgo-driven indirect call promotion and sample profile passes. It's a
82 // wrapper around llvm::promoteCall, et al. that additionally computes !prof
83 // metadata. We place it in a pgo namespace so it's not confused with the
87 // Helper function that transforms Inst (either an indirect-call instruction, or
88 // an invoke instruction , to a conditional call to F. This is like:
89 // if (Inst.CalledValue == F)
94 // TotalCount is the profile count value that the instruction executes.
95 // Count is the profile count value that F is the target function.
96 // These two values are used to update the branch weight.
97 // If \p AttachProfToDirectCall is true, a prof metadata is attached to the
98 // new direct call to contain \p Count.
99 // Returns the promoted direct call instruction.
100 Instruction *promoteIndirectCall(Instruction *Inst, Function *F, uint64_t Count,
102 bool AttachProfToDirectCall,
103 OptimizationRemarkEmitter *ORE);
106 /// Options for the frontend instrumentation based profiling pass.
107 struct InstrProfOptions {
108 // Add the 'noredzone' attribute to added runtime library calls.
109 bool NoRedZone = false;
111 // Do counter register promotion
112 bool DoCounterPromotion = false;
114 // Name of the profile file to use as output
115 std::string InstrProfileOutput;
117 InstrProfOptions() = default;
120 /// Insert frontend instrumentation based profiling.
121 ModulePass *createInstrProfilingLegacyPass(
122 const InstrProfOptions &Options = InstrProfOptions());
124 // Insert AddressSanitizer (address sanity checking) instrumentation
125 FunctionPass *createAddressSanitizerFunctionPass(bool CompileKernel = false,
126 bool Recover = false,
127 bool UseAfterScope = false);
128 ModulePass *createAddressSanitizerModulePass(bool CompileKernel = false,
129 bool Recover = false,
130 bool UseGlobalsGC = true);
132 // Insert MemorySanitizer instrumentation (detection of uninitialized reads)
133 FunctionPass *createMemorySanitizerPass(int TrackOrigins = 0,
134 bool Recover = false);
136 FunctionPass *createHWAddressSanitizerPass(bool CompileKernel = false,
137 bool Recover = false);
139 // Insert ThreadSanitizer (race detection) instrumentation
140 FunctionPass *createThreadSanitizerPass();
142 // Insert DataFlowSanitizer (dynamic data flow analysis) instrumentation
143 ModulePass *createDataFlowSanitizerPass(
144 const std::vector<std::string> &ABIListFiles = std::vector<std::string>(),
145 void *(*getArgTLS)() = nullptr, void *(*getRetValTLS)() = nullptr);
147 // Options for EfficiencySanitizer sub-tools.
148 struct EfficiencySanitizerOptions {
153 } ToolType = ESAN_None;
155 EfficiencySanitizerOptions() = default;
158 // Insert EfficiencySanitizer instrumentation.
159 ModulePass *createEfficiencySanitizerPass(
160 const EfficiencySanitizerOptions &Options = EfficiencySanitizerOptions());
162 // Options for sanitizer coverage instrumentation.
163 struct SanitizerCoverageOptions {
169 } CoverageType = SCK_None;
170 bool IndirectCalls = false;
171 bool TraceBB = false;
172 bool TraceCmp = false;
173 bool TraceDiv = false;
174 bool TraceGep = false;
175 bool Use8bitCounters = false;
176 bool TracePC = false;
177 bool TracePCGuard = false;
178 bool Inline8bitCounters = false;
179 bool PCTable = false;
180 bool NoPrune = false;
181 bool StackDepth = false;
183 SanitizerCoverageOptions() = default;
186 // Insert SanitizerCoverage instrumentation.
187 ModulePass *createSanitizerCoverageModulePass(
188 const SanitizerCoverageOptions &Options = SanitizerCoverageOptions());
190 /// Calculate what to divide by to scale counts.
192 /// Given the maximum count, calculate a divisor that will scale all the
193 /// weights to strictly less than std::numeric_limits<uint32_t>::max().
194 static inline uint64_t calculateCountScale(uint64_t MaxCount) {
195 return MaxCount < std::numeric_limits<uint32_t>::max()
197 : MaxCount / std::numeric_limits<uint32_t>::max() + 1;
200 /// Scale an individual branch count.
202 /// Scale a 64-bit weight down to 32-bits using \c Scale.
204 static inline uint32_t scaleBranchCount(uint64_t Count, uint64_t Scale) {
205 uint64_t Scaled = Count / Scale;
206 assert(Scaled <= std::numeric_limits<uint32_t>::max() && "overflow 32-bits");
210 } // end namespace llvm
212 #endif // LLVM_TRANSFORMS_INSTRUMENTATION_H