LLVM 17.0.0git
DebugInfoMetadata.h
Go to the documentation of this file.
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"
19#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/StringRef.h"
23#include "llvm/IR/Constants.h"
24#include "llvm/IR/Metadata.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
60namespace llvm {
61
62namespace dwarf {
63enum Tag : uint16_t;
64}
65
66class DbgVariableIntrinsic;
67
68extern cl::opt<bool> EnableFSDiscriminator;
69
71 const MDTuple *N = nullptr;
72
73public:
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
106 ++I;
107 return *this;
108 }
109
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.
130class DINode : public MDNode {
131 friend class LLVMContextImpl;
132 friend class MDNode;
133
134protected:
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);
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
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
162public:
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.
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.
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.
229class GenericDINode : public DINode {
230 friend class LLVMContextImpl;
231 friend class MDNode;
232
234 unsigned Tag, ArrayRef<Metadata *> Ops1,
236 : DINode(C, GenericDINodeKind, Storage, Tag, Ops1, Ops2) {
237 setHash(Hash);
238 }
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 {
257 SmallVector<Metadata *, 4>(dwarf_operands()));
258 }
259
260public:
261 unsigned getHash() const { return SubclassData32; }
262
264 (unsigned Tag, StringRef Header,
266 (Tag, Header, DwarfOps))
268 (unsigned Tag, MDString *Header,
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(); }
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).
302class DIAssignID : public MDNode {
303 friend class LLVMContextImpl;
304 friend class MDNode;
305
307 : MDNode(C, DIAssignIDKind, Storage, std::nullopt) {}
308
310
311 static DIAssignID *getImpl(LLVMContext &Context, StorageType Storage,
312 bool ShouldCreate = true);
313
314 TempDIAssignID cloneImpl() const { return getTemporary(getContext()); }
315
316public:
317 // This node has no operands to replace.
318 void replaceOperandWith(unsigned I, Metadata *New) = delete;
319
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.
337class DISubrange : public DINode {
338 friend class LLVMContextImpl;
339 friend class MDNode;
340
342
343 ~DISubrange() = default;
344
345 static DISubrange *getImpl(LLVMContext &Context, int64_t Count,
347 bool ShouldCreate = true);
348
351 bool ShouldCreate = true);
352
354 Metadata *LowerBound, Metadata *UpperBound,
356 bool ShouldCreate = true);
357
358 TempDISubrange cloneImpl() const {
359 return getTemporary(getContext(), getRawCountNode(), getRawLowerBound(),
360 getRawUpperBound(), getRawStride());
361 }
362
363public:
364 DEFINE_MDNODE_GET(DISubrange, (int64_t Count, int64_t LowerBound = 0),
365 (Count, LowerBound))
366
369
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
400class DIGenericSubrange : public DINode {
401 friend class LLVMContextImpl;
402 friend class MDNode;
403
406
407 ~DIGenericSubrange() = default;
408
409 static DIGenericSubrange *getImpl(LLVMContext &Context, Metadata *CountNode,
410 Metadata *LowerBound, Metadata *UpperBound,
412 bool ShouldCreate = true);
413
414 TempDIGenericSubrange cloneImpl() const {
417 }
418
419public:
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
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.
448class DIEnumerator : public DINode {
449 friend class LLVMContextImpl;
450 friend class MDNode;
451
452 APInt Value;
458 Ops) {}
459 ~DIEnumerator() = default;
460
461 static DIEnumerator *getImpl(LLVMContext &Context, const APInt &Value,
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 {
473 }
474
475public:
477 (int64_t Value, bool IsUnsigned, StringRef Name),
480 (int64_t Value, bool IsUnsigned, MDString *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.
508class DIScope : public DINode {
509protected:
512 : DINode(C, ID, Storage, Tag, Ops) {}
513 ~DIScope() = default;
514
515public:
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.
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.
562class DIFile : public DIScope {
563 friend class LLVMContextImpl;
564 friend class MDNode;
565
566public:
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.
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.
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.
586 /// The string value of the checksum.
588
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
598private:
599 std::optional<ChecksumInfo<MDString *>> Checksum;
600 /// An optional source. A nullptr means none.
601 MDString *Source;
602
604 std::optional<ChecksumInfo<MDString *>> CS, MDString *Src,
606 ~DIFile() = default;
607
608 static DIFile *getImpl(LLVMContext &Context, StringRef Filename,
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 {
629 getChecksum(), getSource());
630 }
631
632public:
635 std::optional<ChecksumInfo<StringRef>> CS = std::nullopt,
636 std::optional<StringRef> Source = std::nullopt),
637 (Filename, Directory, CS, Source))
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
675 if (auto *F = getFile())
676 return F->getFilename();
677 return "";
678}
679
681 if (auto *F = getFile())
682 return F->getDirectory();
683 return "";
684}
685
686std::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.
697class DIType : public DIScope {
698 unsigned Line;
699 DIFlags Flags;
700 uint64_t SizeInBits;
701 uint64_t OffsetInBits;
702 uint32_t AlignInBits;
703
704protected:
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
730public:
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; }
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.
803class DIBasicType : public DIType {
804 friend class LLVMContextImpl;
805 friend class MDNode;
806
807 unsigned Encoding;
808
810 uint64_t SizeInBits, uint32_t AlignInBits, unsigned Encoding,
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,
819 uint32_t AlignInBits, unsigned Encoding,
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,
830 bool ShouldCreate = true);
831
832 TempDIBasicType cloneImpl() const {
835 }
836
837public:
839 (Tag, Name, 0, 0, 0, FlagZero))
842 (Tag, Name, SizeInBits, 0, 0, FlagZero))
844 (unsigned Tag, MDString *Name, uint64_t SizeInBits),
845 (Tag, Name, SizeInBits, 0, 0, FlagZero))
848 uint32_t AlignInBits, unsigned Encoding, DIFlags Flags),
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)
871class DIStringType : public DIType {
872 friend class LLVMContextImpl;
873 friend class MDNode;
874
875 unsigned Encoding;
876
878 uint64_t SizeInBits, uint32_t AlignInBits, unsigned Encoding,
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,
887 Metadata *StrLenExp, Metadata *StrLocationExp,
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,
899 unsigned Encoding, StorageType Storage,
900 bool ShouldCreate = true);
901
902 TempDIStringType cloneImpl() const {
907 }
908
909public:
913 (Tag, Name, nullptr, nullptr, nullptr, SizeInBits,
914 AlignInBits, 0))
919 unsigned Encoding),
926 unsigned Encoding),
929
930 TempDIStringType clone() const { return cloneImpl(); }
931
932 static bool classof(const Metadata *MD) {
933 return MD->getMetadataID() == DIStringTypeKind;
934 }
935
937 return cast_or_null<DIVariable>(getRawStringLength());
938 }
939
941 return cast_or_null<DIExpression>(getRawStringLengthExp());
942 }
943
945 return cast_or_null<DIExpression>(getRawStringLocationExp());
946 }
947
948 unsigned getEncoding() const { return Encoding; }
949
950 Metadata *getRawStringLength() const { return getOperand(3); }
951
953
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.).
963class 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
974 std::optional<unsigned> DWARFAddressSpace, DIFlags Flags,
976 : DIType(C, DIDerivedTypeKind, Storage, Tag, Line, SizeInBits,
978 DWARFAddressSpace(DWARFAddressSpace) {}
979 ~DIDerivedType() = default;
980 static DIDerivedType *
981 getImpl(LLVMContext &Context, unsigned Tag, StringRef Name, DIFile *File,
984 std::optional<unsigned> DWARFAddressSpace, DIFlags Flags,
986 bool ShouldCreate = true) {
987 return getImpl(Context, Tag, getCanonicalMDString(Context, Name), File,
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,
996 std::optional<unsigned> DWARFAddressSpace, DIFlags Flags,
998 bool ShouldCreate = true);
999
1000 TempDIDerivedType cloneImpl() const {
1001 return getTemporary(
1004 getDWARFAddressSpace(), getFlags(), getExtraData(), getAnnotations());
1005 }
1006
1007public:
1010 (unsigned Tag, MDString *Name, Metadata *File, unsigned Line,
1013 std::optional<unsigned> DWARFAddressSpace, DIFlags Flags,
1014 Metadata *ExtraData = nullptr, Metadata *Annotations = nullptr),
1016 OffsetInBits, DWARFAddressSpace, Flags, ExtraData, Annotations))
1018 (unsigned Tag, StringRef Name, DIFile *File, unsigned Line,
1021 std::optional<unsigned> DWARFAddressSpace, DIFlags Flags,
1023 DINodeArray Annotations = nullptr),
1025 AlignInBits, OffsetInBits, DWARFAddressSpace, Flags,
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
1062 return dyn_cast_or_null<DIObjCProperty>(getExtraData());
1063 }
1064
1065 uint32_t getVBPtrOffset() const;
1066
1068
1069 Constant *getConstant() const;
1070
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.
1083class DICompositeType : public DIType {
1084 friend class LLVMContextImpl;
1085 friend class MDNode;
1086
1087 unsigned RuntimeLang;
1088
1090 unsigned Line, unsigned RuntimeLang, uint64_t SizeInBits,
1093 : DIType(C, DICompositeTypeKind, Storage, Tag, Line, SizeInBits,
1095 RuntimeLang(RuntimeLang) {}
1096 ~DICompositeType() = default;
1097
1098 /// Change fields in place.
1099 void mutate(unsigned Tag, unsigned Line, unsigned RuntimeLang,
1101 DIFlags Flags) {
1102 assert(isDistinct() && "Only distinct nodes can mutate");
1103 assert(getRawIdentifier() && "Only ODR-uniqued nodes should mutate");
1104 this->RuntimeLang = RuntimeLang;
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,
1112 DINodeArray Elements, unsigned RuntimeLang, DIType *VTableHolder,
1113 DITemplateParameterArray TemplateParams, StringRef Identifier,
1114 DIDerivedType *Discriminator, Metadata *DataLocation,
1116 DINodeArray Annotations, StorageType Storage,
1117 bool ShouldCreate = true) {
1118 return getImpl(
1119 Context, Tag, getCanonicalMDString(Context, Name), File, Line, Scope,
1121 RuntimeLang, VTableHolder, TemplateParams.get(),
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,
1129 DIFlags Flags, Metadata *Elements, unsigned RuntimeLang,
1133 Metadata *Annotations, StorageType Storage, bool ShouldCreate = true);
1134
1135 TempDICompositeType cloneImpl() const {
1136 return getTemporary(
1143 }
1144
1145public:
1148 (unsigned Tag, StringRef Name, DIFile *File, unsigned Line,
1151 DINodeArray Elements, unsigned RuntimeLang, DIType *VTableHolder,
1152 DITemplateParameterArray TemplateParams = nullptr,
1154 Metadata *DataLocation = nullptr, Metadata *Associated = nullptr,
1155 Metadata *Allocated = nullptr, Metadata *Rank = nullptr,
1156 DINodeArray Annotations = nullptr),
1160 Annotations))
1163 (unsigned Tag, MDString *Name, Metadata *File, unsigned Line,
1166 Metadata *Elements, unsigned RuntimeLang, Metadata *VTableHolder,
1169 Metadata *Associated = nullptr, Metadata *Allocated = nullptr,
1170 Metadata *Rank = nullptr, Metadata *Annotations = nullptr),
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,
1190 unsigned RuntimeLang, Metadata *VTableHolder,
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,
1211 unsigned RuntimeLang, Metadata *VTableHolder,
1215
1216 DIType *getBaseType() const { return cast_or_null<DIType>(getRawBaseType()); }
1217 DINodeArray getElements() const {
1218 return cast_or_null<MDTuple>(getRawElements());
1219 }
1221 return cast_or_null<DIType>(getRawVTableHolder());
1222 }
1223 DITemplateParameterArray getTemplateParams() const {
1224 return cast_or_null<MDTuple>(getRawTemplateParams());
1225 }
1227 unsigned getRuntimeLang() const { return RuntimeLang; }
1228
1229 Metadata *getRawBaseType() const { return getOperand(3); }
1230 Metadata *getRawElements() const { return getOperand(4); }
1233 MDString *getRawIdentifier() const { return getOperandAs<MDString>(7); }
1236 return getOperandAs<DIDerivedType>(8);
1237 }
1240 return dyn_cast_or_null<DIVariable>(getRawDataLocation());
1241 }
1243 return dyn_cast_or_null<DIExpression>(getRawDataLocation());
1244 }
1245 Metadata *getRawAssociated() const { return getOperand(10); }
1247 return dyn_cast_or_null<DIVariable>(getRawAssociated());
1248 }
1250 return dyn_cast_or_null<DIExpression>(getRawAssociated());
1251 }
1252 Metadata *getRawAllocated() const { return getOperand(11); }
1254 return dyn_cast_or_null<DIVariable>(getRawAllocated());
1255 }
1257 return dyn_cast_or_null<DIExpression>(getRawAllocated());
1258 }
1259 Metadata *getRawRank() const { return getOperand(12); }
1261 if (auto *MD = dyn_cast_or_null<ConstantAsMetadata>(getRawRank()))
1262 return dyn_cast_or_null<ConstantInt>(MD->getValue());
1263 return nullptr;
1264 }
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
1291 }
1292
1293 void replaceTemplateParams(DITemplateParameterArray TemplateParams) {
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.
1306class 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
1315 uint8_t CC, ArrayRef<Metadata *> Ops);
1316 ~DISubroutineType() = default;
1317
1319 uint8_t CC, DITypeRefArray TypeArray,
1321 bool ShouldCreate = true) {
1322 return getImpl(Context, Flags, CC, TypeArray.get(), Storage, ShouldCreate);
1323 }
1325 uint8_t CC, Metadata *TypeArray,
1327 bool ShouldCreate = true);
1328
1329 TempDISubroutineType cloneImpl() const {
1331 }
1332
1333public:
1336 (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
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.
1363class DICompileUnit : public DIScope {
1364 friend class LLVMContextImpl;
1365 friend class MDNode;
1366
1367public:
1368 enum DebugEmissionKind : unsigned {
1375
1376 enum class DebugNameTableKind : unsigned {
1377 Default = 0,
1378 GNU = 1,
1379 None = 2,
1381 };
1382
1383 static std::optional<DebugEmissionKind> getEmissionKind(StringRef Str);
1384 static const char *emissionKindString(DebugEmissionKind EK);
1385 static std::optional<DebugNameTableKind> getNameTableKind(StringRef Str);
1386 static const char *nameTableKindString(DebugNameTableKind PK);
1387
1388private:
1389 unsigned SourceLanguage;
1390 bool IsOptimized;
1391 unsigned RuntimeVersion;
1392 unsigned EmissionKind;
1393 uint64_t DWOId;
1394 bool SplitDebugInlining;
1395 bool DebugInfoForProfiling;
1396 unsigned NameTableKind;
1397 bool RangesBaseAddress;
1398
1400 bool IsOptimized, unsigned RuntimeVersion,
1401 unsigned EmissionKind, uint64_t DWOId, bool SplitDebugInlining,
1402 bool DebugInfoForProfiling, unsigned NameTableKind,
1403 bool RangesBaseAddress, ArrayRef<Metadata *> Ops);
1404 ~DICompileUnit() = default;
1405
1406 static DICompileUnit *
1407 getImpl(LLVMContext &Context, unsigned SourceLanguage, DIFile *File,
1408 StringRef Producer, bool IsOptimized, StringRef Flags,
1409 unsigned RuntimeVersion, StringRef SplitDebugFilename,
1410 unsigned EmissionKind, DICompositeTypeArray EnumTypes,
1411 DIScopeArray RetainedTypes,
1412 DIGlobalVariableExpressionArray GlobalVariables,
1413 DIImportedEntityArray ImportedEntities, DIMacroNodeArray Macros,
1414 uint64_t DWOId, bool SplitDebugInlining, bool DebugInfoForProfiling,
1415 unsigned NameTableKind, bool RangesBaseAddress, StringRef SysRoot,
1416 StringRef SDK, StorageType Storage, bool ShouldCreate = true) {
1417 return getImpl(
1418 Context, SourceLanguage, File, getCanonicalMDString(Context, Producer),
1419 IsOptimized, getCanonicalMDString(Context, Flags), RuntimeVersion,
1420 getCanonicalMDString(Context, SplitDebugFilename), EmissionKind,
1421 EnumTypes.get(), RetainedTypes.get(), GlobalVariables.get(),
1422 ImportedEntities.get(), Macros.get(), DWOId, SplitDebugInlining,
1423 DebugInfoForProfiling, NameTableKind, RangesBaseAddress,
1424 getCanonicalMDString(Context, SysRoot),
1425 getCanonicalMDString(Context, SDK), Storage, ShouldCreate);
1426 }
1427 static DICompileUnit *
1428 getImpl(LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
1429 MDString *Producer, bool IsOptimized, MDString *Flags,
1430 unsigned RuntimeVersion, MDString *SplitDebugFilename,
1431 unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
1433 Metadata *Macros, uint64_t DWOId, bool SplitDebugInlining,
1434 bool DebugInfoForProfiling, unsigned NameTableKind,
1435 bool RangesBaseAddress, MDString *SysRoot, MDString *SDK,
1436 StorageType Storage, bool ShouldCreate = true);
1437
1438 TempDICompileUnit cloneImpl() const {
1439 return getTemporary(
1446 }
1447
1448public:
1449 static void get() = delete;
1450 static void getIfExists() = delete;
1451
1455 bool IsOptimized, StringRef Flags, unsigned RuntimeVersion,
1457 DICompositeTypeArray EnumTypes, DIScopeArray RetainedTypes,
1458 DIGlobalVariableExpressionArray GlobalVariables,
1459 DIImportedEntityArray ImportedEntities, DIMacroNodeArray Macros,
1460 uint64_t DWOId, bool SplitDebugInlining, bool DebugInfoForProfiling,
1461 DebugNameTableKind NameTableKind, bool RangesBaseAddress,
1463 (SourceLanguage, File, Producer, IsOptimized, Flags, RuntimeVersion,
1465 GlobalVariables, ImportedEntities, Macros, DWOId, SplitDebugInlining,
1466 DebugInfoForProfiling, (unsigned)NameTableKind, RangesBaseAddress,
1467 SysRoot, SDK))
1471 bool IsOptimized, MDString *Flags, unsigned RuntimeVersion,
1475 bool SplitDebugInlining, bool DebugInfoForProfiling,
1476 unsigned NameTableKind, bool RangesBaseAddress, MDString *SysRoot,
1478 (SourceLanguage, File, Producer, IsOptimized, Flags, RuntimeVersion,
1480 GlobalVariables, ImportedEntities, Macros, DWOId, SplitDebugInlining,
1481 DebugInfoForProfiling, NameTableKind, RangesBaseAddress, SysRoot, SDK))
1482
1483 TempDICompileUnit clone() const { return cloneImpl(); }
1484
1485 unsigned getSourceLanguage() const { return SourceLanguage; }
1486 bool isOptimized() const { return IsOptimized; }
1487 unsigned getRuntimeVersion() const { return RuntimeVersion; }
1489 return (DebugEmissionKind)EmissionKind;
1490 }
1492 return EmissionKind == DebugDirectivesOnly;
1493 }
1494 bool getDebugInfoForProfiling() const { return DebugInfoForProfiling; }
1496 return (DebugNameTableKind)NameTableKind;
1497 }
1498 bool getRangesBaseAddress() const { return RangesBaseAddress; }
1500 StringRef getFlags() const { return getStringOperand(2); }
1502 DICompositeTypeArray getEnumTypes() const {
1503 return cast_or_null<MDTuple>(getRawEnumTypes());
1504 }
1505 DIScopeArray getRetainedTypes() const {
1506 return cast_or_null<MDTuple>(getRawRetainedTypes());
1507 }
1508 DIGlobalVariableExpressionArray getGlobalVariables() const {
1509 return cast_or_null<MDTuple>(getRawGlobalVariables());
1510 }
1511 DIImportedEntityArray getImportedEntities() const {
1512 return cast_or_null<MDTuple>(getRawImportedEntities());
1513 }
1514 DIMacroNodeArray getMacros() const {
1515 return cast_or_null<MDTuple>(getRawMacros());
1516 }
1517 uint64_t getDWOId() const { return DWOId; }
1518 void setDWOId(uint64_t DwoId) { DWOId = DwoId; }
1519 bool getSplitDebugInlining() const { return SplitDebugInlining; }
1520 void setSplitDebugInlining(bool SplitDebugInlining) {
1521 this->SplitDebugInlining = SplitDebugInlining;
1522 }
1524 StringRef getSDK() const { return getStringOperand(10); }
1525
1526 MDString *getRawProducer() const { return getOperandAs<MDString>(1); }
1527 MDString *getRawFlags() const { return getOperandAs<MDString>(2); }
1529 return getOperandAs<MDString>(3);
1530 }
1531 Metadata *getRawEnumTypes() const { return getOperand(4); }
1535 Metadata *getRawMacros() const { return getOperand(8); }
1536 MDString *getRawSysRoot() const { return getOperandAs<MDString>(9); }
1537 MDString *getRawSDK() const { return getOperandAs<MDString>(10); }
1538
1539 /// Replace arrays.
1540 ///
1541 /// If this \a isUniqued() and not \a isResolved(), it will be RAUW'ed and
1542 /// deleted on a uniquing collision. In practice, uniquing collisions on \a
1543 /// DICompileUnit should be fairly rare.
1544 /// @{
1545 void replaceEnumTypes(DICompositeTypeArray N) {
1546 replaceOperandWith(4, N.get());
1547 }
1548 void replaceRetainedTypes(DITypeArray N) { replaceOperandWith(5, N.get()); }
1549 void replaceGlobalVariables(DIGlobalVariableExpressionArray N) {
1550 replaceOperandWith(6, N.get());
1551 }
1552 void replaceImportedEntities(DIImportedEntityArray N) {
1553 replaceOperandWith(7, N.get());
1554 }
1555 void replaceMacros(DIMacroNodeArray N) { replaceOperandWith(8, N.get()); }
1556 /// @}
1557
1558 static bool classof(const Metadata *MD) {
1559 return MD->getMetadataID() == DICompileUnitKind;
1560 }
1561};
1562
1563/// A scope for locals.
1564///
1565/// A legal scope for lexical blocks, local variables, and debug info
1566/// locations. Subclasses are \a DISubprogram, \a DILexicalBlock, and \a
1567/// DILexicalBlockFile.
1568class DILocalScope : public DIScope {
1569protected:
1572 : DIScope(C, ID, Storage, Tag, Ops) {}
1573 ~DILocalScope() = default;
1574
1575public:
1576 /// Get the subprogram for this scope.
1577 ///
1578 /// Return this if it's an \a DISubprogram; otherwise, look up the scope
1579 /// chain.
1580 DISubprogram *getSubprogram() const;
1581
1582 /// Traverses the scope chain rooted at RootScope until it hits a Subprogram,
1583 /// recreating the chain with "NewSP" instead.
1584 static DILocalScope *
1586 LLVMContext &Ctx,
1588
1589 /// Get the first non DILexicalBlockFile scope of this scope.
1590 ///
1591 /// Return this if it's not a \a DILexicalBlockFIle; otherwise, look up the
1592 /// scope chain.
1594
1595 static bool classof(const Metadata *MD) {
1596 return MD->getMetadataID() == DISubprogramKind ||
1597 MD->getMetadataID() == DILexicalBlockKind ||
1598 MD->getMetadataID() == DILexicalBlockFileKind;
1599 }
1600};
1601
1602/// Subprogram description.
1604 friend class LLVMContextImpl;
1605 friend class MDNode;
1606
1607 unsigned Line;
1608 unsigned ScopeLine;
1609 unsigned VirtualIndex;
1610
1611 /// In the MS ABI, the implicit 'this' parameter is adjusted in the prologue
1612 /// of method overrides from secondary bases by this amount. It may be
1613 /// negative.
1614 int ThisAdjustment;
1615
1616public:
1617 /// Debug info subprogram flags.
1619#define HANDLE_DISP_FLAG(ID, NAME) SPFlag##NAME = ID,
1620#define DISP_FLAG_LARGEST_NEEDED
1621#include "llvm/IR/DebugInfoFlags.def"
1622 SPFlagNonvirtual = SPFlagZero,
1623 SPFlagVirtuality = SPFlagVirtual | SPFlagPureVirtual,
1624 LLVM_MARK_AS_BITMASK_ENUM(SPFlagLargest)
1625 };
1626
1627 static DISPFlags getFlag(StringRef Flag);
1628 static StringRef getFlagString(DISPFlags Flag);
1629
1630 /// Split up a flags bitfield for easier printing.
1631 ///
1632 /// Split \c Flags into \c SplitFlags, a vector of its components. Returns
1633 /// any remaining (unrecognized) bits.
1635 SmallVectorImpl<DISPFlags> &SplitFlags);
1636
1637 // Helper for converting old bitfields to new flags word.
1638 static DISPFlags toSPFlags(bool IsLocalToUnit, bool IsDefinition,
1639 bool IsOptimized,
1640 unsigned Virtuality = SPFlagNonvirtual,
1641 bool IsMainSubprogram = false);
1642
1643private:
1644 DIFlags Flags;
1645 DISPFlags SPFlags;
1646
1647 DISubprogram(LLVMContext &C, StorageType Storage, unsigned Line,
1648 unsigned ScopeLine, unsigned VirtualIndex, int ThisAdjustment,
1650 ~DISubprogram() = default;
1651
1652 static DISubprogram *
1654 StringRef LinkageName, DIFile *File, unsigned Line,
1655 DISubroutineType *Type, unsigned ScopeLine, DIType *ContainingType,
1656 unsigned VirtualIndex, int ThisAdjustment, DIFlags Flags,
1657 DISPFlags SPFlags, DICompileUnit *Unit,
1658 DITemplateParameterArray TemplateParams, DISubprogram *Declaration,
1659 DINodeArray RetainedNodes, DITypeArray ThrownTypes,
1661 StorageType Storage, bool ShouldCreate = true) {
1662 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1663 getCanonicalMDString(Context, LinkageName), File, Line, Type,
1664 ScopeLine, ContainingType, VirtualIndex, ThisAdjustment,
1665 Flags, SPFlags, Unit, TemplateParams.get(), Declaration,
1666 RetainedNodes.get(), ThrownTypes.get(), Annotations.get(),
1668 Storage, ShouldCreate);
1669 }
1670 static DISubprogram *
1671 getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
1672 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
1673 unsigned ScopeLine, Metadata *ContainingType, unsigned VirtualIndex,
1674 int ThisAdjustment, DIFlags Flags, DISPFlags SPFlags, Metadata *Unit,
1678 bool ShouldCreate = true);
1679
1680 TempDISubprogram cloneImpl() const {
1682 getFile(), getLine(), getType(), getScopeLine(),
1683 getContainingType(), getVirtualIndex(),
1684 getThisAdjustment(), getFlags(), getSPFlags(),
1685 getUnit(), getTemplateParams(), getDeclaration(),
1686 getRetainedNodes(), getThrownTypes(), getAnnotations(),
1687 getTargetFuncName());
1688 }
1689
1690public:
1694 unsigned Line, DISubroutineType *Type, unsigned ScopeLine,
1695 DIType *ContainingType, unsigned VirtualIndex, int ThisAdjustment,
1697 DITemplateParameterArray TemplateParams = nullptr,
1698 DISubprogram *Declaration = nullptr, DINodeArray RetainedNodes = nullptr,
1699 DITypeArray ThrownTypes = nullptr, DINodeArray Annotations = nullptr,
1701 (Scope, Name, LinkageName, File, Line, Type, ScopeLine, ContainingType,
1702 VirtualIndex, ThisAdjustment, Flags, SPFlags, Unit, TemplateParams,
1704
1708 unsigned Line, Metadata *Type, unsigned ScopeLine,
1709 Metadata *ContainingType, unsigned VirtualIndex, int ThisAdjustment,
1714 (Scope, Name, LinkageName, File, Line, Type, ScopeLine, ContainingType,
1715 VirtualIndex, ThisAdjustment, Flags, SPFlags, Unit, TemplateParams,
1717
1718 TempDISubprogram clone() const { return cloneImpl(); }
1719
1720 /// Returns a new temporary DISubprogram with updated Flags
1721 TempDISubprogram cloneWithFlags(DIFlags NewFlags) const {
1722 auto NewSP = clone();
1723 NewSP->Flags = NewFlags;
1724 return NewSP;
1725 }
1726
1727public:
1728 unsigned getLine() const { return Line; }
1729 unsigned getVirtuality() const { return getSPFlags() & SPFlagVirtuality; }
1730 unsigned getVirtualIndex() const { return VirtualIndex; }
1731 int getThisAdjustment() const { return ThisAdjustment; }
1732 unsigned getScopeLine() const { return ScopeLine; }
1733 void setScopeLine(unsigned L) {
1734 assert(isDistinct());
1735 ScopeLine = L;
1736 }
1737 DIFlags getFlags() const { return Flags; }
1738 DISPFlags getSPFlags() const { return SPFlags; }
1739 bool isLocalToUnit() const { return getSPFlags() & SPFlagLocalToUnit; }
1740 bool isDefinition() const { return getSPFlags() & SPFlagDefinition; }
1741 bool isOptimized() const { return getSPFlags() & SPFlagOptimized; }
1742 bool isMainSubprogram() const { return getSPFlags() & SPFlagMainSubprogram; }
1743
1744 bool isArtificial() const { return getFlags() & FlagArtificial; }
1745 bool isPrivate() const {
1746 return (getFlags() & FlagAccessibility) == FlagPrivate;
1747 }
1748 bool isProtected() const {
1749 return (getFlags() & FlagAccessibility) == FlagProtected;
1750 }
1751 bool isPublic() const {
1752 return (getFlags() & FlagAccessibility) == FlagPublic;
1753 }
1754 bool isExplicit() const { return getFlags() & FlagExplicit; }
1755 bool isPrototyped() const { return getFlags() & FlagPrototyped; }
1756 bool areAllCallsDescribed() const {
1757 return getFlags() & FlagAllCallsDescribed;
1758 }
1759 bool isPure() const { return getSPFlags() & SPFlagPure; }
1760 bool isElemental() const { return getSPFlags() & SPFlagElemental; }
1761 bool isRecursive() const { return getSPFlags() & SPFlagRecursive; }
1762 bool isObjCDirect() const { return getSPFlags() & SPFlagObjCDirect; }
1763
1764 /// Check if this is deleted member function.
1765 ///
1766 /// Return true if this subprogram is a C++11 special
1767 /// member function declared deleted.
1768 bool isDeleted() const { return getSPFlags() & SPFlagDeleted; }
1769
1770 /// Check if this is reference-qualified.
1771 ///
1772 /// Return true if this subprogram is a C++11 reference-qualified non-static
1773 /// member function (void foo() &).
1774 bool isLValueReference() const { return getFlags() & FlagLValueReference; }
1775
1776 /// Check if this is rvalue-reference-qualified.
1777 ///
1778 /// Return true if this subprogram is a C++11 rvalue-reference-qualified
1779 /// non-static member function (void foo() &&).
1780 bool isRValueReference() const { return getFlags() & FlagRValueReference; }
1781
1782 /// Check if this is marked as noreturn.
1783 ///
1784 /// Return true if this subprogram is C++11 noreturn or C11 _Noreturn
1785 bool isNoReturn() const { return getFlags() & FlagNoReturn; }
1786
1787 // Check if this routine is a compiler-generated thunk.
1788 //
1789 // Returns true if this subprogram is a thunk generated by the compiler.
1790 bool isThunk() const { return getFlags() & FlagThunk; }
1791
1792 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
1793
1794 StringRef getName() const { return getStringOperand(2); }
1795 StringRef getLinkageName() const { return getStringOperand(3); }
1796 /// Only used by clients of CloneFunction, and only right after the cloning.
1797 void replaceLinkageName(MDString *LN) { replaceOperandWith(3, LN); }
1798
1799 DISubroutineType *getType() const {
1800 return cast_or_null<DISubroutineType>(getRawType());
1801 }
1802 DIType *getContainingType() const {
1803 return cast_or_null<DIType>(getRawContainingType());
1804 }
1805 void replaceType(DISubroutineType *Ty) {
1806 assert(isDistinct() && "Only distinct nodes can mutate");
1807 replaceOperandWith(4, Ty);
1808 }
1809
1810 DICompileUnit *getUnit() const {
1811 return cast_or_null<DICompileUnit>(getRawUnit());
1812 }
1813 void replaceUnit(DICompileUnit *CU) { replaceOperandWith(5, CU); }
1814 DITemplateParameterArray getTemplateParams() const {
1815 return cast_or_null<MDTuple>(getRawTemplateParams());
1816 }
1817 DISubprogram *getDeclaration() const {
1818 return cast_or_null<DISubprogram>(getRawDeclaration());
1819 }
1820 DINodeArray getRetainedNodes() const {
1821 return cast_or_null<MDTuple>(getRawRetainedNodes());
1822 }
1823 DITypeArray getThrownTypes() const {
1824 return cast_or_null<MDTuple>(getRawThrownTypes());
1825 }
1826 DINodeArray getAnnotations() const {
1827 return cast_or_null<MDTuple>(getRawAnnotations());
1828 }
1829 StringRef getTargetFuncName() const {
1830 return (getRawTargetFuncName()) ? getStringOperand(12) : StringRef();
1831 }
1832
1833 Metadata *getRawScope() const { return getOperand(1); }
1834 MDString *getRawName() const { return getOperandAs<MDString>(2); }
1835 MDString *getRawLinkageName() const { return getOperandAs<MDString>(3); }
1836 Metadata *getRawType() const { return getOperand(4); }
1837 Metadata *getRawUnit() const { return getOperand(5); }
1838 Metadata *getRawDeclaration() const { return getOperand(6); }
1839 Metadata *getRawRetainedNodes() const { return getOperand(7); }
1840 Metadata *getRawContainingType() const {
1841 return getNumOperands() > 8 ? getOperandAs<Metadata>(8) : nullptr;
1842 }
1843 Metadata *getRawTemplateParams() const {
1844 return getNumOperands() > 9 ? getOperandAs<Metadata>(9) : nullptr;
1845 }
1846 Metadata *getRawThrownTypes() const {
1847 return getNumOperands() > 10 ? getOperandAs<Metadata>(10) : nullptr;
1848 }
1849 Metadata *getRawAnnotations() const {
1850 return getNumOperands() > 11 ? getOperandAs<Metadata>(11) : nullptr;
1851 }
1852 MDString *getRawTargetFuncName() const {
1853 return getNumOperands() > 12 ? getOperandAs<MDString>(12) : nullptr;
1854 }
1855
1856 void replaceRawLinkageName(MDString *LinkageName) {
1858 }
1859
1860 /// Check if this subprogram describes the given function.
1861 ///
1862 /// FIXME: Should this be looking through bitcasts?
1863 bool describes(const Function *F) const;
1864
1865 static bool classof(const Metadata *MD) {
1866 return MD->getMetadataID() == DISubprogramKind;
1867 }
1868};
1869
1870/// Debug location.
1871///
1872/// A debug location in source code, used for debug info and otherwise.
1873class DILocation : public MDNode {
1874 friend class LLVMContextImpl;
1875 friend class MDNode;
1876
1878 unsigned Column, ArrayRef<Metadata *> MDs, bool ImplicitCode);
1880
1881 static DILocation *getImpl(LLVMContext &Context, unsigned Line,
1882 unsigned Column, Metadata *Scope,
1884 StorageType Storage, bool ShouldCreate = true);
1885 static DILocation *getImpl(LLVMContext &Context, unsigned Line,
1886 unsigned Column, DILocalScope *Scope,
1888 StorageType Storage, bool ShouldCreate = true) {
1889 return getImpl(Context, Line, Column, static_cast<Metadata *>(Scope),
1890 static_cast<Metadata *>(InlinedAt), ImplicitCode, Storage,
1891 ShouldCreate);
1892 }
1893
1894 TempDILocation cloneImpl() const {
1895 // Get the raw scope/inlinedAt since it is possible to invoke this on
1896 // a DILocation containing temporary metadata.
1897 return getTemporary(getContext(), getLine(), getColumn(), getRawScope(),
1898 getRawInlinedAt(), isImplicitCode());
1899 }
1900
1901public:
1902 // Disallow replacing operands.
1903 void replaceOperandWith(unsigned I, Metadata *New) = delete;
1904
1906 (unsigned Line, unsigned Column, Metadata *Scope,
1907 Metadata *InlinedAt = nullptr, bool ImplicitCode = false),
1910 (unsigned Line, unsigned Column, DILocalScope *Scope,
1912 bool ImplicitCode = false),
1914
1915 /// Return a (temporary) clone of this.
1916 TempDILocation clone() const { return cloneImpl(); }
1917
1918 unsigned getLine() const { return SubclassData32; }
1919 unsigned getColumn() const { return SubclassData16; }
1920 DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); }
1921
1922 /// Return the linkage name of Subprogram. If the linkage name is empty,
1923 /// return scope name (the demangled name).
1924 StringRef getSubprogramLinkageName() const {
1925 DISubprogram *SP = getScope()->getSubprogram();
1926 if (!SP)
1927 return "";
1928 auto Name = SP->getLinkageName();
1929 if (!Name.empty())
1930 return Name;
1931 return SP->getName();
1932 }
1933
1934 DILocation *getInlinedAt() const {
1935 return cast_or_null<DILocation>(getRawInlinedAt());
1936 }
1937
1938 /// Check if the location corresponds to an implicit code.
1939 /// When the ImplicitCode flag is true, it means that the Instruction
1940 /// with this DILocation has been added by the front-end but it hasn't been
1941 /// written explicitly by the user (e.g. cleanup stuff in C++ put on a closing
1942 /// bracket). It's useful for code coverage to not show a counter on "empty"
1943 /// lines.
1944 bool isImplicitCode() const { return SubclassData1; }
1945 void setImplicitCode(bool ImplicitCode) { SubclassData1 = ImplicitCode; }
1946
1947 DIFile *getFile() const { return getScope()->getFile(); }
1948 StringRef getFilename() const { return getScope()->getFilename(); }
1949 StringRef getDirectory() const { return getScope()->getDirectory(); }
1950 std::optional<StringRef> getSource() const { return getScope()->getSource(); }
1951
1952 /// Get the scope where this is inlined.
1953 ///
1954 /// Walk through \a getInlinedAt() and return \a getScope() from the deepest
1955 /// location.
1956 DILocalScope *getInlinedAtScope() const {
1957 if (auto *IA = getInlinedAt())
1958 return IA->getInlinedAtScope();
1959 return getScope();
1960 }
1961
1962 /// Get the DWARF discriminator.
1963 ///
1964 /// DWARF discriminators distinguish identical file locations between
1965 /// instructions that are on different basic blocks.
1966 ///
1967 /// There are 3 components stored in discriminator, from lower bits:
1968 ///
1969 /// Base discriminator: assigned by AddDiscriminators pass to identify IRs
1970 /// that are defined by the same source line, but
1971 /// different basic blocks.
1972 /// Duplication factor: assigned by optimizations that will scale down
1973 /// the execution frequency of the original IR.
1974 /// Copy Identifier: assigned by optimizations that clones the IR.
1975 /// Each copy of the IR will be assigned an identifier.
1976 ///
1977 /// Encoding:
1978 ///
1979 /// The above 3 components are encoded into a 32bit unsigned integer in
1980 /// order. If the lowest bit is 1, the current component is empty, and the
1981 /// next component will start in the next bit. Otherwise, the current
1982 /// component is non-empty, and its content starts in the next bit. The
1983 /// value of each components is either 5 bit or 12 bit: if the 7th bit
1984 /// is 0, the bit 2~6 (5 bits) are used to represent the component; if the
1985 /// 7th bit is 1, the bit 2~6 (5 bits) and 8~14 (7 bits) are combined to
1986 /// represent the component. Thus, the number of bits used for a component
1987 /// is either 0 (if it and all the next components are empty); 1 - if it is
1988 /// empty; 7 - if its value is up to and including 0x1f (lsb and msb are both
1989 /// 0); or 14, if its value is up to and including 0x1ff. Note that the last
1990 /// component is also capped at 0x1ff, even in the case when both first
1991 /// components are 0, and we'd technically have 29 bits available.
1992 ///
1993 /// For precise control over the data being encoded in the discriminator,
1994 /// use encodeDiscriminator/decodeDiscriminator.
1995
1996 inline unsigned getDiscriminator() const;
1997
1998 // For the regular discriminator, it stands for all empty components if all
1999 // the lowest 3 bits are non-zero and all higher 29 bits are unused(zero by
2000 // default). Here we fully leverage the higher 29 bits for pseudo probe use.
2001 // This is the format:
2002 // [2:0] - 0x7
2003 // [31:3] - pseudo probe fields guaranteed to be non-zero as a whole
2004 // So if the lower 3 bits is non-zero and the others has at least one
2005 // non-zero bit, it guarantees to be a pseudo probe discriminator
2006 inline static bool isPseudoProbeDiscriminator(unsigned Discriminator) {
2007 return ((Discriminator & 0x7) == 0x7) && (Discriminator & 0xFFFFFFF8);
2008 }
2009
2010 /// Returns a new DILocation with updated \p Discriminator.
2011 inline const DILocation *cloneWithDiscriminator(unsigned Discriminator) const;
2012
2013 /// Returns a new DILocation with updated base discriminator \p BD. Only the
2014 /// base discriminator is set in the new DILocation, the other encoded values
2015 /// are elided.
2016 /// If the discriminator cannot be encoded, the function returns std::nullopt.
2017 inline std::optional<const DILocation *>
2018 cloneWithBaseDiscriminator(unsigned BD) const;
2019
2020 /// Returns the duplication factor stored in the discriminator, or 1 if no
2021 /// duplication factor (or 0) is encoded.
2022 inline unsigned getDuplicationFactor() const;
2023
2024 /// Returns the copy identifier stored in the discriminator.
2025 inline unsigned getCopyIdentifier() const;
2026
2027 /// Returns the base discriminator stored in the discriminator.
2028 inline unsigned getBaseDiscriminator() const;
2029
2030 /// Returns a new DILocation with duplication factor \p DF * current
2031 /// duplication factor encoded in the discriminator. The current duplication
2032 /// factor is as defined by getDuplicationFactor().
2033 /// Returns std::nullopt if encoding failed.
2034 inline std::optional<const DILocation *>
2036
2037 /// When two instructions are combined into a single instruction we also
2038 /// need to combine the original locations into a single location.
2039 /// When the locations are the same we can use either location.
2040 /// When they differ, we need a third location which is distinct from either.
2041 /// If they share a common scope, use this scope and compare the line/column
2042 /// pair of the locations with the common scope:
2043 /// * if both match, keep the line and column;
2044 /// * if only the line number matches, keep the line and set the column as 0;
2045 /// * otherwise set line and column as 0.
2046 /// If they do not share a common scope the location is ambiguous and can't be
2047 /// represented in a line entry. In this case, set line and column as 0 and
2048 /// use the scope of any location.
2049 ///
2050 /// \p LocA \p LocB: The locations to be merged.
2051 static DILocation *getMergedLocation(DILocation *LocA, DILocation *LocB);
2052
2053 /// Try to combine the vector of locations passed as input in a single one.
2054 /// This function applies getMergedLocation() repeatedly left-to-right.
2055 ///
2056 /// \p Locs: The locations to be merged.
2058
2059 /// Return the masked discriminator value for an input discrimnator value D
2060 /// (i.e. zero out the (B+1)-th and above bits for D (B is 0-base).
2061 // Example: an input of (0x1FF, 7) returns 0xFF.
2062 static unsigned getMaskedDiscriminator(unsigned D, unsigned B) {
2063 return (D & getN1Bits(B));
2064 }
2065
2066 /// Return the bits used for base discriminators.
2067 static unsigned getBaseDiscriminatorBits() { return getBaseFSBitEnd(); }
2068
2069 /// Returns the base discriminator for a given encoded discriminator \p D.
2070 static unsigned
2072 bool IsFSDiscriminator = false) {
2073 if (IsFSDiscriminator)
2076 }
2077
2078 /// Raw encoding of the discriminator. APIs such as cloneWithDuplicationFactor
2079 /// have certain special case behavior (e.g. treating empty duplication factor
2080 /// as the value '1').
2081 /// This API, in conjunction with cloneWithDiscriminator, may be used to
2082 /// encode the raw values provided.
2083 ///
2084 /// \p BD: base discriminator
2085 /// \p DF: duplication factor
2086 /// \p CI: copy index
2087 ///
2088 /// The return is std::nullopt if the values cannot be encoded in 32 bits -
2089 /// for example, values for BD or DF larger than 12 bits. Otherwise, the
2090 /// return is the encoded value.
2091 static std::optional<unsigned> encodeDiscriminator(unsigned BD, unsigned DF,
2092 unsigned CI);
2093
2094 /// Raw decoder for values in an encoded discriminator D.
2095 static void decodeDiscriminator(unsigned D, unsigned &BD, unsigned &DF,
2096 unsigned &CI);
2097
2098 /// Returns the duplication factor for a given encoded discriminator \p D, or
2099 /// 1 if no value or 0 is encoded.
2100 static unsigned getDuplicationFactorFromDiscriminator(unsigned D) {
2102 return 1;
2104 unsigned Ret = getUnsignedFromPrefixEncoding(D);
2105 if (Ret == 0)
2106 return 1;
2107 return Ret;
2108 }
2109
2110 /// Returns the copy identifier for a given encoded discriminator \p D.
2111 static unsigned getCopyIdentifierFromDiscriminator(unsigned D) {
2114 }
2115
2116 Metadata *getRawScope() const { return getOperand(0); }
2118 if (getNumOperands() == 2)
2119 return getOperand(1);
2120 return nullptr;
2121 }
2122
2123 static bool classof(const Metadata *MD) {
2124 return MD->getMetadataID() == DILocationKind;
2125 }
2126};
2127
2129protected:
2133
2134public:
2135 DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); }
2136
2137 Metadata *getRawScope() const { return getOperand(1); }
2138
2139 void replaceScope(DIScope *Scope) {
2140 assert(!isUniqued());
2141 setOperand(1, Scope);
2142 }
2143
2144 static bool classof(const Metadata *MD) {
2145 return MD->getMetadataID() == DILexicalBlockKind ||
2146 MD->getMetadataID() == DILexicalBlockFileKind;
2147 }
2148};
2149
2151 friend class LLVMContextImpl;
2152 friend class MDNode;
2153
2154 unsigned Line;
2155 uint16_t Column;
2156
2158 unsigned Column, ArrayRef<Metadata *> Ops)
2159 : DILexicalBlockBase(C, DILexicalBlockKind, Storage, Ops), Line(Line),
2160 Column(Column) {
2161 assert(Column < (1u << 16) && "Expected 16-bit column");
2162 }
2163 ~DILexicalBlock() = default;
2164
2166 DIFile *File, unsigned Line, unsigned Column,
2168 bool ShouldCreate = true) {
2169 return getImpl(Context, static_cast<Metadata *>(Scope),
2170 static_cast<Metadata *>(File), Line, Column, Storage,
2171 ShouldCreate);
2172 }
2173
2174 static DILexicalBlock *getImpl(LLVMContext &Context, Metadata *Scope,
2175 Metadata *File, unsigned Line, unsigned Column,
2176 StorageType Storage, bool ShouldCreate = true);
2177
2178 TempDILexicalBlock cloneImpl() const {
2180 getColumn());
2181 }
2182
2183public:
2185 (DILocalScope * Scope, DIFile *File, unsigned Line,
2186 unsigned Column),
2187 (Scope, File, Line, Column))
2189 (Metadata * Scope, Metadata *File, unsigned Line,
2190 unsigned Column),
2191 (Scope, File, Line, Column))
2192
2193 TempDILexicalBlock clone() const { return cloneImpl(); }
2194
2195 unsigned getLine() const { return Line; }
2196 unsigned getColumn() const { return Column; }
2197
2198 static bool classof(const Metadata *MD) {
2199 return MD->getMetadataID() == DILexicalBlockKind;
2200 }
2201};
2202
2204 friend class LLVMContextImpl;
2205 friend class MDNode;
2206
2207 unsigned Discriminator;
2208
2210 unsigned Discriminator, ArrayRef<Metadata *> Ops)
2211 : DILexicalBlockBase(C, DILexicalBlockFileKind, Storage, Ops),
2212 Discriminator(Discriminator) {}
2213 ~DILexicalBlockFile() = default;
2214
2216 DIFile *File, unsigned Discriminator,
2218 bool ShouldCreate = true) {
2219 return getImpl(Context, static_cast<Metadata *>(Scope),
2220 static_cast<Metadata *>(File), Discriminator, Storage,
2221 ShouldCreate);
2222 }
2223
2224 static DILexicalBlockFile *getImpl(LLVMContext &Context, Metadata *Scope,
2225 Metadata *File, unsigned Discriminator,
2227 bool ShouldCreate = true);
2228
2229 TempDILexicalBlockFile cloneImpl() const {
2230 return getTemporary(getContext(), getScope(), getFile(),
2232 }
2233
2234public:
2237 unsigned Discriminator),
2238 (Scope, File, Discriminator))
2240 (Metadata * Scope, Metadata *File, unsigned Discriminator),
2241 (Scope, File, Discriminator))
2242
2243 TempDILexicalBlockFile clone() const { return cloneImpl(); }
2244 unsigned getDiscriminator() const { return Discriminator; }
2245
2246 static bool classof(const Metadata *MD) {
2247 return MD->getMetadataID() == DILexicalBlockFileKind;
2248 }
2249};
2250
2251unsigned DILocation::getDiscriminator() const {
2252 if (auto *F = dyn_cast<DILexicalBlockFile>(getScope()))
2253 return F->getDiscriminator();
2254 return 0;
2255}
2256
2257const DILocation *
2258DILocation::cloneWithDiscriminator(unsigned Discriminator) const {
2259 DIScope *Scope = getScope();
2260 // Skip all parent DILexicalBlockFile that already have a discriminator
2261 // assigned. We do not want to have nested DILexicalBlockFiles that have
2262 // mutliple discriminators because only the leaf DILexicalBlockFile's
2263 // dominator will be used.
2264 for (auto *LBF = dyn_cast<DILexicalBlockFile>(Scope);
2265 LBF && LBF->getDiscriminator() != 0;
2266 LBF = dyn_cast<DILexicalBlockFile>(Scope))
2267 Scope = LBF->getScope();
2268 DILexicalBlockFile *NewScope =
2269 DILexicalBlockFile::get(getContext(), Scope, getFile(), Discriminator);
2270 return DILocation::get(getContext(), getLine(), getColumn(), NewScope,
2271 getInlinedAt());
2272}
2273
2275 return getBaseDiscriminatorFromDiscriminator(getDiscriminator(),
2277}
2278
2280 return getDuplicationFactorFromDiscriminator(getDiscriminator());
2281}
2282
2284 return getCopyIdentifierFromDiscriminator(getDiscriminator());
2285}
2286
2287std::optional<const DILocation *>
2289 unsigned BD, DF, CI;
2290
2292 BD = getBaseDiscriminator();
2293 if (D == BD)
2294 return this;
2295 return cloneWithDiscriminator(D);
2296 }
2297
2298 decodeDiscriminator(getDiscriminator(), BD, DF, CI);
2299 if (D == BD)
2300 return this;
2301 if (std::optional<unsigned> Encoded = encodeDiscriminator(D, DF, CI))
2302 return cloneWithDiscriminator(*Encoded);
2303 return std::nullopt;
2304}
2305
2306std::optional<const DILocation *>
2308 assert(!EnableFSDiscriminator && "FSDiscriminator should not call this.");
2309 // Do no interfere with pseudo probes. Pseudo probe doesn't need duplication
2310 // factor support as samples collected on cloned probes will be aggregated.
2311 // Also pseudo probe at a callsite uses the dwarf discriminator to store
2312 // pseudo probe related information, such as the probe id.
2313 if (isPseudoProbeDiscriminator(getDiscriminator()))
2314 return this;
2315
2317 if (DF <= 1)
2318 return this;
2319
2320 unsigned BD = getBaseDiscriminator();
2321 unsigned CI = getCopyIdentifier();
2322 if (std::optional<unsigned> D = encodeDiscriminator(BD, DF, CI))
2323 return cloneWithDiscriminator(*D);
2324 return std::nullopt;
2325}
2326
2327class DINamespace : public DIScope {
2328 friend class LLVMContextImpl;
2329 friend class MDNode;
2330
2331 unsigned ExportSymbols : 1;
2332
2333 DINamespace(LLVMContext &Context, StorageType Storage, bool ExportSymbols,
2335 ~DINamespace() = default;
2336
2337 static DINamespace *getImpl(LLVMContext &Context, DIScope *Scope,
2338 StringRef Name, bool ExportSymbols,
2339 StorageType Storage, bool ShouldCreate = true) {
2340 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
2341 ExportSymbols, Storage, ShouldCreate);
2342 }
2343 static DINamespace *getImpl(LLVMContext &Context, Metadata *Scope,
2344 MDString *Name, bool ExportSymbols,
2345 StorageType Storage, bool ShouldCreate = true);
2346
2347 TempDINamespace cloneImpl() const {
2348 return getTemporary(getContext(), getScope(), getName(),
2350 }
2351
2352public:
2354 (DIScope * Scope, StringRef Name, bool ExportSymbols),
2355 (Scope, Name, ExportSymbols))
2357 (Metadata * Scope, MDString *Name, bool ExportSymbols),
2358 (Scope, Name, ExportSymbols))
2359
2360 TempDINamespace clone() const { return cloneImpl(); }
2361
2362 bool getExportSymbols() const { return ExportSymbols; }
2363 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
2364 StringRef getName() const { return getStringOperand(2); }
2365
2366 Metadata *getRawScope() const { return getOperand(1); }
2367 MDString *getRawName() const { return getOperandAs<MDString>(2); }
2368
2369 static bool classof(const Metadata *MD) {
2370 return MD->getMetadataID() == DINamespaceKind;
2371 }
2372};
2373
2374/// Represents a module in the programming language, for example, a Clang
2375/// module, or a Fortran module.
2376class DIModule : public DIScope {
2377 friend class LLVMContextImpl;
2378 friend class MDNode;
2379 unsigned LineNo;
2380 bool IsDecl;
2381
2382 DIModule(LLVMContext &Context, StorageType Storage, unsigned LineNo,
2383 bool IsDecl, ArrayRef<Metadata *> Ops);
2384 ~DIModule() = default;
2385
2386 static DIModule *getImpl(LLVMContext &Context, DIFile *File, DIScope *Scope,
2389 unsigned LineNo, bool IsDecl, StorageType Storage,
2390 bool ShouldCreate = true) {
2391 return getImpl(Context, File, Scope, getCanonicalMDString(Context, Name),
2394 getCanonicalMDString(Context, APINotesFile), LineNo, IsDecl,
2395 Storage, ShouldCreate);
2396 }
2397 static DIModule *getImpl(LLVMContext &Context, Metadata *File,
2400 MDString *APINotesFile, unsigned LineNo, bool IsDecl,
2401 StorageType Storage, bool ShouldCreate = true);
2402
2403 TempDIModule cloneImpl() const {
2405 getConfigurationMacros(), getIncludePath(),
2406 getAPINotesFile(), getLineNo(), getIsDecl());
2407 }
2408
2409public:
2413 StringRef APINotesFile, unsigned LineNo,
2414 bool IsDecl = false),
2416 APINotesFile, LineNo, IsDecl))
2420 MDString *APINotesFile, unsigned LineNo,
2421 bool IsDecl = false),
2423 APINotesFile, LineNo, IsDecl))
2424
2425 TempDIModule clone() const { return cloneImpl(); }
2426
2427 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
2428 StringRef getName() const { return getStringOperand(2); }
2429 StringRef getConfigurationMacros() const { return getStringOperand(3); }
2430 StringRef getIncludePath() const { return getStringOperand(4); }
2431 StringRef getAPINotesFile() const { return getStringOperand(5); }
2432 unsigned getLineNo() const { return LineNo; }
2433 bool getIsDecl() const { return IsDecl; }
2434
2435 Metadata *getRawScope() const { return getOperand(1); }
2436 MDString *getRawName() const { return getOperandAs<MDString>(2); }
2437 MDString *getRawConfigurationMacros() const {
2438 return getOperandAs<MDString>(3);
2439 }
2440 MDString *getRawIncludePath() const { return getOperandAs<MDString>(4); }
2441 MDString *getRawAPINotesFile() const { return getOperandAs<MDString>(5); }
2442
2443 static bool classof(const Metadata *MD) {
2444 return MD->getMetadataID() == DIModuleKind;
2445 }
2446};
2447
2448/// Base class for template parameters.
2450protected:
2452
2454 unsigned Tag, bool IsDefault, ArrayRef<Metadata *> Ops)
2457
2458public:
2459 StringRef getName() const { return getStringOperand(0); }
2460 DIType *getType() const { return cast_or_null<DIType>(getRawType()); }
2461
2462 MDString *getRawName() const { return getOperandAs<MDString>(0); }
2463 Metadata *getRawType() const { return getOperand(1); }
2464 bool isDefault() const { return IsDefault; }
2465
2466 static bool classof(const Metadata *MD) {
2467 return MD->getMetadataID() == DITemplateTypeParameterKind ||
2468 MD->getMetadataID() == DITemplateValueParameterKind;
2469 }
2470};
2471
2473 friend class LLVMContextImpl;
2474 friend class MDNode;
2475
2478 ~DITemplateTypeParameter() = default;
2479
2481 DIType *Type, bool IsDefault,
2483 bool ShouldCreate = true) {
2484 return getImpl(Context, getCanonicalMDString(Context, Name), Type,
2485 IsDefault, Storage, ShouldCreate);
2486 }
2488 Metadata *Type, bool IsDefault,
2490 bool ShouldCreate = true);
2491
2492 TempDITemplateTypeParameter cloneImpl() const {
2493 return getTemporary(getContext(), getName(), getType(), isDefault());
2494 }
2495
2496public:
2499 (Name, Type, IsDefault))
2503
2504 TempDITemplateTypeParameter clone() const { return cloneImpl(); }
2505
2506 static bool classof(const Metadata *MD) {
2507 return MD->getMetadataID() == DITemplateTypeParameterKind;
2508 }
2509};
2510
2512 friend class LLVMContextImpl;
2513 friend class MDNode;
2514
2516 unsigned Tag, bool IsDefault,
2518 : DITemplateParameter(Context, DITemplateValueParameterKind, Storage, Tag,
2519 IsDefault, Ops) {}
2520 ~DITemplateValueParameter() = default;
2521
2522 static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
2524 bool IsDefault, Metadata *Value,
2526 bool ShouldCreate = true) {
2527 return getImpl(Context, Tag, getCanonicalMDString(Context, Name), Type,
2528 IsDefault, Value, Storage, ShouldCreate);
2529 }
2530 static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
2531 MDString *Name, Metadata *Type,
2532 bool IsDefault, Metadata *Value,
2534 bool ShouldCreate = true);
2535
2536 TempDITemplateValueParameter cloneImpl() const {
2537 return getTemporary(getContext(), getTag(), getName(), getType(),
2538 isDefault(), getValue());
2539 }
2540
2541public:
2543 (unsigned Tag, StringRef Name, DIType *Type, bool IsDefault,
2544 Metadata *Value),
2545 (Tag, Name, Type, IsDefault, Value))
2550
2551 TempDITemplateValueParameter clone() const { return cloneImpl(); }
2552
2553 Metadata *getValue() const { return getOperand(2); }
2554
2555 static bool classof(const Metadata *MD) {
2556 return MD->getMetadataID() == DITemplateValueParameterKind;
2557 }
2558};
2559
2560/// Base class for variables.
2561class DIVariable : public DINode {
2562 unsigned Line;
2563 uint32_t AlignInBits;
2564
2565protected:
2566 DIVariable(LLVMContext &C, unsigned ID, StorageType Storage, signed Line,
2567 ArrayRef<Metadata *> Ops, uint32_t AlignInBits = 0);
2568 ~DIVariable() = default;
2569
2570public:
2571 unsigned getLine() const { return Line; }
2572 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
2573 StringRef getName() const { return getStringOperand(1); }
2574 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
2575 DIType *getType() const { return cast_or_null<DIType>(getRawType()); }
2576 uint32_t getAlignInBits() const { return AlignInBits; }
2577 uint32_t getAlignInBytes() const { return getAlignInBits() / CHAR_BIT; }
2578 /// Determines the size of the variable's type.
2579 std::optional<uint64_t> getSizeInBits() const;
2580
2581 /// Return the signedness of this variable's type, or std::nullopt if this
2582 /// type is neither signed nor unsigned.
2583 std::optional<DIBasicType::Signedness> getSignedness() const {
2584 if (auto *BT = dyn_cast<DIBasicType>(getType()))
2585 return BT->getSignedness();
2586 return std::nullopt;
2587 }
2588
2590 if (auto *F = getFile())
2591 return F->getFilename();
2592 return "";
2593 }
2594
2596 if (auto *F = getFile())
2597 return F->getDirectory();
2598 return "";
2599 }
2600
2601 std::optional<StringRef> getSource() const {
2602 if (auto *F = getFile())
2603 return F->getSource();
2604 return std::nullopt;
2605 }
2606
2607 Metadata *getRawScope() const { return getOperand(0); }
2608 MDString *getRawName() const { return getOperandAs<MDString>(1); }
2609 Metadata *getRawFile() const { return getOperand(2); }
2610 Metadata *getRawType() const { return getOperand(3); }
2611
2612 static bool classof(const Metadata *MD) {
2613 return MD->getMetadataID() == DILocalVariableKind ||
2614 MD->getMetadataID() == DIGlobalVariableKind;
2615 }
2616};
2617
2618/// DWARF expression.
2619///
2620/// This is (almost) a DWARF expression that modifies the location of a
2621/// variable, or the location of a single piece of a variable, or (when using
2622/// DW_OP_stack_value) is the constant variable value.
2623///
2624/// TODO: Co-allocate the expression elements.
2625/// TODO: Separate from MDNode, or otherwise drop Distinct and Temporary
2626/// storage types.
2627class DIExpression : public MDNode {
2628 friend class LLVMContextImpl;
2629 friend class MDNode;
2630
2631 std::vector<uint64_t> Elements;
2632
2634 : MDNode(C, DIExpressionKind, Storage, std::nullopt),
2635 Elements(Elements.begin(), Elements.end()) {}
2636 ~DIExpression() = default;
2637
2638 static DIExpression *getImpl(LLVMContext &Context,
2640 bool ShouldCreate = true);
2641
2642 TempDIExpression cloneImpl() const {
2643 return getTemporary(getContext(), getElements());
2644 }
2645
2646public:
2648
2649 TempDIExpression clone() const { return cloneImpl(); }
2650
2651 ArrayRef<uint64_t> getElements() const { return Elements; }
2652
2653 unsigned getNumElements() const { return Elements.size(); }
2654
2655 uint64_t getElement(unsigned I) const {
2656 assert(I < Elements.size() && "Index out of range");
2657 return Elements[I];
2658 }
2659
2661 /// Determine whether this represents a constant value, if so
2662 // return it's sign information.
2663 std::optional<SignedOrUnsignedConstant> isConstant() const;
2664
2665 /// Return the number of unique location operands referred to (via
2666 /// DW_OP_LLVM_arg) in this expression; this is not necessarily the number of
2667 /// instances of DW_OP_LLVM_arg within the expression.
2668 /// For example, for the expression:
2669 /// (DW_OP_LLVM_arg 0, DW_OP_LLVM_arg 1, DW_OP_plus,
2670 /// DW_OP_LLVM_arg 0, DW_OP_mul)
2671 /// This function would return 2, as there are two unique location operands
2672 /// (0 and 1).
2674
2676
2679
2680 /// A lightweight wrapper around an expression operand.
2681 ///
2682 /// TODO: Store arguments directly and change \a DIExpression to store a
2683 /// range of these.
2685 const uint64_t *Op = nullptr;
2686
2687 public:
2688 ExprOperand() = default;
2689 explicit ExprOperand(const uint64_t *Op) : Op(Op) {}
2690
2691 const uint64_t *get() const { return Op; }
2692
2693 /// Get the operand code.
2694 uint64_t getOp() const { return *Op; }
2695
2696 /// Get an argument to the operand.
2697 ///
2698 /// Never returns the operand itself.
2699 uint64_t getArg(unsigned I) const { return Op[I + 1]; }
2700
2701 unsigned getNumArgs() const { return getSize() - 1; }
2702
2703 /// Return the size of the operand.
2704 ///
2705 /// Return the number of elements in the operand (1 + args).
2706 unsigned getSize() const;
2707
2708 /// Append the elements of this operand to \p V.
2710 V.append(get(), get() + getSize());
2711 }
2712 };
2713
2714 /// An iterator for expression operands.
2716 ExprOperand Op;
2717
2718 public:
2719 using iterator_category = std::input_iterator_tag;
2721 using difference_type = std::ptrdiff_t;
2724
2725 expr_op_iterator() = default;
2727
2728 element_iterator getBase() const { return Op.get(); }
2729 const ExprOperand &operator*() const { return Op; }
2730 const ExprOperand *operator->() const { return &Op; }
2731
2733 increment();
2734 return *this;
2735 }
2737 expr_op_iterator T(*this);
2738 increment();
2739 return T;
2740 }
2741
2742 /// Get the next iterator.
2743 ///
2744 /// \a std::next() doesn't work because this is technically an
2745 /// input_iterator, but it's a perfectly valid operation. This is an
2746 /// accessor to provide the same functionality.
2747 expr_op_iterator getNext() const { return ++expr_op_iterator(*this); }
2748
2749 bool operator==(const expr_op_iterator &X) const {
2750 return getBase() == X.getBase();
2751 }
2752 bool operator!=(const expr_op_iterator &X) const {
2753 return getBase() != X.getBase();
2754 }
2755
2756 private:
2757 void increment() { Op = ExprOperand(getBase() + Op.getSize()); }
2758 };
2759
2760 /// Visit the elements via ExprOperand wrappers.
2761 ///
2762 /// These range iterators visit elements through \a ExprOperand wrappers.
2763 /// This is not guaranteed to be a valid range unless \a isValid() gives \c
2764 /// true.
2765 ///
2766 /// \pre \a isValid() gives \c true.
2767 /// @{
2770 }
2773 }
2775 return {expr_op_begin(), expr_op_end()};
2776 }
2777 /// @}
2778
2779 bool isValid() const;
2780
2781 static bool classof(const Metadata *MD) {
2782 return MD->getMetadataID() == DIExpressionKind;
2783 }
2784
2785 /// Return whether the first element a DW_OP_deref.
2786 bool startsWithDeref() const;
2787
2788 /// Return whether there is exactly one operator and it is a DW_OP_deref;
2789 bool isDeref() const;
2790
2791 /// Holds the characteristics of one fragment of a larger variable.
2793 FragmentInfo() = default;
2798 /// Return the index of the first bit of the fragment.
2800 /// Return the index of the bit after the end of the fragment, e.g. for
2801 /// fragment offset=16 and size=32 return their sum, 48.
2803
2804 /// Returns a zero-sized fragment if A and B don't intersect.
2807 uint64_t StartInBits = std::max(A.OffsetInBits, B.OffsetInBits);
2808 uint64_t EndInBits = std::min(A.endInBits(), B.endInBits());
2809 if (EndInBits <= StartInBits)
2810 return {0, 0};
2811 return DIExpression::FragmentInfo(EndInBits - StartInBits, StartInBits);
2812 }
2813 };
2814
2815 /// Retrieve the details of this fragment expression.
2816 static std::optional<FragmentInfo> getFragmentInfo(expr_op_iterator Start,
2817 expr_op_iterator End);
2818
2819 /// Retrieve the details of this fragment expression.
2820 std::optional<FragmentInfo> getFragmentInfo() const {
2822 }
2823
2824 /// Return whether this is a piece of an aggregate variable.
2825 bool isFragment() const { return getFragmentInfo().has_value(); }
2826
2827 /// Return whether this is an implicit location description.
2828 bool isImplicit() const;
2829
2830 /// Return whether the location is computed on the expression stack, meaning
2831 /// it cannot be a simple register location.
2832 bool isComplex() const;
2833
2834 /// Return whether the evaluated expression makes use of a single location at
2835 /// the start of the expression, i.e. if it contains only a single
2836 /// DW_OP_LLVM_arg op as its first operand, or if it contains none.
2837 bool isSingleLocationExpression() const;
2838
2839 /// Removes all elements from \p Expr that do not apply to an undef debug
2840 /// value, which includes every operator that computes the value/location on
2841 /// the DWARF stack, including any DW_OP_LLVM_arg elements (making the result
2842 /// of this function always a single-location expression) while leaving
2843 /// everything that defines what the computed value applies to, i.e. the
2844 /// fragment information.
2845 static const DIExpression *convertToUndefExpression(const DIExpression *Expr);
2846
2847 /// If \p Expr is a non-variadic expression (i.e. one that does not contain
2848 /// DW_OP_LLVM_arg), returns \p Expr converted to variadic form by adding a
2849 /// leading [DW_OP_LLVM_arg, 0] to the expression; otherwise returns \p Expr.
2850 static const DIExpression *
2852
2853 /// If \p Expr is a valid single-location expression, i.e. it refers to only a
2854 /// single debug operand at the start of the expression, then return that
2855 /// expression in a non-variadic form by removing DW_OP_LLVM_arg from the
2856 /// expression if it is present; otherwise returns std::nullopt.
2857 static std::optional<const DIExpression *>
2859
2860 /// Inserts the elements of \p Expr into \p Ops modified to a canonical form,
2861 /// which uses DW_OP_LLVM_arg (i.e. is a variadic expression) and folds the
2862 /// implied derefence from the \p IsIndirect flag into the expression. This
2863 /// allows us to check equivalence between expressions with differing
2864 /// directness or variadicness.
2866 const DIExpression *Expr,
2867 bool IsIndirect);
2868
2869 /// Determines whether two debug values should produce equivalent DWARF
2870 /// expressions, using their DIExpressions and directness, ignoring the
2871 /// differences between otherwise identical expressions in variadic and
2872 /// non-variadic form and not considering the debug operands.
2873 /// \p FirstExpr is the DIExpression for the first debug value.
2874 /// \p FirstIndirect should be true if the first debug value is indirect; in
2875 /// IR this should be true for dbg.declare intrinsics and false for
2876 /// dbg.values, and in MIR this should be true only for DBG_VALUE instructions
2877 /// whose second operand is an immediate value.
2878 /// \p SecondExpr and \p SecondIndirect have the same meaning as the prior
2879 /// arguments, but apply to the second debug value.
2880 static bool isEqualExpression(const DIExpression *FirstExpr,
2881 bool FirstIndirect,
2882 const DIExpression *SecondExpr,
2883 bool SecondIndirect);
2884
2885 /// Append \p Ops with operations to apply the \p Offset.
2886 static void appendOffset(SmallVectorImpl<uint64_t> &Ops, int64_t Offset);
2887
2888 /// If this is a constant offset, extract it. If there is no expression,
2889 /// return true with an offset of zero.
2890 bool extractIfOffset(int64_t &Offset) const;
2891
2892 /// Returns true iff this DIExpression contains at least one instance of
2893 /// `DW_OP_LLVM_arg, n` for all n in [0, N).
2894 bool hasAllLocationOps(unsigned N) const;
2895
2896 /// Checks if the last 4 elements of the expression are DW_OP_constu <DWARF
2897 /// Address Space> DW_OP_swap DW_OP_xderef and extracts the <DWARF Address
2898 /// Space>.
2899 static const DIExpression *extractAddressClass(const DIExpression *Expr,
2900 unsigned &AddrClass);
2901
2902 /// Used for DIExpression::prepend.
2903 enum PrependOps : uint8_t {
2905 DerefBefore = 1 << 0,
2906 DerefAfter = 1 << 1,
2907 StackValue = 1 << 2,
2908 EntryValue = 1 << 3
2910
2911 /// Prepend \p DIExpr with a deref and offset operation and optionally turn it
2912 /// into a stack value or/and an entry value.
2913 static DIExpression *prepend(const DIExpression *Expr, uint8_t Flags,
2914 int64_t Offset = 0);
2915
2916 /// Prepend \p DIExpr with the given opcodes and optionally turn it into a
2917 /// stack value.
2918 static DIExpression *prependOpcodes(const DIExpression *Expr,
2920 bool StackValue = false,
2921 bool EntryValue = false);
2922
2923 /// Append the opcodes \p Ops to \p DIExpr. Unlike \ref appendToStack, the
2924 /// returned expression is a stack value only if \p DIExpr is a stack value.
2925 /// If \p DIExpr describes a fragment, the returned expression will describe
2926 /// the same fragment.
2927 static DIExpression *append(const DIExpression *Expr, ArrayRef<uint64_t> Ops);
2928
2929 /// Convert \p DIExpr into a stack value if it isn't one already by appending
2930 /// DW_OP_deref if needed, and appending \p Ops to the resulting expression.
2931 /// If \p DIExpr describes a fragment, the returned expression will describe
2932 /// the same fragment.
2933 static DIExpression *appendToStack(const DIExpression *Expr,
2934 ArrayRef<uint64_t> Ops);
2935
2936 /// Create a copy of \p Expr by appending the given list of \p Ops to each
2937 /// instance of the operand `DW_OP_LLVM_arg, \p ArgNo`. This is used to
2938 /// modify a specific location used by \p Expr, such as when salvaging that
2939 /// location.
2940 static DIExpression *appendOpsToArg(const DIExpression *Expr,
2941 ArrayRef<uint64_t> Ops, unsigned ArgNo,
2942 bool StackValue = false);
2943
2944 /// Create a copy of \p Expr with each instance of
2945 /// `DW_OP_LLVM_arg, \p OldArg` replaced with `DW_OP_LLVM_arg, \p NewArg`,
2946 /// and each instance of `DW_OP_LLVM_arg, Arg` with `DW_OP_LLVM_arg, Arg - 1`
2947 /// for all Arg > \p OldArg.
2948 /// This is used when replacing one of the operands of a debug value list
2949 /// with another operand in the same list and deleting the old operand.
2950 static DIExpression *replaceArg(const DIExpression *Expr, uint64_t OldArg,
2951 uint64_t NewArg);
2952
2953 /// Create a DIExpression to describe one part of an aggregate variable that
2954 /// is fragmented across multiple Values. The DW_OP_LLVM_fragment operation
2955 /// will be appended to the elements of \c Expr. If \c Expr already contains
2956 /// a \c DW_OP_LLVM_fragment \c OffsetInBits is interpreted as an offset
2957 /// into the existing fragment.
2958 ///
2959 /// \param OffsetInBits Offset of the piece in bits.
2960 /// \param SizeInBits Size of the piece in bits.
2961 /// \return Creating a fragment expression may fail if \c Expr
2962 /// contains arithmetic operations that would be
2963 /// truncated.
2964 static std::optional<DIExpression *>
2965 createFragmentExpression(const DIExpression *Expr, unsigned OffsetInBits,
2966 unsigned SizeInBits);
2967
2968 /// Determine the relative position of the fragments passed in.
2969 /// Returns -1 if this is entirely before Other, 0 if this and Other overlap,
2970 /// 1 if this is entirely after Other.
2971 static int fragmentCmp(const FragmentInfo &A, const FragmentInfo &B) {
2972 uint64_t l1 = A.OffsetInBits;
2973 uint64_t l2 = B.OffsetInBits;
2974 uint64_t r1 = l1 + A.SizeInBits;
2975 uint64_t r2 = l2 + B.SizeInBits;
2976 if (r1 <= l2)
2977 return -1;
2978 else if (r2 <= l1)
2979 return 1;
2980 else
2981 return 0;
2982 }
2983
2984 using ExtOps = std::array<uint64_t, 6>;
2985
2986 /// Returns the ops for a zero- or sign-extension in a DIExpression.
2987 static ExtOps getExtOps(unsigned FromSize, unsigned ToSize, bool Signed);
2988
2989 /// Append a zero- or sign-extension to \p Expr. Converts the expression to a
2990 /// stack value if it isn't one already.
2991 static DIExpression *appendExt(const DIExpression *Expr, unsigned FromSize,
2992 unsigned ToSize, bool Signed);
2993
2994 /// Check if fragments overlap between a pair of FragmentInfos.
2995 static bool fragmentsOverlap(const FragmentInfo &A, const FragmentInfo &B) {
2996 return fragmentCmp(A, B) == 0;
2997 }
2998
2999 /// Determine the relative position of the fragments described by this
3000 /// DIExpression and \p Other. Calls static fragmentCmp implementation.
3001 int fragmentCmp(const DIExpression *Other) const {
3002 auto Fragment1 = *getFragmentInfo();
3003 auto Fragment2 = *Other->getFragmentInfo();
3004 return fragmentCmp(Fragment1, Fragment2);
3005 }
3006
3007 /// Check if fragments overlap between this DIExpression and \p Other.
3009 if (!isFragment() || !Other->isFragment())
3010 return true;
3011 return fragmentCmp(Other) == 0;
3012 }
3013
3014 /// Check if the expression consists of exactly one entry value operand.
3015 /// (This is the only configuration of entry values that is supported.)
3016 bool isEntryValue() const;
3017
3018 /// Try to shorten an expression with an initial constant operand.
3019 /// Returns a new expression and constant on success, or the original
3020 /// expression and constant on failure.
3021 std::pair<DIExpression *, const ConstantInt *>
3022 constantFold(const ConstantInt *CI);
3023};
3024
3027 return std::tie(A.SizeInBits, A.OffsetInBits) ==
3028 std::tie(B.SizeInBits, B.OffsetInBits);
3029}
3030
3033 return std::tie(A.SizeInBits, A.OffsetInBits) <
3034 std::tie(B.SizeInBits, B.OffsetInBits);
3035}
3036
3037template <> struct DenseMapInfo<DIExpression::FragmentInfo> {
3039 static const uint64_t MaxVal = std::numeric_limits<uint64_t>::max();
3040
3041 static inline FragInfo getEmptyKey() { return {MaxVal, MaxVal}; }
3042
3043 static inline FragInfo getTombstoneKey() { return {MaxVal - 1, MaxVal - 1}; }
3044
3045 static unsigned getHashValue(const FragInfo &Frag) {
3046 return (Frag.SizeInBits & 0xffff) << 16 | (Frag.OffsetInBits & 0xffff);
3047 }
3048
3049 static bool isEqual(const FragInfo &A, const FragInfo &B) { return A == B; }
3050};
3051
3052/// Global variables.
3053///
3054/// TODO: Remove DisplayName. It's always equal to Name.
3056 friend class LLVMContextImpl;
3057 friend class MDNode;
3058
3059 bool IsLocalToUnit;
3060 bool IsDefinition;
3061
3063 bool IsLocalToUnit, bool IsDefinition, uint32_t AlignInBits,
3065 : DIVariable(C, DIGlobalVariableKind, Storage, Line, Ops, AlignInBits),
3066 IsLocalToUnit(IsLocalToUnit), IsDefinition(IsDefinition) {}
3067 ~DIGlobalVariable() = default;
3068
3069 static DIGlobalVariable *
3072 bool IsLocalToUnit, bool IsDefinition,
3075 bool ShouldCreate = true) {
3076 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
3078 IsLocalToUnit, IsDefinition, StaticDataMemberDeclaration,
3079 cast_or_null<Metadata>(TemplateParams), AlignInBits,
3080 Annotations.get(), Storage, ShouldCreate);
3081 }
3082 static DIGlobalVariable *
3083 getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
3084 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
3085 bool IsLocalToUnit, bool IsDefinition,
3088 bool ShouldCreate = true);
3089
3090 TempDIGlobalVariable cloneImpl() const {
3095 getAnnotations());
3096 }
3097
3098public:
3102 unsigned Line, DIType *Type, bool IsLocalToUnit, bool IsDefinition,
3104 uint32_t AlignInBits, DINodeArray Annotations),
3105 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, IsDefinition,
3110 unsigned Line, Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
3113 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, IsDefinition,
3115
3116 TempDIGlobalVariable clone() const { return cloneImpl(); }
3117
3118 bool isLocalToUnit() const { return IsLocalToUnit; }
3119 bool isDefinition() const { return IsDefinition; }
3123 return cast_or_null<DIDerivedType>(getRawStaticDataMemberDeclaration());
3124 }
3125 DINodeArray getAnnotations() const {
3126 return cast_or_null<MDTuple>(getRawAnnotations());
3127 }
3128
3129 MDString *getRawLinkageName() const { return getOperandAs<MDString>(5); }
3132 MDTuple *getTemplateParams() const { return getOperandAs<MDTuple>(7); }
3133 Metadata *getRawAnnotations() const { return getOperand(8); }
3134
3135 static bool classof(const Metadata *MD) {
3136 return MD->getMetadataID() == DIGlobalVariableKind;
3137 }
3138};
3139
3140class DICommonBlock : public DIScope {
3141 unsigned LineNo;
3142
3143 friend class LLVMContextImpl;
3144 friend class MDNode;
3145
3146 DICommonBlock(LLVMContext &Context, StorageType Storage, unsigned LineNo,
3148
3149 static DICommonBlock *getImpl(LLVMContext &Context, DIScope *Scope,
3151 DIFile *File, unsigned LineNo,
3152 StorageType Storage, bool ShouldCreate = true) {
3153 return getImpl(Context, Scope, Decl, getCanonicalMDString(Context, Name),
3154 File, LineNo, Storage, ShouldCreate);
3155 }
3156 static DICommonBlock *getImpl(LLVMContext &Context, Metadata *Scope,
3158 unsigned LineNo, StorageType Storage,
3159 bool ShouldCreate = true);
3160
3161 TempDICommonBlock cloneImpl() const {
3163 getFile(), getLineNo());
3164 }
3165
3166public:
3169 DIFile *File, unsigned LineNo),
3170 (Scope, Decl, Name, File, LineNo))
3173 Metadata *File, unsigned LineNo),
3174 (Scope, Decl, Name, File, LineNo))
3175
3176 TempDICommonBlock clone() const { return cloneImpl(); }
3177
3178 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
3180 return cast_or_null<DIGlobalVariable>(getRawDecl());
3181 }
3182 StringRef getName() const { return getStringOperand(2); }
3183 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
3184 unsigned getLineNo() const { return LineNo; }
3185
3186 Metadata *getRawScope() const { return getOperand(0); }
3187 Metadata *getRawDecl() const { return getOperand(1); }
3188 MDString *getRawName() const { return getOperandAs<MDString>(2); }
3189 Metadata *getRawFile() const { return getOperand(3); }
3190
3191 static bool classof(const Metadata *MD) {
3192 return MD->getMetadataID() == DICommonBlockKind;
3193 }
3194};
3195
3196/// Local variable.
3197///
3198/// TODO: Split up flags.
3200 friend class LLVMContextImpl;
3201 friend class MDNode;
3202
3203 unsigned Arg : 16;
3204 DIFlags Flags;
3205
3209 : DIVariable(C, DILocalVariableKind, Storage, Line, Ops, AlignInBits),
3210 Arg(Arg), Flags(Flags) {
3211 assert(Arg < (1 << 16) && "DILocalVariable: Arg out of range");
3212 }
3213 ~DILocalVariable() = default;
3214
3215 static DILocalVariable *getImpl(LLVMContext &Context, DIScope *Scope,
3216 StringRef Name, DIFile *File, unsigned Line,
3217 DIType *Type, unsigned Arg, DIFlags Flags,
3218 uint32_t AlignInBits, DINodeArray Annotations,
3220 bool ShouldCreate = true) {
3221 return getImpl(Context, Scope, getCanonicalMDString(Context, Name), File,
3223 Storage, ShouldCreate);
3224 }
3225 static DILocalVariable *getImpl(LLVMContext &Context, Metadata *Scope,
3226 MDString *Name, Metadata *File, unsigned Line,
3227 Metadata *Type, unsigned Arg, DIFlags Flags,
3230 bool ShouldCreate = true);
3231
3232 TempDILocalVariable cloneImpl() const {
3234 getLine(), getType(), getArg(), getFlags(),
3236 }
3237
3238public:
3241 unsigned Line, DIType *Type, unsigned Arg, DIFlags Flags,
3242 uint32_t AlignInBits, DINodeArray Annotations),
3244 Annotations))
3247 unsigned Line, Metadata *Type, unsigned Arg, DIFlags Flags,
3250 Annotations))
3251
3252 TempDILocalVariable clone() const { return cloneImpl(); }
3253
3254 /// Get the local scope for this variable.
3255 ///
3256 /// Variables must be defined in a local scope.
3258 return cast<DILocalScope>(DIVariable::getScope());
3259 }
3260
3261 bool isParameter() const { return Arg; }
3262 unsigned getArg() const { return Arg; }
3263 DIFlags getFlags() const { return Flags; }
3264
3265 DINodeArray getAnnotations() const {
3266 return cast_or_null<MDTuple>(getRawAnnotations());
3267 }
3268 Metadata *getRawAnnotations() const { return getOperand(4); }
3269
3270 bool isArtificial() const { return getFlags() & FlagArtificial; }
3271 bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
3272
3273 /// Check that a location is valid for this variable.
3274 ///
3275 /// Check that \c DL exists, is in the same subprogram, and has the same
3276 /// inlined-at location as \c this. (Otherwise, it's not a valid attachment
3277 /// to a \a DbgInfoIntrinsic.)
3279 return DL && getScope()->getSubprogram() == DL->getScope()->getSubprogram();
3280 }
3281
3282 static bool classof(const Metadata *MD) {
3283 return MD->getMetadataID() == DILocalVariableKind;
3284 }
3285};
3286
3287/// Label.
3288///
3289class DILabel : public DINode {
3290 friend class LLVMContextImpl;
3291 friend class MDNode;
3292
3293 unsigned Line;
3294
3295 DILabel(LLVMContext &C, StorageType Storage, unsigned Line,
3297 ~DILabel() = default;
3298
3300 DIFile *File, unsigned Line, StorageType Storage,
3301 bool ShouldCreate = true) {
3302 return getImpl(Context, Scope, getCanonicalMDString(Context, Name), File,
3303 Line, Storage, ShouldCreate);
3304 }
3305 static DILabel *getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
3306 Metadata *File, unsigned Line, StorageType Storage,
3307 bool ShouldCreate = true);
3308
3309 TempDILabel cloneImpl() const {
3311 getLine());
3312 }
3313
3314public:
3317 unsigned Line),
3318 (Scope, Name, File, Line))
3321 unsigned Line),
3322 (Scope, Name, File, Line))
3323
3324 TempDILabel clone() const { return cloneImpl(); }
3325
3326 /// Get the local scope for this label.
3327 ///
3328 /// Labels must be defined in a local scope.
3330 return cast_or_null<DILocalScope>(getRawScope());
3331 }
3332 unsigned getLine() const { return Line; }
3333 StringRef getName() const { return getStringOperand(1); }
3334 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
3335
3336 Metadata *getRawScope() const { return getOperand(0); }
3337 MDString *getRawName() const { return getOperandAs<MDString>(1); }
3338 Metadata *getRawFile() const { return getOperand(2); }
3339
3340 /// Check that a location is valid for this label.
3341 ///
3342 /// Check that \c DL exists, is in the same subprogram, and has the same
3343 /// inlined-at location as \c this. (Otherwise, it's not a valid attachment
3344 /// to a \a DbgInfoIntrinsic.)
3346 return DL && getScope()->getSubprogram() == DL->getScope()->getSubprogram();
3347 }
3348
3349 static bool classof(const Metadata *MD) {
3350 return MD->getMetadataID() == DILabelKind;
3351 }
3352};
3353
3354class DIObjCProperty : public DINode {
3355 friend class LLVMContextImpl;
3356 friend class MDNode;
3357
3358 unsigned Line;
3359 unsigned Attributes;
3360
3362 unsigned Attributes, ArrayRef<Metadata *> Ops);
3363 ~DIObjCProperty() = default;
3364
3365 static DIObjCProperty *
3366 getImpl(LLVMContext &Context, StringRef Name, DIFile *File, unsigned Line,
3367 StringRef GetterName, StringRef SetterName, unsigned Attributes,
3368 DIType *Type, StorageType Storage, bool ShouldCreate = true) {
3369 return getImpl(Context, getCanonicalMDString(Context, Name), File, Line,
3371 getCanonicalMDString(Context, SetterName), Attributes, Type,
3372 Storage, ShouldCreate);
3373 }
3374 static DIObjCProperty *getImpl(LLVMContext &Context, MDString *Name,
3375 Metadata *File, unsigned Line,
3377 unsigned Attributes, Metadata *Type,
3378 StorageType Storage, bool ShouldCreate = true);
3379
3380 TempDIObjCProperty cloneImpl() const {
3381 return getTemporary(getContext(), getName(), getFile(), getLine(),
3383 getType());
3384 }
3385
3386public:
3388 (StringRef Name, DIFile *File, unsigned Line,
3390 unsigned Attributes, DIType *Type),
3391 (Name, File, Line, GetterName, SetterName, Attributes,
3392 Type))
3394 (MDString * Name, Metadata *File, unsigned Line,
3396 unsigned Attributes, Metadata *Type),
3397 (Name, File, Line, GetterName, SetterName, Attributes,
3398 Type))
3399
3400 TempDIObjCProperty clone() const { return cloneImpl(); }
3401
3402 unsigned getLine() const { return Line; }
3403 unsigned getAttributes() const { return Attributes; }
3404 StringRef getName() const { return getStringOperand(0); }
3405 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
3408 DIType *getType() const { return cast_or_null<DIType>(getRawType()); }
3409
3411 if (auto *F = getFile())
3412 return F->getFilename();
3413 return "";
3414 }
3415
3417 if (auto *F = getFile())
3418 return F->getDirectory();
3419 return "";
3420 }
3421
3422 MDString *getRawName() const { return getOperandAs<MDString>(0); }
3423 Metadata *getRawFile() const { return getOperand(1); }
3424 MDString *getRawGetterName() const { return getOperandAs<MDString>(2); }
3425 MDString *getRawSetterName() const { return getOperandAs<MDString>(3); }
3426 Metadata *getRawType() const { return getOperand(4); }
3427
3428 static bool classof(const Metadata *MD) {
3429 return MD->getMetadataID() == DIObjCPropertyKind;
3430 }
3431};
3432
3433/// An imported module (C++ using directive or similar).
3434class DIImportedEntity : public DINode {
3435 friend class LLVMContextImpl;
3436 friend class MDNode;
3437
3438 unsigned Line;
3439
3441 unsigned Line, ArrayRef<Metadata *> Ops)
3442 : DINode(C, DIImportedEntityKind, Storage, Tag, Ops), Line(Line) {}
3443 ~DIImportedEntity() = default;
3444
3445 static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
3447 unsigned Line, StringRef Name,
3448 DINodeArray Elements, StorageType Storage,
3449 bool ShouldCreate = true) {
3450 return getImpl(Context, Tag, Scope, Entity, File, Line,
3451 getCanonicalMDString(Context, Name), Elements.get(), Storage,
3452 ShouldCreate);
3453 }
3454 static DIImportedEntity *
3455 getImpl(LLVMContext &Context, unsigned Tag, Metadata *Scope, Metadata *Entity,
3456 Metadata *File, unsigned Line, MDString *Name, Metadata *Elements,
3457 StorageType Storage, bool ShouldCreate = true);
3458
3459 TempDIImportedEntity cloneImpl() const {
3460 return getTemporary(getContext(), getTag(), getScope(), getEntity(),
3461 getFile(), getLine(), getName(), getElements());
3462 }
3463
3464public:
3466 (unsigned Tag, DIScope *Scope, DINode *Entity, DIFile *File,
3467 unsigned Line, StringRef Name = "",
3468 DINodeArray Elements = nullptr),
3469 (Tag, Scope, Entity, File, Line, Name, Elements))
3472 Metadata *File, unsigned Line, MDString *Name,
3473 Metadata *Elements = nullptr),
3474 (Tag, Scope, Entity, File, Line, Name, Elements))
3475
3476 TempDIImportedEntity clone() const { return cloneImpl(); }
3477
3478 unsigned getLine() const { return Line; }
3479 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
3480 DINode *getEntity() const { return cast_or_null<DINode>(getRawEntity()); }
3481 StringRef getName() const { return getStringOperand(2); }
3482 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
3483 DINodeArray getElements() const {
3484 return cast_or_null<MDTuple>(getRawElements());
3485 }
3486
3487 Metadata *getRawScope() const { return getOperand(0); }
3488 Metadata *getRawEntity() const { return getOperand(1); }
3489 MDString *getRawName() const { return getOperandAs<MDString>(2); }
3490 Metadata *getRawFile() const { return getOperand(3); }
3491 Metadata *getRawElements() const { return getOperand(4); }
3492
3493 static bool classof(const Metadata *MD) {
3494 return MD->getMetadataID() == DIImportedEntityKind;
3495 }
3496};
3497
3498/// A pair of DIGlobalVariable and DIExpression.
3500 friend class LLVMContextImpl;
3501 friend class MDNode;
3502
3505 : MDNode(C, DIGlobalVariableExpressionKind, Storage, Ops) {}
3506 ~DIGlobalVariableExpression() = default;
3507
3509 getImpl(LLVMContext &Context, Metadata *Variable, Metadata *Expression,
3510 StorageType Storage, bool ShouldCreate = true);
3511
3512 TempDIGlobalVariableExpression cloneImpl() const {
3514 }
3515
3516public:
3518 (Metadata * Variable, Metadata *Expression),
3519 (Variable, Expression))
3520
3521 TempDIGlobalVariableExpression clone() const { return cloneImpl(); }
3522
3523 Metadata *getRawVariable() const { return getOperand(0); }
3524
3526 return cast_or_null<DIGlobalVariable>(getRawVariable());
3527 }
3528
3529 Metadata *getRawExpression() const { return getOperand(1); }
3530
3532 return cast<DIExpression>(getRawExpression());
3533 }
3534
3535 static bool classof(const Metadata *MD) {
3536 return MD->getMetadataID() == DIGlobalVariableExpressionKind;
3537 }
3538};
3539
3540/// Macro Info DWARF-like metadata node.
3541///
3542/// A metadata node with a DWARF macro info (i.e., a constant named
3543/// \c DW_MACINFO_*, defined in llvm/BinaryFormat/Dwarf.h). Called \a
3544/// DIMacroNode
3545/// because it's potentially used for non-DWARF output.
3546class DIMacroNode : public MDNode {
3547 friend class LLVMContextImpl;
3548 friend class MDNode;
3549
3550protected:
3551 DIMacroNode(LLVMContext &C, unsigned ID, StorageType Storage, unsigned MIType,
3553 ArrayRef<Metadata *> Ops2 = std::nullopt)
3554 : MDNode(C, ID, Storage, Ops1, Ops2) {
3555 assert(MIType < 1u << 16);
3556 SubclassData16 = MIType;
3557 }
3558 ~DIMacroNode() = default;
3559
3560 template <class Ty> Ty *getOperandAs(unsigned I) const {
3561 return cast_or_null<Ty>(getOperand(I));
3562 }
3563
3564 StringRef getStringOperand(unsigned I) const {
3565 if (auto *S = getOperandAs<MDString>(I))
3566 return S->getString();
3567 return StringRef();
3568 }
3569
3571 if (S.empty())
3572 return nullptr;
3573 return MDString::get(