blob: 1349f2778aabfd6ca12b0e906e7974741a869443 [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 Patel5ccdd102009-09-29 18:40:58 +000087 if (MDString *MDS = dyn_cast_or_null<MDString>(DbgNode->getElement(Elt)))
88 return MDS->getString().data();
Daniel Dunbarf612ff62009-09-19 20:40:05 +000089
Devang Patel5ccdd102009-09-29 18:40:58 +000090 return NULL;
Chris Lattnera45664f2008-11-10 02:56:27 +000091}
92
93uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +000094 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +000095 return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +000096
Devang Patele4b27562009-08-28 23:24:31 +000097 if (Elt < DbgNode->getNumElements())
98 if (ConstantInt *CI = dyn_cast<ConstantInt>(DbgNode->getElement(Elt)))
99 return CI->getZExtValue();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000100
Chris Lattnera45664f2008-11-10 02:56:27 +0000101 return 0;
102}
103
Chris Lattnera45664f2008-11-10 02:56:27 +0000104DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000105 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +0000106 return DIDescriptor();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000107
Devang Patele4b27562009-08-28 23:24:31 +0000108 if (Elt < DbgNode->getNumElements() && DbgNode->getElement(Elt))
109 return DIDescriptor(dyn_cast<MDNode>(DbgNode->getElement(Elt)));
110
111 return DIDescriptor();
Chris Lattnera45664f2008-11-10 02:56:27 +0000112}
113
114GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000115 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +0000116 return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000117
Devang Patele4b27562009-08-28 23:24:31 +0000118 if (Elt < DbgNode->getNumElements())
Bill Wendling26c6cf42009-10-08 20:52:51 +0000119 return dyn_cast_or_null<GlobalVariable>(DbgNode->getElement(Elt));
Devang Patele4b27562009-08-28 23:24:31 +0000120 return 0;
Chris Lattnera45664f2008-11-10 02:56:27 +0000121}
122
Chris Lattnera45664f2008-11-10 02:56:27 +0000123//===----------------------------------------------------------------------===//
Devang Patel6ceea332009-08-31 18:49:10 +0000124// Predicates
Chris Lattnera45664f2008-11-10 02:56:27 +0000125//===----------------------------------------------------------------------===//
126
Devang Patel6ceea332009-08-31 18:49:10 +0000127/// isBasicType - Return true if the specified tag is legal for
128/// DIBasicType.
129bool DIDescriptor::isBasicType() const {
Devang Patel5a685092009-08-31 20:27:49 +0000130 assert (!isNull() && "Invalid descriptor!");
Devang Patel6ceea332009-08-31 18:49:10 +0000131 unsigned Tag = getTag();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000132
Devang Patel6ceea332009-08-31 18:49:10 +0000133 return Tag == dwarf::DW_TAG_base_type;
Torok Edwinb07fbd92008-12-13 08:25:29 +0000134}
Chris Lattnera45664f2008-11-10 02:56:27 +0000135
Devang Patel6ceea332009-08-31 18:49:10 +0000136/// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
137bool DIDescriptor::isDerivedType() const {
Devang Patel5a685092009-08-31 20:27:49 +0000138 assert (!isNull() && "Invalid descriptor!");
Devang Patel6ceea332009-08-31 18:49:10 +0000139 unsigned Tag = getTag();
140
Chris Lattnera45664f2008-11-10 02:56:27 +0000141 switch (Tag) {
142 case dwarf::DW_TAG_typedef:
143 case dwarf::DW_TAG_pointer_type:
144 case dwarf::DW_TAG_reference_type:
145 case dwarf::DW_TAG_const_type:
146 case dwarf::DW_TAG_volatile_type:
147 case dwarf::DW_TAG_restrict_type:
148 case dwarf::DW_TAG_member:
149 case dwarf::DW_TAG_inheritance:
150 return true;
151 default:
Devang Patele4b27562009-08-28 23:24:31 +0000152 // CompositeTypes are currently modelled as DerivedTypes.
Devang Patel6ceea332009-08-31 18:49:10 +0000153 return isCompositeType();
Chris Lattnera45664f2008-11-10 02:56:27 +0000154 }
155}
156
Chris Lattnera45664f2008-11-10 02:56:27 +0000157/// isCompositeType - Return true if the specified tag is legal for
158/// DICompositeType.
Devang Patel6ceea332009-08-31 18:49:10 +0000159bool DIDescriptor::isCompositeType() const {
Devang Patel5a685092009-08-31 20:27:49 +0000160 assert (!isNull() && "Invalid descriptor!");
Devang Patel6ceea332009-08-31 18:49:10 +0000161 unsigned Tag = getTag();
162
163 switch (Tag) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000164 case dwarf::DW_TAG_array_type:
165 case dwarf::DW_TAG_structure_type:
166 case dwarf::DW_TAG_union_type:
167 case dwarf::DW_TAG_enumeration_type:
168 case dwarf::DW_TAG_vector_type:
169 case dwarf::DW_TAG_subroutine_type:
Devang Patel25cb0d72009-03-25 03:52:06 +0000170 case dwarf::DW_TAG_class_type:
Chris Lattnera45664f2008-11-10 02:56:27 +0000171 return true;
172 default:
173 return false;
174 }
175}
176
Chris Lattnera45664f2008-11-10 02:56:27 +0000177/// isVariable - Return true if the specified tag is legal for DIVariable.
Devang Patel6ceea332009-08-31 18:49:10 +0000178bool DIDescriptor::isVariable() const {
Devang Patel5a685092009-08-31 20:27:49 +0000179 assert (!isNull() && "Invalid descriptor!");
Devang Patel6ceea332009-08-31 18:49:10 +0000180 unsigned Tag = getTag();
181
Chris Lattnera45664f2008-11-10 02:56:27 +0000182 switch (Tag) {
183 case dwarf::DW_TAG_auto_variable:
184 case dwarf::DW_TAG_arg_variable:
185 case dwarf::DW_TAG_return_variable:
186 return true;
187 default:
188 return false;
189 }
190}
191
Devang Patelecbeb1a2009-09-30 22:34:41 +0000192/// isType - Return true if the specified tag is legal for DIType.
193bool DIDescriptor::isType() const {
194 return isBasicType() || isCompositeType() || isDerivedType();
195}
196
Devang Patel6ceea332009-08-31 18:49:10 +0000197/// isSubprogram - Return true if the specified tag is legal for
198/// DISubprogram.
199bool DIDescriptor::isSubprogram() const {
Devang Patel5a685092009-08-31 20:27:49 +0000200 assert (!isNull() && "Invalid descriptor!");
Devang Patel6ceea332009-08-31 18:49:10 +0000201 unsigned Tag = getTag();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000202
Devang Patel6ceea332009-08-31 18:49:10 +0000203 return Tag == dwarf::DW_TAG_subprogram;
204}
205
206/// isGlobalVariable - Return true if the specified tag is legal for
207/// DIGlobalVariable.
208bool DIDescriptor::isGlobalVariable() const {
Devang Patel5a685092009-08-31 20:27:49 +0000209 assert (!isNull() && "Invalid descriptor!");
Devang Patel6ceea332009-08-31 18:49:10 +0000210 unsigned Tag = getTag();
211
212 return Tag == dwarf::DW_TAG_variable;
213}
214
Devang Patelecbeb1a2009-09-30 22:34:41 +0000215/// isGlobal - Return true if the specified tag is legal for DIGlobal.
216bool DIDescriptor::isGlobal() const {
217 return isGlobalVariable();
218}
219
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000220/// isScope - Return true if the specified tag is one of the scope
Devang Patel43d98b32009-08-31 20:44:45 +0000221/// related tag.
222bool DIDescriptor::isScope() const {
223 assert (!isNull() && "Invalid descriptor!");
224 unsigned Tag = getTag();
225
226 switch (Tag) {
227 case dwarf::DW_TAG_compile_unit:
228 case dwarf::DW_TAG_lexical_block:
229 case dwarf::DW_TAG_subprogram:
230 return true;
231 default:
232 break;
233 }
234 return false;
235}
Devang Patel6ceea332009-08-31 18:49:10 +0000236
Devang Patelc9f322d2009-08-31 21:34:44 +0000237/// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
238bool DIDescriptor::isCompileUnit() const {
239 assert (!isNull() && "Invalid descriptor!");
240 unsigned Tag = getTag();
241
242 return Tag == dwarf::DW_TAG_compile_unit;
243}
244
Devang Patel5e005d82009-08-31 22:00:15 +0000245/// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
246bool DIDescriptor::isLexicalBlock() const {
247 assert (!isNull() && "Invalid descriptor!");
248 unsigned Tag = getTag();
249
250 return Tag == dwarf::DW_TAG_lexical_block;
251}
252
Devang Patelecbeb1a2009-09-30 22:34:41 +0000253/// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
254bool DIDescriptor::isSubrange() const {
255 assert (!isNull() && "Invalid descriptor!");
256 unsigned Tag = getTag();
257
258 return Tag == dwarf::DW_TAG_subrange_type;
259}
260
261/// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
262bool DIDescriptor::isEnumerator() const {
263 assert (!isNull() && "Invalid descriptor!");
264 unsigned Tag = getTag();
265
266 return Tag == dwarf::DW_TAG_enumerator;
267}
268
Devang Patel6ceea332009-08-31 18:49:10 +0000269//===----------------------------------------------------------------------===//
270// Simple Descriptor Constructors and other Methods
271//===----------------------------------------------------------------------===//
272
273DIType::DIType(MDNode *N) : DIDescriptor(N) {
274 if (!N) return;
275 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
276 DbgNode = 0;
277 }
278}
279
Devang Patel68afdc32009-01-05 18:33:01 +0000280unsigned DIArray::getNumElements() const {
Devang Patele4b27562009-08-28 23:24:31 +0000281 assert (DbgNode && "Invalid DIArray");
282 return DbgNode->getNumElements();
Devang Patel68afdc32009-01-05 18:33:01 +0000283}
Chris Lattnera45664f2008-11-10 02:56:27 +0000284
Devang Patelc4999d72009-07-22 18:23:44 +0000285/// replaceAllUsesWith - Replace all uses of debug info referenced by
286/// this descriptor. After this completes, the current debug info value
287/// is erased.
288void DIDerivedType::replaceAllUsesWith(DIDescriptor &D) {
289 if (isNull())
290 return;
291
Devang Patel6930f4f2009-07-22 18:56:16 +0000292 assert (!D.isNull() && "Can not replace with null");
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000293
294 // Since we use a TrackingVH for the node, its easy for clients to manufacture
295 // legitimate situations where they want to replaceAllUsesWith() on something
296 // which, due to uniquing, has merged with the source. We shield clients from
297 // this detail by allowing a value to be replaced with replaceAllUsesWith()
298 // itself.
299 if (getNode() != D.getNode()) {
300 MDNode *Node = DbgNode;
301 Node->replaceAllUsesWith(D.getNode());
302 delete Node;
303 }
Devang Patelc4999d72009-07-22 18:23:44 +0000304}
305
Devang Patelb79b5352009-01-19 23:21:49 +0000306/// Verify - Verify that a compile unit is well formed.
307bool DICompileUnit::Verify() const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000308 if (isNull())
Devang Patelb79b5352009-01-19 23:21:49 +0000309 return false;
Devang Patel5ccdd102009-09-29 18:40:58 +0000310 const char *N = getFilename();
311 if (!N)
Bill Wendling0582ae92009-03-13 04:39:26 +0000312 return false;
Devang Patelb79b5352009-01-19 23:21:49 +0000313 // It is possible that directory and produce string is empty.
Bill Wendling0582ae92009-03-13 04:39:26 +0000314 return true;
Devang Patelb79b5352009-01-19 23:21:49 +0000315}
316
317/// Verify - Verify that a type descriptor is well formed.
318bool DIType::Verify() const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000319 if (isNull())
Devang Patelb79b5352009-01-19 23:21:49 +0000320 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000321 if (getContext().isNull())
Devang Patelb79b5352009-01-19 23:21:49 +0000322 return false;
323
324 DICompileUnit CU = getCompileUnit();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000325 if (!CU.isNull() && !CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000326 return false;
327 return true;
328}
329
330/// Verify - Verify that a composite type descriptor is well formed.
331bool DICompositeType::Verify() const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000332 if (isNull())
Devang Patelb79b5352009-01-19 23:21:49 +0000333 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000334 if (getContext().isNull())
Devang Patelb79b5352009-01-19 23:21:49 +0000335 return false;
336
337 DICompileUnit CU = getCompileUnit();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000338 if (!CU.isNull() && !CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000339 return false;
340 return true;
341}
342
343/// Verify - Verify that a subprogram descriptor is well formed.
344bool DISubprogram::Verify() const {
345 if (isNull())
346 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000347
Devang Patelb79b5352009-01-19 23:21:49 +0000348 if (getContext().isNull())
349 return false;
350
351 DICompileUnit CU = getCompileUnit();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000352 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000353 return false;
354
355 DICompositeType Ty = getType();
356 if (!Ty.isNull() && !Ty.Verify())
357 return false;
358 return true;
359}
360
361/// Verify - Verify that a global variable descriptor is well formed.
362bool DIGlobalVariable::Verify() const {
363 if (isNull())
364 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000365
Devang Patelb79b5352009-01-19 23:21:49 +0000366 if (getContext().isNull())
367 return false;
368
369 DICompileUnit CU = getCompileUnit();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000370 if (!CU.isNull() && !CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000371 return false;
372
373 DIType Ty = getType();
374 if (!Ty.Verify())
375 return false;
376
377 if (!getGlobal())
378 return false;
379
380 return true;
381}
382
383/// Verify - Verify that a variable descriptor is well formed.
384bool DIVariable::Verify() const {
385 if (isNull())
386 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000387
Devang Patelb79b5352009-01-19 23:21:49 +0000388 if (getContext().isNull())
389 return false;
390
391 DIType Ty = getType();
392 if (!Ty.Verify())
393 return false;
394
Devang Patelb79b5352009-01-19 23:21:49 +0000395 return true;
396}
397
Devang Patel36375ee2009-02-17 21:23:59 +0000398/// getOriginalTypeSize - If this type is derived from a base type then
399/// return base type size.
400uint64_t DIDerivedType::getOriginalTypeSize() const {
401 if (getTag() != dwarf::DW_TAG_member)
402 return getSizeInBits();
403 DIType BT = getTypeDerivedFrom();
404 if (BT.getTag() != dwarf::DW_TAG_base_type)
405 return getSizeInBits();
406 return BT.getSizeInBits();
407}
Devang Patelb79b5352009-01-19 23:21:49 +0000408
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000409/// describes - Return true if this subprogram provides debugging
410/// information for the function F.
411bool DISubprogram::describes(const Function *F) {
412 assert (F && "Invalid function");
Devang Patel5ccdd102009-09-29 18:40:58 +0000413 const char *Name = getLinkageName();
414 if (!Name)
415 Name = getName();
416 if (strcmp(F->getName().data(), Name) == 0)
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000417 return true;
418 return false;
419}
420
Devang Patelecbeb1a2009-09-30 22:34:41 +0000421const char *DIScope::getFilename() const {
422 if (isLexicalBlock())
423 return DILexicalBlock(DbgNode).getFilename();
424 else if (isSubprogram())
425 return DISubprogram(DbgNode).getFilename();
426 else if (isCompileUnit())
427 return DICompileUnit(DbgNode).getFilename();
428 else
429 assert (0 && "Invalid DIScope!");
430 return NULL;
431}
432
433const char *DIScope::getDirectory() const {
434 if (isLexicalBlock())
435 return DILexicalBlock(DbgNode).getDirectory();
436 else if (isSubprogram())
437 return DISubprogram(DbgNode).getDirectory();
438 else if (isCompileUnit())
439 return DICompileUnit(DbgNode).getDirectory();
440 else
441 assert (0 && "Invalid DIScope!");
442 return NULL;
443}
444
Chris Lattnera45664f2008-11-10 02:56:27 +0000445//===----------------------------------------------------------------------===//
Devang Patel7136a652009-07-01 22:10:23 +0000446// DIDescriptor: dump routines for all descriptors.
447//===----------------------------------------------------------------------===//
448
449
450/// dump - Print descriptor.
451void DIDescriptor::dump() const {
Devang Patele4b27562009-08-28 23:24:31 +0000452 errs() << "[" << dwarf::TagString(getTag()) << "] ";
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000453 errs().write_hex((intptr_t) &*DbgNode) << ']';
Devang Patel7136a652009-07-01 22:10:23 +0000454}
455
456/// dump - Print compile unit.
457void DICompileUnit::dump() const {
458 if (getLanguage())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000459 errs() << " [" << dwarf::LanguageString(getLanguage()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000460
Devang Patel5ccdd102009-09-29 18:40:58 +0000461 errs() << " [" << getDirectory() << "/" << getFilename() << " ]";
Devang Patel7136a652009-07-01 22:10:23 +0000462}
463
464/// dump - Print type.
465void DIType::dump() const {
466 if (isNull()) return;
467
Devang Patel5ccdd102009-09-29 18:40:58 +0000468 if (const char *Res = getName())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000469 errs() << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000470
471 unsigned Tag = getTag();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000472 errs() << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000473
474 // TODO : Print context
475 getCompileUnit().dump();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000476 errs() << " ["
477 << getLineNumber() << ", "
Chris Lattnera81d29b2009-08-23 07:33:14 +0000478 << getSizeInBits() << ", "
479 << getAlignInBits() << ", "
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000480 << getOffsetInBits()
Chris Lattnera81d29b2009-08-23 07:33:14 +0000481 << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000482
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000483 if (isPrivate())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000484 errs() << " [private] ";
Devang Patel7136a652009-07-01 22:10:23 +0000485 else if (isProtected())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000486 errs() << " [protected] ";
Devang Patel7136a652009-07-01 22:10:23 +0000487
488 if (isForwardDecl())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000489 errs() << " [fwd] ";
Devang Patel7136a652009-07-01 22:10:23 +0000490
Devang Patel6ceea332009-08-31 18:49:10 +0000491 if (isBasicType())
Devang Patele4b27562009-08-28 23:24:31 +0000492 DIBasicType(DbgNode).dump();
Devang Patel6ceea332009-08-31 18:49:10 +0000493 else if (isDerivedType())
Devang Patele4b27562009-08-28 23:24:31 +0000494 DIDerivedType(DbgNode).dump();
Devang Patel6ceea332009-08-31 18:49:10 +0000495 else if (isCompositeType())
Devang Patele4b27562009-08-28 23:24:31 +0000496 DICompositeType(DbgNode).dump();
Devang Patel7136a652009-07-01 22:10:23 +0000497 else {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000498 errs() << "Invalid DIType\n";
Devang Patel7136a652009-07-01 22:10:23 +0000499 return;
500 }
501
Chris Lattnera81d29b2009-08-23 07:33:14 +0000502 errs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000503}
504
505/// dump - Print basic type.
506void DIBasicType::dump() const {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000507 errs() << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000508}
509
510/// dump - Print derived type.
511void DIDerivedType::dump() const {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000512 errs() << "\n\t Derived From: "; getTypeDerivedFrom().dump();
Devang Patel7136a652009-07-01 22:10:23 +0000513}
514
515/// dump - Print composite type.
516void DICompositeType::dump() const {
517 DIArray A = getTypeArray();
518 if (A.isNull())
519 return;
Chris Lattnera81d29b2009-08-23 07:33:14 +0000520 errs() << " [" << A.getNumElements() << " elements]";
Devang Patel7136a652009-07-01 22:10:23 +0000521}
522
523/// dump - Print global.
524void DIGlobal::dump() const {
Devang Patel5ccdd102009-09-29 18:40:58 +0000525 if (const char *Res = getName())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000526 errs() << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000527
528 unsigned Tag = getTag();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000529 errs() << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000530
531 // TODO : Print context
532 getCompileUnit().dump();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000533 errs() << " [" << getLineNumber() << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000534
535 if (isLocalToUnit())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000536 errs() << " [local] ";
Devang Patel7136a652009-07-01 22:10:23 +0000537
538 if (isDefinition())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000539 errs() << " [def] ";
Devang Patel7136a652009-07-01 22:10:23 +0000540
Devang Patel6ceea332009-08-31 18:49:10 +0000541 if (isGlobalVariable())
Devang Patele4b27562009-08-28 23:24:31 +0000542 DIGlobalVariable(DbgNode).dump();
Devang Patel7136a652009-07-01 22:10:23 +0000543
Chris Lattnera81d29b2009-08-23 07:33:14 +0000544 errs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000545}
546
547/// dump - Print subprogram.
548void DISubprogram::dump() const {
Devang Patel5ccdd102009-09-29 18:40:58 +0000549 if (const char *Res = getName())
Devang Patel82dfc0c2009-08-31 22:47:13 +0000550 errs() << " [" << Res << "] ";
551
552 unsigned Tag = getTag();
553 errs() << " [" << dwarf::TagString(Tag) << "] ";
554
555 // TODO : Print context
556 getCompileUnit().dump();
557 errs() << " [" << getLineNumber() << "] ";
558
559 if (isLocalToUnit())
560 errs() << " [local] ";
561
562 if (isDefinition())
563 errs() << " [def] ";
564
565 errs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000566}
567
568/// dump - Print global variable.
569void DIGlobalVariable::dump() const {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000570 errs() << " [";
571 getGlobal()->dump();
572 errs() << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000573}
574
575/// dump - Print variable.
576void DIVariable::dump() const {
Devang Patel5ccdd102009-09-29 18:40:58 +0000577 if (const char *Res = getName())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000578 errs() << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000579
580 getCompileUnit().dump();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000581 errs() << " [" << getLineNumber() << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000582 getType().dump();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000583 errs() << "\n";
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000584
585 // FIXME: Dump complex addresses
Devang Patel7136a652009-07-01 22:10:23 +0000586}
587
588//===----------------------------------------------------------------------===//
Chris Lattnera45664f2008-11-10 02:56:27 +0000589// DIFactory: Basic Helpers
590//===----------------------------------------------------------------------===//
591
Bill Wendlingdc817b62009-05-14 18:26:15 +0000592DIFactory::DIFactory(Module &m)
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000593 : M(m), VMContext(M.getContext()), StopPointFn(0), FuncStartFn(0),
Owen Anderson99035272009-07-07 17:12:53 +0000594 RegionStartFn(0), RegionEndFn(0),
Bill Wendlingdc817b62009-05-14 18:26:15 +0000595 DeclareFn(0) {
Owen Andersond7f2a6c2009-08-05 23:16:16 +0000596 EmptyStructPtr = PointerType::getUnqual(StructType::get(VMContext));
Chris Lattner497a7a82008-11-10 04:10:34 +0000597}
598
Chris Lattnera45664f2008-11-10 02:56:27 +0000599Constant *DIFactory::GetTagConstant(unsigned TAG) {
Devang Patel6906ba52009-01-20 19:22:03 +0000600 assert((TAG & LLVMDebugVersionMask) == 0 &&
Chris Lattnera45664f2008-11-10 02:56:27 +0000601 "Tag too large for debug encoding!");
Owen Anderson1d0be152009-08-13 21:58:54 +0000602 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
Chris Lattnera45664f2008-11-10 02:56:27 +0000603}
604
Chris Lattnera45664f2008-11-10 02:56:27 +0000605//===----------------------------------------------------------------------===//
606// DIFactory: Primary Constructors
607//===----------------------------------------------------------------------===//
608
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000609/// GetOrCreateArray - Create an descriptor for an array of descriptors.
Chris Lattnera45664f2008-11-10 02:56:27 +0000610/// This implicitly uniques the arrays created.
611DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
Devang Patele4b27562009-08-28 23:24:31 +0000612 SmallVector<Value*, 16> Elts;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000613
Devang Patele4b27562009-08-28 23:24:31 +0000614 if (NumTys == 0)
615 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)));
616 else
617 for (unsigned i = 0; i != NumTys; ++i)
618 Elts.push_back(Tys[i].getNode());
Devang Patel82459882009-08-26 05:01:18 +0000619
Devang Patele4b27562009-08-28 23:24:31 +0000620 return DIArray(MDNode::get(VMContext,Elts.data(), Elts.size()));
Chris Lattnera45664f2008-11-10 02:56:27 +0000621}
622
623/// GetOrCreateSubrange - Create a descriptor for a value range. This
624/// implicitly uniques the values returned.
625DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
Devang Patele4b27562009-08-28 23:24:31 +0000626 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000627 GetTagConstant(dwarf::DW_TAG_subrange_type),
Owen Anderson1d0be152009-08-13 21:58:54 +0000628 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
629 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
Chris Lattnera45664f2008-11-10 02:56:27 +0000630 };
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000631
Devang Patele4b27562009-08-28 23:24:31 +0000632 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000633}
634
635
636
637/// CreateCompileUnit - Create a new descriptor for the specified compile
638/// unit. Note that this does not unique compile units within the module.
639DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
Devang Patel5ccdd102009-09-29 18:40:58 +0000640 StringRef Filename,
641 StringRef Directory,
642 StringRef Producer,
Devang Pateldd9db662009-01-30 18:20:31 +0000643 bool isMain,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000644 bool isOptimized,
Devang Patel13319ce2009-02-17 22:43:44 +0000645 const char *Flags,
646 unsigned RunTimeVer) {
Devang Patele4b27562009-08-28 23:24:31 +0000647 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000648 GetTagConstant(dwarf::DW_TAG_compile_unit),
Devang Patele4b27562009-08-28 23:24:31 +0000649 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Owen Anderson1d0be152009-08-13 21:58:54 +0000650 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
Devang Patele4b27562009-08-28 23:24:31 +0000651 MDString::get(VMContext, Filename),
652 MDString::get(VMContext, Directory),
653 MDString::get(VMContext, Producer),
Owen Anderson1d0be152009-08-13 21:58:54 +0000654 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
655 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
Devang Patele4b27562009-08-28 23:24:31 +0000656 MDString::get(VMContext, Flags),
Owen Anderson1d0be152009-08-13 21:58:54 +0000657 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
Chris Lattnera45664f2008-11-10 02:56:27 +0000658 };
Devang Patele4b27562009-08-28 23:24:31 +0000659
660 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000661}
662
663/// CreateEnumerator - Create a single enumerator value.
Devang Patel5ccdd102009-09-29 18:40:58 +0000664DIEnumerator DIFactory::CreateEnumerator(StringRef Name, uint64_t Val){
Devang Patele4b27562009-08-28 23:24:31 +0000665 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000666 GetTagConstant(dwarf::DW_TAG_enumerator),
Devang Patele4b27562009-08-28 23:24:31 +0000667 MDString::get(VMContext, Name),
Owen Anderson1d0be152009-08-13 21:58:54 +0000668 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
Chris Lattnera45664f2008-11-10 02:56:27 +0000669 };
Devang Patele4b27562009-08-28 23:24:31 +0000670 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000671}
672
673
674/// CreateBasicType - Create a basic type like int, float, etc.
675DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
Devang Patel5ccdd102009-09-29 18:40:58 +0000676 StringRef Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000677 DICompileUnit CompileUnit,
678 unsigned LineNumber,
679 uint64_t SizeInBits,
680 uint64_t AlignInBits,
681 uint64_t OffsetInBits, unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000682 unsigned Encoding) {
Devang Patele4b27562009-08-28 23:24:31 +0000683 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000684 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patele4b27562009-08-28 23:24:31 +0000685 Context.getNode(),
686 MDString::get(VMContext, Name),
687 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000688 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
689 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
690 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
691 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
692 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
693 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
Chris Lattnera45664f2008-11-10 02:56:27 +0000694 };
Devang Patele4b27562009-08-28 23:24:31 +0000695 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000696}
697
Devang Patelac16d442009-10-26 16:54:35 +0000698
699/// CreateBasicType - Create a basic type like int, float, etc.
700DIBasicType DIFactory::CreateBasicTypeEx(DIDescriptor Context,
701 StringRef Name,
702 DICompileUnit CompileUnit,
703 unsigned LineNumber,
704 Constant *SizeInBits,
705 Constant *AlignInBits,
706 Constant *OffsetInBits, unsigned Flags,
707 unsigned Encoding) {
708 Value *Elts[] = {
709 GetTagConstant(dwarf::DW_TAG_base_type),
710 Context.getNode(),
711 MDString::get(VMContext, Name),
712 CompileUnit.getNode(),
713 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
714 SizeInBits,
715 AlignInBits,
716 OffsetInBits,
717 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
718 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
719 };
720 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
721}
722
723
Chris Lattnera45664f2008-11-10 02:56:27 +0000724/// CreateDerivedType - Create a derived type like const qualified type,
725/// pointer, typedef, etc.
726DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
727 DIDescriptor Context,
Devang Patel5ccdd102009-09-29 18:40:58 +0000728 StringRef Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000729 DICompileUnit CompileUnit,
730 unsigned LineNumber,
731 uint64_t SizeInBits,
732 uint64_t AlignInBits,
733 uint64_t OffsetInBits,
734 unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000735 DIType DerivedFrom) {
Devang Patele4b27562009-08-28 23:24:31 +0000736 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000737 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000738 Context.getNode(),
739 MDString::get(VMContext, Name),
740 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000741 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
742 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
743 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
744 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
745 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patele4b27562009-08-28 23:24:31 +0000746 DerivedFrom.getNode(),
Chris Lattnera45664f2008-11-10 02:56:27 +0000747 };
Devang Patele4b27562009-08-28 23:24:31 +0000748 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000749}
750
Devang Patelac16d442009-10-26 16:54:35 +0000751
752/// CreateDerivedType - Create a derived type like const qualified type,
753/// pointer, typedef, etc.
754DIDerivedType DIFactory::CreateDerivedTypeEx(unsigned Tag,
755 DIDescriptor Context,
756 StringRef Name,
757 DICompileUnit CompileUnit,
758 unsigned LineNumber,
759 Constant *SizeInBits,
760 Constant *AlignInBits,
761 Constant *OffsetInBits,
762 unsigned Flags,
763 DIType DerivedFrom) {
764 Value *Elts[] = {
765 GetTagConstant(Tag),
766 Context.getNode(),
767 MDString::get(VMContext, Name),
768 CompileUnit.getNode(),
769 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
770 SizeInBits,
771 AlignInBits,
772 OffsetInBits,
773 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
774 DerivedFrom.getNode(),
775 };
776 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
777}
778
779
Chris Lattnera45664f2008-11-10 02:56:27 +0000780/// CreateCompositeType - Create a composite type like array, struct, etc.
781DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
782 DIDescriptor Context,
Devang Patel5ccdd102009-09-29 18:40:58 +0000783 StringRef Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000784 DICompileUnit CompileUnit,
785 unsigned LineNumber,
786 uint64_t SizeInBits,
787 uint64_t AlignInBits,
788 uint64_t OffsetInBits,
789 unsigned Flags,
790 DIType DerivedFrom,
Devang Patel13319ce2009-02-17 22:43:44 +0000791 DIArray Elements,
792 unsigned RuntimeLang) {
Owen Andersone277fed2009-07-07 16:31:25 +0000793
Devang Patele4b27562009-08-28 23:24:31 +0000794 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000795 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000796 Context.getNode(),
797 MDString::get(VMContext, Name),
798 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000799 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
800 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
801 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
802 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
803 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patele4b27562009-08-28 23:24:31 +0000804 DerivedFrom.getNode(),
805 Elements.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000806 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
Chris Lattnera45664f2008-11-10 02:56:27 +0000807 };
Devang Patele4b27562009-08-28 23:24:31 +0000808 return DICompositeType(MDNode::get(VMContext, &Elts[0], 12));
Chris Lattnera45664f2008-11-10 02:56:27 +0000809}
810
811
Devang Patelac16d442009-10-26 16:54:35 +0000812/// CreateCompositeType - Create a composite type like array, struct, etc.
813DICompositeType DIFactory::CreateCompositeTypeEx(unsigned Tag,
814 DIDescriptor Context,
815 StringRef Name,
816 DICompileUnit CompileUnit,
817 unsigned LineNumber,
818 Constant *SizeInBits,
819 Constant *AlignInBits,
820 Constant *OffsetInBits,
821 unsigned Flags,
822 DIType DerivedFrom,
823 DIArray Elements,
824 unsigned RuntimeLang) {
825
826 Value *Elts[] = {
827 GetTagConstant(Tag),
828 Context.getNode(),
829 MDString::get(VMContext, Name),
830 CompileUnit.getNode(),
831 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
832 SizeInBits,
833 AlignInBits,
834 OffsetInBits,
835 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
836 DerivedFrom.getNode(),
837 Elements.getNode(),
838 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
839 };
840 return DICompositeType(MDNode::get(VMContext, &Elts[0], 12));
841}
842
843
Chris Lattnera45664f2008-11-10 02:56:27 +0000844/// CreateSubprogram - Create a new descriptor for the specified subprogram.
845/// See comments in DISubprogram for descriptions of these fields. This
846/// method does not unique the generated descriptors.
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000847DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
Devang Patel5ccdd102009-09-29 18:40:58 +0000848 StringRef Name,
849 StringRef DisplayName,
850 StringRef LinkageName,
Chris Lattnera45664f2008-11-10 02:56:27 +0000851 DICompileUnit CompileUnit,
852 unsigned LineNo, DIType Type,
853 bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000854 bool isDefinition) {
Devang Patel854967e2008-12-17 22:39:29 +0000855
Devang Patele4b27562009-08-28 23:24:31 +0000856 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000857 GetTagConstant(dwarf::DW_TAG_subprogram),
Devang Patele4b27562009-08-28 23:24:31 +0000858 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
859 Context.getNode(),
860 MDString::get(VMContext, Name),
861 MDString::get(VMContext, DisplayName),
862 MDString::get(VMContext, LinkageName),
863 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000864 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000865 Type.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000866 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
867 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition)
Chris Lattnera45664f2008-11-10 02:56:27 +0000868 };
Devang Patele4b27562009-08-28 23:24:31 +0000869 return DISubprogram(MDNode::get(VMContext, &Elts[0], 11));
Chris Lattnera45664f2008-11-10 02:56:27 +0000870}
871
872/// CreateGlobalVariable - Create a new descriptor for the specified global.
873DIGlobalVariable
Devang Patel5ccdd102009-09-29 18:40:58 +0000874DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
875 StringRef DisplayName,
876 StringRef LinkageName,
Chris Lattnera45664f2008-11-10 02:56:27 +0000877 DICompileUnit CompileUnit,
878 unsigned LineNo, DIType Type,bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000879 bool isDefinition, llvm::GlobalVariable *Val) {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000880 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000881 GetTagConstant(dwarf::DW_TAG_variable),
Devang Patele4b27562009-08-28 23:24:31 +0000882 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
883 Context.getNode(),
884 MDString::get(VMContext, Name),
885 MDString::get(VMContext, DisplayName),
886 MDString::get(VMContext, LinkageName),
887 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000888 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000889 Type.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000890 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
891 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
Devang Patele4b27562009-08-28 23:24:31 +0000892 Val
Chris Lattnera45664f2008-11-10 02:56:27 +0000893 };
Devang Patele4b27562009-08-28 23:24:31 +0000894
895 Value *const *Vs = &Elts[0];
896 MDNode *Node = MDNode::get(VMContext,Vs, 12);
897
898 // Create a named metadata so that we do not lose this mdnode.
899 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
900 NMD->addElement(Node);
901
902 return DIGlobalVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +0000903}
904
905
906/// CreateVariable - Create a new descriptor for the specified variable.
907DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
Devang Patel5ccdd102009-09-29 18:40:58 +0000908 StringRef Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000909 DICompileUnit CompileUnit, unsigned LineNo,
Devang Pateldd9db662009-01-30 18:20:31 +0000910 DIType Type) {
Devang Patele4b27562009-08-28 23:24:31 +0000911 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000912 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000913 Context.getNode(),
914 MDString::get(VMContext, Name),
915 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000916 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000917 Type.getNode(),
Chris Lattnera45664f2008-11-10 02:56:27 +0000918 };
Devang Patele4b27562009-08-28 23:24:31 +0000919 return DIVariable(MDNode::get(VMContext, &Elts[0], 6));
Chris Lattnera45664f2008-11-10 02:56:27 +0000920}
921
922
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000923/// CreateComplexVariable - Create a new descriptor for the specified variable
924/// which has a complex address expression for its address.
925DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
926 const std::string &Name,
927 DICompileUnit CompileUnit,
928 unsigned LineNo,
929 DIType Type, SmallVector<Value *, 9> &addr) {
930 SmallVector<Value *, 9> Elts;
931 Elts.push_back(GetTagConstant(Tag));
932 Elts.push_back(Context.getNode());
933 Elts.push_back(MDString::get(VMContext, Name));
934 Elts.push_back(CompileUnit.getNode());
935 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
936 Elts.push_back(Type.getNode());
937 Elts.insert(Elts.end(), addr.begin(), addr.end());
938
939 return DIVariable(MDNode::get(VMContext, &Elts[0], 6+addr.size()));
940}
941
942
Chris Lattnera45664f2008-11-10 02:56:27 +0000943/// CreateBlock - This creates a descriptor for a lexical block with the
Owen Anderson99035272009-07-07 17:12:53 +0000944/// specified parent VMContext.
Devang Patel5e005d82009-08-31 22:00:15 +0000945DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context) {
Devang Patele4b27562009-08-28 23:24:31 +0000946 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000947 GetTagConstant(dwarf::DW_TAG_lexical_block),
Devang Patele4b27562009-08-28 23:24:31 +0000948 Context.getNode()
Chris Lattnera45664f2008-11-10 02:56:27 +0000949 };
Devang Patel5e005d82009-08-31 22:00:15 +0000950 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 2));
Chris Lattnera45664f2008-11-10 02:56:27 +0000951}
952
Devang Patelf98d8fe2009-09-01 01:14:15 +0000953/// CreateLocation - Creates a debug info location.
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000954DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
Daniel Dunbara279bc32009-09-20 02:20:51 +0000955 DIScope S, DILocation OrigLoc) {
Devang Patelf98d8fe2009-09-01 01:14:15 +0000956 Value *Elts[] = {
957 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
958 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
959 S.getNode(),
960 OrigLoc.getNode(),
961 };
962 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
963}
964
Chris Lattnera45664f2008-11-10 02:56:27 +0000965
966//===----------------------------------------------------------------------===//
967// DIFactory: Routines for inserting code into a function
968//===----------------------------------------------------------------------===//
969
970/// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
971/// inserting it at the end of the specified basic block.
972void DIFactory::InsertStopPoint(DICompileUnit CU, unsigned LineNo,
973 unsigned ColNo, BasicBlock *BB) {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000974
Chris Lattnera45664f2008-11-10 02:56:27 +0000975 // Lazily construct llvm.dbg.stoppoint function.
976 if (!StopPointFn)
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000977 StopPointFn = llvm::Intrinsic::getDeclaration(&M,
Chris Lattnera45664f2008-11-10 02:56:27 +0000978 llvm::Intrinsic::dbg_stoppoint);
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000979
Chris Lattnera45664f2008-11-10 02:56:27 +0000980 // Invoke llvm.dbg.stoppoint
981 Value *Args[] = {
Owen Anderson1d0be152009-08-13 21:58:54 +0000982 ConstantInt::get(llvm::Type::getInt32Ty(VMContext), LineNo),
983 ConstantInt::get(llvm::Type::getInt32Ty(VMContext), ColNo),
Devang Patele4b27562009-08-28 23:24:31 +0000984 CU.getNode()
Chris Lattnera45664f2008-11-10 02:56:27 +0000985 };
986 CallInst::Create(StopPointFn, Args, Args+3, "", BB);
987}
988
989/// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
990/// mark the start of the specified subprogram.
991void DIFactory::InsertSubprogramStart(DISubprogram SP, BasicBlock *BB) {
992 // Lazily construct llvm.dbg.func.start.
993 if (!FuncStartFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000994 FuncStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_func_start);
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000995
Chris Lattnera45664f2008-11-10 02:56:27 +0000996 // Call llvm.dbg.func.start which also implicitly sets a stoppoint.
Devang Patele4b27562009-08-28 23:24:31 +0000997 CallInst::Create(FuncStartFn, SP.getNode(), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000998}
999
1000/// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
1001/// mark the start of a region for the specified scoping descriptor.
1002void DIFactory::InsertRegionStart(DIDescriptor D, BasicBlock *BB) {
1003 // Lazily construct llvm.dbg.region.start function.
1004 if (!RegionStartFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +00001005 RegionStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_start);
1006
Chris Lattnera45664f2008-11-10 02:56:27 +00001007 // Call llvm.dbg.func.start.
Devang Patele4b27562009-08-28 23:24:31 +00001008 CallInst::Create(RegionStartFn, D.getNode(), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +00001009}
1010
Chris Lattnera45664f2008-11-10 02:56:27 +00001011/// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
1012/// mark the end of a region for the specified scoping descriptor.
1013void DIFactory::InsertRegionEnd(DIDescriptor D, BasicBlock *BB) {
1014 // Lazily construct llvm.dbg.region.end function.
1015 if (!RegionEndFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +00001016 RegionEndFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_end);
1017
1018 // Call llvm.dbg.region.end.
Devang Patele4b27562009-08-28 23:24:31 +00001019 CallInst::Create(RegionEndFn, D.getNode(), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +00001020}
1021
1022/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Mike Stumpe4250392009-10-01 22:08:58 +00001023void DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1024 Instruction *InsertBefore) {
Chris Lattnera45664f2008-11-10 02:56:27 +00001025 // Cast the storage to a {}* for the call to llvm.dbg.declare.
Mike Stumpe4250392009-10-01 22:08:58 +00001026 Storage = new BitCastInst(Storage, EmptyStructPtr, "", InsertBefore);
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001027
Chris Lattnera45664f2008-11-10 02:56:27 +00001028 if (!DeclareFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +00001029 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1030
Devang Patele4b27562009-08-28 23:24:31 +00001031 Value *Args[] = { Storage, D.getNode() };
Mike Stumpe4250392009-10-01 22:08:58 +00001032 CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
1033}
1034
1035/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1036void DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1037 BasicBlock *InsertAtEnd) {
1038 // Cast the storage to a {}* for the call to llvm.dbg.declare.
1039 Storage = new BitCastInst(Storage, EmptyStructPtr, "", InsertAtEnd);
1040
1041 if (!DeclareFn)
1042 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1043
1044 Value *Args[] = { Storage, D.getNode() };
1045 CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);
Chris Lattnera45664f2008-11-10 02:56:27 +00001046}
Torok Edwin620f2802008-12-16 09:07:36 +00001047
Devang Patele4b27562009-08-28 23:24:31 +00001048
Devang Pateld2f79a12009-07-28 19:55:13 +00001049//===----------------------------------------------------------------------===//
Devang Patel98c65172009-07-30 18:25:15 +00001050// DebugInfoFinder implementations.
Devang Pateld2f79a12009-07-28 19:55:13 +00001051//===----------------------------------------------------------------------===//
1052
Devang Patel98c65172009-07-30 18:25:15 +00001053/// processModule - Process entire module and collect debug info.
1054void DebugInfoFinder::processModule(Module &M) {
Devang Patele4b27562009-08-28 23:24:31 +00001055
Devang Patelbeab41b2009-10-07 22:04:08 +00001056#ifdef ATTACH_DEBUG_INFO_TO_AN_INSN
1057 MetadataContext &TheMetadata = M.getContext().getMetadata();
1058 unsigned MDDbgKind = TheMetadata.getMDKind("dbg");
Devang Patelbeab41b2009-10-07 22:04:08 +00001059#endif
Devang Pateld2f79a12009-07-28 19:55:13 +00001060 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1061 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
1062 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
1063 ++BI) {
1064 if (DbgStopPointInst *SPI = dyn_cast<DbgStopPointInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +00001065 processStopPoint(SPI);
Devang Pateld2f79a12009-07-28 19:55:13 +00001066 else if (DbgFuncStartInst *FSI = dyn_cast<DbgFuncStartInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +00001067 processFuncStart(FSI);
Devang Patele802f1c2009-07-30 17:30:23 +00001068 else if (DbgRegionStartInst *DRS = dyn_cast<DbgRegionStartInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +00001069 processRegionStart(DRS);
Devang Patele802f1c2009-07-30 17:30:23 +00001070 else if (DbgRegionEndInst *DRE = dyn_cast<DbgRegionEndInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +00001071 processRegionEnd(DRE);
Devang Patelb4d31302009-07-31 18:18:52 +00001072 else if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1073 processDeclare(DDI);
Devang Patelbeab41b2009-10-07 22:04:08 +00001074#ifdef ATTACH_DEBUG_INFO_TO_AN_INSN
Devang Patel549e81f2009-10-13 17:35:35 +00001075 else if (MDDbgKind) {
1076 if (MDNode *L = TheMetadata.getMD(MDDbgKind, BI)) {
1077 DILocation Loc(L);
1078 DIScope S(Loc.getScope().getNode());
1079 if (S.isCompileUnit())
1080 addCompileUnit(DICompileUnit(S.getNode()));
1081 else if (S.isSubprogram())
1082 processSubprogram(DISubprogram(S.getNode()));
1083 else if (S.isLexicalBlock())
1084 processLexicalBlock(DILexicalBlock(S.getNode()));
1085 }
Devang Patelbeab41b2009-10-07 22:04:08 +00001086 }
1087#endif
Devang Pateld2f79a12009-07-28 19:55:13 +00001088 }
Devang Patele4b27562009-08-28 23:24:31 +00001089
1090 NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv");
1091 if (!NMD)
1092 return;
1093
1094 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
1095 DIGlobalVariable DIG(cast<MDNode>(NMD->getElement(i)));
Devang Pateld2f79a12009-07-28 19:55:13 +00001096 if (addGlobalVariable(DIG)) {
1097 addCompileUnit(DIG.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +00001098 processType(DIG.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +00001099 }
1100 }
1101}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001102
Devang Patel98c65172009-07-30 18:25:15 +00001103/// processType - Process DIType.
1104void DebugInfoFinder::processType(DIType DT) {
Devang Patel72bcdb62009-08-10 22:09:58 +00001105 if (!addType(DT))
Devang Pateld2f79a12009-07-28 19:55:13 +00001106 return;
1107
1108 addCompileUnit(DT.getCompileUnit());
Devang Patel6ceea332009-08-31 18:49:10 +00001109 if (DT.isCompositeType()) {
Devang Patele4b27562009-08-28 23:24:31 +00001110 DICompositeType DCT(DT.getNode());
Devang Patel98c65172009-07-30 18:25:15 +00001111 processType(DCT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001112 DIArray DA = DCT.getTypeArray();
1113 if (!DA.isNull())
1114 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1115 DIDescriptor D = DA.getElement(i);
Devang Patele4b27562009-08-28 23:24:31 +00001116 DIType TypeE = DIType(D.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001117 if (!TypeE.isNull())
Devang Patel98c65172009-07-30 18:25:15 +00001118 processType(TypeE);
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001119 else
Devang Patele4b27562009-08-28 23:24:31 +00001120 processSubprogram(DISubprogram(D.getNode()));
Devang Pateld2f79a12009-07-28 19:55:13 +00001121 }
Devang Patel6ceea332009-08-31 18:49:10 +00001122 } else if (DT.isDerivedType()) {
Devang Patele4b27562009-08-28 23:24:31 +00001123 DIDerivedType DDT(DT.getNode());
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001124 if (!DDT.isNull())
Devang Patel98c65172009-07-30 18:25:15 +00001125 processType(DDT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001126 }
1127}
1128
Devang Patelbeab41b2009-10-07 22:04:08 +00001129/// processLexicalBlock
1130void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
1131 if (LB.isNull())
1132 return;
1133 DIScope Context = LB.getContext();
1134 if (Context.isLexicalBlock())
1135 return processLexicalBlock(DILexicalBlock(Context.getNode()));
1136 else
1137 return processSubprogram(DISubprogram(Context.getNode()));
1138}
1139
Devang Patel98c65172009-07-30 18:25:15 +00001140/// processSubprogram - Process DISubprogram.
1141void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Patele802f1c2009-07-30 17:30:23 +00001142 if (SP.isNull())
1143 return;
Devang Pateld2f79a12009-07-28 19:55:13 +00001144 if (!addSubprogram(SP))
1145 return;
1146 addCompileUnit(SP.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +00001147 processType(SP.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +00001148}
1149
Devang Patel98c65172009-07-30 18:25:15 +00001150/// processStopPoint - Process DbgStopPointInst.
1151void DebugInfoFinder::processStopPoint(DbgStopPointInst *SPI) {
Devang Patele4b27562009-08-28 23:24:31 +00001152 MDNode *Context = dyn_cast<MDNode>(SPI->getContext());
Devang Pateld2f79a12009-07-28 19:55:13 +00001153 addCompileUnit(DICompileUnit(Context));
1154}
1155
Devang Patel98c65172009-07-30 18:25:15 +00001156/// processFuncStart - Process DbgFuncStartInst.
1157void DebugInfoFinder::processFuncStart(DbgFuncStartInst *FSI) {
Devang Patele4b27562009-08-28 23:24:31 +00001158 MDNode *SP = dyn_cast<MDNode>(FSI->getSubprogram());
Devang Patel98c65172009-07-30 18:25:15 +00001159 processSubprogram(DISubprogram(SP));
Devang Pateld2f79a12009-07-28 19:55:13 +00001160}
1161
Devang Patel98c65172009-07-30 18:25:15 +00001162/// processRegionStart - Process DbgRegionStart.
1163void DebugInfoFinder::processRegionStart(DbgRegionStartInst *DRS) {
Devang Patele4b27562009-08-28 23:24:31 +00001164 MDNode *SP = dyn_cast<MDNode>(DRS->getContext());
Devang Patel98c65172009-07-30 18:25:15 +00001165 processSubprogram(DISubprogram(SP));
Devang Patele802f1c2009-07-30 17:30:23 +00001166}
1167
Devang Patel98c65172009-07-30 18:25:15 +00001168/// processRegionEnd - Process DbgRegionEnd.
1169void DebugInfoFinder::processRegionEnd(DbgRegionEndInst *DRE) {
Devang Patele4b27562009-08-28 23:24:31 +00001170 MDNode *SP = dyn_cast<MDNode>(DRE->getContext());
Devang Patel98c65172009-07-30 18:25:15 +00001171 processSubprogram(DISubprogram(SP));
Devang Patele802f1c2009-07-30 17:30:23 +00001172}
1173
Devang Patelb4d31302009-07-31 18:18:52 +00001174/// processDeclare - Process DbgDeclareInst.
1175void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
Devang Patele4b27562009-08-28 23:24:31 +00001176 DIVariable DV(cast<MDNode>(DDI->getVariable()));
Devang Patelb4d31302009-07-31 18:18:52 +00001177 if (DV.isNull())
1178 return;
1179
Devang Patele4b27562009-08-28 23:24:31 +00001180 if (!NodesSeen.insert(DV.getNode()))
Devang Patelb4d31302009-07-31 18:18:52 +00001181 return;
1182
1183 addCompileUnit(DV.getCompileUnit());
1184 processType(DV.getType());
1185}
1186
Devang Patel72bcdb62009-08-10 22:09:58 +00001187/// addType - Add type into Tys.
1188bool DebugInfoFinder::addType(DIType DT) {
1189 if (DT.isNull())
1190 return false;
1191
Devang Patele4b27562009-08-28 23:24:31 +00001192 if (!NodesSeen.insert(DT.getNode()))
Devang Patel72bcdb62009-08-10 22:09:58 +00001193 return false;
1194
Devang Patele4b27562009-08-28 23:24:31 +00001195 TYs.push_back(DT.getNode());
Devang Patel72bcdb62009-08-10 22:09:58 +00001196 return true;
1197}
1198
Devang Pateld2f79a12009-07-28 19:55:13 +00001199/// addCompileUnit - Add compile unit into CUs.
Devang Patel98c65172009-07-30 18:25:15 +00001200bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001201 if (CU.isNull())
1202 return false;
1203
Devang Patele4b27562009-08-28 23:24:31 +00001204 if (!NodesSeen.insert(CU.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001205 return false;
1206
Devang Patele4b27562009-08-28 23:24:31 +00001207 CUs.push_back(CU.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001208 return true;
1209}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001210
Devang Pateld2f79a12009-07-28 19:55:13 +00001211/// addGlobalVariable - Add global variable into GVs.
Devang Patel98c65172009-07-30 18:25:15 +00001212bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001213 if (DIG.isNull())
1214 return false;
1215
Devang Patele4b27562009-08-28 23:24:31 +00001216 if (!NodesSeen.insert(DIG.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001217 return false;
1218
Devang Patele4b27562009-08-28 23:24:31 +00001219 GVs.push_back(DIG.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001220 return true;
1221}
1222
1223// addSubprogram - Add subprgoram into SPs.
Devang Patel98c65172009-07-30 18:25:15 +00001224bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001225 if (SP.isNull())
1226 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001227
Devang Patele4b27562009-08-28 23:24:31 +00001228 if (!NodesSeen.insert(SP.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001229 return false;
1230
Devang Patele4b27562009-08-28 23:24:31 +00001231 SPs.push_back(SP.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001232 return true;
1233}
1234
Torok Edwin620f2802008-12-16 09:07:36 +00001235namespace llvm {
Bill Wendlingdc817b62009-05-14 18:26:15 +00001236 /// findStopPoint - Find the stoppoint coressponding to this instruction, that
1237 /// is the stoppoint that dominates this instruction.
1238 const DbgStopPointInst *findStopPoint(const Instruction *Inst) {
Torok Edwin620f2802008-12-16 09:07:36 +00001239 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(Inst))
1240 return DSI;
1241
1242 const BasicBlock *BB = Inst->getParent();
1243 BasicBlock::const_iterator I = Inst, B;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001244 while (BB) {
Torok Edwin620f2802008-12-16 09:07:36 +00001245 B = BB->begin();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001246
Torok Edwin620f2802008-12-16 09:07:36 +00001247 // A BB consisting only of a terminator can't have a stoppoint.
Bill Wendlingdc817b62009-05-14 18:26:15 +00001248 while (I != B) {
1249 --I;
1250 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
1251 return DSI;
Torok Edwin620f2802008-12-16 09:07:36 +00001252 }
Bill Wendlingdc817b62009-05-14 18:26:15 +00001253
1254 // This BB didn't have a stoppoint: if there is only one predecessor, look
1255 // for a stoppoint there. We could use getIDom(), but that would require
1256 // dominator info.
Torok Edwin620f2802008-12-16 09:07:36 +00001257 BB = I->getParent()->getUniquePredecessor();
1258 if (BB)
1259 I = BB->getTerminator();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001260 }
1261
Torok Edwin620f2802008-12-16 09:07:36 +00001262 return 0;
1263 }
1264
Bill Wendlingdc817b62009-05-14 18:26:15 +00001265 /// findBBStopPoint - Find the stoppoint corresponding to first real
1266 /// (non-debug intrinsic) instruction in this Basic Block, and return the
1267 /// stoppoint for it.
1268 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB) {
1269 for(BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001270 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
1271 return DSI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001272
1273 // Fallback to looking for stoppoint of unique predecessor. Useful if this
1274 // BB contains no stoppoints, but unique predecessor does.
Torok Edwin620f2802008-12-16 09:07:36 +00001275 BB = BB->getUniquePredecessor();
1276 if (BB)
1277 return findStopPoint(BB->getTerminator());
Bill Wendlingdc817b62009-05-14 18:26:15 +00001278
Torok Edwin620f2802008-12-16 09:07:36 +00001279 return 0;
1280 }
1281
Bill Wendlingdc817b62009-05-14 18:26:15 +00001282 Value *findDbgGlobalDeclare(GlobalVariable *V) {
Torok Edwinff7d0e92009-03-10 13:41:26 +00001283 const Module *M = V->getParent();
Devang Patele4b27562009-08-28 23:24:31 +00001284 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1285 if (!NMD)
1286 return 0;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001287
Devang Patele4b27562009-08-28 23:24:31 +00001288 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
1289 DIGlobalVariable DIG(cast_or_null<MDNode>(NMD->getElement(i)));
1290 if (DIG.isNull())
1291 continue;
1292 if (DIG.getGlobal() == V)
1293 return DIG.getNode();
Torok Edwinff7d0e92009-03-10 13:41:26 +00001294 }
Torok Edwinff7d0e92009-03-10 13:41:26 +00001295 return 0;
1296 }
1297
Bill Wendlingdc817b62009-05-14 18:26:15 +00001298 /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
Torok Edwin620f2802008-12-16 09:07:36 +00001299 /// It looks through pointer casts too.
Bill Wendlingdc817b62009-05-14 18:26:15 +00001300 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts) {
Torok Edwin620f2802008-12-16 09:07:36 +00001301 if (stripCasts) {
1302 V = V->stripPointerCasts();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001303
Torok Edwin620f2802008-12-16 09:07:36 +00001304 // Look for the bitcast.
1305 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001306 I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001307 if (isa<BitCastInst>(I))
1308 return findDbgDeclare(*I, false);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001309
Torok Edwin620f2802008-12-16 09:07:36 +00001310 return 0;
1311 }
1312
Bill Wendlingdc817b62009-05-14 18:26:15 +00001313 // Find llvm.dbg.declare among uses of the instruction.
Torok Edwin620f2802008-12-16 09:07:36 +00001314 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001315 I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001316 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I))
1317 return DDI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001318
Torok Edwin620f2802008-12-16 09:07:36 +00001319 return 0;
1320 }
Torok Edwinff7d0e92009-03-10 13:41:26 +00001321
Devang Patel5ccdd102009-09-29 18:40:58 +00001322bool getLocationInfo(const Value *V, std::string &DisplayName,
1323 std::string &Type, unsigned &LineNo, std::string &File,
Bill Wendlingdc817b62009-05-14 18:26:15 +00001324 std::string &Dir) {
Torok Edwinff7d0e92009-03-10 13:41:26 +00001325 DICompileUnit Unit;
1326 DIType TypeD;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001327
Torok Edwinff7d0e92009-03-10 13:41:26 +00001328 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1329 Value *DIGV = findDbgGlobalDeclare(GV);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001330 if (!DIGV) return false;
Devang Patele4b27562009-08-28 23:24:31 +00001331 DIGlobalVariable Var(cast<MDNode>(DIGV));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001332
Devang Patel5ccdd102009-09-29 18:40:58 +00001333 if (const char *D = Var.getDisplayName())
1334 DisplayName = D;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001335 LineNo = Var.getLineNumber();
1336 Unit = Var.getCompileUnit();
1337 TypeD = Var.getType();
1338 } else {
1339 const DbgDeclareInst *DDI = findDbgDeclare(V);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001340 if (!DDI) return false;
Devang Patele4b27562009-08-28 23:24:31 +00001341 DIVariable Var(cast<MDNode>(DDI->getVariable()));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001342
Devang Patel5ccdd102009-09-29 18:40:58 +00001343 if (const char *D = Var.getName())
1344 DisplayName = D;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001345 LineNo = Var.getLineNumber();
1346 Unit = Var.getCompileUnit();
1347 TypeD = Var.getType();
1348 }
Bill Wendlingdc817b62009-05-14 18:26:15 +00001349
Devang Patel5ccdd102009-09-29 18:40:58 +00001350 if (const char *T = TypeD.getName())
1351 Type = T;
1352 if (const char *F = Unit.getFilename())
1353 File = F;
1354 if (const char *D = Unit.getDirectory())
1355 Dir = D;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001356 return true;
1357 }
Devang Patel13e16b62009-06-26 01:49:18 +00001358
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001359 /// isValidDebugInfoIntrinsic - Return true if SPI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001360 /// info intrinsic.
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001361 bool isValidDebugInfoIntrinsic(DbgStopPointInst &SPI,
Devang Patel9e529c32009-07-02 01:15:24 +00001362 CodeGenOpt::Level OptLev) {
1363 return DIDescriptor::ValidDebugInfo(SPI.getContext(), OptLev);
1364 }
1365
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001366 /// isValidDebugInfoIntrinsic - Return true if FSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001367 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001368 bool isValidDebugInfoIntrinsic(DbgFuncStartInst &FSI,
1369 CodeGenOpt::Level OptLev) {
1370 return DIDescriptor::ValidDebugInfo(FSI.getSubprogram(), OptLev);
1371 }
1372
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001373 /// isValidDebugInfoIntrinsic - Return true if RSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001374 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001375 bool isValidDebugInfoIntrinsic(DbgRegionStartInst &RSI,
1376 CodeGenOpt::Level OptLev) {
1377 return DIDescriptor::ValidDebugInfo(RSI.getContext(), OptLev);
1378 }
1379
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001380 /// isValidDebugInfoIntrinsic - Return true if REI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001381 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001382 bool isValidDebugInfoIntrinsic(DbgRegionEndInst &REI,
1383 CodeGenOpt::Level OptLev) {
1384 return DIDescriptor::ValidDebugInfo(REI.getContext(), OptLev);
1385 }
1386
1387
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001388 /// isValidDebugInfoIntrinsic - Return true if DI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001389 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001390 bool isValidDebugInfoIntrinsic(DbgDeclareInst &DI,
1391 CodeGenOpt::Level OptLev) {
1392 return DIDescriptor::ValidDebugInfo(DI.getVariable(), OptLev);
1393 }
1394
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001395 /// ExtractDebugLocation - Extract debug location information
Devang Patel9e529c32009-07-02 01:15:24 +00001396 /// from llvm.dbg.stoppoint intrinsic.
1397 DebugLoc ExtractDebugLocation(DbgStopPointInst &SPI,
Devang Patel9e529c32009-07-02 01:15:24 +00001398 DebugLocTracker &DebugLocInfo) {
1399 DebugLoc DL;
1400 Value *Context = SPI.getContext();
Devang Patel9e529c32009-07-02 01:15:24 +00001401
1402 // If this location is already tracked then use it.
Devang Patela1434042009-10-01 01:15:28 +00001403 DebugLocTuple Tuple(cast<MDNode>(Context), NULL, SPI.getLine(),
Devang Patel9e529c32009-07-02 01:15:24 +00001404 SPI.getColumn());
1405 DenseMap<DebugLocTuple, unsigned>::iterator II
1406 = DebugLocInfo.DebugIdMap.find(Tuple);
1407 if (II != DebugLocInfo.DebugIdMap.end())
1408 return DebugLoc::get(II->second);
1409
1410 // Add a new location entry.
1411 unsigned Id = DebugLocInfo.DebugLocations.size();
1412 DebugLocInfo.DebugLocations.push_back(Tuple);
1413 DebugLocInfo.DebugIdMap[Tuple] = Id;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001414
Devang Patel9e529c32009-07-02 01:15:24 +00001415 return DebugLoc::get(Id);
1416 }
1417
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001418 /// ExtractDebugLocation - Extract debug location information
Devang Patel1b75f442009-09-16 18:20:05 +00001419 /// from DILocation.
1420 DebugLoc ExtractDebugLocation(DILocation &Loc,
1421 DebugLocTracker &DebugLocInfo) {
1422 DebugLoc DL;
1423 MDNode *Context = Loc.getScope().getNode();
Devang Patela1434042009-10-01 01:15:28 +00001424 MDNode *InlinedLoc = NULL;
1425 if (!Loc.getOrigLocation().isNull())
1426 InlinedLoc = Loc.getOrigLocation().getNode();
Devang Patel1b75f442009-09-16 18:20:05 +00001427 // If this location is already tracked then use it.
Devang Patela1434042009-10-01 01:15:28 +00001428 DebugLocTuple Tuple(Context, InlinedLoc, Loc.getLineNumber(),
Daniel Dunbara279bc32009-09-20 02:20:51 +00001429 Loc.getColumnNumber());
Devang Patel1b75f442009-09-16 18:20:05 +00001430 DenseMap<DebugLocTuple, unsigned>::iterator II
1431 = DebugLocInfo.DebugIdMap.find(Tuple);
1432 if (II != DebugLocInfo.DebugIdMap.end())
1433 return DebugLoc::get(II->second);
1434
1435 // Add a new location entry.
1436 unsigned Id = DebugLocInfo.DebugLocations.size();
1437 DebugLocInfo.DebugLocations.push_back(Tuple);
1438 DebugLocInfo.DebugIdMap[Tuple] = Id;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001439
Devang Patel1b75f442009-09-16 18:20:05 +00001440 return DebugLoc::get(Id);
1441 }
1442
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001443 /// ExtractDebugLocation - Extract debug location information
Devang Patel9e529c32009-07-02 01:15:24 +00001444 /// from llvm.dbg.func_start intrinsic.
1445 DebugLoc ExtractDebugLocation(DbgFuncStartInst &FSI,
Devang Patel9e529c32009-07-02 01:15:24 +00001446 DebugLocTracker &DebugLocInfo) {
1447 DebugLoc DL;
1448 Value *SP = FSI.getSubprogram();
Devang Patel9e529c32009-07-02 01:15:24 +00001449
Devang Patele4b27562009-08-28 23:24:31 +00001450 DISubprogram Subprogram(cast<MDNode>(SP));
Devang Patel9e529c32009-07-02 01:15:24 +00001451 unsigned Line = Subprogram.getLineNumber();
1452 DICompileUnit CU(Subprogram.getCompileUnit());
1453
1454 // If this location is already tracked then use it.
Devang Patela1434042009-10-01 01:15:28 +00001455 DebugLocTuple Tuple(CU.getNode(), NULL, Line, /* Column */ 0);
Devang Patel9e529c32009-07-02 01:15:24 +00001456 DenseMap<DebugLocTuple, unsigned>::iterator II
1457 = DebugLocInfo.DebugIdMap.find(Tuple);
1458 if (II != DebugLocInfo.DebugIdMap.end())
1459 return DebugLoc::get(II->second);
1460
1461 // Add a new location entry.
1462 unsigned Id = DebugLocInfo.DebugLocations.size();
1463 DebugLocInfo.DebugLocations.push_back(Tuple);
1464 DebugLocInfo.DebugIdMap[Tuple] = Id;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001465
Devang Patel9e529c32009-07-02 01:15:24 +00001466 return DebugLoc::get(Id);
1467 }
1468
1469 /// isInlinedFnStart - Return true if FSI is starting an inlined function.
1470 bool isInlinedFnStart(DbgFuncStartInst &FSI, const Function *CurrentFn) {
Devang Patele4b27562009-08-28 23:24:31 +00001471 DISubprogram Subprogram(cast<MDNode>(FSI.getSubprogram()));
Devang Patel9e529c32009-07-02 01:15:24 +00001472 if (Subprogram.describes(CurrentFn))
1473 return false;
1474
1475 return true;
1476 }
1477
1478 /// isInlinedFnEnd - Return true if REI is ending an inlined function.
1479 bool isInlinedFnEnd(DbgRegionEndInst &REI, const Function *CurrentFn) {
Devang Patele4b27562009-08-28 23:24:31 +00001480 DISubprogram Subprogram(cast<MDNode>(REI.getContext()));
Devang Patel9e529c32009-07-02 01:15:24 +00001481 if (Subprogram.isNull() || Subprogram.describes(CurrentFn))
1482 return false;
1483
1484 return true;
1485 }
Torok Edwin620f2802008-12-16 09:07:36 +00001486}