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 DISubprogram *llvm::getDISubprogram(const MDNode *Scope) { 45 if (auto *LocalScope = dyn_cast_or_null<DILocalScope>(Scope)) 46 return LocalScope->getSubprogram(); 47 return nullptr; 48 } 49 50 //===----------------------------------------------------------------------===// 51 // DebugInfoFinder implementations. 52 //===----------------------------------------------------------------------===// 53 54 void DebugInfoFinder::reset() { 55 CUs.clear(); 56 SPs.clear(); 57 GVs.clear(); 58 TYs.clear(); 59 Scopes.clear(); 60 NodesSeen.clear(); 61 } 62 63 void DebugInfoFinder::processModule(const Module &M) { 64 for (auto *CU : M.debug_compile_units()) 65 processCompileUnit(CU); 66 for (auto &F : M.functions()) { 67 if (auto *SP = cast_or_null<DISubprogram>(F.getSubprogram())) 68 processSubprogram(SP); 69 // There could be subprograms from inlined functions referenced from 70 // instructions only. Walk the function to find them. 71 for (const BasicBlock &BB : F) 72 for (const Instruction &I : BB) 73 processInstruction(M, I); 74 } 75 } 76 77 void DebugInfoFinder::processCompileUnit(DICompileUnit *CU) { 78 if (!addCompileUnit(CU)) 79 return; 80 for (auto DIG : CU->getGlobalVariables()) { 81 if (!addGlobalVariable(DIG)) 82 continue; 83 auto *GV = DIG->getVariable(); 84 processScope(GV->getScope()); 85 processType(GV->getType().resolve()); 86 } 87 for (auto *ET : CU->getEnumTypes()) 88 processType(ET); 89 for (auto *RT : CU->getRetainedTypes()) 90 if (auto *T = dyn_cast<DIType>(RT)) 91 processType(T); 92 else 93 processSubprogram(cast<DISubprogram>(RT)); 94 for (auto *Import : CU->getImportedEntities()) { 95 auto *Entity = Import->getEntity().resolve(); 96 if (auto *T = dyn_cast<DIType>(Entity)) 97 processType(T); 98 else if (auto *SP = dyn_cast<DISubprogram>(Entity)) 99 processSubprogram(SP); 100 else if (auto *NS = dyn_cast<DINamespace>(Entity)) 101 processScope(NS->getScope()); 102 else if (auto *M = dyn_cast<DIModule>(Entity)) 103 processScope(M->getScope()); 104 } 105 } 106 107 void DebugInfoFinder::processInstruction(const Module &M, 108 const Instruction &I) { 109 if (auto *DDI = dyn_cast<DbgDeclareInst>(&I)) 110 processDeclare(M, DDI); 111 else if (auto *DVI = dyn_cast<DbgValueInst>(&I)) 112 processValue(M, DVI); 113 114 if (auto DbgLoc = I.getDebugLoc()) 115 processLocation(M, DbgLoc.get()); 116 } 117 118 void DebugInfoFinder::processLocation(const Module &M, const DILocation *Loc) { 119 if (!Loc) 120 return; 121 processScope(Loc->getScope()); 122 processLocation(M, Loc->getInlinedAt()); 123 } 124 125 void DebugInfoFinder::processType(DIType *DT) { 126 if (!addType(DT)) 127 return; 128 processScope(DT->getScope().resolve()); 129 if (auto *ST = dyn_cast<DISubroutineType>(DT)) { 130 for (DITypeRef Ref : ST->getTypeArray()) 131 processType(Ref.resolve()); 132 return; 133 } 134 if (auto *DCT = dyn_cast<DICompositeType>(DT)) { 135 processType(DCT->getBaseType().resolve()); 136 for (Metadata *D : DCT->getElements()) { 137 if (auto *T = dyn_cast<DIType>(D)) 138 processType(T); 139 else if (auto *SP = dyn_cast<DISubprogram>(D)) 140 processSubprogram(SP); 141 } 142 return; 143 } 144 if (auto *DDT = dyn_cast<DIDerivedType>(DT)) { 145 processType(DDT->getBaseType().resolve()); 146 } 147 } 148 149 void DebugInfoFinder::processScope(DIScope *Scope) { 150 if (!Scope) 151 return; 152 if (auto *Ty = dyn_cast<DIType>(Scope)) { 153 processType(Ty); 154 return; 155 } 156 if (auto *CU = dyn_cast<DICompileUnit>(Scope)) { 157 addCompileUnit(CU); 158 return; 159 } 160 if (auto *SP = dyn_cast<DISubprogram>(Scope)) { 161 processSubprogram(SP); 162 return; 163 } 164 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 void DebugInfoFinder::processSubprogram(DISubprogram *SP) { 176 if (!addSubprogram(SP)) 177 return; 178 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 processCompileUnit(SP->getUnit()); 188 processType(SP->getType()); 189 for (auto *Element : SP->getTemplateParams()) { 190 if (auto *TType = dyn_cast<DITemplateTypeParameter>(Element)) { 191 processType(TType->getType().resolve()); 192 } else if (auto *TVal = dyn_cast<DITemplateValueParameter>(Element)) { 193 processType(TVal->getType().resolve()); 194 } 195 } 196 } 197 198 void DebugInfoFinder::processDeclare(const Module &M, 199 const DbgDeclareInst *DDI) { 200 auto *N = dyn_cast<MDNode>(DDI->getVariable()); 201 if (!N) 202 return; 203 204 auto *DV = dyn_cast<DILocalVariable>(N); 205 if (!DV) 206 return; 207 208 if (!NodesSeen.insert(DV).second) 209 return; 210 processScope(DV->getScope()); 211 processType(DV->getType().resolve()); 212 } 213 214 void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) { 215 auto *N = dyn_cast<MDNode>(DVI->getVariable()); 216 if (!N) 217 return; 218 219 auto *DV = dyn_cast<DILocalVariable>(N); 220 if (!DV) 221 return; 222 223 if (!NodesSeen.insert(DV).second) 224 return; 225 processScope(DV->getScope()); 226 processType(DV->getType().resolve()); 227 } 228 229 bool DebugInfoFinder::addType(DIType *DT) { 230 if (!DT) 231 return false; 232 233 if (!NodesSeen.insert(DT).second) 234 return false; 235 236 TYs.push_back(const_cast<DIType *>(DT)); 237 return true; 238 } 239 240 bool DebugInfoFinder::addCompileUnit(DICompileUnit *CU) { 241 if (!CU) 242 return false; 243 if (!NodesSeen.insert(CU).second) 244 return false; 245 246 CUs.push_back(CU); 247 return true; 248 } 249 250 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariableExpression *DIG) { 251 if (!NodesSeen.insert(DIG).second) 252 return false; 253 254 GVs.push_back(DIG); 255 return true; 256 } 257 258 bool DebugInfoFinder::addSubprogram(DISubprogram *SP) { 259 if (!SP) 260 return false; 261 262 if (!NodesSeen.insert(SP).second) 263 return false; 264 265 SPs.push_back(SP); 266 return true; 267 } 268 269 bool DebugInfoFinder::addScope(DIScope *Scope) { 270 if (!Scope) 271 return false; 272 // FIXME: Ocaml binding generates a scope with no content, we treat it 273 // as null for now. 274 if (Scope->getNumOperands() == 0) 275 return false; 276 if (!NodesSeen.insert(Scope).second) 277 return false; 278 Scopes.push_back(Scope); 279 return true; 280 } 281 282 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 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 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 Args.push_back(TempNode.get()); 302 // Add all non-debug location operands back. 303 for (auto Op = N->op_begin() + 1; Op != N->op_end(); Op++) { 304 if (!isa<DILocation>(*Op)) 305 Args.push_back(*Op); 306 } 307 308 // Set the first operand to itself. 309 MDNode *LoopID = MDNode::get(N->getContext(), Args); 310 LoopID->replaceOperandWith(0, LoopID); 311 return LoopID; 312 } 313 314 bool llvm::stripDebugInfo(Function &F) { 315 bool Changed = false; 316 if (F.getMetadata(LLVMContext::MD_dbg)) { 317 Changed = true; 318 F.setSubprogram(nullptr); 319 } 320 321 DenseMap<MDNode*, MDNode*> LoopIDsMap; 322 for (BasicBlock &BB : F) { 323 for (auto II = BB.begin(), End = BB.end(); II != End;) { 324 Instruction &I = *II++; // We may delete the instruction, increment now. 325 if (isa<DbgInfoIntrinsic>(&I)) { 326 I.eraseFromParent(); 327 Changed = true; 328 continue; 329 } 330 if (I.getDebugLoc()) { 331 Changed = true; 332 I.setDebugLoc(DebugLoc()); 333 } 334 } 335 336 auto *TermInst = BB.getTerminator(); 337 if (!TermInst) 338 // This is invalid IR, but we may not have run the verifier yet 339 continue; 340 if (auto *LoopID = TermInst->getMetadata(LLVMContext::MD_loop)) { 341 auto *NewLoopID = LoopIDsMap.lookup(LoopID); 342 if (!NewLoopID) 343 NewLoopID = LoopIDsMap[LoopID] = stripDebugLocFromLoopID(LoopID); 344 if (NewLoopID != LoopID) 345 TermInst->setMetadata(LLVMContext::MD_loop, NewLoopID); 346 } 347 } 348 return Changed; 349 } 350 351 bool llvm::StripDebugInfo(Module &M) { 352 bool Changed = false; 353 354 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(), 355 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 if (NMD->getName().startswith("llvm.dbg.") || 362 NMD->getName() == "llvm.gcov") { 363 NMD->eraseFromParent(); 364 Changed = true; 365 } 366 } 367 368 for (Function &F : M) 369 Changed |= stripDebugInfo(F); 370 371 for (auto &GV : M.globals()) { 372 SmallVector<MDNode *, 1> MDs; 373 GV.getMetadata(LLVMContext::MD_dbg, MDs); 374 if (!MDs.empty()) { 375 GV.eraseMetadata(LLVMContext::MD_dbg); 376 Changed = true; 377 } 378 } 379 380 if (GVMaterializer *Materializer = M.getMaterializer()) 381 Materializer->setStripDebugInfo(); 382 383 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 DebugTypeInfoRemoval(LLVMContext &C) 412 : EmptySubroutineType(DISubroutineType::get(C, DINode::FlagZero, 0, 413 MDNode::get(C, {}))) {} 414 415 Metadata *map(Metadata *M) { 416 if (!M) 417 return nullptr; 418 auto Replacement = Replacements.find(M); 419 if (Replacement != Replacements.end()) 420 return Replacement->second; 421 422 return M; 423 } 424 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 void traverseAndRemap(MDNode *N) { traverse(N); } 429 430 private: 431 // Create a new DISubprogram, to replace the one given. 432 DISubprogram *getReplacementSubprogram(DISubprogram *MDS) { 433 auto *FileAndScope = cast_or_null<DIFile>(map(MDS->getFile())); 434 StringRef LinkageName = MDS->getName().empty() ? MDS->getLinkageName() : ""; 435 DISubprogram *Declaration = nullptr; 436 auto *Type = cast_or_null<DISubroutineType>(map(MDS->getType())); 437 DITypeRef ContainingType(map(MDS->getContainingType())); 438 auto *Unit = cast_or_null<DICompileUnit>(map(MDS->getUnit())); 439 auto Variables = nullptr; 440 auto TemplateParams = nullptr; 441 442 // Make a distinct DISubprogram, for situations that warrent it. 443 auto distinctMDSubprogram = [&]() { 444 return DISubprogram::getDistinct( 445 MDS->getContext(), FileAndScope, MDS->getName(), LinkageName, 446 FileAndScope, MDS->getLine(), Type, MDS->isLocalToUnit(), 447 MDS->isDefinition(), MDS->getScopeLine(), ContainingType, 448 MDS->getVirtuality(), MDS->getVirtualIndex(), 449 MDS->getThisAdjustment(), MDS->getFlags(), MDS->isOptimized(), Unit, 450 TemplateParams, Declaration, Variables); 451 }; 452 453 if (MDS->isDistinct()) 454 return distinctMDSubprogram(); 455 456 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 StringRef OldLinkageName = MDS->getLinkageName(); 465 466 // See if we need to make a distinct one. 467 auto OrigLinkage = NewToLinkageName.find(NewMDS); 468 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 return distinctMDSubprogram(); 476 } 477 478 NewToLinkageName.insert({NewMDS, MDS->getLinkageName()}); 479 return NewMDS; 480 } 481 482 /// Create a new compile unit, to replace the one given 483 DICompileUnit *getReplacementCU(DICompileUnit *CU) { 484 // Drop skeleton CUs. 485 if (CU->getDWOId()) 486 return nullptr; 487 488 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 return DICompileUnit::getDistinct( 494 CU->getContext(), CU->getSourceLanguage(), File, CU->getProducer(), 495 CU->isOptimized(), CU->getFlags(), CU->getRuntimeVersion(), 496 CU->getSplitDebugFilename(), DICompileUnit::LineTablesOnly, EnumTypes, 497 RetainedTypes, GlobalVariables, ImportedEntities, CU->getMacros(), 498 CU->getDWOId(), CU->getSplitDebugInlining(), 499 CU->getDebugInfoForProfiling(), CU->getGnuPubnames()); 500 } 501 502 DILocation *getReplacementMDLocation(DILocation *MLD) { 503 auto *Scope = map(MLD->getScope()); 504 auto *InlinedAt = map(MLD->getInlinedAt()); 505 if (MLD->isDistinct()) 506 return DILocation::getDistinct(MLD->getContext(), MLD->getLine(), 507 MLD->getColumn(), Scope, InlinedAt); 508 return DILocation::get(MLD->getContext(), MLD->getLine(), MLD->getColumn(), 509 Scope, InlinedAt); 510 } 511 512 /// Create a new generic MDNode, to replace the one given 513 MDNode *getReplacementMDNode(MDNode *N) { 514 SmallVector<Metadata *, 8> Ops; 515 Ops.reserve(N->getNumOperands()); 516 for (auto &I : N->operands()) 517 if (I) 518 Ops.push_back(map(I)); 519 auto *Ret = MDNode::get(N->getContext(), Ops); 520 return Ret; 521 } 522 523 /// Attempt to re-map N to a newly created node. 524 void remap(MDNode *N) { 525 if (Replacements.count(N)) 526 return; 527 528 auto doRemap = [&](MDNode *N) -> MDNode * { 529 if (!N) 530 return nullptr; 531 if (auto *MDSub = dyn_cast<DISubprogram>(N)) { 532 remap(MDSub->getUnit()); 533 return getReplacementSubprogram(MDSub); 534 } 535 if (isa<DISubroutineType>(N)) 536 return EmptySubroutineType; 537 if (auto *CU = dyn_cast<DICompileUnit>(N)) 538 return getReplacementCU(CU); 539 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 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 return getReplacementMDNode(N); 553 }; 554 Replacements[N] = doRemap(N); 555 } 556 557 /// Do the remapping traversal. 558 void traverse(MDNode *); 559 }; 560 561 } // end anonymous namespace 562 563 void DebugTypeInfoRemoval::traverse(MDNode *N) { 564 if (!N || Replacements.count(N)) 565 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 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 ToVisit.push_back(N); 580 while (!ToVisit.empty()) { 581 auto *N = ToVisit.back(); 582 if (!Opened.insert(N).second) { 583 // Close it. 584 remap(N); 585 ToVisit.pop_back(); 586 continue; 587 } 588 for (auto &I : N->operands()) 589 if (auto *MDN = dyn_cast_or_null<MDNode>(I)) 590 if (!Opened.count(MDN) && !Replacements.count(MDN) && !prune(N, MDN) && 591 !isa<DICompileUnit>(MDN)) 592 ToVisit.push_back(MDN); 593 } 594 } 595 596 bool llvm::stripNonLineTableDebugInfo(Module &M) { 597 bool Changed = false; 598 599 // First off, delete the debug intrinsics. 600 auto RemoveUses = [&](StringRef Name) { 601 if (auto *DbgVal = M.getFunction(Name)) { 602 while (!DbgVal->use_empty()) 603 cast<Instruction>(DbgVal->user_back())->eraseFromParent(); 604 DbgVal->eraseFromParent(); 605 Changed = true; 606 } 607 }; 608 RemoveUses("llvm.dbg.declare"); 609 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 NMI != NME;) { 614 NamedMDNode *NMD = &*NMI; 615 ++NMI; 616 // Specifically keep dbg.cu around. 617 if (NMD->getName() == "llvm.dbg.cu") 618 continue; 619 } 620 621 // Drop all dbg attachments from global variables. 622 for (auto &GV : M.globals()) 623 GV.eraseMetadata(LLVMContext::MD_dbg); 624 625 DebugTypeInfoRemoval Mapper(M.getContext()); 626 auto remap = [&](MDNode *Node) -> MDNode * { 627 if (!Node) 628 return nullptr; 629 Mapper.traverseAndRemap(Node); 630 auto *NewNode = Mapper.mapNode(Node); 631 Changed |= Node != NewNode; 632 Node = NewNode; 633 return NewNode; 634 }; 635 636 // Rewrite the DebugLocs to be equivalent to what 637 // -gline-tables-only would have created. 638 for (auto &F : M) { 639 if (auto *SP = F.getSubprogram()) { 640 Mapper.traverseAndRemap(SP); 641 auto *NewSP = cast<DISubprogram>(Mapper.mapNode(SP)); 642 Changed |= SP != NewSP; 643 F.setSubprogram(NewSP); 644 } 645 for (auto &BB : F) { 646 for (auto &I : BB) { 647 auto remapDebugLoc = [&](DebugLoc DL) -> DebugLoc { 648 auto *Scope = DL.getScope(); 649 MDNode *InlinedAt = DL.getInlinedAt(); 650 Scope = remap(Scope); 651 InlinedAt = remap(InlinedAt); 652 return DebugLoc::get(DL.getLine(), DL.getCol(), Scope, InlinedAt); 653 }; 654 655 if (I.getDebugLoc() != DebugLoc()) 656 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 for (auto Attachment : MDs) 662 if (auto *T = dyn_cast_or_null<MDTuple>(Attachment.second)) 663 for (unsigned N = 0; N < T->getNumOperands(); ++N) 664 if (auto *Loc = dyn_cast_or_null<DILocation>(T->getOperand(N))) 665 if (Loc != DebugLoc()) 666 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 for (auto &NMD : M.getNamedMDList()) { 674 SmallVector<MDNode *, 8> Ops; 675 for (MDNode *Op : NMD.operands()) 676 Ops.push_back(remap(Op)); 677 678 if (!Changed) 679 continue; 680 681 NMD.clearOperands(); 682 for (auto *Op : Ops) 683 if (Op) 684 NMD.addOperand(Op); 685 } 686 return Changed; 687 } 688 689 unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) { 690 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>( 691 M.getModuleFlag("Debug Info Version"))) 692 return Val->getZExtValue(); 693 return 0; 694 } 695 696 void Instruction::applyMergedLocation(const DILocation *LocA, 697 const DILocation *LocB) { 698 setDebugLoc(DILocation::getMergedLocation(LocA, LocB, 699 DILocation::WithGeneratedLocation)); 700 } 701 702 //===----------------------------------------------------------------------===// 703 // LLVM C API implementations. 704 //===----------------------------------------------------------------------===// 705 706 static unsigned map_from_llvmDWARFsourcelanguage(LLVMDWARFSourceLanguage lang) { 707 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 llvm_unreachable("Unhandled Tag"); 714 } 715 716 template <typename DIT> DIT *unwrapDI(LLVMMetadataRef Ref) { 717 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 unsigned LLVMDebugMetadataVersion() { 729 return DEBUG_METADATA_VERSION; 730 } 731 732 LLVMDIBuilderRef LLVMCreateDIBuilderDisallowUnresolved(LLVMModuleRef M) { 733 return wrap(new DIBuilder(*unwrap(M), false)); 734 } 735 736 LLVMDIBuilderRef LLVMCreateDIBuilder(LLVMModuleRef M) { 737 return wrap(new DIBuilder(*unwrap(M))); 738 } 739 740 unsigned LLVMGetModuleDebugMetadataVersion(LLVMModuleRef M) { 741 return getDebugMetadataVersionFromModule(*unwrap(M)); 742 } 743 744 LLVMBool LLVMStripModuleDebugInfo(LLVMModuleRef M) { 745 return StripDebugInfo(*unwrap(M)); 746 } 747 748 void LLVMDisposeDIBuilder(LLVMDIBuilderRef Builder) { 749 delete unwrap(Builder); 750 } 751 752 void LLVMDIBuilderFinalize(LLVMDIBuilderRef Builder) { 753 unwrap(Builder)->finalize(); 754 } 755 756 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 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 SplitDebugInlining, DebugInfoForProfiling)); 772 } 773 774 LLVMMetadataRef 775 LLVMDIBuilderCreateFile(LLVMDIBuilderRef Builder, const char *Filename, 776 size_t FilenameLen, const char *Directory, 777 size_t DirectoryLen) { 778 return wrap(unwrap(Builder)->createFile(StringRef(Filename, FilenameLen), 779 StringRef(Directory, DirectoryLen))); 780 } 781 782 LLVMMetadataRef 783 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 return wrap(unwrap(Builder)->createModule( 789 unwrapDI<DIScope>(ParentScope), StringRef(Name, NameLen), 790 StringRef(ConfigMacros, ConfigMacrosLen), 791 StringRef(IncludePath, IncludePathLen), 792 StringRef(ISysRoot, ISysRootLen))); 793 } 794 795 LLVMMetadataRef LLVMDIBuilderCreateNameSpace(LLVMDIBuilderRef Builder, 796 LLVMMetadataRef ParentScope, 797 const char *Name, size_t NameLen, 798 LLVMBool ExportSymbols) { 799 return wrap(unwrap(Builder)->createNameSpace( 800 unwrapDI<DIScope>(ParentScope), StringRef(Name, NameLen), ExportSymbols)); 801 } 802 803 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 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 IsOptimized, nullptr, nullptr, nullptr)); 814 } 815 816 817 LLVMMetadataRef LLVMDIBuilderCreateLexicalBlock( 818 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, 819 LLVMMetadataRef File, unsigned Line, unsigned Col) { 820 return wrap(unwrap(Builder)->createLexicalBlock(unwrapDI<DIScope>(Scope), 821 unwrapDI<DIFile>(File), 822 Line, Col)); 823 } 824 825 LLVMMetadataRef 826 LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef Builder, 827 LLVMMetadataRef Scope, 828 LLVMMetadataRef File, 829 unsigned Discriminator) { 830 return wrap(unwrap(Builder)->createLexicalBlockFile(unwrapDI<DIScope>(Scope), 831 unwrapDI<DIFile>(File), 832 Discriminator)); 833 } 834 835 LLVMMetadataRef 836 LLVMDIBuilderCreateImportedModuleFromNamespace(LLVMDIBuilderRef Builder, 837 LLVMMetadataRef Scope, 838 LLVMMetadataRef NS, 839 LLVMMetadataRef File, 840 unsigned Line) { 841 return wrap(unwrap(Builder)->createImportedModule(unwrapDI<DIScope>(Scope), 842 unwrapDI<DINamespace>(NS), 843 unwrapDI<DIFile>(File), 844 Line)); 845 } 846 847 LLVMMetadataRef 848 LLVMDIBuilderCreateImportedModuleFromAlias(LLVMDIBuilderRef Builder, 849 LLVMMetadataRef Scope, 850 LLVMMetadataRef ImportedEntity, 851 LLVMMetadataRef File, 852 unsigned Line) { 853 return wrap(unwrap(Builder)->createImportedModule( 854 unwrapDI<DIScope>(Scope), 855 unwrapDI<DIImportedEntity>(ImportedEntity), 856 unwrapDI<DIFile>(File), Line)); 857 } 858 859 LLVMMetadataRef 860 LLVMDIBuilderCreateImportedModuleFromModule(LLVMDIBuilderRef Builder, 861 LLVMMetadataRef Scope, 862 LLVMMetadataRef M, 863 LLVMMetadataRef File, 864 unsigned Line) { 865 return wrap(unwrap(Builder)->createImportedModule(unwrapDI<DIScope>(Scope), 866 unwrapDI<DIModule>(M), 867 unwrapDI<DIFile>(File), 868 Line)); 869 } 870 871 LLVMMetadataRef 872 LLVMDIBuilderCreateImportedDeclaration(LLVMDIBuilderRef Builder, 873 LLVMMetadataRef Scope, 874 LLVMMetadataRef Decl, 875 LLVMMetadataRef File, 876 unsigned Line, 877 const char *Name, size_t NameLen) { 878 return wrap(unwrap(Builder)->createImportedDeclaration( 879 unwrapDI<DIScope>(Scope), 880 unwrapDI<DINode>(Decl), 881 unwrapDI<DIFile>(File), Line, {Name, NameLen})); 882 } 883 884 LLVMMetadataRef 885 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 unwrap(InlinedAt))); 890 } 891 892 unsigned LLVMDILocationGetLine(LLVMMetadataRef Location) { 893 return unwrapDI<DILocation>(Location)->getLine(); 894 } 895 896 unsigned LLVMDILocationGetColumn(LLVMMetadataRef Location) { 897 return unwrapDI<DILocation>(Location)->getColumn(); 898 } 899 900 LLVMMetadataRef LLVMDILocationGetScope(LLVMMetadataRef Location) { 901 return wrap(unwrapDI<DILocation>(Location)->getScope()); 902 } 903 904 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 NumElements}); 911 return wrap(unwrap(Builder)->createEnumerationType( 912 unwrapDI<DIScope>(Scope), {Name, NameLen}, unwrapDI<DIFile>(File), 913 LineNumber, SizeInBits, AlignInBits, Elts, unwrapDI<DIType>(ClassTy))); 914 } 915 916 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 NumElements}); 924 return wrap(unwrap(Builder)->createUnionType( 925 unwrapDI<DIScope>(Scope), {Name, NameLen}, unwrapDI<DIFile>(File), 926 LineNumber, SizeInBits, AlignInBits, map_from_llvmDIFlags(Flags), 927 Elts, RunTimeLang, {UniqueId, UniqueIdLen})); 928 } 929 930 931 LLVMMetadataRef 932 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 NumSubscripts}); 938 return wrap(unwrap(Builder)->createArrayType(Size, AlignInBits, 939 unwrapDI<DIType>(Ty), Subs)); 940 } 941 942 LLVMMetadataRef 943 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 NumSubscripts}); 949 return wrap(unwrap(Builder)->createVectorType(Size, AlignInBits, 950 unwrapDI<DIType>(Ty), Subs)); 951 } 952 953 LLVMMetadataRef 954 LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef Builder, const char *Name, 955 size_t NameLen, uint64_t SizeInBits, 956 LLVMDWARFTypeEncoding Encoding) { 957 return wrap(unwrap(Builder)->createBasicType({Name, NameLen}, 958 SizeInBits, Encoding)); 959 } 960 961 LLVMMetadataRef LLVMDIBuilderCreatePointerType( 962 LLVMDIBuilderRef Builder, LLVMMetadataRef PointeeTy, 963 uint64_t SizeInBits, uint32_t AlignInBits, unsigned AddressSpace, 964 const char *Name, size_t NameLen) { 965 return wrap(unwrap(Builder)->createPointerType(unwrapDI<DIType>(PointeeTy), 966 SizeInBits, AlignInBits, 967 AddressSpace, {Name, NameLen})); 968 } 969 970 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 NumElements}); 979 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 unwrapDI<DIType>(VTableHolder), {UniqueId, UniqueIdLen})); 984 } 985 986 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 return wrap(unwrap(Builder)->createMemberType(unwrapDI<DIScope>(Scope), 992 {Name, NameLen}, unwrapDI<DIFile>(File), LineNo, SizeInBits, AlignInBits, 993 OffsetInBits, map_from_llvmDIFlags(Flags), unwrapDI<DIType>(Ty))); 994 } 995 996 LLVMMetadataRef 997 LLVMDIBuilderCreateUnspecifiedType(LLVMDIBuilderRef Builder, const char *Name, 998 size_t NameLen) { 999 return wrap(unwrap(Builder)->createUnspecifiedType({Name, NameLen})); 1000 } 1001 1002 LLVMMetadataRef 1003 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 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 AlignInBits)); 1013 } 1014 1015 LLVMMetadataRef 1016 LLVMDIBuilderCreateObjectPointerType(LLVMDIBuilderRef Builder, 1017 LLVMMetadataRef Type) { 1018 return wrap(unwrap(Builder)->createObjectPointerType(unwrapDI<DIType>(Type))); 1019 } 1020 1021 LLVMMetadataRef 1022 LLVMDIBuilderCreateForwardDecl( 1023 LLVMDIBuilderRef Builder, unsigned Tag, const char *Name, 1024 size_t NameLen, LLVMMetadataRef Scope, LLVMMetadataRef File, unsigned Line, 1025 unsigned RuntimeLang, uint64_t SizeInBits, uint32_t AlignInBits, 1026 const char *UniqueIdentifier, size_t UniqueIdentifierLen) { 1027 return wrap(unwrap(Builder)->createForwardDecl( 1028 Tag, {Name, NameLen}, unwrapDI<DIScope>(Scope), 1029 unwrapDI<DIFile>(File), Line, RuntimeLang, SizeInBits, 1030 AlignInBits, {UniqueIdentifier, UniqueIdentifierLen})); 1031 } 1032 1033 LLVMMetadataRef 1034 LLVMDIBuilderCreateReplaceableCompositeType( 1035 LLVMDIBuilderRef Builder, unsigned Tag, const char *Name, 1036 size_t NameLen, LLVMMetadataRef Scope, LLVMMetadataRef File, unsigned Line, 1037 unsigned RuntimeLang, uint64_t SizeInBits, uint32_t AlignInBits, 1038 LLVMDIFlags Flags, const char *UniqueIdentifier, 1039 size_t UniqueIdentifierLen) { 1040 return wrap(unwrap(Builder)->createReplaceableCompositeType( 1041 Tag, {Name, NameLen}, unwrapDI<DIScope>(Scope), 1042 unwrapDI<DIFile>(File), Line, RuntimeLang, SizeInBits, 1043 AlignInBits, map_from_llvmDIFlags(Flags), 1044 {UniqueIdentifier, UniqueIdentifierLen})); 1045 } 1046 1047 LLVMMetadataRef 1048 LLVMDIBuilderCreateQualifiedType(LLVMDIBuilderRef Builder, unsigned Tag, 1049 LLVMMetadataRef Type) { 1050 return wrap(unwrap(Builder)->createQualifiedType(Tag, 1051 unwrapDI<DIType>(Type))); 1052 } 1053 1054 LLVMMetadataRef 1055 LLVMDIBuilderCreateReferenceType(LLVMDIBuilderRef Builder, unsigned Tag, 1056 LLVMMetadataRef Type) { 1057 return wrap(unwrap(Builder)->createReferenceType(Tag, 1058 unwrapDI<DIType>(Type))); 1059 } 1060 1061 LLVMMetadataRef 1062 LLVMDIBuilderCreateNullPtrType(LLVMDIBuilderRef Builder) { 1063 return wrap(unwrap(Builder)->createNullPtrType()); 1064 } 1065 1066 LLVMMetadataRef 1067 LLVMDIBuilderCreateMemberPointerType(LLVMDIBuilderRef Builder, 1068 LLVMMetadataRef PointeeType, 1069 LLVMMetadataRef ClassType, 1070 uint64_t SizeInBits, 1071 uint32_t AlignInBits, 1072 LLVMDIFlags Flags) { 1073 return wrap(unwrap(Builder)->createMemberPointerType( 1074 unwrapDI<DIType>(PointeeType), 1075 unwrapDI<DIType>(ClassType), AlignInBits, SizeInBits, 1076 map_from_llvmDIFlags(Flags))); 1077 } 1078 1079 LLVMMetadataRef 1080 LLVMDIBuilderCreateBitFieldMemberType(LLVMDIBuilderRef Builder, 1081 LLVMMetadataRef Scope, 1082 const char *Name, size_t NameLen, 1083 LLVMMetadataRef File, unsigned LineNumber, 1084 uint64_t SizeInBits, 1085 uint64_t OffsetInBits, 1086 uint64_t StorageOffsetInBits, 1087 LLVMDIFlags Flags, LLVMMetadataRef Type) { 1088 return wrap(unwrap(Builder)->createBitFieldMemberType( 1089 unwrapDI<DIScope>(Scope), {Name, NameLen}, 1090 unwrapDI<DIFile>(File), LineNumber, 1091 SizeInBits, OffsetInBits, StorageOffsetInBits, 1092 map_from_llvmDIFlags(Flags), unwrapDI<DIType>(Type))); 1093 } 1094 1095 LLVMMetadataRef LLVMDIBuilderCreateClassType(LLVMDIBuilderRef Builder, 1096 LLVMMetadataRef Scope, const char *Name, size_t NameLen, 1097 LLVMMetadataRef File, unsigned LineNumber, uint64_t SizeInBits, 1098 uint32_t AlignInBits, uint64_t OffsetInBits, LLVMDIFlags Flags, 1099 LLVMMetadataRef DerivedFrom, 1100 LLVMMetadataRef *Elements, unsigned NumElements, 1101 LLVMMetadataRef VTableHolder, LLVMMetadataRef TemplateParamsNode, 1102 const char *UniqueIdentifier, size_t UniqueIdentifierLen) { 1103 auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements), 1104 NumElements}); 1105 return wrap(unwrap(Builder)->createClassType( 1106 unwrapDI<DIScope>(Scope), {Name, NameLen}, 1107 unwrapDI<DIFile>(File), LineNumber, 1108 SizeInBits, AlignInBits, OffsetInBits, 1109 map_from_llvmDIFlags(Flags), unwrapDI<DIType>(DerivedFrom), 1110 Elts, unwrapDI<DIType>(VTableHolder), 1111 unwrapDI<MDNode>(TemplateParamsNode), 1112 {UniqueIdentifier, UniqueIdentifierLen})); 1113 } 1114 1115 LLVMMetadataRef 1116 LLVMDIBuilderCreateArtificialType(LLVMDIBuilderRef Builder, 1117 LLVMMetadataRef Type) { 1118 return wrap(unwrap(Builder)->createArtificialType(unwrapDI<DIType>(Type))); 1119 } 1120 1121 const char *LLVMDITypeGetName(LLVMMetadataRef DType, size_t *Length) { 1122 StringRef Str = unwrap<DIType>(DType)->getName(); 1123 *Length = Str.size(); 1124 return Str.data(); 1125 } 1126 1127 uint64_t LLVMDITypeGetSizeInBits(LLVMMetadataRef DType) { 1128 return unwrapDI<DIType>(DType)->getSizeInBits(); 1129 } 1130 1131 uint64_t LLVMDITypeGetOffsetInBits(LLVMMetadataRef DType) { 1132 return unwrapDI<DIType>(DType)->getOffsetInBits(); 1133 } 1134 1135 uint32_t LLVMDITypeGetAlignInBits(LLVMMetadataRef DType) { 1136 return unwrapDI<DIType>(DType)->getAlignInBits(); 1137 } 1138 1139 unsigned LLVMDITypeGetLine(LLVMMetadataRef DType) { 1140 return unwrapDI<DIType>(DType)->getLine(); 1141 } 1142 1143 LLVMDIFlags LLVMDITypeGetFlags(LLVMMetadataRef DType) { 1144 return map_to_llvmDIFlags(unwrapDI<DIType>(DType)->getFlags()); 1145 } 1146 1147 LLVMMetadataRef LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef Builder, 1148 LLVMMetadataRef *Types, 1149 size_t Length) { 1150 return wrap( 1151 unwrap(Builder)->getOrCreateTypeArray({unwrap(Types), Length}).get()); 1152 } 1153 1154 LLVMMetadataRef 1155 LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef Builder, 1156 LLVMMetadataRef File, 1157 LLVMMetadataRef *ParameterTypes, 1158 unsigned NumParameterTypes, 1159 LLVMDIFlags Flags) { 1160 auto Elts = unwrap(Builder)->getOrCreateTypeArray({unwrap(ParameterTypes), 1161 NumParameterTypes}); 1162 return wrap(unwrap(Builder)->createSubroutineType( 1163 Elts, map_from_llvmDIFlags(Flags))); 1164 } 1165 1166 LLVMMetadataRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Builder, 1167 int64_t *Addr, size_t Length) { 1168 return wrap(unwrap(Builder)->createExpression(ArrayRef<int64_t>(Addr, 1169 Length))); 1170 } 1171 1172 LLVMMetadataRef 1173 LLVMDIBuilderCreateConstantValueExpression(LLVMDIBuilderRef Builder, 1174 int64_t Value) { 1175 return wrap(unwrap(Builder)->createConstantValueExpression(Value)); 1176 } 1177 1178 LLVMMetadataRef 1179 LLVMDIBuilderCreateGlobalVariableExpression(LLVMDIBuilderRef Builder, 1180 LLVMMetadataRef Scope, 1181 const char *Name, size_t NameLen, 1182 const char *Linkage, size_t LinkLen, 1183 LLVMMetadataRef File, 1184 unsigned LineNo, 1185 LLVMMetadataRef Ty, 1186 LLVMBool LocalToUnit, 1187 LLVMMetadataRef Expr, 1188 LLVMMetadataRef Decl, 1189 uint32_t AlignInBits) { 1190 return wrap(unwrap(Builder)->createGlobalVariableExpression( 1191 unwrapDI<DIScope>(Scope), {Name, NameLen}, {Linkage, LinkLen}, 1192 unwrapDI<DIFile>(File), LineNo, unwrapDI<DIType>(Ty), 1193 LocalToUnit, unwrap<DIExpression>(Expr), 1194 unwrapDI<MDNode>(Decl), AlignInBits)); 1195 } 1196 1197 LLVMMetadataRef LLVMTemporaryMDNode(LLVMContextRef Ctx, LLVMMetadataRef *Data, 1198 size_t Count) { 1199 return wrap( 1200 MDTuple::getTemporary(*unwrap(Ctx), {unwrap(Data), Count}).release()); 1201 } 1202 1203 void LLVMDisposeTemporaryMDNode(LLVMMetadataRef TempNode) { 1204 MDNode::deleteTemporary(unwrapDI<MDNode>(TempNode)); 1205 } 1206 1207 void LLVMMetadataReplaceAllUsesWith(LLVMMetadataRef TargetMetadata, 1208 LLVMMetadataRef Replacement) { 1209 auto *Node = unwrapDI<MDNode>(TargetMetadata); 1210 Node->replaceAllUsesWith(unwrap<Metadata>(Replacement)); 1211 MDNode::deleteTemporary(Node); 1212 } 1213 1214 LLVMMetadataRef 1215 LLVMDIBuilderCreateTempGlobalVariableFwdDecl(LLVMDIBuilderRef Builder, 1216 LLVMMetadataRef Scope, 1217 const char *Name, size_t NameLen, 1218 const char *Linkage, size_t LnkLen, 1219 LLVMMetadataRef File, 1220 unsigned LineNo, 1221 LLVMMetadataRef Ty, 1222 LLVMBool LocalToUnit, 1223 LLVMMetadataRef Decl, 1224 uint32_t AlignInBits) { 1225 return wrap(unwrap(Builder)->createTempGlobalVariableFwdDecl( 1226 unwrapDI<DIScope>(Scope), {Name, NameLen}, {Linkage, LnkLen}, 1227 unwrapDI<DIFile>(File), LineNo, unwrapDI<DIType>(Ty), 1228 LocalToUnit, unwrapDI<MDNode>(Decl), AlignInBits)); 1229 } 1230 1231 LLVMValueRef LLVMDIBuilderInsertDeclareBefore( 1232 LLVMDIBuilderRef Builder, LLVMValueRef Storage, LLVMMetadataRef VarInfo, 1233 LLVMMetadataRef Expr, LLVMMetadataRef DL, LLVMValueRef Instr) { 1234 return wrap(unwrap(Builder)->insertDeclare( 1235 unwrap(Storage), unwrap<DILocalVariable>(VarInfo), 1236 unwrap<DIExpression>(Expr), unwrap<DILocation>(DL), 1237 unwrap<Instruction>(Instr))); 1238 } 1239 1240 LLVMValueRef LLVMDIBuilderInsertDeclareAtEnd( 1241 LLVMDIBuilderRef Builder, LLVMValueRef Storage, LLVMMetadataRef VarInfo, 1242 LLVMMetadataRef Expr, LLVMMetadataRef DL, LLVMBasicBlockRef Block) { 1243 return wrap(unwrap(Builder)->insertDeclare( 1244 unwrap(Storage), unwrap<DILocalVariable>(VarInfo), 1245 unwrap<DIExpression>(Expr), unwrap<DILocation>(DL), 1246 unwrap(Block))); 1247 } 1248 1249 LLVMValueRef LLVMDIBuilderInsertDbgValueBefore(LLVMDIBuilderRef Builder, 1250 LLVMValueRef Val, 1251 LLVMMetadataRef VarInfo, 1252 LLVMMetadataRef Expr, 1253 LLVMMetadataRef DebugLoc, 1254 LLVMValueRef Instr) { 1255 return wrap(unwrap(Builder)->insertDbgValueIntrinsic( 1256 unwrap(Val), unwrap<DILocalVariable>(VarInfo), 1257 unwrap<DIExpression>(Expr), unwrap<DILocation>(DebugLoc), 1258 unwrap<Instruction>(Instr))); 1259 } 1260 1261 LLVMValueRef LLVMDIBuilderInsertDbgValueAtEnd(LLVMDIBuilderRef Builder, 1262 LLVMValueRef Val, 1263 LLVMMetadataRef VarInfo, 1264 LLVMMetadataRef Expr, 1265 LLVMMetadataRef DebugLoc, 1266 LLVMBasicBlockRef Block) { 1267 return wrap(unwrap(Builder)->insertDbgValueIntrinsic( 1268 unwrap(Val), unwrap<DILocalVariable>(VarInfo), 1269 unwrap<DIExpression>(Expr), unwrap<DILocation>(DebugLoc), 1270 unwrap(Block))); 1271 } 1272 1273 LLVMMetadataRef LLVMDIBuilderCreateAutoVariable( 1274 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, 1275 size_t NameLen, LLVMMetadataRef File, unsigned LineNo, LLVMMetadataRef Ty, 1276 LLVMBool AlwaysPreserve, LLVMDIFlags Flags, uint32_t AlignInBits) { 1277 return wrap(unwrap(Builder)->createAutoVariable( 1278 unwrap<DIScope>(Scope), {Name, NameLen}, unwrap<DIFile>(File), 1279 LineNo, unwrap<DIType>(Ty), AlwaysPreserve, 1280 map_from_llvmDIFlags(Flags), AlignInBits)); 1281 } 1282 1283 LLVMMetadataRef LLVMDIBuilderCreateParameterVariable( 1284 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, 1285 size_t NameLen, unsigned ArgNo, LLVMMetadataRef File, unsigned LineNo, 1286 LLVMMetadataRef Ty, LLVMBool AlwaysPreserve, LLVMDIFlags Flags) { 1287 return wrap(unwrap(Builder)->createParameterVariable( 1288 unwrap<DIScope>(Scope), Name, ArgNo, unwrap<DIFile>(File), 1289 LineNo, unwrap<DIType>(Ty), AlwaysPreserve, 1290 map_from_llvmDIFlags(Flags))); 1291 } 1292 1293 LLVMMetadataRef LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef Builder, 1294 int64_t Lo, int64_t Count) { 1295 return wrap(unwrap(Builder)->getOrCreateSubrange(Lo, Count)); 1296 } 1297 1298 LLVMMetadataRef LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef Builder, 1299 LLVMMetadataRef *Data, 1300 size_t Length) { 1301 Metadata **DataValue = unwrap(Data); 1302 return wrap(unwrap(Builder)->getOrCreateArray({DataValue, Length}).get()); 1303 } 1304 1305 LLVMMetadataRef LLVMGetSubprogram(LLVMValueRef Func) { 1306 return wrap(unwrap<Function>(Func)->getSubprogram()); 1307 } 1308 1309 void LLVMSetSubprogram(LLVMValueRef Func, LLVMMetadataRef SP) { 1310 unwrap<Function>(Func)->setSubprogram(unwrap<DISubprogram>(SP)); 1311 } 1312