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");
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));
112 /// isGlobalVariable - Return true if the given value can be coerced to a
114 static bool isGlobalVariable(Value *V) {
115 if (isa<GlobalVariable>(V) || isa<ConstantPointerNull>(V)) {
117 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
118 if (CE->getOpcode() == Instruction::BitCast) {
119 return isa<GlobalVariable>(CE->getOperand(0));
125 /// getUIntOperand - Return ith operand if it is an unsigned integer.
127 static ConstantInt *getUIntOperand(GlobalVariable *GV, unsigned i) {
128 // Make sure the GlobalVariable has an initializer.
129 if (!GV->hasInitializer()) return NULL;
131 // Get the initializer constant.
132 ConstantStruct *CI = dyn_cast<ConstantStruct>(GV->getInitializer());
133 if (!CI) return NULL;
135 // Check if there is at least i + 1 operands.
136 unsigned N = CI->getNumOperands();
137 if (i >= N) return NULL;
140 return dyn_cast<ConstantInt>(CI->getOperand(i));
143 //===----------------------------------------------------------------------===//
145 /// ApplyToFields - Target the visitor to each field of the debug information
147 void DIVisitor::ApplyToFields(DebugInfoDesc *DD) {
148 DD->ApplyToFields(this);
151 //===----------------------------------------------------------------------===//
152 /// DICountVisitor - This DIVisitor counts all the fields in the supplied debug
153 /// the supplied DebugInfoDesc.
154 class DICountVisitor : public DIVisitor {
156 unsigned Count; // Running count of fields.
159 DICountVisitor() : DIVisitor(), Count(0) {}
162 unsigned getCount() const { return Count; }
164 /// Apply - Count each of the fields.
166 virtual void Apply(int &Field) { ++Count; }
167 virtual void Apply(unsigned &Field) { ++Count; }
168 virtual void Apply(int64_t &Field) { ++Count; }
169 virtual void Apply(uint64_t &Field) { ++Count; }
170 virtual void Apply(bool &Field) { ++Count; }
171 virtual void Apply(std::string &Field) { ++Count; }
172 virtual void Apply(DebugInfoDesc *&Field) { ++Count; }
173 virtual void Apply(GlobalVariable *&Field) { ++Count; }
174 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
179 //===----------------------------------------------------------------------===//
180 /// DIDeserializeVisitor - This DIVisitor deserializes all the fields in the
181 /// supplied DebugInfoDesc.
182 class DIDeserializeVisitor : public DIVisitor {
184 DIDeserializer &DR; // Active deserializer.
185 unsigned I; // Current operand index.
186 ConstantStruct *CI; // GlobalVariable constant initializer.
189 DIDeserializeVisitor(DIDeserializer &D, GlobalVariable *GV)
193 , CI(cast<ConstantStruct>(GV->getInitializer()))
196 /// Apply - Set the value of each of the fields.
198 virtual void Apply(int &Field) {
199 Constant *C = CI->getOperand(I++);
200 Field = cast<ConstantInt>(C)->getSExtValue();
202 virtual void Apply(unsigned &Field) {
203 Constant *C = CI->getOperand(I++);
204 Field = cast<ConstantInt>(C)->getZExtValue();
206 virtual void Apply(int64_t &Field) {
207 Constant *C = CI->getOperand(I++);
208 Field = cast<ConstantInt>(C)->getSExtValue();
210 virtual void Apply(uint64_t &Field) {
211 Constant *C = CI->getOperand(I++);
212 Field = cast<ConstantInt>(C)->getZExtValue();
214 virtual void Apply(bool &Field) {
215 Constant *C = CI->getOperand(I++);
216 Field = cast<ConstantInt>(C)->getZExtValue();
218 virtual void Apply(std::string &Field) {
219 Constant *C = CI->getOperand(I++);
220 Field = C->getStringValue();
222 virtual void Apply(DebugInfoDesc *&Field) {
223 Constant *C = CI->getOperand(I++);
224 Field = DR.Deserialize(C);
226 virtual void Apply(GlobalVariable *&Field) {
227 Constant *C = CI->getOperand(I++);
228 Field = getGlobalVariable(C);
230 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
232 Constant *C = CI->getOperand(I++);
233 GlobalVariable *GV = getGlobalVariable(C);
234 if (GV->hasInitializer()) {
235 if (ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer())) {
236 for (unsigned i = 0, N = CA->getNumOperands(); i < N; ++i) {
237 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
238 DebugInfoDesc *DE = DR.Deserialize(GVE);
241 } else if (GV->getInitializer()->isNullValue()) {
242 if (const ArrayType *T =
243 dyn_cast<ArrayType>(GV->getType()->getElementType())) {
244 Field.resize(T->getNumElements());
251 //===----------------------------------------------------------------------===//
252 /// DISerializeVisitor - This DIVisitor serializes all the fields in
253 /// the supplied DebugInfoDesc.
254 class DISerializeVisitor : public DIVisitor {
256 DISerializer &SR; // Active serializer.
257 std::vector<Constant*> &Elements; // Element accumulator.
260 DISerializeVisitor(DISerializer &S, std::vector<Constant*> &E)
266 /// Apply - Set the value of each of the fields.
268 virtual void Apply(int &Field) {
269 Elements.push_back(ConstantInt::get(Type::Int32Ty, int32_t(Field)));
271 virtual void Apply(unsigned &Field) {
272 Elements.push_back(ConstantInt::get(Type::Int32Ty, uint32_t(Field)));
274 virtual void Apply(int64_t &Field) {
275 Elements.push_back(ConstantInt::get(Type::Int64Ty, int64_t(Field)));
277 virtual void Apply(uint64_t &Field) {
278 Elements.push_back(ConstantInt::get(Type::Int64Ty, uint64_t(Field)));
280 virtual void Apply(bool &Field) {
281 Elements.push_back(ConstantInt::get(Type::Int1Ty, Field));
283 virtual void Apply(std::string &Field) {
284 Elements.push_back(SR.getString(Field));
286 virtual void Apply(DebugInfoDesc *&Field) {
287 GlobalVariable *GV = NULL;
289 // If non-NULL then convert to global.
290 if (Field) GV = SR.Serialize(Field);
292 // FIXME - At some point should use specific type.
293 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
296 // Set to pointer to global.
297 Elements.push_back(ConstantExpr::getBitCast(GV, EmptyTy));
300 Elements.push_back(ConstantPointerNull::get(EmptyTy));
303 virtual void Apply(GlobalVariable *&Field) {
304 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
306 Elements.push_back(ConstantExpr::getBitCast(Field, EmptyTy));
308 Elements.push_back(ConstantPointerNull::get(EmptyTy));
311 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
312 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
313 unsigned N = Field.size();
314 ArrayType *AT = ArrayType::get(EmptyTy, N);
315 std::vector<Constant *> ArrayElements;
317 for (unsigned i = 0, N = Field.size(); i < N; ++i) {
318 if (DebugInfoDesc *Element = Field[i]) {
319 GlobalVariable *GVE = SR.Serialize(Element);
320 Constant *CE = ConstantExpr::getBitCast(GVE, EmptyTy);
321 ArrayElements.push_back(cast<Constant>(CE));
323 ArrayElements.push_back(ConstantPointerNull::get(EmptyTy));
327 Constant *CA = ConstantArray::get(AT, ArrayElements);
328 GlobalVariable *CAGV = new GlobalVariable(AT, true,
329 GlobalValue::InternalLinkage,
330 CA, "llvm.dbg.array",
332 CAGV->setSection("llvm.metadata");
333 Constant *CAE = ConstantExpr::getBitCast(CAGV, EmptyTy);
334 Elements.push_back(CAE);
338 //===----------------------------------------------------------------------===//
339 /// DIGetTypesVisitor - This DIVisitor gathers all the field types in
340 /// the supplied DebugInfoDesc.
341 class DIGetTypesVisitor : public DIVisitor {
343 DISerializer &SR; // Active serializer.
344 std::vector<const Type*> &Fields; // Type accumulator.
347 DIGetTypesVisitor(DISerializer &S, std::vector<const Type*> &F)
353 /// Apply - Set the value of each of the fields.
355 virtual void Apply(int &Field) {
356 Fields.push_back(Type::Int32Ty);
358 virtual void Apply(unsigned &Field) {
359 Fields.push_back(Type::Int32Ty);
361 virtual void Apply(int64_t &Field) {
362 Fields.push_back(Type::Int64Ty);
364 virtual void Apply(uint64_t &Field) {
365 Fields.push_back(Type::Int64Ty);
367 virtual void Apply(bool &Field) {
368 Fields.push_back(Type::Int1Ty);
370 virtual void Apply(std::string &Field) {
371 Fields.push_back(SR.getStrPtrType());
373 virtual void Apply(DebugInfoDesc *&Field) {
374 // FIXME - At some point should use specific type.
375 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
376 Fields.push_back(EmptyTy);
378 virtual void Apply(GlobalVariable *&Field) {
379 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
380 Fields.push_back(EmptyTy);
382 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
383 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
384 Fields.push_back(EmptyTy);
388 //===----------------------------------------------------------------------===//
389 /// DIVerifyVisitor - This DIVisitor verifies all the field types against
390 /// a constant initializer.
391 class DIVerifyVisitor : public DIVisitor {
393 DIVerifier &VR; // Active verifier.
394 bool IsValid; // Validity status.
395 unsigned I; // Current operand index.
396 ConstantStruct *CI; // GlobalVariable constant initializer.
399 DIVerifyVisitor(DIVerifier &V, GlobalVariable *GV)
404 , CI(cast<ConstantStruct>(GV->getInitializer()))
409 bool isValid() const { return IsValid; }
411 /// Apply - Set the value of each of the fields.
413 virtual void Apply(int &Field) {
414 Constant *C = CI->getOperand(I++);
415 IsValid = IsValid && isa<ConstantInt>(C);
417 virtual void Apply(unsigned &Field) {
418 Constant *C = CI->getOperand(I++);
419 IsValid = IsValid && isa<ConstantInt>(C);
421 virtual void Apply(int64_t &Field) {
422 Constant *C = CI->getOperand(I++);
423 IsValid = IsValid && isa<ConstantInt>(C);
425 virtual void Apply(uint64_t &Field) {
426 Constant *C = CI->getOperand(I++);
427 IsValid = IsValid && isa<ConstantInt>(C);
429 virtual void Apply(bool &Field) {
430 Constant *C = CI->getOperand(I++);
431 IsValid = IsValid && isa<ConstantInt>(C) && C->getType() == Type::Int1Ty;
433 virtual void Apply(std::string &Field) {
434 Constant *C = CI->getOperand(I++);
436 (!C || isStringValue(C) || C->isNullValue());
438 virtual void Apply(DebugInfoDesc *&Field) {
439 // FIXME - Prepare the correct descriptor.
440 Constant *C = CI->getOperand(I++);
441 IsValid = IsValid && isGlobalVariable(C);
443 virtual void Apply(GlobalVariable *&Field) {
444 Constant *C = CI->getOperand(I++);
445 IsValid = IsValid && isGlobalVariable(C);
447 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
448 Constant *C = CI->getOperand(I++);
449 IsValid = IsValid && isGlobalVariable(C);
450 if (!IsValid) return;
452 GlobalVariable *GV = getGlobalVariable(C);
453 IsValid = IsValid && GV && GV->hasInitializer();
454 if (!IsValid) return;
456 ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer());
457 IsValid = IsValid && CA;
458 if (!IsValid) return;
460 for (unsigned i = 0, N = CA->getNumOperands(); IsValid && i < N; ++i) {
461 IsValid = IsValid && isGlobalVariable(CA->getOperand(i));
462 if (!IsValid) return;
464 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
471 //===----------------------------------------------------------------------===//
473 /// TagFromGlobal - Returns the tag number from a debug info descriptor
474 /// GlobalVariable. Return DIIValid if operand is not an unsigned int.
475 unsigned DebugInfoDesc::TagFromGlobal(GlobalVariable *GV) {
476 ConstantInt *C = getUIntOperand(GV, 0);
477 return C ? ((unsigned)C->getZExtValue() & ~LLVMDebugVersionMask) :
478 (unsigned)DW_TAG_invalid;
481 /// VersionFromGlobal - Returns the version number from a debug info
482 /// descriptor GlobalVariable. Return DIIValid if operand is not an unsigned
484 unsigned DebugInfoDesc::VersionFromGlobal(GlobalVariable *GV) {
485 ConstantInt *C = getUIntOperand(GV, 0);
486 return C ? ((unsigned)C->getZExtValue() & LLVMDebugVersionMask) :
487 (unsigned)DW_TAG_invalid;
490 /// DescFactory - Create an instance of debug info descriptor based on Tag.
491 /// Return NULL if not a recognized Tag.
492 DebugInfoDesc *DebugInfoDesc::DescFactory(unsigned Tag) {
494 case DW_TAG_anchor: return new AnchorDesc();
495 case DW_TAG_compile_unit: return new CompileUnitDesc();
496 case DW_TAG_variable: return new GlobalVariableDesc();
497 case DW_TAG_subprogram: return new SubprogramDesc();
498 case DW_TAG_lexical_block: return new BlockDesc();
499 case DW_TAG_base_type: return new BasicTypeDesc();
501 case DW_TAG_pointer_type:
502 case DW_TAG_reference_type:
503 case DW_TAG_const_type:
504 case DW_TAG_volatile_type:
505 case DW_TAG_restrict_type:
507 case DW_TAG_inheritance: return new DerivedTypeDesc(Tag);
508 case DW_TAG_array_type:
509 case DW_TAG_structure_type:
510 case DW_TAG_union_type:
511 case DW_TAG_enumeration_type:
512 case DW_TAG_vector_type:
513 case DW_TAG_subroutine_type: return new CompositeTypeDesc(Tag);
514 case DW_TAG_subrange_type: return new SubrangeDesc();
515 case DW_TAG_enumerator: return new EnumeratorDesc();
516 case DW_TAG_return_variable:
517 case DW_TAG_arg_variable:
518 case DW_TAG_auto_variable: return new VariableDesc(Tag);
524 /// getLinkage - get linkage appropriate for this type of descriptor.
526 GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const {
527 return GlobalValue::InternalLinkage;
530 /// ApplyToFields - Target the vistor to the fields of the descriptor.
532 void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) {
536 //===----------------------------------------------------------------------===//
538 AnchorDesc::AnchorDesc()
539 : DebugInfoDesc(DW_TAG_anchor)
542 AnchorDesc::AnchorDesc(AnchoredDesc *D)
543 : DebugInfoDesc(DW_TAG_anchor)
544 , AnchorTag(D->getTag())
547 // Implement isa/cast/dyncast.
548 bool AnchorDesc::classof(const DebugInfoDesc *D) {
549 return D->getTag() == DW_TAG_anchor;
552 /// getLinkage - get linkage appropriate for this type of descriptor.
554 GlobalValue::LinkageTypes AnchorDesc::getLinkage() const {
555 return GlobalValue::LinkOnceLinkage;
558 /// ApplyToFields - Target the visitor to the fields of the TransUnitDesc.
560 void AnchorDesc::ApplyToFields(DIVisitor *Visitor) {
561 DebugInfoDesc::ApplyToFields(Visitor);
563 Visitor->Apply(AnchorTag);
566 /// getDescString - Return a string used to compose global names and labels. A
567 /// A global variable name needs to be defined for each debug descriptor that is
568 /// anchored. NOTE: that each global variable named here also needs to be added
569 /// to the list of names left external in the internalizer.
570 /// ExternalNames.insert("llvm.dbg.compile_units");
571 /// ExternalNames.insert("llvm.dbg.global_variables");
572 /// ExternalNames.insert("llvm.dbg.subprograms");
573 const char *AnchorDesc::getDescString() const {
575 case DW_TAG_compile_unit: return CompileUnitDesc::AnchorString;
576 case DW_TAG_variable: return GlobalVariableDesc::AnchorString;
577 case DW_TAG_subprogram: return SubprogramDesc::AnchorString;
581 assert(0 && "Tag does not have a case for anchor string");
585 /// getTypeString - Return a string used to label this descriptors type.
587 const char *AnchorDesc::getTypeString() const {
588 return "llvm.dbg.anchor.type";
592 void AnchorDesc::dump() {
593 cerr << getDescString() << " "
594 << "Version(" << getVersion() << "), "
595 << "Tag(" << getTag() << "), "
596 << "AnchorTag(" << AnchorTag << ")\n";
600 //===----------------------------------------------------------------------===//
602 AnchoredDesc::AnchoredDesc(unsigned T)
607 /// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
609 void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) {
610 DebugInfoDesc::ApplyToFields(Visitor);
612 Visitor->Apply(Anchor);
615 //===----------------------------------------------------------------------===//
617 CompileUnitDesc::CompileUnitDesc()
618 : AnchoredDesc(DW_TAG_compile_unit)
625 // Implement isa/cast/dyncast.
626 bool CompileUnitDesc::classof(const DebugInfoDesc *D) {
627 return D->getTag() == DW_TAG_compile_unit;
630 /// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
632 void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) {
633 AnchoredDesc::ApplyToFields(Visitor);
635 // Handle cases out of sync with compiler.
636 if (getVersion() == 0) {
637 unsigned DebugVersion;
638 Visitor->Apply(DebugVersion);
641 Visitor->Apply(Language);
642 Visitor->Apply(FileName);
643 Visitor->Apply(Directory);
644 Visitor->Apply(Producer);
647 /// getDescString - Return a string used to compose global names and labels.
649 const char *CompileUnitDesc::getDescString() const {
650 return "llvm.dbg.compile_unit";
653 /// getTypeString - Return a string used to label this descriptors type.
655 const char *CompileUnitDesc::getTypeString() const {
656 return "llvm.dbg.compile_unit.type";
659 /// getAnchorString - Return a string used to label this descriptor's anchor.
661 const char *CompileUnitDesc::AnchorString = "llvm.dbg.compile_units";
662 const char *CompileUnitDesc::getAnchorString() const {
667 void CompileUnitDesc::dump() {
668 cerr << getDescString() << " "
669 << "Version(" << getVersion() << "), "
670 << "Tag(" << getTag() << "), "
671 << "Anchor(" << getAnchor() << "), "
672 << "Language(" << Language << "), "
673 << "FileName(\"" << FileName << "\"), "
674 << "Directory(\"" << Directory << "\"), "
675 << "Producer(\"" << Producer << "\")\n";
679 //===----------------------------------------------------------------------===//
681 TypeDesc::TypeDesc(unsigned T)
693 /// ApplyToFields - Target the visitor to the fields of the TypeDesc.
695 void TypeDesc::ApplyToFields(DIVisitor *Visitor) {
696 DebugInfoDesc::ApplyToFields(Visitor);
698 Visitor->Apply(Context);
699 Visitor->Apply(Name);
700 Visitor->Apply(File);
701 Visitor->Apply(Line);
702 Visitor->Apply(Size);
703 Visitor->Apply(Align);
704 Visitor->Apply(Offset);
705 if (getVersion() > LLVMDebugVersion4) Visitor->Apply(Flags);
708 /// getDescString - Return a string used to compose global names and labels.
710 const char *TypeDesc::getDescString() const {
711 return "llvm.dbg.type";
714 /// getTypeString - Return a string used to label this descriptor's type.
716 const char *TypeDesc::getTypeString() const {
717 return "llvm.dbg.type.type";
721 void TypeDesc::dump() {
722 cerr << getDescString() << " "
723 << "Version(" << getVersion() << "), "
724 << "Tag(" << getTag() << "), "
725 << "Context(" << Context << "), "
726 << "Name(\"" << Name << "\"), "
727 << "File(" << File << "), "
728 << "Line(" << Line << "), "
729 << "Size(" << Size << "), "
730 << "Align(" << Align << "), "
731 << "Offset(" << Offset << "), "
732 << "Flags(" << Flags << ")\n";
736 //===----------------------------------------------------------------------===//
738 BasicTypeDesc::BasicTypeDesc()
739 : TypeDesc(DW_TAG_base_type)
743 // Implement isa/cast/dyncast.
744 bool BasicTypeDesc::classof(const DebugInfoDesc *D) {
745 return D->getTag() == DW_TAG_base_type;
748 /// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
750 void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
751 TypeDesc::ApplyToFields(Visitor);
753 Visitor->Apply(Encoding);
756 /// getDescString - Return a string used to compose global names and labels.
758 const char *BasicTypeDesc::getDescString() const {
759 return "llvm.dbg.basictype";
762 /// getTypeString - Return a string used to label this descriptor's type.
764 const char *BasicTypeDesc::getTypeString() const {
765 return "llvm.dbg.basictype.type";
769 void BasicTypeDesc::dump() {
770 cerr << getDescString() << " "
771 << "Version(" << getVersion() << "), "
772 << "Tag(" << getTag() << "), "
773 << "Context(" << getContext() << "), "
774 << "Name(\"" << getName() << "\"), "
775 << "Size(" << getSize() << "), "
776 << "Encoding(" << Encoding << ")\n";
780 //===----------------------------------------------------------------------===//
782 DerivedTypeDesc::DerivedTypeDesc(unsigned T)
787 // Implement isa/cast/dyncast.
788 bool DerivedTypeDesc::classof(const DebugInfoDesc *D) {
789 unsigned T = D->getTag();
792 case DW_TAG_pointer_type:
793 case DW_TAG_reference_type:
794 case DW_TAG_const_type:
795 case DW_TAG_volatile_type:
796 case DW_TAG_restrict_type:
798 case DW_TAG_inheritance:
805 /// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
807 void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) {
808 TypeDesc::ApplyToFields(Visitor);
810 Visitor->Apply(FromType);
813 /// getDescString - Return a string used to compose global names and labels.
815 const char *DerivedTypeDesc::getDescString() const {
816 return "llvm.dbg.derivedtype";
819 /// getTypeString - Return a string used to label this descriptor's type.
821 const char *DerivedTypeDesc::getTypeString() const {
822 return "llvm.dbg.derivedtype.type";
826 void DerivedTypeDesc::dump() {
827 cerr << getDescString() << " "
828 << "Version(" << getVersion() << "), "
829 << "Tag(" << getTag() << "), "
830 << "Context(" << getContext() << "), "
831 << "Name(\"" << getName() << "\"), "
832 << "Size(" << getSize() << "), "
833 << "File(" << getFile() << "), "
834 << "Line(" << getLine() << "), "
835 << "FromType(" << FromType << ")\n";
839 //===----------------------------------------------------------------------===//
841 CompositeTypeDesc::CompositeTypeDesc(unsigned T)
846 // Implement isa/cast/dyncast.
847 bool CompositeTypeDesc::classof(const DebugInfoDesc *D) {
848 unsigned T = D->getTag();
850 case DW_TAG_array_type:
851 case DW_TAG_structure_type:
852 case DW_TAG_union_type:
853 case DW_TAG_enumeration_type:
854 case DW_TAG_vector_type:
855 case DW_TAG_subroutine_type:
862 /// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
864 void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) {
865 DerivedTypeDesc::ApplyToFields(Visitor);
867 Visitor->Apply(Elements);
870 /// getDescString - Return a string used to compose global names and labels.
872 const char *CompositeTypeDesc::getDescString() const {
873 return "llvm.dbg.compositetype";
876 /// getTypeString - Return a string used to label this descriptor's type.
878 const char *CompositeTypeDesc::getTypeString() const {
879 return "llvm.dbg.compositetype.type";
883 void CompositeTypeDesc::dump() {
884 cerr << getDescString() << " "
885 << "Version(" << getVersion() << "), "
886 << "Tag(" << getTag() << "), "
887 << "Context(" << getContext() << "), "
888 << "Name(\"" << getName() << "\"), "
889 << "Size(" << getSize() << "), "
890 << "File(" << getFile() << "), "
891 << "Line(" << getLine() << "), "
892 << "FromType(" << getFromType() << "), "
893 << "Elements.size(" << Elements.size() << ")\n";
897 //===----------------------------------------------------------------------===//
899 SubrangeDesc::SubrangeDesc()
900 : DebugInfoDesc(DW_TAG_subrange_type)
905 // Implement isa/cast/dyncast.
906 bool SubrangeDesc::classof(const DebugInfoDesc *D) {
907 return D->getTag() == DW_TAG_subrange_type;
910 /// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
912 void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) {
913 DebugInfoDesc::ApplyToFields(Visitor);
919 /// getDescString - Return a string used to compose global names and labels.
921 const char *SubrangeDesc::getDescString() const {
922 return "llvm.dbg.subrange";
925 /// getTypeString - Return a string used to label this descriptor's type.
927 const char *SubrangeDesc::getTypeString() const {
928 return "llvm.dbg.subrange.type";
932 void SubrangeDesc::dump() {
933 cerr << getDescString() << " "
934 << "Version(" << getVersion() << "), "
935 << "Tag(" << getTag() << "), "
936 << "Lo(" << Lo << "), "
937 << "Hi(" << Hi << ")\n";
941 //===----------------------------------------------------------------------===//
943 EnumeratorDesc::EnumeratorDesc()
944 : DebugInfoDesc(DW_TAG_enumerator)
949 // Implement isa/cast/dyncast.
950 bool EnumeratorDesc::classof(const DebugInfoDesc *D) {
951 return D->getTag() == DW_TAG_enumerator;
954 /// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
956 void EnumeratorDesc::ApplyToFields(DIVisitor *Visitor) {
957 DebugInfoDesc::ApplyToFields(Visitor);
959 Visitor->Apply(Name);
960 Visitor->Apply(Value);
963 /// getDescString - Return a string used to compose global names and labels.
965 const char *EnumeratorDesc::getDescString() const {
966 return "llvm.dbg.enumerator";
969 /// getTypeString - Return a string used to label this descriptor's type.
971 const char *EnumeratorDesc::getTypeString() const {
972 return "llvm.dbg.enumerator.type";
976 void EnumeratorDesc::dump() {
977 cerr << getDescString() << " "
978 << "Version(" << getVersion() << "), "
979 << "Tag(" << getTag() << "), "
980 << "Name(" << Name << "), "
981 << "Value(" << Value << ")\n";
985 //===----------------------------------------------------------------------===//
987 VariableDesc::VariableDesc(unsigned T)
996 // Implement isa/cast/dyncast.
997 bool VariableDesc::classof(const DebugInfoDesc *D) {
998 unsigned T = D->getTag();
1000 case DW_TAG_auto_variable:
1001 case DW_TAG_arg_variable:
1002 case DW_TAG_return_variable:
1009 /// ApplyToFields - Target the visitor to the fields of the VariableDesc.
1011 void VariableDesc::ApplyToFields(DIVisitor *Visitor) {
1012 DebugInfoDesc::ApplyToFields(Visitor);
1014 Visitor->Apply(Context);
1015 Visitor->Apply(Name);
1016 Visitor->Apply(File);
1017 Visitor->Apply(Line);
1018 Visitor->Apply(TyDesc);
1021 /// getDescString - Return a string used to compose global names and labels.
1023 const char *VariableDesc::getDescString() const {
1024 return "llvm.dbg.variable";
1027 /// getTypeString - Return a string used to label this descriptor's type.
1029 const char *VariableDesc::getTypeString() const {
1030 return "llvm.dbg.variable.type";
1034 void VariableDesc::dump() {
1035 cerr << getDescString() << " "
1036 << "Version(" << getVersion() << "), "
1037 << "Tag(" << getTag() << "), "
1038 << "Context(" << Context << "), "
1039 << "Name(\"" << Name << "\"), "
1040 << "File(" << File << "), "
1041 << "Line(" << Line << "), "
1042 << "TyDesc(" << TyDesc << ")\n";
1046 //===----------------------------------------------------------------------===//
1048 GlobalDesc::GlobalDesc(unsigned T)
1058 , IsDefinition(false)
1061 /// ApplyToFields - Target the visitor to the fields of the global.
1063 void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
1064 AnchoredDesc::ApplyToFields(Visitor);
1066 Visitor->Apply(Context);
1067 Visitor->Apply(Name);
1068 Visitor->Apply(FullName);
1069 Visitor->Apply(LinkageName);
1070 Visitor->Apply(File);
1071 Visitor->Apply(Line);
1072 Visitor->Apply(TyDesc);
1073 Visitor->Apply(IsStatic);
1074 Visitor->Apply(IsDefinition);
1077 //===----------------------------------------------------------------------===//
1079 GlobalVariableDesc::GlobalVariableDesc()
1080 : GlobalDesc(DW_TAG_variable)
1084 // Implement isa/cast/dyncast.
1085 bool GlobalVariableDesc::classof(const DebugInfoDesc *D) {
1086 return D->getTag() == DW_TAG_variable;
1089 /// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
1091 void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
1092 GlobalDesc::ApplyToFields(Visitor);
1094 Visitor->Apply(Global);
1097 /// getDescString - Return a string used to compose global names and labels.
1099 const char *GlobalVariableDesc::getDescString() const {
1100 return "llvm.dbg.global_variable";
1103 /// getTypeString - Return a string used to label this descriptors type.
1105 const char *GlobalVariableDesc::getTypeString() const {
1106 return "llvm.dbg.global_variable.type";
1109 /// getAnchorString - Return a string used to label this descriptor's anchor.
1111 const char *GlobalVariableDesc::AnchorString = "llvm.dbg.global_variables";
1112 const char *GlobalVariableDesc::getAnchorString() const {
1113 return AnchorString;
1117 void GlobalVariableDesc::dump() {
1118 cerr << getDescString() << " "
1119 << "Version(" << getVersion() << "), "
1120 << "Tag(" << getTag() << "), "
1121 << "Anchor(" << getAnchor() << "), "
1122 << "Name(\"" << getName() << "\"), "
1123 << "FullName(\"" << getFullName() << "\"), "
1124 << "LinkageName(\"" << getLinkageName() << "\"), "
1125 << "File(" << getFile() << "),"
1126 << "Line(" << getLine() << "),"
1127 << "Type(" << getType() << "), "
1128 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1129 << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
1130 << "Global(" << Global << ")\n";
1134 //===----------------------------------------------------------------------===//
1136 SubprogramDesc::SubprogramDesc()
1137 : GlobalDesc(DW_TAG_subprogram)
1140 // Implement isa/cast/dyncast.
1141 bool SubprogramDesc::classof(const DebugInfoDesc *D) {
1142 return D->getTag() == DW_TAG_subprogram;
1145 /// ApplyToFields - Target the visitor to the fields of the
1147 void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
1148 GlobalDesc::ApplyToFields(Visitor);
1151 /// getDescString - Return a string used to compose global names and labels.
1153 const char *SubprogramDesc::getDescString() const {
1154 return "llvm.dbg.subprogram";
1157 /// getTypeString - Return a string used to label this descriptors type.
1159 const char *SubprogramDesc::getTypeString() const {
1160 return "llvm.dbg.subprogram.type";
1163 /// getAnchorString - Return a string used to label this descriptor's anchor.
1165 const char *SubprogramDesc::AnchorString = "llvm.dbg.subprograms";
1166 const char *SubprogramDesc::getAnchorString() const {
1167 return AnchorString;
1171 void SubprogramDesc::dump() {
1172 cerr << getDescString() << " "
1173 << "Version(" << getVersion() << "), "
1174 << "Tag(" << getTag() << "), "
1175 << "Anchor(" << getAnchor() << "), "
1176 << "Name(\"" << getName() << "\"), "
1177 << "FullName(\"" << getFullName() << "\"), "
1178 << "LinkageName(\"" << getLinkageName() << "\"), "
1179 << "File(" << getFile() << "),"
1180 << "Line(" << getLine() << "),"
1181 << "Type(" << getType() << "), "
1182 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1183 << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
1187 //===----------------------------------------------------------------------===//
1189 BlockDesc::BlockDesc()
1190 : DebugInfoDesc(DW_TAG_lexical_block)
1194 // Implement isa/cast/dyncast.
1195 bool BlockDesc::classof(const DebugInfoDesc *D) {
1196 return D->getTag() == DW_TAG_lexical_block;
1199 /// ApplyToFields - Target the visitor to the fields of the BlockDesc.
1201 void BlockDesc::ApplyToFields(DIVisitor *Visitor) {
1202 DebugInfoDesc::ApplyToFields(Visitor);
1204 Visitor->Apply(Context);
1207 /// getDescString - Return a string used to compose global names and labels.
1209 const char *BlockDesc::getDescString() const {
1210 return "llvm.dbg.block";
1213 /// getTypeString - Return a string used to label this descriptors type.
1215 const char *BlockDesc::getTypeString() const {
1216 return "llvm.dbg.block.type";
1220 void BlockDesc::dump() {
1221 cerr << getDescString() << " "
1222 << "Version(" << getVersion() << "), "
1223 << "Tag(" << getTag() << "),"
1224 << "Context(" << Context << ")\n";
1228 //===----------------------------------------------------------------------===//
1230 DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
1231 return Deserialize(getGlobalVariable(V));
1233 DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
1235 if (!GV) return NULL;
1237 // Check to see if it has been already deserialized.
1238 DebugInfoDesc *&Slot = GlobalDescs[GV];
1239 if (Slot) return Slot;
1241 // Get the Tag from the global.
1242 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1244 // Create an empty instance of the correct sort.
1245 Slot = DebugInfoDesc::DescFactory(Tag);
1247 // If not a user defined descriptor.
1249 // Deserialize the fields.
1250 DIDeserializeVisitor DRAM(*this, GV);
1251 DRAM.ApplyToFields(Slot);
1257 //===----------------------------------------------------------------------===//
1259 /// getStrPtrType - Return a "sbyte *" type.
1261 const PointerType *DISerializer::getStrPtrType() {
1262 // If not already defined.
1264 // Construct the pointer to signed bytes.
1265 StrPtrTy = PointerType::get(Type::Int8Ty);
1271 /// getEmptyStructPtrType - Return a "{ }*" type.
1273 const PointerType *DISerializer::getEmptyStructPtrType() {
1274 // If not already defined.
1275 if (!EmptyStructPtrTy) {
1276 // Construct the empty structure type.
1277 const StructType *EmptyStructTy =
1278 StructType::get(std::vector<const Type*>());
1279 // Construct the pointer to empty structure type.
1280 EmptyStructPtrTy = PointerType::get(EmptyStructTy);
1283 return EmptyStructPtrTy;
1286 /// getTagType - Return the type describing the specified descriptor (via tag.)
1288 const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
1289 // Attempt to get the previously defined type.
1290 StructType *&Ty = TagTypes[DD->getTag()];
1292 // If not already defined.
1294 // Set up fields vector.
1295 std::vector<const Type*> Fields;
1296 // Get types of fields.
1297 DIGetTypesVisitor GTAM(*this, Fields);
1298 GTAM.ApplyToFields(DD);
1300 // Construct structured type.
1301 Ty = StructType::get(Fields);
1303 // Register type name with module.
1304 M->addTypeName(DD->getTypeString(), Ty);
1310 /// getString - Construct the string as constant string global.
1312 Constant *DISerializer::getString(const std::string &String) {
1313 // Check string cache for previous edition.
1314 Constant *&Slot = StringCache[String];
1315 // Return Constant if previously defined.
1316 if (Slot) return Slot;
1317 // If empty string then use a sbyte* null instead.
1318 if (String.empty()) {
1319 Slot = ConstantPointerNull::get(getStrPtrType());
1321 // Construct string as an llvm constant.
1322 Constant *ConstStr = ConstantArray::get(String);
1323 // Otherwise create and return a new string global.
1324 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
1325 GlobalVariable::InternalLinkage,
1326 ConstStr, ".str", M);
1327 StrGV->setSection("llvm.metadata");
1328 // Convert to generic string pointer.
1329 Slot = ConstantExpr::getBitCast(StrGV, getStrPtrType());
1335 /// Serialize - Recursively cast the specified descriptor into a GlobalVariable
1336 /// so that it can be serialized to a .bc or .ll file.
1337 GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
1338 // Check if the DebugInfoDesc is already in the map.
1339 GlobalVariable *&Slot = DescGlobals[DD];
1341 // See if DebugInfoDesc exists, if so return prior GlobalVariable.
1342 if (Slot) return Slot;
1344 // Get the type associated with the Tag.
1345 const StructType *Ty = getTagType(DD);
1347 // Create the GlobalVariable early to prevent infinite recursion.
1348 GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(),
1349 NULL, DD->getDescString(), M);
1350 GV->setSection("llvm.metadata");
1352 // Insert new GlobalVariable in DescGlobals map.
1355 // Set up elements vector
1356 std::vector<Constant*> Elements;
1358 DISerializeVisitor SRAM(*this, Elements);
1359 SRAM.ApplyToFields(DD);
1361 // Set the globals initializer.
1362 GV->setInitializer(ConstantStruct::get(Ty, Elements));
1367 //===----------------------------------------------------------------------===//
1369 /// Verify - Return true if the GlobalVariable appears to be a valid
1370 /// serialization of a DebugInfoDesc.
1371 bool DIVerifier::Verify(Value *V) {
1372 return !V || Verify(getGlobalVariable(V));
1374 bool DIVerifier::Verify(GlobalVariable *GV) {
1376 if (!GV) return true;
1378 // Check prior validity.
1379 unsigned &ValiditySlot = Validity[GV];
1381 // If visited before then use old state.
1382 if (ValiditySlot) return ValiditySlot == Valid;
1384 // Assume validity for the time being (recursion.)
1385 ValiditySlot = Valid;
1387 // Make sure the global is internal or link once (anchor.)
1388 if (GV->getLinkage() != GlobalValue::InternalLinkage &&
1389 GV->getLinkage() != GlobalValue::LinkOnceLinkage) {
1390 ValiditySlot = Invalid;
1395 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1397 // Check for user defined descriptors.
1398 if (Tag == DW_TAG_invalid) {
1399 ValiditySlot = Valid;
1404 unsigned Version = DebugInfoDesc::VersionFromGlobal(GV);
1406 // Check for version mismatch.
1407 if (Version != LLVMDebugVersion) {
1408 ValiditySlot = Invalid;
1412 // Construct an empty DebugInfoDesc.
1413 DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
1415 // Allow for user defined descriptors.
1416 if (!DD) return true;
1418 // Get the initializer constant.
1419 ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
1421 // Get the operand count.
1422 unsigned N = CI->getNumOperands();
1424 // Get the field count.
1425 unsigned &CountSlot = Counts[Tag];
1427 // Check the operand count to the field count
1428 DICountVisitor CTAM;
1429 CTAM.ApplyToFields(DD);
1430 CountSlot = CTAM.getCount();
1433 // Field count must be at most equal operand count.
1434 if (CountSlot > N) {
1436 ValiditySlot = Invalid;
1440 // Check each field for valid type.
1441 DIVerifyVisitor VRAM(*this, GV);
1442 VRAM.ApplyToFields(DD);
1444 // Release empty DebugInfoDesc.
1447 // If fields are not valid.
1448 if (!VRAM.isValid()) {
1449 ValiditySlot = Invalid;
1456 //===----------------------------------------------------------------------===//
1458 DebugScope::~DebugScope() {
1459 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1460 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
1463 //===----------------------------------------------------------------------===//
1465 MachineModuleInfo::MachineModuleInfo()
1466 : ImmutablePass((intptr_t)&ID)
1480 // Always emit "no personality" info
1481 Personalities.push_back(NULL);
1483 MachineModuleInfo::~MachineModuleInfo() {
1487 /// doInitialization - Initialize the state for a new module.
1489 bool MachineModuleInfo::doInitialization() {
1493 /// doFinalization - Tear down the state after completion of a module.
1495 bool MachineModuleInfo::doFinalization() {
1499 /// BeginFunction - Begin gathering function meta information.
1501 void MachineModuleInfo::BeginFunction(MachineFunction *MF) {
1505 /// EndFunction - Discard function meta information.
1507 void MachineModuleInfo::EndFunction() {
1508 // Clean up scope information.
1515 // Clean up line info.
1518 // Clean up frame info.
1521 // Clean up exception info.
1522 LandingPads.clear();
1528 /// getDescFor - Convert a Value to a debug information descriptor.
1530 // FIXME - use new Value type when available.
1531 DebugInfoDesc *MachineModuleInfo::getDescFor(Value *V) {
1532 return DR.Deserialize(V);
1535 /// Verify - Verify that a Value is debug information descriptor.
1537 bool MachineModuleInfo::Verify(Value *V) {
1538 return VR.Verify(V);
1541 /// AnalyzeModule - Scan the module for global debug information.
1543 void MachineModuleInfo::AnalyzeModule(Module &M) {
1544 SetupCompileUnits(M);
1547 /// needsFrameInfo - Returns true if we need to gather callee-saved register
1548 /// move info for the frame.
1549 bool MachineModuleInfo::needsFrameInfo() const {
1550 return hasDebugInfo() || ExceptionHandling;
1553 /// SetupCompileUnits - Set up the unique vector of compile units.
1555 void MachineModuleInfo::SetupCompileUnits(Module &M) {
1556 std::vector<CompileUnitDesc *>CU = getAnchoredDescriptors<CompileUnitDesc>(M);
1558 for (unsigned i = 0, N = CU.size(); i < N; i++) {
1559 CompileUnits.insert(CU[i]);
1563 /// getCompileUnits - Return a vector of debug compile units.
1565 const UniqueVector<CompileUnitDesc *> MachineModuleInfo::getCompileUnits()const{
1566 return CompileUnits;
1569 /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1570 /// named GlobalVariable.
1571 std::vector<GlobalVariable*>
1572 MachineModuleInfo::getGlobalVariablesUsing(Module &M,
1573 const std::string &RootName) {
1574 return ::getGlobalVariablesUsing(M, RootName);
1577 /// RecordLabel - Records location information and associates it with a
1578 /// debug label. Returns a unique label ID used to generate a label and
1579 /// provide correspondence to the source line list.
1580 unsigned MachineModuleInfo::RecordLabel(unsigned Line, unsigned Column,
1582 unsigned ID = NextLabelID();
1583 Lines.push_back(SourceLineInfo(Line, Column, Source, ID));
1587 /// RecordSource - Register a source file with debug info. Returns an source
1589 unsigned MachineModuleInfo::RecordSource(const std::string &Directory,
1590 const std::string &Source) {
1591 unsigned DirectoryID = Directories.insert(Directory);
1592 return SourceFiles.insert(SourceFileInfo(DirectoryID, Source));
1594 unsigned MachineModuleInfo::RecordSource(const CompileUnitDesc *CompileUnit) {
1595 return RecordSource(CompileUnit->getDirectory(),
1596 CompileUnit->getFileName());
1599 /// RecordRegionStart - Indicate the start of a region.
1601 unsigned MachineModuleInfo::RecordRegionStart(Value *V) {
1602 // FIXME - need to be able to handle split scopes because of bb cloning.
1603 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1604 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1605 unsigned ID = NextLabelID();
1606 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
1610 /// RecordRegionEnd - Indicate the end of a region.
1612 unsigned MachineModuleInfo::RecordRegionEnd(Value *V) {
1613 // FIXME - need to be able to handle split scopes because of bb cloning.
1614 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1615 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1616 unsigned ID = NextLabelID();
1617 Scope->setEndLabelID(ID);
1621 /// RecordVariable - Indicate the declaration of a local variable.
1623 void MachineModuleInfo::RecordVariable(Value *V, unsigned FrameIndex) {
1624 VariableDesc *VD = cast<VariableDesc>(DR.Deserialize(V));
1625 DebugScope *Scope = getOrCreateScope(VD->getContext());
1626 DebugVariable *DV = new DebugVariable(VD, FrameIndex);
1627 Scope->AddVariable(DV);
1630 /// getOrCreateScope - Returns the scope associated with the given descriptor.
1632 DebugScope *MachineModuleInfo::getOrCreateScope(DebugInfoDesc *ScopeDesc) {
1633 DebugScope *&Slot = ScopeMap[ScopeDesc];
1635 // FIXME - breaks down when the context is an inlined function.
1636 DebugInfoDesc *ParentDesc = NULL;
1637 if (BlockDesc *Block = dyn_cast<BlockDesc>(ScopeDesc)) {
1638 ParentDesc = Block->getContext();
1640 DebugScope *Parent = ParentDesc ? getOrCreateScope(ParentDesc) : NULL;
1641 Slot = new DebugScope(Parent, ScopeDesc);
1643 Parent->AddScope(Slot);
1644 } else if (RootScope) {
1645 // FIXME - Add inlined function scopes to the root so we can delete
1646 // them later. Long term, handle inlined functions properly.
1647 RootScope->AddScope(Slot);
1649 // First function is top level function.
1656 //===-EH-------------------------------------------------------------------===//
1658 /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
1659 /// specified MachineBasicBlock.
1660 LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo
1661 (MachineBasicBlock *LandingPad) {
1662 unsigned N = LandingPads.size();
1663 for (unsigned i = 0; i < N; ++i) {
1664 LandingPadInfo &LP = LandingPads[i];
1665 if (LP.LandingPadBlock == LandingPad)
1669 LandingPads.push_back(LandingPadInfo(LandingPad));
1670 return LandingPads[N];
1673 /// addInvoke - Provide the begin and end labels of an invoke style call and
1674 /// associate it with a try landing pad block.
1675 void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad,
1676 unsigned BeginLabel, unsigned EndLabel) {
1677 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1678 LP.BeginLabels.push_back(BeginLabel);
1679 LP.EndLabels.push_back(EndLabel);
1682 /// addLandingPad - Provide the label of a try LandingPad block.
1684 unsigned MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) {
1685 unsigned LandingPadLabel = NextLabelID();
1686 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1687 LP.LandingPadLabel = LandingPadLabel;
1688 return LandingPadLabel;
1691 /// addPersonality - Provide the personality function for the exception
1693 void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad,
1694 Function *Personality) {
1695 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1696 LP.Personality = Personality;
1698 for (unsigned i = 0; i < Personalities.size(); ++i)
1699 if (Personalities[i] == Personality)
1702 Personalities.push_back(Personality);
1705 /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
1707 void MachineModuleInfo::addCatchTypeInfo(MachineBasicBlock *LandingPad,
1708 std::vector<GlobalVariable *> &TyInfo) {
1709 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1710 for (unsigned N = TyInfo.size(); N; --N)
1711 LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
1714 /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
1716 void MachineModuleInfo::addFilterTypeInfo(MachineBasicBlock *LandingPad,
1717 std::vector<GlobalVariable *> &TyInfo) {
1718 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1719 std::vector<unsigned> IdsInFilter (TyInfo.size());
1720 for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
1721 IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
1722 LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
1725 /// TidyLandingPads - Remap landing pad labels and remove any deleted landing
1727 void MachineModuleInfo::TidyLandingPads() {
1728 for (unsigned i = 0; i != LandingPads.size(); ) {
1729 LandingPadInfo &LandingPad = LandingPads[i];
1730 LandingPad.LandingPadLabel = MappedLabel(LandingPad.LandingPadLabel);
1732 if (!LandingPad.LandingPadBlock)
1733 // Must not have cleanups if no landing pad.
1734 LandingPad.TypeIds.clear();
1736 // Special case: we *should* emit LPs with null LP MBB. This indicates
1738 if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
1739 LandingPads.erase(LandingPads.begin() + i);
1743 for (unsigned j=0; j != LandingPads[i].BeginLabels.size(); ) {
1744 unsigned BeginLabel = MappedLabel(LandingPad.BeginLabels[j]);
1745 unsigned EndLabel = MappedLabel(LandingPad.EndLabels[j]);
1748 if (!BeginLabel || !EndLabel) {
1749 LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
1750 LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
1754 LandingPad.BeginLabels[j] = BeginLabel;
1755 LandingPad.EndLabels[j] = EndLabel;
1763 /// getTypeIDFor - Return the type id for the specified typeinfo. This is
1765 unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) {
1766 for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
1767 if (TypeInfos[i] == TI) return i + 1;
1769 TypeInfos.push_back(TI);
1770 return TypeInfos.size();
1773 /// getFilterIDFor - Return the filter id for the specified typeinfos. This is
1775 int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) {
1776 // If the new filter coincides with the tail of an existing filter, then
1777 // re-use the existing filter. Folding filters more than this requires
1778 // re-ordering filters and/or their elements - probably not worth it.
1779 for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
1780 E = FilterEnds.end(); I != E; ++I) {
1781 unsigned i = *I, j = TyIds.size();
1784 if (FilterIds[--i] != TyIds[--j])
1788 // The new filter coincides with range [i, end) of the existing filter.
1794 // Add the new filter.
1795 int FilterID = -(1 + FilterIds.size());
1796 FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
1797 for (unsigned I = 0, N = TyIds.size(); I != N; ++I)
1798 FilterIds.push_back(TyIds[I]);
1799 FilterEnds.push_back(FilterIds.size());
1800 FilterIds.push_back(0); // terminator
1804 /// getPersonality - Return the personality function for the current function.
1805 Function *MachineModuleInfo::getPersonality() const {
1806 // FIXME: Until PR1414 will be fixed, we're using 1 personality function per
1808 return !LandingPads.empty() ? LandingPads[0].Personality : NULL;
1811 /// getPersonalityIndex - Return unique index for current personality
1812 /// function. NULL personality function should always get zero index.
1813 unsigned MachineModuleInfo::getPersonalityIndex() const {
1814 const Function* Personality = NULL;
1816 // Scan landing pads. If there is at least one non-NULL personality - use it.
1817 for (unsigned i = 0; i != LandingPads.size(); ++i)
1818 if (LandingPads[i].Personality) {
1819 Personality = LandingPads[i].Personality;
1823 for (unsigned i = 0; i < Personalities.size(); ++i) {
1824 if (Personalities[i] == Personality)
1828 // This should never happen
1829 assert(0 && "Personality function should be set!");
1833 //===----------------------------------------------------------------------===//
1834 /// DebugLabelFolding pass - This pass prunes out redundant labels. This allows
1835 /// a info consumer to determine if the range of two labels is empty, by seeing
1836 /// if the labels map to the same reduced label.
1840 struct DebugLabelFolder : public MachineFunctionPass {
1842 DebugLabelFolder() : MachineFunctionPass((intptr_t)&ID) {}
1844 virtual bool runOnMachineFunction(MachineFunction &MF);
1845 virtual const char *getPassName() const { return "Label Folder"; }
1848 char DebugLabelFolder::ID = 0;
1850 bool DebugLabelFolder::runOnMachineFunction(MachineFunction &MF) {
1851 // Get machine module info.
1852 MachineModuleInfo *MMI = getAnalysisToUpdate<MachineModuleInfo>();
1853 if (!MMI) return false;
1854 // Get target instruction info.
1855 const TargetInstrInfo *TII = MF.getTarget().getInstrInfo();
1856 if (!TII) return false;
1858 // Track if change is made.
1859 bool MadeChange = false;
1860 // No prior label to begin.
1861 unsigned PriorLabel = 0;
1863 // Iterate through basic blocks.
1864 for (MachineFunction::iterator BB = MF.begin(), E = MF.end();
1866 // Iterate through instructions.
1867 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1869 if ((unsigned)I->getOpcode() == TargetInstrInfo::LABEL) {
1870 // The label ID # is always operand #0, an immediate.
1871 unsigned NextLabel = I->getOperand(0).getImm();
1873 // If there was an immediate prior label.
1875 // Remap the current label to prior label.
1876 MMI->RemapLabel(NextLabel, PriorLabel);
1877 // Delete the current label.
1879 // Indicate a change has been made.
1883 // Start a new round.
1884 PriorLabel = NextLabel;
1887 // No consecutive labels.
1898 FunctionPass *createDebugLabelFoldingPass() { return new DebugLabelFolder(); }