LLVM 24.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;
32class Comdat;
33class CallBase;
34class Module;
35
36/// Check if module has flag attached, if not add the 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.
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.
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
65 // Specify whether to emit .gcno files.
67
68 // Specify whether to force absolute paths in coverage notes files.
70
71 // Specify whether to modify the program to emit .gcda files when run.
73
74 // A four-byte version string. The meaning of a version string is described in
75 // gcc's gcov-io.h
76 char Version[4];
77
78 // Add the 'noredzone' attribute to added runtime library calls.
80
81 // Use atomic profile counter increments.
82 bool Atomic = false;
83
84 // Regexes separated by a semi-colon to filter the files to instrument.
85 std::string Filter;
86
87 // Regexes separated by a semi-colon to filter the files to not instrument.
88 std::string Exclude;
89};
90
91// The pgo-specific indirect call promotion function declared below is used by
92// the pgo-driven indirect call promotion and sample profile passes. It's a
93// wrapper around llvm::promoteCall, et al. that additionally computes !prof
94// metadata. We place it in a pgo namespace so it's not confused with the
95// generic utilities.
96namespace pgo {
97
98// Helper function that transforms CB (either an indirect-call instruction, or
99// an invoke instruction , to a conditional call to F. This is like:
100// if (Inst.CalledValue == F)
101// F(...);
102// else
103// Inst(...);
104// end
105// TotalCount is the profile count value that the instruction executes.
106// Count is the profile count value that F is the target function.
107// These two values are used to update the branch weight.
108// If \p AttachProfToDirectCall is true, a prof metadata is attached to the
109// new direct call to contain \p Count.
110// Returns the promoted direct call instruction.
112 uint64_t Count, uint64_t TotalCount,
113 bool AttachProfToDirectCall,
115} // namespace pgo
116
117/// Options for the frontend instrumentation based profiling pass.
119 // Add the 'noredzone' attribute to added runtime library calls.
120 bool NoRedZone = false;
121
122 // Do counter register promotion
123 bool DoCounterPromotion = false;
124
125 // Use atomic profile counter increments.
126 bool Atomic = false;
127
128 // Use BFI to guide register promotion
129 bool UseBFIInPromotion = false;
130
131 // Use sampling to reduce the profile instrumentation runtime overhead.
132 bool Sampling = false;
133
134 // Name of the profile file to use as output
136
137 InstrProfOptions() = default;
138};
139
140// Create the variable for profile sampling.
142
143// Options for sanitizer coverage instrumentation.
151 bool IndirectCalls = false;
152 bool TraceBB = false;
153 bool TraceCmp = false;
154 bool TraceDiv = false;
155 bool TraceGep = false;
156 bool Use8bitCounters = false;
157 bool TracePC = false;
158 bool TracePCEntryExit = false;
159 bool TracePCGuard = false;
160 bool Inline8bitCounters = false;
161 bool InlineBoolFlag = false;
162 bool PCTable = false;
163 bool NoPrune = false;
164 bool StackDepth = false;
165 bool TraceLoads = false;
166 bool TraceStores = false;
167 bool CollectControlFlow = false;
168 bool GatedCallbacks = false;
170
172};
173
174// Use to ensure the inserted instrumentation has a DebugLocation; if none is
175// attached to the source instruction, try to use a DILocation with offset 0
176// scoped to surrounding function (if it has a DebugLocation).
177//
178// Some non-call instructions may be missing debug info, but when inserting
179// instrumentation calls, some builds (e.g. LTO) want calls to have debug info
180// if the enclosing function does.
182 static void ensureDebugInfo(IRBuilder<> &IRB, const Function &F) {
183 if (IRB.getCurrentDebugLocation())
184 return;
185 if (DISubprogram *SP = F.getSubprogram())
186 IRB.SetCurrentDebugLocation(DILocation::get(SP->getContext(), 0, 0, SP));
187 }
188
190 ensureDebugInfo(*this, *IP->getFunction());
191 }
192
194 ensureDebugInfo(*this, *It.getNodeParent()->getParent());
195 }
196};
197} // end namespace llvm
198
199#endif // LLVM_TRANSFORMS_INSTRUMENTATION_H
uint64_t uint64_t
#define LLVM_ABI
Definition Compiler.h:211
#define F(x, y, z)
Definition MD5.cpp:54
#define T
LLVM Basic Block Representation.
Definition BasicBlock.h:62
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Subprogram description. Uses SubclassData1.
void SetCurrentDebugLocation(const DebugLoc &L)
Set location information used by debugging information.
Definition IRBuilder.h:228
LLVM_ABI DebugLoc getCurrentDebugLocation() const
Get location information used by debugging information.
Definition IRBuilder.cpp:66
IRBuilder(LLVMContext &C, FolderTy Folder, InserterTy Inserter)
Definition IRBuilder.h:2923
LLVM_ABI const Function * getFunction() const
Return the function this instruction belongs to.
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1579
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
The optimization diagnostic interface.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:48
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
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.
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)
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
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.
InstrumentationIRBuilder(BasicBlock::iterator It)
InstrumentationIRBuilder(Instruction *IP)
static void ensureDebugInfo(IRBuilder<> &IRB, const Function &F)
enum llvm::SanitizerCoverageOptions::Type CoverageType