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