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