blob: 47a561bc29c00a253e784cc0e711420f9dce96ed [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 Anderson99035272009-07-07 17:12:53 +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 Patelc4999d72009-07-22 18:23:44 +0000209/// replaceAllUsesWith - Replace all uses of debug info referenced by
210/// this descriptor. After this completes, the current debug info value
211/// is erased.
212void DIDerivedType::replaceAllUsesWith(DIDescriptor &D) {
213 if (isNull())
214 return;
215
Devang Patel6930f4f2009-07-22 18:56:16 +0000216 assert (!D.isNull() && "Can not replace with null");
Devang Patelc4999d72009-07-22 18:23:44 +0000217 getGV()->replaceAllUsesWith(D.getGV());
218 getGV()->eraseFromParent();
219}
220
Devang Patelb79b5352009-01-19 23:21:49 +0000221/// Verify - Verify that a compile unit is well formed.
222bool DICompileUnit::Verify() const {
223 if (isNull())
224 return false;
Bill Wendling0582ae92009-03-13 04:39:26 +0000225 std::string Res;
226 if (getFilename(Res).empty())
227 return false;
Devang Patelb79b5352009-01-19 23:21:49 +0000228 // It is possible that directory and produce string is empty.
Bill Wendling0582ae92009-03-13 04:39:26 +0000229 return true;
Devang Patelb79b5352009-01-19 23:21:49 +0000230}
231
232/// Verify - Verify that a type descriptor is well formed.
233bool DIType::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 composite type descriptor is well formed.
246bool DICompositeType::Verify() const {
247 if (isNull())
248 return false;
249 if (getContext().isNull())
250 return false;
251
252 DICompileUnit CU = getCompileUnit();
253 if (!CU.isNull() && !CU.Verify())
254 return false;
255 return true;
256}
257
258/// Verify - Verify that a subprogram descriptor is well formed.
259bool DISubprogram::Verify() const {
260 if (isNull())
261 return false;
262
263 if (getContext().isNull())
264 return false;
265
266 DICompileUnit CU = getCompileUnit();
267 if (!CU.Verify())
268 return false;
269
270 DICompositeType Ty = getType();
271 if (!Ty.isNull() && !Ty.Verify())
272 return false;
273 return true;
274}
275
276/// Verify - Verify that a global variable descriptor is well formed.
277bool DIGlobalVariable::Verify() const {
278 if (isNull())
279 return false;
280
281 if (getContext().isNull())
282 return false;
283
284 DICompileUnit CU = getCompileUnit();
Chris Lattnere3f6cea2009-05-05 04:55:56 +0000285 if (!CU.isNull() && !CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000286 return false;
287
288 DIType Ty = getType();
289 if (!Ty.Verify())
290 return false;
291
292 if (!getGlobal())
293 return false;
294
295 return true;
296}
297
298/// Verify - Verify that a variable descriptor is well formed.
299bool DIVariable::Verify() const {
300 if (isNull())
301 return false;
302
303 if (getContext().isNull())
304 return false;
305
306 DIType Ty = getType();
307 if (!Ty.Verify())
308 return false;
309
Devang Patelb79b5352009-01-19 23:21:49 +0000310 return true;
311}
312
Devang Patel36375ee2009-02-17 21:23:59 +0000313/// getOriginalTypeSize - If this type is derived from a base type then
314/// return base type size.
315uint64_t DIDerivedType::getOriginalTypeSize() const {
316 if (getTag() != dwarf::DW_TAG_member)
317 return getSizeInBits();
318 DIType BT = getTypeDerivedFrom();
319 if (BT.getTag() != dwarf::DW_TAG_base_type)
320 return getSizeInBits();
321 return BT.getSizeInBits();
322}
Devang Patelb79b5352009-01-19 23:21:49 +0000323
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000324/// describes - Return true if this subprogram provides debugging
325/// information for the function F.
326bool DISubprogram::describes(const Function *F) {
327 assert (F && "Invalid function");
328 std::string Name;
329 getLinkageName(Name);
330 if (Name.empty())
331 getName(Name);
Daniel Dunbar460f6562009-07-26 09:48:23 +0000332 if (F->getName() == Name)
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000333 return true;
334 return false;
335}
336
Chris Lattnera45664f2008-11-10 02:56:27 +0000337//===----------------------------------------------------------------------===//
Devang Patel7136a652009-07-01 22:10:23 +0000338// DIDescriptor: dump routines for all descriptors.
339//===----------------------------------------------------------------------===//
340
341
342/// dump - Print descriptor.
343void DIDescriptor::dump() const {
344 cerr << "[" << dwarf::TagString(getTag()) << "] ";
345 cerr << std::hex << "[GV:" << DbgGV << "]" << std::dec;
346}
347
348/// dump - Print compile unit.
349void DICompileUnit::dump() const {
350 if (getLanguage())
351 cerr << " [" << dwarf::LanguageString(getLanguage()) << "] ";
352
353 std::string Res1, Res2;
354 cerr << " [" << getDirectory(Res1) << "/" << getFilename(Res2) << " ]";
355}
356
357/// dump - Print type.
358void DIType::dump() const {
359 if (isNull()) return;
360
361 std::string Res;
362 if (!getName(Res).empty())
363 cerr << " [" << Res << "] ";
364
365 unsigned Tag = getTag();
366 cerr << " [" << dwarf::TagString(Tag) << "] ";
367
368 // TODO : Print context
369 getCompileUnit().dump();
370 cerr << " ["
371 << getLineNumber() << ", "
372 << getSizeInBits() << ", "
373 << getAlignInBits() << ", "
374 << getOffsetInBits()
375 << "] ";
376
377 if (isPrivate())
378 cerr << " [private] ";
379 else if (isProtected())
380 cerr << " [protected] ";
381
382 if (isForwardDecl())
383 cerr << " [fwd] ";
384
385 if (isBasicType(Tag))
386 DIBasicType(DbgGV).dump();
387 else if (isDerivedType(Tag))
388 DIDerivedType(DbgGV).dump();
389 else if (isCompositeType(Tag))
390 DICompositeType(DbgGV).dump();
391 else {
392 cerr << "Invalid DIType\n";
393 return;
394 }
395
396 cerr << "\n";
397}
398
399/// dump - Print basic type.
400void DIBasicType::dump() const {
401 cerr << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
402}
403
404/// dump - Print derived type.
405void DIDerivedType::dump() const {
406 cerr << "\n\t Derived From: "; getTypeDerivedFrom().dump();
407}
408
409/// dump - Print composite type.
410void DICompositeType::dump() const {
411 DIArray A = getTypeArray();
412 if (A.isNull())
413 return;
414 cerr << " [" << A.getNumElements() << " elements]";
415}
416
417/// dump - Print global.
418void DIGlobal::dump() const {
419 std::string Res;
420 if (!getName(Res).empty())
421 cerr << " [" << Res << "] ";
422
423 unsigned Tag = getTag();
424 cerr << " [" << dwarf::TagString(Tag) << "] ";
425
426 // TODO : Print context
427 getCompileUnit().dump();
428 cerr << " [" << getLineNumber() << "] ";
429
430 if (isLocalToUnit())
431 cerr << " [local] ";
432
433 if (isDefinition())
434 cerr << " [def] ";
435
436 if (isGlobalVariable(Tag))
437 DIGlobalVariable(DbgGV).dump();
438
439 cerr << "\n";
440}
441
442/// dump - Print subprogram.
443void DISubprogram::dump() const {
444 DIGlobal::dump();
445}
446
447/// dump - Print global variable.
448void DIGlobalVariable::dump() const {
449 cerr << " ["; getGlobal()->dump(); cerr << "] ";
450}
451
452/// dump - Print variable.
453void DIVariable::dump() const {
454 std::string Res;
455 if (!getName(Res).empty())
456 cerr << " [" << Res << "] ";
457
458 getCompileUnit().dump();
459 cerr << " [" << getLineNumber() << "] ";
460 getType().dump();
461 cerr << "\n";
462}
463
464//===----------------------------------------------------------------------===//
Chris Lattnera45664f2008-11-10 02:56:27 +0000465// DIFactory: Basic Helpers
466//===----------------------------------------------------------------------===//
467
Bill Wendlingdc817b62009-05-14 18:26:15 +0000468DIFactory::DIFactory(Module &m)
Owen Anderson99035272009-07-07 17:12:53 +0000469 : M(m), VMContext(M.getContext()), StopPointFn(0), FuncStartFn(0),
470 RegionStartFn(0), RegionEndFn(0),
Bill Wendlingdc817b62009-05-14 18:26:15 +0000471 DeclareFn(0) {
Owen Anderson99035272009-07-07 17:12:53 +0000472 EmptyStructPtr = VMContext.getPointerTypeUnqual(VMContext.getStructType());
Chris Lattner497a7a82008-11-10 04:10:34 +0000473}
474
475/// getCastToEmpty - Return this descriptor as a Constant* with type '{}*'.
476/// This is only valid when the descriptor is non-null.
477Constant *DIFactory::getCastToEmpty(DIDescriptor D) {
Owen Anderson0a5372e2009-07-13 04:09:18 +0000478 if (D.isNull()) return VMContext.getNullValue(EmptyStructPtr);
Owen Anderson99035272009-07-07 17:12:53 +0000479 return VMContext.getConstantExprBitCast(D.getGV(), EmptyStructPtr);
Chris Lattner497a7a82008-11-10 04:10:34 +0000480}
481
Chris Lattnera45664f2008-11-10 02:56:27 +0000482Constant *DIFactory::GetTagConstant(unsigned TAG) {
Devang Patel6906ba52009-01-20 19:22:03 +0000483 assert((TAG & LLVMDebugVersionMask) == 0 &&
Chris Lattnera45664f2008-11-10 02:56:27 +0000484 "Tag too large for debug encoding!");
Owen Andersoneed707b2009-07-24 23:12:02 +0000485 return ConstantInt::get(Type::Int32Ty, TAG | LLVMDebugVersion);
Chris Lattnera45664f2008-11-10 02:56:27 +0000486}
487
488Constant *DIFactory::GetStringConstant(const std::string &String) {
489 // Check string cache for previous edition.
490 Constant *&Slot = StringCache[String];
491
492 // Return Constant if previously defined.
493 if (Slot) return Slot;
494
Owen Anderson99035272009-07-07 17:12:53 +0000495 const PointerType *DestTy = VMContext.getPointerTypeUnqual(Type::Int8Ty);
Chris Lattnera45664f2008-11-10 02:56:27 +0000496
Dan Gohmana119de82009-06-14 23:30:43 +0000497 // If empty string then use a i8* null instead.
Chris Lattnera45664f2008-11-10 02:56:27 +0000498 if (String.empty())
Owen Anderson99035272009-07-07 17:12:53 +0000499 return Slot = VMContext.getConstantPointerNull(DestTy);
Chris Lattnera45664f2008-11-10 02:56:27 +0000500
501 // Construct string as an llvm constant.
Owen Anderson99035272009-07-07 17:12:53 +0000502 Constant *ConstStr = VMContext.getConstantArray(String);
Chris Lattnera45664f2008-11-10 02:56:27 +0000503
504 // Otherwise create and return a new string global.
Owen Andersone9b11b42009-07-08 19:03:57 +0000505 GlobalVariable *StrGV = new GlobalVariable(M, ConstStr->getType(), true,
Chris Lattnera45664f2008-11-10 02:56:27 +0000506 GlobalVariable::InternalLinkage,
Owen Andersone9b11b42009-07-08 19:03:57 +0000507 ConstStr, ".str");
Chris Lattnera45664f2008-11-10 02:56:27 +0000508 StrGV->setSection("llvm.metadata");
Owen Anderson99035272009-07-07 17:12:53 +0000509 return Slot = VMContext.getConstantExprBitCast(StrGV, DestTy);
Chris Lattnera45664f2008-11-10 02:56:27 +0000510}
511
Chris Lattnera45664f2008-11-10 02:56:27 +0000512//===----------------------------------------------------------------------===//
513// DIFactory: Primary Constructors
514//===----------------------------------------------------------------------===//
515
Chris Lattnera45664f2008-11-10 02:56:27 +0000516/// GetOrCreateArray - Create an descriptor for an array of descriptors.
517/// This implicitly uniques the arrays created.
518DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
519 SmallVector<Constant*, 16> Elts;
520
521 for (unsigned i = 0; i != NumTys; ++i)
Chris Lattner497a7a82008-11-10 04:10:34 +0000522 Elts.push_back(getCastToEmpty(Tys[i]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000523
Owen Anderson99035272009-07-07 17:12:53 +0000524 Constant *Init = VMContext.getConstantArray(VMContext.getArrayType(EmptyStructPtr,
Chris Lattner497a7a82008-11-10 04:10:34 +0000525 Elts.size()),
Jay Foade3e51c02009-05-21 09:52:38 +0000526 Elts.data(), Elts.size());
Chris Lattnera45664f2008-11-10 02:56:27 +0000527 // If we already have this array, just return the uniqued version.
528 DIDescriptor &Entry = SimpleConstantCache[Init];
529 if (!Entry.isNull()) return DIArray(Entry.getGV());
530
Owen Andersone9b11b42009-07-08 19:03:57 +0000531 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
Chris Lattnera45664f2008-11-10 02:56:27 +0000532 GlobalValue::InternalLinkage,
Owen Andersone9b11b42009-07-08 19:03:57 +0000533 Init, "llvm.dbg.array");
Chris Lattnera45664f2008-11-10 02:56:27 +0000534 GV->setSection("llvm.metadata");
535 Entry = DIDescriptor(GV);
536 return DIArray(GV);
537}
538
539/// GetOrCreateSubrange - Create a descriptor for a value range. This
540/// implicitly uniques the values returned.
541DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
542 Constant *Elts[] = {
543 GetTagConstant(dwarf::DW_TAG_subrange_type),
Owen Andersoneed707b2009-07-24 23:12:02 +0000544 ConstantInt::get(Type::Int64Ty, Lo),
545 ConstantInt::get(Type::Int64Ty, Hi)
Chris Lattnera45664f2008-11-10 02:56:27 +0000546 };
547
Owen Anderson8fa33382009-07-27 22:29:26 +0000548 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000549
550 // If we already have this range, just return the uniqued version.
551 DIDescriptor &Entry = SimpleConstantCache[Init];
552 if (!Entry.isNull()) return DISubrange(Entry.getGV());
553
554 M.addTypeName("llvm.dbg.subrange.type", Init->getType());
555
Owen Andersone9b11b42009-07-08 19:03:57 +0000556 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
Chris Lattnera45664f2008-11-10 02:56:27 +0000557 GlobalValue::InternalLinkage,
Owen Andersone9b11b42009-07-08 19:03:57 +0000558 Init, "llvm.dbg.subrange");
Chris Lattnera45664f2008-11-10 02:56:27 +0000559 GV->setSection("llvm.metadata");
560 Entry = DIDescriptor(GV);
561 return DISubrange(GV);
562}
563
564
565
566/// CreateCompileUnit - Create a new descriptor for the specified compile
567/// unit. Note that this does not unique compile units within the module.
568DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
569 const std::string &Filename,
570 const std::string &Directory,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000571 const std::string &Producer,
Devang Pateldd9db662009-01-30 18:20:31 +0000572 bool isMain,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000573 bool isOptimized,
Devang Patel13319ce2009-02-17 22:43:44 +0000574 const char *Flags,
575 unsigned RunTimeVer) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000576 Constant *Elts[] = {
577 GetTagConstant(dwarf::DW_TAG_compile_unit),
Owen Anderson99035272009-07-07 17:12:53 +0000578 VMContext.getNullValue(EmptyStructPtr),
Owen Andersoneed707b2009-07-24 23:12:02 +0000579 ConstantInt::get(Type::Int32Ty, LangID),
Chris Lattnera45664f2008-11-10 02:56:27 +0000580 GetStringConstant(Filename),
581 GetStringConstant(Directory),
Devang Patel3b64c6b2009-01-23 22:33:47 +0000582 GetStringConstant(Producer),
Owen Andersoneed707b2009-07-24 23:12:02 +0000583 ConstantInt::get(Type::Int1Ty, isMain),
584 ConstantInt::get(Type::Int1Ty, isOptimized),
Devang Patel13319ce2009-02-17 22:43:44 +0000585 GetStringConstant(Flags),
Owen Andersoneed707b2009-07-24 23:12:02 +0000586 ConstantInt::get(Type::Int32Ty, RunTimeVer)
Chris Lattnera45664f2008-11-10 02:56:27 +0000587 };
588
Owen Anderson8fa33382009-07-27 22:29:26 +0000589 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000590
591 M.addTypeName("llvm.dbg.compile_unit.type", Init->getType());
Owen Andersone9b11b42009-07-08 19:03:57 +0000592 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
Devang Patel13e16b62009-06-26 01:49:18 +0000593 GlobalValue::LinkOnceAnyLinkage,
Owen Andersone9b11b42009-07-08 19:03:57 +0000594 Init, "llvm.dbg.compile_unit");
Chris Lattnera45664f2008-11-10 02:56:27 +0000595 GV->setSection("llvm.metadata");
596 return DICompileUnit(GV);
597}
598
599/// CreateEnumerator - Create a single enumerator value.
600DIEnumerator DIFactory::CreateEnumerator(const std::string &Name, uint64_t Val){
601 Constant *Elts[] = {
602 GetTagConstant(dwarf::DW_TAG_enumerator),
603 GetStringConstant(Name),
Owen Andersoneed707b2009-07-24 23:12:02 +0000604 ConstantInt::get(Type::Int64Ty, Val)
Chris Lattnera45664f2008-11-10 02:56:27 +0000605 };
606
Owen Anderson8fa33382009-07-27 22:29:26 +0000607 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000608
609 M.addTypeName("llvm.dbg.enumerator.type", Init->getType());
Owen Andersone9b11b42009-07-08 19:03:57 +0000610 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
Chris Lattnera45664f2008-11-10 02:56:27 +0000611 GlobalValue::InternalLinkage,
Owen Andersone9b11b42009-07-08 19:03:57 +0000612 Init, "llvm.dbg.enumerator");
Chris Lattnera45664f2008-11-10 02:56:27 +0000613 GV->setSection("llvm.metadata");
614 return DIEnumerator(GV);
615}
616
617
618/// CreateBasicType - Create a basic type like int, float, etc.
619DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
Bill Wendling0582ae92009-03-13 04:39:26 +0000620 const std::string &Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000621 DICompileUnit CompileUnit,
622 unsigned LineNumber,
623 uint64_t SizeInBits,
624 uint64_t AlignInBits,
625 uint64_t OffsetInBits, unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000626 unsigned Encoding) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000627 Constant *Elts[] = {
628 GetTagConstant(dwarf::DW_TAG_base_type),
Chris Lattner497a7a82008-11-10 04:10:34 +0000629 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000630 GetStringConstant(Name),
Chris Lattner497a7a82008-11-10 04:10:34 +0000631 getCastToEmpty(CompileUnit),
Owen Andersoneed707b2009-07-24 23:12:02 +0000632 ConstantInt::get(Type::Int32Ty, LineNumber),
633 ConstantInt::get(Type::Int64Ty, SizeInBits),
634 ConstantInt::get(Type::Int64Ty, AlignInBits),
635 ConstantInt::get(Type::Int64Ty, OffsetInBits),
636 ConstantInt::get(Type::Int32Ty, Flags),
637 ConstantInt::get(Type::Int32Ty, Encoding)
Chris Lattnera45664f2008-11-10 02:56:27 +0000638 };
639
Owen Anderson8fa33382009-07-27 22:29:26 +0000640 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000641
642 M.addTypeName("llvm.dbg.basictype.type", Init->getType());
Owen Andersone9b11b42009-07-08 19:03:57 +0000643 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
Chris Lattnera45664f2008-11-10 02:56:27 +0000644 GlobalValue::InternalLinkage,
Owen Andersone9b11b42009-07-08 19:03:57 +0000645 Init, "llvm.dbg.basictype");
Chris Lattnera45664f2008-11-10 02:56:27 +0000646 GV->setSection("llvm.metadata");
647 return DIBasicType(GV);
648}
649
650/// CreateDerivedType - Create a derived type like const qualified type,
651/// pointer, typedef, etc.
652DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
653 DIDescriptor Context,
654 const std::string &Name,
655 DICompileUnit CompileUnit,
656 unsigned LineNumber,
657 uint64_t SizeInBits,
658 uint64_t AlignInBits,
659 uint64_t OffsetInBits,
660 unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000661 DIType DerivedFrom) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000662 Constant *Elts[] = {
663 GetTagConstant(Tag),
Chris Lattner497a7a82008-11-10 04:10:34 +0000664 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000665 GetStringConstant(Name),
Chris Lattner497a7a82008-11-10 04:10:34 +0000666 getCastToEmpty(CompileUnit),
Owen Andersoneed707b2009-07-24 23:12:02 +0000667 ConstantInt::get(Type::Int32Ty, LineNumber),
668 ConstantInt::get(Type::Int64Ty, SizeInBits),
669 ConstantInt::get(Type::Int64Ty, AlignInBits),
670 ConstantInt::get(Type::Int64Ty, OffsetInBits),
671 ConstantInt::get(Type::Int32Ty, Flags),
Devang Pateldd9db662009-01-30 18:20:31 +0000672 getCastToEmpty(DerivedFrom)
Chris Lattnera45664f2008-11-10 02:56:27 +0000673 };
674
Owen Anderson8fa33382009-07-27 22:29:26 +0000675 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000676
677 M.addTypeName("llvm.dbg.derivedtype.type", Init->getType());
Owen Andersone9b11b42009-07-08 19:03:57 +0000678 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
Chris Lattnera45664f2008-11-10 02:56:27 +0000679 GlobalValue::InternalLinkage,
Owen Andersone9b11b42009-07-08 19:03:57 +0000680 Init, "llvm.dbg.derivedtype");
Chris Lattnera45664f2008-11-10 02:56:27 +0000681 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 Andersone277fed2009-07-07 16:31:25 +0000698
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 Andersoneed707b2009-07-24 23:12:02 +0000704 ConstantInt::get(Type::Int32Ty, LineNumber),
705 ConstantInt::get(Type::Int64Ty, SizeInBits),
706 ConstantInt::get(Type::Int64Ty, AlignInBits),
707 ConstantInt::get(Type::Int64Ty, OffsetInBits),
708 ConstantInt::get(Type::Int32Ty, Flags),
Chris Lattner497a7a82008-11-10 04:10:34 +0000709 getCastToEmpty(DerivedFrom),
Devang Patel13319ce2009-02-17 22:43:44 +0000710 getCastToEmpty(Elements),
Owen Andersoneed707b2009-07-24 23:12:02 +0000711 ConstantInt::get(Type::Int32Ty, RuntimeLang)
Chris Lattnera45664f2008-11-10 02:56:27 +0000712 };
713
Owen Anderson8fa33382009-07-27 22:29:26 +0000714 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000715
716 M.addTypeName("llvm.dbg.composite.type", Init->getType());
Owen Andersone9b11b42009-07-08 19:03:57 +0000717 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
Chris Lattnera45664f2008-11-10 02:56:27 +0000718 GlobalValue::InternalLinkage,
Owen Andersone9b11b42009-07-08 19:03:57 +0000719 Init, "llvm.dbg.composite");
Chris Lattnera45664f2008-11-10 02:56:27 +0000720 GV->setSection("llvm.metadata");
721 return DICompositeType(GV);
722}
723
724
725/// CreateSubprogram - Create a new descriptor for the specified subprogram.
726/// See comments in DISubprogram for descriptions of these fields. This
727/// method does not unique the generated descriptors.
728DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
729 const std::string &Name,
730 const std::string &DisplayName,
731 const std::string &LinkageName,
732 DICompileUnit CompileUnit,
733 unsigned LineNo, DIType Type,
734 bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000735 bool isDefinition) {
Devang Patel854967e2008-12-17 22:39:29 +0000736
Chris Lattnera45664f2008-11-10 02:56:27 +0000737 Constant *Elts[] = {
738 GetTagConstant(dwarf::DW_TAG_subprogram),
Owen Anderson99035272009-07-07 17:12:53 +0000739 VMContext.getNullValue(EmptyStructPtr),
Chris Lattner497a7a82008-11-10 04:10:34 +0000740 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000741 GetStringConstant(Name),
742 GetStringConstant(DisplayName),
743 GetStringConstant(LinkageName),
Chris Lattner497a7a82008-11-10 04:10:34 +0000744 getCastToEmpty(CompileUnit),
Owen Andersoneed707b2009-07-24 23:12:02 +0000745 ConstantInt::get(Type::Int32Ty, LineNo),
Chris Lattner497a7a82008-11-10 04:10:34 +0000746 getCastToEmpty(Type),
Owen Andersoneed707b2009-07-24 23:12:02 +0000747 ConstantInt::get(Type::Int1Ty, isLocalToUnit),
748 ConstantInt::get(Type::Int1Ty, isDefinition)
Chris Lattnera45664f2008-11-10 02:56:27 +0000749 };
750
Owen Anderson8fa33382009-07-27 22:29:26 +0000751 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000752
753 M.addTypeName("llvm.dbg.subprogram.type", Init->getType());
Owen Andersone9b11b42009-07-08 19:03:57 +0000754 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
Devang Patel13e16b62009-06-26 01:49:18 +0000755 GlobalValue::LinkOnceAnyLinkage,
Owen Andersone9b11b42009-07-08 19:03:57 +0000756 Init, "llvm.dbg.subprogram");
Chris Lattnera45664f2008-11-10 02:56:27 +0000757 GV->setSection("llvm.metadata");
758 return DISubprogram(GV);
759}
760
761/// CreateGlobalVariable - Create a new descriptor for the specified global.
762DIGlobalVariable
763DIFactory::CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
764 const std::string &DisplayName,
765 const std::string &LinkageName,
766 DICompileUnit CompileUnit,
767 unsigned LineNo, DIType Type,bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000768 bool isDefinition, llvm::GlobalVariable *Val) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000769 Constant *Elts[] = {
770 GetTagConstant(dwarf::DW_TAG_variable),
Owen Anderson99035272009-07-07 17:12:53 +0000771 VMContext.getNullValue(EmptyStructPtr),
Chris Lattner497a7a82008-11-10 04:10:34 +0000772 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000773 GetStringConstant(Name),
774 GetStringConstant(DisplayName),
775 GetStringConstant(LinkageName),
Chris Lattner497a7a82008-11-10 04:10:34 +0000776 getCastToEmpty(CompileUnit),
Owen Andersoneed707b2009-07-24 23:12:02 +0000777 ConstantInt::get(Type::Int32Ty, LineNo),
Chris Lattner497a7a82008-11-10 04:10:34 +0000778 getCastToEmpty(Type),
Owen Andersoneed707b2009-07-24 23:12:02 +0000779 ConstantInt::get(Type::Int1Ty, isLocalToUnit),
780 ConstantInt::get(Type::Int1Ty, isDefinition),
Owen Anderson99035272009-07-07 17:12:53 +0000781 VMContext.getConstantExprBitCast(Val, EmptyStructPtr)
Chris Lattnera45664f2008-11-10 02:56:27 +0000782 };
783
Owen Anderson8fa33382009-07-27 22:29:26 +0000784 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000785
786 M.addTypeName("llvm.dbg.global_variable.type", Init->getType());
Owen Andersone9b11b42009-07-08 19:03:57 +0000787 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
Devang Patel13e16b62009-06-26 01:49:18 +0000788 GlobalValue::LinkOnceAnyLinkage,
Owen Andersone9b11b42009-07-08 19:03:57 +0000789 Init, "llvm.dbg.global_variable");
Chris Lattnera45664f2008-11-10 02:56:27 +0000790 GV->setSection("llvm.metadata");
791 return DIGlobalVariable(GV);
792}
793
794
795/// CreateVariable - Create a new descriptor for the specified variable.
796DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
797 const std::string &Name,
798 DICompileUnit CompileUnit, unsigned LineNo,
Devang Pateldd9db662009-01-30 18:20:31 +0000799 DIType Type) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000800 Constant *Elts[] = {
801 GetTagConstant(Tag),
Chris Lattner497a7a82008-11-10 04:10:34 +0000802 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000803 GetStringConstant(Name),
Chris Lattner497a7a82008-11-10 04:10:34 +0000804 getCastToEmpty(CompileUnit),
Owen Andersoneed707b2009-07-24 23:12:02 +0000805 ConstantInt::get(Type::Int32Ty, LineNo),
Devang Pateldd9db662009-01-30 18:20:31 +0000806 getCastToEmpty(Type)
Chris Lattnera45664f2008-11-10 02:56:27 +0000807 };
808
Owen Anderson8fa33382009-07-27 22:29:26 +0000809 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000810
811 M.addTypeName("llvm.dbg.variable.type", Init->getType());
Owen Andersone9b11b42009-07-08 19:03:57 +0000812 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
Chris Lattnera45664f2008-11-10 02:56:27 +0000813 GlobalValue::InternalLinkage,
Owen Andersone9b11b42009-07-08 19:03:57 +0000814 Init, "llvm.dbg.variable");
Chris Lattnera45664f2008-11-10 02:56:27 +0000815 GV->setSection("llvm.metadata");
816 return DIVariable(GV);
817}
818
819
820/// CreateBlock - This creates a descriptor for a lexical block with the
Owen Anderson99035272009-07-07 17:12:53 +0000821/// specified parent VMContext.
Chris Lattnera45664f2008-11-10 02:56:27 +0000822DIBlock DIFactory::CreateBlock(DIDescriptor Context) {
823 Constant *Elts[] = {
824 GetTagConstant(dwarf::DW_TAG_lexical_block),
Chris Lattner497a7a82008-11-10 04:10:34 +0000825 getCastToEmpty(Context)
Chris Lattnera45664f2008-11-10 02:56:27 +0000826 };
827
Owen Anderson8fa33382009-07-27 22:29:26 +0000828 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000829
830 M.addTypeName("llvm.dbg.block.type", Init->getType());
Owen Andersone9b11b42009-07-08 19:03:57 +0000831 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
Chris Lattnera45664f2008-11-10 02:56:27 +0000832 GlobalValue::InternalLinkage,
Owen Andersone9b11b42009-07-08 19:03:57 +0000833 Init, "llvm.dbg.block");
Chris Lattnera45664f2008-11-10 02:56:27 +0000834 GV->setSection("llvm.metadata");
835 return DIBlock(GV);
836}
837
838
839//===----------------------------------------------------------------------===//
840// DIFactory: Routines for inserting code into a function
841//===----------------------------------------------------------------------===//
842
843/// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
844/// inserting it at the end of the specified basic block.
845void DIFactory::InsertStopPoint(DICompileUnit CU, unsigned LineNo,
846 unsigned ColNo, BasicBlock *BB) {
847
848 // Lazily construct llvm.dbg.stoppoint function.
849 if (!StopPointFn)
850 StopPointFn = llvm::Intrinsic::getDeclaration(&M,
851 llvm::Intrinsic::dbg_stoppoint);
852
853 // Invoke llvm.dbg.stoppoint
854 Value *Args[] = {
Owen Andersoneed707b2009-07-24 23:12:02 +0000855 ConstantInt::get(llvm::Type::Int32Ty, LineNo),
856 ConstantInt::get(llvm::Type::Int32Ty, ColNo),
Chris Lattner497a7a82008-11-10 04:10:34 +0000857 getCastToEmpty(CU)
Chris Lattnera45664f2008-11-10 02:56:27 +0000858 };
859 CallInst::Create(StopPointFn, Args, Args+3, "", BB);
860}
861
862/// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
863/// mark the start of the specified subprogram.
864void DIFactory::InsertSubprogramStart(DISubprogram SP, BasicBlock *BB) {
865 // Lazily construct llvm.dbg.func.start.
866 if (!FuncStartFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000867 FuncStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_func_start);
Chris Lattnera45664f2008-11-10 02:56:27 +0000868
869 // Call llvm.dbg.func.start which also implicitly sets a stoppoint.
Chris Lattner497a7a82008-11-10 04:10:34 +0000870 CallInst::Create(FuncStartFn, getCastToEmpty(SP), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000871}
872
873/// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
874/// mark the start of a region for the specified scoping descriptor.
875void DIFactory::InsertRegionStart(DIDescriptor D, BasicBlock *BB) {
876 // Lazily construct llvm.dbg.region.start function.
877 if (!RegionStartFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000878 RegionStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_start);
879
Chris Lattnera45664f2008-11-10 02:56:27 +0000880 // Call llvm.dbg.func.start.
Chris Lattner497a7a82008-11-10 04:10:34 +0000881 CallInst::Create(RegionStartFn, getCastToEmpty(D), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000882}
883
Chris Lattnera45664f2008-11-10 02:56:27 +0000884/// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
885/// mark the end of a region for the specified scoping descriptor.
886void DIFactory::InsertRegionEnd(DIDescriptor D, BasicBlock *BB) {
887 // Lazily construct llvm.dbg.region.end function.
888 if (!RegionEndFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000889 RegionEndFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_end);
890
891 // Call llvm.dbg.region.end.
Chris Lattner497a7a82008-11-10 04:10:34 +0000892 CallInst::Create(RegionEndFn, getCastToEmpty(D), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000893}
894
895/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Bill Wendlingdc817b62009-05-14 18:26:15 +0000896void DIFactory::InsertDeclare(Value *Storage, DIVariable D, BasicBlock *BB) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000897 // Cast the storage to a {}* for the call to llvm.dbg.declare.
Bill Wendlingdc817b62009-05-14 18:26:15 +0000898 Storage = new BitCastInst(Storage, EmptyStructPtr, "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000899
900 if (!DeclareFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000901 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
902
Chris Lattner497a7a82008-11-10 04:10:34 +0000903 Value *Args[] = { Storage, getCastToEmpty(D) };
Chris Lattnera45664f2008-11-10 02:56:27 +0000904 CallInst::Create(DeclareFn, Args, Args+2, "", BB);
905}
Torok Edwin620f2802008-12-16 09:07:36 +0000906
907namespace llvm {
Bill Wendlingdc817b62009-05-14 18:26:15 +0000908 /// findStopPoint - Find the stoppoint coressponding to this instruction, that
909 /// is the stoppoint that dominates this instruction.
910 const DbgStopPointInst *findStopPoint(const Instruction *Inst) {
Torok Edwin620f2802008-12-16 09:07:36 +0000911 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(Inst))
912 return DSI;
913
914 const BasicBlock *BB = Inst->getParent();
915 BasicBlock::const_iterator I = Inst, B;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000916 while (BB) {
Torok Edwin620f2802008-12-16 09:07:36 +0000917 B = BB->begin();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000918
Torok Edwin620f2802008-12-16 09:07:36 +0000919 // A BB consisting only of a terminator can't have a stoppoint.
Bill Wendlingdc817b62009-05-14 18:26:15 +0000920 while (I != B) {
921 --I;
922 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
923 return DSI;
Torok Edwin620f2802008-12-16 09:07:36 +0000924 }
Bill Wendlingdc817b62009-05-14 18:26:15 +0000925
926 // This BB didn't have a stoppoint: if there is only one predecessor, look
927 // for a stoppoint there. We could use getIDom(), but that would require
928 // dominator info.
Torok Edwin620f2802008-12-16 09:07:36 +0000929 BB = I->getParent()->getUniquePredecessor();
930 if (BB)
931 I = BB->getTerminator();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000932 }
933
Torok Edwin620f2802008-12-16 09:07:36 +0000934 return 0;
935 }
936
Bill Wendlingdc817b62009-05-14 18:26:15 +0000937 /// findBBStopPoint - Find the stoppoint corresponding to first real
938 /// (non-debug intrinsic) instruction in this Basic Block, and return the
939 /// stoppoint for it.
940 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB) {
941 for(BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +0000942 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
943 return DSI;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000944
945 // Fallback to looking for stoppoint of unique predecessor. Useful if this
946 // BB contains no stoppoints, but unique predecessor does.
Torok Edwin620f2802008-12-16 09:07:36 +0000947 BB = BB->getUniquePredecessor();
948 if (BB)
949 return findStopPoint(BB->getTerminator());
Bill Wendlingdc817b62009-05-14 18:26:15 +0000950
Torok Edwin620f2802008-12-16 09:07:36 +0000951 return 0;
952 }
953
Bill Wendlingdc817b62009-05-14 18:26:15 +0000954 Value *findDbgGlobalDeclare(GlobalVariable *V) {
Torok Edwinff7d0e92009-03-10 13:41:26 +0000955 const Module *M = V->getParent();
Owen Anderson99035272009-07-07 17:12:53 +0000956 LLVMContext& Context = M->getContext();
957
Torok Edwinff7d0e92009-03-10 13:41:26 +0000958 const Type *Ty = M->getTypeByName("llvm.dbg.global_variable.type");
Bill Wendlingdc817b62009-05-14 18:26:15 +0000959 if (!Ty) return 0;
960
Owen Anderson99035272009-07-07 17:12:53 +0000961 Ty = Context.getPointerType(Ty, 0);
Torok Edwinff7d0e92009-03-10 13:41:26 +0000962
963 Value *Val = V->stripPointerCasts();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000964 for (Value::use_iterator I = Val->use_begin(), E = Val->use_end();
Torok Edwinff7d0e92009-03-10 13:41:26 +0000965 I != E; ++I) {
966 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(I)) {
967 if (CE->getOpcode() == Instruction::BitCast) {
968 Value *VV = CE;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000969
970 while (VV->hasOneUse())
Torok Edwinff7d0e92009-03-10 13:41:26 +0000971 VV = *VV->use_begin();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000972
Torok Edwinff7d0e92009-03-10 13:41:26 +0000973 if (VV->getType() == Ty)
974 return VV;
975 }
976 }
977 }
978
979 if (Val->getType() == Ty)
980 return Val;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000981
Torok Edwinff7d0e92009-03-10 13:41:26 +0000982 return 0;
983 }
984
Bill Wendlingdc817b62009-05-14 18:26:15 +0000985 /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
Torok Edwin620f2802008-12-16 09:07:36 +0000986 /// It looks through pointer casts too.
Bill Wendlingdc817b62009-05-14 18:26:15 +0000987 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts) {
Torok Edwin620f2802008-12-16 09:07:36 +0000988 if (stripCasts) {
989 V = V->stripPointerCasts();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000990
Torok Edwin620f2802008-12-16 09:07:36 +0000991 // Look for the bitcast.
992 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000993 I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +0000994 if (isa<BitCastInst>(I))
995 return findDbgDeclare(*I, false);
Bill Wendlingdc817b62009-05-14 18:26:15 +0000996
Torok Edwin620f2802008-12-16 09:07:36 +0000997 return 0;
998 }
999
Bill Wendlingdc817b62009-05-14 18:26:15 +00001000 // Find llvm.dbg.declare among uses of the instruction.
Torok Edwin620f2802008-12-16 09:07:36 +00001001 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001002 I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001003 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I))
1004 return DDI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001005
Torok Edwin620f2802008-12-16 09:07:36 +00001006 return 0;
1007 }
Torok Edwinff7d0e92009-03-10 13:41:26 +00001008
Bill Wendlingdc817b62009-05-14 18:26:15 +00001009 bool getLocationInfo(const Value *V, std::string &DisplayName,
1010 std::string &Type, unsigned &LineNo, std::string &File,
1011 std::string &Dir) {
Torok Edwinff7d0e92009-03-10 13:41:26 +00001012 DICompileUnit Unit;
1013 DIType TypeD;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001014
Torok Edwinff7d0e92009-03-10 13:41:26 +00001015 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1016 Value *DIGV = findDbgGlobalDeclare(GV);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001017 if (!DIGV) return false;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001018 DIGlobalVariable Var(cast<GlobalVariable>(DIGV));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001019
Bill Wendling0582ae92009-03-13 04:39:26 +00001020 Var.getDisplayName(DisplayName);
Torok Edwinff7d0e92009-03-10 13:41:26 +00001021 LineNo = Var.getLineNumber();
1022 Unit = Var.getCompileUnit();
1023 TypeD = Var.getType();
1024 } else {
1025 const DbgDeclareInst *DDI = findDbgDeclare(V);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001026 if (!DDI) return false;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001027 DIVariable Var(cast<GlobalVariable>(DDI->getVariable()));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001028
Bill Wendling0582ae92009-03-13 04:39:26 +00001029 Var.getName(DisplayName);
Torok Edwinff7d0e92009-03-10 13:41:26 +00001030 LineNo = Var.getLineNumber();
1031 Unit = Var.getCompileUnit();
1032 TypeD = Var.getType();
1033 }
Bill Wendlingdc817b62009-05-14 18:26:15 +00001034
Bill Wendling0582ae92009-03-13 04:39:26 +00001035 TypeD.getName(Type);
1036 Unit.getFilename(File);
1037 Unit.getDirectory(Dir);
Torok Edwinff7d0e92009-03-10 13:41:26 +00001038 return true;
1039 }
Devang Patel13e16b62009-06-26 01:49:18 +00001040
1041 /// CollectDebugInfoAnchors - Collect debugging information anchors.
1042 void CollectDebugInfoAnchors(Module &M,
1043 SmallVector<GlobalVariable *, 2> &CUs,
1044 SmallVector<GlobalVariable *, 4> &GVs,
1045 SmallVector<GlobalVariable *, 4> &SPs) {
1046
1047 for (Module::global_iterator GVI = M.global_begin(), E = M.global_end();
1048 GVI != E; GVI++) {
1049 GlobalVariable *GV = GVI;
Daniel Dunbar460f6562009-07-26 09:48:23 +00001050 if (GV->hasName() && GV->getName().startswith("llvm.dbg")
Devang Patel13e16b62009-06-26 01:49:18 +00001051 && GV->isConstant() && GV->hasInitializer()) {
1052 DICompileUnit C(GV);
1053 if (C.isNull() == false) {
1054 CUs.push_back(GV);
1055 continue;
1056 }
1057 DIGlobalVariable G(GV);
1058 if (G.isNull() == false) {
1059 GVs.push_back(GV);
1060 continue;
1061 }
1062 DISubprogram S(GV);
1063 if (S.isNull() == false) {
1064 SPs.push_back(GV);
1065 continue;
1066 }
1067 }
1068 }
1069 }
Devang Patel9e529c32009-07-02 01:15:24 +00001070
1071 /// isValidDebugInfoIntrinsic - Return true if SPI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001072 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001073 bool isValidDebugInfoIntrinsic(DbgStopPointInst &SPI,
1074 CodeGenOpt::Level OptLev) {
1075 return DIDescriptor::ValidDebugInfo(SPI.getContext(), OptLev);
1076 }
1077
1078 /// isValidDebugInfoIntrinsic - Return true if FSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001079 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001080 bool isValidDebugInfoIntrinsic(DbgFuncStartInst &FSI,
1081 CodeGenOpt::Level OptLev) {
1082 return DIDescriptor::ValidDebugInfo(FSI.getSubprogram(), OptLev);
1083 }
1084
1085 /// isValidDebugInfoIntrinsic - Return true if RSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001086 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001087 bool isValidDebugInfoIntrinsic(DbgRegionStartInst &RSI,
1088 CodeGenOpt::Level OptLev) {
1089 return DIDescriptor::ValidDebugInfo(RSI.getContext(), OptLev);
1090 }
1091
1092 /// isValidDebugInfoIntrinsic - Return true if REI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001093 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001094 bool isValidDebugInfoIntrinsic(DbgRegionEndInst &REI,
1095 CodeGenOpt::Level OptLev) {
1096 return DIDescriptor::ValidDebugInfo(REI.getContext(), OptLev);
1097 }
1098
1099
1100 /// isValidDebugInfoIntrinsic - Return true if DI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001101 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001102 bool isValidDebugInfoIntrinsic(DbgDeclareInst &DI,
1103 CodeGenOpt::Level OptLev) {
1104 return DIDescriptor::ValidDebugInfo(DI.getVariable(), OptLev);
1105 }
1106
1107 /// ExtractDebugLocation - Extract debug location information
1108 /// from llvm.dbg.stoppoint intrinsic.
1109 DebugLoc ExtractDebugLocation(DbgStopPointInst &SPI,
Devang Patel9e529c32009-07-02 01:15:24 +00001110 DebugLocTracker &DebugLocInfo) {
1111 DebugLoc DL;
1112 Value *Context = SPI.getContext();
Devang Patel9e529c32009-07-02 01:15:24 +00001113
1114 // If this location is already tracked then use it.
1115 DebugLocTuple Tuple(cast<GlobalVariable>(Context), SPI.getLine(),
1116 SPI.getColumn());
1117 DenseMap<DebugLocTuple, unsigned>::iterator II
1118 = DebugLocInfo.DebugIdMap.find(Tuple);
1119 if (II != DebugLocInfo.DebugIdMap.end())
1120 return DebugLoc::get(II->second);
1121
1122 // Add a new location entry.
1123 unsigned Id = DebugLocInfo.DebugLocations.size();
1124 DebugLocInfo.DebugLocations.push_back(Tuple);
1125 DebugLocInfo.DebugIdMap[Tuple] = Id;
1126
1127 return DebugLoc::get(Id);
1128 }
1129
1130 /// ExtractDebugLocation - Extract debug location information
1131 /// from llvm.dbg.func_start intrinsic.
1132 DebugLoc ExtractDebugLocation(DbgFuncStartInst &FSI,
Devang Patel9e529c32009-07-02 01:15:24 +00001133 DebugLocTracker &DebugLocInfo) {
1134 DebugLoc DL;
1135 Value *SP = FSI.getSubprogram();
Devang Patel9e529c32009-07-02 01:15:24 +00001136
1137 DISubprogram Subprogram(cast<GlobalVariable>(SP));
1138 unsigned Line = Subprogram.getLineNumber();
1139 DICompileUnit CU(Subprogram.getCompileUnit());
1140
1141 // If this location is already tracked then use it.
1142 DebugLocTuple Tuple(CU.getGV(), Line, /* Column */ 0);
1143 DenseMap<DebugLocTuple, unsigned>::iterator II
1144 = DebugLocInfo.DebugIdMap.find(Tuple);
1145 if (II != DebugLocInfo.DebugIdMap.end())
1146 return DebugLoc::get(II->second);
1147
1148 // Add a new location entry.
1149 unsigned Id = DebugLocInfo.DebugLocations.size();
1150 DebugLocInfo.DebugLocations.push_back(Tuple);
1151 DebugLocInfo.DebugIdMap[Tuple] = Id;
1152
1153 return DebugLoc::get(Id);
1154 }
1155
1156 /// isInlinedFnStart - Return true if FSI is starting an inlined function.
1157 bool isInlinedFnStart(DbgFuncStartInst &FSI, const Function *CurrentFn) {
1158 DISubprogram Subprogram(cast<GlobalVariable>(FSI.getSubprogram()));
1159 if (Subprogram.describes(CurrentFn))
1160 return false;
1161
1162 return true;
1163 }
1164
1165 /// isInlinedFnEnd - Return true if REI is ending an inlined function.
1166 bool isInlinedFnEnd(DbgRegionEndInst &REI, const Function *CurrentFn) {
1167 DISubprogram Subprogram(cast<GlobalVariable>(REI.getContext()));
1168 if (Subprogram.isNull() || Subprogram.describes(CurrentFn))
1169 return false;
1170
1171 return true;
1172 }
1173
Torok Edwin620f2802008-12-16 09:07:36 +00001174}