LLVM  15.0.0git
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"
19 #include "llvm/IR/Function.h"
20 #include "llvm/IR/IRBuilder.h"
21 #include "llvm/IR/Instruction.h"
22 #include <cassert>
23 #include <cstdint>
24 #include <limits>
25 #include <string>
26 #include <vector>
27 
28 namespace llvm {
29 
30 class Triple;
31 class FunctionPass;
32 class ModulePass;
33 class OptimizationRemarkEmitter;
34 class Comdat;
35 class CallBase;
36 
37 /// Instrumentation passes often insert conditional checks into entry blocks.
38 /// Call this function before splitting the entry block to move instructions
39 /// that must remain in the entry block up before the split point. Static
40 /// allocas and llvm.localescape calls, for example, must remain in the entry
41 /// block.
44 
45 // Create a constant for Str so that we can pass it to the run-time lib.
46 GlobalVariable *createPrivateGlobalForString(Module &M, StringRef Str,
47  bool AllowMerging,
48  const char *NamePrefix = "");
49 
50 // Returns F.getComdat() if it exists.
51 // Otherwise creates a new comdat, sets F's comdat, and returns it.
52 // Returns nullptr on failure.
53 Comdat *getOrCreateFunctionComdat(Function &F, Triple &T);
54 
55 // Insert GCOV profiling instrumentation
56 struct GCOVOptions {
57  static GCOVOptions getDefault();
58 
59  // Specify whether to emit .gcno files.
60  bool EmitNotes;
61 
62  // Specify whether to modify the program to emit .gcda files when run.
63  bool EmitData;
64 
65  // A four-byte version string. The meaning of a version string is described in
66  // gcc's gcov-io.h
67  char Version[4];
68 
69  // Add the 'noredzone' attribute to added runtime library calls.
70  bool NoRedZone;
71 
72  // Use atomic profile counter increments.
73  bool Atomic = false;
74 
75  // Regexes separated by a semi-colon to filter the files to instrument.
76  std::string Filter;
77 
78  // Regexes separated by a semi-colon to filter the files to not instrument.
79  std::string Exclude;
80 };
81 
83 
84 // The pgo-specific indirect call promotion function declared below is used by
85 // the pgo-driven indirect call promotion and sample profile passes. It's a
86 // wrapper around llvm::promoteCall, et al. that additionally computes !prof
87 // metadata. We place it in a pgo namespace so it's not confused with the
88 // generic utilities.
89 namespace pgo {
90 
91 // Helper function that transforms CB (either an indirect-call instruction, or
92 // an invoke instruction , to a conditional call to F. This is like:
93 // if (Inst.CalledValue == F)
94 // F(...);
95 // else
96 // Inst(...);
97 // end
98 // TotalCount is the profile count value that the instruction executes.
99 // Count is the profile count value that F is the target function.
100 // These two values are used to update the branch weight.
101 // If \p AttachProfToDirectCall is true, a prof metadata is attached to the
102 // new direct call to contain \p Count.
103 // Returns the promoted direct call instruction.
105  uint64_t TotalCount, bool AttachProfToDirectCall,
107 } // namespace pgo
108 
109 /// Options for the frontend instrumentation based profiling pass.
111  // Add the 'noredzone' attribute to added runtime library calls.
112  bool NoRedZone = false;
113 
114  // Do counter register promotion
115  bool DoCounterPromotion = false;
116 
117  // Use atomic profile counter increments.
118  bool Atomic = false;
119 
120  // Use BFI to guide register promotion
121  bool UseBFIInPromotion = false;
122 
123  // Name of the profile file to use as output
124  std::string InstrProfileOutput;
125 
126  InstrProfOptions() = default;
127 };
128 
129 /// Insert frontend instrumentation based profiling. Parameter IsCS indicates if
130 // this is the context sensitive instrumentation.
132  const InstrProfOptions &Options = InstrProfOptions(), bool IsCS = false);
133 
135 
136 // Insert DataFlowSanitizer (dynamic data flow analysis) instrumentation
138  const std::vector<std::string> &ABIListFiles = std::vector<std::string>());
139 
140 // Options for sanitizer coverage instrumentation.
142  enum Type {
143  SCK_None = 0,
148  bool IndirectCalls = false;
149  bool TraceBB = false;
150  bool TraceCmp = false;
151  bool TraceDiv = false;
152  bool TraceGep = false;
153  bool Use8bitCounters = false;
154  bool TracePC = false;
155  bool TracePCGuard = false;
156  bool Inline8bitCounters = false;
157  bool InlineBoolFlag = false;
158  bool PCTable = false;
159  bool NoPrune = false;
160  bool StackDepth = false;
161  bool TraceLoads = false;
162  bool TraceStores = false;
163 
164  SanitizerCoverageOptions() = default;
165 };
166 
167 /// Calculate what to divide by to scale counts.
168 ///
169 /// Given the maximum count, calculate a divisor that will scale all the
170 /// weights to strictly less than std::numeric_limits<uint32_t>::max().
171 static inline uint64_t calculateCountScale(uint64_t MaxCount) {
173  ? 1
174  : MaxCount / std::numeric_limits<uint32_t>::max() + 1;
175 }
176 
177 /// Scale an individual branch count.
178 ///
179 /// Scale a 64-bit weight down to 32-bits using \c Scale.
180 ///
181 static inline uint32_t scaleBranchCount(uint64_t Count, uint64_t Scale) {
182  uint64_t Scaled = Count / Scale;
183  assert(Scaled <= std::numeric_limits<uint32_t>::max() && "overflow 32-bits");
184  return Scaled;
185 }
186 
187 // Use to ensure the inserted instrumentation has a DebugLocation; if none is
188 // attached to the source instruction, try to use a DILocation with offset 0
189 // scoped to surrounding function (if it has a DebugLocation).
190 //
191 // Some non-call instructions may be missing debug info, but when inserting
192 // instrumentation calls, some builds (e.g. LTO) want calls to have debug info
193 // if the enclosing function does.
195  static void ensureDebugInfo(IRBuilder<> &IRB, const Function &F) {
196  if (IRB.getCurrentDebugLocation())
197  return;
198  if (DISubprogram *SP = F.getSubprogram())
199  IRB.SetCurrentDebugLocation(DILocation::get(SP->getContext(), 0, 0, SP));
200  }
201 
203  ensureDebugInfo(*this, *IP->getFunction());
204  }
205 };
206 } // end namespace llvm
207 
208 #endif // LLVM_TRANSFORMS_INSTRUMENTATION_H
llvm::createInstrProfilingLegacyPass
ModulePass * createInstrProfilingLegacyPass(const InstrProfOptions &Options=InstrProfOptions(), bool IsCS=false)
Insert frontend instrumentation based profiling. Parameter IsCS indicates if.
Definition: InstrProfiling.cpp:452
llvm::SanitizerCoverageOptions::TraceCmp
bool TraceCmp
Definition: Instrumentation.h:150
llvm::createPrivateGlobalForString
GlobalVariable * createPrivateGlobalForString(Module &M, StringRef Str, bool AllowMerging, const char *NamePrefix="")
Definition: Instrumentation.cpp:60
llvm::SanitizerCoverageOptions::InlineBoolFlag
bool InlineBoolFlag
Definition: Instrumentation.h:157
llvm::PrepareToSplitEntryBlock
BasicBlock::iterator PrepareToSplitEntryBlock(BasicBlock &BB, BasicBlock::iterator IP)
Instrumentation passes often insert conditional checks into entry blocks.
Definition: Instrumentation.cpp:41
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::createDataFlowSanitizerLegacyPassPass
ModulePass * createDataFlowSanitizerLegacyPassPass(const std::vector< std::string > &ABIListFiles=std::vector< std::string >())
llvm::GCOVOptions::Version
char Version[4]
Definition: Instrumentation.h:67
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::InstrProfOptions::InstrProfOptions
InstrProfOptions()=default
llvm::BasicBlock::iterator
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:87
llvm::ModulePass
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:248
llvm::SanitizerCoverageOptions
Definition: Instrumentation.h:141
DebugInfoMetadata.h
llvm::InstrProfOptions::UseBFIInPromotion
bool UseBFIInPromotion
Definition: Instrumentation.h:121
llvm::Function
Definition: Function.h:60
StringRef.h
llvm::SanitizerCoverageOptions::Inline8bitCounters
bool Inline8bitCounters
Definition: Instrumentation.h:156
llvm::SanitizerCoverageOptions::TraceStores
bool TraceStores
Definition: Instrumentation.h:162
llvm::SanitizerCoverageOptions::TraceBB
bool TraceBB
Definition: Instrumentation.h:149
llvm::SanitizerCoverageOptions::SanitizerCoverageOptions
SanitizerCoverageOptions()=default
llvm::IRBuilder
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2495
llvm::SanitizerCoverageOptions::TraceGep
bool TraceGep
Definition: Instrumentation.h:152
llvm::InstrProfOptions::Atomic
bool Atomic
Definition: Instrumentation.h:118
T
#define T
Definition: Mips16ISelLowering.cpp:341
llvm::max
Expected< ExpressionValue > max(const ExpressionValue &Lhs, const ExpressionValue &Rhs)
Definition: FileCheck.cpp:337
llvm::createCGProfileLegacyPass
ModulePass * createCGProfileLegacyPass()
llvm::MDNode::get
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1300
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::SanitizerCoverageOptions::SCK_Edge
@ SCK_Edge
Definition: Instrumentation.h:146
Instruction.h
llvm::GCOVOptions::Exclude
std::string Exclude
Definition: Instrumentation.h:79
llvm::SanitizerCoverageOptions::CoverageType
enum llvm::SanitizerCoverageOptions::Type CoverageType
llvm::calculateCountScale
static uint64_t calculateCountScale(uint64_t MaxCount)
Calculate what to divide by to scale counts.
Definition: Instrumentation.h:171
llvm::SanitizerCoverageOptions::TraceLoads
bool TraceLoads
Definition: Instrumentation.h:161
IP
Definition: NVPTXLowerArgs.cpp:167
llvm::IRBuilderBase::SetCurrentDebugLocation
void SetCurrentDebugLocation(DebugLoc L)
Set location information used by debugging information.
Definition: IRBuilder.h:203
llvm::GCOVOptions::Filter
std::string Filter
Definition: Instrumentation.h:76
llvm::InstrumentationIRBuilder
Definition: Instrumentation.h:194
llvm::Instruction
Definition: Instruction.h:42
Options
const char LLVMTargetMachineRef LLVMPassBuilderOptionsRef Options
Definition: PassBuilderBindings.cpp:48
llvm::InstrProfOptions::DoCounterPromotion
bool DoCounterPromotion
Definition: Instrumentation.h:115
llvm::IRBuilderBase::getCurrentDebugLocation
DebugLoc getCurrentDebugLocation() const
Get location information used by debugging information.
Definition: IRBuilder.cpp:72
llvm::scaleBranchCount
static uint32_t scaleBranchCount(uint64_t Count, uint64_t Scale)
Scale an individual branch count.
Definition: Instrumentation.h:181
llvm::SanitizerCoverageOptions::SCK_BB
@ SCK_BB
Definition: Instrumentation.h:145
llvm::GCOVOptions::EmitData
bool EmitData
Definition: Instrumentation.h:63
BasicBlock.h
uint64_t
Scaled
@ Scaled
Definition: ARCInstrInfo.cpp:35
llvm::InstrumentationIRBuilder::InstrumentationIRBuilder
InstrumentationIRBuilder(Instruction *IP)
Definition: Instrumentation.h:202
llvm::InstrProfOptions::InstrProfileOutput
std::string InstrProfileOutput
Definition: Instrumentation.h:124
llvm::SanitizerCoverageOptions::NoPrune
bool NoPrune
Definition: Instrumentation.h:159
llvm::InstrProfOptions
Options for the frontend instrumentation based profiling pass.
Definition: Instrumentation.h:110
IRBuilder.h
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::SanitizerCoverageOptions::Type
Type
Definition: Instrumentation.h:142
llvm::SanitizerCoverageOptions::Use8bitCounters
bool Use8bitCounters
Definition: Instrumentation.h:153
llvm::ISD::BasicBlock
@ BasicBlock
Various leaf nodes.
Definition: ISDOpcodes.h:71
llvm::GCOVOptions::getDefault
static GCOVOptions getDefault()
Definition: GCOVProfiling.cpp:75
llvm::SanitizerCoverageOptions::TraceDiv
bool TraceDiv
Definition: Instrumentation.h:151
llvm::OptimizationRemarkEmitter
The optimization diagnostic interface.
Definition: OptimizationRemarkEmitter.h:33
llvm::SanitizerCoverageOptions::SCK_Function
@ SCK_Function
Definition: Instrumentation.h:144
uint32_t
Module
Machine Check Debug Module
Definition: MachineCheckDebugify.cpp:122
llvm::SanitizerCoverageOptions::StackDepth
bool StackDepth
Definition: Instrumentation.h:160
llvm::getOrCreateFunctionComdat
Comdat * getOrCreateFunctionComdat(Function &F, Triple &T)
Definition: Instrumentation.cpp:76
Function.h
llvm::SanitizerCoverageOptions::TracePC
bool TracePC
Definition: Instrumentation.h:154
llvm::SanitizerCoverageOptions::SCK_None
@ SCK_None
Definition: Instrumentation.h:143
llvm::pgo::promoteIndirectCall
CallBase & promoteIndirectCall(CallBase &CB, Function *F, uint64_t Count, uint64_t TotalCount, bool AttachProfToDirectCall, OptimizationRemarkEmitter *ORE)
Definition: IndirectCallPromotion.cpp:244
llvm::InstrumentationIRBuilder::ensureDebugInfo
static void ensureDebugInfo(IRBuilder<> &IRB, const Function &F)
Definition: Instrumentation.h:195
llvm::SanitizerCoverageOptions::PCTable
bool PCTable
Definition: Instrumentation.h:158
llvm::InstrProfOptions::NoRedZone
bool NoRedZone
Definition: Instrumentation.h:112
llvm::GCOVOptions
Definition: Instrumentation.h:56
llvm::createInstrOrderFilePass
ModulePass * createInstrOrderFilePass()
Definition: InstrOrderFile.cpp:202
llvm::DISubprogram
Subprogram description.
Definition: DebugInfoMetadata.h:1803
llvm::CallBase
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1174
llvm::GCOVOptions::NoRedZone
bool NoRedZone
Definition: Instrumentation.h:70
BB
Common register allocation spilling lr str ldr sxth r3 ldr mla r4 can lr mov lr str ldr sxth r3 mla r4 and then merge mul and lr str ldr sxth r3 mla r4 It also increase the likelihood the store may become dead bb27 Successors according to LLVM BB
Definition: README.txt:39
llvm::SanitizerCoverageOptions::TracePCGuard
bool TracePCGuard
Definition: Instrumentation.h:155
llvm::SanitizerCoverageOptions::IndirectCalls
bool IndirectCalls
Definition: Instrumentation.h:148
llvm::GCOVOptions::Atomic
bool Atomic
Definition: Instrumentation.h:73
llvm::GCOVOptions::EmitNotes
bool EmitNotes
Definition: Instrumentation.h:60