blob: 390ebf93e2a9a4c62207b2e04232d81c59f415a0 [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),
Bill Wendlingdc817b62009-05-14 18:26:15 +0000611 DeclareFn(0) {
Owen Andersond7f2a6c2009-08-05 23:16:16 +0000612 EmptyStructPtr = PointerType::getUnqual(StructType::get(VMContext));
Chris Lattner497a7a82008-11-10 04:10:34 +0000613}
614
Chris Lattnera45664f2008-11-10 02:56:27 +0000615Constant *DIFactory::GetTagConstant(unsigned TAG) {
Devang Patel6906ba52009-01-20 19:22:03 +0000616 assert((TAG & LLVMDebugVersionMask) == 0 &&
Chris Lattnera45664f2008-11-10 02:56:27 +0000617 "Tag too large for debug encoding!");
Owen Anderson1d0be152009-08-13 21:58:54 +0000618 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
Chris Lattnera45664f2008-11-10 02:56:27 +0000619}
620
Chris Lattnera45664f2008-11-10 02:56:27 +0000621//===----------------------------------------------------------------------===//
622// DIFactory: Primary Constructors
623//===----------------------------------------------------------------------===//
624
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000625/// GetOrCreateArray - Create an descriptor for an array of descriptors.
Chris Lattnera45664f2008-11-10 02:56:27 +0000626/// This implicitly uniques the arrays created.
627DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
Devang Patele4b27562009-08-28 23:24:31 +0000628 SmallVector<Value*, 16> Elts;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000629
Devang Patele4b27562009-08-28 23:24:31 +0000630 if (NumTys == 0)
631 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)));
632 else
633 for (unsigned i = 0; i != NumTys; ++i)
634 Elts.push_back(Tys[i].getNode());
Devang Patel82459882009-08-26 05:01:18 +0000635
Devang Patele4b27562009-08-28 23:24:31 +0000636 return DIArray(MDNode::get(VMContext,Elts.data(), Elts.size()));
Chris Lattnera45664f2008-11-10 02:56:27 +0000637}
638
639/// GetOrCreateSubrange - Create a descriptor for a value range. This
640/// implicitly uniques the values returned.
641DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
Devang Patele4b27562009-08-28 23:24:31 +0000642 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000643 GetTagConstant(dwarf::DW_TAG_subrange_type),
Owen Anderson1d0be152009-08-13 21:58:54 +0000644 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
645 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
Chris Lattnera45664f2008-11-10 02:56:27 +0000646 };
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000647
Devang Patele4b27562009-08-28 23:24:31 +0000648 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000649}
650
651
652
653/// CreateCompileUnit - Create a new descriptor for the specified compile
654/// unit. Note that this does not unique compile units within the module.
655DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
Devang Patel5ccdd102009-09-29 18:40:58 +0000656 StringRef Filename,
657 StringRef Directory,
658 StringRef Producer,
Devang Pateldd9db662009-01-30 18:20:31 +0000659 bool isMain,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000660 bool isOptimized,
Devang Patel13319ce2009-02-17 22:43:44 +0000661 const char *Flags,
662 unsigned RunTimeVer) {
Devang Patele4b27562009-08-28 23:24:31 +0000663 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000664 GetTagConstant(dwarf::DW_TAG_compile_unit),
Devang Patele4b27562009-08-28 23:24:31 +0000665 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Owen Anderson1d0be152009-08-13 21:58:54 +0000666 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
Devang Patele4b27562009-08-28 23:24:31 +0000667 MDString::get(VMContext, Filename),
668 MDString::get(VMContext, Directory),
669 MDString::get(VMContext, Producer),
Owen Anderson1d0be152009-08-13 21:58:54 +0000670 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
671 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
Devang Patele4b27562009-08-28 23:24:31 +0000672 MDString::get(VMContext, Flags),
Owen Anderson1d0be152009-08-13 21:58:54 +0000673 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
Chris Lattnera45664f2008-11-10 02:56:27 +0000674 };
Devang Patele4b27562009-08-28 23:24:31 +0000675
676 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000677}
678
679/// CreateEnumerator - Create a single enumerator value.
Devang Patel5ccdd102009-09-29 18:40:58 +0000680DIEnumerator DIFactory::CreateEnumerator(StringRef Name, uint64_t Val){
Devang Patele4b27562009-08-28 23:24:31 +0000681 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000682 GetTagConstant(dwarf::DW_TAG_enumerator),
Devang Patele4b27562009-08-28 23:24:31 +0000683 MDString::get(VMContext, Name),
Owen Anderson1d0be152009-08-13 21:58:54 +0000684 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
Chris Lattnera45664f2008-11-10 02:56:27 +0000685 };
Devang Patele4b27562009-08-28 23:24:31 +0000686 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000687}
688
689
690/// CreateBasicType - Create a basic type like int, float, etc.
691DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
Devang Patel5ccdd102009-09-29 18:40:58 +0000692 StringRef Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000693 DICompileUnit CompileUnit,
694 unsigned LineNumber,
695 uint64_t SizeInBits,
696 uint64_t AlignInBits,
697 uint64_t OffsetInBits, unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000698 unsigned Encoding) {
Devang Patele4b27562009-08-28 23:24:31 +0000699 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000700 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patele4b27562009-08-28 23:24:31 +0000701 Context.getNode(),
702 MDString::get(VMContext, Name),
703 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000704 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
705 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
706 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
707 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
708 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
709 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
Chris Lattnera45664f2008-11-10 02:56:27 +0000710 };
Devang Patele4b27562009-08-28 23:24:31 +0000711 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000712}
713
Devang Patelac16d442009-10-26 16:54:35 +0000714
715/// CreateBasicType - Create a basic type like int, float, etc.
716DIBasicType DIFactory::CreateBasicTypeEx(DIDescriptor Context,
717 StringRef Name,
718 DICompileUnit CompileUnit,
719 unsigned LineNumber,
720 Constant *SizeInBits,
721 Constant *AlignInBits,
722 Constant *OffsetInBits, unsigned Flags,
723 unsigned Encoding) {
724 Value *Elts[] = {
725 GetTagConstant(dwarf::DW_TAG_base_type),
726 Context.getNode(),
727 MDString::get(VMContext, Name),
728 CompileUnit.getNode(),
729 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
730 SizeInBits,
731 AlignInBits,
732 OffsetInBits,
733 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
734 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
735 };
736 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
737}
738
739
Chris Lattnera45664f2008-11-10 02:56:27 +0000740/// CreateDerivedType - Create a derived type like const qualified type,
741/// pointer, typedef, etc.
742DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
743 DIDescriptor Context,
Devang Patel5ccdd102009-09-29 18:40:58 +0000744 StringRef Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000745 DICompileUnit CompileUnit,
746 unsigned LineNumber,
747 uint64_t SizeInBits,
748 uint64_t AlignInBits,
749 uint64_t OffsetInBits,
750 unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000751 DIType DerivedFrom) {
Devang Patele4b27562009-08-28 23:24:31 +0000752 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000753 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000754 Context.getNode(),
755 MDString::get(VMContext, Name),
756 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000757 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
758 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
759 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
760 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
761 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patele4b27562009-08-28 23:24:31 +0000762 DerivedFrom.getNode(),
Chris Lattnera45664f2008-11-10 02:56:27 +0000763 };
Devang Patele4b27562009-08-28 23:24:31 +0000764 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000765}
766
Devang Patelac16d442009-10-26 16:54:35 +0000767
768/// CreateDerivedType - Create a derived type like const qualified type,
769/// pointer, typedef, etc.
770DIDerivedType DIFactory::CreateDerivedTypeEx(unsigned Tag,
771 DIDescriptor Context,
772 StringRef Name,
773 DICompileUnit CompileUnit,
774 unsigned LineNumber,
775 Constant *SizeInBits,
776 Constant *AlignInBits,
777 Constant *OffsetInBits,
778 unsigned Flags,
779 DIType DerivedFrom) {
780 Value *Elts[] = {
781 GetTagConstant(Tag),
782 Context.getNode(),
783 MDString::get(VMContext, Name),
784 CompileUnit.getNode(),
785 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
786 SizeInBits,
787 AlignInBits,
788 OffsetInBits,
789 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
790 DerivedFrom.getNode(),
791 };
792 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
793}
794
795
Chris Lattnera45664f2008-11-10 02:56:27 +0000796/// CreateCompositeType - Create a composite type like array, struct, etc.
797DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
798 DIDescriptor Context,
Devang Patel5ccdd102009-09-29 18:40:58 +0000799 StringRef Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000800 DICompileUnit CompileUnit,
801 unsigned LineNumber,
802 uint64_t SizeInBits,
803 uint64_t AlignInBits,
804 uint64_t OffsetInBits,
805 unsigned Flags,
806 DIType DerivedFrom,
Devang Patel13319ce2009-02-17 22:43:44 +0000807 DIArray Elements,
808 unsigned RuntimeLang) {
Owen Andersone277fed2009-07-07 16:31:25 +0000809
Devang Patele4b27562009-08-28 23:24:31 +0000810 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000811 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000812 Context.getNode(),
813 MDString::get(VMContext, Name),
814 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000815 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
816 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
817 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
818 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
819 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patele4b27562009-08-28 23:24:31 +0000820 DerivedFrom.getNode(),
821 Elements.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000822 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
Chris Lattnera45664f2008-11-10 02:56:27 +0000823 };
Devang Patele4b27562009-08-28 23:24:31 +0000824 return DICompositeType(MDNode::get(VMContext, &Elts[0], 12));
Chris Lattnera45664f2008-11-10 02:56:27 +0000825}
826
827
Devang Patelac16d442009-10-26 16:54:35 +0000828/// CreateCompositeType - Create a composite type like array, struct, etc.
829DICompositeType DIFactory::CreateCompositeTypeEx(unsigned Tag,
830 DIDescriptor Context,
831 StringRef Name,
832 DICompileUnit CompileUnit,
833 unsigned LineNumber,
834 Constant *SizeInBits,
835 Constant *AlignInBits,
836 Constant *OffsetInBits,
837 unsigned Flags,
838 DIType DerivedFrom,
839 DIArray Elements,
840 unsigned RuntimeLang) {
841
842 Value *Elts[] = {
843 GetTagConstant(Tag),
844 Context.getNode(),
845 MDString::get(VMContext, Name),
846 CompileUnit.getNode(),
847 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
848 SizeInBits,
849 AlignInBits,
850 OffsetInBits,
851 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
852 DerivedFrom.getNode(),
853 Elements.getNode(),
854 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
855 };
856 return DICompositeType(MDNode::get(VMContext, &Elts[0], 12));
857}
858
859
Chris Lattnera45664f2008-11-10 02:56:27 +0000860/// CreateSubprogram - Create a new descriptor for the specified subprogram.
861/// See comments in DISubprogram for descriptions of these fields. This
862/// method does not unique the generated descriptors.
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000863DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
Devang Patel5ccdd102009-09-29 18:40:58 +0000864 StringRef Name,
865 StringRef DisplayName,
866 StringRef LinkageName,
Chris Lattnera45664f2008-11-10 02:56:27 +0000867 DICompileUnit CompileUnit,
868 unsigned LineNo, DIType Type,
869 bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000870 bool isDefinition) {
Devang Patel854967e2008-12-17 22:39:29 +0000871
Devang Patele4b27562009-08-28 23:24:31 +0000872 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000873 GetTagConstant(dwarf::DW_TAG_subprogram),
Devang Patele4b27562009-08-28 23:24:31 +0000874 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
875 Context.getNode(),
876 MDString::get(VMContext, Name),
877 MDString::get(VMContext, DisplayName),
878 MDString::get(VMContext, LinkageName),
879 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000880 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000881 Type.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000882 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
883 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition)
Chris Lattnera45664f2008-11-10 02:56:27 +0000884 };
Devang Patele4b27562009-08-28 23:24:31 +0000885 return DISubprogram(MDNode::get(VMContext, &Elts[0], 11));
Chris Lattnera45664f2008-11-10 02:56:27 +0000886}
887
888/// CreateGlobalVariable - Create a new descriptor for the specified global.
889DIGlobalVariable
Devang Patel5ccdd102009-09-29 18:40:58 +0000890DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
891 StringRef DisplayName,
892 StringRef LinkageName,
Chris Lattnera45664f2008-11-10 02:56:27 +0000893 DICompileUnit CompileUnit,
894 unsigned LineNo, DIType Type,bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000895 bool isDefinition, llvm::GlobalVariable *Val) {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000896 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000897 GetTagConstant(dwarf::DW_TAG_variable),
Devang Patele4b27562009-08-28 23:24:31 +0000898 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
899 Context.getNode(),
900 MDString::get(VMContext, Name),
901 MDString::get(VMContext, DisplayName),
902 MDString::get(VMContext, LinkageName),
903 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000904 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000905 Type.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000906 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
907 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
Devang Patele4b27562009-08-28 23:24:31 +0000908 Val
Chris Lattnera45664f2008-11-10 02:56:27 +0000909 };
Devang Patele4b27562009-08-28 23:24:31 +0000910
911 Value *const *Vs = &Elts[0];
912 MDNode *Node = MDNode::get(VMContext,Vs, 12);
913
914 // Create a named metadata so that we do not lose this mdnode.
915 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
916 NMD->addElement(Node);
917
918 return DIGlobalVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +0000919}
920
921
922/// CreateVariable - Create a new descriptor for the specified variable.
923DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
Devang Patel5ccdd102009-09-29 18:40:58 +0000924 StringRef Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000925 DICompileUnit CompileUnit, unsigned LineNo,
Devang Pateldd9db662009-01-30 18:20:31 +0000926 DIType Type) {
Devang Patele4b27562009-08-28 23:24:31 +0000927 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000928 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000929 Context.getNode(),
930 MDString::get(VMContext, Name),
931 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000932 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000933 Type.getNode(),
Chris Lattnera45664f2008-11-10 02:56:27 +0000934 };
Devang Patele4b27562009-08-28 23:24:31 +0000935 return DIVariable(MDNode::get(VMContext, &Elts[0], 6));
Chris Lattnera45664f2008-11-10 02:56:27 +0000936}
937
938
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000939/// CreateComplexVariable - Create a new descriptor for the specified variable
940/// which has a complex address expression for its address.
941DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
942 const std::string &Name,
943 DICompileUnit CompileUnit,
944 unsigned LineNo,
945 DIType Type, SmallVector<Value *, 9> &addr) {
946 SmallVector<Value *, 9> Elts;
947 Elts.push_back(GetTagConstant(Tag));
948 Elts.push_back(Context.getNode());
949 Elts.push_back(MDString::get(VMContext, Name));
950 Elts.push_back(CompileUnit.getNode());
951 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
952 Elts.push_back(Type.getNode());
953 Elts.insert(Elts.end(), addr.begin(), addr.end());
954
955 return DIVariable(MDNode::get(VMContext, &Elts[0], 6+addr.size()));
956}
957
958
Chris Lattnera45664f2008-11-10 02:56:27 +0000959/// CreateBlock - This creates a descriptor for a lexical block with the
Owen Anderson99035272009-07-07 17:12:53 +0000960/// specified parent VMContext.
Devang Patel5e005d82009-08-31 22:00:15 +0000961DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context) {
Devang Patele4b27562009-08-28 23:24:31 +0000962 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000963 GetTagConstant(dwarf::DW_TAG_lexical_block),
Devang Patele4b27562009-08-28 23:24:31 +0000964 Context.getNode()
Chris Lattnera45664f2008-11-10 02:56:27 +0000965 };
Devang Patel5e005d82009-08-31 22:00:15 +0000966 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 2));
Chris Lattnera45664f2008-11-10 02:56:27 +0000967}
968
Devang Patelf98d8fe2009-09-01 01:14:15 +0000969/// CreateLocation - Creates a debug info location.
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000970DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
Daniel Dunbara279bc32009-09-20 02:20:51 +0000971 DIScope S, DILocation OrigLoc) {
Devang Patelf98d8fe2009-09-01 01:14:15 +0000972 Value *Elts[] = {
973 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
974 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
975 S.getNode(),
976 OrigLoc.getNode(),
977 };
978 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
979}
980
Chris Lattnera45664f2008-11-10 02:56:27 +0000981
982//===----------------------------------------------------------------------===//
983// DIFactory: Routines for inserting code into a function
984//===----------------------------------------------------------------------===//
985
986/// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
987/// inserting it at the end of the specified basic block.
988void DIFactory::InsertStopPoint(DICompileUnit CU, unsigned LineNo,
989 unsigned ColNo, BasicBlock *BB) {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000990
Chris Lattnera45664f2008-11-10 02:56:27 +0000991 // Lazily construct llvm.dbg.stoppoint function.
992 if (!StopPointFn)
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000993 StopPointFn = llvm::Intrinsic::getDeclaration(&M,
Chris Lattnera45664f2008-11-10 02:56:27 +0000994 llvm::Intrinsic::dbg_stoppoint);
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000995
Chris Lattnera45664f2008-11-10 02:56:27 +0000996 // Invoke llvm.dbg.stoppoint
997 Value *Args[] = {
Owen Anderson1d0be152009-08-13 21:58:54 +0000998 ConstantInt::get(llvm::Type::getInt32Ty(VMContext), LineNo),
999 ConstantInt::get(llvm::Type::getInt32Ty(VMContext), ColNo),
Devang Patele4b27562009-08-28 23:24:31 +00001000 CU.getNode()
Chris Lattnera45664f2008-11-10 02:56:27 +00001001 };
1002 CallInst::Create(StopPointFn, Args, Args+3, "", BB);
1003}
1004
1005/// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
1006/// mark the start of the specified subprogram.
1007void DIFactory::InsertSubprogramStart(DISubprogram SP, BasicBlock *BB) {
1008 // Lazily construct llvm.dbg.func.start.
1009 if (!FuncStartFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +00001010 FuncStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_func_start);
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001011
Chris Lattnera45664f2008-11-10 02:56:27 +00001012 // Call llvm.dbg.func.start which also implicitly sets a stoppoint.
Devang Patele4b27562009-08-28 23:24:31 +00001013 CallInst::Create(FuncStartFn, SP.getNode(), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +00001014}
1015
1016/// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
1017/// mark the start of a region for the specified scoping descriptor.
1018void DIFactory::InsertRegionStart(DIDescriptor D, BasicBlock *BB) {
1019 // Lazily construct llvm.dbg.region.start function.
1020 if (!RegionStartFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +00001021 RegionStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_start);
1022
Chris Lattnera45664f2008-11-10 02:56:27 +00001023 // Call llvm.dbg.func.start.
Devang Patele4b27562009-08-28 23:24:31 +00001024 CallInst::Create(RegionStartFn, D.getNode(), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +00001025}
1026
Chris Lattnera45664f2008-11-10 02:56:27 +00001027/// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
1028/// mark the end of a region for the specified scoping descriptor.
1029void DIFactory::InsertRegionEnd(DIDescriptor D, BasicBlock *BB) {
1030 // Lazily construct llvm.dbg.region.end function.
1031 if (!RegionEndFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +00001032 RegionEndFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_end);
1033
1034 // Call llvm.dbg.region.end.
Devang Patele4b27562009-08-28 23:24:31 +00001035 CallInst::Create(RegionEndFn, D.getNode(), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +00001036}
1037
1038/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Devang Patel6daf99b2009-11-10 22:05:35 +00001039Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
Mike Stumpe4250392009-10-01 22:08:58 +00001040 Instruction *InsertBefore) {
Chris Lattnera45664f2008-11-10 02:56:27 +00001041 // Cast the storage to a {}* for the call to llvm.dbg.declare.
Mike Stumpe4250392009-10-01 22:08:58 +00001042 Storage = new BitCastInst(Storage, EmptyStructPtr, "", InsertBefore);
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001043
Chris Lattnera45664f2008-11-10 02:56:27 +00001044 if (!DeclareFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +00001045 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1046
Devang Patele4b27562009-08-28 23:24:31 +00001047 Value *Args[] = { Storage, D.getNode() };
Devang Patel6daf99b2009-11-10 22:05:35 +00001048 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
Mike Stumpe4250392009-10-01 22:08:58 +00001049}
1050
1051/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Devang Patel6daf99b2009-11-10 22:05:35 +00001052Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
Mike Stumpe4250392009-10-01 22:08:58 +00001053 BasicBlock *InsertAtEnd) {
1054 // Cast the storage to a {}* for the call to llvm.dbg.declare.
1055 Storage = new BitCastInst(Storage, EmptyStructPtr, "", InsertAtEnd);
1056
1057 if (!DeclareFn)
1058 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1059
1060 Value *Args[] = { Storage, D.getNode() };
Devang Patel6daf99b2009-11-10 22:05:35 +00001061 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);
Chris Lattnera45664f2008-11-10 02:56:27 +00001062}
Torok Edwin620f2802008-12-16 09:07:36 +00001063
Devang Patele4b27562009-08-28 23:24:31 +00001064
Devang Pateld2f79a12009-07-28 19:55:13 +00001065//===----------------------------------------------------------------------===//
Devang Patel98c65172009-07-30 18:25:15 +00001066// DebugInfoFinder implementations.
Devang Pateld2f79a12009-07-28 19:55:13 +00001067//===----------------------------------------------------------------------===//
1068
Devang Patel98c65172009-07-30 18:25:15 +00001069/// processModule - Process entire module and collect debug info.
1070void DebugInfoFinder::processModule(Module &M) {
Devang Patele4b27562009-08-28 23:24:31 +00001071
Devang Patelbeab41b2009-10-07 22:04:08 +00001072#ifdef ATTACH_DEBUG_INFO_TO_AN_INSN
1073 MetadataContext &TheMetadata = M.getContext().getMetadata();
1074 unsigned MDDbgKind = TheMetadata.getMDKind("dbg");
Devang Patelbeab41b2009-10-07 22:04:08 +00001075#endif
Devang Pateld2f79a12009-07-28 19:55:13 +00001076 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1077 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
1078 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
1079 ++BI) {
1080 if (DbgStopPointInst *SPI = dyn_cast<DbgStopPointInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +00001081 processStopPoint(SPI);
Devang Pateld2f79a12009-07-28 19:55:13 +00001082 else if (DbgFuncStartInst *FSI = dyn_cast<DbgFuncStartInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +00001083 processFuncStart(FSI);
Devang Patele802f1c2009-07-30 17:30:23 +00001084 else if (DbgRegionStartInst *DRS = dyn_cast<DbgRegionStartInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +00001085 processRegionStart(DRS);
Devang Patele802f1c2009-07-30 17:30:23 +00001086 else if (DbgRegionEndInst *DRE = dyn_cast<DbgRegionEndInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +00001087 processRegionEnd(DRE);
Devang Patelb4d31302009-07-31 18:18:52 +00001088 else if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1089 processDeclare(DDI);
Devang Patelbeab41b2009-10-07 22:04:08 +00001090#ifdef ATTACH_DEBUG_INFO_TO_AN_INSN
Devang Patel6daf99b2009-11-10 22:05:35 +00001091 else if (MDDbgKind)
1092 if (MDNode *L = TheMetadata.getMD(MDDbgKind, BI))
1093 processLocation(DILocation(L));
Devang Patelbeab41b2009-10-07 22:04:08 +00001094#endif
Devang Pateld2f79a12009-07-28 19:55:13 +00001095 }
Devang Patele4b27562009-08-28 23:24:31 +00001096
1097 NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv");
1098 if (!NMD)
1099 return;
1100
1101 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
1102 DIGlobalVariable DIG(cast<MDNode>(NMD->getElement(i)));
Devang Pateld2f79a12009-07-28 19:55:13 +00001103 if (addGlobalVariable(DIG)) {
1104 addCompileUnit(DIG.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +00001105 processType(DIG.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +00001106 }
1107 }
1108}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001109
Devang Patel6daf99b2009-11-10 22:05:35 +00001110/// processLocation - Process DILocation.
1111void DebugInfoFinder::processLocation(DILocation Loc) {
1112 if (Loc.isNull()) return;
1113 DIScope S(Loc.getScope().getNode());
1114 if (S.isNull()) return;
1115 if (S.isCompileUnit())
1116 addCompileUnit(DICompileUnit(S.getNode()));
1117 else if (S.isSubprogram())
1118 processSubprogram(DISubprogram(S.getNode()));
1119 else if (S.isLexicalBlock())
1120 processLexicalBlock(DILexicalBlock(S.getNode()));
1121 processLocation(Loc.getOrigLocation());
1122}
1123
Devang Patel98c65172009-07-30 18:25:15 +00001124/// processType - Process DIType.
1125void DebugInfoFinder::processType(DIType DT) {
Devang Patel72bcdb62009-08-10 22:09:58 +00001126 if (!addType(DT))
Devang Pateld2f79a12009-07-28 19:55:13 +00001127 return;
1128
1129 addCompileUnit(DT.getCompileUnit());
Devang Patel6ceea332009-08-31 18:49:10 +00001130 if (DT.isCompositeType()) {
Devang Patele4b27562009-08-28 23:24:31 +00001131 DICompositeType DCT(DT.getNode());
Devang Patel98c65172009-07-30 18:25:15 +00001132 processType(DCT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001133 DIArray DA = DCT.getTypeArray();
1134 if (!DA.isNull())
1135 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1136 DIDescriptor D = DA.getElement(i);
Devang Patele4b27562009-08-28 23:24:31 +00001137 DIType TypeE = DIType(D.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001138 if (!TypeE.isNull())
Devang Patel98c65172009-07-30 18:25:15 +00001139 processType(TypeE);
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001140 else
Devang Patele4b27562009-08-28 23:24:31 +00001141 processSubprogram(DISubprogram(D.getNode()));
Devang Pateld2f79a12009-07-28 19:55:13 +00001142 }
Devang Patel6ceea332009-08-31 18:49:10 +00001143 } else if (DT.isDerivedType()) {
Devang Patele4b27562009-08-28 23:24:31 +00001144 DIDerivedType DDT(DT.getNode());
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001145 if (!DDT.isNull())
Devang Patel98c65172009-07-30 18:25:15 +00001146 processType(DDT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001147 }
1148}
1149
Devang Patelbeab41b2009-10-07 22:04:08 +00001150/// processLexicalBlock
1151void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
1152 if (LB.isNull())
1153 return;
1154 DIScope Context = LB.getContext();
1155 if (Context.isLexicalBlock())
1156 return processLexicalBlock(DILexicalBlock(Context.getNode()));
1157 else
1158 return processSubprogram(DISubprogram(Context.getNode()));
1159}
1160
Devang Patel98c65172009-07-30 18:25:15 +00001161/// processSubprogram - Process DISubprogram.
1162void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Patele802f1c2009-07-30 17:30:23 +00001163 if (SP.isNull())
1164 return;
Devang Pateld2f79a12009-07-28 19:55:13 +00001165 if (!addSubprogram(SP))
1166 return;
1167 addCompileUnit(SP.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +00001168 processType(SP.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +00001169}
1170
Devang Patel98c65172009-07-30 18:25:15 +00001171/// processStopPoint - Process DbgStopPointInst.
1172void DebugInfoFinder::processStopPoint(DbgStopPointInst *SPI) {
Devang Patele4b27562009-08-28 23:24:31 +00001173 MDNode *Context = dyn_cast<MDNode>(SPI->getContext());
Devang Pateld2f79a12009-07-28 19:55:13 +00001174 addCompileUnit(DICompileUnit(Context));
1175}
1176
Devang Patel98c65172009-07-30 18:25:15 +00001177/// processFuncStart - Process DbgFuncStartInst.
1178void DebugInfoFinder::processFuncStart(DbgFuncStartInst *FSI) {
Devang Patele4b27562009-08-28 23:24:31 +00001179 MDNode *SP = dyn_cast<MDNode>(FSI->getSubprogram());
Devang Patel98c65172009-07-30 18:25:15 +00001180 processSubprogram(DISubprogram(SP));
Devang Pateld2f79a12009-07-28 19:55:13 +00001181}
1182
Devang Patel98c65172009-07-30 18:25:15 +00001183/// processRegionStart - Process DbgRegionStart.
1184void DebugInfoFinder::processRegionStart(DbgRegionStartInst *DRS) {
Devang Patele4b27562009-08-28 23:24:31 +00001185 MDNode *SP = dyn_cast<MDNode>(DRS->getContext());
Devang Patel98c65172009-07-30 18:25:15 +00001186 processSubprogram(DISubprogram(SP));
Devang Patele802f1c2009-07-30 17:30:23 +00001187}
1188
Devang Patel98c65172009-07-30 18:25:15 +00001189/// processRegionEnd - Process DbgRegionEnd.
1190void DebugInfoFinder::processRegionEnd(DbgRegionEndInst *DRE) {
Devang Patele4b27562009-08-28 23:24:31 +00001191 MDNode *SP = dyn_cast<MDNode>(DRE->getContext());
Devang Patel98c65172009-07-30 18:25:15 +00001192 processSubprogram(DISubprogram(SP));
Devang Patele802f1c2009-07-30 17:30:23 +00001193}
1194
Devang Patelb4d31302009-07-31 18:18:52 +00001195/// processDeclare - Process DbgDeclareInst.
1196void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
Devang Patele4b27562009-08-28 23:24:31 +00001197 DIVariable DV(cast<MDNode>(DDI->getVariable()));
Devang Patelb4d31302009-07-31 18:18:52 +00001198 if (DV.isNull())
1199 return;
1200
Devang Patele4b27562009-08-28 23:24:31 +00001201 if (!NodesSeen.insert(DV.getNode()))
Devang Patelb4d31302009-07-31 18:18:52 +00001202 return;
1203
1204 addCompileUnit(DV.getCompileUnit());
1205 processType(DV.getType());
1206}
1207
Devang Patel72bcdb62009-08-10 22:09:58 +00001208/// addType - Add type into Tys.
1209bool DebugInfoFinder::addType(DIType DT) {
1210 if (DT.isNull())
1211 return false;
1212
Devang Patele4b27562009-08-28 23:24:31 +00001213 if (!NodesSeen.insert(DT.getNode()))
Devang Patel72bcdb62009-08-10 22:09:58 +00001214 return false;
1215
Devang Patele4b27562009-08-28 23:24:31 +00001216 TYs.push_back(DT.getNode());
Devang Patel72bcdb62009-08-10 22:09:58 +00001217 return true;
1218}
1219
Devang Pateld2f79a12009-07-28 19:55:13 +00001220/// addCompileUnit - Add compile unit into CUs.
Devang Patel98c65172009-07-30 18:25:15 +00001221bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001222 if (CU.isNull())
1223 return false;
1224
Devang Patele4b27562009-08-28 23:24:31 +00001225 if (!NodesSeen.insert(CU.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001226 return false;
1227
Devang Patele4b27562009-08-28 23:24:31 +00001228 CUs.push_back(CU.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001229 return true;
1230}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001231
Devang Pateld2f79a12009-07-28 19:55:13 +00001232/// addGlobalVariable - Add global variable into GVs.
Devang Patel98c65172009-07-30 18:25:15 +00001233bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001234 if (DIG.isNull())
1235 return false;
1236
Devang Patele4b27562009-08-28 23:24:31 +00001237 if (!NodesSeen.insert(DIG.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001238 return false;
1239
Devang Patele4b27562009-08-28 23:24:31 +00001240 GVs.push_back(DIG.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001241 return true;
1242}
1243
1244// addSubprogram - Add subprgoram into SPs.
Devang Patel98c65172009-07-30 18:25:15 +00001245bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001246 if (SP.isNull())
1247 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001248
Devang Patele4b27562009-08-28 23:24:31 +00001249 if (!NodesSeen.insert(SP.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001250 return false;
1251
Devang Patele4b27562009-08-28 23:24:31 +00001252 SPs.push_back(SP.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001253 return true;
1254}
1255
Torok Edwin620f2802008-12-16 09:07:36 +00001256namespace llvm {
Bill Wendlingdc817b62009-05-14 18:26:15 +00001257 /// findStopPoint - Find the stoppoint coressponding to this instruction, that
1258 /// is the stoppoint that dominates this instruction.
1259 const DbgStopPointInst *findStopPoint(const Instruction *Inst) {
Torok Edwin620f2802008-12-16 09:07:36 +00001260 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(Inst))
1261 return DSI;
1262
1263 const BasicBlock *BB = Inst->getParent();
1264 BasicBlock::const_iterator I = Inst, B;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001265 while (BB) {
Torok Edwin620f2802008-12-16 09:07:36 +00001266 B = BB->begin();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001267
Torok Edwin620f2802008-12-16 09:07:36 +00001268 // A BB consisting only of a terminator can't have a stoppoint.
Bill Wendlingdc817b62009-05-14 18:26:15 +00001269 while (I != B) {
1270 --I;
1271 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
1272 return DSI;
Torok Edwin620f2802008-12-16 09:07:36 +00001273 }
Bill Wendlingdc817b62009-05-14 18:26:15 +00001274
1275 // This BB didn't have a stoppoint: if there is only one predecessor, look
1276 // for a stoppoint there. We could use getIDom(), but that would require
1277 // dominator info.
Torok Edwin620f2802008-12-16 09:07:36 +00001278 BB = I->getParent()->getUniquePredecessor();
1279 if (BB)
1280 I = BB->getTerminator();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001281 }
1282
Torok Edwin620f2802008-12-16 09:07:36 +00001283 return 0;
1284 }
1285
Bill Wendlingdc817b62009-05-14 18:26:15 +00001286 /// findBBStopPoint - Find the stoppoint corresponding to first real
1287 /// (non-debug intrinsic) instruction in this Basic Block, and return the
1288 /// stoppoint for it.
1289 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB) {
1290 for(BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001291 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
1292 return DSI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001293
1294 // Fallback to looking for stoppoint of unique predecessor. Useful if this
1295 // BB contains no stoppoints, but unique predecessor does.
Torok Edwin620f2802008-12-16 09:07:36 +00001296 BB = BB->getUniquePredecessor();
1297 if (BB)
1298 return findStopPoint(BB->getTerminator());
Bill Wendlingdc817b62009-05-14 18:26:15 +00001299
Torok Edwin620f2802008-12-16 09:07:36 +00001300 return 0;
1301 }
1302
Bill Wendlingdc817b62009-05-14 18:26:15 +00001303 Value *findDbgGlobalDeclare(GlobalVariable *V) {
Torok Edwinff7d0e92009-03-10 13:41:26 +00001304 const Module *M = V->getParent();
Devang Patele4b27562009-08-28 23:24:31 +00001305 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1306 if (!NMD)
1307 return 0;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001308
Devang Patele4b27562009-08-28 23:24:31 +00001309 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
1310 DIGlobalVariable DIG(cast_or_null<MDNode>(NMD->getElement(i)));
1311 if (DIG.isNull())
1312 continue;
1313 if (DIG.getGlobal() == V)
1314 return DIG.getNode();
Torok Edwinff7d0e92009-03-10 13:41:26 +00001315 }
Torok Edwinff7d0e92009-03-10 13:41:26 +00001316 return 0;
1317 }
1318
Bill Wendlingdc817b62009-05-14 18:26:15 +00001319 /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
Torok Edwin620f2802008-12-16 09:07:36 +00001320 /// It looks through pointer casts too.
Bill Wendlingdc817b62009-05-14 18:26:15 +00001321 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts) {
Torok Edwin620f2802008-12-16 09:07:36 +00001322 if (stripCasts) {
1323 V = V->stripPointerCasts();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001324
Torok Edwin620f2802008-12-16 09:07:36 +00001325 // Look for the bitcast.
1326 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001327 I != E; ++I)
Devang Patel94dfaec2009-10-29 18:20:34 +00001328 if (isa<BitCastInst>(I)) {
1329 const DbgDeclareInst *DDI = findDbgDeclare(*I, false);
1330 if (DDI) return DDI;
1331 }
Torok Edwin620f2802008-12-16 09:07:36 +00001332 return 0;
1333 }
1334
Bill Wendlingdc817b62009-05-14 18:26:15 +00001335 // Find llvm.dbg.declare among uses of the instruction.
Torok Edwin620f2802008-12-16 09:07:36 +00001336 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001337 I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001338 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I))
1339 return DDI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001340
Torok Edwin620f2802008-12-16 09:07:36 +00001341 return 0;
1342 }
Torok Edwinff7d0e92009-03-10 13:41:26 +00001343
Devang Patel5ccdd102009-09-29 18:40:58 +00001344bool getLocationInfo(const Value *V, std::string &DisplayName,
1345 std::string &Type, unsigned &LineNo, std::string &File,
Bill Wendlingdc817b62009-05-14 18:26:15 +00001346 std::string &Dir) {
Torok Edwinff7d0e92009-03-10 13:41:26 +00001347 DICompileUnit Unit;
1348 DIType TypeD;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001349
Torok Edwinff7d0e92009-03-10 13:41:26 +00001350 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1351 Value *DIGV = findDbgGlobalDeclare(GV);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001352 if (!DIGV) return false;
Devang Patele4b27562009-08-28 23:24:31 +00001353 DIGlobalVariable Var(cast<MDNode>(DIGV));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001354
Devang Patel5ccdd102009-09-29 18:40:58 +00001355 if (const char *D = Var.getDisplayName())
1356 DisplayName = D;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001357 LineNo = Var.getLineNumber();
1358 Unit = Var.getCompileUnit();
1359 TypeD = Var.getType();
1360 } else {
1361 const DbgDeclareInst *DDI = findDbgDeclare(V);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001362 if (!DDI) return false;
Devang Patele4b27562009-08-28 23:24:31 +00001363 DIVariable Var(cast<MDNode>(DDI->getVariable()));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001364
Devang Patel5ccdd102009-09-29 18:40:58 +00001365 if (const char *D = Var.getName())
1366 DisplayName = D;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001367 LineNo = Var.getLineNumber();
1368 Unit = Var.getCompileUnit();
1369 TypeD = Var.getType();
1370 }
Bill Wendlingdc817b62009-05-14 18:26:15 +00001371
Devang Patel5ccdd102009-09-29 18:40:58 +00001372 if (const char *T = TypeD.getName())
1373 Type = T;
1374 if (const char *F = Unit.getFilename())
1375 File = F;
1376 if (const char *D = Unit.getDirectory())
1377 Dir = D;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001378 return true;
1379 }
Devang Patel13e16b62009-06-26 01:49:18 +00001380
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001381 /// isValidDebugInfoIntrinsic - Return true if SPI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001382 /// info intrinsic.
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001383 bool isValidDebugInfoIntrinsic(DbgStopPointInst &SPI,
Devang Patel9e529c32009-07-02 01:15:24 +00001384 CodeGenOpt::Level OptLev) {
1385 return DIDescriptor::ValidDebugInfo(SPI.getContext(), OptLev);
1386 }
1387
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001388 /// isValidDebugInfoIntrinsic - Return true if FSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001389 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001390 bool isValidDebugInfoIntrinsic(DbgFuncStartInst &FSI,
1391 CodeGenOpt::Level OptLev) {
1392 return DIDescriptor::ValidDebugInfo(FSI.getSubprogram(), OptLev);
1393 }
1394
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001395 /// isValidDebugInfoIntrinsic - Return true if RSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001396 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001397 bool isValidDebugInfoIntrinsic(DbgRegionStartInst &RSI,
1398 CodeGenOpt::Level OptLev) {
1399 return DIDescriptor::ValidDebugInfo(RSI.getContext(), OptLev);
1400 }
1401
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001402 /// isValidDebugInfoIntrinsic - Return true if REI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001403 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001404 bool isValidDebugInfoIntrinsic(DbgRegionEndInst &REI,
1405 CodeGenOpt::Level OptLev) {
1406 return DIDescriptor::ValidDebugInfo(REI.getContext(), OptLev);
1407 }
1408
1409
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001410 /// isValidDebugInfoIntrinsic - Return true if DI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001411 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001412 bool isValidDebugInfoIntrinsic(DbgDeclareInst &DI,
1413 CodeGenOpt::Level OptLev) {
1414 return DIDescriptor::ValidDebugInfo(DI.getVariable(), OptLev);
1415 }
1416
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001417 /// ExtractDebugLocation - Extract debug location information
Devang Patel9e529c32009-07-02 01:15:24 +00001418 /// from llvm.dbg.stoppoint intrinsic.
1419 DebugLoc ExtractDebugLocation(DbgStopPointInst &SPI,
Devang Patel9e529c32009-07-02 01:15:24 +00001420 DebugLocTracker &DebugLocInfo) {
1421 DebugLoc DL;
1422 Value *Context = SPI.getContext();
Devang Patel9e529c32009-07-02 01:15:24 +00001423
1424 // If this location is already tracked then use it.
Devang Patela1434042009-10-01 01:15:28 +00001425 DebugLocTuple Tuple(cast<MDNode>(Context), NULL, SPI.getLine(),
Devang Patel9e529c32009-07-02 01:15:24 +00001426 SPI.getColumn());
1427 DenseMap<DebugLocTuple, unsigned>::iterator II
1428 = DebugLocInfo.DebugIdMap.find(Tuple);
1429 if (II != DebugLocInfo.DebugIdMap.end())
1430 return DebugLoc::get(II->second);
1431
1432 // Add a new location entry.
1433 unsigned Id = DebugLocInfo.DebugLocations.size();
1434 DebugLocInfo.DebugLocations.push_back(Tuple);
1435 DebugLocInfo.DebugIdMap[Tuple] = Id;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001436
Devang Patel9e529c32009-07-02 01:15:24 +00001437 return DebugLoc::get(Id);
1438 }
1439
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001440 /// ExtractDebugLocation - Extract debug location information
Devang Patel1b75f442009-09-16 18:20:05 +00001441 /// from DILocation.
1442 DebugLoc ExtractDebugLocation(DILocation &Loc,
1443 DebugLocTracker &DebugLocInfo) {
1444 DebugLoc DL;
1445 MDNode *Context = Loc.getScope().getNode();
Devang Patela1434042009-10-01 01:15:28 +00001446 MDNode *InlinedLoc = NULL;
1447 if (!Loc.getOrigLocation().isNull())
1448 InlinedLoc = Loc.getOrigLocation().getNode();
Devang Patel1b75f442009-09-16 18:20:05 +00001449 // If this location is already tracked then use it.
Devang Patela1434042009-10-01 01:15:28 +00001450 DebugLocTuple Tuple(Context, InlinedLoc, Loc.getLineNumber(),
Daniel Dunbara279bc32009-09-20 02:20:51 +00001451 Loc.getColumnNumber());
Devang Patel1b75f442009-09-16 18:20:05 +00001452 DenseMap<DebugLocTuple, unsigned>::iterator II
1453 = DebugLocInfo.DebugIdMap.find(Tuple);
1454 if (II != DebugLocInfo.DebugIdMap.end())
1455 return DebugLoc::get(II->second);
1456
1457 // Add a new location entry.
1458 unsigned Id = DebugLocInfo.DebugLocations.size();
1459 DebugLocInfo.DebugLocations.push_back(Tuple);
1460 DebugLocInfo.DebugIdMap[Tuple] = Id;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001461
Devang Patel1b75f442009-09-16 18:20:05 +00001462 return DebugLoc::get(Id);
1463 }
1464
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001465 /// ExtractDebugLocation - Extract debug location information
Devang Patel9e529c32009-07-02 01:15:24 +00001466 /// from llvm.dbg.func_start intrinsic.
1467 DebugLoc ExtractDebugLocation(DbgFuncStartInst &FSI,
Devang Patel9e529c32009-07-02 01:15:24 +00001468 DebugLocTracker &DebugLocInfo) {
1469 DebugLoc DL;
1470 Value *SP = FSI.getSubprogram();
Devang Patel9e529c32009-07-02 01:15:24 +00001471
Devang Patele4b27562009-08-28 23:24:31 +00001472 DISubprogram Subprogram(cast<MDNode>(SP));
Devang Patel9e529c32009-07-02 01:15:24 +00001473 unsigned Line = Subprogram.getLineNumber();
1474 DICompileUnit CU(Subprogram.getCompileUnit());
1475
1476 // If this location is already tracked then use it.
Devang Patela1434042009-10-01 01:15:28 +00001477 DebugLocTuple Tuple(CU.getNode(), NULL, Line, /* Column */ 0);
Devang Patel9e529c32009-07-02 01:15:24 +00001478 DenseMap<DebugLocTuple, unsigned>::iterator II
1479 = DebugLocInfo.DebugIdMap.find(Tuple);
1480 if (II != DebugLocInfo.DebugIdMap.end())
1481 return DebugLoc::get(II->second);
1482
1483 // Add a new location entry.
1484 unsigned Id = DebugLocInfo.DebugLocations.size();
1485 DebugLocInfo.DebugLocations.push_back(Tuple);
1486 DebugLocInfo.DebugIdMap[Tuple] = Id;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001487
Devang Patel9e529c32009-07-02 01:15:24 +00001488 return DebugLoc::get(Id);
1489 }
1490
1491 /// isInlinedFnStart - Return true if FSI is starting an inlined function.
1492 bool isInlinedFnStart(DbgFuncStartInst &FSI, const Function *CurrentFn) {
Devang Patele4b27562009-08-28 23:24:31 +00001493 DISubprogram Subprogram(cast<MDNode>(FSI.getSubprogram()));
Devang Patel9e529c32009-07-02 01:15:24 +00001494 if (Subprogram.describes(CurrentFn))
1495 return false;
1496
1497 return true;
1498 }
1499
1500 /// isInlinedFnEnd - Return true if REI is ending an inlined function.
1501 bool isInlinedFnEnd(DbgRegionEndInst &REI, const Function *CurrentFn) {
Devang Patele4b27562009-08-28 23:24:31 +00001502 DISubprogram Subprogram(cast<MDNode>(REI.getContext()));
Devang Patel9e529c32009-07-02 01:15:24 +00001503 if (Subprogram.isNull() || Subprogram.describes(CurrentFn))
1504 return false;
1505
1506 return true;
1507 }
Torok Edwin620f2802008-12-16 09:07:36 +00001508}