LCOV - code coverage report
Current view: top level - include/llvm/IR - DebugInfoMetadata.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 499 506 98.6 %
Date: 2018-02-23 15:42:53 Functions: 67 70 95.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- llvm/IR/DebugInfoMetadata.h - Debug info metadata --------*- C++ -*-===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : //
      10             : // Declarations for metadata specific to debug info.
      11             : //
      12             : //===----------------------------------------------------------------------===//
      13             : 
      14             : #ifndef LLVM_IR_DEBUGINFOMETADATA_H
      15             : #define LLVM_IR_DEBUGINFOMETADATA_H
      16             : 
      17             : #include "llvm/ADT/ArrayRef.h"
      18             : #include "llvm/ADT/BitmaskEnum.h"
      19             : #include "llvm/ADT/None.h"
      20             : #include "llvm/ADT/Optional.h"
      21             : #include "llvm/ADT/PointerUnion.h"
      22             : #include "llvm/ADT/STLExtras.h"
      23             : #include "llvm/ADT/SmallVector.h"
      24             : #include "llvm/ADT/StringRef.h"
      25             : #include "llvm/ADT/iterator_range.h"
      26             : #include "llvm/BinaryFormat/Dwarf.h"
      27             : #include "llvm/IR/Constants.h"
      28             : #include "llvm/IR/Metadata.h"
      29             : #include "llvm/Support/Casting.h"
      30             : #include <cassert>
      31             : #include <climits>
      32             : #include <cstddef>
      33             : #include <cstdint>
      34             : #include <iterator>
      35             : #include <type_traits>
      36             : #include <vector>
      37             : 
      38             : // Helper macros for defining get() overrides.
      39             : #define DEFINE_MDNODE_GET_UNPACK_IMPL(...) __VA_ARGS__
      40             : #define DEFINE_MDNODE_GET_UNPACK(ARGS) DEFINE_MDNODE_GET_UNPACK_IMPL ARGS
      41             : #define DEFINE_MDNODE_GET_DISTINCT_TEMPORARY(CLASS, FORMAL, ARGS)              \
      42             :   static CLASS *getDistinct(LLVMContext &Context,                              \
      43             :                             DEFINE_MDNODE_GET_UNPACK(FORMAL)) {                \
      44             :     return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Distinct);         \
      45             :   }                                                                            \
      46             :   static Temp##CLASS getTemporary(LLVMContext &Context,                        \
      47             :                                   DEFINE_MDNODE_GET_UNPACK(FORMAL)) {          \
      48             :     return Temp##CLASS(                                                        \
      49             :         getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Temporary));          \
      50             :   }
      51             : #define DEFINE_MDNODE_GET(CLASS, FORMAL, ARGS)                                 \
      52             :   static CLASS *get(LLVMContext &Context, DEFINE_MDNODE_GET_UNPACK(FORMAL)) {  \
      53             :     return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Uniqued);          \
      54             :   }                                                                            \
      55             :   static CLASS *getIfExists(LLVMContext &Context,                              \
      56             :                             DEFINE_MDNODE_GET_UNPACK(FORMAL)) {                \
      57             :     return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Uniqued,           \
      58             :                    /* ShouldCreate */ false);                                  \
      59             :   }                                                                            \
      60             :   DEFINE_MDNODE_GET_DISTINCT_TEMPORARY(CLASS, FORMAL, ARGS)
      61             : 
      62             : namespace llvm {
      63             : 
      64             : /// Holds a subclass of DINode.
      65             : ///
      66             : /// FIXME: This class doesn't currently make much sense.  Previously it was a
      67             : /// union beteen MDString (for ODR-uniqued types) and things like DIType.  To
      68             : /// support CodeView work, it wasn't deleted outright when MDString-based type
      69             : /// references were deleted; we'll soon need a similar concept for CodeView
      70             : /// DITypeIndex.
      71             : template <class T> class TypedDINodeRef {
      72             :   const Metadata *MD = nullptr;
      73             : 
      74             : public:
      75             :   TypedDINodeRef() = default;
      76             :   TypedDINodeRef(std::nullptr_t) {}
      77        9327 :   TypedDINodeRef(const T *MD) : MD(MD) {}
      78             : 
      79           8 :   explicit TypedDINodeRef(const Metadata *MD) : MD(MD) {
      80             :     assert((!MD || isa<T>(MD)) && "Expected valid type ref");
      81             :   }
      82             : 
      83             :   template <class U>
      84             :   TypedDINodeRef(
      85             :       const TypedDINodeRef<U> &X,
      86             :       typename std::enable_if<std::is_convertible<U *, T *>::value>::type * =
      87             :           nullptr)
      88             :       : MD(X) {}
      89             : 
      90      208539 :   operator Metadata *() const { return const_cast<Metadata *>(MD); }
      91             : 
      92       11881 :   T *resolve() const { return const_cast<T *>(cast_or_null<T>(MD)); }
      93             : 
      94             :   bool operator==(const TypedDINodeRef<T> &X) const { return MD == X.MD; }
      95             :   bool operator!=(const TypedDINodeRef<T> &X) const { return MD != X.MD; }
      96             : };
      97             : 
      98             : using DINodeRef = TypedDINodeRef<DINode>;
      99             : using DIScopeRef = TypedDINodeRef<DIScope>;
     100             : using DITypeRef = TypedDINodeRef<DIType>;
     101             : 
     102             : class DITypeRefArray {
     103             :   const MDTuple *N = nullptr;
     104             : 
     105             : public:
     106             :   DITypeRefArray() = default;
     107             :   DITypeRefArray(const MDTuple *N) : N(N) {}
     108             : 
     109             :   explicit operator bool() const { return get(); }
     110             :   explicit operator MDTuple *() const { return get(); }
     111             : 
     112             :   MDTuple *get() const { return const_cast<MDTuple *>(N); }
     113             :   MDTuple *operator->() const { return get(); }
     114             :   MDTuple &operator*() const { return *get(); }
     115             : 
     116             :   // FIXME: Fix callers and remove condition on N.
     117       59987 :   unsigned size() const { return N ? N->getNumOperands() : 0u; }
     118       71323 :   DITypeRef operator[](unsigned I) const { return DITypeRef(N->getOperand(I)); }
     119             : 
     120             :   class iterator : std::iterator<std::input_iterator_tag, DITypeRef,
     121             :                                  std::ptrdiff_t, void, DITypeRef> {
     122             :     MDNode::op_iterator I = nullptr;
     123             : 
     124             :   public:
     125             :     iterator() = default;
     126             :     explicit iterator(MDNode::op_iterator I) : I(I) {}
     127             : 
     128             :     DITypeRef operator*() const { return DITypeRef(*I); }
     129             : 
     130             :     iterator &operator++() {
     131         587 :       ++I;
     132             :       return *this;
     133             :     }
     134             : 
     135             :     iterator operator++(int) {
     136             :       iterator Temp(*this);
     137             :       ++I;
     138             :       return Temp;
     139             :     }
     140             : 
     141             :     bool operator==(const iterator &X) const { return I == X.I; }
     142             :     bool operator!=(const iterator &X) const { return I != X.I; }
     143             :   };
     144             : 
     145             :   // FIXME: Fix callers and remove condition on N.
     146         315 :   iterator begin() const { return N ? iterator(N->op_begin()) : iterator(); }
     147         315 :   iterator end() const { return N ? iterator(N->op_end()) : iterator(); }
     148             : };
     149             : 
     150             : /// Tagged DWARF-like metadata node.
     151             : ///
     152             : /// A metadata node with a DWARF tag (i.e., a constant named \c DW_TAG_*,
     153             : /// defined in llvm/BinaryFormat/Dwarf.h).  Called \a DINode because it's
     154             : /// potentially used for non-DWARF output.
     155             : class DINode : public MDNode {
     156             :   friend class LLVMContextImpl;
     157             :   friend class MDNode;
     158             : 
     159             : protected:
     160             :   DINode(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
     161             :          ArrayRef<Metadata *> Ops1, ArrayRef<Metadata *> Ops2 = None)
     162      177303 :       : MDNode(C, ID, Storage, Ops1, Ops2) {
     163             :     assert(Tag < 1u << 16);
     164      177303 :     SubclassData16 = Tag;
     165             :   }
     166             :   ~DINode() = default;
     167             : 
     168             :   template <class Ty> Ty *getOperandAs(unsigned I) const {
     169     4596372 :     return cast_or_null<Ty>(getOperand(I));
     170             :   }
     171             : 
     172     4116079 :   StringRef getStringOperand(unsigned I) const {
     173             :     if (auto *S = getOperandAs<MDString>(I))
     174     4048188 :       return S->getString();
     175       67891 :     return StringRef();
     176             :   }
     177             : 
     178             :   static MDString *getCanonicalMDString(LLVMContext &Context, StringRef S) {
     179      218898 :     if (S.empty())
     180             :       return nullptr;
     181      152690 :     return MDString::get(Context, S);
     182             :   }
     183             : 
     184             :   /// Allow subclasses to mutate the tag.
     185           4 :   void setTag(unsigned Tag) { SubclassData16 = Tag; }
     186             : 
     187             : public:
     188      344644 :   unsigned getTag() const { return SubclassData16; }
     189             : 
     190             :   /// Debug info flags.
     191             :   ///
     192             :   /// The three accessibility flags are mutually exclusive and rolled together
     193             :   /// in the first two bits.
     194             :   enum DIFlags : uint32_t {
     195             : #define HANDLE_DI_FLAG(ID, NAME) Flag##NAME = ID,
     196             : #define DI_FLAG_LARGEST_NEEDED
     197             : #include "llvm/IR/DebugInfoFlags.def"
     198             :     FlagAccessibility = FlagPrivate | FlagProtected | FlagPublic,
     199             :     FlagPtrToMemberRep = FlagSingleInheritance | FlagMultipleInheritance |
     200             :                          FlagVirtualInheritance,
     201             :     LLVM_MARK_AS_BITMASK_ENUM(FlagLargest)
     202             :   };
     203             : 
     204             :   static DIFlags getFlag(StringRef Flag);
     205             :   static StringRef getFlagString(DIFlags Flag);
     206             : 
     207             :   /// Split up a flags bitfield.
     208             :   ///
     209             :   /// Split \c Flags into \c SplitFlags, a vector of its components.  Returns
     210             :   /// any remaining (unrecognized) bits.
     211             :   static DIFlags splitFlags(DIFlags Flags,
     212             :                             SmallVectorImpl<DIFlags> &SplitFlags);
     213             : 
     214             :   static bool classof(const Metadata *MD) {
     215        1120 :     switch (MD->getMetadataID()) {
     216             :     default:
     217             :       return false;
     218             :     case GenericDINodeKind:
     219             :     case DISubrangeKind:
     220             :     case DIEnumeratorKind:
     221             :     case DIBasicTypeKind:
     222             :     case DIDerivedTypeKind:
     223             :     case DICompositeTypeKind:
     224             :     case DISubroutineTypeKind:
     225             :     case DIFileKind:
     226             :     case DICompileUnitKind:
     227             :     case DISubprogramKind:
     228             :     case DILexicalBlockKind:
     229             :     case DILexicalBlockFileKind:
     230             :     case DINamespaceKind:
     231             :     case DITemplateTypeParameterKind:
     232             :     case DITemplateValueParameterKind:
     233             :     case DIGlobalVariableKind:
     234             :     case DILocalVariableKind:
     235             :     case DIObjCPropertyKind:
     236             :     case DIImportedEntityKind:
     237             :     case DIModuleKind:
     238             :       return true;
     239             :     }
     240             :   }
     241             : };
     242             : 
     243             : template <class T> struct simplify_type<const TypedDINodeRef<T>> {
     244             :   using SimpleType = Metadata *;
     245             : 
     246             :   static SimpleType getSimplifiedValue(const TypedDINodeRef<T> &MD) {
     247             :     return MD;
     248             :   }
     249             : };
     250             : 
     251             : template <class T>
     252             : struct simplify_type<TypedDINodeRef<T>>
     253             :     : simplify_type<const TypedDINodeRef<T>> {};
     254             : 
     255             : /// Generic tagged DWARF-like metadata node.
     256             : ///
     257             : /// An un-specialized DWARF-like metadata node.  The first operand is a
     258             : /// (possibly empty) null-separated \a MDString header that contains arbitrary
     259             : /// fields.  The remaining operands are \a dwarf_operands(), and are pointers
     260             : /// to other metadata.
     261             : class GenericDINode : public DINode {
     262             :   friend class LLVMContextImpl;
     263             :   friend class MDNode;
     264             : 
     265             :   GenericDINode(LLVMContext &C, StorageType Storage, unsigned Hash,
     266             :                 unsigned Tag, ArrayRef<Metadata *> Ops1,
     267             :                 ArrayRef<Metadata *> Ops2)
     268             :       : DINode(C, GenericDINodeKind, Storage, Tag, Ops1, Ops2) {
     269             :     setHash(Hash);
     270             :   }
     271          96 :   ~GenericDINode() { dropAllReferences(); }
     272             : 
     273          74 :   void setHash(unsigned Hash) { SubclassData32 = Hash; }
     274             :   void recalculateHash();
     275             : 
     276           6 :   static GenericDINode *getImpl(LLVMContext &Context, unsigned Tag,
     277             :                                 StringRef Header, ArrayRef<Metadata *> DwarfOps,
     278             :                                 StorageType Storage, bool ShouldCreate = true) {
     279           6 :     return getImpl(Context, Tag, getCanonicalMDString(Context, Header),
     280           6 :                    DwarfOps, Storage, ShouldCreate);
     281             :   }
     282             : 
     283             :   static GenericDINode *getImpl(LLVMContext &Context, unsigned Tag,
     284             :                                 MDString *Header, ArrayRef<Metadata *> DwarfOps,
     285             :                                 StorageType Storage, bool ShouldCreate = true);
     286             : 
     287           1 :   TempGenericDINode cloneImpl() const {
     288             :     return getTemporary(
     289             :         getContext(), getTag(), getHeader(),
     290           2 :         SmallVector<Metadata *, 4>(dwarf_op_begin(), dwarf_op_end()));
     291             :   }
     292             : 
     293             : public:
     294             :   unsigned getHash() const { return SubclassData32; }
     295             : 
     296           6 :   DEFINE_MDNODE_GET(GenericDINode, (unsigned Tag, StringRef Header,
     297             :                                     ArrayRef<Metadata *> DwarfOps),
     298             :                     (Tag, Header, DwarfOps))
     299          55 :   DEFINE_MDNODE_GET(GenericDINode, (unsigned Tag, MDString *Header,
     300             :                                     ArrayRef<Metadata *> DwarfOps),
     301             :                     (Tag, Header, DwarfOps))
     302             : 
     303             :   /// Return a (temporary) clone of this.
     304           1 :   TempGenericDINode clone() const { return cloneImpl(); }
     305             : 
     306          97 :   unsigned getTag() const { return SubclassData16; }
     307          25 :   StringRef getHeader() const { return getStringOperand(0); }
     308             :   MDString *getRawHeader() const { return getOperandAs<MDString>(0); }
     309             : 
     310           6 :   op_iterator dwarf_op_begin() const { return op_begin() + 1; }
     311           6 :   op_iterator dwarf_op_end() const { return op_end(); }
     312             :   op_range dwarf_operands() const {
     313             :     return op_range(dwarf_op_begin(), dwarf_op_end());
     314             :   }
     315             : 
     316          21 :   unsigned getNumDwarfOperands() const { return getNumOperands() - 1; }
     317             :   const MDOperand &getDwarfOperand(unsigned I) const {
     318           3 :     return getOperand(I + 1);
     319             :   }
     320             :   void replaceDwarfOperandWith(unsigned I, Metadata *New) {
     321           1 :     replaceOperandWith(I + 1, New);
     322             :   }
     323             : 
     324             :   static bool classof(const Metadata *MD) {
     325             :     return MD->getMetadataID() == GenericDINodeKind;
     326             :   }
     327             : };
     328             : 
     329             : /// Array subrange.
     330             : ///
     331             : /// TODO: Merge into node for DW_TAG_array_type, which should have a custom
     332             : /// type.
     333             : class DISubrange : public DINode {
     334             :   friend class LLVMContextImpl;
     335             :   friend class MDNode;
     336             : 
     337             :   int64_t LowerBound;
     338             : 
     339             :   DISubrange(LLVMContext &C, StorageType Storage, Metadata *Node,
     340             :              int64_t LowerBound, ArrayRef<Metadata *> Ops)
     341         566 :       : DINode(C, DISubrangeKind, Storage, dwarf::DW_TAG_subrange_type, Ops),
     342         566 :         LowerBound(LowerBound) {}
     343             : 
     344         420 :   ~DISubrange() = default;
     345             : 
     346             :   static DISubrange *getImpl(LLVMContext &Context, int64_t Count,
     347             :                              int64_t LowerBound, StorageType Storage,
     348             :                              bool ShouldCreate = true);
     349             : 
     350             :   static DISubrange *getImpl(LLVMContext &Context, Metadata *CountNode,
     351             :                              int64_t LowerBound, StorageType Storage,
     352             :                              bool ShouldCreate = true);
     353             : 
     354           1 :   TempDISubrange cloneImpl() const {
     355           2 :     return getTemporary(getContext(), getRawCountNode(), getLowerBound());
     356             :   }
     357             : 
     358             : public:
     359         567 :   DEFINE_MDNODE_GET(DISubrange, (int64_t Count, int64_t LowerBound = 0),
     360             :                     (Count, LowerBound))
     361             : 
     362          77 :   DEFINE_MDNODE_GET(DISubrange, (Metadata *CountNode, int64_t LowerBound = 0),
     363             :                     (CountNode, LowerBound))
     364             : 
     365           1 :   TempDISubrange clone() const { return cloneImpl(); }
     366             : 
     367             :   int64_t getLowerBound() const { return LowerBound; }
     368             : 
     369             :   Metadata *getRawCountNode() const {
     370        2738 :     return getOperand(0).get();
     371             :   }
     372             : 
     373             :   typedef PointerUnion<ConstantInt*, DIVariable*> CountType;
     374             : 
     375        1843 :   CountType getCount() const {
     376             :     if (auto *MD = dyn_cast<ConstantAsMetadata>(getRawCountNode()))
     377        1693 :       return CountType(cast<ConstantInt>(MD->getValue()));
     378             : 
     379             :     if (auto *DV = dyn_cast<DIVariable>(getRawCountNode()))
     380         147 :       return CountType(DV);
     381             : 
     382           3 :     return CountType();
     383             :   }
     384             : 
     385             :   static bool classof(const Metadata *MD) {
     386          40 :     return MD->getMetadataID() == DISubrangeKind;
     387             :   }
     388             : };
     389             : 
     390             : /// Enumeration value.
     391             : ///
     392             : /// TODO: Add a pointer to the context (DW_TAG_enumeration_type) once that no
     393             : /// longer creates a type cycle.
     394             : class DIEnumerator : public DINode {
     395             :   friend class LLVMContextImpl;
     396             :   friend class MDNode;
     397             : 
     398             :   int64_t Value;
     399             :   DIEnumerator(LLVMContext &C, StorageType Storage, int64_t Value,
     400             :                bool IsUnsigned, ArrayRef<Metadata *> Ops)
     401        6575 :       : DINode(C, DIEnumeratorKind, Storage, dwarf::DW_TAG_enumerator, Ops),
     402        6575 :         Value(Value) {
     403        6575 :     SubclassData32 = IsUnsigned;
     404             :   }
     405        6248 :   ~DIEnumerator() = default;
     406             : 
     407         397 :   static DIEnumerator *getImpl(LLVMContext &Context, int64_t Value,
     408             :                                bool IsUnsigned, StringRef Name,
     409             :                                StorageType Storage, bool ShouldCreate = true) {
     410         794 :     return getImpl(Context, Value, IsUnsigned,
     411         397 :                    getCanonicalMDString(Context, Name), Storage, ShouldCreate);
     412             :   }
     413             :   static DIEnumerator *getImpl(LLVMContext &Context, int64_t Value,
     414             :                                bool IsUnsigned, MDString *Name,
     415             :                                StorageType Storage, bool ShouldCreate = true);
     416             : 
     417           1 :   TempDIEnumerator cloneImpl() const {
     418           3 :     return getTemporary(getContext(), getValue(), isUnsigned(), getName());
     419             :   }
     420             : 
     421             : public:
     422         397 :   DEFINE_MDNODE_GET(DIEnumerator, (int64_t Value, bool IsUnsigned, StringRef Name),
     423             :                     (Value, IsUnsigned, Name))
     424        6186 :   DEFINE_MDNODE_GET(DIEnumerator, (int64_t Value, bool IsUnsigned, MDString *Name),
     425             :                     (Value, IsUnsigned, Name))
     426             : 
     427           1 :   TempDIEnumerator clone() const { return cloneImpl(); }
     428             : 
     429             :   int64_t getValue() const { return Value; }
     430       13173 :   bool isUnsigned() const { return SubclassData32; }
     431        6539 :   StringRef getName() const { return getStringOperand(0); }
     432             : 
     433             :   MDString *getRawName() const { return getOperandAs<MDString>(0); }
     434             : 
     435             :   static bool classof(const Metadata *MD) {
     436        6450 :     return MD->getMetadataID() == DIEnumeratorKind;
     437             :   }
     438             : };
     439             : 
     440             : /// Base class for scope-like contexts.
     441             : ///
     442             : /// Base class for lexical scopes and types (which are also declaration
     443             : /// contexts).
     444             : ///
     445             : /// TODO: Separate the concepts of declaration contexts and lexical scopes.
     446             : class DIScope : public DINode {
     447             : protected:
     448             :   DIScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
     449             :           ArrayRef<Metadata *> Ops)
     450             :       : DINode(C, ID, Storage, Tag, Ops) {}
     451             :   ~DIScope() = default;
     452             : 
     453             : public:
     454             :   DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
     455             : 
     456             :   inline StringRef getFilename() const;
     457             :   inline StringRef getDirectory() const;
     458             : 
     459             :   StringRef getName() const;
     460             :   DIScopeRef getScope() const;
     461             : 
     462             :   /// Return the raw underlying file.
     463             :   ///
     464             :   /// A \a DIFile is a \a DIScope, but it doesn't point at a separate file (it
     465             :   /// \em is the file).  If \c this is an \a DIFile, we need to return \c this.
     466             :   /// Otherwise, return the first operand, which is where all other subclasses
     467             :   /// store their file pointer.
     468             :   Metadata *getRawFile() const {
     469     2703472 :     return isa<DIFile>(this) ? const_cast<DIScope *>(this)
     470     2703471 :                              : static_cast<Metadata *>(getOperand(0));
     471             :   }
     472             : 
     473             :   static bool classof(const Metadata *MD) {
     474       45176 :     switch (MD->getMetadataID()) {
     475             :     default:
     476             :       return false;
     477             :     case DIBasicTypeKind:
     478             :     case DIDerivedTypeKind:
     479             :     case DICompositeTypeKind:
     480             :     case DISubroutineTypeKind:
     481             :     case DIFileKind:
     482             :     case DICompileUnitKind:
     483             :     case DISubprogramKind:
     484             :     case DILexicalBlockKind:
     485             :     case DILexicalBlockFileKind:
     486             :     case DINamespaceKind:
     487             :     case DIModuleKind:
     488             :       return true;
     489             :     }
     490             :   }
     491             : };
     492             : 
     493             : /// File.
     494             : ///
     495             : /// TODO: Merge with directory/file node (including users).
     496             : /// TODO: Canonicalize paths on creation.
     497             : class DIFile : public DIScope {
     498             :   friend class LLVMContextImpl;
     499             :   friend class MDNode;
     500             : 
     501             : public:
     502             :   /// Which algorithm (e.g. MD5) a checksum was generated with.
     503             :   ///
     504             :   /// The encoding is explicit because it is used directly in Bitcode. The
     505             :   /// value 0 is reserved to indicate the absence of a checksum in Bitcode.
     506             :   enum ChecksumKind {
     507             :     // The first variant was originally CSK_None, encoded as 0. The new
     508             :     // internal representation removes the need for this by wrapping the
     509             :     // ChecksumInfo in an Optional, but to preserve Bitcode compatibility the 0
     510             :     // encoding is reserved.
     511             :     CSK_MD5 = 1,
     512             :     CSK_SHA1 = 2,
     513             :     CSK_Last = CSK_SHA1 // Should be last enumeration.
     514             :   };
     515             : 
     516             :   /// A single checksum, represented by a \a Kind and a \a Value (a string).
     517             :   template <typename T>
     518             :   struct ChecksumInfo {
     519             :     /// The kind of checksum which \a Value encodes.
     520             :     ChecksumKind Kind;
     521             :     /// The string value of the checksum.
     522             :     T Value;
     523             : 
     524         617 :     ChecksumInfo(ChecksumKind Kind, T Value) : Kind(Kind), Value(Value) { }
     525             :     ~ChecksumInfo() = default;
     526           1 :     bool operator==(const ChecksumInfo<T> &X) const {
     527          56 :       return Kind == X.Kind && Value == X.Value;
     528             :     }
     529             :     bool operator!=(const ChecksumInfo<T> &X) const { return !(*this == X); }
     530          51 :     StringRef getKindAsString() const { return getChecksumKindAsString(Kind); }
     531             :   };
     532             : 
     533             : private:
     534             :   Optional<ChecksumInfo<MDString *>> Checksum;
     535             : 
     536        7075 :   DIFile(LLVMContext &C, StorageType Storage,
     537             :          Optional<ChecksumInfo<MDString *>> CS,
     538             :          ArrayRef<Metadata *> Ops)
     539        7075 :       : DIScope(C, DIFileKind, Storage, dwarf::DW_TAG_file_type, Ops),
     540        7075 :         Checksum(CS) {}
     541        3354 :   ~DIFile() = default;
     542             : 
     543        9845 :   static DIFile *getImpl(LLVMContext &Context, StringRef Filename,
     544             :                          StringRef Directory,
     545             :                          Optional<ChecksumInfo<StringRef>> CS,
     546             :                          StorageType Storage, bool ShouldCreate = true) {
     547             :     Optional<ChecksumInfo<MDString *>> MDChecksum;
     548        9845 :     if (CS)
     549             :       MDChecksum.emplace(CS->Kind, getCanonicalMDString(Context, CS->Value));
     550       19690 :     return getImpl(Context, getCanonicalMDString(Context, Filename),
     551             :                    getCanonicalMDString(Context, Directory), MDChecksum,
     552       19690 :                    Storage, ShouldCreate);
     553             :   }
     554             :   static DIFile *getImpl(LLVMContext &Context, MDString *Filename,
     555             :                          MDString *Directory,
     556             :                          Optional<ChecksumInfo<MDString *>> CS,
     557             :                          StorageType Storage, bool ShouldCreate = true);
     558             : 
     559           1 :   TempDIFile cloneImpl() const {
     560             :     return getTemporary(getContext(), getFilename(), getDirectory(),
     561           1 :                         getChecksum());
     562             :   }
     563             : 
     564             : public:
     565       19691 :   DEFINE_MDNODE_GET(DIFile, (StringRef Filename, StringRef Directory,
     566             :                              Optional<ChecksumInfo<StringRef>> CS = None),
     567             :                     (Filename, Directory, CS))
     568        5594 :   DEFINE_MDNODE_GET(DIFile, (MDString * Filename, MDString *Directory,
     569             :                              Optional<ChecksumInfo<MDString *>> CS = None),
     570             :                     (Filename, Directory, CS))
     571             : 
     572           1 :   TempDIFile clone() const { return cloneImpl(); }
     573             : 
     574     2381669 :   StringRef getFilename() const { return getStringOperand(0); }
     575      679328 :   StringRef getDirectory() const { return getStringOperand(1); }
     576             :   Optional<ChecksumInfo<StringRef>> getChecksum() const {
     577             :     Optional<ChecksumInfo<StringRef>> StringRefChecksum;
     578      597519 :     if (Checksum)
     579        1418 :       StringRefChecksum.emplace(Checksum->Kind, Checksum->Value->getString());
     580             :     return StringRefChecksum;
     581             :   }
     582             : 
     583             :   MDString *getRawFilename() const { return getOperandAs<MDString>(0); }
     584             :   MDString *getRawDirectory() const { return getOperandAs<MDString>(1); }
     585             :   Optional<ChecksumInfo<MDString *>> getRawChecksum() const { return Checksum; }
     586             : 
     587             :   static StringRef getChecksumKindAsString(ChecksumKind CSKind);
     588             :   static Optional<ChecksumKind> getChecksumKind(StringRef CSKindStr);
     589             : 
     590             :   static bool classof(const Metadata *MD) {
     591     2784476 :     return MD->getMetadataID() == DIFileKind;
     592             :   }
     593             : };
     594             : 
     595     1789756 : StringRef DIScope::getFilename() const {
     596             :   if (auto *F = getFile())
     597             :     return F->getFilename();
     598          43 :   return "";
     599             : }
     600             : 
     601       95046 : StringRef DIScope::getDirectory() const {
     602             :   if (auto *F = getFile())
     603             :     return F->getDirectory();
     604           3 :   return "";
     605             : }
     606             : 
     607             : /// Base class for types.
     608             : ///
     609             : /// TODO: Remove the hardcoded name and context, since many types don't use
     610             : /// them.
     611             : /// TODO: Split up flags.
     612             : class DIType : public DIScope {
     613             :   unsigned Line;
     614             :   DIFlags Flags;
     615             :   uint64_t SizeInBits;
     616             :   uint64_t OffsetInBits;
     617             :   uint32_t AlignInBits;
     618             : 
     619             : protected:
     620             :   DIType(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
     621             :          unsigned Line, uint64_t SizeInBits, uint32_t AlignInBits,
     622             :          uint64_t OffsetInBits, DIFlags Flags, ArrayRef<Metadata *> Ops)
     623       70364 :       : DIScope(C, ID, Storage, Tag, Ops) {
     624             :     init(Line, SizeInBits, AlignInBits, OffsetInBits, Flags);
     625             :   }
     626             :   ~DIType() = default;
     627             : 
     628             :   void init(unsigned Line, uint64_t SizeInBits, uint32_t AlignInBits,
     629             :             uint64_t OffsetInBits, DIFlags Flags) {
     630       70368 :     this->Line = Line;
     631       70368 :     this->Flags = Flags;
     632       70368 :     this->SizeInBits = SizeInBits;
     633       70368 :     this->AlignInBits = AlignInBits;
     634       70368 :     this->OffsetInBits = OffsetInBits;
     635             :   }
     636             : 
     637             :   /// Change fields in place.
     638             :   void mutate(unsigned Tag, unsigned Line, uint64_t SizeInBits,
     639             :               uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags) {
     640             :     assert(isDistinct() && "Only distinct nodes can mutate");
     641             :     setTag(Tag);
     642             :     init(Line, SizeInBits, AlignInBits, OffsetInBits, Flags);
     643             :   }
     644             : 
     645             : public:
     646             :   TempDIType clone() const {
     647       30610 :     return TempDIType(cast<DIType>(MDNode::clone().release()));
     648             :   }
     649             : 
     650             :   unsigned getLine() const { return Line; }
     651             :   uint64_t getSizeInBits() const { return SizeInBits; }
     652             :   uint32_t getAlignInBits() const { return AlignInBits; }
     653        7232 :   uint32_t getAlignInBytes() const { return getAlignInBits() / CHAR_BIT; }
     654             :   uint64_t getOffsetInBits() const { return OffsetInBits; }
     655             :   DIFlags getFlags() const { return Flags; }
     656             : 
     657             :   DIScopeRef getScope() const { return DIScopeRef(getRawScope()); }
     658       79663 :   StringRef getName() const { return getStringOperand(2); }
     659             : 
     660             : 
     661      299597 :   Metadata *getRawScope() const { return getOperand(1); }
     662             :   MDString *getRawName() const { return getOperandAs<MDString>(2); }
     663             : 
     664             :   void setFlags(DIFlags NewFlags) {
     665             :     assert(!isUniqued() && "Cannot set flags on uniqued nodes");
     666       15308 :     Flags = NewFlags;
     667             :   }
     668             : 
     669             :   bool isPrivate() const {
     670             :     return (getFlags() & FlagAccessibility) == FlagPrivate;
     671             :   }
     672             :   bool isProtected() const {
     673        4015 :     return (getFlags() & FlagAccessibility) == FlagProtected;
     674             :   }
     675             :   bool isPublic() const {
     676             :     return (getFlags() & FlagAccessibility) == FlagPublic;
     677             :   }
     678       31922 :   bool isForwardDecl() const { return getFlags() & FlagFwdDecl; }
     679        3060 :   bool isAppleBlockExtension() const { return getFlags() & FlagAppleBlock; }
     680       34020 :   bool isBlockByrefStruct() const { return getFlags() & FlagBlockByrefStruct; }
     681        4294 :   bool isVirtual() const { return getFlags() & FlagVirtual; }
     682       45259 :   bool isArtificial() const { return getFlags() & FlagArtificial; }
     683       29820 :   bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
     684             :   bool isObjcClassComplete() const {
     685        3060 :     return getFlags() & FlagObjcClassComplete;
     686             :   }
     687         245 :   bool isVector() const { return getFlags() & FlagVector; }
     688          87 :   bool isBitField() const { return getFlags() & FlagBitField; }
     689        4014 :   bool isStaticMember() const { return getFlags() & FlagStaticMember; }
     690         134 :   bool isLValueReference() const { return getFlags() & FlagLValueReference; }
     691         134 :   bool isRValueReference() const { return getFlags() & FlagRValueReference; }
     692        3060 :   bool isTypePassByValue() const { return getFlags() & FlagTypePassByValue; }
     693             :   bool isTypePassByReference() const {
     694             :     return getFlags() & FlagTypePassByReference;
     695             :   }
     696             : 
     697             :   static bool classof(const Metadata *MD) {
     698      425047 :     switch (MD->getMetadataID()) {
     699             :     default:
     700             :       return false;
     701             :     case DIBasicTypeKind:
     702             :     case DIDerivedTypeKind:
     703             :     case DICompositeTypeKind:
     704             :     case DISubroutineTypeKind:
     705             :       return true;
     706             :     }
     707             :   }
     708             : };
     709             : 
     710             : /// Basic type, like 'int' or 'float'.
     711             : ///
     712             : /// TODO: Split out DW_TAG_unspecified_type.
     713             : /// TODO: Drop unused accessors.
     714             : class DIBasicType : public DIType {
     715             :   friend class LLVMContextImpl;
     716             :   friend class MDNode;
     717             : 
     718             :   unsigned Encoding;
     719             : 
     720             :   DIBasicType(LLVMContext &C, StorageType Storage, unsigned Tag,
     721             :               uint64_t SizeInBits, uint32_t AlignInBits, unsigned Encoding,
     722             :               ArrayRef<Metadata *> Ops)
     723        2700 :       : DIType(C, DIBasicTypeKind, Storage, Tag, 0, SizeInBits, AlignInBits, 0,
     724             :                FlagZero, Ops),
     725        2700 :         Encoding(Encoding) {}
     726        2116 :   ~DIBasicType() = default;
     727             : 
     728        1241 :   static DIBasicType *getImpl(LLVMContext &Context, unsigned Tag,
     729             :                               StringRef Name, uint64_t SizeInBits,
     730             :                               uint32_t AlignInBits, unsigned Encoding,
     731             :                               StorageType Storage, bool ShouldCreate = true) {
     732        1241 :     return getImpl(Context, Tag, getCanonicalMDString(Context, Name),
     733        1241 :                    SizeInBits, AlignInBits, Encoding, Storage, ShouldCreate);
     734             :   }
     735             :   static DIBasicType *getImpl(LLVMContext &Context, unsigned Tag,
     736             :                               MDString *Name, uint64_t SizeInBits,
     737             :                               uint32_t AlignInBits, unsigned Encoding,
     738             :                               StorageType Storage, bool ShouldCreate = true);
     739             : 
     740           2 :   TempDIBasicType cloneImpl() const {
     741             :     return getTemporary(getContext(), getTag(), getName(), getSizeInBits(),
     742           6 :                         getAlignInBits(), getEncoding());
     743             :   }
     744             : 
     745             : public:
     746          68 :   DEFINE_MDNODE_GET(DIBasicType, (unsigned Tag, StringRef Name),
     747             :                     (Tag, Name, 0, 0, 0))
     748        1173 :   DEFINE_MDNODE_GET(DIBasicType,
     749             :                     (unsigned Tag, StringRef Name, uint64_t SizeInBits,
     750             :                      uint32_t AlignInBits, unsigned Encoding),
     751             :                     (Tag, Name, SizeInBits, AlignInBits, Encoding))
     752        1504 :   DEFINE_MDNODE_GET(DIBasicType,
     753             :                     (unsigned Tag, MDString *Name, uint64_t SizeInBits,
     754             :                      uint32_t AlignInBits, unsigned Encoding),
     755             :                     (Tag, Name, SizeInBits, AlignInBits, Encoding))
     756             : 
     757           1 :   TempDIBasicType clone() const { return cloneImpl(); }
     758             : 
     759             :   unsigned getEncoding() const { return Encoding; }
     760             : 
     761             :   static bool classof(const Metadata *MD) {
     762       18588 :     return MD->getMetadataID() == DIBasicTypeKind;
     763             :   }
     764             : };
     765             : 
     766             : /// Derived types.
     767             : ///
     768             : /// This includes qualified types, pointers, references, friends, typedefs, and
     769             : /// class members.
     770             : ///
     771             : /// TODO: Split out members (inheritance, fields, methods, etc.).
     772             : class DIDerivedType : public DIType {
     773             :   friend class LLVMContextImpl;
     774             :   friend class MDNode;
     775             : 
     776             :   /// \brief The DWARF address space of the memory pointed to or referenced by a
     777             :   /// pointer or reference type respectively.
     778             :   Optional<unsigned> DWARFAddressSpace;
     779             : 
     780             :   DIDerivedType(LLVMContext &C, StorageType Storage, unsigned Tag,
     781             :                 unsigned Line, uint64_t SizeInBits, uint32_t AlignInBits,
     782             :                 uint64_t OffsetInBits, Optional<unsigned> DWARFAddressSpace,
     783             :                 DIFlags Flags, ArrayRef<Metadata *> Ops)
     784       37268 :       : DIType(C, DIDerivedTypeKind, Storage, Tag, Line, SizeInBits,
     785             :                AlignInBits, OffsetInBits, Flags, Ops),
     786             :         DWARFAddressSpace(DWARFAddressSpace) {}
     787       18871 :   ~DIDerivedType() = default;
     788             : 
     789       49270 :   static DIDerivedType *getImpl(LLVMContext &Context, unsigned Tag,
     790             :                                 StringRef Name, DIFile *File, unsigned Line,
     791             :                                 DIScopeRef Scope, DITypeRef BaseType,
     792             :                                 uint64_t SizeInBits, uint32_t AlignInBits,
     793             :                                 uint64_t OffsetInBits,
     794             :                                 Optional<unsigned> DWARFAddressSpace,
     795             :                                 DIFlags Flags, Metadata *ExtraData,
     796             :                                 StorageType Storage, bool ShouldCreate = true) {
     797       98540 :     return getImpl(Context, Tag, getCanonicalMDString(Context, Name), File,
     798             :                    Line, Scope, BaseType, SizeInBits, AlignInBits, OffsetInBits,
     799       98540 :                    DWARFAddressSpace, Flags, ExtraData, Storage, ShouldCreate);
     800             :   }
     801             :   static DIDerivedType *getImpl(LLVMContext &Context, unsigned Tag,
     802             :                                 MDString *Name, Metadata *File, unsigned Line,
     803             :                                 Metadata *Scope, Metadata *BaseType,
     804             :                                 uint64_t SizeInBits, uint32_t AlignInBits,
     805             :                                 uint64_t OffsetInBits,
     806             :                                 Optional<unsigned> DWARFAddressSpace,
     807             :                                 DIFlags Flags, Metadata *ExtraData,
     808             :                                 StorageType Storage, bool ShouldCreate = true);
     809             : 
     810       15307 :   TempDIDerivedType cloneImpl() const {
     811             :     return getTemporary(getContext(), getTag(), getName(), getFile(), getLine(),
     812             :                         getScope(), getBaseType(), getSizeInBits(),
     813             :                         getAlignInBits(), getOffsetInBits(),
     814       76535 :                         getDWARFAddressSpace(), getFlags(), getExtraData());
     815             :   }
     816             : 
     817             : public:
     818        2080 :   DEFINE_MDNODE_GET(DIDerivedType,
     819             :                     (unsigned Tag, MDString *Name, Metadata *File,
     820             :                      unsigned Line, Metadata *Scope, Metadata *BaseType,
     821             :                      uint64_t SizeInBits, uint32_t AlignInBits,
     822             :                      uint64_t OffsetInBits,
     823             :                      Optional<unsigned> DWARFAddressSpace, DIFlags Flags,
     824             :                      Metadata *ExtraData = nullptr),
     825             :                     (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
     826             :                      AlignInBits, OffsetInBits, DWARFAddressSpace, Flags,
     827             :                      ExtraData))
     828       79884 :   DEFINE_MDNODE_GET(DIDerivedType,
     829             :                     (unsigned Tag, StringRef Name, DIFile *File, unsigned Line,
     830             :                      DIScopeRef Scope, DITypeRef BaseType, uint64_t SizeInBits,
     831             :                      uint32_t AlignInBits, uint64_t OffsetInBits,
     832             :                      Optional<unsigned> DWARFAddressSpace, DIFlags Flags,
     833             :                      Metadata *ExtraData = nullptr),
     834             :                     (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
     835             :                      AlignInBits, OffsetInBits, DWARFAddressSpace, Flags,
     836             :                      ExtraData))
     837             : 
     838           1 :   TempDIDerivedType clone() const { return cloneImpl(); }
     839             : 
     840             :   /// Get the base type this is derived from.
     841             :   DITypeRef getBaseType() const { return DITypeRef(getRawBaseType()); }
     842      149417 :   Metadata *getRawBaseType() const { return getOperand(3); }
     843             : 
     844             :   /// \returns The DWARF address space of the memory pointed to or referenced by
     845             :   /// a pointer or reference type respectively.
     846             :   Optional<unsigned> getDWARFAddressSpace() const { return DWARFAddressSpace; }
     847             : 
     848             :   /// Get extra data associated with this derived type.
     849             :   ///
     850             :   /// Class type for pointer-to-members, objective-c property node for ivars,
     851             :   /// or global constant wrapper for static members.
     852             :   ///
     853             :   /// TODO: Separate out types that need this extra operand: pointer-to-member
     854             :   /// types and member fields (static members and ivars).
     855             :   Metadata *getExtraData() const { return getRawExtraData(); }
     856       15451 :   Metadata *getRawExtraData() const { return getOperand(4); }
     857             : 
     858             :   /// Get casted version of extra data.
     859             :   /// @{
     860             :   DITypeRef getClassType() const {
     861             :     assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
     862             :     return DITypeRef(getExtraData());
     863             :   }
     864             : 
     865             :   DIObjCProperty *getObjCProperty() const {
     866             :     return dyn_cast_or_null<DIObjCProperty>(getExtraData());
     867             :   }
     868             : 
     869             :   Constant *getStorageOffsetInBits() const {
     870             :     assert(getTag() == dwarf::DW_TAG_member && isBitField());
     871             :     if (auto *C = cast_or_null<ConstantAsMetadata>(getExtraData()))
     872             :       return C->getValue();
     873             :     return nullptr;
     874             :   }
     875             : 
     876             :   Constant *getConstant() const {
     877             :     assert(getTag() == dwarf::DW_TAG_member && isStaticMember());
     878             :     if (auto *C = cast_or_null<ConstantAsMetadata>(getExtraData()))
     879             :       return C->getValue();
     880             :     return nullptr;
     881             :   }
     882             :   Constant *getDiscriminantValue() const {
     883             :     assert(getTag() == dwarf::DW_TAG_member && !isStaticMember());
     884             :     if (auto *C = cast_or_null<ConstantAsMetadata>(getExtraData()))
     885             :       return C->getValue();
     886             :     return nullptr;
     887             :   }
     888             :   /// @}
     889             : 
     890             :   static bool classof(const Metadata *MD) {
     891        8664 :     return MD->getMetadataID() == DIDerivedTypeKind;
     892             :   }
     893             : };
     894             : 
     895             : /// Composite types.
     896             : ///
     897             : /// TODO: Detach from DerivedTypeBase (split out MDEnumType?).
     898             : /// TODO: Create a custom, unrelated node for DW_TAG_array_type.
     899             : class DICompositeType : public DIType {
     900             :   friend class LLVMContextImpl;
     901             :   friend class MDNode;
     902             : 
     903             :   unsigned RuntimeLang;
     904             : 
     905             :   DICompositeType(LLVMContext &C, StorageType Storage, unsigned Tag,
     906             :                   unsigned Line, unsigned RuntimeLang, uint64_t SizeInBits,
     907             :                   uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags,
     908             :                   ArrayRef<Metadata *> Ops)
     909        6146 :       : DIType(C, DICompositeTypeKind, Storage, Tag, Line, SizeInBits,
     910             :                AlignInBits, OffsetInBits, Flags, Ops),
     911        6146 :         RuntimeLang(RuntimeLang) {}
     912        2985 :   ~DICompositeType() = default;
     913             : 
     914             :   /// Change fields in place.
     915             :   void mutate(unsigned Tag, unsigned Line, unsigned RuntimeLang,
     916             :               uint64_t SizeInBits, uint32_t AlignInBits,
     917             :               uint64_t OffsetInBits, DIFlags Flags) {
     918             :     assert(isDistinct() && "Only distinct nodes can mutate");
     919             :     assert(getRawIdentifier() && "Only ODR-uniqued nodes should mutate");
     920           4 :     this->RuntimeLang = RuntimeLang;
     921             :     DIType::mutate(Tag, Line, SizeInBits, AlignInBits, OffsetInBits, Flags);
     922             :   }
     923             : 
     924             :   static DICompositeType *
     925        5056 :   getImpl(LLVMContext &Context, unsigned Tag, StringRef Name, Metadata *File,
     926             :           unsigned Line, DIScopeRef Scope, DITypeRef BaseType,
     927             :           uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits,
     928             :           DIFlags Flags, DINodeArray Elements, unsigned RuntimeLang,
     929             :           DITypeRef VTableHolder, DITemplateParameterArray TemplateParams,
     930             :           StringRef Identifier, DIDerivedType *Discriminator,
     931             :           StorageType Storage, bool ShouldCreate = true) {
     932        5056 :     return getImpl(
     933             :         Context, Tag, getCanonicalMDString(Context, Name), File, Line, Scope,
     934             :         BaseType, SizeInBits, AlignInBits, OffsetInBits, Flags, Elements.get(),
     935             :         RuntimeLang, VTableHolder, TemplateParams.get(),
     936        5056 :         getCanonicalMDString(Context, Identifier), Discriminator, Storage, ShouldCreate);
     937             :   }
     938             :   static DICompositeType *
     939             :   getImpl(LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
     940             :           unsigned Line, Metadata *Scope, Metadata *BaseType,
     941             :           uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits,
     942             :           DIFlags Flags, Metadata *Elements, unsigned RuntimeLang,
     943             :           Metadata *VTableHolder, Metadata *TemplateParams,
     944             :           MDString *Identifier, Metadata *Discriminator,
     945             :           StorageType Storage, bool ShouldCreate = true);
     946             : 
     947           2 :   TempDICompositeType cloneImpl() const {
     948             :     return getTemporary(getContext(), getTag(), getName(), getFile(), getLine(),
     949             :                         getScope(), getBaseType(), getSizeInBits(),
     950             :                         getAlignInBits(), getOffsetInBits(), getFlags(),
     951             :                         getElements(), getRuntimeLang(), getVTableHolder(),
     952          10 :                         getTemplateParams(), getIdentifier(), getDiscriminator());
     953             :   }
     954             : 
     955             : public:
     956        5056 :   DEFINE_MDNODE_GET(DICompositeType,
     957             :                     (unsigned Tag, StringRef Name, DIFile *File, unsigned Line,
     958             :                      DIScopeRef Scope, DITypeRef BaseType, uint64_t SizeInBits,
     959             :                      uint32_t AlignInBits, uint64_t OffsetInBits,
     960             :                      DIFlags Flags, DINodeArray Elements, unsigned RuntimeLang,
     961             :                      DITypeRef VTableHolder,
     962             :                      DITemplateParameterArray TemplateParams = nullptr,
     963             :                      StringRef Identifier = "", DIDerivedType *Discriminator = nullptr),
     964             :                     (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
     965             :                      AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
     966             :                      VTableHolder, TemplateParams, Identifier, Discriminator))
     967        1112 :   DEFINE_MDNODE_GET(DICompositeType,
     968             :                     (unsigned Tag, MDString *Name, Metadata *File,
     969             :                      unsigned Line, Metadata *Scope, Metadata *BaseType,
     970             :                      uint64_t SizeInBits, uint32_t AlignInBits,
     971             :                      uint64_t OffsetInBits, DIFlags Flags, Metadata *Elements,
     972             :                      unsigned RuntimeLang, Metadata *VTableHolder,
     973             :                      Metadata *TemplateParams = nullptr,
     974             :                      MDString *Identifier = nullptr,
     975             :                      Metadata *Discriminator = nullptr),
     976             :                     (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
     977             :                      AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
     978             :                      VTableHolder, TemplateParams, Identifier, Discriminator))
     979             : 
     980           1 :   TempDICompositeType clone() const { return cloneImpl(); }
     981             : 
     982             :   /// Get a DICompositeType with the given ODR identifier.
     983             :   ///
     984             :   /// If \a LLVMContext::isODRUniquingDebugTypes(), gets the mapped
     985             :   /// DICompositeType for the given ODR \c Identifier.  If none exists, creates
     986             :   /// a new node.
     987             :   ///
     988             :   /// Else, returns \c nullptr.
     989             :   static DICompositeType *
     990             :   getODRType(LLVMContext &Context, MDString &Identifier, unsigned Tag,
     991             :              MDString *Name, Metadata *File, unsigned Line, Metadata *Scope,
     992             :              Metadata *BaseType, uint64_t SizeInBits, uint32_t AlignInBits,
     993             :              uint64_t OffsetInBits, DIFlags Flags, Metadata *Elements,
     994             :              unsigned RuntimeLang, Metadata *VTableHolder,
     995             :              Metadata *TemplateParams, Metadata *Discriminator);
     996             :   static DICompositeType *getODRTypeIfExists(LLVMContext &Context,
     997             :                                              MDString &Identifier);
     998             : 
     999             :   /// Build a DICompositeType with the given ODR identifier.
    1000             :   ///
    1001             :   /// Looks up the mapped DICompositeType for the given ODR \c Identifier.  If
    1002             :   /// it doesn't exist, creates a new one.  If it does exist and \a
    1003             :   /// isForwardDecl(), and the new arguments would be a definition, mutates the
    1004             :   /// the type in place.  In either case, returns the type.
    1005             :   ///
    1006             :   /// If not \a LLVMContext::isODRUniquingDebugTypes(), this function returns
    1007             :   /// nullptr.
    1008             :   static DICompositeType *
    1009             :   buildODRType(LLVMContext &Context, MDString &Identifier, unsigned Tag,
    1010             :                MDString *Name, Metadata *File, unsigned Line, Metadata *Scope,
    1011             :                Metadata *BaseType, uint64_t SizeInBits, uint32_t AlignInBits,
    1012             :                uint64_t OffsetInBits, DIFlags Flags, Metadata *Elements,
    1013             :                unsigned RuntimeLang, Metadata *VTableHolder,
    1014             :                Metadata *TemplateParams, Metadata *Discriminator);
    1015             : 
    1016             :   DITypeRef getBaseType() const { return DITypeRef(getRawBaseType()); }
    1017             :   DINodeArray getElements() const {
    1018             :     return cast_or_null<MDTuple>(getRawElements());
    1019             :   }
    1020             :   DITypeRef getVTableHolder() const { return DITypeRef(getRawVTableHolder()); }
    1021             :   DITemplateParameterArray getTemplateParams() const {
    1022             :     return cast_or_null<MDTuple>(getRawTemplateParams());
    1023             :   }
    1024        1807 :   StringRef getIdentifier() const { return getStringOperand(7); }
    1025             :   unsigned getRuntimeLang() const { return RuntimeLang; }
    1026             : 
    1027       13700 :   Metadata *getRawBaseType() const { return getOperand(3); }
    1028        3364 :   Metadata *getRawElements() const { return getOperand(4); }
    1029           1 :   Metadata *getRawVTableHolder() const { return getOperand(5); }
    1030           3 :   Metadata *getRawTemplateParams() const { return getOperand(6); }
    1031             :   MDString *getRawIdentifier() const { return getOperandAs<MDString>(7); }
    1032             :   Metadata *getRawDiscriminator() const { return getOperand(8); }
    1033             :   DIDerivedType *getDiscriminator() const { return getOperandAs<DIDerivedType>(8); }
    1034             : 
    1035             :   /// Replace operands.
    1036             :   ///
    1037             :   /// If this \a isUniqued() and not \a isResolved(), on a uniquing collision
    1038             :   /// this will be RAUW'ed and deleted.  Use a \a TrackingMDRef to keep track
    1039             :   /// of its movement if necessary.
    1040             :   /// @{
    1041             :   void replaceElements(DINodeArray Elements) {
    1042             : #ifndef NDEBUG
    1043             :     for (DINode *Op : getElements())
    1044             :       assert(is_contained(Elements->operands(), Op) &&
    1045             :              "Lost a member during member list replacement");
    1046             : #endif
    1047        3250 :     replaceOperandWith(4, Elements.get());
    1048             :   }
    1049             : 
    1050             :   void replaceVTableHolder(DITypeRef VTableHolder) {
    1051        2995 :     replaceOperandWith(5, VTableHolder);
    1052             :   }
    1053             : 
    1054             :   void replaceTemplateParams(DITemplateParameterArray TemplateParams) {
    1055        2024 :     replaceOperandWith(6, TemplateParams.get());
    1056             :   }
    1057             :   /// @}
    1058             : 
    1059             :   static bool classof(const Metadata *MD) {
    1060       16397 :     return MD->getMetadataID() == DICompositeTypeKind;
    1061             :   }
    1062             : };
    1063             : 
    1064             : /// Type array for a subprogram.
    1065             : ///
    1066             : /// TODO: Fold the array of types in directly as operands.
    1067             : class DISubroutineType : public DIType {
    1068             :   friend class LLVMContextImpl;
    1069             :   friend class MDNode;
    1070             : 
    1071             :   /// The calling convention used with DW_AT_calling_convention. Actually of
    1072             :   /// type dwarf::CallingConvention.
    1073             :   uint8_t CC;
    1074             : 
    1075       24250 :   DISubroutineType(LLVMContext &C, StorageType Storage, DIFlags Flags,
    1076             :                    uint8_t CC, ArrayRef<Metadata *> Ops)
    1077       24250 :       : DIType(C, DISubroutineTypeKind, Storage, dwarf::DW_TAG_subroutine_type,
    1078             :                0, 0, 0, 0, Flags, Ops),
    1079       24250 :         CC(CC) {}
    1080        3579 :   ~DISubroutineType() = default;
    1081             : 
    1082             :   static DISubroutineType *getImpl(LLVMContext &Context, DIFlags Flags,
    1083             :                                    uint8_t CC, DITypeRefArray TypeArray,
    1084             :                                    StorageType Storage,
    1085             :                                    bool ShouldCreate = true) {
    1086       52974 :     return getImpl(Context, Flags, CC, TypeArray.get(), Storage, ShouldCreate);
    1087             :   }
    1088             :   static DISubroutineType *getImpl(LLVMContext &Context, DIFlags Flags,
    1089             :                                    uint8_t CC, Metadata *TypeArray,
    1090             :                                    StorageType Storage,
    1091             :                                    bool ShouldCreate = true);
    1092             : 
    1093           3 :   TempDISubroutineType cloneImpl() const {
    1094           6 :     return getTemporary(getContext(), getFlags(), getCC(), getTypeArray());
    1095             :   }
    1096             : 
    1097             : public:
    1098             :   DEFINE_MDNODE_GET(DISubroutineType,
    1099             :                     (DIFlags Flags, uint8_t CC, DITypeRefArray TypeArray),
    1100             :                     (Flags, CC, TypeArray))
    1101        1998 :   DEFINE_MDNODE_GET(DISubroutineType,
    1102             :                     (DIFlags Flags, uint8_t CC, Metadata *TypeArray),
    1103             :                     (Flags, CC, TypeArray))
    1104             : 
    1105           1 :   TempDISubroutineType clone() const { return cloneImpl(); }
    1106             : 
    1107             :   uint8_t getCC() const { return CC; }
    1108             : 
    1109             :   DITypeRefArray getTypeArray() const {
    1110             :     return cast_or_null<MDTuple>(getRawTypeArray());
    1111             :   }
    1112             : 
    1113      175842 :   Metadata *getRawTypeArray() const { return getOperand(3); }
    1114             : 
    1115             :   static bool classof(const Metadata *MD) {
    1116         218 :     return MD->getMetadataID() == DISubroutineTypeKind;
    1117             :   }
    1118             : };
    1119             : 
    1120             : /// Compile unit.
    1121             : class DICompileUnit : public DIScope {
    1122             :   friend class LLVMContextImpl;
    1123             :   friend class MDNode;
    1124             : 
    1125             : public:
    1126             :   enum DebugEmissionKind : unsigned {
    1127             :     NoDebug = 0,
    1128             :     FullDebug,
    1129             :     LineTablesOnly,
    1130             :     LastEmissionKind = LineTablesOnly
    1131             :   };
    1132             : 
    1133             :   static Optional<DebugEmissionKind> getEmissionKind(StringRef Str);
    1134             :   static const char *EmissionKindString(DebugEmissionKind EK);
    1135             : 
    1136             : private:
    1137             :   unsigned SourceLanguage;
    1138             :   bool IsOptimized;
    1139             :   unsigned RuntimeVersion;
    1140             :   unsigned EmissionKind;
    1141             :   uint64_t DWOId;
    1142             :   bool SplitDebugInlining;
    1143             :   bool DebugInfoForProfiling;
    1144             :   bool GnuPubnames;
    1145             : 
    1146             :   DICompileUnit(LLVMContext &C, StorageType Storage, unsigned SourceLanguage,
    1147             :                 bool IsOptimized, unsigned RuntimeVersion,
    1148             :                 unsigned EmissionKind, uint64_t DWOId, bool SplitDebugInlining,
    1149             :                 bool DebugInfoForProfiling, bool GnuPubnames, ArrayRef<Metadata *> Ops)
    1150        2777 :       : DIScope(C, DICompileUnitKind, Storage, dwarf::DW_TAG_compile_unit, Ops),
    1151             :         SourceLanguage(SourceLanguage), IsOptimized(IsOptimized),
    1152             :         RuntimeVersion(RuntimeVersion), EmissionKind(EmissionKind),
    1153             :         DWOId(DWOId), SplitDebugInlining(SplitDebugInlining),
    1154        2777 :         DebugInfoForProfiling(DebugInfoForProfiling), GnuPubnames(GnuPubnames) {
    1155             :     assert(Storage != Uniqued);
    1156             :   }
    1157        2630 :   ~DICompileUnit() = default;
    1158             : 
    1159             :   static DICompileUnit *
    1160         861 :   getImpl(LLVMContext &Context, unsigned SourceLanguage, DIFile *File,
    1161             :           StringRef Producer, bool IsOptimized, StringRef Flags,
    1162             :           unsigned RuntimeVersion, StringRef SplitDebugFilename,
    1163             :           unsigned EmissionKind, DICompositeTypeArray EnumTypes,
    1164             :           DIScopeArray RetainedTypes,
    1165             :           DIGlobalVariableExpressionArray GlobalVariables,
    1166             :           DIImportedEntityArray ImportedEntities, DIMacroNodeArray Macros,
    1167             :           uint64_t DWOId, bool SplitDebugInlining, bool DebugInfoForProfiling,
    1168             :           bool GnuPubnames, StorageType Storage, bool ShouldCreate = true) {
    1169        1722 :     return getImpl(
    1170             :         Context, SourceLanguage, File, getCanonicalMDString(Context, Producer),
    1171             :         IsOptimized, getCanonicalMDString(Context, Flags), RuntimeVersion,
    1172             :         getCanonicalMDString(Context, SplitDebugFilename), EmissionKind,
    1173             :         EnumTypes.get(), RetainedTypes.get(), GlobalVariables.get(),
    1174             :         ImportedEntities.get(), Macros.get(), DWOId, SplitDebugInlining,
    1175         861 :         DebugInfoForProfiling, GnuPubnames, Storage, ShouldCreate);
    1176             :   }
    1177             :   static DICompileUnit *
    1178             :   getImpl(LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
    1179             :           MDString *Producer, bool IsOptimized, MDString *Flags,
    1180             :           unsigned RuntimeVersion, MDString *SplitDebugFilename,
    1181             :           unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
    1182             :           Metadata *GlobalVariables, Metadata *ImportedEntities,
    1183             :           Metadata *Macros, uint64_t DWOId, bool SplitDebugInlining,
    1184             :           bool DebugInfoForProfiling, bool GnuPubnames, StorageType Storage,
    1185             :           bool ShouldCreate = true);
    1186             : 
    1187         147 :   TempDICompileUnit cloneImpl() const {
    1188             :     return getTemporary(getContext(), getSourceLanguage(), getFile(),
    1189         147 :                         getProducer(), isOptimized(), getFlags(),
    1190             :                         getRuntimeVersion(), getSplitDebugFilename(),
    1191             :                         getEmissionKind(), getEnumTypes(), getRetainedTypes(),
    1192             :                         getGlobalVariables(), getImportedEntities(),
    1193         294 :                         getMacros(), DWOId, getSplitDebugInlining(),
    1194        1029 :                         getDebugInfoForProfiling(), getGnuPubnames());
    1195             :   }
    1196             : 
    1197             : public:
    1198             :   static void get() = delete;
    1199             :   static void getIfExists() = delete;
    1200             : 
    1201         861 :   DEFINE_MDNODE_GET_DISTINCT_TEMPORARY(
    1202             :       DICompileUnit,
    1203             :       (unsigned SourceLanguage, DIFile *File, StringRef Producer,
    1204             :        bool IsOptimized, StringRef Flags, unsigned RuntimeVersion,
    1205             :        StringRef SplitDebugFilename, DebugEmissionKind EmissionKind,
    1206             :        DICompositeTypeArray EnumTypes, DIScopeArray RetainedTypes,
    1207             :        DIGlobalVariableExpressionArray GlobalVariables,
    1208             :        DIImportedEntityArray ImportedEntities, DIMacroNodeArray Macros,
    1209             :        uint64_t DWOId, bool SplitDebugInlining, bool DebugInfoForProfiling,
    1210             :        bool GnuPubnames),
    1211             :       (SourceLanguage, File, Producer, IsOptimized, Flags, RuntimeVersion,
    1212             :        SplitDebugFilename, EmissionKind, EnumTypes, RetainedTypes,
    1213             :        GlobalVariables, ImportedEntities, Macros, DWOId, SplitDebugInlining,
    1214             :        DebugInfoForProfiling, GnuPubnames))
    1215        1916 :   DEFINE_MDNODE_GET_DISTINCT_TEMPORARY(
    1216             :       DICompileUnit,
    1217             :       (unsigned SourceLanguage, Metadata *File, MDString *Producer,
    1218             :        bool IsOptimized, MDString *Flags, unsigned RuntimeVersion,
    1219             :        MDString *SplitDebugFilename, unsigned EmissionKind, Metadata *EnumTypes,
    1220             :        Metadata *RetainedTypes, Metadata *GlobalVariables,
    1221             :        Metadata *ImportedEntities, Metadata *Macros, uint64_t DWOId,
    1222             :        bool SplitDebugInlining, bool DebugInfoForProfiling, bool GnuPubnames),
    1223             :       (SourceLanguage, File, Producer, IsOptimized, Flags, RuntimeVersion,
    1224             :        SplitDebugFilename, EmissionKind, EnumTypes, RetainedTypes,
    1225             :        GlobalVariables, ImportedEntities, Macros, DWOId, SplitDebugInlining,
    1226             :        DebugInfoForProfiling, GnuPubnames))
    1227             : 
    1228           1 :   TempDICompileUnit clone() const { return cloneImpl(); }
    1229             : 
    1230             :   unsigned getSourceLanguage() const { return SourceLanguage; }
    1231             :   bool isOptimized() const { return IsOptimized; }
    1232             :   unsigned getRuntimeVersion() const { return RuntimeVersion; }
    1233             :   DebugEmissionKind getEmissionKind() const {
    1234             :     return (DebugEmissionKind)EmissionKind;
    1235             :   }
    1236             :   bool getDebugInfoForProfiling() const { return DebugInfoForProfiling; }
    1237             :   bool getGnuPubnames() const { return GnuPubnames; }
    1238        2119 :   StringRef getProducer() const { return getStringOperand(1); }
    1239        2200 :   StringRef getFlags() const { return getStringOperand(2); }
    1240        1213 :   StringRef getSplitDebugFilename() const { return getStringOperand(3); }
    1241             :   DICompositeTypeArray getEnumTypes() const {
    1242             :     return cast_or_null<MDTuple>(getRawEnumTypes());
    1243             :   }
    1244             :   DIScopeArray getRetainedTypes() const {
    1245             :     return cast_or_null<MDTuple>(getRawRetainedTypes());
    1246             :   }
    1247             :   DIGlobalVariableExpressionArray getGlobalVariables() const {
    1248             :     return cast_or_null<MDTuple>(getRawGlobalVariables());
    1249             :   }
    1250             :   DIImportedEntityArray getImportedEntities() const {
    1251             :     return cast_or_null<MDTuple>(getRawImportedEntities());
    1252             :   }
    1253             :   DIMacroNodeArray getMacros() const {
    1254             :     return cast_or_null<MDTuple>(getRawMacros());
    1255             :   }
    1256             :   uint64_t getDWOId() const { return DWOId; }
    1257          23 :   void setDWOId(uint64_t DwoId) { DWOId = DwoId; }
    1258             :   bool getSplitDebugInlining() const { return SplitDebugInlining; }
    1259             :   void setSplitDebugInlining(bool SplitDebugInlining) {
    1260             :     this->SplitDebugInlining = SplitDebugInlining;
    1261             :   }
    1262             : 
    1263             :   MDString *getRawProducer() const { return getOperandAs<MDString>(1); }
    1264             :   MDString *getRawFlags() const { return getOperandAs<MDString>(2); }
    1265             :   MDString *getRawSplitDebugFilename() const {
    1266             :     return getOperandAs<MDString>(3);
    1267             :   }
    1268        8438 :   Metadata *getRawEnumTypes() const { return getOperand(4); }
    1269         112 :   Metadata *getRawRetainedTypes() const { return getOperand(5); }
    1270         149 :   Metadata *getRawGlobalVariables() const { return getOperand(6); }
    1271        1665 :   Metadata *getRawImportedEntities() const { return getOperand(7); }
    1272        1779 :   Metadata *getRawMacros() const { return getOperand(8); }
    1273             : 
    1274             :   /// Replace arrays.
    1275             :   ///
    1276             :   /// If this \a isUniqued() and not \a isResolved(), it will be RAUW'ed and
    1277             :   /// deleted on a uniquing collision.  In practice, uniquing collisions on \a
    1278             :   /// DICompileUnit should be fairly rare.
    1279             :   /// @{
    1280             :   void replaceEnumTypes(DICompositeTypeArray N) {
    1281         694 :     replaceOperandWith(4, N.get());
    1282             :   }
    1283             :   void replaceRetainedTypes(DITypeArray N) {
    1284         123 :     replaceOperandWith(5, N.get());
    1285             :   }
    1286             :   void replaceGlobalVariables(DIGlobalVariableExpressionArray N) {
    1287         182 :     replaceOperandWith(6, N.get());
    1288             :   }
    1289             :   void replaceImportedEntities(DIImportedEntityArray N) {
    1290          46 :     replaceOperandWith(7, N.get());
    1291             :   }
    1292           7 :   void replaceMacros(DIMacroNodeArray N) { replaceOperandWith(8, N.get()); }
    1293             :   /// @}
    1294             : 
    1295             :   static bool classof(const Metadata *MD) {
    1296       93130 :     return MD->getMetadataID() == DICompileUnitKind;
    1297             :   }
    1298             : };
    1299             : 
    1300             : /// A scope for locals.
    1301             : ///
    1302             : /// A legal scope for lexical blocks, local variables, and debug info
    1303             : /// locations.  Subclasses are \a DISubprogram, \a DILexicalBlock, and \a
    1304             : /// DILexicalBlockFile.
    1305             : class DILocalScope : public DIScope {
    1306             : protected:
    1307             :   DILocalScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
    1308             :                ArrayRef<Metadata *> Ops)
    1309             :       : DIScope(C, ID, Storage, Tag, Ops) {}
    1310             :   ~DILocalScope() = default;
    1311             : 
    1312             : public:
    1313             :   /// Get the subprogram for this scope.
    1314             :   ///
    1315             :   /// Return this if it's an \a DISubprogram; otherwise, look up the scope
    1316             :   /// chain.
    1317             :   DISubprogram *getSubprogram() const;
    1318             : 
    1319             :   /// Get the first non DILexicalBlockFile scope of this scope.
    1320             :   ///
    1321             :   /// Return this if it's not a \a DILexicalBlockFIle; otherwise, look up the
    1322             :   /// scope chain.
    1323             :   DILocalScope *getNonLexicalBlockFileScope() const;
    1324             : 
    1325             :   static bool classof(const Metadata *MD) {
    1326      375183 :     return MD->getMetadataID() == DISubprogramKind ||
    1327      362073 :            MD->getMetadataID() == DILexicalBlockKind ||
    1328             :            MD->getMetadataID() == DILexicalBlockFileKind;
    1329             :   }
    1330             : };
    1331             : 
    1332             : /// Debug location.
    1333             : ///
    1334             : /// A debug location in source code, used for debug info and otherwise.
    1335             : class DILocation : public MDNode {
    1336             :   friend class LLVMContextImpl;
    1337             :   friend class MDNode;
    1338             : 
    1339             :   DILocation(LLVMContext &C, StorageType Storage, unsigned Line,
    1340             :              unsigned Column, ArrayRef<Metadata *> MDs);
    1341       42450 :   ~DILocation() { dropAllReferences(); }
    1342             : 
    1343             :   static DILocation *getImpl(LLVMContext &Context, unsigned Line,
    1344             :                              unsigned Column, Metadata *Scope,
    1345             :                              Metadata *InlinedAt, StorageType Storage,
    1346             :                              bool ShouldCreate = true);
    1347             :   static DILocation *getImpl(LLVMContext &Context, unsigned Line,
    1348             :                              unsigned Column, DILocalScope *Scope,
    1349             :                              DILocation *InlinedAt, StorageType Storage,
    1350             :                              bool ShouldCreate = true) {
    1351             :     return getImpl(Context, Line, Column, static_cast<Metadata *>(Scope),
    1352      155777 :                    static_cast<Metadata *>(InlinedAt), Storage, ShouldCreate);
    1353             :   }
    1354             : 
    1355             :   /// With a given unsigned int \p U, use up to 13 bits to represent it.
    1356             :   /// old_bit 1~5  --> new_bit 1~5
    1357             :   /// old_bit 6~12 --> new_bit 7~13
    1358             :   /// new_bit_6 is 0 if higher bits (7~13) are all 0
    1359             :   static unsigned getPrefixEncodingFromUnsigned(unsigned U) {
    1360         298 :     U &= 0xfff;
    1361         298 :     return U > 0x1f ? (((U & 0xfe0) << 1) | (U & 0x1f) | 0x20) : U;
    1362             :   }
    1363             : 
    1364             :   /// Reverse transformation as getPrefixEncodingFromUnsigned.
    1365             :   static unsigned getUnsignedFromPrefixEncoding(unsigned U) {
    1366        1284 :     return (U & 0x20) ? (((U >> 1) & 0xfe0) | (U & 0x1f)) : (U & 0x1f);
    1367             :   }
    1368             : 
    1369             :   /// Returns the next component stored in discriminator.
    1370             :   static unsigned getNextComponentInDiscriminator(unsigned D) {
    1371         330 :     if ((D & 1) == 0)
    1372         214 :       return D >> ((D & 0x40) ? 14 : 7);
    1373             :     else
    1374         116 :       return D >> 1;
    1375             :   }
    1376             : 
    1377         302 :   TempDILocation cloneImpl() const {
    1378             :     // Get the raw scope/inlinedAt since it is possible to invoke this on
    1379             :     // a DILocation containing temporary metadata.
    1380             :     return getTemporary(getContext(), getLine(), getColumn(), getRawScope(),
    1381         906 :                         getRawInlinedAt());
    1382             :   }
    1383             : 
    1384             : public:
    1385             :   // Disallow replacing operands.
    1386             :   void replaceOperandWith(unsigned I, Metadata *New) = delete;
    1387             : 
    1388     2342198 :   DEFINE_MDNODE_GET(DILocation,
    1389             :                     (unsigned Line, unsigned Column, Metadata *Scope,
    1390             :                      Metadata *InlinedAt = nullptr),
    1391             :                     (Line, Column, Scope, InlinedAt))
    1392             :   DEFINE_MDNODE_GET(DILocation,
    1393             :                     (unsigned Line, unsigned Column, DILocalScope *Scope,
    1394             :                      DILocation *InlinedAt = nullptr),
    1395             :                     (Line, Column, Scope, InlinedAt))
    1396             : 
    1397             :   /// Return a (temporary) clone of this.
    1398           1 :   TempDILocation clone() const { return cloneImpl(); }
    1399             : 
    1400             :   unsigned getLine() const { return SubclassData32; }
    1401     5371803 :   unsigned getColumn() const { return SubclassData16; }
    1402             :   DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); }
    1403             : 
    1404             :   DILocation *getInlinedAt() const {
    1405             :     return cast_or_null<DILocation>(getRawInlinedAt());
    1406             :   }
    1407             : 
    1408             :   DIFile *getFile() const { return getScope()->getFile(); }
    1409      160713 :   StringRef getFilename() const { return getScope()->getFilename(); }
    1410       89090 :   StringRef getDirectory() const { return getScope()->getDirectory(); }
    1411             : 
    1412             :   /// Get the scope where this is inlined.
    1413             :   ///
    1414             :   /// Walk through \a getInlinedAt() and return \a getScope() from the deepest
    1415             :   /// location.
    1416             :   DILocalScope *getInlinedAtScope() const {
    1417             :     if (auto *IA = getInlinedAt())
    1418             :       return IA->getInlinedAtScope();
    1419             :     return getScope();
    1420             :   }
    1421             : 
    1422             :   /// Check whether this can be discriminated from another location.
    1423             :   ///
    1424             :   /// Check \c this can be discriminated from \c RHS in a linetable entry.
    1425             :   /// Scope and inlined-at chains are not recorded in the linetable, so they
    1426             :   /// cannot be used to distinguish basic blocks.
    1427       51051 :   bool canDiscriminate(const DILocation &RHS) const {
    1428       96750 :     return getLine() != RHS.getLine() ||
    1429       90247 :            getColumn() != RHS.getColumn() ||
    1430             :            getDiscriminator() != RHS.getDiscriminator() ||
    1431       51051 :            getFilename() != RHS.getFilename() ||
    1432       51051 :            getDirectory() != RHS.getDirectory();
    1433             :   }
    1434             : 
    1435             :   /// Get the DWARF discriminator.
    1436             :   ///
    1437             :   /// DWARF discriminators distinguish identical file locations between
    1438             :   /// instructions that are on different basic blocks.
    1439             :   ///
    1440             :   /// There are 3 components stored in discriminator, from lower bits:
    1441             :   ///
    1442             :   /// Base discriminator: assigned by AddDiscriminators pass to identify IRs
    1443             :   ///                     that are defined by the same source line, but
    1444             :   ///                     different basic blocks.
    1445             :   /// Duplication factor: assigned by optimizations that will scale down
    1446             :   ///                     the execution frequency of the original IR.
    1447             :   /// Copy Identifier: assigned by optimizations that clones the IR.
    1448             :   ///                  Each copy of the IR will be assigned an identifier.
    1449             :   ///
    1450             :   /// Encoding:
    1451             :   ///
    1452             :   /// The above 3 components are encoded into a 32bit unsigned integer in
    1453             :   /// order. If the lowest bit is 1, the current component is empty, and the
    1454             :   /// next component will start in the next bit. Otherwise, the current
    1455             :   /// component is non-empty, and its content starts in the next bit. The
    1456             :   /// length of each components is either 5 bit or 12 bit: if the 7th bit
    1457             :   /// is 0, the bit 2~6 (5 bits) are used to represent the component; if the
    1458             :   /// 7th bit is 1, the bit 2~6 (5 bits) and 8~14 (7 bits) are combined to
    1459             :   /// represent the component.
    1460             : 
    1461             :   inline unsigned getDiscriminator() const;
    1462             : 
    1463             :   /// Returns a new DILocation with updated \p Discriminator.
    1464             :   inline const DILocation *cloneWithDiscriminator(unsigned Discriminator) const;
    1465             : 
    1466             :   /// Returns a new DILocation with updated base discriminator \p BD.
    1467             :   inline const DILocation *setBaseDiscriminator(unsigned BD) const;
    1468             : 
    1469             :   /// Returns the duplication factor stored in the discriminator.
    1470             :   inline unsigned getDuplicationFactor() const;
    1471             : 
    1472             :   /// Returns the copy identifier stored in the discriminator.
    1473             :   inline unsigned getCopyIdentifier() const;
    1474             : 
    1475             :   /// Returns the base discriminator stored in the discriminator.
    1476             :   inline unsigned getBaseDiscriminator() const;
    1477             : 
    1478             :   /// Returns a new DILocation with duplication factor \p DF encoded in the
    1479             :   /// discriminator.
    1480             :   inline const DILocation *cloneWithDuplicationFactor(unsigned DF) const;
    1481             : 
    1482             :   /// When two instructions are combined into a single instruction we also
    1483             :   /// need to combine the original locations into a single location.
    1484             :   ///
    1485             :   /// When the locations are the same we can use either location. When they
    1486             :   /// differ, we need a third location which is distinct from either. If
    1487             :   /// they have the same file/line but have a different discriminator we
    1488             :   /// could create a location with a new discriminator. If they are from
    1489             :   /// different files/lines the location is ambiguous and can't be
    1490             :   /// represented in a single line entry.  In this case, no location
    1491             :   /// should be set, unless the merged instruction is a call, which we will
    1492             :   /// set the merged debug location as line 0 of the nearest common scope
    1493             :   /// where 2 locations are inlined from. This only applies to Instruction;
    1494             :   /// for MachineInstruction, as it is post-inline, we will treat the call
    1495             :   /// instruction the same way as other instructions.
    1496             :   ///
    1497             :   /// \p ForInst: The Instruction the merged DILocation is for. If the
    1498             :   /// Instruction is unavailable or non-existent, use nullptr.
    1499             :   static const DILocation *
    1500             :   getMergedLocation(const DILocation *LocA, const DILocation *LocB,
    1501             :                     const Instruction *ForInst = nullptr);
    1502             : 
    1503             :   /// Returns the base discriminator for a given encoded discriminator \p D.
    1504             :   static unsigned getBaseDiscriminatorFromDiscriminator(unsigned D) {
    1505        1182 :     if ((D & 1) == 0)
    1506        1116 :       return getUnsignedFromPrefixEncoding(D >> 1);
    1507             :     else
    1508             :       return 0;
    1509             :   }
    1510             : 
    1511             :   /// Returns the duplication factor for a given encoded discriminator \p D.
    1512         110 :   static unsigned getDuplicationFactorFromDiscriminator(unsigned D) {
    1513             :     D = getNextComponentInDiscriminator(D);
    1514         110 :     if (D == 0 || (D & 1))
    1515             :       return 1;
    1516             :     else
    1517          58 :       return getUnsignedFromPrefixEncoding(D >> 1);
    1518             :   }
    1519             : 
    1520             :   /// Returns the copy identifier for a given encoded discriminator \p D.
    1521         110 :   static unsigned getCopyIdentifierFromDiscriminator(unsigned D) {
    1522             :     return getUnsignedFromPrefixEncoding(getNextComponentInDiscriminator(
    1523         110 :         getNextComponentInDiscriminator(D)));
    1524             :   }
    1525             : 
    1526             : 
    1527     8097100 :   Metadata *getRawScope() const { return getOperand(0); }
    1528             :   Metadata *getRawInlinedAt() const {
    1529     7426896 :     if (getNumOperands() == 2)
    1530             :       return getOperand(1);
    1531             :     return nullptr;
    1532             :   }
    1533             : 
    1534             :   static bool classof(const Metadata *MD) {
    1535      178195 :     return MD->getMetadataID() == DILocationKind;
    1536             :   }
    1537             : };
    1538             : 
    1539             : /// Subprogram description.
    1540             : ///
    1541             : /// TODO: Remove DisplayName.  It's always equal to Name.
    1542             : /// TODO: Split up flags.
    1543             : class DISubprogram : public DILocalScope {
    1544             :   friend class LLVMContextImpl;
    1545             :   friend class MDNode;
    1546             : 
    1547             :   unsigned Line;
    1548             :   unsigned ScopeLine;
    1549             :   unsigned VirtualIndex;
    1550             : 
    1551             :   /// In the MS ABI, the implicit 'this' parameter is adjusted in the prologue
    1552             :   /// of method overrides from secondary bases by this amount. It may be
    1553             :   /// negative.
    1554             :   int ThisAdjustment;
    1555             : 
    1556             :   // Virtuality can only assume three values, so we can pack
    1557             :   // in 2 bits (none/pure/pure_virtual).
    1558             :   unsigned Virtuality : 2;
    1559             : 
    1560             :   // These are boolean flags so one bit is enough.
    1561             :   // MSVC starts a new container field every time the base
    1562             :   // type changes so we can't use 'bool' to ensure these bits
    1563             :   // are packed.
    1564             :   unsigned IsLocalToUnit : 1;
    1565             :   unsigned IsDefinition : 1;
    1566             :   unsigned IsOptimized : 1;
    1567             : 
    1568             :   unsigned Padding : 3;
    1569             : 
    1570             :   DIFlags Flags;
    1571             : 
    1572             :   DISubprogram(LLVMContext &C, StorageType Storage, unsigned Line,
    1573             :                unsigned ScopeLine, unsigned Virtuality, unsigned VirtualIndex,
    1574             :                int ThisAdjustment, DIFlags Flags, bool IsLocalToUnit,
    1575             :                bool IsDefinition, bool IsOptimized, ArrayRef<Metadata *> Ops)
    1576       53111 :       : DILocalScope(C, DISubprogramKind, Storage, dwarf::DW_TAG_subprogram,
    1577             :                      Ops),
    1578             :         Line(Line), ScopeLine(ScopeLine), VirtualIndex(VirtualIndex),
    1579             :         ThisAdjustment(ThisAdjustment), Virtuality(Virtuality),
    1580             :         IsLocalToUnit(IsLocalToUnit), IsDefinition(IsDefinition),
    1581       53111 :         IsOptimized(IsOptimized), Flags(Flags) {
    1582             :     static_assert(dwarf::DW_VIRTUALITY_max < 4, "Virtuality out of range");
    1583             :     assert(Virtuality < 4 && "Virtuality out of range");
    1584             :   }
    1585        6759 :   ~DISubprogram() = default;
    1586             : 
    1587             :   static DISubprogram *
    1588       50109 :   getImpl(LLVMContext &Context, DIScopeRef Scope, StringRef Name,
    1589             :           StringRef LinkageName, DIFile *File, unsigned Line,
    1590             :           DISubroutineType *Type, bool IsLocalToUnit, bool IsDefinition,
    1591             :           unsigned ScopeLine, DITypeRef ContainingType, unsigned Virtuality,
    1592             :           unsigned VirtualIndex, int ThisAdjustment, DIFlags Flags,
    1593             :           bool IsOptimized, DICompileUnit *Unit,
    1594             :           DITemplateParameterArray TemplateParams, DISubprogram *Declaration,
    1595             :           DILocalVariableArray Variables, DITypeArray ThrownTypes,
    1596             :           StorageType Storage, bool ShouldCreate = true) {
    1597      150327 :     return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
    1598             :                    getCanonicalMDString(Context, LinkageName), File, Line, Type,
    1599             :                    IsLocalToUnit, IsDefinition, ScopeLine, ContainingType,
    1600             :                    Virtuality, VirtualIndex, ThisAdjustment, Flags, IsOptimized,
    1601             :                    Unit, TemplateParams.get(), Declaration, Variables.get(),
    1602       50109 :                    ThrownTypes.get(), Storage, ShouldCreate);
    1603             :   }
    1604             :   static DISubprogram *
    1605             :   getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
    1606             :           MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
    1607             :           bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
    1608             :           Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
    1609             :           int ThisAdjustment, DIFlags Flags, bool IsOptimized, Metadata *Unit,
    1610             :           Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
    1611             :           Metadata *ThrownTypes, StorageType Storage, bool ShouldCreate = true);
    1612             : 
    1613          66 :   TempDISubprogram cloneImpl() const {
    1614             :     return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
    1615             :                         getFile(), getLine(), getType(), isLocalToUnit(),
    1616             :                         isDefinition(), getScopeLine(), getContainingType(),
    1617             :                         getVirtuality(), getVirtualIndex(), getThisAdjustment(),
    1618             :                         getFlags(), isOptimized(), getUnit(),
    1619             :                         getTemplateParams(), getDeclaration(), getVariables(),
    1620         396 :                         getThrownTypes());
    1621             :   }
    1622             : 
    1623             : public:
    1624       50109 :   DEFINE_MDNODE_GET(DISubprogram,
    1625             :                     (DIScopeRef Scope, StringRef Name, StringRef LinkageName,
    1626             :                      DIFile *File, unsigned Line, DISubroutineType *Type,
    1627             :                      bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
    1628             :                      DITypeRef ContainingType, unsigned Virtuality,
    1629             :                      unsigned VirtualIndex, int ThisAdjustment, DIFlags Flags,
    1630             :                      bool IsOptimized, DICompileUnit *Unit,
    1631             :                      DITemplateParameterArray TemplateParams = nullptr,
    1632             :                      DISubprogram *Declaration = nullptr,
    1633             :                      DILocalVariableArray Variables = nullptr,
    1634             :                      DITypeArray ThrownTypes = nullptr),
    1635             :                     (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
    1636             :                      IsDefinition, ScopeLine, ContainingType, Virtuality,
    1637             :                      VirtualIndex, ThisAdjustment, Flags, IsOptimized, Unit,
    1638             :                      TemplateParams, Declaration, Variables, ThrownTypes))
    1639        3010 :   DEFINE_MDNODE_GET(
    1640             :       DISubprogram,
    1641             :       (Metadata * Scope, MDString *Name, MDString *LinkageName, Metadata *File,
    1642             :        unsigned Line, Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
    1643             :        unsigned ScopeLine, Metadata *ContainingType, unsigned Virtuality,
    1644             :        unsigned VirtualIndex, int ThisAdjustment, DIFlags Flags,
    1645             :        bool IsOptimized, Metadata *Unit, Metadata *TemplateParams = nullptr,
    1646             :        Metadata *Declaration = nullptr, Metadata *Variables = nullptr,
    1647             :        Metadata *ThrownTypes = nullptr),
    1648             :       (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, IsDefinition,
    1649             :        ScopeLine, ContainingType, Virtuality, VirtualIndex, ThisAdjustment,
    1650             :        Flags, IsOptimized, Unit, TemplateParams, Declaration, Variables,
    1651             :        ThrownTypes))
    1652             : 
    1653           2 :   TempDISubprogram clone() const { return cloneImpl(); }
    1654             : 
    1655             : public:
    1656             :   unsigned getLine() const { return Line; }
    1657       86402 :   unsigned getVirtuality() const { return Virtuality; }
    1658             :   unsigned getVirtualIndex() const { return VirtualIndex; }
    1659             :   int getThisAdjustment() const { return ThisAdjustment; }
    1660             :   unsigned getScopeLine() const { return ScopeLine; }
    1661             :   DIFlags getFlags() const { return Flags; }
    1662       82927 :   bool isLocalToUnit() const { return IsLocalToUnit; }
    1663      385785 :   bool isDefinition() const { return IsDefinition; }
    1664       62841 :   bool isOptimized() const { return IsOptimized; }
    1665             : 
    1666       20394 :   bool isArtificial() const { return getFlags() & FlagArtificial; }
    1667             :   bool isPrivate() const {
    1668             :     return (getFlags() & FlagAccessibility) == FlagPrivate;
    1669             :   }
    1670             :   bool isProtected() const {
    1671       20343 :     return (getFlags() & FlagAccessibility) == FlagProtected;
    1672             :   }
    1673             :   bool isPublic() const {
    1674             :     return (getFlags() & FlagAccessibility) == FlagPublic;
    1675             :   }
    1676       20343 :   bool isExplicit() const { return getFlags() & FlagExplicit; }
    1677       20343 :   bool isPrototyped() const { return getFlags() & FlagPrototyped; }
    1678       20343 :   bool isMainSubprogram() const { return getFlags() & FlagMainSubprogram; }
    1679             : 
    1680             :   /// Check if this is reference-qualified.
    1681             :   ///
    1682             :   /// Return true if this subprogram is a C++11 reference-qualified non-static
    1683             :   /// member function (void foo() &).
    1684       20343 :   bool isLValueReference() const { return getFlags() & FlagLValueReference; }
    1685             : 
    1686             :   /// Check if this is rvalue-reference-qualified.
    1687             :   ///
    1688             :   /// Return true if this subprogram is a C++11 rvalue-reference-qualified
    1689             :   /// non-static member function (void foo() &&).
    1690       20343 :   bool isRValueReference() const { return getFlags() & FlagRValueReference; }
    1691             : 
    1692             :   /// Check if this is marked as noreturn.
    1693             :   ///
    1694             :   /// Return true if this subprogram is C++11 noreturn or C11 _Noreturn
    1695       20343 :   bool isNoReturn() const { return getFlags() & FlagNoReturn; }
    1696             : 
    1697             :   DIScopeRef getScope() const { return DIScopeRef(getRawScope()); }
    1698             : 
    1699      397906 :   StringRef getName() const { return getStringOperand(2); }
    1700      510324 :   StringRef getLinkageName() const { return getStringOperand(3); }
    1701             : 
    1702             :   DISubroutineType *getType() const {
    1703             :     return cast_or_null<DISubroutineType>(getRawType());
    1704             :   }
    1705             :   DITypeRef getContainingType() const {
    1706             :     return DITypeRef(getRawContainingType());
    1707             :   }
    1708             : 
    1709             :   DICompileUnit *getUnit() const {
    1710             :     return cast_or_null<DICompileUnit>(getRawUnit());
    1711             :   }
    1712           4 :   void replaceUnit(DICompileUnit *CU) { replaceOperandWith(5, CU); }
    1713             :   DITemplateParameterArray getTemplateParams() const {
    1714             :     return cast_or_null<MDTuple>(getRawTemplateParams());
    1715             :   }
    1716             :   DISubprogram *getDeclaration() const {
    1717             :     return cast_or_null<DISubprogram>(getRawDeclaration());
    1718             :   }
    1719             :   DILocalVariableArray getVariables() const {
    1720             :     return cast_or_null<MDTuple>(getRawVariables());
    1721             :   }
    1722             :   DITypeArray getThrownTypes() const {
    1723             :     return cast_or_null<MDTuple>(getRawThrownTypes());
    1724             :   }
    1725             : 
    1726      205044 :   Metadata *getRawScope() const { return getOperand(1); }
    1727             :   MDString *getRawName() const { return getOperandAs<MDString>(2); }
    1728             :   MDString *getRawLinkageName() const { return getOperandAs<MDString>(3); }
    1729       25762 :   Metadata *getRawType() const { return getOperand(4); }
    1730     2607116 :   Metadata *getRawUnit() const { return getOperand(5); }
    1731       70121 :   Metadata *getRawDeclaration() const { return getOperand(6); }
    1732      104777 :   Metadata *getRawVariables() const { return getOperand(7); }
    1733             :   Metadata *getRawContainingType() const {
    1734       75308 :     return getNumOperands() > 8 ? getOperandAs<Metadata>(8) : nullptr;
    1735             :   }
    1736             :   Metadata *getRawTemplateParams() const {
    1737      136194 :     return getNumOperands() > 9 ? getOperandAs<Metadata>(9) : nullptr;
    1738             :   }
    1739             :   Metadata *getRawThrownTypes() const {
    1740       95529 :     return getNumOperands() > 10 ? getOperandAs<Metadata>(10) : nullptr;
    1741             :   }
    1742             : 
    1743             :   /// Check if this subprogram describes the given function.
    1744             :   ///
    1745             :   /// FIXME: Should this be looking through bitcasts?
    1746             :   bool describes(const Function *F) const;
    1747             : 
    1748             :   static bool classof(const Metadata *MD) {
    1749      368579 :     return MD->getMetadataID() == DISubprogramKind;
    1750             :   }
    1751             : };
    1752             : 
    1753             : class DILexicalBlockBase : public DILocalScope {
    1754             : protected:
    1755             :   DILexicalBlockBase(LLVMContext &C, unsigned ID, StorageType Storage,
    1756             :                      ArrayRef<Metadata *> Ops)
    1757             :       : DILocalScope(C, ID, Storage, dwarf::DW_TAG_lexical_block, Ops) {}
    1758             :   ~DILexicalBlockBase() = default;
    1759             : 
    1760             : public:
    1761             :   DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); }
    1762             : 
    1763      133281 :   Metadata *getRawScope() const { return getOperand(1); }
    1764             : 
    1765             :   static bool classof(const Metadata *MD) {
    1766     3936755 :     return MD->getMetadataID() == DILexicalBlockKind ||
    1767             :            MD->getMetadataID() == DILexicalBlockFileKind;
    1768             :   }
    1769             : };
    1770             : 
    1771             : class DILexicalBlock : public DILexicalBlockBase {
    1772             :   friend class LLVMContextImpl;
    1773             :   friend class MDNode;
    1774             : 
    1775             :   unsigned Line;
    1776             :   uint16_t Column;
    1777             : 
    1778             :   DILexicalBlock(LLVMContext &C, StorageType Storage, unsigned Line,
    1779             :                  unsigned Column, ArrayRef<Metadata *> Ops)
    1780        5322 :       : DILexicalBlockBase(C, DILexicalBlockKind, Storage, Ops), Line(Line),
    1781        5322 :         Column(Column) {
    1782             :     assert(Column < (1u << 16) && "Expected 16-bit column");
    1783             :   }
    1784        1846 :   ~DILexicalBlock() = default;
    1785             : 
    1786             :   static DILexicalBlock *getImpl(LLVMContext &Context, DILocalScope *Scope,
    1787             :                                  DIFile *File, unsigned Line, unsigned Column,
    1788             :                                  StorageType Storage,
    1789             :                                  bool ShouldCreate = true) {
    1790             :     return getImpl(Context, static_cast<Metadata *>(Scope),
    1791             :                    static_cast<Metadata *>(File), Line, Column, Storage,
    1792          36 :                    ShouldCreate);
    1793             :   }
    1794             : 
    1795             :   static DILexicalBlock *getImpl(LLVMContext &Context, Metadata *Scope,
    1796             :                                  Metadata *File, unsigned Line, unsigned Column,
    1797             :                                  StorageType Storage, bool ShouldCreate = true);
    1798             : 
    1799          26 :   TempDILexicalBlock cloneImpl() const {
    1800             :     return getTemporary(getContext(), getScope(), getFile(), getLine(),
    1801          78 :                         getColumn());
    1802             :   }
    1803             : 
    1804             : public:
    1805             :   DEFINE_MDNODE_GET(DILexicalBlock, (DILocalScope * Scope, DIFile *File,
    1806             :                                      unsigned Line, unsigned Column),
    1807             :                     (Scope, File, Line, Column))
    1808        5290 :   DEFINE_MDNODE_GET(DILexicalBlock, (Metadata * Scope, Metadata *File,
    1809             :                                      unsigned Line, unsigned Column),
    1810             :                     (Scope, File, Line, Column))
    1811             : 
    1812           1 :   TempDILexicalBlock clone() const { return cloneImpl(); }
    1813             : 
    1814             :   unsigned getLine() const { return Line; }
    1815         842 :   unsigned getColumn() const { return Column; }
    1816             : 
    1817             :   static bool classof(const Metadata *MD) {
    1818             :     return MD->getMetadataID() == DILexicalBlockKind;
    1819             :   }
    1820             : };
    1821             : 
    1822             : class DILexicalBlockFile : public DILexicalBlockBase {
    1823             :   friend class LLVMContextImpl;
    1824             :   friend class MDNode;
    1825             : 
    1826             :   unsigned Discriminator;
    1827             : 
    1828             :   DILexicalBlockFile(LLVMContext &C, StorageType Storage,
    1829             :                      unsigned Discriminator, ArrayRef<Metadata *> Ops)
    1830         437 :       : DILexicalBlockBase(C, DILexicalBlockFileKind, Storage, Ops),
    1831         437 :         Discriminator(Discriminator) {}
    1832         427 :   ~DILexicalBlockFile() = default;
    1833             : 
    1834             :   static DILexicalBlockFile *getImpl(LLVMContext &Context, DILocalScope *Scope,
    1835             :                                      DIFile *File, unsigned Discriminator,
    1836             :                                      StorageType Storage,
    1837             :                                      bool ShouldCreate = true) {
    1838             :     return getImpl(Context, static_cast<Metadata *>(Scope),
    1839             :                    static_cast<Metadata *>(File), Discriminator, Storage,
    1840           6 :                    ShouldCreate);
    1841             :   }
    1842             : 
    1843             :   static DILexicalBlockFile *getImpl(LLVMContext &Context, Metadata *Scope,
    1844             :                                      Metadata *File, unsigned Discriminator,
    1845             :                                      StorageType Storage,
    1846             :                                      bool ShouldCreate = true);
    1847             : 
    1848           1 :   TempDILexicalBlockFile cloneImpl() const {
    1849             :     return getTemporary(getContext(), getScope(), getFile(),
    1850           2 :                         getDiscriminator());
    1851             :   }
    1852             : 
    1853             : public:
    1854             :   DEFINE_MDNODE_GET(DILexicalBlockFile, (DILocalScope * Scope, DIFile *File,
    1855             :                                          unsigned Discriminator),
    1856             :                     (Scope, File, Discriminator))
    1857         885 :   DEFINE_MDNODE_GET(DILexicalBlockFile,
    1858             :                     (Metadata * Scope, Metadata *File, unsigned Discriminator),
    1859             :                     (Scope, File, Discriminator))
    1860             : 
    1861           1 :   TempDILexicalBlockFile clone() const { return cloneImpl(); }
    1862             : 
    1863             :   // TODO: Remove these once they're gone from DILexicalBlockBase.
    1864             :   unsigned getLine() const = delete;
    1865             :   unsigned getColumn() const = delete;
    1866             : 
    1867             :   unsigned getDiscriminator() const { return Discriminator; }
    1868             : 
    1869             :   static bool classof(const Metadata *MD) {
    1870     3466949 :     return MD->getMetadataID() == DILexicalBlockFileKind;
    1871             :   }
    1872             : };
    1873             : 
    1874             : unsigned DILocation::getDiscriminator() const {
    1875             :   if (auto *F = dyn_cast<DILexicalBlockFile>(getScope()))
    1876         343 :     return F->getDiscriminator();
    1877             :   return 0;
    1878             : }
    1879             : 
    1880             : const DILocation *
    1881         298 : DILocation::cloneWithDiscriminator(unsigned Discriminator) const {
    1882             :   DIScope *Scope = getScope();
    1883             :   // Skip all parent DILexicalBlockFile that already have a discriminator
    1884             :   // assigned. We do not want to have nested DILexicalBlockFiles that have
    1885             :   // mutliple discriminators because only the leaf DILexicalBlockFile's
    1886             :   // dominator will be used.
    1887             :   for (auto *LBF = dyn_cast<DILexicalBlockFile>(Scope);
    1888         357 :        LBF && LBF->getDiscriminator() != 0;
    1889             :        LBF = dyn_cast<DILexicalBlockFile>(Scope))
    1890             :     Scope = LBF->getScope();
    1891             :   DILexicalBlockFile *NewScope =
    1892             :       DILexicalBlockFile::get(getContext(), Scope, getFile(), Discriminator);
    1893         596 :   return DILocation::get(getContext(), getLine(), getColumn(), NewScope,
    1894         298 :                          getInlinedAt());
    1895             : }
    1896             : 
    1897        1182 : unsigned DILocation::getBaseDiscriminator() const {
    1898        1182 :   return getBaseDiscriminatorFromDiscriminator(getDiscriminator());
    1899             : }
    1900             : 
    1901         110 : unsigned DILocation::getDuplicationFactor() const {
    1902         110 :   return getDuplicationFactorFromDiscriminator(getDiscriminator());
    1903             : }
    1904             : 
    1905         110 : unsigned DILocation::getCopyIdentifier() const {
    1906         110 :   return getCopyIdentifierFromDiscriminator(getDiscriminator());
    1907             : }
    1908             : 
    1909         188 : const DILocation *DILocation::setBaseDiscriminator(unsigned D) const {
    1910         188 :   if (D == 0)
    1911             :     return this;
    1912             :   else
    1913         188 :     return cloneWithDiscriminator(getPrefixEncodingFromUnsigned(D) << 1);
    1914             : }
    1915             : 
    1916         110 : const DILocation *DILocation::cloneWithDuplicationFactor(unsigned DF) const {
    1917         110 :   DF *= getDuplicationFactor();
    1918         110 :   if (DF <= 1)
    1919             :     return this;
    1920             : 
    1921         110 :   unsigned BD = getBaseDiscriminator();
    1922         110 :   unsigned CI = getCopyIdentifier() << (DF > 0x1f ? 14 : 7);
    1923         110 :   unsigned D = CI | (getPrefixEncodingFromUnsigned(DF) << 1);
    1924             : 
    1925         110 :   if (BD == 0)
    1926         110 :     D = (D << 1) | 1;
    1927             :   else
    1928           0 :     D = (D << (BD > 0x1f ? 14 : 7)) | (getPrefixEncodingFromUnsigned(BD) << 1);
    1929             : 
    1930         110 :   return cloneWithDiscriminator(D);
    1931             : }
    1932             : 
    1933             : class DINamespace : public DIScope {
    1934             :   friend class LLVMContextImpl;
    1935             :   friend class MDNode;
    1936             : 
    1937             :   unsigned ExportSymbols : 1;
    1938             : 
    1939             :   DINamespace(LLVMContext &Context, StorageType Storage, bool ExportSymbols,
    1940             :               ArrayRef<Metadata *> Ops)
    1941         432 :       : DIScope(Context, DINamespaceKind, Storage, dwarf::DW_TAG_namespace,
    1942             :                 Ops),
    1943         432 :         ExportSymbols(ExportSymbols) {}
    1944         226 :   ~DINamespace() = default;
    1945             : 
    1946        1522 :   static DINamespace *getImpl(LLVMContext &Context, DIScope *Scope,
    1947             :                               StringRef Name, bool ExportSymbols,
    1948             :                               StorageType Storage, bool ShouldCreate = true) {
    1949        1522 :     return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
    1950        1522 :                    ExportSymbols, Storage, ShouldCreate);
    1951             :   }
    1952             :   static DINamespace *getImpl(LLVMContext &Context, Metadata *Scope,
    1953             :                               MDString *Name, bool ExportSymbols,
    1954             :                               StorageType Storage, bool ShouldCreate = true);
    1955             : 
    1956           1 :   TempDINamespace cloneImpl() const {
    1957             :     return getTemporary(getContext(), getScope(), getName(),
    1958           2 :                         getExportSymbols());
    1959             :   }
    1960             : 
    1961             : public:
    1962        1522 :   DEFINE_MDNODE_GET(DINamespace,
    1963             :                     (DIScope *Scope, StringRef Name, bool ExportSymbols),
    1964             :                     (Scope, Name, ExportSymbols))
    1965         129 :   DEFINE_MDNODE_GET(DINamespace,
    1966             :                     (Metadata *Scope, MDString *Name, bool ExportSymbols),
    1967             :                     (Scope, Name, ExportSymbols))
    1968             : 
    1969           1 :   TempDINamespace clone() const { return cloneImpl(); }
    1970             : 
    1971        2098 :   bool getExportSymbols() const { return ExportSymbols; }
    1972             :   DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
    1973        6173 :   StringRef getName() const { return getStringOperand(2); }
    1974             : 
    1975       55677 :   Metadata *getRawScope() const { return getOperand(1); }
    1976             :   MDString *getRawName() const { return getOperandAs<MDString>(2); }
    1977             : 
    1978             :   static bool classof(const Metadata *MD) {
    1979        9071 :     return MD->getMetadataID() == DINamespaceKind;
    1980             :   }
    1981             : };
    1982             : 
    1983             : /// A (clang) module that has been imported by the compile unit.
    1984             : ///
    1985             : class DIModule : public DIScope {
    1986             :   friend class LLVMContextImpl;
    1987             :   friend class MDNode;
    1988             : 
    1989             :   DIModule(LLVMContext &Context, StorageType Storage, ArrayRef<Metadata *> Ops)
    1990             :       : DIScope(Context, DIModuleKind, Storage, dwarf::DW_TAG_module, Ops) {}
    1991          88 :   ~DIModule() = default;
    1992             : 
    1993          62 :   static DIModule *getImpl(LLVMContext &Context, DIScope *Scope,
    1994             :                            StringRef Name, StringRef ConfigurationMacros,
    1995             :                            StringRef IncludePath, StringRef ISysRoot,
    1996             :                            StorageType Storage, bool ShouldCreate = true) {
    1997          62 :     return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
    1998             :                    getCanonicalMDString(Context, ConfigurationMacros),
    1999             :                    getCanonicalMDString(Context, IncludePath),
    2000             :                    getCanonicalMDString(Context, ISysRoot),
    2001          62 :                    Storage, ShouldCreate);
    2002             :   }
    2003             :   static DIModule *getImpl(LLVMContext &Context, Metadata *Scope,
    2004             :                            MDString *Name, MDString *ConfigurationMacros,
    2005             :                            MDString *IncludePath, MDString *ISysRoot,
    2006             :                            StorageType Storage, bool ShouldCreate = true);
    2007             : 
    2008           1 :   TempDIModule cloneImpl() const {
    2009             :     return getTemporary(getContext(), getScope(), getName(),
    2010             :                         getConfigurationMacros(), getIncludePath(),
    2011           1 :                         getISysRoot());
    2012             :   }
    2013             : 
    2014             : public:
    2015          62 :   DEFINE_MDNODE_GET(DIModule, (DIScope *Scope, StringRef Name,
    2016             :                                StringRef ConfigurationMacros, StringRef IncludePath,
    2017             :                                StringRef ISysRoot),
    2018             :                     (Scope, Name, ConfigurationMacros, IncludePath, ISysRoot))
    2019          33 :   DEFINE_MDNODE_GET(DIModule,
    2020             :                     (Metadata *Scope, MDString *Name, MDString *ConfigurationMacros,
    2021             :                      MDString *IncludePath, MDString *ISysRoot),
    2022             :                     (Scope, Name, ConfigurationMacros, IncludePath, ISysRoot))
    2023             : 
    2024           1 :   TempDIModule clone() const { return cloneImpl(); }
    2025             : 
    2026             :   DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
    2027         297 :   StringRef getName() const { return getStringOperand(1); }
    2028          75 :   StringRef getConfigurationMacros() const { return getStringOperand(2); }
    2029          89 :   StringRef getIncludePath() const { return getStringOperand(3); }
    2030          95 :   StringRef getISysRoot() const { return getStringOperand(4); }
    2031             : 
    2032         397 :   Metadata *getRawScope() const { return getOperand(0); }
    2033             :   MDString *getRawName() const { return getOperandAs<MDString>(1); }
    2034             :   MDString *getRawConfigurationMacros() const { return getOperandAs<MDString>(2); }
    2035             :   MDString *getRawIncludePath() const { return getOperandAs<MDString>(3); }
    2036             :   MDString *getRawISysRoot() const { return getOperandAs<MDString>(4); }
    2037             : 
    2038             :   static bool classof(const Metadata *MD) {
    2039             :     return MD->getMetadataID() == DIModuleKind;
    2040             :   }
    2041             : };
    2042             : 
    2043             : /// Base class for template parameters.
    2044             : class DITemplateParameter : public DINode {
    2045             : protected:
    2046             :   DITemplateParameter(LLVMContext &Context, unsigned ID, StorageType Storage,
    2047             :                       unsigned Tag, ArrayRef<Metadata *> Ops)
    2048             :       : DINode(Context, ID, Storage, Tag, Ops) {}
    2049             :   ~DITemplateParameter() = default;
    2050             : 
    2051             : public:
    2052       12038 :   StringRef getName() const { return getStringOperand(0); }
    2053             :   DITypeRef getType() const { return DITypeRef(getRawType()); }
    2054             : 
    2055             :   MDString *getRawName() const { return getOperandAs<MDString>(0); }
    2056       20171 :   Metadata *getRawType() const { return getOperand(1); }
    2057             : 
    2058             :   static bool classof(const Metadata *MD) {
    2059        2090 :     return MD->getMetadataID() == DITemplateTypeParameterKind ||
    2060             :            MD->getMetadataID() == DITemplateValueParameterKind;
    2061             :   }
    2062             : };
    2063             : 
    2064             : class DITemplateTypeParameter : public DITemplateParameter {
    2065             :   friend class LLVMContextImpl;
    2066             :   friend class MDNode;
    2067             : 
    2068             :   DITemplateTypeParameter(LLVMContext &Context, StorageType Storage,
    2069             :                           ArrayRef<Metadata *> Ops)
    2070             :       : DITemplateParameter(Context, DITemplateTypeParameterKind, Storage,
    2071             :                             dwarf::DW_TAG_template_type_parameter, Ops) {}
    2072         304 :   ~DITemplateTypeParameter() = default;
    2073             : 
    2074        6883 :   static DITemplateTypeParameter *getImpl(LLVMContext &Context, StringRef Name,
    2075             :                                           DITypeRef Type, StorageType Storage,
    2076             :                                           bool ShouldCreate = true) {
    2077        6883 :     return getImpl(Context, getCanonicalMDString(Context, Name), Type, Storage,
    2078        6883 :                    ShouldCreate);
    2079             :   }
    2080             :   static DITemplateTypeParameter *getImpl(LLVMContext &Context, MDString *Name,
    2081             :                                           Metadata *Type, StorageType Storage,
    2082             :                                           bool ShouldCreate = true);
    2083             : 
    2084           2 :   TempDITemplateTypeParameter cloneImpl() const {
    2085           2 :     return getTemporary(getContext(), getName(), getType());
    2086             :   }
    2087             : 
    2088             : public:
    2089        6883 :   DEFINE_MDNODE_GET(DITemplateTypeParameter, (StringRef Name, DITypeRef Type),
    2090             :                     (Name, Type))
    2091          77 :   DEFINE_MDNODE_GET(DITemplateTypeParameter, (MDString * Name, Metadata *Type),
    2092             :                     (Name, Type))
    2093             : 
    2094           1 :   TempDITemplateTypeParameter clone() const { return cloneImpl(); }
    2095             : 
    2096             :   static bool classof(const Metadata *MD) {
    2097        6223 :     return MD->getMetadataID() == DITemplateTypeParameterKind;
    2098             :   }
    2099             : };
    2100             : 
    2101             : class DITemplateValueParameter : public DITemplateParameter {
    2102             :   friend class LLVMContextImpl;
    2103             :   friend class MDNode;
    2104             : 
    2105             :   DITemplateValueParameter(LLVMContext &Context, StorageType Storage,
    2106             :                            unsigned Tag, ArrayRef<Metadata *> Ops)
    2107             :       : DITemplateParameter(Context, DITemplateValueParameterKind, Storage, Tag,
    2108             :                             Ops) {}
    2109         346 :   ~DITemplateValueParameter() = default;
    2110             : 
    2111        1765 :   static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
    2112             :                                            StringRef Name, DITypeRef Type,
    2113             :                                            Metadata *Value, StorageType Storage,
    2114             :                                            bool ShouldCreate = true) {
    2115        1765 :     return getImpl(Context, Tag, getCanonicalMDString(Context, Name), Type,
    2116        1765 :                    Value, Storage, ShouldCreate);
    2117             :   }
    2118             :   static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
    2119             :                                            MDString *Name, Metadata *Type,
    2120             :                                            Metadata *Value, StorageType Storage,
    2121             :                                            bool ShouldCreate = true);
    2122             : 
    2123           2 :   TempDITemplateValueParameter cloneImpl() const {
    2124             :     return getTemporary(getContext(), getTag(), getName(), getType(),
    2125           4 :                         getValue());
    2126             :   }
    2127             : 
    2128             : public:
    2129        1765 :   DEFINE_MDNODE_GET(DITemplateValueParameter, (unsigned Tag, StringRef Name,
    2130             :                                                DITypeRef Type, Metadata *Value),
    2131             :                     (Tag, Name, Type, Value))
    2132          88 :   DEFINE_MDNODE_GET(DITemplateValueParameter, (unsigned Tag, MDString *Name,
    2133             :                                                Metadata *Type, Metadata *Value),
    2134             :                     (Tag, Name, Type, Value))
    2135             : 
    2136           1 :   TempDITemplateValueParameter clone() const { return cloneImpl(); }
    2137             : 
    2138        4365 :   Metadata *getValue() const { return getOperand(2); }
    2139             : 
    2140             :   static bool classof(const Metadata *MD) {
    2141             :     return MD->getMetadataID() == DITemplateValueParameterKind;
    2142             :   }
    2143             : };
    2144             : 
    2145             : /// Base class for variables.
    2146             : class DIVariable : public DINode {
    2147             :   unsigned Line;
    2148             :   uint32_t AlignInBits;
    2149             : 
    2150             : protected:
    2151             :   DIVariable(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Line,
    2152             :              ArrayRef<Metadata *> Ops, uint32_t AlignInBits = 0)
    2153       18122 :       : DINode(C, ID, Storage, dwarf::DW_TAG_variable, Ops), Line(Line),
    2154       18122 :         AlignInBits(AlignInBits) {}
    2155             :   ~DIVariable() = default;
    2156             : 
    2157             : public:
    2158             :   unsigned getLine() const { return Line; }
    2159             :   DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
    2160       23388 :   StringRef getName() const { return getStringOperand(1); }
    2161             :   DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
    2162             :   DITypeRef getType() const { return DITypeRef(getRawType()); }
    2163             :   uint32_t getAlignInBits() const { return AlignInBits; }
    2164        9210 :   uint32_t getAlignInBytes() const { return getAlignInBits() / CHAR_BIT; }
    2165             :   /// Determines the size of the variable's type.
    2166             :   Optional<uint64_t> getSizeInBits() const;
    2167             : 
    2168           1 :   StringRef getFilename() const {
    2169             :     if (auto *F = getFile())
    2170             :       return F->getFilename();
    2171           0 :     return "";
    2172             :   }
    2173             : 
    2174           1 :   StringRef getDirectory() const {
    2175             :     if (auto *F = getFile())
    2176             :       return F->getDirectory();
    2177           0 :     return "";
    2178             :   }
    2179             : 
    2180      132661 :   Metadata *getRawScope() const { return getOperand(0); }
    2181             :   MDString *getRawName() const { return getOperandAs<MDString>(1); }
    2182        9195 :   Metadata *getRawFile() const { return getOperand(2); }
    2183       73985 :   Metadata *getRawType() const { return getOperand(3); }
    2184             : 
    2185             :   static bool classof(const Metadata *MD) {
    2186         152 :     return MD->getMetadataID() == DILocalVariableKind ||
    2187             :            MD->getMetadataID() == DIGlobalVariableKind;
    2188             :   }
    2189             : };
    2190             : 
    2191             : /// DWARF expression.
    2192             : ///
    2193             : /// This is (almost) a DWARF expression that modifies the location of a
    2194             : /// variable, or the location of a single piece of a variable, or (when using
    2195             : /// DW_OP_stack_value) is the constant variable value.
    2196             : ///
    2197             : /// TODO: Co-allocate the expression elements.
    2198             : /// TODO: Separate from MDNode, or otherwise drop Distinct and Temporary
    2199             : /// storage types.
    2200             : class DIExpression : public MDNode {
    2201             :   friend class LLVMContextImpl;
    2202             :   friend class MDNode;
    2203             : 
    2204             :   std::vector<uint64_t> Elements;
    2205             : 
    2206        2514 :   DIExpression(LLVMContext &C, StorageType Storage, ArrayRef<uint64_t> Elements)
    2207        2514 :       : MDNode(C, DIExpressionKind, Storage, None),
    2208        7542 :         Elements(Elements.begin(), Elements.end()) {}
    2209        3546 :   ~DIExpression() = default;
    2210             : 
    2211             :   static DIExpression *getImpl(LLVMContext &Context,
    2212             :                                ArrayRef<uint64_t> Elements, StorageType Storage,
    2213             :                                bool ShouldCreate = true);
    2214             : 
    2215           1 :   TempDIExpression cloneImpl() const {
    2216           1 :     return getTemporary(getContext(), getElements());
    2217             :   }
    2218             : 
    2219             : public:
    2220       24442 :   DEFINE_MDNODE_GET(DIExpression, (ArrayRef<uint64_t> Elements), (Elements))
    2221             : 
    2222           1 :   TempDIExpression clone() const { return cloneImpl(); }
    2223             : 
    2224             :   ArrayRef<uint64_t> getElements() const { return Elements; }
    2225             : 
    2226       29152 :   unsigned getNumElements() const { return Elements.size(); }
    2227             : 
    2228             :   uint64_t getElement(unsigned I) const {
    2229             :     assert(I < Elements.size() && "Index out of range");
    2230          62 :     return Elements[I];
    2231             :   }
    2232             : 
    2233             :   /// Determine whether this represents a standalone constant value.
    2234             :   bool isConstant() const;
    2235             : 
    2236             :   using element_iterator = ArrayRef<uint64_t>::iterator;
    2237             : 
    2238             :   element_iterator elements_begin() const { return getElements().begin(); }
    2239             :   element_iterator elements_end() const { return getElements().end(); }
    2240             : 
    2241             :   /// A lightweight wrapper around an expression operand.
    2242             :   ///
    2243             :   /// TODO: Store arguments directly and change \a DIExpression to store a
    2244             :   /// range of these.
    2245             :   class ExprOperand {
    2246             :     const uint64_t *Op = nullptr;
    2247             : 
    2248             :   public:
    2249       40259 :     ExprOperand() = default;
    2250        1008 :     explicit ExprOperand(const uint64_t *Op) : Op(Op) {}
    2251             : 
    2252      325652 :     const uint64_t *get() const { return Op; }
    2253             : 
    2254             :     /// Get the operand code.
    2255      195920 :     uint64_t getOp() const { return *Op; }
    2256             : 
    2257             :     /// Get an argument to the operand.
    2258             :     ///
    2259             :     /// Never returns the operand itself.
    2260       13943 :     uint64_t getArg(unsigned I) const { return Op[I + 1]; }
    2261             : 
    2262        4721 :     unsigned getNumArgs() const { return getSize() - 1; }
    2263             : 
    2264             :     /// Return the size of the operand.
    2265             :     ///
    2266             :     /// Return the number of elements in the operand (1 + args).
    2267             :     unsigned getSize() const;
    2268             :   };
    2269             : 
    2270             :   /// An iterator for expression operands.
    2271             :   class expr_op_iterator
    2272             :       : public std::iterator<std::input_iterator_tag, ExprOperand> {
    2273             :     ExprOperand Op;
    2274             : 
    2275             :   public:
    2276             :     expr_op_iterator() = default;
    2277             :     explicit expr_op_iterator(element_iterator I) : Op(I) {}
    2278             : 
    2279             :     element_iterator getBase() const { return Op.get(); }
    2280             :     const ExprOperand &operator*() const { return Op; }
    2281             :     const ExprOperand *operator->() const { return &Op; }
    2282             : 
    2283             :     expr_op_iterator &operator++() {
    2284             :       increment();
    2285             :       return *this;
    2286             :     }
    2287             :     expr_op_iterator operator++(int) {
    2288             :       expr_op_iterator T(*this);
    2289             :       increment();
    2290             :       return T;
    2291             :     }
    2292             : 
    2293             :     /// Get the next iterator.
    2294             :     ///
    2295             :     /// \a std::next() doesn't work because this is technically an
    2296             :     /// input_iterator, but it's a perfectly valid operation.  This is an
    2297             :     /// accessor to provide the same functionality.
    2298         222 :     expr_op_iterator getNext() const { return ++expr_op_iterator(*this); }
    2299             : 
    2300             :     bool operator==(const expr_op_iterator &X) const {
    2301             :       return getBase() == X.getBase();
    2302             :     }
    2303             :     bool operator!=(const expr_op_iterator &X) const {
    2304             :       return getBase() != X.getBase();
    2305             :     }
    2306             : 
    2307             :   private:
    2308       82984 :     void increment() { Op = ExprOperand(getBase() + Op.getSize()); }
    2309             :   };
    2310             : 
    2311             :   /// Visit the elements via ExprOperand wrappers.
    2312             :   ///
    2313             :   /// These range iterators visit elements through \a ExprOperand wrappers.
    2314             :   /// This is not guaranteed to be a valid range unless \a isValid() gives \c
    2315             :   /// true.
    2316             :   ///
    2317             :   /// \pre \a isValid() gives \c true.
    2318             :   /// @{
    2319             :   expr_op_iterator expr_op_begin() const {
    2320             :     return expr_op_iterator(elements_begin());
    2321             :   }
    2322             :   expr_op_iterator expr_op_end() const {
    2323             :     return expr_op_iterator(elements_end());
    2324             :   }
    2325             :   iterator_range<expr_op_iterator> expr_ops() const {
    2326             :     return {expr_op_begin(), expr_op_end()};
    2327             :   }
    2328             :   /// @}
    2329             : 
    2330             :   bool isValid() const;
    2331             : 
    2332             :   static bool classof(const Metadata *MD) {
    2333      355438 :     return MD->getMetadataID() == DIExpressionKind;
    2334             :   }
    2335             : 
    2336             :   /// Return whether the first element a DW_OP_deref.
    2337             :   bool startsWithDeref() const {
    2338           8 :     return getNumElements() > 0 && getElement(0) == dwarf::DW_OP_deref;
    2339             :   }
    2340             : 
    2341             :   /// Holds the characteristics of one fragment of a larger variable.
    2342             :   struct FragmentInfo {
    2343             :     uint64_t SizeInBits;
    2344             :     uint64_t OffsetInBits;
    2345             :   };
    2346             : 
    2347             :   /// Retrieve the details of this fragment expression.
    2348             :   static Optional<FragmentInfo> getFragmentInfo(expr_op_iterator Start,
    2349             :                                                 expr_op_iterator End);
    2350             : 
    2351             :   /// Retrieve the details of this fragment expression.
    2352             :   Optional<FragmentInfo> getFragmentInfo() const {
    2353      110618 :     return getFragmentInfo(expr_op_begin(), expr_op_end());
    2354             :   }
    2355             : 
    2356             :   /// Return whether this is a piece of an aggregate variable.
    2357       91515 :   bool isFragment() const { return getFragmentInfo().hasValue(); }
    2358             : 
    2359             :   /// Append \p Ops with operations to apply the \p Offset.
    2360             :   static void appendOffset(SmallVectorImpl<uint64_t> &Ops, int64_t Offset);
    2361             : 
    2362             :   /// If this is a constant offset, extract it. If there is no expression,
    2363             :   /// return true with an offset of zero.
    2364             :   bool extractIfOffset(int64_t &Offset) const;
    2365             : 
    2366             :   /// Constants for DIExpression::prepend.
    2367             :   enum { NoDeref = false, WithDeref = true, WithStackValue = true };
    2368             : 
    2369             :   /// Prepend \p DIExpr with a deref and offset operation and optionally turn it
    2370             :   /// into a stack value.
    2371             :   static DIExpression *prepend(const DIExpression *DIExpr, bool DerefBefore,
    2372             :                                int64_t Offset = 0, bool DerefAfter = false,
    2373             :                                bool StackValue = false);
    2374             : 
    2375             :   /// Prepend \p DIExpr with the given opcodes and optionally turn it into a
    2376             :   /// stack value.
    2377             :   static DIExpression *doPrepend(const DIExpression *DIExpr,
    2378             :                                  SmallVectorImpl<uint64_t> &Ops,
    2379             :                                  bool StackValue = false);
    2380             : 
    2381             :   /// Create a DIExpression to describe one part of an aggregate variable that
    2382             :   /// is fragmented across multiple Values. The DW_OP_LLVM_fragment operation
    2383             :   /// will be appended to the elements of \c Expr. If \c Expr already contains
    2384             :   /// a \c DW_OP_LLVM_fragment \c OffsetInBits is interpreted as an offset
    2385             :   /// into the existing fragment.
    2386             :   ///
    2387             :   /// \param OffsetInBits Offset of the piece in bits.
    2388             :   /// \param SizeInBits   Size of the piece in bits.
    2389             :   /// \return             Creating a fragment expression may fail if \c Expr
    2390             :   ///                     contains arithmetic operations that would be truncated.
    2391             :   static Optional<DIExpression *>
    2392             :   createFragmentExpression(const DIExpression *Expr, unsigned OffsetInBits,
    2393             :                            unsigned SizeInBits);
    2394             : };
    2395             : 
    2396             : /// Global variables.
    2397             : ///
    2398             : /// TODO: Remove DisplayName.  It's always equal to Name.
    2399             : class DIGlobalVariable : public DIVariable {
    2400             :   friend class LLVMContextImpl;
    2401             :   friend class MDNode;
    2402             : 
    2403             :   bool IsLocalToUnit;
    2404             :   bool IsDefinition;
    2405             : 
    2406             :   DIGlobalVariable(LLVMContext &C, StorageType Storage, unsigned Line,
    2407             :                    bool IsLocalToUnit, bool IsDefinition, uint32_t AlignInBits,
    2408             :                    ArrayRef<Metadata *> Ops)
    2409        1319 :       : DIVariable(C, DIGlobalVariableKind, Storage, Line, Ops, AlignInBits),
    2410        1319 :         IsLocalToUnit(IsLocalToUnit), IsDefinition(IsDefinition) {}
    2411        1105 :   ~DIGlobalVariable() = default;
    2412             : 
    2413         612 :   static DIGlobalVariable *getImpl(LLVMContext &Context, DIScope *Scope,
    2414             :                                    StringRef Name, StringRef LinkageName,
    2415             :                                    DIFile *File, unsigned Line, DITypeRef Type,
    2416             :                                    bool IsLocalToUnit, bool IsDefinition,
    2417             :                                    DIDerivedType *StaticDataMemberDeclaration,
    2418             :                                    uint32_t AlignInBits, StorageType Storage,
    2419             :                                    bool ShouldCreate = true) {
    2420         612 :     return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
    2421             :                    getCanonicalMDString(Context, LinkageName), File, Line, Type,
    2422             :                    IsLocalToUnit, IsDefinition, StaticDataMemberDeclaration,
    2423         612 :                    AlignInBits, Storage, ShouldCreate);
    2424             :   }
    2425             :   static DIGlobalVariable *
    2426             :   getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
    2427             :           MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
    2428             :           bool IsLocalToUnit, bool IsDefinition,
    2429             :           Metadata *StaticDataMemberDeclaration, uint32_t AlignInBits,
    2430             :           StorageType Storage, bool ShouldCreate = true);
    2431             : 
    2432           2 :   TempDIGlobalVariable cloneImpl() const {
    2433             :     return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
    2434           2 :                         getFile(), getLine(), getType(), isLocalToUnit(),
    2435           2 :                         isDefinition(), getStaticDataMemberDeclaration(),
    2436          10 :                         getAlignInBits());
    2437             :   }
    2438             : 
    2439             : public:
    2440         612 :   DEFINE_MDNODE_GET(DIGlobalVariable,
    2441             :                     (DIScope * Scope, StringRef Name, StringRef LinkageName,
    2442             :                      DIFile *File, unsigned Line, DITypeRef Type,
    2443             :                      bool IsLocalToUnit, bool IsDefinition,
    2444             :                      DIDerivedType *StaticDataMemberDeclaration,
    2445             :                      uint32_t AlignInBits),
    2446             :                     (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
    2447             :                      IsDefinition, StaticDataMemberDeclaration, AlignInBits))
    2448         708 :   DEFINE_MDNODE_GET(DIGlobalVariable,
    2449             :                     (Metadata * Scope, MDString *Name, MDString *LinkageName,
    2450             :                      Metadata *File, unsigned Line, Metadata *Type,
    2451             :                      bool IsLocalToUnit, bool IsDefinition,
    2452             :                      Metadata *StaticDataMemberDeclaration,
    2453             :                      uint32_t AlignInBits),
    2454             :                     (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
    2455             :                      IsDefinition, StaticDataMemberDeclaration, AlignInBits))
    2456             : 
    2457           1 :   TempDIGlobalVariable clone() const { return cloneImpl(); }
    2458             : 
    2459             :   bool isLocalToUnit() const { return IsLocalToUnit; }
    2460             :   bool isDefinition() const { return IsDefinition; }
    2461         505 :   StringRef getDisplayName() const { return getStringOperand(4); }
    2462        1601 :   StringRef getLinkageName() const { return getStringOperand(5); }
    2463             :   DIDerivedType *getStaticDataMemberDeclaration() const {
    2464             :     return cast_or_null<DIDerivedType>(getRawStaticDataMemberDeclaration());
    2465             :   }
    2466             : 
    2467             :   MDString *getRawLinkageName() const { return getOperandAs<MDString>(5); }
    2468       11419 :   Metadata *getRawStaticDataMemberDeclaration() const { return getOperand(6); }
    2469             : 
    2470             :   static bool classof(const Metadata *MD) {
    2471          17 :     return MD->getMetadataID() == DIGlobalVariableKind;
    2472             :   }
    2473             : };
    2474             : 
    2475             : /// Local variable.
    2476             : ///
    2477             : /// TODO: Split up flags.
    2478             : class DILocalVariable : public DIVariable {
    2479             :   friend class LLVMContextImpl;
    2480             :   friend class MDNode;
    2481             : 
    2482             :   unsigned Arg : 16;
    2483             :   DIFlags Flags;
    2484             : 
    2485             :   DILocalVariable(LLVMContext &C, StorageType Storage, unsigned Line,
    2486             :                   unsigned Arg, DIFlags Flags, uint32_t AlignInBits,
    2487             :                   ArrayRef<Metadata *> Ops)
    2488       16803 :       : DIVariable(C, DILocalVariableKind, Storage, Line, Ops, AlignInBits),
    2489       16803 :         Arg(Arg), Flags(Flags) {
    2490             :     assert(Arg < (1 << 16) && "DILocalVariable: Arg out of range");
    2491             :   }
    2492        6210 :   ~DILocalVariable() = default;
    2493             : 
    2494       14647 :   static DILocalVariable *getImpl(LLVMContext &Context, DIScope *Scope,
    2495             :                                   StringRef Name, DIFile *File, unsigned Line,
    2496             :                                   DITypeRef Type, unsigned Arg, DIFlags Flags,
    2497             :                                   uint32_t AlignInBits, StorageType Storage,
    2498             :                                   bool ShouldCreate = true) {
    2499       14647 :     return getImpl(Context, Scope, getCanonicalMDString(Context, Name), File,
    2500       14647 :                    Line, Type, Arg, Flags, AlignInBits, Storage, ShouldCreate);
    2501             :   }
    2502             :   static DILocalVariable *getImpl(LLVMContext &Context, Metadata *Scope,
    2503             :                                   MDString *Name, Metadata *File, unsigned Line,
    2504             :                                   Metadata *Type, unsigned Arg, DIFlags Flags,
    2505             :                                   uint32_t AlignInBits, StorageType Storage,
    2506             :                                   bool ShouldCreate = true);
    2507             : 
    2508          24 :   TempDILocalVariable cloneImpl() const {
    2509             :     return getTemporary(getContext(), getScope(), getName(), getFile(),
    2510             :                         getLine(), getType(), getArg(), getFlags(),
    2511          72 :                         getAlignInBits());
    2512             :   }
    2513             : 
    2514             : public:
    2515       14647 :   DEFINE_MDNODE_GET(DILocalVariable,
    2516             :                     (DILocalScope * Scope, StringRef Name, DIFile *File,
    2517             :                      unsigned Line, DITypeRef Type, unsigned Arg,
    2518             :                      DIFlags Flags, uint32_t AlignInBits),
    2519             :                     (Scope, Name, File, Line, Type, Arg, Flags, AlignInBits))
    2520        2251 :   DEFINE_MDNODE_GET(DILocalVariable,
    2521             :                     (Metadata * Scope, MDString *Name, Metadata *File,
    2522             :                      unsigned Line, Metadata *Type, unsigned Arg,
    2523             :                      DIFlags Flags, uint32_t AlignInBits),
    2524             :                     (Scope, Name, File, Line, Type, Arg, Flags, AlignInBits))
    2525             : 
    2526           1 :   TempDILocalVariable clone() const { return cloneImpl(); }
    2527             : 
    2528             :   /// Get the local scope for this variable.
    2529             :   ///
    2530             :   /// Variables must be defined in a local scope.
    2531             :   DILocalScope *getScope() const {
    2532             :     return cast<DILocalScope>(DIVariable::getScope());
    2533             :   }
    2534             : 
    2535       34247 :   bool isParameter() const { return Arg; }
    2536       70612 :   unsigned getArg() const { return Arg; }
    2537             :   DIFlags getFlags() const { return Flags; }
    2538             : 
    2539        9063 :   bool isArtificial() const { return getFlags() & FlagArtificial; }
    2540       19482 :   bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
    2541             : 
    2542             :   /// Check that a location is valid for this variable.
    2543             :   ///
    2544             :   /// Check that \c DL exists, is in the same subprogram, and has the same
    2545             :   /// inlined-at location as \c this.  (Otherwise, it's not a valid attachment
    2546             :   /// to a \a DbgInfoIntrinsic.)
    2547             :   bool isValidLocationForIntrinsic(const DILocation *DL) const {
    2548             :     return DL && getScope()->getSubprogram() == DL->getScope()->getSubprogram();
    2549             :   }
    2550             : 
    2551             :   static bool classof(const Metadata *MD) {
    2552          96 :     return MD->getMetadataID() == DILocalVariableKind;
    2553             :   }
    2554             : };
    2555             : 
    2556             : class DIObjCProperty : public DINode {
    2557             :   friend class LLVMContextImpl;
    2558             :   friend class MDNode;
    2559             : 
    2560             :   unsigned Line;
    2561             :   unsigned Attributes;
    2562             : 
    2563             :   DIObjCProperty(LLVMContext &C, StorageType Storage, unsigned Line,
    2564             :                  unsigned Attributes, ArrayRef<Metadata *> Ops)
    2565          90 :       : DINode(C, DIObjCPropertyKind, Storage, dwarf::DW_TAG_APPLE_property,
    2566             :                Ops),
    2567          90 :         Line(Line), Attributes(Attributes) {}
    2568          90 :   ~DIObjCProperty() = default;
    2569             : 
    2570             :   static DIObjCProperty *
    2571          83 :   getImpl(LLVMContext &Context, StringRef Name, DIFile *File, unsigned Line,
    2572             :           StringRef GetterName, StringRef SetterName, unsigned Attributes,
    2573             :           DITypeRef Type, StorageType Storage, bool ShouldCreate = true) {
    2574          83 :     return getImpl(Context, getCanonicalMDString(Context, Name), File, Line,
    2575             :                    getCanonicalMDString(Context, GetterName),
    2576             :                    getCanonicalMDString(Context, SetterName), Attributes, Type,
    2577          83 :                    Storage, ShouldCreate);
    2578             :   }
    2579             :   static DIObjCProperty *getImpl(LLVMContext &Context, MDString *Name,
    2580             :                                  Metadata *File, unsigned Line,
    2581             :                                  MDString *GetterName, MDString *SetterName,
    2582             :                                  unsigned Attributes, Metadata *Type,
    2583             :                                  StorageType Storage, bool ShouldCreate = true);
    2584             : 
    2585           1 :   TempDIObjCProperty cloneImpl() const {
    2586             :     return getTemporary(getContext(), getName(), getFile(), getLine(),
    2587             :                         getGetterName(), getSetterName(), getAttributes(),
    2588           3 :                         getType());
    2589             :   }
    2590             : 
    2591             : public:
    2592          83 :   DEFINE_MDNODE_GET(DIObjCProperty,
    2593             :                     (StringRef Name, DIFile *File, unsigned Line,
    2594             :                      StringRef GetterName, StringRef SetterName,
    2595             :                      unsigned Attributes, DITypeRef Type),
    2596             :                     (Name, File, Line, GetterName, SetterName, Attributes,
    2597             :                      Type))
    2598          39 :   DEFINE_MDNODE_GET(DIObjCProperty,
    2599             :                     (MDString * Name, Metadata *File, unsigned Line,
    2600             :                      MDString *GetterName, MDString *SetterName,
    2601             :                      unsigned Attributes, Metadata *Type),
    2602             :                     (Name, File, Line, GetterName, SetterName, Attributes,
    2603             :                      Type))
    2604             : 
    2605           1 :   TempDIObjCProperty clone() const { return cloneImpl(); }
    2606             : 
    2607             :   unsigned getLine() const { return Line; }
    2608             :   unsigned getAttributes() const { return Attributes; }
    2609          64 :   StringRef getName() const { return getStringOperand(0); }
    2610             :   DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
    2611          64 :   StringRef getGetterName() const { return getStringOperand(2); }
    2612          64 :   StringRef getSetterName() const { return getStringOperand(3); }
    2613             :   DITypeRef getType() const { return DITypeRef(getRawType()); }
    2614             : 
    2615             :   StringRef getFilename() const {
    2616             :     if (auto *F = getFile())
    2617             :       return F->getFilename();
    2618             :     return "";
    2619             :   }
    2620             : 
    2621             :   StringRef getDirectory() const {
    2622             :     if (auto *F = getFile())
    2623             :       return F->getDirectory();
    2624             :     return "";
    2625             :   }
    2626             : 
    2627             :   MDString *getRawName() const { return getOperandAs<MDString>(0); }
    2628         202 :   Metadata *getRawFile() const { return getOperand(1); }
    2629             :   MDString *getRawGetterName() const { return getOperandAs<MDString>(2); }
    2630             :   MDString *getRawSetterName() const { return getOperandAs<MDString>(3); }
    2631         118 :   Metadata *getRawType() const { return getOperand(4); }
    2632             : 
    2633             :   static bool classof(const Metadata *MD) {
    2634             :     return MD->getMetadataID() == DIObjCPropertyKind;
    2635             :   }
    2636             : };
    2637             : 
    2638             : /// An imported module (C++ using directive or similar).
    2639             : class DIImportedEntity : public DINode {
    2640             :   friend class LLVMContextImpl;
    2641             :   friend class MDNode;
    2642             : 
    2643             :   unsigned Line;
    2644             : 
    2645             :   DIImportedEntity(LLVMContext &C, StorageType Storage, unsigned Tag,
    2646             :                    unsigned Line, ArrayRef<Metadata *> Ops)
    2647       18136 :       : DINode(C, DIImportedEntityKind, Storage, Tag, Ops), Line(Line) {}
    2648         351 :   ~DIImportedEntity() = default;
    2649             : 
    2650        8782 :   static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
    2651             :                                    DIScope *Scope, DINodeRef Entity,
    2652             :                                    DIFile *File, unsigned Line, StringRef Name,
    2653             :                                    StorageType Storage,
    2654             :                                    bool ShouldCreate = true) {
    2655        8782 :     return getImpl(Context, Tag, Scope, Entity, File, Line,
    2656        8782 :                    getCanonicalMDString(Context, Name), Storage, ShouldCreate);
    2657             :   }
    2658             :   static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
    2659             :                                    Metadata *Scope, Metadata *Entity,
    2660             :                                    Metadata *File, unsigned Line,
    2661             :                                    MDString *Name, StorageType Storage,
    2662             :                                    bool ShouldCreate = true);
    2663             : 
    2664           1 :   TempDIImportedEntity cloneImpl() const {
    2665             :     return getTemporary(getContext(), getTag(), getScope(), getEntity(),
    2666           3 :                         getFile(), getLine(), getName());
    2667             :   }
    2668             : 
    2669             : public:
    2670        8782 :   DEFINE_MDNODE_GET(DIImportedEntity,
    2671             :                     (unsigned Tag, DIScope *Scope, DINodeRef Entity,
    2672             :                      DIFile *File, unsigned Line, StringRef Name = ""),
    2673             :                     (Tag, Scope, Entity, File, Line, Name))
    2674         292 :   DEFINE_MDNODE_GET(DIImportedEntity,
    2675             :                     (unsigned Tag, Metadata *Scope, Metadata *Entity,
    2676             :                      Metadata *File, unsigned Line, MDString *Name),
    2677             :                     (Tag, Scope, Entity, File, Line, Name))
    2678             : 
    2679           1 :   TempDIImportedEntity clone() const { return cloneImpl(); }
    2680             : 
    2681             :   unsigned getLine() const { return Line; }
    2682             :   DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
    2683             :   DINodeRef getEntity() const { return DINodeRef(getRawEntity()); }
    2684        8833 :   StringRef getName() const { return getStringOperand(2); }
    2685             :   DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
    2686             : 
    2687       57198 :   Metadata *getRawScope() const { return getOperand(0); }
    2688        8956 :   Metadata *getRawEntity() const { return getOperand(1); }
    2689             :   MDString *getRawName() const { return getOperandAs<MDString>(2); }
    2690           2 :   Metadata *getRawFile() const { return getOperand(3); }
    2691             : 
    2692             :   static bool classof(const Metadata *MD) {
    2693             :     return MD->getMetadataID() == DIImportedEntityKind;
    2694             :   }
    2695             : };
    2696             : 
    2697             : /// A pair of DIGlobalVariable and DIExpression.
    2698             : class DIGlobalVariableExpression : public MDNode {
    2699             :   friend class LLVMContextImpl;
    2700             :   friend class MDNode;
    2701             : 
    2702             :   DIGlobalVariableExpression(LLVMContext &C, StorageType Storage,
    2703             :                              ArrayRef<Metadata *> Ops)
    2704        2554 :       : MDNode(C, DIGlobalVariableExpressionKind, Storage, Ops) {}
    2705        1088 :   ~DIGlobalVariableExpression() = default;
    2706             : 
    2707             :   static DIGlobalVariableExpression *
    2708             :   getImpl(LLVMContext &Context, Metadata *Variable, Metadata *Expression,
    2709             :           StorageType Storage, bool ShouldCreate = true);
    2710             : 
    2711           2 :   TempDIGlobalVariableExpression cloneImpl() const {
    2712           2 :     return getTemporary(getContext(), getVariable(), getExpression());
    2713             :   }
    2714             : 
    2715             : public:
    2716        1279 :   DEFINE_MDNODE_GET(DIGlobalVariableExpression,
    2717             :                     (Metadata * Variable, Metadata *Expression),
    2718             :                     (Variable, Expression))
    2719             : 
    2720           1 :   TempDIGlobalVariableExpression clone() const { return cloneImpl(); }
    2721             : 
    2722       10536 :   Metadata *getRawVariable() const { return getOperand(0); }
    2723             : 
    2724             :   DIGlobalVariable *getVariable() const {
    2725             :     return cast_or_null<DIGlobalVariable>(getRawVariable());
    2726             :   }
    2727             : 
    2728           7 :   Metadata *getRawExpression() const { return getOperand(1); }
    2729             : 
    2730             :   DIExpression *getExpression() const {
    2731             :     return cast<DIExpression>(getRawExpression());
    2732             :   }
    2733             : 
    2734             :   static bool classof(const Metadata *MD) {
    2735        2114 :     return MD->getMetadataID() == DIGlobalVariableExpressionKind;
    2736             :   }
    2737             : };
    2738             : 
    2739             : /// Macro Info DWARF-like metadata node.
    2740             : ///
    2741             : /// A metadata node with a DWARF macro info (i.e., a constant named
    2742             : /// \c DW_MACINFO_*, defined in llvm/BinaryFormat/Dwarf.h).  Called \a
    2743             : /// DIMacroNode
    2744             : /// because it's potentially used for non-DWARF output.
    2745             : class DIMacroNode : public MDNode {
    2746             :   friend class LLVMContextImpl;
    2747             :   friend class MDNode;
    2748             : 
    2749             : protected:
    2750             :   DIMacroNode(LLVMContext &C, unsigned ID, StorageType Storage, unsigned MIType,
    2751             :               ArrayRef<Metadata *> Ops1, ArrayRef<Metadata *> Ops2 = None)
    2752        1179 :       : MDNode(C, ID, Storage, Ops1, Ops2) {
    2753             :     assert(MIType < 1u << 16);
    2754        1179 :     SubclassData16 = MIType;
    2755             :   }
    2756             :   ~DIMacroNode() = default;
    2757             : 
    2758             :   template <class Ty> Ty *getOperandAs(unsigned I) const {
    2759        7993 :     return cast_or_null<Ty>(getOperand(I));
    2760             :   }
    2761             : 
    2762        4418 :   StringRef getStringOperand(unsigned I) const {
    2763             :     if (auto *S = getOperandAs<MDString>(I))
    2764        4331 :       return S->getString();
    2765          87 :     return StringRef();
    2766             :   }
    2767             : 
    2768             :   static MDString *getCanonicalMDString(LLVMContext &Context, StringRef S) {
    2769        2144 :     if (S.empty())
    2770             :       return nullptr;
    2771        2097 :     return MDString::get(Context, S);
    2772             :   }
    2773             : 
    2774             : public:
    2775        5949 :   unsigned getMacinfoType() const { return SubclassData16; }
    2776             : 
    2777             :   static bool classof(const Metadata *MD) {
    2778        1205 :     switch (MD->getMetadataID()) {
    2779             :     default:
    2780             :       return false;
    2781             :     case DIMacroKind:
    2782             :     case DIMacroFileKind:
    2783             :       return true;
    2784             :     }
    2785             :   }
    2786             : };
    2787             : 
    2788             : class DIMacro : public DIMacroNode {
    2789             :   friend class LLVMContextImpl;
    2790             :   friend class MDNode;
    2791             : 
    2792             :   unsigned Line;
    2793             : 
    2794             :   DIMacro(LLVMContext &C, StorageType Storage, unsigned MIType, unsigned Line,
    2795             :           ArrayRef<Metadata *> Ops)
    2796        2194 :       : DIMacroNode(C, DIMacroKind, Storage, MIType, Ops), Line(Line) {}
    2797        1097 :   ~DIMacro() = default;
    2798             : 
    2799        1072 :   static DIMacro *getImpl(LLVMContext &Context, unsigned MIType, unsigned Line,
    2800             :                           StringRef Name, StringRef Value, StorageType Storage,
    2801             :                           bool ShouldCreate = true) {
    2802        1072 :     return getImpl(Context, MIType, Line, getCanonicalMDString(Context, Name),
    2803        1072 :                    getCanonicalMDString(Context, Value), Storage, ShouldCreate);
    2804             :   }
    2805             :   static DIMacro *getImpl(LLVMContext &Context, unsigned MIType, unsigned Line,
    2806             :                           MDString *Name, MDString *Value, StorageType Storage,
    2807             :                           bool ShouldCreate = true);
    2808             : 
    2809           0 :   TempDIMacro cloneImpl() const {
    2810             :     return getTemporary(getContext(), getMacinfoType(), getLine(), getName(),
    2811           0 :                         getValue());
    2812             :   }
    2813             : 
    2814             : public:
    2815        1072 :   DEFINE_MDNODE_GET(DIMacro, (unsigned MIType, unsigned Line, StringRef Name,
    2816             :                               StringRef Value = ""),
    2817             :                     (MIType, Line, Name, Value))
    2818          34 :   DEFINE_MDNODE_GET(DIMacro, (unsigned MIType, unsigned Line, MDString *Name,
    2819             :                               MDString *Value),
    2820             :                     (MIType, Line, Name, Value))
    2821             : 
    2822             :   TempDIMacro clone() const { return cloneImpl(); }
    2823             : 
    2824             :   unsigned getLine() const { return Line; }
    2825             : 
    2826        2209 :   StringRef getName() const { return getStringOperand(0); }
    2827        2209 :   StringRef getValue() const { return getStringOperand(1); }
    2828             : 
    2829             :   MDString *getRawName() const { return getOperandAs<MDString>(0); }
    2830             :   MDString *getRawValue() const { return getOperandAs<MDString>(1); }
    2831             : 
    2832             :   static bool classof(const Metadata *MD) {
    2833           7 :     return MD->getMetadataID() == DIMacroKind;
    2834             :   }
    2835             : };
    2836             : 
    2837             : class DIMacroFile : public DIMacroNode {
    2838             :   friend class LLVMContextImpl;
    2839             :   friend class MDNode;
    2840             : 
    2841             :   unsigned Line;
    2842             : 
    2843             :   DIMacroFile(LLVMContext &C, StorageType Storage, unsigned MIType,
    2844             :               unsigned Line, ArrayRef<Metadata *> Ops)
    2845         164 :       : DIMacroNode(C, DIMacroFileKind, Storage, MIType, Ops), Line(Line) {}
    2846          82 :   ~DIMacroFile() = default;
    2847             : 
    2848             :   static DIMacroFile *getImpl(LLVMContext &Context, unsigned MIType,
    2849             :                               unsigned Line, DIFile *File,
    2850             :                               DIMacroNodeArray Elements, StorageType Storage,
    2851             :                               bool ShouldCreate = true) {
    2852             :     return getImpl(Context, MIType, Line, static_cast<Metadata *>(File),
    2853          38 :                    Elements.get(), Storage, ShouldCreate);
    2854             :   }
    2855             : 
    2856             :   static DIMacroFile *getImpl(LLVMContext &Context, unsigned MIType,
    2857             :                               unsigned Line, Metadata *File, Metadata *Elements,
    2858             :                               StorageType Storage, bool ShouldCreate = true);
    2859             : 
    2860           0 :   TempDIMacroFile cloneImpl() const {
    2861             :     return getTemporary(getContext(), getMacinfoType(), getLine(), getFile(),
    2862           0 :                         getElements());
    2863             :   }
    2864             : 
    2865             : public:
    2866             :   DEFINE_MDNODE_GET(DIMacroFile, (unsigned MIType, unsigned Line, DIFile *File,
    2867             :                                   DIMacroNodeArray Elements),
    2868             :                     (MIType, Line, File, Elements))
    2869          46 :   DEFINE_MDNODE_GET(DIMacroFile, (unsigned MIType, unsigned Line,
    2870             :                                   Metadata *File, Metadata *Elements),
    2871             :                     (MIType, Line, File, Elements))
    2872             : 
    2873             :   TempDIMacroFile clone() const { return cloneImpl(); }
    2874             : 
    2875             :   void replaceElements(DIMacroNodeArray Elements) {
    2876             : #ifndef NDEBUG
    2877             :     for (DIMacroNode *Op : getElements())
    2878             :       assert(is_contained(Elements->operands(), Op) &&
    2879             :              "Lost a macro node during macro node list replacement");
    2880             : #endif
    2881             :     replaceOperandWith(1, Elements.get());
    2882             :   }
    2883             : 
    2884             :   unsigned getLine() const { return Line; }
    2885             :   DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
    2886             : 
    2887             :   DIMacroNodeArray getElements() const {
    2888             :     return cast_or_null<MDTuple>(getRawElements());
    2889             :   }
    2890             : 
    2891         278 :   Metadata *getRawFile() const { return getOperand(0); }
    2892           2 :   Metadata *getRawElements() const { return getOperand(1); }
    2893             : 
    2894             :   static bool classof(const Metadata *MD) {
    2895             :     return MD->getMetadataID() == DIMacroFileKind;
    2896             :   }
    2897             : };
    2898             : 
    2899             : } // end namespace llvm
    2900             : 
    2901             : #undef DEFINE_MDNODE_GET_UNPACK_IMPL
    2902             : #undef DEFINE_MDNODE_GET_UNPACK
    2903             : #undef DEFINE_MDNODE_GET
    2904             : 
    2905             : #endif // LLVM_IR_DEBUGINFOMETADATA_H

Generated by: LCOV version 1.13