blob: 7bff11ec5b459a94dec8bb96b31ad5285f3277af [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 Patel36375ee2009-02-17 21:23:59 +0000404 DIType BT = getTypeDerivedFrom();
Devang Patel7cb7e122009-10-30 22:09:30 +0000405 if (!BT.isNull() && BT.isDerivedType())
406 return DIDerivedType(BT.getNode()).getOriginalTypeSize();
407 if (BT.isNull())
Devang Patel36375ee2009-02-17 21:23:59 +0000408 return getSizeInBits();
409 return BT.getSizeInBits();
410}
Devang Patelb79b5352009-01-19 23:21:49 +0000411
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000412/// describes - Return true if this subprogram provides debugging
413/// information for the function F.
414bool DISubprogram::describes(const Function *F) {
415 assert (F && "Invalid function");
Devang Patel5ccdd102009-09-29 18:40:58 +0000416 const char *Name = getLinkageName();
417 if (!Name)
418 Name = getName();
419 if (strcmp(F->getName().data(), Name) == 0)
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000420 return true;
421 return false;
422}
423
Devang Patelecbeb1a2009-09-30 22:34:41 +0000424const char *DIScope::getFilename() const {
425 if (isLexicalBlock())
426 return DILexicalBlock(DbgNode).getFilename();
427 else if (isSubprogram())
428 return DISubprogram(DbgNode).getFilename();
429 else if (isCompileUnit())
430 return DICompileUnit(DbgNode).getFilename();
431 else
432 assert (0 && "Invalid DIScope!");
433 return NULL;
434}
435
436const char *DIScope::getDirectory() const {
437 if (isLexicalBlock())
438 return DILexicalBlock(DbgNode).getDirectory();
439 else if (isSubprogram())
440 return DISubprogram(DbgNode).getDirectory();
441 else if (isCompileUnit())
442 return DICompileUnit(DbgNode).getDirectory();
443 else
444 assert (0 && "Invalid DIScope!");
445 return NULL;
446}
447
Chris Lattnera45664f2008-11-10 02:56:27 +0000448//===----------------------------------------------------------------------===//
Devang Patel7136a652009-07-01 22:10:23 +0000449// DIDescriptor: dump routines for all descriptors.
450//===----------------------------------------------------------------------===//
451
452
453/// dump - Print descriptor.
454void DIDescriptor::dump() const {
Devang Patele4b27562009-08-28 23:24:31 +0000455 errs() << "[" << dwarf::TagString(getTag()) << "] ";
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000456 errs().write_hex((intptr_t) &*DbgNode) << ']';
Devang Patel7136a652009-07-01 22:10:23 +0000457}
458
459/// dump - Print compile unit.
460void DICompileUnit::dump() const {
461 if (getLanguage())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000462 errs() << " [" << dwarf::LanguageString(getLanguage()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000463
Devang Patel5ccdd102009-09-29 18:40:58 +0000464 errs() << " [" << getDirectory() << "/" << getFilename() << " ]";
Devang Patel7136a652009-07-01 22:10:23 +0000465}
466
467/// dump - Print type.
468void DIType::dump() const {
469 if (isNull()) return;
470
Devang Patel5ccdd102009-09-29 18:40:58 +0000471 if (const char *Res = getName())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000472 errs() << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000473
474 unsigned Tag = getTag();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000475 errs() << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000476
477 // TODO : Print context
478 getCompileUnit().dump();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000479 errs() << " ["
480 << getLineNumber() << ", "
Chris Lattnera81d29b2009-08-23 07:33:14 +0000481 << getSizeInBits() << ", "
482 << getAlignInBits() << ", "
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000483 << getOffsetInBits()
Chris Lattnera81d29b2009-08-23 07:33:14 +0000484 << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000485
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000486 if (isPrivate())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000487 errs() << " [private] ";
Devang Patel7136a652009-07-01 22:10:23 +0000488 else if (isProtected())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000489 errs() << " [protected] ";
Devang Patel7136a652009-07-01 22:10:23 +0000490
491 if (isForwardDecl())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000492 errs() << " [fwd] ";
Devang Patel7136a652009-07-01 22:10:23 +0000493
Devang Patel6ceea332009-08-31 18:49:10 +0000494 if (isBasicType())
Devang Patele4b27562009-08-28 23:24:31 +0000495 DIBasicType(DbgNode).dump();
Devang Patel6ceea332009-08-31 18:49:10 +0000496 else if (isDerivedType())
Devang Patele4b27562009-08-28 23:24:31 +0000497 DIDerivedType(DbgNode).dump();
Devang Patel6ceea332009-08-31 18:49:10 +0000498 else if (isCompositeType())
Devang Patele4b27562009-08-28 23:24:31 +0000499 DICompositeType(DbgNode).dump();
Devang Patel7136a652009-07-01 22:10:23 +0000500 else {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000501 errs() << "Invalid DIType\n";
Devang Patel7136a652009-07-01 22:10:23 +0000502 return;
503 }
504
Chris Lattnera81d29b2009-08-23 07:33:14 +0000505 errs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000506}
507
508/// dump - Print basic type.
509void DIBasicType::dump() const {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000510 errs() << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000511}
512
513/// dump - Print derived type.
514void DIDerivedType::dump() const {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000515 errs() << "\n\t Derived From: "; getTypeDerivedFrom().dump();
Devang Patel7136a652009-07-01 22:10:23 +0000516}
517
518/// dump - Print composite type.
519void DICompositeType::dump() const {
520 DIArray A = getTypeArray();
521 if (A.isNull())
522 return;
Chris Lattnera81d29b2009-08-23 07:33:14 +0000523 errs() << " [" << A.getNumElements() << " elements]";
Devang Patel7136a652009-07-01 22:10:23 +0000524}
525
526/// dump - Print global.
527void DIGlobal::dump() const {
Devang Patel5ccdd102009-09-29 18:40:58 +0000528 if (const char *Res = getName())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000529 errs() << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000530
531 unsigned Tag = getTag();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000532 errs() << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000533
534 // TODO : Print context
535 getCompileUnit().dump();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000536 errs() << " [" << getLineNumber() << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000537
538 if (isLocalToUnit())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000539 errs() << " [local] ";
Devang Patel7136a652009-07-01 22:10:23 +0000540
541 if (isDefinition())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000542 errs() << " [def] ";
Devang Patel7136a652009-07-01 22:10:23 +0000543
Devang Patel6ceea332009-08-31 18:49:10 +0000544 if (isGlobalVariable())
Devang Patele4b27562009-08-28 23:24:31 +0000545 DIGlobalVariable(DbgNode).dump();
Devang Patel7136a652009-07-01 22:10:23 +0000546
Chris Lattnera81d29b2009-08-23 07:33:14 +0000547 errs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000548}
549
550/// dump - Print subprogram.
551void DISubprogram::dump() const {
Devang Patel5ccdd102009-09-29 18:40:58 +0000552 if (const char *Res = getName())
Devang Patel82dfc0c2009-08-31 22:47:13 +0000553 errs() << " [" << Res << "] ";
554
555 unsigned Tag = getTag();
556 errs() << " [" << dwarf::TagString(Tag) << "] ";
557
558 // TODO : Print context
559 getCompileUnit().dump();
560 errs() << " [" << getLineNumber() << "] ";
561
562 if (isLocalToUnit())
563 errs() << " [local] ";
564
565 if (isDefinition())
566 errs() << " [def] ";
567
568 errs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000569}
570
571/// dump - Print global variable.
572void DIGlobalVariable::dump() const {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000573 errs() << " [";
574 getGlobal()->dump();
575 errs() << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000576}
577
578/// dump - Print variable.
579void DIVariable::dump() const {
Devang Patel5ccdd102009-09-29 18:40:58 +0000580 if (const char *Res = getName())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000581 errs() << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000582
583 getCompileUnit().dump();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000584 errs() << " [" << getLineNumber() << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000585 getType().dump();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000586 errs() << "\n";
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000587
588 // FIXME: Dump complex addresses
Devang Patel7136a652009-07-01 22:10:23 +0000589}
590
591//===----------------------------------------------------------------------===//
Chris Lattnera45664f2008-11-10 02:56:27 +0000592// DIFactory: Basic Helpers
593//===----------------------------------------------------------------------===//
594
Bill Wendlingdc817b62009-05-14 18:26:15 +0000595DIFactory::DIFactory(Module &m)
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000596 : M(m), VMContext(M.getContext()), StopPointFn(0), FuncStartFn(0),
Owen Anderson99035272009-07-07 17:12:53 +0000597 RegionStartFn(0), RegionEndFn(0),
Bill Wendlingdc817b62009-05-14 18:26:15 +0000598 DeclareFn(0) {
Owen Andersond7f2a6c2009-08-05 23:16:16 +0000599 EmptyStructPtr = PointerType::getUnqual(StructType::get(VMContext));
Chris Lattner497a7a82008-11-10 04:10:34 +0000600}
601
Chris Lattnera45664f2008-11-10 02:56:27 +0000602Constant *DIFactory::GetTagConstant(unsigned TAG) {
Devang Patel6906ba52009-01-20 19:22:03 +0000603 assert((TAG & LLVMDebugVersionMask) == 0 &&
Chris Lattnera45664f2008-11-10 02:56:27 +0000604 "Tag too large for debug encoding!");
Owen Anderson1d0be152009-08-13 21:58:54 +0000605 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
Chris Lattnera45664f2008-11-10 02:56:27 +0000606}
607
Chris Lattnera45664f2008-11-10 02:56:27 +0000608//===----------------------------------------------------------------------===//
609// DIFactory: Primary Constructors
610//===----------------------------------------------------------------------===//
611
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000612/// GetOrCreateArray - Create an descriptor for an array of descriptors.
Chris Lattnera45664f2008-11-10 02:56:27 +0000613/// This implicitly uniques the arrays created.
614DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
Devang Patele4b27562009-08-28 23:24:31 +0000615 SmallVector<Value*, 16> Elts;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000616
Devang Patele4b27562009-08-28 23:24:31 +0000617 if (NumTys == 0)
618 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)));
619 else
620 for (unsigned i = 0; i != NumTys; ++i)
621 Elts.push_back(Tys[i].getNode());
Devang Patel82459882009-08-26 05:01:18 +0000622
Devang Patele4b27562009-08-28 23:24:31 +0000623 return DIArray(MDNode::get(VMContext,Elts.data(), Elts.size()));
Chris Lattnera45664f2008-11-10 02:56:27 +0000624}
625
626/// GetOrCreateSubrange - Create a descriptor for a value range. This
627/// implicitly uniques the values returned.
628DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
Devang Patele4b27562009-08-28 23:24:31 +0000629 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000630 GetTagConstant(dwarf::DW_TAG_subrange_type),
Owen Anderson1d0be152009-08-13 21:58:54 +0000631 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
632 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
Chris Lattnera45664f2008-11-10 02:56:27 +0000633 };
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000634
Devang Patele4b27562009-08-28 23:24:31 +0000635 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000636}
637
638
639
640/// CreateCompileUnit - Create a new descriptor for the specified compile
641/// unit. Note that this does not unique compile units within the module.
642DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
Devang Patel5ccdd102009-09-29 18:40:58 +0000643 StringRef Filename,
644 StringRef Directory,
645 StringRef Producer,
Devang Pateldd9db662009-01-30 18:20:31 +0000646 bool isMain,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000647 bool isOptimized,
Devang Patel13319ce2009-02-17 22:43:44 +0000648 const char *Flags,
649 unsigned RunTimeVer) {
Devang Patele4b27562009-08-28 23:24:31 +0000650 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000651 GetTagConstant(dwarf::DW_TAG_compile_unit),
Devang Patele4b27562009-08-28 23:24:31 +0000652 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Owen Anderson1d0be152009-08-13 21:58:54 +0000653 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
Devang Patele4b27562009-08-28 23:24:31 +0000654 MDString::get(VMContext, Filename),
655 MDString::get(VMContext, Directory),
656 MDString::get(VMContext, Producer),
Owen Anderson1d0be152009-08-13 21:58:54 +0000657 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
658 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
Devang Patele4b27562009-08-28 23:24:31 +0000659 MDString::get(VMContext, Flags),
Owen Anderson1d0be152009-08-13 21:58:54 +0000660 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
Chris Lattnera45664f2008-11-10 02:56:27 +0000661 };
Devang Patele4b27562009-08-28 23:24:31 +0000662
663 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000664}
665
666/// CreateEnumerator - Create a single enumerator value.
Devang Patel5ccdd102009-09-29 18:40:58 +0000667DIEnumerator DIFactory::CreateEnumerator(StringRef Name, uint64_t Val){
Devang Patele4b27562009-08-28 23:24:31 +0000668 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000669 GetTagConstant(dwarf::DW_TAG_enumerator),
Devang Patele4b27562009-08-28 23:24:31 +0000670 MDString::get(VMContext, Name),
Owen Anderson1d0be152009-08-13 21:58:54 +0000671 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
Chris Lattnera45664f2008-11-10 02:56:27 +0000672 };
Devang Patele4b27562009-08-28 23:24:31 +0000673 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000674}
675
676
677/// CreateBasicType - Create a basic type like int, float, etc.
678DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
Devang Patel5ccdd102009-09-29 18:40:58 +0000679 StringRef Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000680 DICompileUnit CompileUnit,
681 unsigned LineNumber,
682 uint64_t SizeInBits,
683 uint64_t AlignInBits,
684 uint64_t OffsetInBits, unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000685 unsigned Encoding) {
Devang Patele4b27562009-08-28 23:24:31 +0000686 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000687 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patele4b27562009-08-28 23:24:31 +0000688 Context.getNode(),
689 MDString::get(VMContext, Name),
690 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000691 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
692 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
693 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
694 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
695 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
696 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
Chris Lattnera45664f2008-11-10 02:56:27 +0000697 };
Devang Patele4b27562009-08-28 23:24:31 +0000698 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000699}
700
Devang Patelac16d442009-10-26 16:54:35 +0000701
702/// CreateBasicType - Create a basic type like int, float, etc.
703DIBasicType DIFactory::CreateBasicTypeEx(DIDescriptor Context,
704 StringRef Name,
705 DICompileUnit CompileUnit,
706 unsigned LineNumber,
707 Constant *SizeInBits,
708 Constant *AlignInBits,
709 Constant *OffsetInBits, unsigned Flags,
710 unsigned Encoding) {
711 Value *Elts[] = {
712 GetTagConstant(dwarf::DW_TAG_base_type),
713 Context.getNode(),
714 MDString::get(VMContext, Name),
715 CompileUnit.getNode(),
716 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
717 SizeInBits,
718 AlignInBits,
719 OffsetInBits,
720 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
721 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
722 };
723 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
724}
725
726
Chris Lattnera45664f2008-11-10 02:56:27 +0000727/// CreateDerivedType - Create a derived type like const qualified type,
728/// pointer, typedef, etc.
729DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
730 DIDescriptor Context,
Devang Patel5ccdd102009-09-29 18:40:58 +0000731 StringRef Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000732 DICompileUnit CompileUnit,
733 unsigned LineNumber,
734 uint64_t SizeInBits,
735 uint64_t AlignInBits,
736 uint64_t OffsetInBits,
737 unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000738 DIType DerivedFrom) {
Devang Patele4b27562009-08-28 23:24:31 +0000739 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000740 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000741 Context.getNode(),
742 MDString::get(VMContext, Name),
743 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000744 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
745 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
746 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
747 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
748 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patele4b27562009-08-28 23:24:31 +0000749 DerivedFrom.getNode(),
Chris Lattnera45664f2008-11-10 02:56:27 +0000750 };
Devang Patele4b27562009-08-28 23:24:31 +0000751 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000752}
753
Devang Patelac16d442009-10-26 16:54:35 +0000754
755/// CreateDerivedType - Create a derived type like const qualified type,
756/// pointer, typedef, etc.
757DIDerivedType DIFactory::CreateDerivedTypeEx(unsigned Tag,
758 DIDescriptor Context,
759 StringRef Name,
760 DICompileUnit CompileUnit,
761 unsigned LineNumber,
762 Constant *SizeInBits,
763 Constant *AlignInBits,
764 Constant *OffsetInBits,
765 unsigned Flags,
766 DIType DerivedFrom) {
767 Value *Elts[] = {
768 GetTagConstant(Tag),
769 Context.getNode(),
770 MDString::get(VMContext, Name),
771 CompileUnit.getNode(),
772 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
773 SizeInBits,
774 AlignInBits,
775 OffsetInBits,
776 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
777 DerivedFrom.getNode(),
778 };
779 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
780}
781
782
Chris Lattnera45664f2008-11-10 02:56:27 +0000783/// CreateCompositeType - Create a composite type like array, struct, etc.
784DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
785 DIDescriptor Context,
Devang Patel5ccdd102009-09-29 18:40:58 +0000786 StringRef Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000787 DICompileUnit CompileUnit,
788 unsigned LineNumber,
789 uint64_t SizeInBits,
790 uint64_t AlignInBits,
791 uint64_t OffsetInBits,
792 unsigned Flags,
793 DIType DerivedFrom,
Devang Patel13319ce2009-02-17 22:43:44 +0000794 DIArray Elements,
795 unsigned RuntimeLang) {
Owen Andersone277fed2009-07-07 16:31:25 +0000796
Devang Patele4b27562009-08-28 23:24:31 +0000797 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000798 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000799 Context.getNode(),
800 MDString::get(VMContext, Name),
801 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000802 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
803 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
804 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
805 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
806 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patele4b27562009-08-28 23:24:31 +0000807 DerivedFrom.getNode(),
808 Elements.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000809 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
Chris Lattnera45664f2008-11-10 02:56:27 +0000810 };
Devang Patele4b27562009-08-28 23:24:31 +0000811 return DICompositeType(MDNode::get(VMContext, &Elts[0], 12));
Chris Lattnera45664f2008-11-10 02:56:27 +0000812}
813
814
Devang Patelac16d442009-10-26 16:54:35 +0000815/// CreateCompositeType - Create a composite type like array, struct, etc.
816DICompositeType DIFactory::CreateCompositeTypeEx(unsigned Tag,
817 DIDescriptor Context,
818 StringRef Name,
819 DICompileUnit CompileUnit,
820 unsigned LineNumber,
821 Constant *SizeInBits,
822 Constant *AlignInBits,
823 Constant *OffsetInBits,
824 unsigned Flags,
825 DIType DerivedFrom,
826 DIArray Elements,
827 unsigned RuntimeLang) {
828
829 Value *Elts[] = {
830 GetTagConstant(Tag),
831 Context.getNode(),
832 MDString::get(VMContext, Name),
833 CompileUnit.getNode(),
834 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
835 SizeInBits,
836 AlignInBits,
837 OffsetInBits,
838 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
839 DerivedFrom.getNode(),
840 Elements.getNode(),
841 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
842 };
843 return DICompositeType(MDNode::get(VMContext, &Elts[0], 12));
844}
845
846
Chris Lattnera45664f2008-11-10 02:56:27 +0000847/// CreateSubprogram - Create a new descriptor for the specified subprogram.
848/// See comments in DISubprogram for descriptions of these fields. This
849/// method does not unique the generated descriptors.
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000850DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
Devang Patel5ccdd102009-09-29 18:40:58 +0000851 StringRef Name,
852 StringRef DisplayName,
853 StringRef LinkageName,
Chris Lattnera45664f2008-11-10 02:56:27 +0000854 DICompileUnit CompileUnit,
855 unsigned LineNo, DIType Type,
856 bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000857 bool isDefinition) {
Devang Patel854967e2008-12-17 22:39:29 +0000858
Devang Patele4b27562009-08-28 23:24:31 +0000859 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000860 GetTagConstant(dwarf::DW_TAG_subprogram),
Devang Patele4b27562009-08-28 23:24:31 +0000861 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
862 Context.getNode(),
863 MDString::get(VMContext, Name),
864 MDString::get(VMContext, DisplayName),
865 MDString::get(VMContext, LinkageName),
866 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000867 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000868 Type.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000869 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
870 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition)
Chris Lattnera45664f2008-11-10 02:56:27 +0000871 };
Devang Patele4b27562009-08-28 23:24:31 +0000872 return DISubprogram(MDNode::get(VMContext, &Elts[0], 11));
Chris Lattnera45664f2008-11-10 02:56:27 +0000873}
874
875/// CreateGlobalVariable - Create a new descriptor for the specified global.
876DIGlobalVariable
Devang Patel5ccdd102009-09-29 18:40:58 +0000877DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
878 StringRef DisplayName,
879 StringRef LinkageName,
Chris Lattnera45664f2008-11-10 02:56:27 +0000880 DICompileUnit CompileUnit,
881 unsigned LineNo, DIType Type,bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000882 bool isDefinition, llvm::GlobalVariable *Val) {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000883 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000884 GetTagConstant(dwarf::DW_TAG_variable),
Devang Patele4b27562009-08-28 23:24:31 +0000885 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
886 Context.getNode(),
887 MDString::get(VMContext, Name),
888 MDString::get(VMContext, DisplayName),
889 MDString::get(VMContext, LinkageName),
890 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000891 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000892 Type.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000893 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
894 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
Devang Patele4b27562009-08-28 23:24:31 +0000895 Val
Chris Lattnera45664f2008-11-10 02:56:27 +0000896 };
Devang Patele4b27562009-08-28 23:24:31 +0000897
898 Value *const *Vs = &Elts[0];
899 MDNode *Node = MDNode::get(VMContext,Vs, 12);
900
901 // Create a named metadata so that we do not lose this mdnode.
902 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
903 NMD->addElement(Node);
904
905 return DIGlobalVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +0000906}
907
908
909/// CreateVariable - Create a new descriptor for the specified variable.
910DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
Devang Patel5ccdd102009-09-29 18:40:58 +0000911 StringRef Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000912 DICompileUnit CompileUnit, unsigned LineNo,
Devang Pateldd9db662009-01-30 18:20:31 +0000913 DIType Type) {
Devang Patele4b27562009-08-28 23:24:31 +0000914 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000915 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000916 Context.getNode(),
917 MDString::get(VMContext, Name),
918 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000919 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000920 Type.getNode(),
Chris Lattnera45664f2008-11-10 02:56:27 +0000921 };
Devang Patele4b27562009-08-28 23:24:31 +0000922 return DIVariable(MDNode::get(VMContext, &Elts[0], 6));
Chris Lattnera45664f2008-11-10 02:56:27 +0000923}
924
925
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000926/// CreateComplexVariable - Create a new descriptor for the specified variable
927/// which has a complex address expression for its address.
928DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
929 const std::string &Name,
930 DICompileUnit CompileUnit,
931 unsigned LineNo,
932 DIType Type, SmallVector<Value *, 9> &addr) {
933 SmallVector<Value *, 9> Elts;
934 Elts.push_back(GetTagConstant(Tag));
935 Elts.push_back(Context.getNode());
936 Elts.push_back(MDString::get(VMContext, Name));
937 Elts.push_back(CompileUnit.getNode());
938 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
939 Elts.push_back(Type.getNode());
940 Elts.insert(Elts.end(), addr.begin(), addr.end());
941
942 return DIVariable(MDNode::get(VMContext, &Elts[0], 6+addr.size()));
943}
944
945
Chris Lattnera45664f2008-11-10 02:56:27 +0000946/// CreateBlock - This creates a descriptor for a lexical block with the
Owen Anderson99035272009-07-07 17:12:53 +0000947/// specified parent VMContext.
Devang Patel5e005d82009-08-31 22:00:15 +0000948DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context) {
Devang Patele4b27562009-08-28 23:24:31 +0000949 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000950 GetTagConstant(dwarf::DW_TAG_lexical_block),
Devang Patele4b27562009-08-28 23:24:31 +0000951 Context.getNode()
Chris Lattnera45664f2008-11-10 02:56:27 +0000952 };
Devang Patel5e005d82009-08-31 22:00:15 +0000953 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 2));
Chris Lattnera45664f2008-11-10 02:56:27 +0000954}
955
Devang Patelf98d8fe2009-09-01 01:14:15 +0000956/// CreateLocation - Creates a debug info location.
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000957DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
Daniel Dunbara279bc32009-09-20 02:20:51 +0000958 DIScope S, DILocation OrigLoc) {
Devang Patelf98d8fe2009-09-01 01:14:15 +0000959 Value *Elts[] = {
960 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
961 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
962 S.getNode(),
963 OrigLoc.getNode(),
964 };
965 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
966}
967
Chris Lattnera45664f2008-11-10 02:56:27 +0000968
969//===----------------------------------------------------------------------===//
970// DIFactory: Routines for inserting code into a function
971//===----------------------------------------------------------------------===//
972
973/// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
974/// inserting it at the end of the specified basic block.
975void DIFactory::InsertStopPoint(DICompileUnit CU, unsigned LineNo,
976 unsigned ColNo, BasicBlock *BB) {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000977
Chris Lattnera45664f2008-11-10 02:56:27 +0000978 // Lazily construct llvm.dbg.stoppoint function.
979 if (!StopPointFn)
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000980 StopPointFn = llvm::Intrinsic::getDeclaration(&M,
Chris Lattnera45664f2008-11-10 02:56:27 +0000981 llvm::Intrinsic::dbg_stoppoint);
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000982
Chris Lattnera45664f2008-11-10 02:56:27 +0000983 // Invoke llvm.dbg.stoppoint
984 Value *Args[] = {
Owen Anderson1d0be152009-08-13 21:58:54 +0000985 ConstantInt::get(llvm::Type::getInt32Ty(VMContext), LineNo),
986 ConstantInt::get(llvm::Type::getInt32Ty(VMContext), ColNo),
Devang Patele4b27562009-08-28 23:24:31 +0000987 CU.getNode()
Chris Lattnera45664f2008-11-10 02:56:27 +0000988 };
989 CallInst::Create(StopPointFn, Args, Args+3, "", BB);
990}
991
992/// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
993/// mark the start of the specified subprogram.
994void DIFactory::InsertSubprogramStart(DISubprogram SP, BasicBlock *BB) {
995 // Lazily construct llvm.dbg.func.start.
996 if (!FuncStartFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000997 FuncStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_func_start);
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000998
Chris Lattnera45664f2008-11-10 02:56:27 +0000999 // Call llvm.dbg.func.start which also implicitly sets a stoppoint.
Devang Patele4b27562009-08-28 23:24:31 +00001000 CallInst::Create(FuncStartFn, SP.getNode(), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +00001001}
1002
1003/// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
1004/// mark the start of a region for the specified scoping descriptor.
1005void DIFactory::InsertRegionStart(DIDescriptor D, BasicBlock *BB) {
1006 // Lazily construct llvm.dbg.region.start function.
1007 if (!RegionStartFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +00001008 RegionStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_start);
1009
Chris Lattnera45664f2008-11-10 02:56:27 +00001010 // Call llvm.dbg.func.start.
Devang Patele4b27562009-08-28 23:24:31 +00001011 CallInst::Create(RegionStartFn, D.getNode(), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +00001012}
1013
Chris Lattnera45664f2008-11-10 02:56:27 +00001014/// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
1015/// mark the end of a region for the specified scoping descriptor.
1016void DIFactory::InsertRegionEnd(DIDescriptor D, BasicBlock *BB) {
1017 // Lazily construct llvm.dbg.region.end function.
1018 if (!RegionEndFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +00001019 RegionEndFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_end);
1020
1021 // Call llvm.dbg.region.end.
Devang Patele4b27562009-08-28 23:24:31 +00001022 CallInst::Create(RegionEndFn, D.getNode(), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +00001023}
1024
1025/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Mike Stumpe4250392009-10-01 22:08:58 +00001026void DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1027 Instruction *InsertBefore) {
Chris Lattnera45664f2008-11-10 02:56:27 +00001028 // Cast the storage to a {}* for the call to llvm.dbg.declare.
Mike Stumpe4250392009-10-01 22:08:58 +00001029 Storage = new BitCastInst(Storage, EmptyStructPtr, "", InsertBefore);
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001030
Chris Lattnera45664f2008-11-10 02:56:27 +00001031 if (!DeclareFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +00001032 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1033
Devang Patele4b27562009-08-28 23:24:31 +00001034 Value *Args[] = { Storage, D.getNode() };
Mike Stumpe4250392009-10-01 22:08:58 +00001035 CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
1036}
1037
1038/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1039void DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1040 BasicBlock *InsertAtEnd) {
1041 // Cast the storage to a {}* for the call to llvm.dbg.declare.
1042 Storage = new BitCastInst(Storage, EmptyStructPtr, "", InsertAtEnd);
1043
1044 if (!DeclareFn)
1045 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1046
1047 Value *Args[] = { Storage, D.getNode() };
1048 CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);
Chris Lattnera45664f2008-11-10 02:56:27 +00001049}
Torok Edwin620f2802008-12-16 09:07:36 +00001050
Devang Patele4b27562009-08-28 23:24:31 +00001051
Devang Pateld2f79a12009-07-28 19:55:13 +00001052//===----------------------------------------------------------------------===//
Devang Patel98c65172009-07-30 18:25:15 +00001053// DebugInfoFinder implementations.
Devang Pateld2f79a12009-07-28 19:55:13 +00001054//===----------------------------------------------------------------------===//
1055
Devang Patel98c65172009-07-30 18:25:15 +00001056/// processModule - Process entire module and collect debug info.
1057void DebugInfoFinder::processModule(Module &M) {
Devang Patele4b27562009-08-28 23:24:31 +00001058
Devang Patelbeab41b2009-10-07 22:04:08 +00001059#ifdef ATTACH_DEBUG_INFO_TO_AN_INSN
1060 MetadataContext &TheMetadata = M.getContext().getMetadata();
1061 unsigned MDDbgKind = TheMetadata.getMDKind("dbg");
Devang Patelbeab41b2009-10-07 22:04:08 +00001062#endif
Devang Pateld2f79a12009-07-28 19:55:13 +00001063 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1064 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
1065 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
1066 ++BI) {
1067 if (DbgStopPointInst *SPI = dyn_cast<DbgStopPointInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +00001068 processStopPoint(SPI);
Devang Pateld2f79a12009-07-28 19:55:13 +00001069 else if (DbgFuncStartInst *FSI = dyn_cast<DbgFuncStartInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +00001070 processFuncStart(FSI);
Devang Patele802f1c2009-07-30 17:30:23 +00001071 else if (DbgRegionStartInst *DRS = dyn_cast<DbgRegionStartInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +00001072 processRegionStart(DRS);
Devang Patele802f1c2009-07-30 17:30:23 +00001073 else if (DbgRegionEndInst *DRE = dyn_cast<DbgRegionEndInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +00001074 processRegionEnd(DRE);
Devang Patelb4d31302009-07-31 18:18:52 +00001075 else if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1076 processDeclare(DDI);
Devang Patelbeab41b2009-10-07 22:04:08 +00001077#ifdef ATTACH_DEBUG_INFO_TO_AN_INSN
Devang Patel549e81f2009-10-13 17:35:35 +00001078 else if (MDDbgKind) {
1079 if (MDNode *L = TheMetadata.getMD(MDDbgKind, BI)) {
1080 DILocation Loc(L);
1081 DIScope S(Loc.getScope().getNode());
1082 if (S.isCompileUnit())
1083 addCompileUnit(DICompileUnit(S.getNode()));
1084 else if (S.isSubprogram())
1085 processSubprogram(DISubprogram(S.getNode()));
1086 else if (S.isLexicalBlock())
1087 processLexicalBlock(DILexicalBlock(S.getNode()));
1088 }
Devang Patelbeab41b2009-10-07 22:04:08 +00001089 }
1090#endif
Devang Pateld2f79a12009-07-28 19:55:13 +00001091 }
Devang Patele4b27562009-08-28 23:24:31 +00001092
1093 NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv");
1094 if (!NMD)
1095 return;
1096
1097 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
1098 DIGlobalVariable DIG(cast<MDNode>(NMD->getElement(i)));
Devang Pateld2f79a12009-07-28 19:55:13 +00001099 if (addGlobalVariable(DIG)) {
1100 addCompileUnit(DIG.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +00001101 processType(DIG.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +00001102 }
1103 }
1104}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001105
Devang Patel98c65172009-07-30 18:25:15 +00001106/// processType - Process DIType.
1107void DebugInfoFinder::processType(DIType DT) {
Devang Patel72bcdb62009-08-10 22:09:58 +00001108 if (!addType(DT))
Devang Pateld2f79a12009-07-28 19:55:13 +00001109 return;
1110
1111 addCompileUnit(DT.getCompileUnit());
Devang Patel6ceea332009-08-31 18:49:10 +00001112 if (DT.isCompositeType()) {
Devang Patele4b27562009-08-28 23:24:31 +00001113 DICompositeType DCT(DT.getNode());
Devang Patel98c65172009-07-30 18:25:15 +00001114 processType(DCT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001115 DIArray DA = DCT.getTypeArray();
1116 if (!DA.isNull())
1117 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1118 DIDescriptor D = DA.getElement(i);
Devang Patele4b27562009-08-28 23:24:31 +00001119 DIType TypeE = DIType(D.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001120 if (!TypeE.isNull())
Devang Patel98c65172009-07-30 18:25:15 +00001121 processType(TypeE);
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001122 else
Devang Patele4b27562009-08-28 23:24:31 +00001123 processSubprogram(DISubprogram(D.getNode()));
Devang Pateld2f79a12009-07-28 19:55:13 +00001124 }
Devang Patel6ceea332009-08-31 18:49:10 +00001125 } else if (DT.isDerivedType()) {
Devang Patele4b27562009-08-28 23:24:31 +00001126 DIDerivedType DDT(DT.getNode());
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001127 if (!DDT.isNull())
Devang Patel98c65172009-07-30 18:25:15 +00001128 processType(DDT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001129 }
1130}
1131
Devang Patelbeab41b2009-10-07 22:04:08 +00001132/// processLexicalBlock
1133void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
1134 if (LB.isNull())
1135 return;
1136 DIScope Context = LB.getContext();
1137 if (Context.isLexicalBlock())
1138 return processLexicalBlock(DILexicalBlock(Context.getNode()));
1139 else
1140 return processSubprogram(DISubprogram(Context.getNode()));
1141}
1142
Devang Patel98c65172009-07-30 18:25:15 +00001143/// processSubprogram - Process DISubprogram.
1144void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Patele802f1c2009-07-30 17:30:23 +00001145 if (SP.isNull())
1146 return;
Devang Pateld2f79a12009-07-28 19:55:13 +00001147 if (!addSubprogram(SP))
1148 return;
1149 addCompileUnit(SP.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +00001150 processType(SP.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +00001151}
1152
Devang Patel98c65172009-07-30 18:25:15 +00001153/// processStopPoint - Process DbgStopPointInst.
1154void DebugInfoFinder::processStopPoint(DbgStopPointInst *SPI) {
Devang Patele4b27562009-08-28 23:24:31 +00001155 MDNode *Context = dyn_cast<MDNode>(SPI->getContext());
Devang Pateld2f79a12009-07-28 19:55:13 +00001156 addCompileUnit(DICompileUnit(Context));
1157}
1158
Devang Patel98c65172009-07-30 18:25:15 +00001159/// processFuncStart - Process DbgFuncStartInst.
1160void DebugInfoFinder::processFuncStart(DbgFuncStartInst *FSI) {
Devang Patele4b27562009-08-28 23:24:31 +00001161 MDNode *SP = dyn_cast<MDNode>(FSI->getSubprogram());
Devang Patel98c65172009-07-30 18:25:15 +00001162 processSubprogram(DISubprogram(SP));
Devang Pateld2f79a12009-07-28 19:55:13 +00001163}
1164
Devang Patel98c65172009-07-30 18:25:15 +00001165/// processRegionStart - Process DbgRegionStart.
1166void DebugInfoFinder::processRegionStart(DbgRegionStartInst *DRS) {
Devang Patele4b27562009-08-28 23:24:31 +00001167 MDNode *SP = dyn_cast<MDNode>(DRS->getContext());
Devang Patel98c65172009-07-30 18:25:15 +00001168 processSubprogram(DISubprogram(SP));
Devang Patele802f1c2009-07-30 17:30:23 +00001169}
1170
Devang Patel98c65172009-07-30 18:25:15 +00001171/// processRegionEnd - Process DbgRegionEnd.
1172void DebugInfoFinder::processRegionEnd(DbgRegionEndInst *DRE) {
Devang Patele4b27562009-08-28 23:24:31 +00001173 MDNode *SP = dyn_cast<MDNode>(DRE->getContext());
Devang Patel98c65172009-07-30 18:25:15 +00001174 processSubprogram(DISubprogram(SP));
Devang Patele802f1c2009-07-30 17:30:23 +00001175}
1176
Devang Patelb4d31302009-07-31 18:18:52 +00001177/// processDeclare - Process DbgDeclareInst.
1178void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
Devang Patele4b27562009-08-28 23:24:31 +00001179 DIVariable DV(cast<MDNode>(DDI->getVariable()));
Devang Patelb4d31302009-07-31 18:18:52 +00001180 if (DV.isNull())
1181 return;
1182
Devang Patele4b27562009-08-28 23:24:31 +00001183 if (!NodesSeen.insert(DV.getNode()))
Devang Patelb4d31302009-07-31 18:18:52 +00001184 return;
1185
1186 addCompileUnit(DV.getCompileUnit());
1187 processType(DV.getType());
1188}
1189
Devang Patel72bcdb62009-08-10 22:09:58 +00001190/// addType - Add type into Tys.
1191bool DebugInfoFinder::addType(DIType DT) {
1192 if (DT.isNull())
1193 return false;
1194
Devang Patele4b27562009-08-28 23:24:31 +00001195 if (!NodesSeen.insert(DT.getNode()))
Devang Patel72bcdb62009-08-10 22:09:58 +00001196 return false;
1197
Devang Patele4b27562009-08-28 23:24:31 +00001198 TYs.push_back(DT.getNode());
Devang Patel72bcdb62009-08-10 22:09:58 +00001199 return true;
1200}
1201
Devang Pateld2f79a12009-07-28 19:55:13 +00001202/// addCompileUnit - Add compile unit into CUs.
Devang Patel98c65172009-07-30 18:25:15 +00001203bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001204 if (CU.isNull())
1205 return false;
1206
Devang Patele4b27562009-08-28 23:24:31 +00001207 if (!NodesSeen.insert(CU.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001208 return false;
1209
Devang Patele4b27562009-08-28 23:24:31 +00001210 CUs.push_back(CU.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001211 return true;
1212}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001213
Devang Pateld2f79a12009-07-28 19:55:13 +00001214/// addGlobalVariable - Add global variable into GVs.
Devang Patel98c65172009-07-30 18:25:15 +00001215bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001216 if (DIG.isNull())
1217 return false;
1218
Devang Patele4b27562009-08-28 23:24:31 +00001219 if (!NodesSeen.insert(DIG.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001220 return false;
1221
Devang Patele4b27562009-08-28 23:24:31 +00001222 GVs.push_back(DIG.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001223 return true;
1224}
1225
1226// addSubprogram - Add subprgoram into SPs.
Devang Patel98c65172009-07-30 18:25:15 +00001227bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001228 if (SP.isNull())
1229 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001230
Devang Patele4b27562009-08-28 23:24:31 +00001231 if (!NodesSeen.insert(SP.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001232 return false;
1233
Devang Patele4b27562009-08-28 23:24:31 +00001234 SPs.push_back(SP.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001235 return true;
1236}
1237
Torok Edwin620f2802008-12-16 09:07:36 +00001238namespace llvm {
Bill Wendlingdc817b62009-05-14 18:26:15 +00001239 /// findStopPoint - Find the stoppoint coressponding to this instruction, that
1240 /// is the stoppoint that dominates this instruction.
1241 const DbgStopPointInst *findStopPoint(const Instruction *Inst) {
Torok Edwin620f2802008-12-16 09:07:36 +00001242 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(Inst))
1243 return DSI;
1244
1245 const BasicBlock *BB = Inst->getParent();
1246 BasicBlock::const_iterator I = Inst, B;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001247 while (BB) {
Torok Edwin620f2802008-12-16 09:07:36 +00001248 B = BB->begin();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001249
Torok Edwin620f2802008-12-16 09:07:36 +00001250 // A BB consisting only of a terminator can't have a stoppoint.
Bill Wendlingdc817b62009-05-14 18:26:15 +00001251 while (I != B) {
1252 --I;
1253 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
1254 return DSI;
Torok Edwin620f2802008-12-16 09:07:36 +00001255 }
Bill Wendlingdc817b62009-05-14 18:26:15 +00001256
1257 // This BB didn't have a stoppoint: if there is only one predecessor, look
1258 // for a stoppoint there. We could use getIDom(), but that would require
1259 // dominator info.
Torok Edwin620f2802008-12-16 09:07:36 +00001260 BB = I->getParent()->getUniquePredecessor();
1261 if (BB)
1262 I = BB->getTerminator();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001263 }
1264
Torok Edwin620f2802008-12-16 09:07:36 +00001265 return 0;
1266 }
1267
Bill Wendlingdc817b62009-05-14 18:26:15 +00001268 /// findBBStopPoint - Find the stoppoint corresponding to first real
1269 /// (non-debug intrinsic) instruction in this Basic Block, and return the
1270 /// stoppoint for it.
1271 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB) {
1272 for(BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001273 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
1274 return DSI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001275
1276 // Fallback to looking for stoppoint of unique predecessor. Useful if this
1277 // BB contains no stoppoints, but unique predecessor does.
Torok Edwin620f2802008-12-16 09:07:36 +00001278 BB = BB->getUniquePredecessor();
1279 if (BB)
1280 return findStopPoint(BB->getTerminator());
Bill Wendlingdc817b62009-05-14 18:26:15 +00001281
Torok Edwin620f2802008-12-16 09:07:36 +00001282 return 0;
1283 }
1284
Bill Wendlingdc817b62009-05-14 18:26:15 +00001285 Value *findDbgGlobalDeclare(GlobalVariable *V) {
Torok Edwinff7d0e92009-03-10 13:41:26 +00001286 const Module *M = V->getParent();
Devang Patele4b27562009-08-28 23:24:31 +00001287 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1288 if (!NMD)
1289 return 0;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001290
Devang Patele4b27562009-08-28 23:24:31 +00001291 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
1292 DIGlobalVariable DIG(cast_or_null<MDNode>(NMD->getElement(i)));
1293 if (DIG.isNull())
1294 continue;
1295 if (DIG.getGlobal() == V)
1296 return DIG.getNode();
Torok Edwinff7d0e92009-03-10 13:41:26 +00001297 }
Torok Edwinff7d0e92009-03-10 13:41:26 +00001298 return 0;
1299 }
1300
Bill Wendlingdc817b62009-05-14 18:26:15 +00001301 /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
Torok Edwin620f2802008-12-16 09:07:36 +00001302 /// It looks through pointer casts too.
Bill Wendlingdc817b62009-05-14 18:26:15 +00001303 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts) {
Torok Edwin620f2802008-12-16 09:07:36 +00001304 if (stripCasts) {
1305 V = V->stripPointerCasts();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001306
Torok Edwin620f2802008-12-16 09:07:36 +00001307 // Look for the bitcast.
1308 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001309 I != E; ++I)
Devang Patel94dfaec2009-10-29 18:20:34 +00001310 if (isa<BitCastInst>(I)) {
1311 const DbgDeclareInst *DDI = findDbgDeclare(*I, false);
1312 if (DDI) return DDI;
1313 }
Torok Edwin620f2802008-12-16 09:07:36 +00001314 return 0;
1315 }
1316
Bill Wendlingdc817b62009-05-14 18:26:15 +00001317 // Find llvm.dbg.declare among uses of the instruction.
Torok Edwin620f2802008-12-16 09:07:36 +00001318 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001319 I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001320 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I))
1321 return DDI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001322
Torok Edwin620f2802008-12-16 09:07:36 +00001323 return 0;
1324 }
Torok Edwinff7d0e92009-03-10 13:41:26 +00001325
Devang Patel5ccdd102009-09-29 18:40:58 +00001326bool getLocationInfo(const Value *V, std::string &DisplayName,
1327 std::string &Type, unsigned &LineNo, std::string &File,
Bill Wendlingdc817b62009-05-14 18:26:15 +00001328 std::string &Dir) {
Torok Edwinff7d0e92009-03-10 13:41:26 +00001329 DICompileUnit Unit;
1330 DIType TypeD;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001331
Torok Edwinff7d0e92009-03-10 13:41:26 +00001332 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1333 Value *DIGV = findDbgGlobalDeclare(GV);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001334 if (!DIGV) return false;
Devang Patele4b27562009-08-28 23:24:31 +00001335 DIGlobalVariable Var(cast<MDNode>(DIGV));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001336
Devang Patel5ccdd102009-09-29 18:40:58 +00001337 if (const char *D = Var.getDisplayName())
1338 DisplayName = D;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001339 LineNo = Var.getLineNumber();
1340 Unit = Var.getCompileUnit();
1341 TypeD = Var.getType();
1342 } else {
1343 const DbgDeclareInst *DDI = findDbgDeclare(V);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001344 if (!DDI) return false;
Devang Patele4b27562009-08-28 23:24:31 +00001345 DIVariable Var(cast<MDNode>(DDI->getVariable()));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001346
Devang Patel5ccdd102009-09-29 18:40:58 +00001347 if (const char *D = Var.getName())
1348 DisplayName = D;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001349 LineNo = Var.getLineNumber();
1350 Unit = Var.getCompileUnit();
1351 TypeD = Var.getType();
1352 }
Bill Wendlingdc817b62009-05-14 18:26:15 +00001353
Devang Patel5ccdd102009-09-29 18:40:58 +00001354 if (const char *T = TypeD.getName())
1355 Type = T;
1356 if (const char *F = Unit.getFilename())
1357 File = F;
1358 if (const char *D = Unit.getDirectory())
1359 Dir = D;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001360 return true;
1361 }
Devang Patel13e16b62009-06-26 01:49:18 +00001362
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001363 /// isValidDebugInfoIntrinsic - Return true if SPI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001364 /// info intrinsic.
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001365 bool isValidDebugInfoIntrinsic(DbgStopPointInst &SPI,
Devang Patel9e529c32009-07-02 01:15:24 +00001366 CodeGenOpt::Level OptLev) {
1367 return DIDescriptor::ValidDebugInfo(SPI.getContext(), OptLev);
1368 }
1369
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001370 /// isValidDebugInfoIntrinsic - Return true if FSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001371 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001372 bool isValidDebugInfoIntrinsic(DbgFuncStartInst &FSI,
1373 CodeGenOpt::Level OptLev) {
1374 return DIDescriptor::ValidDebugInfo(FSI.getSubprogram(), OptLev);
1375 }
1376
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001377 /// isValidDebugInfoIntrinsic - Return true if RSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001378 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001379 bool isValidDebugInfoIntrinsic(DbgRegionStartInst &RSI,
1380 CodeGenOpt::Level OptLev) {
1381 return DIDescriptor::ValidDebugInfo(RSI.getContext(), OptLev);
1382 }
1383
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001384 /// isValidDebugInfoIntrinsic - Return true if REI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001385 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001386 bool isValidDebugInfoIntrinsic(DbgRegionEndInst &REI,
1387 CodeGenOpt::Level OptLev) {
1388 return DIDescriptor::ValidDebugInfo(REI.getContext(), OptLev);
1389 }
1390
1391
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001392 /// isValidDebugInfoIntrinsic - Return true if DI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001393 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001394 bool isValidDebugInfoIntrinsic(DbgDeclareInst &DI,
1395 CodeGenOpt::Level OptLev) {
1396 return DIDescriptor::ValidDebugInfo(DI.getVariable(), OptLev);
1397 }
1398
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001399 /// ExtractDebugLocation - Extract debug location information
Devang Patel9e529c32009-07-02 01:15:24 +00001400 /// from llvm.dbg.stoppoint intrinsic.
1401 DebugLoc ExtractDebugLocation(DbgStopPointInst &SPI,
Devang Patel9e529c32009-07-02 01:15:24 +00001402 DebugLocTracker &DebugLocInfo) {
1403 DebugLoc DL;
1404 Value *Context = SPI.getContext();
Devang Patel9e529c32009-07-02 01:15:24 +00001405
1406 // If this location is already tracked then use it.
Devang Patela1434042009-10-01 01:15:28 +00001407 DebugLocTuple Tuple(cast<MDNode>(Context), NULL, SPI.getLine(),
Devang Patel9e529c32009-07-02 01:15:24 +00001408 SPI.getColumn());
1409 DenseMap<DebugLocTuple, unsigned>::iterator II
1410 = DebugLocInfo.DebugIdMap.find(Tuple);
1411 if (II != DebugLocInfo.DebugIdMap.end())
1412 return DebugLoc::get(II->second);
1413
1414 // Add a new location entry.
1415 unsigned Id = DebugLocInfo.DebugLocations.size();
1416 DebugLocInfo.DebugLocations.push_back(Tuple);
1417 DebugLocInfo.DebugIdMap[Tuple] = Id;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001418
Devang Patel9e529c32009-07-02 01:15:24 +00001419 return DebugLoc::get(Id);
1420 }
1421
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001422 /// ExtractDebugLocation - Extract debug location information
Devang Patel1b75f442009-09-16 18:20:05 +00001423 /// from DILocation.
1424 DebugLoc ExtractDebugLocation(DILocation &Loc,
1425 DebugLocTracker &DebugLocInfo) {
1426 DebugLoc DL;
1427 MDNode *Context = Loc.getScope().getNode();
Devang Patela1434042009-10-01 01:15:28 +00001428 MDNode *InlinedLoc = NULL;
1429 if (!Loc.getOrigLocation().isNull())
1430 InlinedLoc = Loc.getOrigLocation().getNode();
Devang Patel1b75f442009-09-16 18:20:05 +00001431 // If this location is already tracked then use it.
Devang Patela1434042009-10-01 01:15:28 +00001432 DebugLocTuple Tuple(Context, InlinedLoc, Loc.getLineNumber(),
Daniel Dunbara279bc32009-09-20 02:20:51 +00001433 Loc.getColumnNumber());
Devang Patel1b75f442009-09-16 18:20:05 +00001434 DenseMap<DebugLocTuple, unsigned>::iterator II
1435 = DebugLocInfo.DebugIdMap.find(Tuple);
1436 if (II != DebugLocInfo.DebugIdMap.end())
1437 return DebugLoc::get(II->second);
1438
1439 // Add a new location entry.
1440 unsigned Id = DebugLocInfo.DebugLocations.size();
1441 DebugLocInfo.DebugLocations.push_back(Tuple);
1442 DebugLocInfo.DebugIdMap[Tuple] = Id;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001443
Devang Patel1b75f442009-09-16 18:20:05 +00001444 return DebugLoc::get(Id);
1445 }
1446
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001447 /// ExtractDebugLocation - Extract debug location information
Devang Patel9e529c32009-07-02 01:15:24 +00001448 /// from llvm.dbg.func_start intrinsic.
1449 DebugLoc ExtractDebugLocation(DbgFuncStartInst &FSI,
Devang Patel9e529c32009-07-02 01:15:24 +00001450 DebugLocTracker &DebugLocInfo) {
1451 DebugLoc DL;
1452 Value *SP = FSI.getSubprogram();
Devang Patel9e529c32009-07-02 01:15:24 +00001453
Devang Patele4b27562009-08-28 23:24:31 +00001454 DISubprogram Subprogram(cast<MDNode>(SP));
Devang Patel9e529c32009-07-02 01:15:24 +00001455 unsigned Line = Subprogram.getLineNumber();
1456 DICompileUnit CU(Subprogram.getCompileUnit());
1457
1458 // If this location is already tracked then use it.
Devang Patela1434042009-10-01 01:15:28 +00001459 DebugLocTuple Tuple(CU.getNode(), NULL, Line, /* Column */ 0);
Devang Patel9e529c32009-07-02 01:15:24 +00001460 DenseMap<DebugLocTuple, unsigned>::iterator II
1461 = DebugLocInfo.DebugIdMap.find(Tuple);
1462 if (II != DebugLocInfo.DebugIdMap.end())
1463 return DebugLoc::get(II->second);
1464
1465 // Add a new location entry.
1466 unsigned Id = DebugLocInfo.DebugLocations.size();
1467 DebugLocInfo.DebugLocations.push_back(Tuple);
1468 DebugLocInfo.DebugIdMap[Tuple] = Id;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001469
Devang Patel9e529c32009-07-02 01:15:24 +00001470 return DebugLoc::get(Id);
1471 }
1472
1473 /// isInlinedFnStart - Return true if FSI is starting an inlined function.
1474 bool isInlinedFnStart(DbgFuncStartInst &FSI, const Function *CurrentFn) {
Devang Patele4b27562009-08-28 23:24:31 +00001475 DISubprogram Subprogram(cast<MDNode>(FSI.getSubprogram()));
Devang Patel9e529c32009-07-02 01:15:24 +00001476 if (Subprogram.describes(CurrentFn))
1477 return false;
1478
1479 return true;
1480 }
1481
1482 /// isInlinedFnEnd - Return true if REI is ending an inlined function.
1483 bool isInlinedFnEnd(DbgRegionEndInst &REI, const Function *CurrentFn) {
Devang Patele4b27562009-08-28 23:24:31 +00001484 DISubprogram Subprogram(cast<MDNode>(REI.getContext()));
Devang Patel9e529c32009-07-02 01:15:24 +00001485 if (Subprogram.isNull() || Subprogram.describes(CurrentFn))
1486 return false;
1487
1488 return true;
1489 }
Torok Edwin620f2802008-12-16 09:07:36 +00001490}