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 = (6 << 16), // Current version of debug information.
63 LLVMDebugVersion5 = (5 << 16), // Constant for version 5.
64 LLVMDebugVersion4 = (4 << 16), // Constant for version 4.
65 LLVMDebugVersionMask = 0xffff0000 // Mask for version number.
68 //===----------------------------------------------------------------------===//
69 /// DIVisitor - Subclasses of this class apply steps to each of the fields in
70 /// the supplied DebugInfoDesc.
74 virtual ~DIVisitor() {}
76 /// ApplyToFields - Target the visitor to each field of the debug information
78 void ApplyToFields(DebugInfoDesc *DD);
80 /// Apply - Subclasses override each of these methods to perform the
81 /// appropriate action for the type of field.
82 virtual void Apply(int &Field) = 0;
83 virtual void Apply(unsigned &Field) = 0;
84 virtual void Apply(int64_t &Field) = 0;
85 virtual void Apply(uint64_t &Field) = 0;
86 virtual void Apply(bool &Field) = 0;
87 virtual void Apply(std::string &Field) = 0;
88 virtual void Apply(DebugInfoDesc *&Field) = 0;
89 virtual void Apply(GlobalVariable *&Field) = 0;
90 virtual void Apply(std::vector<DebugInfoDesc *> &Field) = 0;
93 //===----------------------------------------------------------------------===//
94 /// DebugInfoDesc - This class is the base class for debug info descriptors.
98 unsigned Tag; // Content indicator. Dwarf values are
99 // used but that does not limit use to
103 explicit DebugInfoDesc(unsigned T) : Tag(T | LLVMDebugVersion) {}
106 virtual ~DebugInfoDesc() {}
109 unsigned getTag() const { return Tag & ~LLVMDebugVersionMask; }
110 unsigned getVersion() const { return Tag & LLVMDebugVersionMask; }
111 void setTag(unsigned T) { Tag = T | LLVMDebugVersion; }
113 /// TagFromGlobal - Returns the tag number from a debug info descriptor
114 /// GlobalVariable. Return DIIValid if operand is not an unsigned int.
115 static unsigned TagFromGlobal(GlobalVariable *GV);
117 /// VersionFromGlobal - Returns the version number from a debug info
118 /// descriptor GlobalVariable. Return DIIValid if operand is not an unsigned
120 static unsigned VersionFromGlobal(GlobalVariable *GV);
122 /// DescFactory - Create an instance of debug info descriptor based on Tag.
123 /// Return NULL if not a recognized Tag.
124 static DebugInfoDesc *DescFactory(unsigned Tag);
126 /// getLinkage - get linkage appropriate for this type of descriptor.
128 virtual GlobalValue::LinkageTypes getLinkage() const;
130 //===--------------------------------------------------------------------===//
131 // Subclasses should supply the following static methods.
133 // Implement isa/cast/dyncast.
134 static bool classof(const DebugInfoDesc *) { return true; }
136 //===--------------------------------------------------------------------===//
137 // Subclasses should supply the following virtual methods.
139 /// ApplyToFields - Target the vistor to the fields of the descriptor.
141 virtual void ApplyToFields(DIVisitor *Visitor);
143 /// getDescString - Return a string used to compose global names and labels.
145 virtual const char *getDescString() const = 0;
147 /// getTypeString - Return a string used to label this descriptor's type.
149 virtual const char *getTypeString() const = 0;
152 virtual void dump() = 0;
156 //===----------------------------------------------------------------------===//
157 /// AnchorDesc - Descriptors of this class act as markers for identifying
158 /// descriptors of certain groups.
160 class AnchorDesc : public DebugInfoDesc {
162 unsigned AnchorTag; // Tag number of descriptors anchored
167 explicit AnchorDesc(AnchoredDesc *D);
170 unsigned getAnchorTag() const { return AnchorTag; }
172 // Implement isa/cast/dyncast.
173 static bool classof(const AnchorDesc *) { return true; }
174 static bool classof(const DebugInfoDesc *D);
176 /// getLinkage - get linkage appropriate for this type of descriptor.
178 virtual GlobalValue::LinkageTypes getLinkage() const;
180 /// ApplyToFields - Target the visitor to the fields of the AnchorDesc.
182 virtual void ApplyToFields(DIVisitor *Visitor);
184 /// getDescString - Return a string used to compose global names and labels.
186 virtual const char *getDescString() const;
188 /// getTypeString - Return a string used to label this descriptor's type.
190 virtual const char *getTypeString() const;
197 //===----------------------------------------------------------------------===//
198 /// AnchoredDesc - This class manages anchors for a variety of top level
200 class AnchoredDesc : public DebugInfoDesc {
202 DebugInfoDesc *Anchor; // Anchor for all descriptors of the
207 explicit AnchoredDesc(unsigned T);
211 AnchorDesc *getAnchor() const { return static_cast<AnchorDesc *>(Anchor); }
212 void setAnchor(AnchorDesc *A) { Anchor = static_cast<DebugInfoDesc *>(A); }
214 //===--------------------------------------------------------------------===//
215 // Subclasses should supply the following virtual methods.
217 /// getAnchorString - Return a string used to label descriptor's anchor.
219 virtual const char *getAnchorString() const = 0;
221 /// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
223 virtual void ApplyToFields(DIVisitor *Visitor);
226 //===----------------------------------------------------------------------===//
227 /// CompileUnitDesc - This class packages debug information associated with a
228 /// source/header file.
229 class CompileUnitDesc : public AnchoredDesc {
231 unsigned Language; // Language number (ex. DW_LANG_C89.)
232 std::string FileName; // Source file name.
233 std::string Directory; // Source file directory.
234 std::string Producer; // Compiler string.
241 unsigned getLanguage() const { return Language; }
242 const std::string &getFileName() const { return FileName; }
243 const std::string &getDirectory() const { return Directory; }
244 const std::string &getProducer() const { return Producer; }
245 void setLanguage(unsigned L) { Language = L; }
246 void setFileName(const std::string &FN) { FileName = FN; }
247 void setDirectory(const std::string &D) { Directory = D; }
248 void setProducer(const std::string &P) { Producer = P; }
250 // FIXME - Need translation unit getter/setter.
252 // Implement isa/cast/dyncast.
253 static bool classof(const CompileUnitDesc *) { return true; }
254 static bool classof(const DebugInfoDesc *D);
256 /// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
258 virtual void ApplyToFields(DIVisitor *Visitor);
260 /// getDescString - Return a string used to compose global names and labels.
262 virtual const char *getDescString() const;
264 /// getTypeString - Return a string used to label this descriptor's type.
266 virtual const char *getTypeString() const;
268 /// getAnchorString - Return a string used to label this descriptor's anchor.
270 static const char *const AnchorString;
271 virtual const char *getAnchorString() const;
278 //===----------------------------------------------------------------------===//
279 /// TypeDesc - This class packages debug information associated with a type.
281 class TypeDesc : public DebugInfoDesc {
284 FlagPrivate = 1 << 0,
285 FlagProtected = 1 << 1
287 DebugInfoDesc *Context; // Context debug descriptor.
288 std::string Name; // Type name (may be empty.)
289 DebugInfoDesc *File; // Defined compile unit (may be NULL.)
290 unsigned Line; // Defined line# (may be zero.)
291 uint64_t Size; // Type bit size (may be zero.)
292 uint64_t Align; // Type bit alignment (may be zero.)
293 uint64_t Offset; // Type bit offset (may be zero.)
294 unsigned Flags; // Miscellaneous flags.
297 explicit TypeDesc(unsigned T);
300 DebugInfoDesc *getContext() const { return Context; }
301 const std::string &getName() const { return Name; }
302 CompileUnitDesc *getFile() const {
303 return static_cast<CompileUnitDesc *>(File);
305 unsigned getLine() const { return Line; }
306 uint64_t getSize() const { return Size; }
307 uint64_t getAlign() const { return Align; }
308 uint64_t getOffset() const { return Offset; }
309 bool isPrivate() const {
310 return (Flags & FlagPrivate) != 0;
312 bool isProtected() const {
313 return (Flags & FlagProtected) != 0;
315 void setContext(DebugInfoDesc *C) { Context = C; }
316 void setName(const std::string &N) { Name = N; }
317 void setFile(CompileUnitDesc *U) {
318 File = static_cast<DebugInfoDesc *>(U);
320 void setLine(unsigned L) { Line = L; }
321 void setSize(uint64_t S) { Size = S; }
322 void setAlign(uint64_t A) { Align = A; }
323 void setOffset(uint64_t O) { Offset = O; }
324 void setIsPrivate() { Flags |= FlagPrivate; }
325 void setIsProtected() { Flags |= FlagProtected; }
327 /// ApplyToFields - Target the visitor to the fields of the TypeDesc.
329 virtual void ApplyToFields(DIVisitor *Visitor);
331 /// getDescString - Return a string used to compose global names and labels.
333 virtual const char *getDescString() const;
335 /// getTypeString - Return a string used to label this descriptor's type.
337 virtual const char *getTypeString() const;
344 //===----------------------------------------------------------------------===//
345 /// BasicTypeDesc - This class packages debug information associated with a
346 /// basic type (eg. int, bool, double.)
347 class BasicTypeDesc : public TypeDesc {
349 unsigned Encoding; // Type encoding.
355 unsigned getEncoding() const { return Encoding; }
356 void setEncoding(unsigned E) { Encoding = E; }
358 // Implement isa/cast/dyncast.
359 static bool classof(const BasicTypeDesc *) { return true; }
360 static bool classof(const DebugInfoDesc *D);
362 /// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
364 virtual void ApplyToFields(DIVisitor *Visitor);
366 /// getDescString - Return a string used to compose global names and labels.
368 virtual const char *getDescString() const;
370 /// getTypeString - Return a string used to label this descriptor's type.
372 virtual const char *getTypeString() const;
380 //===----------------------------------------------------------------------===//
381 /// DerivedTypeDesc - This class packages debug information associated with a
382 /// derived types (eg., typedef, pointer, reference.)
383 class DerivedTypeDesc : public TypeDesc {
385 DebugInfoDesc *FromType; // Type derived from.
388 explicit DerivedTypeDesc(unsigned T);
391 TypeDesc *getFromType() const {
392 return static_cast<TypeDesc *>(FromType);
394 void setFromType(TypeDesc *F) {
395 FromType = static_cast<DebugInfoDesc *>(F);
398 // Implement isa/cast/dyncast.
399 static bool classof(const DerivedTypeDesc *) { return true; }
400 static bool classof(const DebugInfoDesc *D);
402 /// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
404 virtual void ApplyToFields(DIVisitor *Visitor);
406 /// getDescString - Return a string used to compose global names and labels.
408 virtual const char *getDescString() const;
410 /// getTypeString - Return a string used to label this descriptor's type.
412 virtual const char *getTypeString() const;
419 //===----------------------------------------------------------------------===//
420 /// CompositeTypeDesc - This class packages debug information associated with a
421 /// array/struct types (eg., arrays, struct, union, enums.)
422 class CompositeTypeDesc : public DerivedTypeDesc {
424 std::vector<DebugInfoDesc *> Elements;// Information used to compose type.
427 explicit CompositeTypeDesc(unsigned T);
430 std::vector<DebugInfoDesc *> &getElements() { return Elements; }
432 // Implement isa/cast/dyncast.
433 static bool classof(const CompositeTypeDesc *) { return true; }
434 static bool classof(const DebugInfoDesc *D);
436 /// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
438 virtual void ApplyToFields(DIVisitor *Visitor);
440 /// getDescString - Return a string used to compose global names and labels.
442 virtual const char *getDescString() const;
444 /// getTypeString - Return a string used to label this descriptor's type.
446 virtual const char *getTypeString() const;
453 //===----------------------------------------------------------------------===//
454 /// SubrangeDesc - This class packages debug information associated with integer
456 class SubrangeDesc : public DebugInfoDesc {
458 int64_t Lo; // Low value of range.
459 int64_t Hi; // High value of range.
465 int64_t getLo() const { return Lo; }
466 int64_t getHi() const { return Hi; }
467 void setLo(int64_t L) { Lo = L; }
468 void setHi(int64_t H) { Hi = H; }
470 // Implement isa/cast/dyncast.
471 static bool classof(const SubrangeDesc *) { return true; }
472 static bool classof(const DebugInfoDesc *D);
474 /// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
476 virtual void ApplyToFields(DIVisitor *Visitor);
478 /// getDescString - Return a string used to compose global names and labels.
480 virtual const char *getDescString() const;
482 /// getTypeString - Return a string used to label this descriptor's type.
484 virtual const char *getTypeString() const;
491 //===----------------------------------------------------------------------===//
492 /// EnumeratorDesc - This class packages debug information associated with
493 /// named integer constants.
494 class EnumeratorDesc : public DebugInfoDesc {
496 std::string Name; // Enumerator name.
497 int64_t Value; // Enumerator value.
503 const std::string &getName() const { return Name; }
504 int64_t getValue() const { return Value; }
505 void setName(const std::string &N) { Name = N; }
506 void setValue(int64_t V) { Value = V; }
508 // Implement isa/cast/dyncast.
509 static bool classof(const EnumeratorDesc *) { return true; }
510 static bool classof(const DebugInfoDesc *D);
512 /// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
514 virtual void ApplyToFields(DIVisitor *Visitor);
516 /// getDescString - Return a string used to compose global names and labels.
518 virtual const char *getDescString() const;
520 /// getTypeString - Return a string used to label this descriptor's type.
522 virtual const char *getTypeString() const;
529 //===----------------------------------------------------------------------===//
530 /// VariableDesc - This class packages debug information associated with a
531 /// subprogram variable.
533 class VariableDesc : public DebugInfoDesc {
535 DebugInfoDesc *Context; // Context debug descriptor.
536 std::string Name; // Type name (may be empty.)
537 DebugInfoDesc *File; // Defined compile unit (may be NULL.)
538 unsigned Line; // Defined line# (may be zero.)
539 DebugInfoDesc *TyDesc; // Type of variable.
542 explicit VariableDesc(unsigned T);
545 DebugInfoDesc *getContext() const { return Context; }
546 const std::string &getName() const { return Name; }
547 CompileUnitDesc *getFile() const {
548 return static_cast<CompileUnitDesc *>(File);
550 unsigned getLine() const { return Line; }
551 TypeDesc *getType() const {
552 return static_cast<TypeDesc *>(TyDesc);
554 void setContext(DebugInfoDesc *C) { Context = C; }
555 void setName(const std::string &N) { Name = N; }
556 void setFile(CompileUnitDesc *U) {
557 File = static_cast<DebugInfoDesc *>(U);
559 void setLine(unsigned L) { Line = L; }
560 void setType(TypeDesc *T) {
561 TyDesc = static_cast<DebugInfoDesc *>(T);
564 // Implement isa/cast/dyncast.
565 static bool classof(const VariableDesc *) { return true; }
566 static bool classof(const DebugInfoDesc *D);
568 /// ApplyToFields - Target the visitor to the fields of the VariableDesc.
570 virtual void ApplyToFields(DIVisitor *Visitor);
572 /// getDescString - Return a string used to compose global names and labels.
574 virtual const char *getDescString() const;
576 /// getTypeString - Return a string used to label this descriptor's type.
578 virtual const char *getTypeString() const;
585 //===----------------------------------------------------------------------===//
586 /// GlobalDesc - This class is the base descriptor for global functions and
588 class GlobalDesc : public AnchoredDesc {
590 DebugInfoDesc *Context; // Context debug descriptor.
591 std::string Name; // Global name.
592 std::string FullName; // Fully qualified name.
593 std::string LinkageName; // Name for binding to MIPS linkage.
594 DebugInfoDesc *File; // Defined compile unit (may be NULL.)
595 unsigned Line; // Defined line# (may be zero.)
596 DebugInfoDesc *TyDesc; // Type debug descriptor.
597 bool IsStatic; // Is the global a static.
598 bool IsDefinition; // Is the global defined in context.
601 explicit GlobalDesc(unsigned T);
605 DebugInfoDesc *getContext() const { return Context; }
606 const std::string &getName() const { return Name; }
607 const std::string &getFullName() const { return FullName; }
608 const std::string &getLinkageName() const { return LinkageName; }
609 CompileUnitDesc *getFile() const {
610 return static_cast<CompileUnitDesc *>(File);
612 unsigned getLine() const { return Line; }
613 TypeDesc *getType() const {
614 return static_cast<TypeDesc *>(TyDesc);
616 bool isStatic() const { return IsStatic; }
617 bool isDefinition() const { return IsDefinition; }
618 void setContext(DebugInfoDesc *C) { Context = C; }
619 void setName(const std::string &N) { Name = N; }
620 void setFullName(const std::string &N) { FullName = N; }
621 void setLinkageName(const std::string &N) { LinkageName = N; }
622 void setFile(CompileUnitDesc *U) {
623 File = static_cast<DebugInfoDesc *>(U);
625 void setLine(unsigned L) { Line = L; }
626 void setType(TypeDesc *T) {
627 TyDesc = static_cast<DebugInfoDesc *>(T);
629 void setIsStatic(bool IS) { IsStatic = IS; }
630 void setIsDefinition(bool ID) { IsDefinition = ID; }
632 /// ApplyToFields - Target the visitor to the fields of the GlobalDesc.
634 virtual void ApplyToFields(DIVisitor *Visitor);
637 //===----------------------------------------------------------------------===//
638 /// GlobalVariableDesc - This class packages debug information associated with a
640 class GlobalVariableDesc : public GlobalDesc {
642 GlobalVariable *Global; // llvm global.
645 GlobalVariableDesc();
648 GlobalVariable *getGlobalVariable() const { return Global; }
649 void setGlobalVariable(GlobalVariable *GV) { Global = GV; }
651 // Implement isa/cast/dyncast.
652 static bool classof(const GlobalVariableDesc *) { return true; }
653 static bool classof(const DebugInfoDesc *D);
655 /// ApplyToFields - Target the visitor to the fields of the
656 /// GlobalVariableDesc.
657 virtual void ApplyToFields(DIVisitor *Visitor);
659 /// getDescString - Return a string used to compose global names and labels.
661 virtual const char *getDescString() const;
663 /// getTypeString - Return a string used to label this descriptor's type.
665 virtual const char *getTypeString() const;
667 /// getAnchorString - Return a string used to label this descriptor's anchor.
669 static const char *const AnchorString;
670 virtual const char *getAnchorString() const;
677 //===----------------------------------------------------------------------===//
678 /// SubprogramDesc - This class packages debug information associated with a
679 /// subprogram/function.
680 class SubprogramDesc : public GlobalDesc {
688 // Implement isa/cast/dyncast.
689 static bool classof(const SubprogramDesc *) { return true; }
690 static bool classof(const DebugInfoDesc *D);
692 /// ApplyToFields - Target the visitor to the fields of the SubprogramDesc.
694 virtual void ApplyToFields(DIVisitor *Visitor);
696 /// getDescString - Return a string used to compose global names and labels.
698 virtual const char *getDescString() const;
700 /// getTypeString - Return a string used to label this descriptor's type.
702 virtual const char *getTypeString() const;
704 /// getAnchorString - Return a string used to label this descriptor's anchor.
706 static const char *const AnchorString;
707 virtual const char *getAnchorString() const;
714 //===----------------------------------------------------------------------===//
715 /// BlockDesc - This descriptor groups variables and blocks nested in a block.
717 class BlockDesc : public DebugInfoDesc {
719 DebugInfoDesc *Context; // Context debug descriptor.
725 DebugInfoDesc *getContext() const { return Context; }
726 void setContext(DebugInfoDesc *C) { Context = C; }
728 // Implement isa/cast/dyncast.
729 static bool classof(const BlockDesc *) { return true; }
730 static bool classof(const DebugInfoDesc *D);
732 /// ApplyToFields - Target the visitor to the fields of the BlockDesc.
734 virtual void ApplyToFields(DIVisitor *Visitor);
736 /// getDescString - Return a string used to compose global names and labels.
738 virtual const char *getDescString() const;
740 /// getTypeString - Return a string used to label this descriptor's type.
742 virtual const char *getTypeString() const;
749 //===----------------------------------------------------------------------===//
750 /// DIDeserializer - This class is responsible for casting GlobalVariables
751 /// into DebugInfoDesc objects.
752 class DIDeserializer {
753 // Previously defined gloabls.
754 std::map<GlobalVariable*, DebugInfoDesc*> GlobalDescs;
756 const std::map<GlobalVariable *, DebugInfoDesc *> &getGlobalDescs() const {
760 /// Deserialize - Reconstitute a GlobalVariable into it's component
761 /// DebugInfoDesc objects.
762 DebugInfoDesc *Deserialize(Value *V);
763 DebugInfoDesc *Deserialize(GlobalVariable *GV);
766 //===----------------------------------------------------------------------===//
767 /// DISerializer - This class is responsible for casting DebugInfoDesc objects
768 /// into GlobalVariables.
770 Module *M; // Definition space module.
771 PointerType *StrPtrTy; // A "i8*" type. Created lazily.
772 PointerType *EmptyStructPtrTy; // A "{ }*" type. Created lazily.
774 // Types per Tag. Created lazily.
775 std::map<unsigned, StructType *> TagTypes;
777 // Previously defined descriptors.
778 DenseMap<DebugInfoDesc *, GlobalVariable *> DescGlobals;
780 // Previously defined strings.
781 StringMap<Constant*> StringCache;
784 : M(NULL), StrPtrTy(NULL), EmptyStructPtrTy(NULL), TagTypes(),
785 DescGlobals(), StringCache()
789 Module *getModule() const { return M; };
790 void setModule(Module *module) { M = module; }
792 /// getStrPtrType - Return a "i8*" type.
794 const PointerType *getStrPtrType();
796 /// getEmptyStructPtrType - Return a "{ }*" type.
798 const PointerType *getEmptyStructPtrType();
800 /// getTagType - Return the type describing the specified descriptor (via
802 const StructType *getTagType(DebugInfoDesc *DD);
804 /// getString - Construct the string as constant string global.
806 Constant *getString(const std::string &String);
808 /// Serialize - Recursively cast the specified descriptor into a
809 /// GlobalVariable so that it can be serialized to a .bc or .ll file.
810 GlobalVariable *Serialize(DebugInfoDesc *DD);
812 /// addDescriptor - Directly connect DD with existing GV.
813 void addDescriptor(DebugInfoDesc *DD, GlobalVariable *GV);
816 //===----------------------------------------------------------------------===//
817 /// DIVerifier - This class is responsible for verifying the given network of
818 /// GlobalVariables are valid as DebugInfoDesc objects.
825 DenseMap<GlobalVariable *, unsigned> Validity; // Tracks prior results.
826 std::map<unsigned, unsigned> Counts; // Count of fields per Tag type.
829 : Validity(), Counts()
832 /// Verify - Return true if the GlobalVariable appears to be a valid
833 /// serialization of a DebugInfoDesc.
834 bool Verify(Value *V);
835 bool Verify(GlobalVariable *GV);
837 /// isVerified - Return true if the specified GV has already been
838 /// verified as a debug information descriptor.
839 bool isVerified(GlobalVariable *GV);
842 //===----------------------------------------------------------------------===//
843 /// SourceLineInfo - This class is used to record source line correspondence.
845 class SourceLineInfo {
846 unsigned Line; // Source line number.
847 unsigned Column; // Source column.
848 unsigned SourceID; // Source ID number.
849 unsigned LabelID; // Label in code ID number.
851 SourceLineInfo(unsigned L, unsigned C, unsigned S, unsigned I)
852 : Line(L), Column(C), SourceID(S), LabelID(I) {}
855 unsigned getLine() const { return Line; }
856 unsigned getColumn() const { return Column; }
857 unsigned getSourceID() const { return SourceID; }
858 unsigned getLabelID() const { return LabelID; }
861 //===----------------------------------------------------------------------===//
862 /// SourceFileInfo - This class is used to track source information.
864 class SourceFileInfo {
865 unsigned DirectoryID; // Directory ID number.
866 std::string Name; // File name (not including directory.)
868 SourceFileInfo(unsigned D, const std::string &N) : DirectoryID(D), Name(N) {}
871 unsigned getDirectoryID() const { return DirectoryID; }
872 const std::string &getName() const { return Name; }
874 /// operator== - Used by UniqueVector to locate entry.
876 bool operator==(const SourceFileInfo &SI) const {
877 return getDirectoryID() == SI.getDirectoryID() && getName() == SI.getName();
880 /// operator< - Used by UniqueVector to locate entry.
882 bool operator<(const SourceFileInfo &SI) const {
883 return getDirectoryID() < SI.getDirectoryID() ||
884 (getDirectoryID() == SI.getDirectoryID() && getName() < SI.getName());
888 //===----------------------------------------------------------------------===//
889 /// DebugVariable - This class is used to track local variable information.
891 class DebugVariable {
893 VariableDesc *Desc; // Variable Descriptor.
894 unsigned FrameIndex; // Variable frame index.
897 DebugVariable(VariableDesc *D, unsigned I)
903 VariableDesc *getDesc() const { return Desc; }
904 unsigned getFrameIndex() const { return FrameIndex; }
907 //===----------------------------------------------------------------------===//
908 /// DebugScope - This class is used to track scope information.
912 DebugScope *Parent; // Parent to this scope.
913 DebugInfoDesc *Desc; // Debug info descriptor for scope.
914 // Either subprogram or block.
915 unsigned StartLabelID; // Label ID of the beginning of scope.
916 unsigned EndLabelID; // Label ID of the end of scope.
917 std::vector<DebugScope *> Scopes; // Scopes defined in scope.
918 std::vector<DebugVariable *> Variables;// Variables declared in scope.
921 DebugScope(DebugScope *P, DebugInfoDesc *D)
932 DebugScope *getParent() const { return Parent; }
933 DebugInfoDesc *getDesc() const { return Desc; }
934 unsigned getStartLabelID() const { return StartLabelID; }
935 unsigned getEndLabelID() const { return EndLabelID; }
936 std::vector<DebugScope *> &getScopes() { return Scopes; }
937 std::vector<DebugVariable *> &getVariables() { return Variables; }
938 void setStartLabelID(unsigned S) { StartLabelID = S; }
939 void setEndLabelID(unsigned E) { EndLabelID = E; }
941 /// AddScope - Add a scope to the scope.
943 void AddScope(DebugScope *S) { Scopes.push_back(S); }
945 /// AddVariable - Add a variable to the scope.
947 void AddVariable(DebugVariable *V) { Variables.push_back(V); }
950 //===----------------------------------------------------------------------===//
951 /// LandingPadInfo - This structure is used to retain landing pad info for
952 /// the current function.
954 struct LandingPadInfo {
955 MachineBasicBlock *LandingPadBlock; // Landing pad block.
956 SmallVector<unsigned, 1> BeginLabels; // Labels prior to invoke.
957 SmallVector<unsigned, 1> EndLabels; // Labels after invoke.
958 unsigned LandingPadLabel; // Label at beginning of landing pad.
959 Function *Personality; // Personality function.
960 std::vector<int> TypeIds; // List of type ids (filters negative)
962 explicit LandingPadInfo(MachineBasicBlock *MBB)
963 : LandingPadBlock(MBB)
969 //===----------------------------------------------------------------------===//
970 /// MachineModuleInfo - This class contains meta information specific to a
971 /// module. Queries can be made by different debugging and exception handling
972 /// schemes and reformated for specific use.
974 class MachineModuleInfo : public ImmutablePass {
976 // Use the same deserializer/verifier for the module.
980 // CompileUnits - Uniquing vector for compile units.
981 UniqueVector<CompileUnitDesc *> CompileUnits;
983 // Directories - Uniquing vector for directories.
984 UniqueVector<std::string> Directories;
986 // SourceFiles - Uniquing vector for source files.
987 UniqueVector<SourceFileInfo> SourceFiles;
989 // Lines - List of of source line correspondence.
990 std::vector<SourceLineInfo> Lines;
992 // LabelIDList - One entry per assigned label. Normally the entry is equal to
993 // the list index(+1). If the entry is zero then the label has been deleted.
994 // Any other value indicates the label has been deleted by is mapped to
996 std::vector<unsigned> LabelIDList;
998 // ScopeMap - Tracks the scopes in the current function.
999 std::map<DebugInfoDesc *, DebugScope *> ScopeMap;
1001 // RootScope - Top level scope for the current function.
1003 DebugScope *RootScope;
1005 // FrameMoves - List of moves done by a function's prolog. Used to construct
1006 // frame maps by debug and exception handling consumers.
1007 std::vector<MachineMove> FrameMoves;
1009 // LandingPads - List of LandingPadInfo describing the landing pad information
1010 // in the current function.
1011 std::vector<LandingPadInfo> LandingPads;
1013 // TypeInfos - List of C++ TypeInfo used in the current function.
1015 std::vector<GlobalVariable *> TypeInfos;
1017 // FilterIds - List of typeids encoding filters used in the current function.
1019 std::vector<unsigned> FilterIds;
1021 // FilterEnds - List of the indices in FilterIds corresponding to filter
1024 std::vector<unsigned> FilterEnds;
1026 // Personalities - Vector of all personality functions ever seen. Used to emit
1027 // common EH frames.
1028 std::vector<Function *> Personalities;
1030 // UsedFunctions - the functions in the llvm.used list in a more easily
1031 // searchable format.
1032 SmallPtrSet<const Function *, 32> UsedFunctions;
1035 bool CallsUnwindInit;
1037 static char ID; // Pass identification, replacement for typeid
1039 MachineModuleInfo();
1040 ~MachineModuleInfo();
1042 /// doInitialization - Initialize the state for a new module.
1044 bool doInitialization();
1046 /// doFinalization - Tear down the state after completion of a module.
1048 bool doFinalization();
1050 /// BeginFunction - Begin gathering function meta information.
1052 void BeginFunction(MachineFunction *MF);
1054 /// EndFunction - Discard function meta information.
1058 /// getDescFor - Convert a Value to a debug information descriptor.
1060 // FIXME - use new Value type when available.
1061 DebugInfoDesc *getDescFor(Value *V);
1063 /// Verify - Verify that a Value is debug information descriptor.
1065 bool Verify(Value *V) { return VR.Verify(V); }
1067 /// isVerified - Return true if the specified GV has already been
1068 /// verified as a debug information descriptor.
1069 bool isVerified(GlobalVariable *GV) { return VR.isVerified(GV); }
1071 /// AnalyzeModule - Scan the module for global debug information.
1073 void AnalyzeModule(Module &M);
1075 /// hasDebugInfo - Returns true if valid debug info is present.
1077 bool hasDebugInfo() const { return !CompileUnits.empty(); }
1079 bool callsEHReturn() const { return CallsEHReturn; }
1080 void setCallsEHReturn(bool b) { CallsEHReturn = b; }
1082 bool callsUnwindInit() const { return CallsUnwindInit; }
1083 void setCallsUnwindInit(bool b) { CallsUnwindInit = b; }
1085 /// NextLabelID - Return the next unique label id.
1087 unsigned NextLabelID() {
1088 unsigned ID = (unsigned)LabelIDList.size() + 1;
1089 LabelIDList.push_back(ID);
1093 /// RecordSourceLine - Records location information and associates it with a
1094 /// label. Returns a unique label ID used to generate a label and
1095 /// provide correspondence to the source line list.
1096 unsigned RecordSourceLine(unsigned Line, unsigned Column, unsigned Source);
1098 /// InvalidateLabel - Inhibit use of the specified label # from
1099 /// MachineModuleInfo, for example because the code was deleted.
1100 void InvalidateLabel(unsigned LabelID) {
1101 // Remap to zero to indicate deletion.
1102 RemapLabel(LabelID, 0);
1105 /// RemapLabel - Indicate that a label has been merged into another.
1107 void RemapLabel(unsigned OldLabelID, unsigned NewLabelID) {
1108 assert(0 < OldLabelID && OldLabelID <= LabelIDList.size() &&
1109 "Old label ID out of range.");
1110 assert(NewLabelID <= LabelIDList.size() &&
1111 "New label ID out of range.");
1112 LabelIDList[OldLabelID - 1] = NewLabelID;
1115 /// MappedLabel - Find out the label's final ID. Zero indicates deletion.
1116 /// ID != Mapped ID indicates that the label was folded into another label.
1117 unsigned MappedLabel(unsigned LabelID) const {
1118 assert(LabelID <= LabelIDList.size() && "Debug label ID out of range.");
1119 return LabelID ? LabelIDList[LabelID - 1] : 0;
1122 /// RecordSource - Register a source file with debug info. Returns an source
1124 unsigned RecordSource(const std::string &Directory,
1125 const std::string &Source);
1126 unsigned RecordSource(const CompileUnitDesc *CompileUnit);
1128 /// getDirectories - Return the UniqueVector of std::string representing
1130 const UniqueVector<std::string> &getDirectories() const {
1134 /// getSourceFiles - Return the UniqueVector of source files.
1136 const UniqueVector<SourceFileInfo> &getSourceFiles() const {
1140 /// getSourceLines - Return a vector of source lines.
1142 const std::vector<SourceLineInfo> &getSourceLines() const {
1146 /// SetupCompileUnits - Set up the unique vector of compile units.
1148 void SetupCompileUnits(Module &M);
1150 /// getCompileUnits - Return a vector of debug compile units.
1152 const UniqueVector<CompileUnitDesc *> getCompileUnits() const;
1154 /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1155 /// named GlobalVariable.
1156 void getGlobalVariablesUsing(Module &M, const std::string &RootName,
1157 std::vector<GlobalVariable*> &Result);
1159 /// getAnchoredDescriptors - Return a vector of anchored debug descriptors.
1162 void getAnchoredDescriptors(Module &M, std::vector<T*> &AnchoredDescs) {
1164 std::vector<GlobalVariable *> Globals;
1165 getGlobalVariablesUsing(M, Desc.getAnchorString(), Globals);
1167 for (unsigned i = 0, N = Globals.size(); i < N; ++i) {
1168 GlobalVariable *GV = Globals[i];
1170 // FIXME - In the short term, changes are too drastic to continue.
1171 if (DebugInfoDesc::TagFromGlobal(GV) == Desc.getTag() &&
1172 DebugInfoDesc::VersionFromGlobal(GV) == LLVMDebugVersion) {
1173 AnchoredDescs.push_back(cast<T>(DR.Deserialize(GV)));
1178 /// RecordRegionStart - Indicate the start of a region.
1180 unsigned RecordRegionStart(Value *V);
1182 /// RecordRegionEnd - Indicate the end of a region.
1184 unsigned RecordRegionEnd(Value *V);
1186 /// RecordVariable - Indicate the declaration of a local variable.
1188 void RecordVariable(GlobalValue *GV, unsigned FrameIndex);
1190 /// getRootScope - Return current functions root scope.
1192 DebugScope *getRootScope() { return RootScope; }
1194 /// getOrCreateScope - Returns the scope associated with the given descriptor.
1196 DebugScope *getOrCreateScope(DebugInfoDesc *ScopeDesc);
1198 /// getFrameMoves - Returns a reference to a list of moves done in the current
1199 /// function's prologue. Used to construct frame maps for debug and exception
1200 /// handling comsumers.
1201 std::vector<MachineMove> &getFrameMoves() { return FrameMoves; }
1203 //===-EH-----------------------------------------------------------------===//
1205 /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
1206 /// specified MachineBasicBlock.
1207 LandingPadInfo &getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad);
1209 /// addInvoke - Provide the begin and end labels of an invoke style call and
1210 /// associate it with a try landing pad block.
1211 void addInvoke(MachineBasicBlock *LandingPad, unsigned BeginLabel,
1214 /// addLandingPad - Add a new panding pad. Returns the label ID for the
1215 /// landing pad entry.
1216 unsigned addLandingPad(MachineBasicBlock *LandingPad);
1218 /// addPersonality - Provide the personality function for the exception
1220 void addPersonality(MachineBasicBlock *LandingPad, Function *Personality);
1222 /// getPersonalityIndex - Get index of the current personality function inside
1223 /// Personalitites array
1224 unsigned getPersonalityIndex() const;
1226 /// getPersonalities - Return array of personality functions ever seen.
1227 const std::vector<Function *>& getPersonalities() const {
1228 return Personalities;
1231 // UsedFunctions - Return set of the functions in the llvm.used list.
1232 const SmallPtrSet<const Function *, 32>& getUsedFunctions() const {
1233 return UsedFunctions;
1236 /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
1238 void addCatchTypeInfo(MachineBasicBlock *LandingPad,
1239 std::vector<GlobalVariable *> &TyInfo);
1241 /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
1243 void addFilterTypeInfo(MachineBasicBlock *LandingPad,
1244 std::vector<GlobalVariable *> &TyInfo);
1246 /// addCleanup - Add a cleanup action for a landing pad.
1248 void addCleanup(MachineBasicBlock *LandingPad);
1250 /// getTypeIDFor - Return the type id for the specified typeinfo. This is
1252 unsigned getTypeIDFor(GlobalVariable *TI);
1254 /// getFilterIDFor - Return the id of the filter encoded by TyIds. This is
1256 int getFilterIDFor(std::vector<unsigned> &TyIds);
1258 /// TidyLandingPads - Remap landing pad labels and remove any deleted landing
1260 void TidyLandingPads();
1262 /// getLandingPads - Return a reference to the landing pad info for the
1263 /// current function.
1264 const std::vector<LandingPadInfo> &getLandingPads() const {
1268 /// getTypeInfos - Return a reference to the C++ typeinfo for the current
1270 const std::vector<GlobalVariable *> &getTypeInfos() const {
1274 /// getFilterIds - Return a reference to the typeids encoding filters used in
1275 /// the current function.
1276 const std::vector<unsigned> &getFilterIds() const {
1280 /// getPersonality - Return a personality function if available. The presence
1281 /// of one is required to emit exception handling info.
1282 Function *getPersonality() const;
1284 DIDeserializer *getDIDeserializer() { return &DR; }
1285 }; // End class MachineModuleInfo
1287 } // End llvm namespace