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()); 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) { 786 auto File = unwrapDI<DIFile>(FileRef); 787 788 return wrap(unwrap(Builder)->createCompileUnit( 789 map_from_llvmDWARFsourcelanguage(Lang), File, 790 StringRef(Producer, ProducerLen), isOptimized, 791 StringRef(Flags, FlagsLen), RuntimeVer, 792 StringRef(SplitName, SplitNameLen), 793 static_cast<DICompileUnit::DebugEmissionKind>(Kind), DWOId, 794 SplitDebugInlining, DebugInfoForProfiling)); 795 } 796 797 LLVMMetadataRef 798 LLVMDIBuilderCreateFile(LLVMDIBuilderRef Builder, const char *Filename, 799 size_t FilenameLen, const char *Directory, 800 size_t DirectoryLen) { 801 return wrap(unwrap(Builder)->createFile(StringRef(Filename, FilenameLen), 802 StringRef(Directory, DirectoryLen))); 803 } 804 805 LLVMMetadataRef 806 LLVMDIBuilderCreateModule(LLVMDIBuilderRef Builder, LLVMMetadataRef ParentScope, 807 const char *Name, size_t NameLen, 808 const char *ConfigMacros, size_t ConfigMacrosLen, 809 const char *IncludePath, size_t IncludePathLen, 810 const char *SysRoot, size_t SysRootLen) { 811 return wrap(unwrap(Builder)->createModule( 812 unwrapDI<DIScope>(ParentScope), StringRef(Name, NameLen), 813 StringRef(ConfigMacros, ConfigMacrosLen), 814 StringRef(IncludePath, IncludePathLen), 815 StringRef(SysRoot, SysRootLen))); 816 } 817 818 LLVMMetadataRef LLVMDIBuilderCreateNameSpace(LLVMDIBuilderRef Builder, 819 LLVMMetadataRef ParentScope, 820 const char *Name, size_t NameLen, 821 LLVMBool ExportSymbols) { 822 return wrap(unwrap(Builder)->createNameSpace( 823 unwrapDI<DIScope>(ParentScope), StringRef(Name, NameLen), ExportSymbols)); 824 } 825 826 LLVMMetadataRef LLVMDIBuilderCreateFunction( 827 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, 828 size_t NameLen, const char *LinkageName, size_t LinkageNameLen, 829 LLVMMetadataRef File, unsigned LineNo, LLVMMetadataRef Ty, 830 LLVMBool IsLocalToUnit, LLVMBool IsDefinition, 831 unsigned ScopeLine, LLVMDIFlags Flags, LLVMBool IsOptimized) { 832 return wrap(unwrap(Builder)->createFunction( 833 unwrapDI<DIScope>(Scope), {Name, NameLen}, {LinkageName, LinkageNameLen}, 834 unwrapDI<DIFile>(File), LineNo, unwrapDI<DISubroutineType>(Ty), ScopeLine, 835 map_from_llvmDIFlags(Flags), 836 pack_into_DISPFlags(IsLocalToUnit, IsDefinition, IsOptimized), nullptr, 837 nullptr, nullptr)); 838 } 839 840 841 LLVMMetadataRef LLVMDIBuilderCreateLexicalBlock( 842 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, 843 LLVMMetadataRef File, unsigned Line, unsigned Col) { 844 return wrap(unwrap(Builder)->createLexicalBlock(unwrapDI<DIScope>(Scope), 845 unwrapDI<DIFile>(File), 846 Line, Col)); 847 } 848 849 LLVMMetadataRef 850 LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef Builder, 851 LLVMMetadataRef Scope, 852 LLVMMetadataRef File, 853 unsigned Discriminator) { 854 return wrap(unwrap(Builder)->createLexicalBlockFile(unwrapDI<DIScope>(Scope), 855 unwrapDI<DIFile>(File), 856 Discriminator)); 857 } 858 859 LLVMMetadataRef 860 LLVMDIBuilderCreateImportedModuleFromNamespace(LLVMDIBuilderRef Builder, 861 LLVMMetadataRef Scope, 862 LLVMMetadataRef NS, 863 LLVMMetadataRef File, 864 unsigned Line) { 865 return wrap(unwrap(Builder)->createImportedModule(unwrapDI<DIScope>(Scope), 866 unwrapDI<DINamespace>(NS), 867 unwrapDI<DIFile>(File), 868 Line)); 869 } 870 871 LLVMMetadataRef 872 LLVMDIBuilderCreateImportedModuleFromAlias(LLVMDIBuilderRef Builder, 873 LLVMMetadataRef Scope, 874 LLVMMetadataRef ImportedEntity, 875 LLVMMetadataRef File, 876 unsigned Line) { 877 return wrap(unwrap(Builder)->createImportedModule( 878 unwrapDI<DIScope>(Scope), 879 unwrapDI<DIImportedEntity>(ImportedEntity), 880 unwrapDI<DIFile>(File), Line)); 881 } 882 883 LLVMMetadataRef 884 LLVMDIBuilderCreateImportedModuleFromModule(LLVMDIBuilderRef Builder, 885 LLVMMetadataRef Scope, 886 LLVMMetadataRef M, 887 LLVMMetadataRef File, 888 unsigned Line) { 889 return wrap(unwrap(Builder)->createImportedModule(unwrapDI<DIScope>(Scope), 890 unwrapDI<DIModule>(M), 891 unwrapDI<DIFile>(File), 892 Line)); 893 } 894 895 LLVMMetadataRef 896 LLVMDIBuilderCreateImportedDeclaration(LLVMDIBuilderRef Builder, 897 LLVMMetadataRef Scope, 898 LLVMMetadataRef Decl, 899 LLVMMetadataRef File, 900 unsigned Line, 901 const char *Name, size_t NameLen) { 902 return wrap(unwrap(Builder)->createImportedDeclaration( 903 unwrapDI<DIScope>(Scope), 904 unwrapDI<DINode>(Decl), 905 unwrapDI<DIFile>(File), Line, {Name, NameLen})); 906 } 907 908 LLVMMetadataRef 909 LLVMDIBuilderCreateDebugLocation(LLVMContextRef Ctx, unsigned Line, 910 unsigned Column, LLVMMetadataRef Scope, 911 LLVMMetadataRef InlinedAt) { 912 return wrap(DILocation::get(*unwrap(Ctx), Line, Column, unwrap(Scope), 913 unwrap(InlinedAt))); 914 } 915 916 unsigned LLVMDILocationGetLine(LLVMMetadataRef Location) { 917 return unwrapDI<DILocation>(Location)->getLine(); 918 } 919 920 unsigned LLVMDILocationGetColumn(LLVMMetadataRef Location) { 921 return unwrapDI<DILocation>(Location)->getColumn(); 922 } 923 924 LLVMMetadataRef LLVMDILocationGetScope(LLVMMetadataRef Location) { 925 return wrap(unwrapDI<DILocation>(Location)->getScope()); 926 } 927 928 LLVMMetadataRef LLVMDILocationGetInlinedAt(LLVMMetadataRef Location) { 929 return wrap(unwrapDI<DILocation>(Location)->getInlinedAt()); 930 } 931 932 LLVMMetadataRef LLVMDIScopeGetFile(LLVMMetadataRef Scope) { 933 return wrap(unwrapDI<DIScope>(Scope)->getFile()); 934 } 935 936 const char *LLVMDIFileGetDirectory(LLVMMetadataRef File, unsigned *Len) { 937 auto Dir = unwrapDI<DIFile>(File)->getDirectory(); 938 *Len = Dir.size(); 939 return Dir.data(); 940 } 941 942 const char *LLVMDIFileGetFilename(LLVMMetadataRef File, unsigned *Len) { 943 auto Name = unwrapDI<DIFile>(File)->getFilename(); 944 *Len = Name.size(); 945 return Name.data(); 946 } 947 948 const char *LLVMDIFileGetSource(LLVMMetadataRef File, unsigned *Len) { 949 if (auto Src = unwrapDI<DIFile>(File)->getSource()) { 950 *Len = Src->size(); 951 return Src->data(); 952 } 953 *Len = 0; 954 return ""; 955 } 956 957 LLVMMetadataRef LLVMDIBuilderCreateMacro(LLVMDIBuilderRef Builder, 958 LLVMMetadataRef ParentMacroFile, 959 unsigned Line, 960 LLVMDWARFMacinfoRecordType RecordType, 961 const char *Name, size_t NameLen, 962 const char *Value, size_t ValueLen) { 963 return wrap( 964 unwrap(Builder)->createMacro(unwrapDI<DIMacroFile>(ParentMacroFile), Line, 965 static_cast<MacinfoRecordType>(RecordType), 966 {Name, NameLen}, {Value, ValueLen})); 967 } 968 969 LLVMMetadataRef 970 LLVMDIBuilderCreateTempMacroFile(LLVMDIBuilderRef Builder, 971 LLVMMetadataRef ParentMacroFile, unsigned Line, 972 LLVMMetadataRef File) { 973 return wrap(unwrap(Builder)->createTempMacroFile( 974 unwrapDI<DIMacroFile>(ParentMacroFile), Line, unwrapDI<DIFile>(File))); 975 } 976 977 LLVMMetadataRef LLVMDIBuilderCreateEnumerator(LLVMDIBuilderRef Builder, 978 const char *Name, size_t NameLen, 979 int64_t Value, 980 LLVMBool IsUnsigned) { 981 return wrap(unwrap(Builder)->createEnumerator({Name, NameLen}, Value, 982 IsUnsigned != 0)); 983 } 984 985 LLVMMetadataRef LLVMDIBuilderCreateEnumerationType( 986 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, 987 size_t NameLen, LLVMMetadataRef File, unsigned LineNumber, 988 uint64_t SizeInBits, uint32_t AlignInBits, LLVMMetadataRef *Elements, 989 unsigned NumElements, LLVMMetadataRef ClassTy) { 990 auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements), 991 NumElements}); 992 return wrap(unwrap(Builder)->createEnumerationType( 993 unwrapDI<DIScope>(Scope), {Name, NameLen}, unwrapDI<DIFile>(File), 994 LineNumber, SizeInBits, AlignInBits, Elts, unwrapDI<DIType>(ClassTy))); 995 } 996 997 LLVMMetadataRef LLVMDIBuilderCreateUnionType( 998 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, 999 size_t NameLen, LLVMMetadataRef File, unsigned LineNumber, 1000 uint64_t SizeInBits, uint32_t AlignInBits, LLVMDIFlags Flags, 1001 LLVMMetadataRef *Elements, unsigned NumElements, unsigned RunTimeLang, 1002 const char *UniqueId, size_t UniqueIdLen) { 1003 auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements), 1004 NumElements}); 1005 return wrap(unwrap(Builder)->createUnionType( 1006 unwrapDI<DIScope>(Scope), {Name, NameLen}, unwrapDI<DIFile>(File), 1007 LineNumber, SizeInBits, AlignInBits, map_from_llvmDIFlags(Flags), 1008 Elts, RunTimeLang, {UniqueId, UniqueIdLen})); 1009 } 1010 1011 1012 LLVMMetadataRef 1013 LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef Builder, uint64_t Size, 1014 uint32_t AlignInBits, LLVMMetadataRef Ty, 1015 LLVMMetadataRef *Subscripts, 1016 unsigned NumSubscripts) { 1017 auto Subs = unwrap(Builder)->getOrCreateArray({unwrap(Subscripts), 1018 NumSubscripts}); 1019 return wrap(unwrap(Builder)->createArrayType(Size, AlignInBits, 1020 unwrapDI<DIType>(Ty), Subs)); 1021 } 1022 1023 LLVMMetadataRef 1024 LLVMDIBuilderCreateVectorType(LLVMDIBuilderRef Builder, uint64_t Size, 1025 uint32_t AlignInBits, LLVMMetadataRef Ty, 1026 LLVMMetadataRef *Subscripts, 1027 unsigned NumSubscripts) { 1028 auto Subs = unwrap(Builder)->getOrCreateArray({unwrap(Subscripts), 1029 NumSubscripts}); 1030 return wrap(unwrap(Builder)->createVectorType(Size, AlignInBits, 1031 unwrapDI<DIType>(Ty), Subs)); 1032 } 1033 1034 LLVMMetadataRef 1035 LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef Builder, const char *Name, 1036 size_t NameLen, uint64_t SizeInBits, 1037 LLVMDWARFTypeEncoding Encoding, 1038 LLVMDIFlags Flags) { 1039 return wrap(unwrap(Builder)->createBasicType({Name, NameLen}, 1040 SizeInBits, Encoding, 1041 map_from_llvmDIFlags(Flags))); 1042 } 1043 1044 LLVMMetadataRef LLVMDIBuilderCreatePointerType( 1045 LLVMDIBuilderRef Builder, LLVMMetadataRef PointeeTy, 1046 uint64_t SizeInBits, uint32_t AlignInBits, unsigned AddressSpace, 1047 const char *Name, size_t NameLen) { 1048 return wrap(unwrap(Builder)->createPointerType(unwrapDI<DIType>(PointeeTy), 1049 SizeInBits, AlignInBits, 1050 AddressSpace, {Name, NameLen})); 1051 } 1052 1053 LLVMMetadataRef LLVMDIBuilderCreateStructType( 1054 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, 1055 size_t NameLen, LLVMMetadataRef File, unsigned LineNumber, 1056 uint64_t SizeInBits, uint32_t AlignInBits, LLVMDIFlags Flags, 1057 LLVMMetadataRef DerivedFrom, LLVMMetadataRef *Elements, 1058 unsigned NumElements, unsigned RunTimeLang, LLVMMetadataRef VTableHolder, 1059 const char *UniqueId, size_t UniqueIdLen) { 1060 auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements), 1061 NumElements}); 1062 return wrap(unwrap(Builder)->createStructType( 1063 unwrapDI<DIScope>(Scope), {Name, NameLen}, unwrapDI<DIFile>(File), 1064 LineNumber, SizeInBits, AlignInBits, map_from_llvmDIFlags(Flags), 1065 unwrapDI<DIType>(DerivedFrom), Elts, RunTimeLang, 1066 unwrapDI<DIType>(VTableHolder), {UniqueId, UniqueIdLen})); 1067 } 1068 1069 LLVMMetadataRef LLVMDIBuilderCreateMemberType( 1070 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, 1071 size_t NameLen, LLVMMetadataRef File, unsigned LineNo, uint64_t SizeInBits, 1072 uint32_t AlignInBits, uint64_t OffsetInBits, LLVMDIFlags Flags, 1073 LLVMMetadataRef Ty) { 1074 return wrap(unwrap(Builder)->createMemberType(unwrapDI<DIScope>(Scope), 1075 {Name, NameLen}, unwrapDI<DIFile>(File), LineNo, SizeInBits, AlignInBits, 1076 OffsetInBits, map_from_llvmDIFlags(Flags), unwrapDI<DIType>(Ty))); 1077 } 1078 1079 LLVMMetadataRef 1080 LLVMDIBuilderCreateUnspecifiedType(LLVMDIBuilderRef Builder, const char *Name, 1081 size_t NameLen) { 1082 return wrap(unwrap(Builder)->createUnspecifiedType({Name, NameLen})); 1083 } 1084 1085 LLVMMetadataRef 1086 LLVMDIBuilderCreateStaticMemberType( 1087 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, 1088 size_t NameLen, LLVMMetadataRef File, unsigned LineNumber, 1089 LLVMMetadataRef Type, LLVMDIFlags Flags, LLVMValueRef ConstantVal, 1090 uint32_t AlignInBits) { 1091 return wrap(unwrap(Builder)->createStaticMemberType( 1092 unwrapDI<DIScope>(Scope), {Name, NameLen}, 1093 unwrapDI<DIFile>(File), LineNumber, unwrapDI<DIType>(Type), 1094 map_from_llvmDIFlags(Flags), unwrap<Constant>(ConstantVal), 1095 AlignInBits)); 1096 } 1097 1098 LLVMMetadataRef 1099 LLVMDIBuilderCreateObjCIVar(LLVMDIBuilderRef Builder, 1100 const char *Name, size_t NameLen, 1101 LLVMMetadataRef File, unsigned LineNo, 1102 uint64_t SizeInBits, uint32_t AlignInBits, 1103 uint64_t OffsetInBits, LLVMDIFlags Flags, 1104 LLVMMetadataRef Ty, LLVMMetadataRef PropertyNode) { 1105 return wrap(unwrap(Builder)->createObjCIVar( 1106 {Name, NameLen}, unwrapDI<DIFile>(File), LineNo, 1107 SizeInBits, AlignInBits, OffsetInBits, 1108 map_from_llvmDIFlags(Flags), unwrapDI<DIType>(Ty), 1109 unwrapDI<MDNode>(PropertyNode))); 1110 } 1111 1112 LLVMMetadataRef 1113 LLVMDIBuilderCreateObjCProperty(LLVMDIBuilderRef Builder, 1114 const char *Name, size_t NameLen, 1115 LLVMMetadataRef File, unsigned LineNo, 1116 const char *GetterName, size_t GetterNameLen, 1117 const char *SetterName, size_t SetterNameLen, 1118 unsigned PropertyAttributes, 1119 LLVMMetadataRef Ty) { 1120 return wrap(unwrap(Builder)->createObjCProperty( 1121 {Name, NameLen}, unwrapDI<DIFile>(File), LineNo, 1122 {GetterName, GetterNameLen}, {SetterName, SetterNameLen}, 1123 PropertyAttributes, unwrapDI<DIType>(Ty))); 1124 } 1125 1126 LLVMMetadataRef 1127 LLVMDIBuilderCreateObjectPointerType(LLVMDIBuilderRef Builder, 1128 LLVMMetadataRef Type) { 1129 return wrap(unwrap(Builder)->createObjectPointerType(unwrapDI<DIType>(Type))); 1130 } 1131 1132 LLVMMetadataRef 1133 LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef Builder, LLVMMetadataRef Type, 1134 const char *Name, size_t NameLen, 1135 LLVMMetadataRef File, unsigned LineNo, 1136 LLVMMetadataRef Scope, uint32_t AlignInBits) { 1137 return wrap(unwrap(Builder)->createTypedef( 1138 unwrapDI<DIType>(Type), {Name, NameLen}, unwrapDI<DIFile>(File), LineNo, 1139 unwrapDI<DIScope>(Scope), AlignInBits)); 1140 } 1141 1142 LLVMMetadataRef 1143 LLVMDIBuilderCreateInheritance(LLVMDIBuilderRef Builder, 1144 LLVMMetadataRef Ty, LLVMMetadataRef BaseTy, 1145 uint64_t BaseOffset, uint32_t VBPtrOffset, 1146 LLVMDIFlags Flags) { 1147 return wrap(unwrap(Builder)->createInheritance( 1148 unwrapDI<DIType>(Ty), unwrapDI<DIType>(BaseTy), 1149 BaseOffset, VBPtrOffset, map_from_llvmDIFlags(Flags))); 1150 } 1151 1152 LLVMMetadataRef 1153 LLVMDIBuilderCreateForwardDecl( 1154 LLVMDIBuilderRef Builder, unsigned Tag, const char *Name, 1155 size_t NameLen, LLVMMetadataRef Scope, LLVMMetadataRef File, unsigned Line, 1156 unsigned RuntimeLang, uint64_t SizeInBits, uint32_t AlignInBits, 1157 const char *UniqueIdentifier, size_t UniqueIdentifierLen) { 1158 return wrap(unwrap(Builder)->createForwardDecl( 1159 Tag, {Name, NameLen}, unwrapDI<DIScope>(Scope), 1160 unwrapDI<DIFile>(File), Line, RuntimeLang, SizeInBits, 1161 AlignInBits, {UniqueIdentifier, UniqueIdentifierLen})); 1162 } 1163 1164 LLVMMetadataRef 1165 LLVMDIBuilderCreateReplaceableCompositeType( 1166 LLVMDIBuilderRef Builder, unsigned Tag, const char *Name, 1167 size_t NameLen, LLVMMetadataRef Scope, LLVMMetadataRef File, unsigned Line, 1168 unsigned RuntimeLang, uint64_t SizeInBits, uint32_t AlignInBits, 1169 LLVMDIFlags Flags, const char *UniqueIdentifier, 1170 size_t UniqueIdentifierLen) { 1171 return wrap(unwrap(Builder)->createReplaceableCompositeType( 1172 Tag, {Name, NameLen}, unwrapDI<DIScope>(Scope), 1173 unwrapDI<DIFile>(File), Line, RuntimeLang, SizeInBits, 1174 AlignInBits, map_from_llvmDIFlags(Flags), 1175 {UniqueIdentifier, UniqueIdentifierLen})); 1176 } 1177 1178 LLVMMetadataRef 1179 LLVMDIBuilderCreateQualifiedType(LLVMDIBuilderRef Builder, unsigned Tag, 1180 LLVMMetadataRef Type) { 1181 return wrap(unwrap(Builder)->createQualifiedType(Tag, 1182 unwrapDI<DIType>(Type))); 1183 } 1184 1185 LLVMMetadataRef 1186 LLVMDIBuilderCreateReferenceType(LLVMDIBuilderRef Builder, unsigned Tag, 1187 LLVMMetadataRef Type) { 1188 return wrap(unwrap(Builder)->createReferenceType(Tag, 1189 unwrapDI<DIType>(Type))); 1190 } 1191 1192 LLVMMetadataRef 1193 LLVMDIBuilderCreateNullPtrType(LLVMDIBuilderRef Builder) { 1194 return wrap(unwrap(Builder)->createNullPtrType()); 1195 } 1196 1197 LLVMMetadataRef 1198 LLVMDIBuilderCreateMemberPointerType(LLVMDIBuilderRef Builder, 1199 LLVMMetadataRef PointeeType, 1200 LLVMMetadataRef ClassType, 1201 uint64_t SizeInBits, 1202 uint32_t AlignInBits, 1203 LLVMDIFlags Flags) { 1204 return wrap(unwrap(Builder)->createMemberPointerType( 1205 unwrapDI<DIType>(PointeeType), 1206 unwrapDI<DIType>(ClassType), AlignInBits, SizeInBits, 1207 map_from_llvmDIFlags(Flags))); 1208 } 1209 1210 LLVMMetadataRef 1211 LLVMDIBuilderCreateBitFieldMemberType(LLVMDIBuilderRef Builder, 1212 LLVMMetadataRef Scope, 1213 const char *Name, size_t NameLen, 1214 LLVMMetadataRef File, unsigned LineNumber, 1215 uint64_t SizeInBits, 1216 uint64_t OffsetInBits, 1217 uint64_t StorageOffsetInBits, 1218 LLVMDIFlags Flags, LLVMMetadataRef Type) { 1219 return wrap(unwrap(Builder)->createBitFieldMemberType( 1220 unwrapDI<DIScope>(Scope), {Name, NameLen}, 1221 unwrapDI<DIFile>(File), LineNumber, 1222 SizeInBits, OffsetInBits, StorageOffsetInBits, 1223 map_from_llvmDIFlags(Flags), unwrapDI<DIType>(Type))); 1224 } 1225 1226 LLVMMetadataRef LLVMDIBuilderCreateClassType(LLVMDIBuilderRef Builder, 1227 LLVMMetadataRef Scope, const char *Name, size_t NameLen, 1228 LLVMMetadataRef File, unsigned LineNumber, uint64_t SizeInBits, 1229 uint32_t AlignInBits, uint64_t OffsetInBits, LLVMDIFlags Flags, 1230 LLVMMetadataRef DerivedFrom, 1231 LLVMMetadataRef *Elements, unsigned NumElements, 1232 LLVMMetadataRef VTableHolder, LLVMMetadataRef TemplateParamsNode, 1233 const char *UniqueIdentifier, size_t UniqueIdentifierLen) { 1234 auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements), 1235 NumElements}); 1236 return wrap(unwrap(Builder)->createClassType( 1237 unwrapDI<DIScope>(Scope), {Name, NameLen}, 1238 unwrapDI<DIFile>(File), LineNumber, 1239 SizeInBits, AlignInBits, OffsetInBits, 1240 map_from_llvmDIFlags(Flags), unwrapDI<DIType>(DerivedFrom), 1241 Elts, unwrapDI<DIType>(VTableHolder), 1242 unwrapDI<MDNode>(TemplateParamsNode), 1243 {UniqueIdentifier, UniqueIdentifierLen})); 1244 } 1245 1246 LLVMMetadataRef 1247 LLVMDIBuilderCreateArtificialType(LLVMDIBuilderRef Builder, 1248 LLVMMetadataRef Type) { 1249 return wrap(unwrap(Builder)->createArtificialType(unwrapDI<DIType>(Type))); 1250 } 1251 1252 const char *LLVMDITypeGetName(LLVMMetadataRef DType, size_t *Length) { 1253 StringRef Str = unwrap<DIType>(DType)->getName(); 1254 *Length = Str.size(); 1255 return Str.data(); 1256 } 1257 1258 uint64_t LLVMDITypeGetSizeInBits(LLVMMetadataRef DType) { 1259 return unwrapDI<DIType>(DType)->getSizeInBits(); 1260 } 1261 1262 uint64_t LLVMDITypeGetOffsetInBits(LLVMMetadataRef DType) { 1263 return unwrapDI<DIType>(DType)->getOffsetInBits(); 1264 } 1265 1266 uint32_t LLVMDITypeGetAlignInBits(LLVMMetadataRef DType) { 1267 return unwrapDI<DIType>(DType)->getAlignInBits(); 1268 } 1269 1270 unsigned LLVMDITypeGetLine(LLVMMetadataRef DType) { 1271 return unwrapDI<DIType>(DType)->getLine(); 1272 } 1273 1274 LLVMDIFlags LLVMDITypeGetFlags(LLVMMetadataRef DType) { 1275 return map_to_llvmDIFlags(unwrapDI<DIType>(DType)->getFlags()); 1276 } 1277 1278 LLVMMetadataRef LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef Builder, 1279 LLVMMetadataRef *Types, 1280 size_t Length) { 1281 return wrap( 1282 unwrap(Builder)->getOrCreateTypeArray({unwrap(Types), Length}).get()); 1283 } 1284 1285 LLVMMetadataRef 1286 LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef Builder, 1287 LLVMMetadataRef File, 1288 LLVMMetadataRef *ParameterTypes, 1289 unsigned NumParameterTypes, 1290 LLVMDIFlags Flags) { 1291 auto Elts = unwrap(Builder)->getOrCreateTypeArray({unwrap(ParameterTypes), 1292 NumParameterTypes}); 1293 return wrap(unwrap(Builder)->createSubroutineType( 1294 Elts, map_from_llvmDIFlags(Flags))); 1295 } 1296 1297 LLVMMetadataRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Builder, 1298 int64_t *Addr, size_t Length) { 1299 return wrap(unwrap(Builder)->createExpression(ArrayRef<int64_t>(Addr, 1300 Length))); 1301 } 1302 1303 LLVMMetadataRef 1304 LLVMDIBuilderCreateConstantValueExpression(LLVMDIBuilderRef Builder, 1305 int64_t Value) { 1306 return wrap(unwrap(Builder)->createConstantValueExpression(Value)); 1307 } 1308 1309 LLVMMetadataRef LLVMDIBuilderCreateGlobalVariableExpression( 1310 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, 1311 size_t NameLen, const char *Linkage, size_t LinkLen, LLVMMetadataRef File, 1312 unsigned LineNo, LLVMMetadataRef Ty, LLVMBool LocalToUnit, 1313 LLVMMetadataRef Expr, LLVMMetadataRef Decl, uint32_t AlignInBits) { 1314 return wrap(unwrap(Builder)->createGlobalVariableExpression( 1315 unwrapDI<DIScope>(Scope), {Name, NameLen}, {Linkage, LinkLen}, 1316 unwrapDI<DIFile>(File), LineNo, unwrapDI<DIType>(Ty), LocalToUnit, 1317 true, unwrap<DIExpression>(Expr), unwrapDI<MDNode>(Decl), 1318 nullptr, AlignInBits)); 1319 } 1320 1321 LLVMMetadataRef LLVMDIGlobalVariableExpressionGetVariable(LLVMMetadataRef GVE) { 1322 return wrap(unwrapDI<DIGlobalVariableExpression>(GVE)->getVariable()); 1323 } 1324 1325 LLVMMetadataRef LLVMDIGlobalVariableExpressionGetExpression( 1326 LLVMMetadataRef GVE) { 1327 return wrap(unwrapDI<DIGlobalVariableExpression>(GVE)->getExpression()); 1328 } 1329 1330 LLVMMetadataRef LLVMDIVariableGetFile(LLVMMetadataRef Var) { 1331 return wrap(unwrapDI<DIVariable>(Var)->getFile()); 1332 } 1333 1334 LLVMMetadataRef LLVMDIVariableGetScope(LLVMMetadataRef Var) { 1335 return wrap(unwrapDI<DIVariable>(Var)->getScope()); 1336 } 1337 1338 unsigned LLVMDIVariableGetLine(LLVMMetadataRef Var) { 1339 return unwrapDI<DIVariable>(Var)->getLine(); 1340 } 1341 1342 LLVMMetadataRef LLVMTemporaryMDNode(LLVMContextRef Ctx, LLVMMetadataRef *Data, 1343 size_t Count) { 1344 return wrap( 1345 MDTuple::getTemporary(*unwrap(Ctx), {unwrap(Data), Count}).release()); 1346 } 1347 1348 void LLVMDisposeTemporaryMDNode(LLVMMetadataRef TempNode) { 1349 MDNode::deleteTemporary(unwrapDI<MDNode>(TempNode)); 1350 } 1351 1352 void LLVMMetadataReplaceAllUsesWith(LLVMMetadataRef TargetMetadata, 1353 LLVMMetadataRef Replacement) { 1354 auto *Node = unwrapDI<MDNode>(TargetMetadata); 1355 Node->replaceAllUsesWith(unwrap<Metadata>(Replacement)); 1356 MDNode::deleteTemporary(Node); 1357 } 1358 1359 LLVMMetadataRef LLVMDIBuilderCreateTempGlobalVariableFwdDecl( 1360 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, 1361 size_t NameLen, const char *Linkage, size_t LnkLen, LLVMMetadataRef File, 1362 unsigned LineNo, LLVMMetadataRef Ty, LLVMBool LocalToUnit, 1363 LLVMMetadataRef Decl, uint32_t AlignInBits) { 1364 return wrap(unwrap(Builder)->createTempGlobalVariableFwdDecl( 1365 unwrapDI<DIScope>(Scope), {Name, NameLen}, {Linkage, LnkLen}, 1366 unwrapDI<DIFile>(File), LineNo, unwrapDI<DIType>(Ty), LocalToUnit, 1367 unwrapDI<MDNode>(Decl), nullptr, AlignInBits)); 1368 } 1369 1370 LLVMValueRef 1371 LLVMDIBuilderInsertDeclareBefore(LLVMDIBuilderRef Builder, LLVMValueRef Storage, 1372 LLVMMetadataRef VarInfo, LLVMMetadataRef Expr, 1373 LLVMMetadataRef DL, LLVMValueRef Instr) { 1374 return wrap(unwrap(Builder)->insertDeclare( 1375 unwrap(Storage), unwrap<DILocalVariable>(VarInfo), 1376 unwrap<DIExpression>(Expr), unwrap<DILocation>(DL), 1377 unwrap<Instruction>(Instr))); 1378 } 1379 1380 LLVMValueRef LLVMDIBuilderInsertDeclareAtEnd( 1381 LLVMDIBuilderRef Builder, LLVMValueRef Storage, LLVMMetadataRef VarInfo, 1382 LLVMMetadataRef Expr, LLVMMetadataRef DL, LLVMBasicBlockRef Block) { 1383 return wrap(unwrap(Builder)->insertDeclare( 1384 unwrap(Storage), unwrap<DILocalVariable>(VarInfo), 1385 unwrap<DIExpression>(Expr), unwrap<DILocation>(DL), 1386 unwrap(Block))); 1387 } 1388 1389 LLVMValueRef LLVMDIBuilderInsertDbgValueBefore(LLVMDIBuilderRef Builder, 1390 LLVMValueRef Val, 1391 LLVMMetadataRef VarInfo, 1392 LLVMMetadataRef Expr, 1393 LLVMMetadataRef DebugLoc, 1394 LLVMValueRef Instr) { 1395 return wrap(unwrap(Builder)->insertDbgValueIntrinsic( 1396 unwrap(Val), unwrap<DILocalVariable>(VarInfo), 1397 unwrap<DIExpression>(Expr), unwrap<DILocation>(DebugLoc), 1398 unwrap<Instruction>(Instr))); 1399 } 1400 1401 LLVMValueRef LLVMDIBuilderInsertDbgValueAtEnd(LLVMDIBuilderRef Builder, 1402 LLVMValueRef Val, 1403 LLVMMetadataRef VarInfo, 1404 LLVMMetadataRef Expr, 1405 LLVMMetadataRef DebugLoc, 1406 LLVMBasicBlockRef Block) { 1407 return wrap(unwrap(Builder)->insertDbgValueIntrinsic( 1408 unwrap(Val), unwrap<DILocalVariable>(VarInfo), 1409 unwrap<DIExpression>(Expr), unwrap<DILocation>(DebugLoc), 1410 unwrap(Block))); 1411 } 1412 1413 LLVMMetadataRef LLVMDIBuilderCreateAutoVariable( 1414 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, 1415 size_t NameLen, LLVMMetadataRef File, unsigned LineNo, LLVMMetadataRef Ty, 1416 LLVMBool AlwaysPreserve, LLVMDIFlags Flags, uint32_t AlignInBits) { 1417 return wrap(unwrap(Builder)->createAutoVariable( 1418 unwrap<DIScope>(Scope), {Name, NameLen}, unwrap<DIFile>(File), 1419 LineNo, unwrap<DIType>(Ty), AlwaysPreserve, 1420 map_from_llvmDIFlags(Flags), AlignInBits)); 1421 } 1422 1423 LLVMMetadataRef LLVMDIBuilderCreateParameterVariable( 1424 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, 1425 size_t NameLen, unsigned ArgNo, LLVMMetadataRef File, unsigned LineNo, 1426 LLVMMetadataRef Ty, LLVMBool AlwaysPreserve, LLVMDIFlags Flags) { 1427 return wrap(unwrap(Builder)->createParameterVariable( 1428 unwrap<DIScope>(Scope), {Name, NameLen}, ArgNo, unwrap<DIFile>(File), 1429 LineNo, unwrap<DIType>(Ty), AlwaysPreserve, 1430 map_from_llvmDIFlags(Flags))); 1431 } 1432 1433 LLVMMetadataRef LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef Builder, 1434 int64_t Lo, int64_t Count) { 1435 return wrap(unwrap(Builder)->getOrCreateSubrange(Lo, Count)); 1436 } 1437 1438 LLVMMetadataRef LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef Builder, 1439 LLVMMetadataRef *Data, 1440 size_t Length) { 1441 Metadata **DataValue = unwrap(Data); 1442 return wrap(unwrap(Builder)->getOrCreateArray({DataValue, Length}).get()); 1443 } 1444 1445 LLVMMetadataRef LLVMGetSubprogram(LLVMValueRef Func) { 1446 return wrap(unwrap<Function>(Func)->getSubprogram()); 1447 } 1448 1449 void LLVMSetSubprogram(LLVMValueRef Func, LLVMMetadataRef SP) { 1450 unwrap<Function>(Func)->setSubprogram(unwrap<DISubprogram>(SP)); 1451 } 1452 1453 unsigned LLVMDISubprogramGetLine(LLVMMetadataRef Subprogram) { 1454 return unwrapDI<DISubprogram>(Subprogram)->getLine(); 1455 } 1456 1457 LLVMMetadataRef LLVMInstructionGetDebugLoc(LLVMValueRef Inst) { 1458 return wrap(unwrap<Instruction>(Inst)->getDebugLoc().getAsMDNode()); 1459 } 1460 1461 void LLVMInstructionSetDebugLoc(LLVMValueRef Inst, LLVMMetadataRef Loc) { 1462 if (Loc) 1463 unwrap<Instruction>(Inst)->setDebugLoc(DebugLoc(unwrap<MDNode>(Loc))); 1464 else 1465 unwrap<Instruction>(Inst)->setDebugLoc(DebugLoc()); 1466 } 1467 1468 LLVMMetadataKind LLVMGetMetadataKind(LLVMMetadataRef Metadata) { 1469 switch(unwrap(Metadata)->getMetadataID()) { 1470 #define HANDLE_METADATA_LEAF(CLASS) \ 1471 case Metadata::CLASS##Kind: \ 1472 return (LLVMMetadataKind)LLVM##CLASS##MetadataKind; 1473 #include "llvm/IR/Metadata.def" 1474 default: 1475 return (LLVMMetadataKind)LLVMGenericDINodeMetadataKind; 1476 } 1477 } 1478