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 "llvm/IR/DebugInfoMetadata.h" |
19 | #include "llvm/IR/Function.h" |
20 | #include "llvm/IR/IRBuilder.h" |
21 | #include "llvm/IR/Instruction.h" |
22 | #include "llvm/Support/Compiler.h" |
23 | #include <cassert> |
24 | #include <cstdint> |
25 | #include <limits> |
26 | #include <string> |
27 | |
28 | namespace llvm { |
29 | |
30 | class Triple; |
31 | class ; |
32 | class Comdat; |
33 | class CallBase; |
34 | class Module; |
35 | |
36 | /// Check if module has flag attached, if not add the flag. |
37 | LLVM_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. |
44 | LLVM_ABI BasicBlock::iterator PrepareToSplitEntryBlock(BasicBlock &BB, |
45 | BasicBlock::iterator IP); |
46 | |
47 | // Create a constant for Str so that we can pass it to the run-time lib. |
48 | LLVM_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. |
55 | LLVM_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. |
60 | LLVM_ABI void setGlobalVariableLargeSection(const Triple &TargetTriple, |
61 | GlobalVariable &GV); |
62 | |
63 | // Insert GCOV profiling instrumentation |
64 | struct GCOVOptions { |
65 | LLVM_ABI static GCOVOptions getDefault(); |
66 | |
67 | // Specify whether to emit .gcno files. |
68 | bool EmitNotes; |
69 | |
70 | // Specify whether to modify the program to emit .gcda files when run. |
71 | bool EmitData; |
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. |
78 | bool NoRedZone; |
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. |
95 | namespace 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. |
110 | LLVM_ABI CallBase &(CallBase &CB, Function *F, |
111 | uint64_t Count, uint64_t TotalCount, |
112 | bool AttachProfToDirectCall, |
113 | OptimizationRemarkEmitter *ORE); |
114 | } // namespace pgo |
115 | |
116 | /// Options for the frontend instrumentation based profiling pass. |
117 | struct InstrProfOptions { |
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 |
134 | std::string InstrProfileOutput; |
135 | |
136 | InstrProfOptions() = default; |
137 | }; |
138 | |
139 | // Create the variable for profile sampling. |
140 | LLVM_ABI void createProfileSamplingVar(Module &M); |
141 | |
142 | // Options for sanitizer coverage instrumentation. |
143 | struct SanitizerCoverageOptions { |
144 | enum Type { |
145 | SCK_None = 0, |
146 | SCK_Function, |
147 | SCK_BB, |
148 | SCK_Edge |
149 | } CoverageType = SCK_None; |
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; |
167 | int StackDepthCallbackMin = 0; |
168 | |
169 | SanitizerCoverageOptions() = default; |
170 | }; |
171 | |
172 | /// Calculate what to divide by to scale counts. |
173 | /// |
174 | /// Given the maximum count, calculate a divisor that will scale all the |
175 | /// weights to strictly less than std::numeric_limits<uint32_t>::max(). |
176 | static inline uint64_t calculateCountScale(uint64_t MaxCount) { |
177 | return MaxCount < std::numeric_limits<uint32_t>::max() |
178 | ? 1 |
179 | : MaxCount / std::numeric_limits<uint32_t>::max() + 1; |
180 | } |
181 | |
182 | /// Scale an individual branch count. |
183 | /// |
184 | /// Scale a 64-bit weight down to 32-bits using \c Scale. |
185 | /// |
186 | static inline uint32_t scaleBranchCount(uint64_t Count, uint64_t Scale) { |
187 | uint64_t Scaled = Count / Scale; |
188 | assert(Scaled <= std::numeric_limits<uint32_t>::max() && "overflow 32-bits" ); |
189 | return Scaled; |
190 | } |
191 | |
192 | // Use to ensure the inserted instrumentation has a DebugLocation; if none is |
193 | // attached to the source instruction, try to use a DILocation with offset 0 |
194 | // scoped to surrounding function (if it has a DebugLocation). |
195 | // |
196 | // Some non-call instructions may be missing debug info, but when inserting |
197 | // instrumentation calls, some builds (e.g. LTO) want calls to have debug info |
198 | // if the enclosing function does. |
199 | struct InstrumentationIRBuilder : IRBuilder<> { |
200 | static void ensureDebugInfo(IRBuilder<> &IRB, const Function &F) { |
201 | if (IRB.getCurrentDebugLocation()) |
202 | return; |
203 | if (DISubprogram *SP = F.getSubprogram()) |
204 | IRB.SetCurrentDebugLocation(DILocation::get(Context&: SP->getContext(), Line: 0, Column: 0, Scope: SP)); |
205 | } |
206 | |
207 | explicit InstrumentationIRBuilder(Instruction *IP) : IRBuilder<>(IP) { |
208 | ensureDebugInfo(IRB&: *this, F: *IP->getFunction()); |
209 | } |
210 | |
211 | explicit InstrumentationIRBuilder(BasicBlock *BB, BasicBlock::iterator It) |
212 | : IRBuilder<>(BB, It) { |
213 | ensureDebugInfo(IRB&: *this, F: *BB->getParent()); |
214 | } |
215 | }; |
216 | } // end namespace llvm |
217 | |
218 | #endif // LLVM_TRANSFORMS_INSTRUMENTATION_H |
219 | |