xref: /freebsd/contrib/llvm-project/llvm/include/llvm/IR/ModuleSummaryIndex.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===- llvm/ModuleSummaryIndex.h - Module Summary Index ---------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 /// @file
10 /// ModuleSummaryIndex.h This file contains the declarations the classes that
11 ///  hold the module index and summary for function importing.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_IR_MODULESUMMARYINDEX_H
16 #define LLVM_IR_MODULESUMMARYINDEX_H
17 
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/SmallString.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/StringExtras.h"
24 #include "llvm/ADT/StringMap.h"
25 #include "llvm/ADT/StringRef.h"
26 #include "llvm/ADT/iterator_range.h"
27 #include "llvm/IR/ConstantRange.h"
28 #include "llvm/IR/GlobalValue.h"
29 #include "llvm/IR/Module.h"
30 #include "llvm/Support/Allocator.h"
31 #include "llvm/Support/Compiler.h"
32 #include "llvm/Support/InterleavedRange.h"
33 #include "llvm/Support/MathExtras.h"
34 #include "llvm/Support/ScaledNumber.h"
35 #include "llvm/Support/StringSaver.h"
36 #include "llvm/Support/raw_ostream.h"
37 #include <algorithm>
38 #include <array>
39 #include <cassert>
40 #include <cstddef>
41 #include <cstdint>
42 #include <map>
43 #include <memory>
44 #include <optional>
45 #include <set>
46 #include <string>
47 #include <unordered_set>
48 #include <utility>
49 #include <vector>
50 
51 namespace llvm {
52 
53 template <class GraphType> struct GraphTraits;
54 
55 namespace yaml {
56 
57 template <typename T> struct MappingTraits;
58 
59 } // end namespace yaml
60 
61 /// Class to accumulate and hold information about a callee.
62 struct CalleeInfo {
63   enum class HotnessType : uint8_t {
64     Unknown = 0,
65     Cold = 1,
66     None = 2,
67     Hot = 3,
68     Critical = 4
69   };
70 
71   // The size of the bit-field might need to be adjusted if more values are
72   // added to HotnessType enum.
73   uint32_t Hotness : 3;
74 
75   // True if at least one of the calls to the callee is a tail call.
76   LLVM_PREFERRED_TYPE(bool)
77   uint32_t HasTailCall : 1;
78 
79   /// The value stored in RelBlockFreq has to be interpreted as the digits of
80   /// a scaled number with a scale of \p -ScaleShift.
81   static constexpr unsigned RelBlockFreqBits = 28;
82   uint32_t RelBlockFreq : RelBlockFreqBits;
83   static constexpr int32_t ScaleShift = 8;
84   static constexpr uint64_t MaxRelBlockFreq = (1 << RelBlockFreqBits) - 1;
85 
CalleeInfoCalleeInfo86   CalleeInfo()
87       : Hotness(static_cast<uint32_t>(HotnessType::Unknown)),
88         HasTailCall(false), RelBlockFreq(0) {}
CalleeInfoCalleeInfo89   explicit CalleeInfo(HotnessType Hotness, bool HasTC, uint64_t RelBF)
90       : Hotness(static_cast<uint32_t>(Hotness)), HasTailCall(HasTC),
91         RelBlockFreq(RelBF) {}
92 
updateHotnessCalleeInfo93   void updateHotness(const HotnessType OtherHotness) {
94     Hotness = std::max(Hotness, static_cast<uint32_t>(OtherHotness));
95   }
96 
hasTailCallCalleeInfo97   bool hasTailCall() const { return HasTailCall; }
98 
setHasTailCallCalleeInfo99   void setHasTailCall(const bool HasTC) { HasTailCall = HasTC; }
100 
getHotnessCalleeInfo101   HotnessType getHotness() const { return HotnessType(Hotness); }
102 
103   /// Update \p RelBlockFreq from \p BlockFreq and \p EntryFreq
104   ///
105   /// BlockFreq is divided by EntryFreq and added to RelBlockFreq. To represent
106   /// fractional values, the result is represented as a fixed point number with
107   /// scale of -ScaleShift.
updateRelBlockFreqCalleeInfo108   void updateRelBlockFreq(uint64_t BlockFreq, uint64_t EntryFreq) {
109     if (EntryFreq == 0)
110       return;
111     using Scaled64 = ScaledNumber<uint64_t>;
112     Scaled64 Temp(BlockFreq, ScaleShift);
113     Temp /= Scaled64::get(EntryFreq);
114 
115     uint64_t Sum =
116         SaturatingAdd<uint64_t>(Temp.toInt<uint64_t>(), RelBlockFreq);
117     Sum = std::min(Sum, uint64_t(MaxRelBlockFreq));
118     RelBlockFreq = static_cast<uint32_t>(Sum);
119   }
120 };
121 
getHotnessName(CalleeInfo::HotnessType HT)122 inline const char *getHotnessName(CalleeInfo::HotnessType HT) {
123   switch (HT) {
124   case CalleeInfo::HotnessType::Unknown:
125     return "unknown";
126   case CalleeInfo::HotnessType::Cold:
127     return "cold";
128   case CalleeInfo::HotnessType::None:
129     return "none";
130   case CalleeInfo::HotnessType::Hot:
131     return "hot";
132   case CalleeInfo::HotnessType::Critical:
133     return "critical";
134   }
135   llvm_unreachable("invalid hotness");
136 }
137 
138 class GlobalValueSummary;
139 
140 using GlobalValueSummaryList = std::vector<std::unique_ptr<GlobalValueSummary>>;
141 
142 struct alignas(8) GlobalValueSummaryInfo {
143   union NameOrGV {
NameOrGV(bool HaveGVs)144     NameOrGV(bool HaveGVs) {
145       if (HaveGVs)
146         GV = nullptr;
147       else
148         Name = "";
149     }
150 
151     /// The GlobalValue corresponding to this summary. This is only used in
152     /// per-module summaries and when the IR is available. E.g. when module
153     /// analysis is being run, or when parsing both the IR and the summary
154     /// from assembly.
155     const GlobalValue *GV;
156 
157     /// Summary string representation. This StringRef points to BC module
158     /// string table and is valid until module data is stored in memory.
159     /// This is guaranteed to happen until runThinLTOBackend function is
160     /// called, so it is safe to use this field during thin link. This field
161     /// is only valid if summary index was loaded from BC file.
162     StringRef Name;
163   } U;
164 
165   inline GlobalValueSummaryInfo(bool HaveGVs);
166 
167   /// List of global value summary structures for a particular value held
168   /// in the GlobalValueMap. Requires a vector in the case of multiple
169   /// COMDAT values of the same name.
170   GlobalValueSummaryList SummaryList;
171 };
172 
173 /// Map from global value GUID to corresponding summary structures. Use a
174 /// std::map rather than a DenseMap so that pointers to the map's value_type
175 /// (which are used by ValueInfo) are not invalidated by insertion. Also it will
176 /// likely incur less overhead, as the value type is not very small and the size
177 /// of the map is unknown, resulting in inefficiencies due to repeated
178 /// insertions and resizing.
179 using GlobalValueSummaryMapTy =
180     std::map<GlobalValue::GUID, GlobalValueSummaryInfo>;
181 
182 /// Struct that holds a reference to a particular GUID in a global value
183 /// summary.
184 struct ValueInfo {
185   enum Flags { HaveGV = 1, ReadOnly = 2, WriteOnly = 4 };
186   PointerIntPair<const GlobalValueSummaryMapTy::value_type *, 3, int>
187       RefAndFlags;
188 
189   ValueInfo() = default;
ValueInfoValueInfo190   ValueInfo(bool HaveGVs, const GlobalValueSummaryMapTy::value_type *R) {
191     RefAndFlags.setPointer(R);
192     RefAndFlags.setInt(HaveGVs);
193   }
194 
195   explicit operator bool() const { return getRef(); }
196 
getGUIDValueInfo197   GlobalValue::GUID getGUID() const { return getRef()->first; }
getValueValueInfo198   const GlobalValue *getValue() const {
199     assert(haveGVs());
200     return getRef()->second.U.GV;
201   }
202 
getSummaryListValueInfo203   ArrayRef<std::unique_ptr<GlobalValueSummary>> getSummaryList() const {
204     return getRef()->second.SummaryList;
205   }
206 
207   // Even if the index is built with GVs available, we may not have one for
208   // summary entries synthesized for profiled indirect call targets.
hasNameValueInfo209   bool hasName() const { return !haveGVs() || getValue(); }
210 
nameValueInfo211   StringRef name() const {
212     assert(!haveGVs() || getRef()->second.U.GV);
213     return haveGVs() ? getRef()->second.U.GV->getName()
214                      : getRef()->second.U.Name;
215   }
216 
haveGVsValueInfo217   bool haveGVs() const { return RefAndFlags.getInt() & HaveGV; }
isReadOnlyValueInfo218   bool isReadOnly() const {
219     assert(isValidAccessSpecifier());
220     return RefAndFlags.getInt() & ReadOnly;
221   }
isWriteOnlyValueInfo222   bool isWriteOnly() const {
223     assert(isValidAccessSpecifier());
224     return RefAndFlags.getInt() & WriteOnly;
225   }
getAccessSpecifierValueInfo226   unsigned getAccessSpecifier() const {
227     assert(isValidAccessSpecifier());
228     return RefAndFlags.getInt() & (ReadOnly | WriteOnly);
229   }
isValidAccessSpecifierValueInfo230   bool isValidAccessSpecifier() const {
231     unsigned BadAccessMask = ReadOnly | WriteOnly;
232     return (RefAndFlags.getInt() & BadAccessMask) != BadAccessMask;
233   }
setReadOnlyValueInfo234   void setReadOnly() {
235     // We expect ro/wo attribute to set only once during
236     // ValueInfo lifetime.
237     assert(getAccessSpecifier() == 0);
238     RefAndFlags.setInt(RefAndFlags.getInt() | ReadOnly);
239   }
setWriteOnlyValueInfo240   void setWriteOnly() {
241     assert(getAccessSpecifier() == 0);
242     RefAndFlags.setInt(RefAndFlags.getInt() | WriteOnly);
243   }
244 
getRefValueInfo245   const GlobalValueSummaryMapTy::value_type *getRef() const {
246     return RefAndFlags.getPointer();
247   }
248 
249   /// Returns the most constraining visibility among summaries. The
250   /// visibilities, ordered from least to most constraining, are: default,
251   /// protected and hidden.
252   LLVM_ABI GlobalValue::VisibilityTypes getELFVisibility() const;
253 
254   /// Checks if all summaries are DSO local (have the flag set). When DSOLocal
255   /// propagation has been done, set the parameter to enable fast check.
256   LLVM_ABI bool isDSOLocal(bool WithDSOLocalPropagation = false) const;
257 
258   /// Checks if all copies are eligible for auto-hiding (have flag set).
259   LLVM_ABI bool canAutoHide() const;
260 };
261 
262 inline raw_ostream &operator<<(raw_ostream &OS, const ValueInfo &VI) {
263   OS << VI.getGUID();
264   if (!VI.name().empty())
265     OS << " (" << VI.name() << ")";
266   return OS;
267 }
268 
269 inline bool operator==(const ValueInfo &A, const ValueInfo &B) {
270   assert(A.getRef() && B.getRef() &&
271          "Need ValueInfo with non-null Ref for comparison");
272   return A.getRef() == B.getRef();
273 }
274 
275 inline bool operator!=(const ValueInfo &A, const ValueInfo &B) {
276   assert(A.getRef() && B.getRef() &&
277          "Need ValueInfo with non-null Ref for comparison");
278   return A.getRef() != B.getRef();
279 }
280 
281 inline bool operator<(const ValueInfo &A, const ValueInfo &B) {
282   assert(A.getRef() && B.getRef() &&
283          "Need ValueInfo with non-null Ref to compare GUIDs");
284   return A.getGUID() < B.getGUID();
285 }
286 
287 template <> struct DenseMapInfo<ValueInfo> {
288   static inline ValueInfo getEmptyKey() {
289     return ValueInfo(false, (GlobalValueSummaryMapTy::value_type *)-8);
290   }
291 
292   static inline ValueInfo getTombstoneKey() {
293     return ValueInfo(false, (GlobalValueSummaryMapTy::value_type *)-16);
294   }
295 
296   static inline bool isSpecialKey(ValueInfo V) {
297     return V == getTombstoneKey() || V == getEmptyKey();
298   }
299 
300   static bool isEqual(ValueInfo L, ValueInfo R) {
301     // We are not supposed to mix ValueInfo(s) with different HaveGVs flag
302     // in a same container.
303     assert(isSpecialKey(L) || isSpecialKey(R) || (L.haveGVs() == R.haveGVs()));
304     return L.getRef() == R.getRef();
305   }
306   static unsigned getHashValue(ValueInfo I) { return hash_value(I.getRef()); }
307 };
308 
309 // For optional hinted size reporting, holds a pair of the full stack id
310 // (pre-trimming, from the full context in the profile), and the associated
311 // total profiled size.
312 struct ContextTotalSize {
313   uint64_t FullStackId;
314   uint64_t TotalSize;
315 };
316 
317 /// Summary of memprof callsite metadata.
318 struct CallsiteInfo {
319   // Actual callee function.
320   ValueInfo Callee;
321 
322   // Used to record whole program analysis cloning decisions.
323   // The ThinLTO backend will need to create as many clones as there are entries
324   // in the vector (it is expected and should be confirmed that all such
325   // summaries in the same FunctionSummary have the same number of entries).
326   // Each index records version info for the corresponding clone of this
327   // function. The value is the callee clone it calls (becomes the appended
328   // suffix id). Index 0 is the original version, and a value of 0 calls the
329   // original callee.
330   SmallVector<unsigned> Clones{0};
331 
332   // Represents stack ids in this context, recorded as indices into the
333   // StackIds vector in the summary index, which in turn holds the full 64-bit
334   // stack ids. This reduces memory as there are in practice far fewer unique
335   // stack ids than stack id references.
336   SmallVector<unsigned> StackIdIndices;
337 
338   CallsiteInfo(ValueInfo Callee, SmallVector<unsigned> StackIdIndices)
339       : Callee(Callee), StackIdIndices(std::move(StackIdIndices)) {}
340   CallsiteInfo(ValueInfo Callee, SmallVector<unsigned> Clones,
341                SmallVector<unsigned> StackIdIndices)
342       : Callee(Callee), Clones(std::move(Clones)),
343         StackIdIndices(std::move(StackIdIndices)) {}
344 };
345 
346 inline raw_ostream &operator<<(raw_ostream &OS, const CallsiteInfo &SNI) {
347   OS << "Callee: " << SNI.Callee;
348   OS << " Clones: " << llvm::interleaved(SNI.Clones);
349   OS << " StackIds: " << llvm::interleaved(SNI.StackIdIndices);
350   return OS;
351 }
352 
353 // Allocation type assigned to an allocation reached by a given context.
354 // More can be added, now this is cold, notcold and hot.
355 // Values should be powers of two so that they can be ORed, in particular to
356 // track allocations that have different behavior with different calling
357 // contexts.
358 enum class AllocationType : uint8_t {
359   None = 0,
360   NotCold = 1,
361   Cold = 2,
362   Hot = 4,
363   All = 7 // This should always be set to the OR of all values.
364 };
365 
366 /// Summary of a single MIB in a memprof metadata on allocations.
367 struct MIBInfo {
368   // The allocation type for this profiled context.
369   AllocationType AllocType;
370 
371   // Represents stack ids in this context, recorded as indices into the
372   // StackIds vector in the summary index, which in turn holds the full 64-bit
373   // stack ids. This reduces memory as there are in practice far fewer unique
374   // stack ids than stack id references.
375   SmallVector<unsigned> StackIdIndices;
376 
377   MIBInfo(AllocationType AllocType, SmallVector<unsigned> StackIdIndices)
378       : AllocType(AllocType), StackIdIndices(std::move(StackIdIndices)) {}
379 };
380 
381 inline raw_ostream &operator<<(raw_ostream &OS, const MIBInfo &MIB) {
382   OS << "AllocType " << (unsigned)MIB.AllocType;
383   OS << " StackIds: " << llvm::interleaved(MIB.StackIdIndices);
384   return OS;
385 }
386 
387 /// Summary of memprof metadata on allocations.
388 struct AllocInfo {
389   // Used to record whole program analysis cloning decisions.
390   // The ThinLTO backend will need to create as many clones as there are entries
391   // in the vector (it is expected and should be confirmed that all such
392   // summaries in the same FunctionSummary have the same number of entries).
393   // Each index records version info for the corresponding clone of this
394   // function. The value is the allocation type of the corresponding allocation.
395   // Index 0 is the original version. Before cloning, index 0 may have more than
396   // one allocation type.
397   SmallVector<uint8_t> Versions;
398 
399   // Vector of MIBs in this memprof metadata.
400   std::vector<MIBInfo> MIBs;
401 
402   // If requested, keep track of full stack contexts and total profiled sizes
403   // for each MIB. This will be a vector of the same length and order as the
404   // MIBs vector, if non-empty. Note that each MIB in the summary can have
405   // multiple of these as we trim the contexts when possible during matching.
406   // For hinted size reporting we, however, want the original pre-trimmed full
407   // stack context id for better correlation with the profile.
408   std::vector<std::vector<ContextTotalSize>> ContextSizeInfos;
409 
410   AllocInfo(std::vector<MIBInfo> MIBs) : MIBs(std::move(MIBs)) {
411     Versions.push_back(0);
412   }
413   AllocInfo(SmallVector<uint8_t> Versions, std::vector<MIBInfo> MIBs)
414       : Versions(std::move(Versions)), MIBs(std::move(MIBs)) {}
415 };
416 
417 inline raw_ostream &operator<<(raw_ostream &OS, const AllocInfo &AE) {
418   bool First = true;
419   OS << "Versions: ";
420   for (auto V : AE.Versions) {
421     if (!First)
422       OS << ", ";
423     First = false;
424     OS << (unsigned)V;
425   }
426   OS << " MIB:\n";
427   for (auto &M : AE.MIBs) {
428     OS << "\t\t" << M << "\n";
429   }
430   if (!AE.ContextSizeInfos.empty()) {
431     OS << "\tContextSizeInfo per MIB:\n";
432     for (auto Infos : AE.ContextSizeInfos) {
433       OS << "\t\t";
434       bool FirstInfo = true;
435       for (auto [FullStackId, TotalSize] : Infos) {
436         if (!FirstInfo)
437           OS << ", ";
438         FirstInfo = false;
439         OS << "{ " << FullStackId << ", " << TotalSize << " }";
440       }
441       OS << "\n";
442     }
443   }
444   return OS;
445 }
446 
447 /// Function and variable summary information to aid decisions and
448 /// implementation of importing.
449 class GlobalValueSummary {
450 public:
451   /// Sububclass discriminator (for dyn_cast<> et al.)
452   enum SummaryKind : unsigned { AliasKind, FunctionKind, GlobalVarKind };
453 
454   enum ImportKind : unsigned {
455     // The global value definition corresponding to the summary should be
456     // imported from source module
457     Definition = 0,
458 
459     // When its definition doesn't exist in the destination module and not
460     // imported (e.g., function is too large to be inlined), the global value
461     // declaration corresponding to the summary should be imported, or the
462     // attributes from summary should be annotated on the function declaration.
463     Declaration = 1,
464   };
465 
466   /// Group flags (Linkage, NotEligibleToImport, etc.) as a bitfield.
467   struct GVFlags {
468     /// The linkage type of the associated global value.
469     ///
470     /// One use is to flag values that have local linkage types and need to
471     /// have module identifier appended before placing into the combined
472     /// index, to disambiguate from other values with the same name.
473     /// In the future this will be used to update and optimize linkage
474     /// types based on global summary-based analysis.
475     unsigned Linkage : 4;
476 
477     /// Indicates the visibility.
478     unsigned Visibility : 2;
479 
480     /// Indicate if the global value cannot be imported (e.g. it cannot
481     /// be renamed or references something that can't be renamed).
482     unsigned NotEligibleToImport : 1;
483 
484     /// In per-module summary, indicate that the global value must be considered
485     /// a live root for index-based liveness analysis. Used for special LLVM
486     /// values such as llvm.global_ctors that the linker does not know about.
487     ///
488     /// In combined summary, indicate that the global value is live.
489     unsigned Live : 1;
490 
491     /// Indicates that the linker resolved the symbol to a definition from
492     /// within the same linkage unit.
493     unsigned DSOLocal : 1;
494 
495     /// In the per-module summary, indicates that the global value is
496     /// linkonce_odr and global unnamed addr (so eligible for auto-hiding
497     /// via hidden visibility). In the combined summary, indicates that the
498     /// prevailing linkonce_odr copy can be auto-hidden via hidden visibility
499     /// when it is upgraded to weak_odr in the backend. This is legal when
500     /// all copies are eligible for auto-hiding (i.e. all copies were
501     /// linkonce_odr global unnamed addr. If any copy is not (e.g. it was
502     /// originally weak_odr, we cannot auto-hide the prevailing copy as it
503     /// means the symbol was externally visible.
504     unsigned CanAutoHide : 1;
505 
506     /// This field is written by the ThinLTO indexing step to postlink combined
507     /// summary. The value is interpreted as 'ImportKind' enum defined above.
508     unsigned ImportType : 1;
509 
510     /// Convenience Constructors
511     explicit GVFlags(GlobalValue::LinkageTypes Linkage,
512                      GlobalValue::VisibilityTypes Visibility,
513                      bool NotEligibleToImport, bool Live, bool IsLocal,
514                      bool CanAutoHide, ImportKind ImportType)
515         : Linkage(Linkage), Visibility(Visibility),
516           NotEligibleToImport(NotEligibleToImport), Live(Live),
517           DSOLocal(IsLocal), CanAutoHide(CanAutoHide),
518           ImportType(static_cast<unsigned>(ImportType)) {}
519   };
520 
521 private:
522   /// Kind of summary for use in dyn_cast<> et al.
523   SummaryKind Kind;
524 
525   GVFlags Flags;
526 
527   /// This is the hash of the name of the symbol in the original file. It is
528   /// identical to the GUID for global symbols, but differs for local since the
529   /// GUID includes the module level id in the hash.
530   GlobalValue::GUID OriginalName = 0;
531 
532   /// Path of module IR containing value's definition, used to locate
533   /// module during importing.
534   ///
535   /// This is only used during parsing of the combined index, or when
536   /// parsing the per-module index for creation of the combined summary index,
537   /// not during writing of the per-module index which doesn't contain a
538   /// module path string table.
539   StringRef ModulePath;
540 
541   /// List of values referenced by this global value's definition
542   /// (either by the initializer of a global variable, or referenced
543   /// from within a function). This does not include functions called, which
544   /// are listed in the derived FunctionSummary object.
545   /// We use SmallVector<ValueInfo, 0> instead of std::vector<ValueInfo> for its
546   /// smaller memory footprint.
547   SmallVector<ValueInfo, 0> RefEdgeList;
548 
549 protected:
550   GlobalValueSummary(SummaryKind K, GVFlags Flags,
551                      SmallVectorImpl<ValueInfo> &&Refs)
552       : Kind(K), Flags(Flags), RefEdgeList(std::move(Refs)) {
553     assert((K != AliasKind || Refs.empty()) &&
554            "Expect no references for AliasSummary");
555   }
556 
557 public:
558   virtual ~GlobalValueSummary() = default;
559 
560   /// Returns the hash of the original name, it is identical to the GUID for
561   /// externally visible symbols, but not for local ones.
562   GlobalValue::GUID getOriginalName() const { return OriginalName; }
563 
564   /// Initialize the original name hash in this summary.
565   void setOriginalName(GlobalValue::GUID Name) { OriginalName = Name; }
566 
567   /// Which kind of summary subclass this is.
568   SummaryKind getSummaryKind() const { return Kind; }
569 
570   /// Set the path to the module containing this function, for use in
571   /// the combined index.
572   void setModulePath(StringRef ModPath) { ModulePath = ModPath; }
573 
574   /// Get the path to the module containing this function.
575   StringRef modulePath() const { return ModulePath; }
576 
577   /// Get the flags for this GlobalValue (see \p struct GVFlags).
578   GVFlags flags() const { return Flags; }
579 
580   /// Return linkage type recorded for this global value.
581   GlobalValue::LinkageTypes linkage() const {
582     return static_cast<GlobalValue::LinkageTypes>(Flags.Linkage);
583   }
584 
585   /// Sets the linkage to the value determined by global summary-based
586   /// optimization. Will be applied in the ThinLTO backends.
587   void setLinkage(GlobalValue::LinkageTypes Linkage) {
588     Flags.Linkage = Linkage;
589   }
590 
591   /// Return true if this global value can't be imported.
592   bool notEligibleToImport() const { return Flags.NotEligibleToImport; }
593 
594   bool isLive() const { return Flags.Live; }
595 
596   void setLive(bool Live) { Flags.Live = Live; }
597 
598   void setDSOLocal(bool Local) { Flags.DSOLocal = Local; }
599 
600   bool isDSOLocal() const { return Flags.DSOLocal; }
601 
602   void setCanAutoHide(bool CanAutoHide) { Flags.CanAutoHide = CanAutoHide; }
603 
604   bool canAutoHide() const { return Flags.CanAutoHide; }
605 
606   bool shouldImportAsDecl() const {
607     return Flags.ImportType == GlobalValueSummary::ImportKind::Declaration;
608   }
609 
610   void setImportKind(ImportKind IK) { Flags.ImportType = IK; }
611 
612   GlobalValueSummary::ImportKind importType() const {
613     return static_cast<ImportKind>(Flags.ImportType);
614   }
615 
616   GlobalValue::VisibilityTypes getVisibility() const {
617     return (GlobalValue::VisibilityTypes)Flags.Visibility;
618   }
619   void setVisibility(GlobalValue::VisibilityTypes Vis) {
620     Flags.Visibility = (unsigned)Vis;
621   }
622 
623   /// Flag that this global value cannot be imported.
624   void setNotEligibleToImport() { Flags.NotEligibleToImport = true; }
625 
626   /// Return the list of values referenced by this global value definition.
627   ArrayRef<ValueInfo> refs() const { return RefEdgeList; }
628 
629   /// If this is an alias summary, returns the summary of the aliased object (a
630   /// global variable or function), otherwise returns itself.
631   GlobalValueSummary *getBaseObject();
632   const GlobalValueSummary *getBaseObject() const;
633 
634   friend class ModuleSummaryIndex;
635 };
636 
637 GlobalValueSummaryInfo::GlobalValueSummaryInfo(bool HaveGVs) : U(HaveGVs) {}
638 
639 /// Alias summary information.
640 class AliasSummary : public GlobalValueSummary {
641   ValueInfo AliaseeValueInfo;
642 
643   /// This is the Aliasee in the same module as alias (could get from VI, trades
644   /// memory for time). Note that this pointer may be null (and the value info
645   /// empty) when we have a distributed index where the alias is being imported
646   /// (as a copy of the aliasee), but the aliasee is not.
647   GlobalValueSummary *AliaseeSummary = nullptr;
648 
649 public:
650   AliasSummary(GVFlags Flags)
651       : GlobalValueSummary(AliasKind, Flags, SmallVector<ValueInfo, 0>{}) {}
652 
653   /// Check if this is an alias summary.
654   static bool classof(const GlobalValueSummary *GVS) {
655     return GVS->getSummaryKind() == AliasKind;
656   }
657 
658   void setAliasee(ValueInfo &AliaseeVI, GlobalValueSummary *Aliasee) {
659     AliaseeValueInfo = AliaseeVI;
660     AliaseeSummary = Aliasee;
661   }
662 
663   bool hasAliasee() const {
664     assert(!!AliaseeSummary == (AliaseeValueInfo &&
665                                 !AliaseeValueInfo.getSummaryList().empty()) &&
666            "Expect to have both aliasee summary and summary list or neither");
667     return !!AliaseeSummary;
668   }
669 
670   const GlobalValueSummary &getAliasee() const {
671     assert(AliaseeSummary && "Unexpected missing aliasee summary");
672     return *AliaseeSummary;
673   }
674 
675   GlobalValueSummary &getAliasee() {
676     return const_cast<GlobalValueSummary &>(
677                          static_cast<const AliasSummary *>(this)->getAliasee());
678   }
679   ValueInfo getAliaseeVI() const {
680     assert(AliaseeValueInfo && "Unexpected missing aliasee");
681     return AliaseeValueInfo;
682   }
683   GlobalValue::GUID getAliaseeGUID() const {
684     assert(AliaseeValueInfo && "Unexpected missing aliasee");
685     return AliaseeValueInfo.getGUID();
686   }
687 };
688 
689 const inline GlobalValueSummary *GlobalValueSummary::getBaseObject() const {
690   if (auto *AS = dyn_cast<AliasSummary>(this))
691     return &AS->getAliasee();
692   return this;
693 }
694 
695 inline GlobalValueSummary *GlobalValueSummary::getBaseObject() {
696   if (auto *AS = dyn_cast<AliasSummary>(this))
697     return &AS->getAliasee();
698   return this;
699 }
700 
701 /// Function summary information to aid decisions and implementation of
702 /// importing.
703 class FunctionSummary : public GlobalValueSummary {
704 public:
705   /// <CalleeValueInfo, CalleeInfo> call edge pair.
706   using EdgeTy = std::pair<ValueInfo, CalleeInfo>;
707 
708   /// Types for -force-summary-edges-cold debugging option.
709   enum ForceSummaryHotnessType : unsigned {
710     FSHT_None,
711     FSHT_AllNonCritical,
712     FSHT_All
713   };
714 
715   /// An "identifier" for a virtual function. This contains the type identifier
716   /// represented as a GUID and the offset from the address point to the virtual
717   /// function pointer, where "address point" is as defined in the Itanium ABI:
718   /// https://itanium-cxx-abi.github.io/cxx-abi/abi.html#vtable-general
719   struct VFuncId {
720     GlobalValue::GUID GUID;
721     uint64_t Offset;
722   };
723 
724   /// A specification for a virtual function call with all constant integer
725   /// arguments. This is used to perform virtual constant propagation on the
726   /// summary.
727   struct ConstVCall {
728     VFuncId VFunc;
729     std::vector<uint64_t> Args;
730   };
731 
732   /// All type identifier related information. Because these fields are
733   /// relatively uncommon we only allocate space for them if necessary.
734   struct TypeIdInfo {
735     /// List of type identifiers used by this function in llvm.type.test
736     /// intrinsics referenced by something other than an llvm.assume intrinsic,
737     /// represented as GUIDs.
738     std::vector<GlobalValue::GUID> TypeTests;
739 
740     /// List of virtual calls made by this function using (respectively)
741     /// llvm.assume(llvm.type.test) or llvm.type.checked.load intrinsics that do
742     /// not have all constant integer arguments.
743     std::vector<VFuncId> TypeTestAssumeVCalls, TypeCheckedLoadVCalls;
744 
745     /// List of virtual calls made by this function using (respectively)
746     /// llvm.assume(llvm.type.test) or llvm.type.checked.load intrinsics with
747     /// all constant integer arguments.
748     std::vector<ConstVCall> TypeTestAssumeConstVCalls,
749         TypeCheckedLoadConstVCalls;
750   };
751 
752   /// Flags specific to function summaries.
753   struct FFlags {
754     // Function attribute flags. Used to track if a function accesses memory,
755     // recurses or aliases.
756     unsigned ReadNone : 1;
757     unsigned ReadOnly : 1;
758     unsigned NoRecurse : 1;
759     unsigned ReturnDoesNotAlias : 1;
760 
761     // Indicate if the global value cannot be inlined.
762     unsigned NoInline : 1;
763     // Indicate if function should be always inlined.
764     unsigned AlwaysInline : 1;
765     // Indicate if function never raises an exception. Can be modified during
766     // thinlink function attribute propagation
767     unsigned NoUnwind : 1;
768     // Indicate if function contains instructions that mayThrow
769     unsigned MayThrow : 1;
770 
771     // If there are calls to unknown targets (e.g. indirect)
772     unsigned HasUnknownCall : 1;
773 
774     // Indicate if a function must be an unreachable function.
775     //
776     // This bit is sufficient but not necessary;
777     // if this bit is on, the function must be regarded as unreachable;
778     // if this bit is off, the function might be reachable or unreachable.
779     unsigned MustBeUnreachable : 1;
780 
781     FFlags &operator&=(const FFlags &RHS) {
782       this->ReadNone &= RHS.ReadNone;
783       this->ReadOnly &= RHS.ReadOnly;
784       this->NoRecurse &= RHS.NoRecurse;
785       this->ReturnDoesNotAlias &= RHS.ReturnDoesNotAlias;
786       this->NoInline &= RHS.NoInline;
787       this->AlwaysInline &= RHS.AlwaysInline;
788       this->NoUnwind &= RHS.NoUnwind;
789       this->MayThrow &= RHS.MayThrow;
790       this->HasUnknownCall &= RHS.HasUnknownCall;
791       this->MustBeUnreachable &= RHS.MustBeUnreachable;
792       return *this;
793     }
794 
795     bool anyFlagSet() {
796       return this->ReadNone | this->ReadOnly | this->NoRecurse |
797              this->ReturnDoesNotAlias | this->NoInline | this->AlwaysInline |
798              this->NoUnwind | this->MayThrow | this->HasUnknownCall |
799              this->MustBeUnreachable;
800     }
801 
802     operator std::string() {
803       std::string Output;
804       raw_string_ostream OS(Output);
805       OS << "funcFlags: (";
806       OS << "readNone: " << this->ReadNone;
807       OS << ", readOnly: " << this->ReadOnly;
808       OS << ", noRecurse: " << this->NoRecurse;
809       OS << ", returnDoesNotAlias: " << this->ReturnDoesNotAlias;
810       OS << ", noInline: " << this->NoInline;
811       OS << ", alwaysInline: " << this->AlwaysInline;
812       OS << ", noUnwind: " << this->NoUnwind;
813       OS << ", mayThrow: " << this->MayThrow;
814       OS << ", hasUnknownCall: " << this->HasUnknownCall;
815       OS << ", mustBeUnreachable: " << this->MustBeUnreachable;
816       OS << ")";
817       return Output;
818     }
819   };
820 
821   /// Describes the uses of a parameter by the function.
822   struct ParamAccess {
823     static constexpr uint32_t RangeWidth = 64;
824 
825     /// Describes the use of a value in a call instruction, specifying the
826     /// call's target, the value's parameter number, and the possible range of
827     /// offsets from the beginning of the value that are passed.
828     struct Call {
829       uint64_t ParamNo = 0;
830       ValueInfo Callee;
831       ConstantRange Offsets{/*BitWidth=*/RangeWidth, /*isFullSet=*/true};
832 
833       Call() = default;
834       Call(uint64_t ParamNo, ValueInfo Callee, const ConstantRange &Offsets)
835           : ParamNo(ParamNo), Callee(Callee), Offsets(Offsets) {}
836     };
837 
838     uint64_t ParamNo = 0;
839     /// The range contains byte offsets from the parameter pointer which
840     /// accessed by the function. In the per-module summary, it only includes
841     /// accesses made by the function instructions. In the combined summary, it
842     /// also includes accesses by nested function calls.
843     ConstantRange Use{/*BitWidth=*/RangeWidth, /*isFullSet=*/true};
844     /// In the per-module summary, it summarizes the byte offset applied to each
845     /// pointer parameter before passing to each corresponding callee.
846     /// In the combined summary, it's empty and information is propagated by
847     /// inter-procedural analysis and applied to the Use field.
848     std::vector<Call> Calls;
849 
850     ParamAccess() = default;
851     ParamAccess(uint64_t ParamNo, const ConstantRange &Use)
852         : ParamNo(ParamNo), Use(Use) {}
853   };
854 
855   /// Create an empty FunctionSummary (with specified call edges).
856   /// Used to represent external nodes and the dummy root node.
857   static FunctionSummary
858   makeDummyFunctionSummary(SmallVectorImpl<FunctionSummary::EdgeTy> &&Edges) {
859     return FunctionSummary(
860         FunctionSummary::GVFlags(
861             GlobalValue::LinkageTypes::AvailableExternallyLinkage,
862             GlobalValue::DefaultVisibility,
863             /*NotEligibleToImport=*/true, /*Live=*/true, /*IsLocal=*/false,
864             /*CanAutoHide=*/false, GlobalValueSummary::ImportKind::Definition),
865         /*NumInsts=*/0, FunctionSummary::FFlags{}, SmallVector<ValueInfo, 0>(),
866         std::move(Edges), std::vector<GlobalValue::GUID>(),
867         std::vector<FunctionSummary::VFuncId>(),
868         std::vector<FunctionSummary::VFuncId>(),
869         std::vector<FunctionSummary::ConstVCall>(),
870         std::vector<FunctionSummary::ConstVCall>(),
871         std::vector<FunctionSummary::ParamAccess>(),
872         std::vector<CallsiteInfo>(), std::vector<AllocInfo>());
873   }
874 
875   /// A dummy node to reference external functions that aren't in the index
876   LLVM_ABI static FunctionSummary ExternalNode;
877 
878 private:
879   /// Number of instructions (ignoring debug instructions, e.g.) computed
880   /// during the initial compile step when the summary index is first built.
881   unsigned InstCount;
882 
883   /// Function summary specific flags.
884   FFlags FunFlags;
885 
886   /// List of <CalleeValueInfo, CalleeInfo> call edge pairs from this function.
887   /// We use SmallVector<ValueInfo, 0> instead of std::vector<ValueInfo> for its
888   /// smaller memory footprint.
889   SmallVector<EdgeTy, 0> CallGraphEdgeList;
890 
891   std::unique_ptr<TypeIdInfo> TIdInfo;
892 
893   /// Uses for every parameter to this function.
894   using ParamAccessesTy = std::vector<ParamAccess>;
895   std::unique_ptr<ParamAccessesTy> ParamAccesses;
896 
897   /// Optional list of memprof callsite metadata summaries. The correspondence
898   /// between the callsite summary and the callsites in the function is implied
899   /// by the order in the vector (and can be validated by comparing the stack
900   /// ids in the CallsiteInfo to those in the instruction callsite metadata).
901   /// As a memory savings optimization, we only create these for the prevailing
902   /// copy of a symbol when creating the combined index during LTO.
903   using CallsitesTy = std::vector<CallsiteInfo>;
904   std::unique_ptr<CallsitesTy> Callsites;
905 
906   /// Optional list of allocation memprof metadata summaries. The correspondence
907   /// between the alloc memprof summary and the allocation callsites in the
908   /// function is implied by the order in the vector (and can be validated by
909   /// comparing the stack ids in the AllocInfo to those in the instruction
910   /// memprof metadata).
911   /// As a memory savings optimization, we only create these for the prevailing
912   /// copy of a symbol when creating the combined index during LTO.
913   using AllocsTy = std::vector<AllocInfo>;
914   std::unique_ptr<AllocsTy> Allocs;
915 
916 public:
917   FunctionSummary(GVFlags Flags, unsigned NumInsts, FFlags FunFlags,
918                   SmallVectorImpl<ValueInfo> &&Refs,
919                   SmallVectorImpl<EdgeTy> &&CGEdges,
920                   std::vector<GlobalValue::GUID> TypeTests,
921                   std::vector<VFuncId> TypeTestAssumeVCalls,
922                   std::vector<VFuncId> TypeCheckedLoadVCalls,
923                   std::vector<ConstVCall> TypeTestAssumeConstVCalls,
924                   std::vector<ConstVCall> TypeCheckedLoadConstVCalls,
925                   std::vector<ParamAccess> Params, CallsitesTy CallsiteList,
926                   AllocsTy AllocList)
927       : GlobalValueSummary(FunctionKind, Flags, std::move(Refs)),
928         InstCount(NumInsts), FunFlags(FunFlags),
929         CallGraphEdgeList(std::move(CGEdges)) {
930     if (!TypeTests.empty() || !TypeTestAssumeVCalls.empty() ||
931         !TypeCheckedLoadVCalls.empty() || !TypeTestAssumeConstVCalls.empty() ||
932         !TypeCheckedLoadConstVCalls.empty())
933       TIdInfo = std::make_unique<TypeIdInfo>(
934           TypeIdInfo{std::move(TypeTests), std::move(TypeTestAssumeVCalls),
935                      std::move(TypeCheckedLoadVCalls),
936                      std::move(TypeTestAssumeConstVCalls),
937                      std::move(TypeCheckedLoadConstVCalls)});
938     if (!Params.empty())
939       ParamAccesses = std::make_unique<ParamAccessesTy>(std::move(Params));
940     if (!CallsiteList.empty())
941       Callsites = std::make_unique<CallsitesTy>(std::move(CallsiteList));
942     if (!AllocList.empty())
943       Allocs = std::make_unique<AllocsTy>(std::move(AllocList));
944   }
945   // Gets the number of readonly and writeonly refs in RefEdgeList
946   LLVM_ABI std::pair<unsigned, unsigned> specialRefCounts() const;
947 
948   /// Check if this is a function summary.
949   static bool classof(const GlobalValueSummary *GVS) {
950     return GVS->getSummaryKind() == FunctionKind;
951   }
952 
953   /// Get function summary flags.
954   FFlags fflags() const { return FunFlags; }
955 
956   void setNoRecurse() { FunFlags.NoRecurse = true; }
957 
958   void setNoUnwind() { FunFlags.NoUnwind = true; }
959 
960   /// Get the instruction count recorded for this function.
961   unsigned instCount() const { return InstCount; }
962 
963   /// Return the list of <CalleeValueInfo, CalleeInfo> pairs.
964   ArrayRef<EdgeTy> calls() const { return CallGraphEdgeList; }
965 
966   SmallVector<EdgeTy, 0> &mutableCalls() { return CallGraphEdgeList; }
967 
968   void addCall(EdgeTy E) { CallGraphEdgeList.push_back(E); }
969 
970   /// Returns the list of type identifiers used by this function in
971   /// llvm.type.test intrinsics other than by an llvm.assume intrinsic,
972   /// represented as GUIDs.
973   ArrayRef<GlobalValue::GUID> type_tests() const {
974     if (TIdInfo)
975       return TIdInfo->TypeTests;
976     return {};
977   }
978 
979   /// Returns the list of virtual calls made by this function using
980   /// llvm.assume(llvm.type.test) intrinsics that do not have all constant
981   /// integer arguments.
982   ArrayRef<VFuncId> type_test_assume_vcalls() const {
983     if (TIdInfo)
984       return TIdInfo->TypeTestAssumeVCalls;
985     return {};
986   }
987 
988   /// Returns the list of virtual calls made by this function using
989   /// llvm.type.checked.load intrinsics that do not have all constant integer
990   /// arguments.
991   ArrayRef<VFuncId> type_checked_load_vcalls() const {
992     if (TIdInfo)
993       return TIdInfo->TypeCheckedLoadVCalls;
994     return {};
995   }
996 
997   /// Returns the list of virtual calls made by this function using
998   /// llvm.assume(llvm.type.test) intrinsics with all constant integer
999   /// arguments.
1000   ArrayRef<ConstVCall> type_test_assume_const_vcalls() const {
1001     if (TIdInfo)
1002       return TIdInfo->TypeTestAssumeConstVCalls;
1003     return {};
1004   }
1005 
1006   /// Returns the list of virtual calls made by this function using
1007   /// llvm.type.checked.load intrinsics with all constant integer arguments.
1008   ArrayRef<ConstVCall> type_checked_load_const_vcalls() const {
1009     if (TIdInfo)
1010       return TIdInfo->TypeCheckedLoadConstVCalls;
1011     return {};
1012   }
1013 
1014   /// Returns the list of known uses of pointer parameters.
1015   ArrayRef<ParamAccess> paramAccesses() const {
1016     if (ParamAccesses)
1017       return *ParamAccesses;
1018     return {};
1019   }
1020 
1021   /// Sets the list of known uses of pointer parameters.
1022   void setParamAccesses(std::vector<ParamAccess> NewParams) {
1023     if (NewParams.empty())
1024       ParamAccesses.reset();
1025     else if (ParamAccesses)
1026       *ParamAccesses = std::move(NewParams);
1027     else
1028       ParamAccesses = std::make_unique<ParamAccessesTy>(std::move(NewParams));
1029   }
1030 
1031   /// Add a type test to the summary. This is used by WholeProgramDevirt if we
1032   /// were unable to devirtualize a checked call.
1033   void addTypeTest(GlobalValue::GUID Guid) {
1034     if (!TIdInfo)
1035       TIdInfo = std::make_unique<TypeIdInfo>();
1036     TIdInfo->TypeTests.push_back(Guid);
1037   }
1038 
1039   const TypeIdInfo *getTypeIdInfo() const { return TIdInfo.get(); };
1040 
1041   ArrayRef<CallsiteInfo> callsites() const {
1042     if (Callsites)
1043       return *Callsites;
1044     return {};
1045   }
1046 
1047   CallsitesTy &mutableCallsites() {
1048     assert(Callsites);
1049     return *Callsites;
1050   }
1051 
1052   void addCallsite(CallsiteInfo &Callsite) {
1053     if (!Callsites)
1054       Callsites = std::make_unique<CallsitesTy>();
1055     Callsites->push_back(Callsite);
1056   }
1057 
1058   ArrayRef<AllocInfo> allocs() const {
1059     if (Allocs)
1060       return *Allocs;
1061     return {};
1062   }
1063 
1064   AllocsTy &mutableAllocs() {
1065     assert(Allocs);
1066     return *Allocs;
1067   }
1068 
1069   friend struct GraphTraits<ValueInfo>;
1070 };
1071 
1072 template <> struct DenseMapInfo<FunctionSummary::VFuncId> {
1073   static FunctionSummary::VFuncId getEmptyKey() { return {0, uint64_t(-1)}; }
1074 
1075   static FunctionSummary::VFuncId getTombstoneKey() {
1076     return {0, uint64_t(-2)};
1077   }
1078 
1079   static bool isEqual(FunctionSummary::VFuncId L, FunctionSummary::VFuncId R) {
1080     return L.GUID == R.GUID && L.Offset == R.Offset;
1081   }
1082 
1083   static unsigned getHashValue(FunctionSummary::VFuncId I) { return I.GUID; }
1084 };
1085 
1086 template <> struct DenseMapInfo<FunctionSummary::ConstVCall> {
1087   static FunctionSummary::ConstVCall getEmptyKey() {
1088     return {{0, uint64_t(-1)}, {}};
1089   }
1090 
1091   static FunctionSummary::ConstVCall getTombstoneKey() {
1092     return {{0, uint64_t(-2)}, {}};
1093   }
1094 
1095   static bool isEqual(FunctionSummary::ConstVCall L,
1096                       FunctionSummary::ConstVCall R) {
1097     return DenseMapInfo<FunctionSummary::VFuncId>::isEqual(L.VFunc, R.VFunc) &&
1098            L.Args == R.Args;
1099   }
1100 
1101   static unsigned getHashValue(FunctionSummary::ConstVCall I) {
1102     return I.VFunc.GUID;
1103   }
1104 };
1105 
1106 /// The ValueInfo and offset for a function within a vtable definition
1107 /// initializer array.
1108 struct VirtFuncOffset {
1109   VirtFuncOffset(ValueInfo VI, uint64_t Offset)
1110       : FuncVI(VI), VTableOffset(Offset) {}
1111 
1112   ValueInfo FuncVI;
1113   uint64_t VTableOffset;
1114 };
1115 /// List of functions referenced by a particular vtable definition.
1116 using VTableFuncList = std::vector<VirtFuncOffset>;
1117 
1118 /// Global variable summary information to aid decisions and
1119 /// implementation of importing.
1120 ///
1121 /// Global variable summary has two extra flag, telling if it is
1122 /// readonly or writeonly. Both readonly and writeonly variables
1123 /// can be optimized in the backed: readonly variables can be
1124 /// const-folded, while writeonly vars can be completely eliminated
1125 /// together with corresponding stores. We let both things happen
1126 /// by means of internalizing such variables after ThinLTO import.
1127 class GlobalVarSummary : public GlobalValueSummary {
1128 private:
1129   /// For vtable definitions this holds the list of functions and
1130   /// their corresponding offsets within the initializer array.
1131   std::unique_ptr<VTableFuncList> VTableFuncs;
1132 
1133 public:
1134   struct GVarFlags {
1135     GVarFlags(bool ReadOnly, bool WriteOnly, bool Constant,
1136               GlobalObject::VCallVisibility Vis)
1137         : MaybeReadOnly(ReadOnly), MaybeWriteOnly(WriteOnly),
1138           Constant(Constant), VCallVisibility(Vis) {}
1139 
1140     // If true indicates that this global variable might be accessed
1141     // purely by non-volatile load instructions. This in turn means
1142     // it can be internalized in source and destination modules during
1143     // thin LTO import because it neither modified nor its address
1144     // is taken.
1145     unsigned MaybeReadOnly : 1;
1146     // If true indicates that variable is possibly only written to, so
1147     // its value isn't loaded and its address isn't taken anywhere.
1148     // False, when 'Constant' attribute is set.
1149     unsigned MaybeWriteOnly : 1;
1150     // Indicates that value is a compile-time constant. Global variable
1151     // can be 'Constant' while not being 'ReadOnly' on several occasions:
1152     // - it is volatile, (e.g mapped device address)
1153     // - its address is taken, meaning that unlike 'ReadOnly' vars we can't
1154     //   internalize it.
1155     // Constant variables are always imported thus giving compiler an
1156     // opportunity to make some extra optimizations. Readonly constants
1157     // are also internalized.
1158     unsigned Constant : 1;
1159     // Set from metadata on vtable definitions during the module summary
1160     // analysis.
1161     unsigned VCallVisibility : 2;
1162   } VarFlags;
1163 
1164   GlobalVarSummary(GVFlags Flags, GVarFlags VarFlags,
1165                    SmallVectorImpl<ValueInfo> &&Refs)
1166       : GlobalValueSummary(GlobalVarKind, Flags, std::move(Refs)),
1167         VarFlags(VarFlags) {}
1168 
1169   /// Check if this is a global variable summary.
1170   static bool classof(const GlobalValueSummary *GVS) {
1171     return GVS->getSummaryKind() == GlobalVarKind;
1172   }
1173 
1174   GVarFlags varflags() const { return VarFlags; }
1175   void setReadOnly(bool RO) { VarFlags.MaybeReadOnly = RO; }
1176   void setWriteOnly(bool WO) { VarFlags.MaybeWriteOnly = WO; }
1177   bool maybeReadOnly() const { return VarFlags.MaybeReadOnly; }
1178   bool maybeWriteOnly() const { return VarFlags.MaybeWriteOnly; }
1179   bool isConstant() const { return VarFlags.Constant; }
1180   void setVCallVisibility(GlobalObject::VCallVisibility Vis) {
1181     VarFlags.VCallVisibility = Vis;
1182   }
1183   GlobalObject::VCallVisibility getVCallVisibility() const {
1184     return (GlobalObject::VCallVisibility)VarFlags.VCallVisibility;
1185   }
1186 
1187   void setVTableFuncs(VTableFuncList Funcs) {
1188     assert(!VTableFuncs);
1189     VTableFuncs = std::make_unique<VTableFuncList>(std::move(Funcs));
1190   }
1191 
1192   ArrayRef<VirtFuncOffset> vTableFuncs() const {
1193     if (VTableFuncs)
1194       return *VTableFuncs;
1195     return {};
1196   }
1197 };
1198 
1199 struct TypeTestResolution {
1200   /// Specifies which kind of type check we should emit for this byte array.
1201   /// See http://clang.llvm.org/docs/ControlFlowIntegrityDesign.html for full
1202   /// details on each kind of check; the enumerators are described with
1203   /// reference to that document.
1204   enum Kind {
1205     Unsat,     ///< Unsatisfiable type (i.e. no global has this type metadata)
1206     ByteArray, ///< Test a byte array (first example)
1207     Inline,    ///< Inlined bit vector ("Short Inline Bit Vectors")
1208     Single,    ///< Single element (last example in "Short Inline Bit Vectors")
1209     AllOnes,   ///< All-ones bit vector ("Eliminating Bit Vector Checks for
1210                ///  All-Ones Bit Vectors")
1211     Unknown,   ///< Unknown (analysis not performed, don't lower)
1212   } TheKind = Unknown;
1213 
1214   /// Range of size-1 expressed as a bit width. For example, if the size is in
1215   /// range [1,256], this number will be 8. This helps generate the most compact
1216   /// instruction sequences.
1217   unsigned SizeM1BitWidth = 0;
1218 
1219   // The following fields are only used if the target does not support the use
1220   // of absolute symbols to store constants. Their meanings are the same as the
1221   // corresponding fields in LowerTypeTestsModule::TypeIdLowering in
1222   // LowerTypeTests.cpp.
1223 
1224   uint64_t AlignLog2 = 0;
1225   uint64_t SizeM1 = 0;
1226   uint8_t BitMask = 0;
1227   uint64_t InlineBits = 0;
1228 };
1229 
1230 struct WholeProgramDevirtResolution {
1231   enum Kind {
1232     Indir,        ///< Just do a regular virtual call
1233     SingleImpl,   ///< Single implementation devirtualization
1234     BranchFunnel, ///< When retpoline mitigation is enabled, use a branch funnel
1235                   ///< that is defined in the merged module. Otherwise same as
1236                   ///< Indir.
1237   } TheKind = Indir;
1238 
1239   std::string SingleImplName;
1240 
1241   struct ByArg {
1242     enum Kind {
1243       Indir,            ///< Just do a regular virtual call
1244       UniformRetVal,    ///< Uniform return value optimization
1245       UniqueRetVal,     ///< Unique return value optimization
1246       VirtualConstProp, ///< Virtual constant propagation
1247     } TheKind = Indir;
1248 
1249     /// Additional information for the resolution:
1250     /// - UniformRetVal: the uniform return value.
1251     /// - UniqueRetVal: the return value associated with the unique vtable (0 or
1252     ///   1).
1253     uint64_t Info = 0;
1254 
1255     // The following fields are only used if the target does not support the use
1256     // of absolute symbols to store constants.
1257 
1258     uint32_t Byte = 0;
1259     uint32_t Bit = 0;
1260   };
1261 
1262   /// Resolutions for calls with all constant integer arguments (excluding the
1263   /// first argument, "this"), where the key is the argument vector.
1264   std::map<std::vector<uint64_t>, ByArg> ResByArg;
1265 };
1266 
1267 struct TypeIdSummary {
1268   TypeTestResolution TTRes;
1269 
1270   /// Mapping from byte offset to whole-program devirt resolution for that
1271   /// (typeid, byte offset) pair.
1272   std::map<uint64_t, WholeProgramDevirtResolution> WPDRes;
1273 };
1274 
1275 class CfiFunctionIndex {
1276   DenseMap<GlobalValue::GUID, std::set<std::string, std::less<>>> Index;
1277   using IndexIterator =
1278       DenseMap<GlobalValue::GUID,
1279                std::set<std::string, std::less<>>>::const_iterator;
1280   using NestedIterator = std::set<std::string, std::less<>>::const_iterator;
1281 
1282 public:
1283   // Iterates keys of the DenseMap.
1284   class GUIDIterator : public iterator_adaptor_base<GUIDIterator, IndexIterator,
1285                                                     std::forward_iterator_tag,
1286                                                     GlobalValue::GUID> {
1287     using base = GUIDIterator::iterator_adaptor_base;
1288 
1289   public:
1290     GUIDIterator() = default;
1291     explicit GUIDIterator(IndexIterator I) : base(I) {}
1292 
1293     GlobalValue::GUID operator*() const { return this->wrapped()->first; }
1294   };
1295 
1296   CfiFunctionIndex() = default;
1297   template <typename It> CfiFunctionIndex(It B, It E) {
1298     for (; B != E; ++B)
1299       emplace(*B);
1300   }
1301 
1302   std::vector<StringRef> symbols() const {
1303     std::vector<StringRef> Symbols;
1304     for (auto &[GUID, Syms] : Index) {
1305       (void)GUID;
1306       llvm::append_range(Symbols, Syms);
1307     }
1308     return Symbols;
1309   }
1310 
1311   GUIDIterator guid_begin() const { return GUIDIterator(Index.begin()); }
1312   GUIDIterator guid_end() const { return GUIDIterator(Index.end()); }
1313   iterator_range<GUIDIterator> guids() const {
1314     return make_range(guid_begin(), guid_end());
1315   }
1316 
1317   iterator_range<NestedIterator> forGuid(GlobalValue::GUID GUID) const {
1318     auto I = Index.find(GUID);
1319     if (I == Index.end())
1320       return make_range(NestedIterator{}, NestedIterator{});
1321     return make_range(I->second.begin(), I->second.end());
1322   }
1323 
1324   template <typename... Args> void emplace(Args &&...A) {
1325     StringRef S(std::forward<Args>(A)...);
1326     GlobalValue::GUID GUID = GlobalValue::getGUIDAssumingExternalLinkage(
1327         GlobalValue::dropLLVMManglingEscape(S));
1328     Index[GUID].emplace(S);
1329   }
1330 
1331   size_t count(StringRef S) const {
1332     GlobalValue::GUID GUID = GlobalValue::getGUIDAssumingExternalLinkage(
1333         GlobalValue::dropLLVMManglingEscape(S));
1334     auto I = Index.find(GUID);
1335     if (I == Index.end())
1336       return 0;
1337     return I->second.count(S);
1338   }
1339 
1340   bool empty() const { return Index.empty(); }
1341 };
1342 
1343 /// 160 bits SHA1
1344 using ModuleHash = std::array<uint32_t, 5>;
1345 
1346 /// Type used for iterating through the global value summary map.
1347 using const_gvsummary_iterator = GlobalValueSummaryMapTy::const_iterator;
1348 using gvsummary_iterator = GlobalValueSummaryMapTy::iterator;
1349 
1350 /// String table to hold/own module path strings, as well as a hash
1351 /// of the module. The StringMap makes a copy of and owns inserted strings.
1352 using ModulePathStringTableTy = StringMap<ModuleHash>;
1353 
1354 /// Map of global value GUID to its summary, used to identify values defined in
1355 /// a particular module, and provide efficient access to their summary.
1356 using GVSummaryMapTy = DenseMap<GlobalValue::GUID, GlobalValueSummary *>;
1357 
1358 /// Map of a module name to the GUIDs and summaries we will import from that
1359 /// module.
1360 using ModuleToSummariesForIndexTy =
1361     std::map<std::string, GVSummaryMapTy, std::less<>>;
1362 
1363 /// A set of global value summary pointers.
1364 using GVSummaryPtrSet = std::unordered_set<GlobalValueSummary *>;
1365 
1366 /// Map of a type GUID to type id string and summary (multimap used
1367 /// in case of GUID conflicts).
1368 using TypeIdSummaryMapTy =
1369     std::multimap<GlobalValue::GUID, std::pair<StringRef, TypeIdSummary>>;
1370 
1371 /// The following data structures summarize type metadata information.
1372 /// For type metadata overview see https://llvm.org/docs/TypeMetadata.html.
1373 /// Each type metadata includes both the type identifier and the offset of
1374 /// the address point of the type (the address held by objects of that type
1375 /// which may not be the beginning of the virtual table). Vtable definitions
1376 /// are decorated with type metadata for the types they are compatible with.
1377 ///
1378 /// Holds information about vtable definitions decorated with type metadata:
1379 /// the vtable definition value and its address point offset in a type
1380 /// identifier metadata it is decorated (compatible) with.
1381 struct TypeIdOffsetVtableInfo {
1382   TypeIdOffsetVtableInfo(uint64_t Offset, ValueInfo VI)
1383       : AddressPointOffset(Offset), VTableVI(VI) {}
1384 
1385   uint64_t AddressPointOffset;
1386   ValueInfo VTableVI;
1387 };
1388 /// List of vtable definitions decorated by a particular type identifier,
1389 /// and their corresponding offsets in that type identifier's metadata.
1390 /// Note that each type identifier may be compatible with multiple vtables, due
1391 /// to inheritance, which is why this is a vector.
1392 using TypeIdCompatibleVtableInfo = std::vector<TypeIdOffsetVtableInfo>;
1393 
1394 /// Class to hold module path string table and global value map,
1395 /// and encapsulate methods for operating on them.
1396 class ModuleSummaryIndex {
1397 private:
1398   /// Map from value name to list of summary instances for values of that
1399   /// name (may be duplicates in the COMDAT case, e.g.).
1400   GlobalValueSummaryMapTy GlobalValueMap;
1401 
1402   /// Holds strings for combined index, mapping to the corresponding module ID.
1403   ModulePathStringTableTy ModulePathStringTable;
1404 
1405   BumpPtrAllocator TypeIdSaverAlloc;
1406   UniqueStringSaver TypeIdSaver;
1407 
1408   /// Mapping from type identifier GUIDs to type identifier and its summary
1409   /// information. Produced by thin link.
1410   TypeIdSummaryMapTy TypeIdMap;
1411 
1412   /// Mapping from type identifier to information about vtables decorated
1413   /// with that type identifier's metadata. Produced by per module summary
1414   /// analysis and consumed by thin link. For more information, see description
1415   /// above where TypeIdCompatibleVtableInfo is defined.
1416   std::map<StringRef, TypeIdCompatibleVtableInfo, std::less<>>
1417       TypeIdCompatibleVtableMap;
1418 
1419   /// Mapping from original ID to GUID. If original ID can map to multiple
1420   /// GUIDs, it will be mapped to 0.
1421   DenseMap<GlobalValue::GUID, GlobalValue::GUID> OidGuidMap;
1422 
1423   /// Indicates that summary-based GlobalValue GC has run, and values with
1424   /// GVFlags::Live==false are really dead. Otherwise, all values must be
1425   /// considered live.
1426   bool WithGlobalValueDeadStripping = false;
1427 
1428   /// Indicates that summary-based attribute propagation has run and
1429   /// GVarFlags::MaybeReadonly / GVarFlags::MaybeWriteonly are really
1430   /// read/write only.
1431   bool WithAttributePropagation = false;
1432 
1433   /// Indicates that summary-based DSOLocal propagation has run and the flag in
1434   /// every summary of a GV is synchronized.
1435   bool WithDSOLocalPropagation = false;
1436 
1437   /// Indicates that we have whole program visibility.
1438   bool WithWholeProgramVisibility = false;
1439 
1440   /// Indicates that summary-based synthetic entry count propagation has run
1441   bool HasSyntheticEntryCounts = false;
1442 
1443   /// Indicates that we linked with allocator supporting hot/cold new operators.
1444   bool WithSupportsHotColdNew = false;
1445 
1446   /// Indicates that distributed backend should skip compilation of the
1447   /// module. Flag is suppose to be set by distributed ThinLTO indexing
1448   /// when it detected that the module is not needed during the final
1449   /// linking. As result distributed backend should just output a minimal
1450   /// valid object file.
1451   bool SkipModuleByDistributedBackend = false;
1452 
1453   /// If true then we're performing analysis of IR module, or parsing along with
1454   /// the IR from assembly. The value of 'false' means we're reading summary
1455   /// from BC or YAML source. Affects the type of value stored in NameOrGV
1456   /// union.
1457   bool HaveGVs;
1458 
1459   // True if the index was created for a module compiled with -fsplit-lto-unit.
1460   bool EnableSplitLTOUnit;
1461 
1462   // True if the index was created for a module compiled with -funified-lto
1463   bool UnifiedLTO;
1464 
1465   // True if some of the modules were compiled with -fsplit-lto-unit and
1466   // some were not. Set when the combined index is created during the thin link.
1467   bool PartiallySplitLTOUnits = false;
1468 
1469   /// True if some of the FunctionSummary contains a ParamAccess.
1470   bool HasParamAccess = false;
1471 
1472   CfiFunctionIndex CfiFunctionDefs;
1473   CfiFunctionIndex CfiFunctionDecls;
1474 
1475   // Used in cases where we want to record the name of a global, but
1476   // don't have the string owned elsewhere (e.g. the Strtab on a module).
1477   BumpPtrAllocator Alloc;
1478   StringSaver Saver;
1479 
1480   // The total number of basic blocks in the module in the per-module summary or
1481   // the total number of basic blocks in the LTO unit in the combined index.
1482   // FIXME: Putting this in the distributed ThinLTO index files breaks LTO
1483   // backend caching on any BB change to any linked file. It is currently not
1484   // used except in the case of a SamplePGO partial profile, and should be
1485   // reevaluated/redesigned to allow more effective incremental builds in that
1486   // case.
1487   uint64_t BlockCount = 0;
1488 
1489   // List of unique stack ids (hashes). We use a 4B index of the id in the
1490   // stack id lists on the alloc and callsite summaries for memory savings,
1491   // since the number of unique ids is in practice much smaller than the
1492   // number of stack id references in the summaries.
1493   std::vector<uint64_t> StackIds;
1494 
1495   // Temporary map while building StackIds list. Clear when index is completely
1496   // built via releaseTemporaryMemory.
1497   DenseMap<uint64_t, unsigned> StackIdToIndex;
1498 
1499   // YAML I/O support.
1500   friend yaml::MappingTraits<ModuleSummaryIndex>;
1501 
1502   GlobalValueSummaryMapTy::value_type *
1503   getOrInsertValuePtr(GlobalValue::GUID GUID) {
1504     return &*GlobalValueMap.emplace(GUID, GlobalValueSummaryInfo(HaveGVs))
1505                  .first;
1506   }
1507 
1508 public:
1509   // See HaveGVs variable comment.
1510   ModuleSummaryIndex(bool HaveGVs, bool EnableSplitLTOUnit = false,
1511                      bool UnifiedLTO = false)
1512       : TypeIdSaver(TypeIdSaverAlloc), HaveGVs(HaveGVs),
1513         EnableSplitLTOUnit(EnableSplitLTOUnit), UnifiedLTO(UnifiedLTO),
1514         Saver(Alloc) {}
1515 
1516   // Current version for the module summary in bitcode files.
1517   // The BitcodeSummaryVersion should be bumped whenever we introduce changes
1518   // in the way some record are interpreted, like flags for instance.
1519   // Note that incrementing this may require changes in both BitcodeReader.cpp
1520   // and BitcodeWriter.cpp.
1521   static constexpr uint64_t BitcodeSummaryVersion = 12;
1522 
1523   // Regular LTO module name for ASM writer
1524   static constexpr const char *getRegularLTOModuleName() {
1525     return "[Regular LTO]";
1526   }
1527 
1528   bool haveGVs() const { return HaveGVs; }
1529 
1530   LLVM_ABI uint64_t getFlags() const;
1531   LLVM_ABI void setFlags(uint64_t Flags);
1532 
1533   uint64_t getBlockCount() const { return BlockCount; }
1534   void addBlockCount(uint64_t C) { BlockCount += C; }
1535   void setBlockCount(uint64_t C) { BlockCount = C; }
1536 
1537   gvsummary_iterator begin() { return GlobalValueMap.begin(); }
1538   const_gvsummary_iterator begin() const { return GlobalValueMap.begin(); }
1539   gvsummary_iterator end() { return GlobalValueMap.end(); }
1540   const_gvsummary_iterator end() const { return GlobalValueMap.end(); }
1541   size_t size() const { return GlobalValueMap.size(); }
1542 
1543   const std::vector<uint64_t> &stackIds() const { return StackIds; }
1544 
1545   unsigned addOrGetStackIdIndex(uint64_t StackId) {
1546     auto Inserted = StackIdToIndex.insert({StackId, StackIds.size()});
1547     if (Inserted.second)
1548       StackIds.push_back(StackId);
1549     return Inserted.first->second;
1550   }
1551 
1552   uint64_t getStackIdAtIndex(unsigned Index) const {
1553     assert(StackIds.size() > Index);
1554     return StackIds[Index];
1555   }
1556 
1557   // Facility to release memory from data structures only needed during index
1558   // construction (including while building combined index). Currently this only
1559   // releases the temporary map used while constructing a correspondence between
1560   // stack ids and their index in the StackIds vector. Mostly impactful when
1561   // building a large combined index.
1562   void releaseTemporaryMemory() {
1563     assert(StackIdToIndex.size() == StackIds.size());
1564     StackIdToIndex.clear();
1565     StackIds.shrink_to_fit();
1566   }
1567 
1568   /// Convenience function for doing a DFS on a ValueInfo. Marks the function in
1569   /// the FunctionHasParent map.
1570   static void discoverNodes(ValueInfo V,
1571                             std::map<ValueInfo, bool> &FunctionHasParent) {
1572     if (!V.getSummaryList().size())
1573       return; // skip external functions that don't have summaries
1574 
1575     // Mark discovered if we haven't yet
1576     auto S = FunctionHasParent.emplace(V, false);
1577 
1578     // Stop if we've already discovered this node
1579     if (!S.second)
1580       return;
1581 
1582     FunctionSummary *F =
1583         dyn_cast<FunctionSummary>(V.getSummaryList().front().get());
1584     assert(F != nullptr && "Expected FunctionSummary node");
1585 
1586     for (const auto &C : F->calls()) {
1587       // Insert node if necessary
1588       auto S = FunctionHasParent.emplace(C.first, true);
1589 
1590       // Skip nodes that we're sure have parents
1591       if (!S.second && S.first->second)
1592         continue;
1593 
1594       if (S.second)
1595         discoverNodes(C.first, FunctionHasParent);
1596       else
1597         S.first->second = true;
1598     }
1599   }
1600 
1601   // Calculate the callgraph root
1602   FunctionSummary calculateCallGraphRoot() {
1603     // Functions that have a parent will be marked in FunctionHasParent pair.
1604     // Once we've marked all functions, the functions in the map that are false
1605     // have no parent (so they're the roots)
1606     std::map<ValueInfo, bool> FunctionHasParent;
1607 
1608     for (auto &S : *this) {
1609       // Skip external functions
1610       if (!S.second.SummaryList.size() ||
1611           !isa<FunctionSummary>(S.second.SummaryList.front().get()))
1612         continue;
1613       discoverNodes(ValueInfo(HaveGVs, &S), FunctionHasParent);
1614     }
1615 
1616     SmallVector<FunctionSummary::EdgeTy, 0> Edges;
1617     // create edges to all roots in the Index
1618     for (auto &P : FunctionHasParent) {
1619       if (P.second)
1620         continue; // skip over non-root nodes
1621       Edges.push_back(std::make_pair(P.first, CalleeInfo{}));
1622     }
1623     return FunctionSummary::makeDummyFunctionSummary(std::move(Edges));
1624   }
1625 
1626   bool withGlobalValueDeadStripping() const {
1627     return WithGlobalValueDeadStripping;
1628   }
1629   void setWithGlobalValueDeadStripping() {
1630     WithGlobalValueDeadStripping = true;
1631   }
1632 
1633   bool withAttributePropagation() const { return WithAttributePropagation; }
1634   void setWithAttributePropagation() {
1635     WithAttributePropagation = true;
1636   }
1637 
1638   bool withDSOLocalPropagation() const { return WithDSOLocalPropagation; }
1639   void setWithDSOLocalPropagation() { WithDSOLocalPropagation = true; }
1640 
1641   bool withWholeProgramVisibility() const { return WithWholeProgramVisibility; }
1642   void setWithWholeProgramVisibility() { WithWholeProgramVisibility = true; }
1643 
1644   bool isReadOnly(const GlobalVarSummary *GVS) const {
1645     return WithAttributePropagation && GVS->maybeReadOnly();
1646   }
1647   bool isWriteOnly(const GlobalVarSummary *GVS) const {
1648     return WithAttributePropagation && GVS->maybeWriteOnly();
1649   }
1650 
1651   bool withSupportsHotColdNew() const { return WithSupportsHotColdNew; }
1652   void setWithSupportsHotColdNew() { WithSupportsHotColdNew = true; }
1653 
1654   bool skipModuleByDistributedBackend() const {
1655     return SkipModuleByDistributedBackend;
1656   }
1657   void setSkipModuleByDistributedBackend() {
1658     SkipModuleByDistributedBackend = true;
1659   }
1660 
1661   bool enableSplitLTOUnit() const { return EnableSplitLTOUnit; }
1662   void setEnableSplitLTOUnit() { EnableSplitLTOUnit = true; }
1663 
1664   bool hasUnifiedLTO() const { return UnifiedLTO; }
1665   void setUnifiedLTO() { UnifiedLTO = true; }
1666 
1667   bool partiallySplitLTOUnits() const { return PartiallySplitLTOUnits; }
1668   void setPartiallySplitLTOUnits() { PartiallySplitLTOUnits = true; }
1669 
1670   bool hasParamAccess() const { return HasParamAccess; }
1671 
1672   bool isGlobalValueLive(const GlobalValueSummary *GVS) const {
1673     return !WithGlobalValueDeadStripping || GVS->isLive();
1674   }
1675   LLVM_ABI bool isGUIDLive(GlobalValue::GUID GUID) const;
1676 
1677   /// Return a ValueInfo for the index value_type (convenient when iterating
1678   /// index).
1679   ValueInfo getValueInfo(const GlobalValueSummaryMapTy::value_type &R) const {
1680     return ValueInfo(HaveGVs, &R);
1681   }
1682 
1683   /// Return a ValueInfo for GUID if it exists, otherwise return ValueInfo().
1684   ValueInfo getValueInfo(GlobalValue::GUID GUID) const {
1685     auto I = GlobalValueMap.find(GUID);
1686     return ValueInfo(HaveGVs, I == GlobalValueMap.end() ? nullptr : &*I);
1687   }
1688 
1689   /// Return a ValueInfo for \p GUID.
1690   ValueInfo getOrInsertValueInfo(GlobalValue::GUID GUID) {
1691     return ValueInfo(HaveGVs, getOrInsertValuePtr(GUID));
1692   }
1693 
1694   // Save a string in the Index. Use before passing Name to
1695   // getOrInsertValueInfo when the string isn't owned elsewhere (e.g. on the
1696   // module's Strtab).
1697   StringRef saveString(StringRef String) { return Saver.save(String); }
1698 
1699   /// Return a ValueInfo for \p GUID setting value \p Name.
1700   ValueInfo getOrInsertValueInfo(GlobalValue::GUID GUID, StringRef Name) {
1701     assert(!HaveGVs);
1702     auto VP = getOrInsertValuePtr(GUID);
1703     VP->second.U.Name = Name;
1704     return ValueInfo(HaveGVs, VP);
1705   }
1706 
1707   /// Return a ValueInfo for \p GV and mark it as belonging to GV.
1708   ValueInfo getOrInsertValueInfo(const GlobalValue *GV) {
1709     assert(HaveGVs);
1710     auto VP = getOrInsertValuePtr(GV->getGUID());
1711     VP->second.U.GV = GV;
1712     return ValueInfo(HaveGVs, VP);
1713   }
1714 
1715   /// Return the GUID for \p OriginalId in the OidGuidMap.
1716   GlobalValue::GUID getGUIDFromOriginalID(GlobalValue::GUID OriginalID) const {
1717     const auto I = OidGuidMap.find(OriginalID);
1718     return I == OidGuidMap.end() ? 0 : I->second;
1719   }
1720 
1721   CfiFunctionIndex &cfiFunctionDefs() { return CfiFunctionDefs; }
1722   const CfiFunctionIndex &cfiFunctionDefs() const { return CfiFunctionDefs; }
1723 
1724   CfiFunctionIndex &cfiFunctionDecls() { return CfiFunctionDecls; }
1725   const CfiFunctionIndex &cfiFunctionDecls() const { return CfiFunctionDecls; }
1726 
1727   /// Add a global value summary for a value.
1728   void addGlobalValueSummary(const GlobalValue &GV,
1729                              std::unique_ptr<GlobalValueSummary> Summary) {
1730     addGlobalValueSummary(getOrInsertValueInfo(&GV), std::move(Summary));
1731   }
1732 
1733   /// Add a global value summary for a value of the given name.
1734   void addGlobalValueSummary(StringRef ValueName,
1735                              std::unique_ptr<GlobalValueSummary> Summary) {
1736     addGlobalValueSummary(
1737         getOrInsertValueInfo(
1738             GlobalValue::getGUIDAssumingExternalLinkage(ValueName)),
1739         std::move(Summary));
1740   }
1741 
1742   /// Add a global value summary for the given ValueInfo.
1743   void addGlobalValueSummary(ValueInfo VI,
1744                              std::unique_ptr<GlobalValueSummary> Summary) {
1745     if (const FunctionSummary *FS = dyn_cast<FunctionSummary>(Summary.get()))
1746       HasParamAccess |= !FS->paramAccesses().empty();
1747     addOriginalName(VI.getGUID(), Summary->getOriginalName());
1748     // Here we have a notionally const VI, but the value it points to is owned
1749     // by the non-const *this.
1750     const_cast<GlobalValueSummaryMapTy::value_type *>(VI.getRef())
1751         ->second.SummaryList.push_back(std::move(Summary));
1752   }
1753 
1754   /// Add an original name for the value of the given GUID.
1755   void addOriginalName(GlobalValue::GUID ValueGUID,
1756                        GlobalValue::GUID OrigGUID) {
1757     if (OrigGUID == 0 || ValueGUID == OrigGUID)
1758       return;
1759     auto [It, Inserted] = OidGuidMap.try_emplace(OrigGUID, ValueGUID);
1760     if (!Inserted && It->second != ValueGUID)
1761       It->second = 0;
1762   }
1763 
1764   /// Find the summary for ValueInfo \p VI in module \p ModuleId, or nullptr if
1765   /// not found.
1766   GlobalValueSummary *findSummaryInModule(ValueInfo VI, StringRef ModuleId) const {
1767     auto SummaryList = VI.getSummaryList();
1768     auto Summary =
1769         llvm::find_if(SummaryList,
1770                       [&](const std::unique_ptr<GlobalValueSummary> &Summary) {
1771                         return Summary->modulePath() == ModuleId;
1772                       });
1773     if (Summary == SummaryList.end())
1774       return nullptr;
1775     return Summary->get();
1776   }
1777 
1778   /// Find the summary for global \p GUID in module \p ModuleId, or nullptr if
1779   /// not found.
1780   GlobalValueSummary *findSummaryInModule(GlobalValue::GUID ValueGUID,
1781                                           StringRef ModuleId) const {
1782     auto CalleeInfo = getValueInfo(ValueGUID);
1783     if (!CalleeInfo)
1784       return nullptr; // This function does not have a summary
1785     return findSummaryInModule(CalleeInfo, ModuleId);
1786   }
1787 
1788   /// Returns the first GlobalValueSummary for \p GV, asserting that there
1789   /// is only one if \p PerModuleIndex.
1790   GlobalValueSummary *getGlobalValueSummary(const GlobalValue &GV,
1791                                             bool PerModuleIndex = true) const {
1792     assert(GV.hasName() && "Can't get GlobalValueSummary for GV with no name");
1793     return getGlobalValueSummary(GV.getGUID(), PerModuleIndex);
1794   }
1795 
1796   /// Returns the first GlobalValueSummary for \p ValueGUID, asserting that
1797   /// there
1798   /// is only one if \p PerModuleIndex.
1799   LLVM_ABI GlobalValueSummary *
1800   getGlobalValueSummary(GlobalValue::GUID ValueGUID,
1801                         bool PerModuleIndex = true) const;
1802 
1803   /// Table of modules, containing module hash and id.
1804   const StringMap<ModuleHash> &modulePaths() const {
1805     return ModulePathStringTable;
1806   }
1807 
1808   /// Table of modules, containing hash and id.
1809   StringMap<ModuleHash> &modulePaths() { return ModulePathStringTable; }
1810 
1811   /// Get the module SHA1 hash recorded for the given module path.
1812   const ModuleHash &getModuleHash(const StringRef ModPath) const {
1813     auto It = ModulePathStringTable.find(ModPath);
1814     assert(It != ModulePathStringTable.end() && "Module not registered");
1815     return It->second;
1816   }
1817 
1818   /// Convenience method for creating a promoted global name
1819   /// for the given value name of a local, and its original module's ID.
1820   static std::string getGlobalNameForLocal(StringRef Name, ModuleHash ModHash) {
1821     std::string Suffix = utostr((uint64_t(ModHash[0]) << 32) |
1822                                 ModHash[1]); // Take the first 64 bits
1823     return getGlobalNameForLocal(Name, Suffix);
1824   }
1825 
1826   static std::string getGlobalNameForLocal(StringRef Name, StringRef Suffix) {
1827     SmallString<256> NewName(Name);
1828     NewName += ".llvm.";
1829     NewName += Suffix;
1830     return std::string(NewName);
1831   }
1832 
1833   /// Helper to obtain the unpromoted name for a global value (or the original
1834   /// name if not promoted). Split off the rightmost ".llvm.${hash}" suffix,
1835   /// because it is possible in certain clients (not clang at the moment) for
1836   /// two rounds of ThinLTO optimization and therefore promotion to occur.
1837   static StringRef getOriginalNameBeforePromote(StringRef Name) {
1838     std::pair<StringRef, StringRef> Pair = Name.rsplit(".llvm.");
1839     return Pair.first;
1840   }
1841 
1842   typedef ModulePathStringTableTy::value_type ModuleInfo;
1843 
1844   /// Add a new module with the given \p Hash, mapped to the given \p
1845   /// ModID, and return a reference to the module.
1846   ModuleInfo *addModule(StringRef ModPath, ModuleHash Hash = ModuleHash{{0}}) {
1847     return &*ModulePathStringTable.insert({ModPath, Hash}).first;
1848   }
1849 
1850   /// Return module entry for module with the given \p ModPath.
1851   ModuleInfo *getModule(StringRef ModPath) {
1852     auto It = ModulePathStringTable.find(ModPath);
1853     assert(It != ModulePathStringTable.end() && "Module not registered");
1854     return &*It;
1855   }
1856 
1857   /// Return module entry for module with the given \p ModPath.
1858   const ModuleInfo *getModule(StringRef ModPath) const {
1859     auto It = ModulePathStringTable.find(ModPath);
1860     assert(It != ModulePathStringTable.end() && "Module not registered");
1861     return &*It;
1862   }
1863 
1864   /// Check if the given Module has any functions available for exporting
1865   /// in the index. We consider any module present in the ModulePathStringTable
1866   /// to have exported functions.
1867   bool hasExportedFunctions(const Module &M) const {
1868     return ModulePathStringTable.count(M.getModuleIdentifier());
1869   }
1870 
1871   const TypeIdSummaryMapTy &typeIds() const { return TypeIdMap; }
1872 
1873   /// Return an existing or new TypeIdSummary entry for \p TypeId.
1874   /// This accessor can mutate the map and therefore should not be used in
1875   /// the ThinLTO backends.
1876   TypeIdSummary &getOrInsertTypeIdSummary(StringRef TypeId) {
1877     auto TidIter = TypeIdMap.equal_range(
1878         GlobalValue::getGUIDAssumingExternalLinkage(TypeId));
1879     for (auto &[GUID, TypeIdPair] : make_range(TidIter))
1880       if (TypeIdPair.first == TypeId)
1881         return TypeIdPair.second;
1882     auto It =
1883         TypeIdMap.insert({GlobalValue::getGUIDAssumingExternalLinkage(TypeId),
1884                           {TypeIdSaver.save(TypeId), TypeIdSummary()}});
1885     return It->second.second;
1886   }
1887 
1888   /// This returns either a pointer to the type id summary (if present in the
1889   /// summary map) or null (if not present). This may be used when importing.
1890   const TypeIdSummary *getTypeIdSummary(StringRef TypeId) const {
1891     auto TidIter = TypeIdMap.equal_range(
1892         GlobalValue::getGUIDAssumingExternalLinkage(TypeId));
1893     for (const auto &[GUID, TypeIdPair] : make_range(TidIter))
1894       if (TypeIdPair.first == TypeId)
1895         return &TypeIdPair.second;
1896     return nullptr;
1897   }
1898 
1899   TypeIdSummary *getTypeIdSummary(StringRef TypeId) {
1900     return const_cast<TypeIdSummary *>(
1901         static_cast<const ModuleSummaryIndex *>(this)->getTypeIdSummary(
1902             TypeId));
1903   }
1904 
1905   const auto &typeIdCompatibleVtableMap() const {
1906     return TypeIdCompatibleVtableMap;
1907   }
1908 
1909   /// Return an existing or new TypeIdCompatibleVtableMap entry for \p TypeId.
1910   /// This accessor can mutate the map and therefore should not be used in
1911   /// the ThinLTO backends.
1912   TypeIdCompatibleVtableInfo &
1913   getOrInsertTypeIdCompatibleVtableSummary(StringRef TypeId) {
1914     return TypeIdCompatibleVtableMap[TypeIdSaver.save(TypeId)];
1915   }
1916 
1917   /// For the given \p TypeId, this returns the TypeIdCompatibleVtableMap
1918   /// entry if present in the summary map. This may be used when importing.
1919   std::optional<TypeIdCompatibleVtableInfo>
1920   getTypeIdCompatibleVtableSummary(StringRef TypeId) const {
1921     auto I = TypeIdCompatibleVtableMap.find(TypeId);
1922     if (I == TypeIdCompatibleVtableMap.end())
1923       return std::nullopt;
1924     return I->second;
1925   }
1926 
1927   /// Collect for the given module the list of functions it defines
1928   /// (GUID -> Summary).
1929   LLVM_ABI void
1930   collectDefinedFunctionsForModule(StringRef ModulePath,
1931                                    GVSummaryMapTy &GVSummaryMap) const;
1932 
1933   /// Collect for each module the list of Summaries it defines (GUID ->
1934   /// Summary).
1935   template <class Map>
1936   void
1937   collectDefinedGVSummariesPerModule(Map &ModuleToDefinedGVSummaries) const {
1938     for (const auto &GlobalList : *this) {
1939       auto GUID = GlobalList.first;
1940       for (const auto &Summary : GlobalList.second.SummaryList) {
1941         ModuleToDefinedGVSummaries[Summary->modulePath()][GUID] = Summary.get();
1942       }
1943     }
1944   }
1945 
1946   /// Print to an output stream.
1947   LLVM_ABI void print(raw_ostream &OS, bool IsForDebug = false) const;
1948 
1949   /// Dump to stderr (for debugging).
1950   LLVM_ABI void dump() const;
1951 
1952   /// Export summary to dot file for GraphViz.
1953   LLVM_ABI void
1954   exportToDot(raw_ostream &OS,
1955               const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) const;
1956 
1957   /// Print out strongly connected components for debugging.
1958   LLVM_ABI void dumpSCCs(raw_ostream &OS);
1959 
1960   /// Do the access attribute and DSOLocal propagation in combined index.
1961   LLVM_ABI void
1962   propagateAttributes(const DenseSet<GlobalValue::GUID> &PreservedSymbols);
1963 
1964   /// Checks if we can import global variable from another module.
1965   LLVM_ABI bool canImportGlobalVar(const GlobalValueSummary *S,
1966                                    bool AnalyzeRefs) const;
1967 
1968   /// Same as above but checks whether the global var is importable as a
1969   /// declaration.
1970   LLVM_ABI bool canImportGlobalVar(const GlobalValueSummary *S,
1971                                    bool AnalyzeRefs, bool &CanImportDecl) const;
1972 };
1973 
1974 /// GraphTraits definition to build SCC for the index
1975 template <> struct GraphTraits<ValueInfo> {
1976   typedef ValueInfo NodeRef;
1977   using EdgeRef = FunctionSummary::EdgeTy &;
1978 
1979   static NodeRef valueInfoFromEdge(FunctionSummary::EdgeTy &P) {
1980     return P.first;
1981   }
1982   using ChildIteratorType =
1983       mapped_iterator<SmallVector<FunctionSummary::EdgeTy, 0>::iterator,
1984                       decltype(&valueInfoFromEdge)>;
1985 
1986   using ChildEdgeIteratorType =
1987       SmallVector<FunctionSummary::EdgeTy, 0>::iterator;
1988 
1989   static NodeRef getEntryNode(ValueInfo V) { return V; }
1990 
1991   static ChildIteratorType child_begin(NodeRef N) {
1992     if (!N.getSummaryList().size()) // handle external function
1993       return ChildIteratorType(
1994           FunctionSummary::ExternalNode.CallGraphEdgeList.begin(),
1995           &valueInfoFromEdge);
1996     FunctionSummary *F =
1997         cast<FunctionSummary>(N.getSummaryList().front()->getBaseObject());
1998     return ChildIteratorType(F->CallGraphEdgeList.begin(), &valueInfoFromEdge);
1999   }
2000 
2001   static ChildIteratorType child_end(NodeRef N) {
2002     if (!N.getSummaryList().size()) // handle external function
2003       return ChildIteratorType(
2004           FunctionSummary::ExternalNode.CallGraphEdgeList.end(),
2005           &valueInfoFromEdge);
2006     FunctionSummary *F =
2007         cast<FunctionSummary>(N.getSummaryList().front()->getBaseObject());
2008     return ChildIteratorType(F->CallGraphEdgeList.end(), &valueInfoFromEdge);
2009   }
2010 
2011   static ChildEdgeIteratorType child_edge_begin(NodeRef N) {
2012     if (!N.getSummaryList().size()) // handle external function
2013       return FunctionSummary::ExternalNode.CallGraphEdgeList.begin();
2014 
2015     FunctionSummary *F =
2016         cast<FunctionSummary>(N.getSummaryList().front()->getBaseObject());
2017     return F->CallGraphEdgeList.begin();
2018   }
2019 
2020   static ChildEdgeIteratorType child_edge_end(NodeRef N) {
2021     if (!N.getSummaryList().size()) // handle external function
2022       return FunctionSummary::ExternalNode.CallGraphEdgeList.end();
2023 
2024     FunctionSummary *F =
2025         cast<FunctionSummary>(N.getSummaryList().front()->getBaseObject());
2026     return F->CallGraphEdgeList.end();
2027   }
2028 
2029   static NodeRef edge_dest(EdgeRef E) { return E.first; }
2030 };
2031 
2032 template <>
2033 struct GraphTraits<ModuleSummaryIndex *> : public GraphTraits<ValueInfo> {
2034   static NodeRef getEntryNode(ModuleSummaryIndex *I) {
2035     std::unique_ptr<GlobalValueSummary> Root =
2036         std::make_unique<FunctionSummary>(I->calculateCallGraphRoot());
2037     GlobalValueSummaryInfo G(I->haveGVs());
2038     G.SummaryList.push_back(std::move(Root));
2039     static auto P =
2040         GlobalValueSummaryMapTy::value_type(GlobalValue::GUID(0), std::move(G));
2041     return ValueInfo(I->haveGVs(), &P);
2042   }
2043 };
2044 } // end namespace llvm
2045 
2046 #endif // LLVM_IR_MODULESUMMARYINDEX_H
2047