Home | History | Annotate | Download | only in Transforms
      1 //===- Transforms/InstrProfiling.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 /// \file
     10 /// This file provides the interface for LLVM's PGO Instrumentation lowering
     11 /// pass.
     12 //===----------------------------------------------------------------------===//
     13 
     14 #ifndef LLVM_TRANSFORMS_INSTRPROFILING_H
     15 #define LLVM_TRANSFORMS_INSTRPROFILING_H
     16 
     17 #include "llvm/ADT/DenseMap.h"
     18 #include "llvm/ADT/StringRef.h"
     19 #include "llvm/IR/IntrinsicInst.h"
     20 #include "llvm/IR/PassManager.h"
     21 #include "llvm/ProfileData/InstrProf.h"
     22 #include "llvm/Transforms/Instrumentation.h"
     23 #include <cstddef>
     24 #include <cstdint>
     25 #include <cstring>
     26 #include <vector>
     27 
     28 namespace llvm {
     29 
     30 class TargetLibraryInfo;
     31 
     32 /// Instrumentation based profiling lowering pass. This pass lowers
     33 /// the profile instrumented code generated by FE or the IR based
     34 /// instrumentation pass.
     35 class InstrProfiling : public PassInfoMixin<InstrProfiling> {
     36 public:
     37   InstrProfiling() = default;
     38   InstrProfiling(const InstrProfOptions &Options) : Options(Options) {}
     39 
     40   PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
     41   bool run(Module &M, const TargetLibraryInfo &TLI);
     42 
     43 private:
     44   InstrProfOptions Options;
     45   Module *M;
     46   const TargetLibraryInfo *TLI;
     47   struct PerFunctionProfileData {
     48     uint32_t NumValueSites[IPVK_Last + 1];
     49     GlobalVariable *RegionCounters = nullptr;
     50     GlobalVariable *DataVar = nullptr;
     51 
     52     PerFunctionProfileData() {
     53       memset(NumValueSites, 0, sizeof(uint32_t) * (IPVK_Last + 1));
     54     }
     55   };
     56   DenseMap<GlobalVariable *, PerFunctionProfileData> ProfileDataMap;
     57   std::vector<GlobalValue *> UsedVars;
     58   std::vector<GlobalVariable *> ReferencedNames;
     59   GlobalVariable *NamesVar;
     60   size_t NamesSize;
     61 
     62   // The start value of precise value profile range for memory intrinsic sizes.
     63   int64_t MemOPSizeRangeStart;
     64   // The end value of precise value profile range for memory intrinsic sizes.
     65   int64_t MemOPSizeRangeLast;
     66 
     67   bool isMachO() const;
     68 
     69   /// Get the section name for the counter variables.
     70   StringRef getCountersSection() const;
     71 
     72   /// Get the section name for the name variables.
     73   StringRef getNameSection() const;
     74 
     75   /// Get the section name for the profile data variables.
     76   StringRef getDataSection() const;
     77 
     78   /// Get the section name for the coverage mapping data.
     79   StringRef getCoverageSection() const;
     80 
     81   /// Count the number of instrumented value sites for the function.
     82   void computeNumValueSiteCounts(InstrProfValueProfileInst *Ins);
     83 
     84   /// Replace instrprof_value_profile with a call to runtime library.
     85   void lowerValueProfileInst(InstrProfValueProfileInst *Ins);
     86 
     87   /// Replace instrprof_increment with an increment of the appropriate value.
     88   void lowerIncrement(InstrProfIncrementInst *Inc);
     89 
     90   /// Force emitting of name vars for unused functions.
     91   void lowerCoverageData(GlobalVariable *CoverageNamesVar);
     92 
     93   /// Get the region counters for an increment, creating them if necessary.
     94   ///
     95   /// If the counter array doesn't yet exist, the profile data variables
     96   /// referring to them will also be created.
     97   GlobalVariable *getOrCreateRegionCounters(InstrProfIncrementInst *Inc);
     98 
     99   /// Emit the section with compressed function names.
    100   void emitNameData();
    101 
    102   /// Emit value nodes section for value profiling.
    103   void emitVNodes();
    104 
    105   /// Emit runtime registration functions for each profile data variable.
    106   void emitRegistration();
    107 
    108   /// Emit the necessary plumbing to pull in the runtime initialization.
    109   void emitRuntimeHook();
    110 
    111   /// Add uses of our data variables and runtime hook.
    112   void emitUses();
    113 
    114   /// Create a static initializer for our data, on platforms that need it,
    115   /// and for any profile output file that was specified.
    116   void emitInitialization();
    117 };
    118 
    119 } // end namespace llvm
    120 
    121 #endif // LLVM_TRANSFORMS_INSTRPROFILING_H
    122