LLVM  14.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"
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 class CallBase;
32 
33 /// Instrumentation passes often insert conditional checks into entry blocks.
34 /// Call this function before splitting the entry block to move instructions
35 /// that must remain in the entry block up before the split point. Static
36 /// allocas and llvm.localescape calls, for example, must remain in the entry
37 /// block.
40 
41 // Create a constant for Str so that we can pass it to the run-time lib.
42 GlobalVariable *createPrivateGlobalForString(Module &M, StringRef Str,
43  bool AllowMerging,
44  const char *NamePrefix = "");
45 
46 // Returns F.getComdat() if it exists.
47 // Otherwise creates a new comdat, sets F's comdat, and returns it.
48 // Returns nullptr on failure.
49 Comdat *getOrCreateFunctionComdat(Function &F, Triple &T);
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  // Add the 'noredzone' attribute to added runtime library calls.
66  bool NoRedZone;
67 
68  // Use atomic profile counter increments.
69  bool Atomic = false;
70 
71  // Regexes separated by a semi-colon to filter the files to instrument.
72  std::string Filter;
73 
74  // Regexes separated by a semi-colon to filter the files to not instrument.
75  std::string Exclude;
76 };
77 
80 
81 // PGO Instrumention. Parameter IsCS indicates if this is the context senstive
82 // instrumentation.
84 ModulePass *
86  bool IsCS = false);
88  StringRef CSInstrName = StringRef(""));
90  bool SamplePGO = false);
92 
94 
95 // The pgo-specific indirect call promotion function declared below is used by
96 // the pgo-driven indirect call promotion and sample profile passes. It's a
97 // wrapper around llvm::promoteCall, et al. that additionally computes !prof
98 // metadata. We place it in a pgo namespace so it's not confused with the
99 // generic utilities.
100 namespace pgo {
101 
102 // Helper function that transforms CB (either an indirect-call instruction, or
103 // an invoke instruction , to a conditional call to F. This is like:
104 // if (Inst.CalledValue == F)
105 // F(...);
106 // else
107 // Inst(...);
108 // end
109 // TotalCount is the profile count value that the instruction executes.
110 // Count is the profile count value that F is the target function.
111 // These two values are used to update the branch weight.
112 // If \p AttachProfToDirectCall is true, a prof metadata is attached to the
113 // new direct call to contain \p Count.
114 // Returns the promoted direct call instruction.
115 CallBase &promoteIndirectCall(CallBase &CB, Function *F, uint64_t Count,
116  uint64_t TotalCount, bool AttachProfToDirectCall,
118 } // namespace pgo
119 
120 /// Options for the frontend instrumentation based profiling pass.
122  // Add the 'noredzone' attribute to added runtime library calls.
123  bool NoRedZone = false;
124 
125  // Do counter register promotion
126  bool DoCounterPromotion = false;
127 
128  // Use atomic profile counter increments.
129  bool Atomic = false;
130 
131  // Use BFI to guide register promotion
132  bool UseBFIInPromotion = false;
133 
134  // Name of the profile file to use as output
135  std::string InstrProfileOutput;
136 
137  InstrProfOptions() = default;
138 };
139 
140 /// Insert frontend instrumentation based profiling. Parameter IsCS indicates if
141 // this is the context senstive instrumentation.
143  const InstrProfOptions &Options = InstrProfOptions(), bool IsCS = false);
144 
146 
147 // Insert DataFlowSanitizer (dynamic data flow analysis) instrumentation
149  const std::vector<std::string> &ABIListFiles = std::vector<std::string>());
150 
151 // Options for sanitizer coverage instrumentation.
153  enum Type {
154  SCK_None = 0,
159  bool IndirectCalls = false;
160  bool TraceBB = false;
161  bool TraceCmp = false;
162  bool TraceDiv = false;
163  bool TraceGep = false;
164  bool Use8bitCounters = false;
165  bool TracePC = false;
166  bool TracePCGuard = false;
167  bool Inline8bitCounters = false;
168  bool InlineBoolFlag = false;
169  bool PCTable = false;
170  bool NoPrune = false;
171  bool StackDepth = false;
172 
173  SanitizerCoverageOptions() = default;
174 };
175 
176 /// Calculate what to divide by to scale counts.
177 ///
178 /// Given the maximum count, calculate a divisor that will scale all the
179 /// weights to strictly less than std::numeric_limits<uint32_t>::max().
180 static inline uint64_t calculateCountScale(uint64_t MaxCount) {
182  ? 1
183  : MaxCount / std::numeric_limits<uint32_t>::max() + 1;
184 }
185 
186 /// Scale an individual branch count.
187 ///
188 /// Scale a 64-bit weight down to 32-bits using \c Scale.
189 ///
190 static inline uint32_t scaleBranchCount(uint64_t Count, uint64_t Scale) {
191  uint64_t Scaled = Count / Scale;
192  assert(Scaled <= std::numeric_limits<uint32_t>::max() && "overflow 32-bits");
193  return Scaled;
194 }
195 } // end namespace llvm
196 
197 #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:433
llvm::SanitizerCoverageOptions::TraceCmp
bool TraceCmp
Definition: Instrumentation.h:161
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:168
llvm::PrepareToSplitEntryBlock
BasicBlock::iterator PrepareToSplitEntryBlock(BasicBlock &BB, BasicBlock::iterator IP)
Instrumentation passes often insert conditional checks into entry blocks.
Definition: Instrumentation.cpp:41
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::createDataFlowSanitizerLegacyPassPass
ModulePass * createDataFlowSanitizerLegacyPassPass(const std::vector< std::string > &ABIListFiles=std::vector< std::string >())
llvm::GCOVOptions::Version
char Version[4]
Definition: Instrumentation.h:63
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:90
llvm::ModulePass
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:238
llvm::SanitizerCoverageOptions
Definition: Instrumentation.h:152
llvm::InstrProfOptions::UseBFIInPromotion
bool UseBFIInPromotion
Definition: Instrumentation.h:132
llvm::Function
Definition: Function.h:61
StringRef.h
llvm::SanitizerCoverageOptions::Inline8bitCounters
bool Inline8bitCounters
Definition: Instrumentation.h:167
llvm::SanitizerCoverageOptions::TraceBB
bool TraceBB
Definition: Instrumentation.h:160
llvm::SanitizerCoverageOptions::SanitizerCoverageOptions
SanitizerCoverageOptions()=default
llvm::createPGOInstrumentationUseLegacyPass
ModulePass * createPGOInstrumentationUseLegacyPass(StringRef Filename=StringRef(""), bool IsCS=false)
Definition: PGOInstrumentation.cpp:497
llvm::SanitizerCoverageOptions::TraceGep
bool TraceGep
Definition: Instrumentation.h:163
llvm::InstrProfOptions::Atomic
bool Atomic
Definition: Instrumentation.h:129
llvm::createPGOIndirectCallPromotionLegacyPass
ModulePass * createPGOIndirectCallPromotionLegacyPass(bool InLTO=false, bool SamplePGO=false)
Definition: IndirectCallPromotion.cpp:160
T
#define T
Definition: Mips16ISelLowering.cpp:341
llvm::createCGProfileLegacyPass
ModulePass * createCGProfileLegacyPass()
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::SanitizerCoverageOptions::SCK_Edge
@ SCK_Edge
Definition: Instrumentation.h:157
llvm::GCOVOptions::Exclude
std::string Exclude
Definition: Instrumentation.h:75
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:180
llvm::createPGOInstrumentationGenCreateVarLegacyPass
ModulePass * createPGOInstrumentationGenCreateVarLegacyPass(StringRef CSInstrName=StringRef(""))
IP
Definition: NVPTXLowerArgs.cpp:166
llvm::GCOVOptions::Filter
std::string Filter
Definition: Instrumentation.h:72
Options
const char LLVMTargetMachineRef LLVMPassBuilderOptionsRef Options
Definition: PassBuilderBindings.cpp:48
llvm::createGCOVProfilerPass
ModulePass * createGCOVProfilerPass(const GCOVOptions &Options=GCOVOptions::getDefault())
Definition: GCOVProfiling.cpp:251
llvm::createPGOMemOPSizeOptLegacyPass
FunctionPass * createPGOMemOPSizeOptLegacyPass()
llvm::InstrProfOptions::DoCounterPromotion
bool DoCounterPromotion
Definition: Instrumentation.h:126
llvm::scaleBranchCount
static uint32_t scaleBranchCount(uint64_t Count, uint64_t Scale)
Scale an individual branch count.
Definition: Instrumentation.h:190
llvm::SanitizerCoverageOptions::SCK_BB
@ SCK_BB
Definition: Instrumentation.h:156
llvm::GCOVOptions::EmitData
bool EmitData
Definition: Instrumentation.h:59
BasicBlock.h
Scaled
@ Scaled
Definition: ARCInstrInfo.cpp:35
llvm::InstrProfOptions::InstrProfileOutput
std::string InstrProfileOutput
Definition: Instrumentation.h:135
llvm::SanitizerCoverageOptions::NoPrune
bool NoPrune
Definition: Instrumentation.h:170
llvm::createPGOInstrumentationGenLegacyPass
ModulePass * createPGOInstrumentationGenLegacyPass(bool IsCS=false)
Definition: PGOInstrumentation.cpp:483
llvm::InstrProfOptions
Options for the frontend instrumentation based profiling pass.
Definition: Instrumentation.h:121
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::SanitizerCoverageOptions::Type
Type
Definition: Instrumentation.h:153
llvm::SanitizerCoverageOptions::Use8bitCounters
bool Use8bitCounters
Definition: Instrumentation.h:164
llvm::ISD::BasicBlock
@ BasicBlock
Various leaf nodes.
Definition: ISDOpcodes.h:71
llvm::GCOVOptions::getDefault
static GCOVOptions getDefault()
Definition: GCOVProfiling.cpp:81
llvm::SanitizerCoverageOptions::TraceDiv
bool TraceDiv
Definition: Instrumentation.h:162
llvm::OptimizationRemarkEmitter
The optimization diagnostic interface.
Definition: OptimizationRemarkEmitter.h:33
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::SanitizerCoverageOptions::SCK_Function
@ SCK_Function
Definition: Instrumentation.h:155
uint32_t
Module
Machine Check Debug Module
Definition: MachineCheckDebugify.cpp:122
llvm::SanitizerCoverageOptions::StackDepth
bool StackDepth
Definition: Instrumentation.h:171
llvm::getOrCreateFunctionComdat
Comdat * getOrCreateFunctionComdat(Function &F, Triple &T)
Definition: Instrumentation.cpp:76
llvm::SanitizerCoverageOptions::TracePC
bool TracePC
Definition: Instrumentation.h:165
llvm::SanitizerCoverageOptions::SCK_None
@ SCK_None
Definition: Instrumentation.h:154
llvm::pgo::promoteIndirectCall
CallBase & promoteIndirectCall(CallBase &CB, Function *F, uint64_t Count, uint64_t TotalCount, bool AttachProfToDirectCall, OptimizationRemarkEmitter *ORE)
Definition: IndirectCallPromotion.cpp:304
llvm::SanitizerCoverageOptions::PCTable
bool PCTable
Definition: Instrumentation.h:169
llvm::InstrProfOptions::NoRedZone
bool NoRedZone
Definition: Instrumentation.h:123
llvm::GCOVOptions
Definition: Instrumentation.h:52
llvm::max
Align max(MaybeAlign Lhs, Align Rhs)
Definition: Alignment.h:340
llvm::createInstrOrderFilePass
ModulePass * createInstrOrderFilePass()
Definition: InstrOrderFile.cpp:210
llvm::CallBase
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1161
llvm::GCOVOptions::NoRedZone
bool NoRedZone
Definition: Instrumentation.h:66
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
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:166
llvm::SanitizerCoverageOptions::IndirectCalls
bool IndirectCalls
Definition: Instrumentation.h:159
llvm::GCOVOptions::Atomic
bool Atomic
Definition: Instrumentation.h:69
llvm::GCOVOptions::EmitNotes
bool EmitNotes
Definition: Instrumentation.h:56