blob: b1eff9efe16a8ca932e280233d1af9aeb26409b6 [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 Patel65dbc902009-11-25 17:36:49 +000081StringRef
Devang Patel5ccdd102009-09-29 18:40:58 +000082DIDescriptor::getStringField(unsigned Elt) const {
Devang Patele4b27562009-08-28 23:24:31 +000083 if (DbgNode == 0)
Devang Patel65dbc902009-11-25 17:36:49 +000084 return StringRef();
Chris Lattnera45664f2008-11-10 02:56:27 +000085
Daniel Dunbarf612ff62009-09-19 20:40:05 +000086 if (Elt < DbgNode->getNumElements())
Devang Patel65dbc902009-11-25 17:36:49 +000087 if (MDString *MDS = dyn_cast_or_null<MDString>(DbgNode->getElement(Elt)))
88 return MDS->getString();
Daniel Dunbarf612ff62009-09-19 20:40:05 +000089
Devang Patel65dbc902009-11-25 17:36:49 +000090 return StringRef();
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 Patel65dbc902009-11-25 17:36:49 +0000310 StringRef N = getFilename();
311 if (N.empty())
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 Patel65dbc902009-11-25 17:36:49 +0000366 if (getDisplayName().empty())
Devang Patel84c73e92009-11-06 17:58:12 +0000367 return false;
368
Devang Patelb79b5352009-01-19 23:21:49 +0000369 if (getContext().isNull())
370 return false;
371
372 DICompileUnit CU = getCompileUnit();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000373 if (!CU.isNull() && !CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000374 return false;
375
376 DIType Ty = getType();
377 if (!Ty.Verify())
378 return false;
379
380 if (!getGlobal())
381 return false;
382
383 return true;
384}
385
386/// Verify - Verify that a variable descriptor is well formed.
387bool DIVariable::Verify() const {
388 if (isNull())
389 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000390
Devang Patelb79b5352009-01-19 23:21:49 +0000391 if (getContext().isNull())
392 return false;
393
394 DIType Ty = getType();
395 if (!Ty.Verify())
396 return false;
397
Devang Patelb79b5352009-01-19 23:21:49 +0000398 return true;
399}
400
Devang Patel36375ee2009-02-17 21:23:59 +0000401/// getOriginalTypeSize - If this type is derived from a base type then
402/// return base type size.
403uint64_t DIDerivedType::getOriginalTypeSize() const {
Devang Patel61ecbd12009-11-04 23:48:00 +0000404 unsigned Tag = getTag();
405 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
406 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
407 Tag == dwarf::DW_TAG_restrict_type) {
408 DIType BaseType = getTypeDerivedFrom();
Devang Patel5ebfa2d2009-11-06 18:24:05 +0000409 // If this type is not derived from any type then take conservative
410 // approach.
411 if (BaseType.isNull())
412 return getSizeInBits();
Devang Patel61ecbd12009-11-04 23:48:00 +0000413 if (BaseType.isDerivedType())
414 return DIDerivedType(BaseType.getNode()).getOriginalTypeSize();
415 else
416 return BaseType.getSizeInBits();
417 }
418
419 return getSizeInBits();
Devang Patel36375ee2009-02-17 21:23:59 +0000420}
Devang Patelb79b5352009-01-19 23:21:49 +0000421
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000422/// describes - Return true if this subprogram provides debugging
423/// information for the function F.
424bool DISubprogram::describes(const Function *F) {
425 assert (F && "Invalid function");
Devang Patel65dbc902009-11-25 17:36:49 +0000426 StringRef Name = getLinkageName();
427 if (Name.empty())
Devang Patel5ccdd102009-09-29 18:40:58 +0000428 Name = getName();
Devang Patel65dbc902009-11-25 17:36:49 +0000429 if (F->getName() == Name)
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000430 return true;
431 return false;
432}
433
Devang Patel65dbc902009-11-25 17:36:49 +0000434StringRef DIScope::getFilename() const {
Devang Patelecbeb1a2009-09-30 22:34:41 +0000435 if (isLexicalBlock())
436 return DILexicalBlock(DbgNode).getFilename();
437 else if (isSubprogram())
438 return DISubprogram(DbgNode).getFilename();
439 else if (isCompileUnit())
440 return DICompileUnit(DbgNode).getFilename();
441 else
442 assert (0 && "Invalid DIScope!");
Devang Patel65dbc902009-11-25 17:36:49 +0000443 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000444}
445
Devang Patel65dbc902009-11-25 17:36:49 +0000446StringRef DIScope::getDirectory() const {
Devang Patelecbeb1a2009-09-30 22:34:41 +0000447 if (isLexicalBlock())
448 return DILexicalBlock(DbgNode).getDirectory();
449 else if (isSubprogram())
450 return DISubprogram(DbgNode).getDirectory();
451 else if (isCompileUnit())
452 return DICompileUnit(DbgNode).getDirectory();
453 else
454 assert (0 && "Invalid DIScope!");
Devang Patel65dbc902009-11-25 17:36:49 +0000455 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000456}
457
Chris Lattnera45664f2008-11-10 02:56:27 +0000458//===----------------------------------------------------------------------===//
Devang Patel7136a652009-07-01 22:10:23 +0000459// DIDescriptor: dump routines for all descriptors.
460//===----------------------------------------------------------------------===//
461
462
463/// dump - Print descriptor.
464void DIDescriptor::dump() const {
Devang Patele4b27562009-08-28 23:24:31 +0000465 errs() << "[" << dwarf::TagString(getTag()) << "] ";
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000466 errs().write_hex((intptr_t) &*DbgNode) << ']';
Devang Patel7136a652009-07-01 22:10:23 +0000467}
468
469/// dump - Print compile unit.
470void DICompileUnit::dump() const {
471 if (getLanguage())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000472 errs() << " [" << dwarf::LanguageString(getLanguage()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000473
Devang Patel5ccdd102009-09-29 18:40:58 +0000474 errs() << " [" << getDirectory() << "/" << getFilename() << " ]";
Devang Patel7136a652009-07-01 22:10:23 +0000475}
476
477/// dump - Print type.
478void DIType::dump() const {
479 if (isNull()) return;
480
Devang Patel65dbc902009-11-25 17:36:49 +0000481 StringRef Res = getName();
482 if (!Res.empty())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000483 errs() << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000484
485 unsigned Tag = getTag();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000486 errs() << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000487
488 // TODO : Print context
489 getCompileUnit().dump();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000490 errs() << " ["
491 << getLineNumber() << ", "
Chris Lattnera81d29b2009-08-23 07:33:14 +0000492 << getSizeInBits() << ", "
493 << getAlignInBits() << ", "
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000494 << getOffsetInBits()
Chris Lattnera81d29b2009-08-23 07:33:14 +0000495 << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000496
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000497 if (isPrivate())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000498 errs() << " [private] ";
Devang Patel7136a652009-07-01 22:10:23 +0000499 else if (isProtected())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000500 errs() << " [protected] ";
Devang Patel7136a652009-07-01 22:10:23 +0000501
502 if (isForwardDecl())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000503 errs() << " [fwd] ";
Devang Patel7136a652009-07-01 22:10:23 +0000504
Devang Patel6ceea332009-08-31 18:49:10 +0000505 if (isBasicType())
Devang Patele4b27562009-08-28 23:24:31 +0000506 DIBasicType(DbgNode).dump();
Devang Patel6ceea332009-08-31 18:49:10 +0000507 else if (isDerivedType())
Devang Patele4b27562009-08-28 23:24:31 +0000508 DIDerivedType(DbgNode).dump();
Devang Patel6ceea332009-08-31 18:49:10 +0000509 else if (isCompositeType())
Devang Patele4b27562009-08-28 23:24:31 +0000510 DICompositeType(DbgNode).dump();
Devang Patel7136a652009-07-01 22:10:23 +0000511 else {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000512 errs() << "Invalid DIType\n";
Devang Patel7136a652009-07-01 22:10:23 +0000513 return;
514 }
515
Chris Lattnera81d29b2009-08-23 07:33:14 +0000516 errs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000517}
518
519/// dump - Print basic type.
520void DIBasicType::dump() const {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000521 errs() << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000522}
523
524/// dump - Print derived type.
525void DIDerivedType::dump() const {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000526 errs() << "\n\t Derived From: "; getTypeDerivedFrom().dump();
Devang Patel7136a652009-07-01 22:10:23 +0000527}
528
529/// dump - Print composite type.
530void DICompositeType::dump() const {
531 DIArray A = getTypeArray();
532 if (A.isNull())
533 return;
Chris Lattnera81d29b2009-08-23 07:33:14 +0000534 errs() << " [" << A.getNumElements() << " elements]";
Devang Patel7136a652009-07-01 22:10:23 +0000535}
536
537/// dump - Print global.
538void DIGlobal::dump() const {
Devang Patel65dbc902009-11-25 17:36:49 +0000539 StringRef Res = getName();
540 if (!Res.empty())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000541 errs() << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000542
543 unsigned Tag = getTag();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000544 errs() << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000545
546 // TODO : Print context
547 getCompileUnit().dump();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000548 errs() << " [" << getLineNumber() << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000549
550 if (isLocalToUnit())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000551 errs() << " [local] ";
Devang Patel7136a652009-07-01 22:10:23 +0000552
553 if (isDefinition())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000554 errs() << " [def] ";
Devang Patel7136a652009-07-01 22:10:23 +0000555
Devang Patel6ceea332009-08-31 18:49:10 +0000556 if (isGlobalVariable())
Devang Patele4b27562009-08-28 23:24:31 +0000557 DIGlobalVariable(DbgNode).dump();
Devang Patel7136a652009-07-01 22:10:23 +0000558
Chris Lattnera81d29b2009-08-23 07:33:14 +0000559 errs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000560}
561
562/// dump - Print subprogram.
563void DISubprogram::dump() const {
Devang Patel65dbc902009-11-25 17:36:49 +0000564 StringRef Res = getName();
565 if (!Res.empty())
Devang Patel82dfc0c2009-08-31 22:47:13 +0000566 errs() << " [" << Res << "] ";
567
568 unsigned Tag = getTag();
569 errs() << " [" << dwarf::TagString(Tag) << "] ";
570
571 // TODO : Print context
572 getCompileUnit().dump();
573 errs() << " [" << getLineNumber() << "] ";
574
575 if (isLocalToUnit())
576 errs() << " [local] ";
577
578 if (isDefinition())
579 errs() << " [def] ";
580
581 errs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000582}
583
584/// dump - Print global variable.
585void DIGlobalVariable::dump() const {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000586 errs() << " [";
587 getGlobal()->dump();
588 errs() << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000589}
590
591/// dump - Print variable.
592void DIVariable::dump() const {
Devang Patel65dbc902009-11-25 17:36:49 +0000593 StringRef Res = getName();
594 if (!Res.empty())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000595 errs() << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000596
597 getCompileUnit().dump();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000598 errs() << " [" << getLineNumber() << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000599 getType().dump();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000600 errs() << "\n";
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000601
602 // FIXME: Dump complex addresses
Devang Patel7136a652009-07-01 22:10:23 +0000603}
604
605//===----------------------------------------------------------------------===//
Chris Lattnera45664f2008-11-10 02:56:27 +0000606// DIFactory: Basic Helpers
607//===----------------------------------------------------------------------===//
608
Bill Wendlingdc817b62009-05-14 18:26:15 +0000609DIFactory::DIFactory(Module &m)
Devang Patel427ef4e2009-11-17 22:39:08 +0000610 : M(m), VMContext(M.getContext()), DeclareFn(0) {
Devang Patel3ddf7042009-11-13 02:27:33 +0000611 EmptyStructPtr = PointerType::getUnqual(StructType::get(VMContext));
612}
Chris Lattner497a7a82008-11-10 04:10:34 +0000613
Chris Lattnera45664f2008-11-10 02:56:27 +0000614Constant *DIFactory::GetTagConstant(unsigned TAG) {
Devang Patel6906ba52009-01-20 19:22:03 +0000615 assert((TAG & LLVMDebugVersionMask) == 0 &&
Chris Lattnera45664f2008-11-10 02:56:27 +0000616 "Tag too large for debug encoding!");
Owen Anderson1d0be152009-08-13 21:58:54 +0000617 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
Chris Lattnera45664f2008-11-10 02:56:27 +0000618}
619
Chris Lattnera45664f2008-11-10 02:56:27 +0000620//===----------------------------------------------------------------------===//
621// DIFactory: Primary Constructors
622//===----------------------------------------------------------------------===//
623
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000624/// GetOrCreateArray - Create an descriptor for an array of descriptors.
Chris Lattnera45664f2008-11-10 02:56:27 +0000625/// This implicitly uniques the arrays created.
626DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
Devang Patele4b27562009-08-28 23:24:31 +0000627 SmallVector<Value*, 16> Elts;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000628
Devang Patele4b27562009-08-28 23:24:31 +0000629 if (NumTys == 0)
630 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)));
631 else
632 for (unsigned i = 0; i != NumTys; ++i)
633 Elts.push_back(Tys[i].getNode());
Devang Patel82459882009-08-26 05:01:18 +0000634
Devang Patele4b27562009-08-28 23:24:31 +0000635 return DIArray(MDNode::get(VMContext,Elts.data(), Elts.size()));
Chris Lattnera45664f2008-11-10 02:56:27 +0000636}
637
638/// GetOrCreateSubrange - Create a descriptor for a value range. This
639/// implicitly uniques the values returned.
640DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
Devang Patele4b27562009-08-28 23:24:31 +0000641 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000642 GetTagConstant(dwarf::DW_TAG_subrange_type),
Owen Anderson1d0be152009-08-13 21:58:54 +0000643 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
644 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
Chris Lattnera45664f2008-11-10 02:56:27 +0000645 };
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000646
Devang Patele4b27562009-08-28 23:24:31 +0000647 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000648}
649
650
651
652/// CreateCompileUnit - Create a new descriptor for the specified compile
653/// unit. Note that this does not unique compile units within the module.
654DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
Devang Patel65dbc902009-11-25 17:36:49 +0000655 StringRef Filename,
656 StringRef Directory,
657 StringRef Producer,
Devang Pateldd9db662009-01-30 18:20:31 +0000658 bool isMain,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000659 bool isOptimized,
Devang Patel65dbc902009-11-25 17:36:49 +0000660 StringRef Flags,
Devang Patel13319ce2009-02-17 22:43:44 +0000661 unsigned RunTimeVer) {
Devang Patele4b27562009-08-28 23:24:31 +0000662 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000663 GetTagConstant(dwarf::DW_TAG_compile_unit),
Devang Patele4b27562009-08-28 23:24:31 +0000664 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Owen Anderson1d0be152009-08-13 21:58:54 +0000665 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
Devang Patele4b27562009-08-28 23:24:31 +0000666 MDString::get(VMContext, Filename),
667 MDString::get(VMContext, Directory),
668 MDString::get(VMContext, Producer),
Owen Anderson1d0be152009-08-13 21:58:54 +0000669 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
670 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
Devang Patele4b27562009-08-28 23:24:31 +0000671 MDString::get(VMContext, Flags),
Owen Anderson1d0be152009-08-13 21:58:54 +0000672 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
Chris Lattnera45664f2008-11-10 02:56:27 +0000673 };
Devang Patele4b27562009-08-28 23:24:31 +0000674
675 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000676}
677
678/// CreateEnumerator - Create a single enumerator value.
Devang Patel65dbc902009-11-25 17:36:49 +0000679DIEnumerator DIFactory::CreateEnumerator(StringRef Name, uint64_t Val){
Devang Patele4b27562009-08-28 23:24:31 +0000680 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000681 GetTagConstant(dwarf::DW_TAG_enumerator),
Devang Patele4b27562009-08-28 23:24:31 +0000682 MDString::get(VMContext, Name),
Owen Anderson1d0be152009-08-13 21:58:54 +0000683 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
Chris Lattnera45664f2008-11-10 02:56:27 +0000684 };
Devang Patele4b27562009-08-28 23:24:31 +0000685 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000686}
687
688
689/// CreateBasicType - Create a basic type like int, float, etc.
690DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000691 StringRef Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000692 DICompileUnit CompileUnit,
693 unsigned LineNumber,
694 uint64_t SizeInBits,
695 uint64_t AlignInBits,
696 uint64_t OffsetInBits, unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000697 unsigned Encoding) {
Devang Patele4b27562009-08-28 23:24:31 +0000698 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000699 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patele4b27562009-08-28 23:24:31 +0000700 Context.getNode(),
701 MDString::get(VMContext, Name),
702 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000703 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
704 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
705 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
706 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
707 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
708 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
Chris Lattnera45664f2008-11-10 02:56:27 +0000709 };
Devang Patele4b27562009-08-28 23:24:31 +0000710 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000711}
712
Devang Patelac16d442009-10-26 16:54:35 +0000713
714/// CreateBasicType - Create a basic type like int, float, etc.
715DIBasicType DIFactory::CreateBasicTypeEx(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000716 StringRef Name,
Devang Patelac16d442009-10-26 16:54:35 +0000717 DICompileUnit CompileUnit,
718 unsigned LineNumber,
719 Constant *SizeInBits,
720 Constant *AlignInBits,
721 Constant *OffsetInBits, unsigned Flags,
722 unsigned Encoding) {
723 Value *Elts[] = {
724 GetTagConstant(dwarf::DW_TAG_base_type),
725 Context.getNode(),
726 MDString::get(VMContext, Name),
727 CompileUnit.getNode(),
728 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
729 SizeInBits,
730 AlignInBits,
731 OffsetInBits,
732 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
733 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
734 };
735 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
736}
737
738
Chris Lattnera45664f2008-11-10 02:56:27 +0000739/// CreateDerivedType - Create a derived type like const qualified type,
740/// pointer, typedef, etc.
741DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
742 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000743 StringRef Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000744 DICompileUnit CompileUnit,
745 unsigned LineNumber,
746 uint64_t SizeInBits,
747 uint64_t AlignInBits,
748 uint64_t OffsetInBits,
749 unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000750 DIType DerivedFrom) {
Devang Patele4b27562009-08-28 23:24:31 +0000751 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000752 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000753 Context.getNode(),
754 MDString::get(VMContext, Name),
755 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000756 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
757 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
758 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
759 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
760 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patele4b27562009-08-28 23:24:31 +0000761 DerivedFrom.getNode(),
Chris Lattnera45664f2008-11-10 02:56:27 +0000762 };
Devang Patele4b27562009-08-28 23:24:31 +0000763 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000764}
765
Devang Patelac16d442009-10-26 16:54:35 +0000766
767/// CreateDerivedType - Create a derived type like const qualified type,
768/// pointer, typedef, etc.
769DIDerivedType DIFactory::CreateDerivedTypeEx(unsigned Tag,
770 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000771 StringRef Name,
Devang Patelac16d442009-10-26 16:54:35 +0000772 DICompileUnit CompileUnit,
773 unsigned LineNumber,
774 Constant *SizeInBits,
775 Constant *AlignInBits,
776 Constant *OffsetInBits,
777 unsigned Flags,
778 DIType DerivedFrom) {
779 Value *Elts[] = {
780 GetTagConstant(Tag),
781 Context.getNode(),
782 MDString::get(VMContext, Name),
783 CompileUnit.getNode(),
784 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
785 SizeInBits,
786 AlignInBits,
787 OffsetInBits,
788 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
789 DerivedFrom.getNode(),
790 };
791 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
792}
793
794
Chris Lattnera45664f2008-11-10 02:56:27 +0000795/// CreateCompositeType - Create a composite type like array, struct, etc.
796DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
797 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000798 StringRef Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000799 DICompileUnit CompileUnit,
800 unsigned LineNumber,
801 uint64_t SizeInBits,
802 uint64_t AlignInBits,
803 uint64_t OffsetInBits,
804 unsigned Flags,
805 DIType DerivedFrom,
Devang Patel13319ce2009-02-17 22:43:44 +0000806 DIArray Elements,
807 unsigned RuntimeLang) {
Owen Andersone277fed2009-07-07 16:31:25 +0000808
Devang Patele4b27562009-08-28 23:24:31 +0000809 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000810 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000811 Context.getNode(),
812 MDString::get(VMContext, Name),
813 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000814 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
815 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
816 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
817 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
818 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patele4b27562009-08-28 23:24:31 +0000819 DerivedFrom.getNode(),
820 Elements.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000821 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
Chris Lattnera45664f2008-11-10 02:56:27 +0000822 };
Devang Patele4b27562009-08-28 23:24:31 +0000823 return DICompositeType(MDNode::get(VMContext, &Elts[0], 12));
Chris Lattnera45664f2008-11-10 02:56:27 +0000824}
825
826
Devang Patelac16d442009-10-26 16:54:35 +0000827/// CreateCompositeType - Create a composite type like array, struct, etc.
828DICompositeType DIFactory::CreateCompositeTypeEx(unsigned Tag,
829 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000830 StringRef Name,
Devang Patelac16d442009-10-26 16:54:35 +0000831 DICompileUnit CompileUnit,
832 unsigned LineNumber,
833 Constant *SizeInBits,
834 Constant *AlignInBits,
835 Constant *OffsetInBits,
836 unsigned Flags,
837 DIType DerivedFrom,
838 DIArray Elements,
839 unsigned RuntimeLang) {
840
841 Value *Elts[] = {
842 GetTagConstant(Tag),
843 Context.getNode(),
844 MDString::get(VMContext, Name),
845 CompileUnit.getNode(),
846 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
847 SizeInBits,
848 AlignInBits,
849 OffsetInBits,
850 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
851 DerivedFrom.getNode(),
852 Elements.getNode(),
853 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
854 };
855 return DICompositeType(MDNode::get(VMContext, &Elts[0], 12));
856}
857
858
Chris Lattnera45664f2008-11-10 02:56:27 +0000859/// CreateSubprogram - Create a new descriptor for the specified subprogram.
860/// See comments in DISubprogram for descriptions of these fields. This
861/// method does not unique the generated descriptors.
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000862DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000863 StringRef Name,
864 StringRef DisplayName,
865 StringRef LinkageName,
Chris Lattnera45664f2008-11-10 02:56:27 +0000866 DICompileUnit CompileUnit,
867 unsigned LineNo, DIType Type,
868 bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000869 bool isDefinition) {
Devang Patel854967e2008-12-17 22:39:29 +0000870
Devang Patele4b27562009-08-28 23:24:31 +0000871 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000872 GetTagConstant(dwarf::DW_TAG_subprogram),
Devang Patele4b27562009-08-28 23:24:31 +0000873 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
874 Context.getNode(),
875 MDString::get(VMContext, Name),
876 MDString::get(VMContext, DisplayName),
877 MDString::get(VMContext, LinkageName),
878 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000879 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000880 Type.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000881 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
882 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition)
Chris Lattnera45664f2008-11-10 02:56:27 +0000883 };
Devang Patele4b27562009-08-28 23:24:31 +0000884 return DISubprogram(MDNode::get(VMContext, &Elts[0], 11));
Chris Lattnera45664f2008-11-10 02:56:27 +0000885}
886
Devang Patele3a18de2009-12-01 23:09:02 +0000887/// CreateSubprogramDefinition - Create new subprogram descriptor for the
888/// given declaration.
889DISubprogram DIFactory::CreateSubprogramDefinition(DISubprogram &SPDeclaration) {
890 if (SPDeclaration.isDefinition())
891 return DISubprogram(SPDeclaration.getNode());
892
893 MDNode *DeclNode = SPDeclaration.getNode();
894 Value *Elts[] = {
895 GetTagConstant(dwarf::DW_TAG_subprogram),
896 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
897 DeclNode->getElement(2), // Context
898 DeclNode->getElement(3), // Name
899 DeclNode->getElement(4), // DisplayName
900 DeclNode->getElement(5), // LinkageName
901 DeclNode->getElement(6), // CompileUnit
902 DeclNode->getElement(7), // LineNo
903 DeclNode->getElement(8), // Type
904 DeclNode->getElement(9), // isLocalToUnit
905 ConstantInt::get(Type::getInt1Ty(VMContext), true)
906 };
907 return DISubprogram(MDNode::get(VMContext, &Elts[0], 11));
908}
909
Chris Lattnera45664f2008-11-10 02:56:27 +0000910/// CreateGlobalVariable - Create a new descriptor for the specified global.
911DIGlobalVariable
Devang Patel65dbc902009-11-25 17:36:49 +0000912DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
913 StringRef DisplayName,
914 StringRef LinkageName,
Chris Lattnera45664f2008-11-10 02:56:27 +0000915 DICompileUnit CompileUnit,
916 unsigned LineNo, DIType Type,bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000917 bool isDefinition, llvm::GlobalVariable *Val) {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000918 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000919 GetTagConstant(dwarf::DW_TAG_variable),
Devang Patele4b27562009-08-28 23:24:31 +0000920 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
921 Context.getNode(),
922 MDString::get(VMContext, Name),
923 MDString::get(VMContext, DisplayName),
924 MDString::get(VMContext, LinkageName),
925 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000926 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000927 Type.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000928 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
929 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
Devang Patele4b27562009-08-28 23:24:31 +0000930 Val
Chris Lattnera45664f2008-11-10 02:56:27 +0000931 };
Devang Patele4b27562009-08-28 23:24:31 +0000932
933 Value *const *Vs = &Elts[0];
934 MDNode *Node = MDNode::get(VMContext,Vs, 12);
935
936 // Create a named metadata so that we do not lose this mdnode.
937 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
938 NMD->addElement(Node);
939
940 return DIGlobalVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +0000941}
942
943
944/// CreateVariable - Create a new descriptor for the specified variable.
945DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000946 StringRef Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000947 DICompileUnit CompileUnit, unsigned LineNo,
Devang Pateldd9db662009-01-30 18:20:31 +0000948 DIType Type) {
Devang Patele4b27562009-08-28 23:24:31 +0000949 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000950 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000951 Context.getNode(),
952 MDString::get(VMContext, Name),
953 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000954 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000955 Type.getNode(),
Chris Lattnera45664f2008-11-10 02:56:27 +0000956 };
Devang Patele4b27562009-08-28 23:24:31 +0000957 return DIVariable(MDNode::get(VMContext, &Elts[0], 6));
Chris Lattnera45664f2008-11-10 02:56:27 +0000958}
959
960
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000961/// CreateComplexVariable - Create a new descriptor for the specified variable
962/// which has a complex address expression for its address.
963DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
964 const std::string &Name,
965 DICompileUnit CompileUnit,
966 unsigned LineNo,
967 DIType Type, SmallVector<Value *, 9> &addr) {
968 SmallVector<Value *, 9> Elts;
969 Elts.push_back(GetTagConstant(Tag));
970 Elts.push_back(Context.getNode());
971 Elts.push_back(MDString::get(VMContext, Name));
972 Elts.push_back(CompileUnit.getNode());
973 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
974 Elts.push_back(Type.getNode());
975 Elts.insert(Elts.end(), addr.begin(), addr.end());
976
977 return DIVariable(MDNode::get(VMContext, &Elts[0], 6+addr.size()));
978}
979
980
Chris Lattnera45664f2008-11-10 02:56:27 +0000981/// CreateBlock - This creates a descriptor for a lexical block with the
Owen Anderson99035272009-07-07 17:12:53 +0000982/// specified parent VMContext.
Devang Patel5e005d82009-08-31 22:00:15 +0000983DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context) {
Devang Patele4b27562009-08-28 23:24:31 +0000984 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000985 GetTagConstant(dwarf::DW_TAG_lexical_block),
Devang Patele4b27562009-08-28 23:24:31 +0000986 Context.getNode()
Chris Lattnera45664f2008-11-10 02:56:27 +0000987 };
Devang Patel5e005d82009-08-31 22:00:15 +0000988 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 2));
Chris Lattnera45664f2008-11-10 02:56:27 +0000989}
990
Devang Patelf98d8fe2009-09-01 01:14:15 +0000991/// CreateLocation - Creates a debug info location.
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000992DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
Daniel Dunbara279bc32009-09-20 02:20:51 +0000993 DIScope S, DILocation OrigLoc) {
Devang Patelf98d8fe2009-09-01 01:14:15 +0000994 Value *Elts[] = {
995 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
996 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
997 S.getNode(),
998 OrigLoc.getNode(),
999 };
1000 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1001}
1002
Devang Patele54a5e82009-11-23 19:11:20 +00001003/// CreateLocation - Creates a debug info location.
1004DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
1005 DIScope S, MDNode *OrigLoc) {
1006 Value *Elts[] = {
1007 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1008 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
1009 S.getNode(),
1010 OrigLoc
1011 };
1012 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1013}
Chris Lattnera45664f2008-11-10 02:56:27 +00001014
1015//===----------------------------------------------------------------------===//
1016// DIFactory: Routines for inserting code into a function
1017//===----------------------------------------------------------------------===//
1018
Chris Lattnera45664f2008-11-10 02:56:27 +00001019/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Devang Patel6daf99b2009-11-10 22:05:35 +00001020Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
Devang Patel3ddf7042009-11-13 02:27:33 +00001021 Instruction *InsertBefore) {
1022 // Cast the storage to a {}* for the call to llvm.dbg.declare.
1023 Storage = new BitCastInst(Storage, EmptyStructPtr, "", InsertBefore);
1024
Chris Lattnera45664f2008-11-10 02:56:27 +00001025 if (!DeclareFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +00001026 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1027
Devang Patele4b27562009-08-28 23:24:31 +00001028 Value *Args[] = { Storage, D.getNode() };
Devang Patel6daf99b2009-11-10 22:05:35 +00001029 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
Mike Stumpe4250392009-10-01 22:08:58 +00001030}
1031
1032/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Devang Patel6daf99b2009-11-10 22:05:35 +00001033Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
Devang Patel3ddf7042009-11-13 02:27:33 +00001034 BasicBlock *InsertAtEnd) {
1035 // Cast the storage to a {}* for the call to llvm.dbg.declare.
1036 Storage = new BitCastInst(Storage, EmptyStructPtr, "", InsertAtEnd);
1037
Mike Stumpe4250392009-10-01 22:08:58 +00001038 if (!DeclareFn)
1039 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1040
1041 Value *Args[] = { Storage, D.getNode() };
Devang Patel6daf99b2009-11-10 22:05:35 +00001042 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);
Chris Lattnera45664f2008-11-10 02:56:27 +00001043}
Torok Edwin620f2802008-12-16 09:07:36 +00001044
Devang Patele4b27562009-08-28 23:24:31 +00001045
Devang Pateld2f79a12009-07-28 19:55:13 +00001046//===----------------------------------------------------------------------===//
Devang Patel98c65172009-07-30 18:25:15 +00001047// DebugInfoFinder implementations.
Devang Pateld2f79a12009-07-28 19:55:13 +00001048//===----------------------------------------------------------------------===//
1049
Devang Patel98c65172009-07-30 18:25:15 +00001050/// processModule - Process entire module and collect debug info.
1051void DebugInfoFinder::processModule(Module &M) {
Devang Patele4b27562009-08-28 23:24:31 +00001052
Devang Patelbeab41b2009-10-07 22:04:08 +00001053 MetadataContext &TheMetadata = M.getContext().getMetadata();
1054 unsigned MDDbgKind = TheMetadata.getMDKind("dbg");
Devang Patel70d75ca2009-11-12 19:02:56 +00001055
Devang Pateld2f79a12009-07-28 19:55:13 +00001056 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1057 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
1058 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
1059 ++BI) {
Devang Patel70d75ca2009-11-12 19:02:56 +00001060 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
Devang Patelb4d31302009-07-31 18:18:52 +00001061 processDeclare(DDI);
Devang Patel6daf99b2009-11-10 22:05:35 +00001062 else if (MDDbgKind)
1063 if (MDNode *L = TheMetadata.getMD(MDDbgKind, BI))
1064 processLocation(DILocation(L));
Devang Pateld2f79a12009-07-28 19:55:13 +00001065 }
Devang Patele4b27562009-08-28 23:24:31 +00001066
1067 NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv");
1068 if (!NMD)
1069 return;
1070
1071 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
1072 DIGlobalVariable DIG(cast<MDNode>(NMD->getElement(i)));
Devang Pateld2f79a12009-07-28 19:55:13 +00001073 if (addGlobalVariable(DIG)) {
1074 addCompileUnit(DIG.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +00001075 processType(DIG.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +00001076 }
1077 }
1078}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001079
Devang Patel6daf99b2009-11-10 22:05:35 +00001080/// processLocation - Process DILocation.
1081void DebugInfoFinder::processLocation(DILocation Loc) {
1082 if (Loc.isNull()) return;
1083 DIScope S(Loc.getScope().getNode());
1084 if (S.isNull()) return;
1085 if (S.isCompileUnit())
1086 addCompileUnit(DICompileUnit(S.getNode()));
1087 else if (S.isSubprogram())
1088 processSubprogram(DISubprogram(S.getNode()));
1089 else if (S.isLexicalBlock())
1090 processLexicalBlock(DILexicalBlock(S.getNode()));
1091 processLocation(Loc.getOrigLocation());
1092}
1093
Devang Patel98c65172009-07-30 18:25:15 +00001094/// processType - Process DIType.
1095void DebugInfoFinder::processType(DIType DT) {
Devang Patel72bcdb62009-08-10 22:09:58 +00001096 if (!addType(DT))
Devang Pateld2f79a12009-07-28 19:55:13 +00001097 return;
1098
1099 addCompileUnit(DT.getCompileUnit());
Devang Patel6ceea332009-08-31 18:49:10 +00001100 if (DT.isCompositeType()) {
Devang Patele4b27562009-08-28 23:24:31 +00001101 DICompositeType DCT(DT.getNode());
Devang Patel98c65172009-07-30 18:25:15 +00001102 processType(DCT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001103 DIArray DA = DCT.getTypeArray();
1104 if (!DA.isNull())
1105 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1106 DIDescriptor D = DA.getElement(i);
Devang Patele4b27562009-08-28 23:24:31 +00001107 DIType TypeE = DIType(D.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001108 if (!TypeE.isNull())
Devang Patel98c65172009-07-30 18:25:15 +00001109 processType(TypeE);
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001110 else
Devang Patele4b27562009-08-28 23:24:31 +00001111 processSubprogram(DISubprogram(D.getNode()));
Devang Pateld2f79a12009-07-28 19:55:13 +00001112 }
Devang Patel6ceea332009-08-31 18:49:10 +00001113 } else if (DT.isDerivedType()) {
Devang Patele4b27562009-08-28 23:24:31 +00001114 DIDerivedType DDT(DT.getNode());
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001115 if (!DDT.isNull())
Devang Patel98c65172009-07-30 18:25:15 +00001116 processType(DDT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001117 }
1118}
1119
Devang Patelbeab41b2009-10-07 22:04:08 +00001120/// processLexicalBlock
1121void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
1122 if (LB.isNull())
1123 return;
1124 DIScope Context = LB.getContext();
1125 if (Context.isLexicalBlock())
1126 return processLexicalBlock(DILexicalBlock(Context.getNode()));
1127 else
1128 return processSubprogram(DISubprogram(Context.getNode()));
1129}
1130
Devang Patel98c65172009-07-30 18:25:15 +00001131/// processSubprogram - Process DISubprogram.
1132void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Patele802f1c2009-07-30 17:30:23 +00001133 if (SP.isNull())
1134 return;
Devang Pateld2f79a12009-07-28 19:55:13 +00001135 if (!addSubprogram(SP))
1136 return;
1137 addCompileUnit(SP.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +00001138 processType(SP.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +00001139}
1140
Devang Patelb4d31302009-07-31 18:18:52 +00001141/// processDeclare - Process DbgDeclareInst.
1142void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
Devang Patele4b27562009-08-28 23:24:31 +00001143 DIVariable DV(cast<MDNode>(DDI->getVariable()));
Devang Patelb4d31302009-07-31 18:18:52 +00001144 if (DV.isNull())
1145 return;
1146
Devang Patele4b27562009-08-28 23:24:31 +00001147 if (!NodesSeen.insert(DV.getNode()))
Devang Patelb4d31302009-07-31 18:18:52 +00001148 return;
1149
1150 addCompileUnit(DV.getCompileUnit());
1151 processType(DV.getType());
1152}
1153
Devang Patel72bcdb62009-08-10 22:09:58 +00001154/// addType - Add type into Tys.
1155bool DebugInfoFinder::addType(DIType DT) {
1156 if (DT.isNull())
1157 return false;
1158
Devang Patele4b27562009-08-28 23:24:31 +00001159 if (!NodesSeen.insert(DT.getNode()))
Devang Patel72bcdb62009-08-10 22:09:58 +00001160 return false;
1161
Devang Patele4b27562009-08-28 23:24:31 +00001162 TYs.push_back(DT.getNode());
Devang Patel72bcdb62009-08-10 22:09:58 +00001163 return true;
1164}
1165
Devang Pateld2f79a12009-07-28 19:55:13 +00001166/// addCompileUnit - Add compile unit into CUs.
Devang Patel98c65172009-07-30 18:25:15 +00001167bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001168 if (CU.isNull())
1169 return false;
1170
Devang Patele4b27562009-08-28 23:24:31 +00001171 if (!NodesSeen.insert(CU.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001172 return false;
1173
Devang Patele4b27562009-08-28 23:24:31 +00001174 CUs.push_back(CU.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001175 return true;
1176}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001177
Devang Pateld2f79a12009-07-28 19:55:13 +00001178/// addGlobalVariable - Add global variable into GVs.
Devang Patel98c65172009-07-30 18:25:15 +00001179bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001180 if (DIG.isNull())
1181 return false;
1182
Devang Patele4b27562009-08-28 23:24:31 +00001183 if (!NodesSeen.insert(DIG.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001184 return false;
1185
Devang Patele4b27562009-08-28 23:24:31 +00001186 GVs.push_back(DIG.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001187 return true;
1188}
1189
1190// addSubprogram - Add subprgoram into SPs.
Devang Patel98c65172009-07-30 18:25:15 +00001191bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001192 if (SP.isNull())
1193 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001194
Devang Patele4b27562009-08-28 23:24:31 +00001195 if (!NodesSeen.insert(SP.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001196 return false;
1197
Devang Patele4b27562009-08-28 23:24:31 +00001198 SPs.push_back(SP.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001199 return true;
1200}
1201
Torok Edwin620f2802008-12-16 09:07:36 +00001202namespace llvm {
Bill Wendlingdc817b62009-05-14 18:26:15 +00001203 /// findStopPoint - Find the stoppoint coressponding to this instruction, that
1204 /// is the stoppoint that dominates this instruction.
1205 const DbgStopPointInst *findStopPoint(const Instruction *Inst) {
Torok Edwin620f2802008-12-16 09:07:36 +00001206 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(Inst))
1207 return DSI;
1208
1209 const BasicBlock *BB = Inst->getParent();
1210 BasicBlock::const_iterator I = Inst, B;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001211 while (BB) {
Torok Edwin620f2802008-12-16 09:07:36 +00001212 B = BB->begin();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001213
Torok Edwin620f2802008-12-16 09:07:36 +00001214 // A BB consisting only of a terminator can't have a stoppoint.
Bill Wendlingdc817b62009-05-14 18:26:15 +00001215 while (I != B) {
1216 --I;
1217 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
1218 return DSI;
Torok Edwin620f2802008-12-16 09:07:36 +00001219 }
Bill Wendlingdc817b62009-05-14 18:26:15 +00001220
1221 // This BB didn't have a stoppoint: if there is only one predecessor, look
1222 // for a stoppoint there. We could use getIDom(), but that would require
1223 // dominator info.
Torok Edwin620f2802008-12-16 09:07:36 +00001224 BB = I->getParent()->getUniquePredecessor();
1225 if (BB)
1226 I = BB->getTerminator();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001227 }
1228
Torok Edwin620f2802008-12-16 09:07:36 +00001229 return 0;
1230 }
1231
Bill Wendlingdc817b62009-05-14 18:26:15 +00001232 /// findBBStopPoint - Find the stoppoint corresponding to first real
1233 /// (non-debug intrinsic) instruction in this Basic Block, and return the
1234 /// stoppoint for it.
1235 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB) {
1236 for(BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001237 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
1238 return DSI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001239
1240 // Fallback to looking for stoppoint of unique predecessor. Useful if this
1241 // BB contains no stoppoints, but unique predecessor does.
Torok Edwin620f2802008-12-16 09:07:36 +00001242 BB = BB->getUniquePredecessor();
1243 if (BB)
1244 return findStopPoint(BB->getTerminator());
Bill Wendlingdc817b62009-05-14 18:26:15 +00001245
Torok Edwin620f2802008-12-16 09:07:36 +00001246 return 0;
1247 }
1248
Bill Wendlingdc817b62009-05-14 18:26:15 +00001249 Value *findDbgGlobalDeclare(GlobalVariable *V) {
Torok Edwinff7d0e92009-03-10 13:41:26 +00001250 const Module *M = V->getParent();
Devang Patele4b27562009-08-28 23:24:31 +00001251 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1252 if (!NMD)
1253 return 0;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001254
Devang Patele4b27562009-08-28 23:24:31 +00001255 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
1256 DIGlobalVariable DIG(cast_or_null<MDNode>(NMD->getElement(i)));
1257 if (DIG.isNull())
1258 continue;
1259 if (DIG.getGlobal() == V)
1260 return DIG.getNode();
Torok Edwinff7d0e92009-03-10 13:41:26 +00001261 }
Torok Edwinff7d0e92009-03-10 13:41:26 +00001262 return 0;
1263 }
1264
Bill Wendlingdc817b62009-05-14 18:26:15 +00001265 /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
Torok Edwin620f2802008-12-16 09:07:36 +00001266 /// It looks through pointer casts too.
Bill Wendlingdc817b62009-05-14 18:26:15 +00001267 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts) {
Torok Edwin620f2802008-12-16 09:07:36 +00001268 if (stripCasts) {
1269 V = V->stripPointerCasts();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001270
Torok Edwin620f2802008-12-16 09:07:36 +00001271 // Look for the bitcast.
1272 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001273 I != E; ++I)
Devang Patel94dfaec2009-10-29 18:20:34 +00001274 if (isa<BitCastInst>(I)) {
1275 const DbgDeclareInst *DDI = findDbgDeclare(*I, false);
1276 if (DDI) return DDI;
1277 }
Torok Edwin620f2802008-12-16 09:07:36 +00001278 return 0;
1279 }
1280
Bill Wendlingdc817b62009-05-14 18:26:15 +00001281 // Find llvm.dbg.declare among uses of the instruction.
Torok Edwin620f2802008-12-16 09:07:36 +00001282 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001283 I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001284 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I))
1285 return DDI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001286
Torok Edwin620f2802008-12-16 09:07:36 +00001287 return 0;
1288 }
Torok Edwinff7d0e92009-03-10 13:41:26 +00001289
Devang Patel5ccdd102009-09-29 18:40:58 +00001290bool getLocationInfo(const Value *V, std::string &DisplayName,
1291 std::string &Type, unsigned &LineNo, std::string &File,
Bill Wendlingdc817b62009-05-14 18:26:15 +00001292 std::string &Dir) {
Torok Edwinff7d0e92009-03-10 13:41:26 +00001293 DICompileUnit Unit;
1294 DIType TypeD;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001295
Torok Edwinff7d0e92009-03-10 13:41:26 +00001296 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1297 Value *DIGV = findDbgGlobalDeclare(GV);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001298 if (!DIGV) return false;
Devang Patele4b27562009-08-28 23:24:31 +00001299 DIGlobalVariable Var(cast<MDNode>(DIGV));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001300
Devang Patel65dbc902009-11-25 17:36:49 +00001301 StringRef D = Var.getDisplayName();
1302 if (!D.empty())
Devang Patel5ccdd102009-09-29 18:40:58 +00001303 DisplayName = D;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001304 LineNo = Var.getLineNumber();
1305 Unit = Var.getCompileUnit();
1306 TypeD = Var.getType();
1307 } else {
1308 const DbgDeclareInst *DDI = findDbgDeclare(V);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001309 if (!DDI) return false;
Devang Patele4b27562009-08-28 23:24:31 +00001310 DIVariable Var(cast<MDNode>(DDI->getVariable()));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001311
Devang Patel65dbc902009-11-25 17:36:49 +00001312 StringRef D = Var.getName();
1313 if (!D.empty())
Devang Patel5ccdd102009-09-29 18:40:58 +00001314 DisplayName = D;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001315 LineNo = Var.getLineNumber();
1316 Unit = Var.getCompileUnit();
1317 TypeD = Var.getType();
1318 }
Bill Wendlingdc817b62009-05-14 18:26:15 +00001319
Devang Patel65dbc902009-11-25 17:36:49 +00001320 StringRef T = TypeD.getName();
1321 if (!T.empty())
Devang Patel5ccdd102009-09-29 18:40:58 +00001322 Type = T;
Devang Patel65dbc902009-11-25 17:36:49 +00001323 StringRef F = Unit.getFilename();
1324 if (!F.empty())
Devang Patel5ccdd102009-09-29 18:40:58 +00001325 File = F;
Devang Patel65dbc902009-11-25 17:36:49 +00001326 StringRef D = Unit.getDirectory();
1327 if (!D.empty())
Devang Patel5ccdd102009-09-29 18:40:58 +00001328 Dir = D;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001329 return true;
1330 }
Devang Patel13e16b62009-06-26 01:49:18 +00001331
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001332 /// isValidDebugInfoIntrinsic - Return true if SPI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001333 /// info intrinsic.
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001334 bool isValidDebugInfoIntrinsic(DbgStopPointInst &SPI,
Devang Patel9e529c32009-07-02 01:15:24 +00001335 CodeGenOpt::Level OptLev) {
1336 return DIDescriptor::ValidDebugInfo(SPI.getContext(), OptLev);
1337 }
1338
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001339 /// isValidDebugInfoIntrinsic - Return true if FSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001340 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001341 bool isValidDebugInfoIntrinsic(DbgFuncStartInst &FSI,
1342 CodeGenOpt::Level OptLev) {
1343 return DIDescriptor::ValidDebugInfo(FSI.getSubprogram(), OptLev);
1344 }
1345
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001346 /// isValidDebugInfoIntrinsic - Return true if RSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001347 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001348 bool isValidDebugInfoIntrinsic(DbgRegionStartInst &RSI,
1349 CodeGenOpt::Level OptLev) {
1350 return DIDescriptor::ValidDebugInfo(RSI.getContext(), OptLev);
1351 }
1352
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001353 /// isValidDebugInfoIntrinsic - Return true if REI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001354 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001355 bool isValidDebugInfoIntrinsic(DbgRegionEndInst &REI,
1356 CodeGenOpt::Level OptLev) {
1357 return DIDescriptor::ValidDebugInfo(REI.getContext(), OptLev);
1358 }
1359
1360
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001361 /// isValidDebugInfoIntrinsic - Return true if DI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001362 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001363 bool isValidDebugInfoIntrinsic(DbgDeclareInst &DI,
1364 CodeGenOpt::Level OptLev) {
1365 return DIDescriptor::ValidDebugInfo(DI.getVariable(), OptLev);
1366 }
1367
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001368 /// ExtractDebugLocation - Extract debug location information
Devang Patel9e529c32009-07-02 01:15:24 +00001369 /// from llvm.dbg.stoppoint intrinsic.
1370 DebugLoc ExtractDebugLocation(DbgStopPointInst &SPI,
Devang Patel9e529c32009-07-02 01:15:24 +00001371 DebugLocTracker &DebugLocInfo) {
1372 DebugLoc DL;
1373 Value *Context = SPI.getContext();
Devang Patel9e529c32009-07-02 01:15:24 +00001374
1375 // If this location is already tracked then use it.
Devang Patela1434042009-10-01 01:15:28 +00001376 DebugLocTuple Tuple(cast<MDNode>(Context), NULL, SPI.getLine(),
Devang Patel9e529c32009-07-02 01:15:24 +00001377 SPI.getColumn());
1378 DenseMap<DebugLocTuple, unsigned>::iterator II
1379 = DebugLocInfo.DebugIdMap.find(Tuple);
1380 if (II != DebugLocInfo.DebugIdMap.end())
1381 return DebugLoc::get(II->second);
1382
1383 // Add a new location entry.
1384 unsigned Id = DebugLocInfo.DebugLocations.size();
1385 DebugLocInfo.DebugLocations.push_back(Tuple);
1386 DebugLocInfo.DebugIdMap[Tuple] = Id;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001387
Devang Patel9e529c32009-07-02 01:15:24 +00001388 return DebugLoc::get(Id);
1389 }
1390
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001391 /// ExtractDebugLocation - Extract debug location information
Devang Patel1b75f442009-09-16 18:20:05 +00001392 /// from DILocation.
1393 DebugLoc ExtractDebugLocation(DILocation &Loc,
1394 DebugLocTracker &DebugLocInfo) {
1395 DebugLoc DL;
1396 MDNode *Context = Loc.getScope().getNode();
Devang Patela1434042009-10-01 01:15:28 +00001397 MDNode *InlinedLoc = NULL;
1398 if (!Loc.getOrigLocation().isNull())
1399 InlinedLoc = Loc.getOrigLocation().getNode();
Devang Patel1b75f442009-09-16 18:20:05 +00001400 // If this location is already tracked then use it.
Devang Patela1434042009-10-01 01:15:28 +00001401 DebugLocTuple Tuple(Context, InlinedLoc, Loc.getLineNumber(),
Daniel Dunbara279bc32009-09-20 02:20:51 +00001402 Loc.getColumnNumber());
Devang Patel1b75f442009-09-16 18:20:05 +00001403 DenseMap<DebugLocTuple, unsigned>::iterator II
1404 = DebugLocInfo.DebugIdMap.find(Tuple);
1405 if (II != DebugLocInfo.DebugIdMap.end())
1406 return DebugLoc::get(II->second);
1407
1408 // Add a new location entry.
1409 unsigned Id = DebugLocInfo.DebugLocations.size();
1410 DebugLocInfo.DebugLocations.push_back(Tuple);
1411 DebugLocInfo.DebugIdMap[Tuple] = Id;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001412
Devang Patel1b75f442009-09-16 18:20:05 +00001413 return DebugLoc::get(Id);
1414 }
1415
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001416 /// ExtractDebugLocation - Extract debug location information
Devang Patel9e529c32009-07-02 01:15:24 +00001417 /// from llvm.dbg.func_start intrinsic.
1418 DebugLoc ExtractDebugLocation(DbgFuncStartInst &FSI,
Devang Patel9e529c32009-07-02 01:15:24 +00001419 DebugLocTracker &DebugLocInfo) {
1420 DebugLoc DL;
1421 Value *SP = FSI.getSubprogram();
Devang Patel9e529c32009-07-02 01:15:24 +00001422
Devang Patele4b27562009-08-28 23:24:31 +00001423 DISubprogram Subprogram(cast<MDNode>(SP));
Devang Patel9e529c32009-07-02 01:15:24 +00001424 unsigned Line = Subprogram.getLineNumber();
1425 DICompileUnit CU(Subprogram.getCompileUnit());
1426
1427 // If this location is already tracked then use it.
Devang Patela1434042009-10-01 01:15:28 +00001428 DebugLocTuple Tuple(CU.getNode(), NULL, Line, /* Column */ 0);
Devang Patel9e529c32009-07-02 01:15:24 +00001429 DenseMap<DebugLocTuple, unsigned>::iterator II
1430 = DebugLocInfo.DebugIdMap.find(Tuple);
1431 if (II != DebugLocInfo.DebugIdMap.end())
1432 return DebugLoc::get(II->second);
1433
1434 // Add a new location entry.
1435 unsigned Id = DebugLocInfo.DebugLocations.size();
1436 DebugLocInfo.DebugLocations.push_back(Tuple);
1437 DebugLocInfo.DebugIdMap[Tuple] = Id;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001438
Devang Patel9e529c32009-07-02 01:15:24 +00001439 return DebugLoc::get(Id);
1440 }
Devang Patel193f7202009-11-24 01:14:22 +00001441
1442 /// getDISubprogram - Find subprogram that is enclosing this scope.
1443 DISubprogram getDISubprogram(MDNode *Scope) {
1444 DIDescriptor D(Scope);
1445 if (D.isNull())
1446 return DISubprogram();
1447
1448 if (D.isCompileUnit())
1449 return DISubprogram();
1450
1451 if (D.isSubprogram())
1452 return DISubprogram(Scope);
1453
1454 if (D.isLexicalBlock())
1455 return getDISubprogram(DILexicalBlock(Scope).getContext().getNode());
1456
1457 return DISubprogram();
1458 }
1459
1460 /// getDICompositeType - Find underlying composite type.
1461 DICompositeType getDICompositeType(DIType T) {
1462 if (T.isNull())
1463 return DICompositeType();
1464
1465 if (T.isCompositeType())
1466 return DICompositeType(T.getNode());
1467
1468 if (T.isDerivedType())
1469 return getDICompositeType(DIDerivedType(T.getNode()).getTypeDerivedFrom());
1470
1471 return DICompositeType();
1472 }
Torok Edwin620f2802008-12-16 09:07:36 +00001473}