blob: a7e0a79decb161293e1fa996dcf5b787eea1c674 [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 Patel2a610c72009-08-25 05:24:07 +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 Patel2a610c72009-08-25 05:24:07 +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 Patel2a610c72009-08-25 05:24:07 +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 Patel2a610c72009-08-25 05:24:07 +000051 assert(DIVariable(N).Verify() && "Invalid DebugInfo value");
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000052 break;
53 case DW_TAG_compile_unit:
Devang Patel2a610c72009-08-25 05:24:07 +000054 assert(DICompileUnit(N).Verify() && "Invalid DebugInfo value");
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000055 break;
56 case DW_TAG_subprogram:
Devang Patel2a610c72009-08-25 05:24:07 +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 Patel2a610c72009-08-25 05:24:07 +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 Patel2a610c72009-08-25 05:24:07 +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 Patel2a610c72009-08-25 05:24:07 +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 Patel2a610c72009-08-25 05:24:07 +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 Patel2a610c72009-08-25 05:24:07 +000097 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +000098 return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +000099
Devang Patel2a610c72009-08-25 05:24:07 +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 Patel2a610c72009-08-25 05:24:07 +0000108 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +0000109 return DIDescriptor();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000110
Devang Patel2a610c72009-08-25 05:24:07 +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 Patel2a610c72009-08-25 05:24:07 +0000118 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +0000119 return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000120
Devang Patel2a610c72009-08-25 05:24:07 +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 Patel2a610c72009-08-25 05:24:07 +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 Patel2a610c72009-08-25 05:24:07 +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 Patel2a610c72009-08-25 05:24:07 +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 Patel2a610c72009-08-25 05:24:07 +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 Patel2a610c72009-08-25 05:24:07 +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 Patel2a610c72009-08-25 05:24:07 +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 Patel2a610c72009-08-25 05:24:07 +0000370 DIBasicType(DbgNode).dump();
Devang Patel7136a652009-07-01 22:10:23 +0000371 else if (isDerivedType(Tag))
Devang Patel2a610c72009-08-25 05:24:07 +0000372 DIDerivedType(DbgNode).dump();
Devang Patel7136a652009-07-01 22:10:23 +0000373 else if (isCompositeType(Tag))
Devang Patel2a610c72009-08-25 05:24:07 +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 Patel2a610c72009-08-25 05:24:07 +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 Patel2a610c72009-08-25 05:24:07 +0000474 SmallVector<Value*, 16> Elts;
Chris Lattnera45664f2008-11-10 02:56:27 +0000475
Devang Patel2a610c72009-08-25 05:24:07 +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());
Chris Lattnera45664f2008-11-10 02:56:27 +0000481
Devang Patel2a610c72009-08-25 05:24:07 +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 Patel2a610c72009-08-25 05:24:07 +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 Patel2a610c72009-08-25 05:24:07 +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 Patel2a610c72009-08-25 05:24:07 +0000509 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000510 GetTagConstant(dwarf::DW_TAG_compile_unit),
Devang Patel2a610c72009-08-25 05:24:07 +0000511 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Owen Anderson1d0be152009-08-13 21:58:54 +0000512 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
Devang Patel2a610c72009-08-25 05:24:07 +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 Patel2a610c72009-08-25 05:24:07 +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 Patel2a610c72009-08-25 05:24:07 +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 Patel2a610c72009-08-25 05:24:07 +0000527 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000528 GetTagConstant(dwarf::DW_TAG_enumerator),
Devang Patel2a610c72009-08-25 05:24:07 +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 Patel2a610c72009-08-25 05:24:07 +0000532
533 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000534}
535
536
537/// CreateBasicType - Create a basic type like int, float, etc.
538DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
Bill Wendling0582ae92009-03-13 04:39:26 +0000539 const std::string &Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000540 DICompileUnit CompileUnit,
541 unsigned LineNumber,
542 uint64_t SizeInBits,
543 uint64_t AlignInBits,
544 uint64_t OffsetInBits, unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000545 unsigned Encoding) {
Devang Patel2a610c72009-08-25 05:24:07 +0000546 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000547 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patel2a610c72009-08-25 05:24:07 +0000548 Context.getNode(),
549 MDString::get(VMContext, Name),
550 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000551 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
552 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
553 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
554 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
555 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
556 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
Chris Lattnera45664f2008-11-10 02:56:27 +0000557 };
558
Devang Patel2a610c72009-08-25 05:24:07 +0000559 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000560}
561
562/// CreateDerivedType - Create a derived type like const qualified type,
563/// pointer, typedef, etc.
564DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
565 DIDescriptor Context,
566 const std::string &Name,
567 DICompileUnit CompileUnit,
568 unsigned LineNumber,
569 uint64_t SizeInBits,
570 uint64_t AlignInBits,
571 uint64_t OffsetInBits,
572 unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000573 DIType DerivedFrom) {
Devang Patel2a610c72009-08-25 05:24:07 +0000574 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000575 GetTagConstant(Tag),
Devang Patel2a610c72009-08-25 05:24:07 +0000576 Context.getNode(),
577 MDString::get(VMContext, Name),
578 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000579 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
580 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
581 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
582 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
583 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2a610c72009-08-25 05:24:07 +0000584 DerivedFrom.getNode(),
Chris Lattnera45664f2008-11-10 02:56:27 +0000585 };
Devang Patel2a610c72009-08-25 05:24:07 +0000586
587 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000588}
589
590/// CreateCompositeType - Create a composite type like array, struct, etc.
591DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
592 DIDescriptor Context,
593 const std::string &Name,
594 DICompileUnit CompileUnit,
595 unsigned LineNumber,
596 uint64_t SizeInBits,
597 uint64_t AlignInBits,
598 uint64_t OffsetInBits,
599 unsigned Flags,
600 DIType DerivedFrom,
Devang Patel13319ce2009-02-17 22:43:44 +0000601 DIArray Elements,
602 unsigned RuntimeLang) {
Owen Andersone277fed2009-07-07 16:31:25 +0000603
Devang Patel2a610c72009-08-25 05:24:07 +0000604 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000605 GetTagConstant(Tag),
Devang Patel2a610c72009-08-25 05:24:07 +0000606 Context.getNode(),
607 MDString::get(VMContext, Name),
608 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000609 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
610 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
611 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
612 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
613 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2a610c72009-08-25 05:24:07 +0000614 DerivedFrom.getNode(),
615 Elements.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000616 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
Chris Lattnera45664f2008-11-10 02:56:27 +0000617 };
Devang Patel2a610c72009-08-25 05:24:07 +0000618
619 return DICompositeType(MDNode::get(VMContext, &Elts[0], 12));
Chris Lattnera45664f2008-11-10 02:56:27 +0000620}
621
622
623/// CreateSubprogram - Create a new descriptor for the specified subprogram.
624/// See comments in DISubprogram for descriptions of these fields. This
625/// method does not unique the generated descriptors.
626DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
627 const std::string &Name,
628 const std::string &DisplayName,
629 const std::string &LinkageName,
630 DICompileUnit CompileUnit,
631 unsigned LineNo, DIType Type,
632 bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000633 bool isDefinition) {
Devang Patel854967e2008-12-17 22:39:29 +0000634
Devang Patel2a610c72009-08-25 05:24:07 +0000635 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000636 GetTagConstant(dwarf::DW_TAG_subprogram),
Devang Patel2a610c72009-08-25 05:24:07 +0000637 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
638 Context.getNode(),
639 MDString::get(VMContext, Name),
640 MDString::get(VMContext, DisplayName),
641 MDString::get(VMContext, LinkageName),
642 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000643 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2a610c72009-08-25 05:24:07 +0000644 Type.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000645 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
646 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition)
Chris Lattnera45664f2008-11-10 02:56:27 +0000647 };
648
Devang Patel2a610c72009-08-25 05:24:07 +0000649 return DISubprogram(MDNode::get(VMContext, &Elts[0], 11));
Chris Lattnera45664f2008-11-10 02:56:27 +0000650}
651
652/// CreateGlobalVariable - Create a new descriptor for the specified global.
653DIGlobalVariable
654DIFactory::CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
655 const std::string &DisplayName,
656 const std::string &LinkageName,
657 DICompileUnit CompileUnit,
658 unsigned LineNo, DIType Type,bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000659 bool isDefinition, llvm::GlobalVariable *Val) {
Devang Patel2a610c72009-08-25 05:24:07 +0000660 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000661 GetTagConstant(dwarf::DW_TAG_variable),
Devang Patel2a610c72009-08-25 05:24:07 +0000662 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
663 Context.getNode(),
664 MDString::get(VMContext, Name),
665 MDString::get(VMContext, DisplayName),
666 MDString::get(VMContext, LinkageName),
667 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000668 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2a610c72009-08-25 05:24:07 +0000669 Type.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000670 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
671 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
Devang Patel2a610c72009-08-25 05:24:07 +0000672 Val
Chris Lattnera45664f2008-11-10 02:56:27 +0000673 };
Devang Patel2a610c72009-08-25 05:24:07 +0000674
675 Value *const *Vs = &Elts[0];
676 MDNode *Node = MDNode::get(VMContext,Vs, 12);
677
678 // Create a named metadata so that we do not lose this mdnode.
679 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
680 NMD->addElement(Node);
681
682 return DIGlobalVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +0000683}
684
685
686/// CreateVariable - Create a new descriptor for the specified variable.
687DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
688 const std::string &Name,
689 DICompileUnit CompileUnit, unsigned LineNo,
Devang Pateldd9db662009-01-30 18:20:31 +0000690 DIType Type) {
Devang Patel2a610c72009-08-25 05:24:07 +0000691 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000692 GetTagConstant(Tag),
Devang Patel2a610c72009-08-25 05:24:07 +0000693 Context.getNode(),
694 MDString::get(VMContext, Name),
695 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000696 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2a610c72009-08-25 05:24:07 +0000697 Type.getNode(),
Chris Lattnera45664f2008-11-10 02:56:27 +0000698 };
699
Devang Patel2a610c72009-08-25 05:24:07 +0000700 return DIVariable(MDNode::get(VMContext, &Elts[0], 6));
Chris Lattnera45664f2008-11-10 02:56:27 +0000701}
702
703
704/// CreateBlock - This creates a descriptor for a lexical block with the
Owen Anderson99035272009-07-07 17:12:53 +0000705/// specified parent VMContext.
Chris Lattnera45664f2008-11-10 02:56:27 +0000706DIBlock DIFactory::CreateBlock(DIDescriptor Context) {
Devang Patel2a610c72009-08-25 05:24:07 +0000707 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000708 GetTagConstant(dwarf::DW_TAG_lexical_block),
Devang Patel2a610c72009-08-25 05:24:07 +0000709 Context.getNode()
Chris Lattnera45664f2008-11-10 02:56:27 +0000710 };
Devang Patel2a610c72009-08-25 05:24:07 +0000711
712 return DIBlock(MDNode::get(VMContext, &Elts[0], 2));
Chris Lattnera45664f2008-11-10 02:56:27 +0000713}
714
715
716//===----------------------------------------------------------------------===//
717// DIFactory: Routines for inserting code into a function
718//===----------------------------------------------------------------------===//
719
720/// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
721/// inserting it at the end of the specified basic block.
722void DIFactory::InsertStopPoint(DICompileUnit CU, unsigned LineNo,
723 unsigned ColNo, BasicBlock *BB) {
724
725 // Lazily construct llvm.dbg.stoppoint function.
726 if (!StopPointFn)
727 StopPointFn = llvm::Intrinsic::getDeclaration(&M,
728 llvm::Intrinsic::dbg_stoppoint);
729
730 // Invoke llvm.dbg.stoppoint
731 Value *Args[] = {
Owen Anderson1d0be152009-08-13 21:58:54 +0000732 ConstantInt::get(llvm::Type::getInt32Ty(VMContext), LineNo),
733 ConstantInt::get(llvm::Type::getInt32Ty(VMContext), ColNo),
Devang Patel2a610c72009-08-25 05:24:07 +0000734 CU.getNode()
Chris Lattnera45664f2008-11-10 02:56:27 +0000735 };
736 CallInst::Create(StopPointFn, Args, Args+3, "", BB);
737}
738
739/// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
740/// mark the start of the specified subprogram.
741void DIFactory::InsertSubprogramStart(DISubprogram SP, BasicBlock *BB) {
742 // Lazily construct llvm.dbg.func.start.
743 if (!FuncStartFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000744 FuncStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_func_start);
Chris Lattnera45664f2008-11-10 02:56:27 +0000745
746 // Call llvm.dbg.func.start which also implicitly sets a stoppoint.
Devang Patel2a610c72009-08-25 05:24:07 +0000747 CallInst::Create(FuncStartFn, SP.getNode(), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000748}
749
750/// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
751/// mark the start of a region for the specified scoping descriptor.
752void DIFactory::InsertRegionStart(DIDescriptor D, BasicBlock *BB) {
753 // Lazily construct llvm.dbg.region.start function.
754 if (!RegionStartFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000755 RegionStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_start);
756
Chris Lattnera45664f2008-11-10 02:56:27 +0000757 // Call llvm.dbg.func.start.
Devang Patel2a610c72009-08-25 05:24:07 +0000758 CallInst::Create(RegionStartFn, D.getNode(), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000759}
760
Chris Lattnera45664f2008-11-10 02:56:27 +0000761/// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
762/// mark the end of a region for the specified scoping descriptor.
763void DIFactory::InsertRegionEnd(DIDescriptor D, BasicBlock *BB) {
764 // Lazily construct llvm.dbg.region.end function.
765 if (!RegionEndFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000766 RegionEndFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_end);
767
768 // Call llvm.dbg.region.end.
Devang Patel2a610c72009-08-25 05:24:07 +0000769 CallInst::Create(RegionEndFn, D.getNode(), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000770}
771
772/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Bill Wendlingdc817b62009-05-14 18:26:15 +0000773void DIFactory::InsertDeclare(Value *Storage, DIVariable D, BasicBlock *BB) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000774 // Cast the storage to a {}* for the call to llvm.dbg.declare.
Bill Wendlingdc817b62009-05-14 18:26:15 +0000775 Storage = new BitCastInst(Storage, EmptyStructPtr, "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000776
777 if (!DeclareFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000778 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
779
Devang Patel2a610c72009-08-25 05:24:07 +0000780 Value *Args[] = { Storage, D.getNode() };
Chris Lattnera45664f2008-11-10 02:56:27 +0000781 CallInst::Create(DeclareFn, Args, Args+2, "", BB);
782}
Torok Edwin620f2802008-12-16 09:07:36 +0000783
Devang Patel2a610c72009-08-25 05:24:07 +0000784
Devang Pateld2f79a12009-07-28 19:55:13 +0000785//===----------------------------------------------------------------------===//
Devang Patel98c65172009-07-30 18:25:15 +0000786// DebugInfoFinder implementations.
Devang Pateld2f79a12009-07-28 19:55:13 +0000787//===----------------------------------------------------------------------===//
788
Devang Patel98c65172009-07-30 18:25:15 +0000789/// processModule - Process entire module and collect debug info.
790void DebugInfoFinder::processModule(Module &M) {
Devang Patel2a610c72009-08-25 05:24:07 +0000791
792
Devang Pateld2f79a12009-07-28 19:55:13 +0000793 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
794 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
795 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
796 ++BI) {
797 if (DbgStopPointInst *SPI = dyn_cast<DbgStopPointInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +0000798 processStopPoint(SPI);
Devang Pateld2f79a12009-07-28 19:55:13 +0000799 else if (DbgFuncStartInst *FSI = dyn_cast<DbgFuncStartInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +0000800 processFuncStart(FSI);
Devang Patele802f1c2009-07-30 17:30:23 +0000801 else if (DbgRegionStartInst *DRS = dyn_cast<DbgRegionStartInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +0000802 processRegionStart(DRS);
Devang Patele802f1c2009-07-30 17:30:23 +0000803 else if (DbgRegionEndInst *DRE = dyn_cast<DbgRegionEndInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +0000804 processRegionEnd(DRE);
Devang Patelb4d31302009-07-31 18:18:52 +0000805 else if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
806 processDeclare(DDI);
Devang Pateld2f79a12009-07-28 19:55:13 +0000807 }
Devang Patel2a610c72009-08-25 05:24:07 +0000808
809 NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv");
810 if (!NMD)
811 return;
812
813 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
814 DIGlobalVariable DIG(cast<MDNode>(NMD->getElement(i)));
Devang Pateld2f79a12009-07-28 19:55:13 +0000815 if (addGlobalVariable(DIG)) {
816 addCompileUnit(DIG.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +0000817 processType(DIG.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +0000818 }
819 }
820}
821
Devang Patel98c65172009-07-30 18:25:15 +0000822/// processType - Process DIType.
823void DebugInfoFinder::processType(DIType DT) {
Devang Patel72bcdb62009-08-10 22:09:58 +0000824 if (!addType(DT))
Devang Pateld2f79a12009-07-28 19:55:13 +0000825 return;
826
827 addCompileUnit(DT.getCompileUnit());
828 if (DT.isCompositeType(DT.getTag())) {
Devang Patel2a610c72009-08-25 05:24:07 +0000829 DICompositeType DCT(DT.getNode());
Devang Patel98c65172009-07-30 18:25:15 +0000830 processType(DCT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +0000831 DIArray DA = DCT.getTypeArray();
832 if (!DA.isNull())
833 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
834 DIDescriptor D = DA.getElement(i);
Devang Patel2a610c72009-08-25 05:24:07 +0000835 DIType TypeE = DIType(D.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +0000836 if (!TypeE.isNull())
Devang Patel98c65172009-07-30 18:25:15 +0000837 processType(TypeE);
Devang Pateld2f79a12009-07-28 19:55:13 +0000838 else
Devang Patel2a610c72009-08-25 05:24:07 +0000839 processSubprogram(DISubprogram(D.getNode()));
Devang Pateld2f79a12009-07-28 19:55:13 +0000840 }
841 } else if (DT.isDerivedType(DT.getTag())) {
Devang Patel2a610c72009-08-25 05:24:07 +0000842 DIDerivedType DDT(DT.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +0000843 if (!DDT.isNull())
Devang Patel98c65172009-07-30 18:25:15 +0000844 processType(DDT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +0000845 }
846}
847
Devang Patel98c65172009-07-30 18:25:15 +0000848/// processSubprogram - Process DISubprogram.
849void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Patele802f1c2009-07-30 17:30:23 +0000850 if (SP.isNull())
851 return;
Devang Pateld2f79a12009-07-28 19:55:13 +0000852 if (!addSubprogram(SP))
853 return;
854 addCompileUnit(SP.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +0000855 processType(SP.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +0000856}
857
Devang Patel98c65172009-07-30 18:25:15 +0000858/// processStopPoint - Process DbgStopPointInst.
859void DebugInfoFinder::processStopPoint(DbgStopPointInst *SPI) {
Devang Patel2a610c72009-08-25 05:24:07 +0000860 MDNode *Context = dyn_cast<MDNode>(SPI->getContext());
Devang Pateld2f79a12009-07-28 19:55:13 +0000861 addCompileUnit(DICompileUnit(Context));
862}
863
Devang Patel98c65172009-07-30 18:25:15 +0000864/// processFuncStart - Process DbgFuncStartInst.
865void DebugInfoFinder::processFuncStart(DbgFuncStartInst *FSI) {
Devang Patel2a610c72009-08-25 05:24:07 +0000866 MDNode *SP = dyn_cast<MDNode>(FSI->getSubprogram());
Devang Patel98c65172009-07-30 18:25:15 +0000867 processSubprogram(DISubprogram(SP));
Devang Pateld2f79a12009-07-28 19:55:13 +0000868}
869
Devang Patel98c65172009-07-30 18:25:15 +0000870/// processRegionStart - Process DbgRegionStart.
871void DebugInfoFinder::processRegionStart(DbgRegionStartInst *DRS) {
Devang Patel2a610c72009-08-25 05:24:07 +0000872 MDNode *SP = dyn_cast<MDNode>(DRS->getContext());
Devang Patel98c65172009-07-30 18:25:15 +0000873 processSubprogram(DISubprogram(SP));
Devang Patele802f1c2009-07-30 17:30:23 +0000874}
875
Devang Patel98c65172009-07-30 18:25:15 +0000876/// processRegionEnd - Process DbgRegionEnd.
877void DebugInfoFinder::processRegionEnd(DbgRegionEndInst *DRE) {
Devang Patel2a610c72009-08-25 05:24:07 +0000878 MDNode *SP = dyn_cast<MDNode>(DRE->getContext());
Devang Patel98c65172009-07-30 18:25:15 +0000879 processSubprogram(DISubprogram(SP));
Devang Patele802f1c2009-07-30 17:30:23 +0000880}
881
Devang Patelb4d31302009-07-31 18:18:52 +0000882/// processDeclare - Process DbgDeclareInst.
883void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
Devang Patel2a610c72009-08-25 05:24:07 +0000884 DIVariable DV(cast<MDNode>(DDI->getVariable()));
Devang Patelb4d31302009-07-31 18:18:52 +0000885 if (DV.isNull())
886 return;
887
Devang Patel2a610c72009-08-25 05:24:07 +0000888 if (!NodesSeen.insert(DV.getNode()))
Devang Patelb4d31302009-07-31 18:18:52 +0000889 return;
890
891 addCompileUnit(DV.getCompileUnit());
892 processType(DV.getType());
893}
894
Devang Patel72bcdb62009-08-10 22:09:58 +0000895/// addType - Add type into Tys.
896bool DebugInfoFinder::addType(DIType DT) {
897 if (DT.isNull())
898 return false;
899
Devang Patel2a610c72009-08-25 05:24:07 +0000900 if (!NodesSeen.insert(DT.getNode()))
Devang Patel72bcdb62009-08-10 22:09:58 +0000901 return false;
902
Devang Patel2a610c72009-08-25 05:24:07 +0000903 TYs.push_back(DT.getNode());
Devang Patel72bcdb62009-08-10 22:09:58 +0000904 return true;
905}
906
Devang Pateld2f79a12009-07-28 19:55:13 +0000907/// addCompileUnit - Add compile unit into CUs.
Devang Patel98c65172009-07-30 18:25:15 +0000908bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Pateld2f79a12009-07-28 19:55:13 +0000909 if (CU.isNull())
910 return false;
911
Devang Patel2a610c72009-08-25 05:24:07 +0000912 if (!NodesSeen.insert(CU.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +0000913 return false;
914
Devang Patel2a610c72009-08-25 05:24:07 +0000915 CUs.push_back(CU.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +0000916 return true;
917}
918
919/// addGlobalVariable - Add global variable into GVs.
Devang Patel98c65172009-07-30 18:25:15 +0000920bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Pateld2f79a12009-07-28 19:55:13 +0000921 if (DIG.isNull())
922 return false;
923
Devang Patel2a610c72009-08-25 05:24:07 +0000924 if (!NodesSeen.insert(DIG.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +0000925 return false;
926
Devang Patel2a610c72009-08-25 05:24:07 +0000927 GVs.push_back(DIG.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +0000928 return true;
929}
930
931// addSubprogram - Add subprgoram into SPs.
Devang Patel98c65172009-07-30 18:25:15 +0000932bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Pateld2f79a12009-07-28 19:55:13 +0000933 if (SP.isNull())
934 return false;
935
Devang Patel2a610c72009-08-25 05:24:07 +0000936 if (!NodesSeen.insert(SP.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +0000937 return false;
938
Devang Patel2a610c72009-08-25 05:24:07 +0000939 SPs.push_back(SP.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +0000940 return true;
941}
942
Torok Edwin620f2802008-12-16 09:07:36 +0000943namespace llvm {
Bill Wendlingdc817b62009-05-14 18:26:15 +0000944 /// findStopPoint - Find the stoppoint coressponding to this instruction, that
945 /// is the stoppoint that dominates this instruction.
946 const DbgStopPointInst *findStopPoint(const Instruction *Inst) {
Torok Edwin620f2802008-12-16 09:07:36 +0000947 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(Inst))
948 return DSI;
949
950 const BasicBlock *BB = Inst->getParent();
951 BasicBlock::const_iterator I = Inst, B;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000952 while (BB) {
Torok Edwin620f2802008-12-16 09:07:36 +0000953 B = BB->begin();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000954
Torok Edwin620f2802008-12-16 09:07:36 +0000955 // A BB consisting only of a terminator can't have a stoppoint.
Bill Wendlingdc817b62009-05-14 18:26:15 +0000956 while (I != B) {
957 --I;
958 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
959 return DSI;
Torok Edwin620f2802008-12-16 09:07:36 +0000960 }
Bill Wendlingdc817b62009-05-14 18:26:15 +0000961
962 // This BB didn't have a stoppoint: if there is only one predecessor, look
963 // for a stoppoint there. We could use getIDom(), but that would require
964 // dominator info.
Torok Edwin620f2802008-12-16 09:07:36 +0000965 BB = I->getParent()->getUniquePredecessor();
966 if (BB)
967 I = BB->getTerminator();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000968 }
969
Torok Edwin620f2802008-12-16 09:07:36 +0000970 return 0;
971 }
972
Bill Wendlingdc817b62009-05-14 18:26:15 +0000973 /// findBBStopPoint - Find the stoppoint corresponding to first real
974 /// (non-debug intrinsic) instruction in this Basic Block, and return the
975 /// stoppoint for it.
976 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB) {
977 for(BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +0000978 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
979 return DSI;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000980
981 // Fallback to looking for stoppoint of unique predecessor. Useful if this
982 // BB contains no stoppoints, but unique predecessor does.
Torok Edwin620f2802008-12-16 09:07:36 +0000983 BB = BB->getUniquePredecessor();
984 if (BB)
985 return findStopPoint(BB->getTerminator());
Bill Wendlingdc817b62009-05-14 18:26:15 +0000986
Torok Edwin620f2802008-12-16 09:07:36 +0000987 return 0;
988 }
989
Bill Wendlingdc817b62009-05-14 18:26:15 +0000990 Value *findDbgGlobalDeclare(GlobalVariable *V) {
Torok Edwinff7d0e92009-03-10 13:41:26 +0000991 const Module *M = V->getParent();
Devang Patel2a610c72009-08-25 05:24:07 +0000992 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
993 if (!NMD)
994 return 0;
Owen Anderson99035272009-07-07 17:12:53 +0000995
Devang Patel2a610c72009-08-25 05:24:07 +0000996 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
997 DIGlobalVariable DIG(cast_or_null<MDNode>(NMD->getElement(i)));
998 if (DIG.isNull())
999 continue;
1000 if (DIG.getGlobal() == V)
1001 return DIG.getNode();
Torok Edwinff7d0e92009-03-10 13:41:26 +00001002 }
Torok Edwinff7d0e92009-03-10 13:41:26 +00001003 return 0;
1004 }
1005
Bill Wendlingdc817b62009-05-14 18:26:15 +00001006 /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
Torok Edwin620f2802008-12-16 09:07:36 +00001007 /// It looks through pointer casts too.
Bill Wendlingdc817b62009-05-14 18:26:15 +00001008 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts) {
Torok Edwin620f2802008-12-16 09:07:36 +00001009 if (stripCasts) {
1010 V = V->stripPointerCasts();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001011
Torok Edwin620f2802008-12-16 09:07:36 +00001012 // Look for the bitcast.
1013 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001014 I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001015 if (isa<BitCastInst>(I))
1016 return findDbgDeclare(*I, false);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001017
Torok Edwin620f2802008-12-16 09:07:36 +00001018 return 0;
1019 }
1020
Bill Wendlingdc817b62009-05-14 18:26:15 +00001021 // Find llvm.dbg.declare among uses of the instruction.
Torok Edwin620f2802008-12-16 09:07:36 +00001022 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001023 I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001024 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I))
1025 return DDI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001026
Torok Edwin620f2802008-12-16 09:07:36 +00001027 return 0;
1028 }
Torok Edwinff7d0e92009-03-10 13:41:26 +00001029
Bill Wendlingdc817b62009-05-14 18:26:15 +00001030 bool getLocationInfo(const Value *V, std::string &DisplayName,
1031 std::string &Type, unsigned &LineNo, std::string &File,
1032 std::string &Dir) {
Torok Edwinff7d0e92009-03-10 13:41:26 +00001033 DICompileUnit Unit;
1034 DIType TypeD;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001035
Torok Edwinff7d0e92009-03-10 13:41:26 +00001036 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1037 Value *DIGV = findDbgGlobalDeclare(GV);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001038 if (!DIGV) return false;
Devang Patel2a610c72009-08-25 05:24:07 +00001039 DIGlobalVariable Var(cast<MDNode>(DIGV));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001040
Bill Wendling0582ae92009-03-13 04:39:26 +00001041 Var.getDisplayName(DisplayName);
Torok Edwinff7d0e92009-03-10 13:41:26 +00001042 LineNo = Var.getLineNumber();
1043 Unit = Var.getCompileUnit();
1044 TypeD = Var.getType();
1045 } else {
1046 const DbgDeclareInst *DDI = findDbgDeclare(V);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001047 if (!DDI) return false;
Devang Patel2a610c72009-08-25 05:24:07 +00001048 DIVariable Var(cast<MDNode>(DDI->getVariable()));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001049
Bill Wendling0582ae92009-03-13 04:39:26 +00001050 Var.getName(DisplayName);
Torok Edwinff7d0e92009-03-10 13:41:26 +00001051 LineNo = Var.getLineNumber();
1052 Unit = Var.getCompileUnit();
1053 TypeD = Var.getType();
1054 }
Bill Wendlingdc817b62009-05-14 18:26:15 +00001055
Bill Wendling0582ae92009-03-13 04:39:26 +00001056 TypeD.getName(Type);
1057 Unit.getFilename(File);
1058 Unit.getDirectory(Dir);
Torok Edwinff7d0e92009-03-10 13:41:26 +00001059 return true;
1060 }
Devang Patel13e16b62009-06-26 01:49:18 +00001061
Devang Patel9e529c32009-07-02 01:15:24 +00001062 /// isValidDebugInfoIntrinsic - Return true if SPI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001063 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001064 bool isValidDebugInfoIntrinsic(DbgStopPointInst &SPI,
1065 CodeGenOpt::Level OptLev) {
1066 return DIDescriptor::ValidDebugInfo(SPI.getContext(), OptLev);
1067 }
1068
1069 /// isValidDebugInfoIntrinsic - Return true if FSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001070 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001071 bool isValidDebugInfoIntrinsic(DbgFuncStartInst &FSI,
1072 CodeGenOpt::Level OptLev) {
1073 return DIDescriptor::ValidDebugInfo(FSI.getSubprogram(), OptLev);
1074 }
1075
1076 /// isValidDebugInfoIntrinsic - Return true if RSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001077 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001078 bool isValidDebugInfoIntrinsic(DbgRegionStartInst &RSI,
1079 CodeGenOpt::Level OptLev) {
1080 return DIDescriptor::ValidDebugInfo(RSI.getContext(), OptLev);
1081 }
1082
1083 /// isValidDebugInfoIntrinsic - Return true if REI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001084 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001085 bool isValidDebugInfoIntrinsic(DbgRegionEndInst &REI,
1086 CodeGenOpt::Level OptLev) {
1087 return DIDescriptor::ValidDebugInfo(REI.getContext(), OptLev);
1088 }
1089
1090
1091 /// isValidDebugInfoIntrinsic - Return true if DI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001092 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001093 bool isValidDebugInfoIntrinsic(DbgDeclareInst &DI,
1094 CodeGenOpt::Level OptLev) {
1095 return DIDescriptor::ValidDebugInfo(DI.getVariable(), OptLev);
1096 }
1097
1098 /// ExtractDebugLocation - Extract debug location information
1099 /// from llvm.dbg.stoppoint intrinsic.
1100 DebugLoc ExtractDebugLocation(DbgStopPointInst &SPI,
Devang Patel9e529c32009-07-02 01:15:24 +00001101 DebugLocTracker &DebugLocInfo) {
1102 DebugLoc DL;
1103 Value *Context = SPI.getContext();
Devang Patel9e529c32009-07-02 01:15:24 +00001104
1105 // If this location is already tracked then use it.
Devang Patel2a610c72009-08-25 05:24:07 +00001106 DebugLocTuple Tuple(cast<MDNode>(Context), SPI.getLine(),
Devang Patel9e529c32009-07-02 01:15:24 +00001107 SPI.getColumn());
1108 DenseMap<DebugLocTuple, unsigned>::iterator II
1109 = DebugLocInfo.DebugIdMap.find(Tuple);
1110 if (II != DebugLocInfo.DebugIdMap.end())
1111 return DebugLoc::get(II->second);
1112
1113 // Add a new location entry.
1114 unsigned Id = DebugLocInfo.DebugLocations.size();
1115 DebugLocInfo.DebugLocations.push_back(Tuple);
1116 DebugLocInfo.DebugIdMap[Tuple] = Id;
1117
1118 return DebugLoc::get(Id);
1119 }
1120
1121 /// ExtractDebugLocation - Extract debug location information
1122 /// from llvm.dbg.func_start intrinsic.
1123 DebugLoc ExtractDebugLocation(DbgFuncStartInst &FSI,
Devang Patel9e529c32009-07-02 01:15:24 +00001124 DebugLocTracker &DebugLocInfo) {
1125 DebugLoc DL;
1126 Value *SP = FSI.getSubprogram();
Devang Patel9e529c32009-07-02 01:15:24 +00001127
Devang Patel2a610c72009-08-25 05:24:07 +00001128 DISubprogram Subprogram(cast<MDNode>(SP));
Devang Patel9e529c32009-07-02 01:15:24 +00001129 unsigned Line = Subprogram.getLineNumber();
1130 DICompileUnit CU(Subprogram.getCompileUnit());
1131
1132 // If this location is already tracked then use it.
Devang Patel2a610c72009-08-25 05:24:07 +00001133 DebugLocTuple Tuple(CU.getNode(), Line, /* Column */ 0);
Devang Patel9e529c32009-07-02 01:15:24 +00001134 DenseMap<DebugLocTuple, unsigned>::iterator II
1135 = DebugLocInfo.DebugIdMap.find(Tuple);
1136 if (II != DebugLocInfo.DebugIdMap.end())
1137 return DebugLoc::get(II->second);
1138
1139 // Add a new location entry.
1140 unsigned Id = DebugLocInfo.DebugLocations.size();
1141 DebugLocInfo.DebugLocations.push_back(Tuple);
1142 DebugLocInfo.DebugIdMap[Tuple] = Id;
1143
1144 return DebugLoc::get(Id);
1145 }
1146
1147 /// isInlinedFnStart - Return true if FSI is starting an inlined function.
1148 bool isInlinedFnStart(DbgFuncStartInst &FSI, const Function *CurrentFn) {
Devang Patel2a610c72009-08-25 05:24:07 +00001149 DISubprogram Subprogram(cast<MDNode>(FSI.getSubprogram()));
Devang Patel9e529c32009-07-02 01:15:24 +00001150 if (Subprogram.describes(CurrentFn))
1151 return false;
1152
1153 return true;
1154 }
1155
1156 /// isInlinedFnEnd - Return true if REI is ending an inlined function.
1157 bool isInlinedFnEnd(DbgRegionEndInst &REI, const Function *CurrentFn) {
Devang Patel2a610c72009-08-25 05:24:07 +00001158 DISubprogram Subprogram(cast<MDNode>(REI.getContext()));
Devang Patel9e529c32009-07-02 01:15:24 +00001159 if (Subprogram.isNull() || Subprogram.describes(CurrentFn))
1160 return false;
1161
1162 return true;
1163 }
Torok Edwin620f2802008-12-16 09:07:36 +00001164}