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