DebugInfo.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- DebugInfo.h - Debug Information Helpers ------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines a bunch of datatypes that are useful for creating and
  15. // walking debug info in LLVM IR form. They essentially provide wrappers around
  16. // the information in the global variables that's needed when constructing the
  17. // DWARF information.
  18. //
  19. //===----------------------------------------------------------------------===//
  20. #ifndef LLVM_IR_DEBUGINFO_H
  21. #define LLVM_IR_DEBUGINFO_H
  22. #include "llvm/ADT/STLExtras.h"
  23. #include "llvm/ADT/SmallPtrSet.h"
  24. #include "llvm/ADT/SmallVector.h"
  25. #include "llvm/ADT/iterator_range.h"
  26. #include "llvm/IR/DebugInfoMetadata.h"
  27. namespace llvm {
  28. class DbgVariableIntrinsic;
  29. class Instruction;
  30. class Module;
  31. /// Find subprogram that is enclosing this scope.
  32. DISubprogram *getDISubprogram(const MDNode *Scope);
  33. /// Strip debug info in the module if it exists.
  34. ///
  35. /// To do this, we remove all calls to the debugger intrinsics and any named
  36. /// metadata for debugging. We also remove debug locations for instructions.
  37. /// Return true if module is modified.
  38. bool StripDebugInfo(Module &M);
  39. bool stripDebugInfo(Function &F);
  40. /// Downgrade the debug info in a module to contain only line table information.
  41. ///
  42. /// In order to convert debug info to what -gline-tables-only would have
  43. /// created, this does the following:
  44. /// 1) Delete all debug intrinsics.
  45. /// 2) Delete all non-CU named metadata debug info nodes.
  46. /// 3) Create new DebugLocs for each instruction.
  47. /// 4) Create a new CU debug info, and similarly for every metadata node
  48. /// that's reachable from the CU debug info.
  49. /// All debug type metadata nodes are unreachable and garbage collected.
  50. bool stripNonLineTableDebugInfo(Module &M);
  51. /// Update the debug locations contained within the MD_loop metadata attached
  52. /// to the instruction \p I, if one exists. \p Updater is applied to each debug
  53. /// location in the MD_loop metadata: the returned value is included in the
  54. /// updated loop metadata node if it is non-null.
  55. void updateLoopMetadataDebugLocations(
  56. Instruction &I, function_ref<DILocation *(const DILocation &)> Updater);
  57. /// Return Debug Info Metadata Version by checking module flags.
  58. unsigned getDebugMetadataVersionFromModule(const Module &M);
  59. /// Utility to find all debug info in a module.
  60. ///
  61. /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
  62. /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
  63. /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
  64. /// DbgValueInst and DbgLoc attached to instructions. processModule will go
  65. /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
  66. /// used by the CUs.
  67. class DebugInfoFinder {
  68. public:
  69. /// Process entire module and collect debug info anchors.
  70. void processModule(const Module &M);
  71. /// Process a single instruction and collect debug info anchors.
  72. void processInstruction(const Module &M, const Instruction &I);
  73. /// Process DbgVariableIntrinsic.
  74. void processVariable(const Module &M, const DbgVariableIntrinsic &DVI);
  75. /// Process debug info location.
  76. void processLocation(const Module &M, const DILocation *Loc);
  77. /// Clear all lists.
  78. void reset();
  79. private:
  80. void InitializeTypeMap(const Module &M);
  81. void processCompileUnit(DICompileUnit *CU);
  82. void processScope(DIScope *Scope);
  83. void processSubprogram(DISubprogram *SP);
  84. void processType(DIType *DT);
  85. bool addCompileUnit(DICompileUnit *CU);
  86. bool addGlobalVariable(DIGlobalVariableExpression *DIG);
  87. bool addScope(DIScope *Scope);
  88. bool addSubprogram(DISubprogram *SP);
  89. bool addType(DIType *DT);
  90. public:
  91. using compile_unit_iterator =
  92. SmallVectorImpl<DICompileUnit *>::const_iterator;
  93. using subprogram_iterator = SmallVectorImpl<DISubprogram *>::const_iterator;
  94. using global_variable_expression_iterator =
  95. SmallVectorImpl<DIGlobalVariableExpression *>::const_iterator;
  96. using type_iterator = SmallVectorImpl<DIType *>::const_iterator;
  97. using scope_iterator = SmallVectorImpl<DIScope *>::const_iterator;
  98. iterator_range<compile_unit_iterator> compile_units() const {
  99. return make_range(CUs.begin(), CUs.end());
  100. }
  101. iterator_range<subprogram_iterator> subprograms() const {
  102. return make_range(SPs.begin(), SPs.end());
  103. }
  104. iterator_range<global_variable_expression_iterator> global_variables() const {
  105. return make_range(GVs.begin(), GVs.end());
  106. }
  107. iterator_range<type_iterator> types() const {
  108. return make_range(TYs.begin(), TYs.end());
  109. }
  110. iterator_range<scope_iterator> scopes() const {
  111. return make_range(Scopes.begin(), Scopes.end());
  112. }
  113. unsigned compile_unit_count() const { return CUs.size(); }
  114. unsigned global_variable_count() const { return GVs.size(); }
  115. unsigned subprogram_count() const { return SPs.size(); }
  116. unsigned type_count() const { return TYs.size(); }
  117. unsigned scope_count() const { return Scopes.size(); }
  118. private:
  119. SmallVector<DICompileUnit *, 8> CUs;
  120. SmallVector<DISubprogram *, 8> SPs;
  121. SmallVector<DIGlobalVariableExpression *, 8> GVs;
  122. SmallVector<DIType *, 8> TYs;
  123. SmallVector<DIScope *, 8> Scopes;
  124. SmallPtrSet<const MDNode *, 32> NodesSeen;
  125. };
  126. } // end namespace llvm
  127. #endif // LLVM_IR_DEBUGINFO_H
  128. #ifdef __GNUC__
  129. #pragma GCC diagnostic pop
  130. #endif