Revert r87059 for now. It is failing clang tests.
[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                                     const char * Filename,
498                                     const char * Directory,
499                                     const char * 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(const char * Name, uint64_t Val);
507
508     /// CreateBasicType - Create a basic type like int, float, etc.
509     DIBasicType CreateBasicType(DIDescriptor Context, const char * Name,
510                                 DICompileUnit CompileUnit, unsigned LineNumber,
511                                 uint64_t SizeInBits, uint64_t AlignInBits,
512                                 uint64_t OffsetInBits, unsigned Flags,
513                                 unsigned Encoding);
514
515     /// CreateBasicType - Create a basic type like int, float, etc.
516     DIBasicType CreateBasicTypeEx(DIDescriptor Context, const char * Name,
517                                 DICompileUnit CompileUnit, unsigned LineNumber,
518                                 Constant *SizeInBits, Constant *AlignInBits,
519                                 Constant *OffsetInBits, unsigned Flags,
520                                 unsigned Encoding);
521
522     /// CreateDerivedType - Create a derived type like const qualified type,
523     /// pointer, typedef, etc.
524     DIDerivedType CreateDerivedType(unsigned Tag, DIDescriptor Context,
525                                     const char * Name,
526                                     DICompileUnit CompileUnit,
527                                     unsigned LineNumber,
528                                     uint64_t SizeInBits, uint64_t AlignInBits,
529                                     uint64_t OffsetInBits, unsigned Flags,
530                                     DIType DerivedFrom);
531
532     /// CreateDerivedType - Create a derived type like const qualified type,
533     /// pointer, typedef, etc.
534     DIDerivedType CreateDerivedTypeEx(unsigned Tag, DIDescriptor Context,
535                                         const char * Name,
536                                     DICompileUnit CompileUnit,
537                                     unsigned LineNumber,
538                                     Constant *SizeInBits, Constant *AlignInBits,
539                                     Constant *OffsetInBits, unsigned Flags,
540                                     DIType DerivedFrom);
541
542     /// CreateCompositeType - Create a composite type like array, struct, etc.
543     DICompositeType CreateCompositeType(unsigned Tag, DIDescriptor Context,
544                                         const char * Name,
545                                         DICompileUnit CompileUnit,
546                                         unsigned LineNumber,
547                                         uint64_t SizeInBits,
548                                         uint64_t AlignInBits,
549                                         uint64_t OffsetInBits, unsigned Flags,
550                                         DIType DerivedFrom,
551                                         DIArray Elements,
552                                         unsigned RunTimeLang = 0);
553
554     /// CreateCompositeType - Create a composite type like array, struct, etc.
555     DICompositeType CreateCompositeTypeEx(unsigned Tag, DIDescriptor Context,
556                                         const char * Name,
557                                         DICompileUnit CompileUnit,
558                                         unsigned LineNumber,
559                                         Constant *SizeInBits,
560                                         Constant *AlignInBits,
561                                         Constant *OffsetInBits, unsigned Flags,
562                                         DIType DerivedFrom,
563                                         DIArray Elements,
564                                         unsigned RunTimeLang = 0);
565
566     /// CreateSubprogram - Create a new descriptor for the specified subprogram.
567     /// See comments in DISubprogram for descriptions of these fields.
568     DISubprogram CreateSubprogram(DIDescriptor Context, const char * Name,
569                                   const char * DisplayName,
570                                   const char * LinkageName,
571                                   DICompileUnit CompileUnit, unsigned LineNo,
572                                   DIType Type, bool isLocalToUnit,
573                                   bool isDefinition);
574
575     /// CreateGlobalVariable - Create a new descriptor for the specified global.
576     DIGlobalVariable
577     CreateGlobalVariable(DIDescriptor Context, const char * Name,
578                          const char * DisplayName,
579                          const char * LinkageName,
580                          DICompileUnit CompileUnit,
581                          unsigned LineNo, DIType Type, bool isLocalToUnit,
582                          bool isDefinition, llvm::GlobalVariable *GV);
583
584     /// CreateVariable - Create a new descriptor for the specified variable.
585     DIVariable CreateVariable(unsigned Tag, DIDescriptor Context,
586                               const char * Name,
587                               DICompileUnit CompileUnit, unsigned LineNo,
588                               DIType Type);
589
590     /// CreateComplexVariable - Create a new descriptor for the specified
591     /// variable which has a complex address expression for its address.
592     DIVariable CreateComplexVariable(unsigned Tag, DIDescriptor Context,
593                                      const std::string &Name,
594                                      DICompileUnit CompileUnit, unsigned LineNo,
595                                      DIType Type,
596                                      SmallVector<Value *, 9> &addr);
597
598     /// CreateLexicalBlock - This creates a descriptor for a lexical block
599     /// with the specified parent context.
600     DILexicalBlock CreateLexicalBlock(DIDescriptor Context);
601
602     /// CreateLocation - Creates a debug info location.
603     DILocation CreateLocation(unsigned LineNo, unsigned ColumnNo,
604                               DIScope S, DILocation OrigLoc);
605
606     /// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
607     /// inserting it at the end of the specified basic block.
608     void InsertStopPoint(DICompileUnit CU, unsigned LineNo, unsigned ColNo,
609                          BasicBlock *BB);
610
611     /// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
612     /// mark the start of the specified subprogram.
613     void InsertSubprogramStart(DISubprogram SP, BasicBlock *BB);
614
615     /// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
616     /// mark the start of a region for the specified scoping descriptor.
617     void InsertRegionStart(DIDescriptor D, BasicBlock *BB);
618
619     /// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
620     /// mark the end of a region for the specified scoping descriptor.
621     void InsertRegionEnd(DIDescriptor D, BasicBlock *BB);
622
623     /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
624     Instruction *InsertDeclare(llvm::Value *Storage, DIVariable D,
625                                BasicBlock *InsertAtEnd);
626
627     /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
628     Instruction *InsertDeclare(llvm::Value *Storage, DIVariable D,
629                                Instruction *InsertBefore);
630
631   private:
632     Constant *GetTagConstant(unsigned TAG);
633   };
634
635   /// Finds the stoppoint coressponding to this instruction, that is the
636   /// stoppoint that dominates this instruction
637   const DbgStopPointInst *findStopPoint(const Instruction *Inst);
638
639   /// Finds the stoppoint corresponding to first real (non-debug intrinsic)
640   /// instruction in this Basic Block, and returns the stoppoint for it.
641   const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB);
642
643   /// Finds the dbg.declare intrinsic corresponding to this value if any.
644   /// It looks through pointer casts too.
645   const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts = true);
646
647   /// Find the debug info descriptor corresponding to this global variable.
648   Value *findDbgGlobalDeclare(GlobalVariable *V);
649
650 bool getLocationInfo(const Value *V, std::string &DisplayName,
651                      std::string &Type, unsigned &LineNo, std::string &File,
652                      std::string &Dir);
653
654   /// isValidDebugInfoIntrinsic - Return true if SPI is a valid debug
655   /// info intrinsic.
656   bool isValidDebugInfoIntrinsic(DbgStopPointInst &SPI,
657                                  CodeGenOpt::Level OptLev);
658
659   /// isValidDebugInfoIntrinsic - Return true if FSI is a valid debug
660   /// info intrinsic.
661   bool isValidDebugInfoIntrinsic(DbgFuncStartInst &FSI,
662                                  CodeGenOpt::Level OptLev);
663
664   /// isValidDebugInfoIntrinsic - Return true if RSI is a valid debug
665   /// info intrinsic.
666   bool isValidDebugInfoIntrinsic(DbgRegionStartInst &RSI,
667                                  CodeGenOpt::Level OptLev);
668
669   /// isValidDebugInfoIntrinsic - Return true if REI is a valid debug
670   /// info intrinsic.
671   bool isValidDebugInfoIntrinsic(DbgRegionEndInst &REI,
672                                  CodeGenOpt::Level OptLev);
673
674   /// isValidDebugInfoIntrinsic - Return true if DI is a valid debug
675   /// info intrinsic.
676   bool isValidDebugInfoIntrinsic(DbgDeclareInst &DI,
677                                  CodeGenOpt::Level OptLev);
678
679   /// ExtractDebugLocation - Extract debug location information
680   /// from llvm.dbg.stoppoint intrinsic.
681   DebugLoc ExtractDebugLocation(DbgStopPointInst &SPI,
682                                 DebugLocTracker &DebugLocInfo);
683
684   /// ExtractDebugLocation - Extract debug location information
685   /// from DILocation.
686   DebugLoc ExtractDebugLocation(DILocation &Loc,
687                                 DebugLocTracker &DebugLocInfo);
688
689   /// ExtractDebugLocation - Extract debug location information
690   /// from llvm.dbg.func_start intrinsic.
691   DebugLoc ExtractDebugLocation(DbgFuncStartInst &FSI,
692                                 DebugLocTracker &DebugLocInfo);
693
694   class DebugInfoFinder {
695
696   public:
697     /// processModule - Process entire module and collect debug info
698     /// anchors.
699     void processModule(Module &M);
700
701   private:
702     /// processType - Process DIType.
703     void processType(DIType DT);
704
705     /// processLexicalBlock - Process DILexicalBlock.
706     void processLexicalBlock(DILexicalBlock LB);
707
708     /// processSubprogram - Process DISubprogram.
709     void processSubprogram(DISubprogram SP);
710
711     /// processDeclare - Process DbgDeclareInst.
712     void processDeclare(DbgDeclareInst *DDI);
713
714     /// processLocation - Process DILocation.
715     void processLocation(DILocation Loc);
716
717     /// addCompileUnit - Add compile unit into CUs.
718     bool addCompileUnit(DICompileUnit CU);
719
720     /// addGlobalVariable - Add global variable into GVs.
721     bool addGlobalVariable(DIGlobalVariable DIG);
722
723     // addSubprogram - Add subprgoram into SPs.
724     bool addSubprogram(DISubprogram SP);
725
726     /// addType - Add type into Tys.
727     bool addType(DIType DT);
728
729   public:
730     typedef SmallVector<MDNode *, 8>::iterator iterator;
731     iterator compile_unit_begin()    { return CUs.begin(); }
732     iterator compile_unit_end()      { return CUs.end(); }
733     iterator subprogram_begin()      { return SPs.begin(); }
734     iterator subprogram_end()        { return SPs.end(); }
735     iterator global_variable_begin() { return GVs.begin(); }
736     iterator global_variable_end()   { return GVs.end(); }
737     iterator type_begin()            { return TYs.begin(); }
738     iterator type_end()              { return TYs.end(); }
739
740     unsigned compile_unit_count()    { return CUs.size(); }
741     unsigned global_variable_count() { return GVs.size(); }
742     unsigned subprogram_count()      { return SPs.size(); }
743     unsigned type_count()            { return TYs.size(); }
744
745   private:
746     SmallVector<MDNode *, 8> CUs;  // Compile Units
747     SmallVector<MDNode *, 8> SPs;  // Subprograms
748     SmallVector<MDNode *, 8> GVs;  // Global Variables;
749     SmallVector<MDNode *, 8> TYs;  // Types
750     SmallPtrSet<MDNode *, 64> NodesSeen;
751   };
752 } // end namespace llvm
753
754 #endif