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"
25 //===----------------------------------------------------------------------===//
27 //===----------------------------------------------------------------------===//
29 DIDescriptor::DIDescriptor(GlobalVariable *gv, unsigned RequiredTag) {
32 // If this is non-null, check to see if the Tag matches. If not, set to null.
33 if (GV && getTag() != RequiredTag)
38 std::string DIDescriptor::getStringField(unsigned Elt) const {
39 if (GV == 0) return "";
40 Constant *C = GV->getInitializer();
41 if (C == 0 || Elt >= C->getNumOperands())
45 // Fills in the string if it succeeds
46 if (!GetConstantStringInfo(C->getOperand(Elt), Result))
51 uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
52 if (GV == 0) return 0;
53 Constant *C = GV->getInitializer();
54 if (C == 0 || Elt >= C->getNumOperands())
56 if (ConstantInt *CI = dyn_cast<ConstantInt>(C->getOperand(Elt)))
57 return CI->getZExtValue();
62 DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
63 if (GV == 0) return DIDescriptor();
64 Constant *C = GV->getInitializer();
65 if (C == 0 || Elt >= C->getNumOperands())
66 return DIDescriptor();
67 C = C->getOperand(Elt);
68 return DIDescriptor(dyn_cast<GlobalVariable>(C->stripPointerCasts()));
71 GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
72 if (GV == 0) return 0;
73 Constant *C = GV->getInitializer();
74 if (C == 0 || Elt >= C->getNumOperands())
76 C = C->getOperand(Elt);
78 return dyn_cast<GlobalVariable>(C->stripPointerCasts());
83 //===----------------------------------------------------------------------===//
84 // Simple Descriptor Constructors and other Methods
85 //===----------------------------------------------------------------------===//
87 DIAnchor::DIAnchor(GlobalVariable *GV)
88 : DIDescriptor(GV, dwarf::DW_TAG_anchor) {}
89 DIEnumerator::DIEnumerator(GlobalVariable *GV)
90 : DIDescriptor(GV, dwarf::DW_TAG_enumerator) {}
91 DISubrange::DISubrange(GlobalVariable *GV)
92 : DIDescriptor(GV, dwarf::DW_TAG_subrange_type) {}
93 DICompileUnit::DICompileUnit(GlobalVariable *GV)
94 : DIDescriptor(GV, dwarf::DW_TAG_compile_unit) {}
95 DIBasicType::DIBasicType(GlobalVariable *GV)
96 : DIType(GV, dwarf::DW_TAG_base_type) {}
97 DISubprogram::DISubprogram(GlobalVariable *GV)
98 : DIGlobal(GV, dwarf::DW_TAG_subprogram) {}
99 DIGlobalVariable::DIGlobalVariable(GlobalVariable *GV)
100 : DIGlobal(GV, dwarf::DW_TAG_variable) {}
101 DIBlock::DIBlock(GlobalVariable *GV)
102 : DIDescriptor(GV, dwarf::DW_TAG_lexical_block) {}
103 // needed by DIVariable::getType()
104 DIType::DIType(GlobalVariable *gv) : DIDescriptor(gv) {
106 unsigned tag = getTag();
107 if (tag != dwarf::DW_TAG_base_type && !DIDerivedType::isDerivedType(tag) &&
108 !DICompositeType::isCompositeType(tag))
112 /// isDerivedType - Return true if the specified tag is legal for
114 bool DIType::isDerivedType(unsigned Tag) {
116 case dwarf::DW_TAG_typedef:
117 case dwarf::DW_TAG_pointer_type:
118 case dwarf::DW_TAG_reference_type:
119 case dwarf::DW_TAG_const_type:
120 case dwarf::DW_TAG_volatile_type:
121 case dwarf::DW_TAG_restrict_type:
122 case dwarf::DW_TAG_member:
123 case dwarf::DW_TAG_inheritance:
126 // FIXME: Even though it doesn't make sense, CompositeTypes are current
127 // modelled as DerivedTypes, this should return true for them as well.
132 DIDerivedType::DIDerivedType(GlobalVariable *GV) : DIType(GV, true, true) {
133 if (GV && !isDerivedType(getTag()))
137 /// isCompositeType - Return true if the specified tag is legal for
139 bool DIType::isCompositeType(unsigned TAG) {
141 case dwarf::DW_TAG_array_type:
142 case dwarf::DW_TAG_structure_type:
143 case dwarf::DW_TAG_union_type:
144 case dwarf::DW_TAG_enumeration_type:
145 case dwarf::DW_TAG_vector_type:
146 case dwarf::DW_TAG_subroutine_type:
153 DICompositeType::DICompositeType(GlobalVariable *GV)
154 : DIDerivedType(GV, true, true) {
155 if (GV && !isCompositeType(getTag()))
159 /// isVariable - Return true if the specified tag is legal for DIVariable.
160 bool DIVariable::isVariable(unsigned Tag) {
162 case dwarf::DW_TAG_auto_variable:
163 case dwarf::DW_TAG_arg_variable:
164 case dwarf::DW_TAG_return_variable:
171 DIVariable::DIVariable(GlobalVariable *GV) : DIDescriptor(GV) {
172 if (GV && !isVariable(getTag()))
176 unsigned DIArray::getNumElements() const {
177 assert (GV && "Invalid DIArray");
178 Constant *C = GV->getInitializer();
179 assert (C && "Invalid DIArray initializer");
180 return C->getNumOperands();
183 /// Verify - Verify that a compile unit is well formed.
184 bool DICompileUnit::Verify() const {
187 if (getFilename().empty())
189 // It is possible that directory and produce string is empty.
193 /// Verify - Verify that a type descriptor is well formed.
194 bool DIType::Verify() const {
197 if (getContext().isNull())
200 DICompileUnit CU = getCompileUnit();
201 if (!CU.isNull() && !CU.Verify())
206 /// Verify - Verify that a composite type descriptor is well formed.
207 bool DICompositeType::Verify() const {
210 if (getContext().isNull())
213 DICompileUnit CU = getCompileUnit();
214 if (!CU.isNull() && !CU.Verify())
219 /// Verify - Verify that a subprogram descriptor is well formed.
220 bool DISubprogram::Verify() const {
224 if (getContext().isNull())
227 DICompileUnit CU = getCompileUnit();
231 DICompositeType Ty = getType();
232 if (!Ty.isNull() && !Ty.Verify())
237 /// Verify - Verify that a global variable descriptor is well formed.
238 bool DIGlobalVariable::Verify() const {
242 if (getContext().isNull())
245 DICompileUnit CU = getCompileUnit();
249 DIType Ty = getType();
259 /// Verify - Verify that a variable descriptor is well formed.
260 bool DIVariable::Verify() const {
264 if (getContext().isNull())
267 DIType Ty = getType();
277 //===----------------------------------------------------------------------===//
278 // DIFactory: Basic Helpers
279 //===----------------------------------------------------------------------===//
281 DIFactory::DIFactory(Module &m) : M(m) {
282 StopPointFn = FuncStartFn = RegionStartFn = RegionEndFn = DeclareFn = 0;
283 EmptyStructPtr = PointerType::getUnqual(StructType::get(NULL, NULL));
286 /// getCastToEmpty - Return this descriptor as a Constant* with type '{}*'.
287 /// This is only valid when the descriptor is non-null.
288 Constant *DIFactory::getCastToEmpty(DIDescriptor D) {
289 if (D.isNull()) return Constant::getNullValue(EmptyStructPtr);
290 return ConstantExpr::getBitCast(D.getGV(), EmptyStructPtr);
293 Constant *DIFactory::GetTagConstant(unsigned TAG) {
294 assert((TAG & LLVMDebugVersionMask) == 0 &&
295 "Tag too large for debug encoding!");
296 return ConstantInt::get(Type::Int32Ty, TAG | LLVMDebugVersion);
299 Constant *DIFactory::GetStringConstant(const std::string &String) {
300 // Check string cache for previous edition.
301 Constant *&Slot = StringCache[String];
303 // Return Constant if previously defined.
304 if (Slot) return Slot;
306 const PointerType *DestTy = PointerType::getUnqual(Type::Int8Ty);
308 // If empty string then use a sbyte* null instead.
310 return Slot = ConstantPointerNull::get(DestTy);
312 // Construct string as an llvm constant.
313 Constant *ConstStr = ConstantArray::get(String);
315 // Otherwise create and return a new string global.
316 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
317 GlobalVariable::InternalLinkage,
318 ConstStr, ".str", &M);
319 StrGV->setSection("llvm.metadata");
320 return Slot = ConstantExpr::getBitCast(StrGV, DestTy);
323 /// GetOrCreateAnchor - Look up an anchor for the specified tag and name. If it
324 /// already exists, return it. If not, create a new one and return it.
325 DIAnchor DIFactory::GetOrCreateAnchor(unsigned TAG, const char *Name) {
326 const Type *EltTy = StructType::get(Type::Int32Ty, Type::Int32Ty, NULL);
328 // Otherwise, create the global or return it if already in the module.
329 Constant *C = M.getOrInsertGlobal(Name, EltTy);
330 assert(isa<GlobalVariable>(C) && "Incorrectly typed anchor?");
331 GlobalVariable *GV = cast<GlobalVariable>(C);
333 // If it has an initializer, it is already in the module.
334 if (GV->hasInitializer())
335 return SubProgramAnchor = DIAnchor(GV);
337 GV->setLinkage(GlobalValue::LinkOnceLinkage);
338 GV->setSection("llvm.metadata");
339 GV->setConstant(true);
340 M.addTypeName("llvm.dbg.anchor.type", EltTy);
342 // Otherwise, set the initializer.
344 GetTagConstant(dwarf::DW_TAG_anchor),
345 ConstantInt::get(Type::Int32Ty, TAG)
348 GV->setInitializer(ConstantStruct::get(Elts, 2));
354 //===----------------------------------------------------------------------===//
355 // DIFactory: Primary Constructors
356 //===----------------------------------------------------------------------===//
358 /// GetOrCreateCompileUnitAnchor - Return the anchor for compile units,
359 /// creating a new one if there isn't already one in the module.
360 DIAnchor DIFactory::GetOrCreateCompileUnitAnchor() {
361 // If we already created one, just return it.
362 if (!CompileUnitAnchor.isNull())
363 return CompileUnitAnchor;
364 return CompileUnitAnchor = GetOrCreateAnchor(dwarf::DW_TAG_compile_unit,
365 "llvm.dbg.compile_units");
368 /// GetOrCreateSubprogramAnchor - Return the anchor for subprograms,
369 /// creating a new one if there isn't already one in the module.
370 DIAnchor DIFactory::GetOrCreateSubprogramAnchor() {
371 // If we already created one, just return it.
372 if (!SubProgramAnchor.isNull())
373 return SubProgramAnchor;
374 return SubProgramAnchor = GetOrCreateAnchor(dwarf::DW_TAG_subprogram,
375 "llvm.dbg.subprograms");
378 /// GetOrCreateGlobalVariableAnchor - Return the anchor for globals,
379 /// creating a new one if there isn't already one in the module.
380 DIAnchor DIFactory::GetOrCreateGlobalVariableAnchor() {
381 // If we already created one, just return it.
382 if (!GlobalVariableAnchor.isNull())
383 return GlobalVariableAnchor;
384 return GlobalVariableAnchor = GetOrCreateAnchor(dwarf::DW_TAG_variable,
385 "llvm.dbg.global_variables");
388 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
389 /// This implicitly uniques the arrays created.
390 DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
391 SmallVector<Constant*, 16> Elts;
393 for (unsigned i = 0; i != NumTys; ++i)
394 Elts.push_back(getCastToEmpty(Tys[i]));
396 Constant *Init = ConstantArray::get(ArrayType::get(EmptyStructPtr,
398 &Elts[0], Elts.size());
399 // If we already have this array, just return the uniqued version.
400 DIDescriptor &Entry = SimpleConstantCache[Init];
401 if (!Entry.isNull()) return DIArray(Entry.getGV());
403 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
404 GlobalValue::InternalLinkage,
405 Init, "llvm.dbg.array", &M);
406 GV->setSection("llvm.metadata");
407 Entry = DIDescriptor(GV);
411 /// GetOrCreateSubrange - Create a descriptor for a value range. This
412 /// implicitly uniques the values returned.
413 DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
415 GetTagConstant(dwarf::DW_TAG_subrange_type),
416 ConstantInt::get(Type::Int64Ty, Lo),
417 ConstantInt::get(Type::Int64Ty, Hi)
420 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
422 // If we already have this range, just return the uniqued version.
423 DIDescriptor &Entry = SimpleConstantCache[Init];
424 if (!Entry.isNull()) return DISubrange(Entry.getGV());
426 M.addTypeName("llvm.dbg.subrange.type", Init->getType());
428 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
429 GlobalValue::InternalLinkage,
430 Init, "llvm.dbg.subrange", &M);
431 GV->setSection("llvm.metadata");
432 Entry = DIDescriptor(GV);
433 return DISubrange(GV);
438 /// CreateCompileUnit - Create a new descriptor for the specified compile
439 /// unit. Note that this does not unique compile units within the module.
440 DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
441 const std::string &Filename,
442 const std::string &Directory,
443 const std::string &Producer,
447 GetTagConstant(dwarf::DW_TAG_compile_unit),
448 getCastToEmpty(GetOrCreateCompileUnitAnchor()),
449 ConstantInt::get(Type::Int32Ty, LangID),
450 GetStringConstant(Filename),
451 GetStringConstant(Directory),
452 GetStringConstant(Producer),
453 ConstantInt::get(Type::Int1Ty, isOptimized),
454 GetStringConstant(Flags)
457 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
459 M.addTypeName("llvm.dbg.compile_unit.type", Init->getType());
460 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
461 GlobalValue::InternalLinkage,
462 Init, "llvm.dbg.compile_unit", &M);
463 GV->setSection("llvm.metadata");
464 return DICompileUnit(GV);
467 /// CreateEnumerator - Create a single enumerator value.
468 DIEnumerator DIFactory::CreateEnumerator(const std::string &Name, uint64_t Val){
470 GetTagConstant(dwarf::DW_TAG_enumerator),
471 GetStringConstant(Name),
472 ConstantInt::get(Type::Int64Ty, Val)
475 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
477 M.addTypeName("llvm.dbg.enumerator.type", Init->getType());
478 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
479 GlobalValue::InternalLinkage,
480 Init, "llvm.dbg.enumerator", &M);
481 GV->setSection("llvm.metadata");
482 return DIEnumerator(GV);
486 /// CreateBasicType - Create a basic type like int, float, etc.
487 DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
488 const std::string &Name,
489 DICompileUnit CompileUnit,
492 uint64_t AlignInBits,
493 uint64_t OffsetInBits, unsigned Flags,
495 const std::string *FileName,
496 const std::string *Directory) {
498 GetTagConstant(dwarf::DW_TAG_base_type),
499 getCastToEmpty(Context),
500 GetStringConstant(Name),
501 getCastToEmpty(CompileUnit),
502 ConstantInt::get(Type::Int32Ty, LineNumber),
503 ConstantInt::get(Type::Int64Ty, SizeInBits),
504 ConstantInt::get(Type::Int64Ty, AlignInBits),
505 ConstantInt::get(Type::Int64Ty, OffsetInBits),
506 ConstantInt::get(Type::Int32Ty, Flags),
507 ConstantInt::get(Type::Int32Ty, Encoding),
508 GetStringConstant(FileName ? FileName->c_str() : ""),
509 GetStringConstant(Directory ? Directory->c_str() : "")
512 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
514 M.addTypeName("llvm.dbg.basictype.type", Init->getType());
515 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
516 GlobalValue::InternalLinkage,
517 Init, "llvm.dbg.basictype", &M);
518 GV->setSection("llvm.metadata");
519 return DIBasicType(GV);
522 /// CreateDerivedType - Create a derived type like const qualified type,
523 /// pointer, typedef, etc.
524 DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
525 DIDescriptor Context,
526 const std::string &Name,
527 DICompileUnit CompileUnit,
530 uint64_t AlignInBits,
531 uint64_t OffsetInBits,
534 const std::string *FileName,
535 const std::string *Directory) {
538 getCastToEmpty(Context),
539 GetStringConstant(Name),
540 getCastToEmpty(CompileUnit),
541 ConstantInt::get(Type::Int32Ty, LineNumber),
542 ConstantInt::get(Type::Int64Ty, SizeInBits),
543 ConstantInt::get(Type::Int64Ty, AlignInBits),
544 ConstantInt::get(Type::Int64Ty, OffsetInBits),
545 ConstantInt::get(Type::Int32Ty, Flags),
546 getCastToEmpty(DerivedFrom),
547 GetStringConstant(FileName ? FileName->c_str() : ""),
548 GetStringConstant(Directory ? Directory->c_str() : "")
551 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
553 M.addTypeName("llvm.dbg.derivedtype.type", Init->getType());
554 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
555 GlobalValue::InternalLinkage,
556 Init, "llvm.dbg.derivedtype", &M);
557 GV->setSection("llvm.metadata");
558 return DIDerivedType(GV);
561 /// CreateCompositeType - Create a composite type like array, struct, etc.
562 DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
563 DIDescriptor Context,
564 const std::string &Name,
565 DICompileUnit CompileUnit,
568 uint64_t AlignInBits,
569 uint64_t OffsetInBits,
573 const std::string *FileName,
574 const std::string *Directory) {
578 getCastToEmpty(Context),
579 GetStringConstant(Name),
580 getCastToEmpty(CompileUnit),
581 ConstantInt::get(Type::Int32Ty, LineNumber),
582 ConstantInt::get(Type::Int64Ty, SizeInBits),
583 ConstantInt::get(Type::Int64Ty, AlignInBits),
584 ConstantInt::get(Type::Int64Ty, OffsetInBits),
585 ConstantInt::get(Type::Int32Ty, Flags),
586 getCastToEmpty(DerivedFrom),
587 getCastToEmpty(Elements),
588 GetStringConstant(FileName ? FileName->c_str() : ""),
589 GetStringConstant(Directory ? Directory->c_str() : "")
592 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
594 M.addTypeName("llvm.dbg.composite.type", Init->getType());
595 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
596 GlobalValue::InternalLinkage,
597 Init, "llvm.dbg.composite", &M);
598 GV->setSection("llvm.metadata");
599 return DICompositeType(GV);
603 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
604 /// See comments in DISubprogram for descriptions of these fields. This
605 /// method does not unique the generated descriptors.
606 DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
607 const std::string &Name,
608 const std::string &DisplayName,
609 const std::string &LinkageName,
610 DICompileUnit CompileUnit,
611 unsigned LineNo, DIType Type,
614 const std::string *FileName,
615 const std::string *Directory) {
618 GetTagConstant(dwarf::DW_TAG_subprogram),
619 getCastToEmpty(GetOrCreateSubprogramAnchor()),
620 getCastToEmpty(Context),
621 GetStringConstant(Name),
622 GetStringConstant(DisplayName),
623 GetStringConstant(LinkageName),
624 getCastToEmpty(CompileUnit),
625 ConstantInt::get(Type::Int32Ty, LineNo),
626 getCastToEmpty(Type),
627 ConstantInt::get(Type::Int1Ty, isLocalToUnit),
628 ConstantInt::get(Type::Int1Ty, isDefinition),
629 GetStringConstant(FileName ? FileName->c_str() : ""),
630 GetStringConstant(Directory ? Directory->c_str() : "")
633 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
635 M.addTypeName("llvm.dbg.subprogram.type", Init->getType());
636 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
637 GlobalValue::InternalLinkage,
638 Init, "llvm.dbg.subprogram", &M);
639 GV->setSection("llvm.metadata");
640 return DISubprogram(GV);
643 /// CreateGlobalVariable - Create a new descriptor for the specified global.
645 DIFactory::CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
646 const std::string &DisplayName,
647 const std::string &LinkageName,
648 DICompileUnit CompileUnit,
649 unsigned LineNo, DIType Type,bool isLocalToUnit,
650 bool isDefinition, llvm::GlobalVariable *Val,
651 const std::string *FileName,
652 const std::string *Directory) {
654 GetTagConstant(dwarf::DW_TAG_variable),
655 getCastToEmpty(GetOrCreateGlobalVariableAnchor()),
656 getCastToEmpty(Context),
657 GetStringConstant(Name),
658 GetStringConstant(DisplayName),
659 GetStringConstant(LinkageName),
660 getCastToEmpty(CompileUnit),
661 ConstantInt::get(Type::Int32Ty, LineNo),
662 getCastToEmpty(Type),
663 ConstantInt::get(Type::Int1Ty, isLocalToUnit),
664 ConstantInt::get(Type::Int1Ty, isDefinition),
665 ConstantExpr::getBitCast(Val, EmptyStructPtr),
666 GetStringConstant(FileName ? FileName->c_str() : ""),
667 GetStringConstant(Directory ? Directory->c_str() : "")
670 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
672 M.addTypeName("llvm.dbg.global_variable.type", Init->getType());
673 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
674 GlobalValue::InternalLinkage,
675 Init, "llvm.dbg.global_variable", &M);
676 GV->setSection("llvm.metadata");
677 return DIGlobalVariable(GV);
681 /// CreateVariable - Create a new descriptor for the specified variable.
682 DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
683 const std::string &Name,
684 DICompileUnit CompileUnit, unsigned LineNo,
686 const std::string *FileName,
687 const std::string *Directory) {
692 getCastToEmpty(Context),
693 GetStringConstant(Name),
694 getCastToEmpty(CompileUnit),
695 ConstantInt::get(Type::Int32Ty, LineNo),
696 getCastToEmpty(Type),
697 GetStringConstant(FileName ? FileName->c_str() : ""),
698 GetStringConstant(Directory ? Directory->c_str() : "")
701 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
703 M.addTypeName("llvm.dbg.variable.type", Init->getType());
704 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
705 GlobalValue::InternalLinkage,
706 Init, "llvm.dbg.variable", &M);
707 GV->setSection("llvm.metadata");
708 return DIVariable(GV);
712 /// CreateBlock - This creates a descriptor for a lexical block with the
713 /// specified parent context.
714 DIBlock DIFactory::CreateBlock(DIDescriptor Context) {
716 GetTagConstant(dwarf::DW_TAG_lexical_block),
717 getCastToEmpty(Context)
720 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
722 M.addTypeName("llvm.dbg.block.type", Init->getType());
723 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
724 GlobalValue::InternalLinkage,
725 Init, "llvm.dbg.block", &M);
726 GV->setSection("llvm.metadata");
731 //===----------------------------------------------------------------------===//
732 // DIFactory: Routines for inserting code into a function
733 //===----------------------------------------------------------------------===//
735 /// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
736 /// inserting it at the end of the specified basic block.
737 void DIFactory::InsertStopPoint(DICompileUnit CU, unsigned LineNo,
738 unsigned ColNo, BasicBlock *BB) {
740 // Lazily construct llvm.dbg.stoppoint function.
742 StopPointFn = llvm::Intrinsic::getDeclaration(&M,
743 llvm::Intrinsic::dbg_stoppoint);
745 // Invoke llvm.dbg.stoppoint
747 llvm::ConstantInt::get(llvm::Type::Int32Ty, LineNo),
748 llvm::ConstantInt::get(llvm::Type::Int32Ty, ColNo),
751 CallInst::Create(StopPointFn, Args, Args+3, "", BB);
754 /// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
755 /// mark the start of the specified subprogram.
756 void DIFactory::InsertSubprogramStart(DISubprogram SP, BasicBlock *BB) {
757 // Lazily construct llvm.dbg.func.start.
759 FuncStartFn = llvm::Intrinsic::getDeclaration(&M,
760 llvm::Intrinsic::dbg_func_start);
762 // Call llvm.dbg.func.start which also implicitly sets a stoppoint.
763 CallInst::Create(FuncStartFn, getCastToEmpty(SP), "", BB);
766 /// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
767 /// mark the start of a region for the specified scoping descriptor.
768 void DIFactory::InsertRegionStart(DIDescriptor D, BasicBlock *BB) {
769 // Lazily construct llvm.dbg.region.start function.
771 RegionStartFn = llvm::Intrinsic::getDeclaration(&M,
772 llvm::Intrinsic::dbg_region_start);
773 // Call llvm.dbg.func.start.
774 CallInst::Create(RegionStartFn, getCastToEmpty(D), "", BB);
778 /// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
779 /// mark the end of a region for the specified scoping descriptor.
780 void DIFactory::InsertRegionEnd(DIDescriptor D, BasicBlock *BB) {
781 // Lazily construct llvm.dbg.region.end function.
783 RegionEndFn = llvm::Intrinsic::getDeclaration(&M,
784 llvm::Intrinsic::dbg_region_end);
786 CallInst::Create(RegionEndFn, getCastToEmpty(D), "", BB);
789 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
790 void DIFactory::InsertDeclare(llvm::Value *Storage, DIVariable D,
792 // Cast the storage to a {}* for the call to llvm.dbg.declare.
793 Storage = new llvm::BitCastInst(Storage, EmptyStructPtr, "", BB);
796 DeclareFn = llvm::Intrinsic::getDeclaration(&M,
797 llvm::Intrinsic::dbg_declare);
798 Value *Args[] = { Storage, getCastToEmpty(D) };
799 CallInst::Create(DeclareFn, Args, Args+2, "", BB);
803 /// Finds the stoppoint coressponding to this instruction, that is the
804 /// stoppoint that dominates this instruction
805 const DbgStopPointInst *findStopPoint(const Instruction *Inst)
807 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(Inst))
810 const BasicBlock *BB = Inst->getParent();
811 BasicBlock::const_iterator I = Inst, B;
814 // A BB consisting only of a terminator can't have a stoppoint.
818 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
822 // This BB didn't have a stoppoint: if there is only one
823 // predecessor, look for a stoppoint there.
824 // We could use getIDom(), but that would require dominator info.
825 BB = I->getParent()->getUniquePredecessor();
827 I = BB->getTerminator();
832 /// Finds the stoppoint corresponding to first real (non-debug intrinsic)
833 /// instruction in this Basic Block, and returns the stoppoint for it.
834 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB)
836 for(BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
837 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
840 // Fallback to looking for stoppoint of unique predecessor.
841 // Useful if this BB contains no stoppoints, but unique predecessor does.
842 BB = BB->getUniquePredecessor();
844 return findStopPoint(BB->getTerminator());
848 /// Finds the dbg.declare intrinsic corresponding to this value if any.
849 /// It looks through pointer casts too.
850 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts)
853 V = V->stripPointerCasts();
854 // Look for the bitcast.
855 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
857 if (isa<BitCastInst>(I))
858 return findDbgDeclare(*I, false);
863 // Find dbg.declare among uses of the instruction.
864 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
866 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I))