1 //===-- llvm/CodeGen/MachineModuleInfo.cpp ----------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/CodeGen/MachineModuleInfo.h"
12 #include "llvm/Constants.h"
13 #include "llvm/Analysis/ValueTracking.h"
14 #include "llvm/CodeGen/MachineFunctionPass.h"
15 #include "llvm/CodeGen/MachineFunction.h"
16 #include "llvm/CodeGen/MachineLocation.h"
17 #include "llvm/Target/TargetInstrInfo.h"
18 #include "llvm/Target/TargetMachine.h"
19 #include "llvm/Target/TargetOptions.h"
20 #include "llvm/DerivedTypes.h"
21 #include "llvm/GlobalVariable.h"
22 #include "llvm/Intrinsics.h"
23 #include "llvm/Instructions.h"
24 #include "llvm/Module.h"
25 #include "llvm/Support/Dwarf.h"
26 #include "llvm/Support/Streams.h"
28 using namespace llvm::dwarf;
30 // Handle the Pass registration stuff necessary to use TargetData's.
31 static RegisterPass<MachineModuleInfo>
32 X("machinemoduleinfo", "Module Information");
33 char MachineModuleInfo::ID = 0;
35 //===----------------------------------------------------------------------===//
37 /// getGlobalVariablesUsing - Return all of the GlobalVariables which have the
38 /// specified value in their initializer somewhere.
40 getGlobalVariablesUsing(Value *V, std::vector<GlobalVariable*> &Result) {
41 // Scan though value users.
42 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
43 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I)) {
44 // If the user is a GlobalVariable then add to result.
46 } else if (Constant *C = dyn_cast<Constant>(*I)) {
47 // If the user is a constant variable then scan its users
48 getGlobalVariablesUsing(C, Result);
53 /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
54 /// named GlobalVariable.
55 static std::vector<GlobalVariable*>
56 getGlobalVariablesUsing(Module &M, const std::string &RootName) {
57 std::vector<GlobalVariable*> Result; // GlobalVariables matching criteria.
59 std::vector<const Type*> FieldTypes;
60 FieldTypes.push_back(Type::Int32Ty);
61 FieldTypes.push_back(Type::Int32Ty);
63 // Get the GlobalVariable root.
64 GlobalVariable *UseRoot = M.getGlobalVariable(RootName,
65 StructType::get(FieldTypes));
67 // If present and linkonce then scan for users.
68 if (UseRoot && UseRoot->hasLinkOnceLinkage()) {
69 getGlobalVariablesUsing(UseRoot, Result);
75 /// isStringValue - Return true if the given value can be coerced to a string.
77 static bool isStringValue(Value *V) {
78 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
79 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
80 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
81 return Init->isString();
83 } else if (Constant *C = dyn_cast<Constant>(V)) {
84 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
85 return isStringValue(GV);
86 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
87 if (CE->getOpcode() == Instruction::GetElementPtr) {
88 if (CE->getNumOperands() == 3 &&
89 cast<Constant>(CE->getOperand(1))->isNullValue() &&
90 isa<ConstantInt>(CE->getOperand(2))) {
91 return isStringValue(CE->getOperand(0));
99 /// getGlobalVariable - Return either a direct or cast Global value.
101 static GlobalVariable *getGlobalVariable(Value *V) {
102 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
104 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
105 if (CE->getOpcode() == Instruction::BitCast) {
106 return dyn_cast<GlobalVariable>(CE->getOperand(0));
107 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
108 for (unsigned int i=1; i<CE->getNumOperands(); i++) {
109 if (!CE->getOperand(i)->isNullValue())
112 return dyn_cast<GlobalVariable>(CE->getOperand(0));
118 /// isGlobalVariable - Return true if the given value can be coerced to a
120 static bool isGlobalVariable(Value *V) {
121 if (isa<GlobalVariable>(V) || isa<ConstantPointerNull>(V)) {
123 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
124 if (CE->getOpcode() == Instruction::BitCast) {
125 return isa<GlobalVariable>(CE->getOperand(0));
126 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
127 for (unsigned int i=1; i<CE->getNumOperands(); i++) {
128 if (!CE->getOperand(i)->isNullValue())
131 return isa<GlobalVariable>(CE->getOperand(0));
137 /// getUIntOperand - Return ith operand if it is an unsigned integer.
139 static ConstantInt *getUIntOperand(GlobalVariable *GV, unsigned i) {
140 // Make sure the GlobalVariable has an initializer.
141 if (!GV->hasInitializer()) return NULL;
143 // Get the initializer constant.
144 ConstantStruct *CI = dyn_cast<ConstantStruct>(GV->getInitializer());
145 if (!CI) return NULL;
147 // Check if there is at least i + 1 operands.
148 unsigned N = CI->getNumOperands();
149 if (i >= N) return NULL;
152 return dyn_cast<ConstantInt>(CI->getOperand(i));
155 //===----------------------------------------------------------------------===//
157 /// ApplyToFields - Target the visitor to each field of the debug information
159 void DIVisitor::ApplyToFields(DebugInfoDesc *DD) {
160 DD->ApplyToFields(this);
165 //===----------------------------------------------------------------------===//
166 /// DICountVisitor - This DIVisitor counts all the fields in the supplied debug
167 /// the supplied DebugInfoDesc.
168 class DICountVisitor : public DIVisitor {
170 unsigned Count; // Running count of fields.
173 DICountVisitor() : DIVisitor(), Count(0) {}
176 unsigned getCount() const { return Count; }
178 /// Apply - Count each of the fields.
180 virtual void Apply(int &Field) { ++Count; }
181 virtual void Apply(unsigned &Field) { ++Count; }
182 virtual void Apply(int64_t &Field) { ++Count; }
183 virtual void Apply(uint64_t &Field) { ++Count; }
184 virtual void Apply(bool &Field) { ++Count; }
185 virtual void Apply(std::string &Field) { ++Count; }
186 virtual void Apply(DebugInfoDesc *&Field) { ++Count; }
187 virtual void Apply(GlobalVariable *&Field) { ++Count; }
188 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
193 //===----------------------------------------------------------------------===//
194 /// DIDeserializeVisitor - This DIVisitor deserializes all the fields in the
195 /// supplied DebugInfoDesc.
196 class DIDeserializeVisitor : public DIVisitor {
198 DIDeserializer &DR; // Active deserializer.
199 unsigned I; // Current operand index.
200 ConstantStruct *CI; // GlobalVariable constant initializer.
203 DIDeserializeVisitor(DIDeserializer &D, GlobalVariable *GV)
207 , CI(cast<ConstantStruct>(GV->getInitializer()))
210 /// Apply - Set the value of each of the fields.
212 virtual void Apply(int &Field) {
213 Constant *C = CI->getOperand(I++);
214 Field = cast<ConstantInt>(C)->getSExtValue();
216 virtual void Apply(unsigned &Field) {
217 Constant *C = CI->getOperand(I++);
218 Field = cast<ConstantInt>(C)->getZExtValue();
220 virtual void Apply(int64_t &Field) {
221 Constant *C = CI->getOperand(I++);
222 Field = cast<ConstantInt>(C)->getSExtValue();
224 virtual void Apply(uint64_t &Field) {
225 Constant *C = CI->getOperand(I++);
226 Field = cast<ConstantInt>(C)->getZExtValue();
228 virtual void Apply(bool &Field) {
229 Constant *C = CI->getOperand(I++);
230 Field = cast<ConstantInt>(C)->getZExtValue();
232 virtual void Apply(std::string &Field) {
233 Constant *C = CI->getOperand(I++);
235 if (GetConstantStringInfo(C, S))
240 virtual void Apply(DebugInfoDesc *&Field) {
241 Constant *C = CI->getOperand(I++);
242 Field = DR.Deserialize(C);
244 virtual void Apply(GlobalVariable *&Field) {
245 Constant *C = CI->getOperand(I++);
246 Field = getGlobalVariable(C);
248 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
250 Constant *C = CI->getOperand(I++);
251 GlobalVariable *GV = getGlobalVariable(C);
252 if (GV->hasInitializer()) {
253 if (ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer())) {
254 for (unsigned i = 0, N = CA->getNumOperands(); i < N; ++i) {
255 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
256 DebugInfoDesc *DE = DR.Deserialize(GVE);
259 } else if (GV->getInitializer()->isNullValue()) {
260 if (const ArrayType *T =
261 dyn_cast<ArrayType>(GV->getType()->getElementType())) {
262 Field.resize(T->getNumElements());
269 //===----------------------------------------------------------------------===//
270 /// DISerializeVisitor - This DIVisitor serializes all the fields in
271 /// the supplied DebugInfoDesc.
272 class DISerializeVisitor : public DIVisitor {
274 DISerializer &SR; // Active serializer.
275 std::vector<Constant*> &Elements; // Element accumulator.
278 DISerializeVisitor(DISerializer &S, std::vector<Constant*> &E)
284 /// Apply - Set the value of each of the fields.
286 virtual void Apply(int &Field) {
287 Elements.push_back(ConstantInt::get(Type::Int32Ty, int32_t(Field)));
289 virtual void Apply(unsigned &Field) {
290 Elements.push_back(ConstantInt::get(Type::Int32Ty, uint32_t(Field)));
292 virtual void Apply(int64_t &Field) {
293 Elements.push_back(ConstantInt::get(Type::Int64Ty, int64_t(Field)));
295 virtual void Apply(uint64_t &Field) {
296 Elements.push_back(ConstantInt::get(Type::Int64Ty, uint64_t(Field)));
298 virtual void Apply(bool &Field) {
299 Elements.push_back(ConstantInt::get(Type::Int1Ty, Field));
301 virtual void Apply(std::string &Field) {
302 Elements.push_back(SR.getString(Field));
304 virtual void Apply(DebugInfoDesc *&Field) {
305 GlobalVariable *GV = NULL;
307 // If non-NULL then convert to global.
308 if (Field) GV = SR.Serialize(Field);
310 // FIXME - At some point should use specific type.
311 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
314 // Set to pointer to global.
315 Elements.push_back(ConstantExpr::getBitCast(GV, EmptyTy));
318 Elements.push_back(ConstantPointerNull::get(EmptyTy));
321 virtual void Apply(GlobalVariable *&Field) {
322 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
324 Elements.push_back(ConstantExpr::getBitCast(Field, EmptyTy));
326 Elements.push_back(ConstantPointerNull::get(EmptyTy));
329 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
330 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
331 unsigned N = Field.size();
332 ArrayType *AT = ArrayType::get(EmptyTy, N);
333 std::vector<Constant *> ArrayElements;
335 for (unsigned i = 0, N = Field.size(); i < N; ++i) {
336 if (DebugInfoDesc *Element = Field[i]) {
337 GlobalVariable *GVE = SR.Serialize(Element);
338 Constant *CE = ConstantExpr::getBitCast(GVE, EmptyTy);
339 ArrayElements.push_back(cast<Constant>(CE));
341 ArrayElements.push_back(ConstantPointerNull::get(EmptyTy));
345 Constant *CA = ConstantArray::get(AT, ArrayElements);
346 GlobalVariable *CAGV = new GlobalVariable(AT, true,
347 GlobalValue::InternalLinkage,
348 CA, "llvm.dbg.array",
350 CAGV->setSection("llvm.metadata");
351 Constant *CAE = ConstantExpr::getBitCast(CAGV, EmptyTy);
352 Elements.push_back(CAE);
356 //===----------------------------------------------------------------------===//
357 /// DIGetTypesVisitor - This DIVisitor gathers all the field types in
358 /// the supplied DebugInfoDesc.
359 class DIGetTypesVisitor : public DIVisitor {
361 DISerializer &SR; // Active serializer.
362 std::vector<const Type*> &Fields; // Type accumulator.
365 DIGetTypesVisitor(DISerializer &S, std::vector<const Type*> &F)
371 /// Apply - Set the value of each of the fields.
373 virtual void Apply(int &Field) {
374 Fields.push_back(Type::Int32Ty);
376 virtual void Apply(unsigned &Field) {
377 Fields.push_back(Type::Int32Ty);
379 virtual void Apply(int64_t &Field) {
380 Fields.push_back(Type::Int64Ty);
382 virtual void Apply(uint64_t &Field) {
383 Fields.push_back(Type::Int64Ty);
385 virtual void Apply(bool &Field) {
386 Fields.push_back(Type::Int1Ty);
388 virtual void Apply(std::string &Field) {
389 Fields.push_back(SR.getStrPtrType());
391 virtual void Apply(DebugInfoDesc *&Field) {
392 // FIXME - At some point should use specific type.
393 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
394 Fields.push_back(EmptyTy);
396 virtual void Apply(GlobalVariable *&Field) {
397 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
398 Fields.push_back(EmptyTy);
400 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
401 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
402 Fields.push_back(EmptyTy);
406 //===----------------------------------------------------------------------===//
407 /// DIVerifyVisitor - This DIVisitor verifies all the field types against
408 /// a constant initializer.
409 class DIVerifyVisitor : public DIVisitor {
411 DIVerifier &VR; // Active verifier.
412 bool IsValid; // Validity status.
413 unsigned I; // Current operand index.
414 ConstantStruct *CI; // GlobalVariable constant initializer.
417 DIVerifyVisitor(DIVerifier &V, GlobalVariable *GV)
422 , CI(cast<ConstantStruct>(GV->getInitializer()))
427 bool isValid() const { return IsValid; }
429 /// Apply - Set the value of each of the fields.
431 virtual void Apply(int &Field) {
432 Constant *C = CI->getOperand(I++);
433 IsValid = IsValid && isa<ConstantInt>(C);
435 virtual void Apply(unsigned &Field) {
436 Constant *C = CI->getOperand(I++);
437 IsValid = IsValid && isa<ConstantInt>(C);
439 virtual void Apply(int64_t &Field) {
440 Constant *C = CI->getOperand(I++);
441 IsValid = IsValid && isa<ConstantInt>(C);
443 virtual void Apply(uint64_t &Field) {
444 Constant *C = CI->getOperand(I++);
445 IsValid = IsValid && isa<ConstantInt>(C);
447 virtual void Apply(bool &Field) {
448 Constant *C = CI->getOperand(I++);
449 IsValid = IsValid && isa<ConstantInt>(C) && C->getType() == Type::Int1Ty;
451 virtual void Apply(std::string &Field) {
452 Constant *C = CI->getOperand(I++);
454 (!C || isStringValue(C) || C->isNullValue());
456 virtual void Apply(DebugInfoDesc *&Field) {
457 // FIXME - Prepare the correct descriptor.
458 Constant *C = CI->getOperand(I++);
459 IsValid = IsValid && isGlobalVariable(C);
461 virtual void Apply(GlobalVariable *&Field) {
462 Constant *C = CI->getOperand(I++);
463 IsValid = IsValid && isGlobalVariable(C);
465 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
466 Constant *C = CI->getOperand(I++);
467 IsValid = IsValid && isGlobalVariable(C);
468 if (!IsValid) return;
470 GlobalVariable *GV = getGlobalVariable(C);
471 IsValid = IsValid && GV && GV->hasInitializer();
472 if (!IsValid) return;
474 ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer());
475 IsValid = IsValid && CA;
476 if (!IsValid) return;
478 for (unsigned i = 0, N = CA->getNumOperands(); IsValid && i < N; ++i) {
479 IsValid = IsValid && isGlobalVariable(CA->getOperand(i));
480 if (!IsValid) return;
482 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
490 //===----------------------------------------------------------------------===//
492 /// TagFromGlobal - Returns the tag number from a debug info descriptor
493 /// GlobalVariable. Return DIIValid if operand is not an unsigned int.
494 unsigned DebugInfoDesc::TagFromGlobal(GlobalVariable *GV) {
495 ConstantInt *C = getUIntOperand(GV, 0);
496 return C ? ((unsigned)C->getZExtValue() & ~LLVMDebugVersionMask) :
497 (unsigned)DW_TAG_invalid;
500 /// VersionFromGlobal - Returns the version number from a debug info
501 /// descriptor GlobalVariable. Return DIIValid if operand is not an unsigned
503 unsigned DebugInfoDesc::VersionFromGlobal(GlobalVariable *GV) {
504 ConstantInt *C = getUIntOperand(GV, 0);
505 return C ? ((unsigned)C->getZExtValue() & LLVMDebugVersionMask) :
506 (unsigned)DW_TAG_invalid;
509 /// DescFactory - Create an instance of debug info descriptor based on Tag.
510 /// Return NULL if not a recognized Tag.
511 DebugInfoDesc *DebugInfoDesc::DescFactory(unsigned Tag) {
513 case DW_TAG_anchor: return new AnchorDesc();
514 case DW_TAG_compile_unit: return new CompileUnitDesc();
515 case DW_TAG_variable: return new GlobalVariableDesc();
516 case DW_TAG_subprogram: return new SubprogramDesc();
517 case DW_TAG_lexical_block: return new BlockDesc();
518 case DW_TAG_base_type: return new BasicTypeDesc();
520 case DW_TAG_pointer_type:
521 case DW_TAG_reference_type:
522 case DW_TAG_const_type:
523 case DW_TAG_volatile_type:
524 case DW_TAG_restrict_type:
526 case DW_TAG_inheritance: return new DerivedTypeDesc(Tag);
527 case DW_TAG_array_type:
528 case DW_TAG_structure_type:
529 case DW_TAG_union_type:
530 case DW_TAG_enumeration_type:
531 case DW_TAG_vector_type:
532 case DW_TAG_subroutine_type: return new CompositeTypeDesc(Tag);
533 case DW_TAG_subrange_type: return new SubrangeDesc();
534 case DW_TAG_enumerator: return new EnumeratorDesc();
535 case DW_TAG_return_variable:
536 case DW_TAG_arg_variable:
537 case DW_TAG_auto_variable: return new VariableDesc(Tag);
543 /// getLinkage - get linkage appropriate for this type of descriptor.
545 GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const {
546 return GlobalValue::InternalLinkage;
549 /// ApplyToFields - Target the vistor to the fields of the descriptor.
551 void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) {
555 //===----------------------------------------------------------------------===//
557 AnchorDesc::AnchorDesc()
558 : DebugInfoDesc(DW_TAG_anchor)
561 AnchorDesc::AnchorDesc(AnchoredDesc *D)
562 : DebugInfoDesc(DW_TAG_anchor)
563 , AnchorTag(D->getTag())
566 // Implement isa/cast/dyncast.
567 bool AnchorDesc::classof(const DebugInfoDesc *D) {
568 return D->getTag() == DW_TAG_anchor;
571 /// getLinkage - get linkage appropriate for this type of descriptor.
573 GlobalValue::LinkageTypes AnchorDesc::getLinkage() const {
574 return GlobalValue::LinkOnceLinkage;
577 /// ApplyToFields - Target the visitor to the fields of the TransUnitDesc.
579 void AnchorDesc::ApplyToFields(DIVisitor *Visitor) {
580 DebugInfoDesc::ApplyToFields(Visitor);
582 Visitor->Apply(AnchorTag);
585 /// getDescString - Return a string used to compose global names and labels. A
586 /// A global variable name needs to be defined for each debug descriptor that is
587 /// anchored. NOTE: that each global variable named here also needs to be added
588 /// to the list of names left external in the internalizer.
589 /// ExternalNames.insert("llvm.dbg.compile_units");
590 /// ExternalNames.insert("llvm.dbg.global_variables");
591 /// ExternalNames.insert("llvm.dbg.subprograms");
592 const char *AnchorDesc::getDescString() const {
594 case DW_TAG_compile_unit: return CompileUnitDesc::AnchorString;
595 case DW_TAG_variable: return GlobalVariableDesc::AnchorString;
596 case DW_TAG_subprogram: return SubprogramDesc::AnchorString;
600 assert(0 && "Tag does not have a case for anchor string");
604 /// getTypeString - Return a string used to label this descriptors type.
606 const char *AnchorDesc::getTypeString() const {
607 return "llvm.dbg.anchor.type";
611 void AnchorDesc::dump() {
612 cerr << getDescString() << " "
613 << "Version(" << getVersion() << "), "
614 << "Tag(" << getTag() << "), "
615 << "AnchorTag(" << AnchorTag << ")\n";
619 //===----------------------------------------------------------------------===//
621 AnchoredDesc::AnchoredDesc(unsigned T)
626 /// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
628 void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) {
629 DebugInfoDesc::ApplyToFields(Visitor);
631 Visitor->Apply(Anchor);
634 //===----------------------------------------------------------------------===//
636 CompileUnitDesc::CompileUnitDesc()
637 : AnchoredDesc(DW_TAG_compile_unit)
644 // Implement isa/cast/dyncast.
645 bool CompileUnitDesc::classof(const DebugInfoDesc *D) {
646 return D->getTag() == DW_TAG_compile_unit;
649 /// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
651 void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) {
652 AnchoredDesc::ApplyToFields(Visitor);
654 // Handle cases out of sync with compiler.
655 if (getVersion() == 0) {
656 unsigned DebugVersion;
657 Visitor->Apply(DebugVersion);
660 Visitor->Apply(Language);
661 Visitor->Apply(FileName);
662 Visitor->Apply(Directory);
663 Visitor->Apply(Producer);
666 /// getDescString - Return a string used to compose global names and labels.
668 const char *CompileUnitDesc::getDescString() const {
669 return "llvm.dbg.compile_unit";
672 /// getTypeString - Return a string used to label this descriptors type.
674 const char *CompileUnitDesc::getTypeString() const {
675 return "llvm.dbg.compile_unit.type";
678 /// getAnchorString - Return a string used to label this descriptor's anchor.
680 const char *const CompileUnitDesc::AnchorString = "llvm.dbg.compile_units";
681 const char *CompileUnitDesc::getAnchorString() const {
686 void CompileUnitDesc::dump() {
687 cerr << getDescString() << " "
688 << "Version(" << getVersion() << "), "
689 << "Tag(" << getTag() << "), "
690 << "Anchor(" << getAnchor() << "), "
691 << "Language(" << Language << "), "
692 << "FileName(\"" << FileName << "\"), "
693 << "Directory(\"" << Directory << "\"), "
694 << "Producer(\"" << Producer << "\")\n";
698 //===----------------------------------------------------------------------===//
700 TypeDesc::TypeDesc(unsigned T)
712 /// ApplyToFields - Target the visitor to the fields of the TypeDesc.
714 void TypeDesc::ApplyToFields(DIVisitor *Visitor) {
715 DebugInfoDesc::ApplyToFields(Visitor);
717 Visitor->Apply(Context);
718 Visitor->Apply(Name);
719 Visitor->Apply(File);
720 Visitor->Apply(Line);
721 Visitor->Apply(Size);
722 Visitor->Apply(Align);
723 Visitor->Apply(Offset);
724 if (getVersion() > LLVMDebugVersion4) Visitor->Apply(Flags);
727 /// getDescString - Return a string used to compose global names and labels.
729 const char *TypeDesc::getDescString() const {
730 return "llvm.dbg.type";
733 /// getTypeString - Return a string used to label this descriptor's type.
735 const char *TypeDesc::getTypeString() const {
736 return "llvm.dbg.type.type";
740 void TypeDesc::dump() {
741 cerr << getDescString() << " "
742 << "Version(" << getVersion() << "), "
743 << "Tag(" << getTag() << "), "
744 << "Context(" << Context << "), "
745 << "Name(\"" << Name << "\"), "
746 << "File(" << File << "), "
747 << "Line(" << Line << "), "
748 << "Size(" << Size << "), "
749 << "Align(" << Align << "), "
750 << "Offset(" << Offset << "), "
751 << "Flags(" << Flags << ")\n";
755 //===----------------------------------------------------------------------===//
757 BasicTypeDesc::BasicTypeDesc()
758 : TypeDesc(DW_TAG_base_type)
762 // Implement isa/cast/dyncast.
763 bool BasicTypeDesc::classof(const DebugInfoDesc *D) {
764 return D->getTag() == DW_TAG_base_type;
767 /// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
769 void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
770 TypeDesc::ApplyToFields(Visitor);
772 Visitor->Apply(Encoding);
775 /// getDescString - Return a string used to compose global names and labels.
777 const char *BasicTypeDesc::getDescString() const {
778 return "llvm.dbg.basictype";
781 /// getTypeString - Return a string used to label this descriptor's type.
783 const char *BasicTypeDesc::getTypeString() const {
784 return "llvm.dbg.basictype.type";
788 void BasicTypeDesc::dump() {
789 cerr << getDescString() << " "
790 << "Version(" << getVersion() << "), "
791 << "Tag(" << getTag() << "), "
792 << "Context(" << getContext() << "), "
793 << "Name(\"" << getName() << "\"), "
794 << "Size(" << getSize() << "), "
795 << "Encoding(" << Encoding << ")\n";
799 //===----------------------------------------------------------------------===//
801 DerivedTypeDesc::DerivedTypeDesc(unsigned T)
806 // Implement isa/cast/dyncast.
807 bool DerivedTypeDesc::classof(const DebugInfoDesc *D) {
808 unsigned T = D->getTag();
811 case DW_TAG_pointer_type:
812 case DW_TAG_reference_type:
813 case DW_TAG_const_type:
814 case DW_TAG_volatile_type:
815 case DW_TAG_restrict_type:
817 case DW_TAG_inheritance:
824 /// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
826 void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) {
827 TypeDesc::ApplyToFields(Visitor);
829 Visitor->Apply(FromType);
832 /// getDescString - Return a string used to compose global names and labels.
834 const char *DerivedTypeDesc::getDescString() const {
835 return "llvm.dbg.derivedtype";
838 /// getTypeString - Return a string used to label this descriptor's type.
840 const char *DerivedTypeDesc::getTypeString() const {
841 return "llvm.dbg.derivedtype.type";
845 void DerivedTypeDesc::dump() {
846 cerr << getDescString() << " "
847 << "Version(" << getVersion() << "), "
848 << "Tag(" << getTag() << "), "
849 << "Context(" << getContext() << "), "
850 << "Name(\"" << getName() << "\"), "
851 << "Size(" << getSize() << "), "
852 << "File(" << getFile() << "), "
853 << "Line(" << getLine() << "), "
854 << "FromType(" << FromType << ")\n";
858 //===----------------------------------------------------------------------===//
860 CompositeTypeDesc::CompositeTypeDesc(unsigned T)
865 // Implement isa/cast/dyncast.
866 bool CompositeTypeDesc::classof(const DebugInfoDesc *D) {
867 unsigned T = D->getTag();
869 case DW_TAG_array_type:
870 case DW_TAG_structure_type:
871 case DW_TAG_union_type:
872 case DW_TAG_enumeration_type:
873 case DW_TAG_vector_type:
874 case DW_TAG_subroutine_type:
881 /// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
883 void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) {
884 DerivedTypeDesc::ApplyToFields(Visitor);
886 Visitor->Apply(Elements);
889 /// getDescString - Return a string used to compose global names and labels.
891 const char *CompositeTypeDesc::getDescString() const {
892 return "llvm.dbg.compositetype";
895 /// getTypeString - Return a string used to label this descriptor's type.
897 const char *CompositeTypeDesc::getTypeString() const {
898 return "llvm.dbg.compositetype.type";
902 void CompositeTypeDesc::dump() {
903 cerr << getDescString() << " "
904 << "Version(" << getVersion() << "), "
905 << "Tag(" << getTag() << "), "
906 << "Context(" << getContext() << "), "
907 << "Name(\"" << getName() << "\"), "
908 << "Size(" << getSize() << "), "
909 << "File(" << getFile() << "), "
910 << "Line(" << getLine() << "), "
911 << "FromType(" << getFromType() << "), "
912 << "Elements.size(" << Elements.size() << ")\n";
916 //===----------------------------------------------------------------------===//
918 SubrangeDesc::SubrangeDesc()
919 : DebugInfoDesc(DW_TAG_subrange_type)
924 // Implement isa/cast/dyncast.
925 bool SubrangeDesc::classof(const DebugInfoDesc *D) {
926 return D->getTag() == DW_TAG_subrange_type;
929 /// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
931 void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) {
932 DebugInfoDesc::ApplyToFields(Visitor);
938 /// getDescString - Return a string used to compose global names and labels.
940 const char *SubrangeDesc::getDescString() const {
941 return "llvm.dbg.subrange";
944 /// getTypeString - Return a string used to label this descriptor's type.
946 const char *SubrangeDesc::getTypeString() const {
947 return "llvm.dbg.subrange.type";
951 void SubrangeDesc::dump() {
952 cerr << getDescString() << " "
953 << "Version(" << getVersion() << "), "
954 << "Tag(" << getTag() << "), "
955 << "Lo(" << Lo << "), "
956 << "Hi(" << Hi << ")\n";
960 //===----------------------------------------------------------------------===//
962 EnumeratorDesc::EnumeratorDesc()
963 : DebugInfoDesc(DW_TAG_enumerator)
968 // Implement isa/cast/dyncast.
969 bool EnumeratorDesc::classof(const DebugInfoDesc *D) {
970 return D->getTag() == DW_TAG_enumerator;
973 /// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
975 void EnumeratorDesc::ApplyToFields(DIVisitor *Visitor) {
976 DebugInfoDesc::ApplyToFields(Visitor);
978 Visitor->Apply(Name);
979 Visitor->Apply(Value);
982 /// getDescString - Return a string used to compose global names and labels.
984 const char *EnumeratorDesc::getDescString() const {
985 return "llvm.dbg.enumerator";
988 /// getTypeString - Return a string used to label this descriptor's type.
990 const char *EnumeratorDesc::getTypeString() const {
991 return "llvm.dbg.enumerator.type";
995 void EnumeratorDesc::dump() {
996 cerr << getDescString() << " "
997 << "Version(" << getVersion() << "), "
998 << "Tag(" << getTag() << "), "
999 << "Name(" << Name << "), "
1000 << "Value(" << Value << ")\n";
1004 //===----------------------------------------------------------------------===//
1006 VariableDesc::VariableDesc(unsigned T)
1015 // Implement isa/cast/dyncast.
1016 bool VariableDesc::classof(const DebugInfoDesc *D) {
1017 unsigned T = D->getTag();
1019 case DW_TAG_auto_variable:
1020 case DW_TAG_arg_variable:
1021 case DW_TAG_return_variable:
1028 /// ApplyToFields - Target the visitor to the fields of the VariableDesc.
1030 void VariableDesc::ApplyToFields(DIVisitor *Visitor) {
1031 DebugInfoDesc::ApplyToFields(Visitor);
1033 Visitor->Apply(Context);
1034 Visitor->Apply(Name);
1035 Visitor->Apply(File);
1036 Visitor->Apply(Line);
1037 Visitor->Apply(TyDesc);
1040 /// getDescString - Return a string used to compose global names and labels.
1042 const char *VariableDesc::getDescString() const {
1043 return "llvm.dbg.variable";
1046 /// getTypeString - Return a string used to label this descriptor's type.
1048 const char *VariableDesc::getTypeString() const {
1049 return "llvm.dbg.variable.type";
1053 void VariableDesc::dump() {
1054 cerr << getDescString() << " "
1055 << "Version(" << getVersion() << "), "
1056 << "Tag(" << getTag() << "), "
1057 << "Context(" << Context << "), "
1058 << "Name(\"" << Name << "\"), "
1059 << "File(" << File << "), "
1060 << "Line(" << Line << "), "
1061 << "TyDesc(" << TyDesc << ")\n";
1065 //===----------------------------------------------------------------------===//
1067 GlobalDesc::GlobalDesc(unsigned T)
1077 , IsDefinition(false)
1080 /// ApplyToFields - Target the visitor to the fields of the global.
1082 void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
1083 AnchoredDesc::ApplyToFields(Visitor);
1085 Visitor->Apply(Context);
1086 Visitor->Apply(Name);
1087 Visitor->Apply(FullName);
1088 Visitor->Apply(LinkageName);
1089 Visitor->Apply(File);
1090 Visitor->Apply(Line);
1091 Visitor->Apply(TyDesc);
1092 Visitor->Apply(IsStatic);
1093 Visitor->Apply(IsDefinition);
1096 //===----------------------------------------------------------------------===//
1098 GlobalVariableDesc::GlobalVariableDesc()
1099 : GlobalDesc(DW_TAG_variable)
1103 // Implement isa/cast/dyncast.
1104 bool GlobalVariableDesc::classof(const DebugInfoDesc *D) {
1105 return D->getTag() == DW_TAG_variable;
1108 /// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
1110 void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
1111 GlobalDesc::ApplyToFields(Visitor);
1113 Visitor->Apply(Global);
1116 /// getDescString - Return a string used to compose global names and labels.
1118 const char *GlobalVariableDesc::getDescString() const {
1119 return "llvm.dbg.global_variable";
1122 /// getTypeString - Return a string used to label this descriptors type.
1124 const char *GlobalVariableDesc::getTypeString() const {
1125 return "llvm.dbg.global_variable.type";
1128 /// getAnchorString - Return a string used to label this descriptor's anchor.
1130 const char *const GlobalVariableDesc::AnchorString = "llvm.dbg.global_variables";
1131 const char *GlobalVariableDesc::getAnchorString() const {
1132 return AnchorString;
1136 void GlobalVariableDesc::dump() {
1137 cerr << getDescString() << " "
1138 << "Version(" << getVersion() << "), "
1139 << "Tag(" << getTag() << "), "
1140 << "Anchor(" << getAnchor() << "), "
1141 << "Name(\"" << getName() << "\"), "
1142 << "FullName(\"" << getFullName() << "\"), "
1143 << "LinkageName(\"" << getLinkageName() << "\"), "
1144 << "File(" << getFile() << "),"
1145 << "Line(" << getLine() << "),"
1146 << "Type(" << getType() << "), "
1147 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1148 << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
1149 << "Global(" << Global << ")\n";
1153 //===----------------------------------------------------------------------===//
1155 SubprogramDesc::SubprogramDesc()
1156 : GlobalDesc(DW_TAG_subprogram)
1159 // Implement isa/cast/dyncast.
1160 bool SubprogramDesc::classof(const DebugInfoDesc *D) {
1161 return D->getTag() == DW_TAG_subprogram;
1164 /// ApplyToFields - Target the visitor to the fields of the
1166 void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
1167 GlobalDesc::ApplyToFields(Visitor);
1170 /// getDescString - Return a string used to compose global names and labels.
1172 const char *SubprogramDesc::getDescString() const {
1173 return "llvm.dbg.subprogram";
1176 /// getTypeString - Return a string used to label this descriptors type.
1178 const char *SubprogramDesc::getTypeString() const {
1179 return "llvm.dbg.subprogram.type";
1182 /// getAnchorString - Return a string used to label this descriptor's anchor.
1184 const char *const SubprogramDesc::AnchorString = "llvm.dbg.subprograms";
1185 const char *SubprogramDesc::getAnchorString() const {
1186 return AnchorString;
1190 void SubprogramDesc::dump() {
1191 cerr << getDescString() << " "
1192 << "Version(" << getVersion() << "), "
1193 << "Tag(" << getTag() << "), "
1194 << "Anchor(" << getAnchor() << "), "
1195 << "Name(\"" << getName() << "\"), "
1196 << "FullName(\"" << getFullName() << "\"), "
1197 << "LinkageName(\"" << getLinkageName() << "\"), "
1198 << "File(" << getFile() << "),"
1199 << "Line(" << getLine() << "),"
1200 << "Type(" << getType() << "), "
1201 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1202 << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
1206 //===----------------------------------------------------------------------===//
1208 BlockDesc::BlockDesc()
1209 : DebugInfoDesc(DW_TAG_lexical_block)
1213 // Implement isa/cast/dyncast.
1214 bool BlockDesc::classof(const DebugInfoDesc *D) {
1215 return D->getTag() == DW_TAG_lexical_block;
1218 /// ApplyToFields - Target the visitor to the fields of the BlockDesc.
1220 void BlockDesc::ApplyToFields(DIVisitor *Visitor) {
1221 DebugInfoDesc::ApplyToFields(Visitor);
1223 Visitor->Apply(Context);
1226 /// getDescString - Return a string used to compose global names and labels.
1228 const char *BlockDesc::getDescString() const {
1229 return "llvm.dbg.block";
1232 /// getTypeString - Return a string used to label this descriptors type.
1234 const char *BlockDesc::getTypeString() const {
1235 return "llvm.dbg.block.type";
1239 void BlockDesc::dump() {
1240 cerr << getDescString() << " "
1241 << "Version(" << getVersion() << "), "
1242 << "Tag(" << getTag() << "),"
1243 << "Context(" << Context << ")\n";
1247 //===----------------------------------------------------------------------===//
1249 DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
1250 return Deserialize(getGlobalVariable(V));
1252 DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
1254 if (!GV) return NULL;
1256 // Check to see if it has been already deserialized.
1257 DebugInfoDesc *&Slot = GlobalDescs[GV];
1258 if (Slot) return Slot;
1260 // Get the Tag from the global.
1261 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1263 // Create an empty instance of the correct sort.
1264 Slot = DebugInfoDesc::DescFactory(Tag);
1266 // If not a user defined descriptor.
1268 // Deserialize the fields.
1269 DIDeserializeVisitor DRAM(*this, GV);
1270 DRAM.ApplyToFields(Slot);
1276 //===----------------------------------------------------------------------===//
1278 /// getStrPtrType - Return a "sbyte *" type.
1280 const PointerType *DISerializer::getStrPtrType() {
1281 // If not already defined.
1283 // Construct the pointer to signed bytes.
1284 StrPtrTy = PointerType::getUnqual(Type::Int8Ty);
1290 /// getEmptyStructPtrType - Return a "{ }*" type.
1292 const PointerType *DISerializer::getEmptyStructPtrType() {
1293 // If not already defined.
1294 if (!EmptyStructPtrTy) {
1295 // Construct the empty structure type.
1296 const StructType *EmptyStructTy =
1297 StructType::get(std::vector<const Type*>());
1298 // Construct the pointer to empty structure type.
1299 EmptyStructPtrTy = PointerType::getUnqual(EmptyStructTy);
1302 return EmptyStructPtrTy;
1305 /// getTagType - Return the type describing the specified descriptor (via tag.)
1307 const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
1308 // Attempt to get the previously defined type.
1309 StructType *&Ty = TagTypes[DD->getTag()];
1311 // If not already defined.
1313 // Set up fields vector.
1314 std::vector<const Type*> Fields;
1315 // Get types of fields.
1316 DIGetTypesVisitor GTAM(*this, Fields);
1317 GTAM.ApplyToFields(DD);
1319 // Construct structured type.
1320 Ty = StructType::get(Fields);
1322 // Register type name with module.
1323 M->addTypeName(DD->getTypeString(), Ty);
1329 /// getString - Construct the string as constant string global.
1331 Constant *DISerializer::getString(const std::string &String) {
1332 // Check string cache for previous edition.
1333 Constant *&Slot = StringCache[String];
1334 // Return Constant if previously defined.
1335 if (Slot) return Slot;
1336 // If empty string then use a sbyte* null instead.
1337 if (String.empty()) {
1338 Slot = ConstantPointerNull::get(getStrPtrType());
1340 // Construct string as an llvm constant.
1341 Constant *ConstStr = ConstantArray::get(String);
1342 // Otherwise create and return a new string global.
1343 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
1344 GlobalVariable::InternalLinkage,
1345 ConstStr, ".str", M);
1346 StrGV->setSection("llvm.metadata");
1347 // Convert to generic string pointer.
1348 Slot = ConstantExpr::getBitCast(StrGV, getStrPtrType());
1354 /// Serialize - Recursively cast the specified descriptor into a GlobalVariable
1355 /// so that it can be serialized to a .bc or .ll file.
1356 GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
1357 // Check if the DebugInfoDesc is already in the map.
1358 GlobalVariable *&Slot = DescGlobals[DD];
1360 // See if DebugInfoDesc exists, if so return prior GlobalVariable.
1361 if (Slot) return Slot;
1363 // Get the type associated with the Tag.
1364 const StructType *Ty = getTagType(DD);
1366 // Create the GlobalVariable early to prevent infinite recursion.
1367 GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(),
1368 NULL, DD->getDescString(), M);
1369 GV->setSection("llvm.metadata");
1371 // Insert new GlobalVariable in DescGlobals map.
1374 // Set up elements vector
1375 std::vector<Constant*> Elements;
1377 DISerializeVisitor SRAM(*this, Elements);
1378 SRAM.ApplyToFields(DD);
1380 // Set the globals initializer.
1381 GV->setInitializer(ConstantStruct::get(Ty, Elements));
1386 /// addDescriptor - Directly connect DD with existing GV.
1387 void DISerializer::addDescriptor(DebugInfoDesc *DD,
1388 GlobalVariable *GV) {
1389 DescGlobals[DD] = GV;
1392 //===----------------------------------------------------------------------===//
1394 /// Verify - Return true if the GlobalVariable appears to be a valid
1395 /// serialization of a DebugInfoDesc.
1396 bool DIVerifier::Verify(Value *V) {
1397 return !V || Verify(getGlobalVariable(V));
1399 bool DIVerifier::Verify(GlobalVariable *GV) {
1401 if (!GV) return true;
1403 // Check prior validity.
1404 unsigned &ValiditySlot = Validity[GV];
1406 // If visited before then use old state.
1407 if (ValiditySlot) return ValiditySlot == Valid;
1409 // Assume validity for the time being (recursion.)
1410 ValiditySlot = Valid;
1412 // Make sure the global is internal or link once (anchor.)
1413 if (GV->getLinkage() != GlobalValue::InternalLinkage &&
1414 GV->getLinkage() != GlobalValue::LinkOnceLinkage) {
1415 ValiditySlot = Invalid;
1420 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1422 // Check for user defined descriptors.
1423 if (Tag == DW_TAG_invalid) {
1424 ValiditySlot = Valid;
1429 unsigned Version = DebugInfoDesc::VersionFromGlobal(GV);
1431 // Check for version mismatch.
1432 if (Version != LLVMDebugVersion) {
1433 ValiditySlot = Invalid;
1437 // Construct an empty DebugInfoDesc.
1438 DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
1440 // Allow for user defined descriptors.
1441 if (!DD) return true;
1443 // Get the initializer constant.
1444 ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
1446 // Get the operand count.
1447 unsigned N = CI->getNumOperands();
1449 // Get the field count.
1450 unsigned &CountSlot = Counts[Tag];
1452 // Check the operand count to the field count
1453 DICountVisitor CTAM;
1454 CTAM.ApplyToFields(DD);
1455 CountSlot = CTAM.getCount();
1458 // Field count must be at most equal operand count.
1459 if (CountSlot > N) {
1461 ValiditySlot = Invalid;
1465 // Check each field for valid type.
1466 DIVerifyVisitor VRAM(*this, GV);
1467 VRAM.ApplyToFields(DD);
1469 // Release empty DebugInfoDesc.
1472 // If fields are not valid.
1473 if (!VRAM.isValid()) {
1474 ValiditySlot = Invalid;
1481 /// isVerified - Return true if the specified GV has already been
1482 /// verified as a debug information descriptor.
1483 bool DIVerifier::isVerified(GlobalVariable *GV) {
1484 unsigned &ValiditySlot = Validity[GV];
1485 if (ValiditySlot) return ValiditySlot == Valid;
1489 //===----------------------------------------------------------------------===//
1491 DebugScope::~DebugScope() {
1492 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1493 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
1496 //===----------------------------------------------------------------------===//
1498 MachineModuleInfo::MachineModuleInfo()
1499 : ImmutablePass((intptr_t)&ID)
1513 , CallsUnwindInit(0)
1515 // Always emit "no personality" info
1516 Personalities.push_back(NULL);
1518 MachineModuleInfo::~MachineModuleInfo() {
1522 /// doInitialization - Initialize the state for a new module.
1524 bool MachineModuleInfo::doInitialization() {
1528 /// doFinalization - Tear down the state after completion of a module.
1530 bool MachineModuleInfo::doFinalization() {
1534 /// BeginFunction - Begin gathering function meta information.
1536 void MachineModuleInfo::BeginFunction(MachineFunction *MF) {
1540 /// EndFunction - Discard function meta information.
1542 void MachineModuleInfo::EndFunction() {
1543 // Clean up scope information.
1550 // Clean up line info.
1553 // Clean up frame info.
1556 // Clean up exception info.
1557 LandingPads.clear();
1562 CallsUnwindInit = 0;
1565 /// getDescFor - Convert a Value to a debug information descriptor.
1567 // FIXME - use new Value type when available.
1568 DebugInfoDesc *MachineModuleInfo::getDescFor(Value *V) {
1569 return DR.Deserialize(V);
1572 /// AnalyzeModule - Scan the module for global debug information.
1574 void MachineModuleInfo::AnalyzeModule(Module &M) {
1575 SetupCompileUnits(M);
1577 // Insert functions in the llvm.used array into UsedFunctions.
1578 GlobalVariable *GV = M.getGlobalVariable("llvm.used");
1579 if (!GV || !GV->hasInitializer()) return;
1581 // Should be an array of 'i8*'.
1582 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
1583 if (InitList == 0) return;
1585 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
1586 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(InitList->getOperand(i)))
1587 if (CE->getOpcode() == Instruction::BitCast)
1588 if (Function *F = dyn_cast<Function>(CE->getOperand(0)))
1589 UsedFunctions.insert(F);
1593 /// SetupCompileUnits - Set up the unique vector of compile units.
1595 void MachineModuleInfo::SetupCompileUnits(Module &M) {
1596 std::vector<CompileUnitDesc *>CU = getAnchoredDescriptors<CompileUnitDesc>(M);
1598 for (unsigned i = 0, N = CU.size(); i < N; i++) {
1599 CompileUnits.insert(CU[i]);
1603 /// getCompileUnits - Return a vector of debug compile units.
1605 const UniqueVector<CompileUnitDesc *> MachineModuleInfo::getCompileUnits()const{
1606 return CompileUnits;
1609 /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1610 /// named GlobalVariable.
1611 std::vector<GlobalVariable*>
1612 MachineModuleInfo::getGlobalVariablesUsing(Module &M,
1613 const std::string &RootName) {
1614 return ::getGlobalVariablesUsing(M, RootName);
1617 /// RecordSourceLine - Records location information and associates it with a
1618 /// debug label. Returns a unique label ID used to generate a label and
1619 /// provide correspondence to the source line list.
1620 unsigned MachineModuleInfo::RecordSourceLine(unsigned Line, unsigned Column,
1622 unsigned ID = NextLabelID();
1623 Lines.push_back(SourceLineInfo(Line, Column, Source, ID));
1627 /// RecordSource - Register a source file with debug info. Returns an source
1629 unsigned MachineModuleInfo::RecordSource(const std::string &Directory,
1630 const std::string &Source) {
1631 unsigned DirectoryID = Directories.insert(Directory);
1632 return SourceFiles.insert(SourceFileInfo(DirectoryID, Source));
1634 unsigned MachineModuleInfo::RecordSource(const CompileUnitDesc *CompileUnit) {
1635 return RecordSource(CompileUnit->getDirectory(),
1636 CompileUnit->getFileName());
1639 /// RecordRegionStart - Indicate the start of a region.
1641 unsigned MachineModuleInfo::RecordRegionStart(Value *V) {
1642 // FIXME - need to be able to handle split scopes because of bb cloning.
1643 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1644 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1645 unsigned ID = NextLabelID();
1646 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
1650 /// RecordRegionEnd - Indicate the end of a region.
1652 unsigned MachineModuleInfo::RecordRegionEnd(Value *V) {
1653 // FIXME - need to be able to handle split scopes because of bb cloning.
1654 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1655 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1656 unsigned ID = NextLabelID();
1657 Scope->setEndLabelID(ID);
1661 /// RecordVariable - Indicate the declaration of a local variable.
1663 void MachineModuleInfo::RecordVariable(GlobalValue *GV, unsigned FrameIndex) {
1664 VariableDesc *VD = cast<VariableDesc>(DR.Deserialize(GV));
1665 DebugScope *Scope = getOrCreateScope(VD->getContext());
1666 DebugVariable *DV = new DebugVariable(VD, FrameIndex);
1667 Scope->AddVariable(DV);
1670 /// getOrCreateScope - Returns the scope associated with the given descriptor.
1672 DebugScope *MachineModuleInfo::getOrCreateScope(DebugInfoDesc *ScopeDesc) {
1673 DebugScope *&Slot = ScopeMap[ScopeDesc];
1675 // FIXME - breaks down when the context is an inlined function.
1676 DebugInfoDesc *ParentDesc = NULL;
1677 if (BlockDesc *Block = dyn_cast<BlockDesc>(ScopeDesc)) {
1678 ParentDesc = Block->getContext();
1680 DebugScope *Parent = ParentDesc ? getOrCreateScope(ParentDesc) : NULL;
1681 Slot = new DebugScope(Parent, ScopeDesc);
1683 Parent->AddScope(Slot);
1684 } else if (RootScope) {
1685 // FIXME - Add inlined function scopes to the root so we can delete
1686 // them later. Long term, handle inlined functions properly.
1687 RootScope->AddScope(Slot);
1689 // First function is top level function.
1696 //===-EH-------------------------------------------------------------------===//
1698 /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
1699 /// specified MachineBasicBlock.
1700 LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo
1701 (MachineBasicBlock *LandingPad) {
1702 unsigned N = LandingPads.size();
1703 for (unsigned i = 0; i < N; ++i) {
1704 LandingPadInfo &LP = LandingPads[i];
1705 if (LP.LandingPadBlock == LandingPad)
1709 LandingPads.push_back(LandingPadInfo(LandingPad));
1710 return LandingPads[N];
1713 /// addInvoke - Provide the begin and end labels of an invoke style call and
1714 /// associate it with a try landing pad block.
1715 void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad,
1716 unsigned BeginLabel, unsigned EndLabel) {
1717 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1718 LP.BeginLabels.push_back(BeginLabel);
1719 LP.EndLabels.push_back(EndLabel);
1722 /// addLandingPad - Provide the label of a try LandingPad block.
1724 unsigned MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) {
1725 unsigned LandingPadLabel = NextLabelID();
1726 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1727 LP.LandingPadLabel = LandingPadLabel;
1728 return LandingPadLabel;
1731 /// addPersonality - Provide the personality function for the exception
1733 void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad,
1734 Function *Personality) {
1735 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1736 LP.Personality = Personality;
1738 for (unsigned i = 0; i < Personalities.size(); ++i)
1739 if (Personalities[i] == Personality)
1742 Personalities.push_back(Personality);
1745 /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
1747 void MachineModuleInfo::addCatchTypeInfo(MachineBasicBlock *LandingPad,
1748 std::vector<GlobalVariable *> &TyInfo) {
1749 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1750 for (unsigned N = TyInfo.size(); N; --N)
1751 LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
1754 /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
1756 void MachineModuleInfo::addFilterTypeInfo(MachineBasicBlock *LandingPad,
1757 std::vector<GlobalVariable *> &TyInfo) {
1758 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1759 std::vector<unsigned> IdsInFilter (TyInfo.size());
1760 for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
1761 IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
1762 LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
1765 /// addCleanup - Add a cleanup action for a landing pad.
1767 void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) {
1768 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1769 LP.TypeIds.push_back(0);
1772 /// TidyLandingPads - Remap landing pad labels and remove any deleted landing
1774 void MachineModuleInfo::TidyLandingPads() {
1775 for (unsigned i = 0; i != LandingPads.size(); ) {
1776 LandingPadInfo &LandingPad = LandingPads[i];
1777 LandingPad.LandingPadLabel = MappedLabel(LandingPad.LandingPadLabel);
1779 // Special case: we *should* emit LPs with null LP MBB. This indicates
1781 if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
1782 LandingPads.erase(LandingPads.begin() + i);
1786 for (unsigned j=0; j != LandingPads[i].BeginLabels.size(); ) {
1787 unsigned BeginLabel = MappedLabel(LandingPad.BeginLabels[j]);
1788 unsigned EndLabel = MappedLabel(LandingPad.EndLabels[j]);
1790 if (!BeginLabel || !EndLabel) {
1791 LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
1792 LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
1796 LandingPad.BeginLabels[j] = BeginLabel;
1797 LandingPad.EndLabels[j] = EndLabel;
1801 // Remove landing pads with no try-ranges.
1802 if (LandingPads[i].BeginLabels.empty()) {
1803 LandingPads.erase(LandingPads.begin() + i);
1807 // If there is no landing pad, ensure that the list of typeids is empty.
1808 // If the only typeid is a cleanup, this is the same as having no typeids.
1809 if (!LandingPad.LandingPadBlock ||
1810 (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
1811 LandingPad.TypeIds.clear();
1817 /// getTypeIDFor - Return the type id for the specified typeinfo. This is
1819 unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) {
1820 for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
1821 if (TypeInfos[i] == TI) return i + 1;
1823 TypeInfos.push_back(TI);
1824 return TypeInfos.size();
1827 /// getFilterIDFor - Return the filter id for the specified typeinfos. This is
1829 int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) {
1830 // If the new filter coincides with the tail of an existing filter, then
1831 // re-use the existing filter. Folding filters more than this requires
1832 // re-ordering filters and/or their elements - probably not worth it.
1833 for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
1834 E = FilterEnds.end(); I != E; ++I) {
1835 unsigned i = *I, j = TyIds.size();
1838 if (FilterIds[--i] != TyIds[--j])
1842 // The new filter coincides with range [i, end) of the existing filter.
1848 // Add the new filter.
1849 int FilterID = -(1 + FilterIds.size());
1850 FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
1851 for (unsigned I = 0, N = TyIds.size(); I != N; ++I)
1852 FilterIds.push_back(TyIds[I]);
1853 FilterEnds.push_back(FilterIds.size());
1854 FilterIds.push_back(0); // terminator
1858 /// getPersonality - Return the personality function for the current function.
1859 Function *MachineModuleInfo::getPersonality() const {
1860 // FIXME: Until PR1414 will be fixed, we're using 1 personality function per
1862 return !LandingPads.empty() ? LandingPads[0].Personality : NULL;
1865 /// getPersonalityIndex - Return unique index for current personality
1866 /// function. NULL personality function should always get zero index.
1867 unsigned MachineModuleInfo::getPersonalityIndex() const {
1868 const Function* Personality = NULL;
1870 // Scan landing pads. If there is at least one non-NULL personality - use it.
1871 for (unsigned i = 0; i != LandingPads.size(); ++i)
1872 if (LandingPads[i].Personality) {
1873 Personality = LandingPads[i].Personality;
1877 for (unsigned i = 0; i < Personalities.size(); ++i) {
1878 if (Personalities[i] == Personality)
1882 // This should never happen
1883 assert(0 && "Personality function should be set!");
1887 //===----------------------------------------------------------------------===//
1888 /// DebugLabelFolding pass - This pass prunes out redundant labels. This allows
1889 /// a info consumer to determine if the range of two labels is empty, by seeing
1890 /// if the labels map to the same reduced label.
1894 struct DebugLabelFolder : public MachineFunctionPass {
1896 DebugLabelFolder() : MachineFunctionPass((intptr_t)&ID) {}
1898 virtual bool runOnMachineFunction(MachineFunction &MF);
1899 virtual const char *getPassName() const { return "Label Folder"; }
1902 char DebugLabelFolder::ID = 0;
1904 bool DebugLabelFolder::runOnMachineFunction(MachineFunction &MF) {
1905 // Get machine module info.
1906 MachineModuleInfo *MMI = getAnalysisToUpdate<MachineModuleInfo>();
1907 if (!MMI) return false;
1909 // Track if change is made.
1910 bool MadeChange = false;
1911 // No prior label to begin.
1912 unsigned PriorLabel = 0;
1914 // Iterate through basic blocks.
1915 for (MachineFunction::iterator BB = MF.begin(), E = MF.end();
1917 // Iterate through instructions.
1918 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1920 if (I->isDebugLabel()) {
1921 // The label ID # is always operand #0, an immediate.
1922 unsigned NextLabel = I->getOperand(0).getImm();
1924 // If there was an immediate prior label.
1926 // Remap the current label to prior label.
1927 MMI->RemapLabel(NextLabel, PriorLabel);
1928 // Delete the current label.
1930 // Indicate a change has been made.
1934 // Start a new round.
1935 PriorLabel = NextLabel;
1938 // No consecutive labels.
1949 FunctionPass *createDebugLabelFoldingPass() { return new DebugLabelFolder(); }