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