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