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"
52 /// \brief Maps from type identifier to the actual MDNode.
53 typedef DenseMap<const MDString *, MDNode *> DITypeIdentifierMap;
55 #define DECLARE_SIMPLIFY_DESCRIPTOR(DESC) \
57 template <> struct simplify_type<const DESC>; \
58 template <> struct simplify_type<DESC>;
59 DECLARE_SIMPLIFY_DESCRIPTOR(DISubrange)
60 DECLARE_SIMPLIFY_DESCRIPTOR(DIEnumerator)
61 DECLARE_SIMPLIFY_DESCRIPTOR(DITemplateTypeParameter)
62 DECLARE_SIMPLIFY_DESCRIPTOR(DITemplateValueParameter)
63 DECLARE_SIMPLIFY_DESCRIPTOR(DIGlobalVariable)
64 DECLARE_SIMPLIFY_DESCRIPTOR(DIVariable)
65 DECLARE_SIMPLIFY_DESCRIPTOR(DIExpression)
66 DECLARE_SIMPLIFY_DESCRIPTOR(DILocation)
67 DECLARE_SIMPLIFY_DESCRIPTOR(DIObjCProperty)
68 DECLARE_SIMPLIFY_DESCRIPTOR(DIImportedEntity)
69 #undef DECLARE_SIMPLIFY_DESCRIPTOR
71 typedef DebugNodeArray DIArray;
72 typedef MDTypeRefArray DITypeArray;
78 DISubrange(const MDSubrange *N = nullptr) : N(const_cast<MDSubrange *>(N)) {}
80 operator MDSubrange *() const { return N; }
81 MDSubrange *operator->() const { return N; }
82 MDSubrange &operator*() const { return *N; }
89 DIEnumerator(const MDEnumerator *N = nullptr)
90 : N(const_cast<MDEnumerator *>(N)) {}
92 operator MDEnumerator *() const { return N; }
93 MDEnumerator *operator->() const { return N; }
94 MDEnumerator &operator*() const { return *N; }
97 class DITemplateTypeParameter {
98 MDTemplateTypeParameter *N;
101 DITemplateTypeParameter(const MDTemplateTypeParameter *N = nullptr)
102 : N(const_cast<MDTemplateTypeParameter *>(N)) {}
104 operator MDTemplateTypeParameter *() const { return N; }
105 MDTemplateTypeParameter *operator->() const { return N; }
106 MDTemplateTypeParameter &operator*() const { return *N; }
109 class DITemplateValueParameter {
110 MDTemplateValueParameter *N;
113 DITemplateValueParameter(const MDTemplateValueParameter *N = nullptr)
114 : N(const_cast<MDTemplateValueParameter *>(N)) {}
116 operator MDTemplateValueParameter *() const { return N; }
117 MDTemplateValueParameter *operator->() const { return N; }
118 MDTemplateValueParameter &operator*() const { return *N; }
121 class DIGlobalVariable {
125 DIGlobalVariable(const MDGlobalVariable *N = nullptr)
126 : N(const_cast<MDGlobalVariable *>(N)) {}
128 operator MDGlobalVariable *() const { return N; }
129 MDGlobalVariable *operator->() const { return N; }
130 MDGlobalVariable &operator*() const { return *N; }
137 DIVariable(const MDLocalVariable *N = nullptr)
138 : N(const_cast<MDLocalVariable *>(N)) {}
140 operator MDLocalVariable *() const { return N; }
141 MDLocalVariable *operator->() const { return N; }
142 MDLocalVariable &operator*() const { return *N; }
149 DIExpression(const MDExpression *N = nullptr)
150 : N(const_cast<MDExpression *>(N)) {}
152 operator MDExpression *() const { return N; }
153 MDExpression *operator->() const { return N; }
154 MDExpression &operator*() const { return *N; }
161 DILocation(const MDLocation *N = nullptr) : N(const_cast<MDLocation *>(N)) {}
163 operator MDLocation *() const { return N; }
164 MDLocation *operator->() const { return N; }
165 MDLocation &operator*() const { return *N; }
168 class DIObjCProperty {
172 DIObjCProperty(const MDObjCProperty *N = nullptr)
173 : N(const_cast<MDObjCProperty *>(N)) {}
175 operator MDObjCProperty *() const { return N; }
176 MDObjCProperty *operator->() const { return N; }
177 MDObjCProperty &operator*() const { return *N; }
180 class DIImportedEntity {
184 DIImportedEntity(const MDImportedEntity *N = nullptr)
185 : N(const_cast<MDImportedEntity *>(N)) {}
187 operator MDImportedEntity *() const { return N; }
188 MDImportedEntity *operator->() const { return N; }
189 MDImportedEntity &operator*() const { return *N; }
192 #define SIMPLIFY_DESCRIPTOR(DESC) \
193 template <> struct simplify_type<const DESC> { \
194 typedef Metadata *SimpleType; \
195 static SimpleType getSimplifiedValue(const DESC &DI) { return DI; } \
197 template <> struct simplify_type<DESC> : simplify_type<const DESC> {};
198 SIMPLIFY_DESCRIPTOR(DISubrange)
199 SIMPLIFY_DESCRIPTOR(DIEnumerator)
200 SIMPLIFY_DESCRIPTOR(DITemplateTypeParameter)
201 SIMPLIFY_DESCRIPTOR(DITemplateValueParameter)
202 SIMPLIFY_DESCRIPTOR(DIGlobalVariable)
203 SIMPLIFY_DESCRIPTOR(DIVariable)
204 SIMPLIFY_DESCRIPTOR(DIExpression)
205 SIMPLIFY_DESCRIPTOR(DILocation)
206 SIMPLIFY_DESCRIPTOR(DIObjCProperty)
207 SIMPLIFY_DESCRIPTOR(DIImportedEntity)
208 #undef SIMPLIFY_DESCRIPTOR
210 /// \brief Find subprogram that is enclosing this scope.
211 MDSubprogram *getDISubprogram(const MDNode *Scope);
213 /// \brief Find debug info for a given function.
215 /// \returns a valid subprogram, if found. Otherwise, return \c nullptr.
216 MDSubprogram *getDISubprogram(const Function *F);
218 /// \brief Find underlying composite type.
219 MDCompositeTypeBase *getDICompositeType(MDType *T);
221 /// \brief Generate map by visiting all retained types.
222 DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
224 /// \brief Strip debug info in the module if it exists.
226 /// To do this, we remove all calls to the debugger intrinsics and any named
227 /// metadata for debugging. We also remove debug locations for instructions.
228 /// Return true if module is modified.
229 bool StripDebugInfo(Module &M);
230 bool stripDebugInfo(Function &F);
232 /// \brief Return Debug Info Metadata Version by checking module flags.
233 unsigned getDebugMetadataVersionFromModule(const Module &M);
235 /// \brief Utility to find all debug info in a module.
237 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
238 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
239 /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
240 /// DbgValueInst and DbgLoc attached to instructions. processModule will go
241 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
243 class DebugInfoFinder {
245 DebugInfoFinder() : TypeMapInitialized(false) {}
247 /// \brief Process entire module and collect debug info anchors.
248 void processModule(const Module &M);
250 /// \brief Process DbgDeclareInst.
251 void processDeclare(const Module &M, const DbgDeclareInst *DDI);
252 /// \brief Process DbgValueInst.
253 void processValue(const Module &M, const DbgValueInst *DVI);
254 /// \brief Process DILocation.
255 void processLocation(const Module &M, const MDLocation *Loc);
257 /// \brief Clear all lists.
261 void InitializeTypeMap(const Module &M);
263 void processType(MDType *DT);
264 void processSubprogram(MDSubprogram *SP);
265 void processScope(MDScope *Scope);
266 bool addCompileUnit(MDCompileUnit *CU);
267 bool addGlobalVariable(MDGlobalVariable *DIG);
268 bool addSubprogram(MDSubprogram *SP);
269 bool addType(MDType *DT);
270 bool addScope(MDScope *Scope);
273 typedef SmallVectorImpl<MDCompileUnit *>::const_iterator
274 compile_unit_iterator;
275 typedef SmallVectorImpl<MDSubprogram *>::const_iterator subprogram_iterator;
276 typedef SmallVectorImpl<MDGlobalVariable *>::const_iterator
277 global_variable_iterator;
278 typedef SmallVectorImpl<MDType *>::const_iterator type_iterator;
279 typedef SmallVectorImpl<MDScope *>::const_iterator scope_iterator;
281 iterator_range<compile_unit_iterator> compile_units() const {
282 return iterator_range<compile_unit_iterator>(CUs.begin(), CUs.end());
285 iterator_range<subprogram_iterator> subprograms() const {
286 return iterator_range<subprogram_iterator>(SPs.begin(), SPs.end());
289 iterator_range<global_variable_iterator> global_variables() const {
290 return iterator_range<global_variable_iterator>(GVs.begin(), GVs.end());
293 iterator_range<type_iterator> types() const {
294 return iterator_range<type_iterator>(TYs.begin(), TYs.end());
297 iterator_range<scope_iterator> scopes() const {
298 return iterator_range<scope_iterator>(Scopes.begin(), Scopes.end());
301 unsigned compile_unit_count() const { return CUs.size(); }
302 unsigned global_variable_count() const { return GVs.size(); }
303 unsigned subprogram_count() const { return SPs.size(); }
304 unsigned type_count() const { return TYs.size(); }
305 unsigned scope_count() const { return Scopes.size(); }
308 SmallVector<MDCompileUnit *, 8> CUs;
309 SmallVector<MDSubprogram *, 8> SPs;
310 SmallVector<MDGlobalVariable *, 8> GVs;
311 SmallVector<MDType *, 8> TYs;
312 SmallVector<MDScope *, 8> Scopes;
313 SmallPtrSet<const MDNode *, 64> NodesSeen;
314 DITypeIdentifierMap TypeIdentifierMap;
316 /// \brief Specify if TypeIdentifierMap is initialized.
317 bool TypeMapInitialized;
320 DenseMap<const Function *, MDSubprogram *> makeSubprogramMap(const Module &M);
322 } // end namespace llvm