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