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