blob: deb5c9bbfb481b8e674ac7612bb9dc947b40a598 [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"
Devang Patelbf3f5a02009-01-30 01:03:10 +000023#include "llvm/Support/Streams.h"
Chris Lattnera45664f2008-11-10 02:56:27 +000024using 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.
Devang Patel486938f2009-01-12 21:38:43 +0000115bool DIType::isDerivedType(unsigned Tag) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000116 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.
Devang Patel486938f2009-01-12 21:38:43 +0000140bool DIType::isCompositeType(unsigned TAG) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000141 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
Devang Patel36375ee2009-02-17 21:23:59 +0000172DIVariable::DIVariable(GlobalVariable *gv) : DIDescriptor(gv) {
173 if (gv && !isVariable(getTag()))
Chris Lattnera45664f2008-11-10 02:56:27 +0000174 GV = 0;
175}
176
Devang Patel68afdc32009-01-05 18:33:01 +0000177unsigned DIArray::getNumElements() const {
178 assert (GV && "Invalid DIArray");
179 Constant *C = GV->getInitializer();
180 assert (C && "Invalid DIArray initializer");
181 return C->getNumOperands();
182}
Chris Lattnera45664f2008-11-10 02:56:27 +0000183
Devang Patelb79b5352009-01-19 23:21:49 +0000184/// Verify - Verify that a compile unit is well formed.
185bool DICompileUnit::Verify() const {
186 if (isNull())
187 return false;
188 if (getFilename().empty())
189 return false;
190 // It is possible that directory and produce string is empty.
191 return true;
192}
193
194/// Verify - Verify that a type descriptor is well formed.
195bool DIType::Verify() const {
196 if (isNull())
197 return false;
198 if (getContext().isNull())
199 return false;
200
201 DICompileUnit CU = getCompileUnit();
202 if (!CU.isNull() && !CU.Verify())
203 return false;
204 return true;
205}
206
207/// Verify - Verify that a composite type descriptor is well formed.
208bool DICompositeType::Verify() const {
209 if (isNull())
210 return false;
211 if (getContext().isNull())
212 return false;
213
214 DICompileUnit CU = getCompileUnit();
215 if (!CU.isNull() && !CU.Verify())
216 return false;
217 return true;
218}
219
220/// Verify - Verify that a subprogram descriptor is well formed.
221bool DISubprogram::Verify() const {
222 if (isNull())
223 return false;
224
225 if (getContext().isNull())
226 return false;
227
228 DICompileUnit CU = getCompileUnit();
229 if (!CU.Verify())
230 return false;
231
232 DICompositeType Ty = getType();
233 if (!Ty.isNull() && !Ty.Verify())
234 return false;
235 return true;
236}
237
238/// Verify - Verify that a global variable descriptor is well formed.
239bool DIGlobalVariable::Verify() const {
240 if (isNull())
241 return false;
242
243 if (getContext().isNull())
244 return false;
245
246 DICompileUnit CU = getCompileUnit();
247 if (!CU.Verify())
248 return false;
249
250 DIType Ty = getType();
251 if (!Ty.Verify())
252 return false;
253
254 if (!getGlobal())
255 return false;
256
257 return true;
258}
259
260/// Verify - Verify that a variable descriptor is well formed.
261bool DIVariable::Verify() const {
262 if (isNull())
263 return false;
264
265 if (getContext().isNull())
266 return false;
267
268 DIType Ty = getType();
269 if (!Ty.Verify())
270 return false;
271
272
273 return true;
274}
275
Devang Patel36375ee2009-02-17 21:23:59 +0000276/// getOriginalTypeSize - If this type is derived from a base type then
277/// return base type size.
278uint64_t DIDerivedType::getOriginalTypeSize() const {
279 if (getTag() != dwarf::DW_TAG_member)
280 return getSizeInBits();
281 DIType BT = getTypeDerivedFrom();
282 if (BT.getTag() != dwarf::DW_TAG_base_type)
283 return getSizeInBits();
284 return BT.getSizeInBits();
285}
Devang Patelb79b5352009-01-19 23:21:49 +0000286
Chris Lattnera45664f2008-11-10 02:56:27 +0000287//===----------------------------------------------------------------------===//
288// DIFactory: Basic Helpers
289//===----------------------------------------------------------------------===//
290
Chris Lattner497a7a82008-11-10 04:10:34 +0000291DIFactory::DIFactory(Module &m) : M(m) {
292 StopPointFn = FuncStartFn = RegionStartFn = RegionEndFn = DeclareFn = 0;
293 EmptyStructPtr = PointerType::getUnqual(StructType::get(NULL, NULL));
294}
295
296/// getCastToEmpty - Return this descriptor as a Constant* with type '{}*'.
297/// This is only valid when the descriptor is non-null.
298Constant *DIFactory::getCastToEmpty(DIDescriptor D) {
299 if (D.isNull()) return Constant::getNullValue(EmptyStructPtr);
300 return ConstantExpr::getBitCast(D.getGV(), EmptyStructPtr);
301}
302
Chris Lattnera45664f2008-11-10 02:56:27 +0000303Constant *DIFactory::GetTagConstant(unsigned TAG) {
Devang Patel6906ba52009-01-20 19:22:03 +0000304 assert((TAG & LLVMDebugVersionMask) == 0 &&
Chris Lattnera45664f2008-11-10 02:56:27 +0000305 "Tag too large for debug encoding!");
Devang Patel6906ba52009-01-20 19:22:03 +0000306 return ConstantInt::get(Type::Int32Ty, TAG | LLVMDebugVersion);
Chris Lattnera45664f2008-11-10 02:56:27 +0000307}
308
309Constant *DIFactory::GetStringConstant(const std::string &String) {
310 // Check string cache for previous edition.
311 Constant *&Slot = StringCache[String];
312
313 // Return Constant if previously defined.
314 if (Slot) return Slot;
315
316 const PointerType *DestTy = PointerType::getUnqual(Type::Int8Ty);
317
318 // If empty string then use a sbyte* null instead.
319 if (String.empty())
320 return Slot = ConstantPointerNull::get(DestTy);
321
322 // Construct string as an llvm constant.
323 Constant *ConstStr = ConstantArray::get(String);
324
325 // Otherwise create and return a new string global.
326 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
327 GlobalVariable::InternalLinkage,
328 ConstStr, ".str", &M);
329 StrGV->setSection("llvm.metadata");
330 return Slot = ConstantExpr::getBitCast(StrGV, DestTy);
331}
332
333/// GetOrCreateAnchor - Look up an anchor for the specified tag and name. If it
334/// already exists, return it. If not, create a new one and return it.
335DIAnchor DIFactory::GetOrCreateAnchor(unsigned TAG, const char *Name) {
Chris Lattner497a7a82008-11-10 04:10:34 +0000336 const Type *EltTy = StructType::get(Type::Int32Ty, Type::Int32Ty, NULL);
Chris Lattnera45664f2008-11-10 02:56:27 +0000337
338 // Otherwise, create the global or return it if already in the module.
339 Constant *C = M.getOrInsertGlobal(Name, EltTy);
340 assert(isa<GlobalVariable>(C) && "Incorrectly typed anchor?");
341 GlobalVariable *GV = cast<GlobalVariable>(C);
342
343 // If it has an initializer, it is already in the module.
344 if (GV->hasInitializer())
345 return SubProgramAnchor = DIAnchor(GV);
346
347 GV->setLinkage(GlobalValue::LinkOnceLinkage);
348 GV->setSection("llvm.metadata");
349 GV->setConstant(true);
350 M.addTypeName("llvm.dbg.anchor.type", EltTy);
351
352 // Otherwise, set the initializer.
353 Constant *Elts[] = {
354 GetTagConstant(dwarf::DW_TAG_anchor),
355 ConstantInt::get(Type::Int32Ty, TAG)
356 };
357
358 GV->setInitializer(ConstantStruct::get(Elts, 2));
359 return DIAnchor(GV);
360}
361
362
363
364//===----------------------------------------------------------------------===//
365// DIFactory: Primary Constructors
366//===----------------------------------------------------------------------===//
367
368/// GetOrCreateCompileUnitAnchor - Return the anchor for compile units,
369/// creating a new one if there isn't already one in the module.
370DIAnchor DIFactory::GetOrCreateCompileUnitAnchor() {
371 // If we already created one, just return it.
372 if (!CompileUnitAnchor.isNull())
373 return CompileUnitAnchor;
374 return CompileUnitAnchor = GetOrCreateAnchor(dwarf::DW_TAG_compile_unit,
375 "llvm.dbg.compile_units");
376}
377
378/// GetOrCreateSubprogramAnchor - Return the anchor for subprograms,
379/// creating a new one if there isn't already one in the module.
380DIAnchor DIFactory::GetOrCreateSubprogramAnchor() {
381 // If we already created one, just return it.
382 if (!SubProgramAnchor.isNull())
383 return SubProgramAnchor;
384 return SubProgramAnchor = GetOrCreateAnchor(dwarf::DW_TAG_subprogram,
385 "llvm.dbg.subprograms");
386}
387
388/// GetOrCreateGlobalVariableAnchor - Return the anchor for globals,
389/// creating a new one if there isn't already one in the module.
390DIAnchor DIFactory::GetOrCreateGlobalVariableAnchor() {
391 // If we already created one, just return it.
392 if (!GlobalVariableAnchor.isNull())
393 return GlobalVariableAnchor;
394 return GlobalVariableAnchor = GetOrCreateAnchor(dwarf::DW_TAG_variable,
395 "llvm.dbg.global_variables");
396}
397
398/// GetOrCreateArray - Create an descriptor for an array of descriptors.
399/// This implicitly uniques the arrays created.
400DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
401 SmallVector<Constant*, 16> Elts;
402
403 for (unsigned i = 0; i != NumTys; ++i)
Chris Lattner497a7a82008-11-10 04:10:34 +0000404 Elts.push_back(getCastToEmpty(Tys[i]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000405
Chris Lattner497a7a82008-11-10 04:10:34 +0000406 Constant *Init = ConstantArray::get(ArrayType::get(EmptyStructPtr,
407 Elts.size()),
Chris Lattnera45664f2008-11-10 02:56:27 +0000408 &Elts[0], Elts.size());
409 // If we already have this array, just return the uniqued version.
410 DIDescriptor &Entry = SimpleConstantCache[Init];
411 if (!Entry.isNull()) return DIArray(Entry.getGV());
412
413 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
414 GlobalValue::InternalLinkage,
415 Init, "llvm.dbg.array", &M);
416 GV->setSection("llvm.metadata");
417 Entry = DIDescriptor(GV);
418 return DIArray(GV);
419}
420
421/// GetOrCreateSubrange - Create a descriptor for a value range. This
422/// implicitly uniques the values returned.
423DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
424 Constant *Elts[] = {
425 GetTagConstant(dwarf::DW_TAG_subrange_type),
426 ConstantInt::get(Type::Int64Ty, Lo),
427 ConstantInt::get(Type::Int64Ty, Hi)
428 };
429
430 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
431
432 // If we already have this range, just return the uniqued version.
433 DIDescriptor &Entry = SimpleConstantCache[Init];
434 if (!Entry.isNull()) return DISubrange(Entry.getGV());
435
436 M.addTypeName("llvm.dbg.subrange.type", Init->getType());
437
438 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
439 GlobalValue::InternalLinkage,
440 Init, "llvm.dbg.subrange", &M);
441 GV->setSection("llvm.metadata");
442 Entry = DIDescriptor(GV);
443 return DISubrange(GV);
444}
445
446
447
448/// CreateCompileUnit - Create a new descriptor for the specified compile
449/// unit. Note that this does not unique compile units within the module.
450DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
451 const std::string &Filename,
452 const std::string &Directory,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000453 const std::string &Producer,
Devang Pateldd9db662009-01-30 18:20:31 +0000454 bool isMain,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000455 bool isOptimized,
Devang Patel13319ce2009-02-17 22:43:44 +0000456 const char *Flags,
457 unsigned RunTimeVer) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000458 Constant *Elts[] = {
459 GetTagConstant(dwarf::DW_TAG_compile_unit),
Chris Lattner497a7a82008-11-10 04:10:34 +0000460 getCastToEmpty(GetOrCreateCompileUnitAnchor()),
Chris Lattnera45664f2008-11-10 02:56:27 +0000461 ConstantInt::get(Type::Int32Ty, LangID),
462 GetStringConstant(Filename),
463 GetStringConstant(Directory),
Devang Patel3b64c6b2009-01-23 22:33:47 +0000464 GetStringConstant(Producer),
Devang Pateldd9db662009-01-30 18:20:31 +0000465 ConstantInt::get(Type::Int1Ty, isMain),
Devang Patel3b64c6b2009-01-23 22:33:47 +0000466 ConstantInt::get(Type::Int1Ty, isOptimized),
Devang Patel13319ce2009-02-17 22:43:44 +0000467 GetStringConstant(Flags),
468 ConstantInt::get(Type::Int32Ty, RunTimeVer)
Chris Lattnera45664f2008-11-10 02:56:27 +0000469 };
470
471 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
472
473 M.addTypeName("llvm.dbg.compile_unit.type", Init->getType());
474 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
475 GlobalValue::InternalLinkage,
476 Init, "llvm.dbg.compile_unit", &M);
477 GV->setSection("llvm.metadata");
478 return DICompileUnit(GV);
479}
480
481/// CreateEnumerator - Create a single enumerator value.
482DIEnumerator DIFactory::CreateEnumerator(const std::string &Name, uint64_t Val){
483 Constant *Elts[] = {
484 GetTagConstant(dwarf::DW_TAG_enumerator),
485 GetStringConstant(Name),
486 ConstantInt::get(Type::Int64Ty, Val)
487 };
488
489 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
490
491 M.addTypeName("llvm.dbg.enumerator.type", Init->getType());
492 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
493 GlobalValue::InternalLinkage,
494 Init, "llvm.dbg.enumerator", &M);
495 GV->setSection("llvm.metadata");
496 return DIEnumerator(GV);
497}
498
499
500/// CreateBasicType - Create a basic type like int, float, etc.
501DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
502 const std::string &Name,
503 DICompileUnit CompileUnit,
504 unsigned LineNumber,
505 uint64_t SizeInBits,
506 uint64_t AlignInBits,
507 uint64_t OffsetInBits, unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000508 unsigned Encoding) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000509 Constant *Elts[] = {
510 GetTagConstant(dwarf::DW_TAG_base_type),
Chris Lattner497a7a82008-11-10 04:10:34 +0000511 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000512 GetStringConstant(Name),
Chris Lattner497a7a82008-11-10 04:10:34 +0000513 getCastToEmpty(CompileUnit),
Chris Lattnera45664f2008-11-10 02:56:27 +0000514 ConstantInt::get(Type::Int32Ty, LineNumber),
515 ConstantInt::get(Type::Int64Ty, SizeInBits),
516 ConstantInt::get(Type::Int64Ty, AlignInBits),
517 ConstantInt::get(Type::Int64Ty, OffsetInBits),
518 ConstantInt::get(Type::Int32Ty, Flags),
Devang Pateldd9db662009-01-30 18:20:31 +0000519 ConstantInt::get(Type::Int32Ty, Encoding)
Chris Lattnera45664f2008-11-10 02:56:27 +0000520 };
521
522 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
523
524 M.addTypeName("llvm.dbg.basictype.type", Init->getType());
525 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
526 GlobalValue::InternalLinkage,
527 Init, "llvm.dbg.basictype", &M);
528 GV->setSection("llvm.metadata");
529 return DIBasicType(GV);
530}
531
532/// CreateDerivedType - Create a derived type like const qualified type,
533/// pointer, typedef, etc.
534DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
535 DIDescriptor Context,
536 const std::string &Name,
537 DICompileUnit CompileUnit,
538 unsigned LineNumber,
539 uint64_t SizeInBits,
540 uint64_t AlignInBits,
541 uint64_t OffsetInBits,
542 unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000543 DIType DerivedFrom) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000544 Constant *Elts[] = {
545 GetTagConstant(Tag),
Chris Lattner497a7a82008-11-10 04:10:34 +0000546 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000547 GetStringConstant(Name),
Chris Lattner497a7a82008-11-10 04:10:34 +0000548 getCastToEmpty(CompileUnit),
Chris Lattnera45664f2008-11-10 02:56:27 +0000549 ConstantInt::get(Type::Int32Ty, LineNumber),
550 ConstantInt::get(Type::Int64Ty, SizeInBits),
551 ConstantInt::get(Type::Int64Ty, AlignInBits),
552 ConstantInt::get(Type::Int64Ty, OffsetInBits),
553 ConstantInt::get(Type::Int32Ty, Flags),
Devang Pateldd9db662009-01-30 18:20:31 +0000554 getCastToEmpty(DerivedFrom)
Chris Lattnera45664f2008-11-10 02:56:27 +0000555 };
556
557 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
558
559 M.addTypeName("llvm.dbg.derivedtype.type", Init->getType());
560 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
561 GlobalValue::InternalLinkage,
562 Init, "llvm.dbg.derivedtype", &M);
563 GV->setSection("llvm.metadata");
564 return DIDerivedType(GV);
565}
566
567/// CreateCompositeType - Create a composite type like array, struct, etc.
568DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
569 DIDescriptor Context,
570 const std::string &Name,
571 DICompileUnit CompileUnit,
572 unsigned LineNumber,
573 uint64_t SizeInBits,
574 uint64_t AlignInBits,
575 uint64_t OffsetInBits,
576 unsigned Flags,
577 DIType DerivedFrom,
Devang Patel13319ce2009-02-17 22:43:44 +0000578 DIArray Elements,
579 unsigned RuntimeLang) {
Devang Patel854967e2008-12-17 22:39:29 +0000580
Chris Lattnera45664f2008-11-10 02:56:27 +0000581 Constant *Elts[] = {
582 GetTagConstant(Tag),
Chris Lattner497a7a82008-11-10 04:10:34 +0000583 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000584 GetStringConstant(Name),
Chris Lattner497a7a82008-11-10 04:10:34 +0000585 getCastToEmpty(CompileUnit),
Chris Lattnera45664f2008-11-10 02:56:27 +0000586 ConstantInt::get(Type::Int32Ty, LineNumber),
587 ConstantInt::get(Type::Int64Ty, SizeInBits),
588 ConstantInt::get(Type::Int64Ty, AlignInBits),
589 ConstantInt::get(Type::Int64Ty, OffsetInBits),
590 ConstantInt::get(Type::Int32Ty, Flags),
Chris Lattner497a7a82008-11-10 04:10:34 +0000591 getCastToEmpty(DerivedFrom),
Devang Patel13319ce2009-02-17 22:43:44 +0000592 getCastToEmpty(Elements),
593 ConstantInt::get(Type::Int32Ty, RuntimeLang)
Chris Lattnera45664f2008-11-10 02:56:27 +0000594 };
595
596 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
597
598 M.addTypeName("llvm.dbg.composite.type", Init->getType());
599 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
600 GlobalValue::InternalLinkage,
601 Init, "llvm.dbg.composite", &M);
602 GV->setSection("llvm.metadata");
603 return DICompositeType(GV);
604}
605
606
607/// CreateSubprogram - Create a new descriptor for the specified subprogram.
608/// See comments in DISubprogram for descriptions of these fields. This
609/// method does not unique the generated descriptors.
610DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
611 const std::string &Name,
612 const std::string &DisplayName,
613 const std::string &LinkageName,
614 DICompileUnit CompileUnit,
615 unsigned LineNo, DIType Type,
616 bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000617 bool isDefinition) {
Devang Patel854967e2008-12-17 22:39:29 +0000618
Chris Lattnera45664f2008-11-10 02:56:27 +0000619 Constant *Elts[] = {
620 GetTagConstant(dwarf::DW_TAG_subprogram),
Chris Lattner497a7a82008-11-10 04:10:34 +0000621 getCastToEmpty(GetOrCreateSubprogramAnchor()),
622 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000623 GetStringConstant(Name),
624 GetStringConstant(DisplayName),
625 GetStringConstant(LinkageName),
Chris Lattner497a7a82008-11-10 04:10:34 +0000626 getCastToEmpty(CompileUnit),
Chris Lattnera45664f2008-11-10 02:56:27 +0000627 ConstantInt::get(Type::Int32Ty, LineNo),
Chris Lattner497a7a82008-11-10 04:10:34 +0000628 getCastToEmpty(Type),
Chris Lattnera45664f2008-11-10 02:56:27 +0000629 ConstantInt::get(Type::Int1Ty, isLocalToUnit),
Devang Pateldd9db662009-01-30 18:20:31 +0000630 ConstantInt::get(Type::Int1Ty, isDefinition)
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 Pateldd9db662009-01-30 18:20:31 +0000650 bool isDefinition, llvm::GlobalVariable *Val) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000651 Constant *Elts[] = {
652 GetTagConstant(dwarf::DW_TAG_variable),
Chris Lattner497a7a82008-11-10 04:10:34 +0000653 getCastToEmpty(GetOrCreateGlobalVariableAnchor()),
654 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000655 GetStringConstant(Name),
656 GetStringConstant(DisplayName),
657 GetStringConstant(LinkageName),
Chris Lattner497a7a82008-11-10 04:10:34 +0000658 getCastToEmpty(CompileUnit),
Chris Lattnera45664f2008-11-10 02:56:27 +0000659 ConstantInt::get(Type::Int32Ty, LineNo),
Chris Lattner497a7a82008-11-10 04:10:34 +0000660 getCastToEmpty(Type),
Chris Lattnera45664f2008-11-10 02:56:27 +0000661 ConstantInt::get(Type::Int1Ty, isLocalToUnit),
662 ConstantInt::get(Type::Int1Ty, isDefinition),
Devang Pateldd9db662009-01-30 18:20:31 +0000663 ConstantExpr::getBitCast(Val, EmptyStructPtr)
Chris Lattnera45664f2008-11-10 02:56:27 +0000664 };
665
666 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
667
668 M.addTypeName("llvm.dbg.global_variable.type", Init->getType());
669 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
670 GlobalValue::InternalLinkage,
671 Init, "llvm.dbg.global_variable", &M);
672 GV->setSection("llvm.metadata");
673 return DIGlobalVariable(GV);
674}
675
676
677/// CreateVariable - Create a new descriptor for the specified variable.
678DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
679 const std::string &Name,
680 DICompileUnit CompileUnit, unsigned LineNo,
Devang Pateldd9db662009-01-30 18:20:31 +0000681 DIType Type) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000682 Constant *Elts[] = {
683 GetTagConstant(Tag),
Chris Lattner497a7a82008-11-10 04:10:34 +0000684 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000685 GetStringConstant(Name),
Chris Lattner497a7a82008-11-10 04:10:34 +0000686 getCastToEmpty(CompileUnit),
Chris Lattnera45664f2008-11-10 02:56:27 +0000687 ConstantInt::get(Type::Int32Ty, LineNo),
Devang Pateldd9db662009-01-30 18:20:31 +0000688 getCastToEmpty(Type)
Chris Lattnera45664f2008-11-10 02:56:27 +0000689 };
690
691 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
692
693 M.addTypeName("llvm.dbg.variable.type", Init->getType());
694 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
695 GlobalValue::InternalLinkage,
696 Init, "llvm.dbg.variable", &M);
697 GV->setSection("llvm.metadata");
698 return DIVariable(GV);
699}
700
701
702/// CreateBlock - This creates a descriptor for a lexical block with the
703/// specified parent context.
704DIBlock DIFactory::CreateBlock(DIDescriptor Context) {
705 Constant *Elts[] = {
706 GetTagConstant(dwarf::DW_TAG_lexical_block),
Chris Lattner497a7a82008-11-10 04:10:34 +0000707 getCastToEmpty(Context)
Chris Lattnera45664f2008-11-10 02:56:27 +0000708 };
709
710 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
711
712 M.addTypeName("llvm.dbg.block.type", Init->getType());
713 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
714 GlobalValue::InternalLinkage,
715 Init, "llvm.dbg.block", &M);
716 GV->setSection("llvm.metadata");
717 return DIBlock(GV);
718}
719
720
721//===----------------------------------------------------------------------===//
722// DIFactory: Routines for inserting code into a function
723//===----------------------------------------------------------------------===//
724
725/// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
726/// inserting it at the end of the specified basic block.
727void DIFactory::InsertStopPoint(DICompileUnit CU, unsigned LineNo,
728 unsigned ColNo, BasicBlock *BB) {
729
730 // Lazily construct llvm.dbg.stoppoint function.
731 if (!StopPointFn)
732 StopPointFn = llvm::Intrinsic::getDeclaration(&M,
733 llvm::Intrinsic::dbg_stoppoint);
734
735 // Invoke llvm.dbg.stoppoint
736 Value *Args[] = {
737 llvm::ConstantInt::get(llvm::Type::Int32Ty, LineNo),
738 llvm::ConstantInt::get(llvm::Type::Int32Ty, ColNo),
Chris Lattner497a7a82008-11-10 04:10:34 +0000739 getCastToEmpty(CU)
Chris Lattnera45664f2008-11-10 02:56:27 +0000740 };
741 CallInst::Create(StopPointFn, Args, Args+3, "", BB);
742}
743
744/// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
745/// mark the start of the specified subprogram.
746void DIFactory::InsertSubprogramStart(DISubprogram SP, BasicBlock *BB) {
747 // Lazily construct llvm.dbg.func.start.
748 if (!FuncStartFn)
749 FuncStartFn = llvm::Intrinsic::getDeclaration(&M,
750 llvm::Intrinsic::dbg_func_start);
751
752 // Call llvm.dbg.func.start which also implicitly sets a stoppoint.
Chris Lattner497a7a82008-11-10 04:10:34 +0000753 CallInst::Create(FuncStartFn, getCastToEmpty(SP), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000754}
755
756/// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
757/// mark the start of a region for the specified scoping descriptor.
758void DIFactory::InsertRegionStart(DIDescriptor D, BasicBlock *BB) {
759 // Lazily construct llvm.dbg.region.start function.
760 if (!RegionStartFn)
761 RegionStartFn = llvm::Intrinsic::getDeclaration(&M,
762 llvm::Intrinsic::dbg_region_start);
763 // Call llvm.dbg.func.start.
Chris Lattner497a7a82008-11-10 04:10:34 +0000764 CallInst::Create(RegionStartFn, getCastToEmpty(D), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000765}
766
767
768/// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
769/// mark the end of a region for the specified scoping descriptor.
770void DIFactory::InsertRegionEnd(DIDescriptor D, BasicBlock *BB) {
771 // Lazily construct llvm.dbg.region.end function.
772 if (!RegionEndFn)
773 RegionEndFn = llvm::Intrinsic::getDeclaration(&M,
774 llvm::Intrinsic::dbg_region_end);
775
Chris Lattner497a7a82008-11-10 04:10:34 +0000776 CallInst::Create(RegionEndFn, getCastToEmpty(D), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000777}
778
779/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
780void DIFactory::InsertDeclare(llvm::Value *Storage, DIVariable D,
781 BasicBlock *BB) {
782 // Cast the storage to a {}* for the call to llvm.dbg.declare.
Chris Lattner497a7a82008-11-10 04:10:34 +0000783 Storage = new llvm::BitCastInst(Storage, EmptyStructPtr, "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000784
785 if (!DeclareFn)
786 DeclareFn = llvm::Intrinsic::getDeclaration(&M,
787 llvm::Intrinsic::dbg_declare);
Chris Lattner497a7a82008-11-10 04:10:34 +0000788 Value *Args[] = { Storage, getCastToEmpty(D) };
Chris Lattnera45664f2008-11-10 02:56:27 +0000789 CallInst::Create(DeclareFn, Args, Args+2, "", BB);
790}
Torok Edwin620f2802008-12-16 09:07:36 +0000791
792namespace llvm {
793 /// Finds the stoppoint coressponding to this instruction, that is the
794 /// stoppoint that dominates this instruction
795 const DbgStopPointInst *findStopPoint(const Instruction *Inst)
796 {
797 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(Inst))
798 return DSI;
799
800 const BasicBlock *BB = Inst->getParent();
801 BasicBlock::const_iterator I = Inst, B;
802 do {
803 B = BB->begin();
804 // A BB consisting only of a terminator can't have a stoppoint.
805 if (I != B) {
806 do {
807 --I;
808 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
809 return DSI;
810 } while (I != B);
811 }
812 // This BB didn't have a stoppoint: if there is only one
813 // predecessor, look for a stoppoint there.
814 // We could use getIDom(), but that would require dominator info.
815 BB = I->getParent()->getUniquePredecessor();
816 if (BB)
817 I = BB->getTerminator();
818 } while (BB != 0);
819 return 0;
820 }
821
822 /// Finds the stoppoint corresponding to first real (non-debug intrinsic)
823 /// instruction in this Basic Block, and returns the stoppoint for it.
824 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB)
825 {
826 for(BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
827 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
828 return DSI;
829 }
830 // Fallback to looking for stoppoint of unique predecessor.
831 // Useful if this BB contains no stoppoints, but unique predecessor does.
832 BB = BB->getUniquePredecessor();
833 if (BB)
834 return findStopPoint(BB->getTerminator());
835 return 0;
836 }
837
838 /// Finds the dbg.declare intrinsic corresponding to this value if any.
839 /// It looks through pointer casts too.
840 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts)
841 {
842 if (stripCasts) {
843 V = V->stripPointerCasts();
844 // Look for the bitcast.
845 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
846 I != E; ++I) {
847 if (isa<BitCastInst>(I))
848 return findDbgDeclare(*I, false);
849 }
850 return 0;
851 }
852
853 // Find dbg.declare among uses of the instruction.
854 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
855 I != E; ++I) {
856 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I))
857 return DDI;
858 }
859 return 0;
860 }
861}
862
Devang Patelbf3f5a02009-01-30 01:03:10 +0000863/// dump - print compile unit.
864void DICompileUnit::dump() const {
Devang Pateld8e880c2009-02-24 23:15:09 +0000865 if (getLanguage())
866 cerr << " [" << dwarf::LanguageString(getLanguage()) << "] ";
Devang Patelbf3f5a02009-01-30 01:03:10 +0000867 cerr << " [" << getDirectory() << "/" << getFilename() << " ]";
868}
869
870/// dump - print type.
871void DIType::dump() const {
872 if (isNull()) return;
873 if (!getName().empty())
874 cerr << " [" << getName() << "] ";
875 unsigned Tag = getTag();
876 cerr << " [" << dwarf::TagString(Tag) << "] ";
877 // TODO : Print context
878 getCompileUnit().dump();
879 cerr << " ["
880 << getLineNumber() << ", "
881 << getSizeInBits() << ", "
882 << getAlignInBits() << ", "
883 << getOffsetInBits()
884 << "] ";
885 if (isPrivate())
886 cerr << " [private] ";
887 else if (isProtected())
888 cerr << " [protected] ";
889 if (isForwardDecl())
890 cerr << " [fwd] ";
891
892 if (isBasicType(Tag))
893 DIBasicType(GV).dump();
894 else if (isDerivedType(Tag))
895 DIDerivedType(GV).dump();
896 else if (isCompositeType(Tag))
897 DICompositeType(GV).dump();
898 else {
899 cerr << "Invalid DIType\n";
900 return;
901 }
902 cerr << "\n";
903}
904
905/// dump - print basic type.
906void DIBasicType::dump() const {
907 cerr << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
908
909}
910
911/// dump - print derived type.
912void DIDerivedType::dump() const {
913 cerr << "\n\t Derived From: "; getTypeDerivedFrom().dump();
914}
915
916/// dump - print composite type.
917void DICompositeType::dump() const {
918 DIArray A = getTypeArray();
919 if (A.isNull())
920 return;
921 cerr << " [" << A.getNumElements() << " elements]";
922}
923
924/// dump - print global.
925void DIGlobal::dump() const {
926
927 if (!getName().empty())
928 cerr << " [" << getName() << "] ";
929 unsigned Tag = getTag();
930 cerr << " [" << dwarf::TagString(Tag) << "] ";
931 // TODO : Print context
932 getCompileUnit().dump();
933 cerr << " [" << getLineNumber() << "] ";
934 if (isLocalToUnit())
935 cerr << " [local] ";
936 if (isDefinition())
937 cerr << " [def] ";
938
939 if (isGlobalVariable(Tag))
940 DIGlobalVariable(GV).dump();
941
942 cerr << "\n";
943}
944
945/// dump - print subprogram.
946void DISubprogram::dump() const {
947 DIGlobal::dump();
948}
949
950/// dump - print global variable.
951void DIGlobalVariable::dump() const {
952 cerr << " ["; getGlobal()->dump(); cerr << "] ";
953}
954
955/// dump - print variable.
956void DIVariable::dump() const {
957 if (!getName().empty())
958 cerr << " [" << getName() << "] ";
959 getCompileUnit().dump();
960 cerr << " [" << getLineNumber() << "] ";
961 getType().dump();
962 cerr << "\n";
963}