1 //===- Transforms/Instrumentation.h - Instrumentation passes ----*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines constructor functions for instrumentation passes.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_TRANSFORMS_INSTRUMENTATION_H
14 #define LLVM_TRANSFORMS_INSTRUMENTATION_H
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/IR/BasicBlock.h"
29 class OptimizationRemarkEmitter
;
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
37 BasicBlock::iterator
PrepareToSplitEntryBlock(BasicBlock
&BB
,
38 BasicBlock::iterator IP
);
40 // Create a constant for Str so that we can pass it to the run-time lib.
41 GlobalVariable
*createPrivateGlobalForString(Module
&M
, StringRef Str
,
43 const char *NamePrefix
= "");
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.
48 Comdat
*GetOrCreateFunctionComdat(Function
&F
, Triple
&T
,
49 const std::string
&ModuleId
);
51 // Insert GCOV profiling instrumentation
53 static GCOVOptions
getDefault();
55 // Specify whether to emit .gcno files.
58 // Specify whether to modify the program to emit .gcda files when run.
61 // A four-byte version string. The meaning of a version string is described in
65 // Emit a "cfg checksum" that follows the "line number checksum" of a
66 // function. This affects both .gcno and .gcda files.
69 // Add the 'noredzone' attribute to added runtime library calls.
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
;
76 // Emit the exit block immediately after the start block, rather than after
77 // all of the function body's blocks.
78 bool ExitBlockBeforeBody
;
80 // Regexes separated by a semi-colon to filter the files to instrument.
83 // Regexes separated by a semi-colon to filter the files to not instrument.
87 ModulePass
*createGCOVProfilerPass(const GCOVOptions
&Options
=
88 GCOVOptions::getDefault());
90 // PGO Instrumention. Parameter IsCS indicates if this is the context senstive
92 ModulePass
*createPGOInstrumentationGenLegacyPass(bool IsCS
= false);
94 createPGOInstrumentationUseLegacyPass(StringRef Filename
= StringRef(""),
96 ModulePass
*createPGOInstrumentationGenCreateVarLegacyPass(
97 StringRef CSInstrName
= StringRef(""));
98 ModulePass
*createPGOIndirectCallPromotionLegacyPass(bool InLTO
= false,
99 bool SamplePGO
= false);
100 FunctionPass
*createPGOMemOPSizeOptLegacyPass();
102 // The pgo-specific indirect call promotion function declared below is used by
103 // the pgo-driven indirect call promotion and sample profile passes. It's a
104 // wrapper around llvm::promoteCall, et al. that additionally computes !prof
105 // metadata. We place it in a pgo namespace so it's not confused with the
106 // generic utilities.
109 // Helper function that transforms Inst (either an indirect-call instruction, or
110 // an invoke instruction , to a conditional call to F. This is like:
111 // if (Inst.CalledValue == F)
116 // TotalCount is the profile count value that the instruction executes.
117 // Count is the profile count value that F is the target function.
118 // These two values are used to update the branch weight.
119 // If \p AttachProfToDirectCall is true, a prof metadata is attached to the
120 // new direct call to contain \p Count.
121 // Returns the promoted direct call instruction.
122 Instruction
*promoteIndirectCall(Instruction
*Inst
, Function
*F
, uint64_t Count
,
124 bool AttachProfToDirectCall
,
125 OptimizationRemarkEmitter
*ORE
);
128 /// Options for the frontend instrumentation based profiling pass.
129 struct InstrProfOptions
{
130 // Add the 'noredzone' attribute to added runtime library calls.
131 bool NoRedZone
= false;
133 // Do counter register promotion
134 bool DoCounterPromotion
= false;
136 // Use atomic profile counter increments.
139 // Use BFI to guide register promotion
140 bool UseBFIInPromotion
= false;
142 // Name of the profile file to use as output
143 std::string InstrProfileOutput
;
145 InstrProfOptions() = default;
148 /// Insert frontend instrumentation based profiling. Parameter IsCS indicates if
149 // this is the context senstive instrumentation.
150 ModulePass
*createInstrProfilingLegacyPass(
151 const InstrProfOptions
&Options
= InstrProfOptions(), bool IsCS
= false);
153 ModulePass
*createInstrOrderFilePass();
155 // Insert DataFlowSanitizer (dynamic data flow analysis) instrumentation
156 ModulePass
*createDataFlowSanitizerPass(
157 const std::vector
<std::string
> &ABIListFiles
= std::vector
<std::string
>(),
158 void *(*getArgTLS
)() = nullptr, void *(*getRetValTLS
)() = nullptr);
160 // Options for sanitizer coverage instrumentation.
161 struct SanitizerCoverageOptions
{
167 } CoverageType
= SCK_None
;
168 bool IndirectCalls
= false;
169 bool TraceBB
= false;
170 bool TraceCmp
= false;
171 bool TraceDiv
= false;
172 bool TraceGep
= false;
173 bool Use8bitCounters
= false;
174 bool TracePC
= false;
175 bool TracePCGuard
= false;
176 bool Inline8bitCounters
= false;
177 bool PCTable
= false;
178 bool NoPrune
= false;
179 bool StackDepth
= false;
181 SanitizerCoverageOptions() = default;
184 /// Calculate what to divide by to scale counts.
186 /// Given the maximum count, calculate a divisor that will scale all the
187 /// weights to strictly less than std::numeric_limits<uint32_t>::max().
188 static inline uint64_t calculateCountScale(uint64_t MaxCount
) {
189 return MaxCount
< std::numeric_limits
<uint32_t>::max()
191 : MaxCount
/ std::numeric_limits
<uint32_t>::max() + 1;
194 /// Scale an individual branch count.
196 /// Scale a 64-bit weight down to 32-bits using \c Scale.
198 static inline uint32_t scaleBranchCount(uint64_t Count
, uint64_t Scale
) {
199 uint64_t Scaled
= Count
/ Scale
;
200 assert(Scaled
<= std::numeric_limits
<uint32_t>::max() && "overflow 32-bits");
203 } // end namespace llvm
205 #endif // LLVM_TRANSFORMS_INSTRUMENTATION_H