blob: 0d4b213e8061c22e59160ab0c5f92fab36fca676 [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"
Devang Patele4b27562009-08-28 23:24:31 +000024#include "llvm/ADT/SmallPtrSet.h"
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000025#include "llvm/Support/Dwarf.h"
Devang Patel9e529c32009-07-02 01:15:24 +000026#include "llvm/Support/DebugLoc.h"
Chris Lattnera81d29b2009-08-23 07:33:14 +000027#include "llvm/Support/raw_ostream.h"
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.
Devang Patele4b27562009-08-28 23:24:31 +000036/// FIXME : Add DIDescriptor.isValid()
37bool DIDescriptor::ValidDebugInfo(MDNode *N, CodeGenOpt::Level OptLevel) {
38 if (!N)
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000039 return false;
40
Devang Patele4b27562009-08-28 23:24:31 +000041 DIDescriptor DI(N);
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000042
43 // Check current version. Allow Version6 for now.
44 unsigned Version = DI.getVersion();
45 if (Version != LLVMDebugVersion && Version != LLVMDebugVersion6)
46 return false;
47
48 unsigned Tag = DI.getTag();
49 switch (Tag) {
50 case DW_TAG_variable:
Devang Patele4b27562009-08-28 23:24:31 +000051 assert(DIVariable(N).Verify() && "Invalid DebugInfo value");
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000052 break;
53 case DW_TAG_compile_unit:
Devang Patele4b27562009-08-28 23:24:31 +000054 assert(DICompileUnit(N).Verify() && "Invalid DebugInfo value");
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000055 break;
56 case DW_TAG_subprogram:
Devang Patele4b27562009-08-28 23:24:31 +000057 assert(DISubprogram(N).Verify() && "Invalid DebugInfo value");
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000058 break;
59 case DW_TAG_lexical_block:
Bill Wendlingdc817b62009-05-14 18:26:15 +000060 // FIXME: This interfers with the quality of generated code during
61 // optimization.
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000062 if (OptLevel != CodeGenOpt::None)
63 return false;
Bill Wendlingdc817b62009-05-14 18:26:15 +000064 // FALLTHROUGH
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000065 default:
66 break;
67 }
68
69 return true;
70}
71
Devang Patele4b27562009-08-28 23:24:31 +000072DIDescriptor::DIDescriptor(MDNode *N, unsigned RequiredTag) {
73 DbgNode = N;
Chris Lattnera45664f2008-11-10 02:56:27 +000074
Bill Wendlingdc817b62009-05-14 18:26:15 +000075 // If this is non-null, check to see if the Tag matches. If not, set to null.
Devang Patele4b27562009-08-28 23:24:31 +000076 if (N && getTag() != RequiredTag) {
77 DbgNode = 0;
78 }
Chris Lattnera45664f2008-11-10 02:56:27 +000079}
80
Bill Wendling0582ae92009-03-13 04:39:26 +000081const std::string &
82DIDescriptor::getStringField(unsigned Elt, std::string &Result) const {
Devang Patele4b27562009-08-28 23:24:31 +000083 Result.clear();
84 if (DbgNode == 0)
Bill Wendling0582ae92009-03-13 04:39:26 +000085 return Result;
Chris Lattnera45664f2008-11-10 02:56:27 +000086
Devang Patele4b27562009-08-28 23:24:31 +000087 if (Elt < DbgNode->getNumElements())
88 if (MDString *MDS = dyn_cast_or_null<MDString>(DbgNode->getElement(Elt))) {
89 Result.assign(MDS->begin(), MDS->begin() + MDS->length());
90 return Result;
91 }
92
Bill Wendling0582ae92009-03-13 04:39:26 +000093 return Result;
Chris Lattnera45664f2008-11-10 02:56:27 +000094}
95
96uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
Devang Patele4b27562009-08-28 23:24:31 +000097 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +000098 return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +000099
Devang Patele4b27562009-08-28 23:24:31 +0000100 if (Elt < DbgNode->getNumElements())
101 if (ConstantInt *CI = dyn_cast<ConstantInt>(DbgNode->getElement(Elt)))
102 return CI->getZExtValue();
103
Chris Lattnera45664f2008-11-10 02:56:27 +0000104 return 0;
105}
106
Chris Lattnera45664f2008-11-10 02:56:27 +0000107DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
Devang Patele4b27562009-08-28 23:24:31 +0000108 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +0000109 return DIDescriptor();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000110
Devang Patele4b27562009-08-28 23:24:31 +0000111 if (Elt < DbgNode->getNumElements() && DbgNode->getElement(Elt))
112 return DIDescriptor(dyn_cast<MDNode>(DbgNode->getElement(Elt)));
113
114 return DIDescriptor();
Chris Lattnera45664f2008-11-10 02:56:27 +0000115}
116
117GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
Devang Patele4b27562009-08-28 23:24:31 +0000118 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +0000119 return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000120
Devang Patele4b27562009-08-28 23:24:31 +0000121 if (Elt < DbgNode->getNumElements())
122 return dyn_cast<GlobalVariable>(DbgNode->getElement(Elt));
123 return 0;
Chris Lattnera45664f2008-11-10 02:56:27 +0000124}
125
Chris Lattnera45664f2008-11-10 02:56:27 +0000126//===----------------------------------------------------------------------===//
127// Simple Descriptor Constructors and other Methods
128//===----------------------------------------------------------------------===//
129
Bill Wendlingdc817b62009-05-14 18:26:15 +0000130// Needed by DIVariable::getType().
Devang Patele4b27562009-08-28 23:24:31 +0000131DIType::DIType(MDNode *N) : DIDescriptor(N) {
132 if (!N) return;
Torok Edwinb07fbd92008-12-13 08:25:29 +0000133 unsigned tag = getTag();
134 if (tag != dwarf::DW_TAG_base_type && !DIDerivedType::isDerivedType(tag) &&
Devang Patele4b27562009-08-28 23:24:31 +0000135 !DICompositeType::isCompositeType(tag)) {
136 DbgNode = 0;
137 }
Torok Edwinb07fbd92008-12-13 08:25:29 +0000138}
Chris Lattnera45664f2008-11-10 02:56:27 +0000139
140/// isDerivedType - Return true if the specified tag is legal for
141/// DIDerivedType.
Devang Patel486938f2009-01-12 21:38:43 +0000142bool DIType::isDerivedType(unsigned Tag) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000143 switch (Tag) {
144 case dwarf::DW_TAG_typedef:
145 case dwarf::DW_TAG_pointer_type:
146 case dwarf::DW_TAG_reference_type:
147 case dwarf::DW_TAG_const_type:
148 case dwarf::DW_TAG_volatile_type:
149 case dwarf::DW_TAG_restrict_type:
150 case dwarf::DW_TAG_member:
151 case dwarf::DW_TAG_inheritance:
152 return true;
153 default:
Devang Patele4b27562009-08-28 23:24:31 +0000154 // CompositeTypes are currently modelled as DerivedTypes.
155 return isCompositeType(Tag);
Chris Lattnera45664f2008-11-10 02:56:27 +0000156 }
157}
158
Chris Lattnera45664f2008-11-10 02:56:27 +0000159/// isCompositeType - Return true if the specified tag is legal for
160/// DICompositeType.
Devang Patel486938f2009-01-12 21:38:43 +0000161bool DIType::isCompositeType(unsigned TAG) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000162 switch (TAG) {
163 case dwarf::DW_TAG_array_type:
164 case dwarf::DW_TAG_structure_type:
165 case dwarf::DW_TAG_union_type:
166 case dwarf::DW_TAG_enumeration_type:
167 case dwarf::DW_TAG_vector_type:
168 case dwarf::DW_TAG_subroutine_type:
Devang Patel25cb0d72009-03-25 03:52:06 +0000169 case dwarf::DW_TAG_class_type:
Chris Lattnera45664f2008-11-10 02:56:27 +0000170 return true;
171 default:
172 return false;
173 }
174}
175
Chris Lattnera45664f2008-11-10 02:56:27 +0000176/// isVariable - Return true if the specified tag is legal for DIVariable.
177bool DIVariable::isVariable(unsigned Tag) {
178 switch (Tag) {
179 case dwarf::DW_TAG_auto_variable:
180 case dwarf::DW_TAG_arg_variable:
181 case dwarf::DW_TAG_return_variable:
182 return true;
183 default:
184 return false;
185 }
186}
187
Devang Patel68afdc32009-01-05 18:33:01 +0000188unsigned DIArray::getNumElements() const {
Devang Patele4b27562009-08-28 23:24:31 +0000189 assert (DbgNode && "Invalid DIArray");
190 return DbgNode->getNumElements();
Devang Patel68afdc32009-01-05 18:33:01 +0000191}
Chris Lattnera45664f2008-11-10 02:56:27 +0000192
Devang Patelc4999d72009-07-22 18:23:44 +0000193/// replaceAllUsesWith - Replace all uses of debug info referenced by
194/// this descriptor. After this completes, the current debug info value
195/// is erased.
196void DIDerivedType::replaceAllUsesWith(DIDescriptor &D) {
197 if (isNull())
198 return;
199
Devang Patel6930f4f2009-07-22 18:56:16 +0000200 assert (!D.isNull() && "Can not replace with null");
Devang Patele4b27562009-08-28 23:24:31 +0000201 DbgNode->replaceAllUsesWith(D.getNode());
202 delete DbgNode;
Devang Patelc4999d72009-07-22 18:23:44 +0000203}
204
Devang Patelb79b5352009-01-19 23:21:49 +0000205/// Verify - Verify that a compile unit is well formed.
206bool DICompileUnit::Verify() const {
207 if (isNull())
208 return false;
Bill Wendling0582ae92009-03-13 04:39:26 +0000209 std::string Res;
210 if (getFilename(Res).empty())
211 return false;
Devang Patelb79b5352009-01-19 23:21:49 +0000212 // It is possible that directory and produce string is empty.
Bill Wendling0582ae92009-03-13 04:39:26 +0000213 return true;
Devang Patelb79b5352009-01-19 23:21:49 +0000214}
215
216/// Verify - Verify that a type descriptor is well formed.
217bool DIType::Verify() const {
218 if (isNull())
219 return false;
220 if (getContext().isNull())
221 return false;
222
223 DICompileUnit CU = getCompileUnit();
224 if (!CU.isNull() && !CU.Verify())
225 return false;
226 return true;
227}
228
229/// Verify - Verify that a composite type descriptor is well formed.
230bool DICompositeType::Verify() const {
231 if (isNull())
232 return false;
233 if (getContext().isNull())
234 return false;
235
236 DICompileUnit CU = getCompileUnit();
237 if (!CU.isNull() && !CU.Verify())
238 return false;
239 return true;
240}
241
242/// Verify - Verify that a subprogram descriptor is well formed.
243bool DISubprogram::Verify() const {
244 if (isNull())
245 return false;
246
247 if (getContext().isNull())
248 return false;
249
250 DICompileUnit CU = getCompileUnit();
251 if (!CU.Verify())
252 return false;
253
254 DICompositeType Ty = getType();
255 if (!Ty.isNull() && !Ty.Verify())
256 return false;
257 return true;
258}
259
260/// Verify - Verify that a global variable descriptor is well formed.
261bool DIGlobalVariable::Verify() const {
262 if (isNull())
263 return false;
264
265 if (getContext().isNull())
266 return false;
267
268 DICompileUnit CU = getCompileUnit();
Chris Lattnere3f6cea2009-05-05 04:55:56 +0000269 if (!CU.isNull() && !CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000270 return false;
271
272 DIType Ty = getType();
273 if (!Ty.Verify())
274 return false;
275
276 if (!getGlobal())
277 return false;
278
279 return true;
280}
281
282/// Verify - Verify that a variable descriptor is well formed.
283bool DIVariable::Verify() const {
284 if (isNull())
285 return false;
286
287 if (getContext().isNull())
288 return false;
289
290 DIType Ty = getType();
291 if (!Ty.Verify())
292 return false;
293
Devang Patelb79b5352009-01-19 23:21:49 +0000294 return true;
295}
296
Devang Patel36375ee2009-02-17 21:23:59 +0000297/// getOriginalTypeSize - If this type is derived from a base type then
298/// return base type size.
299uint64_t DIDerivedType::getOriginalTypeSize() const {
300 if (getTag() != dwarf::DW_TAG_member)
301 return getSizeInBits();
302 DIType BT = getTypeDerivedFrom();
303 if (BT.getTag() != dwarf::DW_TAG_base_type)
304 return getSizeInBits();
305 return BT.getSizeInBits();
306}
Devang Patelb79b5352009-01-19 23:21:49 +0000307
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000308/// describes - Return true if this subprogram provides debugging
309/// information for the function F.
310bool DISubprogram::describes(const Function *F) {
311 assert (F && "Invalid function");
312 std::string Name;
313 getLinkageName(Name);
314 if (Name.empty())
315 getName(Name);
Daniel Dunbar460f6562009-07-26 09:48:23 +0000316 if (F->getName() == Name)
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000317 return true;
318 return false;
319}
320
Chris Lattnera45664f2008-11-10 02:56:27 +0000321//===----------------------------------------------------------------------===//
Devang Patel7136a652009-07-01 22:10:23 +0000322// DIDescriptor: dump routines for all descriptors.
323//===----------------------------------------------------------------------===//
324
325
326/// dump - Print descriptor.
327void DIDescriptor::dump() const {
Devang Patele4b27562009-08-28 23:24:31 +0000328 errs() << "[" << dwarf::TagString(getTag()) << "] ";
329 errs().write_hex((intptr_t)DbgNode) << ']';
Devang Patel7136a652009-07-01 22:10:23 +0000330}
331
332/// dump - Print compile unit.
333void DICompileUnit::dump() const {
334 if (getLanguage())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000335 errs() << " [" << dwarf::LanguageString(getLanguage()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000336
337 std::string Res1, Res2;
Chris Lattnera81d29b2009-08-23 07:33:14 +0000338 errs() << " [" << getDirectory(Res1) << "/" << getFilename(Res2) << " ]";
Devang Patel7136a652009-07-01 22:10:23 +0000339}
340
341/// dump - Print type.
342void DIType::dump() const {
343 if (isNull()) return;
344
345 std::string Res;
346 if (!getName(Res).empty())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000347 errs() << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000348
349 unsigned Tag = getTag();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000350 errs() << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000351
352 // TODO : Print context
353 getCompileUnit().dump();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000354 errs() << " ["
355 << getLineNumber() << ", "
356 << getSizeInBits() << ", "
357 << getAlignInBits() << ", "
358 << getOffsetInBits()
359 << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000360
361 if (isPrivate())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000362 errs() << " [private] ";
Devang Patel7136a652009-07-01 22:10:23 +0000363 else if (isProtected())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000364 errs() << " [protected] ";
Devang Patel7136a652009-07-01 22:10:23 +0000365
366 if (isForwardDecl())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000367 errs() << " [fwd] ";
Devang Patel7136a652009-07-01 22:10:23 +0000368
369 if (isBasicType(Tag))
Devang Patele4b27562009-08-28 23:24:31 +0000370 DIBasicType(DbgNode).dump();
Devang Patel7136a652009-07-01 22:10:23 +0000371 else if (isDerivedType(Tag))
Devang Patele4b27562009-08-28 23:24:31 +0000372 DIDerivedType(DbgNode).dump();
Devang Patel7136a652009-07-01 22:10:23 +0000373 else if (isCompositeType(Tag))
Devang Patele4b27562009-08-28 23:24:31 +0000374 DICompositeType(DbgNode).dump();
Devang Patel7136a652009-07-01 22:10:23 +0000375 else {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000376 errs() << "Invalid DIType\n";
Devang Patel7136a652009-07-01 22:10:23 +0000377 return;
378 }
379
Chris Lattnera81d29b2009-08-23 07:33:14 +0000380 errs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000381}
382
383/// dump - Print basic type.
384void DIBasicType::dump() const {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000385 errs() << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000386}
387
388/// dump - Print derived type.
389void DIDerivedType::dump() const {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000390 errs() << "\n\t Derived From: "; getTypeDerivedFrom().dump();
Devang Patel7136a652009-07-01 22:10:23 +0000391}
392
393/// dump - Print composite type.
394void DICompositeType::dump() const {
395 DIArray A = getTypeArray();
396 if (A.isNull())
397 return;
Chris Lattnera81d29b2009-08-23 07:33:14 +0000398 errs() << " [" << A.getNumElements() << " elements]";
Devang Patel7136a652009-07-01 22:10:23 +0000399}
400
401/// dump - Print global.
402void DIGlobal::dump() const {
403 std::string Res;
404 if (!getName(Res).empty())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000405 errs() << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000406
407 unsigned Tag = getTag();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000408 errs() << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000409
410 // TODO : Print context
411 getCompileUnit().dump();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000412 errs() << " [" << getLineNumber() << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000413
414 if (isLocalToUnit())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000415 errs() << " [local] ";
Devang Patel7136a652009-07-01 22:10:23 +0000416
417 if (isDefinition())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000418 errs() << " [def] ";
Devang Patel7136a652009-07-01 22:10:23 +0000419
420 if (isGlobalVariable(Tag))
Devang Patele4b27562009-08-28 23:24:31 +0000421 DIGlobalVariable(DbgNode).dump();
Devang Patel7136a652009-07-01 22:10:23 +0000422
Chris Lattnera81d29b2009-08-23 07:33:14 +0000423 errs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000424}
425
426/// dump - Print subprogram.
427void DISubprogram::dump() const {
428 DIGlobal::dump();
429}
430
431/// dump - Print global variable.
432void DIGlobalVariable::dump() const {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000433 errs() << " [";
434 getGlobal()->dump();
435 errs() << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000436}
437
438/// dump - Print variable.
439void DIVariable::dump() const {
440 std::string Res;
441 if (!getName(Res).empty())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000442 errs() << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000443
444 getCompileUnit().dump();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000445 errs() << " [" << getLineNumber() << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000446 getType().dump();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000447 errs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000448}
449
450//===----------------------------------------------------------------------===//
Chris Lattnera45664f2008-11-10 02:56:27 +0000451// DIFactory: Basic Helpers
452//===----------------------------------------------------------------------===//
453
Bill Wendlingdc817b62009-05-14 18:26:15 +0000454DIFactory::DIFactory(Module &m)
Owen Anderson99035272009-07-07 17:12:53 +0000455 : M(m), VMContext(M.getContext()), StopPointFn(0), FuncStartFn(0),
456 RegionStartFn(0), RegionEndFn(0),
Bill Wendlingdc817b62009-05-14 18:26:15 +0000457 DeclareFn(0) {
Owen Andersond7f2a6c2009-08-05 23:16:16 +0000458 EmptyStructPtr = PointerType::getUnqual(StructType::get(VMContext));
Chris Lattner497a7a82008-11-10 04:10:34 +0000459}
460
Chris Lattnera45664f2008-11-10 02:56:27 +0000461Constant *DIFactory::GetTagConstant(unsigned TAG) {
Devang Patel6906ba52009-01-20 19:22:03 +0000462 assert((TAG & LLVMDebugVersionMask) == 0 &&
Chris Lattnera45664f2008-11-10 02:56:27 +0000463 "Tag too large for debug encoding!");
Owen Anderson1d0be152009-08-13 21:58:54 +0000464 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
Chris Lattnera45664f2008-11-10 02:56:27 +0000465}
466
Chris Lattnera45664f2008-11-10 02:56:27 +0000467//===----------------------------------------------------------------------===//
468// DIFactory: Primary Constructors
469//===----------------------------------------------------------------------===//
470
Chris Lattnera45664f2008-11-10 02:56:27 +0000471/// GetOrCreateArray - Create an descriptor for an array of descriptors.
472/// This implicitly uniques the arrays created.
473DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
Devang Patele4b27562009-08-28 23:24:31 +0000474 SmallVector<Value*, 16> Elts;
Chris Lattnera45664f2008-11-10 02:56:27 +0000475
Devang Patele4b27562009-08-28 23:24:31 +0000476 if (NumTys == 0)
477 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)));
478 else
479 for (unsigned i = 0; i != NumTys; ++i)
480 Elts.push_back(Tys[i].getNode());
Devang Patel82459882009-08-26 05:01:18 +0000481
Devang Patele4b27562009-08-28 23:24:31 +0000482 return DIArray(MDNode::get(VMContext,Elts.data(), Elts.size()));
Chris Lattnera45664f2008-11-10 02:56:27 +0000483}
484
485/// GetOrCreateSubrange - Create a descriptor for a value range. This
486/// implicitly uniques the values returned.
487DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
Devang Patele4b27562009-08-28 23:24:31 +0000488 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000489 GetTagConstant(dwarf::DW_TAG_subrange_type),
Owen Anderson1d0be152009-08-13 21:58:54 +0000490 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
491 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
Chris Lattnera45664f2008-11-10 02:56:27 +0000492 };
493
Devang Patele4b27562009-08-28 23:24:31 +0000494 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000495}
496
497
498
499/// CreateCompileUnit - Create a new descriptor for the specified compile
500/// unit. Note that this does not unique compile units within the module.
501DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
502 const std::string &Filename,
503 const std::string &Directory,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000504 const std::string &Producer,
Devang Pateldd9db662009-01-30 18:20:31 +0000505 bool isMain,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000506 bool isOptimized,
Devang Patel13319ce2009-02-17 22:43:44 +0000507 const char *Flags,
508 unsigned RunTimeVer) {
Devang Patele4b27562009-08-28 23:24:31 +0000509 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000510 GetTagConstant(dwarf::DW_TAG_compile_unit),
Devang Patele4b27562009-08-28 23:24:31 +0000511 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Owen Anderson1d0be152009-08-13 21:58:54 +0000512 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
Devang Patele4b27562009-08-28 23:24:31 +0000513 MDString::get(VMContext, Filename),
514 MDString::get(VMContext, Directory),
515 MDString::get(VMContext, Producer),
Owen Anderson1d0be152009-08-13 21:58:54 +0000516 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
517 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
Devang Patele4b27562009-08-28 23:24:31 +0000518 MDString::get(VMContext, Flags),
Owen Anderson1d0be152009-08-13 21:58:54 +0000519 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
Chris Lattnera45664f2008-11-10 02:56:27 +0000520 };
Devang Patele4b27562009-08-28 23:24:31 +0000521
522 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000523}
524
525/// CreateEnumerator - Create a single enumerator value.
526DIEnumerator DIFactory::CreateEnumerator(const std::string &Name, uint64_t Val){
Devang Patele4b27562009-08-28 23:24:31 +0000527 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000528 GetTagConstant(dwarf::DW_TAG_enumerator),
Devang Patele4b27562009-08-28 23:24:31 +0000529 MDString::get(VMContext, Name),
Owen Anderson1d0be152009-08-13 21:58:54 +0000530 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
Chris Lattnera45664f2008-11-10 02:56:27 +0000531 };
Devang Patele4b27562009-08-28 23:24:31 +0000532 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000533}
534
535
536/// CreateBasicType - Create a basic type like int, float, etc.
537DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
Bill Wendling0582ae92009-03-13 04:39:26 +0000538 const std::string &Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000539 DICompileUnit CompileUnit,
540 unsigned LineNumber,
541 uint64_t SizeInBits,
542 uint64_t AlignInBits,
543 uint64_t OffsetInBits, unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000544 unsigned Encoding) {
Devang Patele4b27562009-08-28 23:24:31 +0000545 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000546 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patele4b27562009-08-28 23:24:31 +0000547 Context.getNode(),
548 MDString::get(VMContext, Name),
549 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000550 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
551 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
552 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
553 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
554 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
555 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
Chris Lattnera45664f2008-11-10 02:56:27 +0000556 };
Devang Patele4b27562009-08-28 23:24:31 +0000557 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000558}
559
560/// CreateDerivedType - Create a derived type like const qualified type,
561/// pointer, typedef, etc.
562DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
563 DIDescriptor Context,
564 const std::string &Name,
565 DICompileUnit CompileUnit,
566 unsigned LineNumber,
567 uint64_t SizeInBits,
568 uint64_t AlignInBits,
569 uint64_t OffsetInBits,
570 unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000571 DIType DerivedFrom) {
Devang Patele4b27562009-08-28 23:24:31 +0000572 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000573 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000574 Context.getNode(),
575 MDString::get(VMContext, Name),
576 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000577 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
578 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
579 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
580 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
581 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patele4b27562009-08-28 23:24:31 +0000582 DerivedFrom.getNode(),
Chris Lattnera45664f2008-11-10 02:56:27 +0000583 };
Devang Patele4b27562009-08-28 23:24:31 +0000584 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000585}
586
587/// CreateCompositeType - Create a composite type like array, struct, etc.
588DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
589 DIDescriptor Context,
590 const std::string &Name,
591 DICompileUnit CompileUnit,
592 unsigned LineNumber,
593 uint64_t SizeInBits,
594 uint64_t AlignInBits,
595 uint64_t OffsetInBits,
596 unsigned Flags,
597 DIType DerivedFrom,
Devang Patel13319ce2009-02-17 22:43:44 +0000598 DIArray Elements,
599 unsigned RuntimeLang) {
Owen Andersone277fed2009-07-07 16:31:25 +0000600
Devang Patele4b27562009-08-28 23:24:31 +0000601 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000602 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000603 Context.getNode(),
604 MDString::get(VMContext, Name),
605 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000606 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
607 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
608 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
609 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
610 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patele4b27562009-08-28 23:24:31 +0000611 DerivedFrom.getNode(),
612 Elements.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000613 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
Chris Lattnera45664f2008-11-10 02:56:27 +0000614 };
Devang Patele4b27562009-08-28 23:24:31 +0000615 return DICompositeType(MDNode::get(VMContext, &Elts[0], 12));
Chris Lattnera45664f2008-11-10 02:56:27 +0000616}
617
618
619/// CreateSubprogram - Create a new descriptor for the specified subprogram.
620/// See comments in DISubprogram for descriptions of these fields. This
621/// method does not unique the generated descriptors.
622DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
623 const std::string &Name,
624 const std::string &DisplayName,
625 const std::string &LinkageName,
626 DICompileUnit CompileUnit,
627 unsigned LineNo, DIType Type,
628 bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000629 bool isDefinition) {
Devang Patel854967e2008-12-17 22:39:29 +0000630
Devang Patele4b27562009-08-28 23:24:31 +0000631 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000632 GetTagConstant(dwarf::DW_TAG_subprogram),
Devang Patele4b27562009-08-28 23:24:31 +0000633 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
634 Context.getNode(),
635 MDString::get(VMContext, Name),
636 MDString::get(VMContext, DisplayName),
637 MDString::get(VMContext, LinkageName),
638 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000639 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000640 Type.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000641 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
642 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition)
Chris Lattnera45664f2008-11-10 02:56:27 +0000643 };
Devang Patele4b27562009-08-28 23:24:31 +0000644 return DISubprogram(MDNode::get(VMContext, &Elts[0], 11));
Chris Lattnera45664f2008-11-10 02:56:27 +0000645}
646
647/// CreateGlobalVariable - Create a new descriptor for the specified global.
648DIGlobalVariable
649DIFactory::CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
650 const std::string &DisplayName,
651 const std::string &LinkageName,
652 DICompileUnit CompileUnit,
653 unsigned LineNo, DIType Type,bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000654 bool isDefinition, llvm::GlobalVariable *Val) {
Devang Patele4b27562009-08-28 23:24:31 +0000655 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000656 GetTagConstant(dwarf::DW_TAG_variable),
Devang Patele4b27562009-08-28 23:24:31 +0000657 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
658 Context.getNode(),
659 MDString::get(VMContext, Name),
660 MDString::get(VMContext, DisplayName),
661 MDString::get(VMContext, LinkageName),
662 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000663 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000664 Type.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000665 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
666 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
Devang Patele4b27562009-08-28 23:24:31 +0000667 Val
Chris Lattnera45664f2008-11-10 02:56:27 +0000668 };
Devang Patele4b27562009-08-28 23:24:31 +0000669
670 Value *const *Vs = &Elts[0];
671 MDNode *Node = MDNode::get(VMContext,Vs, 12);
672
673 // Create a named metadata so that we do not lose this mdnode.
674 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
675 NMD->addElement(Node);
676
677 return DIGlobalVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +0000678}
679
680
681/// CreateVariable - Create a new descriptor for the specified variable.
682DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
683 const std::string &Name,
684 DICompileUnit CompileUnit, unsigned LineNo,
Devang Pateldd9db662009-01-30 18:20:31 +0000685 DIType Type) {
Devang Patele4b27562009-08-28 23:24:31 +0000686 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000687 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000688 Context.getNode(),
689 MDString::get(VMContext, Name),
690 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000691 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000692 Type.getNode(),
Chris Lattnera45664f2008-11-10 02:56:27 +0000693 };
Devang Patele4b27562009-08-28 23:24:31 +0000694 return DIVariable(MDNode::get(VMContext, &Elts[0], 6));
Chris Lattnera45664f2008-11-10 02:56:27 +0000695}
696
697
698/// CreateBlock - This creates a descriptor for a lexical block with the
Owen Anderson99035272009-07-07 17:12:53 +0000699/// specified parent VMContext.
Chris Lattnera45664f2008-11-10 02:56:27 +0000700DIBlock DIFactory::CreateBlock(DIDescriptor Context) {
Devang Patele4b27562009-08-28 23:24:31 +0000701 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000702 GetTagConstant(dwarf::DW_TAG_lexical_block),
Devang Patele4b27562009-08-28 23:24:31 +0000703 Context.getNode()
Chris Lattnera45664f2008-11-10 02:56:27 +0000704 };
Devang Patele4b27562009-08-28 23:24:31 +0000705 return DIBlock(MDNode::get(VMContext, &Elts[0], 2));
Chris Lattnera45664f2008-11-10 02:56:27 +0000706}
707
708
709//===----------------------------------------------------------------------===//
710// DIFactory: Routines for inserting code into a function
711//===----------------------------------------------------------------------===//
712
713/// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
714/// inserting it at the end of the specified basic block.
715void DIFactory::InsertStopPoint(DICompileUnit CU, unsigned LineNo,
716 unsigned ColNo, BasicBlock *BB) {
717
718 // Lazily construct llvm.dbg.stoppoint function.
719 if (!StopPointFn)
720 StopPointFn = llvm::Intrinsic::getDeclaration(&M,
721 llvm::Intrinsic::dbg_stoppoint);
722
723 // Invoke llvm.dbg.stoppoint
724 Value *Args[] = {
Owen Anderson1d0be152009-08-13 21:58:54 +0000725 ConstantInt::get(llvm::Type::getInt32Ty(VMContext), LineNo),
726 ConstantInt::get(llvm::Type::getInt32Ty(VMContext), ColNo),
Devang Patele4b27562009-08-28 23:24:31 +0000727 CU.getNode()
Chris Lattnera45664f2008-11-10 02:56:27 +0000728 };
729 CallInst::Create(StopPointFn, Args, Args+3, "", BB);
730}
731
732/// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
733/// mark the start of the specified subprogram.
734void DIFactory::InsertSubprogramStart(DISubprogram SP, BasicBlock *BB) {
735 // Lazily construct llvm.dbg.func.start.
736 if (!FuncStartFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000737 FuncStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_func_start);
Chris Lattnera45664f2008-11-10 02:56:27 +0000738
739 // Call llvm.dbg.func.start which also implicitly sets a stoppoint.
Devang Patele4b27562009-08-28 23:24:31 +0000740 CallInst::Create(FuncStartFn, SP.getNode(), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000741}
742
743/// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
744/// mark the start of a region for the specified scoping descriptor.
745void DIFactory::InsertRegionStart(DIDescriptor D, BasicBlock *BB) {
746 // Lazily construct llvm.dbg.region.start function.
747 if (!RegionStartFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000748 RegionStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_start);
749
Chris Lattnera45664f2008-11-10 02:56:27 +0000750 // Call llvm.dbg.func.start.
Devang Patele4b27562009-08-28 23:24:31 +0000751 CallInst::Create(RegionStartFn, D.getNode(), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000752}
753
Chris Lattnera45664f2008-11-10 02:56:27 +0000754/// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
755/// mark the end of a region for the specified scoping descriptor.
756void DIFactory::InsertRegionEnd(DIDescriptor D, BasicBlock *BB) {
757 // Lazily construct llvm.dbg.region.end function.
758 if (!RegionEndFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000759 RegionEndFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_end);
760
761 // Call llvm.dbg.region.end.
Devang Patele4b27562009-08-28 23:24:31 +0000762 CallInst::Create(RegionEndFn, D.getNode(), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000763}
764
765/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Bill Wendlingdc817b62009-05-14 18:26:15 +0000766void DIFactory::InsertDeclare(Value *Storage, DIVariable D, BasicBlock *BB) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000767 // Cast the storage to a {}* for the call to llvm.dbg.declare.
Bill Wendlingdc817b62009-05-14 18:26:15 +0000768 Storage = new BitCastInst(Storage, EmptyStructPtr, "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000769
770 if (!DeclareFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000771 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
772
Devang Patele4b27562009-08-28 23:24:31 +0000773 Value *Args[] = { Storage, D.getNode() };
Chris Lattnera45664f2008-11-10 02:56:27 +0000774 CallInst::Create(DeclareFn, Args, Args+2, "", BB);
775}
Torok Edwin620f2802008-12-16 09:07:36 +0000776
Devang Patele4b27562009-08-28 23:24:31 +0000777
Devang Pateld2f79a12009-07-28 19:55:13 +0000778//===----------------------------------------------------------------------===//
Devang Patel98c65172009-07-30 18:25:15 +0000779// DebugInfoFinder implementations.
Devang Pateld2f79a12009-07-28 19:55:13 +0000780//===----------------------------------------------------------------------===//
781
Devang Patel98c65172009-07-30 18:25:15 +0000782/// processModule - Process entire module and collect debug info.
783void DebugInfoFinder::processModule(Module &M) {
Devang Patele4b27562009-08-28 23:24:31 +0000784
785
Devang Pateld2f79a12009-07-28 19:55:13 +0000786 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
787 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
788 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
789 ++BI) {
790 if (DbgStopPointInst *SPI = dyn_cast<DbgStopPointInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +0000791 processStopPoint(SPI);
Devang Pateld2f79a12009-07-28 19:55:13 +0000792 else if (DbgFuncStartInst *FSI = dyn_cast<DbgFuncStartInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +0000793 processFuncStart(FSI);
Devang Patele802f1c2009-07-30 17:30:23 +0000794 else if (DbgRegionStartInst *DRS = dyn_cast<DbgRegionStartInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +0000795 processRegionStart(DRS);
Devang Patele802f1c2009-07-30 17:30:23 +0000796 else if (DbgRegionEndInst *DRE = dyn_cast<DbgRegionEndInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +0000797 processRegionEnd(DRE);
Devang Patelb4d31302009-07-31 18:18:52 +0000798 else if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
799 processDeclare(DDI);
Devang Pateld2f79a12009-07-28 19:55:13 +0000800 }
Devang Patele4b27562009-08-28 23:24:31 +0000801
802 NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv");
803 if (!NMD)
804 return;
805
806 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
807 DIGlobalVariable DIG(cast<MDNode>(NMD->getElement(i)));
Devang Pateld2f79a12009-07-28 19:55:13 +0000808 if (addGlobalVariable(DIG)) {
809 addCompileUnit(DIG.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +0000810 processType(DIG.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +0000811 }
812 }
813}
814
Devang Patel98c65172009-07-30 18:25:15 +0000815/// processType - Process DIType.
816void DebugInfoFinder::processType(DIType DT) {
Devang Patel72bcdb62009-08-10 22:09:58 +0000817 if (!addType(DT))
Devang Pateld2f79a12009-07-28 19:55:13 +0000818 return;
819
820 addCompileUnit(DT.getCompileUnit());
821 if (DT.isCompositeType(DT.getTag())) {
Devang Patele4b27562009-08-28 23:24:31 +0000822 DICompositeType DCT(DT.getNode());
Devang Patel98c65172009-07-30 18:25:15 +0000823 processType(DCT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +0000824 DIArray DA = DCT.getTypeArray();
825 if (!DA.isNull())
826 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
827 DIDescriptor D = DA.getElement(i);
Devang Patele4b27562009-08-28 23:24:31 +0000828 DIType TypeE = DIType(D.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +0000829 if (!TypeE.isNull())
Devang Patel98c65172009-07-30 18:25:15 +0000830 processType(TypeE);
Devang Pateld2f79a12009-07-28 19:55:13 +0000831 else
Devang Patele4b27562009-08-28 23:24:31 +0000832 processSubprogram(DISubprogram(D.getNode()));
Devang Pateld2f79a12009-07-28 19:55:13 +0000833 }
834 } else if (DT.isDerivedType(DT.getTag())) {
Devang Patele4b27562009-08-28 23:24:31 +0000835 DIDerivedType DDT(DT.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +0000836 if (!DDT.isNull())
Devang Patel98c65172009-07-30 18:25:15 +0000837 processType(DDT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +0000838 }
839}
840
Devang Patel98c65172009-07-30 18:25:15 +0000841/// processSubprogram - Process DISubprogram.
842void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Patele802f1c2009-07-30 17:30:23 +0000843 if (SP.isNull())
844 return;
Devang Pateld2f79a12009-07-28 19:55:13 +0000845 if (!addSubprogram(SP))
846 return;
847 addCompileUnit(SP.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +0000848 processType(SP.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +0000849}
850
Devang Patel98c65172009-07-30 18:25:15 +0000851/// processStopPoint - Process DbgStopPointInst.
852void DebugInfoFinder::processStopPoint(DbgStopPointInst *SPI) {
Devang Patele4b27562009-08-28 23:24:31 +0000853 MDNode *Context = dyn_cast<MDNode>(SPI->getContext());
Devang Pateld2f79a12009-07-28 19:55:13 +0000854 addCompileUnit(DICompileUnit(Context));
855}
856
Devang Patel98c65172009-07-30 18:25:15 +0000857/// processFuncStart - Process DbgFuncStartInst.
858void DebugInfoFinder::processFuncStart(DbgFuncStartInst *FSI) {
Devang Patele4b27562009-08-28 23:24:31 +0000859 MDNode *SP = dyn_cast<MDNode>(FSI->getSubprogram());
Devang Patel98c65172009-07-30 18:25:15 +0000860 processSubprogram(DISubprogram(SP));
Devang Pateld2f79a12009-07-28 19:55:13 +0000861}
862
Devang Patel98c65172009-07-30 18:25:15 +0000863/// processRegionStart - Process DbgRegionStart.
864void DebugInfoFinder::processRegionStart(DbgRegionStartInst *DRS) {
Devang Patele4b27562009-08-28 23:24:31 +0000865 MDNode *SP = dyn_cast<MDNode>(DRS->getContext());
Devang Patel98c65172009-07-30 18:25:15 +0000866 processSubprogram(DISubprogram(SP));
Devang Patele802f1c2009-07-30 17:30:23 +0000867}
868
Devang Patel98c65172009-07-30 18:25:15 +0000869/// processRegionEnd - Process DbgRegionEnd.
870void DebugInfoFinder::processRegionEnd(DbgRegionEndInst *DRE) {
Devang Patele4b27562009-08-28 23:24:31 +0000871 MDNode *SP = dyn_cast<MDNode>(DRE->getContext());
Devang Patel98c65172009-07-30 18:25:15 +0000872 processSubprogram(DISubprogram(SP));
Devang Patele802f1c2009-07-30 17:30:23 +0000873}
874
Devang Patelb4d31302009-07-31 18:18:52 +0000875/// processDeclare - Process DbgDeclareInst.
876void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
Devang Patele4b27562009-08-28 23:24:31 +0000877 DIVariable DV(cast<MDNode>(DDI->getVariable()));
Devang Patelb4d31302009-07-31 18:18:52 +0000878 if (DV.isNull())
879 return;
880
Devang Patele4b27562009-08-28 23:24:31 +0000881 if (!NodesSeen.insert(DV.getNode()))
Devang Patelb4d31302009-07-31 18:18:52 +0000882 return;
883
884 addCompileUnit(DV.getCompileUnit());
885 processType(DV.getType());
886}
887
Devang Patel72bcdb62009-08-10 22:09:58 +0000888/// addType - Add type into Tys.
889bool DebugInfoFinder::addType(DIType DT) {
890 if (DT.isNull())
891 return false;
892
Devang Patele4b27562009-08-28 23:24:31 +0000893 if (!NodesSeen.insert(DT.getNode()))
Devang Patel72bcdb62009-08-10 22:09:58 +0000894 return false;
895
Devang Patele4b27562009-08-28 23:24:31 +0000896 TYs.push_back(DT.getNode());
Devang Patel72bcdb62009-08-10 22:09:58 +0000897 return true;
898}
899
Devang Pateld2f79a12009-07-28 19:55:13 +0000900/// addCompileUnit - Add compile unit into CUs.
Devang Patel98c65172009-07-30 18:25:15 +0000901bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Pateld2f79a12009-07-28 19:55:13 +0000902 if (CU.isNull())
903 return false;
904
Devang Patele4b27562009-08-28 23:24:31 +0000905 if (!NodesSeen.insert(CU.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +0000906 return false;
907
Devang Patele4b27562009-08-28 23:24:31 +0000908 CUs.push_back(CU.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +0000909 return true;
910}
911
912/// addGlobalVariable - Add global variable into GVs.
Devang Patel98c65172009-07-30 18:25:15 +0000913bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Pateld2f79a12009-07-28 19:55:13 +0000914 if (DIG.isNull())
915 return false;
916
Devang Patele4b27562009-08-28 23:24:31 +0000917 if (!NodesSeen.insert(DIG.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +0000918 return false;
919
Devang Patele4b27562009-08-28 23:24:31 +0000920 GVs.push_back(DIG.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +0000921 return true;
922}
923
924// addSubprogram - Add subprgoram into SPs.
Devang Patel98c65172009-07-30 18:25:15 +0000925bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Pateld2f79a12009-07-28 19:55:13 +0000926 if (SP.isNull())
927 return false;
928
Devang Patele4b27562009-08-28 23:24:31 +0000929 if (!NodesSeen.insert(SP.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +0000930 return false;
931
Devang Patele4b27562009-08-28 23:24:31 +0000932 SPs.push_back(SP.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +0000933 return true;
934}
935
Torok Edwin620f2802008-12-16 09:07:36 +0000936namespace llvm {
Bill Wendlingdc817b62009-05-14 18:26:15 +0000937 /// findStopPoint - Find the stoppoint coressponding to this instruction, that
938 /// is the stoppoint that dominates this instruction.
939 const DbgStopPointInst *findStopPoint(const Instruction *Inst) {
Torok Edwin620f2802008-12-16 09:07:36 +0000940 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(Inst))
941 return DSI;
942
943 const BasicBlock *BB = Inst->getParent();
944 BasicBlock::const_iterator I = Inst, B;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000945 while (BB) {
Torok Edwin620f2802008-12-16 09:07:36 +0000946 B = BB->begin();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000947
Torok Edwin620f2802008-12-16 09:07:36 +0000948 // A BB consisting only of a terminator can't have a stoppoint.
Bill Wendlingdc817b62009-05-14 18:26:15 +0000949 while (I != B) {
950 --I;
951 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
952 return DSI;
Torok Edwin620f2802008-12-16 09:07:36 +0000953 }
Bill Wendlingdc817b62009-05-14 18:26:15 +0000954
955 // This BB didn't have a stoppoint: if there is only one predecessor, look
956 // for a stoppoint there. We could use getIDom(), but that would require
957 // dominator info.
Torok Edwin620f2802008-12-16 09:07:36 +0000958 BB = I->getParent()->getUniquePredecessor();
959 if (BB)
960 I = BB->getTerminator();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000961 }
962
Torok Edwin620f2802008-12-16 09:07:36 +0000963 return 0;
964 }
965
Bill Wendlingdc817b62009-05-14 18:26:15 +0000966 /// findBBStopPoint - Find the stoppoint corresponding to first real
967 /// (non-debug intrinsic) instruction in this Basic Block, and return the
968 /// stoppoint for it.
969 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB) {
970 for(BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +0000971 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
972 return DSI;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000973
974 // Fallback to looking for stoppoint of unique predecessor. Useful if this
975 // BB contains no stoppoints, but unique predecessor does.
Torok Edwin620f2802008-12-16 09:07:36 +0000976 BB = BB->getUniquePredecessor();
977 if (BB)
978 return findStopPoint(BB->getTerminator());
Bill Wendlingdc817b62009-05-14 18:26:15 +0000979
Torok Edwin620f2802008-12-16 09:07:36 +0000980 return 0;
981 }
982
Bill Wendlingdc817b62009-05-14 18:26:15 +0000983 Value *findDbgGlobalDeclare(GlobalVariable *V) {
Torok Edwinff7d0e92009-03-10 13:41:26 +0000984 const Module *M = V->getParent();
Devang Patele4b27562009-08-28 23:24:31 +0000985 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
986 if (!NMD)
987 return 0;
Owen Anderson99035272009-07-07 17:12:53 +0000988
Devang Patele4b27562009-08-28 23:24:31 +0000989 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
990 DIGlobalVariable DIG(cast_or_null<MDNode>(NMD->getElement(i)));
991 if (DIG.isNull())
992 continue;
993 if (DIG.getGlobal() == V)
994 return DIG.getNode();
Torok Edwinff7d0e92009-03-10 13:41:26 +0000995 }
Torok Edwinff7d0e92009-03-10 13:41:26 +0000996 return 0;
997 }
998
Bill Wendlingdc817b62009-05-14 18:26:15 +0000999 /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
Torok Edwin620f2802008-12-16 09:07:36 +00001000 /// It looks through pointer casts too.
Bill Wendlingdc817b62009-05-14 18:26:15 +00001001 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts) {
Torok Edwin620f2802008-12-16 09:07:36 +00001002 if (stripCasts) {
1003 V = V->stripPointerCasts();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001004
Torok Edwin620f2802008-12-16 09:07:36 +00001005 // Look for the bitcast.
1006 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001007 I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001008 if (isa<BitCastInst>(I))
1009 return findDbgDeclare(*I, false);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001010
Torok Edwin620f2802008-12-16 09:07:36 +00001011 return 0;
1012 }
1013
Bill Wendlingdc817b62009-05-14 18:26:15 +00001014 // Find llvm.dbg.declare among uses of the instruction.
Torok Edwin620f2802008-12-16 09:07:36 +00001015 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001016 I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001017 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I))
1018 return DDI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001019
Torok Edwin620f2802008-12-16 09:07:36 +00001020 return 0;
1021 }
Torok Edwinff7d0e92009-03-10 13:41:26 +00001022
Bill Wendlingdc817b62009-05-14 18:26:15 +00001023 bool getLocationInfo(const Value *V, std::string &DisplayName,
1024 std::string &Type, unsigned &LineNo, std::string &File,
1025 std::string &Dir) {
Torok Edwinff7d0e92009-03-10 13:41:26 +00001026 DICompileUnit Unit;
1027 DIType TypeD;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001028
Torok Edwinff7d0e92009-03-10 13:41:26 +00001029 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1030 Value *DIGV = findDbgGlobalDeclare(GV);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001031 if (!DIGV) return false;
Devang Patele4b27562009-08-28 23:24:31 +00001032 DIGlobalVariable Var(cast<MDNode>(DIGV));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001033
Bill Wendling0582ae92009-03-13 04:39:26 +00001034 Var.getDisplayName(DisplayName);
Torok Edwinff7d0e92009-03-10 13:41:26 +00001035 LineNo = Var.getLineNumber();
1036 Unit = Var.getCompileUnit();
1037 TypeD = Var.getType();
1038 } else {
1039 const DbgDeclareInst *DDI = findDbgDeclare(V);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001040 if (!DDI) return false;
Devang Patele4b27562009-08-28 23:24:31 +00001041 DIVariable Var(cast<MDNode>(DDI->getVariable()));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001042
Bill Wendling0582ae92009-03-13 04:39:26 +00001043 Var.getName(DisplayName);
Torok Edwinff7d0e92009-03-10 13:41:26 +00001044 LineNo = Var.getLineNumber();
1045 Unit = Var.getCompileUnit();
1046 TypeD = Var.getType();
1047 }
Bill Wendlingdc817b62009-05-14 18:26:15 +00001048
Bill Wendling0582ae92009-03-13 04:39:26 +00001049 TypeD.getName(Type);
1050 Unit.getFilename(File);
1051 Unit.getDirectory(Dir);
Torok Edwinff7d0e92009-03-10 13:41:26 +00001052 return true;
1053 }
Devang Patel13e16b62009-06-26 01:49:18 +00001054
Devang Patel9e529c32009-07-02 01:15:24 +00001055 /// isValidDebugInfoIntrinsic - Return true if SPI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001056 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001057 bool isValidDebugInfoIntrinsic(DbgStopPointInst &SPI,
1058 CodeGenOpt::Level OptLev) {
1059 return DIDescriptor::ValidDebugInfo(SPI.getContext(), OptLev);
1060 }
1061
1062 /// isValidDebugInfoIntrinsic - Return true if FSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001063 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001064 bool isValidDebugInfoIntrinsic(DbgFuncStartInst &FSI,
1065 CodeGenOpt::Level OptLev) {
1066 return DIDescriptor::ValidDebugInfo(FSI.getSubprogram(), OptLev);
1067 }
1068
1069 /// isValidDebugInfoIntrinsic - Return true if RSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001070 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001071 bool isValidDebugInfoIntrinsic(DbgRegionStartInst &RSI,
1072 CodeGenOpt::Level OptLev) {
1073 return DIDescriptor::ValidDebugInfo(RSI.getContext(), OptLev);
1074 }
1075
1076 /// isValidDebugInfoIntrinsic - Return true if REI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001077 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001078 bool isValidDebugInfoIntrinsic(DbgRegionEndInst &REI,
1079 CodeGenOpt::Level OptLev) {
1080 return DIDescriptor::ValidDebugInfo(REI.getContext(), OptLev);
1081 }
1082
1083
1084 /// isValidDebugInfoIntrinsic - Return true if DI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001085 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001086 bool isValidDebugInfoIntrinsic(DbgDeclareInst &DI,
1087 CodeGenOpt::Level OptLev) {
1088 return DIDescriptor::ValidDebugInfo(DI.getVariable(), OptLev);
1089 }
1090
1091 /// ExtractDebugLocation - Extract debug location information
1092 /// from llvm.dbg.stoppoint intrinsic.
1093 DebugLoc ExtractDebugLocation(DbgStopPointInst &SPI,
Devang Patel9e529c32009-07-02 01:15:24 +00001094 DebugLocTracker &DebugLocInfo) {
1095 DebugLoc DL;
1096 Value *Context = SPI.getContext();
Devang Patel9e529c32009-07-02 01:15:24 +00001097
1098 // If this location is already tracked then use it.
Devang Patele4b27562009-08-28 23:24:31 +00001099 DebugLocTuple Tuple(cast<MDNode>(Context), SPI.getLine(),
Devang Patel9e529c32009-07-02 01:15:24 +00001100 SPI.getColumn());
1101 DenseMap<DebugLocTuple, unsigned>::iterator II
1102 = DebugLocInfo.DebugIdMap.find(Tuple);
1103 if (II != DebugLocInfo.DebugIdMap.end())
1104 return DebugLoc::get(II->second);
1105
1106 // Add a new location entry.
1107 unsigned Id = DebugLocInfo.DebugLocations.size();
1108 DebugLocInfo.DebugLocations.push_back(Tuple);
1109 DebugLocInfo.DebugIdMap[Tuple] = Id;
1110
1111 return DebugLoc::get(Id);
1112 }
1113
1114 /// ExtractDebugLocation - Extract debug location information
1115 /// from llvm.dbg.func_start intrinsic.
1116 DebugLoc ExtractDebugLocation(DbgFuncStartInst &FSI,
Devang Patel9e529c32009-07-02 01:15:24 +00001117 DebugLocTracker &DebugLocInfo) {
1118 DebugLoc DL;
1119 Value *SP = FSI.getSubprogram();
Devang Patel9e529c32009-07-02 01:15:24 +00001120
Devang Patele4b27562009-08-28 23:24:31 +00001121 DISubprogram Subprogram(cast<MDNode>(SP));
Devang Patel9e529c32009-07-02 01:15:24 +00001122 unsigned Line = Subprogram.getLineNumber();
1123 DICompileUnit CU(Subprogram.getCompileUnit());
1124
1125 // If this location is already tracked then use it.
Devang Patele4b27562009-08-28 23:24:31 +00001126 DebugLocTuple Tuple(CU.getNode(), Line, /* Column */ 0);
Devang Patel9e529c32009-07-02 01:15:24 +00001127 DenseMap<DebugLocTuple, unsigned>::iterator II
1128 = DebugLocInfo.DebugIdMap.find(Tuple);
1129 if (II != DebugLocInfo.DebugIdMap.end())
1130 return DebugLoc::get(II->second);
1131
1132 // Add a new location entry.
1133 unsigned Id = DebugLocInfo.DebugLocations.size();
1134 DebugLocInfo.DebugLocations.push_back(Tuple);
1135 DebugLocInfo.DebugIdMap[Tuple] = Id;
1136
1137 return DebugLoc::get(Id);
1138 }
1139
1140 /// isInlinedFnStart - Return true if FSI is starting an inlined function.
1141 bool isInlinedFnStart(DbgFuncStartInst &FSI, const Function *CurrentFn) {
Devang Patele4b27562009-08-28 23:24:31 +00001142 DISubprogram Subprogram(cast<MDNode>(FSI.getSubprogram()));
Devang Patel9e529c32009-07-02 01:15:24 +00001143 if (Subprogram.describes(CurrentFn))
1144 return false;
1145
1146 return true;
1147 }
1148
1149 /// isInlinedFnEnd - Return true if REI is ending an inlined function.
1150 bool isInlinedFnEnd(DbgRegionEndInst &REI, const Function *CurrentFn) {
Devang Patele4b27562009-08-28 23:24:31 +00001151 DISubprogram Subprogram(cast<MDNode>(REI.getContext()));
Devang Patel9e529c32009-07-02 01:15:24 +00001152 if (Subprogram.isNull() || Subprogram.describes(CurrentFn))
1153 return false;
1154
1155 return true;
1156 }
Torok Edwin620f2802008-12-16 09:07:36 +00001157}