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