1 //===-- llvm/ModuleSummaryIndex.h - Module Summary 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 /// ModuleSummaryIndex.h This file contains the declarations the classes that
12 /// hold the module index and summary for function importing.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_IR_MODULESUMMARYINDEX_H
17 #define LLVM_IR_MODULESUMMARYINDEX_H
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/DenseSet.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/ADT/SmallString.h"
23 #include "llvm/ADT/StringExtras.h"
24 #include "llvm/ADT/StringMap.h"
25 #include "llvm/IR/Module.h"
32 template <typename T> struct MappingTraits;
35 /// \brief Class to accumulate and hold information about a callee.
37 enum class HotnessType : uint8_t { Unknown = 0, Cold = 1, None = 2, Hot = 3 };
38 HotnessType Hotness = HotnessType::Unknown;
40 CalleeInfo() = default;
41 explicit CalleeInfo(HotnessType Hotness) : Hotness(Hotness) {}
43 void updateHotness(const HotnessType OtherHotness) {
44 Hotness = std::max(Hotness, OtherHotness);
48 /// Struct to hold value either by GUID or GlobalValue*. Values in combined
49 /// indexes as well as indirect calls are GUIDs, all others are GlobalValues.
51 /// The value representation used in this instance.
57 /// Union of the two possible value types.
60 const GlobalValue *GV;
61 ValueUnion(GlobalValue::GUID Id) : Id(Id) {}
62 ValueUnion(const GlobalValue *GV) : GV(GV) {}
65 /// The value being represented.
67 /// The value representation.
69 /// Constructor for a GUID value
70 ValueInfo(GlobalValue::GUID Id = 0) : TheValue(Id), Kind(VI_GUID) {}
71 /// Constructor for a GlobalValue* value
72 ValueInfo(const GlobalValue *V) : TheValue(V), Kind(VI_Value) {}
73 /// Accessor for GUID value
74 GlobalValue::GUID getGUID() const {
75 assert(Kind == VI_GUID && "Not a GUID type");
78 /// Accessor for GlobalValue* value
79 const GlobalValue *getValue() const {
80 assert(Kind == VI_Value && "Not a Value type");
83 bool isGUID() const { return Kind == VI_GUID; }
86 template <> struct DenseMapInfo<ValueInfo> {
87 static inline ValueInfo getEmptyKey() { return ValueInfo((GlobalValue *)-1); }
88 static inline ValueInfo getTombstoneKey() {
89 return ValueInfo((GlobalValue *)-2);
91 static bool isEqual(ValueInfo L, ValueInfo R) {
92 if (L.isGUID() != R.isGUID())
94 return L.isGUID() ? (L.getGUID() == R.getGUID())
95 : (L.getValue() == R.getValue());
97 static unsigned getHashValue(ValueInfo I) {
98 return I.isGUID() ? I.getGUID() : (uintptr_t)I.getValue();
102 /// \brief Function and variable summary information to aid decisions and
103 /// implementation of importing.
104 class GlobalValueSummary {
106 /// \brief Sububclass discriminator (for dyn_cast<> et al.)
107 enum SummaryKind : unsigned { AliasKind, FunctionKind, GlobalVarKind };
109 /// Group flags (Linkage, NotEligibleToImport, etc.) as a bitfield.
111 /// \brief The linkage type of the associated global value.
113 /// One use is to flag values that have local linkage types and need to
114 /// have module identifier appended before placing into the combined
115 /// index, to disambiguate from other values with the same name.
116 /// In the future this will be used to update and optimize linkage
117 /// types based on global summary-based analysis.
118 unsigned Linkage : 4;
120 /// Indicate if the global value cannot be imported (e.g. it cannot
121 /// be renamed or references something that can't be renamed).
122 unsigned NotEligibleToImport : 1;
124 /// Indicate that the global value must be considered a live root for
125 /// index-based liveness analysis. Used for special LLVM values such as
126 /// llvm.global_ctors that the linker does not know about.
127 unsigned LiveRoot : 1;
129 /// Indicate if the global value should be hidden.
130 unsigned AutoHide : 1;
132 /// Convenience Constructors
133 explicit GVFlags(GlobalValue::LinkageTypes Linkage,
134 bool NotEligibleToImport, bool LiveRoot, bool AutoHide)
135 : Linkage(Linkage), NotEligibleToImport(NotEligibleToImport),
136 LiveRoot(LiveRoot), AutoHide(AutoHide) {}
140 /// Kind of summary for use in dyn_cast<> et al.
145 /// This is the hash of the name of the symbol in the original file. It is
146 /// identical to the GUID for global symbols, but differs for local since the
147 /// GUID includes the module level id in the hash.
148 GlobalValue::GUID OriginalName;
150 /// \brief Path of module IR containing value's definition, used to locate
151 /// module during importing.
153 /// This is only used during parsing of the combined index, or when
154 /// parsing the per-module index for creation of the combined summary index,
155 /// not during writing of the per-module index which doesn't contain a
156 /// module path string table.
157 StringRef ModulePath;
159 /// List of values referenced by this global value's definition
160 /// (either by the initializer of a global variable, or referenced
161 /// from within a function). This does not include functions called, which
162 /// are listed in the derived FunctionSummary object.
163 std::vector<ValueInfo> RefEdgeList;
166 /// GlobalValueSummary constructor.
167 GlobalValueSummary(SummaryKind K, GVFlags Flags, std::vector<ValueInfo> Refs)
168 : Kind(K), Flags(Flags), RefEdgeList(std::move(Refs)) {}
171 virtual ~GlobalValueSummary() = default;
173 /// Returns the hash of the original name, it is identical to the GUID for
174 /// externally visible symbols, but not for local ones.
175 GlobalValue::GUID getOriginalName() { return OriginalName; }
177 /// Initialize the original name hash in this summary.
178 void setOriginalName(GlobalValue::GUID Name) { OriginalName = Name; }
180 /// Which kind of summary subclass this is.
181 SummaryKind getSummaryKind() const { return Kind; }
183 /// Set the path to the module containing this function, for use in
184 /// the combined index.
185 void setModulePath(StringRef ModPath) { ModulePath = ModPath; }
187 /// Get the path to the module containing this function.
188 StringRef modulePath() const { return ModulePath; }
190 /// Get the flags for this GlobalValue (see \p struct GVFlags).
191 GVFlags flags() { return Flags; }
193 /// Return linkage type recorded for this global value.
194 GlobalValue::LinkageTypes linkage() const {
195 return static_cast<GlobalValue::LinkageTypes>(Flags.Linkage);
198 /// Sets the linkage to the value determined by global summary-based
199 /// optimization. Will be applied in the ThinLTO backends.
200 void setLinkage(GlobalValue::LinkageTypes Linkage) {
201 Flags.Linkage = Linkage;
204 /// Sets the visibility to be autohidden.
205 void setAutoHide() { Flags.AutoHide = true; }
207 /// Return true if this global value can't be imported.
208 bool notEligibleToImport() const { return Flags.NotEligibleToImport; }
210 /// Return true if this global value must be considered a root for live
211 /// value analysis on the index.
212 bool liveRoot() const { return Flags.LiveRoot; }
214 /// Flag that this global value must be considered a root for live
215 /// value analysis on the index.
216 void setLiveRoot() { Flags.LiveRoot = true; }
218 /// Flag that this global value cannot be imported.
219 void setNotEligibleToImport() { Flags.NotEligibleToImport = true; }
221 /// Return the list of values referenced by this global value definition.
222 ArrayRef<ValueInfo> refs() const { return RefEdgeList; }
225 /// \brief Alias summary information.
226 class AliasSummary : public GlobalValueSummary {
227 GlobalValueSummary *AliaseeSummary;
230 /// Summary constructors.
231 AliasSummary(GVFlags Flags, std::vector<ValueInfo> Refs)
232 : GlobalValueSummary(AliasKind, Flags, std::move(Refs)) {}
234 /// Check if this is an alias summary.
235 static bool classof(const GlobalValueSummary *GVS) {
236 return GVS->getSummaryKind() == AliasKind;
239 void setAliasee(GlobalValueSummary *Aliasee) { AliaseeSummary = Aliasee; }
241 const GlobalValueSummary &getAliasee() const {
242 return const_cast<AliasSummary *>(this)->getAliasee();
245 GlobalValueSummary &getAliasee() {
246 assert(AliaseeSummary && "Unexpected missing aliasee summary");
247 return *AliaseeSummary;
251 /// \brief Function summary information to aid decisions and implementation of
253 class FunctionSummary : public GlobalValueSummary {
255 /// <CalleeValueInfo, CalleeInfo> call edge pair.
256 typedef std::pair<ValueInfo, CalleeInfo> EdgeTy;
259 /// Number of instructions (ignoring debug instructions, e.g.) computed
260 /// during the initial compile step when the summary index is first built.
263 /// List of <CalleeValueInfo, CalleeInfo> call edge pairs from this function.
264 std::vector<EdgeTy> CallGraphEdgeList;
266 /// List of type identifiers used by this function, represented as GUIDs.
267 std::vector<GlobalValue::GUID> TypeIdList;
270 /// Summary constructors.
271 FunctionSummary(GVFlags Flags, unsigned NumInsts, std::vector<ValueInfo> Refs,
272 std::vector<EdgeTy> CGEdges,
273 std::vector<GlobalValue::GUID> TypeIds)
274 : GlobalValueSummary(FunctionKind, Flags, std::move(Refs)),
275 InstCount(NumInsts), CallGraphEdgeList(std::move(CGEdges)),
276 TypeIdList(std::move(TypeIds)) {}
278 /// Check if this is a function summary.
279 static bool classof(const GlobalValueSummary *GVS) {
280 return GVS->getSummaryKind() == FunctionKind;
283 /// Get the instruction count recorded for this function.
284 unsigned instCount() const { return InstCount; }
286 /// Return the list of <CalleeValueInfo, CalleeInfo> pairs.
287 ArrayRef<EdgeTy> calls() const { return CallGraphEdgeList; }
289 /// Returns the list of type identifiers used by this function.
290 ArrayRef<GlobalValue::GUID> type_tests() const { return TypeIdList; }
293 /// \brief Global variable summary information to aid decisions and
294 /// implementation of importing.
296 /// Currently this doesn't add anything to the base \p GlobalValueSummary,
297 /// but is a placeholder as additional info may be added to the summary
299 class GlobalVarSummary : public GlobalValueSummary {
302 /// Summary constructors.
303 GlobalVarSummary(GVFlags Flags, std::vector<ValueInfo> Refs)
304 : GlobalValueSummary(GlobalVarKind, Flags, std::move(Refs)) {}
306 /// Check if this is a global variable summary.
307 static bool classof(const GlobalValueSummary *GVS) {
308 return GVS->getSummaryKind() == GlobalVarKind;
312 struct TypeTestResolution {
313 /// Specifies which kind of type check we should emit for this byte array.
314 /// See http://clang.llvm.org/docs/ControlFlowIntegrityDesign.html for full
315 /// details on each kind of check; the enumerators are described with
316 /// reference to that document.
318 Unsat, ///< Unsatisfiable type (i.e. no global has this type metadata)
319 ByteArray, ///< Test a byte array (first example)
320 Inline, ///< Inlined bit vector ("Short Inline Bit Vectors")
321 Single, ///< Single element (last example in "Short Inline Bit Vectors")
322 AllOnes, ///< All-ones bit vector ("Eliminating Bit Vector Checks for
323 /// All-Ones Bit Vectors")
326 /// Range of size-1 expressed as a bit width. For example, if the size is in
327 /// range [1,256], this number will be 8. This helps generate the most compact
328 /// instruction sequences.
329 unsigned SizeM1BitWidth = 0;
332 struct TypeIdSummary {
333 TypeTestResolution TTRes;
337 typedef std::array<uint32_t, 5> ModuleHash;
339 /// List of global value summary structures for a particular value held
340 /// in the GlobalValueMap. Requires a vector in the case of multiple
341 /// COMDAT values of the same name.
342 typedef std::vector<std::unique_ptr<GlobalValueSummary>> GlobalValueSummaryList;
344 /// Map from global value GUID to corresponding summary structures.
345 /// Use a std::map rather than a DenseMap since it will likely incur
346 /// less overhead, as the value type is not very small and the size
347 /// of the map is unknown, resulting in inefficiencies due to repeated
348 /// insertions and resizing.
349 typedef std::map<GlobalValue::GUID, GlobalValueSummaryList>
350 GlobalValueSummaryMapTy;
352 /// Type used for iterating through the global value summary map.
353 typedef GlobalValueSummaryMapTy::const_iterator const_gvsummary_iterator;
354 typedef GlobalValueSummaryMapTy::iterator gvsummary_iterator;
356 /// String table to hold/own module path strings, which additionally holds the
357 /// module ID assigned to each module during the plugin step, as well as a hash
358 /// of the module. The StringMap makes a copy of and owns inserted strings.
359 typedef StringMap<std::pair<uint64_t, ModuleHash>> ModulePathStringTableTy;
361 /// Map of global value GUID to its summary, used to identify values defined in
362 /// a particular module, and provide efficient access to their summary.
363 typedef std::map<GlobalValue::GUID, GlobalValueSummary *> GVSummaryMapTy;
365 /// Class to hold module path string table and global value map,
366 /// and encapsulate methods for operating on them.
367 class ModuleSummaryIndex {
369 /// Map from value name to list of summary instances for values of that
370 /// name (may be duplicates in the COMDAT case, e.g.).
371 GlobalValueSummaryMapTy GlobalValueMap;
373 /// Holds strings for combined index, mapping to the corresponding module ID.
374 ModulePathStringTableTy ModulePathStringTable;
376 /// Mapping from type identifiers to summary information for that type
378 // FIXME: Add bitcode read/write support for this field.
379 std::map<std::string, TypeIdSummary> TypeIdMap;
382 friend yaml::MappingTraits<ModuleSummaryIndex>;
385 gvsummary_iterator begin() { return GlobalValueMap.begin(); }
386 const_gvsummary_iterator begin() const { return GlobalValueMap.begin(); }
387 gvsummary_iterator end() { return GlobalValueMap.end(); }
388 const_gvsummary_iterator end() const { return GlobalValueMap.end(); }
389 size_t size() const { return GlobalValueMap.size(); }
391 /// Get the list of global value summary objects for a given value name.
392 const GlobalValueSummaryList &getGlobalValueSummaryList(StringRef ValueName) {
393 return GlobalValueMap[GlobalValue::getGUID(ValueName)];
396 /// Get the list of global value summary objects for a given value name.
397 const const_gvsummary_iterator
398 findGlobalValueSummaryList(StringRef ValueName) const {
399 return GlobalValueMap.find(GlobalValue::getGUID(ValueName));
402 /// Get the list of global value summary objects for a given value GUID.
403 const const_gvsummary_iterator
404 findGlobalValueSummaryList(GlobalValue::GUID ValueGUID) const {
405 return GlobalValueMap.find(ValueGUID);
408 /// Add a global value summary for a value of the given name.
409 void addGlobalValueSummary(StringRef ValueName,
410 std::unique_ptr<GlobalValueSummary> Summary) {
411 GlobalValueMap[GlobalValue::getGUID(ValueName)].push_back(
415 /// Add a global value summary for a value of the given GUID.
416 void addGlobalValueSummary(GlobalValue::GUID ValueGUID,
417 std::unique_ptr<GlobalValueSummary> Summary) {
418 GlobalValueMap[ValueGUID].push_back(std::move(Summary));
421 /// Find the summary for global \p GUID in module \p ModuleId, or nullptr if
423 GlobalValueSummary *findSummaryInModule(GlobalValue::GUID ValueGUID,
424 StringRef ModuleId) const {
425 auto CalleeInfoList = findGlobalValueSummaryList(ValueGUID);
426 if (CalleeInfoList == end()) {
427 return nullptr; // This function does not have a summary
430 llvm::find_if(CalleeInfoList->second,
431 [&](const std::unique_ptr<GlobalValueSummary> &Summary) {
432 return Summary->modulePath() == ModuleId;
434 if (Summary == CalleeInfoList->second.end())
436 return Summary->get();
439 /// Returns the first GlobalValueSummary for \p GV, asserting that there
440 /// is only one if \p PerModuleIndex.
441 GlobalValueSummary *getGlobalValueSummary(const GlobalValue &GV,
442 bool PerModuleIndex = true) const {
443 assert(GV.hasName() && "Can't get GlobalValueSummary for GV with no name");
444 return getGlobalValueSummary(GlobalValue::getGUID(GV.getName()),
448 /// Returns the first GlobalValueSummary for \p ValueGUID, asserting that
450 /// is only one if \p PerModuleIndex.
451 GlobalValueSummary *getGlobalValueSummary(GlobalValue::GUID ValueGUID,
452 bool PerModuleIndex = true) const;
454 /// Table of modules, containing module hash and id.
455 const StringMap<std::pair<uint64_t, ModuleHash>> &modulePaths() const {
456 return ModulePathStringTable;
459 /// Table of modules, containing hash and id.
460 StringMap<std::pair<uint64_t, ModuleHash>> &modulePaths() {
461 return ModulePathStringTable;
464 /// Get the module ID recorded for the given module path.
465 uint64_t getModuleId(const StringRef ModPath) const {
466 return ModulePathStringTable.lookup(ModPath).first;
469 /// Get the module SHA1 hash recorded for the given module path.
470 const ModuleHash &getModuleHash(const StringRef ModPath) const {
471 auto It = ModulePathStringTable.find(ModPath);
472 assert(It != ModulePathStringTable.end() && "Module not registered");
473 return It->second.second;
476 /// Add the given per-module index into this module index/summary,
477 /// assigning it the given module ID. Each module merged in should have
478 /// a unique ID, necessary for consistent renaming of promoted
479 /// static (local) variables.
480 void mergeFrom(std::unique_ptr<ModuleSummaryIndex> Other,
481 uint64_t NextModuleId);
483 /// Convenience method for creating a promoted global name
484 /// for the given value name of a local, and its original module's ID.
485 static std::string getGlobalNameForLocal(StringRef Name, ModuleHash ModHash) {
486 SmallString<256> NewName(Name);
488 NewName += utohexstr(ModHash[0]); // Take the first 32 bits
489 return NewName.str();
492 /// Helper to obtain the unpromoted name for a global value (or the original
493 /// name if not promoted).
494 static StringRef getOriginalNameBeforePromote(StringRef Name) {
495 std::pair<StringRef, StringRef> Pair = Name.split(".llvm.");
499 /// Add a new module path with the given \p Hash, mapped to the given \p
500 /// ModID, and return an iterator to the entry in the index.
501 ModulePathStringTableTy::iterator
502 addModulePath(StringRef ModPath, uint64_t ModId,
503 ModuleHash Hash = ModuleHash{{0}}) {
504 return ModulePathStringTable.insert(std::make_pair(
506 std::make_pair(ModId, Hash))).first;
509 /// Check if the given Module has any functions available for exporting
510 /// in the index. We consider any module present in the ModulePathStringTable
511 /// to have exported functions.
512 bool hasExportedFunctions(const Module &M) const {
513 return ModulePathStringTable.count(M.getModuleIdentifier());
516 TypeIdSummary &getTypeIdSummary(StringRef TypeId) {
517 return TypeIdMap[TypeId];
520 /// Remove entries in the GlobalValueMap that have empty summaries due to the
521 /// eager nature of map entry creation during VST parsing. These would
522 /// also be suppressed during combined index generation in mergeFrom(),
523 /// but if there was only one module or this was the first module we might
524 /// not invoke mergeFrom.
525 void removeEmptySummaryEntries();
527 /// Collect for the given module the list of function it defines
528 /// (GUID -> Summary).
529 void collectDefinedFunctionsForModule(StringRef ModulePath,
530 GVSummaryMapTy &GVSummaryMap) const;
532 /// Collect for each module the list of Summaries it defines (GUID ->
534 void collectDefinedGVSummariesPerModule(
535 StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries) const;
538 } // End llvm namespace