LCOV - code coverage report
Current view: top level - lib/IR - DIBuilder.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 311 352 88.4 %
Date: 2018-06-17 00:07:59 Functions: 77 86 89.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===--- DIBuilder.cpp - Debug Information Builder ------------------------===//
       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 DIBuilder.
      11             : //
      12             : //===----------------------------------------------------------------------===//
      13             : 
      14             : #include "llvm/IR/DIBuilder.h"
      15             : #include "llvm/IR/IRBuilder.h"
      16             : #include "LLVMContextImpl.h"
      17             : #include "llvm/ADT/Optional.h"
      18             : #include "llvm/ADT/STLExtras.h"
      19             : #include "llvm/BinaryFormat/Dwarf.h"
      20             : #include "llvm/IR/Constants.h"
      21             : #include "llvm/IR/DebugInfo.h"
      22             : #include "llvm/IR/IntrinsicInst.h"
      23             : #include "llvm/IR/Module.h"
      24             : #include "llvm/Support/Debug.h"
      25             : 
      26             : using namespace llvm;
      27             : using namespace llvm::dwarf;
      28             : 
      29             : cl::opt<bool>
      30      101193 :     UseDbgAddr("use-dbg-addr",
      31      101193 :                llvm::cl::desc("Use llvm.dbg.addr for all local variables"),
      32      303579 :                cl::init(false), cl::Hidden);
      33             : 
      34      654820 : DIBuilder::DIBuilder(Module &m, bool AllowUnresolvedNodes, DICompileUnit *CU)
      35      654820 :   : M(m), VMContext(M.getContext()), CUNode(CU),
      36             :       DeclareFn(nullptr), ValueFn(nullptr), LabelFn(nullptr),
      37     3274100 :       AllowUnresolvedNodes(AllowUnresolvedNodes) {}
      38             : 
      39      592597 : void DIBuilder::trackIfUnresolved(MDNode *N) {
      40      592597 :   if (!N)
      41             :     return;
      42             :   if (N->isResolved())
      43             :     return;
      44             : 
      45             :   assert(AllowUnresolvedNodes && "Cannot handle unresolved nodes");
      46       81967 :   UnresolvedNodes.emplace_back(N);
      47             : }
      48             : 
      49      140342 : void DIBuilder::finalizeSubprogram(DISubprogram *SP) {
      50             :   MDTuple *Temp = SP->getRetainedNodes().get();
      51      140342 :   if (!Temp || !Temp->isTemporary())
      52       70142 :     return;
      53             : 
      54             :   SmallVector<Metadata *, 16> RetainedNodes;
      55             : 
      56       70200 :   auto PV = PreservedVariables.find(SP);
      57       70200 :   if (PV != PreservedVariables.end())
      58        5694 :     RetainedNodes.append(PV->second.begin(), PV->second.end());
      59             : 
      60       70200 :   auto PL = PreservedLabels.find(SP);
      61       70200 :   if (PL != PreservedLabels.end())
      62           0 :     RetainedNodes.append(PL->second.begin(), PL->second.end());
      63             : 
      64       70200 :   DINodeArray Node = getOrCreateArray(RetainedNodes);
      65             : 
      66             :   TempMDTuple(Temp)->replaceAllUsesWith(Node.get());
      67             : }
      68             : 
      69        3466 : void DIBuilder::finalize() {
      70        3466 :   if (!CUNode) {
      71             :     assert(!AllowUnresolvedNodes &&
      72             :            "creating type nodes without a CU is not supported");
      73           0 :     return;
      74             :   }
      75             : 
      76        3466 :   CUNode->replaceEnumTypes(MDTuple::get(VMContext, AllEnumTypes));
      77             : 
      78             :   SmallVector<Metadata *, 16> RetainValues;
      79             :   // Declarations and definitions of the same type may be retained. Some
      80             :   // clients RAUW these pairs, leaving duplicates in the retained types
      81             :   // list. Use a set to remove the duplicates while we transform the
      82             :   // TrackingVHs back into Values.
      83             :   SmallPtrSet<Metadata *, 16> RetainSet;
      84       13611 :   for (unsigned I = 0, E = AllRetainTypes.size(); I < E; I++)
      85       20290 :     if (RetainSet.insert(AllRetainTypes[I]).second)
      86        5562 :       RetainValues.push_back(AllRetainTypes[I]);
      87             : 
      88        3466 :   if (!RetainValues.empty())
      89        1330 :     CUNode->replaceRetainedTypes(MDTuple::get(VMContext, RetainValues));
      90             : 
      91        3466 :   DISubprogramArray SPs = MDTuple::get(VMContext, AllSubprograms);
      92       73634 :   for (auto *SP : SPs)
      93       70168 :     finalizeSubprogram(SP);
      94       14590 :   for (auto *N : RetainValues)
      95             :     if (auto *SP = dyn_cast<DISubprogram>(N))
      96          32 :       finalizeSubprogram(SP);
      97             : 
      98        3466 :   if (!AllGVs.empty())
      99        1948 :     CUNode->replaceGlobalVariables(MDTuple::get(VMContext, AllGVs));
     100             : 
     101        3466 :   if (!AllImportedModules.empty())
     102        1386 :     CUNode->replaceImportedEntities(MDTuple::get(
     103         693 :         VMContext, SmallVector<Metadata *, 16>(AllImportedModules.begin(),
     104             :                                                AllImportedModules.end())));
     105             : 
     106        3489 :   for (const auto &I : AllMacrosPerParent) {
     107             :     // DIMacroNode's with nullptr parent are DICompileUnit direct children.
     108          28 :     if (!I.first) {
     109          10 :       CUNode->replaceMacros(MDTuple::get(VMContext, I.second.getArrayRef()));
     110           5 :       continue;
     111             :     }
     112             :     // Otherwise, it must be a temporary DIMacroFile that need to be resolved.
     113             :     auto *TMF = cast<DIMacroFile>(I.first);
     114          36 :     auto *MF = DIMacroFile::get(VMContext, dwarf::DW_MACINFO_start_file,
     115             :                                 TMF->getLine(), TMF->getFile(),
     116             :                                 getOrCreateMacroArray(I.second.getArrayRef()));
     117          36 :     replaceTemporary(llvm::TempDIMacroNode(TMF), MF);
     118             :   }
     119             : 
     120             :   // Now that all temp nodes have been replaced or deleted, resolve remaining
     121             :   // cycles.
     122      167400 :   for (const auto &N : UnresolvedNodes)
     123       81967 :     if (N && !N->isResolved())
     124           0 :       N->resolveCycles();
     125        3466 :   UnresolvedNodes.clear();
     126             : 
     127             :   // Can't handle unresolved nodes anymore.
     128        3466 :   AllowUnresolvedNodes = false;
     129             : }
     130             : 
     131             : /// If N is compile unit return NULL otherwise return N.
     132             : static DIScope *getNonCompileUnitScope(DIScope *N) {
     133      922571 :   if (!N || isa<DICompileUnit>(N))
     134             :     return nullptr;
     135             :   return cast<DIScope>(N);
     136             : }
     137             : 
     138        3464 : DICompileUnit *DIBuilder::createCompileUnit(
     139             :     unsigned Lang, DIFile *File, StringRef Producer, bool isOptimized,
     140             :     StringRef Flags, unsigned RunTimeVer, StringRef SplitName,
     141             :     DICompileUnit::DebugEmissionKind Kind, uint64_t DWOId,
     142             :     bool SplitDebugInlining, bool DebugInfoForProfiling, bool GnuPubnames) {
     143             : 
     144             :   assert(((Lang <= dwarf::DW_LANG_Fortran08 && Lang >= dwarf::DW_LANG_C89) ||
     145             :           (Lang <= dwarf::DW_LANG_hi_user && Lang >= dwarf::DW_LANG_lo_user)) &&
     146             :          "Invalid Language tag");
     147             : 
     148             :   assert(!CUNode && "Can only make one compile unit per DIBuilder instance");
     149        6928 :   CUNode = DICompileUnit::getDistinct(
     150             :       VMContext, Lang, File, Producer, isOptimized, Flags, RunTimeVer,
     151             :       SplitName, Kind, nullptr, nullptr, nullptr, nullptr, nullptr, DWOId,
     152             :       SplitDebugInlining, DebugInfoForProfiling, GnuPubnames);
     153             : 
     154             :   // Create a named metadata so that it is easier to find cu in a module.
     155        6928 :   NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu");
     156        3464 :   NMD->addOperand(CUNode);
     157        3464 :   trackIfUnresolved(CUNode);
     158        3464 :   return CUNode;
     159             : }
     160             : 
     161             : static DIImportedEntity *
     162      105292 : createImportedModule(LLVMContext &C, dwarf::Tag Tag, DIScope *Context,
     163             :                      Metadata *NS, DIFile *File, unsigned Line, StringRef Name,
     164             :                      SmallVectorImpl<TrackingMDNodeRef> &AllImportedModules) {
     165             :   if (Line)
     166             :     assert(File && "Source location has line number but no file");
     167      105292 :   unsigned EntitiesCount = C.pImpl->DIImportedEntitys.size();
     168             :   auto *M =
     169      210584 :       DIImportedEntity::get(C, Tag, Context, DINodeRef(NS), File, Line, Name);
     170      210584 :   if (EntitiesCount < C.pImpl->DIImportedEntitys.size())
     171             :     // A new Imported Entity was just added to the context.
     172             :     // Add it to the Imported Modules list.
     173      105289 :     AllImportedModules.emplace_back(M);
     174      105292 :   return M;
     175             : }
     176             : 
     177         702 : DIImportedEntity *DIBuilder::createImportedModule(DIScope *Context,
     178             :                                                   DINamespace *NS, DIFile *File,
     179             :                                                   unsigned Line) {
     180        2106 :   return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
     181             :                                 Context, NS, File, Line, StringRef(),
     182        2106 :                                 AllImportedModules);
     183             : }
     184             : 
     185           3 : DIImportedEntity *DIBuilder::createImportedModule(DIScope *Context,
     186             :                                                   DIImportedEntity *NS,
     187             :                                                   DIFile *File, unsigned Line) {
     188           9 :   return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
     189             :                                 Context, NS, File, Line, StringRef(),
     190           9 :                                 AllImportedModules);
     191             : }
     192             : 
     193           1 : DIImportedEntity *DIBuilder::createImportedModule(DIScope *Context, DIModule *M,
     194             :                                                   DIFile *File, unsigned Line) {
     195           3 :   return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
     196             :                                 Context, M, File, Line, StringRef(),
     197           3 :                                 AllImportedModules);
     198             : }
     199             : 
     200      104586 : DIImportedEntity *DIBuilder::createImportedDeclaration(DIScope *Context,
     201             :                                                        DINode *Decl,
     202             :                                                        DIFile *File,
     203             :                                                        unsigned Line,
     204             :                                                        StringRef Name) {
     205             :   // Make sure to use the unique identifier based metadata reference for
     206             :   // types that have one.
     207      104586 :   return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_declaration,
     208             :                                 Context, Decl, File, Line, Name,
     209      104586 :                                 AllImportedModules);
     210             : }
     211             : 
     212       56921 : DIFile *DIBuilder::createFile(StringRef Filename, StringRef Directory,
     213             :                               Optional<DIFile::ChecksumInfo<StringRef>> CS,
     214             :                               Optional<StringRef> Source) {
     215       56921 :   return DIFile::get(VMContext, Filename, Directory, CS, Source);
     216             : }
     217             : 
     218        1072 : DIMacro *DIBuilder::createMacro(DIMacroFile *Parent, unsigned LineNumber,
     219             :                                 unsigned MacroType, StringRef Name,
     220             :                                 StringRef Value) {
     221             :   assert(!Name.empty() && "Unable to create macro without name");
     222             :   assert((MacroType == dwarf::DW_MACINFO_undef ||
     223             :           MacroType == dwarf::DW_MACINFO_define) &&
     224             :          "Unexpected macro type");
     225        1072 :   auto *M = DIMacro::get(VMContext, MacroType, LineNumber, Name, Value);
     226        1072 :   AllMacrosPerParent[Parent].insert(M);
     227        1072 :   return M;
     228             : }
     229             : 
     230          18 : DIMacroFile *DIBuilder::createTempMacroFile(DIMacroFile *Parent,
     231             :                                             unsigned LineNumber, DIFile *File) {
     232          18 :   auto *MF = DIMacroFile::getTemporary(VMContext, dwarf::DW_MACINFO_start_file,
     233             :                                        LineNumber, File, DIMacroNodeArray())
     234             :                  .release();
     235          18 :   AllMacrosPerParent[Parent].insert(MF);
     236             :   // Add the new temporary DIMacroFile to the macro per parent map as a parent.
     237             :   // This is needed to assure DIMacroFile with no children to have an entry in
     238             :   // the map. Otherwise, it will not be resolved in DIBuilder::finalize().
     239          54 :   AllMacrosPerParent.insert({MF, {}});
     240          18 :   return MF;
     241             : }
     242             : 
     243       13514 : DIEnumerator *DIBuilder::createEnumerator(StringRef Name, int64_t Val,
     244             :                                           bool IsUnsigned) {
     245             :   assert(!Name.empty() && "Unable to create enumerator without name");
     246       27028 :   return DIEnumerator::get(VMContext, Val, IsUnsigned, Name);
     247             : }
     248             : 
     249         449 : DIBasicType *DIBuilder::createUnspecifiedType(StringRef Name) {
     250             :   assert(!Name.empty() && "Unable to create type without name");
     251         898 :   return DIBasicType::get(VMContext, dwarf::DW_TAG_unspecified_type, Name);
     252             : }
     253             : 
     254         449 : DIBasicType *DIBuilder::createNullPtrType() {
     255         449 :   return createUnspecifiedType("decltype(nullptr)");
     256             : }
     257             : 
     258       11932 : DIBasicType *DIBuilder::createBasicType(StringRef Name, uint64_t SizeInBits,
     259             :                                         unsigned Encoding) {
     260             :   assert(!Name.empty() && "Unable to create type without name");
     261       11932 :   return DIBasicType::get(VMContext, dwarf::DW_TAG_base_type, Name, SizeInBits,
     262       11932 :                           0, Encoding);
     263             : }
     264             : 
     265       35689 : DIDerivedType *DIBuilder::createQualifiedType(unsigned Tag, DIType *FromTy) {
     266       35689 :   return DIDerivedType::get(VMContext, Tag, "", nullptr, 0, nullptr, FromTy, 0,
     267       35689 :                             0, 0, None, DINode::FlagZero);
     268             : }
     269             : 
     270      190413 : DIDerivedType *DIBuilder::createPointerType(
     271             :     DIType *PointeeTy,
     272             :     uint64_t SizeInBits,
     273             :     uint32_t AlignInBits,
     274             :     Optional<unsigned> DWARFAddressSpace,
     275             :     StringRef Name) {
     276             :   // FIXME: Why is there a name here?
     277      190413 :   return DIDerivedType::get(VMContext, dwarf::DW_TAG_pointer_type, Name,
     278             :                             nullptr, 0, nullptr, PointeeTy, SizeInBits,
     279             :                             AlignInBits, 0, DWARFAddressSpace,
     280      190413 :                             DINode::FlagZero);
     281             : }
     282             : 
     283          53 : DIDerivedType *DIBuilder::createMemberPointerType(DIType *PointeeTy,
     284             :                                                   DIType *Base,
     285             :                                                   uint64_t SizeInBits,
     286             :                                                   uint32_t AlignInBits,
     287             :                                                   DINode::DIFlags Flags) {
     288          53 :   return DIDerivedType::get(VMContext, dwarf::DW_TAG_ptr_to_member_type, "",
     289             :                             nullptr, 0, nullptr, PointeeTy, SizeInBits,
     290          53 :                             AlignInBits, 0, None, Flags, Base);
     291             : }
     292             : 
     293       71620 : DIDerivedType *DIBuilder::createReferenceType(
     294             :     unsigned Tag, DIType *RTy,
     295             :     uint64_t SizeInBits,
     296             :     uint32_t AlignInBits,
     297             :     Optional<unsigned> DWARFAddressSpace) {
     298             :   assert(RTy && "Unable to create reference type");
     299       71620 :   return DIDerivedType::get(VMContext, Tag, "", nullptr, 0, nullptr, RTy,
     300             :                             SizeInBits, AlignInBits, 0, DWARFAddressSpace,
     301       71620 :                             DINode::FlagZero);
     302             : }
     303             : 
     304       63143 : DIDerivedType *DIBuilder::createTypedef(DIType *Ty, StringRef Name,
     305             :                                         DIFile *File, unsigned LineNo,
     306             :                                         DIScope *Context) {
     307       63143 :   return DIDerivedType::get(VMContext, dwarf::DW_TAG_typedef, Name, File,
     308             :                             LineNo, getNonCompileUnitScope(Context), Ty, 0, 0,
     309       63143 :                             0, None, DINode::FlagZero);
     310             : }
     311             : 
     312           0 : DIDerivedType *DIBuilder::createFriend(DIType *Ty, DIType *FriendTy) {
     313             :   assert(Ty && "Invalid type!");
     314             :   assert(FriendTy && "Invalid friend type!");
     315           0 :   return DIDerivedType::get(VMContext, dwarf::DW_TAG_friend, "", nullptr, 0, Ty,
     316           0 :                             FriendTy, 0, 0, 0, None, DINode::FlagZero);
     317             : }
     318             : 
     319        8139 : DIDerivedType *DIBuilder::createInheritance(DIType *Ty, DIType *BaseTy,
     320             :                                             uint64_t BaseOffset,
     321             :                                             uint32_t VBPtrOffset,
     322             :                                             DINode::DIFlags Flags) {
     323             :   assert(Ty && "Unable to create inheritance");
     324             :   Metadata *ExtraData = ConstantAsMetadata::get(
     325        8139 :       ConstantInt::get(IntegerType::get(VMContext, 32), VBPtrOffset));
     326        8139 :   return DIDerivedType::get(VMContext, dwarf::DW_TAG_inheritance, "", nullptr,
     327             :                             0, Ty, BaseTy, 0, 0, BaseOffset, None,
     328        8139 :                             Flags, ExtraData);
     329             : }
     330             : 
     331       57368 : DIDerivedType *DIBuilder::createMemberType(DIScope *Scope, StringRef Name,
     332             :                                            DIFile *File, unsigned LineNumber,
     333             :                                            uint64_t SizeInBits,
     334             :                                            uint32_t AlignInBits,
     335             :                                            uint64_t OffsetInBits,
     336             :                                            DINode::DIFlags Flags, DIType *Ty) {
     337       57368 :   return DIDerivedType::get(VMContext, dwarf::DW_TAG_member, Name, File,
     338             :                             LineNumber, getNonCompileUnitScope(Scope), Ty,
     339       57368 :                             SizeInBits, AlignInBits, OffsetInBits, None, Flags);
     340             : }
     341             : 
     342             : static ConstantAsMetadata *getConstantOrNull(Constant *C) {
     343       22906 :   if (C)
     344             :     return ConstantAsMetadata::get(C);
     345             :   return nullptr;
     346             : }
     347             : 
     348           0 : DIDerivedType *DIBuilder::createVariantMemberType(DIScope *Scope, StringRef Name,
     349             :                                                   DIFile *File, unsigned LineNumber,
     350             :                                                   uint64_t SizeInBits,
     351             :                                                   uint32_t AlignInBits,
     352             :                                                   uint64_t OffsetInBits,
     353             :                                                   Constant *Discriminant,
     354             :                                                   DINode::DIFlags Flags, DIType *Ty) {
     355           0 :   return DIDerivedType::get(VMContext, dwarf::DW_TAG_member, Name, File,
     356             :                             LineNumber, getNonCompileUnitScope(Scope), Ty,
     357             :                             SizeInBits, AlignInBits, OffsetInBits, None, Flags,
     358           0 :                             getConstantOrNull(Discriminant));
     359             : }
     360             : 
     361          92 : DIDerivedType *DIBuilder::createBitFieldMemberType(
     362             :     DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber,
     363             :     uint64_t SizeInBits, uint64_t OffsetInBits, uint64_t StorageOffsetInBits,
     364             :     DINode::DIFlags Flags, DIType *Ty) {
     365             :   Flags |= DINode::FlagBitField;
     366          92 :   return DIDerivedType::get(
     367             :       VMContext, dwarf::DW_TAG_member, Name, File, LineNumber,
     368             :       getNonCompileUnitScope(Scope), Ty, SizeInBits, /* AlignInBits */ 0,
     369             :       OffsetInBits, None, Flags,
     370          92 :       ConstantAsMetadata::get(ConstantInt::get(IntegerType::get(VMContext, 64),
     371          92 :                                                StorageOffsetInBits)));
     372             : }
     373             : 
     374             : DIDerivedType *
     375        5866 : DIBuilder::createStaticMemberType(DIScope *Scope, StringRef Name, DIFile *File,
     376             :                                   unsigned LineNumber, DIType *Ty,
     377             :                                   DINode::DIFlags Flags, llvm::Constant *Val,
     378             :                                   uint32_t AlignInBits) {
     379             :   Flags |= DINode::FlagStaticMember;
     380        5866 :   return DIDerivedType::get(VMContext, dwarf::DW_TAG_member, Name, File,
     381             :                             LineNumber, getNonCompileUnitScope(Scope), Ty, 0,
     382             :                             AlignInBits, 0, None, Flags,
     383        5866 :                             getConstantOrNull(Val));
     384             : }
     385             : 
     386             : DIDerivedType *
     387          87 : DIBuilder::createObjCIVar(StringRef Name, DIFile *File, unsigned LineNumber,
     388             :                           uint64_t SizeInBits, uint32_t AlignInBits,
     389             :                           uint64_t OffsetInBits, DINode::DIFlags Flags,
     390             :                           DIType *Ty, MDNode *PropertyNode) {
     391          87 :   return DIDerivedType::get(VMContext, dwarf::DW_TAG_member, Name, File,
     392             :                             LineNumber, getNonCompileUnitScope(File), Ty,
     393             :                             SizeInBits, AlignInBits, OffsetInBits, None, Flags,
     394          87 :                             PropertyNode);
     395             : }
     396             : 
     397             : DIObjCProperty *
     398          74 : DIBuilder::createObjCProperty(StringRef Name, DIFile *File, unsigned LineNumber,
     399             :                               StringRef GetterName, StringRef SetterName,
     400             :                               unsigned PropertyAttributes, DIType *Ty) {
     401          74 :   return DIObjCProperty::get(VMContext, Name, File, LineNumber, GetterName,
     402          74 :                              SetterName, PropertyAttributes, Ty);
     403             : }
     404             : 
     405             : DITemplateTypeParameter *
     406       64212 : DIBuilder::createTemplateTypeParameter(DIScope *Context, StringRef Name,
     407             :                                        DIType *Ty) {
     408             :   assert((!Context || isa<DICompileUnit>(Context)) && "Expected compile unit");
     409      128424 :   return DITemplateTypeParameter::get(VMContext, Name, Ty);
     410             : }
     411             : 
     412             : static DITemplateValueParameter *
     413             : createTemplateValueParameterHelper(LLVMContext &VMContext, unsigned Tag,
     414             :                                    DIScope *Context, StringRef Name, DIType *Ty,
     415             :                                    Metadata *MD) {
     416             :   assert((!Context || isa<DICompileUnit>(Context)) && "Expected compile unit");
     417             :   return DITemplateValueParameter::get(VMContext, Tag, Name, Ty, MD);
     418             : }
     419             : 
     420             : DITemplateValueParameter *
     421       17040 : DIBuilder::createTemplateValueParameter(DIScope *Context, StringRef Name,
     422             :                                         DIType *Ty, Constant *Val) {
     423       17040 :   return createTemplateValueParameterHelper(
     424             :       VMContext, dwarf::DW_TAG_template_value_parameter, Context, Name, Ty,
     425       17040 :       getConstantOrNull(Val));
     426             : }
     427             : 
     428             : DITemplateValueParameter *
     429         177 : DIBuilder::createTemplateTemplateParameter(DIScope *Context, StringRef Name,
     430             :                                            DIType *Ty, StringRef Val) {
     431         177 :   return createTemplateValueParameterHelper(
     432             :       VMContext, dwarf::DW_TAG_GNU_template_template_param, Context, Name, Ty,
     433         354 :       MDString::get(VMContext, Val));
     434             : }
     435             : 
     436             : DITemplateValueParameter *
     437        8371 : DIBuilder::createTemplateParameterPack(DIScope *Context, StringRef Name,
     438             :                                        DIType *Ty, DINodeArray Val) {
     439        8371 :   return createTemplateValueParameterHelper(
     440             :       VMContext, dwarf::DW_TAG_GNU_template_parameter_pack, Context, Name, Ty,
     441        8371 :       Val.get());
     442             : }
     443             : 
     444           0 : DICompositeType *DIBuilder::createClassType(
     445             :     DIScope *Context, StringRef Name, DIFile *File, unsigned LineNumber,
     446             :     uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits,
     447             :     DINode::DIFlags Flags, DIType *DerivedFrom, DINodeArray Elements,
     448             :     DIType *VTableHolder, MDNode *TemplateParams, StringRef UniqueIdentifier) {
     449             :   assert((!Context || isa<DIScope>(Context)) &&
     450             :          "createClassType should be called with a valid Context");
     451             : 
     452           0 :   auto *R = DICompositeType::get(
     453             :       VMContext, dwarf::DW_TAG_structure_type, Name, File, LineNumber,
     454             :       getNonCompileUnitScope(Context), DerivedFrom, SizeInBits, AlignInBits,
     455             :       OffsetInBits, Flags, Elements, 0, VTableHolder,
     456             :       cast_or_null<MDTuple>(TemplateParams), UniqueIdentifier);
     457           0 :   trackIfUnresolved(R);
     458           0 :   return R;
     459             : }
     460             : 
     461         215 : DICompositeType *DIBuilder::createStructType(
     462             :     DIScope *Context, StringRef Name, DIFile *File, unsigned LineNumber,
     463             :     uint64_t SizeInBits, uint32_t AlignInBits, DINode::DIFlags Flags,
     464             :     DIType *DerivedFrom, DINodeArray Elements, unsigned RunTimeLang,
     465             :     DIType *VTableHolder, StringRef UniqueIdentifier) {
     466         215 :   auto *R = DICompositeType::get(
     467             :       VMContext, dwarf::DW_TAG_structure_type, Name, File, LineNumber,
     468             :       getNonCompileUnitScope(Context), DerivedFrom, SizeInBits, AlignInBits, 0,
     469             :       Flags, Elements, RunTimeLang, VTableHolder, nullptr, UniqueIdentifier);
     470         215 :   trackIfUnresolved(R);
     471         215 :   return R;
     472             : }
     473             : 
     474           0 : DICompositeType *DIBuilder::createUnionType(
     475             :     DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber,
     476             :     uint64_t SizeInBits, uint32_t AlignInBits, DINode::DIFlags Flags,
     477             :     DINodeArray Elements, unsigned RunTimeLang, StringRef UniqueIdentifier) {
     478           0 :   auto *R = DICompositeType::get(
     479             :       VMContext, dwarf::DW_TAG_union_type, Name, File, LineNumber,
     480             :       getNonCompileUnitScope(Scope), nullptr, SizeInBits, AlignInBits, 0, Flags,
     481             :       Elements, RunTimeLang, nullptr, nullptr, UniqueIdentifier);
     482           0 :   trackIfUnresolved(R);
     483           0 :   return R;
     484             : }
     485             : 
     486           0 : DICompositeType *DIBuilder::createVariantPart(
     487             :     DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber,
     488             :     uint64_t SizeInBits, uint32_t AlignInBits, DINode::DIFlags Flags,
     489             :     DIDerivedType *Discriminator, DINodeArray Elements, StringRef UniqueIdentifier) {
     490           0 :   auto *R = DICompositeType::get(
     491             :       VMContext, dwarf::DW_TAG_variant_part, Name, File, LineNumber,
     492             :       getNonCompileUnitScope(Scope), nullptr, SizeInBits, AlignInBits, 0, Flags,
     493             :       Elements, 0, nullptr, nullptr, UniqueIdentifier, Discriminator);
     494           0 :   trackIfUnresolved(R);
     495           0 :   return R;
     496             : }
     497             : 
     498      436532 : DISubroutineType *DIBuilder::createSubroutineType(DITypeRefArray ParameterTypes,
     499             :                                                   DINode::DIFlags Flags,
     500             :                                                   unsigned CC) {
     501      873064 :   return DISubroutineType::get(VMContext, Flags, CC, ParameterTypes);
     502             : }
     503             : 
     504        1572 : DICompositeType *DIBuilder::createEnumerationType(
     505             :     DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber,
     506             :     uint64_t SizeInBits, uint32_t AlignInBits, DINodeArray Elements,
     507             :     DIType *UnderlyingType, StringRef UniqueIdentifier, bool IsFixed) {
     508        3144 :   auto *CTy = DICompositeType::get(
     509             :       VMContext, dwarf::DW_TAG_enumeration_type, Name, File, LineNumber,
     510             :       getNonCompileUnitScope(Scope), UnderlyingType, SizeInBits, AlignInBits, 0,
     511             :       IsFixed ? DINode::FlagFixedEnum : DINode::FlagZero, Elements, 0, nullptr,
     512             :       nullptr, UniqueIdentifier);
     513        1572 :   AllEnumTypes.push_back(CTy);
     514        1572 :   trackIfUnresolved(CTy);
     515        1572 :   return CTy;
     516             : }
     517             : 
     518        3675 : DICompositeType *DIBuilder::createArrayType(uint64_t Size,
     519             :                                             uint32_t AlignInBits, DIType *Ty,
     520             :                                             DINodeArray Subscripts) {
     521        3675 :   auto *R = DICompositeType::get(VMContext, dwarf::DW_TAG_array_type, "",
     522             :                                  nullptr, 0, nullptr, Ty, Size, AlignInBits, 0,
     523             :                                  DINode::FlagZero, Subscripts, 0, nullptr);
     524        3675 :   trackIfUnresolved(R);
     525        3675 :   return R;
     526             : }
     527             : 
     528          61 : DICompositeType *DIBuilder::createVectorType(uint64_t Size,
     529             :                                              uint32_t AlignInBits, DIType *Ty,
     530             :                                              DINodeArray Subscripts) {
     531          61 :   auto *R = DICompositeType::get(VMContext, dwarf::DW_TAG_array_type, "",
     532             :                                  nullptr, 0, nullptr, Ty, Size, AlignInBits, 0,
     533             :                                  DINode::FlagVector, Subscripts, 0, nullptr);
     534          61 :   trackIfUnresolved(R);
     535          61 :   return R;
     536             : }
     537             : 
     538           1 : DISubprogram *DIBuilder::createArtificialSubprogram(DISubprogram *SP) {
     539           1 :   auto NewSP = SP->cloneWithFlags(SP->getFlags() | DINode::FlagArtificial);
     540           1 :   return MDNode::replaceWithDistinct(std::move(NewSP));
     541             : }
     542             : 
     543      207166 : static DIType *createTypeWithFlags(const DIType *Ty,
     544             :                                    DINode::DIFlags FlagsToSet) {
     545      207166 :   auto NewTy = Ty->cloneWithFlags(Ty->getFlags() | FlagsToSet);
     546      207166 :   return MDNode::replaceWithUniqued(std::move(NewTy));
     547             : }
     548             : 
     549         124 : DIType *DIBuilder::createArtificialType(DIType *Ty) {
     550             :   // FIXME: Restrict this to the nodes where it's valid.
     551         124 :   if (Ty->isArtificial())
     552             :     return Ty;
     553         124 :   return createTypeWithFlags(Ty, DINode::FlagArtificial);
     554             : }
     555             : 
     556      207042 : DIType *DIBuilder::createObjectPointerType(DIType *Ty) {
     557             :   // FIXME: Restrict this to the nodes where it's valid.
     558      207042 :   if (Ty->isObjectPointer())
     559             :     return Ty;
     560             :   DINode::DIFlags Flags = DINode::FlagObjectPointer | DINode::FlagArtificial;
     561      207042 :   return createTypeWithFlags(Ty, Flags);
     562             : }
     563             : 
     564       10145 : void DIBuilder::retainType(DIScope *T) {
     565             :   assert(T && "Expected non-null type");
     566             :   assert((isa<DIType>(T) || (isa<DISubprogram>(T) &&
     567             :                              cast<DISubprogram>(T)->isDefinition() == false)) &&
     568             :          "Expected type or subprogram declaration");
     569       10145 :   AllRetainTypes.emplace_back(T);
     570       10145 : }
     571             : 
     572        5639 : DIBasicType *DIBuilder::createUnspecifiedParameter() { return nullptr; }
     573             : 
     574             : DICompositeType *
     575          69 : DIBuilder::createForwardDecl(unsigned Tag, StringRef Name, DIScope *Scope,
     576             :                              DIFile *F, unsigned Line, unsigned RuntimeLang,
     577             :                              uint64_t SizeInBits, uint32_t AlignInBits,
     578             :                              StringRef UniqueIdentifier) {
     579             :   // FIXME: Define in terms of createReplaceableForwardDecl() by calling
     580             :   // replaceWithUniqued().
     581          69 :   auto *RetTy = DICompositeType::get(
     582             :       VMContext, Tag, Name, F, Line, getNonCompileUnitScope(Scope), nullptr,
     583             :       SizeInBits, AlignInBits, 0, DINode::FlagFwdDecl, nullptr, RuntimeLang,
     584             :       nullptr, nullptr, UniqueIdentifier);
     585          69 :   trackIfUnresolved(RetTy);
     586          69 :   return RetTy;
     587             : }
     588             : 
     589       43142 : DICompositeType *DIBuilder::createReplaceableCompositeType(
     590             :     unsigned Tag, StringRef Name, DIScope *Scope, DIFile *F, unsigned Line,
     591             :     unsigned RuntimeLang, uint64_t SizeInBits, uint32_t AlignInBits,
     592             :     DINode::DIFlags Flags, StringRef UniqueIdentifier) {
     593             :   auto *RetTy =
     594       43142 :       DICompositeType::getTemporary(
     595             :           VMContext, Tag, Name, F, Line, getNonCompileUnitScope(Scope), nullptr,
     596             :           SizeInBits, AlignInBits, 0, Flags, nullptr, RuntimeLang, nullptr,
     597             :           nullptr, UniqueIdentifier)
     598             :           .release();
     599       43142 :   trackIfUnresolved(RetTy);
     600       43142 :   return RetTy;
     601             : }
     602             : 
     603      164583 : DINodeArray DIBuilder::getOrCreateArray(ArrayRef<Metadata *> Elements) {
     604      329166 :   return MDTuple::get(VMContext, Elements);
     605             : }
     606             : 
     607             : DIMacroNodeArray
     608          20 : DIBuilder::getOrCreateMacroArray(ArrayRef<Metadata *> Elements) {
     609          40 :   return MDTuple::get(VMContext, Elements);
     610             : }
     611             : 
     612      436532 : DITypeRefArray DIBuilder::getOrCreateTypeArray(ArrayRef<Metadata *> Elements) {
     613             :   SmallVector<llvm::Metadata *, 16> Elts;
     614     1521595 :   for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
     615     2170126 :     if (Elements[i] && isa<MDNode>(Elements[i]))
     616      919911 :       Elts.push_back(cast<DIType>(Elements[i]));
     617             :     else
     618      165152 :       Elts.push_back(Elements[i]);
     619             :   }
     620     1309596 :   return DITypeRefArray(MDNode::get(VMContext, Elts));
     621             : }
     622             : 
     623        3776 : DISubrange *DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
     624        7552 :   return DISubrange::get(VMContext, Count, Lo);
     625             : }
     626             : 
     627          16 : DISubrange *DIBuilder::getOrCreateSubrange(int64_t Lo, Metadata *CountNode) {
     628          32 :   return DISubrange::get(VMContext, CountNode, Lo);
     629             : }
     630             : 
     631             : static void checkGlobalVariableScope(DIScope *Context) {
     632             : #ifndef NDEBUG
     633             :   if (auto *CT =
     634             :           dyn_cast_or_null<DICompositeType>(getNonCompileUnitScope(Context)))
     635             :     assert(CT->getIdentifier().empty() &&
     636             :            "Context of a global variable should not be a type with identifier");
     637             : #endif
     638             : }
     639             : 
     640        4074 : DIGlobalVariableExpression *DIBuilder::createGlobalVariableExpression(
     641             :     DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *F,
     642             :     unsigned LineNumber, DIType *Ty, bool isLocalToUnit, DIExpression *Expr,
     643             :     MDNode *Decl, uint32_t AlignInBits) {
     644             :   checkGlobalVariableScope(Context);
     645             : 
     646        8148 :   auto *GV = DIGlobalVariable::getDistinct(
     647             :       VMContext, cast_or_null<DIScope>(Context), Name, LinkageName, F,
     648             :       LineNumber, Ty, isLocalToUnit, true, cast_or_null<DIDerivedType>(Decl),
     649             :       AlignInBits);
     650        4074 :   if (!Expr)
     651        4022 :     Expr = createExpression();
     652        4074 :   auto *N = DIGlobalVariableExpression::get(VMContext, GV, Expr);
     653        4074 :   AllGVs.push_back(N);
     654        4074 :   return N;
     655             : }
     656             : 
     657         262 : DIGlobalVariable *DIBuilder::createTempGlobalVariableFwdDecl(
     658             :     DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *F,
     659             :     unsigned LineNumber, DIType *Ty, bool isLocalToUnit, MDNode *Decl,
     660             :     uint32_t AlignInBits) {
     661             :   checkGlobalVariableScope(Context);
     662             : 
     663         524 :   return DIGlobalVariable::getTemporary(
     664             :              VMContext, cast_or_null<DIScope>(Context), Name, LinkageName, F,
     665             :              LineNumber, Ty, isLocalToUnit, false,
     666             :              cast_or_null<DIDerivedType>(Decl), AlignInBits)
     667         262 :       .release();
     668             : }
     669             : 
     670       84143 : static DILocalVariable *createLocalVariable(
     671             :     LLVMContext &VMContext,
     672             :     DenseMap<MDNode *, SmallVector<TrackingMDNodeRef, 1>> &PreservedVariables,
     673             :     DIScope *Scope, StringRef Name, unsigned ArgNo, DIFile *File,
     674             :     unsigned LineNo, DIType *Ty, bool AlwaysPreserve, DINode::DIFlags Flags,
     675             :     uint32_t AlignInBits) {
     676             :   // FIXME: Why getNonCompileUnitScope()?
     677             :   // FIXME: Why is "!Context" okay here?
     678             :   // FIXME: Why doesn't this check for a subprogram or lexical block (AFAICT
     679             :   // the only valid scopes)?
     680             :   DIScope *Context = getNonCompileUnitScope(Scope);
     681             : 
     682             :   auto *Node =
     683             :       DILocalVariable::get(VMContext, cast_or_null<DILocalScope>(Context), Name,
     684       84143 :                            File, LineNo, Ty, ArgNo, Flags, AlignInBits);
     685       84143 :   if (AlwaysPreserve) {
     686             :     // The optimizer may remove local variables. If there is an interest
     687             :     // to preserve variable info in such situation then stash it in a
     688             :     // named mdnode.
     689       12637 :     DISubprogram *Fn = getDISubprogram(Scope);
     690             :     assert(Fn && "Missing subprogram for local variable");
     691       25274 :     PreservedVariables[Fn].emplace_back(Node);
     692             :   }
     693       84143 :   return Node;
     694             : }
     695             : 
     696       16834 : DILocalVariable *DIBuilder::createAutoVariable(DIScope *Scope, StringRef Name,
     697             :                                                DIFile *File, unsigned LineNo,
     698             :                                                DIType *Ty, bool AlwaysPreserve,
     699             :                                                DINode::DIFlags Flags,
     700             :                                                uint32_t AlignInBits) {
     701       16834 :   return createLocalVariable(VMContext, PreservedVariables, Scope, Name,
     702             :                              /* ArgNo */ 0, File, LineNo, Ty, AlwaysPreserve,
     703       16834 :                              Flags, AlignInBits);
     704             : }
     705             : 
     706       67309 : DILocalVariable *DIBuilder::createParameterVariable(
     707             :     DIScope *Scope, StringRef Name, unsigned ArgNo, DIFile *File,
     708             :     unsigned LineNo, DIType *Ty, bool AlwaysPreserve, DINode::DIFlags Flags) {
     709             :   assert(ArgNo && "Expected non-zero argument number for parameter");
     710       67309 :   return createLocalVariable(VMContext, PreservedVariables, Scope, Name, ArgNo,
     711             :                              File, LineNo, Ty, AlwaysPreserve, Flags,
     712       67309 :                              /* AlignInBits */0);
     713             : }
     714             : 
     715           0 : DILabel *DIBuilder::createLabel(
     716             :     DIScope *Scope, StringRef Name, DIFile *File,
     717             :     unsigned LineNo, bool AlwaysPreserve) {
     718             :   DIScope *Context = getNonCompileUnitScope(Scope);
     719             : 
     720             :   auto *Node =
     721           0 :       DILabel::get(VMContext, cast_or_null<DILocalScope>(Context), Name,
     722           0 :                    File, LineNo);
     723             : 
     724           0 :   if (AlwaysPreserve) {
     725             :     /// The optimizer may remove labels. If there is an interest
     726             :     /// to preserve label info in such situation then append it to
     727             :     /// the list of retained nodes of the DISubprogram.
     728           0 :     DISubprogram *Fn = getDISubprogram(Scope);
     729             :     assert(Fn && "Missing subprogram for label");
     730           0 :     PreservedLabels[Fn].emplace_back(Node);
     731             :   }
     732           0 :   return Node;
     733             : }
     734             : 
     735       88188 : DIExpression *DIBuilder::createExpression(ArrayRef<uint64_t> Addr) {
     736      176376 :   return DIExpression::get(VMContext, Addr);
     737             : }
     738             : 
     739       82971 : DIExpression *DIBuilder::createExpression(ArrayRef<int64_t> Signed) {
     740             :   // TODO: Remove the callers of this signed version and delete.
     741             :   SmallVector<uint64_t, 8> Addr(Signed.begin(), Signed.end());
     742      165942 :   return createExpression(Addr);
     743             : }
     744             : 
     745             : template <class... Ts>
     746      275877 : static DISubprogram *getSubprogram(bool IsDistinct, Ts &&... Args) {
     747      275877 :   if (IsDistinct)
     748       70168 :     return DISubprogram::getDistinct(std::forward<Ts>(Args)...);
     749      205709 :   return DISubprogram::get(std::forward<Ts>(Args)...);
     750             : }
     751             : 
     752       70200 : DISubprogram *DIBuilder::createFunction(
     753             :     DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *File,
     754             :     unsigned LineNo, DISubroutineType *Ty, bool isLocalToUnit,
     755             :     bool isDefinition, unsigned ScopeLine, DINode::DIFlags Flags,
     756             :     bool isOptimized, DITemplateParameterArray TParams, DISubprogram *Decl,
     757             :     DITypeArray ThrownTypes) {
     758      210600 :   auto *Node = getSubprogram(
     759             :       /* IsDistinct = */ isDefinition, VMContext,
     760      140400 :       getNonCompileUnitScope(Context), Name, LinkageName, File, LineNo, Ty,
     761             :       isLocalToUnit, isDefinition, ScopeLine, nullptr, 0, 0, 0, Flags,
     762      140400 :       isOptimized, isDefinition ? CUNode : nullptr, TParams, Decl,
     763      280800 :       MDTuple::getTemporary(VMContext, None).release(), ThrownTypes);
     764             : 
     765       70200 :   if (isDefinition)
     766       70168 :     AllSubprograms.push_back(Node);
     767       70200 :   trackIfUnresolved(Node);
     768       70200 :   return Node;
     769             : }
     770             : 
     771       87711 : DISubprogram *DIBuilder::createTempFunctionFwdDecl(
     772             :     DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *File,
     773             :     unsigned LineNo, DISubroutineType *Ty, bool isLocalToUnit,
     774             :     bool isDefinition, unsigned ScopeLine, DINode::DIFlags Flags,
     775             :     bool isOptimized, DITemplateParameterArray TParams, DISubprogram *Decl,
     776             :     DITypeArray ThrownTypes) {
     777      175422 :   return DISubprogram::getTemporary(
     778             :              VMContext, getNonCompileUnitScope(Context), Name, LinkageName,
     779             :              File, LineNo, Ty, isLocalToUnit, isDefinition, ScopeLine, nullptr,
     780             :              0, 0, 0, Flags, isOptimized, isDefinition ? CUNode : nullptr,
     781             :              TParams, Decl, nullptr, ThrownTypes)
     782       87711 :       .release();
     783             : }
     784             : 
     785      205677 : DISubprogram *DIBuilder::createMethod(
     786             :     DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *F,
     787             :     unsigned LineNo, DISubroutineType *Ty, bool isLocalToUnit,
     788             :     bool isDefinition, unsigned VK, unsigned VIndex, int ThisAdjustment,
     789             :     DIType *VTableHolder, DINode::DIFlags Flags, bool isOptimized,
     790             :     DITemplateParameterArray TParams, DITypeArray ThrownTypes) {
     791             :   assert(getNonCompileUnitScope(Context) &&
     792             :          "Methods should have both a Context and a context that isn't "
     793             :          "the compile unit.");
     794             :   // FIXME: Do we want to use different scope/lines?
     795      617031 :   auto *SP = getSubprogram(
     796      411354 :       /* IsDistinct = */ isDefinition, VMContext, cast<DIScope>(Context), Name,
     797             :       LinkageName, F, LineNo, Ty, isLocalToUnit, isDefinition, LineNo,
     798             :       VTableHolder, VK, VIndex, ThisAdjustment, Flags, isOptimized,
     799      617031 :       isDefinition ? CUNode : nullptr, TParams, nullptr, nullptr, ThrownTypes);
     800             : 
     801      205677 :   if (isDefinition)
     802           0 :     AllSubprograms.push_back(SP);
     803      205677 :   trackIfUnresolved(SP);
     804      205677 :   return SP;
     805             : }
     806             : 
     807       20138 : DINamespace *DIBuilder::createNameSpace(DIScope *Scope, StringRef Name,
     808             :                                         bool ExportSymbols) {
     809             : 
     810             :   // It is okay to *not* make anonymous top-level namespaces distinct, because
     811             :   // all nodes that have an anonymous namespace as their parent scope are
     812             :   // guaranteed to be unique and/or are linked to their containing
     813             :   // DICompileUnit. This decision is an explicit tradeoff of link time versus
     814             :   // memory usage versus code simplicity and may get revisited in the future.
     815       40276 :   return DINamespace::get(VMContext, getNonCompileUnitScope(Scope), Name,
     816       20138 :                           ExportSymbols);
     817             : }
     818             : 
     819         149 : DIModule *DIBuilder::createModule(DIScope *Scope, StringRef Name,
     820             :                                   StringRef ConfigurationMacros,
     821             :                                   StringRef IncludePath,
     822             :                                   StringRef ISysRoot) {
     823         149 :  return DIModule::get(VMContext, getNonCompileUnitScope(Scope), Name,
     824         149 :                       ConfigurationMacros, IncludePath, ISysRoot);
     825             : }
     826             : 
     827         317 : DILexicalBlockFile *DIBuilder::createLexicalBlockFile(DIScope *Scope,
     828             :                                                       DIFile *File,
     829             :                                                       unsigned Discriminator) {
     830         634 :   return DILexicalBlockFile::get(VMContext, Scope, File, Discriminator);
     831             : }
     832             : 
     833       27460 : DILexicalBlock *DIBuilder::createLexicalBlock(DIScope *Scope, DIFile *File,
     834             :                                               unsigned Line, unsigned Col) {
     835             :   // Make these distinct, to avoid merging two lexical blocks on the same
     836             :   // file/line/column.
     837       27460 :   return DILexicalBlock::getDistinct(VMContext, getNonCompileUnitScope(Scope),
     838       27460 :                                      File, Line, Col);
     839             : }
     840             : 
     841        1981 : Instruction *DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
     842             :                                       DIExpression *Expr, const DILocation *DL,
     843             :                                       Instruction *InsertBefore) {
     844        1981 :   return insertDeclare(Storage, VarInfo, Expr, DL, InsertBefore->getParent(),
     845        1981 :                        InsertBefore);
     846             : }
     847             : 
     848       82960 : Instruction *DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
     849             :                                       DIExpression *Expr, const DILocation *DL,
     850             :                                       BasicBlock *InsertAtEnd) {
     851             :   // If this block already has a terminator then insert this intrinsic before
     852             :   // the terminator. Otherwise, put it at the end of the block.
     853             :   Instruction *InsertBefore = InsertAtEnd->getTerminator();
     854       82960 :   return insertDeclare(Storage, VarInfo, Expr, DL, InsertAtEnd, InsertBefore);
     855             : }
     856             : 
     857           0 : Instruction *DIBuilder::insertLabel(DILabel *LabelInfo, const DILocation *DL,
     858             :                                     Instruction *InsertBefore) {
     859           0 :   return insertLabel(
     860             :       LabelInfo, DL, InsertBefore ? InsertBefore->getParent() : nullptr,
     861           0 :       InsertBefore);
     862             : }
     863             : 
     864           0 : Instruction *DIBuilder::insertLabel(DILabel *LabelInfo, const DILocation *DL,
     865             :                                     BasicBlock *InsertAtEnd) {
     866           0 :   return insertLabel(LabelInfo, DL, InsertAtEnd, nullptr);
     867             : }
     868             : 
     869       16145 : Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V,
     870             :                                                 DILocalVariable *VarInfo,
     871             :                                                 DIExpression *Expr,
     872             :                                                 const DILocation *DL,
     873             :                                                 Instruction *InsertBefore) {
     874       16145 :   return insertDbgValueIntrinsic(
     875             :       V, VarInfo, Expr, DL, InsertBefore ? InsertBefore->getParent() : nullptr,
     876       16145 :       InsertBefore);
     877             : }
     878             : 
     879           5 : Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V,
     880             :                                                 DILocalVariable *VarInfo,
     881             :                                                 DIExpression *Expr,
     882             :                                                 const DILocation *DL,
     883             :                                                 BasicBlock *InsertAtEnd) {
     884           5 :   return insertDbgValueIntrinsic(V, VarInfo, Expr, DL, InsertAtEnd, nullptr);
     885             : }
     886             : 
     887             : /// Return an IRBuilder for inserting dbg.declare and dbg.value intrinsics. This
     888             : /// abstracts over the various ways to specify an insert position.
     889      101091 : static IRBuilder<> getIRBForDbgInsertion(const DILocation *DL,
     890             :                                          BasicBlock *InsertBB,
     891             :                                          Instruction *InsertBefore) {
     892             :   IRBuilder<> B(DL->getContext());
     893      101091 :   if (InsertBefore)
     894       17880 :     B.SetInsertPoint(InsertBefore);
     895       83211 :   else if (InsertBB)
     896             :     B.SetInsertPoint(InsertBB);
     897      202182 :   B.SetCurrentDebugLocation(DL);
     898      101091 :   return B;
     899             : }
     900             : 
     901             : static Value *getDbgIntrinsicValueImpl(LLVMContext &VMContext, Value *V) {
     902             :   assert(V && "no value passed to dbg intrinsic");
     903      101091 :   return MetadataAsValue::get(VMContext, ValueAsMetadata::get(V));
     904             : }
     905             : 
     906             : static Function *getDeclareIntrin(Module &M) {
     907        5854 :   return Intrinsic::getDeclaration(&M, UseDbgAddr ? Intrinsic::dbg_addr
     908        2927 :                                                   : Intrinsic::dbg_declare);
     909             : }
     910             : 
     911       84941 : Instruction *DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
     912             :                                       DIExpression *Expr, const DILocation *DL,
     913             :                                       BasicBlock *InsertBB, Instruction *InsertBefore) {
     914             :   assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.declare");
     915             :   assert(DL && "Expected debug loc");
     916             :   assert(DL->getScope()->getSubprogram() ==
     917             :              VarInfo->getScope()->getSubprogram() &&
     918             :          "Expected matching subprograms");
     919       84941 :   if (!DeclareFn)
     920        5854 :     DeclareFn = getDeclareIntrin(M);
     921             : 
     922       84941 :   trackIfUnresolved(VarInfo);
     923       84941 :   trackIfUnresolved(Expr);
     924       84941 :   Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, Storage),
     925       84941 :                    MetadataAsValue::get(VMContext, VarInfo),
     926      169882 :                    MetadataAsValue::get(VMContext, Expr)};
     927             : 
     928       84941 :   IRBuilder<> B = getIRBForDbgInsertion(DL, InsertBB, InsertBefore);
     929      254823 :   return B.CreateCall(DeclareFn, Args);
     930             : }
     931             : 
     932       16150 : Instruction *DIBuilder::insertDbgValueIntrinsic(
     933             :     Value *V, DILocalVariable *VarInfo, DIExpression *Expr,
     934             :     const DILocation *DL, BasicBlock *InsertBB, Instruction *InsertBefore) {
     935             :   assert(V && "no value passed to dbg.value");
     936             :   assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.value");
     937             :   assert(DL && "Expected debug loc");
     938             :   assert(DL->getScope()->getSubprogram() ==
     939             :              VarInfo->getScope()->getSubprogram() &&
     940             :          "Expected matching subprograms");
     941       16150 :   if (!ValueFn)
     942       11408 :     ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
     943             : 
     944       16150 :   trackIfUnresolved(VarInfo);
     945       16150 :   trackIfUnresolved(Expr);
     946       16150 :   Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, V),
     947       16150 :                    MetadataAsValue::get(VMContext, VarInfo),
     948       32300 :                    MetadataAsValue::get(VMContext, Expr)};
     949             : 
     950       16150 :   IRBuilder<> B = getIRBForDbgInsertion(DL, InsertBB, InsertBefore);
     951       48450 :   return B.CreateCall(ValueFn, Args);
     952             : }
     953             : 
     954           0 : Instruction *DIBuilder::insertLabel(
     955             :     DILabel *LabelInfo, const DILocation *DL,
     956             :     BasicBlock *InsertBB, Instruction *InsertBefore) {
     957             :   assert(LabelInfo && "empty or invalid DILabel* passed to dbg.label");
     958             :   assert(DL && "Expected debug loc");
     959             :   assert(DL->getScope()->getSubprogram() ==
     960             :              LabelInfo->getScope()->getSubprogram() &&
     961             :          "Expected matching subprograms");
     962           0 :   if (!LabelFn)
     963           0 :     LabelFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_label);
     964             : 
     965           0 :   trackIfUnresolved(LabelInfo);
     966           0 :   Value *Args[] = {MetadataAsValue::get(VMContext, LabelInfo)};
     967             : 
     968           0 :   IRBuilder<> B = getIRBForDbgInsertion(DL, InsertBB, InsertBefore);
     969           0 :   return B.CreateCall(LabelFn, Args);
     970             : }
     971             : 
     972       36193 : void DIBuilder::replaceVTableHolder(DICompositeType *&T,
     973             :                                     DIType *VTableHolder) {
     974             :   {
     975       36193 :     TypedTrackingMDRef<DICompositeType> N(T);
     976             :     N->replaceVTableHolder(VTableHolder);
     977       36193 :     T = N.get();
     978             :   }
     979             : 
     980             :   // If this didn't create a self-reference, just return.
     981       36193 :   if (T != VTableHolder)
     982             :     return;
     983             : 
     984             :   // Look for unresolved operands.  T will drop RAUW support, orphaning any
     985             :   // cycles underneath it.
     986             :   if (T->isResolved())
     987       16440 :     for (const MDOperand &O : T->operands())
     988             :       if (auto *N = dyn_cast_or_null<MDNode>(O))
     989        2406 :         trackIfUnresolved(N);
     990             : }
     991             : 
     992      101913 : void DIBuilder::replaceArrays(DICompositeType *&T, DINodeArray Elements,
     993             :                               DINodeArray TParams) {
     994             :   {
     995      101913 :     TypedTrackingMDRef<DICompositeType> N(T);
     996      101913 :     if (Elements)
     997             :       N->replaceElements(Elements);
     998      101913 :     if (TParams)
     999             :       N->replaceTemplateParams(DITemplateParameterArray(TParams));
    1000      101913 :     T = N.get();
    1001             :   }
    1002             : 
    1003             :   // If T isn't resolved, there's no problem.
    1004      101913 :   if (!T->isResolved())
    1005             :     return;
    1006             : 
    1007             :   // If T is resolved, it may be due to a self-reference cycle.  Track the
    1008             :   // arrays explicitly if they're unresolved, or else the cycles will be
    1009             :   // orphaned.
    1010       96843 :   if (Elements)
    1011       37045 :     trackIfUnresolved(Elements.get());
    1012       96843 :   if (TParams)
    1013       22889 :     trackIfUnresolved(TParams.get());
    1014      303579 : }

Generated by: LCOV version 1.13