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 <cassert>
23 #include <cstdint>
24 #include <limits>
25 #include <string>
26
27 namespace llvm {
28
29 class Triple;
30 class OptimizationRemarkEmitter;
31 class Comdat;
32 class 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.
39 BasicBlock::iterator PrepareToSplitEntryBlock(BasicBlock &BB,
40 BasicBlock::iterator IP);
41
42 // Create a constant for Str so that we can pass it to the run-time lib.
43 GlobalVariable *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.
50 Comdat *getOrCreateFunctionComdat(Function &F, Triple &T);
51
52 // Place global in a large section for x86-64 ELF binaries to mitigate
53 // relocation overflow pressure. This can be be used for metadata globals that
54 // aren't directly accessed by code, which has no performance impact.
55 void setGlobalVariableLargeSection(const Triple &TargetTriple,
56 GlobalVariable &GV);
57
58 // Insert GCOV profiling instrumentation
59 struct GCOVOptions {
60 static GCOVOptions getDefault();
61
62 // Specify whether to emit .gcno files.
63 bool EmitNotes;
64
65 // Specify whether to modify the program to emit .gcda files when run.
66 bool EmitData;
67
68 // A four-byte version string. The meaning of a version string is described in
69 // gcc's gcov-io.h
70 char Version[4];
71
72 // Add the 'noredzone' attribute to added runtime library calls.
73 bool NoRedZone;
74
75 // Use atomic profile counter increments.
76 bool Atomic = false;
77
78 // Regexes separated by a semi-colon to filter the files to instrument.
79 std::string Filter;
80
81 // Regexes separated by a semi-colon to filter the files to not instrument.
82 std::string Exclude;
83 };
84
85 // The pgo-specific indirect call promotion function declared below is used by
86 // the pgo-driven indirect call promotion and sample profile passes. It's a
87 // wrapper around llvm::promoteCall, et al. that additionally computes !prof
88 // metadata. We place it in a pgo namespace so it's not confused with the
89 // generic utilities.
90 namespace pgo {
91
92 // Helper function that transforms CB (either an indirect-call instruction, or
93 // an invoke instruction , to a conditional call to F. This is like:
94 // if (Inst.CalledValue == F)
95 // F(...);
96 // else
97 // Inst(...);
98 // end
99 // TotalCount is the profile count value that the instruction executes.
100 // Count is the profile count value that F is the target function.
101 // These two values are used to update the branch weight.
102 // If \p AttachProfToDirectCall is true, a prof metadata is attached to the
103 // new direct call to contain \p Count.
104 // Returns the promoted direct call instruction.
105 CallBase &promoteIndirectCall(CallBase &CB, Function *F, uint64_t Count,
106 uint64_t TotalCount, bool AttachProfToDirectCall,
107 OptimizationRemarkEmitter *ORE);
108 } // namespace pgo
109
110 /// Options for the frontend instrumentation based profiling pass.
111 struct InstrProfOptions {
112 // Add the 'noredzone' attribute to added runtime library calls.
113 bool NoRedZone = false;
114
115 // Do counter register promotion
116 bool DoCounterPromotion = false;
117
118 // Use atomic profile counter increments.
119 bool Atomic = false;
120
121 // Use BFI to guide register promotion
122 bool UseBFIInPromotion = false;
123
124 // Use sampling to reduce the profile instrumentation runtime overhead.
125 bool Sampling = false;
126
127 // Name of the profile file to use as output
128 std::string InstrProfileOutput;
129
130 InstrProfOptions() = default;
131 };
132
133 // Create the variable for profile sampling.
134 void createProfileSamplingVar(Module &M);
135
136 // Options for sanitizer coverage instrumentation.
137 struct SanitizerCoverageOptions {
138 enum Type {
139 SCK_None = 0,
140 SCK_Function,
141 SCK_BB,
142 SCK_Edge
143 } CoverageType = SCK_None;
144 bool IndirectCalls = false;
145 bool TraceBB = false;
146 bool TraceCmp = false;
147 bool TraceDiv = false;
148 bool TraceGep = false;
149 bool Use8bitCounters = false;
150 bool TracePC = false;
151 bool TracePCGuard = false;
152 bool Inline8bitCounters = false;
153 bool InlineBoolFlag = false;
154 bool PCTable = false;
155 bool NoPrune = false;
156 bool StackDepth = false;
157 bool TraceLoads = false;
158 bool TraceStores = false;
159 bool CollectControlFlow = false;
160
161 SanitizerCoverageOptions() = default;
162 };
163
164 /// Calculate what to divide by to scale counts.
165 ///
166 /// Given the maximum count, calculate a divisor that will scale all the
167 /// weights to strictly less than std::numeric_limits<uint32_t>::max().
calculateCountScale(uint64_t MaxCount)168 static inline uint64_t calculateCountScale(uint64_t MaxCount) {
169 return MaxCount < std::numeric_limits<uint32_t>::max()
170 ? 1
171 : MaxCount / std::numeric_limits<uint32_t>::max() + 1;
172 }
173
174 /// Scale an individual branch count.
175 ///
176 /// Scale a 64-bit weight down to 32-bits using \c Scale.
177 ///
scaleBranchCount(uint64_t Count,uint64_t Scale)178 static inline uint32_t scaleBranchCount(uint64_t Count, uint64_t Scale) {
179 uint64_t Scaled = Count / Scale;
180 assert(Scaled <= std::numeric_limits<uint32_t>::max() && "overflow 32-bits");
181 return Scaled;
182 }
183
184 // Use to ensure the inserted instrumentation has a DebugLocation; if none is
185 // attached to the source instruction, try to use a DILocation with offset 0
186 // scoped to surrounding function (if it has a DebugLocation).
187 //
188 // Some non-call instructions may be missing debug info, but when inserting
189 // instrumentation calls, some builds (e.g. LTO) want calls to have debug info
190 // if the enclosing function does.
191 struct InstrumentationIRBuilder : IRBuilder<> {
ensureDebugInfoInstrumentationIRBuilder192 static void ensureDebugInfo(IRBuilder<> &IRB, const Function &F) {
193 if (IRB.getCurrentDebugLocation())
194 return;
195 if (DISubprogram *SP = F.getSubprogram())
196 IRB.SetCurrentDebugLocation(DILocation::get(SP->getContext(), 0, 0, SP));
197 }
198
InstrumentationIRBuilderInstrumentationIRBuilder199 explicit InstrumentationIRBuilder(Instruction *IP) : IRBuilder<>(IP) {
200 ensureDebugInfo(*this, *IP->getFunction());
201 }
202 };
203 } // end namespace llvm
204
205 #endif // LLVM_TRANSFORMS_INSTRUMENTATION_H
206