Home | History | Annotate | Download | only in IR
      1 //===-- llvm/FunctionInfo.h - Function Info Index ---------------*- 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 /// @file
     11 /// FunctionInfo.h This file contains the declarations the classes that hold
     12 ///  the function info index and summary.
     13 //
     14 //===----------------------------------------------------------------------===//
     15 
     16 #ifndef LLVM_IR_FUNCTIONINFO_H
     17 #define LLVM_IR_FUNCTIONINFO_H
     18 
     19 #include "llvm/ADT/SmallString.h"
     20 #include "llvm/ADT/StringMap.h"
     21 #include "llvm/IR/Module.h"
     22 #include "llvm/Support/MemoryBuffer.h"
     23 #include "llvm/Support/raw_ostream.h"
     24 
     25 namespace llvm {
     26 
     27 /// \brief Function summary information to aid decisions and implementation of
     28 /// importing.
     29 ///
     30 /// This is a separate class from FunctionInfo to enable lazy reading of this
     31 /// function summary information from the combined index file during imporing.
     32 class FunctionSummary {
     33 private:
     34   /// \brief Path of module containing function IR, used to locate module when
     35   /// importing this function.
     36   ///
     37   /// This is only used during parsing of the combined function index, or when
     38   /// parsing the per-module index for creation of the combined function index,
     39   /// not during writing of the per-module index which doesn't contain a
     40   /// module path string table.
     41   StringRef ModulePath;
     42 
     43   /// \brief Used to flag functions that have local linkage types and need to
     44   /// have module identifier appended before placing into the combined
     45   /// index, to disambiguate from other functions with the same name.
     46   ///
     47   /// This is only used in the per-module function index, as it is consumed
     48   /// while creating the combined index.
     49   bool IsLocalFunction;
     50 
     51   // The rest of the information is used to help decide whether importing
     52   // is likely to be profitable.
     53   // Other information will be added as the importing is tuned, such
     54   // as hotness (when profile available), and other function characteristics.
     55 
     56   /// Number of instructions (ignoring debug instructions, e.g.) computed
     57   /// during the initial compile step when the function index is first built.
     58   unsigned InstCount;
     59 
     60 public:
     61   /// Construct a summary object from summary data expected for all
     62   /// summary records.
     63   FunctionSummary(unsigned NumInsts) : InstCount(NumInsts) {}
     64 
     65   /// Set the path to the module containing this function, for use in
     66   /// the combined index.
     67   void setModulePath(StringRef ModPath) { ModulePath = ModPath; }
     68 
     69   /// Get the path to the module containing this function.
     70   StringRef modulePath() const { return ModulePath; }
     71 
     72   /// Record whether this is a local function in the per-module index.
     73   void setLocalFunction(bool IsLocal) { IsLocalFunction = IsLocal; }
     74 
     75   /// Check whether this was a local function, for use in creating
     76   /// the combined index.
     77   bool isLocalFunction() const { return IsLocalFunction; }
     78 
     79   /// Get the instruction count recorded for this function.
     80   unsigned instCount() const { return InstCount; }
     81 };
     82 
     83 /// \brief Class to hold pointer to function summary and information required
     84 /// for parsing it.
     85 ///
     86 /// For the per-module index, this holds the bitcode offset
     87 /// of the corresponding function block. For the combined index,
     88 /// after parsing of the \a ValueSymbolTable, this initially
     89 /// holds the offset of the corresponding function summary bitcode
     90 /// record. After parsing the associated summary information from the summary
     91 /// block the \a FunctionSummary is populated and stored here.
     92 class FunctionInfo {
     93 private:
     94   /// Function summary information used to help make ThinLTO importing
     95   /// decisions.
     96   std::unique_ptr<FunctionSummary> Summary;
     97 
     98   /// \brief The bitcode offset corresponding to either the associated
     99   /// function's function body record, or its function summary record,
    100   /// depending on whether this is a per-module or combined index.
    101   ///
    102   /// This bitcode offset is written to or read from the associated
    103   /// \a ValueSymbolTable entry for the function.
    104   /// For the per-module index this holds the bitcode offset of the
    105   /// function's body record  within bitcode module block in its module,
    106   /// which is used during lazy function parsing or ThinLTO importing.
    107   /// For the combined index this holds the offset of the corresponding
    108   /// function summary record, to enable associating the combined index
    109   /// VST records with the summary records.
    110   uint64_t BitcodeIndex;
    111 
    112 public:
    113   /// Constructor used during parsing of VST entries.
    114   FunctionInfo(uint64_t FuncOffset)
    115       : Summary(nullptr), BitcodeIndex(FuncOffset) {}
    116 
    117   /// Constructor used for per-module index bitcode writing.
    118   FunctionInfo(uint64_t FuncOffset,
    119                std::unique_ptr<FunctionSummary> FuncSummary)
    120       : Summary(std::move(FuncSummary)), BitcodeIndex(FuncOffset) {}
    121 
    122   /// Record the function summary information parsed out of the function
    123   /// summary block during parsing or combined index creation.
    124   void setFunctionSummary(std::unique_ptr<FunctionSummary> FuncSummary) {
    125     Summary = std::move(FuncSummary);
    126   }
    127 
    128   /// Get the function summary recorded for this function.
    129   FunctionSummary *functionSummary() const { return Summary.get(); }
    130 
    131   /// Get the bitcode index recorded for this function, depending on
    132   /// the index type.
    133   uint64_t bitcodeIndex() const { return BitcodeIndex; }
    134 
    135   /// Record the bitcode index for this function, depending on
    136   /// the index type.
    137   void setBitcodeIndex(uint64_t FuncOffset) { BitcodeIndex = FuncOffset; }
    138 };
    139 
    140 /// List of function info structures for a particular function name held
    141 /// in the FunctionMap. Requires a vector in the case of multiple
    142 /// COMDAT functions of the same name.
    143 typedef std::vector<std::unique_ptr<FunctionInfo>> FunctionInfoList;
    144 
    145 /// Map from function name to corresponding function info structures.
    146 typedef StringMap<FunctionInfoList> FunctionInfoMapTy;
    147 
    148 /// Type used for iterating through the function info map.
    149 typedef FunctionInfoMapTy::const_iterator const_funcinfo_iterator;
    150 typedef FunctionInfoMapTy::iterator funcinfo_iterator;
    151 
    152 /// String table to hold/own module path strings, which additionally holds the
    153 /// module ID assigned to each module during the plugin step. The StringMap
    154 /// makes a copy of and owns inserted strings.
    155 typedef StringMap<uint64_t> ModulePathStringTableTy;
    156 
    157 /// Class to hold module path string table and function map,
    158 /// and encapsulate methods for operating on them.
    159 class FunctionInfoIndex {
    160 private:
    161   /// Map from function name to list of function information instances
    162   /// for functions of that name (may be duplicates in the COMDAT case, e.g.).
    163   FunctionInfoMapTy FunctionMap;
    164 
    165   /// Holds strings for combined index, mapping to the corresponding module ID.
    166   ModulePathStringTableTy ModulePathStringTable;
    167 
    168 public:
    169   FunctionInfoIndex() = default;
    170 
    171   // Disable the copy constructor and assignment operators, so
    172   // no unexpected copying/moving occurs.
    173   FunctionInfoIndex(const FunctionInfoIndex &) = delete;
    174   void operator=(const FunctionInfoIndex &) = delete;
    175 
    176   funcinfo_iterator begin() { return FunctionMap.begin(); }
    177   const_funcinfo_iterator begin() const { return FunctionMap.begin(); }
    178   funcinfo_iterator end() { return FunctionMap.end(); }
    179   const_funcinfo_iterator end() const { return FunctionMap.end(); }
    180 
    181   /// Get the list of function info objects for a given function.
    182   const FunctionInfoList &getFunctionInfoList(StringRef FuncName) {
    183     return FunctionMap[FuncName];
    184   }
    185 
    186   /// Get the list of function info objects for a given function.
    187   const const_funcinfo_iterator findFunctionInfoList(StringRef FuncName) const {
    188     return FunctionMap.find(FuncName);
    189   }
    190 
    191   /// Add a function info for a function of the given name.
    192   void addFunctionInfo(StringRef FuncName, std::unique_ptr<FunctionInfo> Info) {
    193     FunctionMap[FuncName].push_back(std::move(Info));
    194   }
    195 
    196   /// Iterator to allow writer to walk through table during emission.
    197   iterator_range<StringMap<uint64_t>::const_iterator>
    198   modPathStringEntries() const {
    199     return llvm::make_range(ModulePathStringTable.begin(),
    200                             ModulePathStringTable.end());
    201   }
    202 
    203   /// Get the module ID recorded for the given module path.
    204   uint64_t getModuleId(const StringRef ModPath) const {
    205     return ModulePathStringTable.lookup(ModPath);
    206   }
    207 
    208   /// Add the given per-module index into this function index/summary,
    209   /// assigning it the given module ID. Each module merged in should have
    210   /// a unique ID, necessary for consistent renaming of promoted
    211   /// static (local) variables.
    212   void mergeFrom(std::unique_ptr<FunctionInfoIndex> Other,
    213                  uint64_t NextModuleId);
    214 
    215   /// Convenience method for creating a promoted global name
    216   /// for the given value name of a local, and its original module's ID.
    217   static std::string getGlobalNameForLocal(StringRef Name, uint64_t ModId) {
    218     SmallString<256> NewName(Name);
    219     NewName += ".llvm.";
    220     raw_svector_ostream(NewName) << ModId;
    221     return NewName.str();
    222   }
    223 
    224   /// Add a new module path, mapped to the given module Id, and return StringRef
    225   /// owned by string table map.
    226   StringRef addModulePath(StringRef ModPath, uint64_t ModId) {
    227     return ModulePathStringTable.insert(std::make_pair(ModPath, ModId))
    228         .first->first();
    229   }
    230 
    231   /// Check if the given Module has any functions available for exporting
    232   /// in the index. We consider any module present in the ModulePathStringTable
    233   /// to have exported functions.
    234   bool hasExportedFunctions(const Module &M) const {
    235     return ModulePathStringTable.count(M.getModuleIdentifier());
    236   }
    237 };
    238 
    239 } // End llvm namespace
    240 
    241 #endif
    242