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