LLVM 19.0.0git
ModuleSummaryIndex.h
Go to the documentation of this file.
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"
24#include "llvm/ADT/StringMap.h"
25#include "llvm/ADT/StringRef.h"
27#include "llvm/IR/GlobalValue.h"
28#include "llvm/IR/Module.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
47namespace llvm {
48
49template <class GraphType> struct GraphTraits;
50
51namespace yaml {
52
53template <typename T> struct MappingTraits;
54
55} // end namespace yaml
56
57/// Class to accumulate and hold information about a callee.
58struct 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.
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;
78 static constexpr int32_t ScaleShift = 8;
79 static constexpr uint64_t MaxRelBlockFreq = (1 << RelBlockFreqBits) - 1;
80
82 : Hotness(static_cast<uint32_t>(HotnessType::Unknown)),
84 explicit CalleeInfo(HotnessType Hotness, bool HasTC, uint64_t RelBF)
85 : Hotness(static_cast<uint32_t>(Hotness)), HasTailCall(HasTC),
86 RelBlockFreq(RelBF) {}
87
88 void updateHotness(const HotnessType OtherHotness) {
89 Hotness = std::max(Hotness, static_cast<uint32_t>(OtherHotness));
90 }
91
92 bool hasTailCall() const { return HasTailCall; }
93
94 void setHasTailCall(const bool HasTC) { HasTailCall = HasTC; }
95
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.
103 void updateRelBlockFreq(uint64_t BlockFreq, uint64_t EntryFreq) {
104 if (EntryFreq == 0)
105 return;
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
118 switch (HT) {
120 return "unknown";
122 return "cold";
124 return "none";
126 return "hot";
128 return "critical";
129 }
130 llvm_unreachable("invalid hotness");
131}
132
133class GlobalValueSummary;
134
135using GlobalValueSummaryList = std::vector<std::unique_ptr<GlobalValueSummary>>;
136
137struct alignas(8) GlobalValueSummaryInfo {
138 union NameOrGV {
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.
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.
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.
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.
175 std::map<GlobalValue::GUID, GlobalValueSummaryInfo>;
176
177/// Struct that holds a reference to a particular GUID in a global value
178/// summary.
179struct ValueInfo {
180 enum Flags { HaveGV = 1, ReadOnly = 2, WriteOnly = 4 };
183
184 ValueInfo() = default;
185 ValueInfo(bool HaveGVs, const GlobalValueSummaryMapTy::value_type *R) {
187 RefAndFlags.setInt(HaveGVs);
188 }
189
190 explicit operator bool() const { return getRef(); }
191
192 GlobalValue::GUID getGUID() const { return getRef()->first; }
193 const GlobalValue *getValue() const {
194 assert(haveGVs());
195 return getRef()->second.U.GV;
196 }
197
199 return getRef()->second.SummaryList;
200 }
201
202 StringRef name() const {
203 return haveGVs() ? getRef()->second.U.GV->getName()
204 : getRef()->second.U.Name;
205 }
206
207 bool haveGVs() const { return RefAndFlags.getInt() & HaveGV; }
208 bool isReadOnly() const {
210 return RefAndFlags.getInt() & ReadOnly;
211 }
212 bool isWriteOnly() const {
214 return RefAndFlags.getInt() & WriteOnly;
215 }
216 unsigned getAccessSpecifier() const {
218 return RefAndFlags.getInt() & (ReadOnly | WriteOnly);
219 }
221 unsigned BadAccessMask = ReadOnly | WriteOnly;
222 return (RefAndFlags.getInt() & BadAccessMask) != BadAccessMask;
223 }
224 void setReadOnly() {
225 // We expect ro/wo attribute to set only once during
226 // ValueInfo lifetime.
229 }
233 }
234
235 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.
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
253 OS << VI.getGUID();
254 if (!VI.name().empty())
255 OS << " (" << VI.name() << ")";
256 return OS;
257}
258
259inline 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
265inline 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
271inline 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
277template <> 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.
301 // Actual callee function.
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.
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.
319
326};
327
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.
354enum 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.
363struct MIBInfo {
364 // The allocation type for this profiled context.
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.
372
375};
376
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.
391struct 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.
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)) {
407 }
410};
411
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.
431public:
432 /// Sububclass discriminator (for dyn_cast<> et al.)
434
435 enum ImportKind : unsigned {
436 // The global value definition corresponding to the summary should be
437 // imported from source module
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.
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).
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
494 bool NotEligibleToImport, bool Live, bool IsLocal,
499 ImportType(static_cast<unsigned>(ImportType)) {}
500 };
501
502private:
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
528protected:
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
535public:
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.
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.
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
591 return static_cast<ImportKind>(Flags.ImportType);
592 }
593
595 return (GlobalValue::VisibilityTypes)Flags.Visibility;
596 }
598 Flags.Visibility = (unsigned)Vis;
599 }
600
601 /// Flag that this global value cannot be imported.
602 void setNotEligibleToImport() { Flags.NotEligibleToImport = true; }
603
604 /// Return the list of values referenced by this global value definition.
605 ArrayRef<ValueInfo> refs() const { return RefEdgeList; }
606
607 /// If this is an alias summary, returns the summary of the aliased object (a
608 /// global variable or function), otherwise returns itself.
610 const GlobalValueSummary *getBaseObject() const;
611
612 friend class ModuleSummaryIndex;
613};
614
616
617/// Alias summary information.
619 ValueInfo AliaseeValueInfo;
620
621 /// This is the Aliasee in the same module as alias (could get from VI, trades
622 /// memory for time). Note that this pointer may be null (and the value info
623 /// empty) when we have a distributed index where the alias is being imported
624 /// (as a copy of the aliasee), but the aliasee is not.
625 GlobalValueSummary *AliaseeSummary;
626
627public:
630 AliaseeSummary(nullptr) {}
631
632 /// Check if this is an alias summary.
633 static bool classof(const GlobalValueSummary *GVS) {
634 return GVS->getSummaryKind() == AliasKind;
635 }
636
637 void setAliasee(ValueInfo &AliaseeVI, GlobalValueSummary *Aliasee) {
638 AliaseeValueInfo = AliaseeVI;
639 AliaseeSummary = Aliasee;
640 }
641
642 bool hasAliasee() const {
643 assert(!!AliaseeSummary == (AliaseeValueInfo &&
644 !AliaseeValueInfo.getSummaryList().empty()) &&
645 "Expect to have both aliasee summary and summary list or neither");
646 return !!AliaseeSummary;
647 }
648
650 assert(AliaseeSummary && "Unexpected missing aliasee summary");
651 return *AliaseeSummary;
652 }
653
655 return const_cast<GlobalValueSummary &>(
656 static_cast<const AliasSummary *>(this)->getAliasee());
657 }
659 assert(AliaseeValueInfo && "Unexpected missing aliasee");
660 return AliaseeValueInfo;
661 }
663 assert(AliaseeValueInfo && "Unexpected missing aliasee");
664 return AliaseeValueInfo.getGUID();
665 }
666};
667
669 if (auto *AS = dyn_cast<AliasSummary>(this))
670 return &AS->getAliasee();
671 return this;
672}
673
675 if (auto *AS = dyn_cast<AliasSummary>(this))
676 return &AS->getAliasee();
677 return this;
678}
679
680/// Function summary information to aid decisions and implementation of
681/// importing.
683public:
684 /// <CalleeValueInfo, CalleeInfo> call edge pair.
685 using EdgeTy = std::pair<ValueInfo, CalleeInfo>;
686
687 /// Types for -force-summary-edges-cold debugging option.
688 enum ForceSummaryHotnessType : unsigned {
692 };
693
694 /// An "identifier" for a virtual function. This contains the type identifier
695 /// represented as a GUID and the offset from the address point to the virtual
696 /// function pointer, where "address point" is as defined in the Itanium ABI:
697 /// https://itanium-cxx-abi.github.io/cxx-abi/abi.html#vtable-general
698 struct VFuncId {
701 };
702
703 /// A specification for a virtual function call with all constant integer
704 /// arguments. This is used to perform virtual constant propagation on the
705 /// summary.
706 struct ConstVCall {
708 std::vector<uint64_t> Args;
709 };
710
711 /// All type identifier related information. Because these fields are
712 /// relatively uncommon we only allocate space for them if necessary.
713 struct TypeIdInfo {
714 /// List of type identifiers used by this function in llvm.type.test
715 /// intrinsics referenced by something other than an llvm.assume intrinsic,
716 /// represented as GUIDs.
717 std::vector<GlobalValue::GUID> TypeTests;
718
719 /// List of virtual calls made by this function using (respectively)
720 /// llvm.assume(llvm.type.test) or llvm.type.checked.load intrinsics that do
721 /// not have all constant integer arguments.
723
724 /// List of virtual calls made by this function using (respectively)
725 /// llvm.assume(llvm.type.test) or llvm.type.checked.load intrinsics with
726 /// all constant integer arguments.
727 std::vector<ConstVCall> TypeTestAssumeConstVCalls,
729 };
730
731 /// Flags specific to function summaries.
732 struct FFlags {
733 // Function attribute flags. Used to track if a function accesses memory,
734 // recurses or aliases.
735 unsigned ReadNone : 1;
736 unsigned ReadOnly : 1;
737 unsigned NoRecurse : 1;
738 unsigned ReturnDoesNotAlias : 1;
739
740 // Indicate if the global value cannot be inlined.
741 unsigned NoInline : 1;
742 // Indicate if function should be always inlined.
743 unsigned AlwaysInline : 1;
744 // Indicate if function never raises an exception. Can be modified during
745 // thinlink function attribute propagation
746 unsigned NoUnwind : 1;
747 // Indicate if function contains instructions that mayThrow
748 unsigned MayThrow : 1;
749
750 // If there are calls to unknown targets (e.g. indirect)
751 unsigned HasUnknownCall : 1;
752
753 // Indicate if a function must be an unreachable function.
754 //
755 // This bit is sufficient but not necessary;
756 // if this bit is on, the function must be regarded as unreachable;
757 // if this bit is off, the function might be reachable or unreachable.
758 unsigned MustBeUnreachable : 1;
759
761 this->ReadNone &= RHS.ReadNone;
762 this->ReadOnly &= RHS.ReadOnly;
763 this->NoRecurse &= RHS.NoRecurse;
765 this->NoInline &= RHS.NoInline;
766 this->AlwaysInline &= RHS.AlwaysInline;
767 this->NoUnwind &= RHS.NoUnwind;
768 this->MayThrow &= RHS.MayThrow;
769 this->HasUnknownCall &= RHS.HasUnknownCall;
771 return *this;
772 }
773
774 bool anyFlagSet() {
775 return this->ReadNone | this->ReadOnly | this->NoRecurse |
776 this->ReturnDoesNotAlias | this->NoInline | this->AlwaysInline |
777 this->NoUnwind | this->MayThrow | this->HasUnknownCall |
778 this->MustBeUnreachable;
779 }
780
781 operator std::string() {
782 std::string Output;
783 raw_string_ostream OS(Output);
784 OS << "funcFlags: (";
785 OS << "readNone: " << this->ReadNone;
786 OS << ", readOnly: " << this->ReadOnly;
787 OS << ", noRecurse: " << this->NoRecurse;
788 OS << ", returnDoesNotAlias: " << this->ReturnDoesNotAlias;
789 OS << ", noInline: " << this->NoInline;
790 OS << ", alwaysInline: " << this->AlwaysInline;
791 OS << ", noUnwind: " << this->NoUnwind;
792 OS << ", mayThrow: " << this->MayThrow;
793 OS << ", hasUnknownCall: " << this->HasUnknownCall;
794 OS << ", mustBeUnreachable: " << this->MustBeUnreachable;
795 OS << ")";
796 return OS.str();
797 }
798 };
799
800 /// Describes the uses of a parameter by the function.
801 struct ParamAccess {
802 static constexpr uint32_t RangeWidth = 64;
803
804 /// Describes the use of a value in a call instruction, specifying the
805 /// call's target, the value's parameter number, and the possible range of
806 /// offsets from the beginning of the value that are passed.
807 struct Call {
810 ConstantRange Offsets{/*BitWidth=*/RangeWidth, /*isFullSet=*/true};
811
812 Call() = default;
815 };
816
818 /// The range contains byte offsets from the parameter pointer which
819 /// accessed by the function. In the per-module summary, it only includes
820 /// accesses made by the function instructions. In the combined summary, it
821 /// also includes accesses by nested function calls.
822 ConstantRange Use{/*BitWidth=*/RangeWidth, /*isFullSet=*/true};
823 /// In the per-module summary, it summarizes the byte offset applied to each
824 /// pointer parameter before passing to each corresponding callee.
825 /// In the combined summary, it's empty and information is propagated by
826 /// inter-procedural analysis and applied to the Use field.
827 std::vector<Call> Calls;
828
829 ParamAccess() = default;
831 : ParamNo(ParamNo), Use(Use) {}
832 };
833
834 /// Create an empty FunctionSummary (with specified call edges).
835 /// Used to represent external nodes and the dummy root node.
836 static FunctionSummary
837 makeDummyFunctionSummary(std::vector<FunctionSummary::EdgeTy> Edges) {
838 return FunctionSummary(
842 /*NotEligibleToImport=*/true, /*Live=*/true, /*IsLocal=*/false,
843 /*CanAutoHide=*/false, GlobalValueSummary::ImportKind::Definition),
844 /*NumInsts=*/0, FunctionSummary::FFlags{}, /*EntryCount=*/0,
845 std::vector<ValueInfo>(), std::move(Edges),
846 std::vector<GlobalValue::GUID>(),
847 std::vector<FunctionSummary::VFuncId>(),
848 std::vector<FunctionSummary::VFuncId>(),
849 std::vector<FunctionSummary::ConstVCall>(),
850 std::vector<FunctionSummary::ConstVCall>(),
851 std::vector<FunctionSummary::ParamAccess>(),
852 std::vector<CallsiteInfo>(), std::vector<AllocInfo>());
853 }
854
855 /// A dummy node to reference external functions that aren't in the index
857
858private:
859 /// Number of instructions (ignoring debug instructions, e.g.) computed
860 /// during the initial compile step when the summary index is first built.
861 unsigned InstCount;
862
863 /// Function summary specific flags.
864 FFlags FunFlags;
865
866 /// The synthesized entry count of the function.
867 /// This is only populated during ThinLink phase and remains unused while
868 /// generating per-module summaries.
869 uint64_t EntryCount = 0;
870
871 /// List of <CalleeValueInfo, CalleeInfo> call edge pairs from this function.
872 std::vector<EdgeTy> CallGraphEdgeList;
873
874 std::unique_ptr<TypeIdInfo> TIdInfo;
875
876 /// Uses for every parameter to this function.
877 using ParamAccessesTy = std::vector<ParamAccess>;
878 std::unique_ptr<ParamAccessesTy> ParamAccesses;
879
880 /// Optional list of memprof callsite metadata summaries. The correspondence
881 /// between the callsite summary and the callsites in the function is implied
882 /// by the order in the vector (and can be validated by comparing the stack
883 /// ids in the CallsiteInfo to those in the instruction callsite metadata).
884 /// As a memory savings optimization, we only create these for the prevailing
885 /// copy of a symbol when creating the combined index during LTO.
886 using CallsitesTy = std::vector<CallsiteInfo>;
887 std::unique_ptr<CallsitesTy> Callsites;
888
889 /// Optional list of allocation memprof metadata summaries. The correspondence
890 /// between the alloc memprof summary and the allocation callsites in the
891 /// function is implied by the order in the vector (and can be validated by
892 /// comparing the stack ids in the AllocInfo to those in the instruction
893 /// memprof metadata).
894 /// As a memory savings optimization, we only create these for the prevailing
895 /// copy of a symbol when creating the combined index during LTO.
896 using AllocsTy = std::vector<AllocInfo>;
897 std::unique_ptr<AllocsTy> Allocs;
898
899public:
900 FunctionSummary(GVFlags Flags, unsigned NumInsts, FFlags FunFlags,
901 uint64_t EntryCount, std::vector<ValueInfo> Refs,
902 std::vector<EdgeTy> CGEdges,
903 std::vector<GlobalValue::GUID> TypeTests,
904 std::vector<VFuncId> TypeTestAssumeVCalls,
905 std::vector<VFuncId> TypeCheckedLoadVCalls,
906 std::vector<ConstVCall> TypeTestAssumeConstVCalls,
907 std::vector<ConstVCall> TypeCheckedLoadConstVCalls,
908 std::vector<ParamAccess> Params, CallsitesTy CallsiteList,
909 AllocsTy AllocList)
910 : GlobalValueSummary(FunctionKind, Flags, std::move(Refs)),
911 InstCount(NumInsts), FunFlags(FunFlags), EntryCount(EntryCount),
912 CallGraphEdgeList(std::move(CGEdges)) {
913 if (!TypeTests.empty() || !TypeTestAssumeVCalls.empty() ||
914 !TypeCheckedLoadVCalls.empty() || !TypeTestAssumeConstVCalls.empty() ||
915 !TypeCheckedLoadConstVCalls.empty())
916 TIdInfo = std::make_unique<TypeIdInfo>(
917 TypeIdInfo{std::move(TypeTests), std::move(TypeTestAssumeVCalls),
918 std::move(TypeCheckedLoadVCalls),
919 std::move(TypeTestAssumeConstVCalls),
920 std::move(TypeCheckedLoadConstVCalls)});
921 if (!Params.empty())
922 ParamAccesses = std::make_unique<ParamAccessesTy>(std::move(Params));
923 if (!CallsiteList.empty())
924 Callsites = std::make_unique<CallsitesTy>(std::move(CallsiteList));
925 if (!AllocList.empty())
926 Allocs = std::make_unique<AllocsTy>(std::move(AllocList));
927 }
928 // Gets the number of readonly and writeonly refs in RefEdgeList
929 std::pair<unsigned, unsigned> specialRefCounts() const;
930
931 /// Check if this is a function summary.
932 static bool classof(const GlobalValueSummary *GVS) {
933 return GVS->getSummaryKind() == FunctionKind;
934 }
935
936 /// Get function summary flags.
937 FFlags fflags() const { return FunFlags; }
938
939 void setNoRecurse() { FunFlags.NoRecurse = true; }
940
941 void setNoUnwind() { FunFlags.NoUnwind = true; }
942
943 /// Get the instruction count recorded for this function.
944 unsigned instCount() const { return InstCount; }
945
946 /// Get the synthetic entry count for this function.
947 uint64_t entryCount() const { return EntryCount; }
948
949 /// Set the synthetic entry count for this function.
950 void setEntryCount(uint64_t EC) { EntryCount = EC; }
951
952 /// Return the list of <CalleeValueInfo, CalleeInfo> pairs.
953 ArrayRef<EdgeTy> calls() const { return CallGraphEdgeList; }
954
955 std::vector<EdgeTy> &mutableCalls() { return CallGraphEdgeList; }
956
957 void addCall(EdgeTy E) { CallGraphEdgeList.push_back(E); }
958
959 /// Returns the list of type identifiers used by this function in
960 /// llvm.type.test intrinsics other than by an llvm.assume intrinsic,
961 /// represented as GUIDs.
963 if (TIdInfo)
964 return TIdInfo->TypeTests;
965 return {};
966 }
967
968 /// Returns the list of virtual calls made by this function using
969 /// llvm.assume(llvm.type.test) intrinsics that do not have all constant
970 /// integer arguments.
972 if (TIdInfo)
973 return TIdInfo->TypeTestAssumeVCalls;
974 return {};
975 }
976
977 /// Returns the list of virtual calls made by this function using
978 /// llvm.type.checked.load intrinsics that do not have all constant integer
979 /// arguments.
981 if (TIdInfo)
982 return TIdInfo->TypeCheckedLoadVCalls;
983 return {};
984 }
985
986 /// Returns the list of virtual calls made by this function using
987 /// llvm.assume(llvm.type.test) intrinsics with all constant integer
988 /// arguments.
990 if (TIdInfo)
991 return TIdInfo->TypeTestAssumeConstVCalls;
992 return {};
993 }
994
995 /// Returns the list of virtual calls made by this function using
996 /// llvm.type.checked.load intrinsics with all constant integer arguments.
998 if (TIdInfo)
999 return TIdInfo->TypeCheckedLoadConstVCalls;
1000 return {};
1001 }
1002
1003 /// Returns the list of known uses of pointer parameters.
1005 if (ParamAccesses)
1006 return *ParamAccesses;
1007 return {};
1008 }
1009
1010 /// Sets the list of known uses of pointer parameters.
1011 void setParamAccesses(std::vector<ParamAccess> NewParams) {
1012 if (NewParams.empty())
1013 ParamAccesses.reset();
1014 else if (ParamAccesses)
1015 *ParamAccesses = std::move(NewParams);
1016 else
1017 ParamAccesses = std::make_unique<ParamAccessesTy>(std::move(NewParams));
1018 }
1019
1020 /// Add a type test to the summary. This is used by WholeProgramDevirt if we
1021 /// were unable to devirtualize a checked call.
1023 if (!TIdInfo)
1024 TIdInfo = std::make_unique<TypeIdInfo>();
1025 TIdInfo->TypeTests.push_back(Guid);
1026 }
1027
1028 const TypeIdInfo *getTypeIdInfo() const { return TIdInfo.get(); };
1029
1031 if (Callsites)
1032 return *Callsites;
1033 return {};
1034 }
1035
1036 CallsitesTy &mutableCallsites() {
1037 assert(Callsites);
1038 return *Callsites;
1039 }
1040
1041 void addCallsite(CallsiteInfo &Callsite) {
1042 if (!Callsites)
1043 Callsites = std::make_unique<CallsitesTy>();
1044 Callsites->push_back(Callsite);
1045 }
1046
1048 if (Allocs)
1049 return *Allocs;
1050 return {};
1051 }
1052
1053 AllocsTy &mutableAllocs() {
1054 assert(Allocs);
1055 return *Allocs;
1056 }
1057
1058 friend struct GraphTraits<ValueInfo>;
1059};
1060
1061template <> struct DenseMapInfo<FunctionSummary::VFuncId> {
1062 static FunctionSummary::VFuncId getEmptyKey() { return {0, uint64_t(-1)}; }
1063
1065 return {0, uint64_t(-2)};
1066 }
1067
1069 return L.GUID == R.GUID && L.Offset == R.Offset;
1070 }
1071
1072 static unsigned getHashValue(FunctionSummary::VFuncId I) { return I.GUID; }
1073};
1074
1075template <> struct DenseMapInfo<FunctionSummary::ConstVCall> {
1077 return {{0, uint64_t(-1)}, {}};
1078 }
1079
1081 return {{0, uint64_t(-2)}, {}};
1082 }
1083
1086 return DenseMapInfo<FunctionSummary::VFuncId>::isEqual(L.VFunc, R.VFunc) &&
1087 L.Args == R.Args;
1088 }
1089
1091 return I.VFunc.GUID;
1092 }
1093};
1094
1095/// The ValueInfo and offset for a function within a vtable definition
1096/// initializer array.
1099 : FuncVI(VI), VTableOffset(Offset) {}
1100
1103};
1104/// List of functions referenced by a particular vtable definition.
1105using VTableFuncList = std::vector<VirtFuncOffset>;
1106
1107/// Global variable summary information to aid decisions and
1108/// implementation of importing.
1109///
1110/// Global variable summary has two extra flag, telling if it is
1111/// readonly or writeonly. Both readonly and writeonly variables
1112/// can be optimized in the backed: readonly variables can be
1113/// const-folded, while writeonly vars can be completely eliminated
1114/// together with corresponding stores. We let both things happen
1115/// by means of internalizing such variables after ThinLTO import.
1117private:
1118 /// For vtable definitions this holds the list of functions and
1119 /// their corresponding offsets within the initializer array.
1120 std::unique_ptr<VTableFuncList> VTableFuncs;
1121
1122public:
1123 struct GVarFlags {
1124 GVarFlags(bool ReadOnly, bool WriteOnly, bool Constant,
1126 : MaybeReadOnly(ReadOnly), MaybeWriteOnly(WriteOnly),
1128
1129 // If true indicates that this global variable might be accessed
1130 // purely by non-volatile load instructions. This in turn means
1131 // it can be internalized in source and destination modules during
1132 // thin LTO import because it neither modified nor its address
1133 // is taken.
1134 unsigned MaybeReadOnly : 1;
1135 // If true indicates that variable is possibly only written to, so
1136 // its value isn't loaded and its address isn't taken anywhere.
1137 // False, when 'Constant' attribute is set.
1138 unsigned MaybeWriteOnly : 1;
1139 // Indicates that value is a compile-time constant. Global variable
1140 // can be 'Constant' while not being 'ReadOnly' on several occasions:
1141 // - it is volatile, (e.g mapped device address)
1142 // - its address is taken, meaning that unlike 'ReadOnly' vars we can't
1143 // internalize it.
1144 // Constant variables are always imported thus giving compiler an
1145 // opportunity to make some extra optimizations. Readonly constants
1146 // are also internalized.
1147 unsigned Constant : 1;
1148 // Set from metadata on vtable definitions during the module summary
1149 // analysis.
1150 unsigned VCallVisibility : 2;
1152
1154 std::vector<ValueInfo> Refs)
1155 : GlobalValueSummary(GlobalVarKind, Flags, std::move(Refs)),
1156 VarFlags(VarFlags) {}
1157
1158 /// Check if this is a global variable summary.
1159 static bool classof(const GlobalValueSummary *GVS) {
1160 return GVS->getSummaryKind() == GlobalVarKind;
1161 }
1162
1163 GVarFlags varflags() const { return VarFlags; }
1164 void setReadOnly(bool RO) { VarFlags.MaybeReadOnly = RO; }
1165 void setWriteOnly(bool WO) { VarFlags.MaybeWriteOnly = WO; }
1166 bool maybeReadOnly() const { return VarFlags.MaybeReadOnly; }
1167 bool maybeWriteOnly() const { return VarFlags.MaybeWriteOnly; }
1168 bool isConstant() const { return VarFlags.Constant; }
1171 }
1174 }
1175
1177 assert(!VTableFuncs);
1178 VTableFuncs = std::make_unique<VTableFuncList>(std::move(Funcs));
1179 }
1180
1182 if (VTableFuncs)
1183 return *VTableFuncs;
1184 return {};
1185 }
1186};
1187
1189 /// Specifies which kind of type check we should emit for this byte array.
1190 /// See http://clang.llvm.org/docs/ControlFlowIntegrityDesign.html for full
1191 /// details on each kind of check; the enumerators are described with
1192 /// reference to that document.
1193 enum Kind {
1194 Unsat, ///< Unsatisfiable type (i.e. no global has this type metadata)
1195 ByteArray, ///< Test a byte array (first example)
1196 Inline, ///< Inlined bit vector ("Short Inline Bit Vectors")
1197 Single, ///< Single element (last example in "Short Inline Bit Vectors")
1198 AllOnes, ///< All-ones bit vector ("Eliminating Bit Vector Checks for
1199 /// All-Ones Bit Vectors")
1200 Unknown, ///< Unknown (analysis not performed, don't lower)
1202
1203 /// Range of size-1 expressed as a bit width. For example, if the size is in
1204 /// range [1,256], this number will be 8. This helps generate the most compact
1205 /// instruction sequences.
1206 unsigned SizeM1BitWidth = 0;
1207
1208 // The following fields are only used if the target does not support the use
1209 // of absolute symbols to store constants. Their meanings are the same as the
1210 // corresponding fields in LowerTypeTestsModule::TypeIdLowering in
1211 // LowerTypeTests.cpp.
1212
1215 uint8_t BitMask = 0;
1217};
1218
1220 enum Kind {
1221 Indir, ///< Just do a regular virtual call
1222 SingleImpl, ///< Single implementation devirtualization
1223 BranchFunnel, ///< When retpoline mitigation is enabled, use a branch funnel
1224 ///< that is defined in the merged module. Otherwise same as
1225 ///< Indir.
1227
1228 std::string SingleImplName;
1229
1230 struct ByArg {
1231 enum Kind {
1232 Indir, ///< Just do a regular virtual call
1233 UniformRetVal, ///< Uniform return value optimization
1234 UniqueRetVal, ///< Unique return value optimization
1235 VirtualConstProp, ///< Virtual constant propagation
1237
1238 /// Additional information for the resolution:
1239 /// - UniformRetVal: the uniform return value.
1240 /// - UniqueRetVal: the return value associated with the unique vtable (0 or
1241 /// 1).
1243
1244 // The following fields are only used if the target does not support the use
1245 // of absolute symbols to store constants.
1246
1249 };
1250
1251 /// Resolutions for calls with all constant integer arguments (excluding the
1252 /// first argument, "this"), where the key is the argument vector.
1253 std::map<std::vector<uint64_t>, ByArg> ResByArg;
1254};
1255
1258
1259 /// Mapping from byte offset to whole-program devirt resolution for that
1260 /// (typeid, byte offset) pair.
1261 std::map<uint64_t, WholeProgramDevirtResolution> WPDRes;
1262};
1263
1264/// 160 bits SHA1
1265using ModuleHash = std::array<uint32_t, 5>;
1266
1267/// Type used for iterating through the global value summary map.
1268using const_gvsummary_iterator = GlobalValueSummaryMapTy::const_iterator;
1269using gvsummary_iterator = GlobalValueSummaryMapTy::iterator;
1270
1271/// String table to hold/own module path strings, as well as a hash
1272/// of the module. The StringMap makes a copy of and owns inserted strings.
1274
1275/// Map of global value GUID to its summary, used to identify values defined in
1276/// a particular module, and provide efficient access to their summary.
1278
1279/// A set of global value summary pointers.
1281
1282/// Map of a type GUID to type id string and summary (multimap used
1283/// in case of GUID conflicts).
1285 std::multimap<GlobalValue::GUID, std::pair<std::string, TypeIdSummary>>;
1286
1287/// The following data structures summarize type metadata information.
1288/// For type metadata overview see https://llvm.org/docs/TypeMetadata.html.
1289/// Each type metadata includes both the type identifier and the offset of
1290/// the address point of the type (the address held by objects of that type
1291/// which may not be the beginning of the virtual table). Vtable definitions
1292/// are decorated with type metadata for the types they are compatible with.
1293///
1294/// Holds information about vtable definitions decorated with type metadata:
1295/// the vtable definition value and its address point offset in a type
1296/// identifier metadata it is decorated (compatible) with.
1300
1303};
1304/// List of vtable definitions decorated by a particular type identifier,
1305/// and their corresponding offsets in that type identifier's metadata.
1306/// Note that each type identifier may be compatible with multiple vtables, due
1307/// to inheritance, which is why this is a vector.
1308using TypeIdCompatibleVtableInfo = std::vector<TypeIdOffsetVtableInfo>;
1309
1310/// Class to hold module path string table and global value map,
1311/// and encapsulate methods for operating on them.
1313private:
1314 /// Map from value name to list of summary instances for values of that
1315 /// name (may be duplicates in the COMDAT case, e.g.).
1316 GlobalValueSummaryMapTy GlobalValueMap;
1317
1318 /// Holds strings for combined index, mapping to the corresponding module ID.
1319 ModulePathStringTableTy ModulePathStringTable;
1320
1321 /// Mapping from type identifier GUIDs to type identifier and its summary
1322 /// information. Produced by thin link.
1323 TypeIdSummaryMapTy TypeIdMap;
1324
1325 /// Mapping from type identifier to information about vtables decorated
1326 /// with that type identifier's metadata. Produced by per module summary
1327 /// analysis and consumed by thin link. For more information, see description
1328 /// above where TypeIdCompatibleVtableInfo is defined.
1329 std::map<std::string, TypeIdCompatibleVtableInfo, std::less<>>
1330 TypeIdCompatibleVtableMap;
1331
1332 /// Mapping from original ID to GUID. If original ID can map to multiple
1333 /// GUIDs, it will be mapped to 0.
1334 std::map<GlobalValue::GUID, GlobalValue::GUID> OidGuidMap;
1335
1336 /// Indicates that summary-based GlobalValue GC has run, and values with
1337 /// GVFlags::Live==false are really dead. Otherwise, all values must be
1338 /// considered live.
1339 bool WithGlobalValueDeadStripping = false;
1340
1341 /// Indicates that summary-based attribute propagation has run and
1342 /// GVarFlags::MaybeReadonly / GVarFlags::MaybeWriteonly are really
1343 /// read/write only.
1344 bool WithAttributePropagation = false;
1345
1346 /// Indicates that summary-based DSOLocal propagation has run and the flag in
1347 /// every summary of a GV is synchronized.
1348 bool WithDSOLocalPropagation = false;
1349
1350 /// Indicates that we have whole program visibility.
1351 bool WithWholeProgramVisibility = false;
1352
1353 /// Indicates that summary-based synthetic entry count propagation has run
1354 bool HasSyntheticEntryCounts = false;
1355
1356 /// Indicates that we linked with allocator supporting hot/cold new operators.
1357 bool WithSupportsHotColdNew = false;
1358
1359 /// Indicates that distributed backend should skip compilation of the
1360 /// module. Flag is suppose to be set by distributed ThinLTO indexing
1361 /// when it detected that the module is not needed during the final
1362 /// linking. As result distributed backend should just output a minimal
1363 /// valid object file.
1364 bool SkipModuleByDistributedBackend = false;
1365
1366 /// If true then we're performing analysis of IR module, or parsing along with
1367 /// the IR from assembly. The value of 'false' means we're reading summary
1368 /// from BC or YAML source. Affects the type of value stored in NameOrGV
1369 /// union.
1370 bool HaveGVs;
1371
1372 // True if the index was created for a module compiled with -fsplit-lto-unit.
1373 bool EnableSplitLTOUnit;
1374
1375 // True if the index was created for a module compiled with -funified-lto
1376 bool UnifiedLTO;
1377
1378 // True if some of the modules were compiled with -fsplit-lto-unit and
1379 // some were not. Set when the combined index is created during the thin link.
1380 bool PartiallySplitLTOUnits = false;
1381
1382 /// True if some of the FunctionSummary contains a ParamAccess.
1383 bool HasParamAccess = false;
1384
1385 std::set<std::string> CfiFunctionDefs;
1386 std::set<std::string> CfiFunctionDecls;
1387
1388 // Used in cases where we want to record the name of a global, but
1389 // don't have the string owned elsewhere (e.g. the Strtab on a module).
1390 BumpPtrAllocator Alloc;
1391 StringSaver Saver;
1392
1393 // The total number of basic blocks in the module in the per-module summary or
1394 // the total number of basic blocks in the LTO unit in the combined index.
1395 // FIXME: Putting this in the distributed ThinLTO index files breaks LTO
1396 // backend caching on any BB change to any linked file. It is currently not
1397 // used except in the case of a SamplePGO partial profile, and should be
1398 // reevaluated/redesigned to allow more effective incremental builds in that
1399 // case.
1400 uint64_t BlockCount;
1401
1402 // List of unique stack ids (hashes). We use a 4B index of the id in the
1403 // stack id lists on the alloc and callsite summaries for memory savings,
1404 // since the number of unique ids is in practice much smaller than the
1405 // number of stack id references in the summaries.
1406 std::vector<uint64_t> StackIds;
1407
1408 // Temporary map while building StackIds list. Clear when index is completely
1409 // built via releaseTemporaryMemory.
1410 DenseMap<uint64_t, unsigned> StackIdToIndex;
1411
1412 // YAML I/O support.
1414
1415 GlobalValueSummaryMapTy::value_type *
1416 getOrInsertValuePtr(GlobalValue::GUID GUID) {
1417 return &*GlobalValueMap.emplace(GUID, GlobalValueSummaryInfo(HaveGVs))
1418 .first;
1419 }
1420
1421public:
1422 // See HaveGVs variable comment.
1423 ModuleSummaryIndex(bool HaveGVs, bool EnableSplitLTOUnit = false,
1424 bool UnifiedLTO = false)
1425 : HaveGVs(HaveGVs), EnableSplitLTOUnit(EnableSplitLTOUnit),
1426 UnifiedLTO(UnifiedLTO), Saver(Alloc), BlockCount(0) {}
1427
1428 // Current version for the module summary in bitcode files.
1429 // The BitcodeSummaryVersion should be bumped whenever we introduce changes
1430 // in the way some record are interpreted, like flags for instance.
1431 // Note that incrementing this may require changes in both BitcodeReader.cpp
1432 // and BitcodeWriter.cpp.
1433 static constexpr uint64_t BitcodeSummaryVersion = 9;
1434
1435 // Regular LTO module name for ASM writer
1436 static constexpr const char *getRegularLTOModuleName() {
1437 return "[Regular LTO]";
1438 }
1439
1440 bool haveGVs() const { return HaveGVs; }
1441
1442 uint64_t getFlags() const;
1443 void setFlags(uint64_t Flags);
1444
1445 uint64_t getBlockCount() const { return BlockCount; }
1446 void addBlockCount(uint64_t C) { BlockCount += C; }
1447 void setBlockCount(uint64_t C) { BlockCount = C; }
1448
1449 gvsummary_iterator begin() { return GlobalValueMap.begin(); }
1450 const_gvsummary_iterator begin() const { return GlobalValueMap.begin(); }
1451 gvsummary_iterator end() { return GlobalValueMap.end(); }
1452 const_gvsummary_iterator end() const { return GlobalValueMap.end(); }
1453 size_t size() const { return GlobalValueMap.size(); }
1454
1455 const std::vector<uint64_t> &stackIds() const { return StackIds; }
1456
1457 unsigned addOrGetStackIdIndex(uint64_t StackId) {
1458 auto Inserted = StackIdToIndex.insert({StackId, StackIds.size()});
1459 if (Inserted.second)
1460 StackIds.push_back(StackId);
1461 return Inserted.first->second;
1462 }
1463
1465 assert(StackIds.size() > Index);
1466 return StackIds[Index];
1467 }
1468
1469 // Facility to release memory from data structures only needed during index
1470 // construction (including while building combined index). Currently this only
1471 // releases the temporary map used while constructing a correspondence between
1472 // stack ids and their index in the StackIds vector. Mostly impactful when
1473 // building a large combined index.
1475 assert(StackIdToIndex.size() == StackIds.size());
1476 StackIdToIndex.clear();
1477 StackIds.shrink_to_fit();
1478 }
1479
1480 /// Convenience function for doing a DFS on a ValueInfo. Marks the function in
1481 /// the FunctionHasParent map.
1483 std::map<ValueInfo, bool> &FunctionHasParent) {
1484 if (!V.getSummaryList().size())
1485 return; // skip external functions that don't have summaries
1486
1487 // Mark discovered if we haven't yet
1488 auto S = FunctionHasParent.emplace(V, false);
1489
1490 // Stop if we've already discovered this node
1491 if (!S.second)
1492 return;
1493
1495 dyn_cast<FunctionSummary>(V.getSummaryList().front().get());
1496 assert(F != nullptr && "Expected FunctionSummary node");
1497
1498 for (const auto &C : F->calls()) {
1499 // Insert node if necessary
1500 auto S = FunctionHasParent.emplace(C.first, true);
1501
1502 // Skip nodes that we're sure have parents
1503 if (!S.second && S.first->second)
1504 continue;
1505
1506 if (S.second)
1507 discoverNodes(C.first, FunctionHasParent);
1508 else
1509 S.first->second = true;
1510 }
1511 }
1512
1513 // Calculate the callgraph root
1515 // Functions that have a parent will be marked in FunctionHasParent pair.
1516 // Once we've marked all functions, the functions in the map that are false
1517 // have no parent (so they're the roots)
1518 std::map<ValueInfo, bool> FunctionHasParent;
1519
1520 for (auto &S : *this) {
1521 // Skip external functions
1522 if (!S.second.SummaryList.size() ||
1523 !isa<FunctionSummary>(S.second.SummaryList.front().get()))
1524 continue;
1525 discoverNodes(ValueInfo(HaveGVs, &S), FunctionHasParent);
1526 }
1527
1528 std::vector<FunctionSummary::EdgeTy> Edges;
1529 // create edges to all roots in the Index
1530 for (auto &P : FunctionHasParent) {
1531 if (P.second)
1532 continue; // skip over non-root nodes
1533 Edges.push_back(std::make_pair(P.first, CalleeInfo{}));
1534 }
1535 if (Edges.empty()) {
1536 // Failed to find root - return an empty node
1538 }
1539 auto CallGraphRoot = FunctionSummary::makeDummyFunctionSummary(Edges);
1540 return CallGraphRoot;
1541 }
1542
1544 return WithGlobalValueDeadStripping;
1545 }
1547 WithGlobalValueDeadStripping = true;
1548 }
1549
1550 bool withAttributePropagation() const { return WithAttributePropagation; }
1552 WithAttributePropagation = true;
1553 }
1554
1555 bool withDSOLocalPropagation() const { return WithDSOLocalPropagation; }
1556 void setWithDSOLocalPropagation() { WithDSOLocalPropagation = true; }
1557
1558 bool withWholeProgramVisibility() const { return WithWholeProgramVisibility; }
1559 void setWithWholeProgramVisibility() { WithWholeProgramVisibility = true; }
1560
1561 bool isReadOnly(const GlobalVarSummary *GVS) const {
1562 return WithAttributePropagation && GVS->maybeReadOnly();
1563 }
1564 bool isWriteOnly(const GlobalVarSummary *GVS) const {
1565 return WithAttributePropagation && GVS->maybeWriteOnly();
1566 }
1567
1568 bool hasSyntheticEntryCounts() const { return HasSyntheticEntryCounts; }
1569 void setHasSyntheticEntryCounts() { HasSyntheticEntryCounts = true; }
1570
1571 bool withSupportsHotColdNew() const { return WithSupportsHotColdNew; }
1572 void setWithSupportsHotColdNew() { WithSupportsHotColdNew = true; }
1573
1575 return SkipModuleByDistributedBackend;
1576 }
1578 SkipModuleByDistributedBackend = true;
1579 }
1580
1581 bool enableSplitLTOUnit() const { return EnableSplitLTOUnit; }
1582 void setEnableSplitLTOUnit() { EnableSplitLTOUnit = true; }
1583
1584 bool hasUnifiedLTO() const { return UnifiedLTO; }
1585 void setUnifiedLTO() { UnifiedLTO = true; }
1586
1587 bool partiallySplitLTOUnits() const { return PartiallySplitLTOUnits; }
1588 void setPartiallySplitLTOUnits() { PartiallySplitLTOUnits = true; }
1589
1590 bool hasParamAccess() const { return HasParamAccess; }
1591
1592 bool isGlobalValueLive(const GlobalValueSummary *GVS) const {
1593 return !WithGlobalValueDeadStripping || GVS->isLive();
1594 }
1595 bool isGUIDLive(GlobalValue::GUID GUID) const;
1596
1597 /// Return a ValueInfo for the index value_type (convenient when iterating
1598 /// index).
1599 ValueInfo getValueInfo(const GlobalValueSummaryMapTy::value_type &R) const {
1600 return ValueInfo(HaveGVs, &R);
1601 }
1602
1603 /// Return a ValueInfo for GUID if it exists, otherwise return ValueInfo().
1605 auto I = GlobalValueMap.find(GUID);
1606 return ValueInfo(HaveGVs, I == GlobalValueMap.end() ? nullptr : &*I);
1607 }
1608
1609 /// Return a ValueInfo for \p GUID.
1611 return ValueInfo(HaveGVs, getOrInsertValuePtr(GUID));
1612 }
1613
1614 // Save a string in the Index. Use before passing Name to
1615 // getOrInsertValueInfo when the string isn't owned elsewhere (e.g. on the
1616 // module's Strtab).
1618
1619 /// Return a ValueInfo for \p GUID setting value \p Name.
1621 assert(!HaveGVs);
1622 auto VP = getOrInsertValuePtr(GUID);
1623 VP->second.U.Name = Name;
1624 return ValueInfo(HaveGVs, VP);
1625 }
1626
1627 /// Return a ValueInfo for \p GV and mark it as belonging to GV.
1629 assert(HaveGVs);
1630 auto VP = getOrInsertValuePtr(GV->getGUID());
1631 VP->second.U.GV = GV;
1632 return ValueInfo(HaveGVs, VP);
1633 }
1634
1635 /// Return the GUID for \p OriginalId in the OidGuidMap.
1637 const auto I = OidGuidMap.find(OriginalID);
1638 return I == OidGuidMap.end() ? 0 : I->second;
1639 }
1640
1641 std::set<std::string> &cfiFunctionDefs() { return CfiFunctionDefs; }
1642 const std::set<std::string> &cfiFunctionDefs() const { return CfiFunctionDefs; }
1643
1644 std::set<std::string> &cfiFunctionDecls() { return CfiFunctionDecls; }
1645 const std::set<std::string> &cfiFunctionDecls() const { return CfiFunctionDecls; }
1646
1647 /// Add a global value summary for a value.
1649 std::unique_ptr<GlobalValueSummary> Summary) {
1650 addGlobalValueSummary(getOrInsertValueInfo(&GV), std::move(Summary));
1651 }
1652
1653 /// Add a global value summary for a value of the given name.
1655 std::unique_ptr<GlobalValueSummary> Summary) {
1657 std::move(Summary));
1658 }
1659
1660 /// Add a global value summary for the given ValueInfo.
1662 std::unique_ptr<GlobalValueSummary> Summary) {
1663 if (const FunctionSummary *FS = dyn_cast<FunctionSummary>(Summary.get()))
1664 HasParamAccess |= !FS->paramAccesses().empty();
1665 addOriginalName(VI.getGUID(), Summary->getOriginalName());
1666 // Here we have a notionally const VI, but the value it points to is owned
1667 // by the non-const *this.
1668 const_cast<GlobalValueSummaryMapTy::value_type *>(VI.getRef())
1669 ->second.SummaryList.push_back(std::move(Summary));
1670 }
1671
1672 /// Add an original name for the value of the given GUID.
1674 GlobalValue::GUID OrigGUID) {
1675 if (OrigGUID == 0 || ValueGUID == OrigGUID)
1676 return;
1677 if (OidGuidMap.count(OrigGUID) && OidGuidMap[OrigGUID] != ValueGUID)
1678 OidGuidMap[OrigGUID] = 0;
1679 else
1680 OidGuidMap[OrigGUID] = ValueGUID;
1681 }
1682
1683 /// Find the summary for ValueInfo \p VI in module \p ModuleId, or nullptr if
1684 /// not found.
1686 auto SummaryList = VI.getSummaryList();
1687 auto Summary =
1688 llvm::find_if(SummaryList,
1689 [&](const std::unique_ptr<GlobalValueSummary> &Summary) {
1690 return Summary->modulePath() == ModuleId;
1691 });
1692 if (Summary == SummaryList.end())
1693 return nullptr;
1694 return Summary->get();
1695 }
1696
1697 /// Find the summary for global \p GUID in module \p ModuleId, or nullptr if
1698 /// not found.
1700 StringRef ModuleId) const {
1701 auto CalleeInfo = getValueInfo(ValueGUID);
1702 if (!CalleeInfo)
1703 return nullptr; // This function does not have a summary
1704 return findSummaryInModule(CalleeInfo, ModuleId);
1705 }
1706
1707 /// Returns the first GlobalValueSummary for \p GV, asserting that there
1708 /// is only one if \p PerModuleIndex.
1710 bool PerModuleIndex = true) const {
1711 assert(GV.hasName() && "Can't get GlobalValueSummary for GV with no name");
1712 return getGlobalValueSummary(GV.getGUID(), PerModuleIndex);
1713 }
1714
1715 /// Returns the first GlobalValueSummary for \p ValueGUID, asserting that
1716 /// there
1717 /// is only one if \p PerModuleIndex.
1719 bool PerModuleIndex = true) const;
1720
1721 /// Table of modules, containing module hash and id.
1723 return ModulePathStringTable;
1724 }
1725
1726 /// Table of modules, containing hash and id.
1727 StringMap<ModuleHash> &modulePaths() { return ModulePathStringTable; }
1728
1729 /// Get the module SHA1 hash recorded for the given module path.
1730 const ModuleHash &getModuleHash(const StringRef ModPath) const {
1731 auto It = ModulePathStringTable.find(ModPath);
1732 assert(It != ModulePathStringTable.end() && "Module not registered");
1733 return It->second;
1734 }
1735
1736 /// Convenience method for creating a promoted global name
1737 /// for the given value name of a local, and its original module's ID.
1738 static std::string getGlobalNameForLocal(StringRef Name, ModuleHash ModHash) {
1739 std::string Suffix = utostr((uint64_t(ModHash[0]) << 32) |
1740 ModHash[1]); // Take the first 64 bits
1741 return getGlobalNameForLocal(Name, Suffix);
1742 }
1743
1744 static std::string getGlobalNameForLocal(StringRef Name, StringRef Suffix) {
1745 SmallString<256> NewName(Name);
1746 NewName += ".llvm.";
1747 NewName += Suffix;
1748 return std::string(NewName);
1749 }
1750
1751 /// Helper to obtain the unpromoted name for a global value (or the original
1752 /// name if not promoted). Split off the rightmost ".llvm.${hash}" suffix,
1753 /// because it is possible in certain clients (not clang at the moment) for
1754 /// two rounds of ThinLTO optimization and therefore promotion to occur.
1756 std::pair<StringRef, StringRef> Pair = Name.rsplit(".llvm.");
1757 return Pair.first;
1758 }
1759
1761
1762 /// Add a new module with the given \p Hash, mapped to the given \p
1763 /// ModID, and return a reference to the module.
1765 return &*ModulePathStringTable.insert({ModPath, Hash}).first;
1766 }
1767
1768 /// Return module entry for module with the given \p ModPath.
1770 auto It = ModulePathStringTable.find(ModPath);
1771 assert(It != ModulePathStringTable.end() && "Module not registered");
1772 return &*It;
1773 }
1774
1775 /// Return module entry for module with the given \p ModPath.
1776 const ModuleInfo *getModule(StringRef ModPath) const {
1777 auto It = ModulePathStringTable.find(ModPath);
1778 assert(It != ModulePathStringTable.end() && "Module not registered");
1779 return &*It;
1780 }
1781
1782 /// Check if the given Module has any functions available for exporting
1783 /// in the index. We consider any module present in the ModulePathStringTable
1784 /// to have exported functions.
1785 bool hasExportedFunctions(const Module &M) const {
1786 return ModulePathStringTable.count(M.getModuleIdentifier());
1787 }
1788
1789 const TypeIdSummaryMapTy &typeIds() const { return TypeIdMap; }
1790
1791 /// Return an existing or new TypeIdSummary entry for \p TypeId.
1792 /// This accessor can mutate the map and therefore should not be used in
1793 /// the ThinLTO backends.
1795 auto TidIter = TypeIdMap.equal_range(GlobalValue::getGUID(TypeId));
1796 for (auto It = TidIter.first; It != TidIter.second; ++It)
1797 if (It->second.first == TypeId)
1798 return It->second.second;
1799 auto It = TypeIdMap.insert(
1800 {GlobalValue::getGUID(TypeId), {std::string(TypeId), TypeIdSummary()}});
1801 return It->second.second;
1802 }
1803
1804 /// This returns either a pointer to the type id summary (if present in the
1805 /// summary map) or null (if not present). This may be used when importing.
1807 auto TidIter = TypeIdMap.equal_range(GlobalValue::getGUID(TypeId));
1808 for (auto It = TidIter.first; It != TidIter.second; ++It)
1809 if (It->second.first == TypeId)
1810 return &It->second.second;
1811 return nullptr;
1812 }
1813
1815 return const_cast<TypeIdSummary *>(
1816 static_cast<const ModuleSummaryIndex *>(this)->getTypeIdSummary(
1817 TypeId));
1818 }
1819
1820 const auto &typeIdCompatibleVtableMap() const {
1821 return TypeIdCompatibleVtableMap;
1822 }
1823
1824 /// Return an existing or new TypeIdCompatibleVtableMap entry for \p TypeId.
1825 /// This accessor can mutate the map and therefore should not be used in
1826 /// the ThinLTO backends.
1829 return TypeIdCompatibleVtableMap[std::string(TypeId)];
1830 }
1831
1832 /// For the given \p TypeId, this returns the TypeIdCompatibleVtableMap
1833 /// entry if present in the summary map. This may be used when importing.
1834 std::optional<TypeIdCompatibleVtableInfo>
1836 auto I = TypeIdCompatibleVtableMap.find(TypeId);
1837 if (I == TypeIdCompatibleVtableMap.end())
1838 return std::nullopt;
1839 return I->second;
1840 }
1841
1842 /// Collect for the given module the list of functions it defines
1843 /// (GUID -> Summary).
1845 GVSummaryMapTy &GVSummaryMap) const;
1846
1847 /// Collect for each module the list of Summaries it defines (GUID ->
1848 /// Summary).
1849 template <class Map>
1850 void
1851 collectDefinedGVSummariesPerModule(Map &ModuleToDefinedGVSummaries) const {
1852 for (const auto &GlobalList : *this) {
1853 auto GUID = GlobalList.first;
1854 for (const auto &Summary : GlobalList.second.SummaryList) {
1855 ModuleToDefinedGVSummaries[Summary->modulePath()][GUID] = Summary.get();
1856 }
1857 }
1858 }
1859
1860 /// Print to an output stream.
1861 void print(raw_ostream &OS, bool IsForDebug = false) const;
1862
1863 /// Dump to stderr (for debugging).
1864 void dump() const;
1865
1866 /// Export summary to dot file for GraphViz.
1867 void
1869 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) const;
1870
1871 /// Print out strongly connected components for debugging.
1872 void dumpSCCs(raw_ostream &OS);
1873
1874 /// Do the access attribute and DSOLocal propagation in combined index.
1876
1877 /// Checks if we can import global variable from another module.
1878 bool canImportGlobalVar(const GlobalValueSummary *S, bool AnalyzeRefs) const;
1879};
1880
1881/// GraphTraits definition to build SCC for the index
1882template <> struct GraphTraits<ValueInfo> {
1885
1887 return P.first;
1888 }
1891 decltype(&valueInfoFromEdge)>;
1892
1893 using ChildEdgeIteratorType = std::vector<FunctionSummary::EdgeTy>::iterator;
1894
1895 static NodeRef getEntryNode(ValueInfo V) { return V; }
1896
1898 if (!N.getSummaryList().size()) // handle external function
1899 return ChildIteratorType(
1900 FunctionSummary::ExternalNode.CallGraphEdgeList.begin(),
1901 &valueInfoFromEdge);
1903 cast<FunctionSummary>(N.getSummaryList().front()->getBaseObject());
1904 return ChildIteratorType(F->CallGraphEdgeList.begin(), &valueInfoFromEdge);
1905 }
1906
1908 if (!N.getSummaryList().size()) // handle external function
1909 return ChildIteratorType(
1910 FunctionSummary::ExternalNode.CallGraphEdgeList.end(),
1911 &valueInfoFromEdge);
1913 cast<FunctionSummary>(N.getSummaryList().front()->getBaseObject());
1914 return ChildIteratorType(F->CallGraphEdgeList.end(), &valueInfoFromEdge);
1915 }
1916
1918 if (!N.getSummaryList().size()) // handle external function
1919 return FunctionSummary::ExternalNode.CallGraphEdgeList.begin();
1920
1922 cast<FunctionSummary>(N.getSummaryList().front()->getBaseObject());
1923 return F->CallGraphEdgeList.begin();
1924 }
1925
1927 if (!N.getSummaryList().size()) // handle external function
1928 return FunctionSummary::ExternalNode.CallGraphEdgeList.end();
1929
1931 cast<FunctionSummary>(N.getSummaryList().front()->getBaseObject());
1932 return F->CallGraphEdgeList.end();
1933 }
1934
1935 static NodeRef edge_dest(EdgeRef E) { return E.first; }
1936};
1937
1938template <>
1941 std::unique_ptr<GlobalValueSummary> Root =
1942 std::make_unique<FunctionSummary>(I->calculateCallGraphRoot());
1943 GlobalValueSummaryInfo G(I->haveGVs());
1944 G.SummaryList.push_back(std::move(Root));
1945 static auto P =
1946 GlobalValueSummaryMapTy::value_type(GlobalValue::GUID(0), std::move(G));
1947 return ValueInfo(I->haveGVs(), &P);
1948 }
1949};
1950} // end namespace llvm
1951
1952#endif // LLVM_IR_MODULESUMMARYINDEX_H
for(const MachineOperand &MO :llvm::drop_begin(OldMI.operands(), Desc.getNumOperands()))
This file defines the StringMap class.
This file defines the BumpPtrAllocator interface.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Looks at all the uses of the given value Returns the Liveness deduced from the uses of this value Adds all uses that cause the result to be MaybeLive to MaybeLiveRetUses If the result is Live
This file defines the DenseMap class.
std::string Name
static const char * PreservedSymbols[]
Definition: IRSymtab.cpp:48
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
#define G(x, y, z)
Definition: MD5.cpp:56
AllocType
Module.h This file contains the declarations for the Module class.
#define P(N)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
raw_pwrite_stream & OS
This file defines the SmallString class.
This file defines the SmallVector class.
This file contains some functions that are useful when dealing with strings.
ScaledNumber< uint64_t > Scaled64
Value * RHS
Alias summary information.
GlobalValue::GUID getAliaseeGUID() const
const GlobalValueSummary & getAliasee() const
ValueInfo getAliaseeVI() const
static bool classof(const GlobalValueSummary *GVS)
Check if this is an alias summary.
AliasSummary(GVFlags Flags)
GlobalValueSummary & getAliasee()
void setAliasee(ValueInfo &AliaseeVI, GlobalValueSummary *Aliasee)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:66
This class represents a range of values.
Definition: ConstantRange.h:47
This is an important base class in LLVM.
Definition: Constant.h:41
unsigned size() const
Definition: DenseMap.h:99
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:220
Implements a dense probed hash-table based set.
Definition: DenseSet.h:271
Function summary information to aid decisions and implementation of importing.
static FunctionSummary ExternalNode
A dummy node to reference external functions that aren't in the index.
void addCallsite(CallsiteInfo &Callsite)
FunctionSummary(GVFlags Flags, unsigned NumInsts, FFlags FunFlags, uint64_t EntryCount, std::vector< ValueInfo > Refs, std::vector< EdgeTy > CGEdges, std::vector< GlobalValue::GUID > TypeTests, std::vector< VFuncId > TypeTestAssumeVCalls, std::vector< VFuncId > TypeCheckedLoadVCalls, std::vector< ConstVCall > TypeTestAssumeConstVCalls, std::vector< ConstVCall > TypeCheckedLoadConstVCalls, std::vector< ParamAccess > Params, CallsitesTy CallsiteList, AllocsTy AllocList)
ArrayRef< VFuncId > type_test_assume_vcalls() const
Returns the list of virtual calls made by this function using llvm.assume(llvm.type....
ArrayRef< ConstVCall > type_test_assume_const_vcalls() const
Returns the list of virtual calls made by this function using llvm.assume(llvm.type....
static FunctionSummary makeDummyFunctionSummary(std::vector< FunctionSummary::EdgeTy > Edges)
Create an empty FunctionSummary (with specified call edges).
std::pair< ValueInfo, CalleeInfo > EdgeTy
<CalleeValueInfo, CalleeInfo> call edge pair.
std::pair< unsigned, unsigned > specialRefCounts() const
void setEntryCount(uint64_t EC)
Set the synthetic entry count for this function.
ArrayRef< AllocInfo > allocs() const
ArrayRef< CallsiteInfo > callsites() const
void addTypeTest(GlobalValue::GUID Guid)
Add a type test to the summary.
uint64_t entryCount() const
Get the synthetic entry count for this function.
std::vector< EdgeTy > & mutableCalls()
ArrayRef< VFuncId > type_checked_load_vcalls() const
Returns the list of virtual calls made by this function using llvm.type.checked.load intrinsics that ...
void setParamAccesses(std::vector< ParamAccess > NewParams)
Sets the list of known uses of pointer parameters.
unsigned instCount() const
Get the instruction count recorded for this function.
const TypeIdInfo * getTypeIdInfo() const
ArrayRef< ConstVCall > type_checked_load_const_vcalls() const
Returns the list of virtual calls made by this function using llvm.type.checked.load intrinsics with ...
ArrayRef< EdgeTy > calls() const
Return the list of <CalleeValueInfo, CalleeInfo> pairs.
ArrayRef< ParamAccess > paramAccesses() const
Returns the list of known uses of pointer parameters.
CallsitesTy & mutableCallsites()
ForceSummaryHotnessType
Types for -force-summary-edges-cold debugging option.
FFlags fflags() const
Get function summary flags.
ArrayRef< GlobalValue::GUID > type_tests() const
Returns the list of type identifiers used by this function in llvm.type.test intrinsics other than by...
static bool classof(const GlobalValueSummary *GVS)
Check if this is a function summary.
Function and variable summary information to aid decisions and implementation of importing.
SummaryKind
Sububclass discriminator (for dyn_cast<> et al.)
GVFlags flags() const
Get the flags for this GlobalValue (see struct GVFlags).
StringRef modulePath() const
Get the path to the module containing this function.
GlobalValueSummary * getBaseObject()
If this is an alias summary, returns the summary of the aliased object (a global variable or function...
SummaryKind getSummaryKind() const
Which kind of summary subclass this is.
GlobalValue::GUID getOriginalName() const
Returns the hash of the original name, it is identical to the GUID for externally visible symbols,...
GlobalValue::VisibilityTypes getVisibility() const
ArrayRef< ValueInfo > refs() const
Return the list of values referenced by this global value definition.
GlobalValueSummary(SummaryKind K, GVFlags Flags, std::vector< ValueInfo > Refs)
void setLinkage(GlobalValue::LinkageTypes Linkage)
Sets the linkage to the value determined by global summary-based optimization.
void setVisibility(GlobalValue::VisibilityTypes Vis)
virtual ~GlobalValueSummary()=default
GlobalValueSummary::ImportKind importType() const
void setModulePath(StringRef ModPath)
Set the path to the module containing this function, for use in the combined index.
void setNotEligibleToImport()
Flag that this global value cannot be imported.
void setCanAutoHide(bool CanAutoHide)
GlobalValue::LinkageTypes linkage() const
Return linkage type recorded for this global value.
bool notEligibleToImport() const
Return true if this global value can't be imported.
void setImportKind(ImportKind IK)
void setOriginalName(GlobalValue::GUID Name)
Initialize the original name hash in this summary.
static GUID getGUID(StringRef GlobalName)
Return a 64-bit global unique ID constructed from global value name (i.e.
Definition: Globals.cpp:75
GUID getGUID() const
Return a 64-bit global unique ID constructed from global value name (i.e.
Definition: GlobalValue.h:595
VisibilityTypes
An enumeration for the kinds of visibility of global values.
Definition: GlobalValue.h:66
@ DefaultVisibility
The GV is visible.
Definition: GlobalValue.h:67
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition: GlobalValue.h:51
@ AvailableExternallyLinkage
Available for inspection, not emission.
Definition: GlobalValue.h:53
Global variable summary information to aid decisions and implementation of importing.
void setVCallVisibility(GlobalObject::VCallVisibility Vis)
struct llvm::GlobalVarSummary::GVarFlags VarFlags
GVarFlags varflags() const
ArrayRef< VirtFuncOffset > vTableFuncs() const
GlobalObject::VCallVisibility getVCallVisibility() const
static bool classof(const GlobalValueSummary *GVS)
Check if this is a global variable summary.
GlobalVarSummary(GVFlags Flags, GVarFlags VarFlags, std::vector< ValueInfo > Refs)
void setVTableFuncs(VTableFuncList Funcs)
Class to hold module path string table and global value map, and encapsulate methods for operating on...
std::set< std::string > & cfiFunctionDecls()
const std::set< std::string > & cfiFunctionDecls() const
TypeIdSummary & getOrInsertTypeIdSummary(StringRef TypeId)
Return an existing or new TypeIdSummary entry for TypeId.
std::optional< TypeIdCompatibleVtableInfo > getTypeIdCompatibleVtableSummary(StringRef TypeId) const
For the given TypeId, this returns the TypeIdCompatibleVtableMap entry if present in the summary map.
gvsummary_iterator end()
void addGlobalValueSummary(ValueInfo VI, std::unique_ptr< GlobalValueSummary > Summary)
Add a global value summary for the given ValueInfo.
ModulePathStringTableTy::value_type ModuleInfo
ValueInfo getOrInsertValueInfo(GlobalValue::GUID GUID)
Return a ValueInfo for GUID.
bool withGlobalValueDeadStripping() const
const std::set< std::string > & cfiFunctionDefs() const
static void discoverNodes(ValueInfo V, std::map< ValueInfo, bool > &FunctionHasParent)
Convenience function for doing a DFS on a ValueInfo.
StringRef saveString(StringRef String)
const TypeIdSummaryMapTy & typeIds() const
static StringRef getOriginalNameBeforePromote(StringRef Name)
Helper to obtain the unpromoted name for a global value (or the original name if not promoted).
const TypeIdSummary * getTypeIdSummary(StringRef TypeId) const
This returns either a pointer to the type id summary (if present in the summary map) or null (if not ...
bool isGUIDLive(GlobalValue::GUID GUID) const
gvsummary_iterator begin()
const_gvsummary_iterator end() const
bool isReadOnly(const GlobalVarSummary *GVS) const
void setFlags(uint64_t Flags)
const_gvsummary_iterator begin() const
bool isWriteOnly(const GlobalVarSummary *GVS) const
const std::vector< uint64_t > & stackIds() const
GlobalValueSummary * findSummaryInModule(GlobalValue::GUID ValueGUID, StringRef ModuleId) const
Find the summary for global GUID in module ModuleId, or nullptr if not found.
ValueInfo getValueInfo(const GlobalValueSummaryMapTy::value_type &R) const
Return a ValueInfo for the index value_type (convenient when iterating index).
const ModuleHash & getModuleHash(const StringRef ModPath) const
Get the module SHA1 hash recorded for the given module path.
static constexpr const char * getRegularLTOModuleName()
void addGlobalValueSummary(StringRef ValueName, std::unique_ptr< GlobalValueSummary > Summary)
Add a global value summary for a value of the given name.
ModuleSummaryIndex(bool HaveGVs, bool EnableSplitLTOUnit=false, bool UnifiedLTO=false)
void collectDefinedFunctionsForModule(StringRef ModulePath, GVSummaryMapTy &GVSummaryMap) const
Collect for the given module the list of functions it defines (GUID -> Summary).
const auto & typeIdCompatibleVtableMap() const
void dumpSCCs(raw_ostream &OS)
Print out strongly connected components for debugging.
bool isGlobalValueLive(const GlobalValueSummary *GVS) const
const ModuleInfo * getModule(StringRef ModPath) const
Return module entry for module with the given ModPath.
void propagateAttributes(const DenseSet< GlobalValue::GUID > &PreservedSymbols)
Do the access attribute and DSOLocal propagation in combined index.
const StringMap< ModuleHash > & modulePaths() const
Table of modules, containing module hash and id.
void dump() const
Dump to stderr (for debugging).
Definition: AsmWriter.cpp:5317
ModuleInfo * addModule(StringRef ModPath, ModuleHash Hash=ModuleHash{{0}})
Add a new module with the given Hash, mapped to the given ModID, and return a reference to the module...
void collectDefinedGVSummariesPerModule(Map &ModuleToDefinedGVSummaries) const
Collect for each module the list of Summaries it defines (GUID -> Summary).
void addGlobalValueSummary(const GlobalValue &GV, std::unique_ptr< GlobalValueSummary > Summary)
Add a global value summary for a value.
bool hasExportedFunctions(const Module &M) const
Check if the given Module has any functions available for exporting in the index.
static std::string getGlobalNameForLocal(StringRef Name, ModuleHash ModHash)
Convenience method for creating a promoted global name for the given value name of a local,...
static constexpr uint64_t BitcodeSummaryVersion
void exportToDot(raw_ostream &OS, const DenseSet< GlobalValue::GUID > &GUIDPreservedSymbols) const
Export summary to dot file for GraphViz.
uint64_t getStackIdAtIndex(unsigned Index) const
StringMap< ModuleHash > & modulePaths()
Table of modules, containing hash and id.
void print(raw_ostream &OS, bool IsForDebug=false) const
Print to an output stream.
Definition: AsmWriter.cpp:5244
bool skipModuleByDistributedBackend() const
ValueInfo getOrInsertValueInfo(const GlobalValue *GV)
Return a ValueInfo for GV and mark it as belonging to GV.
GlobalValueSummary * findSummaryInModule(ValueInfo VI, StringRef ModuleId) const
Find the summary for ValueInfo VI in module ModuleId, or nullptr if not found.
ValueInfo getValueInfo(GlobalValue::GUID GUID) const
Return a ValueInfo for GUID if it exists, otherwise return ValueInfo().
unsigned addOrGetStackIdIndex(uint64_t StackId)
GlobalValue::GUID getGUIDFromOriginalID(GlobalValue::GUID OriginalID) const
Return the GUID for OriginalId in the OidGuidMap.
GlobalValueSummary * getGlobalValueSummary(const GlobalValue &GV, bool PerModuleIndex=true) const
Returns the first GlobalValueSummary for GV, asserting that there is only one if PerModuleIndex.
std::set< std::string > & cfiFunctionDefs()
ModuleInfo * getModule(StringRef ModPath)
Return module entry for module with the given ModPath.
ValueInfo getOrInsertValueInfo(GlobalValue::GUID GUID, StringRef Name)
Return a ValueInfo for GUID setting value Name.
bool canImportGlobalVar(const GlobalValueSummary *S, bool AnalyzeRefs) const
Checks if we can import global variable from another module.
static std::string getGlobalNameForLocal(StringRef Name, StringRef Suffix)
void addOriginalName(GlobalValue::GUID ValueGUID, GlobalValue::GUID OrigGUID)
Add an original name for the value of the given GUID.
FunctionSummary calculateCallGraphRoot()
TypeIdSummary * getTypeIdSummary(StringRef TypeId)
TypeIdCompatibleVtableInfo & getOrInsertTypeIdCompatibleVtableSummary(StringRef TypeId)
Return an existing or new TypeIdCompatibleVtableMap entry for TypeId.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
PointerIntPair - This class implements a pair of a pointer and small integer.
void setPointer(PointerTy PtrVal) &
IntType getInt() const
void setInt(IntType IntVal) &
PointerTy getPointer() const
Simple representation of a scaled number.
Definition: ScaledNumber.h:493
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:479
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringMapEntry - This is used to represent one value that is inserted into a StringMap.
iterator end()
Definition: StringMap.h:220
iterator find(StringRef Key)
Definition: StringMap.h:233
size_type count(StringRef Key) const
count - Return 1 if the element is in the map, 0 otherwise.
Definition: StringMap.h:276
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition: StringMap.h:308
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Saves strings in the provided stable storage and returns a StringRef with a stable character pointer.
Definition: StringSaver.h:21
StringRef save(const char *S)
Definition: StringSaver.h:30
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
bool hasName() const
Definition: Value.h:261
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:661
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ Cold
Attempts to make code in the caller as efficient as possible under the assumption that the call is no...
Definition: CallingConv.h:47
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
GlobalValueSummaryMapTy::iterator gvsummary_iterator
@ Offset
Definition: DWP.cpp:480
bool operator<(int64_t V1, const APSInt &V2)
Definition: APSInt.h:361
const char * getHotnessName(CalleeInfo::HotnessType HT)
bool operator!=(uint64_t V1, const APInt &V2)
Definition: APInt.h:2058
bool operator==(const AddressRangeValuePair &LHS, const AddressRangeValuePair &RHS)
std::vector< VirtFuncOffset > VTableFuncList
List of functions referenced by a particular vtable definition.
std::vector< std::unique_ptr< GlobalValueSummary > > GlobalValueSummaryList
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
GlobalValueSummaryMapTy::const_iterator const_gvsummary_iterator
Type used for iterating through the global value summary map.
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
Definition: APFixedPoint.h:293
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1849
std::multimap< GlobalValue::GUID, std::pair< std::string, TypeIdSummary > > TypeIdSummaryMapTy
Map of a type GUID to type id string and summary (multimap used in case of GUID conflicts).
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1749
std::array< uint32_t, 5 > ModuleHash
160 bits SHA1
std::map< GlobalValue::GUID, GlobalValueSummaryInfo > GlobalValueSummaryMapTy
Map from global value GUID to corresponding summary structures.
std::vector< TypeIdOffsetVtableInfo > TypeIdCompatibleVtableInfo
List of vtable definitions decorated by a particular type identifier, and their corresponding offsets...
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
#define N
Summary of memprof metadata on allocations.
AllocInfo(std::vector< MIBInfo > MIBs)
AllocInfo(SmallVector< uint8_t > Versions, std::vector< MIBInfo > MIBs)
SmallVector< uint8_t > Versions
std::vector< MIBInfo > MIBs
Class to accumulate and hold information about a callee.
static constexpr uint64_t MaxRelBlockFreq
bool hasTailCall() const
void updateHotness(const HotnessType OtherHotness)
CalleeInfo(HotnessType Hotness, bool HasTC, uint64_t RelBF)
HotnessType getHotness() const
static constexpr int32_t ScaleShift
void setHasTailCall(const bool HasTC)
void updateRelBlockFreq(uint64_t BlockFreq, uint64_t EntryFreq)
Update RelBlockFreq from BlockFreq and EntryFreq.
static constexpr unsigned RelBlockFreqBits
The value stored in RelBlockFreq has to be interpreted as the digits of a scaled number with a scale ...
Summary of memprof callsite metadata.
SmallVector< unsigned > StackIdIndices
SmallVector< unsigned > Clones
CallsiteInfo(ValueInfo Callee, SmallVector< unsigned > StackIdIndices)
CallsiteInfo(ValueInfo Callee, SmallVector< unsigned > Clones, SmallVector< unsigned > StackIdIndices)
static FunctionSummary::ConstVCall getEmptyKey()
static FunctionSummary::ConstVCall getTombstoneKey()
static unsigned getHashValue(FunctionSummary::ConstVCall I)
static bool isEqual(FunctionSummary::ConstVCall L, FunctionSummary::ConstVCall R)
static FunctionSummary::VFuncId getEmptyKey()
static bool isEqual(FunctionSummary::VFuncId L, FunctionSummary::VFuncId R)
static FunctionSummary::VFuncId getTombstoneKey()
static unsigned getHashValue(FunctionSummary::VFuncId I)
static bool isEqual(ValueInfo L, ValueInfo R)
static bool isSpecialKey(ValueInfo V)
static unsigned getHashValue(ValueInfo I)
An information struct used to provide DenseMap with the various necessary components for a given valu...
Definition: DenseMapInfo.h:52
A specification for a virtual function call with all constant integer arguments.
Flags specific to function summaries.
FFlags & operator&=(const FFlags &RHS)
Describes the use of a value in a call instruction, specifying the call's target, the value's paramet...
Call(uint64_t ParamNo, ValueInfo Callee, const ConstantRange &Offsets)
Describes the uses of a parameter by the function.
ParamAccess(uint64_t ParamNo, const ConstantRange &Use)
std::vector< Call > Calls
In the per-module summary, it summarizes the byte offset applied to each pointer parameter before pas...
static constexpr uint32_t RangeWidth
All type identifier related information.
std::vector< ConstVCall > TypeCheckedLoadConstVCalls
std::vector< VFuncId > TypeCheckedLoadVCalls
std::vector< ConstVCall > TypeTestAssumeConstVCalls
List of virtual calls made by this function using (respectively) llvm.assume(llvm....
std::vector< GlobalValue::GUID > TypeTests
List of type identifiers used by this function in llvm.type.test intrinsics referenced by something o...
std::vector< VFuncId > TypeTestAssumeVCalls
List of virtual calls made by this function using (respectively) llvm.assume(llvm....
An "identifier" for a virtual function.
GlobalValueSummaryList SummaryList
List of global value summary structures for a particular value held in the GlobalValueMap.
union llvm::GlobalValueSummaryInfo::NameOrGV U
Group flags (Linkage, NotEligibleToImport, etc.) as a bitfield.
GVFlags(GlobalValue::LinkageTypes Linkage, GlobalValue::VisibilityTypes Visibility, bool NotEligibleToImport, bool Live, bool IsLocal, bool CanAutoHide, ImportKind ImportType)
Convenience Constructors.
unsigned DSOLocal
Indicates that the linker resolved the symbol to a definition from within the same linkage unit.
unsigned CanAutoHide
In the per-module summary, indicates that the global value is linkonce_odr and global unnamed addr (s...
unsigned ImportType
This field is written by the ThinLTO indexing step to postlink combined summary.
unsigned NotEligibleToImport
Indicate if the global value cannot be imported (e.g.
unsigned Linkage
The linkage type of the associated global value.
unsigned Visibility
Indicates the visibility.
unsigned Live
In per-module summary, indicate that the global value must be considered a live root for index-based ...
GVarFlags(bool ReadOnly, bool WriteOnly, bool Constant, GlobalObject::VCallVisibility Vis)
static NodeRef getEntryNode(ModuleSummaryIndex *I)
static NodeRef valueInfoFromEdge(FunctionSummary::EdgeTy &P)
static ChildIteratorType child_begin(NodeRef N)
static ChildEdgeIteratorType child_edge_begin(NodeRef N)
static NodeRef edge_dest(EdgeRef E)
std::vector< FunctionSummary::EdgeTy >::iterator ChildEdgeIteratorType
static NodeRef getEntryNode(ValueInfo V)
static ChildIteratorType child_end(NodeRef N)
static ChildEdgeIteratorType child_edge_end(NodeRef N)
FunctionSummary::EdgeTy & EdgeRef
Summary of a single MIB in a memprof metadata on allocations.
MIBInfo(AllocationType AllocType, SmallVector< unsigned > StackIdIndices)
AllocationType AllocType
SmallVector< unsigned > StackIdIndices
The following data structures summarize type metadata information.
TypeIdOffsetVtableInfo(uint64_t Offset, ValueInfo VI)
std::map< uint64_t, WholeProgramDevirtResolution > WPDRes
Mapping from byte offset to whole-program devirt resolution for that (typeid, byte offset) pair.
TypeTestResolution TTRes
Kind
Specifies which kind of type check we should emit for this byte array.
@ Unknown
Unknown (analysis not performed, don't lower)
@ Single
Single element (last example in "Short Inline Bit Vectors")
@ Inline
Inlined bit vector ("Short Inline Bit Vectors")
@ Unsat
Unsatisfiable type (i.e. no global has this type metadata)
@ AllOnes
All-ones bit vector ("Eliminating Bit Vector Checks for All-Ones Bit Vectors")
@ ByteArray
Test a byte array (first example)
unsigned SizeM1BitWidth
Range of size-1 expressed as a bit width.
enum llvm::TypeTestResolution::Kind TheKind
Struct that holds a reference to a particular GUID in a global value summary.
PointerIntPair< const GlobalValueSummaryMapTy::value_type *, 3, int > RefAndFlags
GlobalValue::VisibilityTypes getELFVisibility() const
Returns the most constraining visibility among summaries.
bool isValidAccessSpecifier() const
const GlobalValueSummaryMapTy::value_type * getRef() const
ArrayRef< std::unique_ptr< GlobalValueSummary > > getSummaryList() const
StringRef name() const
bool isWriteOnly() const
const GlobalValue * getValue() const
ValueInfo(bool HaveGVs, const GlobalValueSummaryMapTy::value_type *R)
bool isReadOnly() const
bool canAutoHide() const
Checks if all copies are eligible for auto-hiding (have flag set).
unsigned getAccessSpecifier() const
ValueInfo()=default
bool isDSOLocal(bool WithDSOLocalPropagation=false) const
Checks if all summaries are DSO local (have the flag set).
GlobalValue::GUID getGUID() const
bool haveGVs() const
The ValueInfo and offset for a function within a vtable definition initializer array.
VirtFuncOffset(ValueInfo VI, uint64_t Offset)
@ UniformRetVal
Uniform return value optimization.
@ VirtualConstProp
Virtual constant propagation.
@ UniqueRetVal
Unique return value optimization.
@ Indir
Just do a regular virtual call.
uint64_t Info
Additional information for the resolution:
enum llvm::WholeProgramDevirtResolution::ByArg::Kind TheKind
enum llvm::WholeProgramDevirtResolution::Kind TheKind
std::map< std::vector< uint64_t >, ByArg > ResByArg
Resolutions for calls with all constant integer arguments (excluding the first argument,...
@ SingleImpl
Single implementation devirtualization.
@ Indir
Just do a regular virtual call.
@ BranchFunnel
When retpoline mitigation is enabled, use a branch funnel that is defined in the merged module.
const GlobalValue * GV
The GlobalValue corresponding to this summary.
StringRef Name
Summary string representation.