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