a8e124c6f09cf75a2bb8735c9c59446e5876be46
[oota-llvm.git] / include / llvm / Analysis / DebugInfo.h
1 //===--- llvm/Analysis/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.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_ANALYSIS_DEBUGINFO_H
16 #define LLVM_ANALYSIS_DEBUGINFO_H
17
18 #include "llvm/ADT/StringMap.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/Support/Dwarf.h"
21
22 namespace llvm {
23   class BasicBlock;
24   class Constant;
25   class Function;
26   class GlobalVariable;
27   class Module;
28   class Type;
29   class Value;
30   struct DbgStopPointInst;
31   struct DbgDeclareInst;
32   class Instruction;
33
34   class DIDescriptor {
35   protected:    
36     GlobalVariable *GV;
37
38     /// DIDescriptor constructor.  If the specified GV is non-null, this checks
39     /// to make sure that the tag in the descriptor matches 'RequiredTag'.  If
40     /// not, the debug info is corrupt and we ignore it.
41     DIDescriptor(GlobalVariable *GV, unsigned RequiredTag);
42
43     const std::string &getStringField(unsigned Elt, std::string &Result) const;
44     unsigned getUnsignedField(unsigned Elt) const {
45       return (unsigned)getUInt64Field(Elt);
46     }
47     uint64_t getUInt64Field(unsigned Elt) const;
48     DIDescriptor getDescriptorField(unsigned Elt) const;
49
50     template <typename DescTy>
51     DescTy getFieldAs(unsigned Elt) const {
52       return DescTy(getDescriptorField(Elt).getGV());
53     }
54
55     GlobalVariable *getGlobalVariableField(unsigned Elt) const;
56
57   public:
58     explicit DIDescriptor() : GV(0) {}
59     explicit DIDescriptor(GlobalVariable *gv) : GV(gv) {}
60
61     bool isNull() const { return GV == 0; }
62
63     GlobalVariable *getGV() const { return GV; }
64
65     unsigned getVersion() const {
66       return getUnsignedField(0) & LLVMDebugVersionMask;
67     }
68
69     unsigned getTag() const {
70       return getUnsignedField(0) & ~LLVMDebugVersionMask;
71     }
72
73   };
74
75   /// DIAnchor - A wrapper for various anchor descriptors.
76   class DIAnchor : public DIDescriptor {
77   public:
78     explicit DIAnchor(GlobalVariable *GV = 0);
79
80     unsigned getAnchorTag() const { return getUnsignedField(1); }
81   };
82
83   /// DISubrange - This is used to represent ranges, for array bounds.
84   class DISubrange : public DIDescriptor {
85   public:
86     explicit DISubrange(GlobalVariable *GV = 0);
87
88     int64_t getLo() const { return (int64_t)getUInt64Field(1); }
89     int64_t getHi() const { return (int64_t)getUInt64Field(2); }
90   };
91
92   /// DIArray - This descriptor holds an array of descriptors.
93   class DIArray : public DIDescriptor {
94   public:
95     explicit DIArray(GlobalVariable *GV = 0) : DIDescriptor(GV) {}
96
97     unsigned getNumElements() const;
98     DIDescriptor getElement(unsigned Idx) const {
99       return getDescriptorField(Idx);
100     }
101   };
102
103   /// DICompileUnit - A wrapper for a compile unit.
104   class DICompileUnit : public DIDescriptor {
105   public:
106     explicit DICompileUnit(GlobalVariable *GV = 0);
107
108     unsigned getLanguage() const     { return getUnsignedField(2); }
109     const std::string &getFilename(std::string &F) const {
110       return getStringField(3, F);
111     }
112     const std::string &getDirectory(std::string &F) const {
113       return getStringField(4, F);
114     }
115     const std::string &getProducer(std::string &F) const {
116       return getStringField(5, F);
117     }
118     
119     /// isMain - Each input file is encoded as a separate compile unit in LLVM
120     /// debugging information output. However, many target specific tool chains
121     /// prefer to encode only one compile unit in an object file. In this 
122     /// situation, the LLVM code generator will include  debugging information
123     /// entities in the compile unit that is marked as main compile unit. The 
124     /// code generator accepts maximum one main compile unit per module. If a
125     /// module does not contain any main compile unit then the code generator 
126     /// will emit multiple compile units in the output object file.
127
128     bool isMain() const                { return getUnsignedField(6); }
129     bool isOptimized() const           { return getUnsignedField(7); }
130     const std::string &getFlags(std::string &F) const {
131       return getStringField(8, F);
132     }
133     unsigned getRunTimeVersion() const { return getUnsignedField(9); }
134
135     /// Verify - Verify that a compile unit is well formed.
136     bool Verify() const;
137
138     /// dump - print compile unit.
139     void dump() const;
140   };
141
142   /// DIEnumerator - A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
143   /// FIXME: it seems strange that this doesn't have either a reference to the
144   /// type/precision or a file/line pair for location info.
145   class DIEnumerator : public DIDescriptor {
146   public:
147     explicit DIEnumerator(GlobalVariable *GV = 0);
148
149     const std::string &getName(std::string &F) const {
150       return getStringField(1, F);
151     }
152     uint64_t getEnumValue() const { return getUInt64Field(2); }
153   };
154
155   /// DIType - This is a wrapper for a type.
156   /// FIXME: Types should be factored much better so that CV qualifiers and
157   /// others do not require a huge and empty descriptor full of zeros.
158   class DIType : public DIDescriptor {
159   public:
160     enum {
161       FlagPrivate   = 1 << 0,
162       FlagProtected = 1 << 1,
163       FlagFwdDecl   = 1 << 2
164     };
165
166   protected:
167     DIType(GlobalVariable *GV, unsigned Tag) : DIDescriptor(GV, Tag) {}
168     // This ctor is used when the Tag has already been validated by a derived
169     // ctor.
170     DIType(GlobalVariable *GV, bool, bool) : DIDescriptor(GV) {}
171
172   public:
173     /// isDerivedType - Return true if the specified tag is legal for
174     /// DIDerivedType.
175     static bool isDerivedType(unsigned TAG);
176
177     /// isCompositeType - Return true if the specified tag is legal for
178     /// DICompositeType.
179     static bool isCompositeType(unsigned TAG);
180
181     /// isBasicType - Return true if the specified tag is legal for
182     /// DIBasicType.
183     static bool isBasicType(unsigned TAG) {
184       return TAG == dwarf::DW_TAG_base_type;
185     }
186
187     /// Verify - Verify that a type descriptor is well formed.
188     bool Verify() const;
189   public:
190     explicit DIType(GlobalVariable *GV);
191     explicit DIType() {}
192     virtual ~DIType() {}
193
194     DIDescriptor getContext() const     { return getDescriptorField(1); }
195     const std::string &getName(std::string &F) const {
196       return getStringField(2, F);
197     }
198     DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
199     unsigned getLineNumber() const      { return getUnsignedField(4); }
200     uint64_t getSizeInBits() const      { return getUInt64Field(5); }
201     uint64_t getAlignInBits() const     { return getUInt64Field(6); }
202     // FIXME: Offset is only used for DW_TAG_member nodes.  Making every type
203     // carry this is just plain insane.
204     uint64_t getOffsetInBits() const    { return getUInt64Field(7); }
205     unsigned getFlags() const           { return getUnsignedField(8); }
206     bool isPrivate() const              { return (getFlags() & FlagPrivate) != 0; }
207     bool isProtected() const            { return (getFlags() & FlagProtected) != 0; }
208     bool isForwardDecl() const          { return (getFlags() & FlagFwdDecl) != 0; }
209
210     /// dump - print type.
211     void dump() const;
212   };
213
214   /// DIBasicType - A basic type, like 'int' or 'float'.
215   class DIBasicType : public DIType {
216   public:
217     explicit DIBasicType(GlobalVariable *GV);
218     unsigned getEncoding() const { return getUnsignedField(9); }
219
220     /// dump - print basic type.
221     void dump() const;
222   };
223
224   /// DIDerivedType - A simple derived type, like a const qualified type,
225   /// a typedef, a pointer or reference, etc.
226   class DIDerivedType : public DIType {
227   protected:
228     explicit DIDerivedType(GlobalVariable *GV, bool, bool)
229       : DIType(GV, true, true) {}
230   public:
231     explicit DIDerivedType(GlobalVariable *GV);
232     DIType getTypeDerivedFrom() const { return getFieldAs<DIType>(9); }
233
234     /// getOriginalTypeSize - If this type is derived from a base type then
235     /// return base type size.
236     uint64_t getOriginalTypeSize() const;
237     /// dump - print derived type.
238     void dump() const;
239   };
240
241   /// DICompositeType - This descriptor holds a type that can refer to multiple
242   /// other types, like a function or struct.
243   /// FIXME: Why is this a DIDerivedType??
244   class DICompositeType : public DIDerivedType {
245   public:
246     explicit DICompositeType(GlobalVariable *GV);
247     DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
248     unsigned getRunTimeLang() const { return getUnsignedField(11); }
249
250     /// Verify - Verify that a composite type descriptor is well formed.
251     bool Verify() const;
252
253     /// dump - print composite type.
254     void dump() const;
255   };
256
257   /// DIGlobal - This is a common class for global variables and subprograms.
258   class DIGlobal : public DIDescriptor {
259   protected:
260     explicit DIGlobal(GlobalVariable *GV, unsigned RequiredTag)
261       : DIDescriptor(GV, RequiredTag) {}
262
263     /// isSubprogram - Return true if the specified tag is legal for
264     /// DISubprogram.
265     static bool isSubprogram(unsigned TAG) {
266       return TAG == dwarf::DW_TAG_subprogram;
267     }
268
269     /// isGlobalVariable - Return true if the specified tag is legal for
270     /// DIGlobalVariable.
271     static bool isGlobalVariable(unsigned TAG) {
272       return TAG == dwarf::DW_TAG_variable;
273     }
274
275   public:
276     virtual ~DIGlobal() {}
277
278     DIDescriptor getContext() const     { return getDescriptorField(2); }
279     const std::string &getName(std::string &F) const {
280       return getStringField(3, F);
281     }
282     const std::string &getDisplayName(std::string &F) const {
283       return getStringField(4, F);
284     }
285     const std::string &getLinkageName(std::string &F) const {
286       return getStringField(5, F);
287     }
288     DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(6); }
289     unsigned getLineNumber() const      { return getUnsignedField(7); }
290     DIType getType() const              { return getFieldAs<DIType>(8); }
291
292     /// isLocalToUnit - Return true if this subprogram is local to the current
293     /// compile unit, like 'static' in C.
294     unsigned isLocalToUnit() const      { return getUnsignedField(9); }
295     unsigned isDefinition() const       { return getUnsignedField(10); }
296
297     /// dump - print global.
298     void dump() const;
299   };
300
301   /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
302   class DISubprogram : public DIGlobal {
303   public:
304     explicit DISubprogram(GlobalVariable *GV = 0);
305     DICompositeType getType() const { return getFieldAs<DICompositeType>(8); }
306
307     /// Verify - Verify that a subprogram descriptor is well formed.
308     bool Verify() const;
309
310     /// dump - print subprogram.
311     void dump() const;
312
313     /// describes - Return true if this subprogram provides debugging
314     /// information for the function F.
315     bool describes(const Function *F);
316   };
317
318   /// DIGlobalVariable - This is a wrapper for a global variable.
319   class DIGlobalVariable : public DIGlobal {
320   public:
321     explicit DIGlobalVariable(GlobalVariable *GV = 0);
322     GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
323
324     /// Verify - Verify that a global variable descriptor is well formed.
325     bool Verify() const;
326
327     /// dump - print global variable.
328     void dump() const;
329   };
330
331   /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
332   /// global etc).
333   class DIVariable : public DIDescriptor {
334   public:
335     explicit DIVariable(GlobalVariable *GV = 0);
336
337     DIDescriptor getContext() const { return getDescriptorField(1); }
338     const std::string &getName(std::string &F) const {
339       return getStringField(2, F);
340     }
341     DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
342     unsigned getLineNumber() const      { return getUnsignedField(4); }
343     DIType getType() const              { return getFieldAs<DIType>(5); }
344
345     /// isVariable - Return true if the specified tag is legal for DIVariable.
346     static bool isVariable(unsigned Tag);
347
348     /// Verify - Verify that a variable descriptor is well formed.
349     bool Verify() const;
350
351     /// dump - print variable.
352     void dump() const;
353   };
354
355   /// DIBlock - This is a wrapper for a block (e.g. a function, scope, etc).
356   class DIBlock : public DIDescriptor {
357   public:
358     explicit DIBlock(GlobalVariable *GV = 0);
359     
360     DIDescriptor getContext() const { return getDescriptorField(1); }
361   };
362
363   /// DIFactory - This object assists with the construction of the various
364   /// descriptors.
365   class DIFactory {
366     Module &M;
367     // Cached values for uniquing and faster lookups.
368     DIAnchor CompileUnitAnchor, SubProgramAnchor, GlobalVariableAnchor;
369     const Type *EmptyStructPtr; // "{}*".
370     Function *StopPointFn;   // llvm.dbg.stoppoint
371     Function *FuncStartFn;   // llvm.dbg.func.start
372     Function *RegionStartFn; // llvm.dbg.region.start
373     Function *RegionEndFn;   // llvm.dbg.region.end
374     Function *DeclareFn;     // llvm.dbg.declare
375     StringMap<Constant*> StringCache;
376     DenseMap<Constant*, DIDescriptor> SimpleConstantCache;
377
378     DIFactory(const DIFactory &);     // DO NOT IMPLEMENT
379     void operator=(const DIFactory&); // DO NOT IMPLEMENT
380   public:
381     explicit DIFactory(Module &m);
382
383     /// GetOrCreateCompileUnitAnchor - Return the anchor for compile units,
384     /// creating a new one if there isn't already one in the module.
385     DIAnchor GetOrCreateCompileUnitAnchor();
386
387     /// GetOrCreateSubprogramAnchor - Return the anchor for subprograms,
388     /// creating a new one if there isn't already one in the module.
389     DIAnchor GetOrCreateSubprogramAnchor();
390
391     /// GetOrCreateGlobalVariableAnchor - Return the anchor for globals,
392     /// creating a new one if there isn't already one in the module.
393     DIAnchor GetOrCreateGlobalVariableAnchor();
394
395     /// GetOrCreateArray - Create an descriptor for an array of descriptors. 
396     /// This implicitly uniques the arrays created.
397     DIArray GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys);
398
399     /// GetOrCreateSubrange - Create a descriptor for a value range.  This
400     /// implicitly uniques the values returned.
401     DISubrange GetOrCreateSubrange(int64_t Lo, int64_t Hi);
402
403     /// CreateCompileUnit - Create a new descriptor for the specified compile
404     /// unit.
405     DICompileUnit CreateCompileUnit(unsigned LangID,
406                                     const std::string &Filename,
407                                     const std::string &Directory,
408                                     const std::string &Producer,
409                                     bool isMain = false,
410                                     bool isOptimized = false,
411                                     const char *Flags = "",
412                                     unsigned RunTimeVer = 0);
413
414     /// CreateEnumerator - Create a single enumerator value.
415     DIEnumerator CreateEnumerator(const std::string &Name, uint64_t Val);
416
417     /// CreateBasicType - Create a basic type like int, float, etc.
418     DIBasicType CreateBasicType(DIDescriptor Context, const std::string &Name,
419                                 DICompileUnit CompileUnit, unsigned LineNumber,
420                                 uint64_t SizeInBits, uint64_t AlignInBits,
421                                 uint64_t OffsetInBits, unsigned Flags,
422                                 unsigned Encoding);
423
424     /// CreateDerivedType - Create a derived type like const qualified type,
425     /// pointer, typedef, etc.
426     DIDerivedType CreateDerivedType(unsigned Tag, DIDescriptor Context,
427                                     const std::string &Name,
428                                     DICompileUnit CompileUnit,
429                                     unsigned LineNumber,
430                                     uint64_t SizeInBits, uint64_t AlignInBits,
431                                     uint64_t OffsetInBits, unsigned Flags,
432                                     DIType DerivedFrom);
433
434     /// CreateCompositeType - Create a composite type like array, struct, etc.
435     DICompositeType CreateCompositeType(unsigned Tag, DIDescriptor Context,
436                                         const std::string &Name,
437                                         DICompileUnit CompileUnit,
438                                         unsigned LineNumber,
439                                         uint64_t SizeInBits,
440                                         uint64_t AlignInBits,
441                                         uint64_t OffsetInBits, unsigned Flags,
442                                         DIType DerivedFrom,
443                                         DIArray Elements,
444                                         unsigned RunTimeLang = 0);
445
446     /// CreateSubprogram - Create a new descriptor for the specified subprogram.
447     /// See comments in DISubprogram for descriptions of these fields.
448     DISubprogram CreateSubprogram(DIDescriptor Context, const std::string &Name,
449                                   const std::string &DisplayName,
450                                   const std::string &LinkageName,
451                                   DICompileUnit CompileUnit, unsigned LineNo,
452                                   DIType Type, bool isLocalToUnit,
453                                   bool isDefinition);
454
455     /// CreateGlobalVariable - Create a new descriptor for the specified global.
456     DIGlobalVariable
457     CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
458                          const std::string &DisplayName,
459                          const std::string &LinkageName, 
460                          DICompileUnit CompileUnit,
461                          unsigned LineNo, DIType Type, bool isLocalToUnit,
462                          bool isDefinition, llvm::GlobalVariable *GV);
463
464     /// CreateVariable - Create a new descriptor for the specified variable.
465     DIVariable CreateVariable(unsigned Tag, DIDescriptor Context,
466                               const std::string &Name,
467                               DICompileUnit CompileUnit, unsigned LineNo,
468                               DIType Type);
469
470     /// CreateBlock - This creates a descriptor for a lexical block with the
471     /// specified parent context.
472     DIBlock CreateBlock(DIDescriptor Context);
473
474     /// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
475     /// inserting it at the end of the specified basic block.
476     void InsertStopPoint(DICompileUnit CU, unsigned LineNo, unsigned ColNo,
477                          BasicBlock *BB);
478
479     /// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
480     /// mark the start of the specified subprogram.
481     void InsertSubprogramStart(DISubprogram SP, BasicBlock *BB);
482
483     /// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
484     /// mark the start of a region for the specified scoping descriptor.
485     void InsertRegionStart(DIDescriptor D, BasicBlock *BB);
486
487     /// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
488     /// mark the end of a region for the specified scoping descriptor.
489     void InsertRegionEnd(DIDescriptor D, BasicBlock *BB);
490
491     /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
492     void InsertDeclare(llvm::Value *Storage, DIVariable D, BasicBlock *BB);
493
494   private:
495     Constant *GetTagConstant(unsigned TAG);
496     Constant *GetStringConstant(const std::string &String);
497     DIAnchor GetOrCreateAnchor(unsigned TAG, const char *Name);
498
499     /// getCastToEmpty - Return the descriptor as a Constant* with type '{}*'.
500     Constant *getCastToEmpty(DIDescriptor D);
501   };
502
503   /// Finds the stoppoint coressponding to this instruction, that is the
504   /// stoppoint that dominates this instruction 
505   const DbgStopPointInst *findStopPoint(const Instruction *Inst);
506
507   /// Finds the stoppoint corresponding to first real (non-debug intrinsic) 
508   /// instruction in this Basic Block, and returns the stoppoint for it.
509   const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB);
510
511   /// Finds the dbg.declare intrinsic corresponding to this value if any.
512   /// It looks through pointer casts too.
513   const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts = true);
514
515   /// Find the debug info descriptor corresponding to this global variable.
516   Value *findDbgGlobalDeclare(GlobalVariable *V);
517
518   bool getLocationInfo(const Value *V, std::string &DisplayName, std::string &Type, 
519                        unsigned &LineNo, std::string &File, std::string &Dir); 
520 } // end namespace llvm
521
522 #endif