blob: 047837c2fb2fdf5d673cc78704da6e23a75344c1 [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;
Daniel Dunbarf612ff62009-09-19 20:40:05 +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
Devang Patel5ccdd102009-09-29 18:40:58 +000081const char *
82DIDescriptor::getStringField(unsigned Elt) const {
Devang Patele4b27562009-08-28 23:24:31 +000083 if (DbgNode == 0)
Devang Patel5ccdd102009-09-29 18:40:58 +000084 return NULL;
Chris Lattnera45664f2008-11-10 02:56:27 +000085
Daniel Dunbarf612ff62009-09-19 20:40:05 +000086 if (Elt < DbgNode->getNumElements())
Devang Patel5e8e2d72009-10-30 22:52:47 +000087 if (MDString *MDS = dyn_cast_or_null<MDString>(DbgNode->getElement(Elt))) {
88 if (MDS->getLength() == 0)
89 return NULL;
Devang Patel5ccdd102009-09-29 18:40:58 +000090 return MDS->getString().data();
Devang Patel5e8e2d72009-10-30 22:52:47 +000091 }
Daniel Dunbarf612ff62009-09-19 20:40:05 +000092
Devang Patel5ccdd102009-09-29 18:40:58 +000093 return NULL;
Chris Lattnera45664f2008-11-10 02:56:27 +000094}
95
96uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +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();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000103
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 {
Daniel Dunbarf612ff62009-09-19 20:40:05 +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 {
Daniel Dunbarf612ff62009-09-19 20:40:05 +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())
Bill Wendling26c6cf42009-10-08 20:52:51 +0000122 return dyn_cast_or_null<GlobalVariable>(DbgNode->getElement(Elt));
Devang Patele4b27562009-08-28 23:24:31 +0000123 return 0;
Chris Lattnera45664f2008-11-10 02:56:27 +0000124}
125
Chris Lattnera45664f2008-11-10 02:56:27 +0000126//===----------------------------------------------------------------------===//
Devang Patel6ceea332009-08-31 18:49:10 +0000127// Predicates
Chris Lattnera45664f2008-11-10 02:56:27 +0000128//===----------------------------------------------------------------------===//
129
Devang Patel6ceea332009-08-31 18:49:10 +0000130/// isBasicType - Return true if the specified tag is legal for
131/// DIBasicType.
132bool DIDescriptor::isBasicType() const {
Devang Patel5a685092009-08-31 20:27:49 +0000133 assert (!isNull() && "Invalid descriptor!");
Devang Patel6ceea332009-08-31 18:49:10 +0000134 unsigned Tag = getTag();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000135
Devang Patel6ceea332009-08-31 18:49:10 +0000136 return Tag == dwarf::DW_TAG_base_type;
Torok Edwinb07fbd92008-12-13 08:25:29 +0000137}
Chris Lattnera45664f2008-11-10 02:56:27 +0000138
Devang Patel6ceea332009-08-31 18:49:10 +0000139/// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
140bool DIDescriptor::isDerivedType() const {
Devang Patel5a685092009-08-31 20:27:49 +0000141 assert (!isNull() && "Invalid descriptor!");
Devang Patel6ceea332009-08-31 18:49:10 +0000142 unsigned Tag = getTag();
143
Chris Lattnera45664f2008-11-10 02:56:27 +0000144 switch (Tag) {
145 case dwarf::DW_TAG_typedef:
146 case dwarf::DW_TAG_pointer_type:
147 case dwarf::DW_TAG_reference_type:
148 case dwarf::DW_TAG_const_type:
149 case dwarf::DW_TAG_volatile_type:
150 case dwarf::DW_TAG_restrict_type:
151 case dwarf::DW_TAG_member:
152 case dwarf::DW_TAG_inheritance:
153 return true;
154 default:
Devang Patele4b27562009-08-28 23:24:31 +0000155 // CompositeTypes are currently modelled as DerivedTypes.
Devang Patel6ceea332009-08-31 18:49:10 +0000156 return isCompositeType();
Chris Lattnera45664f2008-11-10 02:56:27 +0000157 }
158}
159
Chris Lattnera45664f2008-11-10 02:56:27 +0000160/// isCompositeType - Return true if the specified tag is legal for
161/// DICompositeType.
Devang Patel6ceea332009-08-31 18:49:10 +0000162bool DIDescriptor::isCompositeType() const {
Devang Patel5a685092009-08-31 20:27:49 +0000163 assert (!isNull() && "Invalid descriptor!");
Devang Patel6ceea332009-08-31 18:49:10 +0000164 unsigned Tag = getTag();
165
166 switch (Tag) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000167 case dwarf::DW_TAG_array_type:
168 case dwarf::DW_TAG_structure_type:
169 case dwarf::DW_TAG_union_type:
170 case dwarf::DW_TAG_enumeration_type:
171 case dwarf::DW_TAG_vector_type:
172 case dwarf::DW_TAG_subroutine_type:
Devang Patel25cb0d72009-03-25 03:52:06 +0000173 case dwarf::DW_TAG_class_type:
Chris Lattnera45664f2008-11-10 02:56:27 +0000174 return true;
175 default:
176 return false;
177 }
178}
179
Chris Lattnera45664f2008-11-10 02:56:27 +0000180/// isVariable - Return true if the specified tag is legal for DIVariable.
Devang Patel6ceea332009-08-31 18:49:10 +0000181bool DIDescriptor::isVariable() const {
Devang Patel5a685092009-08-31 20:27:49 +0000182 assert (!isNull() && "Invalid descriptor!");
Devang Patel6ceea332009-08-31 18:49:10 +0000183 unsigned Tag = getTag();
184
Chris Lattnera45664f2008-11-10 02:56:27 +0000185 switch (Tag) {
186 case dwarf::DW_TAG_auto_variable:
187 case dwarf::DW_TAG_arg_variable:
188 case dwarf::DW_TAG_return_variable:
189 return true;
190 default:
191 return false;
192 }
193}
194
Devang Patelecbeb1a2009-09-30 22:34:41 +0000195/// isType - Return true if the specified tag is legal for DIType.
196bool DIDescriptor::isType() const {
197 return isBasicType() || isCompositeType() || isDerivedType();
198}
199
Devang Patel6ceea332009-08-31 18:49:10 +0000200/// isSubprogram - Return true if the specified tag is legal for
201/// DISubprogram.
202bool DIDescriptor::isSubprogram() const {
Devang Patel5a685092009-08-31 20:27:49 +0000203 assert (!isNull() && "Invalid descriptor!");
Devang Patel6ceea332009-08-31 18:49:10 +0000204 unsigned Tag = getTag();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000205
Devang Patel6ceea332009-08-31 18:49:10 +0000206 return Tag == dwarf::DW_TAG_subprogram;
207}
208
209/// isGlobalVariable - Return true if the specified tag is legal for
210/// DIGlobalVariable.
211bool DIDescriptor::isGlobalVariable() const {
Devang Patel5a685092009-08-31 20:27:49 +0000212 assert (!isNull() && "Invalid descriptor!");
Devang Patel6ceea332009-08-31 18:49:10 +0000213 unsigned Tag = getTag();
214
215 return Tag == dwarf::DW_TAG_variable;
216}
217
Devang Patelecbeb1a2009-09-30 22:34:41 +0000218/// isGlobal - Return true if the specified tag is legal for DIGlobal.
219bool DIDescriptor::isGlobal() const {
220 return isGlobalVariable();
221}
222
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000223/// isScope - Return true if the specified tag is one of the scope
Devang Patel43d98b32009-08-31 20:44:45 +0000224/// related tag.
225bool DIDescriptor::isScope() const {
226 assert (!isNull() && "Invalid descriptor!");
227 unsigned Tag = getTag();
228
229 switch (Tag) {
230 case dwarf::DW_TAG_compile_unit:
231 case dwarf::DW_TAG_lexical_block:
232 case dwarf::DW_TAG_subprogram:
233 return true;
234 default:
235 break;
236 }
237 return false;
238}
Devang Patel6ceea332009-08-31 18:49:10 +0000239
Devang Patelc9f322d2009-08-31 21:34:44 +0000240/// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
241bool DIDescriptor::isCompileUnit() const {
242 assert (!isNull() && "Invalid descriptor!");
243 unsigned Tag = getTag();
244
245 return Tag == dwarf::DW_TAG_compile_unit;
246}
247
Devang Patel5e005d82009-08-31 22:00:15 +0000248/// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
249bool DIDescriptor::isLexicalBlock() const {
250 assert (!isNull() && "Invalid descriptor!");
251 unsigned Tag = getTag();
252
253 return Tag == dwarf::DW_TAG_lexical_block;
254}
255
Devang Patelecbeb1a2009-09-30 22:34:41 +0000256/// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
257bool DIDescriptor::isSubrange() const {
258 assert (!isNull() && "Invalid descriptor!");
259 unsigned Tag = getTag();
260
261 return Tag == dwarf::DW_TAG_subrange_type;
262}
263
264/// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
265bool DIDescriptor::isEnumerator() const {
266 assert (!isNull() && "Invalid descriptor!");
267 unsigned Tag = getTag();
268
269 return Tag == dwarf::DW_TAG_enumerator;
270}
271
Devang Patel6ceea332009-08-31 18:49:10 +0000272//===----------------------------------------------------------------------===//
273// Simple Descriptor Constructors and other Methods
274//===----------------------------------------------------------------------===//
275
276DIType::DIType(MDNode *N) : DIDescriptor(N) {
277 if (!N) return;
278 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
279 DbgNode = 0;
280 }
281}
282
Devang Patel68afdc32009-01-05 18:33:01 +0000283unsigned DIArray::getNumElements() const {
Devang Patele4b27562009-08-28 23:24:31 +0000284 assert (DbgNode && "Invalid DIArray");
285 return DbgNode->getNumElements();
Devang Patel68afdc32009-01-05 18:33:01 +0000286}
Chris Lattnera45664f2008-11-10 02:56:27 +0000287
Devang Patelc4999d72009-07-22 18:23:44 +0000288/// replaceAllUsesWith - Replace all uses of debug info referenced by
289/// this descriptor. After this completes, the current debug info value
290/// is erased.
291void DIDerivedType::replaceAllUsesWith(DIDescriptor &D) {
292 if (isNull())
293 return;
294
Devang Patel6930f4f2009-07-22 18:56:16 +0000295 assert (!D.isNull() && "Can not replace with null");
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000296
297 // Since we use a TrackingVH for the node, its easy for clients to manufacture
298 // legitimate situations where they want to replaceAllUsesWith() on something
299 // which, due to uniquing, has merged with the source. We shield clients from
300 // this detail by allowing a value to be replaced with replaceAllUsesWith()
301 // itself.
302 if (getNode() != D.getNode()) {
303 MDNode *Node = DbgNode;
304 Node->replaceAllUsesWith(D.getNode());
305 delete Node;
306 }
Devang Patelc4999d72009-07-22 18:23:44 +0000307}
308
Devang Patelb79b5352009-01-19 23:21:49 +0000309/// Verify - Verify that a compile unit is well formed.
310bool DICompileUnit::Verify() const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000311 if (isNull())
Devang Patelb79b5352009-01-19 23:21:49 +0000312 return false;
Devang Patel5ccdd102009-09-29 18:40:58 +0000313 const char *N = getFilename();
314 if (!N)
Bill Wendling0582ae92009-03-13 04:39:26 +0000315 return false;
Devang Patelb79b5352009-01-19 23:21:49 +0000316 // It is possible that directory and produce string is empty.
Bill Wendling0582ae92009-03-13 04:39:26 +0000317 return true;
Devang Patelb79b5352009-01-19 23:21:49 +0000318}
319
320/// Verify - Verify that a type descriptor is well formed.
321bool DIType::Verify() const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000322 if (isNull())
Devang Patelb79b5352009-01-19 23:21:49 +0000323 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000324 if (getContext().isNull())
Devang Patelb79b5352009-01-19 23:21:49 +0000325 return false;
326
327 DICompileUnit CU = getCompileUnit();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000328 if (!CU.isNull() && !CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000329 return false;
330 return true;
331}
332
333/// Verify - Verify that a composite type descriptor is well formed.
334bool DICompositeType::Verify() const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000335 if (isNull())
Devang Patelb79b5352009-01-19 23:21:49 +0000336 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000337 if (getContext().isNull())
Devang Patelb79b5352009-01-19 23:21:49 +0000338 return false;
339
340 DICompileUnit CU = getCompileUnit();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000341 if (!CU.isNull() && !CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000342 return false;
343 return true;
344}
345
346/// Verify - Verify that a subprogram descriptor is well formed.
347bool DISubprogram::Verify() const {
348 if (isNull())
349 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000350
Devang Patelb79b5352009-01-19 23:21:49 +0000351 if (getContext().isNull())
352 return false;
353
354 DICompileUnit CU = getCompileUnit();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000355 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000356 return false;
357
358 DICompositeType Ty = getType();
359 if (!Ty.isNull() && !Ty.Verify())
360 return false;
361 return true;
362}
363
364/// Verify - Verify that a global variable descriptor is well formed.
365bool DIGlobalVariable::Verify() const {
366 if (isNull())
367 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000368
Devang Patel84c73e92009-11-06 17:58:12 +0000369 if (!getDisplayName())
370 return false;
371
Devang Patelb79b5352009-01-19 23:21:49 +0000372 if (getContext().isNull())
373 return false;
374
375 DICompileUnit CU = getCompileUnit();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000376 if (!CU.isNull() && !CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000377 return false;
378
379 DIType Ty = getType();
380 if (!Ty.Verify())
381 return false;
382
383 if (!getGlobal())
384 return false;
385
386 return true;
387}
388
389/// Verify - Verify that a variable descriptor is well formed.
390bool DIVariable::Verify() const {
391 if (isNull())
392 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000393
Devang Patelb79b5352009-01-19 23:21:49 +0000394 if (getContext().isNull())
395 return false;
396
397 DIType Ty = getType();
398 if (!Ty.Verify())
399 return false;
400
Devang Patelb79b5352009-01-19 23:21:49 +0000401 return true;
402}
403
Devang Patel36375ee2009-02-17 21:23:59 +0000404/// getOriginalTypeSize - If this type is derived from a base type then
405/// return base type size.
406uint64_t DIDerivedType::getOriginalTypeSize() const {
Devang Patel61ecbd12009-11-04 23:48:00 +0000407 unsigned Tag = getTag();
408 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
409 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
410 Tag == dwarf::DW_TAG_restrict_type) {
411 DIType BaseType = getTypeDerivedFrom();
Devang Patel5ebfa2d2009-11-06 18:24:05 +0000412 // If this type is not derived from any type then take conservative
413 // approach.
414 if (BaseType.isNull())
415 return getSizeInBits();
Devang Patel61ecbd12009-11-04 23:48:00 +0000416 if (BaseType.isDerivedType())
417 return DIDerivedType(BaseType.getNode()).getOriginalTypeSize();
418 else
419 return BaseType.getSizeInBits();
420 }
421
422 return getSizeInBits();
Devang Patel36375ee2009-02-17 21:23:59 +0000423}
Devang Patelb79b5352009-01-19 23:21:49 +0000424
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000425/// describes - Return true if this subprogram provides debugging
426/// information for the function F.
427bool DISubprogram::describes(const Function *F) {
428 assert (F && "Invalid function");
Devang Patel5ccdd102009-09-29 18:40:58 +0000429 const char *Name = getLinkageName();
430 if (!Name)
431 Name = getName();
432 if (strcmp(F->getName().data(), Name) == 0)
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000433 return true;
434 return false;
435}
436
Devang Patelecbeb1a2009-09-30 22:34:41 +0000437const char *DIScope::getFilename() const {
438 if (isLexicalBlock())
439 return DILexicalBlock(DbgNode).getFilename();
440 else if (isSubprogram())
441 return DISubprogram(DbgNode).getFilename();
442 else if (isCompileUnit())
443 return DICompileUnit(DbgNode).getFilename();
444 else
445 assert (0 && "Invalid DIScope!");
446 return NULL;
447}
448
449const char *DIScope::getDirectory() const {
450 if (isLexicalBlock())
451 return DILexicalBlock(DbgNode).getDirectory();
452 else if (isSubprogram())
453 return DISubprogram(DbgNode).getDirectory();
454 else if (isCompileUnit())
455 return DICompileUnit(DbgNode).getDirectory();
456 else
457 assert (0 && "Invalid DIScope!");
458 return NULL;
459}
460
Chris Lattnera45664f2008-11-10 02:56:27 +0000461//===----------------------------------------------------------------------===//
Devang Patel7136a652009-07-01 22:10:23 +0000462// DIDescriptor: dump routines for all descriptors.
463//===----------------------------------------------------------------------===//
464
465
466/// dump - Print descriptor.
467void DIDescriptor::dump() const {
Devang Patele4b27562009-08-28 23:24:31 +0000468 errs() << "[" << dwarf::TagString(getTag()) << "] ";
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000469 errs().write_hex((intptr_t) &*DbgNode) << ']';
Devang Patel7136a652009-07-01 22:10:23 +0000470}
471
472/// dump - Print compile unit.
473void DICompileUnit::dump() const {
474 if (getLanguage())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000475 errs() << " [" << dwarf::LanguageString(getLanguage()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000476
Devang Patel5ccdd102009-09-29 18:40:58 +0000477 errs() << " [" << getDirectory() << "/" << getFilename() << " ]";
Devang Patel7136a652009-07-01 22:10:23 +0000478}
479
480/// dump - Print type.
481void DIType::dump() const {
482 if (isNull()) return;
483
Devang Patel5ccdd102009-09-29 18:40:58 +0000484 if (const char *Res = getName())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000485 errs() << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000486
487 unsigned Tag = getTag();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000488 errs() << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000489
490 // TODO : Print context
491 getCompileUnit().dump();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000492 errs() << " ["
493 << getLineNumber() << ", "
Chris Lattnera81d29b2009-08-23 07:33:14 +0000494 << getSizeInBits() << ", "
495 << getAlignInBits() << ", "
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000496 << getOffsetInBits()
Chris Lattnera81d29b2009-08-23 07:33:14 +0000497 << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000498
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000499 if (isPrivate())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000500 errs() << " [private] ";
Devang Patel7136a652009-07-01 22:10:23 +0000501 else if (isProtected())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000502 errs() << " [protected] ";
Devang Patel7136a652009-07-01 22:10:23 +0000503
504 if (isForwardDecl())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000505 errs() << " [fwd] ";
Devang Patel7136a652009-07-01 22:10:23 +0000506
Devang Patel6ceea332009-08-31 18:49:10 +0000507 if (isBasicType())
Devang Patele4b27562009-08-28 23:24:31 +0000508 DIBasicType(DbgNode).dump();
Devang Patel6ceea332009-08-31 18:49:10 +0000509 else if (isDerivedType())
Devang Patele4b27562009-08-28 23:24:31 +0000510 DIDerivedType(DbgNode).dump();
Devang Patel6ceea332009-08-31 18:49:10 +0000511 else if (isCompositeType())
Devang Patele4b27562009-08-28 23:24:31 +0000512 DICompositeType(DbgNode).dump();
Devang Patel7136a652009-07-01 22:10:23 +0000513 else {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000514 errs() << "Invalid DIType\n";
Devang Patel7136a652009-07-01 22:10:23 +0000515 return;
516 }
517
Chris Lattnera81d29b2009-08-23 07:33:14 +0000518 errs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000519}
520
521/// dump - Print basic type.
522void DIBasicType::dump() const {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000523 errs() << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000524}
525
526/// dump - Print derived type.
527void DIDerivedType::dump() const {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000528 errs() << "\n\t Derived From: "; getTypeDerivedFrom().dump();
Devang Patel7136a652009-07-01 22:10:23 +0000529}
530
531/// dump - Print composite type.
532void DICompositeType::dump() const {
533 DIArray A = getTypeArray();
534 if (A.isNull())
535 return;
Chris Lattnera81d29b2009-08-23 07:33:14 +0000536 errs() << " [" << A.getNumElements() << " elements]";
Devang Patel7136a652009-07-01 22:10:23 +0000537}
538
539/// dump - Print global.
540void DIGlobal::dump() const {
Devang Patel5ccdd102009-09-29 18:40:58 +0000541 if (const char *Res = getName())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000542 errs() << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000543
544 unsigned Tag = getTag();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000545 errs() << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000546
547 // TODO : Print context
548 getCompileUnit().dump();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000549 errs() << " [" << getLineNumber() << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000550
551 if (isLocalToUnit())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000552 errs() << " [local] ";
Devang Patel7136a652009-07-01 22:10:23 +0000553
554 if (isDefinition())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000555 errs() << " [def] ";
Devang Patel7136a652009-07-01 22:10:23 +0000556
Devang Patel6ceea332009-08-31 18:49:10 +0000557 if (isGlobalVariable())
Devang Patele4b27562009-08-28 23:24:31 +0000558 DIGlobalVariable(DbgNode).dump();
Devang Patel7136a652009-07-01 22:10:23 +0000559
Chris Lattnera81d29b2009-08-23 07:33:14 +0000560 errs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000561}
562
563/// dump - Print subprogram.
564void DISubprogram::dump() const {
Devang Patel5ccdd102009-09-29 18:40:58 +0000565 if (const char *Res = getName())
Devang Patel82dfc0c2009-08-31 22:47:13 +0000566 errs() << " [" << Res << "] ";
567
568 unsigned Tag = getTag();
569 errs() << " [" << dwarf::TagString(Tag) << "] ";
570
571 // TODO : Print context
572 getCompileUnit().dump();
573 errs() << " [" << getLineNumber() << "] ";
574
575 if (isLocalToUnit())
576 errs() << " [local] ";
577
578 if (isDefinition())
579 errs() << " [def] ";
580
581 errs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000582}
583
584/// dump - Print global variable.
585void DIGlobalVariable::dump() const {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000586 errs() << " [";
587 getGlobal()->dump();
588 errs() << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000589}
590
591/// dump - Print variable.
592void DIVariable::dump() const {
Devang Patel5ccdd102009-09-29 18:40:58 +0000593 if (const char *Res = getName())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000594 errs() << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000595
596 getCompileUnit().dump();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000597 errs() << " [" << getLineNumber() << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000598 getType().dump();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000599 errs() << "\n";
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000600
601 // FIXME: Dump complex addresses
Devang Patel7136a652009-07-01 22:10:23 +0000602}
603
604//===----------------------------------------------------------------------===//
Chris Lattnera45664f2008-11-10 02:56:27 +0000605// DIFactory: Basic Helpers
606//===----------------------------------------------------------------------===//
607
Bill Wendlingdc817b62009-05-14 18:26:15 +0000608DIFactory::DIFactory(Module &m)
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000609 : M(m), VMContext(M.getContext()), StopPointFn(0), FuncStartFn(0),
Owen Anderson99035272009-07-07 17:12:53 +0000610 RegionStartFn(0), RegionEndFn(0),
Victor Hernandez0418c102009-11-13 01:44:55 +0000611 DeclareFn(0) {}
Chris Lattner497a7a82008-11-10 04:10:34 +0000612
Chris Lattnera45664f2008-11-10 02:56:27 +0000613Constant *DIFactory::GetTagConstant(unsigned TAG) {
Devang Patel6906ba52009-01-20 19:22:03 +0000614 assert((TAG & LLVMDebugVersionMask) == 0 &&
Chris Lattnera45664f2008-11-10 02:56:27 +0000615 "Tag too large for debug encoding!");
Owen Anderson1d0be152009-08-13 21:58:54 +0000616 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
Chris Lattnera45664f2008-11-10 02:56:27 +0000617}
618
Chris Lattnera45664f2008-11-10 02:56:27 +0000619//===----------------------------------------------------------------------===//
620// DIFactory: Primary Constructors
621//===----------------------------------------------------------------------===//
622
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000623/// GetOrCreateArray - Create an descriptor for an array of descriptors.
Chris Lattnera45664f2008-11-10 02:56:27 +0000624/// This implicitly uniques the arrays created.
625DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
Devang Patele4b27562009-08-28 23:24:31 +0000626 SmallVector<Value*, 16> Elts;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000627
Devang Patele4b27562009-08-28 23:24:31 +0000628 if (NumTys == 0)
629 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)));
630 else
631 for (unsigned i = 0; i != NumTys; ++i)
632 Elts.push_back(Tys[i].getNode());
Devang Patel82459882009-08-26 05:01:18 +0000633
Devang Patele4b27562009-08-28 23:24:31 +0000634 return DIArray(MDNode::get(VMContext,Elts.data(), Elts.size()));
Chris Lattnera45664f2008-11-10 02:56:27 +0000635}
636
637/// GetOrCreateSubrange - Create a descriptor for a value range. This
638/// implicitly uniques the values returned.
639DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
Devang Patele4b27562009-08-28 23:24:31 +0000640 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000641 GetTagConstant(dwarf::DW_TAG_subrange_type),
Owen Anderson1d0be152009-08-13 21:58:54 +0000642 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
643 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
Chris Lattnera45664f2008-11-10 02:56:27 +0000644 };
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000645
Devang Patele4b27562009-08-28 23:24:31 +0000646 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000647}
648
649
650
651/// CreateCompileUnit - Create a new descriptor for the specified compile
652/// unit. Note that this does not unique compile units within the module.
653DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
Devang Patelafa5a342009-11-12 00:50:58 +0000654 const char * Filename,
655 const char * Directory,
656 const char * Producer,
Devang Pateldd9db662009-01-30 18:20:31 +0000657 bool isMain,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000658 bool isOptimized,
Devang Patel13319ce2009-02-17 22:43:44 +0000659 const char *Flags,
660 unsigned RunTimeVer) {
Devang Patele4b27562009-08-28 23:24:31 +0000661 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000662 GetTagConstant(dwarf::DW_TAG_compile_unit),
Devang Patele4b27562009-08-28 23:24:31 +0000663 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Owen Anderson1d0be152009-08-13 21:58:54 +0000664 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
Devang Patele4b27562009-08-28 23:24:31 +0000665 MDString::get(VMContext, Filename),
666 MDString::get(VMContext, Directory),
667 MDString::get(VMContext, Producer),
Owen Anderson1d0be152009-08-13 21:58:54 +0000668 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
669 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
Devang Patele4b27562009-08-28 23:24:31 +0000670 MDString::get(VMContext, Flags),
Owen Anderson1d0be152009-08-13 21:58:54 +0000671 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
Chris Lattnera45664f2008-11-10 02:56:27 +0000672 };
Devang Patele4b27562009-08-28 23:24:31 +0000673
674 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000675}
676
677/// CreateEnumerator - Create a single enumerator value.
Devang Patelafa5a342009-11-12 00:50:58 +0000678DIEnumerator DIFactory::CreateEnumerator(const char * Name, uint64_t Val){
Devang Patele4b27562009-08-28 23:24:31 +0000679 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000680 GetTagConstant(dwarf::DW_TAG_enumerator),
Devang Patele4b27562009-08-28 23:24:31 +0000681 MDString::get(VMContext, Name),
Owen Anderson1d0be152009-08-13 21:58:54 +0000682 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
Chris Lattnera45664f2008-11-10 02:56:27 +0000683 };
Devang Patele4b27562009-08-28 23:24:31 +0000684 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000685}
686
687
688/// CreateBasicType - Create a basic type like int, float, etc.
689DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
Devang Patelafa5a342009-11-12 00:50:58 +0000690 const char * Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000691 DICompileUnit CompileUnit,
692 unsigned LineNumber,
693 uint64_t SizeInBits,
694 uint64_t AlignInBits,
695 uint64_t OffsetInBits, unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000696 unsigned Encoding) {
Devang Patele4b27562009-08-28 23:24:31 +0000697 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000698 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patele4b27562009-08-28 23:24:31 +0000699 Context.getNode(),
700 MDString::get(VMContext, Name),
701 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000702 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
703 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
704 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
705 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
706 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
707 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
Chris Lattnera45664f2008-11-10 02:56:27 +0000708 };
Devang Patele4b27562009-08-28 23:24:31 +0000709 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000710}
711
Devang Patelac16d442009-10-26 16:54:35 +0000712
713/// CreateBasicType - Create a basic type like int, float, etc.
714DIBasicType DIFactory::CreateBasicTypeEx(DIDescriptor Context,
Devang Patelafa5a342009-11-12 00:50:58 +0000715 const char * Name,
Devang Patelac16d442009-10-26 16:54:35 +0000716 DICompileUnit CompileUnit,
717 unsigned LineNumber,
718 Constant *SizeInBits,
719 Constant *AlignInBits,
720 Constant *OffsetInBits, unsigned Flags,
721 unsigned Encoding) {
722 Value *Elts[] = {
723 GetTagConstant(dwarf::DW_TAG_base_type),
724 Context.getNode(),
725 MDString::get(VMContext, Name),
726 CompileUnit.getNode(),
727 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
728 SizeInBits,
729 AlignInBits,
730 OffsetInBits,
731 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
732 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
733 };
734 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
735}
736
737
Chris Lattnera45664f2008-11-10 02:56:27 +0000738/// CreateDerivedType - Create a derived type like const qualified type,
739/// pointer, typedef, etc.
740DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
741 DIDescriptor Context,
Devang Patelafa5a342009-11-12 00:50:58 +0000742 const char * Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000743 DICompileUnit CompileUnit,
744 unsigned LineNumber,
745 uint64_t SizeInBits,
746 uint64_t AlignInBits,
747 uint64_t OffsetInBits,
748 unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000749 DIType DerivedFrom) {
Devang Patele4b27562009-08-28 23:24:31 +0000750 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000751 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000752 Context.getNode(),
753 MDString::get(VMContext, Name),
754 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000755 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
756 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
757 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
758 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
759 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patele4b27562009-08-28 23:24:31 +0000760 DerivedFrom.getNode(),
Chris Lattnera45664f2008-11-10 02:56:27 +0000761 };
Devang Patele4b27562009-08-28 23:24:31 +0000762 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000763}
764
Devang Patelac16d442009-10-26 16:54:35 +0000765
766/// CreateDerivedType - Create a derived type like const qualified type,
767/// pointer, typedef, etc.
768DIDerivedType DIFactory::CreateDerivedTypeEx(unsigned Tag,
769 DIDescriptor Context,
Devang Patelafa5a342009-11-12 00:50:58 +0000770 const char * Name,
Devang Patelac16d442009-10-26 16:54:35 +0000771 DICompileUnit CompileUnit,
772 unsigned LineNumber,
773 Constant *SizeInBits,
774 Constant *AlignInBits,
775 Constant *OffsetInBits,
776 unsigned Flags,
777 DIType DerivedFrom) {
778 Value *Elts[] = {
779 GetTagConstant(Tag),
780 Context.getNode(),
781 MDString::get(VMContext, Name),
782 CompileUnit.getNode(),
783 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
784 SizeInBits,
785 AlignInBits,
786 OffsetInBits,
787 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
788 DerivedFrom.getNode(),
789 };
790 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
791}
792
793
Chris Lattnera45664f2008-11-10 02:56:27 +0000794/// CreateCompositeType - Create a composite type like array, struct, etc.
795DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
796 DIDescriptor Context,
Devang Patelafa5a342009-11-12 00:50:58 +0000797 const char * Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000798 DICompileUnit CompileUnit,
799 unsigned LineNumber,
800 uint64_t SizeInBits,
801 uint64_t AlignInBits,
802 uint64_t OffsetInBits,
803 unsigned Flags,
804 DIType DerivedFrom,
Devang Patel13319ce2009-02-17 22:43:44 +0000805 DIArray Elements,
806 unsigned RuntimeLang) {
Owen Andersone277fed2009-07-07 16:31:25 +0000807
Devang Patele4b27562009-08-28 23:24:31 +0000808 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000809 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000810 Context.getNode(),
811 MDString::get(VMContext, Name),
812 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000813 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
814 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
815 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
816 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
817 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patele4b27562009-08-28 23:24:31 +0000818 DerivedFrom.getNode(),
819 Elements.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000820 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
Chris Lattnera45664f2008-11-10 02:56:27 +0000821 };
Devang Patele4b27562009-08-28 23:24:31 +0000822 return DICompositeType(MDNode::get(VMContext, &Elts[0], 12));
Chris Lattnera45664f2008-11-10 02:56:27 +0000823}
824
825
Devang Patelac16d442009-10-26 16:54:35 +0000826/// CreateCompositeType - Create a composite type like array, struct, etc.
827DICompositeType DIFactory::CreateCompositeTypeEx(unsigned Tag,
828 DIDescriptor Context,
Devang Patelafa5a342009-11-12 00:50:58 +0000829 const char * Name,
Devang Patelac16d442009-10-26 16:54:35 +0000830 DICompileUnit CompileUnit,
831 unsigned LineNumber,
832 Constant *SizeInBits,
833 Constant *AlignInBits,
834 Constant *OffsetInBits,
835 unsigned Flags,
836 DIType DerivedFrom,
837 DIArray Elements,
838 unsigned RuntimeLang) {
839
840 Value *Elts[] = {
841 GetTagConstant(Tag),
842 Context.getNode(),
843 MDString::get(VMContext, Name),
844 CompileUnit.getNode(),
845 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
846 SizeInBits,
847 AlignInBits,
848 OffsetInBits,
849 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
850 DerivedFrom.getNode(),
851 Elements.getNode(),
852 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
853 };
854 return DICompositeType(MDNode::get(VMContext, &Elts[0], 12));
855}
856
857
Chris Lattnera45664f2008-11-10 02:56:27 +0000858/// CreateSubprogram - Create a new descriptor for the specified subprogram.
859/// See comments in DISubprogram for descriptions of these fields. This
860/// method does not unique the generated descriptors.
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000861DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
Devang Patelafa5a342009-11-12 00:50:58 +0000862 const char * Name,
863 const char * DisplayName,
864 const char * LinkageName,
Chris Lattnera45664f2008-11-10 02:56:27 +0000865 DICompileUnit CompileUnit,
866 unsigned LineNo, DIType Type,
867 bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000868 bool isDefinition) {
Devang Patel854967e2008-12-17 22:39:29 +0000869
Devang Patele4b27562009-08-28 23:24:31 +0000870 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000871 GetTagConstant(dwarf::DW_TAG_subprogram),
Devang Patele4b27562009-08-28 23:24:31 +0000872 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
873 Context.getNode(),
874 MDString::get(VMContext, Name),
875 MDString::get(VMContext, DisplayName),
876 MDString::get(VMContext, LinkageName),
877 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000878 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000879 Type.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000880 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
881 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition)
Chris Lattnera45664f2008-11-10 02:56:27 +0000882 };
Devang Patele4b27562009-08-28 23:24:31 +0000883 return DISubprogram(MDNode::get(VMContext, &Elts[0], 11));
Chris Lattnera45664f2008-11-10 02:56:27 +0000884}
885
886/// CreateGlobalVariable - Create a new descriptor for the specified global.
887DIGlobalVariable
Devang Patelafa5a342009-11-12 00:50:58 +0000888DIFactory::CreateGlobalVariable(DIDescriptor Context, const char * Name,
889 const char * DisplayName,
890 const char * LinkageName,
Chris Lattnera45664f2008-11-10 02:56:27 +0000891 DICompileUnit CompileUnit,
892 unsigned LineNo, DIType Type,bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000893 bool isDefinition, llvm::GlobalVariable *Val) {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000894 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000895 GetTagConstant(dwarf::DW_TAG_variable),
Devang Patele4b27562009-08-28 23:24:31 +0000896 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
897 Context.getNode(),
898 MDString::get(VMContext, Name),
899 MDString::get(VMContext, DisplayName),
900 MDString::get(VMContext, LinkageName),
901 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000902 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000903 Type.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000904 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
905 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
Devang Patele4b27562009-08-28 23:24:31 +0000906 Val
Chris Lattnera45664f2008-11-10 02:56:27 +0000907 };
Devang Patele4b27562009-08-28 23:24:31 +0000908
909 Value *const *Vs = &Elts[0];
910 MDNode *Node = MDNode::get(VMContext,Vs, 12);
911
912 // Create a named metadata so that we do not lose this mdnode.
913 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
914 NMD->addElement(Node);
915
916 return DIGlobalVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +0000917}
918
919
920/// CreateVariable - Create a new descriptor for the specified variable.
921DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
Devang Patelafa5a342009-11-12 00:50:58 +0000922 const char * Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000923 DICompileUnit CompileUnit, unsigned LineNo,
Devang Pateldd9db662009-01-30 18:20:31 +0000924 DIType Type) {
Devang Patele4b27562009-08-28 23:24:31 +0000925 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000926 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000927 Context.getNode(),
928 MDString::get(VMContext, Name),
929 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000930 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000931 Type.getNode(),
Chris Lattnera45664f2008-11-10 02:56:27 +0000932 };
Devang Patele4b27562009-08-28 23:24:31 +0000933 return DIVariable(MDNode::get(VMContext, &Elts[0], 6));
Chris Lattnera45664f2008-11-10 02:56:27 +0000934}
935
936
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000937/// CreateComplexVariable - Create a new descriptor for the specified variable
938/// which has a complex address expression for its address.
939DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
940 const std::string &Name,
941 DICompileUnit CompileUnit,
942 unsigned LineNo,
943 DIType Type, SmallVector<Value *, 9> &addr) {
944 SmallVector<Value *, 9> Elts;
945 Elts.push_back(GetTagConstant(Tag));
946 Elts.push_back(Context.getNode());
947 Elts.push_back(MDString::get(VMContext, Name));
948 Elts.push_back(CompileUnit.getNode());
949 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
950 Elts.push_back(Type.getNode());
951 Elts.insert(Elts.end(), addr.begin(), addr.end());
952
953 return DIVariable(MDNode::get(VMContext, &Elts[0], 6+addr.size()));
954}
955
956
Chris Lattnera45664f2008-11-10 02:56:27 +0000957/// CreateBlock - This creates a descriptor for a lexical block with the
Owen Anderson99035272009-07-07 17:12:53 +0000958/// specified parent VMContext.
Devang Patel5e005d82009-08-31 22:00:15 +0000959DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context) {
Devang Patele4b27562009-08-28 23:24:31 +0000960 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000961 GetTagConstant(dwarf::DW_TAG_lexical_block),
Devang Patele4b27562009-08-28 23:24:31 +0000962 Context.getNode()
Chris Lattnera45664f2008-11-10 02:56:27 +0000963 };
Devang Patel5e005d82009-08-31 22:00:15 +0000964 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 2));
Chris Lattnera45664f2008-11-10 02:56:27 +0000965}
966
Devang Patelf98d8fe2009-09-01 01:14:15 +0000967/// CreateLocation - Creates a debug info location.
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000968DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
Daniel Dunbara279bc32009-09-20 02:20:51 +0000969 DIScope S, DILocation OrigLoc) {
Devang Patelf98d8fe2009-09-01 01:14:15 +0000970 Value *Elts[] = {
971 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
972 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
973 S.getNode(),
974 OrigLoc.getNode(),
975 };
976 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
977}
978
Chris Lattnera45664f2008-11-10 02:56:27 +0000979
980//===----------------------------------------------------------------------===//
981// DIFactory: Routines for inserting code into a function
982//===----------------------------------------------------------------------===//
983
984/// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
985/// inserting it at the end of the specified basic block.
986void DIFactory::InsertStopPoint(DICompileUnit CU, unsigned LineNo,
987 unsigned ColNo, BasicBlock *BB) {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000988
Chris Lattnera45664f2008-11-10 02:56:27 +0000989 // Lazily construct llvm.dbg.stoppoint function.
990 if (!StopPointFn)
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000991 StopPointFn = llvm::Intrinsic::getDeclaration(&M,
Chris Lattnera45664f2008-11-10 02:56:27 +0000992 llvm::Intrinsic::dbg_stoppoint);
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000993
Chris Lattnera45664f2008-11-10 02:56:27 +0000994 // Invoke llvm.dbg.stoppoint
995 Value *Args[] = {
Owen Anderson1d0be152009-08-13 21:58:54 +0000996 ConstantInt::get(llvm::Type::getInt32Ty(VMContext), LineNo),
997 ConstantInt::get(llvm::Type::getInt32Ty(VMContext), ColNo),
Devang Patele4b27562009-08-28 23:24:31 +0000998 CU.getNode()
Chris Lattnera45664f2008-11-10 02:56:27 +0000999 };
1000 CallInst::Create(StopPointFn, Args, Args+3, "", BB);
1001}
1002
1003/// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
1004/// mark the start of the specified subprogram.
1005void DIFactory::InsertSubprogramStart(DISubprogram SP, BasicBlock *BB) {
1006 // Lazily construct llvm.dbg.func.start.
1007 if (!FuncStartFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +00001008 FuncStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_func_start);
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001009
Chris Lattnera45664f2008-11-10 02:56:27 +00001010 // Call llvm.dbg.func.start which also implicitly sets a stoppoint.
Devang Patele4b27562009-08-28 23:24:31 +00001011 CallInst::Create(FuncStartFn, SP.getNode(), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +00001012}
1013
1014/// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
1015/// mark the start of a region for the specified scoping descriptor.
1016void DIFactory::InsertRegionStart(DIDescriptor D, BasicBlock *BB) {
1017 // Lazily construct llvm.dbg.region.start function.
1018 if (!RegionStartFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +00001019 RegionStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_start);
1020
Chris Lattnera45664f2008-11-10 02:56:27 +00001021 // Call llvm.dbg.func.start.
Devang Patele4b27562009-08-28 23:24:31 +00001022 CallInst::Create(RegionStartFn, D.getNode(), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +00001023}
1024
Chris Lattnera45664f2008-11-10 02:56:27 +00001025/// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
1026/// mark the end of a region for the specified scoping descriptor.
1027void DIFactory::InsertRegionEnd(DIDescriptor D, BasicBlock *BB) {
1028 // Lazily construct llvm.dbg.region.end function.
1029 if (!RegionEndFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +00001030 RegionEndFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_end);
1031
1032 // Call llvm.dbg.region.end.
Devang Patele4b27562009-08-28 23:24:31 +00001033 CallInst::Create(RegionEndFn, D.getNode(), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +00001034}
1035
1036/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Devang Patel6daf99b2009-11-10 22:05:35 +00001037Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
Victor Hernandez0418c102009-11-13 01:44:55 +00001038 Instruction *InsertBefore) {
Chris Lattnera45664f2008-11-10 02:56:27 +00001039 if (!DeclareFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +00001040 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1041
Devang Patele4b27562009-08-28 23:24:31 +00001042 Value *Args[] = { Storage, D.getNode() };
Devang Patel6daf99b2009-11-10 22:05:35 +00001043 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
Mike Stumpe4250392009-10-01 22:08:58 +00001044}
1045
1046/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Devang Patel6daf99b2009-11-10 22:05:35 +00001047Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
Victor Hernandez0418c102009-11-13 01:44:55 +00001048 BasicBlock *InsertAtEnd) {
Mike Stumpe4250392009-10-01 22:08:58 +00001049 if (!DeclareFn)
1050 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1051
1052 Value *Args[] = { Storage, D.getNode() };
Devang Patel6daf99b2009-11-10 22:05:35 +00001053 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);
Chris Lattnera45664f2008-11-10 02:56:27 +00001054}
Torok Edwin620f2802008-12-16 09:07:36 +00001055
Devang Patele4b27562009-08-28 23:24:31 +00001056
Devang Pateld2f79a12009-07-28 19:55:13 +00001057//===----------------------------------------------------------------------===//
Devang Patel98c65172009-07-30 18:25:15 +00001058// DebugInfoFinder implementations.
Devang Pateld2f79a12009-07-28 19:55:13 +00001059//===----------------------------------------------------------------------===//
1060
Devang Patel98c65172009-07-30 18:25:15 +00001061/// processModule - Process entire module and collect debug info.
1062void DebugInfoFinder::processModule(Module &M) {
Devang Patele4b27562009-08-28 23:24:31 +00001063
Devang Patelbeab41b2009-10-07 22:04:08 +00001064 MetadataContext &TheMetadata = M.getContext().getMetadata();
1065 unsigned MDDbgKind = TheMetadata.getMDKind("dbg");
Devang Patel70d75ca2009-11-12 19:02:56 +00001066
Devang Pateld2f79a12009-07-28 19:55:13 +00001067 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1068 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
1069 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
1070 ++BI) {
Devang Patel70d75ca2009-11-12 19:02:56 +00001071 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
Devang Patelb4d31302009-07-31 18:18:52 +00001072 processDeclare(DDI);
Devang Patel6daf99b2009-11-10 22:05:35 +00001073 else if (MDDbgKind)
1074 if (MDNode *L = TheMetadata.getMD(MDDbgKind, BI))
1075 processLocation(DILocation(L));
Devang Pateld2f79a12009-07-28 19:55:13 +00001076 }
Devang Patele4b27562009-08-28 23:24:31 +00001077
1078 NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv");
1079 if (!NMD)
1080 return;
1081
1082 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
1083 DIGlobalVariable DIG(cast<MDNode>(NMD->getElement(i)));
Devang Pateld2f79a12009-07-28 19:55:13 +00001084 if (addGlobalVariable(DIG)) {
1085 addCompileUnit(DIG.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +00001086 processType(DIG.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +00001087 }
1088 }
1089}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001090
Devang Patel6daf99b2009-11-10 22:05:35 +00001091/// processLocation - Process DILocation.
1092void DebugInfoFinder::processLocation(DILocation Loc) {
1093 if (Loc.isNull()) return;
1094 DIScope S(Loc.getScope().getNode());
1095 if (S.isNull()) return;
1096 if (S.isCompileUnit())
1097 addCompileUnit(DICompileUnit(S.getNode()));
1098 else if (S.isSubprogram())
1099 processSubprogram(DISubprogram(S.getNode()));
1100 else if (S.isLexicalBlock())
1101 processLexicalBlock(DILexicalBlock(S.getNode()));
1102 processLocation(Loc.getOrigLocation());
1103}
1104
Devang Patel98c65172009-07-30 18:25:15 +00001105/// processType - Process DIType.
1106void DebugInfoFinder::processType(DIType DT) {
Devang Patel72bcdb62009-08-10 22:09:58 +00001107 if (!addType(DT))
Devang Pateld2f79a12009-07-28 19:55:13 +00001108 return;
1109
1110 addCompileUnit(DT.getCompileUnit());
Devang Patel6ceea332009-08-31 18:49:10 +00001111 if (DT.isCompositeType()) {
Devang Patele4b27562009-08-28 23:24:31 +00001112 DICompositeType DCT(DT.getNode());
Devang Patel98c65172009-07-30 18:25:15 +00001113 processType(DCT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001114 DIArray DA = DCT.getTypeArray();
1115 if (!DA.isNull())
1116 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1117 DIDescriptor D = DA.getElement(i);
Devang Patele4b27562009-08-28 23:24:31 +00001118 DIType TypeE = DIType(D.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001119 if (!TypeE.isNull())
Devang Patel98c65172009-07-30 18:25:15 +00001120 processType(TypeE);
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001121 else
Devang Patele4b27562009-08-28 23:24:31 +00001122 processSubprogram(DISubprogram(D.getNode()));
Devang Pateld2f79a12009-07-28 19:55:13 +00001123 }
Devang Patel6ceea332009-08-31 18:49:10 +00001124 } else if (DT.isDerivedType()) {
Devang Patele4b27562009-08-28 23:24:31 +00001125 DIDerivedType DDT(DT.getNode());
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001126 if (!DDT.isNull())
Devang Patel98c65172009-07-30 18:25:15 +00001127 processType(DDT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001128 }
1129}
1130
Devang Patelbeab41b2009-10-07 22:04:08 +00001131/// processLexicalBlock
1132void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
1133 if (LB.isNull())
1134 return;
1135 DIScope Context = LB.getContext();
1136 if (Context.isLexicalBlock())
1137 return processLexicalBlock(DILexicalBlock(Context.getNode()));
1138 else
1139 return processSubprogram(DISubprogram(Context.getNode()));
1140}
1141
Devang Patel98c65172009-07-30 18:25:15 +00001142/// processSubprogram - Process DISubprogram.
1143void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Patele802f1c2009-07-30 17:30:23 +00001144 if (SP.isNull())
1145 return;
Devang Pateld2f79a12009-07-28 19:55:13 +00001146 if (!addSubprogram(SP))
1147 return;
1148 addCompileUnit(SP.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +00001149 processType(SP.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +00001150}
1151
Devang Patelb4d31302009-07-31 18:18:52 +00001152/// processDeclare - Process DbgDeclareInst.
1153void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
Devang Patele4b27562009-08-28 23:24:31 +00001154 DIVariable DV(cast<MDNode>(DDI->getVariable()));
Devang Patelb4d31302009-07-31 18:18:52 +00001155 if (DV.isNull())
1156 return;
1157
Devang Patele4b27562009-08-28 23:24:31 +00001158 if (!NodesSeen.insert(DV.getNode()))
Devang Patelb4d31302009-07-31 18:18:52 +00001159 return;
1160
1161 addCompileUnit(DV.getCompileUnit());
1162 processType(DV.getType());
1163}
1164
Devang Patel72bcdb62009-08-10 22:09:58 +00001165/// addType - Add type into Tys.
1166bool DebugInfoFinder::addType(DIType DT) {
1167 if (DT.isNull())
1168 return false;
1169
Devang Patele4b27562009-08-28 23:24:31 +00001170 if (!NodesSeen.insert(DT.getNode()))
Devang Patel72bcdb62009-08-10 22:09:58 +00001171 return false;
1172
Devang Patele4b27562009-08-28 23:24:31 +00001173 TYs.push_back(DT.getNode());
Devang Patel72bcdb62009-08-10 22:09:58 +00001174 return true;
1175}
1176
Devang Pateld2f79a12009-07-28 19:55:13 +00001177/// addCompileUnit - Add compile unit into CUs.
Devang Patel98c65172009-07-30 18:25:15 +00001178bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001179 if (CU.isNull())
1180 return false;
1181
Devang Patele4b27562009-08-28 23:24:31 +00001182 if (!NodesSeen.insert(CU.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001183 return false;
1184
Devang Patele4b27562009-08-28 23:24:31 +00001185 CUs.push_back(CU.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001186 return true;
1187}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001188
Devang Pateld2f79a12009-07-28 19:55:13 +00001189/// addGlobalVariable - Add global variable into GVs.
Devang Patel98c65172009-07-30 18:25:15 +00001190bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001191 if (DIG.isNull())
1192 return false;
1193
Devang Patele4b27562009-08-28 23:24:31 +00001194 if (!NodesSeen.insert(DIG.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001195 return false;
1196
Devang Patele4b27562009-08-28 23:24:31 +00001197 GVs.push_back(DIG.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001198 return true;
1199}
1200
1201// addSubprogram - Add subprgoram into SPs.
Devang Patel98c65172009-07-30 18:25:15 +00001202bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001203 if (SP.isNull())
1204 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001205
Devang Patele4b27562009-08-28 23:24:31 +00001206 if (!NodesSeen.insert(SP.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001207 return false;
1208
Devang Patele4b27562009-08-28 23:24:31 +00001209 SPs.push_back(SP.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001210 return true;
1211}
1212
Torok Edwin620f2802008-12-16 09:07:36 +00001213namespace llvm {
Bill Wendlingdc817b62009-05-14 18:26:15 +00001214 /// findStopPoint - Find the stoppoint coressponding to this instruction, that
1215 /// is the stoppoint that dominates this instruction.
1216 const DbgStopPointInst *findStopPoint(const Instruction *Inst) {
Torok Edwin620f2802008-12-16 09:07:36 +00001217 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(Inst))
1218 return DSI;
1219
1220 const BasicBlock *BB = Inst->getParent();
1221 BasicBlock::const_iterator I = Inst, B;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001222 while (BB) {
Torok Edwin620f2802008-12-16 09:07:36 +00001223 B = BB->begin();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001224
Torok Edwin620f2802008-12-16 09:07:36 +00001225 // A BB consisting only of a terminator can't have a stoppoint.
Bill Wendlingdc817b62009-05-14 18:26:15 +00001226 while (I != B) {
1227 --I;
1228 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
1229 return DSI;
Torok Edwin620f2802008-12-16 09:07:36 +00001230 }
Bill Wendlingdc817b62009-05-14 18:26:15 +00001231
1232 // This BB didn't have a stoppoint: if there is only one predecessor, look
1233 // for a stoppoint there. We could use getIDom(), but that would require
1234 // dominator info.
Torok Edwin620f2802008-12-16 09:07:36 +00001235 BB = I->getParent()->getUniquePredecessor();
1236 if (BB)
1237 I = BB->getTerminator();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001238 }
1239
Torok Edwin620f2802008-12-16 09:07:36 +00001240 return 0;
1241 }
1242
Bill Wendlingdc817b62009-05-14 18:26:15 +00001243 /// findBBStopPoint - Find the stoppoint corresponding to first real
1244 /// (non-debug intrinsic) instruction in this Basic Block, and return the
1245 /// stoppoint for it.
1246 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB) {
1247 for(BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001248 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
1249 return DSI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001250
1251 // Fallback to looking for stoppoint of unique predecessor. Useful if this
1252 // BB contains no stoppoints, but unique predecessor does.
Torok Edwin620f2802008-12-16 09:07:36 +00001253 BB = BB->getUniquePredecessor();
1254 if (BB)
1255 return findStopPoint(BB->getTerminator());
Bill Wendlingdc817b62009-05-14 18:26:15 +00001256
Torok Edwin620f2802008-12-16 09:07:36 +00001257 return 0;
1258 }
1259
Bill Wendlingdc817b62009-05-14 18:26:15 +00001260 Value *findDbgGlobalDeclare(GlobalVariable *V) {
Torok Edwinff7d0e92009-03-10 13:41:26 +00001261 const Module *M = V->getParent();
Devang Patele4b27562009-08-28 23:24:31 +00001262 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1263 if (!NMD)
1264 return 0;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001265
Devang Patele4b27562009-08-28 23:24:31 +00001266 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
1267 DIGlobalVariable DIG(cast_or_null<MDNode>(NMD->getElement(i)));
1268 if (DIG.isNull())
1269 continue;
1270 if (DIG.getGlobal() == V)
1271 return DIG.getNode();
Torok Edwinff7d0e92009-03-10 13:41:26 +00001272 }
Torok Edwinff7d0e92009-03-10 13:41:26 +00001273 return 0;
1274 }
1275
Bill Wendlingdc817b62009-05-14 18:26:15 +00001276 /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
Torok Edwin620f2802008-12-16 09:07:36 +00001277 /// It looks through pointer casts too.
Bill Wendlingdc817b62009-05-14 18:26:15 +00001278 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts) {
Torok Edwin620f2802008-12-16 09:07:36 +00001279 if (stripCasts) {
1280 V = V->stripPointerCasts();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001281
Torok Edwin620f2802008-12-16 09:07:36 +00001282 // Look for the bitcast.
1283 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001284 I != E; ++I)
Devang Patel94dfaec2009-10-29 18:20:34 +00001285 if (isa<BitCastInst>(I)) {
1286 const DbgDeclareInst *DDI = findDbgDeclare(*I, false);
1287 if (DDI) return DDI;
1288 }
Torok Edwin620f2802008-12-16 09:07:36 +00001289 return 0;
1290 }
1291
Bill Wendlingdc817b62009-05-14 18:26:15 +00001292 // Find llvm.dbg.declare among uses of the instruction.
Torok Edwin620f2802008-12-16 09:07:36 +00001293 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001294 I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001295 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I))
1296 return DDI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001297
Torok Edwin620f2802008-12-16 09:07:36 +00001298 return 0;
1299 }
Torok Edwinff7d0e92009-03-10 13:41:26 +00001300
Devang Patel5ccdd102009-09-29 18:40:58 +00001301bool getLocationInfo(const Value *V, std::string &DisplayName,
1302 std::string &Type, unsigned &LineNo, std::string &File,
Bill Wendlingdc817b62009-05-14 18:26:15 +00001303 std::string &Dir) {
Torok Edwinff7d0e92009-03-10 13:41:26 +00001304 DICompileUnit Unit;
1305 DIType TypeD;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001306
Torok Edwinff7d0e92009-03-10 13:41:26 +00001307 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1308 Value *DIGV = findDbgGlobalDeclare(GV);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001309 if (!DIGV) return false;
Devang Patele4b27562009-08-28 23:24:31 +00001310 DIGlobalVariable Var(cast<MDNode>(DIGV));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001311
Devang Patel5ccdd102009-09-29 18:40:58 +00001312 if (const char *D = Var.getDisplayName())
1313 DisplayName = D;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001314 LineNo = Var.getLineNumber();
1315 Unit = Var.getCompileUnit();
1316 TypeD = Var.getType();
1317 } else {
1318 const DbgDeclareInst *DDI = findDbgDeclare(V);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001319 if (!DDI) return false;
Devang Patele4b27562009-08-28 23:24:31 +00001320 DIVariable Var(cast<MDNode>(DDI->getVariable()));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001321
Devang Patel5ccdd102009-09-29 18:40:58 +00001322 if (const char *D = Var.getName())
1323 DisplayName = D;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001324 LineNo = Var.getLineNumber();
1325 Unit = Var.getCompileUnit();
1326 TypeD = Var.getType();
1327 }
Bill Wendlingdc817b62009-05-14 18:26:15 +00001328
Devang Patel5ccdd102009-09-29 18:40:58 +00001329 if (const char *T = TypeD.getName())
1330 Type = T;
1331 if (const char *F = Unit.getFilename())
1332 File = F;
1333 if (const char *D = Unit.getDirectory())
1334 Dir = D;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001335 return true;
1336 }
Devang Patel13e16b62009-06-26 01:49:18 +00001337
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001338 /// isValidDebugInfoIntrinsic - Return true if SPI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001339 /// info intrinsic.
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001340 bool isValidDebugInfoIntrinsic(DbgStopPointInst &SPI,
Devang Patel9e529c32009-07-02 01:15:24 +00001341 CodeGenOpt::Level OptLev) {
1342 return DIDescriptor::ValidDebugInfo(SPI.getContext(), OptLev);
1343 }
1344
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001345 /// isValidDebugInfoIntrinsic - Return true if FSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001346 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001347 bool isValidDebugInfoIntrinsic(DbgFuncStartInst &FSI,
1348 CodeGenOpt::Level OptLev) {
1349 return DIDescriptor::ValidDebugInfo(FSI.getSubprogram(), OptLev);
1350 }
1351
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001352 /// isValidDebugInfoIntrinsic - Return true if RSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001353 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001354 bool isValidDebugInfoIntrinsic(DbgRegionStartInst &RSI,
1355 CodeGenOpt::Level OptLev) {
1356 return DIDescriptor::ValidDebugInfo(RSI.getContext(), OptLev);
1357 }
1358
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001359 /// isValidDebugInfoIntrinsic - Return true if REI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001360 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001361 bool isValidDebugInfoIntrinsic(DbgRegionEndInst &REI,
1362 CodeGenOpt::Level OptLev) {
1363 return DIDescriptor::ValidDebugInfo(REI.getContext(), OptLev);
1364 }
1365
1366
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001367 /// isValidDebugInfoIntrinsic - Return true if DI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001368 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001369 bool isValidDebugInfoIntrinsic(DbgDeclareInst &DI,
1370 CodeGenOpt::Level OptLev) {
1371 return DIDescriptor::ValidDebugInfo(DI.getVariable(), OptLev);
1372 }
1373
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001374 /// ExtractDebugLocation - Extract debug location information
Devang Patel9e529c32009-07-02 01:15:24 +00001375 /// from llvm.dbg.stoppoint intrinsic.
1376 DebugLoc ExtractDebugLocation(DbgStopPointInst &SPI,
Devang Patel9e529c32009-07-02 01:15:24 +00001377 DebugLocTracker &DebugLocInfo) {
1378 DebugLoc DL;
1379 Value *Context = SPI.getContext();
Devang Patel9e529c32009-07-02 01:15:24 +00001380
1381 // If this location is already tracked then use it.
Devang Patela1434042009-10-01 01:15:28 +00001382 DebugLocTuple Tuple(cast<MDNode>(Context), NULL, SPI.getLine(),
Devang Patel9e529c32009-07-02 01:15:24 +00001383 SPI.getColumn());
1384 DenseMap<DebugLocTuple, unsigned>::iterator II
1385 = DebugLocInfo.DebugIdMap.find(Tuple);
1386 if (II != DebugLocInfo.DebugIdMap.end())
1387 return DebugLoc::get(II->second);
1388
1389 // Add a new location entry.
1390 unsigned Id = DebugLocInfo.DebugLocations.size();
1391 DebugLocInfo.DebugLocations.push_back(Tuple);
1392 DebugLocInfo.DebugIdMap[Tuple] = Id;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001393
Devang Patel9e529c32009-07-02 01:15:24 +00001394 return DebugLoc::get(Id);
1395 }
1396
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001397 /// ExtractDebugLocation - Extract debug location information
Devang Patel1b75f442009-09-16 18:20:05 +00001398 /// from DILocation.
1399 DebugLoc ExtractDebugLocation(DILocation &Loc,
1400 DebugLocTracker &DebugLocInfo) {
1401 DebugLoc DL;
1402 MDNode *Context = Loc.getScope().getNode();
Devang Patela1434042009-10-01 01:15:28 +00001403 MDNode *InlinedLoc = NULL;
1404 if (!Loc.getOrigLocation().isNull())
1405 InlinedLoc = Loc.getOrigLocation().getNode();
Devang Patel1b75f442009-09-16 18:20:05 +00001406 // If this location is already tracked then use it.
Devang Patela1434042009-10-01 01:15:28 +00001407 DebugLocTuple Tuple(Context, InlinedLoc, Loc.getLineNumber(),
Daniel Dunbara279bc32009-09-20 02:20:51 +00001408 Loc.getColumnNumber());
Devang Patel1b75f442009-09-16 18:20:05 +00001409 DenseMap<DebugLocTuple, unsigned>::iterator II
1410 = DebugLocInfo.DebugIdMap.find(Tuple);
1411 if (II != DebugLocInfo.DebugIdMap.end())
1412 return DebugLoc::get(II->second);
1413
1414 // Add a new location entry.
1415 unsigned Id = DebugLocInfo.DebugLocations.size();
1416 DebugLocInfo.DebugLocations.push_back(Tuple);
1417 DebugLocInfo.DebugIdMap[Tuple] = Id;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001418
Devang Patel1b75f442009-09-16 18:20:05 +00001419 return DebugLoc::get(Id);
1420 }
1421
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001422 /// ExtractDebugLocation - Extract debug location information
Devang Patel9e529c32009-07-02 01:15:24 +00001423 /// from llvm.dbg.func_start intrinsic.
1424 DebugLoc ExtractDebugLocation(DbgFuncStartInst &FSI,
Devang Patel9e529c32009-07-02 01:15:24 +00001425 DebugLocTracker &DebugLocInfo) {
1426 DebugLoc DL;
1427 Value *SP = FSI.getSubprogram();
Devang Patel9e529c32009-07-02 01:15:24 +00001428
Devang Patele4b27562009-08-28 23:24:31 +00001429 DISubprogram Subprogram(cast<MDNode>(SP));
Devang Patel9e529c32009-07-02 01:15:24 +00001430 unsigned Line = Subprogram.getLineNumber();
1431 DICompileUnit CU(Subprogram.getCompileUnit());
1432
1433 // If this location is already tracked then use it.
Devang Patela1434042009-10-01 01:15:28 +00001434 DebugLocTuple Tuple(CU.getNode(), NULL, Line, /* Column */ 0);
Devang Patel9e529c32009-07-02 01:15:24 +00001435 DenseMap<DebugLocTuple, unsigned>::iterator II
1436 = DebugLocInfo.DebugIdMap.find(Tuple);
1437 if (II != DebugLocInfo.DebugIdMap.end())
1438 return DebugLoc::get(II->second);
1439
1440 // Add a new location entry.
1441 unsigned Id = DebugLocInfo.DebugLocations.size();
1442 DebugLocInfo.DebugLocations.push_back(Tuple);
1443 DebugLocInfo.DebugIdMap[Tuple] = Id;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001444
Devang Patel9e529c32009-07-02 01:15:24 +00001445 return DebugLoc::get(Id);
1446 }
Torok Edwin620f2802008-12-16 09:07:36 +00001447}