1 //===-- llvm/CodeGen/MachineModuleInfo.h ------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Collect meta information for a module. This information should be in a
11 // neutral form that can be used by different debugging and exception handling
14 // The organization of information is primarily clustered around the source
15 // compile units. The main exception is source line correspondence where
16 // inlining may interleave code from various compile units.
18 // The following information can be retrieved from the MachineModuleInfo.
20 // -- Source directories - Directories are uniqued based on their canonical
21 // string and assigned a sequential numeric ID (base 1.)
22 // -- Source files - Files are also uniqued based on their name and directory
23 // ID. A file ID is sequential number (base 1.)
24 // -- Source line correspondence - A vector of file ID, line#, column# triples.
25 // A DEBUG_LOCATION instruction is generated by the DAG Legalizer
26 // corresponding to each entry in the source line list. This allows a debug
27 // emitter to generate labels referenced by debug information tables.
29 //===----------------------------------------------------------------------===//
31 #ifndef LLVM_CODEGEN_MACHINEMODULEINFO_H
32 #define LLVM_CODEGEN_MACHINEMODULEINFO_H
34 #include "llvm/Support/Dwarf.h"
35 #include "llvm/Support/DataTypes.h"
36 #include "llvm/ADT/SmallVector.h"
37 #include "llvm/ADT/DenseMap.h"
38 #include "llvm/ADT/UniqueVector.h"
39 #include "llvm/ADT/SmallPtrSet.h"
40 #include "llvm/ADT/StringMap.h"
41 #include "llvm/CodeGen/MachineLocation.h"
42 #include "llvm/GlobalValue.h"
43 #include "llvm/Pass.h"
47 //===----------------------------------------------------------------------===//
48 // Forward declarations.
52 class MachineBasicBlock;
53 class MachineFunction;
58 //===----------------------------------------------------------------------===//
59 // Debug info constants.
62 LLVMDebugVersion = (7 << 16), // Current version of debug information.
63 LLVMDebugVersion6 = (6 << 16), // Constant for version 6.
64 LLVMDebugVersion5 = (5 << 16), // Constant for version 5.
65 LLVMDebugVersion4 = (4 << 16), // Constant for version 4.
66 LLVMDebugVersionMask = 0xffff0000 // Mask for version number.
69 //===----------------------------------------------------------------------===//
70 /// DIVisitor - Subclasses of this class apply steps to each of the fields in
71 /// the supplied DebugInfoDesc.
75 virtual ~DIVisitor() {}
77 /// ApplyToFields - Target the visitor to each field of the debug information
79 void ApplyToFields(DebugInfoDesc *DD);
81 /// Apply - Subclasses override each of these methods to perform the
82 /// appropriate action for the type of field.
83 virtual void Apply(int &Field) = 0;
84 virtual void Apply(unsigned &Field) = 0;
85 virtual void Apply(int64_t &Field) = 0;
86 virtual void Apply(uint64_t &Field) = 0;
87 virtual void Apply(bool &Field) = 0;
88 virtual void Apply(std::string &Field) = 0;
89 virtual void Apply(DebugInfoDesc *&Field) = 0;
90 virtual void Apply(GlobalVariable *&Field) = 0;
91 virtual void Apply(std::vector<DebugInfoDesc *> &Field) = 0;
94 //===----------------------------------------------------------------------===//
95 /// DebugInfoDesc - This class is the base class for debug info descriptors.
99 unsigned Tag; // Content indicator. Dwarf values are
100 // used but that does not limit use to
104 explicit DebugInfoDesc(unsigned T) : Tag(T | LLVMDebugVersion) {}
107 virtual ~DebugInfoDesc() {}
110 unsigned getTag() const { return Tag & ~LLVMDebugVersionMask; }
111 unsigned getVersion() const { return Tag & LLVMDebugVersionMask; }
112 void setTag(unsigned T) { Tag = T | LLVMDebugVersion; }
114 /// TagFromGlobal - Returns the tag number from a debug info descriptor
115 /// GlobalVariable. Return DIIValid if operand is not an unsigned int.
116 static unsigned TagFromGlobal(GlobalVariable *GV);
118 /// VersionFromGlobal - Returns the version number from a debug info
119 /// descriptor GlobalVariable. Return DIIValid if operand is not an unsigned
121 static unsigned VersionFromGlobal(GlobalVariable *GV);
123 /// DescFactory - Create an instance of debug info descriptor based on Tag.
124 /// Return NULL if not a recognized Tag.
125 static DebugInfoDesc *DescFactory(unsigned Tag);
127 /// getLinkage - get linkage appropriate for this type of descriptor.
129 virtual GlobalValue::LinkageTypes getLinkage() const;
131 //===--------------------------------------------------------------------===//
132 // Subclasses should supply the following static methods.
134 // Implement isa/cast/dyncast.
135 static bool classof(const DebugInfoDesc *) { return true; }
137 //===--------------------------------------------------------------------===//
138 // Subclasses should supply the following virtual methods.
140 /// ApplyToFields - Target the vistor to the fields of the descriptor.
142 virtual void ApplyToFields(DIVisitor *Visitor);
144 /// getDescString - Return a string used to compose global names and labels.
146 virtual const char *getDescString() const = 0;
148 /// getTypeString - Return a string used to label this descriptor's type.
150 virtual const char *getTypeString() const = 0;
153 virtual void dump() = 0;
157 //===----------------------------------------------------------------------===//
158 /// AnchorDesc - Descriptors of this class act as markers for identifying
159 /// descriptors of certain groups.
161 class AnchorDesc : public DebugInfoDesc {
163 unsigned AnchorTag; // Tag number of descriptors anchored
168 explicit AnchorDesc(AnchoredDesc *D);
171 unsigned getAnchorTag() const { return AnchorTag; }
173 // Implement isa/cast/dyncast.
174 static bool classof(const AnchorDesc *) { return true; }
175 static bool classof(const DebugInfoDesc *D);
177 /// getLinkage - get linkage appropriate for this type of descriptor.
179 virtual GlobalValue::LinkageTypes getLinkage() const;
181 /// ApplyToFields - Target the visitor to the fields of the AnchorDesc.
183 virtual void ApplyToFields(DIVisitor *Visitor);
185 /// getDescString - Return a string used to compose global names and labels.
187 virtual const char *getDescString() const;
189 /// getTypeString - Return a string used to label this descriptor's type.
191 virtual const char *getTypeString() const;
198 //===----------------------------------------------------------------------===//
199 /// AnchoredDesc - This class manages anchors for a variety of top level
201 class AnchoredDesc : public DebugInfoDesc {
203 DebugInfoDesc *Anchor; // Anchor for all descriptors of the
208 explicit AnchoredDesc(unsigned T);
212 AnchorDesc *getAnchor() const { return static_cast<AnchorDesc *>(Anchor); }
213 void setAnchor(AnchorDesc *A) { Anchor = static_cast<DebugInfoDesc *>(A); }
215 //===--------------------------------------------------------------------===//
216 // Subclasses should supply the following virtual methods.
218 /// getAnchorString - Return a string used to label descriptor's anchor.
220 virtual const char *getAnchorString() const = 0;
222 /// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
224 virtual void ApplyToFields(DIVisitor *Visitor);
227 //===----------------------------------------------------------------------===//
228 /// CompileUnitDesc - This class packages debug information associated with a
229 /// source/header file.
230 class CompileUnitDesc : public AnchoredDesc {
232 unsigned Language; // Language number (ex. DW_LANG_C89.)
233 std::string FileName; // Source file name.
234 std::string Directory; // Source file directory.
235 std::string Producer; // Compiler string.
242 unsigned getLanguage() const { return Language; }
243 const std::string &getFileName() const { return FileName; }
244 const std::string &getDirectory() const { return Directory; }
245 const std::string &getProducer() const { return Producer; }
246 void setLanguage(unsigned L) { Language = L; }
247 void setFileName(const std::string &FN) { FileName = FN; }
248 void setDirectory(const std::string &D) { Directory = D; }
249 void setProducer(const std::string &P) { Producer = P; }
251 // FIXME - Need translation unit getter/setter.
253 // Implement isa/cast/dyncast.
254 static bool classof(const CompileUnitDesc *) { return true; }
255 static bool classof(const DebugInfoDesc *D);
257 /// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
259 virtual void ApplyToFields(DIVisitor *Visitor);
261 /// getDescString - Return a string used to compose global names and labels.
263 virtual const char *getDescString() const;
265 /// getTypeString - Return a string used to label this descriptor's type.
267 virtual const char *getTypeString() const;
269 /// getAnchorString - Return a string used to label this descriptor's anchor.
271 static const char *const AnchorString;
272 virtual const char *getAnchorString() const;
279 //===----------------------------------------------------------------------===//
280 /// TypeDesc - This class packages debug information associated with a type.
282 class TypeDesc : public DebugInfoDesc {
285 FlagPrivate = 1 << 0,
286 FlagProtected = 1 << 1,
289 DebugInfoDesc *Context; // Context debug descriptor.
290 std::string Name; // Type name (may be empty.)
291 DebugInfoDesc *File; // Defined compile unit (may be NULL.)
292 unsigned Line; // Defined line# (may be zero.)
293 uint64_t Size; // Type bit size (may be zero.)
294 uint64_t Align; // Type bit alignment (may be zero.)
295 uint64_t Offset; // Type bit offset (may be zero.)
298 unsigned Flags; // Miscellaneous flags.
301 explicit TypeDesc(unsigned T);
304 DebugInfoDesc *getContext() const { return Context; }
305 const std::string &getName() const { return Name; }
306 CompileUnitDesc *getFile() const {
307 return static_cast<CompileUnitDesc *>(File);
309 unsigned getLine() const { return Line; }
310 uint64_t getSize() const { return Size; }
311 uint64_t getAlign() const { return Align; }
312 uint64_t getOffset() const { return Offset; }
313 bool isPrivate() const {
314 return (Flags & FlagPrivate) != 0;
316 bool isProtected() const {
317 return (Flags & FlagProtected) != 0;
319 bool isForwardDecl() const {
320 return (Flags & FlagFwdDecl) != 0;
322 void setContext(DebugInfoDesc *C) { Context = C; }
323 void setName(const std::string &N) { Name = N; }
324 void setFile(CompileUnitDesc *U) {
325 File = static_cast<DebugInfoDesc *>(U);
327 void setLine(unsigned L) { Line = L; }
328 void setSize(uint64_t S) { Size = S; }
329 void setAlign(uint64_t A) { Align = A; }
330 void setOffset(uint64_t O) { Offset = O; }
331 void setIsPrivate() { Flags |= FlagPrivate; }
332 void setIsProtected() { Flags |= FlagProtected; }
333 void setIsForwardDecl() { Flags |= FlagFwdDecl; }
335 /// ApplyToFields - Target the visitor to the fields of the TypeDesc.
337 virtual void ApplyToFields(DIVisitor *Visitor);
339 /// getDescString - Return a string used to compose global names and labels.
341 virtual const char *getDescString() const;
343 /// getTypeString - Return a string used to label this descriptor's type.
345 virtual const char *getTypeString() const;
352 //===----------------------------------------------------------------------===//
353 /// BasicTypeDesc - This class packages debug information associated with a
354 /// basic type (eg. int, bool, double.)
355 class BasicTypeDesc : public TypeDesc {
357 unsigned Encoding; // Type encoding.
363 unsigned getEncoding() const { return Encoding; }
364 void setEncoding(unsigned E) { Encoding = E; }
366 // Implement isa/cast/dyncast.
367 static bool classof(const BasicTypeDesc *) { return true; }
368 static bool classof(const DebugInfoDesc *D);
370 /// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
372 virtual void ApplyToFields(DIVisitor *Visitor);
374 /// getDescString - Return a string used to compose global names and labels.
376 virtual const char *getDescString() const;
378 /// getTypeString - Return a string used to label this descriptor's type.
380 virtual const char *getTypeString() const;
388 //===----------------------------------------------------------------------===//
389 /// DerivedTypeDesc - This class packages debug information associated with a
390 /// derived types (eg., typedef, pointer, reference.)
391 class DerivedTypeDesc : public TypeDesc {
393 DebugInfoDesc *FromType; // Type derived from.
396 explicit DerivedTypeDesc(unsigned T);
399 TypeDesc *getFromType() const {
400 return static_cast<TypeDesc *>(FromType);
402 void setFromType(TypeDesc *F) {
403 FromType = static_cast<DebugInfoDesc *>(F);
406 // Implement isa/cast/dyncast.
407 static bool classof(const DerivedTypeDesc *) { return true; }
408 static bool classof(const DebugInfoDesc *D);
410 /// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
412 virtual void ApplyToFields(DIVisitor *Visitor);
414 /// getDescString - Return a string used to compose global names and labels.
416 virtual const char *getDescString() const;
418 /// getTypeString - Return a string used to label this descriptor's type.
420 virtual const char *getTypeString() const;
427 //===----------------------------------------------------------------------===//
428 /// CompositeTypeDesc - This class packages debug information associated with a
429 /// array/struct types (eg., arrays, struct, union, enums.)
430 class CompositeTypeDesc : public DerivedTypeDesc {
432 std::vector<DebugInfoDesc *> Elements;// Information used to compose type.
435 explicit CompositeTypeDesc(unsigned T);
438 std::vector<DebugInfoDesc *> &getElements() { return Elements; }
440 // Implement isa/cast/dyncast.
441 static bool classof(const CompositeTypeDesc *) { return true; }
442 static bool classof(const DebugInfoDesc *D);
444 /// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
446 virtual void ApplyToFields(DIVisitor *Visitor);
448 /// getDescString - Return a string used to compose global names and labels.
450 virtual const char *getDescString() const;
452 /// getTypeString - Return a string used to label this descriptor's type.
454 virtual const char *getTypeString() const;
461 //===----------------------------------------------------------------------===//
462 /// SubrangeDesc - This class packages debug information associated with integer
464 class SubrangeDesc : public DebugInfoDesc {
466 int64_t Lo; // Low value of range.
467 int64_t Hi; // High value of range.
473 int64_t getLo() const { return Lo; }
474 int64_t getHi() const { return Hi; }
475 void setLo(int64_t L) { Lo = L; }
476 void setHi(int64_t H) { Hi = H; }
478 // Implement isa/cast/dyncast.
479 static bool classof(const SubrangeDesc *) { return true; }
480 static bool classof(const DebugInfoDesc *D);
482 /// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
484 virtual void ApplyToFields(DIVisitor *Visitor);
486 /// getDescString - Return a string used to compose global names and labels.
488 virtual const char *getDescString() const;
490 /// getTypeString - Return a string used to label this descriptor's type.
492 virtual const char *getTypeString() const;
499 //===----------------------------------------------------------------------===//
500 /// EnumeratorDesc - This class packages debug information associated with
501 /// named integer constants.
502 class EnumeratorDesc : public DebugInfoDesc {
504 std::string Name; // Enumerator name.
505 int64_t Value; // Enumerator value.
511 const std::string &getName() const { return Name; }
512 int64_t getValue() const { return Value; }
513 void setName(const std::string &N) { Name = N; }
514 void setValue(int64_t V) { Value = V; }
516 // Implement isa/cast/dyncast.
517 static bool classof(const EnumeratorDesc *) { return true; }
518 static bool classof(const DebugInfoDesc *D);
520 /// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
522 virtual void ApplyToFields(DIVisitor *Visitor);
524 /// getDescString - Return a string used to compose global names and labels.
526 virtual const char *getDescString() const;
528 /// getTypeString - Return a string used to label this descriptor's type.
530 virtual const char *getTypeString() const;
537 //===----------------------------------------------------------------------===//
538 /// VariableDesc - This class packages debug information associated with a
539 /// subprogram variable.
541 class VariableDesc : public DebugInfoDesc {
543 DebugInfoDesc *Context; // Context debug descriptor.
544 std::string Name; // Type name (may be empty.)
545 DebugInfoDesc *File; // Defined compile unit (may be NULL.)
546 unsigned Line; // Defined line# (may be zero.)
547 DebugInfoDesc *TyDesc; // Type of variable.
550 explicit VariableDesc(unsigned T);
553 DebugInfoDesc *getContext() const { return Context; }
554 const std::string &getName() const { return Name; }
555 CompileUnitDesc *getFile() const {
556 return static_cast<CompileUnitDesc *>(File);
558 unsigned getLine() const { return Line; }
559 TypeDesc *getType() const {
560 return static_cast<TypeDesc *>(TyDesc);
562 void setContext(DebugInfoDesc *C) { Context = C; }
563 void setName(const std::string &N) { Name = N; }
564 void setFile(CompileUnitDesc *U) {
565 File = static_cast<DebugInfoDesc *>(U);
567 void setLine(unsigned L) { Line = L; }
568 void setType(TypeDesc *T) {
569 TyDesc = static_cast<DebugInfoDesc *>(T);
572 // Implement isa/cast/dyncast.
573 static bool classof(const VariableDesc *) { return true; }
574 static bool classof(const DebugInfoDesc *D);
576 /// ApplyToFields - Target the visitor to the fields of the VariableDesc.
578 virtual void ApplyToFields(DIVisitor *Visitor);
580 /// getDescString - Return a string used to compose global names and labels.
582 virtual const char *getDescString() const;
584 /// getTypeString - Return a string used to label this descriptor's type.
586 virtual const char *getTypeString() const;
593 //===----------------------------------------------------------------------===//
594 /// GlobalDesc - This class is the base descriptor for global functions and
596 class GlobalDesc : public AnchoredDesc {
598 DebugInfoDesc *Context; // Context debug descriptor.
599 std::string Name; // Global name.
600 std::string FullName; // Fully qualified name.
601 std::string LinkageName; // Name for binding to MIPS linkage.
602 DebugInfoDesc *File; // Defined compile unit (may be NULL.)
603 unsigned Line; // Defined line# (may be zero.)
604 DebugInfoDesc *TyDesc; // Type debug descriptor.
605 bool IsStatic; // Is the global a static.
606 bool IsDefinition; // Is the global defined in context.
609 explicit GlobalDesc(unsigned T);
613 DebugInfoDesc *getContext() const { return Context; }
614 const std::string &getName() const { return Name; }
615 const std::string &getFullName() const { return FullName; }
616 const std::string &getLinkageName() const { return LinkageName; }
617 CompileUnitDesc *getFile() const {
618 return static_cast<CompileUnitDesc *>(File);
620 unsigned getLine() const { return Line; }
621 TypeDesc *getType() const {
622 return static_cast<TypeDesc *>(TyDesc);
624 bool isStatic() const { return IsStatic; }
625 bool isDefinition() const { return IsDefinition; }
626 void setContext(DebugInfoDesc *C) { Context = C; }
627 void setName(const std::string &N) { Name = N; }
628 void setFullName(const std::string &N) { FullName = N; }
629 void setLinkageName(const std::string &N) { LinkageName = N; }
630 void setFile(CompileUnitDesc *U) {
631 File = static_cast<DebugInfoDesc *>(U);
633 void setLine(unsigned L) { Line = L; }
634 void setType(TypeDesc *T) {
635 TyDesc = static_cast<DebugInfoDesc *>(T);
637 void setIsStatic(bool IS) { IsStatic = IS; }
638 void setIsDefinition(bool ID) { IsDefinition = ID; }
640 /// ApplyToFields - Target the visitor to the fields of the GlobalDesc.
642 virtual void ApplyToFields(DIVisitor *Visitor);
645 //===----------------------------------------------------------------------===//
646 /// GlobalVariableDesc - This class packages debug information associated with a
648 class GlobalVariableDesc : public GlobalDesc {
650 GlobalVariable *Global; // llvm global.
653 GlobalVariableDesc();
656 GlobalVariable *getGlobalVariable() const { return Global; }
657 void setGlobalVariable(GlobalVariable *GV) { Global = GV; }
659 // Implement isa/cast/dyncast.
660 static bool classof(const GlobalVariableDesc *) { return true; }
661 static bool classof(const DebugInfoDesc *D);
663 /// ApplyToFields - Target the visitor to the fields of the
664 /// GlobalVariableDesc.
665 virtual void ApplyToFields(DIVisitor *Visitor);
667 /// getDescString - Return a string used to compose global names and labels.
669 virtual const char *getDescString() const;
671 /// getTypeString - Return a string used to label this descriptor's type.
673 virtual const char *getTypeString() const;
675 /// getAnchorString - Return a string used to label this descriptor's anchor.
677 static const char *const AnchorString;
678 virtual const char *getAnchorString() const;
685 //===----------------------------------------------------------------------===//
686 /// SubprogramDesc - This class packages debug information associated with a
687 /// subprogram/function.
688 class SubprogramDesc : public GlobalDesc {
696 // Implement isa/cast/dyncast.
697 static bool classof(const SubprogramDesc *) { return true; }
698 static bool classof(const DebugInfoDesc *D);
700 /// ApplyToFields - Target the visitor to the fields of the SubprogramDesc.
702 virtual void ApplyToFields(DIVisitor *Visitor);
704 /// getDescString - Return a string used to compose global names and labels.
706 virtual const char *getDescString() const;
708 /// getTypeString - Return a string used to label this descriptor's type.
710 virtual const char *getTypeString() const;
712 /// getAnchorString - Return a string used to label this descriptor's anchor.
714 static const char *const AnchorString;
715 virtual const char *getAnchorString() const;
722 //===----------------------------------------------------------------------===//
723 /// BlockDesc - This descriptor groups variables and blocks nested in a block.
725 class BlockDesc : public DebugInfoDesc {
727 DebugInfoDesc *Context; // Context debug descriptor.
733 DebugInfoDesc *getContext() const { return Context; }
734 void setContext(DebugInfoDesc *C) { Context = C; }
736 // Implement isa/cast/dyncast.
737 static bool classof(const BlockDesc *) { return true; }
738 static bool classof(const DebugInfoDesc *D);
740 /// ApplyToFields - Target the visitor to the fields of the BlockDesc.
742 virtual void ApplyToFields(DIVisitor *Visitor);
744 /// getDescString - Return a string used to compose global names and labels.
746 virtual const char *getDescString() const;
748 /// getTypeString - Return a string used to label this descriptor's type.
750 virtual const char *getTypeString() const;
757 //===----------------------------------------------------------------------===//
758 /// DIDeserializer - This class is responsible for casting GlobalVariables
759 /// into DebugInfoDesc objects.
760 class DIDeserializer {
761 // Previously defined gloabls.
762 std::map<GlobalVariable*, DebugInfoDesc*> GlobalDescs;
764 const std::map<GlobalVariable *, DebugInfoDesc *> &getGlobalDescs() const {
768 /// Deserialize - Reconstitute a GlobalVariable into it's component
769 /// DebugInfoDesc objects.
770 DebugInfoDesc *Deserialize(Value *V);
771 DebugInfoDesc *Deserialize(GlobalVariable *GV);
774 //===----------------------------------------------------------------------===//
775 /// DISerializer - This class is responsible for casting DebugInfoDesc objects
776 /// into GlobalVariables.
778 Module *M; // Definition space module.
779 PointerType *StrPtrTy; // A "i8*" type. Created lazily.
780 PointerType *EmptyStructPtrTy; // A "{ }*" type. Created lazily.
782 // Types per Tag. Created lazily.
783 std::map<unsigned, StructType *> TagTypes;
785 // Previously defined descriptors.
786 DenseMap<DebugInfoDesc *, GlobalVariable *> DescGlobals;
788 // Previously defined strings.
789 StringMap<Constant*> StringCache;
792 : M(NULL), StrPtrTy(NULL), EmptyStructPtrTy(NULL), TagTypes(),
793 DescGlobals(), StringCache()
797 Module *getModule() const { return M; };
798 void setModule(Module *module) { M = module; }
800 /// getStrPtrType - Return a "i8*" type.
802 const PointerType *getStrPtrType();
804 /// getEmptyStructPtrType - Return a "{ }*" type.
806 const PointerType *getEmptyStructPtrType();
808 /// getTagType - Return the type describing the specified descriptor (via
810 const StructType *getTagType(DebugInfoDesc *DD);
812 /// getString - Construct the string as constant string global.
814 Constant *getString(const std::string &String);
816 /// Serialize - Recursively cast the specified descriptor into a
817 /// GlobalVariable so that it can be serialized to a .bc or .ll file.
818 GlobalVariable *Serialize(DebugInfoDesc *DD);
820 /// addDescriptor - Directly connect DD with existing GV.
821 void addDescriptor(DebugInfoDesc *DD, GlobalVariable *GV);
824 //===----------------------------------------------------------------------===//
825 /// DIVerifier - This class is responsible for verifying the given network of
826 /// GlobalVariables are valid as DebugInfoDesc objects.
833 DenseMap<GlobalVariable *, unsigned> Validity; // Tracks prior results.
834 std::map<unsigned, unsigned> Counts; // Count of fields per Tag type.
837 : Validity(), Counts()
840 /// Verify - Return true if the GlobalVariable appears to be a valid
841 /// serialization of a DebugInfoDesc.
842 bool Verify(Value *V);
843 bool Verify(GlobalVariable *GV);
845 /// isVerified - Return true if the specified GV has already been
846 /// verified as a debug information descriptor.
847 bool isVerified(GlobalVariable *GV);
850 //===----------------------------------------------------------------------===//
851 /// SourceLineInfo - This class is used to record source line correspondence.
853 class SourceLineInfo {
854 unsigned Line; // Source line number.
855 unsigned Column; // Source column.
856 unsigned SourceID; // Source ID number.
857 unsigned LabelID; // Label in code ID number.
859 SourceLineInfo(unsigned L, unsigned C, unsigned S, unsigned I)
860 : Line(L), Column(C), SourceID(S), LabelID(I) {}
863 unsigned getLine() const { return Line; }
864 unsigned getColumn() const { return Column; }
865 unsigned getSourceID() const { return SourceID; }
866 unsigned getLabelID() const { return LabelID; }
869 //===----------------------------------------------------------------------===//
870 /// SourceFileInfo - This class is used to track source information.
872 class SourceFileInfo {
873 unsigned DirectoryID; // Directory ID number.
874 std::string Name; // File name (not including directory.)
876 SourceFileInfo(unsigned D, const std::string &N) : DirectoryID(D), Name(N) {}
879 unsigned getDirectoryID() const { return DirectoryID; }
880 const std::string &getName() const { return Name; }
882 /// operator== - Used by UniqueVector to locate entry.
884 bool operator==(const SourceFileInfo &SI) const {
885 return getDirectoryID() == SI.getDirectoryID() && getName() == SI.getName();
888 /// operator< - Used by UniqueVector to locate entry.
890 bool operator<(const SourceFileInfo &SI) const {
891 return getDirectoryID() < SI.getDirectoryID() ||
892 (getDirectoryID() == SI.getDirectoryID() && getName() < SI.getName());
896 //===----------------------------------------------------------------------===//
897 /// DebugVariable - This class is used to track local variable information.
899 class DebugVariable {
901 VariableDesc *Desc; // Variable Descriptor.
902 unsigned FrameIndex; // Variable frame index.
905 DebugVariable(VariableDesc *D, unsigned I)
911 VariableDesc *getDesc() const { return Desc; }
912 unsigned getFrameIndex() const { return FrameIndex; }
915 //===----------------------------------------------------------------------===//
916 /// DebugScope - This class is used to track scope information.
920 DebugScope *Parent; // Parent to this scope.
921 DebugInfoDesc *Desc; // Debug info descriptor for scope.
922 // Either subprogram or block.
923 unsigned StartLabelID; // Label ID of the beginning of scope.
924 unsigned EndLabelID; // Label ID of the end of scope.
925 std::vector<DebugScope *> Scopes; // Scopes defined in scope.
926 std::vector<DebugVariable *> Variables;// Variables declared in scope.
929 DebugScope(DebugScope *P, DebugInfoDesc *D)
940 DebugScope *getParent() const { return Parent; }
941 DebugInfoDesc *getDesc() const { return Desc; }
942 unsigned getStartLabelID() const { return StartLabelID; }
943 unsigned getEndLabelID() const { return EndLabelID; }
944 std::vector<DebugScope *> &getScopes() { return Scopes; }
945 std::vector<DebugVariable *> &getVariables() { return Variables; }
946 void setStartLabelID(unsigned S) { StartLabelID = S; }
947 void setEndLabelID(unsigned E) { EndLabelID = E; }
949 /// AddScope - Add a scope to the scope.
951 void AddScope(DebugScope *S) { Scopes.push_back(S); }
953 /// AddVariable - Add a variable to the scope.
955 void AddVariable(DebugVariable *V) { Variables.push_back(V); }
958 //===----------------------------------------------------------------------===//
959 /// LandingPadInfo - This structure is used to retain landing pad info for
960 /// the current function.
962 struct LandingPadInfo {
963 MachineBasicBlock *LandingPadBlock; // Landing pad block.
964 SmallVector<unsigned, 1> BeginLabels; // Labels prior to invoke.
965 SmallVector<unsigned, 1> EndLabels; // Labels after invoke.
966 unsigned LandingPadLabel; // Label at beginning of landing pad.
967 Function *Personality; // Personality function.
968 std::vector<int> TypeIds; // List of type ids (filters negative)
970 explicit LandingPadInfo(MachineBasicBlock *MBB)
971 : LandingPadBlock(MBB)
977 //===----------------------------------------------------------------------===//
978 /// MachineModuleInfo - This class contains meta information specific to a
979 /// module. Queries can be made by different debugging and exception handling
980 /// schemes and reformated for specific use.
982 class MachineModuleInfo : public ImmutablePass {
984 // Use the same deserializer/verifier for the module.
988 // CompileUnits - Uniquing vector for compile units.
989 UniqueVector<CompileUnitDesc *> CompileUnits;
991 // Directories - Uniquing vector for directories.
992 UniqueVector<std::string> Directories;
994 // SourceFiles - Uniquing vector for source files.
995 UniqueVector<SourceFileInfo> SourceFiles;
997 // Lines - List of of source line correspondence.
998 std::vector<SourceLineInfo> Lines;
1000 // LabelIDList - One entry per assigned label. Normally the entry is equal to
1001 // the list index(+1). If the entry is zero then the label has been deleted.
1002 // Any other value indicates the label has been deleted by is mapped to
1004 std::vector<unsigned> LabelIDList;
1006 // ScopeMap - Tracks the scopes in the current function.
1007 std::map<DebugInfoDesc *, DebugScope *> ScopeMap;
1009 // RootScope - Top level scope for the current function.
1011 DebugScope *RootScope;
1013 // FrameMoves - List of moves done by a function's prolog. Used to construct
1014 // frame maps by debug and exception handling consumers.
1015 std::vector<MachineMove> FrameMoves;
1017 // LandingPads - List of LandingPadInfo describing the landing pad information
1018 // in the current function.
1019 std::vector<LandingPadInfo> LandingPads;
1021 // TypeInfos - List of C++ TypeInfo used in the current function.
1023 std::vector<GlobalVariable *> TypeInfos;
1025 // FilterIds - List of typeids encoding filters used in the current function.
1027 std::vector<unsigned> FilterIds;
1029 // FilterEnds - List of the indices in FilterIds corresponding to filter
1032 std::vector<unsigned> FilterEnds;
1034 // Personalities - Vector of all personality functions ever seen. Used to emit
1035 // common EH frames.
1036 std::vector<Function *> Personalities;
1038 // UsedFunctions - the functions in the llvm.used list in a more easily
1039 // searchable format.
1040 SmallPtrSet<const Function *, 32> UsedFunctions;
1043 bool CallsUnwindInit;
1045 static char ID; // Pass identification, replacement for typeid
1047 MachineModuleInfo();
1048 ~MachineModuleInfo();
1050 /// doInitialization - Initialize the state for a new module.
1052 bool doInitialization();
1054 /// doFinalization - Tear down the state after completion of a module.
1056 bool doFinalization();
1058 /// BeginFunction - Begin gathering function meta information.
1060 void BeginFunction(MachineFunction *MF);
1062 /// EndFunction - Discard function meta information.
1066 /// getDescFor - Convert a Value to a debug information descriptor.
1068 // FIXME - use new Value type when available.
1069 DebugInfoDesc *getDescFor(Value *V);
1071 /// Verify - Verify that a Value is debug information descriptor.
1073 bool Verify(Value *V) { return VR.Verify(V); }
1075 /// isVerified - Return true if the specified GV has already been
1076 /// verified as a debug information descriptor.
1077 bool isVerified(GlobalVariable *GV) { return VR.isVerified(GV); }
1079 /// AnalyzeModule - Scan the module for global debug information.
1081 void AnalyzeModule(Module &M);
1083 /// hasDebugInfo - Returns true if valid debug info is present.
1085 bool hasDebugInfo() const { return !CompileUnits.empty(); }
1087 bool callsEHReturn() const { return CallsEHReturn; }
1088 void setCallsEHReturn(bool b) { CallsEHReturn = b; }
1090 bool callsUnwindInit() const { return CallsUnwindInit; }
1091 void setCallsUnwindInit(bool b) { CallsUnwindInit = b; }
1093 /// NextLabelID - Return the next unique label id.
1095 unsigned NextLabelID() {
1096 unsigned ID = (unsigned)LabelIDList.size() + 1;
1097 LabelIDList.push_back(ID);
1101 /// RecordSourceLine - Records location information and associates it with a
1102 /// label. Returns a unique label ID used to generate a label and
1103 /// provide correspondence to the source line list.
1104 unsigned RecordSourceLine(unsigned Line, unsigned Column, unsigned Source);
1106 /// InvalidateLabel - Inhibit use of the specified label # from
1107 /// MachineModuleInfo, for example because the code was deleted.
1108 void InvalidateLabel(unsigned LabelID) {
1109 // Remap to zero to indicate deletion.
1110 RemapLabel(LabelID, 0);
1113 /// RemapLabel - Indicate that a label has been merged into another.
1115 void RemapLabel(unsigned OldLabelID, unsigned NewLabelID) {
1116 assert(0 < OldLabelID && OldLabelID <= LabelIDList.size() &&
1117 "Old label ID out of range.");
1118 assert(NewLabelID <= LabelIDList.size() &&
1119 "New label ID out of range.");
1120 LabelIDList[OldLabelID - 1] = NewLabelID;
1123 /// MappedLabel - Find out the label's final ID. Zero indicates deletion.
1124 /// ID != Mapped ID indicates that the label was folded into another label.
1125 unsigned MappedLabel(unsigned LabelID) const {
1126 assert(LabelID <= LabelIDList.size() && "Debug label ID out of range.");
1127 return LabelID ? LabelIDList[LabelID - 1] : 0;
1130 /// RecordSource - Register a source file with debug info. Returns an source
1132 unsigned RecordSource(const std::string &Directory,
1133 const std::string &Source);
1134 unsigned RecordSource(const CompileUnitDesc *CompileUnit);
1136 /// getDirectories - Return the UniqueVector of std::string representing
1138 const UniqueVector<std::string> &getDirectories() const {
1142 /// getSourceFiles - Return the UniqueVector of source files.
1144 const UniqueVector<SourceFileInfo> &getSourceFiles() const {
1148 /// getSourceLines - Return a vector of source lines.
1150 const std::vector<SourceLineInfo> &getSourceLines() const {
1154 /// SetupCompileUnits - Set up the unique vector of compile units.
1156 void SetupCompileUnits(Module &M);
1158 /// getCompileUnits - Return a vector of debug compile units.
1160 const UniqueVector<CompileUnitDesc *> getCompileUnits() const;
1162 /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1163 /// named GlobalVariable.
1164 void getGlobalVariablesUsing(Module &M, const std::string &RootName,
1165 std::vector<GlobalVariable*> &Result);
1167 /// getAnchoredDescriptors - Return a vector of anchored debug descriptors.
1170 void getAnchoredDescriptors(Module &M, std::vector<T*> &AnchoredDescs) {
1172 std::vector<GlobalVariable *> Globals;
1173 getGlobalVariablesUsing(M, Desc.getAnchorString(), Globals);
1175 for (unsigned i = 0, N = Globals.size(); i < N; ++i) {
1176 GlobalVariable *GV = Globals[i];
1178 // FIXME - In the short term, changes are too drastic to continue.
1179 if (DebugInfoDesc::TagFromGlobal(GV) == Desc.getTag() &&
1180 DebugInfoDesc::VersionFromGlobal(GV) == LLVMDebugVersion) {
1181 AnchoredDescs.push_back(cast<T>(DR.Deserialize(GV)));
1186 /// RecordRegionStart - Indicate the start of a region.
1188 unsigned RecordRegionStart(Value *V);
1190 /// RecordRegionEnd - Indicate the end of a region.
1192 unsigned RecordRegionEnd(Value *V);
1194 /// RecordVariable - Indicate the declaration of a local variable.
1196 void RecordVariable(GlobalValue *GV, unsigned FrameIndex);
1198 /// getRootScope - Return current functions root scope.
1200 DebugScope *getRootScope() { return RootScope; }
1202 /// getOrCreateScope - Returns the scope associated with the given descriptor.
1204 DebugScope *getOrCreateScope(DebugInfoDesc *ScopeDesc);
1206 /// getFrameMoves - Returns a reference to a list of moves done in the current
1207 /// function's prologue. Used to construct frame maps for debug and exception
1208 /// handling comsumers.
1209 std::vector<MachineMove> &getFrameMoves() { return FrameMoves; }
1211 //===-EH-----------------------------------------------------------------===//
1213 /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
1214 /// specified MachineBasicBlock.
1215 LandingPadInfo &getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad);
1217 /// addInvoke - Provide the begin and end labels of an invoke style call and
1218 /// associate it with a try landing pad block.
1219 void addInvoke(MachineBasicBlock *LandingPad, unsigned BeginLabel,
1222 /// addLandingPad - Add a new panding pad. Returns the label ID for the
1223 /// landing pad entry.
1224 unsigned addLandingPad(MachineBasicBlock *LandingPad);
1226 /// addPersonality - Provide the personality function for the exception
1228 void addPersonality(MachineBasicBlock *LandingPad, Function *Personality);
1230 /// getPersonalityIndex - Get index of the current personality function inside
1231 /// Personalitites array
1232 unsigned getPersonalityIndex() const;
1234 /// getPersonalities - Return array of personality functions ever seen.
1235 const std::vector<Function *>& getPersonalities() const {
1236 return Personalities;
1239 // UsedFunctions - Return set of the functions in the llvm.used list.
1240 const SmallPtrSet<const Function *, 32>& getUsedFunctions() const {
1241 return UsedFunctions;
1244 /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
1246 void addCatchTypeInfo(MachineBasicBlock *LandingPad,
1247 std::vector<GlobalVariable *> &TyInfo);
1249 /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
1251 void addFilterTypeInfo(MachineBasicBlock *LandingPad,
1252 std::vector<GlobalVariable *> &TyInfo);
1254 /// addCleanup - Add a cleanup action for a landing pad.
1256 void addCleanup(MachineBasicBlock *LandingPad);
1258 /// getTypeIDFor - Return the type id for the specified typeinfo. This is
1260 unsigned getTypeIDFor(GlobalVariable *TI);
1262 /// getFilterIDFor - Return the id of the filter encoded by TyIds. This is
1264 int getFilterIDFor(std::vector<unsigned> &TyIds);
1266 /// TidyLandingPads - Remap landing pad labels and remove any deleted landing
1268 void TidyLandingPads();
1270 /// getLandingPads - Return a reference to the landing pad info for the
1271 /// current function.
1272 const std::vector<LandingPadInfo> &getLandingPads() const {
1276 /// getTypeInfos - Return a reference to the C++ typeinfo for the current
1278 const std::vector<GlobalVariable *> &getTypeInfos() const {
1282 /// getFilterIds - Return a reference to the typeids encoding filters used in
1283 /// the current function.
1284 const std::vector<unsigned> &getFilterIds() const {
1288 /// getPersonality - Return a personality function if available. The presence
1289 /// of one is required to emit exception handling info.
1290 Function *getPersonality() const;
1292 DIDeserializer *getDIDeserializer() { return &DR; }
1293 }; // End class MachineModuleInfo
1295 } // End llvm namespace