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