blob: b7c73f8760854274b68ad24907d4783c0c68c0b7 [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"
Chris Lattnera81d29b2009-08-23 07:33:14 +000026#include "llvm/Support/raw_ostream.h"
Chris Lattnera45664f2008-11-10 02:56:27 +000027using namespace llvm;
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000028using namespace llvm::dwarf;
Chris Lattnera45664f2008-11-10 02:56:27 +000029
30//===----------------------------------------------------------------------===//
31// DIDescriptor
32//===----------------------------------------------------------------------===//
33
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000034/// ValidDebugInfo - Return true if V represents valid debug info value.
Devang Patel82459882009-08-26 05:01:18 +000035bool DIDescriptor::ValidDebugInfo(Value *V, CodeGenOpt::Level OptLevel) {
36 if (!V)
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000037 return false;
38
Devang Patel82459882009-08-26 05:01:18 +000039 GlobalVariable *GV = dyn_cast<GlobalVariable>(V->stripPointerCasts());
40 if (!GV)
41 return false;
42
43 if (!GV->hasInternalLinkage () && !GV->hasLinkOnceLinkage())
44 return false;
45
46 DIDescriptor DI(GV);
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000047
48 // Check current version. Allow Version6 for now.
49 unsigned Version = DI.getVersion();
50 if (Version != LLVMDebugVersion && Version != LLVMDebugVersion6)
51 return false;
52
53 unsigned Tag = DI.getTag();
54 switch (Tag) {
55 case DW_TAG_variable:
Devang Patel82459882009-08-26 05:01:18 +000056 assert(DIVariable(GV).Verify() && "Invalid DebugInfo value");
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000057 break;
58 case DW_TAG_compile_unit:
Devang Patel82459882009-08-26 05:01:18 +000059 assert(DICompileUnit(GV).Verify() && "Invalid DebugInfo value");
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000060 break;
61 case DW_TAG_subprogram:
Devang Patel82459882009-08-26 05:01:18 +000062 assert(DISubprogram(GV).Verify() && "Invalid DebugInfo value");
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000063 break;
64 case DW_TAG_lexical_block:
Bill Wendlingdc817b62009-05-14 18:26:15 +000065 // FIXME: This interfers with the quality of generated code during
66 // optimization.
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000067 if (OptLevel != CodeGenOpt::None)
68 return false;
Bill Wendlingdc817b62009-05-14 18:26:15 +000069 // FALLTHROUGH
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000070 default:
71 break;
72 }
73
74 return true;
75}
76
Devang Patel82459882009-08-26 05:01:18 +000077DIDescriptor::DIDescriptor(GlobalVariable *GV, unsigned RequiredTag) {
78 DbgGV = GV;
Chris Lattnera45664f2008-11-10 02:56:27 +000079
Bill Wendlingdc817b62009-05-14 18:26:15 +000080 // If this is non-null, check to see if the Tag matches. If not, set to null.
Devang Patel82459882009-08-26 05:01:18 +000081 if (GV && getTag() != RequiredTag)
82 DbgGV = 0;
Chris Lattnera45664f2008-11-10 02:56:27 +000083}
84
Bill Wendling0582ae92009-03-13 04:39:26 +000085const std::string &
86DIDescriptor::getStringField(unsigned Elt, std::string &Result) const {
Devang Patel82459882009-08-26 05:01:18 +000087 if (DbgGV == 0) {
88 Result.clear();
Bill Wendling0582ae92009-03-13 04:39:26 +000089 return Result;
Devang Patel82459882009-08-26 05:01:18 +000090 }
Chris Lattnera45664f2008-11-10 02:56:27 +000091
Devang Patel82459882009-08-26 05:01:18 +000092 Constant *C = DbgGV->getInitializer();
93 if (C == 0 || Elt >= C->getNumOperands()) {
94 Result.clear();
95 return Result;
96 }
97
98 // Fills in the string if it succeeds
99 if (!GetConstantStringInfo(C->getOperand(Elt), Result))
100 Result.clear();
101
Bill Wendling0582ae92009-03-13 04:39:26 +0000102 return Result;
Chris Lattnera45664f2008-11-10 02:56:27 +0000103}
104
105uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
Devang Patel82459882009-08-26 05:01:18 +0000106 if (DbgGV == 0) return 0;
107 if (!DbgGV->hasInitializer()) return 0;
108
109 Constant *C = DbgGV->getInitializer();
110 if (C == 0 || Elt >= C->getNumOperands())
Chris Lattnera45664f2008-11-10 02:56:27 +0000111 return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000112
Devang Patel82459882009-08-26 05:01:18 +0000113 if (ConstantInt *CI = dyn_cast<ConstantInt>(C->getOperand(Elt)))
114 return CI->getZExtValue();
Chris Lattnera45664f2008-11-10 02:56:27 +0000115 return 0;
116}
117
Chris Lattnera45664f2008-11-10 02:56:27 +0000118DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
Devang Patel82459882009-08-26 05:01:18 +0000119 if (DbgGV == 0) return DIDescriptor();
120
121 Constant *C = DbgGV->getInitializer();
122 if (C == 0 || Elt >= C->getNumOperands())
Chris Lattnera45664f2008-11-10 02:56:27 +0000123 return DIDescriptor();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000124
Devang Patel82459882009-08-26 05:01:18 +0000125 C = C->getOperand(Elt);
126 return DIDescriptor(dyn_cast<GlobalVariable>(C->stripPointerCasts()));
Chris Lattnera45664f2008-11-10 02:56:27 +0000127}
128
129GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
Devang Patel82459882009-08-26 05:01:18 +0000130 if (DbgGV == 0) return 0;
131
132 Constant *C = DbgGV->getInitializer();
133 if (C == 0 || Elt >= C->getNumOperands())
Chris Lattnera45664f2008-11-10 02:56:27 +0000134 return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000135
Devang Patel82459882009-08-26 05:01:18 +0000136 C = C->getOperand(Elt);
137 return dyn_cast<GlobalVariable>(C->stripPointerCasts());
Chris Lattnera45664f2008-11-10 02:56:27 +0000138}
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 Patel82459882009-08-26 05:01:18 +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) &&
Devang Patel82459882009-08-26 05:01:18 +0000149 !DICompositeType::isCompositeType(tag))
150 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:
Devang Patel82459882009-08-26 05:01:18 +0000167 // 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;
Chris Lattnera45664f2008-11-10 02:56:27 +0000170 }
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 Patel82459882009-08-26 05:01:18 +0000203 assert (DbgGV && "Invalid DIArray");
204 Constant *C = DbgGV->getInitializer();
205 assert (C && "Invalid DIArray initializer");
206 return C->getNumOperands();
Devang Patel68afdc32009-01-05 18:33:01 +0000207}
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 Patel82459882009-08-26 05:01:18 +0000217 getGV()->replaceAllUsesWith(D.getGV());
218 getGV()->eraseFromParent();
Devang Patelc4999d72009-07-22 18:23:44 +0000219}
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 {
Devang Patel82459882009-08-26 05:01:18 +0000344 errs() << "[" << dwarf::TagString(getTag()) << "] [GV:";
345 errs().write_hex((intptr_t)DbgGV) << ']';
Devang Patel7136a652009-07-01 22:10:23 +0000346}
347
348/// dump - Print compile unit.
349void DICompileUnit::dump() const {
350 if (getLanguage())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000351 errs() << " [" << dwarf::LanguageString(getLanguage()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000352
353 std::string Res1, Res2;
Chris Lattnera81d29b2009-08-23 07:33:14 +0000354 errs() << " [" << getDirectory(Res1) << "/" << getFilename(Res2) << " ]";
Devang Patel7136a652009-07-01 22:10:23 +0000355}
356
357/// dump - Print type.
358void DIType::dump() const {
359 if (isNull()) return;
360
361 std::string Res;
362 if (!getName(Res).empty())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000363 errs() << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000364
365 unsigned Tag = getTag();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000366 errs() << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000367
368 // TODO : Print context
369 getCompileUnit().dump();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000370 errs() << " ["
371 << getLineNumber() << ", "
372 << getSizeInBits() << ", "
373 << getAlignInBits() << ", "
374 << getOffsetInBits()
375 << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000376
377 if (isPrivate())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000378 errs() << " [private] ";
Devang Patel7136a652009-07-01 22:10:23 +0000379 else if (isProtected())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000380 errs() << " [protected] ";
Devang Patel7136a652009-07-01 22:10:23 +0000381
382 if (isForwardDecl())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000383 errs() << " [fwd] ";
Devang Patel7136a652009-07-01 22:10:23 +0000384
385 if (isBasicType(Tag))
Devang Patel82459882009-08-26 05:01:18 +0000386 DIBasicType(DbgGV).dump();
Devang Patel7136a652009-07-01 22:10:23 +0000387 else if (isDerivedType(Tag))
Devang Patel82459882009-08-26 05:01:18 +0000388 DIDerivedType(DbgGV).dump();
Devang Patel7136a652009-07-01 22:10:23 +0000389 else if (isCompositeType(Tag))
Devang Patel82459882009-08-26 05:01:18 +0000390 DICompositeType(DbgGV).dump();
Devang Patel7136a652009-07-01 22:10:23 +0000391 else {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000392 errs() << "Invalid DIType\n";
Devang Patel7136a652009-07-01 22:10:23 +0000393 return;
394 }
395
Chris Lattnera81d29b2009-08-23 07:33:14 +0000396 errs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000397}
398
399/// dump - Print basic type.
400void DIBasicType::dump() const {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000401 errs() << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000402}
403
404/// dump - Print derived type.
405void DIDerivedType::dump() const {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000406 errs() << "\n\t Derived From: "; getTypeDerivedFrom().dump();
Devang Patel7136a652009-07-01 22:10:23 +0000407}
408
409/// dump - Print composite type.
410void DICompositeType::dump() const {
411 DIArray A = getTypeArray();
412 if (A.isNull())
413 return;
Chris Lattnera81d29b2009-08-23 07:33:14 +0000414 errs() << " [" << A.getNumElements() << " elements]";
Devang Patel7136a652009-07-01 22:10:23 +0000415}
416
417/// dump - Print global.
418void DIGlobal::dump() const {
419 std::string Res;
420 if (!getName(Res).empty())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000421 errs() << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000422
423 unsigned Tag = getTag();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000424 errs() << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000425
426 // TODO : Print context
427 getCompileUnit().dump();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000428 errs() << " [" << getLineNumber() << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000429
430 if (isLocalToUnit())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000431 errs() << " [local] ";
Devang Patel7136a652009-07-01 22:10:23 +0000432
433 if (isDefinition())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000434 errs() << " [def] ";
Devang Patel7136a652009-07-01 22:10:23 +0000435
436 if (isGlobalVariable(Tag))
Devang Patel82459882009-08-26 05:01:18 +0000437 DIGlobalVariable(DbgGV).dump();
Devang Patel7136a652009-07-01 22:10:23 +0000438
Chris Lattnera81d29b2009-08-23 07:33:14 +0000439 errs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000440}
441
442/// dump - Print subprogram.
443void DISubprogram::dump() const {
444 DIGlobal::dump();
445}
446
447/// dump - Print global variable.
448void DIGlobalVariable::dump() const {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000449 errs() << " [";
450 getGlobal()->dump();
451 errs() << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000452}
453
454/// dump - Print variable.
455void DIVariable::dump() const {
456 std::string Res;
457 if (!getName(Res).empty())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000458 errs() << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000459
460 getCompileUnit().dump();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000461 errs() << " [" << getLineNumber() << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000462 getType().dump();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000463 errs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000464}
465
466//===----------------------------------------------------------------------===//
Chris Lattnera45664f2008-11-10 02:56:27 +0000467// DIFactory: Basic Helpers
468//===----------------------------------------------------------------------===//
469
Bill Wendlingdc817b62009-05-14 18:26:15 +0000470DIFactory::DIFactory(Module &m)
Owen Anderson99035272009-07-07 17:12:53 +0000471 : M(m), VMContext(M.getContext()), StopPointFn(0), FuncStartFn(0),
472 RegionStartFn(0), RegionEndFn(0),
Bill Wendlingdc817b62009-05-14 18:26:15 +0000473 DeclareFn(0) {
Owen Andersond7f2a6c2009-08-05 23:16:16 +0000474 EmptyStructPtr = PointerType::getUnqual(StructType::get(VMContext));
Chris Lattner497a7a82008-11-10 04:10:34 +0000475}
476
Devang Patel82459882009-08-26 05:01:18 +0000477/// getCastToEmpty - Return this descriptor as a Constant* with type '{}*'.
478/// This is only valid when the descriptor is non-null.
479Constant *DIFactory::getCastToEmpty(DIDescriptor D) {
480 if (D.isNull()) return llvm::Constant::getNullValue(EmptyStructPtr);
481 return ConstantExpr::getBitCast(D.getGV(), EmptyStructPtr);
482}
483
Chris Lattnera45664f2008-11-10 02:56:27 +0000484Constant *DIFactory::GetTagConstant(unsigned TAG) {
Devang Patel6906ba52009-01-20 19:22:03 +0000485 assert((TAG & LLVMDebugVersionMask) == 0 &&
Chris Lattnera45664f2008-11-10 02:56:27 +0000486 "Tag too large for debug encoding!");
Owen Anderson1d0be152009-08-13 21:58:54 +0000487 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
Chris Lattnera45664f2008-11-10 02:56:27 +0000488}
489
Devang Patel82459882009-08-26 05:01:18 +0000490Constant *DIFactory::GetStringConstant(const std::string &String) {
491 // Check string cache for previous edition.
492 Constant *&Slot = StringCache[String];
493
494 // Return Constant if previously defined.
495 if (Slot) return Slot;
496
497 const PointerType *DestTy = PointerType::getUnqual(Type::getInt8Ty(VMContext));
498
499 // If empty string then use a i8* null instead.
500 if (String.empty())
501 return Slot = ConstantPointerNull::get(DestTy);
502
503 // Construct string as an llvm constant.
504 Constant *ConstStr = ConstantArray::get(VMContext, String);
505
506 // Otherwise create and return a new string global.
507 GlobalVariable *StrGV = new GlobalVariable(M, ConstStr->getType(), true,
508 GlobalVariable::InternalLinkage,
509 ConstStr, ".str");
510 StrGV->setSection("llvm.metadata");
511 return Slot = ConstantExpr::getBitCast(StrGV, DestTy);
512}
513
Chris Lattnera45664f2008-11-10 02:56:27 +0000514//===----------------------------------------------------------------------===//
515// DIFactory: Primary Constructors
516//===----------------------------------------------------------------------===//
517
Chris Lattnera45664f2008-11-10 02:56:27 +0000518/// GetOrCreateArray - Create an descriptor for an array of descriptors.
519/// This implicitly uniques the arrays created.
520DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
Devang Patel82459882009-08-26 05:01:18 +0000521 SmallVector<Constant*, 16> Elts;
Chris Lattnera45664f2008-11-10 02:56:27 +0000522
Devang Patel82459882009-08-26 05:01:18 +0000523 for (unsigned i = 0; i != NumTys; ++i)
524 Elts.push_back(getCastToEmpty(Tys[i]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000525
Devang Patel82459882009-08-26 05:01:18 +0000526 Constant *Init = ConstantArray::get(ArrayType::get(EmptyStructPtr,
527 Elts.size()),
528 Elts.data(), Elts.size());
529 // If we already have this array, just return the uniqued version.
530 DIDescriptor &Entry = SimpleConstantCache[Init];
531 if (!Entry.isNull()) return DIArray(Entry.getGV());
532
533 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
534 GlobalValue::InternalLinkage,
535 Init, "llvm.dbg.array");
536 GV->setSection("llvm.metadata");
537 Entry = DIDescriptor(GV);
538 return DIArray(GV);
Chris Lattnera45664f2008-11-10 02:56:27 +0000539}
540
541/// GetOrCreateSubrange - Create a descriptor for a value range. This
542/// implicitly uniques the values returned.
543DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
Devang Patel82459882009-08-26 05:01:18 +0000544 Constant *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000545 GetTagConstant(dwarf::DW_TAG_subrange_type),
Owen Anderson1d0be152009-08-13 21:58:54 +0000546 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
547 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
Chris Lattnera45664f2008-11-10 02:56:27 +0000548 };
549
Devang Patel82459882009-08-26 05:01:18 +0000550 Constant *Init = ConstantStruct::get(VMContext, Elts,
551 sizeof(Elts)/sizeof(Elts[0]));
552
553 // If we already have this range, just return the uniqued version.
554 DIDescriptor &Entry = SimpleConstantCache[Init];
555 if (!Entry.isNull()) return DISubrange(Entry.getGV());
556
557 M.addTypeName("llvm.dbg.subrange.type", Init->getType());
558
559 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
560 GlobalValue::InternalLinkage,
561 Init, "llvm.dbg.subrange");
562 GV->setSection("llvm.metadata");
563 Entry = DIDescriptor(GV);
564 return DISubrange(GV);
Chris Lattnera45664f2008-11-10 02:56:27 +0000565}
566
567
568
569/// CreateCompileUnit - Create a new descriptor for the specified compile
570/// unit. Note that this does not unique compile units within the module.
571DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
572 const std::string &Filename,
573 const std::string &Directory,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000574 const std::string &Producer,
Devang Pateldd9db662009-01-30 18:20:31 +0000575 bool isMain,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000576 bool isOptimized,
Devang Patel13319ce2009-02-17 22:43:44 +0000577 const char *Flags,
578 unsigned RunTimeVer) {
Devang Patel82459882009-08-26 05:01:18 +0000579 Constant *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000580 GetTagConstant(dwarf::DW_TAG_compile_unit),
Devang Patel82459882009-08-26 05:01:18 +0000581 llvm::Constant::getNullValue(EmptyStructPtr),
Owen Anderson1d0be152009-08-13 21:58:54 +0000582 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
Devang Patel82459882009-08-26 05:01:18 +0000583 GetStringConstant(Filename),
584 GetStringConstant(Directory),
585 GetStringConstant(Producer),
Owen Anderson1d0be152009-08-13 21:58:54 +0000586 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
587 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
Devang Patel82459882009-08-26 05:01:18 +0000588 GetStringConstant(Flags),
Owen Anderson1d0be152009-08-13 21:58:54 +0000589 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
Chris Lattnera45664f2008-11-10 02:56:27 +0000590 };
Devang Patel82459882009-08-26 05:01:18 +0000591
592 Constant *Init = ConstantStruct::get(VMContext, Elts,
593 sizeof(Elts)/sizeof(Elts[0]));
594
595 M.addTypeName("llvm.dbg.compile_unit.type", Init->getType());
596 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
597 GlobalValue::InternalLinkage,
598 Init, "llvm.dbg.compile_unit");
599 GV->setSection("llvm.metadata");
600 return DICompileUnit(GV);
Chris Lattnera45664f2008-11-10 02:56:27 +0000601}
602
603/// CreateEnumerator - Create a single enumerator value.
604DIEnumerator DIFactory::CreateEnumerator(const std::string &Name, uint64_t Val){
Devang Patel82459882009-08-26 05:01:18 +0000605 Constant *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000606 GetTagConstant(dwarf::DW_TAG_enumerator),
Devang Patel82459882009-08-26 05:01:18 +0000607 GetStringConstant(Name),
Owen Anderson1d0be152009-08-13 21:58:54 +0000608 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
Chris Lattnera45664f2008-11-10 02:56:27 +0000609 };
Devang Patel82459882009-08-26 05:01:18 +0000610
611 Constant *Init = ConstantStruct::get(VMContext, Elts,
612 sizeof(Elts)/sizeof(Elts[0]));
613
614 M.addTypeName("llvm.dbg.enumerator.type", Init->getType());
615 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
616 GlobalValue::InternalLinkage,
617 Init, "llvm.dbg.enumerator");
618 GV->setSection("llvm.metadata");
619 return DIEnumerator(GV);
Chris Lattnera45664f2008-11-10 02:56:27 +0000620}
621
622
623/// CreateBasicType - Create a basic type like int, float, etc.
624DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
Bill Wendling0582ae92009-03-13 04:39:26 +0000625 const std::string &Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000626 DICompileUnit CompileUnit,
627 unsigned LineNumber,
628 uint64_t SizeInBits,
629 uint64_t AlignInBits,
630 uint64_t OffsetInBits, unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000631 unsigned Encoding) {
Devang Patel82459882009-08-26 05:01:18 +0000632 Constant *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000633 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patel82459882009-08-26 05:01:18 +0000634 getCastToEmpty(Context),
635 GetStringConstant(Name),
636 getCastToEmpty(CompileUnit),
Owen Anderson1d0be152009-08-13 21:58:54 +0000637 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
638 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
639 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
640 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
641 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
642 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
Chris Lattnera45664f2008-11-10 02:56:27 +0000643 };
644
Devang Patel82459882009-08-26 05:01:18 +0000645 Constant *Init = ConstantStruct::get(VMContext, Elts,
646 sizeof(Elts)/sizeof(Elts[0]));
647
648 M.addTypeName("llvm.dbg.basictype.type", Init->getType());
649 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
650 GlobalValue::InternalLinkage,
651 Init, "llvm.dbg.basictype");
652 GV->setSection("llvm.metadata");
653 return DIBasicType(GV);
Chris Lattnera45664f2008-11-10 02:56:27 +0000654}
655
656/// CreateDerivedType - Create a derived type like const qualified type,
657/// pointer, typedef, etc.
658DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
659 DIDescriptor Context,
660 const std::string &Name,
661 DICompileUnit CompileUnit,
662 unsigned LineNumber,
663 uint64_t SizeInBits,
664 uint64_t AlignInBits,
665 uint64_t OffsetInBits,
666 unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000667 DIType DerivedFrom) {
Devang Patel82459882009-08-26 05:01:18 +0000668 Constant *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000669 GetTagConstant(Tag),
Devang Patel82459882009-08-26 05:01:18 +0000670 getCastToEmpty(Context),
671 GetStringConstant(Name),
672 getCastToEmpty(CompileUnit),
Owen Anderson1d0be152009-08-13 21:58:54 +0000673 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
674 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
675 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
676 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
677 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel82459882009-08-26 05:01:18 +0000678 getCastToEmpty(DerivedFrom)
Chris Lattnera45664f2008-11-10 02:56:27 +0000679 };
Devang Patel82459882009-08-26 05:01:18 +0000680
681 Constant *Init = ConstantStruct::get(VMContext, Elts,
682 sizeof(Elts)/sizeof(Elts[0]));
683
684 M.addTypeName("llvm.dbg.derivedtype.type", Init->getType());
685 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
686 GlobalValue::InternalLinkage,
687 Init, "llvm.dbg.derivedtype");
688 GV->setSection("llvm.metadata");
689 return DIDerivedType(GV);
Chris Lattnera45664f2008-11-10 02:56:27 +0000690}
691
692/// CreateCompositeType - Create a composite type like array, struct, etc.
693DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
694 DIDescriptor Context,
695 const std::string &Name,
696 DICompileUnit CompileUnit,
697 unsigned LineNumber,
698 uint64_t SizeInBits,
699 uint64_t AlignInBits,
700 uint64_t OffsetInBits,
701 unsigned Flags,
702 DIType DerivedFrom,
Devang Patel13319ce2009-02-17 22:43:44 +0000703 DIArray Elements,
704 unsigned RuntimeLang) {
Owen Andersone277fed2009-07-07 16:31:25 +0000705
Devang Patel82459882009-08-26 05:01:18 +0000706 Constant *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000707 GetTagConstant(Tag),
Devang Patel82459882009-08-26 05:01:18 +0000708 getCastToEmpty(Context),
709 GetStringConstant(Name),
710 getCastToEmpty(CompileUnit),
Owen Anderson1d0be152009-08-13 21:58:54 +0000711 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
712 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
713 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
714 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
715 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel82459882009-08-26 05:01:18 +0000716 getCastToEmpty(DerivedFrom),
717 getCastToEmpty(Elements),
Owen Anderson1d0be152009-08-13 21:58:54 +0000718 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
Chris Lattnera45664f2008-11-10 02:56:27 +0000719 };
Devang Patel82459882009-08-26 05:01:18 +0000720
721 Constant *Init = ConstantStruct::get(VMContext, Elts,
722 sizeof(Elts)/sizeof(Elts[0]));
723
724 M.addTypeName("llvm.dbg.composite.type", Init->getType());
725 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
726 GlobalValue::InternalLinkage,
727 Init, "llvm.dbg.composite");
728 GV->setSection("llvm.metadata");
729 return DICompositeType(GV);
Chris Lattnera45664f2008-11-10 02:56:27 +0000730}
731
732
733/// CreateSubprogram - Create a new descriptor for the specified subprogram.
734/// See comments in DISubprogram for descriptions of these fields. This
735/// method does not unique the generated descriptors.
736DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
737 const std::string &Name,
738 const std::string &DisplayName,
739 const std::string &LinkageName,
740 DICompileUnit CompileUnit,
741 unsigned LineNo, DIType Type,
742 bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000743 bool isDefinition) {
Devang Patel854967e2008-12-17 22:39:29 +0000744
Devang Patel82459882009-08-26 05:01:18 +0000745 Constant *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000746 GetTagConstant(dwarf::DW_TAG_subprogram),
Devang Patel82459882009-08-26 05:01:18 +0000747 llvm::Constant::getNullValue(EmptyStructPtr),
748 getCastToEmpty(Context),
749 GetStringConstant(Name),
750 GetStringConstant(DisplayName),
751 GetStringConstant(LinkageName),
752 getCastToEmpty(CompileUnit),
Owen Anderson1d0be152009-08-13 21:58:54 +0000753 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel82459882009-08-26 05:01:18 +0000754 getCastToEmpty(Type),
Owen Anderson1d0be152009-08-13 21:58:54 +0000755 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
756 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition)
Chris Lattnera45664f2008-11-10 02:56:27 +0000757 };
758
Devang Patel82459882009-08-26 05:01:18 +0000759 Constant *Init = ConstantStruct::get(VMContext, Elts,
760 sizeof(Elts)/sizeof(Elts[0]));
761
762 M.addTypeName("llvm.dbg.subprogram.type", Init->getType());
763 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
764 GlobalValue::InternalLinkage,
765 Init, "llvm.dbg.subprogram");
766 GV->setSection("llvm.metadata");
767 return DISubprogram(GV);
Chris Lattnera45664f2008-11-10 02:56:27 +0000768}
769
770/// CreateGlobalVariable - Create a new descriptor for the specified global.
771DIGlobalVariable
772DIFactory::CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
773 const std::string &DisplayName,
774 const std::string &LinkageName,
775 DICompileUnit CompileUnit,
776 unsigned LineNo, DIType Type,bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000777 bool isDefinition, llvm::GlobalVariable *Val) {
Devang Patel82459882009-08-26 05:01:18 +0000778 Constant *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000779 GetTagConstant(dwarf::DW_TAG_variable),
Devang Patel82459882009-08-26 05:01:18 +0000780 llvm::Constant::getNullValue(EmptyStructPtr),
781 getCastToEmpty(Context),
782 GetStringConstant(Name),
783 GetStringConstant(DisplayName),
784 GetStringConstant(LinkageName),
785 getCastToEmpty(CompileUnit),
Owen Anderson1d0be152009-08-13 21:58:54 +0000786 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel82459882009-08-26 05:01:18 +0000787 getCastToEmpty(Type),
Owen Anderson1d0be152009-08-13 21:58:54 +0000788 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
789 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
Devang Patel82459882009-08-26 05:01:18 +0000790 ConstantExpr::getBitCast(Val, EmptyStructPtr)
Chris Lattnera45664f2008-11-10 02:56:27 +0000791 };
Devang Patel82459882009-08-26 05:01:18 +0000792
793 Constant *Init = ConstantStruct::get(VMContext, Elts,
794 sizeof(Elts)/sizeof(Elts[0]));
795
796 M.addTypeName("llvm.dbg.global_variable.type", Init->getType());
797 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
798 GlobalValue::LinkOnceAnyLinkage,
799 Init, "llvm.dbg.global_variable");
800 GV->setSection("llvm.metadata");
801 return DIGlobalVariable(GV);
Chris Lattnera45664f2008-11-10 02:56:27 +0000802}
803
804
805/// CreateVariable - Create a new descriptor for the specified variable.
806DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
807 const std::string &Name,
808 DICompileUnit CompileUnit, unsigned LineNo,
Devang Pateldd9db662009-01-30 18:20:31 +0000809 DIType Type) {
Devang Patel82459882009-08-26 05:01:18 +0000810 Constant *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000811 GetTagConstant(Tag),
Devang Patel82459882009-08-26 05:01:18 +0000812 getCastToEmpty(Context),
813 GetStringConstant(Name),
814 getCastToEmpty(CompileUnit),
Owen Anderson1d0be152009-08-13 21:58:54 +0000815 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel82459882009-08-26 05:01:18 +0000816 getCastToEmpty(Type)
Chris Lattnera45664f2008-11-10 02:56:27 +0000817 };
818
Devang Patel82459882009-08-26 05:01:18 +0000819 Constant *Init = ConstantStruct::get(VMContext, Elts,
820 sizeof(Elts)/sizeof(Elts[0]));
821
822 M.addTypeName("llvm.dbg.variable.type", Init->getType());
823 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
824 GlobalValue::InternalLinkage,
825 Init, "llvm.dbg.variable");
826 GV->setSection("llvm.metadata");
827 return DIVariable(GV);
Chris Lattnera45664f2008-11-10 02:56:27 +0000828}
829
830
831/// CreateBlock - This creates a descriptor for a lexical block with the
Owen Anderson99035272009-07-07 17:12:53 +0000832/// specified parent VMContext.
Chris Lattnera45664f2008-11-10 02:56:27 +0000833DIBlock DIFactory::CreateBlock(DIDescriptor Context) {
Devang Patel82459882009-08-26 05:01:18 +0000834 Constant *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000835 GetTagConstant(dwarf::DW_TAG_lexical_block),
Devang Patel82459882009-08-26 05:01:18 +0000836 getCastToEmpty(Context)
Chris Lattnera45664f2008-11-10 02:56:27 +0000837 };
Devang Patel82459882009-08-26 05:01:18 +0000838
839 Constant *Init = ConstantStruct::get(VMContext, Elts,
840 sizeof(Elts)/sizeof(Elts[0]));
841
842 M.addTypeName("llvm.dbg.block.type", Init->getType());
843 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
844 GlobalValue::InternalLinkage,
845 Init, "llvm.dbg.block");
846 GV->setSection("llvm.metadata");
847 return DIBlock(GV);
Chris Lattnera45664f2008-11-10 02:56:27 +0000848}
849
850
851//===----------------------------------------------------------------------===//
852// DIFactory: Routines for inserting code into a function
853//===----------------------------------------------------------------------===//
854
855/// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
856/// inserting it at the end of the specified basic block.
857void DIFactory::InsertStopPoint(DICompileUnit CU, unsigned LineNo,
858 unsigned ColNo, BasicBlock *BB) {
859
860 // Lazily construct llvm.dbg.stoppoint function.
861 if (!StopPointFn)
862 StopPointFn = llvm::Intrinsic::getDeclaration(&M,
863 llvm::Intrinsic::dbg_stoppoint);
864
865 // Invoke llvm.dbg.stoppoint
866 Value *Args[] = {
Owen Anderson1d0be152009-08-13 21:58:54 +0000867 ConstantInt::get(llvm::Type::getInt32Ty(VMContext), LineNo),
868 ConstantInt::get(llvm::Type::getInt32Ty(VMContext), ColNo),
Devang Patel82459882009-08-26 05:01:18 +0000869 getCastToEmpty(CU)
Chris Lattnera45664f2008-11-10 02:56:27 +0000870 };
871 CallInst::Create(StopPointFn, Args, Args+3, "", BB);
872}
873
874/// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
875/// mark the start of the specified subprogram.
876void DIFactory::InsertSubprogramStart(DISubprogram SP, BasicBlock *BB) {
877 // Lazily construct llvm.dbg.func.start.
878 if (!FuncStartFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000879 FuncStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_func_start);
Chris Lattnera45664f2008-11-10 02:56:27 +0000880
881 // Call llvm.dbg.func.start which also implicitly sets a stoppoint.
Devang Patel82459882009-08-26 05:01:18 +0000882 CallInst::Create(FuncStartFn, getCastToEmpty(SP), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000883}
884
885/// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
886/// mark the start of a region for the specified scoping descriptor.
887void DIFactory::InsertRegionStart(DIDescriptor D, BasicBlock *BB) {
888 // Lazily construct llvm.dbg.region.start function.
889 if (!RegionStartFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000890 RegionStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_start);
891
Chris Lattnera45664f2008-11-10 02:56:27 +0000892 // Call llvm.dbg.func.start.
Devang Patel82459882009-08-26 05:01:18 +0000893 CallInst::Create(RegionStartFn, getCastToEmpty(D), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000894}
895
Chris Lattnera45664f2008-11-10 02:56:27 +0000896/// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
897/// mark the end of a region for the specified scoping descriptor.
898void DIFactory::InsertRegionEnd(DIDescriptor D, BasicBlock *BB) {
899 // Lazily construct llvm.dbg.region.end function.
900 if (!RegionEndFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000901 RegionEndFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_end);
902
903 // Call llvm.dbg.region.end.
Devang Patel82459882009-08-26 05:01:18 +0000904 CallInst::Create(RegionEndFn, getCastToEmpty(D), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000905}
906
907/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Bill Wendlingdc817b62009-05-14 18:26:15 +0000908void DIFactory::InsertDeclare(Value *Storage, DIVariable D, BasicBlock *BB) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000909 // Cast the storage to a {}* for the call to llvm.dbg.declare.
Bill Wendlingdc817b62009-05-14 18:26:15 +0000910 Storage = new BitCastInst(Storage, EmptyStructPtr, "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000911
912 if (!DeclareFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000913 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
914
Devang Patel82459882009-08-26 05:01:18 +0000915 Value *Args[] = { Storage, getCastToEmpty(D) };
Chris Lattnera45664f2008-11-10 02:56:27 +0000916 CallInst::Create(DeclareFn, Args, Args+2, "", BB);
917}
Torok Edwin620f2802008-12-16 09:07:36 +0000918
Devang Pateld2f79a12009-07-28 19:55:13 +0000919//===----------------------------------------------------------------------===//
Devang Patel98c65172009-07-30 18:25:15 +0000920// DebugInfoFinder implementations.
Devang Pateld2f79a12009-07-28 19:55:13 +0000921//===----------------------------------------------------------------------===//
922
Devang Patel98c65172009-07-30 18:25:15 +0000923/// processModule - Process entire module and collect debug info.
924void DebugInfoFinder::processModule(Module &M) {
Devang Patel82459882009-08-26 05:01:18 +0000925
Devang Pateld2f79a12009-07-28 19:55:13 +0000926 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
927 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
928 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
929 ++BI) {
930 if (DbgStopPointInst *SPI = dyn_cast<DbgStopPointInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +0000931 processStopPoint(SPI);
Devang Pateld2f79a12009-07-28 19:55:13 +0000932 else if (DbgFuncStartInst *FSI = dyn_cast<DbgFuncStartInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +0000933 processFuncStart(FSI);
Devang Patele802f1c2009-07-30 17:30:23 +0000934 else if (DbgRegionStartInst *DRS = dyn_cast<DbgRegionStartInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +0000935 processRegionStart(DRS);
Devang Patele802f1c2009-07-30 17:30:23 +0000936 else if (DbgRegionEndInst *DRE = dyn_cast<DbgRegionEndInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +0000937 processRegionEnd(DRE);
Devang Patelb4d31302009-07-31 18:18:52 +0000938 else if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
939 processDeclare(DDI);
Devang Pateld2f79a12009-07-28 19:55:13 +0000940 }
Devang Patel82459882009-08-26 05:01:18 +0000941
942 for (Module::global_iterator GVI = M.global_begin(), GVE = M.global_end();
943 GVI != GVE; ++GVI) {
944 GlobalVariable *GV = GVI;
945 if (!GV->hasName() || !GV->isConstant()
946 || strncmp(GV->getName().data(), "llvm.dbg.global_variable", 24)
947 || !GV->hasInitializer())
948 continue;
949 DIGlobalVariable DIG(GV);
Devang Pateld2f79a12009-07-28 19:55:13 +0000950 if (addGlobalVariable(DIG)) {
951 addCompileUnit(DIG.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +0000952 processType(DIG.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +0000953 }
954 }
955}
956
Devang Patel98c65172009-07-30 18:25:15 +0000957/// processType - Process DIType.
958void DebugInfoFinder::processType(DIType DT) {
Devang Patel72bcdb62009-08-10 22:09:58 +0000959 if (!addType(DT))
Devang Pateld2f79a12009-07-28 19:55:13 +0000960 return;
961
962 addCompileUnit(DT.getCompileUnit());
963 if (DT.isCompositeType(DT.getTag())) {
Devang Patel82459882009-08-26 05:01:18 +0000964 DICompositeType DCT(DT.getGV());
Devang Patel98c65172009-07-30 18:25:15 +0000965 processType(DCT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +0000966 DIArray DA = DCT.getTypeArray();
967 if (!DA.isNull())
968 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
969 DIDescriptor D = DA.getElement(i);
Devang Patel82459882009-08-26 05:01:18 +0000970 DIType TypeE = DIType(D.getGV());
Devang Pateld2f79a12009-07-28 19:55:13 +0000971 if (!TypeE.isNull())
Devang Patel98c65172009-07-30 18:25:15 +0000972 processType(TypeE);
Devang Pateld2f79a12009-07-28 19:55:13 +0000973 else
Devang Patel82459882009-08-26 05:01:18 +0000974 processSubprogram(DISubprogram(D.getGV()));
Devang Pateld2f79a12009-07-28 19:55:13 +0000975 }
976 } else if (DT.isDerivedType(DT.getTag())) {
Devang Patel82459882009-08-26 05:01:18 +0000977 DIDerivedType DDT(DT.getGV());
Devang Pateld2f79a12009-07-28 19:55:13 +0000978 if (!DDT.isNull())
Devang Patel98c65172009-07-30 18:25:15 +0000979 processType(DDT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +0000980 }
981}
982
Devang Patel98c65172009-07-30 18:25:15 +0000983/// processSubprogram - Process DISubprogram.
984void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Patele802f1c2009-07-30 17:30:23 +0000985 if (SP.isNull())
986 return;
Devang Pateld2f79a12009-07-28 19:55:13 +0000987 if (!addSubprogram(SP))
988 return;
989 addCompileUnit(SP.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +0000990 processType(SP.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +0000991}
992
Devang Patel98c65172009-07-30 18:25:15 +0000993/// processStopPoint - Process DbgStopPointInst.
994void DebugInfoFinder::processStopPoint(DbgStopPointInst *SPI) {
Devang Patel82459882009-08-26 05:01:18 +0000995 GlobalVariable *Context = dyn_cast<GlobalVariable>(SPI->getContext());
Devang Pateld2f79a12009-07-28 19:55:13 +0000996 addCompileUnit(DICompileUnit(Context));
997}
998
Devang Patel98c65172009-07-30 18:25:15 +0000999/// processFuncStart - Process DbgFuncStartInst.
1000void DebugInfoFinder::processFuncStart(DbgFuncStartInst *FSI) {
Devang Patel82459882009-08-26 05:01:18 +00001001 GlobalVariable *SP = dyn_cast<GlobalVariable>(FSI->getSubprogram());
Devang Patel98c65172009-07-30 18:25:15 +00001002 processSubprogram(DISubprogram(SP));
Devang Pateld2f79a12009-07-28 19:55:13 +00001003}
1004
Devang Patel98c65172009-07-30 18:25:15 +00001005/// processRegionStart - Process DbgRegionStart.
1006void DebugInfoFinder::processRegionStart(DbgRegionStartInst *DRS) {
Devang Patel82459882009-08-26 05:01:18 +00001007 GlobalVariable *SP = dyn_cast<GlobalVariable>(DRS->getContext());
Devang Patel98c65172009-07-30 18:25:15 +00001008 processSubprogram(DISubprogram(SP));
Devang Patele802f1c2009-07-30 17:30:23 +00001009}
1010
Devang Patel98c65172009-07-30 18:25:15 +00001011/// processRegionEnd - Process DbgRegionEnd.
1012void DebugInfoFinder::processRegionEnd(DbgRegionEndInst *DRE) {
Devang Patel82459882009-08-26 05:01:18 +00001013 GlobalVariable *SP = dyn_cast<GlobalVariable>(DRE->getContext());
Devang Patel98c65172009-07-30 18:25:15 +00001014 processSubprogram(DISubprogram(SP));
Devang Patele802f1c2009-07-30 17:30:23 +00001015}
1016
Devang Patelb4d31302009-07-31 18:18:52 +00001017/// processDeclare - Process DbgDeclareInst.
1018void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
Devang Patel82459882009-08-26 05:01:18 +00001019 DIVariable DV(cast<GlobalVariable>(DDI->getVariable()));
Devang Patelb4d31302009-07-31 18:18:52 +00001020 if (DV.isNull())
1021 return;
1022
Devang Patel82459882009-08-26 05:01:18 +00001023 if (!NodesSeen.insert(DV.getGV()))
Devang Patelb4d31302009-07-31 18:18:52 +00001024 return;
1025
1026 addCompileUnit(DV.getCompileUnit());
1027 processType(DV.getType());
1028}
1029
Devang Patel72bcdb62009-08-10 22:09:58 +00001030/// addType - Add type into Tys.
1031bool DebugInfoFinder::addType(DIType DT) {
1032 if (DT.isNull())
1033 return false;
1034
Devang Patel82459882009-08-26 05:01:18 +00001035 if (!NodesSeen.insert(DT.getGV()))
Devang Patel72bcdb62009-08-10 22:09:58 +00001036 return false;
1037
Devang Patel82459882009-08-26 05:01:18 +00001038 TYs.push_back(DT.getGV());
Devang Patel72bcdb62009-08-10 22:09:58 +00001039 return true;
1040}
1041
Devang Pateld2f79a12009-07-28 19:55:13 +00001042/// addCompileUnit - Add compile unit into CUs.
Devang Patel98c65172009-07-30 18:25:15 +00001043bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001044 if (CU.isNull())
1045 return false;
1046
Devang Patel82459882009-08-26 05:01:18 +00001047 if (!NodesSeen.insert(CU.getGV()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001048 return false;
1049
Devang Patel82459882009-08-26 05:01:18 +00001050 CUs.push_back(CU.getGV());
Devang Pateld2f79a12009-07-28 19:55:13 +00001051 return true;
1052}
1053
1054/// addGlobalVariable - Add global variable into GVs.
Devang Patel98c65172009-07-30 18:25:15 +00001055bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001056 if (DIG.isNull())
1057 return false;
1058
Devang Patel82459882009-08-26 05:01:18 +00001059 if (!NodesSeen.insert(DIG.getGV()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001060 return false;
1061
Devang Patel82459882009-08-26 05:01:18 +00001062 GVs.push_back(DIG.getGV());
Devang Pateld2f79a12009-07-28 19:55:13 +00001063 return true;
1064}
1065
1066// addSubprogram - Add subprgoram into SPs.
Devang Patel98c65172009-07-30 18:25:15 +00001067bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001068 if (SP.isNull())
1069 return false;
1070
Devang Patel82459882009-08-26 05:01:18 +00001071 if (!NodesSeen.insert(SP.getGV()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001072 return false;
1073
Devang Patel82459882009-08-26 05:01:18 +00001074 SPs.push_back(SP.getGV());
Devang Pateld2f79a12009-07-28 19:55:13 +00001075 return true;
1076}
1077
Torok Edwin620f2802008-12-16 09:07:36 +00001078namespace llvm {
Bill Wendlingdc817b62009-05-14 18:26:15 +00001079 /// findStopPoint - Find the stoppoint coressponding to this instruction, that
1080 /// is the stoppoint that dominates this instruction.
1081 const DbgStopPointInst *findStopPoint(const Instruction *Inst) {
Torok Edwin620f2802008-12-16 09:07:36 +00001082 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(Inst))
1083 return DSI;
1084
1085 const BasicBlock *BB = Inst->getParent();
1086 BasicBlock::const_iterator I = Inst, B;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001087 while (BB) {
Torok Edwin620f2802008-12-16 09:07:36 +00001088 B = BB->begin();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001089
Torok Edwin620f2802008-12-16 09:07:36 +00001090 // A BB consisting only of a terminator can't have a stoppoint.
Bill Wendlingdc817b62009-05-14 18:26:15 +00001091 while (I != B) {
1092 --I;
1093 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
1094 return DSI;
Torok Edwin620f2802008-12-16 09:07:36 +00001095 }
Bill Wendlingdc817b62009-05-14 18:26:15 +00001096
1097 // This BB didn't have a stoppoint: if there is only one predecessor, look
1098 // for a stoppoint there. We could use getIDom(), but that would require
1099 // dominator info.
Torok Edwin620f2802008-12-16 09:07:36 +00001100 BB = I->getParent()->getUniquePredecessor();
1101 if (BB)
1102 I = BB->getTerminator();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001103 }
1104
Torok Edwin620f2802008-12-16 09:07:36 +00001105 return 0;
1106 }
1107
Bill Wendlingdc817b62009-05-14 18:26:15 +00001108 /// findBBStopPoint - Find the stoppoint corresponding to first real
1109 /// (non-debug intrinsic) instruction in this Basic Block, and return the
1110 /// stoppoint for it.
1111 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB) {
1112 for(BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001113 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
1114 return DSI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001115
1116 // Fallback to looking for stoppoint of unique predecessor. Useful if this
1117 // BB contains no stoppoints, but unique predecessor does.
Torok Edwin620f2802008-12-16 09:07:36 +00001118 BB = BB->getUniquePredecessor();
1119 if (BB)
1120 return findStopPoint(BB->getTerminator());
Bill Wendlingdc817b62009-05-14 18:26:15 +00001121
Torok Edwin620f2802008-12-16 09:07:36 +00001122 return 0;
1123 }
1124
Bill Wendlingdc817b62009-05-14 18:26:15 +00001125 Value *findDbgGlobalDeclare(GlobalVariable *V) {
Torok Edwinff7d0e92009-03-10 13:41:26 +00001126 const Module *M = V->getParent();
Owen Anderson99035272009-07-07 17:12:53 +00001127
Devang Patel82459882009-08-26 05:01:18 +00001128 const Type *Ty = M->getTypeByName("llvm.dbg.global_variable.type");
1129 if (!Ty) return 0;
1130
1131 Ty = PointerType::get(Ty, 0);
1132
1133 Value *Val = V->stripPointerCasts();
1134 for (Value::use_iterator I = Val->use_begin(), E = Val->use_end();
1135 I != E; ++I) {
1136 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(I)) {
1137 if (CE->getOpcode() == Instruction::BitCast) {
1138 Value *VV = CE;
1139
1140 while (VV->hasOneUse())
1141 VV = *VV->use_begin();
1142
1143 if (VV->getType() == Ty)
1144 return VV;
1145 }
1146 }
Torok Edwinff7d0e92009-03-10 13:41:26 +00001147 }
Devang Patel82459882009-08-26 05:01:18 +00001148
1149 if (Val->getType() == Ty)
1150 return Val;
1151
Torok Edwinff7d0e92009-03-10 13:41:26 +00001152 return 0;
1153 }
1154
Bill Wendlingdc817b62009-05-14 18:26:15 +00001155 /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
Torok Edwin620f2802008-12-16 09:07:36 +00001156 /// It looks through pointer casts too.
Bill Wendlingdc817b62009-05-14 18:26:15 +00001157 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts) {
Torok Edwin620f2802008-12-16 09:07:36 +00001158 if (stripCasts) {
1159 V = V->stripPointerCasts();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001160
Torok Edwin620f2802008-12-16 09:07:36 +00001161 // Look for the bitcast.
1162 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001163 I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001164 if (isa<BitCastInst>(I))
1165 return findDbgDeclare(*I, false);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001166
Torok Edwin620f2802008-12-16 09:07:36 +00001167 return 0;
1168 }
1169
Bill Wendlingdc817b62009-05-14 18:26:15 +00001170 // Find llvm.dbg.declare among uses of the instruction.
Torok Edwin620f2802008-12-16 09:07:36 +00001171 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001172 I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001173 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I))
1174 return DDI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001175
Torok Edwin620f2802008-12-16 09:07:36 +00001176 return 0;
1177 }
Torok Edwinff7d0e92009-03-10 13:41:26 +00001178
Bill Wendlingdc817b62009-05-14 18:26:15 +00001179 bool getLocationInfo(const Value *V, std::string &DisplayName,
1180 std::string &Type, unsigned &LineNo, std::string &File,
1181 std::string &Dir) {
Torok Edwinff7d0e92009-03-10 13:41:26 +00001182 DICompileUnit Unit;
1183 DIType TypeD;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001184
Torok Edwinff7d0e92009-03-10 13:41:26 +00001185 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1186 Value *DIGV = findDbgGlobalDeclare(GV);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001187 if (!DIGV) return false;
Devang Patel82459882009-08-26 05:01:18 +00001188 DIGlobalVariable Var(cast<GlobalVariable>(DIGV));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001189
Bill Wendling0582ae92009-03-13 04:39:26 +00001190 Var.getDisplayName(DisplayName);
Torok Edwinff7d0e92009-03-10 13:41:26 +00001191 LineNo = Var.getLineNumber();
1192 Unit = Var.getCompileUnit();
1193 TypeD = Var.getType();
1194 } else {
1195 const DbgDeclareInst *DDI = findDbgDeclare(V);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001196 if (!DDI) return false;
Devang Patel82459882009-08-26 05:01:18 +00001197 DIVariable Var(cast<GlobalVariable>(DDI->getVariable()));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001198
Bill Wendling0582ae92009-03-13 04:39:26 +00001199 Var.getName(DisplayName);
Torok Edwinff7d0e92009-03-10 13:41:26 +00001200 LineNo = Var.getLineNumber();
1201 Unit = Var.getCompileUnit();
1202 TypeD = Var.getType();
1203 }
Bill Wendlingdc817b62009-05-14 18:26:15 +00001204
Bill Wendling0582ae92009-03-13 04:39:26 +00001205 TypeD.getName(Type);
1206 Unit.getFilename(File);
1207 Unit.getDirectory(Dir);
Torok Edwinff7d0e92009-03-10 13:41:26 +00001208 return true;
1209 }
Devang Patel13e16b62009-06-26 01:49:18 +00001210
Devang Patel9e529c32009-07-02 01:15:24 +00001211 /// isValidDebugInfoIntrinsic - Return true if SPI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001212 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001213 bool isValidDebugInfoIntrinsic(DbgStopPointInst &SPI,
1214 CodeGenOpt::Level OptLev) {
1215 return DIDescriptor::ValidDebugInfo(SPI.getContext(), OptLev);
1216 }
1217
1218 /// isValidDebugInfoIntrinsic - Return true if FSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001219 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001220 bool isValidDebugInfoIntrinsic(DbgFuncStartInst &FSI,
1221 CodeGenOpt::Level OptLev) {
1222 return DIDescriptor::ValidDebugInfo(FSI.getSubprogram(), OptLev);
1223 }
1224
1225 /// isValidDebugInfoIntrinsic - Return true if RSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001226 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001227 bool isValidDebugInfoIntrinsic(DbgRegionStartInst &RSI,
1228 CodeGenOpt::Level OptLev) {
1229 return DIDescriptor::ValidDebugInfo(RSI.getContext(), OptLev);
1230 }
1231
1232 /// isValidDebugInfoIntrinsic - Return true if REI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001233 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001234 bool isValidDebugInfoIntrinsic(DbgRegionEndInst &REI,
1235 CodeGenOpt::Level OptLev) {
1236 return DIDescriptor::ValidDebugInfo(REI.getContext(), OptLev);
1237 }
1238
1239
1240 /// isValidDebugInfoIntrinsic - Return true if DI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001241 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001242 bool isValidDebugInfoIntrinsic(DbgDeclareInst &DI,
1243 CodeGenOpt::Level OptLev) {
1244 return DIDescriptor::ValidDebugInfo(DI.getVariable(), OptLev);
1245 }
1246
1247 /// ExtractDebugLocation - Extract debug location information
1248 /// from llvm.dbg.stoppoint intrinsic.
1249 DebugLoc ExtractDebugLocation(DbgStopPointInst &SPI,
Devang Patel9e529c32009-07-02 01:15:24 +00001250 DebugLocTracker &DebugLocInfo) {
1251 DebugLoc DL;
1252 Value *Context = SPI.getContext();
Devang Patel9e529c32009-07-02 01:15:24 +00001253
1254 // If this location is already tracked then use it.
Devang Patel82459882009-08-26 05:01:18 +00001255 DebugLocTuple Tuple(cast<GlobalVariable>(Context), SPI.getLine(),
Devang Patel9e529c32009-07-02 01:15:24 +00001256 SPI.getColumn());
1257 DenseMap<DebugLocTuple, unsigned>::iterator II
1258 = DebugLocInfo.DebugIdMap.find(Tuple);
1259 if (II != DebugLocInfo.DebugIdMap.end())
1260 return DebugLoc::get(II->second);
1261
1262 // Add a new location entry.
1263 unsigned Id = DebugLocInfo.DebugLocations.size();
1264 DebugLocInfo.DebugLocations.push_back(Tuple);
1265 DebugLocInfo.DebugIdMap[Tuple] = Id;
1266
1267 return DebugLoc::get(Id);
1268 }
1269
1270 /// ExtractDebugLocation - Extract debug location information
1271 /// from llvm.dbg.func_start intrinsic.
1272 DebugLoc ExtractDebugLocation(DbgFuncStartInst &FSI,
Devang Patel9e529c32009-07-02 01:15:24 +00001273 DebugLocTracker &DebugLocInfo) {
1274 DebugLoc DL;
1275 Value *SP = FSI.getSubprogram();
Devang Patel9e529c32009-07-02 01:15:24 +00001276
Devang Patel82459882009-08-26 05:01:18 +00001277 DISubprogram Subprogram(cast<GlobalVariable>(SP));
Devang Patel9e529c32009-07-02 01:15:24 +00001278 unsigned Line = Subprogram.getLineNumber();
1279 DICompileUnit CU(Subprogram.getCompileUnit());
1280
1281 // If this location is already tracked then use it.
Devang Patel82459882009-08-26 05:01:18 +00001282 DebugLocTuple Tuple(CU.getGV(), Line, /* Column */ 0);
Devang Patel9e529c32009-07-02 01:15:24 +00001283 DenseMap<DebugLocTuple, unsigned>::iterator II
1284 = DebugLocInfo.DebugIdMap.find(Tuple);
1285 if (II != DebugLocInfo.DebugIdMap.end())
1286 return DebugLoc::get(II->second);
1287
1288 // Add a new location entry.
1289 unsigned Id = DebugLocInfo.DebugLocations.size();
1290 DebugLocInfo.DebugLocations.push_back(Tuple);
1291 DebugLocInfo.DebugIdMap[Tuple] = Id;
1292
1293 return DebugLoc::get(Id);
1294 }
1295
1296 /// isInlinedFnStart - Return true if FSI is starting an inlined function.
1297 bool isInlinedFnStart(DbgFuncStartInst &FSI, const Function *CurrentFn) {
Devang Patel82459882009-08-26 05:01:18 +00001298 DISubprogram Subprogram(cast<GlobalVariable>(FSI.getSubprogram()));
Devang Patel9e529c32009-07-02 01:15:24 +00001299 if (Subprogram.describes(CurrentFn))
1300 return false;
1301
1302 return true;
1303 }
1304
1305 /// isInlinedFnEnd - Return true if REI is ending an inlined function.
1306 bool isInlinedFnEnd(DbgRegionEndInst &REI, const Function *CurrentFn) {
Devang Patel82459882009-08-26 05:01:18 +00001307 DISubprogram Subprogram(cast<GlobalVariable>(REI.getContext()));
Devang Patel9e529c32009-07-02 01:15:24 +00001308 if (Subprogram.isNull() || Subprogram.describes(CurrentFn))
1309 return false;
1310
1311 return true;
1312 }
Devang Patel82459882009-08-26 05:01:18 +00001313
Torok Edwin620f2802008-12-16 09:07:36 +00001314}