LCOV - code coverage report
Current view: top level - include/llvm/IR - ModuleSummaryIndex.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 180 186 96.8 %
Date: 2018-06-17 00:07:59 Functions: 35 40 87.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- llvm/ModuleSummaryIndex.h - Module Summary 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             : /// ModuleSummaryIndex.h This file contains the declarations the classes that
      12             : ///  hold the module index and summary for function importing.
      13             : //
      14             : //===----------------------------------------------------------------------===//
      15             : 
      16             : #ifndef LLVM_IR_MODULESUMMARYINDEX_H
      17             : #define LLVM_IR_MODULESUMMARYINDEX_H
      18             : 
      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/IR/GlobalValue.h"
      27             : #include "llvm/IR/Module.h"
      28             : #include "llvm/Support/MathExtras.h"
      29             : #include "llvm/Support/ScaledNumber.h"
      30             : #include <algorithm>
      31             : #include <array>
      32             : #include <cassert>
      33             : #include <cstddef>
      34             : #include <cstdint>
      35             : #include <map>
      36             : #include <memory>
      37             : #include <set>
      38             : #include <string>
      39             : #include <utility>
      40             : #include <vector>
      41             : 
      42             : namespace llvm {
      43             : 
      44             : namespace yaml {
      45             : 
      46             : template <typename T> struct MappingTraits;
      47             : 
      48             : } // end namespace yaml
      49             : 
      50             : /// Class to accumulate and hold information about a callee.
      51             : struct CalleeInfo {
      52             :   enum class HotnessType : uint8_t {
      53             :     Unknown = 0,
      54             :     Cold = 1,
      55             :     None = 2,
      56             :     Hot = 3,
      57             :     Critical = 4
      58             :   };
      59             : 
      60             :   // The size of the bit-field might need to be adjusted if more values are
      61             :   // added to HotnessType enum.
      62             :   uint32_t Hotness : 3;
      63             : 
      64             :   /// The value stored in RelBlockFreq has to be interpreted as the digits of
      65             :   /// a scaled number with a scale of \p -ScaleShift.
      66             :   uint32_t RelBlockFreq : 29;
      67             :   static constexpr int32_t ScaleShift = 8;
      68             :   static constexpr uint64_t MaxRelBlockFreq = (1 << 29) - 1;
      69             : 
      70             :   CalleeInfo()
      71             :       : Hotness(static_cast<uint32_t>(HotnessType::Unknown)), RelBlockFreq(0) {}
      72             :   explicit CalleeInfo(HotnessType Hotness, uint64_t RelBF)
      73        1024 :       : Hotness(static_cast<uint32_t>(Hotness)), RelBlockFreq(RelBF) {}
      74             : 
      75             :   void updateHotness(const HotnessType OtherHotness) {
      76         676 :     Hotness = std::max(Hotness, static_cast<uint32_t>(OtherHotness));
      77             :   }
      78             : 
      79         981 :   HotnessType getHotness() const { return HotnessType(Hotness); }
      80             : 
      81             :   /// Update \p RelBlockFreq from \p BlockFreq and \p EntryFreq
      82             :   ///
      83             :   /// BlockFreq is divided by EntryFreq and added to RelBlockFreq. To represent
      84             :   /// fractional values, the result is represented as a fixed point number with
      85             :   /// scale of -ScaleShift.
      86         280 :   void updateRelBlockFreq(uint64_t BlockFreq, uint64_t EntryFreq) {
      87         280 :     if (EntryFreq == 0)
      88           0 :       return;
      89             :     using Scaled64 = ScaledNumber<uint64_t>;
      90             :     Scaled64 Temp(BlockFreq, ScaleShift);
      91         280 :     Temp /= Scaled64::get(EntryFreq);
      92             : 
      93             :     uint64_t Sum =
      94         560 :         SaturatingAdd<uint64_t>(Temp.toInt<uint64_t>(), RelBlockFreq);
      95         560 :     Sum = std::min(Sum, uint64_t(MaxRelBlockFreq));
      96         280 :     RelBlockFreq = static_cast<uint32_t>(Sum);
      97             :   }
      98             : };
      99             : 
     100             : class GlobalValueSummary;
     101             : 
     102             : using GlobalValueSummaryList = std::vector<std::unique_ptr<GlobalValueSummary>>;
     103             : 
     104       12234 : struct GlobalValueSummaryInfo {
     105             :   union NameOrGV {
     106        4096 :     NameOrGV(bool HaveGVs) {
     107        4096 :       if (HaveGVs)
     108        1410 :         GV = nullptr;
     109             :       else
     110        2686 :         Name = "";
     111             :     }
     112             : 
     113             :     /// The GlobalValue corresponding to this summary. This is only used in
     114             :     /// per-module summaries and when the IR is available. E.g. when module
     115             :     /// analysis is being run, or when parsing both the IR and the summary
     116             :     /// from assembly.
     117             :     const GlobalValue *GV;
     118             : 
     119             :     /// Summary string representation. This StringRef points to BC module
     120             :     /// string table and is valid until module data is stored in memory.
     121             :     /// This is guaranteed to happen until runThinLTOBackend function is
     122             :     /// called, so it is safe to use this field during thin link. This field
     123             :     /// is only valid if summary index was loaded from BC file.
     124             :     StringRef Name;
     125             :   } U;
     126             : 
     127        4096 :   GlobalValueSummaryInfo(bool HaveGVs) : U(HaveGVs) {}
     128             : 
     129             :   /// List of global value summary structures for a particular value held
     130             :   /// in the GlobalValueMap. Requires a vector in the case of multiple
     131             :   /// COMDAT values of the same name.
     132             :   GlobalValueSummaryList SummaryList;
     133             : };
     134             : 
     135             : /// Map from global value GUID to corresponding summary structures. Use a
     136             : /// std::map rather than a DenseMap so that pointers to the map's value_type
     137             : /// (which are used by ValueInfo) are not invalidated by insertion. Also it will
     138             : /// likely incur less overhead, as the value type is not very small and the size
     139             : /// of the map is unknown, resulting in inefficiencies due to repeated
     140             : /// insertions and resizing.
     141             : using GlobalValueSummaryMapTy =
     142             :     std::map<GlobalValue::GUID, GlobalValueSummaryInfo>;
     143             : 
     144             : /// Struct that holds a reference to a particular GUID in a global value
     145             : /// summary.
     146             : struct ValueInfo {
     147             :   PointerIntPair<const GlobalValueSummaryMapTy::value_type *, 1, bool>
     148             :       RefAndFlag;
     149             : 
     150             :   ValueInfo() = default;
     151         305 :   ValueInfo(bool HaveGVs, const GlobalValueSummaryMapTy::value_type *R) {
     152             :     RefAndFlag.setPointer(R);
     153             :     RefAndFlag.setInt(HaveGVs);
     154             :   }
     155             : 
     156             :   operator bool() const { return getRef(); }
     157             : 
     158        7879 :   GlobalValue::GUID getGUID() const { return getRef()->first; }
     159             :   const GlobalValue *getValue() const {
     160             :     assert(haveGVs());
     161         775 :     return getRef()->second.U.GV;
     162             :   }
     163             : 
     164             :   ArrayRef<std::unique_ptr<GlobalValueSummary>> getSummaryList() const {
     165             :     return getRef()->second.SummaryList;
     166             :   }
     167             : 
     168         950 :   StringRef name() const {
     169           0 :     return haveGVs() ? getRef()->second.U.GV->getName()
     170        1900 :                      : getRef()->second.U.Name;
     171             :   }
     172             : 
     173             :   bool haveGVs() const { return RefAndFlag.getInt(); }
     174             : 
     175             :   const GlobalValueSummaryMapTy::value_type *getRef() const {
     176             :     return RefAndFlag.getPointer();
     177             :   }
     178             : 
     179             :   bool isDSOLocal() const;
     180             : };
     181             : 
     182             : inline bool operator==(const ValueInfo &A, const ValueInfo &B) {
     183             :   assert(A.getRef() && B.getRef() &&
     184             :          "Need ValueInfo with non-null Ref for comparison");
     185             :   return A.getRef() == B.getRef();
     186             : }
     187             : 
     188             : inline bool operator!=(const ValueInfo &A, const ValueInfo &B) {
     189             :   assert(A.getRef() && B.getRef() &&
     190             :          "Need ValueInfo with non-null Ref for comparison");
     191             :   return A.getRef() != B.getRef();
     192             : }
     193             : 
     194             : inline bool operator<(const ValueInfo &A, const ValueInfo &B) {
     195             :   assert(A.getRef() && B.getRef() &&
     196             :          "Need ValueInfo with non-null Ref to compare GUIDs");
     197          19 :   return A.getGUID() < B.getGUID();
     198             : }
     199             : 
     200             : template <> struct DenseMapInfo<ValueInfo> {
     201             :   static inline ValueInfo getEmptyKey() {
     202             :     return ValueInfo(false, (GlobalValueSummaryMapTy::value_type *)-8);
     203             :   }
     204             : 
     205             :   static inline ValueInfo getTombstoneKey() {
     206             :     return ValueInfo(false, (GlobalValueSummaryMapTy::value_type *)-16);
     207             :   }
     208             : 
     209             :   static inline bool isSpecialKey(ValueInfo V) {
     210             :     return V == getTombstoneKey() || V == getEmptyKey();
     211             :   }
     212             : 
     213             :   static bool isEqual(ValueInfo L, ValueInfo R) {
     214             :     // We are not supposed to mix ValueInfo(s) with different HaveGVs flag
     215             :     // in a same container.
     216             :     assert(isSpecialKey(L) || isSpecialKey(R) || (L.haveGVs() == R.haveGVs()));
     217        1516 :     return L.getRef() == R.getRef();
     218             :   }
     219         555 :   static unsigned getHashValue(ValueInfo I) { return (uintptr_t)I.getRef(); }
     220             : };
     221             : 
     222             : /// Function and variable summary information to aid decisions and
     223             : /// implementation of importing.
     224           2 : class GlobalValueSummary {
     225             : public:
     226             :   /// Sububclass discriminator (for dyn_cast<> et al.)
     227             :   enum SummaryKind : unsigned { AliasKind, FunctionKind, GlobalVarKind };
     228             : 
     229             :   /// Group flags (Linkage, NotEligibleToImport, etc.) as a bitfield.
     230             :   struct GVFlags {
     231             :     /// The linkage type of the associated global value.
     232             :     ///
     233             :     /// One use is to flag values that have local linkage types and need to
     234             :     /// have module identifier appended before placing into the combined
     235             :     /// index, to disambiguate from other values with the same name.
     236             :     /// In the future this will be used to update and optimize linkage
     237             :     /// types based on global summary-based analysis.
     238             :     unsigned Linkage : 4;
     239             : 
     240             :     /// Indicate if the global value cannot be imported (e.g. it cannot
     241             :     /// be renamed or references something that can't be renamed).
     242             :     unsigned NotEligibleToImport : 1;
     243             : 
     244             :     /// In per-module summary, indicate that the global value must be considered
     245             :     /// a live root for index-based liveness analysis. Used for special LLVM
     246             :     /// values such as llvm.global_ctors that the linker does not know about.
     247             :     ///
     248             :     /// In combined summary, indicate that the global value is live.
     249             :     unsigned Live : 1;
     250             : 
     251             :     /// Indicates that the linker resolved the symbol to a definition from
     252             :     /// within the same linkage unit.
     253             :     unsigned DSOLocal : 1;
     254             : 
     255             :     /// Convenience Constructors
     256             :     explicit GVFlags(GlobalValue::LinkageTypes Linkage,
     257             :                      bool NotEligibleToImport, bool Live, bool IsLocal)
     258      102076 :         : Linkage(Linkage), NotEligibleToImport(NotEligibleToImport),
     259      104318 :           Live(Live), DSOLocal(IsLocal) {}
     260             :   };
     261             : 
     262             : private:
     263             :   /// Kind of summary for use in dyn_cast<> et al.
     264             :   SummaryKind Kind;
     265             : 
     266             :   GVFlags Flags;
     267             : 
     268             :   /// This is the hash of the name of the symbol in the original file. It is
     269             :   /// identical to the GUID for global symbols, but differs for local since the
     270             :   /// GUID includes the module level id in the hash.
     271             :   GlobalValue::GUID OriginalName = 0;
     272             : 
     273             :   /// Path of module IR containing value's definition, used to locate
     274             :   /// module during importing.
     275             :   ///
     276             :   /// This is only used during parsing of the combined index, or when
     277             :   /// parsing the per-module index for creation of the combined summary index,
     278             :   /// not during writing of the per-module index which doesn't contain a
     279             :   /// module path string table.
     280             :   StringRef ModulePath;
     281             : 
     282             :   /// List of values referenced by this global value's definition
     283             :   /// (either by the initializer of a global variable, or referenced
     284             :   /// from within a function). This does not include functions called, which
     285             :   /// are listed in the derived FunctionSummary object.
     286             :   std::vector<ValueInfo> RefEdgeList;
     287             : 
     288             : protected:
     289             :   GlobalValueSummary(SummaryKind K, GVFlags Flags, std::vector<ValueInfo> Refs)
     290      104318 :       : Kind(K), Flags(Flags), RefEdgeList(std::move(Refs)) {
     291             :     assert((K != AliasKind || Refs.empty()) &&
     292             :            "Expect no references for AliasSummary");
     293             :   }
     294             : 
     295             : public:
     296      104317 :   virtual ~GlobalValueSummary() = default;
     297             : 
     298             :   /// Returns the hash of the original name, it is identical to the GUID for
     299             :   /// externally visible symbols, but not for local ones.
     300             :   GlobalValue::GUID getOriginalName() const { return OriginalName; }
     301             : 
     302             :   /// Initialize the original name hash in this summary.
     303        1161 :   void setOriginalName(GlobalValue::GUID Name) { OriginalName = Name; }
     304             : 
     305             :   /// Which kind of summary subclass this is.
     306             :   SummaryKind getSummaryKind() const { return Kind; }
     307             : 
     308             :   /// Set the path to the module containing this function, for use in
     309             :   /// the combined index.
     310        2242 :   void setModulePath(StringRef ModPath) { ModulePath = ModPath; }
     311             : 
     312             :   /// Get the path to the module containing this function.
     313             :   StringRef modulePath() const { return ModulePath; }
     314             : 
     315             :   /// Get the flags for this GlobalValue (see \p struct GVFlags).
     316        1924 :   GVFlags flags() const { return Flags; }
     317             : 
     318             :   /// Return linkage type recorded for this global value.
     319             :   GlobalValue::LinkageTypes linkage() const {
     320        5480 :     return static_cast<GlobalValue::LinkageTypes>(Flags.Linkage);
     321             :   }
     322             : 
     323             :   /// Sets the linkage to the value determined by global summary-based
     324             :   /// optimization. Will be applied in the ThinLTO backends.
     325             :   void setLinkage(GlobalValue::LinkageTypes Linkage) {
     326         590 :     Flags.Linkage = Linkage;
     327             :   }
     328             : 
     329             :   /// Return true if this global value can't be imported.
     330         293 :   bool notEligibleToImport() const { return Flags.NotEligibleToImport; }
     331             : 
     332        2629 :   bool isLive() const { return Flags.Live; }
     333             : 
     334         885 :   void setLive(bool Live) { Flags.Live = Live; }
     335             : 
     336         198 :   void setDSOLocal(bool Local) { Flags.DSOLocal = Local; }
     337             : 
     338        1753 :   bool isDSOLocal() const { return Flags.DSOLocal; }
     339             : 
     340             :   /// Flag that this global value cannot be imported.
     341          51 :   void setNotEligibleToImport() { Flags.NotEligibleToImport = true; }
     342             : 
     343             :   /// Return the list of values referenced by this global value definition.
     344             :   ArrayRef<ValueInfo> refs() const { return RefEdgeList; }
     345             : 
     346             :   /// If this is an alias summary, returns the summary of the aliased object (a
     347             :   /// global variable or function), otherwise returns itself.
     348             :   GlobalValueSummary *getBaseObject();
     349             :   const GlobalValueSummary *getBaseObject() const;
     350             : 
     351             :   friend class ModuleSummaryIndex;
     352             : };
     353             : 
     354             : /// Alias summary information.
     355        1024 : class AliasSummary : public GlobalValueSummary {
     356             :   GlobalValueSummary *AliaseeSummary;
     357             :   // AliaseeGUID is only set and accessed when we are building a combined index
     358             :   // via the BitcodeReader.
     359             :   GlobalValue::GUID AliaseeGUID;
     360             : 
     361             : public:
     362         512 :   AliasSummary(GVFlags Flags)
     363         512 :       : GlobalValueSummary(AliasKind, Flags, ArrayRef<ValueInfo>{}),
     364         512 :         AliaseeSummary(nullptr), AliaseeGUID(0) {}
     365             : 
     366             :   /// Check if this is an alias summary.
     367             :   static bool classof(const GlobalValueSummary *GVS) {
     368        4265 :     return GVS->getSummaryKind() == AliasKind;
     369             :   }
     370             : 
     371         512 :   void setAliasee(GlobalValueSummary *Aliasee) { AliaseeSummary = Aliasee; }
     372         383 :   void setAliaseeGUID(GlobalValue::GUID GUID) { AliaseeGUID = GUID; }
     373             : 
     374             :   const GlobalValueSummary &getAliasee() const {
     375             :     assert(AliaseeSummary && "Unexpected missing aliasee summary");
     376             :     return *AliaseeSummary;
     377             :   }
     378             : 
     379             :   GlobalValueSummary &getAliasee() {
     380             :     return const_cast<GlobalValueSummary &>(
     381         565 :                          static_cast<const AliasSummary *>(this)->getAliasee());
     382             :   }
     383             :   const GlobalValue::GUID &getAliaseeGUID() const {
     384             :     assert(AliaseeGUID && "Unexpected missing aliasee GUID");
     385             :     return AliaseeGUID;
     386             :   }
     387             : };
     388             : 
     389             : const inline GlobalValueSummary *GlobalValueSummary::getBaseObject() const {
     390             :   if (auto *AS = dyn_cast<AliasSummary>(this))
     391          46 :     return &AS->getAliasee();
     392             :   return this;
     393             : }
     394             : 
     395             : inline GlobalValueSummary *GlobalValueSummary::getBaseObject() {
     396             :   if (auto *AS = dyn_cast<AliasSummary>(this))
     397             :     return &AS->getAliasee();
     398             :   return this;
     399             : }
     400             : 
     401             : /// Function summary information to aid decisions and implementation of
     402             : /// importing.
     403      208834 : class FunctionSummary : public GlobalValueSummary {
     404             : public:
     405             :   /// <CalleeValueInfo, CalleeInfo> call edge pair.
     406             :   using EdgeTy = std::pair<ValueInfo, CalleeInfo>;
     407             : 
     408             :   /// Types for -force-summary-edges-cold debugging option.
     409             :   enum ForceSummaryHotnessType : unsigned {
     410             :     FSHT_None,
     411             :     FSHT_AllNonCritical,
     412             :     FSHT_All
     413             :   };
     414             : 
     415             :   /// An "identifier" for a virtual function. This contains the type identifier
     416             :   /// represented as a GUID and the offset from the address point to the virtual
     417             :   /// function pointer, where "address point" is as defined in the Itanium ABI:
     418             :   /// https://itanium-cxx-abi.github.io/cxx-abi/abi.html#vtable-general
     419             :   struct VFuncId {
     420             :     GlobalValue::GUID GUID;
     421             :     uint64_t Offset;
     422             :   };
     423             : 
     424             :   /// A specification for a virtual function call with all constant integer
     425             :   /// arguments. This is used to perform virtual constant propagation on the
     426             :   /// summary.
     427         937 :   struct ConstVCall {
     428             :     VFuncId VFunc;
     429             :     std::vector<uint64_t> Args;
     430             :   };
     431             : 
     432             :   /// All type identifier related information. Because these fields are
     433             :   /// relatively uncommon we only allocate space for them if necessary.
     434         612 :   struct TypeIdInfo {
     435             :     /// List of type identifiers used by this function in llvm.type.test
     436             :     /// intrinsics referenced by something other than an llvm.assume intrinsic,
     437             :     /// represented as GUIDs.
     438             :     std::vector<GlobalValue::GUID> TypeTests;
     439             : 
     440             :     /// List of virtual calls made by this function using (respectively)
     441             :     /// llvm.assume(llvm.type.test) or llvm.type.checked.load intrinsics that do
     442             :     /// not have all constant integer arguments.
     443             :     std::vector<VFuncId> TypeTestAssumeVCalls, TypeCheckedLoadVCalls;
     444             : 
     445             :     /// List of virtual calls made by this function using (respectively)
     446             :     /// llvm.assume(llvm.type.test) or llvm.type.checked.load intrinsics with
     447             :     /// all constant integer arguments.
     448             :     std::vector<ConstVCall> TypeTestAssumeConstVCalls,
     449             :         TypeCheckedLoadConstVCalls;
     450             :   };
     451             : 
     452             :   /// Function attribute flags. Used to track if a function accesses memory,
     453             :   /// recurses or aliases.
     454             :   struct FFlags {
     455             :     unsigned ReadNone : 1;
     456             :     unsigned ReadOnly : 1;
     457             :     unsigned NoRecurse : 1;
     458             :     unsigned ReturnDoesNotAlias : 1;
     459             :   };
     460             : 
     461             :   /// Create an empty FunctionSummary (with specified call edges).
     462             :   /// Used to represent external nodes and the dummy root node.
     463             :   static FunctionSummary
     464      101170 :   makeDummyFunctionSummary(std::vector<FunctionSummary::EdgeTy> Edges) {
     465             :     return FunctionSummary(
     466             :         FunctionSummary::GVFlags(
     467             :             GlobalValue::LinkageTypes::AvailableExternallyLinkage,
     468             :             /*NotEligibleToImport=*/true, /*Live=*/true, /*IsLocal=*/false),
     469      101170 :         0, FunctionSummary::FFlags{}, std::vector<ValueInfo>(),
     470      101170 :         std::move(Edges), std::vector<GlobalValue::GUID>(),
     471      101170 :         std::vector<FunctionSummary::VFuncId>(),
     472      101170 :         std::vector<FunctionSummary::VFuncId>(),
     473      101170 :         std::vector<FunctionSummary::ConstVCall>(),
     474      404680 :         std::vector<FunctionSummary::ConstVCall>());
     475             :   }
     476             : 
     477             :   /// A dummy node to reference external functions that aren't in the index
     478             :   static FunctionSummary ExternalNode;
     479             : 
     480             : private:
     481             :   /// Number of instructions (ignoring debug instructions, e.g.) computed
     482             :   /// during the initial compile step when the summary index is first built.
     483             :   unsigned InstCount;
     484             : 
     485             :   /// Function attribute flags. Used to track if a function accesses memory,
     486             :   /// recurses or aliases.
     487             :   FFlags FunFlags;
     488             : 
     489             :   /// List of <CalleeValueInfo, CalleeInfo> call edge pairs from this function.
     490             :   std::vector<EdgeTy> CallGraphEdgeList;
     491             : 
     492             :   std::unique_ptr<TypeIdInfo> TIdInfo;
     493             : 
     494             : public:
     495      103334 :   FunctionSummary(GVFlags Flags, unsigned NumInsts, FFlags FunFlags,
     496             :                   std::vector<ValueInfo> Refs, std::vector<EdgeTy> CGEdges,
     497             :                   std::vector<GlobalValue::GUID> TypeTests,
     498             :                   std::vector<VFuncId> TypeTestAssumeVCalls,
     499             :                   std::vector<VFuncId> TypeCheckedLoadVCalls,
     500             :                   std::vector<ConstVCall> TypeTestAssumeConstVCalls,
     501             :                   std::vector<ConstVCall> TypeCheckedLoadConstVCalls)
     502      103334 :       : GlobalValueSummary(FunctionKind, Flags, std::move(Refs)),
     503             :         InstCount(NumInsts), FunFlags(FunFlags),
     504      206668 :         CallGraphEdgeList(std::move(CGEdges)) {
     505      206526 :     if (!TypeTests.empty() || !TypeTestAssumeVCalls.empty() ||
     506      309816 :         !TypeCheckedLoadVCalls.empty() || !TypeTestAssumeConstVCalls.empty() ||
     507             :         !TypeCheckedLoadConstVCalls.empty())
     508         102 :       TIdInfo = llvm::make_unique<TypeIdInfo>(TypeIdInfo{
     509             :           std::move(TypeTests), std::move(TypeTestAssumeVCalls),
     510             :           std::move(TypeCheckedLoadVCalls),
     511             :           std::move(TypeTestAssumeConstVCalls),
     512             :           std::move(TypeCheckedLoadConstVCalls)});
     513      103334 :   }
     514             : 
     515             :   /// Check if this is a function summary.
     516             :   static bool classof(const GlobalValueSummary *GVS) {
     517        4438 :     return GVS->getSummaryKind() == FunctionKind;
     518             :   }
     519             : 
     520             :   /// Get function attribute flags.
     521        1331 :   FFlags fflags() const { return FunFlags; }
     522             : 
     523             :   /// Get the instruction count recorded for this function.
     524             :   unsigned instCount() const { return InstCount; }
     525             : 
     526             :   /// Return the list of <CalleeValueInfo, CalleeInfo> pairs.
     527             :   ArrayRef<EdgeTy> calls() const { return CallGraphEdgeList; }
     528             : 
     529             :   /// Returns the list of type identifiers used by this function in
     530             :   /// llvm.type.test intrinsics other than by an llvm.assume intrinsic,
     531             :   /// represented as GUIDs.
     532             :   ArrayRef<GlobalValue::GUID> type_tests() const {
     533        1577 :     if (TIdInfo)
     534             :       return TIdInfo->TypeTests;
     535             :     return {};
     536             :   }
     537             : 
     538             :   /// Returns the list of virtual calls made by this function using
     539             :   /// llvm.assume(llvm.type.test) intrinsics that do not have all constant
     540             :   /// integer arguments.
     541             :   ArrayRef<VFuncId> type_test_assume_vcalls() const {
     542        1241 :     if (TIdInfo)
     543             :       return TIdInfo->TypeTestAssumeVCalls;
     544             :     return {};
     545             :   }
     546             : 
     547             :   /// Returns the list of virtual calls made by this function using
     548             :   /// llvm.type.checked.load intrinsics that do not have all constant integer
     549             :   /// arguments.
     550             :   ArrayRef<VFuncId> type_checked_load_vcalls() const {
     551        1241 :     if (TIdInfo)
     552             :       return TIdInfo->TypeCheckedLoadVCalls;
     553             :     return {};
     554             :   }
     555             : 
     556             :   /// Returns the list of virtual calls made by this function using
     557             :   /// llvm.assume(llvm.type.test) intrinsics with all constant integer
     558             :   /// arguments.
     559             :   ArrayRef<ConstVCall> type_test_assume_const_vcalls() const {
     560        1241 :     if (TIdInfo)
     561             :       return TIdInfo->TypeTestAssumeConstVCalls;
     562             :     return {};
     563             :   }
     564             : 
     565             :   /// Returns the list of virtual calls made by this function using
     566             :   /// llvm.type.checked.load intrinsics with all constant integer arguments.
     567             :   ArrayRef<ConstVCall> type_checked_load_const_vcalls() const {
     568        1241 :     if (TIdInfo)
     569             :       return TIdInfo->TypeCheckedLoadConstVCalls;
     570             :     return {};
     571             :   }
     572             : 
     573             :   /// Add a type test to the summary. This is used by WholeProgramDevirt if we
     574             :   /// were unable to devirtualize a checked call.
     575           8 :   void addTypeTest(GlobalValue::GUID Guid) {
     576           8 :     if (!TIdInfo)
     577           0 :       TIdInfo = llvm::make_unique<TypeIdInfo>();
     578           8 :     TIdInfo->TypeTests.push_back(Guid);
     579           8 :   }
     580             : 
     581             :   const TypeIdInfo *getTypeIdInfo() const { return TIdInfo.get(); };
     582             : 
     583             :   friend struct GraphTraits<ValueInfo>;
     584             : };
     585             : 
     586             : template <> struct DenseMapInfo<FunctionSummary::VFuncId> {
     587             :   static FunctionSummary::VFuncId getEmptyKey() { return {0, uint64_t(-1)}; }
     588             : 
     589             :   static FunctionSummary::VFuncId getTombstoneKey() {
     590             :     return {0, uint64_t(-2)};
     591             :   }
     592             : 
     593             :   static bool isEqual(FunctionSummary::VFuncId L, FunctionSummary::VFuncId R) {
     594         419 :     return L.GUID == R.GUID && L.Offset == R.Offset;
     595             :   }
     596             : 
     597           8 :   static unsigned getHashValue(FunctionSummary::VFuncId I) { return I.GUID; }
     598             : };
     599             : 
     600             : template <> struct DenseMapInfo<FunctionSummary::ConstVCall> {
     601             :   static FunctionSummary::ConstVCall getEmptyKey() {
     602          17 :     return {{0, uint64_t(-1)}, {}};
     603             :   }
     604             : 
     605             :   static FunctionSummary::ConstVCall getTombstoneKey() {
     606          10 :     return {{0, uint64_t(-2)}, {}};
     607             :   }
     608             : 
     609         399 :   static bool isEqual(FunctionSummary::ConstVCall L,
     610             :                       FunctionSummary::ConstVCall R) {
     611         388 :     return DenseMapInfo<FunctionSummary::VFuncId>::isEqual(L.VFunc, R.VFunc) &&
     612         787 :            L.Args == R.Args;
     613             :   }
     614             : 
     615             :   static unsigned getHashValue(FunctionSummary::ConstVCall I) {
     616           4 :     return I.VFunc.GUID;
     617             :   }
     618             : };
     619             : 
     620             : /// Global variable summary information to aid decisions and
     621             : /// implementation of importing.
     622             : ///
     623             : /// Currently this doesn't add anything to the base \p GlobalValueSummary,
     624             : /// but is a placeholder as additional info may be added to the summary
     625             : /// for variables.
     626         944 : class GlobalVarSummary : public GlobalValueSummary {
     627             : 
     628             : public:
     629             :   GlobalVarSummary(GVFlags Flags, std::vector<ValueInfo> Refs)
     630         472 :       : GlobalValueSummary(GlobalVarKind, Flags, std::move(Refs)) {}
     631             : 
     632             :   /// Check if this is a global variable summary.
     633             :   static bool classof(const GlobalValueSummary *GVS) {
     634             :     return GVS->getSummaryKind() == GlobalVarKind;
     635             :   }
     636             : };
     637             : 
     638         121 : struct TypeTestResolution {
     639             :   /// Specifies which kind of type check we should emit for this byte array.
     640             :   /// See http://clang.llvm.org/docs/ControlFlowIntegrityDesign.html for full
     641             :   /// details on each kind of check; the enumerators are described with
     642             :   /// reference to that document.
     643             :   enum Kind {
     644             :     Unsat,     ///< Unsatisfiable type (i.e. no global has this type metadata)
     645             :     ByteArray, ///< Test a byte array (first example)
     646             :     Inline,    ///< Inlined bit vector ("Short Inline Bit Vectors")
     647             :     Single,    ///< Single element (last example in "Short Inline Bit Vectors")
     648             :     AllOnes,   ///< All-ones bit vector ("Eliminating Bit Vector Checks for
     649             :                ///  All-Ones Bit Vectors")
     650             :   } TheKind = Unsat;
     651             : 
     652             :   /// Range of size-1 expressed as a bit width. For example, if the size is in
     653             :   /// range [1,256], this number will be 8. This helps generate the most compact
     654             :   /// instruction sequences.
     655             :   unsigned SizeM1BitWidth = 0;
     656             : 
     657             :   // The following fields are only used if the target does not support the use
     658             :   // of absolute symbols to store constants. Their meanings are the same as the
     659             :   // corresponding fields in LowerTypeTestsModule::TypeIdLowering in
     660             :   // LowerTypeTests.cpp.
     661             : 
     662             :   uint64_t AlignLog2 = 0;
     663             :   uint64_t SizeM1 = 0;
     664             :   uint8_t BitMask = 0;
     665             :   uint64_t InlineBits = 0;
     666             : };
     667             : 
     668         180 : struct WholeProgramDevirtResolution {
     669             :   enum Kind {
     670             :     Indir,        ///< Just do a regular virtual call
     671             :     SingleImpl,   ///< Single implementation devirtualization
     672             :     BranchFunnel, ///< When retpoline mitigation is enabled, use a branch funnel
     673             :                   ///< that is defined in the merged module. Otherwise same as
     674             :                   ///< Indir.
     675             :   } TheKind = Indir;
     676             : 
     677             :   std::string SingleImplName;
     678             : 
     679             :   struct ByArg {
     680             :     enum Kind {
     681             :       Indir,            ///< Just do a regular virtual call
     682             :       UniformRetVal,    ///< Uniform return value optimization
     683             :       UniqueRetVal,     ///< Unique return value optimization
     684             :       VirtualConstProp, ///< Virtual constant propagation
     685             :     } TheKind = Indir;
     686             : 
     687             :     /// Additional information for the resolution:
     688             :     /// - UniformRetVal: the uniform return value.
     689             :     /// - UniqueRetVal: the return value associated with the unique vtable (0 or
     690             :     ///   1).
     691             :     uint64_t Info = 0;
     692             : 
     693             :     // The following fields are only used if the target does not support the use
     694             :     // of absolute symbols to store constants.
     695             : 
     696             :     uint32_t Byte = 0;
     697             :     uint32_t Bit = 0;
     698             :   };
     699             : 
     700             :   /// Resolutions for calls with all constant integer arguments (excluding the
     701             :   /// first argument, "this"), where the key is the argument vector.
     702             :   std::map<std::vector<uint64_t>, ByArg> ResByArg;
     703             : };
     704             : 
     705         121 : struct TypeIdSummary {
     706             :   TypeTestResolution TTRes;
     707             : 
     708             :   /// Mapping from byte offset to whole-program devirt resolution for that
     709             :   /// (typeid, byte offset) pair.
     710             :   std::map<uint64_t, WholeProgramDevirtResolution> WPDRes;
     711             : };
     712             : 
     713             : /// 160 bits SHA1
     714             : using ModuleHash = std::array<uint32_t, 5>;
     715             : 
     716             : /// Type used for iterating through the global value summary map.
     717             : using const_gvsummary_iterator = GlobalValueSummaryMapTy::const_iterator;
     718             : using gvsummary_iterator = GlobalValueSummaryMapTy::iterator;
     719             : 
     720             : /// String table to hold/own module path strings, which additionally holds the
     721             : /// module ID assigned to each module during the plugin step, as well as a hash
     722             : /// of the module. The StringMap makes a copy of and owns inserted strings.
     723             : using ModulePathStringTableTy = StringMap<std::pair<uint64_t, ModuleHash>>;
     724             : 
     725             : /// Map of global value GUID to its summary, used to identify values defined in
     726             : /// a particular module, and provide efficient access to their summary.
     727             : using GVSummaryMapTy = DenseMap<GlobalValue::GUID, GlobalValueSummary *>;
     728             : 
     729             : /// Class to hold module path string table and global value map,
     730             : /// and encapsulate methods for operating on them.
     731        5682 : class ModuleSummaryIndex {
     732             : private:
     733             :   /// Map from value name to list of summary instances for values of that
     734             :   /// name (may be duplicates in the COMDAT case, e.g.).
     735             :   GlobalValueSummaryMapTy GlobalValueMap;
     736             : 
     737             :   /// Holds strings for combined index, mapping to the corresponding module ID.
     738             :   ModulePathStringTableTy ModulePathStringTable;
     739             : 
     740             :   /// Mapping from type identifiers to summary information for that type
     741             :   /// identifier.
     742             :   std::map<std::string, TypeIdSummary> TypeIdMap;
     743             : 
     744             :   /// Mapping from original ID to GUID. If original ID can map to multiple
     745             :   /// GUIDs, it will be mapped to 0.
     746             :   std::map<GlobalValue::GUID, GlobalValue::GUID> OidGuidMap;
     747             : 
     748             :   /// Indicates that summary-based GlobalValue GC has run, and values with
     749             :   /// GVFlags::Live==false are really dead. Otherwise, all values must be
     750             :   /// considered live.
     751             :   bool WithGlobalValueDeadStripping = false;
     752             : 
     753             :   /// Indicates that distributed backend should skip compilation of the
     754             :   /// module. Flag is suppose to be set by distributed ThinLTO indexing
     755             :   /// when it detected that the module is not needed during the final
     756             :   /// linking. As result distributed backend should just output a minimal
     757             :   /// valid object file.
     758             :   bool SkipModuleByDistributedBackend = false;
     759             : 
     760             :   /// If true then we're performing analysis of IR module, or parsing along with
     761             :   /// the IR from assembly. The value of 'false' means we're reading summary
     762             :   /// from BC or YAML source. Affects the type of value stored in NameOrGV
     763             :   /// union.
     764             :   bool HaveGVs;
     765             : 
     766             :   std::set<std::string> CfiFunctionDefs;
     767             :   std::set<std::string> CfiFunctionDecls;
     768             : 
     769             :   // YAML I/O support.
     770             :   friend yaml::MappingTraits<ModuleSummaryIndex>;
     771             : 
     772             :   GlobalValueSummaryMapTy::value_type *
     773        4069 :   getOrInsertValuePtr(GlobalValue::GUID GUID) {
     774        8138 :     return &*GlobalValueMap.emplace(GUID, GlobalValueSummaryInfo(HaveGVs))
     775        4069 :                  .first;
     776             :   }
     777             : 
     778             : public:
     779             :   // See HaveGVs variable comment.
     780        1998 :   ModuleSummaryIndex(bool HaveGVs) : HaveGVs(HaveGVs) {}
     781             : 
     782             :   bool haveGVs() const { return HaveGVs; }
     783             : 
     784             :   gvsummary_iterator begin() { return GlobalValueMap.begin(); }
     785             :   const_gvsummary_iterator begin() const { return GlobalValueMap.begin(); }
     786             :   gvsummary_iterator end() { return GlobalValueMap.end(); }
     787             :   const_gvsummary_iterator end() const { return GlobalValueMap.end(); }
     788             :   size_t size() const { return GlobalValueMap.size(); }
     789             : 
     790             :   /// Convenience function for doing a DFS on a ValueInfo. Marks the function in
     791             :   /// the FunctionHasParent map.
     792           7 :   static void discoverNodes(ValueInfo V,
     793             :                             std::map<ValueInfo, bool> &FunctionHasParent) {
     794           7 :     if (!V.getSummaryList().size())
     795             :       return; // skip external functions that don't have summaries
     796             : 
     797             :     // Mark discovered if we haven't yet
     798          12 :     auto S = FunctionHasParent.emplace(V, false);
     799             : 
     800             :     // Stop if we've already discovered this node
     801           6 :     if (!S.second)
     802             :       return;
     803             : 
     804             :     FunctionSummary *F =
     805             :         dyn_cast<FunctionSummary>(V.getSummaryList().front().get());
     806             :     assert(F != nullptr && "Expected FunctionSummary node");
     807             : 
     808          10 :     for (auto &C : F->calls()) {
     809             :       // Insert node if necessary
     810           8 :       auto S = FunctionHasParent.emplace(C.first, true);
     811             : 
     812             :       // Skip nodes that we're sure have parents
     813           4 :       if (!S.second && S.first->second)
     814             :         continue;
     815             : 
     816           3 :       if (S.second)
     817           3 :         discoverNodes(C.first, FunctionHasParent);
     818             :       else
     819           0 :         S.first->second = true;
     820             :     }
     821             :   }
     822             : 
     823             :   // Calculate the callgraph root
     824           1 :   FunctionSummary calculateCallGraphRoot() {
     825             :     // Functions that have a parent will be marked in FunctionHasParent pair.
     826             :     // Once we've marked all functions, the functions in the map that are false
     827             :     // have no parent (so they're the roots)
     828             :     std::map<ValueInfo, bool> FunctionHasParent;
     829             : 
     830           6 :     for (auto &S : *this) {
     831             :       // Skip external functions
     832          14 :       if (!S.second.SummaryList.size() ||
     833             :           !isa<FunctionSummary>(S.second.SummaryList.front().get()))
     834           1 :         continue;
     835           8 :       discoverNodes(ValueInfo(HaveGVs, &S), FunctionHasParent);
     836             :     }
     837             : 
     838             :     std::vector<FunctionSummary::EdgeTy> Edges;
     839             :     // create edges to all roots in the Index
     840           6 :     for (auto &P : FunctionHasParent) {
     841           5 :       if (P.second)
     842           3 :         continue; // skip over non-root nodes
     843           4 :       Edges.push_back(std::make_pair(P.first, CalleeInfo{}));
     844             :     }
     845           1 :     if (Edges.empty()) {
     846             :       // Failed to find root - return an empty node
     847           0 :       return FunctionSummary::makeDummyFunctionSummary({});
     848             :     }
     849           3 :     auto CallGraphRoot = FunctionSummary::makeDummyFunctionSummary(Edges);
     850           1 :     return CallGraphRoot;
     851             :   }
     852             : 
     853             :   bool withGlobalValueDeadStripping() const {
     854             :     return WithGlobalValueDeadStripping;
     855             :   }
     856             :   void setWithGlobalValueDeadStripping() {
     857         436 :     WithGlobalValueDeadStripping = true;
     858             :   }
     859             : 
     860             :   bool skipModuleByDistributedBackend() const {
     861             :     return SkipModuleByDistributedBackend;
     862             :   }
     863             :   void setSkipModuleByDistributedBackend() {
     864           2 :     SkipModuleByDistributedBackend = true;
     865             :   }
     866             : 
     867             :   bool isGlobalValueLive(const GlobalValueSummary *GVS) const {
     868        4275 :     return !WithGlobalValueDeadStripping || GVS->isLive();
     869             :   }
     870             :   bool isGUIDLive(GlobalValue::GUID GUID) const;
     871             : 
     872             :   /// Return a ValueInfo for the index value_type (convenient when iterating
     873             :   /// index).
     874             :   ValueInfo getValueInfo(const GlobalValueSummaryMapTy::value_type &R) const {
     875             :     return ValueInfo(HaveGVs, &R);
     876             :   }
     877             : 
     878             :   /// Return a ValueInfo for GUID if it exists, otherwise return ValueInfo().
     879        7218 :   ValueInfo getValueInfo(GlobalValue::GUID GUID) const {
     880             :     auto I = GlobalValueMap.find(GUID);
     881       21654 :     return ValueInfo(HaveGVs, I == GlobalValueMap.end() ? nullptr : &*I);
     882             :   }
     883             : 
     884             :   /// Return a ValueInfo for \p GUID.
     885             :   ValueInfo getOrInsertValueInfo(GlobalValue::GUID GUID) {
     886        2063 :     return ValueInfo(HaveGVs, getOrInsertValuePtr(GUID));
     887             :   }
     888             : 
     889             :   /// Return a ValueInfo for \p GUID setting value \p Name.
     890             :   ValueInfo getOrInsertValueInfo(GlobalValue::GUID GUID, StringRef Name) {
     891             :     assert(!HaveGVs);
     892        1485 :     auto VP = getOrInsertValuePtr(GUID);
     893        1485 :     VP->second.U.Name = Name;
     894        1485 :     return ValueInfo(HaveGVs, VP);
     895             :   }
     896             : 
     897             :   /// Return a ValueInfo for \p GV and mark it as belonging to GV.
     898         521 :   ValueInfo getOrInsertValueInfo(const GlobalValue *GV) {
     899             :     assert(HaveGVs);
     900         521 :     auto VP = getOrInsertValuePtr(GV->getGUID());
     901         521 :     VP->second.U.GV = GV;
     902        1042 :     return ValueInfo(HaveGVs, VP);
     903             :   }
     904             : 
     905             :   /// Return the GUID for \p OriginalId in the OidGuidMap.
     906             :   GlobalValue::GUID getGUIDFromOriginalID(GlobalValue::GUID OriginalID) const {
     907             :     const auto I = OidGuidMap.find(OriginalID);
     908          96 :     return I == OidGuidMap.end() ? 0 : I->second;
     909             :   }
     910             : 
     911             :   std::set<std::string> &cfiFunctionDefs() { return CfiFunctionDefs; }
     912             :   const std::set<std::string> &cfiFunctionDefs() const { return CfiFunctionDefs; }
     913             : 
     914             :   std::set<std::string> &cfiFunctionDecls() { return CfiFunctionDecls; }
     915             :   const std::set<std::string> &cfiFunctionDecls() const { return CfiFunctionDecls; }
     916             : 
     917             :   /// Add a global value summary for a value of the given name.
     918         880 :   void addGlobalValueSummary(StringRef ValueName,
     919             :                              std::unique_ptr<GlobalValueSummary> Summary) {
     920        1760 :     addGlobalValueSummary(getOrInsertValueInfo(GlobalValue::getGUID(ValueName)),
     921             :                           std::move(Summary));
     922         880 :   }
     923             : 
     924             :   /// Add a global value summary for the given ValueInfo.
     925        3122 :   void addGlobalValueSummary(ValueInfo VI,
     926             :                              std::unique_ptr<GlobalValueSummary> Summary) {
     927        6244 :     addOriginalName(VI.getGUID(), Summary->getOriginalName());
     928             :     // Here we have a notionally const VI, but the value it points to is owned
     929             :     // by the non-const *this.
     930             :     const_cast<GlobalValueSummaryMapTy::value_type *>(VI.getRef())
     931        3122 :         ->second.SummaryList.push_back(std::move(Summary));
     932        3122 :   }
     933             : 
     934             :   /// Add an original name for the value of the given GUID.
     935        3296 :   void addOriginalName(GlobalValue::GUID ValueGUID,
     936             :                        GlobalValue::GUID OrigGUID) {
     937        3296 :     if (OrigGUID == 0 || ValueGUID == OrigGUID)
     938             :       return;
     939           8 :     if (OidGuidMap.count(OrigGUID) && OidGuidMap[OrigGUID] != ValueGUID)
     940           0 :       OidGuidMap[OrigGUID] = 0;
     941             :     else
     942         259 :       OidGuidMap[OrigGUID] = ValueGUID;
     943             :   }
     944             : 
     945             :   /// Find the summary for global \p GUID in module \p ModuleId, or nullptr if
     946             :   /// not found.
     947         768 :   GlobalValueSummary *findSummaryInModule(GlobalValue::GUID ValueGUID,
     948             :                                           StringRef ModuleId) const {
     949         768 :     auto CalleeInfo = getValueInfo(ValueGUID);
     950         768 :     if (!CalleeInfo) {
     951             :       return nullptr; // This function does not have a summary
     952             :     }
     953             :     auto Summary =
     954             :         llvm::find_if(CalleeInfo.getSummaryList(),
     955         727 :                       [&](const std::unique_ptr<GlobalValueSummary> &Summary) {
     956         727 :                         return Summary->modulePath() == ModuleId;
     957         727 :                       });
     958         766 :     if (Summary == CalleeInfo.getSummaryList().end())
     959             :       return nullptr;
     960         680 :     return Summary->get();
     961             :   }
     962             : 
     963             :   /// Returns the first GlobalValueSummary for \p GV, asserting that there
     964             :   /// is only one if \p PerModuleIndex.
     965         255 :   GlobalValueSummary *getGlobalValueSummary(const GlobalValue &GV,
     966             :                                             bool PerModuleIndex = true) const {
     967             :     assert(GV.hasName() && "Can't get GlobalValueSummary for GV with no name");
     968         255 :     return getGlobalValueSummary(GlobalValue::getGUID(GV.getName()),
     969         255 :                                  PerModuleIndex);
     970             :   }
     971             : 
     972             :   /// Returns the first GlobalValueSummary for \p ValueGUID, asserting that
     973             :   /// there
     974             :   /// is only one if \p PerModuleIndex.
     975             :   GlobalValueSummary *getGlobalValueSummary(GlobalValue::GUID ValueGUID,
     976             :                                             bool PerModuleIndex = true) const;
     977             : 
     978             :   /// Table of modules, containing module hash and id.
     979             :   const StringMap<std::pair<uint64_t, ModuleHash>> &modulePaths() const {
     980          90 :     return ModulePathStringTable;
     981             :   }
     982             : 
     983             :   /// Table of modules, containing hash and id.
     984             :   StringMap<std::pair<uint64_t, ModuleHash>> &modulePaths() {
     985          81 :     return ModulePathStringTable;
     986             :   }
     987             : 
     988             :   /// Get the module ID recorded for the given module path.
     989             :   uint64_t getModuleId(const StringRef ModPath) const {
     990         892 :     return ModulePathStringTable.lookup(ModPath).first;
     991             :   }
     992             : 
     993             :   /// Get the module SHA1 hash recorded for the given module path.
     994             :   const ModuleHash &getModuleHash(const StringRef ModPath) const {
     995         323 :     auto It = ModulePathStringTable.find(ModPath);
     996             :     assert(It != ModulePathStringTable.end() && "Module not registered");
     997         119 :     return It->second.second;
     998             :   }
     999             : 
    1000             :   /// Convenience method for creating a promoted global name
    1001             :   /// for the given value name of a local, and its original module's ID.
    1002         119 :   static std::string getGlobalNameForLocal(StringRef Name, ModuleHash ModHash) {
    1003             :     SmallString<256> NewName(Name);
    1004             :     NewName += ".llvm.";
    1005         238 :     NewName += utostr((uint64_t(ModHash[0]) << 32) |
    1006             :                       ModHash[1]); // Take the first 64 bits
    1007         119 :     return NewName.str();
    1008             :   }
    1009             : 
    1010             :   /// Helper to obtain the unpromoted name for a global value (or the original
    1011             :   /// name if not promoted).
    1012             :   static StringRef getOriginalNameBeforePromote(StringRef Name) {
    1013          10 :     std::pair<StringRef, StringRef> Pair = Name.split(".llvm.");
    1014          10 :     return Pair.first;
    1015             :   }
    1016             : 
    1017             :   typedef ModulePathStringTableTy::value_type ModuleInfo;
    1018             : 
    1019             :   /// Add a new module with the given \p Hash, mapped to the given \p
    1020             :   /// ModID, and return a reference to the module.
    1021        1377 :   ModuleInfo *addModule(StringRef ModPath, uint64_t ModId,
    1022             :                         ModuleHash Hash = ModuleHash{{0}}) {
    1023        2754 :     return &*ModulePathStringTable.insert({ModPath, {ModId, Hash}}).first;
    1024             :   }
    1025             : 
    1026             :   /// Check if the given Module has any functions available for exporting
    1027             :   /// in the index. We consider any module present in the ModulePathStringTable
    1028             :   /// to have exported functions.
    1029         342 :   bool hasExportedFunctions(const Module &M) const {
    1030         684 :     return ModulePathStringTable.count(M.getModuleIdentifier());
    1031             :   }
    1032             : 
    1033             :   const std::map<std::string, TypeIdSummary> &typeIds() const {
    1034             :     return TypeIdMap;
    1035             :   }
    1036             : 
    1037             :   /// This accessor should only be used when exporting because it can mutate the
    1038             :   /// map.
    1039          69 :   TypeIdSummary &getOrInsertTypeIdSummary(StringRef TypeId) {
    1040         138 :     return TypeIdMap[TypeId];
    1041             :   }
    1042             : 
    1043             :   /// This returns either a pointer to the type id summary (if present in the
    1044             :   /// summary map) or null (if not present). This may be used when importing.
    1045          73 :   const TypeIdSummary *getTypeIdSummary(StringRef TypeId) const {
    1046          73 :     auto I = TypeIdMap.find(TypeId);
    1047          73 :     if (I == TypeIdMap.end())
    1048             :       return nullptr;
    1049          55 :     return &I->second;
    1050             :   }
    1051             : 
    1052             :   /// Collect for the given module the list of function it defines
    1053             :   /// (GUID -> Summary).
    1054             :   void collectDefinedFunctionsForModule(StringRef ModulePath,
    1055             :                                         GVSummaryMapTy &GVSummaryMap) const;
    1056             : 
    1057             :   /// Collect for each module the list of Summaries it defines (GUID ->
    1058             :   /// Summary).
    1059             :   void collectDefinedGVSummariesPerModule(
    1060             :       StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries) const;
    1061             : 
    1062             :   /// Print to an output stream.
    1063             :   void print(raw_ostream &OS, bool IsForDebug = false) const;
    1064             : 
    1065             :   /// Dump to stderr (for debugging).
    1066             :   void dump() const;
    1067             : 
    1068             :   /// Export summary to dot file for GraphViz.
    1069             :   void exportToDot(raw_ostream& OS) const;
    1070             : 
    1071             :   /// Print out strongly connected components for debugging.
    1072             :   void dumpSCCs(raw_ostream &OS);
    1073             : };
    1074             : 
    1075             : /// GraphTraits definition to build SCC for the index
    1076             : template <> struct GraphTraits<ValueInfo> {
    1077             :   typedef ValueInfo NodeRef;
    1078             : 
    1079          12 :   static NodeRef valueInfoFromEdge(FunctionSummary::EdgeTy &P) {
    1080          12 :     return P.first;
    1081             :   }
    1082             :   using ChildIteratorType =
    1083             :       mapped_iterator<std::vector<FunctionSummary::EdgeTy>::iterator,
    1084             :                       decltype(&valueInfoFromEdge)>;
    1085             : 
    1086             :   static NodeRef getEntryNode(ValueInfo V) { return V; }
    1087             : 
    1088             :   static ChildIteratorType child_begin(NodeRef N) {
    1089          10 :     if (!N.getSummaryList().size()) // handle external function
    1090             :       return ChildIteratorType(
    1091             :           FunctionSummary::ExternalNode.CallGraphEdgeList.begin(),
    1092             :           &valueInfoFromEdge);
    1093             :     FunctionSummary *F =
    1094             :         cast<FunctionSummary>(N.getSummaryList().front()->getBaseObject());
    1095             :     return ChildIteratorType(F->CallGraphEdgeList.begin(), &valueInfoFromEdge);
    1096             :   }
    1097             : 
    1098             :   static ChildIteratorType child_end(NodeRef N) {
    1099          17 :     if (!N.getSummaryList().size()) // handle external function
    1100             :       return ChildIteratorType(
    1101             :           FunctionSummary::ExternalNode.CallGraphEdgeList.end(),
    1102             :           &valueInfoFromEdge);
    1103             :     FunctionSummary *F =
    1104             :         cast<FunctionSummary>(N.getSummaryList().front()->getBaseObject());
    1105             :     return ChildIteratorType(F->CallGraphEdgeList.end(), &valueInfoFromEdge);
    1106             :   }
    1107             : };
    1108             : 
    1109             : template <>
    1110             : struct GraphTraits<ModuleSummaryIndex *> : public GraphTraits<ValueInfo> {
    1111           1 :   static NodeRef getEntryNode(ModuleSummaryIndex *I) {
    1112             :     std::unique_ptr<GlobalValueSummary> Root =
    1113           2 :         make_unique<FunctionSummary>(I->calculateCallGraphRoot());
    1114           1 :     GlobalValueSummaryInfo G(I->haveGVs());
    1115             :     G.SummaryList.push_back(std::move(Root));
    1116             :     static auto P =
    1117           2 :         GlobalValueSummaryMapTy::value_type(GlobalValue::GUID(0), std::move(G));
    1118           3 :     return ValueInfo(I->haveGVs(), &P);
    1119             :   }
    1120             : };
    1121             : 
    1122             : } // end namespace llvm
    1123             : 
    1124             : #endif // LLVM_IR_MODULESUMMARYINDEX_H

Generated by: LCOV version 1.13