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 
51 // Insert GCOV profiling instrumentation
52 struct 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   // Add the 'noredzone' attribute to added runtime library calls.
66   bool NoRedZone;
67 
68   // Use atomic profile counter increments.
69   bool Atomic = false;
70 
71   // Regexes separated by a semi-colon to filter the files to instrument.
72   std::string Filter;
73 
74   // Regexes separated by a semi-colon to filter the files to not instrument.
75   std::string Exclude;
76 };
77 
78 ModulePass *createGCOVProfilerPass(const GCOVOptions &Options =
79                                    GCOVOptions::getDefault());
80 
81 // PGO Instrumention. Parameter IsCS indicates if this is the context senstive
82 // instrumentation.
83 ModulePass *createPGOInstrumentationGenLegacyPass(bool IsCS = false);
84 ModulePass *
85 createPGOInstrumentationUseLegacyPass(StringRef Filename = StringRef(""),
86                                       bool IsCS = false);
87 ModulePass *createPGOInstrumentationGenCreateVarLegacyPass(
88     StringRef CSInstrName = StringRef(""));
89 ModulePass *createPGOIndirectCallPromotionLegacyPass(bool InLTO = false,
90                                                      bool SamplePGO = false);
91 FunctionPass *createPGOMemOPSizeOptLegacyPass();
92 
93 ModulePass *createCGProfileLegacyPass();
94 
95 // The pgo-specific indirect call promotion function declared below is used by
96 // the pgo-driven indirect call promotion and sample profile passes. It's a
97 // wrapper around llvm::promoteCall, et al. that additionally computes !prof
98 // metadata. We place it in a pgo namespace so it's not confused with the
99 // generic utilities.
100 namespace pgo {
101 
102 // Helper function that transforms CB (either an indirect-call instruction, or
103 // an invoke instruction , to a conditional call to F. This is like:
104 //     if (Inst.CalledValue == F)
105 //        F(...);
106 //     else
107 //        Inst(...);
108 //     end
109 // TotalCount is the profile count value that the instruction executes.
110 // Count is the profile count value that F is the target function.
111 // These two values are used to update the branch weight.
112 // If \p AttachProfToDirectCall is true, a prof metadata is attached to the
113 // new direct call to contain \p Count.
114 // Returns the promoted direct call instruction.
115 CallBase &promoteIndirectCall(CallBase &CB, Function *F, uint64_t Count,
116                               uint64_t TotalCount, bool AttachProfToDirectCall,
117                               OptimizationRemarkEmitter *ORE);
118 } // namespace pgo
119 
120 /// Options for the frontend instrumentation based profiling pass.
121 struct InstrProfOptions {
122   // Add the 'noredzone' attribute to added runtime library calls.
123   bool NoRedZone = false;
124 
125   // Do counter register promotion
126   bool DoCounterPromotion = false;
127 
128   // Use atomic profile counter increments.
129   bool Atomic = false;
130 
131   // Use BFI to guide register promotion
132   bool UseBFIInPromotion = false;
133 
134   // Name of the profile file to use as output
135   std::string InstrProfileOutput;
136 
137   InstrProfOptions() = default;
138 };
139 
140 /// Insert frontend instrumentation based profiling. Parameter IsCS indicates if
141 // this is the context senstive instrumentation.
142 ModulePass *createInstrProfilingLegacyPass(
143     const InstrProfOptions &Options = InstrProfOptions(), bool IsCS = false);
144 
145 ModulePass *createInstrOrderFilePass();
146 
147 // Insert DataFlowSanitizer (dynamic data flow analysis) instrumentation
148 ModulePass *createDataFlowSanitizerLegacyPassPass(
149     const std::vector<std::string> &ABIListFiles = std::vector<std::string>());
150 
151 // Options for sanitizer coverage instrumentation.
152 struct SanitizerCoverageOptions {
153   enum Type {
154     SCK_None = 0,
155     SCK_Function,
156     SCK_BB,
157     SCK_Edge
158   } CoverageType = SCK_None;
159   bool IndirectCalls = false;
160   bool TraceBB = false;
161   bool TraceCmp = false;
162   bool TraceDiv = false;
163   bool TraceGep = false;
164   bool Use8bitCounters = false;
165   bool TracePC = false;
166   bool TracePCGuard = false;
167   bool Inline8bitCounters = false;
168   bool InlineBoolFlag = false;
169   bool PCTable = false;
170   bool NoPrune = false;
171   bool StackDepth = false;
172 
173   SanitizerCoverageOptions() = default;
174 };
175 
176 /// Calculate what to divide by to scale counts.
177 ///
178 /// Given the maximum count, calculate a divisor that will scale all the
179 /// weights to strictly less than std::numeric_limits<uint32_t>::max().
180 static inline uint64_t calculateCountScale(uint64_t MaxCount) {
181   return MaxCount < std::numeric_limits<uint32_t>::max()
182              ? 1
183              : MaxCount / std::numeric_limits<uint32_t>::max() + 1;
184 }
185 
186 /// Scale an individual branch count.
187 ///
188 /// Scale a 64-bit weight down to 32-bits using \c Scale.
189 ///
190 static inline uint32_t scaleBranchCount(uint64_t Count, uint64_t Scale) {
191   uint64_t Scaled = Count / Scale;
192   assert(Scaled <= std::numeric_limits<uint32_t>::max() && "overflow 32-bits");
193   return Scaled;
194 }
195 } // end namespace llvm
196 
197 #endif // LLVM_TRANSFORMS_INSTRUMENTATION_H
198