DebugInfo.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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/SmallSet.h"
  25. #include "llvm/ADT/SmallVector.h"
  26. #include "llvm/ADT/TinyPtrVector.h"
  27. #include "llvm/ADT/iterator_range.h"
  28. #include "llvm/IR/DataLayout.h"
  29. #include "llvm/IR/IntrinsicInst.h"
  30. #include "llvm/IR/PassManager.h"
  31. #include <optional>
  32. namespace llvm {
  33. class DbgDeclareInst;
  34. class DbgValueInst;
  35. class DbgVariableIntrinsic;
  36. class Instruction;
  37. class Module;
  38. /// Finds all intrinsics declaring local variables as living in the memory that
  39. /// 'V' points to. This may include a mix of dbg.declare and
  40. /// dbg.addr intrinsics.
  41. TinyPtrVector<DbgVariableIntrinsic *> FindDbgAddrUses(Value *V);
  42. /// Like \c FindDbgAddrUses, but only returns dbg.declare intrinsics, not
  43. /// dbg.addr.
  44. TinyPtrVector<DbgDeclareInst *> FindDbgDeclareUses(Value *V);
  45. /// Finds the llvm.dbg.value intrinsics describing a value.
  46. void findDbgValues(SmallVectorImpl<DbgValueInst *> &DbgValues, Value *V);
  47. /// Finds the debug info intrinsics describing a value.
  48. void findDbgUsers(SmallVectorImpl<DbgVariableIntrinsic *> &DbgInsts, Value *V);
  49. /// Find subprogram that is enclosing this scope.
  50. DISubprogram *getDISubprogram(const MDNode *Scope);
  51. /// Produce a DebugLoc to use for each dbg.declare that is promoted to a
  52. /// dbg.value.
  53. DebugLoc getDebugValueLoc(DbgVariableIntrinsic *DII);
  54. /// Strip debug info in the module if it exists.
  55. ///
  56. /// To do this, we remove all calls to the debugger intrinsics and any named
  57. /// metadata for debugging. We also remove debug locations for instructions.
  58. /// Return true if module is modified.
  59. bool StripDebugInfo(Module &M);
  60. bool stripDebugInfo(Function &F);
  61. /// Downgrade the debug info in a module to contain only line table information.
  62. ///
  63. /// In order to convert debug info to what -gline-tables-only would have
  64. /// created, this does the following:
  65. /// 1) Delete all debug intrinsics.
  66. /// 2) Delete all non-CU named metadata debug info nodes.
  67. /// 3) Create new DebugLocs for each instruction.
  68. /// 4) Create a new CU debug info, and similarly for every metadata node
  69. /// that's reachable from the CU debug info.
  70. /// All debug type metadata nodes are unreachable and garbage collected.
  71. bool stripNonLineTableDebugInfo(Module &M);
  72. /// Update the debug locations contained within the MD_loop metadata attached
  73. /// to the instruction \p I, if one exists. \p Updater is applied to Metadata
  74. /// operand in the MD_loop metadata: the returned value is included in the
  75. /// updated loop metadata node if it is non-null.
  76. void updateLoopMetadataDebugLocations(
  77. Instruction &I, function_ref<Metadata *(Metadata *)> Updater);
  78. /// Return Debug Info Metadata Version by checking module flags.
  79. unsigned getDebugMetadataVersionFromModule(const Module &M);
  80. /// Utility to find all debug info in a module.
  81. ///
  82. /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
  83. /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
  84. /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
  85. /// DbgValueInst and DbgLoc attached to instructions. processModule will go
  86. /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
  87. /// used by the CUs.
  88. class DebugInfoFinder {
  89. public:
  90. /// Process entire module and collect debug info anchors.
  91. void processModule(const Module &M);
  92. /// Process a single instruction and collect debug info anchors.
  93. void processInstruction(const Module &M, const Instruction &I);
  94. /// Process DbgVariableIntrinsic.
  95. void processVariable(const Module &M, const DbgVariableIntrinsic &DVI);
  96. /// Process debug info location.
  97. void processLocation(const Module &M, const DILocation *Loc);
  98. /// Process subprogram.
  99. void processSubprogram(DISubprogram *SP);
  100. /// Clear all lists.
  101. void reset();
  102. private:
  103. void processCompileUnit(DICompileUnit *CU);
  104. void processScope(DIScope *Scope);
  105. void processType(DIType *DT);
  106. bool addCompileUnit(DICompileUnit *CU);
  107. bool addGlobalVariable(DIGlobalVariableExpression *DIG);
  108. bool addScope(DIScope *Scope);
  109. bool addSubprogram(DISubprogram *SP);
  110. bool addType(DIType *DT);
  111. public:
  112. using compile_unit_iterator =
  113. SmallVectorImpl<DICompileUnit *>::const_iterator;
  114. using subprogram_iterator = SmallVectorImpl<DISubprogram *>::const_iterator;
  115. using global_variable_expression_iterator =
  116. SmallVectorImpl<DIGlobalVariableExpression *>::const_iterator;
  117. using type_iterator = SmallVectorImpl<DIType *>::const_iterator;
  118. using scope_iterator = SmallVectorImpl<DIScope *>::const_iterator;
  119. iterator_range<compile_unit_iterator> compile_units() const {
  120. return make_range(CUs.begin(), CUs.end());
  121. }
  122. iterator_range<subprogram_iterator> subprograms() const {
  123. return make_range(SPs.begin(), SPs.end());
  124. }
  125. iterator_range<global_variable_expression_iterator> global_variables() const {
  126. return make_range(GVs.begin(), GVs.end());
  127. }
  128. iterator_range<type_iterator> types() const {
  129. return make_range(TYs.begin(), TYs.end());
  130. }
  131. iterator_range<scope_iterator> scopes() const {
  132. return make_range(Scopes.begin(), Scopes.end());
  133. }
  134. unsigned compile_unit_count() const { return CUs.size(); }
  135. unsigned global_variable_count() const { return GVs.size(); }
  136. unsigned subprogram_count() const { return SPs.size(); }
  137. unsigned type_count() const { return TYs.size(); }
  138. unsigned scope_count() const { return Scopes.size(); }
  139. private:
  140. SmallVector<DICompileUnit *, 8> CUs;
  141. SmallVector<DISubprogram *, 8> SPs;
  142. SmallVector<DIGlobalVariableExpression *, 8> GVs;
  143. SmallVector<DIType *, 8> TYs;
  144. SmallVector<DIScope *, 8> Scopes;
  145. SmallPtrSet<const MDNode *, 32> NodesSeen;
  146. };
  147. /// Assignment Tracking (at).
  148. namespace at {
  149. //
  150. // Utilities for enumerating storing instructions from an assignment ID.
  151. //
  152. /// A range of instructions.
  153. using AssignmentInstRange =
  154. iterator_range<SmallVectorImpl<Instruction *>::iterator>;
  155. /// Return a range of instructions (typically just one) that have \p ID
  156. /// as an attachment.
  157. /// Iterators invalidated by adding or removing DIAssignID metadata to/from any
  158. /// instruction (including by deleting or cloning instructions).
  159. AssignmentInstRange getAssignmentInsts(DIAssignID *ID);
  160. /// Return a range of instructions (typically just one) that perform the
  161. /// assignment that \p DAI encodes.
  162. /// Iterators invalidated by adding or removing DIAssignID metadata to/from any
  163. /// instruction (including by deleting or cloning instructions).
  164. inline AssignmentInstRange getAssignmentInsts(const DbgAssignIntrinsic *DAI) {
  165. return getAssignmentInsts(DAI->getAssignID());
  166. }
  167. //
  168. // Utilities for enumerating llvm.dbg.assign intrinsic from an assignment ID.
  169. //
  170. /// High level: this is an iterator for llvm.dbg.assign intrinsics.
  171. /// Implementation details: this is a wrapper around Value's User iterator that
  172. /// dereferences to a DbgAssignIntrinsic ptr rather than a User ptr.
  173. class DbgAssignIt
  174. : public iterator_adaptor_base<DbgAssignIt, Value::user_iterator,
  175. typename std::iterator_traits<
  176. Value::user_iterator>::iterator_category,
  177. DbgAssignIntrinsic *, std::ptrdiff_t,
  178. DbgAssignIntrinsic **,
  179. DbgAssignIntrinsic *&> {
  180. public:
  181. DbgAssignIt(Value::user_iterator It) : iterator_adaptor_base(It) {}
  182. DbgAssignIntrinsic *operator*() const { return cast<DbgAssignIntrinsic>(*I); }
  183. };
  184. /// A range of llvm.dbg.assign intrinsics.
  185. using AssignmentMarkerRange = iterator_range<DbgAssignIt>;
  186. /// Return a range of dbg.assign intrinsics which use \ID as an operand.
  187. /// Iterators invalidated by deleting an intrinsic contained in this range.
  188. AssignmentMarkerRange getAssignmentMarkers(DIAssignID *ID);
  189. /// Return a range of dbg.assign intrinsics for which \p Inst performs the
  190. /// assignment they encode.
  191. /// Iterators invalidated by deleting an intrinsic contained in this range.
  192. inline AssignmentMarkerRange getAssignmentMarkers(const Instruction *Inst) {
  193. if (auto *ID = Inst->getMetadata(LLVMContext::MD_DIAssignID))
  194. return getAssignmentMarkers(cast<DIAssignID>(ID));
  195. else
  196. return make_range(Value::user_iterator(), Value::user_iterator());
  197. }
  198. /// Delete the llvm.dbg.assign intrinsics linked to \p Inst.
  199. void deleteAssignmentMarkers(const Instruction *Inst);
  200. /// Replace all uses (and attachments) of \p Old with \p New.
  201. void RAUW(DIAssignID *Old, DIAssignID *New);
  202. /// Remove all Assignment Tracking related intrinsics and metadata from \p F.
  203. void deleteAll(Function *F);
  204. /// Helper struct for trackAssignments, below. We don't use the similar
  205. /// DebugVariable class because trackAssignments doesn't (yet?) understand
  206. /// partial variables (fragment info) as input and want to make that clear and
  207. /// explicit using types. In addition, eventually we will want to understand
  208. /// expressions that modify the base address too, which a DebugVariable doesn't
  209. /// capture.
  210. struct VarRecord {
  211. DILocalVariable *Var;
  212. DILocation *DL;
  213. VarRecord(DbgVariableIntrinsic *DVI)
  214. : Var(DVI->getVariable()), DL(getDebugValueLoc(DVI)) {}
  215. VarRecord(DILocalVariable *Var, DILocation *DL) : Var(Var), DL(DL) {}
  216. friend bool operator<(const VarRecord &LHS, const VarRecord &RHS) {
  217. return std::tie(LHS.Var, LHS.DL) < std::tie(RHS.Var, RHS.DL);
  218. }
  219. friend bool operator==(const VarRecord &LHS, const VarRecord &RHS) {
  220. return std::tie(LHS.Var, LHS.DL) == std::tie(RHS.Var, RHS.DL);
  221. }
  222. };
  223. /// Map of backing storage to a set of variables that are stored to it.
  224. /// TODO: Backing storage shouldn't be limited to allocas only. Some local
  225. /// variables have their storage allocated by the calling function (addresses
  226. /// passed in with sret & byval parameters).
  227. using StorageToVarsMap = DenseMap<const AllocaInst *, SmallSet<VarRecord, 2>>;
  228. /// Track assignments to \p Vars between \p Start and \p End.
  229. void trackAssignments(Function::iterator Start, Function::iterator End,
  230. const StorageToVarsMap &Vars, const DataLayout &DL,
  231. bool DebugPrints = false);
  232. /// Describes properties of a store that has a static size and offset into a
  233. /// some base storage. Used by the getAssignmentInfo functions.
  234. struct AssignmentInfo {
  235. AllocaInst const *Base; ///< Base storage.
  236. uint64_t OffsetInBits; ///< Offset into Base.
  237. uint64_t SizeInBits; ///< Number of bits stored.
  238. bool StoreToWholeAlloca; ///< SizeInBits equals the size of the base storage.
  239. AssignmentInfo(const DataLayout &DL, AllocaInst const *Base,
  240. uint64_t OffsetInBits, uint64_t SizeInBits)
  241. : Base(Base), OffsetInBits(OffsetInBits), SizeInBits(SizeInBits),
  242. StoreToWholeAlloca(
  243. OffsetInBits == 0 &&
  244. SizeInBits == DL.getTypeSizeInBits(Base->getAllocatedType())) {}
  245. };
  246. std::optional<AssignmentInfo> getAssignmentInfo(const DataLayout &DL,
  247. const MemIntrinsic *I);
  248. std::optional<AssignmentInfo> getAssignmentInfo(const DataLayout &DL,
  249. const StoreInst *SI);
  250. std::optional<AssignmentInfo> getAssignmentInfo(const DataLayout &DL,
  251. const AllocaInst *AI);
  252. } // end namespace at
  253. /// Convert @llvm.dbg.declare intrinsics into sets of @llvm.dbg.assign
  254. /// intrinsics by treating stores to the dbg.declare'd address as assignments
  255. /// to the variable. Not all kinds of variables are supported yet; those will
  256. /// be left with their dbg.declare intrinsics.
  257. /// The pass sets the debug-info-assignment-tracking module flag to true to
  258. /// indicate assignment tracking has been enabled.
  259. class AssignmentTrackingPass : public PassInfoMixin<AssignmentTrackingPass> {
  260. /// Note: this method does not set the debug-info-assignment-tracking module
  261. /// flag.
  262. void runOnFunction(Function &F);
  263. public:
  264. PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
  265. PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
  266. };
  267. /// Return true if assignment tracking is enabled for module \p M.
  268. bool isAssignmentTrackingEnabled(const Module &M);
  269. } // end namespace llvm
  270. #endif // LLVM_IR_DEBUGINFO_H
  271. #ifdef __GNUC__
  272. #pragma GCC diagnostic pop
  273. #endif