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 /// Convenience Constructors
130 explicit GVFlags(GlobalValue::LinkageTypes Linkage,
131 bool NotEligibleToImport, bool LiveRoot)
132 : Linkage(Linkage), NotEligibleToImport(NotEligibleToImport),
133 LiveRoot(LiveRoot) {}
137 /// Kind of summary for use in dyn_cast<> et al.
142 /// This is the hash of the name of the symbol in the original file. It is
143 /// identical to the GUID for global symbols, but differs for local since the
144 /// GUID includes the module level id in the hash.
145 GlobalValue::GUID OriginalName;
147 /// \brief Path of module IR containing value's definition, used to locate
148 /// module during importing.
150 /// This is only used during parsing of the combined index, or when
151 /// parsing the per-module index for creation of the combined summary index,
152 /// not during writing of the per-module index which doesn't contain a
153 /// module path string table.
154 StringRef ModulePath;
156 /// List of values referenced by this global value's definition
157 /// (either by the initializer of a global variable, or referenced
158 /// from within a function). This does not include functions called, which
159 /// are listed in the derived FunctionSummary object.
160 std::vector<ValueInfo> RefEdgeList;
163 /// GlobalValueSummary constructor.
164 GlobalValueSummary(SummaryKind K, GVFlags Flags, std::vector<ValueInfo> Refs)
165 : Kind(K), Flags(Flags), RefEdgeList(std::move(Refs)) {}
168 virtual ~GlobalValueSummary() = default;
170 /// Returns the hash of the original name, it is identical to the GUID for
171 /// externally visible symbols, but not for local ones.
172 GlobalValue::GUID getOriginalName() { return OriginalName; }
174 /// Initialize the original name hash in this summary.
175 void setOriginalName(GlobalValue::GUID Name) { OriginalName = Name; }
177 /// Which kind of summary subclass this is.
178 SummaryKind getSummaryKind() const { return Kind; }
180 /// Set the path to the module containing this function, for use in
181 /// the combined index.
182 void setModulePath(StringRef ModPath) { ModulePath = ModPath; }
184 /// Get the path to the module containing this function.
185 StringRef modulePath() const { return ModulePath; }
187 /// Get the flags for this GlobalValue (see \p struct GVFlags).
188 GVFlags flags() { return Flags; }
190 /// Return linkage type recorded for this global value.
191 GlobalValue::LinkageTypes linkage() const {
192 return static_cast<GlobalValue::LinkageTypes>(Flags.Linkage);
195 /// Sets the linkage to the value determined by global summary-based
196 /// optimization. Will be applied in the ThinLTO backends.
197 void setLinkage(GlobalValue::LinkageTypes Linkage) {
198 Flags.Linkage = Linkage;
201 /// Return true if this global value can't be imported.
202 bool notEligibleToImport() const { return Flags.NotEligibleToImport; }
204 /// Return true if this global value must be considered a root for live
205 /// value analysis on the index.
206 bool liveRoot() const { return Flags.LiveRoot; }
208 /// Flag that this global value must be considered a root for live
209 /// value analysis on the index.
210 void setLiveRoot() { Flags.LiveRoot = true; }
212 /// Flag that this global value cannot be imported.
213 void setNotEligibleToImport() { Flags.NotEligibleToImport = true; }
215 /// Return the list of values referenced by this global value definition.
216 ArrayRef<ValueInfo> refs() const { return RefEdgeList; }
219 /// \brief Alias summary information.
220 class AliasSummary : public GlobalValueSummary {
221 GlobalValueSummary *AliaseeSummary;
224 /// Summary constructors.
225 AliasSummary(GVFlags Flags, std::vector<ValueInfo> Refs)
226 : GlobalValueSummary(AliasKind, Flags, std::move(Refs)) {}
228 /// Check if this is an alias summary.
229 static bool classof(const GlobalValueSummary *GVS) {
230 return GVS->getSummaryKind() == AliasKind;
233 void setAliasee(GlobalValueSummary *Aliasee) { AliaseeSummary = Aliasee; }
235 const GlobalValueSummary &getAliasee() const {
236 return const_cast<AliasSummary *>(this)->getAliasee();
239 GlobalValueSummary &getAliasee() {
240 assert(AliaseeSummary && "Unexpected missing aliasee summary");
241 return *AliaseeSummary;
245 /// \brief Function summary information to aid decisions and implementation of
247 class FunctionSummary : public GlobalValueSummary {
249 /// <CalleeValueInfo, CalleeInfo> call edge pair.
250 typedef std::pair<ValueInfo, CalleeInfo> EdgeTy;
253 /// Number of instructions (ignoring debug instructions, e.g.) computed
254 /// during the initial compile step when the summary index is first built.
257 /// List of <CalleeValueInfo, CalleeInfo> call edge pairs from this function.
258 std::vector<EdgeTy> CallGraphEdgeList;
260 /// List of type identifiers used by this function, represented as GUIDs.
261 std::vector<GlobalValue::GUID> TypeIdList;
264 /// Summary constructors.
265 FunctionSummary(GVFlags Flags, unsigned NumInsts, std::vector<ValueInfo> Refs,
266 std::vector<EdgeTy> CGEdges,
267 std::vector<GlobalValue::GUID> TypeIds)
268 : GlobalValueSummary(FunctionKind, Flags, std::move(Refs)),
269 InstCount(NumInsts), CallGraphEdgeList(std::move(CGEdges)),
270 TypeIdList(std::move(TypeIds)) {}
272 /// Check if this is a function summary.
273 static bool classof(const GlobalValueSummary *GVS) {
274 return GVS->getSummaryKind() == FunctionKind;
277 /// Get the instruction count recorded for this function.
278 unsigned instCount() const { return InstCount; }
280 /// Return the list of <CalleeValueInfo, CalleeInfo> pairs.
281 ArrayRef<EdgeTy> calls() const { return CallGraphEdgeList; }
283 /// Returns the list of type identifiers used by this function.
284 ArrayRef<GlobalValue::GUID> type_tests() const { return TypeIdList; }
287 /// \brief Global variable summary information to aid decisions and
288 /// implementation of importing.
290 /// Currently this doesn't add anything to the base \p GlobalValueSummary,
291 /// but is a placeholder as additional info may be added to the summary
293 class GlobalVarSummary : public GlobalValueSummary {
296 /// Summary constructors.
297 GlobalVarSummary(GVFlags Flags, std::vector<ValueInfo> Refs)
298 : GlobalValueSummary(GlobalVarKind, Flags, std::move(Refs)) {}
300 /// Check if this is a global variable summary.
301 static bool classof(const GlobalValueSummary *GVS) {
302 return GVS->getSummaryKind() == GlobalVarKind;
306 struct TypeTestResolution {
307 /// Specifies which kind of type check we should emit for this byte array.
308 /// See http://clang.llvm.org/docs/ControlFlowIntegrityDesign.html for full
309 /// details on each kind of check; the enumerators are described with
310 /// reference to that document.
312 Unsat, ///< Unsatisfiable type (i.e. no global has this type metadata)
313 ByteArray, ///< Test a byte array (first example)
314 Inline, ///< Inlined bit vector ("Short Inline Bit Vectors")
315 Single, ///< Single element (last example in "Short Inline Bit Vectors")
316 AllOnes, ///< All-ones bit vector ("Eliminating Bit Vector Checks for
317 /// All-Ones Bit Vectors")
320 /// Range of size-1 expressed as a bit width. For example, if the size is in
321 /// range [1,256], this number will be 8. This helps generate the most compact
322 /// instruction sequences.
323 unsigned SizeM1BitWidth = 0;
326 struct TypeIdSummary {
327 TypeTestResolution TTRes;
331 typedef std::array<uint32_t, 5> ModuleHash;
333 /// List of global value summary structures for a particular value held
334 /// in the GlobalValueMap. Requires a vector in the case of multiple
335 /// COMDAT values of the same name.
336 typedef std::vector<std::unique_ptr<GlobalValueSummary>> GlobalValueSummaryList;
338 /// Map from global value GUID to corresponding summary structures.
339 /// Use a std::map rather than a DenseMap since it will likely incur
340 /// less overhead, as the value type is not very small and the size
341 /// of the map is unknown, resulting in inefficiencies due to repeated
342 /// insertions and resizing.
343 typedef std::map<GlobalValue::GUID, GlobalValueSummaryList>
344 GlobalValueSummaryMapTy;
346 /// Type used for iterating through the global value summary map.
347 typedef GlobalValueSummaryMapTy::const_iterator const_gvsummary_iterator;
348 typedef GlobalValueSummaryMapTy::iterator gvsummary_iterator;
350 /// String table to hold/own module path strings, which additionally holds the
351 /// module ID assigned to each module during the plugin step, as well as a hash
352 /// of the module. The StringMap makes a copy of and owns inserted strings.
353 typedef StringMap<std::pair<uint64_t, ModuleHash>> ModulePathStringTableTy;
355 /// Map of global value GUID to its summary, used to identify values defined in
356 /// a particular module, and provide efficient access to their summary.
357 typedef std::map<GlobalValue::GUID, GlobalValueSummary *> GVSummaryMapTy;
359 /// Class to hold module path string table and global value map,
360 /// and encapsulate methods for operating on them.
361 class ModuleSummaryIndex {
363 /// Map from value name to list of summary instances for values of that
364 /// name (may be duplicates in the COMDAT case, e.g.).
365 GlobalValueSummaryMapTy GlobalValueMap;
367 /// Holds strings for combined index, mapping to the corresponding module ID.
368 ModulePathStringTableTy ModulePathStringTable;
370 /// Mapping from type identifiers to summary information for that type
372 // FIXME: Add bitcode read/write support for this field.
373 std::map<std::string, TypeIdSummary> TypeIdMap;
376 friend yaml::MappingTraits<ModuleSummaryIndex>;
379 gvsummary_iterator begin() { return GlobalValueMap.begin(); }
380 const_gvsummary_iterator begin() const { return GlobalValueMap.begin(); }
381 gvsummary_iterator end() { return GlobalValueMap.end(); }
382 const_gvsummary_iterator end() const { return GlobalValueMap.end(); }
383 size_t size() const { return GlobalValueMap.size(); }
385 /// Get the list of global value summary objects for a given value name.
386 const GlobalValueSummaryList &getGlobalValueSummaryList(StringRef ValueName) {
387 return GlobalValueMap[GlobalValue::getGUID(ValueName)];
390 /// Get the list of global value summary objects for a given value name.
391 const const_gvsummary_iterator
392 findGlobalValueSummaryList(StringRef ValueName) const {
393 return GlobalValueMap.find(GlobalValue::getGUID(ValueName));
396 /// Get the list of global value summary objects for a given value GUID.
397 const const_gvsummary_iterator
398 findGlobalValueSummaryList(GlobalValue::GUID ValueGUID) const {
399 return GlobalValueMap.find(ValueGUID);
402 /// Add a global value summary for a value of the given name.
403 void addGlobalValueSummary(StringRef ValueName,
404 std::unique_ptr<GlobalValueSummary> Summary) {
405 GlobalValueMap[GlobalValue::getGUID(ValueName)].push_back(
409 /// Add a global value summary for a value of the given GUID.
410 void addGlobalValueSummary(GlobalValue::GUID ValueGUID,
411 std::unique_ptr<GlobalValueSummary> Summary) {
412 GlobalValueMap[ValueGUID].push_back(std::move(Summary));
415 /// Find the summary for global \p GUID in module \p ModuleId, or nullptr if
417 GlobalValueSummary *findSummaryInModule(GlobalValue::GUID ValueGUID,
418 StringRef ModuleId) const {
419 auto CalleeInfoList = findGlobalValueSummaryList(ValueGUID);
420 if (CalleeInfoList == end()) {
421 return nullptr; // This function does not have a summary
424 llvm::find_if(CalleeInfoList->second,
425 [&](const std::unique_ptr<GlobalValueSummary> &Summary) {
426 return Summary->modulePath() == ModuleId;
428 if (Summary == CalleeInfoList->second.end())
430 return Summary->get();
433 /// Returns the first GlobalValueSummary for \p GV, asserting that there
434 /// is only one if \p PerModuleIndex.
435 GlobalValueSummary *getGlobalValueSummary(const GlobalValue &GV,
436 bool PerModuleIndex = true) const {
437 assert(GV.hasName() && "Can't get GlobalValueSummary for GV with no name");
438 return getGlobalValueSummary(GlobalValue::getGUID(GV.getName()),
442 /// Returns the first GlobalValueSummary for \p ValueGUID, asserting that
444 /// is only one if \p PerModuleIndex.
445 GlobalValueSummary *getGlobalValueSummary(GlobalValue::GUID ValueGUID,
446 bool PerModuleIndex = true) const;
448 /// Table of modules, containing module hash and id.
449 const StringMap<std::pair<uint64_t, ModuleHash>> &modulePaths() const {
450 return ModulePathStringTable;
453 /// Table of modules, containing hash and id.
454 StringMap<std::pair<uint64_t, ModuleHash>> &modulePaths() {
455 return ModulePathStringTable;
458 /// Get the module ID recorded for the given module path.
459 uint64_t getModuleId(const StringRef ModPath) const {
460 return ModulePathStringTable.lookup(ModPath).first;
463 /// Get the module SHA1 hash recorded for the given module path.
464 const ModuleHash &getModuleHash(const StringRef ModPath) const {
465 auto It = ModulePathStringTable.find(ModPath);
466 assert(It != ModulePathStringTable.end() && "Module not registered");
467 return It->second.second;
470 /// Add the given per-module index into this module index/summary,
471 /// assigning it the given module ID. Each module merged in should have
472 /// a unique ID, necessary for consistent renaming of promoted
473 /// static (local) variables.
474 void mergeFrom(std::unique_ptr<ModuleSummaryIndex> Other,
475 uint64_t NextModuleId);
477 /// Convenience method for creating a promoted global name
478 /// for the given value name of a local, and its original module's ID.
479 static std::string getGlobalNameForLocal(StringRef Name, ModuleHash ModHash) {
480 SmallString<256> NewName(Name);
482 NewName += utohexstr(ModHash[0]); // Take the first 32 bits
483 return NewName.str();
486 /// Helper to obtain the unpromoted name for a global value (or the original
487 /// name if not promoted).
488 static StringRef getOriginalNameBeforePromote(StringRef Name) {
489 std::pair<StringRef, StringRef> Pair = Name.split(".llvm.");
493 /// Add a new module path with the given \p Hash, mapped to the given \p
494 /// ModID, and return an iterator to the entry in the index.
495 ModulePathStringTableTy::iterator
496 addModulePath(StringRef ModPath, uint64_t ModId,
497 ModuleHash Hash = ModuleHash{{0}}) {
498 return ModulePathStringTable.insert(std::make_pair(
500 std::make_pair(ModId, Hash))).first;
503 /// Check if the given Module has any functions available for exporting
504 /// in the index. We consider any module present in the ModulePathStringTable
505 /// to have exported functions.
506 bool hasExportedFunctions(const Module &M) const {
507 return ModulePathStringTable.count(M.getModuleIdentifier());
510 /// Remove entries in the GlobalValueMap that have empty summaries due to the
511 /// eager nature of map entry creation during VST parsing. These would
512 /// also be suppressed during combined index generation in mergeFrom(),
513 /// but if there was only one module or this was the first module we might
514 /// not invoke mergeFrom.
515 void removeEmptySummaryEntries();
517 /// Collect for the given module the list of function it defines
518 /// (GUID -> Summary).
519 void collectDefinedFunctionsForModule(StringRef ModulePath,
520 GVSummaryMapTy &GVSummaryMap) const;
522 /// Collect for each module the list of Summaries it defines (GUID ->
524 void collectDefinedGVSummariesPerModule(
525 StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries) const;
528 } // End llvm namespace