blob: 1d6e3a6e44efd27b4aa9195c4f66561528526cc5 [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())
119 return dyn_cast<GlobalVariable>(DbgNode->getElement(Elt));
120 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
698/// CreateDerivedType - Create a derived type like const qualified type,
699/// pointer, typedef, etc.
700DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
701 DIDescriptor Context,
Devang Patel5ccdd102009-09-29 18:40:58 +0000702 StringRef Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000703 DICompileUnit CompileUnit,
704 unsigned LineNumber,
705 uint64_t SizeInBits,
706 uint64_t AlignInBits,
707 uint64_t OffsetInBits,
708 unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000709 DIType DerivedFrom) {
Devang Patele4b27562009-08-28 23:24:31 +0000710 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000711 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000712 Context.getNode(),
713 MDString::get(VMContext, Name),
714 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000715 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
716 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
717 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
718 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
719 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patele4b27562009-08-28 23:24:31 +0000720 DerivedFrom.getNode(),
Chris Lattnera45664f2008-11-10 02:56:27 +0000721 };
Devang Patele4b27562009-08-28 23:24:31 +0000722 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000723}
724
725/// CreateCompositeType - Create a composite type like array, struct, etc.
726DICompositeType DIFactory::CreateCompositeType(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,
735 DIType DerivedFrom,
Devang Patel13319ce2009-02-17 22:43:44 +0000736 DIArray Elements,
737 unsigned RuntimeLang) {
Owen Andersone277fed2009-07-07 16:31:25 +0000738
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(),
750 Elements.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000751 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
Chris Lattnera45664f2008-11-10 02:56:27 +0000752 };
Devang Patele4b27562009-08-28 23:24:31 +0000753 return DICompositeType(MDNode::get(VMContext, &Elts[0], 12));
Chris Lattnera45664f2008-11-10 02:56:27 +0000754}
755
756
757/// CreateSubprogram - Create a new descriptor for the specified subprogram.
758/// See comments in DISubprogram for descriptions of these fields. This
759/// method does not unique the generated descriptors.
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000760DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
Devang Patel5ccdd102009-09-29 18:40:58 +0000761 StringRef Name,
762 StringRef DisplayName,
763 StringRef LinkageName,
Chris Lattnera45664f2008-11-10 02:56:27 +0000764 DICompileUnit CompileUnit,
765 unsigned LineNo, DIType Type,
766 bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000767 bool isDefinition) {
Devang Patel854967e2008-12-17 22:39:29 +0000768
Devang Patele4b27562009-08-28 23:24:31 +0000769 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000770 GetTagConstant(dwarf::DW_TAG_subprogram),
Devang Patele4b27562009-08-28 23:24:31 +0000771 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
772 Context.getNode(),
773 MDString::get(VMContext, Name),
774 MDString::get(VMContext, DisplayName),
775 MDString::get(VMContext, LinkageName),
776 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000777 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000778 Type.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000779 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
780 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition)
Chris Lattnera45664f2008-11-10 02:56:27 +0000781 };
Devang Patele4b27562009-08-28 23:24:31 +0000782 return DISubprogram(MDNode::get(VMContext, &Elts[0], 11));
Chris Lattnera45664f2008-11-10 02:56:27 +0000783}
784
785/// CreateGlobalVariable - Create a new descriptor for the specified global.
786DIGlobalVariable
Devang Patel5ccdd102009-09-29 18:40:58 +0000787DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
788 StringRef DisplayName,
789 StringRef LinkageName,
Chris Lattnera45664f2008-11-10 02:56:27 +0000790 DICompileUnit CompileUnit,
791 unsigned LineNo, DIType Type,bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000792 bool isDefinition, llvm::GlobalVariable *Val) {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000793 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000794 GetTagConstant(dwarf::DW_TAG_variable),
Devang Patele4b27562009-08-28 23:24:31 +0000795 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
796 Context.getNode(),
797 MDString::get(VMContext, Name),
798 MDString::get(VMContext, DisplayName),
799 MDString::get(VMContext, LinkageName),
800 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000801 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000802 Type.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000803 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
804 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
Devang Patele4b27562009-08-28 23:24:31 +0000805 Val
Chris Lattnera45664f2008-11-10 02:56:27 +0000806 };
Devang Patele4b27562009-08-28 23:24:31 +0000807
808 Value *const *Vs = &Elts[0];
809 MDNode *Node = MDNode::get(VMContext,Vs, 12);
810
811 // Create a named metadata so that we do not lose this mdnode.
812 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
813 NMD->addElement(Node);
814
815 return DIGlobalVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +0000816}
817
818
819/// CreateVariable - Create a new descriptor for the specified variable.
820DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
Devang Patel5ccdd102009-09-29 18:40:58 +0000821 StringRef Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000822 DICompileUnit CompileUnit, unsigned LineNo,
Devang Pateldd9db662009-01-30 18:20:31 +0000823 DIType Type) {
Devang Patele4b27562009-08-28 23:24:31 +0000824 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000825 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000826 Context.getNode(),
827 MDString::get(VMContext, Name),
828 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000829 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000830 Type.getNode(),
Chris Lattnera45664f2008-11-10 02:56:27 +0000831 };
Devang Patele4b27562009-08-28 23:24:31 +0000832 return DIVariable(MDNode::get(VMContext, &Elts[0], 6));
Chris Lattnera45664f2008-11-10 02:56:27 +0000833}
834
835
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000836/// CreateComplexVariable - Create a new descriptor for the specified variable
837/// which has a complex address expression for its address.
838DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
839 const std::string &Name,
840 DICompileUnit CompileUnit,
841 unsigned LineNo,
842 DIType Type, SmallVector<Value *, 9> &addr) {
843 SmallVector<Value *, 9> Elts;
844 Elts.push_back(GetTagConstant(Tag));
845 Elts.push_back(Context.getNode());
846 Elts.push_back(MDString::get(VMContext, Name));
847 Elts.push_back(CompileUnit.getNode());
848 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
849 Elts.push_back(Type.getNode());
850 Elts.insert(Elts.end(), addr.begin(), addr.end());
851
852 return DIVariable(MDNode::get(VMContext, &Elts[0], 6+addr.size()));
853}
854
855
Chris Lattnera45664f2008-11-10 02:56:27 +0000856/// CreateBlock - This creates a descriptor for a lexical block with the
Owen Anderson99035272009-07-07 17:12:53 +0000857/// specified parent VMContext.
Devang Patel5e005d82009-08-31 22:00:15 +0000858DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context) {
Devang Patele4b27562009-08-28 23:24:31 +0000859 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000860 GetTagConstant(dwarf::DW_TAG_lexical_block),
Devang Patele4b27562009-08-28 23:24:31 +0000861 Context.getNode()
Chris Lattnera45664f2008-11-10 02:56:27 +0000862 };
Devang Patel5e005d82009-08-31 22:00:15 +0000863 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 2));
Chris Lattnera45664f2008-11-10 02:56:27 +0000864}
865
Devang Patelf98d8fe2009-09-01 01:14:15 +0000866/// CreateLocation - Creates a debug info location.
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000867DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
Daniel Dunbara279bc32009-09-20 02:20:51 +0000868 DIScope S, DILocation OrigLoc) {
Devang Patelf98d8fe2009-09-01 01:14:15 +0000869 Value *Elts[] = {
870 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
871 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
872 S.getNode(),
873 OrigLoc.getNode(),
874 };
875 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
876}
877
Chris Lattnera45664f2008-11-10 02:56:27 +0000878
879//===----------------------------------------------------------------------===//
880// DIFactory: Routines for inserting code into a function
881//===----------------------------------------------------------------------===//
882
883/// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
884/// inserting it at the end of the specified basic block.
885void DIFactory::InsertStopPoint(DICompileUnit CU, unsigned LineNo,
886 unsigned ColNo, BasicBlock *BB) {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000887
Chris Lattnera45664f2008-11-10 02:56:27 +0000888 // Lazily construct llvm.dbg.stoppoint function.
889 if (!StopPointFn)
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000890 StopPointFn = llvm::Intrinsic::getDeclaration(&M,
Chris Lattnera45664f2008-11-10 02:56:27 +0000891 llvm::Intrinsic::dbg_stoppoint);
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000892
Chris Lattnera45664f2008-11-10 02:56:27 +0000893 // Invoke llvm.dbg.stoppoint
894 Value *Args[] = {
Owen Anderson1d0be152009-08-13 21:58:54 +0000895 ConstantInt::get(llvm::Type::getInt32Ty(VMContext), LineNo),
896 ConstantInt::get(llvm::Type::getInt32Ty(VMContext), ColNo),
Devang Patele4b27562009-08-28 23:24:31 +0000897 CU.getNode()
Chris Lattnera45664f2008-11-10 02:56:27 +0000898 };
899 CallInst::Create(StopPointFn, Args, Args+3, "", BB);
900}
901
902/// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
903/// mark the start of the specified subprogram.
904void DIFactory::InsertSubprogramStart(DISubprogram SP, BasicBlock *BB) {
905 // Lazily construct llvm.dbg.func.start.
906 if (!FuncStartFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000907 FuncStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_func_start);
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000908
Chris Lattnera45664f2008-11-10 02:56:27 +0000909 // Call llvm.dbg.func.start which also implicitly sets a stoppoint.
Devang Patele4b27562009-08-28 23:24:31 +0000910 CallInst::Create(FuncStartFn, SP.getNode(), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000911}
912
913/// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
914/// mark the start of a region for the specified scoping descriptor.
915void DIFactory::InsertRegionStart(DIDescriptor D, BasicBlock *BB) {
916 // Lazily construct llvm.dbg.region.start function.
917 if (!RegionStartFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000918 RegionStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_start);
919
Chris Lattnera45664f2008-11-10 02:56:27 +0000920 // Call llvm.dbg.func.start.
Devang Patele4b27562009-08-28 23:24:31 +0000921 CallInst::Create(RegionStartFn, D.getNode(), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000922}
923
Chris Lattnera45664f2008-11-10 02:56:27 +0000924/// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
925/// mark the end of a region for the specified scoping descriptor.
926void DIFactory::InsertRegionEnd(DIDescriptor D, BasicBlock *BB) {
927 // Lazily construct llvm.dbg.region.end function.
928 if (!RegionEndFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000929 RegionEndFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_end);
930
931 // Call llvm.dbg.region.end.
Devang Patele4b27562009-08-28 23:24:31 +0000932 CallInst::Create(RegionEndFn, D.getNode(), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000933}
934
935/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Mike Stumpe4250392009-10-01 22:08:58 +0000936void DIFactory::InsertDeclare(Value *Storage, DIVariable D,
937 Instruction *InsertBefore) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000938 // Cast the storage to a {}* for the call to llvm.dbg.declare.
Mike Stumpe4250392009-10-01 22:08:58 +0000939 Storage = new BitCastInst(Storage, EmptyStructPtr, "", InsertBefore);
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000940
Chris Lattnera45664f2008-11-10 02:56:27 +0000941 if (!DeclareFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000942 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
943
Devang Patele4b27562009-08-28 23:24:31 +0000944 Value *Args[] = { Storage, D.getNode() };
Mike Stumpe4250392009-10-01 22:08:58 +0000945 CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
946}
947
948/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
949void DIFactory::InsertDeclare(Value *Storage, DIVariable D,
950 BasicBlock *InsertAtEnd) {
951 // Cast the storage to a {}* for the call to llvm.dbg.declare.
952 Storage = new BitCastInst(Storage, EmptyStructPtr, "", InsertAtEnd);
953
954 if (!DeclareFn)
955 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
956
957 Value *Args[] = { Storage, D.getNode() };
958 CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);
Chris Lattnera45664f2008-11-10 02:56:27 +0000959}
Torok Edwin620f2802008-12-16 09:07:36 +0000960
Devang Patele4b27562009-08-28 23:24:31 +0000961
Devang Pateld2f79a12009-07-28 19:55:13 +0000962//===----------------------------------------------------------------------===//
Devang Patel98c65172009-07-30 18:25:15 +0000963// DebugInfoFinder implementations.
Devang Pateld2f79a12009-07-28 19:55:13 +0000964//===----------------------------------------------------------------------===//
965
Devang Patel98c65172009-07-30 18:25:15 +0000966/// processModule - Process entire module and collect debug info.
967void DebugInfoFinder::processModule(Module &M) {
Devang Patele4b27562009-08-28 23:24:31 +0000968
Devang Pateld2f79a12009-07-28 19:55:13 +0000969 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
970 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
971 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
972 ++BI) {
973 if (DbgStopPointInst *SPI = dyn_cast<DbgStopPointInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +0000974 processStopPoint(SPI);
Devang Pateld2f79a12009-07-28 19:55:13 +0000975 else if (DbgFuncStartInst *FSI = dyn_cast<DbgFuncStartInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +0000976 processFuncStart(FSI);
Devang Patele802f1c2009-07-30 17:30:23 +0000977 else if (DbgRegionStartInst *DRS = dyn_cast<DbgRegionStartInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +0000978 processRegionStart(DRS);
Devang Patele802f1c2009-07-30 17:30:23 +0000979 else if (DbgRegionEndInst *DRE = dyn_cast<DbgRegionEndInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +0000980 processRegionEnd(DRE);
Devang Patelb4d31302009-07-31 18:18:52 +0000981 else if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
982 processDeclare(DDI);
Devang Pateld2f79a12009-07-28 19:55:13 +0000983 }
Devang Patele4b27562009-08-28 23:24:31 +0000984
985 NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv");
986 if (!NMD)
987 return;
988
989 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
990 DIGlobalVariable DIG(cast<MDNode>(NMD->getElement(i)));
Devang Pateld2f79a12009-07-28 19:55:13 +0000991 if (addGlobalVariable(DIG)) {
992 addCompileUnit(DIG.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +0000993 processType(DIG.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +0000994 }
995 }
996}
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000997
Devang Patel98c65172009-07-30 18:25:15 +0000998/// processType - Process DIType.
999void DebugInfoFinder::processType(DIType DT) {
Devang Patel72bcdb62009-08-10 22:09:58 +00001000 if (!addType(DT))
Devang Pateld2f79a12009-07-28 19:55:13 +00001001 return;
1002
1003 addCompileUnit(DT.getCompileUnit());
Devang Patel6ceea332009-08-31 18:49:10 +00001004 if (DT.isCompositeType()) {
Devang Patele4b27562009-08-28 23:24:31 +00001005 DICompositeType DCT(DT.getNode());
Devang Patel98c65172009-07-30 18:25:15 +00001006 processType(DCT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001007 DIArray DA = DCT.getTypeArray();
1008 if (!DA.isNull())
1009 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1010 DIDescriptor D = DA.getElement(i);
Devang Patele4b27562009-08-28 23:24:31 +00001011 DIType TypeE = DIType(D.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001012 if (!TypeE.isNull())
Devang Patel98c65172009-07-30 18:25:15 +00001013 processType(TypeE);
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001014 else
Devang Patele4b27562009-08-28 23:24:31 +00001015 processSubprogram(DISubprogram(D.getNode()));
Devang Pateld2f79a12009-07-28 19:55:13 +00001016 }
Devang Patel6ceea332009-08-31 18:49:10 +00001017 } else if (DT.isDerivedType()) {
Devang Patele4b27562009-08-28 23:24:31 +00001018 DIDerivedType DDT(DT.getNode());
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001019 if (!DDT.isNull())
Devang Patel98c65172009-07-30 18:25:15 +00001020 processType(DDT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001021 }
1022}
1023
Devang Patel98c65172009-07-30 18:25:15 +00001024/// processSubprogram - Process DISubprogram.
1025void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Patele802f1c2009-07-30 17:30:23 +00001026 if (SP.isNull())
1027 return;
Devang Pateld2f79a12009-07-28 19:55:13 +00001028 if (!addSubprogram(SP))
1029 return;
1030 addCompileUnit(SP.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +00001031 processType(SP.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +00001032}
1033
Devang Patel98c65172009-07-30 18:25:15 +00001034/// processStopPoint - Process DbgStopPointInst.
1035void DebugInfoFinder::processStopPoint(DbgStopPointInst *SPI) {
Devang Patele4b27562009-08-28 23:24:31 +00001036 MDNode *Context = dyn_cast<MDNode>(SPI->getContext());
Devang Pateld2f79a12009-07-28 19:55:13 +00001037 addCompileUnit(DICompileUnit(Context));
1038}
1039
Devang Patel98c65172009-07-30 18:25:15 +00001040/// processFuncStart - Process DbgFuncStartInst.
1041void DebugInfoFinder::processFuncStart(DbgFuncStartInst *FSI) {
Devang Patele4b27562009-08-28 23:24:31 +00001042 MDNode *SP = dyn_cast<MDNode>(FSI->getSubprogram());
Devang Patel98c65172009-07-30 18:25:15 +00001043 processSubprogram(DISubprogram(SP));
Devang Pateld2f79a12009-07-28 19:55:13 +00001044}
1045
Devang Patel98c65172009-07-30 18:25:15 +00001046/// processRegionStart - Process DbgRegionStart.
1047void DebugInfoFinder::processRegionStart(DbgRegionStartInst *DRS) {
Devang Patele4b27562009-08-28 23:24:31 +00001048 MDNode *SP = dyn_cast<MDNode>(DRS->getContext());
Devang Patel98c65172009-07-30 18:25:15 +00001049 processSubprogram(DISubprogram(SP));
Devang Patele802f1c2009-07-30 17:30:23 +00001050}
1051
Devang Patel98c65172009-07-30 18:25:15 +00001052/// processRegionEnd - Process DbgRegionEnd.
1053void DebugInfoFinder::processRegionEnd(DbgRegionEndInst *DRE) {
Devang Patele4b27562009-08-28 23:24:31 +00001054 MDNode *SP = dyn_cast<MDNode>(DRE->getContext());
Devang Patel98c65172009-07-30 18:25:15 +00001055 processSubprogram(DISubprogram(SP));
Devang Patele802f1c2009-07-30 17:30:23 +00001056}
1057
Devang Patelb4d31302009-07-31 18:18:52 +00001058/// processDeclare - Process DbgDeclareInst.
1059void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
Devang Patele4b27562009-08-28 23:24:31 +00001060 DIVariable DV(cast<MDNode>(DDI->getVariable()));
Devang Patelb4d31302009-07-31 18:18:52 +00001061 if (DV.isNull())
1062 return;
1063
Devang Patele4b27562009-08-28 23:24:31 +00001064 if (!NodesSeen.insert(DV.getNode()))
Devang Patelb4d31302009-07-31 18:18:52 +00001065 return;
1066
1067 addCompileUnit(DV.getCompileUnit());
1068 processType(DV.getType());
1069}
1070
Devang Patel72bcdb62009-08-10 22:09:58 +00001071/// addType - Add type into Tys.
1072bool DebugInfoFinder::addType(DIType DT) {
1073 if (DT.isNull())
1074 return false;
1075
Devang Patele4b27562009-08-28 23:24:31 +00001076 if (!NodesSeen.insert(DT.getNode()))
Devang Patel72bcdb62009-08-10 22:09:58 +00001077 return false;
1078
Devang Patele4b27562009-08-28 23:24:31 +00001079 TYs.push_back(DT.getNode());
Devang Patel72bcdb62009-08-10 22:09:58 +00001080 return true;
1081}
1082
Devang Pateld2f79a12009-07-28 19:55:13 +00001083/// addCompileUnit - Add compile unit into CUs.
Devang Patel98c65172009-07-30 18:25:15 +00001084bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001085 if (CU.isNull())
1086 return false;
1087
Devang Patele4b27562009-08-28 23:24:31 +00001088 if (!NodesSeen.insert(CU.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001089 return false;
1090
Devang Patele4b27562009-08-28 23:24:31 +00001091 CUs.push_back(CU.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001092 return true;
1093}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001094
Devang Pateld2f79a12009-07-28 19:55:13 +00001095/// addGlobalVariable - Add global variable into GVs.
Devang Patel98c65172009-07-30 18:25:15 +00001096bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001097 if (DIG.isNull())
1098 return false;
1099
Devang Patele4b27562009-08-28 23:24:31 +00001100 if (!NodesSeen.insert(DIG.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001101 return false;
1102
Devang Patele4b27562009-08-28 23:24:31 +00001103 GVs.push_back(DIG.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001104 return true;
1105}
1106
1107// addSubprogram - Add subprgoram into SPs.
Devang Patel98c65172009-07-30 18:25:15 +00001108bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001109 if (SP.isNull())
1110 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001111
Devang Patele4b27562009-08-28 23:24:31 +00001112 if (!NodesSeen.insert(SP.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001113 return false;
1114
Devang Patele4b27562009-08-28 23:24:31 +00001115 SPs.push_back(SP.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001116 return true;
1117}
1118
Torok Edwin620f2802008-12-16 09:07:36 +00001119namespace llvm {
Bill Wendlingdc817b62009-05-14 18:26:15 +00001120 /// findStopPoint - Find the stoppoint coressponding to this instruction, that
1121 /// is the stoppoint that dominates this instruction.
1122 const DbgStopPointInst *findStopPoint(const Instruction *Inst) {
Torok Edwin620f2802008-12-16 09:07:36 +00001123 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(Inst))
1124 return DSI;
1125
1126 const BasicBlock *BB = Inst->getParent();
1127 BasicBlock::const_iterator I = Inst, B;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001128 while (BB) {
Torok Edwin620f2802008-12-16 09:07:36 +00001129 B = BB->begin();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001130
Torok Edwin620f2802008-12-16 09:07:36 +00001131 // A BB consisting only of a terminator can't have a stoppoint.
Bill Wendlingdc817b62009-05-14 18:26:15 +00001132 while (I != B) {
1133 --I;
1134 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
1135 return DSI;
Torok Edwin620f2802008-12-16 09:07:36 +00001136 }
Bill Wendlingdc817b62009-05-14 18:26:15 +00001137
1138 // This BB didn't have a stoppoint: if there is only one predecessor, look
1139 // for a stoppoint there. We could use getIDom(), but that would require
1140 // dominator info.
Torok Edwin620f2802008-12-16 09:07:36 +00001141 BB = I->getParent()->getUniquePredecessor();
1142 if (BB)
1143 I = BB->getTerminator();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001144 }
1145
Torok Edwin620f2802008-12-16 09:07:36 +00001146 return 0;
1147 }
1148
Bill Wendlingdc817b62009-05-14 18:26:15 +00001149 /// findBBStopPoint - Find the stoppoint corresponding to first real
1150 /// (non-debug intrinsic) instruction in this Basic Block, and return the
1151 /// stoppoint for it.
1152 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB) {
1153 for(BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001154 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
1155 return DSI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001156
1157 // Fallback to looking for stoppoint of unique predecessor. Useful if this
1158 // BB contains no stoppoints, but unique predecessor does.
Torok Edwin620f2802008-12-16 09:07:36 +00001159 BB = BB->getUniquePredecessor();
1160 if (BB)
1161 return findStopPoint(BB->getTerminator());
Bill Wendlingdc817b62009-05-14 18:26:15 +00001162
Torok Edwin620f2802008-12-16 09:07:36 +00001163 return 0;
1164 }
1165
Bill Wendlingdc817b62009-05-14 18:26:15 +00001166 Value *findDbgGlobalDeclare(GlobalVariable *V) {
Torok Edwinff7d0e92009-03-10 13:41:26 +00001167 const Module *M = V->getParent();
Devang Patele4b27562009-08-28 23:24:31 +00001168 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1169 if (!NMD)
1170 return 0;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001171
Devang Patele4b27562009-08-28 23:24:31 +00001172 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
1173 DIGlobalVariable DIG(cast_or_null<MDNode>(NMD->getElement(i)));
1174 if (DIG.isNull())
1175 continue;
1176 if (DIG.getGlobal() == V)
1177 return DIG.getNode();
Torok Edwinff7d0e92009-03-10 13:41:26 +00001178 }
Torok Edwinff7d0e92009-03-10 13:41:26 +00001179 return 0;
1180 }
1181
Bill Wendlingdc817b62009-05-14 18:26:15 +00001182 /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
Torok Edwin620f2802008-12-16 09:07:36 +00001183 /// It looks through pointer casts too.
Bill Wendlingdc817b62009-05-14 18:26:15 +00001184 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts) {
Torok Edwin620f2802008-12-16 09:07:36 +00001185 if (stripCasts) {
1186 V = V->stripPointerCasts();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001187
Torok Edwin620f2802008-12-16 09:07:36 +00001188 // Look for the bitcast.
1189 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001190 I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001191 if (isa<BitCastInst>(I))
1192 return findDbgDeclare(*I, false);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001193
Torok Edwin620f2802008-12-16 09:07:36 +00001194 return 0;
1195 }
1196
Bill Wendlingdc817b62009-05-14 18:26:15 +00001197 // Find llvm.dbg.declare among uses of the instruction.
Torok Edwin620f2802008-12-16 09:07:36 +00001198 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001199 I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001200 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I))
1201 return DDI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001202
Torok Edwin620f2802008-12-16 09:07:36 +00001203 return 0;
1204 }
Torok Edwinff7d0e92009-03-10 13:41:26 +00001205
Devang Patel5ccdd102009-09-29 18:40:58 +00001206bool getLocationInfo(const Value *V, std::string &DisplayName,
1207 std::string &Type, unsigned &LineNo, std::string &File,
Bill Wendlingdc817b62009-05-14 18:26:15 +00001208 std::string &Dir) {
Torok Edwinff7d0e92009-03-10 13:41:26 +00001209 DICompileUnit Unit;
1210 DIType TypeD;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001211
Torok Edwinff7d0e92009-03-10 13:41:26 +00001212 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1213 Value *DIGV = findDbgGlobalDeclare(GV);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001214 if (!DIGV) return false;
Devang Patele4b27562009-08-28 23:24:31 +00001215 DIGlobalVariable Var(cast<MDNode>(DIGV));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001216
Devang Patel5ccdd102009-09-29 18:40:58 +00001217 if (const char *D = Var.getDisplayName())
1218 DisplayName = D;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001219 LineNo = Var.getLineNumber();
1220 Unit = Var.getCompileUnit();
1221 TypeD = Var.getType();
1222 } else {
1223 const DbgDeclareInst *DDI = findDbgDeclare(V);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001224 if (!DDI) return false;
Devang Patele4b27562009-08-28 23:24:31 +00001225 DIVariable Var(cast<MDNode>(DDI->getVariable()));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001226
Devang Patel5ccdd102009-09-29 18:40:58 +00001227 if (const char *D = Var.getName())
1228 DisplayName = D;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001229 LineNo = Var.getLineNumber();
1230 Unit = Var.getCompileUnit();
1231 TypeD = Var.getType();
1232 }
Bill Wendlingdc817b62009-05-14 18:26:15 +00001233
Devang Patel5ccdd102009-09-29 18:40:58 +00001234 if (const char *T = TypeD.getName())
1235 Type = T;
1236 if (const char *F = Unit.getFilename())
1237 File = F;
1238 if (const char *D = Unit.getDirectory())
1239 Dir = D;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001240 return true;
1241 }
Devang Patel13e16b62009-06-26 01:49:18 +00001242
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001243 /// isValidDebugInfoIntrinsic - Return true if SPI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001244 /// info intrinsic.
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001245 bool isValidDebugInfoIntrinsic(DbgStopPointInst &SPI,
Devang Patel9e529c32009-07-02 01:15:24 +00001246 CodeGenOpt::Level OptLev) {
1247 return DIDescriptor::ValidDebugInfo(SPI.getContext(), OptLev);
1248 }
1249
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001250 /// isValidDebugInfoIntrinsic - Return true if FSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001251 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001252 bool isValidDebugInfoIntrinsic(DbgFuncStartInst &FSI,
1253 CodeGenOpt::Level OptLev) {
1254 return DIDescriptor::ValidDebugInfo(FSI.getSubprogram(), OptLev);
1255 }
1256
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001257 /// isValidDebugInfoIntrinsic - Return true if RSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001258 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001259 bool isValidDebugInfoIntrinsic(DbgRegionStartInst &RSI,
1260 CodeGenOpt::Level OptLev) {
1261 return DIDescriptor::ValidDebugInfo(RSI.getContext(), OptLev);
1262 }
1263
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001264 /// isValidDebugInfoIntrinsic - Return true if REI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001265 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001266 bool isValidDebugInfoIntrinsic(DbgRegionEndInst &REI,
1267 CodeGenOpt::Level OptLev) {
1268 return DIDescriptor::ValidDebugInfo(REI.getContext(), OptLev);
1269 }
1270
1271
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001272 /// isValidDebugInfoIntrinsic - Return true if DI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001273 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001274 bool isValidDebugInfoIntrinsic(DbgDeclareInst &DI,
1275 CodeGenOpt::Level OptLev) {
1276 return DIDescriptor::ValidDebugInfo(DI.getVariable(), OptLev);
1277 }
1278
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001279 /// ExtractDebugLocation - Extract debug location information
Devang Patel9e529c32009-07-02 01:15:24 +00001280 /// from llvm.dbg.stoppoint intrinsic.
1281 DebugLoc ExtractDebugLocation(DbgStopPointInst &SPI,
Devang Patel9e529c32009-07-02 01:15:24 +00001282 DebugLocTracker &DebugLocInfo) {
1283 DebugLoc DL;
1284 Value *Context = SPI.getContext();
Devang Patel9e529c32009-07-02 01:15:24 +00001285
1286 // If this location is already tracked then use it.
Devang Patela1434042009-10-01 01:15:28 +00001287 DebugLocTuple Tuple(cast<MDNode>(Context), NULL, SPI.getLine(),
Devang Patel9e529c32009-07-02 01:15:24 +00001288 SPI.getColumn());
1289 DenseMap<DebugLocTuple, unsigned>::iterator II
1290 = DebugLocInfo.DebugIdMap.find(Tuple);
1291 if (II != DebugLocInfo.DebugIdMap.end())
1292 return DebugLoc::get(II->second);
1293
1294 // Add a new location entry.
1295 unsigned Id = DebugLocInfo.DebugLocations.size();
1296 DebugLocInfo.DebugLocations.push_back(Tuple);
1297 DebugLocInfo.DebugIdMap[Tuple] = Id;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001298
Devang Patel9e529c32009-07-02 01:15:24 +00001299 return DebugLoc::get(Id);
1300 }
1301
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001302 /// ExtractDebugLocation - Extract debug location information
Devang Patel1b75f442009-09-16 18:20:05 +00001303 /// from DILocation.
1304 DebugLoc ExtractDebugLocation(DILocation &Loc,
1305 DebugLocTracker &DebugLocInfo) {
1306 DebugLoc DL;
1307 MDNode *Context = Loc.getScope().getNode();
Devang Patela1434042009-10-01 01:15:28 +00001308 MDNode *InlinedLoc = NULL;
1309 if (!Loc.getOrigLocation().isNull())
1310 InlinedLoc = Loc.getOrigLocation().getNode();
Devang Patel1b75f442009-09-16 18:20:05 +00001311 // If this location is already tracked then use it.
Devang Patela1434042009-10-01 01:15:28 +00001312 DebugLocTuple Tuple(Context, InlinedLoc, Loc.getLineNumber(),
Daniel Dunbara279bc32009-09-20 02:20:51 +00001313 Loc.getColumnNumber());
Devang Patel1b75f442009-09-16 18:20:05 +00001314 DenseMap<DebugLocTuple, unsigned>::iterator II
1315 = DebugLocInfo.DebugIdMap.find(Tuple);
1316 if (II != DebugLocInfo.DebugIdMap.end())
1317 return DebugLoc::get(II->second);
1318
1319 // Add a new location entry.
1320 unsigned Id = DebugLocInfo.DebugLocations.size();
1321 DebugLocInfo.DebugLocations.push_back(Tuple);
1322 DebugLocInfo.DebugIdMap[Tuple] = Id;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001323
Devang Patel1b75f442009-09-16 18:20:05 +00001324 return DebugLoc::get(Id);
1325 }
1326
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001327 /// ExtractDebugLocation - Extract debug location information
Devang Patel9e529c32009-07-02 01:15:24 +00001328 /// from llvm.dbg.func_start intrinsic.
1329 DebugLoc ExtractDebugLocation(DbgFuncStartInst &FSI,
Devang Patel9e529c32009-07-02 01:15:24 +00001330 DebugLocTracker &DebugLocInfo) {
1331 DebugLoc DL;
1332 Value *SP = FSI.getSubprogram();
Devang Patel9e529c32009-07-02 01:15:24 +00001333
Devang Patele4b27562009-08-28 23:24:31 +00001334 DISubprogram Subprogram(cast<MDNode>(SP));
Devang Patel9e529c32009-07-02 01:15:24 +00001335 unsigned Line = Subprogram.getLineNumber();
1336 DICompileUnit CU(Subprogram.getCompileUnit());
1337
1338 // If this location is already tracked then use it.
Devang Patela1434042009-10-01 01:15:28 +00001339 DebugLocTuple Tuple(CU.getNode(), NULL, Line, /* Column */ 0);
Devang Patel9e529c32009-07-02 01:15:24 +00001340 DenseMap<DebugLocTuple, unsigned>::iterator II
1341 = DebugLocInfo.DebugIdMap.find(Tuple);
1342 if (II != DebugLocInfo.DebugIdMap.end())
1343 return DebugLoc::get(II->second);
1344
1345 // Add a new location entry.
1346 unsigned Id = DebugLocInfo.DebugLocations.size();
1347 DebugLocInfo.DebugLocations.push_back(Tuple);
1348 DebugLocInfo.DebugIdMap[Tuple] = Id;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001349
Devang Patel9e529c32009-07-02 01:15:24 +00001350 return DebugLoc::get(Id);
1351 }
1352
1353 /// isInlinedFnStart - Return true if FSI is starting an inlined function.
1354 bool isInlinedFnStart(DbgFuncStartInst &FSI, const Function *CurrentFn) {
Devang Patele4b27562009-08-28 23:24:31 +00001355 DISubprogram Subprogram(cast<MDNode>(FSI.getSubprogram()));
Devang Patel9e529c32009-07-02 01:15:24 +00001356 if (Subprogram.describes(CurrentFn))
1357 return false;
1358
1359 return true;
1360 }
1361
1362 /// isInlinedFnEnd - Return true if REI is ending an inlined function.
1363 bool isInlinedFnEnd(DbgRegionEndInst &REI, const Function *CurrentFn) {
Devang Patele4b27562009-08-28 23:24:31 +00001364 DISubprogram Subprogram(cast<MDNode>(REI.getContext()));
Devang Patel9e529c32009-07-02 01:15:24 +00001365 if (Subprogram.isNull() || Subprogram.describes(CurrentFn))
1366 return false;
1367
1368 return true;
1369 }
Torok Edwin620f2802008-12-16 09:07:36 +00001370}