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
31namespace llvm {
32class BasicBlock;
33class Constant;
34class Function;
35class GlobalVariable;
36class Module;
37class Type;
38class Value;
39class DbgDeclareInst;
40class DbgValueInst;
41class Instruction;
42class Metadata;
43class MDNode;
44class MDString;
45class NamedMDNode;
46class LLVMContext;
47class raw_ostream;
48
49class DIFile;
50class DISubprogram;
51class DILexicalBlock;
52class DILexicalBlockFile;
53class DIVariable;
54class DIType;
55class DIScope;
56class DIObjCProperty;
57
58/// \brief Maps from type identifier to the actual MDNode.
59typedef DenseMap<const MDString *, MDNode *> DITypeIdentifierMap;
60
61class DIDescriptor {
62  MDNode *N;
63
64public:
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>;
76DECLARE_SIMPLIFY_DESCRIPTOR(DIDescriptor)
77DECLARE_SIMPLIFY_DESCRIPTOR(DISubrange)
78DECLARE_SIMPLIFY_DESCRIPTOR(DIEnumerator)
79DECLARE_SIMPLIFY_DESCRIPTOR(DIScope)
80DECLARE_SIMPLIFY_DESCRIPTOR(DIType)
81DECLARE_SIMPLIFY_DESCRIPTOR(DIBasicType)
82DECLARE_SIMPLIFY_DESCRIPTOR(DIDerivedType)
83DECLARE_SIMPLIFY_DESCRIPTOR(DICompositeType)
84DECLARE_SIMPLIFY_DESCRIPTOR(DISubroutineType)
85DECLARE_SIMPLIFY_DESCRIPTOR(DIFile)
86DECLARE_SIMPLIFY_DESCRIPTOR(DICompileUnit)
87DECLARE_SIMPLIFY_DESCRIPTOR(DISubprogram)
88DECLARE_SIMPLIFY_DESCRIPTOR(DILexicalBlock)
89DECLARE_SIMPLIFY_DESCRIPTOR(DILexicalBlockFile)
90DECLARE_SIMPLIFY_DESCRIPTOR(DINameSpace)
91DECLARE_SIMPLIFY_DESCRIPTOR(DITemplateTypeParameter)
92DECLARE_SIMPLIFY_DESCRIPTOR(DITemplateValueParameter)
93DECLARE_SIMPLIFY_DESCRIPTOR(DIGlobalVariable)
94DECLARE_SIMPLIFY_DESCRIPTOR(DIVariable)
95DECLARE_SIMPLIFY_DESCRIPTOR(DIExpression)
96DECLARE_SIMPLIFY_DESCRIPTOR(DILocation)
97DECLARE_SIMPLIFY_DESCRIPTOR(DIObjCProperty)
98DECLARE_SIMPLIFY_DESCRIPTOR(DIImportedEntity)
99#undef DECLARE_SIMPLIFY_DESCRIPTOR
100
101typedef DebugNodeArray DIArray;
102typedef MDTypeRefArray DITypeArray;
103typedef DebugNodeRef DIDescriptorRef;
104typedef MDScopeRef DIScopeRef;
105typedef MDTypeRef DITypeRef;
106
107class DISubrange {
108  MDSubrange *N;
109
110public:
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
118class DIEnumerator {
119  MDEnumerator *N;
120
121public:
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
130class DIScope {
131  MDScope *N;
132
133public:
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
142class DIType {
143  MDType *N;
144
145public:
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
155class DIBasicType {
156  MDBasicType *N;
157
158public:
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
169class DIDerivedType {
170  MDDerivedTypeBase *N;
171
172public:
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
183class DICompositeType {
184  MDCompositeTypeBase *N;
185
186public:
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
197class DISubroutineType {
198  MDSubroutineType *N;
199
200public:
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
212class DIFile {
213  MDFile *N;
214
215public:
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
225class DICompileUnit {
226  MDCompileUnit *N;
227
228public:
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
239class DISubprogram {
240  MDSubprogram *N;
241
242public:
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
253class DILexicalBlock {
254  MDLexicalBlockBase *N;
255
256public:
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
266class DILexicalBlockFile {
267  MDLexicalBlockFile *N;
268
269public:
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
279class DINameSpace {
280  MDNamespace *N;
281
282public:
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
293class DITemplateTypeParameter {
294  MDTemplateTypeParameter *N;
295
296public:
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
305class DITemplateValueParameter {
306  MDTemplateValueParameter *N;
307
308public:
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
317class DIGlobalVariable {
318  MDGlobalVariable *N;
319
320public:
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
330class DIVariable {
331  MDLocalVariable *N;
332
333public:
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
342class DIExpression {
343  MDExpression *N;
344
345public:
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
354class DILocation {
355  MDLocation *N;
356
357public:
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
365class DIObjCProperty {
366  MDObjCProperty *N;
367
368public:
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
377class DIImportedEntity {
378  MDImportedEntity *N;
379
380public:
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> {};
396SIMPLIFY_DESCRIPTOR(DIDescriptor)
397SIMPLIFY_DESCRIPTOR(DISubrange)
398SIMPLIFY_DESCRIPTOR(DIEnumerator)
399SIMPLIFY_DESCRIPTOR(DIScope)
400SIMPLIFY_DESCRIPTOR(DIType)
401SIMPLIFY_DESCRIPTOR(DIBasicType)
402SIMPLIFY_DESCRIPTOR(DIDerivedType)
403SIMPLIFY_DESCRIPTOR(DICompositeType)
404SIMPLIFY_DESCRIPTOR(DISubroutineType)
405SIMPLIFY_DESCRIPTOR(DIFile)
406SIMPLIFY_DESCRIPTOR(DICompileUnit)
407SIMPLIFY_DESCRIPTOR(DISubprogram)
408SIMPLIFY_DESCRIPTOR(DILexicalBlock)
409SIMPLIFY_DESCRIPTOR(DILexicalBlockFile)
410SIMPLIFY_DESCRIPTOR(DINameSpace)
411SIMPLIFY_DESCRIPTOR(DITemplateTypeParameter)
412SIMPLIFY_DESCRIPTOR(DITemplateValueParameter)
413SIMPLIFY_DESCRIPTOR(DIGlobalVariable)
414SIMPLIFY_DESCRIPTOR(DIVariable)
415SIMPLIFY_DESCRIPTOR(DIExpression)
416SIMPLIFY_DESCRIPTOR(DILocation)
417SIMPLIFY_DESCRIPTOR(DIObjCProperty)
418SIMPLIFY_DESCRIPTOR(DIImportedEntity)
419#undef SIMPLIFY_DESCRIPTOR
420
421/// \brief Find subprogram that is enclosing this scope.
422DISubprogram 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.
427DISubprogram getDISubprogram(const Function *F);
428
429/// \brief Find underlying composite type.
430DICompositeType getDICompositeType(DIType T);
431
432/// \brief Generate map by visiting all retained types.
433DITypeIdentifierMap 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.
440bool StripDebugInfo(Module &M);
441bool stripDebugInfo(Function &F);
442
443/// \brief Return Debug Info Metadata Version by checking module flags.
444unsigned 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.
454class DebugInfoFinder {
455public:
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, DILocation Loc);
467
468  /// \brief Clear all lists.
469  void reset();
470
471private:
472  void InitializeTypeMap(const Module &M);
473
474  void processType(DIType DT);
475  void processSubprogram(DISubprogram SP);
476  void processScope(DIScope Scope);
477  bool addCompileUnit(DICompileUnit CU);
478  bool addGlobalVariable(DIGlobalVariable DIG);
479  bool addSubprogram(DISubprogram SP);
480  bool addType(DIType DT);
481  bool addScope(DIScope Scope);
482
483public:
484  typedef SmallVectorImpl<DICompileUnit>::const_iterator compile_unit_iterator;
485  typedef SmallVectorImpl<DISubprogram>::const_iterator subprogram_iterator;
486  typedef SmallVectorImpl<DIGlobalVariable>::const_iterator
487      global_variable_iterator;
488  typedef SmallVectorImpl<DIType>::const_iterator type_iterator;
489  typedef SmallVectorImpl<DIScope>::const_iterator scope_iterator;
490
491  iterator_range<compile_unit_iterator> compile_units() const {
492    return iterator_range<compile_unit_iterator>(CUs.begin(), CUs.end());
493  }
494
495  iterator_range<subprogram_iterator> subprograms() const {
496    return iterator_range<subprogram_iterator>(SPs.begin(), SPs.end());
497  }
498
499  iterator_range<global_variable_iterator> global_variables() const {
500    return iterator_range<global_variable_iterator>(GVs.begin(), GVs.end());
501  }
502
503  iterator_range<type_iterator> types() const {
504    return iterator_range<type_iterator>(TYs.begin(), TYs.end());
505  }
506
507  iterator_range<scope_iterator> scopes() const {
508    return iterator_range<scope_iterator>(Scopes.begin(), Scopes.end());
509  }
510
511  unsigned compile_unit_count() const { return CUs.size(); }
512  unsigned global_variable_count() const { return GVs.size(); }
513  unsigned subprogram_count() const { return SPs.size(); }
514  unsigned type_count() const { return TYs.size(); }
515  unsigned scope_count() const { return Scopes.size(); }
516
517private:
518  SmallVector<DICompileUnit, 8> CUs;
519  SmallVector<DISubprogram, 8> SPs;
520  SmallVector<DIGlobalVariable, 8> GVs;
521  SmallVector<DIType, 8> TYs;
522  SmallVector<DIScope, 8> Scopes;
523  SmallPtrSet<MDNode *, 64> NodesSeen;
524  DITypeIdentifierMap TypeIdentifierMap;
525
526  /// \brief Specify if TypeIdentifierMap is initialized.
527  bool TypeMapInitialized;
528};
529
530DenseMap<const Function *, DISubprogram> makeSubprogramMap(const Module &M);
531
532} // end namespace llvm
533
534#endif
535