blob: 1e2913477def4ae49e4c31154d544390344bf811 [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;
Devang Pateld2f79a12009-07-28 19:55:13 +0000108 if (!DbgGV->hasInitializer()) return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000109
Devang Patel9af2fa82009-06-23 22:25:41 +0000110 Constant *C = DbgGV->getInitializer();
Chris Lattnera45664f2008-11-10 02:56:27 +0000111 if (C == 0 || Elt >= C->getNumOperands())
112 return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000113
Chris Lattnera45664f2008-11-10 02:56:27 +0000114 if (ConstantInt *CI = dyn_cast<ConstantInt>(C->getOperand(Elt)))
115 return CI->getZExtValue();
116 return 0;
117}
118
Chris Lattnera45664f2008-11-10 02:56:27 +0000119DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
Devang Patel9af2fa82009-06-23 22:25:41 +0000120 if (DbgGV == 0) return DIDescriptor();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000121
Devang Patel9af2fa82009-06-23 22:25:41 +0000122 Constant *C = DbgGV->getInitializer();
Chris Lattnera45664f2008-11-10 02:56:27 +0000123 if (C == 0 || Elt >= C->getNumOperands())
124 return DIDescriptor();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000125
Chris Lattnera45664f2008-11-10 02:56:27 +0000126 C = C->getOperand(Elt);
127 return DIDescriptor(dyn_cast<GlobalVariable>(C->stripPointerCasts()));
128}
129
130GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
Devang Patel9af2fa82009-06-23 22:25:41 +0000131 if (DbgGV == 0) return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000132
Devang Patel9af2fa82009-06-23 22:25:41 +0000133 Constant *C = DbgGV->getInitializer();
Chris Lattnera45664f2008-11-10 02:56:27 +0000134 if (C == 0 || Elt >= C->getNumOperands())
135 return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000136
Chris Lattnera45664f2008-11-10 02:56:27 +0000137 C = C->getOperand(Elt);
Chris Lattnera45664f2008-11-10 02:56:27 +0000138 return dyn_cast<GlobalVariable>(C->stripPointerCasts());
139}
140
Chris Lattnera45664f2008-11-10 02:56:27 +0000141//===----------------------------------------------------------------------===//
142// Simple Descriptor Constructors and other Methods
143//===----------------------------------------------------------------------===//
144
Bill Wendlingdc817b62009-05-14 18:26:15 +0000145// Needed by DIVariable::getType().
Devang Patel9af2fa82009-06-23 22:25:41 +0000146DIType::DIType(GlobalVariable *GV) : DIDescriptor(GV) {
147 if (!GV) return;
Torok Edwinb07fbd92008-12-13 08:25:29 +0000148 unsigned tag = getTag();
149 if (tag != dwarf::DW_TAG_base_type && !DIDerivedType::isDerivedType(tag) &&
150 !DICompositeType::isCompositeType(tag))
Devang Patel9af2fa82009-06-23 22:25:41 +0000151 DbgGV = 0;
Torok Edwinb07fbd92008-12-13 08:25:29 +0000152}
Chris Lattnera45664f2008-11-10 02:56:27 +0000153
154/// isDerivedType - Return true if the specified tag is legal for
155/// DIDerivedType.
Devang Patel486938f2009-01-12 21:38:43 +0000156bool DIType::isDerivedType(unsigned Tag) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000157 switch (Tag) {
158 case dwarf::DW_TAG_typedef:
159 case dwarf::DW_TAG_pointer_type:
160 case dwarf::DW_TAG_reference_type:
161 case dwarf::DW_TAG_const_type:
162 case dwarf::DW_TAG_volatile_type:
163 case dwarf::DW_TAG_restrict_type:
164 case dwarf::DW_TAG_member:
165 case dwarf::DW_TAG_inheritance:
166 return true;
167 default:
168 // FIXME: Even though it doesn't make sense, CompositeTypes are current
169 // modelled as DerivedTypes, this should return true for them as well.
170 return false;
171 }
172}
173
Chris Lattnera45664f2008-11-10 02:56:27 +0000174/// isCompositeType - Return true if the specified tag is legal for
175/// DICompositeType.
Devang Patel486938f2009-01-12 21:38:43 +0000176bool DIType::isCompositeType(unsigned TAG) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000177 switch (TAG) {
178 case dwarf::DW_TAG_array_type:
179 case dwarf::DW_TAG_structure_type:
180 case dwarf::DW_TAG_union_type:
181 case dwarf::DW_TAG_enumeration_type:
182 case dwarf::DW_TAG_vector_type:
183 case dwarf::DW_TAG_subroutine_type:
Devang Patel25cb0d72009-03-25 03:52:06 +0000184 case dwarf::DW_TAG_class_type:
Chris Lattnera45664f2008-11-10 02:56:27 +0000185 return true;
186 default:
187 return false;
188 }
189}
190
Chris Lattnera45664f2008-11-10 02:56:27 +0000191/// isVariable - Return true if the specified tag is legal for DIVariable.
192bool DIVariable::isVariable(unsigned Tag) {
193 switch (Tag) {
194 case dwarf::DW_TAG_auto_variable:
195 case dwarf::DW_TAG_arg_variable:
196 case dwarf::DW_TAG_return_variable:
197 return true;
198 default:
199 return false;
200 }
201}
202
Devang Patel68afdc32009-01-05 18:33:01 +0000203unsigned DIArray::getNumElements() const {
Devang Patel9af2fa82009-06-23 22:25:41 +0000204 assert (DbgGV && "Invalid DIArray");
205 Constant *C = DbgGV->getInitializer();
Devang Patel68afdc32009-01-05 18:33:01 +0000206 assert (C && "Invalid DIArray initializer");
207 return C->getNumOperands();
208}
Chris Lattnera45664f2008-11-10 02:56:27 +0000209
Devang Patelc4999d72009-07-22 18:23:44 +0000210/// replaceAllUsesWith - Replace all uses of debug info referenced by
211/// this descriptor. After this completes, the current debug info value
212/// is erased.
213void DIDerivedType::replaceAllUsesWith(DIDescriptor &D) {
214 if (isNull())
215 return;
216
Devang Patel6930f4f2009-07-22 18:56:16 +0000217 assert (!D.isNull() && "Can not replace with null");
Devang Patelc4999d72009-07-22 18:23:44 +0000218 getGV()->replaceAllUsesWith(D.getGV());
219 getGV()->eraseFromParent();
220}
221
Devang Patelb79b5352009-01-19 23:21:49 +0000222/// Verify - Verify that a compile unit is well formed.
223bool DICompileUnit::Verify() const {
224 if (isNull())
225 return false;
Bill Wendling0582ae92009-03-13 04:39:26 +0000226 std::string Res;
227 if (getFilename(Res).empty())
228 return false;
Devang Patelb79b5352009-01-19 23:21:49 +0000229 // It is possible that directory and produce string is empty.
Bill Wendling0582ae92009-03-13 04:39:26 +0000230 return true;
Devang Patelb79b5352009-01-19 23:21:49 +0000231}
232
233/// Verify - Verify that a type descriptor is well formed.
234bool DIType::Verify() const {
235 if (isNull())
236 return false;
237 if (getContext().isNull())
238 return false;
239
240 DICompileUnit CU = getCompileUnit();
241 if (!CU.isNull() && !CU.Verify())
242 return false;
243 return true;
244}
245
246/// Verify - Verify that a composite type descriptor is well formed.
247bool DICompositeType::Verify() const {
248 if (isNull())
249 return false;
250 if (getContext().isNull())
251 return false;
252
253 DICompileUnit CU = getCompileUnit();
254 if (!CU.isNull() && !CU.Verify())
255 return false;
256 return true;
257}
258
259/// Verify - Verify that a subprogram descriptor is well formed.
260bool DISubprogram::Verify() const {
261 if (isNull())
262 return false;
263
264 if (getContext().isNull())
265 return false;
266
267 DICompileUnit CU = getCompileUnit();
268 if (!CU.Verify())
269 return false;
270
271 DICompositeType Ty = getType();
272 if (!Ty.isNull() && !Ty.Verify())
273 return false;
274 return true;
275}
276
277/// Verify - Verify that a global variable descriptor is well formed.
278bool DIGlobalVariable::Verify() const {
279 if (isNull())
280 return false;
281
282 if (getContext().isNull())
283 return false;
284
285 DICompileUnit CU = getCompileUnit();
Chris Lattnere3f6cea2009-05-05 04:55:56 +0000286 if (!CU.isNull() && !CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000287 return false;
288
289 DIType Ty = getType();
290 if (!Ty.Verify())
291 return false;
292
293 if (!getGlobal())
294 return false;
295
296 return true;
297}
298
299/// Verify - Verify that a variable descriptor is well formed.
300bool DIVariable::Verify() const {
301 if (isNull())
302 return false;
303
304 if (getContext().isNull())
305 return false;
306
307 DIType Ty = getType();
308 if (!Ty.Verify())
309 return false;
310
Devang Patelb79b5352009-01-19 23:21:49 +0000311 return true;
312}
313
Devang Patel36375ee2009-02-17 21:23:59 +0000314/// getOriginalTypeSize - If this type is derived from a base type then
315/// return base type size.
316uint64_t DIDerivedType::getOriginalTypeSize() const {
317 if (getTag() != dwarf::DW_TAG_member)
318 return getSizeInBits();
319 DIType BT = getTypeDerivedFrom();
320 if (BT.getTag() != dwarf::DW_TAG_base_type)
321 return getSizeInBits();
322 return BT.getSizeInBits();
323}
Devang Patelb79b5352009-01-19 23:21:49 +0000324
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000325/// describes - Return true if this subprogram provides debugging
326/// information for the function F.
327bool DISubprogram::describes(const Function *F) {
328 assert (F && "Invalid function");
329 std::string Name;
330 getLinkageName(Name);
331 if (Name.empty())
332 getName(Name);
Daniel Dunbar460f6562009-07-26 09:48:23 +0000333 if (F->getName() == Name)
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000334 return true;
335 return false;
336}
337
Chris Lattnera45664f2008-11-10 02:56:27 +0000338//===----------------------------------------------------------------------===//
Devang Patel7136a652009-07-01 22:10:23 +0000339// DIDescriptor: dump routines for all descriptors.
340//===----------------------------------------------------------------------===//
341
342
343/// dump - Print descriptor.
344void DIDescriptor::dump() const {
345 cerr << "[" << dwarf::TagString(getTag()) << "] ";
346 cerr << std::hex << "[GV:" << DbgGV << "]" << std::dec;
347}
348
349/// dump - Print compile unit.
350void DICompileUnit::dump() const {
351 if (getLanguage())
352 cerr << " [" << dwarf::LanguageString(getLanguage()) << "] ";
353
354 std::string Res1, Res2;
355 cerr << " [" << getDirectory(Res1) << "/" << getFilename(Res2) << " ]";
356}
357
358/// dump - Print type.
359void DIType::dump() const {
360 if (isNull()) return;
361
362 std::string Res;
363 if (!getName(Res).empty())
364 cerr << " [" << Res << "] ";
365
366 unsigned Tag = getTag();
367 cerr << " [" << dwarf::TagString(Tag) << "] ";
368
369 // TODO : Print context
370 getCompileUnit().dump();
371 cerr << " ["
372 << getLineNumber() << ", "
373 << getSizeInBits() << ", "
374 << getAlignInBits() << ", "
375 << getOffsetInBits()
376 << "] ";
377
378 if (isPrivate())
379 cerr << " [private] ";
380 else if (isProtected())
381 cerr << " [protected] ";
382
383 if (isForwardDecl())
384 cerr << " [fwd] ";
385
386 if (isBasicType(Tag))
387 DIBasicType(DbgGV).dump();
388 else if (isDerivedType(Tag))
389 DIDerivedType(DbgGV).dump();
390 else if (isCompositeType(Tag))
391 DICompositeType(DbgGV).dump();
392 else {
393 cerr << "Invalid DIType\n";
394 return;
395 }
396
397 cerr << "\n";
398}
399
400/// dump - Print basic type.
401void DIBasicType::dump() const {
402 cerr << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
403}
404
405/// dump - Print derived type.
406void DIDerivedType::dump() const {
407 cerr << "\n\t Derived From: "; getTypeDerivedFrom().dump();
408}
409
410/// dump - Print composite type.
411void DICompositeType::dump() const {
412 DIArray A = getTypeArray();
413 if (A.isNull())
414 return;
415 cerr << " [" << A.getNumElements() << " elements]";
416}
417
418/// dump - Print global.
419void DIGlobal::dump() const {
420 std::string Res;
421 if (!getName(Res).empty())
422 cerr << " [" << Res << "] ";
423
424 unsigned Tag = getTag();
425 cerr << " [" << dwarf::TagString(Tag) << "] ";
426
427 // TODO : Print context
428 getCompileUnit().dump();
429 cerr << " [" << getLineNumber() << "] ";
430
431 if (isLocalToUnit())
432 cerr << " [local] ";
433
434 if (isDefinition())
435 cerr << " [def] ";
436
437 if (isGlobalVariable(Tag))
438 DIGlobalVariable(DbgGV).dump();
439
440 cerr << "\n";
441}
442
443/// dump - Print subprogram.
444void DISubprogram::dump() const {
445 DIGlobal::dump();
446}
447
448/// dump - Print global variable.
449void DIGlobalVariable::dump() const {
450 cerr << " ["; getGlobal()->dump(); cerr << "] ";
451}
452
453/// dump - Print variable.
454void DIVariable::dump() const {
455 std::string Res;
456 if (!getName(Res).empty())
457 cerr << " [" << Res << "] ";
458
459 getCompileUnit().dump();
460 cerr << " [" << getLineNumber() << "] ";
461 getType().dump();
462 cerr << "\n";
463}
464
465//===----------------------------------------------------------------------===//
Chris Lattnera45664f2008-11-10 02:56:27 +0000466// DIFactory: Basic Helpers
467//===----------------------------------------------------------------------===//
468
Bill Wendlingdc817b62009-05-14 18:26:15 +0000469DIFactory::DIFactory(Module &m)
Owen Anderson99035272009-07-07 17:12:53 +0000470 : M(m), VMContext(M.getContext()), StopPointFn(0), FuncStartFn(0),
471 RegionStartFn(0), RegionEndFn(0),
Bill Wendlingdc817b62009-05-14 18:26:15 +0000472 DeclareFn(0) {
Owen Andersondebcb012009-07-29 22:17:13 +0000473 EmptyStructPtr = PointerType::getUnqual(StructType::get());
Chris Lattner497a7a82008-11-10 04:10:34 +0000474}
475
476/// getCastToEmpty - Return this descriptor as a Constant* with type '{}*'.
477/// This is only valid when the descriptor is non-null.
478Constant *DIFactory::getCastToEmpty(DIDescriptor D) {
Owen Andersona7235ea2009-07-31 20:28:14 +0000479 if (D.isNull()) return llvm::Constant::getNullValue(EmptyStructPtr);
Owen Andersonbaf3c402009-07-29 18:55:55 +0000480 return ConstantExpr::getBitCast(D.getGV(), EmptyStructPtr);
Chris Lattner497a7a82008-11-10 04:10:34 +0000481}
482
Chris Lattnera45664f2008-11-10 02:56:27 +0000483Constant *DIFactory::GetTagConstant(unsigned TAG) {
Devang Patel6906ba52009-01-20 19:22:03 +0000484 assert((TAG & LLVMDebugVersionMask) == 0 &&
Chris Lattnera45664f2008-11-10 02:56:27 +0000485 "Tag too large for debug encoding!");
Owen Andersoneed707b2009-07-24 23:12:02 +0000486 return ConstantInt::get(Type::Int32Ty, TAG | LLVMDebugVersion);
Chris Lattnera45664f2008-11-10 02:56:27 +0000487}
488
489Constant *DIFactory::GetStringConstant(const std::string &String) {
490 // Check string cache for previous edition.
491 Constant *&Slot = StringCache[String];
492
493 // Return Constant if previously defined.
494 if (Slot) return Slot;
495
Owen Andersondebcb012009-07-29 22:17:13 +0000496 const PointerType *DestTy = PointerType::getUnqual(Type::Int8Ty);
Chris Lattnera45664f2008-11-10 02:56:27 +0000497
Dan Gohmana119de82009-06-14 23:30:43 +0000498 // If empty string then use a i8* null instead.
Chris Lattnera45664f2008-11-10 02:56:27 +0000499 if (String.empty())
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000500 return Slot = ConstantPointerNull::get(DestTy);
Chris Lattnera45664f2008-11-10 02:56:27 +0000501
502 // Construct string as an llvm constant.
Owen Anderson1fd70962009-07-28 18:32:17 +0000503 Constant *ConstStr = ConstantArray::get(String);
Chris Lattnera45664f2008-11-10 02:56:27 +0000504
505 // Otherwise create and return a new string global.
Owen Andersone9b11b42009-07-08 19:03:57 +0000506 GlobalVariable *StrGV = new GlobalVariable(M, ConstStr->getType(), true,
Chris Lattnera45664f2008-11-10 02:56:27 +0000507 GlobalVariable::InternalLinkage,
Owen Andersone9b11b42009-07-08 19:03:57 +0000508 ConstStr, ".str");
Chris Lattnera45664f2008-11-10 02:56:27 +0000509 StrGV->setSection("llvm.metadata");
Owen Andersonbaf3c402009-07-29 18:55:55 +0000510 return Slot = ConstantExpr::getBitCast(StrGV, DestTy);
Chris Lattnera45664f2008-11-10 02:56:27 +0000511}
512
Chris Lattnera45664f2008-11-10 02:56:27 +0000513//===----------------------------------------------------------------------===//
514// DIFactory: Primary Constructors
515//===----------------------------------------------------------------------===//
516
Chris Lattnera45664f2008-11-10 02:56:27 +0000517/// GetOrCreateArray - Create an descriptor for an array of descriptors.
518/// This implicitly uniques the arrays created.
519DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
520 SmallVector<Constant*, 16> Elts;
521
522 for (unsigned i = 0; i != NumTys; ++i)
Chris Lattner497a7a82008-11-10 04:10:34 +0000523 Elts.push_back(getCastToEmpty(Tys[i]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000524
Owen Andersondebcb012009-07-29 22:17:13 +0000525 Constant *Init = ConstantArray::get(ArrayType::get(EmptyStructPtr,
Chris Lattner497a7a82008-11-10 04:10:34 +0000526 Elts.size()),
Jay Foade3e51c02009-05-21 09:52:38 +0000527 Elts.data(), Elts.size());
Chris Lattnera45664f2008-11-10 02:56:27 +0000528 // If we already have this array, just return the uniqued version.
529 DIDescriptor &Entry = SimpleConstantCache[Init];
530 if (!Entry.isNull()) return DIArray(Entry.getGV());
531
Owen Andersone9b11b42009-07-08 19:03:57 +0000532 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
Chris Lattnera45664f2008-11-10 02:56:27 +0000533 GlobalValue::InternalLinkage,
Owen Andersone9b11b42009-07-08 19:03:57 +0000534 Init, "llvm.dbg.array");
Chris Lattnera45664f2008-11-10 02:56:27 +0000535 GV->setSection("llvm.metadata");
536 Entry = DIDescriptor(GV);
537 return DIArray(GV);
538}
539
540/// GetOrCreateSubrange - Create a descriptor for a value range. This
541/// implicitly uniques the values returned.
542DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
543 Constant *Elts[] = {
544 GetTagConstant(dwarf::DW_TAG_subrange_type),
Owen Andersoneed707b2009-07-24 23:12:02 +0000545 ConstantInt::get(Type::Int64Ty, Lo),
546 ConstantInt::get(Type::Int64Ty, Hi)
Chris Lattnera45664f2008-11-10 02:56:27 +0000547 };
548
Owen Anderson8fa33382009-07-27 22:29:26 +0000549 Constant *Init = ConstantStruct::get(Elts, 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 Andersona7235ea2009-07-31 20:28:14 +0000579 llvm::Constant::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 Anderson8fa33382009-07-27 22:29:26 +0000590 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000591
592 M.addTypeName("llvm.dbg.compile_unit.type", Init->getType());
Owen Andersone9b11b42009-07-08 19:03:57 +0000593 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
Devang Patel13e16b62009-06-26 01:49:18 +0000594 GlobalValue::LinkOnceAnyLinkage,
Owen Andersone9b11b42009-07-08 19:03:57 +0000595 Init, "llvm.dbg.compile_unit");
Chris Lattnera45664f2008-11-10 02:56:27 +0000596 GV->setSection("llvm.metadata");
597 return DICompileUnit(GV);
598}
599
600/// CreateEnumerator - Create a single enumerator value.
601DIEnumerator DIFactory::CreateEnumerator(const std::string &Name, uint64_t Val){
602 Constant *Elts[] = {
603 GetTagConstant(dwarf::DW_TAG_enumerator),
604 GetStringConstant(Name),
Owen Andersoneed707b2009-07-24 23:12:02 +0000605 ConstantInt::get(Type::Int64Ty, Val)
Chris Lattnera45664f2008-11-10 02:56:27 +0000606 };
607
Owen Anderson8fa33382009-07-27 22:29:26 +0000608 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000609
610 M.addTypeName("llvm.dbg.enumerator.type", Init->getType());
Owen Andersone9b11b42009-07-08 19:03:57 +0000611 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
Chris Lattnera45664f2008-11-10 02:56:27 +0000612 GlobalValue::InternalLinkage,
Owen Andersone9b11b42009-07-08 19:03:57 +0000613 Init, "llvm.dbg.enumerator");
Chris Lattnera45664f2008-11-10 02:56:27 +0000614 GV->setSection("llvm.metadata");
615 return DIEnumerator(GV);
616}
617
618
619/// CreateBasicType - Create a basic type like int, float, etc.
620DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
Bill Wendling0582ae92009-03-13 04:39:26 +0000621 const std::string &Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000622 DICompileUnit CompileUnit,
623 unsigned LineNumber,
624 uint64_t SizeInBits,
625 uint64_t AlignInBits,
626 uint64_t OffsetInBits, unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000627 unsigned Encoding) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000628 Constant *Elts[] = {
629 GetTagConstant(dwarf::DW_TAG_base_type),
Chris Lattner497a7a82008-11-10 04:10:34 +0000630 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000631 GetStringConstant(Name),
Chris Lattner497a7a82008-11-10 04:10:34 +0000632 getCastToEmpty(CompileUnit),
Owen Andersoneed707b2009-07-24 23:12:02 +0000633 ConstantInt::get(Type::Int32Ty, LineNumber),
634 ConstantInt::get(Type::Int64Ty, SizeInBits),
635 ConstantInt::get(Type::Int64Ty, AlignInBits),
636 ConstantInt::get(Type::Int64Ty, OffsetInBits),
637 ConstantInt::get(Type::Int32Ty, Flags),
638 ConstantInt::get(Type::Int32Ty, Encoding)
Chris Lattnera45664f2008-11-10 02:56:27 +0000639 };
640
Owen Anderson8fa33382009-07-27 22:29:26 +0000641 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000642
643 M.addTypeName("llvm.dbg.basictype.type", Init->getType());
Owen Andersone9b11b42009-07-08 19:03:57 +0000644 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
Chris Lattnera45664f2008-11-10 02:56:27 +0000645 GlobalValue::InternalLinkage,
Owen Andersone9b11b42009-07-08 19:03:57 +0000646 Init, "llvm.dbg.basictype");
Chris Lattnera45664f2008-11-10 02:56:27 +0000647 GV->setSection("llvm.metadata");
648 return DIBasicType(GV);
649}
650
651/// CreateDerivedType - Create a derived type like const qualified type,
652/// pointer, typedef, etc.
653DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
654 DIDescriptor Context,
655 const std::string &Name,
656 DICompileUnit CompileUnit,
657 unsigned LineNumber,
658 uint64_t SizeInBits,
659 uint64_t AlignInBits,
660 uint64_t OffsetInBits,
661 unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000662 DIType DerivedFrom) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000663 Constant *Elts[] = {
664 GetTagConstant(Tag),
Chris Lattner497a7a82008-11-10 04:10:34 +0000665 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000666 GetStringConstant(Name),
Chris Lattner497a7a82008-11-10 04:10:34 +0000667 getCastToEmpty(CompileUnit),
Owen Andersoneed707b2009-07-24 23:12:02 +0000668 ConstantInt::get(Type::Int32Ty, LineNumber),
669 ConstantInt::get(Type::Int64Ty, SizeInBits),
670 ConstantInt::get(Type::Int64Ty, AlignInBits),
671 ConstantInt::get(Type::Int64Ty, OffsetInBits),
672 ConstantInt::get(Type::Int32Ty, Flags),
Devang Pateldd9db662009-01-30 18:20:31 +0000673 getCastToEmpty(DerivedFrom)
Chris Lattnera45664f2008-11-10 02:56:27 +0000674 };
675
Owen Anderson8fa33382009-07-27 22:29:26 +0000676 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000677
678 M.addTypeName("llvm.dbg.derivedtype.type", Init->getType());
Owen Andersone9b11b42009-07-08 19:03:57 +0000679 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
Chris Lattnera45664f2008-11-10 02:56:27 +0000680 GlobalValue::InternalLinkage,
Owen Andersone9b11b42009-07-08 19:03:57 +0000681 Init, "llvm.dbg.derivedtype");
Chris Lattnera45664f2008-11-10 02:56:27 +0000682 GV->setSection("llvm.metadata");
683 return DIDerivedType(GV);
684}
685
686/// CreateCompositeType - Create a composite type like array, struct, etc.
687DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
688 DIDescriptor Context,
689 const std::string &Name,
690 DICompileUnit CompileUnit,
691 unsigned LineNumber,
692 uint64_t SizeInBits,
693 uint64_t AlignInBits,
694 uint64_t OffsetInBits,
695 unsigned Flags,
696 DIType DerivedFrom,
Devang Patel13319ce2009-02-17 22:43:44 +0000697 DIArray Elements,
698 unsigned RuntimeLang) {
Owen Andersone277fed2009-07-07 16:31:25 +0000699
Chris Lattnera45664f2008-11-10 02:56:27 +0000700 Constant *Elts[] = {
701 GetTagConstant(Tag),
Chris Lattner497a7a82008-11-10 04:10:34 +0000702 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000703 GetStringConstant(Name),
Chris Lattner497a7a82008-11-10 04:10:34 +0000704 getCastToEmpty(CompileUnit),
Owen Andersoneed707b2009-07-24 23:12:02 +0000705 ConstantInt::get(Type::Int32Ty, LineNumber),
706 ConstantInt::get(Type::Int64Ty, SizeInBits),
707 ConstantInt::get(Type::Int64Ty, AlignInBits),
708 ConstantInt::get(Type::Int64Ty, OffsetInBits),
709 ConstantInt::get(Type::Int32Ty, Flags),
Chris Lattner497a7a82008-11-10 04:10:34 +0000710 getCastToEmpty(DerivedFrom),
Devang Patel13319ce2009-02-17 22:43:44 +0000711 getCastToEmpty(Elements),
Owen Andersoneed707b2009-07-24 23:12:02 +0000712 ConstantInt::get(Type::Int32Ty, RuntimeLang)
Chris Lattnera45664f2008-11-10 02:56:27 +0000713 };
714
Owen Anderson8fa33382009-07-27 22:29:26 +0000715 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000716
717 M.addTypeName("llvm.dbg.composite.type", Init->getType());
Owen Andersone9b11b42009-07-08 19:03:57 +0000718 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
Chris Lattnera45664f2008-11-10 02:56:27 +0000719 GlobalValue::InternalLinkage,
Owen Andersone9b11b42009-07-08 19:03:57 +0000720 Init, "llvm.dbg.composite");
Chris Lattnera45664f2008-11-10 02:56:27 +0000721 GV->setSection("llvm.metadata");
722 return DICompositeType(GV);
723}
724
725
726/// CreateSubprogram - Create a new descriptor for the specified subprogram.
727/// See comments in DISubprogram for descriptions of these fields. This
728/// method does not unique the generated descriptors.
729DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
730 const std::string &Name,
731 const std::string &DisplayName,
732 const std::string &LinkageName,
733 DICompileUnit CompileUnit,
734 unsigned LineNo, DIType Type,
735 bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000736 bool isDefinition) {
Devang Patel854967e2008-12-17 22:39:29 +0000737
Chris Lattnera45664f2008-11-10 02:56:27 +0000738 Constant *Elts[] = {
739 GetTagConstant(dwarf::DW_TAG_subprogram),
Owen Andersona7235ea2009-07-31 20:28:14 +0000740 llvm::Constant::getNullValue(EmptyStructPtr),
Chris Lattner497a7a82008-11-10 04:10:34 +0000741 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000742 GetStringConstant(Name),
743 GetStringConstant(DisplayName),
744 GetStringConstant(LinkageName),
Chris Lattner497a7a82008-11-10 04:10:34 +0000745 getCastToEmpty(CompileUnit),
Owen Andersoneed707b2009-07-24 23:12:02 +0000746 ConstantInt::get(Type::Int32Ty, LineNo),
Chris Lattner497a7a82008-11-10 04:10:34 +0000747 getCastToEmpty(Type),
Owen Andersoneed707b2009-07-24 23:12:02 +0000748 ConstantInt::get(Type::Int1Ty, isLocalToUnit),
749 ConstantInt::get(Type::Int1Ty, isDefinition)
Chris Lattnera45664f2008-11-10 02:56:27 +0000750 };
751
Owen Anderson8fa33382009-07-27 22:29:26 +0000752 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000753
754 M.addTypeName("llvm.dbg.subprogram.type", Init->getType());
Owen Andersone9b11b42009-07-08 19:03:57 +0000755 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
Devang Patel13e16b62009-06-26 01:49:18 +0000756 GlobalValue::LinkOnceAnyLinkage,
Owen Andersone9b11b42009-07-08 19:03:57 +0000757 Init, "llvm.dbg.subprogram");
Chris Lattnera45664f2008-11-10 02:56:27 +0000758 GV->setSection("llvm.metadata");
759 return DISubprogram(GV);
760}
761
762/// CreateGlobalVariable - Create a new descriptor for the specified global.
763DIGlobalVariable
764DIFactory::CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
765 const std::string &DisplayName,
766 const std::string &LinkageName,
767 DICompileUnit CompileUnit,
768 unsigned LineNo, DIType Type,bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000769 bool isDefinition, llvm::GlobalVariable *Val) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000770 Constant *Elts[] = {
771 GetTagConstant(dwarf::DW_TAG_variable),
Owen Andersona7235ea2009-07-31 20:28:14 +0000772 llvm::Constant::getNullValue(EmptyStructPtr),
Chris Lattner497a7a82008-11-10 04:10:34 +0000773 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000774 GetStringConstant(Name),
775 GetStringConstant(DisplayName),
776 GetStringConstant(LinkageName),
Chris Lattner497a7a82008-11-10 04:10:34 +0000777 getCastToEmpty(CompileUnit),
Owen Andersoneed707b2009-07-24 23:12:02 +0000778 ConstantInt::get(Type::Int32Ty, LineNo),
Chris Lattner497a7a82008-11-10 04:10:34 +0000779 getCastToEmpty(Type),
Owen Andersoneed707b2009-07-24 23:12:02 +0000780 ConstantInt::get(Type::Int1Ty, isLocalToUnit),
781 ConstantInt::get(Type::Int1Ty, isDefinition),
Owen Andersonbaf3c402009-07-29 18:55:55 +0000782 ConstantExpr::getBitCast(Val, EmptyStructPtr)
Chris Lattnera45664f2008-11-10 02:56:27 +0000783 };
784
Owen Anderson8fa33382009-07-27 22:29:26 +0000785 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000786
787 M.addTypeName("llvm.dbg.global_variable.type", Init->getType());
Owen Andersone9b11b42009-07-08 19:03:57 +0000788 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
Devang Patel13e16b62009-06-26 01:49:18 +0000789 GlobalValue::LinkOnceAnyLinkage,
Owen Andersone9b11b42009-07-08 19:03:57 +0000790 Init, "llvm.dbg.global_variable");
Chris Lattnera45664f2008-11-10 02:56:27 +0000791 GV->setSection("llvm.metadata");
792 return DIGlobalVariable(GV);
793}
794
795
796/// CreateVariable - Create a new descriptor for the specified variable.
797DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
798 const std::string &Name,
799 DICompileUnit CompileUnit, unsigned LineNo,
Devang Pateldd9db662009-01-30 18:20:31 +0000800 DIType Type) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000801 Constant *Elts[] = {
802 GetTagConstant(Tag),
Chris Lattner497a7a82008-11-10 04:10:34 +0000803 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000804 GetStringConstant(Name),
Chris Lattner497a7a82008-11-10 04:10:34 +0000805 getCastToEmpty(CompileUnit),
Owen Andersoneed707b2009-07-24 23:12:02 +0000806 ConstantInt::get(Type::Int32Ty, LineNo),
Devang Pateldd9db662009-01-30 18:20:31 +0000807 getCastToEmpty(Type)
Chris Lattnera45664f2008-11-10 02:56:27 +0000808 };
809
Owen Anderson8fa33382009-07-27 22:29:26 +0000810 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000811
812 M.addTypeName("llvm.dbg.variable.type", Init->getType());
Owen Andersone9b11b42009-07-08 19:03:57 +0000813 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
Chris Lattnera45664f2008-11-10 02:56:27 +0000814 GlobalValue::InternalLinkage,
Owen Andersone9b11b42009-07-08 19:03:57 +0000815 Init, "llvm.dbg.variable");
Chris Lattnera45664f2008-11-10 02:56:27 +0000816 GV->setSection("llvm.metadata");
817 return DIVariable(GV);
818}
819
820
821/// CreateBlock - This creates a descriptor for a lexical block with the
Owen Anderson99035272009-07-07 17:12:53 +0000822/// specified parent VMContext.
Chris Lattnera45664f2008-11-10 02:56:27 +0000823DIBlock DIFactory::CreateBlock(DIDescriptor Context) {
824 Constant *Elts[] = {
825 GetTagConstant(dwarf::DW_TAG_lexical_block),
Chris Lattner497a7a82008-11-10 04:10:34 +0000826 getCastToEmpty(Context)
Chris Lattnera45664f2008-11-10 02:56:27 +0000827 };
828
Owen Anderson8fa33382009-07-27 22:29:26 +0000829 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000830
831 M.addTypeName("llvm.dbg.block.type", Init->getType());
Owen Andersone9b11b42009-07-08 19:03:57 +0000832 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
Chris Lattnera45664f2008-11-10 02:56:27 +0000833 GlobalValue::InternalLinkage,
Owen Andersone9b11b42009-07-08 19:03:57 +0000834 Init, "llvm.dbg.block");
Chris Lattnera45664f2008-11-10 02:56:27 +0000835 GV->setSection("llvm.metadata");
836 return DIBlock(GV);
837}
838
839
840//===----------------------------------------------------------------------===//
841// DIFactory: Routines for inserting code into a function
842//===----------------------------------------------------------------------===//
843
844/// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
845/// inserting it at the end of the specified basic block.
846void DIFactory::InsertStopPoint(DICompileUnit CU, unsigned LineNo,
847 unsigned ColNo, BasicBlock *BB) {
848
849 // Lazily construct llvm.dbg.stoppoint function.
850 if (!StopPointFn)
851 StopPointFn = llvm::Intrinsic::getDeclaration(&M,
852 llvm::Intrinsic::dbg_stoppoint);
853
854 // Invoke llvm.dbg.stoppoint
855 Value *Args[] = {
Owen Andersoneed707b2009-07-24 23:12:02 +0000856 ConstantInt::get(llvm::Type::Int32Ty, LineNo),
857 ConstantInt::get(llvm::Type::Int32Ty, ColNo),
Chris Lattner497a7a82008-11-10 04:10:34 +0000858 getCastToEmpty(CU)
Chris Lattnera45664f2008-11-10 02:56:27 +0000859 };
860 CallInst::Create(StopPointFn, Args, Args+3, "", BB);
861}
862
863/// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
864/// mark the start of the specified subprogram.
865void DIFactory::InsertSubprogramStart(DISubprogram SP, BasicBlock *BB) {
866 // Lazily construct llvm.dbg.func.start.
867 if (!FuncStartFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000868 FuncStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_func_start);
Chris Lattnera45664f2008-11-10 02:56:27 +0000869
870 // Call llvm.dbg.func.start which also implicitly sets a stoppoint.
Chris Lattner497a7a82008-11-10 04:10:34 +0000871 CallInst::Create(FuncStartFn, getCastToEmpty(SP), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000872}
873
874/// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
875/// mark the start of a region for the specified scoping descriptor.
876void DIFactory::InsertRegionStart(DIDescriptor D, BasicBlock *BB) {
877 // Lazily construct llvm.dbg.region.start function.
878 if (!RegionStartFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000879 RegionStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_start);
880
Chris Lattnera45664f2008-11-10 02:56:27 +0000881 // Call llvm.dbg.func.start.
Chris Lattner497a7a82008-11-10 04:10:34 +0000882 CallInst::Create(RegionStartFn, getCastToEmpty(D), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000883}
884
Chris Lattnera45664f2008-11-10 02:56:27 +0000885/// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
886/// mark the end of a region for the specified scoping descriptor.
887void DIFactory::InsertRegionEnd(DIDescriptor D, BasicBlock *BB) {
888 // Lazily construct llvm.dbg.region.end function.
889 if (!RegionEndFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000890 RegionEndFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_end);
891
892 // Call llvm.dbg.region.end.
Chris Lattner497a7a82008-11-10 04:10:34 +0000893 CallInst::Create(RegionEndFn, getCastToEmpty(D), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000894}
895
896/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Bill Wendlingdc817b62009-05-14 18:26:15 +0000897void DIFactory::InsertDeclare(Value *Storage, DIVariable D, BasicBlock *BB) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000898 // Cast the storage to a {}* for the call to llvm.dbg.declare.
Bill Wendlingdc817b62009-05-14 18:26:15 +0000899 Storage = new BitCastInst(Storage, EmptyStructPtr, "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000900
901 if (!DeclareFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000902 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
903
Chris Lattner497a7a82008-11-10 04:10:34 +0000904 Value *Args[] = { Storage, getCastToEmpty(D) };
Chris Lattnera45664f2008-11-10 02:56:27 +0000905 CallInst::Create(DeclareFn, Args, Args+2, "", BB);
906}
Torok Edwin620f2802008-12-16 09:07:36 +0000907
Devang Pateld2f79a12009-07-28 19:55:13 +0000908//===----------------------------------------------------------------------===//
Devang Patel98c65172009-07-30 18:25:15 +0000909// DebugInfoFinder implementations.
Devang Pateld2f79a12009-07-28 19:55:13 +0000910//===----------------------------------------------------------------------===//
911
Devang Patel98c65172009-07-30 18:25:15 +0000912/// processModule - Process entire module and collect debug info.
913void DebugInfoFinder::processModule(Module &M) {
Devang Pateld2f79a12009-07-28 19:55:13 +0000914
915 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
916 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
917 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
918 ++BI) {
919 if (DbgStopPointInst *SPI = dyn_cast<DbgStopPointInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +0000920 processStopPoint(SPI);
Devang Pateld2f79a12009-07-28 19:55:13 +0000921 else if (DbgFuncStartInst *FSI = dyn_cast<DbgFuncStartInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +0000922 processFuncStart(FSI);
Devang Patele802f1c2009-07-30 17:30:23 +0000923 else if (DbgRegionStartInst *DRS = dyn_cast<DbgRegionStartInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +0000924 processRegionStart(DRS);
Devang Patele802f1c2009-07-30 17:30:23 +0000925 else if (DbgRegionEndInst *DRE = dyn_cast<DbgRegionEndInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +0000926 processRegionEnd(DRE);
Devang Patelb4d31302009-07-31 18:18:52 +0000927 else if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
928 processDeclare(DDI);
Devang Pateld2f79a12009-07-28 19:55:13 +0000929 }
930
931 for (Module::global_iterator GVI = M.global_begin(), GVE = M.global_end();
932 GVI != GVE; ++GVI) {
933 GlobalVariable *GV = GVI;
934 if (!GV->hasName() || !GV->isConstant()
935 || strcmp(GV->getName().data(), "llvm.dbg.global_variable")
936 || !GV->hasInitializer())
937 continue;
938 DIGlobalVariable DIG(GV);
939 if (addGlobalVariable(DIG)) {
940 addCompileUnit(DIG.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +0000941 processType(DIG.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +0000942 }
943 }
944}
945
Devang Patel98c65172009-07-30 18:25:15 +0000946/// processType - Process DIType.
947void DebugInfoFinder::processType(DIType DT) {
Devang Pateld2f79a12009-07-28 19:55:13 +0000948 if (DT.isNull())
949 return;
950 if (!NodesSeen.insert(DT.getGV()))
951 return;
952
953 addCompileUnit(DT.getCompileUnit());
954 if (DT.isCompositeType(DT.getTag())) {
955 DICompositeType DCT(DT.getGV());
Devang Patel98c65172009-07-30 18:25:15 +0000956 processType(DCT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +0000957 DIArray DA = DCT.getTypeArray();
958 if (!DA.isNull())
959 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
960 DIDescriptor D = DA.getElement(i);
961 DIType TypeE = DIType(D.getGV());
962 if (!TypeE.isNull())
Devang Patel98c65172009-07-30 18:25:15 +0000963 processType(TypeE);
Devang Pateld2f79a12009-07-28 19:55:13 +0000964 else
Devang Patel98c65172009-07-30 18:25:15 +0000965 processSubprogram(DISubprogram(D.getGV()));
Devang Pateld2f79a12009-07-28 19:55:13 +0000966 }
967 } else if (DT.isDerivedType(DT.getTag())) {
968 DIDerivedType DDT(DT.getGV());
969 if (!DDT.isNull())
Devang Patel98c65172009-07-30 18:25:15 +0000970 processType(DDT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +0000971 }
972}
973
Devang Patel98c65172009-07-30 18:25:15 +0000974/// processSubprogram - Process DISubprogram.
975void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Patele802f1c2009-07-30 17:30:23 +0000976 if (SP.isNull())
977 return;
Devang Pateld2f79a12009-07-28 19:55:13 +0000978 if (!addSubprogram(SP))
979 return;
980 addCompileUnit(SP.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +0000981 processType(SP.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +0000982}
983
Devang Patel98c65172009-07-30 18:25:15 +0000984/// processStopPoint - Process DbgStopPointInst.
985void DebugInfoFinder::processStopPoint(DbgStopPointInst *SPI) {
Devang Pateld2f79a12009-07-28 19:55:13 +0000986 GlobalVariable *Context = dyn_cast<GlobalVariable>(SPI->getContext());
987 addCompileUnit(DICompileUnit(Context));
988}
989
Devang Patel98c65172009-07-30 18:25:15 +0000990/// processFuncStart - Process DbgFuncStartInst.
991void DebugInfoFinder::processFuncStart(DbgFuncStartInst *FSI) {
Devang Pateld2f79a12009-07-28 19:55:13 +0000992 GlobalVariable *SP = dyn_cast<GlobalVariable>(FSI->getSubprogram());
Devang Patel98c65172009-07-30 18:25:15 +0000993 processSubprogram(DISubprogram(SP));
Devang Pateld2f79a12009-07-28 19:55:13 +0000994}
995
Devang Patel98c65172009-07-30 18:25:15 +0000996/// processRegionStart - Process DbgRegionStart.
997void DebugInfoFinder::processRegionStart(DbgRegionStartInst *DRS) {
Devang Patele802f1c2009-07-30 17:30:23 +0000998 GlobalVariable *SP = dyn_cast<GlobalVariable>(DRS->getContext());
Devang Patel98c65172009-07-30 18:25:15 +0000999 processSubprogram(DISubprogram(SP));
Devang Patele802f1c2009-07-30 17:30:23 +00001000}
1001
Devang Patel98c65172009-07-30 18:25:15 +00001002/// processRegionEnd - Process DbgRegionEnd.
1003void DebugInfoFinder::processRegionEnd(DbgRegionEndInst *DRE) {
Devang Patele802f1c2009-07-30 17:30:23 +00001004 GlobalVariable *SP = dyn_cast<GlobalVariable>(DRE->getContext());
Devang Patel98c65172009-07-30 18:25:15 +00001005 processSubprogram(DISubprogram(SP));
Devang Patele802f1c2009-07-30 17:30:23 +00001006}
1007
Devang Patelb4d31302009-07-31 18:18:52 +00001008/// processDeclare - Process DbgDeclareInst.
1009void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
1010 DIVariable DV(cast<GlobalVariable>(DDI->getVariable()));
1011 if (DV.isNull())
1012 return;
1013
1014 if (!NodesSeen.insert(DV.getGV()))
1015 return;
1016
1017 addCompileUnit(DV.getCompileUnit());
1018 processType(DV.getType());
1019}
1020
Devang Pateld2f79a12009-07-28 19:55:13 +00001021/// addCompileUnit - Add compile unit into CUs.
Devang Patel98c65172009-07-30 18:25:15 +00001022bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001023 if (CU.isNull())
1024 return false;
1025
1026 if (!NodesSeen.insert(CU.getGV()))
1027 return false;
1028
1029 CUs.push_back(CU.getGV());
1030 return true;
1031}
1032
1033/// addGlobalVariable - Add global variable into GVs.
Devang Patel98c65172009-07-30 18:25:15 +00001034bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001035 if (DIG.isNull())
1036 return false;
1037
1038 if (!NodesSeen.insert(DIG.getGV()))
1039 return false;
1040
1041 GVs.push_back(DIG.getGV());
1042 return true;
1043}
1044
1045// addSubprogram - Add subprgoram into SPs.
Devang Patel98c65172009-07-30 18:25:15 +00001046bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001047 if (SP.isNull())
1048 return false;
1049
1050 if (!NodesSeen.insert(SP.getGV()))
1051 return false;
1052
1053 SPs.push_back(SP.getGV());
1054 return true;
1055}
1056
Torok Edwin620f2802008-12-16 09:07:36 +00001057namespace llvm {
Bill Wendlingdc817b62009-05-14 18:26:15 +00001058 /// findStopPoint - Find the stoppoint coressponding to this instruction, that
1059 /// is the stoppoint that dominates this instruction.
1060 const DbgStopPointInst *findStopPoint(const Instruction *Inst) {
Torok Edwin620f2802008-12-16 09:07:36 +00001061 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(Inst))
1062 return DSI;
1063
1064 const BasicBlock *BB = Inst->getParent();
1065 BasicBlock::const_iterator I = Inst, B;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001066 while (BB) {
Torok Edwin620f2802008-12-16 09:07:36 +00001067 B = BB->begin();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001068
Torok Edwin620f2802008-12-16 09:07:36 +00001069 // A BB consisting only of a terminator can't have a stoppoint.
Bill Wendlingdc817b62009-05-14 18:26:15 +00001070 while (I != B) {
1071 --I;
1072 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
1073 return DSI;
Torok Edwin620f2802008-12-16 09:07:36 +00001074 }
Bill Wendlingdc817b62009-05-14 18:26:15 +00001075
1076 // This BB didn't have a stoppoint: if there is only one predecessor, look
1077 // for a stoppoint there. We could use getIDom(), but that would require
1078 // dominator info.
Torok Edwin620f2802008-12-16 09:07:36 +00001079 BB = I->getParent()->getUniquePredecessor();
1080 if (BB)
1081 I = BB->getTerminator();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001082 }
1083
Torok Edwin620f2802008-12-16 09:07:36 +00001084 return 0;
1085 }
1086
Bill Wendlingdc817b62009-05-14 18:26:15 +00001087 /// findBBStopPoint - Find the stoppoint corresponding to first real
1088 /// (non-debug intrinsic) instruction in this Basic Block, and return the
1089 /// stoppoint for it.
1090 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB) {
1091 for(BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001092 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
1093 return DSI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001094
1095 // Fallback to looking for stoppoint of unique predecessor. Useful if this
1096 // BB contains no stoppoints, but unique predecessor does.
Torok Edwin620f2802008-12-16 09:07:36 +00001097 BB = BB->getUniquePredecessor();
1098 if (BB)
1099 return findStopPoint(BB->getTerminator());
Bill Wendlingdc817b62009-05-14 18:26:15 +00001100
Torok Edwin620f2802008-12-16 09:07:36 +00001101 return 0;
1102 }
1103
Bill Wendlingdc817b62009-05-14 18:26:15 +00001104 Value *findDbgGlobalDeclare(GlobalVariable *V) {
Torok Edwinff7d0e92009-03-10 13:41:26 +00001105 const Module *M = V->getParent();
Owen Anderson99035272009-07-07 17:12:53 +00001106
Torok Edwinff7d0e92009-03-10 13:41:26 +00001107 const Type *Ty = M->getTypeByName("llvm.dbg.global_variable.type");
Bill Wendlingdc817b62009-05-14 18:26:15 +00001108 if (!Ty) return 0;
1109
Owen Andersondebcb012009-07-29 22:17:13 +00001110 Ty = PointerType::get(Ty, 0);
Torok Edwinff7d0e92009-03-10 13:41:26 +00001111
1112 Value *Val = V->stripPointerCasts();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001113 for (Value::use_iterator I = Val->use_begin(), E = Val->use_end();
Torok Edwinff7d0e92009-03-10 13:41:26 +00001114 I != E; ++I) {
1115 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(I)) {
1116 if (CE->getOpcode() == Instruction::BitCast) {
1117 Value *VV = CE;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001118
1119 while (VV->hasOneUse())
Torok Edwinff7d0e92009-03-10 13:41:26 +00001120 VV = *VV->use_begin();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001121
Torok Edwinff7d0e92009-03-10 13:41:26 +00001122 if (VV->getType() == Ty)
1123 return VV;
1124 }
1125 }
1126 }
1127
1128 if (Val->getType() == Ty)
1129 return Val;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001130
Torok Edwinff7d0e92009-03-10 13:41:26 +00001131 return 0;
1132 }
1133
Bill Wendlingdc817b62009-05-14 18:26:15 +00001134 /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
Torok Edwin620f2802008-12-16 09:07:36 +00001135 /// It looks through pointer casts too.
Bill Wendlingdc817b62009-05-14 18:26:15 +00001136 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts) {
Torok Edwin620f2802008-12-16 09:07:36 +00001137 if (stripCasts) {
1138 V = V->stripPointerCasts();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001139
Torok Edwin620f2802008-12-16 09:07:36 +00001140 // Look for the bitcast.
1141 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001142 I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001143 if (isa<BitCastInst>(I))
1144 return findDbgDeclare(*I, false);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001145
Torok Edwin620f2802008-12-16 09:07:36 +00001146 return 0;
1147 }
1148
Bill Wendlingdc817b62009-05-14 18:26:15 +00001149 // Find llvm.dbg.declare among uses of the instruction.
Torok Edwin620f2802008-12-16 09:07:36 +00001150 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001151 I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001152 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I))
1153 return DDI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001154
Torok Edwin620f2802008-12-16 09:07:36 +00001155 return 0;
1156 }
Torok Edwinff7d0e92009-03-10 13:41:26 +00001157
Bill Wendlingdc817b62009-05-14 18:26:15 +00001158 bool getLocationInfo(const Value *V, std::string &DisplayName,
1159 std::string &Type, unsigned &LineNo, std::string &File,
1160 std::string &Dir) {
Torok Edwinff7d0e92009-03-10 13:41:26 +00001161 DICompileUnit Unit;
1162 DIType TypeD;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001163
Torok Edwinff7d0e92009-03-10 13:41:26 +00001164 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1165 Value *DIGV = findDbgGlobalDeclare(GV);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001166 if (!DIGV) return false;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001167 DIGlobalVariable Var(cast<GlobalVariable>(DIGV));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001168
Bill Wendling0582ae92009-03-13 04:39:26 +00001169 Var.getDisplayName(DisplayName);
Torok Edwinff7d0e92009-03-10 13:41:26 +00001170 LineNo = Var.getLineNumber();
1171 Unit = Var.getCompileUnit();
1172 TypeD = Var.getType();
1173 } else {
1174 const DbgDeclareInst *DDI = findDbgDeclare(V);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001175 if (!DDI) return false;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001176 DIVariable Var(cast<GlobalVariable>(DDI->getVariable()));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001177
Bill Wendling0582ae92009-03-13 04:39:26 +00001178 Var.getName(DisplayName);
Torok Edwinff7d0e92009-03-10 13:41:26 +00001179 LineNo = Var.getLineNumber();
1180 Unit = Var.getCompileUnit();
1181 TypeD = Var.getType();
1182 }
Bill Wendlingdc817b62009-05-14 18:26:15 +00001183
Bill Wendling0582ae92009-03-13 04:39:26 +00001184 TypeD.getName(Type);
1185 Unit.getFilename(File);
1186 Unit.getDirectory(Dir);
Torok Edwinff7d0e92009-03-10 13:41:26 +00001187 return true;
1188 }
Devang Patel13e16b62009-06-26 01:49:18 +00001189
1190 /// CollectDebugInfoAnchors - Collect debugging information anchors.
1191 void CollectDebugInfoAnchors(Module &M,
1192 SmallVector<GlobalVariable *, 2> &CUs,
1193 SmallVector<GlobalVariable *, 4> &GVs,
1194 SmallVector<GlobalVariable *, 4> &SPs) {
1195
1196 for (Module::global_iterator GVI = M.global_begin(), E = M.global_end();
1197 GVI != E; GVI++) {
1198 GlobalVariable *GV = GVI;
Daniel Dunbar460f6562009-07-26 09:48:23 +00001199 if (GV->hasName() && GV->getName().startswith("llvm.dbg")
Devang Patel13e16b62009-06-26 01:49:18 +00001200 && GV->isConstant() && GV->hasInitializer()) {
1201 DICompileUnit C(GV);
1202 if (C.isNull() == false) {
1203 CUs.push_back(GV);
1204 continue;
1205 }
1206 DIGlobalVariable G(GV);
1207 if (G.isNull() == false) {
1208 GVs.push_back(GV);
1209 continue;
1210 }
1211 DISubprogram S(GV);
1212 if (S.isNull() == false) {
1213 SPs.push_back(GV);
1214 continue;
1215 }
1216 }
1217 }
1218 }
Devang Patel9e529c32009-07-02 01:15:24 +00001219
1220 /// isValidDebugInfoIntrinsic - Return true if SPI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001221 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001222 bool isValidDebugInfoIntrinsic(DbgStopPointInst &SPI,
1223 CodeGenOpt::Level OptLev) {
1224 return DIDescriptor::ValidDebugInfo(SPI.getContext(), OptLev);
1225 }
1226
1227 /// isValidDebugInfoIntrinsic - Return true if FSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001228 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001229 bool isValidDebugInfoIntrinsic(DbgFuncStartInst &FSI,
1230 CodeGenOpt::Level OptLev) {
1231 return DIDescriptor::ValidDebugInfo(FSI.getSubprogram(), OptLev);
1232 }
1233
1234 /// isValidDebugInfoIntrinsic - Return true if RSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001235 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001236 bool isValidDebugInfoIntrinsic(DbgRegionStartInst &RSI,
1237 CodeGenOpt::Level OptLev) {
1238 return DIDescriptor::ValidDebugInfo(RSI.getContext(), OptLev);
1239 }
1240
1241 /// isValidDebugInfoIntrinsic - Return true if REI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001242 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001243 bool isValidDebugInfoIntrinsic(DbgRegionEndInst &REI,
1244 CodeGenOpt::Level OptLev) {
1245 return DIDescriptor::ValidDebugInfo(REI.getContext(), OptLev);
1246 }
1247
1248
1249 /// isValidDebugInfoIntrinsic - Return true if DI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001250 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001251 bool isValidDebugInfoIntrinsic(DbgDeclareInst &DI,
1252 CodeGenOpt::Level OptLev) {
1253 return DIDescriptor::ValidDebugInfo(DI.getVariable(), OptLev);
1254 }
1255
1256 /// ExtractDebugLocation - Extract debug location information
1257 /// from llvm.dbg.stoppoint intrinsic.
1258 DebugLoc ExtractDebugLocation(DbgStopPointInst &SPI,
Devang Patel9e529c32009-07-02 01:15:24 +00001259 DebugLocTracker &DebugLocInfo) {
1260 DebugLoc DL;
1261 Value *Context = SPI.getContext();
Devang Patel9e529c32009-07-02 01:15:24 +00001262
1263 // If this location is already tracked then use it.
1264 DebugLocTuple Tuple(cast<GlobalVariable>(Context), SPI.getLine(),
1265 SPI.getColumn());
1266 DenseMap<DebugLocTuple, unsigned>::iterator II
1267 = DebugLocInfo.DebugIdMap.find(Tuple);
1268 if (II != DebugLocInfo.DebugIdMap.end())
1269 return DebugLoc::get(II->second);
1270
1271 // Add a new location entry.
1272 unsigned Id = DebugLocInfo.DebugLocations.size();
1273 DebugLocInfo.DebugLocations.push_back(Tuple);
1274 DebugLocInfo.DebugIdMap[Tuple] = Id;
1275
1276 return DebugLoc::get(Id);
1277 }
1278
1279 /// ExtractDebugLocation - Extract debug location information
1280 /// from llvm.dbg.func_start intrinsic.
1281 DebugLoc ExtractDebugLocation(DbgFuncStartInst &FSI,
Devang Patel9e529c32009-07-02 01:15:24 +00001282 DebugLocTracker &DebugLocInfo) {
1283 DebugLoc DL;
1284 Value *SP = FSI.getSubprogram();
Devang Patel9e529c32009-07-02 01:15:24 +00001285
1286 DISubprogram Subprogram(cast<GlobalVariable>(SP));
1287 unsigned Line = Subprogram.getLineNumber();
1288 DICompileUnit CU(Subprogram.getCompileUnit());
1289
1290 // If this location is already tracked then use it.
1291 DebugLocTuple Tuple(CU.getGV(), Line, /* Column */ 0);
1292 DenseMap<DebugLocTuple, unsigned>::iterator II
1293 = DebugLocInfo.DebugIdMap.find(Tuple);
1294 if (II != DebugLocInfo.DebugIdMap.end())
1295 return DebugLoc::get(II->second);
1296
1297 // Add a new location entry.
1298 unsigned Id = DebugLocInfo.DebugLocations.size();
1299 DebugLocInfo.DebugLocations.push_back(Tuple);
1300 DebugLocInfo.DebugIdMap[Tuple] = Id;
1301
1302 return DebugLoc::get(Id);
1303 }
1304
1305 /// isInlinedFnStart - Return true if FSI is starting an inlined function.
1306 bool isInlinedFnStart(DbgFuncStartInst &FSI, const Function *CurrentFn) {
1307 DISubprogram Subprogram(cast<GlobalVariable>(FSI.getSubprogram()));
1308 if (Subprogram.describes(CurrentFn))
1309 return false;
1310
1311 return true;
1312 }
1313
1314 /// isInlinedFnEnd - Return true if REI is ending an inlined function.
1315 bool isInlinedFnEnd(DbgRegionEndInst &REI, const Function *CurrentFn) {
1316 DISubprogram Subprogram(cast<GlobalVariable>(REI.getContext()));
1317 if (Subprogram.isNull() || Subprogram.describes(CurrentFn))
1318 return false;
1319
1320 return true;
1321 }
1322
Torok Edwin620f2802008-12-16 09:07:36 +00001323}