LLVM 18.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
27namespace llvm {
28
29class Triple;
30class OptimizationRemarkEmitter;
31class Comdat;
32class CallBase;
33
34/// Instrumentation passes often insert conditional checks into entry blocks.
35/// Call this function before splitting the entry block to move instructions
36/// that must remain in the entry block up before the split point. Static
37/// allocas and llvm.localescape calls, for example, must remain in the entry
38/// block.
41
42// Create a constant for Str so that we can pass it to the run-time lib.
43GlobalVariable *createPrivateGlobalForString(Module &M, StringRef Str,
44 bool AllowMerging,
45 const char *NamePrefix = "");
46
47// Returns F.getComdat() if it exists.
48// Otherwise creates a new comdat, sets F's comdat, and returns it.
49// Returns nullptr on failure.
50Comdat *getOrCreateFunctionComdat(Function &F, Triple &T);
51
52// Insert GCOV profiling instrumentation
54 static GCOVOptions getDefault();
55
56 // Specify whether to emit .gcno files.
58
59 // Specify whether to modify the program to emit .gcda files when run.
61
62 // A four-byte version string. The meaning of a version string is described in
63 // gcc's gcov-io.h
64 char Version[4];
65
66 // Add the 'noredzone' attribute to added runtime library calls.
68
69 // Use atomic profile counter increments.
70 bool Atomic = false;
71
72 // Regexes separated by a semi-colon to filter the files to instrument.
73 std::string Filter;
74
75 // Regexes separated by a semi-colon to filter the files to not instrument.
76 std::string Exclude;
77};
78
79// The pgo-specific indirect call promotion function declared below is used by
80// the pgo-driven indirect call promotion and sample profile passes. It's a
81// wrapper around llvm::promoteCall, et al. that additionally computes !prof
82// metadata. We place it in a pgo namespace so it's not confused with the
83// generic utilities.
84namespace pgo {
85
86// Helper function that transforms CB (either an indirect-call instruction, or
87// an invoke instruction , to a conditional call to F. This is like:
88// if (Inst.CalledValue == F)
89// F(...);
90// else
91// Inst(...);
92// end
93// TotalCount is the profile count value that the instruction executes.
94// Count is the profile count value that F is the target function.
95// These two values are used to update the branch weight.
96// If \p AttachProfToDirectCall is true, a prof metadata is attached to the
97// new direct call to contain \p Count.
98// Returns the promoted direct call instruction.
100 uint64_t TotalCount, bool AttachProfToDirectCall,
102} // namespace pgo
103
104/// Options for the frontend instrumentation based profiling pass.
106 // Add the 'noredzone' attribute to added runtime library calls.
107 bool NoRedZone = false;
108
109 // Do counter register promotion
110 bool DoCounterPromotion = false;
111
112 // Use atomic profile counter increments.
113 bool Atomic = false;
114
115 // Use BFI to guide register promotion
116 bool UseBFIInPromotion = false;
117
118 // Name of the profile file to use as output
120
121 InstrProfOptions() = default;
122};
123
124// Options for sanitizer coverage instrumentation.
126 enum Type {
132 bool IndirectCalls = false;
133 bool TraceBB = false;
134 bool TraceCmp = false;
135 bool TraceDiv = false;
136 bool TraceGep = false;
137 bool Use8bitCounters = false;
138 bool TracePC = false;
139 bool TracePCGuard = false;
140 bool Inline8bitCounters = false;
141 bool InlineBoolFlag = false;
142 bool PCTable = false;
143 bool NoPrune = false;
144 bool StackDepth = false;
145 bool TraceLoads = false;
146 bool TraceStores = false;
147 bool CollectControlFlow = false;
148
150};
151
152/// Calculate what to divide by to scale counts.
153///
154/// Given the maximum count, calculate a divisor that will scale all the
155/// weights to strictly less than std::numeric_limits<uint32_t>::max().
156static inline uint64_t calculateCountScale(uint64_t MaxCount) {
157 return MaxCount < std::numeric_limits<uint32_t>::max()
158 ? 1
159 : MaxCount / std::numeric_limits<uint32_t>::max() + 1;
160}
161
162/// Scale an individual branch count.
163///
164/// Scale a 64-bit weight down to 32-bits using \c Scale.
165///
166static inline uint32_t scaleBranchCount(uint64_t Count, uint64_t Scale) {
167 uint64_t Scaled = Count / Scale;
168 assert(Scaled <= std::numeric_limits<uint32_t>::max() && "overflow 32-bits");
169 return Scaled;
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};
191} // end namespace llvm
192
193#endif // LLVM_TRANSFORMS_INSTRUMENTATION_H
@ Scaled
#define F(x, y, z)
Definition: MD5.cpp:55
Machine Check Debug Module
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:173
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1227
Subprogram description.
void SetCurrentDebugLocation(DebugLoc L)
Set location information used by debugging information.
Definition: IRBuilder.h:220
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:2639
const Function * getFunction() const
Return the function this instruction belongs to.
Definition: Instruction.cpp:75
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1504
The optimization diagnostic interface.
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
GlobalVariable * createPrivateGlobalForString(Module &M, StringRef Str, bool AllowMerging, const char *NamePrefix="")
Comdat * getOrCreateFunctionComdat(Function &F, Triple &T)
static uint32_t scaleBranchCount(uint64_t Count, uint64_t Scale)
Scale an individual branch count.
static uint64_t calculateCountScale(uint64_t MaxCount)
Calculate what to divide by to scale counts.
BasicBlock::iterator PrepareToSplitEntryBlock(BasicBlock &BB, BasicBlock::iterator IP)
Instrumentation passes often insert conditional checks into entry blocks.
static GCOVOptions getDefault()
std::string Exclude
std::string Filter
Options for the frontend instrumentation based profiling pass.
std::string InstrProfileOutput
InstrumentationIRBuilder(Instruction *IP)
static void ensureDebugInfo(IRBuilder<> &IRB, const Function &F)
enum llvm::SanitizerCoverageOptions::Type CoverageType