1 //===- DebugInfo.h - Debug Information Helpers ------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_IR_DEBUGINFO_H
18 #define LLVM_IR_DEBUGINFO_H
20 #include "llvm/ADT/SmallPtrSet.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/iterator_range.h"
23 #include "llvm/IR/DebugInfoMetadata.h"
31 /// \brief Find subprogram that is enclosing this scope.
32 DISubprogram *getDISubprogram(const MDNode *Scope);
34 /// \brief Strip debug info in the module if it exists.
36 /// To do this, we remove all calls to the debugger intrinsics and any named
37 /// metadata for debugging. We also remove debug locations for instructions.
38 /// Return true if module is modified.
39 bool StripDebugInfo(Module &M);
40 bool stripDebugInfo(Function &F);
42 /// Downgrade the debug info in a module to contain only line table information.
44 /// In order to convert debug info to what -gline-tables-only would have
45 /// created, this does the following:
46 /// 1) Delete all debug intrinsics.
47 /// 2) Delete all non-CU named metadata debug info nodes.
48 /// 3) Create new DebugLocs for each instruction.
49 /// 4) Create a new CU debug info, and similarly for every metadata node
50 /// that's reachable from the CU debug info.
51 /// All debug type metadata nodes are unreachable and garbage collected.
52 bool stripNonLineTableDebugInfo(Module &M);
54 /// \brief Return Debug Info Metadata Version by checking module flags.
55 unsigned getDebugMetadataVersionFromModule(const Module &M);
57 /// \brief Utility to find all debug info in a module.
59 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
60 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
61 /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
62 /// DbgValueInst and DbgLoc attached to instructions. processModule will go
63 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
65 class DebugInfoFinder {
67 /// \brief Process entire module and collect debug info anchors.
68 void processModule(const Module &M);
70 /// \brief Process DbgDeclareInst.
71 void processDeclare(const Module &M, const DbgDeclareInst *DDI);
72 /// \brief Process DbgValueInst.
73 void processValue(const Module &M, const DbgValueInst *DVI);
74 /// \brief Process debug info location.
75 void processLocation(const Module &M, const DILocation *Loc);
77 /// \brief Clear all lists.
81 void InitializeTypeMap(const Module &M);
83 void processType(DIType *DT);
84 void processSubprogram(DISubprogram *SP);
85 void processScope(DIScope *Scope);
86 bool addCompileUnit(DICompileUnit *CU);
87 bool addGlobalVariable(DIGlobalVariableExpression *DIG);
88 bool addSubprogram(DISubprogram *SP);
89 bool addType(DIType *DT);
90 bool addScope(DIScope *Scope);
93 using compile_unit_iterator =
94 SmallVectorImpl<DICompileUnit *>::const_iterator;
95 using subprogram_iterator = SmallVectorImpl<DISubprogram *>::const_iterator;
96 using global_variable_expression_iterator =
97 SmallVectorImpl<DIGlobalVariableExpression *>::const_iterator;
98 using type_iterator = SmallVectorImpl<DIType *>::const_iterator;
99 using scope_iterator = SmallVectorImpl<DIScope *>::const_iterator;
101 iterator_range<compile_unit_iterator> compile_units() const {
102 return make_range(CUs.begin(), CUs.end());
105 iterator_range<subprogram_iterator> subprograms() const {
106 return make_range(SPs.begin(), SPs.end());
109 iterator_range<global_variable_expression_iterator> global_variables() const {
110 return make_range(GVs.begin(), GVs.end());
113 iterator_range<type_iterator> types() const {
114 return make_range(TYs.begin(), TYs.end());
117 iterator_range<scope_iterator> scopes() const {
118 return make_range(Scopes.begin(), Scopes.end());
121 unsigned compile_unit_count() const { return CUs.size(); }
122 unsigned global_variable_count() const { return GVs.size(); }
123 unsigned subprogram_count() const { return SPs.size(); }
124 unsigned type_count() const { return TYs.size(); }
125 unsigned scope_count() const { return Scopes.size(); }
128 SmallVector<DICompileUnit *, 8> CUs;
129 SmallVector<DISubprogram *, 8> SPs;
130 SmallVector<DIGlobalVariableExpression *, 8> GVs;
131 SmallVector<DIType *, 8> TYs;
132 SmallVector<DIScope *, 8> Scopes;
133 SmallPtrSet<const MDNode *, 32> NodesSeen;
136 } // end namespace llvm
138 #endif // LLVM_IR_DEBUGINFO_H