blob: b64dbf433aae066d7e7cc0f00dc82519413eea00 [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 Patelb79b5352009-01-19 23:21:49 +0000369 if (getContext().isNull())
370 return false;
371
372 DICompileUnit CU = getCompileUnit();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000373 if (!CU.isNull() && !CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000374 return false;
375
376 DIType Ty = getType();
377 if (!Ty.Verify())
378 return false;
379
380 if (!getGlobal())
381 return false;
382
383 return true;
384}
385
386/// Verify - Verify that a variable descriptor is well formed.
387bool DIVariable::Verify() const {
388 if (isNull())
389 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000390
Devang Patelb79b5352009-01-19 23:21:49 +0000391 if (getContext().isNull())
392 return false;
393
394 DIType Ty = getType();
395 if (!Ty.Verify())
396 return false;
397
Devang Patelb79b5352009-01-19 23:21:49 +0000398 return true;
399}
400
Devang Patel36375ee2009-02-17 21:23:59 +0000401/// getOriginalTypeSize - If this type is derived from a base type then
402/// return base type size.
403uint64_t DIDerivedType::getOriginalTypeSize() const {
Devang Patel61ecbd12009-11-04 23:48:00 +0000404 unsigned Tag = getTag();
405 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
406 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
407 Tag == dwarf::DW_TAG_restrict_type) {
408 DIType BaseType = getTypeDerivedFrom();
409 if (BaseType.isDerivedType())
410 return DIDerivedType(BaseType.getNode()).getOriginalTypeSize();
411 else
412 return BaseType.getSizeInBits();
413 }
414
415 return getSizeInBits();
Devang Patel36375ee2009-02-17 21:23:59 +0000416}
Devang Patelb79b5352009-01-19 23:21:49 +0000417
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000418/// describes - Return true if this subprogram provides debugging
419/// information for the function F.
420bool DISubprogram::describes(const Function *F) {
421 assert (F && "Invalid function");
Devang Patel5ccdd102009-09-29 18:40:58 +0000422 const char *Name = getLinkageName();
423 if (!Name)
424 Name = getName();
425 if (strcmp(F->getName().data(), Name) == 0)
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000426 return true;
427 return false;
428}
429
Devang Patelecbeb1a2009-09-30 22:34:41 +0000430const char *DIScope::getFilename() const {
431 if (isLexicalBlock())
432 return DILexicalBlock(DbgNode).getFilename();
433 else if (isSubprogram())
434 return DISubprogram(DbgNode).getFilename();
435 else if (isCompileUnit())
436 return DICompileUnit(DbgNode).getFilename();
437 else
438 assert (0 && "Invalid DIScope!");
439 return NULL;
440}
441
442const char *DIScope::getDirectory() const {
443 if (isLexicalBlock())
444 return DILexicalBlock(DbgNode).getDirectory();
445 else if (isSubprogram())
446 return DISubprogram(DbgNode).getDirectory();
447 else if (isCompileUnit())
448 return DICompileUnit(DbgNode).getDirectory();
449 else
450 assert (0 && "Invalid DIScope!");
451 return NULL;
452}
453
Chris Lattnera45664f2008-11-10 02:56:27 +0000454//===----------------------------------------------------------------------===//
Devang Patel7136a652009-07-01 22:10:23 +0000455// DIDescriptor: dump routines for all descriptors.
456//===----------------------------------------------------------------------===//
457
458
459/// dump - Print descriptor.
460void DIDescriptor::dump() const {
Devang Patele4b27562009-08-28 23:24:31 +0000461 errs() << "[" << dwarf::TagString(getTag()) << "] ";
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000462 errs().write_hex((intptr_t) &*DbgNode) << ']';
Devang Patel7136a652009-07-01 22:10:23 +0000463}
464
465/// dump - Print compile unit.
466void DICompileUnit::dump() const {
467 if (getLanguage())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000468 errs() << " [" << dwarf::LanguageString(getLanguage()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000469
Devang Patel5ccdd102009-09-29 18:40:58 +0000470 errs() << " [" << getDirectory() << "/" << getFilename() << " ]";
Devang Patel7136a652009-07-01 22:10:23 +0000471}
472
473/// dump - Print type.
474void DIType::dump() const {
475 if (isNull()) return;
476
Devang Patel5ccdd102009-09-29 18:40:58 +0000477 if (const char *Res = getName())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000478 errs() << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000479
480 unsigned Tag = getTag();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000481 errs() << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000482
483 // TODO : Print context
484 getCompileUnit().dump();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000485 errs() << " ["
486 << getLineNumber() << ", "
Chris Lattnera81d29b2009-08-23 07:33:14 +0000487 << getSizeInBits() << ", "
488 << getAlignInBits() << ", "
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000489 << getOffsetInBits()
Chris Lattnera81d29b2009-08-23 07:33:14 +0000490 << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000491
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000492 if (isPrivate())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000493 errs() << " [private] ";
Devang Patel7136a652009-07-01 22:10:23 +0000494 else if (isProtected())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000495 errs() << " [protected] ";
Devang Patel7136a652009-07-01 22:10:23 +0000496
497 if (isForwardDecl())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000498 errs() << " [fwd] ";
Devang Patel7136a652009-07-01 22:10:23 +0000499
Devang Patel6ceea332009-08-31 18:49:10 +0000500 if (isBasicType())
Devang Patele4b27562009-08-28 23:24:31 +0000501 DIBasicType(DbgNode).dump();
Devang Patel6ceea332009-08-31 18:49:10 +0000502 else if (isDerivedType())
Devang Patele4b27562009-08-28 23:24:31 +0000503 DIDerivedType(DbgNode).dump();
Devang Patel6ceea332009-08-31 18:49:10 +0000504 else if (isCompositeType())
Devang Patele4b27562009-08-28 23:24:31 +0000505 DICompositeType(DbgNode).dump();
Devang Patel7136a652009-07-01 22:10:23 +0000506 else {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000507 errs() << "Invalid DIType\n";
Devang Patel7136a652009-07-01 22:10:23 +0000508 return;
509 }
510
Chris Lattnera81d29b2009-08-23 07:33:14 +0000511 errs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000512}
513
514/// dump - Print basic type.
515void DIBasicType::dump() const {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000516 errs() << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000517}
518
519/// dump - Print derived type.
520void DIDerivedType::dump() const {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000521 errs() << "\n\t Derived From: "; getTypeDerivedFrom().dump();
Devang Patel7136a652009-07-01 22:10:23 +0000522}
523
524/// dump - Print composite type.
525void DICompositeType::dump() const {
526 DIArray A = getTypeArray();
527 if (A.isNull())
528 return;
Chris Lattnera81d29b2009-08-23 07:33:14 +0000529 errs() << " [" << A.getNumElements() << " elements]";
Devang Patel7136a652009-07-01 22:10:23 +0000530}
531
532/// dump - Print global.
533void DIGlobal::dump() const {
Devang Patel5ccdd102009-09-29 18:40:58 +0000534 if (const char *Res = getName())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000535 errs() << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000536
537 unsigned Tag = getTag();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000538 errs() << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000539
540 // TODO : Print context
541 getCompileUnit().dump();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000542 errs() << " [" << getLineNumber() << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000543
544 if (isLocalToUnit())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000545 errs() << " [local] ";
Devang Patel7136a652009-07-01 22:10:23 +0000546
547 if (isDefinition())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000548 errs() << " [def] ";
Devang Patel7136a652009-07-01 22:10:23 +0000549
Devang Patel6ceea332009-08-31 18:49:10 +0000550 if (isGlobalVariable())
Devang Patele4b27562009-08-28 23:24:31 +0000551 DIGlobalVariable(DbgNode).dump();
Devang Patel7136a652009-07-01 22:10:23 +0000552
Chris Lattnera81d29b2009-08-23 07:33:14 +0000553 errs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000554}
555
556/// dump - Print subprogram.
557void DISubprogram::dump() const {
Devang Patel5ccdd102009-09-29 18:40:58 +0000558 if (const char *Res = getName())
Devang Patel82dfc0c2009-08-31 22:47:13 +0000559 errs() << " [" << Res << "] ";
560
561 unsigned Tag = getTag();
562 errs() << " [" << dwarf::TagString(Tag) << "] ";
563
564 // TODO : Print context
565 getCompileUnit().dump();
566 errs() << " [" << getLineNumber() << "] ";
567
568 if (isLocalToUnit())
569 errs() << " [local] ";
570
571 if (isDefinition())
572 errs() << " [def] ";
573
574 errs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000575}
576
577/// dump - Print global variable.
578void DIGlobalVariable::dump() const {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000579 errs() << " [";
580 getGlobal()->dump();
581 errs() << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000582}
583
584/// dump - Print variable.
585void DIVariable::dump() const {
Devang Patel5ccdd102009-09-29 18:40:58 +0000586 if (const char *Res = getName())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000587 errs() << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000588
589 getCompileUnit().dump();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000590 errs() << " [" << getLineNumber() << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000591 getType().dump();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000592 errs() << "\n";
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000593
594 // FIXME: Dump complex addresses
Devang Patel7136a652009-07-01 22:10:23 +0000595}
596
597//===----------------------------------------------------------------------===//
Chris Lattnera45664f2008-11-10 02:56:27 +0000598// DIFactory: Basic Helpers
599//===----------------------------------------------------------------------===//
600
Bill Wendlingdc817b62009-05-14 18:26:15 +0000601DIFactory::DIFactory(Module &m)
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000602 : M(m), VMContext(M.getContext()), StopPointFn(0), FuncStartFn(0),
Owen Anderson99035272009-07-07 17:12:53 +0000603 RegionStartFn(0), RegionEndFn(0),
Bill Wendlingdc817b62009-05-14 18:26:15 +0000604 DeclareFn(0) {
Owen Andersond7f2a6c2009-08-05 23:16:16 +0000605 EmptyStructPtr = PointerType::getUnqual(StructType::get(VMContext));
Chris Lattner497a7a82008-11-10 04:10:34 +0000606}
607
Chris Lattnera45664f2008-11-10 02:56:27 +0000608Constant *DIFactory::GetTagConstant(unsigned TAG) {
Devang Patel6906ba52009-01-20 19:22:03 +0000609 assert((TAG & LLVMDebugVersionMask) == 0 &&
Chris Lattnera45664f2008-11-10 02:56:27 +0000610 "Tag too large for debug encoding!");
Owen Anderson1d0be152009-08-13 21:58:54 +0000611 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
Chris Lattnera45664f2008-11-10 02:56:27 +0000612}
613
Chris Lattnera45664f2008-11-10 02:56:27 +0000614//===----------------------------------------------------------------------===//
615// DIFactory: Primary Constructors
616//===----------------------------------------------------------------------===//
617
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000618/// GetOrCreateArray - Create an descriptor for an array of descriptors.
Chris Lattnera45664f2008-11-10 02:56:27 +0000619/// This implicitly uniques the arrays created.
620DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
Devang Patele4b27562009-08-28 23:24:31 +0000621 SmallVector<Value*, 16> Elts;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000622
Devang Patele4b27562009-08-28 23:24:31 +0000623 if (NumTys == 0)
624 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)));
625 else
626 for (unsigned i = 0; i != NumTys; ++i)
627 Elts.push_back(Tys[i].getNode());
Devang Patel82459882009-08-26 05:01:18 +0000628
Devang Patele4b27562009-08-28 23:24:31 +0000629 return DIArray(MDNode::get(VMContext,Elts.data(), Elts.size()));
Chris Lattnera45664f2008-11-10 02:56:27 +0000630}
631
632/// GetOrCreateSubrange - Create a descriptor for a value range. This
633/// implicitly uniques the values returned.
634DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
Devang Patele4b27562009-08-28 23:24:31 +0000635 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000636 GetTagConstant(dwarf::DW_TAG_subrange_type),
Owen Anderson1d0be152009-08-13 21:58:54 +0000637 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
638 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
Chris Lattnera45664f2008-11-10 02:56:27 +0000639 };
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000640
Devang Patele4b27562009-08-28 23:24:31 +0000641 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000642}
643
644
645
646/// CreateCompileUnit - Create a new descriptor for the specified compile
647/// unit. Note that this does not unique compile units within the module.
648DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
Devang Patel5ccdd102009-09-29 18:40:58 +0000649 StringRef Filename,
650 StringRef Directory,
651 StringRef Producer,
Devang Pateldd9db662009-01-30 18:20:31 +0000652 bool isMain,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000653 bool isOptimized,
Devang Patel13319ce2009-02-17 22:43:44 +0000654 const char *Flags,
655 unsigned RunTimeVer) {
Devang Patele4b27562009-08-28 23:24:31 +0000656 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000657 GetTagConstant(dwarf::DW_TAG_compile_unit),
Devang Patele4b27562009-08-28 23:24:31 +0000658 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Owen Anderson1d0be152009-08-13 21:58:54 +0000659 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
Devang Patele4b27562009-08-28 23:24:31 +0000660 MDString::get(VMContext, Filename),
661 MDString::get(VMContext, Directory),
662 MDString::get(VMContext, Producer),
Owen Anderson1d0be152009-08-13 21:58:54 +0000663 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
664 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
Devang Patele4b27562009-08-28 23:24:31 +0000665 MDString::get(VMContext, Flags),
Owen Anderson1d0be152009-08-13 21:58:54 +0000666 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
Chris Lattnera45664f2008-11-10 02:56:27 +0000667 };
Devang Patele4b27562009-08-28 23:24:31 +0000668
669 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000670}
671
672/// CreateEnumerator - Create a single enumerator value.
Devang Patel5ccdd102009-09-29 18:40:58 +0000673DIEnumerator DIFactory::CreateEnumerator(StringRef Name, uint64_t Val){
Devang Patele4b27562009-08-28 23:24:31 +0000674 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000675 GetTagConstant(dwarf::DW_TAG_enumerator),
Devang Patele4b27562009-08-28 23:24:31 +0000676 MDString::get(VMContext, Name),
Owen Anderson1d0be152009-08-13 21:58:54 +0000677 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
Chris Lattnera45664f2008-11-10 02:56:27 +0000678 };
Devang Patele4b27562009-08-28 23:24:31 +0000679 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000680}
681
682
683/// CreateBasicType - Create a basic type like int, float, etc.
684DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
Devang Patel5ccdd102009-09-29 18:40:58 +0000685 StringRef Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000686 DICompileUnit CompileUnit,
687 unsigned LineNumber,
688 uint64_t SizeInBits,
689 uint64_t AlignInBits,
690 uint64_t OffsetInBits, unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000691 unsigned Encoding) {
Devang Patele4b27562009-08-28 23:24:31 +0000692 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000693 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patele4b27562009-08-28 23:24:31 +0000694 Context.getNode(),
695 MDString::get(VMContext, Name),
696 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000697 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
698 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
699 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
700 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
701 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
702 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
Chris Lattnera45664f2008-11-10 02:56:27 +0000703 };
Devang Patele4b27562009-08-28 23:24:31 +0000704 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000705}
706
Devang Patelac16d442009-10-26 16:54:35 +0000707
708/// CreateBasicType - Create a basic type like int, float, etc.
709DIBasicType DIFactory::CreateBasicTypeEx(DIDescriptor Context,
710 StringRef Name,
711 DICompileUnit CompileUnit,
712 unsigned LineNumber,
713 Constant *SizeInBits,
714 Constant *AlignInBits,
715 Constant *OffsetInBits, unsigned Flags,
716 unsigned Encoding) {
717 Value *Elts[] = {
718 GetTagConstant(dwarf::DW_TAG_base_type),
719 Context.getNode(),
720 MDString::get(VMContext, Name),
721 CompileUnit.getNode(),
722 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
723 SizeInBits,
724 AlignInBits,
725 OffsetInBits,
726 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
727 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
728 };
729 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
730}
731
732
Chris Lattnera45664f2008-11-10 02:56:27 +0000733/// CreateDerivedType - Create a derived type like const qualified type,
734/// pointer, typedef, etc.
735DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
736 DIDescriptor Context,
Devang Patel5ccdd102009-09-29 18:40:58 +0000737 StringRef Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000738 DICompileUnit CompileUnit,
739 unsigned LineNumber,
740 uint64_t SizeInBits,
741 uint64_t AlignInBits,
742 uint64_t OffsetInBits,
743 unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000744 DIType DerivedFrom) {
Devang Patele4b27562009-08-28 23:24:31 +0000745 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000746 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000747 Context.getNode(),
748 MDString::get(VMContext, Name),
749 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000750 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
751 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
752 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
753 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
754 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patele4b27562009-08-28 23:24:31 +0000755 DerivedFrom.getNode(),
Chris Lattnera45664f2008-11-10 02:56:27 +0000756 };
Devang Patele4b27562009-08-28 23:24:31 +0000757 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000758}
759
Devang Patelac16d442009-10-26 16:54:35 +0000760
761/// CreateDerivedType - Create a derived type like const qualified type,
762/// pointer, typedef, etc.
763DIDerivedType DIFactory::CreateDerivedTypeEx(unsigned Tag,
764 DIDescriptor Context,
765 StringRef Name,
766 DICompileUnit CompileUnit,
767 unsigned LineNumber,
768 Constant *SizeInBits,
769 Constant *AlignInBits,
770 Constant *OffsetInBits,
771 unsigned Flags,
772 DIType DerivedFrom) {
773 Value *Elts[] = {
774 GetTagConstant(Tag),
775 Context.getNode(),
776 MDString::get(VMContext, Name),
777 CompileUnit.getNode(),
778 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
779 SizeInBits,
780 AlignInBits,
781 OffsetInBits,
782 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
783 DerivedFrom.getNode(),
784 };
785 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
786}
787
788
Chris Lattnera45664f2008-11-10 02:56:27 +0000789/// CreateCompositeType - Create a composite type like array, struct, etc.
790DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
791 DIDescriptor Context,
Devang Patel5ccdd102009-09-29 18:40:58 +0000792 StringRef Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000793 DICompileUnit CompileUnit,
794 unsigned LineNumber,
795 uint64_t SizeInBits,
796 uint64_t AlignInBits,
797 uint64_t OffsetInBits,
798 unsigned Flags,
799 DIType DerivedFrom,
Devang Patel13319ce2009-02-17 22:43:44 +0000800 DIArray Elements,
801 unsigned RuntimeLang) {
Owen Andersone277fed2009-07-07 16:31:25 +0000802
Devang Patele4b27562009-08-28 23:24:31 +0000803 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000804 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000805 Context.getNode(),
806 MDString::get(VMContext, Name),
807 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000808 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
809 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
810 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
811 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
812 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patele4b27562009-08-28 23:24:31 +0000813 DerivedFrom.getNode(),
814 Elements.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000815 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
Chris Lattnera45664f2008-11-10 02:56:27 +0000816 };
Devang Patele4b27562009-08-28 23:24:31 +0000817 return DICompositeType(MDNode::get(VMContext, &Elts[0], 12));
Chris Lattnera45664f2008-11-10 02:56:27 +0000818}
819
820
Devang Patelac16d442009-10-26 16:54:35 +0000821/// CreateCompositeType - Create a composite type like array, struct, etc.
822DICompositeType DIFactory::CreateCompositeTypeEx(unsigned Tag,
823 DIDescriptor Context,
824 StringRef Name,
825 DICompileUnit CompileUnit,
826 unsigned LineNumber,
827 Constant *SizeInBits,
828 Constant *AlignInBits,
829 Constant *OffsetInBits,
830 unsigned Flags,
831 DIType DerivedFrom,
832 DIArray Elements,
833 unsigned RuntimeLang) {
834
835 Value *Elts[] = {
836 GetTagConstant(Tag),
837 Context.getNode(),
838 MDString::get(VMContext, Name),
839 CompileUnit.getNode(),
840 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
841 SizeInBits,
842 AlignInBits,
843 OffsetInBits,
844 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
845 DerivedFrom.getNode(),
846 Elements.getNode(),
847 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
848 };
849 return DICompositeType(MDNode::get(VMContext, &Elts[0], 12));
850}
851
852
Chris Lattnera45664f2008-11-10 02:56:27 +0000853/// CreateSubprogram - Create a new descriptor for the specified subprogram.
854/// See comments in DISubprogram for descriptions of these fields. This
855/// method does not unique the generated descriptors.
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000856DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
Devang Patel5ccdd102009-09-29 18:40:58 +0000857 StringRef Name,
858 StringRef DisplayName,
859 StringRef LinkageName,
Chris Lattnera45664f2008-11-10 02:56:27 +0000860 DICompileUnit CompileUnit,
861 unsigned LineNo, DIType Type,
862 bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000863 bool isDefinition) {
Devang Patel854967e2008-12-17 22:39:29 +0000864
Devang Patele4b27562009-08-28 23:24:31 +0000865 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000866 GetTagConstant(dwarf::DW_TAG_subprogram),
Devang Patele4b27562009-08-28 23:24:31 +0000867 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
868 Context.getNode(),
869 MDString::get(VMContext, Name),
870 MDString::get(VMContext, DisplayName),
871 MDString::get(VMContext, LinkageName),
872 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000873 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000874 Type.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000875 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
876 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition)
Chris Lattnera45664f2008-11-10 02:56:27 +0000877 };
Devang Patele4b27562009-08-28 23:24:31 +0000878 return DISubprogram(MDNode::get(VMContext, &Elts[0], 11));
Chris Lattnera45664f2008-11-10 02:56:27 +0000879}
880
881/// CreateGlobalVariable - Create a new descriptor for the specified global.
882DIGlobalVariable
Devang Patel5ccdd102009-09-29 18:40:58 +0000883DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
884 StringRef DisplayName,
885 StringRef LinkageName,
Chris Lattnera45664f2008-11-10 02:56:27 +0000886 DICompileUnit CompileUnit,
887 unsigned LineNo, DIType Type,bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000888 bool isDefinition, llvm::GlobalVariable *Val) {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000889 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000890 GetTagConstant(dwarf::DW_TAG_variable),
Devang Patele4b27562009-08-28 23:24:31 +0000891 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
892 Context.getNode(),
893 MDString::get(VMContext, Name),
894 MDString::get(VMContext, DisplayName),
895 MDString::get(VMContext, LinkageName),
896 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000897 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000898 Type.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000899 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
900 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
Devang Patele4b27562009-08-28 23:24:31 +0000901 Val
Chris Lattnera45664f2008-11-10 02:56:27 +0000902 };
Devang Patele4b27562009-08-28 23:24:31 +0000903
904 Value *const *Vs = &Elts[0];
905 MDNode *Node = MDNode::get(VMContext,Vs, 12);
906
907 // Create a named metadata so that we do not lose this mdnode.
908 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
909 NMD->addElement(Node);
910
911 return DIGlobalVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +0000912}
913
914
915/// CreateVariable - Create a new descriptor for the specified variable.
916DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
Devang Patel5ccdd102009-09-29 18:40:58 +0000917 StringRef Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000918 DICompileUnit CompileUnit, unsigned LineNo,
Devang Pateldd9db662009-01-30 18:20:31 +0000919 DIType Type) {
Devang Patele4b27562009-08-28 23:24:31 +0000920 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000921 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000922 Context.getNode(),
923 MDString::get(VMContext, Name),
924 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000925 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000926 Type.getNode(),
Chris Lattnera45664f2008-11-10 02:56:27 +0000927 };
Devang Patele4b27562009-08-28 23:24:31 +0000928 return DIVariable(MDNode::get(VMContext, &Elts[0], 6));
Chris Lattnera45664f2008-11-10 02:56:27 +0000929}
930
931
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000932/// CreateComplexVariable - Create a new descriptor for the specified variable
933/// which has a complex address expression for its address.
934DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
935 const std::string &Name,
936 DICompileUnit CompileUnit,
937 unsigned LineNo,
938 DIType Type, SmallVector<Value *, 9> &addr) {
939 SmallVector<Value *, 9> Elts;
940 Elts.push_back(GetTagConstant(Tag));
941 Elts.push_back(Context.getNode());
942 Elts.push_back(MDString::get(VMContext, Name));
943 Elts.push_back(CompileUnit.getNode());
944 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
945 Elts.push_back(Type.getNode());
946 Elts.insert(Elts.end(), addr.begin(), addr.end());
947
948 return DIVariable(MDNode::get(VMContext, &Elts[0], 6+addr.size()));
949}
950
951
Chris Lattnera45664f2008-11-10 02:56:27 +0000952/// CreateBlock - This creates a descriptor for a lexical block with the
Owen Anderson99035272009-07-07 17:12:53 +0000953/// specified parent VMContext.
Devang Patel5e005d82009-08-31 22:00:15 +0000954DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context) {
Devang Patele4b27562009-08-28 23:24:31 +0000955 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000956 GetTagConstant(dwarf::DW_TAG_lexical_block),
Devang Patele4b27562009-08-28 23:24:31 +0000957 Context.getNode()
Chris Lattnera45664f2008-11-10 02:56:27 +0000958 };
Devang Patel5e005d82009-08-31 22:00:15 +0000959 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 2));
Chris Lattnera45664f2008-11-10 02:56:27 +0000960}
961
Devang Patelf98d8fe2009-09-01 01:14:15 +0000962/// CreateLocation - Creates a debug info location.
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000963DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
Daniel Dunbara279bc32009-09-20 02:20:51 +0000964 DIScope S, DILocation OrigLoc) {
Devang Patelf98d8fe2009-09-01 01:14:15 +0000965 Value *Elts[] = {
966 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
967 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
968 S.getNode(),
969 OrigLoc.getNode(),
970 };
971 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
972}
973
Chris Lattnera45664f2008-11-10 02:56:27 +0000974
975//===----------------------------------------------------------------------===//
976// DIFactory: Routines for inserting code into a function
977//===----------------------------------------------------------------------===//
978
979/// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
980/// inserting it at the end of the specified basic block.
981void DIFactory::InsertStopPoint(DICompileUnit CU, unsigned LineNo,
982 unsigned ColNo, BasicBlock *BB) {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000983
Chris Lattnera45664f2008-11-10 02:56:27 +0000984 // Lazily construct llvm.dbg.stoppoint function.
985 if (!StopPointFn)
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000986 StopPointFn = llvm::Intrinsic::getDeclaration(&M,
Chris Lattnera45664f2008-11-10 02:56:27 +0000987 llvm::Intrinsic::dbg_stoppoint);
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000988
Chris Lattnera45664f2008-11-10 02:56:27 +0000989 // Invoke llvm.dbg.stoppoint
990 Value *Args[] = {
Owen Anderson1d0be152009-08-13 21:58:54 +0000991 ConstantInt::get(llvm::Type::getInt32Ty(VMContext), LineNo),
992 ConstantInt::get(llvm::Type::getInt32Ty(VMContext), ColNo),
Devang Patele4b27562009-08-28 23:24:31 +0000993 CU.getNode()
Chris Lattnera45664f2008-11-10 02:56:27 +0000994 };
995 CallInst::Create(StopPointFn, Args, Args+3, "", BB);
996}
997
998/// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
999/// mark the start of the specified subprogram.
1000void DIFactory::InsertSubprogramStart(DISubprogram SP, BasicBlock *BB) {
1001 // Lazily construct llvm.dbg.func.start.
1002 if (!FuncStartFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +00001003 FuncStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_func_start);
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001004
Chris Lattnera45664f2008-11-10 02:56:27 +00001005 // Call llvm.dbg.func.start which also implicitly sets a stoppoint.
Devang Patele4b27562009-08-28 23:24:31 +00001006 CallInst::Create(FuncStartFn, SP.getNode(), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +00001007}
1008
1009/// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
1010/// mark the start of a region for the specified scoping descriptor.
1011void DIFactory::InsertRegionStart(DIDescriptor D, BasicBlock *BB) {
1012 // Lazily construct llvm.dbg.region.start function.
1013 if (!RegionStartFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +00001014 RegionStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_start);
1015
Chris Lattnera45664f2008-11-10 02:56:27 +00001016 // Call llvm.dbg.func.start.
Devang Patele4b27562009-08-28 23:24:31 +00001017 CallInst::Create(RegionStartFn, D.getNode(), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +00001018}
1019
Chris Lattnera45664f2008-11-10 02:56:27 +00001020/// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
1021/// mark the end of a region for the specified scoping descriptor.
1022void DIFactory::InsertRegionEnd(DIDescriptor D, BasicBlock *BB) {
1023 // Lazily construct llvm.dbg.region.end function.
1024 if (!RegionEndFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +00001025 RegionEndFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_end);
1026
1027 // Call llvm.dbg.region.end.
Devang Patele4b27562009-08-28 23:24:31 +00001028 CallInst::Create(RegionEndFn, D.getNode(), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +00001029}
1030
1031/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Mike Stumpe4250392009-10-01 22:08:58 +00001032void DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1033 Instruction *InsertBefore) {
Chris Lattnera45664f2008-11-10 02:56:27 +00001034 // Cast the storage to a {}* for the call to llvm.dbg.declare.
Mike Stumpe4250392009-10-01 22:08:58 +00001035 Storage = new BitCastInst(Storage, EmptyStructPtr, "", InsertBefore);
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001036
Chris Lattnera45664f2008-11-10 02:56:27 +00001037 if (!DeclareFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +00001038 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1039
Devang Patele4b27562009-08-28 23:24:31 +00001040 Value *Args[] = { Storage, D.getNode() };
Mike Stumpe4250392009-10-01 22:08:58 +00001041 CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
1042}
1043
1044/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1045void DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1046 BasicBlock *InsertAtEnd) {
1047 // Cast the storage to a {}* for the call to llvm.dbg.declare.
1048 Storage = new BitCastInst(Storage, EmptyStructPtr, "", InsertAtEnd);
1049
1050 if (!DeclareFn)
1051 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1052
1053 Value *Args[] = { Storage, D.getNode() };
1054 CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);
Chris Lattnera45664f2008-11-10 02:56:27 +00001055}
Torok Edwin620f2802008-12-16 09:07:36 +00001056
Devang Patele4b27562009-08-28 23:24:31 +00001057
Devang Pateld2f79a12009-07-28 19:55:13 +00001058//===----------------------------------------------------------------------===//
Devang Patel98c65172009-07-30 18:25:15 +00001059// DebugInfoFinder implementations.
Devang Pateld2f79a12009-07-28 19:55:13 +00001060//===----------------------------------------------------------------------===//
1061
Devang Patel98c65172009-07-30 18:25:15 +00001062/// processModule - Process entire module and collect debug info.
1063void DebugInfoFinder::processModule(Module &M) {
Devang Patele4b27562009-08-28 23:24:31 +00001064
Devang Patelbeab41b2009-10-07 22:04:08 +00001065#ifdef ATTACH_DEBUG_INFO_TO_AN_INSN
1066 MetadataContext &TheMetadata = M.getContext().getMetadata();
1067 unsigned MDDbgKind = TheMetadata.getMDKind("dbg");
Devang Patelbeab41b2009-10-07 22:04:08 +00001068#endif
Devang Pateld2f79a12009-07-28 19:55:13 +00001069 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1070 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
1071 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
1072 ++BI) {
1073 if (DbgStopPointInst *SPI = dyn_cast<DbgStopPointInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +00001074 processStopPoint(SPI);
Devang Pateld2f79a12009-07-28 19:55:13 +00001075 else if (DbgFuncStartInst *FSI = dyn_cast<DbgFuncStartInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +00001076 processFuncStart(FSI);
Devang Patele802f1c2009-07-30 17:30:23 +00001077 else if (DbgRegionStartInst *DRS = dyn_cast<DbgRegionStartInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +00001078 processRegionStart(DRS);
Devang Patele802f1c2009-07-30 17:30:23 +00001079 else if (DbgRegionEndInst *DRE = dyn_cast<DbgRegionEndInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +00001080 processRegionEnd(DRE);
Devang Patelb4d31302009-07-31 18:18:52 +00001081 else if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1082 processDeclare(DDI);
Devang Patelbeab41b2009-10-07 22:04:08 +00001083#ifdef ATTACH_DEBUG_INFO_TO_AN_INSN
Devang Patel549e81f2009-10-13 17:35:35 +00001084 else if (MDDbgKind) {
1085 if (MDNode *L = TheMetadata.getMD(MDDbgKind, BI)) {
1086 DILocation Loc(L);
1087 DIScope S(Loc.getScope().getNode());
1088 if (S.isCompileUnit())
1089 addCompileUnit(DICompileUnit(S.getNode()));
1090 else if (S.isSubprogram())
1091 processSubprogram(DISubprogram(S.getNode()));
1092 else if (S.isLexicalBlock())
1093 processLexicalBlock(DILexicalBlock(S.getNode()));
1094 }
Devang Patelbeab41b2009-10-07 22:04:08 +00001095 }
1096#endif
Devang Pateld2f79a12009-07-28 19:55:13 +00001097 }
Devang Patele4b27562009-08-28 23:24:31 +00001098
1099 NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv");
1100 if (!NMD)
1101 return;
1102
1103 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
1104 DIGlobalVariable DIG(cast<MDNode>(NMD->getElement(i)));
Devang Pateld2f79a12009-07-28 19:55:13 +00001105 if (addGlobalVariable(DIG)) {
1106 addCompileUnit(DIG.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +00001107 processType(DIG.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +00001108 }
1109 }
1110}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001111
Devang Patel98c65172009-07-30 18:25:15 +00001112/// processType - Process DIType.
1113void DebugInfoFinder::processType(DIType DT) {
Devang Patel72bcdb62009-08-10 22:09:58 +00001114 if (!addType(DT))
Devang Pateld2f79a12009-07-28 19:55:13 +00001115 return;
1116
1117 addCompileUnit(DT.getCompileUnit());
Devang Patel6ceea332009-08-31 18:49:10 +00001118 if (DT.isCompositeType()) {
Devang Patele4b27562009-08-28 23:24:31 +00001119 DICompositeType DCT(DT.getNode());
Devang Patel98c65172009-07-30 18:25:15 +00001120 processType(DCT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001121 DIArray DA = DCT.getTypeArray();
1122 if (!DA.isNull())
1123 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1124 DIDescriptor D = DA.getElement(i);
Devang Patele4b27562009-08-28 23:24:31 +00001125 DIType TypeE = DIType(D.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001126 if (!TypeE.isNull())
Devang Patel98c65172009-07-30 18:25:15 +00001127 processType(TypeE);
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001128 else
Devang Patele4b27562009-08-28 23:24:31 +00001129 processSubprogram(DISubprogram(D.getNode()));
Devang Pateld2f79a12009-07-28 19:55:13 +00001130 }
Devang Patel6ceea332009-08-31 18:49:10 +00001131 } else if (DT.isDerivedType()) {
Devang Patele4b27562009-08-28 23:24:31 +00001132 DIDerivedType DDT(DT.getNode());
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001133 if (!DDT.isNull())
Devang Patel98c65172009-07-30 18:25:15 +00001134 processType(DDT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001135 }
1136}
1137
Devang Patelbeab41b2009-10-07 22:04:08 +00001138/// processLexicalBlock
1139void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
1140 if (LB.isNull())
1141 return;
1142 DIScope Context = LB.getContext();
1143 if (Context.isLexicalBlock())
1144 return processLexicalBlock(DILexicalBlock(Context.getNode()));
1145 else
1146 return processSubprogram(DISubprogram(Context.getNode()));
1147}
1148
Devang Patel98c65172009-07-30 18:25:15 +00001149/// processSubprogram - Process DISubprogram.
1150void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Patele802f1c2009-07-30 17:30:23 +00001151 if (SP.isNull())
1152 return;
Devang Pateld2f79a12009-07-28 19:55:13 +00001153 if (!addSubprogram(SP))
1154 return;
1155 addCompileUnit(SP.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +00001156 processType(SP.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +00001157}
1158
Devang Patel98c65172009-07-30 18:25:15 +00001159/// processStopPoint - Process DbgStopPointInst.
1160void DebugInfoFinder::processStopPoint(DbgStopPointInst *SPI) {
Devang Patele4b27562009-08-28 23:24:31 +00001161 MDNode *Context = dyn_cast<MDNode>(SPI->getContext());
Devang Pateld2f79a12009-07-28 19:55:13 +00001162 addCompileUnit(DICompileUnit(Context));
1163}
1164
Devang Patel98c65172009-07-30 18:25:15 +00001165/// processFuncStart - Process DbgFuncStartInst.
1166void DebugInfoFinder::processFuncStart(DbgFuncStartInst *FSI) {
Devang Patele4b27562009-08-28 23:24:31 +00001167 MDNode *SP = dyn_cast<MDNode>(FSI->getSubprogram());
Devang Patel98c65172009-07-30 18:25:15 +00001168 processSubprogram(DISubprogram(SP));
Devang Pateld2f79a12009-07-28 19:55:13 +00001169}
1170
Devang Patel98c65172009-07-30 18:25:15 +00001171/// processRegionStart - Process DbgRegionStart.
1172void DebugInfoFinder::processRegionStart(DbgRegionStartInst *DRS) {
Devang Patele4b27562009-08-28 23:24:31 +00001173 MDNode *SP = dyn_cast<MDNode>(DRS->getContext());
Devang Patel98c65172009-07-30 18:25:15 +00001174 processSubprogram(DISubprogram(SP));
Devang Patele802f1c2009-07-30 17:30:23 +00001175}
1176
Devang Patel98c65172009-07-30 18:25:15 +00001177/// processRegionEnd - Process DbgRegionEnd.
1178void DebugInfoFinder::processRegionEnd(DbgRegionEndInst *DRE) {
Devang Patele4b27562009-08-28 23:24:31 +00001179 MDNode *SP = dyn_cast<MDNode>(DRE->getContext());
Devang Patel98c65172009-07-30 18:25:15 +00001180 processSubprogram(DISubprogram(SP));
Devang Patele802f1c2009-07-30 17:30:23 +00001181}
1182
Devang Patelb4d31302009-07-31 18:18:52 +00001183/// processDeclare - Process DbgDeclareInst.
1184void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
Devang Patele4b27562009-08-28 23:24:31 +00001185 DIVariable DV(cast<MDNode>(DDI->getVariable()));
Devang Patelb4d31302009-07-31 18:18:52 +00001186 if (DV.isNull())
1187 return;
1188
Devang Patele4b27562009-08-28 23:24:31 +00001189 if (!NodesSeen.insert(DV.getNode()))
Devang Patelb4d31302009-07-31 18:18:52 +00001190 return;
1191
1192 addCompileUnit(DV.getCompileUnit());
1193 processType(DV.getType());
1194}
1195
Devang Patel72bcdb62009-08-10 22:09:58 +00001196/// addType - Add type into Tys.
1197bool DebugInfoFinder::addType(DIType DT) {
1198 if (DT.isNull())
1199 return false;
1200
Devang Patele4b27562009-08-28 23:24:31 +00001201 if (!NodesSeen.insert(DT.getNode()))
Devang Patel72bcdb62009-08-10 22:09:58 +00001202 return false;
1203
Devang Patele4b27562009-08-28 23:24:31 +00001204 TYs.push_back(DT.getNode());
Devang Patel72bcdb62009-08-10 22:09:58 +00001205 return true;
1206}
1207
Devang Pateld2f79a12009-07-28 19:55:13 +00001208/// addCompileUnit - Add compile unit into CUs.
Devang Patel98c65172009-07-30 18:25:15 +00001209bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001210 if (CU.isNull())
1211 return false;
1212
Devang Patele4b27562009-08-28 23:24:31 +00001213 if (!NodesSeen.insert(CU.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001214 return false;
1215
Devang Patele4b27562009-08-28 23:24:31 +00001216 CUs.push_back(CU.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001217 return true;
1218}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001219
Devang Pateld2f79a12009-07-28 19:55:13 +00001220/// addGlobalVariable - Add global variable into GVs.
Devang Patel98c65172009-07-30 18:25:15 +00001221bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001222 if (DIG.isNull())
1223 return false;
1224
Devang Patele4b27562009-08-28 23:24:31 +00001225 if (!NodesSeen.insert(DIG.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001226 return false;
1227
Devang Patele4b27562009-08-28 23:24:31 +00001228 GVs.push_back(DIG.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001229 return true;
1230}
1231
1232// addSubprogram - Add subprgoram into SPs.
Devang Patel98c65172009-07-30 18:25:15 +00001233bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001234 if (SP.isNull())
1235 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001236
Devang Patele4b27562009-08-28 23:24:31 +00001237 if (!NodesSeen.insert(SP.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001238 return false;
1239
Devang Patele4b27562009-08-28 23:24:31 +00001240 SPs.push_back(SP.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001241 return true;
1242}
1243
Torok Edwin620f2802008-12-16 09:07:36 +00001244namespace llvm {
Bill Wendlingdc817b62009-05-14 18:26:15 +00001245 /// findStopPoint - Find the stoppoint coressponding to this instruction, that
1246 /// is the stoppoint that dominates this instruction.
1247 const DbgStopPointInst *findStopPoint(const Instruction *Inst) {
Torok Edwin620f2802008-12-16 09:07:36 +00001248 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(Inst))
1249 return DSI;
1250
1251 const BasicBlock *BB = Inst->getParent();
1252 BasicBlock::const_iterator I = Inst, B;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001253 while (BB) {
Torok Edwin620f2802008-12-16 09:07:36 +00001254 B = BB->begin();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001255
Torok Edwin620f2802008-12-16 09:07:36 +00001256 // A BB consisting only of a terminator can't have a stoppoint.
Bill Wendlingdc817b62009-05-14 18:26:15 +00001257 while (I != B) {
1258 --I;
1259 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
1260 return DSI;
Torok Edwin620f2802008-12-16 09:07:36 +00001261 }
Bill Wendlingdc817b62009-05-14 18:26:15 +00001262
1263 // This BB didn't have a stoppoint: if there is only one predecessor, look
1264 // for a stoppoint there. We could use getIDom(), but that would require
1265 // dominator info.
Torok Edwin620f2802008-12-16 09:07:36 +00001266 BB = I->getParent()->getUniquePredecessor();
1267 if (BB)
1268 I = BB->getTerminator();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001269 }
1270
Torok Edwin620f2802008-12-16 09:07:36 +00001271 return 0;
1272 }
1273
Bill Wendlingdc817b62009-05-14 18:26:15 +00001274 /// findBBStopPoint - Find the stoppoint corresponding to first real
1275 /// (non-debug intrinsic) instruction in this Basic Block, and return the
1276 /// stoppoint for it.
1277 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB) {
1278 for(BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001279 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
1280 return DSI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001281
1282 // Fallback to looking for stoppoint of unique predecessor. Useful if this
1283 // BB contains no stoppoints, but unique predecessor does.
Torok Edwin620f2802008-12-16 09:07:36 +00001284 BB = BB->getUniquePredecessor();
1285 if (BB)
1286 return findStopPoint(BB->getTerminator());
Bill Wendlingdc817b62009-05-14 18:26:15 +00001287
Torok Edwin620f2802008-12-16 09:07:36 +00001288 return 0;
1289 }
1290
Bill Wendlingdc817b62009-05-14 18:26:15 +00001291 Value *findDbgGlobalDeclare(GlobalVariable *V) {
Torok Edwinff7d0e92009-03-10 13:41:26 +00001292 const Module *M = V->getParent();
Devang Patele4b27562009-08-28 23:24:31 +00001293 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1294 if (!NMD)
1295 return 0;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001296
Devang Patele4b27562009-08-28 23:24:31 +00001297 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
1298 DIGlobalVariable DIG(cast_or_null<MDNode>(NMD->getElement(i)));
1299 if (DIG.isNull())
1300 continue;
1301 if (DIG.getGlobal() == V)
1302 return DIG.getNode();
Torok Edwinff7d0e92009-03-10 13:41:26 +00001303 }
Torok Edwinff7d0e92009-03-10 13:41:26 +00001304 return 0;
1305 }
1306
Bill Wendlingdc817b62009-05-14 18:26:15 +00001307 /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
Torok Edwin620f2802008-12-16 09:07:36 +00001308 /// It looks through pointer casts too.
Bill Wendlingdc817b62009-05-14 18:26:15 +00001309 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts) {
Torok Edwin620f2802008-12-16 09:07:36 +00001310 if (stripCasts) {
1311 V = V->stripPointerCasts();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001312
Torok Edwin620f2802008-12-16 09:07:36 +00001313 // Look for the bitcast.
1314 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001315 I != E; ++I)
Devang Patel94dfaec2009-10-29 18:20:34 +00001316 if (isa<BitCastInst>(I)) {
1317 const DbgDeclareInst *DDI = findDbgDeclare(*I, false);
1318 if (DDI) return DDI;
1319 }
Torok Edwin620f2802008-12-16 09:07:36 +00001320 return 0;
1321 }
1322
Bill Wendlingdc817b62009-05-14 18:26:15 +00001323 // Find llvm.dbg.declare among uses of the instruction.
Torok Edwin620f2802008-12-16 09:07:36 +00001324 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001325 I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001326 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I))
1327 return DDI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001328
Torok Edwin620f2802008-12-16 09:07:36 +00001329 return 0;
1330 }
Torok Edwinff7d0e92009-03-10 13:41:26 +00001331
Devang Patel5ccdd102009-09-29 18:40:58 +00001332bool getLocationInfo(const Value *V, std::string &DisplayName,
1333 std::string &Type, unsigned &LineNo, std::string &File,
Bill Wendlingdc817b62009-05-14 18:26:15 +00001334 std::string &Dir) {
Torok Edwinff7d0e92009-03-10 13:41:26 +00001335 DICompileUnit Unit;
1336 DIType TypeD;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001337
Torok Edwinff7d0e92009-03-10 13:41:26 +00001338 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1339 Value *DIGV = findDbgGlobalDeclare(GV);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001340 if (!DIGV) return false;
Devang Patele4b27562009-08-28 23:24:31 +00001341 DIGlobalVariable Var(cast<MDNode>(DIGV));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001342
Devang Patel5ccdd102009-09-29 18:40:58 +00001343 if (const char *D = Var.getDisplayName())
1344 DisplayName = D;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001345 LineNo = Var.getLineNumber();
1346 Unit = Var.getCompileUnit();
1347 TypeD = Var.getType();
1348 } else {
1349 const DbgDeclareInst *DDI = findDbgDeclare(V);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001350 if (!DDI) return false;
Devang Patele4b27562009-08-28 23:24:31 +00001351 DIVariable Var(cast<MDNode>(DDI->getVariable()));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001352
Devang Patel5ccdd102009-09-29 18:40:58 +00001353 if (const char *D = Var.getName())
1354 DisplayName = D;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001355 LineNo = Var.getLineNumber();
1356 Unit = Var.getCompileUnit();
1357 TypeD = Var.getType();
1358 }
Bill Wendlingdc817b62009-05-14 18:26:15 +00001359
Devang Patel5ccdd102009-09-29 18:40:58 +00001360 if (const char *T = TypeD.getName())
1361 Type = T;
1362 if (const char *F = Unit.getFilename())
1363 File = F;
1364 if (const char *D = Unit.getDirectory())
1365 Dir = D;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001366 return true;
1367 }
Devang Patel13e16b62009-06-26 01:49:18 +00001368
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001369 /// isValidDebugInfoIntrinsic - Return true if SPI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001370 /// info intrinsic.
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001371 bool isValidDebugInfoIntrinsic(DbgStopPointInst &SPI,
Devang Patel9e529c32009-07-02 01:15:24 +00001372 CodeGenOpt::Level OptLev) {
1373 return DIDescriptor::ValidDebugInfo(SPI.getContext(), OptLev);
1374 }
1375
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001376 /// isValidDebugInfoIntrinsic - Return true if FSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001377 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001378 bool isValidDebugInfoIntrinsic(DbgFuncStartInst &FSI,
1379 CodeGenOpt::Level OptLev) {
1380 return DIDescriptor::ValidDebugInfo(FSI.getSubprogram(), OptLev);
1381 }
1382
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001383 /// isValidDebugInfoIntrinsic - Return true if RSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001384 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001385 bool isValidDebugInfoIntrinsic(DbgRegionStartInst &RSI,
1386 CodeGenOpt::Level OptLev) {
1387 return DIDescriptor::ValidDebugInfo(RSI.getContext(), OptLev);
1388 }
1389
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001390 /// isValidDebugInfoIntrinsic - Return true if REI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001391 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001392 bool isValidDebugInfoIntrinsic(DbgRegionEndInst &REI,
1393 CodeGenOpt::Level OptLev) {
1394 return DIDescriptor::ValidDebugInfo(REI.getContext(), OptLev);
1395 }
1396
1397
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001398 /// isValidDebugInfoIntrinsic - Return true if DI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001399 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001400 bool isValidDebugInfoIntrinsic(DbgDeclareInst &DI,
1401 CodeGenOpt::Level OptLev) {
1402 return DIDescriptor::ValidDebugInfo(DI.getVariable(), OptLev);
1403 }
1404
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001405 /// ExtractDebugLocation - Extract debug location information
Devang Patel9e529c32009-07-02 01:15:24 +00001406 /// from llvm.dbg.stoppoint intrinsic.
1407 DebugLoc ExtractDebugLocation(DbgStopPointInst &SPI,
Devang Patel9e529c32009-07-02 01:15:24 +00001408 DebugLocTracker &DebugLocInfo) {
1409 DebugLoc DL;
1410 Value *Context = SPI.getContext();
Devang Patel9e529c32009-07-02 01:15:24 +00001411
1412 // If this location is already tracked then use it.
Devang Patela1434042009-10-01 01:15:28 +00001413 DebugLocTuple Tuple(cast<MDNode>(Context), NULL, SPI.getLine(),
Devang Patel9e529c32009-07-02 01:15:24 +00001414 SPI.getColumn());
1415 DenseMap<DebugLocTuple, unsigned>::iterator II
1416 = DebugLocInfo.DebugIdMap.find(Tuple);
1417 if (II != DebugLocInfo.DebugIdMap.end())
1418 return DebugLoc::get(II->second);
1419
1420 // Add a new location entry.
1421 unsigned Id = DebugLocInfo.DebugLocations.size();
1422 DebugLocInfo.DebugLocations.push_back(Tuple);
1423 DebugLocInfo.DebugIdMap[Tuple] = Id;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001424
Devang Patel9e529c32009-07-02 01:15:24 +00001425 return DebugLoc::get(Id);
1426 }
1427
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001428 /// ExtractDebugLocation - Extract debug location information
Devang Patel1b75f442009-09-16 18:20:05 +00001429 /// from DILocation.
1430 DebugLoc ExtractDebugLocation(DILocation &Loc,
1431 DebugLocTracker &DebugLocInfo) {
1432 DebugLoc DL;
1433 MDNode *Context = Loc.getScope().getNode();
Devang Patela1434042009-10-01 01:15:28 +00001434 MDNode *InlinedLoc = NULL;
1435 if (!Loc.getOrigLocation().isNull())
1436 InlinedLoc = Loc.getOrigLocation().getNode();
Devang Patel1b75f442009-09-16 18:20:05 +00001437 // If this location is already tracked then use it.
Devang Patela1434042009-10-01 01:15:28 +00001438 DebugLocTuple Tuple(Context, InlinedLoc, Loc.getLineNumber(),
Daniel Dunbara279bc32009-09-20 02:20:51 +00001439 Loc.getColumnNumber());
Devang Patel1b75f442009-09-16 18:20:05 +00001440 DenseMap<DebugLocTuple, unsigned>::iterator II
1441 = DebugLocInfo.DebugIdMap.find(Tuple);
1442 if (II != DebugLocInfo.DebugIdMap.end())
1443 return DebugLoc::get(II->second);
1444
1445 // Add a new location entry.
1446 unsigned Id = DebugLocInfo.DebugLocations.size();
1447 DebugLocInfo.DebugLocations.push_back(Tuple);
1448 DebugLocInfo.DebugIdMap[Tuple] = Id;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001449
Devang Patel1b75f442009-09-16 18:20:05 +00001450 return DebugLoc::get(Id);
1451 }
1452
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001453 /// ExtractDebugLocation - Extract debug location information
Devang Patel9e529c32009-07-02 01:15:24 +00001454 /// from llvm.dbg.func_start intrinsic.
1455 DebugLoc ExtractDebugLocation(DbgFuncStartInst &FSI,
Devang Patel9e529c32009-07-02 01:15:24 +00001456 DebugLocTracker &DebugLocInfo) {
1457 DebugLoc DL;
1458 Value *SP = FSI.getSubprogram();
Devang Patel9e529c32009-07-02 01:15:24 +00001459
Devang Patele4b27562009-08-28 23:24:31 +00001460 DISubprogram Subprogram(cast<MDNode>(SP));
Devang Patel9e529c32009-07-02 01:15:24 +00001461 unsigned Line = Subprogram.getLineNumber();
1462 DICompileUnit CU(Subprogram.getCompileUnit());
1463
1464 // If this location is already tracked then use it.
Devang Patela1434042009-10-01 01:15:28 +00001465 DebugLocTuple Tuple(CU.getNode(), NULL, Line, /* Column */ 0);
Devang Patel9e529c32009-07-02 01:15:24 +00001466 DenseMap<DebugLocTuple, unsigned>::iterator II
1467 = DebugLocInfo.DebugIdMap.find(Tuple);
1468 if (II != DebugLocInfo.DebugIdMap.end())
1469 return DebugLoc::get(II->second);
1470
1471 // Add a new location entry.
1472 unsigned Id = DebugLocInfo.DebugLocations.size();
1473 DebugLocInfo.DebugLocations.push_back(Tuple);
1474 DebugLocInfo.DebugIdMap[Tuple] = Id;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001475
Devang Patel9e529c32009-07-02 01:15:24 +00001476 return DebugLoc::get(Id);
1477 }
1478
1479 /// isInlinedFnStart - Return true if FSI is starting an inlined function.
1480 bool isInlinedFnStart(DbgFuncStartInst &FSI, const Function *CurrentFn) {
Devang Patele4b27562009-08-28 23:24:31 +00001481 DISubprogram Subprogram(cast<MDNode>(FSI.getSubprogram()));
Devang Patel9e529c32009-07-02 01:15:24 +00001482 if (Subprogram.describes(CurrentFn))
1483 return false;
1484
1485 return true;
1486 }
1487
1488 /// isInlinedFnEnd - Return true if REI is ending an inlined function.
1489 bool isInlinedFnEnd(DbgRegionEndInst &REI, const Function *CurrentFn) {
Devang Patele4b27562009-08-28 23:24:31 +00001490 DISubprogram Subprogram(cast<MDNode>(REI.getContext()));
Devang Patel9e529c32009-07-02 01:15:24 +00001491 if (Subprogram.isNull() || Subprogram.describes(CurrentFn))
1492 return false;
1493
1494 return true;
1495 }
Torok Edwin620f2802008-12-16 09:07:36 +00001496}