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.
30 static RegisterPass<MachineModuleInfo>
31 X("machinemoduleinfo", "Module Information");
32 char MachineModuleInfo::ID = 0;
34 //===----------------------------------------------------------------------===//
36 /// getGlobalVariablesUsing - Return all of the GlobalVariables which have the
37 /// specified value in their initializer somewhere.
39 getGlobalVariablesUsing(Value *V, std::vector<GlobalVariable*> &Result) {
40 // Scan though value users.
41 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
42 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I)) {
43 // If the user is a GlobalVariable then add to result.
45 } else if (Constant *C = dyn_cast<Constant>(*I)) {
46 // If the user is a constant variable then scan its users
47 getGlobalVariablesUsing(C, Result);
52 /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
53 /// named GlobalVariable.
54 static std::vector<GlobalVariable*>
55 getGlobalVariablesUsing(Module &M, const std::string &RootName) {
56 std::vector<GlobalVariable*> Result; // GlobalVariables matching criteria.
58 std::vector<const Type*> FieldTypes;
59 FieldTypes.push_back(Type::Int32Ty);
60 FieldTypes.push_back(Type::Int32Ty);
62 // Get the GlobalVariable root.
63 GlobalVariable *UseRoot = M.getGlobalVariable(RootName,
64 StructType::get(FieldTypes));
66 // If present and linkonce then scan for users.
67 if (UseRoot && UseRoot->hasLinkOnceLinkage()) {
68 getGlobalVariablesUsing(UseRoot, Result);
74 /// isStringValue - Return true if the given value can be coerced to a string.
76 static bool isStringValue(Value *V) {
77 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
78 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
79 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
80 return Init->isString();
82 } else if (Constant *C = dyn_cast<Constant>(V)) {
83 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
84 return isStringValue(GV);
85 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
86 if (CE->getOpcode() == Instruction::GetElementPtr) {
87 if (CE->getNumOperands() == 3 &&
88 cast<Constant>(CE->getOperand(1))->isNullValue() &&
89 isa<ConstantInt>(CE->getOperand(2))) {
90 return isStringValue(CE->getOperand(0));
98 /// getGlobalVariable - Return either a direct or cast Global value.
100 static GlobalVariable *getGlobalVariable(Value *V) {
101 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
103 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
104 if (CE->getOpcode() == Instruction::BitCast) {
105 return dyn_cast<GlobalVariable>(CE->getOperand(0));
106 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
107 for (unsigned int i=1; i<CE->getNumOperands(); i++) {
108 if (!CE->getOperand(i)->isNullValue())
111 return dyn_cast<GlobalVariable>(CE->getOperand(0));
117 /// isGlobalVariable - Return true if the given value can be coerced to a
119 static bool isGlobalVariable(Value *V) {
120 if (isa<GlobalVariable>(V) || isa<ConstantPointerNull>(V)) {
122 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
123 if (CE->getOpcode() == Instruction::BitCast) {
124 return isa<GlobalVariable>(CE->getOperand(0));
125 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
126 for (unsigned int i=1; i<CE->getNumOperands(); i++) {
127 if (!CE->getOperand(i)->isNullValue())
130 return isa<GlobalVariable>(CE->getOperand(0));
136 /// getUIntOperand - Return ith operand if it is an unsigned integer.
138 static ConstantInt *getUIntOperand(GlobalVariable *GV, unsigned i) {
139 // Make sure the GlobalVariable has an initializer.
140 if (!GV->hasInitializer()) return NULL;
142 // Get the initializer constant.
143 ConstantStruct *CI = dyn_cast<ConstantStruct>(GV->getInitializer());
144 if (!CI) return NULL;
146 // Check if there is at least i + 1 operands.
147 unsigned N = CI->getNumOperands();
148 if (i >= N) return NULL;
151 return dyn_cast<ConstantInt>(CI->getOperand(i));
154 //===----------------------------------------------------------------------===//
156 /// ApplyToFields - Target the visitor to each field of the debug information
158 void DIVisitor::ApplyToFields(DebugInfoDesc *DD) {
159 DD->ApplyToFields(this);
164 //===----------------------------------------------------------------------===//
165 /// DICountVisitor - This DIVisitor counts all the fields in the supplied debug
166 /// the supplied DebugInfoDesc.
167 class DICountVisitor : public DIVisitor {
169 unsigned Count; // Running count of fields.
172 DICountVisitor() : DIVisitor(), Count(0) {}
175 unsigned getCount() const { return Count; }
177 /// Apply - Count each of the fields.
179 virtual void Apply(int &Field) { ++Count; }
180 virtual void Apply(unsigned &Field) { ++Count; }
181 virtual void Apply(int64_t &Field) { ++Count; }
182 virtual void Apply(uint64_t &Field) { ++Count; }
183 virtual void Apply(bool &Field) { ++Count; }
184 virtual void Apply(std::string &Field) { ++Count; }
185 virtual void Apply(DebugInfoDesc *&Field) { ++Count; }
186 virtual void Apply(GlobalVariable *&Field) { ++Count; }
187 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
192 //===----------------------------------------------------------------------===//
193 /// DIDeserializeVisitor - This DIVisitor deserializes all the fields in the
194 /// supplied DebugInfoDesc.
195 class DIDeserializeVisitor : public DIVisitor {
197 DIDeserializer &DR; // Active deserializer.
198 unsigned I; // Current operand index.
199 ConstantStruct *CI; // GlobalVariable constant initializer.
202 DIDeserializeVisitor(DIDeserializer &D, GlobalVariable *GV)
206 , CI(cast<ConstantStruct>(GV->getInitializer()))
209 /// Apply - Set the value of each of the fields.
211 virtual void Apply(int &Field) {
212 Constant *C = CI->getOperand(I++);
213 Field = cast<ConstantInt>(C)->getSExtValue();
215 virtual void Apply(unsigned &Field) {
216 Constant *C = CI->getOperand(I++);
217 Field = cast<ConstantInt>(C)->getZExtValue();
219 virtual void Apply(int64_t &Field) {
220 Constant *C = CI->getOperand(I++);
221 Field = cast<ConstantInt>(C)->getSExtValue();
223 virtual void Apply(uint64_t &Field) {
224 Constant *C = CI->getOperand(I++);
225 Field = cast<ConstantInt>(C)->getZExtValue();
227 virtual void Apply(bool &Field) {
228 Constant *C = CI->getOperand(I++);
229 Field = cast<ConstantInt>(C)->getZExtValue();
231 virtual void Apply(std::string &Field) {
232 Constant *C = CI->getOperand(I++);
233 Field = C->getStringValue();
235 virtual void Apply(DebugInfoDesc *&Field) {
236 Constant *C = CI->getOperand(I++);
237 Field = DR.Deserialize(C);
239 virtual void Apply(GlobalVariable *&Field) {
240 Constant *C = CI->getOperand(I++);
241 Field = getGlobalVariable(C);
243 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
245 Constant *C = CI->getOperand(I++);
246 GlobalVariable *GV = getGlobalVariable(C);
247 if (GV->hasInitializer()) {
248 if (ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer())) {
249 for (unsigned i = 0, N = CA->getNumOperands(); i < N; ++i) {
250 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
251 DebugInfoDesc *DE = DR.Deserialize(GVE);
254 } else if (GV->getInitializer()->isNullValue()) {
255 if (const ArrayType *T =
256 dyn_cast<ArrayType>(GV->getType()->getElementType())) {
257 Field.resize(T->getNumElements());
264 //===----------------------------------------------------------------------===//
265 /// DISerializeVisitor - This DIVisitor serializes all the fields in
266 /// the supplied DebugInfoDesc.
267 class DISerializeVisitor : public DIVisitor {
269 DISerializer &SR; // Active serializer.
270 std::vector<Constant*> &Elements; // Element accumulator.
273 DISerializeVisitor(DISerializer &S, std::vector<Constant*> &E)
279 /// Apply - Set the value of each of the fields.
281 virtual void Apply(int &Field) {
282 Elements.push_back(ConstantInt::get(Type::Int32Ty, int32_t(Field)));
284 virtual void Apply(unsigned &Field) {
285 Elements.push_back(ConstantInt::get(Type::Int32Ty, uint32_t(Field)));
287 virtual void Apply(int64_t &Field) {
288 Elements.push_back(ConstantInt::get(Type::Int64Ty, int64_t(Field)));
290 virtual void Apply(uint64_t &Field) {
291 Elements.push_back(ConstantInt::get(Type::Int64Ty, uint64_t(Field)));
293 virtual void Apply(bool &Field) {
294 Elements.push_back(ConstantInt::get(Type::Int1Ty, Field));
296 virtual void Apply(std::string &Field) {
297 Elements.push_back(SR.getString(Field));
299 virtual void Apply(DebugInfoDesc *&Field) {
300 GlobalVariable *GV = NULL;
302 // If non-NULL then convert to global.
303 if (Field) GV = SR.Serialize(Field);
305 // FIXME - At some point should use specific type.
306 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
309 // Set to pointer to global.
310 Elements.push_back(ConstantExpr::getBitCast(GV, EmptyTy));
313 Elements.push_back(ConstantPointerNull::get(EmptyTy));
316 virtual void Apply(GlobalVariable *&Field) {
317 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
319 Elements.push_back(ConstantExpr::getBitCast(Field, EmptyTy));
321 Elements.push_back(ConstantPointerNull::get(EmptyTy));
324 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
325 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
326 unsigned N = Field.size();
327 ArrayType *AT = ArrayType::get(EmptyTy, N);
328 std::vector<Constant *> ArrayElements;
330 for (unsigned i = 0, N = Field.size(); i < N; ++i) {
331 if (DebugInfoDesc *Element = Field[i]) {
332 GlobalVariable *GVE = SR.Serialize(Element);
333 Constant *CE = ConstantExpr::getBitCast(GVE, EmptyTy);
334 ArrayElements.push_back(cast<Constant>(CE));
336 ArrayElements.push_back(ConstantPointerNull::get(EmptyTy));
340 Constant *CA = ConstantArray::get(AT, ArrayElements);
341 GlobalVariable *CAGV = new GlobalVariable(AT, true,
342 GlobalValue::InternalLinkage,
343 CA, "llvm.dbg.array",
345 CAGV->setSection("llvm.metadata");
346 Constant *CAE = ConstantExpr::getBitCast(CAGV, EmptyTy);
347 Elements.push_back(CAE);
351 //===----------------------------------------------------------------------===//
352 /// DIGetTypesVisitor - This DIVisitor gathers all the field types in
353 /// the supplied DebugInfoDesc.
354 class DIGetTypesVisitor : public DIVisitor {
356 DISerializer &SR; // Active serializer.
357 std::vector<const Type*> &Fields; // Type accumulator.
360 DIGetTypesVisitor(DISerializer &S, std::vector<const Type*> &F)
366 /// Apply - Set the value of each of the fields.
368 virtual void Apply(int &Field) {
369 Fields.push_back(Type::Int32Ty);
371 virtual void Apply(unsigned &Field) {
372 Fields.push_back(Type::Int32Ty);
374 virtual void Apply(int64_t &Field) {
375 Fields.push_back(Type::Int64Ty);
377 virtual void Apply(uint64_t &Field) {
378 Fields.push_back(Type::Int64Ty);
380 virtual void Apply(bool &Field) {
381 Fields.push_back(Type::Int1Ty);
383 virtual void Apply(std::string &Field) {
384 Fields.push_back(SR.getStrPtrType());
386 virtual void Apply(DebugInfoDesc *&Field) {
387 // FIXME - At some point should use specific type.
388 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
389 Fields.push_back(EmptyTy);
391 virtual void Apply(GlobalVariable *&Field) {
392 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
393 Fields.push_back(EmptyTy);
395 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
396 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
397 Fields.push_back(EmptyTy);
401 //===----------------------------------------------------------------------===//
402 /// DIVerifyVisitor - This DIVisitor verifies all the field types against
403 /// a constant initializer.
404 class DIVerifyVisitor : public DIVisitor {
406 DIVerifier &VR; // Active verifier.
407 bool IsValid; // Validity status.
408 unsigned I; // Current operand index.
409 ConstantStruct *CI; // GlobalVariable constant initializer.
412 DIVerifyVisitor(DIVerifier &V, GlobalVariable *GV)
417 , CI(cast<ConstantStruct>(GV->getInitializer()))
422 bool isValid() const { return IsValid; }
424 /// Apply - Set the value of each of the fields.
426 virtual void Apply(int &Field) {
427 Constant *C = CI->getOperand(I++);
428 IsValid = IsValid && isa<ConstantInt>(C);
430 virtual void Apply(unsigned &Field) {
431 Constant *C = CI->getOperand(I++);
432 IsValid = IsValid && isa<ConstantInt>(C);
434 virtual void Apply(int64_t &Field) {
435 Constant *C = CI->getOperand(I++);
436 IsValid = IsValid && isa<ConstantInt>(C);
438 virtual void Apply(uint64_t &Field) {
439 Constant *C = CI->getOperand(I++);
440 IsValid = IsValid && isa<ConstantInt>(C);
442 virtual void Apply(bool &Field) {
443 Constant *C = CI->getOperand(I++);
444 IsValid = IsValid && isa<ConstantInt>(C) && C->getType() == Type::Int1Ty;
446 virtual void Apply(std::string &Field) {
447 Constant *C = CI->getOperand(I++);
449 (!C || isStringValue(C) || C->isNullValue());
451 virtual void Apply(DebugInfoDesc *&Field) {
452 // FIXME - Prepare the correct descriptor.
453 Constant *C = CI->getOperand(I++);
454 IsValid = IsValid && isGlobalVariable(C);
456 virtual void Apply(GlobalVariable *&Field) {
457 Constant *C = CI->getOperand(I++);
458 IsValid = IsValid && isGlobalVariable(C);
460 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
461 Constant *C = CI->getOperand(I++);
462 IsValid = IsValid && isGlobalVariable(C);
463 if (!IsValid) return;
465 GlobalVariable *GV = getGlobalVariable(C);
466 IsValid = IsValid && GV && GV->hasInitializer();
467 if (!IsValid) return;
469 ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer());
470 IsValid = IsValid && CA;
471 if (!IsValid) return;
473 for (unsigned i = 0, N = CA->getNumOperands(); IsValid && i < N; ++i) {
474 IsValid = IsValid && isGlobalVariable(CA->getOperand(i));
475 if (!IsValid) return;
477 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
485 //===----------------------------------------------------------------------===//
487 /// TagFromGlobal - Returns the tag number from a debug info descriptor
488 /// GlobalVariable. Return DIIValid if operand is not an unsigned int.
489 unsigned DebugInfoDesc::TagFromGlobal(GlobalVariable *GV) {
490 ConstantInt *C = getUIntOperand(GV, 0);
491 return C ? ((unsigned)C->getZExtValue() & ~LLVMDebugVersionMask) :
492 (unsigned)DW_TAG_invalid;
495 /// VersionFromGlobal - Returns the version number from a debug info
496 /// descriptor GlobalVariable. Return DIIValid if operand is not an unsigned
498 unsigned DebugInfoDesc::VersionFromGlobal(GlobalVariable *GV) {
499 ConstantInt *C = getUIntOperand(GV, 0);
500 return C ? ((unsigned)C->getZExtValue() & LLVMDebugVersionMask) :
501 (unsigned)DW_TAG_invalid;
504 /// DescFactory - Create an instance of debug info descriptor based on Tag.
505 /// Return NULL if not a recognized Tag.
506 DebugInfoDesc *DebugInfoDesc::DescFactory(unsigned Tag) {
508 case DW_TAG_anchor: return new AnchorDesc();
509 case DW_TAG_compile_unit: return new CompileUnitDesc();
510 case DW_TAG_variable: return new GlobalVariableDesc();
511 case DW_TAG_subprogram: return new SubprogramDesc();
512 case DW_TAG_lexical_block: return new BlockDesc();
513 case DW_TAG_base_type: return new BasicTypeDesc();
515 case DW_TAG_pointer_type:
516 case DW_TAG_reference_type:
517 case DW_TAG_const_type:
518 case DW_TAG_volatile_type:
519 case DW_TAG_restrict_type:
521 case DW_TAG_inheritance: return new DerivedTypeDesc(Tag);
522 case DW_TAG_array_type:
523 case DW_TAG_structure_type:
524 case DW_TAG_union_type:
525 case DW_TAG_enumeration_type:
526 case DW_TAG_vector_type:
527 case DW_TAG_subroutine_type: return new CompositeTypeDesc(Tag);
528 case DW_TAG_subrange_type: return new SubrangeDesc();
529 case DW_TAG_enumerator: return new EnumeratorDesc();
530 case DW_TAG_return_variable:
531 case DW_TAG_arg_variable:
532 case DW_TAG_auto_variable: return new VariableDesc(Tag);
538 /// getLinkage - get linkage appropriate for this type of descriptor.
540 GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const {
541 return GlobalValue::InternalLinkage;
544 /// ApplyToFields - Target the vistor to the fields of the descriptor.
546 void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) {
550 //===----------------------------------------------------------------------===//
552 AnchorDesc::AnchorDesc()
553 : DebugInfoDesc(DW_TAG_anchor)
556 AnchorDesc::AnchorDesc(AnchoredDesc *D)
557 : DebugInfoDesc(DW_TAG_anchor)
558 , AnchorTag(D->getTag())
561 // Implement isa/cast/dyncast.
562 bool AnchorDesc::classof(const DebugInfoDesc *D) {
563 return D->getTag() == DW_TAG_anchor;
566 /// getLinkage - get linkage appropriate for this type of descriptor.
568 GlobalValue::LinkageTypes AnchorDesc::getLinkage() const {
569 return GlobalValue::LinkOnceLinkage;
572 /// ApplyToFields - Target the visitor to the fields of the TransUnitDesc.
574 void AnchorDesc::ApplyToFields(DIVisitor *Visitor) {
575 DebugInfoDesc::ApplyToFields(Visitor);
577 Visitor->Apply(AnchorTag);
580 /// getDescString - Return a string used to compose global names and labels. A
581 /// A global variable name needs to be defined for each debug descriptor that is
582 /// anchored. NOTE: that each global variable named here also needs to be added
583 /// to the list of names left external in the internalizer.
584 /// ExternalNames.insert("llvm.dbg.compile_units");
585 /// ExternalNames.insert("llvm.dbg.global_variables");
586 /// ExternalNames.insert("llvm.dbg.subprograms");
587 const char *AnchorDesc::getDescString() const {
589 case DW_TAG_compile_unit: return CompileUnitDesc::AnchorString;
590 case DW_TAG_variable: return GlobalVariableDesc::AnchorString;
591 case DW_TAG_subprogram: return SubprogramDesc::AnchorString;
595 assert(0 && "Tag does not have a case for anchor string");
599 /// getTypeString - Return a string used to label this descriptors type.
601 const char *AnchorDesc::getTypeString() const {
602 return "llvm.dbg.anchor.type";
606 void AnchorDesc::dump() {
607 cerr << getDescString() << " "
608 << "Version(" << getVersion() << "), "
609 << "Tag(" << getTag() << "), "
610 << "AnchorTag(" << AnchorTag << ")\n";
614 //===----------------------------------------------------------------------===//
616 AnchoredDesc::AnchoredDesc(unsigned T)
621 /// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
623 void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) {
624 DebugInfoDesc::ApplyToFields(Visitor);
626 Visitor->Apply(Anchor);
629 //===----------------------------------------------------------------------===//
631 CompileUnitDesc::CompileUnitDesc()
632 : AnchoredDesc(DW_TAG_compile_unit)
639 // Implement isa/cast/dyncast.
640 bool CompileUnitDesc::classof(const DebugInfoDesc *D) {
641 return D->getTag() == DW_TAG_compile_unit;
644 /// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
646 void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) {
647 AnchoredDesc::ApplyToFields(Visitor);
649 // Handle cases out of sync with compiler.
650 if (getVersion() == 0) {
651 unsigned DebugVersion;
652 Visitor->Apply(DebugVersion);
655 Visitor->Apply(Language);
656 Visitor->Apply(FileName);
657 Visitor->Apply(Directory);
658 Visitor->Apply(Producer);
661 /// getDescString - Return a string used to compose global names and labels.
663 const char *CompileUnitDesc::getDescString() const {
664 return "llvm.dbg.compile_unit";
667 /// getTypeString - Return a string used to label this descriptors type.
669 const char *CompileUnitDesc::getTypeString() const {
670 return "llvm.dbg.compile_unit.type";
673 /// getAnchorString - Return a string used to label this descriptor's anchor.
675 const char *const CompileUnitDesc::AnchorString = "llvm.dbg.compile_units";
676 const char *CompileUnitDesc::getAnchorString() const {
681 void CompileUnitDesc::dump() {
682 cerr << getDescString() << " "
683 << "Version(" << getVersion() << "), "
684 << "Tag(" << getTag() << "), "
685 << "Anchor(" << getAnchor() << "), "
686 << "Language(" << Language << "), "
687 << "FileName(\"" << FileName << "\"), "
688 << "Directory(\"" << Directory << "\"), "
689 << "Producer(\"" << Producer << "\")\n";
693 //===----------------------------------------------------------------------===//
695 TypeDesc::TypeDesc(unsigned T)
707 /// ApplyToFields - Target the visitor to the fields of the TypeDesc.
709 void TypeDesc::ApplyToFields(DIVisitor *Visitor) {
710 DebugInfoDesc::ApplyToFields(Visitor);
712 Visitor->Apply(Context);
713 Visitor->Apply(Name);
714 Visitor->Apply(File);
715 Visitor->Apply(Line);
716 Visitor->Apply(Size);
717 Visitor->Apply(Align);
718 Visitor->Apply(Offset);
719 if (getVersion() > LLVMDebugVersion4) Visitor->Apply(Flags);
722 /// getDescString - Return a string used to compose global names and labels.
724 const char *TypeDesc::getDescString() const {
725 return "llvm.dbg.type";
728 /// getTypeString - Return a string used to label this descriptor's type.
730 const char *TypeDesc::getTypeString() const {
731 return "llvm.dbg.type.type";
735 void TypeDesc::dump() {
736 cerr << getDescString() << " "
737 << "Version(" << getVersion() << "), "
738 << "Tag(" << getTag() << "), "
739 << "Context(" << Context << "), "
740 << "Name(\"" << Name << "\"), "
741 << "File(" << File << "), "
742 << "Line(" << Line << "), "
743 << "Size(" << Size << "), "
744 << "Align(" << Align << "), "
745 << "Offset(" << Offset << "), "
746 << "Flags(" << Flags << ")\n";
750 //===----------------------------------------------------------------------===//
752 BasicTypeDesc::BasicTypeDesc()
753 : TypeDesc(DW_TAG_base_type)
757 // Implement isa/cast/dyncast.
758 bool BasicTypeDesc::classof(const DebugInfoDesc *D) {
759 return D->getTag() == DW_TAG_base_type;
762 /// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
764 void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
765 TypeDesc::ApplyToFields(Visitor);
767 Visitor->Apply(Encoding);
770 /// getDescString - Return a string used to compose global names and labels.
772 const char *BasicTypeDesc::getDescString() const {
773 return "llvm.dbg.basictype";
776 /// getTypeString - Return a string used to label this descriptor's type.
778 const char *BasicTypeDesc::getTypeString() const {
779 return "llvm.dbg.basictype.type";
783 void BasicTypeDesc::dump() {
784 cerr << getDescString() << " "
785 << "Version(" << getVersion() << "), "
786 << "Tag(" << getTag() << "), "
787 << "Context(" << getContext() << "), "
788 << "Name(\"" << getName() << "\"), "
789 << "Size(" << getSize() << "), "
790 << "Encoding(" << Encoding << ")\n";
794 //===----------------------------------------------------------------------===//
796 DerivedTypeDesc::DerivedTypeDesc(unsigned T)
801 // Implement isa/cast/dyncast.
802 bool DerivedTypeDesc::classof(const DebugInfoDesc *D) {
803 unsigned T = D->getTag();
806 case DW_TAG_pointer_type:
807 case DW_TAG_reference_type:
808 case DW_TAG_const_type:
809 case DW_TAG_volatile_type:
810 case DW_TAG_restrict_type:
812 case DW_TAG_inheritance:
819 /// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
821 void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) {
822 TypeDesc::ApplyToFields(Visitor);
824 Visitor->Apply(FromType);
827 /// getDescString - Return a string used to compose global names and labels.
829 const char *DerivedTypeDesc::getDescString() const {
830 return "llvm.dbg.derivedtype";
833 /// getTypeString - Return a string used to label this descriptor's type.
835 const char *DerivedTypeDesc::getTypeString() const {
836 return "llvm.dbg.derivedtype.type";
840 void DerivedTypeDesc::dump() {
841 cerr << getDescString() << " "
842 << "Version(" << getVersion() << "), "
843 << "Tag(" << getTag() << "), "
844 << "Context(" << getContext() << "), "
845 << "Name(\"" << getName() << "\"), "
846 << "Size(" << getSize() << "), "
847 << "File(" << getFile() << "), "
848 << "Line(" << getLine() << "), "
849 << "FromType(" << FromType << ")\n";
853 //===----------------------------------------------------------------------===//
855 CompositeTypeDesc::CompositeTypeDesc(unsigned T)
860 // Implement isa/cast/dyncast.
861 bool CompositeTypeDesc::classof(const DebugInfoDesc *D) {
862 unsigned T = D->getTag();
864 case DW_TAG_array_type:
865 case DW_TAG_structure_type:
866 case DW_TAG_union_type:
867 case DW_TAG_enumeration_type:
868 case DW_TAG_vector_type:
869 case DW_TAG_subroutine_type:
876 /// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
878 void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) {
879 DerivedTypeDesc::ApplyToFields(Visitor);
881 Visitor->Apply(Elements);
884 /// getDescString - Return a string used to compose global names and labels.
886 const char *CompositeTypeDesc::getDescString() const {
887 return "llvm.dbg.compositetype";
890 /// getTypeString - Return a string used to label this descriptor's type.
892 const char *CompositeTypeDesc::getTypeString() const {
893 return "llvm.dbg.compositetype.type";
897 void CompositeTypeDesc::dump() {
898 cerr << getDescString() << " "
899 << "Version(" << getVersion() << "), "
900 << "Tag(" << getTag() << "), "
901 << "Context(" << getContext() << "), "
902 << "Name(\"" << getName() << "\"), "
903 << "Size(" << getSize() << "), "
904 << "File(" << getFile() << "), "
905 << "Line(" << getLine() << "), "
906 << "FromType(" << getFromType() << "), "
907 << "Elements.size(" << Elements.size() << ")\n";
911 //===----------------------------------------------------------------------===//
913 SubrangeDesc::SubrangeDesc()
914 : DebugInfoDesc(DW_TAG_subrange_type)
919 // Implement isa/cast/dyncast.
920 bool SubrangeDesc::classof(const DebugInfoDesc *D) {
921 return D->getTag() == DW_TAG_subrange_type;
924 /// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
926 void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) {
927 DebugInfoDesc::ApplyToFields(Visitor);
933 /// getDescString - Return a string used to compose global names and labels.
935 const char *SubrangeDesc::getDescString() const {
936 return "llvm.dbg.subrange";
939 /// getTypeString - Return a string used to label this descriptor's type.
941 const char *SubrangeDesc::getTypeString() const {
942 return "llvm.dbg.subrange.type";
946 void SubrangeDesc::dump() {
947 cerr << getDescString() << " "
948 << "Version(" << getVersion() << "), "
949 << "Tag(" << getTag() << "), "
950 << "Lo(" << Lo << "), "
951 << "Hi(" << Hi << ")\n";
955 //===----------------------------------------------------------------------===//
957 EnumeratorDesc::EnumeratorDesc()
958 : DebugInfoDesc(DW_TAG_enumerator)
963 // Implement isa/cast/dyncast.
964 bool EnumeratorDesc::classof(const DebugInfoDesc *D) {
965 return D->getTag() == DW_TAG_enumerator;
968 /// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
970 void EnumeratorDesc::ApplyToFields(DIVisitor *Visitor) {
971 DebugInfoDesc::ApplyToFields(Visitor);
973 Visitor->Apply(Name);
974 Visitor->Apply(Value);
977 /// getDescString - Return a string used to compose global names and labels.
979 const char *EnumeratorDesc::getDescString() const {
980 return "llvm.dbg.enumerator";
983 /// getTypeString - Return a string used to label this descriptor's type.
985 const char *EnumeratorDesc::getTypeString() const {
986 return "llvm.dbg.enumerator.type";
990 void EnumeratorDesc::dump() {
991 cerr << getDescString() << " "
992 << "Version(" << getVersion() << "), "
993 << "Tag(" << getTag() << "), "
994 << "Name(" << Name << "), "
995 << "Value(" << Value << ")\n";
999 //===----------------------------------------------------------------------===//
1001 VariableDesc::VariableDesc(unsigned T)
1010 // Implement isa/cast/dyncast.
1011 bool VariableDesc::classof(const DebugInfoDesc *D) {
1012 unsigned T = D->getTag();
1014 case DW_TAG_auto_variable:
1015 case DW_TAG_arg_variable:
1016 case DW_TAG_return_variable:
1023 /// ApplyToFields - Target the visitor to the fields of the VariableDesc.
1025 void VariableDesc::ApplyToFields(DIVisitor *Visitor) {
1026 DebugInfoDesc::ApplyToFields(Visitor);
1028 Visitor->Apply(Context);
1029 Visitor->Apply(Name);
1030 Visitor->Apply(File);
1031 Visitor->Apply(Line);
1032 Visitor->Apply(TyDesc);
1035 /// getDescString - Return a string used to compose global names and labels.
1037 const char *VariableDesc::getDescString() const {
1038 return "llvm.dbg.variable";
1041 /// getTypeString - Return a string used to label this descriptor's type.
1043 const char *VariableDesc::getTypeString() const {
1044 return "llvm.dbg.variable.type";
1048 void VariableDesc::dump() {
1049 cerr << getDescString() << " "
1050 << "Version(" << getVersion() << "), "
1051 << "Tag(" << getTag() << "), "
1052 << "Context(" << Context << "), "
1053 << "Name(\"" << Name << "\"), "
1054 << "File(" << File << "), "
1055 << "Line(" << Line << "), "
1056 << "TyDesc(" << TyDesc << ")\n";
1060 //===----------------------------------------------------------------------===//
1062 GlobalDesc::GlobalDesc(unsigned T)
1072 , IsDefinition(false)
1075 /// ApplyToFields - Target the visitor to the fields of the global.
1077 void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
1078 AnchoredDesc::ApplyToFields(Visitor);
1080 Visitor->Apply(Context);
1081 Visitor->Apply(Name);
1082 Visitor->Apply(FullName);
1083 Visitor->Apply(LinkageName);
1084 Visitor->Apply(File);
1085 Visitor->Apply(Line);
1086 Visitor->Apply(TyDesc);
1087 Visitor->Apply(IsStatic);
1088 Visitor->Apply(IsDefinition);
1091 //===----------------------------------------------------------------------===//
1093 GlobalVariableDesc::GlobalVariableDesc()
1094 : GlobalDesc(DW_TAG_variable)
1098 // Implement isa/cast/dyncast.
1099 bool GlobalVariableDesc::classof(const DebugInfoDesc *D) {
1100 return D->getTag() == DW_TAG_variable;
1103 /// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
1105 void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
1106 GlobalDesc::ApplyToFields(Visitor);
1108 Visitor->Apply(Global);
1111 /// getDescString - Return a string used to compose global names and labels.
1113 const char *GlobalVariableDesc::getDescString() const {
1114 return "llvm.dbg.global_variable";
1117 /// getTypeString - Return a string used to label this descriptors type.
1119 const char *GlobalVariableDesc::getTypeString() const {
1120 return "llvm.dbg.global_variable.type";
1123 /// getAnchorString - Return a string used to label this descriptor's anchor.
1125 const char *const GlobalVariableDesc::AnchorString = "llvm.dbg.global_variables";
1126 const char *GlobalVariableDesc::getAnchorString() const {
1127 return AnchorString;
1131 void GlobalVariableDesc::dump() {
1132 cerr << getDescString() << " "
1133 << "Version(" << getVersion() << "), "
1134 << "Tag(" << getTag() << "), "
1135 << "Anchor(" << getAnchor() << "), "
1136 << "Name(\"" << getName() << "\"), "
1137 << "FullName(\"" << getFullName() << "\"), "
1138 << "LinkageName(\"" << getLinkageName() << "\"), "
1139 << "File(" << getFile() << "),"
1140 << "Line(" << getLine() << "),"
1141 << "Type(" << getType() << "), "
1142 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1143 << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
1144 << "Global(" << Global << ")\n";
1148 //===----------------------------------------------------------------------===//
1150 SubprogramDesc::SubprogramDesc()
1151 : GlobalDesc(DW_TAG_subprogram)
1154 // Implement isa/cast/dyncast.
1155 bool SubprogramDesc::classof(const DebugInfoDesc *D) {
1156 return D->getTag() == DW_TAG_subprogram;
1159 /// ApplyToFields - Target the visitor to the fields of the
1161 void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
1162 GlobalDesc::ApplyToFields(Visitor);
1165 /// getDescString - Return a string used to compose global names and labels.
1167 const char *SubprogramDesc::getDescString() const {
1168 return "llvm.dbg.subprogram";
1171 /// getTypeString - Return a string used to label this descriptors type.
1173 const char *SubprogramDesc::getTypeString() const {
1174 return "llvm.dbg.subprogram.type";
1177 /// getAnchorString - Return a string used to label this descriptor's anchor.
1179 const char *const SubprogramDesc::AnchorString = "llvm.dbg.subprograms";
1180 const char *SubprogramDesc::getAnchorString() const {
1181 return AnchorString;
1185 void SubprogramDesc::dump() {
1186 cerr << getDescString() << " "
1187 << "Version(" << getVersion() << "), "
1188 << "Tag(" << getTag() << "), "
1189 << "Anchor(" << getAnchor() << "), "
1190 << "Name(\"" << getName() << "\"), "
1191 << "FullName(\"" << getFullName() << "\"), "
1192 << "LinkageName(\"" << getLinkageName() << "\"), "
1193 << "File(" << getFile() << "),"
1194 << "Line(" << getLine() << "),"
1195 << "Type(" << getType() << "), "
1196 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1197 << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
1201 //===----------------------------------------------------------------------===//
1203 BlockDesc::BlockDesc()
1204 : DebugInfoDesc(DW_TAG_lexical_block)
1208 // Implement isa/cast/dyncast.
1209 bool BlockDesc::classof(const DebugInfoDesc *D) {
1210 return D->getTag() == DW_TAG_lexical_block;
1213 /// ApplyToFields - Target the visitor to the fields of the BlockDesc.
1215 void BlockDesc::ApplyToFields(DIVisitor *Visitor) {
1216 DebugInfoDesc::ApplyToFields(Visitor);
1218 Visitor->Apply(Context);
1221 /// getDescString - Return a string used to compose global names and labels.
1223 const char *BlockDesc::getDescString() const {
1224 return "llvm.dbg.block";
1227 /// getTypeString - Return a string used to label this descriptors type.
1229 const char *BlockDesc::getTypeString() const {
1230 return "llvm.dbg.block.type";
1234 void BlockDesc::dump() {
1235 cerr << getDescString() << " "
1236 << "Version(" << getVersion() << "), "
1237 << "Tag(" << getTag() << "),"
1238 << "Context(" << Context << ")\n";
1242 //===----------------------------------------------------------------------===//
1244 DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
1245 return Deserialize(getGlobalVariable(V));
1247 DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
1249 if (!GV) return NULL;
1251 // Check to see if it has been already deserialized.
1252 DebugInfoDesc *&Slot = GlobalDescs[GV];
1253 if (Slot) return Slot;
1255 // Get the Tag from the global.
1256 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1258 // Create an empty instance of the correct sort.
1259 Slot = DebugInfoDesc::DescFactory(Tag);
1261 // If not a user defined descriptor.
1263 // Deserialize the fields.
1264 DIDeserializeVisitor DRAM(*this, GV);
1265 DRAM.ApplyToFields(Slot);
1271 //===----------------------------------------------------------------------===//
1273 /// getStrPtrType - Return a "sbyte *" type.
1275 const PointerType *DISerializer::getStrPtrType() {
1276 // If not already defined.
1278 // Construct the pointer to signed bytes.
1279 StrPtrTy = PointerType::getUnqual(Type::Int8Ty);
1285 /// getEmptyStructPtrType - Return a "{ }*" type.
1287 const PointerType *DISerializer::getEmptyStructPtrType() {
1288 // If not already defined.
1289 if (!EmptyStructPtrTy) {
1290 // Construct the empty structure type.
1291 const StructType *EmptyStructTy =
1292 StructType::get(std::vector<const Type*>());
1293 // Construct the pointer to empty structure type.
1294 EmptyStructPtrTy = PointerType::getUnqual(EmptyStructTy);
1297 return EmptyStructPtrTy;
1300 /// getTagType - Return the type describing the specified descriptor (via tag.)
1302 const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
1303 // Attempt to get the previously defined type.
1304 StructType *&Ty = TagTypes[DD->getTag()];
1306 // If not already defined.
1308 // Set up fields vector.
1309 std::vector<const Type*> Fields;
1310 // Get types of fields.
1311 DIGetTypesVisitor GTAM(*this, Fields);
1312 GTAM.ApplyToFields(DD);
1314 // Construct structured type.
1315 Ty = StructType::get(Fields);
1317 // Register type name with module.
1318 M->addTypeName(DD->getTypeString(), Ty);
1324 /// getString - Construct the string as constant string global.
1326 Constant *DISerializer::getString(const std::string &String) {
1327 // Check string cache for previous edition.
1328 Constant *&Slot = StringCache[String];
1329 // Return Constant if previously defined.
1330 if (Slot) return Slot;
1331 // If empty string then use a sbyte* null instead.
1332 if (String.empty()) {
1333 Slot = ConstantPointerNull::get(getStrPtrType());
1335 // Construct string as an llvm constant.
1336 Constant *ConstStr = ConstantArray::get(String);
1337 // Otherwise create and return a new string global.
1338 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
1339 GlobalVariable::InternalLinkage,
1340 ConstStr, ".str", M);
1341 StrGV->setSection("llvm.metadata");
1342 // Convert to generic string pointer.
1343 Slot = ConstantExpr::getBitCast(StrGV, getStrPtrType());
1349 /// Serialize - Recursively cast the specified descriptor into a GlobalVariable
1350 /// so that it can be serialized to a .bc or .ll file.
1351 GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
1352 // Check if the DebugInfoDesc is already in the map.
1353 GlobalVariable *&Slot = DescGlobals[DD];
1355 // See if DebugInfoDesc exists, if so return prior GlobalVariable.
1356 if (Slot) return Slot;
1358 // Get the type associated with the Tag.
1359 const StructType *Ty = getTagType(DD);
1361 // Create the GlobalVariable early to prevent infinite recursion.
1362 GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(),
1363 NULL, DD->getDescString(), M);
1364 GV->setSection("llvm.metadata");
1366 // Insert new GlobalVariable in DescGlobals map.
1369 // Set up elements vector
1370 std::vector<Constant*> Elements;
1372 DISerializeVisitor SRAM(*this, Elements);
1373 SRAM.ApplyToFields(DD);
1375 // Set the globals initializer.
1376 GV->setInitializer(ConstantStruct::get(Ty, Elements));
1381 /// addDescriptor - Directly connect DD with existing GV.
1382 void DISerializer::addDescriptor(DebugInfoDesc *DD,
1383 GlobalVariable *GV) {
1384 DescGlobals[DD] = GV;
1387 //===----------------------------------------------------------------------===//
1389 /// Verify - Return true if the GlobalVariable appears to be a valid
1390 /// serialization of a DebugInfoDesc.
1391 bool DIVerifier::Verify(Value *V) {
1392 return !V || Verify(getGlobalVariable(V));
1394 bool DIVerifier::Verify(GlobalVariable *GV) {
1396 if (!GV) return true;
1398 // Check prior validity.
1399 unsigned &ValiditySlot = Validity[GV];
1401 // If visited before then use old state.
1402 if (ValiditySlot) return ValiditySlot == Valid;
1404 // Assume validity for the time being (recursion.)
1405 ValiditySlot = Valid;
1407 // Make sure the global is internal or link once (anchor.)
1408 if (GV->getLinkage() != GlobalValue::InternalLinkage &&
1409 GV->getLinkage() != GlobalValue::LinkOnceLinkage) {
1410 ValiditySlot = Invalid;
1415 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1417 // Check for user defined descriptors.
1418 if (Tag == DW_TAG_invalid) {
1419 ValiditySlot = Valid;
1424 unsigned Version = DebugInfoDesc::VersionFromGlobal(GV);
1426 // Check for version mismatch.
1427 if (Version != LLVMDebugVersion) {
1428 ValiditySlot = Invalid;
1432 // Construct an empty DebugInfoDesc.
1433 DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
1435 // Allow for user defined descriptors.
1436 if (!DD) return true;
1438 // Get the initializer constant.
1439 ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
1441 // Get the operand count.
1442 unsigned N = CI->getNumOperands();
1444 // Get the field count.
1445 unsigned &CountSlot = Counts[Tag];
1447 // Check the operand count to the field count
1448 DICountVisitor CTAM;
1449 CTAM.ApplyToFields(DD);
1450 CountSlot = CTAM.getCount();
1453 // Field count must be at most equal operand count.
1454 if (CountSlot > N) {
1456 ValiditySlot = Invalid;
1460 // Check each field for valid type.
1461 DIVerifyVisitor VRAM(*this, GV);
1462 VRAM.ApplyToFields(DD);
1464 // Release empty DebugInfoDesc.
1467 // If fields are not valid.
1468 if (!VRAM.isValid()) {
1469 ValiditySlot = Invalid;
1476 /// isVerified - Return true if the specified GV has already been
1477 /// verified as a debug information descriptor.
1478 bool DIVerifier::isVerified(GlobalVariable *GV) {
1479 unsigned &ValiditySlot = Validity[GV];
1480 if (ValiditySlot) return ValiditySlot == Valid;
1484 //===----------------------------------------------------------------------===//
1486 DebugScope::~DebugScope() {
1487 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1488 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
1491 //===----------------------------------------------------------------------===//
1493 MachineModuleInfo::MachineModuleInfo()
1494 : ImmutablePass((intptr_t)&ID)
1508 , CallsUnwindInit(0)
1510 // Always emit "no personality" info
1511 Personalities.push_back(NULL);
1513 MachineModuleInfo::~MachineModuleInfo() {
1517 /// doInitialization - Initialize the state for a new module.
1519 bool MachineModuleInfo::doInitialization() {
1523 /// doFinalization - Tear down the state after completion of a module.
1525 bool MachineModuleInfo::doFinalization() {
1529 /// BeginFunction - Begin gathering function meta information.
1531 void MachineModuleInfo::BeginFunction(MachineFunction *MF) {
1535 /// EndFunction - Discard function meta information.
1537 void MachineModuleInfo::EndFunction() {
1538 // Clean up scope information.
1545 // Clean up line info.
1548 // Clean up frame info.
1551 // Clean up exception info.
1552 LandingPads.clear();
1557 CallsUnwindInit = 0;
1560 /// getDescFor - Convert a Value to a debug information descriptor.
1562 // FIXME - use new Value type when available.
1563 DebugInfoDesc *MachineModuleInfo::getDescFor(Value *V) {
1564 return DR.Deserialize(V);
1567 /// AnalyzeModule - Scan the module for global debug information.
1569 void MachineModuleInfo::AnalyzeModule(Module &M) {
1570 SetupCompileUnits(M);
1572 // Insert functions in the llvm.used array into UsedFunctions.
1573 GlobalVariable *GV = M.getGlobalVariable("llvm.used");
1574 if (!GV || !GV->hasInitializer()) return;
1576 // Should be an array of 'i8*'.
1577 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
1578 if (InitList == 0) return;
1580 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
1581 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(InitList->getOperand(i)))
1582 if (CE->getOpcode() == Instruction::BitCast)
1583 if (Function *F = dyn_cast<Function>(CE->getOperand(0)))
1584 UsedFunctions.insert(F);
1588 /// SetupCompileUnits - Set up the unique vector of compile units.
1590 void MachineModuleInfo::SetupCompileUnits(Module &M) {
1591 std::vector<CompileUnitDesc *>CU = getAnchoredDescriptors<CompileUnitDesc>(M);
1593 for (unsigned i = 0, N = CU.size(); i < N; i++) {
1594 CompileUnits.insert(CU[i]);
1598 /// getCompileUnits - Return a vector of debug compile units.
1600 const UniqueVector<CompileUnitDesc *> MachineModuleInfo::getCompileUnits()const{
1601 return CompileUnits;
1604 /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1605 /// named GlobalVariable.
1606 std::vector<GlobalVariable*>
1607 MachineModuleInfo::getGlobalVariablesUsing(Module &M,
1608 const std::string &RootName) {
1609 return ::getGlobalVariablesUsing(M, RootName);
1612 /// RecordSourceLine - Records location information and associates it with a
1613 /// debug label. Returns a unique label ID used to generate a label and
1614 /// provide correspondence to the source line list.
1615 unsigned MachineModuleInfo::RecordSourceLine(unsigned Line, unsigned Column,
1617 unsigned ID = NextLabelID();
1618 Lines.push_back(SourceLineInfo(Line, Column, Source, ID));
1622 /// RecordSource - Register a source file with debug info. Returns an source
1624 unsigned MachineModuleInfo::RecordSource(const std::string &Directory,
1625 const std::string &Source) {
1626 unsigned DirectoryID = Directories.insert(Directory);
1627 return SourceFiles.insert(SourceFileInfo(DirectoryID, Source));
1629 unsigned MachineModuleInfo::RecordSource(const CompileUnitDesc *CompileUnit) {
1630 return RecordSource(CompileUnit->getDirectory(),
1631 CompileUnit->getFileName());
1634 /// RecordRegionStart - Indicate the start of a region.
1636 unsigned MachineModuleInfo::RecordRegionStart(Value *V) {
1637 // FIXME - need to be able to handle split scopes because of bb cloning.
1638 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1639 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1640 unsigned ID = NextLabelID();
1641 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
1645 /// RecordRegionEnd - Indicate the end of a region.
1647 unsigned MachineModuleInfo::RecordRegionEnd(Value *V) {
1648 // FIXME - need to be able to handle split scopes because of bb cloning.
1649 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1650 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1651 unsigned ID = NextLabelID();
1652 Scope->setEndLabelID(ID);
1656 /// RecordVariable - Indicate the declaration of a local variable.
1658 void MachineModuleInfo::RecordVariable(GlobalValue *GV, unsigned FrameIndex) {
1659 VariableDesc *VD = cast<VariableDesc>(DR.Deserialize(GV));
1660 DebugScope *Scope = getOrCreateScope(VD->getContext());
1661 DebugVariable *DV = new DebugVariable(VD, FrameIndex);
1662 Scope->AddVariable(DV);
1665 /// getOrCreateScope - Returns the scope associated with the given descriptor.
1667 DebugScope *MachineModuleInfo::getOrCreateScope(DebugInfoDesc *ScopeDesc) {
1668 DebugScope *&Slot = ScopeMap[ScopeDesc];
1670 // FIXME - breaks down when the context is an inlined function.
1671 DebugInfoDesc *ParentDesc = NULL;
1672 if (BlockDesc *Block = dyn_cast<BlockDesc>(ScopeDesc)) {
1673 ParentDesc = Block->getContext();
1675 DebugScope *Parent = ParentDesc ? getOrCreateScope(ParentDesc) : NULL;
1676 Slot = new DebugScope(Parent, ScopeDesc);
1678 Parent->AddScope(Slot);
1679 } else if (RootScope) {
1680 // FIXME - Add inlined function scopes to the root so we can delete
1681 // them later. Long term, handle inlined functions properly.
1682 RootScope->AddScope(Slot);
1684 // First function is top level function.
1691 //===-EH-------------------------------------------------------------------===//
1693 /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
1694 /// specified MachineBasicBlock.
1695 LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo
1696 (MachineBasicBlock *LandingPad) {
1697 unsigned N = LandingPads.size();
1698 for (unsigned i = 0; i < N; ++i) {
1699 LandingPadInfo &LP = LandingPads[i];
1700 if (LP.LandingPadBlock == LandingPad)
1704 LandingPads.push_back(LandingPadInfo(LandingPad));
1705 return LandingPads[N];
1708 /// addInvoke - Provide the begin and end labels of an invoke style call and
1709 /// associate it with a try landing pad block.
1710 void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad,
1711 unsigned BeginLabel, unsigned EndLabel) {
1712 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1713 LP.BeginLabels.push_back(BeginLabel);
1714 LP.EndLabels.push_back(EndLabel);
1717 /// addLandingPad - Provide the label of a try LandingPad block.
1719 unsigned MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) {
1720 unsigned LandingPadLabel = NextLabelID();
1721 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1722 LP.LandingPadLabel = LandingPadLabel;
1723 return LandingPadLabel;
1726 /// addPersonality - Provide the personality function for the exception
1728 void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad,
1729 Function *Personality) {
1730 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1731 LP.Personality = Personality;
1733 for (unsigned i = 0; i < Personalities.size(); ++i)
1734 if (Personalities[i] == Personality)
1737 Personalities.push_back(Personality);
1740 /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
1742 void MachineModuleInfo::addCatchTypeInfo(MachineBasicBlock *LandingPad,
1743 std::vector<GlobalVariable *> &TyInfo) {
1744 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1745 for (unsigned N = TyInfo.size(); N; --N)
1746 LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
1749 /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
1751 void MachineModuleInfo::addFilterTypeInfo(MachineBasicBlock *LandingPad,
1752 std::vector<GlobalVariable *> &TyInfo) {
1753 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1754 std::vector<unsigned> IdsInFilter (TyInfo.size());
1755 for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
1756 IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
1757 LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
1760 /// addCleanup - Add a cleanup action for a landing pad.
1762 void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) {
1763 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1764 LP.TypeIds.push_back(0);
1767 /// TidyLandingPads - Remap landing pad labels and remove any deleted landing
1769 void MachineModuleInfo::TidyLandingPads() {
1770 for (unsigned i = 0; i != LandingPads.size(); ) {
1771 LandingPadInfo &LandingPad = LandingPads[i];
1772 LandingPad.LandingPadLabel = MappedLabel(LandingPad.LandingPadLabel);
1774 // Special case: we *should* emit LPs with null LP MBB. This indicates
1776 if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
1777 LandingPads.erase(LandingPads.begin() + i);
1781 for (unsigned j=0; j != LandingPads[i].BeginLabels.size(); ) {
1782 unsigned BeginLabel = MappedLabel(LandingPad.BeginLabels[j]);
1783 unsigned EndLabel = MappedLabel(LandingPad.EndLabels[j]);
1785 if (!BeginLabel || !EndLabel) {
1786 LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
1787 LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
1791 LandingPad.BeginLabels[j] = BeginLabel;
1792 LandingPad.EndLabels[j] = EndLabel;
1796 // Remove landing pads with no try-ranges.
1797 if (LandingPads[i].BeginLabels.empty()) {
1798 LandingPads.erase(LandingPads.begin() + i);
1802 // If there is no landing pad, ensure that the list of typeids is empty.
1803 // If the only typeid is a cleanup, this is the same as having no typeids.
1804 if (!LandingPad.LandingPadBlock ||
1805 (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
1806 LandingPad.TypeIds.clear();
1812 /// getTypeIDFor - Return the type id for the specified typeinfo. This is
1814 unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) {
1815 for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
1816 if (TypeInfos[i] == TI) return i + 1;
1818 TypeInfos.push_back(TI);
1819 return TypeInfos.size();
1822 /// getFilterIDFor - Return the filter id for the specified typeinfos. This is
1824 int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) {
1825 // If the new filter coincides with the tail of an existing filter, then
1826 // re-use the existing filter. Folding filters more than this requires
1827 // re-ordering filters and/or their elements - probably not worth it.
1828 for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
1829 E = FilterEnds.end(); I != E; ++I) {
1830 unsigned i = *I, j = TyIds.size();
1833 if (FilterIds[--i] != TyIds[--j])
1837 // The new filter coincides with range [i, end) of the existing filter.
1843 // Add the new filter.
1844 int FilterID = -(1 + FilterIds.size());
1845 FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
1846 for (unsigned I = 0, N = TyIds.size(); I != N; ++I)
1847 FilterIds.push_back(TyIds[I]);
1848 FilterEnds.push_back(FilterIds.size());
1849 FilterIds.push_back(0); // terminator
1853 /// getPersonality - Return the personality function for the current function.
1854 Function *MachineModuleInfo::getPersonality() const {
1855 // FIXME: Until PR1414 will be fixed, we're using 1 personality function per
1857 return !LandingPads.empty() ? LandingPads[0].Personality : NULL;
1860 /// getPersonalityIndex - Return unique index for current personality
1861 /// function. NULL personality function should always get zero index.
1862 unsigned MachineModuleInfo::getPersonalityIndex() const {
1863 const Function* Personality = NULL;
1865 // Scan landing pads. If there is at least one non-NULL personality - use it.
1866 for (unsigned i = 0; i != LandingPads.size(); ++i)
1867 if (LandingPads[i].Personality) {
1868 Personality = LandingPads[i].Personality;
1872 for (unsigned i = 0; i < Personalities.size(); ++i) {
1873 if (Personalities[i] == Personality)
1877 // This should never happen
1878 assert(0 && "Personality function should be set!");
1882 //===----------------------------------------------------------------------===//
1883 /// DebugLabelFolding pass - This pass prunes out redundant labels. This allows
1884 /// a info consumer to determine if the range of two labels is empty, by seeing
1885 /// if the labels map to the same reduced label.
1889 struct DebugLabelFolder : public MachineFunctionPass {
1891 DebugLabelFolder() : MachineFunctionPass((intptr_t)&ID) {}
1893 virtual bool runOnMachineFunction(MachineFunction &MF);
1894 virtual const char *getPassName() const { return "Label Folder"; }
1897 char DebugLabelFolder::ID = 0;
1899 bool DebugLabelFolder::runOnMachineFunction(MachineFunction &MF) {
1900 // Get machine module info.
1901 MachineModuleInfo *MMI = getAnalysisToUpdate<MachineModuleInfo>();
1902 if (!MMI) return false;
1904 // Track if change is made.
1905 bool MadeChange = false;
1906 // No prior label to begin.
1907 unsigned PriorLabel = 0;
1909 // Iterate through basic blocks.
1910 for (MachineFunction::iterator BB = MF.begin(), E = MF.end();
1912 // Iterate through instructions.
1913 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1915 if (I->isDebugLabel()) {
1916 // The label ID # is always operand #0, an immediate.
1917 unsigned NextLabel = I->getOperand(0).getImm();
1919 // If there was an immediate prior label.
1921 // Remap the current label to prior label.
1922 MMI->RemapLabel(NextLabel, PriorLabel);
1923 // Delete the current label.
1925 // Indicate a change has been made.
1929 // Start a new round.
1930 PriorLabel = NextLabel;
1933 // No consecutive labels.
1944 FunctionPass *createDebugLabelFoldingPass() { return new DebugLabelFolder(); }