LCOV - code coverage report
Current view: top level - lib/IR - DebugInfoMetadata.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 333 343 97.1 %
Date: 2018-10-20 13:21:21 Functions: 60 60 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- DebugInfoMetadata.cpp - Implement debug info metadata --------------===//
       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             : // This file implements the debug info Metadata classes.
      11             : //
      12             : //===----------------------------------------------------------------------===//
      13             : 
      14             : #include "llvm/IR/DebugInfoMetadata.h"
      15             : #include "LLVMContextImpl.h"
      16             : #include "MetadataImpl.h"
      17             : #include "llvm/ADT/SmallSet.h"
      18             : #include "llvm/ADT/StringSwitch.h"
      19             : #include "llvm/IR/DIBuilder.h"
      20             : #include "llvm/IR/Function.h"
      21             : #include "llvm/IR/Instructions.h"
      22             : 
      23             : using namespace llvm;
      24             : 
      25     1502103 : DILocation::DILocation(LLVMContext &C, StorageType Storage, unsigned Line,
      26             :                        unsigned Column, ArrayRef<Metadata *> MDs,
      27     1502103 :                        bool ImplicitCode)
      28     3004206 :     : MDNode(C, DILocationKind, Storage, MDs) {
      29             :   assert((MDs.size() == 1 || MDs.size() == 2) &&
      30             :          "Expected a scope and optional inlined-at");
      31             : 
      32             :   // Set line and column.
      33             :   assert(Column < (1u << 16) && "Expected 16-bit column");
      34             : 
      35     1502103 :   SubclassData32 = Line;
      36     1502103 :   SubclassData16 = Column;
      37             : 
      38             :   setImplicitCode(ImplicitCode);
      39     1502103 : }
      40             : 
      41             : static void adjustColumn(unsigned &Column) {
      42             :   // Set to unknown on overflow.  We only have 16 bits to play with here.
      43     3985708 :   if (Column >= (1u << 16))
      44             :     Column = 0;
      45             : }
      46             : 
      47     3971246 : DILocation *DILocation::getImpl(LLVMContext &Context, unsigned Line,
      48             :                                 unsigned Column, Metadata *Scope,
      49             :                                 Metadata *InlinedAt, bool ImplicitCode,
      50             :                                 StorageType Storage, bool ShouldCreate) {
      51             :   // Fixup column.
      52             :   adjustColumn(Column);
      53             : 
      54     3971246 :   if (Storage == Uniqued) {
      55     3712300 :     if (auto *N = getUniqued(Context.pImpl->DILocations,
      56     7424600 :                              DILocationInfo::KeyTy(Line, Column, Scope,
      57             :                                                    InlinedAt, ImplicitCode)))
      58             :       return N;
      59     1243157 :     if (!ShouldCreate)
      60             :       return nullptr;
      61             :   } else {
      62             :     assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
      63             :   }
      64             : 
      65             :   SmallVector<Metadata *, 2> Ops;
      66     1502103 :   Ops.push_back(Scope);
      67     1502103 :   if (InlinedAt)
      68      872518 :     Ops.push_back(InlinedAt);
      69     1502103 :   return storeImpl(new (Ops.size()) DILocation(Context, Storage, Line, Column,
      70     1502103 :                                                Ops, ImplicitCode),
      71     1502103 :                    Storage, Context.pImpl->DILocations);
      72             : }
      73             : 
      74      262730 : const DILocation *DILocation::getMergedLocation(const DILocation *LocA,
      75             :                                                 const DILocation *LocB) {
      76      262730 :   if (!LocA || !LocB)
      77             :     return nullptr;
      78             : 
      79      189051 :   if (LocA == LocB)
      80             :     return LocA;
      81             : 
      82             :   SmallPtrSet<DILocation *, 5> InlinedLocationsA;
      83      122548 :   for (DILocation *L = LocA->getInlinedAt(); L; L = L->getInlinedAt())
      84       41263 :     InlinedLocationsA.insert(L);
      85      105640 :   SmallSet<std::pair<DIScope *, DILocation *>, 5> Locations;
      86             :   DIScope *S = LocA->getScope();
      87             :   DILocation *L = LocA->getInlinedAt();
      88      556411 :   while (S) {
      89      450771 :     Locations.insert(std::make_pair(S, L));
      90      450771 :     S = S->getScope().resolve();
      91      146903 :     if (!S && L) {
      92             :       S = L->getScope();
      93             :       L = L->getInlinedAt();
      94             :     }
      95             :   }
      96             :   const DILocation *Result = LocB;
      97             :   S = LocB->getScope();
      98             :   L = LocB->getInlinedAt();
      99      493076 :   while (S) {
     100      493075 :     if (Locations.count(std::make_pair(S, L)))
     101             :       break;
     102      387436 :     S = S->getScope().resolve();
     103      150931 :     if (!S && L) {
     104             :       S = L->getScope();
     105             :       L = L->getInlinedAt();
     106             :     }
     107             :   }
     108             : 
     109             :   // If the two locations are irreconsilable, just pick one. This is misleading,
     110             :   // but on the other hand, it's a "line 0" location.
     111      105640 :   if (!S || !isa<DILocalScope>(S))
     112             :     S = LocA->getScope();
     113             :   return DILocation::get(Result->getContext(), 0, 0, S, L);
     114             : }
     115             : 
     116        3744 : DINode::DIFlags DINode::getFlag(StringRef Flag) {
     117        3744 :   return StringSwitch<DIFlags>(Flag)
     118             : #define HANDLE_DI_FLAG(ID, NAME) .Case("DIFlag" #NAME, Flag##NAME)
     119             : #include "llvm/IR/DebugInfoFlags.def"
     120        3744 :       .Default(DINode::FlagZero);
     121             : }
     122             : 
     123        9782 : StringRef DINode::getFlagString(DIFlags Flag) {
     124        9782 :   switch (Flag) {
     125             : #define HANDLE_DI_FLAG(ID, NAME)                                               \
     126             :   case Flag##NAME:                                                             \
     127             :     return "DIFlag" #NAME;
     128             : #include "llvm/IR/DebugInfoFlags.def"
     129             :   }
     130           3 :   return "";
     131             : }
     132             : 
     133        7542 : DINode::DIFlags DINode::splitFlags(DIFlags Flags,
     134             :                                    SmallVectorImpl<DIFlags> &SplitFlags) {
     135             :   // Flags that are packed together need to be specially handled, so
     136             :   // that, for example, we emit "DIFlagPublic" and not
     137             :   // "DIFlagPrivate | DIFlagProtected".
     138        7542 :   if (DIFlags A = Flags & FlagAccessibility) {
     139         517 :     if (A == FlagPrivate)
     140          39 :       SplitFlags.push_back(FlagPrivate);
     141         478 :     else if (A == FlagProtected)
     142          65 :       SplitFlags.push_back(FlagProtected);
     143             :     else
     144         413 :       SplitFlags.push_back(FlagPublic);
     145             :     Flags &= ~A;
     146             :   }
     147        7542 :   if (DIFlags R = Flags & FlagPtrToMemberRep) {
     148          14 :     if (R == FlagSingleInheritance)
     149          10 :       SplitFlags.push_back(FlagSingleInheritance);
     150           4 :     else if (R == FlagMultipleInheritance)
     151           2 :       SplitFlags.push_back(FlagMultipleInheritance);
     152             :     else
     153           2 :       SplitFlags.push_back(FlagVirtualInheritance);
     154             :     Flags &= ~R;
     155             :   }
     156        7542 :   if ((Flags & FlagIndirectVirtualBase) == FlagIndirectVirtualBase) {
     157             :     Flags &= ~FlagIndirectVirtualBase;
     158           1 :     SplitFlags.push_back(FlagIndirectVirtualBase);
     159             :   }
     160             : 
     161             : #define HANDLE_DI_FLAG(ID, NAME)                                               \
     162             :   if (DIFlags Bit = Flags & Flag##NAME) {                                      \
     163             :     SplitFlags.push_back(Bit);                                                 \
     164             :     Flags &= ~Bit;                                                             \
     165             :   }
     166             : #include "llvm/IR/DebugInfoFlags.def"
     167        7542 :   return Flags;
     168             : }
     169             : 
     170      919081 : DIScopeRef DIScope::getScope() const {
     171             :   if (auto *T = dyn_cast<DIType>(this))
     172             :     return T->getScope();
     173             : 
     174             :   if (auto *SP = dyn_cast<DISubprogram>(this))
     175             :     return SP->getScope();
     176             : 
     177             :   if (auto *LB = dyn_cast<DILexicalBlockBase>(this))
     178      242688 :     return LB->getScope();
     179             : 
     180             :   if (auto *NS = dyn_cast<DINamespace>(this))
     181       87799 :     return NS->getScope();
     182             : 
     183             :   if (auto *M = dyn_cast<DIModule>(this))
     184          12 :     return M->getScope();
     185             : 
     186             :   assert((isa<DIFile>(this) || isa<DICompileUnit>(this)) &&
     187             :          "Unhandled type of scope.");
     188      252892 :   return nullptr;
     189             : }
     190             : 
     191        1077 : StringRef DIScope::getName() const {
     192             :   if (auto *T = dyn_cast<DIType>(this))
     193             :     return T->getName();
     194             :   if (auto *SP = dyn_cast<DISubprogram>(this))
     195             :     return SP->getName();
     196             :   if (auto *NS = dyn_cast<DINamespace>(this))
     197             :     return NS->getName();
     198             :   if (auto *M = dyn_cast<DIModule>(this))
     199             :     return M->getName();
     200             :   assert((isa<DILexicalBlockBase>(this) || isa<DIFile>(this) ||
     201             :           isa<DICompileUnit>(this)) &&
     202             :          "Unhandled type of scope.");
     203         434 :   return "";
     204             : }
     205             : 
     206             : #ifndef NDEBUG
     207             : static bool isCanonical(const MDString *S) {
     208             :   return !S || !S->getString().empty();
     209             : }
     210             : #endif
     211             : 
     212          61 : GenericDINode *GenericDINode::getImpl(LLVMContext &Context, unsigned Tag,
     213             :                                       MDString *Header,
     214             :                                       ArrayRef<Metadata *> DwarfOps,
     215             :                                       StorageType Storage, bool ShouldCreate) {
     216             :   unsigned Hash = 0;
     217          61 :   if (Storage == Uniqued) {
     218             :     GenericDINodeInfo::KeyTy Key(Tag, Header, DwarfOps);
     219          62 :     if (auto *N = getUniqued(Context.pImpl->GenericDINodes, Key))
     220          13 :       return N;
     221          36 :     if (!ShouldCreate)
     222             :       return nullptr;
     223          36 :     Hash = Key.getHash();
     224             :   } else {
     225             :     assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
     226             :   }
     227             : 
     228             :   // Use a nullptr for empty headers.
     229             :   assert(isCanonical(Header) && "Expected canonical MDString");
     230          48 :   Metadata *PreOps[] = {Header};
     231          48 :   return storeImpl(new (DwarfOps.size() + 1) GenericDINode(
     232          48 :                        Context, Storage, Hash, Tag, PreOps, DwarfOps),
     233          48 :                    Storage, Context.pImpl->GenericDINodes);
     234             : }
     235             : 
     236          15 : void GenericDINode::recalculateHash() {
     237             :   setHash(GenericDINodeInfo::KeyTy::calculateHash(this));
     238          15 : }
     239             : 
     240             : #define UNWRAP_ARGS_IMPL(...) __VA_ARGS__
     241             : #define UNWRAP_ARGS(ARGS) UNWRAP_ARGS_IMPL ARGS
     242             : #define DEFINE_GETIMPL_LOOKUP(CLASS, ARGS)                                     \
     243             :   do {                                                                         \
     244             :     if (Storage == Uniqued) {                                                  \
     245             :       if (auto *N = getUniqued(Context.pImpl->CLASS##s,                        \
     246             :                                CLASS##Info::KeyTy(UNWRAP_ARGS(ARGS))))         \
     247             :         return N;                                                              \
     248             :       if (!ShouldCreate)                                                       \
     249             :         return nullptr;                                                        \
     250             :     } else {                                                                   \
     251             :       assert(ShouldCreate &&                                                   \
     252             :              "Expected non-uniqued nodes to always be created");               \
     253             :     }                                                                          \
     254             :   } while (false)
     255             : #define DEFINE_GETIMPL_STORE(CLASS, ARGS, OPS)                                 \
     256             :   return storeImpl(new (array_lengthof(OPS))                                   \
     257             :                        CLASS(Context, Storage, UNWRAP_ARGS(ARGS), OPS),        \
     258             :                    Storage, Context.pImpl->CLASS##s)
     259             : #define DEFINE_GETIMPL_STORE_NO_OPS(CLASS, ARGS)                               \
     260             :   return storeImpl(new (0u) CLASS(Context, Storage, UNWRAP_ARGS(ARGS)),        \
     261             :                    Storage, Context.pImpl->CLASS##s)
     262             : #define DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(CLASS, OPS)                   \
     263             :   return storeImpl(new (array_lengthof(OPS)) CLASS(Context, Storage, OPS),     \
     264             :                    Storage, Context.pImpl->CLASS##s)
     265             : #define DEFINE_GETIMPL_STORE_N(CLASS, ARGS, OPS, NUM_OPS)                      \
     266             :   return storeImpl(new (NUM_OPS)                                               \
     267             :                        CLASS(Context, Storage, UNWRAP_ARGS(ARGS), OPS),        \
     268             :                    Storage, Context.pImpl->CLASS##s)
     269             : 
     270         901 : DISubrange *DISubrange::getImpl(LLVMContext &Context, int64_t Count, int64_t Lo,
     271             :                                 StorageType Storage, bool ShouldCreate) {
     272             :   auto *CountNode = ConstantAsMetadata::get(
     273         901 :       ConstantInt::getSigned(Type::getInt64Ty(Context), Count));
     274         901 :   return getImpl(Context, CountNode, Lo, Storage, ShouldCreate);
     275             : }
     276             : 
     277         997 : DISubrange *DISubrange::getImpl(LLVMContext &Context, Metadata *CountNode,
     278             :                                 int64_t Lo, StorageType Storage,
     279             :                                 bool ShouldCreate) {
     280        1993 :   DEFINE_GETIMPL_LOOKUP(DISubrange, (CountNode, Lo));
     281         837 :   Metadata *Ops[] = { CountNode };
     282        1674 :   DEFINE_GETIMPL_STORE(DISubrange, (CountNode, Lo), Ops);
     283             : }
     284             : 
     285        7644 : DIEnumerator *DIEnumerator::getImpl(LLVMContext &Context, int64_t Value,
     286             :                                     bool IsUnsigned, MDString *Name,
     287             :                                     StorageType Storage, bool ShouldCreate) {
     288             :   assert(isCanonical(Name) && "Expected canonical MDString");
     289       15287 :   DEFINE_GETIMPL_LOOKUP(DIEnumerator, (Value, IsUnsigned, Name));
     290        7619 :   Metadata *Ops[] = {Name};
     291       15238 :   DEFINE_GETIMPL_STORE(DIEnumerator, (Value, IsUnsigned), Ops);
     292             : }
     293             : 
     294        4404 : DIBasicType *DIBasicType::getImpl(LLVMContext &Context, unsigned Tag,
     295             :                                   MDString *Name, uint64_t SizeInBits,
     296             :                                   uint32_t AlignInBits, unsigned Encoding,
     297             :                                   DIFlags Flags, StorageType Storage,
     298             :                                   bool ShouldCreate) {
     299             :   assert(isCanonical(Name) && "Expected canonical MDString");
     300        8806 :   DEFINE_GETIMPL_LOOKUP(DIBasicType,
     301             :                         (Tag, Name, SizeInBits, AlignInBits, Encoding, Flags));
     302        4016 :   Metadata *Ops[] = {nullptr, nullptr, Name};
     303        8032 :   DEFINE_GETIMPL_STORE(DIBasicType, (Tag, SizeInBits, AlignInBits, Encoding,
     304             :                       Flags), Ops);
     305             : }
     306             : 
     307         408 : Optional<DIBasicType::Signedness> DIBasicType::getSignedness() const {
     308         408 :   switch (getEncoding()) {
     309           9 :   case dwarf::DW_ATE_signed:
     310             :   case dwarf::DW_ATE_signed_char:
     311             :     return Signedness::Signed;
     312         396 :   case dwarf::DW_ATE_unsigned:
     313             :   case dwarf::DW_ATE_unsigned_char:
     314             :     return Signedness::Unsigned;
     315             :   default:
     316             :     return None;
     317             :   }
     318             : }
     319             : 
     320      105440 : DIDerivedType *DIDerivedType::getImpl(
     321             :     LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
     322             :     unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
     323             :     uint32_t AlignInBits, uint64_t OffsetInBits,
     324             :     Optional<unsigned> DWARFAddressSpace, DIFlags Flags, Metadata *ExtraData,
     325             :     StorageType Storage, bool ShouldCreate) {
     326             :   assert(isCanonical(Name) && "Expected canonical MDString");
     327      250794 :   DEFINE_GETIMPL_LOOKUP(DIDerivedType,
     328             :                         (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
     329             :                          AlignInBits, OffsetInBits, DWARFAddressSpace, Flags,
     330             :                          ExtraData));
     331       76239 :   Metadata *Ops[] = {File, Scope, Name, BaseType, ExtraData};
     332      152531 :   DEFINE_GETIMPL_STORE(
     333             :       DIDerivedType, (Tag, Line, SizeInBits, AlignInBits, OffsetInBits,
     334             :                       DWARFAddressSpace, Flags), Ops);
     335             : }
     336             : 
     337       10554 : DICompositeType *DICompositeType::getImpl(
     338             :     LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
     339             :     unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
     340             :     uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags,
     341             :     Metadata *Elements, unsigned RuntimeLang, Metadata *VTableHolder,
     342             :     Metadata *TemplateParams, MDString *Identifier, Metadata *Discriminator,
     343             :     StorageType Storage, bool ShouldCreate) {
     344             :   assert(isCanonical(Name) && "Expected canonical MDString");
     345             : 
     346             :   // Keep this in sync with buildODRType.
     347       12715 :   DEFINE_GETIMPL_LOOKUP(
     348             :       DICompositeType, (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
     349             :                         AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
     350             :                         VTableHolder, TemplateParams, Identifier, Discriminator));
     351             :   Metadata *Ops[] = {File,     Scope,        Name,           BaseType,
     352             :                      Elements, VTableHolder, TemplateParams, Identifier,
     353       10520 :                      Discriminator};
     354       21040 :   DEFINE_GETIMPL_STORE(DICompositeType, (Tag, Line, RuntimeLang, SizeInBits,
     355             :                                          AlignInBits, OffsetInBits, Flags),
     356             :                        Ops);
     357             : }
     358             : 
     359         653 : DICompositeType *DICompositeType::buildODRType(
     360             :     LLVMContext &Context, MDString &Identifier, unsigned Tag, MDString *Name,
     361             :     Metadata *File, unsigned Line, Metadata *Scope, Metadata *BaseType,
     362             :     uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits,
     363             :     DIFlags Flags, Metadata *Elements, unsigned RuntimeLang,
     364             :     Metadata *VTableHolder, Metadata *TemplateParams, Metadata *Discriminator) {
     365             :   assert(!Identifier.getString().empty() && "Expected valid identifier");
     366         653 :   if (!Context.isODRUniquingDebugTypes())
     367             :     return nullptr;
     368         163 :   auto *&CT = (*Context.pImpl->DITypeMap)[&Identifier];
     369         163 :   if (!CT)
     370         124 :     return CT = DICompositeType::getDistinct(
     371             :                Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
     372             :                AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
     373         124 :                VTableHolder, TemplateParams, &Identifier, Discriminator);
     374             : 
     375             :   // Only mutate CT if it's a forward declaration and the new operands aren't.
     376             :   assert(CT->getRawIdentifier() == &Identifier && "Wrong ODR identifier?");
     377          39 :   if (!CT->isForwardDecl() || (Flags & DINode::FlagFwdDecl))
     378             :     return CT;
     379             : 
     380             :   // Mutate CT in place.  Keep this in sync with getImpl.
     381             :   CT->mutate(Tag, Line, RuntimeLang, SizeInBits, AlignInBits, OffsetInBits,
     382             :              Flags);
     383             :   Metadata *Ops[] = {File,     Scope,        Name,           BaseType,
     384             :                      Elements, VTableHolder, TemplateParams, &Identifier,
     385           5 :                      Discriminator};
     386             :   assert((std::end(Ops) - std::begin(Ops)) == (int)CT->getNumOperands() &&
     387             :          "Mismatched number of operands");
     388          50 :   for (unsigned I = 0, E = CT->getNumOperands(); I != E; ++I)
     389          90 :     if (Ops[I] != CT->getOperand(I))
     390          11 :       CT->setOperand(I, Ops[I]);
     391           5 :   return CT;
     392             : }
     393             : 
     394           4 : DICompositeType *DICompositeType::getODRType(
     395             :     LLVMContext &Context, MDString &Identifier, unsigned Tag, MDString *Name,
     396             :     Metadata *File, unsigned Line, Metadata *Scope, Metadata *BaseType,
     397             :     uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits,
     398             :     DIFlags Flags, Metadata *Elements, unsigned RuntimeLang,
     399             :     Metadata *VTableHolder, Metadata *TemplateParams, Metadata *Discriminator) {
     400             :   assert(!Identifier.getString().empty() && "Expected valid identifier");
     401           4 :   if (!Context.isODRUniquingDebugTypes())
     402             :     return nullptr;
     403           3 :   auto *&CT = (*Context.pImpl->DITypeMap)[&Identifier];
     404           3 :   if (!CT)
     405           1 :     CT = DICompositeType::getDistinct(
     406             :         Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
     407             :         AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang, VTableHolder,
     408             :         TemplateParams, &Identifier, Discriminator);
     409           3 :   return CT;
     410             : }
     411             : 
     412           5 : DICompositeType *DICompositeType::getODRTypeIfExists(LLVMContext &Context,
     413             :                                                      MDString &Identifier) {
     414             :   assert(!Identifier.getString().empty() && "Expected valid identifier");
     415           5 :   if (!Context.isODRUniquingDebugTypes())
     416             :     return nullptr;
     417           8 :   return Context.pImpl->DITypeMap->lookup(&Identifier);
     418             : }
     419             : 
     420       90479 : DISubroutineType *DISubroutineType::getImpl(LLVMContext &Context, DIFlags Flags,
     421             :                                             uint8_t CC, Metadata *TypeArray,
     422             :                                             StorageType Storage,
     423             :                                             bool ShouldCreate) {
     424      180949 :   DEFINE_GETIMPL_LOOKUP(DISubroutineType, (Flags, CC, TypeArray));
     425       46706 :   Metadata *Ops[] = {nullptr, nullptr, nullptr, TypeArray};
     426       93412 :   DEFINE_GETIMPL_STORE(DISubroutineType, (Flags, CC), Ops);
     427             : }
     428             : 
     429             : // FIXME: Implement this string-enum correspondence with a .def file and macros,
     430             : // so that the association is explicit rather than implied.
     431             : static const char *ChecksumKindName[DIFile::CSK_Last] = {
     432             :   "CSK_MD5",
     433             :   "CSK_SHA1"
     434             : };
     435             : 
     436          74 : StringRef DIFile::getChecksumKindAsString(ChecksumKind CSKind) {
     437             :   assert(CSKind <= DIFile::CSK_Last && "Invalid checksum kind");
     438             :   // The first space was originally the CSK_None variant, which is now
     439             :   // obsolete, but the space is still reserved in ChecksumKind, so we account
     440             :   // for it here.
     441          74 :   return ChecksumKindName[CSKind - 1];
     442             : }
     443             : 
     444         176 : Optional<DIFile::ChecksumKind> DIFile::getChecksumKind(StringRef CSKindStr) {
     445         176 :   return StringSwitch<Optional<DIFile::ChecksumKind>>(CSKindStr)
     446         176 :       .Case("CSK_MD5", DIFile::CSK_MD5)
     447         176 :       .Case("CSK_SHA1", DIFile::CSK_SHA1)
     448         176 :       .Default(None);
     449             : }
     450             : 
     451       23978 : DIFile *DIFile::getImpl(LLVMContext &Context, MDString *Filename,
     452             :                         MDString *Directory,
     453             :                         Optional<DIFile::ChecksumInfo<MDString *>> CS,
     454             :                         Optional<MDString *> Source, StorageType Storage,
     455             :                         bool ShouldCreate) {
     456             :   assert(isCanonical(Filename) && "Expected canonical MDString");
     457             :   assert(isCanonical(Directory) && "Expected canonical MDString");
     458             :   assert((!CS || isCanonical(CS->Value)) && "Expected canonical MDString");
     459             :   assert((!Source || isCanonical(*Source)) && "Expected canonical MDString");
     460       71856 :   DEFINE_GETIMPL_LOOKUP(DIFile, (Filename, Directory, CS, Source));
     461         253 :   Metadata *Ops[] = {Filename, Directory, CS ? CS->Value : nullptr,
     462        9983 :                      Source.getValueOr(nullptr)};
     463       19460 :   DEFINE_GETIMPL_STORE(DIFile, (CS, Source), Ops);
     464             : }
     465             : 
     466        4178 : DICompileUnit *DICompileUnit::getImpl(
     467             :     LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
     468             :     MDString *Producer, bool IsOptimized, MDString *Flags,
     469             :     unsigned RuntimeVersion, MDString *SplitDebugFilename,
     470             :     unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
     471             :     Metadata *GlobalVariables, Metadata *ImportedEntities, Metadata *Macros,
     472             :     uint64_t DWOId, bool SplitDebugInlining, bool DebugInfoForProfiling,
     473             :     unsigned NameTableKind, StorageType Storage, bool ShouldCreate) {
     474             :   assert(Storage != Uniqued && "Cannot unique DICompileUnit");
     475             :   assert(isCanonical(Producer) && "Expected canonical MDString");
     476             :   assert(isCanonical(Flags) && "Expected canonical MDString");
     477             :   assert(isCanonical(SplitDebugFilename) && "Expected canonical MDString");
     478             : 
     479             :   Metadata *Ops[] = {
     480             :       File,      Producer,      Flags,           SplitDebugFilename,
     481             :       EnumTypes, RetainedTypes, GlobalVariables, ImportedEntities,
     482        4178 :       Macros};
     483             :   return storeImpl(new (array_lengthof(Ops)) DICompileUnit(
     484             :                        Context, Storage, SourceLanguage, IsOptimized,
     485             :                        RuntimeVersion, EmissionKind, DWOId, SplitDebugInlining,
     486        4178 :                        DebugInfoForProfiling, NameTableKind, Ops),
     487        4178 :                    Storage);
     488             : }
     489             : 
     490             : Optional<DICompileUnit::DebugEmissionKind>
     491        1901 : DICompileUnit::getEmissionKind(StringRef Str) {
     492        1901 :   return StringSwitch<Optional<DebugEmissionKind>>(Str)
     493        1901 :       .Case("NoDebug", NoDebug)
     494        1901 :       .Case("FullDebug", FullDebug)
     495        1901 :       .Case("LineTablesOnly", LineTablesOnly)
     496        1901 :       .Case("DebugDirectivesOnly", DebugDirectivesOnly)
     497        1901 :       .Default(None);
     498             : }
     499             : 
     500             : Optional<DICompileUnit::DebugNameTableKind>
     501          38 : DICompileUnit::getNameTableKind(StringRef Str) {
     502          38 :   return StringSwitch<Optional<DebugNameTableKind>>(Str)
     503          38 :       .Case("Default", DebugNameTableKind::Default)
     504          38 :       .Case("GNU", DebugNameTableKind::GNU)
     505          38 :       .Case("None", DebugNameTableKind::None)
     506          38 :       .Default(None);
     507             : }
     508             : 
     509        1178 : const char *DICompileUnit::emissionKindString(DebugEmissionKind EK) {
     510        1178 :   switch (EK) {
     511             :   case NoDebug:        return "NoDebug";
     512         838 :   case FullDebug:      return "FullDebug";
     513         200 :   case LineTablesOnly: return "LineTablesOnly";
     514          18 :   case DebugDirectivesOnly: return "DebugDirectivesOnly";
     515             :   }
     516           0 :   return nullptr;
     517             : }
     518             : 
     519         470 : const char *DICompileUnit::nameTableKindString(DebugNameTableKind NTK) {
     520         470 :   switch (NTK) {
     521             :   case DebugNameTableKind::Default:
     522             :     return nullptr;
     523           6 :   case DebugNameTableKind::GNU:
     524           6 :     return "GNU";
     525         464 :   case DebugNameTableKind::None:
     526         464 :     return "None";
     527             :   }
     528             :   return nullptr;
     529             : }
     530             : 
     531     2912273 : DISubprogram *DILocalScope::getSubprogram() const {
     532             :   if (auto *Block = dyn_cast<DILexicalBlockBase>(this))
     533     1145616 :     return Block->getScope()->getSubprogram();
     534             :   return const_cast<DISubprogram *>(cast<DISubprogram>(this));
     535             : }
     536             : 
     537     6110846 : DILocalScope *DILocalScope::getNonLexicalBlockFileScope() const {
     538             :   if (auto *File = dyn_cast<DILexicalBlockFile>(this))
     539     1340083 :     return File->getScope()->getNonLexicalBlockFileScope();
     540             :   return const_cast<DILocalScope *>(this);
     541             : }
     542             : 
     543       83953 : DISubprogram *DISubprogram::getImpl(
     544             :     LLVMContext &Context, Metadata *Scope, MDString *Name,
     545             :     MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
     546             :     bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
     547             :     Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
     548             :     int ThisAdjustment, DIFlags Flags, bool IsOptimized, Metadata *Unit,
     549             :     Metadata *TemplateParams, Metadata *Declaration, Metadata *RetainedNodes,
     550             :     Metadata *ThrownTypes, StorageType Storage, bool ShouldCreate) {
     551             :   assert(isCanonical(Name) && "Expected canonical MDString");
     552             :   assert(isCanonical(LinkageName) && "Expected canonical MDString");
     553      111785 :   DEFINE_GETIMPL_LOOKUP(
     554             :       DISubprogram, (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
     555             :                      IsDefinition, ScopeLine, ContainingType, Virtuality,
     556             :                      VirtualIndex, ThisAdjustment, Flags, IsOptimized, Unit,
     557             :                      TemplateParams, Declaration, RetainedNodes, ThrownTypes));
     558             :   SmallVector<Metadata *, 11> Ops = {
     559             :       File,        Scope,         Name,           LinkageName,    Type,       Unit,
     560       83945 :       Declaration, RetainedNodes, ContainingType, TemplateParams, ThrownTypes};
     561       83945 :   if (!ThrownTypes) {
     562             :     Ops.pop_back();
     563       83910 :     if (!TemplateParams) {
     564             :       Ops.pop_back();
     565       78538 :       if (!ContainingType)
     566             :         Ops.pop_back();
     567             :     }
     568             :   }
     569      251835 :   DEFINE_GETIMPL_STORE_N(DISubprogram,
     570             :                          (Line, ScopeLine, Virtuality, VirtualIndex,
     571             :                           ThisAdjustment, Flags, IsLocalToUnit, IsDefinition,
     572             :                           IsOptimized),
     573             :                          Ops, Ops.size());
     574             : }
     575             : 
     576       76975 : bool DISubprogram::describes(const Function *F) const {
     577             :   assert(F && "Invalid function");
     578       76975 :   if (F->getSubprogram() == this)
     579             :     return true;
     580             :   StringRef Name = getLinkageName();
     581       60156 :   if (Name.empty())
     582             :     Name = getName();
     583       60156 :   return F->getName() == Name;
     584             : }
     585             : 
     586       14462 : DILexicalBlock *DILexicalBlock::getImpl(LLVMContext &Context, Metadata *Scope,
     587             :                                         Metadata *File, unsigned Line,
     588             :                                         unsigned Column, StorageType Storage,
     589             :                                         bool ShouldCreate) {
     590             :   // Fixup column.
     591             :   adjustColumn(Column);
     592             : 
     593             :   assert(Scope && "Expected scope");
     594       14504 :   DEFINE_GETIMPL_LOOKUP(DILexicalBlock, (Scope, File, Line, Column));
     595       14458 :   Metadata *Ops[] = {File, Scope};
     596       28916 :   DEFINE_GETIMPL_STORE(DILexicalBlock, (Line, Column), Ops);
     597             : }
     598             : 
     599      247195 : DILexicalBlockFile *DILexicalBlockFile::getImpl(LLVMContext &Context,
     600             :                                                 Metadata *Scope, Metadata *File,
     601             :                                                 unsigned Discriminator,
     602             :                                                 StorageType Storage,
     603             :                                                 bool ShouldCreate) {
     604             :   assert(Scope && "Expected scope");
     605      494389 :   DEFINE_GETIMPL_LOOKUP(DILexicalBlockFile, (Scope, File, Discriminator));
     606       15550 :   Metadata *Ops[] = {File, Scope};
     607       31100 :   DEFINE_GETIMPL_STORE(DILexicalBlockFile, (Discriminator), Ops);
     608             : }
     609             : 
     610        2986 : DINamespace *DINamespace::getImpl(LLVMContext &Context, Metadata *Scope,
     611             :                                   MDString *Name, bool ExportSymbols,
     612             :                                   StorageType Storage, bool ShouldCreate) {
     613             :   assert(isCanonical(Name) && "Expected canonical MDString");
     614        5971 :   DEFINE_GETIMPL_LOOKUP(DINamespace, (Scope, Name, ExportSymbols));
     615             :   // The nullptr is for DIScope's File operand. This should be refactored.
     616         650 :   Metadata *Ops[] = {nullptr, Scope, Name};
     617        1300 :   DEFINE_GETIMPL_STORE(DINamespace, (ExportSymbols), Ops);
     618             : }
     619             : 
     620          97 : DIModule *DIModule::getImpl(LLVMContext &Context, Metadata *Scope,
     621             :                             MDString *Name, MDString *ConfigurationMacros,
     622             :                             MDString *IncludePath, MDString *ISysRoot,
     623             :                             StorageType Storage, bool ShouldCreate) {
     624             :   assert(isCanonical(Name) && "Expected canonical MDString");
     625         193 :   DEFINE_GETIMPL_LOOKUP(
     626             :       DIModule, (Scope, Name, ConfigurationMacros, IncludePath, ISysRoot));
     627          90 :   Metadata *Ops[] = {Scope, Name, ConfigurationMacros, IncludePath, ISysRoot};
     628         180 :   DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DIModule, Ops);
     629             : }
     630             : 
     631       14872 : DITemplateTypeParameter *DITemplateTypeParameter::getImpl(LLVMContext &Context,
     632             :                                                           MDString *Name,
     633             :                                                           Metadata *Type,
     634             :                                                           StorageType Storage,
     635             :                                                           bool ShouldCreate) {
     636             :   assert(isCanonical(Name) && "Expected canonical MDString");
     637       29742 :   DEFINE_GETIMPL_LOOKUP(DITemplateTypeParameter, (Name, Type));
     638        4931 :   Metadata *Ops[] = {Name, Type};
     639        9862 :   DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DITemplateTypeParameter, Ops);
     640             : }
     641             : 
     642        4120 : DITemplateValueParameter *DITemplateValueParameter::getImpl(
     643             :     LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *Type,
     644             :     Metadata *Value, StorageType Storage, bool ShouldCreate) {
     645             :   assert(isCanonical(Name) && "Expected canonical MDString");
     646        8237 :   DEFINE_GETIMPL_LOOKUP(DITemplateValueParameter, (Tag, Name, Type, Value));
     647        1323 :   Metadata *Ops[] = {Name, Type, Value};
     648        2646 :   DEFINE_GETIMPL_STORE(DITemplateValueParameter, (Tag), Ops);
     649             : }
     650             : 
     651             : DIGlobalVariable *
     652        1993 : DIGlobalVariable::getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
     653             :                           MDString *LinkageName, Metadata *File, unsigned Line,
     654             :                           Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
     655             :                           Metadata *StaticDataMemberDeclaration,
     656             :                           Metadata *TemplateParams, uint32_t AlignInBits,
     657             :                           StorageType Storage, bool ShouldCreate) {
     658             :   assert(isCanonical(Name) && "Expected canonical MDString");
     659             :   assert(isCanonical(LinkageName) && "Expected canonical MDString");
     660        2567 :   DEFINE_GETIMPL_LOOKUP(DIGlobalVariable, (Scope, Name, LinkageName, File, Line,
     661             :                                            Type, IsLocalToUnit, IsDefinition,
     662             :                                            StaticDataMemberDeclaration,
     663             :                                            TemplateParams, AlignInBits));
     664             :   Metadata *Ops[] = {Scope,
     665             :                      Name,
     666             :                      File,
     667             :                      Type,
     668             :                      Name,
     669             :                      LinkageName,
     670             :                      StaticDataMemberDeclaration,
     671        1992 :                      TemplateParams};
     672        3984 :   DEFINE_GETIMPL_STORE(DIGlobalVariable,
     673             :                        (Line, IsLocalToUnit, IsDefinition, AlignInBits), Ops);
     674             : }
     675             : 
     676       37669 : DILocalVariable *DILocalVariable::getImpl(LLVMContext &Context, Metadata *Scope,
     677             :                                           MDString *Name, Metadata *File,
     678             :                                           unsigned Line, Metadata *Type,
     679             :                                           unsigned Arg, DIFlags Flags,
     680             :                                           uint32_t AlignInBits,
     681             :                                           StorageType Storage,
     682             :                                           bool ShouldCreate) {
     683             :   // 64K ought to be enough for any frontend.
     684             :   assert(Arg <= UINT16_MAX && "Expected argument number to fit in 16-bits");
     685             : 
     686             :   assert(Scope && "Expected scope");
     687             :   assert(isCanonical(Name) && "Expected canonical MDString");
     688       75314 :   DEFINE_GETIMPL_LOOKUP(DILocalVariable,
     689             :                         (Scope, Name, File, Line, Type, Arg, Flags,
     690             :                          AlignInBits));
     691       37536 :   Metadata *Ops[] = {Scope, Name, File, Type};
     692       75072 :   DEFINE_GETIMPL_STORE(DILocalVariable, (Line, Arg, Flags, AlignInBits), Ops);
     693             : }
     694             : 
     695       60549 : Optional<uint64_t> DIVariable::getSizeInBits() const {
     696             :   // This is used by the Verifier so be mindful of broken types.
     697             :   const Metadata *RawType = getRawType();
     698       89252 :   while (RawType) {
     699             :     // Try to get the size directly.
     700             :     if (auto *T = dyn_cast<DIType>(RawType))
     701       89252 :       if (uint64_t Size = T->getSizeInBits())
     702             :         return Size;
     703             : 
     704             :     if (auto *DT = dyn_cast<DIDerivedType>(RawType)) {
     705             :       // Look at the base type.
     706             :       RawType = DT->getRawBaseType();
     707             :       continue;
     708             :     }
     709             : 
     710             :     // Missing type or size.
     711             :     break;
     712             :   }
     713             : 
     714             :   // Fail gracefully.
     715             :   return None;
     716             : }
     717             : 
     718          35 : DILabel *DILabel::getImpl(LLVMContext &Context, Metadata *Scope,
     719             :                           MDString *Name, Metadata *File, unsigned Line,
     720             :                           StorageType Storage,
     721             :                           bool ShouldCreate) {
     722             :   assert(Scope && "Expected scope");
     723             :   assert(isCanonical(Name) && "Expected canonical MDString");
     724          70 :   DEFINE_GETIMPL_LOOKUP(DILabel,
     725             :                         (Scope, Name, File, Line));
     726          35 :   Metadata *Ops[] = {Scope, Name, File};
     727          70 :   DEFINE_GETIMPL_STORE(DILabel, (Line), Ops);
     728             : }
     729             : 
     730       64938 : DIExpression *DIExpression::getImpl(LLVMContext &Context,
     731             :                                     ArrayRef<uint64_t> Elements,
     732             :                                     StorageType Storage, bool ShouldCreate) {
     733      129875 :   DEFINE_GETIMPL_LOOKUP(DIExpression, (Elements));
     734        3742 :   DEFINE_GETIMPL_STORE_NO_OPS(DIExpression, (Elements));
     735             : }
     736             : 
     737      272017 : unsigned DIExpression::ExprOperand::getSize() const {
     738      544034 :   switch (getOp()) {
     739             :   case dwarf::DW_OP_LLVM_fragment:
     740             :     return 3;
     741      156317 :   case dwarf::DW_OP_constu:
     742             :   case dwarf::DW_OP_plus_uconst:
     743      156317 :     return 2;
     744      112573 :   default:
     745      112573 :     return 1;
     746             :   }
     747             : }
     748             : 
     749       35022 : bool DIExpression::isValid() const {
     750       38465 :   for (auto I = expr_op_begin(), E = expr_op_end(); I != E; ++I) {
     751             :     // Check that there's space for the operand.
     752        4375 :     if (I->get() + I->getSize() > E->get())
     753         932 :       return false;
     754             : 
     755             :     // Check that the operand is valid.
     756        8744 :     switch (I->getOp()) {
     757             :     default:
     758             :       return false;
     759             :     case dwarf::DW_OP_LLVM_fragment:
     760             :       // A fragment operator must appear at the end.
     761         922 :       return I->get() + I->getSize() == E->get();
     762             :     case dwarf::DW_OP_stack_value: {
     763             :       // Must be the last one or followed by a DW_OP_LLVM_fragment.
     764         481 :       if (I->get() + I->getSize() == E->get())
     765             :         break;
     766          83 :       auto J = I;
     767          83 :       if ((++J)->getOp() != dwarf::DW_OP_LLVM_fragment)
     768           0 :         return false;
     769             :       break;
     770             :     }
     771             :     case dwarf::DW_OP_swap: {
     772             :       // Must be more than one implicit element on the stack.
     773             : 
     774             :       // FIXME: A better way to implement this would be to add a local variable
     775             :       // that keeps track of the stack depth and introduce something like a
     776             :       // DW_LLVM_OP_implicit_location as a placeholder for the location this
     777             :       // DIExpression is attached to, or else pass the number of implicit stack
     778             :       // elements into isValid.
     779         244 :       if (getNumElements() == 1)
     780             :         return false;
     781             :       break;
     782             :     }
     783             :     case dwarf::DW_OP_constu:
     784             :     case dwarf::DW_OP_plus_uconst:
     785             :     case dwarf::DW_OP_plus:
     786             :     case dwarf::DW_OP_minus:
     787             :     case dwarf::DW_OP_mul:
     788             :     case dwarf::DW_OP_div:
     789             :     case dwarf::DW_OP_mod:
     790             :     case dwarf::DW_OP_or:
     791             :     case dwarf::DW_OP_and:
     792             :     case dwarf::DW_OP_xor:
     793             :     case dwarf::DW_OP_shl:
     794             :     case dwarf::DW_OP_shr:
     795             :     case dwarf::DW_OP_shra:
     796             :     case dwarf::DW_OP_deref:
     797             :     case dwarf::DW_OP_xderef:
     798             :     case dwarf::DW_OP_lit0:
     799             :     case dwarf::DW_OP_not:
     800             :     case dwarf::DW_OP_dup:
     801             :       break;
     802             :     }
     803             :   }
     804       34090 :   return true;
     805             : }
     806             : 
     807             : Optional<DIExpression::FragmentInfo>
     808      551078 : DIExpression::getFragmentInfo(expr_op_iterator Start, expr_op_iterator End) {
     809      757974 :   for (auto I = Start; I != End; ++I)
     810      219721 :     if (I->getOp() == dwarf::DW_OP_LLVM_fragment) {
     811             :       DIExpression::FragmentInfo Info = {I->getArg(1), I->getArg(0)};
     812             :       return Info;
     813             :     }
     814             :   return None;
     815             : }
     816             : 
     817       24976 : void DIExpression::appendOffset(SmallVectorImpl<uint64_t> &Ops,
     818             :                                 int64_t Offset) {
     819       24976 :   if (Offset > 0) {
     820       15140 :     Ops.push_back(dwarf::DW_OP_plus_uconst);
     821       15140 :     Ops.push_back(Offset);
     822        9836 :   } else if (Offset < 0) {
     823        1052 :     Ops.push_back(dwarf::DW_OP_constu);
     824        1052 :     Ops.push_back(-Offset);
     825        1052 :     Ops.push_back(dwarf::DW_OP_minus);
     826             :   }
     827       24976 : }
     828             : 
     829         222 : bool DIExpression::extractIfOffset(int64_t &Offset) const {
     830         222 :   if (getNumElements() == 0) {
     831         216 :     Offset = 0;
     832         216 :     return true;
     833             :   }
     834             : 
     835           6 :   if (getNumElements() == 2 && Elements[0] == dwarf::DW_OP_plus_uconst) {
     836           6 :     Offset = Elements[1];
     837           6 :     return true;
     838             :   }
     839             : 
     840           0 :   if (getNumElements() == 3 && Elements[0] == dwarf::DW_OP_constu) {
     841           0 :     if (Elements[2] == dwarf::DW_OP_plus) {
     842           0 :       Offset = Elements[1];
     843           0 :       return true;
     844             :     }
     845           0 :     if (Elements[2] == dwarf::DW_OP_minus) {
     846           0 :       Offset = -Elements[1];
     847           0 :       return true;
     848             :     }
     849             :   }
     850             : 
     851             :   return false;
     852             : }
     853             : 
     854       14843 : DIExpression *DIExpression::prepend(const DIExpression *Expr, bool DerefBefore,
     855             :                                     int64_t Offset, bool DerefAfter,
     856             :                                     bool StackValue) {
     857             :   SmallVector<uint64_t, 8> Ops;
     858       14843 :   if (DerefBefore)
     859         880 :     Ops.push_back(dwarf::DW_OP_deref);
     860             : 
     861       14843 :   appendOffset(Ops, Offset);
     862       14843 :   if (DerefAfter)
     863           9 :     Ops.push_back(dwarf::DW_OP_deref);
     864             : 
     865       14843 :   return prependOpcodes(Expr, Ops, StackValue);
     866             : }
     867             : 
     868       19879 : DIExpression *DIExpression::prependOpcodes(const DIExpression *Expr,
     869             :                                            SmallVectorImpl<uint64_t> &Ops,
     870             :                                            bool StackValue) {
     871             :   assert(Expr && "Can't prepend ops to this expression");
     872             : 
     873             :   // If there are no ops to prepend, do not even add the DW_OP_stack_value.
     874       19879 :   if (Ops.empty())
     875             :     StackValue = false;
     876       24488 :   for (auto Op : Expr->expr_ops()) {
     877             :     // A DW_OP_stack_value comes at the end, but before a DW_OP_LLVM_fragment.
     878        4609 :     if (StackValue) {
     879        2515 :       if (Op.getOp() == dwarf::DW_OP_stack_value)
     880             :         StackValue = false;
     881        1983 :       else if (Op.getOp() == dwarf::DW_OP_LLVM_fragment) {
     882           5 :         Ops.push_back(dwarf::DW_OP_stack_value);
     883             :         StackValue = false;
     884             :       }
     885             :     }
     886        4609 :     Op.appendToVector(Ops);
     887             :   }
     888       19879 :   if (StackValue)
     889       10207 :     Ops.push_back(dwarf::DW_OP_stack_value);
     890       19879 :   return DIExpression::get(Expr->getContext(), Ops);
     891             : }
     892             : 
     893        5368 : DIExpression *DIExpression::append(const DIExpression *Expr,
     894             :                                    ArrayRef<uint64_t> Ops) {
     895             :   assert(Expr && !Ops.empty() && "Can't append ops to this expression");
     896             : 
     897             :   // Copy Expr's current op list.
     898             :   SmallVector<uint64_t, 16> NewOps;
     899        5388 :   for (auto Op : Expr->expr_ops()) {
     900             :     // Append new opcodes before DW_OP_{stack_value, LLVM_fragment}.
     901          20 :     if (Op.getOp() == dwarf::DW_OP_stack_value ||
     902             :         Op.getOp() == dwarf::DW_OP_LLVM_fragment) {
     903           6 :       NewOps.append(Ops.begin(), Ops.end());
     904             : 
     905             :       // Ensure that the new opcodes are only appended once.
     906             :       Ops = None;
     907             :     }
     908          20 :     Op.appendToVector(NewOps);
     909             :   }
     910             : 
     911        5368 :   NewOps.append(Ops.begin(), Ops.end());
     912        5368 :   return DIExpression::get(Expr->getContext(), NewOps);
     913             : }
     914             : 
     915           7 : DIExpression *DIExpression::appendToStack(const DIExpression *Expr,
     916             :                                           ArrayRef<uint64_t> Ops) {
     917             :   assert(Expr && !Ops.empty() && "Can't append ops to this expression");
     918             :   assert(none_of(Ops,
     919             :                  [](uint64_t Op) {
     920             :                    return Op == dwarf::DW_OP_stack_value ||
     921             :                           Op == dwarf::DW_OP_LLVM_fragment;
     922             :                  }) &&
     923             :          "Can't append this op");
     924             : 
     925             :   // Append a DW_OP_deref after Expr's current op list if it's non-empty and
     926             :   // has no DW_OP_stack_value.
     927             :   //
     928             :   // Match .* DW_OP_stack_value (DW_OP_LLVM_fragment A B)?.
     929             :   Optional<FragmentInfo> FI = Expr->getFragmentInfo();
     930           7 :   unsigned DropUntilStackValue = FI.hasValue() ? 3 : 0;
     931             :   ArrayRef<uint64_t> ExprOpsBeforeFragment =
     932           7 :       Expr->getElements().drop_back(DropUntilStackValue);
     933           7 :   bool NeedsDeref = (Expr->getNumElements() > DropUntilStackValue) &&
     934           4 :                     (ExprOpsBeforeFragment.back() != dwarf::DW_OP_stack_value);
     935           5 :   bool NeedsStackValue = NeedsDeref || ExprOpsBeforeFragment.empty();
     936             : 
     937             :   // Append a DW_OP_deref after Expr's current op list if needed, then append
     938             :   // the new ops, and finally ensure that a single DW_OP_stack_value is present.
     939             :   SmallVector<uint64_t, 16> NewOps;
     940           7 :   if (NeedsDeref)
     941           2 :     NewOps.push_back(dwarf::DW_OP_deref);
     942          14 :   NewOps.append(Ops.begin(), Ops.end());
     943           7 :   if (NeedsStackValue)
     944           5 :     NewOps.push_back(dwarf::DW_OP_stack_value);
     945           7 :   return DIExpression::append(Expr, NewOps);
     946             : }
     947             : 
     948         414 : Optional<DIExpression *> DIExpression::createFragmentExpression(
     949             :     const DIExpression *Expr, unsigned OffsetInBits, unsigned SizeInBits) {
     950             :   SmallVector<uint64_t, 8> Ops;
     951             :   // Copy over the expression, but leave off any trailing DW_OP_LLVM_fragment.
     952         414 :   if (Expr) {
     953         441 :     for (auto Op : Expr->expr_ops()) {
     954          62 :       switch (Op.getOp()) {
     955             :       default: break;
     956             :       case dwarf::DW_OP_plus:
     957             :       case dwarf::DW_OP_minus:
     958             :         // We can't safely split arithmetic into multiple fragments because we
     959             :         // can't express carry-over between fragments.
     960             :         //
     961             :         // FIXME: We *could* preserve the lowest fragment of a constant offset
     962             :         // operation if the offset fits into SizeInBits.
     963           4 :         return None;
     964          23 :       case dwarf::DW_OP_LLVM_fragment: {
     965             :         // Make the new offset point into the existing fragment.
     966             :         uint64_t FragmentOffsetInBits = Op.getArg(0);
     967             :         uint64_t FragmentSizeInBits = Op.getArg(1);
     968             :         (void)FragmentSizeInBits;
     969             :         assert((OffsetInBits + SizeInBits <= FragmentSizeInBits) &&
     970             :                "new fragment outside of original fragment");
     971          23 :         OffsetInBits += FragmentOffsetInBits;
     972          23 :         continue;
     973             :       }
     974             :       }
     975           4 :       Op.appendToVector(Ops);
     976             :     }
     977             :   }
     978         410 :   Ops.push_back(dwarf::DW_OP_LLVM_fragment);
     979         410 :   Ops.push_back(OffsetInBits);
     980         410 :   Ops.push_back(SizeInBits);
     981         410 :   return DIExpression::get(Expr->getContext(), Ops);
     982             : }
     983             : 
     984        2171 : bool DIExpression::isConstant() const {
     985             :   // Recognize DW_OP_constu C DW_OP_stack_value (DW_OP_LLVM_fragment Len Ofs)?.
     986        2171 :   if (getNumElements() != 3 && getNumElements() != 6)
     987             :     return false;
     988          31 :   if (getElement(0) != dwarf::DW_OP_constu ||
     989             :       getElement(2) != dwarf::DW_OP_stack_value)
     990             :     return false;
     991          28 :   if (getNumElements() == 6 && getElement(3) != dwarf::DW_OP_LLVM_fragment)
     992           0 :     return false;
     993             :   return true;
     994             : }
     995             : 
     996             : DIGlobalVariableExpression *
     997        1945 : DIGlobalVariableExpression::getImpl(LLVMContext &Context, Metadata *Variable,
     998             :                                     Metadata *Expression, StorageType Storage,
     999             :                                     bool ShouldCreate) {
    1000        3792 :   DEFINE_GETIMPL_LOOKUP(DIGlobalVariableExpression, (Variable, Expression));
    1001        1942 :   Metadata *Ops[] = {Variable, Expression};
    1002        3884 :   DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DIGlobalVariableExpression, Ops);
    1003             : }
    1004             : 
    1005         123 : DIObjCProperty *DIObjCProperty::getImpl(
    1006             :     LLVMContext &Context, MDString *Name, Metadata *File, unsigned Line,
    1007             :     MDString *GetterName, MDString *SetterName, unsigned Attributes,
    1008             :     Metadata *Type, StorageType Storage, bool ShouldCreate) {
    1009             :   assert(isCanonical(Name) && "Expected canonical MDString");
    1010             :   assert(isCanonical(GetterName) && "Expected canonical MDString");
    1011             :   assert(isCanonical(SetterName) && "Expected canonical MDString");
    1012         245 :   DEFINE_GETIMPL_LOOKUP(DIObjCProperty, (Name, File, Line, GetterName,
    1013             :                                          SetterName, Attributes, Type));
    1014          91 :   Metadata *Ops[] = {Name, File, GetterName, SetterName, Type};
    1015         182 :   DEFINE_GETIMPL_STORE(DIObjCProperty, (Line, Attributes), Ops);
    1016             : }
    1017             : 
    1018       16764 : DIImportedEntity *DIImportedEntity::getImpl(LLVMContext &Context, unsigned Tag,
    1019             :                                             Metadata *Scope, Metadata *Entity,
    1020             :                                             Metadata *File, unsigned Line,
    1021             :                                             MDString *Name, StorageType Storage,
    1022             :                                             bool ShouldCreate) {
    1023             :   assert(isCanonical(Name) && "Expected canonical MDString");
    1024       33527 :   DEFINE_GETIMPL_LOOKUP(DIImportedEntity,
    1025             :                         (Tag, Scope, Entity, File, Line, Name));
    1026       16758 :   Metadata *Ops[] = {Scope, Entity, Name, File};
    1027       33516 :   DEFINE_GETIMPL_STORE(DIImportedEntity, (Tag, Line), Ops);
    1028             : }
    1029             : 
    1030        1462 : DIMacro *DIMacro::getImpl(LLVMContext &Context, unsigned MIType,
    1031             :                           unsigned Line, MDString *Name, MDString *Value,
    1032             :                           StorageType Storage, bool ShouldCreate) {
    1033             :   assert(isCanonical(Name) && "Expected canonical MDString");
    1034        2924 :   DEFINE_GETIMPL_LOOKUP(DIMacro, (MIType, Line, Name, Value));
    1035        1451 :   Metadata *Ops[] = { Name, Value };
    1036        2902 :   DEFINE_GETIMPL_STORE(DIMacro, (MIType, Line), Ops);
    1037             : }
    1038             : 
    1039          92 : DIMacroFile *DIMacroFile::getImpl(LLVMContext &Context, unsigned MIType,
    1040             :                                   unsigned Line, Metadata *File,
    1041             :                                   Metadata *Elements, StorageType Storage,
    1042             :                                   bool ShouldCreate) {
    1043         162 :   DEFINE_GETIMPL_LOOKUP(DIMacroFile,
    1044             :                         (MIType, Line, File, Elements));
    1045          90 :   Metadata *Ops[] = { File, Elements };
    1046         180 :   DEFINE_GETIMPL_STORE(DIMacroFile, (MIType, Line), Ops);
    1047             : }

Generated by: LCOV version 1.13