LLVM  7.0.0svn
DebugInfo.cpp
Go to the documentation of this file.
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"
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 
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 
55  CUs.clear();
56  SPs.clear();
57  GVs.clear();
58  TYs.clear();
59  Scopes.clear();
60  NodesSeen.clear();
61 }
62 
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 
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 
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 
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 
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 
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 
295  bool Changed = false;
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 
332  bool Changed = false;
333 
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()) {
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 {
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 = [&]() {
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;
474  CU->getContext(), CU->getSourceLanguage(), File, CU->getProducer(),
475  CU->isOptimized(), CU->getFlags(), CU->getRuntimeVersion(),
477  RetainedTypes, GlobalVariables, ImportedEntities, CU->getMacros(),
478  CU->getDWOId(), CU->getSplitDebugInlining(),
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) {
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 
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 
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).
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()) {
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 
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 
677  const DILocation *LocB) {
678  setDebugLoc(DILocation::getMergedLocation(LocA, LocB, this));
679 }
680 
681 //===----------------------------------------------------------------------===//
682 // LLVM C API implementations.
683 //===----------------------------------------------------------------------===//
684 
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 
696  return DEBUG_METADATA_VERSION;
697 }
698 
700  return wrap(new DIBuilder(*unwrap(M), false));
701 }
702 
704  return wrap(new DIBuilder(*unwrap(M)));
705 }
706 
709 }
710 
712  return StripDebugInfo(*unwrap(M));
713 }
714 
716  delete unwrap(Builder);
717 }
718 
720  unwrap(Builder)->finalize();
721 }
722 
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(
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 
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 
751  unsigned Column, LLVMMetadataRef Scope,
752  LLVMMetadataRef InlinedAt) {
753  return wrap(DILocation::get(*unwrap(Ctx), Line, Column, unwrap(Scope),
754  unwrap(InlinedAt)));
755 }
const NoneType None
Definition: None.h:24
uint64_t CallInst * C
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks &#39;this&#39; from the containing basic block and deletes it.
Definition: Instruction.cpp:67
void processLocation(const Module &M, const DILocation *Loc)
Process debug info location.
Definition: DebugInfo.cpp:107
Tracking metadata reference owned by Metadata.
Definition: Metadata.h:709
bool isDistinct() const
Definition: Metadata.h:941
DILocation * get() const
Get the underlying DILocation.
Definition: DebugLoc.cpp:21
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
struct LLVMOpaqueModule * LLVMModuleRef
The top-level container for all other LLVM Intermediate Representation (IR) objects.
Definition: Types.h:62
unsigned getRuntimeVersion() const
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
unsigned LLVMGetModuleDebugMetadataVersion(LLVMModuleRef M)
The version of debug metadata that&#39;s present in the provided Module.
Definition: DebugInfo.cpp:707
void replaceOperandWith(unsigned I, Metadata *New)
Replace a specific operand.
Definition: Metadata.cpp:859
Implements a dense probed hash-table based set.
Definition: DenseSet.h:221
DIFile * getFile() const
named_metadata_iterator named_metadata_end()
Definition: Module.h:689
This file contains the declarations for metadata subclasses.
unsigned getDebugMetadataVersionFromModule(const Module &M)
Return Debug Info Metadata Version by checking module flags.
Definition: DebugInfo.cpp:669
void reset()
Clear all lists.
Definition: DebugInfo.cpp:54
uint64_t getDWOId() const
struct LLVMOpaqueDIBuilder * LLVMDIBuilderRef
Represents an LLVM debug info builder.
Definition: Types.h:104
A debug info location.
Definition: DebugLoc.h:34
Metadata node.
Definition: Metadata.h:862
F(f)
bool stripDebugInfo(Function &F)
Definition: DebugInfo.cpp:294
StringRef getProducer() const
void reserve(size_type N)
Definition: SmallVector.h:378
bool getDebugInfoForProfiling() const
op_iterator op_end() const
Definition: Metadata.h:1061
void processModule(const Module &M)
Process entire module and collect debug info anchors.
Definition: DebugInfo.cpp:63
Tuple of metadata.
Definition: Metadata.h:1104
LLVMMetadataRef LLVMDIBuilderCreateCompileUnit(LLVMDIBuilderRef Builder, LLVMDWARFSourceLanguage Lang, LLVMMetadataRef FileRef, const char *Producer, size_t ProducerLen, LLVMBool isOptimized, const char *Flags, size_t FlagsLen, unsigned RuntimeVer, const char *SplitName, size_t SplitNameLen, LLVMDWARFEmissionKind Kind, unsigned DWOId, LLVMBool SplitDebugInlining, LLVMBool DebugInfoForProfiling)
A CompileUnit provides an anchor for all debugging information generated during this instance of comp...
Definition: DebugInfo.cpp:723
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:191
LLVMDWARFEmissionKind
The amount of debug information to emit.
Definition: DebugInfo.h:118
StringRef getSplitDebugFilename() const
A tuple of MDNodes.
Definition: Metadata.h:1323
StringRef getFlags() const
Attribute unwrap(LLVMAttributeRef Attr)
Definition: Attributes.h:195
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:237
void eraseFromParent()
Drop all references and remove the node from parent module.
Definition: Metadata.cpp:1094
MDNode * getMetadata(unsigned KindID) const
Get the current metadata attachments for the given kind, if any.
Definition: Metadata.cpp:1439
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:828
static TempMDTuple getTemporary(LLVMContext &Context, ArrayRef< Metadata *> MDs)
Definition: Metadata.h:1176
bool stripNonLineTableDebugInfo(Module &M)
Downgrade the debug info in a module to contain only line table information.
Definition: DebugInfo.cpp:576
Holds a subclass of DINode.
DISubprogram * getDISubprogram(const MDNode *Scope)
Find subprogram that is enclosing this scope.
Definition: DebugInfo.cpp:44
op_iterator op_begin() const
Definition: Metadata.h:1057
Subprogram description.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:267
LLVMDIBuilderRef LLVMCreateDIBuilder(LLVMModuleRef M)
Construct a builder for a module and collect unresolved nodes attached to the module in order to reso...
Definition: DebugInfo.cpp:703
op_range operands() const
Definition: Metadata.h:1065
LLVMContext & getContext() const
Definition: Metadata.h:922
struct LLVMOpaqueContext * LLVMContextRef
The top-level container for all LLVM global data.
Definition: Types.h:54
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
bool StripDebugInfo(Module &M)
Strip debug info in the module if it exists.
Definition: DebugInfo.cpp:331
Debug location.
LLVMDIBuilderRef LLVMCreateDIBuilderDisallowUnresolved(LLVMModuleRef M)
Construct a builder for a module, and do not allow for unresolved nodes attached to the module...
Definition: DebugInfo.cpp:699
DIMacroNodeArray getMacros() const
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata *> MDs)
Definition: Metadata.h:1164
void setSubprogram(DISubprogram *SP)
Set the attached subprogram.
Definition: Metadata.cpp:1501
iterator_range< iterator > functions()
Definition: Module.h:583
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
Definition: Instruction.h:282
LLVMMetadataRef LLVMDIBuilderCreateDebugLocation(LLVMContextRef Ctx, unsigned Line, unsigned Column, LLVMMetadataRef Scope, LLVMMetadataRef InlinedAt)
Creates a new DebugLocation that describes a source location.
Definition: DebugInfo.cpp:750
Import information from summary.
LLVM Basic Block Representation.
Definition: BasicBlock.h:59
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:69
Metadata * getModuleFlag(StringRef Key) const
Return the corresponding value if Key appears in module flags, otherwise return null.
Definition: Module.cpp:302
static MDTuple * getDistinct(LLVMContext &Context, ArrayRef< Metadata *> MDs)
Definition: Metadata.h:1172
This file contains the declarations for the subclasses of Constant, which represent the different fla...
unsigned LLVMDebugMetadataVersion()
The current debug metadata version number.
Definition: DebugInfo.cpp:695
A pair of DIGlobalVariable and DIExpression.
DILocalVariable * getVariable() const
Definition: IntrinsicInst.h:80
static const unsigned End
LLVMDWARFSourceLanguage
Source languages known by DWARF.
Definition: DebugInfo.h:68
void processValue(const Module &M, const DbgValueInst *DVI)
Process DbgValueInst.
Definition: DebugInfo.cpp:194
int LLVMBool
Definition: Types.h:29
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Base class for scope-like contexts.
static const DILocation * getMergedLocation(const DILocation *LocA, const DILocation *LocB, const Instruction *ForInst=nullptr)
When two instructions are combined into a single instruction we also need to combine the original loc...
Iterator for intrusive lists based on ilist_node.
LLVMMetadataRef LLVMDIBuilderCreateFile(LLVMDIBuilderRef Builder, const char *Filename, size_t FilenameLen, const char *Directory, size_t DirectoryLen)
Create a file descriptor to hold debugging information for a file.
Definition: DebugInfo.cpp:742
Base class for types.
StringRef getName() const
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:862
Module.h This file contains the declarations for the Module class.
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
void applyMergedLocation(const DILocation *LocA, const DILocation *LocB)
Merge 2 debug locations and apply it to the Instruction.
Definition: DebugInfo.cpp:676
DISubprogram * getSubprogram() const
Get the subprogram for this scope.
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
Definition: Module.cpp:172
unsigned getSourceLanguage() const
void LLVMDIBuilderFinalize(LLVMDIBuilderRef Builder)
Construct any deferred debug info descriptors.
Definition: DebugInfo.cpp:719
StringRef getName() const
Definition: Metadata.cpp:1098
bool getSplitDebugInlining() const
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:285
LLVMAttributeRef wrap(Attribute Attr)
Definition: Attributes.h:190
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:61
void LLVMDisposeDIBuilder(LLVMDIBuilderRef Builder)
Deallocates the DIBuilder and everything it owns.
Definition: DebugInfo.cpp:715
This represents the llvm.dbg.value instruction.
void processDeclare(const Module &M, const DbgDeclareInst *DDI)
Process DbgDeclareInst.
Definition: DebugInfo.cpp:178
#define I(x, y, z)
Definition: MD5.cpp:58
#define N
iterator_range< debug_compile_units_iterator > debug_compile_units() const
Return an iterator for all DICompileUnits listed in this Module&#39;s llvm.dbg.cu named metadata node and...
Definition: Module.h:755
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:323
static unsigned map_from_llvmDWARFsourcelanguage(LLVMDWARFSourceLanguage lang)
Definition: DebugInfo.cpp:685
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
Definition: DenseSet.h:91
LLVMBool LLVMStripModuleDebugInfo(LLVMModuleRef M)
Strip debug info in the module if it exists.
Definition: DebugInfo.cpp:711
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition: DenseMap.h:141
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition: DenseMap.h:181
const unsigned Kind
struct LLVMOpaqueMetadata * LLVMMetadataRef
Represents an LLVM Metadata.
Definition: Types.h:90
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static MDNode * stripDebugLocFromLoopID(MDNode *N)
Definition: DebugInfo.cpp:262
DIScopeRef getScope() const
The access may reference the value stored in memory.
iterator_range< global_iterator > globals()
Definition: Module.h:561
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
unsigned getNumOperands() const
Return number of MDNode operands.
Definition: Metadata.h:1073
This represents the llvm.dbg.declare instruction.
Root of the metadata hierarchy.
Definition: Metadata.h:58
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
named_metadata_iterator named_metadata_begin()
Definition: Module.h:684
DIScopeRef getScope() const