blob: 61c9a4af3e1521d04530f2e5e2dc821dbea5d736 [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 Anderson99035272009-07-07 17:12:53 +0000548 Constant *Init = VMContext.getConstantStruct(Elts,
549 sizeof(Elts)/sizeof(Elts[0]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000550
551 // If we already have this range, just return the uniqued version.
552 DIDescriptor &Entry = SimpleConstantCache[Init];
553 if (!Entry.isNull()) return DISubrange(Entry.getGV());
554
555 M.addTypeName("llvm.dbg.subrange.type", Init->getType());
556
Owen Andersone9b11b42009-07-08 19:03:57 +0000557 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
Chris Lattnera45664f2008-11-10 02:56:27 +0000558 GlobalValue::InternalLinkage,
Owen Andersone9b11b42009-07-08 19:03:57 +0000559 Init, "llvm.dbg.subrange");
Chris Lattnera45664f2008-11-10 02:56:27 +0000560 GV->setSection("llvm.metadata");
561 Entry = DIDescriptor(GV);
562 return DISubrange(GV);
563}
564
565
566
567/// CreateCompileUnit - Create a new descriptor for the specified compile
568/// unit. Note that this does not unique compile units within the module.
569DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
570 const std::string &Filename,
571 const std::string &Directory,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000572 const std::string &Producer,
Devang Pateldd9db662009-01-30 18:20:31 +0000573 bool isMain,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000574 bool isOptimized,
Devang Patel13319ce2009-02-17 22:43:44 +0000575 const char *Flags,
576 unsigned RunTimeVer) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000577 Constant *Elts[] = {
578 GetTagConstant(dwarf::DW_TAG_compile_unit),
Owen Anderson99035272009-07-07 17:12:53 +0000579 VMContext.getNullValue(EmptyStructPtr),
Owen Andersoneed707b2009-07-24 23:12:02 +0000580 ConstantInt::get(Type::Int32Ty, LangID),
Chris Lattnera45664f2008-11-10 02:56:27 +0000581 GetStringConstant(Filename),
582 GetStringConstant(Directory),
Devang Patel3b64c6b2009-01-23 22:33:47 +0000583 GetStringConstant(Producer),
Owen Andersoneed707b2009-07-24 23:12:02 +0000584 ConstantInt::get(Type::Int1Ty, isMain),
585 ConstantInt::get(Type::Int1Ty, isOptimized),
Devang Patel13319ce2009-02-17 22:43:44 +0000586 GetStringConstant(Flags),
Owen Andersoneed707b2009-07-24 23:12:02 +0000587 ConstantInt::get(Type::Int32Ty, RunTimeVer)
Chris Lattnera45664f2008-11-10 02:56:27 +0000588 };
589
Owen Anderson99035272009-07-07 17:12:53 +0000590 Constant *Init = VMContext.getConstantStruct(Elts,
591 sizeof(Elts)/sizeof(Elts[0]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000592
593 M.addTypeName("llvm.dbg.compile_unit.type", Init->getType());
Owen Andersone9b11b42009-07-08 19:03:57 +0000594 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
Devang Patel13e16b62009-06-26 01:49:18 +0000595 GlobalValue::LinkOnceAnyLinkage,
Owen Andersone9b11b42009-07-08 19:03:57 +0000596 Init, "llvm.dbg.compile_unit");
Chris Lattnera45664f2008-11-10 02:56:27 +0000597 GV->setSection("llvm.metadata");
598 return DICompileUnit(GV);
599}
600
601/// CreateEnumerator - Create a single enumerator value.
602DIEnumerator DIFactory::CreateEnumerator(const std::string &Name, uint64_t Val){
603 Constant *Elts[] = {
604 GetTagConstant(dwarf::DW_TAG_enumerator),
605 GetStringConstant(Name),
Owen Andersoneed707b2009-07-24 23:12:02 +0000606 ConstantInt::get(Type::Int64Ty, Val)
Chris Lattnera45664f2008-11-10 02:56:27 +0000607 };
608
Owen Anderson99035272009-07-07 17:12:53 +0000609 Constant *Init = VMContext.getConstantStruct(Elts,
610 sizeof(Elts)/sizeof(Elts[0]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000611
612 M.addTypeName("llvm.dbg.enumerator.type", Init->getType());
Owen Andersone9b11b42009-07-08 19:03:57 +0000613 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
Chris Lattnera45664f2008-11-10 02:56:27 +0000614 GlobalValue::InternalLinkage,
Owen Andersone9b11b42009-07-08 19:03:57 +0000615 Init, "llvm.dbg.enumerator");
Chris Lattnera45664f2008-11-10 02:56:27 +0000616 GV->setSection("llvm.metadata");
617 return DIEnumerator(GV);
618}
619
620
621/// CreateBasicType - Create a basic type like int, float, etc.
622DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
Bill Wendling0582ae92009-03-13 04:39:26 +0000623 const std::string &Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000624 DICompileUnit CompileUnit,
625 unsigned LineNumber,
626 uint64_t SizeInBits,
627 uint64_t AlignInBits,
628 uint64_t OffsetInBits, unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000629 unsigned Encoding) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000630 Constant *Elts[] = {
631 GetTagConstant(dwarf::DW_TAG_base_type),
Chris Lattner497a7a82008-11-10 04:10:34 +0000632 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000633 GetStringConstant(Name),
Chris Lattner497a7a82008-11-10 04:10:34 +0000634 getCastToEmpty(CompileUnit),
Owen Andersoneed707b2009-07-24 23:12:02 +0000635 ConstantInt::get(Type::Int32Ty, LineNumber),
636 ConstantInt::get(Type::Int64Ty, SizeInBits),
637 ConstantInt::get(Type::Int64Ty, AlignInBits),
638 ConstantInt::get(Type::Int64Ty, OffsetInBits),
639 ConstantInt::get(Type::Int32Ty, Flags),
640 ConstantInt::get(Type::Int32Ty, Encoding)
Chris Lattnera45664f2008-11-10 02:56:27 +0000641 };
642
Owen Anderson99035272009-07-07 17:12:53 +0000643 Constant *Init = VMContext.getConstantStruct(Elts,
644 sizeof(Elts)/sizeof(Elts[0]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000645
646 M.addTypeName("llvm.dbg.basictype.type", Init->getType());
Owen Andersone9b11b42009-07-08 19:03:57 +0000647 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
Chris Lattnera45664f2008-11-10 02:56:27 +0000648 GlobalValue::InternalLinkage,
Owen Andersone9b11b42009-07-08 19:03:57 +0000649 Init, "llvm.dbg.basictype");
Chris Lattnera45664f2008-11-10 02:56:27 +0000650 GV->setSection("llvm.metadata");
651 return DIBasicType(GV);
652}
653
654/// CreateDerivedType - Create a derived type like const qualified type,
655/// pointer, typedef, etc.
656DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
657 DIDescriptor Context,
658 const std::string &Name,
659 DICompileUnit CompileUnit,
660 unsigned LineNumber,
661 uint64_t SizeInBits,
662 uint64_t AlignInBits,
663 uint64_t OffsetInBits,
664 unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000665 DIType DerivedFrom) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000666 Constant *Elts[] = {
667 GetTagConstant(Tag),
Chris Lattner497a7a82008-11-10 04:10:34 +0000668 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000669 GetStringConstant(Name),
Chris Lattner497a7a82008-11-10 04:10:34 +0000670 getCastToEmpty(CompileUnit),
Owen Andersoneed707b2009-07-24 23:12:02 +0000671 ConstantInt::get(Type::Int32Ty, LineNumber),
672 ConstantInt::get(Type::Int64Ty, SizeInBits),
673 ConstantInt::get(Type::Int64Ty, AlignInBits),
674 ConstantInt::get(Type::Int64Ty, OffsetInBits),
675 ConstantInt::get(Type::Int32Ty, Flags),
Devang Pateldd9db662009-01-30 18:20:31 +0000676 getCastToEmpty(DerivedFrom)
Chris Lattnera45664f2008-11-10 02:56:27 +0000677 };
678
Owen Anderson99035272009-07-07 17:12:53 +0000679 Constant *Init = VMContext.getConstantStruct(Elts,
680 sizeof(Elts)/sizeof(Elts[0]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000681
682 M.addTypeName("llvm.dbg.derivedtype.type", Init->getType());
Owen Andersone9b11b42009-07-08 19:03:57 +0000683 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
Chris Lattnera45664f2008-11-10 02:56:27 +0000684 GlobalValue::InternalLinkage,
Owen Andersone9b11b42009-07-08 19:03:57 +0000685 Init, "llvm.dbg.derivedtype");
Chris Lattnera45664f2008-11-10 02:56:27 +0000686 GV->setSection("llvm.metadata");
687 return DIDerivedType(GV);
688}
689
690/// CreateCompositeType - Create a composite type like array, struct, etc.
691DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
692 DIDescriptor Context,
693 const std::string &Name,
694 DICompileUnit CompileUnit,
695 unsigned LineNumber,
696 uint64_t SizeInBits,
697 uint64_t AlignInBits,
698 uint64_t OffsetInBits,
699 unsigned Flags,
700 DIType DerivedFrom,
Devang Patel13319ce2009-02-17 22:43:44 +0000701 DIArray Elements,
702 unsigned RuntimeLang) {
Owen Andersone277fed2009-07-07 16:31:25 +0000703
Chris Lattnera45664f2008-11-10 02:56:27 +0000704 Constant *Elts[] = {
705 GetTagConstant(Tag),
Chris Lattner497a7a82008-11-10 04:10:34 +0000706 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000707 GetStringConstant(Name),
Chris Lattner497a7a82008-11-10 04:10:34 +0000708 getCastToEmpty(CompileUnit),
Owen Andersoneed707b2009-07-24 23:12:02 +0000709 ConstantInt::get(Type::Int32Ty, LineNumber),
710 ConstantInt::get(Type::Int64Ty, SizeInBits),
711 ConstantInt::get(Type::Int64Ty, AlignInBits),
712 ConstantInt::get(Type::Int64Ty, OffsetInBits),
713 ConstantInt::get(Type::Int32Ty, Flags),
Chris Lattner497a7a82008-11-10 04:10:34 +0000714 getCastToEmpty(DerivedFrom),
Devang Patel13319ce2009-02-17 22:43:44 +0000715 getCastToEmpty(Elements),
Owen Andersoneed707b2009-07-24 23:12:02 +0000716 ConstantInt::get(Type::Int32Ty, RuntimeLang)
Chris Lattnera45664f2008-11-10 02:56:27 +0000717 };
718
Owen Anderson99035272009-07-07 17:12:53 +0000719 Constant *Init = VMContext.getConstantStruct(Elts,
720 sizeof(Elts)/sizeof(Elts[0]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000721
722 M.addTypeName("llvm.dbg.composite.type", Init->getType());
Owen Andersone9b11b42009-07-08 19:03:57 +0000723 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
Chris Lattnera45664f2008-11-10 02:56:27 +0000724 GlobalValue::InternalLinkage,
Owen Andersone9b11b42009-07-08 19:03:57 +0000725 Init, "llvm.dbg.composite");
Chris Lattnera45664f2008-11-10 02:56:27 +0000726 GV->setSection("llvm.metadata");
727 return DICompositeType(GV);
728}
729
730
731/// CreateSubprogram - Create a new descriptor for the specified subprogram.
732/// See comments in DISubprogram for descriptions of these fields. This
733/// method does not unique the generated descriptors.
734DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
735 const std::string &Name,
736 const std::string &DisplayName,
737 const std::string &LinkageName,
738 DICompileUnit CompileUnit,
739 unsigned LineNo, DIType Type,
740 bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000741 bool isDefinition) {
Devang Patel854967e2008-12-17 22:39:29 +0000742
Chris Lattnera45664f2008-11-10 02:56:27 +0000743 Constant *Elts[] = {
744 GetTagConstant(dwarf::DW_TAG_subprogram),
Owen Anderson99035272009-07-07 17:12:53 +0000745 VMContext.getNullValue(EmptyStructPtr),
Chris Lattner497a7a82008-11-10 04:10:34 +0000746 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000747 GetStringConstant(Name),
748 GetStringConstant(DisplayName),
749 GetStringConstant(LinkageName),
Chris Lattner497a7a82008-11-10 04:10:34 +0000750 getCastToEmpty(CompileUnit),
Owen Andersoneed707b2009-07-24 23:12:02 +0000751 ConstantInt::get(Type::Int32Ty, LineNo),
Chris Lattner497a7a82008-11-10 04:10:34 +0000752 getCastToEmpty(Type),
Owen Andersoneed707b2009-07-24 23:12:02 +0000753 ConstantInt::get(Type::Int1Ty, isLocalToUnit),
754 ConstantInt::get(Type::Int1Ty, isDefinition)
Chris Lattnera45664f2008-11-10 02:56:27 +0000755 };
756
Owen Anderson99035272009-07-07 17:12:53 +0000757 Constant *Init = VMContext.getConstantStruct(Elts,
758 sizeof(Elts)/sizeof(Elts[0]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000759
760 M.addTypeName("llvm.dbg.subprogram.type", Init->getType());
Owen Andersone9b11b42009-07-08 19:03:57 +0000761 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
Devang Patel13e16b62009-06-26 01:49:18 +0000762 GlobalValue::LinkOnceAnyLinkage,
Owen Andersone9b11b42009-07-08 19:03:57 +0000763 Init, "llvm.dbg.subprogram");
Chris Lattnera45664f2008-11-10 02:56:27 +0000764 GV->setSection("llvm.metadata");
765 return DISubprogram(GV);
766}
767
768/// CreateGlobalVariable - Create a new descriptor for the specified global.
769DIGlobalVariable
770DIFactory::CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
771 const std::string &DisplayName,
772 const std::string &LinkageName,
773 DICompileUnit CompileUnit,
774 unsigned LineNo, DIType Type,bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000775 bool isDefinition, llvm::GlobalVariable *Val) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000776 Constant *Elts[] = {
777 GetTagConstant(dwarf::DW_TAG_variable),
Owen Anderson99035272009-07-07 17:12:53 +0000778 VMContext.getNullValue(EmptyStructPtr),
Chris Lattner497a7a82008-11-10 04:10:34 +0000779 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000780 GetStringConstant(Name),
781 GetStringConstant(DisplayName),
782 GetStringConstant(LinkageName),
Chris Lattner497a7a82008-11-10 04:10:34 +0000783 getCastToEmpty(CompileUnit),
Owen Andersoneed707b2009-07-24 23:12:02 +0000784 ConstantInt::get(Type::Int32Ty, LineNo),
Chris Lattner497a7a82008-11-10 04:10:34 +0000785 getCastToEmpty(Type),
Owen Andersoneed707b2009-07-24 23:12:02 +0000786 ConstantInt::get(Type::Int1Ty, isLocalToUnit),
787 ConstantInt::get(Type::Int1Ty, isDefinition),
Owen Anderson99035272009-07-07 17:12:53 +0000788 VMContext.getConstantExprBitCast(Val, EmptyStructPtr)
Chris Lattnera45664f2008-11-10 02:56:27 +0000789 };
790
Owen Anderson99035272009-07-07 17:12:53 +0000791 Constant *Init = VMContext.getConstantStruct(Elts,
792 sizeof(Elts)/sizeof(Elts[0]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000793
794 M.addTypeName("llvm.dbg.global_variable.type", Init->getType());
Owen Andersone9b11b42009-07-08 19:03:57 +0000795 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
Devang Patel13e16b62009-06-26 01:49:18 +0000796 GlobalValue::LinkOnceAnyLinkage,
Owen Andersone9b11b42009-07-08 19:03:57 +0000797 Init, "llvm.dbg.global_variable");
Chris Lattnera45664f2008-11-10 02:56:27 +0000798 GV->setSection("llvm.metadata");
799 return DIGlobalVariable(GV);
800}
801
802
803/// CreateVariable - Create a new descriptor for the specified variable.
804DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
805 const std::string &Name,
806 DICompileUnit CompileUnit, unsigned LineNo,
Devang Pateldd9db662009-01-30 18:20:31 +0000807 DIType Type) {
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 Andersoneed707b2009-07-24 23:12:02 +0000813 ConstantInt::get(Type::Int32Ty, LineNo),
Devang Pateldd9db662009-01-30 18:20:31 +0000814 getCastToEmpty(Type)
Chris Lattnera45664f2008-11-10 02:56:27 +0000815 };
816
Owen Anderson99035272009-07-07 17:12:53 +0000817 Constant *Init = VMContext.getConstantStruct(Elts,
818 sizeof(Elts)/sizeof(Elts[0]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000819
820 M.addTypeName("llvm.dbg.variable.type", Init->getType());
Owen Andersone9b11b42009-07-08 19:03:57 +0000821 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
Chris Lattnera45664f2008-11-10 02:56:27 +0000822 GlobalValue::InternalLinkage,
Owen Andersone9b11b42009-07-08 19:03:57 +0000823 Init, "llvm.dbg.variable");
Chris Lattnera45664f2008-11-10 02:56:27 +0000824 GV->setSection("llvm.metadata");
825 return DIVariable(GV);
826}
827
828
829/// CreateBlock - This creates a descriptor for a lexical block with the
Owen Anderson99035272009-07-07 17:12:53 +0000830/// specified parent VMContext.
Chris Lattnera45664f2008-11-10 02:56:27 +0000831DIBlock 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 Anderson99035272009-07-07 17:12:53 +0000837 Constant *Init = VMContext.getConstantStruct(Elts,
838 sizeof(Elts)/sizeof(Elts[0]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000839
840 M.addTypeName("llvm.dbg.block.type", Init->getType());
Owen Andersone9b11b42009-07-08 19:03:57 +0000841 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
Chris Lattnera45664f2008-11-10 02:56:27 +0000842 GlobalValue::InternalLinkage,
Owen Andersone9b11b42009-07-08 19:03:57 +0000843 Init, "llvm.dbg.block");
Chris Lattnera45664f2008-11-10 02:56:27 +0000844 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 Andersoneed707b2009-07-24 23:12:02 +0000865 ConstantInt::get(llvm::Type::Int32Ty, LineNo),
866 ConstantInt::get(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();
Owen Anderson99035272009-07-07 17:12:53 +0000966 LLVMContext& Context = M->getContext();
967
Torok Edwinff7d0e92009-03-10 13:41:26 +0000968 const Type *Ty = M->getTypeByName("llvm.dbg.global_variable.type");
Bill Wendlingdc817b62009-05-14 18:26:15 +0000969 if (!Ty) return 0;
970
Owen Anderson99035272009-07-07 17:12:53 +0000971 Ty = Context.getPointerType(Ty, 0);
Torok Edwinff7d0e92009-03-10 13:41:26 +0000972
973 Value *Val = V->stripPointerCasts();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000974 for (Value::use_iterator I = Val->use_begin(), E = Val->use_end();
Torok Edwinff7d0e92009-03-10 13:41:26 +0000975 I != E; ++I) {
976 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(I)) {
977 if (CE->getOpcode() == Instruction::BitCast) {
978 Value *VV = CE;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000979
980 while (VV->hasOneUse())
Torok Edwinff7d0e92009-03-10 13:41:26 +0000981 VV = *VV->use_begin();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000982
Torok Edwinff7d0e92009-03-10 13:41:26 +0000983 if (VV->getType() == Ty)
984 return VV;
985 }
986 }
987 }
988
989 if (Val->getType() == Ty)
990 return Val;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000991
Torok Edwinff7d0e92009-03-10 13:41:26 +0000992 return 0;
993 }
994
Bill Wendlingdc817b62009-05-14 18:26:15 +0000995 /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
Torok Edwin620f2802008-12-16 09:07:36 +0000996 /// It looks through pointer casts too.
Bill Wendlingdc817b62009-05-14 18:26:15 +0000997 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts) {
Torok Edwin620f2802008-12-16 09:07:36 +0000998 if (stripCasts) {
999 V = V->stripPointerCasts();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001000
Torok Edwin620f2802008-12-16 09:07:36 +00001001 // Look for the bitcast.
1002 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001003 I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001004 if (isa<BitCastInst>(I))
1005 return findDbgDeclare(*I, false);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001006
Torok Edwin620f2802008-12-16 09:07:36 +00001007 return 0;
1008 }
1009
Bill Wendlingdc817b62009-05-14 18:26:15 +00001010 // Find llvm.dbg.declare among uses of the instruction.
Torok Edwin620f2802008-12-16 09:07:36 +00001011 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001012 I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001013 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I))
1014 return DDI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001015
Torok Edwin620f2802008-12-16 09:07:36 +00001016 return 0;
1017 }
Torok Edwinff7d0e92009-03-10 13:41:26 +00001018
Bill Wendlingdc817b62009-05-14 18:26:15 +00001019 bool getLocationInfo(const Value *V, std::string &DisplayName,
1020 std::string &Type, unsigned &LineNo, std::string &File,
1021 std::string &Dir) {
Torok Edwinff7d0e92009-03-10 13:41:26 +00001022 DICompileUnit Unit;
1023 DIType TypeD;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001024
Torok Edwinff7d0e92009-03-10 13:41:26 +00001025 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1026 Value *DIGV = findDbgGlobalDeclare(GV);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001027 if (!DIGV) return false;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001028 DIGlobalVariable Var(cast<GlobalVariable>(DIGV));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001029
Bill Wendling0582ae92009-03-13 04:39:26 +00001030 Var.getDisplayName(DisplayName);
Torok Edwinff7d0e92009-03-10 13:41:26 +00001031 LineNo = Var.getLineNumber();
1032 Unit = Var.getCompileUnit();
1033 TypeD = Var.getType();
1034 } else {
1035 const DbgDeclareInst *DDI = findDbgDeclare(V);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001036 if (!DDI) return false;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001037 DIVariable Var(cast<GlobalVariable>(DDI->getVariable()));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001038
Bill Wendling0582ae92009-03-13 04:39:26 +00001039 Var.getName(DisplayName);
Torok Edwinff7d0e92009-03-10 13:41:26 +00001040 LineNo = Var.getLineNumber();
1041 Unit = Var.getCompileUnit();
1042 TypeD = Var.getType();
1043 }
Bill Wendlingdc817b62009-05-14 18:26:15 +00001044
Bill Wendling0582ae92009-03-13 04:39:26 +00001045 TypeD.getName(Type);
1046 Unit.getFilename(File);
1047 Unit.getDirectory(Dir);
Torok Edwinff7d0e92009-03-10 13:41:26 +00001048 return true;
1049 }
Devang Patel13e16b62009-06-26 01:49:18 +00001050
1051 /// CollectDebugInfoAnchors - Collect debugging information anchors.
1052 void CollectDebugInfoAnchors(Module &M,
1053 SmallVector<GlobalVariable *, 2> &CUs,
1054 SmallVector<GlobalVariable *, 4> &GVs,
1055 SmallVector<GlobalVariable *, 4> &SPs) {
1056
1057 for (Module::global_iterator GVI = M.global_begin(), E = M.global_end();
1058 GVI != E; GVI++) {
1059 GlobalVariable *GV = GVI;
Daniel Dunbar460f6562009-07-26 09:48:23 +00001060 if (GV->hasName() && GV->getName().startswith("llvm.dbg")
Devang Patel13e16b62009-06-26 01:49:18 +00001061 && GV->isConstant() && GV->hasInitializer()) {
1062 DICompileUnit C(GV);
1063 if (C.isNull() == false) {
1064 CUs.push_back(GV);
1065 continue;
1066 }
1067 DIGlobalVariable G(GV);
1068 if (G.isNull() == false) {
1069 GVs.push_back(GV);
1070 continue;
1071 }
1072 DISubprogram S(GV);
1073 if (S.isNull() == false) {
1074 SPs.push_back(GV);
1075 continue;
1076 }
1077 }
1078 }
1079 }
Devang Patel9e529c32009-07-02 01:15:24 +00001080
1081 /// isValidDebugInfoIntrinsic - Return true if SPI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001082 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001083 bool isValidDebugInfoIntrinsic(DbgStopPointInst &SPI,
1084 CodeGenOpt::Level OptLev) {
1085 return DIDescriptor::ValidDebugInfo(SPI.getContext(), OptLev);
1086 }
1087
1088 /// isValidDebugInfoIntrinsic - Return true if FSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001089 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001090 bool isValidDebugInfoIntrinsic(DbgFuncStartInst &FSI,
1091 CodeGenOpt::Level OptLev) {
1092 return DIDescriptor::ValidDebugInfo(FSI.getSubprogram(), OptLev);
1093 }
1094
1095 /// isValidDebugInfoIntrinsic - Return true if RSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001096 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001097 bool isValidDebugInfoIntrinsic(DbgRegionStartInst &RSI,
1098 CodeGenOpt::Level OptLev) {
1099 return DIDescriptor::ValidDebugInfo(RSI.getContext(), OptLev);
1100 }
1101
1102 /// isValidDebugInfoIntrinsic - Return true if REI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001103 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001104 bool isValidDebugInfoIntrinsic(DbgRegionEndInst &REI,
1105 CodeGenOpt::Level OptLev) {
1106 return DIDescriptor::ValidDebugInfo(REI.getContext(), OptLev);
1107 }
1108
1109
1110 /// isValidDebugInfoIntrinsic - Return true if DI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001111 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001112 bool isValidDebugInfoIntrinsic(DbgDeclareInst &DI,
1113 CodeGenOpt::Level OptLev) {
1114 return DIDescriptor::ValidDebugInfo(DI.getVariable(), OptLev);
1115 }
1116
1117 /// ExtractDebugLocation - Extract debug location information
1118 /// from llvm.dbg.stoppoint intrinsic.
1119 DebugLoc ExtractDebugLocation(DbgStopPointInst &SPI,
Devang Patel9e529c32009-07-02 01:15:24 +00001120 DebugLocTracker &DebugLocInfo) {
1121 DebugLoc DL;
1122 Value *Context = SPI.getContext();
Devang Patel9e529c32009-07-02 01:15:24 +00001123
1124 // If this location is already tracked then use it.
1125 DebugLocTuple Tuple(cast<GlobalVariable>(Context), SPI.getLine(),
1126 SPI.getColumn());
1127 DenseMap<DebugLocTuple, unsigned>::iterator II
1128 = DebugLocInfo.DebugIdMap.find(Tuple);
1129 if (II != DebugLocInfo.DebugIdMap.end())
1130 return DebugLoc::get(II->second);
1131
1132 // Add a new location entry.
1133 unsigned Id = DebugLocInfo.DebugLocations.size();
1134 DebugLocInfo.DebugLocations.push_back(Tuple);
1135 DebugLocInfo.DebugIdMap[Tuple] = Id;
1136
1137 return DebugLoc::get(Id);
1138 }
1139
1140 /// ExtractDebugLocation - Extract debug location information
1141 /// from llvm.dbg.func_start intrinsic.
1142 DebugLoc ExtractDebugLocation(DbgFuncStartInst &FSI,
Devang Patel9e529c32009-07-02 01:15:24 +00001143 DebugLocTracker &DebugLocInfo) {
1144 DebugLoc DL;
1145 Value *SP = FSI.getSubprogram();
Devang Patel9e529c32009-07-02 01:15:24 +00001146
1147 DISubprogram Subprogram(cast<GlobalVariable>(SP));
1148 unsigned Line = Subprogram.getLineNumber();
1149 DICompileUnit CU(Subprogram.getCompileUnit());
1150
1151 // If this location is already tracked then use it.
1152 DebugLocTuple Tuple(CU.getGV(), Line, /* Column */ 0);
1153 DenseMap<DebugLocTuple, unsigned>::iterator II
1154 = DebugLocInfo.DebugIdMap.find(Tuple);
1155 if (II != DebugLocInfo.DebugIdMap.end())
1156 return DebugLoc::get(II->second);
1157
1158 // Add a new location entry.
1159 unsigned Id = DebugLocInfo.DebugLocations.size();
1160 DebugLocInfo.DebugLocations.push_back(Tuple);
1161 DebugLocInfo.DebugIdMap[Tuple] = Id;
1162
1163 return DebugLoc::get(Id);
1164 }
1165
1166 /// isInlinedFnStart - Return true if FSI is starting an inlined function.
1167 bool isInlinedFnStart(DbgFuncStartInst &FSI, const Function *CurrentFn) {
1168 DISubprogram Subprogram(cast<GlobalVariable>(FSI.getSubprogram()));
1169 if (Subprogram.describes(CurrentFn))
1170 return false;
1171
1172 return true;
1173 }
1174
1175 /// isInlinedFnEnd - Return true if REI is ending an inlined function.
1176 bool isInlinedFnEnd(DbgRegionEndInst &REI, const Function *CurrentFn) {
1177 DISubprogram Subprogram(cast<GlobalVariable>(REI.getContext()));
1178 if (Subprogram.isNull() || Subprogram.describes(CurrentFn))
1179 return false;
1180
1181 return true;
1182 }
1183
Torok Edwin620f2802008-12-16 09:07:36 +00001184}