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