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