1 //===-- llvm/CodeGen/MachineModuleInfo.cpp ----------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by James M. Laskey and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/CodeGen/MachineModuleInfo.h"
12 #include "llvm/Constants.h"
13 #include "llvm/CodeGen/MachineFunctionPass.h"
14 #include "llvm/CodeGen/MachineFunction.h"
15 #include "llvm/CodeGen/MachineLocation.h"
16 #include "llvm/Target/TargetInstrInfo.h"
17 #include "llvm/Target/TargetMachine.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/GlobalVariable.h"
20 #include "llvm/Intrinsics.h"
21 #include "llvm/Instructions.h"
22 #include "llvm/Module.h"
23 #include "llvm/Support/Dwarf.h"
24 #include "llvm/Support/Streams.h"
26 using namespace llvm::dwarf;
28 // Handle the Pass registration stuff necessary to use TargetData's.
30 RegisterPass<MachineModuleInfo> X("machinemoduleinfo", "Module Information");
33 //===----------------------------------------------------------------------===//
35 /// getGlobalVariablesUsing - Return all of the GlobalVariables which have the
36 /// specified value in their initializer somewhere.
38 getGlobalVariablesUsing(Value *V, std::vector<GlobalVariable*> &Result) {
39 // Scan though value users.
40 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
41 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I)) {
42 // If the user is a GlobalVariable then add to result.
44 } else if (Constant *C = dyn_cast<Constant>(*I)) {
45 // If the user is a constant variable then scan its users
46 getGlobalVariablesUsing(C, Result);
51 /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
52 /// named GlobalVariable.
53 static std::vector<GlobalVariable*>
54 getGlobalVariablesUsing(Module &M, const std::string &RootName) {
55 std::vector<GlobalVariable*> Result; // GlobalVariables matching criteria.
57 std::vector<const Type*> FieldTypes;
58 FieldTypes.push_back(Type::Int32Ty);
59 FieldTypes.push_back(Type::Int32Ty);
61 // Get the GlobalVariable root.
62 GlobalVariable *UseRoot = M.getGlobalVariable(RootName,
63 StructType::get(FieldTypes));
65 // If present and linkonce then scan for users.
66 if (UseRoot && UseRoot->hasLinkOnceLinkage()) {
67 getGlobalVariablesUsing(UseRoot, Result);
73 /// isStringValue - Return true if the given value can be coerced to a string.
75 static bool isStringValue(Value *V) {
76 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
77 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
78 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
79 return Init->isString();
81 } else if (Constant *C = dyn_cast<Constant>(V)) {
82 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
83 return isStringValue(GV);
84 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
85 if (CE->getOpcode() == Instruction::GetElementPtr) {
86 if (CE->getNumOperands() == 3 &&
87 cast<Constant>(CE->getOperand(1))->isNullValue() &&
88 isa<ConstantInt>(CE->getOperand(2))) {
89 return isStringValue(CE->getOperand(0));
97 /// getGlobalVariable - Return either a direct or cast Global value.
99 static GlobalVariable *getGlobalVariable(Value *V) {
100 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
102 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
103 if (CE->getOpcode() == Instruction::BitCast) {
104 return dyn_cast<GlobalVariable>(CE->getOperand(0));
110 /// isGlobalVariable - Return true if the given value can be coerced to a
112 static bool isGlobalVariable(Value *V) {
113 if (isa<GlobalVariable>(V) || isa<ConstantPointerNull>(V)) {
115 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
116 if (CE->getOpcode() == Instruction::BitCast) {
117 return isa<GlobalVariable>(CE->getOperand(0));
123 /// getUIntOperand - Return ith operand if it is an unsigned integer.
125 static ConstantInt *getUIntOperand(GlobalVariable *GV, unsigned i) {
126 // Make sure the GlobalVariable has an initializer.
127 if (!GV->hasInitializer()) return NULL;
129 // Get the initializer constant.
130 ConstantStruct *CI = dyn_cast<ConstantStruct>(GV->getInitializer());
131 if (!CI) return NULL;
133 // Check if there is at least i + 1 operands.
134 unsigned N = CI->getNumOperands();
135 if (i >= N) return NULL;
138 return dyn_cast<ConstantInt>(CI->getOperand(i));
141 //===----------------------------------------------------------------------===//
143 /// ApplyToFields - Target the visitor to each field of the debug information
145 void DIVisitor::ApplyToFields(DebugInfoDesc *DD) {
146 DD->ApplyToFields(this);
149 //===----------------------------------------------------------------------===//
150 /// DICountVisitor - This DIVisitor counts all the fields in the supplied debug
151 /// the supplied DebugInfoDesc.
152 class DICountVisitor : public DIVisitor {
154 unsigned Count; // Running count of fields.
157 DICountVisitor() : DIVisitor(), Count(0) {}
160 unsigned getCount() const { return Count; }
162 /// Apply - Count each of the fields.
164 virtual void Apply(int &Field) { ++Count; }
165 virtual void Apply(unsigned &Field) { ++Count; }
166 virtual void Apply(int64_t &Field) { ++Count; }
167 virtual void Apply(uint64_t &Field) { ++Count; }
168 virtual void Apply(bool &Field) { ++Count; }
169 virtual void Apply(std::string &Field) { ++Count; }
170 virtual void Apply(DebugInfoDesc *&Field) { ++Count; }
171 virtual void Apply(GlobalVariable *&Field) { ++Count; }
172 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
177 //===----------------------------------------------------------------------===//
178 /// DIDeserializeVisitor - This DIVisitor deserializes all the fields in the
179 /// supplied DebugInfoDesc.
180 class DIDeserializeVisitor : public DIVisitor {
182 DIDeserializer &DR; // Active deserializer.
183 unsigned I; // Current operand index.
184 ConstantStruct *CI; // GlobalVariable constant initializer.
187 DIDeserializeVisitor(DIDeserializer &D, GlobalVariable *GV)
191 , CI(cast<ConstantStruct>(GV->getInitializer()))
194 /// Apply - Set the value of each of the fields.
196 virtual void Apply(int &Field) {
197 Constant *C = CI->getOperand(I++);
198 Field = cast<ConstantInt>(C)->getSExtValue();
200 virtual void Apply(unsigned &Field) {
201 Constant *C = CI->getOperand(I++);
202 Field = cast<ConstantInt>(C)->getZExtValue();
204 virtual void Apply(int64_t &Field) {
205 Constant *C = CI->getOperand(I++);
206 Field = cast<ConstantInt>(C)->getSExtValue();
208 virtual void Apply(uint64_t &Field) {
209 Constant *C = CI->getOperand(I++);
210 Field = cast<ConstantInt>(C)->getZExtValue();
212 virtual void Apply(bool &Field) {
213 Constant *C = CI->getOperand(I++);
214 Field = cast<ConstantInt>(C)->getZExtValue();
216 virtual void Apply(std::string &Field) {
217 Constant *C = CI->getOperand(I++);
218 Field = C->getStringValue();
220 virtual void Apply(DebugInfoDesc *&Field) {
221 Constant *C = CI->getOperand(I++);
222 Field = DR.Deserialize(C);
224 virtual void Apply(GlobalVariable *&Field) {
225 Constant *C = CI->getOperand(I++);
226 Field = getGlobalVariable(C);
228 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
230 Constant *C = CI->getOperand(I++);
231 GlobalVariable *GV = getGlobalVariable(C);
232 if (GV->hasInitializer()) {
233 if (ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer())) {
234 for (unsigned i = 0, N = CA->getNumOperands(); i < N; ++i) {
235 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
236 DebugInfoDesc *DE = DR.Deserialize(GVE);
239 } else if (GV->getInitializer()->isNullValue()) {
240 if (const ArrayType *T =
241 dyn_cast<ArrayType>(GV->getType()->getElementType())) {
242 Field.resize(T->getNumElements());
249 //===----------------------------------------------------------------------===//
250 /// DISerializeVisitor - This DIVisitor serializes all the fields in
251 /// the supplied DebugInfoDesc.
252 class DISerializeVisitor : public DIVisitor {
254 DISerializer &SR; // Active serializer.
255 std::vector<Constant*> &Elements; // Element accumulator.
258 DISerializeVisitor(DISerializer &S, std::vector<Constant*> &E)
264 /// Apply - Set the value of each of the fields.
266 virtual void Apply(int &Field) {
267 Elements.push_back(ConstantInt::get(Type::Int32Ty, int32_t(Field)));
269 virtual void Apply(unsigned &Field) {
270 Elements.push_back(ConstantInt::get(Type::Int32Ty, uint32_t(Field)));
272 virtual void Apply(int64_t &Field) {
273 Elements.push_back(ConstantInt::get(Type::Int64Ty, int64_t(Field)));
275 virtual void Apply(uint64_t &Field) {
276 Elements.push_back(ConstantInt::get(Type::Int64Ty, uint64_t(Field)));
278 virtual void Apply(bool &Field) {
279 Elements.push_back(ConstantInt::get(Type::Int1Ty, Field));
281 virtual void Apply(std::string &Field) {
282 Elements.push_back(SR.getString(Field));
284 virtual void Apply(DebugInfoDesc *&Field) {
285 GlobalVariable *GV = NULL;
287 // If non-NULL then convert to global.
288 if (Field) GV = SR.Serialize(Field);
290 // FIXME - At some point should use specific type.
291 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
294 // Set to pointer to global.
295 Elements.push_back(ConstantExpr::getBitCast(GV, EmptyTy));
298 Elements.push_back(ConstantPointerNull::get(EmptyTy));
301 virtual void Apply(GlobalVariable *&Field) {
302 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
304 Elements.push_back(ConstantExpr::getBitCast(Field, EmptyTy));
306 Elements.push_back(ConstantPointerNull::get(EmptyTy));
309 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
310 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
311 unsigned N = Field.size();
312 ArrayType *AT = ArrayType::get(EmptyTy, N);
313 std::vector<Constant *> ArrayElements;
315 for (unsigned i = 0, N = Field.size(); i < N; ++i) {
316 if (DebugInfoDesc *Element = Field[i]) {
317 GlobalVariable *GVE = SR.Serialize(Element);
318 Constant *CE = ConstantExpr::getBitCast(GVE, EmptyTy);
319 ArrayElements.push_back(cast<Constant>(CE));
321 ArrayElements.push_back(ConstantPointerNull::get(EmptyTy));
325 Constant *CA = ConstantArray::get(AT, ArrayElements);
326 GlobalVariable *CAGV = new GlobalVariable(AT, true,
327 GlobalValue::InternalLinkage,
328 CA, "llvm.dbg.array",
330 CAGV->setSection("llvm.metadata");
331 Constant *CAE = ConstantExpr::getBitCast(CAGV, EmptyTy);
332 Elements.push_back(CAE);
336 //===----------------------------------------------------------------------===//
337 /// DIGetTypesVisitor - This DIVisitor gathers all the field types in
338 /// the supplied DebugInfoDesc.
339 class DIGetTypesVisitor : public DIVisitor {
341 DISerializer &SR; // Active serializer.
342 std::vector<const Type*> &Fields; // Type accumulator.
345 DIGetTypesVisitor(DISerializer &S, std::vector<const Type*> &F)
351 /// Apply - Set the value of each of the fields.
353 virtual void Apply(int &Field) {
354 Fields.push_back(Type::Int32Ty);
356 virtual void Apply(unsigned &Field) {
357 Fields.push_back(Type::Int32Ty);
359 virtual void Apply(int64_t &Field) {
360 Fields.push_back(Type::Int64Ty);
362 virtual void Apply(uint64_t &Field) {
363 Fields.push_back(Type::Int64Ty);
365 virtual void Apply(bool &Field) {
366 Fields.push_back(Type::Int1Ty);
368 virtual void Apply(std::string &Field) {
369 Fields.push_back(SR.getStrPtrType());
371 virtual void Apply(DebugInfoDesc *&Field) {
372 // FIXME - At some point should use specific type.
373 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
374 Fields.push_back(EmptyTy);
376 virtual void Apply(GlobalVariable *&Field) {
377 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
378 Fields.push_back(EmptyTy);
380 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
381 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
382 Fields.push_back(EmptyTy);
386 //===----------------------------------------------------------------------===//
387 /// DIVerifyVisitor - This DIVisitor verifies all the field types against
388 /// a constant initializer.
389 class DIVerifyVisitor : public DIVisitor {
391 DIVerifier &VR; // Active verifier.
392 bool IsValid; // Validity status.
393 unsigned I; // Current operand index.
394 ConstantStruct *CI; // GlobalVariable constant initializer.
397 DIVerifyVisitor(DIVerifier &V, GlobalVariable *GV)
402 , CI(cast<ConstantStruct>(GV->getInitializer()))
407 bool isValid() const { return IsValid; }
409 /// Apply - Set the value of each of the fields.
411 virtual void Apply(int &Field) {
412 Constant *C = CI->getOperand(I++);
413 IsValid = IsValid && isa<ConstantInt>(C);
415 virtual void Apply(unsigned &Field) {
416 Constant *C = CI->getOperand(I++);
417 IsValid = IsValid && isa<ConstantInt>(C);
419 virtual void Apply(int64_t &Field) {
420 Constant *C = CI->getOperand(I++);
421 IsValid = IsValid && isa<ConstantInt>(C);
423 virtual void Apply(uint64_t &Field) {
424 Constant *C = CI->getOperand(I++);
425 IsValid = IsValid && isa<ConstantInt>(C);
427 virtual void Apply(bool &Field) {
428 Constant *C = CI->getOperand(I++);
429 IsValid = IsValid && isa<ConstantInt>(C) && C->getType() == Type::Int1Ty;
431 virtual void Apply(std::string &Field) {
432 Constant *C = CI->getOperand(I++);
434 (!C || isStringValue(C) || C->isNullValue());
436 virtual void Apply(DebugInfoDesc *&Field) {
437 // FIXME - Prepare the correct descriptor.
438 Constant *C = CI->getOperand(I++);
439 IsValid = IsValid && isGlobalVariable(C);
441 virtual void Apply(GlobalVariable *&Field) {
442 Constant *C = CI->getOperand(I++);
443 IsValid = IsValid && isGlobalVariable(C);
445 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
446 Constant *C = CI->getOperand(I++);
447 IsValid = IsValid && isGlobalVariable(C);
448 if (!IsValid) return;
450 GlobalVariable *GV = getGlobalVariable(C);
451 IsValid = IsValid && GV && GV->hasInitializer();
452 if (!IsValid) return;
454 ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer());
455 IsValid = IsValid && CA;
456 if (!IsValid) return;
458 for (unsigned i = 0, N = CA->getNumOperands(); IsValid && i < N; ++i) {
459 IsValid = IsValid && isGlobalVariable(CA->getOperand(i));
460 if (!IsValid) return;
462 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
469 //===----------------------------------------------------------------------===//
471 /// TagFromGlobal - Returns the tag number from a debug info descriptor
472 /// GlobalVariable. Return DIIValid if operand is not an unsigned int.
473 unsigned DebugInfoDesc::TagFromGlobal(GlobalVariable *GV) {
474 ConstantInt *C = getUIntOperand(GV, 0);
475 return C ? ((unsigned)C->getZExtValue() & ~LLVMDebugVersionMask) :
476 (unsigned)DW_TAG_invalid;
479 /// VersionFromGlobal - Returns the version number from a debug info
480 /// descriptor GlobalVariable. Return DIIValid if operand is not an unsigned
482 unsigned DebugInfoDesc::VersionFromGlobal(GlobalVariable *GV) {
483 ConstantInt *C = getUIntOperand(GV, 0);
484 return C ? ((unsigned)C->getZExtValue() & LLVMDebugVersionMask) :
485 (unsigned)DW_TAG_invalid;
488 /// DescFactory - Create an instance of debug info descriptor based on Tag.
489 /// Return NULL if not a recognized Tag.
490 DebugInfoDesc *DebugInfoDesc::DescFactory(unsigned Tag) {
492 case DW_TAG_anchor: return new AnchorDesc();
493 case DW_TAG_compile_unit: return new CompileUnitDesc();
494 case DW_TAG_variable: return new GlobalVariableDesc();
495 case DW_TAG_subprogram: return new SubprogramDesc();
496 case DW_TAG_lexical_block: return new BlockDesc();
497 case DW_TAG_base_type: return new BasicTypeDesc();
499 case DW_TAG_pointer_type:
500 case DW_TAG_reference_type:
501 case DW_TAG_const_type:
502 case DW_TAG_volatile_type:
503 case DW_TAG_restrict_type:
505 case DW_TAG_inheritance: return new DerivedTypeDesc(Tag);
506 case DW_TAG_array_type:
507 case DW_TAG_structure_type:
508 case DW_TAG_union_type:
509 case DW_TAG_enumeration_type:
510 case DW_TAG_vector_type:
511 case DW_TAG_subroutine_type: return new CompositeTypeDesc(Tag);
512 case DW_TAG_subrange_type: return new SubrangeDesc();
513 case DW_TAG_enumerator: return new EnumeratorDesc();
514 case DW_TAG_return_variable:
515 case DW_TAG_arg_variable:
516 case DW_TAG_auto_variable: return new VariableDesc(Tag);
522 /// getLinkage - get linkage appropriate for this type of descriptor.
524 GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const {
525 return GlobalValue::InternalLinkage;
528 /// ApplyToFields - Target the vistor to the fields of the descriptor.
530 void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) {
534 //===----------------------------------------------------------------------===//
536 AnchorDesc::AnchorDesc()
537 : DebugInfoDesc(DW_TAG_anchor)
540 AnchorDesc::AnchorDesc(AnchoredDesc *D)
541 : DebugInfoDesc(DW_TAG_anchor)
542 , AnchorTag(D->getTag())
545 // Implement isa/cast/dyncast.
546 bool AnchorDesc::classof(const DebugInfoDesc *D) {
547 return D->getTag() == DW_TAG_anchor;
550 /// getLinkage - get linkage appropriate for this type of descriptor.
552 GlobalValue::LinkageTypes AnchorDesc::getLinkage() const {
553 return GlobalValue::LinkOnceLinkage;
556 /// ApplyToFields - Target the visitor to the fields of the TransUnitDesc.
558 void AnchorDesc::ApplyToFields(DIVisitor *Visitor) {
559 DebugInfoDesc::ApplyToFields(Visitor);
561 Visitor->Apply(AnchorTag);
564 /// getDescString - Return a string used to compose global names and labels. A
565 /// A global variable name needs to be defined for each debug descriptor that is
566 /// anchored. NOTE: that each global variable named here also needs to be added
567 /// to the list of names left external in the internalizer.
568 /// ExternalNames.insert("llvm.dbg.compile_units");
569 /// ExternalNames.insert("llvm.dbg.global_variables");
570 /// ExternalNames.insert("llvm.dbg.subprograms");
571 const char *AnchorDesc::getDescString() const {
573 case DW_TAG_compile_unit: return CompileUnitDesc::AnchorString;
574 case DW_TAG_variable: return GlobalVariableDesc::AnchorString;
575 case DW_TAG_subprogram: return SubprogramDesc::AnchorString;
579 assert(0 && "Tag does not have a case for anchor string");
583 /// getTypeString - Return a string used to label this descriptors type.
585 const char *AnchorDesc::getTypeString() const {
586 return "llvm.dbg.anchor.type";
590 void AnchorDesc::dump() {
591 cerr << getDescString() << " "
592 << "Version(" << getVersion() << "), "
593 << "Tag(" << getTag() << "), "
594 << "AnchorTag(" << AnchorTag << ")\n";
598 //===----------------------------------------------------------------------===//
600 AnchoredDesc::AnchoredDesc(unsigned T)
605 /// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
607 void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) {
608 DebugInfoDesc::ApplyToFields(Visitor);
610 Visitor->Apply(Anchor);
613 //===----------------------------------------------------------------------===//
615 CompileUnitDesc::CompileUnitDesc()
616 : AnchoredDesc(DW_TAG_compile_unit)
623 // Implement isa/cast/dyncast.
624 bool CompileUnitDesc::classof(const DebugInfoDesc *D) {
625 return D->getTag() == DW_TAG_compile_unit;
628 /// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
630 void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) {
631 AnchoredDesc::ApplyToFields(Visitor);
633 // Handle cases out of sync with compiler.
634 if (getVersion() == 0) {
635 unsigned DebugVersion;
636 Visitor->Apply(DebugVersion);
639 Visitor->Apply(Language);
640 Visitor->Apply(FileName);
641 Visitor->Apply(Directory);
642 Visitor->Apply(Producer);
645 /// getDescString - Return a string used to compose global names and labels.
647 const char *CompileUnitDesc::getDescString() const {
648 return "llvm.dbg.compile_unit";
651 /// getTypeString - Return a string used to label this descriptors type.
653 const char *CompileUnitDesc::getTypeString() const {
654 return "llvm.dbg.compile_unit.type";
657 /// getAnchorString - Return a string used to label this descriptor's anchor.
659 const char *CompileUnitDesc::AnchorString = "llvm.dbg.compile_units";
660 const char *CompileUnitDesc::getAnchorString() const {
665 void CompileUnitDesc::dump() {
666 cerr << getDescString() << " "
667 << "Version(" << getVersion() << "), "
668 << "Tag(" << getTag() << "), "
669 << "Anchor(" << getAnchor() << "), "
670 << "Language(" << Language << "), "
671 << "FileName(\"" << FileName << "\"), "
672 << "Directory(\"" << Directory << "\"), "
673 << "Producer(\"" << Producer << "\")\n";
677 //===----------------------------------------------------------------------===//
679 TypeDesc::TypeDesc(unsigned T)
691 /// ApplyToFields - Target the visitor to the fields of the TypeDesc.
693 void TypeDesc::ApplyToFields(DIVisitor *Visitor) {
694 DebugInfoDesc::ApplyToFields(Visitor);
696 Visitor->Apply(Context);
697 Visitor->Apply(Name);
698 Visitor->Apply(File);
699 Visitor->Apply(Line);
700 Visitor->Apply(Size);
701 Visitor->Apply(Align);
702 Visitor->Apply(Offset);
703 if (getVersion() > LLVMDebugVersion4) Visitor->Apply(Flags);
706 /// getDescString - Return a string used to compose global names and labels.
708 const char *TypeDesc::getDescString() const {
709 return "llvm.dbg.type";
712 /// getTypeString - Return a string used to label this descriptor's type.
714 const char *TypeDesc::getTypeString() const {
715 return "llvm.dbg.type.type";
719 void TypeDesc::dump() {
720 cerr << getDescString() << " "
721 << "Version(" << getVersion() << "), "
722 << "Tag(" << getTag() << "), "
723 << "Context(" << Context << "), "
724 << "Name(\"" << Name << "\"), "
725 << "File(" << File << "), "
726 << "Line(" << Line << "), "
727 << "Size(" << Size << "), "
728 << "Align(" << Align << "), "
729 << "Offset(" << Offset << "), "
730 << "Flags(" << Flags << ")\n";
734 //===----------------------------------------------------------------------===//
736 BasicTypeDesc::BasicTypeDesc()
737 : TypeDesc(DW_TAG_base_type)
741 // Implement isa/cast/dyncast.
742 bool BasicTypeDesc::classof(const DebugInfoDesc *D) {
743 return D->getTag() == DW_TAG_base_type;
746 /// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
748 void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
749 TypeDesc::ApplyToFields(Visitor);
751 Visitor->Apply(Encoding);
754 /// getDescString - Return a string used to compose global names and labels.
756 const char *BasicTypeDesc::getDescString() const {
757 return "llvm.dbg.basictype";
760 /// getTypeString - Return a string used to label this descriptor's type.
762 const char *BasicTypeDesc::getTypeString() const {
763 return "llvm.dbg.basictype.type";
767 void BasicTypeDesc::dump() {
768 cerr << getDescString() << " "
769 << "Version(" << getVersion() << "), "
770 << "Tag(" << getTag() << "), "
771 << "Context(" << getContext() << "), "
772 << "Name(\"" << getName() << "\"), "
773 << "Size(" << getSize() << "), "
774 << "Encoding(" << Encoding << ")\n";
778 //===----------------------------------------------------------------------===//
780 DerivedTypeDesc::DerivedTypeDesc(unsigned T)
785 // Implement isa/cast/dyncast.
786 bool DerivedTypeDesc::classof(const DebugInfoDesc *D) {
787 unsigned T = D->getTag();
790 case DW_TAG_pointer_type:
791 case DW_TAG_reference_type:
792 case DW_TAG_const_type:
793 case DW_TAG_volatile_type:
794 case DW_TAG_restrict_type:
796 case DW_TAG_inheritance:
803 /// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
805 void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) {
806 TypeDesc::ApplyToFields(Visitor);
808 Visitor->Apply(FromType);
811 /// getDescString - Return a string used to compose global names and labels.
813 const char *DerivedTypeDesc::getDescString() const {
814 return "llvm.dbg.derivedtype";
817 /// getTypeString - Return a string used to label this descriptor's type.
819 const char *DerivedTypeDesc::getTypeString() const {
820 return "llvm.dbg.derivedtype.type";
824 void DerivedTypeDesc::dump() {
825 cerr << getDescString() << " "
826 << "Version(" << getVersion() << "), "
827 << "Tag(" << getTag() << "), "
828 << "Context(" << getContext() << "), "
829 << "Name(\"" << getName() << "\"), "
830 << "Size(" << getSize() << "), "
831 << "File(" << getFile() << "), "
832 << "Line(" << getLine() << "), "
833 << "FromType(" << FromType << ")\n";
837 //===----------------------------------------------------------------------===//
839 CompositeTypeDesc::CompositeTypeDesc(unsigned T)
844 // Implement isa/cast/dyncast.
845 bool CompositeTypeDesc::classof(const DebugInfoDesc *D) {
846 unsigned T = D->getTag();
848 case DW_TAG_array_type:
849 case DW_TAG_structure_type:
850 case DW_TAG_union_type:
851 case DW_TAG_enumeration_type:
852 case DW_TAG_vector_type:
853 case DW_TAG_subroutine_type:
860 /// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
862 void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) {
863 DerivedTypeDesc::ApplyToFields(Visitor);
865 Visitor->Apply(Elements);
868 /// getDescString - Return a string used to compose global names and labels.
870 const char *CompositeTypeDesc::getDescString() const {
871 return "llvm.dbg.compositetype";
874 /// getTypeString - Return a string used to label this descriptor's type.
876 const char *CompositeTypeDesc::getTypeString() const {
877 return "llvm.dbg.compositetype.type";
881 void CompositeTypeDesc::dump() {
882 cerr << getDescString() << " "
883 << "Version(" << getVersion() << "), "
884 << "Tag(" << getTag() << "), "
885 << "Context(" << getContext() << "), "
886 << "Name(\"" << getName() << "\"), "
887 << "Size(" << getSize() << "), "
888 << "File(" << getFile() << "), "
889 << "Line(" << getLine() << "), "
890 << "FromType(" << getFromType() << "), "
891 << "Elements.size(" << Elements.size() << ")\n";
895 //===----------------------------------------------------------------------===//
897 SubrangeDesc::SubrangeDesc()
898 : DebugInfoDesc(DW_TAG_subrange_type)
903 // Implement isa/cast/dyncast.
904 bool SubrangeDesc::classof(const DebugInfoDesc *D) {
905 return D->getTag() == DW_TAG_subrange_type;
908 /// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
910 void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) {
911 DebugInfoDesc::ApplyToFields(Visitor);
917 /// getDescString - Return a string used to compose global names and labels.
919 const char *SubrangeDesc::getDescString() const {
920 return "llvm.dbg.subrange";
923 /// getTypeString - Return a string used to label this descriptor's type.
925 const char *SubrangeDesc::getTypeString() const {
926 return "llvm.dbg.subrange.type";
930 void SubrangeDesc::dump() {
931 cerr << getDescString() << " "
932 << "Version(" << getVersion() << "), "
933 << "Tag(" << getTag() << "), "
934 << "Lo(" << Lo << "), "
935 << "Hi(" << Hi << ")\n";
939 //===----------------------------------------------------------------------===//
941 EnumeratorDesc::EnumeratorDesc()
942 : DebugInfoDesc(DW_TAG_enumerator)
947 // Implement isa/cast/dyncast.
948 bool EnumeratorDesc::classof(const DebugInfoDesc *D) {
949 return D->getTag() == DW_TAG_enumerator;
952 /// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
954 void EnumeratorDesc::ApplyToFields(DIVisitor *Visitor) {
955 DebugInfoDesc::ApplyToFields(Visitor);
957 Visitor->Apply(Name);
958 Visitor->Apply(Value);
961 /// getDescString - Return a string used to compose global names and labels.
963 const char *EnumeratorDesc::getDescString() const {
964 return "llvm.dbg.enumerator";
967 /// getTypeString - Return a string used to label this descriptor's type.
969 const char *EnumeratorDesc::getTypeString() const {
970 return "llvm.dbg.enumerator.type";
974 void EnumeratorDesc::dump() {
975 cerr << getDescString() << " "
976 << "Version(" << getVersion() << "), "
977 << "Tag(" << getTag() << "), "
978 << "Name(" << Name << "), "
979 << "Value(" << Value << ")\n";
983 //===----------------------------------------------------------------------===//
985 VariableDesc::VariableDesc(unsigned T)
994 // Implement isa/cast/dyncast.
995 bool VariableDesc::classof(const DebugInfoDesc *D) {
996 unsigned T = D->getTag();
998 case DW_TAG_auto_variable:
999 case DW_TAG_arg_variable:
1000 case DW_TAG_return_variable:
1007 /// ApplyToFields - Target the visitor to the fields of the VariableDesc.
1009 void VariableDesc::ApplyToFields(DIVisitor *Visitor) {
1010 DebugInfoDesc::ApplyToFields(Visitor);
1012 Visitor->Apply(Context);
1013 Visitor->Apply(Name);
1014 Visitor->Apply(File);
1015 Visitor->Apply(Line);
1016 Visitor->Apply(TyDesc);
1019 /// getDescString - Return a string used to compose global names and labels.
1021 const char *VariableDesc::getDescString() const {
1022 return "llvm.dbg.variable";
1025 /// getTypeString - Return a string used to label this descriptor's type.
1027 const char *VariableDesc::getTypeString() const {
1028 return "llvm.dbg.variable.type";
1032 void VariableDesc::dump() {
1033 cerr << getDescString() << " "
1034 << "Version(" << getVersion() << "), "
1035 << "Tag(" << getTag() << "), "
1036 << "Context(" << Context << "), "
1037 << "Name(\"" << Name << "\"), "
1038 << "File(" << File << "), "
1039 << "Line(" << Line << "), "
1040 << "TyDesc(" << TyDesc << ")\n";
1044 //===----------------------------------------------------------------------===//
1046 GlobalDesc::GlobalDesc(unsigned T)
1056 , IsDefinition(false)
1059 /// ApplyToFields - Target the visitor to the fields of the global.
1061 void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
1062 AnchoredDesc::ApplyToFields(Visitor);
1064 Visitor->Apply(Context);
1065 Visitor->Apply(Name);
1066 Visitor->Apply(FullName);
1067 Visitor->Apply(LinkageName);
1068 Visitor->Apply(File);
1069 Visitor->Apply(Line);
1070 Visitor->Apply(TyDesc);
1071 Visitor->Apply(IsStatic);
1072 Visitor->Apply(IsDefinition);
1075 //===----------------------------------------------------------------------===//
1077 GlobalVariableDesc::GlobalVariableDesc()
1078 : GlobalDesc(DW_TAG_variable)
1082 // Implement isa/cast/dyncast.
1083 bool GlobalVariableDesc::classof(const DebugInfoDesc *D) {
1084 return D->getTag() == DW_TAG_variable;
1087 /// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
1089 void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
1090 GlobalDesc::ApplyToFields(Visitor);
1092 Visitor->Apply(Global);
1095 /// getDescString - Return a string used to compose global names and labels.
1097 const char *GlobalVariableDesc::getDescString() const {
1098 return "llvm.dbg.global_variable";
1101 /// getTypeString - Return a string used to label this descriptors type.
1103 const char *GlobalVariableDesc::getTypeString() const {
1104 return "llvm.dbg.global_variable.type";
1107 /// getAnchorString - Return a string used to label this descriptor's anchor.
1109 const char *GlobalVariableDesc::AnchorString = "llvm.dbg.global_variables";
1110 const char *GlobalVariableDesc::getAnchorString() const {
1111 return AnchorString;
1115 void GlobalVariableDesc::dump() {
1116 cerr << getDescString() << " "
1117 << "Version(" << getVersion() << "), "
1118 << "Tag(" << getTag() << "), "
1119 << "Anchor(" << getAnchor() << "), "
1120 << "Name(\"" << getName() << "\"), "
1121 << "FullName(\"" << getFullName() << "\"), "
1122 << "LinkageName(\"" << getLinkageName() << "\"), "
1123 << "File(" << getFile() << "),"
1124 << "Line(" << getLine() << "),"
1125 << "Type(" << getType() << "), "
1126 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1127 << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
1128 << "Global(" << Global << ")\n";
1132 //===----------------------------------------------------------------------===//
1134 SubprogramDesc::SubprogramDesc()
1135 : GlobalDesc(DW_TAG_subprogram)
1138 // Implement isa/cast/dyncast.
1139 bool SubprogramDesc::classof(const DebugInfoDesc *D) {
1140 return D->getTag() == DW_TAG_subprogram;
1143 /// ApplyToFields - Target the visitor to the fields of the
1145 void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
1146 GlobalDesc::ApplyToFields(Visitor);
1149 /// getDescString - Return a string used to compose global names and labels.
1151 const char *SubprogramDesc::getDescString() const {
1152 return "llvm.dbg.subprogram";
1155 /// getTypeString - Return a string used to label this descriptors type.
1157 const char *SubprogramDesc::getTypeString() const {
1158 return "llvm.dbg.subprogram.type";
1161 /// getAnchorString - Return a string used to label this descriptor's anchor.
1163 const char *SubprogramDesc::AnchorString = "llvm.dbg.subprograms";
1164 const char *SubprogramDesc::getAnchorString() const {
1165 return AnchorString;
1169 void SubprogramDesc::dump() {
1170 cerr << getDescString() << " "
1171 << "Version(" << getVersion() << "), "
1172 << "Tag(" << getTag() << "), "
1173 << "Anchor(" << getAnchor() << "), "
1174 << "Name(\"" << getName() << "\"), "
1175 << "FullName(\"" << getFullName() << "\"), "
1176 << "LinkageName(\"" << getLinkageName() << "\"), "
1177 << "File(" << getFile() << "),"
1178 << "Line(" << getLine() << "),"
1179 << "Type(" << getType() << "), "
1180 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1181 << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
1185 //===----------------------------------------------------------------------===//
1187 BlockDesc::BlockDesc()
1188 : DebugInfoDesc(DW_TAG_lexical_block)
1192 // Implement isa/cast/dyncast.
1193 bool BlockDesc::classof(const DebugInfoDesc *D) {
1194 return D->getTag() == DW_TAG_lexical_block;
1197 /// ApplyToFields - Target the visitor to the fields of the BlockDesc.
1199 void BlockDesc::ApplyToFields(DIVisitor *Visitor) {
1200 DebugInfoDesc::ApplyToFields(Visitor);
1202 Visitor->Apply(Context);
1205 /// getDescString - Return a string used to compose global names and labels.
1207 const char *BlockDesc::getDescString() const {
1208 return "llvm.dbg.block";
1211 /// getTypeString - Return a string used to label this descriptors type.
1213 const char *BlockDesc::getTypeString() const {
1214 return "llvm.dbg.block.type";
1218 void BlockDesc::dump() {
1219 cerr << getDescString() << " "
1220 << "Version(" << getVersion() << "), "
1221 << "Tag(" << getTag() << "),"
1222 << "Context(" << Context << ")\n";
1226 //===----------------------------------------------------------------------===//
1228 DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
1229 return Deserialize(getGlobalVariable(V));
1231 DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
1233 if (!GV) return NULL;
1235 // Check to see if it has been already deserialized.
1236 DebugInfoDesc *&Slot = GlobalDescs[GV];
1237 if (Slot) return Slot;
1239 // Get the Tag from the global.
1240 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1242 // Create an empty instance of the correct sort.
1243 Slot = DebugInfoDesc::DescFactory(Tag);
1245 // If not a user defined descriptor.
1247 // Deserialize the fields.
1248 DIDeserializeVisitor DRAM(*this, GV);
1249 DRAM.ApplyToFields(Slot);
1255 //===----------------------------------------------------------------------===//
1257 /// getStrPtrType - Return a "sbyte *" type.
1259 const PointerType *DISerializer::getStrPtrType() {
1260 // If not already defined.
1262 // Construct the pointer to signed bytes.
1263 StrPtrTy = PointerType::get(Type::Int8Ty);
1269 /// getEmptyStructPtrType - Return a "{ }*" type.
1271 const PointerType *DISerializer::getEmptyStructPtrType() {
1272 // If not already defined.
1273 if (!EmptyStructPtrTy) {
1274 // Construct the empty structure type.
1275 const StructType *EmptyStructTy =
1276 StructType::get(std::vector<const Type*>());
1277 // Construct the pointer to empty structure type.
1278 EmptyStructPtrTy = PointerType::get(EmptyStructTy);
1281 return EmptyStructPtrTy;
1284 /// getTagType - Return the type describing the specified descriptor (via tag.)
1286 const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
1287 // Attempt to get the previously defined type.
1288 StructType *&Ty = TagTypes[DD->getTag()];
1290 // If not already defined.
1292 // Set up fields vector.
1293 std::vector<const Type*> Fields;
1294 // Get types of fields.
1295 DIGetTypesVisitor GTAM(*this, Fields);
1296 GTAM.ApplyToFields(DD);
1298 // Construct structured type.
1299 Ty = StructType::get(Fields);
1301 // Register type name with module.
1302 M->addTypeName(DD->getTypeString(), Ty);
1308 /// getString - Construct the string as constant string global.
1310 Constant *DISerializer::getString(const std::string &String) {
1311 // Check string cache for previous edition.
1312 Constant *&Slot = StringCache[String];
1313 // Return Constant if previously defined.
1314 if (Slot) return Slot;
1315 // If empty string then use a sbyte* null instead.
1316 if (String.empty()) {
1317 Slot = ConstantPointerNull::get(getStrPtrType());
1319 // Construct string as an llvm constant.
1320 Constant *ConstStr = ConstantArray::get(String);
1321 // Otherwise create and return a new string global.
1322 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
1323 GlobalVariable::InternalLinkage,
1324 ConstStr, "str", M);
1325 StrGV->setSection("llvm.metadata");
1326 // Convert to generic string pointer.
1327 Slot = ConstantExpr::getBitCast(StrGV, getStrPtrType());
1333 /// Serialize - Recursively cast the specified descriptor into a GlobalVariable
1334 /// so that it can be serialized to a .bc or .ll file.
1335 GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
1336 // Check if the DebugInfoDesc is already in the map.
1337 GlobalVariable *&Slot = DescGlobals[DD];
1339 // See if DebugInfoDesc exists, if so return prior GlobalVariable.
1340 if (Slot) return Slot;
1342 // Get the type associated with the Tag.
1343 const StructType *Ty = getTagType(DD);
1345 // Create the GlobalVariable early to prevent infinite recursion.
1346 GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(),
1347 NULL, DD->getDescString(), M);
1348 GV->setSection("llvm.metadata");
1350 // Insert new GlobalVariable in DescGlobals map.
1353 // Set up elements vector
1354 std::vector<Constant*> Elements;
1356 DISerializeVisitor SRAM(*this, Elements);
1357 SRAM.ApplyToFields(DD);
1359 // Set the globals initializer.
1360 GV->setInitializer(ConstantStruct::get(Ty, Elements));
1365 //===----------------------------------------------------------------------===//
1367 /// Verify - Return true if the GlobalVariable appears to be a valid
1368 /// serialization of a DebugInfoDesc.
1369 bool DIVerifier::Verify(Value *V) {
1370 return !V || Verify(getGlobalVariable(V));
1372 bool DIVerifier::Verify(GlobalVariable *GV) {
1374 if (!GV) return true;
1376 // Check prior validity.
1377 unsigned &ValiditySlot = Validity[GV];
1379 // If visited before then use old state.
1380 if (ValiditySlot) return ValiditySlot == Valid;
1382 // Assume validity for the time being (recursion.)
1383 ValiditySlot = Valid;
1385 // Make sure the global is internal or link once (anchor.)
1386 if (GV->getLinkage() != GlobalValue::InternalLinkage &&
1387 GV->getLinkage() != GlobalValue::LinkOnceLinkage) {
1388 ValiditySlot = Invalid;
1393 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1395 // Check for user defined descriptors.
1396 if (Tag == DW_TAG_invalid) {
1397 ValiditySlot = Valid;
1402 unsigned Version = DebugInfoDesc::VersionFromGlobal(GV);
1404 // Check for version mismatch.
1405 if (Version != LLVMDebugVersion) {
1406 ValiditySlot = Invalid;
1410 // Construct an empty DebugInfoDesc.
1411 DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
1413 // Allow for user defined descriptors.
1414 if (!DD) return true;
1416 // Get the initializer constant.
1417 ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
1419 // Get the operand count.
1420 unsigned N = CI->getNumOperands();
1422 // Get the field count.
1423 unsigned &CountSlot = Counts[Tag];
1425 // Check the operand count to the field count
1426 DICountVisitor CTAM;
1427 CTAM.ApplyToFields(DD);
1428 CountSlot = CTAM.getCount();
1431 // Field count must be at most equal operand count.
1432 if (CountSlot > N) {
1434 ValiditySlot = Invalid;
1438 // Check each field for valid type.
1439 DIVerifyVisitor VRAM(*this, GV);
1440 VRAM.ApplyToFields(DD);
1442 // Release empty DebugInfoDesc.
1445 // If fields are not valid.
1446 if (!VRAM.isValid()) {
1447 ValiditySlot = Invalid;
1454 //===----------------------------------------------------------------------===//
1456 DebugScope::~DebugScope() {
1457 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1458 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
1461 //===----------------------------------------------------------------------===//
1463 MachineModuleInfo::MachineModuleInfo()
1475 MachineModuleInfo::~MachineModuleInfo() {
1479 /// doInitialization - Initialize the state for a new module.
1481 bool MachineModuleInfo::doInitialization() {
1485 /// doFinalization - Tear down the state after completion of a module.
1487 bool MachineModuleInfo::doFinalization() {
1491 /// BeginFunction - Begin gathering function meta information.
1493 void MachineModuleInfo::BeginFunction(MachineFunction *MF) {
1497 /// EndFunction - Discard function meta information.
1499 void MachineModuleInfo::EndFunction() {
1500 // Clean up scope information.
1507 // Clean up frame info.
1511 /// getDescFor - Convert a Value to a debug information descriptor.
1513 // FIXME - use new Value type when available.
1514 DebugInfoDesc *MachineModuleInfo::getDescFor(Value *V) {
1515 return DR.Deserialize(V);
1518 /// Verify - Verify that a Value is debug information descriptor.
1520 bool MachineModuleInfo::Verify(Value *V) {
1521 return VR.Verify(V);
1524 /// AnalyzeModule - Scan the module for global debug information.
1526 void MachineModuleInfo::AnalyzeModule(Module &M) {
1527 SetupCompileUnits(M);
1530 /// SetupCompileUnits - Set up the unique vector of compile units.
1532 void MachineModuleInfo::SetupCompileUnits(Module &M) {
1533 std::vector<CompileUnitDesc *>CU = getAnchoredDescriptors<CompileUnitDesc>(M);
1535 for (unsigned i = 0, N = CU.size(); i < N; i++) {
1536 CompileUnits.insert(CU[i]);
1540 /// getCompileUnits - Return a vector of debug compile units.
1542 const UniqueVector<CompileUnitDesc *> MachineModuleInfo::getCompileUnits()const{
1543 return CompileUnits;
1546 /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1547 /// named GlobalVariable.
1548 std::vector<GlobalVariable*>
1549 MachineModuleInfo::getGlobalVariablesUsing(Module &M,
1550 const std::string &RootName) {
1551 return ::getGlobalVariablesUsing(M, RootName);
1554 /// RecordLabel - Records location information and associates it with a
1555 /// debug label. Returns a unique label ID used to generate a label and
1556 /// provide correspondence to the source line list.
1557 unsigned MachineModuleInfo::RecordLabel(unsigned Line, unsigned Column,
1559 unsigned ID = NextLabelID();
1560 Lines.push_back(SourceLineInfo(Line, Column, Source, ID));
1564 /// RecordSource - Register a source file with debug info. Returns an source
1566 unsigned MachineModuleInfo::RecordSource(const std::string &Directory,
1567 const std::string &Source) {
1568 unsigned DirectoryID = Directories.insert(Directory);
1569 return SourceFiles.insert(SourceFileInfo(DirectoryID, Source));
1571 unsigned MachineModuleInfo::RecordSource(const CompileUnitDesc *CompileUnit) {
1572 return RecordSource(CompileUnit->getDirectory(),
1573 CompileUnit->getFileName());
1576 /// RecordRegionStart - Indicate the start of a region.
1578 unsigned MachineModuleInfo::RecordRegionStart(Value *V) {
1579 // FIXME - need to be able to handle split scopes because of bb cloning.
1580 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1581 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1582 unsigned ID = NextLabelID();
1583 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
1587 /// RecordRegionEnd - Indicate the end of a region.
1589 unsigned MachineModuleInfo::RecordRegionEnd(Value *V) {
1590 // FIXME - need to be able to handle split scopes because of bb cloning.
1591 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1592 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1593 unsigned ID = NextLabelID();
1594 Scope->setEndLabelID(ID);
1598 /// RecordVariable - Indicate the declaration of a local variable.
1600 void MachineModuleInfo::RecordVariable(Value *V, unsigned FrameIndex) {
1601 VariableDesc *VD = cast<VariableDesc>(DR.Deserialize(V));
1602 DebugScope *Scope = getOrCreateScope(VD->getContext());
1603 DebugVariable *DV = new DebugVariable(VD, FrameIndex);
1604 Scope->AddVariable(DV);
1607 /// getOrCreateScope - Returns the scope associated with the given descriptor.
1609 DebugScope *MachineModuleInfo::getOrCreateScope(DebugInfoDesc *ScopeDesc) {
1610 DebugScope *&Slot = ScopeMap[ScopeDesc];
1612 // FIXME - breaks down when the context is an inlined function.
1613 DebugInfoDesc *ParentDesc = NULL;
1614 if (BlockDesc *Block = dyn_cast<BlockDesc>(ScopeDesc)) {
1615 ParentDesc = Block->getContext();
1617 DebugScope *Parent = ParentDesc ? getOrCreateScope(ParentDesc) : NULL;
1618 Slot = new DebugScope(Parent, ScopeDesc);
1620 Parent->AddScope(Slot);
1621 } else if (RootScope) {
1622 // FIXME - Add inlined function scopes to the root so we can delete
1623 // them later. Long term, handle inlined functions properly.
1624 RootScope->AddScope(Slot);
1626 // First function is top level function.
1633 //===----------------------------------------------------------------------===//
1634 /// DebugLabelFolding pass - This pass prunes out redundant labels. This allows
1635 /// a info consumer to determine if the range of two labels is empty, by seeing
1636 /// if the labels map to the same reduced label.
1640 struct DebugLabelFolder : public MachineFunctionPass {
1641 virtual bool runOnMachineFunction(MachineFunction &MF);
1642 virtual const char *getPassName() const { return "Label Folder"; }
1645 bool DebugLabelFolder::runOnMachineFunction(MachineFunction &MF) {
1646 // Get machine module info.
1647 MachineModuleInfo *MMI = getAnalysisToUpdate<MachineModuleInfo>();
1648 if (!MMI) return false;
1649 // Get target instruction info.
1650 const TargetInstrInfo *TII = MF.getTarget().getInstrInfo();
1651 if (!TII) return false;
1653 // Track if change is made.
1654 bool MadeChange = false;
1655 // No prior label to begin.
1656 unsigned PriorLabel = 0;
1658 // Iterate through basic blocks.
1659 for (MachineFunction::iterator BB = MF.begin(), E = MF.end();
1661 // Iterate through instructions.
1662 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1664 if ((unsigned)I->getOpcode() == TargetInstrInfo::LABEL) {
1665 // The label ID # is always operand #0, an immediate.
1666 unsigned NextLabel = I->getOperand(0).getImm();
1668 // If there was an immediate prior label.
1670 // Remap the current label to prior label.
1671 MMI->RemapLabel(NextLabel, PriorLabel);
1672 // Delete the current label.
1674 // Indicate a change has been made.
1678 // Start a new round.
1679 PriorLabel = NextLabel;
1682 // No consecutive labels.
1693 FunctionPass *createDebugLabelFoldingPass() { return new DebugLabelFolder(); }