blob: c960d5b0ab5015f52705bdfc55786f035301a9fe [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;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020031class CallBase;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010032
33/// Instrumentation passes often insert conditional checks into entry blocks.
34/// Call this function before splitting the entry block to move instructions
35/// that must remain in the entry block up before the split point. Static
36/// allocas and llvm.localescape calls, for example, must remain in the entry
37/// block.
38BasicBlock::iterator PrepareToSplitEntryBlock(BasicBlock &BB,
39 BasicBlock::iterator IP);
40
Andrew Walbran16937d02019-10-22 13:54:20 +010041// Create a constant for Str so that we can pass it to the run-time lib.
42GlobalVariable *createPrivateGlobalForString(Module &M, StringRef Str,
43 bool AllowMerging,
44 const char *NamePrefix = "");
45
46// Returns F.getComdat() if it exists.
47// Otherwise creates a new comdat, sets F's comdat, and returns it.
48// Returns nullptr on failure.
49Comdat *GetOrCreateFunctionComdat(Function &F, Triple &T,
50 const std::string &ModuleId);
51
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010052// Insert GCOV profiling instrumentation
53struct GCOVOptions {
54 static GCOVOptions getDefault();
55
56 // Specify whether to emit .gcno files.
57 bool EmitNotes;
58
59 // Specify whether to modify the program to emit .gcda files when run.
60 bool EmitData;
61
62 // A four-byte version string. The meaning of a version string is described in
63 // gcc's gcov-io.h
64 char Version[4];
65
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010066 // Add the 'noredzone' attribute to added runtime library calls.
67 bool NoRedZone;
68
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020069 // Use atomic profile counter increments.
70 bool Atomic = false;
Andrew Walbran16937d02019-10-22 13:54:20 +010071
72 // Regexes separated by a semi-colon to filter the files to instrument.
73 std::string Filter;
74
75 // Regexes separated by a semi-colon to filter the files to not instrument.
76 std::string Exclude;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010077};
78
79ModulePass *createGCOVProfilerPass(const GCOVOptions &Options =
80 GCOVOptions::getDefault());
81
Andrew Walbran3d2c1972020-04-07 12:24:26 +010082// PGO Instrumention. Parameter IsCS indicates if this is the context senstive
83// instrumentation.
84ModulePass *createPGOInstrumentationGenLegacyPass(bool IsCS = false);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010085ModulePass *
Andrew Walbran3d2c1972020-04-07 12:24:26 +010086createPGOInstrumentationUseLegacyPass(StringRef Filename = StringRef(""),
87 bool IsCS = false);
88ModulePass *createPGOInstrumentationGenCreateVarLegacyPass(
89 StringRef CSInstrName = StringRef(""));
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010090ModulePass *createPGOIndirectCallPromotionLegacyPass(bool InLTO = false,
91 bool SamplePGO = false);
92FunctionPass *createPGOMemOPSizeOptLegacyPass();
93
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020094ModulePass *createCGProfileLegacyPass();
95
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010096// The pgo-specific indirect call promotion function declared below is used by
97// the pgo-driven indirect call promotion and sample profile passes. It's a
98// wrapper around llvm::promoteCall, et al. that additionally computes !prof
99// metadata. We place it in a pgo namespace so it's not confused with the
100// generic utilities.
101namespace pgo {
102
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200103// Helper function that transforms CB (either an indirect-call instruction, or
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100104// an invoke instruction , to a conditional call to F. This is like:
105// if (Inst.CalledValue == F)
106// F(...);
107// else
108// Inst(...);
109// end
110// TotalCount is the profile count value that the instruction executes.
111// Count is the profile count value that F is the target function.
112// These two values are used to update the branch weight.
113// If \p AttachProfToDirectCall is true, a prof metadata is attached to the
114// new direct call to contain \p Count.
115// Returns the promoted direct call instruction.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200116CallBase &promoteIndirectCall(CallBase &CB, Function *F, uint64_t Count,
117 uint64_t TotalCount, bool AttachProfToDirectCall,
118 OptimizationRemarkEmitter *ORE);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100119} // namespace pgo
120
121/// Options for the frontend instrumentation based profiling pass.
122struct InstrProfOptions {
123 // Add the 'noredzone' attribute to added runtime library calls.
124 bool NoRedZone = false;
125
126 // Do counter register promotion
127 bool DoCounterPromotion = false;
128
Andrew Scull0372a572018-11-16 15:47:06 +0000129 // Use atomic profile counter increments.
130 bool Atomic = false;
131
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100132 // Use BFI to guide register promotion
133 bool UseBFIInPromotion = 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
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100141/// Insert frontend instrumentation based profiling. Parameter IsCS indicates if
142// this is the context senstive instrumentation.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100143ModulePass *createInstrProfilingLegacyPass(
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100144 const InstrProfOptions &Options = InstrProfOptions(), bool IsCS = false);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100145
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100146ModulePass *createInstrOrderFilePass();
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100147
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100148// Insert DataFlowSanitizer (dynamic data flow analysis) instrumentation
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200149ModulePass *createDataFlowSanitizerLegacyPassPass(
150 const std::vector<std::string> &ABIListFiles = std::vector<std::string>());
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100151
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100152// Options for sanitizer coverage instrumentation.
153struct SanitizerCoverageOptions {
154 enum Type {
155 SCK_None = 0,
156 SCK_Function,
157 SCK_BB,
158 SCK_Edge
159 } CoverageType = SCK_None;
160 bool IndirectCalls = false;
161 bool TraceBB = false;
162 bool TraceCmp = false;
163 bool TraceDiv = false;
164 bool TraceGep = false;
165 bool Use8bitCounters = false;
166 bool TracePC = false;
167 bool TracePCGuard = false;
168 bool Inline8bitCounters = false;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200169 bool InlineBoolFlag = false;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100170 bool PCTable = false;
171 bool NoPrune = false;
172 bool StackDepth = false;
173
174 SanitizerCoverageOptions() = default;
175};
176
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100177/// Calculate what to divide by to scale counts.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100178///
179/// Given the maximum count, calculate a divisor that will scale all the
180/// weights to strictly less than std::numeric_limits<uint32_t>::max().
181static inline uint64_t calculateCountScale(uint64_t MaxCount) {
182 return MaxCount < std::numeric_limits<uint32_t>::max()
183 ? 1
184 : MaxCount / std::numeric_limits<uint32_t>::max() + 1;
185}
186
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100187/// Scale an individual branch count.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100188///
189/// Scale a 64-bit weight down to 32-bits using \c Scale.
190///
191static inline uint32_t scaleBranchCount(uint64_t Count, uint64_t Scale) {
192 uint64_t Scaled = Count / Scale;
193 assert(Scaled <= std::numeric_limits<uint32_t>::max() && "overflow 32-bits");
194 return Scaled;
195}
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100196} // end namespace llvm
197
198#endif // LLVM_TRANSFORMS_INSTRUMENTATION_H