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/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");
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.
55 static std::vector<GlobalVariable*>
56 getGlobalVariablesUsing(Module &M, const std::string &RootName) {
57 std::vector<GlobalVariable*> Result; // GlobalVariables matching criteria.
59 std::vector<const Type*> FieldTypes;
60 FieldTypes.push_back(Type::Int32Ty);
61 FieldTypes.push_back(Type::Int32Ty);
63 // Get the GlobalVariable root.
64 GlobalVariable *UseRoot = M.getGlobalVariable(RootName,
65 StructType::get(FieldTypes));
67 // If present and linkonce then scan for users.
68 if (UseRoot && UseRoot->hasLinkOnceLinkage()) {
69 getGlobalVariablesUsing(UseRoot, Result);
75 /// isStringValue - Return true if the given value can be coerced to a string.
77 static bool isStringValue(Value *V) {
78 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
79 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
80 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
81 return Init->isString();
83 } else if (Constant *C = dyn_cast<Constant>(V)) {
84 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
85 return isStringValue(GV);
86 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
87 if (CE->getOpcode() == Instruction::GetElementPtr) {
88 if (CE->getNumOperands() == 3 &&
89 cast<Constant>(CE->getOperand(1))->isNullValue() &&
90 isa<ConstantInt>(CE->getOperand(2))) {
91 return isStringValue(CE->getOperand(0));
99 /// getGlobalVariable - Return either a direct or cast Global value.
101 static GlobalVariable *getGlobalVariable(Value *V) {
102 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
104 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
105 if (CE->getOpcode() == Instruction::BitCast) {
106 return dyn_cast<GlobalVariable>(CE->getOperand(0));
107 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
108 for (unsigned int i=1; i<CE->getNumOperands(); i++) {
109 if (!CE->getOperand(i)->isNullValue())
112 return dyn_cast<GlobalVariable>(CE->getOperand(0));
118 /// isGlobalVariable - Return true if the given value can be coerced to a
120 static bool isGlobalVariable(Value *V) {
121 if (isa<GlobalVariable>(V) || isa<ConstantPointerNull>(V)) {
123 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
124 if (CE->getOpcode() == Instruction::BitCast) {
125 return isa<GlobalVariable>(CE->getOperand(0));
126 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
127 for (unsigned int i=1; i<CE->getNumOperands(); i++) {
128 if (!CE->getOperand(i)->isNullValue())
131 return isa<GlobalVariable>(CE->getOperand(0));
137 /// getUIntOperand - Return ith operand if it is an unsigned integer.
139 static ConstantInt *getUIntOperand(GlobalVariable *GV, unsigned i) {
140 // Make sure the GlobalVariable has an initializer.
141 if (!GV->hasInitializer()) return NULL;
143 // Get the initializer constant.
144 ConstantStruct *CI = dyn_cast<ConstantStruct>(GV->getInitializer());
145 if (!CI) return NULL;
147 // Check if there is at least i + 1 operands.
148 unsigned N = CI->getNumOperands();
149 if (i >= N) return NULL;
152 return dyn_cast<ConstantInt>(CI->getOperand(i));
155 //===----------------------------------------------------------------------===//
157 /// ApplyToFields - Target the visitor to each field of the debug information
159 void DIVisitor::ApplyToFields(DebugInfoDesc *DD) {
160 DD->ApplyToFields(this);
163 //===----------------------------------------------------------------------===//
164 /// DICountVisitor - This DIVisitor counts all the fields in the supplied debug
165 /// the supplied DebugInfoDesc.
166 class DICountVisitor : public DIVisitor {
168 unsigned Count; // Running count of fields.
171 DICountVisitor() : DIVisitor(), Count(0) {}
174 unsigned getCount() const { return Count; }
176 /// Apply - Count each of the fields.
178 virtual void Apply(int &Field) { ++Count; }
179 virtual void Apply(unsigned &Field) { ++Count; }
180 virtual void Apply(int64_t &Field) { ++Count; }
181 virtual void Apply(uint64_t &Field) { ++Count; }
182 virtual void Apply(bool &Field) { ++Count; }
183 virtual void Apply(std::string &Field) { ++Count; }
184 virtual void Apply(DebugInfoDesc *&Field) { ++Count; }
185 virtual void Apply(GlobalVariable *&Field) { ++Count; }
186 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
191 //===----------------------------------------------------------------------===//
192 /// DIDeserializeVisitor - This DIVisitor deserializes all the fields in the
193 /// supplied DebugInfoDesc.
194 class DIDeserializeVisitor : public DIVisitor {
196 DIDeserializer &DR; // Active deserializer.
197 unsigned I; // Current operand index.
198 ConstantStruct *CI; // GlobalVariable constant initializer.
201 DIDeserializeVisitor(DIDeserializer &D, GlobalVariable *GV)
205 , CI(cast<ConstantStruct>(GV->getInitializer()))
208 /// Apply - Set the value of each of the fields.
210 virtual void Apply(int &Field) {
211 Constant *C = CI->getOperand(I++);
212 Field = cast<ConstantInt>(C)->getSExtValue();
214 virtual void Apply(unsigned &Field) {
215 Constant *C = CI->getOperand(I++);
216 Field = cast<ConstantInt>(C)->getZExtValue();
218 virtual void Apply(int64_t &Field) {
219 Constant *C = CI->getOperand(I++);
220 Field = cast<ConstantInt>(C)->getSExtValue();
222 virtual void Apply(uint64_t &Field) {
223 Constant *C = CI->getOperand(I++);
224 Field = cast<ConstantInt>(C)->getZExtValue();
226 virtual void Apply(bool &Field) {
227 Constant *C = CI->getOperand(I++);
228 Field = cast<ConstantInt>(C)->getZExtValue();
230 virtual void Apply(std::string &Field) {
231 Constant *C = CI->getOperand(I++);
232 Field = C->getStringValue();
234 virtual void Apply(DebugInfoDesc *&Field) {
235 Constant *C = CI->getOperand(I++);
236 Field = DR.Deserialize(C);
238 virtual void Apply(GlobalVariable *&Field) {
239 Constant *C = CI->getOperand(I++);
240 Field = getGlobalVariable(C);
242 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
244 Constant *C = CI->getOperand(I++);
245 GlobalVariable *GV = getGlobalVariable(C);
246 if (GV->hasInitializer()) {
247 if (ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer())) {
248 for (unsigned i = 0, N = CA->getNumOperands(); i < N; ++i) {
249 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
250 DebugInfoDesc *DE = DR.Deserialize(GVE);
253 } else if (GV->getInitializer()->isNullValue()) {
254 if (const ArrayType *T =
255 dyn_cast<ArrayType>(GV->getType()->getElementType())) {
256 Field.resize(T->getNumElements());
263 //===----------------------------------------------------------------------===//
264 /// DISerializeVisitor - This DIVisitor serializes all the fields in
265 /// the supplied DebugInfoDesc.
266 class DISerializeVisitor : public DIVisitor {
268 DISerializer &SR; // Active serializer.
269 std::vector<Constant*> &Elements; // Element accumulator.
272 DISerializeVisitor(DISerializer &S, std::vector<Constant*> &E)
278 /// Apply - Set the value of each of the fields.
280 virtual void Apply(int &Field) {
281 Elements.push_back(ConstantInt::get(Type::Int32Ty, int32_t(Field)));
283 virtual void Apply(unsigned &Field) {
284 Elements.push_back(ConstantInt::get(Type::Int32Ty, uint32_t(Field)));
286 virtual void Apply(int64_t &Field) {
287 Elements.push_back(ConstantInt::get(Type::Int64Ty, int64_t(Field)));
289 virtual void Apply(uint64_t &Field) {
290 Elements.push_back(ConstantInt::get(Type::Int64Ty, uint64_t(Field)));
292 virtual void Apply(bool &Field) {
293 Elements.push_back(ConstantInt::get(Type::Int1Ty, Field));
295 virtual void Apply(std::string &Field) {
296 Elements.push_back(SR.getString(Field));
298 virtual void Apply(DebugInfoDesc *&Field) {
299 GlobalVariable *GV = NULL;
301 // If non-NULL then convert to global.
302 if (Field) GV = SR.Serialize(Field);
304 // FIXME - At some point should use specific type.
305 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
308 // Set to pointer to global.
309 Elements.push_back(ConstantExpr::getBitCast(GV, EmptyTy));
312 Elements.push_back(ConstantPointerNull::get(EmptyTy));
315 virtual void Apply(GlobalVariable *&Field) {
316 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
318 Elements.push_back(ConstantExpr::getBitCast(Field, EmptyTy));
320 Elements.push_back(ConstantPointerNull::get(EmptyTy));
323 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
324 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
325 unsigned N = Field.size();
326 ArrayType *AT = ArrayType::get(EmptyTy, N);
327 std::vector<Constant *> ArrayElements;
329 for (unsigned i = 0, N = Field.size(); i < N; ++i) {
330 if (DebugInfoDesc *Element = Field[i]) {
331 GlobalVariable *GVE = SR.Serialize(Element);
332 Constant *CE = ConstantExpr::getBitCast(GVE, EmptyTy);
333 ArrayElements.push_back(cast<Constant>(CE));
335 ArrayElements.push_back(ConstantPointerNull::get(EmptyTy));
339 Constant *CA = ConstantArray::get(AT, ArrayElements);
340 GlobalVariable *CAGV = new GlobalVariable(AT, true,
341 GlobalValue::InternalLinkage,
342 CA, "llvm.dbg.array",
344 CAGV->setSection("llvm.metadata");
345 Constant *CAE = ConstantExpr::getBitCast(CAGV, EmptyTy);
346 Elements.push_back(CAE);
350 //===----------------------------------------------------------------------===//
351 /// DIGetTypesVisitor - This DIVisitor gathers all the field types in
352 /// the supplied DebugInfoDesc.
353 class DIGetTypesVisitor : public DIVisitor {
355 DISerializer &SR; // Active serializer.
356 std::vector<const Type*> &Fields; // Type accumulator.
359 DIGetTypesVisitor(DISerializer &S, std::vector<const Type*> &F)
365 /// Apply - Set the value of each of the fields.
367 virtual void Apply(int &Field) {
368 Fields.push_back(Type::Int32Ty);
370 virtual void Apply(unsigned &Field) {
371 Fields.push_back(Type::Int32Ty);
373 virtual void Apply(int64_t &Field) {
374 Fields.push_back(Type::Int64Ty);
376 virtual void Apply(uint64_t &Field) {
377 Fields.push_back(Type::Int64Ty);
379 virtual void Apply(bool &Field) {
380 Fields.push_back(Type::Int1Ty);
382 virtual void Apply(std::string &Field) {
383 Fields.push_back(SR.getStrPtrType());
385 virtual void Apply(DebugInfoDesc *&Field) {
386 // FIXME - At some point should use specific type.
387 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
388 Fields.push_back(EmptyTy);
390 virtual void Apply(GlobalVariable *&Field) {
391 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
392 Fields.push_back(EmptyTy);
394 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
395 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
396 Fields.push_back(EmptyTy);
400 //===----------------------------------------------------------------------===//
401 /// DIVerifyVisitor - This DIVisitor verifies all the field types against
402 /// a constant initializer.
403 class DIVerifyVisitor : public DIVisitor {
405 DIVerifier &VR; // Active verifier.
406 bool IsValid; // Validity status.
407 unsigned I; // Current operand index.
408 ConstantStruct *CI; // GlobalVariable constant initializer.
411 DIVerifyVisitor(DIVerifier &V, GlobalVariable *GV)
416 , CI(cast<ConstantStruct>(GV->getInitializer()))
421 bool isValid() const { return IsValid; }
423 /// Apply - Set the value of each of the fields.
425 virtual void Apply(int &Field) {
426 Constant *C = CI->getOperand(I++);
427 IsValid = IsValid && isa<ConstantInt>(C);
429 virtual void Apply(unsigned &Field) {
430 Constant *C = CI->getOperand(I++);
431 IsValid = IsValid && isa<ConstantInt>(C);
433 virtual void Apply(int64_t &Field) {
434 Constant *C = CI->getOperand(I++);
435 IsValid = IsValid && isa<ConstantInt>(C);
437 virtual void Apply(uint64_t &Field) {
438 Constant *C = CI->getOperand(I++);
439 IsValid = IsValid && isa<ConstantInt>(C);
441 virtual void Apply(bool &Field) {
442 Constant *C = CI->getOperand(I++);
443 IsValid = IsValid && isa<ConstantInt>(C) && C->getType() == Type::Int1Ty;
445 virtual void Apply(std::string &Field) {
446 Constant *C = CI->getOperand(I++);
448 (!C || isStringValue(C) || C->isNullValue());
450 virtual void Apply(DebugInfoDesc *&Field) {
451 // FIXME - Prepare the correct descriptor.
452 Constant *C = CI->getOperand(I++);
453 IsValid = IsValid && isGlobalVariable(C);
455 virtual void Apply(GlobalVariable *&Field) {
456 Constant *C = CI->getOperand(I++);
457 IsValid = IsValid && isGlobalVariable(C);
459 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
460 Constant *C = CI->getOperand(I++);
461 IsValid = IsValid && isGlobalVariable(C);
462 if (!IsValid) return;
464 GlobalVariable *GV = getGlobalVariable(C);
465 IsValid = IsValid && GV && GV->hasInitializer();
466 if (!IsValid) return;
468 ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer());
469 IsValid = IsValid && CA;
470 if (!IsValid) return;
472 for (unsigned i = 0, N = CA->getNumOperands(); IsValid && i < N; ++i) {
473 IsValid = IsValid && isGlobalVariable(CA->getOperand(i));
474 if (!IsValid) return;
476 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
483 //===----------------------------------------------------------------------===//
485 /// TagFromGlobal - Returns the tag number from a debug info descriptor
486 /// GlobalVariable. Return DIIValid if operand is not an unsigned int.
487 unsigned DebugInfoDesc::TagFromGlobal(GlobalVariable *GV) {
488 ConstantInt *C = getUIntOperand(GV, 0);
489 return C ? ((unsigned)C->getZExtValue() & ~LLVMDebugVersionMask) :
490 (unsigned)DW_TAG_invalid;
493 /// VersionFromGlobal - Returns the version number from a debug info
494 /// descriptor GlobalVariable. Return DIIValid if operand is not an unsigned
496 unsigned DebugInfoDesc::VersionFromGlobal(GlobalVariable *GV) {
497 ConstantInt *C = getUIntOperand(GV, 0);
498 return C ? ((unsigned)C->getZExtValue() & LLVMDebugVersionMask) :
499 (unsigned)DW_TAG_invalid;
502 /// DescFactory - Create an instance of debug info descriptor based on Tag.
503 /// Return NULL if not a recognized Tag.
504 DebugInfoDesc *DebugInfoDesc::DescFactory(unsigned Tag) {
506 case DW_TAG_anchor: return new AnchorDesc();
507 case DW_TAG_compile_unit: return new CompileUnitDesc();
508 case DW_TAG_variable: return new GlobalVariableDesc();
509 case DW_TAG_subprogram: return new SubprogramDesc();
510 case DW_TAG_lexical_block: return new BlockDesc();
511 case DW_TAG_base_type: return new BasicTypeDesc();
513 case DW_TAG_pointer_type:
514 case DW_TAG_reference_type:
515 case DW_TAG_const_type:
516 case DW_TAG_volatile_type:
517 case DW_TAG_restrict_type:
519 case DW_TAG_inheritance: return new DerivedTypeDesc(Tag);
520 case DW_TAG_array_type:
521 case DW_TAG_structure_type:
522 case DW_TAG_union_type:
523 case DW_TAG_enumeration_type:
524 case DW_TAG_vector_type:
525 case DW_TAG_subroutine_type: return new CompositeTypeDesc(Tag);
526 case DW_TAG_subrange_type: return new SubrangeDesc();
527 case DW_TAG_enumerator: return new EnumeratorDesc();
528 case DW_TAG_return_variable:
529 case DW_TAG_arg_variable:
530 case DW_TAG_auto_variable: return new VariableDesc(Tag);
536 /// getLinkage - get linkage appropriate for this type of descriptor.
538 GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const {
539 return GlobalValue::InternalLinkage;
542 /// ApplyToFields - Target the vistor to the fields of the descriptor.
544 void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) {
548 //===----------------------------------------------------------------------===//
550 AnchorDesc::AnchorDesc()
551 : DebugInfoDesc(DW_TAG_anchor)
554 AnchorDesc::AnchorDesc(AnchoredDesc *D)
555 : DebugInfoDesc(DW_TAG_anchor)
556 , AnchorTag(D->getTag())
559 // Implement isa/cast/dyncast.
560 bool AnchorDesc::classof(const DebugInfoDesc *D) {
561 return D->getTag() == DW_TAG_anchor;
564 /// getLinkage - get linkage appropriate for this type of descriptor.
566 GlobalValue::LinkageTypes AnchorDesc::getLinkage() const {
567 return GlobalValue::LinkOnceLinkage;
570 /// ApplyToFields - Target the visitor to the fields of the TransUnitDesc.
572 void AnchorDesc::ApplyToFields(DIVisitor *Visitor) {
573 DebugInfoDesc::ApplyToFields(Visitor);
575 Visitor->Apply(AnchorTag);
578 /// getDescString - Return a string used to compose global names and labels. A
579 /// A global variable name needs to be defined for each debug descriptor that is
580 /// anchored. NOTE: that each global variable named here also needs to be added
581 /// to the list of names left external in the internalizer.
582 /// ExternalNames.insert("llvm.dbg.compile_units");
583 /// ExternalNames.insert("llvm.dbg.global_variables");
584 /// ExternalNames.insert("llvm.dbg.subprograms");
585 const char *AnchorDesc::getDescString() const {
587 case DW_TAG_compile_unit: return CompileUnitDesc::AnchorString;
588 case DW_TAG_variable: return GlobalVariableDesc::AnchorString;
589 case DW_TAG_subprogram: return SubprogramDesc::AnchorString;
593 assert(0 && "Tag does not have a case for anchor string");
597 /// getTypeString - Return a string used to label this descriptors type.
599 const char *AnchorDesc::getTypeString() const {
600 return "llvm.dbg.anchor.type";
604 void AnchorDesc::dump() {
605 cerr << getDescString() << " "
606 << "Version(" << getVersion() << "), "
607 << "Tag(" << getTag() << "), "
608 << "AnchorTag(" << AnchorTag << ")\n";
612 //===----------------------------------------------------------------------===//
614 AnchoredDesc::AnchoredDesc(unsigned T)
619 /// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
621 void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) {
622 DebugInfoDesc::ApplyToFields(Visitor);
624 Visitor->Apply(Anchor);
627 //===----------------------------------------------------------------------===//
629 CompileUnitDesc::CompileUnitDesc()
630 : AnchoredDesc(DW_TAG_compile_unit)
637 // Implement isa/cast/dyncast.
638 bool CompileUnitDesc::classof(const DebugInfoDesc *D) {
639 return D->getTag() == DW_TAG_compile_unit;
642 /// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
644 void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) {
645 AnchoredDesc::ApplyToFields(Visitor);
647 // Handle cases out of sync with compiler.
648 if (getVersion() == 0) {
649 unsigned DebugVersion;
650 Visitor->Apply(DebugVersion);
653 Visitor->Apply(Language);
654 Visitor->Apply(FileName);
655 Visitor->Apply(Directory);
656 Visitor->Apply(Producer);
659 /// getDescString - Return a string used to compose global names and labels.
661 const char *CompileUnitDesc::getDescString() const {
662 return "llvm.dbg.compile_unit";
665 /// getTypeString - Return a string used to label this descriptors type.
667 const char *CompileUnitDesc::getTypeString() const {
668 return "llvm.dbg.compile_unit.type";
671 /// getAnchorString - Return a string used to label this descriptor's anchor.
673 const char *CompileUnitDesc::AnchorString = "llvm.dbg.compile_units";
674 const char *CompileUnitDesc::getAnchorString() const {
679 void CompileUnitDesc::dump() {
680 cerr << getDescString() << " "
681 << "Version(" << getVersion() << "), "
682 << "Tag(" << getTag() << "), "
683 << "Anchor(" << getAnchor() << "), "
684 << "Language(" << Language << "), "
685 << "FileName(\"" << FileName << "\"), "
686 << "Directory(\"" << Directory << "\"), "
687 << "Producer(\"" << Producer << "\")\n";
691 //===----------------------------------------------------------------------===//
693 TypeDesc::TypeDesc(unsigned T)
705 /// ApplyToFields - Target the visitor to the fields of the TypeDesc.
707 void TypeDesc::ApplyToFields(DIVisitor *Visitor) {
708 DebugInfoDesc::ApplyToFields(Visitor);
710 Visitor->Apply(Context);
711 Visitor->Apply(Name);
712 Visitor->Apply(File);
713 Visitor->Apply(Line);
714 Visitor->Apply(Size);
715 Visitor->Apply(Align);
716 Visitor->Apply(Offset);
717 if (getVersion() > LLVMDebugVersion4) Visitor->Apply(Flags);
720 /// getDescString - Return a string used to compose global names and labels.
722 const char *TypeDesc::getDescString() const {
723 return "llvm.dbg.type";
726 /// getTypeString - Return a string used to label this descriptor's type.
728 const char *TypeDesc::getTypeString() const {
729 return "llvm.dbg.type.type";
733 void TypeDesc::dump() {
734 cerr << getDescString() << " "
735 << "Version(" << getVersion() << "), "
736 << "Tag(" << getTag() << "), "
737 << "Context(" << Context << "), "
738 << "Name(\"" << Name << "\"), "
739 << "File(" << File << "), "
740 << "Line(" << Line << "), "
741 << "Size(" << Size << "), "
742 << "Align(" << Align << "), "
743 << "Offset(" << Offset << "), "
744 << "Flags(" << Flags << ")\n";
748 //===----------------------------------------------------------------------===//
750 BasicTypeDesc::BasicTypeDesc()
751 : TypeDesc(DW_TAG_base_type)
755 // Implement isa/cast/dyncast.
756 bool BasicTypeDesc::classof(const DebugInfoDesc *D) {
757 return D->getTag() == DW_TAG_base_type;
760 /// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
762 void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
763 TypeDesc::ApplyToFields(Visitor);
765 Visitor->Apply(Encoding);
768 /// getDescString - Return a string used to compose global names and labels.
770 const char *BasicTypeDesc::getDescString() const {
771 return "llvm.dbg.basictype";
774 /// getTypeString - Return a string used to label this descriptor's type.
776 const char *BasicTypeDesc::getTypeString() const {
777 return "llvm.dbg.basictype.type";
781 void BasicTypeDesc::dump() {
782 cerr << getDescString() << " "
783 << "Version(" << getVersion() << "), "
784 << "Tag(" << getTag() << "), "
785 << "Context(" << getContext() << "), "
786 << "Name(\"" << getName() << "\"), "
787 << "Size(" << getSize() << "), "
788 << "Encoding(" << Encoding << ")\n";
792 //===----------------------------------------------------------------------===//
794 DerivedTypeDesc::DerivedTypeDesc(unsigned T)
799 // Implement isa/cast/dyncast.
800 bool DerivedTypeDesc::classof(const DebugInfoDesc *D) {
801 unsigned T = D->getTag();
804 case DW_TAG_pointer_type:
805 case DW_TAG_reference_type:
806 case DW_TAG_const_type:
807 case DW_TAG_volatile_type:
808 case DW_TAG_restrict_type:
810 case DW_TAG_inheritance:
817 /// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
819 void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) {
820 TypeDesc::ApplyToFields(Visitor);
822 Visitor->Apply(FromType);
825 /// getDescString - Return a string used to compose global names and labels.
827 const char *DerivedTypeDesc::getDescString() const {
828 return "llvm.dbg.derivedtype";
831 /// getTypeString - Return a string used to label this descriptor's type.
833 const char *DerivedTypeDesc::getTypeString() const {
834 return "llvm.dbg.derivedtype.type";
838 void DerivedTypeDesc::dump() {
839 cerr << getDescString() << " "
840 << "Version(" << getVersion() << "), "
841 << "Tag(" << getTag() << "), "
842 << "Context(" << getContext() << "), "
843 << "Name(\"" << getName() << "\"), "
844 << "Size(" << getSize() << "), "
845 << "File(" << getFile() << "), "
846 << "Line(" << getLine() << "), "
847 << "FromType(" << FromType << ")\n";
851 //===----------------------------------------------------------------------===//
853 CompositeTypeDesc::CompositeTypeDesc(unsigned T)
858 // Implement isa/cast/dyncast.
859 bool CompositeTypeDesc::classof(const DebugInfoDesc *D) {
860 unsigned T = D->getTag();
862 case DW_TAG_array_type:
863 case DW_TAG_structure_type:
864 case DW_TAG_union_type:
865 case DW_TAG_enumeration_type:
866 case DW_TAG_vector_type:
867 case DW_TAG_subroutine_type:
874 /// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
876 void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) {
877 DerivedTypeDesc::ApplyToFields(Visitor);
879 Visitor->Apply(Elements);
882 /// getDescString - Return a string used to compose global names and labels.
884 const char *CompositeTypeDesc::getDescString() const {
885 return "llvm.dbg.compositetype";
888 /// getTypeString - Return a string used to label this descriptor's type.
890 const char *CompositeTypeDesc::getTypeString() const {
891 return "llvm.dbg.compositetype.type";
895 void CompositeTypeDesc::dump() {
896 cerr << getDescString() << " "
897 << "Version(" << getVersion() << "), "
898 << "Tag(" << getTag() << "), "
899 << "Context(" << getContext() << "), "
900 << "Name(\"" << getName() << "\"), "
901 << "Size(" << getSize() << "), "
902 << "File(" << getFile() << "), "
903 << "Line(" << getLine() << "), "
904 << "FromType(" << getFromType() << "), "
905 << "Elements.size(" << Elements.size() << ")\n";
909 //===----------------------------------------------------------------------===//
911 SubrangeDesc::SubrangeDesc()
912 : DebugInfoDesc(DW_TAG_subrange_type)
917 // Implement isa/cast/dyncast.
918 bool SubrangeDesc::classof(const DebugInfoDesc *D) {
919 return D->getTag() == DW_TAG_subrange_type;
922 /// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
924 void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) {
925 DebugInfoDesc::ApplyToFields(Visitor);
931 /// getDescString - Return a string used to compose global names and labels.
933 const char *SubrangeDesc::getDescString() const {
934 return "llvm.dbg.subrange";
937 /// getTypeString - Return a string used to label this descriptor's type.
939 const char *SubrangeDesc::getTypeString() const {
940 return "llvm.dbg.subrange.type";
944 void SubrangeDesc::dump() {
945 cerr << getDescString() << " "
946 << "Version(" << getVersion() << "), "
947 << "Tag(" << getTag() << "), "
948 << "Lo(" << Lo << "), "
949 << "Hi(" << Hi << ")\n";
953 //===----------------------------------------------------------------------===//
955 EnumeratorDesc::EnumeratorDesc()
956 : DebugInfoDesc(DW_TAG_enumerator)
961 // Implement isa/cast/dyncast.
962 bool EnumeratorDesc::classof(const DebugInfoDesc *D) {
963 return D->getTag() == DW_TAG_enumerator;
966 /// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
968 void EnumeratorDesc::ApplyToFields(DIVisitor *Visitor) {
969 DebugInfoDesc::ApplyToFields(Visitor);
971 Visitor->Apply(Name);
972 Visitor->Apply(Value);
975 /// getDescString - Return a string used to compose global names and labels.
977 const char *EnumeratorDesc::getDescString() const {
978 return "llvm.dbg.enumerator";
981 /// getTypeString - Return a string used to label this descriptor's type.
983 const char *EnumeratorDesc::getTypeString() const {
984 return "llvm.dbg.enumerator.type";
988 void EnumeratorDesc::dump() {
989 cerr << getDescString() << " "
990 << "Version(" << getVersion() << "), "
991 << "Tag(" << getTag() << "), "
992 << "Name(" << Name << "), "
993 << "Value(" << Value << ")\n";
997 //===----------------------------------------------------------------------===//
999 VariableDesc::VariableDesc(unsigned T)
1008 // Implement isa/cast/dyncast.
1009 bool VariableDesc::classof(const DebugInfoDesc *D) {
1010 unsigned T = D->getTag();
1012 case DW_TAG_auto_variable:
1013 case DW_TAG_arg_variable:
1014 case DW_TAG_return_variable:
1021 /// ApplyToFields - Target the visitor to the fields of the VariableDesc.
1023 void VariableDesc::ApplyToFields(DIVisitor *Visitor) {
1024 DebugInfoDesc::ApplyToFields(Visitor);
1026 Visitor->Apply(Context);
1027 Visitor->Apply(Name);
1028 Visitor->Apply(File);
1029 Visitor->Apply(Line);
1030 Visitor->Apply(TyDesc);
1033 /// getDescString - Return a string used to compose global names and labels.
1035 const char *VariableDesc::getDescString() const {
1036 return "llvm.dbg.variable";
1039 /// getTypeString - Return a string used to label this descriptor's type.
1041 const char *VariableDesc::getTypeString() const {
1042 return "llvm.dbg.variable.type";
1046 void VariableDesc::dump() {
1047 cerr << getDescString() << " "
1048 << "Version(" << getVersion() << "), "
1049 << "Tag(" << getTag() << "), "
1050 << "Context(" << Context << "), "
1051 << "Name(\"" << Name << "\"), "
1052 << "File(" << File << "), "
1053 << "Line(" << Line << "), "
1054 << "TyDesc(" << TyDesc << ")\n";
1058 //===----------------------------------------------------------------------===//
1060 GlobalDesc::GlobalDesc(unsigned T)
1070 , IsDefinition(false)
1073 /// ApplyToFields - Target the visitor to the fields of the global.
1075 void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
1076 AnchoredDesc::ApplyToFields(Visitor);
1078 Visitor->Apply(Context);
1079 Visitor->Apply(Name);
1080 Visitor->Apply(FullName);
1081 Visitor->Apply(LinkageName);
1082 Visitor->Apply(File);
1083 Visitor->Apply(Line);
1084 Visitor->Apply(TyDesc);
1085 Visitor->Apply(IsStatic);
1086 Visitor->Apply(IsDefinition);
1089 //===----------------------------------------------------------------------===//
1091 GlobalVariableDesc::GlobalVariableDesc()
1092 : GlobalDesc(DW_TAG_variable)
1096 // Implement isa/cast/dyncast.
1097 bool GlobalVariableDesc::classof(const DebugInfoDesc *D) {
1098 return D->getTag() == DW_TAG_variable;
1101 /// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
1103 void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
1104 GlobalDesc::ApplyToFields(Visitor);
1106 Visitor->Apply(Global);
1109 /// getDescString - Return a string used to compose global names and labels.
1111 const char *GlobalVariableDesc::getDescString() const {
1112 return "llvm.dbg.global_variable";
1115 /// getTypeString - Return a string used to label this descriptors type.
1117 const char *GlobalVariableDesc::getTypeString() const {
1118 return "llvm.dbg.global_variable.type";
1121 /// getAnchorString - Return a string used to label this descriptor's anchor.
1123 const char *GlobalVariableDesc::AnchorString = "llvm.dbg.global_variables";
1124 const char *GlobalVariableDesc::getAnchorString() const {
1125 return AnchorString;
1129 void GlobalVariableDesc::dump() {
1130 cerr << getDescString() << " "
1131 << "Version(" << getVersion() << "), "
1132 << "Tag(" << getTag() << "), "
1133 << "Anchor(" << getAnchor() << "), "
1134 << "Name(\"" << getName() << "\"), "
1135 << "FullName(\"" << getFullName() << "\"), "
1136 << "LinkageName(\"" << getLinkageName() << "\"), "
1137 << "File(" << getFile() << "),"
1138 << "Line(" << getLine() << "),"
1139 << "Type(" << getType() << "), "
1140 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1141 << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
1142 << "Global(" << Global << ")\n";
1146 //===----------------------------------------------------------------------===//
1148 SubprogramDesc::SubprogramDesc()
1149 : GlobalDesc(DW_TAG_subprogram)
1152 // Implement isa/cast/dyncast.
1153 bool SubprogramDesc::classof(const DebugInfoDesc *D) {
1154 return D->getTag() == DW_TAG_subprogram;
1157 /// ApplyToFields - Target the visitor to the fields of the
1159 void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
1160 GlobalDesc::ApplyToFields(Visitor);
1163 /// getDescString - Return a string used to compose global names and labels.
1165 const char *SubprogramDesc::getDescString() const {
1166 return "llvm.dbg.subprogram";
1169 /// getTypeString - Return a string used to label this descriptors type.
1171 const char *SubprogramDesc::getTypeString() const {
1172 return "llvm.dbg.subprogram.type";
1175 /// getAnchorString - Return a string used to label this descriptor's anchor.
1177 const char *SubprogramDesc::AnchorString = "llvm.dbg.subprograms";
1178 const char *SubprogramDesc::getAnchorString() const {
1179 return AnchorString;
1183 void SubprogramDesc::dump() {
1184 cerr << getDescString() << " "
1185 << "Version(" << getVersion() << "), "
1186 << "Tag(" << getTag() << "), "
1187 << "Anchor(" << getAnchor() << "), "
1188 << "Name(\"" << getName() << "\"), "
1189 << "FullName(\"" << getFullName() << "\"), "
1190 << "LinkageName(\"" << getLinkageName() << "\"), "
1191 << "File(" << getFile() << "),"
1192 << "Line(" << getLine() << "),"
1193 << "Type(" << getType() << "), "
1194 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1195 << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
1199 //===----------------------------------------------------------------------===//
1201 BlockDesc::BlockDesc()
1202 : DebugInfoDesc(DW_TAG_lexical_block)
1206 // Implement isa/cast/dyncast.
1207 bool BlockDesc::classof(const DebugInfoDesc *D) {
1208 return D->getTag() == DW_TAG_lexical_block;
1211 /// ApplyToFields - Target the visitor to the fields of the BlockDesc.
1213 void BlockDesc::ApplyToFields(DIVisitor *Visitor) {
1214 DebugInfoDesc::ApplyToFields(Visitor);
1216 Visitor->Apply(Context);
1219 /// getDescString - Return a string used to compose global names and labels.
1221 const char *BlockDesc::getDescString() const {
1222 return "llvm.dbg.block";
1225 /// getTypeString - Return a string used to label this descriptors type.
1227 const char *BlockDesc::getTypeString() const {
1228 return "llvm.dbg.block.type";
1232 void BlockDesc::dump() {
1233 cerr << getDescString() << " "
1234 << "Version(" << getVersion() << "), "
1235 << "Tag(" << getTag() << "),"
1236 << "Context(" << Context << ")\n";
1240 //===----------------------------------------------------------------------===//
1242 DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
1243 return Deserialize(getGlobalVariable(V));
1245 DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
1247 if (!GV) return NULL;
1249 // Check to see if it has been already deserialized.
1250 DebugInfoDesc *&Slot = GlobalDescs[GV];
1251 if (Slot) return Slot;
1253 // Get the Tag from the global.
1254 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1256 // Create an empty instance of the correct sort.
1257 Slot = DebugInfoDesc::DescFactory(Tag);
1259 // If not a user defined descriptor.
1261 // Deserialize the fields.
1262 DIDeserializeVisitor DRAM(*this, GV);
1263 DRAM.ApplyToFields(Slot);
1269 //===----------------------------------------------------------------------===//
1271 /// getStrPtrType - Return a "sbyte *" type.
1273 const PointerType *DISerializer::getStrPtrType() {
1274 // If not already defined.
1276 // Construct the pointer to signed bytes.
1277 StrPtrTy = PointerType::getUnqual(Type::Int8Ty);
1283 /// getEmptyStructPtrType - Return a "{ }*" type.
1285 const PointerType *DISerializer::getEmptyStructPtrType() {
1286 // If not already defined.
1287 if (!EmptyStructPtrTy) {
1288 // Construct the empty structure type.
1289 const StructType *EmptyStructTy =
1290 StructType::get(std::vector<const Type*>());
1291 // Construct the pointer to empty structure type.
1292 EmptyStructPtrTy = PointerType::getUnqual(EmptyStructTy);
1295 return EmptyStructPtrTy;
1298 /// getTagType - Return the type describing the specified descriptor (via tag.)
1300 const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
1301 // Attempt to get the previously defined type.
1302 StructType *&Ty = TagTypes[DD->getTag()];
1304 // If not already defined.
1306 // Set up fields vector.
1307 std::vector<const Type*> Fields;
1308 // Get types of fields.
1309 DIGetTypesVisitor GTAM(*this, Fields);
1310 GTAM.ApplyToFields(DD);
1312 // Construct structured type.
1313 Ty = StructType::get(Fields);
1315 // Register type name with module.
1316 M->addTypeName(DD->getTypeString(), Ty);
1322 /// getString - Construct the string as constant string global.
1324 Constant *DISerializer::getString(const std::string &String) {
1325 // Check string cache for previous edition.
1326 Constant *&Slot = StringCache[String];
1327 // Return Constant if previously defined.
1328 if (Slot) return Slot;
1329 // If empty string then use a sbyte* null instead.
1330 if (String.empty()) {
1331 Slot = ConstantPointerNull::get(getStrPtrType());
1333 // Construct string as an llvm constant.
1334 Constant *ConstStr = ConstantArray::get(String);
1335 // Otherwise create and return a new string global.
1336 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
1337 GlobalVariable::InternalLinkage,
1338 ConstStr, ".str", M);
1339 StrGV->setSection("llvm.metadata");
1340 // Convert to generic string pointer.
1341 Slot = ConstantExpr::getBitCast(StrGV, getStrPtrType());
1347 /// Serialize - Recursively cast the specified descriptor into a GlobalVariable
1348 /// so that it can be serialized to a .bc or .ll file.
1349 GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
1350 // Check if the DebugInfoDesc is already in the map.
1351 GlobalVariable *&Slot = DescGlobals[DD];
1353 // See if DebugInfoDesc exists, if so return prior GlobalVariable.
1354 if (Slot) return Slot;
1356 // Get the type associated with the Tag.
1357 const StructType *Ty = getTagType(DD);
1359 // Create the GlobalVariable early to prevent infinite recursion.
1360 GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(),
1361 NULL, DD->getDescString(), M);
1362 GV->setSection("llvm.metadata");
1364 // Insert new GlobalVariable in DescGlobals map.
1367 // Set up elements vector
1368 std::vector<Constant*> Elements;
1370 DISerializeVisitor SRAM(*this, Elements);
1371 SRAM.ApplyToFields(DD);
1373 // Set the globals initializer.
1374 GV->setInitializer(ConstantStruct::get(Ty, Elements));
1379 /// addDescriptor - Directly connect DD with existing GV.
1380 void DISerializer::addDescriptor(DebugInfoDesc *DD,
1381 GlobalVariable *GV) {
1382 DescGlobals[DD] = GV;
1385 //===----------------------------------------------------------------------===//
1387 /// Verify - Return true if the GlobalVariable appears to be a valid
1388 /// serialization of a DebugInfoDesc.
1389 bool DIVerifier::Verify(Value *V) {
1390 return !V || Verify(getGlobalVariable(V));
1392 bool DIVerifier::Verify(GlobalVariable *GV) {
1394 if (!GV) return true;
1396 // Check prior validity.
1397 unsigned &ValiditySlot = Validity[GV];
1399 // If visited before then use old state.
1400 if (ValiditySlot) return ValiditySlot == Valid;
1402 // Assume validity for the time being (recursion.)
1403 ValiditySlot = Valid;
1405 // Make sure the global is internal or link once (anchor.)
1406 if (GV->getLinkage() != GlobalValue::InternalLinkage &&
1407 GV->getLinkage() != GlobalValue::LinkOnceLinkage) {
1408 ValiditySlot = Invalid;
1413 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1415 // Check for user defined descriptors.
1416 if (Tag == DW_TAG_invalid) {
1417 ValiditySlot = Valid;
1422 unsigned Version = DebugInfoDesc::VersionFromGlobal(GV);
1424 // Check for version mismatch.
1425 if (Version != LLVMDebugVersion) {
1426 ValiditySlot = Invalid;
1430 // Construct an empty DebugInfoDesc.
1431 DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
1433 // Allow for user defined descriptors.
1434 if (!DD) return true;
1436 // Get the initializer constant.
1437 ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
1439 // Get the operand count.
1440 unsigned N = CI->getNumOperands();
1442 // Get the field count.
1443 unsigned &CountSlot = Counts[Tag];
1445 // Check the operand count to the field count
1446 DICountVisitor CTAM;
1447 CTAM.ApplyToFields(DD);
1448 CountSlot = CTAM.getCount();
1451 // Field count must be at most equal operand count.
1452 if (CountSlot > N) {
1454 ValiditySlot = Invalid;
1458 // Check each field for valid type.
1459 DIVerifyVisitor VRAM(*this, GV);
1460 VRAM.ApplyToFields(DD);
1462 // Release empty DebugInfoDesc.
1465 // If fields are not valid.
1466 if (!VRAM.isValid()) {
1467 ValiditySlot = Invalid;
1474 //===----------------------------------------------------------------------===//
1476 DebugScope::~DebugScope() {
1477 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1478 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
1481 //===----------------------------------------------------------------------===//
1483 MachineModuleInfo::MachineModuleInfo()
1484 : ImmutablePass((intptr_t)&ID)
1498 , CallsUnwindInit(0)
1500 // Always emit "no personality" info
1501 Personalities.push_back(NULL);
1503 MachineModuleInfo::~MachineModuleInfo() {
1507 /// doInitialization - Initialize the state for a new module.
1509 bool MachineModuleInfo::doInitialization() {
1513 /// doFinalization - Tear down the state after completion of a module.
1515 bool MachineModuleInfo::doFinalization() {
1519 /// BeginFunction - Begin gathering function meta information.
1521 void MachineModuleInfo::BeginFunction(MachineFunction *MF) {
1525 /// EndFunction - Discard function meta information.
1527 void MachineModuleInfo::EndFunction() {
1528 // Clean up scope information.
1535 // Clean up line info.
1538 // Clean up frame info.
1541 // Clean up exception info.
1542 LandingPads.clear();
1547 CallsUnwindInit = 0;
1550 /// getDescFor - Convert a Value to a debug information descriptor.
1552 // FIXME - use new Value type when available.
1553 DebugInfoDesc *MachineModuleInfo::getDescFor(Value *V) {
1554 return DR.Deserialize(V);
1557 /// Verify - Verify that a Value is debug information descriptor.
1559 bool MachineModuleInfo::Verify(Value *V) {
1560 return VR.Verify(V);
1563 /// AnalyzeModule - Scan the module for global debug information.
1565 void MachineModuleInfo::AnalyzeModule(Module &M) {
1566 SetupCompileUnits(M);
1568 // Insert functions in the llvm.used array into UsedFunctions.
1569 GlobalVariable *GV = M.getGlobalVariable("llvm.used");
1570 if (!GV || !GV->hasInitializer()) return;
1572 // Should be an array of 'i8*'.
1573 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
1574 if (InitList == 0) return;
1576 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
1577 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(InitList->getOperand(i)))
1578 if (CE->getOpcode() == Instruction::BitCast)
1579 if (Function *F = dyn_cast<Function>(CE->getOperand(0)))
1580 UsedFunctions.insert(F);
1584 /// needsFrameInfo - Returns true if we need to gather callee-saved register
1585 /// move info for the frame.
1586 bool MachineModuleInfo::needsFrameInfo() const {
1587 return hasDebugInfo() || ExceptionHandling;
1590 /// SetupCompileUnits - Set up the unique vector of compile units.
1592 void MachineModuleInfo::SetupCompileUnits(Module &M) {
1593 std::vector<CompileUnitDesc *>CU = getAnchoredDescriptors<CompileUnitDesc>(M);
1595 for (unsigned i = 0, N = CU.size(); i < N; i++) {
1596 CompileUnits.insert(CU[i]);
1600 /// getCompileUnits - Return a vector of debug compile units.
1602 const UniqueVector<CompileUnitDesc *> MachineModuleInfo::getCompileUnits()const{
1603 return CompileUnits;
1606 /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1607 /// named GlobalVariable.
1608 std::vector<GlobalVariable*>
1609 MachineModuleInfo::getGlobalVariablesUsing(Module &M,
1610 const std::string &RootName) {
1611 return ::getGlobalVariablesUsing(M, RootName);
1614 /// RecordLabel - Records location information and associates it with a
1615 /// debug label. Returns a unique label ID used to generate a label and
1616 /// provide correspondence to the source line list.
1617 unsigned MachineModuleInfo::RecordLabel(unsigned Line, unsigned Column,
1619 unsigned ID = NextLabelID();
1620 Lines.push_back(SourceLineInfo(Line, Column, Source, ID));
1624 /// RecordSource - Register a source file with debug info. Returns an source
1626 unsigned MachineModuleInfo::RecordSource(const std::string &Directory,
1627 const std::string &Source) {
1628 unsigned DirectoryID = Directories.insert(Directory);
1629 return SourceFiles.insert(SourceFileInfo(DirectoryID, Source));
1631 unsigned MachineModuleInfo::RecordSource(const CompileUnitDesc *CompileUnit) {
1632 return RecordSource(CompileUnit->getDirectory(),
1633 CompileUnit->getFileName());
1636 /// RecordRegionStart - Indicate the start of a region.
1638 unsigned MachineModuleInfo::RecordRegionStart(Value *V) {
1639 // FIXME - need to be able to handle split scopes because of bb cloning.
1640 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1641 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1642 unsigned ID = NextLabelID();
1643 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
1647 /// RecordRegionEnd - Indicate the end of a region.
1649 unsigned MachineModuleInfo::RecordRegionEnd(Value *V) {
1650 // FIXME - need to be able to handle split scopes because of bb cloning.
1651 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1652 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1653 unsigned ID = NextLabelID();
1654 Scope->setEndLabelID(ID);
1658 /// RecordVariable - Indicate the declaration of a local variable.
1660 void MachineModuleInfo::RecordVariable(Value *V, unsigned FrameIndex) {
1661 VariableDesc *VD = cast<VariableDesc>(DR.Deserialize(V));
1662 DebugScope *Scope = getOrCreateScope(VD->getContext());
1663 DebugVariable *DV = new DebugVariable(VD, FrameIndex);
1664 Scope->AddVariable(DV);
1667 /// getOrCreateScope - Returns the scope associated with the given descriptor.
1669 DebugScope *MachineModuleInfo::getOrCreateScope(DebugInfoDesc *ScopeDesc) {
1670 DebugScope *&Slot = ScopeMap[ScopeDesc];
1672 // FIXME - breaks down when the context is an inlined function.
1673 DebugInfoDesc *ParentDesc = NULL;
1674 if (BlockDesc *Block = dyn_cast<BlockDesc>(ScopeDesc)) {
1675 ParentDesc = Block->getContext();
1677 DebugScope *Parent = ParentDesc ? getOrCreateScope(ParentDesc) : NULL;
1678 Slot = new DebugScope(Parent, ScopeDesc);
1680 Parent->AddScope(Slot);
1681 } else if (RootScope) {
1682 // FIXME - Add inlined function scopes to the root so we can delete
1683 // them later. Long term, handle inlined functions properly.
1684 RootScope->AddScope(Slot);
1686 // First function is top level function.
1693 //===-EH-------------------------------------------------------------------===//
1695 /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
1696 /// specified MachineBasicBlock.
1697 LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo
1698 (MachineBasicBlock *LandingPad) {
1699 unsigned N = LandingPads.size();
1700 for (unsigned i = 0; i < N; ++i) {
1701 LandingPadInfo &LP = LandingPads[i];
1702 if (LP.LandingPadBlock == LandingPad)
1706 LandingPads.push_back(LandingPadInfo(LandingPad));
1707 return LandingPads[N];
1710 /// addInvoke - Provide the begin and end labels of an invoke style call and
1711 /// associate it with a try landing pad block.
1712 void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad,
1713 unsigned BeginLabel, unsigned EndLabel) {
1714 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1715 LP.BeginLabels.push_back(BeginLabel);
1716 LP.EndLabels.push_back(EndLabel);
1719 /// addLandingPad - Provide the label of a try LandingPad block.
1721 unsigned MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) {
1722 unsigned LandingPadLabel = NextLabelID();
1723 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1724 LP.LandingPadLabel = LandingPadLabel;
1725 return LandingPadLabel;
1728 /// addPersonality - Provide the personality function for the exception
1730 void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad,
1731 Function *Personality) {
1732 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1733 LP.Personality = Personality;
1735 for (unsigned i = 0; i < Personalities.size(); ++i)
1736 if (Personalities[i] == Personality)
1739 Personalities.push_back(Personality);
1742 /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
1744 void MachineModuleInfo::addCatchTypeInfo(MachineBasicBlock *LandingPad,
1745 std::vector<GlobalVariable *> &TyInfo) {
1746 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1747 for (unsigned N = TyInfo.size(); N; --N)
1748 LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
1751 /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
1753 void MachineModuleInfo::addFilterTypeInfo(MachineBasicBlock *LandingPad,
1754 std::vector<GlobalVariable *> &TyInfo) {
1755 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1756 std::vector<unsigned> IdsInFilter (TyInfo.size());
1757 for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
1758 IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
1759 LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
1762 /// addCleanup - Add a cleanup action for a landing pad.
1764 void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) {
1765 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1766 LP.TypeIds.push_back(0);
1769 /// TidyLandingPads - Remap landing pad labels and remove any deleted landing
1771 void MachineModuleInfo::TidyLandingPads() {
1772 for (unsigned i = 0; i != LandingPads.size(); ) {
1773 LandingPadInfo &LandingPad = LandingPads[i];
1774 LandingPad.LandingPadLabel = MappedLabel(LandingPad.LandingPadLabel);
1776 // Special case: we *should* emit LPs with null LP MBB. This indicates
1778 if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
1779 LandingPads.erase(LandingPads.begin() + i);
1783 for (unsigned j=0; j != LandingPads[i].BeginLabels.size(); ) {
1784 unsigned BeginLabel = MappedLabel(LandingPad.BeginLabels[j]);
1785 unsigned EndLabel = MappedLabel(LandingPad.EndLabels[j]);
1787 if (!BeginLabel || !EndLabel) {
1788 LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
1789 LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
1793 LandingPad.BeginLabels[j] = BeginLabel;
1794 LandingPad.EndLabels[j] = EndLabel;
1798 // Remove landing pads with no try-ranges.
1799 if (LandingPads[i].BeginLabels.empty()) {
1800 LandingPads.erase(LandingPads.begin() + i);
1804 // If there is no landing pad, ensure that the list of typeids is empty.
1805 // If the only typeid is a cleanup, this is the same as having no typeids.
1806 if (!LandingPad.LandingPadBlock ||
1807 (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
1808 LandingPad.TypeIds.clear();
1814 /// getTypeIDFor - Return the type id for the specified typeinfo. This is
1816 unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) {
1817 for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
1818 if (TypeInfos[i] == TI) return i + 1;
1820 TypeInfos.push_back(TI);
1821 return TypeInfos.size();
1824 /// getFilterIDFor - Return the filter id for the specified typeinfos. This is
1826 int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) {
1827 // If the new filter coincides with the tail of an existing filter, then
1828 // re-use the existing filter. Folding filters more than this requires
1829 // re-ordering filters and/or their elements - probably not worth it.
1830 for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
1831 E = FilterEnds.end(); I != E; ++I) {
1832 unsigned i = *I, j = TyIds.size();
1835 if (FilterIds[--i] != TyIds[--j])
1839 // The new filter coincides with range [i, end) of the existing filter.
1845 // Add the new filter.
1846 int FilterID = -(1 + FilterIds.size());
1847 FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
1848 for (unsigned I = 0, N = TyIds.size(); I != N; ++I)
1849 FilterIds.push_back(TyIds[I]);
1850 FilterEnds.push_back(FilterIds.size());
1851 FilterIds.push_back(0); // terminator
1855 /// getPersonality - Return the personality function for the current function.
1856 Function *MachineModuleInfo::getPersonality() const {
1857 // FIXME: Until PR1414 will be fixed, we're using 1 personality function per
1859 return !LandingPads.empty() ? LandingPads[0].Personality : NULL;
1862 /// getPersonalityIndex - Return unique index for current personality
1863 /// function. NULL personality function should always get zero index.
1864 unsigned MachineModuleInfo::getPersonalityIndex() const {
1865 const Function* Personality = NULL;
1867 // Scan landing pads. If there is at least one non-NULL personality - use it.
1868 for (unsigned i = 0; i != LandingPads.size(); ++i)
1869 if (LandingPads[i].Personality) {
1870 Personality = LandingPads[i].Personality;
1874 for (unsigned i = 0; i < Personalities.size(); ++i) {
1875 if (Personalities[i] == Personality)
1879 // This should never happen
1880 assert(0 && "Personality function should be set!");
1884 //===----------------------------------------------------------------------===//
1885 /// DebugLabelFolding pass - This pass prunes out redundant labels. This allows
1886 /// a info consumer to determine if the range of two labels is empty, by seeing
1887 /// if the labels map to the same reduced label.
1891 struct DebugLabelFolder : public MachineFunctionPass {
1893 DebugLabelFolder() : MachineFunctionPass((intptr_t)&ID) {}
1895 virtual bool runOnMachineFunction(MachineFunction &MF);
1896 virtual const char *getPassName() const { return "Label Folder"; }
1899 char DebugLabelFolder::ID = 0;
1901 bool DebugLabelFolder::runOnMachineFunction(MachineFunction &MF) {
1902 // Get machine module info.
1903 MachineModuleInfo *MMI = getAnalysisToUpdate<MachineModuleInfo>();
1904 if (!MMI) return false;
1906 // Track if change is made.
1907 bool MadeChange = false;
1908 // No prior label to begin.
1909 unsigned PriorLabel = 0;
1911 // Iterate through basic blocks.
1912 for (MachineFunction::iterator BB = MF.begin(), E = MF.end();
1914 // Iterate through instructions.
1915 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1917 if (I->isDebugLabel()) {
1918 // The label ID # is always operand #0, an immediate.
1919 unsigned NextLabel = I->getOperand(0).getImm();
1921 // If there was an immediate prior label.
1923 // Remap the current label to prior label.
1924 MMI->RemapLabel(NextLabel, PriorLabel);
1925 // Delete the current label.
1927 // Indicate a change has been made.
1931 // Start a new round.
1932 PriorLabel = NextLabel;
1935 // No consecutive labels.
1946 FunctionPass *createDebugLabelFoldingPass() { return new DebugLabelFolder(); }