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/DenseMap.h"
21 #include "llvm/ADT/SmallPtrSet.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/ADT/iterator_range.h"
25 #include "llvm/IR/DebugInfoMetadata.h"
26 #include "llvm/Support/Casting.h"
27 #include "llvm/Support/Dwarf.h"
28 #include "llvm/Support/ErrorHandling.h"
36 /// \brief Maps from type identifier to the actual MDNode.
37 typedef DenseMap<const MDString *, MDType *> DITypeIdentifierMap;
39 typedef DebugNodeArray DIArray;
40 typedef MDTypeRefArray DITypeArray;
42 /// \brief Find subprogram that is enclosing this scope.
43 MDSubprogram *getDISubprogram(const MDNode *Scope);
45 /// \brief Find debug info for a given function.
47 /// \returns a valid subprogram, if found. Otherwise, return \c nullptr.
48 MDSubprogram *getDISubprogram(const Function *F);
50 /// \brief Find underlying composite type.
51 MDCompositeTypeBase *getDICompositeType(MDType *T);
53 /// \brief Generate map by visiting all retained types.
54 DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
56 /// \brief Strip debug info in the module if it exists.
58 /// To do this, we remove all calls to the debugger intrinsics and any named
59 /// metadata for debugging. We also remove debug locations for instructions.
60 /// Return true if module is modified.
61 bool StripDebugInfo(Module &M);
62 bool stripDebugInfo(Function &F);
64 /// \brief Return Debug Info Metadata Version by checking module flags.
65 unsigned getDebugMetadataVersionFromModule(const Module &M);
67 /// \brief Utility to find all debug info in a module.
69 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
70 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
71 /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
72 /// DbgValueInst and DbgLoc attached to instructions. processModule will go
73 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
75 class DebugInfoFinder {
77 DebugInfoFinder() : TypeMapInitialized(false) {}
79 /// \brief Process entire module and collect debug info anchors.
80 void processModule(const Module &M);
82 /// \brief Process DbgDeclareInst.
83 void processDeclare(const Module &M, const DbgDeclareInst *DDI);
84 /// \brief Process DbgValueInst.
85 void processValue(const Module &M, const DbgValueInst *DVI);
86 /// \brief Process debug info location.
87 void processLocation(const Module &M, const MDLocation *Loc);
89 /// \brief Clear all lists.
93 void InitializeTypeMap(const Module &M);
95 void processType(MDType *DT);
96 void processSubprogram(MDSubprogram *SP);
97 void processScope(MDScope *Scope);
98 bool addCompileUnit(MDCompileUnit *CU);
99 bool addGlobalVariable(MDGlobalVariable *DIG);
100 bool addSubprogram(MDSubprogram *SP);
101 bool addType(MDType *DT);
102 bool addScope(MDScope *Scope);
105 typedef SmallVectorImpl<MDCompileUnit *>::const_iterator
106 compile_unit_iterator;
107 typedef SmallVectorImpl<MDSubprogram *>::const_iterator subprogram_iterator;
108 typedef SmallVectorImpl<MDGlobalVariable *>::const_iterator
109 global_variable_iterator;
110 typedef SmallVectorImpl<MDType *>::const_iterator type_iterator;
111 typedef SmallVectorImpl<MDScope *>::const_iterator scope_iterator;
113 iterator_range<compile_unit_iterator> compile_units() const {
114 return iterator_range<compile_unit_iterator>(CUs.begin(), CUs.end());
117 iterator_range<subprogram_iterator> subprograms() const {
118 return iterator_range<subprogram_iterator>(SPs.begin(), SPs.end());
121 iterator_range<global_variable_iterator> global_variables() const {
122 return iterator_range<global_variable_iterator>(GVs.begin(), GVs.end());
125 iterator_range<type_iterator> types() const {
126 return iterator_range<type_iterator>(TYs.begin(), TYs.end());
129 iterator_range<scope_iterator> scopes() const {
130 return iterator_range<scope_iterator>(Scopes.begin(), Scopes.end());
133 unsigned compile_unit_count() const { return CUs.size(); }
134 unsigned global_variable_count() const { return GVs.size(); }
135 unsigned subprogram_count() const { return SPs.size(); }
136 unsigned type_count() const { return TYs.size(); }
137 unsigned scope_count() const { return Scopes.size(); }
140 SmallVector<MDCompileUnit *, 8> CUs;
141 SmallVector<MDSubprogram *, 8> SPs;
142 SmallVector<MDGlobalVariable *, 8> GVs;
143 SmallVector<MDType *, 8> TYs;
144 SmallVector<MDScope *, 8> Scopes;
145 SmallPtrSet<const MDNode *, 64> NodesSeen;
146 DITypeIdentifierMap TypeIdentifierMap;
148 /// \brief Specify if TypeIdentifierMap is initialized.
149 bool TypeMapInitialized;
152 DenseMap<const Function *, MDSubprogram *> makeSubprogramMap(const Module &M);
154 } // end namespace llvm