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