blob: 81d0ddb6f5555da3366a48820f2487aafa32281f [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
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 Patelbeab41b2009-10-07 22:04:08 +0000969#ifdef ATTACH_DEBUG_INFO_TO_AN_INSN
970 MetadataContext &TheMetadata = M.getContext().getMetadata();
971 unsigned MDDbgKind = TheMetadata.getMDKind("dbg");
972 if (!MDDbgKind)
973 return;
974#endif
Devang Pateld2f79a12009-07-28 19:55:13 +0000975 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
976 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
977 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
978 ++BI) {
979 if (DbgStopPointInst *SPI = dyn_cast<DbgStopPointInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +0000980 processStopPoint(SPI);
Devang Pateld2f79a12009-07-28 19:55:13 +0000981 else if (DbgFuncStartInst *FSI = dyn_cast<DbgFuncStartInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +0000982 processFuncStart(FSI);
Devang Patele802f1c2009-07-30 17:30:23 +0000983 else if (DbgRegionStartInst *DRS = dyn_cast<DbgRegionStartInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +0000984 processRegionStart(DRS);
Devang Patele802f1c2009-07-30 17:30:23 +0000985 else if (DbgRegionEndInst *DRE = dyn_cast<DbgRegionEndInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +0000986 processRegionEnd(DRE);
Devang Patelb4d31302009-07-31 18:18:52 +0000987 else if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
988 processDeclare(DDI);
Devang Patelbeab41b2009-10-07 22:04:08 +0000989#ifdef ATTACH_DEBUG_INFO_TO_AN_INSN
990 else if (MDNode *L = TheMetadata.getMD(MDDbgKind, BI)) {
991 DILocation Loc(L);
992 DIScope S(Loc.getScope().getNode());
993 if (S.isCompileUnit())
994 addCompileUnit(DICompileUnit(S.getNode()));
995 else if (S.isSubprogram())
996 processSubprogram(DISubprogram(S.getNode()));
997 else if (S.isLexicalBlock())
998 processLexicalBlock(DILexicalBlock(S.getNode()));
999 }
1000#endif
Devang Pateld2f79a12009-07-28 19:55:13 +00001001 }
Devang Patele4b27562009-08-28 23:24:31 +00001002
1003 NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv");
1004 if (!NMD)
1005 return;
1006
1007 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
1008 DIGlobalVariable DIG(cast<MDNode>(NMD->getElement(i)));
Devang Pateld2f79a12009-07-28 19:55:13 +00001009 if (addGlobalVariable(DIG)) {
1010 addCompileUnit(DIG.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +00001011 processType(DIG.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +00001012 }
1013 }
1014}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001015
Devang Patel98c65172009-07-30 18:25:15 +00001016/// processType - Process DIType.
1017void DebugInfoFinder::processType(DIType DT) {
Devang Patel72bcdb62009-08-10 22:09:58 +00001018 if (!addType(DT))
Devang Pateld2f79a12009-07-28 19:55:13 +00001019 return;
1020
1021 addCompileUnit(DT.getCompileUnit());
Devang Patel6ceea332009-08-31 18:49:10 +00001022 if (DT.isCompositeType()) {
Devang Patele4b27562009-08-28 23:24:31 +00001023 DICompositeType DCT(DT.getNode());
Devang Patel98c65172009-07-30 18:25:15 +00001024 processType(DCT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001025 DIArray DA = DCT.getTypeArray();
1026 if (!DA.isNull())
1027 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1028 DIDescriptor D = DA.getElement(i);
Devang Patele4b27562009-08-28 23:24:31 +00001029 DIType TypeE = DIType(D.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001030 if (!TypeE.isNull())
Devang Patel98c65172009-07-30 18:25:15 +00001031 processType(TypeE);
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001032 else
Devang Patele4b27562009-08-28 23:24:31 +00001033 processSubprogram(DISubprogram(D.getNode()));
Devang Pateld2f79a12009-07-28 19:55:13 +00001034 }
Devang Patel6ceea332009-08-31 18:49:10 +00001035 } else if (DT.isDerivedType()) {
Devang Patele4b27562009-08-28 23:24:31 +00001036 DIDerivedType DDT(DT.getNode());
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001037 if (!DDT.isNull())
Devang Patel98c65172009-07-30 18:25:15 +00001038 processType(DDT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001039 }
1040}
1041
Devang Patelbeab41b2009-10-07 22:04:08 +00001042/// processLexicalBlock
1043void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
1044 if (LB.isNull())
1045 return;
1046 DIScope Context = LB.getContext();
1047 if (Context.isLexicalBlock())
1048 return processLexicalBlock(DILexicalBlock(Context.getNode()));
1049 else
1050 return processSubprogram(DISubprogram(Context.getNode()));
1051}
1052
Devang Patel98c65172009-07-30 18:25:15 +00001053/// processSubprogram - Process DISubprogram.
1054void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Patele802f1c2009-07-30 17:30:23 +00001055 if (SP.isNull())
1056 return;
Devang Pateld2f79a12009-07-28 19:55:13 +00001057 if (!addSubprogram(SP))
1058 return;
1059 addCompileUnit(SP.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +00001060 processType(SP.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +00001061}
1062
Devang Patel98c65172009-07-30 18:25:15 +00001063/// processStopPoint - Process DbgStopPointInst.
1064void DebugInfoFinder::processStopPoint(DbgStopPointInst *SPI) {
Devang Patele4b27562009-08-28 23:24:31 +00001065 MDNode *Context = dyn_cast<MDNode>(SPI->getContext());
Devang Pateld2f79a12009-07-28 19:55:13 +00001066 addCompileUnit(DICompileUnit(Context));
1067}
1068
Devang Patel98c65172009-07-30 18:25:15 +00001069/// processFuncStart - Process DbgFuncStartInst.
1070void DebugInfoFinder::processFuncStart(DbgFuncStartInst *FSI) {
Devang Patele4b27562009-08-28 23:24:31 +00001071 MDNode *SP = dyn_cast<MDNode>(FSI->getSubprogram());
Devang Patel98c65172009-07-30 18:25:15 +00001072 processSubprogram(DISubprogram(SP));
Devang Pateld2f79a12009-07-28 19:55:13 +00001073}
1074
Devang Patel98c65172009-07-30 18:25:15 +00001075/// processRegionStart - Process DbgRegionStart.
1076void DebugInfoFinder::processRegionStart(DbgRegionStartInst *DRS) {
Devang Patele4b27562009-08-28 23:24:31 +00001077 MDNode *SP = dyn_cast<MDNode>(DRS->getContext());
Devang Patel98c65172009-07-30 18:25:15 +00001078 processSubprogram(DISubprogram(SP));
Devang Patele802f1c2009-07-30 17:30:23 +00001079}
1080
Devang Patel98c65172009-07-30 18:25:15 +00001081/// processRegionEnd - Process DbgRegionEnd.
1082void DebugInfoFinder::processRegionEnd(DbgRegionEndInst *DRE) {
Devang Patele4b27562009-08-28 23:24:31 +00001083 MDNode *SP = dyn_cast<MDNode>(DRE->getContext());
Devang Patel98c65172009-07-30 18:25:15 +00001084 processSubprogram(DISubprogram(SP));
Devang Patele802f1c2009-07-30 17:30:23 +00001085}
1086
Devang Patelb4d31302009-07-31 18:18:52 +00001087/// processDeclare - Process DbgDeclareInst.
1088void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
Devang Patele4b27562009-08-28 23:24:31 +00001089 DIVariable DV(cast<MDNode>(DDI->getVariable()));
Devang Patelb4d31302009-07-31 18:18:52 +00001090 if (DV.isNull())
1091 return;
1092
Devang Patele4b27562009-08-28 23:24:31 +00001093 if (!NodesSeen.insert(DV.getNode()))
Devang Patelb4d31302009-07-31 18:18:52 +00001094 return;
1095
1096 addCompileUnit(DV.getCompileUnit());
1097 processType(DV.getType());
1098}
1099
Devang Patel72bcdb62009-08-10 22:09:58 +00001100/// addType - Add type into Tys.
1101bool DebugInfoFinder::addType(DIType DT) {
1102 if (DT.isNull())
1103 return false;
1104
Devang Patele4b27562009-08-28 23:24:31 +00001105 if (!NodesSeen.insert(DT.getNode()))
Devang Patel72bcdb62009-08-10 22:09:58 +00001106 return false;
1107
Devang Patele4b27562009-08-28 23:24:31 +00001108 TYs.push_back(DT.getNode());
Devang Patel72bcdb62009-08-10 22:09:58 +00001109 return true;
1110}
1111
Devang Pateld2f79a12009-07-28 19:55:13 +00001112/// addCompileUnit - Add compile unit into CUs.
Devang Patel98c65172009-07-30 18:25:15 +00001113bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001114 if (CU.isNull())
1115 return false;
1116
Devang Patele4b27562009-08-28 23:24:31 +00001117 if (!NodesSeen.insert(CU.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001118 return false;
1119
Devang Patele4b27562009-08-28 23:24:31 +00001120 CUs.push_back(CU.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001121 return true;
1122}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001123
Devang Pateld2f79a12009-07-28 19:55:13 +00001124/// addGlobalVariable - Add global variable into GVs.
Devang Patel98c65172009-07-30 18:25:15 +00001125bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001126 if (DIG.isNull())
1127 return false;
1128
Devang Patele4b27562009-08-28 23:24:31 +00001129 if (!NodesSeen.insert(DIG.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001130 return false;
1131
Devang Patele4b27562009-08-28 23:24:31 +00001132 GVs.push_back(DIG.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001133 return true;
1134}
1135
1136// addSubprogram - Add subprgoram into SPs.
Devang Patel98c65172009-07-30 18:25:15 +00001137bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001138 if (SP.isNull())
1139 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001140
Devang Patele4b27562009-08-28 23:24:31 +00001141 if (!NodesSeen.insert(SP.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001142 return false;
1143
Devang Patele4b27562009-08-28 23:24:31 +00001144 SPs.push_back(SP.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001145 return true;
1146}
1147
Torok Edwin620f2802008-12-16 09:07:36 +00001148namespace llvm {
Bill Wendlingdc817b62009-05-14 18:26:15 +00001149 /// findStopPoint - Find the stoppoint coressponding to this instruction, that
1150 /// is the stoppoint that dominates this instruction.
1151 const DbgStopPointInst *findStopPoint(const Instruction *Inst) {
Torok Edwin620f2802008-12-16 09:07:36 +00001152 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(Inst))
1153 return DSI;
1154
1155 const BasicBlock *BB = Inst->getParent();
1156 BasicBlock::const_iterator I = Inst, B;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001157 while (BB) {
Torok Edwin620f2802008-12-16 09:07:36 +00001158 B = BB->begin();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001159
Torok Edwin620f2802008-12-16 09:07:36 +00001160 // A BB consisting only of a terminator can't have a stoppoint.
Bill Wendlingdc817b62009-05-14 18:26:15 +00001161 while (I != B) {
1162 --I;
1163 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
1164 return DSI;
Torok Edwin620f2802008-12-16 09:07:36 +00001165 }
Bill Wendlingdc817b62009-05-14 18:26:15 +00001166
1167 // This BB didn't have a stoppoint: if there is only one predecessor, look
1168 // for a stoppoint there. We could use getIDom(), but that would require
1169 // dominator info.
Torok Edwin620f2802008-12-16 09:07:36 +00001170 BB = I->getParent()->getUniquePredecessor();
1171 if (BB)
1172 I = BB->getTerminator();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001173 }
1174
Torok Edwin620f2802008-12-16 09:07:36 +00001175 return 0;
1176 }
1177
Bill Wendlingdc817b62009-05-14 18:26:15 +00001178 /// findBBStopPoint - Find the stoppoint corresponding to first real
1179 /// (non-debug intrinsic) instruction in this Basic Block, and return the
1180 /// stoppoint for it.
1181 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB) {
1182 for(BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001183 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
1184 return DSI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001185
1186 // Fallback to looking for stoppoint of unique predecessor. Useful if this
1187 // BB contains no stoppoints, but unique predecessor does.
Torok Edwin620f2802008-12-16 09:07:36 +00001188 BB = BB->getUniquePredecessor();
1189 if (BB)
1190 return findStopPoint(BB->getTerminator());
Bill Wendlingdc817b62009-05-14 18:26:15 +00001191
Torok Edwin620f2802008-12-16 09:07:36 +00001192 return 0;
1193 }
1194
Bill Wendlingdc817b62009-05-14 18:26:15 +00001195 Value *findDbgGlobalDeclare(GlobalVariable *V) {
Torok Edwinff7d0e92009-03-10 13:41:26 +00001196 const Module *M = V->getParent();
Devang Patele4b27562009-08-28 23:24:31 +00001197 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1198 if (!NMD)
1199 return 0;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001200
Devang Patele4b27562009-08-28 23:24:31 +00001201 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
1202 DIGlobalVariable DIG(cast_or_null<MDNode>(NMD->getElement(i)));
1203 if (DIG.isNull())
1204 continue;
1205 if (DIG.getGlobal() == V)
1206 return DIG.getNode();
Torok Edwinff7d0e92009-03-10 13:41:26 +00001207 }
Torok Edwinff7d0e92009-03-10 13:41:26 +00001208 return 0;
1209 }
1210
Bill Wendlingdc817b62009-05-14 18:26:15 +00001211 /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
Torok Edwin620f2802008-12-16 09:07:36 +00001212 /// It looks through pointer casts too.
Bill Wendlingdc817b62009-05-14 18:26:15 +00001213 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts) {
Torok Edwin620f2802008-12-16 09:07:36 +00001214 if (stripCasts) {
1215 V = V->stripPointerCasts();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001216
Torok Edwin620f2802008-12-16 09:07:36 +00001217 // Look for the bitcast.
1218 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001219 I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001220 if (isa<BitCastInst>(I))
1221 return findDbgDeclare(*I, false);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001222
Torok Edwin620f2802008-12-16 09:07:36 +00001223 return 0;
1224 }
1225
Bill Wendlingdc817b62009-05-14 18:26:15 +00001226 // Find llvm.dbg.declare among uses of the instruction.
Torok Edwin620f2802008-12-16 09:07:36 +00001227 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001228 I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001229 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I))
1230 return DDI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001231
Torok Edwin620f2802008-12-16 09:07:36 +00001232 return 0;
1233 }
Torok Edwinff7d0e92009-03-10 13:41:26 +00001234
Devang Patel5ccdd102009-09-29 18:40:58 +00001235bool getLocationInfo(const Value *V, std::string &DisplayName,
1236 std::string &Type, unsigned &LineNo, std::string &File,
Bill Wendlingdc817b62009-05-14 18:26:15 +00001237 std::string &Dir) {
Torok Edwinff7d0e92009-03-10 13:41:26 +00001238 DICompileUnit Unit;
1239 DIType TypeD;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001240
Torok Edwinff7d0e92009-03-10 13:41:26 +00001241 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1242 Value *DIGV = findDbgGlobalDeclare(GV);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001243 if (!DIGV) return false;
Devang Patele4b27562009-08-28 23:24:31 +00001244 DIGlobalVariable Var(cast<MDNode>(DIGV));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001245
Devang Patel5ccdd102009-09-29 18:40:58 +00001246 if (const char *D = Var.getDisplayName())
1247 DisplayName = D;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001248 LineNo = Var.getLineNumber();
1249 Unit = Var.getCompileUnit();
1250 TypeD = Var.getType();
1251 } else {
1252 const DbgDeclareInst *DDI = findDbgDeclare(V);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001253 if (!DDI) return false;
Devang Patele4b27562009-08-28 23:24:31 +00001254 DIVariable Var(cast<MDNode>(DDI->getVariable()));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001255
Devang Patel5ccdd102009-09-29 18:40:58 +00001256 if (const char *D = Var.getName())
1257 DisplayName = D;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001258 LineNo = Var.getLineNumber();
1259 Unit = Var.getCompileUnit();
1260 TypeD = Var.getType();
1261 }
Bill Wendlingdc817b62009-05-14 18:26:15 +00001262
Devang Patel5ccdd102009-09-29 18:40:58 +00001263 if (const char *T = TypeD.getName())
1264 Type = T;
1265 if (const char *F = Unit.getFilename())
1266 File = F;
1267 if (const char *D = Unit.getDirectory())
1268 Dir = D;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001269 return true;
1270 }
Devang Patel13e16b62009-06-26 01:49:18 +00001271
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001272 /// isValidDebugInfoIntrinsic - Return true if SPI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001273 /// info intrinsic.
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001274 bool isValidDebugInfoIntrinsic(DbgStopPointInst &SPI,
Devang Patel9e529c32009-07-02 01:15:24 +00001275 CodeGenOpt::Level OptLev) {
1276 return DIDescriptor::ValidDebugInfo(SPI.getContext(), OptLev);
1277 }
1278
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001279 /// isValidDebugInfoIntrinsic - Return true if FSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001280 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001281 bool isValidDebugInfoIntrinsic(DbgFuncStartInst &FSI,
1282 CodeGenOpt::Level OptLev) {
1283 return DIDescriptor::ValidDebugInfo(FSI.getSubprogram(), OptLev);
1284 }
1285
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001286 /// isValidDebugInfoIntrinsic - Return true if RSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001287 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001288 bool isValidDebugInfoIntrinsic(DbgRegionStartInst &RSI,
1289 CodeGenOpt::Level OptLev) {
1290 return DIDescriptor::ValidDebugInfo(RSI.getContext(), OptLev);
1291 }
1292
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001293 /// isValidDebugInfoIntrinsic - Return true if REI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001294 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001295 bool isValidDebugInfoIntrinsic(DbgRegionEndInst &REI,
1296 CodeGenOpt::Level OptLev) {
1297 return DIDescriptor::ValidDebugInfo(REI.getContext(), OptLev);
1298 }
1299
1300
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001301 /// isValidDebugInfoIntrinsic - Return true if DI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001302 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001303 bool isValidDebugInfoIntrinsic(DbgDeclareInst &DI,
1304 CodeGenOpt::Level OptLev) {
1305 return DIDescriptor::ValidDebugInfo(DI.getVariable(), OptLev);
1306 }
1307
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001308 /// ExtractDebugLocation - Extract debug location information
Devang Patel9e529c32009-07-02 01:15:24 +00001309 /// from llvm.dbg.stoppoint intrinsic.
1310 DebugLoc ExtractDebugLocation(DbgStopPointInst &SPI,
Devang Patel9e529c32009-07-02 01:15:24 +00001311 DebugLocTracker &DebugLocInfo) {
1312 DebugLoc DL;
1313 Value *Context = SPI.getContext();
Devang Patel9e529c32009-07-02 01:15:24 +00001314
1315 // If this location is already tracked then use it.
Devang Patela1434042009-10-01 01:15:28 +00001316 DebugLocTuple Tuple(cast<MDNode>(Context), NULL, SPI.getLine(),
Devang Patel9e529c32009-07-02 01:15:24 +00001317 SPI.getColumn());
1318 DenseMap<DebugLocTuple, unsigned>::iterator II
1319 = DebugLocInfo.DebugIdMap.find(Tuple);
1320 if (II != DebugLocInfo.DebugIdMap.end())
1321 return DebugLoc::get(II->second);
1322
1323 // Add a new location entry.
1324 unsigned Id = DebugLocInfo.DebugLocations.size();
1325 DebugLocInfo.DebugLocations.push_back(Tuple);
1326 DebugLocInfo.DebugIdMap[Tuple] = Id;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001327
Devang Patel9e529c32009-07-02 01:15:24 +00001328 return DebugLoc::get(Id);
1329 }
1330
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001331 /// ExtractDebugLocation - Extract debug location information
Devang Patel1b75f442009-09-16 18:20:05 +00001332 /// from DILocation.
1333 DebugLoc ExtractDebugLocation(DILocation &Loc,
1334 DebugLocTracker &DebugLocInfo) {
1335 DebugLoc DL;
1336 MDNode *Context = Loc.getScope().getNode();
Devang Patela1434042009-10-01 01:15:28 +00001337 MDNode *InlinedLoc = NULL;
1338 if (!Loc.getOrigLocation().isNull())
1339 InlinedLoc = Loc.getOrigLocation().getNode();
Devang Patel1b75f442009-09-16 18:20:05 +00001340 // If this location is already tracked then use it.
Devang Patela1434042009-10-01 01:15:28 +00001341 DebugLocTuple Tuple(Context, InlinedLoc, Loc.getLineNumber(),
Daniel Dunbara279bc32009-09-20 02:20:51 +00001342 Loc.getColumnNumber());
Devang Patel1b75f442009-09-16 18:20:05 +00001343 DenseMap<DebugLocTuple, unsigned>::iterator II
1344 = DebugLocInfo.DebugIdMap.find(Tuple);
1345 if (II != DebugLocInfo.DebugIdMap.end())
1346 return DebugLoc::get(II->second);
1347
1348 // Add a new location entry.
1349 unsigned Id = DebugLocInfo.DebugLocations.size();
1350 DebugLocInfo.DebugLocations.push_back(Tuple);
1351 DebugLocInfo.DebugIdMap[Tuple] = Id;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001352
Devang Patel1b75f442009-09-16 18:20:05 +00001353 return DebugLoc::get(Id);
1354 }
1355
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001356 /// ExtractDebugLocation - Extract debug location information
Devang Patel9e529c32009-07-02 01:15:24 +00001357 /// from llvm.dbg.func_start intrinsic.
1358 DebugLoc ExtractDebugLocation(DbgFuncStartInst &FSI,
Devang Patel9e529c32009-07-02 01:15:24 +00001359 DebugLocTracker &DebugLocInfo) {
1360 DebugLoc DL;
1361 Value *SP = FSI.getSubprogram();
Devang Patel9e529c32009-07-02 01:15:24 +00001362
Devang Patele4b27562009-08-28 23:24:31 +00001363 DISubprogram Subprogram(cast<MDNode>(SP));
Devang Patel9e529c32009-07-02 01:15:24 +00001364 unsigned Line = Subprogram.getLineNumber();
1365 DICompileUnit CU(Subprogram.getCompileUnit());
1366
1367 // If this location is already tracked then use it.
Devang Patela1434042009-10-01 01:15:28 +00001368 DebugLocTuple Tuple(CU.getNode(), NULL, Line, /* Column */ 0);
Devang Patel9e529c32009-07-02 01:15:24 +00001369 DenseMap<DebugLocTuple, unsigned>::iterator II
1370 = DebugLocInfo.DebugIdMap.find(Tuple);
1371 if (II != DebugLocInfo.DebugIdMap.end())
1372 return DebugLoc::get(II->second);
1373
1374 // Add a new location entry.
1375 unsigned Id = DebugLocInfo.DebugLocations.size();
1376 DebugLocInfo.DebugLocations.push_back(Tuple);
1377 DebugLocInfo.DebugIdMap[Tuple] = Id;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001378
Devang Patel9e529c32009-07-02 01:15:24 +00001379 return DebugLoc::get(Id);
1380 }
1381
1382 /// isInlinedFnStart - Return true if FSI is starting an inlined function.
1383 bool isInlinedFnStart(DbgFuncStartInst &FSI, const Function *CurrentFn) {
Devang Patele4b27562009-08-28 23:24:31 +00001384 DISubprogram Subprogram(cast<MDNode>(FSI.getSubprogram()));
Devang Patel9e529c32009-07-02 01:15:24 +00001385 if (Subprogram.describes(CurrentFn))
1386 return false;
1387
1388 return true;
1389 }
1390
1391 /// isInlinedFnEnd - Return true if REI is ending an inlined function.
1392 bool isInlinedFnEnd(DbgRegionEndInst &REI, const Function *CurrentFn) {
Devang Patele4b27562009-08-28 23:24:31 +00001393 DISubprogram Subprogram(cast<MDNode>(REI.getContext()));
Devang Patel9e529c32009-07-02 01:15:24 +00001394 if (Subprogram.isNull() || Subprogram.describes(CurrentFn))
1395 return false;
1396
1397 return true;
1398 }
Torok Edwin620f2802008-12-16 09:07:36 +00001399}