LLVM  6.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 #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;
30 }
31 
32 inline void *getDFSanRetValTLSPtrForJIT() {
33  extern __thread __attribute__((tls_model("initial-exec")))
34  void *__dfsan_retval_tls;
35  return (void *)&__dfsan_retval_tls;
36 }
37 #endif
38 
39 namespace llvm {
40 
41 class FunctionPass;
42 class ModulePass;
43 
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
48 /// block.
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 
84 
85 // PGO Instrumention
87 ModulePass *
90  bool SamplePGO = false);
92 
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);
96 
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)
100 // F(...);
101 // else
102 // Inst(...);
103 // end
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,
111  uint64_t TotalCount,
112  bool AttachProfToDirectCall);
113 
114 /// Options for the frontend instrumentation based profiling pass.
116  // Add the 'noredzone' attribute to added runtime library calls.
117  bool NoRedZone = false;
118 
119  // Do counter register promotion
120  bool DoCounterPromotion = false;
121 
122  // Name of the profile file to use as output
123  std::string InstrProfileOutput;
124 
125  InstrProfOptions() = default;
126 };
127 
128 /// Insert frontend instrumentation based profiling.
130  const InstrProfOptions &Options = InstrProfOptions());
131 
132 // Insert AddressSanitizer (address sanity checking) instrumentation
133 FunctionPass *createAddressSanitizerFunctionPass(bool CompileKernel = false,
134  bool Recover = false,
135  bool UseAfterScope = false);
136 ModulePass *createAddressSanitizerModulePass(bool CompileKernel = false,
137  bool Recover = false,
138  bool UseGlobalsGC = true);
139 
140 // Insert MemorySanitizer instrumentation (detection of uninitialized reads)
141 FunctionPass *createMemorySanitizerPass(int TrackOrigins = 0,
142  bool Recover = false);
143 
144 // Insert ThreadSanitizer (race detection) instrumentation
146 
147 // Insert DataFlowSanitizer (dynamic data flow analysis) instrumentation
149  const std::vector<std::string> &ABIListFiles = std::vector<std::string>(),
150  void *(*getArgTLS)() = nullptr, void *(*getRetValTLS)() = nullptr);
151 
152 // Options for EfficiencySanitizer sub-tools.
154  enum Type {
155  ESAN_None = 0,
158  } ToolType = ESAN_None;
159 
160  EfficiencySanitizerOptions() = default;
161 };
162 
163 // Insert EfficiencySanitizer instrumentation.
166 
167 // Options for sanitizer coverage instrumentation.
169  enum Type {
170  SCK_None = 0,
173  SCK_Edge
174  } CoverageType = SCK_None;
175  bool IndirectCalls = false;
176  bool TraceBB = false;
177  bool TraceCmp = false;
178  bool TraceDiv = false;
179  bool TraceGep = false;
180  bool Use8bitCounters = false;
181  bool TracePC = false;
182  bool TracePCGuard = false;
183  bool Inline8bitCounters = false;
184  bool NoPrune = false;
185 
186  SanitizerCoverageOptions() = default;
187 };
188 
189 // Insert SanitizerCoverage instrumentation.
192 
193 #if defined(__GNUC__) && defined(__linux__) && !defined(ANDROID)
194 inline ModulePass *createDataFlowSanitizerPassForJIT(
195  const std::vector<std::string> &ABIListFiles = std::vector<std::string>()) {
196  return createDataFlowSanitizerPass(ABIListFiles, getDFSanArgTLSPtrForJIT,
197  getDFSanRetValTLSPtrForJIT);
198 }
199 #endif
200 
201 // BoundsChecking - This pass instruments the code to perform run-time bounds
202 // checking on loads, stores, and other memory intrinsics.
204 
205 /// \brief 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) {
211  ? 1
212  : MaxCount / std::numeric_limits<uint32_t>::max() + 1;
213 }
214 
215 /// \brief 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  uint64_t Scaled = Count / Scale;
221  assert(Scaled <= std::numeric_limits<uint32_t>::max() && "overflow 32-bits");
222  return Scaled;
223 }
224 
225 } // end namespace llvm
226 
227 #endif // LLVM_TRANSFORMS_INSTRUMENTATION_H
ModulePass * createPGOInstrumentationGenLegacyPass()
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
FunctionPass * createBoundsCheckingPass()
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
Various leaf nodes.
Definition: ISDOpcodes.h:60
FunctionPass * createPGOMemOPSizeOptLegacyPass()
static GCOVOptions getDefault()
ModulePass * createGCOVProfilerPass(const GCOVOptions &Options=GCOVOptions::getDefault())
ModulePass * createInstrProfilingLegacyPass(const InstrProfOptions &Options=InstrProfOptions())
Insert frontend instrumentation based profiling.
ModulePass * createEfficiencySanitizerPass(const EfficiencySanitizerOptions &Options=EfficiencySanitizerOptions())
__attribute__((weak)) int LLVMFuzzerInitialize(int *argc
#define F(x, y, z)
Definition: MD5.cpp:55
bool isLegalToPromote(Instruction *Inst, Function *F, const char **Reason)
ModulePass * createPGOInstrumentationUseLegacyPass(StringRef Filename=StringRef(""))
FunctionPass * createThreadSanitizerPass()
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
static uint64_t calculateCountScale(uint64_t MaxCount)
Calculate what to divide by to scale counts.
Instruction * promoteIndirectCall(Instruction *Inst, Function *F, uint64_t Count, uint64_t TotalCount, bool AttachProfToDirectCall)
static uint32_t scaleBranchCount(uint64_t Count, uint64_t Scale)
Scale an individual branch count.
std::string InstrProfileOutput
always inline
BasicBlock::iterator PrepareToSplitEntryBlock(BasicBlock &BB, BasicBlock::iterator IP)
Instrumentation passes often insert conditional checks into entry blocks.
Options for the frontend instrumentation based profiling pass.
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:91
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:235
ModulePass * createSanitizerCoverageModulePass(const SanitizerCoverageOptions &Options=SanitizerCoverageOptions())
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 * createMemorySanitizerPass(int TrackOrigins=0, bool Recover=false)
FunctionPass * createAddressSanitizerFunctionPass(bool CompileKernel=false, bool Recover=false, bool UseAfterScope=false)
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
ModulePass * createAddressSanitizerModulePass(bool CompileKernel=false, bool Recover=false, bool UseGlobalsGC=true)
ModulePass * createPGOIndirectCallPromotionLegacyPass(bool InLTO=false, bool SamplePGO=false)