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 class DILexicalBlockFile;
56 /// \brief Maps from type identifier to the actual MDNode.
57 typedef DenseMap<const MDString *, MDNode *> DITypeIdentifierMap;
59 #define DECLARE_SIMPLIFY_DESCRIPTOR(DESC) \
61 template <> struct simplify_type<const DESC>; \
62 template <> struct simplify_type<DESC>;
63 DECLARE_SIMPLIFY_DESCRIPTOR(DISubrange)
64 DECLARE_SIMPLIFY_DESCRIPTOR(DIEnumerator)
65 DECLARE_SIMPLIFY_DESCRIPTOR(DIFile)
66 DECLARE_SIMPLIFY_DESCRIPTOR(DICompileUnit)
67 DECLARE_SIMPLIFY_DESCRIPTOR(DISubprogram)
68 DECLARE_SIMPLIFY_DESCRIPTOR(DILexicalBlock)
69 DECLARE_SIMPLIFY_DESCRIPTOR(DILexicalBlockFile)
70 DECLARE_SIMPLIFY_DESCRIPTOR(DINameSpace)
71 DECLARE_SIMPLIFY_DESCRIPTOR(DITemplateTypeParameter)
72 DECLARE_SIMPLIFY_DESCRIPTOR(DITemplateValueParameter)
73 DECLARE_SIMPLIFY_DESCRIPTOR(DIGlobalVariable)
74 DECLARE_SIMPLIFY_DESCRIPTOR(DIVariable)
75 DECLARE_SIMPLIFY_DESCRIPTOR(DIExpression)
76 DECLARE_SIMPLIFY_DESCRIPTOR(DILocation)
77 DECLARE_SIMPLIFY_DESCRIPTOR(DIObjCProperty)
78 DECLARE_SIMPLIFY_DESCRIPTOR(DIImportedEntity)
79 #undef DECLARE_SIMPLIFY_DESCRIPTOR
81 typedef DebugNodeArray DIArray;
82 typedef MDTypeRefArray DITypeArray;
88 DISubrange(const MDSubrange *N = nullptr) : N(const_cast<MDSubrange *>(N)) {}
90 operator MDSubrange *() const { return N; }
91 MDSubrange *operator->() const { return N; }
92 MDSubrange &operator*() const { return *N; }
99 DIEnumerator(const MDEnumerator *N = nullptr)
100 : N(const_cast<MDEnumerator *>(N)) {}
102 operator MDEnumerator *() const { return N; }
103 MDEnumerator *operator->() const { return N; }
104 MDEnumerator &operator*() const { return *N; }
111 DIFile(const MDFile *N = nullptr) : N(const_cast<MDFile *>(N)) {}
113 operator MDFile *() const { return N; }
114 MDFile *operator->() const { return N; }
115 MDFile &operator*() const { return *N; }
118 class DICompileUnit {
122 DICompileUnit(const MDCompileUnit *N = nullptr)
123 : N(const_cast<MDCompileUnit *>(N)) {}
125 operator MDCompileUnit *() const { return N; }
126 MDCompileUnit *operator->() const { return N; }
127 MDCompileUnit &operator*() const { return *N; }
134 DISubprogram(const MDSubprogram *N = nullptr)
135 : N(const_cast<MDSubprogram *>(N)) {}
137 operator MDSubprogram *() const { return N; }
138 MDSubprogram *operator->() const { return N; }
139 MDSubprogram &operator*() const { return *N; }
142 class DILexicalBlock {
143 MDLexicalBlockBase *N;
146 DILexicalBlock(const MDLexicalBlockBase *N = nullptr)
147 : N(const_cast<MDLexicalBlockBase *>(N)) {}
149 operator MDLexicalBlockBase *() const { return N; }
150 MDLexicalBlockBase *operator->() const { return N; }
151 MDLexicalBlockBase &operator*() const { return *N; }
154 class DILexicalBlockFile {
155 MDLexicalBlockFile *N;
158 DILexicalBlockFile(const MDLexicalBlockFile *N = nullptr)
159 : N(const_cast<MDLexicalBlockFile *>(N)) {}
161 operator MDLexicalBlockFile *() const { return N; }
162 MDLexicalBlockFile *operator->() const { return N; }
163 MDLexicalBlockFile &operator*() const { return *N; }
170 DINameSpace(const MDNamespace *N = nullptr)
171 : N(const_cast<MDNamespace *>(N)) {}
173 operator MDNamespace *() const { return N; }
174 MDNamespace *operator->() const { return N; }
175 MDNamespace &operator*() const { return *N; }
178 class DITemplateTypeParameter {
179 MDTemplateTypeParameter *N;
182 DITemplateTypeParameter(const MDTemplateTypeParameter *N = nullptr)
183 : N(const_cast<MDTemplateTypeParameter *>(N)) {}
185 operator MDTemplateTypeParameter *() const { return N; }
186 MDTemplateTypeParameter *operator->() const { return N; }
187 MDTemplateTypeParameter &operator*() const { return *N; }
190 class DITemplateValueParameter {
191 MDTemplateValueParameter *N;
194 DITemplateValueParameter(const MDTemplateValueParameter *N = nullptr)
195 : N(const_cast<MDTemplateValueParameter *>(N)) {}
197 operator MDTemplateValueParameter *() const { return N; }
198 MDTemplateValueParameter *operator->() const { return N; }
199 MDTemplateValueParameter &operator*() const { return *N; }
202 class DIGlobalVariable {
206 DIGlobalVariable(const MDGlobalVariable *N = nullptr)
207 : N(const_cast<MDGlobalVariable *>(N)) {}
209 operator MDGlobalVariable *() const { return N; }
210 MDGlobalVariable *operator->() const { return N; }
211 MDGlobalVariable &operator*() const { return *N; }
218 DIVariable(const MDLocalVariable *N = nullptr)
219 : N(const_cast<MDLocalVariable *>(N)) {}
221 operator MDLocalVariable *() const { return N; }
222 MDLocalVariable *operator->() const { return N; }
223 MDLocalVariable &operator*() const { return *N; }
230 DIExpression(const MDExpression *N = nullptr)
231 : N(const_cast<MDExpression *>(N)) {}
233 operator MDExpression *() const { return N; }
234 MDExpression *operator->() const { return N; }
235 MDExpression &operator*() const { return *N; }
242 DILocation(const MDLocation *N = nullptr) : N(const_cast<MDLocation *>(N)) {}
244 operator MDLocation *() const { return N; }
245 MDLocation *operator->() const { return N; }
246 MDLocation &operator*() const { return *N; }
249 class DIObjCProperty {
253 DIObjCProperty(const MDObjCProperty *N = nullptr)
254 : N(const_cast<MDObjCProperty *>(N)) {}
256 operator MDObjCProperty *() const { return N; }
257 MDObjCProperty *operator->() const { return N; }
258 MDObjCProperty &operator*() const { return *N; }
261 class DIImportedEntity {
265 DIImportedEntity(const MDImportedEntity *N = nullptr)
266 : N(const_cast<MDImportedEntity *>(N)) {}
268 operator MDImportedEntity *() const { return N; }
269 MDImportedEntity *operator->() const { return N; }
270 MDImportedEntity &operator*() const { return *N; }
273 #define SIMPLIFY_DESCRIPTOR(DESC) \
274 template <> struct simplify_type<const DESC> { \
275 typedef Metadata *SimpleType; \
276 static SimpleType getSimplifiedValue(const DESC &DI) { return DI; } \
278 template <> struct simplify_type<DESC> : simplify_type<const DESC> {};
279 SIMPLIFY_DESCRIPTOR(DISubrange)
280 SIMPLIFY_DESCRIPTOR(DIEnumerator)
281 SIMPLIFY_DESCRIPTOR(DIFile)
282 SIMPLIFY_DESCRIPTOR(DICompileUnit)
283 SIMPLIFY_DESCRIPTOR(DISubprogram)
284 SIMPLIFY_DESCRIPTOR(DILexicalBlock)
285 SIMPLIFY_DESCRIPTOR(DILexicalBlockFile)
286 SIMPLIFY_DESCRIPTOR(DINameSpace)
287 SIMPLIFY_DESCRIPTOR(DITemplateTypeParameter)
288 SIMPLIFY_DESCRIPTOR(DITemplateValueParameter)
289 SIMPLIFY_DESCRIPTOR(DIGlobalVariable)
290 SIMPLIFY_DESCRIPTOR(DIVariable)
291 SIMPLIFY_DESCRIPTOR(DIExpression)
292 SIMPLIFY_DESCRIPTOR(DILocation)
293 SIMPLIFY_DESCRIPTOR(DIObjCProperty)
294 SIMPLIFY_DESCRIPTOR(DIImportedEntity)
295 #undef SIMPLIFY_DESCRIPTOR
297 /// \brief Find subprogram that is enclosing this scope.
298 MDSubprogram *getDISubprogram(const MDNode *Scope);
300 /// \brief Find debug info for a given function.
301 /// \returns a valid DISubprogram, if found. Otherwise, it returns an empty
303 MDSubprogram *getDISubprogram(const Function *F);
305 /// \brief Find underlying composite type.
306 MDCompositeTypeBase *getDICompositeType(MDType *T);
308 /// \brief Generate map by visiting all retained types.
309 DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
311 /// \brief Strip debug info in the module if it exists.
313 /// To do this, we remove all calls to the debugger intrinsics and any named
314 /// metadata for debugging. We also remove debug locations for instructions.
315 /// Return true if module is modified.
316 bool StripDebugInfo(Module &M);
317 bool stripDebugInfo(Function &F);
319 /// \brief Return Debug Info Metadata Version by checking module flags.
320 unsigned getDebugMetadataVersionFromModule(const Module &M);
322 /// \brief Utility to find all debug info in a module.
324 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
325 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
326 /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
327 /// DbgValueInst and DbgLoc attached to instructions. processModule will go
328 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
330 class DebugInfoFinder {
332 DebugInfoFinder() : TypeMapInitialized(false) {}
334 /// \brief Process entire module and collect debug info anchors.
335 void processModule(const Module &M);
337 /// \brief Process DbgDeclareInst.
338 void processDeclare(const Module &M, const DbgDeclareInst *DDI);
339 /// \brief Process DbgValueInst.
340 void processValue(const Module &M, const DbgValueInst *DVI);
341 /// \brief Process DILocation.
342 void processLocation(const Module &M, const MDLocation *Loc);
344 /// \brief Clear all lists.
348 void InitializeTypeMap(const Module &M);
350 void processType(MDType *DT);
351 void processSubprogram(MDSubprogram *SP);
352 void processScope(MDScope *Scope);
353 bool addCompileUnit(MDCompileUnit *CU);
354 bool addGlobalVariable(MDGlobalVariable *DIG);
355 bool addSubprogram(MDSubprogram *SP);
356 bool addType(MDType *DT);
357 bool addScope(MDScope *Scope);
360 typedef SmallVectorImpl<MDCompileUnit *>::const_iterator
361 compile_unit_iterator;
362 typedef SmallVectorImpl<MDSubprogram *>::const_iterator subprogram_iterator;
363 typedef SmallVectorImpl<MDGlobalVariable *>::const_iterator
364 global_variable_iterator;
365 typedef SmallVectorImpl<MDType *>::const_iterator type_iterator;
366 typedef SmallVectorImpl<MDScope *>::const_iterator scope_iterator;
368 iterator_range<compile_unit_iterator> compile_units() const {
369 return iterator_range<compile_unit_iterator>(CUs.begin(), CUs.end());
372 iterator_range<subprogram_iterator> subprograms() const {
373 return iterator_range<subprogram_iterator>(SPs.begin(), SPs.end());
376 iterator_range<global_variable_iterator> global_variables() const {
377 return iterator_range<global_variable_iterator>(GVs.begin(), GVs.end());
380 iterator_range<type_iterator> types() const {
381 return iterator_range<type_iterator>(TYs.begin(), TYs.end());
384 iterator_range<scope_iterator> scopes() const {
385 return iterator_range<scope_iterator>(Scopes.begin(), Scopes.end());
388 unsigned compile_unit_count() const { return CUs.size(); }
389 unsigned global_variable_count() const { return GVs.size(); }
390 unsigned subprogram_count() const { return SPs.size(); }
391 unsigned type_count() const { return TYs.size(); }
392 unsigned scope_count() const { return Scopes.size(); }
395 SmallVector<MDCompileUnit *, 8> CUs;
396 SmallVector<MDSubprogram *, 8> SPs;
397 SmallVector<MDGlobalVariable *, 8> GVs;
398 SmallVector<MDType *, 8> TYs;
399 SmallVector<MDScope *, 8> Scopes;
400 SmallPtrSet<const MDNode *, 64> NodesSeen;
401 DITypeIdentifierMap TypeIdentifierMap;
403 /// \brief Specify if TypeIdentifierMap is initialized.
404 bool TypeMapInitialized;
407 DenseMap<const Function *, MDSubprogram *> makeSubprogramMap(const Module &M);
409 } // end namespace llvm