1 //===- LLVMContextImpl.h - The LLVMContextImpl opaque class -----*- 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 // This file declares LLVMContextImpl, the opaque implementation 10 // of LLVMContext. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_LIB_IR_LLVMCONTEXTIMPL_H 15 #define LLVM_LIB_IR_LLVMCONTEXTIMPL_H 16 17 #include "ConstantsContext.h" 18 #include "llvm/ADT/APFloat.h" 19 #include "llvm/ADT/APInt.h" 20 #include "llvm/ADT/ArrayRef.h" 21 #include "llvm/ADT/DenseMap.h" 22 #include "llvm/ADT/DenseMapInfo.h" 23 #include "llvm/ADT/DenseSet.h" 24 #include "llvm/ADT/FoldingSet.h" 25 #include "llvm/ADT/Hashing.h" 26 #include "llvm/ADT/STLExtras.h" 27 #include "llvm/ADT/SmallPtrSet.h" 28 #include "llvm/ADT/SmallVector.h" 29 #include "llvm/ADT/StringMap.h" 30 #include "llvm/BinaryFormat/Dwarf.h" 31 #include "llvm/IR/Constants.h" 32 #include "llvm/IR/DebugInfoMetadata.h" 33 #include "llvm/IR/DerivedTypes.h" 34 #include "llvm/IR/LLVMContext.h" 35 #include "llvm/IR/Metadata.h" 36 #include "llvm/IR/Module.h" 37 #include "llvm/IR/TrackingMDRef.h" 38 #include "llvm/IR/Type.h" 39 #include "llvm/IR/Value.h" 40 #include "llvm/Support/Allocator.h" 41 #include "llvm/Support/Casting.h" 42 #include "llvm/Support/StringSaver.h" 43 #include <algorithm> 44 #include <cassert> 45 #include <cstddef> 46 #include <cstdint> 47 #include <memory> 48 #include <optional> 49 #include <string> 50 #include <utility> 51 #include <vector> 52 53 namespace llvm { 54 55 class AttributeImpl; 56 class AttributeListImpl; 57 class AttributeSetNode; 58 class BasicBlock; 59 class ConstantRangeAttributeImpl; 60 class ConstantRangeListAttributeImpl; 61 struct DiagnosticHandler; 62 class DbgMarker; 63 class ElementCount; 64 class Function; 65 class GlobalObject; 66 class GlobalValue; 67 class InlineAsm; 68 class LLVMRemarkStreamer; 69 class OptPassGate; 70 namespace remarks { 71 class RemarkStreamer; 72 } 73 template <typename T> class StringMapEntry; 74 class StringRef; 75 class TypedPointerType; 76 class ValueHandleBase; 77 78 template <> struct DenseMapInfo<APFloat> { 79 static inline APFloat getEmptyKey() { return APFloat(APFloat::Bogus(), 1); } 80 static inline APFloat getTombstoneKey() { 81 return APFloat(APFloat::Bogus(), 2); 82 } 83 84 static unsigned getHashValue(const APFloat &Key) { 85 return static_cast<unsigned>(hash_value(Key)); 86 } 87 88 static bool isEqual(const APFloat &LHS, const APFloat &RHS) { 89 return LHS.bitwiseIsEqual(RHS); 90 } 91 }; 92 93 struct AnonStructTypeKeyInfo { 94 struct KeyTy { 95 ArrayRef<Type *> ETypes; 96 bool isPacked; 97 98 KeyTy(const ArrayRef<Type *> &E, bool P) : ETypes(E), isPacked(P) {} 99 100 KeyTy(const StructType *ST) 101 : ETypes(ST->elements()), isPacked(ST->isPacked()) {} 102 103 bool operator==(const KeyTy &that) const { 104 if (isPacked != that.isPacked) 105 return false; 106 if (ETypes != that.ETypes) 107 return false; 108 return true; 109 } 110 bool operator!=(const KeyTy &that) const { return !this->operator==(that); } 111 }; 112 113 static inline StructType *getEmptyKey() { 114 return DenseMapInfo<StructType *>::getEmptyKey(); 115 } 116 117 static inline StructType *getTombstoneKey() { 118 return DenseMapInfo<StructType *>::getTombstoneKey(); 119 } 120 121 static unsigned getHashValue(const KeyTy &Key) { 122 return hash_combine(hash_combine_range(Key.ETypes), Key.isPacked); 123 } 124 125 static unsigned getHashValue(const StructType *ST) { 126 return getHashValue(KeyTy(ST)); 127 } 128 129 static bool isEqual(const KeyTy &LHS, const StructType *RHS) { 130 if (RHS == getEmptyKey() || RHS == getTombstoneKey()) 131 return false; 132 return LHS == KeyTy(RHS); 133 } 134 135 static bool isEqual(const StructType *LHS, const StructType *RHS) { 136 return LHS == RHS; 137 } 138 }; 139 140 struct FunctionTypeKeyInfo { 141 struct KeyTy { 142 const Type *ReturnType; 143 ArrayRef<Type *> Params; 144 bool isVarArg; 145 146 KeyTy(const Type *R, const ArrayRef<Type *> &P, bool V) 147 : ReturnType(R), Params(P), isVarArg(V) {} 148 KeyTy(const FunctionType *FT) 149 : ReturnType(FT->getReturnType()), Params(FT->params()), 150 isVarArg(FT->isVarArg()) {} 151 152 bool operator==(const KeyTy &that) const { 153 if (ReturnType != that.ReturnType) 154 return false; 155 if (isVarArg != that.isVarArg) 156 return false; 157 if (Params != that.Params) 158 return false; 159 return true; 160 } 161 bool operator!=(const KeyTy &that) const { return !this->operator==(that); } 162 }; 163 164 static inline FunctionType *getEmptyKey() { 165 return DenseMapInfo<FunctionType *>::getEmptyKey(); 166 } 167 168 static inline FunctionType *getTombstoneKey() { 169 return DenseMapInfo<FunctionType *>::getTombstoneKey(); 170 } 171 172 static unsigned getHashValue(const KeyTy &Key) { 173 return hash_combine(Key.ReturnType, hash_combine_range(Key.Params), 174 Key.isVarArg); 175 } 176 177 static unsigned getHashValue(const FunctionType *FT) { 178 return getHashValue(KeyTy(FT)); 179 } 180 181 static bool isEqual(const KeyTy &LHS, const FunctionType *RHS) { 182 if (RHS == getEmptyKey() || RHS == getTombstoneKey()) 183 return false; 184 return LHS == KeyTy(RHS); 185 } 186 187 static bool isEqual(const FunctionType *LHS, const FunctionType *RHS) { 188 return LHS == RHS; 189 } 190 }; 191 192 struct TargetExtTypeKeyInfo { 193 struct KeyTy { 194 StringRef Name; 195 ArrayRef<Type *> TypeParams; 196 ArrayRef<unsigned> IntParams; 197 198 KeyTy(StringRef N, const ArrayRef<Type *> &TP, const ArrayRef<unsigned> &IP) 199 : Name(N), TypeParams(TP), IntParams(IP) {} 200 KeyTy(const TargetExtType *TT) 201 : Name(TT->getName()), TypeParams(TT->type_params()), 202 IntParams(TT->int_params()) {} 203 204 bool operator==(const KeyTy &that) const { 205 return Name == that.Name && TypeParams == that.TypeParams && 206 IntParams == that.IntParams; 207 } 208 bool operator!=(const KeyTy &that) const { return !this->operator==(that); } 209 }; 210 211 static inline TargetExtType *getEmptyKey() { 212 return DenseMapInfo<TargetExtType *>::getEmptyKey(); 213 } 214 215 static inline TargetExtType *getTombstoneKey() { 216 return DenseMapInfo<TargetExtType *>::getTombstoneKey(); 217 } 218 219 static unsigned getHashValue(const KeyTy &Key) { 220 return hash_combine(Key.Name, hash_combine_range(Key.TypeParams), 221 hash_combine_range(Key.IntParams)); 222 } 223 224 static unsigned getHashValue(const TargetExtType *FT) { 225 return getHashValue(KeyTy(FT)); 226 } 227 228 static bool isEqual(const KeyTy &LHS, const TargetExtType *RHS) { 229 if (RHS == getEmptyKey() || RHS == getTombstoneKey()) 230 return false; 231 return LHS == KeyTy(RHS); 232 } 233 234 static bool isEqual(const TargetExtType *LHS, const TargetExtType *RHS) { 235 return LHS == RHS; 236 } 237 }; 238 239 /// Structure for hashing arbitrary MDNode operands. 240 class MDNodeOpsKey { 241 ArrayRef<Metadata *> RawOps; 242 ArrayRef<MDOperand> Ops; 243 unsigned Hash; 244 245 protected: 246 MDNodeOpsKey(ArrayRef<Metadata *> Ops) 247 : RawOps(Ops), Hash(calculateHash(Ops)) {} 248 249 template <class NodeTy> 250 MDNodeOpsKey(const NodeTy *N, unsigned Offset = 0) 251 : Ops(N->op_begin() + Offset, N->op_end()), Hash(N->getHash()) {} 252 253 template <class NodeTy> 254 bool compareOps(const NodeTy *RHS, unsigned Offset = 0) const { 255 if (getHash() != RHS->getHash()) 256 return false; 257 258 assert((RawOps.empty() || Ops.empty()) && "Two sets of operands?"); 259 return RawOps.empty() ? compareOps(Ops, RHS, Offset) 260 : compareOps(RawOps, RHS, Offset); 261 } 262 263 static unsigned calculateHash(MDNode *N, unsigned Offset = 0); 264 265 private: 266 template <class T> 267 static bool compareOps(ArrayRef<T> Ops, const MDNode *RHS, unsigned Offset) { 268 if (Ops.size() != RHS->getNumOperands() - Offset) 269 return false; 270 return std::equal(Ops.begin(), Ops.end(), RHS->op_begin() + Offset); 271 } 272 273 static unsigned calculateHash(ArrayRef<Metadata *> Ops); 274 275 public: 276 unsigned getHash() const { return Hash; } 277 }; 278 279 template <class NodeTy> struct MDNodeKeyImpl; 280 281 /// Configuration point for MDNodeInfo::isEqual(). 282 template <class NodeTy> struct MDNodeSubsetEqualImpl { 283 using KeyTy = MDNodeKeyImpl<NodeTy>; 284 285 static bool isSubsetEqual(const KeyTy &LHS, const NodeTy *RHS) { 286 return false; 287 } 288 289 static bool isSubsetEqual(const NodeTy *LHS, const NodeTy *RHS) { 290 return false; 291 } 292 }; 293 294 /// DenseMapInfo for MDTuple. 295 /// 296 /// Note that we don't need the is-function-local bit, since that's implicit in 297 /// the operands. 298 template <> struct MDNodeKeyImpl<MDTuple> : MDNodeOpsKey { 299 MDNodeKeyImpl(ArrayRef<Metadata *> Ops) : MDNodeOpsKey(Ops) {} 300 MDNodeKeyImpl(const MDTuple *N) : MDNodeOpsKey(N) {} 301 302 bool isKeyOf(const MDTuple *RHS) const { return compareOps(RHS); } 303 304 unsigned getHashValue() const { return getHash(); } 305 306 static unsigned calculateHash(MDTuple *N) { 307 return MDNodeOpsKey::calculateHash(N); 308 } 309 }; 310 311 /// DenseMapInfo for DILocation. 312 template <> struct MDNodeKeyImpl<DILocation> { 313 Metadata *Scope; 314 Metadata *InlinedAt; 315 #ifdef EXPERIMENTAL_KEY_INSTRUCTIONS 316 uint64_t AtomGroup : 61; 317 uint64_t AtomRank : 3; 318 #endif 319 unsigned Line; 320 uint16_t Column; 321 bool ImplicitCode; 322 323 MDNodeKeyImpl(unsigned Line, uint16_t Column, Metadata *Scope, 324 Metadata *InlinedAt, bool ImplicitCode, uint64_t AtomGroup, 325 uint8_t AtomRank) 326 : Scope(Scope), InlinedAt(InlinedAt), 327 #ifdef EXPERIMENTAL_KEY_INSTRUCTIONS 328 AtomGroup(AtomGroup), AtomRank(AtomRank), 329 #endif 330 Line(Line), Column(Column), ImplicitCode(ImplicitCode) { 331 } 332 333 MDNodeKeyImpl(const DILocation *L) 334 : Scope(L->getRawScope()), InlinedAt(L->getRawInlinedAt()), 335 #ifdef EXPERIMENTAL_KEY_INSTRUCTIONS 336 AtomGroup(L->getAtomGroup()), AtomRank(L->getAtomRank()), 337 #endif 338 Line(L->getLine()), Column(L->getColumn()), 339 ImplicitCode(L->isImplicitCode()) { 340 } 341 342 bool isKeyOf(const DILocation *RHS) const { 343 return Line == RHS->getLine() && Column == RHS->getColumn() && 344 Scope == RHS->getRawScope() && InlinedAt == RHS->getRawInlinedAt() && 345 ImplicitCode == RHS->isImplicitCode() 346 #ifdef EXPERIMENTAL_KEY_INSTRUCTIONS 347 && AtomGroup == RHS->getAtomGroup() && 348 AtomRank == RHS->getAtomRank(); 349 #else 350 ; 351 #endif 352 } 353 354 unsigned getHashValue() const { 355 #ifdef EXPERIMENTAL_KEY_INSTRUCTIONS 356 // Hashing AtomGroup and AtomRank substantially impacts performance whether 357 // Key Instructions is enabled or not. We can't detect whether it's enabled 358 // here cheaply; avoiding hashing zero values is a good approximation. This 359 // affects Key Instruction builds too, but any potential costs incurred by 360 // messing with the hash distribution* appear to still be massively 361 // outweighed by the overall compile time savings by performing this check. 362 // * (hash_combine(x) != hash_combine(x, 0)) 363 if (AtomGroup || AtomRank) 364 return hash_combine(Line, Column, Scope, InlinedAt, ImplicitCode, 365 AtomGroup, (uint8_t)AtomRank); 366 #endif 367 return hash_combine(Line, Column, Scope, InlinedAt, ImplicitCode); 368 } 369 }; 370 371 /// DenseMapInfo for GenericDINode. 372 template <> struct MDNodeKeyImpl<GenericDINode> : MDNodeOpsKey { 373 unsigned Tag; 374 MDString *Header; 375 376 MDNodeKeyImpl(unsigned Tag, MDString *Header, ArrayRef<Metadata *> DwarfOps) 377 : MDNodeOpsKey(DwarfOps), Tag(Tag), Header(Header) {} 378 MDNodeKeyImpl(const GenericDINode *N) 379 : MDNodeOpsKey(N, 1), Tag(N->getTag()), Header(N->getRawHeader()) {} 380 381 bool isKeyOf(const GenericDINode *RHS) const { 382 return Tag == RHS->getTag() && Header == RHS->getRawHeader() && 383 compareOps(RHS, 1); 384 } 385 386 unsigned getHashValue() const { return hash_combine(getHash(), Tag, Header); } 387 388 static unsigned calculateHash(GenericDINode *N) { 389 return MDNodeOpsKey::calculateHash(N, 1); 390 } 391 }; 392 393 template <> struct MDNodeKeyImpl<DISubrange> { 394 Metadata *CountNode; 395 Metadata *LowerBound; 396 Metadata *UpperBound; 397 Metadata *Stride; 398 399 MDNodeKeyImpl(Metadata *CountNode, Metadata *LowerBound, Metadata *UpperBound, 400 Metadata *Stride) 401 : CountNode(CountNode), LowerBound(LowerBound), UpperBound(UpperBound), 402 Stride(Stride) {} 403 MDNodeKeyImpl(const DISubrange *N) 404 : CountNode(N->getRawCountNode()), LowerBound(N->getRawLowerBound()), 405 UpperBound(N->getRawUpperBound()), Stride(N->getRawStride()) {} 406 407 bool isKeyOf(const DISubrange *RHS) const { 408 auto BoundsEqual = [=](Metadata *Node1, Metadata *Node2) -> bool { 409 if (Node1 == Node2) 410 return true; 411 412 ConstantAsMetadata *MD1 = dyn_cast_or_null<ConstantAsMetadata>(Node1); 413 ConstantAsMetadata *MD2 = dyn_cast_or_null<ConstantAsMetadata>(Node2); 414 if (MD1 && MD2) { 415 ConstantInt *CV1 = cast<ConstantInt>(MD1->getValue()); 416 ConstantInt *CV2 = cast<ConstantInt>(MD2->getValue()); 417 if (CV1->getSExtValue() == CV2->getSExtValue()) 418 return true; 419 } 420 return false; 421 }; 422 423 return BoundsEqual(CountNode, RHS->getRawCountNode()) && 424 BoundsEqual(LowerBound, RHS->getRawLowerBound()) && 425 BoundsEqual(UpperBound, RHS->getRawUpperBound()) && 426 BoundsEqual(Stride, RHS->getRawStride()); 427 } 428 429 unsigned getHashValue() const { 430 if (CountNode) 431 if (auto *MD = dyn_cast<ConstantAsMetadata>(CountNode)) 432 return hash_combine(cast<ConstantInt>(MD->getValue())->getSExtValue(), 433 LowerBound, UpperBound, Stride); 434 return hash_combine(CountNode, LowerBound, UpperBound, Stride); 435 } 436 }; 437 438 template <> struct MDNodeKeyImpl<DIGenericSubrange> { 439 Metadata *CountNode; 440 Metadata *LowerBound; 441 Metadata *UpperBound; 442 Metadata *Stride; 443 444 MDNodeKeyImpl(Metadata *CountNode, Metadata *LowerBound, Metadata *UpperBound, 445 Metadata *Stride) 446 : CountNode(CountNode), LowerBound(LowerBound), UpperBound(UpperBound), 447 Stride(Stride) {} 448 MDNodeKeyImpl(const DIGenericSubrange *N) 449 : CountNode(N->getRawCountNode()), LowerBound(N->getRawLowerBound()), 450 UpperBound(N->getRawUpperBound()), Stride(N->getRawStride()) {} 451 452 bool isKeyOf(const DIGenericSubrange *RHS) const { 453 return (CountNode == RHS->getRawCountNode()) && 454 (LowerBound == RHS->getRawLowerBound()) && 455 (UpperBound == RHS->getRawUpperBound()) && 456 (Stride == RHS->getRawStride()); 457 } 458 459 unsigned getHashValue() const { 460 auto *MD = dyn_cast_or_null<ConstantAsMetadata>(CountNode); 461 if (CountNode && MD) 462 return hash_combine(cast<ConstantInt>(MD->getValue())->getSExtValue(), 463 LowerBound, UpperBound, Stride); 464 return hash_combine(CountNode, LowerBound, UpperBound, Stride); 465 } 466 }; 467 468 template <> struct MDNodeKeyImpl<DIEnumerator> { 469 APInt Value; 470 MDString *Name; 471 bool IsUnsigned; 472 473 MDNodeKeyImpl(APInt Value, bool IsUnsigned, MDString *Name) 474 : Value(std::move(Value)), Name(Name), IsUnsigned(IsUnsigned) {} 475 MDNodeKeyImpl(int64_t Value, bool IsUnsigned, MDString *Name) 476 : Value(APInt(64, Value, !IsUnsigned)), Name(Name), 477 IsUnsigned(IsUnsigned) {} 478 MDNodeKeyImpl(const DIEnumerator *N) 479 : Value(N->getValue()), Name(N->getRawName()), 480 IsUnsigned(N->isUnsigned()) {} 481 482 bool isKeyOf(const DIEnumerator *RHS) const { 483 return Value.getBitWidth() == RHS->getValue().getBitWidth() && 484 Value == RHS->getValue() && IsUnsigned == RHS->isUnsigned() && 485 Name == RHS->getRawName(); 486 } 487 488 unsigned getHashValue() const { return hash_combine(Value, Name); } 489 }; 490 491 template <> struct MDNodeKeyImpl<DIBasicType> { 492 unsigned Tag; 493 MDString *Name; 494 Metadata *SizeInBits; 495 uint32_t AlignInBits; 496 unsigned Encoding; 497 uint32_t NumExtraInhabitants; 498 unsigned Flags; 499 500 MDNodeKeyImpl(unsigned Tag, MDString *Name, Metadata *SizeInBits, 501 uint32_t AlignInBits, unsigned Encoding, 502 uint32_t NumExtraInhabitants, unsigned Flags) 503 : Tag(Tag), Name(Name), SizeInBits(SizeInBits), AlignInBits(AlignInBits), 504 Encoding(Encoding), NumExtraInhabitants(NumExtraInhabitants), 505 Flags(Flags) {} 506 MDNodeKeyImpl(const DIBasicType *N) 507 : Tag(N->getTag()), Name(N->getRawName()), 508 SizeInBits(N->getRawSizeInBits()), AlignInBits(N->getAlignInBits()), 509 Encoding(N->getEncoding()), 510 NumExtraInhabitants(N->getNumExtraInhabitants()), Flags(N->getFlags()) { 511 } 512 513 bool isKeyOf(const DIBasicType *RHS) const { 514 return Tag == RHS->getTag() && Name == RHS->getRawName() && 515 SizeInBits == RHS->getRawSizeInBits() && 516 AlignInBits == RHS->getAlignInBits() && 517 Encoding == RHS->getEncoding() && 518 NumExtraInhabitants == RHS->getNumExtraInhabitants() && 519 Flags == RHS->getFlags(); 520 } 521 522 unsigned getHashValue() const { 523 return hash_combine(Tag, Name, SizeInBits, AlignInBits, Encoding); 524 } 525 }; 526 527 template <> struct MDNodeKeyImpl<DIFixedPointType> { 528 unsigned Tag; 529 MDString *Name; 530 Metadata *SizeInBits; 531 uint32_t AlignInBits; 532 unsigned Encoding; 533 unsigned Flags; 534 unsigned Kind; 535 int Factor; 536 APInt Numerator; 537 APInt Denominator; 538 539 MDNodeKeyImpl(unsigned Tag, MDString *Name, Metadata *SizeInBits, 540 uint32_t AlignInBits, unsigned Encoding, unsigned Flags, 541 unsigned Kind, int Factor, APInt Numerator, APInt Denominator) 542 : Tag(Tag), Name(Name), SizeInBits(SizeInBits), AlignInBits(AlignInBits), 543 Encoding(Encoding), Flags(Flags), Kind(Kind), Factor(Factor), 544 Numerator(Numerator), Denominator(Denominator) {} 545 MDNodeKeyImpl(const DIFixedPointType *N) 546 : Tag(N->getTag()), Name(N->getRawName()), 547 SizeInBits(N->getRawSizeInBits()), AlignInBits(N->getAlignInBits()), 548 Encoding(N->getEncoding()), Flags(N->getFlags()), Kind(N->getKind()), 549 Factor(N->getFactorRaw()), Numerator(N->getNumeratorRaw()), 550 Denominator(N->getDenominatorRaw()) {} 551 552 bool isKeyOf(const DIFixedPointType *RHS) const { 553 return Name == RHS->getRawName() && SizeInBits == RHS->getRawSizeInBits() && 554 AlignInBits == RHS->getAlignInBits() && Kind == RHS->getKind() && 555 (RHS->isRational() ? (Numerator == RHS->getNumerator() && 556 Denominator == RHS->getDenominator()) 557 : Factor == RHS->getFactor()); 558 } 559 560 unsigned getHashValue() const { 561 return hash_combine(Name, Flags, Kind, Factor, Numerator, Denominator); 562 } 563 }; 564 565 template <> struct MDNodeKeyImpl<DIStringType> { 566 unsigned Tag; 567 MDString *Name; 568 Metadata *StringLength; 569 Metadata *StringLengthExp; 570 Metadata *StringLocationExp; 571 Metadata *SizeInBits; 572 uint32_t AlignInBits; 573 unsigned Encoding; 574 575 MDNodeKeyImpl(unsigned Tag, MDString *Name, Metadata *StringLength, 576 Metadata *StringLengthExp, Metadata *StringLocationExp, 577 Metadata *SizeInBits, uint32_t AlignInBits, unsigned Encoding) 578 : Tag(Tag), Name(Name), StringLength(StringLength), 579 StringLengthExp(StringLengthExp), StringLocationExp(StringLocationExp), 580 SizeInBits(SizeInBits), AlignInBits(AlignInBits), Encoding(Encoding) {} 581 MDNodeKeyImpl(const DIStringType *N) 582 : Tag(N->getTag()), Name(N->getRawName()), 583 StringLength(N->getRawStringLength()), 584 StringLengthExp(N->getRawStringLengthExp()), 585 StringLocationExp(N->getRawStringLocationExp()), 586 SizeInBits(N->getRawSizeInBits()), AlignInBits(N->getAlignInBits()), 587 Encoding(N->getEncoding()) {} 588 589 bool isKeyOf(const DIStringType *RHS) const { 590 return Tag == RHS->getTag() && Name == RHS->getRawName() && 591 StringLength == RHS->getRawStringLength() && 592 StringLengthExp == RHS->getRawStringLengthExp() && 593 StringLocationExp == RHS->getRawStringLocationExp() && 594 SizeInBits == RHS->getRawSizeInBits() && 595 AlignInBits == RHS->getAlignInBits() && 596 Encoding == RHS->getEncoding(); 597 } 598 unsigned getHashValue() const { 599 // Intentionally computes the hash on a subset of the operands for 600 // performance reason. The subset has to be significant enough to avoid 601 // collision "most of the time". There is no correctness issue in case of 602 // collision because of the full check above. 603 return hash_combine(Tag, Name, StringLength, Encoding); 604 } 605 }; 606 607 template <> struct MDNodeKeyImpl<DIDerivedType> { 608 unsigned Tag; 609 MDString *Name; 610 Metadata *File; 611 unsigned Line; 612 Metadata *Scope; 613 Metadata *BaseType; 614 Metadata *SizeInBits; 615 Metadata *OffsetInBits; 616 uint32_t AlignInBits; 617 std::optional<unsigned> DWARFAddressSpace; 618 std::optional<DIDerivedType::PtrAuthData> PtrAuthData; 619 unsigned Flags; 620 Metadata *ExtraData; 621 Metadata *Annotations; 622 623 MDNodeKeyImpl(unsigned Tag, MDString *Name, Metadata *File, unsigned Line, 624 Metadata *Scope, Metadata *BaseType, Metadata *SizeInBits, 625 uint32_t AlignInBits, Metadata *OffsetInBits, 626 std::optional<unsigned> DWARFAddressSpace, 627 std::optional<DIDerivedType::PtrAuthData> PtrAuthData, 628 unsigned Flags, Metadata *ExtraData, Metadata *Annotations) 629 : Tag(Tag), Name(Name), File(File), Line(Line), Scope(Scope), 630 BaseType(BaseType), SizeInBits(SizeInBits), OffsetInBits(OffsetInBits), 631 AlignInBits(AlignInBits), DWARFAddressSpace(DWARFAddressSpace), 632 PtrAuthData(PtrAuthData), Flags(Flags), ExtraData(ExtraData), 633 Annotations(Annotations) {} 634 MDNodeKeyImpl(const DIDerivedType *N) 635 : Tag(N->getTag()), Name(N->getRawName()), File(N->getRawFile()), 636 Line(N->getLine()), Scope(N->getRawScope()), 637 BaseType(N->getRawBaseType()), SizeInBits(N->getRawSizeInBits()), 638 OffsetInBits(N->getRawOffsetInBits()), AlignInBits(N->getAlignInBits()), 639 DWARFAddressSpace(N->getDWARFAddressSpace()), 640 PtrAuthData(N->getPtrAuthData()), Flags(N->getFlags()), 641 ExtraData(N->getRawExtraData()), Annotations(N->getRawAnnotations()) {} 642 643 bool isKeyOf(const DIDerivedType *RHS) const { 644 return Tag == RHS->getTag() && Name == RHS->getRawName() && 645 File == RHS->getRawFile() && Line == RHS->getLine() && 646 Scope == RHS->getRawScope() && BaseType == RHS->getRawBaseType() && 647 SizeInBits == RHS->getRawSizeInBits() && 648 AlignInBits == RHS->getAlignInBits() && 649 OffsetInBits == RHS->getRawOffsetInBits() && 650 DWARFAddressSpace == RHS->getDWARFAddressSpace() && 651 PtrAuthData == RHS->getPtrAuthData() && Flags == RHS->getFlags() && 652 ExtraData == RHS->getRawExtraData() && 653 Annotations == RHS->getRawAnnotations(); 654 } 655 656 unsigned getHashValue() const { 657 // If this is a member inside an ODR type, only hash the type and the name. 658 // Otherwise the hash will be stronger than 659 // MDNodeSubsetEqualImpl::isODRMember(). 660 if (Tag == dwarf::DW_TAG_member && Name) 661 if (auto *CT = dyn_cast_or_null<DICompositeType>(Scope)) 662 if (CT->getRawIdentifier()) 663 return hash_combine(Name, Scope); 664 665 // Intentionally computes the hash on a subset of the operands for 666 // performance reason. The subset has to be significant enough to avoid 667 // collision "most of the time". There is no correctness issue in case of 668 // collision because of the full check above. 669 return hash_combine(Tag, Name, File, Line, Scope, BaseType, Flags); 670 } 671 }; 672 673 template <> struct MDNodeKeyImpl<DISubrangeType> { 674 MDString *Name; 675 Metadata *File; 676 unsigned Line; 677 Metadata *Scope; 678 Metadata *SizeInBits; 679 uint32_t AlignInBits; 680 unsigned Flags; 681 Metadata *BaseType; 682 Metadata *LowerBound; 683 Metadata *UpperBound; 684 Metadata *Stride; 685 Metadata *Bias; 686 687 MDNodeKeyImpl(MDString *Name, Metadata *File, unsigned Line, Metadata *Scope, 688 Metadata *SizeInBits, uint32_t AlignInBits, unsigned Flags, 689 Metadata *BaseType, Metadata *LowerBound, Metadata *UpperBound, 690 Metadata *Stride, Metadata *Bias) 691 : Name(Name), File(File), Line(Line), Scope(Scope), 692 SizeInBits(SizeInBits), AlignInBits(AlignInBits), Flags(Flags), 693 BaseType(BaseType), LowerBound(LowerBound), UpperBound(UpperBound), 694 Stride(Stride), Bias(Bias) {} 695 MDNodeKeyImpl(const DISubrangeType *N) 696 : Name(N->getRawName()), File(N->getRawFile()), Line(N->getLine()), 697 Scope(N->getRawScope()), SizeInBits(N->getRawSizeInBits()), 698 AlignInBits(N->getAlignInBits()), Flags(N->getFlags()), 699 BaseType(N->getRawBaseType()), LowerBound(N->getRawLowerBound()), 700 UpperBound(N->getRawUpperBound()), Stride(N->getRawStride()), 701 Bias(N->getRawBias()) {} 702 703 bool isKeyOf(const DISubrangeType *RHS) const { 704 auto BoundsEqual = [=](Metadata *Node1, Metadata *Node2) -> bool { 705 if (Node1 == Node2) 706 return true; 707 708 ConstantAsMetadata *MD1 = dyn_cast_or_null<ConstantAsMetadata>(Node1); 709 ConstantAsMetadata *MD2 = dyn_cast_or_null<ConstantAsMetadata>(Node2); 710 if (MD1 && MD2) { 711 ConstantInt *CV1 = cast<ConstantInt>(MD1->getValue()); 712 ConstantInt *CV2 = cast<ConstantInt>(MD2->getValue()); 713 if (CV1->getSExtValue() == CV2->getSExtValue()) 714 return true; 715 } 716 return false; 717 }; 718 719 return Name == RHS->getRawName() && File == RHS->getRawFile() && 720 Line == RHS->getLine() && Scope == RHS->getRawScope() && 721 SizeInBits == RHS->getRawSizeInBits() && 722 AlignInBits == RHS->getAlignInBits() && Flags == RHS->getFlags() && 723 BaseType == RHS->getRawBaseType() && 724 BoundsEqual(LowerBound, RHS->getRawLowerBound()) && 725 BoundsEqual(UpperBound, RHS->getRawUpperBound()) && 726 BoundsEqual(Stride, RHS->getRawStride()) && 727 BoundsEqual(Bias, RHS->getRawBias()); 728 } 729 730 unsigned getHashValue() const { 731 unsigned val = 0; 732 auto HashBound = [&](Metadata *Node) -> void { 733 ConstantAsMetadata *MD = dyn_cast_or_null<ConstantAsMetadata>(Node); 734 if (MD) { 735 ConstantInt *CV = cast<ConstantInt>(MD->getValue()); 736 val = hash_combine(val, CV->getSExtValue()); 737 } else { 738 val = hash_combine(val, Node); 739 } 740 }; 741 742 HashBound(LowerBound); 743 HashBound(UpperBound); 744 HashBound(Stride); 745 HashBound(Bias); 746 747 return hash_combine(val, Name, File, Line, Scope, BaseType, Flags); 748 } 749 }; 750 751 template <> struct MDNodeSubsetEqualImpl<DIDerivedType> { 752 using KeyTy = MDNodeKeyImpl<DIDerivedType>; 753 754 static bool isSubsetEqual(const KeyTy &LHS, const DIDerivedType *RHS) { 755 return isODRMember(LHS.Tag, LHS.Scope, LHS.Name, RHS); 756 } 757 758 static bool isSubsetEqual(const DIDerivedType *LHS, 759 const DIDerivedType *RHS) { 760 return isODRMember(LHS->getTag(), LHS->getRawScope(), LHS->getRawName(), 761 RHS); 762 } 763 764 /// Subprograms compare equal if they declare the same function in an ODR 765 /// type. 766 static bool isODRMember(unsigned Tag, const Metadata *Scope, 767 const MDString *Name, const DIDerivedType *RHS) { 768 // Check whether the LHS is eligible. 769 if (Tag != dwarf::DW_TAG_member || !Name) 770 return false; 771 772 auto *CT = dyn_cast_or_null<DICompositeType>(Scope); 773 if (!CT || !CT->getRawIdentifier()) 774 return false; 775 776 // Compare to the RHS. 777 return Tag == RHS->getTag() && Name == RHS->getRawName() && 778 Scope == RHS->getRawScope(); 779 } 780 }; 781 782 template <> struct MDNodeKeyImpl<DICompositeType> { 783 unsigned Tag; 784 MDString *Name; 785 Metadata *File; 786 unsigned Line; 787 Metadata *Scope; 788 Metadata *BaseType; 789 Metadata *SizeInBits; 790 Metadata *OffsetInBits; 791 uint32_t AlignInBits; 792 unsigned Flags; 793 Metadata *Elements; 794 unsigned RuntimeLang; 795 Metadata *VTableHolder; 796 Metadata *TemplateParams; 797 MDString *Identifier; 798 Metadata *Discriminator; 799 Metadata *DataLocation; 800 Metadata *Associated; 801 Metadata *Allocated; 802 Metadata *Rank; 803 Metadata *Annotations; 804 Metadata *Specification; 805 uint32_t NumExtraInhabitants; 806 Metadata *BitStride; 807 808 MDNodeKeyImpl(unsigned Tag, MDString *Name, Metadata *File, unsigned Line, 809 Metadata *Scope, Metadata *BaseType, Metadata *SizeInBits, 810 uint32_t AlignInBits, Metadata *OffsetInBits, unsigned Flags, 811 Metadata *Elements, unsigned RuntimeLang, 812 Metadata *VTableHolder, Metadata *TemplateParams, 813 MDString *Identifier, Metadata *Discriminator, 814 Metadata *DataLocation, Metadata *Associated, 815 Metadata *Allocated, Metadata *Rank, Metadata *Annotations, 816 Metadata *Specification, uint32_t NumExtraInhabitants, 817 Metadata *BitStride) 818 : Tag(Tag), Name(Name), File(File), Line(Line), Scope(Scope), 819 BaseType(BaseType), SizeInBits(SizeInBits), OffsetInBits(OffsetInBits), 820 AlignInBits(AlignInBits), Flags(Flags), Elements(Elements), 821 RuntimeLang(RuntimeLang), VTableHolder(VTableHolder), 822 TemplateParams(TemplateParams), Identifier(Identifier), 823 Discriminator(Discriminator), DataLocation(DataLocation), 824 Associated(Associated), Allocated(Allocated), Rank(Rank), 825 Annotations(Annotations), Specification(Specification), 826 NumExtraInhabitants(NumExtraInhabitants), BitStride(BitStride) {} 827 MDNodeKeyImpl(const DICompositeType *N) 828 : Tag(N->getTag()), Name(N->getRawName()), File(N->getRawFile()), 829 Line(N->getLine()), Scope(N->getRawScope()), 830 BaseType(N->getRawBaseType()), SizeInBits(N->getRawSizeInBits()), 831 OffsetInBits(N->getRawOffsetInBits()), AlignInBits(N->getAlignInBits()), 832 Flags(N->getFlags()), Elements(N->getRawElements()), 833 RuntimeLang(N->getRuntimeLang()), VTableHolder(N->getRawVTableHolder()), 834 TemplateParams(N->getRawTemplateParams()), 835 Identifier(N->getRawIdentifier()), 836 Discriminator(N->getRawDiscriminator()), 837 DataLocation(N->getRawDataLocation()), 838 Associated(N->getRawAssociated()), Allocated(N->getRawAllocated()), 839 Rank(N->getRawRank()), Annotations(N->getRawAnnotations()), 840 Specification(N->getSpecification()), 841 NumExtraInhabitants(N->getNumExtraInhabitants()), 842 BitStride(N->getRawBitStride()) {} 843 844 bool isKeyOf(const DICompositeType *RHS) const { 845 return Tag == RHS->getTag() && Name == RHS->getRawName() && 846 File == RHS->getRawFile() && Line == RHS->getLine() && 847 Scope == RHS->getRawScope() && BaseType == RHS->getRawBaseType() && 848 SizeInBits == RHS->getRawSizeInBits() && 849 AlignInBits == RHS->getAlignInBits() && 850 OffsetInBits == RHS->getRawOffsetInBits() && 851 Flags == RHS->getFlags() && Elements == RHS->getRawElements() && 852 RuntimeLang == RHS->getRuntimeLang() && 853 VTableHolder == RHS->getRawVTableHolder() && 854 TemplateParams == RHS->getRawTemplateParams() && 855 Identifier == RHS->getRawIdentifier() && 856 Discriminator == RHS->getRawDiscriminator() && 857 DataLocation == RHS->getRawDataLocation() && 858 Associated == RHS->getRawAssociated() && 859 Allocated == RHS->getRawAllocated() && Rank == RHS->getRawRank() && 860 Annotations == RHS->getRawAnnotations() && 861 Specification == RHS->getSpecification() && 862 NumExtraInhabitants == RHS->getNumExtraInhabitants() && 863 BitStride == RHS->getRawBitStride(); 864 } 865 866 unsigned getHashValue() const { 867 // Intentionally computes the hash on a subset of the operands for 868 // performance reason. The subset has to be significant enough to avoid 869 // collision "most of the time". There is no correctness issue in case of 870 // collision because of the full check above. 871 return hash_combine(Name, File, Line, BaseType, Scope, Elements, 872 TemplateParams, Annotations); 873 } 874 }; 875 876 template <> struct MDNodeKeyImpl<DISubroutineType> { 877 unsigned Flags; 878 uint8_t CC; 879 Metadata *TypeArray; 880 881 MDNodeKeyImpl(unsigned Flags, uint8_t CC, Metadata *TypeArray) 882 : Flags(Flags), CC(CC), TypeArray(TypeArray) {} 883 MDNodeKeyImpl(const DISubroutineType *N) 884 : Flags(N->getFlags()), CC(N->getCC()), TypeArray(N->getRawTypeArray()) {} 885 886 bool isKeyOf(const DISubroutineType *RHS) const { 887 return Flags == RHS->getFlags() && CC == RHS->getCC() && 888 TypeArray == RHS->getRawTypeArray(); 889 } 890 891 unsigned getHashValue() const { return hash_combine(Flags, CC, TypeArray); } 892 }; 893 894 template <> struct MDNodeKeyImpl<DIFile> { 895 MDString *Filename; 896 MDString *Directory; 897 std::optional<DIFile::ChecksumInfo<MDString *>> Checksum; 898 MDString *Source; 899 900 MDNodeKeyImpl(MDString *Filename, MDString *Directory, 901 std::optional<DIFile::ChecksumInfo<MDString *>> Checksum, 902 MDString *Source) 903 : Filename(Filename), Directory(Directory), Checksum(Checksum), 904 Source(Source) {} 905 MDNodeKeyImpl(const DIFile *N) 906 : Filename(N->getRawFilename()), Directory(N->getRawDirectory()), 907 Checksum(N->getRawChecksum()), Source(N->getRawSource()) {} 908 909 bool isKeyOf(const DIFile *RHS) const { 910 return Filename == RHS->getRawFilename() && 911 Directory == RHS->getRawDirectory() && 912 Checksum == RHS->getRawChecksum() && Source == RHS->getRawSource(); 913 } 914 915 unsigned getHashValue() const { 916 return hash_combine(Filename, Directory, Checksum ? Checksum->Kind : 0, 917 Checksum ? Checksum->Value : nullptr, Source); 918 } 919 }; 920 921 template <> struct MDNodeKeyImpl<DISubprogram> { 922 Metadata *Scope; 923 MDString *Name; 924 MDString *LinkageName; 925 Metadata *File; 926 unsigned Line; 927 unsigned ScopeLine; 928 Metadata *Type; 929 Metadata *ContainingType; 930 unsigned VirtualIndex; 931 int ThisAdjustment; 932 unsigned Flags; 933 unsigned SPFlags; 934 Metadata *Unit; 935 Metadata *TemplateParams; 936 Metadata *Declaration; 937 Metadata *RetainedNodes; 938 Metadata *ThrownTypes; 939 Metadata *Annotations; 940 MDString *TargetFuncName; 941 bool UsesKeyInstructions; 942 943 MDNodeKeyImpl(Metadata *Scope, MDString *Name, MDString *LinkageName, 944 Metadata *File, unsigned Line, Metadata *Type, 945 unsigned ScopeLine, Metadata *ContainingType, 946 unsigned VirtualIndex, int ThisAdjustment, unsigned Flags, 947 unsigned SPFlags, Metadata *Unit, Metadata *TemplateParams, 948 Metadata *Declaration, Metadata *RetainedNodes, 949 Metadata *ThrownTypes, Metadata *Annotations, 950 MDString *TargetFuncName, bool UsesKeyInstructions) 951 : Scope(Scope), Name(Name), LinkageName(LinkageName), File(File), 952 Line(Line), ScopeLine(ScopeLine), Type(Type), 953 ContainingType(ContainingType), VirtualIndex(VirtualIndex), 954 ThisAdjustment(ThisAdjustment), Flags(Flags), SPFlags(SPFlags), 955 Unit(Unit), TemplateParams(TemplateParams), Declaration(Declaration), 956 RetainedNodes(RetainedNodes), ThrownTypes(ThrownTypes), 957 Annotations(Annotations), TargetFuncName(TargetFuncName), 958 UsesKeyInstructions(UsesKeyInstructions) {} 959 MDNodeKeyImpl(const DISubprogram *N) 960 : Scope(N->getRawScope()), Name(N->getRawName()), 961 LinkageName(N->getRawLinkageName()), File(N->getRawFile()), 962 Line(N->getLine()), ScopeLine(N->getScopeLine()), Type(N->getRawType()), 963 ContainingType(N->getRawContainingType()), 964 VirtualIndex(N->getVirtualIndex()), 965 ThisAdjustment(N->getThisAdjustment()), Flags(N->getFlags()), 966 SPFlags(N->getSPFlags()), Unit(N->getRawUnit()), 967 TemplateParams(N->getRawTemplateParams()), 968 Declaration(N->getRawDeclaration()), 969 RetainedNodes(N->getRawRetainedNodes()), 970 ThrownTypes(N->getRawThrownTypes()), 971 Annotations(N->getRawAnnotations()), 972 TargetFuncName(N->getRawTargetFuncName()), 973 UsesKeyInstructions(N->getKeyInstructionsEnabled()) {} 974 975 bool isKeyOf(const DISubprogram *RHS) const { 976 return Scope == RHS->getRawScope() && Name == RHS->getRawName() && 977 LinkageName == RHS->getRawLinkageName() && 978 File == RHS->getRawFile() && Line == RHS->getLine() && 979 Type == RHS->getRawType() && ScopeLine == RHS->getScopeLine() && 980 ContainingType == RHS->getRawContainingType() && 981 VirtualIndex == RHS->getVirtualIndex() && 982 ThisAdjustment == RHS->getThisAdjustment() && 983 Flags == RHS->getFlags() && SPFlags == RHS->getSPFlags() && 984 Unit == RHS->getUnit() && 985 TemplateParams == RHS->getRawTemplateParams() && 986 Declaration == RHS->getRawDeclaration() && 987 RetainedNodes == RHS->getRawRetainedNodes() && 988 ThrownTypes == RHS->getRawThrownTypes() && 989 Annotations == RHS->getRawAnnotations() && 990 TargetFuncName == RHS->getRawTargetFuncName() && 991 UsesKeyInstructions == RHS->getKeyInstructionsEnabled(); 992 } 993 994 bool isDefinition() const { return SPFlags & DISubprogram::SPFlagDefinition; } 995 996 unsigned getHashValue() const { 997 // Use the Scope's linkage name instead of using the scope directly, as the 998 // scope may be a temporary one which can replaced, which would produce a 999 // different hash for the same DISubprogram. 1000 llvm::StringRef ScopeLinkageName; 1001 if (auto *CT = dyn_cast_or_null<DICompositeType>(Scope)) 1002 if (auto *ID = CT->getRawIdentifier()) 1003 ScopeLinkageName = ID->getString(); 1004 1005 // If this is a declaration inside an ODR type, only hash the type and the 1006 // name. Otherwise the hash will be stronger than 1007 // MDNodeSubsetEqualImpl::isDeclarationOfODRMember(). 1008 if (!isDefinition() && LinkageName && 1009 isa_and_nonnull<DICompositeType>(Scope)) 1010 return hash_combine(LinkageName, ScopeLinkageName); 1011 1012 // Intentionally computes the hash on a subset of the operands for 1013 // performance reason. The subset has to be significant enough to avoid 1014 // collision "most of the time". There is no correctness issue in case of 1015 // collision because of the full check above. 1016 return hash_combine(Name, ScopeLinkageName, File, Type, Line); 1017 } 1018 }; 1019 1020 template <> struct MDNodeSubsetEqualImpl<DISubprogram> { 1021 using KeyTy = MDNodeKeyImpl<DISubprogram>; 1022 1023 static bool isSubsetEqual(const KeyTy &LHS, const DISubprogram *RHS) { 1024 return isDeclarationOfODRMember(LHS.isDefinition(), LHS.Scope, 1025 LHS.LinkageName, LHS.TemplateParams, RHS); 1026 } 1027 1028 static bool isSubsetEqual(const DISubprogram *LHS, const DISubprogram *RHS) { 1029 return isDeclarationOfODRMember(LHS->isDefinition(), LHS->getRawScope(), 1030 LHS->getRawLinkageName(), 1031 LHS->getRawTemplateParams(), RHS); 1032 } 1033 1034 /// Subprograms compare equal if they declare the same function in an ODR 1035 /// type. 1036 static bool isDeclarationOfODRMember(bool IsDefinition, const Metadata *Scope, 1037 const MDString *LinkageName, 1038 const Metadata *TemplateParams, 1039 const DISubprogram *RHS) { 1040 // Check whether the LHS is eligible. 1041 if (IsDefinition || !Scope || !LinkageName) 1042 return false; 1043 1044 auto *CT = dyn_cast_or_null<DICompositeType>(Scope); 1045 if (!CT || !CT->getRawIdentifier()) 1046 return false; 1047 1048 // Compare to the RHS. 1049 // FIXME: We need to compare template parameters here to avoid incorrect 1050 // collisions in mapMetadata when RF_ReuseAndMutateDistinctMDs and a 1051 // ODR-DISubprogram has a non-ODR template parameter (i.e., a 1052 // DICompositeType that does not have an identifier). Eventually we should 1053 // decouple ODR logic from uniquing logic. 1054 return IsDefinition == RHS->isDefinition() && Scope == RHS->getRawScope() && 1055 LinkageName == RHS->getRawLinkageName() && 1056 TemplateParams == RHS->getRawTemplateParams(); 1057 } 1058 }; 1059 1060 template <> struct MDNodeKeyImpl<DILexicalBlock> { 1061 Metadata *Scope; 1062 Metadata *File; 1063 unsigned Line; 1064 unsigned Column; 1065 1066 MDNodeKeyImpl(Metadata *Scope, Metadata *File, unsigned Line, unsigned Column) 1067 : Scope(Scope), File(File), Line(Line), Column(Column) {} 1068 MDNodeKeyImpl(const DILexicalBlock *N) 1069 : Scope(N->getRawScope()), File(N->getRawFile()), Line(N->getLine()), 1070 Column(N->getColumn()) {} 1071 1072 bool isKeyOf(const DILexicalBlock *RHS) const { 1073 return Scope == RHS->getRawScope() && File == RHS->getRawFile() && 1074 Line == RHS->getLine() && Column == RHS->getColumn(); 1075 } 1076 1077 unsigned getHashValue() const { 1078 return hash_combine(Scope, File, Line, Column); 1079 } 1080 }; 1081 1082 template <> struct MDNodeKeyImpl<DILexicalBlockFile> { 1083 Metadata *Scope; 1084 Metadata *File; 1085 unsigned Discriminator; 1086 1087 MDNodeKeyImpl(Metadata *Scope, Metadata *File, unsigned Discriminator) 1088 : Scope(Scope), File(File), Discriminator(Discriminator) {} 1089 MDNodeKeyImpl(const DILexicalBlockFile *N) 1090 : Scope(N->getRawScope()), File(N->getRawFile()), 1091 Discriminator(N->getDiscriminator()) {} 1092 1093 bool isKeyOf(const DILexicalBlockFile *RHS) const { 1094 return Scope == RHS->getRawScope() && File == RHS->getRawFile() && 1095 Discriminator == RHS->getDiscriminator(); 1096 } 1097 1098 unsigned getHashValue() const { 1099 return hash_combine(Scope, File, Discriminator); 1100 } 1101 }; 1102 1103 template <> struct MDNodeKeyImpl<DINamespace> { 1104 Metadata *Scope; 1105 MDString *Name; 1106 bool ExportSymbols; 1107 1108 MDNodeKeyImpl(Metadata *Scope, MDString *Name, bool ExportSymbols) 1109 : Scope(Scope), Name(Name), ExportSymbols(ExportSymbols) {} 1110 MDNodeKeyImpl(const DINamespace *N) 1111 : Scope(N->getRawScope()), Name(N->getRawName()), 1112 ExportSymbols(N->getExportSymbols()) {} 1113 1114 bool isKeyOf(const DINamespace *RHS) const { 1115 return Scope == RHS->getRawScope() && Name == RHS->getRawName() && 1116 ExportSymbols == RHS->getExportSymbols(); 1117 } 1118 1119 unsigned getHashValue() const { return hash_combine(Scope, Name); } 1120 }; 1121 1122 template <> struct MDNodeKeyImpl<DICommonBlock> { 1123 Metadata *Scope; 1124 Metadata *Decl; 1125 MDString *Name; 1126 Metadata *File; 1127 unsigned LineNo; 1128 1129 MDNodeKeyImpl(Metadata *Scope, Metadata *Decl, MDString *Name, Metadata *File, 1130 unsigned LineNo) 1131 : Scope(Scope), Decl(Decl), Name(Name), File(File), LineNo(LineNo) {} 1132 MDNodeKeyImpl(const DICommonBlock *N) 1133 : Scope(N->getRawScope()), Decl(N->getRawDecl()), Name(N->getRawName()), 1134 File(N->getRawFile()), LineNo(N->getLineNo()) {} 1135 1136 bool isKeyOf(const DICommonBlock *RHS) const { 1137 return Scope == RHS->getRawScope() && Decl == RHS->getRawDecl() && 1138 Name == RHS->getRawName() && File == RHS->getRawFile() && 1139 LineNo == RHS->getLineNo(); 1140 } 1141 1142 unsigned getHashValue() const { 1143 return hash_combine(Scope, Decl, Name, File, LineNo); 1144 } 1145 }; 1146 1147 template <> struct MDNodeKeyImpl<DIModule> { 1148 Metadata *File; 1149 Metadata *Scope; 1150 MDString *Name; 1151 MDString *ConfigurationMacros; 1152 MDString *IncludePath; 1153 MDString *APINotesFile; 1154 unsigned LineNo; 1155 bool IsDecl; 1156 1157 MDNodeKeyImpl(Metadata *File, Metadata *Scope, MDString *Name, 1158 MDString *ConfigurationMacros, MDString *IncludePath, 1159 MDString *APINotesFile, unsigned LineNo, bool IsDecl) 1160 : File(File), Scope(Scope), Name(Name), 1161 ConfigurationMacros(ConfigurationMacros), IncludePath(IncludePath), 1162 APINotesFile(APINotesFile), LineNo(LineNo), IsDecl(IsDecl) {} 1163 MDNodeKeyImpl(const DIModule *N) 1164 : File(N->getRawFile()), Scope(N->getRawScope()), Name(N->getRawName()), 1165 ConfigurationMacros(N->getRawConfigurationMacros()), 1166 IncludePath(N->getRawIncludePath()), 1167 APINotesFile(N->getRawAPINotesFile()), LineNo(N->getLineNo()), 1168 IsDecl(N->getIsDecl()) {} 1169 1170 bool isKeyOf(const DIModule *RHS) const { 1171 return Scope == RHS->getRawScope() && Name == RHS->getRawName() && 1172 ConfigurationMacros == RHS->getRawConfigurationMacros() && 1173 IncludePath == RHS->getRawIncludePath() && 1174 APINotesFile == RHS->getRawAPINotesFile() && 1175 File == RHS->getRawFile() && LineNo == RHS->getLineNo() && 1176 IsDecl == RHS->getIsDecl(); 1177 } 1178 1179 unsigned getHashValue() const { 1180 return hash_combine(Scope, Name, ConfigurationMacros, IncludePath); 1181 } 1182 }; 1183 1184 template <> struct MDNodeKeyImpl<DITemplateTypeParameter> { 1185 MDString *Name; 1186 Metadata *Type; 1187 bool IsDefault; 1188 1189 MDNodeKeyImpl(MDString *Name, Metadata *Type, bool IsDefault) 1190 : Name(Name), Type(Type), IsDefault(IsDefault) {} 1191 MDNodeKeyImpl(const DITemplateTypeParameter *N) 1192 : Name(N->getRawName()), Type(N->getRawType()), 1193 IsDefault(N->isDefault()) {} 1194 1195 bool isKeyOf(const DITemplateTypeParameter *RHS) const { 1196 return Name == RHS->getRawName() && Type == RHS->getRawType() && 1197 IsDefault == RHS->isDefault(); 1198 } 1199 1200 unsigned getHashValue() const { return hash_combine(Name, Type, IsDefault); } 1201 }; 1202 1203 template <> struct MDNodeKeyImpl<DITemplateValueParameter> { 1204 unsigned Tag; 1205 MDString *Name; 1206 Metadata *Type; 1207 bool IsDefault; 1208 Metadata *Value; 1209 1210 MDNodeKeyImpl(unsigned Tag, MDString *Name, Metadata *Type, bool IsDefault, 1211 Metadata *Value) 1212 : Tag(Tag), Name(Name), Type(Type), IsDefault(IsDefault), Value(Value) {} 1213 MDNodeKeyImpl(const DITemplateValueParameter *N) 1214 : Tag(N->getTag()), Name(N->getRawName()), Type(N->getRawType()), 1215 IsDefault(N->isDefault()), Value(N->getValue()) {} 1216 1217 bool isKeyOf(const DITemplateValueParameter *RHS) const { 1218 return Tag == RHS->getTag() && Name == RHS->getRawName() && 1219 Type == RHS->getRawType() && IsDefault == RHS->isDefault() && 1220 Value == RHS->getValue(); 1221 } 1222 1223 unsigned getHashValue() const { 1224 return hash_combine(Tag, Name, Type, IsDefault, Value); 1225 } 1226 }; 1227 1228 template <> struct MDNodeKeyImpl<DIGlobalVariable> { 1229 Metadata *Scope; 1230 MDString *Name; 1231 MDString *LinkageName; 1232 Metadata *File; 1233 unsigned Line; 1234 Metadata *Type; 1235 bool IsLocalToUnit; 1236 bool IsDefinition; 1237 Metadata *StaticDataMemberDeclaration; 1238 Metadata *TemplateParams; 1239 uint32_t AlignInBits; 1240 Metadata *Annotations; 1241 1242 MDNodeKeyImpl(Metadata *Scope, MDString *Name, MDString *LinkageName, 1243 Metadata *File, unsigned Line, Metadata *Type, 1244 bool IsLocalToUnit, bool IsDefinition, 1245 Metadata *StaticDataMemberDeclaration, Metadata *TemplateParams, 1246 uint32_t AlignInBits, Metadata *Annotations) 1247 : Scope(Scope), Name(Name), LinkageName(LinkageName), File(File), 1248 Line(Line), Type(Type), IsLocalToUnit(IsLocalToUnit), 1249 IsDefinition(IsDefinition), 1250 StaticDataMemberDeclaration(StaticDataMemberDeclaration), 1251 TemplateParams(TemplateParams), AlignInBits(AlignInBits), 1252 Annotations(Annotations) {} 1253 MDNodeKeyImpl(const DIGlobalVariable *N) 1254 : Scope(N->getRawScope()), Name(N->getRawName()), 1255 LinkageName(N->getRawLinkageName()), File(N->getRawFile()), 1256 Line(N->getLine()), Type(N->getRawType()), 1257 IsLocalToUnit(N->isLocalToUnit()), IsDefinition(N->isDefinition()), 1258 StaticDataMemberDeclaration(N->getRawStaticDataMemberDeclaration()), 1259 TemplateParams(N->getRawTemplateParams()), 1260 AlignInBits(N->getAlignInBits()), Annotations(N->getRawAnnotations()) {} 1261 1262 bool isKeyOf(const DIGlobalVariable *RHS) const { 1263 return Scope == RHS->getRawScope() && Name == RHS->getRawName() && 1264 LinkageName == RHS->getRawLinkageName() && 1265 File == RHS->getRawFile() && Line == RHS->getLine() && 1266 Type == RHS->getRawType() && IsLocalToUnit == RHS->isLocalToUnit() && 1267 IsDefinition == RHS->isDefinition() && 1268 StaticDataMemberDeclaration == 1269 RHS->getRawStaticDataMemberDeclaration() && 1270 TemplateParams == RHS->getRawTemplateParams() && 1271 AlignInBits == RHS->getAlignInBits() && 1272 Annotations == RHS->getRawAnnotations(); 1273 } 1274 1275 unsigned getHashValue() const { 1276 // We do not use AlignInBits in hashing function here on purpose: 1277 // in most cases this param for local variable is zero (for function param 1278 // it is always zero). This leads to lots of hash collisions and errors on 1279 // cases with lots of similar variables. 1280 // clang/test/CodeGen/debug-info-257-args.c is an example of this problem, 1281 // generated IR is random for each run and test fails with Align included. 1282 // TODO: make hashing work fine with such situations 1283 return hash_combine(Scope, Name, LinkageName, File, Line, Type, 1284 IsLocalToUnit, IsDefinition, /* AlignInBits, */ 1285 StaticDataMemberDeclaration, Annotations); 1286 } 1287 }; 1288 1289 template <> struct MDNodeKeyImpl<DILocalVariable> { 1290 Metadata *Scope; 1291 MDString *Name; 1292 Metadata *File; 1293 unsigned Line; 1294 Metadata *Type; 1295 unsigned Arg; 1296 unsigned Flags; 1297 uint32_t AlignInBits; 1298 Metadata *Annotations; 1299 1300 MDNodeKeyImpl(Metadata *Scope, MDString *Name, Metadata *File, unsigned Line, 1301 Metadata *Type, unsigned Arg, unsigned Flags, 1302 uint32_t AlignInBits, Metadata *Annotations) 1303 : Scope(Scope), Name(Name), File(File), Line(Line), Type(Type), Arg(Arg), 1304 Flags(Flags), AlignInBits(AlignInBits), Annotations(Annotations) {} 1305 MDNodeKeyImpl(const DILocalVariable *N) 1306 : Scope(N->getRawScope()), Name(N->getRawName()), File(N->getRawFile()), 1307 Line(N->getLine()), Type(N->getRawType()), Arg(N->getArg()), 1308 Flags(N->getFlags()), AlignInBits(N->getAlignInBits()), 1309 Annotations(N->getRawAnnotations()) {} 1310 1311 bool isKeyOf(const DILocalVariable *RHS) const { 1312 return Scope == RHS->getRawScope() && Name == RHS->getRawName() && 1313 File == RHS->getRawFile() && Line == RHS->getLine() && 1314 Type == RHS->getRawType() && Arg == RHS->getArg() && 1315 Flags == RHS->getFlags() && AlignInBits == RHS->getAlignInBits() && 1316 Annotations == RHS->getRawAnnotations(); 1317 } 1318 1319 unsigned getHashValue() const { 1320 // We do not use AlignInBits in hashing function here on purpose: 1321 // in most cases this param for local variable is zero (for function param 1322 // it is always zero). This leads to lots of hash collisions and errors on 1323 // cases with lots of similar variables. 1324 // clang/test/CodeGen/debug-info-257-args.c is an example of this problem, 1325 // generated IR is random for each run and test fails with Align included. 1326 // TODO: make hashing work fine with such situations 1327 return hash_combine(Scope, Name, File, Line, Type, Arg, Flags, Annotations); 1328 } 1329 }; 1330 1331 template <> struct MDNodeKeyImpl<DILabel> { 1332 Metadata *Scope; 1333 MDString *Name; 1334 Metadata *File; 1335 unsigned Line; 1336 unsigned Column; 1337 bool IsArtificial; 1338 std::optional<unsigned> CoroSuspendIdx; 1339 1340 MDNodeKeyImpl(Metadata *Scope, MDString *Name, Metadata *File, unsigned Line, 1341 unsigned Column, bool IsArtificial, 1342 std::optional<unsigned> CoroSuspendIdx) 1343 : Scope(Scope), Name(Name), File(File), Line(Line), Column(Column), 1344 IsArtificial(IsArtificial), CoroSuspendIdx(CoroSuspendIdx) {} 1345 MDNodeKeyImpl(const DILabel *N) 1346 : Scope(N->getRawScope()), Name(N->getRawName()), File(N->getRawFile()), 1347 Line(N->getLine()), Column(N->getColumn()), 1348 IsArtificial(N->isArtificial()), 1349 CoroSuspendIdx(N->getCoroSuspendIdx()) {} 1350 1351 bool isKeyOf(const DILabel *RHS) const { 1352 return Scope == RHS->getRawScope() && Name == RHS->getRawName() && 1353 File == RHS->getRawFile() && Line == RHS->getLine() && 1354 Column == RHS->getColumn() && IsArtificial == RHS->isArtificial() && 1355 CoroSuspendIdx == RHS->getCoroSuspendIdx(); 1356 } 1357 1358 /// Using name and line to get hash value. It should already be mostly unique. 1359 unsigned getHashValue() const { 1360 return hash_combine(Scope, Name, Line, Column, IsArtificial, 1361 CoroSuspendIdx); 1362 } 1363 }; 1364 1365 template <> struct MDNodeKeyImpl<DIExpression> { 1366 ArrayRef<uint64_t> Elements; 1367 1368 MDNodeKeyImpl(ArrayRef<uint64_t> Elements) : Elements(Elements) {} 1369 MDNodeKeyImpl(const DIExpression *N) : Elements(N->getElements()) {} 1370 1371 bool isKeyOf(const DIExpression *RHS) const { 1372 return Elements == RHS->getElements(); 1373 } 1374 1375 unsigned getHashValue() const { return hash_combine_range(Elements); } 1376 }; 1377 1378 template <> struct MDNodeKeyImpl<DIGlobalVariableExpression> { 1379 Metadata *Variable; 1380 Metadata *Expression; 1381 1382 MDNodeKeyImpl(Metadata *Variable, Metadata *Expression) 1383 : Variable(Variable), Expression(Expression) {} 1384 MDNodeKeyImpl(const DIGlobalVariableExpression *N) 1385 : Variable(N->getRawVariable()), Expression(N->getRawExpression()) {} 1386 1387 bool isKeyOf(const DIGlobalVariableExpression *RHS) const { 1388 return Variable == RHS->getRawVariable() && 1389 Expression == RHS->getRawExpression(); 1390 } 1391 1392 unsigned getHashValue() const { return hash_combine(Variable, Expression); } 1393 }; 1394 1395 template <> struct MDNodeKeyImpl<DIObjCProperty> { 1396 MDString *Name; 1397 Metadata *File; 1398 unsigned Line; 1399 MDString *GetterName; 1400 MDString *SetterName; 1401 unsigned Attributes; 1402 Metadata *Type; 1403 1404 MDNodeKeyImpl(MDString *Name, Metadata *File, unsigned Line, 1405 MDString *GetterName, MDString *SetterName, unsigned Attributes, 1406 Metadata *Type) 1407 : Name(Name), File(File), Line(Line), GetterName(GetterName), 1408 SetterName(SetterName), Attributes(Attributes), Type(Type) {} 1409 MDNodeKeyImpl(const DIObjCProperty *N) 1410 : Name(N->getRawName()), File(N->getRawFile()), Line(N->getLine()), 1411 GetterName(N->getRawGetterName()), SetterName(N->getRawSetterName()), 1412 Attributes(N->getAttributes()), Type(N->getRawType()) {} 1413 1414 bool isKeyOf(const DIObjCProperty *RHS) const { 1415 return Name == RHS->getRawName() && File == RHS->getRawFile() && 1416 Line == RHS->getLine() && GetterName == RHS->getRawGetterName() && 1417 SetterName == RHS->getRawSetterName() && 1418 Attributes == RHS->getAttributes() && Type == RHS->getRawType(); 1419 } 1420 1421 unsigned getHashValue() const { 1422 return hash_combine(Name, File, Line, GetterName, SetterName, Attributes, 1423 Type); 1424 } 1425 }; 1426 1427 template <> struct MDNodeKeyImpl<DIImportedEntity> { 1428 unsigned Tag; 1429 Metadata *Scope; 1430 Metadata *Entity; 1431 Metadata *File; 1432 unsigned Line; 1433 MDString *Name; 1434 Metadata *Elements; 1435 1436 MDNodeKeyImpl(unsigned Tag, Metadata *Scope, Metadata *Entity, Metadata *File, 1437 unsigned Line, MDString *Name, Metadata *Elements) 1438 : Tag(Tag), Scope(Scope), Entity(Entity), File(File), Line(Line), 1439 Name(Name), Elements(Elements) {} 1440 MDNodeKeyImpl(const DIImportedEntity *N) 1441 : Tag(N->getTag()), Scope(N->getRawScope()), Entity(N->getRawEntity()), 1442 File(N->getRawFile()), Line(N->getLine()), Name(N->getRawName()), 1443 Elements(N->getRawElements()) {} 1444 1445 bool isKeyOf(const DIImportedEntity *RHS) const { 1446 return Tag == RHS->getTag() && Scope == RHS->getRawScope() && 1447 Entity == RHS->getRawEntity() && File == RHS->getFile() && 1448 Line == RHS->getLine() && Name == RHS->getRawName() && 1449 Elements == RHS->getRawElements(); 1450 } 1451 1452 unsigned getHashValue() const { 1453 return hash_combine(Tag, Scope, Entity, File, Line, Name, Elements); 1454 } 1455 }; 1456 1457 template <> struct MDNodeKeyImpl<DIMacro> { 1458 unsigned MIType; 1459 unsigned Line; 1460 MDString *Name; 1461 MDString *Value; 1462 1463 MDNodeKeyImpl(unsigned MIType, unsigned Line, MDString *Name, MDString *Value) 1464 : MIType(MIType), Line(Line), Name(Name), Value(Value) {} 1465 MDNodeKeyImpl(const DIMacro *N) 1466 : MIType(N->getMacinfoType()), Line(N->getLine()), Name(N->getRawName()), 1467 Value(N->getRawValue()) {} 1468 1469 bool isKeyOf(const DIMacro *RHS) const { 1470 return MIType == RHS->getMacinfoType() && Line == RHS->getLine() && 1471 Name == RHS->getRawName() && Value == RHS->getRawValue(); 1472 } 1473 1474 unsigned getHashValue() const { 1475 return hash_combine(MIType, Line, Name, Value); 1476 } 1477 }; 1478 1479 template <> struct MDNodeKeyImpl<DIMacroFile> { 1480 unsigned MIType; 1481 unsigned Line; 1482 Metadata *File; 1483 Metadata *Elements; 1484 1485 MDNodeKeyImpl(unsigned MIType, unsigned Line, Metadata *File, 1486 Metadata *Elements) 1487 : MIType(MIType), Line(Line), File(File), Elements(Elements) {} 1488 MDNodeKeyImpl(const DIMacroFile *N) 1489 : MIType(N->getMacinfoType()), Line(N->getLine()), File(N->getRawFile()), 1490 Elements(N->getRawElements()) {} 1491 1492 bool isKeyOf(const DIMacroFile *RHS) const { 1493 return MIType == RHS->getMacinfoType() && Line == RHS->getLine() && 1494 File == RHS->getRawFile() && Elements == RHS->getRawElements(); 1495 } 1496 1497 unsigned getHashValue() const { 1498 return hash_combine(MIType, Line, File, Elements); 1499 } 1500 }; 1501 1502 // DIArgLists are not MDNodes, but we still want to unique them in a DenseSet 1503 // based on a hash of their arguments. 1504 struct DIArgListKeyInfo { 1505 ArrayRef<ValueAsMetadata *> Args; 1506 1507 DIArgListKeyInfo(ArrayRef<ValueAsMetadata *> Args) : Args(Args) {} 1508 DIArgListKeyInfo(const DIArgList *N) : Args(N->getArgs()) {} 1509 1510 bool isKeyOf(const DIArgList *RHS) const { return Args == RHS->getArgs(); } 1511 1512 unsigned getHashValue() const { return hash_combine_range(Args); } 1513 }; 1514 1515 /// DenseMapInfo for DIArgList. 1516 struct DIArgListInfo { 1517 using KeyTy = DIArgListKeyInfo; 1518 1519 static inline DIArgList *getEmptyKey() { 1520 return DenseMapInfo<DIArgList *>::getEmptyKey(); 1521 } 1522 1523 static inline DIArgList *getTombstoneKey() { 1524 return DenseMapInfo<DIArgList *>::getTombstoneKey(); 1525 } 1526 1527 static unsigned getHashValue(const KeyTy &Key) { return Key.getHashValue(); } 1528 1529 static unsigned getHashValue(const DIArgList *N) { 1530 return KeyTy(N).getHashValue(); 1531 } 1532 1533 static bool isEqual(const KeyTy &LHS, const DIArgList *RHS) { 1534 if (RHS == getEmptyKey() || RHS == getTombstoneKey()) 1535 return false; 1536 return LHS.isKeyOf(RHS); 1537 } 1538 1539 static bool isEqual(const DIArgList *LHS, const DIArgList *RHS) { 1540 return LHS == RHS; 1541 } 1542 }; 1543 1544 /// DenseMapInfo for MDNode subclasses. 1545 template <class NodeTy> struct MDNodeInfo { 1546 using KeyTy = MDNodeKeyImpl<NodeTy>; 1547 using SubsetEqualTy = MDNodeSubsetEqualImpl<NodeTy>; 1548 1549 static inline NodeTy *getEmptyKey() { 1550 return DenseMapInfo<NodeTy *>::getEmptyKey(); 1551 } 1552 1553 static inline NodeTy *getTombstoneKey() { 1554 return DenseMapInfo<NodeTy *>::getTombstoneKey(); 1555 } 1556 1557 static unsigned getHashValue(const KeyTy &Key) { return Key.getHashValue(); } 1558 1559 static unsigned getHashValue(const NodeTy *N) { 1560 return KeyTy(N).getHashValue(); 1561 } 1562 1563 static bool isEqual(const KeyTy &LHS, const NodeTy *RHS) { 1564 if (RHS == getEmptyKey() || RHS == getTombstoneKey()) 1565 return false; 1566 return SubsetEqualTy::isSubsetEqual(LHS, RHS) || LHS.isKeyOf(RHS); 1567 } 1568 1569 static bool isEqual(const NodeTy *LHS, const NodeTy *RHS) { 1570 if (LHS == RHS) 1571 return true; 1572 if (RHS == getEmptyKey() || RHS == getTombstoneKey()) 1573 return false; 1574 return SubsetEqualTy::isSubsetEqual(LHS, RHS); 1575 } 1576 }; 1577 1578 #define HANDLE_MDNODE_LEAF(CLASS) using CLASS##Info = MDNodeInfo<CLASS>; 1579 #include "llvm/IR/Metadata.def" 1580 1581 /// Multimap-like storage for metadata attachments. 1582 class MDAttachments { 1583 public: 1584 struct Attachment { 1585 unsigned MDKind; 1586 TrackingMDNodeRef Node; 1587 }; 1588 1589 private: 1590 SmallVector<Attachment, 1> Attachments; 1591 1592 public: 1593 bool empty() const { return Attachments.empty(); } 1594 size_t size() const { return Attachments.size(); } 1595 1596 /// Returns the first attachment with the given ID or nullptr if no such 1597 /// attachment exists. 1598 MDNode *lookup(unsigned ID) const; 1599 1600 /// Appends all attachments with the given ID to \c Result in insertion order. 1601 /// If the global has no attachments with the given ID, or if ID is invalid, 1602 /// leaves Result unchanged. 1603 void get(unsigned ID, SmallVectorImpl<MDNode *> &Result) const; 1604 1605 /// Appends all attachments for the global to \c Result, sorting by attachment 1606 /// ID. Attachments with the same ID appear in insertion order. This function 1607 /// does \em not clear \c Result. 1608 void getAll(SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const; 1609 1610 /// Set an attachment to a particular node. 1611 /// 1612 /// Set the \c ID attachment to \c MD, replacing the current attachments at \c 1613 /// ID (if anyway). 1614 void set(unsigned ID, MDNode *MD); 1615 1616 /// Adds an attachment to a particular node. 1617 void insert(unsigned ID, MDNode &MD); 1618 1619 /// Remove attachments with the given ID. 1620 /// 1621 /// Remove the attachments at \c ID, if any. 1622 bool erase(unsigned ID); 1623 1624 /// Erase matching attachments. 1625 /// 1626 /// Erases all attachments matching the \c shouldRemove predicate. 1627 template <class PredTy> void remove_if(PredTy shouldRemove) { 1628 llvm::erase_if(Attachments, shouldRemove); 1629 } 1630 }; 1631 1632 class LLVMContextImpl { 1633 public: 1634 /// OwnedModules - The set of modules instantiated in this context, and which 1635 /// will be automatically deleted if this context is deleted. 1636 SmallPtrSet<Module *, 4> OwnedModules; 1637 1638 /// MachineFunctionNums - Keep the next available unique number available for 1639 /// a MachineFunction in given module. Module must in OwnedModules. 1640 DenseMap<Module *, unsigned> MachineFunctionNums; 1641 1642 /// The main remark streamer used by all the other streamers (e.g. IR, MIR, 1643 /// frontends, etc.). This should only be used by the specific streamers, and 1644 /// never directly. 1645 std::unique_ptr<remarks::RemarkStreamer> MainRemarkStreamer; 1646 1647 std::unique_ptr<DiagnosticHandler> DiagHandler; 1648 bool RespectDiagnosticFilters = false; 1649 bool DiagnosticsHotnessRequested = false; 1650 /// The minimum hotness value a diagnostic needs in order to be included in 1651 /// optimization diagnostics. 1652 /// 1653 /// The threshold is an Optional value, which maps to one of the 3 states: 1654 /// 1). 0 => threshold disabled. All emarks will be printed. 1655 /// 2). positive int => manual threshold by user. Remarks with hotness exceed 1656 /// threshold will be printed. 1657 /// 3). None => 'auto' threshold by user. The actual value is not 1658 /// available at command line, but will be synced with 1659 /// hotness threhold from profile summary during 1660 /// compilation. 1661 /// 1662 /// State 1 and 2 are considered as terminal states. State transition is 1663 /// only allowed from 3 to 2, when the threshold is first synced with profile 1664 /// summary. This ensures that the threshold is set only once and stays 1665 /// constant. 1666 /// 1667 /// If threshold option is not specified, it is disabled (0) by default. 1668 std::optional<uint64_t> DiagnosticsHotnessThreshold = 0; 1669 1670 /// The percentage of difference between profiling branch weights and 1671 /// llvm.expect branch weights to tolerate when emiting MisExpect diagnostics 1672 std::optional<uint32_t> DiagnosticsMisExpectTolerance = 0; 1673 bool MisExpectWarningRequested = false; 1674 1675 /// The specialized remark streamer used by LLVM's OptimizationRemarkEmitter. 1676 std::unique_ptr<LLVMRemarkStreamer> LLVMRS; 1677 1678 LLVMContext::YieldCallbackTy YieldCallback = nullptr; 1679 void *YieldOpaqueHandle = nullptr; 1680 1681 DenseMap<const Value *, ValueName *> ValueNames; 1682 1683 DenseMap<unsigned, std::unique_ptr<ConstantInt>> IntZeroConstants; 1684 DenseMap<unsigned, std::unique_ptr<ConstantInt>> IntOneConstants; 1685 DenseMap<APInt, std::unique_ptr<ConstantInt>> IntConstants; 1686 DenseMap<std::pair<ElementCount, APInt>, std::unique_ptr<ConstantInt>> 1687 IntSplatConstants; 1688 1689 DenseMap<APFloat, std::unique_ptr<ConstantFP>> FPConstants; 1690 DenseMap<std::pair<ElementCount, APFloat>, std::unique_ptr<ConstantFP>> 1691 FPSplatConstants; 1692 1693 FoldingSet<AttributeImpl> AttrsSet; 1694 FoldingSet<AttributeListImpl> AttrsLists; 1695 FoldingSet<AttributeSetNode> AttrsSetNodes; 1696 1697 StringMap<MDString, BumpPtrAllocator> MDStringCache; 1698 DenseMap<Value *, ValueAsMetadata *> ValuesAsMetadata; 1699 DenseMap<Metadata *, MetadataAsValue *> MetadataAsValues; 1700 DenseSet<DIArgList *, DIArgListInfo> DIArgLists; 1701 1702 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \ 1703 DenseSet<CLASS *, CLASS##Info> CLASS##s; 1704 #include "llvm/IR/Metadata.def" 1705 1706 // Optional map for looking up composite types by identifier. 1707 std::optional<DenseMap<const MDString *, DICompositeType *>> DITypeMap; 1708 1709 // MDNodes may be uniqued or not uniqued. When they're not uniqued, they 1710 // aren't in the MDNodeSet, but they're still shared between objects, so no 1711 // one object can destroy them. Keep track of them here so we can delete 1712 // them on context teardown. 1713 std::vector<MDNode *> DistinctMDNodes; 1714 1715 // ConstantRangeListAttributeImpl is a TrailingObjects/ArrayRef of 1716 // ConstantRange. Since this is a dynamically sized class, it's not 1717 // possible to use SpecificBumpPtrAllocator. Instead, we use normal Alloc 1718 // for allocation and record all allocated pointers in this vector. In the 1719 // LLVMContext destructor, call the destuctors of everything in the vector. 1720 std::vector<ConstantRangeListAttributeImpl *> ConstantRangeListAttributes; 1721 1722 DenseMap<Type *, std::unique_ptr<ConstantAggregateZero>> CAZConstants; 1723 1724 using ArrayConstantsTy = ConstantUniqueMap<ConstantArray>; 1725 ArrayConstantsTy ArrayConstants; 1726 1727 using StructConstantsTy = ConstantUniqueMap<ConstantStruct>; 1728 StructConstantsTy StructConstants; 1729 1730 using VectorConstantsTy = ConstantUniqueMap<ConstantVector>; 1731 VectorConstantsTy VectorConstants; 1732 1733 DenseMap<PointerType *, std::unique_ptr<ConstantPointerNull>> CPNConstants; 1734 1735 DenseMap<TargetExtType *, std::unique_ptr<ConstantTargetNone>> CTNConstants; 1736 1737 DenseMap<Type *, std::unique_ptr<UndefValue>> UVConstants; 1738 1739 DenseMap<Type *, std::unique_ptr<PoisonValue>> PVConstants; 1740 1741 StringMap<std::unique_ptr<ConstantDataSequential>> CDSConstants; 1742 1743 DenseMap<const BasicBlock *, BlockAddress *> BlockAddresses; 1744 1745 DenseMap<const GlobalValue *, DSOLocalEquivalent *> DSOLocalEquivalents; 1746 1747 DenseMap<const GlobalValue *, NoCFIValue *> NoCFIValues; 1748 1749 ConstantUniqueMap<ConstantPtrAuth> ConstantPtrAuths; 1750 1751 ConstantUniqueMap<ConstantExpr> ExprConstants; 1752 1753 ConstantUniqueMap<InlineAsm> InlineAsms; 1754 1755 ConstantInt *TheTrueVal = nullptr; 1756 ConstantInt *TheFalseVal = nullptr; 1757 1758 // Basic type instances. 1759 Type VoidTy, LabelTy, HalfTy, BFloatTy, FloatTy, DoubleTy, MetadataTy, 1760 TokenTy; 1761 Type X86_FP80Ty, FP128Ty, PPC_FP128Ty, X86_AMXTy; 1762 IntegerType Int1Ty, Int8Ty, Int16Ty, Int32Ty, Int64Ty, Int128Ty; 1763 1764 std::unique_ptr<ConstantTokenNone> TheNoneToken; 1765 1766 BumpPtrAllocator Alloc; 1767 UniqueStringSaver Saver{Alloc}; 1768 SpecificBumpPtrAllocator<ConstantRangeAttributeImpl> 1769 ConstantRangeAttributeAlloc; 1770 1771 DenseMap<unsigned, IntegerType *> IntegerTypes; 1772 1773 using FunctionTypeSet = DenseSet<FunctionType *, FunctionTypeKeyInfo>; 1774 FunctionTypeSet FunctionTypes; 1775 using StructTypeSet = DenseSet<StructType *, AnonStructTypeKeyInfo>; 1776 StructTypeSet AnonStructTypes; 1777 StringMap<StructType *> NamedStructTypes; 1778 unsigned NamedStructTypesUniqueID = 0; 1779 1780 using TargetExtTypeSet = DenseSet<TargetExtType *, TargetExtTypeKeyInfo>; 1781 TargetExtTypeSet TargetExtTypes; 1782 1783 DenseMap<std::pair<Type *, uint64_t>, ArrayType *> ArrayTypes; 1784 DenseMap<std::pair<Type *, ElementCount>, VectorType *> VectorTypes; 1785 PointerType *AS0PointerType = nullptr; // AddrSpace = 0 1786 DenseMap<unsigned, PointerType *> PointerTypes; 1787 DenseMap<std::pair<Type *, unsigned>, TypedPointerType *> ASTypedPointerTypes; 1788 1789 /// ValueHandles - This map keeps track of all of the value handles that are 1790 /// watching a Value*. The Value::HasValueHandle bit is used to know 1791 /// whether or not a value has an entry in this map. 1792 using ValueHandlesTy = DenseMap<Value *, ValueHandleBase *>; 1793 ValueHandlesTy ValueHandles; 1794 1795 /// CustomMDKindNames - Map to hold the metadata string to ID mapping. 1796 StringMap<unsigned> CustomMDKindNames; 1797 1798 /// Collection of metadata used in this context. 1799 DenseMap<const Value *, MDAttachments> ValueMetadata; 1800 1801 /// Map DIAssignID -> Instructions with that attachment. 1802 /// Managed by Instruction via Instruction::updateDIAssignIDMapping. 1803 /// Query using the at:: functions defined in DebugInfo.h. 1804 DenseMap<DIAssignID *, SmallVector<Instruction *, 1>> AssignmentIDToInstrs; 1805 1806 /// Collection of per-GlobalObject sections used in this context. 1807 DenseMap<const GlobalObject *, StringRef> GlobalObjectSections; 1808 1809 /// Collection of per-GlobalValue partitions used in this context. 1810 DenseMap<const GlobalValue *, StringRef> GlobalValuePartitions; 1811 1812 DenseMap<const GlobalValue *, GlobalValue::SanitizerMetadata> 1813 GlobalValueSanitizerMetadata; 1814 1815 /// DiscriminatorTable - This table maps file:line locations to an 1816 /// integer representing the next DWARF path discriminator to assign to 1817 /// instructions in different blocks at the same location. 1818 DenseMap<std::pair<const char *, unsigned>, unsigned> DiscriminatorTable; 1819 1820 /// A set of interned tags for operand bundles. The StringMap maps 1821 /// bundle tags to their IDs. 1822 /// 1823 /// \see LLVMContext::getOperandBundleTagID 1824 StringMap<uint32_t> BundleTagCache; 1825 1826 StringMapEntry<uint32_t> *getOrInsertBundleTag(StringRef Tag); 1827 void getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const; 1828 uint32_t getOperandBundleTagID(StringRef Tag) const; 1829 1830 /// A set of interned synchronization scopes. The StringMap maps 1831 /// synchronization scope names to their respective synchronization scope IDs. 1832 StringMap<SyncScope::ID> SSC; 1833 1834 /// getOrInsertSyncScopeID - Maps synchronization scope name to 1835 /// synchronization scope ID. Every synchronization scope registered with 1836 /// LLVMContext has unique ID except pre-defined ones. 1837 SyncScope::ID getOrInsertSyncScopeID(StringRef SSN); 1838 1839 /// getSyncScopeNames - Populates client supplied SmallVector with 1840 /// synchronization scope names registered with LLVMContext. Synchronization 1841 /// scope names are ordered by increasing synchronization scope IDs. 1842 void getSyncScopeNames(SmallVectorImpl<StringRef> &SSNs) const; 1843 1844 /// getSyncScopeName - Returns the name of a SyncScope::ID 1845 /// registered with LLVMContext, if any. 1846 std::optional<StringRef> getSyncScopeName(SyncScope::ID Id) const; 1847 1848 /// Maintain the GC name for each function. 1849 /// 1850 /// This saves allocating an additional word in Function for programs which 1851 /// do not use GC (i.e., most programs) at the cost of increased overhead for 1852 /// clients which do use GC. 1853 DenseMap<const Function *, std::string> GCNames; 1854 1855 /// Flag to indicate if Value (other than GlobalValue) retains their name or 1856 /// not. 1857 bool DiscardValueNames = false; 1858 1859 LLVMContextImpl(LLVMContext &C); 1860 ~LLVMContextImpl(); 1861 1862 mutable OptPassGate *OPG = nullptr; 1863 1864 /// Access the object which can disable optional passes and individual 1865 /// optimizations at compile time. 1866 OptPassGate &getOptPassGate() const; 1867 1868 /// Set the object which can disable optional passes and individual 1869 /// optimizations at compile time. 1870 /// 1871 /// The lifetime of the object must be guaranteed to extend as long as the 1872 /// LLVMContext is used by compilation. 1873 void setOptPassGate(OptPassGate &); 1874 1875 /// Mapping of blocks to collections of "trailing" DbgVariableRecords. As part 1876 /// of the "RemoveDIs" project, debug-info variable location records are going 1877 /// to cease being instructions... which raises the problem of where should 1878 /// they be recorded when we remove the terminator of a blocks, such as: 1879 /// 1880 /// %foo = add i32 0, 0 1881 /// br label %bar 1882 /// 1883 /// If the branch is removed, a legitimate transient state while editing a 1884 /// block, any debug-records between those two instructions will not have a 1885 /// location. Each block thus records any DbgVariableRecord records that 1886 /// "trail" in such a way. These are stored in LLVMContext because typically 1887 /// LLVM only edits a small number of blocks at a time, so there's no need to 1888 /// bloat BasicBlock with such a data structure. 1889 SmallDenseMap<BasicBlock *, DbgMarker *> TrailingDbgRecords; 1890 1891 // Set, get and delete operations for TrailingDbgRecords. 1892 void setTrailingDbgRecords(BasicBlock *B, DbgMarker *M) { 1893 assert(!TrailingDbgRecords.count(B)); 1894 TrailingDbgRecords[B] = M; 1895 } 1896 1897 DbgMarker *getTrailingDbgRecords(BasicBlock *B) { 1898 return TrailingDbgRecords.lookup(B); 1899 } 1900 1901 void deleteTrailingDbgRecords(BasicBlock *B) { TrailingDbgRecords.erase(B); } 1902 1903 std::string DefaultTargetCPU; 1904 std::string DefaultTargetFeatures; 1905 1906 /// The next available source atom group number. The front end is responsible 1907 /// for assigning source atom numbers, but certain optimisations need to 1908 /// assign new group numbers to a set of instructions. Most often code 1909 /// duplication optimisations like loop unroll. Tracking a global maximum 1910 /// value means we can know (cheaply) we're never using a group number that's 1911 /// already used within this function. 1912 /// 1913 /// Start a 1 because 0 means the source location isn't part of an atom group. 1914 uint64_t NextAtomGroup = 1; 1915 }; 1916 1917 } // end namespace llvm 1918 1919 #endif // LLVM_LIB_IR_LLVMCONTEXTIMPL_H 1920