blob: 7c1cc35ee9dc85eacda330f166f02cf424dfcf29 [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();
412 if (BaseType.isDerivedType())
413 return DIDerivedType(BaseType.getNode()).getOriginalTypeSize();
414 else
415 return BaseType.getSizeInBits();
416 }
417
418 return getSizeInBits();
Devang Patel36375ee2009-02-17 21:23:59 +0000419}
Devang Patelb79b5352009-01-19 23:21:49 +0000420
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000421/// describes - Return true if this subprogram provides debugging
422/// information for the function F.
423bool DISubprogram::describes(const Function *F) {
424 assert (F && "Invalid function");
Devang Patel5ccdd102009-09-29 18:40:58 +0000425 const char *Name = getLinkageName();
426 if (!Name)
427 Name = getName();
428 if (strcmp(F->getName().data(), Name) == 0)
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000429 return true;
430 return false;
431}
432
Devang Patelecbeb1a2009-09-30 22:34:41 +0000433const char *DIScope::getFilename() const {
434 if (isLexicalBlock())
435 return DILexicalBlock(DbgNode).getFilename();
436 else if (isSubprogram())
437 return DISubprogram(DbgNode).getFilename();
438 else if (isCompileUnit())
439 return DICompileUnit(DbgNode).getFilename();
440 else
441 assert (0 && "Invalid DIScope!");
442 return NULL;
443}
444
445const char *DIScope::getDirectory() const {
446 if (isLexicalBlock())
447 return DILexicalBlock(DbgNode).getDirectory();
448 else if (isSubprogram())
449 return DISubprogram(DbgNode).getDirectory();
450 else if (isCompileUnit())
451 return DICompileUnit(DbgNode).getDirectory();
452 else
453 assert (0 && "Invalid DIScope!");
454 return NULL;
455}
456
Chris Lattnera45664f2008-11-10 02:56:27 +0000457//===----------------------------------------------------------------------===//
Devang Patel7136a652009-07-01 22:10:23 +0000458// DIDescriptor: dump routines for all descriptors.
459//===----------------------------------------------------------------------===//
460
461
462/// dump - Print descriptor.
463void DIDescriptor::dump() const {
Devang Patele4b27562009-08-28 23:24:31 +0000464 errs() << "[" << dwarf::TagString(getTag()) << "] ";
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000465 errs().write_hex((intptr_t) &*DbgNode) << ']';
Devang Patel7136a652009-07-01 22:10:23 +0000466}
467
468/// dump - Print compile unit.
469void DICompileUnit::dump() const {
470 if (getLanguage())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000471 errs() << " [" << dwarf::LanguageString(getLanguage()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000472
Devang Patel5ccdd102009-09-29 18:40:58 +0000473 errs() << " [" << getDirectory() << "/" << getFilename() << " ]";
Devang Patel7136a652009-07-01 22:10:23 +0000474}
475
476/// dump - Print type.
477void DIType::dump() const {
478 if (isNull()) return;
479
Devang Patel5ccdd102009-09-29 18:40:58 +0000480 if (const char *Res = getName())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000481 errs() << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000482
483 unsigned Tag = getTag();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000484 errs() << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000485
486 // TODO : Print context
487 getCompileUnit().dump();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000488 errs() << " ["
489 << getLineNumber() << ", "
Chris Lattnera81d29b2009-08-23 07:33:14 +0000490 << getSizeInBits() << ", "
491 << getAlignInBits() << ", "
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000492 << getOffsetInBits()
Chris Lattnera81d29b2009-08-23 07:33:14 +0000493 << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000494
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000495 if (isPrivate())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000496 errs() << " [private] ";
Devang Patel7136a652009-07-01 22:10:23 +0000497 else if (isProtected())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000498 errs() << " [protected] ";
Devang Patel7136a652009-07-01 22:10:23 +0000499
500 if (isForwardDecl())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000501 errs() << " [fwd] ";
Devang Patel7136a652009-07-01 22:10:23 +0000502
Devang Patel6ceea332009-08-31 18:49:10 +0000503 if (isBasicType())
Devang Patele4b27562009-08-28 23:24:31 +0000504 DIBasicType(DbgNode).dump();
Devang Patel6ceea332009-08-31 18:49:10 +0000505 else if (isDerivedType())
Devang Patele4b27562009-08-28 23:24:31 +0000506 DIDerivedType(DbgNode).dump();
Devang Patel6ceea332009-08-31 18:49:10 +0000507 else if (isCompositeType())
Devang Patele4b27562009-08-28 23:24:31 +0000508 DICompositeType(DbgNode).dump();
Devang Patel7136a652009-07-01 22:10:23 +0000509 else {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000510 errs() << "Invalid DIType\n";
Devang Patel7136a652009-07-01 22:10:23 +0000511 return;
512 }
513
Chris Lattnera81d29b2009-08-23 07:33:14 +0000514 errs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000515}
516
517/// dump - Print basic type.
518void DIBasicType::dump() const {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000519 errs() << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000520}
521
522/// dump - Print derived type.
523void DIDerivedType::dump() const {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000524 errs() << "\n\t Derived From: "; getTypeDerivedFrom().dump();
Devang Patel7136a652009-07-01 22:10:23 +0000525}
526
527/// dump - Print composite type.
528void DICompositeType::dump() const {
529 DIArray A = getTypeArray();
530 if (A.isNull())
531 return;
Chris Lattnera81d29b2009-08-23 07:33:14 +0000532 errs() << " [" << A.getNumElements() << " elements]";
Devang Patel7136a652009-07-01 22:10:23 +0000533}
534
535/// dump - Print global.
536void DIGlobal::dump() const {
Devang Patel5ccdd102009-09-29 18:40:58 +0000537 if (const char *Res = getName())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000538 errs() << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000539
540 unsigned Tag = getTag();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000541 errs() << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000542
543 // TODO : Print context
544 getCompileUnit().dump();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000545 errs() << " [" << getLineNumber() << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000546
547 if (isLocalToUnit())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000548 errs() << " [local] ";
Devang Patel7136a652009-07-01 22:10:23 +0000549
550 if (isDefinition())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000551 errs() << " [def] ";
Devang Patel7136a652009-07-01 22:10:23 +0000552
Devang Patel6ceea332009-08-31 18:49:10 +0000553 if (isGlobalVariable())
Devang Patele4b27562009-08-28 23:24:31 +0000554 DIGlobalVariable(DbgNode).dump();
Devang Patel7136a652009-07-01 22:10:23 +0000555
Chris Lattnera81d29b2009-08-23 07:33:14 +0000556 errs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000557}
558
559/// dump - Print subprogram.
560void DISubprogram::dump() const {
Devang Patel5ccdd102009-09-29 18:40:58 +0000561 if (const char *Res = getName())
Devang Patel82dfc0c2009-08-31 22:47:13 +0000562 errs() << " [" << Res << "] ";
563
564 unsigned Tag = getTag();
565 errs() << " [" << dwarf::TagString(Tag) << "] ";
566
567 // TODO : Print context
568 getCompileUnit().dump();
569 errs() << " [" << getLineNumber() << "] ";
570
571 if (isLocalToUnit())
572 errs() << " [local] ";
573
574 if (isDefinition())
575 errs() << " [def] ";
576
577 errs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000578}
579
580/// dump - Print global variable.
581void DIGlobalVariable::dump() const {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000582 errs() << " [";
583 getGlobal()->dump();
584 errs() << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000585}
586
587/// dump - Print variable.
588void DIVariable::dump() const {
Devang Patel5ccdd102009-09-29 18:40:58 +0000589 if (const char *Res = getName())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000590 errs() << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000591
592 getCompileUnit().dump();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000593 errs() << " [" << getLineNumber() << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000594 getType().dump();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000595 errs() << "\n";
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000596
597 // FIXME: Dump complex addresses
Devang Patel7136a652009-07-01 22:10:23 +0000598}
599
600//===----------------------------------------------------------------------===//
Chris Lattnera45664f2008-11-10 02:56:27 +0000601// DIFactory: Basic Helpers
602//===----------------------------------------------------------------------===//
603
Bill Wendlingdc817b62009-05-14 18:26:15 +0000604DIFactory::DIFactory(Module &m)
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000605 : M(m), VMContext(M.getContext()), StopPointFn(0), FuncStartFn(0),
Owen Anderson99035272009-07-07 17:12:53 +0000606 RegionStartFn(0), RegionEndFn(0),
Bill Wendlingdc817b62009-05-14 18:26:15 +0000607 DeclareFn(0) {
Owen Andersond7f2a6c2009-08-05 23:16:16 +0000608 EmptyStructPtr = PointerType::getUnqual(StructType::get(VMContext));
Chris Lattner497a7a82008-11-10 04:10:34 +0000609}
610
Chris Lattnera45664f2008-11-10 02:56:27 +0000611Constant *DIFactory::GetTagConstant(unsigned TAG) {
Devang Patel6906ba52009-01-20 19:22:03 +0000612 assert((TAG & LLVMDebugVersionMask) == 0 &&
Chris Lattnera45664f2008-11-10 02:56:27 +0000613 "Tag too large for debug encoding!");
Owen Anderson1d0be152009-08-13 21:58:54 +0000614 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
Chris Lattnera45664f2008-11-10 02:56:27 +0000615}
616
Chris Lattnera45664f2008-11-10 02:56:27 +0000617//===----------------------------------------------------------------------===//
618// DIFactory: Primary Constructors
619//===----------------------------------------------------------------------===//
620
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000621/// GetOrCreateArray - Create an descriptor for an array of descriptors.
Chris Lattnera45664f2008-11-10 02:56:27 +0000622/// This implicitly uniques the arrays created.
623DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
Devang Patele4b27562009-08-28 23:24:31 +0000624 SmallVector<Value*, 16> Elts;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000625
Devang Patele4b27562009-08-28 23:24:31 +0000626 if (NumTys == 0)
627 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)));
628 else
629 for (unsigned i = 0; i != NumTys; ++i)
630 Elts.push_back(Tys[i].getNode());
Devang Patel82459882009-08-26 05:01:18 +0000631
Devang Patele4b27562009-08-28 23:24:31 +0000632 return DIArray(MDNode::get(VMContext,Elts.data(), Elts.size()));
Chris Lattnera45664f2008-11-10 02:56:27 +0000633}
634
635/// GetOrCreateSubrange - Create a descriptor for a value range. This
636/// implicitly uniques the values returned.
637DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
Devang Patele4b27562009-08-28 23:24:31 +0000638 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000639 GetTagConstant(dwarf::DW_TAG_subrange_type),
Owen Anderson1d0be152009-08-13 21:58:54 +0000640 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
641 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
Chris Lattnera45664f2008-11-10 02:56:27 +0000642 };
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000643
Devang Patele4b27562009-08-28 23:24:31 +0000644 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000645}
646
647
648
649/// CreateCompileUnit - Create a new descriptor for the specified compile
650/// unit. Note that this does not unique compile units within the module.
651DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
Devang Patel5ccdd102009-09-29 18:40:58 +0000652 StringRef Filename,
653 StringRef Directory,
654 StringRef Producer,
Devang Pateldd9db662009-01-30 18:20:31 +0000655 bool isMain,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000656 bool isOptimized,
Devang Patel13319ce2009-02-17 22:43:44 +0000657 const char *Flags,
658 unsigned RunTimeVer) {
Devang Patele4b27562009-08-28 23:24:31 +0000659 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000660 GetTagConstant(dwarf::DW_TAG_compile_unit),
Devang Patele4b27562009-08-28 23:24:31 +0000661 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Owen Anderson1d0be152009-08-13 21:58:54 +0000662 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
Devang Patele4b27562009-08-28 23:24:31 +0000663 MDString::get(VMContext, Filename),
664 MDString::get(VMContext, Directory),
665 MDString::get(VMContext, Producer),
Owen Anderson1d0be152009-08-13 21:58:54 +0000666 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
667 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
Devang Patele4b27562009-08-28 23:24:31 +0000668 MDString::get(VMContext, Flags),
Owen Anderson1d0be152009-08-13 21:58:54 +0000669 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
Chris Lattnera45664f2008-11-10 02:56:27 +0000670 };
Devang Patele4b27562009-08-28 23:24:31 +0000671
672 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000673}
674
675/// CreateEnumerator - Create a single enumerator value.
Devang Patel5ccdd102009-09-29 18:40:58 +0000676DIEnumerator DIFactory::CreateEnumerator(StringRef Name, uint64_t Val){
Devang Patele4b27562009-08-28 23:24:31 +0000677 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000678 GetTagConstant(dwarf::DW_TAG_enumerator),
Devang Patele4b27562009-08-28 23:24:31 +0000679 MDString::get(VMContext, Name),
Owen Anderson1d0be152009-08-13 21:58:54 +0000680 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
Chris Lattnera45664f2008-11-10 02:56:27 +0000681 };
Devang Patele4b27562009-08-28 23:24:31 +0000682 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000683}
684
685
686/// CreateBasicType - Create a basic type like int, float, etc.
687DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
Devang Patel5ccdd102009-09-29 18:40:58 +0000688 StringRef Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000689 DICompileUnit CompileUnit,
690 unsigned LineNumber,
691 uint64_t SizeInBits,
692 uint64_t AlignInBits,
693 uint64_t OffsetInBits, unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000694 unsigned Encoding) {
Devang Patele4b27562009-08-28 23:24:31 +0000695 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000696 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patele4b27562009-08-28 23:24:31 +0000697 Context.getNode(),
698 MDString::get(VMContext, Name),
699 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000700 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
701 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
702 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
703 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
704 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
705 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
Chris Lattnera45664f2008-11-10 02:56:27 +0000706 };
Devang Patele4b27562009-08-28 23:24:31 +0000707 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000708}
709
Devang Patelac16d442009-10-26 16:54:35 +0000710
711/// CreateBasicType - Create a basic type like int, float, etc.
712DIBasicType DIFactory::CreateBasicTypeEx(DIDescriptor Context,
713 StringRef Name,
714 DICompileUnit CompileUnit,
715 unsigned LineNumber,
716 Constant *SizeInBits,
717 Constant *AlignInBits,
718 Constant *OffsetInBits, unsigned Flags,
719 unsigned Encoding) {
720 Value *Elts[] = {
721 GetTagConstant(dwarf::DW_TAG_base_type),
722 Context.getNode(),
723 MDString::get(VMContext, Name),
724 CompileUnit.getNode(),
725 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
726 SizeInBits,
727 AlignInBits,
728 OffsetInBits,
729 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
730 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
731 };
732 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
733}
734
735
Chris Lattnera45664f2008-11-10 02:56:27 +0000736/// CreateDerivedType - Create a derived type like const qualified type,
737/// pointer, typedef, etc.
738DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
739 DIDescriptor Context,
Devang Patel5ccdd102009-09-29 18:40:58 +0000740 StringRef Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000741 DICompileUnit CompileUnit,
742 unsigned LineNumber,
743 uint64_t SizeInBits,
744 uint64_t AlignInBits,
745 uint64_t OffsetInBits,
746 unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000747 DIType DerivedFrom) {
Devang Patele4b27562009-08-28 23:24:31 +0000748 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000749 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000750 Context.getNode(),
751 MDString::get(VMContext, Name),
752 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000753 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
754 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
755 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
756 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
757 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patele4b27562009-08-28 23:24:31 +0000758 DerivedFrom.getNode(),
Chris Lattnera45664f2008-11-10 02:56:27 +0000759 };
Devang Patele4b27562009-08-28 23:24:31 +0000760 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000761}
762
Devang Patelac16d442009-10-26 16:54:35 +0000763
764/// CreateDerivedType - Create a derived type like const qualified type,
765/// pointer, typedef, etc.
766DIDerivedType DIFactory::CreateDerivedTypeEx(unsigned Tag,
767 DIDescriptor Context,
768 StringRef Name,
769 DICompileUnit CompileUnit,
770 unsigned LineNumber,
771 Constant *SizeInBits,
772 Constant *AlignInBits,
773 Constant *OffsetInBits,
774 unsigned Flags,
775 DIType DerivedFrom) {
776 Value *Elts[] = {
777 GetTagConstant(Tag),
778 Context.getNode(),
779 MDString::get(VMContext, Name),
780 CompileUnit.getNode(),
781 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
782 SizeInBits,
783 AlignInBits,
784 OffsetInBits,
785 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
786 DerivedFrom.getNode(),
787 };
788 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
789}
790
791
Chris Lattnera45664f2008-11-10 02:56:27 +0000792/// CreateCompositeType - Create a composite type like array, struct, etc.
793DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
794 DIDescriptor Context,
Devang Patel5ccdd102009-09-29 18:40:58 +0000795 StringRef Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000796 DICompileUnit CompileUnit,
797 unsigned LineNumber,
798 uint64_t SizeInBits,
799 uint64_t AlignInBits,
800 uint64_t OffsetInBits,
801 unsigned Flags,
802 DIType DerivedFrom,
Devang Patel13319ce2009-02-17 22:43:44 +0000803 DIArray Elements,
804 unsigned RuntimeLang) {
Owen Andersone277fed2009-07-07 16:31:25 +0000805
Devang Patele4b27562009-08-28 23:24:31 +0000806 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000807 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000808 Context.getNode(),
809 MDString::get(VMContext, Name),
810 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000811 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
812 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
813 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
814 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
815 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patele4b27562009-08-28 23:24:31 +0000816 DerivedFrom.getNode(),
817 Elements.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000818 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
Chris Lattnera45664f2008-11-10 02:56:27 +0000819 };
Devang Patele4b27562009-08-28 23:24:31 +0000820 return DICompositeType(MDNode::get(VMContext, &Elts[0], 12));
Chris Lattnera45664f2008-11-10 02:56:27 +0000821}
822
823
Devang Patelac16d442009-10-26 16:54:35 +0000824/// CreateCompositeType - Create a composite type like array, struct, etc.
825DICompositeType DIFactory::CreateCompositeTypeEx(unsigned Tag,
826 DIDescriptor Context,
827 StringRef Name,
828 DICompileUnit CompileUnit,
829 unsigned LineNumber,
830 Constant *SizeInBits,
831 Constant *AlignInBits,
832 Constant *OffsetInBits,
833 unsigned Flags,
834 DIType DerivedFrom,
835 DIArray Elements,
836 unsigned RuntimeLang) {
837
838 Value *Elts[] = {
839 GetTagConstant(Tag),
840 Context.getNode(),
841 MDString::get(VMContext, Name),
842 CompileUnit.getNode(),
843 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
844 SizeInBits,
845 AlignInBits,
846 OffsetInBits,
847 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
848 DerivedFrom.getNode(),
849 Elements.getNode(),
850 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
851 };
852 return DICompositeType(MDNode::get(VMContext, &Elts[0], 12));
853}
854
855
Chris Lattnera45664f2008-11-10 02:56:27 +0000856/// CreateSubprogram - Create a new descriptor for the specified subprogram.
857/// See comments in DISubprogram for descriptions of these fields. This
858/// method does not unique the generated descriptors.
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000859DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
Devang Patel5ccdd102009-09-29 18:40:58 +0000860 StringRef Name,
861 StringRef DisplayName,
862 StringRef LinkageName,
Chris Lattnera45664f2008-11-10 02:56:27 +0000863 DICompileUnit CompileUnit,
864 unsigned LineNo, DIType Type,
865 bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000866 bool isDefinition) {
Devang Patel854967e2008-12-17 22:39:29 +0000867
Devang Patele4b27562009-08-28 23:24:31 +0000868 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000869 GetTagConstant(dwarf::DW_TAG_subprogram),
Devang Patele4b27562009-08-28 23:24:31 +0000870 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
871 Context.getNode(),
872 MDString::get(VMContext, Name),
873 MDString::get(VMContext, DisplayName),
874 MDString::get(VMContext, LinkageName),
875 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000876 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000877 Type.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000878 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
879 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition)
Chris Lattnera45664f2008-11-10 02:56:27 +0000880 };
Devang Patele4b27562009-08-28 23:24:31 +0000881 return DISubprogram(MDNode::get(VMContext, &Elts[0], 11));
Chris Lattnera45664f2008-11-10 02:56:27 +0000882}
883
884/// CreateGlobalVariable - Create a new descriptor for the specified global.
885DIGlobalVariable
Devang Patel5ccdd102009-09-29 18:40:58 +0000886DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
887 StringRef DisplayName,
888 StringRef LinkageName,
Chris Lattnera45664f2008-11-10 02:56:27 +0000889 DICompileUnit CompileUnit,
890 unsigned LineNo, DIType Type,bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000891 bool isDefinition, llvm::GlobalVariable *Val) {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000892 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000893 GetTagConstant(dwarf::DW_TAG_variable),
Devang Patele4b27562009-08-28 23:24:31 +0000894 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
895 Context.getNode(),
896 MDString::get(VMContext, Name),
897 MDString::get(VMContext, DisplayName),
898 MDString::get(VMContext, LinkageName),
899 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000900 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000901 Type.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000902 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
903 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
Devang Patele4b27562009-08-28 23:24:31 +0000904 Val
Chris Lattnera45664f2008-11-10 02:56:27 +0000905 };
Devang Patele4b27562009-08-28 23:24:31 +0000906
907 Value *const *Vs = &Elts[0];
908 MDNode *Node = MDNode::get(VMContext,Vs, 12);
909
910 // Create a named metadata so that we do not lose this mdnode.
911 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
912 NMD->addElement(Node);
913
914 return DIGlobalVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +0000915}
916
917
918/// CreateVariable - Create a new descriptor for the specified variable.
919DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
Devang Patel5ccdd102009-09-29 18:40:58 +0000920 StringRef Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000921 DICompileUnit CompileUnit, unsigned LineNo,
Devang Pateldd9db662009-01-30 18:20:31 +0000922 DIType Type) {
Devang Patele4b27562009-08-28 23:24:31 +0000923 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000924 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000925 Context.getNode(),
926 MDString::get(VMContext, Name),
927 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000928 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000929 Type.getNode(),
Chris Lattnera45664f2008-11-10 02:56:27 +0000930 };
Devang Patele4b27562009-08-28 23:24:31 +0000931 return DIVariable(MDNode::get(VMContext, &Elts[0], 6));
Chris Lattnera45664f2008-11-10 02:56:27 +0000932}
933
934
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000935/// CreateComplexVariable - Create a new descriptor for the specified variable
936/// which has a complex address expression for its address.
937DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
938 const std::string &Name,
939 DICompileUnit CompileUnit,
940 unsigned LineNo,
941 DIType Type, SmallVector<Value *, 9> &addr) {
942 SmallVector<Value *, 9> Elts;
943 Elts.push_back(GetTagConstant(Tag));
944 Elts.push_back(Context.getNode());
945 Elts.push_back(MDString::get(VMContext, Name));
946 Elts.push_back(CompileUnit.getNode());
947 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
948 Elts.push_back(Type.getNode());
949 Elts.insert(Elts.end(), addr.begin(), addr.end());
950
951 return DIVariable(MDNode::get(VMContext, &Elts[0], 6+addr.size()));
952}
953
954
Chris Lattnera45664f2008-11-10 02:56:27 +0000955/// CreateBlock - This creates a descriptor for a lexical block with the
Owen Anderson99035272009-07-07 17:12:53 +0000956/// specified parent VMContext.
Devang Patel5e005d82009-08-31 22:00:15 +0000957DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context) {
Devang Patele4b27562009-08-28 23:24:31 +0000958 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000959 GetTagConstant(dwarf::DW_TAG_lexical_block),
Devang Patele4b27562009-08-28 23:24:31 +0000960 Context.getNode()
Chris Lattnera45664f2008-11-10 02:56:27 +0000961 };
Devang Patel5e005d82009-08-31 22:00:15 +0000962 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 2));
Chris Lattnera45664f2008-11-10 02:56:27 +0000963}
964
Devang Patelf98d8fe2009-09-01 01:14:15 +0000965/// CreateLocation - Creates a debug info location.
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000966DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
Daniel Dunbara279bc32009-09-20 02:20:51 +0000967 DIScope S, DILocation OrigLoc) {
Devang Patelf98d8fe2009-09-01 01:14:15 +0000968 Value *Elts[] = {
969 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
970 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
971 S.getNode(),
972 OrigLoc.getNode(),
973 };
974 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
975}
976
Chris Lattnera45664f2008-11-10 02:56:27 +0000977
978//===----------------------------------------------------------------------===//
979// DIFactory: Routines for inserting code into a function
980//===----------------------------------------------------------------------===//
981
982/// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
983/// inserting it at the end of the specified basic block.
984void DIFactory::InsertStopPoint(DICompileUnit CU, unsigned LineNo,
985 unsigned ColNo, BasicBlock *BB) {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000986
Chris Lattnera45664f2008-11-10 02:56:27 +0000987 // Lazily construct llvm.dbg.stoppoint function.
988 if (!StopPointFn)
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000989 StopPointFn = llvm::Intrinsic::getDeclaration(&M,
Chris Lattnera45664f2008-11-10 02:56:27 +0000990 llvm::Intrinsic::dbg_stoppoint);
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000991
Chris Lattnera45664f2008-11-10 02:56:27 +0000992 // Invoke llvm.dbg.stoppoint
993 Value *Args[] = {
Owen Anderson1d0be152009-08-13 21:58:54 +0000994 ConstantInt::get(llvm::Type::getInt32Ty(VMContext), LineNo),
995 ConstantInt::get(llvm::Type::getInt32Ty(VMContext), ColNo),
Devang Patele4b27562009-08-28 23:24:31 +0000996 CU.getNode()
Chris Lattnera45664f2008-11-10 02:56:27 +0000997 };
998 CallInst::Create(StopPointFn, Args, Args+3, "", BB);
999}
1000
1001/// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
1002/// mark the start of the specified subprogram.
1003void DIFactory::InsertSubprogramStart(DISubprogram SP, BasicBlock *BB) {
1004 // Lazily construct llvm.dbg.func.start.
1005 if (!FuncStartFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +00001006 FuncStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_func_start);
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001007
Chris Lattnera45664f2008-11-10 02:56:27 +00001008 // Call llvm.dbg.func.start which also implicitly sets a stoppoint.
Devang Patele4b27562009-08-28 23:24:31 +00001009 CallInst::Create(FuncStartFn, SP.getNode(), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +00001010}
1011
1012/// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
1013/// mark the start of a region for the specified scoping descriptor.
1014void DIFactory::InsertRegionStart(DIDescriptor D, BasicBlock *BB) {
1015 // Lazily construct llvm.dbg.region.start function.
1016 if (!RegionStartFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +00001017 RegionStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_start);
1018
Chris Lattnera45664f2008-11-10 02:56:27 +00001019 // Call llvm.dbg.func.start.
Devang Patele4b27562009-08-28 23:24:31 +00001020 CallInst::Create(RegionStartFn, D.getNode(), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +00001021}
1022
Chris Lattnera45664f2008-11-10 02:56:27 +00001023/// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
1024/// mark the end of a region for the specified scoping descriptor.
1025void DIFactory::InsertRegionEnd(DIDescriptor D, BasicBlock *BB) {
1026 // Lazily construct llvm.dbg.region.end function.
1027 if (!RegionEndFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +00001028 RegionEndFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_end);
1029
1030 // Call llvm.dbg.region.end.
Devang Patele4b27562009-08-28 23:24:31 +00001031 CallInst::Create(RegionEndFn, D.getNode(), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +00001032}
1033
1034/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Mike Stumpe4250392009-10-01 22:08:58 +00001035void DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1036 Instruction *InsertBefore) {
Chris Lattnera45664f2008-11-10 02:56:27 +00001037 // Cast the storage to a {}* for the call to llvm.dbg.declare.
Mike Stumpe4250392009-10-01 22:08:58 +00001038 Storage = new BitCastInst(Storage, EmptyStructPtr, "", InsertBefore);
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001039
Chris Lattnera45664f2008-11-10 02:56:27 +00001040 if (!DeclareFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +00001041 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1042
Devang Patele4b27562009-08-28 23:24:31 +00001043 Value *Args[] = { Storage, D.getNode() };
Mike Stumpe4250392009-10-01 22:08:58 +00001044 CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
1045}
1046
1047/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1048void DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1049 BasicBlock *InsertAtEnd) {
1050 // Cast the storage to a {}* for the call to llvm.dbg.declare.
1051 Storage = new BitCastInst(Storage, EmptyStructPtr, "", InsertAtEnd);
1052
1053 if (!DeclareFn)
1054 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1055
1056 Value *Args[] = { Storage, D.getNode() };
1057 CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);
Chris Lattnera45664f2008-11-10 02:56:27 +00001058}
Torok Edwin620f2802008-12-16 09:07:36 +00001059
Devang Patele4b27562009-08-28 23:24:31 +00001060
Devang Pateld2f79a12009-07-28 19:55:13 +00001061//===----------------------------------------------------------------------===//
Devang Patel98c65172009-07-30 18:25:15 +00001062// DebugInfoFinder implementations.
Devang Pateld2f79a12009-07-28 19:55:13 +00001063//===----------------------------------------------------------------------===//
1064
Devang Patel98c65172009-07-30 18:25:15 +00001065/// processModule - Process entire module and collect debug info.
1066void DebugInfoFinder::processModule(Module &M) {
Devang Patele4b27562009-08-28 23:24:31 +00001067
Devang Patelbeab41b2009-10-07 22:04:08 +00001068#ifdef ATTACH_DEBUG_INFO_TO_AN_INSN
1069 MetadataContext &TheMetadata = M.getContext().getMetadata();
1070 unsigned MDDbgKind = TheMetadata.getMDKind("dbg");
Devang Patelbeab41b2009-10-07 22:04:08 +00001071#endif
Devang Pateld2f79a12009-07-28 19:55:13 +00001072 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1073 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
1074 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
1075 ++BI) {
1076 if (DbgStopPointInst *SPI = dyn_cast<DbgStopPointInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +00001077 processStopPoint(SPI);
Devang Pateld2f79a12009-07-28 19:55:13 +00001078 else if (DbgFuncStartInst *FSI = dyn_cast<DbgFuncStartInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +00001079 processFuncStart(FSI);
Devang Patele802f1c2009-07-30 17:30:23 +00001080 else if (DbgRegionStartInst *DRS = dyn_cast<DbgRegionStartInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +00001081 processRegionStart(DRS);
Devang Patele802f1c2009-07-30 17:30:23 +00001082 else if (DbgRegionEndInst *DRE = dyn_cast<DbgRegionEndInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +00001083 processRegionEnd(DRE);
Devang Patelb4d31302009-07-31 18:18:52 +00001084 else if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1085 processDeclare(DDI);
Devang Patelbeab41b2009-10-07 22:04:08 +00001086#ifdef ATTACH_DEBUG_INFO_TO_AN_INSN
Devang Patel549e81f2009-10-13 17:35:35 +00001087 else if (MDDbgKind) {
1088 if (MDNode *L = TheMetadata.getMD(MDDbgKind, BI)) {
1089 DILocation Loc(L);
1090 DIScope S(Loc.getScope().getNode());
1091 if (S.isCompileUnit())
1092 addCompileUnit(DICompileUnit(S.getNode()));
1093 else if (S.isSubprogram())
1094 processSubprogram(DISubprogram(S.getNode()));
1095 else if (S.isLexicalBlock())
1096 processLexicalBlock(DILexicalBlock(S.getNode()));
1097 }
Devang Patelbeab41b2009-10-07 22:04:08 +00001098 }
1099#endif
Devang Pateld2f79a12009-07-28 19:55:13 +00001100 }
Devang Patele4b27562009-08-28 23:24:31 +00001101
1102 NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv");
1103 if (!NMD)
1104 return;
1105
1106 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
1107 DIGlobalVariable DIG(cast<MDNode>(NMD->getElement(i)));
Devang Pateld2f79a12009-07-28 19:55:13 +00001108 if (addGlobalVariable(DIG)) {
1109 addCompileUnit(DIG.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +00001110 processType(DIG.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +00001111 }
1112 }
1113}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001114
Devang Patel98c65172009-07-30 18:25:15 +00001115/// processType - Process DIType.
1116void DebugInfoFinder::processType(DIType DT) {
Devang Patel72bcdb62009-08-10 22:09:58 +00001117 if (!addType(DT))
Devang Pateld2f79a12009-07-28 19:55:13 +00001118 return;
1119
1120 addCompileUnit(DT.getCompileUnit());
Devang Patel6ceea332009-08-31 18:49:10 +00001121 if (DT.isCompositeType()) {
Devang Patele4b27562009-08-28 23:24:31 +00001122 DICompositeType DCT(DT.getNode());
Devang Patel98c65172009-07-30 18:25:15 +00001123 processType(DCT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001124 DIArray DA = DCT.getTypeArray();
1125 if (!DA.isNull())
1126 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1127 DIDescriptor D = DA.getElement(i);
Devang Patele4b27562009-08-28 23:24:31 +00001128 DIType TypeE = DIType(D.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001129 if (!TypeE.isNull())
Devang Patel98c65172009-07-30 18:25:15 +00001130 processType(TypeE);
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001131 else
Devang Patele4b27562009-08-28 23:24:31 +00001132 processSubprogram(DISubprogram(D.getNode()));
Devang Pateld2f79a12009-07-28 19:55:13 +00001133 }
Devang Patel6ceea332009-08-31 18:49:10 +00001134 } else if (DT.isDerivedType()) {
Devang Patele4b27562009-08-28 23:24:31 +00001135 DIDerivedType DDT(DT.getNode());
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001136 if (!DDT.isNull())
Devang Patel98c65172009-07-30 18:25:15 +00001137 processType(DDT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001138 }
1139}
1140
Devang Patelbeab41b2009-10-07 22:04:08 +00001141/// processLexicalBlock
1142void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
1143 if (LB.isNull())
1144 return;
1145 DIScope Context = LB.getContext();
1146 if (Context.isLexicalBlock())
1147 return processLexicalBlock(DILexicalBlock(Context.getNode()));
1148 else
1149 return processSubprogram(DISubprogram(Context.getNode()));
1150}
1151
Devang Patel98c65172009-07-30 18:25:15 +00001152/// processSubprogram - Process DISubprogram.
1153void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Patele802f1c2009-07-30 17:30:23 +00001154 if (SP.isNull())
1155 return;
Devang Pateld2f79a12009-07-28 19:55:13 +00001156 if (!addSubprogram(SP))
1157 return;
1158 addCompileUnit(SP.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +00001159 processType(SP.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +00001160}
1161
Devang Patel98c65172009-07-30 18:25:15 +00001162/// processStopPoint - Process DbgStopPointInst.
1163void DebugInfoFinder::processStopPoint(DbgStopPointInst *SPI) {
Devang Patele4b27562009-08-28 23:24:31 +00001164 MDNode *Context = dyn_cast<MDNode>(SPI->getContext());
Devang Pateld2f79a12009-07-28 19:55:13 +00001165 addCompileUnit(DICompileUnit(Context));
1166}
1167
Devang Patel98c65172009-07-30 18:25:15 +00001168/// processFuncStart - Process DbgFuncStartInst.
1169void DebugInfoFinder::processFuncStart(DbgFuncStartInst *FSI) {
Devang Patele4b27562009-08-28 23:24:31 +00001170 MDNode *SP = dyn_cast<MDNode>(FSI->getSubprogram());
Devang Patel98c65172009-07-30 18:25:15 +00001171 processSubprogram(DISubprogram(SP));
Devang Pateld2f79a12009-07-28 19:55:13 +00001172}
1173
Devang Patel98c65172009-07-30 18:25:15 +00001174/// processRegionStart - Process DbgRegionStart.
1175void DebugInfoFinder::processRegionStart(DbgRegionStartInst *DRS) {
Devang Patele4b27562009-08-28 23:24:31 +00001176 MDNode *SP = dyn_cast<MDNode>(DRS->getContext());
Devang Patel98c65172009-07-30 18:25:15 +00001177 processSubprogram(DISubprogram(SP));
Devang Patele802f1c2009-07-30 17:30:23 +00001178}
1179
Devang Patel98c65172009-07-30 18:25:15 +00001180/// processRegionEnd - Process DbgRegionEnd.
1181void DebugInfoFinder::processRegionEnd(DbgRegionEndInst *DRE) {
Devang Patele4b27562009-08-28 23:24:31 +00001182 MDNode *SP = dyn_cast<MDNode>(DRE->getContext());
Devang Patel98c65172009-07-30 18:25:15 +00001183 processSubprogram(DISubprogram(SP));
Devang Patele802f1c2009-07-30 17:30:23 +00001184}
1185
Devang Patelb4d31302009-07-31 18:18:52 +00001186/// processDeclare - Process DbgDeclareInst.
1187void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
Devang Patele4b27562009-08-28 23:24:31 +00001188 DIVariable DV(cast<MDNode>(DDI->getVariable()));
Devang Patelb4d31302009-07-31 18:18:52 +00001189 if (DV.isNull())
1190 return;
1191
Devang Patele4b27562009-08-28 23:24:31 +00001192 if (!NodesSeen.insert(DV.getNode()))
Devang Patelb4d31302009-07-31 18:18:52 +00001193 return;
1194
1195 addCompileUnit(DV.getCompileUnit());
1196 processType(DV.getType());
1197}
1198
Devang Patel72bcdb62009-08-10 22:09:58 +00001199/// addType - Add type into Tys.
1200bool DebugInfoFinder::addType(DIType DT) {
1201 if (DT.isNull())
1202 return false;
1203
Devang Patele4b27562009-08-28 23:24:31 +00001204 if (!NodesSeen.insert(DT.getNode()))
Devang Patel72bcdb62009-08-10 22:09:58 +00001205 return false;
1206
Devang Patele4b27562009-08-28 23:24:31 +00001207 TYs.push_back(DT.getNode());
Devang Patel72bcdb62009-08-10 22:09:58 +00001208 return true;
1209}
1210
Devang Pateld2f79a12009-07-28 19:55:13 +00001211/// addCompileUnit - Add compile unit into CUs.
Devang Patel98c65172009-07-30 18:25:15 +00001212bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001213 if (CU.isNull())
1214 return false;
1215
Devang Patele4b27562009-08-28 23:24:31 +00001216 if (!NodesSeen.insert(CU.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001217 return false;
1218
Devang Patele4b27562009-08-28 23:24:31 +00001219 CUs.push_back(CU.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001220 return true;
1221}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001222
Devang Pateld2f79a12009-07-28 19:55:13 +00001223/// addGlobalVariable - Add global variable into GVs.
Devang Patel98c65172009-07-30 18:25:15 +00001224bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001225 if (DIG.isNull())
1226 return false;
1227
Devang Patele4b27562009-08-28 23:24:31 +00001228 if (!NodesSeen.insert(DIG.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001229 return false;
1230
Devang Patele4b27562009-08-28 23:24:31 +00001231 GVs.push_back(DIG.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001232 return true;
1233}
1234
1235// addSubprogram - Add subprgoram into SPs.
Devang Patel98c65172009-07-30 18:25:15 +00001236bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001237 if (SP.isNull())
1238 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001239
Devang Patele4b27562009-08-28 23:24:31 +00001240 if (!NodesSeen.insert(SP.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001241 return false;
1242
Devang Patele4b27562009-08-28 23:24:31 +00001243 SPs.push_back(SP.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001244 return true;
1245}
1246
Torok Edwin620f2802008-12-16 09:07:36 +00001247namespace llvm {
Bill Wendlingdc817b62009-05-14 18:26:15 +00001248 /// findStopPoint - Find the stoppoint coressponding to this instruction, that
1249 /// is the stoppoint that dominates this instruction.
1250 const DbgStopPointInst *findStopPoint(const Instruction *Inst) {
Torok Edwin620f2802008-12-16 09:07:36 +00001251 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(Inst))
1252 return DSI;
1253
1254 const BasicBlock *BB = Inst->getParent();
1255 BasicBlock::const_iterator I = Inst, B;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001256 while (BB) {
Torok Edwin620f2802008-12-16 09:07:36 +00001257 B = BB->begin();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001258
Torok Edwin620f2802008-12-16 09:07:36 +00001259 // A BB consisting only of a terminator can't have a stoppoint.
Bill Wendlingdc817b62009-05-14 18:26:15 +00001260 while (I != B) {
1261 --I;
1262 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
1263 return DSI;
Torok Edwin620f2802008-12-16 09:07:36 +00001264 }
Bill Wendlingdc817b62009-05-14 18:26:15 +00001265
1266 // This BB didn't have a stoppoint: if there is only one predecessor, look
1267 // for a stoppoint there. We could use getIDom(), but that would require
1268 // dominator info.
Torok Edwin620f2802008-12-16 09:07:36 +00001269 BB = I->getParent()->getUniquePredecessor();
1270 if (BB)
1271 I = BB->getTerminator();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001272 }
1273
Torok Edwin620f2802008-12-16 09:07:36 +00001274 return 0;
1275 }
1276
Bill Wendlingdc817b62009-05-14 18:26:15 +00001277 /// findBBStopPoint - Find the stoppoint corresponding to first real
1278 /// (non-debug intrinsic) instruction in this Basic Block, and return the
1279 /// stoppoint for it.
1280 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB) {
1281 for(BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001282 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
1283 return DSI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001284
1285 // Fallback to looking for stoppoint of unique predecessor. Useful if this
1286 // BB contains no stoppoints, but unique predecessor does.
Torok Edwin620f2802008-12-16 09:07:36 +00001287 BB = BB->getUniquePredecessor();
1288 if (BB)
1289 return findStopPoint(BB->getTerminator());
Bill Wendlingdc817b62009-05-14 18:26:15 +00001290
Torok Edwin620f2802008-12-16 09:07:36 +00001291 return 0;
1292 }
1293
Bill Wendlingdc817b62009-05-14 18:26:15 +00001294 Value *findDbgGlobalDeclare(GlobalVariable *V) {
Torok Edwinff7d0e92009-03-10 13:41:26 +00001295 const Module *M = V->getParent();
Devang Patele4b27562009-08-28 23:24:31 +00001296 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1297 if (!NMD)
1298 return 0;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001299
Devang Patele4b27562009-08-28 23:24:31 +00001300 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
1301 DIGlobalVariable DIG(cast_or_null<MDNode>(NMD->getElement(i)));
1302 if (DIG.isNull())
1303 continue;
1304 if (DIG.getGlobal() == V)
1305 return DIG.getNode();
Torok Edwinff7d0e92009-03-10 13:41:26 +00001306 }
Torok Edwinff7d0e92009-03-10 13:41:26 +00001307 return 0;
1308 }
1309
Bill Wendlingdc817b62009-05-14 18:26:15 +00001310 /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
Torok Edwin620f2802008-12-16 09:07:36 +00001311 /// It looks through pointer casts too.
Bill Wendlingdc817b62009-05-14 18:26:15 +00001312 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts) {
Torok Edwin620f2802008-12-16 09:07:36 +00001313 if (stripCasts) {
1314 V = V->stripPointerCasts();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001315
Torok Edwin620f2802008-12-16 09:07:36 +00001316 // Look for the bitcast.
1317 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001318 I != E; ++I)
Devang Patel94dfaec2009-10-29 18:20:34 +00001319 if (isa<BitCastInst>(I)) {
1320 const DbgDeclareInst *DDI = findDbgDeclare(*I, false);
1321 if (DDI) return DDI;
1322 }
Torok Edwin620f2802008-12-16 09:07:36 +00001323 return 0;
1324 }
1325
Bill Wendlingdc817b62009-05-14 18:26:15 +00001326 // Find llvm.dbg.declare among uses of the instruction.
Torok Edwin620f2802008-12-16 09:07:36 +00001327 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001328 I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001329 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I))
1330 return DDI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001331
Torok Edwin620f2802008-12-16 09:07:36 +00001332 return 0;
1333 }
Torok Edwinff7d0e92009-03-10 13:41:26 +00001334
Devang Patel5ccdd102009-09-29 18:40:58 +00001335bool getLocationInfo(const Value *V, std::string &DisplayName,
1336 std::string &Type, unsigned &LineNo, std::string &File,
Bill Wendlingdc817b62009-05-14 18:26:15 +00001337 std::string &Dir) {
Torok Edwinff7d0e92009-03-10 13:41:26 +00001338 DICompileUnit Unit;
1339 DIType TypeD;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001340
Torok Edwinff7d0e92009-03-10 13:41:26 +00001341 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1342 Value *DIGV = findDbgGlobalDeclare(GV);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001343 if (!DIGV) return false;
Devang Patele4b27562009-08-28 23:24:31 +00001344 DIGlobalVariable Var(cast<MDNode>(DIGV));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001345
Devang Patel5ccdd102009-09-29 18:40:58 +00001346 if (const char *D = Var.getDisplayName())
1347 DisplayName = D;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001348 LineNo = Var.getLineNumber();
1349 Unit = Var.getCompileUnit();
1350 TypeD = Var.getType();
1351 } else {
1352 const DbgDeclareInst *DDI = findDbgDeclare(V);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001353 if (!DDI) return false;
Devang Patele4b27562009-08-28 23:24:31 +00001354 DIVariable Var(cast<MDNode>(DDI->getVariable()));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001355
Devang Patel5ccdd102009-09-29 18:40:58 +00001356 if (const char *D = Var.getName())
1357 DisplayName = D;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001358 LineNo = Var.getLineNumber();
1359 Unit = Var.getCompileUnit();
1360 TypeD = Var.getType();
1361 }
Bill Wendlingdc817b62009-05-14 18:26:15 +00001362
Devang Patel5ccdd102009-09-29 18:40:58 +00001363 if (const char *T = TypeD.getName())
1364 Type = T;
1365 if (const char *F = Unit.getFilename())
1366 File = F;
1367 if (const char *D = Unit.getDirectory())
1368 Dir = D;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001369 return true;
1370 }
Devang Patel13e16b62009-06-26 01:49:18 +00001371
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001372 /// isValidDebugInfoIntrinsic - Return true if SPI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001373 /// info intrinsic.
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001374 bool isValidDebugInfoIntrinsic(DbgStopPointInst &SPI,
Devang Patel9e529c32009-07-02 01:15:24 +00001375 CodeGenOpt::Level OptLev) {
1376 return DIDescriptor::ValidDebugInfo(SPI.getContext(), OptLev);
1377 }
1378
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001379 /// isValidDebugInfoIntrinsic - Return true if FSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001380 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001381 bool isValidDebugInfoIntrinsic(DbgFuncStartInst &FSI,
1382 CodeGenOpt::Level OptLev) {
1383 return DIDescriptor::ValidDebugInfo(FSI.getSubprogram(), OptLev);
1384 }
1385
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001386 /// isValidDebugInfoIntrinsic - Return true if RSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001387 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001388 bool isValidDebugInfoIntrinsic(DbgRegionStartInst &RSI,
1389 CodeGenOpt::Level OptLev) {
1390 return DIDescriptor::ValidDebugInfo(RSI.getContext(), OptLev);
1391 }
1392
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001393 /// isValidDebugInfoIntrinsic - Return true if REI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001394 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001395 bool isValidDebugInfoIntrinsic(DbgRegionEndInst &REI,
1396 CodeGenOpt::Level OptLev) {
1397 return DIDescriptor::ValidDebugInfo(REI.getContext(), OptLev);
1398 }
1399
1400
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001401 /// isValidDebugInfoIntrinsic - Return true if DI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001402 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001403 bool isValidDebugInfoIntrinsic(DbgDeclareInst &DI,
1404 CodeGenOpt::Level OptLev) {
1405 return DIDescriptor::ValidDebugInfo(DI.getVariable(), OptLev);
1406 }
1407
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001408 /// ExtractDebugLocation - Extract debug location information
Devang Patel9e529c32009-07-02 01:15:24 +00001409 /// from llvm.dbg.stoppoint intrinsic.
1410 DebugLoc ExtractDebugLocation(DbgStopPointInst &SPI,
Devang Patel9e529c32009-07-02 01:15:24 +00001411 DebugLocTracker &DebugLocInfo) {
1412 DebugLoc DL;
1413 Value *Context = SPI.getContext();
Devang Patel9e529c32009-07-02 01:15:24 +00001414
1415 // If this location is already tracked then use it.
Devang Patela1434042009-10-01 01:15:28 +00001416 DebugLocTuple Tuple(cast<MDNode>(Context), NULL, SPI.getLine(),
Devang Patel9e529c32009-07-02 01:15:24 +00001417 SPI.getColumn());
1418 DenseMap<DebugLocTuple, unsigned>::iterator II
1419 = DebugLocInfo.DebugIdMap.find(Tuple);
1420 if (II != DebugLocInfo.DebugIdMap.end())
1421 return DebugLoc::get(II->second);
1422
1423 // Add a new location entry.
1424 unsigned Id = DebugLocInfo.DebugLocations.size();
1425 DebugLocInfo.DebugLocations.push_back(Tuple);
1426 DebugLocInfo.DebugIdMap[Tuple] = Id;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001427
Devang Patel9e529c32009-07-02 01:15:24 +00001428 return DebugLoc::get(Id);
1429 }
1430
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001431 /// ExtractDebugLocation - Extract debug location information
Devang Patel1b75f442009-09-16 18:20:05 +00001432 /// from DILocation.
1433 DebugLoc ExtractDebugLocation(DILocation &Loc,
1434 DebugLocTracker &DebugLocInfo) {
1435 DebugLoc DL;
1436 MDNode *Context = Loc.getScope().getNode();
Devang Patela1434042009-10-01 01:15:28 +00001437 MDNode *InlinedLoc = NULL;
1438 if (!Loc.getOrigLocation().isNull())
1439 InlinedLoc = Loc.getOrigLocation().getNode();
Devang Patel1b75f442009-09-16 18:20:05 +00001440 // If this location is already tracked then use it.
Devang Patela1434042009-10-01 01:15:28 +00001441 DebugLocTuple Tuple(Context, InlinedLoc, Loc.getLineNumber(),
Daniel Dunbara279bc32009-09-20 02:20:51 +00001442 Loc.getColumnNumber());
Devang Patel1b75f442009-09-16 18:20:05 +00001443 DenseMap<DebugLocTuple, unsigned>::iterator II
1444 = DebugLocInfo.DebugIdMap.find(Tuple);
1445 if (II != DebugLocInfo.DebugIdMap.end())
1446 return DebugLoc::get(II->second);
1447
1448 // Add a new location entry.
1449 unsigned Id = DebugLocInfo.DebugLocations.size();
1450 DebugLocInfo.DebugLocations.push_back(Tuple);
1451 DebugLocInfo.DebugIdMap[Tuple] = Id;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001452
Devang Patel1b75f442009-09-16 18:20:05 +00001453 return DebugLoc::get(Id);
1454 }
1455
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001456 /// ExtractDebugLocation - Extract debug location information
Devang Patel9e529c32009-07-02 01:15:24 +00001457 /// from llvm.dbg.func_start intrinsic.
1458 DebugLoc ExtractDebugLocation(DbgFuncStartInst &FSI,
Devang Patel9e529c32009-07-02 01:15:24 +00001459 DebugLocTracker &DebugLocInfo) {
1460 DebugLoc DL;
1461 Value *SP = FSI.getSubprogram();
Devang Patel9e529c32009-07-02 01:15:24 +00001462
Devang Patele4b27562009-08-28 23:24:31 +00001463 DISubprogram Subprogram(cast<MDNode>(SP));
Devang Patel9e529c32009-07-02 01:15:24 +00001464 unsigned Line = Subprogram.getLineNumber();
1465 DICompileUnit CU(Subprogram.getCompileUnit());
1466
1467 // If this location is already tracked then use it.
Devang Patela1434042009-10-01 01:15:28 +00001468 DebugLocTuple Tuple(CU.getNode(), NULL, Line, /* Column */ 0);
Devang Patel9e529c32009-07-02 01:15:24 +00001469 DenseMap<DebugLocTuple, unsigned>::iterator II
1470 = DebugLocInfo.DebugIdMap.find(Tuple);
1471 if (II != DebugLocInfo.DebugIdMap.end())
1472 return DebugLoc::get(II->second);
1473
1474 // Add a new location entry.
1475 unsigned Id = DebugLocInfo.DebugLocations.size();
1476 DebugLocInfo.DebugLocations.push_back(Tuple);
1477 DebugLocInfo.DebugIdMap[Tuple] = Id;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001478
Devang Patel9e529c32009-07-02 01:15:24 +00001479 return DebugLoc::get(Id);
1480 }
1481
1482 /// isInlinedFnStart - Return true if FSI is starting an inlined function.
1483 bool isInlinedFnStart(DbgFuncStartInst &FSI, const Function *CurrentFn) {
Devang Patele4b27562009-08-28 23:24:31 +00001484 DISubprogram Subprogram(cast<MDNode>(FSI.getSubprogram()));
Devang Patel9e529c32009-07-02 01:15:24 +00001485 if (Subprogram.describes(CurrentFn))
1486 return false;
1487
1488 return true;
1489 }
1490
1491 /// isInlinedFnEnd - Return true if REI is ending an inlined function.
1492 bool isInlinedFnEnd(DbgRegionEndInst &REI, const Function *CurrentFn) {
Devang Patele4b27562009-08-28 23:24:31 +00001493 DISubprogram Subprogram(cast<MDNode>(REI.getContext()));
Devang Patel9e529c32009-07-02 01:15:24 +00001494 if (Subprogram.isNull() || Subprogram.describes(CurrentFn))
1495 return false;
1496
1497 return true;
1498 }
Torok Edwin620f2802008-12-16 09:07:36 +00001499}