blob: 2f336572a1df4ef75c4bdbb5370c1d1c39fbaa43 [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"
Chris Lattnera45664f2008-11-10 02:56:27 +000023using namespace llvm;
24
25//===----------------------------------------------------------------------===//
26// DIDescriptor
27//===----------------------------------------------------------------------===//
28
29DIDescriptor::DIDescriptor(GlobalVariable *gv, unsigned RequiredTag) {
30 GV = gv;
31
32 // If this is non-null, check to see if the Tag matches. If not, set to null.
33 if (GV && getTag() != RequiredTag)
34 GV = 0;
35}
36
37
38std::string DIDescriptor::getStringField(unsigned Elt) const {
39 if (GV == 0) return "";
40 Constant *C = GV->getInitializer();
41 if (C == 0 || Elt >= C->getNumOperands())
42 return "";
43
44 std::string Result;
45 // Fills in the string if it succeeds
46 if (!GetConstantStringInfo(C->getOperand(Elt), Result))
47 Result.clear();
48 return Result;
49}
50
51uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
52 if (GV == 0) return 0;
53 Constant *C = GV->getInitializer();
54 if (C == 0 || Elt >= C->getNumOperands())
55 return 0;
56 if (ConstantInt *CI = dyn_cast<ConstantInt>(C->getOperand(Elt)))
57 return CI->getZExtValue();
58 return 0;
59}
60
61
62DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
63 if (GV == 0) return DIDescriptor();
64 Constant *C = GV->getInitializer();
65 if (C == 0 || Elt >= C->getNumOperands())
66 return DIDescriptor();
67 C = C->getOperand(Elt);
68 return DIDescriptor(dyn_cast<GlobalVariable>(C->stripPointerCasts()));
69}
70
71GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
72 if (GV == 0) return 0;
73 Constant *C = GV->getInitializer();
74 if (C == 0 || Elt >= C->getNumOperands())
75 return 0;
76 C = C->getOperand(Elt);
77
78 return dyn_cast<GlobalVariable>(C->stripPointerCasts());
79}
80
81
82
Chris Lattnera45664f2008-11-10 02:56:27 +000083//===----------------------------------------------------------------------===//
84// Simple Descriptor Constructors and other Methods
85//===----------------------------------------------------------------------===//
86
87DIAnchor::DIAnchor(GlobalVariable *GV)
88 : DIDescriptor(GV, dwarf::DW_TAG_anchor) {}
89DIEnumerator::DIEnumerator(GlobalVariable *GV)
90 : DIDescriptor(GV, dwarf::DW_TAG_enumerator) {}
91DISubrange::DISubrange(GlobalVariable *GV)
92 : DIDescriptor(GV, dwarf::DW_TAG_subrange_type) {}
93DICompileUnit::DICompileUnit(GlobalVariable *GV)
94 : DIDescriptor(GV, dwarf::DW_TAG_compile_unit) {}
95DIBasicType::DIBasicType(GlobalVariable *GV)
96 : DIType(GV, dwarf::DW_TAG_base_type) {}
97DISubprogram::DISubprogram(GlobalVariable *GV)
98 : DIGlobal(GV, dwarf::DW_TAG_subprogram) {}
99DIGlobalVariable::DIGlobalVariable(GlobalVariable *GV)
100 : DIGlobal(GV, dwarf::DW_TAG_variable) {}
101DIBlock::DIBlock(GlobalVariable *GV)
102 : DIDescriptor(GV, dwarf::DW_TAG_lexical_block) {}
Torok Edwinb07fbd92008-12-13 08:25:29 +0000103// needed by DIVariable::getType()
Torok Edwina70c68e2008-12-16 09:06:01 +0000104DIType::DIType(GlobalVariable *gv) : DIDescriptor(gv) {
105 if (!gv) return;
Torok Edwinb07fbd92008-12-13 08:25:29 +0000106 unsigned tag = getTag();
107 if (tag != dwarf::DW_TAG_base_type && !DIDerivedType::isDerivedType(tag) &&
108 !DICompositeType::isCompositeType(tag))
109 GV = 0;
110}
Chris Lattnera45664f2008-11-10 02:56:27 +0000111
112/// isDerivedType - Return true if the specified tag is legal for
113/// DIDerivedType.
Devang Patel486938f2009-01-12 21:38:43 +0000114bool DIType::isDerivedType(unsigned Tag) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000115 switch (Tag) {
116 case dwarf::DW_TAG_typedef:
117 case dwarf::DW_TAG_pointer_type:
118 case dwarf::DW_TAG_reference_type:
119 case dwarf::DW_TAG_const_type:
120 case dwarf::DW_TAG_volatile_type:
121 case dwarf::DW_TAG_restrict_type:
122 case dwarf::DW_TAG_member:
123 case dwarf::DW_TAG_inheritance:
124 return true;
125 default:
126 // FIXME: Even though it doesn't make sense, CompositeTypes are current
127 // modelled as DerivedTypes, this should return true for them as well.
128 return false;
129 }
130}
131
132DIDerivedType::DIDerivedType(GlobalVariable *GV) : DIType(GV, true, true) {
133 if (GV && !isDerivedType(getTag()))
134 GV = 0;
135}
136
137/// isCompositeType - Return true if the specified tag is legal for
138/// DICompositeType.
Devang Patel486938f2009-01-12 21:38:43 +0000139bool DIType::isCompositeType(unsigned TAG) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000140 switch (TAG) {
141 case dwarf::DW_TAG_array_type:
142 case dwarf::DW_TAG_structure_type:
143 case dwarf::DW_TAG_union_type:
144 case dwarf::DW_TAG_enumeration_type:
145 case dwarf::DW_TAG_vector_type:
146 case dwarf::DW_TAG_subroutine_type:
147 return true;
148 default:
149 return false;
150 }
151}
152
153DICompositeType::DICompositeType(GlobalVariable *GV)
154 : DIDerivedType(GV, true, true) {
155 if (GV && !isCompositeType(getTag()))
156 GV = 0;
157}
158
159/// isVariable - Return true if the specified tag is legal for DIVariable.
160bool DIVariable::isVariable(unsigned Tag) {
161 switch (Tag) {
162 case dwarf::DW_TAG_auto_variable:
163 case dwarf::DW_TAG_arg_variable:
164 case dwarf::DW_TAG_return_variable:
165 return true;
166 default:
167 return false;
168 }
169}
170
171DIVariable::DIVariable(GlobalVariable *GV) : DIDescriptor(GV) {
172 if (GV && !isVariable(getTag()))
173 GV = 0;
174}
175
Devang Patel68afdc32009-01-05 18:33:01 +0000176unsigned DIArray::getNumElements() const {
177 assert (GV && "Invalid DIArray");
178 Constant *C = GV->getInitializer();
179 assert (C && "Invalid DIArray initializer");
180 return C->getNumOperands();
181}
Chris Lattnera45664f2008-11-10 02:56:27 +0000182
Devang Patelb79b5352009-01-19 23:21:49 +0000183/// Verify - Verify that a compile unit is well formed.
184bool DICompileUnit::Verify() const {
185 if (isNull())
186 return false;
187 if (getFilename().empty())
188 return false;
189 // It is possible that directory and produce string is empty.
190 return true;
191}
192
193/// Verify - Verify that a type descriptor is well formed.
194bool DIType::Verify() const {
195 if (isNull())
196 return false;
197 if (getContext().isNull())
198 return false;
199
200 DICompileUnit CU = getCompileUnit();
201 if (!CU.isNull() && !CU.Verify())
202 return false;
203 return true;
204}
205
206/// Verify - Verify that a composite type descriptor is well formed.
207bool DICompositeType::Verify() const {
208 if (isNull())
209 return false;
210 if (getContext().isNull())
211 return false;
212
213 DICompileUnit CU = getCompileUnit();
214 if (!CU.isNull() && !CU.Verify())
215 return false;
216 return true;
217}
218
219/// Verify - Verify that a subprogram descriptor is well formed.
220bool DISubprogram::Verify() const {
221 if (isNull())
222 return false;
223
224 if (getContext().isNull())
225 return false;
226
227 DICompileUnit CU = getCompileUnit();
228 if (!CU.Verify())
229 return false;
230
231 DICompositeType Ty = getType();
232 if (!Ty.isNull() && !Ty.Verify())
233 return false;
234 return true;
235}
236
237/// Verify - Verify that a global variable descriptor is well formed.
238bool DIGlobalVariable::Verify() const {
239 if (isNull())
240 return false;
241
242 if (getContext().isNull())
243 return false;
244
245 DICompileUnit CU = getCompileUnit();
246 if (!CU.Verify())
247 return false;
248
249 DIType Ty = getType();
250 if (!Ty.Verify())
251 return false;
252
253 if (!getGlobal())
254 return false;
255
256 return true;
257}
258
259/// Verify - Verify that a variable descriptor is well formed.
260bool DIVariable::Verify() const {
261 if (isNull())
262 return false;
263
264 if (getContext().isNull())
265 return false;
266
267 DIType Ty = getType();
268 if (!Ty.Verify())
269 return false;
270
271
272 return true;
273}
274
275
276
Chris Lattnera45664f2008-11-10 02:56:27 +0000277//===----------------------------------------------------------------------===//
278// DIFactory: Basic Helpers
279//===----------------------------------------------------------------------===//
280
Chris Lattner497a7a82008-11-10 04:10:34 +0000281DIFactory::DIFactory(Module &m) : M(m) {
282 StopPointFn = FuncStartFn = RegionStartFn = RegionEndFn = DeclareFn = 0;
283 EmptyStructPtr = PointerType::getUnqual(StructType::get(NULL, NULL));
284}
285
286/// getCastToEmpty - Return this descriptor as a Constant* with type '{}*'.
287/// This is only valid when the descriptor is non-null.
288Constant *DIFactory::getCastToEmpty(DIDescriptor D) {
289 if (D.isNull()) return Constant::getNullValue(EmptyStructPtr);
290 return ConstantExpr::getBitCast(D.getGV(), EmptyStructPtr);
291}
292
Chris Lattnera45664f2008-11-10 02:56:27 +0000293Constant *DIFactory::GetTagConstant(unsigned TAG) {
Devang Patel6906ba52009-01-20 19:22:03 +0000294 assert((TAG & LLVMDebugVersionMask) == 0 &&
Chris Lattnera45664f2008-11-10 02:56:27 +0000295 "Tag too large for debug encoding!");
Devang Patel6906ba52009-01-20 19:22:03 +0000296 return ConstantInt::get(Type::Int32Ty, TAG | LLVMDebugVersion);
Chris Lattnera45664f2008-11-10 02:56:27 +0000297}
298
299Constant *DIFactory::GetStringConstant(const std::string &String) {
300 // Check string cache for previous edition.
301 Constant *&Slot = StringCache[String];
302
303 // Return Constant if previously defined.
304 if (Slot) return Slot;
305
306 const PointerType *DestTy = PointerType::getUnqual(Type::Int8Ty);
307
308 // If empty string then use a sbyte* null instead.
309 if (String.empty())
310 return Slot = ConstantPointerNull::get(DestTy);
311
312 // Construct string as an llvm constant.
313 Constant *ConstStr = ConstantArray::get(String);
314
315 // Otherwise create and return a new string global.
316 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
317 GlobalVariable::InternalLinkage,
318 ConstStr, ".str", &M);
319 StrGV->setSection("llvm.metadata");
320 return Slot = ConstantExpr::getBitCast(StrGV, DestTy);
321}
322
323/// GetOrCreateAnchor - Look up an anchor for the specified tag and name. If it
324/// already exists, return it. If not, create a new one and return it.
325DIAnchor DIFactory::GetOrCreateAnchor(unsigned TAG, const char *Name) {
Chris Lattner497a7a82008-11-10 04:10:34 +0000326 const Type *EltTy = StructType::get(Type::Int32Ty, Type::Int32Ty, NULL);
Chris Lattnera45664f2008-11-10 02:56:27 +0000327
328 // Otherwise, create the global or return it if already in the module.
329 Constant *C = M.getOrInsertGlobal(Name, EltTy);
330 assert(isa<GlobalVariable>(C) && "Incorrectly typed anchor?");
331 GlobalVariable *GV = cast<GlobalVariable>(C);
332
333 // If it has an initializer, it is already in the module.
334 if (GV->hasInitializer())
335 return SubProgramAnchor = DIAnchor(GV);
336
337 GV->setLinkage(GlobalValue::LinkOnceLinkage);
338 GV->setSection("llvm.metadata");
339 GV->setConstant(true);
340 M.addTypeName("llvm.dbg.anchor.type", EltTy);
341
342 // Otherwise, set the initializer.
343 Constant *Elts[] = {
344 GetTagConstant(dwarf::DW_TAG_anchor),
345 ConstantInt::get(Type::Int32Ty, TAG)
346 };
347
348 GV->setInitializer(ConstantStruct::get(Elts, 2));
349 return DIAnchor(GV);
350}
351
352
353
354//===----------------------------------------------------------------------===//
355// DIFactory: Primary Constructors
356//===----------------------------------------------------------------------===//
357
358/// GetOrCreateCompileUnitAnchor - Return the anchor for compile units,
359/// creating a new one if there isn't already one in the module.
360DIAnchor DIFactory::GetOrCreateCompileUnitAnchor() {
361 // If we already created one, just return it.
362 if (!CompileUnitAnchor.isNull())
363 return CompileUnitAnchor;
364 return CompileUnitAnchor = GetOrCreateAnchor(dwarf::DW_TAG_compile_unit,
365 "llvm.dbg.compile_units");
366}
367
368/// GetOrCreateSubprogramAnchor - Return the anchor for subprograms,
369/// creating a new one if there isn't already one in the module.
370DIAnchor DIFactory::GetOrCreateSubprogramAnchor() {
371 // If we already created one, just return it.
372 if (!SubProgramAnchor.isNull())
373 return SubProgramAnchor;
374 return SubProgramAnchor = GetOrCreateAnchor(dwarf::DW_TAG_subprogram,
375 "llvm.dbg.subprograms");
376}
377
378/// GetOrCreateGlobalVariableAnchor - Return the anchor for globals,
379/// creating a new one if there isn't already one in the module.
380DIAnchor DIFactory::GetOrCreateGlobalVariableAnchor() {
381 // If we already created one, just return it.
382 if (!GlobalVariableAnchor.isNull())
383 return GlobalVariableAnchor;
384 return GlobalVariableAnchor = GetOrCreateAnchor(dwarf::DW_TAG_variable,
385 "llvm.dbg.global_variables");
386}
387
388/// GetOrCreateArray - Create an descriptor for an array of descriptors.
389/// This implicitly uniques the arrays created.
390DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
391 SmallVector<Constant*, 16> Elts;
392
393 for (unsigned i = 0; i != NumTys; ++i)
Chris Lattner497a7a82008-11-10 04:10:34 +0000394 Elts.push_back(getCastToEmpty(Tys[i]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000395
Chris Lattner497a7a82008-11-10 04:10:34 +0000396 Constant *Init = ConstantArray::get(ArrayType::get(EmptyStructPtr,
397 Elts.size()),
Chris Lattnera45664f2008-11-10 02:56:27 +0000398 &Elts[0], Elts.size());
399 // If we already have this array, just return the uniqued version.
400 DIDescriptor &Entry = SimpleConstantCache[Init];
401 if (!Entry.isNull()) return DIArray(Entry.getGV());
402
403 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
404 GlobalValue::InternalLinkage,
405 Init, "llvm.dbg.array", &M);
406 GV->setSection("llvm.metadata");
407 Entry = DIDescriptor(GV);
408 return DIArray(GV);
409}
410
411/// GetOrCreateSubrange - Create a descriptor for a value range. This
412/// implicitly uniques the values returned.
413DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
414 Constant *Elts[] = {
415 GetTagConstant(dwarf::DW_TAG_subrange_type),
416 ConstantInt::get(Type::Int64Ty, Lo),
417 ConstantInt::get(Type::Int64Ty, Hi)
418 };
419
420 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
421
422 // If we already have this range, just return the uniqued version.
423 DIDescriptor &Entry = SimpleConstantCache[Init];
424 if (!Entry.isNull()) return DISubrange(Entry.getGV());
425
426 M.addTypeName("llvm.dbg.subrange.type", Init->getType());
427
428 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
429 GlobalValue::InternalLinkage,
430 Init, "llvm.dbg.subrange", &M);
431 GV->setSection("llvm.metadata");
432 Entry = DIDescriptor(GV);
433 return DISubrange(GV);
434}
435
436
437
438/// CreateCompileUnit - Create a new descriptor for the specified compile
439/// unit. Note that this does not unique compile units within the module.
440DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
441 const std::string &Filename,
442 const std::string &Directory,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000443 const std::string &Producer,
444 bool isOptimized,
445 const char *Flags) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000446 Constant *Elts[] = {
447 GetTagConstant(dwarf::DW_TAG_compile_unit),
Chris Lattner497a7a82008-11-10 04:10:34 +0000448 getCastToEmpty(GetOrCreateCompileUnitAnchor()),
Chris Lattnera45664f2008-11-10 02:56:27 +0000449 ConstantInt::get(Type::Int32Ty, LangID),
450 GetStringConstant(Filename),
451 GetStringConstant(Directory),
Devang Patel3b64c6b2009-01-23 22:33:47 +0000452 GetStringConstant(Producer),
453 ConstantInt::get(Type::Int1Ty, isOptimized),
454 GetStringConstant(Flags)
Chris Lattnera45664f2008-11-10 02:56:27 +0000455 };
456
457 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
458
459 M.addTypeName("llvm.dbg.compile_unit.type", Init->getType());
460 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
461 GlobalValue::InternalLinkage,
462 Init, "llvm.dbg.compile_unit", &M);
463 GV->setSection("llvm.metadata");
464 return DICompileUnit(GV);
465}
466
467/// CreateEnumerator - Create a single enumerator value.
468DIEnumerator DIFactory::CreateEnumerator(const std::string &Name, uint64_t Val){
469 Constant *Elts[] = {
470 GetTagConstant(dwarf::DW_TAG_enumerator),
471 GetStringConstant(Name),
472 ConstantInt::get(Type::Int64Ty, Val)
473 };
474
475 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
476
477 M.addTypeName("llvm.dbg.enumerator.type", Init->getType());
478 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
479 GlobalValue::InternalLinkage,
480 Init, "llvm.dbg.enumerator", &M);
481 GV->setSection("llvm.metadata");
482 return DIEnumerator(GV);
483}
484
485
486/// CreateBasicType - Create a basic type like int, float, etc.
487DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
488 const std::string &Name,
489 DICompileUnit CompileUnit,
490 unsigned LineNumber,
491 uint64_t SizeInBits,
492 uint64_t AlignInBits,
493 uint64_t OffsetInBits, unsigned Flags,
Devang Patel854967e2008-12-17 22:39:29 +0000494 unsigned Encoding,
495 const std::string *FileName,
496 const std::string *Directory) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000497 Constant *Elts[] = {
498 GetTagConstant(dwarf::DW_TAG_base_type),
Chris Lattner497a7a82008-11-10 04:10:34 +0000499 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000500 GetStringConstant(Name),
Chris Lattner497a7a82008-11-10 04:10:34 +0000501 getCastToEmpty(CompileUnit),
Chris Lattnera45664f2008-11-10 02:56:27 +0000502 ConstantInt::get(Type::Int32Ty, LineNumber),
503 ConstantInt::get(Type::Int64Ty, SizeInBits),
504 ConstantInt::get(Type::Int64Ty, AlignInBits),
505 ConstantInt::get(Type::Int64Ty, OffsetInBits),
506 ConstantInt::get(Type::Int32Ty, Flags),
Devang Patel854967e2008-12-17 22:39:29 +0000507 ConstantInt::get(Type::Int32Ty, Encoding),
508 GetStringConstant(FileName ? FileName->c_str() : ""),
509 GetStringConstant(Directory ? Directory->c_str() : "")
Chris Lattnera45664f2008-11-10 02:56:27 +0000510 };
511
512 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
513
514 M.addTypeName("llvm.dbg.basictype.type", Init->getType());
515 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
516 GlobalValue::InternalLinkage,
517 Init, "llvm.dbg.basictype", &M);
518 GV->setSection("llvm.metadata");
519 return DIBasicType(GV);
520}
521
522/// CreateDerivedType - Create a derived type like const qualified type,
523/// pointer, typedef, etc.
524DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
525 DIDescriptor Context,
526 const std::string &Name,
527 DICompileUnit CompileUnit,
528 unsigned LineNumber,
529 uint64_t SizeInBits,
530 uint64_t AlignInBits,
531 uint64_t OffsetInBits,
532 unsigned Flags,
Devang Patel854967e2008-12-17 22:39:29 +0000533 DIType DerivedFrom,
534 const std::string *FileName,
535 const std::string *Directory) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000536 Constant *Elts[] = {
537 GetTagConstant(Tag),
Chris Lattner497a7a82008-11-10 04:10:34 +0000538 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000539 GetStringConstant(Name),
Chris Lattner497a7a82008-11-10 04:10:34 +0000540 getCastToEmpty(CompileUnit),
Chris Lattnera45664f2008-11-10 02:56:27 +0000541 ConstantInt::get(Type::Int32Ty, LineNumber),
542 ConstantInt::get(Type::Int64Ty, SizeInBits),
543 ConstantInt::get(Type::Int64Ty, AlignInBits),
544 ConstantInt::get(Type::Int64Ty, OffsetInBits),
545 ConstantInt::get(Type::Int32Ty, Flags),
Devang Patel854967e2008-12-17 22:39:29 +0000546 getCastToEmpty(DerivedFrom),
547 GetStringConstant(FileName ? FileName->c_str() : ""),
548 GetStringConstant(Directory ? Directory->c_str() : "")
Chris Lattnera45664f2008-11-10 02:56:27 +0000549 };
550
551 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
552
553 M.addTypeName("llvm.dbg.derivedtype.type", Init->getType());
554 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
555 GlobalValue::InternalLinkage,
556 Init, "llvm.dbg.derivedtype", &M);
557 GV->setSection("llvm.metadata");
558 return DIDerivedType(GV);
559}
560
561/// CreateCompositeType - Create a composite type like array, struct, etc.
562DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
563 DIDescriptor Context,
564 const std::string &Name,
565 DICompileUnit CompileUnit,
566 unsigned LineNumber,
567 uint64_t SizeInBits,
568 uint64_t AlignInBits,
569 uint64_t OffsetInBits,
570 unsigned Flags,
571 DIType DerivedFrom,
Devang Patel854967e2008-12-17 22:39:29 +0000572 DIArray Elements,
573 const std::string *FileName,
574 const std::string *Directory) {
575
Chris Lattnera45664f2008-11-10 02:56:27 +0000576 Constant *Elts[] = {
577 GetTagConstant(Tag),
Chris Lattner497a7a82008-11-10 04:10:34 +0000578 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000579 GetStringConstant(Name),
Chris Lattner497a7a82008-11-10 04:10:34 +0000580 getCastToEmpty(CompileUnit),
Chris Lattnera45664f2008-11-10 02:56:27 +0000581 ConstantInt::get(Type::Int32Ty, LineNumber),
582 ConstantInt::get(Type::Int64Ty, SizeInBits),
583 ConstantInt::get(Type::Int64Ty, AlignInBits),
584 ConstantInt::get(Type::Int64Ty, OffsetInBits),
585 ConstantInt::get(Type::Int32Ty, Flags),
Chris Lattner497a7a82008-11-10 04:10:34 +0000586 getCastToEmpty(DerivedFrom),
Devang Patel854967e2008-12-17 22:39:29 +0000587 getCastToEmpty(Elements),
588 GetStringConstant(FileName ? FileName->c_str() : ""),
589 GetStringConstant(Directory ? Directory->c_str() : "")
Chris Lattnera45664f2008-11-10 02:56:27 +0000590 };
591
592 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
593
594 M.addTypeName("llvm.dbg.composite.type", Init->getType());
595 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
596 GlobalValue::InternalLinkage,
597 Init, "llvm.dbg.composite", &M);
598 GV->setSection("llvm.metadata");
599 return DICompositeType(GV);
600}
601
602
603/// CreateSubprogram - Create a new descriptor for the specified subprogram.
604/// See comments in DISubprogram for descriptions of these fields. This
605/// method does not unique the generated descriptors.
606DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
607 const std::string &Name,
608 const std::string &DisplayName,
609 const std::string &LinkageName,
610 DICompileUnit CompileUnit,
611 unsigned LineNo, DIType Type,
612 bool isLocalToUnit,
Devang Patel854967e2008-12-17 22:39:29 +0000613 bool isDefinition,
614 const std::string *FileName,
615 const std::string *Directory) {
616
Chris Lattnera45664f2008-11-10 02:56:27 +0000617 Constant *Elts[] = {
618 GetTagConstant(dwarf::DW_TAG_subprogram),
Chris Lattner497a7a82008-11-10 04:10:34 +0000619 getCastToEmpty(GetOrCreateSubprogramAnchor()),
620 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000621 GetStringConstant(Name),
622 GetStringConstant(DisplayName),
623 GetStringConstant(LinkageName),
Chris Lattner497a7a82008-11-10 04:10:34 +0000624 getCastToEmpty(CompileUnit),
Chris Lattnera45664f2008-11-10 02:56:27 +0000625 ConstantInt::get(Type::Int32Ty, LineNo),
Chris Lattner497a7a82008-11-10 04:10:34 +0000626 getCastToEmpty(Type),
Chris Lattnera45664f2008-11-10 02:56:27 +0000627 ConstantInt::get(Type::Int1Ty, isLocalToUnit),
Devang Patel854967e2008-12-17 22:39:29 +0000628 ConstantInt::get(Type::Int1Ty, isDefinition),
629 GetStringConstant(FileName ? FileName->c_str() : ""),
630 GetStringConstant(Directory ? Directory->c_str() : "")
Chris Lattnera45664f2008-11-10 02:56:27 +0000631 };
632
633 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
634
635 M.addTypeName("llvm.dbg.subprogram.type", Init->getType());
636 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
637 GlobalValue::InternalLinkage,
638 Init, "llvm.dbg.subprogram", &M);
639 GV->setSection("llvm.metadata");
640 return DISubprogram(GV);
641}
642
643/// CreateGlobalVariable - Create a new descriptor for the specified global.
644DIGlobalVariable
645DIFactory::CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
646 const std::string &DisplayName,
647 const std::string &LinkageName,
648 DICompileUnit CompileUnit,
649 unsigned LineNo, DIType Type,bool isLocalToUnit,
Devang Patel854967e2008-12-17 22:39:29 +0000650 bool isDefinition, llvm::GlobalVariable *Val,
651 const std::string *FileName,
652 const std::string *Directory) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000653 Constant *Elts[] = {
654 GetTagConstant(dwarf::DW_TAG_variable),
Chris Lattner497a7a82008-11-10 04:10:34 +0000655 getCastToEmpty(GetOrCreateGlobalVariableAnchor()),
656 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000657 GetStringConstant(Name),
658 GetStringConstant(DisplayName),
659 GetStringConstant(LinkageName),
Chris Lattner497a7a82008-11-10 04:10:34 +0000660 getCastToEmpty(CompileUnit),
Chris Lattnera45664f2008-11-10 02:56:27 +0000661 ConstantInt::get(Type::Int32Ty, LineNo),
Chris Lattner497a7a82008-11-10 04:10:34 +0000662 getCastToEmpty(Type),
Chris Lattnera45664f2008-11-10 02:56:27 +0000663 ConstantInt::get(Type::Int1Ty, isLocalToUnit),
664 ConstantInt::get(Type::Int1Ty, isDefinition),
Devang Patel854967e2008-12-17 22:39:29 +0000665 ConstantExpr::getBitCast(Val, EmptyStructPtr),
666 GetStringConstant(FileName ? FileName->c_str() : ""),
667 GetStringConstant(Directory ? Directory->c_str() : "")
Chris Lattnera45664f2008-11-10 02:56:27 +0000668 };
669
670 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
671
672 M.addTypeName("llvm.dbg.global_variable.type", Init->getType());
673 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
674 GlobalValue::InternalLinkage,
675 Init, "llvm.dbg.global_variable", &M);
676 GV->setSection("llvm.metadata");
677 return DIGlobalVariable(GV);
678}
679
680
681/// CreateVariable - Create a new descriptor for the specified variable.
682DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
683 const std::string &Name,
684 DICompileUnit CompileUnit, unsigned LineNo,
Devang Patel854967e2008-12-17 22:39:29 +0000685 DIType Type,
686 const std::string *FileName,
687 const std::string *Directory) {
688
Chris Lattnera45664f2008-11-10 02:56:27 +0000689
690 Constant *Elts[] = {
691 GetTagConstant(Tag),
Chris Lattner497a7a82008-11-10 04:10:34 +0000692 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000693 GetStringConstant(Name),
Chris Lattner497a7a82008-11-10 04:10:34 +0000694 getCastToEmpty(CompileUnit),
Chris Lattnera45664f2008-11-10 02:56:27 +0000695 ConstantInt::get(Type::Int32Ty, LineNo),
Chris Lattner497a7a82008-11-10 04:10:34 +0000696 getCastToEmpty(Type),
Devang Patel854967e2008-12-17 22:39:29 +0000697 GetStringConstant(FileName ? FileName->c_str() : ""),
698 GetStringConstant(Directory ? Directory->c_str() : "")
Chris Lattnera45664f2008-11-10 02:56:27 +0000699 };
700
701 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
702
703 M.addTypeName("llvm.dbg.variable.type", Init->getType());
704 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
705 GlobalValue::InternalLinkage,
706 Init, "llvm.dbg.variable", &M);
707 GV->setSection("llvm.metadata");
708 return DIVariable(GV);
709}
710
711
712/// CreateBlock - This creates a descriptor for a lexical block with the
713/// specified parent context.
714DIBlock DIFactory::CreateBlock(DIDescriptor Context) {
715 Constant *Elts[] = {
716 GetTagConstant(dwarf::DW_TAG_lexical_block),
Chris Lattner497a7a82008-11-10 04:10:34 +0000717 getCastToEmpty(Context)
Chris Lattnera45664f2008-11-10 02:56:27 +0000718 };
719
720 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
721
722 M.addTypeName("llvm.dbg.block.type", Init->getType());
723 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
724 GlobalValue::InternalLinkage,
725 Init, "llvm.dbg.block", &M);
726 GV->setSection("llvm.metadata");
727 return DIBlock(GV);
728}
729
730
731//===----------------------------------------------------------------------===//
732// DIFactory: Routines for inserting code into a function
733//===----------------------------------------------------------------------===//
734
735/// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
736/// inserting it at the end of the specified basic block.
737void DIFactory::InsertStopPoint(DICompileUnit CU, unsigned LineNo,
738 unsigned ColNo, BasicBlock *BB) {
739
740 // Lazily construct llvm.dbg.stoppoint function.
741 if (!StopPointFn)
742 StopPointFn = llvm::Intrinsic::getDeclaration(&M,
743 llvm::Intrinsic::dbg_stoppoint);
744
745 // Invoke llvm.dbg.stoppoint
746 Value *Args[] = {
747 llvm::ConstantInt::get(llvm::Type::Int32Ty, LineNo),
748 llvm::ConstantInt::get(llvm::Type::Int32Ty, ColNo),
Chris Lattner497a7a82008-11-10 04:10:34 +0000749 getCastToEmpty(CU)
Chris Lattnera45664f2008-11-10 02:56:27 +0000750 };
751 CallInst::Create(StopPointFn, Args, Args+3, "", BB);
752}
753
754/// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
755/// mark the start of the specified subprogram.
756void DIFactory::InsertSubprogramStart(DISubprogram SP, BasicBlock *BB) {
757 // Lazily construct llvm.dbg.func.start.
758 if (!FuncStartFn)
759 FuncStartFn = llvm::Intrinsic::getDeclaration(&M,
760 llvm::Intrinsic::dbg_func_start);
761
762 // Call llvm.dbg.func.start which also implicitly sets a stoppoint.
Chris Lattner497a7a82008-11-10 04:10:34 +0000763 CallInst::Create(FuncStartFn, getCastToEmpty(SP), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000764}
765
766/// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
767/// mark the start of a region for the specified scoping descriptor.
768void DIFactory::InsertRegionStart(DIDescriptor D, BasicBlock *BB) {
769 // Lazily construct llvm.dbg.region.start function.
770 if (!RegionStartFn)
771 RegionStartFn = llvm::Intrinsic::getDeclaration(&M,
772 llvm::Intrinsic::dbg_region_start);
773 // Call llvm.dbg.func.start.
Chris Lattner497a7a82008-11-10 04:10:34 +0000774 CallInst::Create(RegionStartFn, getCastToEmpty(D), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000775}
776
777
778/// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
779/// mark the end of a region for the specified scoping descriptor.
780void DIFactory::InsertRegionEnd(DIDescriptor D, BasicBlock *BB) {
781 // Lazily construct llvm.dbg.region.end function.
782 if (!RegionEndFn)
783 RegionEndFn = llvm::Intrinsic::getDeclaration(&M,
784 llvm::Intrinsic::dbg_region_end);
785
Chris Lattner497a7a82008-11-10 04:10:34 +0000786 CallInst::Create(RegionEndFn, getCastToEmpty(D), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000787}
788
789/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
790void DIFactory::InsertDeclare(llvm::Value *Storage, DIVariable D,
791 BasicBlock *BB) {
792 // Cast the storage to a {}* for the call to llvm.dbg.declare.
Chris Lattner497a7a82008-11-10 04:10:34 +0000793 Storage = new llvm::BitCastInst(Storage, EmptyStructPtr, "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000794
795 if (!DeclareFn)
796 DeclareFn = llvm::Intrinsic::getDeclaration(&M,
797 llvm::Intrinsic::dbg_declare);
Chris Lattner497a7a82008-11-10 04:10:34 +0000798 Value *Args[] = { Storage, getCastToEmpty(D) };
Chris Lattnera45664f2008-11-10 02:56:27 +0000799 CallInst::Create(DeclareFn, Args, Args+2, "", BB);
800}
Torok Edwin620f2802008-12-16 09:07:36 +0000801
802namespace llvm {
803 /// Finds the stoppoint coressponding to this instruction, that is the
804 /// stoppoint that dominates this instruction
805 const DbgStopPointInst *findStopPoint(const Instruction *Inst)
806 {
807 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(Inst))
808 return DSI;
809
810 const BasicBlock *BB = Inst->getParent();
811 BasicBlock::const_iterator I = Inst, B;
812 do {
813 B = BB->begin();
814 // A BB consisting only of a terminator can't have a stoppoint.
815 if (I != B) {
816 do {
817 --I;
818 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
819 return DSI;
820 } while (I != B);
821 }
822 // This BB didn't have a stoppoint: if there is only one
823 // predecessor, look for a stoppoint there.
824 // We could use getIDom(), but that would require dominator info.
825 BB = I->getParent()->getUniquePredecessor();
826 if (BB)
827 I = BB->getTerminator();
828 } while (BB != 0);
829 return 0;
830 }
831
832 /// Finds the stoppoint corresponding to first real (non-debug intrinsic)
833 /// instruction in this Basic Block, and returns the stoppoint for it.
834 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB)
835 {
836 for(BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
837 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
838 return DSI;
839 }
840 // Fallback to looking for stoppoint of unique predecessor.
841 // Useful if this BB contains no stoppoints, but unique predecessor does.
842 BB = BB->getUniquePredecessor();
843 if (BB)
844 return findStopPoint(BB->getTerminator());
845 return 0;
846 }
847
848 /// Finds the dbg.declare intrinsic corresponding to this value if any.
849 /// It looks through pointer casts too.
850 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts)
851 {
852 if (stripCasts) {
853 V = V->stripPointerCasts();
854 // Look for the bitcast.
855 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
856 I != E; ++I) {
857 if (isa<BitCastInst>(I))
858 return findDbgDeclare(*I, false);
859 }
860 return 0;
861 }
862
863 // Find dbg.declare among uses of the instruction.
864 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
865 I != E; ++I) {
866 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I))
867 return DDI;
868 }
869 return 0;
870 }
871}
872