2762086b6e9d37d04ec3c7fb09472e995e705339
[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 BasicBlock;
33 class Constant;
34 class Function;
35 class GlobalVariable;
36 class Module;
37 class Type;
38 class Value;
39 class DbgDeclareInst;
40 class DbgValueInst;
41 class Instruction;
42 class Metadata;
43 class MDNode;
44 class MDString;
45 class NamedMDNode;
46 class LLVMContext;
47 class raw_ostream;
48
49 class DIFile;
50 class DISubprogram;
51 class DILexicalBlock;
52 class DILexicalBlockFile;
53 class DIVariable;
54 class DIType;
55 class DIScope;
56 class DIObjCProperty;
57
58 /// \brief Maps from type identifier to the actual MDNode.
59 typedef DenseMap<const MDString *, MDNode *> DITypeIdentifierMap;
60
61 #define DECLARE_SIMPLIFY_DESCRIPTOR(DESC)                                      \
62   class DESC;                                                                  \
63   template <> struct simplify_type<const DESC>;                                \
64   template <> struct simplify_type<DESC>;
65 DECLARE_SIMPLIFY_DESCRIPTOR(DISubrange)
66 DECLARE_SIMPLIFY_DESCRIPTOR(DIEnumerator)
67 DECLARE_SIMPLIFY_DESCRIPTOR(DIScope)
68 DECLARE_SIMPLIFY_DESCRIPTOR(DIType)
69 DECLARE_SIMPLIFY_DESCRIPTOR(DIBasicType)
70 DECLARE_SIMPLIFY_DESCRIPTOR(DIDerivedType)
71 DECLARE_SIMPLIFY_DESCRIPTOR(DICompositeType)
72 DECLARE_SIMPLIFY_DESCRIPTOR(DISubroutineType)
73 DECLARE_SIMPLIFY_DESCRIPTOR(DIFile)
74 DECLARE_SIMPLIFY_DESCRIPTOR(DICompileUnit)
75 DECLARE_SIMPLIFY_DESCRIPTOR(DISubprogram)
76 DECLARE_SIMPLIFY_DESCRIPTOR(DILexicalBlock)
77 DECLARE_SIMPLIFY_DESCRIPTOR(DILexicalBlockFile)
78 DECLARE_SIMPLIFY_DESCRIPTOR(DINameSpace)
79 DECLARE_SIMPLIFY_DESCRIPTOR(DITemplateTypeParameter)
80 DECLARE_SIMPLIFY_DESCRIPTOR(DITemplateValueParameter)
81 DECLARE_SIMPLIFY_DESCRIPTOR(DIGlobalVariable)
82 DECLARE_SIMPLIFY_DESCRIPTOR(DIVariable)
83 DECLARE_SIMPLIFY_DESCRIPTOR(DIExpression)
84 DECLARE_SIMPLIFY_DESCRIPTOR(DILocation)
85 DECLARE_SIMPLIFY_DESCRIPTOR(DIObjCProperty)
86 DECLARE_SIMPLIFY_DESCRIPTOR(DIImportedEntity)
87 #undef DECLARE_SIMPLIFY_DESCRIPTOR
88
89 typedef DebugNodeArray DIArray;
90 typedef MDTypeRefArray DITypeArray;
91
92 class DISubrange {
93   MDSubrange *N;
94
95 public:
96   DISubrange(const MDSubrange *N = nullptr) : N(const_cast<MDSubrange *>(N)) {}
97
98   operator MDSubrange *() const { return N; }
99   MDSubrange *operator->() const { return N; }
100   MDSubrange &operator*() const { return *N; }
101 };
102
103 class DIEnumerator {
104   MDEnumerator *N;
105
106 public:
107   DIEnumerator(const MDEnumerator *N = nullptr)
108       : N(const_cast<MDEnumerator *>(N)) {}
109
110   operator MDEnumerator *() const { return N; }
111   MDEnumerator *operator->() const { return N; }
112   MDEnumerator &operator*() const { return *N; }
113 };
114
115 class DIScope {
116   MDScope *N;
117
118 public:
119   DIScope(const MDScope *N = nullptr) : N(const_cast<MDScope *>(N)) {}
120
121   operator MDScope *() const { return N; }
122   MDScope *operator->() const { return N; }
123   MDScope &operator*() const { return *N; }
124 };
125
126 class DIType {
127   MDType *N;
128
129 public:
130   DIType(const MDType *N = nullptr) : N(const_cast<MDType *>(N)) {}
131
132   operator DIScope() const { return N; }
133   operator MDType *() const { return N; }
134   MDType *operator->() const { return N; }
135   MDType &operator*() const { return *N; }
136 };
137
138 class DIBasicType {
139   MDBasicType *N;
140
141 public:
142   DIBasicType(const MDBasicType *N = nullptr)
143       : N(const_cast<MDBasicType *>(N)) {}
144
145   operator DIType() const { return N; }
146   operator MDBasicType *() const { return N; }
147   MDBasicType *operator->() const { return N; }
148   MDBasicType &operator*() const { return *N; }
149 };
150
151 class DIDerivedType {
152   MDDerivedTypeBase *N;
153
154 public:
155   DIDerivedType(const MDDerivedTypeBase *N = nullptr)
156       : N(const_cast<MDDerivedTypeBase *>(N)) {}
157
158   operator DIType() const { return N; }
159   operator MDDerivedTypeBase *() const { return N; }
160   MDDerivedTypeBase *operator->() const { return N; }
161   MDDerivedTypeBase &operator*() const { return *N; }
162 };
163
164 class DICompositeType {
165   MDCompositeTypeBase *N;
166
167 public:
168   DICompositeType(const MDCompositeTypeBase *N = nullptr)
169       : N(const_cast<MDCompositeTypeBase *>(N)) {}
170
171   operator DIType() const { return N; }
172   operator MDCompositeTypeBase *() const { return N; }
173   MDCompositeTypeBase *operator->() const { return N; }
174   MDCompositeTypeBase &operator*() const { return *N; }
175 };
176
177 class DISubroutineType {
178   MDSubroutineType *N;
179
180 public:
181   DISubroutineType(const MDSubroutineType *N = nullptr)
182       : N(const_cast<MDSubroutineType *>(N)) {}
183
184   operator DIType() const { return N; }
185   operator DICompositeType() const { return N; }
186   operator MDSubroutineType *() const { return N; }
187   MDSubroutineType *operator->() const { return N; }
188   MDSubroutineType &operator*() const { return *N; }
189 };
190
191 class DIFile {
192   MDFile *N;
193
194 public:
195   DIFile(const MDFile *N = nullptr) : N(const_cast<MDFile *>(N)) {}
196
197   operator DIScope() const { return N; }
198   operator MDFile *() const { return N; }
199   MDFile *operator->() const { return N; }
200   MDFile &operator*() const { return *N; }
201 };
202
203 class DICompileUnit {
204   MDCompileUnit *N;
205
206 public:
207   DICompileUnit(const MDCompileUnit *N = nullptr)
208       : N(const_cast<MDCompileUnit *>(N)) {}
209
210   operator DIScope() const { return N; }
211   operator MDCompileUnit *() const { return N; }
212   MDCompileUnit *operator->() const { return N; }
213   MDCompileUnit &operator*() const { return *N; }
214 };
215
216 class DISubprogram {
217   MDSubprogram *N;
218
219 public:
220   DISubprogram(const MDSubprogram *N = nullptr)
221       : N(const_cast<MDSubprogram *>(N)) {}
222
223   operator DIScope() const { return N; }
224   operator MDSubprogram *() const { return N; }
225   MDSubprogram *operator->() const { return N; }
226   MDSubprogram &operator*() const { return *N; }
227 };
228
229 class DILexicalBlock {
230   MDLexicalBlockBase *N;
231
232 public:
233   DILexicalBlock(const MDLexicalBlockBase *N = nullptr)
234       : N(const_cast<MDLexicalBlockBase *>(N)) {}
235
236   operator MDLexicalBlockBase *() const { return N; }
237   MDLexicalBlockBase *operator->() const { return N; }
238   MDLexicalBlockBase &operator*() const { return *N; }
239 };
240
241 class DILexicalBlockFile {
242   MDLexicalBlockFile *N;
243
244 public:
245   DILexicalBlockFile(const MDLexicalBlockFile *N = nullptr)
246       : N(const_cast<MDLexicalBlockFile *>(N)) {}
247
248   operator MDLexicalBlockFile *() const { return N; }
249   MDLexicalBlockFile *operator->() const { return N; }
250   MDLexicalBlockFile &operator*() const { return *N; }
251 };
252
253 class DINameSpace {
254   MDNamespace *N;
255
256 public:
257   DINameSpace(const MDNamespace *N = nullptr)
258       : N(const_cast<MDNamespace *>(N)) {}
259
260   operator DIScope() const { return N; }
261   operator MDNamespace *() const { return N; }
262   MDNamespace *operator->() const { return N; }
263   MDNamespace &operator*() const { return *N; }
264 };
265
266 class DITemplateTypeParameter {
267   MDTemplateTypeParameter *N;
268
269 public:
270   DITemplateTypeParameter(const MDTemplateTypeParameter *N = nullptr)
271       : N(const_cast<MDTemplateTypeParameter *>(N)) {}
272
273   operator MDTemplateTypeParameter *() const { return N; }
274   MDTemplateTypeParameter *operator->() const { return N; }
275   MDTemplateTypeParameter &operator*() const { return *N; }
276 };
277
278 class DITemplateValueParameter {
279   MDTemplateValueParameter *N;
280
281 public:
282   DITemplateValueParameter(const MDTemplateValueParameter *N = nullptr)
283       : N(const_cast<MDTemplateValueParameter *>(N)) {}
284
285   operator MDTemplateValueParameter *() const { return N; }
286   MDTemplateValueParameter *operator->() const { return N; }
287   MDTemplateValueParameter &operator*() const { return *N; }
288 };
289
290 class DIGlobalVariable {
291   MDGlobalVariable *N;
292
293 public:
294   DIGlobalVariable(const MDGlobalVariable *N = nullptr)
295       : N(const_cast<MDGlobalVariable *>(N)) {}
296
297   operator MDGlobalVariable *() const { return N; }
298   MDGlobalVariable *operator->() const { return N; }
299   MDGlobalVariable &operator*() const { return *N; }
300 };
301
302 class DIVariable {
303   MDLocalVariable *N;
304
305 public:
306   DIVariable(const MDLocalVariable *N = nullptr)
307       : N(const_cast<MDLocalVariable *>(N)) {}
308
309   operator MDLocalVariable *() const { return N; }
310   MDLocalVariable *operator->() const { return N; }
311   MDLocalVariable &operator*() const { return *N; }
312 };
313
314 class DIExpression {
315   MDExpression *N;
316
317 public:
318   DIExpression(const MDExpression *N = nullptr)
319       : N(const_cast<MDExpression *>(N)) {}
320
321   operator MDExpression *() const { return N; }
322   MDExpression *operator->() const { return N; }
323   MDExpression &operator*() const { return *N; }
324 };
325
326 class DILocation {
327   MDLocation *N;
328
329 public:
330   DILocation(const MDLocation *N = nullptr) : N(const_cast<MDLocation *>(N)) {}
331
332   operator MDLocation *() const { return N; }
333   MDLocation *operator->() const { return N; }
334   MDLocation &operator*() const { return *N; }
335 };
336
337 class DIObjCProperty {
338   MDObjCProperty *N;
339
340 public:
341   DIObjCProperty(const MDObjCProperty *N = nullptr)
342       : N(const_cast<MDObjCProperty *>(N)) {}
343
344   operator MDObjCProperty *() const { return N; }
345   MDObjCProperty *operator->() const { return N; }
346   MDObjCProperty &operator*() const { return *N; }
347 };
348
349 class DIImportedEntity {
350   MDImportedEntity *N;
351
352 public:
353   DIImportedEntity(const MDImportedEntity *N = nullptr)
354       : N(const_cast<MDImportedEntity *>(N)) {}
355
356   operator MDImportedEntity *() const { return N; }
357   MDImportedEntity *operator->() const { return N; }
358   MDImportedEntity &operator*() const { return *N; }
359 };
360
361 #define SIMPLIFY_DESCRIPTOR(DESC)                                              \
362   template <> struct simplify_type<const DESC> {                               \
363     typedef Metadata *SimpleType;                                              \
364     static SimpleType getSimplifiedValue(const DESC &DI) { return DI; }        \
365   };                                                                           \
366   template <> struct simplify_type<DESC> : simplify_type<const DESC> {};
367 SIMPLIFY_DESCRIPTOR(DISubrange)
368 SIMPLIFY_DESCRIPTOR(DIEnumerator)
369 SIMPLIFY_DESCRIPTOR(DIScope)
370 SIMPLIFY_DESCRIPTOR(DIType)
371 SIMPLIFY_DESCRIPTOR(DIBasicType)
372 SIMPLIFY_DESCRIPTOR(DIDerivedType)
373 SIMPLIFY_DESCRIPTOR(DICompositeType)
374 SIMPLIFY_DESCRIPTOR(DISubroutineType)
375 SIMPLIFY_DESCRIPTOR(DIFile)
376 SIMPLIFY_DESCRIPTOR(DICompileUnit)
377 SIMPLIFY_DESCRIPTOR(DISubprogram)
378 SIMPLIFY_DESCRIPTOR(DILexicalBlock)
379 SIMPLIFY_DESCRIPTOR(DILexicalBlockFile)
380 SIMPLIFY_DESCRIPTOR(DINameSpace)
381 SIMPLIFY_DESCRIPTOR(DITemplateTypeParameter)
382 SIMPLIFY_DESCRIPTOR(DITemplateValueParameter)
383 SIMPLIFY_DESCRIPTOR(DIGlobalVariable)
384 SIMPLIFY_DESCRIPTOR(DIVariable)
385 SIMPLIFY_DESCRIPTOR(DIExpression)
386 SIMPLIFY_DESCRIPTOR(DILocation)
387 SIMPLIFY_DESCRIPTOR(DIObjCProperty)
388 SIMPLIFY_DESCRIPTOR(DIImportedEntity)
389 #undef SIMPLIFY_DESCRIPTOR
390
391 /// \brief Find subprogram that is enclosing this scope.
392 MDSubprogram *getDISubprogram(const MDNode *Scope);
393
394 /// \brief Find debug info for a given function.
395 /// \returns a valid DISubprogram, if found. Otherwise, it returns an empty
396 /// DISubprogram.
397 MDSubprogram *getDISubprogram(const Function *F);
398
399 /// \brief Find underlying composite type.
400 MDCompositeTypeBase *getDICompositeType(MDType *T);
401
402 /// \brief Generate map by visiting all retained types.
403 DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
404
405 /// \brief Strip debug info in the module if it exists.
406 ///
407 /// To do this, we remove all calls to the debugger intrinsics and any named
408 /// metadata for debugging. We also remove debug locations for instructions.
409 /// Return true if module is modified.
410 bool StripDebugInfo(Module &M);
411 bool stripDebugInfo(Function &F);
412
413 /// \brief Return Debug Info Metadata Version by checking module flags.
414 unsigned getDebugMetadataVersionFromModule(const Module &M);
415
416 /// \brief Utility to find all debug info in a module.
417 ///
418 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
419 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
420 /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
421 /// DbgValueInst and DbgLoc attached to instructions. processModule will go
422 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
423 /// used by the CUs.
424 class DebugInfoFinder {
425 public:
426   DebugInfoFinder() : TypeMapInitialized(false) {}
427
428   /// \brief Process entire module and collect debug info anchors.
429   void processModule(const Module &M);
430
431   /// \brief Process DbgDeclareInst.
432   void processDeclare(const Module &M, const DbgDeclareInst *DDI);
433   /// \brief Process DbgValueInst.
434   void processValue(const Module &M, const DbgValueInst *DVI);
435   /// \brief Process DILocation.
436   void processLocation(const Module &M, const MDLocation *Loc);
437
438   /// \brief Clear all lists.
439   void reset();
440
441 private:
442   void InitializeTypeMap(const Module &M);
443
444   void processType(MDType *DT);
445   void processSubprogram(MDSubprogram *SP);
446   void processScope(MDScope *Scope);
447   bool addCompileUnit(MDCompileUnit *CU);
448   bool addGlobalVariable(MDGlobalVariable *DIG);
449   bool addSubprogram(MDSubprogram *SP);
450   bool addType(MDType *DT);
451   bool addScope(MDScope *Scope);
452
453 public:
454   typedef SmallVectorImpl<MDCompileUnit *>::const_iterator
455       compile_unit_iterator;
456   typedef SmallVectorImpl<MDSubprogram *>::const_iterator subprogram_iterator;
457   typedef SmallVectorImpl<MDGlobalVariable *>::const_iterator
458       global_variable_iterator;
459   typedef SmallVectorImpl<MDType *>::const_iterator type_iterator;
460   typedef SmallVectorImpl<MDScope *>::const_iterator scope_iterator;
461
462   iterator_range<compile_unit_iterator> compile_units() const {
463     return iterator_range<compile_unit_iterator>(CUs.begin(), CUs.end());
464   }
465
466   iterator_range<subprogram_iterator> subprograms() const {
467     return iterator_range<subprogram_iterator>(SPs.begin(), SPs.end());
468   }
469
470   iterator_range<global_variable_iterator> global_variables() const {
471     return iterator_range<global_variable_iterator>(GVs.begin(), GVs.end());
472   }
473
474   iterator_range<type_iterator> types() const {
475     return iterator_range<type_iterator>(TYs.begin(), TYs.end());
476   }
477
478   iterator_range<scope_iterator> scopes() const {
479     return iterator_range<scope_iterator>(Scopes.begin(), Scopes.end());
480   }
481
482   unsigned compile_unit_count() const { return CUs.size(); }
483   unsigned global_variable_count() const { return GVs.size(); }
484   unsigned subprogram_count() const { return SPs.size(); }
485   unsigned type_count() const { return TYs.size(); }
486   unsigned scope_count() const { return Scopes.size(); }
487
488 private:
489   SmallVector<MDCompileUnit *, 8> CUs;
490   SmallVector<MDSubprogram *, 8> SPs;
491   SmallVector<MDGlobalVariable *, 8> GVs;
492   SmallVector<MDType *, 8> TYs;
493   SmallVector<MDScope *, 8> Scopes;
494   SmallPtrSet<const MDNode *, 64> NodesSeen;
495   DITypeIdentifierMap TypeIdentifierMap;
496
497   /// \brief Specify if TypeIdentifierMap is initialized.
498   bool TypeMapInitialized;
499 };
500
501 DenseMap<const Function *, MDSubprogram *> makeSubprogramMap(const Module &M);
502
503 } // end namespace llvm
504
505 #endif