c1be6460aa15f6c88a63ef61300482e8dbd9e605
[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
314   /// DIGlobalVariable - This is a wrapper for a global variable.
315   class DIGlobalVariable : public DIGlobal {
316   public:
317     explicit DIGlobalVariable(GlobalVariable *GV = 0);
318     GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
319
320     /// Verify - Verify that a global variable descriptor is well formed.
321     bool Verify() const;
322
323     /// dump - print global variable.
324     void dump() const;
325   };
326
327   /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
328   /// global etc).
329   class DIVariable : public DIDescriptor {
330   public:
331     explicit DIVariable(GlobalVariable *GV = 0);
332
333     DIDescriptor getContext() const { return getDescriptorField(1); }
334     const std::string &getName(std::string &F) const {
335       return getStringField(2, F);
336     }
337     DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
338     unsigned getLineNumber() const      { return getUnsignedField(4); }
339     DIType getType() const              { return getFieldAs<DIType>(5); }
340
341     /// isVariable - Return true if the specified tag is legal for DIVariable.
342     static bool isVariable(unsigned Tag);
343
344     /// Verify - Verify that a variable descriptor is well formed.
345     bool Verify() const;
346
347     /// dump - print variable.
348     void dump() const;
349   };
350
351   /// DIBlock - This is a wrapper for a block (e.g. a function, scope, etc).
352   class DIBlock : public DIDescriptor {
353   public:
354     explicit DIBlock(GlobalVariable *GV = 0);
355     
356     DIDescriptor getContext() const { return getDescriptorField(1); }
357   };
358
359   /// DIFactory - This object assists with the construction of the various
360   /// descriptors.
361   class DIFactory {
362     Module &M;
363     // Cached values for uniquing and faster lookups.
364     DIAnchor CompileUnitAnchor, SubProgramAnchor, GlobalVariableAnchor;
365     const Type *EmptyStructPtr; // "{}*".
366     Function *StopPointFn;   // llvm.dbg.stoppoint
367     Function *FuncStartFn;   // llvm.dbg.func.start
368     Function *RegionStartFn; // llvm.dbg.region.start
369     Function *RegionEndFn;   // llvm.dbg.region.end
370     Function *DeclareFn;     // llvm.dbg.declare
371     StringMap<Constant*> StringCache;
372     DenseMap<Constant*, DIDescriptor> SimpleConstantCache;
373
374     DIFactory(const DIFactory &);     // DO NOT IMPLEMENT
375     void operator=(const DIFactory&); // DO NOT IMPLEMENT
376   public:
377     explicit DIFactory(Module &m);
378
379     /// GetOrCreateCompileUnitAnchor - Return the anchor for compile units,
380     /// creating a new one if there isn't already one in the module.
381     DIAnchor GetOrCreateCompileUnitAnchor();
382
383     /// GetOrCreateSubprogramAnchor - Return the anchor for subprograms,
384     /// creating a new one if there isn't already one in the module.
385     DIAnchor GetOrCreateSubprogramAnchor();
386
387     /// GetOrCreateGlobalVariableAnchor - Return the anchor for globals,
388     /// creating a new one if there isn't already one in the module.
389     DIAnchor GetOrCreateGlobalVariableAnchor();
390
391     /// GetOrCreateArray - Create an descriptor for an array of descriptors. 
392     /// This implicitly uniques the arrays created.
393     DIArray GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys);
394
395     /// GetOrCreateSubrange - Create a descriptor for a value range.  This
396     /// implicitly uniques the values returned.
397     DISubrange GetOrCreateSubrange(int64_t Lo, int64_t Hi);
398
399     /// CreateCompileUnit - Create a new descriptor for the specified compile
400     /// unit.
401     DICompileUnit CreateCompileUnit(unsigned LangID,
402                                     const std::string &Filename,
403                                     const std::string &Directory,
404                                     const std::string &Producer,
405                                     bool isMain = false,
406                                     bool isOptimized = false,
407                                     const char *Flags = "",
408                                     unsigned RunTimeVer = 0);
409
410     /// CreateEnumerator - Create a single enumerator value.
411     DIEnumerator CreateEnumerator(const std::string &Name, uint64_t Val);
412
413     /// CreateBasicType - Create a basic type like int, float, etc.
414     DIBasicType CreateBasicType(DIDescriptor Context, const std::string &Name,
415                                 DICompileUnit CompileUnit, unsigned LineNumber,
416                                 uint64_t SizeInBits, uint64_t AlignInBits,
417                                 uint64_t OffsetInBits, unsigned Flags,
418                                 unsigned Encoding);
419
420     /// CreateDerivedType - Create a derived type like const qualified type,
421     /// pointer, typedef, etc.
422     DIDerivedType CreateDerivedType(unsigned Tag, DIDescriptor Context,
423                                     const std::string &Name,
424                                     DICompileUnit CompileUnit,
425                                     unsigned LineNumber,
426                                     uint64_t SizeInBits, uint64_t AlignInBits,
427                                     uint64_t OffsetInBits, unsigned Flags,
428                                     DIType DerivedFrom);
429
430     /// CreateCompositeType - Create a composite type like array, struct, etc.
431     DICompositeType CreateCompositeType(unsigned Tag, DIDescriptor Context,
432                                         const std::string &Name,
433                                         DICompileUnit CompileUnit,
434                                         unsigned LineNumber,
435                                         uint64_t SizeInBits,
436                                         uint64_t AlignInBits,
437                                         uint64_t OffsetInBits, unsigned Flags,
438                                         DIType DerivedFrom,
439                                         DIArray Elements,
440                                         unsigned RunTimeLang = 0);
441
442     /// CreateSubprogram - Create a new descriptor for the specified subprogram.
443     /// See comments in DISubprogram for descriptions of these fields.
444     DISubprogram CreateSubprogram(DIDescriptor Context, const std::string &Name,
445                                   const std::string &DisplayName,
446                                   const std::string &LinkageName,
447                                   DICompileUnit CompileUnit, unsigned LineNo,
448                                   DIType Type, bool isLocalToUnit,
449                                   bool isDefinition);
450
451     /// CreateGlobalVariable - Create a new descriptor for the specified global.
452     DIGlobalVariable
453     CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
454                          const std::string &DisplayName,
455                          const std::string &LinkageName, 
456                          DICompileUnit CompileUnit,
457                          unsigned LineNo, DIType Type, bool isLocalToUnit,
458                          bool isDefinition, llvm::GlobalVariable *GV);
459
460     /// CreateVariable - Create a new descriptor for the specified variable.
461     DIVariable CreateVariable(unsigned Tag, DIDescriptor Context,
462                               const std::string &Name,
463                               DICompileUnit CompileUnit, unsigned LineNo,
464                               DIType Type);
465
466     /// CreateBlock - This creates a descriptor for a lexical block with the
467     /// specified parent context.
468     DIBlock CreateBlock(DIDescriptor Context);
469
470     /// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
471     /// inserting it at the end of the specified basic block.
472     void InsertStopPoint(DICompileUnit CU, unsigned LineNo, unsigned ColNo,
473                          BasicBlock *BB);
474
475     /// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
476     /// mark the start of the specified subprogram.
477     void InsertSubprogramStart(DISubprogram SP, BasicBlock *BB);
478
479     /// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
480     /// mark the start of a region for the specified scoping descriptor.
481     void InsertRegionStart(DIDescriptor D, BasicBlock *BB);
482
483     /// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
484     /// mark the end of a region for the specified scoping descriptor.
485     void InsertRegionEnd(DIDescriptor D, BasicBlock *BB);
486
487     /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
488     void InsertDeclare(llvm::Value *Storage, DIVariable D, BasicBlock *BB);
489
490   private:
491     Constant *GetTagConstant(unsigned TAG);
492     Constant *GetStringConstant(const std::string &String);
493     DIAnchor GetOrCreateAnchor(unsigned TAG, const char *Name);
494
495     /// getCastToEmpty - Return the descriptor as a Constant* with type '{}*'.
496     Constant *getCastToEmpty(DIDescriptor D);
497   };
498
499   /// Finds the stoppoint coressponding to this instruction, that is the
500   /// stoppoint that dominates this instruction 
501   const DbgStopPointInst *findStopPoint(const Instruction *Inst);
502
503   /// Finds the stoppoint corresponding to first real (non-debug intrinsic) 
504   /// instruction in this Basic Block, and returns the stoppoint for it.
505   const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB);
506
507   /// Finds the dbg.declare intrinsic corresponding to this value if any.
508   /// It looks through pointer casts too.
509   const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts = true);
510 } // end namespace llvm
511
512 #endif