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"
26 //===----------------------------------------------------------------------===//
28 //===----------------------------------------------------------------------===//
30 DIDescriptor::DIDescriptor(GlobalVariable *gv, unsigned RequiredTag) {
33 // If this is non-null, check to see if the Tag matches. If not, set to null.
34 if (GV && getTag() != RequiredTag)
39 std::string DIDescriptor::getStringField(unsigned Elt) const {
40 if (GV == 0) return "";
41 Constant *C = GV->getInitializer();
42 if (C == 0 || Elt >= C->getNumOperands())
46 // Fills in the string if it succeeds
47 if (!GetConstantStringInfo(C->getOperand(Elt), Result))
52 uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
53 if (GV == 0) return 0;
54 Constant *C = GV->getInitializer();
55 if (C == 0 || Elt >= C->getNumOperands())
57 if (ConstantInt *CI = dyn_cast<ConstantInt>(C->getOperand(Elt)))
58 return CI->getZExtValue();
63 DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
64 if (GV == 0) return DIDescriptor();
65 Constant *C = GV->getInitializer();
66 if (C == 0 || Elt >= C->getNumOperands())
67 return DIDescriptor();
68 C = C->getOperand(Elt);
69 return DIDescriptor(dyn_cast<GlobalVariable>(C->stripPointerCasts()));
72 GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
73 if (GV == 0) return 0;
74 Constant *C = GV->getInitializer();
75 if (C == 0 || Elt >= C->getNumOperands())
77 C = C->getOperand(Elt);
79 return dyn_cast<GlobalVariable>(C->stripPointerCasts());
84 //===----------------------------------------------------------------------===//
85 // Simple Descriptor Constructors and other Methods
86 //===----------------------------------------------------------------------===//
88 DIAnchor::DIAnchor(GlobalVariable *GV)
89 : DIDescriptor(GV, dwarf::DW_TAG_anchor) {}
90 DIEnumerator::DIEnumerator(GlobalVariable *GV)
91 : DIDescriptor(GV, dwarf::DW_TAG_enumerator) {}
92 DISubrange::DISubrange(GlobalVariable *GV)
93 : DIDescriptor(GV, dwarf::DW_TAG_subrange_type) {}
94 DICompileUnit::DICompileUnit(GlobalVariable *GV)
95 : DIDescriptor(GV, dwarf::DW_TAG_compile_unit) {}
96 DIBasicType::DIBasicType(GlobalVariable *GV)
97 : DIType(GV, dwarf::DW_TAG_base_type) {}
98 DISubprogram::DISubprogram(GlobalVariable *GV)
99 : DIGlobal(GV, dwarf::DW_TAG_subprogram) {}
100 DIGlobalVariable::DIGlobalVariable(GlobalVariable *GV)
101 : DIGlobal(GV, dwarf::DW_TAG_variable) {}
102 DIBlock::DIBlock(GlobalVariable *GV)
103 : DIDescriptor(GV, dwarf::DW_TAG_lexical_block) {}
104 // needed by DIVariable::getType()
105 DIType::DIType(GlobalVariable *gv) : DIDescriptor(gv) {
107 unsigned tag = getTag();
108 if (tag != dwarf::DW_TAG_base_type && !DIDerivedType::isDerivedType(tag) &&
109 !DICompositeType::isCompositeType(tag))
113 /// isDerivedType - Return true if the specified tag is legal for
115 bool DIDerivedType::isDerivedType(unsigned Tag) {
117 case dwarf::DW_TAG_typedef:
118 case dwarf::DW_TAG_pointer_type:
119 case dwarf::DW_TAG_reference_type:
120 case dwarf::DW_TAG_const_type:
121 case dwarf::DW_TAG_volatile_type:
122 case dwarf::DW_TAG_restrict_type:
123 case dwarf::DW_TAG_member:
124 case dwarf::DW_TAG_inheritance:
127 // FIXME: Even though it doesn't make sense, CompositeTypes are current
128 // modelled as DerivedTypes, this should return true for them as well.
133 DIDerivedType::DIDerivedType(GlobalVariable *GV) : DIType(GV, true, true) {
134 if (GV && !isDerivedType(getTag()))
138 /// isCompositeType - Return true if the specified tag is legal for
140 bool DICompositeType::isCompositeType(unsigned TAG) {
142 case dwarf::DW_TAG_array_type:
143 case dwarf::DW_TAG_structure_type:
144 case dwarf::DW_TAG_union_type:
145 case dwarf::DW_TAG_enumeration_type:
146 case dwarf::DW_TAG_vector_type:
147 case dwarf::DW_TAG_subroutine_type:
154 DICompositeType::DICompositeType(GlobalVariable *GV)
155 : DIDerivedType(GV, true, true) {
156 if (GV && !isCompositeType(getTag()))
160 /// isVariable - Return true if the specified tag is legal for DIVariable.
161 bool DIVariable::isVariable(unsigned Tag) {
163 case dwarf::DW_TAG_auto_variable:
164 case dwarf::DW_TAG_arg_variable:
165 case dwarf::DW_TAG_return_variable:
172 DIVariable::DIVariable(GlobalVariable *GV) : DIDescriptor(GV) {
173 if (GV && !isVariable(getTag()))
179 //===----------------------------------------------------------------------===//
180 // DIFactory: Basic Helpers
181 //===----------------------------------------------------------------------===//
183 DIFactory::DIFactory(Module &m) : M(m) {
184 StopPointFn = FuncStartFn = RegionStartFn = RegionEndFn = DeclareFn = 0;
185 EmptyStructPtr = PointerType::getUnqual(StructType::get(NULL, NULL));
188 /// getCastToEmpty - Return this descriptor as a Constant* with type '{}*'.
189 /// This is only valid when the descriptor is non-null.
190 Constant *DIFactory::getCastToEmpty(DIDescriptor D) {
191 if (D.isNull()) return Constant::getNullValue(EmptyStructPtr);
192 return ConstantExpr::getBitCast(D.getGV(), EmptyStructPtr);
195 Constant *DIFactory::GetTagConstant(unsigned TAG) {
196 assert((TAG & DIDescriptor::VersionMask) == 0 &&
197 "Tag too large for debug encoding!");
198 return ConstantInt::get(Type::Int32Ty, TAG | DIDescriptor::Version7);
201 Constant *DIFactory::GetStringConstant(const std::string &String) {
202 // Check string cache for previous edition.
203 Constant *&Slot = StringCache[String];
205 // Return Constant if previously defined.
206 if (Slot) return Slot;
208 const PointerType *DestTy = PointerType::getUnqual(Type::Int8Ty);
210 // If empty string then use a sbyte* null instead.
212 return Slot = ConstantPointerNull::get(DestTy);
214 // Construct string as an llvm constant.
215 Constant *ConstStr = ConstantArray::get(String);
217 // Otherwise create and return a new string global.
218 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
219 GlobalVariable::InternalLinkage,
220 ConstStr, ".str", &M);
221 StrGV->setSection("llvm.metadata");
222 return Slot = ConstantExpr::getBitCast(StrGV, DestTy);
225 /// GetOrCreateAnchor - Look up an anchor for the specified tag and name. If it
226 /// already exists, return it. If not, create a new one and return it.
227 DIAnchor DIFactory::GetOrCreateAnchor(unsigned TAG, const char *Name) {
228 const Type *EltTy = StructType::get(Type::Int32Ty, Type::Int32Ty, NULL);
230 // Otherwise, create the global or return it if already in the module.
231 Constant *C = M.getOrInsertGlobal(Name, EltTy);
232 assert(isa<GlobalVariable>(C) && "Incorrectly typed anchor?");
233 GlobalVariable *GV = cast<GlobalVariable>(C);
235 // If it has an initializer, it is already in the module.
236 if (GV->hasInitializer())
237 return SubProgramAnchor = DIAnchor(GV);
239 GV->setLinkage(GlobalValue::LinkOnceLinkage);
240 GV->setSection("llvm.metadata");
241 GV->setConstant(true);
242 M.addTypeName("llvm.dbg.anchor.type", EltTy);
244 // Otherwise, set the initializer.
246 GetTagConstant(dwarf::DW_TAG_anchor),
247 ConstantInt::get(Type::Int32Ty, TAG)
250 GV->setInitializer(ConstantStruct::get(Elts, 2));
256 //===----------------------------------------------------------------------===//
257 // DIFactory: Primary Constructors
258 //===----------------------------------------------------------------------===//
260 /// GetOrCreateCompileUnitAnchor - Return the anchor for compile units,
261 /// creating a new one if there isn't already one in the module.
262 DIAnchor DIFactory::GetOrCreateCompileUnitAnchor() {
263 // If we already created one, just return it.
264 if (!CompileUnitAnchor.isNull())
265 return CompileUnitAnchor;
266 return CompileUnitAnchor = GetOrCreateAnchor(dwarf::DW_TAG_compile_unit,
267 "llvm.dbg.compile_units");
270 /// GetOrCreateSubprogramAnchor - Return the anchor for subprograms,
271 /// creating a new one if there isn't already one in the module.
272 DIAnchor DIFactory::GetOrCreateSubprogramAnchor() {
273 // If we already created one, just return it.
274 if (!SubProgramAnchor.isNull())
275 return SubProgramAnchor;
276 return SubProgramAnchor = GetOrCreateAnchor(dwarf::DW_TAG_subprogram,
277 "llvm.dbg.subprograms");
280 /// GetOrCreateGlobalVariableAnchor - Return the anchor for globals,
281 /// creating a new one if there isn't already one in the module.
282 DIAnchor DIFactory::GetOrCreateGlobalVariableAnchor() {
283 // If we already created one, just return it.
284 if (!GlobalVariableAnchor.isNull())
285 return GlobalVariableAnchor;
286 return GlobalVariableAnchor = GetOrCreateAnchor(dwarf::DW_TAG_variable,
287 "llvm.dbg.global_variables");
290 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
291 /// This implicitly uniques the arrays created.
292 DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
293 SmallVector<Constant*, 16> Elts;
295 for (unsigned i = 0; i != NumTys; ++i)
296 Elts.push_back(getCastToEmpty(Tys[i]));
298 Constant *Init = ConstantArray::get(ArrayType::get(EmptyStructPtr,
300 &Elts[0], Elts.size());
301 // If we already have this array, just return the uniqued version.
302 DIDescriptor &Entry = SimpleConstantCache[Init];
303 if (!Entry.isNull()) return DIArray(Entry.getGV());
305 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
306 GlobalValue::InternalLinkage,
307 Init, "llvm.dbg.array", &M);
308 GV->setSection("llvm.metadata");
309 Entry = DIDescriptor(GV);
313 /// GetOrCreateSubrange - Create a descriptor for a value range. This
314 /// implicitly uniques the values returned.
315 DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
317 GetTagConstant(dwarf::DW_TAG_subrange_type),
318 ConstantInt::get(Type::Int64Ty, Lo),
319 ConstantInt::get(Type::Int64Ty, Hi)
322 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
324 // If we already have this range, just return the uniqued version.
325 DIDescriptor &Entry = SimpleConstantCache[Init];
326 if (!Entry.isNull()) return DISubrange(Entry.getGV());
328 M.addTypeName("llvm.dbg.subrange.type", Init->getType());
330 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
331 GlobalValue::InternalLinkage,
332 Init, "llvm.dbg.subrange", &M);
333 GV->setSection("llvm.metadata");
334 Entry = DIDescriptor(GV);
335 return DISubrange(GV);
340 /// CreateCompileUnit - Create a new descriptor for the specified compile
341 /// unit. Note that this does not unique compile units within the module.
342 DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
343 const std::string &Filename,
344 const std::string &Directory,
345 const std::string &Producer) {
347 GetTagConstant(dwarf::DW_TAG_compile_unit),
348 getCastToEmpty(GetOrCreateCompileUnitAnchor()),
349 ConstantInt::get(Type::Int32Ty, LangID),
350 GetStringConstant(Filename),
351 GetStringConstant(Directory),
352 GetStringConstant(Producer)
355 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
357 M.addTypeName("llvm.dbg.compile_unit.type", Init->getType());
358 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
359 GlobalValue::InternalLinkage,
360 Init, "llvm.dbg.compile_unit", &M);
361 GV->setSection("llvm.metadata");
362 return DICompileUnit(GV);
365 /// CreateEnumerator - Create a single enumerator value.
366 DIEnumerator DIFactory::CreateEnumerator(const std::string &Name, uint64_t Val){
368 GetTagConstant(dwarf::DW_TAG_enumerator),
369 GetStringConstant(Name),
370 ConstantInt::get(Type::Int64Ty, Val)
373 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
375 M.addTypeName("llvm.dbg.enumerator.type", Init->getType());
376 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
377 GlobalValue::InternalLinkage,
378 Init, "llvm.dbg.enumerator", &M);
379 GV->setSection("llvm.metadata");
380 return DIEnumerator(GV);
384 /// CreateBasicType - Create a basic type like int, float, etc.
385 DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
386 const std::string &Name,
387 DICompileUnit CompileUnit,
390 uint64_t AlignInBits,
391 uint64_t OffsetInBits, unsigned Flags,
393 const std::string *FileName,
394 const std::string *Directory) {
396 GetTagConstant(dwarf::DW_TAG_base_type),
397 getCastToEmpty(Context),
398 GetStringConstant(Name),
399 getCastToEmpty(CompileUnit),
400 ConstantInt::get(Type::Int32Ty, LineNumber),
401 ConstantInt::get(Type::Int64Ty, SizeInBits),
402 ConstantInt::get(Type::Int64Ty, AlignInBits),
403 ConstantInt::get(Type::Int64Ty, OffsetInBits),
404 ConstantInt::get(Type::Int32Ty, Flags),
405 ConstantInt::get(Type::Int32Ty, Encoding),
406 GetStringConstant(FileName ? FileName->c_str() : ""),
407 GetStringConstant(Directory ? Directory->c_str() : "")
410 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
412 M.addTypeName("llvm.dbg.basictype.type", Init->getType());
413 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
414 GlobalValue::InternalLinkage,
415 Init, "llvm.dbg.basictype", &M);
416 GV->setSection("llvm.metadata");
417 return DIBasicType(GV);
420 /// CreateDerivedType - Create a derived type like const qualified type,
421 /// pointer, typedef, etc.
422 DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
423 DIDescriptor Context,
424 const std::string &Name,
425 DICompileUnit CompileUnit,
428 uint64_t AlignInBits,
429 uint64_t OffsetInBits,
432 const std::string *FileName,
433 const std::string *Directory) {
436 getCastToEmpty(Context),
437 GetStringConstant(Name),
438 getCastToEmpty(CompileUnit),
439 ConstantInt::get(Type::Int32Ty, LineNumber),
440 ConstantInt::get(Type::Int64Ty, SizeInBits),
441 ConstantInt::get(Type::Int64Ty, AlignInBits),
442 ConstantInt::get(Type::Int64Ty, OffsetInBits),
443 ConstantInt::get(Type::Int32Ty, Flags),
444 getCastToEmpty(DerivedFrom),
445 GetStringConstant(FileName ? FileName->c_str() : ""),
446 GetStringConstant(Directory ? Directory->c_str() : "")
449 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
451 M.addTypeName("llvm.dbg.derivedtype.type", Init->getType());
452 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
453 GlobalValue::InternalLinkage,
454 Init, "llvm.dbg.derivedtype", &M);
455 GV->setSection("llvm.metadata");
456 return DIDerivedType(GV);
459 /// CreateCompositeType - Create a composite type like array, struct, etc.
460 DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
461 DIDescriptor Context,
462 const std::string &Name,
463 DICompileUnit CompileUnit,
466 uint64_t AlignInBits,
467 uint64_t OffsetInBits,
471 const std::string *FileName,
472 const std::string *Directory) {
476 getCastToEmpty(Context),
477 GetStringConstant(Name),
478 getCastToEmpty(CompileUnit),
479 ConstantInt::get(Type::Int32Ty, LineNumber),
480 ConstantInt::get(Type::Int64Ty, SizeInBits),
481 ConstantInt::get(Type::Int64Ty, AlignInBits),
482 ConstantInt::get(Type::Int64Ty, OffsetInBits),
483 ConstantInt::get(Type::Int32Ty, Flags),
484 getCastToEmpty(DerivedFrom),
485 getCastToEmpty(Elements),
486 GetStringConstant(FileName ? FileName->c_str() : ""),
487 GetStringConstant(Directory ? Directory->c_str() : "")
490 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
492 M.addTypeName("llvm.dbg.composite.type", Init->getType());
493 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
494 GlobalValue::InternalLinkage,
495 Init, "llvm.dbg.composite", &M);
496 GV->setSection("llvm.metadata");
497 return DICompositeType(GV);
501 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
502 /// See comments in DISubprogram for descriptions of these fields. This
503 /// method does not unique the generated descriptors.
504 DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
505 const std::string &Name,
506 const std::string &DisplayName,
507 const std::string &LinkageName,
508 DICompileUnit CompileUnit,
509 unsigned LineNo, DIType Type,
512 const std::string *FileName,
513 const std::string *Directory) {
516 GetTagConstant(dwarf::DW_TAG_subprogram),
517 getCastToEmpty(GetOrCreateSubprogramAnchor()),
518 getCastToEmpty(Context),
519 GetStringConstant(Name),
520 GetStringConstant(DisplayName),
521 GetStringConstant(LinkageName),
522 getCastToEmpty(CompileUnit),
523 ConstantInt::get(Type::Int32Ty, LineNo),
524 getCastToEmpty(Type),
525 ConstantInt::get(Type::Int1Ty, isLocalToUnit),
526 ConstantInt::get(Type::Int1Ty, isDefinition),
527 GetStringConstant(FileName ? FileName->c_str() : ""),
528 GetStringConstant(Directory ? Directory->c_str() : "")
531 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
533 M.addTypeName("llvm.dbg.subprogram.type", Init->getType());
534 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
535 GlobalValue::InternalLinkage,
536 Init, "llvm.dbg.subprogram", &M);
537 GV->setSection("llvm.metadata");
538 return DISubprogram(GV);
541 /// CreateGlobalVariable - Create a new descriptor for the specified global.
543 DIFactory::CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
544 const std::string &DisplayName,
545 const std::string &LinkageName,
546 DICompileUnit CompileUnit,
547 unsigned LineNo, DIType Type,bool isLocalToUnit,
548 bool isDefinition, llvm::GlobalVariable *Val,
549 const std::string *FileName,
550 const std::string *Directory) {
552 GetTagConstant(dwarf::DW_TAG_variable),
553 getCastToEmpty(GetOrCreateGlobalVariableAnchor()),
554 getCastToEmpty(Context),
555 GetStringConstant(Name),
556 GetStringConstant(DisplayName),
557 GetStringConstant(LinkageName),
558 getCastToEmpty(CompileUnit),
559 ConstantInt::get(Type::Int32Ty, LineNo),
560 getCastToEmpty(Type),
561 ConstantInt::get(Type::Int1Ty, isLocalToUnit),
562 ConstantInt::get(Type::Int1Ty, isDefinition),
563 ConstantExpr::getBitCast(Val, EmptyStructPtr),
564 GetStringConstant(FileName ? FileName->c_str() : ""),
565 GetStringConstant(Directory ? Directory->c_str() : "")
568 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
570 M.addTypeName("llvm.dbg.global_variable.type", Init->getType());
571 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
572 GlobalValue::InternalLinkage,
573 Init, "llvm.dbg.global_variable", &M);
574 GV->setSection("llvm.metadata");
575 return DIGlobalVariable(GV);
579 /// CreateVariable - Create a new descriptor for the specified variable.
580 DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
581 const std::string &Name,
582 DICompileUnit CompileUnit, unsigned LineNo,
584 const std::string *FileName,
585 const std::string *Directory) {
590 getCastToEmpty(Context),
591 GetStringConstant(Name),
592 getCastToEmpty(CompileUnit),
593 ConstantInt::get(Type::Int32Ty, LineNo),
594 getCastToEmpty(Type),
595 GetStringConstant(FileName ? FileName->c_str() : ""),
596 GetStringConstant(Directory ? Directory->c_str() : "")
599 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
601 M.addTypeName("llvm.dbg.variable.type", Init->getType());
602 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
603 GlobalValue::InternalLinkage,
604 Init, "llvm.dbg.variable", &M);
605 GV->setSection("llvm.metadata");
606 return DIVariable(GV);
610 /// CreateBlock - This creates a descriptor for a lexical block with the
611 /// specified parent context.
612 DIBlock DIFactory::CreateBlock(DIDescriptor Context) {
614 GetTagConstant(dwarf::DW_TAG_lexical_block),
615 getCastToEmpty(Context)
618 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
620 M.addTypeName("llvm.dbg.block.type", Init->getType());
621 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
622 GlobalValue::InternalLinkage,
623 Init, "llvm.dbg.block", &M);
624 GV->setSection("llvm.metadata");
629 //===----------------------------------------------------------------------===//
630 // DIFactory: Routines for inserting code into a function
631 //===----------------------------------------------------------------------===//
633 /// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
634 /// inserting it at the end of the specified basic block.
635 void DIFactory::InsertStopPoint(DICompileUnit CU, unsigned LineNo,
636 unsigned ColNo, BasicBlock *BB) {
638 // Lazily construct llvm.dbg.stoppoint function.
640 StopPointFn = llvm::Intrinsic::getDeclaration(&M,
641 llvm::Intrinsic::dbg_stoppoint);
643 // Invoke llvm.dbg.stoppoint
645 llvm::ConstantInt::get(llvm::Type::Int32Ty, LineNo),
646 llvm::ConstantInt::get(llvm::Type::Int32Ty, ColNo),
649 CallInst::Create(StopPointFn, Args, Args+3, "", BB);
652 /// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
653 /// mark the start of the specified subprogram.
654 void DIFactory::InsertSubprogramStart(DISubprogram SP, BasicBlock *BB) {
655 // Lazily construct llvm.dbg.func.start.
657 FuncStartFn = llvm::Intrinsic::getDeclaration(&M,
658 llvm::Intrinsic::dbg_func_start);
660 // Call llvm.dbg.func.start which also implicitly sets a stoppoint.
661 CallInst::Create(FuncStartFn, getCastToEmpty(SP), "", BB);
664 /// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
665 /// mark the start of a region for the specified scoping descriptor.
666 void DIFactory::InsertRegionStart(DIDescriptor D, BasicBlock *BB) {
667 // Lazily construct llvm.dbg.region.start function.
669 RegionStartFn = llvm::Intrinsic::getDeclaration(&M,
670 llvm::Intrinsic::dbg_region_start);
671 // Call llvm.dbg.func.start.
672 CallInst::Create(RegionStartFn, getCastToEmpty(D), "", BB);
676 /// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
677 /// mark the end of a region for the specified scoping descriptor.
678 void DIFactory::InsertRegionEnd(DIDescriptor D, BasicBlock *BB) {
679 // Lazily construct llvm.dbg.region.end function.
681 RegionEndFn = llvm::Intrinsic::getDeclaration(&M,
682 llvm::Intrinsic::dbg_region_end);
684 CallInst::Create(RegionEndFn, getCastToEmpty(D), "", BB);
687 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
688 void DIFactory::InsertDeclare(llvm::Value *Storage, DIVariable D,
690 // Cast the storage to a {}* for the call to llvm.dbg.declare.
691 Storage = new llvm::BitCastInst(Storage, EmptyStructPtr, "", BB);
694 DeclareFn = llvm::Intrinsic::getDeclaration(&M,
695 llvm::Intrinsic::dbg_declare);
696 Value *Args[] = { Storage, getCastToEmpty(D) };
697 CallInst::Create(DeclareFn, Args, Args+2, "", BB);
701 /// Finds the stoppoint coressponding to this instruction, that is the
702 /// stoppoint that dominates this instruction
703 const DbgStopPointInst *findStopPoint(const Instruction *Inst)
705 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(Inst))
708 const BasicBlock *BB = Inst->getParent();
709 BasicBlock::const_iterator I = Inst, B;
712 // A BB consisting only of a terminator can't have a stoppoint.
716 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
720 // This BB didn't have a stoppoint: if there is only one
721 // predecessor, look for a stoppoint there.
722 // We could use getIDom(), but that would require dominator info.
723 BB = I->getParent()->getUniquePredecessor();
725 I = BB->getTerminator();
730 /// Finds the stoppoint corresponding to first real (non-debug intrinsic)
731 /// instruction in this Basic Block, and returns the stoppoint for it.
732 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB)
734 for(BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
735 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
738 // Fallback to looking for stoppoint of unique predecessor.
739 // Useful if this BB contains no stoppoints, but unique predecessor does.
740 BB = BB->getUniquePredecessor();
742 return findStopPoint(BB->getTerminator());
746 /// Finds the dbg.declare intrinsic corresponding to this value if any.
747 /// It looks through pointer casts too.
748 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts)
751 V = V->stripPointerCasts();
752 // Look for the bitcast.
753 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
755 if (isa<BitCastInst>(I))
756 return findDbgDeclare(*I, false);
761 // Find dbg.declare among uses of the instruction.
762 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
764 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I))