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

Generated by: LCOV version 1.13