@@ -45,58 +45,54 @@ struct CalleeInfo {
45
45
}
46
46
};
47
47
48
- // / Struct to hold value either by GUID or GlobalValue*. Values in combined
49
- // / indexes as well as indirect calls are GUIDs, all others are GlobalValues.
50
- struct ValueInfo {
51
- // / The value representation used in this instance.
52
- enum ValueInfoKind {
53
- VI_GUID,
54
- VI_Value,
55
- };
48
+ class GlobalValueSummary ;
56
49
57
- // / Union of the two possible value types.
58
- union ValueUnion {
59
- GlobalValue::GUID Id;
60
- const GlobalValue *GV;
61
- ValueUnion (GlobalValue::GUID Id) : Id (Id) {}
62
- ValueUnion (const GlobalValue *GV) : GV (GV) {}
63
- };
50
+ typedef std::vector<std::unique_ptr<GlobalValueSummary>> GlobalValueSummaryList;
64
51
65
- // / The value being represented.
66
- ValueUnion TheValue;
67
- // / The value representation.
68
- ValueInfoKind Kind;
69
- // / Constructor for a GUID value
70
- ValueInfo (GlobalValue::GUID Id = 0 ) : TheValue(Id), Kind(VI_GUID) {}
71
- // / Constructor for a GlobalValue* value
72
- ValueInfo (const GlobalValue *V) : TheValue(V), Kind(VI_Value) {}
73
- // / Accessor for GUID value
74
- GlobalValue::GUID getGUID () const {
75
- assert (Kind == VI_GUID && " Not a GUID type" );
76
- return TheValue.Id ;
77
- }
78
- // / Accessor for GlobalValue* value
79
- const GlobalValue *getValue () const {
80
- assert (Kind == VI_Value && " Not a Value type" );
81
- return TheValue.GV ;
82
- }
83
- bool isGUID () const { return Kind == VI_GUID; }
52
+ struct GlobalValueSummaryInfo {
53
+ // / The GlobalValue corresponding to this summary. This is only used in
54
+ // / per-module summaries.
55
+ const GlobalValue *GV = nullptr ;
56
+
57
+ // / List of global value summary structures for a particular value held
58
+ // / in the GlobalValueMap. Requires a vector in the case of multiple
59
+ // / COMDAT values of the same name.
60
+ GlobalValueSummaryList SummaryList;
84
61
};
85
62
86
- template <> struct DenseMapInfo <ValueInfo> {
87
- static inline ValueInfo getEmptyKey () { return ValueInfo ((GlobalValue *)-1 ); }
88
- static inline ValueInfo getTombstoneKey () {
89
- return ValueInfo ((GlobalValue *)-2 );
63
+ // / Map from global value GUID to corresponding summary structures. Use a
64
+ // / std::map rather than a DenseMap so that pointers to the map's value_type
65
+ // / (which are used by ValueInfo) are not invalidated by insertion. Also it will
66
+ // / likely incur less overhead, as the value type is not very small and the size
67
+ // / of the map is unknown, resulting in inefficiencies due to repeated
68
+ // / insertions and resizing.
69
+ typedef std::map<GlobalValue::GUID, GlobalValueSummaryInfo>
70
+ GlobalValueSummaryMapTy;
71
+
72
+ // / Struct that holds a reference to a particular GUID in a global value
73
+ // / summary.
74
+ struct ValueInfo {
75
+ const GlobalValueSummaryMapTy::value_type *Ref = nullptr ;
76
+ ValueInfo () = default ;
77
+ ValueInfo (const GlobalValueSummaryMapTy::value_type *Ref) : Ref(Ref) {}
78
+ operator bool () const { return Ref; }
79
+
80
+ GlobalValue::GUID getGUID () const { return Ref->first ; }
81
+ const GlobalValue *getValue () const { return Ref->second .GV ; }
82
+ ArrayRef<std::unique_ptr<GlobalValueSummary>> getSummaryList () const {
83
+ return Ref->second .SummaryList ;
90
84
}
91
- static bool isEqual (ValueInfo L, ValueInfo R) {
92
- if (L. isGUID () != R. isGUID ())
93
- return false ;
94
- return L. isGUID () ? (L. getGUID () == R. getGUID ())
95
- : (L. getValue () == R. getValue () );
85
+ };
86
+
87
+ template <> struct DenseMapInfo <ValueInfo> {
88
+ static inline ValueInfo getEmptyKey () {
89
+ return ValueInfo ((GlobalValueSummaryMapTy::value_type *)- 1 );
96
90
}
97
- static unsigned getHashValue ( ValueInfo I ) {
98
- return I. isGUID () ? I. getGUID () : ( uintptr_t )I. getValue ( );
91
+ static inline ValueInfo getTombstoneKey ( ) {
92
+ return ValueInfo ((GlobalValueSummaryMapTy::value_type *)- 2 );
99
93
}
94
+ static bool isEqual (ValueInfo L, ValueInfo R) { return L.Ref == R.Ref ; }
95
+ static unsigned getHashValue (ValueInfo I) { return (uintptr_t )I.Ref ; }
100
96
};
101
97
102
98
// / \brief Function and variable summary information to aid decisions and
@@ -483,19 +479,6 @@ struct TypeIdSummary {
483
479
// / 160 bits SHA1
484
480
typedef std::array<uint32_t , 5 > ModuleHash;
485
481
486
- // / List of global value summary structures for a particular value held
487
- // / in the GlobalValueMap. Requires a vector in the case of multiple
488
- // / COMDAT values of the same name.
489
- typedef std::vector<std::unique_ptr<GlobalValueSummary>> GlobalValueSummaryList;
490
-
491
- // / Map from global value GUID to corresponding summary structures.
492
- // / Use a std::map rather than a DenseMap since it will likely incur
493
- // / less overhead, as the value type is not very small and the size
494
- // / of the map is unknown, resulting in inefficiencies due to repeated
495
- // / insertions and resizing.
496
- typedef std::map<GlobalValue::GUID, GlobalValueSummaryList>
497
- GlobalValueSummaryMapTy;
498
-
499
482
// / Type used for iterating through the global value summary map.
500
483
typedef GlobalValueSummaryMapTy::const_iterator const_gvsummary_iterator;
501
484
typedef GlobalValueSummaryMapTy::iterator gvsummary_iterator;
@@ -532,28 +515,34 @@ class ModuleSummaryIndex {
532
515
// YAML I/O support.
533
516
friend yaml::MappingTraits<ModuleSummaryIndex>;
534
517
518
+ GlobalValueSummaryMapTy::value_type *
519
+ getOrInsertValuePtr (GlobalValue::GUID GUID) {
520
+ return &*GlobalValueMap.emplace (GUID, GlobalValueSummaryInfo{}).first ;
521
+ }
522
+
535
523
public:
536
524
gvsummary_iterator begin () { return GlobalValueMap.begin (); }
537
525
const_gvsummary_iterator begin () const { return GlobalValueMap.begin (); }
538
526
gvsummary_iterator end () { return GlobalValueMap.end (); }
539
527
const_gvsummary_iterator end () const { return GlobalValueMap.end (); }
540
528
size_t size () const { return GlobalValueMap.size (); }
541
529
542
- // / Get the list of global value summary objects for a given value name.
543
- const GlobalValueSummaryList &getGlobalValueSummaryList (StringRef ValueName) {
544
- return GlobalValueMap[GlobalValue::getGUID (ValueName)];
530
+ // / Return a ValueInfo for GUID if it exists, otherwise return ValueInfo().
531
+ ValueInfo getValueInfo (GlobalValue::GUID GUID) const {
532
+ auto I = GlobalValueMap.find (GUID);
533
+ return ValueInfo (I == GlobalValueMap.end () ? nullptr : &*I);
545
534
}
546
535
547
- // / Get the list of global value summary objects for a given value name.
548
- const const_gvsummary_iterator
549
- findGlobalValueSummaryList (StringRef ValueName) const {
550
- return GlobalValueMap.find (GlobalValue::getGUID (ValueName));
536
+ // / Return a ValueInfo for \p GUID.
537
+ ValueInfo getOrInsertValueInfo (GlobalValue::GUID GUID) {
538
+ return ValueInfo (getOrInsertValuePtr (GUID));
551
539
}
552
540
553
- // / Get the list of global value summary objects for a given value GUID.
554
- const const_gvsummary_iterator
555
- findGlobalValueSummaryList (GlobalValue::GUID ValueGUID) const {
556
- return GlobalValueMap.find (ValueGUID);
541
+ // / Return a ValueInfo for \p GV and mark it as belonging to GV.
542
+ ValueInfo getOrInsertValueInfo (const GlobalValue *GV) {
543
+ auto VP = getOrInsertValuePtr (GV->getGUID ());
544
+ VP->second .GV = GV;
545
+ return ValueInfo (VP);
557
546
}
558
547
559
548
// / Return the GUID for \p OriginalId in the OidGuidMap.
@@ -565,17 +554,18 @@ class ModuleSummaryIndex {
565
554
// / Add a global value summary for a value of the given name.
566
555
void addGlobalValueSummary (StringRef ValueName,
567
556
std::unique_ptr<GlobalValueSummary> Summary) {
568
- addOriginalName (GlobalValue::getGUID (ValueName),
569
- Summary->getOriginalName ());
570
- GlobalValueMap[GlobalValue::getGUID (ValueName)].push_back (
571
- std::move (Summary));
557
+ addGlobalValueSummary (getOrInsertValueInfo (GlobalValue::getGUID (ValueName)),
558
+ std::move (Summary));
572
559
}
573
560
574
- // / Add a global value summary for a value of the given GUID .
575
- void addGlobalValueSummary (GlobalValue::GUID ValueGUID ,
561
+ // / Add a global value summary for the given ValueInfo .
562
+ void addGlobalValueSummary (ValueInfo VI ,
576
563
std::unique_ptr<GlobalValueSummary> Summary) {
577
- addOriginalName (ValueGUID, Summary->getOriginalName ());
578
- GlobalValueMap[ValueGUID].push_back (std::move (Summary));
564
+ addOriginalName (VI.getGUID (), Summary->getOriginalName ());
565
+ // Here we have a notionally const VI, but the value it points to is owned
566
+ // by the non-const *this.
567
+ const_cast <GlobalValueSummaryMapTy::value_type *>(VI.Ref )
568
+ ->second .SummaryList .push_back (std::move (Summary));
579
569
}
580
570
581
571
// / Add an original name for the value of the given GUID.
@@ -593,16 +583,16 @@ class ModuleSummaryIndex {
593
583
// / not found.
594
584
GlobalValueSummary *findSummaryInModule (GlobalValue::GUID ValueGUID,
595
585
StringRef ModuleId) const {
596
- auto CalleeInfoList = findGlobalValueSummaryList (ValueGUID);
597
- if (CalleeInfoList == end () ) {
586
+ auto CalleeInfo = getValueInfo (ValueGUID);
587
+ if (!CalleeInfo ) {
598
588
return nullptr ; // This function does not have a summary
599
589
}
600
590
auto Summary =
601
- llvm::find_if (CalleeInfoList-> second ,
591
+ llvm::find_if (CalleeInfo. getSummaryList () ,
602
592
[&](const std::unique_ptr<GlobalValueSummary> &Summary) {
603
593
return Summary->modulePath () == ModuleId;
604
594
});
605
- if (Summary == CalleeInfoList-> second .end ())
595
+ if (Summary == CalleeInfo. getSummaryList () .end ())
606
596
return nullptr ;
607
597
return Summary->get ();
608
598
}
0 commit comments