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