1 //===- DebugInfo.cpp - Debug Information Helper Classes -------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the helper classes used to build and interpret debug 11 // information in LLVM IR form. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "llvm-c/DebugInfo.h" 16 #include "llvm/ADT/DenseMap.h" 17 #include "llvm/ADT/DenseSet.h" 18 #include "llvm/ADT/None.h" 19 #include "llvm/ADT/STLExtras.h" 20 #include "llvm/ADT/SmallPtrSet.h" 21 #include "llvm/ADT/SmallVector.h" 22 #include "llvm/ADT/StringRef.h" 23 #include "llvm/IR/BasicBlock.h" 24 #include "llvm/IR/Constants.h" 25 #include "llvm/IR/DebugInfoMetadata.h" 26 #include "llvm/IR/DebugLoc.h" 27 #include "llvm/IR/DebugInfo.h" 28 #include "llvm/IR/DIBuilder.h" 29 #include "llvm/IR/Function.h" 30 #include "llvm/IR/GVMaterializer.h" 31 #include "llvm/IR/Instruction.h" 32 #include "llvm/IR/IntrinsicInst.h" 33 #include "llvm/IR/LLVMContext.h" 34 #include "llvm/IR/Metadata.h" 35 #include "llvm/IR/Module.h" 36 #include "llvm/Support/Casting.h" 37 #include <algorithm> 38 #include <cassert> 39 #include <utility> 40 41 using namespace llvm; 42 using namespace llvm::dwarf; 43 44 DISubprogram *llvm::getDISubprogram(const MDNode *Scope) { 45 if (auto *LocalScope = dyn_cast_or_null<DILocalScope>(Scope)) 46 return LocalScope->getSubprogram(); 47 return nullptr; 48 } 49 50 //===----------------------------------------------------------------------===// 51 // DebugInfoFinder implementations. 52 //===----------------------------------------------------------------------===// 53 54 void DebugInfoFinder::reset() { 55 CUs.clear(); 56 SPs.clear(); 57 GVs.clear(); 58 TYs.clear(); 59 Scopes.clear(); 60 NodesSeen.clear(); 61 } 62 63 void DebugInfoFinder::processModule(const Module &M) { 64 for (auto *CU : M.debug_compile_units()) { 65 addCompileUnit(CU); 66 for (auto DIG : CU->getGlobalVariables()) { 67 if (!addGlobalVariable(DIG)) 68 continue; 69 auto *GV = DIG->getVariable(); 70 processScope(GV->getScope()); 71 processType(GV->getType().resolve()); 72 } 73 for (auto *ET : CU->getEnumTypes()) 74 processType(ET); 75 for (auto *RT : CU->getRetainedTypes()) 76 if (auto *T = dyn_cast<DIType>(RT)) 77 processType(T); 78 else 79 processSubprogram(cast<DISubprogram>(RT)); 80 for (auto *Import : CU->getImportedEntities()) { 81 auto *Entity = Import->getEntity().resolve(); 82 if (auto *T = dyn_cast<DIType>(Entity)) 83 processType(T); 84 else if (auto *SP = dyn_cast<DISubprogram>(Entity)) 85 processSubprogram(SP); 86 else if (auto *NS = dyn_cast<DINamespace>(Entity)) 87 processScope(NS->getScope()); 88 else if (auto *M = dyn_cast<DIModule>(Entity)) 89 processScope(M->getScope()); 90 } 91 } 92 for (auto &F : M.functions()) { 93 if (auto *SP = cast_or_null<DISubprogram>(F.getSubprogram())) 94 processSubprogram(SP); 95 // There could be subprograms from inlined functions referenced from 96 // instructions only. Walk the function to find them. 97 for (const BasicBlock &BB : F) { 98 for (const Instruction &I : BB) { 99 if (!I.getDebugLoc()) 100 continue; 101 processLocation(M, I.getDebugLoc().get()); 102 } 103 } 104 } 105 } 106 107 void DebugInfoFinder::processLocation(const Module &M, const DILocation *Loc) { 108 if (!Loc) 109 return; 110 processScope(Loc->getScope()); 111 processLocation(M, Loc->getInlinedAt()); 112 } 113 114 void DebugInfoFinder::processType(DIType *DT) { 115 if (!addType(DT)) 116 return; 117 processScope(DT->getScope().resolve()); 118 if (auto *ST = dyn_cast<DISubroutineType>(DT)) { 119 for (DITypeRef Ref : ST->getTypeArray()) 120 processType(Ref.resolve()); 121 return; 122 } 123 if (auto *DCT = dyn_cast<DICompositeType>(DT)) { 124 processType(DCT->getBaseType().resolve()); 125 for (Metadata *D : DCT->getElements()) { 126 if (auto *T = dyn_cast<DIType>(D)) 127 processType(T); 128 else if (auto *SP = dyn_cast<DISubprogram>(D)) 129 processSubprogram(SP); 130 } 131 return; 132 } 133 if (auto *DDT = dyn_cast<DIDerivedType>(DT)) { 134 processType(DDT->getBaseType().resolve()); 135 } 136 } 137 138 void DebugInfoFinder::processScope(DIScope *Scope) { 139 if (!Scope) 140 return; 141 if (auto *Ty = dyn_cast<DIType>(Scope)) { 142 processType(Ty); 143 return; 144 } 145 if (auto *CU = dyn_cast<DICompileUnit>(Scope)) { 146 addCompileUnit(CU); 147 return; 148 } 149 if (auto *SP = dyn_cast<DISubprogram>(Scope)) { 150 processSubprogram(SP); 151 return; 152 } 153 if (!addScope(Scope)) 154 return; 155 if (auto *LB = dyn_cast<DILexicalBlockBase>(Scope)) { 156 processScope(LB->getScope()); 157 } else if (auto *NS = dyn_cast<DINamespace>(Scope)) { 158 processScope(NS->getScope()); 159 } else if (auto *M = dyn_cast<DIModule>(Scope)) { 160 processScope(M->getScope()); 161 } 162 } 163 164 void DebugInfoFinder::processSubprogram(DISubprogram *SP) { 165 if (!addSubprogram(SP)) 166 return; 167 processScope(SP->getScope().resolve()); 168 processType(SP->getType()); 169 for (auto *Element : SP->getTemplateParams()) { 170 if (auto *TType = dyn_cast<DITemplateTypeParameter>(Element)) { 171 processType(TType->getType().resolve()); 172 } else if (auto *TVal = dyn_cast<DITemplateValueParameter>(Element)) { 173 processType(TVal->getType().resolve()); 174 } 175 } 176 } 177 178 void DebugInfoFinder::processDeclare(const Module &M, 179 const DbgDeclareInst *DDI) { 180 auto *N = dyn_cast<MDNode>(DDI->getVariable()); 181 if (!N) 182 return; 183 184 auto *DV = dyn_cast<DILocalVariable>(N); 185 if (!DV) 186 return; 187 188 if (!NodesSeen.insert(DV).second) 189 return; 190 processScope(DV->getScope()); 191 processType(DV->getType().resolve()); 192 } 193 194 void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) { 195 auto *N = dyn_cast<MDNode>(DVI->getVariable()); 196 if (!N) 197 return; 198 199 auto *DV = dyn_cast<DILocalVariable>(N); 200 if (!DV) 201 return; 202 203 if (!NodesSeen.insert(DV).second) 204 return; 205 processScope(DV->getScope()); 206 processType(DV->getType().resolve()); 207 } 208 209 bool DebugInfoFinder::addType(DIType *DT) { 210 if (!DT) 211 return false; 212 213 if (!NodesSeen.insert(DT).second) 214 return false; 215 216 TYs.push_back(const_cast<DIType *>(DT)); 217 return true; 218 } 219 220 bool DebugInfoFinder::addCompileUnit(DICompileUnit *CU) { 221 if (!CU) 222 return false; 223 if (!NodesSeen.insert(CU).second) 224 return false; 225 226 CUs.push_back(CU); 227 return true; 228 } 229 230 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariableExpression *DIG) { 231 if (!NodesSeen.insert(DIG).second) 232 return false; 233 234 GVs.push_back(DIG); 235 return true; 236 } 237 238 bool DebugInfoFinder::addSubprogram(DISubprogram *SP) { 239 if (!SP) 240 return false; 241 242 if (!NodesSeen.insert(SP).second) 243 return false; 244 245 SPs.push_back(SP); 246 return true; 247 } 248 249 bool DebugInfoFinder::addScope(DIScope *Scope) { 250 if (!Scope) 251 return false; 252 // FIXME: Ocaml binding generates a scope with no content, we treat it 253 // as null for now. 254 if (Scope->getNumOperands() == 0) 255 return false; 256 if (!NodesSeen.insert(Scope).second) 257 return false; 258 Scopes.push_back(Scope); 259 return true; 260 } 261 262 static MDNode *stripDebugLocFromLoopID(MDNode *N) { 263 assert(N->op_begin() != N->op_end() && "Missing self reference?"); 264 265 // if there is no debug location, we do not have to rewrite this MDNode. 266 if (std::none_of(N->op_begin() + 1, N->op_end(), [](const MDOperand &Op) { 267 return isa<DILocation>(Op.get()); 268 })) 269 return N; 270 271 // If there is only the debug location without any actual loop metadata, we 272 // can remove the metadata. 273 if (std::none_of(N->op_begin() + 1, N->op_end(), [](const MDOperand &Op) { 274 return !isa<DILocation>(Op.get()); 275 })) 276 return nullptr; 277 278 SmallVector<Metadata *, 4> Args; 279 // Reserve operand 0 for loop id self reference. 280 auto TempNode = MDNode::getTemporary(N->getContext(), None); 281 Args.push_back(TempNode.get()); 282 // Add all non-debug location operands back. 283 for (auto Op = N->op_begin() + 1; Op != N->op_end(); Op++) { 284 if (!isa<DILocation>(*Op)) 285 Args.push_back(*Op); 286 } 287 288 // Set the first operand to itself. 289 MDNode *LoopID = MDNode::get(N->getContext(), Args); 290 LoopID->replaceOperandWith(0, LoopID); 291 return LoopID; 292 } 293 294 bool llvm::stripDebugInfo(Function &F) { 295 bool Changed = false; 296 if (F.getMetadata(LLVMContext::MD_dbg)) { 297 Changed = true; 298 F.setSubprogram(nullptr); 299 } 300 301 DenseMap<MDNode*, MDNode*> LoopIDsMap; 302 for (BasicBlock &BB : F) { 303 for (auto II = BB.begin(), End = BB.end(); II != End;) { 304 Instruction &I = *II++; // We may delete the instruction, increment now. 305 if (isa<DbgInfoIntrinsic>(&I)) { 306 I.eraseFromParent(); 307 Changed = true; 308 continue; 309 } 310 if (I.getDebugLoc()) { 311 Changed = true; 312 I.setDebugLoc(DebugLoc()); 313 } 314 } 315 316 auto *TermInst = BB.getTerminator(); 317 if (!TermInst) 318 // This is invalid IR, but we may not have run the verifier yet 319 continue; 320 if (auto *LoopID = TermInst->getMetadata(LLVMContext::MD_loop)) { 321 auto *NewLoopID = LoopIDsMap.lookup(LoopID); 322 if (!NewLoopID) 323 NewLoopID = LoopIDsMap[LoopID] = stripDebugLocFromLoopID(LoopID); 324 if (NewLoopID != LoopID) 325 TermInst->setMetadata(LLVMContext::MD_loop, NewLoopID); 326 } 327 } 328 return Changed; 329 } 330 331 bool llvm::StripDebugInfo(Module &M) { 332 bool Changed = false; 333 334 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(), 335 NME = M.named_metadata_end(); NMI != NME;) { 336 NamedMDNode *NMD = &*NMI; 337 ++NMI; 338 339 // We're stripping debug info, and without them, coverage information 340 // doesn't quite make sense. 341 if (NMD->getName().startswith("llvm.dbg.") || 342 NMD->getName() == "llvm.gcov") { 343 NMD->eraseFromParent(); 344 Changed = true; 345 } 346 } 347 348 for (Function &F : M) 349 Changed |= stripDebugInfo(F); 350 351 for (auto &GV : M.globals()) { 352 SmallVector<MDNode *, 1> MDs; 353 GV.getMetadata(LLVMContext::MD_dbg, MDs); 354 if (!MDs.empty()) { 355 GV.eraseMetadata(LLVMContext::MD_dbg); 356 Changed = true; 357 } 358 } 359 360 if (GVMaterializer *Materializer = M.getMaterializer()) 361 Materializer->setStripDebugInfo(); 362 363 return Changed; 364 } 365 366 namespace { 367 368 /// Helper class to downgrade -g metadata to -gline-tables-only metadata. 369 class DebugTypeInfoRemoval { 370 DenseMap<Metadata *, Metadata *> Replacements; 371 372 public: 373 /// The (void)() type. 374 MDNode *EmptySubroutineType; 375 376 private: 377 /// Remember what linkage name we originally had before stripping. If we end 378 /// up making two subprograms identical who originally had different linkage 379 /// names, then we need to make one of them distinct, to avoid them getting 380 /// uniqued. Maps the new node to the old linkage name. 381 DenseMap<DISubprogram *, StringRef> NewToLinkageName; 382 383 // TODO: Remember the distinct subprogram we created for a given linkage name, 384 // so that we can continue to unique whenever possible. Map <newly created 385 // node, old linkage name> to the first (possibly distinct) mdsubprogram 386 // created for that combination. This is not strictly needed for correctness, 387 // but can cut down on the number of MDNodes and let us diff cleanly with the 388 // output of -gline-tables-only. 389 390 public: 391 DebugTypeInfoRemoval(LLVMContext &C) 392 : EmptySubroutineType(DISubroutineType::get(C, DINode::FlagZero, 0, 393 MDNode::get(C, {}))) {} 394 395 Metadata *map(Metadata *M) { 396 if (!M) 397 return nullptr; 398 auto Replacement = Replacements.find(M); 399 if (Replacement != Replacements.end()) 400 return Replacement->second; 401 402 return M; 403 } 404 MDNode *mapNode(Metadata *N) { return dyn_cast_or_null<MDNode>(map(N)); } 405 406 /// Recursively remap N and all its referenced children. Does a DF post-order 407 /// traversal, so as to remap bottoms up. 408 void traverseAndRemap(MDNode *N) { traverse(N); } 409 410 private: 411 // Create a new DISubprogram, to replace the one given. 412 DISubprogram *getReplacementSubprogram(DISubprogram *MDS) { 413 auto *FileAndScope = cast_or_null<DIFile>(map(MDS->getFile())); 414 StringRef LinkageName = MDS->getName().empty() ? MDS->getLinkageName() : ""; 415 DISubprogram *Declaration = nullptr; 416 auto *Type = cast_or_null<DISubroutineType>(map(MDS->getType())); 417 DITypeRef ContainingType(map(MDS->getContainingType())); 418 auto *Unit = cast_or_null<DICompileUnit>(map(MDS->getUnit())); 419 auto Variables = nullptr; 420 auto TemplateParams = nullptr; 421 422 // Make a distinct DISubprogram, for situations that warrent it. 423 auto distinctMDSubprogram = [&]() { 424 return DISubprogram::getDistinct( 425 MDS->getContext(), FileAndScope, MDS->getName(), LinkageName, 426 FileAndScope, MDS->getLine(), Type, MDS->isLocalToUnit(), 427 MDS->isDefinition(), MDS->getScopeLine(), ContainingType, 428 MDS->getVirtuality(), MDS->getVirtualIndex(), 429 MDS->getThisAdjustment(), MDS->getFlags(), MDS->isOptimized(), Unit, 430 TemplateParams, Declaration, Variables); 431 }; 432 433 if (MDS->isDistinct()) 434 return distinctMDSubprogram(); 435 436 auto *NewMDS = DISubprogram::get( 437 MDS->getContext(), FileAndScope, MDS->getName(), LinkageName, 438 FileAndScope, MDS->getLine(), Type, MDS->isLocalToUnit(), 439 MDS->isDefinition(), MDS->getScopeLine(), ContainingType, 440 MDS->getVirtuality(), MDS->getVirtualIndex(), MDS->getThisAdjustment(), 441 MDS->getFlags(), MDS->isOptimized(), Unit, TemplateParams, Declaration, 442 Variables); 443 444 StringRef OldLinkageName = MDS->getLinkageName(); 445 446 // See if we need to make a distinct one. 447 auto OrigLinkage = NewToLinkageName.find(NewMDS); 448 if (OrigLinkage != NewToLinkageName.end()) { 449 if (OrigLinkage->second == OldLinkageName) 450 // We're good. 451 return NewMDS; 452 453 // Otherwise, need to make a distinct one. 454 // TODO: Query the map to see if we already have one. 455 return distinctMDSubprogram(); 456 } 457 458 NewToLinkageName.insert({NewMDS, MDS->getLinkageName()}); 459 return NewMDS; 460 } 461 462 /// Create a new compile unit, to replace the one given 463 DICompileUnit *getReplacementCU(DICompileUnit *CU) { 464 // Drop skeleton CUs. 465 if (CU->getDWOId()) 466 return nullptr; 467 468 auto *File = cast_or_null<DIFile>(map(CU->getFile())); 469 MDTuple *EnumTypes = nullptr; 470 MDTuple *RetainedTypes = nullptr; 471 MDTuple *GlobalVariables = nullptr; 472 MDTuple *ImportedEntities = nullptr; 473 return DICompileUnit::getDistinct( 474 CU->getContext(), CU->getSourceLanguage(), File, CU->getProducer(), 475 CU->isOptimized(), CU->getFlags(), CU->getRuntimeVersion(), 476 CU->getSplitDebugFilename(), DICompileUnit::LineTablesOnly, EnumTypes, 477 RetainedTypes, GlobalVariables, ImportedEntities, CU->getMacros(), 478 CU->getDWOId(), CU->getSplitDebugInlining(), 479 CU->getDebugInfoForProfiling(), CU->getGnuPubnames()); 480 } 481 482 DILocation *getReplacementMDLocation(DILocation *MLD) { 483 auto *Scope = map(MLD->getScope()); 484 auto *InlinedAt = map(MLD->getInlinedAt()); 485 if (MLD->isDistinct()) 486 return DILocation::getDistinct(MLD->getContext(), MLD->getLine(), 487 MLD->getColumn(), Scope, InlinedAt); 488 return DILocation::get(MLD->getContext(), MLD->getLine(), MLD->getColumn(), 489 Scope, InlinedAt); 490 } 491 492 /// Create a new generic MDNode, to replace the one given 493 MDNode *getReplacementMDNode(MDNode *N) { 494 SmallVector<Metadata *, 8> Ops; 495 Ops.reserve(N->getNumOperands()); 496 for (auto &I : N->operands()) 497 if (I) 498 Ops.push_back(map(I)); 499 auto *Ret = MDNode::get(N->getContext(), Ops); 500 return Ret; 501 } 502 503 /// Attempt to re-map N to a newly created node. 504 void remap(MDNode *N) { 505 if (Replacements.count(N)) 506 return; 507 508 auto doRemap = [&](MDNode *N) -> MDNode * { 509 if (!N) 510 return nullptr; 511 if (auto *MDSub = dyn_cast<DISubprogram>(N)) { 512 remap(MDSub->getUnit()); 513 return getReplacementSubprogram(MDSub); 514 } 515 if (isa<DISubroutineType>(N)) 516 return EmptySubroutineType; 517 if (auto *CU = dyn_cast<DICompileUnit>(N)) 518 return getReplacementCU(CU); 519 if (isa<DIFile>(N)) 520 return N; 521 if (auto *MDLB = dyn_cast<DILexicalBlockBase>(N)) 522 // Remap to our referenced scope (recursively). 523 return mapNode(MDLB->getScope()); 524 if (auto *MLD = dyn_cast<DILocation>(N)) 525 return getReplacementMDLocation(MLD); 526 527 // Otherwise, if we see these, just drop them now. Not strictly necessary, 528 // but this speeds things up a little. 529 if (isa<DINode>(N)) 530 return nullptr; 531 532 return getReplacementMDNode(N); 533 }; 534 Replacements[N] = doRemap(N); 535 } 536 537 /// Do the remapping traversal. 538 void traverse(MDNode *); 539 }; 540 541 } // end anonymous namespace 542 543 void DebugTypeInfoRemoval::traverse(MDNode *N) { 544 if (!N || Replacements.count(N)) 545 return; 546 547 // To avoid cycles, as well as for efficiency sake, we will sometimes prune 548 // parts of the graph. 549 auto prune = [](MDNode *Parent, MDNode *Child) { 550 if (auto *MDS = dyn_cast<DISubprogram>(Parent)) 551 return Child == MDS->getVariables().get(); 552 return false; 553 }; 554 555 SmallVector<MDNode *, 16> ToVisit; 556 DenseSet<MDNode *> Opened; 557 558 // Visit each node starting at N in post order, and map them. 559 ToVisit.push_back(N); 560 while (!ToVisit.empty()) { 561 auto *N = ToVisit.back(); 562 if (!Opened.insert(N).second) { 563 // Close it. 564 remap(N); 565 ToVisit.pop_back(); 566 continue; 567 } 568 for (auto &I : N->operands()) 569 if (auto *MDN = dyn_cast_or_null<MDNode>(I)) 570 if (!Opened.count(MDN) && !Replacements.count(MDN) && !prune(N, MDN) && 571 !isa<DICompileUnit>(MDN)) 572 ToVisit.push_back(MDN); 573 } 574 } 575 576 bool llvm::stripNonLineTableDebugInfo(Module &M) { 577 bool Changed = false; 578 579 // First off, delete the debug intrinsics. 580 auto RemoveUses = [&](StringRef Name) { 581 if (auto *DbgVal = M.getFunction(Name)) { 582 while (!DbgVal->use_empty()) 583 cast<Instruction>(DbgVal->user_back())->eraseFromParent(); 584 DbgVal->eraseFromParent(); 585 Changed = true; 586 } 587 }; 588 RemoveUses("llvm.dbg.declare"); 589 RemoveUses("llvm.dbg.value"); 590 591 // Delete non-CU debug info named metadata nodes. 592 for (auto NMI = M.named_metadata_begin(), NME = M.named_metadata_end(); 593 NMI != NME;) { 594 NamedMDNode *NMD = &*NMI; 595 ++NMI; 596 // Specifically keep dbg.cu around. 597 if (NMD->getName() == "llvm.dbg.cu") 598 continue; 599 } 600 601 // Drop all dbg attachments from global variables. 602 for (auto &GV : M.globals()) 603 GV.eraseMetadata(LLVMContext::MD_dbg); 604 605 DebugTypeInfoRemoval Mapper(M.getContext()); 606 auto remap = [&](MDNode *Node) -> MDNode * { 607 if (!Node) 608 return nullptr; 609 Mapper.traverseAndRemap(Node); 610 auto *NewNode = Mapper.mapNode(Node); 611 Changed |= Node != NewNode; 612 Node = NewNode; 613 return NewNode; 614 }; 615 616 // Rewrite the DebugLocs to be equivalent to what 617 // -gline-tables-only would have created. 618 for (auto &F : M) { 619 if (auto *SP = F.getSubprogram()) { 620 Mapper.traverseAndRemap(SP); 621 auto *NewSP = cast<DISubprogram>(Mapper.mapNode(SP)); 622 Changed |= SP != NewSP; 623 F.setSubprogram(NewSP); 624 } 625 for (auto &BB : F) { 626 for (auto &I : BB) { 627 auto remapDebugLoc = [&](DebugLoc DL) -> DebugLoc { 628 auto *Scope = DL.getScope(); 629 MDNode *InlinedAt = DL.getInlinedAt(); 630 Scope = remap(Scope); 631 InlinedAt = remap(InlinedAt); 632 return DebugLoc::get(DL.getLine(), DL.getCol(), Scope, InlinedAt); 633 }; 634 635 if (I.getDebugLoc() != DebugLoc()) 636 I.setDebugLoc(remapDebugLoc(I.getDebugLoc())); 637 638 // Remap DILocations in untyped MDNodes (e.g., llvm.loop). 639 SmallVector<std::pair<unsigned, MDNode *>, 2> MDs; 640 I.getAllMetadata(MDs); 641 for (auto Attachment : MDs) 642 if (auto *T = dyn_cast_or_null<MDTuple>(Attachment.second)) 643 for (unsigned N = 0; N < T->getNumOperands(); ++N) 644 if (auto *Loc = dyn_cast_or_null<DILocation>(T->getOperand(N))) 645 if (Loc != DebugLoc()) 646 T->replaceOperandWith(N, remapDebugLoc(Loc)); 647 } 648 } 649 } 650 651 // Create a new llvm.dbg.cu, which is equivalent to the one 652 // -gline-tables-only would have created. 653 for (auto &NMD : M.getNamedMDList()) { 654 SmallVector<MDNode *, 8> Ops; 655 for (MDNode *Op : NMD.operands()) 656 Ops.push_back(remap(Op)); 657 658 if (!Changed) 659 continue; 660 661 NMD.clearOperands(); 662 for (auto *Op : Ops) 663 if (Op) 664 NMD.addOperand(Op); 665 } 666 return Changed; 667 } 668 669 unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) { 670 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>( 671 M.getModuleFlag("Debug Info Version"))) 672 return Val->getZExtValue(); 673 return 0; 674 } 675 676 void Instruction::applyMergedLocation(const DILocation *LocA, 677 const DILocation *LocB) { 678 setDebugLoc(DILocation::getMergedLocation(LocA, LocB, this)); 679 } 680 681 //===----------------------------------------------------------------------===// 682 // LLVM C API implementations. 683 //===----------------------------------------------------------------------===// 684 685 static unsigned map_from_llvmDWARFsourcelanguage(LLVMDWARFSourceLanguage lang) { 686 switch (lang) { 687 #define HANDLE_DW_LANG(ID, NAME, VERSION, VENDOR) \ 688 case LLVMDWARFSourceLanguage##NAME: return ID; 689 #include "llvm/BinaryFormat/Dwarf.def" 690 #undef HANDLE_DW_LANG 691 } 692 llvm_unreachable("Unhandled Tag"); 693 } 694 695 unsigned LLVMDebugMetadataVersion() { 696 return DEBUG_METADATA_VERSION; 697 } 698 699 LLVMDIBuilderRef LLVMCreateDIBuilderDisallowUnresolved(LLVMModuleRef M) { 700 return wrap(new DIBuilder(*unwrap(M), false)); 701 } 702 703 LLVMDIBuilderRef LLVMCreateDIBuilder(LLVMModuleRef M) { 704 return wrap(new DIBuilder(*unwrap(M))); 705 } 706 707 unsigned LLVMGetModuleDebugMetadataVersion(LLVMModuleRef M) { 708 return getDebugMetadataVersionFromModule(*unwrap(M)); 709 } 710 711 LLVMBool LLVMStripModuleDebugInfo(LLVMModuleRef M) { 712 return StripDebugInfo(*unwrap(M)); 713 } 714 715 void LLVMDisposeDIBuilder(LLVMDIBuilderRef Builder) { 716 delete unwrap(Builder); 717 } 718 719 void LLVMDIBuilderFinalize(LLVMDIBuilderRef Builder) { 720 unwrap(Builder)->finalize(); 721 } 722 723 LLVMMetadataRef LLVMDIBuilderCreateCompileUnit( 724 LLVMDIBuilderRef Builder, LLVMDWARFSourceLanguage Lang, 725 LLVMMetadataRef FileRef, const char *Producer, size_t ProducerLen, 726 LLVMBool isOptimized, const char *Flags, size_t FlagsLen, 727 unsigned RuntimeVer, const char *SplitName, size_t SplitNameLen, 728 LLVMDWARFEmissionKind Kind, unsigned DWOId, LLVMBool SplitDebugInlining, 729 LLVMBool DebugInfoForProfiling) { 730 auto File = unwrap<DIFile>(FileRef); 731 732 return wrap(unwrap(Builder)->createCompileUnit( 733 map_from_llvmDWARFsourcelanguage(Lang), File, 734 StringRef(Producer, ProducerLen), isOptimized, 735 StringRef(Flags, FlagsLen), RuntimeVer, 736 StringRef(SplitName, SplitNameLen), 737 static_cast<DICompileUnit::DebugEmissionKind>(Kind), DWOId, 738 SplitDebugInlining, DebugInfoForProfiling)); 739 } 740 741 LLVMMetadataRef 742 LLVMDIBuilderCreateFile(LLVMDIBuilderRef Builder, const char *Filename, 743 size_t FilenameLen, const char *Directory, 744 size_t DirectoryLen) { 745 return wrap(unwrap(Builder)->createFile(StringRef(Filename, FilenameLen), 746 StringRef(Directory, DirectoryLen))); 747 } 748 749 LLVMMetadataRef 750 LLVMDIBuilderCreateDebugLocation(LLVMContextRef Ctx, unsigned Line, 751 unsigned Column, LLVMMetadataRef Scope, 752 LLVMMetadataRef InlinedAt) { 753 return wrap(DILocation::get(*unwrap(Ctx), Line, Column, unwrap(Scope), 754 unwrap(InlinedAt))); 755 } 756