1 //===-- llvm/CodeGen/MachineModuleInfo.cpp ----------------------*- 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 #include "llvm/CodeGen/MachineModuleInfo.h"
12 #include "llvm/Constants.h"
13 #include "llvm/Analysis/ValueTracking.h"
14 #include "llvm/CodeGen/MachineFunctionPass.h"
15 #include "llvm/CodeGen/MachineFunction.h"
16 #include "llvm/CodeGen/Passes.h"
17 #include "llvm/Target/TargetInstrInfo.h"
18 #include "llvm/Target/TargetMachine.h"
19 #include "llvm/Target/TargetOptions.h"
20 #include "llvm/DerivedTypes.h"
21 #include "llvm/GlobalVariable.h"
22 #include "llvm/Intrinsics.h"
23 #include "llvm/Instructions.h"
24 #include "llvm/Module.h"
25 #include "llvm/Support/Dwarf.h"
26 #include "llvm/Support/Streams.h"
28 using namespace llvm::dwarf;
30 // Handle the Pass registration stuff necessary to use TargetData's.
31 static RegisterPass<MachineModuleInfo>
32 X("machinemoduleinfo", "Module Information");
33 char MachineModuleInfo::ID = 0;
35 //===----------------------------------------------------------------------===//
37 /// getGlobalVariablesUsing - Return all of the GlobalVariables which have the
38 /// specified value in their initializer somewhere.
40 getGlobalVariablesUsing(Value *V, std::vector<GlobalVariable*> &Result) {
41 // Scan though value users.
42 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
43 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I)) {
44 // If the user is a GlobalVariable then add to result.
46 } else if (Constant *C = dyn_cast<Constant>(*I)) {
47 // If the user is a constant variable then scan its users
48 getGlobalVariablesUsing(C, Result);
53 /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
54 /// named GlobalVariable.
56 getGlobalVariablesUsing(Module &M, const std::string &RootName,
57 std::vector<GlobalVariable*> &Result) {
58 std::vector<const Type*> FieldTypes;
59 FieldTypes.push_back(Type::Int32Ty);
60 FieldTypes.push_back(Type::Int32Ty);
62 // Get the GlobalVariable root.
63 GlobalVariable *UseRoot = M.getGlobalVariable(RootName,
64 StructType::get(FieldTypes));
66 // If present and linkonce then scan for users.
67 if (UseRoot && UseRoot->hasLinkOnceLinkage())
68 getGlobalVariablesUsing(UseRoot, Result);
71 /// isStringValue - Return true if the given value can be coerced to a string.
73 static bool isStringValue(Value *V) {
74 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
75 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
76 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
77 return Init->isString();
79 } else if (Constant *C = dyn_cast<Constant>(V)) {
80 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
81 return isStringValue(GV);
82 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
83 if (CE->getOpcode() == Instruction::GetElementPtr) {
84 if (CE->getNumOperands() == 3 &&
85 cast<Constant>(CE->getOperand(1))->isNullValue() &&
86 isa<ConstantInt>(CE->getOperand(2))) {
87 return isStringValue(CE->getOperand(0));
95 /// getGlobalVariable - Return either a direct or cast Global value.
97 static GlobalVariable *getGlobalVariable(Value *V) {
98 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
100 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
101 if (CE->getOpcode() == Instruction::BitCast) {
102 return dyn_cast<GlobalVariable>(CE->getOperand(0));
103 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
104 for (unsigned int i=1; i<CE->getNumOperands(); i++) {
105 if (!CE->getOperand(i)->isNullValue())
108 return dyn_cast<GlobalVariable>(CE->getOperand(0));
114 /// isGlobalVariable - Return true if the given value can be coerced to a
116 static bool isGlobalVariable(Value *V) {
117 if (isa<GlobalVariable>(V) || isa<ConstantPointerNull>(V)) {
119 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
120 if (CE->getOpcode() == Instruction::BitCast) {
121 return isa<GlobalVariable>(CE->getOperand(0));
122 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
123 for (unsigned int i=1; i<CE->getNumOperands(); i++) {
124 if (!CE->getOperand(i)->isNullValue())
127 return isa<GlobalVariable>(CE->getOperand(0));
133 /// getUIntOperand - Return ith operand if it is an unsigned integer.
135 static ConstantInt *getUIntOperand(GlobalVariable *GV, unsigned i) {
136 // Make sure the GlobalVariable has an initializer.
137 if (!GV->hasInitializer()) return NULL;
139 // Get the initializer constant.
140 ConstantStruct *CI = dyn_cast<ConstantStruct>(GV->getInitializer());
141 if (!CI) return NULL;
143 // Check if there is at least i + 1 operands.
144 unsigned N = CI->getNumOperands();
145 if (i >= N) return NULL;
148 return dyn_cast<ConstantInt>(CI->getOperand(i));
151 //===----------------------------------------------------------------------===//
153 static unsigned CountFields(DebugInfoDesc *DD) {
156 switch (DD->getTag()) {
157 case DW_TAG_anchor: // AnchorDesc
162 case DW_TAG_compile_unit: // CompileUnitDesc
164 // if (Version == 0) DebugVersion
171 // Handle cases out of sync with compiler.
172 if (DD->getVersion() == 0)
176 case DW_TAG_variable: // GlobalVariableDesc
190 case DW_TAG_subprogram: // SubprogramDesc
203 case DW_TAG_lexical_block: // BlockDesc
208 case DW_TAG_base_type: // BasicTypeDesc
217 // if (Version > LLVMDebugVersion4) Flags
221 if (DD->getVersion() > LLVMDebugVersion4)
226 case DW_TAG_pointer_type:
227 case DW_TAG_reference_type:
228 case DW_TAG_const_type:
229 case DW_TAG_volatile_type:
230 case DW_TAG_restrict_type:
232 case DW_TAG_inheritance: // DerivedTypeDesc
241 // if (Version > LLVMDebugVersion4) Flags
245 if (DD->getVersion() > LLVMDebugVersion4)
249 case DW_TAG_array_type:
250 case DW_TAG_structure_type:
251 case DW_TAG_union_type:
252 case DW_TAG_enumeration_type:
253 case DW_TAG_vector_type:
254 case DW_TAG_subroutine_type: // CompositeTypeDesc
263 // if (Version > LLVMDebugVersion4) Flags
268 if (DD->getVersion() > LLVMDebugVersion4)
272 case DW_TAG_subrange_type: // SubrangeDesc
278 case DW_TAG_enumerator: // EnumeratorDesc
284 case DW_TAG_return_variable:
285 case DW_TAG_arg_variable:
286 case DW_TAG_auto_variable: // VariableDesc
302 //===----------------------------------------------------------------------===//
304 /// ApplyToFields - Target the visitor to each field of the debug information
306 void DIVisitor::ApplyToFields(DebugInfoDesc *DD) {
307 DD->ApplyToFields(this);
312 //===----------------------------------------------------------------------===//
313 /// DIDeserializeVisitor - This DIVisitor deserializes all the fields in the
314 /// supplied DebugInfoDesc.
315 class DIDeserializeVisitor : public DIVisitor {
317 DIDeserializer &DR; // Active deserializer.
318 unsigned I; // Current operand index.
319 ConstantStruct *CI; // GlobalVariable constant initializer.
322 DIDeserializeVisitor(DIDeserializer &D, GlobalVariable *GV)
323 : DIVisitor(), DR(D), I(0), CI(cast<ConstantStruct>(GV->getInitializer()))
326 /// Apply - Set the value of each of the fields.
328 virtual void Apply(int &Field) {
329 Constant *C = CI->getOperand(I++);
330 Field = cast<ConstantInt>(C)->getSExtValue();
332 virtual void Apply(unsigned &Field) {
333 Constant *C = CI->getOperand(I++);
334 Field = cast<ConstantInt>(C)->getZExtValue();
336 virtual void Apply(int64_t &Field) {
337 Constant *C = CI->getOperand(I++);
338 Field = cast<ConstantInt>(C)->getSExtValue();
340 virtual void Apply(uint64_t &Field) {
341 Constant *C = CI->getOperand(I++);
342 Field = cast<ConstantInt>(C)->getZExtValue();
344 virtual void Apply(bool &Field) {
345 Constant *C = CI->getOperand(I++);
346 Field = cast<ConstantInt>(C)->getZExtValue();
348 virtual void Apply(std::string &Field) {
349 Constant *C = CI->getOperand(I++);
350 // Fills in the string if it succeeds
351 if (!GetConstantStringInfo(C, Field))
354 virtual void Apply(DebugInfoDesc *&Field) {
355 Constant *C = CI->getOperand(I++);
356 Field = DR.Deserialize(C);
358 virtual void Apply(GlobalVariable *&Field) {
359 Constant *C = CI->getOperand(I++);
360 Field = getGlobalVariable(C);
362 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
364 Constant *C = CI->getOperand(I++);
365 GlobalVariable *GV = getGlobalVariable(C);
366 if (GV && GV->hasInitializer()) {
367 if (ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer())) {
368 for (unsigned i = 0, N = CA->getNumOperands(); i < N; ++i) {
369 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
370 DebugInfoDesc *DE = DR.Deserialize(GVE);
373 } else if (GV->getInitializer()->isNullValue()) {
374 if (const ArrayType *T =
375 dyn_cast<ArrayType>(GV->getType()->getElementType())) {
376 Field.resize(T->getNumElements());
383 //===----------------------------------------------------------------------===//
384 /// DISerializeVisitor - This DIVisitor serializes all the fields in
385 /// the supplied DebugInfoDesc.
386 class DISerializeVisitor : public DIVisitor {
388 DISerializer &SR; // Active serializer.
389 std::vector<Constant*> &Elements; // Element accumulator.
392 DISerializeVisitor(DISerializer &S, std::vector<Constant*> &E)
398 /// Apply - Set the value of each of the fields.
400 virtual void Apply(int &Field) {
401 Elements.push_back(ConstantInt::get(Type::Int32Ty, int32_t(Field)));
403 virtual void Apply(unsigned &Field) {
404 Elements.push_back(ConstantInt::get(Type::Int32Ty, uint32_t(Field)));
406 virtual void Apply(int64_t &Field) {
407 Elements.push_back(ConstantInt::get(Type::Int64Ty, int64_t(Field)));
409 virtual void Apply(uint64_t &Field) {
410 Elements.push_back(ConstantInt::get(Type::Int64Ty, uint64_t(Field)));
412 virtual void Apply(bool &Field) {
413 Elements.push_back(ConstantInt::get(Type::Int1Ty, Field));
415 virtual void Apply(std::string &Field) {
416 Elements.push_back(SR.getString(Field));
418 virtual void Apply(DebugInfoDesc *&Field) {
419 GlobalVariable *GV = NULL;
421 // If non-NULL then convert to global.
422 if (Field) GV = SR.Serialize(Field);
424 // FIXME - At some point should use specific type.
425 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
428 // Set to pointer to global.
429 Elements.push_back(ConstantExpr::getBitCast(GV, EmptyTy));
432 Elements.push_back(ConstantPointerNull::get(EmptyTy));
435 virtual void Apply(GlobalVariable *&Field) {
436 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
438 Elements.push_back(ConstantExpr::getBitCast(Field, EmptyTy));
440 Elements.push_back(ConstantPointerNull::get(EmptyTy));
443 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
444 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
445 unsigned N = Field.size();
446 ArrayType *AT = ArrayType::get(EmptyTy, N);
447 std::vector<Constant *> ArrayElements;
449 for (unsigned i = 0, N = Field.size(); i < N; ++i) {
450 if (DebugInfoDesc *Element = Field[i]) {
451 GlobalVariable *GVE = SR.Serialize(Element);
452 Constant *CE = ConstantExpr::getBitCast(GVE, EmptyTy);
453 ArrayElements.push_back(cast<Constant>(CE));
455 ArrayElements.push_back(ConstantPointerNull::get(EmptyTy));
459 Constant *CA = ConstantArray::get(AT, ArrayElements);
460 GlobalVariable *CAGV = new GlobalVariable(AT, true,
461 GlobalValue::InternalLinkage,
462 CA, "llvm.dbg.array",
464 CAGV->setSection("llvm.metadata");
465 Constant *CAE = ConstantExpr::getBitCast(CAGV, EmptyTy);
466 Elements.push_back(CAE);
470 //===----------------------------------------------------------------------===//
471 /// DIGetTypesVisitor - This DIVisitor gathers all the field types in
472 /// the supplied DebugInfoDesc.
473 class DIGetTypesVisitor : public DIVisitor {
475 DISerializer &SR; // Active serializer.
476 std::vector<const Type*> &Fields; // Type accumulator.
479 DIGetTypesVisitor(DISerializer &S, std::vector<const Type*> &F)
485 /// Apply - Set the value of each of the fields.
487 virtual void Apply(int &Field) {
488 Fields.push_back(Type::Int32Ty);
490 virtual void Apply(unsigned &Field) {
491 Fields.push_back(Type::Int32Ty);
493 virtual void Apply(int64_t &Field) {
494 Fields.push_back(Type::Int64Ty);
496 virtual void Apply(uint64_t &Field) {
497 Fields.push_back(Type::Int64Ty);
499 virtual void Apply(bool &Field) {
500 Fields.push_back(Type::Int1Ty);
502 virtual void Apply(std::string &Field) {
503 Fields.push_back(SR.getStrPtrType());
505 virtual void Apply(DebugInfoDesc *&Field) {
506 // FIXME - At some point should use specific type.
507 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
508 Fields.push_back(EmptyTy);
510 virtual void Apply(GlobalVariable *&Field) {
511 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
512 Fields.push_back(EmptyTy);
514 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
515 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
516 Fields.push_back(EmptyTy);
520 //===----------------------------------------------------------------------===//
521 /// DIVerifyVisitor - This DIVisitor verifies all the field types against
522 /// a constant initializer.
523 class DIVerifyVisitor : public DIVisitor {
525 DIVerifier &VR; // Active verifier.
526 bool IsValid; // Validity status.
527 unsigned I; // Current operand index.
528 ConstantStruct *CI; // GlobalVariable constant initializer.
531 DIVerifyVisitor(DIVerifier &V, GlobalVariable *GV)
536 , CI(cast<ConstantStruct>(GV->getInitializer()))
541 bool isValid() const { return IsValid; }
543 /// Apply - Set the value of each of the fields.
545 virtual void Apply(int &Field) {
546 Constant *C = CI->getOperand(I++);
547 IsValid = IsValid && isa<ConstantInt>(C);
549 virtual void Apply(unsigned &Field) {
550 Constant *C = CI->getOperand(I++);
551 IsValid = IsValid && isa<ConstantInt>(C);
553 virtual void Apply(int64_t &Field) {
554 Constant *C = CI->getOperand(I++);
555 IsValid = IsValid && isa<ConstantInt>(C);
557 virtual void Apply(uint64_t &Field) {
558 Constant *C = CI->getOperand(I++);
559 IsValid = IsValid && isa<ConstantInt>(C);
561 virtual void Apply(bool &Field) {
562 Constant *C = CI->getOperand(I++);
563 IsValid = IsValid && isa<ConstantInt>(C) && C->getType() == Type::Int1Ty;
565 virtual void Apply(std::string &Field) {
566 Constant *C = CI->getOperand(I++);
568 (!C || isStringValue(C) || C->isNullValue());
570 virtual void Apply(DebugInfoDesc *&Field) {
571 // FIXME - Prepare the correct descriptor.
572 Constant *C = CI->getOperand(I++);
573 IsValid = IsValid && isGlobalVariable(C);
575 virtual void Apply(GlobalVariable *&Field) {
576 Constant *C = CI->getOperand(I++);
577 IsValid = IsValid && isGlobalVariable(C);
579 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
580 Constant *C = CI->getOperand(I++);
581 IsValid = IsValid && isGlobalVariable(C);
582 if (!IsValid) return;
584 GlobalVariable *GV = getGlobalVariable(C);
585 IsValid = IsValid && GV && GV->hasInitializer();
586 if (!IsValid) return;
588 ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer());
589 IsValid = IsValid && CA;
590 if (!IsValid) return;
592 for (unsigned i = 0, N = CA->getNumOperands(); IsValid && i < N; ++i) {
593 IsValid = IsValid && isGlobalVariable(CA->getOperand(i));
594 if (!IsValid) return;
596 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
604 //===----------------------------------------------------------------------===//
606 /// TagFromGlobal - Returns the tag number from a debug info descriptor
607 /// GlobalVariable. Return DIIValid if operand is not an unsigned int.
608 unsigned DebugInfoDesc::TagFromGlobal(GlobalVariable *GV) {
609 ConstantInt *C = getUIntOperand(GV, 0);
610 return C ? ((unsigned)C->getZExtValue() & ~LLVMDebugVersionMask) :
611 (unsigned)DW_TAG_invalid;
614 /// VersionFromGlobal - Returns the version number from a debug info
615 /// descriptor GlobalVariable. Return DIIValid if operand is not an unsigned
617 unsigned DebugInfoDesc::VersionFromGlobal(GlobalVariable *GV) {
618 ConstantInt *C = getUIntOperand(GV, 0);
619 return C ? ((unsigned)C->getZExtValue() & LLVMDebugVersionMask) :
620 (unsigned)DW_TAG_invalid;
623 /// DescFactory - Create an instance of debug info descriptor based on Tag.
624 /// Return NULL if not a recognized Tag.
625 DebugInfoDesc *DebugInfoDesc::DescFactory(unsigned Tag) {
627 case DW_TAG_anchor: return new AnchorDesc();
628 case DW_TAG_compile_unit: return new CompileUnitDesc();
629 case DW_TAG_variable: return new GlobalVariableDesc();
630 case DW_TAG_subprogram: return new SubprogramDesc();
631 case DW_TAG_lexical_block: return new BlockDesc();
632 case DW_TAG_base_type: return new BasicTypeDesc();
634 case DW_TAG_pointer_type:
635 case DW_TAG_reference_type:
636 case DW_TAG_const_type:
637 case DW_TAG_volatile_type:
638 case DW_TAG_restrict_type:
640 case DW_TAG_inheritance: return new DerivedTypeDesc(Tag);
641 case DW_TAG_array_type:
642 case DW_TAG_structure_type:
643 case DW_TAG_union_type:
644 case DW_TAG_enumeration_type:
645 case DW_TAG_vector_type:
646 case DW_TAG_subroutine_type: return new CompositeTypeDesc(Tag);
647 case DW_TAG_subrange_type: return new SubrangeDesc();
648 case DW_TAG_enumerator: return new EnumeratorDesc();
649 case DW_TAG_return_variable:
650 case DW_TAG_arg_variable:
651 case DW_TAG_auto_variable: return new VariableDesc(Tag);
657 /// getLinkage - get linkage appropriate for this type of descriptor.
659 GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const {
660 return GlobalValue::InternalLinkage;
663 /// ApplyToFields - Target the vistor to the fields of the descriptor.
665 void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) {
669 //===----------------------------------------------------------------------===//
671 AnchorDesc::AnchorDesc()
672 : DebugInfoDesc(DW_TAG_anchor)
675 AnchorDesc::AnchorDesc(AnchoredDesc *D)
676 : DebugInfoDesc(DW_TAG_anchor)
677 , AnchorTag(D->getTag())
680 // Implement isa/cast/dyncast.
681 bool AnchorDesc::classof(const DebugInfoDesc *D) {
682 return D->getTag() == DW_TAG_anchor;
685 /// getLinkage - get linkage appropriate for this type of descriptor.
687 GlobalValue::LinkageTypes AnchorDesc::getLinkage() const {
688 return GlobalValue::LinkOnceLinkage;
691 /// ApplyToFields - Target the visitor to the fields of the TransUnitDesc.
693 void AnchorDesc::ApplyToFields(DIVisitor *Visitor) {
694 DebugInfoDesc::ApplyToFields(Visitor);
696 Visitor->Apply(AnchorTag);
699 /// getDescString - Return a string used to compose global names and labels. A
700 /// A global variable name needs to be defined for each debug descriptor that is
701 /// anchored. NOTE: that each global variable named here also needs to be added
702 /// to the list of names left external in the internalizer.
703 /// ExternalNames.insert("llvm.dbg.compile_units");
704 /// ExternalNames.insert("llvm.dbg.global_variables");
705 /// ExternalNames.insert("llvm.dbg.subprograms");
706 const char *AnchorDesc::getDescString() const {
708 case DW_TAG_compile_unit: return CompileUnitDesc::AnchorString;
709 case DW_TAG_variable: return GlobalVariableDesc::AnchorString;
710 case DW_TAG_subprogram: return SubprogramDesc::AnchorString;
714 assert(0 && "Tag does not have a case for anchor string");
718 /// getTypeString - Return a string used to label this descriptors type.
720 const char *AnchorDesc::getTypeString() const {
721 return "llvm.dbg.anchor.type";
725 void AnchorDesc::dump() {
726 cerr << getDescString() << " "
727 << "Version(" << getVersion() << "), "
728 << "Tag(" << getTag() << "), "
729 << "AnchorTag(" << AnchorTag << ")\n";
733 //===----------------------------------------------------------------------===//
735 AnchoredDesc::AnchoredDesc(unsigned T)
740 /// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
742 void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) {
743 DebugInfoDesc::ApplyToFields(Visitor);
745 Visitor->Apply(Anchor);
748 //===----------------------------------------------------------------------===//
750 CompileUnitDesc::CompileUnitDesc()
751 : AnchoredDesc(DW_TAG_compile_unit)
758 // Implement isa/cast/dyncast.
759 bool CompileUnitDesc::classof(const DebugInfoDesc *D) {
760 return D->getTag() == DW_TAG_compile_unit;
763 /// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
765 void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) {
766 AnchoredDesc::ApplyToFields(Visitor);
768 // Handle cases out of sync with compiler.
769 if (getVersion() == 0) {
770 unsigned DebugVersion;
771 Visitor->Apply(DebugVersion);
774 Visitor->Apply(Language);
775 Visitor->Apply(FileName);
776 Visitor->Apply(Directory);
777 Visitor->Apply(Producer);
780 /// getDescString - Return a string used to compose global names and labels.
782 const char *CompileUnitDesc::getDescString() const {
783 return "llvm.dbg.compile_unit";
786 /// getTypeString - Return a string used to label this descriptors type.
788 const char *CompileUnitDesc::getTypeString() const {
789 return "llvm.dbg.compile_unit.type";
792 /// getAnchorString - Return a string used to label this descriptor's anchor.
794 const char *const CompileUnitDesc::AnchorString = "llvm.dbg.compile_units";
795 const char *CompileUnitDesc::getAnchorString() const {
800 void CompileUnitDesc::dump() {
801 cerr << getDescString() << " "
802 << "Version(" << getVersion() << "), "
803 << "Tag(" << getTag() << "), "
804 << "Anchor(" << getAnchor() << "), "
805 << "Language(" << Language << "), "
806 << "FileName(\"" << FileName << "\"), "
807 << "Directory(\"" << Directory << "\"), "
808 << "Producer(\"" << Producer << "\")\n";
812 //===----------------------------------------------------------------------===//
814 TypeDesc::TypeDesc(unsigned T)
826 /// ApplyToFields - Target the visitor to the fields of the TypeDesc.
828 void TypeDesc::ApplyToFields(DIVisitor *Visitor) {
829 DebugInfoDesc::ApplyToFields(Visitor);
831 Visitor->Apply(Context);
832 Visitor->Apply(Name);
833 Visitor->Apply(File);
834 Visitor->Apply(Line);
835 Visitor->Apply(Size);
836 Visitor->Apply(Align);
837 Visitor->Apply(Offset);
838 if (getVersion() > LLVMDebugVersion4) Visitor->Apply(Flags);
841 /// getDescString - Return a string used to compose global names and labels.
843 const char *TypeDesc::getDescString() const {
844 return "llvm.dbg.type";
847 /// getTypeString - Return a string used to label this descriptor's type.
849 const char *TypeDesc::getTypeString() const {
850 return "llvm.dbg.type.type";
854 void TypeDesc::dump() {
855 cerr << getDescString() << " "
856 << "Version(" << getVersion() << "), "
857 << "Tag(" << getTag() << "), "
858 << "Context(" << Context << "), "
859 << "Name(\"" << Name << "\"), "
860 << "File(" << File << "), "
861 << "Line(" << Line << "), "
862 << "Size(" << Size << "), "
863 << "Align(" << Align << "), "
864 << "Offset(" << Offset << "), "
865 << "Flags(" << Flags << ")\n";
869 //===----------------------------------------------------------------------===//
871 BasicTypeDesc::BasicTypeDesc()
872 : TypeDesc(DW_TAG_base_type)
876 // Implement isa/cast/dyncast.
877 bool BasicTypeDesc::classof(const DebugInfoDesc *D) {
878 return D->getTag() == DW_TAG_base_type;
881 /// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
883 void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
884 TypeDesc::ApplyToFields(Visitor);
886 Visitor->Apply(Encoding);
889 /// getDescString - Return a string used to compose global names and labels.
891 const char *BasicTypeDesc::getDescString() const {
892 return "llvm.dbg.basictype";
895 /// getTypeString - Return a string used to label this descriptor's type.
897 const char *BasicTypeDesc::getTypeString() const {
898 return "llvm.dbg.basictype.type";
902 void BasicTypeDesc::dump() {
903 cerr << getDescString() << " "
904 << "Version(" << getVersion() << "), "
905 << "Tag(" << getTag() << "), "
906 << "Context(" << getContext() << "), "
907 << "Name(\"" << getName() << "\"), "
908 << "Size(" << getSize() << "), "
909 << "Encoding(" << Encoding << "),"
910 << "Flags(" << Flags << ")\n";
914 //===----------------------------------------------------------------------===//
916 DerivedTypeDesc::DerivedTypeDesc(unsigned T)
921 // Implement isa/cast/dyncast.
922 bool DerivedTypeDesc::classof(const DebugInfoDesc *D) {
923 unsigned T = D->getTag();
926 case DW_TAG_pointer_type:
927 case DW_TAG_reference_type:
928 case DW_TAG_const_type:
929 case DW_TAG_volatile_type:
930 case DW_TAG_restrict_type:
932 case DW_TAG_inheritance:
939 /// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
941 void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) {
942 TypeDesc::ApplyToFields(Visitor);
944 Visitor->Apply(FromType);
947 /// getDescString - Return a string used to compose global names and labels.
949 const char *DerivedTypeDesc::getDescString() const {
950 return "llvm.dbg.derivedtype";
953 /// getTypeString - Return a string used to label this descriptor's type.
955 const char *DerivedTypeDesc::getTypeString() const {
956 return "llvm.dbg.derivedtype.type";
960 void DerivedTypeDesc::dump() {
961 cerr << getDescString() << " "
962 << "Version(" << getVersion() << "), "
963 << "Tag(" << getTag() << "), "
964 << "Context(" << getContext() << "), "
965 << "Name(\"" << getName() << "\"), "
966 << "Size(" << getSize() << "), "
967 << "File(" << getFile() << "), "
968 << "Line(" << getLine() << "), "
969 << "FromType(" << FromType << "),"
970 << "Flags(" << Flags << ")\n";
974 //===----------------------------------------------------------------------===//
976 CompositeTypeDesc::CompositeTypeDesc(unsigned T)
981 // Implement isa/cast/dyncast.
982 bool CompositeTypeDesc::classof(const DebugInfoDesc *D) {
983 unsigned T = D->getTag();
985 case DW_TAG_array_type:
986 case DW_TAG_structure_type:
987 case DW_TAG_union_type:
988 case DW_TAG_enumeration_type:
989 case DW_TAG_vector_type:
990 case DW_TAG_subroutine_type:
997 /// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
999 void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) {
1000 DerivedTypeDesc::ApplyToFields(Visitor);
1002 Visitor->Apply(Elements);
1005 /// getDescString - Return a string used to compose global names and labels.
1007 const char *CompositeTypeDesc::getDescString() const {
1008 return "llvm.dbg.compositetype";
1011 /// getTypeString - Return a string used to label this descriptor's type.
1013 const char *CompositeTypeDesc::getTypeString() const {
1014 return "llvm.dbg.compositetype.type";
1018 void CompositeTypeDesc::dump() {
1019 cerr << getDescString() << " "
1020 << "Version(" << getVersion() << "), "
1021 << "Tag(" << getTag() << "), "
1022 << "Context(" << getContext() << "), "
1023 << "Name(\"" << getName() << "\"), "
1024 << "Size(" << getSize() << "), "
1025 << "File(" << getFile() << "), "
1026 << "Line(" << getLine() << "), "
1027 << "FromType(" << getFromType() << "), "
1028 << "Elements.size(" << Elements.size() << "),"
1029 << "Flags(" << Flags << ")\n";
1033 //===----------------------------------------------------------------------===//
1035 SubrangeDesc::SubrangeDesc()
1036 : DebugInfoDesc(DW_TAG_subrange_type)
1041 // Implement isa/cast/dyncast.
1042 bool SubrangeDesc::classof(const DebugInfoDesc *D) {
1043 return D->getTag() == DW_TAG_subrange_type;
1046 /// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
1048 void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) {
1049 DebugInfoDesc::ApplyToFields(Visitor);
1055 /// getDescString - Return a string used to compose global names and labels.
1057 const char *SubrangeDesc::getDescString() const {
1058 return "llvm.dbg.subrange";
1061 /// getTypeString - Return a string used to label this descriptor's type.
1063 const char *SubrangeDesc::getTypeString() const {
1064 return "llvm.dbg.subrange.type";
1068 void SubrangeDesc::dump() {
1069 cerr << getDescString() << " "
1070 << "Version(" << getVersion() << "), "
1071 << "Tag(" << getTag() << "), "
1072 << "Lo(" << Lo << "), "
1073 << "Hi(" << Hi << ")\n";
1077 //===----------------------------------------------------------------------===//
1079 EnumeratorDesc::EnumeratorDesc()
1080 : DebugInfoDesc(DW_TAG_enumerator)
1085 // Implement isa/cast/dyncast.
1086 bool EnumeratorDesc::classof(const DebugInfoDesc *D) {
1087 return D->getTag() == DW_TAG_enumerator;
1090 /// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
1092 void EnumeratorDesc::ApplyToFields(DIVisitor *Visitor) {
1093 DebugInfoDesc::ApplyToFields(Visitor);
1095 Visitor->Apply(Name);
1096 Visitor->Apply(Value);
1099 /// getDescString - Return a string used to compose global names and labels.
1101 const char *EnumeratorDesc::getDescString() const {
1102 return "llvm.dbg.enumerator";
1105 /// getTypeString - Return a string used to label this descriptor's type.
1107 const char *EnumeratorDesc::getTypeString() const {
1108 return "llvm.dbg.enumerator.type";
1112 void EnumeratorDesc::dump() {
1113 cerr << getDescString() << " "
1114 << "Version(" << getVersion() << "), "
1115 << "Tag(" << getTag() << "), "
1116 << "Name(" << Name << "), "
1117 << "Value(" << Value << ")\n";
1121 //===----------------------------------------------------------------------===//
1123 VariableDesc::VariableDesc(unsigned T)
1132 // Implement isa/cast/dyncast.
1133 bool VariableDesc::classof(const DebugInfoDesc *D) {
1134 unsigned T = D->getTag();
1136 case DW_TAG_auto_variable:
1137 case DW_TAG_arg_variable:
1138 case DW_TAG_return_variable:
1145 /// ApplyToFields - Target the visitor to the fields of the VariableDesc.
1147 void VariableDesc::ApplyToFields(DIVisitor *Visitor) {
1148 DebugInfoDesc::ApplyToFields(Visitor);
1150 Visitor->Apply(Context);
1151 Visitor->Apply(Name);
1152 Visitor->Apply(File);
1153 Visitor->Apply(Line);
1154 Visitor->Apply(TyDesc);
1157 /// getDescString - Return a string used to compose global names and labels.
1159 const char *VariableDesc::getDescString() const {
1160 return "llvm.dbg.variable";
1163 /// getTypeString - Return a string used to label this descriptor's type.
1165 const char *VariableDesc::getTypeString() const {
1166 return "llvm.dbg.variable.type";
1170 void VariableDesc::dump() {
1171 cerr << getDescString() << " "
1172 << "Version(" << getVersion() << "), "
1173 << "Tag(" << getTag() << "), "
1174 << "Context(" << Context << "), "
1175 << "Name(\"" << Name << "\"), "
1176 << "File(" << File << "), "
1177 << "Line(" << Line << "), "
1178 << "TyDesc(" << TyDesc << ")\n";
1182 //===----------------------------------------------------------------------===//
1184 GlobalDesc::GlobalDesc(unsigned T)
1194 , IsDefinition(false)
1197 /// ApplyToFields - Target the visitor to the fields of the global.
1199 void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
1200 AnchoredDesc::ApplyToFields(Visitor);
1202 Visitor->Apply(Context);
1203 Visitor->Apply(Name);
1204 Visitor->Apply(FullName);
1205 Visitor->Apply(LinkageName);
1206 Visitor->Apply(File);
1207 Visitor->Apply(Line);
1208 Visitor->Apply(TyDesc);
1209 Visitor->Apply(IsStatic);
1210 Visitor->Apply(IsDefinition);
1213 //===----------------------------------------------------------------------===//
1215 GlobalVariableDesc::GlobalVariableDesc()
1216 : GlobalDesc(DW_TAG_variable)
1220 // Implement isa/cast/dyncast.
1221 bool GlobalVariableDesc::classof(const DebugInfoDesc *D) {
1222 return D->getTag() == DW_TAG_variable;
1225 /// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
1227 void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
1228 GlobalDesc::ApplyToFields(Visitor);
1230 Visitor->Apply(Global);
1233 /// getDescString - Return a string used to compose global names and labels.
1235 const char *GlobalVariableDesc::getDescString() const {
1236 return "llvm.dbg.global_variable";
1239 /// getTypeString - Return a string used to label this descriptors type.
1241 const char *GlobalVariableDesc::getTypeString() const {
1242 return "llvm.dbg.global_variable.type";
1245 /// getAnchorString - Return a string used to label this descriptor's anchor.
1247 const char *const GlobalVariableDesc::AnchorString = "llvm.dbg.global_variables";
1248 const char *GlobalVariableDesc::getAnchorString() const {
1249 return AnchorString;
1253 void GlobalVariableDesc::dump() {
1254 cerr << getDescString() << " "
1255 << "Version(" << getVersion() << "), "
1256 << "Tag(" << getTag() << "), "
1257 << "Anchor(" << getAnchor() << "), "
1258 << "Name(\"" << getName() << "\"), "
1259 << "FullName(\"" << getFullName() << "\"), "
1260 << "LinkageName(\"" << getLinkageName() << "\"), "
1261 << "File(" << getFile() << "),"
1262 << "Line(" << getLine() << "),"
1263 << "Type(" << getType() << "), "
1264 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1265 << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
1266 << "Global(" << Global << ")\n";
1270 //===----------------------------------------------------------------------===//
1272 SubprogramDesc::SubprogramDesc()
1273 : GlobalDesc(DW_TAG_subprogram)
1276 // Implement isa/cast/dyncast.
1277 bool SubprogramDesc::classof(const DebugInfoDesc *D) {
1278 return D->getTag() == DW_TAG_subprogram;
1281 /// ApplyToFields - Target the visitor to the fields of the
1283 void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
1284 GlobalDesc::ApplyToFields(Visitor);
1287 /// getDescString - Return a string used to compose global names and labels.
1289 const char *SubprogramDesc::getDescString() const {
1290 return "llvm.dbg.subprogram";
1293 /// getTypeString - Return a string used to label this descriptors type.
1295 const char *SubprogramDesc::getTypeString() const {
1296 return "llvm.dbg.subprogram.type";
1299 /// getAnchorString - Return a string used to label this descriptor's anchor.
1301 const char *const SubprogramDesc::AnchorString = "llvm.dbg.subprograms";
1302 const char *SubprogramDesc::getAnchorString() const {
1303 return AnchorString;
1307 void SubprogramDesc::dump() {
1308 cerr << getDescString() << " "
1309 << "Version(" << getVersion() << "), "
1310 << "Tag(" << getTag() << "), "
1311 << "Anchor(" << getAnchor() << "), "
1312 << "Name(\"" << getName() << "\"), "
1313 << "FullName(\"" << getFullName() << "\"), "
1314 << "LinkageName(\"" << getLinkageName() << "\"), "
1315 << "File(" << getFile() << "),"
1316 << "Line(" << getLine() << "),"
1317 << "Type(" << getType() << "), "
1318 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1319 << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
1323 //===----------------------------------------------------------------------===//
1325 BlockDesc::BlockDesc()
1326 : DebugInfoDesc(DW_TAG_lexical_block)
1330 // Implement isa/cast/dyncast.
1331 bool BlockDesc::classof(const DebugInfoDesc *D) {
1332 return D->getTag() == DW_TAG_lexical_block;
1335 /// ApplyToFields - Target the visitor to the fields of the BlockDesc.
1337 void BlockDesc::ApplyToFields(DIVisitor *Visitor) {
1338 DebugInfoDesc::ApplyToFields(Visitor);
1340 Visitor->Apply(Context);
1343 /// getDescString - Return a string used to compose global names and labels.
1345 const char *BlockDesc::getDescString() const {
1346 return "llvm.dbg.block";
1349 /// getTypeString - Return a string used to label this descriptors type.
1351 const char *BlockDesc::getTypeString() const {
1352 return "llvm.dbg.block.type";
1356 void BlockDesc::dump() {
1357 cerr << getDescString() << " "
1358 << "Version(" << getVersion() << "), "
1359 << "Tag(" << getTag() << "),"
1360 << "Context(" << Context << ")\n";
1364 //===----------------------------------------------------------------------===//
1366 DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
1367 return Deserialize(getGlobalVariable(V));
1369 DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
1371 if (!GV) return NULL;
1373 // Check to see if it has been already deserialized.
1374 DebugInfoDesc *&Slot = GlobalDescs[GV];
1375 if (Slot) return Slot;
1377 // Get the Tag from the global.
1378 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1380 // Create an empty instance of the correct sort.
1381 Slot = DebugInfoDesc::DescFactory(Tag);
1383 // If not a user defined descriptor.
1385 // Deserialize the fields.
1386 DIDeserializeVisitor DRAM(*this, GV);
1387 DRAM.ApplyToFields(Slot);
1393 //===----------------------------------------------------------------------===//
1395 /// getStrPtrType - Return a "sbyte *" type.
1397 const PointerType *DISerializer::getStrPtrType() {
1398 // If not already defined.
1400 // Construct the pointer to signed bytes.
1401 StrPtrTy = PointerType::getUnqual(Type::Int8Ty);
1407 /// getEmptyStructPtrType - Return a "{ }*" type.
1409 const PointerType *DISerializer::getEmptyStructPtrType() {
1410 // If not already defined.
1411 if (EmptyStructPtrTy) return EmptyStructPtrTy;
1413 // Construct the pointer to empty structure type.
1414 const StructType *EmptyStructTy = StructType::get(NULL, NULL);
1416 // Construct the pointer to empty structure type.
1417 EmptyStructPtrTy = PointerType::getUnqual(EmptyStructTy);
1418 return EmptyStructPtrTy;
1421 /// getTagType - Return the type describing the specified descriptor (via tag.)
1423 const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
1424 // Attempt to get the previously defined type.
1425 StructType *&Ty = TagTypes[DD->getTag()];
1427 // If not already defined.
1429 // Set up fields vector.
1430 std::vector<const Type*> Fields;
1431 // Get types of fields.
1432 DIGetTypesVisitor GTAM(*this, Fields);
1433 GTAM.ApplyToFields(DD);
1435 // Construct structured type.
1436 Ty = StructType::get(Fields);
1438 // Register type name with module.
1439 M->addTypeName(DD->getTypeString(), Ty);
1445 /// getString - Construct the string as constant string global.
1447 Constant *DISerializer::getString(const std::string &String) {
1448 // Check string cache for previous edition.
1449 Constant *&Slot = StringCache[String];
1451 // Return Constant if previously defined.
1452 if (Slot) return Slot;
1454 // If empty string then use a sbyte* null instead.
1455 if (String.empty()) {
1456 Slot = ConstantPointerNull::get(getStrPtrType());
1458 // Construct string as an llvm constant.
1459 Constant *ConstStr = ConstantArray::get(String);
1461 // Otherwise create and return a new string global.
1462 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
1463 GlobalVariable::InternalLinkage,
1464 ConstStr, ".str", M);
1465 StrGV->setSection("llvm.metadata");
1467 // Convert to generic string pointer.
1468 Slot = ConstantExpr::getBitCast(StrGV, getStrPtrType());
1475 /// Serialize - Recursively cast the specified descriptor into a GlobalVariable
1476 /// so that it can be serialized to a .bc or .ll file.
1477 GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
1478 // Check if the DebugInfoDesc is already in the map.
1479 GlobalVariable *&Slot = DescGlobals[DD];
1481 // See if DebugInfoDesc exists, if so return prior GlobalVariable.
1482 if (Slot) return Slot;
1484 // Get the type associated with the Tag.
1485 const StructType *Ty = getTagType(DD);
1487 // Create the GlobalVariable early to prevent infinite recursion.
1488 GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(),
1489 NULL, DD->getDescString(), M);
1490 GV->setSection("llvm.metadata");
1492 // Insert new GlobalVariable in DescGlobals map.
1495 // Set up elements vector
1496 std::vector<Constant*> Elements;
1498 DISerializeVisitor SRAM(*this, Elements);
1499 SRAM.ApplyToFields(DD);
1501 // Set the globals initializer.
1502 GV->setInitializer(ConstantStruct::get(Ty, Elements));
1507 /// addDescriptor - Directly connect DD with existing GV.
1508 void DISerializer::addDescriptor(DebugInfoDesc *DD,
1509 GlobalVariable *GV) {
1510 DescGlobals[DD] = GV;
1513 //===----------------------------------------------------------------------===//
1515 /// Verify - Return true if the GlobalVariable appears to be a valid
1516 /// serialization of a DebugInfoDesc.
1517 bool DIVerifier::Verify(Value *V) {
1518 return !V || Verify(getGlobalVariable(V));
1520 bool DIVerifier::Verify(GlobalVariable *GV) {
1522 if (!GV) return true;
1524 // Check prior validity.
1525 unsigned &ValiditySlot = Validity[GV];
1527 // If visited before then use old state.
1528 if (ValiditySlot) return ValiditySlot == Valid;
1530 // Assume validity for the time being (recursion.)
1531 ValiditySlot = Valid;
1533 // Make sure the global is internal or link once (anchor.)
1534 if (GV->getLinkage() != GlobalValue::InternalLinkage &&
1535 GV->getLinkage() != GlobalValue::LinkOnceLinkage) {
1536 ValiditySlot = Invalid;
1541 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1543 // Check for user defined descriptors.
1544 if (Tag == DW_TAG_invalid) {
1545 ValiditySlot = Valid;
1550 unsigned Version = DebugInfoDesc::VersionFromGlobal(GV);
1552 // Check for version mismatch.
1553 if (Version != LLVMDebugVersion) {
1554 ValiditySlot = Invalid;
1558 // Construct an empty DebugInfoDesc.
1559 DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
1561 // Allow for user defined descriptors.
1562 if (!DD) return true;
1564 // Get the initializer constant.
1565 ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
1567 // Get the operand count.
1568 unsigned N = CI->getNumOperands();
1570 // Get the field count.
1571 unsigned &CountSlot = Counts[Tag];
1574 // Check the operand count to the field count
1575 CountSlot = CountFields(DD);
1577 // Field count must be at most equal operand count.
1578 if (CountSlot > N) {
1580 ValiditySlot = Invalid;
1584 // Check each field for valid type.
1585 DIVerifyVisitor VRAM(*this, GV);
1586 VRAM.ApplyToFields(DD);
1588 // Release empty DebugInfoDesc.
1591 // If fields are not valid.
1592 if (!VRAM.isValid()) {
1593 ValiditySlot = Invalid;
1600 /// isVerified - Return true if the specified GV has already been
1601 /// verified as a debug information descriptor.
1602 bool DIVerifier::isVerified(GlobalVariable *GV) {
1603 unsigned &ValiditySlot = Validity[GV];
1604 if (ValiditySlot) return ValiditySlot == Valid;
1608 //===----------------------------------------------------------------------===//
1610 DebugScope::~DebugScope() {
1611 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1612 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
1615 //===----------------------------------------------------------------------===//
1617 MachineModuleInfo::MachineModuleInfo()
1618 : ImmutablePass(&ID)
1632 , CallsUnwindInit(0)
1634 // Always emit "no personality" info
1635 Personalities.push_back(NULL);
1637 MachineModuleInfo::~MachineModuleInfo() {
1641 /// doInitialization - Initialize the state for a new module.
1643 bool MachineModuleInfo::doInitialization() {
1647 /// doFinalization - Tear down the state after completion of a module.
1649 bool MachineModuleInfo::doFinalization() {
1653 /// BeginFunction - Begin gathering function meta information.
1655 void MachineModuleInfo::BeginFunction(MachineFunction *MF) {
1659 /// EndFunction - Discard function meta information.
1661 void MachineModuleInfo::EndFunction() {
1662 // Clean up scope information.
1669 // Clean up line info.
1672 // Clean up frame info.
1675 // Clean up exception info.
1676 LandingPads.clear();
1681 CallsUnwindInit = 0;
1684 /// getDescFor - Convert a Value to a debug information descriptor.
1686 // FIXME - use new Value type when available.
1687 DebugInfoDesc *MachineModuleInfo::getDescFor(Value *V) {
1688 return DR.Deserialize(V);
1691 /// AnalyzeModule - Scan the module for global debug information.
1693 void MachineModuleInfo::AnalyzeModule(Module &M) {
1694 SetupCompileUnits(M);
1696 // Insert functions in the llvm.used array into UsedFunctions.
1697 GlobalVariable *GV = M.getGlobalVariable("llvm.used");
1698 if (!GV || !GV->hasInitializer()) return;
1700 // Should be an array of 'i8*'.
1701 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
1702 if (InitList == 0) return;
1704 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
1705 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(InitList->getOperand(i)))
1706 if (CE->getOpcode() == Instruction::BitCast)
1707 if (Function *F = dyn_cast<Function>(CE->getOperand(0)))
1708 UsedFunctions.insert(F);
1712 /// SetupCompileUnits - Set up the unique vector of compile units.
1714 void MachineModuleInfo::SetupCompileUnits(Module &M) {
1715 std::vector<CompileUnitDesc *> CU;
1716 getAnchoredDescriptors<CompileUnitDesc>(M, CU);
1718 for (unsigned i = 0, N = CU.size(); i < N; i++) {
1719 CompileUnits.insert(CU[i]);
1723 /// getCompileUnits - Return a vector of debug compile units.
1725 const UniqueVector<CompileUnitDesc *> MachineModuleInfo::getCompileUnits()const{
1726 return CompileUnits;
1729 /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1730 /// named GlobalVariable.
1732 MachineModuleInfo::getGlobalVariablesUsing(Module &M,
1733 const std::string &RootName,
1734 std::vector<GlobalVariable*>&Result){
1735 return ::getGlobalVariablesUsing(M, RootName, Result);
1738 /// RecordSourceLine - Records location information and associates it with a
1739 /// debug label. Returns a unique label ID used to generate a label and
1740 /// provide correspondence to the source line list.
1741 unsigned MachineModuleInfo::RecordSourceLine(unsigned Line, unsigned Column,
1743 unsigned ID = NextLabelID();
1744 Lines.push_back(SourceLineInfo(Line, Column, Source, ID));
1748 /// RecordSource - Register a source file with debug info. Returns an source
1750 unsigned MachineModuleInfo::RecordSource(const std::string &Directory,
1751 const std::string &Source) {
1752 unsigned DirectoryID = Directories.insert(Directory);
1753 return SourceFiles.insert(SourceFileInfo(DirectoryID, Source));
1755 unsigned MachineModuleInfo::RecordSource(const CompileUnitDesc *CompileUnit) {
1756 return RecordSource(CompileUnit->getDirectory(),
1757 CompileUnit->getFileName());
1760 /// RecordRegionStart - Indicate the start of a region.
1762 unsigned MachineModuleInfo::RecordRegionStart(Value *V) {
1763 // FIXME - need to be able to handle split scopes because of bb cloning.
1764 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1765 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1766 unsigned ID = NextLabelID();
1767 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
1771 /// RecordRegionEnd - Indicate the end of a region.
1773 unsigned MachineModuleInfo::RecordRegionEnd(Value *V) {
1774 // FIXME - need to be able to handle split scopes because of bb cloning.
1775 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1776 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1777 unsigned ID = NextLabelID();
1778 Scope->setEndLabelID(ID);
1782 /// RecordVariable - Indicate the declaration of a local variable.
1784 void MachineModuleInfo::RecordVariable(GlobalValue *GV, unsigned FrameIndex) {
1785 VariableDesc *VD = cast<VariableDesc>(DR.Deserialize(GV));
1786 DebugScope *Scope = getOrCreateScope(VD->getContext());
1787 DebugVariable *DV = new DebugVariable(VD, FrameIndex);
1788 Scope->AddVariable(DV);
1791 /// getOrCreateScope - Returns the scope associated with the given descriptor.
1793 DebugScope *MachineModuleInfo::getOrCreateScope(DebugInfoDesc *ScopeDesc) {
1794 DebugScope *&Slot = ScopeMap[ScopeDesc];
1796 // FIXME - breaks down when the context is an inlined function.
1797 DebugInfoDesc *ParentDesc = NULL;
1798 if (BlockDesc *Block = dyn_cast<BlockDesc>(ScopeDesc)) {
1799 ParentDesc = Block->getContext();
1801 DebugScope *Parent = ParentDesc ? getOrCreateScope(ParentDesc) : NULL;
1802 Slot = new DebugScope(Parent, ScopeDesc);
1804 Parent->AddScope(Slot);
1805 } else if (RootScope) {
1806 // FIXME - Add inlined function scopes to the root so we can delete
1807 // them later. Long term, handle inlined functions properly.
1808 RootScope->AddScope(Slot);
1810 // First function is top level function.
1817 //===-EH-------------------------------------------------------------------===//
1819 /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
1820 /// specified MachineBasicBlock.
1821 LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo
1822 (MachineBasicBlock *LandingPad) {
1823 unsigned N = LandingPads.size();
1824 for (unsigned i = 0; i < N; ++i) {
1825 LandingPadInfo &LP = LandingPads[i];
1826 if (LP.LandingPadBlock == LandingPad)
1830 LandingPads.push_back(LandingPadInfo(LandingPad));
1831 return LandingPads[N];
1834 /// addInvoke - Provide the begin and end labels of an invoke style call and
1835 /// associate it with a try landing pad block.
1836 void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad,
1837 unsigned BeginLabel, unsigned EndLabel) {
1838 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1839 LP.BeginLabels.push_back(BeginLabel);
1840 LP.EndLabels.push_back(EndLabel);
1843 /// addLandingPad - Provide the label of a try LandingPad block.
1845 unsigned MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) {
1846 unsigned LandingPadLabel = NextLabelID();
1847 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1848 LP.LandingPadLabel = LandingPadLabel;
1849 return LandingPadLabel;
1852 /// addPersonality - Provide the personality function for the exception
1854 void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad,
1855 Function *Personality) {
1856 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1857 LP.Personality = Personality;
1859 for (unsigned i = 0; i < Personalities.size(); ++i)
1860 if (Personalities[i] == Personality)
1863 Personalities.push_back(Personality);
1866 /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
1868 void MachineModuleInfo::addCatchTypeInfo(MachineBasicBlock *LandingPad,
1869 std::vector<GlobalVariable *> &TyInfo) {
1870 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1871 for (unsigned N = TyInfo.size(); N; --N)
1872 LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
1875 /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
1877 void MachineModuleInfo::addFilterTypeInfo(MachineBasicBlock *LandingPad,
1878 std::vector<GlobalVariable *> &TyInfo) {
1879 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1880 std::vector<unsigned> IdsInFilter(TyInfo.size());
1881 for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
1882 IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
1883 LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
1886 /// addCleanup - Add a cleanup action for a landing pad.
1888 void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) {
1889 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1890 LP.TypeIds.push_back(0);
1893 /// TidyLandingPads - Remap landing pad labels and remove any deleted landing
1895 void MachineModuleInfo::TidyLandingPads() {
1896 for (unsigned i = 0; i != LandingPads.size(); ) {
1897 LandingPadInfo &LandingPad = LandingPads[i];
1898 LandingPad.LandingPadLabel = MappedLabel(LandingPad.LandingPadLabel);
1900 // Special case: we *should* emit LPs with null LP MBB. This indicates
1902 if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
1903 LandingPads.erase(LandingPads.begin() + i);
1907 for (unsigned j=0; j != LandingPads[i].BeginLabels.size(); ) {
1908 unsigned BeginLabel = MappedLabel(LandingPad.BeginLabels[j]);
1909 unsigned EndLabel = MappedLabel(LandingPad.EndLabels[j]);
1911 if (!BeginLabel || !EndLabel) {
1912 LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
1913 LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
1917 LandingPad.BeginLabels[j] = BeginLabel;
1918 LandingPad.EndLabels[j] = EndLabel;
1922 // Remove landing pads with no try-ranges.
1923 if (LandingPads[i].BeginLabels.empty()) {
1924 LandingPads.erase(LandingPads.begin() + i);
1928 // If there is no landing pad, ensure that the list of typeids is empty.
1929 // If the only typeid is a cleanup, this is the same as having no typeids.
1930 if (!LandingPad.LandingPadBlock ||
1931 (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
1932 LandingPad.TypeIds.clear();
1938 /// getTypeIDFor - Return the type id for the specified typeinfo. This is
1940 unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) {
1941 for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
1942 if (TypeInfos[i] == TI) return i + 1;
1944 TypeInfos.push_back(TI);
1945 return TypeInfos.size();
1948 /// getFilterIDFor - Return the filter id for the specified typeinfos. This is
1950 int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) {
1951 // If the new filter coincides with the tail of an existing filter, then
1952 // re-use the existing filter. Folding filters more than this requires
1953 // re-ordering filters and/or their elements - probably not worth it.
1954 for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
1955 E = FilterEnds.end(); I != E; ++I) {
1956 unsigned i = *I, j = TyIds.size();
1959 if (FilterIds[--i] != TyIds[--j])
1963 // The new filter coincides with range [i, end) of the existing filter.
1969 // Add the new filter.
1970 int FilterID = -(1 + FilterIds.size());
1971 FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
1972 for (unsigned I = 0, N = TyIds.size(); I != N; ++I)
1973 FilterIds.push_back(TyIds[I]);
1974 FilterEnds.push_back(FilterIds.size());
1975 FilterIds.push_back(0); // terminator
1979 /// getPersonality - Return the personality function for the current function.
1980 Function *MachineModuleInfo::getPersonality() const {
1981 // FIXME: Until PR1414 will be fixed, we're using 1 personality function per
1983 return !LandingPads.empty() ? LandingPads[0].Personality : NULL;
1986 /// getPersonalityIndex - Return unique index for current personality
1987 /// function. NULL personality function should always get zero index.
1988 unsigned MachineModuleInfo::getPersonalityIndex() const {
1989 const Function* Personality = NULL;
1991 // Scan landing pads. If there is at least one non-NULL personality - use it.
1992 for (unsigned i = 0; i != LandingPads.size(); ++i)
1993 if (LandingPads[i].Personality) {
1994 Personality = LandingPads[i].Personality;
1998 for (unsigned i = 0; i < Personalities.size(); ++i) {
1999 if (Personalities[i] == Personality)
2003 // This should never happen
2004 assert(0 && "Personality function should be set!");
2008 //===----------------------------------------------------------------------===//
2009 /// DebugLabelFolding pass - This pass prunes out redundant labels. This allows
2010 /// a info consumer to determine if the range of two labels is empty, by seeing
2011 /// if the labels map to the same reduced label.
2015 struct DebugLabelFolder : public MachineFunctionPass {
2017 DebugLabelFolder() : MachineFunctionPass(&ID) {}
2019 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
2020 AU.addPreservedID(MachineLoopInfoID);
2021 AU.addPreservedID(MachineDominatorsID);
2022 MachineFunctionPass::getAnalysisUsage(AU);
2025 virtual bool runOnMachineFunction(MachineFunction &MF);
2026 virtual const char *getPassName() const { return "Label Folder"; }
2029 char DebugLabelFolder::ID = 0;
2031 bool DebugLabelFolder::runOnMachineFunction(MachineFunction &MF) {
2032 // Get machine module info.
2033 MachineModuleInfo *MMI = getAnalysisToUpdate<MachineModuleInfo>();
2034 if (!MMI) return false;
2036 // Track if change is made.
2037 bool MadeChange = false;
2038 // No prior label to begin.
2039 unsigned PriorLabel = 0;
2041 // Iterate through basic blocks.
2042 for (MachineFunction::iterator BB = MF.begin(), E = MF.end();
2044 // Iterate through instructions.
2045 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
2047 if (I->isDebugLabel()) {
2048 // The label ID # is always operand #0, an immediate.
2049 unsigned NextLabel = I->getOperand(0).getImm();
2051 // If there was an immediate prior label.
2053 // Remap the current label to prior label.
2054 MMI->RemapLabel(NextLabel, PriorLabel);
2055 // Delete the current label.
2057 // Indicate a change has been made.
2061 // Start a new round.
2062 PriorLabel = NextLabel;
2065 // No consecutive labels.
2076 FunctionPass *createDebugLabelFoldingPass() { return new DebugLabelFolder(); }