LLVM 22.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"
23#include <cassert>
24#include <cstdint>
25#include <limits>
26#include <string>
27
28namespace llvm {
29
30class Triple;
31class OptimizationRemarkEmitter;
32class Comdat;
33class CallBase;
34class Module;
35
36/// Check if module has flag attached, if not add the flag.
37LLVM_ABI bool checkIfAlreadyInstrumented(Module &M, StringRef Flag);
38
39/// Instrumentation passes often insert conditional checks into entry blocks.
40/// Call this function before splitting the entry block to move instructions
41/// that must remain in the entry block up before the split point. Static
42/// allocas and llvm.localescape calls, for example, must remain in the entry
43/// block.
46
47// Create a constant for Str so that we can pass it to the run-time lib.
48LLVM_ABI GlobalVariable *createPrivateGlobalForString(Module &M, StringRef Str,
49 bool AllowMerging,
50 Twine NamePrefix = "");
51
52// Returns F.getComdat() if it exists.
53// Otherwise creates a new comdat, sets F's comdat, and returns it.
54// Returns nullptr on failure.
55LLVM_ABI Comdat *getOrCreateFunctionComdat(Function &F, Triple &T);
56
57// Place global in a large section for x86-64 ELF binaries to mitigate
58// relocation overflow pressure. This can be be used for metadata globals that
59// aren't directly accessed by code, which has no performance impact.
60LLVM_ABI void setGlobalVariableLargeSection(const Triple &TargetTriple,
61 GlobalVariable &GV);
62
63// Insert GCOV profiling instrumentation
66
67 // Specify whether to emit .gcno files.
69
70 // Specify whether to modify the program to emit .gcda files when run.
72
73 // A four-byte version string. The meaning of a version string is described in
74 // gcc's gcov-io.h
75 char Version[4];
76
77 // Add the 'noredzone' attribute to added runtime library calls.
79
80 // Use atomic profile counter increments.
81 bool Atomic = false;
82
83 // Regexes separated by a semi-colon to filter the files to instrument.
84 std::string Filter;
85
86 // Regexes separated by a semi-colon to filter the files to not instrument.
87 std::string Exclude;
88};
89
90// The pgo-specific indirect call promotion function declared below is used by
91// the pgo-driven indirect call promotion and sample profile passes. It's a
92// wrapper around llvm::promoteCall, et al. that additionally computes !prof
93// metadata. We place it in a pgo namespace so it's not confused with the
94// generic utilities.
95namespace pgo {
96
97// Helper function that transforms CB (either an indirect-call instruction, or
98// an invoke instruction , to a conditional call to F. This is like:
99// if (Inst.CalledValue == F)
100// F(...);
101// else
102// Inst(...);
103// end
104// TotalCount is the profile count value that the instruction executes.
105// Count is the profile count value that F is the target function.
106// These two values are used to update the branch weight.
107// If \p AttachProfToDirectCall is true, a prof metadata is attached to the
108// new direct call to contain \p Count.
109// Returns the promoted direct call instruction.
111 uint64_t Count, uint64_t TotalCount,
112 bool AttachProfToDirectCall,
114} // namespace pgo
115
116/// Options for the frontend instrumentation based profiling pass.
118 // Add the 'noredzone' attribute to added runtime library calls.
119 bool NoRedZone = false;
120
121 // Do counter register promotion
122 bool DoCounterPromotion = false;
123
124 // Use atomic profile counter increments.
125 bool Atomic = false;
126
127 // Use BFI to guide register promotion
128 bool UseBFIInPromotion = false;
129
130 // Use sampling to reduce the profile instrumentation runtime overhead.
131 bool Sampling = false;
132
133 // Name of the profile file to use as output
135
136 InstrProfOptions() = default;
137};
138
139// Create the variable for profile sampling.
141
142// Options for sanitizer coverage instrumentation.
144 enum Type {
150 bool IndirectCalls = false;
151 bool TraceBB = false;
152 bool TraceCmp = false;
153 bool TraceDiv = false;
154 bool TraceGep = false;
155 bool Use8bitCounters = false;
156 bool TracePC = false;
157 bool TracePCGuard = false;
158 bool Inline8bitCounters = false;
159 bool InlineBoolFlag = false;
160 bool PCTable = false;
161 bool NoPrune = false;
162 bool StackDepth = false;
163 bool TraceLoads = false;
164 bool TraceStores = false;
165 bool CollectControlFlow = false;
166 bool GatedCallbacks = false;
168
170};
171
172// Use to ensure the inserted instrumentation has a DebugLocation; if none is
173// attached to the source instruction, try to use a DILocation with offset 0
174// scoped to surrounding function (if it has a DebugLocation).
175//
176// Some non-call instructions may be missing debug info, but when inserting
177// instrumentation calls, some builds (e.g. LTO) want calls to have debug info
178// if the enclosing function does.
180 static void ensureDebugInfo(IRBuilder<> &IRB, const Function &F) {
181 if (IRB.getCurrentDebugLocation())
182 return;
183 if (DISubprogram *SP = F.getSubprogram())
184 IRB.SetCurrentDebugLocation(DILocation::get(SP->getContext(), 0, 0, SP));
185 }
186
188 ensureDebugInfo(*this, *IP->getFunction());
189 }
190
192 : IRBuilder<>(BB, It) {
193 ensureDebugInfo(*this, *BB->getParent());
194 }
195};
196} // end namespace llvm
197
198#endif // LLVM_TRANSFORMS_INSTRUMENTATION_H
#define LLVM_ABI
Definition: Compiler.h:213
#define F(x, y, z)
Definition: MD5.cpp:55
Machine Check Debug Module
LLVM Basic Block Representation.
Definition: BasicBlock.h:62
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:213
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:170
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1116
Subprogram description. Uses SubclassData1.
BasicBlock * BB
Definition: IRBuilder.h:146
void SetCurrentDebugLocation(DebugLoc L)
Set location information used by debugging information.
Definition: IRBuilder.h:247
LLVM_ABI DebugLoc getCurrentDebugLocation() const
Get location information used by debugging information.
Definition: IRBuilder.cpp:63
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2780
LLVM_ABI const Function * getFunction() const
Return the function this instruction belongs to.
Definition: Instruction.cpp:82
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1565
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
The optimization diagnostic interface.
LLVM_ABI CallBase & promoteIndirectCall(CallBase &CB, Function *F, uint64_t Count, uint64_t TotalCount, bool AttachProfToDirectCall, OptimizationRemarkEmitter *ORE)
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
LLVM_ABI GlobalVariable * createPrivateGlobalForString(Module &M, StringRef Str, bool AllowMerging, Twine NamePrefix="")
LLVM_ABI void createProfileSamplingVar(Module &M)
LLVM_ABI Comdat * getOrCreateFunctionComdat(Function &F, Triple &T)
LLVM_ABI void setGlobalVariableLargeSection(const Triple &TargetTriple, GlobalVariable &GV)
LLVM_ABI BasicBlock::iterator PrepareToSplitEntryBlock(BasicBlock &BB, BasicBlock::iterator IP)
Instrumentation passes often insert conditional checks into entry blocks.
LLVM_ABI bool checkIfAlreadyInstrumented(Module &M, StringRef Flag)
Check if module has flag attached, if not add the flag.
static LLVM_ABI GCOVOptions getDefault()
std::string Exclude
std::string Filter
Options for the frontend instrumentation based profiling pass.
std::string InstrProfileOutput
InstrumentationIRBuilder(BasicBlock *BB, BasicBlock::iterator It)
InstrumentationIRBuilder(Instruction *IP)
static void ensureDebugInfo(IRBuilder<> &IRB, const Function &F)
enum llvm::SanitizerCoverageOptions::Type CoverageType