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 <cassert>
19 #include <cstdint>
20 #include <limits>
21 #include <string>
22 #include <vector>
23 
24 namespace llvm {
25 
26 class Triple;
27 class FunctionPass;
28 class ModulePass;
29 class OptimizationRemarkEmitter;
30 class Comdat;
31 class CallBase;
32 
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.
38 BasicBlock::iterator PrepareToSplitEntryBlock(BasicBlock &BB,
39                                               BasicBlock::iterator IP);
40 
41 // Create a constant for Str so that we can pass it to the run-time lib.
42 GlobalVariable *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.
49 Comdat *GetOrCreateFunctionComdat(Function &F, Triple &T,
50                                   const std::string &ModuleId);
51 
52 // Insert GCOV profiling instrumentation
53 struct 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 
66   // Add the 'noredzone' attribute to added runtime library calls.
67   bool NoRedZone;
68 
69   // Use atomic profile counter increments.
70   bool Atomic = false;
71 
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;
77 };
78 
79 ModulePass *createGCOVProfilerPass(const GCOVOptions &Options =
80                                    GCOVOptions::getDefault());
81 
82 // PGO Instrumention. Parameter IsCS indicates if this is the context senstive
83 // instrumentation.
84 ModulePass *createPGOInstrumentationGenLegacyPass(bool IsCS = false);
85 ModulePass *
86 createPGOInstrumentationUseLegacyPass(StringRef Filename = StringRef(""),
87                                       bool IsCS = false);
88 ModulePass *createPGOInstrumentationGenCreateVarLegacyPass(
89     StringRef CSInstrName = StringRef(""));
90 ModulePass *createPGOIndirectCallPromotionLegacyPass(bool InLTO = false,
91                                                      bool SamplePGO = false);
92 FunctionPass *createPGOMemOPSizeOptLegacyPass();
93 
94 ModulePass *createCGProfileLegacyPass();
95 
96 // 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.
101 namespace pgo {
102 
103 // Helper function that transforms CB (either an indirect-call instruction, or
104 // 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.
116 CallBase &promoteIndirectCall(CallBase &CB, Function *F, uint64_t Count,
117                               uint64_t TotalCount, bool AttachProfToDirectCall,
118                               OptimizationRemarkEmitter *ORE);
119 } // namespace pgo
120 
121 /// Options for the frontend instrumentation based profiling pass.
122 struct 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 
129   // Use atomic profile counter increments.
130   bool Atomic = false;
131 
132   // Use BFI to guide register promotion
133   bool UseBFIInPromotion = false;
134 
135   // 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. Parameter IsCS indicates if
142 // this is the context senstive instrumentation.
143 ModulePass *createInstrProfilingLegacyPass(
144     const InstrProfOptions &Options = InstrProfOptions(), bool IsCS = false);
145 
146 ModulePass *createInstrOrderFilePass();
147 
148 // Insert DataFlowSanitizer (dynamic data flow analysis) instrumentation
149 ModulePass *createDataFlowSanitizerLegacyPassPass(
150     const std::vector<std::string> &ABIListFiles = std::vector<std::string>());
151 
152 // Options for sanitizer coverage instrumentation.
153 struct 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;
169   bool InlineBoolFlag = false;
170   bool PCTable = false;
171   bool NoPrune = false;
172   bool StackDepth = false;
173 
174   SanitizerCoverageOptions() = default;
175 };
176 
177 /// Calculate what to divide by to scale counts.
178 ///
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().
181 static 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 
187 /// Scale an individual branch count.
188 ///
189 /// Scale a 64-bit weight down to 32-bits using \c Scale.
190 ///
191 static 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 }
196 } // end namespace llvm
197 
198 #endif // LLVM_TRANSFORMS_INSTRUMENTATION_H
199