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::Version6);
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,
394 GetTagConstant(dwarf::DW_TAG_base_type),
395 getCastToEmpty(Context),
396 GetStringConstant(Name),
397 getCastToEmpty(CompileUnit),
398 ConstantInt::get(Type::Int32Ty, LineNumber),
399 ConstantInt::get(Type::Int64Ty, SizeInBits),
400 ConstantInt::get(Type::Int64Ty, AlignInBits),
401 ConstantInt::get(Type::Int64Ty, OffsetInBits),
402 ConstantInt::get(Type::Int32Ty, Flags),
403 ConstantInt::get(Type::Int32Ty, Encoding)
406 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
408 M.addTypeName("llvm.dbg.basictype.type", Init->getType());
409 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
410 GlobalValue::InternalLinkage,
411 Init, "llvm.dbg.basictype", &M);
412 GV->setSection("llvm.metadata");
413 return DIBasicType(GV);
416 /// CreateDerivedType - Create a derived type like const qualified type,
417 /// pointer, typedef, etc.
418 DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
419 DIDescriptor Context,
420 const std::string &Name,
421 DICompileUnit CompileUnit,
424 uint64_t AlignInBits,
425 uint64_t OffsetInBits,
427 DIType DerivedFrom) {
430 getCastToEmpty(Context),
431 GetStringConstant(Name),
432 getCastToEmpty(CompileUnit),
433 ConstantInt::get(Type::Int32Ty, LineNumber),
434 ConstantInt::get(Type::Int64Ty, SizeInBits),
435 ConstantInt::get(Type::Int64Ty, AlignInBits),
436 ConstantInt::get(Type::Int64Ty, OffsetInBits),
437 ConstantInt::get(Type::Int32Ty, Flags),
438 getCastToEmpty(DerivedFrom)
441 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
443 M.addTypeName("llvm.dbg.derivedtype.type", Init->getType());
444 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
445 GlobalValue::InternalLinkage,
446 Init, "llvm.dbg.derivedtype", &M);
447 GV->setSection("llvm.metadata");
448 return DIDerivedType(GV);
451 /// CreateCompositeType - Create a composite type like array, struct, etc.
452 DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
453 DIDescriptor Context,
454 const std::string &Name,
455 DICompileUnit CompileUnit,
458 uint64_t AlignInBits,
459 uint64_t OffsetInBits,
465 getCastToEmpty(Context),
466 GetStringConstant(Name),
467 getCastToEmpty(CompileUnit),
468 ConstantInt::get(Type::Int32Ty, LineNumber),
469 ConstantInt::get(Type::Int64Ty, SizeInBits),
470 ConstantInt::get(Type::Int64Ty, AlignInBits),
471 ConstantInt::get(Type::Int64Ty, OffsetInBits),
472 ConstantInt::get(Type::Int32Ty, Flags),
473 getCastToEmpty(DerivedFrom),
474 getCastToEmpty(Elements)
477 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
479 M.addTypeName("llvm.dbg.composite.type", Init->getType());
480 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
481 GlobalValue::InternalLinkage,
482 Init, "llvm.dbg.composite", &M);
483 GV->setSection("llvm.metadata");
484 return DICompositeType(GV);
488 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
489 /// See comments in DISubprogram for descriptions of these fields. This
490 /// method does not unique the generated descriptors.
491 DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
492 const std::string &Name,
493 const std::string &DisplayName,
494 const std::string &LinkageName,
495 DICompileUnit CompileUnit,
496 unsigned LineNo, DIType Type,
500 GetTagConstant(dwarf::DW_TAG_subprogram),
501 getCastToEmpty(GetOrCreateSubprogramAnchor()),
502 getCastToEmpty(Context),
503 GetStringConstant(Name),
504 GetStringConstant(DisplayName),
505 GetStringConstant(LinkageName),
506 getCastToEmpty(CompileUnit),
507 ConstantInt::get(Type::Int32Ty, LineNo),
508 getCastToEmpty(Type),
509 ConstantInt::get(Type::Int1Ty, isLocalToUnit),
510 ConstantInt::get(Type::Int1Ty, isDefinition)
513 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
515 M.addTypeName("llvm.dbg.subprogram.type", Init->getType());
516 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
517 GlobalValue::InternalLinkage,
518 Init, "llvm.dbg.subprogram", &M);
519 GV->setSection("llvm.metadata");
520 return DISubprogram(GV);
523 /// CreateGlobalVariable - Create a new descriptor for the specified global.
525 DIFactory::CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
526 const std::string &DisplayName,
527 const std::string &LinkageName,
528 DICompileUnit CompileUnit,
529 unsigned LineNo, DIType Type,bool isLocalToUnit,
530 bool isDefinition, llvm::GlobalVariable *Val) {
533 GetTagConstant(dwarf::DW_TAG_variable),
534 getCastToEmpty(GetOrCreateGlobalVariableAnchor()),
535 getCastToEmpty(Context),
536 GetStringConstant(Name),
537 GetStringConstant(DisplayName),
538 GetStringConstant(LinkageName),
539 getCastToEmpty(CompileUnit),
540 ConstantInt::get(Type::Int32Ty, LineNo),
541 getCastToEmpty(Type),
542 ConstantInt::get(Type::Int1Ty, isLocalToUnit),
543 ConstantInt::get(Type::Int1Ty, isDefinition),
544 ConstantExpr::getBitCast(Val, EmptyStructPtr)
547 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
549 M.addTypeName("llvm.dbg.global_variable.type", Init->getType());
550 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
551 GlobalValue::InternalLinkage,
552 Init, "llvm.dbg.global_variable", &M);
553 GV->setSection("llvm.metadata");
554 return DIGlobalVariable(GV);
558 /// CreateVariable - Create a new descriptor for the specified variable.
559 DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
560 const std::string &Name,
561 DICompileUnit CompileUnit, unsigned LineNo,
566 getCastToEmpty(Context),
567 GetStringConstant(Name),
568 getCastToEmpty(CompileUnit),
569 ConstantInt::get(Type::Int32Ty, LineNo),
570 getCastToEmpty(Type),
573 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
575 M.addTypeName("llvm.dbg.variable.type", Init->getType());
576 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
577 GlobalValue::InternalLinkage,
578 Init, "llvm.dbg.variable", &M);
579 GV->setSection("llvm.metadata");
580 return DIVariable(GV);
584 /// CreateBlock - This creates a descriptor for a lexical block with the
585 /// specified parent context.
586 DIBlock DIFactory::CreateBlock(DIDescriptor Context) {
588 GetTagConstant(dwarf::DW_TAG_lexical_block),
589 getCastToEmpty(Context)
592 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
594 M.addTypeName("llvm.dbg.block.type", Init->getType());
595 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
596 GlobalValue::InternalLinkage,
597 Init, "llvm.dbg.block", &M);
598 GV->setSection("llvm.metadata");
603 //===----------------------------------------------------------------------===//
604 // DIFactory: Routines for inserting code into a function
605 //===----------------------------------------------------------------------===//
607 /// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
608 /// inserting it at the end of the specified basic block.
609 void DIFactory::InsertStopPoint(DICompileUnit CU, unsigned LineNo,
610 unsigned ColNo, BasicBlock *BB) {
612 // Lazily construct llvm.dbg.stoppoint function.
614 StopPointFn = llvm::Intrinsic::getDeclaration(&M,
615 llvm::Intrinsic::dbg_stoppoint);
617 // Invoke llvm.dbg.stoppoint
619 llvm::ConstantInt::get(llvm::Type::Int32Ty, LineNo),
620 llvm::ConstantInt::get(llvm::Type::Int32Ty, ColNo),
623 CallInst::Create(StopPointFn, Args, Args+3, "", BB);
626 /// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
627 /// mark the start of the specified subprogram.
628 void DIFactory::InsertSubprogramStart(DISubprogram SP, BasicBlock *BB) {
629 // Lazily construct llvm.dbg.func.start.
631 FuncStartFn = llvm::Intrinsic::getDeclaration(&M,
632 llvm::Intrinsic::dbg_func_start);
634 // Call llvm.dbg.func.start which also implicitly sets a stoppoint.
635 CallInst::Create(FuncStartFn, getCastToEmpty(SP), "", BB);
638 /// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
639 /// mark the start of a region for the specified scoping descriptor.
640 void DIFactory::InsertRegionStart(DIDescriptor D, BasicBlock *BB) {
641 // Lazily construct llvm.dbg.region.start function.
643 RegionStartFn = llvm::Intrinsic::getDeclaration(&M,
644 llvm::Intrinsic::dbg_region_start);
645 // Call llvm.dbg.func.start.
646 CallInst::Create(RegionStartFn, getCastToEmpty(D), "", BB);
650 /// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
651 /// mark the end of a region for the specified scoping descriptor.
652 void DIFactory::InsertRegionEnd(DIDescriptor D, BasicBlock *BB) {
653 // Lazily construct llvm.dbg.region.end function.
655 RegionEndFn = llvm::Intrinsic::getDeclaration(&M,
656 llvm::Intrinsic::dbg_region_end);
658 CallInst::Create(RegionEndFn, getCastToEmpty(D), "", BB);
661 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
662 void DIFactory::InsertDeclare(llvm::Value *Storage, DIVariable D,
664 // Cast the storage to a {}* for the call to llvm.dbg.declare.
665 Storage = new llvm::BitCastInst(Storage, EmptyStructPtr, "", BB);
668 DeclareFn = llvm::Intrinsic::getDeclaration(&M,
669 llvm::Intrinsic::dbg_declare);
670 Value *Args[] = { Storage, getCastToEmpty(D) };
671 CallInst::Create(DeclareFn, Args, Args+2, "", BB);
675 /// Finds the stoppoint coressponding to this instruction, that is the
676 /// stoppoint that dominates this instruction
677 const DbgStopPointInst *findStopPoint(const Instruction *Inst)
679 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(Inst))
682 const BasicBlock *BB = Inst->getParent();
683 BasicBlock::const_iterator I = Inst, B;
686 // A BB consisting only of a terminator can't have a stoppoint.
690 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
694 // This BB didn't have a stoppoint: if there is only one
695 // predecessor, look for a stoppoint there.
696 // We could use getIDom(), but that would require dominator info.
697 BB = I->getParent()->getUniquePredecessor();
699 I = BB->getTerminator();
704 /// Finds the stoppoint corresponding to first real (non-debug intrinsic)
705 /// instruction in this Basic Block, and returns the stoppoint for it.
706 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB)
708 for(BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
709 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
712 // Fallback to looking for stoppoint of unique predecessor.
713 // Useful if this BB contains no stoppoints, but unique predecessor does.
714 BB = BB->getUniquePredecessor();
716 return findStopPoint(BB->getTerminator());
720 /// Finds the dbg.declare intrinsic corresponding to this value if any.
721 /// It looks through pointer casts too.
722 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts)
725 V = V->stripPointerCasts();
726 // Look for the bitcast.
727 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
729 if (isa<BitCastInst>(I))
730 return findDbgDeclare(*I, false);
735 // Find dbg.declare among uses of the instruction.
736 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
738 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I))