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