c12cc0c1c94f08bb617a49cc26fc061466fc61e5
[oota-llvm.git] / include / llvm / IR / DebugInfo.h
1 //===- DebugInfo.h - Debug Information Helpers ------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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
13 // DWARF information.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #ifndef LLVM_IR_DEBUGINFO_H
18 #define LLVM_IR_DEBUGINFO_H
19
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"
29 #include <iterator>
30
31 namespace llvm {
32 class Module;
33 class DbgDeclareInst;
34 class DbgValueInst;
35
36 /// \brief Maps from type identifier to the actual MDNode.
37 typedef DenseMap<const MDString *, MDType *> DITypeIdentifierMap;
38
39 typedef DebugNodeArray DIArray;
40 typedef MDTypeRefArray DITypeArray;
41
42 /// \brief Find subprogram that is enclosing this scope.
43 MDSubprogram *getDISubprogram(const MDNode *Scope);
44
45 /// \brief Find debug info for a given function.
46 ///
47 /// \returns a valid subprogram, if found. Otherwise, return \c nullptr.
48 MDSubprogram *getDISubprogram(const Function *F);
49
50 /// \brief Find underlying composite type.
51 MDCompositeTypeBase *getDICompositeType(MDType *T);
52
53 /// \brief Generate map by visiting all retained types.
54 DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
55
56 /// \brief Strip debug info in the module if it exists.
57 ///
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);
63
64 /// \brief Return Debug Info Metadata Version by checking module flags.
65 unsigned getDebugMetadataVersionFromModule(const Module &M);
66
67 /// \brief Utility to find all debug info in a module.
68 ///
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
74 /// used by the CUs.
75 class DebugInfoFinder {
76 public:
77   DebugInfoFinder() : TypeMapInitialized(false) {}
78
79   /// \brief Process entire module and collect debug info anchors.
80   void processModule(const Module &M);
81
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);
88
89   /// \brief Clear all lists.
90   void reset();
91
92 private:
93   void InitializeTypeMap(const Module &M);
94
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);
103
104 public:
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;
112
113   iterator_range<compile_unit_iterator> compile_units() const {
114     return iterator_range<compile_unit_iterator>(CUs.begin(), CUs.end());
115   }
116
117   iterator_range<subprogram_iterator> subprograms() const {
118     return iterator_range<subprogram_iterator>(SPs.begin(), SPs.end());
119   }
120
121   iterator_range<global_variable_iterator> global_variables() const {
122     return iterator_range<global_variable_iterator>(GVs.begin(), GVs.end());
123   }
124
125   iterator_range<type_iterator> types() const {
126     return iterator_range<type_iterator>(TYs.begin(), TYs.end());
127   }
128
129   iterator_range<scope_iterator> scopes() const {
130     return iterator_range<scope_iterator>(Scopes.begin(), Scopes.end());
131   }
132
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(); }
138
139 private:
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;
147
148   /// \brief Specify if TypeIdentifierMap is initialized.
149   bool TypeMapInitialized;
150 };
151
152 DenseMap<const Function *, MDSubprogram *> makeSubprogramMap(const Module &M);
153
154 } // end namespace llvm
155
156 #endif