OSDN Git Service

Add call branch annotation for ICP promoted direct call in SamplePGO mode.
[android-x86/external-llvm.git] / include / llvm / Transforms / Instrumentation.h
1 //===- 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
25 #if defined(__GNUC__) && defined(__linux__) && !defined(ANDROID)
26 inline void *getDFSanArgTLSPtrForJIT() {
27   extern __thread __attribute__((tls_model("initial-exec")))
28     void *__dfsan_arg_tls;
29   return (void *)&__dfsan_arg_tls;
30 }
31
32 inline void *getDFSanRetValTLSPtrForJIT() {
33   extern __thread __attribute__((tls_model("initial-exec")))
34     void *__dfsan_retval_tls;
35   return (void *)&__dfsan_retval_tls;
36 }
37 #endif
38
39 namespace llvm {
40
41 class FunctionPass;
42 class ModulePass;
43
44 /// Instrumentation passes often insert conditional checks into entry blocks.
45 /// Call this function before splitting the entry block to move instructions
46 /// that must remain in the entry block up before the split point. Static
47 /// allocas and llvm.localescape calls, for example, must remain in the entry
48 /// block.
49 BasicBlock::iterator PrepareToSplitEntryBlock(BasicBlock &BB,
50                                               BasicBlock::iterator IP);
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   // Emit a "cfg checksum" that follows the "line number checksum" of a
67   // function. This affects both .gcno and .gcda files.
68   bool UseCfgChecksum;
69
70   // Add the 'noredzone' attribute to added runtime library calls.
71   bool NoRedZone;
72
73   // Emit the name of the function in the .gcda files. This is redundant, as
74   // the function identifier can be used to find the name from the .gcno file.
75   bool FunctionNamesInData;
76
77   // Emit the exit block immediately after the start block, rather than after
78   // all of the function body's blocks.
79   bool ExitBlockBeforeBody;
80 };
81
82 ModulePass *createGCOVProfilerPass(const GCOVOptions &Options =
83                                    GCOVOptions::getDefault());
84
85 // PGO Instrumention
86 ModulePass *createPGOInstrumentationGenLegacyPass();
87 ModulePass *
88 createPGOInstrumentationUseLegacyPass(StringRef Filename = StringRef(""));
89 ModulePass *createPGOIndirectCallPromotionLegacyPass(bool InLTO = false,
90                                                      bool SamplePGO = false);
91
92 // Helper function to check if it is legal to promote indirect call \p Inst
93 // to a direct call of function \p F. Stores the reason in \p Reason.
94 bool isLegalToPromote(Instruction *Inst, Function *F, const char **Reason);
95
96 // Helper function that transforms Inst (either an indirect-call instruction, or
97 // an invoke instruction , to a conditional call to F. This is like:
98 //     if (Inst.CalledValue == F)
99 //        F(...);
100 //     else
101 //        Inst(...);
102 //     end
103 // TotalCount is the profile count value that the instruction executes.
104 // Count is the profile count value that F is the target function.
105 // These two values are used to update the branch weight.
106 // If \p AttachProfToDirectCall is true, a prof metadata is attached to the
107 // new direct call to contain \p Count.
108 // Returns the promoted direct call instruction.
109 Instruction *promoteIndirectCall(Instruction *Inst, Function *F, uint64_t Count,
110                                  uint64_t TotalCount,
111                                  bool AttachProfToDirectCall);
112
113 /// Options for the frontend instrumentation based profiling pass.
114 struct InstrProfOptions {
115   // Add the 'noredzone' attribute to added runtime library calls.
116   bool NoRedZone = false;
117
118   // Name of the profile file to use as output
119   std::string InstrProfileOutput;
120
121   InstrProfOptions() = default;
122 };
123
124 /// Insert frontend instrumentation based profiling.
125 ModulePass *createInstrProfilingLegacyPass(
126     const InstrProfOptions &Options = InstrProfOptions());
127
128 // Insert AddressSanitizer (address sanity checking) instrumentation
129 FunctionPass *createAddressSanitizerFunctionPass(bool CompileKernel = false,
130                                                  bool Recover = false,
131                                                  bool UseAfterScope = false);
132 ModulePass *createAddressSanitizerModulePass(bool CompileKernel = false,
133                                              bool Recover = false);
134
135 // Insert MemorySanitizer instrumentation (detection of uninitialized reads)
136 FunctionPass *createMemorySanitizerPass(int TrackOrigins = 0,
137                                         bool Recover = false);
138
139 // Insert ThreadSanitizer (race detection) instrumentation
140 FunctionPass *createThreadSanitizerPass();
141
142 // Insert DataFlowSanitizer (dynamic data flow analysis) instrumentation
143 ModulePass *createDataFlowSanitizerPass(
144     const std::vector<std::string> &ABIListFiles = std::vector<std::string>(),
145     void *(*getArgTLS)() = nullptr, void *(*getRetValTLS)() = nullptr);
146
147 // Options for EfficiencySanitizer sub-tools.
148 struct EfficiencySanitizerOptions {
149   enum Type {
150     ESAN_None = 0,
151     ESAN_CacheFrag,
152     ESAN_WorkingSet,
153   } ToolType = ESAN_None;
154
155   EfficiencySanitizerOptions() = default;
156 };
157
158 // Insert EfficiencySanitizer instrumentation.
159 ModulePass *createEfficiencySanitizerPass(
160     const EfficiencySanitizerOptions &Options = EfficiencySanitizerOptions());
161
162 // Options for sanitizer coverage instrumentation.
163 struct SanitizerCoverageOptions {
164   enum Type {
165     SCK_None = 0,
166     SCK_Function,
167     SCK_BB,
168     SCK_Edge
169   } CoverageType = SCK_None;
170   bool IndirectCalls = false;
171   bool TraceBB = false;
172   bool TraceCmp = false;
173   bool TraceDiv = false;
174   bool TraceGep = false;
175   bool Use8bitCounters = false;
176   bool TracePC = false;
177   bool TracePCGuard = false;
178
179   SanitizerCoverageOptions() = default;
180 };
181
182 // Insert SanitizerCoverage instrumentation.
183 ModulePass *createSanitizerCoverageModulePass(
184     const SanitizerCoverageOptions &Options = SanitizerCoverageOptions());
185
186 #if defined(__GNUC__) && defined(__linux__) && !defined(ANDROID)
187 inline ModulePass *createDataFlowSanitizerPassForJIT(
188     const std::vector<std::string> &ABIListFiles = std::vector<std::string>()) {
189   return createDataFlowSanitizerPass(ABIListFiles, getDFSanArgTLSPtrForJIT,
190                                      getDFSanRetValTLSPtrForJIT);
191 }
192 #endif
193
194 // BoundsChecking - This pass instruments the code to perform run-time bounds
195 // checking on loads, stores, and other memory intrinsics.
196 FunctionPass *createBoundsCheckingPass();
197
198 /// \brief Calculate what to divide by to scale counts.
199 ///
200 /// Given the maximum count, calculate a divisor that will scale all the
201 /// weights to strictly less than std::numeric_limits<uint32_t>::max().
202 static inline uint64_t calculateCountScale(uint64_t MaxCount) {
203   return MaxCount < std::numeric_limits<uint32_t>::max()
204              ? 1
205              : MaxCount / std::numeric_limits<uint32_t>::max() + 1;
206 }
207
208 /// \brief Scale an individual branch count.
209 ///
210 /// Scale a 64-bit weight down to 32-bits using \c Scale.
211 ///
212 static inline uint32_t scaleBranchCount(uint64_t Count, uint64_t Scale) {
213   uint64_t Scaled = Count / Scale;
214   assert(Scaled <= std::numeric_limits<uint32_t>::max() && "overflow 32-bits");
215   return Scaled;
216 }
217
218 } // end namespace llvm
219
220 #endif // LLVM_TRANSFORMS_INSTRUMENTATION_H