blob: 3732818de36c0f0c81485f71496ab4d2e00f5f8b [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"
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()
38bool DIDescriptor::ValidDebugInfo(MDNode *N, CodeGenOpt::Level OptLevel) {
39 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
49 unsigned Tag = DI.getTag();
50 switch (Tag) {
51 case DW_TAG_variable:
Devang Patele4b27562009-08-28 23:24:31 +000052 assert(DIVariable(N).Verify() && "Invalid DebugInfo value");
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000053 break;
54 case DW_TAG_compile_unit:
Devang Patele4b27562009-08-28 23:24:31 +000055 assert(DICompileUnit(N).Verify() && "Invalid DebugInfo value");
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000056 break;
57 case DW_TAG_subprogram:
Devang Patele4b27562009-08-28 23:24:31 +000058 assert(DISubprogram(N).Verify() && "Invalid DebugInfo value");
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000059 break;
60 case DW_TAG_lexical_block:
Bill Wendlingdc817b62009-05-14 18:26:15 +000061 // FIXME: This interfers with the quality of generated code during
62 // optimization.
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000063 if (OptLevel != CodeGenOpt::None)
64 return false;
Bill Wendlingdc817b62009-05-14 18:26:15 +000065 // FALLTHROUGH
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000066 default:
67 break;
68 }
69
70 return true;
71}
72
Devang Patele4b27562009-08-28 23:24:31 +000073DIDescriptor::DIDescriptor(MDNode *N, unsigned RequiredTag) {
74 DbgNode = N;
Daniel Dunbarf612ff62009-09-19 20:40:05 +000075
Bill Wendlingdc817b62009-05-14 18:26:15 +000076 // If this is non-null, check to see if the Tag matches. If not, set to null.
Devang Patele4b27562009-08-28 23:24:31 +000077 if (N && getTag() != RequiredTag) {
78 DbgNode = 0;
79 }
Chris Lattnera45664f2008-11-10 02:56:27 +000080}
81
Devang Patel65dbc902009-11-25 17:36:49 +000082StringRef
Devang Patel5ccdd102009-09-29 18:40:58 +000083DIDescriptor::getStringField(unsigned Elt) const {
Devang Patele4b27562009-08-28 23:24:31 +000084 if (DbgNode == 0)
Devang Patel65dbc902009-11-25 17:36:49 +000085 return StringRef();
Chris Lattnera45664f2008-11-10 02:56:27 +000086
Daniel Dunbarf612ff62009-09-19 20:40:05 +000087 if (Elt < DbgNode->getNumElements())
Devang Patel65dbc902009-11-25 17:36:49 +000088 if (MDString *MDS = dyn_cast_or_null<MDString>(DbgNode->getElement(Elt)))
89 return MDS->getString();
Daniel Dunbarf612ff62009-09-19 20:40:05 +000090
Devang Patel65dbc902009-11-25 17:36:49 +000091 return StringRef();
Chris Lattnera45664f2008-11-10 02:56:27 +000092}
93
94uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +000095 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +000096 return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +000097
Devang Patele4b27562009-08-28 23:24:31 +000098 if (Elt < DbgNode->getNumElements())
99 if (ConstantInt *CI = dyn_cast<ConstantInt>(DbgNode->getElement(Elt)))
100 return CI->getZExtValue();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000101
Chris Lattnera45664f2008-11-10 02:56:27 +0000102 return 0;
103}
104
Chris Lattnera45664f2008-11-10 02:56:27 +0000105DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000106 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +0000107 return DIDescriptor();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000108
Devang Patele4b27562009-08-28 23:24:31 +0000109 if (Elt < DbgNode->getNumElements() && DbgNode->getElement(Elt))
110 return DIDescriptor(dyn_cast<MDNode>(DbgNode->getElement(Elt)));
111
112 return DIDescriptor();
Chris Lattnera45664f2008-11-10 02:56:27 +0000113}
114
115GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000116 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +0000117 return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000118
Devang Patele4b27562009-08-28 23:24:31 +0000119 if (Elt < DbgNode->getNumElements())
Bill Wendling26c6cf42009-10-08 20:52:51 +0000120 return dyn_cast_or_null<GlobalVariable>(DbgNode->getElement(Elt));
Devang Patele4b27562009-08-28 23:24:31 +0000121 return 0;
Chris Lattnera45664f2008-11-10 02:56:27 +0000122}
123
Chris Lattnera45664f2008-11-10 02:56:27 +0000124//===----------------------------------------------------------------------===//
Devang Patel6ceea332009-08-31 18:49:10 +0000125// Predicates
Chris Lattnera45664f2008-11-10 02:56:27 +0000126//===----------------------------------------------------------------------===//
127
Devang Patel6ceea332009-08-31 18:49:10 +0000128/// isBasicType - Return true if the specified tag is legal for
129/// DIBasicType.
130bool DIDescriptor::isBasicType() const {
Devang Patel5a685092009-08-31 20:27:49 +0000131 assert (!isNull() && "Invalid descriptor!");
Devang Patel6ceea332009-08-31 18:49:10 +0000132 unsigned Tag = getTag();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000133
Devang Patel6ceea332009-08-31 18:49:10 +0000134 return Tag == dwarf::DW_TAG_base_type;
Torok Edwinb07fbd92008-12-13 08:25:29 +0000135}
Chris Lattnera45664f2008-11-10 02:56:27 +0000136
Devang Patel6ceea332009-08-31 18:49:10 +0000137/// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
138bool DIDescriptor::isDerivedType() const {
Devang Patel5a685092009-08-31 20:27:49 +0000139 assert (!isNull() && "Invalid descriptor!");
Devang Patel6ceea332009-08-31 18:49:10 +0000140 unsigned Tag = getTag();
141
Chris Lattnera45664f2008-11-10 02:56:27 +0000142 switch (Tag) {
143 case dwarf::DW_TAG_typedef:
144 case dwarf::DW_TAG_pointer_type:
145 case dwarf::DW_TAG_reference_type:
146 case dwarf::DW_TAG_const_type:
147 case dwarf::DW_TAG_volatile_type:
148 case dwarf::DW_TAG_restrict_type:
149 case dwarf::DW_TAG_member:
150 case dwarf::DW_TAG_inheritance:
151 return true;
152 default:
Devang Patele4b27562009-08-28 23:24:31 +0000153 // CompositeTypes are currently modelled as DerivedTypes.
Devang Patel6ceea332009-08-31 18:49:10 +0000154 return isCompositeType();
Chris Lattnera45664f2008-11-10 02:56:27 +0000155 }
156}
157
Chris Lattnera45664f2008-11-10 02:56:27 +0000158/// isCompositeType - Return true if the specified tag is legal for
159/// DICompositeType.
Devang Patel6ceea332009-08-31 18:49:10 +0000160bool DIDescriptor::isCompositeType() const {
Devang Patel5a685092009-08-31 20:27:49 +0000161 assert (!isNull() && "Invalid descriptor!");
Devang Patel6ceea332009-08-31 18:49:10 +0000162 unsigned Tag = getTag();
163
164 switch (Tag) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000165 case dwarf::DW_TAG_array_type:
166 case dwarf::DW_TAG_structure_type:
167 case dwarf::DW_TAG_union_type:
168 case dwarf::DW_TAG_enumeration_type:
169 case dwarf::DW_TAG_vector_type:
170 case dwarf::DW_TAG_subroutine_type:
Devang Patel25cb0d72009-03-25 03:52:06 +0000171 case dwarf::DW_TAG_class_type:
Chris Lattnera45664f2008-11-10 02:56:27 +0000172 return true;
173 default:
174 return false;
175 }
176}
177
Chris Lattnera45664f2008-11-10 02:56:27 +0000178/// isVariable - Return true if the specified tag is legal for DIVariable.
Devang Patel6ceea332009-08-31 18:49:10 +0000179bool DIDescriptor::isVariable() const {
Devang Patel5a685092009-08-31 20:27:49 +0000180 assert (!isNull() && "Invalid descriptor!");
Devang Patel6ceea332009-08-31 18:49:10 +0000181 unsigned Tag = getTag();
182
Chris Lattnera45664f2008-11-10 02:56:27 +0000183 switch (Tag) {
184 case dwarf::DW_TAG_auto_variable:
185 case dwarf::DW_TAG_arg_variable:
186 case dwarf::DW_TAG_return_variable:
187 return true;
188 default:
189 return false;
190 }
191}
192
Devang Patelecbeb1a2009-09-30 22:34:41 +0000193/// isType - Return true if the specified tag is legal for DIType.
194bool DIDescriptor::isType() const {
195 return isBasicType() || isCompositeType() || isDerivedType();
196}
197
Devang Patel6ceea332009-08-31 18:49:10 +0000198/// isSubprogram - Return true if the specified tag is legal for
199/// DISubprogram.
200bool DIDescriptor::isSubprogram() const {
Devang Patel5a685092009-08-31 20:27:49 +0000201 assert (!isNull() && "Invalid descriptor!");
Devang Patel6ceea332009-08-31 18:49:10 +0000202 unsigned Tag = getTag();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000203
Devang Patel6ceea332009-08-31 18:49:10 +0000204 return Tag == dwarf::DW_TAG_subprogram;
205}
206
207/// isGlobalVariable - Return true if the specified tag is legal for
208/// DIGlobalVariable.
209bool DIDescriptor::isGlobalVariable() const {
Devang Patel5a685092009-08-31 20:27:49 +0000210 assert (!isNull() && "Invalid descriptor!");
Devang Patel6ceea332009-08-31 18:49:10 +0000211 unsigned Tag = getTag();
212
213 return Tag == dwarf::DW_TAG_variable;
214}
215
Devang Patelecbeb1a2009-09-30 22:34:41 +0000216/// isGlobal - Return true if the specified tag is legal for DIGlobal.
217bool DIDescriptor::isGlobal() const {
218 return isGlobalVariable();
219}
220
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000221/// isScope - Return true if the specified tag is one of the scope
Devang Patel43d98b32009-08-31 20:44:45 +0000222/// related tag.
223bool DIDescriptor::isScope() const {
224 assert (!isNull() && "Invalid descriptor!");
225 unsigned Tag = getTag();
226
227 switch (Tag) {
228 case dwarf::DW_TAG_compile_unit:
229 case dwarf::DW_TAG_lexical_block:
230 case dwarf::DW_TAG_subprogram:
Devang Patel6404e4e2009-12-15 19:16:48 +0000231 case dwarf::DW_TAG_namespace:
Devang Patel43d98b32009-08-31 20:44:45 +0000232 return true;
233 default:
234 break;
235 }
236 return false;
237}
Devang Patel6ceea332009-08-31 18:49:10 +0000238
Devang Patelc9f322d2009-08-31 21:34:44 +0000239/// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
240bool DIDescriptor::isCompileUnit() const {
241 assert (!isNull() && "Invalid descriptor!");
242 unsigned Tag = getTag();
243
244 return Tag == dwarf::DW_TAG_compile_unit;
245}
246
Devang Patel6404e4e2009-12-15 19:16:48 +0000247/// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
248bool DIDescriptor::isNameSpace() const {
249 assert (!isNull() && "Invalid descriptor!");
250 unsigned Tag = getTag();
251
252 return Tag == dwarf::DW_TAG_namespace;
253}
254
Devang Patel5e005d82009-08-31 22:00:15 +0000255/// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
256bool DIDescriptor::isLexicalBlock() const {
257 assert (!isNull() && "Invalid descriptor!");
258 unsigned Tag = getTag();
259
260 return Tag == dwarf::DW_TAG_lexical_block;
261}
262
Devang Patelecbeb1a2009-09-30 22:34:41 +0000263/// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
264bool DIDescriptor::isSubrange() const {
265 assert (!isNull() && "Invalid descriptor!");
266 unsigned Tag = getTag();
267
268 return Tag == dwarf::DW_TAG_subrange_type;
269}
270
271/// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
272bool DIDescriptor::isEnumerator() const {
273 assert (!isNull() && "Invalid descriptor!");
274 unsigned Tag = getTag();
275
276 return Tag == dwarf::DW_TAG_enumerator;
277}
278
Devang Patel6ceea332009-08-31 18:49:10 +0000279//===----------------------------------------------------------------------===//
280// Simple Descriptor Constructors and other Methods
281//===----------------------------------------------------------------------===//
282
283DIType::DIType(MDNode *N) : DIDescriptor(N) {
284 if (!N) return;
285 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
286 DbgNode = 0;
287 }
288}
289
Devang Patel68afdc32009-01-05 18:33:01 +0000290unsigned DIArray::getNumElements() const {
Devang Patele4b27562009-08-28 23:24:31 +0000291 assert (DbgNode && "Invalid DIArray");
292 return DbgNode->getNumElements();
Devang Patel68afdc32009-01-05 18:33:01 +0000293}
Chris Lattnera45664f2008-11-10 02:56:27 +0000294
Devang Patelc4999d72009-07-22 18:23:44 +0000295/// replaceAllUsesWith - Replace all uses of debug info referenced by
296/// this descriptor. After this completes, the current debug info value
297/// is erased.
298void DIDerivedType::replaceAllUsesWith(DIDescriptor &D) {
299 if (isNull())
300 return;
301
Devang Patel6930f4f2009-07-22 18:56:16 +0000302 assert (!D.isNull() && "Can not replace with null");
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000303
304 // Since we use a TrackingVH for the node, its easy for clients to manufacture
305 // legitimate situations where they want to replaceAllUsesWith() on something
306 // which, due to uniquing, has merged with the source. We shield clients from
307 // this detail by allowing a value to be replaced with replaceAllUsesWith()
308 // itself.
309 if (getNode() != D.getNode()) {
310 MDNode *Node = DbgNode;
311 Node->replaceAllUsesWith(D.getNode());
312 delete Node;
313 }
Devang Patelc4999d72009-07-22 18:23:44 +0000314}
315
Devang Patelb79b5352009-01-19 23:21:49 +0000316/// Verify - Verify that a compile unit is well formed.
317bool DICompileUnit::Verify() const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000318 if (isNull())
Devang Patelb79b5352009-01-19 23:21:49 +0000319 return false;
Devang Patel65dbc902009-11-25 17:36:49 +0000320 StringRef N = getFilename();
321 if (N.empty())
Bill Wendling0582ae92009-03-13 04:39:26 +0000322 return false;
Devang Patelb79b5352009-01-19 23:21:49 +0000323 // It is possible that directory and produce string is empty.
Bill Wendling0582ae92009-03-13 04:39:26 +0000324 return true;
Devang Patelb79b5352009-01-19 23:21:49 +0000325}
326
327/// Verify - Verify that a type descriptor is well formed.
328bool DIType::Verify() const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000329 if (isNull())
Devang Patelb79b5352009-01-19 23:21:49 +0000330 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000331 if (getContext().isNull())
Devang Patelb79b5352009-01-19 23:21:49 +0000332 return false;
333
334 DICompileUnit CU = getCompileUnit();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000335 if (!CU.isNull() && !CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000336 return false;
337 return true;
338}
339
340/// Verify - Verify that a composite type descriptor is well formed.
341bool DICompositeType::Verify() const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000342 if (isNull())
Devang Patelb79b5352009-01-19 23:21:49 +0000343 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000344 if (getContext().isNull())
Devang Patelb79b5352009-01-19 23:21:49 +0000345 return false;
346
347 DICompileUnit CU = getCompileUnit();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000348 if (!CU.isNull() && !CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000349 return false;
350 return true;
351}
352
353/// Verify - Verify that a subprogram descriptor is well formed.
354bool DISubprogram::Verify() const {
355 if (isNull())
356 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000357
Devang Patelb79b5352009-01-19 23:21:49 +0000358 if (getContext().isNull())
359 return false;
360
361 DICompileUnit CU = getCompileUnit();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000362 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000363 return false;
364
365 DICompositeType Ty = getType();
366 if (!Ty.isNull() && !Ty.Verify())
367 return false;
368 return true;
369}
370
371/// Verify - Verify that a global variable descriptor is well formed.
372bool DIGlobalVariable::Verify() const {
373 if (isNull())
374 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000375
Devang Patel65dbc902009-11-25 17:36:49 +0000376 if (getDisplayName().empty())
Devang Patel84c73e92009-11-06 17:58:12 +0000377 return false;
378
Devang Patelb79b5352009-01-19 23:21:49 +0000379 if (getContext().isNull())
380 return false;
381
382 DICompileUnit CU = getCompileUnit();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000383 if (!CU.isNull() && !CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000384 return false;
385
386 DIType Ty = getType();
387 if (!Ty.Verify())
388 return false;
389
390 if (!getGlobal())
391 return false;
392
393 return true;
394}
395
396/// Verify - Verify that a variable descriptor is well formed.
397bool DIVariable::Verify() const {
398 if (isNull())
399 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000400
Devang Patelb79b5352009-01-19 23:21:49 +0000401 if (getContext().isNull())
402 return false;
403
404 DIType Ty = getType();
405 if (!Ty.Verify())
406 return false;
407
Devang Patelb79b5352009-01-19 23:21:49 +0000408 return true;
409}
410
Devang Patel36375ee2009-02-17 21:23:59 +0000411/// getOriginalTypeSize - If this type is derived from a base type then
412/// return base type size.
413uint64_t DIDerivedType::getOriginalTypeSize() const {
Devang Patel61ecbd12009-11-04 23:48:00 +0000414 unsigned Tag = getTag();
415 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
416 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
417 Tag == dwarf::DW_TAG_restrict_type) {
418 DIType BaseType = getTypeDerivedFrom();
Devang Patel5ebfa2d2009-11-06 18:24:05 +0000419 // If this type is not derived from any type then take conservative
420 // approach.
421 if (BaseType.isNull())
422 return getSizeInBits();
Devang Patel61ecbd12009-11-04 23:48:00 +0000423 if (BaseType.isDerivedType())
424 return DIDerivedType(BaseType.getNode()).getOriginalTypeSize();
425 else
426 return BaseType.getSizeInBits();
427 }
428
429 return getSizeInBits();
Devang Patel36375ee2009-02-17 21:23:59 +0000430}
Devang Patelb79b5352009-01-19 23:21:49 +0000431
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000432/// describes - Return true if this subprogram provides debugging
433/// information for the function F.
434bool DISubprogram::describes(const Function *F) {
435 assert (F && "Invalid function");
Devang Patel65dbc902009-11-25 17:36:49 +0000436 StringRef Name = getLinkageName();
437 if (Name.empty())
Devang Patel5ccdd102009-09-29 18:40:58 +0000438 Name = getName();
Devang Patel65dbc902009-11-25 17:36:49 +0000439 if (F->getName() == Name)
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000440 return true;
441 return false;
442}
443
Devang Patel65dbc902009-11-25 17:36:49 +0000444StringRef DIScope::getFilename() const {
Devang Patelecbeb1a2009-09-30 22:34:41 +0000445 if (isLexicalBlock())
446 return DILexicalBlock(DbgNode).getFilename();
447 else if (isSubprogram())
448 return DISubprogram(DbgNode).getFilename();
449 else if (isCompileUnit())
450 return DICompileUnit(DbgNode).getFilename();
Devang Patel6404e4e2009-12-15 19:16:48 +0000451 else if (isNameSpace())
452 return DINameSpace(DbgNode).getFilename();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000453 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
Devang Patel65dbc902009-11-25 17:36:49 +0000458StringRef DIScope::getDirectory() const {
Devang Patelecbeb1a2009-09-30 22:34:41 +0000459 if (isLexicalBlock())
460 return DILexicalBlock(DbgNode).getDirectory();
461 else if (isSubprogram())
462 return DISubprogram(DbgNode).getDirectory();
463 else if (isCompileUnit())
464 return DICompileUnit(DbgNode).getDirectory();
Devang Patel6404e4e2009-12-15 19:16:48 +0000465 else if (isNameSpace())
466 return DINameSpace(DbgNode).getDirectory();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000467 else
468 assert (0 && "Invalid DIScope!");
Devang Patel65dbc902009-11-25 17:36:49 +0000469 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000470}
471
Chris Lattnera45664f2008-11-10 02:56:27 +0000472//===----------------------------------------------------------------------===//
Devang Patel7136a652009-07-01 22:10:23 +0000473// DIDescriptor: dump routines for all descriptors.
474//===----------------------------------------------------------------------===//
475
476
477/// dump - Print descriptor.
478void DIDescriptor::dump() const {
David Greene0eb5b662009-12-23 19:45:49 +0000479 dbgs() << "[" << dwarf::TagString(getTag()) << "] ";
480 dbgs().write_hex((intptr_t) &*DbgNode) << ']';
Devang Patel7136a652009-07-01 22:10:23 +0000481}
482
483/// dump - Print compile unit.
484void DICompileUnit::dump() const {
485 if (getLanguage())
David Greene0eb5b662009-12-23 19:45:49 +0000486 dbgs() << " [" << dwarf::LanguageString(getLanguage()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000487
David Greene0eb5b662009-12-23 19:45:49 +0000488 dbgs() << " [" << getDirectory() << "/" << getFilename() << " ]";
Devang Patel7136a652009-07-01 22:10:23 +0000489}
490
491/// dump - Print type.
492void DIType::dump() const {
493 if (isNull()) return;
494
Devang Patel65dbc902009-11-25 17:36:49 +0000495 StringRef Res = getName();
496 if (!Res.empty())
David Greene0eb5b662009-12-23 19:45:49 +0000497 dbgs() << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000498
499 unsigned Tag = getTag();
David Greene0eb5b662009-12-23 19:45:49 +0000500 dbgs() << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000501
502 // TODO : Print context
503 getCompileUnit().dump();
David Greene0eb5b662009-12-23 19:45:49 +0000504 dbgs() << " ["
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000505 << getLineNumber() << ", "
Chris Lattnera81d29b2009-08-23 07:33:14 +0000506 << getSizeInBits() << ", "
507 << getAlignInBits() << ", "
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000508 << getOffsetInBits()
Chris Lattnera81d29b2009-08-23 07:33:14 +0000509 << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000510
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000511 if (isPrivate())
David Greene0eb5b662009-12-23 19:45:49 +0000512 dbgs() << " [private] ";
Devang Patel7136a652009-07-01 22:10:23 +0000513 else if (isProtected())
David Greene0eb5b662009-12-23 19:45:49 +0000514 dbgs() << " [protected] ";
Devang Patel7136a652009-07-01 22:10:23 +0000515
516 if (isForwardDecl())
David Greene0eb5b662009-12-23 19:45:49 +0000517 dbgs() << " [fwd] ";
Devang Patel7136a652009-07-01 22:10:23 +0000518
Devang Patel6ceea332009-08-31 18:49:10 +0000519 if (isBasicType())
Devang Patele4b27562009-08-28 23:24:31 +0000520 DIBasicType(DbgNode).dump();
Devang Patel6ceea332009-08-31 18:49:10 +0000521 else if (isDerivedType())
Devang Patele4b27562009-08-28 23:24:31 +0000522 DIDerivedType(DbgNode).dump();
Devang Patel6ceea332009-08-31 18:49:10 +0000523 else if (isCompositeType())
Devang Patele4b27562009-08-28 23:24:31 +0000524 DICompositeType(DbgNode).dump();
Devang Patel7136a652009-07-01 22:10:23 +0000525 else {
David Greene0eb5b662009-12-23 19:45:49 +0000526 dbgs() << "Invalid DIType\n";
Devang Patel7136a652009-07-01 22:10:23 +0000527 return;
528 }
529
David Greene0eb5b662009-12-23 19:45:49 +0000530 dbgs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000531}
532
533/// dump - Print basic type.
534void DIBasicType::dump() const {
David Greene0eb5b662009-12-23 19:45:49 +0000535 dbgs() << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000536}
537
538/// dump - Print derived type.
539void DIDerivedType::dump() const {
David Greene0eb5b662009-12-23 19:45:49 +0000540 dbgs() << "\n\t Derived From: "; getTypeDerivedFrom().dump();
Devang Patel7136a652009-07-01 22:10:23 +0000541}
542
543/// dump - Print composite type.
544void DICompositeType::dump() const {
545 DIArray A = getTypeArray();
546 if (A.isNull())
547 return;
David Greene0eb5b662009-12-23 19:45:49 +0000548 dbgs() << " [" << A.getNumElements() << " elements]";
Devang Patel7136a652009-07-01 22:10:23 +0000549}
550
551/// dump - Print global.
552void DIGlobal::dump() const {
Devang Patel65dbc902009-11-25 17:36:49 +0000553 StringRef Res = getName();
554 if (!Res.empty())
David Greene0eb5b662009-12-23 19:45:49 +0000555 dbgs() << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000556
557 unsigned Tag = getTag();
David Greene0eb5b662009-12-23 19:45:49 +0000558 dbgs() << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000559
560 // TODO : Print context
561 getCompileUnit().dump();
David Greene0eb5b662009-12-23 19:45:49 +0000562 dbgs() << " [" << getLineNumber() << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000563
564 if (isLocalToUnit())
David Greene0eb5b662009-12-23 19:45:49 +0000565 dbgs() << " [local] ";
Devang Patel7136a652009-07-01 22:10:23 +0000566
567 if (isDefinition())
David Greene0eb5b662009-12-23 19:45:49 +0000568 dbgs() << " [def] ";
Devang Patel7136a652009-07-01 22:10:23 +0000569
Devang Patel6ceea332009-08-31 18:49:10 +0000570 if (isGlobalVariable())
Devang Patele4b27562009-08-28 23:24:31 +0000571 DIGlobalVariable(DbgNode).dump();
Devang Patel7136a652009-07-01 22:10:23 +0000572
David Greene0eb5b662009-12-23 19:45:49 +0000573 dbgs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000574}
575
576/// dump - Print subprogram.
577void DISubprogram::dump() const {
Devang Patel65dbc902009-11-25 17:36:49 +0000578 StringRef Res = getName();
579 if (!Res.empty())
David Greene0eb5b662009-12-23 19:45:49 +0000580 dbgs() << " [" << Res << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000581
582 unsigned Tag = getTag();
David Greene0eb5b662009-12-23 19:45:49 +0000583 dbgs() << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000584
585 // TODO : Print context
586 getCompileUnit().dump();
David Greene0eb5b662009-12-23 19:45:49 +0000587 dbgs() << " [" << getLineNumber() << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000588
589 if (isLocalToUnit())
David Greene0eb5b662009-12-23 19:45:49 +0000590 dbgs() << " [local] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000591
592 if (isDefinition())
David Greene0eb5b662009-12-23 19:45:49 +0000593 dbgs() << " [def] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000594
David Greene0eb5b662009-12-23 19:45:49 +0000595 dbgs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000596}
597
598/// dump - Print global variable.
599void DIGlobalVariable::dump() const {
David Greene0eb5b662009-12-23 19:45:49 +0000600 dbgs() << " [";
Chris Lattnera81d29b2009-08-23 07:33:14 +0000601 getGlobal()->dump();
David Greene0eb5b662009-12-23 19:45:49 +0000602 dbgs() << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000603}
604
605/// dump - Print variable.
606void DIVariable::dump() const {
Devang Patel65dbc902009-11-25 17:36:49 +0000607 StringRef Res = getName();
608 if (!Res.empty())
David Greene0eb5b662009-12-23 19:45:49 +0000609 dbgs() << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000610
611 getCompileUnit().dump();
David Greene0eb5b662009-12-23 19:45:49 +0000612 dbgs() << " [" << getLineNumber() << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000613 getType().dump();
David Greene0eb5b662009-12-23 19:45:49 +0000614 dbgs() << "\n";
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000615
616 // FIXME: Dump complex addresses
Devang Patel7136a652009-07-01 22:10:23 +0000617}
618
619//===----------------------------------------------------------------------===//
Chris Lattnera45664f2008-11-10 02:56:27 +0000620// DIFactory: Basic Helpers
621//===----------------------------------------------------------------------===//
622
Bill Wendlingdc817b62009-05-14 18:26:15 +0000623DIFactory::DIFactory(Module &m)
Devang Patel427ef4e2009-11-17 22:39:08 +0000624 : M(m), VMContext(M.getContext()), DeclareFn(0) {
Devang Patel3ddf7042009-11-13 02:27:33 +0000625 EmptyStructPtr = PointerType::getUnqual(StructType::get(VMContext));
626}
Chris Lattner497a7a82008-11-10 04:10:34 +0000627
Chris Lattnera45664f2008-11-10 02:56:27 +0000628Constant *DIFactory::GetTagConstant(unsigned TAG) {
Devang Patel6906ba52009-01-20 19:22:03 +0000629 assert((TAG & LLVMDebugVersionMask) == 0 &&
Chris Lattnera45664f2008-11-10 02:56:27 +0000630 "Tag too large for debug encoding!");
Owen Anderson1d0be152009-08-13 21:58:54 +0000631 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
Chris Lattnera45664f2008-11-10 02:56:27 +0000632}
633
Chris Lattnera45664f2008-11-10 02:56:27 +0000634//===----------------------------------------------------------------------===//
635// DIFactory: Primary Constructors
636//===----------------------------------------------------------------------===//
637
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000638/// GetOrCreateArray - Create an descriptor for an array of descriptors.
Chris Lattnera45664f2008-11-10 02:56:27 +0000639/// This implicitly uniques the arrays created.
640DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
Devang Patele4b27562009-08-28 23:24:31 +0000641 SmallVector<Value*, 16> Elts;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000642
Devang Patele4b27562009-08-28 23:24:31 +0000643 if (NumTys == 0)
644 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)));
645 else
646 for (unsigned i = 0; i != NumTys; ++i)
647 Elts.push_back(Tys[i].getNode());
Devang Patel82459882009-08-26 05:01:18 +0000648
Devang Patele4b27562009-08-28 23:24:31 +0000649 return DIArray(MDNode::get(VMContext,Elts.data(), Elts.size()));
Chris Lattnera45664f2008-11-10 02:56:27 +0000650}
651
652/// GetOrCreateSubrange - Create a descriptor for a value range. This
653/// implicitly uniques the values returned.
654DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
Devang Patele4b27562009-08-28 23:24:31 +0000655 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000656 GetTagConstant(dwarf::DW_TAG_subrange_type),
Owen Anderson1d0be152009-08-13 21:58:54 +0000657 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
658 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
Chris Lattnera45664f2008-11-10 02:56:27 +0000659 };
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000660
Devang Patele4b27562009-08-28 23:24:31 +0000661 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000662}
663
664
665
666/// CreateCompileUnit - Create a new descriptor for the specified compile
667/// unit. Note that this does not unique compile units within the module.
668DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
Devang Patel65dbc902009-11-25 17:36:49 +0000669 StringRef Filename,
670 StringRef Directory,
671 StringRef Producer,
Devang Pateldd9db662009-01-30 18:20:31 +0000672 bool isMain,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000673 bool isOptimized,
Devang Patel65dbc902009-11-25 17:36:49 +0000674 StringRef Flags,
Devang Patel13319ce2009-02-17 22:43:44 +0000675 unsigned RunTimeVer) {
Devang Patele4b27562009-08-28 23:24:31 +0000676 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000677 GetTagConstant(dwarf::DW_TAG_compile_unit),
Devang Patele4b27562009-08-28 23:24:31 +0000678 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Owen Anderson1d0be152009-08-13 21:58:54 +0000679 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
Devang Patele4b27562009-08-28 23:24:31 +0000680 MDString::get(VMContext, Filename),
681 MDString::get(VMContext, Directory),
682 MDString::get(VMContext, Producer),
Owen Anderson1d0be152009-08-13 21:58:54 +0000683 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
684 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
Devang Patele4b27562009-08-28 23:24:31 +0000685 MDString::get(VMContext, Flags),
Owen Anderson1d0be152009-08-13 21:58:54 +0000686 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
Chris Lattnera45664f2008-11-10 02:56:27 +0000687 };
Devang Patele4b27562009-08-28 23:24:31 +0000688
689 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000690}
691
692/// CreateEnumerator - Create a single enumerator value.
Devang Patel65dbc902009-11-25 17:36:49 +0000693DIEnumerator DIFactory::CreateEnumerator(StringRef Name, uint64_t Val){
Devang Patele4b27562009-08-28 23:24:31 +0000694 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000695 GetTagConstant(dwarf::DW_TAG_enumerator),
Devang Patele4b27562009-08-28 23:24:31 +0000696 MDString::get(VMContext, Name),
Owen Anderson1d0be152009-08-13 21:58:54 +0000697 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
Chris Lattnera45664f2008-11-10 02:56:27 +0000698 };
Devang Patele4b27562009-08-28 23:24:31 +0000699 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000700}
701
702
703/// CreateBasicType - Create a basic type like int, float, etc.
704DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000705 StringRef Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000706 DICompileUnit CompileUnit,
707 unsigned LineNumber,
708 uint64_t SizeInBits,
709 uint64_t AlignInBits,
710 uint64_t OffsetInBits, unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000711 unsigned Encoding) {
Devang Patele4b27562009-08-28 23:24:31 +0000712 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000713 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patele4b27562009-08-28 23:24:31 +0000714 Context.getNode(),
715 MDString::get(VMContext, Name),
716 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000717 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
718 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
719 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
720 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
721 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
722 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
Chris Lattnera45664f2008-11-10 02:56:27 +0000723 };
Devang Patele4b27562009-08-28 23:24:31 +0000724 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000725}
726
Devang Patelac16d442009-10-26 16:54:35 +0000727
728/// CreateBasicType - Create a basic type like int, float, etc.
729DIBasicType DIFactory::CreateBasicTypeEx(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000730 StringRef Name,
Devang Patelac16d442009-10-26 16:54:35 +0000731 DICompileUnit CompileUnit,
732 unsigned LineNumber,
733 Constant *SizeInBits,
734 Constant *AlignInBits,
735 Constant *OffsetInBits, unsigned Flags,
736 unsigned Encoding) {
737 Value *Elts[] = {
738 GetTagConstant(dwarf::DW_TAG_base_type),
739 Context.getNode(),
740 MDString::get(VMContext, Name),
741 CompileUnit.getNode(),
742 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
743 SizeInBits,
744 AlignInBits,
745 OffsetInBits,
746 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
747 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
748 };
749 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
750}
751
752
Chris Lattnera45664f2008-11-10 02:56:27 +0000753/// CreateDerivedType - Create a derived type like const qualified type,
754/// pointer, typedef, etc.
755DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
756 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000757 StringRef Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000758 DICompileUnit CompileUnit,
759 unsigned LineNumber,
760 uint64_t SizeInBits,
761 uint64_t AlignInBits,
762 uint64_t OffsetInBits,
763 unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000764 DIType DerivedFrom) {
Devang Patele4b27562009-08-28 23:24:31 +0000765 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000766 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000767 Context.getNode(),
768 MDString::get(VMContext, Name),
769 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000770 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
771 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
772 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
773 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
774 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patele4b27562009-08-28 23:24:31 +0000775 DerivedFrom.getNode(),
Chris Lattnera45664f2008-11-10 02:56:27 +0000776 };
Devang Patele4b27562009-08-28 23:24:31 +0000777 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000778}
779
Devang Patelac16d442009-10-26 16:54:35 +0000780
781/// CreateDerivedType - Create a derived type like const qualified type,
782/// pointer, typedef, etc.
783DIDerivedType DIFactory::CreateDerivedTypeEx(unsigned Tag,
784 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000785 StringRef Name,
Devang Patelac16d442009-10-26 16:54:35 +0000786 DICompileUnit CompileUnit,
787 unsigned LineNumber,
788 Constant *SizeInBits,
789 Constant *AlignInBits,
790 Constant *OffsetInBits,
791 unsigned Flags,
792 DIType DerivedFrom) {
793 Value *Elts[] = {
794 GetTagConstant(Tag),
795 Context.getNode(),
796 MDString::get(VMContext, Name),
797 CompileUnit.getNode(),
798 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
799 SizeInBits,
800 AlignInBits,
801 OffsetInBits,
802 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
803 DerivedFrom.getNode(),
804 };
805 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
806}
807
808
Chris Lattnera45664f2008-11-10 02:56:27 +0000809/// CreateCompositeType - Create a composite type like array, struct, etc.
810DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
811 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000812 StringRef Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000813 DICompileUnit CompileUnit,
814 unsigned LineNumber,
815 uint64_t SizeInBits,
816 uint64_t AlignInBits,
817 uint64_t OffsetInBits,
818 unsigned Flags,
819 DIType DerivedFrom,
Devang Patel13319ce2009-02-17 22:43:44 +0000820 DIArray Elements,
821 unsigned RuntimeLang) {
Owen Andersone277fed2009-07-07 16:31:25 +0000822
Devang Patele4b27562009-08-28 23:24:31 +0000823 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000824 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000825 Context.getNode(),
826 MDString::get(VMContext, Name),
827 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000828 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
829 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
830 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
831 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
832 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patele4b27562009-08-28 23:24:31 +0000833 DerivedFrom.getNode(),
834 Elements.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000835 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
Chris Lattnera45664f2008-11-10 02:56:27 +0000836 };
Devang Patele4b27562009-08-28 23:24:31 +0000837 return DICompositeType(MDNode::get(VMContext, &Elts[0], 12));
Chris Lattnera45664f2008-11-10 02:56:27 +0000838}
839
840
Devang Patelac16d442009-10-26 16:54:35 +0000841/// CreateCompositeType - Create a composite type like array, struct, etc.
842DICompositeType DIFactory::CreateCompositeTypeEx(unsigned Tag,
843 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000844 StringRef Name,
Devang Patelac16d442009-10-26 16:54:35 +0000845 DICompileUnit CompileUnit,
846 unsigned LineNumber,
847 Constant *SizeInBits,
848 Constant *AlignInBits,
849 Constant *OffsetInBits,
850 unsigned Flags,
851 DIType DerivedFrom,
852 DIArray Elements,
853 unsigned RuntimeLang) {
854
855 Value *Elts[] = {
856 GetTagConstant(Tag),
857 Context.getNode(),
858 MDString::get(VMContext, Name),
859 CompileUnit.getNode(),
860 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
861 SizeInBits,
862 AlignInBits,
863 OffsetInBits,
864 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
865 DerivedFrom.getNode(),
866 Elements.getNode(),
867 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
868 };
869 return DICompositeType(MDNode::get(VMContext, &Elts[0], 12));
870}
871
872
Chris Lattnera45664f2008-11-10 02:56:27 +0000873/// CreateSubprogram - Create a new descriptor for the specified subprogram.
874/// See comments in DISubprogram for descriptions of these fields. This
875/// method does not unique the generated descriptors.
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000876DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000877 StringRef Name,
878 StringRef DisplayName,
879 StringRef LinkageName,
Chris Lattnera45664f2008-11-10 02:56:27 +0000880 DICompileUnit CompileUnit,
881 unsigned LineNo, DIType Type,
882 bool isLocalToUnit,
Devang Patel5d11eb02009-12-03 19:11:07 +0000883 bool isDefinition,
884 unsigned VK, unsigned VIndex,
885 DIType ContainingType) {
Devang Patel854967e2008-12-17 22:39:29 +0000886
Devang Patele4b27562009-08-28 23:24:31 +0000887 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000888 GetTagConstant(dwarf::DW_TAG_subprogram),
Devang Patele4b27562009-08-28 23:24:31 +0000889 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
890 Context.getNode(),
891 MDString::get(VMContext, Name),
892 MDString::get(VMContext, DisplayName),
893 MDString::get(VMContext, LinkageName),
894 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000895 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000896 Type.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000897 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
Devang Patel5d11eb02009-12-03 19:11:07 +0000898 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
899 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
900 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
901 ContainingType.getNode()
Chris Lattnera45664f2008-11-10 02:56:27 +0000902 };
Devang Patel5d11eb02009-12-03 19:11:07 +0000903 return DISubprogram(MDNode::get(VMContext, &Elts[0], 14));
Chris Lattnera45664f2008-11-10 02:56:27 +0000904}
905
Devang Patele3a18de2009-12-01 23:09:02 +0000906/// CreateSubprogramDefinition - Create new subprogram descriptor for the
907/// given declaration.
908DISubprogram DIFactory::CreateSubprogramDefinition(DISubprogram &SPDeclaration) {
909 if (SPDeclaration.isDefinition())
910 return DISubprogram(SPDeclaration.getNode());
911
912 MDNode *DeclNode = SPDeclaration.getNode();
913 Value *Elts[] = {
914 GetTagConstant(dwarf::DW_TAG_subprogram),
915 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
916 DeclNode->getElement(2), // Context
917 DeclNode->getElement(3), // Name
918 DeclNode->getElement(4), // DisplayName
919 DeclNode->getElement(5), // LinkageName
920 DeclNode->getElement(6), // CompileUnit
921 DeclNode->getElement(7), // LineNo
922 DeclNode->getElement(8), // Type
923 DeclNode->getElement(9), // isLocalToUnit
Devang Patel5d11eb02009-12-03 19:11:07 +0000924 ConstantInt::get(Type::getInt1Ty(VMContext), true),
925 DeclNode->getElement(11), // Virtuality
926 DeclNode->getElement(12), // VIndex
927 DeclNode->getElement(13) // Containting Type
Devang Patele3a18de2009-12-01 23:09:02 +0000928 };
Devang Patel5d11eb02009-12-03 19:11:07 +0000929 return DISubprogram(MDNode::get(VMContext, &Elts[0], 14));
Devang Patele3a18de2009-12-01 23:09:02 +0000930}
931
Chris Lattnera45664f2008-11-10 02:56:27 +0000932/// CreateGlobalVariable - Create a new descriptor for the specified global.
933DIGlobalVariable
Devang Patel65dbc902009-11-25 17:36:49 +0000934DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
935 StringRef DisplayName,
936 StringRef LinkageName,
Chris Lattnera45664f2008-11-10 02:56:27 +0000937 DICompileUnit CompileUnit,
938 unsigned LineNo, DIType Type,bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000939 bool isDefinition, llvm::GlobalVariable *Val) {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000940 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000941 GetTagConstant(dwarf::DW_TAG_variable),
Devang Patele4b27562009-08-28 23:24:31 +0000942 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
943 Context.getNode(),
944 MDString::get(VMContext, Name),
945 MDString::get(VMContext, DisplayName),
946 MDString::get(VMContext, LinkageName),
947 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000948 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000949 Type.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000950 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
951 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
Devang Patele4b27562009-08-28 23:24:31 +0000952 Val
Chris Lattnera45664f2008-11-10 02:56:27 +0000953 };
Devang Patele4b27562009-08-28 23:24:31 +0000954
955 Value *const *Vs = &Elts[0];
956 MDNode *Node = MDNode::get(VMContext,Vs, 12);
957
958 // Create a named metadata so that we do not lose this mdnode.
959 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
960 NMD->addElement(Node);
961
962 return DIGlobalVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +0000963}
964
965
966/// CreateVariable - Create a new descriptor for the specified variable.
967DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000968 StringRef Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000969 DICompileUnit CompileUnit, unsigned LineNo,
Devang Pateldd9db662009-01-30 18:20:31 +0000970 DIType Type) {
Devang Patele4b27562009-08-28 23:24:31 +0000971 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000972 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000973 Context.getNode(),
974 MDString::get(VMContext, Name),
975 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000976 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000977 Type.getNode(),
Chris Lattnera45664f2008-11-10 02:56:27 +0000978 };
Devang Patele4b27562009-08-28 23:24:31 +0000979 return DIVariable(MDNode::get(VMContext, &Elts[0], 6));
Chris Lattnera45664f2008-11-10 02:56:27 +0000980}
981
982
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000983/// CreateComplexVariable - Create a new descriptor for the specified variable
984/// which has a complex address expression for its address.
985DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
986 const std::string &Name,
987 DICompileUnit CompileUnit,
988 unsigned LineNo,
989 DIType Type, SmallVector<Value *, 9> &addr) {
990 SmallVector<Value *, 9> Elts;
991 Elts.push_back(GetTagConstant(Tag));
992 Elts.push_back(Context.getNode());
993 Elts.push_back(MDString::get(VMContext, Name));
994 Elts.push_back(CompileUnit.getNode());
995 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
996 Elts.push_back(Type.getNode());
997 Elts.insert(Elts.end(), addr.begin(), addr.end());
998
999 return DIVariable(MDNode::get(VMContext, &Elts[0], 6+addr.size()));
1000}
1001
1002
Chris Lattnera45664f2008-11-10 02:56:27 +00001003/// CreateBlock - This creates a descriptor for a lexical block with the
Owen Anderson99035272009-07-07 17:12:53 +00001004/// specified parent VMContext.
Devang Patel5e005d82009-08-31 22:00:15 +00001005DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context) {
Devang Patele4b27562009-08-28 23:24:31 +00001006 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001007 GetTagConstant(dwarf::DW_TAG_lexical_block),
Devang Patele4b27562009-08-28 23:24:31 +00001008 Context.getNode()
Chris Lattnera45664f2008-11-10 02:56:27 +00001009 };
Devang Patel5e005d82009-08-31 22:00:15 +00001010 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 2));
Chris Lattnera45664f2008-11-10 02:56:27 +00001011}
1012
Devang Patel6404e4e2009-12-15 19:16:48 +00001013/// CreateNameSpace - This creates new descriptor for a namespace
1014/// with the specified parent context.
1015DINameSpace DIFactory::CreateNameSpace(DIDescriptor Context, StringRef Name,
1016 DICompileUnit CompileUnit,
1017 unsigned LineNo) {
1018 Value *Elts[] = {
1019 GetTagConstant(dwarf::DW_TAG_namespace),
1020 Context.getNode(),
1021 MDString::get(VMContext, Name),
1022 CompileUnit.getNode(),
1023 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1024 };
1025 return DINameSpace(MDNode::get(VMContext, &Elts[0], 5));
1026}
1027
Devang Patelf98d8fe2009-09-01 01:14:15 +00001028/// CreateLocation - Creates a debug info location.
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001029DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
Daniel Dunbara279bc32009-09-20 02:20:51 +00001030 DIScope S, DILocation OrigLoc) {
Devang Patelf98d8fe2009-09-01 01:14:15 +00001031 Value *Elts[] = {
1032 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1033 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
1034 S.getNode(),
1035 OrigLoc.getNode(),
1036 };
1037 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1038}
1039
Devang Patele54a5e82009-11-23 19:11:20 +00001040/// CreateLocation - Creates a debug info location.
1041DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
1042 DIScope S, MDNode *OrigLoc) {
1043 Value *Elts[] = {
1044 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1045 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
1046 S.getNode(),
1047 OrigLoc
1048 };
1049 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1050}
Chris Lattnera45664f2008-11-10 02:56:27 +00001051
1052//===----------------------------------------------------------------------===//
1053// DIFactory: Routines for inserting code into a function
1054//===----------------------------------------------------------------------===//
1055
Chris Lattnera45664f2008-11-10 02:56:27 +00001056/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Devang Patel6daf99b2009-11-10 22:05:35 +00001057Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
Devang Patel3ddf7042009-11-13 02:27:33 +00001058 Instruction *InsertBefore) {
1059 // Cast the storage to a {}* for the call to llvm.dbg.declare.
1060 Storage = new BitCastInst(Storage, EmptyStructPtr, "", InsertBefore);
1061
Chris Lattnera45664f2008-11-10 02:56:27 +00001062 if (!DeclareFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +00001063 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1064
Devang Patele4b27562009-08-28 23:24:31 +00001065 Value *Args[] = { Storage, D.getNode() };
Devang Patel6daf99b2009-11-10 22:05:35 +00001066 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
Mike Stumpe4250392009-10-01 22:08:58 +00001067}
1068
1069/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Devang Patel6daf99b2009-11-10 22:05:35 +00001070Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
Devang Patel3ddf7042009-11-13 02:27:33 +00001071 BasicBlock *InsertAtEnd) {
1072 // Cast the storage to a {}* for the call to llvm.dbg.declare.
1073 Storage = new BitCastInst(Storage, EmptyStructPtr, "", InsertAtEnd);
1074
Mike Stumpe4250392009-10-01 22:08:58 +00001075 if (!DeclareFn)
1076 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1077
1078 Value *Args[] = { Storage, D.getNode() };
Devang Patel6daf99b2009-11-10 22:05:35 +00001079 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);
Chris Lattnera45664f2008-11-10 02:56:27 +00001080}
Torok Edwin620f2802008-12-16 09:07:36 +00001081
Victor Hernandezc59b3352009-12-07 21:54:43 +00001082/// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1083Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, Value *Offset,
1084 DIVariable D,
1085 Instruction *InsertBefore) {
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001086 assert(V && "no value passed to dbg.value");
1087 assert(Offset->getType() == Type::getInt64Ty(V->getContext()) &&
1088 "offset must be i64");
1089 if (!ValueFn)
1090 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1091
1092 Value *Elts[] = { V };
1093 Value *Args[] = { MDNode::get(V->getContext(), Elts, 1), Offset,
1094 D.getNode() };
1095 return CallInst::Create(ValueFn, Args, Args+3, "", InsertBefore);
1096}
1097
Victor Hernandezc59b3352009-12-07 21:54:43 +00001098/// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1099Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, Value *Offset,
1100 DIVariable D,
1101 BasicBlock *InsertAtEnd) {
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001102 assert(V && "no value passed to dbg.value");
1103 assert(Offset->getType() == Type::getInt64Ty(V->getContext()) &&
1104 "offset must be i64");
1105 if (!ValueFn)
1106 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1107
1108 Value *Elts[] = { V };
1109 Value *Args[] = { MDNode::get(V->getContext(), Elts, 1), Offset,
1110 D.getNode() };
1111 return CallInst::Create(ValueFn, Args, Args+3, "", InsertAtEnd);
1112}
Devang Patele4b27562009-08-28 23:24:31 +00001113
Devang Pateld2f79a12009-07-28 19:55:13 +00001114//===----------------------------------------------------------------------===//
Devang Patel98c65172009-07-30 18:25:15 +00001115// DebugInfoFinder implementations.
Devang Pateld2f79a12009-07-28 19:55:13 +00001116//===----------------------------------------------------------------------===//
1117
Devang Patel98c65172009-07-30 18:25:15 +00001118/// processModule - Process entire module and collect debug info.
1119void DebugInfoFinder::processModule(Module &M) {
Devang Patele4b27562009-08-28 23:24:31 +00001120
Devang Patelbeab41b2009-10-07 22:04:08 +00001121 MetadataContext &TheMetadata = M.getContext().getMetadata();
Chris Lattner0eb41982009-12-28 20:45:51 +00001122 unsigned MDDbgKind = TheMetadata.getMDKindID("dbg");
Devang Patel70d75ca2009-11-12 19:02:56 +00001123
Devang Pateld2f79a12009-07-28 19:55:13 +00001124 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1125 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
1126 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
1127 ++BI) {
Devang Patel70d75ca2009-11-12 19:02:56 +00001128 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
Devang Patelb4d31302009-07-31 18:18:52 +00001129 processDeclare(DDI);
Chris Lattner0eb41982009-12-28 20:45:51 +00001130 else if (MDNode *L = TheMetadata.getMD(MDDbgKind, BI))
1131 processLocation(DILocation(L));
Devang Pateld2f79a12009-07-28 19:55:13 +00001132 }
Devang Patele4b27562009-08-28 23:24:31 +00001133
1134 NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv");
1135 if (!NMD)
1136 return;
1137
1138 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
1139 DIGlobalVariable DIG(cast<MDNode>(NMD->getElement(i)));
Devang Pateld2f79a12009-07-28 19:55:13 +00001140 if (addGlobalVariable(DIG)) {
1141 addCompileUnit(DIG.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +00001142 processType(DIG.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +00001143 }
1144 }
1145}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001146
Devang Patel6daf99b2009-11-10 22:05:35 +00001147/// processLocation - Process DILocation.
1148void DebugInfoFinder::processLocation(DILocation Loc) {
1149 if (Loc.isNull()) return;
1150 DIScope S(Loc.getScope().getNode());
1151 if (S.isNull()) return;
1152 if (S.isCompileUnit())
1153 addCompileUnit(DICompileUnit(S.getNode()));
1154 else if (S.isSubprogram())
1155 processSubprogram(DISubprogram(S.getNode()));
1156 else if (S.isLexicalBlock())
1157 processLexicalBlock(DILexicalBlock(S.getNode()));
1158 processLocation(Loc.getOrigLocation());
1159}
1160
Devang Patel98c65172009-07-30 18:25:15 +00001161/// processType - Process DIType.
1162void DebugInfoFinder::processType(DIType DT) {
Devang Patel72bcdb62009-08-10 22:09:58 +00001163 if (!addType(DT))
Devang Pateld2f79a12009-07-28 19:55:13 +00001164 return;
1165
1166 addCompileUnit(DT.getCompileUnit());
Devang Patel6ceea332009-08-31 18:49:10 +00001167 if (DT.isCompositeType()) {
Devang Patele4b27562009-08-28 23:24:31 +00001168 DICompositeType DCT(DT.getNode());
Devang Patel98c65172009-07-30 18:25:15 +00001169 processType(DCT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001170 DIArray DA = DCT.getTypeArray();
1171 if (!DA.isNull())
1172 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1173 DIDescriptor D = DA.getElement(i);
Devang Patele4b27562009-08-28 23:24:31 +00001174 DIType TypeE = DIType(D.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001175 if (!TypeE.isNull())
Devang Patel98c65172009-07-30 18:25:15 +00001176 processType(TypeE);
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001177 else
Devang Patele4b27562009-08-28 23:24:31 +00001178 processSubprogram(DISubprogram(D.getNode()));
Devang Pateld2f79a12009-07-28 19:55:13 +00001179 }
Devang Patel6ceea332009-08-31 18:49:10 +00001180 } else if (DT.isDerivedType()) {
Devang Patele4b27562009-08-28 23:24:31 +00001181 DIDerivedType DDT(DT.getNode());
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001182 if (!DDT.isNull())
Devang Patel98c65172009-07-30 18:25:15 +00001183 processType(DDT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001184 }
1185}
1186
Devang Patelbeab41b2009-10-07 22:04:08 +00001187/// processLexicalBlock
1188void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
1189 if (LB.isNull())
1190 return;
1191 DIScope Context = LB.getContext();
1192 if (Context.isLexicalBlock())
1193 return processLexicalBlock(DILexicalBlock(Context.getNode()));
1194 else
1195 return processSubprogram(DISubprogram(Context.getNode()));
1196}
1197
Devang Patel98c65172009-07-30 18:25:15 +00001198/// processSubprogram - Process DISubprogram.
1199void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Patele802f1c2009-07-30 17:30:23 +00001200 if (SP.isNull())
1201 return;
Devang Pateld2f79a12009-07-28 19:55:13 +00001202 if (!addSubprogram(SP))
1203 return;
1204 addCompileUnit(SP.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +00001205 processType(SP.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +00001206}
1207
Devang Patelb4d31302009-07-31 18:18:52 +00001208/// processDeclare - Process DbgDeclareInst.
1209void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
Devang Patele4b27562009-08-28 23:24:31 +00001210 DIVariable DV(cast<MDNode>(DDI->getVariable()));
Devang Patelb4d31302009-07-31 18:18:52 +00001211 if (DV.isNull())
1212 return;
1213
Devang Patele4b27562009-08-28 23:24:31 +00001214 if (!NodesSeen.insert(DV.getNode()))
Devang Patelb4d31302009-07-31 18:18:52 +00001215 return;
1216
1217 addCompileUnit(DV.getCompileUnit());
1218 processType(DV.getType());
1219}
1220
Devang Patel72bcdb62009-08-10 22:09:58 +00001221/// addType - Add type into Tys.
1222bool DebugInfoFinder::addType(DIType DT) {
1223 if (DT.isNull())
1224 return false;
1225
Devang Patele4b27562009-08-28 23:24:31 +00001226 if (!NodesSeen.insert(DT.getNode()))
Devang Patel72bcdb62009-08-10 22:09:58 +00001227 return false;
1228
Devang Patele4b27562009-08-28 23:24:31 +00001229 TYs.push_back(DT.getNode());
Devang Patel72bcdb62009-08-10 22:09:58 +00001230 return true;
1231}
1232
Devang Pateld2f79a12009-07-28 19:55:13 +00001233/// addCompileUnit - Add compile unit into CUs.
Devang Patel98c65172009-07-30 18:25:15 +00001234bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001235 if (CU.isNull())
1236 return false;
1237
Devang Patele4b27562009-08-28 23:24:31 +00001238 if (!NodesSeen.insert(CU.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001239 return false;
1240
Devang Patele4b27562009-08-28 23:24:31 +00001241 CUs.push_back(CU.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001242 return true;
1243}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001244
Devang Pateld2f79a12009-07-28 19:55:13 +00001245/// addGlobalVariable - Add global variable into GVs.
Devang Patel98c65172009-07-30 18:25:15 +00001246bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001247 if (DIG.isNull())
1248 return false;
1249
Devang Patele4b27562009-08-28 23:24:31 +00001250 if (!NodesSeen.insert(DIG.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001251 return false;
1252
Devang Patele4b27562009-08-28 23:24:31 +00001253 GVs.push_back(DIG.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001254 return true;
1255}
1256
1257// addSubprogram - Add subprgoram into SPs.
Devang Patel98c65172009-07-30 18:25:15 +00001258bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001259 if (SP.isNull())
1260 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001261
Devang Patele4b27562009-08-28 23:24:31 +00001262 if (!NodesSeen.insert(SP.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001263 return false;
1264
Devang Patele4b27562009-08-28 23:24:31 +00001265 SPs.push_back(SP.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001266 return true;
1267}
1268
Torok Edwin620f2802008-12-16 09:07:36 +00001269namespace llvm {
Bill Wendlingdc817b62009-05-14 18:26:15 +00001270 /// findStopPoint - Find the stoppoint coressponding to this instruction, that
1271 /// is the stoppoint that dominates this instruction.
1272 const DbgStopPointInst *findStopPoint(const Instruction *Inst) {
Torok Edwin620f2802008-12-16 09:07:36 +00001273 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(Inst))
1274 return DSI;
1275
1276 const BasicBlock *BB = Inst->getParent();
1277 BasicBlock::const_iterator I = Inst, B;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001278 while (BB) {
Torok Edwin620f2802008-12-16 09:07:36 +00001279 B = BB->begin();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001280
Torok Edwin620f2802008-12-16 09:07:36 +00001281 // A BB consisting only of a terminator can't have a stoppoint.
Bill Wendlingdc817b62009-05-14 18:26:15 +00001282 while (I != B) {
1283 --I;
1284 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
1285 return DSI;
Torok Edwin620f2802008-12-16 09:07:36 +00001286 }
Bill Wendlingdc817b62009-05-14 18:26:15 +00001287
1288 // This BB didn't have a stoppoint: if there is only one predecessor, look
1289 // for a stoppoint there. We could use getIDom(), but that would require
1290 // dominator info.
Torok Edwin620f2802008-12-16 09:07:36 +00001291 BB = I->getParent()->getUniquePredecessor();
1292 if (BB)
1293 I = BB->getTerminator();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001294 }
1295
Torok Edwin620f2802008-12-16 09:07:36 +00001296 return 0;
1297 }
1298
Bill Wendlingdc817b62009-05-14 18:26:15 +00001299 /// findBBStopPoint - Find the stoppoint corresponding to first real
1300 /// (non-debug intrinsic) instruction in this Basic Block, and return the
1301 /// stoppoint for it.
1302 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB) {
1303 for(BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001304 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
1305 return DSI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001306
1307 // Fallback to looking for stoppoint of unique predecessor. Useful if this
1308 // BB contains no stoppoints, but unique predecessor does.
Torok Edwin620f2802008-12-16 09:07:36 +00001309 BB = BB->getUniquePredecessor();
1310 if (BB)
1311 return findStopPoint(BB->getTerminator());
Bill Wendlingdc817b62009-05-14 18:26:15 +00001312
Torok Edwin620f2802008-12-16 09:07:36 +00001313 return 0;
1314 }
1315
Bill Wendlingdc817b62009-05-14 18:26:15 +00001316 Value *findDbgGlobalDeclare(GlobalVariable *V) {
Torok Edwinff7d0e92009-03-10 13:41:26 +00001317 const Module *M = V->getParent();
Devang Patele4b27562009-08-28 23:24:31 +00001318 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1319 if (!NMD)
1320 return 0;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001321
Devang Patele4b27562009-08-28 23:24:31 +00001322 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
1323 DIGlobalVariable DIG(cast_or_null<MDNode>(NMD->getElement(i)));
1324 if (DIG.isNull())
1325 continue;
1326 if (DIG.getGlobal() == V)
1327 return DIG.getNode();
Torok Edwinff7d0e92009-03-10 13:41:26 +00001328 }
Torok Edwinff7d0e92009-03-10 13:41:26 +00001329 return 0;
1330 }
1331
Bill Wendlingdc817b62009-05-14 18:26:15 +00001332 /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
Torok Edwin620f2802008-12-16 09:07:36 +00001333 /// It looks through pointer casts too.
Bill Wendlingdc817b62009-05-14 18:26:15 +00001334 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts) {
Torok Edwin620f2802008-12-16 09:07:36 +00001335 if (stripCasts) {
1336 V = V->stripPointerCasts();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001337
Torok Edwin620f2802008-12-16 09:07:36 +00001338 // Look for the bitcast.
1339 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001340 I != E; ++I)
Devang Patel94dfaec2009-10-29 18:20:34 +00001341 if (isa<BitCastInst>(I)) {
1342 const DbgDeclareInst *DDI = findDbgDeclare(*I, false);
1343 if (DDI) return DDI;
1344 }
Torok Edwin620f2802008-12-16 09:07:36 +00001345 return 0;
1346 }
1347
Bill Wendlingdc817b62009-05-14 18:26:15 +00001348 // Find llvm.dbg.declare among uses of the instruction.
Torok Edwin620f2802008-12-16 09:07:36 +00001349 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001350 I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001351 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I))
1352 return DDI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001353
Torok Edwin620f2802008-12-16 09:07:36 +00001354 return 0;
1355 }
Torok Edwinff7d0e92009-03-10 13:41:26 +00001356
Devang Patel5ccdd102009-09-29 18:40:58 +00001357bool getLocationInfo(const Value *V, std::string &DisplayName,
1358 std::string &Type, unsigned &LineNo, std::string &File,
Bill Wendlingdc817b62009-05-14 18:26:15 +00001359 std::string &Dir) {
Torok Edwinff7d0e92009-03-10 13:41:26 +00001360 DICompileUnit Unit;
1361 DIType TypeD;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001362
Torok Edwinff7d0e92009-03-10 13:41:26 +00001363 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1364 Value *DIGV = findDbgGlobalDeclare(GV);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001365 if (!DIGV) return false;
Devang Patele4b27562009-08-28 23:24:31 +00001366 DIGlobalVariable Var(cast<MDNode>(DIGV));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001367
Devang Patel65dbc902009-11-25 17:36:49 +00001368 StringRef D = Var.getDisplayName();
1369 if (!D.empty())
Devang Patel5ccdd102009-09-29 18:40:58 +00001370 DisplayName = D;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001371 LineNo = Var.getLineNumber();
1372 Unit = Var.getCompileUnit();
1373 TypeD = Var.getType();
1374 } else {
1375 const DbgDeclareInst *DDI = findDbgDeclare(V);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001376 if (!DDI) return false;
Devang Patele4b27562009-08-28 23:24:31 +00001377 DIVariable Var(cast<MDNode>(DDI->getVariable()));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001378
Devang Patel65dbc902009-11-25 17:36:49 +00001379 StringRef D = Var.getName();
1380 if (!D.empty())
Devang Patel5ccdd102009-09-29 18:40:58 +00001381 DisplayName = D;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001382 LineNo = Var.getLineNumber();
1383 Unit = Var.getCompileUnit();
1384 TypeD = Var.getType();
1385 }
Bill Wendlingdc817b62009-05-14 18:26:15 +00001386
Devang Patel65dbc902009-11-25 17:36:49 +00001387 StringRef T = TypeD.getName();
1388 if (!T.empty())
Devang Patel5ccdd102009-09-29 18:40:58 +00001389 Type = T;
Devang Patel65dbc902009-11-25 17:36:49 +00001390 StringRef F = Unit.getFilename();
1391 if (!F.empty())
Devang Patel5ccdd102009-09-29 18:40:58 +00001392 File = F;
Devang Patel65dbc902009-11-25 17:36:49 +00001393 StringRef D = Unit.getDirectory();
1394 if (!D.empty())
Devang Patel5ccdd102009-09-29 18:40:58 +00001395 Dir = D;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001396 return true;
1397 }
Devang Patel13e16b62009-06-26 01:49:18 +00001398
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001399 /// isValidDebugInfoIntrinsic - Return true if SPI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001400 /// info intrinsic.
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001401 bool isValidDebugInfoIntrinsic(DbgStopPointInst &SPI,
Devang Patel9e529c32009-07-02 01:15:24 +00001402 CodeGenOpt::Level OptLev) {
1403 return DIDescriptor::ValidDebugInfo(SPI.getContext(), OptLev);
1404 }
1405
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001406 /// isValidDebugInfoIntrinsic - Return true if FSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001407 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001408 bool isValidDebugInfoIntrinsic(DbgFuncStartInst &FSI,
1409 CodeGenOpt::Level OptLev) {
1410 return DIDescriptor::ValidDebugInfo(FSI.getSubprogram(), OptLev);
1411 }
1412
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001413 /// isValidDebugInfoIntrinsic - Return true if RSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001414 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001415 bool isValidDebugInfoIntrinsic(DbgRegionStartInst &RSI,
1416 CodeGenOpt::Level OptLev) {
1417 return DIDescriptor::ValidDebugInfo(RSI.getContext(), OptLev);
1418 }
1419
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001420 /// isValidDebugInfoIntrinsic - Return true if REI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001421 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001422 bool isValidDebugInfoIntrinsic(DbgRegionEndInst &REI,
1423 CodeGenOpt::Level OptLev) {
1424 return DIDescriptor::ValidDebugInfo(REI.getContext(), OptLev);
1425 }
1426
1427
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001428 /// isValidDebugInfoIntrinsic - Return true if DI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001429 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001430 bool isValidDebugInfoIntrinsic(DbgDeclareInst &DI,
1431 CodeGenOpt::Level OptLev) {
1432 return DIDescriptor::ValidDebugInfo(DI.getVariable(), OptLev);
1433 }
1434
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001435 /// ExtractDebugLocation - Extract debug location information
Devang Patel9e529c32009-07-02 01:15:24 +00001436 /// from llvm.dbg.stoppoint intrinsic.
1437 DebugLoc ExtractDebugLocation(DbgStopPointInst &SPI,
Devang Patel9e529c32009-07-02 01:15:24 +00001438 DebugLocTracker &DebugLocInfo) {
1439 DebugLoc DL;
1440 Value *Context = SPI.getContext();
Devang Patel9e529c32009-07-02 01:15:24 +00001441
1442 // If this location is already tracked then use it.
Devang Patela1434042009-10-01 01:15:28 +00001443 DebugLocTuple Tuple(cast<MDNode>(Context), NULL, SPI.getLine(),
Devang Patel9e529c32009-07-02 01:15:24 +00001444 SPI.getColumn());
1445 DenseMap<DebugLocTuple, unsigned>::iterator II
1446 = DebugLocInfo.DebugIdMap.find(Tuple);
1447 if (II != DebugLocInfo.DebugIdMap.end())
1448 return DebugLoc::get(II->second);
1449
1450 // Add a new location entry.
1451 unsigned Id = DebugLocInfo.DebugLocations.size();
1452 DebugLocInfo.DebugLocations.push_back(Tuple);
1453 DebugLocInfo.DebugIdMap[Tuple] = Id;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001454
Devang Patel9e529c32009-07-02 01:15:24 +00001455 return DebugLoc::get(Id);
1456 }
1457
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001458 /// ExtractDebugLocation - Extract debug location information
Devang Patel1b75f442009-09-16 18:20:05 +00001459 /// from DILocation.
1460 DebugLoc ExtractDebugLocation(DILocation &Loc,
1461 DebugLocTracker &DebugLocInfo) {
1462 DebugLoc DL;
1463 MDNode *Context = Loc.getScope().getNode();
Devang Patela1434042009-10-01 01:15:28 +00001464 MDNode *InlinedLoc = NULL;
1465 if (!Loc.getOrigLocation().isNull())
1466 InlinedLoc = Loc.getOrigLocation().getNode();
Devang Patel1b75f442009-09-16 18:20:05 +00001467 // If this location is already tracked then use it.
Devang Patela1434042009-10-01 01:15:28 +00001468 DebugLocTuple Tuple(Context, InlinedLoc, Loc.getLineNumber(),
Daniel Dunbara279bc32009-09-20 02:20:51 +00001469 Loc.getColumnNumber());
Devang Patel1b75f442009-09-16 18:20:05 +00001470 DenseMap<DebugLocTuple, unsigned>::iterator II
1471 = DebugLocInfo.DebugIdMap.find(Tuple);
1472 if (II != DebugLocInfo.DebugIdMap.end())
1473 return DebugLoc::get(II->second);
1474
1475 // Add a new location entry.
1476 unsigned Id = DebugLocInfo.DebugLocations.size();
1477 DebugLocInfo.DebugLocations.push_back(Tuple);
1478 DebugLocInfo.DebugIdMap[Tuple] = Id;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001479
Devang Patel1b75f442009-09-16 18:20:05 +00001480 return DebugLoc::get(Id);
1481 }
1482
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001483 /// ExtractDebugLocation - Extract debug location information
Devang Patel9e529c32009-07-02 01:15:24 +00001484 /// from llvm.dbg.func_start intrinsic.
1485 DebugLoc ExtractDebugLocation(DbgFuncStartInst &FSI,
Devang Patel9e529c32009-07-02 01:15:24 +00001486 DebugLocTracker &DebugLocInfo) {
1487 DebugLoc DL;
1488 Value *SP = FSI.getSubprogram();
Devang Patel9e529c32009-07-02 01:15:24 +00001489
Devang Patele4b27562009-08-28 23:24:31 +00001490 DISubprogram Subprogram(cast<MDNode>(SP));
Devang Patel9e529c32009-07-02 01:15:24 +00001491 unsigned Line = Subprogram.getLineNumber();
1492 DICompileUnit CU(Subprogram.getCompileUnit());
1493
1494 // If this location is already tracked then use it.
Devang Patela1434042009-10-01 01:15:28 +00001495 DebugLocTuple Tuple(CU.getNode(), NULL, Line, /* Column */ 0);
Devang Patel9e529c32009-07-02 01:15:24 +00001496 DenseMap<DebugLocTuple, unsigned>::iterator II
1497 = DebugLocInfo.DebugIdMap.find(Tuple);
1498 if (II != DebugLocInfo.DebugIdMap.end())
1499 return DebugLoc::get(II->second);
1500
1501 // Add a new location entry.
1502 unsigned Id = DebugLocInfo.DebugLocations.size();
1503 DebugLocInfo.DebugLocations.push_back(Tuple);
1504 DebugLocInfo.DebugIdMap[Tuple] = Id;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001505
Devang Patel9e529c32009-07-02 01:15:24 +00001506 return DebugLoc::get(Id);
1507 }
Devang Patel193f7202009-11-24 01:14:22 +00001508
1509 /// getDISubprogram - Find subprogram that is enclosing this scope.
1510 DISubprogram getDISubprogram(MDNode *Scope) {
1511 DIDescriptor D(Scope);
1512 if (D.isNull())
1513 return DISubprogram();
1514
1515 if (D.isCompileUnit())
1516 return DISubprogram();
1517
1518 if (D.isSubprogram())
1519 return DISubprogram(Scope);
1520
1521 if (D.isLexicalBlock())
1522 return getDISubprogram(DILexicalBlock(Scope).getContext().getNode());
1523
1524 return DISubprogram();
1525 }
1526
1527 /// getDICompositeType - Find underlying composite type.
1528 DICompositeType getDICompositeType(DIType T) {
1529 if (T.isNull())
1530 return DICompositeType();
1531
1532 if (T.isCompositeType())
1533 return DICompositeType(T.getNode());
1534
1535 if (T.isDerivedType())
1536 return getDICompositeType(DIDerivedType(T.getNode()).getTypeDerivedFrom());
1537
1538 return DICompositeType();
1539 }
Torok Edwin620f2802008-12-16 09:07:36 +00001540}