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 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 
    114   // Name of the profile file to use as output
    115   std::string InstrProfileOutput;
    116 
    117   InstrProfOptions() = default;
    118 };
    119 
    120 /// Insert frontend instrumentation based profiling.
    121 ModulePass *createInstrProfilingLegacyPass(
    122     const InstrProfOptions &Options = InstrProfOptions());
    123 
    124 // Insert AddressSanitizer (address sanity checking) instrumentation
    125 FunctionPass *createAddressSanitizerFunctionPass(bool CompileKernel = false,
    126                                                  bool Recover = false,
    127                                                  bool UseAfterScope = false);
    128 ModulePass *createAddressSanitizerModulePass(bool CompileKernel = false,
    129                                              bool Recover = false,
    130                                              bool UseGlobalsGC = true);
    131 
    132 // Insert MemorySanitizer instrumentation (detection of uninitialized reads)
    133 FunctionPass *createMemorySanitizerPass(int TrackOrigins = 0,
    134                                         bool Recover = false);
    135 
    136 FunctionPass *createHWAddressSanitizerPass(bool CompileKernel = false,
    137                                            bool Recover = false);
    138 
    139 // Insert ThreadSanitizer (race detection) instrumentation
    140 FunctionPass *createThreadSanitizerPass();
    141 
    142 // Insert DataFlowSanitizer (dynamic data flow analysis) instrumentation
    143 ModulePass *createDataFlowSanitizerPass(
    144     const std::vector<std::string> &ABIListFiles = std::vector<std::string>(),
    145     void *(*getArgTLS)() = nullptr, void *(*getRetValTLS)() = nullptr);
    146 
    147 // Options for EfficiencySanitizer sub-tools.
    148 struct EfficiencySanitizerOptions {
    149   enum Type {
    150     ESAN_None = 0,
    151     ESAN_CacheFrag,
    152     ESAN_WorkingSet,
    153   } ToolType = ESAN_None;
    154 
    155   EfficiencySanitizerOptions() = default;
    156 };
    157 
    158 // Insert EfficiencySanitizer instrumentation.
    159 ModulePass *createEfficiencySanitizerPass(
    160     const EfficiencySanitizerOptions &Options = EfficiencySanitizerOptions());
    161 
    162 // Options for sanitizer coverage instrumentation.
    163 struct SanitizerCoverageOptions {
    164   enum Type {
    165     SCK_None = 0,
    166     SCK_Function,
    167     SCK_BB,
    168     SCK_Edge
    169   } CoverageType = SCK_None;
    170   bool IndirectCalls = false;
    171   bool TraceBB = false;
    172   bool TraceCmp = false;
    173   bool TraceDiv = false;
    174   bool TraceGep = false;
    175   bool Use8bitCounters = false;
    176   bool TracePC = false;
    177   bool TracePCGuard = false;
    178   bool Inline8bitCounters = false;
    179   bool PCTable = false;
    180   bool NoPrune = false;
    181   bool StackDepth = false;
    182 
    183   SanitizerCoverageOptions() = default;
    184 };
    185 
    186 // Insert SanitizerCoverage instrumentation.
    187 ModulePass *createSanitizerCoverageModulePass(
    188     const SanitizerCoverageOptions &Options = SanitizerCoverageOptions());
    189 
    190 /// Calculate what to divide by to scale counts.
    191 ///
    192 /// Given the maximum count, calculate a divisor that will scale all the
    193 /// weights to strictly less than std::numeric_limits<uint32_t>::max().
    194 static inline uint64_t calculateCountScale(uint64_t MaxCount) {
    195   return MaxCount < std::numeric_limits<uint32_t>::max()
    196              ? 1
    197              : MaxCount / std::numeric_limits<uint32_t>::max() + 1;
    198 }
    199 
    200 /// Scale an individual branch count.
    201 ///
    202 /// Scale a 64-bit weight down to 32-bits using \c Scale.
    203 ///
    204 static inline uint32_t scaleBranchCount(uint64_t Count, uint64_t Scale) {
    205   uint64_t Scaled = Count / Scale;
    206   assert(Scaled <= std::numeric_limits<uint32_t>::max() && "overflow 32-bits");
    207   return Scaled;
    208 }
    209 
    210 } // end namespace llvm
    211 
    212 #endif // LLVM_TRANSFORMS_INSTRUMENTATION_H
    213