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), OriginalName(0), 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 assert(AliaseeSummary && "Unexpected missing aliasee summary"); 237 return *AliaseeSummary; 238 } 239 240 GlobalValueSummary &getAliasee() { 241 return const_cast<GlobalValueSummary &>( 242 static_cast<const AliasSummary *>(this)->getAliasee()); 243 } 244 }; 245 246 /// \brief Function summary information to aid decisions and implementation of 247 /// importing. 248 class FunctionSummary : public GlobalValueSummary { 249 public: 250 /// <CalleeValueInfo, CalleeInfo> call edge pair. 251 typedef std::pair<ValueInfo, CalleeInfo> EdgeTy; 252 253 /// An "identifier" for a virtual function. This contains the type identifier 254 /// represented as a GUID and the offset from the address point to the virtual 255 /// function pointer, where "address point" is as defined in the Itanium ABI: 256 /// https://mentorembedded.github.io/cxx-abi/abi.html#vtable-general 257 struct VFuncId { 258 GlobalValue::GUID GUID; 259 uint64_t Offset; 260 }; 261 262 /// A specification for a virtual function call with all constant integer 263 /// arguments. This is used to perform virtual constant propagation on the 264 /// summary. 265 struct ConstVCall { 266 VFuncId VFunc; 267 std::vector<uint64_t> Args; 268 }; 269 270 private: 271 /// Number of instructions (ignoring debug instructions, e.g.) computed 272 /// during the initial compile step when the summary index is first built. 273 unsigned InstCount; 274 275 /// List of <CalleeValueInfo, CalleeInfo> call edge pairs from this function. 276 std::vector<EdgeTy> CallGraphEdgeList; 277 278 /// All type identifier related information. Because these fields are 279 /// relatively uncommon we only allocate space for them if necessary. 280 struct TypeIdInfo { 281 /// List of type identifiers used by this function in llvm.type.test 282 /// intrinsics other than by an llvm.assume intrinsic, represented as GUIDs. 283 std::vector<GlobalValue::GUID> TypeTests; 284 285 /// List of virtual calls made by this function using (respectively) 286 /// llvm.assume(llvm.type.test) or llvm.type.checked.load intrinsics that do 287 /// not have all constant integer arguments. 288 std::vector<VFuncId> TypeTestAssumeVCalls, TypeCheckedLoadVCalls; 289 290 /// List of virtual calls made by this function using (respectively) 291 /// llvm.assume(llvm.type.test) or llvm.type.checked.load intrinsics with 292 /// all constant integer arguments. 293 std::vector<ConstVCall> TypeTestAssumeConstVCalls, 294 TypeCheckedLoadConstVCalls; 295 }; 296 297 std::unique_ptr<TypeIdInfo> TIdInfo; 298 299 public: 300 /// Summary constructors. 301 FunctionSummary(GVFlags Flags, unsigned NumInsts, std::vector<ValueInfo> Refs, 302 std::vector<EdgeTy> CGEdges, 303 std::vector<GlobalValue::GUID> TypeTests, 304 std::vector<VFuncId> TypeTestAssumeVCalls, 305 std::vector<VFuncId> TypeCheckedLoadVCalls, 306 std::vector<ConstVCall> TypeTestAssumeConstVCalls, 307 std::vector<ConstVCall> TypeCheckedLoadConstVCalls) 308 : GlobalValueSummary(FunctionKind, Flags, std::move(Refs)), 309 InstCount(NumInsts), CallGraphEdgeList(std::move(CGEdges)) { 310 if (!TypeTests.empty() || !TypeTestAssumeVCalls.empty() || 311 !TypeCheckedLoadVCalls.empty() || !TypeTestAssumeConstVCalls.empty() || 312 !TypeCheckedLoadConstVCalls.empty()) 313 TIdInfo = llvm::make_unique<TypeIdInfo>(TypeIdInfo{ 314 std::move(TypeTests), std::move(TypeTestAssumeVCalls), 315 std::move(TypeCheckedLoadVCalls), 316 std::move(TypeTestAssumeConstVCalls), 317 std::move(TypeCheckedLoadConstVCalls)}); 318 } 319 320 /// Check if this is a function summary. 321 static bool classof(const GlobalValueSummary *GVS) { 322 return GVS->getSummaryKind() == FunctionKind; 323 } 324 325 /// Get the instruction count recorded for this function. 326 unsigned instCount() const { return InstCount; } 327 328 /// Return the list of <CalleeValueInfo, CalleeInfo> pairs. 329 ArrayRef<EdgeTy> calls() const { return CallGraphEdgeList; } 330 331 /// Returns the list of type identifiers used by this function in 332 /// llvm.type.test intrinsics other than by an llvm.assume intrinsic, 333 /// represented as GUIDs. 334 ArrayRef<GlobalValue::GUID> type_tests() const { 335 if (TIdInfo) 336 return TIdInfo->TypeTests; 337 return {}; 338 } 339 340 /// Returns the list of virtual calls made by this function using 341 /// llvm.assume(llvm.type.test) intrinsics that do not have all constant 342 /// integer arguments. 343 ArrayRef<VFuncId> type_test_assume_vcalls() const { 344 if (TIdInfo) 345 return TIdInfo->TypeTestAssumeVCalls; 346 return {}; 347 } 348 349 /// Returns the list of virtual calls made by this function using 350 /// llvm.type.checked.load intrinsics that do not have all constant integer 351 /// arguments. 352 ArrayRef<VFuncId> type_checked_load_vcalls() const { 353 if (TIdInfo) 354 return TIdInfo->TypeCheckedLoadVCalls; 355 return {}; 356 } 357 358 /// Returns the list of virtual calls made by this function using 359 /// llvm.assume(llvm.type.test) intrinsics with all constant integer 360 /// arguments. 361 ArrayRef<ConstVCall> type_test_assume_const_vcalls() const { 362 if (TIdInfo) 363 return TIdInfo->TypeTestAssumeConstVCalls; 364 return {}; 365 } 366 367 /// Returns the list of virtual calls made by this function using 368 /// llvm.type.checked.load intrinsics with all constant integer arguments. 369 ArrayRef<ConstVCall> type_checked_load_const_vcalls() const { 370 if (TIdInfo) 371 return TIdInfo->TypeCheckedLoadConstVCalls; 372 return {}; 373 } 374 375 /// Add a type test to the summary. This is used by WholeProgramDevirt if we 376 /// were unable to devirtualize a checked call. 377 void addTypeTest(GlobalValue::GUID Guid) { 378 if (!TIdInfo) 379 TIdInfo = llvm::make_unique<TypeIdInfo>(); 380 TIdInfo->TypeTests.push_back(Guid); 381 } 382 }; 383 384 template <> struct DenseMapInfo<FunctionSummary::VFuncId> { 385 static FunctionSummary::VFuncId getEmptyKey() { return {0, uint64_t(-1)}; } 386 static FunctionSummary::VFuncId getTombstoneKey() { 387 return {0, uint64_t(-2)}; 388 } 389 static bool isEqual(FunctionSummary::VFuncId L, FunctionSummary::VFuncId R) { 390 return L.GUID == R.GUID && L.Offset == R.Offset; 391 } 392 static unsigned getHashValue(FunctionSummary::VFuncId I) { return I.GUID; } 393 }; 394 395 template <> struct DenseMapInfo<FunctionSummary::ConstVCall> { 396 static FunctionSummary::ConstVCall getEmptyKey() { 397 return {{0, uint64_t(-1)}, {}}; 398 } 399 static FunctionSummary::ConstVCall getTombstoneKey() { 400 return {{0, uint64_t(-2)}, {}}; 401 } 402 static bool isEqual(FunctionSummary::ConstVCall L, 403 FunctionSummary::ConstVCall R) { 404 return DenseMapInfo<FunctionSummary::VFuncId>::isEqual(L.VFunc, R.VFunc) && 405 L.Args == R.Args; 406 } 407 static unsigned getHashValue(FunctionSummary::ConstVCall I) { 408 return I.VFunc.GUID; 409 } 410 }; 411 412 /// \brief Global variable summary information to aid decisions and 413 /// implementation of importing. 414 /// 415 /// Currently this doesn't add anything to the base \p GlobalValueSummary, 416 /// but is a placeholder as additional info may be added to the summary 417 /// for variables. 418 class GlobalVarSummary : public GlobalValueSummary { 419 420 public: 421 /// Summary constructors. 422 GlobalVarSummary(GVFlags Flags, std::vector<ValueInfo> Refs) 423 : GlobalValueSummary(GlobalVarKind, Flags, std::move(Refs)) {} 424 425 /// Check if this is a global variable summary. 426 static bool classof(const GlobalValueSummary *GVS) { 427 return GVS->getSummaryKind() == GlobalVarKind; 428 } 429 }; 430 431 struct TypeTestResolution { 432 /// Specifies which kind of type check we should emit for this byte array. 433 /// See http://clang.llvm.org/docs/ControlFlowIntegrityDesign.html for full 434 /// details on each kind of check; the enumerators are described with 435 /// reference to that document. 436 enum Kind { 437 Unsat, ///< Unsatisfiable type (i.e. no global has this type metadata) 438 ByteArray, ///< Test a byte array (first example) 439 Inline, ///< Inlined bit vector ("Short Inline Bit Vectors") 440 Single, ///< Single element (last example in "Short Inline Bit Vectors") 441 AllOnes, ///< All-ones bit vector ("Eliminating Bit Vector Checks for 442 /// All-Ones Bit Vectors") 443 } TheKind = Unsat; 444 445 /// Range of size-1 expressed as a bit width. For example, if the size is in 446 /// range [1,256], this number will be 8. This helps generate the most compact 447 /// instruction sequences. 448 unsigned SizeM1BitWidth = 0; 449 }; 450 451 struct WholeProgramDevirtResolution { 452 enum Kind { 453 Indir, ///< Just do a regular virtual call 454 SingleImpl, ///< Single implementation devirtualization 455 } TheKind = Indir; 456 457 std::string SingleImplName; 458 459 struct ByArg { 460 enum Kind { 461 Indir, ///< Just do a regular virtual call 462 UniformRetVal, ///< Uniform return value optimization 463 UniqueRetVal, ///< Unique return value optimization 464 VirtualConstProp, ///< Virtual constant propagation 465 } TheKind = Indir; 466 467 /// Additional information for the resolution: 468 /// - UniformRetVal: the uniform return value. 469 /// - UniqueRetVal: the return value associated with the unique vtable (0 or 470 /// 1). 471 uint64_t Info = 0; 472 }; 473 474 /// Resolutions for calls with all constant integer arguments (excluding the 475 /// first argument, "this"), where the key is the argument vector. 476 std::map<std::vector<uint64_t>, ByArg> ResByArg; 477 }; 478 479 struct TypeIdSummary { 480 TypeTestResolution TTRes; 481 482 /// Mapping from byte offset to whole-program devirt resolution for that 483 /// (typeid, byte offset) pair. 484 std::map<uint64_t, WholeProgramDevirtResolution> WPDRes; 485 }; 486 487 /// 160 bits SHA1 488 typedef std::array<uint32_t, 5> ModuleHash; 489 490 /// List of global value summary structures for a particular value held 491 /// in the GlobalValueMap. Requires a vector in the case of multiple 492 /// COMDAT values of the same name. 493 typedef std::vector<std::unique_ptr<GlobalValueSummary>> GlobalValueSummaryList; 494 495 /// Map from global value GUID to corresponding summary structures. 496 /// Use a std::map rather than a DenseMap since it will likely incur 497 /// less overhead, as the value type is not very small and the size 498 /// of the map is unknown, resulting in inefficiencies due to repeated 499 /// insertions and resizing. 500 typedef std::map<GlobalValue::GUID, GlobalValueSummaryList> 501 GlobalValueSummaryMapTy; 502 503 /// Type used for iterating through the global value summary map. 504 typedef GlobalValueSummaryMapTy::const_iterator const_gvsummary_iterator; 505 typedef GlobalValueSummaryMapTy::iterator gvsummary_iterator; 506 507 /// String table to hold/own module path strings, which additionally holds the 508 /// module ID assigned to each module during the plugin step, as well as a hash 509 /// of the module. The StringMap makes a copy of and owns inserted strings. 510 typedef StringMap<std::pair<uint64_t, ModuleHash>> ModulePathStringTableTy; 511 512 /// Map of global value GUID to its summary, used to identify values defined in 513 /// a particular module, and provide efficient access to their summary. 514 typedef std::map<GlobalValue::GUID, GlobalValueSummary *> GVSummaryMapTy; 515 516 /// Class to hold module path string table and global value map, 517 /// and encapsulate methods for operating on them. 518 class ModuleSummaryIndex { 519 private: 520 /// Map from value name to list of summary instances for values of that 521 /// name (may be duplicates in the COMDAT case, e.g.). 522 GlobalValueSummaryMapTy GlobalValueMap; 523 524 /// Holds strings for combined index, mapping to the corresponding module ID. 525 ModulePathStringTableTy ModulePathStringTable; 526 527 /// Mapping from type identifiers to summary information for that type 528 /// identifier. 529 // FIXME: Add bitcode read/write support for this field. 530 std::map<std::string, TypeIdSummary> TypeIdMap; 531 532 /// Mapping from original ID to GUID. If original ID can map to multiple 533 /// GUIDs, it will be mapped to 0. 534 std::map<GlobalValue::GUID, GlobalValue::GUID> OidGuidMap; 535 536 // YAML I/O support. 537 friend yaml::MappingTraits<ModuleSummaryIndex>; 538 539 public: 540 gvsummary_iterator begin() { return GlobalValueMap.begin(); } 541 const_gvsummary_iterator begin() const { return GlobalValueMap.begin(); } 542 gvsummary_iterator end() { return GlobalValueMap.end(); } 543 const_gvsummary_iterator end() const { return GlobalValueMap.end(); } 544 size_t size() const { return GlobalValueMap.size(); } 545 546 /// Get the list of global value summary objects for a given value name. 547 const GlobalValueSummaryList &getGlobalValueSummaryList(StringRef ValueName) { 548 return GlobalValueMap[GlobalValue::getGUID(ValueName)]; 549 } 550 551 /// Get the list of global value summary objects for a given value name. 552 const const_gvsummary_iterator 553 findGlobalValueSummaryList(StringRef ValueName) const { 554 return GlobalValueMap.find(GlobalValue::getGUID(ValueName)); 555 } 556 557 /// Get the list of global value summary objects for a given value GUID. 558 const const_gvsummary_iterator 559 findGlobalValueSummaryList(GlobalValue::GUID ValueGUID) const { 560 return GlobalValueMap.find(ValueGUID); 561 } 562 563 /// Return the GUID for \p OriginalId in the OidGuidMap. 564 GlobalValue::GUID getGUIDFromOriginalID(GlobalValue::GUID OriginalID) const { 565 const auto I = OidGuidMap.find(OriginalID); 566 return I == OidGuidMap.end() ? 0 : I->second; 567 } 568 569 /// Add a global value summary for a value of the given name. 570 void addGlobalValueSummary(StringRef ValueName, 571 std::unique_ptr<GlobalValueSummary> Summary) { 572 addOriginalName(GlobalValue::getGUID(ValueName), 573 Summary->getOriginalName()); 574 GlobalValueMap[GlobalValue::getGUID(ValueName)].push_back( 575 std::move(Summary)); 576 } 577 578 /// Add a global value summary for a value of the given GUID. 579 void addGlobalValueSummary(GlobalValue::GUID ValueGUID, 580 std::unique_ptr<GlobalValueSummary> Summary) { 581 addOriginalName(ValueGUID, Summary->getOriginalName()); 582 GlobalValueMap[ValueGUID].push_back(std::move(Summary)); 583 } 584 585 /// Add an original name for the value of the given GUID. 586 void addOriginalName(GlobalValue::GUID ValueGUID, 587 GlobalValue::GUID OrigGUID) { 588 if (OrigGUID == 0 || ValueGUID == OrigGUID) 589 return; 590 if (OidGuidMap.count(OrigGUID) && OidGuidMap[OrigGUID] != ValueGUID) 591 OidGuidMap[OrigGUID] = 0; 592 else 593 OidGuidMap[OrigGUID] = ValueGUID; 594 } 595 596 /// Find the summary for global \p GUID in module \p ModuleId, or nullptr if 597 /// not found. 598 GlobalValueSummary *findSummaryInModule(GlobalValue::GUID ValueGUID, 599 StringRef ModuleId) const { 600 auto CalleeInfoList = findGlobalValueSummaryList(ValueGUID); 601 if (CalleeInfoList == end()) { 602 return nullptr; // This function does not have a summary 603 } 604 auto Summary = 605 llvm::find_if(CalleeInfoList->second, 606 [&](const std::unique_ptr<GlobalValueSummary> &Summary) { 607 return Summary->modulePath() == ModuleId; 608 }); 609 if (Summary == CalleeInfoList->second.end()) 610 return nullptr; 611 return Summary->get(); 612 } 613 614 /// Returns the first GlobalValueSummary for \p GV, asserting that there 615 /// is only one if \p PerModuleIndex. 616 GlobalValueSummary *getGlobalValueSummary(const GlobalValue &GV, 617 bool PerModuleIndex = true) const { 618 assert(GV.hasName() && "Can't get GlobalValueSummary for GV with no name"); 619 return getGlobalValueSummary(GlobalValue::getGUID(GV.getName()), 620 PerModuleIndex); 621 } 622 623 /// Returns the first GlobalValueSummary for \p ValueGUID, asserting that 624 /// there 625 /// is only one if \p PerModuleIndex. 626 GlobalValueSummary *getGlobalValueSummary(GlobalValue::GUID ValueGUID, 627 bool PerModuleIndex = true) const; 628 629 /// Table of modules, containing module hash and id. 630 const StringMap<std::pair<uint64_t, ModuleHash>> &modulePaths() const { 631 return ModulePathStringTable; 632 } 633 634 /// Table of modules, containing hash and id. 635 StringMap<std::pair<uint64_t, ModuleHash>> &modulePaths() { 636 return ModulePathStringTable; 637 } 638 639 /// Get the module ID recorded for the given module path. 640 uint64_t getModuleId(const StringRef ModPath) const { 641 return ModulePathStringTable.lookup(ModPath).first; 642 } 643 644 /// Get the module SHA1 hash recorded for the given module path. 645 const ModuleHash &getModuleHash(const StringRef ModPath) const { 646 auto It = ModulePathStringTable.find(ModPath); 647 assert(It != ModulePathStringTable.end() && "Module not registered"); 648 return It->second.second; 649 } 650 651 /// Add the given per-module index into this module index/summary, 652 /// assigning it the given module ID. Each module merged in should have 653 /// a unique ID, necessary for consistent renaming of promoted 654 /// static (local) variables. 655 void mergeFrom(std::unique_ptr<ModuleSummaryIndex> Other, 656 uint64_t NextModuleId); 657 658 /// Convenience method for creating a promoted global name 659 /// for the given value name of a local, and its original module's ID. 660 static std::string getGlobalNameForLocal(StringRef Name, ModuleHash ModHash) { 661 SmallString<256> NewName(Name); 662 NewName += ".llvm."; 663 NewName += utohexstr(ModHash[0]); // Take the first 32 bits 664 return NewName.str(); 665 } 666 667 /// Helper to obtain the unpromoted name for a global value (or the original 668 /// name if not promoted). 669 static StringRef getOriginalNameBeforePromote(StringRef Name) { 670 std::pair<StringRef, StringRef> Pair = Name.split(".llvm."); 671 return Pair.first; 672 } 673 674 /// Add a new module path with the given \p Hash, mapped to the given \p 675 /// ModID, and return an iterator to the entry in the index. 676 ModulePathStringTableTy::iterator 677 addModulePath(StringRef ModPath, uint64_t ModId, 678 ModuleHash Hash = ModuleHash{{0}}) { 679 return ModulePathStringTable.insert(std::make_pair( 680 ModPath, 681 std::make_pair(ModId, Hash))).first; 682 } 683 684 /// Check if the given Module has any functions available for exporting 685 /// in the index. We consider any module present in the ModulePathStringTable 686 /// to have exported functions. 687 bool hasExportedFunctions(const Module &M) const { 688 return ModulePathStringTable.count(M.getModuleIdentifier()); 689 } 690 691 const std::map<std::string, TypeIdSummary> &typeIds() const { 692 return TypeIdMap; 693 } 694 695 /// This accessor should only be used when exporting because it can mutate the 696 /// map. 697 TypeIdSummary &getOrInsertTypeIdSummary(StringRef TypeId) { 698 return TypeIdMap[TypeId]; 699 } 700 701 /// This returns either a pointer to the type id summary (if present in the 702 /// summary map) or null (if not present). This may be used when importing. 703 const TypeIdSummary *getTypeIdSummary(StringRef TypeId) const { 704 auto I = TypeIdMap.find(TypeId); 705 if (I == TypeIdMap.end()) 706 return nullptr; 707 return &I->second; 708 } 709 710 /// Remove entries in the GlobalValueMap that have empty summaries due to the 711 /// eager nature of map entry creation during VST parsing. These would 712 /// also be suppressed during combined index generation in mergeFrom(), 713 /// but if there was only one module or this was the first module we might 714 /// not invoke mergeFrom. 715 void removeEmptySummaryEntries(); 716 717 /// Collect for the given module the list of function it defines 718 /// (GUID -> Summary). 719 void collectDefinedFunctionsForModule(StringRef ModulePath, 720 GVSummaryMapTy &GVSummaryMap) const; 721 722 /// Collect for each module the list of Summaries it defines (GUID -> 723 /// Summary). 724 void collectDefinedGVSummariesPerModule( 725 StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries) const; 726 }; 727 728 } // End llvm namespace 729 730 #endif 731