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/ArrayRef.h"
20 #include "llvm/ADT/DenseMap.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/ADT/StringRef.h"
26 #include "llvm/ADT/TinyPtrVector.h"
27 #include "llvm/IR/GlobalValue.h"
28 #include "llvm/IR/Module.h"
29 #include "llvm/Support/Allocator.h"
30 #include "llvm/Support/MathExtras.h"
31 #include "llvm/Support/ScaledNumber.h"
32 #include "llvm/Support/StringSaver.h"
49 template <typename T> struct MappingTraits;
51 } // end namespace yaml
53 /// Class to accumulate and hold information about a callee.
55 enum class HotnessType : uint8_t {
63 // The size of the bit-field might need to be adjusted if more values are
64 // added to HotnessType enum.
67 /// The value stored in RelBlockFreq has to be interpreted as the digits of
68 /// a scaled number with a scale of \p -ScaleShift.
69 uint32_t RelBlockFreq : 29;
70 static constexpr int32_t ScaleShift = 8;
71 static constexpr uint64_t MaxRelBlockFreq = (1 << 29) - 1;
74 : Hotness(static_cast<uint32_t>(HotnessType::Unknown)), RelBlockFreq(0) {}
75 explicit CalleeInfo(HotnessType Hotness, uint64_t RelBF)
76 : Hotness(static_cast<uint32_t>(Hotness)), RelBlockFreq(RelBF) {}
78 void updateHotness(const HotnessType OtherHotness) {
79 Hotness = std::max(Hotness, static_cast<uint32_t>(OtherHotness));
82 HotnessType getHotness() const { return HotnessType(Hotness); }
84 /// Update \p RelBlockFreq from \p BlockFreq and \p EntryFreq
86 /// BlockFreq is divided by EntryFreq and added to RelBlockFreq. To represent
87 /// fractional values, the result is represented as a fixed point number with
88 /// scale of -ScaleShift.
89 void updateRelBlockFreq(uint64_t BlockFreq, uint64_t EntryFreq) {
92 using Scaled64 = ScaledNumber<uint64_t>;
93 Scaled64 Temp(BlockFreq, ScaleShift);
94 Temp /= Scaled64::get(EntryFreq);
97 SaturatingAdd<uint64_t>(Temp.toInt<uint64_t>(), RelBlockFreq);
98 Sum = std::min(Sum, uint64_t(MaxRelBlockFreq));
99 RelBlockFreq = static_cast<uint32_t>(Sum);
103 inline const char *getHotnessName(CalleeInfo::HotnessType HT) {
105 case CalleeInfo::HotnessType::Unknown:
107 case CalleeInfo::HotnessType::Cold:
109 case CalleeInfo::HotnessType::None:
111 case CalleeInfo::HotnessType::Hot:
113 case CalleeInfo::HotnessType::Critical:
116 llvm_unreachable("invalid hotness");
119 class GlobalValueSummary;
121 using GlobalValueSummaryList = std::vector<std::unique_ptr<GlobalValueSummary>>;
123 struct GlobalValueSummaryInfo {
125 NameOrGV(bool HaveGVs) {
132 /// The GlobalValue corresponding to this summary. This is only used in
133 /// per-module summaries and when the IR is available. E.g. when module
134 /// analysis is being run, or when parsing both the IR and the summary
136 const GlobalValue *GV;
138 /// Summary string representation. This StringRef points to BC module
139 /// string table and is valid until module data is stored in memory.
140 /// This is guaranteed to happen until runThinLTOBackend function is
141 /// called, so it is safe to use this field during thin link. This field
142 /// is only valid if summary index was loaded from BC file.
146 GlobalValueSummaryInfo(bool HaveGVs) : U(HaveGVs) {}
148 /// List of global value summary structures for a particular value held
149 /// in the GlobalValueMap. Requires a vector in the case of multiple
150 /// COMDAT values of the same name.
151 GlobalValueSummaryList SummaryList;
154 /// Map from global value GUID to corresponding summary structures. Use a
155 /// std::map rather than a DenseMap so that pointers to the map's value_type
156 /// (which are used by ValueInfo) are not invalidated by insertion. Also it will
157 /// likely incur less overhead, as the value type is not very small and the size
158 /// of the map is unknown, resulting in inefficiencies due to repeated
159 /// insertions and resizing.
160 using GlobalValueSummaryMapTy =
161 std::map<GlobalValue::GUID, GlobalValueSummaryInfo>;
163 /// Struct that holds a reference to a particular GUID in a global value
166 PointerIntPair<const GlobalValueSummaryMapTy::value_type *, 2, int>
169 ValueInfo() = default;
170 ValueInfo(bool HaveGVs, const GlobalValueSummaryMapTy::value_type *R) {
171 RefAndFlags.setPointer(R);
172 RefAndFlags.setInt(HaveGVs);
175 operator bool() const { return getRef(); }
177 GlobalValue::GUID getGUID() const { return getRef()->first; }
178 const GlobalValue *getValue() const {
180 return getRef()->second.U.GV;
183 ArrayRef<std::unique_ptr<GlobalValueSummary>> getSummaryList() const {
184 return getRef()->second.SummaryList;
187 StringRef name() const {
188 return haveGVs() ? getRef()->second.U.GV->getName()
189 : getRef()->second.U.Name;
192 bool haveGVs() const { return RefAndFlags.getInt() & 0x1; }
193 bool isReadOnly() const { return RefAndFlags.getInt() & 0x2; }
194 void setReadOnly() { RefAndFlags.setInt(RefAndFlags.getInt() | 0x2); }
196 const GlobalValueSummaryMapTy::value_type *getRef() const {
197 return RefAndFlags.getPointer();
200 bool isDSOLocal() const;
203 inline raw_ostream &operator<<(raw_ostream &OS, const ValueInfo &VI) {
205 if (!VI.name().empty())
206 OS << " (" << VI.name() << ")";
210 inline bool operator==(const ValueInfo &A, const ValueInfo &B) {
211 assert(A.getRef() && B.getRef() &&
212 "Need ValueInfo with non-null Ref for comparison");
213 return A.getRef() == B.getRef();
216 inline bool operator!=(const ValueInfo &A, const ValueInfo &B) {
217 assert(A.getRef() && B.getRef() &&
218 "Need ValueInfo with non-null Ref for comparison");
219 return A.getRef() != B.getRef();
222 inline bool operator<(const ValueInfo &A, const ValueInfo &B) {
223 assert(A.getRef() && B.getRef() &&
224 "Need ValueInfo with non-null Ref to compare GUIDs");
225 return A.getGUID() < B.getGUID();
228 template <> struct DenseMapInfo<ValueInfo> {
229 static inline ValueInfo getEmptyKey() {
230 return ValueInfo(false, (GlobalValueSummaryMapTy::value_type *)-8);
233 static inline ValueInfo getTombstoneKey() {
234 return ValueInfo(false, (GlobalValueSummaryMapTy::value_type *)-16);
237 static inline bool isSpecialKey(ValueInfo V) {
238 return V == getTombstoneKey() || V == getEmptyKey();
241 static bool isEqual(ValueInfo L, ValueInfo R) {
242 // We are not supposed to mix ValueInfo(s) with different HaveGVs flag
243 // in a same container.
244 assert(isSpecialKey(L) || isSpecialKey(R) || (L.haveGVs() == R.haveGVs()));
245 return L.getRef() == R.getRef();
247 static unsigned getHashValue(ValueInfo I) { return (uintptr_t)I.getRef(); }
250 /// Function and variable summary information to aid decisions and
251 /// implementation of importing.
252 class GlobalValueSummary {
254 /// Sububclass discriminator (for dyn_cast<> et al.)
255 enum SummaryKind : unsigned { AliasKind, FunctionKind, GlobalVarKind };
257 /// Group flags (Linkage, NotEligibleToImport, etc.) as a bitfield.
259 /// The linkage type of the associated global value.
261 /// One use is to flag values that have local linkage types and need to
262 /// have module identifier appended before placing into the combined
263 /// index, to disambiguate from other values with the same name.
264 /// In the future this will be used to update and optimize linkage
265 /// types based on global summary-based analysis.
266 unsigned Linkage : 4;
268 /// Indicate if the global value cannot be imported (e.g. it cannot
269 /// be renamed or references something that can't be renamed).
270 unsigned NotEligibleToImport : 1;
272 /// In per-module summary, indicate that the global value must be considered
273 /// a live root for index-based liveness analysis. Used for special LLVM
274 /// values such as llvm.global_ctors that the linker does not know about.
276 /// In combined summary, indicate that the global value is live.
279 /// Indicates that the linker resolved the symbol to a definition from
280 /// within the same linkage unit.
281 unsigned DSOLocal : 1;
283 /// Convenience Constructors
284 explicit GVFlags(GlobalValue::LinkageTypes Linkage,
285 bool NotEligibleToImport, bool Live, bool IsLocal)
286 : Linkage(Linkage), NotEligibleToImport(NotEligibleToImport),
287 Live(Live), DSOLocal(IsLocal) {}
291 /// Kind of summary for use in dyn_cast<> et al.
296 /// This is the hash of the name of the symbol in the original file. It is
297 /// identical to the GUID for global symbols, but differs for local since the
298 /// GUID includes the module level id in the hash.
299 GlobalValue::GUID OriginalName = 0;
301 /// Path of module IR containing value's definition, used to locate
302 /// module during importing.
304 /// This is only used during parsing of the combined index, or when
305 /// parsing the per-module index for creation of the combined summary index,
306 /// not during writing of the per-module index which doesn't contain a
307 /// module path string table.
308 StringRef ModulePath;
310 /// List of values referenced by this global value's definition
311 /// (either by the initializer of a global variable, or referenced
312 /// from within a function). This does not include functions called, which
313 /// are listed in the derived FunctionSummary object.
314 std::vector<ValueInfo> RefEdgeList;
317 GlobalValueSummary(SummaryKind K, GVFlags Flags, std::vector<ValueInfo> Refs)
318 : Kind(K), Flags(Flags), RefEdgeList(std::move(Refs)) {
319 assert((K != AliasKind || Refs.empty()) &&
320 "Expect no references for AliasSummary");
324 virtual ~GlobalValueSummary() = default;
326 /// Returns the hash of the original name, it is identical to the GUID for
327 /// externally visible symbols, but not for local ones.
328 GlobalValue::GUID getOriginalName() const { return OriginalName; }
330 /// Initialize the original name hash in this summary.
331 void setOriginalName(GlobalValue::GUID Name) { OriginalName = Name; }
333 /// Which kind of summary subclass this is.
334 SummaryKind getSummaryKind() const { return Kind; }
336 /// Set the path to the module containing this function, for use in
337 /// the combined index.
338 void setModulePath(StringRef ModPath) { ModulePath = ModPath; }
340 /// Get the path to the module containing this function.
341 StringRef modulePath() const { return ModulePath; }
343 /// Get the flags for this GlobalValue (see \p struct GVFlags).
344 GVFlags flags() const { return Flags; }
346 /// Return linkage type recorded for this global value.
347 GlobalValue::LinkageTypes linkage() const {
348 return static_cast<GlobalValue::LinkageTypes>(Flags.Linkage);
351 /// Sets the linkage to the value determined by global summary-based
352 /// optimization. Will be applied in the ThinLTO backends.
353 void setLinkage(GlobalValue::LinkageTypes Linkage) {
354 Flags.Linkage = Linkage;
357 /// Return true if this global value can't be imported.
358 bool notEligibleToImport() const { return Flags.NotEligibleToImport; }
360 bool isLive() const { return Flags.Live; }
362 void setLive(bool Live) { Flags.Live = Live; }
364 void setDSOLocal(bool Local) { Flags.DSOLocal = Local; }
366 bool isDSOLocal() const { return Flags.DSOLocal; }
368 /// Flag that this global value cannot be imported.
369 void setNotEligibleToImport() { Flags.NotEligibleToImport = true; }
371 /// Return the list of values referenced by this global value definition.
372 ArrayRef<ValueInfo> refs() const { return RefEdgeList; }
374 /// If this is an alias summary, returns the summary of the aliased object (a
375 /// global variable or function), otherwise returns itself.
376 GlobalValueSummary *getBaseObject();
377 const GlobalValueSummary *getBaseObject() const;
379 friend class ModuleSummaryIndex;
382 /// Alias summary information.
383 class AliasSummary : public GlobalValueSummary {
384 GlobalValueSummary *AliaseeSummary;
385 // AliaseeGUID is only set and accessed when we are building a combined index
386 // via the BitcodeReader.
387 GlobalValue::GUID AliaseeGUID;
390 AliasSummary(GVFlags Flags)
391 : GlobalValueSummary(AliasKind, Flags, ArrayRef<ValueInfo>{}),
392 AliaseeSummary(nullptr), AliaseeGUID(0) {}
394 /// Check if this is an alias summary.
395 static bool classof(const GlobalValueSummary *GVS) {
396 return GVS->getSummaryKind() == AliasKind;
399 void setAliasee(GlobalValueSummary *Aliasee) { AliaseeSummary = Aliasee; }
400 void setAliaseeGUID(GlobalValue::GUID GUID) { AliaseeGUID = GUID; }
402 bool hasAliasee() const { return !!AliaseeSummary; }
404 const GlobalValueSummary &getAliasee() const {
405 assert(AliaseeSummary && "Unexpected missing aliasee summary");
406 return *AliaseeSummary;
409 GlobalValueSummary &getAliasee() {
410 return const_cast<GlobalValueSummary &>(
411 static_cast<const AliasSummary *>(this)->getAliasee());
413 bool hasAliaseeGUID() const { return AliaseeGUID != 0; }
414 const GlobalValue::GUID &getAliaseeGUID() const {
415 assert(AliaseeGUID && "Unexpected missing aliasee GUID");
420 const inline GlobalValueSummary *GlobalValueSummary::getBaseObject() const {
421 if (auto *AS = dyn_cast<AliasSummary>(this))
422 return &AS->getAliasee();
426 inline GlobalValueSummary *GlobalValueSummary::getBaseObject() {
427 if (auto *AS = dyn_cast<AliasSummary>(this))
428 return &AS->getAliasee();
432 /// Function summary information to aid decisions and implementation of
434 class FunctionSummary : public GlobalValueSummary {
436 /// <CalleeValueInfo, CalleeInfo> call edge pair.
437 using EdgeTy = std::pair<ValueInfo, CalleeInfo>;
439 /// Types for -force-summary-edges-cold debugging option.
440 enum ForceSummaryHotnessType : unsigned {
446 /// An "identifier" for a virtual function. This contains the type identifier
447 /// represented as a GUID and the offset from the address point to the virtual
448 /// function pointer, where "address point" is as defined in the Itanium ABI:
449 /// https://itanium-cxx-abi.github.io/cxx-abi/abi.html#vtable-general
451 GlobalValue::GUID GUID;
455 /// A specification for a virtual function call with all constant integer
456 /// arguments. This is used to perform virtual constant propagation on the
460 std::vector<uint64_t> Args;
463 /// All type identifier related information. Because these fields are
464 /// relatively uncommon we only allocate space for them if necessary.
466 /// List of type identifiers used by this function in llvm.type.test
467 /// intrinsics referenced by something other than an llvm.assume intrinsic,
468 /// represented as GUIDs.
469 std::vector<GlobalValue::GUID> TypeTests;
471 /// List of virtual calls made by this function using (respectively)
472 /// llvm.assume(llvm.type.test) or llvm.type.checked.load intrinsics that do
473 /// not have all constant integer arguments.
474 std::vector<VFuncId> TypeTestAssumeVCalls, TypeCheckedLoadVCalls;
476 /// List of virtual calls made by this function using (respectively)
477 /// llvm.assume(llvm.type.test) or llvm.type.checked.load intrinsics with
478 /// all constant integer arguments.
479 std::vector<ConstVCall> TypeTestAssumeConstVCalls,
480 TypeCheckedLoadConstVCalls;
483 /// Flags specific to function summaries.
485 // Function attribute flags. Used to track if a function accesses memory,
486 // recurses or aliases.
487 unsigned ReadNone : 1;
488 unsigned ReadOnly : 1;
489 unsigned NoRecurse : 1;
490 unsigned ReturnDoesNotAlias : 1;
492 // Indicate if the global value cannot be inlined.
493 unsigned NoInline : 1;
496 /// Create an empty FunctionSummary (with specified call edges).
497 /// Used to represent external nodes and the dummy root node.
498 static FunctionSummary
499 makeDummyFunctionSummary(std::vector<FunctionSummary::EdgeTy> Edges) {
500 return FunctionSummary(
501 FunctionSummary::GVFlags(
502 GlobalValue::LinkageTypes::AvailableExternallyLinkage,
503 /*NotEligibleToImport=*/true, /*Live=*/true, /*IsLocal=*/false),
504 /*InsCount=*/0, FunctionSummary::FFlags{}, /*EntryCount=*/0,
505 std::vector<ValueInfo>(), std::move(Edges),
506 std::vector<GlobalValue::GUID>(),
507 std::vector<FunctionSummary::VFuncId>(),
508 std::vector<FunctionSummary::VFuncId>(),
509 std::vector<FunctionSummary::ConstVCall>(),
510 std::vector<FunctionSummary::ConstVCall>());
513 /// A dummy node to reference external functions that aren't in the index
514 static FunctionSummary ExternalNode;
517 /// Number of instructions (ignoring debug instructions, e.g.) computed
518 /// during the initial compile step when the summary index is first built.
521 /// Function summary specific flags.
524 /// The synthesized entry count of the function.
525 /// This is only populated during ThinLink phase and remains unused while
526 /// generating per-module summaries.
527 uint64_t EntryCount = 0;
529 /// List of <CalleeValueInfo, CalleeInfo> call edge pairs from this function.
530 std::vector<EdgeTy> CallGraphEdgeList;
532 std::unique_ptr<TypeIdInfo> TIdInfo;
535 FunctionSummary(GVFlags Flags, unsigned NumInsts, FFlags FunFlags,
536 uint64_t EntryCount, std::vector<ValueInfo> Refs,
537 std::vector<EdgeTy> CGEdges,
538 std::vector<GlobalValue::GUID> TypeTests,
539 std::vector<VFuncId> TypeTestAssumeVCalls,
540 std::vector<VFuncId> TypeCheckedLoadVCalls,
541 std::vector<ConstVCall> TypeTestAssumeConstVCalls,
542 std::vector<ConstVCall> TypeCheckedLoadConstVCalls)
543 : GlobalValueSummary(FunctionKind, Flags, std::move(Refs)),
544 InstCount(NumInsts), FunFlags(FunFlags), EntryCount(EntryCount),
545 CallGraphEdgeList(std::move(CGEdges)) {
546 if (!TypeTests.empty() || !TypeTestAssumeVCalls.empty() ||
547 !TypeCheckedLoadVCalls.empty() || !TypeTestAssumeConstVCalls.empty() ||
548 !TypeCheckedLoadConstVCalls.empty())
549 TIdInfo = llvm::make_unique<TypeIdInfo>(TypeIdInfo{
550 std::move(TypeTests), std::move(TypeTestAssumeVCalls),
551 std::move(TypeCheckedLoadVCalls),
552 std::move(TypeTestAssumeConstVCalls),
553 std::move(TypeCheckedLoadConstVCalls)});
555 // Gets the number of immutable refs in RefEdgeList
556 unsigned immutableRefCount() const;
558 /// Check if this is a function summary.
559 static bool classof(const GlobalValueSummary *GVS) {
560 return GVS->getSummaryKind() == FunctionKind;
563 /// Get function summary flags.
564 FFlags fflags() const { return FunFlags; }
566 /// Get the instruction count recorded for this function.
567 unsigned instCount() const { return InstCount; }
569 /// Get the synthetic entry count for this function.
570 uint64_t entryCount() const { return EntryCount; }
572 /// Set the synthetic entry count for this function.
573 void setEntryCount(uint64_t EC) { EntryCount = EC; }
575 /// Return the list of <CalleeValueInfo, CalleeInfo> pairs.
576 ArrayRef<EdgeTy> calls() const { return CallGraphEdgeList; }
578 /// Returns the list of type identifiers used by this function in
579 /// llvm.type.test intrinsics other than by an llvm.assume intrinsic,
580 /// represented as GUIDs.
581 ArrayRef<GlobalValue::GUID> type_tests() const {
583 return TIdInfo->TypeTests;
587 /// Returns the list of virtual calls made by this function using
588 /// llvm.assume(llvm.type.test) intrinsics that do not have all constant
589 /// integer arguments.
590 ArrayRef<VFuncId> type_test_assume_vcalls() const {
592 return TIdInfo->TypeTestAssumeVCalls;
596 /// Returns the list of virtual calls made by this function using
597 /// llvm.type.checked.load intrinsics that do not have all constant integer
599 ArrayRef<VFuncId> type_checked_load_vcalls() const {
601 return TIdInfo->TypeCheckedLoadVCalls;
605 /// Returns the list of virtual calls made by this function using
606 /// llvm.assume(llvm.type.test) intrinsics with all constant integer
608 ArrayRef<ConstVCall> type_test_assume_const_vcalls() const {
610 return TIdInfo->TypeTestAssumeConstVCalls;
614 /// Returns the list of virtual calls made by this function using
615 /// llvm.type.checked.load intrinsics with all constant integer arguments.
616 ArrayRef<ConstVCall> type_checked_load_const_vcalls() const {
618 return TIdInfo->TypeCheckedLoadConstVCalls;
622 /// Add a type test to the summary. This is used by WholeProgramDevirt if we
623 /// were unable to devirtualize a checked call.
624 void addTypeTest(GlobalValue::GUID Guid) {
626 TIdInfo = llvm::make_unique<TypeIdInfo>();
627 TIdInfo->TypeTests.push_back(Guid);
630 const TypeIdInfo *getTypeIdInfo() const { return TIdInfo.get(); };
632 friend struct GraphTraits<ValueInfo>;
635 template <> struct DenseMapInfo<FunctionSummary::VFuncId> {
636 static FunctionSummary::VFuncId getEmptyKey() { return {0, uint64_t(-1)}; }
638 static FunctionSummary::VFuncId getTombstoneKey() {
639 return {0, uint64_t(-2)};
642 static bool isEqual(FunctionSummary::VFuncId L, FunctionSummary::VFuncId R) {
643 return L.GUID == R.GUID && L.Offset == R.Offset;
646 static unsigned getHashValue(FunctionSummary::VFuncId I) { return I.GUID; }
649 template <> struct DenseMapInfo<FunctionSummary::ConstVCall> {
650 static FunctionSummary::ConstVCall getEmptyKey() {
651 return {{0, uint64_t(-1)}, {}};
654 static FunctionSummary::ConstVCall getTombstoneKey() {
655 return {{0, uint64_t(-2)}, {}};
658 static bool isEqual(FunctionSummary::ConstVCall L,
659 FunctionSummary::ConstVCall R) {
660 return DenseMapInfo<FunctionSummary::VFuncId>::isEqual(L.VFunc, R.VFunc) &&
664 static unsigned getHashValue(FunctionSummary::ConstVCall I) {
669 /// Global variable summary information to aid decisions and
670 /// implementation of importing.
672 /// Global variable summary has extra flag, telling if it is
673 /// modified during the program run or not. This affects ThinLTO
675 class GlobalVarSummary : public GlobalValueSummary {
678 GVarFlags(bool ReadOnly = false) : ReadOnly(ReadOnly) {}
680 unsigned ReadOnly : 1;
683 GlobalVarSummary(GVFlags Flags, GVarFlags VarFlags,
684 std::vector<ValueInfo> Refs)
685 : GlobalValueSummary(GlobalVarKind, Flags, std::move(Refs)),
686 VarFlags(VarFlags) {}
688 /// Check if this is a global variable summary.
689 static bool classof(const GlobalValueSummary *GVS) {
690 return GVS->getSummaryKind() == GlobalVarKind;
693 GVarFlags varflags() const { return VarFlags; }
694 void setReadOnly(bool RO) { VarFlags.ReadOnly = RO; }
695 bool isReadOnly() const { return VarFlags.ReadOnly; }
698 struct TypeTestResolution {
699 /// Specifies which kind of type check we should emit for this byte array.
700 /// See http://clang.llvm.org/docs/ControlFlowIntegrityDesign.html for full
701 /// details on each kind of check; the enumerators are described with
702 /// reference to that document.
704 Unsat, ///< Unsatisfiable type (i.e. no global has this type metadata)
705 ByteArray, ///< Test a byte array (first example)
706 Inline, ///< Inlined bit vector ("Short Inline Bit Vectors")
707 Single, ///< Single element (last example in "Short Inline Bit Vectors")
708 AllOnes, ///< All-ones bit vector ("Eliminating Bit Vector Checks for
709 /// All-Ones Bit Vectors")
712 /// Range of size-1 expressed as a bit width. For example, if the size is in
713 /// range [1,256], this number will be 8. This helps generate the most compact
714 /// instruction sequences.
715 unsigned SizeM1BitWidth = 0;
717 // The following fields are only used if the target does not support the use
718 // of absolute symbols to store constants. Their meanings are the same as the
719 // corresponding fields in LowerTypeTestsModule::TypeIdLowering in
720 // LowerTypeTests.cpp.
722 uint64_t AlignLog2 = 0;
725 uint64_t InlineBits = 0;
728 struct WholeProgramDevirtResolution {
730 Indir, ///< Just do a regular virtual call
731 SingleImpl, ///< Single implementation devirtualization
732 BranchFunnel, ///< When retpoline mitigation is enabled, use a branch funnel
733 ///< that is defined in the merged module. Otherwise same as
737 std::string SingleImplName;
741 Indir, ///< Just do a regular virtual call
742 UniformRetVal, ///< Uniform return value optimization
743 UniqueRetVal, ///< Unique return value optimization
744 VirtualConstProp, ///< Virtual constant propagation
747 /// Additional information for the resolution:
748 /// - UniformRetVal: the uniform return value.
749 /// - UniqueRetVal: the return value associated with the unique vtable (0 or
753 // The following fields are only used if the target does not support the use
754 // of absolute symbols to store constants.
760 /// Resolutions for calls with all constant integer arguments (excluding the
761 /// first argument, "this"), where the key is the argument vector.
762 std::map<std::vector<uint64_t>, ByArg> ResByArg;
765 struct TypeIdSummary {
766 TypeTestResolution TTRes;
768 /// Mapping from byte offset to whole-program devirt resolution for that
769 /// (typeid, byte offset) pair.
770 std::map<uint64_t, WholeProgramDevirtResolution> WPDRes;
774 using ModuleHash = std::array<uint32_t, 5>;
776 /// Type used for iterating through the global value summary map.
777 using const_gvsummary_iterator = GlobalValueSummaryMapTy::const_iterator;
778 using gvsummary_iterator = GlobalValueSummaryMapTy::iterator;
780 /// String table to hold/own module path strings, which additionally holds the
781 /// module ID assigned to each module during the plugin step, as well as a hash
782 /// of the module. The StringMap makes a copy of and owns inserted strings.
783 using ModulePathStringTableTy = StringMap<std::pair<uint64_t, ModuleHash>>;
785 /// Map of global value GUID to its summary, used to identify values defined in
786 /// a particular module, and provide efficient access to their summary.
787 using GVSummaryMapTy = DenseMap<GlobalValue::GUID, GlobalValueSummary *>;
789 /// Map of a type GUID to type id string and summary (multimap used
790 /// in case of GUID conflicts).
791 using TypeIdSummaryMapTy =
792 std::multimap<GlobalValue::GUID, std::pair<std::string, TypeIdSummary>>;
794 /// Class to hold module path string table and global value map,
795 /// and encapsulate methods for operating on them.
796 class ModuleSummaryIndex {
798 /// Map from value name to list of summary instances for values of that
799 /// name (may be duplicates in the COMDAT case, e.g.).
800 GlobalValueSummaryMapTy GlobalValueMap;
802 /// Holds strings for combined index, mapping to the corresponding module ID.
803 ModulePathStringTableTy ModulePathStringTable;
805 /// Mapping from type identifier GUIDs to type identifier and its summary
807 TypeIdSummaryMapTy TypeIdMap;
809 /// Mapping from original ID to GUID. If original ID can map to multiple
810 /// GUIDs, it will be mapped to 0.
811 std::map<GlobalValue::GUID, GlobalValue::GUID> OidGuidMap;
813 /// Indicates that summary-based GlobalValue GC has run, and values with
814 /// GVFlags::Live==false are really dead. Otherwise, all values must be
816 bool WithGlobalValueDeadStripping = false;
818 /// Indicates that summary-based synthetic entry count propagation has run
819 bool HasSyntheticEntryCounts = false;
821 /// Indicates that distributed backend should skip compilation of the
822 /// module. Flag is suppose to be set by distributed ThinLTO indexing
823 /// when it detected that the module is not needed during the final
824 /// linking. As result distributed backend should just output a minimal
825 /// valid object file.
826 bool SkipModuleByDistributedBackend = false;
828 /// If true then we're performing analysis of IR module, or parsing along with
829 /// the IR from assembly. The value of 'false' means we're reading summary
830 /// from BC or YAML source. Affects the type of value stored in NameOrGV
834 // True if the index was created for a module compiled with -fsplit-lto-unit.
835 bool EnableSplitLTOUnit;
837 // True if some of the modules were compiled with -fsplit-lto-unit and
838 // some were not. Set when the combined index is created during the thin link.
839 bool PartiallySplitLTOUnits = false;
841 std::set<std::string> CfiFunctionDefs;
842 std::set<std::string> CfiFunctionDecls;
844 // Used in cases where we want to record the name of a global, but
845 // don't have the string owned elsewhere (e.g. the Strtab on a module).
847 BumpPtrAllocator Alloc;
850 friend yaml::MappingTraits<ModuleSummaryIndex>;
852 GlobalValueSummaryMapTy::value_type *
853 getOrInsertValuePtr(GlobalValue::GUID GUID) {
854 return &*GlobalValueMap.emplace(GUID, GlobalValueSummaryInfo(HaveGVs))
859 // See HaveGVs variable comment.
860 ModuleSummaryIndex(bool HaveGVs, bool EnableSplitLTOUnit = false)
861 : HaveGVs(HaveGVs), EnableSplitLTOUnit(EnableSplitLTOUnit), Saver(Alloc) {
864 bool haveGVs() const { return HaveGVs; }
866 gvsummary_iterator begin() { return GlobalValueMap.begin(); }
867 const_gvsummary_iterator begin() const { return GlobalValueMap.begin(); }
868 gvsummary_iterator end() { return GlobalValueMap.end(); }
869 const_gvsummary_iterator end() const { return GlobalValueMap.end(); }
870 size_t size() const { return GlobalValueMap.size(); }
872 /// Convenience function for doing a DFS on a ValueInfo. Marks the function in
873 /// the FunctionHasParent map.
874 static void discoverNodes(ValueInfo V,
875 std::map<ValueInfo, bool> &FunctionHasParent) {
876 if (!V.getSummaryList().size())
877 return; // skip external functions that don't have summaries
879 // Mark discovered if we haven't yet
880 auto S = FunctionHasParent.emplace(V, false);
882 // Stop if we've already discovered this node
887 dyn_cast<FunctionSummary>(V.getSummaryList().front().get());
888 assert(F != nullptr && "Expected FunctionSummary node");
890 for (auto &C : F->calls()) {
891 // Insert node if necessary
892 auto S = FunctionHasParent.emplace(C.first, true);
894 // Skip nodes that we're sure have parents
895 if (!S.second && S.first->second)
899 discoverNodes(C.first, FunctionHasParent);
901 S.first->second = true;
905 // Calculate the callgraph root
906 FunctionSummary calculateCallGraphRoot() {
907 // Functions that have a parent will be marked in FunctionHasParent pair.
908 // Once we've marked all functions, the functions in the map that are false
909 // have no parent (so they're the roots)
910 std::map<ValueInfo, bool> FunctionHasParent;
912 for (auto &S : *this) {
913 // Skip external functions
914 if (!S.second.SummaryList.size() ||
915 !isa<FunctionSummary>(S.second.SummaryList.front().get()))
917 discoverNodes(ValueInfo(HaveGVs, &S), FunctionHasParent);
920 std::vector<FunctionSummary::EdgeTy> Edges;
921 // create edges to all roots in the Index
922 for (auto &P : FunctionHasParent) {
924 continue; // skip over non-root nodes
925 Edges.push_back(std::make_pair(P.first, CalleeInfo{}));
928 // Failed to find root - return an empty node
929 return FunctionSummary::makeDummyFunctionSummary({});
931 auto CallGraphRoot = FunctionSummary::makeDummyFunctionSummary(Edges);
932 return CallGraphRoot;
935 bool withGlobalValueDeadStripping() const {
936 return WithGlobalValueDeadStripping;
938 void setWithGlobalValueDeadStripping() {
939 WithGlobalValueDeadStripping = true;
942 bool hasSyntheticEntryCounts() const { return HasSyntheticEntryCounts; }
943 void setHasSyntheticEntryCounts() { HasSyntheticEntryCounts = true; }
945 bool skipModuleByDistributedBackend() const {
946 return SkipModuleByDistributedBackend;
948 void setSkipModuleByDistributedBackend() {
949 SkipModuleByDistributedBackend = true;
952 bool enableSplitLTOUnit() const { return EnableSplitLTOUnit; }
953 void setEnableSplitLTOUnit() { EnableSplitLTOUnit = true; }
955 bool partiallySplitLTOUnits() const { return PartiallySplitLTOUnits; }
956 void setPartiallySplitLTOUnits() { PartiallySplitLTOUnits = true; }
958 bool isGlobalValueLive(const GlobalValueSummary *GVS) const {
959 return !WithGlobalValueDeadStripping || GVS->isLive();
961 bool isGUIDLive(GlobalValue::GUID GUID) const;
963 /// Return a ValueInfo for the index value_type (convenient when iterating
965 ValueInfo getValueInfo(const GlobalValueSummaryMapTy::value_type &R) const {
966 return ValueInfo(HaveGVs, &R);
969 /// Return a ValueInfo for GUID if it exists, otherwise return ValueInfo().
970 ValueInfo getValueInfo(GlobalValue::GUID GUID) const {
971 auto I = GlobalValueMap.find(GUID);
972 return ValueInfo(HaveGVs, I == GlobalValueMap.end() ? nullptr : &*I);
975 /// Return a ValueInfo for \p GUID.
976 ValueInfo getOrInsertValueInfo(GlobalValue::GUID GUID) {
977 return ValueInfo(HaveGVs, getOrInsertValuePtr(GUID));
980 // Save a string in the Index. Use before passing Name to
981 // getOrInsertValueInfo when the string isn't owned elsewhere (e.g. on the
983 StringRef saveString(StringRef String) { return Saver.save(String); }
985 /// Return a ValueInfo for \p GUID setting value \p Name.
986 ValueInfo getOrInsertValueInfo(GlobalValue::GUID GUID, StringRef Name) {
988 auto VP = getOrInsertValuePtr(GUID);
989 VP->second.U.Name = Name;
990 return ValueInfo(HaveGVs, VP);
993 /// Return a ValueInfo for \p GV and mark it as belonging to GV.
994 ValueInfo getOrInsertValueInfo(const GlobalValue *GV) {
996 auto VP = getOrInsertValuePtr(GV->getGUID());
997 VP->second.U.GV = GV;
998 return ValueInfo(HaveGVs, VP);
1001 /// Return the GUID for \p OriginalId in the OidGuidMap.
1002 GlobalValue::GUID getGUIDFromOriginalID(GlobalValue::GUID OriginalID) const {
1003 const auto I = OidGuidMap.find(OriginalID);
1004 return I == OidGuidMap.end() ? 0 : I->second;
1007 std::set<std::string> &cfiFunctionDefs() { return CfiFunctionDefs; }
1008 const std::set<std::string> &cfiFunctionDefs() const { return CfiFunctionDefs; }
1010 std::set<std::string> &cfiFunctionDecls() { return CfiFunctionDecls; }
1011 const std::set<std::string> &cfiFunctionDecls() const { return CfiFunctionDecls; }
1013 /// Add a global value summary for a value.
1014 void addGlobalValueSummary(const GlobalValue &GV,
1015 std::unique_ptr<GlobalValueSummary> Summary) {
1016 addGlobalValueSummary(getOrInsertValueInfo(&GV), std::move(Summary));
1019 /// Add a global value summary for a value of the given name.
1020 void addGlobalValueSummary(StringRef ValueName,
1021 std::unique_ptr<GlobalValueSummary> Summary) {
1022 addGlobalValueSummary(getOrInsertValueInfo(GlobalValue::getGUID(ValueName)),
1023 std::move(Summary));
1026 /// Add a global value summary for the given ValueInfo.
1027 void addGlobalValueSummary(ValueInfo VI,
1028 std::unique_ptr<GlobalValueSummary> Summary) {
1029 addOriginalName(VI.getGUID(), Summary->getOriginalName());
1030 // Here we have a notionally const VI, but the value it points to is owned
1031 // by the non-const *this.
1032 const_cast<GlobalValueSummaryMapTy::value_type *>(VI.getRef())
1033 ->second.SummaryList.push_back(std::move(Summary));
1036 /// Add an original name for the value of the given GUID.
1037 void addOriginalName(GlobalValue::GUID ValueGUID,
1038 GlobalValue::GUID OrigGUID) {
1039 if (OrigGUID == 0 || ValueGUID == OrigGUID)
1041 if (OidGuidMap.count(OrigGUID) && OidGuidMap[OrigGUID] != ValueGUID)
1042 OidGuidMap[OrigGUID] = 0;
1044 OidGuidMap[OrigGUID] = ValueGUID;
1047 /// Find the summary for global \p GUID in module \p ModuleId, or nullptr if
1049 GlobalValueSummary *findSummaryInModule(GlobalValue::GUID ValueGUID,
1050 StringRef ModuleId) const {
1051 auto CalleeInfo = getValueInfo(ValueGUID);
1053 return nullptr; // This function does not have a summary
1056 llvm::find_if(CalleeInfo.getSummaryList(),
1057 [&](const std::unique_ptr<GlobalValueSummary> &Summary) {
1058 return Summary->modulePath() == ModuleId;
1060 if (Summary == CalleeInfo.getSummaryList().end())
1062 return Summary->get();
1065 /// Returns the first GlobalValueSummary for \p GV, asserting that there
1066 /// is only one if \p PerModuleIndex.
1067 GlobalValueSummary *getGlobalValueSummary(const GlobalValue &GV,
1068 bool PerModuleIndex = true) const {
1069 assert(GV.hasName() && "Can't get GlobalValueSummary for GV with no name");
1070 return getGlobalValueSummary(GV.getGUID(), PerModuleIndex);
1073 /// Returns the first GlobalValueSummary for \p ValueGUID, asserting that
1075 /// is only one if \p PerModuleIndex.
1076 GlobalValueSummary *getGlobalValueSummary(GlobalValue::GUID ValueGUID,
1077 bool PerModuleIndex = true) const;
1079 /// Table of modules, containing module hash and id.
1080 const StringMap<std::pair<uint64_t, ModuleHash>> &modulePaths() const {
1081 return ModulePathStringTable;
1084 /// Table of modules, containing hash and id.
1085 StringMap<std::pair<uint64_t, ModuleHash>> &modulePaths() {
1086 return ModulePathStringTable;
1089 /// Get the module ID recorded for the given module path.
1090 uint64_t getModuleId(const StringRef ModPath) const {
1091 return ModulePathStringTable.lookup(ModPath).first;
1094 /// Get the module SHA1 hash recorded for the given module path.
1095 const ModuleHash &getModuleHash(const StringRef ModPath) const {
1096 auto It = ModulePathStringTable.find(ModPath);
1097 assert(It != ModulePathStringTable.end() && "Module not registered");
1098 return It->second.second;
1101 /// Convenience method for creating a promoted global name
1102 /// for the given value name of a local, and its original module's ID.
1103 static std::string getGlobalNameForLocal(StringRef Name, ModuleHash ModHash) {
1104 SmallString<256> NewName(Name);
1105 NewName += ".llvm.";
1106 NewName += utostr((uint64_t(ModHash[0]) << 32) |
1107 ModHash[1]); // Take the first 64 bits
1108 return NewName.str();
1111 /// Helper to obtain the unpromoted name for a global value (or the original
1112 /// name if not promoted).
1113 static StringRef getOriginalNameBeforePromote(StringRef Name) {
1114 std::pair<StringRef, StringRef> Pair = Name.split(".llvm.");
1118 typedef ModulePathStringTableTy::value_type ModuleInfo;
1120 /// Add a new module with the given \p Hash, mapped to the given \p
1121 /// ModID, and return a reference to the module.
1122 ModuleInfo *addModule(StringRef ModPath, uint64_t ModId,
1123 ModuleHash Hash = ModuleHash{{0}}) {
1124 return &*ModulePathStringTable.insert({ModPath, {ModId, Hash}}).first;
1127 /// Return module entry for module with the given \p ModPath.
1128 ModuleInfo *getModule(StringRef ModPath) {
1129 auto It = ModulePathStringTable.find(ModPath);
1130 assert(It != ModulePathStringTable.end() && "Module not registered");
1134 /// Check if the given Module has any functions available for exporting
1135 /// in the index. We consider any module present in the ModulePathStringTable
1136 /// to have exported functions.
1137 bool hasExportedFunctions(const Module &M) const {
1138 return ModulePathStringTable.count(M.getModuleIdentifier());
1141 const TypeIdSummaryMapTy &typeIds() const { return TypeIdMap; }
1143 /// Return an existing or new TypeIdSummary entry for \p TypeId.
1144 /// This accessor can mutate the map and therefore should not be used in
1145 /// the ThinLTO backends.
1146 TypeIdSummary &getOrInsertTypeIdSummary(StringRef TypeId) {
1147 auto TidIter = TypeIdMap.equal_range(GlobalValue::getGUID(TypeId));
1148 for (auto It = TidIter.first; It != TidIter.second; ++It)
1149 if (It->second.first == TypeId)
1150 return It->second.second;
1151 auto It = TypeIdMap.insert(
1152 {GlobalValue::getGUID(TypeId), {TypeId, TypeIdSummary()}});
1153 return It->second.second;
1156 /// This returns either a pointer to the type id summary (if present in the
1157 /// summary map) or null (if not present). This may be used when importing.
1158 const TypeIdSummary *getTypeIdSummary(StringRef TypeId) const {
1159 auto TidIter = TypeIdMap.equal_range(GlobalValue::getGUID(TypeId));
1160 for (auto It = TidIter.first; It != TidIter.second; ++It)
1161 if (It->second.first == TypeId)
1162 return &It->second.second;
1166 /// Collect for the given module the list of functions it defines
1167 /// (GUID -> Summary).
1168 void collectDefinedFunctionsForModule(StringRef ModulePath,
1169 GVSummaryMapTy &GVSummaryMap) const;
1171 /// Collect for each module the list of Summaries it defines (GUID ->
1173 void collectDefinedGVSummariesPerModule(
1174 StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries) const;
1176 /// Print to an output stream.
1177 void print(raw_ostream &OS, bool IsForDebug = false) const;
1179 /// Dump to stderr (for debugging).
1182 /// Export summary to dot file for GraphViz.
1183 void exportToDot(raw_ostream& OS) const;
1185 /// Print out strongly connected components for debugging.
1186 void dumpSCCs(raw_ostream &OS);
1188 /// Analyze index and detect unmodified globals
1189 void propagateConstants(const DenseSet<GlobalValue::GUID> &PreservedSymbols);
1192 /// GraphTraits definition to build SCC for the index
1193 template <> struct GraphTraits<ValueInfo> {
1194 typedef ValueInfo NodeRef;
1195 using EdgeRef = FunctionSummary::EdgeTy &;
1197 static NodeRef valueInfoFromEdge(FunctionSummary::EdgeTy &P) {
1200 using ChildIteratorType =
1201 mapped_iterator<std::vector<FunctionSummary::EdgeTy>::iterator,
1202 decltype(&valueInfoFromEdge)>;
1204 using ChildEdgeIteratorType = std::vector<FunctionSummary::EdgeTy>::iterator;
1206 static NodeRef getEntryNode(ValueInfo V) { return V; }
1208 static ChildIteratorType child_begin(NodeRef N) {
1209 if (!N.getSummaryList().size()) // handle external function
1210 return ChildIteratorType(
1211 FunctionSummary::ExternalNode.CallGraphEdgeList.begin(),
1212 &valueInfoFromEdge);
1213 FunctionSummary *F =
1214 cast<FunctionSummary>(N.getSummaryList().front()->getBaseObject());
1215 return ChildIteratorType(F->CallGraphEdgeList.begin(), &valueInfoFromEdge);
1218 static ChildIteratorType child_end(NodeRef N) {
1219 if (!N.getSummaryList().size()) // handle external function
1220 return ChildIteratorType(
1221 FunctionSummary::ExternalNode.CallGraphEdgeList.end(),
1222 &valueInfoFromEdge);
1223 FunctionSummary *F =
1224 cast<FunctionSummary>(N.getSummaryList().front()->getBaseObject());
1225 return ChildIteratorType(F->CallGraphEdgeList.end(), &valueInfoFromEdge);
1228 static ChildEdgeIteratorType child_edge_begin(NodeRef N) {
1229 if (!N.getSummaryList().size()) // handle external function
1230 return FunctionSummary::ExternalNode.CallGraphEdgeList.begin();
1232 FunctionSummary *F =
1233 cast<FunctionSummary>(N.getSummaryList().front()->getBaseObject());
1234 return F->CallGraphEdgeList.begin();
1237 static ChildEdgeIteratorType child_edge_end(NodeRef N) {
1238 if (!N.getSummaryList().size()) // handle external function
1239 return FunctionSummary::ExternalNode.CallGraphEdgeList.end();
1241 FunctionSummary *F =
1242 cast<FunctionSummary>(N.getSummaryList().front()->getBaseObject());
1243 return F->CallGraphEdgeList.end();
1246 static NodeRef edge_dest(EdgeRef E) { return E.first; }
1250 struct GraphTraits<ModuleSummaryIndex *> : public GraphTraits<ValueInfo> {
1251 static NodeRef getEntryNode(ModuleSummaryIndex *I) {
1252 std::unique_ptr<GlobalValueSummary> Root =
1253 make_unique<FunctionSummary>(I->calculateCallGraphRoot());
1254 GlobalValueSummaryInfo G(I->haveGVs());
1255 G.SummaryList.push_back(std::move(Root));
1257 GlobalValueSummaryMapTy::value_type(GlobalValue::GUID(0), std::move(G));
1258 return ValueInfo(I->haveGVs(), &P);
1262 static inline bool canImportGlobalVar(GlobalValueSummary *S) {
1263 assert(isa<GlobalVarSummary>(S->getBaseObject()));
1265 // We don't import GV with references, because it can result
1266 // in promotion of local variables in the source module.
1267 return !GlobalValue::isInterposableLinkage(S->linkage()) &&
1268 !S->notEligibleToImport() && S->refs().empty();
1270 } // end namespace llvm
1272 #endif // LLVM_IR_MODULESUMMARYINDEX_H