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