1 //===--- DebugInfo.cpp - Debug Information Helper Classes -----------------===//
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 // This file implements the helper classes used to build and interpret debug
11 // information in LLVM IR form.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Analysis/DebugInfo.h"
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/Intrinsics.h"
19 #include "llvm/IntrinsicInst.h"
20 #include "llvm/Instructions.h"
21 #include "llvm/Module.h"
22 #include "llvm/Analysis/ValueTracking.h"
23 #include "llvm/Support/Dwarf.h"
24 #include "llvm/Support/Streams.h"
27 using namespace llvm::dwarf;
29 //===----------------------------------------------------------------------===//
31 //===----------------------------------------------------------------------===//
33 /// ValidDebugInfo - Return true if V represents valid debug info value.
34 bool DIDescriptor::ValidDebugInfo(Value *V, CodeGenOpt::Level OptLevel) {
38 GlobalVariable *GV = dyn_cast<GlobalVariable>(V->stripPointerCasts());
42 if (!GV->hasInternalLinkage () && !GV->hasLinkOnceLinkage())
47 // Check current version. Allow Version6 for now.
48 unsigned Version = DI.getVersion();
49 if (Version != LLVMDebugVersion && Version != LLVMDebugVersion6)
52 unsigned Tag = DI.getTag();
55 assert(DIVariable(GV).Verify() && "Invalid DebugInfo value");
57 case DW_TAG_compile_unit:
58 assert(DICompileUnit(GV).Verify() && "Invalid DebugInfo value");
60 case DW_TAG_subprogram:
61 assert(DISubprogram(GV).Verify() && "Invalid DebugInfo value");
63 case DW_TAG_lexical_block:
64 // FIXME: This interfers with the quality of generated code during
66 if (OptLevel != CodeGenOpt::None)
76 DIDescriptor::DIDescriptor(GlobalVariable *GV, unsigned RequiredTag) {
79 // If this is non-null, check to see if the Tag matches. If not, set to null.
80 if (GV && getTag() != RequiredTag)
85 DIDescriptor::getStringField(unsigned Elt, std::string &Result) const {
91 Constant *C = DbgGV->getInitializer();
92 if (C == 0 || Elt >= C->getNumOperands()) {
97 // Fills in the string if it succeeds
98 if (!GetConstantStringInfo(C->getOperand(Elt), Result))
104 uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
105 if (DbgGV == 0) return 0;
107 Constant *C = DbgGV->getInitializer();
108 if (C == 0 || Elt >= C->getNumOperands())
111 if (ConstantInt *CI = dyn_cast<ConstantInt>(C->getOperand(Elt)))
112 return CI->getZExtValue();
116 DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
117 if (DbgGV == 0) return DIDescriptor();
119 Constant *C = DbgGV->getInitializer();
120 if (C == 0 || Elt >= C->getNumOperands())
121 return DIDescriptor();
123 C = C->getOperand(Elt);
124 return DIDescriptor(dyn_cast<GlobalVariable>(C->stripPointerCasts()));
127 GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
128 if (DbgGV == 0) return 0;
130 Constant *C = DbgGV->getInitializer();
131 if (C == 0 || Elt >= C->getNumOperands())
134 C = C->getOperand(Elt);
135 return dyn_cast<GlobalVariable>(C->stripPointerCasts());
138 //===----------------------------------------------------------------------===//
139 // Simple Descriptor Constructors and other Methods
140 //===----------------------------------------------------------------------===//
142 // Needed by DIVariable::getType().
143 DIType::DIType(GlobalVariable *GV) : DIDescriptor(GV) {
145 unsigned tag = getTag();
146 if (tag != dwarf::DW_TAG_base_type && !DIDerivedType::isDerivedType(tag) &&
147 !DICompositeType::isCompositeType(tag))
151 /// isDerivedType - Return true if the specified tag is legal for
153 bool DIType::isDerivedType(unsigned Tag) {
155 case dwarf::DW_TAG_typedef:
156 case dwarf::DW_TAG_pointer_type:
157 case dwarf::DW_TAG_reference_type:
158 case dwarf::DW_TAG_const_type:
159 case dwarf::DW_TAG_volatile_type:
160 case dwarf::DW_TAG_restrict_type:
161 case dwarf::DW_TAG_member:
162 case dwarf::DW_TAG_inheritance:
165 // FIXME: Even though it doesn't make sense, CompositeTypes are current
166 // modelled as DerivedTypes, this should return true for them as well.
171 /// isCompositeType - Return true if the specified tag is legal for
173 bool DIType::isCompositeType(unsigned TAG) {
175 case dwarf::DW_TAG_array_type:
176 case dwarf::DW_TAG_structure_type:
177 case dwarf::DW_TAG_union_type:
178 case dwarf::DW_TAG_enumeration_type:
179 case dwarf::DW_TAG_vector_type:
180 case dwarf::DW_TAG_subroutine_type:
181 case dwarf::DW_TAG_class_type:
188 /// isVariable - Return true if the specified tag is legal for DIVariable.
189 bool DIVariable::isVariable(unsigned Tag) {
191 case dwarf::DW_TAG_auto_variable:
192 case dwarf::DW_TAG_arg_variable:
193 case dwarf::DW_TAG_return_variable:
200 unsigned DIArray::getNumElements() const {
201 assert (DbgGV && "Invalid DIArray");
202 Constant *C = DbgGV->getInitializer();
203 assert (C && "Invalid DIArray initializer");
204 return C->getNumOperands();
207 /// Verify - Verify that a compile unit is well formed.
208 bool DICompileUnit::Verify() const {
212 if (getFilename(Res).empty())
214 // It is possible that directory and produce string is empty.
218 /// Verify - Verify that a type descriptor is well formed.
219 bool DIType::Verify() const {
222 if (getContext().isNull())
225 DICompileUnit CU = getCompileUnit();
226 if (!CU.isNull() && !CU.Verify())
231 /// Verify - Verify that a composite type descriptor is well formed.
232 bool DICompositeType::Verify() const {
235 if (getContext().isNull())
238 DICompileUnit CU = getCompileUnit();
239 if (!CU.isNull() && !CU.Verify())
244 /// Verify - Verify that a subprogram descriptor is well formed.
245 bool DISubprogram::Verify() const {
249 if (getContext().isNull())
252 DICompileUnit CU = getCompileUnit();
256 DICompositeType Ty = getType();
257 if (!Ty.isNull() && !Ty.Verify())
262 /// Verify - Verify that a global variable descriptor is well formed.
263 bool DIGlobalVariable::Verify() const {
267 if (getContext().isNull())
270 DICompileUnit CU = getCompileUnit();
271 if (!CU.isNull() && !CU.Verify())
274 DIType Ty = getType();
284 /// Verify - Verify that a variable descriptor is well formed.
285 bool DIVariable::Verify() const {
289 if (getContext().isNull())
292 DIType Ty = getType();
299 /// getOriginalTypeSize - If this type is derived from a base type then
300 /// return base type size.
301 uint64_t DIDerivedType::getOriginalTypeSize() const {
302 if (getTag() != dwarf::DW_TAG_member)
303 return getSizeInBits();
304 DIType BT = getTypeDerivedFrom();
305 if (BT.getTag() != dwarf::DW_TAG_base_type)
306 return getSizeInBits();
307 return BT.getSizeInBits();
310 /// describes - Return true if this subprogram provides debugging
311 /// information for the function F.
312 bool DISubprogram::describes(const Function *F) {
313 assert (F && "Invalid function");
315 getLinkageName(Name);
318 if (!Name.empty() && (strcmp(Name.c_str(), F->getNameStart()) == false))
323 //===----------------------------------------------------------------------===//
324 // DIFactory: Basic Helpers
325 //===----------------------------------------------------------------------===//
327 DIFactory::DIFactory(Module &m)
328 : M(m), StopPointFn(0), FuncStartFn(0), RegionStartFn(0), RegionEndFn(0),
330 EmptyStructPtr = PointerType::getUnqual(StructType::get());
333 /// getCastToEmpty - Return this descriptor as a Constant* with type '{}*'.
334 /// This is only valid when the descriptor is non-null.
335 Constant *DIFactory::getCastToEmpty(DIDescriptor D) {
336 if (D.isNull()) return Constant::getNullValue(EmptyStructPtr);
337 return ConstantExpr::getBitCast(D.getGV(), EmptyStructPtr);
340 Constant *DIFactory::GetTagConstant(unsigned TAG) {
341 assert((TAG & LLVMDebugVersionMask) == 0 &&
342 "Tag too large for debug encoding!");
343 return ConstantInt::get(Type::Int32Ty, TAG | LLVMDebugVersion);
346 Constant *DIFactory::GetStringConstant(const std::string &String) {
347 // Check string cache for previous edition.
348 Constant *&Slot = StringCache[String];
350 // Return Constant if previously defined.
351 if (Slot) return Slot;
353 const PointerType *DestTy = PointerType::getUnqual(Type::Int8Ty);
355 // If empty string then use a i8* null instead.
357 return Slot = ConstantPointerNull::get(DestTy);
359 // Construct string as an llvm constant.
360 Constant *ConstStr = ConstantArray::get(String);
362 // Otherwise create and return a new string global.
363 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
364 GlobalVariable::InternalLinkage,
365 ConstStr, ".str", &M);
366 StrGV->setSection("llvm.metadata");
367 return Slot = ConstantExpr::getBitCast(StrGV, DestTy);
370 //===----------------------------------------------------------------------===//
371 // DIFactory: Primary Constructors
372 //===----------------------------------------------------------------------===//
374 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
375 /// This implicitly uniques the arrays created.
376 DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
377 SmallVector<Constant*, 16> Elts;
379 for (unsigned i = 0; i != NumTys; ++i)
380 Elts.push_back(getCastToEmpty(Tys[i]));
382 Constant *Init = ConstantArray::get(ArrayType::get(EmptyStructPtr,
384 Elts.data(), Elts.size());
385 // If we already have this array, just return the uniqued version.
386 DIDescriptor &Entry = SimpleConstantCache[Init];
387 if (!Entry.isNull()) return DIArray(Entry.getGV());
389 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
390 GlobalValue::InternalLinkage,
391 Init, "llvm.dbg.array", &M);
392 GV->setSection("llvm.metadata");
393 Entry = DIDescriptor(GV);
397 /// GetOrCreateSubrange - Create a descriptor for a value range. This
398 /// implicitly uniques the values returned.
399 DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
401 GetTagConstant(dwarf::DW_TAG_subrange_type),
402 ConstantInt::get(Type::Int64Ty, Lo),
403 ConstantInt::get(Type::Int64Ty, Hi)
406 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
408 // If we already have this range, just return the uniqued version.
409 DIDescriptor &Entry = SimpleConstantCache[Init];
410 if (!Entry.isNull()) return DISubrange(Entry.getGV());
412 M.addTypeName("llvm.dbg.subrange.type", Init->getType());
414 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
415 GlobalValue::InternalLinkage,
416 Init, "llvm.dbg.subrange", &M);
417 GV->setSection("llvm.metadata");
418 Entry = DIDescriptor(GV);
419 return DISubrange(GV);
424 /// CreateCompileUnit - Create a new descriptor for the specified compile
425 /// unit. Note that this does not unique compile units within the module.
426 DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
427 const std::string &Filename,
428 const std::string &Directory,
429 const std::string &Producer,
433 unsigned RunTimeVer) {
435 GetTagConstant(dwarf::DW_TAG_compile_unit),
436 Constant::getNullValue(EmptyStructPtr),
437 ConstantInt::get(Type::Int32Ty, LangID),
438 GetStringConstant(Filename),
439 GetStringConstant(Directory),
440 GetStringConstant(Producer),
441 ConstantInt::get(Type::Int1Ty, isMain),
442 ConstantInt::get(Type::Int1Ty, isOptimized),
443 GetStringConstant(Flags),
444 ConstantInt::get(Type::Int32Ty, RunTimeVer)
447 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
449 M.addTypeName("llvm.dbg.compile_unit.type", Init->getType());
450 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
451 GlobalValue::LinkOnceAnyLinkage,
452 Init, "llvm.dbg.compile_unit", &M);
453 GV->setSection("llvm.metadata");
454 return DICompileUnit(GV);
457 /// CreateEnumerator - Create a single enumerator value.
458 DIEnumerator DIFactory::CreateEnumerator(const std::string &Name, uint64_t Val){
460 GetTagConstant(dwarf::DW_TAG_enumerator),
461 GetStringConstant(Name),
462 ConstantInt::get(Type::Int64Ty, Val)
465 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
467 M.addTypeName("llvm.dbg.enumerator.type", Init->getType());
468 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
469 GlobalValue::InternalLinkage,
470 Init, "llvm.dbg.enumerator", &M);
471 GV->setSection("llvm.metadata");
472 return DIEnumerator(GV);
476 /// CreateBasicType - Create a basic type like int, float, etc.
477 DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
478 const std::string &Name,
479 DICompileUnit CompileUnit,
482 uint64_t AlignInBits,
483 uint64_t OffsetInBits, unsigned Flags,
486 GetTagConstant(dwarf::DW_TAG_base_type),
487 getCastToEmpty(Context),
488 GetStringConstant(Name),
489 getCastToEmpty(CompileUnit),
490 ConstantInt::get(Type::Int32Ty, LineNumber),
491 ConstantInt::get(Type::Int64Ty, SizeInBits),
492 ConstantInt::get(Type::Int64Ty, AlignInBits),
493 ConstantInt::get(Type::Int64Ty, OffsetInBits),
494 ConstantInt::get(Type::Int32Ty, Flags),
495 ConstantInt::get(Type::Int32Ty, Encoding)
498 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
500 M.addTypeName("llvm.dbg.basictype.type", Init->getType());
501 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
502 GlobalValue::InternalLinkage,
503 Init, "llvm.dbg.basictype", &M);
504 GV->setSection("llvm.metadata");
505 return DIBasicType(GV);
508 /// CreateDerivedType - Create a derived type like const qualified type,
509 /// pointer, typedef, etc.
510 DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
511 DIDescriptor Context,
512 const std::string &Name,
513 DICompileUnit CompileUnit,
516 uint64_t AlignInBits,
517 uint64_t OffsetInBits,
519 DIType DerivedFrom) {
522 getCastToEmpty(Context),
523 GetStringConstant(Name),
524 getCastToEmpty(CompileUnit),
525 ConstantInt::get(Type::Int32Ty, LineNumber),
526 ConstantInt::get(Type::Int64Ty, SizeInBits),
527 ConstantInt::get(Type::Int64Ty, AlignInBits),
528 ConstantInt::get(Type::Int64Ty, OffsetInBits),
529 ConstantInt::get(Type::Int32Ty, Flags),
530 getCastToEmpty(DerivedFrom)
533 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
535 M.addTypeName("llvm.dbg.derivedtype.type", Init->getType());
536 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
537 GlobalValue::InternalLinkage,
538 Init, "llvm.dbg.derivedtype", &M);
539 GV->setSection("llvm.metadata");
540 return DIDerivedType(GV);
543 /// CreateCompositeType - Create a composite type like array, struct, etc.
544 DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
545 DIDescriptor Context,
546 const std::string &Name,
547 DICompileUnit CompileUnit,
550 uint64_t AlignInBits,
551 uint64_t OffsetInBits,
555 unsigned RuntimeLang) {
559 getCastToEmpty(Context),
560 GetStringConstant(Name),
561 getCastToEmpty(CompileUnit),
562 ConstantInt::get(Type::Int32Ty, LineNumber),
563 ConstantInt::get(Type::Int64Ty, SizeInBits),
564 ConstantInt::get(Type::Int64Ty, AlignInBits),
565 ConstantInt::get(Type::Int64Ty, OffsetInBits),
566 ConstantInt::get(Type::Int32Ty, Flags),
567 getCastToEmpty(DerivedFrom),
568 getCastToEmpty(Elements),
569 ConstantInt::get(Type::Int32Ty, RuntimeLang)
572 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
574 M.addTypeName("llvm.dbg.composite.type", Init->getType());
575 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
576 GlobalValue::InternalLinkage,
577 Init, "llvm.dbg.composite", &M);
578 GV->setSection("llvm.metadata");
579 return DICompositeType(GV);
583 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
584 /// See comments in DISubprogram for descriptions of these fields. This
585 /// method does not unique the generated descriptors.
586 DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
587 const std::string &Name,
588 const std::string &DisplayName,
589 const std::string &LinkageName,
590 DICompileUnit CompileUnit,
591 unsigned LineNo, DIType Type,
596 GetTagConstant(dwarf::DW_TAG_subprogram),
597 Constant::getNullValue(EmptyStructPtr),
598 getCastToEmpty(Context),
599 GetStringConstant(Name),
600 GetStringConstant(DisplayName),
601 GetStringConstant(LinkageName),
602 getCastToEmpty(CompileUnit),
603 ConstantInt::get(Type::Int32Ty, LineNo),
604 getCastToEmpty(Type),
605 ConstantInt::get(Type::Int1Ty, isLocalToUnit),
606 ConstantInt::get(Type::Int1Ty, isDefinition)
609 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
611 M.addTypeName("llvm.dbg.subprogram.type", Init->getType());
612 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
613 GlobalValue::LinkOnceAnyLinkage,
614 Init, "llvm.dbg.subprogram", &M);
615 GV->setSection("llvm.metadata");
616 return DISubprogram(GV);
619 /// CreateGlobalVariable - Create a new descriptor for the specified global.
621 DIFactory::CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
622 const std::string &DisplayName,
623 const std::string &LinkageName,
624 DICompileUnit CompileUnit,
625 unsigned LineNo, DIType Type,bool isLocalToUnit,
626 bool isDefinition, llvm::GlobalVariable *Val) {
628 GetTagConstant(dwarf::DW_TAG_variable),
629 Constant::getNullValue(EmptyStructPtr),
630 getCastToEmpty(Context),
631 GetStringConstant(Name),
632 GetStringConstant(DisplayName),
633 GetStringConstant(LinkageName),
634 getCastToEmpty(CompileUnit),
635 ConstantInt::get(Type::Int32Ty, LineNo),
636 getCastToEmpty(Type),
637 ConstantInt::get(Type::Int1Ty, isLocalToUnit),
638 ConstantInt::get(Type::Int1Ty, isDefinition),
639 ConstantExpr::getBitCast(Val, EmptyStructPtr)
642 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
644 M.addTypeName("llvm.dbg.global_variable.type", Init->getType());
645 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
646 GlobalValue::LinkOnceAnyLinkage,
647 Init, "llvm.dbg.global_variable", &M);
648 GV->setSection("llvm.metadata");
649 return DIGlobalVariable(GV);
653 /// CreateVariable - Create a new descriptor for the specified variable.
654 DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
655 const std::string &Name,
656 DICompileUnit CompileUnit, unsigned LineNo,
660 getCastToEmpty(Context),
661 GetStringConstant(Name),
662 getCastToEmpty(CompileUnit),
663 ConstantInt::get(Type::Int32Ty, LineNo),
667 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
669 M.addTypeName("llvm.dbg.variable.type", Init->getType());
670 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
671 GlobalValue::InternalLinkage,
672 Init, "llvm.dbg.variable", &M);
673 GV->setSection("llvm.metadata");
674 return DIVariable(GV);
678 /// CreateBlock - This creates a descriptor for a lexical block with the
679 /// specified parent context.
680 DIBlock DIFactory::CreateBlock(DIDescriptor Context) {
682 GetTagConstant(dwarf::DW_TAG_lexical_block),
683 getCastToEmpty(Context)
686 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
688 M.addTypeName("llvm.dbg.block.type", Init->getType());
689 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
690 GlobalValue::InternalLinkage,
691 Init, "llvm.dbg.block", &M);
692 GV->setSection("llvm.metadata");
697 //===----------------------------------------------------------------------===//
698 // DIFactory: Routines for inserting code into a function
699 //===----------------------------------------------------------------------===//
701 /// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
702 /// inserting it at the end of the specified basic block.
703 void DIFactory::InsertStopPoint(DICompileUnit CU, unsigned LineNo,
704 unsigned ColNo, BasicBlock *BB) {
706 // Lazily construct llvm.dbg.stoppoint function.
708 StopPointFn = llvm::Intrinsic::getDeclaration(&M,
709 llvm::Intrinsic::dbg_stoppoint);
711 // Invoke llvm.dbg.stoppoint
713 llvm::ConstantInt::get(llvm::Type::Int32Ty, LineNo),
714 llvm::ConstantInt::get(llvm::Type::Int32Ty, ColNo),
717 CallInst::Create(StopPointFn, Args, Args+3, "", BB);
720 /// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
721 /// mark the start of the specified subprogram.
722 void DIFactory::InsertSubprogramStart(DISubprogram SP, BasicBlock *BB) {
723 // Lazily construct llvm.dbg.func.start.
725 FuncStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_func_start);
727 // Call llvm.dbg.func.start which also implicitly sets a stoppoint.
728 CallInst::Create(FuncStartFn, getCastToEmpty(SP), "", BB);
731 /// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
732 /// mark the start of a region for the specified scoping descriptor.
733 void DIFactory::InsertRegionStart(DIDescriptor D, BasicBlock *BB) {
734 // Lazily construct llvm.dbg.region.start function.
736 RegionStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_start);
738 // Call llvm.dbg.func.start.
739 CallInst::Create(RegionStartFn, getCastToEmpty(D), "", BB);
742 /// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
743 /// mark the end of a region for the specified scoping descriptor.
744 void DIFactory::InsertRegionEnd(DIDescriptor D, BasicBlock *BB) {
745 // Lazily construct llvm.dbg.region.end function.
747 RegionEndFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_end);
749 // Call llvm.dbg.region.end.
750 CallInst::Create(RegionEndFn, getCastToEmpty(D), "", BB);
753 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
754 void DIFactory::InsertDeclare(Value *Storage, DIVariable D, BasicBlock *BB) {
755 // Cast the storage to a {}* for the call to llvm.dbg.declare.
756 Storage = new BitCastInst(Storage, EmptyStructPtr, "", BB);
759 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
761 Value *Args[] = { Storage, getCastToEmpty(D) };
762 CallInst::Create(DeclareFn, Args, Args+2, "", BB);
766 /// findStopPoint - Find the stoppoint coressponding to this instruction, that
767 /// is the stoppoint that dominates this instruction.
768 const DbgStopPointInst *findStopPoint(const Instruction *Inst) {
769 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(Inst))
772 const BasicBlock *BB = Inst->getParent();
773 BasicBlock::const_iterator I = Inst, B;
777 // A BB consisting only of a terminator can't have a stoppoint.
780 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
784 // This BB didn't have a stoppoint: if there is only one predecessor, look
785 // for a stoppoint there. We could use getIDom(), but that would require
787 BB = I->getParent()->getUniquePredecessor();
789 I = BB->getTerminator();
795 /// findBBStopPoint - Find the stoppoint corresponding to first real
796 /// (non-debug intrinsic) instruction in this Basic Block, and return the
797 /// stoppoint for it.
798 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB) {
799 for(BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
800 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
803 // Fallback to looking for stoppoint of unique predecessor. Useful if this
804 // BB contains no stoppoints, but unique predecessor does.
805 BB = BB->getUniquePredecessor();
807 return findStopPoint(BB->getTerminator());
812 Value *findDbgGlobalDeclare(GlobalVariable *V) {
813 const Module *M = V->getParent();
814 const Type *Ty = M->getTypeByName("llvm.dbg.global_variable.type");
817 Ty = PointerType::get(Ty, 0);
819 Value *Val = V->stripPointerCasts();
820 for (Value::use_iterator I = Val->use_begin(), E = Val->use_end();
822 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(I)) {
823 if (CE->getOpcode() == Instruction::BitCast) {
826 while (VV->hasOneUse())
827 VV = *VV->use_begin();
829 if (VV->getType() == Ty)
835 if (Val->getType() == Ty)
841 /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
842 /// It looks through pointer casts too.
843 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts) {
845 V = V->stripPointerCasts();
847 // Look for the bitcast.
848 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
850 if (isa<BitCastInst>(I))
851 return findDbgDeclare(*I, false);
856 // Find llvm.dbg.declare among uses of the instruction.
857 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
859 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I))
865 bool getLocationInfo(const Value *V, std::string &DisplayName,
866 std::string &Type, unsigned &LineNo, std::string &File,
871 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
872 Value *DIGV = findDbgGlobalDeclare(GV);
873 if (!DIGV) return false;
874 DIGlobalVariable Var(cast<GlobalVariable>(DIGV));
876 Var.getDisplayName(DisplayName);
877 LineNo = Var.getLineNumber();
878 Unit = Var.getCompileUnit();
879 TypeD = Var.getType();
881 const DbgDeclareInst *DDI = findDbgDeclare(V);
882 if (!DDI) return false;
883 DIVariable Var(cast<GlobalVariable>(DDI->getVariable()));
885 Var.getName(DisplayName);
886 LineNo = Var.getLineNumber();
887 Unit = Var.getCompileUnit();
888 TypeD = Var.getType();
892 Unit.getFilename(File);
893 Unit.getDirectory(Dir);
897 /// CollectDebugInfoAnchors - Collect debugging information anchors.
898 void CollectDebugInfoAnchors(Module &M,
899 SmallVector<GlobalVariable *, 2> &CUs,
900 SmallVector<GlobalVariable *, 4> &GVs,
901 SmallVector<GlobalVariable *, 4> &SPs) {
903 for (Module::global_iterator GVI = M.global_begin(), E = M.global_end();
905 GlobalVariable *GV = GVI;
906 if (GV->hasName() && strncmp(GV->getNameStart(), "llvm.dbg", 8) == 0
907 && GV->isConstant() && GV->hasInitializer()) {
909 if (C.isNull() == false) {
913 DIGlobalVariable G(GV);
914 if (G.isNull() == false) {
919 if (S.isNull() == false) {
928 /// dump - Print descriptor.
929 void DIDescriptor::dump() const {
930 cerr << "[" << dwarf::TagString(getTag()) << "] ";
931 cerr << std::hex << "[GV:" << DbgGV << "]" << std::dec;
934 /// dump - Print compile unit.
935 void DICompileUnit::dump() const {
937 cerr << " [" << dwarf::LanguageString(getLanguage()) << "] ";
939 std::string Res1, Res2;
940 cerr << " [" << getDirectory(Res1) << "/" << getFilename(Res2) << " ]";
943 /// dump - Print type.
944 void DIType::dump() const {
945 if (isNull()) return;
948 if (!getName(Res).empty())
949 cerr << " [" << Res << "] ";
951 unsigned Tag = getTag();
952 cerr << " [" << dwarf::TagString(Tag) << "] ";
954 // TODO : Print context
955 getCompileUnit().dump();
957 << getLineNumber() << ", "
958 << getSizeInBits() << ", "
959 << getAlignInBits() << ", "
964 cerr << " [private] ";
965 else if (isProtected())
966 cerr << " [protected] ";
971 if (isBasicType(Tag))
972 DIBasicType(DbgGV).dump();
973 else if (isDerivedType(Tag))
974 DIDerivedType(DbgGV).dump();
975 else if (isCompositeType(Tag))
976 DICompositeType(DbgGV).dump();
978 cerr << "Invalid DIType\n";
985 /// dump - Print basic type.
986 void DIBasicType::dump() const {
987 cerr << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
990 /// dump - Print derived type.
991 void DIDerivedType::dump() const {
992 cerr << "\n\t Derived From: "; getTypeDerivedFrom().dump();
995 /// dump - Print composite type.
996 void DICompositeType::dump() const {
997 DIArray A = getTypeArray();
1000 cerr << " [" << A.getNumElements() << " elements]";
1003 /// dump - Print global.
1004 void DIGlobal::dump() const {
1006 if (!getName(Res).empty())
1007 cerr << " [" << Res << "] ";
1009 unsigned Tag = getTag();
1010 cerr << " [" << dwarf::TagString(Tag) << "] ";
1012 // TODO : Print context
1013 getCompileUnit().dump();
1014 cerr << " [" << getLineNumber() << "] ";
1016 if (isLocalToUnit())
1017 cerr << " [local] ";
1022 if (isGlobalVariable(Tag))
1023 DIGlobalVariable(DbgGV).dump();
1028 /// dump - Print subprogram.
1029 void DISubprogram::dump() const {
1033 /// dump - Print global variable.
1034 void DIGlobalVariable::dump() const {
1035 cerr << " ["; getGlobal()->dump(); cerr << "] ";
1038 /// dump - Print variable.
1039 void DIVariable::dump() const {
1041 if (!getName(Res).empty())
1042 cerr << " [" << Res << "] ";
1044 getCompileUnit().dump();
1045 cerr << " [" << getLineNumber() << "] ";