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"
26 using namespace llvm::dwarf
;
28 //===----------------------------------------------------------------------===//
30 //===----------------------------------------------------------------------===//
32 /// ValidDebugInfo - Return true if V represents valid debug info value.
33 bool DIDescriptor::ValidDebugInfo(Value
*V
, CodeGenOpt::Level OptLevel
) {
37 GlobalVariable
*GV
= dyn_cast
<GlobalVariable
>(V
->stripPointerCasts());
41 if (!GV
->hasInternalLinkage () && !GV
->hasLinkOnceLinkage())
46 // Check current version. Allow Version6 for now.
47 unsigned Version
= DI
.getVersion();
48 if (Version
!= LLVMDebugVersion
&& Version
!= LLVMDebugVersion6
)
51 unsigned Tag
= DI
.getTag();
54 assert(DIVariable(GV
).Verify() && "Invalid DebugInfo value");
56 case DW_TAG_compile_unit
:
57 assert(DICompileUnit(GV
).Verify() && "Invalid DebugInfo value");
59 case DW_TAG_subprogram
:
60 assert(DISubprogram(GV
).Verify() && "Invalid DebugInfo value");
62 case DW_TAG_lexical_block
:
63 /// FIXME. This interfers with the quality of generated code when
64 /// during optimization.
65 if (OptLevel
!= CodeGenOpt::None
)
74 DIDescriptor::DIDescriptor(GlobalVariable
*gv
, unsigned RequiredTag
) {
77 // If this is non-null, check to see if the Tag matches. If not, set to null.
78 if (GV
&& getTag() != RequiredTag
)
83 DIDescriptor::getStringField(unsigned Elt
, std::string
&Result
) const {
89 Constant
*C
= GV
->getInitializer();
90 if (C
== 0 || Elt
>= C
->getNumOperands()) {
95 // Fills in the string if it succeeds
96 if (!GetConstantStringInfo(C
->getOperand(Elt
), Result
))
102 uint64_t DIDescriptor::getUInt64Field(unsigned Elt
) const {
103 if (GV
== 0) return 0;
104 Constant
*C
= GV
->getInitializer();
105 if (C
== 0 || Elt
>= C
->getNumOperands())
107 if (ConstantInt
*CI
= dyn_cast
<ConstantInt
>(C
->getOperand(Elt
)))
108 return CI
->getZExtValue();
112 DIDescriptor
DIDescriptor::getDescriptorField(unsigned Elt
) const {
113 if (GV
== 0) return DIDescriptor();
114 Constant
*C
= GV
->getInitializer();
115 if (C
== 0 || Elt
>= C
->getNumOperands())
116 return DIDescriptor();
117 C
= C
->getOperand(Elt
);
118 return DIDescriptor(dyn_cast
<GlobalVariable
>(C
->stripPointerCasts()));
121 GlobalVariable
*DIDescriptor::getGlobalVariableField(unsigned Elt
) const {
122 if (GV
== 0) return 0;
123 Constant
*C
= GV
->getInitializer();
124 if (C
== 0 || Elt
>= C
->getNumOperands())
126 C
= C
->getOperand(Elt
);
128 return dyn_cast
<GlobalVariable
>(C
->stripPointerCasts());
133 //===----------------------------------------------------------------------===//
134 // Simple Descriptor Constructors and other Methods
135 //===----------------------------------------------------------------------===//
137 DIAnchor::DIAnchor(GlobalVariable
*GV
)
138 : DIDescriptor(GV
, dwarf::DW_TAG_anchor
) {}
139 DIEnumerator::DIEnumerator(GlobalVariable
*GV
)
140 : DIDescriptor(GV
, dwarf::DW_TAG_enumerator
) {}
141 DISubrange::DISubrange(GlobalVariable
*GV
)
142 : DIDescriptor(GV
, dwarf::DW_TAG_subrange_type
) {}
143 DICompileUnit::DICompileUnit(GlobalVariable
*GV
)
144 : DIDescriptor(GV
, dwarf::DW_TAG_compile_unit
) {}
145 DIBasicType::DIBasicType(GlobalVariable
*GV
)
146 : DIType(GV
, dwarf::DW_TAG_base_type
) {}
147 DISubprogram::DISubprogram(GlobalVariable
*GV
)
148 : DIGlobal(GV
, dwarf::DW_TAG_subprogram
) {}
149 DIGlobalVariable::DIGlobalVariable(GlobalVariable
*GV
)
150 : DIGlobal(GV
, dwarf::DW_TAG_variable
) {}
151 DIBlock::DIBlock(GlobalVariable
*GV
)
152 : DIDescriptor(GV
, dwarf::DW_TAG_lexical_block
) {}
153 // needed by DIVariable::getType()
154 DIType::DIType(GlobalVariable
*gv
) : DIDescriptor(gv
) {
156 unsigned tag
= getTag();
157 if (tag
!= dwarf::DW_TAG_base_type
&& !DIDerivedType::isDerivedType(tag
) &&
158 !DICompositeType::isCompositeType(tag
))
162 /// isDerivedType - Return true if the specified tag is legal for
164 bool DIType::isDerivedType(unsigned Tag
) {
166 case dwarf::DW_TAG_typedef
:
167 case dwarf::DW_TAG_pointer_type
:
168 case dwarf::DW_TAG_reference_type
:
169 case dwarf::DW_TAG_const_type
:
170 case dwarf::DW_TAG_volatile_type
:
171 case dwarf::DW_TAG_restrict_type
:
172 case dwarf::DW_TAG_member
:
173 case dwarf::DW_TAG_inheritance
:
176 // FIXME: Even though it doesn't make sense, CompositeTypes are current
177 // modelled as DerivedTypes, this should return true for them as well.
182 DIDerivedType::DIDerivedType(GlobalVariable
*GV
) : DIType(GV
, true, true) {
183 if (GV
&& !isDerivedType(getTag()))
187 /// isCompositeType - Return true if the specified tag is legal for
189 bool DIType::isCompositeType(unsigned TAG
) {
191 case dwarf::DW_TAG_array_type
:
192 case dwarf::DW_TAG_structure_type
:
193 case dwarf::DW_TAG_union_type
:
194 case dwarf::DW_TAG_enumeration_type
:
195 case dwarf::DW_TAG_vector_type
:
196 case dwarf::DW_TAG_subroutine_type
:
197 case dwarf::DW_TAG_class_type
:
204 DICompositeType::DICompositeType(GlobalVariable
*GV
)
205 : DIDerivedType(GV
, true, true) {
206 if (GV
&& !isCompositeType(getTag()))
210 /// isVariable - Return true if the specified tag is legal for DIVariable.
211 bool DIVariable::isVariable(unsigned Tag
) {
213 case dwarf::DW_TAG_auto_variable
:
214 case dwarf::DW_TAG_arg_variable
:
215 case dwarf::DW_TAG_return_variable
:
222 DIVariable::DIVariable(GlobalVariable
*gv
) : DIDescriptor(gv
) {
223 if (gv
&& !isVariable(getTag()))
227 unsigned DIArray::getNumElements() const {
228 assert (GV
&& "Invalid DIArray");
229 Constant
*C
= GV
->getInitializer();
230 assert (C
&& "Invalid DIArray initializer");
231 return C
->getNumOperands();
234 /// Verify - Verify that a compile unit is well formed.
235 bool DICompileUnit::Verify() const {
239 if (getFilename(Res
).empty())
241 // It is possible that directory and produce string is empty.
245 /// Verify - Verify that a type descriptor is well formed.
246 bool DIType::Verify() const {
249 if (getContext().isNull())
252 DICompileUnit CU
= getCompileUnit();
253 if (!CU
.isNull() && !CU
.Verify())
258 /// Verify - Verify that a composite type descriptor is well formed.
259 bool DICompositeType::Verify() const {
262 if (getContext().isNull())
265 DICompileUnit CU
= getCompileUnit();
266 if (!CU
.isNull() && !CU
.Verify())
271 /// Verify - Verify that a subprogram descriptor is well formed.
272 bool DISubprogram::Verify() const {
276 if (getContext().isNull())
279 DICompileUnit CU
= getCompileUnit();
283 DICompositeType Ty
= getType();
284 if (!Ty
.isNull() && !Ty
.Verify())
289 /// Verify - Verify that a global variable descriptor is well formed.
290 bool DIGlobalVariable::Verify() const {
294 if (getContext().isNull())
297 DICompileUnit CU
= getCompileUnit();
301 DIType Ty
= getType();
311 /// Verify - Verify that a variable descriptor is well formed.
312 bool DIVariable::Verify() const {
316 if (getContext().isNull())
319 DIType Ty
= getType();
327 /// getOriginalTypeSize - If this type is derived from a base type then
328 /// return base type size.
329 uint64_t DIDerivedType::getOriginalTypeSize() const {
330 if (getTag() != dwarf::DW_TAG_member
)
331 return getSizeInBits();
332 DIType BT
= getTypeDerivedFrom();
333 if (BT
.getTag() != dwarf::DW_TAG_base_type
)
334 return getSizeInBits();
335 return BT
.getSizeInBits();
338 /// describes - Return true if this subprogram provides debugging
339 /// information for the function F.
340 bool DISubprogram::describes(const Function
*F
) {
341 assert (F
&& "Invalid function");
343 getLinkageName(Name
);
346 if (!Name
.empty() && (strcmp(Name
.c_str(), F
->getNameStart()) == false))
351 //===----------------------------------------------------------------------===//
352 // DIFactory: Basic Helpers
353 //===----------------------------------------------------------------------===//
355 DIFactory::DIFactory(Module
&m
) : M(m
) {
356 StopPointFn
= FuncStartFn
= RegionStartFn
= RegionEndFn
= DeclareFn
= 0;
357 EmptyStructPtr
= PointerType::getUnqual(StructType::get(NULL
, NULL
));
360 /// getCastToEmpty - Return this descriptor as a Constant* with type '{}*'.
361 /// This is only valid when the descriptor is non-null.
362 Constant
*DIFactory::getCastToEmpty(DIDescriptor D
) {
363 if (D
.isNull()) return Constant::getNullValue(EmptyStructPtr
);
364 return ConstantExpr::getBitCast(D
.getGV(), EmptyStructPtr
);
367 Constant
*DIFactory::GetTagConstant(unsigned TAG
) {
368 assert((TAG
& LLVMDebugVersionMask
) == 0 &&
369 "Tag too large for debug encoding!");
370 return ConstantInt::get(Type::Int32Ty
, TAG
| LLVMDebugVersion
);
373 Constant
*DIFactory::GetStringConstant(const std::string
&String
) {
374 // Check string cache for previous edition.
375 Constant
*&Slot
= StringCache
[String
];
377 // Return Constant if previously defined.
378 if (Slot
) return Slot
;
380 const PointerType
*DestTy
= PointerType::getUnqual(Type::Int8Ty
);
382 // If empty string then use a sbyte* null instead.
384 return Slot
= ConstantPointerNull::get(DestTy
);
386 // Construct string as an llvm constant.
387 Constant
*ConstStr
= ConstantArray::get(String
);
389 // Otherwise create and return a new string global.
390 GlobalVariable
*StrGV
= new GlobalVariable(ConstStr
->getType(), true,
391 GlobalVariable::InternalLinkage
,
392 ConstStr
, ".str", &M
);
393 StrGV
->setSection("llvm.metadata");
394 return Slot
= ConstantExpr::getBitCast(StrGV
, DestTy
);
397 /// GetOrCreateAnchor - Look up an anchor for the specified tag and name. If it
398 /// already exists, return it. If not, create a new one and return it.
399 DIAnchor
DIFactory::GetOrCreateAnchor(unsigned TAG
, const char *Name
) {
400 const Type
*EltTy
= StructType::get(Type::Int32Ty
, Type::Int32Ty
, NULL
);
402 // Otherwise, create the global or return it if already in the module.
403 Constant
*C
= M
.getOrInsertGlobal(Name
, EltTy
);
404 assert(isa
<GlobalVariable
>(C
) && "Incorrectly typed anchor?");
405 GlobalVariable
*GV
= cast
<GlobalVariable
>(C
);
407 // If it has an initializer, it is already in the module.
408 if (GV
->hasInitializer())
409 return SubProgramAnchor
= DIAnchor(GV
);
411 GV
->setLinkage(GlobalValue::LinkOnceAnyLinkage
);
412 GV
->setSection("llvm.metadata");
413 GV
->setConstant(true);
414 M
.addTypeName("llvm.dbg.anchor.type", EltTy
);
416 // Otherwise, set the initializer.
418 GetTagConstant(dwarf::DW_TAG_anchor
),
419 ConstantInt::get(Type::Int32Ty
, TAG
)
422 GV
->setInitializer(ConstantStruct::get(Elts
, 2));
428 //===----------------------------------------------------------------------===//
429 // DIFactory: Primary Constructors
430 //===----------------------------------------------------------------------===//
432 /// GetOrCreateCompileUnitAnchor - Return the anchor for compile units,
433 /// creating a new one if there isn't already one in the module.
434 DIAnchor
DIFactory::GetOrCreateCompileUnitAnchor() {
435 // If we already created one, just return it.
436 if (!CompileUnitAnchor
.isNull())
437 return CompileUnitAnchor
;
438 return CompileUnitAnchor
= GetOrCreateAnchor(dwarf::DW_TAG_compile_unit
,
439 "llvm.dbg.compile_units");
442 /// GetOrCreateSubprogramAnchor - Return the anchor for subprograms,
443 /// creating a new one if there isn't already one in the module.
444 DIAnchor
DIFactory::GetOrCreateSubprogramAnchor() {
445 // If we already created one, just return it.
446 if (!SubProgramAnchor
.isNull())
447 return SubProgramAnchor
;
448 return SubProgramAnchor
= GetOrCreateAnchor(dwarf::DW_TAG_subprogram
,
449 "llvm.dbg.subprograms");
452 /// GetOrCreateGlobalVariableAnchor - Return the anchor for globals,
453 /// creating a new one if there isn't already one in the module.
454 DIAnchor
DIFactory::GetOrCreateGlobalVariableAnchor() {
455 // If we already created one, just return it.
456 if (!GlobalVariableAnchor
.isNull())
457 return GlobalVariableAnchor
;
458 return GlobalVariableAnchor
= GetOrCreateAnchor(dwarf::DW_TAG_variable
,
459 "llvm.dbg.global_variables");
462 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
463 /// This implicitly uniques the arrays created.
464 DIArray
DIFactory::GetOrCreateArray(DIDescriptor
*Tys
, unsigned NumTys
) {
465 SmallVector
<Constant
*, 16> Elts
;
467 for (unsigned i
= 0; i
!= NumTys
; ++i
)
468 Elts
.push_back(getCastToEmpty(Tys
[i
]));
470 Constant
*Init
= ConstantArray::get(ArrayType::get(EmptyStructPtr
,
472 &Elts
[0], Elts
.size());
473 // If we already have this array, just return the uniqued version.
474 DIDescriptor
&Entry
= SimpleConstantCache
[Init
];
475 if (!Entry
.isNull()) return DIArray(Entry
.getGV());
477 GlobalVariable
*GV
= new GlobalVariable(Init
->getType(), true,
478 GlobalValue::InternalLinkage
,
479 Init
, "llvm.dbg.array", &M
);
480 GV
->setSection("llvm.metadata");
481 Entry
= DIDescriptor(GV
);
485 /// GetOrCreateSubrange - Create a descriptor for a value range. This
486 /// implicitly uniques the values returned.
487 DISubrange
DIFactory::GetOrCreateSubrange(int64_t Lo
, int64_t Hi
) {
489 GetTagConstant(dwarf::DW_TAG_subrange_type
),
490 ConstantInt::get(Type::Int64Ty
, Lo
),
491 ConstantInt::get(Type::Int64Ty
, Hi
)
494 Constant
*Init
= ConstantStruct::get(Elts
, sizeof(Elts
)/sizeof(Elts
[0]));
496 // If we already have this range, just return the uniqued version.
497 DIDescriptor
&Entry
= SimpleConstantCache
[Init
];
498 if (!Entry
.isNull()) return DISubrange(Entry
.getGV());
500 M
.addTypeName("llvm.dbg.subrange.type", Init
->getType());
502 GlobalVariable
*GV
= new GlobalVariable(Init
->getType(), true,
503 GlobalValue::InternalLinkage
,
504 Init
, "llvm.dbg.subrange", &M
);
505 GV
->setSection("llvm.metadata");
506 Entry
= DIDescriptor(GV
);
507 return DISubrange(GV
);
512 /// CreateCompileUnit - Create a new descriptor for the specified compile
513 /// unit. Note that this does not unique compile units within the module.
514 DICompileUnit
DIFactory::CreateCompileUnit(unsigned LangID
,
515 const std::string
&Filename
,
516 const std::string
&Directory
,
517 const std::string
&Producer
,
521 unsigned RunTimeVer
) {
523 GetTagConstant(dwarf::DW_TAG_compile_unit
),
524 getCastToEmpty(GetOrCreateCompileUnitAnchor()),
525 ConstantInt::get(Type::Int32Ty
, LangID
),
526 GetStringConstant(Filename
),
527 GetStringConstant(Directory
),
528 GetStringConstant(Producer
),
529 ConstantInt::get(Type::Int1Ty
, isMain
),
530 ConstantInt::get(Type::Int1Ty
, isOptimized
),
531 GetStringConstant(Flags
),
532 ConstantInt::get(Type::Int32Ty
, RunTimeVer
)
535 Constant
*Init
= ConstantStruct::get(Elts
, sizeof(Elts
)/sizeof(Elts
[0]));
537 M
.addTypeName("llvm.dbg.compile_unit.type", Init
->getType());
538 GlobalVariable
*GV
= new GlobalVariable(Init
->getType(), true,
539 GlobalValue::InternalLinkage
,
540 Init
, "llvm.dbg.compile_unit", &M
);
541 GV
->setSection("llvm.metadata");
542 return DICompileUnit(GV
);
545 /// CreateEnumerator - Create a single enumerator value.
546 DIEnumerator
DIFactory::CreateEnumerator(const std::string
&Name
, uint64_t Val
){
548 GetTagConstant(dwarf::DW_TAG_enumerator
),
549 GetStringConstant(Name
),
550 ConstantInt::get(Type::Int64Ty
, Val
)
553 Constant
*Init
= ConstantStruct::get(Elts
, sizeof(Elts
)/sizeof(Elts
[0]));
555 M
.addTypeName("llvm.dbg.enumerator.type", Init
->getType());
556 GlobalVariable
*GV
= new GlobalVariable(Init
->getType(), true,
557 GlobalValue::InternalLinkage
,
558 Init
, "llvm.dbg.enumerator", &M
);
559 GV
->setSection("llvm.metadata");
560 return DIEnumerator(GV
);
564 /// CreateBasicType - Create a basic type like int, float, etc.
565 DIBasicType
DIFactory::CreateBasicType(DIDescriptor Context
,
566 const std::string
&Name
,
567 DICompileUnit CompileUnit
,
570 uint64_t AlignInBits
,
571 uint64_t OffsetInBits
, unsigned Flags
,
574 GetTagConstant(dwarf::DW_TAG_base_type
),
575 getCastToEmpty(Context
),
576 GetStringConstant(Name
),
577 getCastToEmpty(CompileUnit
),
578 ConstantInt::get(Type::Int32Ty
, LineNumber
),
579 ConstantInt::get(Type::Int64Ty
, SizeInBits
),
580 ConstantInt::get(Type::Int64Ty
, AlignInBits
),
581 ConstantInt::get(Type::Int64Ty
, OffsetInBits
),
582 ConstantInt::get(Type::Int32Ty
, Flags
),
583 ConstantInt::get(Type::Int32Ty
, Encoding
)
586 Constant
*Init
= ConstantStruct::get(Elts
, sizeof(Elts
)/sizeof(Elts
[0]));
588 M
.addTypeName("llvm.dbg.basictype.type", Init
->getType());
589 GlobalVariable
*GV
= new GlobalVariable(Init
->getType(), true,
590 GlobalValue::InternalLinkage
,
591 Init
, "llvm.dbg.basictype", &M
);
592 GV
->setSection("llvm.metadata");
593 return DIBasicType(GV
);
596 /// CreateDerivedType - Create a derived type like const qualified type,
597 /// pointer, typedef, etc.
598 DIDerivedType
DIFactory::CreateDerivedType(unsigned Tag
,
599 DIDescriptor Context
,
600 const std::string
&Name
,
601 DICompileUnit CompileUnit
,
604 uint64_t AlignInBits
,
605 uint64_t OffsetInBits
,
607 DIType DerivedFrom
) {
610 getCastToEmpty(Context
),
611 GetStringConstant(Name
),
612 getCastToEmpty(CompileUnit
),
613 ConstantInt::get(Type::Int32Ty
, LineNumber
),
614 ConstantInt::get(Type::Int64Ty
, SizeInBits
),
615 ConstantInt::get(Type::Int64Ty
, AlignInBits
),
616 ConstantInt::get(Type::Int64Ty
, OffsetInBits
),
617 ConstantInt::get(Type::Int32Ty
, Flags
),
618 getCastToEmpty(DerivedFrom
)
621 Constant
*Init
= ConstantStruct::get(Elts
, sizeof(Elts
)/sizeof(Elts
[0]));
623 M
.addTypeName("llvm.dbg.derivedtype.type", Init
->getType());
624 GlobalVariable
*GV
= new GlobalVariable(Init
->getType(), true,
625 GlobalValue::InternalLinkage
,
626 Init
, "llvm.dbg.derivedtype", &M
);
627 GV
->setSection("llvm.metadata");
628 return DIDerivedType(GV
);
631 /// CreateCompositeType - Create a composite type like array, struct, etc.
632 DICompositeType
DIFactory::CreateCompositeType(unsigned Tag
,
633 DIDescriptor Context
,
634 const std::string
&Name
,
635 DICompileUnit CompileUnit
,
638 uint64_t AlignInBits
,
639 uint64_t OffsetInBits
,
643 unsigned RuntimeLang
) {
647 getCastToEmpty(Context
),
648 GetStringConstant(Name
),
649 getCastToEmpty(CompileUnit
),
650 ConstantInt::get(Type::Int32Ty
, LineNumber
),
651 ConstantInt::get(Type::Int64Ty
, SizeInBits
),
652 ConstantInt::get(Type::Int64Ty
, AlignInBits
),
653 ConstantInt::get(Type::Int64Ty
, OffsetInBits
),
654 ConstantInt::get(Type::Int32Ty
, Flags
),
655 getCastToEmpty(DerivedFrom
),
656 getCastToEmpty(Elements
),
657 ConstantInt::get(Type::Int32Ty
, RuntimeLang
)
660 Constant
*Init
= ConstantStruct::get(Elts
, sizeof(Elts
)/sizeof(Elts
[0]));
662 M
.addTypeName("llvm.dbg.composite.type", Init
->getType());
663 GlobalVariable
*GV
= new GlobalVariable(Init
->getType(), true,
664 GlobalValue::InternalLinkage
,
665 Init
, "llvm.dbg.composite", &M
);
666 GV
->setSection("llvm.metadata");
667 return DICompositeType(GV
);
671 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
672 /// See comments in DISubprogram for descriptions of these fields. This
673 /// method does not unique the generated descriptors.
674 DISubprogram
DIFactory::CreateSubprogram(DIDescriptor Context
,
675 const std::string
&Name
,
676 const std::string
&DisplayName
,
677 const std::string
&LinkageName
,
678 DICompileUnit CompileUnit
,
679 unsigned LineNo
, DIType Type
,
684 GetTagConstant(dwarf::DW_TAG_subprogram
),
685 getCastToEmpty(GetOrCreateSubprogramAnchor()),
686 getCastToEmpty(Context
),
687 GetStringConstant(Name
),
688 GetStringConstant(DisplayName
),
689 GetStringConstant(LinkageName
),
690 getCastToEmpty(CompileUnit
),
691 ConstantInt::get(Type::Int32Ty
, LineNo
),
692 getCastToEmpty(Type
),
693 ConstantInt::get(Type::Int1Ty
, isLocalToUnit
),
694 ConstantInt::get(Type::Int1Ty
, isDefinition
)
697 Constant
*Init
= ConstantStruct::get(Elts
, sizeof(Elts
)/sizeof(Elts
[0]));
699 M
.addTypeName("llvm.dbg.subprogram.type", Init
->getType());
700 GlobalVariable
*GV
= new GlobalVariable(Init
->getType(), true,
701 GlobalValue::InternalLinkage
,
702 Init
, "llvm.dbg.subprogram", &M
);
703 GV
->setSection("llvm.metadata");
704 return DISubprogram(GV
);
707 /// CreateGlobalVariable - Create a new descriptor for the specified global.
709 DIFactory::CreateGlobalVariable(DIDescriptor Context
, const std::string
&Name
,
710 const std::string
&DisplayName
,
711 const std::string
&LinkageName
,
712 DICompileUnit CompileUnit
,
713 unsigned LineNo
, DIType Type
,bool isLocalToUnit
,
714 bool isDefinition
, llvm::GlobalVariable
*Val
) {
716 GetTagConstant(dwarf::DW_TAG_variable
),
717 getCastToEmpty(GetOrCreateGlobalVariableAnchor()),
718 getCastToEmpty(Context
),
719 GetStringConstant(Name
),
720 GetStringConstant(DisplayName
),
721 GetStringConstant(LinkageName
),
722 getCastToEmpty(CompileUnit
),
723 ConstantInt::get(Type::Int32Ty
, LineNo
),
724 getCastToEmpty(Type
),
725 ConstantInt::get(Type::Int1Ty
, isLocalToUnit
),
726 ConstantInt::get(Type::Int1Ty
, isDefinition
),
727 ConstantExpr::getBitCast(Val
, EmptyStructPtr
)
730 Constant
*Init
= ConstantStruct::get(Elts
, sizeof(Elts
)/sizeof(Elts
[0]));
732 M
.addTypeName("llvm.dbg.global_variable.type", Init
->getType());
733 GlobalVariable
*GV
= new GlobalVariable(Init
->getType(), true,
734 GlobalValue::InternalLinkage
,
735 Init
, "llvm.dbg.global_variable", &M
);
736 GV
->setSection("llvm.metadata");
737 return DIGlobalVariable(GV
);
741 /// CreateVariable - Create a new descriptor for the specified variable.
742 DIVariable
DIFactory::CreateVariable(unsigned Tag
, DIDescriptor Context
,
743 const std::string
&Name
,
744 DICompileUnit CompileUnit
, unsigned LineNo
,
748 getCastToEmpty(Context
),
749 GetStringConstant(Name
),
750 getCastToEmpty(CompileUnit
),
751 ConstantInt::get(Type::Int32Ty
, LineNo
),
755 Constant
*Init
= ConstantStruct::get(Elts
, sizeof(Elts
)/sizeof(Elts
[0]));
757 M
.addTypeName("llvm.dbg.variable.type", Init
->getType());
758 GlobalVariable
*GV
= new GlobalVariable(Init
->getType(), true,
759 GlobalValue::InternalLinkage
,
760 Init
, "llvm.dbg.variable", &M
);
761 GV
->setSection("llvm.metadata");
762 return DIVariable(GV
);
766 /// CreateBlock - This creates a descriptor for a lexical block with the
767 /// specified parent context.
768 DIBlock
DIFactory::CreateBlock(DIDescriptor Context
) {
770 GetTagConstant(dwarf::DW_TAG_lexical_block
),
771 getCastToEmpty(Context
)
774 Constant
*Init
= ConstantStruct::get(Elts
, sizeof(Elts
)/sizeof(Elts
[0]));
776 M
.addTypeName("llvm.dbg.block.type", Init
->getType());
777 GlobalVariable
*GV
= new GlobalVariable(Init
->getType(), true,
778 GlobalValue::InternalLinkage
,
779 Init
, "llvm.dbg.block", &M
);
780 GV
->setSection("llvm.metadata");
785 //===----------------------------------------------------------------------===//
786 // DIFactory: Routines for inserting code into a function
787 //===----------------------------------------------------------------------===//
789 /// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
790 /// inserting it at the end of the specified basic block.
791 void DIFactory::InsertStopPoint(DICompileUnit CU
, unsigned LineNo
,
792 unsigned ColNo
, BasicBlock
*BB
) {
794 // Lazily construct llvm.dbg.stoppoint function.
796 StopPointFn
= llvm::Intrinsic::getDeclaration(&M
,
797 llvm::Intrinsic::dbg_stoppoint
);
799 // Invoke llvm.dbg.stoppoint
801 llvm::ConstantInt::get(llvm::Type::Int32Ty
, LineNo
),
802 llvm::ConstantInt::get(llvm::Type::Int32Ty
, ColNo
),
805 CallInst::Create(StopPointFn
, Args
, Args
+3, "", BB
);
808 /// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
809 /// mark the start of the specified subprogram.
810 void DIFactory::InsertSubprogramStart(DISubprogram SP
, BasicBlock
*BB
) {
811 // Lazily construct llvm.dbg.func.start.
813 FuncStartFn
= llvm::Intrinsic::getDeclaration(&M
,
814 llvm::Intrinsic::dbg_func_start
);
816 // Call llvm.dbg.func.start which also implicitly sets a stoppoint.
817 CallInst::Create(FuncStartFn
, getCastToEmpty(SP
), "", BB
);
820 /// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
821 /// mark the start of a region for the specified scoping descriptor.
822 void DIFactory::InsertRegionStart(DIDescriptor D
, BasicBlock
*BB
) {
823 // Lazily construct llvm.dbg.region.start function.
825 RegionStartFn
= llvm::Intrinsic::getDeclaration(&M
,
826 llvm::Intrinsic::dbg_region_start
);
827 // Call llvm.dbg.func.start.
828 CallInst::Create(RegionStartFn
, getCastToEmpty(D
), "", BB
);
832 /// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
833 /// mark the end of a region for the specified scoping descriptor.
834 void DIFactory::InsertRegionEnd(DIDescriptor D
, BasicBlock
*BB
) {
835 // Lazily construct llvm.dbg.region.end function.
837 RegionEndFn
= llvm::Intrinsic::getDeclaration(&M
,
838 llvm::Intrinsic::dbg_region_end
);
840 CallInst::Create(RegionEndFn
, getCastToEmpty(D
), "", BB
);
843 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
844 void DIFactory::InsertDeclare(llvm::Value
*Storage
, DIVariable D
,
846 // Cast the storage to a {}* for the call to llvm.dbg.declare.
847 Storage
= new llvm::BitCastInst(Storage
, EmptyStructPtr
, "", BB
);
850 DeclareFn
= llvm::Intrinsic::getDeclaration(&M
,
851 llvm::Intrinsic::dbg_declare
);
852 Value
*Args
[] = { Storage
, getCastToEmpty(D
) };
853 CallInst::Create(DeclareFn
, Args
, Args
+2, "", BB
);
857 /// Finds the stoppoint coressponding to this instruction, that is the
858 /// stoppoint that dominates this instruction
859 const DbgStopPointInst
*findStopPoint(const Instruction
*Inst
)
861 if (const DbgStopPointInst
*DSI
= dyn_cast
<DbgStopPointInst
>(Inst
))
864 const BasicBlock
*BB
= Inst
->getParent();
865 BasicBlock::const_iterator I
= Inst
, B
;
868 // A BB consisting only of a terminator can't have a stoppoint.
872 if (const DbgStopPointInst
*DSI
= dyn_cast
<DbgStopPointInst
>(I
))
876 // This BB didn't have a stoppoint: if there is only one
877 // predecessor, look for a stoppoint there.
878 // We could use getIDom(), but that would require dominator info.
879 BB
= I
->getParent()->getUniquePredecessor();
881 I
= BB
->getTerminator();
886 /// Finds the stoppoint corresponding to first real (non-debug intrinsic)
887 /// instruction in this Basic Block, and returns the stoppoint for it.
888 const DbgStopPointInst
*findBBStopPoint(const BasicBlock
*BB
)
890 for(BasicBlock::const_iterator I
= BB
->begin(), E
= BB
->end(); I
!= E
; ++I
) {
891 if (const DbgStopPointInst
*DSI
= dyn_cast
<DbgStopPointInst
>(I
))
894 // Fallback to looking for stoppoint of unique predecessor.
895 // Useful if this BB contains no stoppoints, but unique predecessor does.
896 BB
= BB
->getUniquePredecessor();
898 return findStopPoint(BB
->getTerminator());
902 Value
*findDbgGlobalDeclare(GlobalVariable
*V
)
904 const Module
*M
= V
->getParent();
905 const Type
*Ty
= M
->getTypeByName("llvm.dbg.global_variable.type");
908 Ty
= PointerType::get(Ty
, 0);
910 Value
*Val
= V
->stripPointerCasts();
911 for (Value::use_iterator I
= Val
->use_begin(), E
=Val
->use_end();
913 if (ConstantExpr
*CE
= dyn_cast
<ConstantExpr
>(I
)) {
914 if (CE
->getOpcode() == Instruction::BitCast
) {
916 while (VV
->hasOneUse()) {
917 VV
= *VV
->use_begin();
919 if (VV
->getType() == Ty
)
925 if (Val
->getType() == Ty
)
930 /// Finds the dbg.declare intrinsic corresponding to this value if any.
931 /// It looks through pointer casts too.
932 const DbgDeclareInst
*findDbgDeclare(const Value
*V
, bool stripCasts
)
935 V
= V
->stripPointerCasts();
936 // Look for the bitcast.
937 for (Value::use_const_iterator I
= V
->use_begin(), E
=V
->use_end();
939 if (isa
<BitCastInst
>(I
))
940 return findDbgDeclare(*I
, false);
945 // Find dbg.declare among uses of the instruction.
946 for (Value::use_const_iterator I
= V
->use_begin(), E
=V
->use_end();
948 if (const DbgDeclareInst
*DDI
= dyn_cast
<DbgDeclareInst
>(I
))
954 bool getLocationInfo(const Value
*V
, std::string
&DisplayName
, std::string
&Type
,
955 unsigned &LineNo
, std::string
&File
, std::string
&Dir
)
959 if (GlobalVariable
*GV
= dyn_cast
<GlobalVariable
>(const_cast<Value
*>(V
))) {
960 Value
*DIGV
= findDbgGlobalDeclare(GV
);
963 DIGlobalVariable
Var(cast
<GlobalVariable
>(DIGV
));
964 Var
.getDisplayName(DisplayName
);
965 LineNo
= Var
.getLineNumber();
966 Unit
= Var
.getCompileUnit();
967 TypeD
= Var
.getType();
969 const DbgDeclareInst
*DDI
= findDbgDeclare(V
);
972 DIVariable
Var(cast
<GlobalVariable
>(DDI
->getVariable()));
973 Var
.getName(DisplayName
);
974 LineNo
= Var
.getLineNumber();
975 Unit
= Var
.getCompileUnit();
976 TypeD
= Var
.getType();
979 Unit
.getFilename(File
);
980 Unit
.getDirectory(Dir
);
985 /// dump - print compile unit.
986 void DICompileUnit::dump() const {
988 cerr
<< " [" << dwarf::LanguageString(getLanguage()) << "] ";
990 std::string Res1
, Res2
;
991 cerr
<< " [" << getDirectory(Res1
) << "/" << getFilename(Res2
) << " ]";
994 /// dump - print type.
995 void DIType::dump() const {
996 if (isNull()) return;
999 if (!getName(Res
).empty())
1000 cerr
<< " [" << Res
<< "] ";
1002 unsigned Tag
= getTag();
1003 cerr
<< " [" << dwarf::TagString(Tag
) << "] ";
1005 // TODO : Print context
1006 getCompileUnit().dump();
1008 << getLineNumber() << ", "
1009 << getSizeInBits() << ", "
1010 << getAlignInBits() << ", "
1011 << getOffsetInBits()
1015 cerr
<< " [private] ";
1016 else if (isProtected())
1017 cerr
<< " [protected] ";
1019 if (isForwardDecl())
1022 if (isBasicType(Tag
))
1023 DIBasicType(GV
).dump();
1024 else if (isDerivedType(Tag
))
1025 DIDerivedType(GV
).dump();
1026 else if (isCompositeType(Tag
))
1027 DICompositeType(GV
).dump();
1029 cerr
<< "Invalid DIType\n";
1036 /// dump - print basic type.
1037 void DIBasicType::dump() const {
1038 cerr
<< " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
1042 /// dump - print derived type.
1043 void DIDerivedType::dump() const {
1044 cerr
<< "\n\t Derived From: "; getTypeDerivedFrom().dump();
1047 /// dump - print composite type.
1048 void DICompositeType::dump() const {
1049 DIArray A
= getTypeArray();
1052 cerr
<< " [" << A
.getNumElements() << " elements]";
1055 /// dump - print global.
1056 void DIGlobal::dump() const {
1058 if (!getName(Res
).empty())
1059 cerr
<< " [" << Res
<< "] ";
1061 unsigned Tag
= getTag();
1062 cerr
<< " [" << dwarf::TagString(Tag
) << "] ";
1064 // TODO : Print context
1065 getCompileUnit().dump();
1066 cerr
<< " [" << getLineNumber() << "] ";
1068 if (isLocalToUnit())
1069 cerr
<< " [local] ";
1074 if (isGlobalVariable(Tag
))
1075 DIGlobalVariable(GV
).dump();
1080 /// dump - print subprogram.
1081 void DISubprogram::dump() const {
1085 /// dump - print global variable.
1086 void DIGlobalVariable::dump() const {
1087 cerr
<< " ["; getGlobal()->dump(); cerr
<< "] ";
1090 /// dump - print variable.
1091 void DIVariable::dump() const {
1093 if (!getName(Res
).empty())
1094 cerr
<< " [" << Res
<< "] ";
1096 getCompileUnit().dump();
1097 cerr
<< " [" << getLineNumber() << "] ";