]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/IR/ModuleSummaryIndex.h
Merge ^/head r314420 through r314481.
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / IR / ModuleSummaryIndex.h
1 //===-- llvm/ModuleSummaryIndex.h - Module Summary Index --------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 /// @file
11 /// ModuleSummaryIndex.h This file contains the declarations the classes that
12 ///  hold the module index and summary for function importing.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_IR_MODULESUMMARYINDEX_H
17 #define LLVM_IR_MODULESUMMARYINDEX_H
18
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/DenseSet.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/ADT/SmallString.h"
23 #include "llvm/ADT/StringExtras.h"
24 #include "llvm/ADT/StringMap.h"
25 #include "llvm/IR/Module.h"
26
27 #include <array>
28
29 namespace llvm {
30
31 namespace yaml {
32 template <typename T> struct MappingTraits;
33 }
34
35 /// \brief Class to accumulate and hold information about a callee.
36 struct CalleeInfo {
37   enum class HotnessType : uint8_t { Unknown = 0, Cold = 1, None = 2, Hot = 3 };
38   HotnessType Hotness = HotnessType::Unknown;
39
40   CalleeInfo() = default;
41   explicit CalleeInfo(HotnessType Hotness) : Hotness(Hotness) {}
42
43   void updateHotness(const HotnessType OtherHotness) {
44     Hotness = std::max(Hotness, OtherHotness);
45   }
46 };
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   };
56
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   };
64
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; }
84 };
85
86 template <> struct DenseMapInfo<ValueInfo> {
87   static inline ValueInfo getEmptyKey() { return ValueInfo((GlobalValue *)-1); }
88   static inline ValueInfo getTombstoneKey() {
89     return ValueInfo((GlobalValue *)-2);
90   }
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());
96   }
97   static unsigned getHashValue(ValueInfo I) {
98     return I.isGUID() ? I.getGUID() : (uintptr_t)I.getValue();
99   }
100 };
101
102 /// \brief Function and variable summary information to aid decisions and
103 /// implementation of importing.
104 class GlobalValueSummary {
105 public:
106   /// \brief Sububclass discriminator (for dyn_cast<> et al.)
107   enum SummaryKind : unsigned { AliasKind, FunctionKind, GlobalVarKind };
108
109   /// Group flags (Linkage, NotEligibleToImport, etc.) as a bitfield.
110   struct GVFlags {
111     /// \brief The linkage type of the associated global value.
112     ///
113     /// One use is to flag values that have local linkage types and need to
114     /// have module identifier appended before placing into the combined
115     /// index, to disambiguate from other values with the same name.
116     /// In the future this will be used to update and optimize linkage
117     /// types based on global summary-based analysis.
118     unsigned Linkage : 4;
119
120     /// Indicate if the global value cannot be imported (e.g. it cannot
121     /// be renamed or references something that can't be renamed).
122     unsigned NotEligibleToImport : 1;
123
124     /// Indicate that the global value must be considered a live root for
125     /// index-based liveness analysis. Used for special LLVM values such as
126     /// llvm.global_ctors that the linker does not know about.
127     unsigned LiveRoot : 1;
128
129     /// Convenience Constructors
130     explicit GVFlags(GlobalValue::LinkageTypes Linkage,
131                      bool NotEligibleToImport, bool LiveRoot)
132         : Linkage(Linkage), NotEligibleToImport(NotEligibleToImport),
133           LiveRoot(LiveRoot) {}
134   };
135
136 private:
137   /// Kind of summary for use in dyn_cast<> et al.
138   SummaryKind Kind;
139
140   GVFlags Flags;
141
142   /// This is the hash of the name of the symbol in the original file. It is
143   /// identical to the GUID for global symbols, but differs for local since the
144   /// GUID includes the module level id in the hash.
145   GlobalValue::GUID OriginalName;
146
147   /// \brief Path of module IR containing value's definition, used to locate
148   /// module during importing.
149   ///
150   /// This is only used during parsing of the combined index, or when
151   /// parsing the per-module index for creation of the combined summary index,
152   /// not during writing of the per-module index which doesn't contain a
153   /// module path string table.
154   StringRef ModulePath;
155
156   /// List of values referenced by this global value's definition
157   /// (either by the initializer of a global variable, or referenced
158   /// from within a function). This does not include functions called, which
159   /// are listed in the derived FunctionSummary object.
160   std::vector<ValueInfo> RefEdgeList;
161
162 protected:
163   /// GlobalValueSummary constructor.
164   GlobalValueSummary(SummaryKind K, GVFlags Flags, std::vector<ValueInfo> Refs)
165       : Kind(K), Flags(Flags), RefEdgeList(std::move(Refs)) {}
166
167 public:
168   virtual ~GlobalValueSummary() = default;
169
170   /// Returns the hash of the original name, it is identical to the GUID for
171   /// externally visible symbols, but not for local ones.
172   GlobalValue::GUID getOriginalName() { return OriginalName; }
173
174   /// Initialize the original name hash in this summary.
175   void setOriginalName(GlobalValue::GUID Name) { OriginalName = Name; }
176
177   /// Which kind of summary subclass this is.
178   SummaryKind getSummaryKind() const { return Kind; }
179
180   /// Set the path to the module containing this function, for use in
181   /// the combined index.
182   void setModulePath(StringRef ModPath) { ModulePath = ModPath; }
183
184   /// Get the path to the module containing this function.
185   StringRef modulePath() const { return ModulePath; }
186
187   /// Get the flags for this GlobalValue (see \p struct GVFlags).
188   GVFlags flags() { return Flags; }
189
190   /// Return linkage type recorded for this global value.
191   GlobalValue::LinkageTypes linkage() const {
192     return static_cast<GlobalValue::LinkageTypes>(Flags.Linkage);
193   }
194
195   /// Sets the linkage to the value determined by global summary-based
196   /// optimization. Will be applied in the ThinLTO backends.
197   void setLinkage(GlobalValue::LinkageTypes Linkage) {
198     Flags.Linkage = Linkage;
199   }
200
201   /// Return true if this global value can't be imported.
202   bool notEligibleToImport() const { return Flags.NotEligibleToImport; }
203
204   /// Return true if this global value must be considered a root for live
205   /// value analysis on the index.
206   bool liveRoot() const { return Flags.LiveRoot; }
207
208   /// Flag that this global value must be considered a root for live
209   /// value analysis on the index.
210   void setLiveRoot() { Flags.LiveRoot = true; }
211
212   /// Flag that this global value cannot be imported.
213   void setNotEligibleToImport() { Flags.NotEligibleToImport = true; }
214
215   /// Return the list of values referenced by this global value definition.
216   ArrayRef<ValueInfo> refs() const { return RefEdgeList; }
217 };
218
219 /// \brief Alias summary information.
220 class AliasSummary : public GlobalValueSummary {
221   GlobalValueSummary *AliaseeSummary;
222
223 public:
224   /// Summary constructors.
225   AliasSummary(GVFlags Flags, std::vector<ValueInfo> Refs)
226       : GlobalValueSummary(AliasKind, Flags, std::move(Refs)) {}
227
228   /// Check if this is an alias summary.
229   static bool classof(const GlobalValueSummary *GVS) {
230     return GVS->getSummaryKind() == AliasKind;
231   }
232
233   void setAliasee(GlobalValueSummary *Aliasee) { AliaseeSummary = Aliasee; }
234
235   const GlobalValueSummary &getAliasee() const {
236     return const_cast<AliasSummary *>(this)->getAliasee();
237   }
238
239   GlobalValueSummary &getAliasee() {
240     assert(AliaseeSummary && "Unexpected missing aliasee summary");
241     return *AliaseeSummary;
242   }
243 };
244
245 /// \brief Function summary information to aid decisions and implementation of
246 /// importing.
247 class FunctionSummary : public GlobalValueSummary {
248 public:
249   /// <CalleeValueInfo, CalleeInfo> call edge pair.
250   typedef std::pair<ValueInfo, CalleeInfo> EdgeTy;
251
252 private:
253   /// Number of instructions (ignoring debug instructions, e.g.) computed
254   /// during the initial compile step when the summary index is first built.
255   unsigned InstCount;
256
257   /// List of <CalleeValueInfo, CalleeInfo> call edge pairs from this function.
258   std::vector<EdgeTy> CallGraphEdgeList;
259
260   /// List of type identifiers used by this function, represented as GUIDs.
261   std::vector<GlobalValue::GUID> TypeIdList;
262
263 public:
264   /// Summary constructors.
265   FunctionSummary(GVFlags Flags, unsigned NumInsts, std::vector<ValueInfo> Refs,
266                   std::vector<EdgeTy> CGEdges,
267                   std::vector<GlobalValue::GUID> TypeIds)
268       : GlobalValueSummary(FunctionKind, Flags, std::move(Refs)),
269         InstCount(NumInsts), CallGraphEdgeList(std::move(CGEdges)),
270         TypeIdList(std::move(TypeIds)) {}
271
272   /// Check if this is a function summary.
273   static bool classof(const GlobalValueSummary *GVS) {
274     return GVS->getSummaryKind() == FunctionKind;
275   }
276
277   /// Get the instruction count recorded for this function.
278   unsigned instCount() const { return InstCount; }
279
280   /// Return the list of <CalleeValueInfo, CalleeInfo> pairs.
281   ArrayRef<EdgeTy> calls() const { return CallGraphEdgeList; }
282
283   /// Returns the list of type identifiers used by this function.
284   ArrayRef<GlobalValue::GUID> type_tests() const { return TypeIdList; }
285 };
286
287 /// \brief Global variable summary information to aid decisions and
288 /// implementation of importing.
289 ///
290 /// Currently this doesn't add anything to the base \p GlobalValueSummary,
291 /// but is a placeholder as additional info may be added to the summary
292 /// for variables.
293 class GlobalVarSummary : public GlobalValueSummary {
294
295 public:
296   /// Summary constructors.
297   GlobalVarSummary(GVFlags Flags, std::vector<ValueInfo> Refs)
298       : GlobalValueSummary(GlobalVarKind, Flags, std::move(Refs)) {}
299
300   /// Check if this is a global variable summary.
301   static bool classof(const GlobalValueSummary *GVS) {
302     return GVS->getSummaryKind() == GlobalVarKind;
303   }
304 };
305
306 struct TypeTestResolution {
307   /// Specifies which kind of type check we should emit for this byte array.
308   /// See http://clang.llvm.org/docs/ControlFlowIntegrityDesign.html for full
309   /// details on each kind of check; the enumerators are described with
310   /// reference to that document.
311   enum Kind {
312     Unsat,     ///< Unsatisfiable type (i.e. no global has this type metadata)
313     ByteArray, ///< Test a byte array (first example)
314     Inline,    ///< Inlined bit vector ("Short Inline Bit Vectors")
315     Single,    ///< Single element (last example in "Short Inline Bit Vectors")
316     AllOnes,   ///< All-ones bit vector ("Eliminating Bit Vector Checks for
317                ///  All-Ones Bit Vectors")
318   } TheKind = Unsat;
319
320   /// Range of size-1 expressed as a bit width. For example, if the size is in
321   /// range [1,256], this number will be 8. This helps generate the most compact
322   /// instruction sequences.
323   unsigned SizeM1BitWidth = 0;
324 };
325
326 struct TypeIdSummary {
327   TypeTestResolution TTRes;
328 };
329
330 /// 160 bits SHA1
331 typedef std::array<uint32_t, 5> ModuleHash;
332
333 /// List of global value summary structures for a particular value held
334 /// in the GlobalValueMap. Requires a vector in the case of multiple
335 /// COMDAT values of the same name.
336 typedef std::vector<std::unique_ptr<GlobalValueSummary>> GlobalValueSummaryList;
337
338 /// Map from global value GUID to corresponding summary structures.
339 /// Use a std::map rather than a DenseMap since it will likely incur
340 /// less overhead, as the value type is not very small and the size
341 /// of the map is unknown, resulting in inefficiencies due to repeated
342 /// insertions and resizing.
343 typedef std::map<GlobalValue::GUID, GlobalValueSummaryList>
344     GlobalValueSummaryMapTy;
345
346 /// Type used for iterating through the global value summary map.
347 typedef GlobalValueSummaryMapTy::const_iterator const_gvsummary_iterator;
348 typedef GlobalValueSummaryMapTy::iterator gvsummary_iterator;
349
350 /// String table to hold/own module path strings, which additionally holds the
351 /// module ID assigned to each module during the plugin step, as well as a hash
352 /// of the module. The StringMap makes a copy of and owns inserted strings.
353 typedef StringMap<std::pair<uint64_t, ModuleHash>> ModulePathStringTableTy;
354
355 /// Map of global value GUID to its summary, used to identify values defined in
356 /// a particular module, and provide efficient access to their summary.
357 typedef std::map<GlobalValue::GUID, GlobalValueSummary *> GVSummaryMapTy;
358
359 /// Class to hold module path string table and global value map,
360 /// and encapsulate methods for operating on them.
361 class ModuleSummaryIndex {
362 private:
363   /// Map from value name to list of summary instances for values of that
364   /// name (may be duplicates in the COMDAT case, e.g.).
365   GlobalValueSummaryMapTy GlobalValueMap;
366
367   /// Holds strings for combined index, mapping to the corresponding module ID.
368   ModulePathStringTableTy ModulePathStringTable;
369
370   /// Mapping from type identifiers to summary information for that type
371   /// identifier.
372   // FIXME: Add bitcode read/write support for this field.
373   std::map<std::string, TypeIdSummary> TypeIdMap;
374
375   // YAML I/O support.
376   friend yaml::MappingTraits<ModuleSummaryIndex>;
377
378 public:
379   gvsummary_iterator begin() { return GlobalValueMap.begin(); }
380   const_gvsummary_iterator begin() const { return GlobalValueMap.begin(); }
381   gvsummary_iterator end() { return GlobalValueMap.end(); }
382   const_gvsummary_iterator end() const { return GlobalValueMap.end(); }
383   size_t size() const { return GlobalValueMap.size(); }
384
385   /// Get the list of global value summary objects for a given value name.
386   const GlobalValueSummaryList &getGlobalValueSummaryList(StringRef ValueName) {
387     return GlobalValueMap[GlobalValue::getGUID(ValueName)];
388   }
389
390   /// Get the list of global value summary objects for a given value name.
391   const const_gvsummary_iterator
392   findGlobalValueSummaryList(StringRef ValueName) const {
393     return GlobalValueMap.find(GlobalValue::getGUID(ValueName));
394   }
395
396   /// Get the list of global value summary objects for a given value GUID.
397   const const_gvsummary_iterator
398   findGlobalValueSummaryList(GlobalValue::GUID ValueGUID) const {
399     return GlobalValueMap.find(ValueGUID);
400   }
401
402   /// Add a global value summary for a value of the given name.
403   void addGlobalValueSummary(StringRef ValueName,
404                              std::unique_ptr<GlobalValueSummary> Summary) {
405     GlobalValueMap[GlobalValue::getGUID(ValueName)].push_back(
406         std::move(Summary));
407   }
408
409   /// Add a global value summary for a value of the given GUID.
410   void addGlobalValueSummary(GlobalValue::GUID ValueGUID,
411                              std::unique_ptr<GlobalValueSummary> Summary) {
412     GlobalValueMap[ValueGUID].push_back(std::move(Summary));
413   }
414
415   /// Find the summary for global \p GUID in module \p ModuleId, or nullptr if
416   /// not found.
417   GlobalValueSummary *findSummaryInModule(GlobalValue::GUID ValueGUID,
418                                           StringRef ModuleId) const {
419     auto CalleeInfoList = findGlobalValueSummaryList(ValueGUID);
420     if (CalleeInfoList == end()) {
421       return nullptr; // This function does not have a summary
422     }
423     auto Summary =
424         llvm::find_if(CalleeInfoList->second,
425                       [&](const std::unique_ptr<GlobalValueSummary> &Summary) {
426                         return Summary->modulePath() == ModuleId;
427                       });
428     if (Summary == CalleeInfoList->second.end())
429       return nullptr;
430     return Summary->get();
431   }
432
433   /// Returns the first GlobalValueSummary for \p GV, asserting that there
434   /// is only one if \p PerModuleIndex.
435   GlobalValueSummary *getGlobalValueSummary(const GlobalValue &GV,
436                                             bool PerModuleIndex = true) const {
437     assert(GV.hasName() && "Can't get GlobalValueSummary for GV with no name");
438     return getGlobalValueSummary(GlobalValue::getGUID(GV.getName()),
439                                  PerModuleIndex);
440   }
441
442   /// Returns the first GlobalValueSummary for \p ValueGUID, asserting that
443   /// there
444   /// is only one if \p PerModuleIndex.
445   GlobalValueSummary *getGlobalValueSummary(GlobalValue::GUID ValueGUID,
446                                             bool PerModuleIndex = true) const;
447
448   /// Table of modules, containing module hash and id.
449   const StringMap<std::pair<uint64_t, ModuleHash>> &modulePaths() const {
450     return ModulePathStringTable;
451   }
452
453   /// Table of modules, containing hash and id.
454   StringMap<std::pair<uint64_t, ModuleHash>> &modulePaths() {
455     return ModulePathStringTable;
456   }
457
458   /// Get the module ID recorded for the given module path.
459   uint64_t getModuleId(const StringRef ModPath) const {
460     return ModulePathStringTable.lookup(ModPath).first;
461   }
462
463   /// Get the module SHA1 hash recorded for the given module path.
464   const ModuleHash &getModuleHash(const StringRef ModPath) const {
465     auto It = ModulePathStringTable.find(ModPath);
466     assert(It != ModulePathStringTable.end() && "Module not registered");
467     return It->second.second;
468   }
469
470   /// Add the given per-module index into this module index/summary,
471   /// assigning it the given module ID. Each module merged in should have
472   /// a unique ID, necessary for consistent renaming of promoted
473   /// static (local) variables.
474   void mergeFrom(std::unique_ptr<ModuleSummaryIndex> Other,
475                  uint64_t NextModuleId);
476
477   /// Convenience method for creating a promoted global name
478   /// for the given value name of a local, and its original module's ID.
479   static std::string getGlobalNameForLocal(StringRef Name, ModuleHash ModHash) {
480     SmallString<256> NewName(Name);
481     NewName += ".llvm.";
482     NewName += utohexstr(ModHash[0]); // Take the first 32 bits
483     return NewName.str();
484   }
485
486   /// Helper to obtain the unpromoted name for a global value (or the original
487   /// name if not promoted).
488   static StringRef getOriginalNameBeforePromote(StringRef Name) {
489     std::pair<StringRef, StringRef> Pair = Name.split(".llvm.");
490     return Pair.first;
491   }
492
493   /// Add a new module path with the given \p Hash, mapped to the given \p
494   /// ModID, and return an iterator to the entry in the index.
495   ModulePathStringTableTy::iterator
496   addModulePath(StringRef ModPath, uint64_t ModId,
497                 ModuleHash Hash = ModuleHash{{0}}) {
498     return ModulePathStringTable.insert(std::make_pair(
499                                             ModPath,
500                                             std::make_pair(ModId, Hash))).first;
501   }
502
503   /// Check if the given Module has any functions available for exporting
504   /// in the index. We consider any module present in the ModulePathStringTable
505   /// to have exported functions.
506   bool hasExportedFunctions(const Module &M) const {
507     return ModulePathStringTable.count(M.getModuleIdentifier());
508   }
509
510   /// Remove entries in the GlobalValueMap that have empty summaries due to the
511   /// eager nature of map entry creation during VST parsing. These would
512   /// also be suppressed during combined index generation in mergeFrom(),
513   /// but if there was only one module or this was the first module we might
514   /// not invoke mergeFrom.
515   void removeEmptySummaryEntries();
516
517   /// Collect for the given module the list of function it defines
518   /// (GUID -> Summary).
519   void collectDefinedFunctionsForModule(StringRef ModulePath,
520                                         GVSummaryMapTy &GVSummaryMap) const;
521
522   /// Collect for each module the list of Summaries it defines (GUID ->
523   /// Summary).
524   void collectDefinedGVSummariesPerModule(
525       StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries) const;
526 };
527
528 } // End llvm namespace
529
530 #endif