LLVM  8.0.0svn
Instrumentation.h
Go to the documentation of this file.
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 OptimizationRemarkEmitter;
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.
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.
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.
76 
77  // Emit the exit block immediately after the start block, rather than after
78  // all of the function body's blocks.
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 
90 
91 // PGO Instrumention
93 ModulePass *
96  bool SamplePGO = false);
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 *promoteIndirectCall(Instruction *Inst, Function *F, uint64_t Count,
120  uint64_t TotalCount,
121  bool AttachProfToDirectCall,
123 } // namespace pgo
124 
125 /// Options for the frontend instrumentation based profiling pass.
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.
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 
154 // Insert MemorySanitizer instrumentation (detection of uninitialized reads)
155 FunctionPass *createMemorySanitizerPass(int TrackOrigins = 0,
156  bool Recover = false,
157  bool EnableKmsan = false);
158 
159 FunctionPass *createHWAddressSanitizerPass(bool CompileKernel = false,
160  bool Recover = false);
161 
162 // Insert ThreadSanitizer (race detection) instrumentation
164 
165 // Insert DataFlowSanitizer (dynamic data flow analysis) instrumentation
167  const std::vector<std::string> &ABIListFiles = std::vector<std::string>(),
168  void *(*getArgTLS)() = nullptr, void *(*getRetValTLS)() = nullptr);
169 
170 // Options for EfficiencySanitizer sub-tools.
172  enum Type {
173  ESAN_None = 0,
176  } ToolType = ESAN_None;
177 
178  EfficiencySanitizerOptions() = default;
179 };
180 
181 // Insert EfficiencySanitizer instrumentation.
184 
185 // Options for sanitizer coverage instrumentation.
187  enum Type {
188  SCK_None = 0,
191  SCK_Edge
192  } CoverageType = SCK_None;
193  bool IndirectCalls = false;
194  bool TraceBB = false;
195  bool TraceCmp = false;
196  bool TraceDiv = false;
197  bool TraceGep = false;
198  bool Use8bitCounters = false;
199  bool TracePC = false;
200  bool TracePCGuard = false;
201  bool Inline8bitCounters = false;
202  bool PCTable = false;
203  bool NoPrune = false;
204  bool StackDepth = false;
205 
206  SanitizerCoverageOptions() = default;
207 };
208 
209 // Insert SanitizerCoverage instrumentation.
212 
213 /// Calculate what to divide by to scale counts.
214 ///
215 /// Given the maximum count, calculate a divisor that will scale all the
216 /// weights to strictly less than std::numeric_limits<uint32_t>::max().
217 static inline uint64_t calculateCountScale(uint64_t MaxCount) {
219  ? 1
220  : MaxCount / std::numeric_limits<uint32_t>::max() + 1;
221 }
222 
223 /// Scale an individual branch count.
224 ///
225 /// Scale a 64-bit weight down to 32-bits using \c Scale.
226 ///
227 static inline uint32_t scaleBranchCount(uint64_t Count, uint64_t Scale) {
228  uint64_t Scaled = Count / Scale;
229  assert(Scaled <= std::numeric_limits<uint32_t>::max() && "overflow 32-bits");
230  return Scaled;
231 }
232 
233 } // end namespace llvm
234 
235 #endif // LLVM_TRANSFORMS_INSTRUMENTATION_H
ModulePass * createPGOInstrumentationGenLegacyPass()
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
Various leaf nodes.
Definition: ISDOpcodes.h:60
ModulePass * createGCOVProfilerPass(const GCOVOptions &Options=GCOVOptions::getDefault())
FunctionPass * createPGOMemOPSizeOptLegacyPass()
static GCOVOptions getDefault()
F(f)
ModulePass * createInstrProfilingLegacyPass(const InstrProfOptions &Options=InstrProfOptions())
Insert frontend instrumentation based profiling.
ModulePass * createEfficiencySanitizerPass(const EfficiencySanitizerOptions &Options=EfficiencySanitizerOptions())
std::string Filter
Instruction * promoteIndirectCall(Instruction *Inst, Function *F, uint64_t Count, uint64_t TotalCount, bool AttachProfToDirectCall, OptimizationRemarkEmitter *ORE)
ModulePass * createPGOInstrumentationUseLegacyPass(StringRef Filename=StringRef(""))
FunctionPass * createThreadSanitizerPass()
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:285
static uint64_t calculateCountScale(uint64_t MaxCount)
Calculate what to divide by to scale counts.
static uint32_t scaleBranchCount(uint64_t Count, uint64_t Scale)
Scale an individual branch count.
std::string InstrProfileOutput
GlobalVariable * createPrivateGlobalForString(Module &M, StringRef Str, bool AllowMerging, const char *NamePrefix="")
BasicBlock::iterator PrepareToSplitEntryBlock(BasicBlock &BB, BasicBlock::iterator IP)
Instrumentation passes often insert conditional checks into entry blocks.
Comdat * GetOrCreateFunctionComdat(Function &F, Triple &T, const std::string &ModuleId)
Options for the frontend instrumentation based profiling pass.
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:90
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:225
ModulePass * createSanitizerCoverageModulePass(const SanitizerCoverageOptions &Options=SanitizerCoverageOptions())
FunctionPass * createHWAddressSanitizerPass(bool CompileKernel=false, bool Recover=false)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ModulePass * createDataFlowSanitizerPass(const std::vector< std::string > &ABIListFiles=std::vector< std::string >(), void *(*getArgTLS)()=nullptr, void *(*getRetValTLS)()=nullptr)
FunctionPass * createAddressSanitizerFunctionPass(bool CompileKernel=false, bool Recover=false, bool UseAfterScope=false)
std::string Exclude
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
FunctionPass * createMemorySanitizerPass(int TrackOrigins=0, bool Recover=false, bool EnableKmsan=false)
ModulePass * createPGOIndirectCallPromotionLegacyPass(bool InLTO=false, bool SamplePGO=false)
ModulePass * createAddressSanitizerModulePass(bool CompileKernel=false, bool Recover=false, bool UseGlobalsGC=true)
The optimization diagnostic interface.