blob: f537d55927377ca1a5f1465550c09c0851d48a6a [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 Patel9e529c32009-07-02 01:15:24 +000024#include "llvm/Support/DebugLoc.h"
Devang Patelbf3f5a02009-01-30 01:03:10 +000025#include "llvm/Support/Streams.h"
Bill Wendlingdc817b62009-05-14 18:26:15 +000026
Chris Lattnera45664f2008-11-10 02:56:27 +000027using namespace llvm;
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000028using namespace llvm::dwarf;
Chris Lattnera45664f2008-11-10 02:56:27 +000029
30//===----------------------------------------------------------------------===//
31// DIDescriptor
32//===----------------------------------------------------------------------===//
33
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000034/// ValidDebugInfo - Return true if V represents valid debug info value.
35bool DIDescriptor::ValidDebugInfo(Value *V, CodeGenOpt::Level OptLevel) {
36 if (!V)
37 return false;
38
39 GlobalVariable *GV = dyn_cast<GlobalVariable>(V->stripPointerCasts());
40 if (!GV)
41 return false;
42
43 if (!GV->hasInternalLinkage () && !GV->hasLinkOnceLinkage())
44 return false;
45
46 DIDescriptor DI(GV);
47
48 // Check current version. Allow Version6 for now.
49 unsigned Version = DI.getVersion();
50 if (Version != LLVMDebugVersion && Version != LLVMDebugVersion6)
51 return false;
52
53 unsigned Tag = DI.getTag();
54 switch (Tag) {
55 case DW_TAG_variable:
56 assert(DIVariable(GV).Verify() && "Invalid DebugInfo value");
57 break;
58 case DW_TAG_compile_unit:
59 assert(DICompileUnit(GV).Verify() && "Invalid DebugInfo value");
60 break;
61 case DW_TAG_subprogram:
62 assert(DISubprogram(GV).Verify() && "Invalid DebugInfo value");
63 break;
64 case DW_TAG_lexical_block:
Bill Wendlingdc817b62009-05-14 18:26:15 +000065 // FIXME: This interfers with the quality of generated code during
66 // optimization.
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000067 if (OptLevel != CodeGenOpt::None)
68 return false;
Bill Wendlingdc817b62009-05-14 18:26:15 +000069 // FALLTHROUGH
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000070 default:
71 break;
72 }
73
74 return true;
75}
76
Devang Patel9af2fa82009-06-23 22:25:41 +000077DIDescriptor::DIDescriptor(GlobalVariable *GV, unsigned RequiredTag) {
78 DbgGV = GV;
Chris Lattnera45664f2008-11-10 02:56:27 +000079
Bill Wendlingdc817b62009-05-14 18:26:15 +000080 // If this is non-null, check to see if the Tag matches. If not, set to null.
Chris Lattnera45664f2008-11-10 02:56:27 +000081 if (GV && getTag() != RequiredTag)
Devang Patel9af2fa82009-06-23 22:25:41 +000082 DbgGV = 0;
Chris Lattnera45664f2008-11-10 02:56:27 +000083}
84
Bill Wendling0582ae92009-03-13 04:39:26 +000085const std::string &
86DIDescriptor::getStringField(unsigned Elt, std::string &Result) const {
Devang Patel9af2fa82009-06-23 22:25:41 +000087 if (DbgGV == 0) {
Bill Wendling0582ae92009-03-13 04:39:26 +000088 Result.clear();
89 return Result;
90 }
Chris Lattnera45664f2008-11-10 02:56:27 +000091
Devang Patel9af2fa82009-06-23 22:25:41 +000092 Constant *C = DbgGV->getInitializer();
Bill Wendling0582ae92009-03-13 04:39:26 +000093 if (C == 0 || Elt >= C->getNumOperands()) {
94 Result.clear();
95 return Result;
96 }
Bill Wendlingdc817b62009-05-14 18:26:15 +000097
Chris Lattnera45664f2008-11-10 02:56:27 +000098 // Fills in the string if it succeeds
Bill Wendling0582ae92009-03-13 04:39:26 +000099 if (!GetConstantStringInfo(C->getOperand(Elt), Result))
100 Result.clear();
101
102 return Result;
Chris Lattnera45664f2008-11-10 02:56:27 +0000103}
104
105uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
Devang Patel9af2fa82009-06-23 22:25:41 +0000106 if (DbgGV == 0) return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000107
Devang Patel9af2fa82009-06-23 22:25:41 +0000108 Constant *C = DbgGV->getInitializer();
Chris Lattnera45664f2008-11-10 02:56:27 +0000109 if (C == 0 || Elt >= C->getNumOperands())
110 return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000111
Chris Lattnera45664f2008-11-10 02:56:27 +0000112 if (ConstantInt *CI = dyn_cast<ConstantInt>(C->getOperand(Elt)))
113 return CI->getZExtValue();
114 return 0;
115}
116
Chris Lattnera45664f2008-11-10 02:56:27 +0000117DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
Devang Patel9af2fa82009-06-23 22:25:41 +0000118 if (DbgGV == 0) return DIDescriptor();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000119
Devang Patel9af2fa82009-06-23 22:25:41 +0000120 Constant *C = DbgGV->getInitializer();
Chris Lattnera45664f2008-11-10 02:56:27 +0000121 if (C == 0 || Elt >= C->getNumOperands())
122 return DIDescriptor();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000123
Chris Lattnera45664f2008-11-10 02:56:27 +0000124 C = C->getOperand(Elt);
125 return DIDescriptor(dyn_cast<GlobalVariable>(C->stripPointerCasts()));
126}
127
128GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
Devang Patel9af2fa82009-06-23 22:25:41 +0000129 if (DbgGV == 0) return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000130
Devang Patel9af2fa82009-06-23 22:25:41 +0000131 Constant *C = DbgGV->getInitializer();
Chris Lattnera45664f2008-11-10 02:56:27 +0000132 if (C == 0 || Elt >= C->getNumOperands())
133 return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000134
Chris Lattnera45664f2008-11-10 02:56:27 +0000135 C = C->getOperand(Elt);
Chris Lattnera45664f2008-11-10 02:56:27 +0000136 return dyn_cast<GlobalVariable>(C->stripPointerCasts());
137}
138
Chris Lattnera45664f2008-11-10 02:56:27 +0000139//===----------------------------------------------------------------------===//
140// Simple Descriptor Constructors and other Methods
141//===----------------------------------------------------------------------===//
142
Bill Wendlingdc817b62009-05-14 18:26:15 +0000143// Needed by DIVariable::getType().
Devang Patel9af2fa82009-06-23 22:25:41 +0000144DIType::DIType(GlobalVariable *GV) : DIDescriptor(GV) {
145 if (!GV) return;
Torok Edwinb07fbd92008-12-13 08:25:29 +0000146 unsigned tag = getTag();
147 if (tag != dwarf::DW_TAG_base_type && !DIDerivedType::isDerivedType(tag) &&
148 !DICompositeType::isCompositeType(tag))
Devang Patel9af2fa82009-06-23 22:25:41 +0000149 DbgGV = 0;
Torok Edwinb07fbd92008-12-13 08:25:29 +0000150}
Chris Lattnera45664f2008-11-10 02:56:27 +0000151
152/// isDerivedType - Return true if the specified tag is legal for
153/// DIDerivedType.
Devang Patel486938f2009-01-12 21:38:43 +0000154bool DIType::isDerivedType(unsigned Tag) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000155 switch (Tag) {
156 case dwarf::DW_TAG_typedef:
157 case dwarf::DW_TAG_pointer_type:
158 case dwarf::DW_TAG_reference_type:
159 case dwarf::DW_TAG_const_type:
160 case dwarf::DW_TAG_volatile_type:
161 case dwarf::DW_TAG_restrict_type:
162 case dwarf::DW_TAG_member:
163 case dwarf::DW_TAG_inheritance:
164 return true;
165 default:
166 // FIXME: Even though it doesn't make sense, CompositeTypes are current
167 // modelled as DerivedTypes, this should return true for them as well.
168 return false;
169 }
170}
171
Chris Lattnera45664f2008-11-10 02:56:27 +0000172/// isCompositeType - Return true if the specified tag is legal for
173/// DICompositeType.
Devang Patel486938f2009-01-12 21:38:43 +0000174bool DIType::isCompositeType(unsigned TAG) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000175 switch (TAG) {
176 case dwarf::DW_TAG_array_type:
177 case dwarf::DW_TAG_structure_type:
178 case dwarf::DW_TAG_union_type:
179 case dwarf::DW_TAG_enumeration_type:
180 case dwarf::DW_TAG_vector_type:
181 case dwarf::DW_TAG_subroutine_type:
Devang Patel25cb0d72009-03-25 03:52:06 +0000182 case dwarf::DW_TAG_class_type:
Chris Lattnera45664f2008-11-10 02:56:27 +0000183 return true;
184 default:
185 return false;
186 }
187}
188
Chris Lattnera45664f2008-11-10 02:56:27 +0000189/// isVariable - Return true if the specified tag is legal for DIVariable.
190bool DIVariable::isVariable(unsigned Tag) {
191 switch (Tag) {
192 case dwarf::DW_TAG_auto_variable:
193 case dwarf::DW_TAG_arg_variable:
194 case dwarf::DW_TAG_return_variable:
195 return true;
196 default:
197 return false;
198 }
199}
200
Devang Patel68afdc32009-01-05 18:33:01 +0000201unsigned DIArray::getNumElements() const {
Devang Patel9af2fa82009-06-23 22:25:41 +0000202 assert (DbgGV && "Invalid DIArray");
203 Constant *C = DbgGV->getInitializer();
Devang Patel68afdc32009-01-05 18:33:01 +0000204 assert (C && "Invalid DIArray initializer");
205 return C->getNumOperands();
206}
Chris Lattnera45664f2008-11-10 02:56:27 +0000207
Devang Patelb79b5352009-01-19 23:21:49 +0000208/// Verify - Verify that a compile unit is well formed.
209bool DICompileUnit::Verify() const {
210 if (isNull())
211 return false;
Bill Wendling0582ae92009-03-13 04:39:26 +0000212 std::string Res;
213 if (getFilename(Res).empty())
214 return false;
Devang Patelb79b5352009-01-19 23:21:49 +0000215 // It is possible that directory and produce string is empty.
Bill Wendling0582ae92009-03-13 04:39:26 +0000216 return true;
Devang Patelb79b5352009-01-19 23:21:49 +0000217}
218
219/// Verify - Verify that a type descriptor is well formed.
220bool DIType::Verify() const {
221 if (isNull())
222 return false;
223 if (getContext().isNull())
224 return false;
225
226 DICompileUnit CU = getCompileUnit();
227 if (!CU.isNull() && !CU.Verify())
228 return false;
229 return true;
230}
231
232/// Verify - Verify that a composite type descriptor is well formed.
233bool DICompositeType::Verify() const {
234 if (isNull())
235 return false;
236 if (getContext().isNull())
237 return false;
238
239 DICompileUnit CU = getCompileUnit();
240 if (!CU.isNull() && !CU.Verify())
241 return false;
242 return true;
243}
244
245/// Verify - Verify that a subprogram descriptor is well formed.
246bool DISubprogram::Verify() const {
247 if (isNull())
248 return false;
249
250 if (getContext().isNull())
251 return false;
252
253 DICompileUnit CU = getCompileUnit();
254 if (!CU.Verify())
255 return false;
256
257 DICompositeType Ty = getType();
258 if (!Ty.isNull() && !Ty.Verify())
259 return false;
260 return true;
261}
262
263/// Verify - Verify that a global variable descriptor is well formed.
264bool DIGlobalVariable::Verify() const {
265 if (isNull())
266 return false;
267
268 if (getContext().isNull())
269 return false;
270
271 DICompileUnit CU = getCompileUnit();
Chris Lattnere3f6cea2009-05-05 04:55:56 +0000272 if (!CU.isNull() && !CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000273 return false;
274
275 DIType Ty = getType();
276 if (!Ty.Verify())
277 return false;
278
279 if (!getGlobal())
280 return false;
281
282 return true;
283}
284
285/// Verify - Verify that a variable descriptor is well formed.
286bool DIVariable::Verify() const {
287 if (isNull())
288 return false;
289
290 if (getContext().isNull())
291 return false;
292
293 DIType Ty = getType();
294 if (!Ty.Verify())
295 return false;
296
Devang Patelb79b5352009-01-19 23:21:49 +0000297 return true;
298}
299
Devang Patel36375ee2009-02-17 21:23:59 +0000300/// getOriginalTypeSize - If this type is derived from a base type then
301/// return base type size.
302uint64_t DIDerivedType::getOriginalTypeSize() const {
303 if (getTag() != dwarf::DW_TAG_member)
304 return getSizeInBits();
305 DIType BT = getTypeDerivedFrom();
306 if (BT.getTag() != dwarf::DW_TAG_base_type)
307 return getSizeInBits();
308 return BT.getSizeInBits();
309}
Devang Patelb79b5352009-01-19 23:21:49 +0000310
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000311/// describes - Return true if this subprogram provides debugging
312/// information for the function F.
313bool DISubprogram::describes(const Function *F) {
314 assert (F && "Invalid function");
315 std::string Name;
316 getLinkageName(Name);
317 if (Name.empty())
318 getName(Name);
319 if (!Name.empty() && (strcmp(Name.c_str(), F->getNameStart()) == false))
320 return true;
321 return false;
322}
323
Chris Lattnera45664f2008-11-10 02:56:27 +0000324//===----------------------------------------------------------------------===//
Devang Patel7136a652009-07-01 22:10:23 +0000325// DIDescriptor: dump routines for all descriptors.
326//===----------------------------------------------------------------------===//
327
328
329/// dump - Print descriptor.
330void DIDescriptor::dump() const {
331 cerr << "[" << dwarf::TagString(getTag()) << "] ";
332 cerr << std::hex << "[GV:" << DbgGV << "]" << std::dec;
333}
334
335/// dump - Print compile unit.
336void DICompileUnit::dump() const {
337 if (getLanguage())
338 cerr << " [" << dwarf::LanguageString(getLanguage()) << "] ";
339
340 std::string Res1, Res2;
341 cerr << " [" << getDirectory(Res1) << "/" << getFilename(Res2) << " ]";
342}
343
344/// dump - Print type.
345void DIType::dump() const {
346 if (isNull()) return;
347
348 std::string Res;
349 if (!getName(Res).empty())
350 cerr << " [" << Res << "] ";
351
352 unsigned Tag = getTag();
353 cerr << " [" << dwarf::TagString(Tag) << "] ";
354
355 // TODO : Print context
356 getCompileUnit().dump();
357 cerr << " ["
358 << getLineNumber() << ", "
359 << getSizeInBits() << ", "
360 << getAlignInBits() << ", "
361 << getOffsetInBits()
362 << "] ";
363
364 if (isPrivate())
365 cerr << " [private] ";
366 else if (isProtected())
367 cerr << " [protected] ";
368
369 if (isForwardDecl())
370 cerr << " [fwd] ";
371
372 if (isBasicType(Tag))
373 DIBasicType(DbgGV).dump();
374 else if (isDerivedType(Tag))
375 DIDerivedType(DbgGV).dump();
376 else if (isCompositeType(Tag))
377 DICompositeType(DbgGV).dump();
378 else {
379 cerr << "Invalid DIType\n";
380 return;
381 }
382
383 cerr << "\n";
384}
385
386/// dump - Print basic type.
387void DIBasicType::dump() const {
388 cerr << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
389}
390
391/// dump - Print derived type.
392void DIDerivedType::dump() const {
393 cerr << "\n\t Derived From: "; getTypeDerivedFrom().dump();
394}
395
396/// dump - Print composite type.
397void DICompositeType::dump() const {
398 DIArray A = getTypeArray();
399 if (A.isNull())
400 return;
401 cerr << " [" << A.getNumElements() << " elements]";
402}
403
404/// dump - Print global.
405void DIGlobal::dump() const {
406 std::string Res;
407 if (!getName(Res).empty())
408 cerr << " [" << Res << "] ";
409
410 unsigned Tag = getTag();
411 cerr << " [" << dwarf::TagString(Tag) << "] ";
412
413 // TODO : Print context
414 getCompileUnit().dump();
415 cerr << " [" << getLineNumber() << "] ";
416
417 if (isLocalToUnit())
418 cerr << " [local] ";
419
420 if (isDefinition())
421 cerr << " [def] ";
422
423 if (isGlobalVariable(Tag))
424 DIGlobalVariable(DbgGV).dump();
425
426 cerr << "\n";
427}
428
429/// dump - Print subprogram.
430void DISubprogram::dump() const {
431 DIGlobal::dump();
432}
433
434/// dump - Print global variable.
435void DIGlobalVariable::dump() const {
436 cerr << " ["; getGlobal()->dump(); cerr << "] ";
437}
438
439/// dump - Print variable.
440void DIVariable::dump() const {
441 std::string Res;
442 if (!getName(Res).empty())
443 cerr << " [" << Res << "] ";
444
445 getCompileUnit().dump();
446 cerr << " [" << getLineNumber() << "] ";
447 getType().dump();
448 cerr << "\n";
449}
450
451//===----------------------------------------------------------------------===//
Chris Lattnera45664f2008-11-10 02:56:27 +0000452// DIFactory: Basic Helpers
453//===----------------------------------------------------------------------===//
454
Bill Wendlingdc817b62009-05-14 18:26:15 +0000455DIFactory::DIFactory(Module &m)
456 : M(m), StopPointFn(0), FuncStartFn(0), RegionStartFn(0), RegionEndFn(0),
457 DeclareFn(0) {
Chris Lattner0fd38062009-07-01 04:13:31 +0000458 EmptyStructPtr = PointerType::getUnqual(StructType::get());
Chris Lattner497a7a82008-11-10 04:10:34 +0000459}
460
461/// getCastToEmpty - Return this descriptor as a Constant* with type '{}*'.
462/// This is only valid when the descriptor is non-null.
463Constant *DIFactory::getCastToEmpty(DIDescriptor D) {
464 if (D.isNull()) return Constant::getNullValue(EmptyStructPtr);
465 return ConstantExpr::getBitCast(D.getGV(), EmptyStructPtr);
466}
467
Chris Lattnera45664f2008-11-10 02:56:27 +0000468Constant *DIFactory::GetTagConstant(unsigned TAG) {
Devang Patel6906ba52009-01-20 19:22:03 +0000469 assert((TAG & LLVMDebugVersionMask) == 0 &&
Chris Lattnera45664f2008-11-10 02:56:27 +0000470 "Tag too large for debug encoding!");
Devang Patel6906ba52009-01-20 19:22:03 +0000471 return ConstantInt::get(Type::Int32Ty, TAG | LLVMDebugVersion);
Chris Lattnera45664f2008-11-10 02:56:27 +0000472}
473
474Constant *DIFactory::GetStringConstant(const std::string &String) {
475 // Check string cache for previous edition.
476 Constant *&Slot = StringCache[String];
477
478 // Return Constant if previously defined.
479 if (Slot) return Slot;
480
481 const PointerType *DestTy = PointerType::getUnqual(Type::Int8Ty);
482
Dan Gohmana119de82009-06-14 23:30:43 +0000483 // If empty string then use a i8* null instead.
Chris Lattnera45664f2008-11-10 02:56:27 +0000484 if (String.empty())
485 return Slot = ConstantPointerNull::get(DestTy);
486
487 // Construct string as an llvm constant.
488 Constant *ConstStr = ConstantArray::get(String);
489
490 // Otherwise create and return a new string global.
491 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
492 GlobalVariable::InternalLinkage,
493 ConstStr, ".str", &M);
494 StrGV->setSection("llvm.metadata");
495 return Slot = ConstantExpr::getBitCast(StrGV, DestTy);
496}
497
Chris Lattnera45664f2008-11-10 02:56:27 +0000498//===----------------------------------------------------------------------===//
499// DIFactory: Primary Constructors
500//===----------------------------------------------------------------------===//
501
Chris Lattnera45664f2008-11-10 02:56:27 +0000502/// GetOrCreateArray - Create an descriptor for an array of descriptors.
503/// This implicitly uniques the arrays created.
504DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
505 SmallVector<Constant*, 16> Elts;
506
507 for (unsigned i = 0; i != NumTys; ++i)
Chris Lattner497a7a82008-11-10 04:10:34 +0000508 Elts.push_back(getCastToEmpty(Tys[i]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000509
Chris Lattner497a7a82008-11-10 04:10:34 +0000510 Constant *Init = ConstantArray::get(ArrayType::get(EmptyStructPtr,
511 Elts.size()),
Jay Foade3e51c02009-05-21 09:52:38 +0000512 Elts.data(), Elts.size());
Chris Lattnera45664f2008-11-10 02:56:27 +0000513 // If we already have this array, just return the uniqued version.
514 DIDescriptor &Entry = SimpleConstantCache[Init];
515 if (!Entry.isNull()) return DIArray(Entry.getGV());
516
517 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
518 GlobalValue::InternalLinkage,
519 Init, "llvm.dbg.array", &M);
520 GV->setSection("llvm.metadata");
521 Entry = DIDescriptor(GV);
522 return DIArray(GV);
523}
524
525/// GetOrCreateSubrange - Create a descriptor for a value range. This
526/// implicitly uniques the values returned.
527DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
528 Constant *Elts[] = {
529 GetTagConstant(dwarf::DW_TAG_subrange_type),
530 ConstantInt::get(Type::Int64Ty, Lo),
531 ConstantInt::get(Type::Int64Ty, Hi)
532 };
533
534 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
535
536 // If we already have this range, just return the uniqued version.
537 DIDescriptor &Entry = SimpleConstantCache[Init];
538 if (!Entry.isNull()) return DISubrange(Entry.getGV());
539
540 M.addTypeName("llvm.dbg.subrange.type", Init->getType());
541
542 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
543 GlobalValue::InternalLinkage,
544 Init, "llvm.dbg.subrange", &M);
545 GV->setSection("llvm.metadata");
546 Entry = DIDescriptor(GV);
547 return DISubrange(GV);
548}
549
550
551
552/// CreateCompileUnit - Create a new descriptor for the specified compile
553/// unit. Note that this does not unique compile units within the module.
554DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
555 const std::string &Filename,
556 const std::string &Directory,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000557 const std::string &Producer,
Devang Pateldd9db662009-01-30 18:20:31 +0000558 bool isMain,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000559 bool isOptimized,
Devang Patel13319ce2009-02-17 22:43:44 +0000560 const char *Flags,
561 unsigned RunTimeVer) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000562 Constant *Elts[] = {
563 GetTagConstant(dwarf::DW_TAG_compile_unit),
Devang Patel13e16b62009-06-26 01:49:18 +0000564 Constant::getNullValue(EmptyStructPtr),
Chris Lattnera45664f2008-11-10 02:56:27 +0000565 ConstantInt::get(Type::Int32Ty, LangID),
566 GetStringConstant(Filename),
567 GetStringConstant(Directory),
Devang Patel3b64c6b2009-01-23 22:33:47 +0000568 GetStringConstant(Producer),
Devang Pateldd9db662009-01-30 18:20:31 +0000569 ConstantInt::get(Type::Int1Ty, isMain),
Devang Patel3b64c6b2009-01-23 22:33:47 +0000570 ConstantInt::get(Type::Int1Ty, isOptimized),
Devang Patel13319ce2009-02-17 22:43:44 +0000571 GetStringConstant(Flags),
572 ConstantInt::get(Type::Int32Ty, RunTimeVer)
Chris Lattnera45664f2008-11-10 02:56:27 +0000573 };
574
575 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
576
577 M.addTypeName("llvm.dbg.compile_unit.type", Init->getType());
578 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
Devang Patel13e16b62009-06-26 01:49:18 +0000579 GlobalValue::LinkOnceAnyLinkage,
Chris Lattnera45664f2008-11-10 02:56:27 +0000580 Init, "llvm.dbg.compile_unit", &M);
581 GV->setSection("llvm.metadata");
582 return DICompileUnit(GV);
583}
584
585/// CreateEnumerator - Create a single enumerator value.
586DIEnumerator DIFactory::CreateEnumerator(const std::string &Name, uint64_t Val){
587 Constant *Elts[] = {
588 GetTagConstant(dwarf::DW_TAG_enumerator),
589 GetStringConstant(Name),
590 ConstantInt::get(Type::Int64Ty, Val)
591 };
592
593 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
594
595 M.addTypeName("llvm.dbg.enumerator.type", Init->getType());
596 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
597 GlobalValue::InternalLinkage,
598 Init, "llvm.dbg.enumerator", &M);
599 GV->setSection("llvm.metadata");
600 return DIEnumerator(GV);
601}
602
603
604/// CreateBasicType - Create a basic type like int, float, etc.
605DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
Bill Wendling0582ae92009-03-13 04:39:26 +0000606 const std::string &Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000607 DICompileUnit CompileUnit,
608 unsigned LineNumber,
609 uint64_t SizeInBits,
610 uint64_t AlignInBits,
611 uint64_t OffsetInBits, unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000612 unsigned Encoding) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000613 Constant *Elts[] = {
614 GetTagConstant(dwarf::DW_TAG_base_type),
Chris Lattner497a7a82008-11-10 04:10:34 +0000615 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000616 GetStringConstant(Name),
Chris Lattner497a7a82008-11-10 04:10:34 +0000617 getCastToEmpty(CompileUnit),
Chris Lattnera45664f2008-11-10 02:56:27 +0000618 ConstantInt::get(Type::Int32Ty, LineNumber),
619 ConstantInt::get(Type::Int64Ty, SizeInBits),
620 ConstantInt::get(Type::Int64Ty, AlignInBits),
621 ConstantInt::get(Type::Int64Ty, OffsetInBits),
622 ConstantInt::get(Type::Int32Ty, Flags),
Devang Pateldd9db662009-01-30 18:20:31 +0000623 ConstantInt::get(Type::Int32Ty, Encoding)
Chris Lattnera45664f2008-11-10 02:56:27 +0000624 };
625
626 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
627
628 M.addTypeName("llvm.dbg.basictype.type", Init->getType());
629 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
630 GlobalValue::InternalLinkage,
631 Init, "llvm.dbg.basictype", &M);
632 GV->setSection("llvm.metadata");
633 return DIBasicType(GV);
634}
635
636/// CreateDerivedType - Create a derived type like const qualified type,
637/// pointer, typedef, etc.
638DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
639 DIDescriptor Context,
640 const std::string &Name,
641 DICompileUnit CompileUnit,
642 unsigned LineNumber,
643 uint64_t SizeInBits,
644 uint64_t AlignInBits,
645 uint64_t OffsetInBits,
646 unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000647 DIType DerivedFrom) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000648 Constant *Elts[] = {
649 GetTagConstant(Tag),
Chris Lattner497a7a82008-11-10 04:10:34 +0000650 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000651 GetStringConstant(Name),
Chris Lattner497a7a82008-11-10 04:10:34 +0000652 getCastToEmpty(CompileUnit),
Chris Lattnera45664f2008-11-10 02:56:27 +0000653 ConstantInt::get(Type::Int32Ty, LineNumber),
654 ConstantInt::get(Type::Int64Ty, SizeInBits),
655 ConstantInt::get(Type::Int64Ty, AlignInBits),
656 ConstantInt::get(Type::Int64Ty, OffsetInBits),
657 ConstantInt::get(Type::Int32Ty, Flags),
Devang Pateldd9db662009-01-30 18:20:31 +0000658 getCastToEmpty(DerivedFrom)
Chris Lattnera45664f2008-11-10 02:56:27 +0000659 };
660
661 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
662
663 M.addTypeName("llvm.dbg.derivedtype.type", Init->getType());
664 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
665 GlobalValue::InternalLinkage,
666 Init, "llvm.dbg.derivedtype", &M);
667 GV->setSection("llvm.metadata");
668 return DIDerivedType(GV);
669}
670
671/// CreateCompositeType - Create a composite type like array, struct, etc.
672DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
673 DIDescriptor Context,
674 const std::string &Name,
675 DICompileUnit CompileUnit,
676 unsigned LineNumber,
677 uint64_t SizeInBits,
678 uint64_t AlignInBits,
679 uint64_t OffsetInBits,
680 unsigned Flags,
681 DIType DerivedFrom,
Devang Patel13319ce2009-02-17 22:43:44 +0000682 DIArray Elements,
683 unsigned RuntimeLang) {
Devang Patel854967e2008-12-17 22:39:29 +0000684
Chris Lattnera45664f2008-11-10 02:56:27 +0000685 Constant *Elts[] = {
686 GetTagConstant(Tag),
Chris Lattner497a7a82008-11-10 04:10:34 +0000687 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000688 GetStringConstant(Name),
Chris Lattner497a7a82008-11-10 04:10:34 +0000689 getCastToEmpty(CompileUnit),
Chris Lattnera45664f2008-11-10 02:56:27 +0000690 ConstantInt::get(Type::Int32Ty, LineNumber),
691 ConstantInt::get(Type::Int64Ty, SizeInBits),
692 ConstantInt::get(Type::Int64Ty, AlignInBits),
693 ConstantInt::get(Type::Int64Ty, OffsetInBits),
694 ConstantInt::get(Type::Int32Ty, Flags),
Chris Lattner497a7a82008-11-10 04:10:34 +0000695 getCastToEmpty(DerivedFrom),
Devang Patel13319ce2009-02-17 22:43:44 +0000696 getCastToEmpty(Elements),
697 ConstantInt::get(Type::Int32Ty, RuntimeLang)
Chris Lattnera45664f2008-11-10 02:56:27 +0000698 };
699
700 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
701
702 M.addTypeName("llvm.dbg.composite.type", Init->getType());
703 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
704 GlobalValue::InternalLinkage,
705 Init, "llvm.dbg.composite", &M);
706 GV->setSection("llvm.metadata");
707 return DICompositeType(GV);
708}
709
710
711/// CreateSubprogram - Create a new descriptor for the specified subprogram.
712/// See comments in DISubprogram for descriptions of these fields. This
713/// method does not unique the generated descriptors.
714DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
715 const std::string &Name,
716 const std::string &DisplayName,
717 const std::string &LinkageName,
718 DICompileUnit CompileUnit,
719 unsigned LineNo, DIType Type,
720 bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000721 bool isDefinition) {
Devang Patel854967e2008-12-17 22:39:29 +0000722
Chris Lattnera45664f2008-11-10 02:56:27 +0000723 Constant *Elts[] = {
724 GetTagConstant(dwarf::DW_TAG_subprogram),
Devang Patel13e16b62009-06-26 01:49:18 +0000725 Constant::getNullValue(EmptyStructPtr),
Chris Lattner497a7a82008-11-10 04:10:34 +0000726 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000727 GetStringConstant(Name),
728 GetStringConstant(DisplayName),
729 GetStringConstant(LinkageName),
Chris Lattner497a7a82008-11-10 04:10:34 +0000730 getCastToEmpty(CompileUnit),
Chris Lattnera45664f2008-11-10 02:56:27 +0000731 ConstantInt::get(Type::Int32Ty, LineNo),
Chris Lattner497a7a82008-11-10 04:10:34 +0000732 getCastToEmpty(Type),
Chris Lattnera45664f2008-11-10 02:56:27 +0000733 ConstantInt::get(Type::Int1Ty, isLocalToUnit),
Devang Pateldd9db662009-01-30 18:20:31 +0000734 ConstantInt::get(Type::Int1Ty, isDefinition)
Chris Lattnera45664f2008-11-10 02:56:27 +0000735 };
736
737 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
738
739 M.addTypeName("llvm.dbg.subprogram.type", Init->getType());
740 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
Devang Patel13e16b62009-06-26 01:49:18 +0000741 GlobalValue::LinkOnceAnyLinkage,
Chris Lattnera45664f2008-11-10 02:56:27 +0000742 Init, "llvm.dbg.subprogram", &M);
743 GV->setSection("llvm.metadata");
744 return DISubprogram(GV);
745}
746
747/// CreateGlobalVariable - Create a new descriptor for the specified global.
748DIGlobalVariable
749DIFactory::CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
750 const std::string &DisplayName,
751 const std::string &LinkageName,
752 DICompileUnit CompileUnit,
753 unsigned LineNo, DIType Type,bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000754 bool isDefinition, llvm::GlobalVariable *Val) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000755 Constant *Elts[] = {
756 GetTagConstant(dwarf::DW_TAG_variable),
Devang Patel13e16b62009-06-26 01:49:18 +0000757 Constant::getNullValue(EmptyStructPtr),
Chris Lattner497a7a82008-11-10 04:10:34 +0000758 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000759 GetStringConstant(Name),
760 GetStringConstant(DisplayName),
761 GetStringConstant(LinkageName),
Chris Lattner497a7a82008-11-10 04:10:34 +0000762 getCastToEmpty(CompileUnit),
Chris Lattnera45664f2008-11-10 02:56:27 +0000763 ConstantInt::get(Type::Int32Ty, LineNo),
Chris Lattner497a7a82008-11-10 04:10:34 +0000764 getCastToEmpty(Type),
Chris Lattnera45664f2008-11-10 02:56:27 +0000765 ConstantInt::get(Type::Int1Ty, isLocalToUnit),
766 ConstantInt::get(Type::Int1Ty, isDefinition),
Devang Pateldd9db662009-01-30 18:20:31 +0000767 ConstantExpr::getBitCast(Val, EmptyStructPtr)
Chris Lattnera45664f2008-11-10 02:56:27 +0000768 };
769
770 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
771
772 M.addTypeName("llvm.dbg.global_variable.type", Init->getType());
773 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
Devang Patel13e16b62009-06-26 01:49:18 +0000774 GlobalValue::LinkOnceAnyLinkage,
Chris Lattnera45664f2008-11-10 02:56:27 +0000775 Init, "llvm.dbg.global_variable", &M);
776 GV->setSection("llvm.metadata");
777 return DIGlobalVariable(GV);
778}
779
780
781/// CreateVariable - Create a new descriptor for the specified variable.
782DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
783 const std::string &Name,
784 DICompileUnit CompileUnit, unsigned LineNo,
Devang Pateldd9db662009-01-30 18:20:31 +0000785 DIType Type) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000786 Constant *Elts[] = {
787 GetTagConstant(Tag),
Chris Lattner497a7a82008-11-10 04:10:34 +0000788 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000789 GetStringConstant(Name),
Chris Lattner497a7a82008-11-10 04:10:34 +0000790 getCastToEmpty(CompileUnit),
Chris Lattnera45664f2008-11-10 02:56:27 +0000791 ConstantInt::get(Type::Int32Ty, LineNo),
Devang Pateldd9db662009-01-30 18:20:31 +0000792 getCastToEmpty(Type)
Chris Lattnera45664f2008-11-10 02:56:27 +0000793 };
794
795 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
796
797 M.addTypeName("llvm.dbg.variable.type", Init->getType());
798 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
799 GlobalValue::InternalLinkage,
800 Init, "llvm.dbg.variable", &M);
801 GV->setSection("llvm.metadata");
802 return DIVariable(GV);
803}
804
805
806/// CreateBlock - This creates a descriptor for a lexical block with the
807/// specified parent context.
808DIBlock DIFactory::CreateBlock(DIDescriptor Context) {
809 Constant *Elts[] = {
810 GetTagConstant(dwarf::DW_TAG_lexical_block),
Chris Lattner497a7a82008-11-10 04:10:34 +0000811 getCastToEmpty(Context)
Chris Lattnera45664f2008-11-10 02:56:27 +0000812 };
813
814 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
815
816 M.addTypeName("llvm.dbg.block.type", Init->getType());
817 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
818 GlobalValue::InternalLinkage,
819 Init, "llvm.dbg.block", &M);
820 GV->setSection("llvm.metadata");
821 return DIBlock(GV);
822}
823
824
825//===----------------------------------------------------------------------===//
826// DIFactory: Routines for inserting code into a function
827//===----------------------------------------------------------------------===//
828
829/// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
830/// inserting it at the end of the specified basic block.
831void DIFactory::InsertStopPoint(DICompileUnit CU, unsigned LineNo,
832 unsigned ColNo, BasicBlock *BB) {
833
834 // Lazily construct llvm.dbg.stoppoint function.
835 if (!StopPointFn)
836 StopPointFn = llvm::Intrinsic::getDeclaration(&M,
837 llvm::Intrinsic::dbg_stoppoint);
838
839 // Invoke llvm.dbg.stoppoint
840 Value *Args[] = {
841 llvm::ConstantInt::get(llvm::Type::Int32Ty, LineNo),
842 llvm::ConstantInt::get(llvm::Type::Int32Ty, ColNo),
Chris Lattner497a7a82008-11-10 04:10:34 +0000843 getCastToEmpty(CU)
Chris Lattnera45664f2008-11-10 02:56:27 +0000844 };
845 CallInst::Create(StopPointFn, Args, Args+3, "", BB);
846}
847
848/// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
849/// mark the start of the specified subprogram.
850void DIFactory::InsertSubprogramStart(DISubprogram SP, BasicBlock *BB) {
851 // Lazily construct llvm.dbg.func.start.
852 if (!FuncStartFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000853 FuncStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_func_start);
Chris Lattnera45664f2008-11-10 02:56:27 +0000854
855 // Call llvm.dbg.func.start which also implicitly sets a stoppoint.
Chris Lattner497a7a82008-11-10 04:10:34 +0000856 CallInst::Create(FuncStartFn, getCastToEmpty(SP), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000857}
858
859/// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
860/// mark the start of a region for the specified scoping descriptor.
861void DIFactory::InsertRegionStart(DIDescriptor D, BasicBlock *BB) {
862 // Lazily construct llvm.dbg.region.start function.
863 if (!RegionStartFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000864 RegionStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_start);
865
Chris Lattnera45664f2008-11-10 02:56:27 +0000866 // Call llvm.dbg.func.start.
Chris Lattner497a7a82008-11-10 04:10:34 +0000867 CallInst::Create(RegionStartFn, getCastToEmpty(D), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000868}
869
Chris Lattnera45664f2008-11-10 02:56:27 +0000870/// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
871/// mark the end of a region for the specified scoping descriptor.
872void DIFactory::InsertRegionEnd(DIDescriptor D, BasicBlock *BB) {
873 // Lazily construct llvm.dbg.region.end function.
874 if (!RegionEndFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000875 RegionEndFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_end);
876
877 // Call llvm.dbg.region.end.
Chris Lattner497a7a82008-11-10 04:10:34 +0000878 CallInst::Create(RegionEndFn, getCastToEmpty(D), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000879}
880
881/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Bill Wendlingdc817b62009-05-14 18:26:15 +0000882void DIFactory::InsertDeclare(Value *Storage, DIVariable D, BasicBlock *BB) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000883 // Cast the storage to a {}* for the call to llvm.dbg.declare.
Bill Wendlingdc817b62009-05-14 18:26:15 +0000884 Storage = new BitCastInst(Storage, EmptyStructPtr, "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000885
886 if (!DeclareFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000887 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
888
Chris Lattner497a7a82008-11-10 04:10:34 +0000889 Value *Args[] = { Storage, getCastToEmpty(D) };
Chris Lattnera45664f2008-11-10 02:56:27 +0000890 CallInst::Create(DeclareFn, Args, Args+2, "", BB);
891}
Torok Edwin620f2802008-12-16 09:07:36 +0000892
893namespace llvm {
Bill Wendlingdc817b62009-05-14 18:26:15 +0000894 /// findStopPoint - Find the stoppoint coressponding to this instruction, that
895 /// is the stoppoint that dominates this instruction.
896 const DbgStopPointInst *findStopPoint(const Instruction *Inst) {
Torok Edwin620f2802008-12-16 09:07:36 +0000897 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(Inst))
898 return DSI;
899
900 const BasicBlock *BB = Inst->getParent();
901 BasicBlock::const_iterator I = Inst, B;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000902 while (BB) {
Torok Edwin620f2802008-12-16 09:07:36 +0000903 B = BB->begin();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000904
Torok Edwin620f2802008-12-16 09:07:36 +0000905 // A BB consisting only of a terminator can't have a stoppoint.
Bill Wendlingdc817b62009-05-14 18:26:15 +0000906 while (I != B) {
907 --I;
908 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
909 return DSI;
Torok Edwin620f2802008-12-16 09:07:36 +0000910 }
Bill Wendlingdc817b62009-05-14 18:26:15 +0000911
912 // This BB didn't have a stoppoint: if there is only one predecessor, look
913 // for a stoppoint there. We could use getIDom(), but that would require
914 // dominator info.
Torok Edwin620f2802008-12-16 09:07:36 +0000915 BB = I->getParent()->getUniquePredecessor();
916 if (BB)
917 I = BB->getTerminator();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000918 }
919
Torok Edwin620f2802008-12-16 09:07:36 +0000920 return 0;
921 }
922
Bill Wendlingdc817b62009-05-14 18:26:15 +0000923 /// findBBStopPoint - Find the stoppoint corresponding to first real
924 /// (non-debug intrinsic) instruction in this Basic Block, and return the
925 /// stoppoint for it.
926 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB) {
927 for(BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +0000928 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
929 return DSI;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000930
931 // Fallback to looking for stoppoint of unique predecessor. Useful if this
932 // BB contains no stoppoints, but unique predecessor does.
Torok Edwin620f2802008-12-16 09:07:36 +0000933 BB = BB->getUniquePredecessor();
934 if (BB)
935 return findStopPoint(BB->getTerminator());
Bill Wendlingdc817b62009-05-14 18:26:15 +0000936
Torok Edwin620f2802008-12-16 09:07:36 +0000937 return 0;
938 }
939
Bill Wendlingdc817b62009-05-14 18:26:15 +0000940 Value *findDbgGlobalDeclare(GlobalVariable *V) {
Torok Edwinff7d0e92009-03-10 13:41:26 +0000941 const Module *M = V->getParent();
942 const Type *Ty = M->getTypeByName("llvm.dbg.global_variable.type");
Bill Wendlingdc817b62009-05-14 18:26:15 +0000943 if (!Ty) return 0;
944
Torok Edwinff7d0e92009-03-10 13:41:26 +0000945 Ty = PointerType::get(Ty, 0);
946
947 Value *Val = V->stripPointerCasts();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000948 for (Value::use_iterator I = Val->use_begin(), E = Val->use_end();
Torok Edwinff7d0e92009-03-10 13:41:26 +0000949 I != E; ++I) {
950 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(I)) {
951 if (CE->getOpcode() == Instruction::BitCast) {
952 Value *VV = CE;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000953
954 while (VV->hasOneUse())
Torok Edwinff7d0e92009-03-10 13:41:26 +0000955 VV = *VV->use_begin();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000956
Torok Edwinff7d0e92009-03-10 13:41:26 +0000957 if (VV->getType() == Ty)
958 return VV;
959 }
960 }
961 }
962
963 if (Val->getType() == Ty)
964 return Val;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000965
Torok Edwinff7d0e92009-03-10 13:41:26 +0000966 return 0;
967 }
968
Bill Wendlingdc817b62009-05-14 18:26:15 +0000969 /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
Torok Edwin620f2802008-12-16 09:07:36 +0000970 /// It looks through pointer casts too.
Bill Wendlingdc817b62009-05-14 18:26:15 +0000971 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts) {
Torok Edwin620f2802008-12-16 09:07:36 +0000972 if (stripCasts) {
973 V = V->stripPointerCasts();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000974
Torok Edwin620f2802008-12-16 09:07:36 +0000975 // Look for the bitcast.
976 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000977 I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +0000978 if (isa<BitCastInst>(I))
979 return findDbgDeclare(*I, false);
Bill Wendlingdc817b62009-05-14 18:26:15 +0000980
Torok Edwin620f2802008-12-16 09:07:36 +0000981 return 0;
982 }
983
Bill Wendlingdc817b62009-05-14 18:26:15 +0000984 // Find llvm.dbg.declare among uses of the instruction.
Torok Edwin620f2802008-12-16 09:07:36 +0000985 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000986 I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +0000987 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I))
988 return DDI;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000989
Torok Edwin620f2802008-12-16 09:07:36 +0000990 return 0;
991 }
Torok Edwinff7d0e92009-03-10 13:41:26 +0000992
Bill Wendlingdc817b62009-05-14 18:26:15 +0000993 bool getLocationInfo(const Value *V, std::string &DisplayName,
994 std::string &Type, unsigned &LineNo, std::string &File,
995 std::string &Dir) {
Torok Edwinff7d0e92009-03-10 13:41:26 +0000996 DICompileUnit Unit;
997 DIType TypeD;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000998
Torok Edwinff7d0e92009-03-10 13:41:26 +0000999 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1000 Value *DIGV = findDbgGlobalDeclare(GV);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001001 if (!DIGV) return false;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001002 DIGlobalVariable Var(cast<GlobalVariable>(DIGV));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001003
Bill Wendling0582ae92009-03-13 04:39:26 +00001004 Var.getDisplayName(DisplayName);
Torok Edwinff7d0e92009-03-10 13:41:26 +00001005 LineNo = Var.getLineNumber();
1006 Unit = Var.getCompileUnit();
1007 TypeD = Var.getType();
1008 } else {
1009 const DbgDeclareInst *DDI = findDbgDeclare(V);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001010 if (!DDI) return false;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001011 DIVariable Var(cast<GlobalVariable>(DDI->getVariable()));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001012
Bill Wendling0582ae92009-03-13 04:39:26 +00001013 Var.getName(DisplayName);
Torok Edwinff7d0e92009-03-10 13:41:26 +00001014 LineNo = Var.getLineNumber();
1015 Unit = Var.getCompileUnit();
1016 TypeD = Var.getType();
1017 }
Bill Wendlingdc817b62009-05-14 18:26:15 +00001018
Bill Wendling0582ae92009-03-13 04:39:26 +00001019 TypeD.getName(Type);
1020 Unit.getFilename(File);
1021 Unit.getDirectory(Dir);
Torok Edwinff7d0e92009-03-10 13:41:26 +00001022 return true;
1023 }
Devang Patel13e16b62009-06-26 01:49:18 +00001024
1025 /// CollectDebugInfoAnchors - Collect debugging information anchors.
1026 void CollectDebugInfoAnchors(Module &M,
1027 SmallVector<GlobalVariable *, 2> &CUs,
1028 SmallVector<GlobalVariable *, 4> &GVs,
1029 SmallVector<GlobalVariable *, 4> &SPs) {
1030
1031 for (Module::global_iterator GVI = M.global_begin(), E = M.global_end();
1032 GVI != E; GVI++) {
1033 GlobalVariable *GV = GVI;
1034 if (GV->hasName() && strncmp(GV->getNameStart(), "llvm.dbg", 8) == 0
1035 && GV->isConstant() && GV->hasInitializer()) {
1036 DICompileUnit C(GV);
1037 if (C.isNull() == false) {
1038 CUs.push_back(GV);
1039 continue;
1040 }
1041 DIGlobalVariable G(GV);
1042 if (G.isNull() == false) {
1043 GVs.push_back(GV);
1044 continue;
1045 }
1046 DISubprogram S(GV);
1047 if (S.isNull() == false) {
1048 SPs.push_back(GV);
1049 continue;
1050 }
1051 }
1052 }
1053 }
Devang Patel9e529c32009-07-02 01:15:24 +00001054
1055 /// isValidDebugInfoIntrinsic - Return true if SPI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001056 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001057 bool isValidDebugInfoIntrinsic(DbgStopPointInst &SPI,
1058 CodeGenOpt::Level OptLev) {
1059 return DIDescriptor::ValidDebugInfo(SPI.getContext(), OptLev);
1060 }
1061
1062 /// isValidDebugInfoIntrinsic - Return true if FSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001063 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001064 bool isValidDebugInfoIntrinsic(DbgFuncStartInst &FSI,
1065 CodeGenOpt::Level OptLev) {
1066 return DIDescriptor::ValidDebugInfo(FSI.getSubprogram(), OptLev);
1067 }
1068
1069 /// isValidDebugInfoIntrinsic - Return true if RSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001070 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001071 bool isValidDebugInfoIntrinsic(DbgRegionStartInst &RSI,
1072 CodeGenOpt::Level OptLev) {
1073 return DIDescriptor::ValidDebugInfo(RSI.getContext(), OptLev);
1074 }
1075
1076 /// isValidDebugInfoIntrinsic - Return true if REI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001077 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001078 bool isValidDebugInfoIntrinsic(DbgRegionEndInst &REI,
1079 CodeGenOpt::Level OptLev) {
1080 return DIDescriptor::ValidDebugInfo(REI.getContext(), OptLev);
1081 }
1082
1083
1084 /// isValidDebugInfoIntrinsic - Return true if DI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001085 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001086 bool isValidDebugInfoIntrinsic(DbgDeclareInst &DI,
1087 CodeGenOpt::Level OptLev) {
1088 return DIDescriptor::ValidDebugInfo(DI.getVariable(), OptLev);
1089 }
1090
1091 /// ExtractDebugLocation - Extract debug location information
1092 /// from llvm.dbg.stoppoint intrinsic.
1093 DebugLoc ExtractDebugLocation(DbgStopPointInst &SPI,
1094 CodeGenOpt::Level OptLev,
1095 DebugLocTracker &DebugLocInfo) {
1096 DebugLoc DL;
1097 Value *Context = SPI.getContext();
1098 if (DIDescriptor::ValidDebugInfo(Context, OptLev) == false)
1099 return DL;
1100
1101 // If this location is already tracked then use it.
1102 DebugLocTuple Tuple(cast<GlobalVariable>(Context), SPI.getLine(),
1103 SPI.getColumn());
1104 DenseMap<DebugLocTuple, unsigned>::iterator II
1105 = DebugLocInfo.DebugIdMap.find(Tuple);
1106 if (II != DebugLocInfo.DebugIdMap.end())
1107 return DebugLoc::get(II->second);
1108
1109 // Add a new location entry.
1110 unsigned Id = DebugLocInfo.DebugLocations.size();
1111 DebugLocInfo.DebugLocations.push_back(Tuple);
1112 DebugLocInfo.DebugIdMap[Tuple] = Id;
1113
1114 return DebugLoc::get(Id);
1115 }
1116
1117 /// ExtractDebugLocation - Extract debug location information
1118 /// from llvm.dbg.func_start intrinsic.
1119 DebugLoc ExtractDebugLocation(DbgFuncStartInst &FSI,
1120 CodeGenOpt::Level OptLev,
1121 DebugLocTracker &DebugLocInfo) {
1122 DebugLoc DL;
1123 Value *SP = FSI.getSubprogram();
1124 if (DIDescriptor::ValidDebugInfo(SP, OptLev) == false)
1125 return DL;
1126
1127 DISubprogram Subprogram(cast<GlobalVariable>(SP));
1128 unsigned Line = Subprogram.getLineNumber();
1129 DICompileUnit CU(Subprogram.getCompileUnit());
1130
1131 // If this location is already tracked then use it.
1132 DebugLocTuple Tuple(CU.getGV(), Line, /* Column */ 0);
1133 DenseMap<DebugLocTuple, unsigned>::iterator II
1134 = DebugLocInfo.DebugIdMap.find(Tuple);
1135 if (II != DebugLocInfo.DebugIdMap.end())
1136 return DebugLoc::get(II->second);
1137
1138 // Add a new location entry.
1139 unsigned Id = DebugLocInfo.DebugLocations.size();
1140 DebugLocInfo.DebugLocations.push_back(Tuple);
1141 DebugLocInfo.DebugIdMap[Tuple] = Id;
1142
1143 return DebugLoc::get(Id);
1144 }
1145
1146 /// isInlinedFnStart - Return true if FSI is starting an inlined function.
1147 bool isInlinedFnStart(DbgFuncStartInst &FSI, const Function *CurrentFn) {
1148 DISubprogram Subprogram(cast<GlobalVariable>(FSI.getSubprogram()));
1149 if (Subprogram.describes(CurrentFn))
1150 return false;
1151
1152 return true;
1153 }
1154
1155 /// isInlinedFnEnd - Return true if REI is ending an inlined function.
1156 bool isInlinedFnEnd(DbgRegionEndInst &REI, const Function *CurrentFn) {
1157 DISubprogram Subprogram(cast<GlobalVariable>(REI.getContext()));
1158 if (Subprogram.isNull() || Subprogram.describes(CurrentFn))
1159 return false;
1160
1161 return true;
1162 }
1163
Torok Edwin620f2802008-12-16 09:07:36 +00001164}