OSDN Git Service

24e4247e847e63ace4fd6c541f87ceee2d2e8562
[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 <vector>
20
21 #if defined(__GNUC__) && defined(__linux__) && !defined(ANDROID)
22 inline void *getDFSanArgTLSPtrForJIT() {
23   extern __thread __attribute__((tls_model("initial-exec")))
24     void *__dfsan_arg_tls;
25   return (void *)&__dfsan_arg_tls;
26 }
27
28 inline void *getDFSanRetValTLSPtrForJIT() {
29   extern __thread __attribute__((tls_model("initial-exec")))
30     void *__dfsan_retval_tls;
31   return (void *)&__dfsan_retval_tls;
32 }
33 #endif
34
35 namespace llvm {
36
37 class TargetMachine;
38
39 /// Instrumentation passes often insert conditional checks into entry blocks.
40 /// Call this function before splitting the entry block to move instructions
41 /// that must remain in the entry block up before the split point. Static
42 /// allocas and llvm.localescape calls, for example, must remain in the entry
43 /// block.
44 BasicBlock::iterator PrepareToSplitEntryBlock(BasicBlock &BB,
45                                               BasicBlock::iterator IP);
46
47 class ModulePass;
48 class FunctionPass;
49
50 // Insert GCOV profiling instrumentation
51 struct GCOVOptions {
52   static GCOVOptions getDefault();
53
54   // Specify whether to emit .gcno files.
55   bool EmitNotes;
56
57   // Specify whether to modify the program to emit .gcda files when run.
58   bool EmitData;
59
60   // A four-byte version string. The meaning of a version string is described in
61   // gcc's gcov-io.h
62   char Version[4];
63
64   // Emit a "cfg checksum" that follows the "line number checksum" of a
65   // function. This affects both .gcno and .gcda files.
66   bool UseCfgChecksum;
67
68   // Add the 'noredzone' attribute to added runtime library calls.
69   bool NoRedZone;
70
71   // Emit the name of the function in the .gcda files. This is redundant, as
72   // the function identifier can be used to find the name from the .gcno file.
73   bool FunctionNamesInData;
74
75   // Emit the exit block immediately after the start block, rather than after
76   // all of the function body's blocks.
77   bool ExitBlockBeforeBody;
78 };
79 ModulePass *createGCOVProfilerPass(const GCOVOptions &Options =
80                                    GCOVOptions::getDefault());
81
82 // PGO Instrumention
83 ModulePass *createPGOInstrumentationGenLegacyPass();
84 ModulePass *
85 createPGOInstrumentationUseLegacyPass(StringRef Filename = StringRef(""));
86 ModulePass *createPGOIndirectCallPromotionPass(bool InLTO = false);
87
88 /// Options for the frontend instrumentation based profiling pass.
89 struct InstrProfOptions {
90   InstrProfOptions() : NoRedZone(false) {}
91
92   // Add the 'noredzone' attribute to added runtime library calls.
93   bool NoRedZone;
94
95   // Name of the profile file to use as output
96   std::string InstrProfileOutput;
97 };
98
99 /// Insert frontend instrumentation based profiling.
100 ModulePass *createInstrProfilingLegacyPass(
101     const InstrProfOptions &Options = InstrProfOptions());
102
103 // Insert AddressSanitizer (address sanity checking) instrumentation
104 FunctionPass *createAddressSanitizerFunctionPass(bool CompileKernel = false,
105                                                  bool Recover = false);
106 ModulePass *createAddressSanitizerModulePass(bool CompileKernel = false,
107                                              bool Recover = false);
108
109 // Insert MemorySanitizer instrumentation (detection of uninitialized reads)
110 FunctionPass *createMemorySanitizerPass(int TrackOrigins = 0);
111
112 // Insert ThreadSanitizer (race detection) instrumentation
113 FunctionPass *createThreadSanitizerPass();
114
115 // Insert DataFlowSanitizer (dynamic data flow analysis) instrumentation
116 ModulePass *createDataFlowSanitizerPass(
117     const std::vector<std::string> &ABIListFiles = std::vector<std::string>(),
118     void *(*getArgTLS)() = nullptr, void *(*getRetValTLS)() = nullptr);
119
120 // Options for EfficiencySanitizer sub-tools.
121 struct EfficiencySanitizerOptions {
122   EfficiencySanitizerOptions() : ToolType(ESAN_None) {}
123   enum Type {
124     ESAN_None = 0,
125     ESAN_CacheFrag,
126   } ToolType;
127 };
128
129 // Insert EfficiencySanitizer instrumentation.
130 FunctionPass *createEfficiencySanitizerPass(
131     const EfficiencySanitizerOptions &Options = EfficiencySanitizerOptions());
132
133 // Options for sanitizer coverage instrumentation.
134 struct SanitizerCoverageOptions {
135   SanitizerCoverageOptions()
136       : CoverageType(SCK_None), IndirectCalls(false), TraceBB(false),
137         TraceCmp(false), Use8bitCounters(false), TracePC(false) {}
138
139   enum Type {
140     SCK_None = 0,
141     SCK_Function,
142     SCK_BB,
143     SCK_Edge
144   } CoverageType;
145   bool IndirectCalls;
146   bool TraceBB;
147   bool TraceCmp;
148   bool Use8bitCounters;
149   bool TracePC;
150 };
151
152 // Insert SanitizerCoverage instrumentation.
153 ModulePass *createSanitizerCoverageModulePass(
154     const SanitizerCoverageOptions &Options = SanitizerCoverageOptions());
155
156 #if defined(__GNUC__) && defined(__linux__) && !defined(ANDROID)
157 inline ModulePass *createDataFlowSanitizerPassForJIT(
158     const std::vector<std::string> &ABIListFiles = std::vector<std::string>()) {
159   return createDataFlowSanitizerPass(ABIListFiles, getDFSanArgTLSPtrForJIT,
160                                      getDFSanRetValTLSPtrForJIT);
161 }
162 #endif
163
164 // BoundsChecking - This pass instruments the code to perform run-time bounds
165 // checking on loads, stores, and other memory intrinsics.
166 FunctionPass *createBoundsCheckingPass();
167
168 /// \brief Calculate what to divide by to scale counts.
169 ///
170 /// Given the maximum count, calculate a divisor that will scale all the
171 /// weights to strictly less than UINT32_MAX.
172 static inline uint64_t calculateCountScale(uint64_t MaxCount) {
173   return MaxCount < UINT32_MAX ? 1 : MaxCount / UINT32_MAX + 1;
174 }
175
176 /// \brief Scale an individual branch count.
177 ///
178 /// Scale a 64-bit weight down to 32-bits using \c Scale.
179 ///
180 static inline uint32_t scaleBranchCount(uint64_t Count, uint64_t Scale) {
181   uint64_t Scaled = Count / Scale;
182   assert(Scaled <= UINT32_MAX && "overflow 32-bits");
183   return Scaled;
184 }
185
186 } // End llvm namespace
187
188 #endif