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