blob: cf2c2777e7d5f120b11baf84eb929b995895ad17 [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"
21#include "llvm/Module.h"
22#include "llvm/Analysis/ValueTracking.h"
Devang Patele4b27562009-08-28 23:24:31 +000023#include "llvm/ADT/SmallPtrSet.h"
Dan Gohman17aa92c2010-07-21 23:38:33 +000024#include "llvm/ADT/SmallString.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"
Chris Lattnera81d29b2009-08-23 07:33:14 +000027#include "llvm/Support/raw_ostream.h"
Chris Lattnera45664f2008-11-10 02:56:27 +000028using namespace llvm;
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000029using namespace llvm::dwarf;
Chris Lattnera45664f2008-11-10 02:56:27 +000030
31//===----------------------------------------------------------------------===//
32// DIDescriptor
33//===----------------------------------------------------------------------===//
34
Devang Patel5b164b52010-08-02 22:51:46 +000035DIDescriptor::DIDescriptor(const DIFile F) : DbgNode(F.DbgNode) {
36}
37
38DIDescriptor::DIDescriptor(const DISubprogram F) : DbgNode(F.DbgNode) {
39}
40
41DIDescriptor::DIDescriptor(const DILexicalBlock F) : DbgNode(F.DbgNode) {
42}
43
44DIDescriptor::DIDescriptor(const DIVariable F) : DbgNode(F.DbgNode) {
45}
46
47DIDescriptor::DIDescriptor(const DIType F) : DbgNode(F.DbgNode) {
48}
49
Jim Grosbache62b6902010-07-21 21:36:25 +000050StringRef
Devang Patel5ccdd102009-09-29 18:40:58 +000051DIDescriptor::getStringField(unsigned Elt) const {
Devang Patele4b27562009-08-28 23:24:31 +000052 if (DbgNode == 0)
Devang Patel65dbc902009-11-25 17:36:49 +000053 return StringRef();
Chris Lattnera45664f2008-11-10 02:56:27 +000054
Chris Lattner5d0cacd2009-12-31 01:22:29 +000055 if (Elt < DbgNode->getNumOperands())
56 if (MDString *MDS = dyn_cast_or_null<MDString>(DbgNode->getOperand(Elt)))
Devang Patel65dbc902009-11-25 17:36:49 +000057 return MDS->getString();
Daniel Dunbarf612ff62009-09-19 20:40:05 +000058
Devang Patel65dbc902009-11-25 17:36:49 +000059 return StringRef();
Chris Lattnera45664f2008-11-10 02:56:27 +000060}
61
62uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +000063 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +000064 return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +000065
Chris Lattner5d0cacd2009-12-31 01:22:29 +000066 if (Elt < DbgNode->getNumOperands())
67 if (ConstantInt *CI = dyn_cast<ConstantInt>(DbgNode->getOperand(Elt)))
Devang Patele4b27562009-08-28 23:24:31 +000068 return CI->getZExtValue();
Daniel Dunbarf612ff62009-09-19 20:40:05 +000069
Chris Lattnera45664f2008-11-10 02:56:27 +000070 return 0;
71}
72
Chris Lattnera45664f2008-11-10 02:56:27 +000073DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +000074 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +000075 return DIDescriptor();
Bill Wendlingdc817b62009-05-14 18:26:15 +000076
Chris Lattner7a2f3e02010-03-31 05:53:47 +000077 if (Elt < DbgNode->getNumOperands())
Jim Grosbache62b6902010-07-21 21:36:25 +000078 return
79 DIDescriptor(dyn_cast_or_null<const MDNode>(DbgNode->getOperand(Elt)));
Devang Patele4b27562009-08-28 23:24:31 +000080 return DIDescriptor();
Chris Lattnera45664f2008-11-10 02:56:27 +000081}
82
83GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +000084 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +000085 return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +000086
Chris Lattner5d0cacd2009-12-31 01:22:29 +000087 if (Elt < DbgNode->getNumOperands())
88 return dyn_cast_or_null<GlobalVariable>(DbgNode->getOperand(Elt));
Devang Patele4b27562009-08-28 23:24:31 +000089 return 0;
Chris Lattnera45664f2008-11-10 02:56:27 +000090}
91
Devang Patel27398962010-08-09 21:39:24 +000092Constant *DIDescriptor::getConstantField(unsigned Elt) const {
93 if (DbgNode == 0)
94 return 0;
95
96 if (Elt < DbgNode->getNumOperands())
97 return dyn_cast_or_null<Constant>(DbgNode->getOperand(Elt));
98 return 0;
99}
100
Stuart Hastings215aa152010-06-11 20:08:44 +0000101Function *DIDescriptor::getFunctionField(unsigned Elt) const {
102 if (DbgNode == 0)
103 return 0;
104
105 if (Elt < DbgNode->getNumOperands())
106 return dyn_cast_or_null<Function>(DbgNode->getOperand(Elt));
107 return 0;
108}
109
Chris Lattnerf0908a32009-12-31 03:02:08 +0000110unsigned DIVariable::getNumAddrElements() const {
111 return DbgNode->getNumOperands()-6;
112}
113
114
Chris Lattnera45664f2008-11-10 02:56:27 +0000115//===----------------------------------------------------------------------===//
Devang Patel6ceea332009-08-31 18:49:10 +0000116// Predicates
Chris Lattnera45664f2008-11-10 02:56:27 +0000117//===----------------------------------------------------------------------===//
118
Devang Patel6ceea332009-08-31 18:49:10 +0000119/// isBasicType - Return true if the specified tag is legal for
120/// DIBasicType.
121bool DIDescriptor::isBasicType() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000122 return DbgNode && getTag() == dwarf::DW_TAG_base_type;
Torok Edwinb07fbd92008-12-13 08:25:29 +0000123}
Chris Lattnera45664f2008-11-10 02:56:27 +0000124
Devang Patel6ceea332009-08-31 18:49:10 +0000125/// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
126bool DIDescriptor::isDerivedType() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000127 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000128 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000129 case dwarf::DW_TAG_typedef:
130 case dwarf::DW_TAG_pointer_type:
131 case dwarf::DW_TAG_reference_type:
132 case dwarf::DW_TAG_const_type:
133 case dwarf::DW_TAG_volatile_type:
134 case dwarf::DW_TAG_restrict_type:
135 case dwarf::DW_TAG_member:
136 case dwarf::DW_TAG_inheritance:
137 return true;
138 default:
Devang Patele4b27562009-08-28 23:24:31 +0000139 // CompositeTypes are currently modelled as DerivedTypes.
Devang Patel6ceea332009-08-31 18:49:10 +0000140 return isCompositeType();
Chris Lattnera45664f2008-11-10 02:56:27 +0000141 }
142}
143
Chris Lattnera45664f2008-11-10 02:56:27 +0000144/// isCompositeType - Return true if the specified tag is legal for
145/// DICompositeType.
Devang Patel6ceea332009-08-31 18:49:10 +0000146bool DIDescriptor::isCompositeType() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000147 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000148 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000149 case dwarf::DW_TAG_array_type:
150 case dwarf::DW_TAG_structure_type:
151 case dwarf::DW_TAG_union_type:
152 case dwarf::DW_TAG_enumeration_type:
153 case dwarf::DW_TAG_vector_type:
154 case dwarf::DW_TAG_subroutine_type:
Devang Patel25cb0d72009-03-25 03:52:06 +0000155 case dwarf::DW_TAG_class_type:
Chris Lattnera45664f2008-11-10 02:56:27 +0000156 return true;
157 default:
158 return false;
159 }
160}
161
Chris Lattnera45664f2008-11-10 02:56:27 +0000162/// isVariable - Return true if the specified tag is legal for DIVariable.
Devang Patel6ceea332009-08-31 18:49:10 +0000163bool DIDescriptor::isVariable() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000164 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000165 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000166 case dwarf::DW_TAG_auto_variable:
167 case dwarf::DW_TAG_arg_variable:
168 case dwarf::DW_TAG_return_variable:
169 return true;
170 default:
171 return false;
172 }
173}
174
Devang Patelecbeb1a2009-09-30 22:34:41 +0000175/// isType - Return true if the specified tag is legal for DIType.
176bool DIDescriptor::isType() const {
177 return isBasicType() || isCompositeType() || isDerivedType();
178}
179
Devang Patel6ceea332009-08-31 18:49:10 +0000180/// isSubprogram - Return true if the specified tag is legal for
181/// DISubprogram.
182bool DIDescriptor::isSubprogram() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000183 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
Devang Patel6ceea332009-08-31 18:49:10 +0000184}
185
186/// isGlobalVariable - Return true if the specified tag is legal for
187/// DIGlobalVariable.
188bool DIDescriptor::isGlobalVariable() const {
Devang Patel29368072010-08-10 07:11:13 +0000189 return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
190 getTag() == dwarf::DW_TAG_constant);
Devang Patel6ceea332009-08-31 18:49:10 +0000191}
192
Devang Patelecbeb1a2009-09-30 22:34:41 +0000193/// isGlobal - Return true if the specified tag is legal for DIGlobal.
194bool DIDescriptor::isGlobal() const {
195 return isGlobalVariable();
196}
197
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000198/// isScope - Return true if the specified tag is one of the scope
Devang Patel43d98b32009-08-31 20:44:45 +0000199/// related tag.
200bool DIDescriptor::isScope() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000201 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000202 switch (getTag()) {
203 case dwarf::DW_TAG_compile_unit:
204 case dwarf::DW_TAG_lexical_block:
205 case dwarf::DW_TAG_subprogram:
206 case dwarf::DW_TAG_namespace:
207 return true;
208 default:
209 break;
Devang Patel43d98b32009-08-31 20:44:45 +0000210 }
211 return false;
212}
Devang Patel6ceea332009-08-31 18:49:10 +0000213
Devang Patelc9f322d2009-08-31 21:34:44 +0000214/// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
215bool DIDescriptor::isCompileUnit() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000216 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
Devang Patelc9f322d2009-08-31 21:34:44 +0000217}
218
Devang Patel7aa81892010-03-08 22:27:22 +0000219/// isFile - Return true if the specified tag is DW_TAG_file_type.
220bool DIDescriptor::isFile() const {
221 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
222}
223
Devang Patel6404e4e2009-12-15 19:16:48 +0000224/// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
225bool DIDescriptor::isNameSpace() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000226 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
Devang Patel6404e4e2009-12-15 19:16:48 +0000227}
228
Devang Patel5e005d82009-08-31 22:00:15 +0000229/// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
230bool DIDescriptor::isLexicalBlock() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000231 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block;
Devang Patel5e005d82009-08-31 22:00:15 +0000232}
233
Devang Patelecbeb1a2009-09-30 22:34:41 +0000234/// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
235bool DIDescriptor::isSubrange() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000236 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
Devang Patelecbeb1a2009-09-30 22:34:41 +0000237}
238
239/// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
240bool DIDescriptor::isEnumerator() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000241 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
Devang Patelecbeb1a2009-09-30 22:34:41 +0000242}
243
Devang Patel6ceea332009-08-31 18:49:10 +0000244//===----------------------------------------------------------------------===//
245// Simple Descriptor Constructors and other Methods
246//===----------------------------------------------------------------------===//
247
Devang Patele9f8f5e2010-05-07 20:54:48 +0000248DIType::DIType(const MDNode *N) : DIScope(N) {
Devang Patel6ceea332009-08-31 18:49:10 +0000249 if (!N) return;
250 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
251 DbgNode = 0;
252 }
253}
254
Devang Patel68afdc32009-01-05 18:33:01 +0000255unsigned DIArray::getNumElements() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000256 if (!DbgNode)
257 return 0;
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000258 return DbgNode->getNumOperands();
Devang Patel68afdc32009-01-05 18:33:01 +0000259}
Chris Lattnera45664f2008-11-10 02:56:27 +0000260
Devang Patelc4999d72009-07-22 18:23:44 +0000261/// replaceAllUsesWith - Replace all uses of debug info referenced by
Dan Gohman872814a2010-07-21 18:54:18 +0000262/// this descriptor.
Devang Patelc4999d72009-07-22 18:23:44 +0000263void DIDerivedType::replaceAllUsesWith(DIDescriptor &D) {
Devang Patel3c91b052010-03-08 20:52:55 +0000264 if (!DbgNode)
Devang Patelc4999d72009-07-22 18:23:44 +0000265 return;
266
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000267 // Since we use a TrackingVH for the node, its easy for clients to manufacture
268 // legitimate situations where they want to replaceAllUsesWith() on something
269 // which, due to uniquing, has merged with the source. We shield clients from
270 // this detail by allowing a value to be replaced with replaceAllUsesWith()
271 // itself.
Devang Pateled66bf52010-05-07 18:19:32 +0000272 if (DbgNode != D) {
Devang Patele9f8f5e2010-05-07 20:54:48 +0000273 MDNode *Node = const_cast<MDNode*>(DbgNode);
274 const MDNode *DN = D;
275 const Value *V = cast_or_null<Value>(DN);
276 Node->replaceAllUsesWith(const_cast<Value*>(V));
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000277 }
Devang Patelc4999d72009-07-22 18:23:44 +0000278}
279
Devang Patelb79b5352009-01-19 23:21:49 +0000280/// Verify - Verify that a compile unit is well formed.
281bool DICompileUnit::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000282 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000283 return false;
Devang Patel65dbc902009-11-25 17:36:49 +0000284 StringRef N = getFilename();
285 if (N.empty())
Bill Wendling0582ae92009-03-13 04:39:26 +0000286 return false;
Devang Patelb79b5352009-01-19 23:21:49 +0000287 // It is possible that directory and produce string is empty.
Bill Wendling0582ae92009-03-13 04:39:26 +0000288 return true;
Devang Patelb79b5352009-01-19 23:21:49 +0000289}
290
291/// Verify - Verify that a type descriptor is well formed.
292bool DIType::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000293 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000294 return false;
Devang Patel3c91b052010-03-08 20:52:55 +0000295 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000296 return false;
297
298 DICompileUnit CU = getCompileUnit();
Devang Patel3c91b052010-03-08 20:52:55 +0000299 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000300 return false;
301 return true;
302}
303
304/// Verify - Verify that a composite type descriptor is well formed.
305bool DICompositeType::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000306 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000307 return false;
Devang Patel3c91b052010-03-08 20:52:55 +0000308 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000309 return false;
310
311 DICompileUnit CU = getCompileUnit();
Devang Patel3c91b052010-03-08 20:52:55 +0000312 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000313 return false;
314 return true;
315}
316
317/// Verify - Verify that a subprogram descriptor is well formed.
318bool DISubprogram::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000319 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000320 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000321
Devang Patel3c91b052010-03-08 20:52:55 +0000322 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000323 return false;
324
325 DICompileUnit CU = getCompileUnit();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000326 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000327 return false;
328
329 DICompositeType Ty = getType();
Devang Patel3c91b052010-03-08 20:52:55 +0000330 if (!Ty.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000331 return false;
332 return true;
333}
334
335/// Verify - Verify that a global variable descriptor is well formed.
336bool DIGlobalVariable::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000337 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000338 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000339
Devang Patel65dbc902009-11-25 17:36:49 +0000340 if (getDisplayName().empty())
Devang Patel84c73e92009-11-06 17:58:12 +0000341 return false;
342
Devang Patel3c91b052010-03-08 20:52:55 +0000343 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000344 return false;
345
346 DICompileUnit CU = getCompileUnit();
Devang Patel3c91b052010-03-08 20:52:55 +0000347 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000348 return false;
349
350 DIType Ty = getType();
351 if (!Ty.Verify())
352 return false;
353
Devang Patel27398962010-08-09 21:39:24 +0000354 if (!getGlobal() && !getConstant())
Devang Patelb79b5352009-01-19 23:21:49 +0000355 return false;
356
357 return true;
358}
359
360/// Verify - Verify that a variable descriptor is well formed.
361bool DIVariable::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000362 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000363 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000364
Devang Patel3c91b052010-03-08 20:52:55 +0000365 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000366 return false;
367
Devang Patel62077af2010-05-07 21:42:24 +0000368 if (!getCompileUnit().Verify())
369 return false;
370
Devang Patelb79b5352009-01-19 23:21:49 +0000371 DIType Ty = getType();
372 if (!Ty.Verify())
373 return false;
374
Devang Patelb79b5352009-01-19 23:21:49 +0000375 return true;
376}
377
Devang Patel3c91b052010-03-08 20:52:55 +0000378/// Verify - Verify that a location descriptor is well formed.
379bool DILocation::Verify() const {
380 if (!DbgNode)
381 return false;
Jim Grosbache62b6902010-07-21 21:36:25 +0000382
Devang Patel3c91b052010-03-08 20:52:55 +0000383 return DbgNode->getNumOperands() == 4;
384}
385
Devang Patel47e22652010-05-07 23:04:32 +0000386/// Verify - Verify that a namespace descriptor is well formed.
387bool DINameSpace::Verify() const {
388 if (!DbgNode)
389 return false;
390 if (getName().empty())
391 return false;
392 if (!getCompileUnit().Verify())
393 return false;
394 return true;
395}
396
Devang Patel36375ee2009-02-17 21:23:59 +0000397/// getOriginalTypeSize - If this type is derived from a base type then
398/// return base type size.
399uint64_t DIDerivedType::getOriginalTypeSize() const {
Devang Patel61ecbd12009-11-04 23:48:00 +0000400 unsigned Tag = getTag();
401 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
402 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
403 Tag == dwarf::DW_TAG_restrict_type) {
404 DIType BaseType = getTypeDerivedFrom();
Jim Grosbache62b6902010-07-21 21:36:25 +0000405 // If this type is not derived from any type then take conservative
Devang Patel5ebfa2d2009-11-06 18:24:05 +0000406 // approach.
Devang Patel3c91b052010-03-08 20:52:55 +0000407 if (!BaseType.isValid())
Devang Patel5ebfa2d2009-11-06 18:24:05 +0000408 return getSizeInBits();
Devang Patel61ecbd12009-11-04 23:48:00 +0000409 if (BaseType.isDerivedType())
Devang Patel2db49d72010-05-07 18:11:54 +0000410 return DIDerivedType(BaseType).getOriginalTypeSize();
Devang Patel61ecbd12009-11-04 23:48:00 +0000411 else
412 return BaseType.getSizeInBits();
413 }
Jim Grosbache62b6902010-07-21 21:36:25 +0000414
Devang Patel61ecbd12009-11-04 23:48:00 +0000415 return getSizeInBits();
Devang Patel36375ee2009-02-17 21:23:59 +0000416}
Devang Patelb79b5352009-01-19 23:21:49 +0000417
Jim Grosbache62b6902010-07-21 21:36:25 +0000418/// isInlinedFnArgument - Return true if this variable provides debugging
Devang Patel719f6a92010-04-29 20:40:36 +0000419/// information for an inlined function arguments.
420bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
421 assert(CurFn && "Invalid function");
422 if (!getContext().isSubprogram())
423 return false;
Jim Grosbache62b6902010-07-21 21:36:25 +0000424 // This variable is not inlined function argument if its scope
Devang Patel719f6a92010-04-29 20:40:36 +0000425 // does not describe current function.
Devang Patel2db49d72010-05-07 18:11:54 +0000426 return !(DISubprogram(getContext()).describes(CurFn));
Devang Patel719f6a92010-04-29 20:40:36 +0000427}
428
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000429/// describes - Return true if this subprogram provides debugging
430/// information for the function F.
431bool DISubprogram::describes(const Function *F) {
Chris Lattner099b7792009-12-29 09:22:47 +0000432 assert(F && "Invalid function");
Devang Patelffd33cd2010-06-16 06:42:02 +0000433 if (F == getFunction())
434 return true;
Devang Patel65dbc902009-11-25 17:36:49 +0000435 StringRef Name = getLinkageName();
436 if (Name.empty())
Devang Patel5ccdd102009-09-29 18:40:58 +0000437 Name = getName();
Devang Patel65dbc902009-11-25 17:36:49 +0000438 if (F->getName() == Name)
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000439 return true;
440 return false;
441}
442
Jim Grosbache62b6902010-07-21 21:36:25 +0000443unsigned DISubprogram::isOptimized() const {
Devang Patelccff8122010-04-30 19:38:23 +0000444 assert (DbgNode && "Invalid subprogram descriptor!");
445 if (DbgNode->getNumOperands() == 16)
446 return getUnsignedField(15);
447 return 0;
448}
449
Devang Patel65dbc902009-11-25 17:36:49 +0000450StringRef DIScope::getFilename() const {
Devang Patel77bf2952010-03-08 22:02:50 +0000451 if (!DbgNode)
452 return StringRef();
Jim Grosbache62b6902010-07-21 21:36:25 +0000453 if (isLexicalBlock())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000454 return DILexicalBlock(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000455 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000456 return DISubprogram(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000457 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000458 return DICompileUnit(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000459 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000460 return DINameSpace(DbgNode).getFilename();
Devang Patel77bf2952010-03-08 22:02:50 +0000461 if (isType())
462 return DIType(DbgNode).getFilename();
Devang Patel7aa81892010-03-08 22:27:22 +0000463 if (isFile())
464 return DIFile(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000465 assert(0 && "Invalid DIScope!");
Devang Patel65dbc902009-11-25 17:36:49 +0000466 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000467}
468
Devang Patel65dbc902009-11-25 17:36:49 +0000469StringRef DIScope::getDirectory() const {
Devang Patel77bf2952010-03-08 22:02:50 +0000470 if (!DbgNode)
471 return StringRef();
Jim Grosbache62b6902010-07-21 21:36:25 +0000472 if (isLexicalBlock())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000473 return DILexicalBlock(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000474 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000475 return DISubprogram(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000476 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000477 return DICompileUnit(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000478 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000479 return DINameSpace(DbgNode).getDirectory();
Devang Patel77bf2952010-03-08 22:02:50 +0000480 if (isType())
481 return DIType(DbgNode).getDirectory();
Devang Patel7aa81892010-03-08 22:27:22 +0000482 if (isFile())
483 return DIFile(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000484 assert(0 && "Invalid DIScope!");
Devang Patel65dbc902009-11-25 17:36:49 +0000485 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000486}
487
Chris Lattnera45664f2008-11-10 02:56:27 +0000488//===----------------------------------------------------------------------===//
Devang Patel7136a652009-07-01 22:10:23 +0000489// DIDescriptor: dump routines for all descriptors.
490//===----------------------------------------------------------------------===//
491
492
Dan Gohman50404362010-05-07 15:30:29 +0000493/// print - Print descriptor.
494void DIDescriptor::print(raw_ostream &OS) const {
495 OS << "[" << dwarf::TagString(getTag()) << "] ";
496 OS.write_hex((intptr_t) &*DbgNode) << ']';
Devang Patel7136a652009-07-01 22:10:23 +0000497}
498
Dan Gohman50404362010-05-07 15:30:29 +0000499/// print - Print compile unit.
500void DICompileUnit::print(raw_ostream &OS) const {
Devang Patel7136a652009-07-01 22:10:23 +0000501 if (getLanguage())
Dan Gohman50404362010-05-07 15:30:29 +0000502 OS << " [" << dwarf::LanguageString(getLanguage()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000503
Dan Gohmanfaa19c32010-05-10 20:07:44 +0000504 OS << " [" << getDirectory() << "/" << getFilename() << "]";
Devang Patel7136a652009-07-01 22:10:23 +0000505}
506
Dan Gohman50404362010-05-07 15:30:29 +0000507/// print - Print type.
508void DIType::print(raw_ostream &OS) const {
Devang Patel3c91b052010-03-08 20:52:55 +0000509 if (!DbgNode) return;
Devang Patel7136a652009-07-01 22:10:23 +0000510
Devang Patel65dbc902009-11-25 17:36:49 +0000511 StringRef Res = getName();
512 if (!Res.empty())
Dan Gohman50404362010-05-07 15:30:29 +0000513 OS << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000514
515 unsigned Tag = getTag();
Dan Gohman50404362010-05-07 15:30:29 +0000516 OS << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000517
518 // TODO : Print context
Dan Gohmanc014d092010-05-07 16:17:22 +0000519 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000520 OS << " ["
Dan Gohman9a7063e2010-05-07 16:39:27 +0000521 << "line " << getLineNumber() << ", "
522 << getSizeInBits() << " bits, "
523 << getAlignInBits() << " bit alignment, "
524 << getOffsetInBits() << " bit offset"
Chris Lattnera81d29b2009-08-23 07:33:14 +0000525 << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000526
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000527 if (isPrivate())
Dan Gohman50404362010-05-07 15:30:29 +0000528 OS << " [private] ";
Devang Patel7136a652009-07-01 22:10:23 +0000529 else if (isProtected())
Dan Gohman50404362010-05-07 15:30:29 +0000530 OS << " [protected] ";
Devang Patel7136a652009-07-01 22:10:23 +0000531
532 if (isForwardDecl())
Dan Gohman50404362010-05-07 15:30:29 +0000533 OS << " [fwd] ";
Devang Patel7136a652009-07-01 22:10:23 +0000534
Devang Patel6ceea332009-08-31 18:49:10 +0000535 if (isBasicType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000536 DIBasicType(DbgNode).print(OS);
Devang Patel6ceea332009-08-31 18:49:10 +0000537 else if (isDerivedType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000538 DIDerivedType(DbgNode).print(OS);
Devang Patel6ceea332009-08-31 18:49:10 +0000539 else if (isCompositeType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000540 DICompositeType(DbgNode).print(OS);
Devang Patel7136a652009-07-01 22:10:23 +0000541 else {
Dan Gohman50404362010-05-07 15:30:29 +0000542 OS << "Invalid DIType\n";
Devang Patel7136a652009-07-01 22:10:23 +0000543 return;
544 }
545
Dan Gohman50404362010-05-07 15:30:29 +0000546 OS << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000547}
548
Dan Gohman50404362010-05-07 15:30:29 +0000549/// print - Print basic type.
550void DIBasicType::print(raw_ostream &OS) const {
551 OS << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000552}
553
Dan Gohman50404362010-05-07 15:30:29 +0000554/// print - Print derived type.
555void DIDerivedType::print(raw_ostream &OS) const {
Dan Gohmanc014d092010-05-07 16:17:22 +0000556 OS << "\n\t Derived From: "; getTypeDerivedFrom().print(OS);
Devang Patel7136a652009-07-01 22:10:23 +0000557}
558
Dan Gohman50404362010-05-07 15:30:29 +0000559/// print - Print composite type.
560void DICompositeType::print(raw_ostream &OS) const {
Devang Patel7136a652009-07-01 22:10:23 +0000561 DIArray A = getTypeArray();
Dan Gohman50404362010-05-07 15:30:29 +0000562 OS << " [" << A.getNumElements() << " elements]";
Devang Patel7136a652009-07-01 22:10:23 +0000563}
564
Dan Gohman50404362010-05-07 15:30:29 +0000565/// print - Print subprogram.
566void DISubprogram::print(raw_ostream &OS) const {
Devang Patel65dbc902009-11-25 17:36:49 +0000567 StringRef Res = getName();
568 if (!Res.empty())
Dan Gohman50404362010-05-07 15:30:29 +0000569 OS << " [" << Res << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000570
571 unsigned Tag = getTag();
Dan Gohman50404362010-05-07 15:30:29 +0000572 OS << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000573
574 // TODO : Print context
Dan Gohmanc014d092010-05-07 16:17:22 +0000575 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000576 OS << " [" << getLineNumber() << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000577
578 if (isLocalToUnit())
Dan Gohman50404362010-05-07 15:30:29 +0000579 OS << " [local] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000580
581 if (isDefinition())
Dan Gohman50404362010-05-07 15:30:29 +0000582 OS << " [def] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000583
Dan Gohman50404362010-05-07 15:30:29 +0000584 OS << "\n";
585}
586
587/// print - Print global variable.
588void DIGlobalVariable::print(raw_ostream &OS) const {
589 OS << " [";
Devang Patela49d8772010-05-07 23:19:07 +0000590 StringRef Res = getName();
591 if (!Res.empty())
592 OS << " [" << Res << "] ";
593
594 unsigned Tag = getTag();
595 OS << " [" << dwarf::TagString(Tag) << "] ";
596
597 // TODO : Print context
598 getCompileUnit().print(OS);
599 OS << " [" << getLineNumber() << "] ";
600
601 if (isLocalToUnit())
602 OS << " [local] ";
603
604 if (isDefinition())
605 OS << " [def] ";
606
607 if (isGlobalVariable())
608 DIGlobalVariable(DbgNode).print(OS);
609 OS << "]\n";
Dan Gohman50404362010-05-07 15:30:29 +0000610}
611
612/// print - Print variable.
613void DIVariable::print(raw_ostream &OS) const {
614 StringRef Res = getName();
615 if (!Res.empty())
616 OS << " [" << Res << "] ";
617
Dan Gohmanc014d092010-05-07 16:17:22 +0000618 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000619 OS << " [" << getLineNumber() << "] ";
Dan Gohmanc014d092010-05-07 16:17:22 +0000620 getType().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000621 OS << "\n";
622
623 // FIXME: Dump complex addresses
624}
625
626/// dump - Print descriptor to dbgs() with a newline.
627void DIDescriptor::dump() const {
628 print(dbgs()); dbgs() << '\n';
629}
630
631/// dump - Print compile unit to dbgs() with a newline.
632void DICompileUnit::dump() const {
633 print(dbgs()); dbgs() << '\n';
634}
635
636/// dump - Print type to dbgs() with a newline.
637void DIType::dump() const {
638 print(dbgs()); dbgs() << '\n';
639}
640
641/// dump - Print basic type to dbgs() with a newline.
642void DIBasicType::dump() const {
643 print(dbgs()); dbgs() << '\n';
644}
645
646/// dump - Print derived type to dbgs() with a newline.
647void DIDerivedType::dump() const {
648 print(dbgs()); dbgs() << '\n';
649}
650
651/// dump - Print composite type to dbgs() with a newline.
652void DICompositeType::dump() const {
653 print(dbgs()); dbgs() << '\n';
654}
655
Dan Gohman50404362010-05-07 15:30:29 +0000656/// dump - Print subprogram to dbgs() with a newline.
657void DISubprogram::dump() const {
658 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000659}
660
661/// dump - Print global variable.
662void DIGlobalVariable::dump() const {
Dan Gohman50404362010-05-07 15:30:29 +0000663 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000664}
665
666/// dump - Print variable.
667void DIVariable::dump() const {
Dan Gohman50404362010-05-07 15:30:29 +0000668 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000669}
670
671//===----------------------------------------------------------------------===//
Chris Lattnera45664f2008-11-10 02:56:27 +0000672// DIFactory: Basic Helpers
673//===----------------------------------------------------------------------===//
674
Bill Wendlingdc817b62009-05-14 18:26:15 +0000675DIFactory::DIFactory(Module &m)
Victor Hernandez06203122010-01-23 00:03:28 +0000676 : M(m), VMContext(M.getContext()), DeclareFn(0), ValueFn(0) {}
Chris Lattner497a7a82008-11-10 04:10:34 +0000677
Chris Lattnera45664f2008-11-10 02:56:27 +0000678Constant *DIFactory::GetTagConstant(unsigned TAG) {
Devang Patel6906ba52009-01-20 19:22:03 +0000679 assert((TAG & LLVMDebugVersionMask) == 0 &&
Chris Lattnera45664f2008-11-10 02:56:27 +0000680 "Tag too large for debug encoding!");
Owen Anderson1d0be152009-08-13 21:58:54 +0000681 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
Chris Lattnera45664f2008-11-10 02:56:27 +0000682}
683
Chris Lattnera45664f2008-11-10 02:56:27 +0000684//===----------------------------------------------------------------------===//
685// DIFactory: Primary Constructors
686//===----------------------------------------------------------------------===//
687
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000688/// GetOrCreateArray - Create an descriptor for an array of descriptors.
Chris Lattnera45664f2008-11-10 02:56:27 +0000689/// This implicitly uniques the arrays created.
690DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
Devang Patele4b27562009-08-28 23:24:31 +0000691 SmallVector<Value*, 16> Elts;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000692
Devang Patele4b27562009-08-28 23:24:31 +0000693 if (NumTys == 0)
694 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)));
695 else
696 for (unsigned i = 0; i != NumTys; ++i)
Devang Patel2db49d72010-05-07 18:11:54 +0000697 Elts.push_back(Tys[i]);
Devang Patel82459882009-08-26 05:01:18 +0000698
Devang Patele4b27562009-08-28 23:24:31 +0000699 return DIArray(MDNode::get(VMContext,Elts.data(), Elts.size()));
Chris Lattnera45664f2008-11-10 02:56:27 +0000700}
701
702/// GetOrCreateSubrange - Create a descriptor for a value range. This
703/// implicitly uniques the values returned.
704DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
Devang Patele4b27562009-08-28 23:24:31 +0000705 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000706 GetTagConstant(dwarf::DW_TAG_subrange_type),
Owen Anderson1d0be152009-08-13 21:58:54 +0000707 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
708 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
Chris Lattnera45664f2008-11-10 02:56:27 +0000709 };
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000710
Devang Patele4b27562009-08-28 23:24:31 +0000711 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000712}
713
714
715
716/// CreateCompileUnit - Create a new descriptor for the specified compile
717/// unit. Note that this does not unique compile units within the module.
718DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
Devang Patel65dbc902009-11-25 17:36:49 +0000719 StringRef Filename,
720 StringRef Directory,
721 StringRef Producer,
Devang Pateldd9db662009-01-30 18:20:31 +0000722 bool isMain,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000723 bool isOptimized,
Devang Patel65dbc902009-11-25 17:36:49 +0000724 StringRef Flags,
Devang Patel13319ce2009-02-17 22:43:44 +0000725 unsigned RunTimeVer) {
Devang Patele4b27562009-08-28 23:24:31 +0000726 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000727 GetTagConstant(dwarf::DW_TAG_compile_unit),
Devang Patele4b27562009-08-28 23:24:31 +0000728 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Owen Anderson1d0be152009-08-13 21:58:54 +0000729 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
Devang Patele4b27562009-08-28 23:24:31 +0000730 MDString::get(VMContext, Filename),
731 MDString::get(VMContext, Directory),
732 MDString::get(VMContext, Producer),
Owen Anderson1d0be152009-08-13 21:58:54 +0000733 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
734 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
Devang Patele4b27562009-08-28 23:24:31 +0000735 MDString::get(VMContext, Flags),
Owen Anderson1d0be152009-08-13 21:58:54 +0000736 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
Chris Lattnera45664f2008-11-10 02:56:27 +0000737 };
Devang Patele4b27562009-08-28 23:24:31 +0000738
739 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000740}
741
Devang Patel7aa81892010-03-08 22:27:22 +0000742/// CreateFile - Create a new descriptor for the specified file.
743DIFile DIFactory::CreateFile(StringRef Filename,
744 StringRef Directory,
745 DICompileUnit CU) {
746 Value *Elts[] = {
747 GetTagConstant(dwarf::DW_TAG_file_type),
748 MDString::get(VMContext, Filename),
749 MDString::get(VMContext, Directory),
Devang Patel2db49d72010-05-07 18:11:54 +0000750 CU
Devang Patel7aa81892010-03-08 22:27:22 +0000751 };
752
753 return DIFile(MDNode::get(VMContext, &Elts[0], 4));
754}
755
Chris Lattnera45664f2008-11-10 02:56:27 +0000756/// CreateEnumerator - Create a single enumerator value.
Devang Patel65dbc902009-11-25 17:36:49 +0000757DIEnumerator DIFactory::CreateEnumerator(StringRef Name, uint64_t Val){
Devang Patele4b27562009-08-28 23:24:31 +0000758 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000759 GetTagConstant(dwarf::DW_TAG_enumerator),
Devang Patele4b27562009-08-28 23:24:31 +0000760 MDString::get(VMContext, Name),
Owen Anderson1d0be152009-08-13 21:58:54 +0000761 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
Chris Lattnera45664f2008-11-10 02:56:27 +0000762 };
Devang Patele4b27562009-08-28 23:24:31 +0000763 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000764}
765
766
767/// CreateBasicType - Create a basic type like int, float, etc.
768DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000769 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000770 DIFile F,
Chris Lattnera45664f2008-11-10 02:56:27 +0000771 unsigned LineNumber,
772 uint64_t SizeInBits,
773 uint64_t AlignInBits,
774 uint64_t OffsetInBits, unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000775 unsigned Encoding) {
Devang Patele4b27562009-08-28 23:24:31 +0000776 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000777 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patel2db49d72010-05-07 18:11:54 +0000778 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000779 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000780 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000781 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
782 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
783 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
784 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
785 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
786 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
Chris Lattnera45664f2008-11-10 02:56:27 +0000787 };
Devang Patele4b27562009-08-28 23:24:31 +0000788 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000789}
790
Devang Patelac16d442009-10-26 16:54:35 +0000791
792/// CreateBasicType - Create a basic type like int, float, etc.
793DIBasicType DIFactory::CreateBasicTypeEx(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000794 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000795 DIFile F,
Devang Patelac16d442009-10-26 16:54:35 +0000796 unsigned LineNumber,
797 Constant *SizeInBits,
798 Constant *AlignInBits,
799 Constant *OffsetInBits, unsigned Flags,
800 unsigned Encoding) {
801 Value *Elts[] = {
802 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patel2db49d72010-05-07 18:11:54 +0000803 Context,
Devang Patelac16d442009-10-26 16:54:35 +0000804 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000805 F,
Devang Patelac16d442009-10-26 16:54:35 +0000806 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
807 SizeInBits,
808 AlignInBits,
809 OffsetInBits,
810 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
811 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
812 };
813 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
814}
815
Devang Patelb4645642010-02-06 01:02:37 +0000816/// CreateArtificialType - Create a new DIType with "artificial" flag set.
817DIType DIFactory::CreateArtificialType(DIType Ty) {
818 if (Ty.isArtificial())
819 return Ty;
820
821 SmallVector<Value *, 9> Elts;
Devang Patel2db49d72010-05-07 18:11:54 +0000822 MDNode *N = Ty;
Devang Patelb4645642010-02-06 01:02:37 +0000823 assert (N && "Unexpected input DIType!");
824 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
825 if (Value *V = N->getOperand(i))
826 Elts.push_back(V);
827 else
828 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
829 }
830
831 unsigned CurFlags = Ty.getFlags();
832 CurFlags = CurFlags | DIType::FlagArtificial;
833
834 // Flags are stored at this slot.
835 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
836
837 return DIType(MDNode::get(VMContext, Elts.data(), Elts.size()));
838}
Devang Patelac16d442009-10-26 16:54:35 +0000839
Chris Lattnera45664f2008-11-10 02:56:27 +0000840/// CreateDerivedType - Create a derived type like const qualified type,
841/// pointer, typedef, etc.
842DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
843 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000844 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000845 DIFile F,
Chris Lattnera45664f2008-11-10 02:56:27 +0000846 unsigned LineNumber,
847 uint64_t SizeInBits,
848 uint64_t AlignInBits,
849 uint64_t OffsetInBits,
850 unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000851 DIType DerivedFrom) {
Devang Patele4b27562009-08-28 23:24:31 +0000852 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000853 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000854 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000855 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000856 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000857 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
858 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
859 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
860 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
861 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000862 DerivedFrom,
Chris Lattnera45664f2008-11-10 02:56:27 +0000863 };
Devang Patele4b27562009-08-28 23:24:31 +0000864 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000865}
866
Devang Patelac16d442009-10-26 16:54:35 +0000867
868/// CreateDerivedType - Create a derived type like const qualified type,
869/// pointer, typedef, etc.
870DIDerivedType DIFactory::CreateDerivedTypeEx(unsigned Tag,
871 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000872 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000873 DIFile F,
Devang Patelac16d442009-10-26 16:54:35 +0000874 unsigned LineNumber,
875 Constant *SizeInBits,
876 Constant *AlignInBits,
877 Constant *OffsetInBits,
878 unsigned Flags,
879 DIType DerivedFrom) {
880 Value *Elts[] = {
881 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000882 Context,
Devang Patelac16d442009-10-26 16:54:35 +0000883 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000884 F,
Devang Patelac16d442009-10-26 16:54:35 +0000885 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
886 SizeInBits,
887 AlignInBits,
888 OffsetInBits,
889 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000890 DerivedFrom,
Devang Patelac16d442009-10-26 16:54:35 +0000891 };
892 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
893}
894
895
Chris Lattnera45664f2008-11-10 02:56:27 +0000896/// CreateCompositeType - Create a composite type like array, struct, etc.
897DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
898 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000899 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000900 DIFile F,
Chris Lattnera45664f2008-11-10 02:56:27 +0000901 unsigned LineNumber,
902 uint64_t SizeInBits,
903 uint64_t AlignInBits,
904 uint64_t OffsetInBits,
905 unsigned Flags,
906 DIType DerivedFrom,
Devang Patel13319ce2009-02-17 22:43:44 +0000907 DIArray Elements,
Devang Patel0fd7f9d2010-01-26 21:14:59 +0000908 unsigned RuntimeLang,
909 MDNode *ContainingType) {
Owen Andersone277fed2009-07-07 16:31:25 +0000910
Devang Patele4b27562009-08-28 23:24:31 +0000911 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000912 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000913 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000914 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000915 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000916 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
917 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
918 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
919 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
920 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000921 DerivedFrom,
922 Elements,
Devang Patel0fd7f9d2010-01-26 21:14:59 +0000923 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
924 ContainingType
Chris Lattnera45664f2008-11-10 02:56:27 +0000925 };
Devang Patele7e5a0f2010-08-10 20:01:20 +0000926
927 MDNode *Node = MDNode::get(VMContext, &Elts[0], 13);
928 // Create a named metadata so that we do not lose this enum info.
929 if (Tag == dwarf::DW_TAG_enumeration_type) {
930 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.enum");
931 NMD->addOperand(Node);
932 }
933 return DICompositeType(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +0000934}
935
936
Devang Patelac16d442009-10-26 16:54:35 +0000937/// CreateCompositeType - Create a composite type like array, struct, etc.
938DICompositeType DIFactory::CreateCompositeTypeEx(unsigned Tag,
939 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000940 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000941 DIFile F,
Devang Patelac16d442009-10-26 16:54:35 +0000942 unsigned LineNumber,
943 Constant *SizeInBits,
944 Constant *AlignInBits,
945 Constant *OffsetInBits,
946 unsigned Flags,
947 DIType DerivedFrom,
948 DIArray Elements,
Devang Patel6bf058c2010-08-10 20:22:49 +0000949 unsigned RuntimeLang,
950 MDNode *ContainingType) {
Devang Patelac16d442009-10-26 16:54:35 +0000951 Value *Elts[] = {
952 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000953 Context,
Devang Patelac16d442009-10-26 16:54:35 +0000954 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000955 F,
Devang Patelac16d442009-10-26 16:54:35 +0000956 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
957 SizeInBits,
958 AlignInBits,
959 OffsetInBits,
960 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000961 DerivedFrom,
962 Elements,
Devang Patel6bf058c2010-08-10 20:22:49 +0000963 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
964 ContainingType
Devang Patelac16d442009-10-26 16:54:35 +0000965 };
Devang Patel6bf058c2010-08-10 20:22:49 +0000966 MDNode *Node = MDNode::get(VMContext, &Elts[0], 13);
Devang Patele7e5a0f2010-08-10 20:01:20 +0000967 // Create a named metadata so that we do not lose this enum info.
968 if (Tag == dwarf::DW_TAG_enumeration_type) {
969 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.enum");
970 NMD->addOperand(Node);
971 }
972 return DICompositeType(Node);
Devang Patelac16d442009-10-26 16:54:35 +0000973}
974
975
Chris Lattnera45664f2008-11-10 02:56:27 +0000976/// CreateSubprogram - Create a new descriptor for the specified subprogram.
977/// See comments in DISubprogram for descriptions of these fields. This
978/// method does not unique the generated descriptors.
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000979DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000980 StringRef Name,
981 StringRef DisplayName,
982 StringRef LinkageName,
Devang Patel4b945502010-03-09 00:44:10 +0000983 DIFile F,
Devang Patel2e369932010-01-23 00:26:28 +0000984 unsigned LineNo, DIType Ty,
Chris Lattnera45664f2008-11-10 02:56:27 +0000985 bool isLocalToUnit,
Devang Patel5d11eb02009-12-03 19:11:07 +0000986 bool isDefinition,
987 unsigned VK, unsigned VIndex,
Devang Patel4e0d19d2010-02-03 19:57:19 +0000988 DIType ContainingType,
Devang Patelccff8122010-04-30 19:38:23 +0000989 bool isArtificial,
Stuart Hastings215aa152010-06-11 20:08:44 +0000990 bool isOptimized,
991 Function *Fn) {
Devang Patel854967e2008-12-17 22:39:29 +0000992
Devang Patele4b27562009-08-28 23:24:31 +0000993 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000994 GetTagConstant(dwarf::DW_TAG_subprogram),
Devang Patele4b27562009-08-28 23:24:31 +0000995 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Devang Patel2db49d72010-05-07 18:11:54 +0000996 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000997 MDString::get(VMContext, Name),
998 MDString::get(VMContext, DisplayName),
999 MDString::get(VMContext, LinkageName),
Devang Patel2db49d72010-05-07 18:11:54 +00001000 F,
Owen Anderson1d0be152009-08-13 21:58:54 +00001001 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001002 Ty,
Owen Anderson1d0be152009-08-13 21:58:54 +00001003 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
Devang Patel5d11eb02009-12-03 19:11:07 +00001004 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1005 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
1006 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
Devang Patel2db49d72010-05-07 18:11:54 +00001007 ContainingType,
Devang Patelccff8122010-04-30 19:38:23 +00001008 ConstantInt::get(Type::getInt1Ty(VMContext), isArtificial),
Stuart Hastings215aa152010-06-11 20:08:44 +00001009 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
1010 Fn
Chris Lattnera45664f2008-11-10 02:56:27 +00001011 };
Devang Patelfd5fdc32010-06-28 05:53:08 +00001012 MDNode *Node = MDNode::get(VMContext, &Elts[0], 17);
1013
1014 // Create a named metadata so that we do not lose this mdnode.
1015 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
1016 NMD->addOperand(Node);
1017 return DISubprogram(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +00001018}
1019
Devang Patele3a18de2009-12-01 23:09:02 +00001020/// CreateSubprogramDefinition - Create new subprogram descriptor for the
Jim Grosbache62b6902010-07-21 21:36:25 +00001021/// given declaration.
1022DISubprogram DIFactory::CreateSubprogramDefinition(DISubprogram &SPDeclaration){
Devang Patele3a18de2009-12-01 23:09:02 +00001023 if (SPDeclaration.isDefinition())
Devang Patel2db49d72010-05-07 18:11:54 +00001024 return DISubprogram(SPDeclaration);
Devang Patele3a18de2009-12-01 23:09:02 +00001025
Devang Patel2db49d72010-05-07 18:11:54 +00001026 MDNode *DeclNode = SPDeclaration;
Devang Patele3a18de2009-12-01 23:09:02 +00001027 Value *Elts[] = {
1028 GetTagConstant(dwarf::DW_TAG_subprogram),
1029 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001030 DeclNode->getOperand(2), // Context
1031 DeclNode->getOperand(3), // Name
1032 DeclNode->getOperand(4), // DisplayName
1033 DeclNode->getOperand(5), // LinkageName
1034 DeclNode->getOperand(6), // CompileUnit
1035 DeclNode->getOperand(7), // LineNo
1036 DeclNode->getOperand(8), // Type
1037 DeclNode->getOperand(9), // isLocalToUnit
Stuart Hastings6d56b9f2010-06-05 00:39:29 +00001038 ConstantInt::get(Type::getInt1Ty(VMContext), true),
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001039 DeclNode->getOperand(11), // Virtuality
1040 DeclNode->getOperand(12), // VIndex
Devang Patel4e0d19d2010-02-03 19:57:19 +00001041 DeclNode->getOperand(13), // Containting Type
Devang Patelccff8122010-04-30 19:38:23 +00001042 DeclNode->getOperand(14), // isArtificial
Devang Patelacc6efa2010-06-27 21:04:31 +00001043 DeclNode->getOperand(15), // isOptimized
1044 SPDeclaration.getFunction()
Devang Patele3a18de2009-12-01 23:09:02 +00001045 };
Devang Patelfd5fdc32010-06-28 05:53:08 +00001046 MDNode *Node =MDNode::get(VMContext, &Elts[0], 16);
1047
1048 // Create a named metadata so that we do not lose this mdnode.
1049 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
1050 NMD->addOperand(Node);
1051 return DISubprogram(Node);
Devang Patele3a18de2009-12-01 23:09:02 +00001052}
1053
Chris Lattnera45664f2008-11-10 02:56:27 +00001054/// CreateGlobalVariable - Create a new descriptor for the specified global.
1055DIGlobalVariable
Devang Patel65dbc902009-11-25 17:36:49 +00001056DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
1057 StringRef DisplayName,
1058 StringRef LinkageName,
Devang Patel4b945502010-03-09 00:44:10 +00001059 DIFile F,
Devang Patel2e369932010-01-23 00:26:28 +00001060 unsigned LineNo, DIType Ty,bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +00001061 bool isDefinition, llvm::GlobalVariable *Val) {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001062 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001063 GetTagConstant(dwarf::DW_TAG_variable),
Devang Patele4b27562009-08-28 23:24:31 +00001064 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Devang Patel2db49d72010-05-07 18:11:54 +00001065 Context,
Devang Patele4b27562009-08-28 23:24:31 +00001066 MDString::get(VMContext, Name),
1067 MDString::get(VMContext, DisplayName),
1068 MDString::get(VMContext, LinkageName),
Devang Patel2db49d72010-05-07 18:11:54 +00001069 F,
Owen Anderson1d0be152009-08-13 21:58:54 +00001070 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001071 Ty,
Owen Anderson1d0be152009-08-13 21:58:54 +00001072 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1073 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
Devang Patele4b27562009-08-28 23:24:31 +00001074 Val
Chris Lattnera45664f2008-11-10 02:56:27 +00001075 };
Devang Patele4b27562009-08-28 23:24:31 +00001076
1077 Value *const *Vs = &Elts[0];
1078 MDNode *Node = MDNode::get(VMContext,Vs, 12);
1079
1080 // Create a named metadata so that we do not lose this mdnode.
1081 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001082 NMD->addOperand(Node);
Devang Patele4b27562009-08-28 23:24:31 +00001083
1084 return DIGlobalVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +00001085}
1086
Devang Patel27398962010-08-09 21:39:24 +00001087/// CreateGlobalVariable - Create a new descriptor for the specified constant.
1088DIGlobalVariable
1089DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
1090 StringRef DisplayName,
1091 StringRef LinkageName,
1092 DIFile F,
1093 unsigned LineNo, DIType Ty,bool isLocalToUnit,
1094 bool isDefinition, llvm::Constant *Val) {
1095 Value *Elts[] = {
Devang Patel29368072010-08-10 07:11:13 +00001096 GetTagConstant(dwarf::DW_TAG_constant),
Devang Patel27398962010-08-09 21:39:24 +00001097 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
1098 Context,
1099 MDString::get(VMContext, Name),
1100 MDString::get(VMContext, DisplayName),
1101 MDString::get(VMContext, LinkageName),
1102 F,
1103 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1104 Ty,
1105 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1106 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1107 Val
1108 };
1109
1110 Value *const *Vs = &Elts[0];
1111 MDNode *Node = MDNode::get(VMContext,Vs, 12);
1112
1113 // Create a named metadata so that we do not lose this mdnode.
1114 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
1115 NMD->addOperand(Node);
1116
1117 return DIGlobalVariable(Node);
1118}
Chris Lattnera45664f2008-11-10 02:56:27 +00001119
1120/// CreateVariable - Create a new descriptor for the specified variable.
1121DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +00001122 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +00001123 DIFile F,
1124 unsigned LineNo,
Devang Patel6ed0ce32010-05-20 20:35:24 +00001125 DIType Ty, bool AlwaysPreserve) {
Devang Patele4b27562009-08-28 23:24:31 +00001126 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001127 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +00001128 Context,
Devang Patele4b27562009-08-28 23:24:31 +00001129 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +00001130 F,
Owen Anderson1d0be152009-08-13 21:58:54 +00001131 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001132 Ty,
Chris Lattnera45664f2008-11-10 02:56:27 +00001133 };
Devang Patel98e1cac2010-05-14 21:01:35 +00001134 MDNode *Node = MDNode::get(VMContext, &Elts[0], 6);
Devang Patel6ed0ce32010-05-20 20:35:24 +00001135 if (AlwaysPreserve) {
1136 // The optimizer may remove local variable. If there is an interest
1137 // to preserve variable info in such situation then stash it in a
1138 // named mdnode.
Devang Patel2f7d5292010-06-16 00:53:55 +00001139 DISubprogram Fn(getDISubprogram(Context));
Devang Patel10de3bb2010-06-21 18:36:58 +00001140 StringRef FName = "fn";
1141 if (Fn.getFunction())
1142 FName = Fn.getFunction()->getName();
Devang Patel10de3bb2010-06-21 18:36:58 +00001143 char One = '\1';
1144 if (FName.startswith(StringRef(&One, 1)))
1145 FName = FName.substr(1);
Dan Gohman17aa92c2010-07-21 23:38:33 +00001146
1147 SmallString<32> Out;
1148 NamedMDNode *FnLocals =
1149 M.getOrInsertNamedMetadata(Twine("llvm.dbg.lv.", FName).toStringRef(Out));
Devang Patel2f7d5292010-06-16 00:53:55 +00001150 FnLocals->addOperand(Node);
Devang Patel98e1cac2010-05-14 21:01:35 +00001151 }
1152 return DIVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +00001153}
1154
1155
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001156/// CreateComplexVariable - Create a new descriptor for the specified variable
1157/// which has a complex address expression for its address.
1158DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
1159 const std::string &Name,
Devang Patel4b945502010-03-09 00:44:10 +00001160 DIFile F,
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001161 unsigned LineNo,
Jim Grosbache62b6902010-07-21 21:36:25 +00001162 DIType Ty,
Devang Patel2e369932010-01-23 00:26:28 +00001163 SmallVector<Value *, 9> &addr) {
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001164 SmallVector<Value *, 9> Elts;
1165 Elts.push_back(GetTagConstant(Tag));
Devang Patel2db49d72010-05-07 18:11:54 +00001166 Elts.push_back(Context);
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001167 Elts.push_back(MDString::get(VMContext, Name));
Devang Patel2db49d72010-05-07 18:11:54 +00001168 Elts.push_back(F);
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001169 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
Devang Patel2db49d72010-05-07 18:11:54 +00001170 Elts.push_back(Ty);
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001171 Elts.insert(Elts.end(), addr.begin(), addr.end());
1172
1173 return DIVariable(MDNode::get(VMContext, &Elts[0], 6+addr.size()));
1174}
1175
1176
Chris Lattnera45664f2008-11-10 02:56:27 +00001177/// CreateBlock - This creates a descriptor for a lexical block with the
Owen Anderson99035272009-07-07 17:12:53 +00001178/// specified parent VMContext.
Devang Patel3d821aa2010-02-16 21:39:34 +00001179DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context,
Stuart Hastings0db42712010-07-19 23:56:30 +00001180 DIFile F, unsigned LineNo,
1181 unsigned Col) {
1182 // Defeat MDNode uniqing for lexical blocks.
1183 static unsigned int unique_id = 0;
Devang Patele4b27562009-08-28 23:24:31 +00001184 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001185 GetTagConstant(dwarf::DW_TAG_lexical_block),
Devang Patel2db49d72010-05-07 18:11:54 +00001186 Context,
Devang Patel3d821aa2010-02-16 21:39:34 +00001187 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Stuart Hastings0db42712010-07-19 23:56:30 +00001188 ConstantInt::get(Type::getInt32Ty(VMContext), Col),
1189 F,
1190 ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
Chris Lattnera45664f2008-11-10 02:56:27 +00001191 };
Stuart Hastings0db42712010-07-19 23:56:30 +00001192 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 6));
Chris Lattnera45664f2008-11-10 02:56:27 +00001193}
1194
Devang Patel6404e4e2009-12-15 19:16:48 +00001195/// CreateNameSpace - This creates new descriptor for a namespace
1196/// with the specified parent context.
1197DINameSpace DIFactory::CreateNameSpace(DIDescriptor Context, StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +00001198 DIFile F,
Devang Patel6404e4e2009-12-15 19:16:48 +00001199 unsigned LineNo) {
1200 Value *Elts[] = {
1201 GetTagConstant(dwarf::DW_TAG_namespace),
Devang Patel2db49d72010-05-07 18:11:54 +00001202 Context,
Devang Patel6404e4e2009-12-15 19:16:48 +00001203 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +00001204 F,
Devang Patel6404e4e2009-12-15 19:16:48 +00001205 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1206 };
1207 return DINameSpace(MDNode::get(VMContext, &Elts[0], 5));
1208}
1209
Devang Patelf98d8fe2009-09-01 01:14:15 +00001210/// CreateLocation - Creates a debug info location.
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001211DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
Daniel Dunbara279bc32009-09-20 02:20:51 +00001212 DIScope S, DILocation OrigLoc) {
Devang Patelf98d8fe2009-09-01 01:14:15 +00001213 Value *Elts[] = {
1214 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1215 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001216 S,
1217 OrigLoc,
Devang Patelf98d8fe2009-09-01 01:14:15 +00001218 };
1219 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1220}
1221
Chris Lattnera45664f2008-11-10 02:56:27 +00001222//===----------------------------------------------------------------------===//
1223// DIFactory: Routines for inserting code into a function
1224//===----------------------------------------------------------------------===//
1225
Chris Lattnera45664f2008-11-10 02:56:27 +00001226/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Devang Patel6daf99b2009-11-10 22:05:35 +00001227Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001228 Instruction *InsertBefore) {
Victor Hernandez4cf292a2010-01-26 02:07:38 +00001229 assert(Storage && "no storage passed to dbg.declare");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001230 assert(D.Verify() && "empty DIVariable passed to dbg.declare");
Chris Lattnera45664f2008-11-10 02:56:27 +00001231 if (!DeclareFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +00001232 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1233
Victor Hernandez756462b2010-01-18 20:42:09 +00001234 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
Devang Patel2db49d72010-05-07 18:11:54 +00001235 D };
Devang Patel6daf99b2009-11-10 22:05:35 +00001236 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
Mike Stumpe4250392009-10-01 22:08:58 +00001237}
1238
1239/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Devang Patel6daf99b2009-11-10 22:05:35 +00001240Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001241 BasicBlock *InsertAtEnd) {
Victor Hernandez4cf292a2010-01-26 02:07:38 +00001242 assert(Storage && "no storage passed to dbg.declare");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001243 assert(D.Verify() && "invalid DIVariable passed to dbg.declare");
Mike Stumpe4250392009-10-01 22:08:58 +00001244 if (!DeclareFn)
1245 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1246
Victor Hernandez756462b2010-01-18 20:42:09 +00001247 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
Devang Patel2db49d72010-05-07 18:11:54 +00001248 D };
Devang Patel27a53de2010-01-29 18:30:57 +00001249
1250 // If this block already has a terminator then insert this intrinsic
1251 // before the terminator.
Jim Grosbache62b6902010-07-21 21:36:25 +00001252 if (TerminatorInst *T = InsertAtEnd->getTerminator())
Devang Patel27a53de2010-01-29 18:30:57 +00001253 return CallInst::Create(DeclareFn, Args, Args+2, "", T);
1254 else
1255 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);}
Torok Edwin620f2802008-12-16 09:07:36 +00001256
Victor Hernandezc59b3352009-12-07 21:54:43 +00001257/// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001258Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
Victor Hernandezc59b3352009-12-07 21:54:43 +00001259 DIVariable D,
1260 Instruction *InsertBefore) {
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001261 assert(V && "no value passed to dbg.value");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001262 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001263 if (!ValueFn)
1264 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1265
Victor Hernandezc8b7cd02010-01-20 05:44:11 +00001266 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001267 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
Devang Patel2db49d72010-05-07 18:11:54 +00001268 D };
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001269 return CallInst::Create(ValueFn, Args, Args+3, "", InsertBefore);
1270}
1271
Victor Hernandezc59b3352009-12-07 21:54:43 +00001272/// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001273Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
Victor Hernandezc59b3352009-12-07 21:54:43 +00001274 DIVariable D,
1275 BasicBlock *InsertAtEnd) {
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001276 assert(V && "no value passed to dbg.value");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001277 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001278 if (!ValueFn)
1279 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1280
Jim Grosbache62b6902010-07-21 21:36:25 +00001281 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001282 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
Devang Patel2db49d72010-05-07 18:11:54 +00001283 D };
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001284 return CallInst::Create(ValueFn, Args, Args+3, "", InsertAtEnd);
1285}
Devang Patele4b27562009-08-28 23:24:31 +00001286
Devang Pateld2f79a12009-07-28 19:55:13 +00001287//===----------------------------------------------------------------------===//
Devang Patel98c65172009-07-30 18:25:15 +00001288// DebugInfoFinder implementations.
Devang Pateld2f79a12009-07-28 19:55:13 +00001289//===----------------------------------------------------------------------===//
1290
Devang Patel98c65172009-07-30 18:25:15 +00001291/// processModule - Process entire module and collect debug info.
1292void DebugInfoFinder::processModule(Module &M) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001293 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1294 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
1295 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
1296 ++BI) {
Devang Patel01c5ff62010-05-04 01:05:02 +00001297 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
Devang Patelb4d31302009-07-31 18:18:52 +00001298 processDeclare(DDI);
Jim Grosbache62b6902010-07-21 21:36:25 +00001299
Chris Lattner28a9bf62010-04-02 20:44:29 +00001300 DebugLoc Loc = BI->getDebugLoc();
1301 if (Loc.isUnknown())
1302 continue;
Jim Grosbache62b6902010-07-21 21:36:25 +00001303
Chris Lattner28a9bf62010-04-02 20:44:29 +00001304 LLVMContext &Ctx = BI->getContext();
1305 DIDescriptor Scope(Loc.getScope(Ctx));
Jim Grosbache62b6902010-07-21 21:36:25 +00001306
Chris Lattner28a9bf62010-04-02 20:44:29 +00001307 if (Scope.isCompileUnit())
Devang Patel2db49d72010-05-07 18:11:54 +00001308 addCompileUnit(DICompileUnit(Scope));
Chris Lattner28a9bf62010-04-02 20:44:29 +00001309 else if (Scope.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001310 processSubprogram(DISubprogram(Scope));
Chris Lattner28a9bf62010-04-02 20:44:29 +00001311 else if (Scope.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001312 processLexicalBlock(DILexicalBlock(Scope));
Jim Grosbache62b6902010-07-21 21:36:25 +00001313
Chris Lattner28a9bf62010-04-02 20:44:29 +00001314 if (MDNode *IA = Loc.getInlinedAt(Ctx))
1315 processLocation(DILocation(IA));
Devang Pateld2f79a12009-07-28 19:55:13 +00001316 }
Devang Patele4b27562009-08-28 23:24:31 +00001317
Devang Patelfd5fdc32010-06-28 05:53:08 +00001318 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
1319 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1320 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
1321 if (addGlobalVariable(DIG)) {
1322 addCompileUnit(DIG.getCompileUnit());
1323 processType(DIG.getType());
1324 }
Devang Pateld2f79a12009-07-28 19:55:13 +00001325 }
1326 }
Devang Patelfd5fdc32010-06-28 05:53:08 +00001327
1328 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
1329 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
1330 processSubprogram(DISubprogram(NMD->getOperand(i)));
Devang Pateld2f79a12009-07-28 19:55:13 +00001331}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001332
Devang Patel6daf99b2009-11-10 22:05:35 +00001333/// processLocation - Process DILocation.
1334void DebugInfoFinder::processLocation(DILocation Loc) {
Devang Patel3c91b052010-03-08 20:52:55 +00001335 if (!Loc.Verify()) return;
Devang Patel2db49d72010-05-07 18:11:54 +00001336 DIDescriptor S(Loc.getScope());
Devang Patel6daf99b2009-11-10 22:05:35 +00001337 if (S.isCompileUnit())
Devang Patel2db49d72010-05-07 18:11:54 +00001338 addCompileUnit(DICompileUnit(S));
Devang Patel6daf99b2009-11-10 22:05:35 +00001339 else if (S.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001340 processSubprogram(DISubprogram(S));
Devang Patel6daf99b2009-11-10 22:05:35 +00001341 else if (S.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001342 processLexicalBlock(DILexicalBlock(S));
Devang Patel6daf99b2009-11-10 22:05:35 +00001343 processLocation(Loc.getOrigLocation());
1344}
1345
Devang Patel98c65172009-07-30 18:25:15 +00001346/// processType - Process DIType.
1347void DebugInfoFinder::processType(DIType DT) {
Devang Patel72bcdb62009-08-10 22:09:58 +00001348 if (!addType(DT))
Devang Pateld2f79a12009-07-28 19:55:13 +00001349 return;
1350
1351 addCompileUnit(DT.getCompileUnit());
Devang Patel6ceea332009-08-31 18:49:10 +00001352 if (DT.isCompositeType()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001353 DICompositeType DCT(DT);
Devang Patel98c65172009-07-30 18:25:15 +00001354 processType(DCT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001355 DIArray DA = DCT.getTypeArray();
Devang Patel3c91b052010-03-08 20:52:55 +00001356 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1357 DIDescriptor D = DA.getElement(i);
1358 if (D.isType())
Devang Patel2db49d72010-05-07 18:11:54 +00001359 processType(DIType(D));
Devang Patel3c91b052010-03-08 20:52:55 +00001360 else if (D.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001361 processSubprogram(DISubprogram(D));
Devang Patel3c91b052010-03-08 20:52:55 +00001362 }
Devang Patel6ceea332009-08-31 18:49:10 +00001363 } else if (DT.isDerivedType()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001364 DIDerivedType DDT(DT);
Devang Patel3c91b052010-03-08 20:52:55 +00001365 processType(DDT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001366 }
1367}
1368
Devang Patelbeab41b2009-10-07 22:04:08 +00001369/// processLexicalBlock
1370void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
Devang Patelbeab41b2009-10-07 22:04:08 +00001371 DIScope Context = LB.getContext();
1372 if (Context.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001373 return processLexicalBlock(DILexicalBlock(Context));
Devang Patelbeab41b2009-10-07 22:04:08 +00001374 else
Devang Patel2db49d72010-05-07 18:11:54 +00001375 return processSubprogram(DISubprogram(Context));
Devang Patelbeab41b2009-10-07 22:04:08 +00001376}
1377
Devang Patel98c65172009-07-30 18:25:15 +00001378/// processSubprogram - Process DISubprogram.
1379void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001380 if (!addSubprogram(SP))
1381 return;
1382 addCompileUnit(SP.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +00001383 processType(SP.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +00001384}
1385
Devang Patelb4d31302009-07-31 18:18:52 +00001386/// processDeclare - Process DbgDeclareInst.
1387void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
Devang Patel3c91b052010-03-08 20:52:55 +00001388 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1389 if (!N) return;
1390
1391 DIDescriptor DV(N);
1392 if (!DV.isVariable())
Devang Patelb4d31302009-07-31 18:18:52 +00001393 return;
1394
Devang Patel2db49d72010-05-07 18:11:54 +00001395 if (!NodesSeen.insert(DV))
Devang Patelb4d31302009-07-31 18:18:52 +00001396 return;
1397
Devang Patel3c91b052010-03-08 20:52:55 +00001398 addCompileUnit(DIVariable(N).getCompileUnit());
1399 processType(DIVariable(N).getType());
Devang Patelb4d31302009-07-31 18:18:52 +00001400}
1401
Devang Patel72bcdb62009-08-10 22:09:58 +00001402/// addType - Add type into Tys.
1403bool DebugInfoFinder::addType(DIType DT) {
Devang Patel3c91b052010-03-08 20:52:55 +00001404 if (!DT.isValid())
Devang Patel72bcdb62009-08-10 22:09:58 +00001405 return false;
1406
Devang Patel2db49d72010-05-07 18:11:54 +00001407 if (!NodesSeen.insert(DT))
Devang Patel72bcdb62009-08-10 22:09:58 +00001408 return false;
1409
Devang Patel2db49d72010-05-07 18:11:54 +00001410 TYs.push_back(DT);
Devang Patel72bcdb62009-08-10 22:09:58 +00001411 return true;
1412}
1413
Devang Pateld2f79a12009-07-28 19:55:13 +00001414/// addCompileUnit - Add compile unit into CUs.
Devang Patel98c65172009-07-30 18:25:15 +00001415bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Patel3c91b052010-03-08 20:52:55 +00001416 if (!CU.Verify())
Devang Pateld2f79a12009-07-28 19:55:13 +00001417 return false;
1418
Devang Patel2db49d72010-05-07 18:11:54 +00001419 if (!NodesSeen.insert(CU))
Devang Pateld2f79a12009-07-28 19:55:13 +00001420 return false;
1421
Devang Patel2db49d72010-05-07 18:11:54 +00001422 CUs.push_back(CU);
Devang Pateld2f79a12009-07-28 19:55:13 +00001423 return true;
1424}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001425
Devang Pateld2f79a12009-07-28 19:55:13 +00001426/// addGlobalVariable - Add global variable into GVs.
Devang Patel98c65172009-07-30 18:25:15 +00001427bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Patel2db49d72010-05-07 18:11:54 +00001428 if (!DIDescriptor(DIG).isGlobalVariable())
Devang Pateld2f79a12009-07-28 19:55:13 +00001429 return false;
1430
Devang Patel2db49d72010-05-07 18:11:54 +00001431 if (!NodesSeen.insert(DIG))
Devang Pateld2f79a12009-07-28 19:55:13 +00001432 return false;
1433
Devang Patel2db49d72010-05-07 18:11:54 +00001434 GVs.push_back(DIG);
Devang Pateld2f79a12009-07-28 19:55:13 +00001435 return true;
1436}
1437
1438// addSubprogram - Add subprgoram into SPs.
Devang Patel98c65172009-07-30 18:25:15 +00001439bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Patel2db49d72010-05-07 18:11:54 +00001440 if (!DIDescriptor(SP).isSubprogram())
Devang Pateld2f79a12009-07-28 19:55:13 +00001441 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001442
Devang Patel2db49d72010-05-07 18:11:54 +00001443 if (!NodesSeen.insert(SP))
Devang Pateld2f79a12009-07-28 19:55:13 +00001444 return false;
1445
Devang Patel2db49d72010-05-07 18:11:54 +00001446 SPs.push_back(SP);
Devang Pateld2f79a12009-07-28 19:55:13 +00001447 return true;
1448}
1449
Victor Hernandez756462b2010-01-18 20:42:09 +00001450/// Find the debug info descriptor corresponding to this global variable.
1451static Value *findDbgGlobalDeclare(GlobalVariable *V) {
Chris Lattner099b7792009-12-29 09:22:47 +00001452 const Module *M = V->getParent();
1453 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1454 if (!NMD)
Torok Edwinff7d0e92009-03-10 13:41:26 +00001455 return 0;
Chris Lattner099b7792009-12-29 09:22:47 +00001456
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001457 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman872814a2010-07-21 18:54:18 +00001458 DIDescriptor DIG(cast<MDNode>(NMD->getOperand(i)));
Devang Patel3c91b052010-03-08 20:52:55 +00001459 if (!DIG.isGlobalVariable())
Chris Lattner099b7792009-12-29 09:22:47 +00001460 continue;
Devang Patel2db49d72010-05-07 18:11:54 +00001461 if (DIGlobalVariable(DIG).getGlobal() == V)
1462 return DIG;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001463 }
Chris Lattner099b7792009-12-29 09:22:47 +00001464 return 0;
1465}
Torok Edwinff7d0e92009-03-10 13:41:26 +00001466
Chris Lattner099b7792009-12-29 09:22:47 +00001467/// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
1468/// It looks through pointer casts too.
Victor Hernandez756462b2010-01-18 20:42:09 +00001469static const DbgDeclareInst *findDbgDeclare(const Value *V) {
Victor Hernandez3a328652010-01-15 19:04:09 +00001470 V = V->stripPointerCasts();
Jim Grosbache62b6902010-07-21 21:36:25 +00001471
Victor Hernandez3a328652010-01-15 19:04:09 +00001472 if (!isa<Instruction>(V) && !isa<Argument>(V))
Torok Edwin620f2802008-12-16 09:07:36 +00001473 return 0;
Jim Grosbache62b6902010-07-21 21:36:25 +00001474
Victor Hernandez3a328652010-01-15 19:04:09 +00001475 const Function *F = NULL;
1476 if (const Instruction *I = dyn_cast<Instruction>(V))
1477 F = I->getParent()->getParent();
1478 else if (const Argument *A = dyn_cast<Argument>(V))
1479 F = A->getParent();
Jim Grosbache62b6902010-07-21 21:36:25 +00001480
Victor Hernandez3a328652010-01-15 19:04:09 +00001481 for (Function::const_iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
1482 for (BasicBlock::const_iterator BI = (*FI).begin(), BE = (*FI).end();
1483 BI != BE; ++BI)
1484 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1485 if (DDI->getAddress() == V)
1486 return DDI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001487
Chris Lattner099b7792009-12-29 09:22:47 +00001488 return 0;
1489}
Bill Wendlingdc817b62009-05-14 18:26:15 +00001490
Chris Lattner099b7792009-12-29 09:22:47 +00001491bool llvm::getLocationInfo(const Value *V, std::string &DisplayName,
1492 std::string &Type, unsigned &LineNo,
1493 std::string &File, std::string &Dir) {
1494 DICompileUnit Unit;
1495 DIType TypeD;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001496
Chris Lattner099b7792009-12-29 09:22:47 +00001497 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1498 Value *DIGV = findDbgGlobalDeclare(GV);
1499 if (!DIGV) return false;
1500 DIGlobalVariable Var(cast<MDNode>(DIGV));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001501
Chris Lattner099b7792009-12-29 09:22:47 +00001502 StringRef D = Var.getDisplayName();
Devang Patel65dbc902009-11-25 17:36:49 +00001503 if (!D.empty())
Chris Lattner099b7792009-12-29 09:22:47 +00001504 DisplayName = D;
1505 LineNo = Var.getLineNumber();
1506 Unit = Var.getCompileUnit();
1507 TypeD = Var.getType();
1508 } else {
1509 const DbgDeclareInst *DDI = findDbgDeclare(V);
1510 if (!DDI) return false;
1511 DIVariable Var(cast<MDNode>(DDI->getVariable()));
1512
1513 StringRef D = Var.getName();
1514 if (!D.empty())
1515 DisplayName = D;
1516 LineNo = Var.getLineNumber();
1517 Unit = Var.getCompileUnit();
1518 TypeD = Var.getType();
Torok Edwinff7d0e92009-03-10 13:41:26 +00001519 }
Devang Patel13e16b62009-06-26 01:49:18 +00001520
Chris Lattner099b7792009-12-29 09:22:47 +00001521 StringRef T = TypeD.getName();
1522 if (!T.empty())
1523 Type = T;
1524 StringRef F = Unit.getFilename();
1525 if (!F.empty())
1526 File = F;
1527 StringRef D = Unit.getDirectory();
1528 if (!D.empty())
1529 Dir = D;
1530 return true;
1531}
Devang Patel9e529c32009-07-02 01:15:24 +00001532
Chris Lattner099b7792009-12-29 09:22:47 +00001533/// getDISubprogram - Find subprogram that is enclosing this scope.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001534DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
Chris Lattner099b7792009-12-29 09:22:47 +00001535 DIDescriptor D(Scope);
Chris Lattner099b7792009-12-29 09:22:47 +00001536 if (D.isSubprogram())
1537 return DISubprogram(Scope);
Jim Grosbache62b6902010-07-21 21:36:25 +00001538
Chris Lattner099b7792009-12-29 09:22:47 +00001539 if (D.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001540 return getDISubprogram(DILexicalBlock(Scope).getContext());
Jim Grosbache62b6902010-07-21 21:36:25 +00001541
Chris Lattner099b7792009-12-29 09:22:47 +00001542 return DISubprogram();
1543}
Devang Patel193f7202009-11-24 01:14:22 +00001544
Chris Lattner099b7792009-12-29 09:22:47 +00001545/// getDICompositeType - Find underlying composite type.
1546DICompositeType llvm::getDICompositeType(DIType T) {
Chris Lattner099b7792009-12-29 09:22:47 +00001547 if (T.isCompositeType())
Devang Patel2db49d72010-05-07 18:11:54 +00001548 return DICompositeType(T);
Jim Grosbache62b6902010-07-21 21:36:25 +00001549
Chris Lattner099b7792009-12-29 09:22:47 +00001550 if (T.isDerivedType())
Devang Patel2db49d72010-05-07 18:11:54 +00001551 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
Jim Grosbache62b6902010-07-21 21:36:25 +00001552
Chris Lattner099b7792009-12-29 09:22:47 +00001553 return DICompositeType();
Torok Edwin620f2802008-12-16 09:07:36 +00001554}