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 namespace llvm {
26 
27 class FunctionPass;
28 class ModulePass;
29 class OptimizationRemarkEmitter;
30 
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
35 /// block.
38 
39 // Insert GCOV profiling instrumentation
40 struct GCOVOptions {
41  static GCOVOptions getDefault();
42 
43  // Specify whether to emit .gcno files.
44  bool EmitNotes;
45 
46  // Specify whether to modify the program to emit .gcda files when run.
47  bool EmitData;
48 
49  // A four-byte version string. The meaning of a version string is described in
50  // gcc's gcov-io.h
51  char Version[4];
52 
53  // Emit a "cfg checksum" that follows the "line number checksum" of a
54  // function. This affects both .gcno and .gcda files.
56 
57  // Add the 'noredzone' attribute to added runtime library calls.
58  bool NoRedZone;
59 
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.
63 
64  // Emit the exit block immediately after the start block, rather than after
65  // all of the function body's blocks.
67 };
68 
71 
72 // PGO Instrumention
74 ModulePass *
77  bool SamplePGO = false);
79 
80 // Helper function to check if it is legal to promote indirect call \p Inst
81 // to a direct call of function \p F. Stores the reason in \p Reason.
82 bool isLegalToPromote(Instruction *Inst, Function *F, const char **Reason);
83 
84 // Helper function that transforms Inst (either an indirect-call instruction, or
85 // an invoke instruction , to a conditional call to F. This is like:
86 // if (Inst.CalledValue == F)
87 // F(...);
88 // else
89 // Inst(...);
90 // end
91 // TotalCount is the profile count value that the instruction executes.
92 // Count is the profile count value that F is the target function.
93 // These two values are used to update the branch weight.
94 // If \p AttachProfToDirectCall is true, a prof metadata is attached to the
95 // new direct call to contain \p Count.
96 // Returns the promoted direct call instruction.
97 Instruction *promoteIndirectCall(Instruction *Inst, Function *F, uint64_t Count,
98  uint64_t TotalCount,
99  bool AttachProfToDirectCall,
101 
102 /// Options for the frontend instrumentation based profiling pass.
104  // Add the 'noredzone' attribute to added runtime library calls.
105  bool NoRedZone = false;
106 
107  // Do counter register promotion
108  bool DoCounterPromotion = false;
109 
110  // Name of the profile file to use as output
111  std::string InstrProfileOutput;
112 
113  InstrProfOptions() = default;
114 };
115 
116 /// Insert frontend instrumentation based profiling.
118  const InstrProfOptions &Options = InstrProfOptions());
119 
120 // Insert AddressSanitizer (address sanity checking) instrumentation
121 FunctionPass *createAddressSanitizerFunctionPass(bool CompileKernel = false,
122  bool Recover = false,
123  bool UseAfterScope = false);
124 ModulePass *createAddressSanitizerModulePass(bool CompileKernel = false,
125  bool Recover = false,
126  bool UseGlobalsGC = true);
127 
128 // Insert MemorySanitizer instrumentation (detection of uninitialized reads)
129 FunctionPass *createMemorySanitizerPass(int TrackOrigins = 0,
130  bool Recover = false);
131 
132 // Insert ThreadSanitizer (race detection) instrumentation
134 
135 // Insert DataFlowSanitizer (dynamic data flow analysis) instrumentation
137  const std::vector<std::string> &ABIListFiles = std::vector<std::string>(),
138  void *(*getArgTLS)() = nullptr, void *(*getRetValTLS)() = nullptr);
139 
140 // Options for EfficiencySanitizer sub-tools.
142  enum Type {
143  ESAN_None = 0,
146  } ToolType = ESAN_None;
147 
148  EfficiencySanitizerOptions() = default;
149 };
150 
151 // Insert EfficiencySanitizer instrumentation.
154 
155 // Options for sanitizer coverage instrumentation.
157  enum Type {
158  SCK_None = 0,
161  SCK_Edge
162  } CoverageType = SCK_None;
163  bool IndirectCalls = false;
164  bool TraceBB = false;
165  bool TraceCmp = false;
166  bool TraceDiv = false;
167  bool TraceGep = false;
168  bool Use8bitCounters = false;
169  bool TracePC = false;
170  bool TracePCGuard = false;
171  bool Inline8bitCounters = false;
172  bool PCTable = false;
173  bool NoPrune = false;
174  bool StackDepth = false;
175 
176  SanitizerCoverageOptions() = default;
177 };
178 
179 // Insert SanitizerCoverage instrumentation.
182 
183 /// \brief Calculate what to divide by to scale counts.
184 ///
185 /// Given the maximum count, calculate a divisor that will scale all the
186 /// weights to strictly less than std::numeric_limits<uint32_t>::max().
187 static inline uint64_t calculateCountScale(uint64_t MaxCount) {
189  ? 1
190  : MaxCount / std::numeric_limits<uint32_t>::max() + 1;
191 }
192 
193 /// \brief Scale an individual branch count.
194 ///
195 /// Scale a 64-bit weight down to 32-bits using \c Scale.
196 ///
197 static inline uint32_t scaleBranchCount(uint64_t Count, uint64_t Scale) {
198  uint64_t Scaled = Count / Scale;
199  assert(Scaled <= std::numeric_limits<uint32_t>::max() && "overflow 32-bits");
200  return Scaled;
201 }
202 
203 } // end namespace llvm
204 
205 #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
FunctionPass * createPGOMemOPSizeOptLegacyPass()
static GCOVOptions getDefault()
ModulePass * createGCOVProfilerPass(const GCOVOptions &Options=GCOVOptions::getDefault())
F(f)
ModulePass * createInstrProfilingLegacyPass(const InstrProfOptions &Options=InstrProfOptions())
Insert frontend instrumentation based profiling.
ModulePass * createEfficiencySanitizerPass(const EfficiencySanitizerOptions &Options=EfficiencySanitizerOptions())
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: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
BasicBlock::iterator PrepareToSplitEntryBlock(BasicBlock &BB, BasicBlock::iterator IP)
Instrumentation passes often insert conditional checks into entry blocks.
Instruction * promoteIndirectCall(Instruction *Inst, Function *F, uint64_t Count, uint64_t TotalCount, bool AttachProfToDirectCall, OptimizationRemarkEmitter *ORE)
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:225
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 * createPGOIndirectCallPromotionLegacyPass(bool InLTO=false, bool SamplePGO=false)
ModulePass * createAddressSanitizerModulePass(bool CompileKernel=false, bool Recover=false, bool UseGlobalsGC=true)
The optimization diagnostic interface.