DebugInfo.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //===- DebugInfo.h - Debug Information Helpers ------------------*- C++ -*-===//
  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 defines a bunch of datatypes that are useful for creating and
  11. // walking debug info in LLVM IR form. They essentially provide wrappers around
  12. // the information in the global variables that's needed when constructing the
  13. // DWARF information.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #ifndef LLVM_IR_DEBUGINFO_H
  17. #define LLVM_IR_DEBUGINFO_H
  18. #include "llvm/ADT/DenseMap.h"
  19. #include "llvm/ADT/SmallPtrSet.h"
  20. #include "llvm/ADT/SmallVector.h"
  21. #include "llvm/ADT/StringRef.h"
  22. #include "llvm/ADT/iterator_range.h"
  23. #include "llvm/IR/DebugInfoMetadata.h"
  24. #include "llvm/Support/Casting.h"
  25. #include "llvm/Support/Dwarf.h"
  26. #include "llvm/Support/ErrorHandling.h"
  27. #include <iterator>
  28. namespace llvm {
  29. class Module;
  30. class DbgDeclareInst;
  31. class DbgValueInst;
  32. /// \brief Maps from type identifier to the actual MDNode.
  33. typedef DenseMap<const MDString *, DIType *> DITypeIdentifierMap;
  34. /// \brief Find subprogram that is enclosing this scope.
  35. DISubprogram *getDISubprogram(const MDNode *Scope);
  36. /// \brief Find debug info for a given function.
  37. ///
  38. /// \returns a valid subprogram, if found. Otherwise, return \c nullptr.
  39. DISubprogram *getDISubprogram(const Function *F);
  40. /// \brief Find underlying composite type.
  41. DICompositeTypeBase *getDICompositeType(DIType *T);
  42. /// \brief Generate map by visiting all retained types.
  43. DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
  44. /// \brief Strip debug info in the module if it exists.
  45. ///
  46. /// To do this, we remove all calls to the debugger intrinsics and any named
  47. /// metadata for debugging. We also remove debug locations for instructions.
  48. /// Return true if module is modified.
  49. bool StripDebugInfo(Module &M);
  50. bool stripDebugInfo(Function &F);
  51. /// \brief Return Debug Info Metadata Version by checking module flags.
  52. unsigned getDebugMetadataVersionFromModule(const Module &M);
  53. /// \brief Utility to find all debug info in a module.
  54. ///
  55. /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
  56. /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
  57. /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
  58. /// DbgValueInst and DbgLoc attached to instructions. processModule will go
  59. /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
  60. /// used by the CUs.
  61. class DebugInfoFinder {
  62. public:
  63. DebugInfoFinder() : TypeMapInitialized(false) {}
  64. /// \brief Process entire module and collect debug info anchors.
  65. void processModule(const Module &M);
  66. /// \brief Process DbgDeclareInst.
  67. void processDeclare(const Module &M, const DbgDeclareInst *DDI);
  68. /// \brief Process DbgValueInst.
  69. void processValue(const Module &M, const DbgValueInst *DVI);
  70. /// \brief Process debug info location.
  71. void processLocation(const Module &M, const DILocation *Loc);
  72. /// \brief Clear all lists.
  73. void reset();
  74. // HLSL Change Begins.
  75. /// \brief Append new global variable.
  76. bool appendGlobalVariable(DIGlobalVariable *DIG);
  77. // HLSL Change Ends.
  78. private:
  79. void InitializeTypeMap(const Module &M);
  80. void processType(DIType *DT);
  81. void processSubprogram(DISubprogram *SP);
  82. void processScope(DIScope *Scope);
  83. bool addCompileUnit(DICompileUnit *CU);
  84. bool addGlobalVariable(DIGlobalVariable *DIG);
  85. bool addSubprogram(DISubprogram *SP);
  86. bool addType(DIType *DT);
  87. bool addScope(DIScope *Scope);
  88. public:
  89. typedef SmallVectorImpl<DICompileUnit *>::const_iterator
  90. compile_unit_iterator;
  91. typedef SmallVectorImpl<DISubprogram *>::const_iterator subprogram_iterator;
  92. typedef SmallVectorImpl<DIGlobalVariable *>::const_iterator
  93. global_variable_iterator;
  94. typedef SmallVectorImpl<DIType *>::const_iterator type_iterator;
  95. typedef SmallVectorImpl<DIScope *>::const_iterator scope_iterator;
  96. iterator_range<compile_unit_iterator> compile_units() const {
  97. return iterator_range<compile_unit_iterator>(CUs.begin(), CUs.end());
  98. }
  99. iterator_range<subprogram_iterator> subprograms() const {
  100. return iterator_range<subprogram_iterator>(SPs.begin(), SPs.end());
  101. }
  102. iterator_range<global_variable_iterator> global_variables() const {
  103. return iterator_range<global_variable_iterator>(GVs.begin(), GVs.end());
  104. }
  105. iterator_range<type_iterator> types() const {
  106. return iterator_range<type_iterator>(TYs.begin(), TYs.end());
  107. }
  108. iterator_range<scope_iterator> scopes() const {
  109. return iterator_range<scope_iterator>(Scopes.begin(), Scopes.end());
  110. }
  111. unsigned compile_unit_count() const { return CUs.size(); }
  112. unsigned global_variable_count() const { return GVs.size(); }
  113. unsigned subprogram_count() const { return SPs.size(); }
  114. unsigned type_count() const { return TYs.size(); }
  115. unsigned scope_count() const { return Scopes.size(); }
  116. private:
  117. SmallVector<DICompileUnit *, 8> CUs;
  118. SmallVector<DISubprogram *, 8> SPs;
  119. SmallVector<DIGlobalVariable *, 8> GVs;
  120. SmallVector<DIType *, 8> TYs;
  121. SmallVector<DIScope *, 8> Scopes;
  122. SmallPtrSet<const MDNode *, 64> NodesSeen;
  123. DITypeIdentifierMap TypeIdentifierMap;
  124. /// \brief Specify if TypeIdentifierMap is initialized.
  125. bool TypeMapInitialized;
  126. };
  127. DenseMap<const Function *, DISubprogram *> makeSubprogramMap(const Module &M);
  128. } // end namespace llvm
  129. #endif