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

Generated by: LCOV version 1.13