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