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