blob: 11a56bdf7d48aaf5e7f05515ca19cdb72152eea8 [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"
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000023#include "llvm/Support/Dwarf.h"
Devang Patelbf3f5a02009-01-30 01:03:10 +000024#include "llvm/Support/Streams.h"
Bill Wendlingdc817b62009-05-14 18:26:15 +000025
Chris Lattnera45664f2008-11-10 02:56:27 +000026using namespace llvm;
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000027using namespace llvm::dwarf;
Chris Lattnera45664f2008-11-10 02:56:27 +000028
29//===----------------------------------------------------------------------===//
30// DIDescriptor
31//===----------------------------------------------------------------------===//
32
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000033/// ValidDebugInfo - Return true if V represents valid debug info value.
34bool DIDescriptor::ValidDebugInfo(Value *V, CodeGenOpt::Level OptLevel) {
35 if (!V)
36 return false;
37
38 GlobalVariable *GV = dyn_cast<GlobalVariable>(V->stripPointerCasts());
39 if (!GV)
40 return false;
41
42 if (!GV->hasInternalLinkage () && !GV->hasLinkOnceLinkage())
43 return false;
44
45 DIDescriptor DI(GV);
46
47 // Check current version. Allow Version6 for now.
48 unsigned Version = DI.getVersion();
49 if (Version != LLVMDebugVersion && Version != LLVMDebugVersion6)
50 return false;
51
52 unsigned Tag = DI.getTag();
53 switch (Tag) {
54 case DW_TAG_variable:
55 assert(DIVariable(GV).Verify() && "Invalid DebugInfo value");
56 break;
57 case DW_TAG_compile_unit:
58 assert(DICompileUnit(GV).Verify() && "Invalid DebugInfo value");
59 break;
60 case DW_TAG_subprogram:
61 assert(DISubprogram(GV).Verify() && "Invalid DebugInfo value");
62 break;
63 case DW_TAG_lexical_block:
Bill Wendlingdc817b62009-05-14 18:26:15 +000064 // FIXME: This interfers with the quality of generated code during
65 // optimization.
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000066 if (OptLevel != CodeGenOpt::None)
67 return false;
Bill Wendlingdc817b62009-05-14 18:26:15 +000068 // FALLTHROUGH
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000069 default:
70 break;
71 }
72
73 return true;
74}
75
Devang Patel9af2fa82009-06-23 22:25:41 +000076DIDescriptor::DIDescriptor(GlobalVariable *GV, unsigned RequiredTag) {
77 DbgGV = GV;
Chris Lattnera45664f2008-11-10 02:56:27 +000078
Bill Wendlingdc817b62009-05-14 18:26:15 +000079 // If this is non-null, check to see if the Tag matches. If not, set to null.
Chris Lattnera45664f2008-11-10 02:56:27 +000080 if (GV && getTag() != RequiredTag)
Devang Patel9af2fa82009-06-23 22:25:41 +000081 DbgGV = 0;
Chris Lattnera45664f2008-11-10 02:56:27 +000082}
83
Bill Wendling0582ae92009-03-13 04:39:26 +000084const std::string &
85DIDescriptor::getStringField(unsigned Elt, std::string &Result) const {
Devang Patel9af2fa82009-06-23 22:25:41 +000086 if (DbgGV == 0) {
Bill Wendling0582ae92009-03-13 04:39:26 +000087 Result.clear();
88 return Result;
89 }
Chris Lattnera45664f2008-11-10 02:56:27 +000090
Devang Patel9af2fa82009-06-23 22:25:41 +000091 Constant *C = DbgGV->getInitializer();
Bill Wendling0582ae92009-03-13 04:39:26 +000092 if (C == 0 || Elt >= C->getNumOperands()) {
93 Result.clear();
94 return Result;
95 }
Bill Wendlingdc817b62009-05-14 18:26:15 +000096
Chris Lattnera45664f2008-11-10 02:56:27 +000097 // Fills in the string if it succeeds
Bill Wendling0582ae92009-03-13 04:39:26 +000098 if (!GetConstantStringInfo(C->getOperand(Elt), Result))
99 Result.clear();
100
101 return Result;
Chris Lattnera45664f2008-11-10 02:56:27 +0000102}
103
104uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
Devang Patel9af2fa82009-06-23 22:25:41 +0000105 if (DbgGV == 0) return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000106
Devang Patel9af2fa82009-06-23 22:25:41 +0000107 Constant *C = DbgGV->getInitializer();
Chris Lattnera45664f2008-11-10 02:56:27 +0000108 if (C == 0 || Elt >= C->getNumOperands())
109 return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000110
Chris Lattnera45664f2008-11-10 02:56:27 +0000111 if (ConstantInt *CI = dyn_cast<ConstantInt>(C->getOperand(Elt)))
112 return CI->getZExtValue();
113 return 0;
114}
115
Chris Lattnera45664f2008-11-10 02:56:27 +0000116DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
Devang Patel9af2fa82009-06-23 22:25:41 +0000117 if (DbgGV == 0) return DIDescriptor();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000118
Devang Patel9af2fa82009-06-23 22:25:41 +0000119 Constant *C = DbgGV->getInitializer();
Chris Lattnera45664f2008-11-10 02:56:27 +0000120 if (C == 0 || Elt >= C->getNumOperands())
121 return DIDescriptor();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000122
Chris Lattnera45664f2008-11-10 02:56:27 +0000123 C = C->getOperand(Elt);
124 return DIDescriptor(dyn_cast<GlobalVariable>(C->stripPointerCasts()));
125}
126
127GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
Devang Patel9af2fa82009-06-23 22:25:41 +0000128 if (DbgGV == 0) return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000129
Devang Patel9af2fa82009-06-23 22:25:41 +0000130 Constant *C = DbgGV->getInitializer();
Chris Lattnera45664f2008-11-10 02:56:27 +0000131 if (C == 0 || Elt >= C->getNumOperands())
132 return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000133
Chris Lattnera45664f2008-11-10 02:56:27 +0000134 C = C->getOperand(Elt);
Chris Lattnera45664f2008-11-10 02:56:27 +0000135 return dyn_cast<GlobalVariable>(C->stripPointerCasts());
136}
137
Chris Lattnera45664f2008-11-10 02:56:27 +0000138//===----------------------------------------------------------------------===//
139// Simple Descriptor Constructors and other Methods
140//===----------------------------------------------------------------------===//
141
Bill Wendlingdc817b62009-05-14 18:26:15 +0000142// Needed by DIVariable::getType().
Devang Patel9af2fa82009-06-23 22:25:41 +0000143DIType::DIType(GlobalVariable *GV) : DIDescriptor(GV) {
144 if (!GV) return;
Torok Edwinb07fbd92008-12-13 08:25:29 +0000145 unsigned tag = getTag();
146 if (tag != dwarf::DW_TAG_base_type && !DIDerivedType::isDerivedType(tag) &&
147 !DICompositeType::isCompositeType(tag))
Devang Patel9af2fa82009-06-23 22:25:41 +0000148 DbgGV = 0;
Torok Edwinb07fbd92008-12-13 08:25:29 +0000149}
Chris Lattnera45664f2008-11-10 02:56:27 +0000150
151/// isDerivedType - Return true if the specified tag is legal for
152/// DIDerivedType.
Devang Patel486938f2009-01-12 21:38:43 +0000153bool DIType::isDerivedType(unsigned Tag) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000154 switch (Tag) {
155 case dwarf::DW_TAG_typedef:
156 case dwarf::DW_TAG_pointer_type:
157 case dwarf::DW_TAG_reference_type:
158 case dwarf::DW_TAG_const_type:
159 case dwarf::DW_TAG_volatile_type:
160 case dwarf::DW_TAG_restrict_type:
161 case dwarf::DW_TAG_member:
162 case dwarf::DW_TAG_inheritance:
163 return true;
164 default:
165 // FIXME: Even though it doesn't make sense, CompositeTypes are current
166 // modelled as DerivedTypes, this should return true for them as well.
167 return false;
168 }
169}
170
Chris Lattnera45664f2008-11-10 02:56:27 +0000171/// isCompositeType - Return true if the specified tag is legal for
172/// DICompositeType.
Devang Patel486938f2009-01-12 21:38:43 +0000173bool DIType::isCompositeType(unsigned TAG) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000174 switch (TAG) {
175 case dwarf::DW_TAG_array_type:
176 case dwarf::DW_TAG_structure_type:
177 case dwarf::DW_TAG_union_type:
178 case dwarf::DW_TAG_enumeration_type:
179 case dwarf::DW_TAG_vector_type:
180 case dwarf::DW_TAG_subroutine_type:
Devang Patel25cb0d72009-03-25 03:52:06 +0000181 case dwarf::DW_TAG_class_type:
Chris Lattnera45664f2008-11-10 02:56:27 +0000182 return true;
183 default:
184 return false;
185 }
186}
187
Chris Lattnera45664f2008-11-10 02:56:27 +0000188/// isVariable - Return true if the specified tag is legal for DIVariable.
189bool DIVariable::isVariable(unsigned Tag) {
190 switch (Tag) {
191 case dwarf::DW_TAG_auto_variable:
192 case dwarf::DW_TAG_arg_variable:
193 case dwarf::DW_TAG_return_variable:
194 return true;
195 default:
196 return false;
197 }
198}
199
Devang Patel68afdc32009-01-05 18:33:01 +0000200unsigned DIArray::getNumElements() const {
Devang Patel9af2fa82009-06-23 22:25:41 +0000201 assert (DbgGV && "Invalid DIArray");
202 Constant *C = DbgGV->getInitializer();
Devang Patel68afdc32009-01-05 18:33:01 +0000203 assert (C && "Invalid DIArray initializer");
204 return C->getNumOperands();
205}
Chris Lattnera45664f2008-11-10 02:56:27 +0000206
Devang Patelb79b5352009-01-19 23:21:49 +0000207/// Verify - Verify that a compile unit is well formed.
208bool DICompileUnit::Verify() const {
209 if (isNull())
210 return false;
Bill Wendling0582ae92009-03-13 04:39:26 +0000211 std::string Res;
212 if (getFilename(Res).empty())
213 return false;
Devang Patelb79b5352009-01-19 23:21:49 +0000214 // It is possible that directory and produce string is empty.
Bill Wendling0582ae92009-03-13 04:39:26 +0000215 return true;
Devang Patelb79b5352009-01-19 23:21:49 +0000216}
217
218/// Verify - Verify that a type descriptor is well formed.
219bool DIType::Verify() const {
220 if (isNull())
221 return false;
222 if (getContext().isNull())
223 return false;
224
225 DICompileUnit CU = getCompileUnit();
226 if (!CU.isNull() && !CU.Verify())
227 return false;
228 return true;
229}
230
231/// Verify - Verify that a composite type descriptor is well formed.
232bool DICompositeType::Verify() const {
233 if (isNull())
234 return false;
235 if (getContext().isNull())
236 return false;
237
238 DICompileUnit CU = getCompileUnit();
239 if (!CU.isNull() && !CU.Verify())
240 return false;
241 return true;
242}
243
244/// Verify - Verify that a subprogram descriptor is well formed.
245bool DISubprogram::Verify() const {
246 if (isNull())
247 return false;
248
249 if (getContext().isNull())
250 return false;
251
252 DICompileUnit CU = getCompileUnit();
253 if (!CU.Verify())
254 return false;
255
256 DICompositeType Ty = getType();
257 if (!Ty.isNull() && !Ty.Verify())
258 return false;
259 return true;
260}
261
262/// Verify - Verify that a global variable descriptor is well formed.
263bool DIGlobalVariable::Verify() const {
264 if (isNull())
265 return false;
266
267 if (getContext().isNull())
268 return false;
269
270 DICompileUnit CU = getCompileUnit();
Chris Lattnere3f6cea2009-05-05 04:55:56 +0000271 if (!CU.isNull() && !CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000272 return false;
273
274 DIType Ty = getType();
275 if (!Ty.Verify())
276 return false;
277
278 if (!getGlobal())
279 return false;
280
281 return true;
282}
283
284/// Verify - Verify that a variable descriptor is well formed.
285bool DIVariable::Verify() const {
286 if (isNull())
287 return false;
288
289 if (getContext().isNull())
290 return false;
291
292 DIType Ty = getType();
293 if (!Ty.Verify())
294 return false;
295
Devang Patelb79b5352009-01-19 23:21:49 +0000296 return true;
297}
298
Devang Patel36375ee2009-02-17 21:23:59 +0000299/// getOriginalTypeSize - If this type is derived from a base type then
300/// return base type size.
301uint64_t DIDerivedType::getOriginalTypeSize() const {
302 if (getTag() != dwarf::DW_TAG_member)
303 return getSizeInBits();
304 DIType BT = getTypeDerivedFrom();
305 if (BT.getTag() != dwarf::DW_TAG_base_type)
306 return getSizeInBits();
307 return BT.getSizeInBits();
308}
Devang Patelb79b5352009-01-19 23:21:49 +0000309
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000310/// describes - Return true if this subprogram provides debugging
311/// information for the function F.
312bool DISubprogram::describes(const Function *F) {
313 assert (F && "Invalid function");
314 std::string Name;
315 getLinkageName(Name);
316 if (Name.empty())
317 getName(Name);
318 if (!Name.empty() && (strcmp(Name.c_str(), F->getNameStart()) == false))
319 return true;
320 return false;
321}
322
Chris Lattnera45664f2008-11-10 02:56:27 +0000323//===----------------------------------------------------------------------===//
324// DIFactory: Basic Helpers
325//===----------------------------------------------------------------------===//
326
Bill Wendlingdc817b62009-05-14 18:26:15 +0000327DIFactory::DIFactory(Module &m)
328 : M(m), StopPointFn(0), FuncStartFn(0), RegionStartFn(0), RegionEndFn(0),
329 DeclareFn(0) {
Chris Lattner497a7a82008-11-10 04:10:34 +0000330 EmptyStructPtr = PointerType::getUnqual(StructType::get(NULL, NULL));
331}
332
333/// getCastToEmpty - Return this descriptor as a Constant* with type '{}*'.
334/// This is only valid when the descriptor is non-null.
335Constant *DIFactory::getCastToEmpty(DIDescriptor D) {
336 if (D.isNull()) return Constant::getNullValue(EmptyStructPtr);
337 return ConstantExpr::getBitCast(D.getGV(), EmptyStructPtr);
338}
339
Chris Lattnera45664f2008-11-10 02:56:27 +0000340Constant *DIFactory::GetTagConstant(unsigned TAG) {
Devang Patel6906ba52009-01-20 19:22:03 +0000341 assert((TAG & LLVMDebugVersionMask) == 0 &&
Chris Lattnera45664f2008-11-10 02:56:27 +0000342 "Tag too large for debug encoding!");
Devang Patel6906ba52009-01-20 19:22:03 +0000343 return ConstantInt::get(Type::Int32Ty, TAG | LLVMDebugVersion);
Chris Lattnera45664f2008-11-10 02:56:27 +0000344}
345
346Constant *DIFactory::GetStringConstant(const std::string &String) {
347 // Check string cache for previous edition.
348 Constant *&Slot = StringCache[String];
349
350 // Return Constant if previously defined.
351 if (Slot) return Slot;
352
353 const PointerType *DestTy = PointerType::getUnqual(Type::Int8Ty);
354
Dan Gohmana119de82009-06-14 23:30:43 +0000355 // If empty string then use a i8* null instead.
Chris Lattnera45664f2008-11-10 02:56:27 +0000356 if (String.empty())
357 return Slot = ConstantPointerNull::get(DestTy);
358
359 // Construct string as an llvm constant.
360 Constant *ConstStr = ConstantArray::get(String);
361
362 // Otherwise create and return a new string global.
363 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
364 GlobalVariable::InternalLinkage,
365 ConstStr, ".str", &M);
366 StrGV->setSection("llvm.metadata");
367 return Slot = ConstantExpr::getBitCast(StrGV, DestTy);
368}
369
370/// GetOrCreateAnchor - Look up an anchor for the specified tag and name. If it
371/// already exists, return it. If not, create a new one and return it.
372DIAnchor DIFactory::GetOrCreateAnchor(unsigned TAG, const char *Name) {
Chris Lattner497a7a82008-11-10 04:10:34 +0000373 const Type *EltTy = StructType::get(Type::Int32Ty, Type::Int32Ty, NULL);
Chris Lattnera45664f2008-11-10 02:56:27 +0000374
375 // Otherwise, create the global or return it if already in the module.
376 Constant *C = M.getOrInsertGlobal(Name, EltTy);
377 assert(isa<GlobalVariable>(C) && "Incorrectly typed anchor?");
378 GlobalVariable *GV = cast<GlobalVariable>(C);
379
380 // If it has an initializer, it is already in the module.
381 if (GV->hasInitializer())
382 return SubProgramAnchor = DIAnchor(GV);
383
Duncan Sands667d4b82009-03-07 15:45:40 +0000384 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
Chris Lattnera45664f2008-11-10 02:56:27 +0000385 GV->setSection("llvm.metadata");
386 GV->setConstant(true);
387 M.addTypeName("llvm.dbg.anchor.type", EltTy);
388
389 // Otherwise, set the initializer.
390 Constant *Elts[] = {
391 GetTagConstant(dwarf::DW_TAG_anchor),
392 ConstantInt::get(Type::Int32Ty, TAG)
393 };
394
395 GV->setInitializer(ConstantStruct::get(Elts, 2));
396 return DIAnchor(GV);
397}
398
399
400
401//===----------------------------------------------------------------------===//
402// DIFactory: Primary Constructors
403//===----------------------------------------------------------------------===//
404
405/// GetOrCreateCompileUnitAnchor - Return the anchor for compile units,
406/// creating a new one if there isn't already one in the module.
407DIAnchor DIFactory::GetOrCreateCompileUnitAnchor() {
408 // If we already created one, just return it.
409 if (!CompileUnitAnchor.isNull())
410 return CompileUnitAnchor;
411 return CompileUnitAnchor = GetOrCreateAnchor(dwarf::DW_TAG_compile_unit,
412 "llvm.dbg.compile_units");
413}
414
415/// GetOrCreateSubprogramAnchor - Return the anchor for subprograms,
416/// creating a new one if there isn't already one in the module.
417DIAnchor DIFactory::GetOrCreateSubprogramAnchor() {
418 // If we already created one, just return it.
419 if (!SubProgramAnchor.isNull())
420 return SubProgramAnchor;
421 return SubProgramAnchor = GetOrCreateAnchor(dwarf::DW_TAG_subprogram,
422 "llvm.dbg.subprograms");
423}
424
425/// GetOrCreateGlobalVariableAnchor - Return the anchor for globals,
426/// creating a new one if there isn't already one in the module.
427DIAnchor DIFactory::GetOrCreateGlobalVariableAnchor() {
428 // If we already created one, just return it.
429 if (!GlobalVariableAnchor.isNull())
430 return GlobalVariableAnchor;
431 return GlobalVariableAnchor = GetOrCreateAnchor(dwarf::DW_TAG_variable,
432 "llvm.dbg.global_variables");
433}
434
435/// GetOrCreateArray - Create an descriptor for an array of descriptors.
436/// This implicitly uniques the arrays created.
437DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
438 SmallVector<Constant*, 16> Elts;
439
440 for (unsigned i = 0; i != NumTys; ++i)
Chris Lattner497a7a82008-11-10 04:10:34 +0000441 Elts.push_back(getCastToEmpty(Tys[i]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000442
Chris Lattner497a7a82008-11-10 04:10:34 +0000443 Constant *Init = ConstantArray::get(ArrayType::get(EmptyStructPtr,
444 Elts.size()),
Jay Foade3e51c02009-05-21 09:52:38 +0000445 Elts.data(), Elts.size());
Chris Lattnera45664f2008-11-10 02:56:27 +0000446 // If we already have this array, just return the uniqued version.
447 DIDescriptor &Entry = SimpleConstantCache[Init];
448 if (!Entry.isNull()) return DIArray(Entry.getGV());
449
450 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
451 GlobalValue::InternalLinkage,
452 Init, "llvm.dbg.array", &M);
453 GV->setSection("llvm.metadata");
454 Entry = DIDescriptor(GV);
455 return DIArray(GV);
456}
457
458/// GetOrCreateSubrange - Create a descriptor for a value range. This
459/// implicitly uniques the values returned.
460DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
461 Constant *Elts[] = {
462 GetTagConstant(dwarf::DW_TAG_subrange_type),
463 ConstantInt::get(Type::Int64Ty, Lo),
464 ConstantInt::get(Type::Int64Ty, Hi)
465 };
466
467 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
468
469 // If we already have this range, just return the uniqued version.
470 DIDescriptor &Entry = SimpleConstantCache[Init];
471 if (!Entry.isNull()) return DISubrange(Entry.getGV());
472
473 M.addTypeName("llvm.dbg.subrange.type", Init->getType());
474
475 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
476 GlobalValue::InternalLinkage,
477 Init, "llvm.dbg.subrange", &M);
478 GV->setSection("llvm.metadata");
479 Entry = DIDescriptor(GV);
480 return DISubrange(GV);
481}
482
483
484
485/// CreateCompileUnit - Create a new descriptor for the specified compile
486/// unit. Note that this does not unique compile units within the module.
487DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
488 const std::string &Filename,
489 const std::string &Directory,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000490 const std::string &Producer,
Devang Pateldd9db662009-01-30 18:20:31 +0000491 bool isMain,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000492 bool isOptimized,
Devang Patel13319ce2009-02-17 22:43:44 +0000493 const char *Flags,
494 unsigned RunTimeVer) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000495 Constant *Elts[] = {
496 GetTagConstant(dwarf::DW_TAG_compile_unit),
Chris Lattner497a7a82008-11-10 04:10:34 +0000497 getCastToEmpty(GetOrCreateCompileUnitAnchor()),
Chris Lattnera45664f2008-11-10 02:56:27 +0000498 ConstantInt::get(Type::Int32Ty, LangID),
499 GetStringConstant(Filename),
500 GetStringConstant(Directory),
Devang Patel3b64c6b2009-01-23 22:33:47 +0000501 GetStringConstant(Producer),
Devang Pateldd9db662009-01-30 18:20:31 +0000502 ConstantInt::get(Type::Int1Ty, isMain),
Devang Patel3b64c6b2009-01-23 22:33:47 +0000503 ConstantInt::get(Type::Int1Ty, isOptimized),
Devang Patel13319ce2009-02-17 22:43:44 +0000504 GetStringConstant(Flags),
505 ConstantInt::get(Type::Int32Ty, RunTimeVer)
Chris Lattnera45664f2008-11-10 02:56:27 +0000506 };
507
508 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
509
510 M.addTypeName("llvm.dbg.compile_unit.type", Init->getType());
511 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
512 GlobalValue::InternalLinkage,
513 Init, "llvm.dbg.compile_unit", &M);
514 GV->setSection("llvm.metadata");
515 return DICompileUnit(GV);
516}
517
518/// CreateEnumerator - Create a single enumerator value.
519DIEnumerator DIFactory::CreateEnumerator(const std::string &Name, uint64_t Val){
520 Constant *Elts[] = {
521 GetTagConstant(dwarf::DW_TAG_enumerator),
522 GetStringConstant(Name),
523 ConstantInt::get(Type::Int64Ty, Val)
524 };
525
526 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
527
528 M.addTypeName("llvm.dbg.enumerator.type", Init->getType());
529 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
530 GlobalValue::InternalLinkage,
531 Init, "llvm.dbg.enumerator", &M);
532 GV->setSection("llvm.metadata");
533 return DIEnumerator(GV);
534}
535
536
537/// CreateBasicType - Create a basic type like int, float, etc.
538DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
Bill Wendling0582ae92009-03-13 04:39:26 +0000539 const std::string &Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000540 DICompileUnit CompileUnit,
541 unsigned LineNumber,
542 uint64_t SizeInBits,
543 uint64_t AlignInBits,
544 uint64_t OffsetInBits, unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000545 unsigned Encoding) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000546 Constant *Elts[] = {
547 GetTagConstant(dwarf::DW_TAG_base_type),
Chris Lattner497a7a82008-11-10 04:10:34 +0000548 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000549 GetStringConstant(Name),
Chris Lattner497a7a82008-11-10 04:10:34 +0000550 getCastToEmpty(CompileUnit),
Chris Lattnera45664f2008-11-10 02:56:27 +0000551 ConstantInt::get(Type::Int32Ty, LineNumber),
552 ConstantInt::get(Type::Int64Ty, SizeInBits),
553 ConstantInt::get(Type::Int64Ty, AlignInBits),
554 ConstantInt::get(Type::Int64Ty, OffsetInBits),
555 ConstantInt::get(Type::Int32Ty, Flags),
Devang Pateldd9db662009-01-30 18:20:31 +0000556 ConstantInt::get(Type::Int32Ty, Encoding)
Chris Lattnera45664f2008-11-10 02:56:27 +0000557 };
558
559 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
560
561 M.addTypeName("llvm.dbg.basictype.type", Init->getType());
562 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
563 GlobalValue::InternalLinkage,
564 Init, "llvm.dbg.basictype", &M);
565 GV->setSection("llvm.metadata");
566 return DIBasicType(GV);
567}
568
569/// CreateDerivedType - Create a derived type like const qualified type,
570/// pointer, typedef, etc.
571DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
572 DIDescriptor Context,
573 const std::string &Name,
574 DICompileUnit CompileUnit,
575 unsigned LineNumber,
576 uint64_t SizeInBits,
577 uint64_t AlignInBits,
578 uint64_t OffsetInBits,
579 unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000580 DIType DerivedFrom) {
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),
Devang Pateldd9db662009-01-30 18:20:31 +0000591 getCastToEmpty(DerivedFrom)
Chris Lattnera45664f2008-11-10 02:56:27 +0000592 };
593
594 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
595
596 M.addTypeName("llvm.dbg.derivedtype.type", Init->getType());
597 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
598 GlobalValue::InternalLinkage,
599 Init, "llvm.dbg.derivedtype", &M);
600 GV->setSection("llvm.metadata");
601 return DIDerivedType(GV);
602}
603
604/// CreateCompositeType - Create a composite type like array, struct, etc.
605DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
606 DIDescriptor Context,
607 const std::string &Name,
608 DICompileUnit CompileUnit,
609 unsigned LineNumber,
610 uint64_t SizeInBits,
611 uint64_t AlignInBits,
612 uint64_t OffsetInBits,
613 unsigned Flags,
614 DIType DerivedFrom,
Devang Patel13319ce2009-02-17 22:43:44 +0000615 DIArray Elements,
616 unsigned RuntimeLang) {
Devang Patel854967e2008-12-17 22:39:29 +0000617
Chris Lattnera45664f2008-11-10 02:56:27 +0000618 Constant *Elts[] = {
619 GetTagConstant(Tag),
Chris Lattner497a7a82008-11-10 04:10:34 +0000620 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000621 GetStringConstant(Name),
Chris Lattner497a7a82008-11-10 04:10:34 +0000622 getCastToEmpty(CompileUnit),
Chris Lattnera45664f2008-11-10 02:56:27 +0000623 ConstantInt::get(Type::Int32Ty, LineNumber),
624 ConstantInt::get(Type::Int64Ty, SizeInBits),
625 ConstantInt::get(Type::Int64Ty, AlignInBits),
626 ConstantInt::get(Type::Int64Ty, OffsetInBits),
627 ConstantInt::get(Type::Int32Ty, Flags),
Chris Lattner497a7a82008-11-10 04:10:34 +0000628 getCastToEmpty(DerivedFrom),
Devang Patel13319ce2009-02-17 22:43:44 +0000629 getCastToEmpty(Elements),
630 ConstantInt::get(Type::Int32Ty, RuntimeLang)
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.composite.type", Init->getType());
636 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
637 GlobalValue::InternalLinkage,
638 Init, "llvm.dbg.composite", &M);
639 GV->setSection("llvm.metadata");
640 return DICompositeType(GV);
641}
642
643
644/// CreateSubprogram - Create a new descriptor for the specified subprogram.
645/// See comments in DISubprogram for descriptions of these fields. This
646/// method does not unique the generated descriptors.
647DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
648 const std::string &Name,
649 const std::string &DisplayName,
650 const std::string &LinkageName,
651 DICompileUnit CompileUnit,
652 unsigned LineNo, DIType Type,
653 bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000654 bool isDefinition) {
Devang Patel854967e2008-12-17 22:39:29 +0000655
Chris Lattnera45664f2008-11-10 02:56:27 +0000656 Constant *Elts[] = {
657 GetTagConstant(dwarf::DW_TAG_subprogram),
Chris Lattner497a7a82008-11-10 04:10:34 +0000658 getCastToEmpty(GetOrCreateSubprogramAnchor()),
659 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000660 GetStringConstant(Name),
661 GetStringConstant(DisplayName),
662 GetStringConstant(LinkageName),
Chris Lattner497a7a82008-11-10 04:10:34 +0000663 getCastToEmpty(CompileUnit),
Chris Lattnera45664f2008-11-10 02:56:27 +0000664 ConstantInt::get(Type::Int32Ty, LineNo),
Chris Lattner497a7a82008-11-10 04:10:34 +0000665 getCastToEmpty(Type),
Chris Lattnera45664f2008-11-10 02:56:27 +0000666 ConstantInt::get(Type::Int1Ty, isLocalToUnit),
Devang Pateldd9db662009-01-30 18:20:31 +0000667 ConstantInt::get(Type::Int1Ty, isDefinition)
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.subprogram.type", Init->getType());
673 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
674 GlobalValue::InternalLinkage,
675 Init, "llvm.dbg.subprogram", &M);
676 GV->setSection("llvm.metadata");
677 return DISubprogram(GV);
678}
679
680/// CreateGlobalVariable - Create a new descriptor for the specified global.
681DIGlobalVariable
682DIFactory::CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
683 const std::string &DisplayName,
684 const std::string &LinkageName,
685 DICompileUnit CompileUnit,
686 unsigned LineNo, DIType Type,bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000687 bool isDefinition, llvm::GlobalVariable *Val) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000688 Constant *Elts[] = {
689 GetTagConstant(dwarf::DW_TAG_variable),
Chris Lattner497a7a82008-11-10 04:10:34 +0000690 getCastToEmpty(GetOrCreateGlobalVariableAnchor()),
691 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000692 GetStringConstant(Name),
693 GetStringConstant(DisplayName),
694 GetStringConstant(LinkageName),
Chris Lattner497a7a82008-11-10 04:10:34 +0000695 getCastToEmpty(CompileUnit),
Chris Lattnera45664f2008-11-10 02:56:27 +0000696 ConstantInt::get(Type::Int32Ty, LineNo),
Chris Lattner497a7a82008-11-10 04:10:34 +0000697 getCastToEmpty(Type),
Chris Lattnera45664f2008-11-10 02:56:27 +0000698 ConstantInt::get(Type::Int1Ty, isLocalToUnit),
699 ConstantInt::get(Type::Int1Ty, isDefinition),
Devang Pateldd9db662009-01-30 18:20:31 +0000700 ConstantExpr::getBitCast(Val, EmptyStructPtr)
Chris Lattnera45664f2008-11-10 02:56:27 +0000701 };
702
703 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
704
705 M.addTypeName("llvm.dbg.global_variable.type", Init->getType());
706 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
707 GlobalValue::InternalLinkage,
708 Init, "llvm.dbg.global_variable", &M);
709 GV->setSection("llvm.metadata");
710 return DIGlobalVariable(GV);
711}
712
713
714/// CreateVariable - Create a new descriptor for the specified variable.
715DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
716 const std::string &Name,
717 DICompileUnit CompileUnit, unsigned LineNo,
Devang Pateldd9db662009-01-30 18:20:31 +0000718 DIType Type) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000719 Constant *Elts[] = {
720 GetTagConstant(Tag),
Chris Lattner497a7a82008-11-10 04:10:34 +0000721 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000722 GetStringConstant(Name),
Chris Lattner497a7a82008-11-10 04:10:34 +0000723 getCastToEmpty(CompileUnit),
Chris Lattnera45664f2008-11-10 02:56:27 +0000724 ConstantInt::get(Type::Int32Ty, LineNo),
Devang Pateldd9db662009-01-30 18:20:31 +0000725 getCastToEmpty(Type)
Chris Lattnera45664f2008-11-10 02:56:27 +0000726 };
727
728 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
729
730 M.addTypeName("llvm.dbg.variable.type", Init->getType());
731 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
732 GlobalValue::InternalLinkage,
733 Init, "llvm.dbg.variable", &M);
734 GV->setSection("llvm.metadata");
735 return DIVariable(GV);
736}
737
738
739/// CreateBlock - This creates a descriptor for a lexical block with the
740/// specified parent context.
741DIBlock DIFactory::CreateBlock(DIDescriptor Context) {
742 Constant *Elts[] = {
743 GetTagConstant(dwarf::DW_TAG_lexical_block),
Chris Lattner497a7a82008-11-10 04:10:34 +0000744 getCastToEmpty(Context)
Chris Lattnera45664f2008-11-10 02:56:27 +0000745 };
746
747 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
748
749 M.addTypeName("llvm.dbg.block.type", Init->getType());
750 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
751 GlobalValue::InternalLinkage,
752 Init, "llvm.dbg.block", &M);
753 GV->setSection("llvm.metadata");
754 return DIBlock(GV);
755}
756
757
758//===----------------------------------------------------------------------===//
759// DIFactory: Routines for inserting code into a function
760//===----------------------------------------------------------------------===//
761
762/// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
763/// inserting it at the end of the specified basic block.
764void DIFactory::InsertStopPoint(DICompileUnit CU, unsigned LineNo,
765 unsigned ColNo, BasicBlock *BB) {
766
767 // Lazily construct llvm.dbg.stoppoint function.
768 if (!StopPointFn)
769 StopPointFn = llvm::Intrinsic::getDeclaration(&M,
770 llvm::Intrinsic::dbg_stoppoint);
771
772 // Invoke llvm.dbg.stoppoint
773 Value *Args[] = {
774 llvm::ConstantInt::get(llvm::Type::Int32Ty, LineNo),
775 llvm::ConstantInt::get(llvm::Type::Int32Ty, ColNo),
Chris Lattner497a7a82008-11-10 04:10:34 +0000776 getCastToEmpty(CU)
Chris Lattnera45664f2008-11-10 02:56:27 +0000777 };
778 CallInst::Create(StopPointFn, Args, Args+3, "", BB);
779}
780
781/// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
782/// mark the start of the specified subprogram.
783void DIFactory::InsertSubprogramStart(DISubprogram SP, BasicBlock *BB) {
784 // Lazily construct llvm.dbg.func.start.
785 if (!FuncStartFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000786 FuncStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_func_start);
Chris Lattnera45664f2008-11-10 02:56:27 +0000787
788 // Call llvm.dbg.func.start which also implicitly sets a stoppoint.
Chris Lattner497a7a82008-11-10 04:10:34 +0000789 CallInst::Create(FuncStartFn, getCastToEmpty(SP), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000790}
791
792/// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
793/// mark the start of a region for the specified scoping descriptor.
794void DIFactory::InsertRegionStart(DIDescriptor D, BasicBlock *BB) {
795 // Lazily construct llvm.dbg.region.start function.
796 if (!RegionStartFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000797 RegionStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_start);
798
Chris Lattnera45664f2008-11-10 02:56:27 +0000799 // Call llvm.dbg.func.start.
Chris Lattner497a7a82008-11-10 04:10:34 +0000800 CallInst::Create(RegionStartFn, getCastToEmpty(D), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000801}
802
Chris Lattnera45664f2008-11-10 02:56:27 +0000803/// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
804/// mark the end of a region for the specified scoping descriptor.
805void DIFactory::InsertRegionEnd(DIDescriptor D, BasicBlock *BB) {
806 // Lazily construct llvm.dbg.region.end function.
807 if (!RegionEndFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000808 RegionEndFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_end);
809
810 // Call llvm.dbg.region.end.
Chris Lattner497a7a82008-11-10 04:10:34 +0000811 CallInst::Create(RegionEndFn, getCastToEmpty(D), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000812}
813
814/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Bill Wendlingdc817b62009-05-14 18:26:15 +0000815void DIFactory::InsertDeclare(Value *Storage, DIVariable D, BasicBlock *BB) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000816 // Cast the storage to a {}* for the call to llvm.dbg.declare.
Bill Wendlingdc817b62009-05-14 18:26:15 +0000817 Storage = new BitCastInst(Storage, EmptyStructPtr, "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000818
819 if (!DeclareFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000820 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
821
Chris Lattner497a7a82008-11-10 04:10:34 +0000822 Value *Args[] = { Storage, getCastToEmpty(D) };
Chris Lattnera45664f2008-11-10 02:56:27 +0000823 CallInst::Create(DeclareFn, Args, Args+2, "", BB);
824}
Torok Edwin620f2802008-12-16 09:07:36 +0000825
826namespace llvm {
Bill Wendlingdc817b62009-05-14 18:26:15 +0000827 /// findStopPoint - Find the stoppoint coressponding to this instruction, that
828 /// is the stoppoint that dominates this instruction.
829 const DbgStopPointInst *findStopPoint(const Instruction *Inst) {
Torok Edwin620f2802008-12-16 09:07:36 +0000830 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(Inst))
831 return DSI;
832
833 const BasicBlock *BB = Inst->getParent();
834 BasicBlock::const_iterator I = Inst, B;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000835 while (BB) {
Torok Edwin620f2802008-12-16 09:07:36 +0000836 B = BB->begin();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000837
Torok Edwin620f2802008-12-16 09:07:36 +0000838 // A BB consisting only of a terminator can't have a stoppoint.
Bill Wendlingdc817b62009-05-14 18:26:15 +0000839 while (I != B) {
840 --I;
841 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
842 return DSI;
Torok Edwin620f2802008-12-16 09:07:36 +0000843 }
Bill Wendlingdc817b62009-05-14 18:26:15 +0000844
845 // This BB didn't have a stoppoint: if there is only one predecessor, look
846 // for a stoppoint there. We could use getIDom(), but that would require
847 // dominator info.
Torok Edwin620f2802008-12-16 09:07:36 +0000848 BB = I->getParent()->getUniquePredecessor();
849 if (BB)
850 I = BB->getTerminator();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000851 }
852
Torok Edwin620f2802008-12-16 09:07:36 +0000853 return 0;
854 }
855
Bill Wendlingdc817b62009-05-14 18:26:15 +0000856 /// findBBStopPoint - Find the stoppoint corresponding to first real
857 /// (non-debug intrinsic) instruction in this Basic Block, and return the
858 /// stoppoint for it.
859 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB) {
860 for(BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +0000861 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
862 return DSI;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000863
864 // Fallback to looking for stoppoint of unique predecessor. Useful if this
865 // BB contains no stoppoints, but unique predecessor does.
Torok Edwin620f2802008-12-16 09:07:36 +0000866 BB = BB->getUniquePredecessor();
867 if (BB)
868 return findStopPoint(BB->getTerminator());
Bill Wendlingdc817b62009-05-14 18:26:15 +0000869
Torok Edwin620f2802008-12-16 09:07:36 +0000870 return 0;
871 }
872
Bill Wendlingdc817b62009-05-14 18:26:15 +0000873 Value *findDbgGlobalDeclare(GlobalVariable *V) {
Torok Edwinff7d0e92009-03-10 13:41:26 +0000874 const Module *M = V->getParent();
875 const Type *Ty = M->getTypeByName("llvm.dbg.global_variable.type");
Bill Wendlingdc817b62009-05-14 18:26:15 +0000876 if (!Ty) return 0;
877
Torok Edwinff7d0e92009-03-10 13:41:26 +0000878 Ty = PointerType::get(Ty, 0);
879
880 Value *Val = V->stripPointerCasts();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000881 for (Value::use_iterator I = Val->use_begin(), E = Val->use_end();
Torok Edwinff7d0e92009-03-10 13:41:26 +0000882 I != E; ++I) {
883 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(I)) {
884 if (CE->getOpcode() == Instruction::BitCast) {
885 Value *VV = CE;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000886
887 while (VV->hasOneUse())
Torok Edwinff7d0e92009-03-10 13:41:26 +0000888 VV = *VV->use_begin();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000889
Torok Edwinff7d0e92009-03-10 13:41:26 +0000890 if (VV->getType() == Ty)
891 return VV;
892 }
893 }
894 }
895
896 if (Val->getType() == Ty)
897 return Val;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000898
Torok Edwinff7d0e92009-03-10 13:41:26 +0000899 return 0;
900 }
901
Bill Wendlingdc817b62009-05-14 18:26:15 +0000902 /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
Torok Edwin620f2802008-12-16 09:07:36 +0000903 /// It looks through pointer casts too.
Bill Wendlingdc817b62009-05-14 18:26:15 +0000904 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts) {
Torok Edwin620f2802008-12-16 09:07:36 +0000905 if (stripCasts) {
906 V = V->stripPointerCasts();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000907
Torok Edwin620f2802008-12-16 09:07:36 +0000908 // Look for the bitcast.
909 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000910 I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +0000911 if (isa<BitCastInst>(I))
912 return findDbgDeclare(*I, false);
Bill Wendlingdc817b62009-05-14 18:26:15 +0000913
Torok Edwin620f2802008-12-16 09:07:36 +0000914 return 0;
915 }
916
Bill Wendlingdc817b62009-05-14 18:26:15 +0000917 // Find llvm.dbg.declare among uses of the instruction.
Torok Edwin620f2802008-12-16 09:07:36 +0000918 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000919 I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +0000920 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I))
921 return DDI;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000922
Torok Edwin620f2802008-12-16 09:07:36 +0000923 return 0;
924 }
Torok Edwinff7d0e92009-03-10 13:41:26 +0000925
Bill Wendlingdc817b62009-05-14 18:26:15 +0000926 bool getLocationInfo(const Value *V, std::string &DisplayName,
927 std::string &Type, unsigned &LineNo, std::string &File,
928 std::string &Dir) {
Torok Edwinff7d0e92009-03-10 13:41:26 +0000929 DICompileUnit Unit;
930 DIType TypeD;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000931
Torok Edwinff7d0e92009-03-10 13:41:26 +0000932 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
933 Value *DIGV = findDbgGlobalDeclare(GV);
Bill Wendlingdc817b62009-05-14 18:26:15 +0000934 if (!DIGV) return false;
Torok Edwinff7d0e92009-03-10 13:41:26 +0000935 DIGlobalVariable Var(cast<GlobalVariable>(DIGV));
Bill Wendlingdc817b62009-05-14 18:26:15 +0000936
Bill Wendling0582ae92009-03-13 04:39:26 +0000937 Var.getDisplayName(DisplayName);
Torok Edwinff7d0e92009-03-10 13:41:26 +0000938 LineNo = Var.getLineNumber();
939 Unit = Var.getCompileUnit();
940 TypeD = Var.getType();
941 } else {
942 const DbgDeclareInst *DDI = findDbgDeclare(V);
Bill Wendlingdc817b62009-05-14 18:26:15 +0000943 if (!DDI) return false;
Torok Edwinff7d0e92009-03-10 13:41:26 +0000944 DIVariable Var(cast<GlobalVariable>(DDI->getVariable()));
Bill Wendlingdc817b62009-05-14 18:26:15 +0000945
Bill Wendling0582ae92009-03-13 04:39:26 +0000946 Var.getName(DisplayName);
Torok Edwinff7d0e92009-03-10 13:41:26 +0000947 LineNo = Var.getLineNumber();
948 Unit = Var.getCompileUnit();
949 TypeD = Var.getType();
950 }
Bill Wendlingdc817b62009-05-14 18:26:15 +0000951
Bill Wendling0582ae92009-03-13 04:39:26 +0000952 TypeD.getName(Type);
953 Unit.getFilename(File);
954 Unit.getDirectory(Dir);
Torok Edwinff7d0e92009-03-10 13:41:26 +0000955 return true;
956 }
Torok Edwin620f2802008-12-16 09:07:36 +0000957}
958
Bill Wendlingdc817b62009-05-14 18:26:15 +0000959/// dump - Print descriptor.
Bill Wendling16de0132009-05-05 22:19:25 +0000960void DIDescriptor::dump() const {
Bill Wendlingad9c2782009-05-08 20:28:06 +0000961 cerr << "[" << dwarf::TagString(getTag()) << "] ";
Devang Patel9af2fa82009-06-23 22:25:41 +0000962 cerr << std::hex << "[GV:" << DbgGV << "]" << std::dec;
Bill Wendling16de0132009-05-05 22:19:25 +0000963}
964
Bill Wendlingdc817b62009-05-14 18:26:15 +0000965/// dump - Print compile unit.
Devang Patelbf3f5a02009-01-30 01:03:10 +0000966void DICompileUnit::dump() const {
Devang Pateld8e880c2009-02-24 23:15:09 +0000967 if (getLanguage())
968 cerr << " [" << dwarf::LanguageString(getLanguage()) << "] ";
Bill Wendlingccbdc7a2009-03-09 05:04:40 +0000969
Bill Wendling0582ae92009-03-13 04:39:26 +0000970 std::string Res1, Res2;
971 cerr << " [" << getDirectory(Res1) << "/" << getFilename(Res2) << " ]";
Devang Patelbf3f5a02009-01-30 01:03:10 +0000972}
973
Bill Wendlingdc817b62009-05-14 18:26:15 +0000974/// dump - Print type.
Devang Patelbf3f5a02009-01-30 01:03:10 +0000975void DIType::dump() const {
976 if (isNull()) return;
Bill Wendlingccbdc7a2009-03-09 05:04:40 +0000977
Bill Wendling0582ae92009-03-13 04:39:26 +0000978 std::string Res;
979 if (!getName(Res).empty())
980 cerr << " [" << Res << "] ";
Bill Wendlingccbdc7a2009-03-09 05:04:40 +0000981
Devang Patelbf3f5a02009-01-30 01:03:10 +0000982 unsigned Tag = getTag();
983 cerr << " [" << dwarf::TagString(Tag) << "] ";
Bill Wendlingccbdc7a2009-03-09 05:04:40 +0000984
Devang Patelbf3f5a02009-01-30 01:03:10 +0000985 // TODO : Print context
986 getCompileUnit().dump();
987 cerr << " ["
988 << getLineNumber() << ", "
989 << getSizeInBits() << ", "
990 << getAlignInBits() << ", "
991 << getOffsetInBits()
992 << "] ";
Bill Wendlingccbdc7a2009-03-09 05:04:40 +0000993
Devang Patelbf3f5a02009-01-30 01:03:10 +0000994 if (isPrivate())
995 cerr << " [private] ";
996 else if (isProtected())
997 cerr << " [protected] ";
Bill Wendlingccbdc7a2009-03-09 05:04:40 +0000998
Devang Patelbf3f5a02009-01-30 01:03:10 +0000999 if (isForwardDecl())
1000 cerr << " [fwd] ";
1001
1002 if (isBasicType(Tag))
Devang Patel9af2fa82009-06-23 22:25:41 +00001003 DIBasicType(DbgGV).dump();
Devang Patelbf3f5a02009-01-30 01:03:10 +00001004 else if (isDerivedType(Tag))
Devang Patel9af2fa82009-06-23 22:25:41 +00001005 DIDerivedType(DbgGV).dump();
Devang Patelbf3f5a02009-01-30 01:03:10 +00001006 else if (isCompositeType(Tag))
Devang Patel9af2fa82009-06-23 22:25:41 +00001007 DICompositeType(DbgGV).dump();
Devang Patelbf3f5a02009-01-30 01:03:10 +00001008 else {
1009 cerr << "Invalid DIType\n";
1010 return;
1011 }
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001012
Devang Patelbf3f5a02009-01-30 01:03:10 +00001013 cerr << "\n";
1014}
1015
Bill Wendlingdc817b62009-05-14 18:26:15 +00001016/// dump - Print basic type.
Devang Patelbf3f5a02009-01-30 01:03:10 +00001017void DIBasicType::dump() const {
1018 cerr << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
Devang Patelbf3f5a02009-01-30 01:03:10 +00001019}
1020
Bill Wendlingdc817b62009-05-14 18:26:15 +00001021/// dump - Print derived type.
Devang Patelbf3f5a02009-01-30 01:03:10 +00001022void DIDerivedType::dump() const {
1023 cerr << "\n\t Derived From: "; getTypeDerivedFrom().dump();
1024}
1025
Bill Wendlingdc817b62009-05-14 18:26:15 +00001026/// dump - Print composite type.
Devang Patelbf3f5a02009-01-30 01:03:10 +00001027void DICompositeType::dump() const {
1028 DIArray A = getTypeArray();
1029 if (A.isNull())
1030 return;
1031 cerr << " [" << A.getNumElements() << " elements]";
1032}
1033
Bill Wendlingdc817b62009-05-14 18:26:15 +00001034/// dump - Print global.
Devang Patelbf3f5a02009-01-30 01:03:10 +00001035void DIGlobal::dump() const {
Bill Wendling0582ae92009-03-13 04:39:26 +00001036 std::string Res;
1037 if (!getName(Res).empty())
1038 cerr << " [" << Res << "] ";
Devang Patelbf3f5a02009-01-30 01:03:10 +00001039
Devang Patelbf3f5a02009-01-30 01:03:10 +00001040 unsigned Tag = getTag();
1041 cerr << " [" << dwarf::TagString(Tag) << "] ";
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001042
Devang Patelbf3f5a02009-01-30 01:03:10 +00001043 // TODO : Print context
1044 getCompileUnit().dump();
1045 cerr << " [" << getLineNumber() << "] ";
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001046
Devang Patelbf3f5a02009-01-30 01:03:10 +00001047 if (isLocalToUnit())
1048 cerr << " [local] ";
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001049
Devang Patelbf3f5a02009-01-30 01:03:10 +00001050 if (isDefinition())
1051 cerr << " [def] ";
1052
1053 if (isGlobalVariable(Tag))
Devang Patel9af2fa82009-06-23 22:25:41 +00001054 DIGlobalVariable(DbgGV).dump();
Devang Patelbf3f5a02009-01-30 01:03:10 +00001055
1056 cerr << "\n";
1057}
1058
Bill Wendlingdc817b62009-05-14 18:26:15 +00001059/// dump - Print subprogram.
Devang Patelbf3f5a02009-01-30 01:03:10 +00001060void DISubprogram::dump() const {
1061 DIGlobal::dump();
1062}
1063
Bill Wendlingdc817b62009-05-14 18:26:15 +00001064/// dump - Print global variable.
Devang Patelbf3f5a02009-01-30 01:03:10 +00001065void DIGlobalVariable::dump() const {
1066 cerr << " ["; getGlobal()->dump(); cerr << "] ";
1067}
1068
Bill Wendlingdc817b62009-05-14 18:26:15 +00001069/// dump - Print variable.
Devang Patelbf3f5a02009-01-30 01:03:10 +00001070void DIVariable::dump() const {
Bill Wendling0582ae92009-03-13 04:39:26 +00001071 std::string Res;
1072 if (!getName(Res).empty())
1073 cerr << " [" << Res << "] ";
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001074
Devang Patelbf3f5a02009-01-30 01:03:10 +00001075 getCompileUnit().dump();
1076 cerr << " [" << getLineNumber() << "] ";
1077 getType().dump();
1078 cerr << "\n";
1079}