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 *, DIType *> DITypeIdentifierMap;
39 /// \brief Find subprogram that is enclosing this scope.
40 DISubprogram *getDISubprogram(const MDNode *Scope);
42 /// \brief Find debug info for a given function.
44 /// \returns a valid subprogram, if found. Otherwise, return \c nullptr.
45 DISubprogram *getDISubprogram(const Function *F);
47 /// \brief Generate map by visiting all retained types.
48 DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
50 /// \brief Strip debug info in the module if it exists.
52 /// To do this, we remove all calls to the debugger intrinsics and any named
53 /// metadata for debugging. We also remove debug locations for instructions.
54 /// Return true if module is modified.
55 bool StripDebugInfo(Module &M);
56 bool stripDebugInfo(Function &F);
58 /// \brief Return Debug Info Metadata Version by checking module flags.
59 unsigned getDebugMetadataVersionFromModule(const Module &M);
61 /// \brief Utility to find all debug info in a module.
63 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
64 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
65 /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
66 /// DbgValueInst and DbgLoc attached to instructions. processModule will go
67 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
69 class DebugInfoFinder {
71 DebugInfoFinder() : TypeMapInitialized(false) {}
73 /// \brief Process entire module and collect debug info anchors.
74 void processModule(const Module &M);
76 /// \brief Process DbgDeclareInst.
77 void processDeclare(const Module &M, const DbgDeclareInst *DDI);
78 /// \brief Process DbgValueInst.
79 void processValue(const Module &M, const DbgValueInst *DVI);
80 /// \brief Process debug info location.
81 void processLocation(const Module &M, const DILocation *Loc);
83 /// \brief Clear all lists.
87 void InitializeTypeMap(const Module &M);
89 void processType(DIType *DT);
90 void processSubprogram(DISubprogram *SP);
91 void processScope(DIScope *Scope);
92 bool addCompileUnit(DICompileUnit *CU);
93 bool addGlobalVariable(DIGlobalVariable *DIG);
94 bool addSubprogram(DISubprogram *SP);
95 bool addType(DIType *DT);
96 bool addScope(DIScope *Scope);
99 typedef SmallVectorImpl<DICompileUnit *>::const_iterator
100 compile_unit_iterator;
101 typedef SmallVectorImpl<DISubprogram *>::const_iterator subprogram_iterator;
102 typedef SmallVectorImpl<DIGlobalVariable *>::const_iterator
103 global_variable_iterator;
104 typedef SmallVectorImpl<DIType *>::const_iterator type_iterator;
105 typedef SmallVectorImpl<DIScope *>::const_iterator scope_iterator;
107 iterator_range<compile_unit_iterator> compile_units() const {
108 return iterator_range<compile_unit_iterator>(CUs.begin(), CUs.end());
111 iterator_range<subprogram_iterator> subprograms() const {
112 return iterator_range<subprogram_iterator>(SPs.begin(), SPs.end());
115 iterator_range<global_variable_iterator> global_variables() const {
116 return iterator_range<global_variable_iterator>(GVs.begin(), GVs.end());
119 iterator_range<type_iterator> types() const {
120 return iterator_range<type_iterator>(TYs.begin(), TYs.end());
123 iterator_range<scope_iterator> scopes() const {
124 return iterator_range<scope_iterator>(Scopes.begin(), Scopes.end());
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(); }
134 SmallVector<DICompileUnit *, 8> CUs;
135 SmallVector<DISubprogram *, 8> SPs;
136 SmallVector<DIGlobalVariable *, 8> GVs;
137 SmallVector<DIType *, 8> TYs;
138 SmallVector<DIScope *, 8> Scopes;
139 SmallPtrSet<const MDNode *, 64> NodesSeen;
140 DITypeIdentifierMap TypeIdentifierMap;
142 /// \brief Specify if TypeIdentifierMap is initialized.
143 bool TypeMapInitialized;
146 DenseMap<const Function *, DISubprogram *> makeSubprogramMap(const Module &M);
148 } // end namespace llvm