DebugInfo.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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/TinyPtrVector.h"
  26. #include "llvm/ADT/iterator_range.h"
  27. #include "llvm/IR/DebugInfoMetadata.h"
  28. namespace llvm {
  29. class DbgDeclareInst;
  30. class DbgValueInst;
  31. class DbgVariableIntrinsic;
  32. class Instruction;
  33. class Module;
  34. /// Finds all intrinsics declaring local variables as living in the memory that
  35. /// 'V' points to. This may include a mix of dbg.declare and
  36. /// dbg.addr intrinsics.
  37. TinyPtrVector<DbgVariableIntrinsic *> FindDbgAddrUses(Value *V);
  38. /// Like \c FindDbgAddrUses, but only returns dbg.declare intrinsics, not
  39. /// dbg.addr.
  40. TinyPtrVector<DbgDeclareInst *> FindDbgDeclareUses(Value *V);
  41. /// Finds the llvm.dbg.value intrinsics describing a value.
  42. void findDbgValues(SmallVectorImpl<DbgValueInst *> &DbgValues, Value *V);
  43. /// Finds the debug info intrinsics describing a value.
  44. void findDbgUsers(SmallVectorImpl<DbgVariableIntrinsic *> &DbgInsts, Value *V);
  45. /// Find subprogram that is enclosing this scope.
  46. DISubprogram *getDISubprogram(const MDNode *Scope);
  47. /// Strip debug info in the module if it exists.
  48. ///
  49. /// To do this, we remove all calls to the debugger intrinsics and any named
  50. /// metadata for debugging. We also remove debug locations for instructions.
  51. /// Return true if module is modified.
  52. bool StripDebugInfo(Module &M);
  53. bool stripDebugInfo(Function &F);
  54. /// Downgrade the debug info in a module to contain only line table information.
  55. ///
  56. /// In order to convert debug info to what -gline-tables-only would have
  57. /// created, this does the following:
  58. /// 1) Delete all debug intrinsics.
  59. /// 2) Delete all non-CU named metadata debug info nodes.
  60. /// 3) Create new DebugLocs for each instruction.
  61. /// 4) Create a new CU debug info, and similarly for every metadata node
  62. /// that's reachable from the CU debug info.
  63. /// All debug type metadata nodes are unreachable and garbage collected.
  64. bool stripNonLineTableDebugInfo(Module &M);
  65. /// Update the debug locations contained within the MD_loop metadata attached
  66. /// to the instruction \p I, if one exists. \p Updater is applied to Metadata
  67. /// operand in the MD_loop metadata: the returned value is included in the
  68. /// updated loop metadata node if it is non-null.
  69. void updateLoopMetadataDebugLocations(
  70. Instruction &I, function_ref<Metadata *(Metadata *)> Updater);
  71. /// Return Debug Info Metadata Version by checking module flags.
  72. unsigned getDebugMetadataVersionFromModule(const Module &M);
  73. /// Utility to find all debug info in a module.
  74. ///
  75. /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
  76. /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
  77. /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
  78. /// DbgValueInst and DbgLoc attached to instructions. processModule will go
  79. /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
  80. /// used by the CUs.
  81. class DebugInfoFinder {
  82. public:
  83. /// Process entire module and collect debug info anchors.
  84. void processModule(const Module &M);
  85. /// Process a single instruction and collect debug info anchors.
  86. void processInstruction(const Module &M, const Instruction &I);
  87. /// Process DbgVariableIntrinsic.
  88. void processVariable(const Module &M, const DbgVariableIntrinsic &DVI);
  89. /// Process debug info location.
  90. void processLocation(const Module &M, const DILocation *Loc);
  91. /// Process subprogram.
  92. void processSubprogram(DISubprogram *SP);
  93. /// Clear all lists.
  94. void reset();
  95. private:
  96. void processCompileUnit(DICompileUnit *CU);
  97. void processScope(DIScope *Scope);
  98. void processType(DIType *DT);
  99. bool addCompileUnit(DICompileUnit *CU);
  100. bool addGlobalVariable(DIGlobalVariableExpression *DIG);
  101. bool addScope(DIScope *Scope);
  102. bool addSubprogram(DISubprogram *SP);
  103. bool addType(DIType *DT);
  104. public:
  105. using compile_unit_iterator =
  106. SmallVectorImpl<DICompileUnit *>::const_iterator;
  107. using subprogram_iterator = SmallVectorImpl<DISubprogram *>::const_iterator;
  108. using global_variable_expression_iterator =
  109. SmallVectorImpl<DIGlobalVariableExpression *>::const_iterator;
  110. using type_iterator = SmallVectorImpl<DIType *>::const_iterator;
  111. using scope_iterator = SmallVectorImpl<DIScope *>::const_iterator;
  112. iterator_range<compile_unit_iterator> compile_units() const {
  113. return make_range(CUs.begin(), CUs.end());
  114. }
  115. iterator_range<subprogram_iterator> subprograms() const {
  116. return make_range(SPs.begin(), SPs.end());
  117. }
  118. iterator_range<global_variable_expression_iterator> global_variables() const {
  119. return make_range(GVs.begin(), GVs.end());
  120. }
  121. iterator_range<type_iterator> types() const {
  122. return make_range(TYs.begin(), TYs.end());
  123. }
  124. iterator_range<scope_iterator> scopes() const {
  125. return make_range(Scopes.begin(), Scopes.end());
  126. }
  127. unsigned compile_unit_count() const { return CUs.size(); }
  128. unsigned global_variable_count() const { return GVs.size(); }
  129. unsigned subprogram_count() const { return SPs.size(); }
  130. unsigned type_count() const { return TYs.size(); }
  131. unsigned scope_count() const { return Scopes.size(); }
  132. private:
  133. SmallVector<DICompileUnit *, 8> CUs;
  134. SmallVector<DISubprogram *, 8> SPs;
  135. SmallVector<DIGlobalVariableExpression *, 8> GVs;
  136. SmallVector<DIType *, 8> TYs;
  137. SmallVector<DIScope *, 8> Scopes;
  138. SmallPtrSet<const MDNode *, 32> NodesSeen;
  139. };
  140. } // end namespace llvm
  141. #endif // LLVM_IR_DEBUGINFO_H
  142. #ifdef __GNUC__
  143. #pragma GCC diagnostic pop
  144. #endif