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