1 //===- Transforms/Instrumentation.h - Instrumentation passes ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines constructor functions for instrumentation passes.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_TRANSFORMS_INSTRUMENTATION_H
15 #define LLVM_TRANSFORMS_INSTRUMENTATION_H
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/IR/BasicBlock.h"
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;
28 inline void *getDFSanRetValTLSPtrForJIT() {
29 extern __thread __attribute__((tls_model("initial-exec")))
30 void *__dfsan_retval_tls;
31 return (void *)&__dfsan_retval_tls;
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
44 BasicBlock::iterator PrepareToSplitEntryBlock(BasicBlock &BB,
45 BasicBlock::iterator IP);
50 // Insert GCOV profiling instrumentation
52 static GCOVOptions getDefault();
54 // Specify whether to emit .gcno files.
57 // Specify whether to modify the program to emit .gcda files when run.
60 // A four-byte version string. The meaning of a version string is described in
64 // Emit a "cfg checksum" that follows the "line number checksum" of a
65 // function. This affects both .gcno and .gcda files.
68 // Add the 'noredzone' attribute to added runtime library calls.
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;
75 // Emit the exit block immediately after the start block, rather than after
76 // all of the function body's blocks.
77 bool ExitBlockBeforeBody;
79 ModulePass *createGCOVProfilerPass(const GCOVOptions &Options =
80 GCOVOptions::getDefault());
83 ModulePass *createPGOInstrumentationGenPass();
85 createPGOInstrumentationUsePass(StringRef Filename = StringRef(""));
87 /// Options for the frontend instrumentation based profiling pass.
88 struct InstrProfOptions {
89 InstrProfOptions() : NoRedZone(false) {}
91 // Add the 'noredzone' attribute to added runtime library calls.
94 // Name of the profile file to use as output
95 std::string InstrProfileOutput;
98 /// Insert frontend instrumentation based profiling.
99 ModulePass *createInstrProfilingPass(
100 const InstrProfOptions &Options = InstrProfOptions());
102 // Insert AddressSanitizer (address sanity checking) instrumentation
103 FunctionPass *createAddressSanitizerFunctionPass(bool CompileKernel = false,
104 bool Recover = false);
105 ModulePass *createAddressSanitizerModulePass(bool CompileKernel = false,
106 bool Recover = false);
108 // Insert MemorySanitizer instrumentation (detection of uninitialized reads)
109 FunctionPass *createMemorySanitizerPass(int TrackOrigins = 0);
111 // Insert ThreadSanitizer (race detection) instrumentation
112 FunctionPass *createThreadSanitizerPass();
114 // Insert DataFlowSanitizer (dynamic data flow analysis) instrumentation
115 ModulePass *createDataFlowSanitizerPass(
116 const std::vector<std::string> &ABIListFiles = std::vector<std::string>(),
117 void *(*getArgTLS)() = nullptr, void *(*getRetValTLS)() = nullptr);
119 // Options for sanitizer coverage instrumentation.
120 struct SanitizerCoverageOptions {
121 SanitizerCoverageOptions()
122 : CoverageType(SCK_None), IndirectCalls(false), TraceBB(false),
123 TraceCmp(false), Use8bitCounters(false) {}
134 bool Use8bitCounters;
137 // Insert SanitizerCoverage instrumentation.
138 ModulePass *createSanitizerCoverageModulePass(
139 const SanitizerCoverageOptions &Options = SanitizerCoverageOptions());
141 #if defined(__GNUC__) && defined(__linux__) && !defined(ANDROID)
142 inline ModulePass *createDataFlowSanitizerPassForJIT(
143 const std::vector<std::string> &ABIListFiles = std::vector<std::string>()) {
144 return createDataFlowSanitizerPass(ABIListFiles, getDFSanArgTLSPtrForJIT,
145 getDFSanRetValTLSPtrForJIT);
149 // BoundsChecking - This pass instruments the code to perform run-time bounds
150 // checking on loads, stores, and other memory intrinsics.
151 FunctionPass *createBoundsCheckingPass();
153 /// \brief This pass splits the stack into a safe stack and an unsafe stack to
154 /// protect against stack-based overflow vulnerabilities.
155 FunctionPass *createSafeStackPass(const TargetMachine *TM = nullptr);
157 /// \brief Calculate what to divide by to scale counts.
159 /// Given the maximum count, calculate a divisor that will scale all the
160 /// weights to strictly less than UINT32_MAX.
161 static inline uint64_t calculateCountScale(uint64_t MaxCount) {
162 return MaxCount < UINT32_MAX ? 1 : MaxCount / UINT32_MAX + 1;
165 /// \brief Scale an individual branch count.
167 /// Scale a 64-bit weight down to 32-bits using \c Scale.
169 static inline uint32_t scaleBranchCount(uint64_t Count, uint64_t Scale) {
170 uint64_t Scaled = Count / Scale;
171 assert(Scaled <= UINT32_MAX && "overflow 32-bits");
175 } // End llvm namespace