blob: eac682034d9c4afac52d83cf4fe88509530df601 [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"
Owen Anderson76f600b2009-07-06 22:37:39 +000021#include "llvm/LLVMContext.h"
Chris Lattnera45664f2008-11-10 02:56:27 +000022#include "llvm/Module.h"
23#include "llvm/Analysis/ValueTracking.h"
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000024#include "llvm/Support/Dwarf.h"
Devang Patel9e529c32009-07-02 01:15:24 +000025#include "llvm/Support/DebugLoc.h"
Devang Patelbf3f5a02009-01-30 01:03:10 +000026#include "llvm/Support/Streams.h"
Bill Wendlingdc817b62009-05-14 18:26:15 +000027
Chris Lattnera45664f2008-11-10 02:56:27 +000028using namespace llvm;
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000029using namespace llvm::dwarf;
Chris Lattnera45664f2008-11-10 02:56:27 +000030
31//===----------------------------------------------------------------------===//
32// DIDescriptor
33//===----------------------------------------------------------------------===//
34
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000035/// ValidDebugInfo - Return true if V represents valid debug info value.
36bool DIDescriptor::ValidDebugInfo(Value *V, CodeGenOpt::Level OptLevel) {
37 if (!V)
38 return false;
39
40 GlobalVariable *GV = dyn_cast<GlobalVariable>(V->stripPointerCasts());
41 if (!GV)
42 return false;
43
44 if (!GV->hasInternalLinkage () && !GV->hasLinkOnceLinkage())
45 return false;
46
47 DIDescriptor DI(GV);
48
49 // Check current version. Allow Version6 for now.
50 unsigned Version = DI.getVersion();
51 if (Version != LLVMDebugVersion && Version != LLVMDebugVersion6)
52 return false;
53
54 unsigned Tag = DI.getTag();
55 switch (Tag) {
56 case DW_TAG_variable:
57 assert(DIVariable(GV).Verify() && "Invalid DebugInfo value");
58 break;
59 case DW_TAG_compile_unit:
60 assert(DICompileUnit(GV).Verify() && "Invalid DebugInfo value");
61 break;
62 case DW_TAG_subprogram:
63 assert(DISubprogram(GV).Verify() && "Invalid DebugInfo value");
64 break;
65 case DW_TAG_lexical_block:
Bill Wendlingdc817b62009-05-14 18:26:15 +000066 // FIXME: This interfers with the quality of generated code during
67 // optimization.
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000068 if (OptLevel != CodeGenOpt::None)
69 return false;
Bill Wendlingdc817b62009-05-14 18:26:15 +000070 // FALLTHROUGH
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000071 default:
72 break;
73 }
74
75 return true;
76}
77
Devang Patel9af2fa82009-06-23 22:25:41 +000078DIDescriptor::DIDescriptor(GlobalVariable *GV, unsigned RequiredTag) {
79 DbgGV = GV;
Chris Lattnera45664f2008-11-10 02:56:27 +000080
Bill Wendlingdc817b62009-05-14 18:26:15 +000081 // If this is non-null, check to see if the Tag matches. If not, set to null.
Chris Lattnera45664f2008-11-10 02:56:27 +000082 if (GV && getTag() != RequiredTag)
Devang Patel9af2fa82009-06-23 22:25:41 +000083 DbgGV = 0;
Chris Lattnera45664f2008-11-10 02:56:27 +000084}
85
Bill Wendling0582ae92009-03-13 04:39:26 +000086const std::string &
87DIDescriptor::getStringField(unsigned Elt, std::string &Result) const {
Devang Patel9af2fa82009-06-23 22:25:41 +000088 if (DbgGV == 0) {
Bill Wendling0582ae92009-03-13 04:39:26 +000089 Result.clear();
90 return Result;
91 }
Chris Lattnera45664f2008-11-10 02:56:27 +000092
Devang Patel9af2fa82009-06-23 22:25:41 +000093 Constant *C = DbgGV->getInitializer();
Bill Wendling0582ae92009-03-13 04:39:26 +000094 if (C == 0 || Elt >= C->getNumOperands()) {
95 Result.clear();
96 return Result;
97 }
Bill Wendlingdc817b62009-05-14 18:26:15 +000098
Chris Lattnera45664f2008-11-10 02:56:27 +000099 // Fills in the string if it succeeds
Bill Wendling0582ae92009-03-13 04:39:26 +0000100 if (!GetConstantStringInfo(C->getOperand(Elt), Result))
101 Result.clear();
102
103 return Result;
Chris Lattnera45664f2008-11-10 02:56:27 +0000104}
105
106uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
Devang Patel9af2fa82009-06-23 22:25:41 +0000107 if (DbgGV == 0) return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000108
Devang Patel9af2fa82009-06-23 22:25:41 +0000109 Constant *C = DbgGV->getInitializer();
Chris Lattnera45664f2008-11-10 02:56:27 +0000110 if (C == 0 || Elt >= C->getNumOperands())
111 return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000112
Chris Lattnera45664f2008-11-10 02:56:27 +0000113 if (ConstantInt *CI = dyn_cast<ConstantInt>(C->getOperand(Elt)))
114 return CI->getZExtValue();
115 return 0;
116}
117
Chris Lattnera45664f2008-11-10 02:56:27 +0000118DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
Devang Patel9af2fa82009-06-23 22:25:41 +0000119 if (DbgGV == 0) return DIDescriptor();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000120
Devang Patel9af2fa82009-06-23 22:25:41 +0000121 Constant *C = DbgGV->getInitializer();
Chris Lattnera45664f2008-11-10 02:56:27 +0000122 if (C == 0 || Elt >= C->getNumOperands())
123 return DIDescriptor();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000124
Chris Lattnera45664f2008-11-10 02:56:27 +0000125 C = C->getOperand(Elt);
126 return DIDescriptor(dyn_cast<GlobalVariable>(C->stripPointerCasts()));
127}
128
129GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
Devang Patel9af2fa82009-06-23 22:25:41 +0000130 if (DbgGV == 0) return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000131
Devang Patel9af2fa82009-06-23 22:25:41 +0000132 Constant *C = DbgGV->getInitializer();
Chris Lattnera45664f2008-11-10 02:56:27 +0000133 if (C == 0 || Elt >= C->getNumOperands())
134 return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000135
Chris Lattnera45664f2008-11-10 02:56:27 +0000136 C = C->getOperand(Elt);
Chris Lattnera45664f2008-11-10 02:56:27 +0000137 return dyn_cast<GlobalVariable>(C->stripPointerCasts());
138}
139
Chris Lattnera45664f2008-11-10 02:56:27 +0000140//===----------------------------------------------------------------------===//
141// Simple Descriptor Constructors and other Methods
142//===----------------------------------------------------------------------===//
143
Bill Wendlingdc817b62009-05-14 18:26:15 +0000144// Needed by DIVariable::getType().
Devang Patel9af2fa82009-06-23 22:25:41 +0000145DIType::DIType(GlobalVariable *GV) : DIDescriptor(GV) {
146 if (!GV) return;
Torok Edwinb07fbd92008-12-13 08:25:29 +0000147 unsigned tag = getTag();
148 if (tag != dwarf::DW_TAG_base_type && !DIDerivedType::isDerivedType(tag) &&
149 !DICompositeType::isCompositeType(tag))
Devang Patel9af2fa82009-06-23 22:25:41 +0000150 DbgGV = 0;
Torok Edwinb07fbd92008-12-13 08:25:29 +0000151}
Chris Lattnera45664f2008-11-10 02:56:27 +0000152
153/// isDerivedType - Return true if the specified tag is legal for
154/// DIDerivedType.
Devang Patel486938f2009-01-12 21:38:43 +0000155bool DIType::isDerivedType(unsigned Tag) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000156 switch (Tag) {
157 case dwarf::DW_TAG_typedef:
158 case dwarf::DW_TAG_pointer_type:
159 case dwarf::DW_TAG_reference_type:
160 case dwarf::DW_TAG_const_type:
161 case dwarf::DW_TAG_volatile_type:
162 case dwarf::DW_TAG_restrict_type:
163 case dwarf::DW_TAG_member:
164 case dwarf::DW_TAG_inheritance:
165 return true;
166 default:
167 // FIXME: Even though it doesn't make sense, CompositeTypes are current
168 // modelled as DerivedTypes, this should return true for them as well.
169 return false;
170 }
171}
172
Chris Lattnera45664f2008-11-10 02:56:27 +0000173/// isCompositeType - Return true if the specified tag is legal for
174/// DICompositeType.
Devang Patel486938f2009-01-12 21:38:43 +0000175bool DIType::isCompositeType(unsigned TAG) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000176 switch (TAG) {
177 case dwarf::DW_TAG_array_type:
178 case dwarf::DW_TAG_structure_type:
179 case dwarf::DW_TAG_union_type:
180 case dwarf::DW_TAG_enumeration_type:
181 case dwarf::DW_TAG_vector_type:
182 case dwarf::DW_TAG_subroutine_type:
Devang Patel25cb0d72009-03-25 03:52:06 +0000183 case dwarf::DW_TAG_class_type:
Chris Lattnera45664f2008-11-10 02:56:27 +0000184 return true;
185 default:
186 return false;
187 }
188}
189
Chris Lattnera45664f2008-11-10 02:56:27 +0000190/// isVariable - Return true if the specified tag is legal for DIVariable.
191bool DIVariable::isVariable(unsigned Tag) {
192 switch (Tag) {
193 case dwarf::DW_TAG_auto_variable:
194 case dwarf::DW_TAG_arg_variable:
195 case dwarf::DW_TAG_return_variable:
196 return true;
197 default:
198 return false;
199 }
200}
201
Devang Patel68afdc32009-01-05 18:33:01 +0000202unsigned DIArray::getNumElements() const {
Devang Patel9af2fa82009-06-23 22:25:41 +0000203 assert (DbgGV && "Invalid DIArray");
204 Constant *C = DbgGV->getInitializer();
Devang Patel68afdc32009-01-05 18:33:01 +0000205 assert (C && "Invalid DIArray initializer");
206 return C->getNumOperands();
207}
Chris Lattnera45664f2008-11-10 02:56:27 +0000208
Devang Patelb79b5352009-01-19 23:21:49 +0000209/// Verify - Verify that a compile unit is well formed.
210bool DICompileUnit::Verify() const {
211 if (isNull())
212 return false;
Bill Wendling0582ae92009-03-13 04:39:26 +0000213 std::string Res;
214 if (getFilename(Res).empty())
215 return false;
Devang Patelb79b5352009-01-19 23:21:49 +0000216 // It is possible that directory and produce string is empty.
Bill Wendling0582ae92009-03-13 04:39:26 +0000217 return true;
Devang Patelb79b5352009-01-19 23:21:49 +0000218}
219
220/// Verify - Verify that a type descriptor is well formed.
221bool DIType::Verify() const {
222 if (isNull())
223 return false;
224 if (getContext().isNull())
225 return false;
226
227 DICompileUnit CU = getCompileUnit();
228 if (!CU.isNull() && !CU.Verify())
229 return false;
230 return true;
231}
232
233/// Verify - Verify that a composite type descriptor is well formed.
234bool DICompositeType::Verify() const {
235 if (isNull())
236 return false;
237 if (getContext().isNull())
238 return false;
239
240 DICompileUnit CU = getCompileUnit();
241 if (!CU.isNull() && !CU.Verify())
242 return false;
243 return true;
244}
245
246/// Verify - Verify that a subprogram descriptor is well formed.
247bool DISubprogram::Verify() const {
248 if (isNull())
249 return false;
250
251 if (getContext().isNull())
252 return false;
253
254 DICompileUnit CU = getCompileUnit();
255 if (!CU.Verify())
256 return false;
257
258 DICompositeType Ty = getType();
259 if (!Ty.isNull() && !Ty.Verify())
260 return false;
261 return true;
262}
263
264/// Verify - Verify that a global variable descriptor is well formed.
265bool DIGlobalVariable::Verify() const {
266 if (isNull())
267 return false;
268
269 if (getContext().isNull())
270 return false;
271
272 DICompileUnit CU = getCompileUnit();
Chris Lattnere3f6cea2009-05-05 04:55:56 +0000273 if (!CU.isNull() && !CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000274 return false;
275
276 DIType Ty = getType();
277 if (!Ty.Verify())
278 return false;
279
280 if (!getGlobal())
281 return false;
282
283 return true;
284}
285
286/// Verify - Verify that a variable descriptor is well formed.
287bool DIVariable::Verify() const {
288 if (isNull())
289 return false;
290
291 if (getContext().isNull())
292 return false;
293
294 DIType Ty = getType();
295 if (!Ty.Verify())
296 return false;
297
Devang Patelb79b5352009-01-19 23:21:49 +0000298 return true;
299}
300
Devang Patel36375ee2009-02-17 21:23:59 +0000301/// getOriginalTypeSize - If this type is derived from a base type then
302/// return base type size.
303uint64_t DIDerivedType::getOriginalTypeSize() const {
304 if (getTag() != dwarf::DW_TAG_member)
305 return getSizeInBits();
306 DIType BT = getTypeDerivedFrom();
307 if (BT.getTag() != dwarf::DW_TAG_base_type)
308 return getSizeInBits();
309 return BT.getSizeInBits();
310}
Devang Patelb79b5352009-01-19 23:21:49 +0000311
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000312/// describes - Return true if this subprogram provides debugging
313/// information for the function F.
314bool DISubprogram::describes(const Function *F) {
315 assert (F && "Invalid function");
316 std::string Name;
317 getLinkageName(Name);
318 if (Name.empty())
319 getName(Name);
320 if (!Name.empty() && (strcmp(Name.c_str(), F->getNameStart()) == false))
321 return true;
322 return false;
323}
324
Chris Lattnera45664f2008-11-10 02:56:27 +0000325//===----------------------------------------------------------------------===//
Devang Patel7136a652009-07-01 22:10:23 +0000326// DIDescriptor: dump routines for all descriptors.
327//===----------------------------------------------------------------------===//
328
329
330/// dump - Print descriptor.
331void DIDescriptor::dump() const {
332 cerr << "[" << dwarf::TagString(getTag()) << "] ";
333 cerr << std::hex << "[GV:" << DbgGV << "]" << std::dec;
334}
335
336/// dump - Print compile unit.
337void DICompileUnit::dump() const {
338 if (getLanguage())
339 cerr << " [" << dwarf::LanguageString(getLanguage()) << "] ";
340
341 std::string Res1, Res2;
342 cerr << " [" << getDirectory(Res1) << "/" << getFilename(Res2) << " ]";
343}
344
345/// dump - Print type.
346void DIType::dump() const {
347 if (isNull()) return;
348
349 std::string Res;
350 if (!getName(Res).empty())
351 cerr << " [" << Res << "] ";
352
353 unsigned Tag = getTag();
354 cerr << " [" << dwarf::TagString(Tag) << "] ";
355
356 // TODO : Print context
357 getCompileUnit().dump();
358 cerr << " ["
359 << getLineNumber() << ", "
360 << getSizeInBits() << ", "
361 << getAlignInBits() << ", "
362 << getOffsetInBits()
363 << "] ";
364
365 if (isPrivate())
366 cerr << " [private] ";
367 else if (isProtected())
368 cerr << " [protected] ";
369
370 if (isForwardDecl())
371 cerr << " [fwd] ";
372
373 if (isBasicType(Tag))
374 DIBasicType(DbgGV).dump();
375 else if (isDerivedType(Tag))
376 DIDerivedType(DbgGV).dump();
377 else if (isCompositeType(Tag))
378 DICompositeType(DbgGV).dump();
379 else {
380 cerr << "Invalid DIType\n";
381 return;
382 }
383
384 cerr << "\n";
385}
386
387/// dump - Print basic type.
388void DIBasicType::dump() const {
389 cerr << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
390}
391
392/// dump - Print derived type.
393void DIDerivedType::dump() const {
394 cerr << "\n\t Derived From: "; getTypeDerivedFrom().dump();
395}
396
397/// dump - Print composite type.
398void DICompositeType::dump() const {
399 DIArray A = getTypeArray();
400 if (A.isNull())
401 return;
402 cerr << " [" << A.getNumElements() << " elements]";
403}
404
405/// dump - Print global.
406void DIGlobal::dump() const {
407 std::string Res;
408 if (!getName(Res).empty())
409 cerr << " [" << Res << "] ";
410
411 unsigned Tag = getTag();
412 cerr << " [" << dwarf::TagString(Tag) << "] ";
413
414 // TODO : Print context
415 getCompileUnit().dump();
416 cerr << " [" << getLineNumber() << "] ";
417
418 if (isLocalToUnit())
419 cerr << " [local] ";
420
421 if (isDefinition())
422 cerr << " [def] ";
423
424 if (isGlobalVariable(Tag))
425 DIGlobalVariable(DbgGV).dump();
426
427 cerr << "\n";
428}
429
430/// dump - Print subprogram.
431void DISubprogram::dump() const {
432 DIGlobal::dump();
433}
434
435/// dump - Print global variable.
436void DIGlobalVariable::dump() const {
437 cerr << " ["; getGlobal()->dump(); cerr << "] ";
438}
439
440/// dump - Print variable.
441void DIVariable::dump() const {
442 std::string Res;
443 if (!getName(Res).empty())
444 cerr << " [" << Res << "] ";
445
446 getCompileUnit().dump();
447 cerr << " [" << getLineNumber() << "] ";
448 getType().dump();
449 cerr << "\n";
450}
451
452//===----------------------------------------------------------------------===//
Chris Lattnera45664f2008-11-10 02:56:27 +0000453// DIFactory: Basic Helpers
454//===----------------------------------------------------------------------===//
455
Bill Wendlingdc817b62009-05-14 18:26:15 +0000456DIFactory::DIFactory(Module &m)
457 : M(m), StopPointFn(0), FuncStartFn(0), RegionStartFn(0), RegionEndFn(0),
458 DeclareFn(0) {
Owen Anderson76f600b2009-07-06 22:37:39 +0000459 EmptyStructPtr =
460 M.getContext().getPointerTypeUnqual(M.getContext().getStructType());
Chris Lattner497a7a82008-11-10 04:10:34 +0000461}
462
463/// getCastToEmpty - Return this descriptor as a Constant* with type '{}*'.
464/// This is only valid when the descriptor is non-null.
465Constant *DIFactory::getCastToEmpty(DIDescriptor D) {
Owen Anderson76f600b2009-07-06 22:37:39 +0000466 if (D.isNull()) return M.getContext().getNullValue(EmptyStructPtr);
467 return M.getContext().getConstantExprBitCast(D.getGV(), EmptyStructPtr);
Chris Lattner497a7a82008-11-10 04:10:34 +0000468}
469
Chris Lattnera45664f2008-11-10 02:56:27 +0000470Constant *DIFactory::GetTagConstant(unsigned TAG) {
Devang Patel6906ba52009-01-20 19:22:03 +0000471 assert((TAG & LLVMDebugVersionMask) == 0 &&
Chris Lattnera45664f2008-11-10 02:56:27 +0000472 "Tag too large for debug encoding!");
Devang Patel6906ba52009-01-20 19:22:03 +0000473 return ConstantInt::get(Type::Int32Ty, TAG | LLVMDebugVersion);
Chris Lattnera45664f2008-11-10 02:56:27 +0000474}
475
476Constant *DIFactory::GetStringConstant(const std::string &String) {
477 // Check string cache for previous edition.
478 Constant *&Slot = StringCache[String];
479
480 // Return Constant if previously defined.
481 if (Slot) return Slot;
482
Owen Anderson76f600b2009-07-06 22:37:39 +0000483 const PointerType *DestTy = M.getContext().getPointerTypeUnqual(Type::Int8Ty);
Chris Lattnera45664f2008-11-10 02:56:27 +0000484
Dan Gohmana119de82009-06-14 23:30:43 +0000485 // If empty string then use a i8* null instead.
Chris Lattnera45664f2008-11-10 02:56:27 +0000486 if (String.empty())
Owen Anderson76f600b2009-07-06 22:37:39 +0000487 return Slot = M.getContext().getConstantPointerNull(DestTy);
Chris Lattnera45664f2008-11-10 02:56:27 +0000488
489 // Construct string as an llvm constant.
Owen Anderson76f600b2009-07-06 22:37:39 +0000490 Constant *ConstStr = M.getContext().getConstantArray(String);
Chris Lattnera45664f2008-11-10 02:56:27 +0000491
492 // Otherwise create and return a new string global.
493 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
494 GlobalVariable::InternalLinkage,
495 ConstStr, ".str", &M);
496 StrGV->setSection("llvm.metadata");
Owen Anderson76f600b2009-07-06 22:37:39 +0000497 return Slot = M.getContext().getConstantExprBitCast(StrGV, DestTy);
Chris Lattnera45664f2008-11-10 02:56:27 +0000498}
499
Chris Lattnera45664f2008-11-10 02:56:27 +0000500//===----------------------------------------------------------------------===//
501// DIFactory: Primary Constructors
502//===----------------------------------------------------------------------===//
503
Chris Lattnera45664f2008-11-10 02:56:27 +0000504/// GetOrCreateArray - Create an descriptor for an array of descriptors.
505/// This implicitly uniques the arrays created.
506DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
507 SmallVector<Constant*, 16> Elts;
508
Owen Anderson76f600b2009-07-06 22:37:39 +0000509 LLVMContext& Ctxt = M.getContext();
510
Chris Lattnera45664f2008-11-10 02:56:27 +0000511 for (unsigned i = 0; i != NumTys; ++i)
Chris Lattner497a7a82008-11-10 04:10:34 +0000512 Elts.push_back(getCastToEmpty(Tys[i]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000513
Owen Anderson76f600b2009-07-06 22:37:39 +0000514 Constant *Init = Ctxt.getConstantArray(Ctxt.getArrayType(EmptyStructPtr,
Chris Lattner497a7a82008-11-10 04:10:34 +0000515 Elts.size()),
Jay Foade3e51c02009-05-21 09:52:38 +0000516 Elts.data(), Elts.size());
Chris Lattnera45664f2008-11-10 02:56:27 +0000517 // If we already have this array, just return the uniqued version.
518 DIDescriptor &Entry = SimpleConstantCache[Init];
519 if (!Entry.isNull()) return DIArray(Entry.getGV());
520
521 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
522 GlobalValue::InternalLinkage,
523 Init, "llvm.dbg.array", &M);
524 GV->setSection("llvm.metadata");
525 Entry = DIDescriptor(GV);
526 return DIArray(GV);
527}
528
529/// GetOrCreateSubrange - Create a descriptor for a value range. This
530/// implicitly uniques the values returned.
531DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
532 Constant *Elts[] = {
533 GetTagConstant(dwarf::DW_TAG_subrange_type),
Owen Anderson76f600b2009-07-06 22:37:39 +0000534 M.getContext().getConstantInt(Type::Int64Ty, Lo),
535 M.getContext().getConstantInt(Type::Int64Ty, Hi)
Chris Lattnera45664f2008-11-10 02:56:27 +0000536 };
537
Owen Anderson76f600b2009-07-06 22:37:39 +0000538 Constant *Init =
539 M.getContext().getConstantStruct(Elts, sizeof(Elts)/sizeof(Elts[0]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000540
541 // If we already have this range, just return the uniqued version.
542 DIDescriptor &Entry = SimpleConstantCache[Init];
543 if (!Entry.isNull()) return DISubrange(Entry.getGV());
544
545 M.addTypeName("llvm.dbg.subrange.type", Init->getType());
546
547 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
548 GlobalValue::InternalLinkage,
549 Init, "llvm.dbg.subrange", &M);
550 GV->setSection("llvm.metadata");
551 Entry = DIDescriptor(GV);
552 return DISubrange(GV);
553}
554
555
556
557/// CreateCompileUnit - Create a new descriptor for the specified compile
558/// unit. Note that this does not unique compile units within the module.
559DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
560 const std::string &Filename,
561 const std::string &Directory,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000562 const std::string &Producer,
Devang Pateldd9db662009-01-30 18:20:31 +0000563 bool isMain,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000564 bool isOptimized,
Devang Patel13319ce2009-02-17 22:43:44 +0000565 const char *Flags,
566 unsigned RunTimeVer) {
Owen Anderson76f600b2009-07-06 22:37:39 +0000567 LLVMContext& Ctxt = M.getContext();
Chris Lattnera45664f2008-11-10 02:56:27 +0000568 Constant *Elts[] = {
569 GetTagConstant(dwarf::DW_TAG_compile_unit),
Owen Anderson76f600b2009-07-06 22:37:39 +0000570 Ctxt.getNullValue(EmptyStructPtr),
571 Ctxt.getConstantInt(Type::Int32Ty, LangID),
Chris Lattnera45664f2008-11-10 02:56:27 +0000572 GetStringConstant(Filename),
573 GetStringConstant(Directory),
Devang Patel3b64c6b2009-01-23 22:33:47 +0000574 GetStringConstant(Producer),
Owen Anderson76f600b2009-07-06 22:37:39 +0000575 Ctxt.getConstantInt(Type::Int1Ty, isMain),
576 Ctxt.getConstantInt(Type::Int1Ty, isOptimized),
Devang Patel13319ce2009-02-17 22:43:44 +0000577 GetStringConstant(Flags),
Owen Anderson76f600b2009-07-06 22:37:39 +0000578 Ctxt.getConstantInt(Type::Int32Ty, RunTimeVer)
Chris Lattnera45664f2008-11-10 02:56:27 +0000579 };
580
Owen Anderson76f600b2009-07-06 22:37:39 +0000581 Constant *Init =
582 Ctxt.getConstantStruct(Elts, sizeof(Elts)/sizeof(Elts[0]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000583
584 M.addTypeName("llvm.dbg.compile_unit.type", Init->getType());
585 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
Devang Patel13e16b62009-06-26 01:49:18 +0000586 GlobalValue::LinkOnceAnyLinkage,
Chris Lattnera45664f2008-11-10 02:56:27 +0000587 Init, "llvm.dbg.compile_unit", &M);
588 GV->setSection("llvm.metadata");
589 return DICompileUnit(GV);
590}
591
592/// CreateEnumerator - Create a single enumerator value.
593DIEnumerator DIFactory::CreateEnumerator(const std::string &Name, uint64_t Val){
594 Constant *Elts[] = {
595 GetTagConstant(dwarf::DW_TAG_enumerator),
596 GetStringConstant(Name),
Owen Anderson76f600b2009-07-06 22:37:39 +0000597 M.getContext().getConstantInt(Type::Int64Ty, Val)
Chris Lattnera45664f2008-11-10 02:56:27 +0000598 };
599
Owen Anderson76f600b2009-07-06 22:37:39 +0000600 Constant *Init =
601 M.getContext().getConstantStruct(Elts, sizeof(Elts)/sizeof(Elts[0]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000602
603 M.addTypeName("llvm.dbg.enumerator.type", Init->getType());
604 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
605 GlobalValue::InternalLinkage,
606 Init, "llvm.dbg.enumerator", &M);
607 GV->setSection("llvm.metadata");
608 return DIEnumerator(GV);
609}
610
611
612/// CreateBasicType - Create a basic type like int, float, etc.
613DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
Bill Wendling0582ae92009-03-13 04:39:26 +0000614 const std::string &Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000615 DICompileUnit CompileUnit,
616 unsigned LineNumber,
617 uint64_t SizeInBits,
618 uint64_t AlignInBits,
619 uint64_t OffsetInBits, unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000620 unsigned Encoding) {
Owen Anderson76f600b2009-07-06 22:37:39 +0000621
622 LLVMContext& Ctxt = M.getContext();
Chris Lattnera45664f2008-11-10 02:56:27 +0000623 Constant *Elts[] = {
624 GetTagConstant(dwarf::DW_TAG_base_type),
Chris Lattner497a7a82008-11-10 04:10:34 +0000625 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000626 GetStringConstant(Name),
Chris Lattner497a7a82008-11-10 04:10:34 +0000627 getCastToEmpty(CompileUnit),
Owen Anderson76f600b2009-07-06 22:37:39 +0000628 Ctxt.getConstantInt(Type::Int32Ty, LineNumber),
629 Ctxt.getConstantInt(Type::Int64Ty, SizeInBits),
630 Ctxt.getConstantInt(Type::Int64Ty, AlignInBits),
631 Ctxt.getConstantInt(Type::Int64Ty, OffsetInBits),
632 Ctxt.getConstantInt(Type::Int32Ty, Flags),
633 Ctxt.getConstantInt(Type::Int32Ty, Encoding)
Chris Lattnera45664f2008-11-10 02:56:27 +0000634 };
635
Owen Anderson76f600b2009-07-06 22:37:39 +0000636 Constant *Init =
637 Ctxt.getConstantStruct(Elts, sizeof(Elts)/sizeof(Elts[0]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000638
639 M.addTypeName("llvm.dbg.basictype.type", Init->getType());
640 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
641 GlobalValue::InternalLinkage,
642 Init, "llvm.dbg.basictype", &M);
643 GV->setSection("llvm.metadata");
644 return DIBasicType(GV);
645}
646
647/// CreateDerivedType - Create a derived type like const qualified type,
648/// pointer, typedef, etc.
649DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
650 DIDescriptor Context,
651 const std::string &Name,
652 DICompileUnit CompileUnit,
653 unsigned LineNumber,
654 uint64_t SizeInBits,
655 uint64_t AlignInBits,
656 uint64_t OffsetInBits,
657 unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000658 DIType DerivedFrom) {
Owen Anderson76f600b2009-07-06 22:37:39 +0000659
660 LLVMContext& Ctxt = M.getContext();
Chris Lattnera45664f2008-11-10 02:56:27 +0000661 Constant *Elts[] = {
662 GetTagConstant(Tag),
Chris Lattner497a7a82008-11-10 04:10:34 +0000663 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000664 GetStringConstant(Name),
Chris Lattner497a7a82008-11-10 04:10:34 +0000665 getCastToEmpty(CompileUnit),
Owen Anderson76f600b2009-07-06 22:37:39 +0000666 Ctxt.getConstantInt(Type::Int32Ty, LineNumber),
667 Ctxt.getConstantInt(Type::Int64Ty, SizeInBits),
668 Ctxt.getConstantInt(Type::Int64Ty, AlignInBits),
669 Ctxt.getConstantInt(Type::Int64Ty, OffsetInBits),
670 Ctxt.getConstantInt(Type::Int32Ty, Flags),
Devang Pateldd9db662009-01-30 18:20:31 +0000671 getCastToEmpty(DerivedFrom)
Chris Lattnera45664f2008-11-10 02:56:27 +0000672 };
673
Owen Anderson76f600b2009-07-06 22:37:39 +0000674 Constant *Init =
675 Ctxt.getConstantStruct(Elts, sizeof(Elts)/sizeof(Elts[0]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000676
677 M.addTypeName("llvm.dbg.derivedtype.type", Init->getType());
678 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
679 GlobalValue::InternalLinkage,
680 Init, "llvm.dbg.derivedtype", &M);
681 GV->setSection("llvm.metadata");
682 return DIDerivedType(GV);
683}
684
685/// CreateCompositeType - Create a composite type like array, struct, etc.
686DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
687 DIDescriptor Context,
688 const std::string &Name,
689 DICompileUnit CompileUnit,
690 unsigned LineNumber,
691 uint64_t SizeInBits,
692 uint64_t AlignInBits,
693 uint64_t OffsetInBits,
694 unsigned Flags,
695 DIType DerivedFrom,
Devang Patel13319ce2009-02-17 22:43:44 +0000696 DIArray Elements,
697 unsigned RuntimeLang) {
Owen Anderson76f600b2009-07-06 22:37:39 +0000698 LLVMContext& Ctxt = M.getContext();
Chris Lattnera45664f2008-11-10 02:56:27 +0000699 Constant *Elts[] = {
700 GetTagConstant(Tag),
Chris Lattner497a7a82008-11-10 04:10:34 +0000701 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000702 GetStringConstant(Name),
Chris Lattner497a7a82008-11-10 04:10:34 +0000703 getCastToEmpty(CompileUnit),
Owen Anderson76f600b2009-07-06 22:37:39 +0000704 Ctxt.getConstantInt(Type::Int32Ty, LineNumber),
705 Ctxt.getConstantInt(Type::Int64Ty, SizeInBits),
706 Ctxt.getConstantInt(Type::Int64Ty, AlignInBits),
707 Ctxt.getConstantInt(Type::Int64Ty, OffsetInBits),
708 Ctxt.getConstantInt(Type::Int32Ty, Flags),
Chris Lattner497a7a82008-11-10 04:10:34 +0000709 getCastToEmpty(DerivedFrom),
Devang Patel13319ce2009-02-17 22:43:44 +0000710 getCastToEmpty(Elements),
Owen Anderson76f600b2009-07-06 22:37:39 +0000711 Ctxt.getConstantInt(Type::Int32Ty, RuntimeLang)
Chris Lattnera45664f2008-11-10 02:56:27 +0000712 };
713
Owen Anderson76f600b2009-07-06 22:37:39 +0000714 Constant *Init =
715 Ctxt.getConstantStruct(Elts, sizeof(Elts)/sizeof(Elts[0]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000716
717 M.addTypeName("llvm.dbg.composite.type", Init->getType());
718 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
719 GlobalValue::InternalLinkage,
720 Init, "llvm.dbg.composite", &M);
721 GV->setSection("llvm.metadata");
722 return DICompositeType(GV);
723}
724
725
726/// CreateSubprogram - Create a new descriptor for the specified subprogram.
727/// See comments in DISubprogram for descriptions of these fields. This
728/// method does not unique the generated descriptors.
729DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
730 const std::string &Name,
731 const std::string &DisplayName,
732 const std::string &LinkageName,
733 DICompileUnit CompileUnit,
734 unsigned LineNo, DIType Type,
735 bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000736 bool isDefinition) {
Devang Patel854967e2008-12-17 22:39:29 +0000737
Owen Anderson76f600b2009-07-06 22:37:39 +0000738 LLVMContext& Ctxt = M.getContext();
Chris Lattnera45664f2008-11-10 02:56:27 +0000739 Constant *Elts[] = {
740 GetTagConstant(dwarf::DW_TAG_subprogram),
Owen Anderson76f600b2009-07-06 22:37:39 +0000741 Ctxt.getNullValue(EmptyStructPtr),
Chris Lattner497a7a82008-11-10 04:10:34 +0000742 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000743 GetStringConstant(Name),
744 GetStringConstant(DisplayName),
745 GetStringConstant(LinkageName),
Chris Lattner497a7a82008-11-10 04:10:34 +0000746 getCastToEmpty(CompileUnit),
Owen Anderson76f600b2009-07-06 22:37:39 +0000747 Ctxt.getConstantInt(Type::Int32Ty, LineNo),
Chris Lattner497a7a82008-11-10 04:10:34 +0000748 getCastToEmpty(Type),
Owen Anderson76f600b2009-07-06 22:37:39 +0000749 Ctxt.getConstantInt(Type::Int1Ty, isLocalToUnit),
750 Ctxt.getConstantInt(Type::Int1Ty, isDefinition)
Chris Lattnera45664f2008-11-10 02:56:27 +0000751 };
752
Owen Anderson76f600b2009-07-06 22:37:39 +0000753 Constant *Init =
754 Ctxt.getConstantStruct(Elts, sizeof(Elts)/sizeof(Elts[0]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000755
756 M.addTypeName("llvm.dbg.subprogram.type", Init->getType());
757 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
Devang Patel13e16b62009-06-26 01:49:18 +0000758 GlobalValue::LinkOnceAnyLinkage,
Chris Lattnera45664f2008-11-10 02:56:27 +0000759 Init, "llvm.dbg.subprogram", &M);
760 GV->setSection("llvm.metadata");
761 return DISubprogram(GV);
762}
763
764/// CreateGlobalVariable - Create a new descriptor for the specified global.
765DIGlobalVariable
766DIFactory::CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
767 const std::string &DisplayName,
768 const std::string &LinkageName,
769 DICompileUnit CompileUnit,
770 unsigned LineNo, DIType Type,bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000771 bool isDefinition, llvm::GlobalVariable *Val) {
Owen Anderson76f600b2009-07-06 22:37:39 +0000772
773 LLVMContext& Ctxt = M.getContext();
Chris Lattnera45664f2008-11-10 02:56:27 +0000774 Constant *Elts[] = {
775 GetTagConstant(dwarf::DW_TAG_variable),
Owen Anderson76f600b2009-07-06 22:37:39 +0000776 Ctxt.getNullValue(EmptyStructPtr),
Chris Lattner497a7a82008-11-10 04:10:34 +0000777 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000778 GetStringConstant(Name),
779 GetStringConstant(DisplayName),
780 GetStringConstant(LinkageName),
Chris Lattner497a7a82008-11-10 04:10:34 +0000781 getCastToEmpty(CompileUnit),
Owen Anderson76f600b2009-07-06 22:37:39 +0000782 Ctxt.getConstantInt(Type::Int32Ty, LineNo),
Chris Lattner497a7a82008-11-10 04:10:34 +0000783 getCastToEmpty(Type),
Owen Anderson76f600b2009-07-06 22:37:39 +0000784 Ctxt.getConstantInt(Type::Int1Ty, isLocalToUnit),
785 Ctxt.getConstantInt(Type::Int1Ty, isDefinition),
786 Ctxt.getConstantExprBitCast(Val, EmptyStructPtr)
Chris Lattnera45664f2008-11-10 02:56:27 +0000787 };
788
Owen Anderson76f600b2009-07-06 22:37:39 +0000789 Constant *Init =
790 Ctxt.getConstantStruct(Elts, sizeof(Elts)/sizeof(Elts[0]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000791
792 M.addTypeName("llvm.dbg.global_variable.type", Init->getType());
793 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
Devang Patel13e16b62009-06-26 01:49:18 +0000794 GlobalValue::LinkOnceAnyLinkage,
Chris Lattnera45664f2008-11-10 02:56:27 +0000795 Init, "llvm.dbg.global_variable", &M);
796 GV->setSection("llvm.metadata");
797 return DIGlobalVariable(GV);
798}
799
800
801/// CreateVariable - Create a new descriptor for the specified variable.
802DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
803 const std::string &Name,
804 DICompileUnit CompileUnit, unsigned LineNo,
Devang Pateldd9db662009-01-30 18:20:31 +0000805 DIType Type) {
Owen Anderson76f600b2009-07-06 22:37:39 +0000806 LLVMContext& Ctxt = M.getContext();
807
Chris Lattnera45664f2008-11-10 02:56:27 +0000808 Constant *Elts[] = {
809 GetTagConstant(Tag),
Chris Lattner497a7a82008-11-10 04:10:34 +0000810 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000811 GetStringConstant(Name),
Chris Lattner497a7a82008-11-10 04:10:34 +0000812 getCastToEmpty(CompileUnit),
Owen Anderson76f600b2009-07-06 22:37:39 +0000813 Ctxt.getConstantInt(Type::Int32Ty, LineNo),
Devang Pateldd9db662009-01-30 18:20:31 +0000814 getCastToEmpty(Type)
Chris Lattnera45664f2008-11-10 02:56:27 +0000815 };
816
Owen Anderson76f600b2009-07-06 22:37:39 +0000817 Constant *Init =
818 Ctxt.getConstantStruct(Elts, sizeof(Elts)/sizeof(Elts[0]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000819
820 M.addTypeName("llvm.dbg.variable.type", Init->getType());
821 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
822 GlobalValue::InternalLinkage,
823 Init, "llvm.dbg.variable", &M);
824 GV->setSection("llvm.metadata");
825 return DIVariable(GV);
826}
827
828
829/// CreateBlock - This creates a descriptor for a lexical block with the
830/// specified parent context.
831DIBlock DIFactory::CreateBlock(DIDescriptor Context) {
832 Constant *Elts[] = {
833 GetTagConstant(dwarf::DW_TAG_lexical_block),
Chris Lattner497a7a82008-11-10 04:10:34 +0000834 getCastToEmpty(Context)
Chris Lattnera45664f2008-11-10 02:56:27 +0000835 };
836
Owen Anderson76f600b2009-07-06 22:37:39 +0000837 Constant *Init =
838 M.getContext().getConstantStruct(Elts, sizeof(Elts)/sizeof(Elts[0]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000839
840 M.addTypeName("llvm.dbg.block.type", Init->getType());
841 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
842 GlobalValue::InternalLinkage,
843 Init, "llvm.dbg.block", &M);
844 GV->setSection("llvm.metadata");
845 return DIBlock(GV);
846}
847
848
849//===----------------------------------------------------------------------===//
850// DIFactory: Routines for inserting code into a function
851//===----------------------------------------------------------------------===//
852
853/// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
854/// inserting it at the end of the specified basic block.
855void DIFactory::InsertStopPoint(DICompileUnit CU, unsigned LineNo,
856 unsigned ColNo, BasicBlock *BB) {
857
858 // Lazily construct llvm.dbg.stoppoint function.
859 if (!StopPointFn)
860 StopPointFn = llvm::Intrinsic::getDeclaration(&M,
861 llvm::Intrinsic::dbg_stoppoint);
862
863 // Invoke llvm.dbg.stoppoint
864 Value *Args[] = {
Owen Anderson76f600b2009-07-06 22:37:39 +0000865 M.getContext().getConstantInt(llvm::Type::Int32Ty, LineNo),
866 M.getContext().getConstantInt(llvm::Type::Int32Ty, ColNo),
Chris Lattner497a7a82008-11-10 04:10:34 +0000867 getCastToEmpty(CU)
Chris Lattnera45664f2008-11-10 02:56:27 +0000868 };
869 CallInst::Create(StopPointFn, Args, Args+3, "", BB);
870}
871
872/// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
873/// mark the start of the specified subprogram.
874void DIFactory::InsertSubprogramStart(DISubprogram SP, BasicBlock *BB) {
875 // Lazily construct llvm.dbg.func.start.
876 if (!FuncStartFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000877 FuncStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_func_start);
Chris Lattnera45664f2008-11-10 02:56:27 +0000878
879 // Call llvm.dbg.func.start which also implicitly sets a stoppoint.
Chris Lattner497a7a82008-11-10 04:10:34 +0000880 CallInst::Create(FuncStartFn, getCastToEmpty(SP), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000881}
882
883/// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
884/// mark the start of a region for the specified scoping descriptor.
885void DIFactory::InsertRegionStart(DIDescriptor D, BasicBlock *BB) {
886 // Lazily construct llvm.dbg.region.start function.
887 if (!RegionStartFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000888 RegionStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_start);
889
Chris Lattnera45664f2008-11-10 02:56:27 +0000890 // Call llvm.dbg.func.start.
Chris Lattner497a7a82008-11-10 04:10:34 +0000891 CallInst::Create(RegionStartFn, getCastToEmpty(D), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000892}
893
Chris Lattnera45664f2008-11-10 02:56:27 +0000894/// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
895/// mark the end of a region for the specified scoping descriptor.
896void DIFactory::InsertRegionEnd(DIDescriptor D, BasicBlock *BB) {
897 // Lazily construct llvm.dbg.region.end function.
898 if (!RegionEndFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000899 RegionEndFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_end);
900
901 // Call llvm.dbg.region.end.
Chris Lattner497a7a82008-11-10 04:10:34 +0000902 CallInst::Create(RegionEndFn, getCastToEmpty(D), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000903}
904
905/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Bill Wendlingdc817b62009-05-14 18:26:15 +0000906void DIFactory::InsertDeclare(Value *Storage, DIVariable D, BasicBlock *BB) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000907 // Cast the storage to a {}* for the call to llvm.dbg.declare.
Bill Wendlingdc817b62009-05-14 18:26:15 +0000908 Storage = new BitCastInst(Storage, EmptyStructPtr, "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000909
910 if (!DeclareFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000911 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
912
Chris Lattner497a7a82008-11-10 04:10:34 +0000913 Value *Args[] = { Storage, getCastToEmpty(D) };
Chris Lattnera45664f2008-11-10 02:56:27 +0000914 CallInst::Create(DeclareFn, Args, Args+2, "", BB);
915}
Torok Edwin620f2802008-12-16 09:07:36 +0000916
917namespace llvm {
Bill Wendlingdc817b62009-05-14 18:26:15 +0000918 /// findStopPoint - Find the stoppoint coressponding to this instruction, that
919 /// is the stoppoint that dominates this instruction.
920 const DbgStopPointInst *findStopPoint(const Instruction *Inst) {
Torok Edwin620f2802008-12-16 09:07:36 +0000921 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(Inst))
922 return DSI;
923
924 const BasicBlock *BB = Inst->getParent();
925 BasicBlock::const_iterator I = Inst, B;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000926 while (BB) {
Torok Edwin620f2802008-12-16 09:07:36 +0000927 B = BB->begin();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000928
Torok Edwin620f2802008-12-16 09:07:36 +0000929 // A BB consisting only of a terminator can't have a stoppoint.
Bill Wendlingdc817b62009-05-14 18:26:15 +0000930 while (I != B) {
931 --I;
932 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
933 return DSI;
Torok Edwin620f2802008-12-16 09:07:36 +0000934 }
Bill Wendlingdc817b62009-05-14 18:26:15 +0000935
936 // This BB didn't have a stoppoint: if there is only one predecessor, look
937 // for a stoppoint there. We could use getIDom(), but that would require
938 // dominator info.
Torok Edwin620f2802008-12-16 09:07:36 +0000939 BB = I->getParent()->getUniquePredecessor();
940 if (BB)
941 I = BB->getTerminator();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000942 }
943
Torok Edwin620f2802008-12-16 09:07:36 +0000944 return 0;
945 }
946
Bill Wendlingdc817b62009-05-14 18:26:15 +0000947 /// findBBStopPoint - Find the stoppoint corresponding to first real
948 /// (non-debug intrinsic) instruction in this Basic Block, and return the
949 /// stoppoint for it.
950 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB) {
951 for(BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +0000952 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
953 return DSI;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000954
955 // Fallback to looking for stoppoint of unique predecessor. Useful if this
956 // BB contains no stoppoints, but unique predecessor does.
Torok Edwin620f2802008-12-16 09:07:36 +0000957 BB = BB->getUniquePredecessor();
958 if (BB)
959 return findStopPoint(BB->getTerminator());
Bill Wendlingdc817b62009-05-14 18:26:15 +0000960
Torok Edwin620f2802008-12-16 09:07:36 +0000961 return 0;
962 }
963
Bill Wendlingdc817b62009-05-14 18:26:15 +0000964 Value *findDbgGlobalDeclare(GlobalVariable *V) {
Torok Edwinff7d0e92009-03-10 13:41:26 +0000965 const Module *M = V->getParent();
966 const Type *Ty = M->getTypeByName("llvm.dbg.global_variable.type");
Bill Wendlingdc817b62009-05-14 18:26:15 +0000967 if (!Ty) return 0;
968
Owen Anderson76f600b2009-07-06 22:37:39 +0000969 Ty = V->getParent()->getContext().getPointerType(Ty, 0);
Torok Edwinff7d0e92009-03-10 13:41:26 +0000970
971 Value *Val = V->stripPointerCasts();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000972 for (Value::use_iterator I = Val->use_begin(), E = Val->use_end();
Torok Edwinff7d0e92009-03-10 13:41:26 +0000973 I != E; ++I) {
974 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(I)) {
975 if (CE->getOpcode() == Instruction::BitCast) {
976 Value *VV = CE;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000977
978 while (VV->hasOneUse())
Torok Edwinff7d0e92009-03-10 13:41:26 +0000979 VV = *VV->use_begin();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000980
Torok Edwinff7d0e92009-03-10 13:41:26 +0000981 if (VV->getType() == Ty)
982 return VV;
983 }
984 }
985 }
986
987 if (Val->getType() == Ty)
988 return Val;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000989
Torok Edwinff7d0e92009-03-10 13:41:26 +0000990 return 0;
991 }
992
Bill Wendlingdc817b62009-05-14 18:26:15 +0000993 /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
Torok Edwin620f2802008-12-16 09:07:36 +0000994 /// It looks through pointer casts too.
Bill Wendlingdc817b62009-05-14 18:26:15 +0000995 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts) {
Torok Edwin620f2802008-12-16 09:07:36 +0000996 if (stripCasts) {
997 V = V->stripPointerCasts();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000998
Torok Edwin620f2802008-12-16 09:07:36 +0000999 // Look for the bitcast.
1000 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001001 I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001002 if (isa<BitCastInst>(I))
1003 return findDbgDeclare(*I, false);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001004
Torok Edwin620f2802008-12-16 09:07:36 +00001005 return 0;
1006 }
1007
Bill Wendlingdc817b62009-05-14 18:26:15 +00001008 // Find llvm.dbg.declare among uses of the instruction.
Torok Edwin620f2802008-12-16 09:07:36 +00001009 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001010 I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001011 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I))
1012 return DDI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001013
Torok Edwin620f2802008-12-16 09:07:36 +00001014 return 0;
1015 }
Torok Edwinff7d0e92009-03-10 13:41:26 +00001016
Bill Wendlingdc817b62009-05-14 18:26:15 +00001017 bool getLocationInfo(const Value *V, std::string &DisplayName,
1018 std::string &Type, unsigned &LineNo, std::string &File,
1019 std::string &Dir) {
Torok Edwinff7d0e92009-03-10 13:41:26 +00001020 DICompileUnit Unit;
1021 DIType TypeD;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001022
Torok Edwinff7d0e92009-03-10 13:41:26 +00001023 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1024 Value *DIGV = findDbgGlobalDeclare(GV);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001025 if (!DIGV) return false;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001026 DIGlobalVariable Var(cast<GlobalVariable>(DIGV));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001027
Bill Wendling0582ae92009-03-13 04:39:26 +00001028 Var.getDisplayName(DisplayName);
Torok Edwinff7d0e92009-03-10 13:41:26 +00001029 LineNo = Var.getLineNumber();
1030 Unit = Var.getCompileUnit();
1031 TypeD = Var.getType();
1032 } else {
1033 const DbgDeclareInst *DDI = findDbgDeclare(V);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001034 if (!DDI) return false;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001035 DIVariable Var(cast<GlobalVariable>(DDI->getVariable()));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001036
Bill Wendling0582ae92009-03-13 04:39:26 +00001037 Var.getName(DisplayName);
Torok Edwinff7d0e92009-03-10 13:41:26 +00001038 LineNo = Var.getLineNumber();
1039 Unit = Var.getCompileUnit();
1040 TypeD = Var.getType();
1041 }
Bill Wendlingdc817b62009-05-14 18:26:15 +00001042
Bill Wendling0582ae92009-03-13 04:39:26 +00001043 TypeD.getName(Type);
1044 Unit.getFilename(File);
1045 Unit.getDirectory(Dir);
Torok Edwinff7d0e92009-03-10 13:41:26 +00001046 return true;
1047 }
Devang Patel13e16b62009-06-26 01:49:18 +00001048
1049 /// CollectDebugInfoAnchors - Collect debugging information anchors.
1050 void CollectDebugInfoAnchors(Module &M,
1051 SmallVector<GlobalVariable *, 2> &CUs,
1052 SmallVector<GlobalVariable *, 4> &GVs,
1053 SmallVector<GlobalVariable *, 4> &SPs) {
1054
1055 for (Module::global_iterator GVI = M.global_begin(), E = M.global_end();
1056 GVI != E; GVI++) {
1057 GlobalVariable *GV = GVI;
1058 if (GV->hasName() && strncmp(GV->getNameStart(), "llvm.dbg", 8) == 0
1059 && GV->isConstant() && GV->hasInitializer()) {
1060 DICompileUnit C(GV);
1061 if (C.isNull() == false) {
1062 CUs.push_back(GV);
1063 continue;
1064 }
1065 DIGlobalVariable G(GV);
1066 if (G.isNull() == false) {
1067 GVs.push_back(GV);
1068 continue;
1069 }
1070 DISubprogram S(GV);
1071 if (S.isNull() == false) {
1072 SPs.push_back(GV);
1073 continue;
1074 }
1075 }
1076 }
1077 }
Devang Patel9e529c32009-07-02 01:15:24 +00001078
1079 /// isValidDebugInfoIntrinsic - Return true if SPI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001080 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001081 bool isValidDebugInfoIntrinsic(DbgStopPointInst &SPI,
1082 CodeGenOpt::Level OptLev) {
1083 return DIDescriptor::ValidDebugInfo(SPI.getContext(), OptLev);
1084 }
1085
1086 /// isValidDebugInfoIntrinsic - Return true if FSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001087 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001088 bool isValidDebugInfoIntrinsic(DbgFuncStartInst &FSI,
1089 CodeGenOpt::Level OptLev) {
1090 return DIDescriptor::ValidDebugInfo(FSI.getSubprogram(), OptLev);
1091 }
1092
1093 /// isValidDebugInfoIntrinsic - Return true if RSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001094 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001095 bool isValidDebugInfoIntrinsic(DbgRegionStartInst &RSI,
1096 CodeGenOpt::Level OptLev) {
1097 return DIDescriptor::ValidDebugInfo(RSI.getContext(), OptLev);
1098 }
1099
1100 /// isValidDebugInfoIntrinsic - Return true if REI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001101 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001102 bool isValidDebugInfoIntrinsic(DbgRegionEndInst &REI,
1103 CodeGenOpt::Level OptLev) {
1104 return DIDescriptor::ValidDebugInfo(REI.getContext(), OptLev);
1105 }
1106
1107
1108 /// isValidDebugInfoIntrinsic - Return true if DI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001109 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001110 bool isValidDebugInfoIntrinsic(DbgDeclareInst &DI,
1111 CodeGenOpt::Level OptLev) {
1112 return DIDescriptor::ValidDebugInfo(DI.getVariable(), OptLev);
1113 }
1114
1115 /// ExtractDebugLocation - Extract debug location information
1116 /// from llvm.dbg.stoppoint intrinsic.
1117 DebugLoc ExtractDebugLocation(DbgStopPointInst &SPI,
Devang Patel9e529c32009-07-02 01:15:24 +00001118 DebugLocTracker &DebugLocInfo) {
1119 DebugLoc DL;
1120 Value *Context = SPI.getContext();
Devang Patel9e529c32009-07-02 01:15:24 +00001121
1122 // If this location is already tracked then use it.
1123 DebugLocTuple Tuple(cast<GlobalVariable>(Context), SPI.getLine(),
1124 SPI.getColumn());
1125 DenseMap<DebugLocTuple, unsigned>::iterator II
1126 = DebugLocInfo.DebugIdMap.find(Tuple);
1127 if (II != DebugLocInfo.DebugIdMap.end())
1128 return DebugLoc::get(II->second);
1129
1130 // Add a new location entry.
1131 unsigned Id = DebugLocInfo.DebugLocations.size();
1132 DebugLocInfo.DebugLocations.push_back(Tuple);
1133 DebugLocInfo.DebugIdMap[Tuple] = Id;
1134
1135 return DebugLoc::get(Id);
1136 }
1137
1138 /// ExtractDebugLocation - Extract debug location information
1139 /// from llvm.dbg.func_start intrinsic.
1140 DebugLoc ExtractDebugLocation(DbgFuncStartInst &FSI,
Devang Patel9e529c32009-07-02 01:15:24 +00001141 DebugLocTracker &DebugLocInfo) {
1142 DebugLoc DL;
1143 Value *SP = FSI.getSubprogram();
Devang Patel9e529c32009-07-02 01:15:24 +00001144
1145 DISubprogram Subprogram(cast<GlobalVariable>(SP));
1146 unsigned Line = Subprogram.getLineNumber();
1147 DICompileUnit CU(Subprogram.getCompileUnit());
1148
1149 // If this location is already tracked then use it.
1150 DebugLocTuple Tuple(CU.getGV(), Line, /* Column */ 0);
1151 DenseMap<DebugLocTuple, unsigned>::iterator II
1152 = DebugLocInfo.DebugIdMap.find(Tuple);
1153 if (II != DebugLocInfo.DebugIdMap.end())
1154 return DebugLoc::get(II->second);
1155
1156 // Add a new location entry.
1157 unsigned Id = DebugLocInfo.DebugLocations.size();
1158 DebugLocInfo.DebugLocations.push_back(Tuple);
1159 DebugLocInfo.DebugIdMap[Tuple] = Id;
1160
1161 return DebugLoc::get(Id);
1162 }
1163
1164 /// isInlinedFnStart - Return true if FSI is starting an inlined function.
1165 bool isInlinedFnStart(DbgFuncStartInst &FSI, const Function *CurrentFn) {
1166 DISubprogram Subprogram(cast<GlobalVariable>(FSI.getSubprogram()));
1167 if (Subprogram.describes(CurrentFn))
1168 return false;
1169
1170 return true;
1171 }
1172
1173 /// isInlinedFnEnd - Return true if REI is ending an inlined function.
1174 bool isInlinedFnEnd(DbgRegionEndInst &REI, const Function *CurrentFn) {
1175 DISubprogram Subprogram(cast<GlobalVariable>(REI.getContext()));
1176 if (Subprogram.isNull() || Subprogram.describes(CurrentFn))
1177 return false;
1178
1179 return true;
1180 }
1181
Torok Edwin620f2802008-12-16 09:07:36 +00001182}