LLVM  16.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 ModulePass;
32 class OptimizationRemarkEmitter;
33 class Comdat;
34 class CallBase;
35 
36 /// Instrumentation passes often insert conditional checks into entry blocks.
37 /// Call this function before splitting the entry block to move instructions
38 /// that must remain in the entry block up before the split point. Static
39 /// allocas and llvm.localescape calls, for example, must remain in the entry
40 /// block.
43 
44 // Create a constant for Str so that we can pass it to the run-time lib.
45 GlobalVariable *createPrivateGlobalForString(Module &M, StringRef Str,
46  bool AllowMerging,
47  const char *NamePrefix = "");
48 
49 // Returns F.getComdat() if it exists.
50 // Otherwise creates a new comdat, sets F's comdat, and returns it.
51 // Returns nullptr on failure.
52 Comdat *getOrCreateFunctionComdat(Function &F, Triple &T);
53 
54 // Insert GCOV profiling instrumentation
55 struct GCOVOptions {
56  static GCOVOptions getDefault();
57 
58  // Specify whether to emit .gcno files.
59  bool EmitNotes;
60 
61  // Specify whether to modify the program to emit .gcda files when run.
62  bool EmitData;
63 
64  // A four-byte version string. The meaning of a version string is described in
65  // gcc's gcov-io.h
66  char Version[4];
67 
68  // Add the 'noredzone' attribute to added runtime library calls.
69  bool NoRedZone;
70 
71  // Use atomic profile counter increments.
72  bool Atomic = false;
73 
74  // Regexes separated by a semi-colon to filter the files to instrument.
75  std::string Filter;
76 
77  // Regexes separated by a semi-colon to filter the files to not instrument.
78  std::string Exclude;
79 };
80 
81 // The pgo-specific indirect call promotion function declared below is used by
82 // the pgo-driven indirect call promotion and sample profile passes. It's a
83 // wrapper around llvm::promoteCall, et al. that additionally computes !prof
84 // metadata. We place it in a pgo namespace so it's not confused with the
85 // generic utilities.
86 namespace pgo {
87 
88 // Helper function that transforms CB (either an indirect-call instruction, or
89 // an invoke instruction , to a conditional call to F. This is like:
90 // if (Inst.CalledValue == F)
91 // F(...);
92 // else
93 // Inst(...);
94 // end
95 // TotalCount is the profile count value that the instruction executes.
96 // Count is the profile count value that F is the target function.
97 // These two values are used to update the branch weight.
98 // If \p AttachProfToDirectCall is true, a prof metadata is attached to the
99 // new direct call to contain \p Count.
100 // Returns the promoted direct call instruction.
102  uint64_t TotalCount, bool AttachProfToDirectCall,
104 } // namespace pgo
105 
106 /// Options for the frontend instrumentation based profiling pass.
108  // Add the 'noredzone' attribute to added runtime library calls.
109  bool NoRedZone = false;
110 
111  // Do counter register promotion
112  bool DoCounterPromotion = false;
113 
114  // Use atomic profile counter increments.
115  bool Atomic = false;
116 
117  // Use BFI to guide register promotion
118  bool UseBFIInPromotion = false;
119 
120  // Name of the profile file to use as output
121  std::string InstrProfileOutput;
122 
123  InstrProfOptions() = default;
124 };
125 
126 // Insert DataFlowSanitizer (dynamic data flow analysis) instrumentation
128  const std::vector<std::string> &ABIListFiles = std::vector<std::string>());
129 
130 // Options for sanitizer coverage instrumentation.
132  enum Type {
133  SCK_None = 0,
138  bool IndirectCalls = false;
139  bool TraceBB = false;
140  bool TraceCmp = false;
141  bool TraceDiv = false;
142  bool TraceGep = false;
143  bool Use8bitCounters = false;
144  bool TracePC = false;
145  bool TracePCGuard = false;
146  bool Inline8bitCounters = false;
147  bool InlineBoolFlag = false;
148  bool PCTable = false;
149  bool NoPrune = false;
150  bool StackDepth = false;
151  bool TraceLoads = false;
152  bool TraceStores = false;
153  bool CollectControlFlow = false;
154 
155  SanitizerCoverageOptions() = default;
156 };
157 
158 /// Options for SanitizerBinaryMetadata.
160  bool Covered = false;
161  bool Atomics = false;
162  SanitizerBinaryMetadataOptions() = default;
163 };
164 
165 /// Calculate what to divide by to scale counts.
166 ///
167 /// Given the maximum count, calculate a divisor that will scale all the
168 /// weights to strictly less than std::numeric_limits<uint32_t>::max().
169 static inline uint64_t calculateCountScale(uint64_t MaxCount) {
171  ? 1
172  : MaxCount / std::numeric_limits<uint32_t>::max() + 1;
173 }
174 
175 /// Scale an individual branch count.
176 ///
177 /// Scale a 64-bit weight down to 32-bits using \c Scale.
178 ///
179 static inline uint32_t scaleBranchCount(uint64_t Count, uint64_t Scale) {
180  uint64_t Scaled = Count / Scale;
181  assert(Scaled <= std::numeric_limits<uint32_t>::max() && "overflow 32-bits");
182  return Scaled;
183 }
184 
185 // Use to ensure the inserted instrumentation has a DebugLocation; if none is
186 // attached to the source instruction, try to use a DILocation with offset 0
187 // scoped to surrounding function (if it has a DebugLocation).
188 //
189 // Some non-call instructions may be missing debug info, but when inserting
190 // instrumentation calls, some builds (e.g. LTO) want calls to have debug info
191 // if the enclosing function does.
193  static void ensureDebugInfo(IRBuilder<> &IRB, const Function &F) {
194  if (IRB.getCurrentDebugLocation())
195  return;
196  if (DISubprogram *SP = F.getSubprogram())
197  IRB.SetCurrentDebugLocation(DILocation::get(SP->getContext(), 0, 0, SP));
198  }
199 
201  ensureDebugInfo(*this, *IP->getFunction());
202  }
203 };
204 } // end namespace llvm
205 
206 #endif // LLVM_TRANSFORMS_INSTRUMENTATION_H
llvm::SanitizerCoverageOptions::TraceCmp
bool TraceCmp
Definition: Instrumentation.h:140
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:147
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:18
llvm::GCOVOptions::Version
char Version[4]
Definition: Instrumentation.h:66
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:131
DebugInfoMetadata.h
llvm::InstrProfOptions::UseBFIInPromotion
bool UseBFIInPromotion
Definition: Instrumentation.h:118
llvm::Function
Definition: Function.h:60
StringRef.h
llvm::SanitizerCoverageOptions::Inline8bitCounters
bool Inline8bitCounters
Definition: Instrumentation.h:146
llvm::SanitizerCoverageOptions::TraceStores
bool TraceStores
Definition: Instrumentation.h:152
llvm::SanitizerCoverageOptions::TraceBB
bool TraceBB
Definition: Instrumentation.h:139
llvm::SanitizerBinaryMetadataOptions::Atomics
bool Atomics
Definition: Instrumentation.h:161
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:2524
llvm::SanitizerCoverageOptions::TraceGep
bool TraceGep
Definition: Instrumentation.h:142
llvm::InstrProfOptions::Atomic
bool Atomic
Definition: Instrumentation.h:115
T
#define T
Definition: Mips16ISelLowering.cpp:341
llvm::max
Expected< ExpressionValue > max(const ExpressionValue &Lhs, const ExpressionValue &Rhs)
Definition: FileCheck.cpp:337
llvm::MDNode::get
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1400
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::SanitizerCoverageOptions::SCK_Edge
@ SCK_Edge
Definition: Instrumentation.h:136
Instruction.h
llvm::GCOVOptions::Exclude
std::string Exclude
Definition: Instrumentation.h:78
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:169
llvm::SanitizerCoverageOptions::TraceLoads
bool TraceLoads
Definition: Instrumentation.h:151
IP
Definition: NVPTXLowerArgs.cpp:168
llvm::IRBuilderBase::SetCurrentDebugLocation
void SetCurrentDebugLocation(DebugLoc L)
Set location information used by debugging information.
Definition: IRBuilder.h:211
llvm::GCOVOptions::Filter
std::string Filter
Definition: Instrumentation.h:75
llvm::InstrumentationIRBuilder
Definition: Instrumentation.h:192
llvm::Instruction
Definition: Instruction.h:42
llvm::SanitizerCoverageOptions::CollectControlFlow
bool CollectControlFlow
Definition: Instrumentation.h:153
llvm::InstrProfOptions::DoCounterPromotion
bool DoCounterPromotion
Definition: Instrumentation.h:112
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:179
llvm::SanitizerCoverageOptions::SCK_BB
@ SCK_BB
Definition: Instrumentation.h:135
llvm::GCOVOptions::EmitData
bool EmitData
Definition: Instrumentation.h:62
BasicBlock.h
uint64_t
Scaled
@ Scaled
Definition: ARCInstrInfo.cpp:35
llvm::InstrumentationIRBuilder::InstrumentationIRBuilder
InstrumentationIRBuilder(Instruction *IP)
Definition: Instrumentation.h:200
llvm::SanitizerBinaryMetadataOptions::Covered
bool Covered
Definition: Instrumentation.h:160
llvm::InstrProfOptions::InstrProfileOutput
std::string InstrProfileOutput
Definition: Instrumentation.h:121
llvm::SanitizerCoverageOptions::NoPrune
bool NoPrune
Definition: Instrumentation.h:149
llvm::InstrProfOptions
Options for the frontend instrumentation based profiling pass.
Definition: Instrumentation.h:107
IRBuilder.h
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::SanitizerCoverageOptions::Type
Type
Definition: Instrumentation.h:132
llvm::SanitizerCoverageOptions::Use8bitCounters
bool Use8bitCounters
Definition: Instrumentation.h:143
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:141
llvm::OptimizationRemarkEmitter
The optimization diagnostic interface.
Definition: OptimizationRemarkEmitter.h:33
llvm::SanitizerCoverageOptions::SCK_Function
@ SCK_Function
Definition: Instrumentation.h:134
uint32_t
Module
Machine Check Debug Module
Definition: MachineCheckDebugify.cpp:122
llvm::SanitizerCoverageOptions::StackDepth
bool StackDepth
Definition: Instrumentation.h:150
llvm::SanitizerBinaryMetadataOptions
Options for SanitizerBinaryMetadata.
Definition: Instrumentation.h:159
llvm::getOrCreateFunctionComdat
Comdat * getOrCreateFunctionComdat(Function &F, Triple &T)
Definition: Instrumentation.cpp:76
Function.h
llvm::SanitizerCoverageOptions::TracePC
bool TracePC
Definition: Instrumentation.h:144
llvm::SanitizerCoverageOptions::SCK_None
@ SCK_None
Definition: Instrumentation.h:133
llvm::pgo::promoteIndirectCall
CallBase & promoteIndirectCall(CallBase &CB, Function *F, uint64_t Count, uint64_t TotalCount, bool AttachProfToDirectCall, OptimizationRemarkEmitter *ORE)
Definition: IndirectCallPromotion.cpp:244
llvm::SanitizerBinaryMetadataOptions::SanitizerBinaryMetadataOptions
SanitizerBinaryMetadataOptions()=default
llvm::InstrumentationIRBuilder::ensureDebugInfo
static void ensureDebugInfo(IRBuilder<> &IRB, const Function &F)
Definition: Instrumentation.h:193
llvm::SanitizerCoverageOptions::PCTable
bool PCTable
Definition: Instrumentation.h:148
llvm::InstrProfOptions::NoRedZone
bool NoRedZone
Definition: Instrumentation.h:109
llvm::GCOVOptions
Definition: Instrumentation.h:55
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:69
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:145
llvm::SanitizerCoverageOptions::IndirectCalls
bool IndirectCalls
Definition: Instrumentation.h:138
llvm::GCOVOptions::Atomic
bool Atomic
Definition: Instrumentation.h:72
llvm::GCOVOptions::EmitNotes
bool EmitNotes
Definition: Instrumentation.h:59
llvm::createDataFlowSanitizerLegacyPassPass
ModulePass * createDataFlowSanitizerLegacyPassPass(const std::vector< std::string > &ABIListFiles=std::vector< std::string >())
Definition: DataFlowSanitizer.cpp:3367