blob: f1f7d8eff35a71015a877f9ff9448091c37dc9d1 [file] [log] [blame]
Chris Lattnera45664f2008-11-10 02:56:27 +00001//===--- DebugInfo.cpp - Debug Information Helper Classes -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the helper classes used to build and interpret debug
11// information in LLVM IR form.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Analysis/DebugInfo.h"
16#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Intrinsics.h"
Torok Edwin620f2802008-12-16 09:07:36 +000019#include "llvm/IntrinsicInst.h"
Chris Lattnera45664f2008-11-10 02:56:27 +000020#include "llvm/Instructions.h"
21#include "llvm/Module.h"
22#include "llvm/Analysis/ValueTracking.h"
23#include "llvm/Support/Dwarf.h"
24using namespace llvm;
25
26//===----------------------------------------------------------------------===//
27// DIDescriptor
28//===----------------------------------------------------------------------===//
29
30DIDescriptor::DIDescriptor(GlobalVariable *gv, unsigned RequiredTag) {
31 GV = gv;
32
33 // If this is non-null, check to see if the Tag matches. If not, set to null.
34 if (GV && getTag() != RequiredTag)
35 GV = 0;
36}
37
38
39std::string DIDescriptor::getStringField(unsigned Elt) const {
40 if (GV == 0) return "";
41 Constant *C = GV->getInitializer();
42 if (C == 0 || Elt >= C->getNumOperands())
43 return "";
44
45 std::string Result;
46 // Fills in the string if it succeeds
47 if (!GetConstantStringInfo(C->getOperand(Elt), Result))
48 Result.clear();
49 return Result;
50}
51
52uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
53 if (GV == 0) return 0;
54 Constant *C = GV->getInitializer();
55 if (C == 0 || Elt >= C->getNumOperands())
56 return 0;
57 if (ConstantInt *CI = dyn_cast<ConstantInt>(C->getOperand(Elt)))
58 return CI->getZExtValue();
59 return 0;
60}
61
62
63DIDescriptor 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()));
70}
71
72GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
73 if (GV == 0) return 0;
74 Constant *C = GV->getInitializer();
75 if (C == 0 || Elt >= C->getNumOperands())
76 return 0;
77 C = C->getOperand(Elt);
78
79 return dyn_cast<GlobalVariable>(C->stripPointerCasts());
80}
81
82
83
Chris Lattnera45664f2008-11-10 02:56:27 +000084//===----------------------------------------------------------------------===//
85// Simple Descriptor Constructors and other Methods
86//===----------------------------------------------------------------------===//
87
88DIAnchor::DIAnchor(GlobalVariable *GV)
89 : DIDescriptor(GV, dwarf::DW_TAG_anchor) {}
90DIEnumerator::DIEnumerator(GlobalVariable *GV)
91 : DIDescriptor(GV, dwarf::DW_TAG_enumerator) {}
92DISubrange::DISubrange(GlobalVariable *GV)
93 : DIDescriptor(GV, dwarf::DW_TAG_subrange_type) {}
94DICompileUnit::DICompileUnit(GlobalVariable *GV)
95 : DIDescriptor(GV, dwarf::DW_TAG_compile_unit) {}
96DIBasicType::DIBasicType(GlobalVariable *GV)
97 : DIType(GV, dwarf::DW_TAG_base_type) {}
98DISubprogram::DISubprogram(GlobalVariable *GV)
99 : DIGlobal(GV, dwarf::DW_TAG_subprogram) {}
100DIGlobalVariable::DIGlobalVariable(GlobalVariable *GV)
101 : DIGlobal(GV, dwarf::DW_TAG_variable) {}
102DIBlock::DIBlock(GlobalVariable *GV)
103 : DIDescriptor(GV, dwarf::DW_TAG_lexical_block) {}
Torok Edwinb07fbd92008-12-13 08:25:29 +0000104// needed by DIVariable::getType()
Torok Edwina70c68e2008-12-16 09:06:01 +0000105DIType::DIType(GlobalVariable *gv) : DIDescriptor(gv) {
106 if (!gv) return;
Torok Edwinb07fbd92008-12-13 08:25:29 +0000107 unsigned tag = getTag();
108 if (tag != dwarf::DW_TAG_base_type && !DIDerivedType::isDerivedType(tag) &&
109 !DICompositeType::isCompositeType(tag))
110 GV = 0;
111}
Chris Lattnera45664f2008-11-10 02:56:27 +0000112
113/// isDerivedType - Return true if the specified tag is legal for
114/// DIDerivedType.
115bool DIDerivedType::isDerivedType(unsigned Tag) {
116 switch (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:
125 return true;
126 default:
127 // FIXME: Even though it doesn't make sense, CompositeTypes are current
128 // modelled as DerivedTypes, this should return true for them as well.
129 return false;
130 }
131}
132
133DIDerivedType::DIDerivedType(GlobalVariable *GV) : DIType(GV, true, true) {
134 if (GV && !isDerivedType(getTag()))
135 GV = 0;
136}
137
138/// isCompositeType - Return true if the specified tag is legal for
139/// DICompositeType.
140bool DICompositeType::isCompositeType(unsigned TAG) {
141 switch (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:
148 return true;
149 default:
150 return false;
151 }
152}
153
154DICompositeType::DICompositeType(GlobalVariable *GV)
155 : DIDerivedType(GV, true, true) {
156 if (GV && !isCompositeType(getTag()))
157 GV = 0;
158}
159
160/// isVariable - Return true if the specified tag is legal for DIVariable.
161bool DIVariable::isVariable(unsigned Tag) {
162 switch (Tag) {
163 case dwarf::DW_TAG_auto_variable:
164 case dwarf::DW_TAG_arg_variable:
165 case dwarf::DW_TAG_return_variable:
166 return true;
167 default:
168 return false;
169 }
170}
171
172DIVariable::DIVariable(GlobalVariable *GV) : DIDescriptor(GV) {
173 if (GV && !isVariable(getTag()))
174 GV = 0;
175}
176
177
178
179//===----------------------------------------------------------------------===//
180// DIFactory: Basic Helpers
181//===----------------------------------------------------------------------===//
182
Chris Lattner497a7a82008-11-10 04:10:34 +0000183DIFactory::DIFactory(Module &m) : M(m) {
184 StopPointFn = FuncStartFn = RegionStartFn = RegionEndFn = DeclareFn = 0;
185 EmptyStructPtr = PointerType::getUnqual(StructType::get(NULL, NULL));
186}
187
188/// getCastToEmpty - Return this descriptor as a Constant* with type '{}*'.
189/// This is only valid when the descriptor is non-null.
190Constant *DIFactory::getCastToEmpty(DIDescriptor D) {
191 if (D.isNull()) return Constant::getNullValue(EmptyStructPtr);
192 return ConstantExpr::getBitCast(D.getGV(), EmptyStructPtr);
193}
194
Chris Lattnera45664f2008-11-10 02:56:27 +0000195Constant *DIFactory::GetTagConstant(unsigned TAG) {
196 assert((TAG & DIDescriptor::VersionMask) == 0 &&
197 "Tag too large for debug encoding!");
Devang Patel854967e2008-12-17 22:39:29 +0000198 return ConstantInt::get(Type::Int32Ty, TAG | DIDescriptor::Version7);
Chris Lattnera45664f2008-11-10 02:56:27 +0000199}
200
201Constant *DIFactory::GetStringConstant(const std::string &String) {
202 // Check string cache for previous edition.
203 Constant *&Slot = StringCache[String];
204
205 // Return Constant if previously defined.
206 if (Slot) return Slot;
207
208 const PointerType *DestTy = PointerType::getUnqual(Type::Int8Ty);
209
210 // If empty string then use a sbyte* null instead.
211 if (String.empty())
212 return Slot = ConstantPointerNull::get(DestTy);
213
214 // Construct string as an llvm constant.
215 Constant *ConstStr = ConstantArray::get(String);
216
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);
223}
224
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.
227DIAnchor DIFactory::GetOrCreateAnchor(unsigned TAG, const char *Name) {
Chris Lattner497a7a82008-11-10 04:10:34 +0000228 const Type *EltTy = StructType::get(Type::Int32Ty, Type::Int32Ty, NULL);
Chris Lattnera45664f2008-11-10 02:56:27 +0000229
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);
234
235 // If it has an initializer, it is already in the module.
236 if (GV->hasInitializer())
237 return SubProgramAnchor = DIAnchor(GV);
238
239 GV->setLinkage(GlobalValue::LinkOnceLinkage);
240 GV->setSection("llvm.metadata");
241 GV->setConstant(true);
242 M.addTypeName("llvm.dbg.anchor.type", EltTy);
243
244 // Otherwise, set the initializer.
245 Constant *Elts[] = {
246 GetTagConstant(dwarf::DW_TAG_anchor),
247 ConstantInt::get(Type::Int32Ty, TAG)
248 };
249
250 GV->setInitializer(ConstantStruct::get(Elts, 2));
251 return DIAnchor(GV);
252}
253
254
255
256//===----------------------------------------------------------------------===//
257// DIFactory: Primary Constructors
258//===----------------------------------------------------------------------===//
259
260/// GetOrCreateCompileUnitAnchor - Return the anchor for compile units,
261/// creating a new one if there isn't already one in the module.
262DIAnchor 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");
268}
269
270/// GetOrCreateSubprogramAnchor - Return the anchor for subprograms,
271/// creating a new one if there isn't already one in the module.
272DIAnchor 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");
278}
279
280/// GetOrCreateGlobalVariableAnchor - Return the anchor for globals,
281/// creating a new one if there isn't already one in the module.
282DIAnchor 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");
288}
289
290/// GetOrCreateArray - Create an descriptor for an array of descriptors.
291/// This implicitly uniques the arrays created.
292DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
293 SmallVector<Constant*, 16> Elts;
294
295 for (unsigned i = 0; i != NumTys; ++i)
Chris Lattner497a7a82008-11-10 04:10:34 +0000296 Elts.push_back(getCastToEmpty(Tys[i]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000297
Chris Lattner497a7a82008-11-10 04:10:34 +0000298 Constant *Init = ConstantArray::get(ArrayType::get(EmptyStructPtr,
299 Elts.size()),
Chris Lattnera45664f2008-11-10 02:56:27 +0000300 &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());
304
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);
310 return DIArray(GV);
311}
312
313/// GetOrCreateSubrange - Create a descriptor for a value range. This
314/// implicitly uniques the values returned.
315DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
316 Constant *Elts[] = {
317 GetTagConstant(dwarf::DW_TAG_subrange_type),
318 ConstantInt::get(Type::Int64Ty, Lo),
319 ConstantInt::get(Type::Int64Ty, Hi)
320 };
321
322 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
323
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());
327
328 M.addTypeName("llvm.dbg.subrange.type", Init->getType());
329
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);
336}
337
338
339
340/// CreateCompileUnit - Create a new descriptor for the specified compile
341/// unit. Note that this does not unique compile units within the module.
342DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
343 const std::string &Filename,
344 const std::string &Directory,
345 const std::string &Producer) {
346 Constant *Elts[] = {
347 GetTagConstant(dwarf::DW_TAG_compile_unit),
Chris Lattner497a7a82008-11-10 04:10:34 +0000348 getCastToEmpty(GetOrCreateCompileUnitAnchor()),
Chris Lattnera45664f2008-11-10 02:56:27 +0000349 ConstantInt::get(Type::Int32Ty, LangID),
350 GetStringConstant(Filename),
351 GetStringConstant(Directory),
352 GetStringConstant(Producer)
353 };
354
355 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
356
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);
363}
364
365/// CreateEnumerator - Create a single enumerator value.
366DIEnumerator DIFactory::CreateEnumerator(const std::string &Name, uint64_t Val){
367 Constant *Elts[] = {
368 GetTagConstant(dwarf::DW_TAG_enumerator),
369 GetStringConstant(Name),
370 ConstantInt::get(Type::Int64Ty, Val)
371 };
372
373 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
374
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);
381}
382
383
384/// CreateBasicType - Create a basic type like int, float, etc.
385DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
386 const std::string &Name,
387 DICompileUnit CompileUnit,
388 unsigned LineNumber,
389 uint64_t SizeInBits,
390 uint64_t AlignInBits,
391 uint64_t OffsetInBits, unsigned Flags,
Devang Patel854967e2008-12-17 22:39:29 +0000392 unsigned Encoding,
393 const std::string *FileName,
394 const std::string *Directory) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000395 Constant *Elts[] = {
396 GetTagConstant(dwarf::DW_TAG_base_type),
Chris Lattner497a7a82008-11-10 04:10:34 +0000397 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000398 GetStringConstant(Name),
Chris Lattner497a7a82008-11-10 04:10:34 +0000399 getCastToEmpty(CompileUnit),
Chris Lattnera45664f2008-11-10 02:56:27 +0000400 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),
Devang Patel854967e2008-12-17 22:39:29 +0000405 ConstantInt::get(Type::Int32Ty, Encoding),
406 GetStringConstant(FileName ? FileName->c_str() : ""),
407 GetStringConstant(Directory ? Directory->c_str() : "")
Chris Lattnera45664f2008-11-10 02:56:27 +0000408 };
409
410 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
411
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);
418}
419
420/// CreateDerivedType - Create a derived type like const qualified type,
421/// pointer, typedef, etc.
422DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
423 DIDescriptor Context,
424 const std::string &Name,
425 DICompileUnit CompileUnit,
426 unsigned LineNumber,
427 uint64_t SizeInBits,
428 uint64_t AlignInBits,
429 uint64_t OffsetInBits,
430 unsigned Flags,
Devang Patel854967e2008-12-17 22:39:29 +0000431 DIType DerivedFrom,
432 const std::string *FileName,
433 const std::string *Directory) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000434 Constant *Elts[] = {
435 GetTagConstant(Tag),
Chris Lattner497a7a82008-11-10 04:10:34 +0000436 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000437 GetStringConstant(Name),
Chris Lattner497a7a82008-11-10 04:10:34 +0000438 getCastToEmpty(CompileUnit),
Chris Lattnera45664f2008-11-10 02:56:27 +0000439 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),
Devang Patel854967e2008-12-17 22:39:29 +0000444 getCastToEmpty(DerivedFrom),
445 GetStringConstant(FileName ? FileName->c_str() : ""),
446 GetStringConstant(Directory ? Directory->c_str() : "")
Chris Lattnera45664f2008-11-10 02:56:27 +0000447 };
448
449 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
450
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);
457}
458
459/// CreateCompositeType - Create a composite type like array, struct, etc.
460DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
461 DIDescriptor Context,
462 const std::string &Name,
463 DICompileUnit CompileUnit,
464 unsigned LineNumber,
465 uint64_t SizeInBits,
466 uint64_t AlignInBits,
467 uint64_t OffsetInBits,
468 unsigned Flags,
469 DIType DerivedFrom,
Devang Patel854967e2008-12-17 22:39:29 +0000470 DIArray Elements,
471 const std::string *FileName,
472 const std::string *Directory) {
473
Chris Lattnera45664f2008-11-10 02:56:27 +0000474 Constant *Elts[] = {
475 GetTagConstant(Tag),
Chris Lattner497a7a82008-11-10 04:10:34 +0000476 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000477 GetStringConstant(Name),
Chris Lattner497a7a82008-11-10 04:10:34 +0000478 getCastToEmpty(CompileUnit),
Chris Lattnera45664f2008-11-10 02:56:27 +0000479 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),
Chris Lattner497a7a82008-11-10 04:10:34 +0000484 getCastToEmpty(DerivedFrom),
Devang Patel854967e2008-12-17 22:39:29 +0000485 getCastToEmpty(Elements),
486 GetStringConstant(FileName ? FileName->c_str() : ""),
487 GetStringConstant(Directory ? Directory->c_str() : "")
Chris Lattnera45664f2008-11-10 02:56:27 +0000488 };
489
490 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
491
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);
498}
499
500
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.
504DISubprogram 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,
510 bool isLocalToUnit,
Devang Patel854967e2008-12-17 22:39:29 +0000511 bool isDefinition,
512 const std::string *FileName,
513 const std::string *Directory) {
514
Chris Lattnera45664f2008-11-10 02:56:27 +0000515 Constant *Elts[] = {
516 GetTagConstant(dwarf::DW_TAG_subprogram),
Chris Lattner497a7a82008-11-10 04:10:34 +0000517 getCastToEmpty(GetOrCreateSubprogramAnchor()),
518 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000519 GetStringConstant(Name),
520 GetStringConstant(DisplayName),
521 GetStringConstant(LinkageName),
Chris Lattner497a7a82008-11-10 04:10:34 +0000522 getCastToEmpty(CompileUnit),
Chris Lattnera45664f2008-11-10 02:56:27 +0000523 ConstantInt::get(Type::Int32Ty, LineNo),
Chris Lattner497a7a82008-11-10 04:10:34 +0000524 getCastToEmpty(Type),
Chris Lattnera45664f2008-11-10 02:56:27 +0000525 ConstantInt::get(Type::Int1Ty, isLocalToUnit),
Devang Patel854967e2008-12-17 22:39:29 +0000526 ConstantInt::get(Type::Int1Ty, isDefinition),
527 GetStringConstant(FileName ? FileName->c_str() : ""),
528 GetStringConstant(Directory ? Directory->c_str() : "")
Chris Lattnera45664f2008-11-10 02:56:27 +0000529 };
530
531 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
532
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);
539}
540
541/// CreateGlobalVariable - Create a new descriptor for the specified global.
542DIGlobalVariable
543DIFactory::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,
Devang Patel854967e2008-12-17 22:39:29 +0000548 bool isDefinition, llvm::GlobalVariable *Val,
549 const std::string *FileName,
550 const std::string *Directory) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000551 Constant *Elts[] = {
552 GetTagConstant(dwarf::DW_TAG_variable),
Chris Lattner497a7a82008-11-10 04:10:34 +0000553 getCastToEmpty(GetOrCreateGlobalVariableAnchor()),
554 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000555 GetStringConstant(Name),
556 GetStringConstant(DisplayName),
557 GetStringConstant(LinkageName),
Chris Lattner497a7a82008-11-10 04:10:34 +0000558 getCastToEmpty(CompileUnit),
Chris Lattnera45664f2008-11-10 02:56:27 +0000559 ConstantInt::get(Type::Int32Ty, LineNo),
Chris Lattner497a7a82008-11-10 04:10:34 +0000560 getCastToEmpty(Type),
Chris Lattnera45664f2008-11-10 02:56:27 +0000561 ConstantInt::get(Type::Int1Ty, isLocalToUnit),
562 ConstantInt::get(Type::Int1Ty, isDefinition),
Devang Patel854967e2008-12-17 22:39:29 +0000563 ConstantExpr::getBitCast(Val, EmptyStructPtr),
564 GetStringConstant(FileName ? FileName->c_str() : ""),
565 GetStringConstant(Directory ? Directory->c_str() : "")
Chris Lattnera45664f2008-11-10 02:56:27 +0000566 };
567
568 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
569
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);
576}
577
578
579/// CreateVariable - Create a new descriptor for the specified variable.
580DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
581 const std::string &Name,
582 DICompileUnit CompileUnit, unsigned LineNo,
Devang Patel854967e2008-12-17 22:39:29 +0000583 DIType Type,
584 const std::string *FileName,
585 const std::string *Directory) {
586
Chris Lattnera45664f2008-11-10 02:56:27 +0000587
588 Constant *Elts[] = {
589 GetTagConstant(Tag),
Chris Lattner497a7a82008-11-10 04:10:34 +0000590 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000591 GetStringConstant(Name),
Chris Lattner497a7a82008-11-10 04:10:34 +0000592 getCastToEmpty(CompileUnit),
Chris Lattnera45664f2008-11-10 02:56:27 +0000593 ConstantInt::get(Type::Int32Ty, LineNo),
Chris Lattner497a7a82008-11-10 04:10:34 +0000594 getCastToEmpty(Type),
Devang Patel854967e2008-12-17 22:39:29 +0000595 GetStringConstant(FileName ? FileName->c_str() : ""),
596 GetStringConstant(Directory ? Directory->c_str() : "")
Chris Lattnera45664f2008-11-10 02:56:27 +0000597 };
598
599 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
600
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);
607}
608
609
610/// CreateBlock - This creates a descriptor for a lexical block with the
611/// specified parent context.
612DIBlock DIFactory::CreateBlock(DIDescriptor Context) {
613 Constant *Elts[] = {
614 GetTagConstant(dwarf::DW_TAG_lexical_block),
Chris Lattner497a7a82008-11-10 04:10:34 +0000615 getCastToEmpty(Context)
Chris Lattnera45664f2008-11-10 02:56:27 +0000616 };
617
618 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
619
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");
625 return DIBlock(GV);
626}
627
628
629//===----------------------------------------------------------------------===//
630// DIFactory: Routines for inserting code into a function
631//===----------------------------------------------------------------------===//
632
633/// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
634/// inserting it at the end of the specified basic block.
635void DIFactory::InsertStopPoint(DICompileUnit CU, unsigned LineNo,
636 unsigned ColNo, BasicBlock *BB) {
637
638 // Lazily construct llvm.dbg.stoppoint function.
639 if (!StopPointFn)
640 StopPointFn = llvm::Intrinsic::getDeclaration(&M,
641 llvm::Intrinsic::dbg_stoppoint);
642
643 // Invoke llvm.dbg.stoppoint
644 Value *Args[] = {
645 llvm::ConstantInt::get(llvm::Type::Int32Ty, LineNo),
646 llvm::ConstantInt::get(llvm::Type::Int32Ty, ColNo),
Chris Lattner497a7a82008-11-10 04:10:34 +0000647 getCastToEmpty(CU)
Chris Lattnera45664f2008-11-10 02:56:27 +0000648 };
649 CallInst::Create(StopPointFn, Args, Args+3, "", BB);
650}
651
652/// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
653/// mark the start of the specified subprogram.
654void DIFactory::InsertSubprogramStart(DISubprogram SP, BasicBlock *BB) {
655 // Lazily construct llvm.dbg.func.start.
656 if (!FuncStartFn)
657 FuncStartFn = llvm::Intrinsic::getDeclaration(&M,
658 llvm::Intrinsic::dbg_func_start);
659
660 // Call llvm.dbg.func.start which also implicitly sets a stoppoint.
Chris Lattner497a7a82008-11-10 04:10:34 +0000661 CallInst::Create(FuncStartFn, getCastToEmpty(SP), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000662}
663
664/// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
665/// mark the start of a region for the specified scoping descriptor.
666void DIFactory::InsertRegionStart(DIDescriptor D, BasicBlock *BB) {
667 // Lazily construct llvm.dbg.region.start function.
668 if (!RegionStartFn)
669 RegionStartFn = llvm::Intrinsic::getDeclaration(&M,
670 llvm::Intrinsic::dbg_region_start);
671 // Call llvm.dbg.func.start.
Chris Lattner497a7a82008-11-10 04:10:34 +0000672 CallInst::Create(RegionStartFn, getCastToEmpty(D), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000673}
674
675
676/// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
677/// mark the end of a region for the specified scoping descriptor.
678void DIFactory::InsertRegionEnd(DIDescriptor D, BasicBlock *BB) {
679 // Lazily construct llvm.dbg.region.end function.
680 if (!RegionEndFn)
681 RegionEndFn = llvm::Intrinsic::getDeclaration(&M,
682 llvm::Intrinsic::dbg_region_end);
683
Chris Lattner497a7a82008-11-10 04:10:34 +0000684 CallInst::Create(RegionEndFn, getCastToEmpty(D), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000685}
686
687/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
688void DIFactory::InsertDeclare(llvm::Value *Storage, DIVariable D,
689 BasicBlock *BB) {
690 // Cast the storage to a {}* for the call to llvm.dbg.declare.
Chris Lattner497a7a82008-11-10 04:10:34 +0000691 Storage = new llvm::BitCastInst(Storage, EmptyStructPtr, "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000692
693 if (!DeclareFn)
694 DeclareFn = llvm::Intrinsic::getDeclaration(&M,
695 llvm::Intrinsic::dbg_declare);
Chris Lattner497a7a82008-11-10 04:10:34 +0000696 Value *Args[] = { Storage, getCastToEmpty(D) };
Chris Lattnera45664f2008-11-10 02:56:27 +0000697 CallInst::Create(DeclareFn, Args, Args+2, "", BB);
698}
Torok Edwin620f2802008-12-16 09:07:36 +0000699
700namespace llvm {
701 /// Finds the stoppoint coressponding to this instruction, that is the
702 /// stoppoint that dominates this instruction
703 const DbgStopPointInst *findStopPoint(const Instruction *Inst)
704 {
705 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(Inst))
706 return DSI;
707
708 const BasicBlock *BB = Inst->getParent();
709 BasicBlock::const_iterator I = Inst, B;
710 do {
711 B = BB->begin();
712 // A BB consisting only of a terminator can't have a stoppoint.
713 if (I != B) {
714 do {
715 --I;
716 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
717 return DSI;
718 } while (I != B);
719 }
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();
724 if (BB)
725 I = BB->getTerminator();
726 } while (BB != 0);
727 return 0;
728 }
729
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)
733 {
734 for(BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
735 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
736 return DSI;
737 }
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();
741 if (BB)
742 return findStopPoint(BB->getTerminator());
743 return 0;
744 }
745
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)
749 {
750 if (stripCasts) {
751 V = V->stripPointerCasts();
752 // Look for the bitcast.
753 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
754 I != E; ++I) {
755 if (isa<BitCastInst>(I))
756 return findDbgDeclare(*I, false);
757 }
758 return 0;
759 }
760
761 // Find dbg.declare among uses of the instruction.
762 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
763 I != E; ++I) {
764 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I))
765 return DDI;
766 }
767 return 0;
768 }
769}
770