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