Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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 | namespace llvm { |
| 26 | |
| 27 | class FunctionPass; |
| 28 | class ModulePass; |
| 29 | class OptimizationRemarkEmitter; |
| 30 | |
| 31 | /// Instrumentation passes often insert conditional checks into entry blocks. |
| 32 | /// Call this function before splitting the entry block to move instructions |
| 33 | /// that must remain in the entry block up before the split point. Static |
| 34 | /// allocas and llvm.localescape calls, for example, must remain in the entry |
| 35 | /// block. |
| 36 | BasicBlock::iterator PrepareToSplitEntryBlock(BasicBlock &BB, |
| 37 | BasicBlock::iterator IP); |
| 38 | |
| 39 | // Insert GCOV profiling instrumentation |
| 40 | struct GCOVOptions { |
| 41 | static GCOVOptions getDefault(); |
| 42 | |
| 43 | // Specify whether to emit .gcno files. |
| 44 | bool EmitNotes; |
| 45 | |
| 46 | // Specify whether to modify the program to emit .gcda files when run. |
| 47 | bool EmitData; |
| 48 | |
| 49 | // A four-byte version string. The meaning of a version string is described in |
| 50 | // gcc's gcov-io.h |
| 51 | char Version[4]; |
| 52 | |
| 53 | // Emit a "cfg checksum" that follows the "line number checksum" of a |
| 54 | // function. This affects both .gcno and .gcda files. |
| 55 | bool UseCfgChecksum; |
| 56 | |
| 57 | // Add the 'noredzone' attribute to added runtime library calls. |
| 58 | bool NoRedZone; |
| 59 | |
| 60 | // Emit the name of the function in the .gcda files. This is redundant, as |
| 61 | // the function identifier can be used to find the name from the .gcno file. |
| 62 | bool FunctionNamesInData; |
| 63 | |
| 64 | // Emit the exit block immediately after the start block, rather than after |
| 65 | // all of the function body's blocks. |
| 66 | bool ExitBlockBeforeBody; |
| 67 | }; |
| 68 | |
| 69 | ModulePass *createGCOVProfilerPass(const GCOVOptions &Options = |
| 70 | GCOVOptions::getDefault()); |
| 71 | |
| 72 | // PGO Instrumention |
| 73 | ModulePass *createPGOInstrumentationGenLegacyPass(); |
| 74 | ModulePass * |
| 75 | createPGOInstrumentationUseLegacyPass(StringRef Filename = StringRef("")); |
| 76 | ModulePass *createPGOIndirectCallPromotionLegacyPass(bool InLTO = false, |
| 77 | bool SamplePGO = false); |
| 78 | FunctionPass *createPGOMemOPSizeOptLegacyPass(); |
| 79 | |
| 80 | // The pgo-specific indirect call promotion function declared below is used by |
| 81 | // the pgo-driven indirect call promotion and sample profile passes. It's a |
| 82 | // wrapper around llvm::promoteCall, et al. that additionally computes !prof |
| 83 | // metadata. We place it in a pgo namespace so it's not confused with the |
| 84 | // generic utilities. |
| 85 | namespace pgo { |
| 86 | |
| 87 | // Helper function that transforms Inst (either an indirect-call instruction, or |
| 88 | // an invoke instruction , to a conditional call to F. This is like: |
| 89 | // if (Inst.CalledValue == F) |
| 90 | // F(...); |
| 91 | // else |
| 92 | // Inst(...); |
| 93 | // end |
| 94 | // TotalCount is the profile count value that the instruction executes. |
| 95 | // Count is the profile count value that F is the target function. |
| 96 | // These two values are used to update the branch weight. |
| 97 | // If \p AttachProfToDirectCall is true, a prof metadata is attached to the |
| 98 | // new direct call to contain \p Count. |
| 99 | // Returns the promoted direct call instruction. |
| 100 | Instruction *promoteIndirectCall(Instruction *Inst, Function *F, uint64_t Count, |
| 101 | uint64_t TotalCount, |
| 102 | bool AttachProfToDirectCall, |
| 103 | OptimizationRemarkEmitter *ORE); |
| 104 | } // namespace pgo |
| 105 | |
| 106 | /// Options for the frontend instrumentation based profiling pass. |
| 107 | struct InstrProfOptions { |
| 108 | // Add the 'noredzone' attribute to added runtime library calls. |
| 109 | bool NoRedZone = false; |
| 110 | |
| 111 | // Do counter register promotion |
| 112 | bool DoCounterPromotion = false; |
| 113 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 114 | // Use atomic profile counter increments. |
| 115 | bool Atomic = false; |
| 116 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 117 | // Name of the profile file to use as output |
| 118 | std::string InstrProfileOutput; |
| 119 | |
| 120 | InstrProfOptions() = default; |
| 121 | }; |
| 122 | |
| 123 | /// Insert frontend instrumentation based profiling. |
| 124 | ModulePass *createInstrProfilingLegacyPass( |
| 125 | const InstrProfOptions &Options = InstrProfOptions()); |
| 126 | |
| 127 | // Insert AddressSanitizer (address sanity checking) instrumentation |
| 128 | FunctionPass *createAddressSanitizerFunctionPass(bool CompileKernel = false, |
| 129 | bool Recover = false, |
| 130 | bool UseAfterScope = false); |
| 131 | ModulePass *createAddressSanitizerModulePass(bool CompileKernel = false, |
| 132 | bool Recover = false, |
| 133 | bool UseGlobalsGC = true); |
| 134 | |
| 135 | // Insert MemorySanitizer instrumentation (detection of uninitialized reads) |
| 136 | FunctionPass *createMemorySanitizerPass(int TrackOrigins = 0, |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 137 | bool Recover = false, |
| 138 | bool EnableKmsan = false); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 139 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 140 | FunctionPass *createHWAddressSanitizerPass(bool CompileKernel = false, |
| 141 | bool Recover = false); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 142 | |
| 143 | // Insert ThreadSanitizer (race detection) instrumentation |
| 144 | FunctionPass *createThreadSanitizerPass(); |
| 145 | |
| 146 | // Insert DataFlowSanitizer (dynamic data flow analysis) instrumentation |
| 147 | ModulePass *createDataFlowSanitizerPass( |
| 148 | const std::vector<std::string> &ABIListFiles = std::vector<std::string>(), |
| 149 | void *(*getArgTLS)() = nullptr, void *(*getRetValTLS)() = nullptr); |
| 150 | |
| 151 | // Options for EfficiencySanitizer sub-tools. |
| 152 | struct EfficiencySanitizerOptions { |
| 153 | enum Type { |
| 154 | ESAN_None = 0, |
| 155 | ESAN_CacheFrag, |
| 156 | ESAN_WorkingSet, |
| 157 | } ToolType = ESAN_None; |
| 158 | |
| 159 | EfficiencySanitizerOptions() = default; |
| 160 | }; |
| 161 | |
| 162 | // Insert EfficiencySanitizer instrumentation. |
| 163 | ModulePass *createEfficiencySanitizerPass( |
| 164 | const EfficiencySanitizerOptions &Options = EfficiencySanitizerOptions()); |
| 165 | |
| 166 | // Options for sanitizer coverage instrumentation. |
| 167 | struct SanitizerCoverageOptions { |
| 168 | enum Type { |
| 169 | SCK_None = 0, |
| 170 | SCK_Function, |
| 171 | SCK_BB, |
| 172 | SCK_Edge |
| 173 | } CoverageType = SCK_None; |
| 174 | bool IndirectCalls = false; |
| 175 | bool TraceBB = false; |
| 176 | bool TraceCmp = false; |
| 177 | bool TraceDiv = false; |
| 178 | bool TraceGep = false; |
| 179 | bool Use8bitCounters = false; |
| 180 | bool TracePC = false; |
| 181 | bool TracePCGuard = false; |
| 182 | bool Inline8bitCounters = false; |
| 183 | bool PCTable = false; |
| 184 | bool NoPrune = false; |
| 185 | bool StackDepth = false; |
| 186 | |
| 187 | SanitizerCoverageOptions() = default; |
| 188 | }; |
| 189 | |
| 190 | // Insert SanitizerCoverage instrumentation. |
| 191 | ModulePass *createSanitizerCoverageModulePass( |
| 192 | const SanitizerCoverageOptions &Options = SanitizerCoverageOptions()); |
| 193 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 194 | /// Calculate what to divide by to scale counts. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 195 | /// |
| 196 | /// Given the maximum count, calculate a divisor that will scale all the |
| 197 | /// weights to strictly less than std::numeric_limits<uint32_t>::max(). |
| 198 | static inline uint64_t calculateCountScale(uint64_t MaxCount) { |
| 199 | return MaxCount < std::numeric_limits<uint32_t>::max() |
| 200 | ? 1 |
| 201 | : MaxCount / std::numeric_limits<uint32_t>::max() + 1; |
| 202 | } |
| 203 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 204 | /// Scale an individual branch count. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 205 | /// |
| 206 | /// Scale a 64-bit weight down to 32-bits using \c Scale. |
| 207 | /// |
| 208 | static inline uint32_t scaleBranchCount(uint64_t Count, uint64_t Scale) { |
| 209 | uint64_t Scaled = Count / Scale; |
| 210 | assert(Scaled <= std::numeric_limits<uint32_t>::max() && "overflow 32-bits"); |
| 211 | return Scaled; |
| 212 | } |
| 213 | |
| 214 | } // end namespace llvm |
| 215 | |
| 216 | #endif // LLVM_TRANSFORMS_INSTRUMENTATION_H |