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