blob: d6d9529ba9a22c4735100bffd22fdb75916153a4 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- Transforms/Instrumentation.h - Instrumentation passes ----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines constructor functions for instrumentation passes.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_TRANSFORMS_INSTRUMENTATION_H
15#define LLVM_TRANSFORMS_INSTRUMENTATION_H
16
17#include "llvm/ADT/StringRef.h"
18#include "llvm/IR/BasicBlock.h"
19#include <cassert>
20#include <cstdint>
21#include <limits>
22#include <string>
23#include <vector>
24
25namespace llvm {
26
27class FunctionPass;
28class ModulePass;
29class OptimizationRemarkEmitter;
30
31/// Instrumentation passes often insert conditional checks into entry blocks.
32/// Call this function before splitting the entry block to move instructions
33/// that must remain in the entry block up before the split point. Static
34/// allocas and llvm.localescape calls, for example, must remain in the entry
35/// block.
36BasicBlock::iterator PrepareToSplitEntryBlock(BasicBlock &BB,
37 BasicBlock::iterator IP);
38
39// Insert GCOV profiling instrumentation
40struct GCOVOptions {
41 static GCOVOptions getDefault();
42
43 // Specify whether to emit .gcno files.
44 bool EmitNotes;
45
46 // Specify whether to modify the program to emit .gcda files when run.
47 bool EmitData;
48
49 // A four-byte version string. The meaning of a version string is described in
50 // gcc's gcov-io.h
51 char Version[4];
52
53 // Emit a "cfg checksum" that follows the "line number checksum" of a
54 // function. This affects both .gcno and .gcda files.
55 bool UseCfgChecksum;
56
57 // Add the 'noredzone' attribute to added runtime library calls.
58 bool NoRedZone;
59
60 // Emit the name of the function in the .gcda files. This is redundant, as
61 // the function identifier can be used to find the name from the .gcno file.
62 bool FunctionNamesInData;
63
64 // Emit the exit block immediately after the start block, rather than after
65 // all of the function body's blocks.
66 bool ExitBlockBeforeBody;
67};
68
69ModulePass *createGCOVProfilerPass(const GCOVOptions &Options =
70 GCOVOptions::getDefault());
71
72// PGO Instrumention
73ModulePass *createPGOInstrumentationGenLegacyPass();
74ModulePass *
75createPGOInstrumentationUseLegacyPass(StringRef Filename = StringRef(""));
76ModulePass *createPGOIndirectCallPromotionLegacyPass(bool InLTO = false,
77 bool SamplePGO = false);
78FunctionPass *createPGOMemOPSizeOptLegacyPass();
79
80// The pgo-specific indirect call promotion function declared below is used by
81// the pgo-driven indirect call promotion and sample profile passes. It's a
82// wrapper around llvm::promoteCall, et al. that additionally computes !prof
83// metadata. We place it in a pgo namespace so it's not confused with the
84// generic utilities.
85namespace pgo {
86
87// Helper function that transforms Inst (either an indirect-call instruction, or
88// an invoke instruction , to a conditional call to F. This is like:
89// if (Inst.CalledValue == F)
90// F(...);
91// else
92// Inst(...);
93// end
94// TotalCount is the profile count value that the instruction executes.
95// Count is the profile count value that F is the target function.
96// These two values are used to update the branch weight.
97// If \p AttachProfToDirectCall is true, a prof metadata is attached to the
98// new direct call to contain \p Count.
99// Returns the promoted direct call instruction.
100Instruction *promoteIndirectCall(Instruction *Inst, Function *F, uint64_t Count,
101 uint64_t TotalCount,
102 bool AttachProfToDirectCall,
103 OptimizationRemarkEmitter *ORE);
104} // namespace pgo
105
106/// Options for the frontend instrumentation based profiling pass.
107struct 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
Andrew Scull0372a572018-11-16 15:47:06 +0000114 // Use atomic profile counter increments.
115 bool Atomic = false;
116
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100117 // Name of the profile file to use as output
118 std::string InstrProfileOutput;
119
120 InstrProfOptions() = default;
121};
122
123/// Insert frontend instrumentation based profiling.
124ModulePass *createInstrProfilingLegacyPass(
125 const InstrProfOptions &Options = InstrProfOptions());
126
127// Insert AddressSanitizer (address sanity checking) instrumentation
128FunctionPass *createAddressSanitizerFunctionPass(bool CompileKernel = false,
129 bool Recover = false,
130 bool UseAfterScope = false);
131ModulePass *createAddressSanitizerModulePass(bool CompileKernel = false,
132 bool Recover = false,
133 bool UseGlobalsGC = true);
134
135// Insert MemorySanitizer instrumentation (detection of uninitialized reads)
136FunctionPass *createMemorySanitizerPass(int TrackOrigins = 0,
Andrew Scull0372a572018-11-16 15:47:06 +0000137 bool Recover = false,
138 bool EnableKmsan = false);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100139
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100140FunctionPass *createHWAddressSanitizerPass(bool CompileKernel = false,
141 bool Recover = false);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100142
143// Insert ThreadSanitizer (race detection) instrumentation
144FunctionPass *createThreadSanitizerPass();
145
146// Insert DataFlowSanitizer (dynamic data flow analysis) instrumentation
147ModulePass *createDataFlowSanitizerPass(
148 const std::vector<std::string> &ABIListFiles = std::vector<std::string>(),
149 void *(*getArgTLS)() = nullptr, void *(*getRetValTLS)() = nullptr);
150
151// Options for EfficiencySanitizer sub-tools.
152struct EfficiencySanitizerOptions {
153 enum Type {
154 ESAN_None = 0,
155 ESAN_CacheFrag,
156 ESAN_WorkingSet,
157 } ToolType = ESAN_None;
158
159 EfficiencySanitizerOptions() = default;
160};
161
162// Insert EfficiencySanitizer instrumentation.
163ModulePass *createEfficiencySanitizerPass(
164 const EfficiencySanitizerOptions &Options = EfficiencySanitizerOptions());
165
166// Options for sanitizer coverage instrumentation.
167struct SanitizerCoverageOptions {
168 enum Type {
169 SCK_None = 0,
170 SCK_Function,
171 SCK_BB,
172 SCK_Edge
173 } CoverageType = SCK_None;
174 bool IndirectCalls = false;
175 bool TraceBB = false;
176 bool TraceCmp = false;
177 bool TraceDiv = false;
178 bool TraceGep = false;
179 bool Use8bitCounters = false;
180 bool TracePC = false;
181 bool TracePCGuard = false;
182 bool Inline8bitCounters = false;
183 bool PCTable = false;
184 bool NoPrune = false;
185 bool StackDepth = false;
186
187 SanitizerCoverageOptions() = default;
188};
189
190// Insert SanitizerCoverage instrumentation.
191ModulePass *createSanitizerCoverageModulePass(
192 const SanitizerCoverageOptions &Options = SanitizerCoverageOptions());
193
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100194/// Calculate what to divide by to scale counts.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100195///
196/// Given the maximum count, calculate a divisor that will scale all the
197/// weights to strictly less than std::numeric_limits<uint32_t>::max().
198static inline uint64_t calculateCountScale(uint64_t MaxCount) {
199 return MaxCount < std::numeric_limits<uint32_t>::max()
200 ? 1
201 : MaxCount / std::numeric_limits<uint32_t>::max() + 1;
202}
203
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100204/// Scale an individual branch count.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100205///
206/// Scale a 64-bit weight down to 32-bits using \c Scale.
207///
208static inline uint32_t scaleBranchCount(uint64_t Count, uint64_t Scale) {
209 uint64_t Scaled = Count / Scale;
210 assert(Scaled <= std::numeric_limits<uint32_t>::max() && "overflow 32-bits");
211 return Scaled;
212}
213
214} // end namespace llvm
215
216#endif // LLVM_TRANSFORMS_INSTRUMENTATION_H