1 //===-- llvm/FunctionInfo.h - Function Info Index ---------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// FunctionInfo.h This file contains the declarations the classes that hold
12 /// the function info index and summary.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_IR_FUNCTIONINFO_H
17 #define LLVM_IR_FUNCTIONINFO_H
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"
27 /// \brief Function summary information to aid decisions and implementation of
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 {
34 /// \brief Path of module containing function IR, used to locate module when
35 /// importing this function.
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.
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.
47 /// This is only used in the per-module function index, as it is consumed
48 /// while creating the combined index.
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.
56 /// Number of instructions (ignoring debug instructions, e.g.) computed
57 /// during the initial compile step when the function index is first built.
61 /// Construct a summary object from summary data expected for all
63 FunctionSummary(unsigned NumInsts) : InstCount(NumInsts) {}
65 /// Set the path to the module containing this function, for use in
66 /// the combined index.
67 void setModulePath(StringRef ModPath) { ModulePath = ModPath; }
69 /// Get the path to the module containing this function.
70 StringRef modulePath() const { return ModulePath; }
72 /// Record whether this is a local function in the per-module index.
73 void setLocalFunction(bool IsLocal) { IsLocalFunction = IsLocal; }
75 /// Check whether this was a local function, for use in creating
76 /// the combined index.
77 bool isLocalFunction() const { return IsLocalFunction; }
79 /// Get the instruction count recorded for this function.
80 unsigned instCount() const { return InstCount; }
83 /// \brief Class to hold pointer to function summary and information required
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.
94 /// Function summary information used to help make ThinLTO importing
96 std::unique_ptr<FunctionSummary> Summary;
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.
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;
113 /// Constructor used during parsing of VST entries.
114 FunctionInfo(uint64_t FuncOffset)
115 : Summary(nullptr), BitcodeIndex(FuncOffset) {}
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) {}
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);
128 /// Get the function summary recorded for this function.
129 FunctionSummary *functionSummary() const { return Summary.get(); }
131 /// Get the bitcode index recorded for this function, depending on
133 uint64_t bitcodeIndex() const { return BitcodeIndex; }
135 /// Record the bitcode index for this function, depending on
137 void setBitcodeIndex(uint64_t FuncOffset) { BitcodeIndex = FuncOffset; }
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;
145 /// Map from function name to corresponding function info structures.
146 typedef StringMap<FunctionInfoList> FunctionInfoMapTy;
148 /// Type used for iterating through the function info map.
149 typedef FunctionInfoMapTy::const_iterator const_funcinfo_iterator;
150 typedef FunctionInfoMapTy::iterator funcinfo_iterator;
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;
157 /// Class to hold module path string table and function map,
158 /// and encapsulate methods for operating on them.
159 class FunctionInfoIndex {
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;
165 /// Holds strings for combined index, mapping to the corresponding module ID.
166 ModulePathStringTableTy ModulePathStringTable;
169 FunctionInfoIndex() = default;
170 ~FunctionInfoIndex() = default;
172 // Disable the copy constructor and assignment operators, so
173 // no unexpected copying/moving occurs.
174 FunctionInfoIndex(const FunctionInfoIndex &) = delete;
175 void operator=(const FunctionInfoIndex &) = delete;
177 funcinfo_iterator begin() { return FunctionMap.begin(); }
178 const_funcinfo_iterator begin() const { return FunctionMap.begin(); }
179 funcinfo_iterator end() { return FunctionMap.end(); }
180 const_funcinfo_iterator end() const { return FunctionMap.end(); }
182 /// Get the list of function info objects for a given function.
183 const FunctionInfoList &getFunctionInfoList(StringRef FuncName) {
184 return FunctionMap[FuncName];
187 /// Add a function info for a function of the given name.
188 void addFunctionInfo(StringRef FuncName, std::unique_ptr<FunctionInfo> Info) {
189 FunctionMap[FuncName].push_back(std::move(Info));
192 /// Iterator to allow writer to walk through table during emission.
193 iterator_range<StringMap<uint64_t>::const_iterator> modPathStringEntries()
195 return llvm::make_range(ModulePathStringTable.begin(),
196 ModulePathStringTable.end());
199 /// Get the module ID recorded for the given module path.
200 uint64_t getModuleId(const StringRef ModPath) const {
201 return ModulePathStringTable.lookup(ModPath);
204 /// Add the given per-module index into this function index/summary,
205 /// assigning it the given module ID. Each module merged in should have
206 /// a unique ID, necessary for consistent renaming of promoted
207 /// static (local) variables.
208 void mergeFrom(std::unique_ptr<FunctionInfoIndex> Other,
209 uint64_t NextModuleId);
211 /// Convenience method for creating a promoted global name
212 /// for the given value name of a local, and its original module's ID.
213 static std::string getGlobalNameForLocal(StringRef Name, uint64_t ModId) {
214 SmallString<256> NewName(Name);
216 raw_svector_ostream(NewName) << ModId;
217 return NewName.str();
220 /// Add a new module path, mapped to the given module Id, and return StringRef
221 /// owned by string table map.
222 StringRef addModulePath(StringRef ModPath, uint64_t ModId) {
223 return ModulePathStringTable.insert(std::make_pair(ModPath, ModId))
228 } // End llvm namespace