DebugInfo.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. bool hasDebugInfo(const Module &M); // HLSL Change - Helper function to check if there's real debug info (variables, types)
  54. /// \brief Utility to find all debug info in a module.
  55. ///
  56. /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
  57. /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
  58. /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
  59. /// DbgValueInst and DbgLoc attached to instructions. processModule will go
  60. /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
  61. /// used by the CUs.
  62. class DebugInfoFinder {
  63. public:
  64. DebugInfoFinder() : TypeMapInitialized(false) {}
  65. /// \brief Process entire module and collect debug info anchors.
  66. void processModule(const Module &M);
  67. /// \brief Process DbgDeclareInst.
  68. void processDeclare(const Module &M, const DbgDeclareInst *DDI);
  69. /// \brief Process DbgValueInst.
  70. void processValue(const Module &M, const DbgValueInst *DVI);
  71. /// \brief Process debug info location.
  72. void processLocation(const Module &M, const DILocation *Loc);
  73. /// \brief Clear all lists.
  74. void reset();
  75. // HLSL Change Begins.
  76. /// \brief Append new global variable.
  77. bool appendGlobalVariable(DIGlobalVariable *DIG);
  78. // HLSL Change Ends.
  79. private:
  80. void InitializeTypeMap(const Module &M);
  81. void processType(DIType *DT);
  82. void processSubprogram(DISubprogram *SP);
  83. void processScope(DIScope *Scope);
  84. bool addCompileUnit(DICompileUnit *CU);
  85. bool addGlobalVariable(DIGlobalVariable *DIG);
  86. bool addSubprogram(DISubprogram *SP);
  87. bool addType(DIType *DT);
  88. bool addScope(DIScope *Scope);
  89. public:
  90. typedef SmallVectorImpl<DICompileUnit *>::const_iterator
  91. compile_unit_iterator;
  92. typedef SmallVectorImpl<DISubprogram *>::const_iterator subprogram_iterator;
  93. typedef SmallVectorImpl<DIGlobalVariable *>::const_iterator
  94. global_variable_iterator;
  95. typedef SmallVectorImpl<DIType *>::const_iterator type_iterator;
  96. typedef SmallVectorImpl<DIScope *>::const_iterator scope_iterator;
  97. iterator_range<compile_unit_iterator> compile_units() const {
  98. return iterator_range<compile_unit_iterator>(CUs.begin(), CUs.end());
  99. }
  100. iterator_range<subprogram_iterator> subprograms() const {
  101. return iterator_range<subprogram_iterator>(SPs.begin(), SPs.end());
  102. }
  103. iterator_range<global_variable_iterator> global_variables() const {
  104. return iterator_range<global_variable_iterator>(GVs.begin(), GVs.end());
  105. }
  106. iterator_range<type_iterator> types() const {
  107. return iterator_range<type_iterator>(TYs.begin(), TYs.end());
  108. }
  109. iterator_range<scope_iterator> scopes() const {
  110. return iterator_range<scope_iterator>(Scopes.begin(), Scopes.end());
  111. }
  112. unsigned compile_unit_count() const { return CUs.size(); }
  113. unsigned global_variable_count() const { return GVs.size(); }
  114. unsigned subprogram_count() const { return SPs.size(); }
  115. unsigned type_count() const { return TYs.size(); }
  116. unsigned scope_count() const { return Scopes.size(); }
  117. private:
  118. SmallVector<DICompileUnit *, 8> CUs;
  119. SmallVector<DISubprogram *, 8> SPs;
  120. SmallVector<DIGlobalVariable *, 8> GVs;
  121. SmallVector<DIType *, 8> TYs;
  122. SmallVector<DIScope *, 8> Scopes;
  123. SmallPtrSet<const MDNode *, 64> NodesSeen;
  124. DITypeIdentifierMap TypeIdentifierMap;
  125. /// \brief Specify if TypeIdentifierMap is initialized.
  126. bool TypeMapInitialized;
  127. };
  128. DenseMap<const Function *, DISubprogram *> makeSubprogramMap(const Module &M);
  129. } // end namespace llvm
  130. #endif