1 //===-- llvm/CodeGen/MachineModuleInfo.cpp ----------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by James M. Laskey and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/CodeGen/MachineModuleInfo.h"
12 #include "llvm/Constants.h"
13 #include "llvm/CodeGen/MachineFunctionPass.h"
14 #include "llvm/CodeGen/MachineFunction.h"
15 #include "llvm/CodeGen/MachineLocation.h"
16 #include "llvm/Target/TargetInstrInfo.h"
17 #include "llvm/Target/TargetMachine.h"
18 #include "llvm/Target/TargetOptions.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/GlobalVariable.h"
21 #include "llvm/Intrinsics.h"
22 #include "llvm/Instructions.h"
23 #include "llvm/Module.h"
24 #include "llvm/Support/Dwarf.h"
25 #include "llvm/Support/Streams.h"
27 using namespace llvm::dwarf;
29 // Handle the Pass registration stuff necessary to use TargetData's.
31 RegisterPass<MachineModuleInfo> X("machinemoduleinfo", "Module Information");
34 //===----------------------------------------------------------------------===//
36 /// getGlobalVariablesUsing - Return all of the GlobalVariables which have the
37 /// specified value in their initializer somewhere.
39 getGlobalVariablesUsing(Value *V, std::vector<GlobalVariable*> &Result) {
40 // Scan though value users.
41 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
42 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I)) {
43 // If the user is a GlobalVariable then add to result.
45 } else if (Constant *C = dyn_cast<Constant>(*I)) {
46 // If the user is a constant variable then scan its users
47 getGlobalVariablesUsing(C, Result);
52 /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
53 /// named GlobalVariable.
54 static std::vector<GlobalVariable*>
55 getGlobalVariablesUsing(Module &M, const std::string &RootName) {
56 std::vector<GlobalVariable*> Result; // GlobalVariables matching criteria.
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);
74 /// isStringValue - Return true if the given value can be coerced to a string.
76 static bool isStringValue(Value *V) {
77 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
78 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
79 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
80 return Init->isString();
82 } else if (Constant *C = dyn_cast<Constant>(V)) {
83 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
84 return isStringValue(GV);
85 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
86 if (CE->getOpcode() == Instruction::GetElementPtr) {
87 if (CE->getNumOperands() == 3 &&
88 cast<Constant>(CE->getOperand(1))->isNullValue() &&
89 isa<ConstantInt>(CE->getOperand(2))) {
90 return isStringValue(CE->getOperand(0));
98 /// getGlobalVariable - Return either a direct or cast Global value.
100 static GlobalVariable *getGlobalVariable(Value *V) {
101 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
103 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
104 if (CE->getOpcode() == Instruction::BitCast) {
105 return dyn_cast<GlobalVariable>(CE->getOperand(0));
111 /// isGlobalVariable - Return true if the given value can be coerced to a
113 static bool isGlobalVariable(Value *V) {
114 if (isa<GlobalVariable>(V) || isa<ConstantPointerNull>(V)) {
116 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
117 if (CE->getOpcode() == Instruction::BitCast) {
118 return isa<GlobalVariable>(CE->getOperand(0));
124 /// getUIntOperand - Return ith operand if it is an unsigned integer.
126 static ConstantInt *getUIntOperand(GlobalVariable *GV, unsigned i) {
127 // Make sure the GlobalVariable has an initializer.
128 if (!GV->hasInitializer()) return NULL;
130 // Get the initializer constant.
131 ConstantStruct *CI = dyn_cast<ConstantStruct>(GV->getInitializer());
132 if (!CI) return NULL;
134 // Check if there is at least i + 1 operands.
135 unsigned N = CI->getNumOperands();
136 if (i >= N) return NULL;
139 return dyn_cast<ConstantInt>(CI->getOperand(i));
142 //===----------------------------------------------------------------------===//
144 /// ApplyToFields - Target the visitor to each field of the debug information
146 void DIVisitor::ApplyToFields(DebugInfoDesc *DD) {
147 DD->ApplyToFields(this);
150 //===----------------------------------------------------------------------===//
151 /// DICountVisitor - This DIVisitor counts all the fields in the supplied debug
152 /// the supplied DebugInfoDesc.
153 class DICountVisitor : public DIVisitor {
155 unsigned Count; // Running count of fields.
158 DICountVisitor() : DIVisitor(), Count(0) {}
161 unsigned getCount() const { return Count; }
163 /// Apply - Count each of the fields.
165 virtual void Apply(int &Field) { ++Count; }
166 virtual void Apply(unsigned &Field) { ++Count; }
167 virtual void Apply(int64_t &Field) { ++Count; }
168 virtual void Apply(uint64_t &Field) { ++Count; }
169 virtual void Apply(bool &Field) { ++Count; }
170 virtual void Apply(std::string &Field) { ++Count; }
171 virtual void Apply(DebugInfoDesc *&Field) { ++Count; }
172 virtual void Apply(GlobalVariable *&Field) { ++Count; }
173 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
178 //===----------------------------------------------------------------------===//
179 /// DIDeserializeVisitor - This DIVisitor deserializes all the fields in the
180 /// supplied DebugInfoDesc.
181 class DIDeserializeVisitor : public DIVisitor {
183 DIDeserializer &DR; // Active deserializer.
184 unsigned I; // Current operand index.
185 ConstantStruct *CI; // GlobalVariable constant initializer.
188 DIDeserializeVisitor(DIDeserializer &D, GlobalVariable *GV)
192 , CI(cast<ConstantStruct>(GV->getInitializer()))
195 /// Apply - Set the value of each of the fields.
197 virtual void Apply(int &Field) {
198 Constant *C = CI->getOperand(I++);
199 Field = cast<ConstantInt>(C)->getSExtValue();
201 virtual void Apply(unsigned &Field) {
202 Constant *C = CI->getOperand(I++);
203 Field = cast<ConstantInt>(C)->getZExtValue();
205 virtual void Apply(int64_t &Field) {
206 Constant *C = CI->getOperand(I++);
207 Field = cast<ConstantInt>(C)->getSExtValue();
209 virtual void Apply(uint64_t &Field) {
210 Constant *C = CI->getOperand(I++);
211 Field = cast<ConstantInt>(C)->getZExtValue();
213 virtual void Apply(bool &Field) {
214 Constant *C = CI->getOperand(I++);
215 Field = cast<ConstantInt>(C)->getZExtValue();
217 virtual void Apply(std::string &Field) {
218 Constant *C = CI->getOperand(I++);
219 Field = C->getStringValue();
221 virtual void Apply(DebugInfoDesc *&Field) {
222 Constant *C = CI->getOperand(I++);
223 Field = DR.Deserialize(C);
225 virtual void Apply(GlobalVariable *&Field) {
226 Constant *C = CI->getOperand(I++);
227 Field = getGlobalVariable(C);
229 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
231 Constant *C = CI->getOperand(I++);
232 GlobalVariable *GV = getGlobalVariable(C);
233 if (GV->hasInitializer()) {
234 if (ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer())) {
235 for (unsigned i = 0, N = CA->getNumOperands(); i < N; ++i) {
236 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
237 DebugInfoDesc *DE = DR.Deserialize(GVE);
240 } else if (GV->getInitializer()->isNullValue()) {
241 if (const ArrayType *T =
242 dyn_cast<ArrayType>(GV->getType()->getElementType())) {
243 Field.resize(T->getNumElements());
250 //===----------------------------------------------------------------------===//
251 /// DISerializeVisitor - This DIVisitor serializes all the fields in
252 /// the supplied DebugInfoDesc.
253 class DISerializeVisitor : public DIVisitor {
255 DISerializer &SR; // Active serializer.
256 std::vector<Constant*> &Elements; // Element accumulator.
259 DISerializeVisitor(DISerializer &S, std::vector<Constant*> &E)
265 /// Apply - Set the value of each of the fields.
267 virtual void Apply(int &Field) {
268 Elements.push_back(ConstantInt::get(Type::Int32Ty, int32_t(Field)));
270 virtual void Apply(unsigned &Field) {
271 Elements.push_back(ConstantInt::get(Type::Int32Ty, uint32_t(Field)));
273 virtual void Apply(int64_t &Field) {
274 Elements.push_back(ConstantInt::get(Type::Int64Ty, int64_t(Field)));
276 virtual void Apply(uint64_t &Field) {
277 Elements.push_back(ConstantInt::get(Type::Int64Ty, uint64_t(Field)));
279 virtual void Apply(bool &Field) {
280 Elements.push_back(ConstantInt::get(Type::Int1Ty, Field));
282 virtual void Apply(std::string &Field) {
283 Elements.push_back(SR.getString(Field));
285 virtual void Apply(DebugInfoDesc *&Field) {
286 GlobalVariable *GV = NULL;
288 // If non-NULL then convert to global.
289 if (Field) GV = SR.Serialize(Field);
291 // FIXME - At some point should use specific type.
292 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
295 // Set to pointer to global.
296 Elements.push_back(ConstantExpr::getBitCast(GV, EmptyTy));
299 Elements.push_back(ConstantPointerNull::get(EmptyTy));
302 virtual void Apply(GlobalVariable *&Field) {
303 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
305 Elements.push_back(ConstantExpr::getBitCast(Field, EmptyTy));
307 Elements.push_back(ConstantPointerNull::get(EmptyTy));
310 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
311 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
312 unsigned N = Field.size();
313 ArrayType *AT = ArrayType::get(EmptyTy, N);
314 std::vector<Constant *> ArrayElements;
316 for (unsigned i = 0, N = Field.size(); i < N; ++i) {
317 if (DebugInfoDesc *Element = Field[i]) {
318 GlobalVariable *GVE = SR.Serialize(Element);
319 Constant *CE = ConstantExpr::getBitCast(GVE, EmptyTy);
320 ArrayElements.push_back(cast<Constant>(CE));
322 ArrayElements.push_back(ConstantPointerNull::get(EmptyTy));
326 Constant *CA = ConstantArray::get(AT, ArrayElements);
327 GlobalVariable *CAGV = new GlobalVariable(AT, true,
328 GlobalValue::InternalLinkage,
329 CA, "llvm.dbg.array",
331 CAGV->setSection("llvm.metadata");
332 Constant *CAE = ConstantExpr::getBitCast(CAGV, EmptyTy);
333 Elements.push_back(CAE);
337 //===----------------------------------------------------------------------===//
338 /// DIGetTypesVisitor - This DIVisitor gathers all the field types in
339 /// the supplied DebugInfoDesc.
340 class DIGetTypesVisitor : public DIVisitor {
342 DISerializer &SR; // Active serializer.
343 std::vector<const Type*> &Fields; // Type accumulator.
346 DIGetTypesVisitor(DISerializer &S, std::vector<const Type*> &F)
352 /// Apply - Set the value of each of the fields.
354 virtual void Apply(int &Field) {
355 Fields.push_back(Type::Int32Ty);
357 virtual void Apply(unsigned &Field) {
358 Fields.push_back(Type::Int32Ty);
360 virtual void Apply(int64_t &Field) {
361 Fields.push_back(Type::Int64Ty);
363 virtual void Apply(uint64_t &Field) {
364 Fields.push_back(Type::Int64Ty);
366 virtual void Apply(bool &Field) {
367 Fields.push_back(Type::Int1Ty);
369 virtual void Apply(std::string &Field) {
370 Fields.push_back(SR.getStrPtrType());
372 virtual void Apply(DebugInfoDesc *&Field) {
373 // FIXME - At some point should use specific type.
374 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
375 Fields.push_back(EmptyTy);
377 virtual void Apply(GlobalVariable *&Field) {
378 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
379 Fields.push_back(EmptyTy);
381 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
382 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
383 Fields.push_back(EmptyTy);
387 //===----------------------------------------------------------------------===//
388 /// DIVerifyVisitor - This DIVisitor verifies all the field types against
389 /// a constant initializer.
390 class DIVerifyVisitor : public DIVisitor {
392 DIVerifier &VR; // Active verifier.
393 bool IsValid; // Validity status.
394 unsigned I; // Current operand index.
395 ConstantStruct *CI; // GlobalVariable constant initializer.
398 DIVerifyVisitor(DIVerifier &V, GlobalVariable *GV)
403 , CI(cast<ConstantStruct>(GV->getInitializer()))
408 bool isValid() const { return IsValid; }
410 /// Apply - Set the value of each of the fields.
412 virtual void Apply(int &Field) {
413 Constant *C = CI->getOperand(I++);
414 IsValid = IsValid && isa<ConstantInt>(C);
416 virtual void Apply(unsigned &Field) {
417 Constant *C = CI->getOperand(I++);
418 IsValid = IsValid && isa<ConstantInt>(C);
420 virtual void Apply(int64_t &Field) {
421 Constant *C = CI->getOperand(I++);
422 IsValid = IsValid && isa<ConstantInt>(C);
424 virtual void Apply(uint64_t &Field) {
425 Constant *C = CI->getOperand(I++);
426 IsValid = IsValid && isa<ConstantInt>(C);
428 virtual void Apply(bool &Field) {
429 Constant *C = CI->getOperand(I++);
430 IsValid = IsValid && isa<ConstantInt>(C) && C->getType() == Type::Int1Ty;
432 virtual void Apply(std::string &Field) {
433 Constant *C = CI->getOperand(I++);
435 (!C || isStringValue(C) || C->isNullValue());
437 virtual void Apply(DebugInfoDesc *&Field) {
438 // FIXME - Prepare the correct descriptor.
439 Constant *C = CI->getOperand(I++);
440 IsValid = IsValid && isGlobalVariable(C);
442 virtual void Apply(GlobalVariable *&Field) {
443 Constant *C = CI->getOperand(I++);
444 IsValid = IsValid && isGlobalVariable(C);
446 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
447 Constant *C = CI->getOperand(I++);
448 IsValid = IsValid && isGlobalVariable(C);
449 if (!IsValid) return;
451 GlobalVariable *GV = getGlobalVariable(C);
452 IsValid = IsValid && GV && GV->hasInitializer();
453 if (!IsValid) return;
455 ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer());
456 IsValid = IsValid && CA;
457 if (!IsValid) return;
459 for (unsigned i = 0, N = CA->getNumOperands(); IsValid && i < N; ++i) {
460 IsValid = IsValid && isGlobalVariable(CA->getOperand(i));
461 if (!IsValid) return;
463 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
470 //===----------------------------------------------------------------------===//
472 /// TagFromGlobal - Returns the tag number from a debug info descriptor
473 /// GlobalVariable. Return DIIValid if operand is not an unsigned int.
474 unsigned DebugInfoDesc::TagFromGlobal(GlobalVariable *GV) {
475 ConstantInt *C = getUIntOperand(GV, 0);
476 return C ? ((unsigned)C->getZExtValue() & ~LLVMDebugVersionMask) :
477 (unsigned)DW_TAG_invalid;
480 /// VersionFromGlobal - Returns the version number from a debug info
481 /// descriptor GlobalVariable. Return DIIValid if operand is not an unsigned
483 unsigned DebugInfoDesc::VersionFromGlobal(GlobalVariable *GV) {
484 ConstantInt *C = getUIntOperand(GV, 0);
485 return C ? ((unsigned)C->getZExtValue() & LLVMDebugVersionMask) :
486 (unsigned)DW_TAG_invalid;
489 /// DescFactory - Create an instance of debug info descriptor based on Tag.
490 /// Return NULL if not a recognized Tag.
491 DebugInfoDesc *DebugInfoDesc::DescFactory(unsigned Tag) {
493 case DW_TAG_anchor: return new AnchorDesc();
494 case DW_TAG_compile_unit: return new CompileUnitDesc();
495 case DW_TAG_variable: return new GlobalVariableDesc();
496 case DW_TAG_subprogram: return new SubprogramDesc();
497 case DW_TAG_lexical_block: return new BlockDesc();
498 case DW_TAG_base_type: return new BasicTypeDesc();
500 case DW_TAG_pointer_type:
501 case DW_TAG_reference_type:
502 case DW_TAG_const_type:
503 case DW_TAG_volatile_type:
504 case DW_TAG_restrict_type:
506 case DW_TAG_inheritance: return new DerivedTypeDesc(Tag);
507 case DW_TAG_array_type:
508 case DW_TAG_structure_type:
509 case DW_TAG_union_type:
510 case DW_TAG_enumeration_type:
511 case DW_TAG_vector_type:
512 case DW_TAG_subroutine_type: return new CompositeTypeDesc(Tag);
513 case DW_TAG_subrange_type: return new SubrangeDesc();
514 case DW_TAG_enumerator: return new EnumeratorDesc();
515 case DW_TAG_return_variable:
516 case DW_TAG_arg_variable:
517 case DW_TAG_auto_variable: return new VariableDesc(Tag);
523 /// getLinkage - get linkage appropriate for this type of descriptor.
525 GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const {
526 return GlobalValue::InternalLinkage;
529 /// ApplyToFields - Target the vistor to the fields of the descriptor.
531 void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) {
535 //===----------------------------------------------------------------------===//
537 AnchorDesc::AnchorDesc()
538 : DebugInfoDesc(DW_TAG_anchor)
541 AnchorDesc::AnchorDesc(AnchoredDesc *D)
542 : DebugInfoDesc(DW_TAG_anchor)
543 , AnchorTag(D->getTag())
546 // Implement isa/cast/dyncast.
547 bool AnchorDesc::classof(const DebugInfoDesc *D) {
548 return D->getTag() == DW_TAG_anchor;
551 /// getLinkage - get linkage appropriate for this type of descriptor.
553 GlobalValue::LinkageTypes AnchorDesc::getLinkage() const {
554 return GlobalValue::LinkOnceLinkage;
557 /// ApplyToFields - Target the visitor to the fields of the TransUnitDesc.
559 void AnchorDesc::ApplyToFields(DIVisitor *Visitor) {
560 DebugInfoDesc::ApplyToFields(Visitor);
562 Visitor->Apply(AnchorTag);
565 /// getDescString - Return a string used to compose global names and labels. A
566 /// A global variable name needs to be defined for each debug descriptor that is
567 /// anchored. NOTE: that each global variable named here also needs to be added
568 /// to the list of names left external in the internalizer.
569 /// ExternalNames.insert("llvm.dbg.compile_units");
570 /// ExternalNames.insert("llvm.dbg.global_variables");
571 /// ExternalNames.insert("llvm.dbg.subprograms");
572 const char *AnchorDesc::getDescString() const {
574 case DW_TAG_compile_unit: return CompileUnitDesc::AnchorString;
575 case DW_TAG_variable: return GlobalVariableDesc::AnchorString;
576 case DW_TAG_subprogram: return SubprogramDesc::AnchorString;
580 assert(0 && "Tag does not have a case for anchor string");
584 /// getTypeString - Return a string used to label this descriptors type.
586 const char *AnchorDesc::getTypeString() const {
587 return "llvm.dbg.anchor.type";
591 void AnchorDesc::dump() {
592 cerr << getDescString() << " "
593 << "Version(" << getVersion() << "), "
594 << "Tag(" << getTag() << "), "
595 << "AnchorTag(" << AnchorTag << ")\n";
599 //===----------------------------------------------------------------------===//
601 AnchoredDesc::AnchoredDesc(unsigned T)
606 /// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
608 void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) {
609 DebugInfoDesc::ApplyToFields(Visitor);
611 Visitor->Apply(Anchor);
614 //===----------------------------------------------------------------------===//
616 CompileUnitDesc::CompileUnitDesc()
617 : AnchoredDesc(DW_TAG_compile_unit)
624 // Implement isa/cast/dyncast.
625 bool CompileUnitDesc::classof(const DebugInfoDesc *D) {
626 return D->getTag() == DW_TAG_compile_unit;
629 /// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
631 void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) {
632 AnchoredDesc::ApplyToFields(Visitor);
634 // Handle cases out of sync with compiler.
635 if (getVersion() == 0) {
636 unsigned DebugVersion;
637 Visitor->Apply(DebugVersion);
640 Visitor->Apply(Language);
641 Visitor->Apply(FileName);
642 Visitor->Apply(Directory);
643 Visitor->Apply(Producer);
646 /// getDescString - Return a string used to compose global names and labels.
648 const char *CompileUnitDesc::getDescString() const {
649 return "llvm.dbg.compile_unit";
652 /// getTypeString - Return a string used to label this descriptors type.
654 const char *CompileUnitDesc::getTypeString() const {
655 return "llvm.dbg.compile_unit.type";
658 /// getAnchorString - Return a string used to label this descriptor's anchor.
660 const char *CompileUnitDesc::AnchorString = "llvm.dbg.compile_units";
661 const char *CompileUnitDesc::getAnchorString() const {
666 void CompileUnitDesc::dump() {
667 cerr << getDescString() << " "
668 << "Version(" << getVersion() << "), "
669 << "Tag(" << getTag() << "), "
670 << "Anchor(" << getAnchor() << "), "
671 << "Language(" << Language << "), "
672 << "FileName(\"" << FileName << "\"), "
673 << "Directory(\"" << Directory << "\"), "
674 << "Producer(\"" << Producer << "\")\n";
678 //===----------------------------------------------------------------------===//
680 TypeDesc::TypeDesc(unsigned T)
692 /// ApplyToFields - Target the visitor to the fields of the TypeDesc.
694 void TypeDesc::ApplyToFields(DIVisitor *Visitor) {
695 DebugInfoDesc::ApplyToFields(Visitor);
697 Visitor->Apply(Context);
698 Visitor->Apply(Name);
699 Visitor->Apply(File);
700 Visitor->Apply(Line);
701 Visitor->Apply(Size);
702 Visitor->Apply(Align);
703 Visitor->Apply(Offset);
704 if (getVersion() > LLVMDebugVersion4) Visitor->Apply(Flags);
707 /// getDescString - Return a string used to compose global names and labels.
709 const char *TypeDesc::getDescString() const {
710 return "llvm.dbg.type";
713 /// getTypeString - Return a string used to label this descriptor's type.
715 const char *TypeDesc::getTypeString() const {
716 return "llvm.dbg.type.type";
720 void TypeDesc::dump() {
721 cerr << getDescString() << " "
722 << "Version(" << getVersion() << "), "
723 << "Tag(" << getTag() << "), "
724 << "Context(" << Context << "), "
725 << "Name(\"" << Name << "\"), "
726 << "File(" << File << "), "
727 << "Line(" << Line << "), "
728 << "Size(" << Size << "), "
729 << "Align(" << Align << "), "
730 << "Offset(" << Offset << "), "
731 << "Flags(" << Flags << ")\n";
735 //===----------------------------------------------------------------------===//
737 BasicTypeDesc::BasicTypeDesc()
738 : TypeDesc(DW_TAG_base_type)
742 // Implement isa/cast/dyncast.
743 bool BasicTypeDesc::classof(const DebugInfoDesc *D) {
744 return D->getTag() == DW_TAG_base_type;
747 /// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
749 void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
750 TypeDesc::ApplyToFields(Visitor);
752 Visitor->Apply(Encoding);
755 /// getDescString - Return a string used to compose global names and labels.
757 const char *BasicTypeDesc::getDescString() const {
758 return "llvm.dbg.basictype";
761 /// getTypeString - Return a string used to label this descriptor's type.
763 const char *BasicTypeDesc::getTypeString() const {
764 return "llvm.dbg.basictype.type";
768 void BasicTypeDesc::dump() {
769 cerr << getDescString() << " "
770 << "Version(" << getVersion() << "), "
771 << "Tag(" << getTag() << "), "
772 << "Context(" << getContext() << "), "
773 << "Name(\"" << getName() << "\"), "
774 << "Size(" << getSize() << "), "
775 << "Encoding(" << Encoding << ")\n";
779 //===----------------------------------------------------------------------===//
781 DerivedTypeDesc::DerivedTypeDesc(unsigned T)
786 // Implement isa/cast/dyncast.
787 bool DerivedTypeDesc::classof(const DebugInfoDesc *D) {
788 unsigned T = D->getTag();
791 case DW_TAG_pointer_type:
792 case DW_TAG_reference_type:
793 case DW_TAG_const_type:
794 case DW_TAG_volatile_type:
795 case DW_TAG_restrict_type:
797 case DW_TAG_inheritance:
804 /// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
806 void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) {
807 TypeDesc::ApplyToFields(Visitor);
809 Visitor->Apply(FromType);
812 /// getDescString - Return a string used to compose global names and labels.
814 const char *DerivedTypeDesc::getDescString() const {
815 return "llvm.dbg.derivedtype";
818 /// getTypeString - Return a string used to label this descriptor's type.
820 const char *DerivedTypeDesc::getTypeString() const {
821 return "llvm.dbg.derivedtype.type";
825 void DerivedTypeDesc::dump() {
826 cerr << getDescString() << " "
827 << "Version(" << getVersion() << "), "
828 << "Tag(" << getTag() << "), "
829 << "Context(" << getContext() << "), "
830 << "Name(\"" << getName() << "\"), "
831 << "Size(" << getSize() << "), "
832 << "File(" << getFile() << "), "
833 << "Line(" << getLine() << "), "
834 << "FromType(" << FromType << ")\n";
838 //===----------------------------------------------------------------------===//
840 CompositeTypeDesc::CompositeTypeDesc(unsigned T)
845 // Implement isa/cast/dyncast.
846 bool CompositeTypeDesc::classof(const DebugInfoDesc *D) {
847 unsigned T = D->getTag();
849 case DW_TAG_array_type:
850 case DW_TAG_structure_type:
851 case DW_TAG_union_type:
852 case DW_TAG_enumeration_type:
853 case DW_TAG_vector_type:
854 case DW_TAG_subroutine_type:
861 /// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
863 void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) {
864 DerivedTypeDesc::ApplyToFields(Visitor);
866 Visitor->Apply(Elements);
869 /// getDescString - Return a string used to compose global names and labels.
871 const char *CompositeTypeDesc::getDescString() const {
872 return "llvm.dbg.compositetype";
875 /// getTypeString - Return a string used to label this descriptor's type.
877 const char *CompositeTypeDesc::getTypeString() const {
878 return "llvm.dbg.compositetype.type";
882 void CompositeTypeDesc::dump() {
883 cerr << getDescString() << " "
884 << "Version(" << getVersion() << "), "
885 << "Tag(" << getTag() << "), "
886 << "Context(" << getContext() << "), "
887 << "Name(\"" << getName() << "\"), "
888 << "Size(" << getSize() << "), "
889 << "File(" << getFile() << "), "
890 << "Line(" << getLine() << "), "
891 << "FromType(" << getFromType() << "), "
892 << "Elements.size(" << Elements.size() << ")\n";
896 //===----------------------------------------------------------------------===//
898 SubrangeDesc::SubrangeDesc()
899 : DebugInfoDesc(DW_TAG_subrange_type)
904 // Implement isa/cast/dyncast.
905 bool SubrangeDesc::classof(const DebugInfoDesc *D) {
906 return D->getTag() == DW_TAG_subrange_type;
909 /// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
911 void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) {
912 DebugInfoDesc::ApplyToFields(Visitor);
918 /// getDescString - Return a string used to compose global names and labels.
920 const char *SubrangeDesc::getDescString() const {
921 return "llvm.dbg.subrange";
924 /// getTypeString - Return a string used to label this descriptor's type.
926 const char *SubrangeDesc::getTypeString() const {
927 return "llvm.dbg.subrange.type";
931 void SubrangeDesc::dump() {
932 cerr << getDescString() << " "
933 << "Version(" << getVersion() << "), "
934 << "Tag(" << getTag() << "), "
935 << "Lo(" << Lo << "), "
936 << "Hi(" << Hi << ")\n";
940 //===----------------------------------------------------------------------===//
942 EnumeratorDesc::EnumeratorDesc()
943 : DebugInfoDesc(DW_TAG_enumerator)
948 // Implement isa/cast/dyncast.
949 bool EnumeratorDesc::classof(const DebugInfoDesc *D) {
950 return D->getTag() == DW_TAG_enumerator;
953 /// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
955 void EnumeratorDesc::ApplyToFields(DIVisitor *Visitor) {
956 DebugInfoDesc::ApplyToFields(Visitor);
958 Visitor->Apply(Name);
959 Visitor->Apply(Value);
962 /// getDescString - Return a string used to compose global names and labels.
964 const char *EnumeratorDesc::getDescString() const {
965 return "llvm.dbg.enumerator";
968 /// getTypeString - Return a string used to label this descriptor's type.
970 const char *EnumeratorDesc::getTypeString() const {
971 return "llvm.dbg.enumerator.type";
975 void EnumeratorDesc::dump() {
976 cerr << getDescString() << " "
977 << "Version(" << getVersion() << "), "
978 << "Tag(" << getTag() << "), "
979 << "Name(" << Name << "), "
980 << "Value(" << Value << ")\n";
984 //===----------------------------------------------------------------------===//
986 VariableDesc::VariableDesc(unsigned T)
995 // Implement isa/cast/dyncast.
996 bool VariableDesc::classof(const DebugInfoDesc *D) {
997 unsigned T = D->getTag();
999 case DW_TAG_auto_variable:
1000 case DW_TAG_arg_variable:
1001 case DW_TAG_return_variable:
1008 /// ApplyToFields - Target the visitor to the fields of the VariableDesc.
1010 void VariableDesc::ApplyToFields(DIVisitor *Visitor) {
1011 DebugInfoDesc::ApplyToFields(Visitor);
1013 Visitor->Apply(Context);
1014 Visitor->Apply(Name);
1015 Visitor->Apply(File);
1016 Visitor->Apply(Line);
1017 Visitor->Apply(TyDesc);
1020 /// getDescString - Return a string used to compose global names and labels.
1022 const char *VariableDesc::getDescString() const {
1023 return "llvm.dbg.variable";
1026 /// getTypeString - Return a string used to label this descriptor's type.
1028 const char *VariableDesc::getTypeString() const {
1029 return "llvm.dbg.variable.type";
1033 void VariableDesc::dump() {
1034 cerr << getDescString() << " "
1035 << "Version(" << getVersion() << "), "
1036 << "Tag(" << getTag() << "), "
1037 << "Context(" << Context << "), "
1038 << "Name(\"" << Name << "\"), "
1039 << "File(" << File << "), "
1040 << "Line(" << Line << "), "
1041 << "TyDesc(" << TyDesc << ")\n";
1045 //===----------------------------------------------------------------------===//
1047 GlobalDesc::GlobalDesc(unsigned T)
1057 , IsDefinition(false)
1060 /// ApplyToFields - Target the visitor to the fields of the global.
1062 void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
1063 AnchoredDesc::ApplyToFields(Visitor);
1065 Visitor->Apply(Context);
1066 Visitor->Apply(Name);
1067 Visitor->Apply(FullName);
1068 Visitor->Apply(LinkageName);
1069 Visitor->Apply(File);
1070 Visitor->Apply(Line);
1071 Visitor->Apply(TyDesc);
1072 Visitor->Apply(IsStatic);
1073 Visitor->Apply(IsDefinition);
1076 //===----------------------------------------------------------------------===//
1078 GlobalVariableDesc::GlobalVariableDesc()
1079 : GlobalDesc(DW_TAG_variable)
1083 // Implement isa/cast/dyncast.
1084 bool GlobalVariableDesc::classof(const DebugInfoDesc *D) {
1085 return D->getTag() == DW_TAG_variable;
1088 /// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
1090 void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
1091 GlobalDesc::ApplyToFields(Visitor);
1093 Visitor->Apply(Global);
1096 /// getDescString - Return a string used to compose global names and labels.
1098 const char *GlobalVariableDesc::getDescString() const {
1099 return "llvm.dbg.global_variable";
1102 /// getTypeString - Return a string used to label this descriptors type.
1104 const char *GlobalVariableDesc::getTypeString() const {
1105 return "llvm.dbg.global_variable.type";
1108 /// getAnchorString - Return a string used to label this descriptor's anchor.
1110 const char *GlobalVariableDesc::AnchorString = "llvm.dbg.global_variables";
1111 const char *GlobalVariableDesc::getAnchorString() const {
1112 return AnchorString;
1116 void GlobalVariableDesc::dump() {
1117 cerr << getDescString() << " "
1118 << "Version(" << getVersion() << "), "
1119 << "Tag(" << getTag() << "), "
1120 << "Anchor(" << getAnchor() << "), "
1121 << "Name(\"" << getName() << "\"), "
1122 << "FullName(\"" << getFullName() << "\"), "
1123 << "LinkageName(\"" << getLinkageName() << "\"), "
1124 << "File(" << getFile() << "),"
1125 << "Line(" << getLine() << "),"
1126 << "Type(" << getType() << "), "
1127 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1128 << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
1129 << "Global(" << Global << ")\n";
1133 //===----------------------------------------------------------------------===//
1135 SubprogramDesc::SubprogramDesc()
1136 : GlobalDesc(DW_TAG_subprogram)
1139 // Implement isa/cast/dyncast.
1140 bool SubprogramDesc::classof(const DebugInfoDesc *D) {
1141 return D->getTag() == DW_TAG_subprogram;
1144 /// ApplyToFields - Target the visitor to the fields of the
1146 void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
1147 GlobalDesc::ApplyToFields(Visitor);
1150 /// getDescString - Return a string used to compose global names and labels.
1152 const char *SubprogramDesc::getDescString() const {
1153 return "llvm.dbg.subprogram";
1156 /// getTypeString - Return a string used to label this descriptors type.
1158 const char *SubprogramDesc::getTypeString() const {
1159 return "llvm.dbg.subprogram.type";
1162 /// getAnchorString - Return a string used to label this descriptor's anchor.
1164 const char *SubprogramDesc::AnchorString = "llvm.dbg.subprograms";
1165 const char *SubprogramDesc::getAnchorString() const {
1166 return AnchorString;
1170 void SubprogramDesc::dump() {
1171 cerr << getDescString() << " "
1172 << "Version(" << getVersion() << "), "
1173 << "Tag(" << getTag() << "), "
1174 << "Anchor(" << getAnchor() << "), "
1175 << "Name(\"" << getName() << "\"), "
1176 << "FullName(\"" << getFullName() << "\"), "
1177 << "LinkageName(\"" << getLinkageName() << "\"), "
1178 << "File(" << getFile() << "),"
1179 << "Line(" << getLine() << "),"
1180 << "Type(" << getType() << "), "
1181 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1182 << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
1186 //===----------------------------------------------------------------------===//
1188 BlockDesc::BlockDesc()
1189 : DebugInfoDesc(DW_TAG_lexical_block)
1193 // Implement isa/cast/dyncast.
1194 bool BlockDesc::classof(const DebugInfoDesc *D) {
1195 return D->getTag() == DW_TAG_lexical_block;
1198 /// ApplyToFields - Target the visitor to the fields of the BlockDesc.
1200 void BlockDesc::ApplyToFields(DIVisitor *Visitor) {
1201 DebugInfoDesc::ApplyToFields(Visitor);
1203 Visitor->Apply(Context);
1206 /// getDescString - Return a string used to compose global names and labels.
1208 const char *BlockDesc::getDescString() const {
1209 return "llvm.dbg.block";
1212 /// getTypeString - Return a string used to label this descriptors type.
1214 const char *BlockDesc::getTypeString() const {
1215 return "llvm.dbg.block.type";
1219 void BlockDesc::dump() {
1220 cerr << getDescString() << " "
1221 << "Version(" << getVersion() << "), "
1222 << "Tag(" << getTag() << "),"
1223 << "Context(" << Context << ")\n";
1227 //===----------------------------------------------------------------------===//
1229 DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
1230 return Deserialize(getGlobalVariable(V));
1232 DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
1234 if (!GV) return NULL;
1236 // Check to see if it has been already deserialized.
1237 DebugInfoDesc *&Slot = GlobalDescs[GV];
1238 if (Slot) return Slot;
1240 // Get the Tag from the global.
1241 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1243 // Create an empty instance of the correct sort.
1244 Slot = DebugInfoDesc::DescFactory(Tag);
1246 // If not a user defined descriptor.
1248 // Deserialize the fields.
1249 DIDeserializeVisitor DRAM(*this, GV);
1250 DRAM.ApplyToFields(Slot);
1256 //===----------------------------------------------------------------------===//
1258 /// getStrPtrType - Return a "sbyte *" type.
1260 const PointerType *DISerializer::getStrPtrType() {
1261 // If not already defined.
1263 // Construct the pointer to signed bytes.
1264 StrPtrTy = PointerType::get(Type::Int8Ty);
1270 /// getEmptyStructPtrType - Return a "{ }*" type.
1272 const PointerType *DISerializer::getEmptyStructPtrType() {
1273 // If not already defined.
1274 if (!EmptyStructPtrTy) {
1275 // Construct the empty structure type.
1276 const StructType *EmptyStructTy =
1277 StructType::get(std::vector<const Type*>());
1278 // Construct the pointer to empty structure type.
1279 EmptyStructPtrTy = PointerType::get(EmptyStructTy);
1282 return EmptyStructPtrTy;
1285 /// getTagType - Return the type describing the specified descriptor (via tag.)
1287 const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
1288 // Attempt to get the previously defined type.
1289 StructType *&Ty = TagTypes[DD->getTag()];
1291 // If not already defined.
1293 // Set up fields vector.
1294 std::vector<const Type*> Fields;
1295 // Get types of fields.
1296 DIGetTypesVisitor GTAM(*this, Fields);
1297 GTAM.ApplyToFields(DD);
1299 // Construct structured type.
1300 Ty = StructType::get(Fields);
1302 // Register type name with module.
1303 M->addTypeName(DD->getTypeString(), Ty);
1309 /// getString - Construct the string as constant string global.
1311 Constant *DISerializer::getString(const std::string &String) {
1312 // Check string cache for previous edition.
1313 Constant *&Slot = StringCache[String];
1314 // Return Constant if previously defined.
1315 if (Slot) return Slot;
1316 // If empty string then use a sbyte* null instead.
1317 if (String.empty()) {
1318 Slot = ConstantPointerNull::get(getStrPtrType());
1320 // Construct string as an llvm constant.
1321 Constant *ConstStr = ConstantArray::get(String);
1322 // Otherwise create and return a new string global.
1323 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
1324 GlobalVariable::InternalLinkage,
1325 ConstStr, "str", M);
1326 StrGV->setSection("llvm.metadata");
1327 // Convert to generic string pointer.
1328 Slot = ConstantExpr::getBitCast(StrGV, getStrPtrType());
1334 /// Serialize - Recursively cast the specified descriptor into a GlobalVariable
1335 /// so that it can be serialized to a .bc or .ll file.
1336 GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
1337 // Check if the DebugInfoDesc is already in the map.
1338 GlobalVariable *&Slot = DescGlobals[DD];
1340 // See if DebugInfoDesc exists, if so return prior GlobalVariable.
1341 if (Slot) return Slot;
1343 // Get the type associated with the Tag.
1344 const StructType *Ty = getTagType(DD);
1346 // Create the GlobalVariable early to prevent infinite recursion.
1347 GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(),
1348 NULL, DD->getDescString(), M);
1349 GV->setSection("llvm.metadata");
1351 // Insert new GlobalVariable in DescGlobals map.
1354 // Set up elements vector
1355 std::vector<Constant*> Elements;
1357 DISerializeVisitor SRAM(*this, Elements);
1358 SRAM.ApplyToFields(DD);
1360 // Set the globals initializer.
1361 GV->setInitializer(ConstantStruct::get(Ty, Elements));
1366 //===----------------------------------------------------------------------===//
1368 /// Verify - Return true if the GlobalVariable appears to be a valid
1369 /// serialization of a DebugInfoDesc.
1370 bool DIVerifier::Verify(Value *V) {
1371 return !V || Verify(getGlobalVariable(V));
1373 bool DIVerifier::Verify(GlobalVariable *GV) {
1375 if (!GV) return true;
1377 // Check prior validity.
1378 unsigned &ValiditySlot = Validity[GV];
1380 // If visited before then use old state.
1381 if (ValiditySlot) return ValiditySlot == Valid;
1383 // Assume validity for the time being (recursion.)
1384 ValiditySlot = Valid;
1386 // Make sure the global is internal or link once (anchor.)
1387 if (GV->getLinkage() != GlobalValue::InternalLinkage &&
1388 GV->getLinkage() != GlobalValue::LinkOnceLinkage) {
1389 ValiditySlot = Invalid;
1394 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1396 // Check for user defined descriptors.
1397 if (Tag == DW_TAG_invalid) {
1398 ValiditySlot = Valid;
1403 unsigned Version = DebugInfoDesc::VersionFromGlobal(GV);
1405 // Check for version mismatch.
1406 if (Version != LLVMDebugVersion) {
1407 ValiditySlot = Invalid;
1411 // Construct an empty DebugInfoDesc.
1412 DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
1414 // Allow for user defined descriptors.
1415 if (!DD) return true;
1417 // Get the initializer constant.
1418 ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
1420 // Get the operand count.
1421 unsigned N = CI->getNumOperands();
1423 // Get the field count.
1424 unsigned &CountSlot = Counts[Tag];
1426 // Check the operand count to the field count
1427 DICountVisitor CTAM;
1428 CTAM.ApplyToFields(DD);
1429 CountSlot = CTAM.getCount();
1432 // Field count must be at most equal operand count.
1433 if (CountSlot > N) {
1435 ValiditySlot = Invalid;
1439 // Check each field for valid type.
1440 DIVerifyVisitor VRAM(*this, GV);
1441 VRAM.ApplyToFields(DD);
1443 // Release empty DebugInfoDesc.
1446 // If fields are not valid.
1447 if (!VRAM.isValid()) {
1448 ValiditySlot = Invalid;
1455 //===----------------------------------------------------------------------===//
1457 DebugScope::~DebugScope() {
1458 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1459 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
1462 //===----------------------------------------------------------------------===//
1464 MachineModuleInfo::MachineModuleInfo()
1477 MachineModuleInfo::~MachineModuleInfo() {
1481 /// doInitialization - Initialize the state for a new module.
1483 bool MachineModuleInfo::doInitialization() {
1487 /// doFinalization - Tear down the state after completion of a module.
1489 bool MachineModuleInfo::doFinalization() {
1493 /// BeginFunction - Begin gathering function meta information.
1495 void MachineModuleInfo::BeginFunction(MachineFunction *MF) {
1499 /// EndFunction - Discard function meta information.
1501 void MachineModuleInfo::EndFunction() {
1502 // Clean up scope information.
1509 // Clean up line info.
1512 // Clean up frame info.
1515 // Clean up exception info.
1516 LandingPads.clear();
1520 /// getDescFor - Convert a Value to a debug information descriptor.
1522 // FIXME - use new Value type when available.
1523 DebugInfoDesc *MachineModuleInfo::getDescFor(Value *V) {
1524 return DR.Deserialize(V);
1527 /// Verify - Verify that a Value is debug information descriptor.
1529 bool MachineModuleInfo::Verify(Value *V) {
1530 return VR.Verify(V);
1533 /// AnalyzeModule - Scan the module for global debug information.
1535 void MachineModuleInfo::AnalyzeModule(Module &M) {
1536 SetupCompileUnits(M);
1539 /// needsFrameInfo - Returns true if we need to gather callee-saved register
1540 /// move info for the frame.
1541 bool MachineModuleInfo::needsFrameInfo() const {
1542 return hasDebugInfo() || ExceptionHandling;
1545 /// SetupCompileUnits - Set up the unique vector of compile units.
1547 void MachineModuleInfo::SetupCompileUnits(Module &M) {
1548 std::vector<CompileUnitDesc *>CU = getAnchoredDescriptors<CompileUnitDesc>(M);
1550 for (unsigned i = 0, N = CU.size(); i < N; i++) {
1551 CompileUnits.insert(CU[i]);
1555 /// getCompileUnits - Return a vector of debug compile units.
1557 const UniqueVector<CompileUnitDesc *> MachineModuleInfo::getCompileUnits()const{
1558 return CompileUnits;
1561 /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1562 /// named GlobalVariable.
1563 std::vector<GlobalVariable*>
1564 MachineModuleInfo::getGlobalVariablesUsing(Module &M,
1565 const std::string &RootName) {
1566 return ::getGlobalVariablesUsing(M, RootName);
1569 /// RecordLabel - Records location information and associates it with a
1570 /// debug label. Returns a unique label ID used to generate a label and
1571 /// provide correspondence to the source line list.
1572 unsigned MachineModuleInfo::RecordLabel(unsigned Line, unsigned Column,
1574 unsigned ID = NextLabelID();
1575 Lines.push_back(SourceLineInfo(Line, Column, Source, ID));
1579 /// RecordSource - Register a source file with debug info. Returns an source
1581 unsigned MachineModuleInfo::RecordSource(const std::string &Directory,
1582 const std::string &Source) {
1583 unsigned DirectoryID = Directories.insert(Directory);
1584 return SourceFiles.insert(SourceFileInfo(DirectoryID, Source));
1586 unsigned MachineModuleInfo::RecordSource(const CompileUnitDesc *CompileUnit) {
1587 return RecordSource(CompileUnit->getDirectory(),
1588 CompileUnit->getFileName());
1591 /// RecordRegionStart - Indicate the start of a region.
1593 unsigned MachineModuleInfo::RecordRegionStart(Value *V) {
1594 // FIXME - need to be able to handle split scopes because of bb cloning.
1595 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1596 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1597 unsigned ID = NextLabelID();
1598 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
1602 /// RecordRegionEnd - Indicate the end of a region.
1604 unsigned MachineModuleInfo::RecordRegionEnd(Value *V) {
1605 // FIXME - need to be able to handle split scopes because of bb cloning.
1606 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1607 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1608 unsigned ID = NextLabelID();
1609 Scope->setEndLabelID(ID);
1613 /// RecordVariable - Indicate the declaration of a local variable.
1615 void MachineModuleInfo::RecordVariable(Value *V, unsigned FrameIndex) {
1616 VariableDesc *VD = cast<VariableDesc>(DR.Deserialize(V));
1617 DebugScope *Scope = getOrCreateScope(VD->getContext());
1618 DebugVariable *DV = new DebugVariable(VD, FrameIndex);
1619 Scope->AddVariable(DV);
1622 /// getOrCreateScope - Returns the scope associated with the given descriptor.
1624 DebugScope *MachineModuleInfo::getOrCreateScope(DebugInfoDesc *ScopeDesc) {
1625 DebugScope *&Slot = ScopeMap[ScopeDesc];
1627 // FIXME - breaks down when the context is an inlined function.
1628 DebugInfoDesc *ParentDesc = NULL;
1629 if (BlockDesc *Block = dyn_cast<BlockDesc>(ScopeDesc)) {
1630 ParentDesc = Block->getContext();
1632 DebugScope *Parent = ParentDesc ? getOrCreateScope(ParentDesc) : NULL;
1633 Slot = new DebugScope(Parent, ScopeDesc);
1635 Parent->AddScope(Slot);
1636 } else if (RootScope) {
1637 // FIXME - Add inlined function scopes to the root so we can delete
1638 // them later. Long term, handle inlined functions properly.
1639 RootScope->AddScope(Slot);
1641 // First function is top level function.
1648 //===-EH-------------------------------------------------------------------===//
1650 /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
1651 /// specified MachineBasicBlock.
1652 LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo
1653 (MachineBasicBlock *LandingPad) {
1654 unsigned N = LandingPads.size();
1655 for (unsigned i = 0; i < N; ++i) {
1656 LandingPadInfo &LP = LandingPads[i];
1657 if (LP.LandingPadBlock == LandingPad)
1661 LandingPads.push_back(LandingPadInfo(LandingPad));
1662 return LandingPads[N];
1665 /// addInvoke - Provide the begin and end labels of an invoke style call and
1666 /// associate it with a try landing pad block.
1667 void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad,
1668 unsigned BeginLabel, unsigned EndLabel) {
1669 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1670 if (!LP.BeginLabel) LP.BeginLabel = BeginLabel;
1671 LP.EndLabel = EndLabel;
1674 /// addLandingPad - Provide the label of a try LandingPad block.
1676 unsigned MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) {
1677 unsigned LandingPadLabel = NextLabelID();
1678 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1679 LP.LandingPadLabel = LandingPadLabel;
1680 return LandingPadLabel;
1683 /// addPersonality - Provide the personality function for the exception
1685 void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad,
1686 Function *Personality) {
1687 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1688 LP.Personality = Personality;
1691 /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
1693 void MachineModuleInfo::addCatchTypeInfo(MachineBasicBlock *LandingPad,
1694 std::vector<GlobalVariable *> &TyInfo) {
1695 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1696 for (unsigned N = TyInfo.size(); N; --N)
1697 LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
1700 /// setIsFilterLandingPad - Indicates that the landing pad is a throw filter.
1702 void MachineModuleInfo::setIsFilterLandingPad(MachineBasicBlock *LandingPad) {
1703 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1707 /// TidyLandingPads - Remap landing pad labels and remove any deleted landing
1709 void MachineModuleInfo::TidyLandingPads() {
1710 for (unsigned i = 0; i != LandingPads.size(); ) {
1711 LandingPadInfo &LandingPad = LandingPads[i];
1712 LandingPad.BeginLabel = MappedLabel(LandingPad.BeginLabel);
1713 LandingPad.EndLabel = MappedLabel(LandingPad.EndLabel);
1714 LandingPad.LandingPadLabel = MappedLabel(LandingPad.LandingPadLabel);
1716 if (!LandingPad.BeginLabel ||
1717 !LandingPad.EndLabel ||
1718 !LandingPad.LandingPadLabel) {
1719 LandingPads.erase(LandingPads.begin() + i);
1727 /// getTypeIDFor - Return the type id for the specified typeinfo. This is
1729 unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) {
1730 for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
1731 if (TypeInfos[i] == TI) return i + 1;
1733 TypeInfos.push_back(TI);
1734 return TypeInfos.size();
1737 /// getLandingPadInfos - Return a reference to the landing pad info for the
1738 /// current function.
1739 Function *MachineModuleInfo::getPersonality() const {
1740 return !LandingPads.empty() ? LandingPads[0].Personality : NULL;
1744 //===----------------------------------------------------------------------===//
1745 /// DebugLabelFolding pass - This pass prunes out redundant labels. This allows
1746 /// a info consumer to determine if the range of two labels is empty, by seeing
1747 /// if the labels map to the same reduced label.
1751 struct DebugLabelFolder : public MachineFunctionPass {
1752 virtual bool runOnMachineFunction(MachineFunction &MF);
1753 virtual const char *getPassName() const { return "Label Folder"; }
1756 bool DebugLabelFolder::runOnMachineFunction(MachineFunction &MF) {
1757 // Get machine module info.
1758 MachineModuleInfo *MMI = getAnalysisToUpdate<MachineModuleInfo>();
1759 if (!MMI) return false;
1760 // Get target instruction info.
1761 const TargetInstrInfo *TII = MF.getTarget().getInstrInfo();
1762 if (!TII) return false;
1764 // Track if change is made.
1765 bool MadeChange = false;
1766 // No prior label to begin.
1767 unsigned PriorLabel = 0;
1769 // Iterate through basic blocks.
1770 for (MachineFunction::iterator BB = MF.begin(), E = MF.end();
1772 // Iterate through instructions.
1773 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1775 if ((unsigned)I->getOpcode() == TargetInstrInfo::LABEL) {
1776 // The label ID # is always operand #0, an immediate.
1777 unsigned NextLabel = I->getOperand(0).getImm();
1779 // If there was an immediate prior label.
1781 // Remap the current label to prior label.
1782 MMI->RemapLabel(NextLabel, PriorLabel);
1783 // Delete the current label.
1785 // Indicate a change has been made.
1789 // Start a new round.
1790 PriorLabel = NextLabel;
1793 // No consecutive labels.
1804 FunctionPass *createDebugLabelFoldingPass() { return new DebugLabelFolder(); }