LCOV - code coverage report
Current view: top level - lib/IR - DebugInfo.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 349 476 73.3 %
Date: 2018-05-20 00:06:23 Functions: 63 104 60.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- DebugInfo.cpp - Debug Information Helper Classes -------------------===//
       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 helper classes used to build and interpret debug
      11             : // information in LLVM IR form.
      12             : //
      13             : //===----------------------------------------------------------------------===//
      14             : 
      15             : #include "llvm-c/DebugInfo.h"
      16             : #include "llvm/ADT/DenseMap.h"
      17             : #include "llvm/ADT/DenseSet.h"
      18             : #include "llvm/ADT/None.h"
      19             : #include "llvm/ADT/STLExtras.h"
      20             : #include "llvm/ADT/SmallPtrSet.h"
      21             : #include "llvm/ADT/SmallVector.h"
      22             : #include "llvm/ADT/StringRef.h"
      23             : #include "llvm/IR/BasicBlock.h"
      24             : #include "llvm/IR/Constants.h"
      25             : #include "llvm/IR/DebugInfoMetadata.h"
      26             : #include "llvm/IR/DebugLoc.h"
      27             : #include "llvm/IR/DebugInfo.h"
      28             : #include "llvm/IR/DIBuilder.h"
      29             : #include "llvm/IR/Function.h"
      30             : #include "llvm/IR/GVMaterializer.h"
      31             : #include "llvm/IR/Instruction.h"
      32             : #include "llvm/IR/IntrinsicInst.h"
      33             : #include "llvm/IR/LLVMContext.h"
      34             : #include "llvm/IR/Metadata.h"
      35             : #include "llvm/IR/Module.h"
      36             : #include "llvm/Support/Casting.h"
      37             : #include <algorithm>
      38             : #include <cassert>
      39             : #include <utility>
      40             : 
      41             : using namespace llvm;
      42             : using namespace llvm::dwarf;
      43             : 
      44      291634 : DISubprogram *llvm::getDISubprogram(const MDNode *Scope) {
      45             :   if (auto *LocalScope = dyn_cast_or_null<DILocalScope>(Scope))
      46      291634 :     return LocalScope->getSubprogram();
      47             :   return nullptr;
      48             : }
      49             : 
      50             : //===----------------------------------------------------------------------===//
      51             : // DebugInfoFinder implementations.
      52             : //===----------------------------------------------------------------------===//
      53             : 
      54           0 : void DebugInfoFinder::reset() {
      55             :   CUs.clear();
      56             :   SPs.clear();
      57             :   GVs.clear();
      58             :   TYs.clear();
      59             :   Scopes.clear();
      60           0 :   NodesSeen.clear();
      61           0 : }
      62             : 
      63          28 : void DebugInfoFinder::processModule(const Module &M) {
      64          28 :   for (auto *CU : M.debug_compile_units())
      65          20 :     processCompileUnit(CU);
      66          82 :   for (auto &F : M.functions()) {
      67          54 :     if (auto *SP = cast_or_null<DISubprogram>(F.getSubprogram()))
      68          15 :       processSubprogram(SP);
      69             :     // There could be subprograms from inlined functions referenced from
      70             :     // instructions only. Walk the function to find them.
      71          94 :     for (const BasicBlock &BB : F)
      72         151 :       for (const Instruction &I : BB)
      73         111 :         processInstruction(M, I);
      74             :   }
      75          28 : }
      76             : 
      77         106 : void DebugInfoFinder::processCompileUnit(DICompileUnit *CU) {
      78         106 :   if (!addCompileUnit(CU))
      79             :     return;
      80          86 :   for (auto DIG : CU->getGlobalVariables()) {
      81           8 :     if (!addGlobalVariable(DIG))
      82           0 :       continue;
      83             :     auto *GV = DIG->getVariable();
      84           8 :     processScope(GV->getScope());
      85           8 :     processType(GV->getType().resolve());
      86             :   }
      87          78 :   for (auto *ET : CU->getEnumTypes())
      88           0 :     processType(ET);
      89          78 :   for (auto *RT : CU->getRetainedTypes())
      90             :     if (auto *T = dyn_cast<DIType>(RT))
      91           0 :       processType(T);
      92             :     else
      93           0 :       processSubprogram(cast<DISubprogram>(RT));
      94          79 :   for (auto *Import : CU->getImportedEntities()) {
      95             :     auto *Entity = Import->getEntity().resolve();
      96             :     if (auto *T = dyn_cast<DIType>(Entity))
      97           0 :       processType(T);
      98             :     else if (auto *SP = dyn_cast<DISubprogram>(Entity))
      99           0 :       processSubprogram(SP);
     100             :     else if (auto *NS = dyn_cast<DINamespace>(Entity))
     101           0 :       processScope(NS->getScope());
     102             :     else if (auto *M = dyn_cast<DIModule>(Entity))
     103           0 :       processScope(M->getScope());
     104             :   }
     105             : }
     106             : 
     107        4883 : void DebugInfoFinder::processInstruction(const Module &M,
     108             :                                          const Instruction &I) {
     109             :   if (auto *DDI = dyn_cast<DbgDeclareInst>(&I))
     110          24 :     processDeclare(M, DDI);
     111             :   else if (auto *DVI = dyn_cast<DbgValueInst>(&I))
     112          41 :     processValue(M, DVI);
     113             : 
     114        4883 :   if (auto DbgLoc = I.getDebugLoc())
     115         525 :     processLocation(M, DbgLoc.get());
     116        4883 : }
     117             : 
     118         525 : void DebugInfoFinder::processLocation(const Module &M, const DILocation *Loc) {
     119        1094 :   if (!Loc)
     120             :     return;
     121         569 :   processScope(Loc->getScope());
     122             :   processLocation(M, Loc->getInlinedAt());
     123             : }
     124             : 
     125         236 : void DebugInfoFinder::processType(DIType *DT) {
     126         245 :   if (!addType(DT))
     127             :     return;
     128         145 :   processScope(DT->getScope().resolve());
     129             :   if (auto *ST = dyn_cast<DISubroutineType>(DT)) {
     130         164 :     for (DITypeRef Ref : ST->getTypeArray())
     131          93 :       processType(Ref.resolve());
     132             :     return;
     133             :   }
     134             :   if (auto *DCT = dyn_cast<DICompositeType>(DT)) {
     135          10 :     processType(DCT->getBaseType().resolve());
     136          11 :     for (Metadata *D : DCT->getElements()) {
     137             :       if (auto *T = dyn_cast<DIType>(D))
     138           1 :         processType(T);
     139             :       else if (auto *SP = dyn_cast<DISubprogram>(D))
     140           0 :         processSubprogram(SP);
     141             :     }
     142             :     return;
     143             :   }
     144             :   if (auto *DDT = dyn_cast<DIDerivedType>(DT)) {
     145             :     processType(DDT->getBaseType().resolve());
     146             :   }
     147             : }
     148             : 
     149         845 : void DebugInfoFinder::processScope(DIScope *Scope) {
     150         890 :   if (!Scope)
     151             :     return;
     152             :   if (auto *Ty = dyn_cast<DIType>(Scope)) {
     153           1 :     processType(Ty);
     154           1 :     return;
     155             :   }
     156             :   if (auto *CU = dyn_cast<DICompileUnit>(Scope)) {
     157           1 :     addCompileUnit(CU);
     158           1 :     return;
     159             :   }
     160             :   if (auto *SP = dyn_cast<DISubprogram>(Scope)) {
     161         519 :     processSubprogram(SP);
     162         519 :     return;
     163             :   }
     164         171 :   if (!addScope(Scope))
     165             :     return;
     166             :   if (auto *LB = dyn_cast<DILexicalBlockBase>(Scope)) {
     167             :     processScope(LB->getScope());
     168             :   } else if (auto *NS = dyn_cast<DINamespace>(Scope)) {
     169             :     processScope(NS->getScope());
     170             :   } else if (auto *M = dyn_cast<DIModule>(Scope)) {
     171             :     processScope(M->getScope());
     172             :   }
     173             : }
     174             : 
     175         534 : void DebugInfoFinder::processSubprogram(DISubprogram *SP) {
     176         534 :   if (!addSubprogram(SP))
     177             :     return;
     178          86 :   processScope(SP->getScope().resolve());
     179             :   // Some of the users, e.g. CloneFunctionInto / CloneModule, need to set up a
     180             :   // ValueMap containing identity mappings for all of the DICompileUnit's, not
     181             :   // just DISubprogram's, referenced from anywhere within the Function being
     182             :   // cloned prior to calling MapMetadata / RemapInstruction to avoid their
     183             :   // duplication later as DICompileUnit's are also directly referenced by
     184             :   // llvm.dbg.cu list. Thefore we need to collect DICompileUnit's here as well.
     185             :   // Also, DICompileUnit's may reference DISubprogram's too and therefore need
     186             :   // to be at least looked through.
     187          86 :   processCompileUnit(SP->getUnit());
     188          86 :   processType(SP->getType());
     189          86 :   for (auto *Element : SP->getTemplateParams()) {
     190             :     if (auto *TType = dyn_cast<DITemplateTypeParameter>(Element)) {
     191           0 :       processType(TType->getType().resolve());
     192             :     } else if (auto *TVal = dyn_cast<DITemplateValueParameter>(Element)) {
     193           0 :       processType(TVal->getType().resolve());
     194             :     }
     195             :   }
     196             : }
     197             : 
     198          24 : void DebugInfoFinder::processDeclare(const Module &M,
     199             :                                      const DbgDeclareInst *DDI) {
     200             :   auto *N = dyn_cast<MDNode>(DDI->getVariable());
     201          24 :   if (!N)
     202             :     return;
     203             : 
     204             :   auto *DV = dyn_cast<DILocalVariable>(N);
     205             :   if (!DV)
     206             :     return;
     207             : 
     208          24 :   if (!NodesSeen.insert(DV).second)
     209             :     return;
     210          20 :   processScope(DV->getScope());
     211          20 :   processType(DV->getType().resolve());
     212             : }
     213             : 
     214          41 : void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
     215             :   auto *N = dyn_cast<MDNode>(DVI->getVariable());
     216          41 :   if (!N)
     217             :     return;
     218             : 
     219             :   auto *DV = dyn_cast<DILocalVariable>(N);
     220             :   if (!DV)
     221             :     return;
     222             : 
     223          41 :   if (!NodesSeen.insert(DV).second)
     224             :     return;
     225          17 :   processScope(DV->getScope());
     226          17 :   processType(DV->getType().resolve());
     227             : }
     228             : 
     229         245 : bool DebugInfoFinder::addType(DIType *DT) {
     230         245 :   if (!DT)
     231             :     return false;
     232             : 
     233         224 :   if (!NodesSeen.insert(DT).second)
     234             :     return false;
     235             : 
     236         145 :   TYs.push_back(const_cast<DIType *>(DT));
     237         145 :   return true;
     238             : }
     239             : 
     240         107 : bool DebugInfoFinder::addCompileUnit(DICompileUnit *CU) {
     241         107 :   if (!CU)
     242             :     return false;
     243         107 :   if (!NodesSeen.insert(CU).second)
     244             :     return false;
     245             : 
     246          78 :   CUs.push_back(CU);
     247          78 :   return true;
     248             : }
     249             : 
     250           8 : bool DebugInfoFinder::addGlobalVariable(DIGlobalVariableExpression *DIG) {
     251           8 :   if (!NodesSeen.insert(DIG).second)
     252             :     return false;
     253             : 
     254           8 :   GVs.push_back(DIG);
     255           8 :   return true;
     256             : }
     257             : 
     258         534 : bool DebugInfoFinder::addSubprogram(DISubprogram *SP) {
     259         534 :   if (!SP)
     260             :     return false;
     261             : 
     262         534 :   if (!NodesSeen.insert(SP).second)
     263             :     return false;
     264             : 
     265          86 :   SPs.push_back(SP);
     266          86 :   return true;
     267             : }
     268             : 
     269         171 : bool DebugInfoFinder::addScope(DIScope *Scope) {
     270         171 :   if (!Scope)
     271             :     return false;
     272             :   // FIXME: Ocaml binding generates a scope with no content, we treat it
     273             :   // as null for now.
     274         171 :   if (Scope->getNumOperands() == 0)
     275             :     return false;
     276         171 :   if (!NodesSeen.insert(Scope).second)
     277             :     return false;
     278          75 :   Scopes.push_back(Scope);
     279          75 :   return true;
     280             : }
     281             : 
     282         737 : static MDNode *stripDebugLocFromLoopID(MDNode *N) {
     283             :   assert(N->op_begin() != N->op_end() && "Missing self reference?");
     284             : 
     285             :   // if there is no debug location, we do not have to rewrite this MDNode.
     286        1474 :   if (std::none_of(N->op_begin() + 1, N->op_end(), [](const MDOperand &Op) {
     287             :         return isa<DILocation>(Op.get());
     288             :       }))
     289             :     return N;
     290             : 
     291             :   // If there is only the debug location without any actual loop metadata, we
     292             :   // can remove the metadata.
     293           4 :   if (std::none_of(N->op_begin() + 1, N->op_end(), [](const MDOperand &Op) {
     294             :         return !isa<DILocation>(Op.get());
     295             :       }))
     296             :     return nullptr;
     297             : 
     298             :   SmallVector<Metadata *, 4> Args;
     299             :   // Reserve operand 0 for loop id self reference.
     300             :   auto TempNode = MDNode::getTemporary(N->getContext(), None);
     301           2 :   Args.push_back(TempNode.get());
     302             :   // Add all non-debug location operands back.
     303           7 :   for (auto Op = N->op_begin() + 1; Op != N->op_end(); Op++) {
     304           5 :     if (!isa<DILocation>(*Op))
     305           2 :       Args.push_back(*Op);
     306             :   }
     307             : 
     308             :   // Set the first operand to itself.
     309             :   MDNode *LoopID = MDNode::get(N->getContext(), Args);
     310           2 :   LoopID->replaceOperandWith(0, LoopID);
     311             :   return LoopID;
     312             : }
     313             : 
     314      295410 : bool llvm::stripDebugInfo(Function &F) {
     315             :   bool Changed = false;
     316      295410 :   if (F.getMetadata(LLVMContext::MD_dbg)) {
     317             :     Changed = true;
     318         421 :     F.setSubprogram(nullptr);
     319             :   }
     320             : 
     321             :   DenseMap<MDNode*, MDNode*> LoopIDsMap;
     322      629268 :   for (BasicBlock &BB : F) {
     323     1879324 :     for (auto II = BB.begin(), End = BB.end(); II != End;) {
     324             :       Instruction &I = *II++; // We may delete the instruction, increment now.
     325        4827 :       if (isa<DbgInfoIntrinsic>(&I)) {
     326        4827 :         I.eraseFromParent();
     327             :         Changed = true;
     328        4827 :         continue;
     329             :       }
     330     1540639 :       if (I.getDebugLoc()) {
     331             :         Changed = true;
     332       10638 :         I.setDebugLoc(DebugLoc());
     333             :       }
     334             :     }
     335             : 
     336             :     auto *TermInst = BB.getTerminator();
     337      333858 :     if (!TermInst)
     338             :       // This is invalid IR, but we may not have run the verifier yet
     339           0 :       continue;
     340      667716 :     if (auto *LoopID = TermInst->getMetadata(LLVMContext::MD_loop)) {
     341             :       auto *NewLoopID = LoopIDsMap.lookup(LoopID);
     342          18 :       if (!NewLoopID)
     343        1474 :         NewLoopID = LoopIDsMap[LoopID] = stripDebugLocFromLoopID(LoopID);
     344         755 :       if (NewLoopID != LoopID)
     345           5 :         TermInst->setMetadata(LLVMContext::MD_loop, NewLoopID);
     346             :     }
     347             :   }
     348      295410 :   return Changed;
     349             : }
     350             : 
     351       34109 : bool llvm::StripDebugInfo(Module &M) {
     352             :   bool Changed = false;
     353             : 
     354             :   for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
     355       37295 :          NME = M.named_metadata_end(); NMI != NME;) {
     356             :     NamedMDNode *NMD = &*NMI;
     357             :     ++NMI;
     358             : 
     359             :     // We're stripping debug info, and without them, coverage information
     360             :     // doesn't quite make sense.
     361        5954 :     if (NMD->getName().startswith("llvm.dbg.") ||
     362        2769 :         NMD->getName() == "llvm.gcov") {
     363         418 :       NMD->eraseFromParent();
     364             :       Changed = true;
     365             :     }
     366             :   }
     367             : 
     368      329034 :   for (Function &F : M)
     369      294925 :     Changed |= stripDebugInfo(F);
     370             : 
     371       72611 :   for (auto &GV : M.globals()) {
     372             :     SmallVector<MDNode *, 1> MDs;
     373       38502 :     GV.getMetadata(LLVMContext::MD_dbg, MDs);
     374       38502 :     if (!MDs.empty()) {
     375          17 :       GV.eraseMetadata(LLVMContext::MD_dbg);
     376             :       Changed = true;
     377             :     }
     378             :   }
     379             : 
     380       34109 :   if (GVMaterializer *Materializer = M.getMaterializer())
     381         598 :     Materializer->setStripDebugInfo();
     382             : 
     383       34109 :   return Changed;
     384             : }
     385             : 
     386             : namespace {
     387             : 
     388             : /// Helper class to downgrade -g metadata to -gline-tables-only metadata.
     389             : class DebugTypeInfoRemoval {
     390             :   DenseMap<Metadata *, Metadata *> Replacements;
     391             : 
     392             : public:
     393             :   /// The (void)() type.
     394             :   MDNode *EmptySubroutineType;
     395             : 
     396             : private:
     397             :   /// Remember what linkage name we originally had before stripping. If we end
     398             :   /// up making two subprograms identical who originally had different linkage
     399             :   /// names, then we need to make one of them distinct, to avoid them getting
     400             :   /// uniqued. Maps the new node to the old linkage name.
     401             :   DenseMap<DISubprogram *, StringRef> NewToLinkageName;
     402             : 
     403             :   // TODO: Remember the distinct subprogram we created for a given linkage name,
     404             :   // so that we can continue to unique whenever possible. Map <newly created
     405             :   // node, old linkage name> to the first (possibly distinct) mdsubprogram
     406             :   // created for that combination. This is not strictly needed for correctness,
     407             :   // but can cut down on the number of MDNodes and let us diff cleanly with the
     408             :   // output of -gline-tables-only.
     409             : 
     410             : public:
     411          13 :   DebugTypeInfoRemoval(LLVMContext &C)
     412          13 :       : EmptySubroutineType(DISubroutineType::get(C, DINode::FlagZero, 0,
     413          39 :                                                   MDNode::get(C, {}))) {}
     414             : 
     415         183 :   Metadata *map(Metadata *M) {
     416         183 :     if (!M)
     417             :       return nullptr;
     418         171 :     auto Replacement = Replacements.find(M);
     419         171 :     if (Replacement != Replacements.end())
     420         114 :       return Replacement->second;
     421             : 
     422             :     return M;
     423             :   }
     424          66 :   MDNode *mapNode(Metadata *N) { return dyn_cast_or_null<MDNode>(map(N)); }
     425             : 
     426             :   /// Recursively remap N and all its referenced children. Does a DF post-order
     427             :   /// traversal, so as to remap bottoms up.
     428          66 :   void traverseAndRemap(MDNode *N) { traverse(N); }
     429             : 
     430             : private:
     431             :   // Create a new DISubprogram, to replace the one given.
     432           8 :   DISubprogram *getReplacementSubprogram(DISubprogram *MDS) {
     433          24 :     auto *FileAndScope = cast_or_null<DIFile>(map(MDS->getFile()));
     434          17 :     StringRef LinkageName = MDS->getName().empty() ? MDS->getLinkageName() : "";
     435           8 :     DISubprogram *Declaration = nullptr;
     436          24 :     auto *Type = cast_or_null<DISubroutineType>(map(MDS->getType()));
     437          16 :     DITypeRef ContainingType(map(MDS->getContainingType()));
     438          24 :     auto *Unit = cast_or_null<DICompileUnit>(map(MDS->getUnit()));
     439           8 :     auto Variables = nullptr;
     440           8 :     auto TemplateParams = nullptr;
     441             : 
     442             :     // Make a distinct DISubprogram, for situations that warrent it.
     443           6 :     auto distinctMDSubprogram = [&]() {
     444          60 :       return DISubprogram::getDistinct(
     445          30 :           MDS->getContext(), FileAndScope, MDS->getName(), LinkageName,
     446           6 :           FileAndScope, MDS->getLine(), Type, MDS->isLocalToUnit(),
     447           6 :           MDS->isDefinition(), MDS->getScopeLine(), ContainingType,
     448             :           MDS->getVirtuality(), MDS->getVirtualIndex(),
     449          12 :           MDS->getThisAdjustment(), MDS->getFlags(), MDS->isOptimized(), Unit,
     450           6 :           TemplateParams, Declaration, Variables);
     451          14 :     };
     452             : 
     453           8 :     if (MDS->isDistinct())
     454           6 :       return distinctMDSubprogram();
     455             : 
     456          12 :     auto *NewMDS = DISubprogram::get(
     457             :         MDS->getContext(), FileAndScope, MDS->getName(), LinkageName,
     458             :         FileAndScope, MDS->getLine(), Type, MDS->isLocalToUnit(),
     459             :         MDS->isDefinition(), MDS->getScopeLine(), ContainingType,
     460             :         MDS->getVirtuality(), MDS->getVirtualIndex(), MDS->getThisAdjustment(),
     461             :         MDS->getFlags(), MDS->isOptimized(), Unit, TemplateParams, Declaration,
     462             :         Variables);
     463             : 
     464           2 :     StringRef OldLinkageName = MDS->getLinkageName();
     465             : 
     466             :     // See if we need to make a distinct one.
     467           2 :     auto OrigLinkage = NewToLinkageName.find(NewMDS);
     468           2 :     if (OrigLinkage != NewToLinkageName.end()) {
     469             :       if (OrigLinkage->second == OldLinkageName)
     470             :         // We're good.
     471             :         return NewMDS;
     472             : 
     473             :       // Otherwise, need to make a distinct one.
     474             :       // TODO: Query the map to see if we already have one.
     475           0 :       return distinctMDSubprogram();
     476             :     }
     477             : 
     478           4 :     NewToLinkageName.insert({NewMDS, MDS->getLinkageName()});
     479           2 :     return NewMDS;
     480             :   }
     481             : 
     482             :   /// Create a new compile unit, to replace the one given
     483          19 :   DICompileUnit *getReplacementCU(DICompileUnit *CU) {
     484             :     // Drop skeleton CUs.
     485          19 :     if (CU->getDWOId())
     486             :       return nullptr;
     487             : 
     488          18 :     auto *File = cast_or_null<DIFile>(map(CU->getFile()));
     489             :     MDTuple *EnumTypes = nullptr;
     490             :     MDTuple *RetainedTypes = nullptr;
     491             :     MDTuple *GlobalVariables = nullptr;
     492             :     MDTuple *ImportedEntities = nullptr;
     493         108 :     return DICompileUnit::getDistinct(
     494             :         CU->getContext(), CU->getSourceLanguage(), File, CU->getProducer(),
     495          18 :         CU->isOptimized(), CU->getFlags(), CU->getRuntimeVersion(),
     496             :         CU->getSplitDebugFilename(), DICompileUnit::LineTablesOnly, EnumTypes,
     497             :         RetainedTypes, GlobalVariables, ImportedEntities, CU->getMacros(),
     498          18 :         CU->getDWOId(), CU->getSplitDebugInlining(),
     499          36 :         CU->getDebugInfoForProfiling(), CU->getGnuPubnames());
     500             :   }
     501             : 
     502           0 :   DILocation *getReplacementMDLocation(DILocation *MLD) {
     503           0 :     auto *Scope = map(MLD->getScope());
     504           0 :     auto *InlinedAt = map(MLD->getInlinedAt());
     505           0 :     if (MLD->isDistinct())
     506           0 :       return DILocation::getDistinct(MLD->getContext(), MLD->getLine(),
     507           0 :                                      MLD->getColumn(), Scope, InlinedAt);
     508           0 :     return DILocation::get(MLD->getContext(), MLD->getLine(), MLD->getColumn(),
     509           0 :                            Scope, InlinedAt);
     510             :   }
     511             : 
     512             :   /// Create a new generic MDNode, to replace the one given
     513          31 :   MDNode *getReplacementMDNode(MDNode *N) {
     514             :     SmallVector<Metadata *, 8> Ops;
     515          31 :     Ops.reserve(N->getNumOperands());
     516         173 :     for (auto &I : N->operands())
     517          71 :       if (I)
     518          67 :         Ops.push_back(map(I));
     519             :     auto *Ret = MDNode::get(N->getContext(), Ops);
     520          31 :     return Ret;
     521             :   }
     522             : 
     523             :   /// Attempt to re-map N to a newly created node.
     524         102 :   void remap(MDNode *N) {
     525         102 :     if (Replacements.count(N))
     526          13 :       return;
     527             : 
     528          89 :     auto doRemap = [&](MDNode *N) -> MDNode * {
     529          89 :       if (!N)
     530             :         return nullptr;
     531             :       if (auto *MDSub = dyn_cast<DISubprogram>(N)) {
     532          73 :         remap(MDSub->getUnit());
     533           8 :         return getReplacementSubprogram(MDSub);
     534             :       }
     535          80 :       if (isa<DISubroutineType>(N))
     536           7 :         return EmptySubroutineType;
     537             :       if (auto *CU = dyn_cast<DICompileUnit>(N))
     538          19 :         return getReplacementCU(CU);
     539          54 :       if (isa<DIFile>(N))
     540             :         return N;
     541             :       if (auto *MDLB = dyn_cast<DILexicalBlockBase>(N))
     542             :         // Remap to our referenced scope (recursively).
     543             :         return mapNode(MDLB->getScope());
     544             :       if (auto *MLD = dyn_cast<DILocation>(N))
     545           0 :         return getReplacementMDLocation(MLD);
     546             : 
     547             :       // Otherwise, if we see these, just drop them now. Not strictly necessary,
     548             :       // but this speeds things up a little.
     549             :       if (isa<DINode>(N))
     550             :         return nullptr;
     551             : 
     552          31 :       return getReplacementMDNode(N);
     553          89 :     };
     554         178 :     Replacements[N] = doRemap(N);
     555             :   }
     556             : 
     557             :   /// Do the remapping traversal.
     558             :   void traverse(MDNode *);
     559             : };
     560             : 
     561             : } // end anonymous namespace
     562             : 
     563          66 : void DebugTypeInfoRemoval::traverse(MDNode *N) {
     564          66 :   if (!N || Replacements.count(N))
     565          26 :     return;
     566             : 
     567             :   // To avoid cycles, as well as for efficiency sake, we will sometimes prune
     568             :   // parts of the graph.
     569             :   auto prune = [](MDNode *Parent, MDNode *Child) {
     570             :     if (auto *MDS = dyn_cast<DISubprogram>(Parent))
     571          29 :       return Child == MDS->getRetainedNodes().get();
     572             :     return false;
     573             :   };
     574             : 
     575             :   SmallVector<MDNode *, 16> ToVisit;
     576             :   DenseSet<MDNode *> Opened;
     577             : 
     578             :   // Visit each node starting at N in post order, and map them.
     579          40 :   ToVisit.push_back(N);
     580         216 :   while (!ToVisit.empty()) {
     581         176 :     auto *N = ToVisit.back();
     582         270 :     if (!Opened.insert(N).second) {
     583             :       // Close it.
     584          94 :       remap(N);
     585             :       ToVisit.pop_back();
     586          94 :       continue;
     587             :     }
     588         960 :     for (auto &I : N->operands())
     589         398 :       if (auto *MDN = dyn_cast_or_null<MDNode>(I))
     590         231 :         if (!Opened.count(MDN) && !Replacements.count(MDN) && !prune(N, MDN) &&
     591             :             !isa<DICompileUnit>(MDN))
     592          54 :           ToVisit.push_back(MDN);
     593             :   }
     594             : }
     595             : 
     596          13 : bool llvm::stripNonLineTableDebugInfo(Module &M) {
     597          13 :   bool Changed = false;
     598             : 
     599             :   // First off, delete the debug intrinsics.
     600          26 :   auto RemoveUses = [&](StringRef Name) {
     601          26 :     if (auto *DbgVal = M.getFunction(Name)) {
     602           9 :       while (!DbgVal->use_empty())
     603           3 :         cast<Instruction>(DbgVal->user_back())->eraseFromParent();
     604           3 :       DbgVal->eraseFromParent();
     605           3 :       Changed = true;
     606             :     }
     607          39 :   };
     608          13 :   RemoveUses("llvm.dbg.declare");
     609          13 :   RemoveUses("llvm.dbg.value");
     610             : 
     611             :   // Delete non-CU debug info named metadata nodes.
     612             :   for (auto NMI = M.named_metadata_begin(), NME = M.named_metadata_end();
     613          35 :        NMI != NME;) {
     614             :     NamedMDNode *NMD = &*NMI;
     615             :     ++NMI;
     616             :     // Specifically keep dbg.cu around.
     617          22 :     if (NMD->getName() == "llvm.dbg.cu")
     618             :       continue;
     619             :   }
     620             : 
     621             :   // Drop all dbg attachments from global variables.
     622          15 :   for (auto &GV : M.globals())
     623           2 :     GV.eraseMetadata(LLVMContext::MD_dbg);
     624             : 
     625          13 :   DebugTypeInfoRemoval Mapper(M.getContext());
     626          76 :   auto remap = [&](MDNode *Node) -> MDNode * {
     627          76 :     if (!Node)
     628             :       return nullptr;
     629         122 :     Mapper.traverseAndRemap(Node);
     630             :     auto *NewNode = Mapper.mapNode(Node);
     631          61 :     Changed |= Node != NewNode;
     632             :     Node = NewNode;
     633          61 :     return NewNode;
     634          13 :   };
     635             : 
     636             :   // Rewrite the DebugLocs to be equivalent to what
     637             :   // -gline-tables-only would have created.
     638          32 :   for (auto &F : M) {
     639          19 :     if (auto *SP = F.getSubprogram()) {
     640             :       Mapper.traverseAndRemap(SP);
     641             :       auto *NewSP = cast<DISubprogram>(Mapper.mapNode(SP));
     642           5 :       Changed |= SP != NewSP;
     643           5 :       F.setSubprogram(NewSP);
     644             :     }
     645          34 :     for (auto &BB : F) {
     646          45 :       for (auto &I : BB) {
     647          15 :         auto remapDebugLoc = [&](DebugLoc DL) -> DebugLoc {
     648          15 :           auto *Scope = DL.getScope();
     649          15 :           MDNode *InlinedAt = DL.getInlinedAt();
     650          15 :           Scope = remap(Scope);
     651          15 :           InlinedAt = remap(InlinedAt);
     652          15 :           return DebugLoc::get(DL.getLine(), DL.getCol(), Scope, InlinedAt);
     653          30 :         };
     654             : 
     655          30 :         if (I.getDebugLoc() != DebugLoc())
     656          39 :           I.setDebugLoc(remapDebugLoc(I.getDebugLoc()));
     657             : 
     658             :         // Remap DILocations in untyped MDNodes (e.g., llvm.loop).
     659             :         SmallVector<std::pair<unsigned, MDNode *>, 2> MDs;
     660             :         I.getAllMetadata(MDs);
     661          66 :         for (auto Attachment : MDs)
     662             :           if (auto *T = dyn_cast_or_null<MDTuple>(Attachment.second))
     663          27 :             for (unsigned N = 0; N < T->getNumOperands(); ++N)
     664          11 :               if (auto *Loc = dyn_cast_or_null<DILocation>(T->getOperand(N)))
     665           4 :                 if (Loc != DebugLoc())
     666           8 :                   T->replaceOperandWith(N, remapDebugLoc(Loc));
     667             :       }
     668             :     }
     669             :   }
     670             : 
     671             :   // Create a new llvm.dbg.cu, which is equivalent to the one
     672             :   // -gline-tables-only would have created.
     673          35 :   for (auto &NMD : M.getNamedMDList()) {
     674             :     SmallVector<MDNode *, 8> Ops;
     675          68 :     for (MDNode *Op : NMD.operands())
     676          46 :       Ops.push_back(remap(Op));
     677             :  
     678          22 :     if (!Changed)
     679             :       continue;
     680             :  
     681          21 :     NMD.clearOperands();
     682         111 :     for (auto *Op : Ops)
     683          45 :       if (Op)
     684          44 :         NMD.addOperand(Op);
     685             :   }
     686          26 :   return Changed;
     687             : }
     688             : 
     689       35615 : unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
     690             :   if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
     691       69283 :           M.getModuleFlag("Debug Info Version")))
     692        1946 :     return Val->getZExtValue();
     693             :   return 0;
     694             : }
     695             : 
     696       44685 : void Instruction::applyMergedLocation(const DILocation *LocA,
     697             :                                       const DILocation *LocB) {
     698       89370 :   setDebugLoc(DILocation::getMergedLocation(LocA, LocB,
     699             :                                             DILocation::WithGeneratedLocation));
     700       44685 : }
     701             : 
     702             : //===----------------------------------------------------------------------===//
     703             : // LLVM C API implementations.
     704             : //===----------------------------------------------------------------------===//
     705             : 
     706           1 : static unsigned map_from_llvmDWARFsourcelanguage(LLVMDWARFSourceLanguage lang) {
     707           1 :   switch (lang) {
     708             : #define HANDLE_DW_LANG(ID, NAME, VERSION, VENDOR) \
     709             : case LLVMDWARFSourceLanguage##NAME: return ID;
     710             : #include "llvm/BinaryFormat/Dwarf.def"
     711             : #undef HANDLE_DW_LANG
     712             :   }
     713           0 :   llvm_unreachable("Unhandled Tag");
     714             : }
     715             : 
     716             : template <typename DIT> DIT *unwrapDI(LLVMMetadataRef Ref) {
     717          20 :   return (DIT *)(Ref ? unwrap<MDNode>(Ref) : nullptr);
     718             : }
     719             : 
     720             : static DINode::DIFlags map_from_llvmDIFlags(LLVMDIFlags Flags) {
     721             :   return static_cast<DINode::DIFlags>(Flags);
     722             : }
     723             : 
     724             : static LLVMDIFlags map_to_llvmDIFlags(DINode::DIFlags Flags) {
     725             :   return static_cast<LLVMDIFlags>(Flags);
     726             : }
     727             : 
     728           0 : unsigned LLVMDebugMetadataVersion() {
     729           0 :   return DEBUG_METADATA_VERSION;
     730             : }
     731             : 
     732           0 : LLVMDIBuilderRef LLVMCreateDIBuilderDisallowUnresolved(LLVMModuleRef M) {
     733           0 :   return wrap(new DIBuilder(*unwrap(M), false));
     734             : }
     735             : 
     736           1 : LLVMDIBuilderRef LLVMCreateDIBuilder(LLVMModuleRef M) {
     737           1 :   return wrap(new DIBuilder(*unwrap(M)));
     738             : }
     739             : 
     740           0 : unsigned LLVMGetModuleDebugMetadataVersion(LLVMModuleRef M) {
     741           0 :   return getDebugMetadataVersionFromModule(*unwrap(M));
     742             : }
     743             : 
     744           0 : LLVMBool LLVMStripModuleDebugInfo(LLVMModuleRef M) {
     745           0 :   return StripDebugInfo(*unwrap(M));
     746             : }
     747             : 
     748           1 : void LLVMDisposeDIBuilder(LLVMDIBuilderRef Builder) {
     749           1 :   delete unwrap(Builder);
     750           1 : }
     751             : 
     752           1 : void LLVMDIBuilderFinalize(LLVMDIBuilderRef Builder) {
     753           1 :   unwrap(Builder)->finalize();
     754           1 : }
     755             : 
     756           1 : LLVMMetadataRef LLVMDIBuilderCreateCompileUnit(
     757             :     LLVMDIBuilderRef Builder, LLVMDWARFSourceLanguage Lang,
     758             :     LLVMMetadataRef FileRef, const char *Producer, size_t ProducerLen,
     759             :     LLVMBool isOptimized, const char *Flags, size_t FlagsLen,
     760             :     unsigned RuntimeVer, const char *SplitName, size_t SplitNameLen,
     761             :     LLVMDWARFEmissionKind Kind, unsigned DWOId, LLVMBool SplitDebugInlining,
     762             :     LLVMBool DebugInfoForProfiling) {
     763             :   auto File = unwrapDI<DIFile>(FileRef);
     764             : 
     765           3 :   return wrap(unwrap(Builder)->createCompileUnit(
     766             :                  map_from_llvmDWARFsourcelanguage(Lang), File,
     767             :                  StringRef(Producer, ProducerLen), isOptimized,
     768             :                  StringRef(Flags, FlagsLen), RuntimeVer,
     769             :                  StringRef(SplitName, SplitNameLen),
     770             :                  static_cast<DICompileUnit::DebugEmissionKind>(Kind), DWOId,
     771           1 :                  SplitDebugInlining, DebugInfoForProfiling));
     772             : }
     773             : 
     774             : LLVMMetadataRef
     775           1 : LLVMDIBuilderCreateFile(LLVMDIBuilderRef Builder, const char *Filename,
     776             :                         size_t FilenameLen, const char *Directory,
     777             :                         size_t DirectoryLen) {
     778           1 :   return wrap(unwrap(Builder)->createFile(StringRef(Filename, FilenameLen),
     779           1 :                                           StringRef(Directory, DirectoryLen)));
     780             : }
     781             : 
     782             : LLVMMetadataRef
     783           2 : LLVMDIBuilderCreateModule(LLVMDIBuilderRef Builder, LLVMMetadataRef ParentScope,
     784             :                           const char *Name, size_t NameLen,
     785             :                           const char *ConfigMacros, size_t ConfigMacrosLen,
     786             :                           const char *IncludePath, size_t IncludePathLen,
     787             :                           const char *ISysRoot, size_t ISysRootLen) {
     788           2 :   return wrap(unwrap(Builder)->createModule(
     789             :       unwrapDI<DIScope>(ParentScope), StringRef(Name, NameLen),
     790             :       StringRef(ConfigMacros, ConfigMacrosLen),
     791             :       StringRef(IncludePath, IncludePathLen),
     792           2 :       StringRef(ISysRoot, ISysRootLen)));
     793             : }
     794             : 
     795           1 : LLVMMetadataRef LLVMDIBuilderCreateNameSpace(LLVMDIBuilderRef Builder,
     796             :                                              LLVMMetadataRef ParentScope,
     797             :                                              const char *Name, size_t NameLen,
     798             :                                              LLVMBool ExportSymbols) {
     799           2 :   return wrap(unwrap(Builder)->createNameSpace(
     800           1 :       unwrapDI<DIScope>(ParentScope), StringRef(Name, NameLen), ExportSymbols));
     801             : }
     802             : 
     803           1 : LLVMMetadataRef LLVMDIBuilderCreateFunction(
     804             :     LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
     805             :     size_t NameLen, const char *LinkageName, size_t LinkageNameLen,
     806             :     LLVMMetadataRef File, unsigned LineNo, LLVMMetadataRef Ty,
     807             :     LLVMBool IsLocalToUnit, LLVMBool IsDefinition,
     808             :     unsigned ScopeLine, LLVMDIFlags Flags, LLVMBool IsOptimized) {
     809           2 :   return wrap(unwrap(Builder)->createFunction(
     810             :       unwrapDI<DIScope>(Scope), {Name, NameLen}, {LinkageName, LinkageNameLen},
     811             :       unwrapDI<DIFile>(File), LineNo, unwrapDI<DISubroutineType>(Ty),
     812             :       IsLocalToUnit, IsDefinition, ScopeLine, map_from_llvmDIFlags(Flags),
     813           1 :       IsOptimized, nullptr, nullptr, nullptr));
     814             : }
     815             : 
     816             : 
     817           1 : LLVMMetadataRef LLVMDIBuilderCreateLexicalBlock(
     818             :     LLVMDIBuilderRef Builder, LLVMMetadataRef Scope,
     819             :     LLVMMetadataRef File, unsigned Line, unsigned Col) {
     820           1 :   return wrap(unwrap(Builder)->createLexicalBlock(unwrapDI<DIScope>(Scope),
     821             :                                                   unwrapDI<DIFile>(File),
     822           1 :                                                   Line, Col));
     823             : }
     824             : 
     825             : LLVMMetadataRef
     826           0 : LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef Builder,
     827             :                                     LLVMMetadataRef Scope,
     828             :                                     LLVMMetadataRef File,
     829             :                                     unsigned Discriminator) {
     830           0 :   return wrap(unwrap(Builder)->createLexicalBlockFile(unwrapDI<DIScope>(Scope),
     831             :                                                       unwrapDI<DIFile>(File),
     832           0 :                                                       Discriminator));
     833             : }
     834             : 
     835             : LLVMMetadataRef
     836           0 : LLVMDIBuilderCreateImportedModuleFromNamespace(LLVMDIBuilderRef Builder,
     837             :                                                LLVMMetadataRef Scope,
     838             :                                                LLVMMetadataRef NS,
     839             :                                                LLVMMetadataRef File,
     840             :                                                unsigned Line) {
     841           0 :   return wrap(unwrap(Builder)->createImportedModule(unwrapDI<DIScope>(Scope),
     842             :                                                     unwrapDI<DINamespace>(NS),
     843             :                                                     unwrapDI<DIFile>(File),
     844           0 :                                                     Line));
     845             : }
     846             : 
     847             : LLVMMetadataRef
     848           1 : LLVMDIBuilderCreateImportedModuleFromAlias(LLVMDIBuilderRef Builder,
     849             :                                            LLVMMetadataRef Scope,
     850             :                                            LLVMMetadataRef ImportedEntity,
     851             :                                            LLVMMetadataRef File,
     852             :                                            unsigned Line) {
     853           1 :   return wrap(unwrap(Builder)->createImportedModule(
     854             :                   unwrapDI<DIScope>(Scope),
     855             :                   unwrapDI<DIImportedEntity>(ImportedEntity),
     856           1 :                   unwrapDI<DIFile>(File), Line));
     857             : }
     858             : 
     859             : LLVMMetadataRef
     860           1 : LLVMDIBuilderCreateImportedModuleFromModule(LLVMDIBuilderRef Builder,
     861             :                                             LLVMMetadataRef Scope,
     862             :                                             LLVMMetadataRef M,
     863             :                                             LLVMMetadataRef File,
     864             :                                             unsigned Line) {
     865           1 :   return wrap(unwrap(Builder)->createImportedModule(unwrapDI<DIScope>(Scope),
     866             :                                                     unwrapDI<DIModule>(M),
     867             :                                                     unwrapDI<DIFile>(File),
     868           1 :                                                     Line));
     869             : }
     870             : 
     871             : LLVMMetadataRef
     872           0 : LLVMDIBuilderCreateImportedDeclaration(LLVMDIBuilderRef Builder,
     873             :                                        LLVMMetadataRef Scope,
     874             :                                        LLVMMetadataRef Decl,
     875             :                                        LLVMMetadataRef File,
     876             :                                        unsigned Line,
     877             :                                        const char *Name, size_t NameLen) {
     878           0 :   return wrap(unwrap(Builder)->createImportedDeclaration(
     879             :                   unwrapDI<DIScope>(Scope),
     880             :                   unwrapDI<DINode>(Decl),
     881           0 :                   unwrapDI<DIFile>(File), Line, {Name, NameLen}));
     882             : }
     883             : 
     884             : LLVMMetadataRef
     885           2 : LLVMDIBuilderCreateDebugLocation(LLVMContextRef Ctx, unsigned Line,
     886             :                                  unsigned Column, LLVMMetadataRef Scope,
     887             :                                  LLVMMetadataRef InlinedAt) {
     888             :   return wrap(DILocation::get(*unwrap(Ctx), Line, Column, unwrap(Scope),
     889           2 :                               unwrap(InlinedAt)));
     890             : }
     891             : 
     892           0 : unsigned LLVMDILocationGetLine(LLVMMetadataRef Location) {
     893           0 :   return unwrapDI<DILocation>(Location)->getLine();
     894             : }
     895             : 
     896           0 : unsigned LLVMDILocationGetColumn(LLVMMetadataRef Location) {
     897           0 :   return unwrapDI<DILocation>(Location)->getColumn();
     898             : }
     899             : 
     900           0 : LLVMMetadataRef LLVMDILocationGetScope(LLVMMetadataRef Location) {
     901           0 :   return wrap(unwrapDI<DILocation>(Location)->getScope());
     902             : }
     903             : 
     904           0 : LLVMMetadataRef LLVMDIBuilderCreateEnumerationType(
     905             :   LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
     906             :   size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
     907             :   uint64_t SizeInBits, uint32_t AlignInBits, LLVMMetadataRef *Elements,
     908             :   unsigned NumElements, LLVMMetadataRef ClassTy) {
     909             : auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements),
     910           0 :                                                NumElements});
     911           0 : return wrap(unwrap(Builder)->createEnumerationType(
     912             :     unwrapDI<DIScope>(Scope), {Name, NameLen}, unwrapDI<DIFile>(File),
     913           0 :     LineNumber, SizeInBits, AlignInBits, Elts, unwrapDI<DIType>(ClassTy)));
     914             : }
     915             : 
     916           0 : LLVMMetadataRef LLVMDIBuilderCreateUnionType(
     917             :   LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
     918             :   size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
     919             :   uint64_t SizeInBits, uint32_t AlignInBits, LLVMDIFlags Flags,
     920             :   LLVMMetadataRef *Elements, unsigned NumElements, unsigned RunTimeLang,
     921             :   const char *UniqueId, size_t UniqueIdLen) {
     922             :   auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements),
     923           0 :                                                  NumElements});
     924           0 :   return wrap(unwrap(Builder)->createUnionType(
     925             :      unwrapDI<DIScope>(Scope), {Name, NameLen}, unwrapDI<DIFile>(File),
     926             :      LineNumber, SizeInBits, AlignInBits, map_from_llvmDIFlags(Flags),
     927           0 :      Elts, RunTimeLang, {UniqueId, UniqueIdLen}));
     928             : }
     929             : 
     930             : 
     931             : LLVMMetadataRef
     932           0 : LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef Builder, uint64_t Size,
     933             :                              uint32_t AlignInBits, LLVMMetadataRef Ty,
     934             :                              LLVMMetadataRef *Subscripts,
     935             :                              unsigned NumSubscripts) {
     936             :   auto Subs = unwrap(Builder)->getOrCreateArray({unwrap(Subscripts),
     937           0 :                                                  NumSubscripts});
     938           0 :   return wrap(unwrap(Builder)->createArrayType(Size, AlignInBits,
     939           0 :                                                unwrapDI<DIType>(Ty), Subs));
     940             : }
     941             : 
     942             : LLVMMetadataRef
     943           1 : LLVMDIBuilderCreateVectorType(LLVMDIBuilderRef Builder, uint64_t Size,
     944             :                               uint32_t AlignInBits, LLVMMetadataRef Ty,
     945             :                               LLVMMetadataRef *Subscripts,
     946             :                               unsigned NumSubscripts) {
     947             :   auto Subs = unwrap(Builder)->getOrCreateArray({unwrap(Subscripts),
     948           2 :                                                  NumSubscripts});
     949           1 :   return wrap(unwrap(Builder)->createVectorType(Size, AlignInBits,
     950           1 :                                                 unwrapDI<DIType>(Ty), Subs));
     951             : }
     952             : 
     953             : LLVMMetadataRef
     954           1 : LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef Builder, const char *Name,
     955             :                              size_t NameLen, uint64_t SizeInBits,
     956             :                              LLVMDWARFTypeEncoding Encoding) {
     957           1 :   return wrap(unwrap(Builder)->createBasicType({Name, NameLen},
     958           1 :                                                SizeInBits, Encoding));
     959             : }
     960             : 
     961           1 : LLVMMetadataRef LLVMDIBuilderCreatePointerType(
     962             :     LLVMDIBuilderRef Builder, LLVMMetadataRef PointeeTy,
     963             :     uint64_t SizeInBits, uint32_t AlignInBits, unsigned AddressSpace,
     964             :     const char *Name, size_t NameLen) {
     965           1 :   return wrap(unwrap(Builder)->createPointerType(unwrapDI<DIType>(PointeeTy),
     966             :                                          SizeInBits, AlignInBits,
     967           1 :                                          AddressSpace, {Name, NameLen}));
     968             : }
     969             : 
     970           1 : LLVMMetadataRef LLVMDIBuilderCreateStructType(
     971             :     LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
     972             :     size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
     973             :     uint64_t SizeInBits, uint32_t AlignInBits, LLVMDIFlags Flags,
     974             :     LLVMMetadataRef DerivedFrom, LLVMMetadataRef *Elements,
     975             :     unsigned NumElements, unsigned RunTimeLang, LLVMMetadataRef VTableHolder,
     976             :     const char *UniqueId, size_t UniqueIdLen) {
     977             :   auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements),
     978           2 :                                                  NumElements});
     979           1 :   return wrap(unwrap(Builder)->createStructType(
     980             :       unwrapDI<DIScope>(Scope), {Name, NameLen}, unwrapDI<DIFile>(File),
     981             :       LineNumber, SizeInBits, AlignInBits, map_from_llvmDIFlags(Flags),
     982             :       unwrapDI<DIType>(DerivedFrom), Elts, RunTimeLang,
     983           1 :       unwrapDI<DIType>(VTableHolder), {UniqueId, UniqueIdLen}));
     984             : }
     985             : 
     986           0 : LLVMMetadataRef LLVMDIBuilderCreateMemberType(
     987             :     LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
     988             :     size_t NameLen, LLVMMetadataRef File, unsigned LineNo, uint64_t SizeInBits,
     989             :     uint32_t AlignInBits, uint64_t OffsetInBits, LLVMDIFlags Flags,
     990             :     LLVMMetadataRef Ty) {
     991           0 :   return wrap(unwrap(Builder)->createMemberType(unwrapDI<DIScope>(Scope),
     992             :       {Name, NameLen}, unwrapDI<DIFile>(File), LineNo, SizeInBits, AlignInBits,
     993           0 :       OffsetInBits, map_from_llvmDIFlags(Flags), unwrapDI<DIType>(Ty)));
     994             : }
     995             : 
     996             : LLVMMetadataRef
     997           0 : LLVMDIBuilderCreateUnspecifiedType(LLVMDIBuilderRef Builder, const char *Name,
     998             :                                    size_t NameLen) {
     999           0 :   return wrap(unwrap(Builder)->createUnspecifiedType({Name, NameLen}));
    1000             : }
    1001             : 
    1002             : LLVMMetadataRef
    1003           0 : LLVMDIBuilderCreateStaticMemberType(
    1004             :     LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
    1005             :     size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
    1006             :     LLVMMetadataRef Type, LLVMDIFlags Flags, LLVMValueRef ConstantVal,
    1007             :     uint32_t AlignInBits) {
    1008           0 :   return wrap(unwrap(Builder)->createStaticMemberType(
    1009             :                   unwrapDI<DIScope>(Scope), {Name, NameLen},
    1010             :                   unwrapDI<DIFile>(File), LineNumber, unwrapDI<DIType>(Type),
    1011             :                   map_from_llvmDIFlags(Flags), unwrap<Constant>(ConstantVal),
    1012           0 :                   AlignInBits));
    1013             : }
    1014             : 
    1015             : LLVMMetadataRef
    1016           0 : LLVMDIBuilderCreateObjectPointerType(LLVMDIBuilderRef Builder,
    1017             :                                      LLVMMetadataRef Type) {
    1018           0 :   return wrap(unwrap(Builder)->createObjectPointerType(unwrapDI<DIType>(Type)));
    1019             : }
    1020             : 
    1021             : LLVMMetadataRef
    1022           1 : LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef Builder, LLVMMetadataRef Type,
    1023             :                            const char *Name, size_t NameLen,
    1024             :                            LLVMMetadataRef File, unsigned LineNo,
    1025             :                            LLVMMetadataRef Scope) {
    1026           1 :   return wrap(unwrap(Builder)->createTypedef(
    1027             :                   unwrapDI<DIType>(Type), {Name, NameLen},
    1028             :                   unwrapDI<DIFile>(File), LineNo,
    1029           1 :                   unwrapDI<DIScope>(Scope)));
    1030             : }
    1031             : 
    1032             : LLVMMetadataRef
    1033           0 : LLVMDIBuilderCreateForwardDecl(
    1034             :     LLVMDIBuilderRef Builder, unsigned Tag, const char *Name,
    1035             :     size_t NameLen, LLVMMetadataRef Scope, LLVMMetadataRef File, unsigned Line,
    1036             :     unsigned RuntimeLang, uint64_t SizeInBits, uint32_t AlignInBits,
    1037             :     const char *UniqueIdentifier, size_t UniqueIdentifierLen) {
    1038           0 :   return wrap(unwrap(Builder)->createForwardDecl(
    1039             :                   Tag, {Name, NameLen}, unwrapDI<DIScope>(Scope),
    1040             :                   unwrapDI<DIFile>(File), Line, RuntimeLang, SizeInBits,
    1041           0 :                   AlignInBits, {UniqueIdentifier, UniqueIdentifierLen}));
    1042             : }
    1043             : 
    1044             : LLVMMetadataRef
    1045           1 : LLVMDIBuilderCreateReplaceableCompositeType(
    1046             :     LLVMDIBuilderRef Builder, unsigned Tag, const char *Name,
    1047             :     size_t NameLen, LLVMMetadataRef Scope, LLVMMetadataRef File, unsigned Line,
    1048             :     unsigned RuntimeLang, uint64_t SizeInBits, uint32_t AlignInBits,
    1049             :     LLVMDIFlags Flags, const char *UniqueIdentifier,
    1050             :     size_t UniqueIdentifierLen) {
    1051           1 :   return wrap(unwrap(Builder)->createReplaceableCompositeType(
    1052             :                   Tag, {Name, NameLen}, unwrapDI<DIScope>(Scope),
    1053             :                   unwrapDI<DIFile>(File), Line, RuntimeLang, SizeInBits,
    1054             :                   AlignInBits, map_from_llvmDIFlags(Flags),
    1055           1 :                   {UniqueIdentifier, UniqueIdentifierLen}));
    1056             : }
    1057             : 
    1058             : LLVMMetadataRef
    1059           0 : LLVMDIBuilderCreateQualifiedType(LLVMDIBuilderRef Builder, unsigned Tag,
    1060             :                                  LLVMMetadataRef Type) {
    1061           0 :   return wrap(unwrap(Builder)->createQualifiedType(Tag,
    1062           0 :                                                    unwrapDI<DIType>(Type)));
    1063             : }
    1064             : 
    1065             : LLVMMetadataRef
    1066           0 : LLVMDIBuilderCreateReferenceType(LLVMDIBuilderRef Builder, unsigned Tag,
    1067             :                                  LLVMMetadataRef Type) {
    1068           0 :   return wrap(unwrap(Builder)->createReferenceType(Tag,
    1069           0 :                                                    unwrapDI<DIType>(Type)));
    1070             : }
    1071             : 
    1072             : LLVMMetadataRef
    1073           0 : LLVMDIBuilderCreateNullPtrType(LLVMDIBuilderRef Builder) {
    1074           0 :   return wrap(unwrap(Builder)->createNullPtrType());
    1075             : }
    1076             : 
    1077             : LLVMMetadataRef
    1078           0 : LLVMDIBuilderCreateMemberPointerType(LLVMDIBuilderRef Builder,
    1079             :                                      LLVMMetadataRef PointeeType,
    1080             :                                      LLVMMetadataRef ClassType,
    1081             :                                      uint64_t SizeInBits,
    1082             :                                      uint32_t AlignInBits,
    1083             :                                      LLVMDIFlags Flags) {
    1084           0 :   return wrap(unwrap(Builder)->createMemberPointerType(
    1085             :                   unwrapDI<DIType>(PointeeType),
    1086             :                   unwrapDI<DIType>(ClassType), AlignInBits, SizeInBits,
    1087           0 :                   map_from_llvmDIFlags(Flags)));
    1088             : }
    1089             : 
    1090             : LLVMMetadataRef
    1091           0 : LLVMDIBuilderCreateBitFieldMemberType(LLVMDIBuilderRef Builder,
    1092             :                                       LLVMMetadataRef Scope,
    1093             :                                       const char *Name, size_t NameLen,
    1094             :                                       LLVMMetadataRef File, unsigned LineNumber,
    1095             :                                       uint64_t SizeInBits,
    1096             :                                       uint64_t OffsetInBits,
    1097             :                                       uint64_t StorageOffsetInBits,
    1098             :                                       LLVMDIFlags Flags, LLVMMetadataRef Type) {
    1099           0 :   return wrap(unwrap(Builder)->createBitFieldMemberType(
    1100             :                   unwrapDI<DIScope>(Scope), {Name, NameLen},
    1101             :                   unwrapDI<DIFile>(File), LineNumber,
    1102             :                   SizeInBits, OffsetInBits, StorageOffsetInBits,
    1103           0 :                   map_from_llvmDIFlags(Flags), unwrapDI<DIType>(Type)));
    1104             : }
    1105             : 
    1106           0 : LLVMMetadataRef LLVMDIBuilderCreateClassType(LLVMDIBuilderRef Builder,
    1107             :     LLVMMetadataRef Scope, const char *Name, size_t NameLen,
    1108             :     LLVMMetadataRef File, unsigned LineNumber, uint64_t SizeInBits,
    1109             :     uint32_t AlignInBits, uint64_t OffsetInBits, LLVMDIFlags Flags,
    1110             :     LLVMMetadataRef DerivedFrom,
    1111             :     LLVMMetadataRef *Elements, unsigned NumElements,
    1112             :     LLVMMetadataRef VTableHolder, LLVMMetadataRef TemplateParamsNode,
    1113             :     const char *UniqueIdentifier, size_t UniqueIdentifierLen) {
    1114             :   auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements),
    1115           0 :                                                  NumElements});
    1116           0 :   return wrap(unwrap(Builder)->createClassType(
    1117             :                   unwrapDI<DIScope>(Scope), {Name, NameLen},
    1118             :                   unwrapDI<DIFile>(File), LineNumber,
    1119             :                   SizeInBits, AlignInBits, OffsetInBits,
    1120             :                   map_from_llvmDIFlags(Flags), unwrapDI<DIType>(DerivedFrom),
    1121             :                   Elts, unwrapDI<DIType>(VTableHolder),
    1122             :                   unwrapDI<MDNode>(TemplateParamsNode),
    1123           0 :                   {UniqueIdentifier, UniqueIdentifierLen}));
    1124             : }
    1125             : 
    1126             : LLVMMetadataRef
    1127           0 : LLVMDIBuilderCreateArtificialType(LLVMDIBuilderRef Builder,
    1128             :                                   LLVMMetadataRef Type) {
    1129           0 :   return wrap(unwrap(Builder)->createArtificialType(unwrapDI<DIType>(Type)));
    1130             : }
    1131             : 
    1132           0 : const char *LLVMDITypeGetName(LLVMMetadataRef DType, size_t *Length) {
    1133             :   StringRef Str = unwrap<DIType>(DType)->getName();
    1134           0 :   *Length = Str.size();
    1135           0 :   return Str.data();
    1136             : }
    1137             : 
    1138           0 : uint64_t LLVMDITypeGetSizeInBits(LLVMMetadataRef DType) {
    1139           0 :   return unwrapDI<DIType>(DType)->getSizeInBits();
    1140             : }
    1141             : 
    1142           0 : uint64_t LLVMDITypeGetOffsetInBits(LLVMMetadataRef DType) {
    1143           0 :   return unwrapDI<DIType>(DType)->getOffsetInBits();
    1144             : }
    1145             : 
    1146           0 : uint32_t LLVMDITypeGetAlignInBits(LLVMMetadataRef DType) {
    1147           0 :   return unwrapDI<DIType>(DType)->getAlignInBits();
    1148             : }
    1149             : 
    1150           0 : unsigned LLVMDITypeGetLine(LLVMMetadataRef DType) {
    1151           0 :   return unwrapDI<DIType>(DType)->getLine();
    1152             : }
    1153             : 
    1154           0 : LLVMDIFlags LLVMDITypeGetFlags(LLVMMetadataRef DType) {
    1155           0 :   return map_to_llvmDIFlags(unwrapDI<DIType>(DType)->getFlags());
    1156             : }
    1157             : 
    1158           0 : LLVMMetadataRef LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef Builder,
    1159             :                                                   LLVMMetadataRef *Types,
    1160             :                                                   size_t Length) {
    1161             :   return wrap(
    1162           0 :       unwrap(Builder)->getOrCreateTypeArray({unwrap(Types), Length}).get());
    1163             : }
    1164             : 
    1165             : LLVMMetadataRef
    1166           1 : LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef Builder,
    1167             :                                   LLVMMetadataRef File,
    1168             :                                   LLVMMetadataRef *ParameterTypes,
    1169             :                                   unsigned NumParameterTypes,
    1170             :                                   LLVMDIFlags Flags) {
    1171             :   auto Elts = unwrap(Builder)->getOrCreateTypeArray({unwrap(ParameterTypes),
    1172           2 :                                                      NumParameterTypes});
    1173           1 :   return wrap(unwrap(Builder)->createSubroutineType(
    1174           1 :     Elts, map_from_llvmDIFlags(Flags)));
    1175             : }
    1176             : 
    1177           1 : LLVMMetadataRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Builder,
    1178             :                                               int64_t *Addr, size_t Length) {
    1179           1 :   return wrap(unwrap(Builder)->createExpression(ArrayRef<int64_t>(Addr,
    1180           1 :                                                                   Length)));
    1181             : }
    1182             : 
    1183             : LLVMMetadataRef
    1184           2 : LLVMDIBuilderCreateConstantValueExpression(LLVMDIBuilderRef Builder,
    1185             :                                            int64_t Value) {
    1186           4 :   return wrap(unwrap(Builder)->createConstantValueExpression(Value));
    1187             : }
    1188             : 
    1189             : LLVMMetadataRef
    1190           1 : LLVMDIBuilderCreateGlobalVariableExpression(LLVMDIBuilderRef Builder,
    1191             :                                             LLVMMetadataRef Scope,
    1192             :                                             const char *Name, size_t NameLen,
    1193             :                                             const char *Linkage, size_t LinkLen,
    1194             :                                             LLVMMetadataRef File,
    1195             :                                             unsigned LineNo,
    1196             :                                             LLVMMetadataRef Ty,
    1197             :                                             LLVMBool LocalToUnit,
    1198             :                                             LLVMMetadataRef Expr,
    1199             :                                             LLVMMetadataRef Decl,
    1200             :                                             uint32_t AlignInBits) {
    1201           2 :   return wrap(unwrap(Builder)->createGlobalVariableExpression(
    1202             :                   unwrapDI<DIScope>(Scope), {Name, NameLen}, {Linkage, LinkLen},
    1203             :                   unwrapDI<DIFile>(File), LineNo, unwrapDI<DIType>(Ty),
    1204             :                   LocalToUnit, unwrap<DIExpression>(Expr),
    1205           1 :                   unwrapDI<MDNode>(Decl), AlignInBits));
    1206             : }
    1207             : 
    1208           0 : LLVMMetadataRef LLVMTemporaryMDNode(LLVMContextRef Ctx, LLVMMetadataRef *Data,
    1209             :                                     size_t Count) {
    1210             :   return wrap(
    1211           0 :       MDTuple::getTemporary(*unwrap(Ctx), {unwrap(Data), Count}).release());
    1212             : }
    1213             : 
    1214           0 : void LLVMDisposeTemporaryMDNode(LLVMMetadataRef TempNode) {
    1215           0 :   MDNode::deleteTemporary(unwrapDI<MDNode>(TempNode));
    1216           0 : }
    1217             : 
    1218           1 : void LLVMMetadataReplaceAllUsesWith(LLVMMetadataRef TargetMetadata,
    1219             :                                     LLVMMetadataRef Replacement) {
    1220             :   auto *Node = unwrapDI<MDNode>(TargetMetadata);
    1221             :   Node->replaceAllUsesWith(unwrap<Metadata>(Replacement));
    1222           1 :   MDNode::deleteTemporary(Node);
    1223           1 : }
    1224             : 
    1225             : LLVMMetadataRef
    1226           0 : LLVMDIBuilderCreateTempGlobalVariableFwdDecl(LLVMDIBuilderRef Builder,
    1227             :                                              LLVMMetadataRef Scope,
    1228             :                                              const char *Name, size_t NameLen,
    1229             :                                              const char *Linkage, size_t LnkLen,
    1230             :                                              LLVMMetadataRef File,
    1231             :                                              unsigned LineNo,
    1232             :                                              LLVMMetadataRef Ty,
    1233             :                                              LLVMBool LocalToUnit,
    1234             :                                              LLVMMetadataRef Decl,
    1235             :                                              uint32_t AlignInBits) {
    1236           0 :   return wrap(unwrap(Builder)->createTempGlobalVariableFwdDecl(
    1237             :                   unwrapDI<DIScope>(Scope), {Name, NameLen}, {Linkage, LnkLen},
    1238             :                   unwrapDI<DIFile>(File), LineNo, unwrapDI<DIType>(Ty),
    1239           0 :                   LocalToUnit, unwrapDI<MDNode>(Decl), AlignInBits));
    1240             : }
    1241             : 
    1242           0 : LLVMValueRef LLVMDIBuilderInsertDeclareBefore(
    1243             :   LLVMDIBuilderRef Builder, LLVMValueRef Storage, LLVMMetadataRef VarInfo,
    1244             :   LLVMMetadataRef Expr, LLVMMetadataRef DL, LLVMValueRef Instr) {
    1245           0 :   return wrap(unwrap(Builder)->insertDeclare(
    1246             :                   unwrap(Storage), unwrap<DILocalVariable>(VarInfo),
    1247             :                   unwrap<DIExpression>(Expr), unwrap<DILocation>(DL),
    1248           0 :                   unwrap<Instruction>(Instr)));
    1249             : }
    1250             : 
    1251           3 : LLVMValueRef LLVMDIBuilderInsertDeclareAtEnd(
    1252             :     LLVMDIBuilderRef Builder, LLVMValueRef Storage, LLVMMetadataRef VarInfo,
    1253             :     LLVMMetadataRef Expr, LLVMMetadataRef DL, LLVMBasicBlockRef Block) {
    1254           3 :   return wrap(unwrap(Builder)->insertDeclare(
    1255             :                   unwrap(Storage), unwrap<DILocalVariable>(VarInfo),
    1256             :                   unwrap<DIExpression>(Expr), unwrap<DILocation>(DL),
    1257           3 :                   unwrap(Block)));
    1258             : }
    1259             : 
    1260           0 : LLVMValueRef LLVMDIBuilderInsertDbgValueBefore(LLVMDIBuilderRef Builder,
    1261             :                                                LLVMValueRef Val,
    1262             :                                                LLVMMetadataRef VarInfo,
    1263             :                                                LLVMMetadataRef Expr,
    1264             :                                                LLVMMetadataRef DebugLoc,
    1265             :                                                LLVMValueRef Instr) {
    1266           0 :   return wrap(unwrap(Builder)->insertDbgValueIntrinsic(
    1267             :                   unwrap(Val), unwrap<DILocalVariable>(VarInfo),
    1268             :                   unwrap<DIExpression>(Expr), unwrap<DILocation>(DebugLoc),
    1269           0 :                   unwrap<Instruction>(Instr)));
    1270             : }
    1271             : 
    1272           1 : LLVMValueRef LLVMDIBuilderInsertDbgValueAtEnd(LLVMDIBuilderRef Builder,
    1273             :                                               LLVMValueRef Val,
    1274             :                                               LLVMMetadataRef VarInfo,
    1275             :                                               LLVMMetadataRef Expr,
    1276             :                                               LLVMMetadataRef DebugLoc,
    1277             :                                               LLVMBasicBlockRef Block) {
    1278           1 :   return wrap(unwrap(Builder)->insertDbgValueIntrinsic(
    1279             :                   unwrap(Val), unwrap<DILocalVariable>(VarInfo),
    1280             :                   unwrap<DIExpression>(Expr), unwrap<DILocation>(DebugLoc),
    1281           1 :                   unwrap(Block)));
    1282             : }
    1283             : 
    1284           1 : LLVMMetadataRef LLVMDIBuilderCreateAutoVariable(
    1285             :     LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
    1286             :     size_t NameLen, LLVMMetadataRef File, unsigned LineNo, LLVMMetadataRef Ty,
    1287             :     LLVMBool AlwaysPreserve, LLVMDIFlags Flags, uint32_t AlignInBits) {
    1288           2 :   return wrap(unwrap(Builder)->createAutoVariable(
    1289             :                   unwrap<DIScope>(Scope), {Name, NameLen}, unwrap<DIFile>(File),
    1290             :                   LineNo, unwrap<DIType>(Ty), AlwaysPreserve,
    1291           1 :                   map_from_llvmDIFlags(Flags), AlignInBits));
    1292             : }
    1293             : 
    1294           3 : LLVMMetadataRef LLVMDIBuilderCreateParameterVariable(
    1295             :     LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
    1296             :     size_t NameLen, unsigned ArgNo, LLVMMetadataRef File, unsigned LineNo,
    1297             :     LLVMMetadataRef Ty, LLVMBool AlwaysPreserve, LLVMDIFlags Flags) {
    1298           6 :   return wrap(unwrap(Builder)->createParameterVariable(
    1299             :                   unwrap<DIScope>(Scope), Name, ArgNo, unwrap<DIFile>(File),
    1300             :                   LineNo, unwrap<DIType>(Ty), AlwaysPreserve,
    1301           3 :                   map_from_llvmDIFlags(Flags)));
    1302             : }
    1303             : 
    1304           1 : LLVMMetadataRef LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef Builder,
    1305             :                                                  int64_t Lo, int64_t Count) {
    1306           1 :   return wrap(unwrap(Builder)->getOrCreateSubrange(Lo, Count));
    1307             : }
    1308             : 
    1309           0 : LLVMMetadataRef LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef Builder,
    1310             :                                               LLVMMetadataRef *Data,
    1311             :                                               size_t Length) {
    1312             :   Metadata **DataValue = unwrap(Data);
    1313           0 :   return wrap(unwrap(Builder)->getOrCreateArray({DataValue, Length}).get());
    1314             : }
    1315             : 
    1316           0 : LLVMMetadataRef LLVMGetSubprogram(LLVMValueRef Func) {
    1317           0 :   return wrap(unwrap<Function>(Func)->getSubprogram());
    1318             : }
    1319             : 
    1320           1 : void LLVMSetSubprogram(LLVMValueRef Func, LLVMMetadataRef SP) {
    1321           1 :   unwrap<Function>(Func)->setSubprogram(unwrap<DISubprogram>(SP));
    1322           1 : }

Generated by: LCOV version 1.13