Add utility functions to search for DbgStopPointInst corresponding to an
[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_SUPPORT_DEBUGINFO_H
16 #define LLVM_SUPPORT_DEBUGINFO_H
17
18 #include "llvm/ADT/StringMap.h"
19 #include "llvm/ADT/DenseMap.h"
20
21 namespace llvm {
22   class BasicBlock;
23   class Constant;
24   class Function;
25   class GlobalVariable;
26   class Module;
27   class Type;
28   class Value;
29   class DbgStopPointInst;
30   class DbgDeclareInst;
31   class Instruction;
32   
33   class DIDescriptor {
34   public:
35     enum {
36       Version6    = 6 << 16,     // Current version of debug information.
37       Version5    = 5 << 16,     // Constant for version 5.
38       Version4    = 4 << 16,     // Constant for version 4.
39       VersionMask = 0xffff0000   // Mask for version number.
40     };
41     
42   protected:    
43     GlobalVariable *GV;
44     
45     /// DIDescriptor constructor.  If the specified GV is non-null, this checks
46     /// to make sure that the tag in the descriptor matches 'RequiredTag'.  If
47     /// not, the debug info is corrupt and we ignore it.
48     DIDescriptor(GlobalVariable *GV, unsigned RequiredTag);
49     
50     unsigned getTag() const {
51       return getUnsignedField(0) & ~VersionMask;
52     }
53     unsigned getVersion() const {
54       return getUnsignedField(0) & VersionMask;
55     }
56     
57     std::string getStringField(unsigned Elt) const;
58     unsigned getUnsignedField(unsigned Elt) const {
59       return (unsigned)getUInt64Field(Elt);
60     }
61     uint64_t getUInt64Field(unsigned Elt) const;
62     DIDescriptor getDescriptorField(unsigned Elt) const;
63     
64     template <typename DescTy>
65     DescTy getFieldAs(unsigned Elt) const {
66       return DescTy(getDescriptorField(Elt).getGV());
67     }
68   
69     GlobalVariable *getGlobalVariableField(unsigned Elt) const;
70     
71   public:
72     explicit DIDescriptor() : GV(0) {}
73     explicit DIDescriptor(GlobalVariable *gv) : GV(gv) {}
74
75     bool isNull() const { return GV == 0; }
76
77     GlobalVariable *getGV() const { return GV; }
78   };
79   
80   /// DIAnchor - A wrapper for various anchor descriptors.
81   class DIAnchor : public DIDescriptor {
82   public:
83     explicit DIAnchor(GlobalVariable *GV = 0);
84     
85     unsigned getAnchorTag() const { return getUnsignedField(1); }
86   };
87   
88   /// DIArray - This descriptor holds an array of descriptors.
89   class DIArray : public DIDescriptor {
90   public:
91     explicit DIArray(GlobalVariable *GV = 0) : DIDescriptor(GV) {}
92     
93     unsigned getNumElements() const;
94     DIDescriptor getElement(unsigned Idx) const;
95   };
96   
97   /// DICompileUnit - A wrapper for a compile unit.
98   class DICompileUnit : public DIDescriptor {
99   public:
100     explicit DICompileUnit(GlobalVariable *GV = 0);
101     
102     unsigned getLanguage() const     { return getUnsignedField(2); }
103     std::string getFilename() const  { return getStringField(3); }
104     std::string getDirectory() const { return getStringField(4); }
105     std::string getProducer() const  { return getStringField(5); }
106   };
107
108   /// DIEnumerator - A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
109   /// FIXME: it seems strange that this doesn't have either a reference to the
110   /// type/precision or a file/line pair for location info.
111   class DIEnumerator : public DIDescriptor {
112   public:
113     explicit DIEnumerator(GlobalVariable *GV = 0);
114     
115     std::string getName() const  { return getStringField(1); }
116     uint64_t getLanguage() const { return getUInt64Field(2); }
117   };
118   
119   /// DISubrange - This is used to represent ranges, for array bounds.
120   class DISubrange : public DIDescriptor {
121   public:
122     explicit DISubrange(GlobalVariable *GV = 0);
123     
124     int64_t getLo() const { return (int64_t)getUInt64Field(1); }
125     int64_t getHi() const { return (int64_t)getUInt64Field(2); }
126   };
127   
128   /// DIType - This is a wrapper for a type.
129   /// FIXME: Types should be factored much better so that CV qualifiers and
130   /// others do not require a huge and empty descriptor full of zeros.
131   class DIType : public DIDescriptor {
132   protected:
133     DIType(GlobalVariable *GV, unsigned Tag) : DIDescriptor(GV, Tag) {}
134     // This ctor is used when the Tag has already been validated by a derived
135     // ctor.
136     DIType(GlobalVariable *GV, bool, bool) : DIDescriptor(GV) {}
137   public:
138     explicit DIType(GlobalVariable *GV);
139     explicit DIType() {}
140     
141     DIDescriptor getContext() const     { return getDescriptorField(1); }
142     std::string getName() const         { return getStringField(2); }
143     DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
144     unsigned getLineNumber() const      { return getUnsignedField(4); }
145     uint64_t getSizeInBits() const      { return getUInt64Field(5); }
146     uint64_t getAlignInBits() const     { return getUInt64Field(6); }
147     // FIXME: Offset is only used for DW_TAG_member nodes.  Making every type
148     // carry this is just plain insane.
149     uint64_t getOffsetInBits() const    { return getUInt64Field(7); }
150     unsigned getFlags() const           { return getUnsignedField(8); }
151   };
152   
153   /// DIBasicType - A basic type, like 'int' or 'float'.
154   class DIBasicType : public DIType {
155   public:
156     explicit DIBasicType(GlobalVariable *GV);
157     
158     unsigned getEncoding() const { return getUnsignedField(9); }
159   };
160   
161   /// DIDerivedType - A simple derived type, like a const qualified type,
162   /// a typedef, a pointer or reference, etc.
163   class DIDerivedType : public DIType {
164   protected:
165     explicit DIDerivedType(GlobalVariable *GV, bool, bool)
166       : DIType(GV, true, true) {}
167   public:
168     explicit DIDerivedType(GlobalVariable *GV);
169     
170     DIType getTypeDerivedFrom() const { return getFieldAs<DIType>(9); }
171     
172     /// isDerivedType - Return true if the specified tag is legal for
173     /// DIDerivedType.
174     static bool isDerivedType(unsigned TAG);
175   };
176
177   
178   /// DICompositeType - This descriptor holds a type that can refer to multiple
179   /// other types, like a function or struct.
180   /// FIXME: Why is this a DIDerivedType??
181   class DICompositeType : public DIDerivedType {
182   public:
183     explicit DICompositeType(GlobalVariable *GV);
184     
185     DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
186     
187     /// isCompositeType - Return true if the specified tag is legal for
188     /// DICompositeType.
189     static bool isCompositeType(unsigned TAG);
190   };
191   
192   /// DIGlobal - This is a common class for global variables and subprograms.
193   class DIGlobal : public DIDescriptor {
194   protected:
195     explicit DIGlobal(GlobalVariable *GV, unsigned RequiredTag)
196       : DIDescriptor(GV, RequiredTag) {}
197   public:
198     
199     DIDescriptor getContext() const     { return getDescriptorField(2); }
200     std::string getName() const         { return getStringField(3); }
201     std::string getDisplayName() const  { return getStringField(4); }
202     std::string getLinkageName() const  { return getStringField(5); }
203     
204     DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(6); }
205     unsigned getLineNumber() const      { return getUnsignedField(7); }
206     DIType getType() const              { return getFieldAs<DIType>(8); }
207     
208     /// isLocalToUnit - Return true if this subprogram is local to the current
209     /// compile unit, like 'static' in C.
210     unsigned isLocalToUnit() const      { return getUnsignedField(9); }
211     unsigned isDefinition() const       { return getUnsignedField(10); }
212   };
213   
214   
215   /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
216   class DISubprogram : public DIGlobal {
217   public:
218     explicit DISubprogram(GlobalVariable *GV = 0);
219
220   };
221   
222   /// DIGlobalVariable - This is a wrapper for a global variable.
223   class DIGlobalVariable : public DIGlobal {
224   public:
225     explicit DIGlobalVariable(GlobalVariable *GV = 0);
226     
227     GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
228   };
229   
230   
231   /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
232   /// global etc).
233   class DIVariable : public DIDescriptor {
234   public:
235     explicit DIVariable(GlobalVariable *GV = 0);
236     
237     DIDescriptor getContext() const { return getDescriptorField(1); }
238     std::string getName() const { return getStringField(2); }
239     
240     DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
241     unsigned getLineNumber() const      { return getUnsignedField(4); }
242     DIType getType() const              { return getFieldAs<DIType>(5); }
243     
244     /// isVariable - Return true if the specified tag is legal for DIVariable.
245     static bool isVariable(unsigned Tag);
246   };
247   
248   
249   /// DIBlock - This is a wrapper for a block (e.g. a function, scope, etc).
250   class DIBlock : public DIDescriptor {
251   public:
252     explicit DIBlock(GlobalVariable *GV = 0);
253     
254     DIDescriptor getContext() const { return getDescriptorField(1); }
255   };
256   
257   /// DIFactory - This object assists with the construction of the various
258   /// descriptors.
259   class DIFactory {
260     Module &M;
261     // Cached values for uniquing and faster lookups.
262     DIAnchor CompileUnitAnchor, SubProgramAnchor, GlobalVariableAnchor;
263     const Type *EmptyStructPtr; // "{}*".
264     Function *StopPointFn;   // llvm.dbg.stoppoint
265     Function *FuncStartFn;   // llvm.dbg.func.start
266     Function *RegionStartFn; // llvm.dbg.region.start
267     Function *RegionEndFn;   // llvm.dbg.region.end
268     Function *DeclareFn;     // llvm.dbg.declare
269     StringMap<Constant*> StringCache;
270     DenseMap<Constant*, DIDescriptor> SimpleConstantCache;
271     
272     DIFactory(const DIFactory &);     // DO NOT IMPLEMENT
273     void operator=(const DIFactory&); // DO NOT IMPLEMENT
274   public:
275     explicit DIFactory(Module &m);
276     
277     /// GetOrCreateCompileUnitAnchor - Return the anchor for compile units,
278     /// creating a new one if there isn't already one in the module.
279     DIAnchor GetOrCreateCompileUnitAnchor();
280
281     /// GetOrCreateSubprogramAnchor - Return the anchor for subprograms,
282     /// creating a new one if there isn't already one in the module.
283     DIAnchor GetOrCreateSubprogramAnchor();
284
285     /// GetOrCreateGlobalVariableAnchor - Return the anchor for globals,
286     /// creating a new one if there isn't already one in the module.
287     DIAnchor GetOrCreateGlobalVariableAnchor();
288
289     /// GetOrCreateArray - Create an descriptor for an array of descriptors. 
290     /// This implicitly uniques the arrays created.
291     DIArray GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys);
292
293     /// GetOrCreateSubrange - Create a descriptor for a value range.  This
294     /// implicitly uniques the values returned.
295     DISubrange GetOrCreateSubrange(int64_t Lo, int64_t Hi);
296     
297     
298     /// CreateCompileUnit - Create a new descriptor for the specified compile
299     /// unit.
300     DICompileUnit CreateCompileUnit(unsigned LangID,
301                                     const std::string &Filename,
302                                     const std::string &Directory,
303                                     const std::string &Producer);
304
305     /// CreateEnumerator - Create a single enumerator value.
306     DIEnumerator CreateEnumerator(const std::string &Name, uint64_t Val);
307     
308     /// CreateBasicType - Create a basic type like int, float, etc.
309     DIBasicType CreateBasicType(DIDescriptor Context, const std::string &Name,
310                                 DICompileUnit CompileUnit, unsigned LineNumber,
311                                 uint64_t SizeInBits, uint64_t AlignInBits,
312                                 uint64_t OffsetInBits, unsigned Flags,
313                                 unsigned Encoding);
314  
315     /// CreateDerivedType - Create a derived type like const qualified type,
316     /// pointer, typedef, etc.
317     DIDerivedType CreateDerivedType(unsigned Tag, DIDescriptor Context,
318                                     const std::string &Name,
319                                     DICompileUnit CompileUnit,
320                                     unsigned LineNumber,
321                                     uint64_t SizeInBits, uint64_t AlignInBits,
322                                     uint64_t OffsetInBits, unsigned Flags,
323                                     DIType DerivedFrom);
324
325     /// CreateCompositeType - Create a composite type like array, struct, etc.
326     DICompositeType CreateCompositeType(unsigned Tag, DIDescriptor Context,
327                                         const std::string &Name,
328                                         DICompileUnit CompileUnit,
329                                         unsigned LineNumber,
330                                         uint64_t SizeInBits,
331                                         uint64_t AlignInBits,
332                                         uint64_t OffsetInBits, unsigned Flags,
333                                         DIType DerivedFrom,
334                                         DIArray Elements);
335     
336     /// CreateSubprogram - Create a new descriptor for the specified subprogram.
337     /// See comments in DISubprogram for descriptions of these fields.
338     DISubprogram CreateSubprogram(DIDescriptor Context, const std::string &Name,
339                                   const std::string &DisplayName,
340                                   const std::string &LinkageName,
341                                   DICompileUnit CompileUnit, unsigned LineNo,
342                                   DIType Type, bool isLocalToUnit,
343                                   bool isDefinition);
344
345     /// CreateGlobalVariable - Create a new descriptor for the specified global.
346     DIGlobalVariable
347     CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
348                          const std::string &DisplayName,
349                          const std::string &LinkageName, 
350                          DICompileUnit CompileUnit,
351                          unsigned LineNo, DIType Type, bool isLocalToUnit,
352                          bool isDefinition, llvm::GlobalVariable *GV);
353     
354     
355     /// CreateVariable - Create a new descriptor for the specified variable.
356     DIVariable CreateVariable(unsigned Tag, DIDescriptor Context,
357                               const std::string &Name,
358                               DICompileUnit CompileUnit, unsigned LineNo,
359                               DIType Type);
360     
361     /// CreateBlock - This creates a descriptor for a lexical block with the
362     /// specified parent context.
363     DIBlock CreateBlock(DIDescriptor Context);
364     
365     /// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
366     /// inserting it at the end of the specified basic block.
367     void InsertStopPoint(DICompileUnit CU, unsigned LineNo, unsigned ColNo,
368                          BasicBlock *BB);
369     
370     /// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
371     /// mark the start of the specified subprogram.
372     void InsertSubprogramStart(DISubprogram SP, BasicBlock *BB);
373
374     /// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
375     /// mark the start of a region for the specified scoping descriptor.
376     void InsertRegionStart(DIDescriptor D, BasicBlock *BB);
377     
378     /// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
379     /// mark the end of a region for the specified scoping descriptor.
380     void InsertRegionEnd(DIDescriptor D, BasicBlock *BB);
381     
382     /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
383     void InsertDeclare(llvm::Value *Storage, DIVariable D, BasicBlock *BB);
384     
385   private:
386     Constant *GetTagConstant(unsigned TAG);
387     Constant *GetStringConstant(const std::string &String);
388     DIAnchor GetOrCreateAnchor(unsigned TAG, const char *Name);
389     
390     /// getCastToEmpty - Return the descriptor as a Constant* with type '{}*'.
391     Constant *getCastToEmpty(DIDescriptor D);
392   };
393   
394   /// Finds the stoppoint coressponding to this instruction, that is the
395   /// stoppoint that dominates this instruction 
396   const DbgStopPointInst *findStopPoint(const Instruction *Inst);
397
398   /// Finds the stoppoint corresponding to first real (non-debug intrinsic) 
399   /// instruction in this Basic Block, and returns the stoppoint for it.
400   const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB);
401
402   /// Finds the dbg.declare intrinsic corresponding to this value if any.
403   /// It looks through pointer casts too.
404   const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts = true);
405 } // end namespace llvm
406
407 #endif