LLVM  9.0.0svn
Instrumentation.h
Go to the documentation of this file.
1 //===- Transforms/Instrumentation.h - Instrumentation passes ----*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines constructor functions for instrumentation passes.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_TRANSFORMS_INSTRUMENTATION_H
14 #define LLVM_TRANSFORMS_INSTRUMENTATION_H
15 
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/IR/BasicBlock.h"
18 #include <cassert>
19 #include <cstdint>
20 #include <limits>
21 #include <string>
22 #include <vector>
23 
24 namespace llvm {
25 
26 class Triple;
27 class FunctionPass;
28 class ModulePass;
29 class OptimizationRemarkEmitter;
30 class Comdat;
31 
32 /// Instrumentation passes often insert conditional checks into entry blocks.
33 /// Call this function before splitting the entry block to move instructions
34 /// that must remain in the entry block up before the split point. Static
35 /// allocas and llvm.localescape calls, for example, must remain in the entry
36 /// block.
39 
40 // Create a constant for Str so that we can pass it to the run-time lib.
41 GlobalVariable *createPrivateGlobalForString(Module &M, StringRef Str,
42  bool AllowMerging,
43  const char *NamePrefix = "");
44 
45 // Returns F.getComdat() if it exists.
46 // Otherwise creates a new comdat, sets F's comdat, and returns it.
47 // Returns nullptr on failure.
48 Comdat *GetOrCreateFunctionComdat(Function &F, Triple &T,
49  const std::string &ModuleId);
50 
51 // Insert GCOV profiling instrumentation
52 struct GCOVOptions {
53  static GCOVOptions getDefault();
54 
55  // Specify whether to emit .gcno files.
56  bool EmitNotes;
57 
58  // Specify whether to modify the program to emit .gcda files when run.
59  bool EmitData;
60 
61  // A four-byte version string. The meaning of a version string is described in
62  // gcc's gcov-io.h
63  char Version[4];
64 
65  // Emit a "cfg checksum" that follows the "line number checksum" of a
66  // function. This affects both .gcno and .gcda files.
68 
69  // Add the 'noredzone' attribute to added runtime library calls.
70  bool NoRedZone;
71 
72  // Emit the name of the function in the .gcda files. This is redundant, as
73  // the function identifier can be used to find the name from the .gcno file.
75 
76  // Emit the exit block immediately after the start block, rather than after
77  // all of the function body's blocks.
79 
80  // Regexes separated by a semi-colon to filter the files to instrument.
81  std::string Filter;
82 
83  // Regexes separated by a semi-colon to filter the files to not instrument.
84  std::string Exclude;
85 };
86 
89 
90 // PGO Instrumention
92 ModulePass *
95  bool SamplePGO = false);
97 
98 // The pgo-specific indirect call promotion function declared below is used by
99 // the pgo-driven indirect call promotion and sample profile passes. It's a
100 // wrapper around llvm::promoteCall, et al. that additionally computes !prof
101 // metadata. We place it in a pgo namespace so it's not confused with the
102 // generic utilities.
103 namespace pgo {
104 
105 // Helper function that transforms Inst (either an indirect-call instruction, or
106 // an invoke instruction , to a conditional call to F. This is like:
107 // if (Inst.CalledValue == F)
108 // F(...);
109 // else
110 // Inst(...);
111 // end
112 // TotalCount is the profile count value that the instruction executes.
113 // Count is the profile count value that F is the target function.
114 // These two values are used to update the branch weight.
115 // If \p AttachProfToDirectCall is true, a prof metadata is attached to the
116 // new direct call to contain \p Count.
117 // Returns the promoted direct call instruction.
118 Instruction *promoteIndirectCall(Instruction *Inst, Function *F, uint64_t Count,
119  uint64_t TotalCount,
120  bool AttachProfToDirectCall,
122 } // namespace pgo
123 
124 /// Options for the frontend instrumentation based profiling pass.
126  // Add the 'noredzone' attribute to added runtime library calls.
127  bool NoRedZone = false;
128 
129  // Do counter register promotion
130  bool DoCounterPromotion = false;
131 
132  // Use atomic profile counter increments.
133  bool Atomic = false;
134 
135  // Name of the profile file to use as output
136  std::string InstrProfileOutput;
137 
138  InstrProfOptions() = default;
139 };
140 
141 /// Insert frontend instrumentation based profiling.
143  const InstrProfOptions &Options = InstrProfOptions());
144 
145 FunctionPass *createHWAddressSanitizerPass(bool CompileKernel = false,
146  bool Recover = false);
147 
148 // Insert DataFlowSanitizer (dynamic data flow analysis) instrumentation
150  const std::vector<std::string> &ABIListFiles = std::vector<std::string>(),
151  void *(*getArgTLS)() = nullptr, void *(*getRetValTLS)() = nullptr);
152 
153 // Options for EfficiencySanitizer sub-tools.
155  enum Type {
156  ESAN_None = 0,
159  } ToolType = ESAN_None;
160 
161  EfficiencySanitizerOptions() = default;
162 };
163 
164 // Insert EfficiencySanitizer instrumentation.
167 
168 // Options for sanitizer coverage instrumentation.
170  enum Type {
171  SCK_None = 0,
174  SCK_Edge
175  } CoverageType = SCK_None;
176  bool IndirectCalls = false;
177  bool TraceBB = false;
178  bool TraceCmp = false;
179  bool TraceDiv = false;
180  bool TraceGep = false;
181  bool Use8bitCounters = false;
182  bool TracePC = false;
183  bool TracePCGuard = false;
184  bool Inline8bitCounters = false;
185  bool PCTable = false;
186  bool NoPrune = false;
187  bool StackDepth = false;
188 
189  SanitizerCoverageOptions() = default;
190 };
191 
192 // Insert SanitizerCoverage instrumentation.
195 
196 /// Calculate what to divide by to scale counts.
197 ///
198 /// Given the maximum count, calculate a divisor that will scale all the
199 /// weights to strictly less than std::numeric_limits<uint32_t>::max().
200 static inline uint64_t calculateCountScale(uint64_t MaxCount) {
202  ? 1
203  : MaxCount / std::numeric_limits<uint32_t>::max() + 1;
204 }
205 
206 /// Scale an individual branch count.
207 ///
208 /// Scale a 64-bit weight down to 32-bits using \c Scale.
209 ///
210 static inline uint32_t scaleBranchCount(uint64_t Count, uint64_t Scale) {
211  uint64_t Scaled = Count / Scale;
212  assert(Scaled <= std::numeric_limits<uint32_t>::max() && "overflow 32-bits");
213  return Scaled;
214 }
215 } // end namespace llvm
216 
217 #endif // LLVM_TRANSFORMS_INSTRUMENTATION_H
ModulePass * createPGOInstrumentationGenLegacyPass()
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Various leaf nodes.
Definition: ISDOpcodes.h:59
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 class - This class is used to implement most global optimizations.
Definition: Pass.h:284
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:89
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:224
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)
std::string Exclude
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
ModulePass * createPGOIndirectCallPromotionLegacyPass(bool InLTO=false, bool SamplePGO=false)
The optimization diagnostic interface.