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