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