LCOV - code coverage report
Current view: top level - include/llvm/IR - DebugInfoMetadata.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 532 609 87.4 %
Date: 2018-10-20 13:21:21 Functions: 70 144 48.6 %
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       17696 :   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           0 :   operator Metadata *() const { return const_cast<Metadata *>(MD); }
      91             : 
      92           0 :   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      120941 :   unsigned size() const { return N ? N->getNumOperands() : 0u; }
     118      149440 :   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           0 :     explicit iterator(MDNode::op_iterator I) : I(I) {}
     127             : 
     128           0 :     DITypeRef operator*() const { return DITypeRef(*I); }
     129             : 
     130             :     iterator &operator++() {
     131         550 :       ++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           0 :     bool operator!=(const iterator &X) const { return I != X.I; }
     143             :   };
     144             : 
     145             :   // FIXME: Fix callers and remove condition on N.
     146         331 :   iterator begin() const { return N ? iterator(N->op_begin()) : iterator(); }
     147         331 :   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        6392 :       : MDNode(C, ID, Storage, Ops1, Ops2) {
     163             :     assert(Tag < 1u << 16);
     164      337252 :     SubclassData16 = Tag;
     165             :   }
     166             :   ~DINode() = default;
     167             : 
     168             :   template <class Ty> Ty *getOperandAs(unsigned I) const {
     169     3485845 :     return cast_or_null<Ty>(getOperand(I));
     170             :   }
     171             : 
     172     4840174 :   StringRef getStringOperand(unsigned I) const {
     173             :     if (auto *S = getOperandAs<MDString>(I))
     174     4710979 :       return S->getString();
     175      129195 :     return StringRef();
     176             :   }
     177             : 
     178             :   static MDString *getCanonicalMDString(LLVMContext &Context, StringRef S) {
     179       21003 :     if (S.empty())
     180             :       return nullptr;
     181      277464 :     return MDString::get(Context, S);
     182             :   }
     183             : 
     184             :   /// Allow subclasses to mutate the tag.
     185           5 :   void setTag(unsigned Tag) { SubclassData16 = Tag; }
     186             : 
     187             : public:
     188      536077 :   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        2154 :     switch (MD->getMetadataID()) {
     216             :     default:
     217             :       return false;
     218           0 :     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          48 :       : DINode(C, GenericDINodeKind, Storage, Tag, Ops1, Ops2) {
     270             :     setHash(Hash);
     271             :   }
     272          96 :   ~GenericDINode() { dropAllReferences(); }
     273             : 
     274          59 :   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          11 :     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           1 :         SmallVector<Metadata *, 4>(dwarf_op_begin(), dwarf_op_end()));
     292             :   }
     293             : 
     294             : public:
     295           0 :   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         837 :       : DINode(C, DISubrangeKind, Storage, dwarf::DW_TAG_subrange_type, Ops),
     343         837 :         LowerBound(LowerBound) {}
     344             : 
     345         445 :   ~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         900 :   DEFINE_MDNODE_GET(DISubrange, (int64_t Count, int64_t LowerBound = 0),
     361             :                     (Count, LowerBound))
     362             : 
     363          96 :   DEFINE_MDNODE_GET(DISubrange, (Metadata *CountNode, int64_t LowerBound = 0),
     364             :                     (CountNode, LowerBound))
     365             : 
     366           1 :   TempDISubrange clone() const { return cloneImpl(); }
     367             : 
     368           0 :   int64_t getLowerBound() const { return LowerBound; }
     369             : 
     370             :   Metadata *getRawCountNode() const {
     371        7046 :     return getOperand(0).get();
     372             :   }
     373             : 
     374             :   typedef PointerUnion<ConstantInt*, DIVariable*> CountType;
     375             : 
     376             :   CountType getCount() const {
     377             :     if (auto *MD = dyn_cast<ConstantAsMetadata>(getRawCountNode()))
     378             :       return CountType(cast<ConstantInt>(MD->getValue()));
     379             : 
     380             :     if (auto *DV = dyn_cast<DIVariable>(getRawCountNode()))
     381             :       return CountType(DV);
     382             : 
     383             :     return CountType();
     384             :   }
     385             : 
     386             :   static bool classof(const Metadata *MD) {
     387         176 :     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        7619 :       : DINode(C, DIEnumeratorKind, Storage, dwarf::DW_TAG_enumerator, Ops),
     403        7619 :         Value(Value) {
     404        7619 :     SubclassData32 = IsUnsigned;
     405             :   }
     406        6265 :   ~DIEnumerator() = default;
     407             : 
     408        1442 :   static DIEnumerator *getImpl(LLVMContext &Context, int64_t Value,
     409             :                                bool IsUnsigned, StringRef Name,
     410             :                                StorageType Storage, bool ShouldCreate = true) {
     411        2884 :     return getImpl(Context, Value, IsUnsigned,
     412        1442 :                    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        1442 :   DEFINE_MDNODE_GET(DIEnumerator, (int64_t Value, bool IsUnsigned, StringRef Name),
     424             :                     (Value, IsUnsigned, Name))
     425        6202 :   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           0 :   int64_t getValue() const { return Value; }
     431       15136 :   bool isUnsigned() const { return SubclassData32; }
     432        7504 :   StringRef getName() const { return getStringOperand(0); }
     433             : 
     434             :   MDString *getRawName() const { return getOperandAs<MDString>(0); }
     435             : 
     436             :   static bool classof(const Metadata *MD) {
     437        7502 :     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        9820 :       : 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         299 :     return isa<DIFile>(this) ? const_cast<DIScope *>(this)
     472     3909759 :                              : static_cast<Metadata *>(getOperand(0));
     473             :   }
     474             : 
     475             :   static bool classof(const Metadata *MD) {
     476      128530 :     switch (MD->getMetadataID()) {
     477             :     default:
     478             :       return false;
     479           0 :     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        1255 :     ChecksumInfo(ChecksumKind Kind, T Value) : Kind(Kind), Value(Value) { }
     527             :     ~ChecksumInfo() = default;
     528           1 :     bool operator==(const ChecksumInfo<T> &X) const {
     529          51 :       return Kind == X.Kind && Value == X.Value;
     530             :     }
     531             :     bool operator!=(const ChecksumInfo<T> &X) const { return !(*this == X); }
     532          74 :     StringRef getKindAsString() const { return getChecksumKindAsString(Kind); }
     533             :   };
     534             : 
     535             : private:
     536             :   Optional<ChecksumInfo<MDString *>> Checksum;
     537             :   Optional<MDString *> Source;
     538             : 
     539        9730 :   DIFile(LLVMContext &C, StorageType Storage,
     540             :          Optional<ChecksumInfo<MDString *>> CS, Optional<MDString *> Src,
     541             :          ArrayRef<Metadata *> Ops)
     542        9730 :       : DIScope(C, DIFileKind, Storage, dwarf::DW_TAG_file_type, Ops),
     543        9730 :         Checksum(CS), Source(Src) {}
     544        3939 :   ~DIFile() = default;
     545             : 
     546       20814 :   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       20814 :     if (CS)
     553          99 :       MDChecksum.emplace(CS->Kind, getCanonicalMDString(Context, CS->Value));
     554       41628 :     return getImpl(Context, getCanonicalMDString(Context, Filename),
     555             :                    getCanonicalMDString(Context, Directory), MDChecksum,
     556       20822 :                    Source ? Optional<MDString *>(getCanonicalMDString(Context, *Source)) : None,
     557       20814 :                    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       41628 :   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        6328 :   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     3199227 :   StringRef getFilename() const { return getStringOperand(0); }
     583      906646 :   StringRef getDirectory() const { return getStringOperand(1); }
     584             :   Optional<ChecksumInfo<StringRef>> getChecksum() const {
     585             :     Optional<ChecksumInfo<StringRef>> StringRefChecksum;
     586       22751 :     if (Checksum)
     587        1076 :       StringRefChecksum.emplace(Checksum->Kind, Checksum->Value->getString());
     588             :     return StringRefChecksum;
     589             :   }
     590             :   Optional<StringRef> getSource() const {
     591      894941 :     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     2155481 :     return MD->getMetadataID() == DIFileKind;
     604             :   }
     605             : };
     606             : 
     607     2297003 : StringRef DIScope::getFilename() const {
     608             :   if (auto *F = getFile())
     609             :     return F->getFilename();
     610          55 :   return "";
     611             : }
     612             : 
     613       14181 : StringRef DIScope::getDirectory() const {
     614             :   if (auto *F = getFile())
     615             :     return F->getDirectory();
     616           4 :   return "";
     617             : }
     618             : 
     619        1348 : 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      137481 :       : 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      137486 :     this->Line = Line;
     649      137486 :     this->Flags = Flags;
     650      137486 :     this->SizeInBits = SizeInBits;
     651      137486 :     this->AlignInBits = AlignInBits;
     652       76239 :     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       32752 :     return TempDIType(cast<DIType>(MDNode::clone().release()));
     666             :   }
     667             : 
     668           0 :   unsigned getLine() const { return Line; }
     669           0 :   uint64_t getSizeInBits() const { return SizeInBits; }
     670           0 :   uint32_t getAlignInBits() const { return AlignInBits; }
     671       15215 :   uint32_t getAlignInBytes() const { return getAlignInBits() / CHAR_BIT; }
     672           0 :   uint64_t getOffsetInBits() const { return OffsetInBits; }
     673           0 :   DIFlags getFlags() const { return Flags; }
     674             : 
     675             :   DIScopeRef getScope() const { return DIScopeRef(getRawScope()); }
     676      144589 :   StringRef getName() const { return getStringOperand(2); }
     677             : 
     678             : 
     679      565792 :   Metadata *getRawScope() const { return getOperand(1); }
     680             :   MDString *getRawName() const { return getOperandAs<MDString>(2); }
     681             : 
     682             :   /// Returns a new temporary DIType with updated Flags
     683             :   TempDIType cloneWithFlags(DIFlags NewFlags) const {
     684             :     auto NewTy = clone();
     685       32751 :     NewTy->Flags = NewFlags;
     686             :     return NewTy;
     687             :   }
     688             : 
     689             :   bool isPrivate() const {
     690             :     return (getFlags() & FlagAccessibility) == FlagPrivate;
     691             :   }
     692             :   bool isProtected() const {
     693        8908 :     return (getFlags() & FlagAccessibility) == FlagProtected;
     694             :   }
     695             :   bool isPublic() const {
     696             :     return (getFlags() & FlagAccessibility) == FlagPublic;
     697             :   }
     698       58354 :   bool isForwardDecl() const { return getFlags() & FlagFwdDecl; }
     699        6038 :   bool isAppleBlockExtension() const { return getFlags() & FlagAppleBlock; }
     700      115000 :   bool isBlockByrefStruct() const { return getFlags() & FlagBlockByrefStruct; }
     701        8878 :   bool isVirtual() const { return getFlags() & FlagVirtual; }
     702       95889 :   bool isArtificial() const { return getFlags() & FlagArtificial; }
     703       84687 :   bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
     704             :   bool isObjcClassComplete() const {
     705        6038 :     return getFlags() & FlagObjcClassComplete;
     706             :   }
     707         545 :   bool isVector() const { return getFlags() & FlagVector; }
     708         104 :   bool isBitField() const { return getFlags() & FlagBitField; }
     709        8907 :   bool isStaticMember() const { return getFlags() & FlagStaticMember; }
     710         292 :   bool isLValueReference() const { return getFlags() & FlagLValueReference; }
     711         292 :   bool isRValueReference() const { return getFlags() & FlagRValueReference; }
     712        6038 :   bool isTypePassByValue() const { return getFlags() & FlagTypePassByValue; }
     713             :   bool isTypePassByReference() const {
     714             :     return getFlags() & FlagTypePassByReference;
     715             :   }
     716        8414 :   bool isBigEndian() const { return getFlags() & FlagBigEndian; }
     717             :   bool isLittleEndian() const { return getFlags() & FlagLittleEndian; }
     718             : 
     719             :   static bool classof(const Metadata *MD) {
     720     3693649 :     switch (MD->getMetadataID()) {
     721             :     default:
     722             :       return false;
     723           0 :     case DIBasicTypeKind:
     724             :     case DIDerivedTypeKind:
     725             :     case DICompositeTypeKind:
     726             :     case DISubroutineTypeKind:
     727             :       return true;
     728             :     }
     729             :   }
     730             : };
     731             : 
     732             : /// Basic type, like 'int' or 'float'.
     733             : ///
     734             : /// TODO: Split out DW_TAG_unspecified_type.
     735             : /// TODO: Drop unused accessors.
     736             : class DIBasicType : public DIType {
     737             :   friend class LLVMContextImpl;
     738             :   friend class MDNode;
     739             : 
     740             :   unsigned Encoding;
     741             : 
     742             :   DIBasicType(LLVMContext &C, StorageType Storage, unsigned Tag,
     743             :               uint64_t SizeInBits, uint32_t AlignInBits, unsigned Encoding,
     744             :               DIFlags Flags, ArrayRef<Metadata *> Ops)
     745        4016 :       : DIType(C, DIBasicTypeKind, Storage, Tag, 0, SizeInBits, AlignInBits, 0,
     746             :                Flags, Ops),
     747        4016 :         Encoding(Encoding) {}
     748        2525 :   ~DIBasicType() = default;
     749             : 
     750        2616 :   static DIBasicType *getImpl(LLVMContext &Context, unsigned Tag,
     751             :                               StringRef Name, uint64_t SizeInBits,
     752             :                               uint32_t AlignInBits, unsigned Encoding,
     753             :                               DIFlags Flags, StorageType Storage,
     754             :                               bool ShouldCreate = true) {
     755        5232 :     return getImpl(Context, Tag, getCanonicalMDString(Context, Name),
     756             :                    SizeInBits, AlignInBits, Encoding, Flags, Storage,
     757        2616 :                    ShouldCreate);
     758             :   }
     759             :   static DIBasicType *getImpl(LLVMContext &Context, unsigned Tag,
     760             :                               MDString *Name, uint64_t SizeInBits,
     761             :                               uint32_t AlignInBits, unsigned Encoding,
     762             :                               DIFlags Flags, StorageType Storage,
     763             :                               bool ShouldCreate = true);
     764             : 
     765           2 :   TempDIBasicType cloneImpl() const {
     766             :     return getTemporary(getContext(), getTag(), getName(), getSizeInBits(),
     767           2 :                         getAlignInBits(), getEncoding(), getFlags());
     768             :   }
     769             : 
     770             : public:
     771          86 :   DEFINE_MDNODE_GET(DIBasicType, (unsigned Tag, StringRef Name),
     772             :                     (Tag, Name, 0, 0, 0, FlagZero))
     773        2530 :   DEFINE_MDNODE_GET(DIBasicType,
     774             :                     (unsigned Tag, StringRef Name, uint64_t SizeInBits,
     775             :                      uint32_t AlignInBits, unsigned Encoding, DIFlags Flags),
     776             :                     (Tag, Name, SizeInBits, AlignInBits, Encoding, Flags))
     777        1788 :   DEFINE_MDNODE_GET(DIBasicType,
     778             :                     (unsigned Tag, MDString *Name, uint64_t SizeInBits,
     779             :                      uint32_t AlignInBits, unsigned Encoding, DIFlags Flags),
     780             :                     (Tag, Name, SizeInBits, AlignInBits, Encoding, Flags))
     781             : 
     782           1 :   TempDIBasicType clone() const { return cloneImpl(); }
     783             : 
     784           0 :   unsigned getEncoding() const { return Encoding; }
     785             : 
     786             :   enum class Signedness { Signed, Unsigned };
     787             : 
     788             :   /// Return the signedness of this type, or None if this type is neither
     789             :   /// signed nor unsigned.
     790             :   Optional<Signedness> getSignedness() const;
     791             : 
     792             :   static bool classof(const Metadata *MD) {
     793       39002 :     return MD->getMetadataID() == DIBasicTypeKind;
     794             :   }
     795             : };
     796             : 
     797             : /// Derived types.
     798             : ///
     799             : /// This includes qualified types, pointers, references, friends, typedefs, and
     800             : /// class members.
     801             : ///
     802             : /// TODO: Split out members (inheritance, fields, methods, etc.).
     803             : class DIDerivedType : public DIType {
     804             :   friend class LLVMContextImpl;
     805             :   friend class MDNode;
     806             : 
     807             :   /// The DWARF address space of the memory pointed to or referenced by a
     808             :   /// pointer or reference type respectively.
     809             :   Optional<unsigned> DWARFAddressSpace;
     810             : 
     811             :   DIDerivedType(LLVMContext &C, StorageType Storage, unsigned Tag,
     812             :                 unsigned Line, uint64_t SizeInBits, uint32_t AlignInBits,
     813             :                 uint64_t OffsetInBits, Optional<unsigned> DWARFAddressSpace,
     814             :                 DIFlags Flags, ArrayRef<Metadata *> Ops)
     815       76239 :       : DIType(C, DIDerivedTypeKind, Storage, Tag, Line, SizeInBits,
     816             :                AlignInBits, OffsetInBits, Flags, Ops),
     817             :         DWARFAddressSpace(DWARFAddressSpace) {}
     818       35007 :   ~DIDerivedType() = default;
     819             : 
     820      103102 :   static DIDerivedType *getImpl(LLVMContext &Context, unsigned Tag,
     821             :                                 StringRef Name, DIFile *File, unsigned Line,
     822             :                                 DIScopeRef Scope, DITypeRef BaseType,
     823             :                                 uint64_t SizeInBits, uint32_t AlignInBits,
     824             :                                 uint64_t OffsetInBits,
     825             :                                 Optional<unsigned> DWARFAddressSpace,
     826             :                                 DIFlags Flags, Metadata *ExtraData,
     827             :                                 StorageType Storage, bool ShouldCreate = true) {
     828      124826 :     return getImpl(Context, Tag, getCanonicalMDString(Context, Name), File,
     829             :                    Line, Scope, BaseType, SizeInBits, AlignInBits, OffsetInBits,
     830      103102 :                    DWARFAddressSpace, Flags, ExtraData, Storage, ShouldCreate);
     831             :   }
     832             :   static DIDerivedType *getImpl(LLVMContext &Context, unsigned Tag,
     833             :                                 MDString *Name, Metadata *File, unsigned Line,
     834             :                                 Metadata *Scope, Metadata *BaseType,
     835             :                                 uint64_t SizeInBits, uint32_t AlignInBits,
     836             :                                 uint64_t OffsetInBits,
     837             :                                 Optional<unsigned> DWARFAddressSpace,
     838             :                                 DIFlags Flags, Metadata *ExtraData,
     839             :                                 StorageType Storage, bool ShouldCreate = true);
     840             : 
     841       32752 :   TempDIDerivedType cloneImpl() const {
     842             :     return getTemporary(getContext(), getTag(), getName(), getFile(), getLine(),
     843             :                         getScope(), getBaseType(), getSizeInBits(),
     844             :                         getAlignInBits(), getOffsetInBits(),
     845       98258 :                         getDWARFAddressSpace(), getFlags(), getExtraData());
     846             :   }
     847             : 
     848             : public:
     849        2338 :   DEFINE_MDNODE_GET(DIDerivedType,
     850             :                     (unsigned Tag, MDString *Name, Metadata *File,
     851             :                      unsigned Line, Metadata *Scope, Metadata *BaseType,
     852             :                      uint64_t SizeInBits, uint32_t AlignInBits,
     853             :                      uint64_t OffsetInBits,
     854             :                      Optional<unsigned> DWARFAddressSpace, DIFlags Flags,
     855             :                      Metadata *ExtraData = nullptr),
     856             :                     (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
     857             :                      AlignInBits, OffsetInBits, DWARFAddressSpace, Flags,
     858             :                      ExtraData))
     859      103103 :   DEFINE_MDNODE_GET(DIDerivedType,
     860             :                     (unsigned Tag, StringRef Name, DIFile *File, unsigned Line,
     861             :                      DIScopeRef Scope, DITypeRef BaseType, uint64_t SizeInBits,
     862             :                      uint32_t AlignInBits, uint64_t OffsetInBits,
     863             :                      Optional<unsigned> DWARFAddressSpace, DIFlags Flags,
     864             :                      Metadata *ExtraData = nullptr),
     865             :                     (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
     866             :                      AlignInBits, OffsetInBits, DWARFAddressSpace, Flags,
     867             :                      ExtraData))
     868             : 
     869           1 :   TempDIDerivedType clone() const { return cloneImpl(); }
     870             : 
     871             :   /// Get the base type this is derived from.
     872             :   DITypeRef getBaseType() const { return DITypeRef(getRawBaseType()); }
     873      322051 :   Metadata *getRawBaseType() const { return getOperand(3); }
     874             : 
     875             :   /// \returns The DWARF address space of the memory pointed to or referenced by
     876             :   /// a pointer or reference type respectively.
     877             :   Optional<unsigned> getDWARFAddressSpace() const { return DWARFAddressSpace; }
     878             : 
     879             :   /// Get extra data associated with this derived type.
     880             :   ///
     881             :   /// Class type for pointer-to-members, objective-c property node for ivars,
     882             :   /// global constant wrapper for static members, or virtual base pointer offset
     883             :   /// for inheritance.
     884             :   ///
     885             :   /// TODO: Separate out types that need this extra operand: pointer-to-member
     886             :   /// types and member fields (static members and ivars).
     887             :   Metadata *getExtraData() const { return getRawExtraData(); }
     888       32925 :   Metadata *getRawExtraData() const { return getOperand(4); }
     889             : 
     890             :   /// Get casted version of extra data.
     891             :   /// @{
     892             :   DITypeRef getClassType() const {
     893             :     assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
     894             :     return DITypeRef(getExtraData());
     895             :   }
     896             : 
     897             :   DIObjCProperty *getObjCProperty() const {
     898             :     return dyn_cast_or_null<DIObjCProperty>(getExtraData());
     899             :   }
     900             : 
     901           9 :   uint32_t getVBPtrOffset() const {
     902             :     assert(getTag() == dwarf::DW_TAG_inheritance);
     903             :     if (auto *CM = cast_or_null<ConstantAsMetadata>(getExtraData()))
     904             :       if (auto *CI = dyn_cast_or_null<ConstantInt>(CM->getValue()))
     905           3 :         return static_cast<uint32_t>(CI->getZExtValue());
     906             :     return 0;
     907             :   }
     908             : 
     909             :   Constant *getStorageOffsetInBits() const {
     910             :     assert(getTag() == dwarf::DW_TAG_member && isBitField());
     911             :     if (auto *C = cast_or_null<ConstantAsMetadata>(getExtraData()))
     912             :       return C->getValue();
     913             :     return nullptr;
     914             :   }
     915             : 
     916             :   Constant *getConstant() const {
     917             :     assert(getTag() == dwarf::DW_TAG_member && isStaticMember());
     918             :     if (auto *C = cast_or_null<ConstantAsMetadata>(getExtraData()))
     919             :       return C->getValue();
     920             :     return nullptr;
     921             :   }
     922             :   Constant *getDiscriminantValue() const {
     923             :     assert(getTag() == dwarf::DW_TAG_member && !isStaticMember());
     924             :     if (auto *C = cast_or_null<ConstantAsMetadata>(getExtraData()))
     925             :       return C->getValue();
     926             :     return nullptr;
     927             :   }
     928             :   /// @}
     929             : 
     930             :   static bool classof(const Metadata *MD) {
     931       16799 :     return MD->getMetadataID() == DIDerivedTypeKind;
     932             :   }
     933             : };
     934             : 
     935             : /// Composite types.
     936             : ///
     937             : /// TODO: Detach from DerivedTypeBase (split out MDEnumType?).
     938             : /// TODO: Create a custom, unrelated node for DW_TAG_array_type.
     939             : class DICompositeType : public DIType {
     940             :   friend class LLVMContextImpl;
     941             :   friend class MDNode;
     942             : 
     943             :   unsigned RuntimeLang;
     944             : 
     945             :   DICompositeType(LLVMContext &C, StorageType Storage, unsigned Tag,
     946             :                   unsigned Line, unsigned RuntimeLang, uint64_t SizeInBits,
     947             :                   uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags,
     948             :                   ArrayRef<Metadata *> Ops)
     949       10520 :       : DIType(C, DICompositeTypeKind, Storage, Tag, Line, SizeInBits,
     950             :                AlignInBits, OffsetInBits, Flags, Ops),
     951       10520 :         RuntimeLang(RuntimeLang) {}
     952        3562 :   ~DICompositeType() = default;
     953             : 
     954             :   /// Change fields in place.
     955             :   void mutate(unsigned Tag, unsigned Line, unsigned RuntimeLang,
     956             :               uint64_t SizeInBits, uint32_t AlignInBits,
     957             :               uint64_t OffsetInBits, DIFlags Flags) {
     958             :     assert(isDistinct() && "Only distinct nodes can mutate");
     959             :     assert(getRawIdentifier() && "Only ODR-uniqued nodes should mutate");
     960           5 :     this->RuntimeLang = RuntimeLang;
     961             :     DIType::mutate(Tag, Line, SizeInBits, AlignInBits, OffsetInBits, Flags);
     962             :   }
     963             : 
     964             :   static DICompositeType *
     965        9318 :   getImpl(LLVMContext &Context, unsigned Tag, StringRef Name, Metadata *File,
     966             :           unsigned Line, DIScopeRef Scope, DITypeRef BaseType,
     967             :           uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits,
     968             :           DIFlags Flags, DINodeArray Elements, unsigned RuntimeLang,
     969             :           DITypeRef VTableHolder, DITemplateParameterArray TemplateParams,
     970             :           StringRef Identifier, DIDerivedType *Discriminator,
     971             :           StorageType Storage, bool ShouldCreate = true) {
     972       18636 :     return getImpl(
     973             :         Context, Tag, getCanonicalMDString(Context, Name), File, Line, Scope,
     974        9318 :         BaseType, SizeInBits, AlignInBits, OffsetInBits, Flags, Elements.get(),
     975        9318 :         RuntimeLang, VTableHolder, TemplateParams.get(),
     976        9318 :         getCanonicalMDString(Context, Identifier), Discriminator, Storage, ShouldCreate);
     977             :   }
     978             :   static DICompositeType *
     979             :   getImpl(LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
     980             :           unsigned Line, Metadata *Scope, Metadata *BaseType,
     981             :           uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits,
     982             :           DIFlags Flags, Metadata *Elements, unsigned RuntimeLang,
     983             :           Metadata *VTableHolder, Metadata *TemplateParams,
     984             :           MDString *Identifier, Metadata *Discriminator,
     985             :           StorageType Storage, bool ShouldCreate = true);
     986             : 
     987           3 :   TempDICompositeType cloneImpl() const {
     988             :     return getTemporary(getContext(), getTag(), getName(), getFile(), getLine(),
     989             :                         getScope(), getBaseType(), getSizeInBits(),
     990             :                         getAlignInBits(), getOffsetInBits(), getFlags(),
     991             :                         getElements(), getRuntimeLang(), getVTableHolder(),
     992           8 :                         getTemplateParams(), getIdentifier(), getDiscriminator());
     993             :   }
     994             : 
     995             : public:
     996        9315 :   DEFINE_MDNODE_GET(DICompositeType,
     997             :                     (unsigned Tag, StringRef Name, DIFile *File, unsigned Line,
     998             :                      DIScopeRef Scope, DITypeRef BaseType, uint64_t SizeInBits,
     999             :                      uint32_t AlignInBits, uint64_t OffsetInBits,
    1000             :                      DIFlags Flags, DINodeArray Elements, unsigned RuntimeLang,
    1001             :                      DITypeRef VTableHolder,
    1002             :                      DITemplateParameterArray TemplateParams = nullptr,
    1003             :                      StringRef Identifier = "", DIDerivedType *Discriminator = nullptr),
    1004             :                     (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
    1005             :                      AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
    1006             :                      VTableHolder, TemplateParams, Identifier, Discriminator))
    1007        1236 :   DEFINE_MDNODE_GET(DICompositeType,
    1008             :                     (unsigned Tag, MDString *Name, Metadata *File,
    1009             :                      unsigned Line, Metadata *Scope, Metadata *BaseType,
    1010             :                      uint64_t SizeInBits, uint32_t AlignInBits,
    1011             :                      uint64_t OffsetInBits, DIFlags Flags, Metadata *Elements,
    1012             :                      unsigned RuntimeLang, Metadata *VTableHolder,
    1013             :                      Metadata *TemplateParams = nullptr,
    1014             :                      MDString *Identifier = nullptr,
    1015             :                      Metadata *Discriminator = nullptr),
    1016             :                     (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
    1017             :                      AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
    1018             :                      VTableHolder, TemplateParams, Identifier, Discriminator))
    1019             : 
    1020           1 :   TempDICompositeType clone() const { return cloneImpl(); }
    1021             : 
    1022             :   /// Get a DICompositeType with the given ODR identifier.
    1023             :   ///
    1024             :   /// If \a LLVMContext::isODRUniquingDebugTypes(), gets the mapped
    1025             :   /// DICompositeType for the given ODR \c Identifier.  If none exists, creates
    1026             :   /// a new node.
    1027             :   ///
    1028             :   /// Else, returns \c nullptr.
    1029             :   static DICompositeType *
    1030             :   getODRType(LLVMContext &Context, MDString &Identifier, unsigned Tag,
    1031             :              MDString *Name, Metadata *File, unsigned Line, Metadata *Scope,
    1032             :              Metadata *BaseType, uint64_t SizeInBits, uint32_t AlignInBits,
    1033             :              uint64_t OffsetInBits, DIFlags Flags, Metadata *Elements,
    1034             :              unsigned RuntimeLang, Metadata *VTableHolder,
    1035             :              Metadata *TemplateParams, Metadata *Discriminator);
    1036             :   static DICompositeType *getODRTypeIfExists(LLVMContext &Context,
    1037             :                                              MDString &Identifier);
    1038             : 
    1039             :   /// Build a DICompositeType with the given ODR identifier.
    1040             :   ///
    1041             :   /// Looks up the mapped DICompositeType for the given ODR \c Identifier.  If
    1042             :   /// it doesn't exist, creates a new one.  If it does exist and \a
    1043             :   /// isForwardDecl(), and the new arguments would be a definition, mutates the
    1044             :   /// the type in place.  In either case, returns the type.
    1045             :   ///
    1046             :   /// If not \a LLVMContext::isODRUniquingDebugTypes(), this function returns
    1047             :   /// nullptr.
    1048             :   static DICompositeType *
    1049             :   buildODRType(LLVMContext &Context, MDString &Identifier, unsigned Tag,
    1050             :                MDString *Name, Metadata *File, unsigned Line, Metadata *Scope,
    1051             :                Metadata *BaseType, uint64_t SizeInBits, uint32_t AlignInBits,
    1052             :                uint64_t OffsetInBits, DIFlags Flags, Metadata *Elements,
    1053             :                unsigned RuntimeLang, Metadata *VTableHolder,
    1054             :                Metadata *TemplateParams, Metadata *Discriminator);
    1055             : 
    1056             :   DITypeRef getBaseType() const { return DITypeRef(getRawBaseType()); }
    1057             :   DINodeArray getElements() const {
    1058             :     return cast_or_null<MDTuple>(getRawElements());
    1059             :   }
    1060             :   DITypeRef getVTableHolder() const { return DITypeRef(getRawVTableHolder()); }
    1061             :   DITemplateParameterArray getTemplateParams() const {
    1062             :     return cast_or_null<MDTuple>(getRawTemplateParams());
    1063             :   }
    1064        2138 :   StringRef getIdentifier() const { return getStringOperand(7); }
    1065           0 :   unsigned getRuntimeLang() const { return RuntimeLang; }
    1066             : 
    1067       17482 :   Metadata *getRawBaseType() const { return getOperand(3); }
    1068        6780 :   Metadata *getRawElements() const { return getOperand(4); }
    1069           1 :   Metadata *getRawVTableHolder() const { return getOperand(5); }
    1070           4 :   Metadata *getRawTemplateParams() const { return getOperand(6); }
    1071             :   MDString *getRawIdentifier() const { return getOperandAs<MDString>(7); }
    1072             :   Metadata *getRawDiscriminator() const { return getOperand(8); }
    1073             :   DIDerivedType *getDiscriminator() const { return getOperandAs<DIDerivedType>(8); }
    1074             : 
    1075             :   /// Replace operands.
    1076             :   ///
    1077             :   /// If this \a isUniqued() and not \a isResolved(), on a uniquing collision
    1078             :   /// this will be RAUW'ed and deleted.  Use a \a TrackingMDRef to keep track
    1079             :   /// of its movement if necessary.
    1080             :   /// @{
    1081             :   void replaceElements(DINodeArray Elements) {
    1082             : #ifndef NDEBUG
    1083             :     for (DINode *Op : getElements())
    1084             :       assert(is_contained(Elements->operands(), Op) &&
    1085             :              "Lost a member during member list replacement");
    1086             : #endif
    1087        6158 :     replaceOperandWith(4, Elements.get());
    1088             :   }
    1089             : 
    1090             :   void replaceVTableHolder(DITypeRef VTableHolder) {
    1091        5891 :     replaceOperandWith(5, VTableHolder);
    1092             :   }
    1093             : 
    1094             :   void replaceTemplateParams(DITemplateParameterArray TemplateParams) {
    1095        4054 :     replaceOperandWith(6, TemplateParams.get());
    1096             :   }
    1097             :   /// @}
    1098             : 
    1099             :   static bool classof(const Metadata *MD) {
    1100       39288 :     return MD->getMetadataID() == DICompositeTypeKind;
    1101             :   }
    1102             : };
    1103             : 
    1104             : /// Type array for a subprogram.
    1105             : ///
    1106             : /// TODO: Fold the array of types in directly as operands.
    1107             : class DISubroutineType : public DIType {
    1108             :   friend class LLVMContextImpl;
    1109             :   friend class MDNode;
    1110             : 
    1111             :   /// The calling convention used with DW_AT_calling_convention. Actually of
    1112             :   /// type dwarf::CallingConvention.
    1113             :   uint8_t CC;
    1114             : 
    1115       46706 :   DISubroutineType(LLVMContext &C, StorageType Storage, DIFlags Flags,
    1116             :                    uint8_t CC, ArrayRef<Metadata *> Ops)
    1117       46706 :       : DIType(C, DISubroutineTypeKind, Storage, dwarf::DW_TAG_subroutine_type,
    1118             :                0, 0, 0, 0, Flags, Ops),
    1119       46706 :         CC(CC) {}
    1120        4221 :   ~DISubroutineType() = default;
    1121             : 
    1122             :   static DISubroutineType *getImpl(LLVMContext &Context, DIFlags Flags,
    1123             :                                    uint8_t CC, DITypeRefArray TypeArray,
    1124             :                                    StorageType Storage,
    1125             :                                    bool ShouldCreate = true) {
    1126       88179 :     return getImpl(Context, Flags, CC, TypeArray.get(), Storage, ShouldCreate);
    1127             :   }
    1128             :   static DISubroutineType *getImpl(LLVMContext &Context, DIFlags Flags,
    1129             :                                    uint8_t CC, Metadata *TypeArray,
    1130             :                                    StorageType Storage,
    1131             :                                    bool ShouldCreate = true);
    1132             : 
    1133           5 :   TempDISubroutineType cloneImpl() const {
    1134           5 :     return getTemporary(getContext(), getFlags(), getCC(), getTypeArray());
    1135             :   }
    1136             : 
    1137             : public:
    1138             :   DEFINE_MDNODE_GET(DISubroutineType,
    1139             :                     (DIFlags Flags, uint8_t CC, DITypeRefArray TypeArray),
    1140             :                     (Flags, CC, TypeArray))
    1141        2298 :   DEFINE_MDNODE_GET(DISubroutineType,
    1142             :                     (DIFlags Flags, uint8_t CC, Metadata *TypeArray),
    1143             :                     (Flags, CC, TypeArray))
    1144             : 
    1145           1 :   TempDISubroutineType clone() const { return cloneImpl(); }
    1146             : 
    1147           0 :   uint8_t getCC() const { return CC; }
    1148             : 
    1149             :   DITypeRefArray getTypeArray() const {
    1150             :     return cast_or_null<MDTuple>(getRawTypeArray());
    1151             :   }
    1152             : 
    1153      325189 :   Metadata *getRawTypeArray() const { return getOperand(3); }
    1154             : 
    1155             :   static bool classof(const Metadata *MD) {
    1156         142 :     return MD->getMetadataID() == DISubroutineTypeKind;
    1157             :   }
    1158             : };
    1159             : 
    1160             : /// Compile unit.
    1161             : class DICompileUnit : public DIScope {
    1162             :   friend class LLVMContextImpl;
    1163             :   friend class MDNode;
    1164             : 
    1165             : public:
    1166             :   enum DebugEmissionKind : unsigned {
    1167             :     NoDebug = 0,
    1168             :     FullDebug,
    1169             :     LineTablesOnly,
    1170             :     DebugDirectivesOnly,
    1171             :     LastEmissionKind = DebugDirectivesOnly
    1172             :   };
    1173             : 
    1174             :   enum class DebugNameTableKind : unsigned {
    1175             :     Default = 0,
    1176             :     GNU = 1,
    1177             :     None = 2,
    1178             :     LastDebugNameTableKind = None
    1179             :   };
    1180             : 
    1181             :   static Optional<DebugEmissionKind> getEmissionKind(StringRef Str);
    1182             :   static const char *emissionKindString(DebugEmissionKind EK);
    1183             :   static Optional<DebugNameTableKind> getNameTableKind(StringRef Str);
    1184             :   static const char *nameTableKindString(DebugNameTableKind PK);
    1185             : 
    1186             : private:
    1187             :   unsigned SourceLanguage;
    1188             :   bool IsOptimized;
    1189             :   unsigned RuntimeVersion;
    1190             :   unsigned EmissionKind;
    1191             :   uint64_t DWOId;
    1192             :   bool SplitDebugInlining;
    1193             :   bool DebugInfoForProfiling;
    1194             :   unsigned NameTableKind;
    1195             : 
    1196             :   DICompileUnit(LLVMContext &C, StorageType Storage, unsigned SourceLanguage,
    1197             :                 bool IsOptimized, unsigned RuntimeVersion,
    1198             :                 unsigned EmissionKind, uint64_t DWOId, bool SplitDebugInlining,
    1199             :                 bool DebugInfoForProfiling, unsigned NameTableKind,
    1200             :                 ArrayRef<Metadata *> Ops)
    1201        4178 :       : DIScope(C, DICompileUnitKind, Storage, dwarf::DW_TAG_compile_unit, Ops),
    1202             :         SourceLanguage(SourceLanguage), IsOptimized(IsOptimized),
    1203             :         RuntimeVersion(RuntimeVersion), EmissionKind(EmissionKind),
    1204             :         DWOId(DWOId), SplitDebugInlining(SplitDebugInlining),
    1205             :         DebugInfoForProfiling(DebugInfoForProfiling),
    1206        4178 :         NameTableKind(NameTableKind) {
    1207             :     assert(Storage != Uniqued);
    1208             :   }
    1209        3850 :   ~DICompileUnit() = default;
    1210             : 
    1211             :   static DICompileUnit *
    1212        1729 :   getImpl(LLVMContext &Context, unsigned SourceLanguage, DIFile *File,
    1213             :           StringRef Producer, bool IsOptimized, StringRef Flags,
    1214             :           unsigned RuntimeVersion, StringRef SplitDebugFilename,
    1215             :           unsigned EmissionKind, DICompositeTypeArray EnumTypes,
    1216             :           DIScopeArray RetainedTypes,
    1217             :           DIGlobalVariableExpressionArray GlobalVariables,
    1218             :           DIImportedEntityArray ImportedEntities, DIMacroNodeArray Macros,
    1219             :           uint64_t DWOId, bool SplitDebugInlining, bool DebugInfoForProfiling,
    1220             :           unsigned NameTableKind, StorageType Storage,
    1221             :           bool ShouldCreate = true) {
    1222        3335 :     return getImpl(
    1223             :         Context, SourceLanguage, File, getCanonicalMDString(Context, Producer),
    1224             :         IsOptimized, getCanonicalMDString(Context, Flags), RuntimeVersion,
    1225             :         getCanonicalMDString(Context, SplitDebugFilename), EmissionKind,
    1226        1729 :         EnumTypes.get(), RetainedTypes.get(), GlobalVariables.get(),
    1227        1729 :         ImportedEntities.get(), Macros.get(), DWOId, SplitDebugInlining,
    1228        1729 :         DebugInfoForProfiling, NameTableKind, Storage, ShouldCreate);
    1229             :   }
    1230             :   static DICompileUnit *
    1231             :   getImpl(LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
    1232             :           MDString *Producer, bool IsOptimized, MDString *Flags,
    1233             :           unsigned RuntimeVersion, MDString *SplitDebugFilename,
    1234             :           unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
    1235             :           Metadata *GlobalVariables, Metadata *ImportedEntities,
    1236             :           Metadata *Macros, uint64_t DWOId, bool SplitDebugInlining,
    1237             :           bool DebugInfoForProfiling, unsigned NameTableKind,
    1238             :           StorageType Storage, bool ShouldCreate = true);
    1239             : 
    1240         117 :   TempDICompileUnit cloneImpl() const {
    1241             :     return getTemporary(getContext(), getSourceLanguage(), getFile(),
    1242         117 :                         getProducer(), isOptimized(), getFlags(),
    1243             :                         getRuntimeVersion(), getSplitDebugFilename(),
    1244             :                         getEmissionKind(), getEnumTypes(), getRetainedTypes(),
    1245             :                         getGlobalVariables(), getImportedEntities(),
    1246         234 :                         getMacros(), DWOId, getSplitDebugInlining(),
    1247         117 :                         getDebugInfoForProfiling(), getNameTableKind());
    1248             :   }
    1249             : 
    1250             : public:
    1251             :   static void get() = delete;
    1252             :   static void getIfExists() = delete;
    1253             : 
    1254        1729 :   DEFINE_MDNODE_GET_DISTINCT_TEMPORARY(
    1255             :       DICompileUnit,
    1256             :       (unsigned SourceLanguage, DIFile *File, StringRef Producer,
    1257             :        bool IsOptimized, StringRef Flags, unsigned RuntimeVersion,
    1258             :        StringRef SplitDebugFilename, DebugEmissionKind EmissionKind,
    1259             :        DICompositeTypeArray EnumTypes, DIScopeArray RetainedTypes,
    1260             :        DIGlobalVariableExpressionArray GlobalVariables,
    1261             :        DIImportedEntityArray ImportedEntities, DIMacroNodeArray Macros,
    1262             :        uint64_t DWOId, bool SplitDebugInlining, bool DebugInfoForProfiling,
    1263             :        DebugNameTableKind NameTableKind),
    1264             :       (SourceLanguage, File, Producer, IsOptimized, Flags, RuntimeVersion,
    1265             :        SplitDebugFilename, EmissionKind, EnumTypes, RetainedTypes,
    1266             :        GlobalVariables, ImportedEntities, Macros, DWOId, SplitDebugInlining,
    1267             :        DebugInfoForProfiling, (unsigned)NameTableKind))
    1268        2449 :   DEFINE_MDNODE_GET_DISTINCT_TEMPORARY(
    1269             :       DICompileUnit,
    1270             :       (unsigned SourceLanguage, Metadata *File, MDString *Producer,
    1271             :        bool IsOptimized, MDString *Flags, unsigned RuntimeVersion,
    1272             :        MDString *SplitDebugFilename, unsigned EmissionKind, Metadata *EnumTypes,
    1273             :        Metadata *RetainedTypes, Metadata *GlobalVariables,
    1274             :        Metadata *ImportedEntities, Metadata *Macros, uint64_t DWOId,
    1275             :        bool SplitDebugInlining, bool DebugInfoForProfiling,
    1276             :        unsigned NameTableKind),
    1277             :       (SourceLanguage, File, Producer, IsOptimized, Flags, RuntimeVersion,
    1278             :        SplitDebugFilename, EmissionKind, EnumTypes, RetainedTypes,
    1279             :        GlobalVariables, ImportedEntities, Macros, DWOId, SplitDebugInlining,
    1280             :        DebugInfoForProfiling, NameTableKind))
    1281             : 
    1282           1 :   TempDICompileUnit clone() const { return cloneImpl(); }
    1283             : 
    1284           0 :   unsigned getSourceLanguage() const { return SourceLanguage; }
    1285           0 :   bool isOptimized() const { return IsOptimized; }
    1286           0 :   unsigned getRuntimeVersion() const { return RuntimeVersion; }
    1287           0 :   DebugEmissionKind getEmissionKind() const {
    1288           0 :     return (DebugEmissionKind)EmissionKind;
    1289             :   }
    1290           0 :   bool isDebugDirectivesOnly() const {
    1291           0 :     return EmissionKind == DebugDirectivesOnly;
    1292             :   }
    1293           0 :   bool getDebugInfoForProfiling() const { return DebugInfoForProfiling; }
    1294           0 :   DebugNameTableKind getNameTableKind() const {
    1295           0 :     return (DebugNameTableKind)NameTableKind;
    1296             :   }
    1297        2811 :   StringRef getProducer() const { return getStringOperand(1); }
    1298        2881 :   StringRef getFlags() const { return getStringOperand(2); }
    1299        1348 :   StringRef getSplitDebugFilename() const { return getStringOperand(3); }
    1300             :   DICompositeTypeArray getEnumTypes() const {
    1301             :     return cast_or_null<MDTuple>(getRawEnumTypes());
    1302             :   }
    1303             :   DIScopeArray getRetainedTypes() const {
    1304             :     return cast_or_null<MDTuple>(getRawRetainedTypes());
    1305             :   }
    1306             :   DIGlobalVariableExpressionArray getGlobalVariables() const {
    1307             :     return cast_or_null<MDTuple>(getRawGlobalVariables());
    1308             :   }
    1309             :   DIImportedEntityArray getImportedEntities() const {
    1310             :     return cast_or_null<MDTuple>(getRawImportedEntities());
    1311             :   }
    1312             :   DIMacroNodeArray getMacros() const {
    1313             :     return cast_or_null<MDTuple>(getRawMacros());
    1314             :   }
    1315           0 :   uint64_t getDWOId() const { return DWOId; }
    1316          23 :   void setDWOId(uint64_t DwoId) { DWOId = DwoId; }
    1317           0 :   bool getSplitDebugInlining() const { return SplitDebugInlining; }
    1318             :   void setSplitDebugInlining(bool SplitDebugInlining) {
    1319             :     this->SplitDebugInlining = SplitDebugInlining;
    1320             :   }
    1321             : 
    1322             :   MDString *getRawProducer() const { return getOperandAs<MDString>(1); }
    1323             :   MDString *getRawFlags() const { return getOperandAs<MDString>(2); }
    1324             :   MDString *getRawSplitDebugFilename() const {
    1325             :     return getOperandAs<MDString>(3);
    1326             :   }
    1327       10648 :   Metadata *getRawEnumTypes() const { return getOperand(4); }
    1328         132 :   Metadata *getRawRetainedTypes() const { return getOperand(5); }
    1329         236 :   Metadata *getRawGlobalVariables() const { return getOperand(6); }
    1330        2744 :   Metadata *getRawImportedEntities() const { return getOperand(7); }
    1331        2813 :   Metadata *getRawMacros() const { return getOperand(8); }
    1332             : 
    1333             :   /// Replace arrays.
    1334             :   ///
    1335             :   /// If this \a isUniqued() and not \a isResolved(), it will be RAUW'ed and
    1336             :   /// deleted on a uniquing collision.  In practice, uniquing collisions on \a
    1337             :   /// DICompileUnit should be fairly rare.
    1338             :   /// @{
    1339             :   void replaceEnumTypes(DICompositeTypeArray N) {
    1340        1594 :     replaceOperandWith(4, N.get());
    1341             :   }
    1342             :   void replaceRetainedTypes(DITypeArray N) {
    1343         166 :     replaceOperandWith(5, N.get());
    1344             :   }
    1345             :   void replaceGlobalVariables(DIGlobalVariableExpressionArray N) {
    1346         240 :     replaceOperandWith(6, N.get());
    1347             :   }
    1348             :   void replaceImportedEntities(DIImportedEntityArray N) {
    1349          96 :     replaceOperandWith(7, N.get());
    1350             :   }
    1351           8 :   void replaceMacros(DIMacroNodeArray N) { replaceOperandWith(8, N.get()); }
    1352             :   /// @}
    1353             : 
    1354             :   static bool classof(const Metadata *MD) {
    1355      154912 :     return MD->getMetadataID() == DICompileUnitKind;
    1356             :   }
    1357             : };
    1358             : 
    1359             : /// A scope for locals.
    1360             : ///
    1361             : /// A legal scope for lexical blocks, local variables, and debug info
    1362             : /// locations.  Subclasses are \a DISubprogram, \a DILexicalBlock, and \a
    1363             : /// DILexicalBlockFile.
    1364             : class DILocalScope : public DIScope {
    1365             : protected:
    1366             :   DILocalScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
    1367             :                ArrayRef<Metadata *> Ops)
    1368             :       : DIScope(C, ID, Storage, Tag, Ops) {}
    1369             :   ~DILocalScope() = default;
    1370             : 
    1371             : public:
    1372             :   /// Get the subprogram for this scope.
    1373             :   ///
    1374             :   /// Return this if it's an \a DISubprogram; otherwise, look up the scope
    1375             :   /// chain.
    1376             :   DISubprogram *getSubprogram() const;
    1377             : 
    1378             :   /// Get the first non DILexicalBlockFile scope of this scope.
    1379             :   ///
    1380             :   /// Return this if it's not a \a DILexicalBlockFIle; otherwise, look up the
    1381             :   /// scope chain.
    1382             :   DILocalScope *getNonLexicalBlockFileScope() const;
    1383             : 
    1384             :   static bool classof(const Metadata *MD) {
    1385      676241 :     return MD->getMetadataID() == DISubprogramKind ||
    1386      730119 :            MD->getMetadataID() == DILexicalBlockKind ||
    1387             :            MD->getMetadataID() == DILexicalBlockFileKind;
    1388             :   }
    1389             : };
    1390             : 
    1391             : /// Debug location.
    1392             : ///
    1393             : /// A debug location in source code, used for debug info and otherwise.
    1394             : class DILocation : public MDNode {
    1395             :   friend class LLVMContextImpl;
    1396             :   friend class MDNode;
    1397             : 
    1398             :   DILocation(LLVMContext &C, StorageType Storage, unsigned Line,
    1399             :              unsigned Column, ArrayRef<Metadata *> MDs, bool ImplicitCode);
    1400       53346 :   ~DILocation() { dropAllReferences(); }
    1401             : 
    1402             :   static DILocation *getImpl(LLVMContext &Context, unsigned Line,
    1403             :                              unsigned Column, Metadata *Scope,
    1404             :                              Metadata *InlinedAt, bool ImplicitCode,
    1405             :                              StorageType Storage, bool ShouldCreate = true);
    1406             :   static DILocation *getImpl(LLVMContext &Context, unsigned Line,
    1407             :                              unsigned Column, DILocalScope *Scope,
    1408             :                              DILocation *InlinedAt, bool ImplicitCode,
    1409             :                              StorageType Storage, bool ShouldCreate = true) {
    1410      261883 :     return getImpl(Context, Line, Column, static_cast<Metadata *>(Scope),
    1411             :                    static_cast<Metadata *>(InlinedAt), ImplicitCode, Storage,
    1412             :                    ShouldCreate);
    1413             :   }
    1414             : 
    1415             :   /// With a given unsigned int \p U, use up to 13 bits to represent it.
    1416             :   /// old_bit 1~5  --> new_bit 1~5
    1417             :   /// old_bit 6~12 --> new_bit 7~13
    1418             :   /// new_bit_6 is 0 if higher bits (7~13) are all 0
    1419             :   static unsigned getPrefixEncodingFromUnsigned(unsigned U) {
    1420         298 :     U &= 0xfff;
    1421         246 :     return U > 0x1f ? (((U & 0xfe0) << 1) | (U & 0x1f) | 0x20) : U;
    1422             :   }
    1423             : 
    1424             :   /// Reverse transformation as getPrefixEncodingFromUnsigned.
    1425             :   static unsigned getUnsignedFromPrefixEncoding(unsigned U) {
    1426        1559 :     return (U & 0x20) ? (((U >> 1) & 0xfe0) | (U & 0x1f)) : (U & 0x1f);
    1427             :   }
    1428             : 
    1429             :   /// Returns the next component stored in discriminator.
    1430             :   static unsigned getNextComponentInDiscriminator(unsigned D) {
    1431         110 :     if ((D & 1) == 0)
    1432         428 :       return D >> ((D & 0x40) ? 14 : 7);
    1433             :     else
    1434         116 :       return D >> 1;
    1435             :   }
    1436             : 
    1437         196 :   TempDILocation cloneImpl() const {
    1438             :     // Get the raw scope/inlinedAt since it is possible to invoke this on
    1439             :     // a DILocation containing temporary metadata.
    1440             :     return getTemporary(getContext(), getLine(), getColumn(), getRawScope(),
    1441         784 :                         getRawInlinedAt(), isImplicitCode());
    1442             :   }
    1443             : 
    1444             : public:
    1445             :   // Disallow replacing operands.
    1446             :   void replaceOperandWith(unsigned I, Metadata *New) = delete;
    1447             : 
    1448     3709359 :   DEFINE_MDNODE_GET(DILocation,
    1449             :                     (unsigned Line, unsigned Column, Metadata *Scope,
    1450             :                      Metadata *InlinedAt = nullptr, bool ImplicitCode = false),
    1451             :                     (Line, Column, Scope, InlinedAt, ImplicitCode))
    1452             :   DEFINE_MDNODE_GET(DILocation,
    1453             :                     (unsigned Line, unsigned Column, DILocalScope *Scope,
    1454             :                      DILocation *InlinedAt = nullptr,
    1455             :                      bool ImplicitCode = false),
    1456             :                     (Line, Column, Scope, InlinedAt, ImplicitCode))
    1457             : 
    1458             :   /// Return a (temporary) clone of this.
    1459           1 :   TempDILocation clone() const { return cloneImpl(); }
    1460             : 
    1461           0 :   unsigned getLine() const { return SubclassData32; }
    1462     5937169 :   unsigned getColumn() const { return SubclassData16; }
    1463             :   DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); }
    1464             : 
    1465             :   DILocation *getInlinedAt() const {
    1466             :     return cast_or_null<DILocation>(getRawInlinedAt());
    1467             :   }
    1468             : 
    1469             :   /// Check if the location corresponds to an implicit code.
    1470             :   /// When the ImplicitCode flag is true, it means that the Instruction
    1471             :   /// with this DILocation has been added by the front-end but it hasn't been
    1472             :   /// written explicitly by the user (e.g. cleanup stuff in C++ put on a closing
    1473             :   /// bracket). It's useful for code coverage to not show a counter on "empty"
    1474             :   /// lines.
    1475     5999411 :   bool isImplicitCode() const { return ImplicitCode; }
    1476     1502103 :   void setImplicitCode(bool ImplicitCode) { this->ImplicitCode = ImplicitCode; }
    1477             : 
    1478             :   DIFile *getFile() const { return getScope()->getFile(); }
    1479      100294 :   StringRef getFilename() const { return getScope()->getFilename(); }
    1480           0 :   StringRef getDirectory() const { return getScope()->getDirectory(); }
    1481             :   Optional<StringRef> getSource() const { return getScope()->getSource(); }
    1482             : 
    1483             :   /// Get the scope where this is inlined.
    1484             :   ///
    1485             :   /// Walk through \a getInlinedAt() and return \a getScope() from the deepest
    1486             :   /// location.
    1487             :   DILocalScope *getInlinedAtScope() const {
    1488             :     if (auto *IA = getInlinedAt())
    1489             :       return IA->getInlinedAtScope();
    1490             :     return getScope();
    1491             :   }
    1492             : 
    1493             :   /// Get the DWARF discriminator.
    1494             :   ///
    1495             :   /// DWARF discriminators distinguish identical file locations between
    1496             :   /// instructions that are on different basic blocks.
    1497             :   ///
    1498             :   /// There are 3 components stored in discriminator, from lower bits:
    1499             :   ///
    1500             :   /// Base discriminator: assigned by AddDiscriminators pass to identify IRs
    1501             :   ///                     that are defined by the same source line, but
    1502             :   ///                     different basic blocks.
    1503             :   /// Duplication factor: assigned by optimizations that will scale down
    1504             :   ///                     the execution frequency of the original IR.
    1505             :   /// Copy Identifier: assigned by optimizations that clones the IR.
    1506             :   ///                  Each copy of the IR will be assigned an identifier.
    1507             :   ///
    1508             :   /// Encoding:
    1509             :   ///
    1510             :   /// The above 3 components are encoded into a 32bit unsigned integer in
    1511             :   /// order. If the lowest bit is 1, the current component is empty, and the
    1512             :   /// next component will start in the next bit. Otherwise, the current
    1513             :   /// component is non-empty, and its content starts in the next bit. The
    1514             :   /// length of each components is either 5 bit or 12 bit: if the 7th bit
    1515             :   /// is 0, the bit 2~6 (5 bits) are used to represent the component; if the
    1516             :   /// 7th bit is 1, the bit 2~6 (5 bits) and 8~14 (7 bits) are combined to
    1517             :   /// represent the component.
    1518             : 
    1519             :   inline unsigned getDiscriminator() const;
    1520             : 
    1521             :   /// Returns a new DILocation with updated \p Discriminator.
    1522             :   inline const DILocation *cloneWithDiscriminator(unsigned Discriminator) const;
    1523             : 
    1524             :   /// Returns a new DILocation with updated base discriminator \p BD.
    1525             :   inline const DILocation *setBaseDiscriminator(unsigned BD) const;
    1526             : 
    1527             :   /// Returns the duplication factor stored in the discriminator.
    1528             :   inline unsigned getDuplicationFactor() const;
    1529             : 
    1530             :   /// Returns the copy identifier stored in the discriminator.
    1531             :   inline unsigned getCopyIdentifier() const;
    1532             : 
    1533             :   /// Returns the base discriminator stored in the discriminator.
    1534             :   inline unsigned getBaseDiscriminator() const;
    1535             : 
    1536             :   /// Returns a new DILocation with duplication factor \p DF encoded in the
    1537             :   /// discriminator.
    1538             :   inline const DILocation *cloneWithDuplicationFactor(unsigned DF) const;
    1539             : 
    1540             :   /// When two instructions are combined into a single instruction we also
    1541             :   /// need to combine the original locations into a single location.
    1542             :   ///
    1543             :   /// When the locations are the same we can use either location. When they
    1544             :   /// differ, we need a third location which is distinct from either. If they
    1545             :   /// have the same file/line but have a different discriminator we could
    1546             :   /// create a location with a new discriminator. If they are from different
    1547             :   /// files/lines the location is ambiguous and can't be represented in a line
    1548             :   /// entry. In this case, if \p GenerateLocation is true, we will set the
    1549             :   /// merged debug location as line 0 of the nearest common scope where the two
    1550             :   /// locations are inlined from.
    1551             :   ///
    1552             :   /// \p GenerateLocation: Whether the merged location can be generated when
    1553             :   /// \p LocA and \p LocB differ.
    1554             :   static const DILocation *getMergedLocation(const DILocation *LocA,
    1555             :                                              const DILocation *LocB);
    1556             : 
    1557             :   /// Returns the base discriminator for a given encoded discriminator \p D.
    1558             :   static unsigned getBaseDiscriminatorFromDiscriminator(unsigned D) {
    1559        1457 :     if ((D & 1) == 0)
    1560        1391 :       return getUnsignedFromPrefixEncoding(D >> 1);
    1561             :     else
    1562             :       return 0;
    1563             :   }
    1564             : 
    1565             :   /// Returns the duplication factor for a given encoded discriminator \p D.
    1566         110 :   static unsigned getDuplicationFactorFromDiscriminator(unsigned D) {
    1567             :     D = getNextComponentInDiscriminator(D);
    1568         110 :     if (D == 0 || (D & 1))
    1569             :       return 1;
    1570             :     else
    1571          58 :       return getUnsignedFromPrefixEncoding(D >> 1);
    1572             :   }
    1573             : 
    1574             :   /// Returns the copy identifier for a given encoded discriminator \p D.
    1575         110 :   static unsigned getCopyIdentifierFromDiscriminator(unsigned D) {
    1576             :     return getUnsignedFromPrefixEncoding(getNextComponentInDiscriminator(
    1577         110 :         getNextComponentInDiscriminator(D)));
    1578             :   }
    1579             : 
    1580             : 
    1581    11272522 :   Metadata *getRawScope() const { return getOperand(0); }
    1582             :   Metadata *getRawInlinedAt() const {
    1583    14589122 :     if (getNumOperands() == 2)
    1584             :       return getOperand(1);
    1585             :     return nullptr;
    1586             :   }
    1587             : 
    1588             :   static bool classof(const Metadata *MD) {
    1589      210195 :     return MD->getMetadataID() == DILocationKind;
    1590             :   }
    1591             : };
    1592             : 
    1593             : /// Subprogram description.
    1594             : ///
    1595             : /// TODO: Remove DisplayName.  It's always equal to Name.
    1596             : /// TODO: Split up flags.
    1597             : class DISubprogram : public DILocalScope {
    1598             :   friend class LLVMContextImpl;
    1599             :   friend class MDNode;
    1600             : 
    1601             :   unsigned Line;
    1602             :   unsigned ScopeLine;
    1603             :   unsigned VirtualIndex;
    1604             : 
    1605             :   /// In the MS ABI, the implicit 'this' parameter is adjusted in the prologue
    1606             :   /// of method overrides from secondary bases by this amount. It may be
    1607             :   /// negative.
    1608             :   int ThisAdjustment;
    1609             : 
    1610             :   // Virtuality can only assume three values, so we can pack
    1611             :   // in 2 bits (none/pure/pure_virtual).
    1612             :   unsigned Virtuality : 2;
    1613             : 
    1614             :   // These are boolean flags so one bit is enough.
    1615             :   // MSVC starts a new container field every time the base
    1616             :   // type changes so we can't use 'bool' to ensure these bits
    1617             :   // are packed.
    1618             :   unsigned IsLocalToUnit : 1;
    1619             :   unsigned IsDefinition : 1;
    1620             :   unsigned IsOptimized : 1;
    1621             : 
    1622             :   unsigned Padding : 3;
    1623             : 
    1624             :   DIFlags Flags;
    1625             : 
    1626             :   DISubprogram(LLVMContext &C, StorageType Storage, unsigned Line,
    1627             :                unsigned ScopeLine, unsigned Virtuality, unsigned VirtualIndex,
    1628             :                int ThisAdjustment, DIFlags Flags, bool IsLocalToUnit,
    1629             :                bool IsDefinition, bool IsOptimized, ArrayRef<Metadata *> Ops)
    1630       83945 :       : DILocalScope(C, DISubprogramKind, Storage, dwarf::DW_TAG_subprogram,
    1631             :                      Ops),
    1632             :         Line(Line), ScopeLine(ScopeLine), VirtualIndex(VirtualIndex),
    1633             :         ThisAdjustment(ThisAdjustment), Virtuality(Virtuality),
    1634             :         IsLocalToUnit(IsLocalToUnit), IsDefinition(IsDefinition),
    1635       83945 :         IsOptimized(IsOptimized), Flags(Flags) {
    1636             :     static_assert(dwarf::DW_VIRTUALITY_max < 4, "Virtuality out of range");
    1637             :     assert(Virtuality < 4 && "Virtuality out of range");
    1638             :   }
    1639        8509 :   ~DISubprogram() = default;
    1640             : 
    1641             :   static DISubprogram *
    1642       80498 :   getImpl(LLVMContext &Context, DIScopeRef Scope, StringRef Name,
    1643             :           StringRef LinkageName, DIFile *File, unsigned Line,
    1644             :           DISubroutineType *Type, bool IsLocalToUnit, bool IsDefinition,
    1645             :           unsigned ScopeLine, DITypeRef ContainingType, unsigned Virtuality,
    1646             :           unsigned VirtualIndex, int ThisAdjustment, DIFlags Flags,
    1647             :           bool IsOptimized, DICompileUnit *Unit,
    1648             :           DITemplateParameterArray TemplateParams, DISubprogram *Declaration,
    1649             :           DINodeArray RetainedNodes, DITypeArray ThrownTypes,
    1650             :           StorageType Storage, bool ShouldCreate = true) {
    1651      160530 :     return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
    1652             :                    getCanonicalMDString(Context, LinkageName), File, Line, Type,
    1653             :                    IsLocalToUnit, IsDefinition, ScopeLine, ContainingType,
    1654             :                    Virtuality, VirtualIndex, ThisAdjustment, Flags, IsOptimized,
    1655       80498 :                    Unit, TemplateParams.get(), Declaration, RetainedNodes.get(),
    1656       80498 :                    ThrownTypes.get(), Storage, ShouldCreate);
    1657             :   }
    1658             :   static DISubprogram *
    1659             :   getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
    1660             :           MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
    1661             :           bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
    1662             :           Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
    1663             :           int ThisAdjustment, DIFlags Flags, bool IsOptimized, Metadata *Unit,
    1664             :           Metadata *TemplateParams, Metadata *Declaration, Metadata *RetainedNodes,
    1665             :           Metadata *ThrownTypes, StorageType Storage, bool ShouldCreate = true);
    1666             : 
    1667          35 :   TempDISubprogram cloneImpl() const {
    1668             :     return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
    1669             :                         getFile(), getLine(), getType(), isLocalToUnit(),
    1670             :                         isDefinition(), getScopeLine(), getContainingType(),
    1671             :                         getVirtuality(), getVirtualIndex(), getThisAdjustment(),
    1672             :                         getFlags(), isOptimized(), getUnit(),
    1673             :                         getTemplateParams(), getDeclaration(), getRetainedNodes(),
    1674          35 :                         getThrownTypes());
    1675             :   }
    1676             : 
    1677             : public:
    1678       80491 :   DEFINE_MDNODE_GET(DISubprogram,
    1679             :                     (DIScopeRef Scope, StringRef Name, StringRef LinkageName,
    1680             :                      DIFile *File, unsigned Line, DISubroutineType *Type,
    1681             :                      bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
    1682             :                      DITypeRef ContainingType, unsigned Virtuality,
    1683             :                      unsigned VirtualIndex, int ThisAdjustment, DIFlags Flags,
    1684             :                      bool IsOptimized, DICompileUnit *Unit,
    1685             :                      DITemplateParameterArray TemplateParams = nullptr,
    1686             :                      DISubprogram *Declaration = nullptr,
    1687             :                      DINodeArray RetainedNodes = nullptr,
    1688             :                      DITypeArray ThrownTypes = nullptr),
    1689             :                     (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
    1690             :                      IsDefinition, ScopeLine, ContainingType, Virtuality,
    1691             :                      VirtualIndex, ThisAdjustment, Flags, IsOptimized, Unit,
    1692             :                      TemplateParams, Declaration, RetainedNodes, ThrownTypes))
    1693        3455 :   DEFINE_MDNODE_GET(
    1694             :       DISubprogram,
    1695             :       (Metadata * Scope, MDString *Name, MDString *LinkageName, Metadata *File,
    1696             :        unsigned Line, Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
    1697             :        unsigned ScopeLine, Metadata *ContainingType, unsigned Virtuality,
    1698             :        unsigned VirtualIndex, int ThisAdjustment, DIFlags Flags,
    1699             :        bool IsOptimized, Metadata *Unit, Metadata *TemplateParams = nullptr,
    1700             :        Metadata *Declaration = nullptr, Metadata *RetainedNodes = nullptr,
    1701             :        Metadata *ThrownTypes = nullptr),
    1702             :       (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, IsDefinition,
    1703             :        ScopeLine, ContainingType, Virtuality, VirtualIndex, ThisAdjustment,
    1704             :        Flags, IsOptimized, Unit, TemplateParams, Declaration, RetainedNodes,
    1705             :        ThrownTypes))
    1706             : 
    1707           4 :   TempDISubprogram clone() const { return cloneImpl(); }
    1708             : 
    1709             :   /// Returns a new temporary DISubprogram with updated Flags
    1710             :   TempDISubprogram cloneWithFlags(DIFlags NewFlags) const {
    1711             :     auto NewSP = clone();
    1712           1 :     NewSP->Flags = NewFlags;
    1713             :     return NewSP;
    1714             :   }
    1715             : 
    1716             : public:
    1717           0 :   unsigned getLine() const { return Line; }
    1718      161911 :   unsigned getVirtuality() const { return Virtuality; }
    1719           0 :   unsigned getVirtualIndex() const { return VirtualIndex; }
    1720           0 :   int getThisAdjustment() const { return ThisAdjustment; }
    1721           0 :   unsigned getScopeLine() const { return ScopeLine; }
    1722           0 :   DIFlags getFlags() const { return Flags; }
    1723      161889 :   bool isLocalToUnit() const { return IsLocalToUnit; }
    1724      460298 :   bool isDefinition() const { return IsDefinition; }
    1725      121289 :   bool isOptimized() const { return IsOptimized; }
    1726             : 
    1727       40936 :   bool isArtificial() const { return getFlags() & FlagArtificial; }
    1728             :   bool isPrivate() const {
    1729             :     return (getFlags() & FlagAccessibility) == FlagPrivate;
    1730             :   }
    1731             :   bool isProtected() const {
    1732       40867 :     return (getFlags() & FlagAccessibility) == FlagProtected;
    1733             :   }
    1734             :   bool isPublic() const {
    1735             :     return (getFlags() & FlagAccessibility) == FlagPublic;
    1736             :   }
    1737       40867 :   bool isExplicit() const { return getFlags() & FlagExplicit; }
    1738       40867 :   bool isPrototyped() const { return getFlags() & FlagPrototyped; }
    1739             :   bool areAllCallsDescribed() const {
    1740     1448799 :     return getFlags() & FlagAllCallsDescribed;
    1741             :   }
    1742       40867 :   bool isMainSubprogram() const { return getFlags() & FlagMainSubprogram; }
    1743             : 
    1744             :   /// Check if this is reference-qualified.
    1745             :   ///
    1746             :   /// Return true if this subprogram is a C++11 reference-qualified non-static
    1747             :   /// member function (void foo() &).
    1748       40867 :   bool isLValueReference() const { return getFlags() & FlagLValueReference; }
    1749             : 
    1750             :   /// Check if this is rvalue-reference-qualified.
    1751             :   ///
    1752             :   /// Return true if this subprogram is a C++11 rvalue-reference-qualified
    1753             :   /// non-static member function (void foo() &&).
    1754       40867 :   bool isRValueReference() const { return getFlags() & FlagRValueReference; }
    1755             : 
    1756             :   /// Check if this is marked as noreturn.
    1757             :   ///
    1758             :   /// Return true if this subprogram is C++11 noreturn or C11 _Noreturn
    1759       40867 :   bool isNoReturn() const { return getFlags() & FlagNoReturn; }
    1760             : 
    1761             :   // Check if this routine is a compiler-generated thunk.
    1762             :   //
    1763             :   // Returns true if this subprogram is a thunk generated by the compiler.
    1764         237 :   bool isThunk() const { return getFlags() & FlagThunk; }
    1765             : 
    1766             :   DIScopeRef getScope() const { return DIScopeRef(getRawScope()); }
    1767             : 
    1768      158731 :   StringRef getName() const { return getStringOperand(2); }
    1769      314044 :   StringRef getLinkageName() const { return getStringOperand(3); }
    1770             : 
    1771             :   DISubroutineType *getType() const {
    1772             :     return cast_or_null<DISubroutineType>(getRawType());
    1773             :   }
    1774             :   DITypeRef getContainingType() const {
    1775             :     return DITypeRef(getRawContainingType());
    1776             :   }
    1777             : 
    1778             :   DICompileUnit *getUnit() const {
    1779             :     return cast_or_null<DICompileUnit>(getRawUnit());
    1780             :   }
    1781          21 :   void replaceUnit(DICompileUnit *CU) { replaceOperandWith(5, CU); }
    1782             :   DITemplateParameterArray getTemplateParams() const {
    1783             :     return cast_or_null<MDTuple>(getRawTemplateParams());
    1784             :   }
    1785             :   DISubprogram *getDeclaration() const {
    1786             :     return cast_or_null<DISubprogram>(getRawDeclaration());
    1787             :   }
    1788             :   DINodeArray getRetainedNodes() const {
    1789             :     return cast_or_null<MDTuple>(getRawRetainedNodes());
    1790             :   }
    1791             :   DITypeArray getThrownTypes() const {
    1792             :     return cast_or_null<MDTuple>(getRawThrownTypes());
    1793             :   }
    1794             : 
    1795      625109 :   Metadata *getRawScope() const { return getOperand(1); }
    1796             :   MDString *getRawName() const { return getOperandAs<MDString>(2); }
    1797             :   MDString *getRawLinkageName() const { return getOperandAs<MDString>(3); }
    1798       49379 :   Metadata *getRawType() const { return getOperand(4); }
    1799     4593195 :   Metadata *getRawUnit() const { return getOperand(5); }
    1800      127704 :   Metadata *getRawDeclaration() const { return getOperand(6); }
    1801      151826 :   Metadata *getRawRetainedNodes() const { return getOperand(7); }
    1802             :   Metadata *getRawContainingType() const {
    1803      136606 :     return getNumOperands() > 8 ? getOperandAs<Metadata>(8) : nullptr;
    1804             :   }
    1805             :   Metadata *getRawTemplateParams() const {
    1806      259077 :     return getNumOperands() > 9 ? getOperandAs<Metadata>(9) : nullptr;
    1807             :   }
    1808             :   Metadata *getRawThrownTypes() const {
    1809      176565 :     return getNumOperands() > 10 ? getOperandAs<Metadata>(10) : nullptr;
    1810             :   }
    1811             : 
    1812             :   /// Check if this subprogram describes the given function.
    1813             :   ///
    1814             :   /// FIXME: Should this be looking through bitcasts?
    1815             :   bool describes(const Function *F) const;
    1816             : 
    1817             :   static bool classof(const Metadata *MD) {
    1818      687478 :     return MD->getMetadataID() == DISubprogramKind;
    1819             :   }
    1820             : };
    1821             : 
    1822             : class DILexicalBlockBase : public DILocalScope {
    1823             : protected:
    1824             :   DILexicalBlockBase(LLVMContext &C, unsigned ID, StorageType Storage,
    1825             :                      ArrayRef<Metadata *> Ops)
    1826             :       : DILocalScope(C, ID, Storage, dwarf::DW_TAG_lexical_block, Ops) {}
    1827             :   ~DILexicalBlockBase() = default;
    1828             : 
    1829             : public:
    1830             :   DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); }
    1831             : 
    1832     3689550 :   Metadata *getRawScope() const { return getOperand(1); }
    1833             : 
    1834             :   static bool classof(const Metadata *MD) {
    1835    10322828 :     return MD->getMetadataID() == DILexicalBlockKind ||
    1836             :            MD->getMetadataID() == DILexicalBlockFileKind;
    1837             :   }
    1838             : };
    1839             : 
    1840             : class DILexicalBlock : public DILexicalBlockBase {
    1841             :   friend class LLVMContextImpl;
    1842             :   friend class MDNode;
    1843             : 
    1844             :   unsigned Line;
    1845             :   uint16_t Column;
    1846             : 
    1847             :   DILexicalBlock(LLVMContext &C, StorageType Storage, unsigned Line,
    1848             :                  unsigned Column, ArrayRef<Metadata *> Ops)
    1849       14458 :       : DILexicalBlockBase(C, DILexicalBlockKind, Storage, Ops), Line(Line),
    1850       14458 :         Column(Column) {
    1851             :     assert(Column < (1u << 16) && "Expected 16-bit column");
    1852             :   }
    1853        2070 :   ~DILexicalBlock() = default;
    1854             : 
    1855             :   static DILexicalBlock *getImpl(LLVMContext &Context, DILocalScope *Scope,
    1856             :                                  DIFile *File, unsigned Line, unsigned Column,
    1857             :                                  StorageType Storage,
    1858             :                                  bool ShouldCreate = true) {
    1859          37 :     return getImpl(Context, static_cast<Metadata *>(Scope),
    1860             :                    static_cast<Metadata *>(File), Line, Column, Storage,
    1861             :                    ShouldCreate);
    1862             :   }
    1863             : 
    1864             :   static DILexicalBlock *getImpl(LLVMContext &Context, Metadata *Scope,
    1865             :                                  Metadata *File, unsigned Line, unsigned Column,
    1866             :                                  StorageType Storage, bool ShouldCreate = true);
    1867             : 
    1868          26 :   TempDILexicalBlock cloneImpl() const {
    1869             :     return getTemporary(getContext(), getScope(), getFile(), getLine(),
    1870          78 :                         getColumn());
    1871             :   }
    1872             : 
    1873             : public:
    1874             :   DEFINE_MDNODE_GET(DILexicalBlock, (DILocalScope * Scope, DIFile *File,
    1875             :                                      unsigned Line, unsigned Column),
    1876             :                     (Scope, File, Line, Column))
    1877       14425 :   DEFINE_MDNODE_GET(DILexicalBlock, (Metadata * Scope, Metadata *File,
    1878             :                                      unsigned Line, unsigned Column),
    1879             :                     (Scope, File, Line, Column))
    1880             : 
    1881           1 :   TempDILexicalBlock clone() const { return cloneImpl(); }
    1882             : 
    1883           0 :   unsigned getLine() const { return Line; }
    1884         915 :   unsigned getColumn() const { return Column; }
    1885             : 
    1886             :   static bool classof(const Metadata *MD) {
    1887         169 :     return MD->getMetadataID() == DILexicalBlockKind;
    1888             :   }
    1889             : };
    1890             : 
    1891             : class DILexicalBlockFile : public DILexicalBlockBase {
    1892             :   friend class LLVMContextImpl;
    1893             :   friend class MDNode;
    1894             : 
    1895             :   unsigned Discriminator;
    1896             : 
    1897             :   DILexicalBlockFile(LLVMContext &C, StorageType Storage,
    1898             :                      unsigned Discriminator, ArrayRef<Metadata *> Ops)
    1899       15550 :       : DILexicalBlockBase(C, DILexicalBlockFileKind, Storage, Ops),
    1900       15550 :         Discriminator(Discriminator) {}
    1901         465 :   ~DILexicalBlockFile() = default;
    1902             : 
    1903             :   static DILexicalBlockFile *getImpl(LLVMContext &Context, DILocalScope *Scope,
    1904             :                                      DIFile *File, unsigned Discriminator,
    1905             :                                      StorageType Storage,
    1906             :                                      bool ShouldCreate = true) {
    1907           6 :     return getImpl(Context, static_cast<Metadata *>(Scope),
    1908             :                    static_cast<Metadata *>(File), Discriminator, Storage,
    1909             :                    ShouldCreate);
    1910             :   }
    1911             : 
    1912             :   static DILexicalBlockFile *getImpl(LLVMContext &Context, Metadata *Scope,
    1913             :                                      Metadata *File, unsigned Discriminator,
    1914             :                                      StorageType Storage,
    1915             :                                      bool ShouldCreate = true);
    1916             : 
    1917           1 :   TempDILexicalBlockFile cloneImpl() const {
    1918             :     return getTemporary(getContext(), getScope(), getFile(),
    1919           1 :                         getDiscriminator());
    1920             :   }
    1921             : 
    1922             : public:
    1923             :   DEFINE_MDNODE_GET(DILexicalBlockFile, (DILocalScope * Scope, DIFile *File,
    1924             :                                          unsigned Discriminator),
    1925             :                     (Scope, File, Discriminator))
    1926      247189 :   DEFINE_MDNODE_GET(DILexicalBlockFile,
    1927             :                     (Metadata * Scope, Metadata *File, unsigned Discriminator),
    1928             :                     (Scope, File, Discriminator))
    1929             : 
    1930           1 :   TempDILexicalBlockFile clone() const { return cloneImpl(); }
    1931             : 
    1932             :   // TODO: Remove these once they're gone from DILexicalBlockBase.
    1933             :   unsigned getLine() const = delete;
    1934             :   unsigned getColumn() const = delete;
    1935             : 
    1936           0 :   unsigned getDiscriminator() const { return Discriminator; }
    1937             : 
    1938             :   static bool classof(const Metadata *MD) {
    1939     8433579 :     return MD->getMetadataID() == DILexicalBlockFileKind;
    1940             :   }
    1941             : };
    1942             : 
    1943             : unsigned DILocation::getDiscriminator() const {
    1944             :   if (auto *F = dyn_cast<DILexicalBlockFile>(getScope()))
    1945       26879 :     return F->getDiscriminator();
    1946             :   return 0;
    1947             : }
    1948             : 
    1949             : const DILocation *
    1950         298 : DILocation::cloneWithDiscriminator(unsigned Discriminator) const {
    1951             :   DIScope *Scope = getScope();
    1952             :   // Skip all parent DILexicalBlockFile that already have a discriminator
    1953             :   // assigned. We do not want to have nested DILexicalBlockFiles that have
    1954             :   // mutliple discriminators because only the leaf DILexicalBlockFile's
    1955             :   // dominator will be used.
    1956             :   for (auto *LBF = dyn_cast<DILexicalBlockFile>(Scope);
    1957         357 :        LBF && LBF->getDiscriminator() != 0;
    1958             :        LBF = dyn_cast<DILexicalBlockFile>(Scope))
    1959             :     Scope = LBF->getScope();
    1960             :   DILexicalBlockFile *NewScope =
    1961             :       DILexicalBlockFile::get(getContext(), Scope, getFile(), Discriminator);
    1962         298 :   return DILocation::get(getContext(), getLine(), getColumn(), NewScope,
    1963         298 :                          getInlinedAt());
    1964             : }
    1965             : 
    1966        1457 : unsigned DILocation::getBaseDiscriminator() const {
    1967        1457 :   return getBaseDiscriminatorFromDiscriminator(getDiscriminator());
    1968             : }
    1969             : 
    1970         110 : unsigned DILocation::getDuplicationFactor() const {
    1971         110 :   return getDuplicationFactorFromDiscriminator(getDiscriminator());
    1972             : }
    1973             : 
    1974         110 : unsigned DILocation::getCopyIdentifier() const {
    1975         110 :   return getCopyIdentifierFromDiscriminator(getDiscriminator());
    1976             : }
    1977             : 
    1978         188 : const DILocation *DILocation::setBaseDiscriminator(unsigned D) const {
    1979         188 :   if (D == 0)
    1980             :     return this;
    1981             :   else
    1982         188 :     return cloneWithDiscriminator(getPrefixEncodingFromUnsigned(D) << 1);
    1983             : }
    1984             : 
    1985         110 : const DILocation *DILocation::cloneWithDuplicationFactor(unsigned DF) const {
    1986         110 :   DF *= getDuplicationFactor();
    1987         110 :   if (DF <= 1)
    1988             :     return this;
    1989             : 
    1990         110 :   unsigned BD = getBaseDiscriminator();
    1991         162 :   unsigned CI = getCopyIdentifier() << (DF > 0x1f ? 14 : 7);
    1992         110 :   unsigned D = CI | (getPrefixEncodingFromUnsigned(DF) << 1);
    1993             : 
    1994         110 :   if (BD == 0)
    1995         110 :     D = (D << 1) | 1;
    1996             :   else
    1997           0 :     D = (D << (BD > 0x1f ? 14 : 7)) | (getPrefixEncodingFromUnsigned(BD) << 1);
    1998             : 
    1999         110 :   return cloneWithDiscriminator(D);
    2000             : }
    2001             : 
    2002             : class DINamespace : public DIScope {
    2003             :   friend class LLVMContextImpl;
    2004             :   friend class MDNode;
    2005             : 
    2006             :   unsigned ExportSymbols : 1;
    2007             : 
    2008             :   DINamespace(LLVMContext &Context, StorageType Storage, bool ExportSymbols,
    2009             :               ArrayRef<Metadata *> Ops)
    2010         650 :       : DIScope(Context, DINamespaceKind, Storage, dwarf::DW_TAG_namespace,
    2011             :                 Ops),
    2012         650 :         ExportSymbols(ExportSymbols) {}
    2013         272 :   ~DINamespace() = default;
    2014             : 
    2015        2848 :   static DINamespace *getImpl(LLVMContext &Context, DIScope *Scope,
    2016             :                               StringRef Name, bool ExportSymbols,
    2017             :                               StorageType Storage, bool ShouldCreate = true) {
    2018        5633 :     return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
    2019        2848 :                    ExportSymbols, Storage, ShouldCreate);
    2020             :   }
    2021             :   static DINamespace *getImpl(LLVMContext &Context, Metadata *Scope,
    2022             :                               MDString *Name, bool ExportSymbols,
    2023             :                               StorageType Storage, bool ShouldCreate = true);
    2024             : 
    2025           1 :   TempDINamespace cloneImpl() const {
    2026             :     return getTemporary(getContext(), getScope(), getName(),
    2027           2 :                         getExportSymbols());
    2028             :   }
    2029             : 
    2030             : public:
    2031        2848 :   DEFINE_MDNODE_GET(DINamespace,
    2032             :                     (DIScope *Scope, StringRef Name, bool ExportSymbols),
    2033             :                     (Scope, Name, ExportSymbols))
    2034         138 :   DEFINE_MDNODE_GET(DINamespace,
    2035             :                     (Metadata *Scope, MDString *Name, bool ExportSymbols),
    2036             :                     (Scope, Name, ExportSymbols))
    2037             : 
    2038           1 :   TempDINamespace clone() const { return cloneImpl(); }
    2039             : 
    2040        3650 :   bool getExportSymbols() const { return ExportSymbols; }
    2041             :   DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
    2042        1130 :   StringRef getName() const { return getStringOperand(2); }
    2043             : 
    2044      243934 :   Metadata *getRawScope() const { return getOperand(1); }
    2045             :   MDString *getRawName() const { return getOperandAs<MDString>(2); }
    2046             : 
    2047             :   static bool classof(const Metadata *MD) {
    2048       16881 :     return MD->getMetadataID() == DINamespaceKind;
    2049             :   }
    2050             : };
    2051             : 
    2052             : /// A (clang) module that has been imported by the compile unit.
    2053             : ///
    2054             : class DIModule : public DIScope {
    2055             :   friend class LLVMContextImpl;
    2056             :   friend class MDNode;
    2057             : 
    2058             :   DIModule(LLVMContext &Context, StorageType Storage, ArrayRef<Metadata *> Ops)
    2059             :       : DIScope(Context, DIModuleKind, Storage, dwarf::DW_TAG_module, Ops) {}
    2060          88 :   ~DIModule() = default;
    2061             : 
    2062          64 :   static DIModule *getImpl(LLVMContext &Context, DIScope *Scope,
    2063             :                            StringRef Name, StringRef ConfigurationMacros,
    2064             :                            StringRef IncludePath, StringRef ISysRoot,
    2065             :                            StorageType Storage, bool ShouldCreate = true) {
    2066         265 :     return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
    2067             :                    getCanonicalMDString(Context, ConfigurationMacros),
    2068             :                    getCanonicalMDString(Context, IncludePath),
    2069             :                    getCanonicalMDString(Context, ISysRoot),
    2070          64 :                    Storage, ShouldCreate);
    2071             :   }
    2072             :   static DIModule *getImpl(LLVMContext &Context, Metadata *Scope,
    2073             :                            MDString *Name, MDString *ConfigurationMacros,
    2074             :                            MDString *IncludePath, MDString *ISysRoot,
    2075             :                            StorageType Storage, bool ShouldCreate = true);
    2076             : 
    2077           1 :   TempDIModule cloneImpl() const {
    2078             :     return getTemporary(getContext(), getScope(), getName(),
    2079             :                         getConfigurationMacros(), getIncludePath(),
    2080           1 :                         getISysRoot());
    2081             :   }
    2082             : 
    2083             : public:
    2084          64 :   DEFINE_MDNODE_GET(DIModule, (DIScope *Scope, StringRef Name,
    2085             :                                StringRef ConfigurationMacros, StringRef IncludePath,
    2086             :                                StringRef ISysRoot),
    2087             :                     (Scope, Name, ConfigurationMacros, IncludePath, ISysRoot))
    2088          33 :   DEFINE_MDNODE_GET(DIModule,
    2089             :                     (Metadata *Scope, MDString *Name, MDString *ConfigurationMacros,
    2090             :                      MDString *IncludePath, MDString *ISysRoot),
    2091             :                     (Scope, Name, ConfigurationMacros, IncludePath, ISysRoot))
    2092             : 
    2093           1 :   TempDIModule clone() const { return cloneImpl(); }
    2094             : 
    2095             :   DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
    2096         299 :   StringRef getName() const { return getStringOperand(1); }
    2097          77 :   StringRef getConfigurationMacros() const { return getStringOperand(2); }
    2098          91 :   StringRef getIncludePath() const { return getStringOperand(3); }
    2099          97 :   StringRef getISysRoot() const { return getStringOperand(4); }
    2100             : 
    2101         400 :   Metadata *getRawScope() const { return getOperand(0); }
    2102             :   MDString *getRawName() const { return getOperandAs<MDString>(1); }
    2103             :   MDString *getRawConfigurationMacros() const { return getOperandAs<MDString>(2); }
    2104             :   MDString *getRawIncludePath() const { return getOperandAs<MDString>(3); }
    2105             :   MDString *getRawISysRoot() const { return getOperandAs<MDString>(4); }
    2106             : 
    2107             :   static bool classof(const Metadata *MD) {
    2108           0 :     return MD->getMetadataID() == DIModuleKind;
    2109             :   }
    2110             : };
    2111             : 
    2112             : /// Base class for template parameters.
    2113             : class DITemplateParameter : public DINode {
    2114             : protected:
    2115             :   DITemplateParameter(LLVMContext &Context, unsigned ID, StorageType Storage,
    2116             :                       unsigned Tag, ArrayRef<Metadata *> Ops)
    2117        6254 :       : DINode(Context, ID, Storage, Tag, Ops) {}
    2118             :   ~DITemplateParameter() = default;
    2119             : 
    2120             : public:
    2121       26673 :   StringRef getName() const { return getStringOperand(0); }
    2122             :   DITypeRef getType() const { return DITypeRef(getRawType()); }
    2123             : 
    2124             :   MDString *getRawName() const { return getOperandAs<MDString>(0); }
    2125       43217 :   Metadata *getRawType() const { return getOperand(1); }
    2126             : 
    2127             :   static bool classof(const Metadata *MD) {
    2128        2178 :     return MD->getMetadataID() == DITemplateTypeParameterKind ||
    2129             :            MD->getMetadataID() == DITemplateValueParameterKind;
    2130             :   }
    2131             : };
    2132             : 
    2133             : class DITemplateTypeParameter : public DITemplateParameter {
    2134             :   friend class LLVMContextImpl;
    2135             :   friend class MDNode;
    2136             : 
    2137             :   DITemplateTypeParameter(LLVMContext &Context, StorageType Storage,
    2138             :                           ArrayRef<Metadata *> Ops)
    2139             :       : DITemplateParameter(Context, DITemplateTypeParameterKind, Storage,
    2140             :                             dwarf::DW_TAG_template_type_parameter, Ops) {}
    2141         353 :   ~DITemplateTypeParameter() = default;
    2142             : 
    2143       14775 :   static DITemplateTypeParameter *getImpl(LLVMContext &Context, StringRef Name,
    2144             :                                           DITypeRef Type, StorageType Storage,
    2145             :                                           bool ShouldCreate = true) {
    2146       26949 :     return getImpl(Context, getCanonicalMDString(Context, Name), Type, Storage,
    2147       14775 :                    ShouldCreate);
    2148             :   }
    2149             :   static DITemplateTypeParameter *getImpl(LLVMContext &Context, MDString *Name,
    2150             :                                           Metadata *Type, StorageType Storage,
    2151             :                                           bool ShouldCreate = true);
    2152             : 
    2153           2 :   TempDITemplateTypeParameter cloneImpl() const {
    2154           2 :     return getTemporary(getContext(), getName(), getType());
    2155             :   }
    2156             : 
    2157             : public:
    2158       14775 :   DEFINE_MDNODE_GET(DITemplateTypeParameter, (StringRef Name, DITypeRef Type),
    2159             :                     (Name, Type))
    2160          97 :   DEFINE_MDNODE_GET(DITemplateTypeParameter, (MDString * Name, Metadata *Type),
    2161             :                     (Name, Type))
    2162             : 
    2163           1 :   TempDITemplateTypeParameter clone() const { return cloneImpl(); }
    2164             : 
    2165             :   static bool classof(const Metadata *MD) {
    2166       14384 :     return MD->getMetadataID() == DITemplateTypeParameterKind;
    2167             :   }
    2168             : };
    2169             : 
    2170             : class DITemplateValueParameter : public DITemplateParameter {
    2171             :   friend class LLVMContextImpl;
    2172             :   friend class MDNode;
    2173             : 
    2174             :   DITemplateValueParameter(LLVMContext &Context, StorageType Storage,
    2175             :                            unsigned Tag, ArrayRef<Metadata *> Ops)
    2176             :       : DITemplateParameter(Context, DITemplateValueParameterKind, Storage, Tag,
    2177             :                             Ops) {}
    2178         353 :   ~DITemplateValueParameter() = default;
    2179             : 
    2180        4030 :   static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
    2181             :                                            StringRef Name, DITypeRef Type,
    2182             :                                            Metadata *Value, StorageType Storage,
    2183             :                                            bool ShouldCreate = true) {
    2184        7376 :     return getImpl(Context, Tag, getCanonicalMDString(Context, Name), Type,
    2185        4030 :                    Value, Storage, ShouldCreate);
    2186             :   }
    2187             :   static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
    2188             :                                            MDString *Name, Metadata *Type,
    2189             :                                            Metadata *Value, StorageType Storage,
    2190             :                                            bool ShouldCreate = true);
    2191             : 
    2192           3 :   TempDITemplateValueParameter cloneImpl() const {
    2193             :     return getTemporary(getContext(), getTag(), getName(), getType(),
    2194           3 :                         getValue());
    2195             :   }
    2196             : 
    2197             : public:
    2198        4030 :   DEFINE_MDNODE_GET(DITemplateValueParameter, (unsigned Tag, StringRef Name,
    2199             :                                                DITypeRef Type, Metadata *Value),
    2200             :                     (Tag, Name, Type, Value))
    2201          90 :   DEFINE_MDNODE_GET(DITemplateValueParameter, (unsigned Tag, MDString *Name,
    2202             :                                                Metadata *Type, Metadata *Value),
    2203             :                     (Tag, Name, Type, Value))
    2204             : 
    2205           1 :   TempDITemplateValueParameter clone() const { return cloneImpl(); }
    2206             : 
    2207        8858 :   Metadata *getValue() const { return getOperand(2); }
    2208             : 
    2209             :   static bool classof(const Metadata *MD) {
    2210           0 :     return MD->getMetadataID() == DITemplateValueParameterKind;
    2211             :   }
    2212             : };
    2213             : 
    2214             : /// Base class for variables.
    2215             : class DIVariable : public DINode {
    2216             :   unsigned Line;
    2217             :   uint32_t AlignInBits;
    2218             : 
    2219             : protected:
    2220             :   DIVariable(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Line,
    2221             :              ArrayRef<Metadata *> Ops, uint32_t AlignInBits = 0)
    2222       39528 :       : DINode(C, ID, Storage, dwarf::DW_TAG_variable, Ops), Line(Line),
    2223       39528 :         AlignInBits(AlignInBits) {}
    2224             :   ~DIVariable() = default;
    2225             : 
    2226             : public:
    2227           0 :   unsigned getLine() const { return Line; }
    2228             :   DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
    2229       46066 :   StringRef getName() const { return getStringOperand(1); }
    2230             :   DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
    2231             :   DITypeRef getType() const { return DITypeRef(getRawType()); }
    2232           0 :   uint32_t getAlignInBits() const { return AlignInBits; }
    2233       23441 :   uint32_t getAlignInBytes() const { return getAlignInBits() / CHAR_BIT; }
    2234             :   /// Determines the size of the variable's type.
    2235             :   Optional<uint64_t> getSizeInBits() const;
    2236             : 
    2237             :   /// Return the signedness of this variable's type, or None if this type is
    2238             :   /// neither signed nor unsigned.
    2239         408 :   Optional<DIBasicType::Signedness> getSignedness() const {
    2240             :     if (auto *BT = dyn_cast<DIBasicType>(getType().resolve()))
    2241         408 :       return BT->getSignedness();
    2242             :     return None;
    2243             :   }
    2244             : 
    2245           2 :   StringRef getFilename() const {
    2246             :     if (auto *F = getFile())
    2247             :       return F->getFilename();
    2248           0 :     return "";
    2249             :   }
    2250             : 
    2251           2 :   StringRef getDirectory() const {
    2252             :     if (auto *F = getFile())
    2253             :       return F->getDirectory();
    2254           0 :     return "";
    2255             :   }
    2256             : 
    2257             :   Optional<StringRef> getSource() const {
    2258             :     if (auto *F = getFile())
    2259             :       return F->getSource();
    2260             :     return None;
    2261             :   }
    2262             : 
    2263      377393 :   Metadata *getRawScope() const { return getOperand(0); }
    2264             :   MDString *getRawName() const { return getOperandAs<MDString>(1); }
    2265       23339 :   Metadata *getRawFile() const { return getOperand(2); }
    2266      235647 :   Metadata *getRawType() const { return getOperand(3); }
    2267             : 
    2268             :   static bool classof(const Metadata *MD) {
    2269         187 :     return MD->getMetadataID() == DILocalVariableKind ||
    2270             :            MD->getMetadataID() == DIGlobalVariableKind;
    2271             :   }
    2272             : };
    2273             : 
    2274             : /// DWARF expression.
    2275             : ///
    2276             : /// This is (almost) a DWARF expression that modifies the location of a
    2277             : /// variable, or the location of a single piece of a variable, or (when using
    2278             : /// DW_OP_stack_value) is the constant variable value.
    2279             : ///
    2280             : /// TODO: Co-allocate the expression elements.
    2281             : /// TODO: Separate from MDNode, or otherwise drop Distinct and Temporary
    2282             : /// storage types.
    2283             : class DIExpression : public MDNode {
    2284             :   friend class LLVMContextImpl;
    2285             :   friend class MDNode;
    2286             : 
    2287             :   std::vector<uint64_t> Elements;
    2288             : 
    2289        3742 :   DIExpression(LLVMContext &C, StorageType Storage, ArrayRef<uint64_t> Elements)
    2290        3742 :       : MDNode(C, DIExpressionKind, Storage, None),
    2291        7484 :         Elements(Elements.begin(), Elements.end()) {}
    2292        2072 :   ~DIExpression() = default;
    2293             : 
    2294             :   static DIExpression *getImpl(LLVMContext &Context,
    2295             :                                ArrayRef<uint64_t> Elements, StorageType Storage,
    2296             :                                bool ShouldCreate = true);
    2297             : 
    2298           1 :   TempDIExpression cloneImpl() const {
    2299           1 :     return getTemporary(getContext(), getElements());
    2300             :   }
    2301             : 
    2302             : public:
    2303       64937 :   DEFINE_MDNODE_GET(DIExpression, (ArrayRef<uint64_t> Elements), (Elements))
    2304             : 
    2305           1 :   TempDIExpression clone() const { return cloneImpl(); }
    2306             : 
    2307             :   ArrayRef<uint64_t> getElements() const { return Elements; }
    2308             : 
    2309      128939 :   unsigned getNumElements() const { return Elements.size(); }
    2310             : 
    2311             :   uint64_t getElement(unsigned I) const {
    2312             :     assert(I < Elements.size() && "Index out of range");
    2313          68 :     return Elements[I];
    2314             :   }
    2315             : 
    2316             :   /// Determine whether this represents a standalone constant value.
    2317             :   bool isConstant() const;
    2318             : 
    2319             :   using element_iterator = ArrayRef<uint64_t>::iterator;
    2320             : 
    2321             :   element_iterator elements_begin() const { return getElements().begin(); }
    2322             :   element_iterator elements_end() const { return getElements().end(); }
    2323             : 
    2324             :   /// A lightweight wrapper around an expression operand.
    2325             :   ///
    2326             :   /// TODO: Store arguments directly and change \a DIExpression to store a
    2327             :   /// range of these.
    2328             :   class ExprOperand {
    2329             :     const uint64_t *Op = nullptr;
    2330             : 
    2331             :   public:
    2332       13083 :     ExprOperand() = default;
    2333        1284 :     explicit ExprOperand(const uint64_t *Op) : Op(Op) {}
    2334             : 
    2335           0 :     const uint64_t *get() const { return Op; }
    2336             : 
    2337             :     /// Get the operand code.
    2338      562269 :     uint64_t getOp() const { return *Op; }
    2339             : 
    2340             :     /// Get an argument to the operand.
    2341             :     ///
    2342             :     /// Never returns the operand itself.
    2343       38747 :     uint64_t getArg(unsigned I) const { return Op[I + 1]; }
    2344             : 
    2345         422 :     unsigned getNumArgs() const { return getSize() - 1; }
    2346             : 
    2347             :     /// Return the size of the operand.
    2348             :     ///
    2349             :     /// Return the number of elements in the operand (1 + args).
    2350             :     unsigned getSize() const;
    2351             : 
    2352             :     /// Append the elements of this operand to \p V.
    2353        4633 :     void appendToVector(SmallVectorImpl<uint64_t> &V) const {
    2354        4633 :       V.append(get(), get() + getSize());
    2355        4633 :     }
    2356             :   };
    2357             : 
    2358             :   /// An iterator for expression operands.
    2359             :   class expr_op_iterator
    2360             :       : public std::iterator<std::input_iterator_tag, ExprOperand> {
    2361             :     ExprOperand Op;
    2362             : 
    2363             :   public:
    2364             :     expr_op_iterator() = default;
    2365             :     explicit expr_op_iterator(element_iterator I) : Op(I) {}
    2366             : 
    2367     1148671 :     element_iterator getBase() const { return Op.get(); }
    2368             :     const ExprOperand &operator*() const { return Op; }
    2369             :     const ExprOperand *operator->() const { return &Op; }
    2370             : 
    2371             :     expr_op_iterator &operator++() {
    2372             :       increment();
    2373             :       return *this;
    2374             :     }
    2375           0 :     expr_op_iterator operator++(int) {
    2376           0 :       expr_op_iterator T(*this);
    2377             :       increment();
    2378           0 :       return T;
    2379             :     }
    2380             : 
    2381             :     /// Get the next iterator.
    2382             :     ///
    2383             :     /// \a std::next() doesn't work because this is technically an
    2384             :     /// input_iterator, but it's a perfectly valid operation.  This is an
    2385             :     /// accessor to provide the same functionality.
    2386           0 :     expr_op_iterator getNext() const { return ++expr_op_iterator(*this); }
    2387             : 
    2388             :     bool operator==(const expr_op_iterator &X) const {
    2389           0 :       return getBase() == X.getBase();
    2390             :     }
    2391             :     bool operator!=(const expr_op_iterator &X) const {
    2392             :       return getBase() != X.getBase();
    2393             :     }
    2394             : 
    2395             :   private:
    2396      258038 :     void increment() { Op = ExprOperand(getBase() + Op.getSize()); }
    2397             :   };
    2398             : 
    2399             :   /// Visit the elements via ExprOperand wrappers.
    2400             :   ///
    2401             :   /// These range iterators visit elements through \a ExprOperand wrappers.
    2402             :   /// This is not guaranteed to be a valid range unless \a isValid() gives \c
    2403             :   /// true.
    2404             :   ///
    2405             :   /// \pre \a isValid() gives \c true.
    2406             :   /// @{
    2407             :   expr_op_iterator expr_op_begin() const {
    2408             :     return expr_op_iterator(elements_begin());
    2409             :   }
    2410             :   expr_op_iterator expr_op_end() const {
    2411             :     return expr_op_iterator(elements_end());
    2412             :   }
    2413             :   iterator_range<expr_op_iterator> expr_ops() const {
    2414             :     return {expr_op_begin(), expr_op_end()};
    2415             :   }
    2416             :   /// @}
    2417             : 
    2418             :   bool isValid() const;
    2419             : 
    2420             :   static bool classof(const Metadata *MD) {
    2421      393213 :     return MD->getMetadataID() == DIExpressionKind;
    2422             :   }
    2423             : 
    2424             :   /// Return whether the first element a DW_OP_deref.
    2425             :   bool startsWithDeref() const {
    2426          10 :     return getNumElements() > 0 && getElement(0) == dwarf::DW_OP_deref;
    2427             :   }
    2428             : 
    2429             :   /// Holds the characteristics of one fragment of a larger variable.
    2430             :   struct FragmentInfo {
    2431             :     uint64_t SizeInBits;
    2432             :     uint64_t OffsetInBits;
    2433             :   };
    2434             : 
    2435             :   /// Retrieve the details of this fragment expression.
    2436             :   static Optional<FragmentInfo> getFragmentInfo(expr_op_iterator Start,
    2437             :                                                 expr_op_iterator End);
    2438             : 
    2439             :   /// Retrieve the details of this fragment expression.
    2440             :   Optional<FragmentInfo> getFragmentInfo() const {
    2441      450049 :     return getFragmentInfo(expr_op_begin(), expr_op_end());
    2442             :   }
    2443             : 
    2444             :   /// Return whether this is a piece of an aggregate variable.
    2445      301459 :   bool isFragment() const { return getFragmentInfo().hasValue(); }
    2446             : 
    2447             :   /// Append \p Ops with operations to apply the \p Offset.
    2448             :   static void appendOffset(SmallVectorImpl<uint64_t> &Ops, int64_t Offset);
    2449             : 
    2450             :   /// If this is a constant offset, extract it. If there is no expression,
    2451             :   /// return true with an offset of zero.
    2452             :   bool extractIfOffset(int64_t &Offset) const;
    2453             : 
    2454             :   /// Constants for DIExpression::prepend.
    2455             :   enum { NoDeref = false, WithDeref = true, WithStackValue = true };
    2456             : 
    2457             :   /// Prepend \p DIExpr with a deref and offset operation and optionally turn it
    2458             :   /// into a stack value.
    2459             :   static DIExpression *prepend(const DIExpression *Expr, bool DerefBefore,
    2460             :                                int64_t Offset = 0, bool DerefAfter = false,
    2461             :                                bool StackValue = false);
    2462             : 
    2463             :   /// Prepend \p DIExpr with the given opcodes and optionally turn it into a
    2464             :   /// stack value.
    2465             :   static DIExpression *prependOpcodes(const DIExpression *Expr,
    2466             :                                       SmallVectorImpl<uint64_t> &Ops,
    2467             :                                       bool StackValue = false);
    2468             : 
    2469             :   /// Append the opcodes \p Ops to \p DIExpr. Unlike \ref appendToStack, the
    2470             :   /// returned expression is a stack value only if \p DIExpr is a stack value.
    2471             :   /// If \p DIExpr describes a fragment, the returned expression will describe
    2472             :   /// the same fragment.
    2473             :   static DIExpression *append(const DIExpression *Expr, ArrayRef<uint64_t> Ops);
    2474             : 
    2475             :   /// Convert \p DIExpr into a stack value if it isn't one already by appending
    2476             :   /// DW_OP_deref if needed, and appending \p Ops to the resulting expression.
    2477             :   /// If \p DIExpr describes a fragment, the returned expression will describe
    2478             :   /// the same fragment.
    2479             :   static DIExpression *appendToStack(const DIExpression *Expr,
    2480             :                                      ArrayRef<uint64_t> Ops);
    2481             : 
    2482             :   /// Create a DIExpression to describe one part of an aggregate variable that
    2483             :   /// is fragmented across multiple Values. The DW_OP_LLVM_fragment operation
    2484             :   /// will be appended to the elements of \c Expr. If \c Expr already contains
    2485             :   /// a \c DW_OP_LLVM_fragment \c OffsetInBits is interpreted as an offset
    2486             :   /// into the existing fragment.
    2487             :   ///
    2488             :   /// \param OffsetInBits Offset of the piece in bits.
    2489             :   /// \param SizeInBits   Size of the piece in bits.
    2490             :   /// \return             Creating a fragment expression may fail if \c Expr
    2491             :   ///                     contains arithmetic operations that would be truncated.
    2492             :   static Optional<DIExpression *>
    2493             :   createFragmentExpression(const DIExpression *Expr, unsigned OffsetInBits,
    2494             :                            unsigned SizeInBits);
    2495             : 
    2496             :   /// Determine the relative position of the fragments described by this
    2497             :   /// DIExpression and \p Other.
    2498             :   /// Returns -1 if this is entirely before Other, 0 if this and Other overlap,
    2499             :   /// 1 if this is entirely after Other.
    2500        1153 :   int fragmentCmp(const DIExpression *Other) const {
    2501        1153 :     auto Fragment1 = *getFragmentInfo();
    2502        1153 :     auto Fragment2 = *Other->getFragmentInfo();
    2503        1153 :     unsigned l1 = Fragment1.OffsetInBits;
    2504        1153 :     unsigned l2 = Fragment2.OffsetInBits;
    2505        1153 :     unsigned r1 = l1 + Fragment1.SizeInBits;
    2506        1153 :     unsigned r2 = l2 + Fragment2.SizeInBits;
    2507        1153 :     if (r1 <= l2)
    2508             :       return -1;
    2509         818 :     else if (r2 <= l1)
    2510             :       return 1;
    2511             :     else
    2512         378 :       return 0;
    2513             :   }
    2514             : 
    2515             :   /// Check if fragments overlap between this DIExpression and \p Other.
    2516        3729 :   bool fragmentsOverlap(const DIExpression *Other) const {
    2517        4749 :     if (!isFragment() || !Other->isFragment())
    2518        2709 :       return true;
    2519        1020 :     return fragmentCmp(Other) == 0;
    2520             :   }
    2521             : };
    2522             : 
    2523             : /// Global variables.
    2524             : ///
    2525             : /// TODO: Remove DisplayName.  It's always equal to Name.
    2526             : class DIGlobalVariable : public DIVariable {
    2527             :   friend class LLVMContextImpl;
    2528             :   friend class MDNode;
    2529             : 
    2530             :   bool IsLocalToUnit;
    2531             :   bool IsDefinition;
    2532             : 
    2533             :   DIGlobalVariable(LLVMContext &C, StorageType Storage, unsigned Line,
    2534             :                    bool IsLocalToUnit, bool IsDefinition, uint32_t AlignInBits,
    2535             :                    ArrayRef<Metadata *> Ops)
    2536        1992 :       : DIVariable(C, DIGlobalVariableKind, Storage, Line, Ops, AlignInBits),
    2537        1992 :         IsLocalToUnit(IsLocalToUnit), IsDefinition(IsDefinition) {}
    2538        1499 :   ~DIGlobalVariable() = default;
    2539             : 
    2540             :   static DIGlobalVariable *
    2541         927 :   getImpl(LLVMContext &Context, DIScope *Scope, StringRef Name,
    2542             :           StringRef LinkageName, DIFile *File, unsigned Line, DITypeRef Type,
    2543             :           bool IsLocalToUnit, bool IsDefinition,
    2544             :           DIDerivedType *StaticDataMemberDeclaration, MDTuple *TemplateParams,
    2545             :           uint32_t AlignInBits, StorageType Storage, bool ShouldCreate = true) {
    2546        3143 :     return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
    2547             :                    getCanonicalMDString(Context, LinkageName), File, Line, Type,
    2548             :                    IsLocalToUnit, IsDefinition, StaticDataMemberDeclaration,
    2549             :                    cast_or_null<Metadata>(TemplateParams), AlignInBits, Storage,
    2550         927 :                    ShouldCreate);
    2551             :   }
    2552             :   static DIGlobalVariable *
    2553             :   getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
    2554             :           MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
    2555             :           bool IsLocalToUnit, bool IsDefinition,
    2556             :           Metadata *StaticDataMemberDeclaration, Metadata *TemplateParams,
    2557             :           uint32_t AlignInBits, StorageType Storage, bool ShouldCreate = true);
    2558             : 
    2559           2 :   TempDIGlobalVariable cloneImpl() const {
    2560             :     return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
    2561           2 :                         getFile(), getLine(), getType(), isLocalToUnit(),
    2562           2 :                         isDefinition(), getStaticDataMemberDeclaration(),
    2563           6 :                         getTemplateParams(), getAlignInBits());
    2564             :   }
    2565             : 
    2566             : public:
    2567         926 :   DEFINE_MDNODE_GET(DIGlobalVariable,
    2568             :                     (DIScope * Scope, StringRef Name, StringRef LinkageName,
    2569             :                      DIFile *File, unsigned Line, DITypeRef Type,
    2570             :                      bool IsLocalToUnit, bool IsDefinition,
    2571             :                      DIDerivedType *StaticDataMemberDeclaration,
    2572             :                      MDTuple *TemplateParams, uint32_t AlignInBits),
    2573             :                     (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
    2574             :                      IsDefinition, StaticDataMemberDeclaration, TemplateParams,
    2575             :                      AlignInBits))
    2576        1066 :   DEFINE_MDNODE_GET(DIGlobalVariable,
    2577             :                     (Metadata * Scope, MDString *Name, MDString *LinkageName,
    2578             :                      Metadata *File, unsigned Line, Metadata *Type,
    2579             :                      bool IsLocalToUnit, bool IsDefinition,
    2580             :                      Metadata *StaticDataMemberDeclaration,
    2581             :                      Metadata *TemplateParams, uint32_t AlignInBits),
    2582             :                     (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
    2583             :                      IsDefinition, StaticDataMemberDeclaration, TemplateParams,
    2584             :                      AlignInBits))
    2585             : 
    2586           1 :   TempDIGlobalVariable clone() const { return cloneImpl(); }
    2587             : 
    2588           0 :   bool isLocalToUnit() const { return IsLocalToUnit; }
    2589           0 :   bool isDefinition() const { return IsDefinition; }
    2590        1010 :   StringRef getDisplayName() const { return getStringOperand(4); }
    2591        3169 :   StringRef getLinkageName() const { return getStringOperand(5); }
    2592             :   DIDerivedType *getStaticDataMemberDeclaration() const {
    2593             :     return cast_or_null<DIDerivedType>(getRawStaticDataMemberDeclaration());
    2594             :   }
    2595             : 
    2596             :   MDString *getRawLinkageName() const { return getOperandAs<MDString>(5); }
    2597       13658 :   Metadata *getRawStaticDataMemberDeclaration() const { return getOperand(6); }
    2598             :   Metadata *getRawTemplateParams() const { return getOperand(7); }
    2599             :   MDTuple *getTemplateParams() const { return getOperandAs<MDTuple>(7); }
    2600             : 
    2601             :   static bool classof(const Metadata *MD) {
    2602          50 :     return MD->getMetadataID() == DIGlobalVariableKind;
    2603             :   }
    2604             : };
    2605             : 
    2606             : /// Local variable.
    2607             : ///
    2608             : /// TODO: Split up flags.
    2609             : class DILocalVariable : public DIVariable {
    2610             :   friend class LLVMContextImpl;
    2611             :   friend class MDNode;
    2612             : 
    2613             :   unsigned Arg : 16;
    2614             :   DIFlags Flags;
    2615             : 
    2616             :   DILocalVariable(LLVMContext &C, StorageType Storage, unsigned Line,
    2617             :                   unsigned Arg, DIFlags Flags, uint32_t AlignInBits,
    2618             :                   ArrayRef<Metadata *> Ops)
    2619       37536 :       : DIVariable(C, DILocalVariableKind, Storage, Line, Ops, AlignInBits),
    2620       37536 :         Arg(Arg), Flags(Flags) {
    2621             :     assert(Arg < (1 << 16) && "DILocalVariable: Arg out of range");
    2622             :   }
    2623        8101 :   ~DILocalVariable() = default;
    2624             : 
    2625       35006 :   static DILocalVariable *getImpl(LLVMContext &Context, DIScope *Scope,
    2626             :                                   StringRef Name, DIFile *File, unsigned Line,
    2627             :                                   DITypeRef Type, unsigned Arg, DIFlags Flags,
    2628             :                                   uint32_t AlignInBits, StorageType Storage,
    2629             :                                   bool ShouldCreate = true) {
    2630       68554 :     return getImpl(Context, Scope, getCanonicalMDString(Context, Name), File,
    2631       35006 :                    Line, Type, Arg, Flags, AlignInBits, Storage, ShouldCreate);
    2632             :   }
    2633             :   static DILocalVariable *getImpl(LLVMContext &Context, Metadata *Scope,
    2634             :                                   MDString *Name, Metadata *File, unsigned Line,
    2635             :                                   Metadata *Type, unsigned Arg, DIFlags Flags,
    2636             :                                   uint32_t AlignInBits, StorageType Storage,
    2637             :                                   bool ShouldCreate = true);
    2638             : 
    2639          24 :   TempDILocalVariable cloneImpl() const {
    2640             :     return getTemporary(getContext(), getScope(), getName(), getFile(),
    2641             :                         getLine(), getType(), getArg(), getFlags(),
    2642          24 :                         getAlignInBits());
    2643             :   }
    2644             : 
    2645             : public:
    2646       35006 :   DEFINE_MDNODE_GET(DILocalVariable,
    2647             :                     (DILocalScope * Scope, StringRef Name, DIFile *File,
    2648             :                      unsigned Line, DITypeRef Type, unsigned Arg,
    2649             :                      DIFlags Flags, uint32_t AlignInBits),
    2650             :                     (Scope, Name, File, Line, Type, Arg, Flags, AlignInBits))
    2651        2663 :   DEFINE_MDNODE_GET(DILocalVariable,
    2652             :                     (Metadata * Scope, MDString *Name, Metadata *File,
    2653             :                      unsigned Line, Metadata *Type, unsigned Arg,
    2654             :                      DIFlags Flags, uint32_t AlignInBits),
    2655             :                     (Scope, Name, File, Line, Type, Arg, Flags, AlignInBits))
    2656             : 
    2657           1 :   TempDILocalVariable clone() const { return cloneImpl(); }
    2658             : 
    2659             :   /// Get the local scope for this variable.
    2660             :   ///
    2661             :   /// Variables must be defined in a local scope.
    2662             :   DILocalScope *getScope() const {
    2663             :     return cast<DILocalScope>(DIVariable::getScope());
    2664             :   }
    2665             : 
    2666      155882 :   bool isParameter() const { return Arg; }
    2667       96537 :   unsigned getArg() const { return Arg; }
    2668           0 :   DIFlags getFlags() const { return Flags; }
    2669             : 
    2670       22715 :   bool isArtificial() const { return getFlags() & FlagArtificial; }
    2671       77237 :   bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
    2672             : 
    2673             :   /// Check that a location is valid for this variable.
    2674             :   ///
    2675             :   /// Check that \c DL exists, is in the same subprogram, and has the same
    2676             :   /// inlined-at location as \c this.  (Otherwise, it's not a valid attachment
    2677             :   /// to a \a DbgInfoIntrinsic.)
    2678             :   bool isValidLocationForIntrinsic(const DILocation *DL) const {
    2679             :     return DL && getScope()->getSubprogram() == DL->getScope()->getSubprogram();
    2680             :   }
    2681             : 
    2682             :   static bool classof(const Metadata *MD) {
    2683      127049 :     return MD->getMetadataID() == DILocalVariableKind;
    2684             :   }
    2685             : };
    2686             : 
    2687             : /// Label.
    2688             : ///
    2689             : class DILabel : public DINode {
    2690             :   friend class LLVMContextImpl;
    2691             :   friend class MDNode;
    2692             : 
    2693             :   unsigned Line;
    2694             : 
    2695             :   DILabel(LLVMContext &C, StorageType Storage, unsigned Line,
    2696             :           ArrayRef<Metadata *> Ops)
    2697          35 :       : DINode(C, DILabelKind, Storage, dwarf::DW_TAG_label, Ops), Line(Line) {}
    2698          35 :   ~DILabel() = default;
    2699             : 
    2700           0 :   static DILabel *getImpl(LLVMContext &Context, DIScope *Scope,
    2701             :                           StringRef Name, DIFile *File, unsigned Line,
    2702             :                           StorageType Storage,
    2703             :                           bool ShouldCreate = true) {
    2704           0 :     return getImpl(Context, Scope, getCanonicalMDString(Context, Name), File,
    2705           0 :                    Line, Storage, ShouldCreate);
    2706             :   }
    2707             :   static DILabel *getImpl(LLVMContext &Context, Metadata *Scope,
    2708             :                           MDString *Name, Metadata *File, unsigned Line,
    2709             :                           StorageType Storage,
    2710             :                           bool ShouldCreate = true);
    2711             : 
    2712           0 :   TempDILabel cloneImpl() const {
    2713             :     return getTemporary(getContext(), getScope(), getName(), getFile(),
    2714           0 :                         getLine());
    2715             :   }
    2716             : 
    2717             : public:
    2718           0 :   DEFINE_MDNODE_GET(DILabel,
    2719             :                     (DILocalScope * Scope, StringRef Name, DIFile *File,
    2720             :                      unsigned Line),
    2721             :                     (Scope, Name, File, Line))
    2722          35 :   DEFINE_MDNODE_GET(DILabel,
    2723             :                     (Metadata * Scope, MDString *Name, Metadata *File,
    2724             :                      unsigned Line),
    2725             :                     (Scope, Name, File, Line))
    2726             : 
    2727             :   TempDILabel clone() const { return cloneImpl(); }
    2728             : 
    2729             :   /// Get the local scope for this label.
    2730             :   ///
    2731             :   /// Labels must be defined in a local scope.
    2732             :   DILocalScope *getScope() const {
    2733             :     return cast_or_null<DILocalScope>(getRawScope());
    2734             :   }
    2735           0 :   unsigned getLine() const { return Line; }
    2736          22 :   StringRef getName() const { return getStringOperand(1); }
    2737             :   DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
    2738             : 
    2739         239 :   Metadata *getRawScope() const { return getOperand(0); }
    2740             :   MDString *getRawName() const { return getOperandAs<MDString>(1); }
    2741           7 :   Metadata *getRawFile() const { return getOperand(2); }
    2742             : 
    2743             :   /// Check that a location is valid for this label.
    2744             :   ///
    2745             :   /// Check that \c DL exists, is in the same subprogram, and has the same
    2746             :   /// inlined-at location as \c this.  (Otherwise, it's not a valid attachment
    2747             :   /// to a \a DbgInfoIntrinsic.)
    2748             :   bool isValidLocationForIntrinsic(const DILocation *DL) const {
    2749             :     return DL && getScope()->getSubprogram() == DL->getScope()->getSubprogram();
    2750             :   }
    2751             : 
    2752             :   static bool classof(const Metadata *MD) {
    2753           0 :     return MD->getMetadataID() == DILabelKind;
    2754             :   }
    2755             : };
    2756             : 
    2757             : class DIObjCProperty : public DINode {
    2758             :   friend class LLVMContextImpl;
    2759             :   friend class MDNode;
    2760             : 
    2761             :   unsigned Line;
    2762             :   unsigned Attributes;
    2763             : 
    2764             :   DIObjCProperty(LLVMContext &C, StorageType Storage, unsigned Line,
    2765             :                  unsigned Attributes, ArrayRef<Metadata *> Ops)
    2766          91 :       : DINode(C, DIObjCPropertyKind, Storage, dwarf::DW_TAG_APPLE_property,
    2767             :                Ops),
    2768          91 :         Line(Line), Attributes(Attributes) {}
    2769          90 :   ~DIObjCProperty() = default;
    2770             : 
    2771             :   static DIObjCProperty *
    2772          84 :   getImpl(LLVMContext &Context, StringRef Name, DIFile *File, unsigned Line,
    2773             :           StringRef GetterName, StringRef SetterName, unsigned Attributes,
    2774             :           DITypeRef Type, StorageType Storage, bool ShouldCreate = true) {
    2775         197 :     return getImpl(Context, getCanonicalMDString(Context, Name), File, Line,
    2776             :                    getCanonicalMDString(Context, GetterName),
    2777             :                    getCanonicalMDString(Context, SetterName), Attributes, Type,
    2778          84 :                    Storage, ShouldCreate);
    2779             :   }
    2780             :   static DIObjCProperty *getImpl(LLVMContext &Context, MDString *Name,
    2781             :                                  Metadata *File, unsigned Line,
    2782             :                                  MDString *GetterName, MDString *SetterName,
    2783             :                                  unsigned Attributes, Metadata *Type,
    2784             :                                  StorageType Storage, bool ShouldCreate = true);
    2785             : 
    2786           1 :   TempDIObjCProperty cloneImpl() const {
    2787             :     return getTemporary(getContext(), getName(), getFile(), getLine(),
    2788             :                         getGetterName(), getSetterName(), getAttributes(),
    2789           1 :                         getType());
    2790             :   }
    2791             : 
    2792             : public:
    2793          84 :   DEFINE_MDNODE_GET(DIObjCProperty,
    2794             :                     (StringRef Name, DIFile *File, unsigned Line,
    2795             :                      StringRef GetterName, StringRef SetterName,
    2796             :                      unsigned Attributes, DITypeRef Type),
    2797             :                     (Name, File, Line, GetterName, SetterName, Attributes,
    2798             :                      Type))
    2799          39 :   DEFINE_MDNODE_GET(DIObjCProperty,
    2800             :                     (MDString * Name, Metadata *File, unsigned Line,
    2801             :                      MDString *GetterName, MDString *SetterName,
    2802             :                      unsigned Attributes, Metadata *Type),
    2803             :                     (Name, File, Line, GetterName, SetterName, Attributes,
    2804             :                      Type))
    2805             : 
    2806           1 :   TempDIObjCProperty clone() const { return cloneImpl(); }
    2807             : 
    2808           0 :   unsigned getLine() const { return Line; }
    2809           0 :   unsigned getAttributes() const { return Attributes; }
    2810          64 :   StringRef getName() const { return getStringOperand(0); }
    2811             :   DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
    2812          64 :   StringRef getGetterName() const { return getStringOperand(2); }
    2813          64 :   StringRef getSetterName() const { return getStringOperand(3); }
    2814             :   DITypeRef getType() const { return DITypeRef(getRawType()); }
    2815             : 
    2816             :   StringRef getFilename() const {
    2817             :     if (auto *F = getFile())
    2818             :       return F->getFilename();
    2819             :     return "";
    2820             :   }
    2821             : 
    2822             :   StringRef getDirectory() const {
    2823             :     if (auto *F = getFile())
    2824             :       return F->getDirectory();
    2825             :     return "";
    2826             :   }
    2827             : 
    2828             :   Optional<StringRef> getSource() const {
    2829             :     if (auto *F = getFile())
    2830             :       return F->getSource();
    2831             :     return None;
    2832             :   }
    2833             : 
    2834             :   MDString *getRawName() const { return getOperandAs<MDString>(0); }
    2835         203 :   Metadata *getRawFile() const { return getOperand(1); }
    2836             :   MDString *getRawGetterName() const { return getOperandAs<MDString>(2); }
    2837             :   MDString *getRawSetterName() const { return getOperandAs<MDString>(3); }
    2838         118 :   Metadata *getRawType() const { return getOperand(4); }
    2839             : 
    2840             :   static bool classof(const Metadata *MD) {
    2841           0 :     return MD->getMetadataID() == DIObjCPropertyKind;
    2842             :   }
    2843             : };
    2844             : 
    2845             : /// An imported module (C++ using directive or similar).
    2846             : class DIImportedEntity : public DINode {
    2847             :   friend class LLVMContextImpl;
    2848             :   friend class MDNode;
    2849             : 
    2850             :   unsigned Line;
    2851             : 
    2852             :   DIImportedEntity(LLVMContext &C, StorageType Storage, unsigned Tag,
    2853             :                    unsigned Line, ArrayRef<Metadata *> Ops)
    2854       16758 :       : DINode(C, DIImportedEntityKind, Storage, Tag, Ops), Line(Line) {}
    2855         352 :   ~DIImportedEntity() = default;
    2856             : 
    2857       16471 :   static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
    2858             :                                    DIScope *Scope, DINodeRef Entity,
    2859             :                                    DIFile *File, unsigned Line, StringRef Name,
    2860             :                                    StorageType Storage,
    2861             :                                    bool ShouldCreate = true) {
    2862       16501 :     return getImpl(Context, Tag, Scope, Entity, File, Line,
    2863       16471 :                    getCanonicalMDString(Context, Name), Storage, ShouldCreate);
    2864             :   }
    2865             :   static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
    2866             :                                    Metadata *Scope, Metadata *Entity,
    2867             :                                    Metadata *File, unsigned Line,
    2868             :                                    MDString *Name, StorageType Storage,
    2869             :                                    bool ShouldCreate = true);
    2870             : 
    2871           1 :   TempDIImportedEntity cloneImpl() const {
    2872             :     return getTemporary(getContext(), getTag(), getScope(), getEntity(),
    2873           1 :                         getFile(), getLine(), getName());
    2874             :   }
    2875             : 
    2876             : public:
    2877       16471 :   DEFINE_MDNODE_GET(DIImportedEntity,
    2878             :                     (unsigned Tag, DIScope *Scope, DINodeRef Entity,
    2879             :                      DIFile *File, unsigned Line, StringRef Name = ""),
    2880             :                     (Tag, Scope, Entity, File, Line, Name))
    2881         293 :   DEFINE_MDNODE_GET(DIImportedEntity,
    2882             :                     (unsigned Tag, Metadata *Scope, Metadata *Entity,
    2883             :                      Metadata *File, unsigned Line, MDString *Name),
    2884             :                     (Tag, Scope, Entity, File, Line, Name))
    2885             : 
    2886           1 :   TempDIImportedEntity clone() const { return cloneImpl(); }
    2887             : 
    2888           0 :   unsigned getLine() const { return Line; }
    2889             :   DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
    2890             :   DINodeRef getEntity() const { return DINodeRef(getRawEntity()); }
    2891       16723 :   StringRef getName() const { return getStringOperand(2); }
    2892             :   DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
    2893             : 
    2894       97823 :   Metadata *getRawScope() const { return getOperand(0); }
    2895       16647 :   Metadata *getRawEntity() const { return getOperand(1); }
    2896             :   MDString *getRawName() const { return getOperandAs<MDString>(2); }
    2897           2 :   Metadata *getRawFile() const { return getOperand(3); }
    2898             : 
    2899             :   static bool classof(const Metadata *MD) {
    2900             :     return MD->getMetadataID() == DIImportedEntityKind;
    2901             :   }
    2902             : };
    2903             : 
    2904             : /// A pair of DIGlobalVariable and DIExpression.
    2905             : class DIGlobalVariableExpression : public MDNode {
    2906             :   friend class LLVMContextImpl;
    2907             :   friend class MDNode;
    2908             : 
    2909             :   DIGlobalVariableExpression(LLVMContext &C, StorageType Storage,
    2910             :                              ArrayRef<Metadata *> Ops)
    2911        3884 :       : MDNode(C, DIGlobalVariableExpressionKind, Storage, Ops) {}
    2912        1473 :   ~DIGlobalVariableExpression() = default;
    2913             : 
    2914             :   static DIGlobalVariableExpression *
    2915             :   getImpl(LLVMContext &Context, Metadata *Variable, Metadata *Expression,
    2916             :           StorageType Storage, bool ShouldCreate = true);
    2917             : 
    2918           2 :   TempDIGlobalVariableExpression cloneImpl() const {
    2919           2 :     return getTemporary(getContext(), getVariable(), getExpression());
    2920             :   }
    2921             : 
    2922             : public:
    2923        1945 :   DEFINE_MDNODE_GET(DIGlobalVariableExpression,
    2924             :                     (Metadata * Variable, Metadata *Expression),
    2925             :                     (Variable, Expression))
    2926             : 
    2927           1 :   TempDIGlobalVariableExpression clone() const { return cloneImpl(); }
    2928             : 
    2929       18653 :   Metadata *getRawVariable() const { return getOperand(0); }
    2930             : 
    2931             :   DIGlobalVariable *getVariable() const {
    2932             :     return cast_or_null<DIGlobalVariable>(getRawVariable());
    2933             :   }
    2934             : 
    2935           7 :   Metadata *getRawExpression() const { return getOperand(1); }
    2936             : 
    2937             :   DIExpression *getExpression() const {
    2938             :     return cast<DIExpression>(getRawExpression());
    2939             :   }
    2940             : 
    2941             :   static bool classof(const Metadata *MD) {
    2942        3482 :     return MD->getMetadataID() == DIGlobalVariableExpressionKind;
    2943             :   }
    2944             : };
    2945             : 
    2946             : /// Macro Info DWARF-like metadata node.
    2947             : ///
    2948             : /// A metadata node with a DWARF macro info (i.e., a constant named
    2949             : /// \c DW_MACINFO_*, defined in llvm/BinaryFormat/Dwarf.h).  Called \a
    2950             : /// DIMacroNode
    2951             : /// because it's potentially used for non-DWARF output.
    2952             : class DIMacroNode : public MDNode {
    2953             :   friend class LLVMContextImpl;
    2954             :   friend class MDNode;
    2955             : 
    2956             : protected:
    2957             :   DIMacroNode(LLVMContext &C, unsigned ID, StorageType Storage, unsigned MIType,
    2958             :               ArrayRef<Metadata *> Ops1, ArrayRef<Metadata *> Ops2 = None)
    2959        1541 :       : MDNode(C, ID, Storage, Ops1, Ops2) {
    2960             :     assert(MIType < 1u << 16);
    2961        1541 :     SubclassData16 = MIType;
    2962             :   }
    2963             :   ~DIMacroNode() = default;
    2964             : 
    2965             :   template <class Ty> Ty *getOperandAs(unsigned I) const {
    2966       10677 :     return cast_or_null<Ty>(getOperand(I));
    2967             :   }
    2968             : 
    2969        5834 :   StringRef getStringOperand(unsigned I) const {
    2970             :     if (auto *S = getOperandAs<MDString>(I))
    2971        5725 :       return S->getString();
    2972         109 :     return StringRef();
    2973             :   }
    2974             : 
    2975             :   static MDString *getCanonicalMDString(LLVMContext &Context, StringRef S) {
    2976        2856 :     if (S.empty())
    2977             :       return nullptr;
    2978        2796 :     return MDString::get(Context, S);
    2979             :   }
    2980             : 
    2981             : public:
    2982        7940 :   unsigned getMacinfoType() const { return SubclassData16; }
    2983             : 
    2984             :   static bool classof(const Metadata *MD) {
    2985        3130 :     switch (MD->getMetadataID()) {
    2986             :     default:
    2987             :       return false;
    2988           0 :     case DIMacroKind:
    2989             :     case DIMacroFileKind:
    2990             :       return true;
    2991             :     }
    2992             :   }
    2993             : };
    2994             : 
    2995             : class DIMacro : public DIMacroNode {
    2996             :   friend class LLVMContextImpl;
    2997             :   friend class MDNode;
    2998             : 
    2999             :   unsigned Line;
    3000             : 
    3001             :   DIMacro(LLVMContext &C, StorageType Storage, unsigned MIType, unsigned Line,
    3002             :           ArrayRef<Metadata *> Ops)
    3003        1451 :       : DIMacroNode(C, DIMacroKind, Storage, MIType, Ops), Line(Line) {}
    3004        1451 :   ~DIMacro() = default;
    3005             : 
    3006        1428 :   static DIMacro *getImpl(LLVMContext &Context, unsigned MIType, unsigned Line,
    3007             :                           StringRef Name, StringRef Value, StorageType Storage,
    3008             :                           bool ShouldCreate = true) {
    3009        4224 :     return getImpl(Context, MIType, Line, getCanonicalMDString(Context, Name),
    3010        1428 :                    getCanonicalMDString(Context, Value), Storage, ShouldCreate);
    3011             :   }
    3012             :   static DIMacro *getImpl(LLVMContext &Context, unsigned MIType, unsigned Line,
    3013             :                           MDString *Name, MDString *Value, StorageType Storage,
    3014             :                           bool ShouldCreate = true);
    3015             : 
    3016           0 :   TempDIMacro cloneImpl() const {
    3017             :     return getTemporary(getContext(), getMacinfoType(), getLine(), getName(),
    3018           0 :                         getValue());
    3019             :   }
    3020             : 
    3021             : public:
    3022        1428 :   DEFINE_MDNODE_GET(DIMacro, (unsigned MIType, unsigned Line, StringRef Name,
    3023             :                               StringRef Value = ""),
    3024             :                     (MIType, Line, Name, Value))
    3025          34 :   DEFINE_MDNODE_GET(DIMacro, (unsigned MIType, unsigned Line, MDString *Name,
    3026             :                               MDString *Value),
    3027             :                     (MIType, Line, Name, Value))
    3028             : 
    3029             :   TempDIMacro clone() const { return cloneImpl(); }
    3030             : 
    3031           0 :   unsigned getLine() const { return Line; }
    3032             : 
    3033        2917 :   StringRef getName() const { return getStringOperand(0); }
    3034        2917 :   StringRef getValue() const { return getStringOperand(1); }
    3035             : 
    3036             :   MDString *getRawName() const { return getOperandAs<MDString>(0); }
    3037             :   MDString *getRawValue() const { return getOperandAs<MDString>(1); }
    3038             : 
    3039             :   static bool classof(const Metadata *MD) {
    3040           7 :     return MD->getMetadataID() == DIMacroKind;
    3041             :   }
    3042             : };
    3043             : 
    3044             : class DIMacroFile : public DIMacroNode {
    3045             :   friend class LLVMContextImpl;
    3046             :   friend class MDNode;
    3047             : 
    3048             :   unsigned Line;
    3049             : 
    3050             :   DIMacroFile(LLVMContext &C, StorageType Storage, unsigned MIType,
    3051             :               unsigned Line, ArrayRef<Metadata *> Ops)
    3052          90 :       : DIMacroNode(C, DIMacroFileKind, Storage, MIType, Ops), Line(Line) {}
    3053          90 :   ~DIMacroFile() = default;
    3054             : 
    3055             :   static DIMacroFile *getImpl(LLVMContext &Context, unsigned MIType,
    3056             :                               unsigned Line, DIFile *File,
    3057             :                               DIMacroNodeArray Elements, StorageType Storage,
    3058             :                               bool ShouldCreate = true) {
    3059          46 :     return getImpl(Context, MIType, Line, static_cast<Metadata *>(File),
    3060             :                    Elements.get(), Storage, ShouldCreate);
    3061             :   }
    3062             : 
    3063             :   static DIMacroFile *getImpl(LLVMContext &Context, unsigned MIType,
    3064             :                               unsigned Line, Metadata *File, Metadata *Elements,
    3065             :                               StorageType Storage, bool ShouldCreate = true);
    3066             : 
    3067           0 :   TempDIMacroFile cloneImpl() const {
    3068             :     return getTemporary(getContext(), getMacinfoType(), getLine(), getFile(),
    3069           0 :                         getElements());
    3070             :   }
    3071             : 
    3072             : public:
    3073             :   DEFINE_MDNODE_GET(DIMacroFile, (unsigned MIType, unsigned Line, DIFile *File,
    3074             :                                   DIMacroNodeArray Elements),
    3075             :                     (MIType, Line, File, Elements))
    3076          46 :   DEFINE_MDNODE_GET(DIMacroFile, (unsigned MIType, unsigned Line,
    3077             :                                   Metadata *File, Metadata *Elements),
    3078             :                     (MIType, Line, File, Elements))
    3079             : 
    3080             :   TempDIMacroFile clone() const { return cloneImpl(); }
    3081             : 
    3082             :   void replaceElements(DIMacroNodeArray Elements) {
    3083             : #ifndef NDEBUG
    3084             :     for (DIMacroNode *Op : getElements())
    3085             :       assert(is_contained(Elements->operands(), Op) &&
    3086             :              "Lost a macro node during macro node list replacement");
    3087             : #endif
    3088             :     replaceOperandWith(1, Elements.get());
    3089             :   }
    3090             : 
    3091           0 :   unsigned getLine() const { return Line; }
    3092             :   DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
    3093             : 
    3094             :   DIMacroNodeArray getElements() const {
    3095             :     return cast_or_null<MDTuple>(getRawElements());
    3096             :   }
    3097             : 
    3098         294 :   Metadata *getRawFile() const { return getOperand(0); }
    3099           2 :   Metadata *getRawElements() const { return getOperand(1); }
    3100             : 
    3101             :   static bool classof(const Metadata *MD) {
    3102           0 :     return MD->getMetadataID() == DIMacroFileKind;
    3103             :   }
    3104             : };
    3105             : 
    3106             : } // end namespace llvm
    3107             : 
    3108             : #undef DEFINE_MDNODE_GET_UNPACK_IMPL
    3109             : #undef DEFINE_MDNODE_GET_UNPACK
    3110             : #undef DEFINE_MDNODE_GET
    3111             : 
    3112             : #endif // LLVM_IR_DEBUGINFOMETADATA_H

Generated by: LCOV version 1.13