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