1 //===- llvm/IR/DebugInfoMetadata.h - Debug info metadata --------*- 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 // Declarations for metadata specific to debug info. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_IR_DEBUGINFOMETADATA_H 14 #define LLVM_IR_DEBUGINFOMETADATA_H 15 16 #include "llvm/ADT/ArrayRef.h" 17 #include "llvm/ADT/BitmaskEnum.h" 18 #include "llvm/ADT/PointerUnion.h" 19 #include "llvm/ADT/STLExtras.h" 20 #include "llvm/ADT/SmallVector.h" 21 #include "llvm/ADT/StringRef.h" 22 #include "llvm/ADT/iterator_range.h" 23 #include "llvm/IR/Constants.h" 24 #include "llvm/IR/Metadata.h" 25 #include "llvm/Support/Casting.h" 26 #include "llvm/Support/CommandLine.h" 27 #include "llvm/Support/Discriminator.h" 28 #include <cassert> 29 #include <climits> 30 #include <cstddef> 31 #include <cstdint> 32 #include <iterator> 33 #include <optional> 34 #include <vector> 35 36 // Helper macros for defining get() overrides. 37 #define DEFINE_MDNODE_GET_UNPACK_IMPL(...) __VA_ARGS__ 38 #define DEFINE_MDNODE_GET_UNPACK(ARGS) DEFINE_MDNODE_GET_UNPACK_IMPL ARGS 39 #define DEFINE_MDNODE_GET_DISTINCT_TEMPORARY(CLASS, FORMAL, ARGS) \ 40 static CLASS *getDistinct(LLVMContext &Context, \ 41 DEFINE_MDNODE_GET_UNPACK(FORMAL)) { \ 42 return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Distinct); \ 43 } \ 44 static Temp##CLASS getTemporary(LLVMContext &Context, \ 45 DEFINE_MDNODE_GET_UNPACK(FORMAL)) { \ 46 return Temp##CLASS( \ 47 getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Temporary)); \ 48 } 49 #define DEFINE_MDNODE_GET(CLASS, FORMAL, ARGS) \ 50 static CLASS *get(LLVMContext &Context, DEFINE_MDNODE_GET_UNPACK(FORMAL)) { \ 51 return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Uniqued); \ 52 } \ 53 static CLASS *getIfExists(LLVMContext &Context, \ 54 DEFINE_MDNODE_GET_UNPACK(FORMAL)) { \ 55 return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Uniqued, \ 56 /* ShouldCreate */ false); \ 57 } \ 58 DEFINE_MDNODE_GET_DISTINCT_TEMPORARY(CLASS, FORMAL, ARGS) 59 60 namespace llvm { 61 62 namespace dwarf { 63 enum Tag : uint16_t; 64 } 65 66 class DbgVariableIntrinsic; 67 68 extern cl::opt<bool> EnableFSDiscriminator; 69 70 class DITypeRefArray { 71 const MDTuple *N = nullptr; 72 73 public: 74 DITypeRefArray() = default; 75 DITypeRefArray(const MDTuple *N) : N(N) {} 76 77 explicit operator bool() const { return get(); } 78 explicit operator MDTuple *() const { return get(); } 79 80 MDTuple *get() const { return const_cast<MDTuple *>(N); } 81 MDTuple *operator->() const { return get(); } 82 MDTuple &operator*() const { return *get(); } 83 84 // FIXME: Fix callers and remove condition on N. 85 unsigned size() const { return N ? N->getNumOperands() : 0u; } 86 DIType *operator[](unsigned I) const { 87 return cast_or_null<DIType>(N->getOperand(I)); 88 } 89 90 class iterator { 91 MDNode::op_iterator I = nullptr; 92 93 public: 94 using iterator_category = std::input_iterator_tag; 95 using value_type = DIType *; 96 using difference_type = std::ptrdiff_t; 97 using pointer = void; 98 using reference = DIType *; 99 100 iterator() = default; 101 explicit iterator(MDNode::op_iterator I) : I(I) {} 102 103 DIType *operator*() const { return cast_or_null<DIType>(*I); } 104 105 iterator &operator++() { 106 ++I; 107 return *this; 108 } 109 110 iterator operator++(int) { 111 iterator Temp(*this); 112 ++I; 113 return Temp; 114 } 115 116 bool operator==(const iterator &X) const { return I == X.I; } 117 bool operator!=(const iterator &X) const { return I != X.I; } 118 }; 119 120 // FIXME: Fix callers and remove condition on N. 121 iterator begin() const { return N ? iterator(N->op_begin()) : iterator(); } 122 iterator end() const { return N ? iterator(N->op_end()) : iterator(); } 123 }; 124 125 /// Tagged DWARF-like metadata node. 126 /// 127 /// A metadata node with a DWARF tag (i.e., a constant named \c DW_TAG_*, 128 /// defined in llvm/BinaryFormat/Dwarf.h). Called \a DINode because it's 129 /// potentially used for non-DWARF output. 130 class DINode : public MDNode { 131 friend class LLVMContextImpl; 132 friend class MDNode; 133 134 protected: 135 DINode(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag, 136 ArrayRef<Metadata *> Ops1, ArrayRef<Metadata *> Ops2 = std::nullopt) 137 : MDNode(C, ID, Storage, Ops1, Ops2) { 138 assert(Tag < 1u << 16); 139 SubclassData16 = Tag; 140 } 141 ~DINode() = default; 142 143 template <class Ty> Ty *getOperandAs(unsigned I) const { 144 return cast_or_null<Ty>(getOperand(I)); 145 } 146 147 StringRef getStringOperand(unsigned I) const { 148 if (auto *S = getOperandAs<MDString>(I)) 149 return S->getString(); 150 return StringRef(); 151 } 152 153 static MDString *getCanonicalMDString(LLVMContext &Context, StringRef S) { 154 if (S.empty()) 155 return nullptr; 156 return MDString::get(Context, S); 157 } 158 159 /// Allow subclasses to mutate the tag. 160 void setTag(unsigned Tag) { SubclassData16 = Tag; } 161 162 public: 163 dwarf::Tag getTag() const; 164 165 /// Debug info flags. 166 /// 167 /// The three accessibility flags are mutually exclusive and rolled together 168 /// in the first two bits. 169 enum DIFlags : uint32_t { 170 #define HANDLE_DI_FLAG(ID, NAME) Flag##NAME = ID, 171 #define DI_FLAG_LARGEST_NEEDED 172 #include "llvm/IR/DebugInfoFlags.def" 173 FlagAccessibility = FlagPrivate | FlagProtected | FlagPublic, 174 FlagPtrToMemberRep = FlagSingleInheritance | FlagMultipleInheritance | 175 FlagVirtualInheritance, 176 LLVM_MARK_AS_BITMASK_ENUM(FlagLargest) 177 }; 178 179 static DIFlags getFlag(StringRef Flag); 180 static StringRef getFlagString(DIFlags Flag); 181 182 /// Split up a flags bitfield. 183 /// 184 /// Split \c Flags into \c SplitFlags, a vector of its components. Returns 185 /// any remaining (unrecognized) bits. 186 static DIFlags splitFlags(DIFlags Flags, 187 SmallVectorImpl<DIFlags> &SplitFlags); 188 189 static bool classof(const Metadata *MD) { 190 switch (MD->getMetadataID()) { 191 default: 192 return false; 193 case GenericDINodeKind: 194 case DISubrangeKind: 195 case DIEnumeratorKind: 196 case DIBasicTypeKind: 197 case DIStringTypeKind: 198 case DIDerivedTypeKind: 199 case DICompositeTypeKind: 200 case DISubroutineTypeKind: 201 case DIFileKind: 202 case DICompileUnitKind: 203 case DISubprogramKind: 204 case DILexicalBlockKind: 205 case DILexicalBlockFileKind: 206 case DINamespaceKind: 207 case DICommonBlockKind: 208 case DITemplateTypeParameterKind: 209 case DITemplateValueParameterKind: 210 case DIGlobalVariableKind: 211 case DILocalVariableKind: 212 case DILabelKind: 213 case DIObjCPropertyKind: 214 case DIImportedEntityKind: 215 case DIModuleKind: 216 case DIGenericSubrangeKind: 217 case DIAssignIDKind: 218 return true; 219 } 220 } 221 }; 222 223 /// Generic tagged DWARF-like metadata node. 224 /// 225 /// An un-specialized DWARF-like metadata node. The first operand is a 226 /// (possibly empty) null-separated \a MDString header that contains arbitrary 227 /// fields. The remaining operands are \a dwarf_operands(), and are pointers 228 /// to other metadata. 229 class GenericDINode : public DINode { 230 friend class LLVMContextImpl; 231 friend class MDNode; 232 233 GenericDINode(LLVMContext &C, StorageType Storage, unsigned Hash, 234 unsigned Tag, ArrayRef<Metadata *> Ops1, 235 ArrayRef<Metadata *> Ops2) 236 : DINode(C, GenericDINodeKind, Storage, Tag, Ops1, Ops2) { 237 setHash(Hash); 238 } 239 ~GenericDINode() { dropAllReferences(); } 240 241 void setHash(unsigned Hash) { SubclassData32 = Hash; } 242 void recalculateHash(); 243 244 static GenericDINode *getImpl(LLVMContext &Context, unsigned Tag, 245 StringRef Header, ArrayRef<Metadata *> DwarfOps, 246 StorageType Storage, bool ShouldCreate = true) { 247 return getImpl(Context, Tag, getCanonicalMDString(Context, Header), 248 DwarfOps, Storage, ShouldCreate); 249 } 250 251 static GenericDINode *getImpl(LLVMContext &Context, unsigned Tag, 252 MDString *Header, ArrayRef<Metadata *> DwarfOps, 253 StorageType Storage, bool ShouldCreate = true); 254 255 TempGenericDINode cloneImpl() const { 256 return getTemporary(getContext(), getTag(), getHeader(), 257 SmallVector<Metadata *, 4>(dwarf_operands())); 258 } 259 260 public: 261 unsigned getHash() const { return SubclassData32; } 262 263 DEFINE_MDNODE_GET(GenericDINode, 264 (unsigned Tag, StringRef Header, 265 ArrayRef<Metadata *> DwarfOps), 266 (Tag, Header, DwarfOps)) 267 DEFINE_MDNODE_GET(GenericDINode, 268 (unsigned Tag, MDString *Header, 269 ArrayRef<Metadata *> DwarfOps), 270 (Tag, Header, DwarfOps)) 271 272 /// Return a (temporary) clone of this. 273 TempGenericDINode clone() const { return cloneImpl(); } 274 275 dwarf::Tag getTag() const; 276 StringRef getHeader() const { return getStringOperand(0); } 277 MDString *getRawHeader() const { return getOperandAs<MDString>(0); } 278 279 op_iterator dwarf_op_begin() const { return op_begin() + 1; } 280 op_iterator dwarf_op_end() const { return op_end(); } 281 op_range dwarf_operands() const { 282 return op_range(dwarf_op_begin(), dwarf_op_end()); 283 } 284 285 unsigned getNumDwarfOperands() const { return getNumOperands() - 1; } 286 const MDOperand &getDwarfOperand(unsigned I) const { 287 return getOperand(I + 1); 288 } 289 void replaceDwarfOperandWith(unsigned I, Metadata *New) { 290 replaceOperandWith(I + 1, New); 291 } 292 293 static bool classof(const Metadata *MD) { 294 return MD->getMetadataID() == GenericDINodeKind; 295 } 296 }; 297 298 /// Assignment ID. 299 /// Used to link stores (as an attachment) and dbg.assigns (as an operand). 300 /// DIAssignID metadata is never uniqued as we compare instances using 301 /// referential equality (the instance/address is the ID). 302 class DIAssignID : public MDNode { 303 friend class LLVMContextImpl; 304 friend class MDNode; 305 306 DIAssignID(LLVMContext &C, StorageType Storage) 307 : MDNode(C, DIAssignIDKind, Storage, std::nullopt) {} 308 309 ~DIAssignID() { dropAllReferences(); } 310 311 static DIAssignID *getImpl(LLVMContext &Context, StorageType Storage, 312 bool ShouldCreate = true); 313 314 TempDIAssignID cloneImpl() const { return getTemporary(getContext()); } 315 316 public: 317 // This node has no operands to replace. 318 void replaceOperandWith(unsigned I, Metadata *New) = delete; 319 320 static DIAssignID *getDistinct(LLVMContext &Context) { 321 return getImpl(Context, Distinct); 322 } 323 static TempDIAssignID getTemporary(LLVMContext &Context) { 324 return TempDIAssignID(getImpl(Context, Temporary)); 325 } 326 // NOTE: Do not define get(LLVMContext&) - see class comment. 327 328 static bool classof(const Metadata *MD) { 329 return MD->getMetadataID() == DIAssignIDKind; 330 } 331 }; 332 333 /// Array subrange. 334 /// 335 /// TODO: Merge into node for DW_TAG_array_type, which should have a custom 336 /// type. 337 class DISubrange : public DINode { 338 friend class LLVMContextImpl; 339 friend class MDNode; 340 341 DISubrange(LLVMContext &C, StorageType Storage, ArrayRef<Metadata *> Ops); 342 343 ~DISubrange() = default; 344 345 static DISubrange *getImpl(LLVMContext &Context, int64_t Count, 346 int64_t LowerBound, StorageType Storage, 347 bool ShouldCreate = true); 348 349 static DISubrange *getImpl(LLVMContext &Context, Metadata *CountNode, 350 int64_t LowerBound, StorageType Storage, 351 bool ShouldCreate = true); 352 353 static DISubrange *getImpl(LLVMContext &Context, Metadata *CountNode, 354 Metadata *LowerBound, Metadata *UpperBound, 355 Metadata *Stride, StorageType Storage, 356 bool ShouldCreate = true); 357 358 TempDISubrange cloneImpl() const { 359 return getTemporary(getContext(), getRawCountNode(), getRawLowerBound(), 360 getRawUpperBound(), getRawStride()); 361 } 362 363 public: 364 DEFINE_MDNODE_GET(DISubrange, (int64_t Count, int64_t LowerBound = 0), 365 (Count, LowerBound)) 366 367 DEFINE_MDNODE_GET(DISubrange, (Metadata * CountNode, int64_t LowerBound = 0), 368 (CountNode, LowerBound)) 369 370 DEFINE_MDNODE_GET(DISubrange, 371 (Metadata * CountNode, Metadata *LowerBound, 372 Metadata *UpperBound, Metadata *Stride), 373 (CountNode, LowerBound, UpperBound, Stride)) 374 375 TempDISubrange clone() const { return cloneImpl(); } 376 377 Metadata *getRawCountNode() const { return getOperand(0).get(); } 378 379 Metadata *getRawLowerBound() const { return getOperand(1).get(); } 380 381 Metadata *getRawUpperBound() const { return getOperand(2).get(); } 382 383 Metadata *getRawStride() const { return getOperand(3).get(); } 384 385 typedef PointerUnion<ConstantInt *, DIVariable *, DIExpression *> BoundType; 386 387 BoundType getCount() const; 388 389 BoundType getLowerBound() const; 390 391 BoundType getUpperBound() const; 392 393 BoundType getStride() const; 394 395 static bool classof(const Metadata *MD) { 396 return MD->getMetadataID() == DISubrangeKind; 397 } 398 }; 399 400 class DIGenericSubrange : public DINode { 401 friend class LLVMContextImpl; 402 friend class MDNode; 403 404 DIGenericSubrange(LLVMContext &C, StorageType Storage, 405 ArrayRef<Metadata *> Ops); 406 407 ~DIGenericSubrange() = default; 408 409 static DIGenericSubrange *getImpl(LLVMContext &Context, Metadata *CountNode, 410 Metadata *LowerBound, Metadata *UpperBound, 411 Metadata *Stride, StorageType Storage, 412 bool ShouldCreate = true); 413 414 TempDIGenericSubrange cloneImpl() const { 415 return getTemporary(getContext(), getRawCountNode(), getRawLowerBound(), 416 getRawUpperBound(), getRawStride()); 417 } 418 419 public: 420 DEFINE_MDNODE_GET(DIGenericSubrange, 421 (Metadata * CountNode, Metadata *LowerBound, 422 Metadata *UpperBound, Metadata *Stride), 423 (CountNode, LowerBound, UpperBound, Stride)) 424 425 TempDIGenericSubrange clone() const { return cloneImpl(); } 426 427 Metadata *getRawCountNode() const { return getOperand(0).get(); } 428 Metadata *getRawLowerBound() const { return getOperand(1).get(); } 429 Metadata *getRawUpperBound() const { return getOperand(2).get(); } 430 Metadata *getRawStride() const { return getOperand(3).get(); } 431 432 using BoundType = PointerUnion<DIVariable *, DIExpression *>; 433 434 BoundType getCount() const; 435 BoundType getLowerBound() const; 436 BoundType getUpperBound() const; 437 BoundType getStride() const; 438 439 static bool classof(const Metadata *MD) { 440 return MD->getMetadataID() == DIGenericSubrangeKind; 441 } 442 }; 443 444 /// Enumeration value. 445 /// 446 /// TODO: Add a pointer to the context (DW_TAG_enumeration_type) once that no 447 /// longer creates a type cycle. 448 class DIEnumerator : public DINode { 449 friend class LLVMContextImpl; 450 friend class MDNode; 451 452 APInt Value; 453 DIEnumerator(LLVMContext &C, StorageType Storage, const APInt &Value, 454 bool IsUnsigned, ArrayRef<Metadata *> Ops); 455 DIEnumerator(LLVMContext &C, StorageType Storage, int64_t Value, 456 bool IsUnsigned, ArrayRef<Metadata *> Ops) 457 : DIEnumerator(C, Storage, APInt(64, Value, !IsUnsigned), IsUnsigned, 458 Ops) {} 459 ~DIEnumerator() = default; 460 461 static DIEnumerator *getImpl(LLVMContext &Context, const APInt &Value, 462 bool IsUnsigned, StringRef Name, 463 StorageType Storage, bool ShouldCreate = true) { 464 return getImpl(Context, Value, IsUnsigned, 465 getCanonicalMDString(Context, Name), Storage, ShouldCreate); 466 } 467 static DIEnumerator *getImpl(LLVMContext &Context, const APInt &Value, 468 bool IsUnsigned, MDString *Name, 469 StorageType Storage, bool ShouldCreate = true); 470 471 TempDIEnumerator cloneImpl() const { 472 return getTemporary(getContext(), getValue(), isUnsigned(), getName()); 473 } 474 475 public: 476 DEFINE_MDNODE_GET(DIEnumerator, 477 (int64_t Value, bool IsUnsigned, StringRef Name), 478 (APInt(64, Value, !IsUnsigned), IsUnsigned, Name)) 479 DEFINE_MDNODE_GET(DIEnumerator, 480 (int64_t Value, bool IsUnsigned, MDString *Name), 481 (APInt(64, Value, !IsUnsigned), IsUnsigned, Name)) 482 DEFINE_MDNODE_GET(DIEnumerator, 483 (APInt Value, bool IsUnsigned, StringRef Name), 484 (Value, IsUnsigned, Name)) 485 DEFINE_MDNODE_GET(DIEnumerator, 486 (APInt Value, bool IsUnsigned, MDString *Name), 487 (Value, IsUnsigned, Name)) 488 489 TempDIEnumerator clone() const { return cloneImpl(); } 490 491 const APInt &getValue() const { return Value; } 492 bool isUnsigned() const { return SubclassData32; } 493 StringRef getName() const { return getStringOperand(0); } 494 495 MDString *getRawName() const { return getOperandAs<MDString>(0); } 496 497 static bool classof(const Metadata *MD) { 498 return MD->getMetadataID() == DIEnumeratorKind; 499 } 500 }; 501 502 /// Base class for scope-like contexts. 503 /// 504 /// Base class for lexical scopes and types (which are also declaration 505 /// contexts). 506 /// 507 /// TODO: Separate the concepts of declaration contexts and lexical scopes. 508 class DIScope : public DINode { 509 protected: 510 DIScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag, 511 ArrayRef<Metadata *> Ops) 512 : DINode(C, ID, Storage, Tag, Ops) {} 513 ~DIScope() = default; 514 515 public: 516 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); } 517 518 inline StringRef getFilename() const; 519 inline StringRef getDirectory() const; 520 inline std::optional<StringRef> getSource() const; 521 522 StringRef getName() const; 523 DIScope *getScope() const; 524 525 /// Return the raw underlying file. 526 /// 527 /// A \a DIFile is a \a DIScope, but it doesn't point at a separate file (it 528 /// \em is the file). If \c this is an \a DIFile, we need to return \c this. 529 /// Otherwise, return the first operand, which is where all other subclasses 530 /// store their file pointer. 531 Metadata *getRawFile() const { 532 return isa<DIFile>(this) ? const_cast<DIScope *>(this) 533 : static_cast<Metadata *>(getOperand(0)); 534 } 535 536 static bool classof(const Metadata *MD) { 537 switch (MD->getMetadataID()) { 538 default: 539 return false; 540 case DIBasicTypeKind: 541 case DIStringTypeKind: 542 case DIDerivedTypeKind: 543 case DICompositeTypeKind: 544 case DISubroutineTypeKind: 545 case DIFileKind: 546 case DICompileUnitKind: 547 case DISubprogramKind: 548 case DILexicalBlockKind: 549 case DILexicalBlockFileKind: 550 case DINamespaceKind: 551 case DICommonBlockKind: 552 case DIModuleKind: 553 return true; 554 } 555 } 556 }; 557 558 /// File. 559 /// 560 /// TODO: Merge with directory/file node (including users). 561 /// TODO: Canonicalize paths on creation. 562 class DIFile : public DIScope { 563 friend class LLVMContextImpl; 564 friend class MDNode; 565 566 public: 567 /// Which algorithm (e.g. MD5) a checksum was generated with. 568 /// 569 /// The encoding is explicit because it is used directly in Bitcode. The 570 /// value 0 is reserved to indicate the absence of a checksum in Bitcode. 571 enum ChecksumKind { 572 // The first variant was originally CSK_None, encoded as 0. The new 573 // internal representation removes the need for this by wrapping the 574 // ChecksumInfo in an Optional, but to preserve Bitcode compatibility the 0 575 // encoding is reserved. 576 CSK_MD5 = 1, 577 CSK_SHA1 = 2, 578 CSK_SHA256 = 3, 579 CSK_Last = CSK_SHA256 // Should be last enumeration. 580 }; 581 582 /// A single checksum, represented by a \a Kind and a \a Value (a string). 583 template <typename T> struct ChecksumInfo { 584 /// The kind of checksum which \a Value encodes. 585 ChecksumKind Kind; 586 /// The string value of the checksum. 587 T Value; 588 589 ChecksumInfo(ChecksumKind Kind, T Value) : Kind(Kind), Value(Value) {} 590 ~ChecksumInfo() = default; 591 bool operator==(const ChecksumInfo<T> &X) const { 592 return Kind == X.Kind && Value == X.Value; 593 } 594 bool operator!=(const ChecksumInfo<T> &X) const { return !(*this == X); } 595 StringRef getKindAsString() const { return getChecksumKindAsString(Kind); } 596 }; 597 598 private: 599 std::optional<ChecksumInfo<MDString *>> Checksum; 600 /// An optional source. A nullptr means none. 601 MDString *Source; 602 603 DIFile(LLVMContext &C, StorageType Storage, 604 std::optional<ChecksumInfo<MDString *>> CS, MDString *Src, 605 ArrayRef<Metadata *> Ops); 606 ~DIFile() = default; 607 608 static DIFile *getImpl(LLVMContext &Context, StringRef Filename, 609 StringRef Directory, 610 std::optional<ChecksumInfo<StringRef>> CS, 611 std::optional<StringRef> Source, StorageType Storage, 612 bool ShouldCreate = true) { 613 std::optional<ChecksumInfo<MDString *>> MDChecksum; 614 if (CS) 615 MDChecksum.emplace(CS->Kind, getCanonicalMDString(Context, CS->Value)); 616 return getImpl(Context, getCanonicalMDString(Context, Filename), 617 getCanonicalMDString(Context, Directory), MDChecksum, 618 Source ? MDString::get(Context, *Source) : nullptr, Storage, 619 ShouldCreate); 620 } 621 static DIFile *getImpl(LLVMContext &Context, MDString *Filename, 622 MDString *Directory, 623 std::optional<ChecksumInfo<MDString *>> CS, 624 MDString *Source, StorageType Storage, 625 bool ShouldCreate = true); 626 627 TempDIFile cloneImpl() const { 628 return getTemporary(getContext(), getFilename(), getDirectory(), 629 getChecksum(), getSource()); 630 } 631 632 public: 633 DEFINE_MDNODE_GET(DIFile, 634 (StringRef Filename, StringRef Directory, 635 std::optional<ChecksumInfo<StringRef>> CS = std::nullopt, 636 std::optional<StringRef> Source = std::nullopt), 637 (Filename, Directory, CS, Source)) 638 DEFINE_MDNODE_GET(DIFile, 639 (MDString * Filename, MDString *Directory, 640 std::optional<ChecksumInfo<MDString *>> CS = std::nullopt, 641 MDString *Source = nullptr), 642 (Filename, Directory, CS, Source)) 643 644 TempDIFile clone() const { return cloneImpl(); } 645 646 StringRef getFilename() const { return getStringOperand(0); } 647 StringRef getDirectory() const { return getStringOperand(1); } 648 std::optional<ChecksumInfo<StringRef>> getChecksum() const { 649 std::optional<ChecksumInfo<StringRef>> StringRefChecksum; 650 if (Checksum) 651 StringRefChecksum.emplace(Checksum->Kind, Checksum->Value->getString()); 652 return StringRefChecksum; 653 } 654 std::optional<StringRef> getSource() const { 655 return Source ? std::optional<StringRef>(Source->getString()) 656 : std::nullopt; 657 } 658 659 MDString *getRawFilename() const { return getOperandAs<MDString>(0); } 660 MDString *getRawDirectory() const { return getOperandAs<MDString>(1); } 661 std::optional<ChecksumInfo<MDString *>> getRawChecksum() const { 662 return Checksum; 663 } 664 MDString *getRawSource() const { return Source; } 665 666 static StringRef getChecksumKindAsString(ChecksumKind CSKind); 667 static std::optional<ChecksumKind> getChecksumKind(StringRef CSKindStr); 668 669 static bool classof(const Metadata *MD) { 670 return MD->getMetadataID() == DIFileKind; 671 } 672 }; 673 674 StringRef DIScope::getFilename() const { 675 if (auto *F = getFile()) 676 return F->getFilename(); 677 return ""; 678 } 679 680 StringRef DIScope::getDirectory() const { 681 if (auto *F = getFile()) 682 return F->getDirectory(); 683 return ""; 684 } 685 686 std::optional<StringRef> DIScope::getSource() const { 687 if (auto *F = getFile()) 688 return F->getSource(); 689 return std::nullopt; 690 } 691 692 /// Base class for types. 693 /// 694 /// TODO: Remove the hardcoded name and context, since many types don't use 695 /// them. 696 /// TODO: Split up flags. 697 class DIType : public DIScope { 698 unsigned Line; 699 DIFlags Flags; 700 uint64_t SizeInBits; 701 uint64_t OffsetInBits; 702 uint32_t AlignInBits; 703 704 protected: 705 DIType(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag, 706 unsigned Line, uint64_t SizeInBits, uint32_t AlignInBits, 707 uint64_t OffsetInBits, DIFlags Flags, ArrayRef<Metadata *> Ops) 708 : DIScope(C, ID, Storage, Tag, Ops) { 709 init(Line, SizeInBits, AlignInBits, OffsetInBits, Flags); 710 } 711 ~DIType() = default; 712 713 void init(unsigned Line, uint64_t SizeInBits, uint32_t AlignInBits, 714 uint64_t OffsetInBits, DIFlags Flags) { 715 this->Line = Line; 716 this->Flags = Flags; 717 this->SizeInBits = SizeInBits; 718 this->AlignInBits = AlignInBits; 719 this->OffsetInBits = OffsetInBits; 720 } 721 722 /// Change fields in place. 723 void mutate(unsigned Tag, unsigned Line, uint64_t SizeInBits, 724 uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags) { 725 assert(isDistinct() && "Only distinct nodes can mutate"); 726 setTag(Tag); 727 init(Line, SizeInBits, AlignInBits, OffsetInBits, Flags); 728 } 729 730 public: 731 TempDIType clone() const { 732 return TempDIType(cast<DIType>(MDNode::clone().release())); 733 } 734 735 unsigned getLine() const { return Line; } 736 uint64_t getSizeInBits() const { return SizeInBits; } 737 uint32_t getAlignInBits() const { return AlignInBits; } 738 uint32_t getAlignInBytes() const { return getAlignInBits() / CHAR_BIT; } 739 uint64_t getOffsetInBits() const { return OffsetInBits; } 740 DIFlags getFlags() const { return Flags; } 741 742 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); } 743 StringRef getName() const { return getStringOperand(2); } 744 745 Metadata *getRawScope() const { return getOperand(1); } 746 MDString *getRawName() const { return getOperandAs<MDString>(2); } 747 748 /// Returns a new temporary DIType with updated Flags 749 TempDIType cloneWithFlags(DIFlags NewFlags) const { 750 auto NewTy = clone(); 751 NewTy->Flags = NewFlags; 752 return NewTy; 753 } 754 755 bool isPrivate() const { 756 return (getFlags() & FlagAccessibility) == FlagPrivate; 757 } 758 bool isProtected() const { 759 return (getFlags() & FlagAccessibility) == FlagProtected; 760 } 761 bool isPublic() const { 762 return (getFlags() & FlagAccessibility) == FlagPublic; 763 } 764 bool isForwardDecl() const { return getFlags() & FlagFwdDecl; } 765 bool isAppleBlockExtension() const { return getFlags() & FlagAppleBlock; } 766 bool isVirtual() const { return getFlags() & FlagVirtual; } 767 bool isArtificial() const { return getFlags() & FlagArtificial; } 768 bool isObjectPointer() const { return getFlags() & FlagObjectPointer; } 769 bool isObjcClassComplete() const { 770 return getFlags() & FlagObjcClassComplete; 771 } 772 bool isVector() const { return getFlags() & FlagVector; } 773 bool isBitField() const { return getFlags() & FlagBitField; } 774 bool isStaticMember() const { return getFlags() & FlagStaticMember; } 775 bool isLValueReference() const { return getFlags() & FlagLValueReference; } 776 bool isRValueReference() const { return getFlags() & FlagRValueReference; } 777 bool isTypePassByValue() const { return getFlags() & FlagTypePassByValue; } 778 bool isTypePassByReference() const { 779 return getFlags() & FlagTypePassByReference; 780 } 781 bool isBigEndian() const { return getFlags() & FlagBigEndian; } 782 bool isLittleEndian() const { return getFlags() & FlagLittleEndian; } 783 bool getExportSymbols() const { return getFlags() & FlagExportSymbols; } 784 785 static bool classof(const Metadata *MD) { 786 switch (MD->getMetadataID()) { 787 default: 788 return false; 789 case DIBasicTypeKind: 790 case DIStringTypeKind: 791 case DIDerivedTypeKind: 792 case DICompositeTypeKind: 793 case DISubroutineTypeKind: 794 return true; 795 } 796 } 797 }; 798 799 /// Basic type, like 'int' or 'float'. 800 /// 801 /// TODO: Split out DW_TAG_unspecified_type. 802 /// TODO: Drop unused accessors. 803 class DIBasicType : public DIType { 804 friend class LLVMContextImpl; 805 friend class MDNode; 806 807 unsigned Encoding; 808 809 DIBasicType(LLVMContext &C, StorageType Storage, unsigned Tag, 810 uint64_t SizeInBits, uint32_t AlignInBits, unsigned Encoding, 811 DIFlags Flags, ArrayRef<Metadata *> Ops) 812 : DIType(C, DIBasicTypeKind, Storage, Tag, 0, SizeInBits, AlignInBits, 0, 813 Flags, Ops), 814 Encoding(Encoding) {} 815 ~DIBasicType() = default; 816 817 static DIBasicType *getImpl(LLVMContext &Context, unsigned Tag, 818 StringRef Name, uint64_t SizeInBits, 819 uint32_t AlignInBits, unsigned Encoding, 820 DIFlags Flags, StorageType Storage, 821 bool ShouldCreate = true) { 822 return getImpl(Context, Tag, getCanonicalMDString(Context, Name), 823 SizeInBits, AlignInBits, Encoding, Flags, Storage, 824 ShouldCreate); 825 } 826 static DIBasicType *getImpl(LLVMContext &Context, unsigned Tag, 827 MDString *Name, uint64_t SizeInBits, 828 uint32_t AlignInBits, unsigned Encoding, 829 DIFlags Flags, StorageType Storage, 830 bool ShouldCreate = true); 831 832 TempDIBasicType cloneImpl() const { 833 return getTemporary(getContext(), getTag(), getName(), getSizeInBits(), 834 getAlignInBits(), getEncoding(), getFlags()); 835 } 836 837 public: 838 DEFINE_MDNODE_GET(DIBasicType, (unsigned Tag, StringRef Name), 839 (Tag, Name, 0, 0, 0, FlagZero)) 840 DEFINE_MDNODE_GET(DIBasicType, 841 (unsigned Tag, StringRef Name, uint64_t SizeInBits), 842 (Tag, Name, SizeInBits, 0, 0, FlagZero)) 843 DEFINE_MDNODE_GET(DIBasicType, 844 (unsigned Tag, MDString *Name, uint64_t SizeInBits), 845 (Tag, Name, SizeInBits, 0, 0, FlagZero)) 846 DEFINE_MDNODE_GET(DIBasicType, 847 (unsigned Tag, StringRef Name, uint64_t SizeInBits, 848 uint32_t AlignInBits, unsigned Encoding, DIFlags Flags), 849 (Tag, Name, SizeInBits, AlignInBits, Encoding, Flags)) 850 DEFINE_MDNODE_GET(DIBasicType, 851 (unsigned Tag, MDString *Name, uint64_t SizeInBits, 852 uint32_t AlignInBits, unsigned Encoding, DIFlags Flags), 853 (Tag, Name, SizeInBits, AlignInBits, Encoding, Flags)) 854 855 TempDIBasicType clone() const { return cloneImpl(); } 856 857 unsigned getEncoding() const { return Encoding; } 858 859 enum class Signedness { Signed, Unsigned }; 860 861 /// Return the signedness of this type, or std::nullopt if this type is 862 /// neither signed nor unsigned. 863 std::optional<Signedness> getSignedness() const; 864 865 static bool classof(const Metadata *MD) { 866 return MD->getMetadataID() == DIBasicTypeKind; 867 } 868 }; 869 870 /// String type, Fortran CHARACTER(n) 871 class DIStringType : public DIType { 872 friend class LLVMContextImpl; 873 friend class MDNode; 874 875 unsigned Encoding; 876 877 DIStringType(LLVMContext &C, StorageType Storage, unsigned Tag, 878 uint64_t SizeInBits, uint32_t AlignInBits, unsigned Encoding, 879 ArrayRef<Metadata *> Ops) 880 : DIType(C, DIStringTypeKind, Storage, Tag, 0, SizeInBits, AlignInBits, 0, 881 FlagZero, Ops), 882 Encoding(Encoding) {} 883 ~DIStringType() = default; 884 885 static DIStringType *getImpl(LLVMContext &Context, unsigned Tag, 886 StringRef Name, Metadata *StringLength, 887 Metadata *StrLenExp, Metadata *StrLocationExp, 888 uint64_t SizeInBits, uint32_t AlignInBits, 889 unsigned Encoding, StorageType Storage, 890 bool ShouldCreate = true) { 891 return getImpl(Context, Tag, getCanonicalMDString(Context, Name), 892 StringLength, StrLenExp, StrLocationExp, SizeInBits, 893 AlignInBits, Encoding, Storage, ShouldCreate); 894 } 895 static DIStringType *getImpl(LLVMContext &Context, unsigned Tag, 896 MDString *Name, Metadata *StringLength, 897 Metadata *StrLenExp, Metadata *StrLocationExp, 898 uint64_t SizeInBits, uint32_t AlignInBits, 899 unsigned Encoding, StorageType Storage, 900 bool ShouldCreate = true); 901 902 TempDIStringType cloneImpl() const { 903 return getTemporary(getContext(), getTag(), getRawName(), 904 getRawStringLength(), getRawStringLengthExp(), 905 getRawStringLocationExp(), getSizeInBits(), 906 getAlignInBits(), getEncoding()); 907 } 908 909 public: 910 DEFINE_MDNODE_GET(DIStringType, 911 (unsigned Tag, StringRef Name, uint64_t SizeInBits, 912 uint32_t AlignInBits), 913 (Tag, Name, nullptr, nullptr, nullptr, SizeInBits, 914 AlignInBits, 0)) 915 DEFINE_MDNODE_GET(DIStringType, 916 (unsigned Tag, MDString *Name, Metadata *StringLength, 917 Metadata *StringLengthExp, Metadata *StringLocationExp, 918 uint64_t SizeInBits, uint32_t AlignInBits, 919 unsigned Encoding), 920 (Tag, Name, StringLength, StringLengthExp, 921 StringLocationExp, SizeInBits, AlignInBits, Encoding)) 922 DEFINE_MDNODE_GET(DIStringType, 923 (unsigned Tag, StringRef Name, Metadata *StringLength, 924 Metadata *StringLengthExp, Metadata *StringLocationExp, 925 uint64_t SizeInBits, uint32_t AlignInBits, 926 unsigned Encoding), 927 (Tag, Name, StringLength, StringLengthExp, 928 StringLocationExp, SizeInBits, AlignInBits, Encoding)) 929 930 TempDIStringType clone() const { return cloneImpl(); } 931 932 static bool classof(const Metadata *MD) { 933 return MD->getMetadataID() == DIStringTypeKind; 934 } 935 936 DIVariable *getStringLength() const { 937 return cast_or_null<DIVariable>(getRawStringLength()); 938 } 939 940 DIExpression *getStringLengthExp() const { 941 return cast_or_null<DIExpression>(getRawStringLengthExp()); 942 } 943 944 DIExpression *getStringLocationExp() const { 945 return cast_or_null<DIExpression>(getRawStringLocationExp()); 946 } 947 948 unsigned getEncoding() const { return Encoding; } 949 950 Metadata *getRawStringLength() const { return getOperand(3); } 951 952 Metadata *getRawStringLengthExp() const { return getOperand(4); } 953 954 Metadata *getRawStringLocationExp() const { return getOperand(5); } 955 }; 956 957 /// Derived types. 958 /// 959 /// This includes qualified types, pointers, references, friends, typedefs, and 960 /// class members. 961 /// 962 /// TODO: Split out members (inheritance, fields, methods, etc.). 963 class DIDerivedType : public DIType { 964 friend class LLVMContextImpl; 965 friend class MDNode; 966 967 /// The DWARF address space of the memory pointed to or referenced by a 968 /// pointer or reference type respectively. 969 std::optional<unsigned> DWARFAddressSpace; 970 971 DIDerivedType(LLVMContext &C, StorageType Storage, unsigned Tag, 972 unsigned Line, uint64_t SizeInBits, uint32_t AlignInBits, 973 uint64_t OffsetInBits, 974 std::optional<unsigned> DWARFAddressSpace, DIFlags Flags, 975 ArrayRef<Metadata *> Ops) 976 : DIType(C, DIDerivedTypeKind, Storage, Tag, Line, SizeInBits, 977 AlignInBits, OffsetInBits, Flags, Ops), 978 DWARFAddressSpace(DWARFAddressSpace) {} 979 ~DIDerivedType() = default; 980 static DIDerivedType * 981 getImpl(LLVMContext &Context, unsigned Tag, StringRef Name, DIFile *File, 982 unsigned Line, DIScope *Scope, DIType *BaseType, uint64_t SizeInBits, 983 uint32_t AlignInBits, uint64_t OffsetInBits, 984 std::optional<unsigned> DWARFAddressSpace, DIFlags Flags, 985 Metadata *ExtraData, DINodeArray Annotations, StorageType Storage, 986 bool ShouldCreate = true) { 987 return getImpl(Context, Tag, getCanonicalMDString(Context, Name), File, 988 Line, Scope, BaseType, SizeInBits, AlignInBits, OffsetInBits, 989 DWARFAddressSpace, Flags, ExtraData, Annotations.get(), 990 Storage, ShouldCreate); 991 } 992 static DIDerivedType * 993 getImpl(LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File, 994 unsigned Line, Metadata *Scope, Metadata *BaseType, 995 uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits, 996 std::optional<unsigned> DWARFAddressSpace, DIFlags Flags, 997 Metadata *ExtraData, Metadata *Annotations, StorageType Storage, 998 bool ShouldCreate = true); 999 1000 TempDIDerivedType cloneImpl() const { 1001 return getTemporary( 1002 getContext(), getTag(), getName(), getFile(), getLine(), getScope(), 1003 getBaseType(), getSizeInBits(), getAlignInBits(), getOffsetInBits(), 1004 getDWARFAddressSpace(), getFlags(), getExtraData(), getAnnotations()); 1005 } 1006 1007 public: 1008 DEFINE_MDNODE_GET( 1009 DIDerivedType, 1010 (unsigned Tag, MDString *Name, Metadata *File, unsigned Line, 1011 Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits, 1012 uint32_t AlignInBits, uint64_t OffsetInBits, 1013 std::optional<unsigned> DWARFAddressSpace, DIFlags Flags, 1014 Metadata *ExtraData = nullptr, Metadata *Annotations = nullptr), 1015 (Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits, 1016 OffsetInBits, DWARFAddressSpace, Flags, ExtraData, Annotations)) 1017 DEFINE_MDNODE_GET(DIDerivedType, 1018 (unsigned Tag, StringRef Name, DIFile *File, unsigned Line, 1019 DIScope *Scope, DIType *BaseType, uint64_t SizeInBits, 1020 uint32_t AlignInBits, uint64_t OffsetInBits, 1021 std::optional<unsigned> DWARFAddressSpace, DIFlags Flags, 1022 Metadata *ExtraData = nullptr, 1023 DINodeArray Annotations = nullptr), 1024 (Tag, Name, File, Line, Scope, BaseType, SizeInBits, 1025 AlignInBits, OffsetInBits, DWARFAddressSpace, Flags, 1026 ExtraData, Annotations)) 1027 1028 TempDIDerivedType clone() const { return cloneImpl(); } 1029 1030 /// Get the base type this is derived from. 1031 DIType *getBaseType() const { return cast_or_null<DIType>(getRawBaseType()); } 1032 Metadata *getRawBaseType() const { return getOperand(3); } 1033 1034 /// \returns The DWARF address space of the memory pointed to or referenced by 1035 /// a pointer or reference type respectively. 1036 std::optional<unsigned> getDWARFAddressSpace() const { 1037 return DWARFAddressSpace; 1038 } 1039 1040 /// Get extra data associated with this derived type. 1041 /// 1042 /// Class type for pointer-to-members, objective-c property node for ivars, 1043 /// global constant wrapper for static members, or virtual base pointer offset 1044 /// for inheritance. 1045 /// 1046 /// TODO: Separate out types that need this extra operand: pointer-to-member 1047 /// types and member fields (static members and ivars). 1048 Metadata *getExtraData() const { return getRawExtraData(); } 1049 Metadata *getRawExtraData() const { return getOperand(4); } 1050 1051 /// Get annotations associated with this derived type. 1052 DINodeArray getAnnotations() const { 1053 return cast_or_null<MDTuple>(getRawAnnotations()); 1054 } 1055 Metadata *getRawAnnotations() const { return getOperand(5); } 1056 1057 /// Get casted version of extra data. 1058 /// @{ 1059 DIType *getClassType() const; 1060 1061 DIObjCProperty *getObjCProperty() const { 1062 return dyn_cast_or_null<DIObjCProperty>(getExtraData()); 1063 } 1064 1065 uint32_t getVBPtrOffset() const; 1066 1067 Constant *getStorageOffsetInBits() const; 1068 1069 Constant *getConstant() const; 1070 1071 Constant *getDiscriminantValue() const; 1072 /// @} 1073 1074 static bool classof(const Metadata *MD) { 1075 return MD->getMetadataID() == DIDerivedTypeKind; 1076 } 1077 }; 1078 1079 /// Composite types. 1080 /// 1081 /// TODO: Detach from DerivedTypeBase (split out MDEnumType?). 1082 /// TODO: Create a custom, unrelated node for DW_TAG_array_type. 1083 class DICompositeType : public DIType { 1084 friend class LLVMContextImpl; 1085 friend class MDNode; 1086 1087 unsigned RuntimeLang; 1088 1089 DICompositeType(LLVMContext &C, StorageType Storage, unsigned Tag, 1090 unsigned Line, unsigned RuntimeLang, uint64_t SizeInBits, 1091 uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags, 1092 ArrayRef<Metadata *> Ops) 1093 : DIType(C, DICompositeTypeKind, Storage, Tag, Line, SizeInBits, 1094 AlignInBits, OffsetInBits, Flags, Ops), 1095 RuntimeLang(RuntimeLang) {} 1096 ~DICompositeType() = default; 1097 1098 /// Change fields in place. 1099 void mutate(unsigned Tag, unsigned Line, unsigned RuntimeLang, 1100 uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits, 1101 DIFlags Flags) { 1102 assert(isDistinct() && "Only distinct nodes can mutate"); 1103 assert(getRawIdentifier() && "Only ODR-uniqued nodes should mutate"); 1104 this->RuntimeLang = RuntimeLang; 1105 DIType::mutate(Tag, Line, SizeInBits, AlignInBits, OffsetInBits, Flags); 1106 } 1107 1108 static DICompositeType * 1109 getImpl(LLVMContext &Context, unsigned Tag, StringRef Name, Metadata *File, 1110 unsigned Line, DIScope *Scope, DIType *BaseType, uint64_t SizeInBits, 1111 uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags, 1112 DINodeArray Elements, unsigned RuntimeLang, DIType *VTableHolder, 1113 DITemplateParameterArray TemplateParams, StringRef Identifier, 1114 DIDerivedType *Discriminator, Metadata *DataLocation, 1115 Metadata *Associated, Metadata *Allocated, Metadata *Rank, 1116 DINodeArray Annotations, StorageType Storage, 1117 bool ShouldCreate = true) { 1118 return getImpl( 1119 Context, Tag, getCanonicalMDString(Context, Name), File, Line, Scope, 1120 BaseType, SizeInBits, AlignInBits, OffsetInBits, Flags, Elements.get(), 1121 RuntimeLang, VTableHolder, TemplateParams.get(), 1122 getCanonicalMDString(Context, Identifier), Discriminator, DataLocation, 1123 Associated, Allocated, Rank, Annotations.get(), Storage, ShouldCreate); 1124 } 1125 static DICompositeType * 1126 getImpl(LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File, 1127 unsigned Line, Metadata *Scope, Metadata *BaseType, 1128 uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits, 1129 DIFlags Flags, Metadata *Elements, unsigned RuntimeLang, 1130 Metadata *VTableHolder, Metadata *TemplateParams, 1131 MDString *Identifier, Metadata *Discriminator, Metadata *DataLocation, 1132 Metadata *Associated, Metadata *Allocated, Metadata *Rank, 1133 Metadata *Annotations, StorageType Storage, bool ShouldCreate = true); 1134 1135 TempDICompositeType cloneImpl() const { 1136 return getTemporary( 1137 getContext(), getTag(), getName(), getFile(), getLine(), getScope(), 1138 getBaseType(), getSizeInBits(), getAlignInBits(), getOffsetInBits(), 1139 getFlags(), getElements(), getRuntimeLang(), getVTableHolder(), 1140 getTemplateParams(), getIdentifier(), getDiscriminator(), 1141 getRawDataLocation(), getRawAssociated(), getRawAllocated(), 1142 getRawRank(), getAnnotations()); 1143 } 1144 1145 public: 1146 DEFINE_MDNODE_GET( 1147 DICompositeType, 1148 (unsigned Tag, StringRef Name, DIFile *File, unsigned Line, 1149 DIScope *Scope, DIType *BaseType, uint64_t SizeInBits, 1150 uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags, 1151 DINodeArray Elements, unsigned RuntimeLang, DIType *VTableHolder, 1152 DITemplateParameterArray TemplateParams = nullptr, 1153 StringRef Identifier = "", DIDerivedType *Discriminator = nullptr, 1154 Metadata *DataLocation = nullptr, Metadata *Associated = nullptr, 1155 Metadata *Allocated = nullptr, Metadata *Rank = nullptr, 1156 DINodeArray Annotations = nullptr), 1157 (Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits, 1158 OffsetInBits, Flags, Elements, RuntimeLang, VTableHolder, TemplateParams, 1159 Identifier, Discriminator, DataLocation, Associated, Allocated, Rank, 1160 Annotations)) 1161 DEFINE_MDNODE_GET( 1162 DICompositeType, 1163 (unsigned Tag, MDString *Name, Metadata *File, unsigned Line, 1164 Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits, 1165 uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags, 1166 Metadata *Elements, unsigned RuntimeLang, Metadata *VTableHolder, 1167 Metadata *TemplateParams = nullptr, MDString *Identifier = nullptr, 1168 Metadata *Discriminator = nullptr, Metadata *DataLocation = nullptr, 1169 Metadata *Associated = nullptr, Metadata *Allocated = nullptr, 1170 Metadata *Rank = nullptr, Metadata *Annotations = nullptr), 1171 (Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits, 1172 OffsetInBits, Flags, Elements, RuntimeLang, VTableHolder, TemplateParams, 1173 Identifier, Discriminator, DataLocation, Associated, Allocated, Rank, 1174 Annotations)) 1175 1176 TempDICompositeType clone() const { return cloneImpl(); } 1177 1178 /// Get a DICompositeType with the given ODR identifier. 1179 /// 1180 /// If \a LLVMContext::isODRUniquingDebugTypes(), gets the mapped 1181 /// DICompositeType for the given ODR \c Identifier. If none exists, creates 1182 /// a new node. 1183 /// 1184 /// Else, returns \c nullptr. 1185 static DICompositeType * 1186 getODRType(LLVMContext &Context, MDString &Identifier, unsigned Tag, 1187 MDString *Name, Metadata *File, unsigned Line, Metadata *Scope, 1188 Metadata *BaseType, uint64_t SizeInBits, uint32_t AlignInBits, 1189 uint64_t OffsetInBits, DIFlags Flags, Metadata *Elements, 1190 unsigned RuntimeLang, Metadata *VTableHolder, 1191 Metadata *TemplateParams, Metadata *Discriminator, 1192 Metadata *DataLocation, Metadata *Associated, Metadata *Allocated, 1193 Metadata *Rank, Metadata *Annotations); 1194 static DICompositeType *getODRTypeIfExists(LLVMContext &Context, 1195 MDString &Identifier); 1196 1197 /// Build a DICompositeType with the given ODR identifier. 1198 /// 1199 /// Looks up the mapped DICompositeType for the given ODR \c Identifier. If 1200 /// it doesn't exist, creates a new one. If it does exist and \a 1201 /// isForwardDecl(), and the new arguments would be a definition, mutates the 1202 /// the type in place. In either case, returns the type. 1203 /// 1204 /// If not \a LLVMContext::isODRUniquingDebugTypes(), this function returns 1205 /// nullptr. 1206 static DICompositeType * 1207 buildODRType(LLVMContext &Context, MDString &Identifier, unsigned Tag, 1208 MDString *Name, Metadata *File, unsigned Line, Metadata *Scope, 1209 Metadata *BaseType, uint64_t SizeInBits, uint32_t AlignInBits, 1210 uint64_t OffsetInBits, DIFlags Flags, Metadata *Elements, 1211 unsigned RuntimeLang, Metadata *VTableHolder, 1212 Metadata *TemplateParams, Metadata *Discriminator, 1213 Metadata *DataLocation, Metadata *Associated, 1214 Metadata *Allocated, Metadata *Rank, Metadata *Annotations); 1215 1216 DIType *getBaseType() const { return cast_or_null<DIType>(getRawBaseType()); } 1217 DINodeArray getElements() const { 1218 return cast_or_null<MDTuple>(getRawElements()); 1219 } 1220 DIType *getVTableHolder() const { 1221 return cast_or_null<DIType>(getRawVTableHolder()); 1222 } 1223 DITemplateParameterArray getTemplateParams() const { 1224 return cast_or_null<MDTuple>(getRawTemplateParams()); 1225 } 1226 StringRef getIdentifier() const { return getStringOperand(7); } 1227 unsigned getRuntimeLang() const { return RuntimeLang; } 1228 1229 Metadata *getRawBaseType() const { return getOperand(3); } 1230 Metadata *getRawElements() const { return getOperand(4); } 1231 Metadata *getRawVTableHolder() const { return getOperand(5); } 1232 Metadata *getRawTemplateParams() const { return getOperand(6); } 1233 MDString *getRawIdentifier() const { return getOperandAs<MDString>(7); } 1234 Metadata *getRawDiscriminator() const { return getOperand(8); } 1235 DIDerivedType *getDiscriminator() const { 1236 return getOperandAs<DIDerivedType>(8); 1237 } 1238 Metadata *getRawDataLocation() const { return getOperand(9); } 1239 DIVariable *getDataLocation() const { 1240 return dyn_cast_or_null<DIVariable>(getRawDataLocation()); 1241 } 1242 DIExpression *getDataLocationExp() const { 1243 return dyn_cast_or_null<DIExpression>(getRawDataLocation()); 1244 } 1245 Metadata *getRawAssociated() const { return getOperand(10); } 1246 DIVariable *getAssociated() const { 1247 return dyn_cast_or_null<DIVariable>(getRawAssociated()); 1248 } 1249 DIExpression *getAssociatedExp() const { 1250 return dyn_cast_or_null<DIExpression>(getRawAssociated()); 1251 } 1252 Metadata *getRawAllocated() const { return getOperand(11); } 1253 DIVariable *getAllocated() const { 1254 return dyn_cast_or_null<DIVariable>(getRawAllocated()); 1255 } 1256 DIExpression *getAllocatedExp() const { 1257 return dyn_cast_or_null<DIExpression>(getRawAllocated()); 1258 } 1259 Metadata *getRawRank() const { return getOperand(12); } 1260 ConstantInt *getRankConst() const { 1261 if (auto *MD = dyn_cast_or_null<ConstantAsMetadata>(getRawRank())) 1262 return dyn_cast_or_null<ConstantInt>(MD->getValue()); 1263 return nullptr; 1264 } 1265 DIExpression *getRankExp() const { 1266 return dyn_cast_or_null<DIExpression>(getRawRank()); 1267 } 1268 1269 Metadata *getRawAnnotations() const { return getOperand(13); } 1270 DINodeArray getAnnotations() const { 1271 return cast_or_null<MDTuple>(getRawAnnotations()); 1272 } 1273 1274 /// Replace operands. 1275 /// 1276 /// If this \a isUniqued() and not \a isResolved(), on a uniquing collision 1277 /// this will be RAUW'ed and deleted. Use a \a TrackingMDRef to keep track 1278 /// of its movement if necessary. 1279 /// @{ 1280 void replaceElements(DINodeArray Elements) { 1281 #ifndef NDEBUG 1282 for (DINode *Op : getElements()) 1283 assert(is_contained(Elements->operands(), Op) && 1284 "Lost a member during member list replacement"); 1285 #endif 1286 replaceOperandWith(4, Elements.get()); 1287 } 1288 1289 void replaceVTableHolder(DIType *VTableHolder) { 1290 replaceOperandWith(5, VTableHolder); 1291 } 1292 1293 void replaceTemplateParams(DITemplateParameterArray TemplateParams) { 1294 replaceOperandWith(6, TemplateParams.get()); 1295 } 1296 /// @} 1297 1298 static bool classof(const Metadata *MD) { 1299 return MD->getMetadataID() == DICompositeTypeKind; 1300 } 1301 }; 1302 1303 /// Type array for a subprogram. 1304 /// 1305 /// TODO: Fold the array of types in directly as operands. 1306 class DISubroutineType : public DIType { 1307 friend class LLVMContextImpl; 1308 friend class MDNode; 1309 1310 /// The calling convention used with DW_AT_calling_convention. Actually of 1311 /// type dwarf::CallingConvention. 1312 uint8_t CC; 1313 1314 DISubroutineType(LLVMContext &C, StorageType Storage, DIFlags Flags, 1315 uint8_t CC, ArrayRef<Metadata *> Ops); 1316 ~DISubroutineType() = default; 1317 1318 static DISubroutineType *getImpl(LLVMContext &Context, DIFlags Flags, 1319 uint8_t CC, DITypeRefArray TypeArray, 1320 StorageType Storage, 1321 bool ShouldCreate = true) { 1322 return getImpl(Context, Flags, CC, TypeArray.get(), Storage, ShouldCreate); 1323 } 1324 static DISubroutineType *getImpl(LLVMContext &Context, DIFlags Flags, 1325 uint8_t CC, Metadata *TypeArray, 1326 StorageType Storage, 1327 bool ShouldCreate = true); 1328 1329 TempDISubroutineType cloneImpl() const { 1330 return getTemporary(getContext(), getFlags(), getCC(), getTypeArray()); 1331 } 1332 1333 public: 1334 DEFINE_MDNODE_GET(DISubroutineType, 1335 (DIFlags Flags, uint8_t CC, DITypeRefArray TypeArray), 1336 (Flags, CC, TypeArray)) 1337 DEFINE_MDNODE_GET(DISubroutineType, 1338 (DIFlags Flags, uint8_t CC, Metadata *TypeArray), 1339 (Flags, CC, TypeArray)) 1340 1341 TempDISubroutineType clone() const { return cloneImpl(); } 1342 // Returns a new temporary DISubroutineType with updated CC 1343 TempDISubroutineType cloneWithCC(uint8_t CC) const { 1344 auto NewTy = clone(); 1345 NewTy->CC = CC; 1346 return NewTy; 1347 } 1348 1349 uint8_t getCC() const { return CC; } 1350 1351 DITypeRefArray getTypeArray() const { 1352 return cast_or_null<MDTuple>(getRawTypeArray()); 1353 } 1354 1355 Metadata *getRawTypeArray() const { return getOperand(3); } 1356 1357 static bool classof(const Metadata *MD) { 1358 return MD->getMetadataID() == DISubroutineTypeKind; 1359 } 1360 }; 1361 1362 /// Compile unit. 1363 class DICompileUnit : public DIScope { 1364 friend class LLVMContextImpl; 1365 friend class MDNode; 1366 1367 public: 1368 enum DebugEmissionKind : unsigned { 1369 NoDebug = 0, 1370 FullDebug, 1371 LineTablesOnly, 1372 DebugDirectivesOnly, 1373 LastEmissionKind = DebugDirectivesOnly 1374 }; 1375 1376 enum class DebugNameTableKind : unsigned { 1377 Default = 0, 1378 GNU = 1, 1379 None = 2, 1380 Apple = 3, 1381 LastDebugNameTableKind = Apple 1382 }; 1383 1384 static std::optional<DebugEmissionKind> getEmissionKind(StringRef Str); 1385 static const char *emissionKindString(DebugEmissionKind EK); 1386 static std::optional<DebugNameTableKind> getNameTableKind(StringRef Str); 1387 static const char *nameTableKindString(DebugNameTableKind PK); 1388 1389 private: 1390 unsigned SourceLanguage; 1391 bool IsOptimized; 1392 unsigned RuntimeVersion; 1393 unsigned EmissionKind; 1394 uint64_t DWOId; 1395 bool SplitDebugInlining; 1396 bool DebugInfoForProfiling; 1397 unsigned NameTableKind; 1398 bool RangesBaseAddress; 1399 1400 DICompileUnit(LLVMContext &C, StorageType Storage, unsigned SourceLanguage, 1401 bool IsOptimized, unsigned RuntimeVersion, 1402 unsigned EmissionKind, uint64_t DWOId, bool SplitDebugInlining, 1403 bool DebugInfoForProfiling, unsigned NameTableKind, 1404 bool RangesBaseAddress, ArrayRef<Metadata *> Ops); 1405 ~DICompileUnit() = default; 1406 1407 static DICompileUnit * 1408 getImpl(LLVMContext &Context, unsigned SourceLanguage, DIFile *File, 1409 StringRef Producer, bool IsOptimized, StringRef Flags, 1410 unsigned RuntimeVersion, StringRef SplitDebugFilename, 1411 unsigned EmissionKind, DICompositeTypeArray EnumTypes, 1412 DIScopeArray RetainedTypes, 1413 DIGlobalVariableExpressionArray GlobalVariables, 1414 DIImportedEntityArray ImportedEntities, DIMacroNodeArray Macros, 1415 uint64_t DWOId, bool SplitDebugInlining, bool DebugInfoForProfiling, 1416 unsigned NameTableKind, bool RangesBaseAddress, StringRef SysRoot, 1417 StringRef SDK, StorageType Storage, bool ShouldCreate = true) { 1418 return getImpl( 1419 Context, SourceLanguage, File, getCanonicalMDString(Context, Producer), 1420 IsOptimized, getCanonicalMDString(Context, Flags), RuntimeVersion, 1421 getCanonicalMDString(Context, SplitDebugFilename), EmissionKind, 1422 EnumTypes.get(), RetainedTypes.get(), GlobalVariables.get(), 1423 ImportedEntities.get(), Macros.get(), DWOId, SplitDebugInlining, 1424 DebugInfoForProfiling, NameTableKind, RangesBaseAddress, 1425 getCanonicalMDString(Context, SysRoot), 1426 getCanonicalMDString(Context, SDK), Storage, ShouldCreate); 1427 } 1428 static DICompileUnit * 1429 getImpl(LLVMContext &Context, unsigned SourceLanguage, Metadata *File, 1430 MDString *Producer, bool IsOptimized, MDString *Flags, 1431 unsigned RuntimeVersion, MDString *SplitDebugFilename, 1432 unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes, 1433 Metadata *GlobalVariables, Metadata *ImportedEntities, 1434 Metadata *Macros, uint64_t DWOId, bool SplitDebugInlining, 1435 bool DebugInfoForProfiling, unsigned NameTableKind, 1436 bool RangesBaseAddress, MDString *SysRoot, MDString *SDK, 1437 StorageType Storage, bool ShouldCreate = true); 1438 1439 TempDICompileUnit cloneImpl() const { 1440 return getTemporary( 1441 getContext(), getSourceLanguage(), getFile(), getProducer(), 1442 isOptimized(), getFlags(), getRuntimeVersion(), getSplitDebugFilename(), 1443 getEmissionKind(), getEnumTypes(), getRetainedTypes(), 1444 getGlobalVariables(), getImportedEntities(), getMacros(), DWOId, 1445 getSplitDebugInlining(), getDebugInfoForProfiling(), getNameTableKind(), 1446 getRangesBaseAddress(), getSysRoot(), getSDK()); 1447 } 1448 1449 public: 1450 static void get() = delete; 1451 static void getIfExists() = delete; 1452 1453 DEFINE_MDNODE_GET_DISTINCT_TEMPORARY( 1454 DICompileUnit, 1455 (unsigned SourceLanguage, DIFile *File, StringRef Producer, 1456 bool IsOptimized, StringRef Flags, unsigned RuntimeVersion, 1457 StringRef SplitDebugFilename, DebugEmissionKind EmissionKind, 1458 DICompositeTypeArray EnumTypes, DIScopeArray RetainedTypes, 1459 DIGlobalVariableExpressionArray GlobalVariables, 1460 DIImportedEntityArray ImportedEntities, DIMacroNodeArray Macros, 1461 uint64_t DWOId, bool SplitDebugInlining, bool DebugInfoForProfiling, 1462 DebugNameTableKind NameTableKind, bool RangesBaseAddress, 1463 StringRef SysRoot, StringRef SDK), 1464 (SourceLanguage, File, Producer, IsOptimized, Flags, RuntimeVersion, 1465 SplitDebugFilename, EmissionKind, EnumTypes, RetainedTypes, 1466 GlobalVariables, ImportedEntities, Macros, DWOId, SplitDebugInlining, 1467 DebugInfoForProfiling, (unsigned)NameTableKind, RangesBaseAddress, 1468 SysRoot, SDK)) 1469 DEFINE_MDNODE_GET_DISTINCT_TEMPORARY( 1470 DICompileUnit, 1471 (unsigned SourceLanguage, Metadata *File, MDString *Producer, 1472 bool IsOptimized, MDString *Flags, unsigned RuntimeVersion, 1473 MDString *SplitDebugFilename, unsigned EmissionKind, Metadata *EnumTypes, 1474 Metadata *RetainedTypes, Metadata *GlobalVariables, 1475 Metadata *ImportedEntities, Metadata *Macros, uint64_t DWOId, 1476 bool SplitDebugInlining, bool DebugInfoForProfiling, 1477 unsigned NameTableKind, bool RangesBaseAddress, MDString *SysRoot, 1478 MDString *SDK), 1479 (SourceLanguage, File, Producer, IsOptimized, Flags, RuntimeVersion, 1480 SplitDebugFilename, EmissionKind, EnumTypes, RetainedTypes, 1481 GlobalVariables, ImportedEntities, Macros, DWOId, SplitDebugInlining, 1482 DebugInfoForProfiling, NameTableKind, RangesBaseAddress, SysRoot, SDK)) 1483 1484 TempDICompileUnit clone() const { return cloneImpl(); } 1485 1486 unsigned getSourceLanguage() const { return SourceLanguage; } 1487 bool isOptimized() const { return IsOptimized; } 1488 unsigned getRuntimeVersion() const { return RuntimeVersion; } 1489 DebugEmissionKind getEmissionKind() const { 1490 return (DebugEmissionKind)EmissionKind; 1491 } 1492 bool isDebugDirectivesOnly() const { 1493 return EmissionKind == DebugDirectivesOnly; 1494 } 1495 bool getDebugInfoForProfiling() const { return DebugInfoForProfiling; } 1496 DebugNameTableKind getNameTableKind() const { 1497 return (DebugNameTableKind)NameTableKind; 1498 } 1499 bool getRangesBaseAddress() const { return RangesBaseAddress; } 1500 StringRef getProducer() const { return getStringOperand(1); } 1501 StringRef getFlags() const { return getStringOperand(2); } 1502 StringRef getSplitDebugFilename() const { return getStringOperand(3); } 1503 DICompositeTypeArray getEnumTypes() const { 1504 return cast_or_null<MDTuple>(getRawEnumTypes()); 1505 } 1506 DIScopeArray getRetainedTypes() const { 1507 return cast_or_null<MDTuple>(getRawRetainedTypes()); 1508 } 1509 DIGlobalVariableExpressionArray getGlobalVariables() const { 1510 return cast_or_null<MDTuple>(getRawGlobalVariables()); 1511 } 1512 DIImportedEntityArray getImportedEntities() const { 1513 return cast_or_null<MDTuple>(getRawImportedEntities()); 1514 } 1515 DIMacroNodeArray getMacros() const { 1516 return cast_or_null<MDTuple>(getRawMacros()); 1517 } 1518 uint64_t getDWOId() const { return DWOId; } 1519 void setDWOId(uint64_t DwoId) { DWOId = DwoId; } 1520 bool getSplitDebugInlining() const { return SplitDebugInlining; } 1521 void setSplitDebugInlining(bool SplitDebugInlining) { 1522 this->SplitDebugInlining = SplitDebugInlining; 1523 } 1524 StringRef getSysRoot() const { return getStringOperand(9); } 1525 StringRef getSDK() const { return getStringOperand(10); } 1526 1527 MDString *getRawProducer() const { return getOperandAs<MDString>(1); } 1528 MDString *getRawFlags() const { return getOperandAs<MDString>(2); } 1529 MDString *getRawSplitDebugFilename() const { 1530 return getOperandAs<MDString>(3); 1531 } 1532 Metadata *getRawEnumTypes() const { return getOperand(4); } 1533 Metadata *getRawRetainedTypes() const { return getOperand(5); } 1534 Metadata *getRawGlobalVariables() const { return getOperand(6); } 1535 Metadata *getRawImportedEntities() const { return getOperand(7); } 1536 Metadata *getRawMacros() const { return getOperand(8); } 1537 MDString *getRawSysRoot() const { return getOperandAs<MDString>(9); } 1538 MDString *getRawSDK() const { return getOperandAs<MDString>(10); } 1539 1540 /// Replace arrays. 1541 /// 1542 /// If this \a isUniqued() and not \a isResolved(), it will be RAUW'ed and 1543 /// deleted on a uniquing collision. In practice, uniquing collisions on \a 1544 /// DICompileUnit should be fairly rare. 1545 /// @{ 1546 void replaceEnumTypes(DICompositeTypeArray N) { 1547 replaceOperandWith(4, N.get()); 1548 } 1549 void replaceRetainedTypes(DITypeArray N) { replaceOperandWith(5, N.get()); } 1550 void replaceGlobalVariables(DIGlobalVariableExpressionArray N) { 1551 replaceOperandWith(6, N.get()); 1552 } 1553 void replaceImportedEntities(DIImportedEntityArray N) { 1554 replaceOperandWith(7, N.get()); 1555 } 1556 void replaceMacros(DIMacroNodeArray N) { replaceOperandWith(8, N.get()); } 1557 /// @} 1558 1559 static bool classof(const Metadata *MD) { 1560 return MD->getMetadataID() == DICompileUnitKind; 1561 } 1562 }; 1563 1564 /// A scope for locals. 1565 /// 1566 /// A legal scope for lexical blocks, local variables, and debug info 1567 /// locations. Subclasses are \a DISubprogram, \a DILexicalBlock, and \a 1568 /// DILexicalBlockFile. 1569 class DILocalScope : public DIScope { 1570 protected: 1571 DILocalScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag, 1572 ArrayRef<Metadata *> Ops) 1573 : DIScope(C, ID, Storage, Tag, Ops) {} 1574 ~DILocalScope() = default; 1575 1576 public: 1577 /// Get the subprogram for this scope. 1578 /// 1579 /// Return this if it's an \a DISubprogram; otherwise, look up the scope 1580 /// chain. 1581 DISubprogram *getSubprogram() const; 1582 1583 /// Traverses the scope chain rooted at RootScope until it hits a Subprogram, 1584 /// recreating the chain with "NewSP" instead. 1585 static DILocalScope * 1586 cloneScopeForSubprogram(DILocalScope &RootScope, DISubprogram &NewSP, 1587 LLVMContext &Ctx, 1588 DenseMap<const MDNode *, MDNode *> &Cache); 1589 1590 /// Get the first non DILexicalBlockFile scope of this scope. 1591 /// 1592 /// Return this if it's not a \a DILexicalBlockFIle; otherwise, look up the 1593 /// scope chain. 1594 DILocalScope *getNonLexicalBlockFileScope() const; 1595 1596 static bool classof(const Metadata *MD) { 1597 return MD->getMetadataID() == DISubprogramKind || 1598 MD->getMetadataID() == DILexicalBlockKind || 1599 MD->getMetadataID() == DILexicalBlockFileKind; 1600 } 1601 }; 1602 1603 /// Subprogram description. 1604 class DISubprogram : public DILocalScope { 1605 friend class LLVMContextImpl; 1606 friend class MDNode; 1607 1608 unsigned Line; 1609 unsigned ScopeLine; 1610 unsigned VirtualIndex; 1611 1612 /// In the MS ABI, the implicit 'this' parameter is adjusted in the prologue 1613 /// of method overrides from secondary bases by this amount. It may be 1614 /// negative. 1615 int ThisAdjustment; 1616 1617 public: 1618 /// Debug info subprogram flags. 1619 enum DISPFlags : uint32_t { 1620 #define HANDLE_DISP_FLAG(ID, NAME) SPFlag##NAME = ID, 1621 #define DISP_FLAG_LARGEST_NEEDED 1622 #include "llvm/IR/DebugInfoFlags.def" 1623 SPFlagNonvirtual = SPFlagZero, 1624 SPFlagVirtuality = SPFlagVirtual | SPFlagPureVirtual, 1625 LLVM_MARK_AS_BITMASK_ENUM(SPFlagLargest) 1626 }; 1627 1628 static DISPFlags getFlag(StringRef Flag); 1629 static StringRef getFlagString(DISPFlags Flag); 1630 1631 /// Split up a flags bitfield for easier printing. 1632 /// 1633 /// Split \c Flags into \c SplitFlags, a vector of its components. Returns 1634 /// any remaining (unrecognized) bits. 1635 static DISPFlags splitFlags(DISPFlags Flags, 1636 SmallVectorImpl<DISPFlags> &SplitFlags); 1637 1638 // Helper for converting old bitfields to new flags word. 1639 static DISPFlags toSPFlags(bool IsLocalToUnit, bool IsDefinition, 1640 bool IsOptimized, 1641 unsigned Virtuality = SPFlagNonvirtual, 1642 bool IsMainSubprogram = false); 1643 1644 private: 1645 DIFlags Flags; 1646 DISPFlags SPFlags; 1647 1648 DISubprogram(LLVMContext &C, StorageType Storage, unsigned Line, 1649 unsigned ScopeLine, unsigned VirtualIndex, int ThisAdjustment, 1650 DIFlags Flags, DISPFlags SPFlags, ArrayRef<Metadata *> Ops); 1651 ~DISubprogram() = default; 1652 1653 static DISubprogram * 1654 getImpl(LLVMContext &Context, DIScope *Scope, StringRef Name, 1655 StringRef LinkageName, DIFile *File, unsigned Line, 1656 DISubroutineType *Type, unsigned ScopeLine, DIType *ContainingType, 1657 unsigned VirtualIndex, int ThisAdjustment, DIFlags Flags, 1658 DISPFlags SPFlags, DICompileUnit *Unit, 1659 DITemplateParameterArray TemplateParams, DISubprogram *Declaration, 1660 DINodeArray RetainedNodes, DITypeArray ThrownTypes, 1661 DINodeArray Annotations, StringRef TargetFuncName, 1662 StorageType Storage, bool ShouldCreate = true) { 1663 return getImpl(Context, Scope, getCanonicalMDString(Context, Name), 1664 getCanonicalMDString(Context, LinkageName), File, Line, Type, 1665 ScopeLine, ContainingType, VirtualIndex, ThisAdjustment, 1666 Flags, SPFlags, Unit, TemplateParams.get(), Declaration, 1667 RetainedNodes.get(), ThrownTypes.get(), Annotations.get(), 1668 getCanonicalMDString(Context, TargetFuncName), 1669 Storage, ShouldCreate); 1670 } 1671 static DISubprogram * 1672 getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name, 1673 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type, 1674 unsigned ScopeLine, Metadata *ContainingType, unsigned VirtualIndex, 1675 int ThisAdjustment, DIFlags Flags, DISPFlags SPFlags, Metadata *Unit, 1676 Metadata *TemplateParams, Metadata *Declaration, 1677 Metadata *RetainedNodes, Metadata *ThrownTypes, Metadata *Annotations, 1678 MDString *TargetFuncName, StorageType Storage, 1679 bool ShouldCreate = true); 1680 1681 TempDISubprogram cloneImpl() const { 1682 return getTemporary(getContext(), getScope(), getName(), getLinkageName(), 1683 getFile(), getLine(), getType(), getScopeLine(), 1684 getContainingType(), getVirtualIndex(), 1685 getThisAdjustment(), getFlags(), getSPFlags(), 1686 getUnit(), getTemplateParams(), getDeclaration(), 1687 getRetainedNodes(), getThrownTypes(), getAnnotations(), 1688 getTargetFuncName()); 1689 } 1690 1691 public: 1692 DEFINE_MDNODE_GET( 1693 DISubprogram, 1694 (DIScope * Scope, StringRef Name, StringRef LinkageName, DIFile *File, 1695 unsigned Line, DISubroutineType *Type, unsigned ScopeLine, 1696 DIType *ContainingType, unsigned VirtualIndex, int ThisAdjustment, 1697 DIFlags Flags, DISPFlags SPFlags, DICompileUnit *Unit, 1698 DITemplateParameterArray TemplateParams = nullptr, 1699 DISubprogram *Declaration = nullptr, DINodeArray RetainedNodes = nullptr, 1700 DITypeArray ThrownTypes = nullptr, DINodeArray Annotations = nullptr, 1701 StringRef TargetFuncName = ""), 1702 (Scope, Name, LinkageName, File, Line, Type, ScopeLine, ContainingType, 1703 VirtualIndex, ThisAdjustment, Flags, SPFlags, Unit, TemplateParams, 1704 Declaration, RetainedNodes, ThrownTypes, Annotations, TargetFuncName)) 1705 1706 DEFINE_MDNODE_GET( 1707 DISubprogram, 1708 (Metadata * Scope, MDString *Name, MDString *LinkageName, Metadata *File, 1709 unsigned Line, Metadata *Type, unsigned ScopeLine, 1710 Metadata *ContainingType, unsigned VirtualIndex, int ThisAdjustment, 1711 DIFlags Flags, DISPFlags SPFlags, Metadata *Unit, 1712 Metadata *TemplateParams = nullptr, Metadata *Declaration = nullptr, 1713 Metadata *RetainedNodes = nullptr, Metadata *ThrownTypes = nullptr, 1714 Metadata *Annotations = nullptr, MDString *TargetFuncName = nullptr), 1715 (Scope, Name, LinkageName, File, Line, Type, ScopeLine, ContainingType, 1716 VirtualIndex, ThisAdjustment, Flags, SPFlags, Unit, TemplateParams, 1717 Declaration, RetainedNodes, ThrownTypes, Annotations, TargetFuncName)) 1718 1719 TempDISubprogram clone() const { return cloneImpl(); } 1720 1721 /// Returns a new temporary DISubprogram with updated Flags 1722 TempDISubprogram cloneWithFlags(DIFlags NewFlags) const { 1723 auto NewSP = clone(); 1724 NewSP->Flags = NewFlags; 1725 return NewSP; 1726 } 1727 1728 public: 1729 unsigned getLine() const { return Line; } 1730 unsigned getVirtuality() const { return getSPFlags() & SPFlagVirtuality; } 1731 unsigned getVirtualIndex() const { return VirtualIndex; } 1732 int getThisAdjustment() const { return ThisAdjustment; } 1733 unsigned getScopeLine() const { return ScopeLine; } 1734 void setScopeLine(unsigned L) { 1735 assert(isDistinct()); 1736 ScopeLine = L; 1737 } 1738 DIFlags getFlags() const { return Flags; } 1739 DISPFlags getSPFlags() const { return SPFlags; } 1740 bool isLocalToUnit() const { return getSPFlags() & SPFlagLocalToUnit; } 1741 bool isDefinition() const { return getSPFlags() & SPFlagDefinition; } 1742 bool isOptimized() const { return getSPFlags() & SPFlagOptimized; } 1743 bool isMainSubprogram() const { return getSPFlags() & SPFlagMainSubprogram; } 1744 1745 bool isArtificial() const { return getFlags() & FlagArtificial; } 1746 bool isPrivate() const { 1747 return (getFlags() & FlagAccessibility) == FlagPrivate; 1748 } 1749 bool isProtected() const { 1750 return (getFlags() & FlagAccessibility) == FlagProtected; 1751 } 1752 bool isPublic() const { 1753 return (getFlags() & FlagAccessibility) == FlagPublic; 1754 } 1755 bool isExplicit() const { return getFlags() & FlagExplicit; } 1756 bool isPrototyped() const { return getFlags() & FlagPrototyped; } 1757 bool areAllCallsDescribed() const { 1758 return getFlags() & FlagAllCallsDescribed; 1759 } 1760 bool isPure() const { return getSPFlags() & SPFlagPure; } 1761 bool isElemental() const { return getSPFlags() & SPFlagElemental; } 1762 bool isRecursive() const { return getSPFlags() & SPFlagRecursive; } 1763 bool isObjCDirect() const { return getSPFlags() & SPFlagObjCDirect; } 1764 1765 /// Check if this is deleted member function. 1766 /// 1767 /// Return true if this subprogram is a C++11 special 1768 /// member function declared deleted. 1769 bool isDeleted() const { return getSPFlags() & SPFlagDeleted; } 1770 1771 /// Check if this is reference-qualified. 1772 /// 1773 /// Return true if this subprogram is a C++11 reference-qualified non-static 1774 /// member function (void foo() &). 1775 bool isLValueReference() const { return getFlags() & FlagLValueReference; } 1776 1777 /// Check if this is rvalue-reference-qualified. 1778 /// 1779 /// Return true if this subprogram is a C++11 rvalue-reference-qualified 1780 /// non-static member function (void foo() &&). 1781 bool isRValueReference() const { return getFlags() & FlagRValueReference; } 1782 1783 /// Check if this is marked as noreturn. 1784 /// 1785 /// Return true if this subprogram is C++11 noreturn or C11 _Noreturn 1786 bool isNoReturn() const { return getFlags() & FlagNoReturn; } 1787 1788 // Check if this routine is a compiler-generated thunk. 1789 // 1790 // Returns true if this subprogram is a thunk generated by the compiler. 1791 bool isThunk() const { return getFlags() & FlagThunk; } 1792 1793 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); } 1794 1795 StringRef getName() const { return getStringOperand(2); } 1796 StringRef getLinkageName() const { return getStringOperand(3); } 1797 /// Only used by clients of CloneFunction, and only right after the cloning. 1798 void replaceLinkageName(MDString *LN) { replaceOperandWith(3, LN); } 1799 1800 DISubroutineType *getType() const { 1801 return cast_or_null<DISubroutineType>(getRawType()); 1802 } 1803 DIType *getContainingType() const { 1804 return cast_or_null<DIType>(getRawContainingType()); 1805 } 1806 void replaceType(DISubroutineType *Ty) { 1807 assert(isDistinct() && "Only distinct nodes can mutate"); 1808 replaceOperandWith(4, Ty); 1809 } 1810 1811 DICompileUnit *getUnit() const { 1812 return cast_or_null<DICompileUnit>(getRawUnit()); 1813 } 1814 void replaceUnit(DICompileUnit *CU) { replaceOperandWith(5, CU); } 1815 DITemplateParameterArray getTemplateParams() const { 1816 return cast_or_null<MDTuple>(getRawTemplateParams()); 1817 } 1818 DISubprogram *getDeclaration() const { 1819 return cast_or_null<DISubprogram>(getRawDeclaration()); 1820 } 1821 DINodeArray getRetainedNodes() const { 1822 return cast_or_null<MDTuple>(getRawRetainedNodes()); 1823 } 1824 DITypeArray getThrownTypes() const { 1825 return cast_or_null<MDTuple>(getRawThrownTypes()); 1826 } 1827 DINodeArray getAnnotations() const { 1828 return cast_or_null<MDTuple>(getRawAnnotations()); 1829 } 1830 StringRef getTargetFuncName() const { 1831 return (getRawTargetFuncName()) ? getStringOperand(12) : StringRef(); 1832 } 1833 1834 Metadata *getRawScope() const { return getOperand(1); } 1835 MDString *getRawName() const { return getOperandAs<MDString>(2); } 1836 MDString *getRawLinkageName() const { return getOperandAs<MDString>(3); } 1837 Metadata *getRawType() const { return getOperand(4); } 1838 Metadata *getRawUnit() const { return getOperand(5); } 1839 Metadata *getRawDeclaration() const { return getOperand(6); } 1840 Metadata *getRawRetainedNodes() const { return getOperand(7); } 1841 Metadata *getRawContainingType() const { 1842 return getNumOperands() > 8 ? getOperandAs<Metadata>(8) : nullptr; 1843 } 1844 Metadata *getRawTemplateParams() const { 1845 return getNumOperands() > 9 ? getOperandAs<Metadata>(9) : nullptr; 1846 } 1847 Metadata *getRawThrownTypes() const { 1848 return getNumOperands() > 10 ? getOperandAs<Metadata>(10) : nullptr; 1849 } 1850 Metadata *getRawAnnotations() const { 1851 return getNumOperands() > 11 ? getOperandAs<Metadata>(11) : nullptr; 1852 } 1853 MDString *getRawTargetFuncName() const { 1854 return getNumOperands() > 12 ? getOperandAs<MDString>(12) : nullptr; 1855 } 1856 1857 void replaceRawLinkageName(MDString *LinkageName) { 1858 replaceOperandWith(3, LinkageName); 1859 } 1860 void replaceRetainedNodes(DINodeArray N) { 1861 replaceOperandWith(7, N.get()); 1862 } 1863 1864 /// Check if this subprogram describes the given function. 1865 /// 1866 /// FIXME: Should this be looking through bitcasts? 1867 bool describes(const Function *F) const; 1868 1869 static bool classof(const Metadata *MD) { 1870 return MD->getMetadataID() == DISubprogramKind; 1871 } 1872 }; 1873 1874 /// Debug location. 1875 /// 1876 /// A debug location in source code, used for debug info and otherwise. 1877 class DILocation : public MDNode { 1878 friend class LLVMContextImpl; 1879 friend class MDNode; 1880 1881 DILocation(LLVMContext &C, StorageType Storage, unsigned Line, 1882 unsigned Column, ArrayRef<Metadata *> MDs, bool ImplicitCode); 1883 ~DILocation() { dropAllReferences(); } 1884 1885 static DILocation *getImpl(LLVMContext &Context, unsigned Line, 1886 unsigned Column, Metadata *Scope, 1887 Metadata *InlinedAt, bool ImplicitCode, 1888 StorageType Storage, bool ShouldCreate = true); 1889 static DILocation *getImpl(LLVMContext &Context, unsigned Line, 1890 unsigned Column, DILocalScope *Scope, 1891 DILocation *InlinedAt, bool ImplicitCode, 1892 StorageType Storage, bool ShouldCreate = true) { 1893 return getImpl(Context, Line, Column, static_cast<Metadata *>(Scope), 1894 static_cast<Metadata *>(InlinedAt), ImplicitCode, Storage, 1895 ShouldCreate); 1896 } 1897 1898 TempDILocation cloneImpl() const { 1899 // Get the raw scope/inlinedAt since it is possible to invoke this on 1900 // a DILocation containing temporary metadata. 1901 return getTemporary(getContext(), getLine(), getColumn(), getRawScope(), 1902 getRawInlinedAt(), isImplicitCode()); 1903 } 1904 1905 public: 1906 // Disallow replacing operands. 1907 void replaceOperandWith(unsigned I, Metadata *New) = delete; 1908 1909 DEFINE_MDNODE_GET(DILocation, 1910 (unsigned Line, unsigned Column, Metadata *Scope, 1911 Metadata *InlinedAt = nullptr, bool ImplicitCode = false), 1912 (Line, Column, Scope, InlinedAt, ImplicitCode)) 1913 DEFINE_MDNODE_GET(DILocation, 1914 (unsigned Line, unsigned Column, DILocalScope *Scope, 1915 DILocation *InlinedAt = nullptr, 1916 bool ImplicitCode = false), 1917 (Line, Column, Scope, InlinedAt, ImplicitCode)) 1918 1919 /// Return a (temporary) clone of this. 1920 TempDILocation clone() const { return cloneImpl(); } 1921 1922 unsigned getLine() const { return SubclassData32; } 1923 unsigned getColumn() const { return SubclassData16; } 1924 DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); } 1925 1926 /// Return the linkage name of Subprogram. If the linkage name is empty, 1927 /// return scope name (the demangled name). 1928 StringRef getSubprogramLinkageName() const { 1929 DISubprogram *SP = getScope()->getSubprogram(); 1930 if (!SP) 1931 return ""; 1932 auto Name = SP->getLinkageName(); 1933 if (!Name.empty()) 1934 return Name; 1935 return SP->getName(); 1936 } 1937 1938 DILocation *getInlinedAt() const { 1939 return cast_or_null<DILocation>(getRawInlinedAt()); 1940 } 1941 1942 /// Check if the location corresponds to an implicit code. 1943 /// When the ImplicitCode flag is true, it means that the Instruction 1944 /// with this DILocation has been added by the front-end but it hasn't been 1945 /// written explicitly by the user (e.g. cleanup stuff in C++ put on a closing 1946 /// bracket). It's useful for code coverage to not show a counter on "empty" 1947 /// lines. 1948 bool isImplicitCode() const { return SubclassData1; } 1949 void setImplicitCode(bool ImplicitCode) { SubclassData1 = ImplicitCode; } 1950 1951 DIFile *getFile() const { return getScope()->getFile(); } 1952 StringRef getFilename() const { return getScope()->getFilename(); } 1953 StringRef getDirectory() const { return getScope()->getDirectory(); } 1954 std::optional<StringRef> getSource() const { return getScope()->getSource(); } 1955 1956 /// Get the scope where this is inlined. 1957 /// 1958 /// Walk through \a getInlinedAt() and return \a getScope() from the deepest 1959 /// location. 1960 DILocalScope *getInlinedAtScope() const { 1961 if (auto *IA = getInlinedAt()) 1962 return IA->getInlinedAtScope(); 1963 return getScope(); 1964 } 1965 1966 /// Get the DWARF discriminator. 1967 /// 1968 /// DWARF discriminators distinguish identical file locations between 1969 /// instructions that are on different basic blocks. 1970 /// 1971 /// There are 3 components stored in discriminator, from lower bits: 1972 /// 1973 /// Base discriminator: assigned by AddDiscriminators pass to identify IRs 1974 /// that are defined by the same source line, but 1975 /// different basic blocks. 1976 /// Duplication factor: assigned by optimizations that will scale down 1977 /// the execution frequency of the original IR. 1978 /// Copy Identifier: assigned by optimizations that clones the IR. 1979 /// Each copy of the IR will be assigned an identifier. 1980 /// 1981 /// Encoding: 1982 /// 1983 /// The above 3 components are encoded into a 32bit unsigned integer in 1984 /// order. If the lowest bit is 1, the current component is empty, and the 1985 /// next component will start in the next bit. Otherwise, the current 1986 /// component is non-empty, and its content starts in the next bit. The 1987 /// value of each components is either 5 bit or 12 bit: if the 7th bit 1988 /// is 0, the bit 2~6 (5 bits) are used to represent the component; if the 1989 /// 7th bit is 1, the bit 2~6 (5 bits) and 8~14 (7 bits) are combined to 1990 /// represent the component. Thus, the number of bits used for a component 1991 /// is either 0 (if it and all the next components are empty); 1 - if it is 1992 /// empty; 7 - if its value is up to and including 0x1f (lsb and msb are both 1993 /// 0); or 14, if its value is up to and including 0x1ff. Note that the last 1994 /// component is also capped at 0x1ff, even in the case when both first 1995 /// components are 0, and we'd technically have 29 bits available. 1996 /// 1997 /// For precise control over the data being encoded in the discriminator, 1998 /// use encodeDiscriminator/decodeDiscriminator. 1999 2000 inline unsigned getDiscriminator() const; 2001 2002 // For the regular discriminator, it stands for all empty components if all 2003 // the lowest 3 bits are non-zero and all higher 29 bits are unused(zero by 2004 // default). Here we fully leverage the higher 29 bits for pseudo probe use. 2005 // This is the format: 2006 // [2:0] - 0x7 2007 // [31:3] - pseudo probe fields guaranteed to be non-zero as a whole 2008 // So if the lower 3 bits is non-zero and the others has at least one 2009 // non-zero bit, it guarantees to be a pseudo probe discriminator 2010 inline static bool isPseudoProbeDiscriminator(unsigned Discriminator) { 2011 return ((Discriminator & 0x7) == 0x7) && (Discriminator & 0xFFFFFFF8); 2012 } 2013 2014 /// Returns a new DILocation with updated \p Discriminator. 2015 inline const DILocation *cloneWithDiscriminator(unsigned Discriminator) const; 2016 2017 /// Returns a new DILocation with updated base discriminator \p BD. Only the 2018 /// base discriminator is set in the new DILocation, the other encoded values 2019 /// are elided. 2020 /// If the discriminator cannot be encoded, the function returns std::nullopt. 2021 inline std::optional<const DILocation *> 2022 cloneWithBaseDiscriminator(unsigned BD) const; 2023 2024 /// Returns the duplication factor stored in the discriminator, or 1 if no 2025 /// duplication factor (or 0) is encoded. 2026 inline unsigned getDuplicationFactor() const; 2027 2028 /// Returns the copy identifier stored in the discriminator. 2029 inline unsigned getCopyIdentifier() const; 2030 2031 /// Returns the base discriminator stored in the discriminator. 2032 inline unsigned getBaseDiscriminator() const; 2033 2034 /// Returns a new DILocation with duplication factor \p DF * current 2035 /// duplication factor encoded in the discriminator. The current duplication 2036 /// factor is as defined by getDuplicationFactor(). 2037 /// Returns std::nullopt if encoding failed. 2038 inline std::optional<const DILocation *> 2039 cloneByMultiplyingDuplicationFactor(unsigned DF) const; 2040 2041 /// When two instructions are combined into a single instruction we also 2042 /// need to combine the original locations into a single location. 2043 /// When the locations are the same we can use either location. 2044 /// When they differ, we need a third location which is distinct from either. 2045 /// If they share a common scope, use this scope and compare the line/column 2046 /// pair of the locations with the common scope: 2047 /// * if both match, keep the line and column; 2048 /// * if only the line number matches, keep the line and set the column as 0; 2049 /// * otherwise set line and column as 0. 2050 /// If they do not share a common scope the location is ambiguous and can't be 2051 /// represented in a line entry. In this case, set line and column as 0 and 2052 /// use the scope of any location. 2053 /// 2054 /// \p LocA \p LocB: The locations to be merged. 2055 static DILocation *getMergedLocation(DILocation *LocA, DILocation *LocB); 2056 2057 /// Try to combine the vector of locations passed as input in a single one. 2058 /// This function applies getMergedLocation() repeatedly left-to-right. 2059 /// 2060 /// \p Locs: The locations to be merged. 2061 static DILocation *getMergedLocations(ArrayRef<DILocation *> Locs); 2062 2063 /// Return the masked discriminator value for an input discrimnator value D 2064 /// (i.e. zero out the (B+1)-th and above bits for D (B is 0-base). 2065 // Example: an input of (0x1FF, 7) returns 0xFF. 2066 static unsigned getMaskedDiscriminator(unsigned D, unsigned B) { 2067 return (D & getN1Bits(B)); 2068 } 2069 2070 /// Return the bits used for base discriminators. 2071 static unsigned getBaseDiscriminatorBits() { return getBaseFSBitEnd(); } 2072 2073 /// Returns the base discriminator for a given encoded discriminator \p D. 2074 static unsigned 2075 getBaseDiscriminatorFromDiscriminator(unsigned D, 2076 bool IsFSDiscriminator = false) { 2077 if (IsFSDiscriminator) 2078 return getMaskedDiscriminator(D, getBaseDiscriminatorBits()); 2079 return getUnsignedFromPrefixEncoding(D); 2080 } 2081 2082 /// Raw encoding of the discriminator. APIs such as cloneWithDuplicationFactor 2083 /// have certain special case behavior (e.g. treating empty duplication factor 2084 /// as the value '1'). 2085 /// This API, in conjunction with cloneWithDiscriminator, may be used to 2086 /// encode the raw values provided. 2087 /// 2088 /// \p BD: base discriminator 2089 /// \p DF: duplication factor 2090 /// \p CI: copy index 2091 /// 2092 /// The return is std::nullopt if the values cannot be encoded in 32 bits - 2093 /// for example, values for BD or DF larger than 12 bits. Otherwise, the 2094 /// return is the encoded value. 2095 static std::optional<unsigned> encodeDiscriminator(unsigned BD, unsigned DF, 2096 unsigned CI); 2097 2098 /// Raw decoder for values in an encoded discriminator D. 2099 static void decodeDiscriminator(unsigned D, unsigned &BD, unsigned &DF, 2100 unsigned &CI); 2101 2102 /// Returns the duplication factor for a given encoded discriminator \p D, or 2103 /// 1 if no value or 0 is encoded. 2104 static unsigned getDuplicationFactorFromDiscriminator(unsigned D) { 2105 if (EnableFSDiscriminator) 2106 return 1; 2107 D = getNextComponentInDiscriminator(D); 2108 unsigned Ret = getUnsignedFromPrefixEncoding(D); 2109 if (Ret == 0) 2110 return 1; 2111 return Ret; 2112 } 2113 2114 /// Returns the copy identifier for a given encoded discriminator \p D. 2115 static unsigned getCopyIdentifierFromDiscriminator(unsigned D) { 2116 return getUnsignedFromPrefixEncoding( 2117 getNextComponentInDiscriminator(getNextComponentInDiscriminator(D))); 2118 } 2119 2120 Metadata *getRawScope() const { return getOperand(0); } 2121 Metadata *getRawInlinedAt() const { 2122 if (getNumOperands() == 2) 2123 return getOperand(1); 2124 return nullptr; 2125 } 2126 2127 static bool classof(const Metadata *MD) { 2128 return MD->getMetadataID() == DILocationKind; 2129 } 2130 }; 2131 2132 class DILexicalBlockBase : public DILocalScope { 2133 protected: 2134 DILexicalBlockBase(LLVMContext &C, unsigned ID, StorageType Storage, 2135 ArrayRef<Metadata *> Ops); 2136 ~DILexicalBlockBase() = default; 2137 2138 public: 2139 DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); } 2140 2141 Metadata *getRawScope() const { return getOperand(1); } 2142 2143 void replaceScope(DIScope *Scope) { 2144 assert(!isUniqued()); 2145 setOperand(1, Scope); 2146 } 2147 2148 static bool classof(const Metadata *MD) { 2149 return MD->getMetadataID() == DILexicalBlockKind || 2150 MD->getMetadataID() == DILexicalBlockFileKind; 2151 } 2152 }; 2153 2154 class DILexicalBlock : public DILexicalBlockBase { 2155 friend class LLVMContextImpl; 2156 friend class MDNode; 2157 2158 unsigned Line; 2159 uint16_t Column; 2160 2161 DILexicalBlock(LLVMContext &C, StorageType Storage, unsigned Line, 2162 unsigned Column, ArrayRef<Metadata *> Ops) 2163 : DILexicalBlockBase(C, DILexicalBlockKind, Storage, Ops), Line(Line), 2164 Column(Column) { 2165 assert(Column < (1u << 16) && "Expected 16-bit column"); 2166 } 2167 ~DILexicalBlock() = default; 2168 2169 static DILexicalBlock *getImpl(LLVMContext &Context, DILocalScope *Scope, 2170 DIFile *File, unsigned Line, unsigned Column, 2171 StorageType Storage, 2172 bool ShouldCreate = true) { 2173 return getImpl(Context, static_cast<Metadata *>(Scope), 2174 static_cast<Metadata *>(File), Line, Column, Storage, 2175 ShouldCreate); 2176 } 2177 2178 static DILexicalBlock *getImpl(LLVMContext &Context, Metadata *Scope, 2179 Metadata *File, unsigned Line, unsigned Column, 2180 StorageType Storage, bool ShouldCreate = true); 2181 2182 TempDILexicalBlock cloneImpl() const { 2183 return getTemporary(getContext(), getScope(), getFile(), getLine(), 2184 getColumn()); 2185 } 2186 2187 public: 2188 DEFINE_MDNODE_GET(DILexicalBlock, 2189 (DILocalScope * Scope, DIFile *File, unsigned Line, 2190 unsigned Column), 2191 (Scope, File, Line, Column)) 2192 DEFINE_MDNODE_GET(DILexicalBlock, 2193 (Metadata * Scope, Metadata *File, unsigned Line, 2194 unsigned Column), 2195 (Scope, File, Line, Column)) 2196 2197 TempDILexicalBlock clone() const { return cloneImpl(); } 2198 2199 unsigned getLine() const { return Line; } 2200 unsigned getColumn() const { return Column; } 2201 2202 static bool classof(const Metadata *MD) { 2203 return MD->getMetadataID() == DILexicalBlockKind; 2204 } 2205 }; 2206 2207 class DILexicalBlockFile : public DILexicalBlockBase { 2208 friend class LLVMContextImpl; 2209 friend class MDNode; 2210 2211 unsigned Discriminator; 2212 2213 DILexicalBlockFile(LLVMContext &C, StorageType Storage, 2214 unsigned Discriminator, ArrayRef<Metadata *> Ops) 2215 : DILexicalBlockBase(C, DILexicalBlockFileKind, Storage, Ops), 2216 Discriminator(Discriminator) {} 2217 ~DILexicalBlockFile() = default; 2218 2219 static DILexicalBlockFile *getImpl(LLVMContext &Context, DILocalScope *Scope, 2220 DIFile *File, unsigned Discriminator, 2221 StorageType Storage, 2222 bool ShouldCreate = true) { 2223 return getImpl(Context, static_cast<Metadata *>(Scope), 2224 static_cast<Metadata *>(File), Discriminator, Storage, 2225 ShouldCreate); 2226 } 2227 2228 static DILexicalBlockFile *getImpl(LLVMContext &Context, Metadata *Scope, 2229 Metadata *File, unsigned Discriminator, 2230 StorageType Storage, 2231 bool ShouldCreate = true); 2232 2233 TempDILexicalBlockFile cloneImpl() const { 2234 return getTemporary(getContext(), getScope(), getFile(), 2235 getDiscriminator()); 2236 } 2237 2238 public: 2239 DEFINE_MDNODE_GET(DILexicalBlockFile, 2240 (DILocalScope * Scope, DIFile *File, 2241 unsigned Discriminator), 2242 (Scope, File, Discriminator)) 2243 DEFINE_MDNODE_GET(DILexicalBlockFile, 2244 (Metadata * Scope, Metadata *File, unsigned Discriminator), 2245 (Scope, File, Discriminator)) 2246 2247 TempDILexicalBlockFile clone() const { return cloneImpl(); } 2248 unsigned getDiscriminator() const { return Discriminator; } 2249 2250 static bool classof(const Metadata *MD) { 2251 return MD->getMetadataID() == DILexicalBlockFileKind; 2252 } 2253 }; 2254 2255 unsigned DILocation::getDiscriminator() const { 2256 if (auto *F = dyn_cast<DILexicalBlockFile>(getScope())) 2257 return F->getDiscriminator(); 2258 return 0; 2259 } 2260 2261 const DILocation * 2262 DILocation::cloneWithDiscriminator(unsigned Discriminator) const { 2263 DIScope *Scope = getScope(); 2264 // Skip all parent DILexicalBlockFile that already have a discriminator 2265 // assigned. We do not want to have nested DILexicalBlockFiles that have 2266 // mutliple discriminators because only the leaf DILexicalBlockFile's 2267 // dominator will be used. 2268 for (auto *LBF = dyn_cast<DILexicalBlockFile>(Scope); 2269 LBF && LBF->getDiscriminator() != 0; 2270 LBF = dyn_cast<DILexicalBlockFile>(Scope)) 2271 Scope = LBF->getScope(); 2272 DILexicalBlockFile *NewScope = 2273 DILexicalBlockFile::get(getContext(), Scope, getFile(), Discriminator); 2274 return DILocation::get(getContext(), getLine(), getColumn(), NewScope, 2275 getInlinedAt()); 2276 } 2277 2278 unsigned DILocation::getBaseDiscriminator() const { 2279 return getBaseDiscriminatorFromDiscriminator(getDiscriminator(), 2280 EnableFSDiscriminator); 2281 } 2282 2283 unsigned DILocation::getDuplicationFactor() const { 2284 return getDuplicationFactorFromDiscriminator(getDiscriminator()); 2285 } 2286 2287 unsigned DILocation::getCopyIdentifier() const { 2288 return getCopyIdentifierFromDiscriminator(getDiscriminator()); 2289 } 2290 2291 std::optional<const DILocation *> 2292 DILocation::cloneWithBaseDiscriminator(unsigned D) const { 2293 unsigned BD, DF, CI; 2294 2295 if (EnableFSDiscriminator) { 2296 BD = getBaseDiscriminator(); 2297 if (D == BD) 2298 return this; 2299 return cloneWithDiscriminator(D); 2300 } 2301 2302 decodeDiscriminator(getDiscriminator(), BD, DF, CI); 2303 if (D == BD) 2304 return this; 2305 if (std::optional<unsigned> Encoded = encodeDiscriminator(D, DF, CI)) 2306 return cloneWithDiscriminator(*Encoded); 2307 return std::nullopt; 2308 } 2309 2310 std::optional<const DILocation *> 2311 DILocation::cloneByMultiplyingDuplicationFactor(unsigned DF) const { 2312 assert(!EnableFSDiscriminator && "FSDiscriminator should not call this."); 2313 // Do no interfere with pseudo probes. Pseudo probe doesn't need duplication 2314 // factor support as samples collected on cloned probes will be aggregated. 2315 // Also pseudo probe at a callsite uses the dwarf discriminator to store 2316 // pseudo probe related information, such as the probe id. 2317 if (isPseudoProbeDiscriminator(getDiscriminator())) 2318 return this; 2319 2320 DF *= getDuplicationFactor(); 2321 if (DF <= 1) 2322 return this; 2323 2324 unsigned BD = getBaseDiscriminator(); 2325 unsigned CI = getCopyIdentifier(); 2326 if (std::optional<unsigned> D = encodeDiscriminator(BD, DF, CI)) 2327 return cloneWithDiscriminator(*D); 2328 return std::nullopt; 2329 } 2330 2331 class DINamespace : public DIScope { 2332 friend class LLVMContextImpl; 2333 friend class MDNode; 2334 2335 unsigned ExportSymbols : 1; 2336 2337 DINamespace(LLVMContext &Context, StorageType Storage, bool ExportSymbols, 2338 ArrayRef<Metadata *> Ops); 2339 ~DINamespace() = default; 2340 2341 static DINamespace *getImpl(LLVMContext &Context, DIScope *Scope, 2342 StringRef Name, bool ExportSymbols, 2343 StorageType Storage, bool ShouldCreate = true) { 2344 return getImpl(Context, Scope, getCanonicalMDString(Context, Name), 2345 ExportSymbols, Storage, ShouldCreate); 2346 } 2347 static DINamespace *getImpl(LLVMContext &Context, Metadata *Scope, 2348 MDString *Name, bool ExportSymbols, 2349 StorageType Storage, bool ShouldCreate = true); 2350 2351 TempDINamespace cloneImpl() const { 2352 return getTemporary(getContext(), getScope(), getName(), 2353 getExportSymbols()); 2354 } 2355 2356 public: 2357 DEFINE_MDNODE_GET(DINamespace, 2358 (DIScope * Scope, StringRef Name, bool ExportSymbols), 2359 (Scope, Name, ExportSymbols)) 2360 DEFINE_MDNODE_GET(DINamespace, 2361 (Metadata * Scope, MDString *Name, bool ExportSymbols), 2362 (Scope, Name, ExportSymbols)) 2363 2364 TempDINamespace clone() const { return cloneImpl(); } 2365 2366 bool getExportSymbols() const { return ExportSymbols; } 2367 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); } 2368 StringRef getName() const { return getStringOperand(2); } 2369 2370 Metadata *getRawScope() const { return getOperand(1); } 2371 MDString *getRawName() const { return getOperandAs<MDString>(2); } 2372 2373 static bool classof(const Metadata *MD) { 2374 return MD->getMetadataID() == DINamespaceKind; 2375 } 2376 }; 2377 2378 /// Represents a module in the programming language, for example, a Clang 2379 /// module, or a Fortran module. 2380 class DIModule : public DIScope { 2381 friend class LLVMContextImpl; 2382 friend class MDNode; 2383 unsigned LineNo; 2384 bool IsDecl; 2385 2386 DIModule(LLVMContext &Context, StorageType Storage, unsigned LineNo, 2387 bool IsDecl, ArrayRef<Metadata *> Ops); 2388 ~DIModule() = default; 2389 2390 static DIModule *getImpl(LLVMContext &Context, DIFile *File, DIScope *Scope, 2391 StringRef Name, StringRef ConfigurationMacros, 2392 StringRef IncludePath, StringRef APINotesFile, 2393 unsigned LineNo, bool IsDecl, StorageType Storage, 2394 bool ShouldCreate = true) { 2395 return getImpl(Context, File, Scope, getCanonicalMDString(Context, Name), 2396 getCanonicalMDString(Context, ConfigurationMacros), 2397 getCanonicalMDString(Context, IncludePath), 2398 getCanonicalMDString(Context, APINotesFile), LineNo, IsDecl, 2399 Storage, ShouldCreate); 2400 } 2401 static DIModule *getImpl(LLVMContext &Context, Metadata *File, 2402 Metadata *Scope, MDString *Name, 2403 MDString *ConfigurationMacros, MDString *IncludePath, 2404 MDString *APINotesFile, unsigned LineNo, bool IsDecl, 2405 StorageType Storage, bool ShouldCreate = true); 2406 2407 TempDIModule cloneImpl() const { 2408 return getTemporary(getContext(), getFile(), getScope(), getName(), 2409 getConfigurationMacros(), getIncludePath(), 2410 getAPINotesFile(), getLineNo(), getIsDecl()); 2411 } 2412 2413 public: 2414 DEFINE_MDNODE_GET(DIModule, 2415 (DIFile * File, DIScope *Scope, StringRef Name, 2416 StringRef ConfigurationMacros, StringRef IncludePath, 2417 StringRef APINotesFile, unsigned LineNo, 2418 bool IsDecl = false), 2419 (File, Scope, Name, ConfigurationMacros, IncludePath, 2420 APINotesFile, LineNo, IsDecl)) 2421 DEFINE_MDNODE_GET(DIModule, 2422 (Metadata * File, Metadata *Scope, MDString *Name, 2423 MDString *ConfigurationMacros, MDString *IncludePath, 2424 MDString *APINotesFile, unsigned LineNo, 2425 bool IsDecl = false), 2426 (File, Scope, Name, ConfigurationMacros, IncludePath, 2427 APINotesFile, LineNo, IsDecl)) 2428 2429 TempDIModule clone() const { return cloneImpl(); } 2430 2431 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); } 2432 StringRef getName() const { return getStringOperand(2); } 2433 StringRef getConfigurationMacros() const { return getStringOperand(3); } 2434 StringRef getIncludePath() const { return getStringOperand(4); } 2435 StringRef getAPINotesFile() const { return getStringOperand(5); } 2436 unsigned getLineNo() const { return LineNo; } 2437 bool getIsDecl() const { return IsDecl; } 2438 2439 Metadata *getRawScope() const { return getOperand(1); } 2440 MDString *getRawName() const { return getOperandAs<MDString>(2); } 2441 MDString *getRawConfigurationMacros() const { 2442 return getOperandAs<MDString>(3); 2443 } 2444 MDString *getRawIncludePath() const { return getOperandAs<MDString>(4); } 2445 MDString *getRawAPINotesFile() const { return getOperandAs<MDString>(5); } 2446 2447 static bool classof(const Metadata *MD) { 2448 return MD->getMetadataID() == DIModuleKind; 2449 } 2450 }; 2451 2452 /// Base class for template parameters. 2453 class DITemplateParameter : public DINode { 2454 protected: 2455 bool IsDefault; 2456 2457 DITemplateParameter(LLVMContext &Context, unsigned ID, StorageType Storage, 2458 unsigned Tag, bool IsDefault, ArrayRef<Metadata *> Ops) 2459 : DINode(Context, ID, Storage, Tag, Ops), IsDefault(IsDefault) {} 2460 ~DITemplateParameter() = default; 2461 2462 public: 2463 StringRef getName() const { return getStringOperand(0); } 2464 DIType *getType() const { return cast_or_null<DIType>(getRawType()); } 2465 2466 MDString *getRawName() const { return getOperandAs<MDString>(0); } 2467 Metadata *getRawType() const { return getOperand(1); } 2468 bool isDefault() const { return IsDefault; } 2469 2470 static bool classof(const Metadata *MD) { 2471 return MD->getMetadataID() == DITemplateTypeParameterKind || 2472 MD->getMetadataID() == DITemplateValueParameterKind; 2473 } 2474 }; 2475 2476 class DITemplateTypeParameter : public DITemplateParameter { 2477 friend class LLVMContextImpl; 2478 friend class MDNode; 2479 2480 DITemplateTypeParameter(LLVMContext &Context, StorageType Storage, 2481 bool IsDefault, ArrayRef<Metadata *> Ops); 2482 ~DITemplateTypeParameter() = default; 2483 2484 static DITemplateTypeParameter *getImpl(LLVMContext &Context, StringRef Name, 2485 DIType *Type, bool IsDefault, 2486 StorageType Storage, 2487 bool ShouldCreate = true) { 2488 return getImpl(Context, getCanonicalMDString(Context, Name), Type, 2489 IsDefault, Storage, ShouldCreate); 2490 } 2491 static DITemplateTypeParameter *getImpl(LLVMContext &Context, MDString *Name, 2492 Metadata *Type, bool IsDefault, 2493 StorageType Storage, 2494 bool ShouldCreate = true); 2495 2496 TempDITemplateTypeParameter cloneImpl() const { 2497 return getTemporary(getContext(), getName(), getType(), isDefault()); 2498 } 2499 2500 public: 2501 DEFINE_MDNODE_GET(DITemplateTypeParameter, 2502 (StringRef Name, DIType *Type, bool IsDefault), 2503 (Name, Type, IsDefault)) 2504 DEFINE_MDNODE_GET(DITemplateTypeParameter, 2505 (MDString * Name, Metadata *Type, bool IsDefault), 2506 (Name, Type, IsDefault)) 2507 2508 TempDITemplateTypeParameter clone() const { return cloneImpl(); } 2509 2510 static bool classof(const Metadata *MD) { 2511 return MD->getMetadataID() == DITemplateTypeParameterKind; 2512 } 2513 }; 2514 2515 class DITemplateValueParameter : public DITemplateParameter { 2516 friend class LLVMContextImpl; 2517 friend class MDNode; 2518 2519 DITemplateValueParameter(LLVMContext &Context, StorageType Storage, 2520 unsigned Tag, bool IsDefault, 2521 ArrayRef<Metadata *> Ops) 2522 : DITemplateParameter(Context, DITemplateValueParameterKind, Storage, Tag, 2523 IsDefault, Ops) {} 2524 ~DITemplateValueParameter() = default; 2525 2526 static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag, 2527 StringRef Name, DIType *Type, 2528 bool IsDefault, Metadata *Value, 2529 StorageType Storage, 2530 bool ShouldCreate = true) { 2531 return getImpl(Context, Tag, getCanonicalMDString(Context, Name), Type, 2532 IsDefault, Value, Storage, ShouldCreate); 2533 } 2534 static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag, 2535 MDString *Name, Metadata *Type, 2536 bool IsDefault, Metadata *Value, 2537 StorageType Storage, 2538 bool ShouldCreate = true); 2539 2540 TempDITemplateValueParameter cloneImpl() const { 2541 return getTemporary(getContext(), getTag(), getName(), getType(), 2542 isDefault(), getValue()); 2543 } 2544 2545 public: 2546 DEFINE_MDNODE_GET(DITemplateValueParameter, 2547 (unsigned Tag, StringRef Name, DIType *Type, bool IsDefault, 2548 Metadata *Value), 2549 (Tag, Name, Type, IsDefault, Value)) 2550 DEFINE_MDNODE_GET(DITemplateValueParameter, 2551 (unsigned Tag, MDString *Name, Metadata *Type, 2552 bool IsDefault, Metadata *Value), 2553 (Tag, Name, Type, IsDefault, Value)) 2554 2555 TempDITemplateValueParameter clone() const { return cloneImpl(); } 2556 2557 Metadata *getValue() const { return getOperand(2); } 2558 2559 static bool classof(const Metadata *MD) { 2560 return MD->getMetadataID() == DITemplateValueParameterKind; 2561 } 2562 }; 2563 2564 /// Base class for variables. 2565 class DIVariable : public DINode { 2566 unsigned Line; 2567 uint32_t AlignInBits; 2568 2569 protected: 2570 DIVariable(LLVMContext &C, unsigned ID, StorageType Storage, signed Line, 2571 ArrayRef<Metadata *> Ops, uint32_t AlignInBits = 0); 2572 ~DIVariable() = default; 2573 2574 public: 2575 unsigned getLine() const { return Line; } 2576 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); } 2577 StringRef getName() const { return getStringOperand(1); } 2578 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); } 2579 DIType *getType() const { return cast_or_null<DIType>(getRawType()); } 2580 uint32_t getAlignInBits() const { return AlignInBits; } 2581 uint32_t getAlignInBytes() const { return getAlignInBits() / CHAR_BIT; } 2582 /// Determines the size of the variable's type. 2583 std::optional<uint64_t> getSizeInBits() const; 2584 2585 /// Return the signedness of this variable's type, or std::nullopt if this 2586 /// type is neither signed nor unsigned. 2587 std::optional<DIBasicType::Signedness> getSignedness() const { 2588 if (auto *BT = dyn_cast<DIBasicType>(getType())) 2589 return BT->getSignedness(); 2590 return std::nullopt; 2591 } 2592 2593 StringRef getFilename() const { 2594 if (auto *F = getFile()) 2595 return F->getFilename(); 2596 return ""; 2597 } 2598 2599 StringRef getDirectory() const { 2600 if (auto *F = getFile()) 2601 return F->getDirectory(); 2602 return ""; 2603 } 2604 2605 std::optional<StringRef> getSource() const { 2606 if (auto *F = getFile()) 2607 return F->getSource(); 2608 return std::nullopt; 2609 } 2610 2611 Metadata *getRawScope() const { return getOperand(0); } 2612 MDString *getRawName() const { return getOperandAs<MDString>(1); } 2613 Metadata *getRawFile() const { return getOperand(2); } 2614 Metadata *getRawType() const { return getOperand(3); } 2615 2616 static bool classof(const Metadata *MD) { 2617 return MD->getMetadataID() == DILocalVariableKind || 2618 MD->getMetadataID() == DIGlobalVariableKind; 2619 } 2620 }; 2621 2622 /// DWARF expression. 2623 /// 2624 /// This is (almost) a DWARF expression that modifies the location of a 2625 /// variable, or the location of a single piece of a variable, or (when using 2626 /// DW_OP_stack_value) is the constant variable value. 2627 /// 2628 /// TODO: Co-allocate the expression elements. 2629 /// TODO: Separate from MDNode, or otherwise drop Distinct and Temporary 2630 /// storage types. 2631 class DIExpression : public MDNode { 2632 friend class LLVMContextImpl; 2633 friend class MDNode; 2634 2635 std::vector<uint64_t> Elements; 2636 2637 DIExpression(LLVMContext &C, StorageType Storage, ArrayRef<uint64_t> Elements) 2638 : MDNode(C, DIExpressionKind, Storage, std::nullopt), 2639 Elements(Elements.begin(), Elements.end()) {} 2640 ~DIExpression() = default; 2641 2642 static DIExpression *getImpl(LLVMContext &Context, 2643 ArrayRef<uint64_t> Elements, StorageType Storage, 2644 bool ShouldCreate = true); 2645 2646 TempDIExpression cloneImpl() const { 2647 return getTemporary(getContext(), getElements()); 2648 } 2649 2650 public: 2651 DEFINE_MDNODE_GET(DIExpression, (ArrayRef<uint64_t> Elements), (Elements)) 2652 2653 TempDIExpression clone() const { return cloneImpl(); } 2654 2655 ArrayRef<uint64_t> getElements() const { return Elements; } 2656 2657 unsigned getNumElements() const { return Elements.size(); } 2658 2659 uint64_t getElement(unsigned I) const { 2660 assert(I < Elements.size() && "Index out of range"); 2661 return Elements[I]; 2662 } 2663 2664 enum SignedOrUnsignedConstant { SignedConstant, UnsignedConstant }; 2665 /// Determine whether this represents a constant value, if so 2666 // return it's sign information. 2667 std::optional<SignedOrUnsignedConstant> isConstant() const; 2668 2669 /// Return the number of unique location operands referred to (via 2670 /// DW_OP_LLVM_arg) in this expression; this is not necessarily the number of 2671 /// instances of DW_OP_LLVM_arg within the expression. 2672 /// For example, for the expression: 2673 /// (DW_OP_LLVM_arg 0, DW_OP_LLVM_arg 1, DW_OP_plus, 2674 /// DW_OP_LLVM_arg 0, DW_OP_mul) 2675 /// This function would return 2, as there are two unique location operands 2676 /// (0 and 1). 2677 uint64_t getNumLocationOperands() const; 2678 2679 using element_iterator = ArrayRef<uint64_t>::iterator; 2680 2681 element_iterator elements_begin() const { return getElements().begin(); } 2682 element_iterator elements_end() const { return getElements().end(); } 2683 2684 /// A lightweight wrapper around an expression operand. 2685 /// 2686 /// TODO: Store arguments directly and change \a DIExpression to store a 2687 /// range of these. 2688 class ExprOperand { 2689 const uint64_t *Op = nullptr; 2690 2691 public: 2692 ExprOperand() = default; 2693 explicit ExprOperand(const uint64_t *Op) : Op(Op) {} 2694 2695 const uint64_t *get() const { return Op; } 2696 2697 /// Get the operand code. 2698 uint64_t getOp() const { return *Op; } 2699 2700 /// Get an argument to the operand. 2701 /// 2702 /// Never returns the operand itself. 2703 uint64_t getArg(unsigned I) const { return Op[I + 1]; } 2704 2705 unsigned getNumArgs() const { return getSize() - 1; } 2706 2707 /// Return the size of the operand. 2708 /// 2709 /// Return the number of elements in the operand (1 + args). 2710 unsigned getSize() const; 2711 2712 /// Append the elements of this operand to \p V. 2713 void appendToVector(SmallVectorImpl<uint64_t> &V) const { 2714 V.append(get(), get() + getSize()); 2715 } 2716 }; 2717 2718 /// An iterator for expression operands. 2719 class expr_op_iterator { 2720 ExprOperand Op; 2721 2722 public: 2723 using iterator_category = std::input_iterator_tag; 2724 using value_type = ExprOperand; 2725 using difference_type = std::ptrdiff_t; 2726 using pointer = value_type *; 2727 using reference = value_type &; 2728 2729 expr_op_iterator() = default; 2730 explicit expr_op_iterator(element_iterator I) : Op(I) {} 2731 2732 element_iterator getBase() const { return Op.get(); } 2733 const ExprOperand &operator*() const { return Op; } 2734 const ExprOperand *operator->() const { return &Op; } 2735 2736 expr_op_iterator &operator++() { 2737 increment(); 2738 return *this; 2739 } 2740 expr_op_iterator operator++(int) { 2741 expr_op_iterator T(*this); 2742 increment(); 2743 return T; 2744 } 2745 2746 /// Get the next iterator. 2747 /// 2748 /// \a std::next() doesn't work because this is technically an 2749 /// input_iterator, but it's a perfectly valid operation. This is an 2750 /// accessor to provide the same functionality. 2751 expr_op_iterator getNext() const { return ++expr_op_iterator(*this); } 2752 2753 bool operator==(const expr_op_iterator &X) const { 2754 return getBase() == X.getBase(); 2755 } 2756 bool operator!=(const expr_op_iterator &X) const { 2757 return getBase() != X.getBase(); 2758 } 2759 2760 private: 2761 void increment() { Op = ExprOperand(getBase() + Op.getSize()); } 2762 }; 2763 2764 /// Visit the elements via ExprOperand wrappers. 2765 /// 2766 /// These range iterators visit elements through \a ExprOperand wrappers. 2767 /// This is not guaranteed to be a valid range unless \a isValid() gives \c 2768 /// true. 2769 /// 2770 /// \pre \a isValid() gives \c true. 2771 /// @{ 2772 expr_op_iterator expr_op_begin() const { 2773 return expr_op_iterator(elements_begin()); 2774 } 2775 expr_op_iterator expr_op_end() const { 2776 return expr_op_iterator(elements_end()); 2777 } 2778 iterator_range<expr_op_iterator> expr_ops() const { 2779 return {expr_op_begin(), expr_op_end()}; 2780 } 2781 /// @} 2782 2783 bool isValid() const; 2784 2785 static bool classof(const Metadata *MD) { 2786 return MD->getMetadataID() == DIExpressionKind; 2787 } 2788 2789 /// Return whether the first element a DW_OP_deref. 2790 bool startsWithDeref() const; 2791 2792 /// Return whether there is exactly one operator and it is a DW_OP_deref; 2793 bool isDeref() const; 2794 2795 /// Holds the characteristics of one fragment of a larger variable. 2796 struct FragmentInfo { 2797 FragmentInfo() = default; 2798 FragmentInfo(uint64_t SizeInBits, uint64_t OffsetInBits) 2799 : SizeInBits(SizeInBits), OffsetInBits(OffsetInBits) {} 2800 uint64_t SizeInBits; 2801 uint64_t OffsetInBits; 2802 /// Return the index of the first bit of the fragment. 2803 uint64_t startInBits() const { return OffsetInBits; } 2804 /// Return the index of the bit after the end of the fragment, e.g. for 2805 /// fragment offset=16 and size=32 return their sum, 48. 2806 uint64_t endInBits() const { return OffsetInBits + SizeInBits; } 2807 2808 /// Returns a zero-sized fragment if A and B don't intersect. 2809 static DIExpression::FragmentInfo intersect(DIExpression::FragmentInfo A, 2810 DIExpression::FragmentInfo B) { 2811 uint64_t StartInBits = std::max(A.OffsetInBits, B.OffsetInBits); 2812 uint64_t EndInBits = std::min(A.endInBits(), B.endInBits()); 2813 if (EndInBits <= StartInBits) 2814 return {0, 0}; 2815 return DIExpression::FragmentInfo(EndInBits - StartInBits, StartInBits); 2816 } 2817 }; 2818 2819 /// Retrieve the details of this fragment expression. 2820 static std::optional<FragmentInfo> getFragmentInfo(expr_op_iterator Start, 2821 expr_op_iterator End); 2822 2823 /// Retrieve the details of this fragment expression. 2824 std::optional<FragmentInfo> getFragmentInfo() const { 2825 return getFragmentInfo(expr_op_begin(), expr_op_end()); 2826 } 2827 2828 /// Return whether this is a piece of an aggregate variable. 2829 bool isFragment() const { return getFragmentInfo().has_value(); } 2830 2831 /// Return whether this is an implicit location description. 2832 bool isImplicit() const; 2833 2834 /// Return whether the location is computed on the expression stack, meaning 2835 /// it cannot be a simple register location. 2836 bool isComplex() const; 2837 2838 /// Return whether the evaluated expression makes use of a single location at 2839 /// the start of the expression, i.e. if it contains only a single 2840 /// DW_OP_LLVM_arg op as its first operand, or if it contains none. 2841 bool isSingleLocationExpression() const; 2842 2843 /// Removes all elements from \p Expr that do not apply to an undef debug 2844 /// value, which includes every operator that computes the value/location on 2845 /// the DWARF stack, including any DW_OP_LLVM_arg elements (making the result 2846 /// of this function always a single-location expression) while leaving 2847 /// everything that defines what the computed value applies to, i.e. the 2848 /// fragment information. 2849 static const DIExpression *convertToUndefExpression(const DIExpression *Expr); 2850 2851 /// If \p Expr is a non-variadic expression (i.e. one that does not contain 2852 /// DW_OP_LLVM_arg), returns \p Expr converted to variadic form by adding a 2853 /// leading [DW_OP_LLVM_arg, 0] to the expression; otherwise returns \p Expr. 2854 static const DIExpression * 2855 convertToVariadicExpression(const DIExpression *Expr); 2856 2857 /// If \p Expr is a valid single-location expression, i.e. it refers to only a 2858 /// single debug operand at the start of the expression, then return that 2859 /// expression in a non-variadic form by removing DW_OP_LLVM_arg from the 2860 /// expression if it is present; otherwise returns std::nullopt. 2861 static std::optional<const DIExpression *> 2862 convertToNonVariadicExpression(const DIExpression *Expr); 2863 2864 /// Inserts the elements of \p Expr into \p Ops modified to a canonical form, 2865 /// which uses DW_OP_LLVM_arg (i.e. is a variadic expression) and folds the 2866 /// implied derefence from the \p IsIndirect flag into the expression. This 2867 /// allows us to check equivalence between expressions with differing 2868 /// directness or variadicness. 2869 static void canonicalizeExpressionOps(SmallVectorImpl<uint64_t> &Ops, 2870 const DIExpression *Expr, 2871 bool IsIndirect); 2872 2873 /// Determines whether two debug values should produce equivalent DWARF 2874 /// expressions, using their DIExpressions and directness, ignoring the 2875 /// differences between otherwise identical expressions in variadic and 2876 /// non-variadic form and not considering the debug operands. 2877 /// \p FirstExpr is the DIExpression for the first debug value. 2878 /// \p FirstIndirect should be true if the first debug value is indirect; in 2879 /// IR this should be true for dbg.declare intrinsics and false for 2880 /// dbg.values, and in MIR this should be true only for DBG_VALUE instructions 2881 /// whose second operand is an immediate value. 2882 /// \p SecondExpr and \p SecondIndirect have the same meaning as the prior 2883 /// arguments, but apply to the second debug value. 2884 static bool isEqualExpression(const DIExpression *FirstExpr, 2885 bool FirstIndirect, 2886 const DIExpression *SecondExpr, 2887 bool SecondIndirect); 2888 2889 /// Append \p Ops with operations to apply the \p Offset. 2890 static void appendOffset(SmallVectorImpl<uint64_t> &Ops, int64_t Offset); 2891 2892 /// If this is a constant offset, extract it. If there is no expression, 2893 /// return true with an offset of zero. 2894 bool extractIfOffset(int64_t &Offset) const; 2895 2896 /// Returns true iff this DIExpression contains at least one instance of 2897 /// `DW_OP_LLVM_arg, n` for all n in [0, N). 2898 bool hasAllLocationOps(unsigned N) const; 2899 2900 /// Checks if the last 4 elements of the expression are DW_OP_constu <DWARF 2901 /// Address Space> DW_OP_swap DW_OP_xderef and extracts the <DWARF Address 2902 /// Space>. 2903 static const DIExpression *extractAddressClass(const DIExpression *Expr, 2904 unsigned &AddrClass); 2905 2906 /// Used for DIExpression::prepend. 2907 enum PrependOps : uint8_t { 2908 ApplyOffset = 0, 2909 DerefBefore = 1 << 0, 2910 DerefAfter = 1 << 1, 2911 StackValue = 1 << 2, 2912 EntryValue = 1 << 3 2913 }; 2914 2915 /// Prepend \p DIExpr with a deref and offset operation and optionally turn it 2916 /// into a stack value or/and an entry value. 2917 static DIExpression *prepend(const DIExpression *Expr, uint8_t Flags, 2918 int64_t Offset = 0); 2919 2920 /// Prepend \p DIExpr with the given opcodes and optionally turn it into a 2921 /// stack value. 2922 static DIExpression *prependOpcodes(const DIExpression *Expr, 2923 SmallVectorImpl<uint64_t> &Ops, 2924 bool StackValue = false, 2925 bool EntryValue = false); 2926 2927 /// Append the opcodes \p Ops to \p DIExpr. Unlike \ref appendToStack, the 2928 /// returned expression is a stack value only if \p DIExpr is a stack value. 2929 /// If \p DIExpr describes a fragment, the returned expression will describe 2930 /// the same fragment. 2931 static DIExpression *append(const DIExpression *Expr, ArrayRef<uint64_t> Ops); 2932 2933 /// Convert \p DIExpr into a stack value if it isn't one already by appending 2934 /// DW_OP_deref if needed, and appending \p Ops to the resulting expression. 2935 /// If \p DIExpr describes a fragment, the returned expression will describe 2936 /// the same fragment. 2937 static DIExpression *appendToStack(const DIExpression *Expr, 2938 ArrayRef<uint64_t> Ops); 2939 2940 /// Create a copy of \p Expr by appending the given list of \p Ops to each 2941 /// instance of the operand `DW_OP_LLVM_arg, \p ArgNo`. This is used to 2942 /// modify a specific location used by \p Expr, such as when salvaging that 2943 /// location. 2944 static DIExpression *appendOpsToArg(const DIExpression *Expr, 2945 ArrayRef<uint64_t> Ops, unsigned ArgNo, 2946 bool StackValue = false); 2947 2948 /// Create a copy of \p Expr with each instance of 2949 /// `DW_OP_LLVM_arg, \p OldArg` replaced with `DW_OP_LLVM_arg, \p NewArg`, 2950 /// and each instance of `DW_OP_LLVM_arg, Arg` with `DW_OP_LLVM_arg, Arg - 1` 2951 /// for all Arg > \p OldArg. 2952 /// This is used when replacing one of the operands of a debug value list 2953 /// with another operand in the same list and deleting the old operand. 2954 static DIExpression *replaceArg(const DIExpression *Expr, uint64_t OldArg, 2955 uint64_t NewArg); 2956 2957 /// Create a DIExpression to describe one part of an aggregate variable that 2958 /// is fragmented across multiple Values. The DW_OP_LLVM_fragment operation 2959 /// will be appended to the elements of \c Expr. If \c Expr already contains 2960 /// a \c DW_OP_LLVM_fragment \c OffsetInBits is interpreted as an offset 2961 /// into the existing fragment. 2962 /// 2963 /// \param OffsetInBits Offset of the piece in bits. 2964 /// \param SizeInBits Size of the piece in bits. 2965 /// \return Creating a fragment expression may fail if \c Expr 2966 /// contains arithmetic operations that would be 2967 /// truncated. 2968 static std::optional<DIExpression *> 2969 createFragmentExpression(const DIExpression *Expr, unsigned OffsetInBits, 2970 unsigned SizeInBits); 2971 2972 /// Determine the relative position of the fragments passed in. 2973 /// Returns -1 if this is entirely before Other, 0 if this and Other overlap, 2974 /// 1 if this is entirely after Other. 2975 static int fragmentCmp(const FragmentInfo &A, const FragmentInfo &B) { 2976 uint64_t l1 = A.OffsetInBits; 2977 uint64_t l2 = B.OffsetInBits; 2978 uint64_t r1 = l1 + A.SizeInBits; 2979 uint64_t r2 = l2 + B.SizeInBits; 2980 if (r1 <= l2) 2981 return -1; 2982 else if (r2 <= l1) 2983 return 1; 2984 else 2985 return 0; 2986 } 2987 2988 using ExtOps = std::array<uint64_t, 6>; 2989 2990 /// Returns the ops for a zero- or sign-extension in a DIExpression. 2991 static ExtOps getExtOps(unsigned FromSize, unsigned ToSize, bool Signed); 2992 2993 /// Append a zero- or sign-extension to \p Expr. Converts the expression to a 2994 /// stack value if it isn't one already. 2995 static DIExpression *appendExt(const DIExpression *Expr, unsigned FromSize, 2996 unsigned ToSize, bool Signed); 2997 2998 /// Check if fragments overlap between a pair of FragmentInfos. 2999 static bool fragmentsOverlap(const FragmentInfo &A, const FragmentInfo &B) { 3000 return fragmentCmp(A, B) == 0; 3001 } 3002 3003 /// Determine the relative position of the fragments described by this 3004 /// DIExpression and \p Other. Calls static fragmentCmp implementation. 3005 int fragmentCmp(const DIExpression *Other) const { 3006 auto Fragment1 = *getFragmentInfo(); 3007 auto Fragment2 = *Other->getFragmentInfo(); 3008 return fragmentCmp(Fragment1, Fragment2); 3009 } 3010 3011 /// Check if fragments overlap between this DIExpression and \p Other. 3012 bool fragmentsOverlap(const DIExpression *Other) const { 3013 if (!isFragment() || !Other->isFragment()) 3014 return true; 3015 return fragmentCmp(Other) == 0; 3016 } 3017 3018 /// Check if the expression consists of exactly one entry value operand. 3019 /// (This is the only configuration of entry values that is supported.) 3020 bool isEntryValue() const; 3021 3022 /// Try to shorten an expression with an initial constant operand. 3023 /// Returns a new expression and constant on success, or the original 3024 /// expression and constant on failure. 3025 std::pair<DIExpression *, const ConstantInt *> 3026 constantFold(const ConstantInt *CI); 3027 }; 3028 3029 inline bool operator==(const DIExpression::FragmentInfo &A, 3030 const DIExpression::FragmentInfo &B) { 3031 return std::tie(A.SizeInBits, A.OffsetInBits) == 3032 std::tie(B.SizeInBits, B.OffsetInBits); 3033 } 3034 3035 inline bool operator<(const DIExpression::FragmentInfo &A, 3036 const DIExpression::FragmentInfo &B) { 3037 return std::tie(A.SizeInBits, A.OffsetInBits) < 3038 std::tie(B.SizeInBits, B.OffsetInBits); 3039 } 3040 3041 template <> struct DenseMapInfo<DIExpression::FragmentInfo> { 3042 using FragInfo = DIExpression::FragmentInfo; 3043 static const uint64_t MaxVal = std::numeric_limits<uint64_t>::max(); 3044 3045 static inline FragInfo getEmptyKey() { return {MaxVal, MaxVal}; } 3046 3047 static inline FragInfo getTombstoneKey() { return {MaxVal - 1, MaxVal - 1}; } 3048 3049 static unsigned getHashValue(const FragInfo &Frag) { 3050 return (Frag.SizeInBits & 0xffff) << 16 | (Frag.OffsetInBits & 0xffff); 3051 } 3052 3053 static bool isEqual(const FragInfo &A, const FragInfo &B) { return A == B; } 3054 }; 3055 3056 /// Global variables. 3057 /// 3058 /// TODO: Remove DisplayName. It's always equal to Name. 3059 class DIGlobalVariable : public DIVariable { 3060 friend class LLVMContextImpl; 3061 friend class MDNode; 3062 3063 bool IsLocalToUnit; 3064 bool IsDefinition; 3065 3066 DIGlobalVariable(LLVMContext &C, StorageType Storage, unsigned Line, 3067 bool IsLocalToUnit, bool IsDefinition, uint32_t AlignInBits, 3068 ArrayRef<Metadata *> Ops) 3069 : DIVariable(C, DIGlobalVariableKind, Storage, Line, Ops, AlignInBits), 3070 IsLocalToUnit(IsLocalToUnit), IsDefinition(IsDefinition) {} 3071 ~DIGlobalVariable() = default; 3072 3073 static DIGlobalVariable * 3074 getImpl(LLVMContext &Context, DIScope *Scope, StringRef Name, 3075 StringRef LinkageName, DIFile *File, unsigned Line, DIType *Type, 3076 bool IsLocalToUnit, bool IsDefinition, 3077 DIDerivedType *StaticDataMemberDeclaration, MDTuple *TemplateParams, 3078 uint32_t AlignInBits, DINodeArray Annotations, StorageType Storage, 3079 bool ShouldCreate = true) { 3080 return getImpl(Context, Scope, getCanonicalMDString(Context, Name), 3081 getCanonicalMDString(Context, LinkageName), File, Line, Type, 3082 IsLocalToUnit, IsDefinition, StaticDataMemberDeclaration, 3083 cast_or_null<Metadata>(TemplateParams), AlignInBits, 3084 Annotations.get(), Storage, ShouldCreate); 3085 } 3086 static DIGlobalVariable * 3087 getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name, 3088 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type, 3089 bool IsLocalToUnit, bool IsDefinition, 3090 Metadata *StaticDataMemberDeclaration, Metadata *TemplateParams, 3091 uint32_t AlignInBits, Metadata *Annotations, StorageType Storage, 3092 bool ShouldCreate = true); 3093 3094 TempDIGlobalVariable cloneImpl() const { 3095 return getTemporary(getContext(), getScope(), getName(), getLinkageName(), 3096 getFile(), getLine(), getType(), isLocalToUnit(), 3097 isDefinition(), getStaticDataMemberDeclaration(), 3098 getTemplateParams(), getAlignInBits(), 3099 getAnnotations()); 3100 } 3101 3102 public: 3103 DEFINE_MDNODE_GET( 3104 DIGlobalVariable, 3105 (DIScope * Scope, StringRef Name, StringRef LinkageName, DIFile *File, 3106 unsigned Line, DIType *Type, bool IsLocalToUnit, bool IsDefinition, 3107 DIDerivedType *StaticDataMemberDeclaration, MDTuple *TemplateParams, 3108 uint32_t AlignInBits, DINodeArray Annotations), 3109 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, IsDefinition, 3110 StaticDataMemberDeclaration, TemplateParams, AlignInBits, Annotations)) 3111 DEFINE_MDNODE_GET( 3112 DIGlobalVariable, 3113 (Metadata * Scope, MDString *Name, MDString *LinkageName, Metadata *File, 3114 unsigned Line, Metadata *Type, bool IsLocalToUnit, bool IsDefinition, 3115 Metadata *StaticDataMemberDeclaration, Metadata *TemplateParams, 3116 uint32_t AlignInBits, Metadata *Annotations), 3117 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, IsDefinition, 3118 StaticDataMemberDeclaration, TemplateParams, AlignInBits, Annotations)) 3119 3120 TempDIGlobalVariable clone() const { return cloneImpl(); } 3121 3122 bool isLocalToUnit() const { return IsLocalToUnit; } 3123 bool isDefinition() const { return IsDefinition; } 3124 StringRef getDisplayName() const { return getStringOperand(4); } 3125 StringRef getLinkageName() const { return getStringOperand(5); } 3126 DIDerivedType *getStaticDataMemberDeclaration() const { 3127 return cast_or_null<DIDerivedType>(getRawStaticDataMemberDeclaration()); 3128 } 3129 DINodeArray getAnnotations() const { 3130 return cast_or_null<MDTuple>(getRawAnnotations()); 3131 } 3132 3133 MDString *getRawLinkageName() const { return getOperandAs<MDString>(5); } 3134 Metadata *getRawStaticDataMemberDeclaration() const { return getOperand(6); } 3135 Metadata *getRawTemplateParams() const { return getOperand(7); } 3136 MDTuple *getTemplateParams() const { return getOperandAs<MDTuple>(7); } 3137 Metadata *getRawAnnotations() const { return getOperand(8); } 3138 3139 static bool classof(const Metadata *MD) { 3140 return MD->getMetadataID() == DIGlobalVariableKind; 3141 } 3142 }; 3143 3144 class DICommonBlock : public DIScope { 3145 unsigned LineNo; 3146 3147 friend class LLVMContextImpl; 3148 friend class MDNode; 3149 3150 DICommonBlock(LLVMContext &Context, StorageType Storage, unsigned LineNo, 3151 ArrayRef<Metadata *> Ops); 3152 3153 static DICommonBlock *getImpl(LLVMContext &Context, DIScope *Scope, 3154 DIGlobalVariable *Decl, StringRef Name, 3155 DIFile *File, unsigned LineNo, 3156 StorageType Storage, bool ShouldCreate = true) { 3157 return getImpl(Context, Scope, Decl, getCanonicalMDString(Context, Name), 3158 File, LineNo, Storage, ShouldCreate); 3159 } 3160 static DICommonBlock *getImpl(LLVMContext &Context, Metadata *Scope, 3161 Metadata *Decl, MDString *Name, Metadata *File, 3162 unsigned LineNo, StorageType Storage, 3163 bool ShouldCreate = true); 3164 3165 TempDICommonBlock cloneImpl() const { 3166 return getTemporary(getContext(), getScope(), getDecl(), getName(), 3167 getFile(), getLineNo()); 3168 } 3169 3170 public: 3171 DEFINE_MDNODE_GET(DICommonBlock, 3172 (DIScope * Scope, DIGlobalVariable *Decl, StringRef Name, 3173 DIFile *File, unsigned LineNo), 3174 (Scope, Decl, Name, File, LineNo)) 3175 DEFINE_MDNODE_GET(DICommonBlock, 3176 (Metadata * Scope, Metadata *Decl, MDString *Name, 3177 Metadata *File, unsigned LineNo), 3178 (Scope, Decl, Name, File, LineNo)) 3179 3180 TempDICommonBlock clone() const { return cloneImpl(); } 3181 3182 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); } 3183 DIGlobalVariable *getDecl() const { 3184 return cast_or_null<DIGlobalVariable>(getRawDecl()); 3185 } 3186 StringRef getName() const { return getStringOperand(2); } 3187 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); } 3188 unsigned getLineNo() const { return LineNo; } 3189 3190 Metadata *getRawScope() const { return getOperand(0); } 3191 Metadata *getRawDecl() const { return getOperand(1); } 3192 MDString *getRawName() const { return getOperandAs<MDString>(2); } 3193 Metadata *getRawFile() const { return getOperand(3); } 3194 3195 static bool classof(const Metadata *MD) { 3196 return MD->getMetadataID() == DICommonBlockKind; 3197 } 3198 }; 3199 3200 /// Local variable. 3201 /// 3202 /// TODO: Split up flags. 3203 class DILocalVariable : public DIVariable { 3204 friend class LLVMContextImpl; 3205 friend class MDNode; 3206 3207 unsigned Arg : 16; 3208 DIFlags Flags; 3209 3210 DILocalVariable(LLVMContext &C, StorageType Storage, unsigned Line, 3211 unsigned Arg, DIFlags Flags, uint32_t AlignInBits, 3212 ArrayRef<Metadata *> Ops) 3213 : DIVariable(C, DILocalVariableKind, Storage, Line, Ops, AlignInBits), 3214 Arg(Arg), Flags(Flags) { 3215 assert(Arg < (1 << 16) && "DILocalVariable: Arg out of range"); 3216 } 3217 ~DILocalVariable() = default; 3218 3219 static DILocalVariable *getImpl(LLVMContext &Context, DIScope *Scope, 3220 StringRef Name, DIFile *File, unsigned Line, 3221 DIType *Type, unsigned Arg, DIFlags Flags, 3222 uint32_t AlignInBits, DINodeArray Annotations, 3223 StorageType Storage, 3224 bool ShouldCreate = true) { 3225 return getImpl(Context, Scope, getCanonicalMDString(Context, Name), File, 3226 Line, Type, Arg, Flags, AlignInBits, Annotations.get(), 3227 Storage, ShouldCreate); 3228 } 3229 static DILocalVariable *getImpl(LLVMContext &Context, Metadata *Scope, 3230 MDString *Name, Metadata *File, unsigned Line, 3231 Metadata *Type, unsigned Arg, DIFlags Flags, 3232 uint32_t AlignInBits, Metadata *Annotations, 3233 StorageType Storage, 3234 bool ShouldCreate = true); 3235 3236 TempDILocalVariable cloneImpl() const { 3237 return getTemporary(getContext(), getScope(), getName(), getFile(), 3238 getLine(), getType(), getArg(), getFlags(), 3239 getAlignInBits(), getAnnotations()); 3240 } 3241 3242 public: 3243 DEFINE_MDNODE_GET(DILocalVariable, 3244 (DILocalScope * Scope, StringRef Name, DIFile *File, 3245 unsigned Line, DIType *Type, unsigned Arg, DIFlags Flags, 3246 uint32_t AlignInBits, DINodeArray Annotations), 3247 (Scope, Name, File, Line, Type, Arg, Flags, AlignInBits, 3248 Annotations)) 3249 DEFINE_MDNODE_GET(DILocalVariable, 3250 (Metadata * Scope, MDString *Name, Metadata *File, 3251 unsigned Line, Metadata *Type, unsigned Arg, DIFlags Flags, 3252 uint32_t AlignInBits, Metadata *Annotations), 3253 (Scope, Name, File, Line, Type, Arg, Flags, AlignInBits, 3254 Annotations)) 3255 3256 TempDILocalVariable clone() const { return cloneImpl(); } 3257 3258 /// Get the local scope for this variable. 3259 /// 3260 /// Variables must be defined in a local scope. 3261 DILocalScope *getScope() const { 3262 return cast<DILocalScope>(DIVariable::getScope()); 3263 } 3264 3265 bool isParameter() const { return Arg; } 3266 unsigned getArg() const { return Arg; } 3267 DIFlags getFlags() const { return Flags; } 3268 3269 DINodeArray getAnnotations() const { 3270 return cast_or_null<MDTuple>(getRawAnnotations()); 3271 } 3272 Metadata *getRawAnnotations() const { return getOperand(4); } 3273 3274 bool isArtificial() const { return getFlags() & FlagArtificial; } 3275 bool isObjectPointer() const { return getFlags() & FlagObjectPointer; } 3276 3277 /// Check that a location is valid for this variable. 3278 /// 3279 /// Check that \c DL exists, is in the same subprogram, and has the same 3280 /// inlined-at location as \c this. (Otherwise, it's not a valid attachment 3281 /// to a \a DbgInfoIntrinsic.) 3282 bool isValidLocationForIntrinsic(const DILocation *DL) const { 3283 return DL && getScope()->getSubprogram() == DL->getScope()->getSubprogram(); 3284 } 3285 3286 static bool classof(const Metadata *MD) { 3287 return MD->getMetadataID() == DILocalVariableKind; 3288 } 3289 }; 3290 3291 /// Label. 3292 /// 3293 class DILabel : public DINode { 3294 friend class LLVMContextImpl; 3295 friend class MDNode; 3296 3297 unsigned Line; 3298 3299 DILabel(LLVMContext &C, StorageType Storage, unsigned Line, 3300 ArrayRef<Metadata *> Ops); 3301 ~DILabel() = default; 3302 3303 static DILabel *getImpl(LLVMContext &Context, DIScope *Scope, StringRef Name, 3304 DIFile *File, unsigned Line, StorageType Storage, 3305 bool ShouldCreate = true) { 3306 return getImpl(Context, Scope, getCanonicalMDString(Context, Name), File, 3307 Line, Storage, ShouldCreate); 3308 } 3309 static DILabel *getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name, 3310 Metadata *File, unsigned Line, StorageType Storage, 3311 bool ShouldCreate = true); 3312 3313 TempDILabel cloneImpl() const { 3314 return getTemporary(getContext(), getScope(), getName(), getFile(), 3315 getLine()); 3316 } 3317 3318 public: 3319 DEFINE_MDNODE_GET(DILabel, 3320 (DILocalScope * Scope, StringRef Name, DIFile *File, 3321 unsigned Line), 3322 (Scope, Name, File, Line)) 3323 DEFINE_MDNODE_GET(DILabel, 3324 (Metadata * Scope, MDString *Name, Metadata *File, 3325 unsigned Line), 3326 (Scope, Name, File, Line)) 3327 3328 TempDILabel clone() const { return cloneImpl(); } 3329 3330 /// Get the local scope for this label. 3331 /// 3332 /// Labels must be defined in a local scope. 3333 DILocalScope *getScope() const { 3334 return cast_or_null<DILocalScope>(getRawScope()); 3335 } 3336 unsigned getLine() const { return Line; } 3337 StringRef getName() const { return getStringOperand(1); } 3338 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); } 3339 3340 Metadata *getRawScope() const { return getOperand(0); } 3341 MDString *getRawName() const { return getOperandAs<MDString>(1); } 3342 Metadata *getRawFile() const { return getOperand(2); } 3343 3344 /// Check that a location is valid for this label. 3345 /// 3346 /// Check that \c DL exists, is in the same subprogram, and has the same 3347 /// inlined-at location as \c this. (Otherwise, it's not a valid attachment 3348 /// to a \a DbgInfoIntrinsic.) 3349 bool isValidLocationForIntrinsic(const DILocation *DL) const { 3350 return DL && getScope()->getSubprogram() == DL->getScope()->getSubprogram(); 3351 } 3352 3353 static bool classof(const Metadata *MD) { 3354 return MD->getMetadataID() == DILabelKind; 3355 } 3356 }; 3357 3358 class DIObjCProperty : public DINode { 3359 friend class LLVMContextImpl; 3360 friend class MDNode; 3361 3362 unsigned Line; 3363 unsigned Attributes; 3364 3365 DIObjCProperty(LLVMContext &C, StorageType Storage, unsigned Line, 3366 unsigned Attributes, ArrayRef<Metadata *> Ops); 3367 ~DIObjCProperty() = default; 3368 3369 static DIObjCProperty * 3370 getImpl(LLVMContext &Context, StringRef Name, DIFile *File, unsigned Line, 3371 StringRef GetterName, StringRef SetterName, unsigned Attributes, 3372 DIType *Type, StorageType Storage, bool ShouldCreate = true) { 3373 return getImpl(Context, getCanonicalMDString(Context, Name), File, Line, 3374 getCanonicalMDString(Context, GetterName), 3375 getCanonicalMDString(Context, SetterName), Attributes, Type, 3376 Storage, ShouldCreate); 3377 } 3378 static DIObjCProperty *getImpl(LLVMContext &Context, MDString *Name, 3379 Metadata *File, unsigned Line, 3380 MDString *GetterName, MDString *SetterName, 3381 unsigned Attributes, Metadata *Type, 3382 StorageType Storage, bool ShouldCreate = true); 3383 3384 TempDIObjCProperty cloneImpl() const { 3385 return getTemporary(getContext(), getName(), getFile(), getLine(), 3386 getGetterName(), getSetterName(), getAttributes(), 3387 getType()); 3388 } 3389 3390 public: 3391 DEFINE_MDNODE_GET(DIObjCProperty, 3392 (StringRef Name, DIFile *File, unsigned Line, 3393 StringRef GetterName, StringRef SetterName, 3394 unsigned Attributes, DIType *Type), 3395 (Name, File, Line, GetterName, SetterName, Attributes, 3396 Type)) 3397 DEFINE_MDNODE_GET(DIObjCProperty, 3398 (MDString * Name, Metadata *File, unsigned Line, 3399 MDString *GetterName, MDString *SetterName, 3400 unsigned Attributes, Metadata *Type), 3401 (Name, File, Line, GetterName, SetterName, Attributes, 3402 Type)) 3403 3404 TempDIObjCProperty clone() const { return cloneImpl(); } 3405 3406 unsigned getLine() const { return Line; } 3407 unsigned getAttributes() const { return Attributes; } 3408 StringRef getName() const { return getStringOperand(0); } 3409 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); } 3410 StringRef getGetterName() const { return getStringOperand(2); } 3411 StringRef getSetterName() const { return getStringOperand(3); } 3412 DIType *getType() const { return cast_or_null<DIType>(getRawType()); } 3413 3414 StringRef getFilename() const { 3415 if (auto *F = getFile()) 3416 return F->getFilename(); 3417 return ""; 3418 } 3419 3420 StringRef getDirectory() const { 3421 if (auto *F = getFile()) 3422 return F->getDirectory(); 3423 return ""; 3424 } 3425 3426 MDString *getRawName() const { return getOperandAs<MDString>(0); } 3427 Metadata *getRawFile() const { return getOperand(1); } 3428 MDString *getRawGetterName() const { return getOperandAs<MDString>(2); } 3429 MDString *getRawSetterName() const { return getOperandAs<MDString>(3); } 3430 Metadata *getRawType() const { return getOperand(4); } 3431 3432 static bool classof(const Metadata *MD) { 3433 return MD->getMetadataID() == DIObjCPropertyKind; 3434 } 3435 }; 3436 3437 /// An imported module (C++ using directive or similar). 3438 class DIImportedEntity : public DINode { 3439 friend class LLVMContextImpl; 3440 friend class MDNode; 3441 3442 unsigned Line; 3443 3444 DIImportedEntity(LLVMContext &C, StorageType Storage, unsigned Tag, 3445 unsigned Line, ArrayRef<Metadata *> Ops) 3446 : DINode(C, DIImportedEntityKind, Storage, Tag, Ops), Line(Line) {} 3447 ~DIImportedEntity() = default; 3448 3449 static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag, 3450 DIScope *Scope, DINode *Entity, DIFile *File, 3451 unsigned Line, StringRef Name, 3452 DINodeArray Elements, StorageType Storage, 3453 bool ShouldCreate = true) { 3454 return getImpl(Context, Tag, Scope, Entity, File, Line, 3455 getCanonicalMDString(Context, Name), Elements.get(), Storage, 3456 ShouldCreate); 3457 } 3458 static DIImportedEntity * 3459 getImpl(LLVMContext &Context, unsigned Tag, Metadata *Scope, Metadata *Entity, 3460 Metadata *File, unsigned Line, MDString *Name, Metadata *Elements, 3461 StorageType Storage, bool ShouldCreate = true); 3462 3463 TempDIImportedEntity cloneImpl() const { 3464 return getTemporary(getContext(), getTag(), getScope(), getEntity(), 3465 getFile(), getLine(), getName(), getElements()); 3466 } 3467 3468 public: 3469 DEFINE_MDNODE_GET(DIImportedEntity, 3470 (unsigned Tag, DIScope *Scope, DINode *Entity, DIFile *File, 3471 unsigned Line, StringRef Name = "", 3472 DINodeArray Elements = nullptr), 3473 (Tag, Scope, Entity, File, Line, Name, Elements)) 3474 DEFINE_MDNODE_GET(DIImportedEntity, 3475 (unsigned Tag, Metadata *Scope, Metadata *Entity, 3476 Metadata *File, unsigned Line, MDString *Name, 3477 Metadata *Elements = nullptr), 3478 (Tag, Scope, Entity, File, Line, Name, Elements)) 3479 3480 TempDIImportedEntity clone() const { return cloneImpl(); } 3481 3482 unsigned getLine() const { return Line; } 3483 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); } 3484 DINode *getEntity() const { return cast_or_null<DINode>(getRawEntity()); } 3485 StringRef getName() const { return getStringOperand(2); } 3486 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); } 3487 DINodeArray getElements() const { 3488 return cast_or_null<MDTuple>(getRawElements()); 3489 } 3490 3491 Metadata *getRawScope() const { return getOperand(0); } 3492 Metadata *getRawEntity() const { return getOperand(1); } 3493 MDString *getRawName() const { return getOperandAs<MDString>(2); } 3494 Metadata *getRawFile() const { return getOperand(3); } 3495 Metadata *getRawElements() const { return getOperand(4); } 3496 3497 static bool classof(const Metadata *MD) { 3498 return MD->getMetadataID() == DIImportedEntityKind; 3499 } 3500 }; 3501 3502 /// A pair of DIGlobalVariable and DIExpression. 3503 class DIGlobalVariableExpression : public MDNode { 3504 friend class LLVMContextImpl; 3505 friend class MDNode; 3506 3507 DIGlobalVariableExpression(LLVMContext &C, StorageType Storage, 3508 ArrayRef<Metadata *> Ops) 3509 : MDNode(C, DIGlobalVariableExpressionKind, Storage, Ops) {} 3510 ~DIGlobalVariableExpression() = default; 3511 3512 static DIGlobalVariableExpression * 3513 getImpl(LLVMContext &Context, Metadata *Variable, Metadata *Expression, 3514 StorageType Storage, bool ShouldCreate = true); 3515 3516 TempDIGlobalVariableExpression cloneImpl() const { 3517 return getTemporary(getContext(), getVariable(), getExpression()); 3518 } 3519 3520 public: 3521 DEFINE_MDNODE_GET(DIGlobalVariableExpression, 3522 (Metadata * Variable, Metadata *Expression), 3523 (Variable, Expression)) 3524 3525 TempDIGlobalVariableExpression clone() const { return cloneImpl(); } 3526 3527 Metadata *getRawVariable() const { return getOperand(0); } 3528 3529 DIGlobalVariable *getVariable() const { 3530 return cast_or_null<DIGlobalVariable>(getRawVariable()); 3531 } 3532 3533 Metadata *getRawExpression() const { return getOperand(1); } 3534 3535 DIExpression *getExpression() const { 3536 return cast<DIExpression>(getRawExpression()); 3537 } 3538 3539 static bool classof(const Metadata *MD) { 3540 return MD->getMetadataID() == DIGlobalVariableExpressionKind; 3541 } 3542 }; 3543 3544 /// Macro Info DWARF-like metadata node. 3545 /// 3546 /// A metadata node with a DWARF macro info (i.e., a constant named 3547 /// \c DW_MACINFO_*, defined in llvm/BinaryFormat/Dwarf.h). Called \a 3548 /// DIMacroNode 3549 /// because it's potentially used for non-DWARF output. 3550 class DIMacroNode : public MDNode { 3551 friend class LLVMContextImpl; 3552 friend class MDNode; 3553 3554 protected: 3555 DIMacroNode(LLVMContext &C, unsigned ID, StorageType Storage, unsigned MIType, 3556 ArrayRef<Metadata *> Ops1, 3557 ArrayRef<Metadata *> Ops2 = std::nullopt) 3558 : MDNode(C, ID, Storage, Ops1, Ops2) { 3559 assert(MIType < 1u << 16); 3560 SubclassData16 = MIType; 3561 } 3562 ~DIMacroNode() = default; 3563 3564 template <class Ty> Ty *getOperandAs(unsigned I) const { 3565 return cast_or_null<Ty>(getOperand(I)); 3566 } 3567 3568 StringRef getStringOperand(unsigned I) const { 3569 if (auto *S = getOperandAs<MDString>(I)) 3570 return S->getString(); 3571 return StringRef(); 3572 } 3573 3574 static MDString *getCanonicalMDString(LLVMContext &Context, StringRef S) { 3575 if (S.empty()) 3576 return nullptr; 3577 return MDString::get(Context, S); 3578 } 3579 3580 public: 3581 unsigned getMacinfoType() const { return SubclassData16; } 3582 3583 static bool classof(const Metadata *MD) { 3584 switch (MD->getMetadataID()) { 3585 default: 3586 return false; 3587 case DIMacroKind: 3588 case DIMacroFileKind: 3589 return true; 3590 } 3591 } 3592 }; 3593 3594 class DIMacro : public DIMacroNode { 3595 friend class LLVMContextImpl; 3596 friend class MDNode; 3597 3598 unsigned Line; 3599 3600 DIMacro(LLVMContext &C, StorageType Storage, unsigned MIType, unsigned Line, 3601 ArrayRef<Metadata *> Ops) 3602 : DIMacroNode(C, DIMacroKind, Storage, MIType, Ops), Line(Line) {} 3603 ~DIMacro() = default; 3604 3605 static DIMacro *getImpl(LLVMContext &Context, unsigned MIType, unsigned Line, 3606 StringRef Name, StringRef Value, StorageType Storage, 3607 bool ShouldCreate = true) { 3608 return getImpl(Context, MIType, Line, getCanonicalMDString(Context, Name), 3609 getCanonicalMDString(Context, Value), Storage, ShouldCreate); 3610 } 3611 static DIMacro *getImpl(LLVMContext &Context, unsigned MIType, unsigned Line, 3612 MDString *Name, MDString *Value, StorageType Storage, 3613 bool ShouldCreate = true); 3614 3615 TempDIMacro cloneImpl() const { 3616 return getTemporary(getContext(), getMacinfoType(), getLine(), getName(), 3617 getValue()); 3618 } 3619 3620 public: 3621 DEFINE_MDNODE_GET(DIMacro, 3622 (unsigned MIType, unsigned Line, StringRef Name, 3623 StringRef Value = ""), 3624 (MIType, Line, Name, Value)) 3625 DEFINE_MDNODE_GET(DIMacro, 3626 (unsigned MIType, unsigned Line, MDString *Name, 3627 MDString *Value), 3628 (MIType, Line, Name, Value)) 3629 3630 TempDIMacro clone() const { return cloneImpl(); } 3631 3632 unsigned getLine() const { return Line; } 3633 3634 StringRef getName() const { return getStringOperand(0); } 3635 StringRef getValue() const { return getStringOperand(1); } 3636 3637 MDString *getRawName() const { return getOperandAs<MDString>(0); } 3638 MDString *getRawValue() const { return getOperandAs<MDString>(1); } 3639 3640 static bool classof(const Metadata *MD) { 3641 return MD->getMetadataID() == DIMacroKind; 3642 } 3643 }; 3644 3645 class DIMacroFile : public DIMacroNode { 3646 friend class LLVMContextImpl; 3647 friend class MDNode; 3648 3649 unsigned Line; 3650 3651 DIMacroFile(LLVMContext &C, StorageType Storage, unsigned MIType, 3652 unsigned Line, ArrayRef<Metadata *> Ops) 3653 : DIMacroNode(C, DIMacroFileKind, Storage, MIType, Ops), Line(Line) {} 3654 ~DIMacroFile() = default; 3655 3656 static DIMacroFile *getImpl(LLVMContext &Context, unsigned MIType, 3657 unsigned Line, DIFile *File, 3658 DIMacroNodeArray Elements, StorageType Storage, 3659 bool ShouldCreate = true) { 3660 return getImpl(Context, MIType, Line, static_cast<Metadata *>(File), 3661 Elements.get(), Storage, ShouldCreate); 3662 } 3663 3664 static DIMacroFile *getImpl(LLVMContext &Context, unsigned MIType, 3665 unsigned Line, Metadata *File, Metadata *Elements, 3666 StorageType Storage, bool ShouldCreate = true); 3667 3668 TempDIMacroFile cloneImpl() const { 3669 return getTemporary(getContext(), getMacinfoType(), getLine(), getFile(), 3670 getElements()); 3671 } 3672 3673 public: 3674 DEFINE_MDNODE_GET(DIMacroFile, 3675 (unsigned MIType, unsigned Line, DIFile *File, 3676 DIMacroNodeArray Elements), 3677 (MIType, Line, File, Elements)) 3678 DEFINE_MDNODE_GET(DIMacroFile, 3679 (unsigned MIType, unsigned Line, Metadata *File, 3680 Metadata *Elements), 3681 (MIType, Line, File, Elements)) 3682 3683 TempDIMacroFile clone() const { return cloneImpl(); } 3684 3685 void replaceElements(DIMacroNodeArray Elements) { 3686 #ifndef NDEBUG 3687 for (DIMacroNode *Op : getElements()) 3688 assert(is_contained(Elements->operands(), Op) && 3689 "Lost a macro node during macro node list replacement"); 3690 #endif 3691 replaceOperandWith(1, Elements.get()); 3692 } 3693 3694 unsigned getLine() const { return Line; } 3695 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); } 3696 3697 DIMacroNodeArray getElements() const { 3698 return cast_or_null<MDTuple>(getRawElements()); 3699 } 3700 3701 Metadata *getRawFile() const { return getOperand(0); } 3702 Metadata *getRawElements() const { return getOperand(1); } 3703 3704 static bool classof(const Metadata *MD) { 3705 return MD->getMetadataID() == DIMacroFileKind; 3706 } 3707 }; 3708 3709 /// List of ValueAsMetadata, to be used as an argument to a dbg.value 3710 /// intrinsic. 3711 class DIArgList : public MDNode { 3712 friend class LLVMContextImpl; 3713 friend class MDNode; 3714 using iterator = SmallVectorImpl<ValueAsMetadata *>::iterator; 3715 3716 SmallVector<ValueAsMetadata *, 4> Args; 3717 3718 DIArgList(LLVMContext &C, StorageType Storage, 3719 ArrayRef<ValueAsMetadata *> Args) 3720 : MDNode(C, DIArgListKind, Storage, std::nullopt), 3721 Args(Args.begin(), Args.end()) { 3722 track(); 3723 } 3724 ~DIArgList() { untrack(); } 3725 3726 static DIArgList *getImpl(LLVMContext &Context, 3727 ArrayRef<ValueAsMetadata *> Args, 3728 StorageType Storage, bool ShouldCreate = true); 3729 3730 TempDIArgList cloneImpl() const { 3731 return getTemporary(getContext(), getArgs()); 3732 } 3733 3734 void track(); 3735 void untrack(); 3736 void dropAllReferences(); 3737 3738 public: 3739 DEFINE_MDNODE_GET(DIArgList, (ArrayRef<ValueAsMetadata *> Args), (Args)) 3740 3741 TempDIArgList clone() const { return cloneImpl(); } 3742 3743 ArrayRef<ValueAsMetadata *> getArgs() const { return Args; } 3744 3745 iterator args_begin() { return Args.begin(); } 3746 iterator args_end() { return Args.end(); } 3747 3748 static bool classof(const Metadata *MD) { 3749 return MD->getMetadataID() == DIArgListKind; 3750 } 3751 3752 void handleChangedOperand(void *Ref, Metadata *New); 3753 }; 3754 3755 /// Identifies a unique instance of a variable. 3756 /// 3757 /// Storage for identifying a potentially inlined instance of a variable, 3758 /// or a fragment thereof. This guarantees that exactly one variable instance 3759 /// may be identified by this class, even when that variable is a fragment of 3760 /// an aggregate variable and/or there is another inlined instance of the same 3761 /// source code variable nearby. 3762 /// This class does not necessarily uniquely identify that variable: it is 3763 /// possible that a DebugVariable with different parameters may point to the 3764 /// same variable instance, but not that one DebugVariable points to multiple 3765 /// variable instances. 3766 class DebugVariable { 3767 using FragmentInfo = DIExpression::FragmentInfo; 3768 3769 const DILocalVariable *Variable; 3770 std::optional<FragmentInfo> Fragment; 3771 const DILocation *InlinedAt; 3772 3773 /// Fragment that will overlap all other fragments. Used as default when 3774 /// caller demands a fragment. 3775 static const FragmentInfo DefaultFragment; 3776 3777 public: 3778 DebugVariable(const DbgVariableIntrinsic *DII); 3779 3780 DebugVariable(const DILocalVariable *Var, 3781 std::optional<FragmentInfo> FragmentInfo, 3782 const DILocation *InlinedAt) 3783 : Variable(Var), Fragment(FragmentInfo), InlinedAt(InlinedAt) {} 3784 3785 DebugVariable(const DILocalVariable *Var, const DIExpression *DIExpr, 3786 const DILocation *InlinedAt) 3787 : Variable(Var), 3788 Fragment(DIExpr ? DIExpr->getFragmentInfo() : std::nullopt), 3789 InlinedAt(InlinedAt) {} 3790 3791 const DILocalVariable *getVariable() const { return Variable; } 3792 std::optional<FragmentInfo> getFragment() const { return Fragment; } 3793 const DILocation *getInlinedAt() const { return InlinedAt; } 3794 3795 FragmentInfo getFragmentOrDefault() const { 3796 return Fragment.value_or(DefaultFragment); 3797 } 3798 3799 static bool isDefaultFragment(const FragmentInfo F) { 3800 return F == DefaultFragment; 3801 } 3802 3803 bool operator==(const DebugVariable &Other) const { 3804 return std::tie(Variable, Fragment, InlinedAt) == 3805 std::tie(Other.Variable, Other.Fragment, Other.InlinedAt); 3806 } 3807 3808 bool operator<(const DebugVariable &Other) const { 3809 return std::tie(Variable, Fragment, InlinedAt) < 3810 std::tie(Other.Variable, Other.Fragment, Other.InlinedAt); 3811 } 3812 }; 3813 3814 template <> struct DenseMapInfo<DebugVariable> { 3815 using FragmentInfo = DIExpression::FragmentInfo; 3816 3817 /// Empty key: no key should be generated that has no DILocalVariable. 3818 static inline DebugVariable getEmptyKey() { 3819 return DebugVariable(nullptr, std::nullopt, nullptr); 3820 } 3821 3822 /// Difference in tombstone is that the Optional is meaningful. 3823 static inline DebugVariable getTombstoneKey() { 3824 return DebugVariable(nullptr, {{0, 0}}, nullptr); 3825 } 3826 3827 static unsigned getHashValue(const DebugVariable &D) { 3828 unsigned HV = 0; 3829 const std::optional<FragmentInfo> Fragment = D.getFragment(); 3830 if (Fragment) 3831 HV = DenseMapInfo<FragmentInfo>::getHashValue(*Fragment); 3832 3833 return hash_combine(D.getVariable(), HV, D.getInlinedAt()); 3834 } 3835 3836 static bool isEqual(const DebugVariable &A, const DebugVariable &B) { 3837 return A == B; 3838 } 3839 }; 3840 3841 /// Identifies a unique instance of a whole variable (discards/ignores fragment 3842 /// information). 3843 class DebugVariableAggregate : public DebugVariable { 3844 public: 3845 DebugVariableAggregate(const DbgVariableIntrinsic *DVI); 3846 DebugVariableAggregate(const DebugVariable &V) 3847 : DebugVariable(V.getVariable(), std::nullopt, V.getInlinedAt()) {} 3848 }; 3849 3850 template <> 3851 struct DenseMapInfo<DebugVariableAggregate> 3852 : public DenseMapInfo<DebugVariable> {}; 3853 } // end namespace llvm 3854 3855 #undef DEFINE_MDNODE_GET_UNPACK_IMPL 3856 #undef DEFINE_MDNODE_GET_UNPACK 3857 #undef DEFINE_MDNODE_GET 3858 3859 #endif // LLVM_IR_DEBUGINFOMETADATA_H 3860