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++);
234 // Fills in the string if it succeeds
235 if (!GetConstantStringInfo(C, Field))
238 virtual void Apply(DebugInfoDesc *&Field) {
239 Constant *C = CI->getOperand(I++);
240 Field = DR.Deserialize(C);
242 virtual void Apply(GlobalVariable *&Field) {
243 Constant *C = CI->getOperand(I++);
244 Field = getGlobalVariable(C);
246 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
248 Constant *C = CI->getOperand(I++);
249 GlobalVariable *GV = getGlobalVariable(C);
250 if (GV->hasInitializer()) {
251 if (ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer())) {
252 for (unsigned i = 0, N = CA->getNumOperands(); i < N; ++i) {
253 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
254 DebugInfoDesc *DE = DR.Deserialize(GVE);
257 } else if (GV->getInitializer()->isNullValue()) {
258 if (const ArrayType *T =
259 dyn_cast<ArrayType>(GV->getType()->getElementType())) {
260 Field.resize(T->getNumElements());
267 //===----------------------------------------------------------------------===//
268 /// DISerializeVisitor - This DIVisitor serializes all the fields in
269 /// the supplied DebugInfoDesc.
270 class DISerializeVisitor : public DIVisitor {
272 DISerializer &SR; // Active serializer.
273 std::vector<Constant*> &Elements; // Element accumulator.
276 DISerializeVisitor(DISerializer &S, std::vector<Constant*> &E)
282 /// Apply - Set the value of each of the fields.
284 virtual void Apply(int &Field) {
285 Elements.push_back(ConstantInt::get(Type::Int32Ty, int32_t(Field)));
287 virtual void Apply(unsigned &Field) {
288 Elements.push_back(ConstantInt::get(Type::Int32Ty, uint32_t(Field)));
290 virtual void Apply(int64_t &Field) {
291 Elements.push_back(ConstantInt::get(Type::Int64Ty, int64_t(Field)));
293 virtual void Apply(uint64_t &Field) {
294 Elements.push_back(ConstantInt::get(Type::Int64Ty, uint64_t(Field)));
296 virtual void Apply(bool &Field) {
297 Elements.push_back(ConstantInt::get(Type::Int1Ty, Field));
299 virtual void Apply(std::string &Field) {
300 Elements.push_back(SR.getString(Field));
302 virtual void Apply(DebugInfoDesc *&Field) {
303 GlobalVariable *GV = NULL;
305 // If non-NULL then convert to global.
306 if (Field) GV = SR.Serialize(Field);
308 // FIXME - At some point should use specific type.
309 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
312 // Set to pointer to global.
313 Elements.push_back(ConstantExpr::getBitCast(GV, EmptyTy));
316 Elements.push_back(ConstantPointerNull::get(EmptyTy));
319 virtual void Apply(GlobalVariable *&Field) {
320 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
322 Elements.push_back(ConstantExpr::getBitCast(Field, EmptyTy));
324 Elements.push_back(ConstantPointerNull::get(EmptyTy));
327 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
328 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
329 unsigned N = Field.size();
330 ArrayType *AT = ArrayType::get(EmptyTy, N);
331 std::vector<Constant *> ArrayElements;
333 for (unsigned i = 0, N = Field.size(); i < N; ++i) {
334 if (DebugInfoDesc *Element = Field[i]) {
335 GlobalVariable *GVE = SR.Serialize(Element);
336 Constant *CE = ConstantExpr::getBitCast(GVE, EmptyTy);
337 ArrayElements.push_back(cast<Constant>(CE));
339 ArrayElements.push_back(ConstantPointerNull::get(EmptyTy));
343 Constant *CA = ConstantArray::get(AT, ArrayElements);
344 GlobalVariable *CAGV = new GlobalVariable(AT, true,
345 GlobalValue::InternalLinkage,
346 CA, "llvm.dbg.array",
348 CAGV->setSection("llvm.metadata");
349 Constant *CAE = ConstantExpr::getBitCast(CAGV, EmptyTy);
350 Elements.push_back(CAE);
354 //===----------------------------------------------------------------------===//
355 /// DIGetTypesVisitor - This DIVisitor gathers all the field types in
356 /// the supplied DebugInfoDesc.
357 class DIGetTypesVisitor : public DIVisitor {
359 DISerializer &SR; // Active serializer.
360 std::vector<const Type*> &Fields; // Type accumulator.
363 DIGetTypesVisitor(DISerializer &S, std::vector<const Type*> &F)
369 /// Apply - Set the value of each of the fields.
371 virtual void Apply(int &Field) {
372 Fields.push_back(Type::Int32Ty);
374 virtual void Apply(unsigned &Field) {
375 Fields.push_back(Type::Int32Ty);
377 virtual void Apply(int64_t &Field) {
378 Fields.push_back(Type::Int64Ty);
380 virtual void Apply(uint64_t &Field) {
381 Fields.push_back(Type::Int64Ty);
383 virtual void Apply(bool &Field) {
384 Fields.push_back(Type::Int1Ty);
386 virtual void Apply(std::string &Field) {
387 Fields.push_back(SR.getStrPtrType());
389 virtual void Apply(DebugInfoDesc *&Field) {
390 // FIXME - At some point should use specific type.
391 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
392 Fields.push_back(EmptyTy);
394 virtual void Apply(GlobalVariable *&Field) {
395 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
396 Fields.push_back(EmptyTy);
398 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
399 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
400 Fields.push_back(EmptyTy);
404 //===----------------------------------------------------------------------===//
405 /// DIVerifyVisitor - This DIVisitor verifies all the field types against
406 /// a constant initializer.
407 class DIVerifyVisitor : public DIVisitor {
409 DIVerifier &VR; // Active verifier.
410 bool IsValid; // Validity status.
411 unsigned I; // Current operand index.
412 ConstantStruct *CI; // GlobalVariable constant initializer.
415 DIVerifyVisitor(DIVerifier &V, GlobalVariable *GV)
420 , CI(cast<ConstantStruct>(GV->getInitializer()))
425 bool isValid() const { return IsValid; }
427 /// Apply - Set the value of each of the fields.
429 virtual void Apply(int &Field) {
430 Constant *C = CI->getOperand(I++);
431 IsValid = IsValid && isa<ConstantInt>(C);
433 virtual void Apply(unsigned &Field) {
434 Constant *C = CI->getOperand(I++);
435 IsValid = IsValid && isa<ConstantInt>(C);
437 virtual void Apply(int64_t &Field) {
438 Constant *C = CI->getOperand(I++);
439 IsValid = IsValid && isa<ConstantInt>(C);
441 virtual void Apply(uint64_t &Field) {
442 Constant *C = CI->getOperand(I++);
443 IsValid = IsValid && isa<ConstantInt>(C);
445 virtual void Apply(bool &Field) {
446 Constant *C = CI->getOperand(I++);
447 IsValid = IsValid && isa<ConstantInt>(C) && C->getType() == Type::Int1Ty;
449 virtual void Apply(std::string &Field) {
450 Constant *C = CI->getOperand(I++);
452 (!C || isStringValue(C) || C->isNullValue());
454 virtual void Apply(DebugInfoDesc *&Field) {
455 // FIXME - Prepare the correct descriptor.
456 Constant *C = CI->getOperand(I++);
457 IsValid = IsValid && isGlobalVariable(C);
459 virtual void Apply(GlobalVariable *&Field) {
460 Constant *C = CI->getOperand(I++);
461 IsValid = IsValid && isGlobalVariable(C);
463 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
464 Constant *C = CI->getOperand(I++);
465 IsValid = IsValid && isGlobalVariable(C);
466 if (!IsValid) return;
468 GlobalVariable *GV = getGlobalVariable(C);
469 IsValid = IsValid && GV && GV->hasInitializer();
470 if (!IsValid) return;
472 ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer());
473 IsValid = IsValid && CA;
474 if (!IsValid) return;
476 for (unsigned i = 0, N = CA->getNumOperands(); IsValid && i < N; ++i) {
477 IsValid = IsValid && isGlobalVariable(CA->getOperand(i));
478 if (!IsValid) return;
480 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
488 //===----------------------------------------------------------------------===//
490 /// TagFromGlobal - Returns the tag number from a debug info descriptor
491 /// GlobalVariable. Return DIIValid if operand is not an unsigned int.
492 unsigned DebugInfoDesc::TagFromGlobal(GlobalVariable *GV) {
493 ConstantInt *C = getUIntOperand(GV, 0);
494 return C ? ((unsigned)C->getZExtValue() & ~LLVMDebugVersionMask) :
495 (unsigned)DW_TAG_invalid;
498 /// VersionFromGlobal - Returns the version number from a debug info
499 /// descriptor GlobalVariable. Return DIIValid if operand is not an unsigned
501 unsigned DebugInfoDesc::VersionFromGlobal(GlobalVariable *GV) {
502 ConstantInt *C = getUIntOperand(GV, 0);
503 return C ? ((unsigned)C->getZExtValue() & LLVMDebugVersionMask) :
504 (unsigned)DW_TAG_invalid;
507 /// DescFactory - Create an instance of debug info descriptor based on Tag.
508 /// Return NULL if not a recognized Tag.
509 DebugInfoDesc *DebugInfoDesc::DescFactory(unsigned Tag) {
511 case DW_TAG_anchor: return new AnchorDesc();
512 case DW_TAG_compile_unit: return new CompileUnitDesc();
513 case DW_TAG_variable: return new GlobalVariableDesc();
514 case DW_TAG_subprogram: return new SubprogramDesc();
515 case DW_TAG_lexical_block: return new BlockDesc();
516 case DW_TAG_base_type: return new BasicTypeDesc();
518 case DW_TAG_pointer_type:
519 case DW_TAG_reference_type:
520 case DW_TAG_const_type:
521 case DW_TAG_volatile_type:
522 case DW_TAG_restrict_type:
524 case DW_TAG_inheritance: return new DerivedTypeDesc(Tag);
525 case DW_TAG_array_type:
526 case DW_TAG_structure_type:
527 case DW_TAG_union_type:
528 case DW_TAG_enumeration_type:
529 case DW_TAG_vector_type:
530 case DW_TAG_subroutine_type: return new CompositeTypeDesc(Tag);
531 case DW_TAG_subrange_type: return new SubrangeDesc();
532 case DW_TAG_enumerator: return new EnumeratorDesc();
533 case DW_TAG_return_variable:
534 case DW_TAG_arg_variable:
535 case DW_TAG_auto_variable: return new VariableDesc(Tag);
541 /// getLinkage - get linkage appropriate for this type of descriptor.
543 GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const {
544 return GlobalValue::InternalLinkage;
547 /// ApplyToFields - Target the vistor to the fields of the descriptor.
549 void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) {
553 //===----------------------------------------------------------------------===//
555 AnchorDesc::AnchorDesc()
556 : DebugInfoDesc(DW_TAG_anchor)
559 AnchorDesc::AnchorDesc(AnchoredDesc *D)
560 : DebugInfoDesc(DW_TAG_anchor)
561 , AnchorTag(D->getTag())
564 // Implement isa/cast/dyncast.
565 bool AnchorDesc::classof(const DebugInfoDesc *D) {
566 return D->getTag() == DW_TAG_anchor;
569 /// getLinkage - get linkage appropriate for this type of descriptor.
571 GlobalValue::LinkageTypes AnchorDesc::getLinkage() const {
572 return GlobalValue::LinkOnceLinkage;
575 /// ApplyToFields - Target the visitor to the fields of the TransUnitDesc.
577 void AnchorDesc::ApplyToFields(DIVisitor *Visitor) {
578 DebugInfoDesc::ApplyToFields(Visitor);
580 Visitor->Apply(AnchorTag);
583 /// getDescString - Return a string used to compose global names and labels. A
584 /// A global variable name needs to be defined for each debug descriptor that is
585 /// anchored. NOTE: that each global variable named here also needs to be added
586 /// to the list of names left external in the internalizer.
587 /// ExternalNames.insert("llvm.dbg.compile_units");
588 /// ExternalNames.insert("llvm.dbg.global_variables");
589 /// ExternalNames.insert("llvm.dbg.subprograms");
590 const char *AnchorDesc::getDescString() const {
592 case DW_TAG_compile_unit: return CompileUnitDesc::AnchorString;
593 case DW_TAG_variable: return GlobalVariableDesc::AnchorString;
594 case DW_TAG_subprogram: return SubprogramDesc::AnchorString;
598 assert(0 && "Tag does not have a case for anchor string");
602 /// getTypeString - Return a string used to label this descriptors type.
604 const char *AnchorDesc::getTypeString() const {
605 return "llvm.dbg.anchor.type";
609 void AnchorDesc::dump() {
610 cerr << getDescString() << " "
611 << "Version(" << getVersion() << "), "
612 << "Tag(" << getTag() << "), "
613 << "AnchorTag(" << AnchorTag << ")\n";
617 //===----------------------------------------------------------------------===//
619 AnchoredDesc::AnchoredDesc(unsigned T)
624 /// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
626 void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) {
627 DebugInfoDesc::ApplyToFields(Visitor);
629 Visitor->Apply(Anchor);
632 //===----------------------------------------------------------------------===//
634 CompileUnitDesc::CompileUnitDesc()
635 : AnchoredDesc(DW_TAG_compile_unit)
642 // Implement isa/cast/dyncast.
643 bool CompileUnitDesc::classof(const DebugInfoDesc *D) {
644 return D->getTag() == DW_TAG_compile_unit;
647 /// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
649 void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) {
650 AnchoredDesc::ApplyToFields(Visitor);
652 // Handle cases out of sync with compiler.
653 if (getVersion() == 0) {
654 unsigned DebugVersion;
655 Visitor->Apply(DebugVersion);
658 Visitor->Apply(Language);
659 Visitor->Apply(FileName);
660 Visitor->Apply(Directory);
661 Visitor->Apply(Producer);
664 /// getDescString - Return a string used to compose global names and labels.
666 const char *CompileUnitDesc::getDescString() const {
667 return "llvm.dbg.compile_unit";
670 /// getTypeString - Return a string used to label this descriptors type.
672 const char *CompileUnitDesc::getTypeString() const {
673 return "llvm.dbg.compile_unit.type";
676 /// getAnchorString - Return a string used to label this descriptor's anchor.
678 const char *const CompileUnitDesc::AnchorString = "llvm.dbg.compile_units";
679 const char *CompileUnitDesc::getAnchorString() const {
684 void CompileUnitDesc::dump() {
685 cerr << getDescString() << " "
686 << "Version(" << getVersion() << "), "
687 << "Tag(" << getTag() << "), "
688 << "Anchor(" << getAnchor() << "), "
689 << "Language(" << Language << "), "
690 << "FileName(\"" << FileName << "\"), "
691 << "Directory(\"" << Directory << "\"), "
692 << "Producer(\"" << Producer << "\")\n";
696 //===----------------------------------------------------------------------===//
698 TypeDesc::TypeDesc(unsigned T)
710 /// ApplyToFields - Target the visitor to the fields of the TypeDesc.
712 void TypeDesc::ApplyToFields(DIVisitor *Visitor) {
713 DebugInfoDesc::ApplyToFields(Visitor);
715 Visitor->Apply(Context);
716 Visitor->Apply(Name);
717 Visitor->Apply(File);
718 Visitor->Apply(Line);
719 Visitor->Apply(Size);
720 Visitor->Apply(Align);
721 Visitor->Apply(Offset);
722 if (getVersion() > LLVMDebugVersion4) Visitor->Apply(Flags);
725 /// getDescString - Return a string used to compose global names and labels.
727 const char *TypeDesc::getDescString() const {
728 return "llvm.dbg.type";
731 /// getTypeString - Return a string used to label this descriptor's type.
733 const char *TypeDesc::getTypeString() const {
734 return "llvm.dbg.type.type";
738 void TypeDesc::dump() {
739 cerr << getDescString() << " "
740 << "Version(" << getVersion() << "), "
741 << "Tag(" << getTag() << "), "
742 << "Context(" << Context << "), "
743 << "Name(\"" << Name << "\"), "
744 << "File(" << File << "), "
745 << "Line(" << Line << "), "
746 << "Size(" << Size << "), "
747 << "Align(" << Align << "), "
748 << "Offset(" << Offset << "), "
749 << "Flags(" << Flags << ")\n";
753 //===----------------------------------------------------------------------===//
755 BasicTypeDesc::BasicTypeDesc()
756 : TypeDesc(DW_TAG_base_type)
760 // Implement isa/cast/dyncast.
761 bool BasicTypeDesc::classof(const DebugInfoDesc *D) {
762 return D->getTag() == DW_TAG_base_type;
765 /// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
767 void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
768 TypeDesc::ApplyToFields(Visitor);
770 Visitor->Apply(Encoding);
773 /// getDescString - Return a string used to compose global names and labels.
775 const char *BasicTypeDesc::getDescString() const {
776 return "llvm.dbg.basictype";
779 /// getTypeString - Return a string used to label this descriptor's type.
781 const char *BasicTypeDesc::getTypeString() const {
782 return "llvm.dbg.basictype.type";
786 void BasicTypeDesc::dump() {
787 cerr << getDescString() << " "
788 << "Version(" << getVersion() << "), "
789 << "Tag(" << getTag() << "), "
790 << "Context(" << getContext() << "), "
791 << "Name(\"" << getName() << "\"), "
792 << "Size(" << getSize() << "), "
793 << "Encoding(" << Encoding << ")\n";
797 //===----------------------------------------------------------------------===//
799 DerivedTypeDesc::DerivedTypeDesc(unsigned T)
804 // Implement isa/cast/dyncast.
805 bool DerivedTypeDesc::classof(const DebugInfoDesc *D) {
806 unsigned T = D->getTag();
809 case DW_TAG_pointer_type:
810 case DW_TAG_reference_type:
811 case DW_TAG_const_type:
812 case DW_TAG_volatile_type:
813 case DW_TAG_restrict_type:
815 case DW_TAG_inheritance:
822 /// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
824 void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) {
825 TypeDesc::ApplyToFields(Visitor);
827 Visitor->Apply(FromType);
830 /// getDescString - Return a string used to compose global names and labels.
832 const char *DerivedTypeDesc::getDescString() const {
833 return "llvm.dbg.derivedtype";
836 /// getTypeString - Return a string used to label this descriptor's type.
838 const char *DerivedTypeDesc::getTypeString() const {
839 return "llvm.dbg.derivedtype.type";
843 void DerivedTypeDesc::dump() {
844 cerr << getDescString() << " "
845 << "Version(" << getVersion() << "), "
846 << "Tag(" << getTag() << "), "
847 << "Context(" << getContext() << "), "
848 << "Name(\"" << getName() << "\"), "
849 << "Size(" << getSize() << "), "
850 << "File(" << getFile() << "), "
851 << "Line(" << getLine() << "), "
852 << "FromType(" << FromType << ")\n";
856 //===----------------------------------------------------------------------===//
858 CompositeTypeDesc::CompositeTypeDesc(unsigned T)
863 // Implement isa/cast/dyncast.
864 bool CompositeTypeDesc::classof(const DebugInfoDesc *D) {
865 unsigned T = D->getTag();
867 case DW_TAG_array_type:
868 case DW_TAG_structure_type:
869 case DW_TAG_union_type:
870 case DW_TAG_enumeration_type:
871 case DW_TAG_vector_type:
872 case DW_TAG_subroutine_type:
879 /// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
881 void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) {
882 DerivedTypeDesc::ApplyToFields(Visitor);
884 Visitor->Apply(Elements);
887 /// getDescString - Return a string used to compose global names and labels.
889 const char *CompositeTypeDesc::getDescString() const {
890 return "llvm.dbg.compositetype";
893 /// getTypeString - Return a string used to label this descriptor's type.
895 const char *CompositeTypeDesc::getTypeString() const {
896 return "llvm.dbg.compositetype.type";
900 void CompositeTypeDesc::dump() {
901 cerr << getDescString() << " "
902 << "Version(" << getVersion() << "), "
903 << "Tag(" << getTag() << "), "
904 << "Context(" << getContext() << "), "
905 << "Name(\"" << getName() << "\"), "
906 << "Size(" << getSize() << "), "
907 << "File(" << getFile() << "), "
908 << "Line(" << getLine() << "), "
909 << "FromType(" << getFromType() << "), "
910 << "Elements.size(" << Elements.size() << ")\n";
914 //===----------------------------------------------------------------------===//
916 SubrangeDesc::SubrangeDesc()
917 : DebugInfoDesc(DW_TAG_subrange_type)
922 // Implement isa/cast/dyncast.
923 bool SubrangeDesc::classof(const DebugInfoDesc *D) {
924 return D->getTag() == DW_TAG_subrange_type;
927 /// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
929 void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) {
930 DebugInfoDesc::ApplyToFields(Visitor);
936 /// getDescString - Return a string used to compose global names and labels.
938 const char *SubrangeDesc::getDescString() const {
939 return "llvm.dbg.subrange";
942 /// getTypeString - Return a string used to label this descriptor's type.
944 const char *SubrangeDesc::getTypeString() const {
945 return "llvm.dbg.subrange.type";
949 void SubrangeDesc::dump() {
950 cerr << getDescString() << " "
951 << "Version(" << getVersion() << "), "
952 << "Tag(" << getTag() << "), "
953 << "Lo(" << Lo << "), "
954 << "Hi(" << Hi << ")\n";
958 //===----------------------------------------------------------------------===//
960 EnumeratorDesc::EnumeratorDesc()
961 : DebugInfoDesc(DW_TAG_enumerator)
966 // Implement isa/cast/dyncast.
967 bool EnumeratorDesc::classof(const DebugInfoDesc *D) {
968 return D->getTag() == DW_TAG_enumerator;
971 /// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
973 void EnumeratorDesc::ApplyToFields(DIVisitor *Visitor) {
974 DebugInfoDesc::ApplyToFields(Visitor);
976 Visitor->Apply(Name);
977 Visitor->Apply(Value);
980 /// getDescString - Return a string used to compose global names and labels.
982 const char *EnumeratorDesc::getDescString() const {
983 return "llvm.dbg.enumerator";
986 /// getTypeString - Return a string used to label this descriptor's type.
988 const char *EnumeratorDesc::getTypeString() const {
989 return "llvm.dbg.enumerator.type";
993 void EnumeratorDesc::dump() {
994 cerr << getDescString() << " "
995 << "Version(" << getVersion() << "), "
996 << "Tag(" << getTag() << "), "
997 << "Name(" << Name << "), "
998 << "Value(" << Value << ")\n";
1002 //===----------------------------------------------------------------------===//
1004 VariableDesc::VariableDesc(unsigned T)
1013 // Implement isa/cast/dyncast.
1014 bool VariableDesc::classof(const DebugInfoDesc *D) {
1015 unsigned T = D->getTag();
1017 case DW_TAG_auto_variable:
1018 case DW_TAG_arg_variable:
1019 case DW_TAG_return_variable:
1026 /// ApplyToFields - Target the visitor to the fields of the VariableDesc.
1028 void VariableDesc::ApplyToFields(DIVisitor *Visitor) {
1029 DebugInfoDesc::ApplyToFields(Visitor);
1031 Visitor->Apply(Context);
1032 Visitor->Apply(Name);
1033 Visitor->Apply(File);
1034 Visitor->Apply(Line);
1035 Visitor->Apply(TyDesc);
1038 /// getDescString - Return a string used to compose global names and labels.
1040 const char *VariableDesc::getDescString() const {
1041 return "llvm.dbg.variable";
1044 /// getTypeString - Return a string used to label this descriptor's type.
1046 const char *VariableDesc::getTypeString() const {
1047 return "llvm.dbg.variable.type";
1051 void VariableDesc::dump() {
1052 cerr << getDescString() << " "
1053 << "Version(" << getVersion() << "), "
1054 << "Tag(" << getTag() << "), "
1055 << "Context(" << Context << "), "
1056 << "Name(\"" << Name << "\"), "
1057 << "File(" << File << "), "
1058 << "Line(" << Line << "), "
1059 << "TyDesc(" << TyDesc << ")\n";
1063 //===----------------------------------------------------------------------===//
1065 GlobalDesc::GlobalDesc(unsigned T)
1075 , IsDefinition(false)
1078 /// ApplyToFields - Target the visitor to the fields of the global.
1080 void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
1081 AnchoredDesc::ApplyToFields(Visitor);
1083 Visitor->Apply(Context);
1084 Visitor->Apply(Name);
1085 Visitor->Apply(FullName);
1086 Visitor->Apply(LinkageName);
1087 Visitor->Apply(File);
1088 Visitor->Apply(Line);
1089 Visitor->Apply(TyDesc);
1090 Visitor->Apply(IsStatic);
1091 Visitor->Apply(IsDefinition);
1094 //===----------------------------------------------------------------------===//
1096 GlobalVariableDesc::GlobalVariableDesc()
1097 : GlobalDesc(DW_TAG_variable)
1101 // Implement isa/cast/dyncast.
1102 bool GlobalVariableDesc::classof(const DebugInfoDesc *D) {
1103 return D->getTag() == DW_TAG_variable;
1106 /// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
1108 void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
1109 GlobalDesc::ApplyToFields(Visitor);
1111 Visitor->Apply(Global);
1114 /// getDescString - Return a string used to compose global names and labels.
1116 const char *GlobalVariableDesc::getDescString() const {
1117 return "llvm.dbg.global_variable";
1120 /// getTypeString - Return a string used to label this descriptors type.
1122 const char *GlobalVariableDesc::getTypeString() const {
1123 return "llvm.dbg.global_variable.type";
1126 /// getAnchorString - Return a string used to label this descriptor's anchor.
1128 const char *const GlobalVariableDesc::AnchorString = "llvm.dbg.global_variables";
1129 const char *GlobalVariableDesc::getAnchorString() const {
1130 return AnchorString;
1134 void GlobalVariableDesc::dump() {
1135 cerr << getDescString() << " "
1136 << "Version(" << getVersion() << "), "
1137 << "Tag(" << getTag() << "), "
1138 << "Anchor(" << getAnchor() << "), "
1139 << "Name(\"" << getName() << "\"), "
1140 << "FullName(\"" << getFullName() << "\"), "
1141 << "LinkageName(\"" << getLinkageName() << "\"), "
1142 << "File(" << getFile() << "),"
1143 << "Line(" << getLine() << "),"
1144 << "Type(" << getType() << "), "
1145 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1146 << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
1147 << "Global(" << Global << ")\n";
1151 //===----------------------------------------------------------------------===//
1153 SubprogramDesc::SubprogramDesc()
1154 : GlobalDesc(DW_TAG_subprogram)
1157 // Implement isa/cast/dyncast.
1158 bool SubprogramDesc::classof(const DebugInfoDesc *D) {
1159 return D->getTag() == DW_TAG_subprogram;
1162 /// ApplyToFields - Target the visitor to the fields of the
1164 void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
1165 GlobalDesc::ApplyToFields(Visitor);
1168 /// getDescString - Return a string used to compose global names and labels.
1170 const char *SubprogramDesc::getDescString() const {
1171 return "llvm.dbg.subprogram";
1174 /// getTypeString - Return a string used to label this descriptors type.
1176 const char *SubprogramDesc::getTypeString() const {
1177 return "llvm.dbg.subprogram.type";
1180 /// getAnchorString - Return a string used to label this descriptor's anchor.
1182 const char *const SubprogramDesc::AnchorString = "llvm.dbg.subprograms";
1183 const char *SubprogramDesc::getAnchorString() const {
1184 return AnchorString;
1188 void SubprogramDesc::dump() {
1189 cerr << getDescString() << " "
1190 << "Version(" << getVersion() << "), "
1191 << "Tag(" << getTag() << "), "
1192 << "Anchor(" << getAnchor() << "), "
1193 << "Name(\"" << getName() << "\"), "
1194 << "FullName(\"" << getFullName() << "\"), "
1195 << "LinkageName(\"" << getLinkageName() << "\"), "
1196 << "File(" << getFile() << "),"
1197 << "Line(" << getLine() << "),"
1198 << "Type(" << getType() << "), "
1199 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1200 << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
1204 //===----------------------------------------------------------------------===//
1206 BlockDesc::BlockDesc()
1207 : DebugInfoDesc(DW_TAG_lexical_block)
1211 // Implement isa/cast/dyncast.
1212 bool BlockDesc::classof(const DebugInfoDesc *D) {
1213 return D->getTag() == DW_TAG_lexical_block;
1216 /// ApplyToFields - Target the visitor to the fields of the BlockDesc.
1218 void BlockDesc::ApplyToFields(DIVisitor *Visitor) {
1219 DebugInfoDesc::ApplyToFields(Visitor);
1221 Visitor->Apply(Context);
1224 /// getDescString - Return a string used to compose global names and labels.
1226 const char *BlockDesc::getDescString() const {
1227 return "llvm.dbg.block";
1230 /// getTypeString - Return a string used to label this descriptors type.
1232 const char *BlockDesc::getTypeString() const {
1233 return "llvm.dbg.block.type";
1237 void BlockDesc::dump() {
1238 cerr << getDescString() << " "
1239 << "Version(" << getVersion() << "), "
1240 << "Tag(" << getTag() << "),"
1241 << "Context(" << Context << ")\n";
1245 //===----------------------------------------------------------------------===//
1247 DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
1248 return Deserialize(getGlobalVariable(V));
1250 DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
1252 if (!GV) return NULL;
1254 // Check to see if it has been already deserialized.
1255 DebugInfoDesc *&Slot = GlobalDescs[GV];
1256 if (Slot) return Slot;
1258 // Get the Tag from the global.
1259 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1261 // Create an empty instance of the correct sort.
1262 Slot = DebugInfoDesc::DescFactory(Tag);
1264 // If not a user defined descriptor.
1266 // Deserialize the fields.
1267 DIDeserializeVisitor DRAM(*this, GV);
1268 DRAM.ApplyToFields(Slot);
1274 //===----------------------------------------------------------------------===//
1276 /// getStrPtrType - Return a "sbyte *" type.
1278 const PointerType *DISerializer::getStrPtrType() {
1279 // If not already defined.
1281 // Construct the pointer to signed bytes.
1282 StrPtrTy = PointerType::getUnqual(Type::Int8Ty);
1288 /// getEmptyStructPtrType - Return a "{ }*" type.
1290 const PointerType *DISerializer::getEmptyStructPtrType() {
1291 // If not already defined.
1292 if (!EmptyStructPtrTy) {
1293 // Construct the empty structure type.
1294 const StructType *EmptyStructTy =
1295 StructType::get(std::vector<const Type*>());
1296 // Construct the pointer to empty structure type.
1297 EmptyStructPtrTy = PointerType::getUnqual(EmptyStructTy);
1300 return EmptyStructPtrTy;
1303 /// getTagType - Return the type describing the specified descriptor (via tag.)
1305 const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
1306 // Attempt to get the previously defined type.
1307 StructType *&Ty = TagTypes[DD->getTag()];
1309 // If not already defined.
1311 // Set up fields vector.
1312 std::vector<const Type*> Fields;
1313 // Get types of fields.
1314 DIGetTypesVisitor GTAM(*this, Fields);
1315 GTAM.ApplyToFields(DD);
1317 // Construct structured type.
1318 Ty = StructType::get(Fields);
1320 // Register type name with module.
1321 M->addTypeName(DD->getTypeString(), Ty);
1327 /// getString - Construct the string as constant string global.
1329 Constant *DISerializer::getString(const std::string &String) {
1330 // Check string cache for previous edition.
1331 Constant *&Slot = StringCache[String];
1332 // Return Constant if previously defined.
1333 if (Slot) return Slot;
1334 // If empty string then use a sbyte* null instead.
1335 if (String.empty()) {
1336 Slot = ConstantPointerNull::get(getStrPtrType());
1338 // Construct string as an llvm constant.
1339 Constant *ConstStr = ConstantArray::get(String);
1340 // Otherwise create and return a new string global.
1341 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
1342 GlobalVariable::InternalLinkage,
1343 ConstStr, ".str", M);
1344 StrGV->setSection("llvm.metadata");
1345 // Convert to generic string pointer.
1346 Slot = ConstantExpr::getBitCast(StrGV, getStrPtrType());
1352 /// Serialize - Recursively cast the specified descriptor into a GlobalVariable
1353 /// so that it can be serialized to a .bc or .ll file.
1354 GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
1355 // Check if the DebugInfoDesc is already in the map.
1356 GlobalVariable *&Slot = DescGlobals[DD];
1358 // See if DebugInfoDesc exists, if so return prior GlobalVariable.
1359 if (Slot) return Slot;
1361 // Get the type associated with the Tag.
1362 const StructType *Ty = getTagType(DD);
1364 // Create the GlobalVariable early to prevent infinite recursion.
1365 GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(),
1366 NULL, DD->getDescString(), M);
1367 GV->setSection("llvm.metadata");
1369 // Insert new GlobalVariable in DescGlobals map.
1372 // Set up elements vector
1373 std::vector<Constant*> Elements;
1375 DISerializeVisitor SRAM(*this, Elements);
1376 SRAM.ApplyToFields(DD);
1378 // Set the globals initializer.
1379 GV->setInitializer(ConstantStruct::get(Ty, Elements));
1384 /// addDescriptor - Directly connect DD with existing GV.
1385 void DISerializer::addDescriptor(DebugInfoDesc *DD,
1386 GlobalVariable *GV) {
1387 DescGlobals[DD] = GV;
1390 //===----------------------------------------------------------------------===//
1392 /// Verify - Return true if the GlobalVariable appears to be a valid
1393 /// serialization of a DebugInfoDesc.
1394 bool DIVerifier::Verify(Value *V) {
1395 return !V || Verify(getGlobalVariable(V));
1397 bool DIVerifier::Verify(GlobalVariable *GV) {
1399 if (!GV) return true;
1401 // Check prior validity.
1402 unsigned &ValiditySlot = Validity[GV];
1404 // If visited before then use old state.
1405 if (ValiditySlot) return ValiditySlot == Valid;
1407 // Assume validity for the time being (recursion.)
1408 ValiditySlot = Valid;
1410 // Make sure the global is internal or link once (anchor.)
1411 if (GV->getLinkage() != GlobalValue::InternalLinkage &&
1412 GV->getLinkage() != GlobalValue::LinkOnceLinkage) {
1413 ValiditySlot = Invalid;
1418 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1420 // Check for user defined descriptors.
1421 if (Tag == DW_TAG_invalid) {
1422 ValiditySlot = Valid;
1427 unsigned Version = DebugInfoDesc::VersionFromGlobal(GV);
1429 // Check for version mismatch.
1430 if (Version != LLVMDebugVersion) {
1431 ValiditySlot = Invalid;
1435 // Construct an empty DebugInfoDesc.
1436 DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
1438 // Allow for user defined descriptors.
1439 if (!DD) return true;
1441 // Get the initializer constant.
1442 ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
1444 // Get the operand count.
1445 unsigned N = CI->getNumOperands();
1447 // Get the field count.
1448 unsigned &CountSlot = Counts[Tag];
1450 // Check the operand count to the field count
1451 DICountVisitor CTAM;
1452 CTAM.ApplyToFields(DD);
1453 CountSlot = CTAM.getCount();
1456 // Field count must be at most equal operand count.
1457 if (CountSlot > N) {
1459 ValiditySlot = Invalid;
1463 // Check each field for valid type.
1464 DIVerifyVisitor VRAM(*this, GV);
1465 VRAM.ApplyToFields(DD);
1467 // Release empty DebugInfoDesc.
1470 // If fields are not valid.
1471 if (!VRAM.isValid()) {
1472 ValiditySlot = Invalid;
1479 /// isVerified - Return true if the specified GV has already been
1480 /// verified as a debug information descriptor.
1481 bool DIVerifier::isVerified(GlobalVariable *GV) {
1482 unsigned &ValiditySlot = Validity[GV];
1483 if (ValiditySlot) return ValiditySlot == Valid;
1487 //===----------------------------------------------------------------------===//
1489 DebugScope::~DebugScope() {
1490 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1491 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
1494 //===----------------------------------------------------------------------===//
1496 MachineModuleInfo::MachineModuleInfo()
1497 : ImmutablePass((intptr_t)&ID)
1511 , CallsUnwindInit(0)
1513 // Always emit "no personality" info
1514 Personalities.push_back(NULL);
1516 MachineModuleInfo::~MachineModuleInfo() {
1520 /// doInitialization - Initialize the state for a new module.
1522 bool MachineModuleInfo::doInitialization() {
1526 /// doFinalization - Tear down the state after completion of a module.
1528 bool MachineModuleInfo::doFinalization() {
1532 /// BeginFunction - Begin gathering function meta information.
1534 void MachineModuleInfo::BeginFunction(MachineFunction *MF) {
1538 /// EndFunction - Discard function meta information.
1540 void MachineModuleInfo::EndFunction() {
1541 // Clean up scope information.
1548 // Clean up line info.
1551 // Clean up frame info.
1554 // Clean up exception info.
1555 LandingPads.clear();
1560 CallsUnwindInit = 0;
1563 /// getDescFor - Convert a Value to a debug information descriptor.
1565 // FIXME - use new Value type when available.
1566 DebugInfoDesc *MachineModuleInfo::getDescFor(Value *V) {
1567 return DR.Deserialize(V);
1570 /// AnalyzeModule - Scan the module for global debug information.
1572 void MachineModuleInfo::AnalyzeModule(Module &M) {
1573 SetupCompileUnits(M);
1575 // Insert functions in the llvm.used array into UsedFunctions.
1576 GlobalVariable *GV = M.getGlobalVariable("llvm.used");
1577 if (!GV || !GV->hasInitializer()) return;
1579 // Should be an array of 'i8*'.
1580 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
1581 if (InitList == 0) return;
1583 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
1584 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(InitList->getOperand(i)))
1585 if (CE->getOpcode() == Instruction::BitCast)
1586 if (Function *F = dyn_cast<Function>(CE->getOperand(0)))
1587 UsedFunctions.insert(F);
1591 /// SetupCompileUnits - Set up the unique vector of compile units.
1593 void MachineModuleInfo::SetupCompileUnits(Module &M) {
1594 std::vector<CompileUnitDesc *>CU = getAnchoredDescriptors<CompileUnitDesc>(M);
1596 for (unsigned i = 0, N = CU.size(); i < N; i++) {
1597 CompileUnits.insert(CU[i]);
1601 /// getCompileUnits - Return a vector of debug compile units.
1603 const UniqueVector<CompileUnitDesc *> MachineModuleInfo::getCompileUnits()const{
1604 return CompileUnits;
1607 /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1608 /// named GlobalVariable.
1609 std::vector<GlobalVariable*>
1610 MachineModuleInfo::getGlobalVariablesUsing(Module &M,
1611 const std::string &RootName) {
1612 return ::getGlobalVariablesUsing(M, RootName);
1615 /// RecordSourceLine - Records location information and associates it with a
1616 /// debug label. Returns a unique label ID used to generate a label and
1617 /// provide correspondence to the source line list.
1618 unsigned MachineModuleInfo::RecordSourceLine(unsigned Line, unsigned Column,
1620 unsigned ID = NextLabelID();
1621 Lines.push_back(SourceLineInfo(Line, Column, Source, ID));
1625 /// RecordSource - Register a source file with debug info. Returns an source
1627 unsigned MachineModuleInfo::RecordSource(const std::string &Directory,
1628 const std::string &Source) {
1629 unsigned DirectoryID = Directories.insert(Directory);
1630 return SourceFiles.insert(SourceFileInfo(DirectoryID, Source));
1632 unsigned MachineModuleInfo::RecordSource(const CompileUnitDesc *CompileUnit) {
1633 return RecordSource(CompileUnit->getDirectory(),
1634 CompileUnit->getFileName());
1637 /// RecordRegionStart - Indicate the start of a region.
1639 unsigned MachineModuleInfo::RecordRegionStart(Value *V) {
1640 // FIXME - need to be able to handle split scopes because of bb cloning.
1641 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1642 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1643 unsigned ID = NextLabelID();
1644 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
1648 /// RecordRegionEnd - Indicate the end of a region.
1650 unsigned MachineModuleInfo::RecordRegionEnd(Value *V) {
1651 // FIXME - need to be able to handle split scopes because of bb cloning.
1652 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1653 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1654 unsigned ID = NextLabelID();
1655 Scope->setEndLabelID(ID);
1659 /// RecordVariable - Indicate the declaration of a local variable.
1661 void MachineModuleInfo::RecordVariable(GlobalValue *GV, unsigned FrameIndex) {
1662 VariableDesc *VD = cast<VariableDesc>(DR.Deserialize(GV));
1663 DebugScope *Scope = getOrCreateScope(VD->getContext());
1664 DebugVariable *DV = new DebugVariable(VD, FrameIndex);
1665 Scope->AddVariable(DV);
1668 /// getOrCreateScope - Returns the scope associated with the given descriptor.
1670 DebugScope *MachineModuleInfo::getOrCreateScope(DebugInfoDesc *ScopeDesc) {
1671 DebugScope *&Slot = ScopeMap[ScopeDesc];
1673 // FIXME - breaks down when the context is an inlined function.
1674 DebugInfoDesc *ParentDesc = NULL;
1675 if (BlockDesc *Block = dyn_cast<BlockDesc>(ScopeDesc)) {
1676 ParentDesc = Block->getContext();
1678 DebugScope *Parent = ParentDesc ? getOrCreateScope(ParentDesc) : NULL;
1679 Slot = new DebugScope(Parent, ScopeDesc);
1681 Parent->AddScope(Slot);
1682 } else if (RootScope) {
1683 // FIXME - Add inlined function scopes to the root so we can delete
1684 // them later. Long term, handle inlined functions properly.
1685 RootScope->AddScope(Slot);
1687 // First function is top level function.
1694 //===-EH-------------------------------------------------------------------===//
1696 /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
1697 /// specified MachineBasicBlock.
1698 LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo
1699 (MachineBasicBlock *LandingPad) {
1700 unsigned N = LandingPads.size();
1701 for (unsigned i = 0; i < N; ++i) {
1702 LandingPadInfo &LP = LandingPads[i];
1703 if (LP.LandingPadBlock == LandingPad)
1707 LandingPads.push_back(LandingPadInfo(LandingPad));
1708 return LandingPads[N];
1711 /// addInvoke - Provide the begin and end labels of an invoke style call and
1712 /// associate it with a try landing pad block.
1713 void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad,
1714 unsigned BeginLabel, unsigned EndLabel) {
1715 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1716 LP.BeginLabels.push_back(BeginLabel);
1717 LP.EndLabels.push_back(EndLabel);
1720 /// addLandingPad - Provide the label of a try LandingPad block.
1722 unsigned MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) {
1723 unsigned LandingPadLabel = NextLabelID();
1724 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1725 LP.LandingPadLabel = LandingPadLabel;
1726 return LandingPadLabel;
1729 /// addPersonality - Provide the personality function for the exception
1731 void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad,
1732 Function *Personality) {
1733 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1734 LP.Personality = Personality;
1736 for (unsigned i = 0; i < Personalities.size(); ++i)
1737 if (Personalities[i] == Personality)
1740 Personalities.push_back(Personality);
1743 /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
1745 void MachineModuleInfo::addCatchTypeInfo(MachineBasicBlock *LandingPad,
1746 std::vector<GlobalVariable *> &TyInfo) {
1747 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1748 for (unsigned N = TyInfo.size(); N; --N)
1749 LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
1752 /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
1754 void MachineModuleInfo::addFilterTypeInfo(MachineBasicBlock *LandingPad,
1755 std::vector<GlobalVariable *> &TyInfo) {
1756 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1757 std::vector<unsigned> IdsInFilter (TyInfo.size());
1758 for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
1759 IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
1760 LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
1763 /// addCleanup - Add a cleanup action for a landing pad.
1765 void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) {
1766 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1767 LP.TypeIds.push_back(0);
1770 /// TidyLandingPads - Remap landing pad labels and remove any deleted landing
1772 void MachineModuleInfo::TidyLandingPads() {
1773 for (unsigned i = 0; i != LandingPads.size(); ) {
1774 LandingPadInfo &LandingPad = LandingPads[i];
1775 LandingPad.LandingPadLabel = MappedLabel(LandingPad.LandingPadLabel);
1777 // Special case: we *should* emit LPs with null LP MBB. This indicates
1779 if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
1780 LandingPads.erase(LandingPads.begin() + i);
1784 for (unsigned j=0; j != LandingPads[i].BeginLabels.size(); ) {
1785 unsigned BeginLabel = MappedLabel(LandingPad.BeginLabels[j]);
1786 unsigned EndLabel = MappedLabel(LandingPad.EndLabels[j]);
1788 if (!BeginLabel || !EndLabel) {
1789 LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
1790 LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
1794 LandingPad.BeginLabels[j] = BeginLabel;
1795 LandingPad.EndLabels[j] = EndLabel;
1799 // Remove landing pads with no try-ranges.
1800 if (LandingPads[i].BeginLabels.empty()) {
1801 LandingPads.erase(LandingPads.begin() + i);
1805 // If there is no landing pad, ensure that the list of typeids is empty.
1806 // If the only typeid is a cleanup, this is the same as having no typeids.
1807 if (!LandingPad.LandingPadBlock ||
1808 (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
1809 LandingPad.TypeIds.clear();
1815 /// getTypeIDFor - Return the type id for the specified typeinfo. This is
1817 unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) {
1818 for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
1819 if (TypeInfos[i] == TI) return i + 1;
1821 TypeInfos.push_back(TI);
1822 return TypeInfos.size();
1825 /// getFilterIDFor - Return the filter id for the specified typeinfos. This is
1827 int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) {
1828 // If the new filter coincides with the tail of an existing filter, then
1829 // re-use the existing filter. Folding filters more than this requires
1830 // re-ordering filters and/or their elements - probably not worth it.
1831 for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
1832 E = FilterEnds.end(); I != E; ++I) {
1833 unsigned i = *I, j = TyIds.size();
1836 if (FilterIds[--i] != TyIds[--j])
1840 // The new filter coincides with range [i, end) of the existing filter.
1846 // Add the new filter.
1847 int FilterID = -(1 + FilterIds.size());
1848 FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
1849 for (unsigned I = 0, N = TyIds.size(); I != N; ++I)
1850 FilterIds.push_back(TyIds[I]);
1851 FilterEnds.push_back(FilterIds.size());
1852 FilterIds.push_back(0); // terminator
1856 /// getPersonality - Return the personality function for the current function.
1857 Function *MachineModuleInfo::getPersonality() const {
1858 // FIXME: Until PR1414 will be fixed, we're using 1 personality function per
1860 return !LandingPads.empty() ? LandingPads[0].Personality : NULL;
1863 /// getPersonalityIndex - Return unique index for current personality
1864 /// function. NULL personality function should always get zero index.
1865 unsigned MachineModuleInfo::getPersonalityIndex() const {
1866 const Function* Personality = NULL;
1868 // Scan landing pads. If there is at least one non-NULL personality - use it.
1869 for (unsigned i = 0; i != LandingPads.size(); ++i)
1870 if (LandingPads[i].Personality) {
1871 Personality = LandingPads[i].Personality;
1875 for (unsigned i = 0; i < Personalities.size(); ++i) {
1876 if (Personalities[i] == Personality)
1880 // This should never happen
1881 assert(0 && "Personality function should be set!");
1885 //===----------------------------------------------------------------------===//
1886 /// DebugLabelFolding pass - This pass prunes out redundant labels. This allows
1887 /// a info consumer to determine if the range of two labels is empty, by seeing
1888 /// if the labels map to the same reduced label.
1892 struct DebugLabelFolder : public MachineFunctionPass {
1894 DebugLabelFolder() : MachineFunctionPass((intptr_t)&ID) {}
1896 virtual bool runOnMachineFunction(MachineFunction &MF);
1897 virtual const char *getPassName() const { return "Label Folder"; }
1900 char DebugLabelFolder::ID = 0;
1902 bool DebugLabelFolder::runOnMachineFunction(MachineFunction &MF) {
1903 // Get machine module info.
1904 MachineModuleInfo *MMI = getAnalysisToUpdate<MachineModuleInfo>();
1905 if (!MMI) return false;
1907 // Track if change is made.
1908 bool MadeChange = false;
1909 // No prior label to begin.
1910 unsigned PriorLabel = 0;
1912 // Iterate through basic blocks.
1913 for (MachineFunction::iterator BB = MF.begin(), E = MF.end();
1915 // Iterate through instructions.
1916 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1918 if (I->isDebugLabel()) {
1919 // The label ID # is always operand #0, an immediate.
1920 unsigned NextLabel = I->getOperand(0).getImm();
1922 // If there was an immediate prior label.
1924 // Remap the current label to prior label.
1925 MMI->RemapLabel(NextLabel, PriorLabel);
1926 // Delete the current label.
1928 // Indicate a change has been made.
1932 // Start a new round.
1933 PriorLabel = NextLabel;
1936 // No consecutive labels.
1947 FunctionPass *createDebugLabelFoldingPass() { return new DebugLabelFolder(); }