blob: 29175a6a2d4e8bbf7f22182d2fde04a47636f83d [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"
19#include "llvm/Instructions.h"
20#include "llvm/Module.h"
21#include "llvm/Analysis/ValueTracking.h"
22#include "llvm/Support/Dwarf.h"
23using 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()
104DIType::DIType(GlobalVariable *GV) : DIDescriptor(GV) {
105 if (!GV) return;
106 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.
114bool DIDerivedType::isDerivedType(unsigned Tag) {
115 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.
139bool DICompositeType::isCompositeType(unsigned TAG) {
140 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
176
177
178//===----------------------------------------------------------------------===//
179// DIFactory: Basic Helpers
180//===----------------------------------------------------------------------===//
181
Chris Lattner497a7a82008-11-10 04:10:34 +0000182DIFactory::DIFactory(Module &m) : M(m) {
183 StopPointFn = FuncStartFn = RegionStartFn = RegionEndFn = DeclareFn = 0;
184 EmptyStructPtr = PointerType::getUnqual(StructType::get(NULL, NULL));
185}
186
187/// getCastToEmpty - Return this descriptor as a Constant* with type '{}*'.
188/// This is only valid when the descriptor is non-null.
189Constant *DIFactory::getCastToEmpty(DIDescriptor D) {
190 if (D.isNull()) return Constant::getNullValue(EmptyStructPtr);
191 return ConstantExpr::getBitCast(D.getGV(), EmptyStructPtr);
192}
193
Chris Lattnera45664f2008-11-10 02:56:27 +0000194Constant *DIFactory::GetTagConstant(unsigned TAG) {
195 assert((TAG & DIDescriptor::VersionMask) == 0 &&
196 "Tag too large for debug encoding!");
197 return ConstantInt::get(Type::Int32Ty, TAG | DIDescriptor::Version6);
198}
199
200Constant *DIFactory::GetStringConstant(const std::string &String) {
201 // Check string cache for previous edition.
202 Constant *&Slot = StringCache[String];
203
204 // Return Constant if previously defined.
205 if (Slot) return Slot;
206
207 const PointerType *DestTy = PointerType::getUnqual(Type::Int8Ty);
208
209 // If empty string then use a sbyte* null instead.
210 if (String.empty())
211 return Slot = ConstantPointerNull::get(DestTy);
212
213 // Construct string as an llvm constant.
214 Constant *ConstStr = ConstantArray::get(String);
215
216 // Otherwise create and return a new string global.
217 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
218 GlobalVariable::InternalLinkage,
219 ConstStr, ".str", &M);
220 StrGV->setSection("llvm.metadata");
221 return Slot = ConstantExpr::getBitCast(StrGV, DestTy);
222}
223
224/// GetOrCreateAnchor - Look up an anchor for the specified tag and name. If it
225/// already exists, return it. If not, create a new one and return it.
226DIAnchor DIFactory::GetOrCreateAnchor(unsigned TAG, const char *Name) {
Chris Lattner497a7a82008-11-10 04:10:34 +0000227 const Type *EltTy = StructType::get(Type::Int32Ty, Type::Int32Ty, NULL);
Chris Lattnera45664f2008-11-10 02:56:27 +0000228
229 // Otherwise, create the global or return it if already in the module.
230 Constant *C = M.getOrInsertGlobal(Name, EltTy);
231 assert(isa<GlobalVariable>(C) && "Incorrectly typed anchor?");
232 GlobalVariable *GV = cast<GlobalVariable>(C);
233
234 // If it has an initializer, it is already in the module.
235 if (GV->hasInitializer())
236 return SubProgramAnchor = DIAnchor(GV);
237
238 GV->setLinkage(GlobalValue::LinkOnceLinkage);
239 GV->setSection("llvm.metadata");
240 GV->setConstant(true);
241 M.addTypeName("llvm.dbg.anchor.type", EltTy);
242
243 // Otherwise, set the initializer.
244 Constant *Elts[] = {
245 GetTagConstant(dwarf::DW_TAG_anchor),
246 ConstantInt::get(Type::Int32Ty, TAG)
247 };
248
249 GV->setInitializer(ConstantStruct::get(Elts, 2));
250 return DIAnchor(GV);
251}
252
253
254
255//===----------------------------------------------------------------------===//
256// DIFactory: Primary Constructors
257//===----------------------------------------------------------------------===//
258
259/// GetOrCreateCompileUnitAnchor - Return the anchor for compile units,
260/// creating a new one if there isn't already one in the module.
261DIAnchor DIFactory::GetOrCreateCompileUnitAnchor() {
262 // If we already created one, just return it.
263 if (!CompileUnitAnchor.isNull())
264 return CompileUnitAnchor;
265 return CompileUnitAnchor = GetOrCreateAnchor(dwarf::DW_TAG_compile_unit,
266 "llvm.dbg.compile_units");
267}
268
269/// GetOrCreateSubprogramAnchor - Return the anchor for subprograms,
270/// creating a new one if there isn't already one in the module.
271DIAnchor DIFactory::GetOrCreateSubprogramAnchor() {
272 // If we already created one, just return it.
273 if (!SubProgramAnchor.isNull())
274 return SubProgramAnchor;
275 return SubProgramAnchor = GetOrCreateAnchor(dwarf::DW_TAG_subprogram,
276 "llvm.dbg.subprograms");
277}
278
279/// GetOrCreateGlobalVariableAnchor - Return the anchor for globals,
280/// creating a new one if there isn't already one in the module.
281DIAnchor DIFactory::GetOrCreateGlobalVariableAnchor() {
282 // If we already created one, just return it.
283 if (!GlobalVariableAnchor.isNull())
284 return GlobalVariableAnchor;
285 return GlobalVariableAnchor = GetOrCreateAnchor(dwarf::DW_TAG_variable,
286 "llvm.dbg.global_variables");
287}
288
289/// GetOrCreateArray - Create an descriptor for an array of descriptors.
290/// This implicitly uniques the arrays created.
291DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
292 SmallVector<Constant*, 16> Elts;
293
294 for (unsigned i = 0; i != NumTys; ++i)
Chris Lattner497a7a82008-11-10 04:10:34 +0000295 Elts.push_back(getCastToEmpty(Tys[i]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000296
Chris Lattner497a7a82008-11-10 04:10:34 +0000297 Constant *Init = ConstantArray::get(ArrayType::get(EmptyStructPtr,
298 Elts.size()),
Chris Lattnera45664f2008-11-10 02:56:27 +0000299 &Elts[0], Elts.size());
300 // If we already have this array, just return the uniqued version.
301 DIDescriptor &Entry = SimpleConstantCache[Init];
302 if (!Entry.isNull()) return DIArray(Entry.getGV());
303
304 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
305 GlobalValue::InternalLinkage,
306 Init, "llvm.dbg.array", &M);
307 GV->setSection("llvm.metadata");
308 Entry = DIDescriptor(GV);
309 return DIArray(GV);
310}
311
312/// GetOrCreateSubrange - Create a descriptor for a value range. This
313/// implicitly uniques the values returned.
314DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
315 Constant *Elts[] = {
316 GetTagConstant(dwarf::DW_TAG_subrange_type),
317 ConstantInt::get(Type::Int64Ty, Lo),
318 ConstantInt::get(Type::Int64Ty, Hi)
319 };
320
321 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
322
323 // If we already have this range, just return the uniqued version.
324 DIDescriptor &Entry = SimpleConstantCache[Init];
325 if (!Entry.isNull()) return DISubrange(Entry.getGV());
326
327 M.addTypeName("llvm.dbg.subrange.type", Init->getType());
328
329 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
330 GlobalValue::InternalLinkage,
331 Init, "llvm.dbg.subrange", &M);
332 GV->setSection("llvm.metadata");
333 Entry = DIDescriptor(GV);
334 return DISubrange(GV);
335}
336
337
338
339/// CreateCompileUnit - Create a new descriptor for the specified compile
340/// unit. Note that this does not unique compile units within the module.
341DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
342 const std::string &Filename,
343 const std::string &Directory,
344 const std::string &Producer) {
345 Constant *Elts[] = {
346 GetTagConstant(dwarf::DW_TAG_compile_unit),
Chris Lattner497a7a82008-11-10 04:10:34 +0000347 getCastToEmpty(GetOrCreateCompileUnitAnchor()),
Chris Lattnera45664f2008-11-10 02:56:27 +0000348 ConstantInt::get(Type::Int32Ty, LangID),
349 GetStringConstant(Filename),
350 GetStringConstant(Directory),
351 GetStringConstant(Producer)
352 };
353
354 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
355
356 M.addTypeName("llvm.dbg.compile_unit.type", Init->getType());
357 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
358 GlobalValue::InternalLinkage,
359 Init, "llvm.dbg.compile_unit", &M);
360 GV->setSection("llvm.metadata");
361 return DICompileUnit(GV);
362}
363
364/// CreateEnumerator - Create a single enumerator value.
365DIEnumerator DIFactory::CreateEnumerator(const std::string &Name, uint64_t Val){
366 Constant *Elts[] = {
367 GetTagConstant(dwarf::DW_TAG_enumerator),
368 GetStringConstant(Name),
369 ConstantInt::get(Type::Int64Ty, Val)
370 };
371
372 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
373
374 M.addTypeName("llvm.dbg.enumerator.type", Init->getType());
375 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
376 GlobalValue::InternalLinkage,
377 Init, "llvm.dbg.enumerator", &M);
378 GV->setSection("llvm.metadata");
379 return DIEnumerator(GV);
380}
381
382
383/// CreateBasicType - Create a basic type like int, float, etc.
384DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
385 const std::string &Name,
386 DICompileUnit CompileUnit,
387 unsigned LineNumber,
388 uint64_t SizeInBits,
389 uint64_t AlignInBits,
390 uint64_t OffsetInBits, unsigned Flags,
391 unsigned Encoding) {
392 Constant *Elts[] = {
393 GetTagConstant(dwarf::DW_TAG_base_type),
Chris Lattner497a7a82008-11-10 04:10:34 +0000394 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000395 GetStringConstant(Name),
Chris Lattner497a7a82008-11-10 04:10:34 +0000396 getCastToEmpty(CompileUnit),
Chris Lattnera45664f2008-11-10 02:56:27 +0000397 ConstantInt::get(Type::Int32Ty, LineNumber),
398 ConstantInt::get(Type::Int64Ty, SizeInBits),
399 ConstantInt::get(Type::Int64Ty, AlignInBits),
400 ConstantInt::get(Type::Int64Ty, OffsetInBits),
401 ConstantInt::get(Type::Int32Ty, Flags),
402 ConstantInt::get(Type::Int32Ty, Encoding)
403 };
404
405 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
406
407 M.addTypeName("llvm.dbg.basictype.type", Init->getType());
408 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
409 GlobalValue::InternalLinkage,
410 Init, "llvm.dbg.basictype", &M);
411 GV->setSection("llvm.metadata");
412 return DIBasicType(GV);
413}
414
415/// CreateDerivedType - Create a derived type like const qualified type,
416/// pointer, typedef, etc.
417DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
418 DIDescriptor Context,
419 const std::string &Name,
420 DICompileUnit CompileUnit,
421 unsigned LineNumber,
422 uint64_t SizeInBits,
423 uint64_t AlignInBits,
424 uint64_t OffsetInBits,
425 unsigned Flags,
426 DIType DerivedFrom) {
427 Constant *Elts[] = {
428 GetTagConstant(Tag),
Chris Lattner497a7a82008-11-10 04:10:34 +0000429 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000430 GetStringConstant(Name),
Chris Lattner497a7a82008-11-10 04:10:34 +0000431 getCastToEmpty(CompileUnit),
Chris Lattnera45664f2008-11-10 02:56:27 +0000432 ConstantInt::get(Type::Int32Ty, LineNumber),
433 ConstantInt::get(Type::Int64Ty, SizeInBits),
434 ConstantInt::get(Type::Int64Ty, AlignInBits),
435 ConstantInt::get(Type::Int64Ty, OffsetInBits),
436 ConstantInt::get(Type::Int32Ty, Flags),
Chris Lattner497a7a82008-11-10 04:10:34 +0000437 getCastToEmpty(DerivedFrom)
Chris Lattnera45664f2008-11-10 02:56:27 +0000438 };
439
440 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
441
442 M.addTypeName("llvm.dbg.derivedtype.type", Init->getType());
443 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
444 GlobalValue::InternalLinkage,
445 Init, "llvm.dbg.derivedtype", &M);
446 GV->setSection("llvm.metadata");
447 return DIDerivedType(GV);
448}
449
450/// CreateCompositeType - Create a composite type like array, struct, etc.
451DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
452 DIDescriptor Context,
453 const std::string &Name,
454 DICompileUnit CompileUnit,
455 unsigned LineNumber,
456 uint64_t SizeInBits,
457 uint64_t AlignInBits,
458 uint64_t OffsetInBits,
459 unsigned Flags,
460 DIType DerivedFrom,
461 DIArray Elements) {
462 Constant *Elts[] = {
463 GetTagConstant(Tag),
Chris Lattner497a7a82008-11-10 04:10:34 +0000464 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000465 GetStringConstant(Name),
Chris Lattner497a7a82008-11-10 04:10:34 +0000466 getCastToEmpty(CompileUnit),
Chris Lattnera45664f2008-11-10 02:56:27 +0000467 ConstantInt::get(Type::Int32Ty, LineNumber),
468 ConstantInt::get(Type::Int64Ty, SizeInBits),
469 ConstantInt::get(Type::Int64Ty, AlignInBits),
470 ConstantInt::get(Type::Int64Ty, OffsetInBits),
471 ConstantInt::get(Type::Int32Ty, Flags),
Chris Lattner497a7a82008-11-10 04:10:34 +0000472 getCastToEmpty(DerivedFrom),
473 getCastToEmpty(Elements)
Chris Lattnera45664f2008-11-10 02:56:27 +0000474 };
475
476 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
477
478 M.addTypeName("llvm.dbg.composite.type", Init->getType());
479 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
480 GlobalValue::InternalLinkage,
481 Init, "llvm.dbg.composite", &M);
482 GV->setSection("llvm.metadata");
483 return DICompositeType(GV);
484}
485
486
487/// CreateSubprogram - Create a new descriptor for the specified subprogram.
488/// See comments in DISubprogram for descriptions of these fields. This
489/// method does not unique the generated descriptors.
490DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
491 const std::string &Name,
492 const std::string &DisplayName,
493 const std::string &LinkageName,
494 DICompileUnit CompileUnit,
495 unsigned LineNo, DIType Type,
496 bool isLocalToUnit,
497 bool isDefinition) {
498 Constant *Elts[] = {
499 GetTagConstant(dwarf::DW_TAG_subprogram),
Chris Lattner497a7a82008-11-10 04:10:34 +0000500 getCastToEmpty(GetOrCreateSubprogramAnchor()),
501 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000502 GetStringConstant(Name),
503 GetStringConstant(DisplayName),
504 GetStringConstant(LinkageName),
Chris Lattner497a7a82008-11-10 04:10:34 +0000505 getCastToEmpty(CompileUnit),
Chris Lattnera45664f2008-11-10 02:56:27 +0000506 ConstantInt::get(Type::Int32Ty, LineNo),
Chris Lattner497a7a82008-11-10 04:10:34 +0000507 getCastToEmpty(Type),
Chris Lattnera45664f2008-11-10 02:56:27 +0000508 ConstantInt::get(Type::Int1Ty, isLocalToUnit),
509 ConstantInt::get(Type::Int1Ty, isDefinition)
510 };
511
512 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
513
514 M.addTypeName("llvm.dbg.subprogram.type", Init->getType());
515 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
516 GlobalValue::InternalLinkage,
517 Init, "llvm.dbg.subprogram", &M);
518 GV->setSection("llvm.metadata");
519 return DISubprogram(GV);
520}
521
522/// CreateGlobalVariable - Create a new descriptor for the specified global.
523DIGlobalVariable
524DIFactory::CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
525 const std::string &DisplayName,
526 const std::string &LinkageName,
527 DICompileUnit CompileUnit,
528 unsigned LineNo, DIType Type,bool isLocalToUnit,
529 bool isDefinition, llvm::GlobalVariable *Val) {
530
531 Constant *Elts[] = {
532 GetTagConstant(dwarf::DW_TAG_variable),
Chris Lattner497a7a82008-11-10 04:10:34 +0000533 getCastToEmpty(GetOrCreateGlobalVariableAnchor()),
534 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000535 GetStringConstant(Name),
536 GetStringConstant(DisplayName),
537 GetStringConstant(LinkageName),
Chris Lattner497a7a82008-11-10 04:10:34 +0000538 getCastToEmpty(CompileUnit),
Chris Lattnera45664f2008-11-10 02:56:27 +0000539 ConstantInt::get(Type::Int32Ty, LineNo),
Chris Lattner497a7a82008-11-10 04:10:34 +0000540 getCastToEmpty(Type),
Chris Lattnera45664f2008-11-10 02:56:27 +0000541 ConstantInt::get(Type::Int1Ty, isLocalToUnit),
542 ConstantInt::get(Type::Int1Ty, isDefinition),
Chris Lattner497a7a82008-11-10 04:10:34 +0000543 ConstantExpr::getBitCast(Val, EmptyStructPtr)
Chris Lattnera45664f2008-11-10 02:56:27 +0000544 };
545
546 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
547
548 M.addTypeName("llvm.dbg.global_variable.type", Init->getType());
549 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
550 GlobalValue::InternalLinkage,
551 Init, "llvm.dbg.global_variable", &M);
552 GV->setSection("llvm.metadata");
553 return DIGlobalVariable(GV);
554}
555
556
557/// CreateVariable - Create a new descriptor for the specified variable.
558DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
559 const std::string &Name,
560 DICompileUnit CompileUnit, unsigned LineNo,
561 DIType Type) {
562
563 Constant *Elts[] = {
564 GetTagConstant(Tag),
Chris Lattner497a7a82008-11-10 04:10:34 +0000565 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000566 GetStringConstant(Name),
Chris Lattner497a7a82008-11-10 04:10:34 +0000567 getCastToEmpty(CompileUnit),
Chris Lattnera45664f2008-11-10 02:56:27 +0000568 ConstantInt::get(Type::Int32Ty, LineNo),
Chris Lattner497a7a82008-11-10 04:10:34 +0000569 getCastToEmpty(Type),
Chris Lattnera45664f2008-11-10 02:56:27 +0000570 };
571
572 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
573
574 M.addTypeName("llvm.dbg.variable.type", Init->getType());
575 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
576 GlobalValue::InternalLinkage,
577 Init, "llvm.dbg.variable", &M);
578 GV->setSection("llvm.metadata");
579 return DIVariable(GV);
580}
581
582
583/// CreateBlock - This creates a descriptor for a lexical block with the
584/// specified parent context.
585DIBlock DIFactory::CreateBlock(DIDescriptor Context) {
586 Constant *Elts[] = {
587 GetTagConstant(dwarf::DW_TAG_lexical_block),
Chris Lattner497a7a82008-11-10 04:10:34 +0000588 getCastToEmpty(Context)
Chris Lattnera45664f2008-11-10 02:56:27 +0000589 };
590
591 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
592
593 M.addTypeName("llvm.dbg.block.type", Init->getType());
594 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
595 GlobalValue::InternalLinkage,
596 Init, "llvm.dbg.block", &M);
597 GV->setSection("llvm.metadata");
598 return DIBlock(GV);
599}
600
601
602//===----------------------------------------------------------------------===//
603// DIFactory: Routines for inserting code into a function
604//===----------------------------------------------------------------------===//
605
606/// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
607/// inserting it at the end of the specified basic block.
608void DIFactory::InsertStopPoint(DICompileUnit CU, unsigned LineNo,
609 unsigned ColNo, BasicBlock *BB) {
610
611 // Lazily construct llvm.dbg.stoppoint function.
612 if (!StopPointFn)
613 StopPointFn = llvm::Intrinsic::getDeclaration(&M,
614 llvm::Intrinsic::dbg_stoppoint);
615
616 // Invoke llvm.dbg.stoppoint
617 Value *Args[] = {
618 llvm::ConstantInt::get(llvm::Type::Int32Ty, LineNo),
619 llvm::ConstantInt::get(llvm::Type::Int32Ty, ColNo),
Chris Lattner497a7a82008-11-10 04:10:34 +0000620 getCastToEmpty(CU)
Chris Lattnera45664f2008-11-10 02:56:27 +0000621 };
622 CallInst::Create(StopPointFn, Args, Args+3, "", BB);
623}
624
625/// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
626/// mark the start of the specified subprogram.
627void DIFactory::InsertSubprogramStart(DISubprogram SP, BasicBlock *BB) {
628 // Lazily construct llvm.dbg.func.start.
629 if (!FuncStartFn)
630 FuncStartFn = llvm::Intrinsic::getDeclaration(&M,
631 llvm::Intrinsic::dbg_func_start);
632
633 // Call llvm.dbg.func.start which also implicitly sets a stoppoint.
Chris Lattner497a7a82008-11-10 04:10:34 +0000634 CallInst::Create(FuncStartFn, getCastToEmpty(SP), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000635}
636
637/// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
638/// mark the start of a region for the specified scoping descriptor.
639void DIFactory::InsertRegionStart(DIDescriptor D, BasicBlock *BB) {
640 // Lazily construct llvm.dbg.region.start function.
641 if (!RegionStartFn)
642 RegionStartFn = llvm::Intrinsic::getDeclaration(&M,
643 llvm::Intrinsic::dbg_region_start);
644 // Call llvm.dbg.func.start.
Chris Lattner497a7a82008-11-10 04:10:34 +0000645 CallInst::Create(RegionStartFn, getCastToEmpty(D), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000646}
647
648
649/// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
650/// mark the end of a region for the specified scoping descriptor.
651void DIFactory::InsertRegionEnd(DIDescriptor D, BasicBlock *BB) {
652 // Lazily construct llvm.dbg.region.end function.
653 if (!RegionEndFn)
654 RegionEndFn = llvm::Intrinsic::getDeclaration(&M,
655 llvm::Intrinsic::dbg_region_end);
656
Chris Lattner497a7a82008-11-10 04:10:34 +0000657 CallInst::Create(RegionEndFn, getCastToEmpty(D), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000658}
659
660/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
661void DIFactory::InsertDeclare(llvm::Value *Storage, DIVariable D,
662 BasicBlock *BB) {
663 // Cast the storage to a {}* for the call to llvm.dbg.declare.
Chris Lattner497a7a82008-11-10 04:10:34 +0000664 Storage = new llvm::BitCastInst(Storage, EmptyStructPtr, "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000665
666 if (!DeclareFn)
667 DeclareFn = llvm::Intrinsic::getDeclaration(&M,
668 llvm::Intrinsic::dbg_declare);
Chris Lattner497a7a82008-11-10 04:10:34 +0000669 Value *Args[] = { Storage, getCastToEmpty(D) };
Chris Lattnera45664f2008-11-10 02:56:27 +0000670 CallInst::Create(DeclareFn, Args, Args+2, "", BB);
671}