blob: 29cb281813682845996c34c038237dd34751d653 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- Transforms/Instrumentation.h - Instrumentation passes ----*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
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 <cassert>
19#include <cstdint>
20#include <limits>
21#include <string>
22#include <vector>
23
24namespace llvm {
25
Andrew Walbran16937d02019-10-22 13:54:20 +010026class Triple;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010027class FunctionPass;
28class ModulePass;
29class OptimizationRemarkEmitter;
Andrew Walbran16937d02019-10-22 13:54:20 +010030class Comdat;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010031
32/// Instrumentation passes often insert conditional checks into entry blocks.
33/// Call this function before splitting the entry block to move instructions
34/// that must remain in the entry block up before the split point. Static
35/// allocas and llvm.localescape calls, for example, must remain in the entry
36/// block.
37BasicBlock::iterator PrepareToSplitEntryBlock(BasicBlock &BB,
38 BasicBlock::iterator IP);
39
Andrew Walbran16937d02019-10-22 13:54:20 +010040// Create a constant for Str so that we can pass it to the run-time lib.
41GlobalVariable *createPrivateGlobalForString(Module &M, StringRef Str,
42 bool AllowMerging,
43 const char *NamePrefix = "");
44
45// Returns F.getComdat() if it exists.
46// Otherwise creates a new comdat, sets F's comdat, and returns it.
47// Returns nullptr on failure.
48Comdat *GetOrCreateFunctionComdat(Function &F, Triple &T,
49 const std::string &ModuleId);
50
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010051// Insert GCOV profiling instrumentation
52struct GCOVOptions {
53 static GCOVOptions getDefault();
54
55 // Specify whether to emit .gcno files.
56 bool EmitNotes;
57
58 // Specify whether to modify the program to emit .gcda files when run.
59 bool EmitData;
60
61 // A four-byte version string. The meaning of a version string is described in
62 // gcc's gcov-io.h
63 char Version[4];
64
65 // Emit a "cfg checksum" that follows the "line number checksum" of a
66 // function. This affects both .gcno and .gcda files.
67 bool UseCfgChecksum;
68
69 // Add the 'noredzone' attribute to added runtime library calls.
70 bool NoRedZone;
71
72 // Emit the name of the function in the .gcda files. This is redundant, as
73 // the function identifier can be used to find the name from the .gcno file.
74 bool FunctionNamesInData;
75
76 // Emit the exit block immediately after the start block, rather than after
77 // all of the function body's blocks.
78 bool ExitBlockBeforeBody;
Andrew Walbran16937d02019-10-22 13:54:20 +010079
80 // Regexes separated by a semi-colon to filter the files to instrument.
81 std::string Filter;
82
83 // Regexes separated by a semi-colon to filter the files to not instrument.
84 std::string Exclude;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010085};
86
87ModulePass *createGCOVProfilerPass(const GCOVOptions &Options =
88 GCOVOptions::getDefault());
89
90// PGO Instrumention
91ModulePass *createPGOInstrumentationGenLegacyPass();
92ModulePass *
93createPGOInstrumentationUseLegacyPass(StringRef Filename = StringRef(""));
94ModulePass *createPGOIndirectCallPromotionLegacyPass(bool InLTO = false,
95 bool SamplePGO = false);
96FunctionPass *createPGOMemOPSizeOptLegacyPass();
97
98// The pgo-specific indirect call promotion function declared below is used by
99// the pgo-driven indirect call promotion and sample profile passes. It's a
100// wrapper around llvm::promoteCall, et al. that additionally computes !prof
101// metadata. We place it in a pgo namespace so it's not confused with the
102// generic utilities.
103namespace pgo {
104
105// Helper function that transforms Inst (either an indirect-call instruction, or
106// an invoke instruction , to a conditional call to F. This is like:
107// if (Inst.CalledValue == F)
108// F(...);
109// else
110// Inst(...);
111// end
112// TotalCount is the profile count value that the instruction executes.
113// Count is the profile count value that F is the target function.
114// These two values are used to update the branch weight.
115// If \p AttachProfToDirectCall is true, a prof metadata is attached to the
116// new direct call to contain \p Count.
117// Returns the promoted direct call instruction.
118Instruction *promoteIndirectCall(Instruction *Inst, Function *F, uint64_t Count,
119 uint64_t TotalCount,
120 bool AttachProfToDirectCall,
121 OptimizationRemarkEmitter *ORE);
122} // namespace pgo
123
124/// Options for the frontend instrumentation based profiling pass.
125struct InstrProfOptions {
126 // Add the 'noredzone' attribute to added runtime library calls.
127 bool NoRedZone = false;
128
129 // Do counter register promotion
130 bool DoCounterPromotion = false;
131
Andrew Scull0372a572018-11-16 15:47:06 +0000132 // Use atomic profile counter increments.
133 bool Atomic = false;
134
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100135 // Name of the profile file to use as output
136 std::string InstrProfileOutput;
137
138 InstrProfOptions() = default;
139};
140
141/// Insert frontend instrumentation based profiling.
142ModulePass *createInstrProfilingLegacyPass(
143 const InstrProfOptions &Options = InstrProfOptions());
144
145// Insert AddressSanitizer (address sanity checking) instrumentation
146FunctionPass *createAddressSanitizerFunctionPass(bool CompileKernel = false,
147 bool Recover = false,
148 bool UseAfterScope = false);
149ModulePass *createAddressSanitizerModulePass(bool CompileKernel = false,
150 bool Recover = false,
Andrew Walbran16937d02019-10-22 13:54:20 +0100151 bool UseGlobalsGC = true,
152 bool UseOdrIndicator = true);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100153
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100154FunctionPass *createHWAddressSanitizerPass(bool CompileKernel = false,
155 bool Recover = false);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100156
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100157// Insert DataFlowSanitizer (dynamic data flow analysis) instrumentation
158ModulePass *createDataFlowSanitizerPass(
159 const std::vector<std::string> &ABIListFiles = std::vector<std::string>(),
160 void *(*getArgTLS)() = nullptr, void *(*getRetValTLS)() = nullptr);
161
162// Options for EfficiencySanitizer sub-tools.
163struct EfficiencySanitizerOptions {
164 enum Type {
165 ESAN_None = 0,
166 ESAN_CacheFrag,
167 ESAN_WorkingSet,
168 } ToolType = ESAN_None;
169
170 EfficiencySanitizerOptions() = default;
171};
172
173// Insert EfficiencySanitizer instrumentation.
174ModulePass *createEfficiencySanitizerPass(
175 const EfficiencySanitizerOptions &Options = EfficiencySanitizerOptions());
176
177// Options for sanitizer coverage instrumentation.
178struct SanitizerCoverageOptions {
179 enum Type {
180 SCK_None = 0,
181 SCK_Function,
182 SCK_BB,
183 SCK_Edge
184 } CoverageType = SCK_None;
185 bool IndirectCalls = false;
186 bool TraceBB = false;
187 bool TraceCmp = false;
188 bool TraceDiv = false;
189 bool TraceGep = false;
190 bool Use8bitCounters = false;
191 bool TracePC = false;
192 bool TracePCGuard = false;
193 bool Inline8bitCounters = false;
194 bool PCTable = false;
195 bool NoPrune = false;
196 bool StackDepth = false;
197
198 SanitizerCoverageOptions() = default;
199};
200
201// Insert SanitizerCoverage instrumentation.
202ModulePass *createSanitizerCoverageModulePass(
203 const SanitizerCoverageOptions &Options = SanitizerCoverageOptions());
204
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100205/// Calculate what to divide by to scale counts.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100206///
207/// Given the maximum count, calculate a divisor that will scale all the
208/// weights to strictly less than std::numeric_limits<uint32_t>::max().
209static inline uint64_t calculateCountScale(uint64_t MaxCount) {
210 return MaxCount < std::numeric_limits<uint32_t>::max()
211 ? 1
212 : MaxCount / std::numeric_limits<uint32_t>::max() + 1;
213}
214
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100215/// Scale an individual branch count.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100216///
217/// Scale a 64-bit weight down to 32-bits using \c Scale.
218///
219static inline uint32_t scaleBranchCount(uint64_t Count, uint64_t Scale) {
220 uint64_t Scaled = Count / Scale;
221 assert(Scaled <= std::numeric_limits<uint32_t>::max() && "overflow 32-bits");
222 return Scaled;
223}
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100224} // end namespace llvm
225
226#endif // LLVM_TRANSFORMS_INSTRUMENTATION_H