blob: 0dfffc898ca23e3cb326d07e6f360e838432262c [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"
Chris Lattner784b8502009-12-29 09:15:46 +000016#include "llvm/Target/TargetMachine.h" // FIXME: LAYERING VIOLATION!
Chris Lattnera45664f2008-11-10 02:56:27 +000017#include "llvm/Constants.h"
18#include "llvm/DerivedTypes.h"
19#include "llvm/Intrinsics.h"
Torok Edwin620f2802008-12-16 09:07:36 +000020#include "llvm/IntrinsicInst.h"
Chris Lattnera45664f2008-11-10 02:56:27 +000021#include "llvm/Instructions.h"
22#include "llvm/Module.h"
23#include "llvm/Analysis/ValueTracking.h"
Devang Patele4b27562009-08-28 23:24:31 +000024#include "llvm/ADT/SmallPtrSet.h"
David Greene0eb5b662009-12-23 19:45:49 +000025#include "llvm/Support/Debug.h"
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000026#include "llvm/Support/Dwarf.h"
Devang Patel9e529c32009-07-02 01:15:24 +000027#include "llvm/Support/DebugLoc.h"
Chris Lattnera81d29b2009-08-23 07:33:14 +000028#include "llvm/Support/raw_ostream.h"
Chris Lattnera45664f2008-11-10 02:56:27 +000029using namespace llvm;
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000030using namespace llvm::dwarf;
Chris Lattnera45664f2008-11-10 02:56:27 +000031
32//===----------------------------------------------------------------------===//
33// DIDescriptor
34//===----------------------------------------------------------------------===//
35
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000036/// ValidDebugInfo - Return true if V represents valid debug info value.
Devang Patele4b27562009-08-28 23:24:31 +000037/// FIXME : Add DIDescriptor.isValid()
Chris Lattner784b8502009-12-29 09:15:46 +000038bool DIDescriptor::ValidDebugInfo(MDNode *N, unsigned OptLevel) {
Devang Patele4b27562009-08-28 23:24:31 +000039 if (!N)
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000040 return false;
41
Devang Patele4b27562009-08-28 23:24:31 +000042 DIDescriptor DI(N);
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000043
44 // Check current version. Allow Version6 for now.
45 unsigned Version = DI.getVersion();
46 if (Version != LLVMDebugVersion && Version != LLVMDebugVersion6)
47 return false;
48
Chris Lattner099b7792009-12-29 09:22:47 +000049 switch (DI.getTag()) {
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000050 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 {
Chris Lattner099b7792009-12-29 09:22:47 +0000130 assert(!isNull() && "Invalid descriptor!");
131 return getTag() == dwarf::DW_TAG_base_type;
Torok Edwinb07fbd92008-12-13 08:25:29 +0000132}
Chris Lattnera45664f2008-11-10 02:56:27 +0000133
Devang Patel6ceea332009-08-31 18:49:10 +0000134/// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
135bool DIDescriptor::isDerivedType() const {
Chris Lattner099b7792009-12-29 09:22:47 +0000136 assert(!isNull() && "Invalid descriptor!");
137 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000138 case dwarf::DW_TAG_typedef:
139 case dwarf::DW_TAG_pointer_type:
140 case dwarf::DW_TAG_reference_type:
141 case dwarf::DW_TAG_const_type:
142 case dwarf::DW_TAG_volatile_type:
143 case dwarf::DW_TAG_restrict_type:
144 case dwarf::DW_TAG_member:
145 case dwarf::DW_TAG_inheritance:
146 return true;
147 default:
Devang Patele4b27562009-08-28 23:24:31 +0000148 // CompositeTypes are currently modelled as DerivedTypes.
Devang Patel6ceea332009-08-31 18:49:10 +0000149 return isCompositeType();
Chris Lattnera45664f2008-11-10 02:56:27 +0000150 }
151}
152
Chris Lattnera45664f2008-11-10 02:56:27 +0000153/// isCompositeType - Return true if the specified tag is legal for
154/// DICompositeType.
Devang Patel6ceea332009-08-31 18:49:10 +0000155bool DIDescriptor::isCompositeType() const {
Chris Lattner099b7792009-12-29 09:22:47 +0000156 assert(!isNull() && "Invalid descriptor!");
157 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000158 case dwarf::DW_TAG_array_type:
159 case dwarf::DW_TAG_structure_type:
160 case dwarf::DW_TAG_union_type:
161 case dwarf::DW_TAG_enumeration_type:
162 case dwarf::DW_TAG_vector_type:
163 case dwarf::DW_TAG_subroutine_type:
Devang Patel25cb0d72009-03-25 03:52:06 +0000164 case dwarf::DW_TAG_class_type:
Chris Lattnera45664f2008-11-10 02:56:27 +0000165 return true;
166 default:
167 return false;
168 }
169}
170
Chris Lattnera45664f2008-11-10 02:56:27 +0000171/// isVariable - Return true if the specified tag is legal for DIVariable.
Devang Patel6ceea332009-08-31 18:49:10 +0000172bool DIDescriptor::isVariable() const {
Chris Lattner099b7792009-12-29 09:22:47 +0000173 assert(!isNull() && "Invalid descriptor!");
174 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000175 case dwarf::DW_TAG_auto_variable:
176 case dwarf::DW_TAG_arg_variable:
177 case dwarf::DW_TAG_return_variable:
178 return true;
179 default:
180 return false;
181 }
182}
183
Devang Patelecbeb1a2009-09-30 22:34:41 +0000184/// isType - Return true if the specified tag is legal for DIType.
185bool DIDescriptor::isType() const {
186 return isBasicType() || isCompositeType() || isDerivedType();
187}
188
Devang Patel6ceea332009-08-31 18:49:10 +0000189/// isSubprogram - Return true if the specified tag is legal for
190/// DISubprogram.
191bool DIDescriptor::isSubprogram() const {
Chris Lattner099b7792009-12-29 09:22:47 +0000192 assert(!isNull() && "Invalid descriptor!");
193 return getTag() == dwarf::DW_TAG_subprogram;
Devang Patel6ceea332009-08-31 18:49:10 +0000194}
195
196/// isGlobalVariable - Return true if the specified tag is legal for
197/// DIGlobalVariable.
198bool DIDescriptor::isGlobalVariable() const {
Chris Lattner099b7792009-12-29 09:22:47 +0000199 assert(!isNull() && "Invalid descriptor!");
200 return getTag() == dwarf::DW_TAG_variable;
Devang Patel6ceea332009-08-31 18:49:10 +0000201}
202
Devang Patelecbeb1a2009-09-30 22:34:41 +0000203/// isGlobal - Return true if the specified tag is legal for DIGlobal.
204bool DIDescriptor::isGlobal() const {
205 return isGlobalVariable();
206}
207
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000208/// isScope - Return true if the specified tag is one of the scope
Devang Patel43d98b32009-08-31 20:44:45 +0000209/// related tag.
210bool DIDescriptor::isScope() const {
Chris Lattner099b7792009-12-29 09:22:47 +0000211 assert(!isNull() && "Invalid descriptor!");
212 switch (getTag()) {
213 case dwarf::DW_TAG_compile_unit:
214 case dwarf::DW_TAG_lexical_block:
215 case dwarf::DW_TAG_subprogram:
216 case dwarf::DW_TAG_namespace:
217 return true;
218 default:
219 break;
Devang Patel43d98b32009-08-31 20:44:45 +0000220 }
221 return false;
222}
Devang Patel6ceea332009-08-31 18:49:10 +0000223
Devang Patelc9f322d2009-08-31 21:34:44 +0000224/// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
225bool DIDescriptor::isCompileUnit() const {
Chris Lattner099b7792009-12-29 09:22:47 +0000226 assert(!isNull() && "Invalid descriptor!");
227 return getTag() == dwarf::DW_TAG_compile_unit;
Devang Patelc9f322d2009-08-31 21:34:44 +0000228}
229
Devang Patel6404e4e2009-12-15 19:16:48 +0000230/// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
231bool DIDescriptor::isNameSpace() const {
Chris Lattner099b7792009-12-29 09:22:47 +0000232 assert(!isNull() && "Invalid descriptor!");
233 return getTag() == dwarf::DW_TAG_namespace;
Devang Patel6404e4e2009-12-15 19:16:48 +0000234}
235
Devang Patel5e005d82009-08-31 22:00:15 +0000236/// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
237bool DIDescriptor::isLexicalBlock() const {
Chris Lattner099b7792009-12-29 09:22:47 +0000238 assert(!isNull() && "Invalid descriptor!");
239 return getTag() == dwarf::DW_TAG_lexical_block;
Devang Patel5e005d82009-08-31 22:00:15 +0000240}
241
Devang Patelecbeb1a2009-09-30 22:34:41 +0000242/// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
243bool DIDescriptor::isSubrange() const {
Chris Lattner099b7792009-12-29 09:22:47 +0000244 assert(!isNull() && "Invalid descriptor!");
245 return getTag() == dwarf::DW_TAG_subrange_type;
Devang Patelecbeb1a2009-09-30 22:34:41 +0000246}
247
248/// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
249bool DIDescriptor::isEnumerator() const {
Chris Lattner099b7792009-12-29 09:22:47 +0000250 assert(!isNull() && "Invalid descriptor!");
251 return getTag() == dwarf::DW_TAG_enumerator;
Devang Patelecbeb1a2009-09-30 22:34:41 +0000252}
253
Devang Patel6ceea332009-08-31 18:49:10 +0000254//===----------------------------------------------------------------------===//
255// Simple Descriptor Constructors and other Methods
256//===----------------------------------------------------------------------===//
257
258DIType::DIType(MDNode *N) : DIDescriptor(N) {
259 if (!N) return;
260 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
261 DbgNode = 0;
262 }
263}
264
Devang Patel68afdc32009-01-05 18:33:01 +0000265unsigned DIArray::getNumElements() const {
Chris Lattner099b7792009-12-29 09:22:47 +0000266 assert(DbgNode && "Invalid DIArray");
Devang Patele4b27562009-08-28 23:24:31 +0000267 return DbgNode->getNumElements();
Devang Patel68afdc32009-01-05 18:33:01 +0000268}
Chris Lattnera45664f2008-11-10 02:56:27 +0000269
Devang Patelc4999d72009-07-22 18:23:44 +0000270/// replaceAllUsesWith - Replace all uses of debug info referenced by
271/// this descriptor. After this completes, the current debug info value
272/// is erased.
273void DIDerivedType::replaceAllUsesWith(DIDescriptor &D) {
274 if (isNull())
275 return;
276
Chris Lattner099b7792009-12-29 09:22:47 +0000277 assert(!D.isNull() && "Can not replace with null");
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000278
279 // Since we use a TrackingVH for the node, its easy for clients to manufacture
280 // legitimate situations where they want to replaceAllUsesWith() on something
281 // which, due to uniquing, has merged with the source. We shield clients from
282 // this detail by allowing a value to be replaced with replaceAllUsesWith()
283 // itself.
284 if (getNode() != D.getNode()) {
285 MDNode *Node = DbgNode;
286 Node->replaceAllUsesWith(D.getNode());
Chris Lattnerb76359e2009-12-31 01:05:46 +0000287 Node->destroy();
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000288 }
Devang Patelc4999d72009-07-22 18:23:44 +0000289}
290
Devang Patelb79b5352009-01-19 23:21:49 +0000291/// Verify - Verify that a compile unit is well formed.
292bool DICompileUnit::Verify() const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000293 if (isNull())
Devang Patelb79b5352009-01-19 23:21:49 +0000294 return false;
Devang Patel65dbc902009-11-25 17:36:49 +0000295 StringRef N = getFilename();
296 if (N.empty())
Bill Wendling0582ae92009-03-13 04:39:26 +0000297 return false;
Devang Patelb79b5352009-01-19 23:21:49 +0000298 // It is possible that directory and produce string is empty.
Bill Wendling0582ae92009-03-13 04:39:26 +0000299 return true;
Devang Patelb79b5352009-01-19 23:21:49 +0000300}
301
302/// Verify - Verify that a type descriptor is well formed.
303bool DIType::Verify() const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000304 if (isNull())
Devang Patelb79b5352009-01-19 23:21:49 +0000305 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000306 if (getContext().isNull())
Devang Patelb79b5352009-01-19 23:21:49 +0000307 return false;
308
309 DICompileUnit CU = getCompileUnit();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000310 if (!CU.isNull() && !CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000311 return false;
312 return true;
313}
314
315/// Verify - Verify that a composite type descriptor is well formed.
316bool DICompositeType::Verify() const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000317 if (isNull())
Devang Patelb79b5352009-01-19 23:21:49 +0000318 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000319 if (getContext().isNull())
Devang Patelb79b5352009-01-19 23:21:49 +0000320 return false;
321
322 DICompileUnit CU = getCompileUnit();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000323 if (!CU.isNull() && !CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000324 return false;
325 return true;
326}
327
328/// Verify - Verify that a subprogram descriptor is well formed.
329bool DISubprogram::Verify() const {
330 if (isNull())
331 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000332
Devang Patelb79b5352009-01-19 23:21:49 +0000333 if (getContext().isNull())
334 return false;
335
336 DICompileUnit CU = getCompileUnit();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000337 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000338 return false;
339
340 DICompositeType Ty = getType();
341 if (!Ty.isNull() && !Ty.Verify())
342 return false;
343 return true;
344}
345
346/// Verify - Verify that a global variable descriptor is well formed.
347bool DIGlobalVariable::Verify() const {
348 if (isNull())
349 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000350
Devang Patel65dbc902009-11-25 17:36:49 +0000351 if (getDisplayName().empty())
Devang Patel84c73e92009-11-06 17:58:12 +0000352 return false;
353
Devang Patelb79b5352009-01-19 23:21:49 +0000354 if (getContext().isNull())
355 return false;
356
357 DICompileUnit CU = getCompileUnit();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000358 if (!CU.isNull() && !CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000359 return false;
360
361 DIType Ty = getType();
362 if (!Ty.Verify())
363 return false;
364
365 if (!getGlobal())
366 return false;
367
368 return true;
369}
370
371/// Verify - Verify that a variable descriptor is well formed.
372bool DIVariable::Verify() const {
373 if (isNull())
374 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000375
Devang Patelb79b5352009-01-19 23:21:49 +0000376 if (getContext().isNull())
377 return false;
378
379 DIType Ty = getType();
380 if (!Ty.Verify())
381 return false;
382
Devang Patelb79b5352009-01-19 23:21:49 +0000383 return true;
384}
385
Devang Patel36375ee2009-02-17 21:23:59 +0000386/// getOriginalTypeSize - If this type is derived from a base type then
387/// return base type size.
388uint64_t DIDerivedType::getOriginalTypeSize() const {
Devang Patel61ecbd12009-11-04 23:48:00 +0000389 unsigned Tag = getTag();
390 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
391 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
392 Tag == dwarf::DW_TAG_restrict_type) {
393 DIType BaseType = getTypeDerivedFrom();
Devang Patel5ebfa2d2009-11-06 18:24:05 +0000394 // If this type is not derived from any type then take conservative
395 // approach.
396 if (BaseType.isNull())
397 return getSizeInBits();
Devang Patel61ecbd12009-11-04 23:48:00 +0000398 if (BaseType.isDerivedType())
399 return DIDerivedType(BaseType.getNode()).getOriginalTypeSize();
400 else
401 return BaseType.getSizeInBits();
402 }
403
404 return getSizeInBits();
Devang Patel36375ee2009-02-17 21:23:59 +0000405}
Devang Patelb79b5352009-01-19 23:21:49 +0000406
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000407/// describes - Return true if this subprogram provides debugging
408/// information for the function F.
409bool DISubprogram::describes(const Function *F) {
Chris Lattner099b7792009-12-29 09:22:47 +0000410 assert(F && "Invalid function");
Devang Patel65dbc902009-11-25 17:36:49 +0000411 StringRef Name = getLinkageName();
412 if (Name.empty())
Devang Patel5ccdd102009-09-29 18:40:58 +0000413 Name = getName();
Devang Patel65dbc902009-11-25 17:36:49 +0000414 if (F->getName() == Name)
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000415 return true;
416 return false;
417}
418
Devang Patel65dbc902009-11-25 17:36:49 +0000419StringRef DIScope::getFilename() const {
Devang Patelecbeb1a2009-09-30 22:34:41 +0000420 if (isLexicalBlock())
421 return DILexicalBlock(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000422 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000423 return DISubprogram(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000424 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000425 return DICompileUnit(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000426 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000427 return DINameSpace(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000428 assert(0 && "Invalid DIScope!");
Devang Patel65dbc902009-11-25 17:36:49 +0000429 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000430}
431
Devang Patel65dbc902009-11-25 17:36:49 +0000432StringRef DIScope::getDirectory() const {
Devang Patelecbeb1a2009-09-30 22:34:41 +0000433 if (isLexicalBlock())
434 return DILexicalBlock(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000435 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000436 return DISubprogram(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000437 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000438 return DICompileUnit(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000439 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000440 return DINameSpace(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000441 assert(0 && "Invalid DIScope!");
Devang Patel65dbc902009-11-25 17:36:49 +0000442 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000443}
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 {
David Greene0eb5b662009-12-23 19:45:49 +0000452 dbgs() << "[" << dwarf::TagString(getTag()) << "] ";
453 dbgs().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())
David Greene0eb5b662009-12-23 19:45:49 +0000459 dbgs() << " [" << dwarf::LanguageString(getLanguage()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000460
David Greene0eb5b662009-12-23 19:45:49 +0000461 dbgs() << " [" << 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 Patel65dbc902009-11-25 17:36:49 +0000468 StringRef Res = getName();
469 if (!Res.empty())
David Greene0eb5b662009-12-23 19:45:49 +0000470 dbgs() << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000471
472 unsigned Tag = getTag();
David Greene0eb5b662009-12-23 19:45:49 +0000473 dbgs() << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000474
475 // TODO : Print context
476 getCompileUnit().dump();
David Greene0eb5b662009-12-23 19:45:49 +0000477 dbgs() << " ["
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000478 << getLineNumber() << ", "
Chris Lattnera81d29b2009-08-23 07:33:14 +0000479 << getSizeInBits() << ", "
480 << getAlignInBits() << ", "
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000481 << getOffsetInBits()
Chris Lattnera81d29b2009-08-23 07:33:14 +0000482 << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000483
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000484 if (isPrivate())
David Greene0eb5b662009-12-23 19:45:49 +0000485 dbgs() << " [private] ";
Devang Patel7136a652009-07-01 22:10:23 +0000486 else if (isProtected())
David Greene0eb5b662009-12-23 19:45:49 +0000487 dbgs() << " [protected] ";
Devang Patel7136a652009-07-01 22:10:23 +0000488
489 if (isForwardDecl())
David Greene0eb5b662009-12-23 19:45:49 +0000490 dbgs() << " [fwd] ";
Devang Patel7136a652009-07-01 22:10:23 +0000491
Devang Patel6ceea332009-08-31 18:49:10 +0000492 if (isBasicType())
Devang Patele4b27562009-08-28 23:24:31 +0000493 DIBasicType(DbgNode).dump();
Devang Patel6ceea332009-08-31 18:49:10 +0000494 else if (isDerivedType())
Devang Patele4b27562009-08-28 23:24:31 +0000495 DIDerivedType(DbgNode).dump();
Devang Patel6ceea332009-08-31 18:49:10 +0000496 else if (isCompositeType())
Devang Patele4b27562009-08-28 23:24:31 +0000497 DICompositeType(DbgNode).dump();
Devang Patel7136a652009-07-01 22:10:23 +0000498 else {
David Greene0eb5b662009-12-23 19:45:49 +0000499 dbgs() << "Invalid DIType\n";
Devang Patel7136a652009-07-01 22:10:23 +0000500 return;
501 }
502
David Greene0eb5b662009-12-23 19:45:49 +0000503 dbgs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000504}
505
506/// dump - Print basic type.
507void DIBasicType::dump() const {
David Greene0eb5b662009-12-23 19:45:49 +0000508 dbgs() << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000509}
510
511/// dump - Print derived type.
512void DIDerivedType::dump() const {
David Greene0eb5b662009-12-23 19:45:49 +0000513 dbgs() << "\n\t Derived From: "; getTypeDerivedFrom().dump();
Devang Patel7136a652009-07-01 22:10:23 +0000514}
515
516/// dump - Print composite type.
517void DICompositeType::dump() const {
518 DIArray A = getTypeArray();
519 if (A.isNull())
520 return;
David Greene0eb5b662009-12-23 19:45:49 +0000521 dbgs() << " [" << A.getNumElements() << " elements]";
Devang Patel7136a652009-07-01 22:10:23 +0000522}
523
524/// dump - Print global.
525void DIGlobal::dump() const {
Devang Patel65dbc902009-11-25 17:36:49 +0000526 StringRef Res = getName();
527 if (!Res.empty())
David Greene0eb5b662009-12-23 19:45:49 +0000528 dbgs() << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000529
530 unsigned Tag = getTag();
David Greene0eb5b662009-12-23 19:45:49 +0000531 dbgs() << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000532
533 // TODO : Print context
534 getCompileUnit().dump();
David Greene0eb5b662009-12-23 19:45:49 +0000535 dbgs() << " [" << getLineNumber() << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000536
537 if (isLocalToUnit())
David Greene0eb5b662009-12-23 19:45:49 +0000538 dbgs() << " [local] ";
Devang Patel7136a652009-07-01 22:10:23 +0000539
540 if (isDefinition())
David Greene0eb5b662009-12-23 19:45:49 +0000541 dbgs() << " [def] ";
Devang Patel7136a652009-07-01 22:10:23 +0000542
Devang Patel6ceea332009-08-31 18:49:10 +0000543 if (isGlobalVariable())
Devang Patele4b27562009-08-28 23:24:31 +0000544 DIGlobalVariable(DbgNode).dump();
Devang Patel7136a652009-07-01 22:10:23 +0000545
David Greene0eb5b662009-12-23 19:45:49 +0000546 dbgs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000547}
548
549/// dump - Print subprogram.
550void DISubprogram::dump() const {
Devang Patel65dbc902009-11-25 17:36:49 +0000551 StringRef Res = getName();
552 if (!Res.empty())
David Greene0eb5b662009-12-23 19:45:49 +0000553 dbgs() << " [" << Res << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000554
555 unsigned Tag = getTag();
David Greene0eb5b662009-12-23 19:45:49 +0000556 dbgs() << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000557
558 // TODO : Print context
559 getCompileUnit().dump();
David Greene0eb5b662009-12-23 19:45:49 +0000560 dbgs() << " [" << getLineNumber() << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000561
562 if (isLocalToUnit())
David Greene0eb5b662009-12-23 19:45:49 +0000563 dbgs() << " [local] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000564
565 if (isDefinition())
David Greene0eb5b662009-12-23 19:45:49 +0000566 dbgs() << " [def] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000567
David Greene0eb5b662009-12-23 19:45:49 +0000568 dbgs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000569}
570
571/// dump - Print global variable.
572void DIGlobalVariable::dump() const {
David Greene0eb5b662009-12-23 19:45:49 +0000573 dbgs() << " [";
Chris Lattnera81d29b2009-08-23 07:33:14 +0000574 getGlobal()->dump();
David Greene0eb5b662009-12-23 19:45:49 +0000575 dbgs() << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000576}
577
578/// dump - Print variable.
579void DIVariable::dump() const {
Devang Patel65dbc902009-11-25 17:36:49 +0000580 StringRef Res = getName();
581 if (!Res.empty())
David Greene0eb5b662009-12-23 19:45:49 +0000582 dbgs() << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000583
584 getCompileUnit().dump();
David Greene0eb5b662009-12-23 19:45:49 +0000585 dbgs() << " [" << getLineNumber() << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000586 getType().dump();
David Greene0eb5b662009-12-23 19:45:49 +0000587 dbgs() << "\n";
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000588
589 // FIXME: Dump complex addresses
Devang Patel7136a652009-07-01 22:10:23 +0000590}
591
592//===----------------------------------------------------------------------===//
Chris Lattnera45664f2008-11-10 02:56:27 +0000593// DIFactory: Basic Helpers
594//===----------------------------------------------------------------------===//
595
Bill Wendlingdc817b62009-05-14 18:26:15 +0000596DIFactory::DIFactory(Module &m)
Devang Patel427ef4e2009-11-17 22:39:08 +0000597 : M(m), VMContext(M.getContext()), DeclareFn(0) {
Devang Patel3ddf7042009-11-13 02:27:33 +0000598 EmptyStructPtr = PointerType::getUnqual(StructType::get(VMContext));
599}
Chris Lattner497a7a82008-11-10 04:10:34 +0000600
Chris Lattnera45664f2008-11-10 02:56:27 +0000601Constant *DIFactory::GetTagConstant(unsigned TAG) {
Devang Patel6906ba52009-01-20 19:22:03 +0000602 assert((TAG & LLVMDebugVersionMask) == 0 &&
Chris Lattnera45664f2008-11-10 02:56:27 +0000603 "Tag too large for debug encoding!");
Owen Anderson1d0be152009-08-13 21:58:54 +0000604 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
Chris Lattnera45664f2008-11-10 02:56:27 +0000605}
606
Chris Lattnera45664f2008-11-10 02:56:27 +0000607//===----------------------------------------------------------------------===//
608// DIFactory: Primary Constructors
609//===----------------------------------------------------------------------===//
610
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000611/// GetOrCreateArray - Create an descriptor for an array of descriptors.
Chris Lattnera45664f2008-11-10 02:56:27 +0000612/// This implicitly uniques the arrays created.
613DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
Devang Patele4b27562009-08-28 23:24:31 +0000614 SmallVector<Value*, 16> Elts;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000615
Devang Patele4b27562009-08-28 23:24:31 +0000616 if (NumTys == 0)
617 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)));
618 else
619 for (unsigned i = 0; i != NumTys; ++i)
620 Elts.push_back(Tys[i].getNode());
Devang Patel82459882009-08-26 05:01:18 +0000621
Devang Patele4b27562009-08-28 23:24:31 +0000622 return DIArray(MDNode::get(VMContext,Elts.data(), Elts.size()));
Chris Lattnera45664f2008-11-10 02:56:27 +0000623}
624
625/// GetOrCreateSubrange - Create a descriptor for a value range. This
626/// implicitly uniques the values returned.
627DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
Devang Patele4b27562009-08-28 23:24:31 +0000628 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000629 GetTagConstant(dwarf::DW_TAG_subrange_type),
Owen Anderson1d0be152009-08-13 21:58:54 +0000630 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
631 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
Chris Lattnera45664f2008-11-10 02:56:27 +0000632 };
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000633
Devang Patele4b27562009-08-28 23:24:31 +0000634 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000635}
636
637
638
639/// CreateCompileUnit - Create a new descriptor for the specified compile
640/// unit. Note that this does not unique compile units within the module.
641DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
Devang Patel65dbc902009-11-25 17:36:49 +0000642 StringRef Filename,
643 StringRef Directory,
644 StringRef Producer,
Devang Pateldd9db662009-01-30 18:20:31 +0000645 bool isMain,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000646 bool isOptimized,
Devang Patel65dbc902009-11-25 17:36:49 +0000647 StringRef Flags,
Devang Patel13319ce2009-02-17 22:43:44 +0000648 unsigned RunTimeVer) {
Devang Patele4b27562009-08-28 23:24:31 +0000649 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000650 GetTagConstant(dwarf::DW_TAG_compile_unit),
Devang Patele4b27562009-08-28 23:24:31 +0000651 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Owen Anderson1d0be152009-08-13 21:58:54 +0000652 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
Devang Patele4b27562009-08-28 23:24:31 +0000653 MDString::get(VMContext, Filename),
654 MDString::get(VMContext, Directory),
655 MDString::get(VMContext, Producer),
Owen Anderson1d0be152009-08-13 21:58:54 +0000656 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
657 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
Devang Patele4b27562009-08-28 23:24:31 +0000658 MDString::get(VMContext, Flags),
Owen Anderson1d0be152009-08-13 21:58:54 +0000659 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
Chris Lattnera45664f2008-11-10 02:56:27 +0000660 };
Devang Patele4b27562009-08-28 23:24:31 +0000661
662 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000663}
664
665/// CreateEnumerator - Create a single enumerator value.
Devang Patel65dbc902009-11-25 17:36:49 +0000666DIEnumerator DIFactory::CreateEnumerator(StringRef Name, uint64_t Val){
Devang Patele4b27562009-08-28 23:24:31 +0000667 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000668 GetTagConstant(dwarf::DW_TAG_enumerator),
Devang Patele4b27562009-08-28 23:24:31 +0000669 MDString::get(VMContext, Name),
Owen Anderson1d0be152009-08-13 21:58:54 +0000670 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
Chris Lattnera45664f2008-11-10 02:56:27 +0000671 };
Devang Patele4b27562009-08-28 23:24:31 +0000672 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000673}
674
675
676/// CreateBasicType - Create a basic type like int, float, etc.
677DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000678 StringRef Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000679 DICompileUnit CompileUnit,
680 unsigned LineNumber,
681 uint64_t SizeInBits,
682 uint64_t AlignInBits,
683 uint64_t OffsetInBits, unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000684 unsigned Encoding) {
Devang Patele4b27562009-08-28 23:24:31 +0000685 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000686 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patele4b27562009-08-28 23:24:31 +0000687 Context.getNode(),
688 MDString::get(VMContext, Name),
689 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000690 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
691 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
692 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
693 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
694 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
695 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
Chris Lattnera45664f2008-11-10 02:56:27 +0000696 };
Devang Patele4b27562009-08-28 23:24:31 +0000697 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000698}
699
Devang Patelac16d442009-10-26 16:54:35 +0000700
701/// CreateBasicType - Create a basic type like int, float, etc.
702DIBasicType DIFactory::CreateBasicTypeEx(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000703 StringRef Name,
Devang Patelac16d442009-10-26 16:54:35 +0000704 DICompileUnit CompileUnit,
705 unsigned LineNumber,
706 Constant *SizeInBits,
707 Constant *AlignInBits,
708 Constant *OffsetInBits, unsigned Flags,
709 unsigned Encoding) {
710 Value *Elts[] = {
711 GetTagConstant(dwarf::DW_TAG_base_type),
712 Context.getNode(),
713 MDString::get(VMContext, Name),
714 CompileUnit.getNode(),
715 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
716 SizeInBits,
717 AlignInBits,
718 OffsetInBits,
719 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
720 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
721 };
722 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
723}
724
725
Chris Lattnera45664f2008-11-10 02:56:27 +0000726/// CreateDerivedType - Create a derived type like const qualified type,
727/// pointer, typedef, etc.
728DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
729 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000730 StringRef Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000731 DICompileUnit CompileUnit,
732 unsigned LineNumber,
733 uint64_t SizeInBits,
734 uint64_t AlignInBits,
735 uint64_t OffsetInBits,
736 unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000737 DIType DerivedFrom) {
Devang Patele4b27562009-08-28 23:24:31 +0000738 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000739 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000740 Context.getNode(),
741 MDString::get(VMContext, Name),
742 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000743 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
744 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
745 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
746 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
747 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patele4b27562009-08-28 23:24:31 +0000748 DerivedFrom.getNode(),
Chris Lattnera45664f2008-11-10 02:56:27 +0000749 };
Devang Patele4b27562009-08-28 23:24:31 +0000750 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000751}
752
Devang Patelac16d442009-10-26 16:54:35 +0000753
754/// CreateDerivedType - Create a derived type like const qualified type,
755/// pointer, typedef, etc.
756DIDerivedType DIFactory::CreateDerivedTypeEx(unsigned Tag,
757 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000758 StringRef Name,
Devang Patelac16d442009-10-26 16:54:35 +0000759 DICompileUnit CompileUnit,
760 unsigned LineNumber,
761 Constant *SizeInBits,
762 Constant *AlignInBits,
763 Constant *OffsetInBits,
764 unsigned Flags,
765 DIType DerivedFrom) {
766 Value *Elts[] = {
767 GetTagConstant(Tag),
768 Context.getNode(),
769 MDString::get(VMContext, Name),
770 CompileUnit.getNode(),
771 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
772 SizeInBits,
773 AlignInBits,
774 OffsetInBits,
775 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
776 DerivedFrom.getNode(),
777 };
778 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
779}
780
781
Chris Lattnera45664f2008-11-10 02:56:27 +0000782/// CreateCompositeType - Create a composite type like array, struct, etc.
783DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
784 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000785 StringRef Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000786 DICompileUnit CompileUnit,
787 unsigned LineNumber,
788 uint64_t SizeInBits,
789 uint64_t AlignInBits,
790 uint64_t OffsetInBits,
791 unsigned Flags,
792 DIType DerivedFrom,
Devang Patel13319ce2009-02-17 22:43:44 +0000793 DIArray Elements,
794 unsigned RuntimeLang) {
Owen Andersone277fed2009-07-07 16:31:25 +0000795
Devang Patele4b27562009-08-28 23:24:31 +0000796 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000797 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000798 Context.getNode(),
799 MDString::get(VMContext, Name),
800 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000801 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
802 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
803 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
804 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
805 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patele4b27562009-08-28 23:24:31 +0000806 DerivedFrom.getNode(),
807 Elements.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000808 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
Chris Lattnera45664f2008-11-10 02:56:27 +0000809 };
Devang Patele4b27562009-08-28 23:24:31 +0000810 return DICompositeType(MDNode::get(VMContext, &Elts[0], 12));
Chris Lattnera45664f2008-11-10 02:56:27 +0000811}
812
813
Devang Patelac16d442009-10-26 16:54:35 +0000814/// CreateCompositeType - Create a composite type like array, struct, etc.
815DICompositeType DIFactory::CreateCompositeTypeEx(unsigned Tag,
816 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000817 StringRef Name,
Devang Patelac16d442009-10-26 16:54:35 +0000818 DICompileUnit CompileUnit,
819 unsigned LineNumber,
820 Constant *SizeInBits,
821 Constant *AlignInBits,
822 Constant *OffsetInBits,
823 unsigned Flags,
824 DIType DerivedFrom,
825 DIArray Elements,
826 unsigned RuntimeLang) {
827
828 Value *Elts[] = {
829 GetTagConstant(Tag),
830 Context.getNode(),
831 MDString::get(VMContext, Name),
832 CompileUnit.getNode(),
833 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
834 SizeInBits,
835 AlignInBits,
836 OffsetInBits,
837 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
838 DerivedFrom.getNode(),
839 Elements.getNode(),
840 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
841 };
842 return DICompositeType(MDNode::get(VMContext, &Elts[0], 12));
843}
844
845
Chris Lattnera45664f2008-11-10 02:56:27 +0000846/// CreateSubprogram - Create a new descriptor for the specified subprogram.
847/// See comments in DISubprogram for descriptions of these fields. This
848/// method does not unique the generated descriptors.
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000849DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000850 StringRef Name,
851 StringRef DisplayName,
852 StringRef LinkageName,
Chris Lattnera45664f2008-11-10 02:56:27 +0000853 DICompileUnit CompileUnit,
854 unsigned LineNo, DIType Type,
855 bool isLocalToUnit,
Devang Patel5d11eb02009-12-03 19:11:07 +0000856 bool isDefinition,
857 unsigned VK, unsigned VIndex,
858 DIType ContainingType) {
Devang Patel854967e2008-12-17 22:39:29 +0000859
Devang Patele4b27562009-08-28 23:24:31 +0000860 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000861 GetTagConstant(dwarf::DW_TAG_subprogram),
Devang Patele4b27562009-08-28 23:24:31 +0000862 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
863 Context.getNode(),
864 MDString::get(VMContext, Name),
865 MDString::get(VMContext, DisplayName),
866 MDString::get(VMContext, LinkageName),
867 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000868 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000869 Type.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000870 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
Devang Patel5d11eb02009-12-03 19:11:07 +0000871 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
872 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
873 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
874 ContainingType.getNode()
Chris Lattnera45664f2008-11-10 02:56:27 +0000875 };
Devang Patel5d11eb02009-12-03 19:11:07 +0000876 return DISubprogram(MDNode::get(VMContext, &Elts[0], 14));
Chris Lattnera45664f2008-11-10 02:56:27 +0000877}
878
Devang Patele3a18de2009-12-01 23:09:02 +0000879/// CreateSubprogramDefinition - Create new subprogram descriptor for the
880/// given declaration.
881DISubprogram DIFactory::CreateSubprogramDefinition(DISubprogram &SPDeclaration) {
882 if (SPDeclaration.isDefinition())
883 return DISubprogram(SPDeclaration.getNode());
884
885 MDNode *DeclNode = SPDeclaration.getNode();
886 Value *Elts[] = {
887 GetTagConstant(dwarf::DW_TAG_subprogram),
888 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
889 DeclNode->getElement(2), // Context
890 DeclNode->getElement(3), // Name
891 DeclNode->getElement(4), // DisplayName
892 DeclNode->getElement(5), // LinkageName
893 DeclNode->getElement(6), // CompileUnit
894 DeclNode->getElement(7), // LineNo
895 DeclNode->getElement(8), // Type
896 DeclNode->getElement(9), // isLocalToUnit
Devang Patel5d11eb02009-12-03 19:11:07 +0000897 ConstantInt::get(Type::getInt1Ty(VMContext), true),
898 DeclNode->getElement(11), // Virtuality
899 DeclNode->getElement(12), // VIndex
900 DeclNode->getElement(13) // Containting Type
Devang Patele3a18de2009-12-01 23:09:02 +0000901 };
Devang Patel5d11eb02009-12-03 19:11:07 +0000902 return DISubprogram(MDNode::get(VMContext, &Elts[0], 14));
Devang Patele3a18de2009-12-01 23:09:02 +0000903}
904
Chris Lattnera45664f2008-11-10 02:56:27 +0000905/// CreateGlobalVariable - Create a new descriptor for the specified global.
906DIGlobalVariable
Devang Patel65dbc902009-11-25 17:36:49 +0000907DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
908 StringRef DisplayName,
909 StringRef LinkageName,
Chris Lattnera45664f2008-11-10 02:56:27 +0000910 DICompileUnit CompileUnit,
911 unsigned LineNo, DIType Type,bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000912 bool isDefinition, llvm::GlobalVariable *Val) {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000913 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000914 GetTagConstant(dwarf::DW_TAG_variable),
Devang Patele4b27562009-08-28 23:24:31 +0000915 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
916 Context.getNode(),
917 MDString::get(VMContext, Name),
918 MDString::get(VMContext, DisplayName),
919 MDString::get(VMContext, LinkageName),
920 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000921 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000922 Type.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000923 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
924 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
Devang Patele4b27562009-08-28 23:24:31 +0000925 Val
Chris Lattnera45664f2008-11-10 02:56:27 +0000926 };
Devang Patele4b27562009-08-28 23:24:31 +0000927
928 Value *const *Vs = &Elts[0];
929 MDNode *Node = MDNode::get(VMContext,Vs, 12);
930
931 // Create a named metadata so that we do not lose this mdnode.
932 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
933 NMD->addElement(Node);
934
935 return DIGlobalVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +0000936}
937
938
939/// CreateVariable - Create a new descriptor for the specified variable.
940DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000941 StringRef Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000942 DICompileUnit CompileUnit, unsigned LineNo,
Devang Pateldd9db662009-01-30 18:20:31 +0000943 DIType Type) {
Devang Patele4b27562009-08-28 23:24:31 +0000944 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000945 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000946 Context.getNode(),
947 MDString::get(VMContext, Name),
948 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000949 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000950 Type.getNode(),
Chris Lattnera45664f2008-11-10 02:56:27 +0000951 };
Devang Patele4b27562009-08-28 23:24:31 +0000952 return DIVariable(MDNode::get(VMContext, &Elts[0], 6));
Chris Lattnera45664f2008-11-10 02:56:27 +0000953}
954
955
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000956/// CreateComplexVariable - Create a new descriptor for the specified variable
957/// which has a complex address expression for its address.
958DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
959 const std::string &Name,
960 DICompileUnit CompileUnit,
961 unsigned LineNo,
962 DIType Type, SmallVector<Value *, 9> &addr) {
963 SmallVector<Value *, 9> Elts;
964 Elts.push_back(GetTagConstant(Tag));
965 Elts.push_back(Context.getNode());
966 Elts.push_back(MDString::get(VMContext, Name));
967 Elts.push_back(CompileUnit.getNode());
968 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
969 Elts.push_back(Type.getNode());
970 Elts.insert(Elts.end(), addr.begin(), addr.end());
971
972 return DIVariable(MDNode::get(VMContext, &Elts[0], 6+addr.size()));
973}
974
975
Chris Lattnera45664f2008-11-10 02:56:27 +0000976/// CreateBlock - This creates a descriptor for a lexical block with the
Owen Anderson99035272009-07-07 17:12:53 +0000977/// specified parent VMContext.
Devang Patel5e005d82009-08-31 22:00:15 +0000978DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context) {
Devang Patele4b27562009-08-28 23:24:31 +0000979 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000980 GetTagConstant(dwarf::DW_TAG_lexical_block),
Devang Patele4b27562009-08-28 23:24:31 +0000981 Context.getNode()
Chris Lattnera45664f2008-11-10 02:56:27 +0000982 };
Devang Patel5e005d82009-08-31 22:00:15 +0000983 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 2));
Chris Lattnera45664f2008-11-10 02:56:27 +0000984}
985
Devang Patel6404e4e2009-12-15 19:16:48 +0000986/// CreateNameSpace - This creates new descriptor for a namespace
987/// with the specified parent context.
988DINameSpace DIFactory::CreateNameSpace(DIDescriptor Context, StringRef Name,
989 DICompileUnit CompileUnit,
990 unsigned LineNo) {
991 Value *Elts[] = {
992 GetTagConstant(dwarf::DW_TAG_namespace),
993 Context.getNode(),
994 MDString::get(VMContext, Name),
995 CompileUnit.getNode(),
996 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
997 };
998 return DINameSpace(MDNode::get(VMContext, &Elts[0], 5));
999}
1000
Devang Patelf98d8fe2009-09-01 01:14:15 +00001001/// CreateLocation - Creates a debug info location.
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001002DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
Daniel Dunbara279bc32009-09-20 02:20:51 +00001003 DIScope S, DILocation OrigLoc) {
Devang Patelf98d8fe2009-09-01 01:14:15 +00001004 Value *Elts[] = {
1005 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1006 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
1007 S.getNode(),
1008 OrigLoc.getNode(),
1009 };
1010 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1011}
1012
Devang Patele54a5e82009-11-23 19:11:20 +00001013/// CreateLocation - Creates a debug info location.
1014DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
1015 DIScope S, MDNode *OrigLoc) {
1016 Value *Elts[] = {
1017 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1018 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
1019 S.getNode(),
1020 OrigLoc
1021 };
1022 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1023}
Chris Lattnera45664f2008-11-10 02:56:27 +00001024
1025//===----------------------------------------------------------------------===//
1026// DIFactory: Routines for inserting code into a function
1027//===----------------------------------------------------------------------===//
1028
Chris Lattnera45664f2008-11-10 02:56:27 +00001029/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Devang Patel6daf99b2009-11-10 22:05:35 +00001030Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
Devang Patel3ddf7042009-11-13 02:27:33 +00001031 Instruction *InsertBefore) {
1032 // Cast the storage to a {}* for the call to llvm.dbg.declare.
1033 Storage = new BitCastInst(Storage, EmptyStructPtr, "", InsertBefore);
1034
Chris Lattnera45664f2008-11-10 02:56:27 +00001035 if (!DeclareFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +00001036 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1037
Devang Patele4b27562009-08-28 23:24:31 +00001038 Value *Args[] = { Storage, D.getNode() };
Devang Patel6daf99b2009-11-10 22:05:35 +00001039 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
Mike Stumpe4250392009-10-01 22:08:58 +00001040}
1041
1042/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Devang Patel6daf99b2009-11-10 22:05:35 +00001043Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
Devang Patel3ddf7042009-11-13 02:27:33 +00001044 BasicBlock *InsertAtEnd) {
1045 // Cast the storage to a {}* for the call to llvm.dbg.declare.
1046 Storage = new BitCastInst(Storage, EmptyStructPtr, "", InsertAtEnd);
1047
Mike Stumpe4250392009-10-01 22:08:58 +00001048 if (!DeclareFn)
1049 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1050
1051 Value *Args[] = { Storage, D.getNode() };
Devang Patel6daf99b2009-11-10 22:05:35 +00001052 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);
Chris Lattnera45664f2008-11-10 02:56:27 +00001053}
Torok Edwin620f2802008-12-16 09:07:36 +00001054
Victor Hernandezc59b3352009-12-07 21:54:43 +00001055/// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1056Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, Value *Offset,
1057 DIVariable D,
1058 Instruction *InsertBefore) {
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001059 assert(V && "no value passed to dbg.value");
1060 assert(Offset->getType() == Type::getInt64Ty(V->getContext()) &&
1061 "offset must be i64");
1062 if (!ValueFn)
1063 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1064
1065 Value *Elts[] = { V };
1066 Value *Args[] = { MDNode::get(V->getContext(), Elts, 1), Offset,
1067 D.getNode() };
1068 return CallInst::Create(ValueFn, Args, Args+3, "", InsertBefore);
1069}
1070
Victor Hernandezc59b3352009-12-07 21:54:43 +00001071/// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1072Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, Value *Offset,
1073 DIVariable D,
1074 BasicBlock *InsertAtEnd) {
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001075 assert(V && "no value passed to dbg.value");
1076 assert(Offset->getType() == Type::getInt64Ty(V->getContext()) &&
1077 "offset must be i64");
1078 if (!ValueFn)
1079 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1080
1081 Value *Elts[] = { V };
1082 Value *Args[] = { MDNode::get(V->getContext(), Elts, 1), Offset,
1083 D.getNode() };
1084 return CallInst::Create(ValueFn, Args, Args+3, "", InsertAtEnd);
1085}
Devang Patele4b27562009-08-28 23:24:31 +00001086
Devang Pateld2f79a12009-07-28 19:55:13 +00001087//===----------------------------------------------------------------------===//
Devang Patel98c65172009-07-30 18:25:15 +00001088// DebugInfoFinder implementations.
Devang Pateld2f79a12009-07-28 19:55:13 +00001089//===----------------------------------------------------------------------===//
1090
Devang Patel98c65172009-07-30 18:25:15 +00001091/// processModule - Process entire module and collect debug info.
1092void DebugInfoFinder::processModule(Module &M) {
Chris Lattner08113472009-12-29 09:01:33 +00001093 unsigned MDDbgKind = M.getMDKindID("dbg");
Devang Patel70d75ca2009-11-12 19:02:56 +00001094
Devang Pateld2f79a12009-07-28 19:55:13 +00001095 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1096 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
1097 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
1098 ++BI) {
Devang Patel70d75ca2009-11-12 19:02:56 +00001099 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
Devang Patelb4d31302009-07-31 18:18:52 +00001100 processDeclare(DDI);
Chris Lattner3990b122009-12-28 23:41:32 +00001101 else if (MDNode *L = BI->getMetadata(MDDbgKind))
Chris Lattner0eb41982009-12-28 20:45:51 +00001102 processLocation(DILocation(L));
Devang Pateld2f79a12009-07-28 19:55:13 +00001103 }
Devang Patele4b27562009-08-28 23:24:31 +00001104
1105 NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv");
1106 if (!NMD)
1107 return;
1108
1109 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
1110 DIGlobalVariable DIG(cast<MDNode>(NMD->getElement(i)));
Devang Pateld2f79a12009-07-28 19:55:13 +00001111 if (addGlobalVariable(DIG)) {
1112 addCompileUnit(DIG.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +00001113 processType(DIG.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +00001114 }
1115 }
1116}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001117
Devang Patel6daf99b2009-11-10 22:05:35 +00001118/// processLocation - Process DILocation.
1119void DebugInfoFinder::processLocation(DILocation Loc) {
1120 if (Loc.isNull()) return;
1121 DIScope S(Loc.getScope().getNode());
1122 if (S.isNull()) return;
1123 if (S.isCompileUnit())
1124 addCompileUnit(DICompileUnit(S.getNode()));
1125 else if (S.isSubprogram())
1126 processSubprogram(DISubprogram(S.getNode()));
1127 else if (S.isLexicalBlock())
1128 processLexicalBlock(DILexicalBlock(S.getNode()));
1129 processLocation(Loc.getOrigLocation());
1130}
1131
Devang Patel98c65172009-07-30 18:25:15 +00001132/// processType - Process DIType.
1133void DebugInfoFinder::processType(DIType DT) {
Devang Patel72bcdb62009-08-10 22:09:58 +00001134 if (!addType(DT))
Devang Pateld2f79a12009-07-28 19:55:13 +00001135 return;
1136
1137 addCompileUnit(DT.getCompileUnit());
Devang Patel6ceea332009-08-31 18:49:10 +00001138 if (DT.isCompositeType()) {
Devang Patele4b27562009-08-28 23:24:31 +00001139 DICompositeType DCT(DT.getNode());
Devang Patel98c65172009-07-30 18:25:15 +00001140 processType(DCT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001141 DIArray DA = DCT.getTypeArray();
1142 if (!DA.isNull())
1143 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1144 DIDescriptor D = DA.getElement(i);
Devang Patele4b27562009-08-28 23:24:31 +00001145 DIType TypeE = DIType(D.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001146 if (!TypeE.isNull())
Devang Patel98c65172009-07-30 18:25:15 +00001147 processType(TypeE);
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001148 else
Devang Patele4b27562009-08-28 23:24:31 +00001149 processSubprogram(DISubprogram(D.getNode()));
Devang Pateld2f79a12009-07-28 19:55:13 +00001150 }
Devang Patel6ceea332009-08-31 18:49:10 +00001151 } else if (DT.isDerivedType()) {
Devang Patele4b27562009-08-28 23:24:31 +00001152 DIDerivedType DDT(DT.getNode());
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001153 if (!DDT.isNull())
Devang Patel98c65172009-07-30 18:25:15 +00001154 processType(DDT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001155 }
1156}
1157
Devang Patelbeab41b2009-10-07 22:04:08 +00001158/// processLexicalBlock
1159void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
1160 if (LB.isNull())
1161 return;
1162 DIScope Context = LB.getContext();
1163 if (Context.isLexicalBlock())
1164 return processLexicalBlock(DILexicalBlock(Context.getNode()));
1165 else
1166 return processSubprogram(DISubprogram(Context.getNode()));
1167}
1168
Devang Patel98c65172009-07-30 18:25:15 +00001169/// processSubprogram - Process DISubprogram.
1170void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Patele802f1c2009-07-30 17:30:23 +00001171 if (SP.isNull())
1172 return;
Devang Pateld2f79a12009-07-28 19:55:13 +00001173 if (!addSubprogram(SP))
1174 return;
1175 addCompileUnit(SP.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +00001176 processType(SP.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +00001177}
1178
Devang Patelb4d31302009-07-31 18:18:52 +00001179/// processDeclare - Process DbgDeclareInst.
1180void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
Devang Patele4b27562009-08-28 23:24:31 +00001181 DIVariable DV(cast<MDNode>(DDI->getVariable()));
Devang Patelb4d31302009-07-31 18:18:52 +00001182 if (DV.isNull())
1183 return;
1184
Devang Patele4b27562009-08-28 23:24:31 +00001185 if (!NodesSeen.insert(DV.getNode()))
Devang Patelb4d31302009-07-31 18:18:52 +00001186 return;
1187
1188 addCompileUnit(DV.getCompileUnit());
1189 processType(DV.getType());
1190}
1191
Devang Patel72bcdb62009-08-10 22:09:58 +00001192/// addType - Add type into Tys.
1193bool DebugInfoFinder::addType(DIType DT) {
1194 if (DT.isNull())
1195 return false;
1196
Devang Patele4b27562009-08-28 23:24:31 +00001197 if (!NodesSeen.insert(DT.getNode()))
Devang Patel72bcdb62009-08-10 22:09:58 +00001198 return false;
1199
Devang Patele4b27562009-08-28 23:24:31 +00001200 TYs.push_back(DT.getNode());
Devang Patel72bcdb62009-08-10 22:09:58 +00001201 return true;
1202}
1203
Devang Pateld2f79a12009-07-28 19:55:13 +00001204/// addCompileUnit - Add compile unit into CUs.
Devang Patel98c65172009-07-30 18:25:15 +00001205bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001206 if (CU.isNull())
1207 return false;
1208
Devang Patele4b27562009-08-28 23:24:31 +00001209 if (!NodesSeen.insert(CU.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001210 return false;
1211
Devang Patele4b27562009-08-28 23:24:31 +00001212 CUs.push_back(CU.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001213 return true;
1214}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001215
Devang Pateld2f79a12009-07-28 19:55:13 +00001216/// addGlobalVariable - Add global variable into GVs.
Devang Patel98c65172009-07-30 18:25:15 +00001217bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001218 if (DIG.isNull())
1219 return false;
1220
Devang Patele4b27562009-08-28 23:24:31 +00001221 if (!NodesSeen.insert(DIG.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001222 return false;
1223
Devang Patele4b27562009-08-28 23:24:31 +00001224 GVs.push_back(DIG.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001225 return true;
1226}
1227
1228// addSubprogram - Add subprgoram into SPs.
Devang Patel98c65172009-07-30 18:25:15 +00001229bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001230 if (SP.isNull())
1231 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001232
Devang Patele4b27562009-08-28 23:24:31 +00001233 if (!NodesSeen.insert(SP.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001234 return false;
1235
Devang Patele4b27562009-08-28 23:24:31 +00001236 SPs.push_back(SP.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001237 return true;
1238}
1239
Chris Lattner099b7792009-12-29 09:22:47 +00001240/// findStopPoint - Find the stoppoint coressponding to this instruction, that
1241/// is the stoppoint that dominates this instruction.
1242const DbgStopPointInst *llvm::findStopPoint(const Instruction *Inst) {
1243 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(Inst))
1244 return DSI;
Torok Edwin620f2802008-12-16 09:07:36 +00001245
Chris Lattner099b7792009-12-29 09:22:47 +00001246 const BasicBlock *BB = Inst->getParent();
1247 BasicBlock::const_iterator I = Inst, B;
1248 while (BB) {
1249 B = BB->begin();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001250
Chris Lattner099b7792009-12-29 09:22:47 +00001251 // A BB consisting only of a terminator can't have a stoppoint.
1252 while (I != B) {
1253 --I;
Torok Edwin620f2802008-12-16 09:07:36 +00001254 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
1255 return DSI;
Chris Lattner099b7792009-12-29 09:22:47 +00001256 }
Bill Wendlingdc817b62009-05-14 18:26:15 +00001257
Chris Lattner099b7792009-12-29 09:22:47 +00001258 // This BB didn't have a stoppoint: if there is only one predecessor, look
1259 // for a stoppoint there. We could use getIDom(), but that would require
1260 // dominator info.
1261 BB = I->getParent()->getUniquePredecessor();
Torok Edwin620f2802008-12-16 09:07:36 +00001262 if (BB)
Chris Lattner099b7792009-12-29 09:22:47 +00001263 I = BB->getTerminator();
Torok Edwin620f2802008-12-16 09:07:36 +00001264 }
1265
Chris Lattner099b7792009-12-29 09:22:47 +00001266 return 0;
1267}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001268
Chris Lattner099b7792009-12-29 09:22:47 +00001269/// findBBStopPoint - Find the stoppoint corresponding to first real
1270/// (non-debug intrinsic) instruction in this Basic Block, and return the
1271/// stoppoint for it.
1272const DbgStopPointInst *llvm::findBBStopPoint(const BasicBlock *BB) {
1273 for(BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
1274 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
1275 return DSI;
1276
1277 // Fallback to looking for stoppoint of unique predecessor. Useful if this
1278 // BB contains no stoppoints, but unique predecessor does.
1279 BB = BB->getUniquePredecessor();
1280 if (BB)
1281 return findStopPoint(BB->getTerminator());
1282
1283 return 0;
1284}
1285
1286Value *llvm::findDbgGlobalDeclare(GlobalVariable *V) {
1287 const Module *M = V->getParent();
1288 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1289 if (!NMD)
Torok Edwinff7d0e92009-03-10 13:41:26 +00001290 return 0;
Chris Lattner099b7792009-12-29 09:22:47 +00001291
1292 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
1293 DIGlobalVariable DIG(cast_or_null<MDNode>(NMD->getElement(i)));
1294 if (DIG.isNull())
1295 continue;
1296 if (DIG.getGlobal() == V)
1297 return DIG.getNode();
Torok Edwinff7d0e92009-03-10 13:41:26 +00001298 }
Chris Lattner099b7792009-12-29 09:22:47 +00001299 return 0;
1300}
Torok Edwinff7d0e92009-03-10 13:41:26 +00001301
Chris Lattner099b7792009-12-29 09:22:47 +00001302/// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
1303/// It looks through pointer casts too.
1304const DbgDeclareInst *llvm::findDbgDeclare(const Value *V, bool stripCasts) {
1305 if (stripCasts) {
1306 V = V->stripPointerCasts();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001307
Chris Lattner099b7792009-12-29 09:22:47 +00001308 // Look for the bitcast.
Torok Edwin620f2802008-12-16 09:07:36 +00001309 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001310 I != E; ++I)
Chris Lattner099b7792009-12-29 09:22:47 +00001311 if (isa<BitCastInst>(I)) {
1312 const DbgDeclareInst *DDI = findDbgDeclare(*I, false);
1313 if (DDI) return DDI;
1314 }
Torok Edwin620f2802008-12-16 09:07:36 +00001315 return 0;
1316 }
Torok Edwinff7d0e92009-03-10 13:41:26 +00001317
Chris Lattner099b7792009-12-29 09:22:47 +00001318 // Find llvm.dbg.declare among uses of the instruction.
1319 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
1320 I != E; ++I)
1321 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I))
1322 return DDI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001323
Chris Lattner099b7792009-12-29 09:22:47 +00001324 return 0;
1325}
Bill Wendlingdc817b62009-05-14 18:26:15 +00001326
Chris Lattner099b7792009-12-29 09:22:47 +00001327bool llvm::getLocationInfo(const Value *V, std::string &DisplayName,
1328 std::string &Type, unsigned &LineNo,
1329 std::string &File, std::string &Dir) {
1330 DICompileUnit Unit;
1331 DIType TypeD;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001332
Chris Lattner099b7792009-12-29 09:22:47 +00001333 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1334 Value *DIGV = findDbgGlobalDeclare(GV);
1335 if (!DIGV) return false;
1336 DIGlobalVariable Var(cast<MDNode>(DIGV));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001337
Chris Lattner099b7792009-12-29 09:22:47 +00001338 StringRef D = Var.getDisplayName();
Devang Patel65dbc902009-11-25 17:36:49 +00001339 if (!D.empty())
Chris Lattner099b7792009-12-29 09:22:47 +00001340 DisplayName = D;
1341 LineNo = Var.getLineNumber();
1342 Unit = Var.getCompileUnit();
1343 TypeD = Var.getType();
1344 } else {
1345 const DbgDeclareInst *DDI = findDbgDeclare(V);
1346 if (!DDI) return false;
1347 DIVariable Var(cast<MDNode>(DDI->getVariable()));
1348
1349 StringRef D = Var.getName();
1350 if (!D.empty())
1351 DisplayName = D;
1352 LineNo = Var.getLineNumber();
1353 Unit = Var.getCompileUnit();
1354 TypeD = Var.getType();
Torok Edwinff7d0e92009-03-10 13:41:26 +00001355 }
Devang Patel13e16b62009-06-26 01:49:18 +00001356
Chris Lattner099b7792009-12-29 09:22:47 +00001357 StringRef T = TypeD.getName();
1358 if (!T.empty())
1359 Type = T;
1360 StringRef F = Unit.getFilename();
1361 if (!F.empty())
1362 File = F;
1363 StringRef D = Unit.getDirectory();
1364 if (!D.empty())
1365 Dir = D;
1366 return true;
1367}
Devang Patel9e529c32009-07-02 01:15:24 +00001368
Chris Lattner099b7792009-12-29 09:22:47 +00001369/// ExtractDebugLocation - Extract debug location information
1370/// from llvm.dbg.stoppoint intrinsic.
1371DebugLoc llvm::ExtractDebugLocation(DbgStopPointInst &SPI,
1372 DebugLocTracker &DebugLocInfo) {
1373 DebugLoc DL;
1374 Value *Context = SPI.getContext();
Devang Patel9e529c32009-07-02 01:15:24 +00001375
Chris Lattner099b7792009-12-29 09:22:47 +00001376 // If this location is already tracked then use it.
1377 DebugLocTuple Tuple(cast<MDNode>(Context), NULL, SPI.getLine(),
1378 SPI.getColumn());
1379 DenseMap<DebugLocTuple, unsigned>::iterator II
1380 = DebugLocInfo.DebugIdMap.find(Tuple);
1381 if (II != DebugLocInfo.DebugIdMap.end())
1382 return DebugLoc::get(II->second);
Devang Patel9e529c32009-07-02 01:15:24 +00001383
Chris Lattner099b7792009-12-29 09:22:47 +00001384 // Add a new location entry.
1385 unsigned Id = DebugLocInfo.DebugLocations.size();
1386 DebugLocInfo.DebugLocations.push_back(Tuple);
1387 DebugLocInfo.DebugIdMap[Tuple] = Id;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001388
Chris Lattner099b7792009-12-29 09:22:47 +00001389 return DebugLoc::get(Id);
1390}
Devang Patel9e529c32009-07-02 01:15:24 +00001391
Chris Lattner099b7792009-12-29 09:22:47 +00001392/// ExtractDebugLocation - Extract debug location information
1393/// from DILocation.
1394DebugLoc llvm::ExtractDebugLocation(DILocation &Loc,
1395 DebugLocTracker &DebugLocInfo) {
1396 DebugLoc DL;
1397 MDNode *Context = Loc.getScope().getNode();
1398 MDNode *InlinedLoc = NULL;
1399 if (!Loc.getOrigLocation().isNull())
1400 InlinedLoc = Loc.getOrigLocation().getNode();
1401 // If this location is already tracked then use it.
1402 DebugLocTuple Tuple(Context, InlinedLoc, Loc.getLineNumber(),
1403 Loc.getColumnNumber());
1404 DenseMap<DebugLocTuple, unsigned>::iterator II
1405 = DebugLocInfo.DebugIdMap.find(Tuple);
1406 if (II != DebugLocInfo.DebugIdMap.end())
1407 return DebugLoc::get(II->second);
Devang Patel1b75f442009-09-16 18:20:05 +00001408
Chris Lattner099b7792009-12-29 09:22:47 +00001409 // Add a new location entry.
1410 unsigned Id = DebugLocInfo.DebugLocations.size();
1411 DebugLocInfo.DebugLocations.push_back(Tuple);
1412 DebugLocInfo.DebugIdMap[Tuple] = Id;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001413
Chris Lattner099b7792009-12-29 09:22:47 +00001414 return DebugLoc::get(Id);
1415}
Devang Patel1b75f442009-09-16 18:20:05 +00001416
Chris Lattner099b7792009-12-29 09:22:47 +00001417/// ExtractDebugLocation - Extract debug location information
1418/// from llvm.dbg.func_start intrinsic.
1419DebugLoc llvm::ExtractDebugLocation(DbgFuncStartInst &FSI,
1420 DebugLocTracker &DebugLocInfo) {
1421 DebugLoc DL;
1422 Value *SP = FSI.getSubprogram();
Devang Patel9e529c32009-07-02 01:15:24 +00001423
Chris Lattner099b7792009-12-29 09:22:47 +00001424 DISubprogram Subprogram(cast<MDNode>(SP));
1425 unsigned Line = Subprogram.getLineNumber();
1426 DICompileUnit CU(Subprogram.getCompileUnit());
Devang Patel9e529c32009-07-02 01:15:24 +00001427
Chris Lattner099b7792009-12-29 09:22:47 +00001428 // If this location is already tracked then use it.
1429 DebugLocTuple Tuple(CU.getNode(), NULL, Line, /* Column */ 0);
1430 DenseMap<DebugLocTuple, unsigned>::iterator II
1431 = DebugLocInfo.DebugIdMap.find(Tuple);
1432 if (II != DebugLocInfo.DebugIdMap.end())
1433 return DebugLoc::get(II->second);
Devang Patel9e529c32009-07-02 01:15:24 +00001434
Chris Lattner099b7792009-12-29 09:22:47 +00001435 // Add a new location entry.
1436 unsigned Id = DebugLocInfo.DebugLocations.size();
1437 DebugLocInfo.DebugLocations.push_back(Tuple);
1438 DebugLocInfo.DebugIdMap[Tuple] = Id;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001439
Chris Lattner099b7792009-12-29 09:22:47 +00001440 return DebugLoc::get(Id);
1441}
Devang Patel193f7202009-11-24 01:14:22 +00001442
Chris Lattner099b7792009-12-29 09:22:47 +00001443/// getDISubprogram - Find subprogram that is enclosing this scope.
1444DISubprogram llvm::getDISubprogram(MDNode *Scope) {
1445 DIDescriptor D(Scope);
1446 if (D.isNull())
Devang Patel193f7202009-11-24 01:14:22 +00001447 return DISubprogram();
Chris Lattner099b7792009-12-29 09:22:47 +00001448
1449 if (D.isCompileUnit())
1450 return DISubprogram();
1451
1452 if (D.isSubprogram())
1453 return DISubprogram(Scope);
1454
1455 if (D.isLexicalBlock())
1456 return getDISubprogram(DILexicalBlock(Scope).getContext().getNode());
1457
1458 return DISubprogram();
1459}
Devang Patel193f7202009-11-24 01:14:22 +00001460
Chris Lattner099b7792009-12-29 09:22:47 +00001461/// getDICompositeType - Find underlying composite type.
1462DICompositeType llvm::getDICompositeType(DIType T) {
1463 if (T.isNull())
Devang Patel193f7202009-11-24 01:14:22 +00001464 return DICompositeType();
Chris Lattner099b7792009-12-29 09:22:47 +00001465
1466 if (T.isCompositeType())
1467 return DICompositeType(T.getNode());
1468
1469 if (T.isDerivedType())
1470 return getDICompositeType(DIDerivedType(T.getNode()).getTypeDerivedFrom());
1471
1472 return DICompositeType();
Torok Edwin620f2802008-12-16 09:07:36 +00001473}