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 "AttributeImpl.h" 18 #include "ConstantsContext.h" 19 #include "llvm/ADT/APFloat.h" 20 #include "llvm/ADT/APInt.h" 21 #include "llvm/ADT/ArrayRef.h" 22 #include "llvm/ADT/DenseMap.h" 23 #include "llvm/ADT/DenseMapInfo.h" 24 #include "llvm/ADT/DenseSet.h" 25 #include "llvm/ADT/FoldingSet.h" 26 #include "llvm/ADT/Hashing.h" 27 #include "llvm/ADT/Optional.h" 28 #include "llvm/ADT/STLExtras.h" 29 #include "llvm/ADT/SmallPtrSet.h" 30 #include "llvm/ADT/SmallVector.h" 31 #include "llvm/ADT/StringMap.h" 32 #include "llvm/BinaryFormat/Dwarf.h" 33 #include "llvm/IR/Constants.h" 34 #include "llvm/IR/DebugInfoMetadata.h" 35 #include "llvm/IR/DerivedTypes.h" 36 #include "llvm/IR/LLVMContext.h" 37 #include "llvm/IR/LLVMRemarkStreamer.h" 38 #include "llvm/IR/Metadata.h" 39 #include "llvm/IR/TrackingMDRef.h" 40 #include "llvm/Support/Allocator.h" 41 #include "llvm/Support/Casting.h" 42 #include "llvm/Support/StringSaver.h" 43 #include "llvm/Support/YAMLTraits.h" 44 #include <algorithm> 45 #include <cassert> 46 #include <cstddef> 47 #include <cstdint> 48 #include <memory> 49 #include <string> 50 #include <utility> 51 #include <vector> 52 53 namespace llvm { 54 55 class StringRef; 56 class Type; 57 class Value; 58 class ValueHandleBase; 59 60 using DenseMapAPIntKeyInfo = DenseMapInfo<APInt>; 61 62 struct DenseMapAPFloatKeyInfo { getEmptyKeyDenseMapAPFloatKeyInfo63 static inline APFloat getEmptyKey() { return APFloat(APFloat::Bogus(), 1); } getTombstoneKeyDenseMapAPFloatKeyInfo64 static inline APFloat getTombstoneKey() { return APFloat(APFloat::Bogus(), 2); } 65 getHashValueDenseMapAPFloatKeyInfo66 static unsigned getHashValue(const APFloat &Key) { 67 return static_cast<unsigned>(hash_value(Key)); 68 } 69 isEqualDenseMapAPFloatKeyInfo70 static bool isEqual(const APFloat &LHS, const APFloat &RHS) { 71 return LHS.bitwiseIsEqual(RHS); 72 } 73 }; 74 75 struct AnonStructTypeKeyInfo { 76 struct KeyTy { 77 ArrayRef<Type*> ETypes; 78 bool isPacked; 79 KeyTyAnonStructTypeKeyInfo::KeyTy80 KeyTy(const ArrayRef<Type*>& E, bool P) : 81 ETypes(E), isPacked(P) {} 82 KeyTyAnonStructTypeKeyInfo::KeyTy83 KeyTy(const StructType *ST) 84 : ETypes(ST->elements()), isPacked(ST->isPacked()) {} 85 86 bool operator==(const KeyTy& that) const { 87 if (isPacked != that.isPacked) 88 return false; 89 if (ETypes != that.ETypes) 90 return false; 91 return true; 92 } 93 bool operator!=(const KeyTy& that) const { 94 return !this->operator==(that); 95 } 96 }; 97 getEmptyKeyAnonStructTypeKeyInfo98 static inline StructType* getEmptyKey() { 99 return DenseMapInfo<StructType*>::getEmptyKey(); 100 } 101 getTombstoneKeyAnonStructTypeKeyInfo102 static inline StructType* getTombstoneKey() { 103 return DenseMapInfo<StructType*>::getTombstoneKey(); 104 } 105 getHashValueAnonStructTypeKeyInfo106 static unsigned getHashValue(const KeyTy& Key) { 107 return hash_combine(hash_combine_range(Key.ETypes.begin(), 108 Key.ETypes.end()), 109 Key.isPacked); 110 } 111 getHashValueAnonStructTypeKeyInfo112 static unsigned getHashValue(const StructType *ST) { 113 return getHashValue(KeyTy(ST)); 114 } 115 isEqualAnonStructTypeKeyInfo116 static bool isEqual(const KeyTy& LHS, const StructType *RHS) { 117 if (RHS == getEmptyKey() || RHS == getTombstoneKey()) 118 return false; 119 return LHS == KeyTy(RHS); 120 } 121 isEqualAnonStructTypeKeyInfo122 static bool isEqual(const StructType *LHS, const StructType *RHS) { 123 return LHS == RHS; 124 } 125 }; 126 127 struct FunctionTypeKeyInfo { 128 struct KeyTy { 129 const Type *ReturnType; 130 ArrayRef<Type*> Params; 131 bool isVarArg; 132 KeyTyFunctionTypeKeyInfo::KeyTy133 KeyTy(const Type* R, const ArrayRef<Type*>& P, bool V) : 134 ReturnType(R), Params(P), isVarArg(V) {} KeyTyFunctionTypeKeyInfo::KeyTy135 KeyTy(const FunctionType *FT) 136 : ReturnType(FT->getReturnType()), Params(FT->params()), 137 isVarArg(FT->isVarArg()) {} 138 139 bool operator==(const KeyTy& that) const { 140 if (ReturnType != that.ReturnType) 141 return false; 142 if (isVarArg != that.isVarArg) 143 return false; 144 if (Params != that.Params) 145 return false; 146 return true; 147 } 148 bool operator!=(const KeyTy& that) const { 149 return !this->operator==(that); 150 } 151 }; 152 getEmptyKeyFunctionTypeKeyInfo153 static inline FunctionType* getEmptyKey() { 154 return DenseMapInfo<FunctionType*>::getEmptyKey(); 155 } 156 getTombstoneKeyFunctionTypeKeyInfo157 static inline FunctionType* getTombstoneKey() { 158 return DenseMapInfo<FunctionType*>::getTombstoneKey(); 159 } 160 getHashValueFunctionTypeKeyInfo161 static unsigned getHashValue(const KeyTy& Key) { 162 return hash_combine(Key.ReturnType, 163 hash_combine_range(Key.Params.begin(), 164 Key.Params.end()), 165 Key.isVarArg); 166 } 167 getHashValueFunctionTypeKeyInfo168 static unsigned getHashValue(const FunctionType *FT) { 169 return getHashValue(KeyTy(FT)); 170 } 171 isEqualFunctionTypeKeyInfo172 static bool isEqual(const KeyTy& LHS, const FunctionType *RHS) { 173 if (RHS == getEmptyKey() || RHS == getTombstoneKey()) 174 return false; 175 return LHS == KeyTy(RHS); 176 } 177 isEqualFunctionTypeKeyInfo178 static bool isEqual(const FunctionType *LHS, const FunctionType *RHS) { 179 return LHS == RHS; 180 } 181 }; 182 183 /// Structure for hashing arbitrary MDNode operands. 184 class MDNodeOpsKey { 185 ArrayRef<Metadata *> RawOps; 186 ArrayRef<MDOperand> Ops; 187 unsigned Hash; 188 189 protected: MDNodeOpsKey(ArrayRef<Metadata * > Ops)190 MDNodeOpsKey(ArrayRef<Metadata *> Ops) 191 : RawOps(Ops), Hash(calculateHash(Ops)) {} 192 193 template <class NodeTy> 194 MDNodeOpsKey(const NodeTy *N, unsigned Offset = 0) 195 : Ops(N->op_begin() + Offset, N->op_end()), Hash(N->getHash()) {} 196 197 template <class NodeTy> 198 bool compareOps(const NodeTy *RHS, unsigned Offset = 0) const { 199 if (getHash() != RHS->getHash()) 200 return false; 201 202 assert((RawOps.empty() || Ops.empty()) && "Two sets of operands?"); 203 return RawOps.empty() ? compareOps(Ops, RHS, Offset) 204 : compareOps(RawOps, RHS, Offset); 205 } 206 207 static unsigned calculateHash(MDNode *N, unsigned Offset = 0); 208 209 private: 210 template <class T> compareOps(ArrayRef<T> Ops,const MDNode * RHS,unsigned Offset)211 static bool compareOps(ArrayRef<T> Ops, const MDNode *RHS, unsigned Offset) { 212 if (Ops.size() != RHS->getNumOperands() - Offset) 213 return false; 214 return std::equal(Ops.begin(), Ops.end(), RHS->op_begin() + Offset); 215 } 216 217 static unsigned calculateHash(ArrayRef<Metadata *> Ops); 218 219 public: getHash()220 unsigned getHash() const { return Hash; } 221 }; 222 223 template <class NodeTy> struct MDNodeKeyImpl; 224 225 /// Configuration point for MDNodeInfo::isEqual(). 226 template <class NodeTy> struct MDNodeSubsetEqualImpl { 227 using KeyTy = MDNodeKeyImpl<NodeTy>; 228 isSubsetEqualMDNodeSubsetEqualImpl229 static bool isSubsetEqual(const KeyTy &LHS, const NodeTy *RHS) { 230 return false; 231 } 232 isSubsetEqualMDNodeSubsetEqualImpl233 static bool isSubsetEqual(const NodeTy *LHS, const NodeTy *RHS) { 234 return false; 235 } 236 }; 237 238 /// DenseMapInfo for MDTuple. 239 /// 240 /// Note that we don't need the is-function-local bit, since that's implicit in 241 /// the operands. 242 template <> struct MDNodeKeyImpl<MDTuple> : MDNodeOpsKey { 243 MDNodeKeyImpl(ArrayRef<Metadata *> Ops) : MDNodeOpsKey(Ops) {} 244 MDNodeKeyImpl(const MDTuple *N) : MDNodeOpsKey(N) {} 245 246 bool isKeyOf(const MDTuple *RHS) const { return compareOps(RHS); } 247 248 unsigned getHashValue() const { return getHash(); } 249 250 static unsigned calculateHash(MDTuple *N) { 251 return MDNodeOpsKey::calculateHash(N); 252 } 253 }; 254 255 /// DenseMapInfo for DILocation. 256 template <> struct MDNodeKeyImpl<DILocation> { 257 unsigned Line; 258 unsigned Column; 259 Metadata *Scope; 260 Metadata *InlinedAt; 261 bool ImplicitCode; 262 263 MDNodeKeyImpl(unsigned Line, unsigned Column, Metadata *Scope, 264 Metadata *InlinedAt, bool ImplicitCode) 265 : Line(Line), Column(Column), Scope(Scope), InlinedAt(InlinedAt), 266 ImplicitCode(ImplicitCode) {} 267 MDNodeKeyImpl(const DILocation *L) 268 : Line(L->getLine()), Column(L->getColumn()), Scope(L->getRawScope()), 269 InlinedAt(L->getRawInlinedAt()), ImplicitCode(L->isImplicitCode()) {} 270 271 bool isKeyOf(const DILocation *RHS) const { 272 return Line == RHS->getLine() && Column == RHS->getColumn() && 273 Scope == RHS->getRawScope() && InlinedAt == RHS->getRawInlinedAt() && 274 ImplicitCode == RHS->isImplicitCode(); 275 } 276 277 unsigned getHashValue() const { 278 return hash_combine(Line, Column, Scope, InlinedAt, ImplicitCode); 279 } 280 }; 281 282 /// DenseMapInfo for GenericDINode. 283 template <> struct MDNodeKeyImpl<GenericDINode> : MDNodeOpsKey { 284 unsigned Tag; 285 MDString *Header; 286 287 MDNodeKeyImpl(unsigned Tag, MDString *Header, ArrayRef<Metadata *> DwarfOps) 288 : MDNodeOpsKey(DwarfOps), Tag(Tag), Header(Header) {} 289 MDNodeKeyImpl(const GenericDINode *N) 290 : MDNodeOpsKey(N, 1), Tag(N->getTag()), Header(N->getRawHeader()) {} 291 292 bool isKeyOf(const GenericDINode *RHS) const { 293 return Tag == RHS->getTag() && Header == RHS->getRawHeader() && 294 compareOps(RHS, 1); 295 } 296 297 unsigned getHashValue() const { return hash_combine(getHash(), Tag, Header); } 298 299 static unsigned calculateHash(GenericDINode *N) { 300 return MDNodeOpsKey::calculateHash(N, 1); 301 } 302 }; 303 304 template <> struct MDNodeKeyImpl<DISubrange> { 305 Metadata *CountNode; 306 Metadata *LowerBound; 307 Metadata *UpperBound; 308 Metadata *Stride; 309 310 MDNodeKeyImpl(Metadata *CountNode, Metadata *LowerBound, Metadata *UpperBound, 311 Metadata *Stride) 312 : CountNode(CountNode), LowerBound(LowerBound), UpperBound(UpperBound), 313 Stride(Stride) {} 314 MDNodeKeyImpl(const DISubrange *N) 315 : CountNode(N->getRawCountNode()), LowerBound(N->getRawLowerBound()), 316 UpperBound(N->getRawUpperBound()), Stride(N->getRawStride()) {} 317 318 bool isKeyOf(const DISubrange *RHS) const { 319 auto BoundsEqual = [=](Metadata *Node1, Metadata *Node2) -> bool { 320 if (Node1 == Node2) 321 return true; 322 323 ConstantAsMetadata *MD1 = dyn_cast_or_null<ConstantAsMetadata>(Node1); 324 ConstantAsMetadata *MD2 = dyn_cast_or_null<ConstantAsMetadata>(Node2); 325 if (MD1 && MD2) { 326 ConstantInt *CV1 = cast<ConstantInt>(MD1->getValue()); 327 ConstantInt *CV2 = cast<ConstantInt>(MD2->getValue()); 328 if (CV1->getSExtValue() == CV2->getSExtValue()) 329 return true; 330 } 331 return false; 332 }; 333 334 return BoundsEqual(CountNode, RHS->getRawCountNode()) && 335 BoundsEqual(LowerBound, RHS->getRawLowerBound()) && 336 BoundsEqual(UpperBound, RHS->getRawUpperBound()) && 337 BoundsEqual(Stride, RHS->getRawStride()); 338 } 339 340 unsigned getHashValue() const { 341 if (CountNode) 342 if (auto *MD = dyn_cast<ConstantAsMetadata>(CountNode)) 343 return hash_combine(cast<ConstantInt>(MD->getValue())->getSExtValue(), 344 LowerBound, UpperBound, Stride); 345 return hash_combine(CountNode, LowerBound, UpperBound, Stride); 346 } 347 }; 348 349 template <> struct MDNodeKeyImpl<DIGenericSubrange> { 350 Metadata *CountNode; 351 Metadata *LowerBound; 352 Metadata *UpperBound; 353 Metadata *Stride; 354 355 MDNodeKeyImpl(Metadata *CountNode, Metadata *LowerBound, Metadata *UpperBound, 356 Metadata *Stride) 357 : CountNode(CountNode), LowerBound(LowerBound), UpperBound(UpperBound), 358 Stride(Stride) {} 359 MDNodeKeyImpl(const DIGenericSubrange *N) 360 : CountNode(N->getRawCountNode()), LowerBound(N->getRawLowerBound()), 361 UpperBound(N->getRawUpperBound()), Stride(N->getRawStride()) {} 362 363 bool isKeyOf(const DIGenericSubrange *RHS) const { 364 return (CountNode == RHS->getRawCountNode()) && 365 (LowerBound == RHS->getRawLowerBound()) && 366 (UpperBound == RHS->getRawUpperBound()) && 367 (Stride == RHS->getRawStride()); 368 } 369 370 unsigned getHashValue() const { 371 auto *MD = dyn_cast_or_null<ConstantAsMetadata>(CountNode); 372 if (CountNode && MD) 373 return hash_combine(cast<ConstantInt>(MD->getValue())->getSExtValue(), 374 LowerBound, UpperBound, Stride); 375 return hash_combine(CountNode, LowerBound, UpperBound, Stride); 376 } 377 }; 378 379 template <> struct MDNodeKeyImpl<DIEnumerator> { 380 APInt Value; 381 MDString *Name; 382 bool IsUnsigned; 383 384 MDNodeKeyImpl(APInt Value, bool IsUnsigned, MDString *Name) 385 : Value(Value), Name(Name), IsUnsigned(IsUnsigned) {} 386 MDNodeKeyImpl(int64_t Value, bool IsUnsigned, MDString *Name) 387 : Value(APInt(64, Value, !IsUnsigned)), Name(Name), 388 IsUnsigned(IsUnsigned) {} 389 MDNodeKeyImpl(const DIEnumerator *N) 390 : Value(N->getValue()), Name(N->getRawName()), 391 IsUnsigned(N->isUnsigned()) {} 392 393 bool isKeyOf(const DIEnumerator *RHS) const { 394 return Value.getBitWidth() == RHS->getValue().getBitWidth() && 395 Value == RHS->getValue() && IsUnsigned == RHS->isUnsigned() && 396 Name == RHS->getRawName(); 397 } 398 399 unsigned getHashValue() const { return hash_combine(Value, Name); } 400 }; 401 402 template <> struct MDNodeKeyImpl<DIBasicType> { 403 unsigned Tag; 404 MDString *Name; 405 uint64_t SizeInBits; 406 uint32_t AlignInBits; 407 unsigned Encoding; 408 unsigned Flags; 409 410 MDNodeKeyImpl(unsigned Tag, MDString *Name, uint64_t SizeInBits, 411 uint32_t AlignInBits, unsigned Encoding, unsigned Flags) 412 : Tag(Tag), Name(Name), SizeInBits(SizeInBits), AlignInBits(AlignInBits), 413 Encoding(Encoding), Flags(Flags) {} 414 MDNodeKeyImpl(const DIBasicType *N) 415 : Tag(N->getTag()), Name(N->getRawName()), SizeInBits(N->getSizeInBits()), 416 AlignInBits(N->getAlignInBits()), Encoding(N->getEncoding()), Flags(N->getFlags()) {} 417 418 bool isKeyOf(const DIBasicType *RHS) const { 419 return Tag == RHS->getTag() && Name == RHS->getRawName() && 420 SizeInBits == RHS->getSizeInBits() && 421 AlignInBits == RHS->getAlignInBits() && 422 Encoding == RHS->getEncoding() && 423 Flags == RHS->getFlags(); 424 } 425 426 unsigned getHashValue() const { 427 return hash_combine(Tag, Name, SizeInBits, AlignInBits, Encoding); 428 } 429 }; 430 431 template <> struct MDNodeKeyImpl<DIStringType> { 432 unsigned Tag; 433 MDString *Name; 434 Metadata *StringLength; 435 Metadata *StringLengthExp; 436 uint64_t SizeInBits; 437 uint32_t AlignInBits; 438 unsigned Encoding; 439 440 MDNodeKeyImpl(unsigned Tag, MDString *Name, Metadata *StringLength, 441 Metadata *StringLengthExp, uint64_t SizeInBits, 442 uint32_t AlignInBits, unsigned Encoding) 443 : Tag(Tag), Name(Name), StringLength(StringLength), 444 StringLengthExp(StringLengthExp), SizeInBits(SizeInBits), 445 AlignInBits(AlignInBits), Encoding(Encoding) {} 446 MDNodeKeyImpl(const DIStringType *N) 447 : Tag(N->getTag()), Name(N->getRawName()), 448 StringLength(N->getRawStringLength()), 449 StringLengthExp(N->getRawStringLengthExp()), 450 SizeInBits(N->getSizeInBits()), AlignInBits(N->getAlignInBits()), 451 Encoding(N->getEncoding()) {} 452 453 bool isKeyOf(const DIStringType *RHS) const { 454 return Tag == RHS->getTag() && Name == RHS->getRawName() && 455 SizeInBits == RHS->getSizeInBits() && 456 AlignInBits == RHS->getAlignInBits() && 457 Encoding == RHS->getEncoding(); 458 } 459 unsigned getHashValue() const { return hash_combine(Tag, Name, Encoding); } 460 }; 461 462 template <> struct MDNodeKeyImpl<DIDerivedType> { 463 unsigned Tag; 464 MDString *Name; 465 Metadata *File; 466 unsigned Line; 467 Metadata *Scope; 468 Metadata *BaseType; 469 uint64_t SizeInBits; 470 uint64_t OffsetInBits; 471 uint32_t AlignInBits; 472 Optional<unsigned> DWARFAddressSpace; 473 unsigned Flags; 474 Metadata *ExtraData; 475 476 MDNodeKeyImpl(unsigned Tag, MDString *Name, Metadata *File, unsigned Line, 477 Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits, 478 uint32_t AlignInBits, uint64_t OffsetInBits, 479 Optional<unsigned> DWARFAddressSpace, unsigned Flags, 480 Metadata *ExtraData) 481 : Tag(Tag), Name(Name), File(File), Line(Line), Scope(Scope), 482 BaseType(BaseType), SizeInBits(SizeInBits), OffsetInBits(OffsetInBits), 483 AlignInBits(AlignInBits), DWARFAddressSpace(DWARFAddressSpace), 484 Flags(Flags), ExtraData(ExtraData) {} 485 MDNodeKeyImpl(const DIDerivedType *N) 486 : Tag(N->getTag()), Name(N->getRawName()), File(N->getRawFile()), 487 Line(N->getLine()), Scope(N->getRawScope()), 488 BaseType(N->getRawBaseType()), SizeInBits(N->getSizeInBits()), 489 OffsetInBits(N->getOffsetInBits()), AlignInBits(N->getAlignInBits()), 490 DWARFAddressSpace(N->getDWARFAddressSpace()), Flags(N->getFlags()), 491 ExtraData(N->getRawExtraData()) {} 492 493 bool isKeyOf(const DIDerivedType *RHS) const { 494 return Tag == RHS->getTag() && Name == RHS->getRawName() && 495 File == RHS->getRawFile() && Line == RHS->getLine() && 496 Scope == RHS->getRawScope() && BaseType == RHS->getRawBaseType() && 497 SizeInBits == RHS->getSizeInBits() && 498 AlignInBits == RHS->getAlignInBits() && 499 OffsetInBits == RHS->getOffsetInBits() && 500 DWARFAddressSpace == RHS->getDWARFAddressSpace() && 501 Flags == RHS->getFlags() && 502 ExtraData == RHS->getRawExtraData(); 503 } 504 505 unsigned getHashValue() const { 506 // If this is a member inside an ODR type, only hash the type and the name. 507 // Otherwise the hash will be stronger than 508 // MDNodeSubsetEqualImpl::isODRMember(). 509 if (Tag == dwarf::DW_TAG_member && Name) 510 if (auto *CT = dyn_cast_or_null<DICompositeType>(Scope)) 511 if (CT->getRawIdentifier()) 512 return hash_combine(Name, Scope); 513 514 // Intentionally computes the hash on a subset of the operands for 515 // performance reason. The subset has to be significant enough to avoid 516 // collision "most of the time". There is no correctness issue in case of 517 // collision because of the full check above. 518 return hash_combine(Tag, Name, File, Line, Scope, BaseType, Flags); 519 } 520 }; 521 522 template <> struct MDNodeSubsetEqualImpl<DIDerivedType> { 523 using KeyTy = MDNodeKeyImpl<DIDerivedType>; 524 525 static bool isSubsetEqual(const KeyTy &LHS, const DIDerivedType *RHS) { 526 return isODRMember(LHS.Tag, LHS.Scope, LHS.Name, RHS); 527 } 528 529 static bool isSubsetEqual(const DIDerivedType *LHS, const DIDerivedType *RHS) { 530 return isODRMember(LHS->getTag(), LHS->getRawScope(), LHS->getRawName(), 531 RHS); 532 } 533 534 /// Subprograms compare equal if they declare the same function in an ODR 535 /// type. 536 static bool isODRMember(unsigned Tag, const Metadata *Scope, 537 const MDString *Name, const DIDerivedType *RHS) { 538 // Check whether the LHS is eligible. 539 if (Tag != dwarf::DW_TAG_member || !Name) 540 return false; 541 542 auto *CT = dyn_cast_or_null<DICompositeType>(Scope); 543 if (!CT || !CT->getRawIdentifier()) 544 return false; 545 546 // Compare to the RHS. 547 return Tag == RHS->getTag() && Name == RHS->getRawName() && 548 Scope == RHS->getRawScope(); 549 } 550 }; 551 552 template <> struct MDNodeKeyImpl<DICompositeType> { 553 unsigned Tag; 554 MDString *Name; 555 Metadata *File; 556 unsigned Line; 557 Metadata *Scope; 558 Metadata *BaseType; 559 uint64_t SizeInBits; 560 uint64_t OffsetInBits; 561 uint32_t AlignInBits; 562 unsigned Flags; 563 Metadata *Elements; 564 unsigned RuntimeLang; 565 Metadata *VTableHolder; 566 Metadata *TemplateParams; 567 MDString *Identifier; 568 Metadata *Discriminator; 569 Metadata *DataLocation; 570 Metadata *Associated; 571 Metadata *Allocated; 572 Metadata *Rank; 573 574 MDNodeKeyImpl(unsigned Tag, MDString *Name, Metadata *File, unsigned Line, 575 Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits, 576 uint32_t AlignInBits, uint64_t OffsetInBits, unsigned Flags, 577 Metadata *Elements, unsigned RuntimeLang, 578 Metadata *VTableHolder, Metadata *TemplateParams, 579 MDString *Identifier, Metadata *Discriminator, 580 Metadata *DataLocation, Metadata *Associated, 581 Metadata *Allocated, Metadata *Rank) 582 : Tag(Tag), Name(Name), File(File), Line(Line), Scope(Scope), 583 BaseType(BaseType), SizeInBits(SizeInBits), OffsetInBits(OffsetInBits), 584 AlignInBits(AlignInBits), Flags(Flags), Elements(Elements), 585 RuntimeLang(RuntimeLang), VTableHolder(VTableHolder), 586 TemplateParams(TemplateParams), Identifier(Identifier), 587 Discriminator(Discriminator), DataLocation(DataLocation), 588 Associated(Associated), Allocated(Allocated), Rank(Rank) {} 589 MDNodeKeyImpl(const DICompositeType *N) 590 : Tag(N->getTag()), Name(N->getRawName()), File(N->getRawFile()), 591 Line(N->getLine()), Scope(N->getRawScope()), 592 BaseType(N->getRawBaseType()), SizeInBits(N->getSizeInBits()), 593 OffsetInBits(N->getOffsetInBits()), AlignInBits(N->getAlignInBits()), 594 Flags(N->getFlags()), Elements(N->getRawElements()), 595 RuntimeLang(N->getRuntimeLang()), VTableHolder(N->getRawVTableHolder()), 596 TemplateParams(N->getRawTemplateParams()), 597 Identifier(N->getRawIdentifier()), 598 Discriminator(N->getRawDiscriminator()), 599 DataLocation(N->getRawDataLocation()), 600 Associated(N->getRawAssociated()), Allocated(N->getRawAllocated()), 601 Rank(N->getRawRank()) {} 602 603 bool isKeyOf(const DICompositeType *RHS) const { 604 return Tag == RHS->getTag() && Name == RHS->getRawName() && 605 File == RHS->getRawFile() && Line == RHS->getLine() && 606 Scope == RHS->getRawScope() && BaseType == RHS->getRawBaseType() && 607 SizeInBits == RHS->getSizeInBits() && 608 AlignInBits == RHS->getAlignInBits() && 609 OffsetInBits == RHS->getOffsetInBits() && Flags == RHS->getFlags() && 610 Elements == RHS->getRawElements() && 611 RuntimeLang == RHS->getRuntimeLang() && 612 VTableHolder == RHS->getRawVTableHolder() && 613 TemplateParams == RHS->getRawTemplateParams() && 614 Identifier == RHS->getRawIdentifier() && 615 Discriminator == RHS->getRawDiscriminator() && 616 DataLocation == RHS->getRawDataLocation() && 617 Associated == RHS->getRawAssociated() && 618 Allocated == RHS->getRawAllocated() && Rank == RHS->getRawRank(); 619 } 620 621 unsigned getHashValue() const { 622 // Intentionally computes the hash on a subset of the operands for 623 // performance reason. The subset has to be significant enough to avoid 624 // collision "most of the time". There is no correctness issue in case of 625 // collision because of the full check above. 626 return hash_combine(Name, File, Line, BaseType, Scope, Elements, 627 TemplateParams); 628 } 629 }; 630 631 template <> struct MDNodeKeyImpl<DISubroutineType> { 632 unsigned Flags; 633 uint8_t CC; 634 Metadata *TypeArray; 635 636 MDNodeKeyImpl(unsigned Flags, uint8_t CC, Metadata *TypeArray) 637 : Flags(Flags), CC(CC), TypeArray(TypeArray) {} 638 MDNodeKeyImpl(const DISubroutineType *N) 639 : Flags(N->getFlags()), CC(N->getCC()), TypeArray(N->getRawTypeArray()) {} 640 641 bool isKeyOf(const DISubroutineType *RHS) const { 642 return Flags == RHS->getFlags() && CC == RHS->getCC() && 643 TypeArray == RHS->getRawTypeArray(); 644 } 645 646 unsigned getHashValue() const { return hash_combine(Flags, CC, TypeArray); } 647 }; 648 649 template <> struct MDNodeKeyImpl<DIFile> { 650 MDString *Filename; 651 MDString *Directory; 652 Optional<DIFile::ChecksumInfo<MDString *>> Checksum; 653 Optional<MDString *> Source; 654 655 MDNodeKeyImpl(MDString *Filename, MDString *Directory, 656 Optional<DIFile::ChecksumInfo<MDString *>> Checksum, 657 Optional<MDString *> Source) 658 : Filename(Filename), Directory(Directory), Checksum(Checksum), 659 Source(Source) {} 660 MDNodeKeyImpl(const DIFile *N) 661 : Filename(N->getRawFilename()), Directory(N->getRawDirectory()), 662 Checksum(N->getRawChecksum()), Source(N->getRawSource()) {} 663 664 bool isKeyOf(const DIFile *RHS) const { 665 return Filename == RHS->getRawFilename() && 666 Directory == RHS->getRawDirectory() && 667 Checksum == RHS->getRawChecksum() && 668 Source == RHS->getRawSource(); 669 } 670 671 unsigned getHashValue() const { 672 return hash_combine( 673 Filename, Directory, Checksum ? Checksum->Kind : 0, 674 Checksum ? Checksum->Value : nullptr, Source.getValueOr(nullptr)); 675 } 676 }; 677 678 template <> struct MDNodeKeyImpl<DISubprogram> { 679 Metadata *Scope; 680 MDString *Name; 681 MDString *LinkageName; 682 Metadata *File; 683 unsigned Line; 684 Metadata *Type; 685 unsigned ScopeLine; 686 Metadata *ContainingType; 687 unsigned VirtualIndex; 688 int ThisAdjustment; 689 unsigned Flags; 690 unsigned SPFlags; 691 Metadata *Unit; 692 Metadata *TemplateParams; 693 Metadata *Declaration; 694 Metadata *RetainedNodes; 695 Metadata *ThrownTypes; 696 697 MDNodeKeyImpl(Metadata *Scope, MDString *Name, MDString *LinkageName, 698 Metadata *File, unsigned Line, Metadata *Type, 699 unsigned ScopeLine, Metadata *ContainingType, 700 unsigned VirtualIndex, int ThisAdjustment, unsigned Flags, 701 unsigned SPFlags, Metadata *Unit, Metadata *TemplateParams, 702 Metadata *Declaration, Metadata *RetainedNodes, 703 Metadata *ThrownTypes) 704 : Scope(Scope), Name(Name), LinkageName(LinkageName), File(File), 705 Line(Line), Type(Type), ScopeLine(ScopeLine), 706 ContainingType(ContainingType), VirtualIndex(VirtualIndex), 707 ThisAdjustment(ThisAdjustment), Flags(Flags), SPFlags(SPFlags), 708 Unit(Unit), TemplateParams(TemplateParams), Declaration(Declaration), 709 RetainedNodes(RetainedNodes), ThrownTypes(ThrownTypes) {} 710 MDNodeKeyImpl(const DISubprogram *N) 711 : Scope(N->getRawScope()), Name(N->getRawName()), 712 LinkageName(N->getRawLinkageName()), File(N->getRawFile()), 713 Line(N->getLine()), Type(N->getRawType()), ScopeLine(N->getScopeLine()), 714 ContainingType(N->getRawContainingType()), 715 VirtualIndex(N->getVirtualIndex()), 716 ThisAdjustment(N->getThisAdjustment()), Flags(N->getFlags()), 717 SPFlags(N->getSPFlags()), Unit(N->getRawUnit()), 718 TemplateParams(N->getRawTemplateParams()), 719 Declaration(N->getRawDeclaration()), 720 RetainedNodes(N->getRawRetainedNodes()), 721 ThrownTypes(N->getRawThrownTypes()) {} 722 723 bool isKeyOf(const DISubprogram *RHS) const { 724 return Scope == RHS->getRawScope() && Name == RHS->getRawName() && 725 LinkageName == RHS->getRawLinkageName() && 726 File == RHS->getRawFile() && Line == RHS->getLine() && 727 Type == RHS->getRawType() && ScopeLine == RHS->getScopeLine() && 728 ContainingType == RHS->getRawContainingType() && 729 VirtualIndex == RHS->getVirtualIndex() && 730 ThisAdjustment == RHS->getThisAdjustment() && 731 Flags == RHS->getFlags() && SPFlags == RHS->getSPFlags() && 732 Unit == RHS->getUnit() && 733 TemplateParams == RHS->getRawTemplateParams() && 734 Declaration == RHS->getRawDeclaration() && 735 RetainedNodes == RHS->getRawRetainedNodes() && 736 ThrownTypes == RHS->getRawThrownTypes(); 737 } 738 739 bool isDefinition() const { return SPFlags & DISubprogram::SPFlagDefinition; } 740 741 unsigned getHashValue() const { 742 // If this is a declaration inside an ODR type, only hash the type and the 743 // name. Otherwise the hash will be stronger than 744 // MDNodeSubsetEqualImpl::isDeclarationOfODRMember(). 745 if (!isDefinition() && LinkageName) 746 if (auto *CT = dyn_cast_or_null<DICompositeType>(Scope)) 747 if (CT->getRawIdentifier()) 748 return hash_combine(LinkageName, Scope); 749 750 // Intentionally computes the hash on a subset of the operands for 751 // performance reason. The subset has to be significant enough to avoid 752 // collision "most of the time". There is no correctness issue in case of 753 // collision because of the full check above. 754 return hash_combine(Name, Scope, File, Type, Line); 755 } 756 }; 757 758 template <> struct MDNodeSubsetEqualImpl<DISubprogram> { 759 using KeyTy = MDNodeKeyImpl<DISubprogram>; 760 761 static bool isSubsetEqual(const KeyTy &LHS, const DISubprogram *RHS) { 762 return isDeclarationOfODRMember(LHS.isDefinition(), LHS.Scope, 763 LHS.LinkageName, LHS.TemplateParams, RHS); 764 } 765 766 static bool isSubsetEqual(const DISubprogram *LHS, const DISubprogram *RHS) { 767 return isDeclarationOfODRMember(LHS->isDefinition(), LHS->getRawScope(), 768 LHS->getRawLinkageName(), 769 LHS->getRawTemplateParams(), RHS); 770 } 771 772 /// Subprograms compare equal if they declare the same function in an ODR 773 /// type. 774 static bool isDeclarationOfODRMember(bool IsDefinition, const Metadata *Scope, 775 const MDString *LinkageName, 776 const Metadata *TemplateParams, 777 const DISubprogram *RHS) { 778 // Check whether the LHS is eligible. 779 if (IsDefinition || !Scope || !LinkageName) 780 return false; 781 782 auto *CT = dyn_cast_or_null<DICompositeType>(Scope); 783 if (!CT || !CT->getRawIdentifier()) 784 return false; 785 786 // Compare to the RHS. 787 // FIXME: We need to compare template parameters here to avoid incorrect 788 // collisions in mapMetadata when RF_ReuseAndMutateDistinctMDs and a 789 // ODR-DISubprogram has a non-ODR template parameter (i.e., a 790 // DICompositeType that does not have an identifier). Eventually we should 791 // decouple ODR logic from uniquing logic. 792 return IsDefinition == RHS->isDefinition() && Scope == RHS->getRawScope() && 793 LinkageName == RHS->getRawLinkageName() && 794 TemplateParams == RHS->getRawTemplateParams(); 795 } 796 }; 797 798 template <> struct MDNodeKeyImpl<DILexicalBlock> { 799 Metadata *Scope; 800 Metadata *File; 801 unsigned Line; 802 unsigned Column; 803 804 MDNodeKeyImpl(Metadata *Scope, Metadata *File, unsigned Line, unsigned Column) 805 : Scope(Scope), File(File), Line(Line), Column(Column) {} 806 MDNodeKeyImpl(const DILexicalBlock *N) 807 : Scope(N->getRawScope()), File(N->getRawFile()), Line(N->getLine()), 808 Column(N->getColumn()) {} 809 810 bool isKeyOf(const DILexicalBlock *RHS) const { 811 return Scope == RHS->getRawScope() && File == RHS->getRawFile() && 812 Line == RHS->getLine() && Column == RHS->getColumn(); 813 } 814 815 unsigned getHashValue() const { 816 return hash_combine(Scope, File, Line, Column); 817 } 818 }; 819 820 template <> struct MDNodeKeyImpl<DILexicalBlockFile> { 821 Metadata *Scope; 822 Metadata *File; 823 unsigned Discriminator; 824 825 MDNodeKeyImpl(Metadata *Scope, Metadata *File, unsigned Discriminator) 826 : Scope(Scope), File(File), Discriminator(Discriminator) {} 827 MDNodeKeyImpl(const DILexicalBlockFile *N) 828 : Scope(N->getRawScope()), File(N->getRawFile()), 829 Discriminator(N->getDiscriminator()) {} 830 831 bool isKeyOf(const DILexicalBlockFile *RHS) const { 832 return Scope == RHS->getRawScope() && File == RHS->getRawFile() && 833 Discriminator == RHS->getDiscriminator(); 834 } 835 836 unsigned getHashValue() const { 837 return hash_combine(Scope, File, Discriminator); 838 } 839 }; 840 841 template <> struct MDNodeKeyImpl<DINamespace> { 842 Metadata *Scope; 843 MDString *Name; 844 bool ExportSymbols; 845 846 MDNodeKeyImpl(Metadata *Scope, MDString *Name, bool ExportSymbols) 847 : Scope(Scope), Name(Name), ExportSymbols(ExportSymbols) {} 848 MDNodeKeyImpl(const DINamespace *N) 849 : Scope(N->getRawScope()), Name(N->getRawName()), 850 ExportSymbols(N->getExportSymbols()) {} 851 852 bool isKeyOf(const DINamespace *RHS) const { 853 return Scope == RHS->getRawScope() && Name == RHS->getRawName() && 854 ExportSymbols == RHS->getExportSymbols(); 855 } 856 857 unsigned getHashValue() const { 858 return hash_combine(Scope, Name); 859 } 860 }; 861 862 template <> struct MDNodeKeyImpl<DICommonBlock> { 863 Metadata *Scope; 864 Metadata *Decl; 865 MDString *Name; 866 Metadata *File; 867 unsigned LineNo; 868 869 MDNodeKeyImpl(Metadata *Scope, Metadata *Decl, MDString *Name, 870 Metadata *File, unsigned LineNo) 871 : Scope(Scope), Decl(Decl), Name(Name), File(File), LineNo(LineNo) {} 872 MDNodeKeyImpl(const DICommonBlock *N) 873 : Scope(N->getRawScope()), Decl(N->getRawDecl()), Name(N->getRawName()), 874 File(N->getRawFile()), LineNo(N->getLineNo()) {} 875 876 bool isKeyOf(const DICommonBlock *RHS) const { 877 return Scope == RHS->getRawScope() && Decl == RHS->getRawDecl() && 878 Name == RHS->getRawName() && File == RHS->getRawFile() && 879 LineNo == RHS->getLineNo(); 880 } 881 882 unsigned getHashValue() const { 883 return hash_combine(Scope, Decl, Name, File, LineNo); 884 } 885 }; 886 887 template <> struct MDNodeKeyImpl<DIModule> { 888 Metadata *File; 889 Metadata *Scope; 890 MDString *Name; 891 MDString *ConfigurationMacros; 892 MDString *IncludePath; 893 MDString *APINotesFile; 894 unsigned LineNo; 895 bool IsDecl; 896 897 MDNodeKeyImpl(Metadata *File, Metadata *Scope, MDString *Name, 898 MDString *ConfigurationMacros, MDString *IncludePath, 899 MDString *APINotesFile, unsigned LineNo, bool IsDecl) 900 : File(File), Scope(Scope), Name(Name), 901 ConfigurationMacros(ConfigurationMacros), IncludePath(IncludePath), 902 APINotesFile(APINotesFile), LineNo(LineNo), IsDecl(IsDecl) {} 903 MDNodeKeyImpl(const DIModule *N) 904 : File(N->getRawFile()), Scope(N->getRawScope()), Name(N->getRawName()), 905 ConfigurationMacros(N->getRawConfigurationMacros()), 906 IncludePath(N->getRawIncludePath()), 907 APINotesFile(N->getRawAPINotesFile()), LineNo(N->getLineNo()), 908 IsDecl(N->getIsDecl()) {} 909 910 bool isKeyOf(const DIModule *RHS) const { 911 return Scope == RHS->getRawScope() && Name == RHS->getRawName() && 912 ConfigurationMacros == RHS->getRawConfigurationMacros() && 913 IncludePath == RHS->getRawIncludePath() && 914 APINotesFile == RHS->getRawAPINotesFile() && 915 File == RHS->getRawFile() && LineNo == RHS->getLineNo() && 916 IsDecl == RHS->getIsDecl(); 917 } 918 919 unsigned getHashValue() const { 920 return hash_combine(Scope, Name, ConfigurationMacros, IncludePath); 921 } 922 }; 923 924 template <> struct MDNodeKeyImpl<DITemplateTypeParameter> { 925 MDString *Name; 926 Metadata *Type; 927 bool IsDefault; 928 929 MDNodeKeyImpl(MDString *Name, Metadata *Type, bool IsDefault) 930 : Name(Name), Type(Type), IsDefault(IsDefault) {} 931 MDNodeKeyImpl(const DITemplateTypeParameter *N) 932 : Name(N->getRawName()), Type(N->getRawType()), 933 IsDefault(N->isDefault()) {} 934 935 bool isKeyOf(const DITemplateTypeParameter *RHS) const { 936 return Name == RHS->getRawName() && Type == RHS->getRawType() && 937 IsDefault == RHS->isDefault(); 938 } 939 940 unsigned getHashValue() const { return hash_combine(Name, Type, IsDefault); } 941 }; 942 943 template <> struct MDNodeKeyImpl<DITemplateValueParameter> { 944 unsigned Tag; 945 MDString *Name; 946 Metadata *Type; 947 bool IsDefault; 948 Metadata *Value; 949 950 MDNodeKeyImpl(unsigned Tag, MDString *Name, Metadata *Type, bool IsDefault, 951 Metadata *Value) 952 : Tag(Tag), Name(Name), Type(Type), IsDefault(IsDefault), Value(Value) {} 953 MDNodeKeyImpl(const DITemplateValueParameter *N) 954 : Tag(N->getTag()), Name(N->getRawName()), Type(N->getRawType()), 955 IsDefault(N->isDefault()), Value(N->getValue()) {} 956 957 bool isKeyOf(const DITemplateValueParameter *RHS) const { 958 return Tag == RHS->getTag() && Name == RHS->getRawName() && 959 Type == RHS->getRawType() && IsDefault == RHS->isDefault() && 960 Value == RHS->getValue(); 961 } 962 963 unsigned getHashValue() const { 964 return hash_combine(Tag, Name, Type, IsDefault, Value); 965 } 966 }; 967 968 template <> struct MDNodeKeyImpl<DIGlobalVariable> { 969 Metadata *Scope; 970 MDString *Name; 971 MDString *LinkageName; 972 Metadata *File; 973 unsigned Line; 974 Metadata *Type; 975 bool IsLocalToUnit; 976 bool IsDefinition; 977 Metadata *StaticDataMemberDeclaration; 978 Metadata *TemplateParams; 979 uint32_t AlignInBits; 980 981 MDNodeKeyImpl(Metadata *Scope, MDString *Name, MDString *LinkageName, 982 Metadata *File, unsigned Line, Metadata *Type, 983 bool IsLocalToUnit, bool IsDefinition, 984 Metadata *StaticDataMemberDeclaration, Metadata *TemplateParams, 985 uint32_t AlignInBits) 986 : Scope(Scope), Name(Name), LinkageName(LinkageName), File(File), 987 Line(Line), Type(Type), IsLocalToUnit(IsLocalToUnit), 988 IsDefinition(IsDefinition), 989 StaticDataMemberDeclaration(StaticDataMemberDeclaration), 990 TemplateParams(TemplateParams), AlignInBits(AlignInBits) {} 991 MDNodeKeyImpl(const DIGlobalVariable *N) 992 : Scope(N->getRawScope()), Name(N->getRawName()), 993 LinkageName(N->getRawLinkageName()), File(N->getRawFile()), 994 Line(N->getLine()), Type(N->getRawType()), 995 IsLocalToUnit(N->isLocalToUnit()), IsDefinition(N->isDefinition()), 996 StaticDataMemberDeclaration(N->getRawStaticDataMemberDeclaration()), 997 TemplateParams(N->getRawTemplateParams()), 998 AlignInBits(N->getAlignInBits()) {} 999 1000 bool isKeyOf(const DIGlobalVariable *RHS) const { 1001 return Scope == RHS->getRawScope() && Name == RHS->getRawName() && 1002 LinkageName == RHS->getRawLinkageName() && 1003 File == RHS->getRawFile() && Line == RHS->getLine() && 1004 Type == RHS->getRawType() && IsLocalToUnit == RHS->isLocalToUnit() && 1005 IsDefinition == RHS->isDefinition() && 1006 StaticDataMemberDeclaration == 1007 RHS->getRawStaticDataMemberDeclaration() && 1008 TemplateParams == RHS->getRawTemplateParams() && 1009 AlignInBits == RHS->getAlignInBits(); 1010 } 1011 1012 unsigned getHashValue() const { 1013 // We do not use AlignInBits in hashing function here on purpose: 1014 // in most cases this param for local variable is zero (for function param 1015 // it is always zero). This leads to lots of hash collisions and errors on 1016 // cases with lots of similar variables. 1017 // clang/test/CodeGen/debug-info-257-args.c is an example of this problem, 1018 // generated IR is random for each run and test fails with Align included. 1019 // TODO: make hashing work fine with such situations 1020 return hash_combine(Scope, Name, LinkageName, File, Line, Type, 1021 IsLocalToUnit, IsDefinition, /* AlignInBits, */ 1022 StaticDataMemberDeclaration); 1023 } 1024 }; 1025 1026 template <> struct MDNodeKeyImpl<DILocalVariable> { 1027 Metadata *Scope; 1028 MDString *Name; 1029 Metadata *File; 1030 unsigned Line; 1031 Metadata *Type; 1032 unsigned Arg; 1033 unsigned Flags; 1034 uint32_t AlignInBits; 1035 1036 MDNodeKeyImpl(Metadata *Scope, MDString *Name, Metadata *File, unsigned Line, 1037 Metadata *Type, unsigned Arg, unsigned Flags, 1038 uint32_t AlignInBits) 1039 : Scope(Scope), Name(Name), File(File), Line(Line), Type(Type), Arg(Arg), 1040 Flags(Flags), AlignInBits(AlignInBits) {} 1041 MDNodeKeyImpl(const DILocalVariable *N) 1042 : Scope(N->getRawScope()), Name(N->getRawName()), File(N->getRawFile()), 1043 Line(N->getLine()), Type(N->getRawType()), Arg(N->getArg()), 1044 Flags(N->getFlags()), AlignInBits(N->getAlignInBits()) {} 1045 1046 bool isKeyOf(const DILocalVariable *RHS) const { 1047 return Scope == RHS->getRawScope() && Name == RHS->getRawName() && 1048 File == RHS->getRawFile() && Line == RHS->getLine() && 1049 Type == RHS->getRawType() && Arg == RHS->getArg() && 1050 Flags == RHS->getFlags() && AlignInBits == RHS->getAlignInBits(); 1051 } 1052 1053 unsigned getHashValue() const { 1054 // We do not use AlignInBits in hashing function here on purpose: 1055 // in most cases this param for local variable is zero (for function param 1056 // it is always zero). This leads to lots of hash collisions and errors on 1057 // cases with lots of similar variables. 1058 // clang/test/CodeGen/debug-info-257-args.c is an example of this problem, 1059 // generated IR is random for each run and test fails with Align included. 1060 // TODO: make hashing work fine with such situations 1061 return hash_combine(Scope, Name, File, Line, Type, Arg, Flags); 1062 } 1063 }; 1064 1065 template <> struct MDNodeKeyImpl<DILabel> { 1066 Metadata *Scope; 1067 MDString *Name; 1068 Metadata *File; 1069 unsigned Line; 1070 1071 MDNodeKeyImpl(Metadata *Scope, MDString *Name, Metadata *File, unsigned Line) 1072 : Scope(Scope), Name(Name), File(File), Line(Line) {} 1073 MDNodeKeyImpl(const DILabel *N) 1074 : Scope(N->getRawScope()), Name(N->getRawName()), File(N->getRawFile()), 1075 Line(N->getLine()) {} 1076 1077 bool isKeyOf(const DILabel *RHS) const { 1078 return Scope == RHS->getRawScope() && Name == RHS->getRawName() && 1079 File == RHS->getRawFile() && Line == RHS->getLine(); 1080 } 1081 1082 /// Using name and line to get hash value. It should already be mostly unique. 1083 unsigned getHashValue() const { 1084 return hash_combine(Scope, Name, Line); 1085 } 1086 }; 1087 1088 template <> struct MDNodeKeyImpl<DIExpression> { 1089 ArrayRef<uint64_t> Elements; 1090 1091 MDNodeKeyImpl(ArrayRef<uint64_t> Elements) : Elements(Elements) {} 1092 MDNodeKeyImpl(const DIExpression *N) : Elements(N->getElements()) {} 1093 1094 bool isKeyOf(const DIExpression *RHS) const { 1095 return Elements == RHS->getElements(); 1096 } 1097 1098 unsigned getHashValue() const { 1099 return hash_combine_range(Elements.begin(), Elements.end()); 1100 } 1101 }; 1102 1103 template <> struct MDNodeKeyImpl<DIGlobalVariableExpression> { 1104 Metadata *Variable; 1105 Metadata *Expression; 1106 1107 MDNodeKeyImpl(Metadata *Variable, Metadata *Expression) 1108 : Variable(Variable), Expression(Expression) {} 1109 MDNodeKeyImpl(const DIGlobalVariableExpression *N) 1110 : Variable(N->getRawVariable()), Expression(N->getRawExpression()) {} 1111 1112 bool isKeyOf(const DIGlobalVariableExpression *RHS) const { 1113 return Variable == RHS->getRawVariable() && 1114 Expression == RHS->getRawExpression(); 1115 } 1116 1117 unsigned getHashValue() const { return hash_combine(Variable, Expression); } 1118 }; 1119 1120 template <> struct MDNodeKeyImpl<DIObjCProperty> { 1121 MDString *Name; 1122 Metadata *File; 1123 unsigned Line; 1124 MDString *GetterName; 1125 MDString *SetterName; 1126 unsigned Attributes; 1127 Metadata *Type; 1128 1129 MDNodeKeyImpl(MDString *Name, Metadata *File, unsigned Line, 1130 MDString *GetterName, MDString *SetterName, unsigned Attributes, 1131 Metadata *Type) 1132 : Name(Name), File(File), Line(Line), GetterName(GetterName), 1133 SetterName(SetterName), Attributes(Attributes), Type(Type) {} 1134 MDNodeKeyImpl(const DIObjCProperty *N) 1135 : Name(N->getRawName()), File(N->getRawFile()), Line(N->getLine()), 1136 GetterName(N->getRawGetterName()), SetterName(N->getRawSetterName()), 1137 Attributes(N->getAttributes()), Type(N->getRawType()) {} 1138 1139 bool isKeyOf(const DIObjCProperty *RHS) const { 1140 return Name == RHS->getRawName() && File == RHS->getRawFile() && 1141 Line == RHS->getLine() && GetterName == RHS->getRawGetterName() && 1142 SetterName == RHS->getRawSetterName() && 1143 Attributes == RHS->getAttributes() && Type == RHS->getRawType(); 1144 } 1145 1146 unsigned getHashValue() const { 1147 return hash_combine(Name, File, Line, GetterName, SetterName, Attributes, 1148 Type); 1149 } 1150 }; 1151 1152 template <> struct MDNodeKeyImpl<DIImportedEntity> { 1153 unsigned Tag; 1154 Metadata *Scope; 1155 Metadata *Entity; 1156 Metadata *File; 1157 unsigned Line; 1158 MDString *Name; 1159 1160 MDNodeKeyImpl(unsigned Tag, Metadata *Scope, Metadata *Entity, Metadata *File, 1161 unsigned Line, MDString *Name) 1162 : Tag(Tag), Scope(Scope), Entity(Entity), File(File), Line(Line), 1163 Name(Name) {} 1164 MDNodeKeyImpl(const DIImportedEntity *N) 1165 : Tag(N->getTag()), Scope(N->getRawScope()), Entity(N->getRawEntity()), 1166 File(N->getRawFile()), Line(N->getLine()), Name(N->getRawName()) {} 1167 1168 bool isKeyOf(const DIImportedEntity *RHS) const { 1169 return Tag == RHS->getTag() && Scope == RHS->getRawScope() && 1170 Entity == RHS->getRawEntity() && File == RHS->getFile() && 1171 Line == RHS->getLine() && Name == RHS->getRawName(); 1172 } 1173 1174 unsigned getHashValue() const { 1175 return hash_combine(Tag, Scope, Entity, File, Line, Name); 1176 } 1177 }; 1178 1179 template <> struct MDNodeKeyImpl<DIMacro> { 1180 unsigned MIType; 1181 unsigned Line; 1182 MDString *Name; 1183 MDString *Value; 1184 1185 MDNodeKeyImpl(unsigned MIType, unsigned Line, MDString *Name, MDString *Value) 1186 : MIType(MIType), Line(Line), Name(Name), Value(Value) {} 1187 MDNodeKeyImpl(const DIMacro *N) 1188 : MIType(N->getMacinfoType()), Line(N->getLine()), Name(N->getRawName()), 1189 Value(N->getRawValue()) {} 1190 1191 bool isKeyOf(const DIMacro *RHS) const { 1192 return MIType == RHS->getMacinfoType() && Line == RHS->getLine() && 1193 Name == RHS->getRawName() && Value == RHS->getRawValue(); 1194 } 1195 1196 unsigned getHashValue() const { 1197 return hash_combine(MIType, Line, Name, Value); 1198 } 1199 }; 1200 1201 template <> struct MDNodeKeyImpl<DIMacroFile> { 1202 unsigned MIType; 1203 unsigned Line; 1204 Metadata *File; 1205 Metadata *Elements; 1206 1207 MDNodeKeyImpl(unsigned MIType, unsigned Line, Metadata *File, 1208 Metadata *Elements) 1209 : MIType(MIType), Line(Line), File(File), Elements(Elements) {} 1210 MDNodeKeyImpl(const DIMacroFile *N) 1211 : MIType(N->getMacinfoType()), Line(N->getLine()), File(N->getRawFile()), 1212 Elements(N->getRawElements()) {} 1213 1214 bool isKeyOf(const DIMacroFile *RHS) const { 1215 return MIType == RHS->getMacinfoType() && Line == RHS->getLine() && 1216 File == RHS->getRawFile() && Elements == RHS->getRawElements(); 1217 } 1218 1219 unsigned getHashValue() const { 1220 return hash_combine(MIType, Line, File, Elements); 1221 } 1222 }; 1223 1224 template <> struct MDNodeKeyImpl<DIArgList> { 1225 ArrayRef<ValueAsMetadata *> Args; 1226 1227 MDNodeKeyImpl(ArrayRef<ValueAsMetadata *> Args) : Args(Args) {} 1228 MDNodeKeyImpl(const DIArgList *N) : Args(N->getArgs()) {} 1229 1230 bool isKeyOf(const DIArgList *RHS) const { return Args == RHS->getArgs(); } 1231 1232 unsigned getHashValue() const { 1233 return hash_combine_range(Args.begin(), Args.end()); 1234 } 1235 }; 1236 1237 /// DenseMapInfo for MDNode subclasses. 1238 template <class NodeTy> struct MDNodeInfo { 1239 using KeyTy = MDNodeKeyImpl<NodeTy>; 1240 using SubsetEqualTy = MDNodeSubsetEqualImpl<NodeTy>; 1241 1242 static inline NodeTy *getEmptyKey() { 1243 return DenseMapInfo<NodeTy *>::getEmptyKey(); 1244 } 1245 1246 static inline NodeTy *getTombstoneKey() { 1247 return DenseMapInfo<NodeTy *>::getTombstoneKey(); 1248 } 1249 1250 static unsigned getHashValue(const KeyTy &Key) { return Key.getHashValue(); } 1251 1252 static unsigned getHashValue(const NodeTy *N) { 1253 return KeyTy(N).getHashValue(); 1254 } 1255 1256 static bool isEqual(const KeyTy &LHS, const NodeTy *RHS) { 1257 if (RHS == getEmptyKey() || RHS == getTombstoneKey()) 1258 return false; 1259 return SubsetEqualTy::isSubsetEqual(LHS, RHS) || LHS.isKeyOf(RHS); 1260 } 1261 1262 static bool isEqual(const NodeTy *LHS, const NodeTy *RHS) { 1263 if (LHS == RHS) 1264 return true; 1265 if (RHS == getEmptyKey() || RHS == getTombstoneKey()) 1266 return false; 1267 return SubsetEqualTy::isSubsetEqual(LHS, RHS); 1268 } 1269 }; 1270 1271 #define HANDLE_MDNODE_LEAF(CLASS) using CLASS##Info = MDNodeInfo<CLASS>; 1272 #include "llvm/IR/Metadata.def" 1273 1274 /// Multimap-like storage for metadata attachments. 1275 class MDAttachments { 1276 public: 1277 struct Attachment { 1278 unsigned MDKind; 1279 TrackingMDNodeRef Node; 1280 }; 1281 1282 private: 1283 SmallVector<Attachment, 1> Attachments; 1284 1285 public: 1286 bool empty() const { return Attachments.empty(); } 1287 size_t size() const { return Attachments.size(); } 1288 1289 /// Returns the first attachment with the given ID or nullptr if no such 1290 /// attachment exists. 1291 MDNode *lookup(unsigned ID) const; 1292 1293 /// Appends all attachments with the given ID to \c Result in insertion order. 1294 /// If the global has no attachments with the given ID, or if ID is invalid, 1295 /// leaves Result unchanged. 1296 void get(unsigned ID, SmallVectorImpl<MDNode *> &Result) const; 1297 1298 /// Appends all attachments for the global to \c Result, sorting by attachment 1299 /// ID. Attachments with the same ID appear in insertion order. This function 1300 /// does \em not clear \c Result. 1301 void getAll(SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const; 1302 1303 /// Set an attachment to a particular node. 1304 /// 1305 /// Set the \c ID attachment to \c MD, replacing the current attachments at \c 1306 /// ID (if anyway). 1307 void set(unsigned ID, MDNode *MD); 1308 1309 /// Adds an attachment to a particular node. 1310 void insert(unsigned ID, MDNode &MD); 1311 1312 /// Remove attachments with the given ID. 1313 /// 1314 /// Remove the attachments at \c ID, if any. 1315 bool erase(unsigned ID); 1316 1317 /// Erase matching attachments. 1318 /// 1319 /// Erases all attachments matching the \c shouldRemove predicate. 1320 template <class PredTy> void remove_if(PredTy shouldRemove) { 1321 llvm::erase_if(Attachments, shouldRemove); 1322 } 1323 }; 1324 1325 class LLVMContextImpl { 1326 public: 1327 /// OwnedModules - The set of modules instantiated in this context, and which 1328 /// will be automatically deleted if this context is deleted. 1329 SmallPtrSet<Module*, 4> OwnedModules; 1330 1331 /// The main remark streamer used by all the other streamers (e.g. IR, MIR, 1332 /// frontends, etc.). This should only be used by the specific streamers, and 1333 /// never directly. 1334 std::unique_ptr<remarks::RemarkStreamer> MainRemarkStreamer; 1335 1336 std::unique_ptr<DiagnosticHandler> DiagHandler; 1337 bool RespectDiagnosticFilters = false; 1338 bool DiagnosticsHotnessRequested = false; 1339 /// The minimum hotness value a diagnostic needs in order to be included in 1340 /// optimization diagnostics. 1341 /// 1342 /// The threshold is an Optional value, which maps to one of the 3 states: 1343 /// 1). 0 => threshold disabled. All emarks will be printed. 1344 /// 2). positive int => manual threshold by user. Remarks with hotness exceed 1345 /// threshold will be printed. 1346 /// 3). None => 'auto' threshold by user. The actual value is not 1347 /// available at command line, but will be synced with 1348 /// hotness threhold from profile summary during 1349 /// compilation. 1350 /// 1351 /// State 1 and 2 are considered as terminal states. State transition is 1352 /// only allowed from 3 to 2, when the threshold is first synced with profile 1353 /// summary. This ensures that the threshold is set only once and stays 1354 /// constant. 1355 /// 1356 /// If threshold option is not specified, it is disabled (0) by default. 1357 Optional<uint64_t> DiagnosticsHotnessThreshold = 0; 1358 1359 /// The specialized remark streamer used by LLVM's OptimizationRemarkEmitter. 1360 std::unique_ptr<LLVMRemarkStreamer> LLVMRS; 1361 1362 LLVMContext::YieldCallbackTy YieldCallback = nullptr; 1363 void *YieldOpaqueHandle = nullptr; 1364 1365 using IntMapTy = 1366 DenseMap<APInt, std::unique_ptr<ConstantInt>, DenseMapAPIntKeyInfo>; 1367 IntMapTy IntConstants; 1368 1369 using FPMapTy = 1370 DenseMap<APFloat, std::unique_ptr<ConstantFP>, DenseMapAPFloatKeyInfo>; 1371 FPMapTy FPConstants; 1372 1373 FoldingSet<AttributeImpl> AttrsSet; 1374 FoldingSet<AttributeListImpl> AttrsLists; 1375 FoldingSet<AttributeSetNode> AttrsSetNodes; 1376 1377 StringMap<MDString, BumpPtrAllocator> MDStringCache; 1378 DenseMap<Value *, ValueAsMetadata *> ValuesAsMetadata; 1379 DenseMap<Metadata *, MetadataAsValue *> MetadataAsValues; 1380 1381 DenseMap<const Value*, ValueName*> ValueNames; 1382 1383 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \ 1384 DenseSet<CLASS *, CLASS##Info> CLASS##s; 1385 #include "llvm/IR/Metadata.def" 1386 1387 // Optional map for looking up composite types by identifier. 1388 Optional<DenseMap<const MDString *, DICompositeType *>> DITypeMap; 1389 1390 // MDNodes may be uniqued or not uniqued. When they're not uniqued, they 1391 // aren't in the MDNodeSet, but they're still shared between objects, so no 1392 // one object can destroy them. Keep track of them here so we can delete 1393 // them on context teardown. 1394 std::vector<MDNode *> DistinctMDNodes; 1395 1396 DenseMap<Type *, std::unique_ptr<ConstantAggregateZero>> CAZConstants; 1397 1398 using ArrayConstantsTy = ConstantUniqueMap<ConstantArray>; 1399 ArrayConstantsTy ArrayConstants; 1400 1401 using StructConstantsTy = ConstantUniqueMap<ConstantStruct>; 1402 StructConstantsTy StructConstants; 1403 1404 using VectorConstantsTy = ConstantUniqueMap<ConstantVector>; 1405 VectorConstantsTy VectorConstants; 1406 1407 DenseMap<PointerType *, std::unique_ptr<ConstantPointerNull>> CPNConstants; 1408 1409 DenseMap<Type *, std::unique_ptr<UndefValue>> UVConstants; 1410 1411 DenseMap<Type *, std::unique_ptr<PoisonValue>> PVConstants; 1412 1413 StringMap<std::unique_ptr<ConstantDataSequential>> CDSConstants; 1414 1415 DenseMap<std::pair<const Function *, const BasicBlock *>, BlockAddress *> 1416 BlockAddresses; 1417 1418 DenseMap<const GlobalValue *, DSOLocalEquivalent *> DSOLocalEquivalents; 1419 1420 ConstantUniqueMap<ConstantExpr> ExprConstants; 1421 1422 ConstantUniqueMap<InlineAsm> InlineAsms; 1423 1424 ConstantInt *TheTrueVal = nullptr; 1425 ConstantInt *TheFalseVal = nullptr; 1426 1427 std::unique_ptr<ConstantTokenNone> TheNoneToken; 1428 1429 // Basic type instances. 1430 Type VoidTy, LabelTy, HalfTy, BFloatTy, FloatTy, DoubleTy, MetadataTy, 1431 TokenTy; 1432 Type X86_FP80Ty, FP128Ty, PPC_FP128Ty, X86_MMXTy, X86_AMXTy; 1433 IntegerType Int1Ty, Int8Ty, Int16Ty, Int32Ty, Int64Ty, Int128Ty; 1434 1435 BumpPtrAllocator Alloc; 1436 UniqueStringSaver Saver{Alloc}; 1437 1438 DenseMap<unsigned, IntegerType*> IntegerTypes; 1439 1440 using FunctionTypeSet = DenseSet<FunctionType *, FunctionTypeKeyInfo>; 1441 FunctionTypeSet FunctionTypes; 1442 using StructTypeSet = DenseSet<StructType *, AnonStructTypeKeyInfo>; 1443 StructTypeSet AnonStructTypes; 1444 StringMap<StructType*> NamedStructTypes; 1445 unsigned NamedStructTypesUniqueID = 0; 1446 1447 DenseMap<std::pair<Type *, uint64_t>, ArrayType*> ArrayTypes; 1448 DenseMap<std::pair<Type *, ElementCount>, VectorType*> VectorTypes; 1449 // TODO: clean up the following after we no longer support non-opaque pointer 1450 // types. 1451 bool ForceOpaquePointers; 1452 DenseMap<Type*, PointerType*> PointerTypes; // Pointers in AddrSpace = 0 1453 DenseMap<std::pair<Type*, unsigned>, PointerType*> ASPointerTypes; 1454 1455 /// ValueHandles - This map keeps track of all of the value handles that are 1456 /// watching a Value*. The Value::HasValueHandle bit is used to know 1457 /// whether or not a value has an entry in this map. 1458 using ValueHandlesTy = DenseMap<Value *, ValueHandleBase *>; 1459 ValueHandlesTy ValueHandles; 1460 1461 /// CustomMDKindNames - Map to hold the metadata string to ID mapping. 1462 StringMap<unsigned> CustomMDKindNames; 1463 1464 /// Collection of metadata used in this context. 1465 DenseMap<const Value *, MDAttachments> ValueMetadata; 1466 1467 /// Collection of per-GlobalObject sections used in this context. 1468 DenseMap<const GlobalObject *, StringRef> GlobalObjectSections; 1469 1470 /// Collection of per-GlobalValue partitions used in this context. 1471 DenseMap<const GlobalValue *, StringRef> GlobalValuePartitions; 1472 1473 /// DiscriminatorTable - This table maps file:line locations to an 1474 /// integer representing the next DWARF path discriminator to assign to 1475 /// instructions in different blocks at the same location. 1476 DenseMap<std::pair<const char *, unsigned>, unsigned> DiscriminatorTable; 1477 1478 /// A set of interned tags for operand bundles. The StringMap maps 1479 /// bundle tags to their IDs. 1480 /// 1481 /// \see LLVMContext::getOperandBundleTagID 1482 StringMap<uint32_t> BundleTagCache; 1483 1484 StringMapEntry<uint32_t> *getOrInsertBundleTag(StringRef Tag); 1485 void getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const; 1486 uint32_t getOperandBundleTagID(StringRef Tag) const; 1487 1488 /// A set of interned synchronization scopes. The StringMap maps 1489 /// synchronization scope names to their respective synchronization scope IDs. 1490 StringMap<SyncScope::ID> SSC; 1491 1492 /// getOrInsertSyncScopeID - Maps synchronization scope name to 1493 /// synchronization scope ID. Every synchronization scope registered with 1494 /// LLVMContext has unique ID except pre-defined ones. 1495 SyncScope::ID getOrInsertSyncScopeID(StringRef SSN); 1496 1497 /// getSyncScopeNames - Populates client supplied SmallVector with 1498 /// synchronization scope names registered with LLVMContext. Synchronization 1499 /// scope names are ordered by increasing synchronization scope IDs. 1500 void getSyncScopeNames(SmallVectorImpl<StringRef> &SSNs) const; 1501 1502 /// Maintain the GC name for each function. 1503 /// 1504 /// This saves allocating an additional word in Function for programs which 1505 /// do not use GC (i.e., most programs) at the cost of increased overhead for 1506 /// clients which do use GC. 1507 DenseMap<const Function*, std::string> GCNames; 1508 1509 /// Flag to indicate if Value (other than GlobalValue) retains their name or 1510 /// not. 1511 bool DiscardValueNames = false; 1512 1513 LLVMContextImpl(LLVMContext &C); 1514 ~LLVMContextImpl(); 1515 1516 /// Destroy the ConstantArrays if they are not used. 1517 void dropTriviallyDeadConstantArrays(); 1518 1519 mutable OptPassGate *OPG = nullptr; 1520 1521 /// Access the object which can disable optional passes and individual 1522 /// optimizations at compile time. 1523 OptPassGate &getOptPassGate() const; 1524 1525 /// Set the object which can disable optional passes and individual 1526 /// optimizations at compile time. 1527 /// 1528 /// The lifetime of the object must be guaranteed to extend as long as the 1529 /// LLVMContext is used by compilation. 1530 void setOptPassGate(OptPassGate&); 1531 }; 1532 1533 } // end namespace llvm 1534 1535 #endif // LLVM_LIB_IR_LLVMCONTEXTIMPL_H 1536