Implement loop splitting analysis.
[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/ADT/SmallVector.h"
21 #include "llvm/ADT/SmallPtrSet.h"
22 #include "llvm/ADT/StringRef.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   class DbgDeclareInst;
34   class Instruction;
35   class MDNode;
36   class LLVMContext;
37   class raw_ostream;
38
39   /// DIDescriptor - A thin wraper around MDNode to access encoded debug info.
40   /// This should not be stored in a container, because underly MDNode may
41   /// change in certain situations.
42   class DIDescriptor {
43   protected:
44     const MDNode *DbgNode;
45
46     StringRef getStringField(unsigned Elt) 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));
56     }
57
58     GlobalVariable *getGlobalVariableField(unsigned Elt) const;
59     Function *getFunctionField(unsigned Elt) const;
60
61   public:
62     explicit DIDescriptor() : DbgNode(0) {}
63     explicit DIDescriptor(const MDNode *N) : DbgNode(N) {}
64
65     bool Verify() const { return DbgNode != 0; }
66
67     operator MDNode *() const { return const_cast<MDNode*>(DbgNode); }
68     MDNode *operator ->() const { return const_cast<MDNode*>(DbgNode); }
69
70     unsigned getVersion() const {
71       return getUnsignedField(0) & LLVMDebugVersionMask;
72     }
73
74     unsigned getTag() const {
75       return getUnsignedField(0) & ~LLVMDebugVersionMask;
76     }
77
78     /// print - print descriptor.
79     void print(raw_ostream &OS) const;
80
81     /// dump - print descriptor to dbgs() with a newline.
82     void dump() const;
83
84     bool isDerivedType() const;
85     bool isCompositeType() const;
86     bool isBasicType() const;
87     bool isVariable() const;
88     bool isSubprogram() const;
89     bool isGlobalVariable() const;
90     bool isScope() const;
91     bool isFile() const;
92     bool isCompileUnit() const;
93     bool isNameSpace() const;
94     bool isLexicalBlock() const;
95     bool isSubrange() const;
96     bool isEnumerator() const;
97     bool isType() const;
98     bool isGlobal() const;
99   };
100
101   /// DISubrange - This is used to represent ranges, for array bounds.
102   class DISubrange : public DIDescriptor {
103   public:
104     explicit DISubrange(const MDNode *N = 0) : DIDescriptor(N) {}
105
106     int64_t getLo() const { return (int64_t)getUInt64Field(1); }
107     int64_t getHi() const { return (int64_t)getUInt64Field(2); }
108   };
109
110   /// DIArray - This descriptor holds an array of descriptors.
111   class DIArray : public DIDescriptor {
112   public:
113     explicit DIArray(const MDNode *N = 0)
114       : DIDescriptor(N) {}
115
116     unsigned getNumElements() const;
117     DIDescriptor getElement(unsigned Idx) const {
118       return getDescriptorField(Idx);
119     }
120   };
121
122   /// DIScope - A base class for various scopes.
123   class DIScope : public DIDescriptor {
124   public:
125     explicit DIScope(const MDNode *N = 0) : DIDescriptor (N) {}
126     virtual ~DIScope() {}
127
128     StringRef getFilename() const;
129     StringRef getDirectory() const;
130   };
131
132   /// DICompileUnit - A wrapper for a compile unit.
133   class DICompileUnit : public DIScope {
134   public:
135     explicit DICompileUnit(const MDNode *N = 0) : DIScope(N) {}
136
137     unsigned getLanguage() const   { return getUnsignedField(2); }
138     StringRef getFilename() const  { return getStringField(3);   }
139     StringRef getDirectory() const { return getStringField(4);   }
140     StringRef getProducer() const  { return getStringField(5);   }
141
142     /// isMain - Each input file is encoded as a separate compile unit in LLVM
143     /// debugging information output. However, many target specific tool chains
144     /// prefer to encode only one compile unit in an object file. In this
145     /// situation, the LLVM code generator will include  debugging information
146     /// entities in the compile unit that is marked as main compile unit. The
147     /// code generator accepts maximum one main compile unit per module. If a
148     /// module does not contain any main compile unit then the code generator
149     /// will emit multiple compile units in the output object file.
150
151     bool isMain() const                { return getUnsignedField(6); }
152     bool isOptimized() const           { return getUnsignedField(7); }
153     StringRef getFlags() const       { return getStringField(8);   }
154     unsigned getRunTimeVersion() const { return getUnsignedField(9); }
155
156     /// Verify - Verify that a compile unit is well formed.
157     bool Verify() const;
158
159     /// print - print compile unit.
160     void print(raw_ostream &OS) const;
161
162     /// dump - print compile unit to dbgs() with a newline.
163     void dump() const;
164   };
165
166   /// DIFile - This is a wrapper for a file.
167   class DIFile : public DIScope {
168   public:
169     explicit DIFile(const MDNode *N = 0) : DIScope(N) {
170       if (DbgNode && !isFile())
171         DbgNode = 0;
172     }
173     StringRef getFilename() const  { return getStringField(1);   }
174     StringRef getDirectory() const { return getStringField(2);   }
175     DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
176   };
177
178   /// DIEnumerator - A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
179   /// FIXME: it seems strange that this doesn't have either a reference to the
180   /// type/precision or a file/line pair for location info.
181   class DIEnumerator : public DIDescriptor {
182   public:
183     explicit DIEnumerator(const MDNode *N = 0) : DIDescriptor(N) {}
184
185     StringRef getName() const        { return getStringField(1); }
186     uint64_t getEnumValue() const      { return getUInt64Field(2); }
187   };
188
189   /// DIType - This is a wrapper for a type.
190   /// FIXME: Types should be factored much better so that CV qualifiers and
191   /// others do not require a huge and empty descriptor full of zeros.
192   class DIType : public DIScope {
193   public:
194     enum {
195       FlagPrivate          = 1 << 0,
196       FlagProtected        = 1 << 1,
197       FlagFwdDecl          = 1 << 2,
198       FlagAppleBlock       = 1 << 3,
199       FlagBlockByrefStruct = 1 << 4,
200       FlagVirtual          = 1 << 5,
201       FlagArtificial       = 1 << 6  // To identify artificial arguments in
202                                      // a subroutine type. e.g. "this" in c++.
203     };
204
205   protected:
206     // This ctor is used when the Tag has already been validated by a derived
207     // ctor.
208     DIType(const MDNode *N, bool, bool) : DIScope(N) {}
209
210   public:
211
212     /// Verify - Verify that a type descriptor is well formed.
213     bool Verify() const;
214   public:
215     explicit DIType(const MDNode *N);
216     explicit DIType() {}
217     virtual ~DIType() {}
218
219     DIScope getContext() const          { return getFieldAs<DIScope>(1); }
220     StringRef getName() const           { return getStringField(2);     }
221     DICompileUnit getCompileUnit() const{ 
222       if (getVersion() == llvm::LLVMDebugVersion7)
223         return getFieldAs<DICompileUnit>(3);
224
225       DIFile F = getFieldAs<DIFile>(3);
226       return F.getCompileUnit();
227     }
228     unsigned getLineNumber() const      { return getUnsignedField(4); }
229     uint64_t getSizeInBits() const      { return getUInt64Field(5); }
230     uint64_t getAlignInBits() const     { return getUInt64Field(6); }
231     // FIXME: Offset is only used for DW_TAG_member nodes.  Making every type
232     // carry this is just plain insane.
233     uint64_t getOffsetInBits() const    { return getUInt64Field(7); }
234     unsigned getFlags() const           { return getUnsignedField(8); }
235     bool isPrivate() const {
236       return (getFlags() & FlagPrivate) != 0;
237     }
238     bool isProtected() const {
239       return (getFlags() & FlagProtected) != 0;
240     }
241     bool isForwardDecl() const {
242       return (getFlags() & FlagFwdDecl) != 0;
243     }
244     // isAppleBlock - Return true if this is the Apple Blocks extension.
245     bool isAppleBlockExtension() const {
246       return (getFlags() & FlagAppleBlock) != 0;
247     }
248     bool isBlockByrefStruct() const {
249       return (getFlags() & FlagBlockByrefStruct) != 0;
250     }
251     bool isVirtual() const {
252       return (getFlags() & FlagVirtual) != 0;
253     }
254     bool isArtificial() const {
255       return (getFlags() & FlagArtificial) != 0;
256     }
257     bool isValid() const {
258       return DbgNode && (isBasicType() || isDerivedType() || isCompositeType());
259     }
260     StringRef getFilename() const    { return getCompileUnit().getFilename();}
261     StringRef getDirectory() const   { return getCompileUnit().getDirectory();}
262
263     /// print - print type.
264     void print(raw_ostream &OS) const;
265
266     /// dump - print type to dbgs() with a newline.
267     void dump() const;
268   };
269
270   /// DIBasicType - A basic type, like 'int' or 'float'.
271   class DIBasicType : public DIType {
272   public:
273     explicit DIBasicType(const MDNode *N = 0) : DIType(N) {}
274
275     unsigned getEncoding() const { return getUnsignedField(9); }
276
277     /// print - print basic type.
278     void print(raw_ostream &OS) const;
279
280     /// dump - print basic type to dbgs() with a newline.
281     void dump() const;
282   };
283
284   /// DIDerivedType - A simple derived type, like a const qualified type,
285   /// a typedef, a pointer or reference, etc.
286   class DIDerivedType : public DIType {
287   protected:
288     explicit DIDerivedType(const MDNode *N, bool, bool)
289       : DIType(N, true, true) {}
290   public:
291     explicit DIDerivedType(const MDNode *N = 0)
292       : DIType(N, true, true) {}
293
294     DIType getTypeDerivedFrom() const { return getFieldAs<DIType>(9); }
295
296     /// getOriginalTypeSize - If this type is derived from a base type then
297     /// return base type size.
298     uint64_t getOriginalTypeSize() const;
299
300     /// print - print derived type.
301     void print(raw_ostream &OS) const;
302
303     /// dump - print derived type to dbgs() with a newline.
304     void dump() const;
305
306     /// replaceAllUsesWith - Replace all uses of debug info referenced by
307     /// this descriptor. After this completes, the current debug info value
308     /// is erased.
309     void replaceAllUsesWith(DIDescriptor &D);
310   };
311
312   /// DICompositeType - This descriptor holds a type that can refer to multiple
313   /// other types, like a function or struct.
314   /// FIXME: Why is this a DIDerivedType??
315   class DICompositeType : public DIDerivedType {
316   public:
317     explicit DICompositeType(const MDNode *N = 0)
318       : DIDerivedType(N, true, true) {
319       if (N && !isCompositeType())
320         DbgNode = 0;
321     }
322
323     DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
324     unsigned getRunTimeLang() const { return getUnsignedField(11); }
325     DICompositeType getContainingType() const {
326       return getFieldAs<DICompositeType>(12);
327     }
328
329     /// Verify - Verify that a composite type descriptor is well formed.
330     bool Verify() const;
331
332     /// print - print composite type.
333     void print(raw_ostream &OS) const;
334
335     /// dump - print composite type to dbgs() with a newline.
336     void dump() const;
337   };
338
339   /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
340   class DISubprogram : public DIScope {
341   public:
342     explicit DISubprogram(const MDNode *N = 0) : DIScope(N) {}
343
344     DIScope getContext() const          { return getFieldAs<DIScope>(2); }
345     StringRef getName() const         { return getStringField(3); }
346     StringRef getDisplayName() const  { return getStringField(4); }
347     StringRef getLinkageName() const  { return getStringField(5); }
348     DICompileUnit getCompileUnit() const{ 
349       if (getVersion() == llvm::LLVMDebugVersion7)
350         return getFieldAs<DICompileUnit>(6);
351
352       DIFile F = getFieldAs<DIFile>(6); 
353       return F.getCompileUnit();
354     }
355     unsigned getLineNumber() const      { return getUnsignedField(7); }
356     DICompositeType getType() const { return getFieldAs<DICompositeType>(8); }
357
358     /// getReturnTypeName - Subprogram return types are encoded either as
359     /// DIType or as DICompositeType.
360     StringRef getReturnTypeName() const {
361       DICompositeType DCT(getFieldAs<DICompositeType>(8));
362       if (DCT.Verify()) {
363         DIArray A = DCT.getTypeArray();
364         DIType T(A.getElement(0));
365         return T.getName();
366       }
367       DIType T(getFieldAs<DIType>(8));
368       return T.getName();
369     }
370
371     /// isLocalToUnit - Return true if this subprogram is local to the current
372     /// compile unit, like 'static' in C.
373     unsigned isLocalToUnit() const     { return getUnsignedField(9); }
374     unsigned isDefinition() const      { return getUnsignedField(10); }
375
376     unsigned getVirtuality() const { return getUnsignedField(11); }
377     unsigned getVirtualIndex() const { return getUnsignedField(12); }
378
379     DICompositeType getContainingType() const {
380       return getFieldAs<DICompositeType>(13);
381     }
382     unsigned isArtificial() const    { return getUnsignedField(14); }
383     unsigned isOptimized() const;
384
385     StringRef getFilename() const    { 
386       if (getVersion() == llvm::LLVMDebugVersion7)
387         return getCompileUnit().getFilename();
388
389       DIFile F = getFieldAs<DIFile>(6); 
390       return F.getFilename();
391     }
392
393     StringRef getDirectory() const   { 
394       if (getVersion() == llvm::LLVMDebugVersion7)
395         return getCompileUnit().getFilename();
396
397       DIFile F = getFieldAs<DIFile>(6); 
398       return F.getDirectory();
399     }
400
401     /// Verify - Verify that a subprogram descriptor is well formed.
402     bool Verify() const;
403
404     /// print - print subprogram.
405     void print(raw_ostream &OS) const;
406
407     /// dump - print subprogram to dbgs() with a newline.
408     void dump() const;
409
410     /// describes - Return true if this subprogram provides debugging
411     /// information for the function F.
412     bool describes(const Function *F);
413
414     Function *getFunction() const { return getFunctionField(16); }
415   };
416
417   /// DIGlobalVariable - This is a wrapper for a global variable.
418   class DIGlobalVariable : public DIDescriptor {
419   public:
420     explicit DIGlobalVariable(const MDNode *N = 0) : DIDescriptor(N) {}
421
422     DIScope getContext() const          { return getFieldAs<DIScope>(2); }
423     StringRef getName() const         { return getStringField(3); }
424     StringRef getDisplayName() const  { return getStringField(4); }
425     StringRef getLinkageName() const  { return getStringField(5); }
426     DICompileUnit getCompileUnit() const{ 
427       if (getVersion() == llvm::LLVMDebugVersion7)
428         return getFieldAs<DICompileUnit>(6);
429
430       DIFile F = getFieldAs<DIFile>(6); 
431       return F.getCompileUnit();
432     }
433
434     unsigned getLineNumber() const      { return getUnsignedField(7); }
435     DIType getType() const              { return getFieldAs<DIType>(8); }
436     unsigned isLocalToUnit() const      { return getUnsignedField(9); }
437     unsigned isDefinition() const       { return getUnsignedField(10); }
438
439     GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
440
441     /// Verify - Verify that a global variable descriptor is well formed.
442     bool Verify() const;
443
444     /// print - print global variable.
445     void print(raw_ostream &OS) const;
446
447     /// dump - print global variable to dbgs() with a newline.
448     void dump() const;
449   };
450
451   /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
452   /// global etc).
453   class DIVariable : public DIDescriptor {
454   public:
455     explicit DIVariable(const MDNode *N = 0)
456       : DIDescriptor(N) {}
457
458     DIScope getContext() const          { return getFieldAs<DIScope>(1); }
459     StringRef getName() const           { return getStringField(2);     }
460     DICompileUnit getCompileUnit() const{ 
461       if (getVersion() == llvm::LLVMDebugVersion7)
462         return getFieldAs<DICompileUnit>(3);
463
464       DIFile F = getFieldAs<DIFile>(3); 
465       return F.getCompileUnit();
466     }
467     unsigned getLineNumber() const      { return getUnsignedField(4); }
468     DIType getType() const              { return getFieldAs<DIType>(5); }
469
470
471     /// Verify - Verify that a variable descriptor is well formed.
472     bool Verify() const;
473
474     /// HasComplexAddr - Return true if the variable has a complex address.
475     bool hasComplexAddress() const {
476       return getNumAddrElements() > 0;
477     }
478
479     unsigned getNumAddrElements() const;
480     
481     uint64_t getAddrElement(unsigned Idx) const {
482       return getUInt64Field(Idx+6);
483     }
484
485     /// isBlockByrefVariable - Return true if the variable was declared as
486     /// a "__block" variable (Apple Blocks).
487     bool isBlockByrefVariable() const {
488       return getType().isBlockByrefStruct();
489     }
490
491     /// isInlinedFnArgument - Return trule if this variable provides debugging
492     /// information for an inlined function arguments.
493     bool isInlinedFnArgument(const Function *CurFn);
494
495     /// print - print variable.
496     void print(raw_ostream &OS) const;
497
498     /// dump - print variable to dbgs() with a newline.
499     void dump() const;
500   };
501
502   /// DILexicalBlock - This is a wrapper for a lexical block.
503   class DILexicalBlock : public DIScope {
504   public:
505     explicit DILexicalBlock(const MDNode *N = 0) : DIScope(N) {}
506     DIScope getContext() const       { return getFieldAs<DIScope>(1);      }
507     unsigned getLineNumber() const   { return getUnsignedField(2);         }
508     unsigned getColumnNumber() const { return getUnsignedField(3);         }
509     StringRef getDirectory() const {
510       DIFile F = getFieldAs<DIFile>(4);
511       StringRef dir = F.getDirectory();
512       return !dir.empty() ? dir : getContext().getDirectory();
513     }
514     StringRef getFilename() const {
515       DIFile F = getFieldAs<DIFile>(4);
516       StringRef filename = F.getFilename();
517       return !filename.empty() ? filename : getContext().getFilename();
518     }
519   };
520
521   /// DINameSpace - A wrapper for a C++ style name space.
522   class DINameSpace : public DIScope { 
523   public:
524     explicit DINameSpace(const MDNode *N = 0) : DIScope(N) {}
525     DIScope getContext() const     { return getFieldAs<DIScope>(1);      }
526     StringRef getName() const      { return getStringField(2);           }
527     StringRef getDirectory() const { return getContext().getDirectory(); }
528     StringRef getFilename() const  { return getContext().getFilename();  }
529     DICompileUnit getCompileUnit() const{ 
530       if (getVersion() == llvm::LLVMDebugVersion7)
531         return getFieldAs<DICompileUnit>(3);
532
533       DIFile F = getFieldAs<DIFile>(3); 
534       return F.getCompileUnit();
535     }
536     unsigned getLineNumber() const { return getUnsignedField(4);         }
537     bool Verify() const;
538   };
539
540   /// DILocation - This object holds location information. This object
541   /// is not associated with any DWARF tag.
542   class DILocation : public DIDescriptor {
543   public:
544     explicit DILocation(const MDNode *N) : DIDescriptor(N) { }
545
546     unsigned getLineNumber() const     { return getUnsignedField(0); }
547     unsigned getColumnNumber() const   { return getUnsignedField(1); }
548     DIScope  getScope() const          { return getFieldAs<DIScope>(2); }
549     DILocation getOrigLocation() const { return getFieldAs<DILocation>(3); }
550     StringRef getFilename() const    { return getScope().getFilename(); }
551     StringRef getDirectory() const   { return getScope().getDirectory(); }
552     bool Verify() const;
553   };
554
555   /// DIFactory - This object assists with the construction of the various
556   /// descriptors.
557   class DIFactory {
558     Module &M;
559     LLVMContext& VMContext;
560
561     Function *DeclareFn;     // llvm.dbg.declare
562     Function *ValueFn;       // llvm.dbg.value
563
564     DIFactory(const DIFactory &);     // DO NOT IMPLEMENT
565     void operator=(const DIFactory&); // DO NOT IMPLEMENT
566   public:
567     enum ComplexAddrKind { OpPlus=1, OpDeref };
568
569     explicit DIFactory(Module &m);
570
571     /// GetOrCreateArray - Create an descriptor for an array of descriptors.
572     /// This implicitly uniques the arrays created.
573     DIArray GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys);
574
575     /// GetOrCreateSubrange - Create a descriptor for a value range.  This
576     /// implicitly uniques the values returned.
577     DISubrange GetOrCreateSubrange(int64_t Lo, int64_t Hi);
578
579     /// CreateCompileUnit - Create a new descriptor for the specified compile
580     /// unit.
581     DICompileUnit CreateCompileUnit(unsigned LangID,
582                                     StringRef Filename,
583                                     StringRef Directory,
584                                     StringRef Producer,
585                                     bool isMain = false,
586                                     bool isOptimized = false,
587                                     StringRef Flags = "",
588                                     unsigned RunTimeVer = 0);
589
590     /// CreateFile -  Create a new descriptor for the specified file.
591     DIFile CreateFile(StringRef Filename, StringRef Directory,
592                       DICompileUnit CU);
593
594     /// CreateEnumerator - Create a single enumerator value.
595     DIEnumerator CreateEnumerator(StringRef Name, uint64_t Val);
596
597     /// CreateBasicType - Create a basic type like int, float, etc.
598     DIBasicType CreateBasicType(DIDescriptor Context, StringRef Name,
599                                 DIFile F, unsigned LineNumber,
600                                 uint64_t SizeInBits, uint64_t AlignInBits,
601                                 uint64_t OffsetInBits, unsigned Flags,
602                                 unsigned Encoding);
603
604     /// CreateBasicType - Create a basic type like int, float, etc.
605     DIBasicType CreateBasicTypeEx(DIDescriptor Context, StringRef Name,
606                                 DIFile F, unsigned LineNumber,
607                                 Constant *SizeInBits, Constant *AlignInBits,
608                                 Constant *OffsetInBits, unsigned Flags,
609                                 unsigned Encoding);
610
611     /// CreateDerivedType - Create a derived type like const qualified type,
612     /// pointer, typedef, etc.
613     DIDerivedType CreateDerivedType(unsigned Tag, DIDescriptor Context,
614                                     StringRef Name,
615                                     DIFile F,
616                                     unsigned LineNumber,
617                                     uint64_t SizeInBits, uint64_t AlignInBits,
618                                     uint64_t OffsetInBits, unsigned Flags,
619                                     DIType DerivedFrom);
620
621     /// CreateDerivedType - Create a derived type like const qualified type,
622     /// pointer, typedef, etc.
623     DIDerivedType CreateDerivedTypeEx(unsigned Tag, DIDescriptor Context,
624                                       StringRef Name,
625                                       DIFile F,
626                                       unsigned LineNumber,
627                                       Constant *SizeInBits, 
628                                       Constant *AlignInBits,
629                                       Constant *OffsetInBits, unsigned Flags,
630                                       DIType DerivedFrom);
631
632     /// CreateCompositeType - Create a composite type like array, struct, etc.
633     DICompositeType CreateCompositeType(unsigned Tag, DIDescriptor Context,
634                                         StringRef Name,
635                                         DIFile F,
636                                         unsigned LineNumber,
637                                         uint64_t SizeInBits,
638                                         uint64_t AlignInBits,
639                                         uint64_t OffsetInBits, unsigned Flags,
640                                         DIType DerivedFrom,
641                                         DIArray Elements,
642                                         unsigned RunTimeLang = 0,
643                                         MDNode *ContainingType = 0);
644
645     /// CreateArtificialType - Create a new DIType with "artificial" flag set.
646     DIType CreateArtificialType(DIType Ty);
647
648     /// CreateCompositeType - Create a composite type like array, struct, etc.
649     DICompositeType CreateCompositeTypeEx(unsigned Tag, DIDescriptor Context,
650                                           StringRef Name,
651                                           DIFile F,
652                                           unsigned LineNumber,
653                                           Constant *SizeInBits,
654                                           Constant *AlignInBits,
655                                           Constant *OffsetInBits, 
656                                           unsigned Flags,
657                                           DIType DerivedFrom,
658                                           DIArray Elements,
659                                           unsigned RunTimeLang = 0);
660
661     /// CreateSubprogram - Create a new descriptor for the specified subprogram.
662     /// See comments in DISubprogram for descriptions of these fields.
663     DISubprogram CreateSubprogram(DIDescriptor Context, StringRef Name,
664                                   StringRef DisplayName,
665                                   StringRef LinkageName,
666                                   DIFile F, unsigned LineNo,
667                                   DIType Ty, bool isLocalToUnit,
668                                   bool isDefinition,
669                                   unsigned VK = 0,
670                                   unsigned VIndex = 0,
671                                   DIType = DIType(),
672                                   bool isArtificial = 0,
673                                   bool isOptimized = false,
674                                   Function *Fn = 0);
675
676     /// CreateSubprogramDefinition - Create new subprogram descriptor for the
677     /// given declaration. 
678     DISubprogram CreateSubprogramDefinition(DISubprogram &SPDeclaration);
679
680     /// CreateGlobalVariable - Create a new descriptor for the specified global.
681     DIGlobalVariable
682     CreateGlobalVariable(DIDescriptor Context, StringRef Name,
683                          StringRef DisplayName,
684                          StringRef LinkageName,
685                          DIFile F,
686                          unsigned LineNo, DIType Ty, bool isLocalToUnit,
687                          bool isDefinition, llvm::GlobalVariable *GV);
688
689     /// CreateVariable - Create a new descriptor for the specified variable.
690     DIVariable CreateVariable(unsigned Tag, DIDescriptor Context,
691                               StringRef Name,
692                               DIFile F, unsigned LineNo,
693                               DIType Ty, bool AlwaysPreserve = false);
694
695     /// CreateComplexVariable - Create a new descriptor for the specified
696     /// variable which has a complex address expression for its address.
697     DIVariable CreateComplexVariable(unsigned Tag, DIDescriptor Context,
698                                      const std::string &Name,
699                                      DIFile F, unsigned LineNo,
700                                      DIType Ty,
701                                      SmallVector<Value *, 9> &addr);
702
703     /// CreateLexicalBlock - This creates a descriptor for a lexical block
704     /// with the specified parent context.
705     DILexicalBlock CreateLexicalBlock(DIDescriptor Context, DIFile F,
706                                       unsigned Line = 0, unsigned Col = 0);
707
708     /// CreateNameSpace - This creates new descriptor for a namespace
709     /// with the specified parent context.
710     DINameSpace CreateNameSpace(DIDescriptor Context, StringRef Name,
711                                 DIFile F, unsigned LineNo);
712
713     /// CreateLocation - Creates a debug info location.
714     DILocation CreateLocation(unsigned LineNo, unsigned ColumnNo,
715                               DIScope S, DILocation OrigLoc);
716
717     /// CreateLocation - Creates a debug info location.
718     DILocation CreateLocation(unsigned LineNo, unsigned ColumnNo,
719                               DIScope S, MDNode *OrigLoc = 0);
720
721     /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
722     Instruction *InsertDeclare(llvm::Value *Storage, DIVariable D,
723                                BasicBlock *InsertAtEnd);
724
725     /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
726     Instruction *InsertDeclare(llvm::Value *Storage, DIVariable D,
727                                Instruction *InsertBefore);
728
729     /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
730     Instruction *InsertDbgValueIntrinsic(llvm::Value *V, uint64_t Offset,
731                                          DIVariable D, BasicBlock *InsertAtEnd);
732
733     /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
734     Instruction *InsertDbgValueIntrinsic(llvm::Value *V, uint64_t Offset,
735                                        DIVariable D, Instruction *InsertBefore);
736   private:
737     Constant *GetTagConstant(unsigned TAG);
738   };
739
740   bool getLocationInfo(const Value *V, std::string &DisplayName,
741                        std::string &Type, unsigned &LineNo, std::string &File,
742                        std::string &Dir);
743
744   /// getDISubprogram - Find subprogram that is enclosing this scope.
745   DISubprogram getDISubprogram(const MDNode *Scope);
746
747   /// getDICompositeType - Find underlying composite type.
748   DICompositeType getDICompositeType(DIType T);
749
750   class DebugInfoFinder {
751   public:
752     /// processModule - Process entire module and collect debug info
753     /// anchors.
754     void processModule(Module &M);
755
756   private:
757     /// processType - Process DIType.
758     void processType(DIType DT);
759
760     /// processLexicalBlock - Process DILexicalBlock.
761     void processLexicalBlock(DILexicalBlock LB);
762
763     /// processSubprogram - Process DISubprogram.
764     void processSubprogram(DISubprogram SP);
765
766     /// processDeclare - Process DbgDeclareInst.
767     void processDeclare(DbgDeclareInst *DDI);
768
769     /// processLocation - Process DILocation.
770     void processLocation(DILocation Loc);
771
772     /// addCompileUnit - Add compile unit into CUs.
773     bool addCompileUnit(DICompileUnit CU);
774
775     /// addGlobalVariable - Add global variable into GVs.
776     bool addGlobalVariable(DIGlobalVariable DIG);
777
778     // addSubprogram - Add subprgoram into SPs.
779     bool addSubprogram(DISubprogram SP);
780
781     /// addType - Add type into Tys.
782     bool addType(DIType DT);
783
784   public:
785     typedef SmallVector<MDNode *, 8>::const_iterator iterator;
786     iterator compile_unit_begin()    const { return CUs.begin(); }
787     iterator compile_unit_end()      const { return CUs.end(); }
788     iterator subprogram_begin()      const { return SPs.begin(); }
789     iterator subprogram_end()        const { return SPs.end(); }
790     iterator global_variable_begin() const { return GVs.begin(); }
791     iterator global_variable_end()   const { return GVs.end(); }
792     iterator type_begin()            const { return TYs.begin(); }
793     iterator type_end()              const { return TYs.end(); }
794
795     unsigned compile_unit_count()    const { return CUs.size(); }
796     unsigned global_variable_count() const { return GVs.size(); }
797     unsigned subprogram_count()      const { return SPs.size(); }
798     unsigned type_count()            const { return TYs.size(); }
799
800   private:
801     SmallVector<MDNode *, 8> CUs;  // Compile Units
802     SmallVector<MDNode *, 8> SPs;  // Subprograms
803     SmallVector<MDNode *, 8> GVs;  // Global Variables;
804     SmallVector<MDNode *, 8> TYs;  // Types
805     SmallPtrSet<MDNode *, 64> NodesSeen;
806   };
807 } // end namespace llvm
808
809 #endif