blob: 06a66d5539e444fca67a904a84d6e0e89d6c24f3 [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"
Dan Gohman489b29b2010-08-20 22:02:26 +000025#include "llvm/ADT/STLExtras.h"
David Greene0eb5b662009-12-23 19:45:49 +000026#include "llvm/Support/Debug.h"
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000027#include "llvm/Support/Dwarf.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
Devang Patel5b164b52010-08-02 22:51:46 +000036DIDescriptor::DIDescriptor(const DIFile F) : DbgNode(F.DbgNode) {
37}
38
39DIDescriptor::DIDescriptor(const DISubprogram F) : DbgNode(F.DbgNode) {
40}
41
42DIDescriptor::DIDescriptor(const DILexicalBlock F) : DbgNode(F.DbgNode) {
43}
44
45DIDescriptor::DIDescriptor(const DIVariable F) : DbgNode(F.DbgNode) {
46}
47
48DIDescriptor::DIDescriptor(const DIType F) : DbgNode(F.DbgNode) {
49}
50
Jim Grosbache62b6902010-07-21 21:36:25 +000051StringRef
Devang Patel5ccdd102009-09-29 18:40:58 +000052DIDescriptor::getStringField(unsigned Elt) const {
Devang Patele4b27562009-08-28 23:24:31 +000053 if (DbgNode == 0)
Devang Patel65dbc902009-11-25 17:36:49 +000054 return StringRef();
Chris Lattnera45664f2008-11-10 02:56:27 +000055
Chris Lattner5d0cacd2009-12-31 01:22:29 +000056 if (Elt < DbgNode->getNumOperands())
57 if (MDString *MDS = dyn_cast_or_null<MDString>(DbgNode->getOperand(Elt)))
Devang Patel65dbc902009-11-25 17:36:49 +000058 return MDS->getString();
Daniel Dunbarf612ff62009-09-19 20:40:05 +000059
Devang Patel65dbc902009-11-25 17:36:49 +000060 return StringRef();
Chris Lattnera45664f2008-11-10 02:56:27 +000061}
62
63uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +000064 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +000065 return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +000066
Chris Lattner5d0cacd2009-12-31 01:22:29 +000067 if (Elt < DbgNode->getNumOperands())
68 if (ConstantInt *CI = dyn_cast<ConstantInt>(DbgNode->getOperand(Elt)))
Devang Patele4b27562009-08-28 23:24:31 +000069 return CI->getZExtValue();
Daniel Dunbarf612ff62009-09-19 20:40:05 +000070
Chris Lattnera45664f2008-11-10 02:56:27 +000071 return 0;
72}
73
Chris Lattnera45664f2008-11-10 02:56:27 +000074DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +000075 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +000076 return DIDescriptor();
Bill Wendlingdc817b62009-05-14 18:26:15 +000077
Chris Lattner7a2f3e02010-03-31 05:53:47 +000078 if (Elt < DbgNode->getNumOperands())
Jim Grosbache62b6902010-07-21 21:36:25 +000079 return
80 DIDescriptor(dyn_cast_or_null<const MDNode>(DbgNode->getOperand(Elt)));
Devang Patele4b27562009-08-28 23:24:31 +000081 return DIDescriptor();
Chris Lattnera45664f2008-11-10 02:56:27 +000082}
83
84GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +000085 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +000086 return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +000087
Chris Lattner5d0cacd2009-12-31 01:22:29 +000088 if (Elt < DbgNode->getNumOperands())
89 return dyn_cast_or_null<GlobalVariable>(DbgNode->getOperand(Elt));
Devang Patele4b27562009-08-28 23:24:31 +000090 return 0;
Chris Lattnera45664f2008-11-10 02:56:27 +000091}
92
Devang Patel27398962010-08-09 21:39:24 +000093Constant *DIDescriptor::getConstantField(unsigned Elt) const {
94 if (DbgNode == 0)
95 return 0;
96
97 if (Elt < DbgNode->getNumOperands())
98 return dyn_cast_or_null<Constant>(DbgNode->getOperand(Elt));
99 return 0;
100}
101
Stuart Hastings215aa152010-06-11 20:08:44 +0000102Function *DIDescriptor::getFunctionField(unsigned Elt) const {
103 if (DbgNode == 0)
104 return 0;
105
106 if (Elt < DbgNode->getNumOperands())
107 return dyn_cast_or_null<Function>(DbgNode->getOperand(Elt));
108 return 0;
109}
110
Chris Lattnerf0908a32009-12-31 03:02:08 +0000111unsigned DIVariable::getNumAddrElements() const {
Devang Patel3cf763d2010-09-29 23:07:21 +0000112 if (getVersion() <= llvm::LLVMDebugVersion8)
113 return DbgNode->getNumOperands()-6;
114 return DbgNode->getNumOperands()-7;
Chris Lattnerf0908a32009-12-31 03:02:08 +0000115}
116
117
Chris Lattnera45664f2008-11-10 02:56:27 +0000118//===----------------------------------------------------------------------===//
Devang Patel6ceea332009-08-31 18:49:10 +0000119// Predicates
Chris Lattnera45664f2008-11-10 02:56:27 +0000120//===----------------------------------------------------------------------===//
121
Devang Patel6ceea332009-08-31 18:49:10 +0000122/// isBasicType - Return true if the specified tag is legal for
123/// DIBasicType.
124bool DIDescriptor::isBasicType() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000125 return DbgNode && getTag() == dwarf::DW_TAG_base_type;
Torok Edwinb07fbd92008-12-13 08:25:29 +0000126}
Chris Lattnera45664f2008-11-10 02:56:27 +0000127
Devang Patel6ceea332009-08-31 18:49:10 +0000128/// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
129bool DIDescriptor::isDerivedType() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000130 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000131 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000132 case dwarf::DW_TAG_typedef:
133 case dwarf::DW_TAG_pointer_type:
134 case dwarf::DW_TAG_reference_type:
135 case dwarf::DW_TAG_const_type:
136 case dwarf::DW_TAG_volatile_type:
137 case dwarf::DW_TAG_restrict_type:
138 case dwarf::DW_TAG_member:
139 case dwarf::DW_TAG_inheritance:
Devang Patel49d96382010-08-23 23:16:25 +0000140 case dwarf::DW_TAG_friend:
Chris Lattnera45664f2008-11-10 02:56:27 +0000141 return true;
142 default:
Devang Patele4b27562009-08-28 23:24:31 +0000143 // CompositeTypes are currently modelled as DerivedTypes.
Devang Patel6ceea332009-08-31 18:49:10 +0000144 return isCompositeType();
Chris Lattnera45664f2008-11-10 02:56:27 +0000145 }
146}
147
Chris Lattnera45664f2008-11-10 02:56:27 +0000148/// isCompositeType - Return true if the specified tag is legal for
149/// DICompositeType.
Devang Patel6ceea332009-08-31 18:49:10 +0000150bool DIDescriptor::isCompositeType() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000151 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000152 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000153 case dwarf::DW_TAG_array_type:
154 case dwarf::DW_TAG_structure_type:
155 case dwarf::DW_TAG_union_type:
156 case dwarf::DW_TAG_enumeration_type:
157 case dwarf::DW_TAG_vector_type:
158 case dwarf::DW_TAG_subroutine_type:
Devang Patel25cb0d72009-03-25 03:52:06 +0000159 case dwarf::DW_TAG_class_type:
Chris Lattnera45664f2008-11-10 02:56:27 +0000160 return true;
161 default:
162 return false;
163 }
164}
165
Chris Lattnera45664f2008-11-10 02:56:27 +0000166/// isVariable - Return true if the specified tag is legal for DIVariable.
Devang Patel6ceea332009-08-31 18:49:10 +0000167bool DIDescriptor::isVariable() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000168 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000169 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000170 case dwarf::DW_TAG_auto_variable:
171 case dwarf::DW_TAG_arg_variable:
172 case dwarf::DW_TAG_return_variable:
173 return true;
174 default:
175 return false;
176 }
177}
178
Devang Patelecbeb1a2009-09-30 22:34:41 +0000179/// isType - Return true if the specified tag is legal for DIType.
180bool DIDescriptor::isType() const {
181 return isBasicType() || isCompositeType() || isDerivedType();
182}
183
Devang Patel6ceea332009-08-31 18:49:10 +0000184/// isSubprogram - Return true if the specified tag is legal for
185/// DISubprogram.
186bool DIDescriptor::isSubprogram() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000187 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
Devang Patel6ceea332009-08-31 18:49:10 +0000188}
189
190/// isGlobalVariable - Return true if the specified tag is legal for
191/// DIGlobalVariable.
192bool DIDescriptor::isGlobalVariable() const {
Devang Patel29368072010-08-10 07:11:13 +0000193 return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
194 getTag() == dwarf::DW_TAG_constant);
Devang Patel6ceea332009-08-31 18:49:10 +0000195}
196
Devang Patelecbeb1a2009-09-30 22:34:41 +0000197/// isGlobal - Return true if the specified tag is legal for DIGlobal.
198bool DIDescriptor::isGlobal() const {
199 return isGlobalVariable();
200}
201
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000202/// isScope - Return true if the specified tag is one of the scope
Devang Patel43d98b32009-08-31 20:44:45 +0000203/// related tag.
204bool DIDescriptor::isScope() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000205 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000206 switch (getTag()) {
207 case dwarf::DW_TAG_compile_unit:
208 case dwarf::DW_TAG_lexical_block:
209 case dwarf::DW_TAG_subprogram:
210 case dwarf::DW_TAG_namespace:
211 return true;
212 default:
213 break;
Devang Patel43d98b32009-08-31 20:44:45 +0000214 }
215 return false;
216}
Devang Patel6ceea332009-08-31 18:49:10 +0000217
Devang Patelc9f322d2009-08-31 21:34:44 +0000218/// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
219bool DIDescriptor::isCompileUnit() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000220 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
Devang Patelc9f322d2009-08-31 21:34:44 +0000221}
222
Devang Patel7aa81892010-03-08 22:27:22 +0000223/// isFile - Return true if the specified tag is DW_TAG_file_type.
224bool DIDescriptor::isFile() const {
225 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
226}
227
Devang Patel6404e4e2009-12-15 19:16:48 +0000228/// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
229bool DIDescriptor::isNameSpace() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000230 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
Devang Patel6404e4e2009-12-15 19:16:48 +0000231}
232
Devang Patel5e005d82009-08-31 22:00:15 +0000233/// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
234bool DIDescriptor::isLexicalBlock() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000235 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block;
Devang Patel5e005d82009-08-31 22:00:15 +0000236}
237
Devang Patelecbeb1a2009-09-30 22:34:41 +0000238/// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
239bool DIDescriptor::isSubrange() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000240 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
Devang Patelecbeb1a2009-09-30 22:34:41 +0000241}
242
243/// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
244bool DIDescriptor::isEnumerator() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000245 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
Devang Patelecbeb1a2009-09-30 22:34:41 +0000246}
247
Devang Patel6ceea332009-08-31 18:49:10 +0000248//===----------------------------------------------------------------------===//
249// Simple Descriptor Constructors and other Methods
250//===----------------------------------------------------------------------===//
251
Devang Patele9f8f5e2010-05-07 20:54:48 +0000252DIType::DIType(const MDNode *N) : DIScope(N) {
Devang Patel6ceea332009-08-31 18:49:10 +0000253 if (!N) return;
254 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
255 DbgNode = 0;
256 }
257}
258
Devang Patel68afdc32009-01-05 18:33:01 +0000259unsigned DIArray::getNumElements() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000260 if (!DbgNode)
261 return 0;
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000262 return DbgNode->getNumOperands();
Devang Patel68afdc32009-01-05 18:33:01 +0000263}
Chris Lattnera45664f2008-11-10 02:56:27 +0000264
Devang Patelc4999d72009-07-22 18:23:44 +0000265/// replaceAllUsesWith - Replace all uses of debug info referenced by
Dan Gohman872814a2010-07-21 18:54:18 +0000266/// this descriptor.
Dan Gohman489b29b2010-08-20 22:02:26 +0000267void DIType::replaceAllUsesWith(DIDescriptor &D) {
Devang Patel3c91b052010-03-08 20:52:55 +0000268 if (!DbgNode)
Devang Patelc4999d72009-07-22 18:23:44 +0000269 return;
270
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000271 // Since we use a TrackingVH for the node, its easy for clients to manufacture
272 // legitimate situations where they want to replaceAllUsesWith() on something
273 // which, due to uniquing, has merged with the source. We shield clients from
274 // this detail by allowing a value to be replaced with replaceAllUsesWith()
275 // itself.
Devang Pateled66bf52010-05-07 18:19:32 +0000276 if (DbgNode != D) {
Devang Patele9f8f5e2010-05-07 20:54:48 +0000277 MDNode *Node = const_cast<MDNode*>(DbgNode);
278 const MDNode *DN = D;
279 const Value *V = cast_or_null<Value>(DN);
280 Node->replaceAllUsesWith(const_cast<Value*>(V));
Dan Gohman489b29b2010-08-20 22:02:26 +0000281 MDNode::deleteTemporary(Node);
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000282 }
Devang Patelc4999d72009-07-22 18:23:44 +0000283}
284
Devang Patelb79b5352009-01-19 23:21:49 +0000285/// Verify - Verify that a compile unit is well formed.
286bool DICompileUnit::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000287 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000288 return false;
Devang Patel65dbc902009-11-25 17:36:49 +0000289 StringRef N = getFilename();
290 if (N.empty())
Bill Wendling0582ae92009-03-13 04:39:26 +0000291 return false;
Devang Patelb79b5352009-01-19 23:21:49 +0000292 // It is possible that directory and produce string is empty.
Bill Wendling0582ae92009-03-13 04:39:26 +0000293 return true;
Devang Patelb79b5352009-01-19 23:21:49 +0000294}
295
296/// Verify - Verify that a type descriptor is well formed.
297bool DIType::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000298 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000299 return false;
Devang Patel3c91b052010-03-08 20:52:55 +0000300 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000301 return false;
302
303 DICompileUnit CU = getCompileUnit();
Devang Patel3c91b052010-03-08 20:52:55 +0000304 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000305 return false;
306 return true;
307}
308
Devang Patel0c4720c2010-08-23 18:25:56 +0000309/// Verify - Verify that a basic type descriptor is well formed.
310bool DIBasicType::Verify() const {
311 return isBasicType();
312}
313
314/// Verify - Verify that a derived type descriptor is well formed.
315bool DIDerivedType::Verify() const {
316 return isDerivedType();
317}
318
Devang Patelb79b5352009-01-19 23:21:49 +0000319/// Verify - Verify that a composite type descriptor is well formed.
320bool DICompositeType::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000321 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000322 return false;
Devang Patel3c91b052010-03-08 20:52:55 +0000323 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000324 return false;
325
326 DICompileUnit CU = getCompileUnit();
Devang Patel3c91b052010-03-08 20:52:55 +0000327 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000328 return false;
329 return true;
330}
331
332/// Verify - Verify that a subprogram descriptor is well formed.
333bool DISubprogram::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000334 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000335 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000336
Devang Patel3c91b052010-03-08 20:52:55 +0000337 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000338 return false;
339
340 DICompileUnit CU = getCompileUnit();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000341 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000342 return false;
343
344 DICompositeType Ty = getType();
Devang Patel3c91b052010-03-08 20:52:55 +0000345 if (!Ty.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000346 return false;
347 return true;
348}
349
350/// Verify - Verify that a global variable descriptor is well formed.
351bool DIGlobalVariable::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000352 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000353 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000354
Devang Patel65dbc902009-11-25 17:36:49 +0000355 if (getDisplayName().empty())
Devang Patel84c73e92009-11-06 17:58:12 +0000356 return false;
357
Devang Patel3c91b052010-03-08 20:52:55 +0000358 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000359 return false;
360
361 DICompileUnit CU = getCompileUnit();
Devang Patel3c91b052010-03-08 20:52:55 +0000362 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000363 return false;
364
365 DIType Ty = getType();
366 if (!Ty.Verify())
367 return false;
368
Devang Patel27398962010-08-09 21:39:24 +0000369 if (!getGlobal() && !getConstant())
Devang Patelb79b5352009-01-19 23:21:49 +0000370 return false;
371
372 return true;
373}
374
375/// Verify - Verify that a variable descriptor is well formed.
376bool DIVariable::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000377 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000378 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000379
Devang Patel3c91b052010-03-08 20:52:55 +0000380 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000381 return false;
382
Devang Patel62077af2010-05-07 21:42:24 +0000383 if (!getCompileUnit().Verify())
384 return false;
385
Devang Patelb79b5352009-01-19 23:21:49 +0000386 DIType Ty = getType();
387 if (!Ty.Verify())
388 return false;
389
Devang Patelb79b5352009-01-19 23:21:49 +0000390 return true;
391}
392
Devang Patel3c91b052010-03-08 20:52:55 +0000393/// Verify - Verify that a location descriptor is well formed.
394bool DILocation::Verify() const {
395 if (!DbgNode)
396 return false;
Jim Grosbache62b6902010-07-21 21:36:25 +0000397
Devang Patel3c91b052010-03-08 20:52:55 +0000398 return DbgNode->getNumOperands() == 4;
399}
400
Devang Patel47e22652010-05-07 23:04:32 +0000401/// Verify - Verify that a namespace descriptor is well formed.
402bool DINameSpace::Verify() const {
403 if (!DbgNode)
404 return false;
405 if (getName().empty())
406 return false;
407 if (!getCompileUnit().Verify())
408 return false;
409 return true;
410}
411
Devang Patel36375ee2009-02-17 21:23:59 +0000412/// getOriginalTypeSize - If this type is derived from a base type then
413/// return base type size.
414uint64_t DIDerivedType::getOriginalTypeSize() const {
Devang Patel61ecbd12009-11-04 23:48:00 +0000415 unsigned Tag = getTag();
416 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
417 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
418 Tag == dwarf::DW_TAG_restrict_type) {
419 DIType BaseType = getTypeDerivedFrom();
Jim Grosbache62b6902010-07-21 21:36:25 +0000420 // If this type is not derived from any type then take conservative
Devang Patel5ebfa2d2009-11-06 18:24:05 +0000421 // approach.
Devang Patel3c91b052010-03-08 20:52:55 +0000422 if (!BaseType.isValid())
Devang Patel5ebfa2d2009-11-06 18:24:05 +0000423 return getSizeInBits();
Devang Patel61ecbd12009-11-04 23:48:00 +0000424 if (BaseType.isDerivedType())
Devang Patel2db49d72010-05-07 18:11:54 +0000425 return DIDerivedType(BaseType).getOriginalTypeSize();
Devang Patel61ecbd12009-11-04 23:48:00 +0000426 else
427 return BaseType.getSizeInBits();
428 }
Jim Grosbache62b6902010-07-21 21:36:25 +0000429
Devang Patel61ecbd12009-11-04 23:48:00 +0000430 return getSizeInBits();
Devang Patel36375ee2009-02-17 21:23:59 +0000431}
Devang Patelb79b5352009-01-19 23:21:49 +0000432
Jim Grosbache62b6902010-07-21 21:36:25 +0000433/// isInlinedFnArgument - Return true if this variable provides debugging
Devang Patel719f6a92010-04-29 20:40:36 +0000434/// information for an inlined function arguments.
435bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
436 assert(CurFn && "Invalid function");
437 if (!getContext().isSubprogram())
438 return false;
Jim Grosbache62b6902010-07-21 21:36:25 +0000439 // This variable is not inlined function argument if its scope
Devang Patel719f6a92010-04-29 20:40:36 +0000440 // does not describe current function.
Devang Patel2db49d72010-05-07 18:11:54 +0000441 return !(DISubprogram(getContext()).describes(CurFn));
Devang Patel719f6a92010-04-29 20:40:36 +0000442}
443
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000444/// describes - Return true if this subprogram provides debugging
445/// information for the function F.
446bool DISubprogram::describes(const Function *F) {
Chris Lattner099b7792009-12-29 09:22:47 +0000447 assert(F && "Invalid function");
Devang Patelffd33cd2010-06-16 06:42:02 +0000448 if (F == getFunction())
449 return true;
Devang Patel65dbc902009-11-25 17:36:49 +0000450 StringRef Name = getLinkageName();
451 if (Name.empty())
Devang Patel5ccdd102009-09-29 18:40:58 +0000452 Name = getName();
Devang Patel65dbc902009-11-25 17:36:49 +0000453 if (F->getName() == Name)
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000454 return true;
455 return false;
456}
457
Jim Grosbache62b6902010-07-21 21:36:25 +0000458unsigned DISubprogram::isOptimized() const {
Devang Patelccff8122010-04-30 19:38:23 +0000459 assert (DbgNode && "Invalid subprogram descriptor!");
460 if (DbgNode->getNumOperands() == 16)
461 return getUnsignedField(15);
462 return 0;
463}
464
Devang Patel65dbc902009-11-25 17:36:49 +0000465StringRef DIScope::getFilename() const {
Devang Patel77bf2952010-03-08 22:02:50 +0000466 if (!DbgNode)
467 return StringRef();
Jim Grosbache62b6902010-07-21 21:36:25 +0000468 if (isLexicalBlock())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000469 return DILexicalBlock(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000470 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000471 return DISubprogram(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000472 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000473 return DICompileUnit(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000474 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000475 return DINameSpace(DbgNode).getFilename();
Devang Patel77bf2952010-03-08 22:02:50 +0000476 if (isType())
477 return DIType(DbgNode).getFilename();
Devang Patel7aa81892010-03-08 22:27:22 +0000478 if (isFile())
479 return DIFile(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000480 assert(0 && "Invalid DIScope!");
Devang Patel65dbc902009-11-25 17:36:49 +0000481 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000482}
483
Devang Patel65dbc902009-11-25 17:36:49 +0000484StringRef DIScope::getDirectory() const {
Devang Patel77bf2952010-03-08 22:02:50 +0000485 if (!DbgNode)
486 return StringRef();
Jim Grosbache62b6902010-07-21 21:36:25 +0000487 if (isLexicalBlock())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000488 return DILexicalBlock(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000489 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000490 return DISubprogram(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000491 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000492 return DICompileUnit(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000493 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000494 return DINameSpace(DbgNode).getDirectory();
Devang Patel77bf2952010-03-08 22:02:50 +0000495 if (isType())
496 return DIType(DbgNode).getDirectory();
Devang Patel7aa81892010-03-08 22:27:22 +0000497 if (isFile())
498 return DIFile(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000499 assert(0 && "Invalid DIScope!");
Devang Patel65dbc902009-11-25 17:36:49 +0000500 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000501}
502
Chris Lattnera45664f2008-11-10 02:56:27 +0000503//===----------------------------------------------------------------------===//
Devang Patel7136a652009-07-01 22:10:23 +0000504// DIDescriptor: dump routines for all descriptors.
505//===----------------------------------------------------------------------===//
506
507
Dan Gohman50404362010-05-07 15:30:29 +0000508/// print - Print descriptor.
509void DIDescriptor::print(raw_ostream &OS) const {
510 OS << "[" << dwarf::TagString(getTag()) << "] ";
511 OS.write_hex((intptr_t) &*DbgNode) << ']';
Devang Patel7136a652009-07-01 22:10:23 +0000512}
513
Dan Gohman50404362010-05-07 15:30:29 +0000514/// print - Print compile unit.
515void DICompileUnit::print(raw_ostream &OS) const {
Devang Patel7136a652009-07-01 22:10:23 +0000516 if (getLanguage())
Dan Gohman50404362010-05-07 15:30:29 +0000517 OS << " [" << dwarf::LanguageString(getLanguage()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000518
Dan Gohmanfaa19c32010-05-10 20:07:44 +0000519 OS << " [" << getDirectory() << "/" << getFilename() << "]";
Devang Patel7136a652009-07-01 22:10:23 +0000520}
521
Dan Gohman50404362010-05-07 15:30:29 +0000522/// print - Print type.
523void DIType::print(raw_ostream &OS) const {
Devang Patel3c91b052010-03-08 20:52:55 +0000524 if (!DbgNode) return;
Devang Patel7136a652009-07-01 22:10:23 +0000525
Devang Patel65dbc902009-11-25 17:36:49 +0000526 StringRef Res = getName();
527 if (!Res.empty())
Dan Gohman50404362010-05-07 15:30:29 +0000528 OS << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000529
530 unsigned Tag = getTag();
Dan Gohman50404362010-05-07 15:30:29 +0000531 OS << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000532
533 // TODO : Print context
Dan Gohmanc014d092010-05-07 16:17:22 +0000534 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000535 OS << " ["
Dan Gohman9a7063e2010-05-07 16:39:27 +0000536 << "line " << getLineNumber() << ", "
537 << getSizeInBits() << " bits, "
538 << getAlignInBits() << " bit alignment, "
539 << getOffsetInBits() << " bit offset"
Chris Lattnera81d29b2009-08-23 07:33:14 +0000540 << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000541
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000542 if (isPrivate())
Dan Gohman50404362010-05-07 15:30:29 +0000543 OS << " [private] ";
Devang Patel7136a652009-07-01 22:10:23 +0000544 else if (isProtected())
Dan Gohman50404362010-05-07 15:30:29 +0000545 OS << " [protected] ";
Devang Patel7136a652009-07-01 22:10:23 +0000546
547 if (isForwardDecl())
Dan Gohman50404362010-05-07 15:30:29 +0000548 OS << " [fwd] ";
Devang Patel7136a652009-07-01 22:10:23 +0000549
Devang Patel6ceea332009-08-31 18:49:10 +0000550 if (isBasicType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000551 DIBasicType(DbgNode).print(OS);
Devang Patel6ceea332009-08-31 18:49:10 +0000552 else if (isDerivedType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000553 DIDerivedType(DbgNode).print(OS);
Devang Patel6ceea332009-08-31 18:49:10 +0000554 else if (isCompositeType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000555 DICompositeType(DbgNode).print(OS);
Devang Patel7136a652009-07-01 22:10:23 +0000556 else {
Dan Gohman50404362010-05-07 15:30:29 +0000557 OS << "Invalid DIType\n";
Devang Patel7136a652009-07-01 22:10:23 +0000558 return;
559 }
560
Dan Gohman50404362010-05-07 15:30:29 +0000561 OS << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000562}
563
Dan Gohman50404362010-05-07 15:30:29 +0000564/// print - Print basic type.
565void DIBasicType::print(raw_ostream &OS) const {
566 OS << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000567}
568
Dan Gohman50404362010-05-07 15:30:29 +0000569/// print - Print derived type.
570void DIDerivedType::print(raw_ostream &OS) const {
Dan Gohmanc014d092010-05-07 16:17:22 +0000571 OS << "\n\t Derived From: "; getTypeDerivedFrom().print(OS);
Devang Patel7136a652009-07-01 22:10:23 +0000572}
573
Dan Gohman50404362010-05-07 15:30:29 +0000574/// print - Print composite type.
575void DICompositeType::print(raw_ostream &OS) const {
Devang Patel7136a652009-07-01 22:10:23 +0000576 DIArray A = getTypeArray();
Dan Gohman50404362010-05-07 15:30:29 +0000577 OS << " [" << A.getNumElements() << " elements]";
Devang Patel7136a652009-07-01 22:10:23 +0000578}
579
Dan Gohman50404362010-05-07 15:30:29 +0000580/// print - Print subprogram.
581void DISubprogram::print(raw_ostream &OS) const {
Devang Patel65dbc902009-11-25 17:36:49 +0000582 StringRef Res = getName();
583 if (!Res.empty())
Dan Gohman50404362010-05-07 15:30:29 +0000584 OS << " [" << Res << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000585
586 unsigned Tag = getTag();
Dan Gohman50404362010-05-07 15:30:29 +0000587 OS << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000588
589 // TODO : Print context
Dan Gohmanc014d092010-05-07 16:17:22 +0000590 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000591 OS << " [" << getLineNumber() << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000592
593 if (isLocalToUnit())
Dan Gohman50404362010-05-07 15:30:29 +0000594 OS << " [local] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000595
596 if (isDefinition())
Dan Gohman50404362010-05-07 15:30:29 +0000597 OS << " [def] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000598
Dan Gohman50404362010-05-07 15:30:29 +0000599 OS << "\n";
600}
601
602/// print - Print global variable.
603void DIGlobalVariable::print(raw_ostream &OS) const {
604 OS << " [";
Devang Patela49d8772010-05-07 23:19:07 +0000605 StringRef Res = getName();
606 if (!Res.empty())
607 OS << " [" << Res << "] ";
608
609 unsigned Tag = getTag();
610 OS << " [" << dwarf::TagString(Tag) << "] ";
611
612 // TODO : Print context
613 getCompileUnit().print(OS);
614 OS << " [" << getLineNumber() << "] ";
615
616 if (isLocalToUnit())
617 OS << " [local] ";
618
619 if (isDefinition())
620 OS << " [def] ";
621
622 if (isGlobalVariable())
623 DIGlobalVariable(DbgNode).print(OS);
624 OS << "]\n";
Dan Gohman50404362010-05-07 15:30:29 +0000625}
626
627/// print - Print variable.
628void DIVariable::print(raw_ostream &OS) const {
629 StringRef Res = getName();
630 if (!Res.empty())
631 OS << " [" << Res << "] ";
632
Dan Gohmanc014d092010-05-07 16:17:22 +0000633 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000634 OS << " [" << getLineNumber() << "] ";
Dan Gohmanc014d092010-05-07 16:17:22 +0000635 getType().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000636 OS << "\n";
637
638 // FIXME: Dump complex addresses
639}
640
641/// dump - Print descriptor to dbgs() with a newline.
642void DIDescriptor::dump() const {
643 print(dbgs()); dbgs() << '\n';
644}
645
646/// dump - Print compile unit to dbgs() with a newline.
647void DICompileUnit::dump() const {
648 print(dbgs()); dbgs() << '\n';
649}
650
651/// dump - Print type to dbgs() with a newline.
652void DIType::dump() const {
653 print(dbgs()); dbgs() << '\n';
654}
655
656/// dump - Print basic type to dbgs() with a newline.
657void DIBasicType::dump() const {
658 print(dbgs()); dbgs() << '\n';
659}
660
661/// dump - Print derived type to dbgs() with a newline.
662void DIDerivedType::dump() const {
663 print(dbgs()); dbgs() << '\n';
664}
665
666/// dump - Print composite type to dbgs() with a newline.
667void DICompositeType::dump() const {
668 print(dbgs()); dbgs() << '\n';
669}
670
Dan Gohman50404362010-05-07 15:30:29 +0000671/// dump - Print subprogram to dbgs() with a newline.
672void DISubprogram::dump() const {
673 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000674}
675
676/// dump - Print global variable.
677void DIGlobalVariable::dump() const {
Dan Gohman50404362010-05-07 15:30:29 +0000678 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000679}
680
681/// dump - Print variable.
682void DIVariable::dump() const {
Dan Gohman50404362010-05-07 15:30:29 +0000683 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000684}
685
686//===----------------------------------------------------------------------===//
Chris Lattnera45664f2008-11-10 02:56:27 +0000687// DIFactory: Basic Helpers
688//===----------------------------------------------------------------------===//
689
Bill Wendlingdc817b62009-05-14 18:26:15 +0000690DIFactory::DIFactory(Module &m)
Victor Hernandez06203122010-01-23 00:03:28 +0000691 : M(m), VMContext(M.getContext()), DeclareFn(0), ValueFn(0) {}
Chris Lattner497a7a82008-11-10 04:10:34 +0000692
Chris Lattnera45664f2008-11-10 02:56:27 +0000693Constant *DIFactory::GetTagConstant(unsigned TAG) {
Devang Patel6906ba52009-01-20 19:22:03 +0000694 assert((TAG & LLVMDebugVersionMask) == 0 &&
Chris Lattnera45664f2008-11-10 02:56:27 +0000695 "Tag too large for debug encoding!");
Owen Anderson1d0be152009-08-13 21:58:54 +0000696 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
Chris Lattnera45664f2008-11-10 02:56:27 +0000697}
698
Chris Lattnera45664f2008-11-10 02:56:27 +0000699//===----------------------------------------------------------------------===//
700// DIFactory: Primary Constructors
701//===----------------------------------------------------------------------===//
702
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000703/// GetOrCreateArray - Create an descriptor for an array of descriptors.
Chris Lattnera45664f2008-11-10 02:56:27 +0000704/// This implicitly uniques the arrays created.
705DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
Benjamin Kramera53557e2010-09-21 16:41:29 +0000706 if (NumTys == 0) {
707 Value *Null = llvm::Constant::getNullValue(Type::getInt32Ty(VMContext));
708 return DIArray(MDNode::get(VMContext, &Null, 1));
709 }
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000710
Benjamin Kramera53557e2010-09-21 16:41:29 +0000711 SmallVector<Value *, 16> Elts(Tys, Tys+NumTys);
712 return DIArray(MDNode::get(VMContext, Elts.data(), Elts.size()));
Chris Lattnera45664f2008-11-10 02:56:27 +0000713}
714
715/// GetOrCreateSubrange - Create a descriptor for a value range. This
716/// implicitly uniques the values returned.
717DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
Devang Patele4b27562009-08-28 23:24:31 +0000718 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000719 GetTagConstant(dwarf::DW_TAG_subrange_type),
Owen Anderson1d0be152009-08-13 21:58:54 +0000720 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
721 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
Chris Lattnera45664f2008-11-10 02:56:27 +0000722 };
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000723
Devang Patele4b27562009-08-28 23:24:31 +0000724 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000725}
726
727
728
729/// CreateCompileUnit - Create a new descriptor for the specified compile
730/// unit. Note that this does not unique compile units within the module.
731DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
Devang Patel65dbc902009-11-25 17:36:49 +0000732 StringRef Filename,
733 StringRef Directory,
734 StringRef Producer,
Devang Pateldd9db662009-01-30 18:20:31 +0000735 bool isMain,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000736 bool isOptimized,
Devang Patel65dbc902009-11-25 17:36:49 +0000737 StringRef Flags,
Devang Patel13319ce2009-02-17 22:43:44 +0000738 unsigned RunTimeVer) {
Devang Patele4b27562009-08-28 23:24:31 +0000739 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000740 GetTagConstant(dwarf::DW_TAG_compile_unit),
Devang Patele4b27562009-08-28 23:24:31 +0000741 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Owen Anderson1d0be152009-08-13 21:58:54 +0000742 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
Devang Patele4b27562009-08-28 23:24:31 +0000743 MDString::get(VMContext, Filename),
744 MDString::get(VMContext, Directory),
745 MDString::get(VMContext, Producer),
Owen Anderson1d0be152009-08-13 21:58:54 +0000746 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
747 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
Devang Patele4b27562009-08-28 23:24:31 +0000748 MDString::get(VMContext, Flags),
Owen Anderson1d0be152009-08-13 21:58:54 +0000749 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
Chris Lattnera45664f2008-11-10 02:56:27 +0000750 };
Devang Patele4b27562009-08-28 23:24:31 +0000751
752 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000753}
754
Devang Patel7aa81892010-03-08 22:27:22 +0000755/// CreateFile - Create a new descriptor for the specified file.
756DIFile DIFactory::CreateFile(StringRef Filename,
757 StringRef Directory,
758 DICompileUnit CU) {
759 Value *Elts[] = {
760 GetTagConstant(dwarf::DW_TAG_file_type),
761 MDString::get(VMContext, Filename),
762 MDString::get(VMContext, Directory),
Devang Patel2db49d72010-05-07 18:11:54 +0000763 CU
Devang Patel7aa81892010-03-08 22:27:22 +0000764 };
765
766 return DIFile(MDNode::get(VMContext, &Elts[0], 4));
767}
768
Chris Lattnera45664f2008-11-10 02:56:27 +0000769/// CreateEnumerator - Create a single enumerator value.
Devang Patel65dbc902009-11-25 17:36:49 +0000770DIEnumerator DIFactory::CreateEnumerator(StringRef Name, uint64_t Val){
Devang Patele4b27562009-08-28 23:24:31 +0000771 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000772 GetTagConstant(dwarf::DW_TAG_enumerator),
Devang Patele4b27562009-08-28 23:24:31 +0000773 MDString::get(VMContext, Name),
Owen Anderson1d0be152009-08-13 21:58:54 +0000774 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
Chris Lattnera45664f2008-11-10 02:56:27 +0000775 };
Devang Patele4b27562009-08-28 23:24:31 +0000776 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000777}
778
779
780/// CreateBasicType - Create a basic type like int, float, etc.
781DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000782 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000783 DIFile F,
Chris Lattnera45664f2008-11-10 02:56:27 +0000784 unsigned LineNumber,
785 uint64_t SizeInBits,
786 uint64_t AlignInBits,
787 uint64_t OffsetInBits, unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000788 unsigned Encoding) {
Devang Patele4b27562009-08-28 23:24:31 +0000789 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000790 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patel2db49d72010-05-07 18:11:54 +0000791 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000792 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000793 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000794 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
795 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
796 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
797 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
798 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
799 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
Chris Lattnera45664f2008-11-10 02:56:27 +0000800 };
Devang Patele4b27562009-08-28 23:24:31 +0000801 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000802}
803
Devang Patelac16d442009-10-26 16:54:35 +0000804
805/// CreateBasicType - Create a basic type like int, float, etc.
806DIBasicType DIFactory::CreateBasicTypeEx(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000807 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000808 DIFile F,
Devang Patelac16d442009-10-26 16:54:35 +0000809 unsigned LineNumber,
810 Constant *SizeInBits,
811 Constant *AlignInBits,
812 Constant *OffsetInBits, unsigned Flags,
813 unsigned Encoding) {
814 Value *Elts[] = {
815 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patel2db49d72010-05-07 18:11:54 +0000816 Context,
Devang Patelac16d442009-10-26 16:54:35 +0000817 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000818 F,
Devang Patelac16d442009-10-26 16:54:35 +0000819 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
820 SizeInBits,
821 AlignInBits,
822 OffsetInBits,
823 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
824 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
825 };
826 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
827}
828
Devang Patelb4645642010-02-06 01:02:37 +0000829/// CreateArtificialType - Create a new DIType with "artificial" flag set.
830DIType DIFactory::CreateArtificialType(DIType Ty) {
831 if (Ty.isArtificial())
832 return Ty;
833
834 SmallVector<Value *, 9> Elts;
Devang Patel2db49d72010-05-07 18:11:54 +0000835 MDNode *N = Ty;
Devang Patelb4645642010-02-06 01:02:37 +0000836 assert (N && "Unexpected input DIType!");
837 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
838 if (Value *V = N->getOperand(i))
839 Elts.push_back(V);
840 else
841 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
842 }
843
844 unsigned CurFlags = Ty.getFlags();
845 CurFlags = CurFlags | DIType::FlagArtificial;
846
847 // Flags are stored at this slot.
848 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
849
850 return DIType(MDNode::get(VMContext, Elts.data(), Elts.size()));
851}
Devang Patelac16d442009-10-26 16:54:35 +0000852
Chris Lattnera45664f2008-11-10 02:56:27 +0000853/// CreateDerivedType - Create a derived type like const qualified type,
854/// pointer, typedef, etc.
855DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
856 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000857 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000858 DIFile F,
Chris Lattnera45664f2008-11-10 02:56:27 +0000859 unsigned LineNumber,
860 uint64_t SizeInBits,
861 uint64_t AlignInBits,
862 uint64_t OffsetInBits,
863 unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000864 DIType DerivedFrom) {
Devang Patele4b27562009-08-28 23:24:31 +0000865 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000866 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000867 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000868 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000869 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000870 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
871 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
872 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
873 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
874 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000875 DerivedFrom,
Chris Lattnera45664f2008-11-10 02:56:27 +0000876 };
Devang Patele4b27562009-08-28 23:24:31 +0000877 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000878}
879
Devang Patelac16d442009-10-26 16:54:35 +0000880
881/// CreateDerivedType - Create a derived type like const qualified type,
882/// pointer, typedef, etc.
883DIDerivedType DIFactory::CreateDerivedTypeEx(unsigned Tag,
884 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000885 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000886 DIFile F,
Devang Patelac16d442009-10-26 16:54:35 +0000887 unsigned LineNumber,
888 Constant *SizeInBits,
889 Constant *AlignInBits,
890 Constant *OffsetInBits,
891 unsigned Flags,
892 DIType DerivedFrom) {
893 Value *Elts[] = {
894 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000895 Context,
Devang Patelac16d442009-10-26 16:54:35 +0000896 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000897 F,
Devang Patelac16d442009-10-26 16:54:35 +0000898 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
899 SizeInBits,
900 AlignInBits,
901 OffsetInBits,
902 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000903 DerivedFrom,
Devang Patelac16d442009-10-26 16:54:35 +0000904 };
905 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
906}
907
908
Chris Lattnera45664f2008-11-10 02:56:27 +0000909/// CreateCompositeType - Create a composite type like array, struct, etc.
910DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
911 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000912 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000913 DIFile F,
Chris Lattnera45664f2008-11-10 02:56:27 +0000914 unsigned LineNumber,
915 uint64_t SizeInBits,
916 uint64_t AlignInBits,
917 uint64_t OffsetInBits,
918 unsigned Flags,
919 DIType DerivedFrom,
Devang Patel13319ce2009-02-17 22:43:44 +0000920 DIArray Elements,
Devang Patel0fd7f9d2010-01-26 21:14:59 +0000921 unsigned RuntimeLang,
922 MDNode *ContainingType) {
Owen Andersone277fed2009-07-07 16:31:25 +0000923
Devang Patele4b27562009-08-28 23:24:31 +0000924 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000925 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000926 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000927 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000928 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000929 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
930 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
931 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
932 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
933 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000934 DerivedFrom,
935 Elements,
Devang Patel0fd7f9d2010-01-26 21:14:59 +0000936 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
937 ContainingType
Chris Lattnera45664f2008-11-10 02:56:27 +0000938 };
Devang Patele7e5a0f2010-08-10 20:01:20 +0000939
940 MDNode *Node = MDNode::get(VMContext, &Elts[0], 13);
941 // Create a named metadata so that we do not lose this enum info.
942 if (Tag == dwarf::DW_TAG_enumeration_type) {
943 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.enum");
944 NMD->addOperand(Node);
945 }
946 return DICompositeType(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +0000947}
948
949
Dan Gohman489b29b2010-08-20 22:02:26 +0000950/// CreateTemporaryType - Create a temporary forward-declared type.
Dan Gohmana3833f12010-08-20 22:39:47 +0000951DIType DIFactory::CreateTemporaryType() {
Dan Gohman489b29b2010-08-20 22:02:26 +0000952 // Give the temporary MDNode a tag. It doesn't matter what tag we
953 // use here as long as DIType accepts it.
954 Value *Elts[] = {
955 GetTagConstant(DW_TAG_base_type)
956 };
957 MDNode *Node = MDNode::getTemporary(VMContext, Elts, array_lengthof(Elts));
958 return DIType(Node);
959}
960
961
Devang Patelac16d442009-10-26 16:54:35 +0000962/// CreateCompositeType - Create a composite type like array, struct, etc.
963DICompositeType DIFactory::CreateCompositeTypeEx(unsigned Tag,
964 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000965 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000966 DIFile F,
Devang Patelac16d442009-10-26 16:54:35 +0000967 unsigned LineNumber,
968 Constant *SizeInBits,
969 Constant *AlignInBits,
970 Constant *OffsetInBits,
971 unsigned Flags,
972 DIType DerivedFrom,
973 DIArray Elements,
Devang Patel6bf058c2010-08-10 20:22:49 +0000974 unsigned RuntimeLang,
975 MDNode *ContainingType) {
Devang Patelac16d442009-10-26 16:54:35 +0000976 Value *Elts[] = {
977 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000978 Context,
Devang Patelac16d442009-10-26 16:54:35 +0000979 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000980 F,
Devang Patelac16d442009-10-26 16:54:35 +0000981 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
982 SizeInBits,
983 AlignInBits,
984 OffsetInBits,
985 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000986 DerivedFrom,
987 Elements,
Devang Patel6bf058c2010-08-10 20:22:49 +0000988 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
989 ContainingType
Devang Patelac16d442009-10-26 16:54:35 +0000990 };
Devang Patel6bf058c2010-08-10 20:22:49 +0000991 MDNode *Node = MDNode::get(VMContext, &Elts[0], 13);
Devang Patele7e5a0f2010-08-10 20:01:20 +0000992 // Create a named metadata so that we do not lose this enum info.
993 if (Tag == dwarf::DW_TAG_enumeration_type) {
994 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.enum");
995 NMD->addOperand(Node);
996 }
997 return DICompositeType(Node);
Devang Patelac16d442009-10-26 16:54:35 +0000998}
999
1000
Chris Lattnera45664f2008-11-10 02:56:27 +00001001/// CreateSubprogram - Create a new descriptor for the specified subprogram.
1002/// See comments in DISubprogram for descriptions of these fields. This
1003/// method does not unique the generated descriptors.
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001004DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +00001005 StringRef Name,
1006 StringRef DisplayName,
1007 StringRef LinkageName,
Devang Patel4b945502010-03-09 00:44:10 +00001008 DIFile F,
Devang Patel2e369932010-01-23 00:26:28 +00001009 unsigned LineNo, DIType Ty,
Chris Lattnera45664f2008-11-10 02:56:27 +00001010 bool isLocalToUnit,
Devang Patel5d11eb02009-12-03 19:11:07 +00001011 bool isDefinition,
1012 unsigned VK, unsigned VIndex,
Devang Patel4e0d19d2010-02-03 19:57:19 +00001013 DIType ContainingType,
Devang Patel9dd2b472010-09-29 21:04:46 +00001014 unsigned Flags,
Stuart Hastings215aa152010-06-11 20:08:44 +00001015 bool isOptimized,
1016 Function *Fn) {
Devang Patel854967e2008-12-17 22:39:29 +00001017
Devang Patele4b27562009-08-28 23:24:31 +00001018 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001019 GetTagConstant(dwarf::DW_TAG_subprogram),
Devang Patele4b27562009-08-28 23:24:31 +00001020 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Devang Patel2db49d72010-05-07 18:11:54 +00001021 Context,
Devang Patele4b27562009-08-28 23:24:31 +00001022 MDString::get(VMContext, Name),
1023 MDString::get(VMContext, DisplayName),
1024 MDString::get(VMContext, LinkageName),
Devang Patel2db49d72010-05-07 18:11:54 +00001025 F,
Owen Anderson1d0be152009-08-13 21:58:54 +00001026 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001027 Ty,
Owen Anderson1d0be152009-08-13 21:58:54 +00001028 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
Devang Patel5d11eb02009-12-03 19:11:07 +00001029 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1030 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
1031 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
Devang Patel2db49d72010-05-07 18:11:54 +00001032 ContainingType,
Devang Patel9dd2b472010-09-29 21:04:46 +00001033 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Stuart Hastings215aa152010-06-11 20:08:44 +00001034 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
1035 Fn
Chris Lattnera45664f2008-11-10 02:56:27 +00001036 };
Devang Patelfd5fdc32010-06-28 05:53:08 +00001037 MDNode *Node = MDNode::get(VMContext, &Elts[0], 17);
1038
1039 // Create a named metadata so that we do not lose this mdnode.
1040 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
1041 NMD->addOperand(Node);
1042 return DISubprogram(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +00001043}
1044
Devang Patele3a18de2009-12-01 23:09:02 +00001045/// CreateSubprogramDefinition - Create new subprogram descriptor for the
Jim Grosbache62b6902010-07-21 21:36:25 +00001046/// given declaration.
1047DISubprogram DIFactory::CreateSubprogramDefinition(DISubprogram &SPDeclaration){
Devang Patele3a18de2009-12-01 23:09:02 +00001048 if (SPDeclaration.isDefinition())
Devang Patel2db49d72010-05-07 18:11:54 +00001049 return DISubprogram(SPDeclaration);
Devang Patele3a18de2009-12-01 23:09:02 +00001050
Devang Patel2db49d72010-05-07 18:11:54 +00001051 MDNode *DeclNode = SPDeclaration;
Devang Patele3a18de2009-12-01 23:09:02 +00001052 Value *Elts[] = {
1053 GetTagConstant(dwarf::DW_TAG_subprogram),
1054 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001055 DeclNode->getOperand(2), // Context
1056 DeclNode->getOperand(3), // Name
1057 DeclNode->getOperand(4), // DisplayName
1058 DeclNode->getOperand(5), // LinkageName
1059 DeclNode->getOperand(6), // CompileUnit
1060 DeclNode->getOperand(7), // LineNo
1061 DeclNode->getOperand(8), // Type
1062 DeclNode->getOperand(9), // isLocalToUnit
Stuart Hastings6d56b9f2010-06-05 00:39:29 +00001063 ConstantInt::get(Type::getInt1Ty(VMContext), true),
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001064 DeclNode->getOperand(11), // Virtuality
1065 DeclNode->getOperand(12), // VIndex
Devang Patel4e0d19d2010-02-03 19:57:19 +00001066 DeclNode->getOperand(13), // Containting Type
Devang Patel9dd2b472010-09-29 21:04:46 +00001067 DeclNode->getOperand(14), // Flags
Devang Patelacc6efa2010-06-27 21:04:31 +00001068 DeclNode->getOperand(15), // isOptimized
1069 SPDeclaration.getFunction()
Devang Patele3a18de2009-12-01 23:09:02 +00001070 };
Devang Patelfd5fdc32010-06-28 05:53:08 +00001071 MDNode *Node =MDNode::get(VMContext, &Elts[0], 16);
1072
1073 // Create a named metadata so that we do not lose this mdnode.
1074 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
1075 NMD->addOperand(Node);
1076 return DISubprogram(Node);
Devang Patele3a18de2009-12-01 23:09:02 +00001077}
1078
Chris Lattnera45664f2008-11-10 02:56:27 +00001079/// CreateGlobalVariable - Create a new descriptor for the specified global.
1080DIGlobalVariable
Devang Patel65dbc902009-11-25 17:36:49 +00001081DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
1082 StringRef DisplayName,
1083 StringRef LinkageName,
Devang Patel4b945502010-03-09 00:44:10 +00001084 DIFile F,
Devang Patel2e369932010-01-23 00:26:28 +00001085 unsigned LineNo, DIType Ty,bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +00001086 bool isDefinition, llvm::GlobalVariable *Val) {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001087 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001088 GetTagConstant(dwarf::DW_TAG_variable),
Devang Patele4b27562009-08-28 23:24:31 +00001089 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Devang Patel2db49d72010-05-07 18:11:54 +00001090 Context,
Devang Patele4b27562009-08-28 23:24:31 +00001091 MDString::get(VMContext, Name),
1092 MDString::get(VMContext, DisplayName),
1093 MDString::get(VMContext, LinkageName),
Devang Patel2db49d72010-05-07 18:11:54 +00001094 F,
Owen Anderson1d0be152009-08-13 21:58:54 +00001095 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001096 Ty,
Owen Anderson1d0be152009-08-13 21:58:54 +00001097 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1098 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
Devang Patele4b27562009-08-28 23:24:31 +00001099 Val
Chris Lattnera45664f2008-11-10 02:56:27 +00001100 };
Devang Patele4b27562009-08-28 23:24:31 +00001101
1102 Value *const *Vs = &Elts[0];
1103 MDNode *Node = MDNode::get(VMContext,Vs, 12);
1104
1105 // Create a named metadata so that we do not lose this mdnode.
1106 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001107 NMD->addOperand(Node);
Devang Patele4b27562009-08-28 23:24:31 +00001108
1109 return DIGlobalVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +00001110}
1111
Devang Patel27398962010-08-09 21:39:24 +00001112/// CreateGlobalVariable - Create a new descriptor for the specified constant.
1113DIGlobalVariable
1114DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
1115 StringRef DisplayName,
1116 StringRef LinkageName,
1117 DIFile F,
1118 unsigned LineNo, DIType Ty,bool isLocalToUnit,
1119 bool isDefinition, llvm::Constant *Val) {
1120 Value *Elts[] = {
Devang Patelebd53742010-08-11 23:17:54 +00001121 GetTagConstant(dwarf::DW_TAG_variable),
Devang Patel27398962010-08-09 21:39:24 +00001122 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
1123 Context,
1124 MDString::get(VMContext, Name),
1125 MDString::get(VMContext, DisplayName),
1126 MDString::get(VMContext, LinkageName),
1127 F,
1128 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1129 Ty,
1130 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1131 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1132 Val
1133 };
1134
1135 Value *const *Vs = &Elts[0];
1136 MDNode *Node = MDNode::get(VMContext,Vs, 12);
1137
1138 // Create a named metadata so that we do not lose this mdnode.
1139 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
1140 NMD->addOperand(Node);
1141
1142 return DIGlobalVariable(Node);
1143}
Chris Lattnera45664f2008-11-10 02:56:27 +00001144
1145/// CreateVariable - Create a new descriptor for the specified variable.
1146DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +00001147 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +00001148 DIFile F,
1149 unsigned LineNo,
Devang Patel3cf763d2010-09-29 23:07:21 +00001150 DIType Ty, bool AlwaysPreserve,
1151 unsigned Flags) {
Devang Patele4b27562009-08-28 23:24:31 +00001152 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001153 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +00001154 Context,
Devang Patele4b27562009-08-28 23:24:31 +00001155 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +00001156 F,
Owen Anderson1d0be152009-08-13 21:58:54 +00001157 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001158 Ty,
Devang Patel3cf763d2010-09-29 23:07:21 +00001159 ConstantInt::get(Type::getInt32Ty(VMContext), Flags)
Chris Lattnera45664f2008-11-10 02:56:27 +00001160 };
Devang Patel3cf763d2010-09-29 23:07:21 +00001161 MDNode *Node = MDNode::get(VMContext, &Elts[0], 7);
Devang Patel6ed0ce32010-05-20 20:35:24 +00001162 if (AlwaysPreserve) {
1163 // The optimizer may remove local variable. If there is an interest
1164 // to preserve variable info in such situation then stash it in a
1165 // named mdnode.
Devang Patel2f7d5292010-06-16 00:53:55 +00001166 DISubprogram Fn(getDISubprogram(Context));
Devang Patel10de3bb2010-06-21 18:36:58 +00001167 StringRef FName = "fn";
1168 if (Fn.getFunction())
1169 FName = Fn.getFunction()->getName();
Devang Patel10de3bb2010-06-21 18:36:58 +00001170 char One = '\1';
1171 if (FName.startswith(StringRef(&One, 1)))
1172 FName = FName.substr(1);
Dan Gohman17aa92c2010-07-21 23:38:33 +00001173
1174 SmallString<32> Out;
1175 NamedMDNode *FnLocals =
1176 M.getOrInsertNamedMetadata(Twine("llvm.dbg.lv.", FName).toStringRef(Out));
Devang Patel2f7d5292010-06-16 00:53:55 +00001177 FnLocals->addOperand(Node);
Devang Patel98e1cac2010-05-14 21:01:35 +00001178 }
1179 return DIVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +00001180}
1181
1182
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001183/// CreateComplexVariable - Create a new descriptor for the specified variable
1184/// which has a complex address expression for its address.
1185DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
Benjamin Kramer28b4afc2010-09-21 16:00:03 +00001186 StringRef Name, DIFile F,
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001187 unsigned LineNo,
Benjamin Kramer28b4afc2010-09-21 16:00:03 +00001188 DIType Ty, Value *const *Addr,
1189 unsigned NumAddr) {
1190 SmallVector<Value *, 15> Elts;
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001191 Elts.push_back(GetTagConstant(Tag));
Devang Patel2db49d72010-05-07 18:11:54 +00001192 Elts.push_back(Context);
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001193 Elts.push_back(MDString::get(VMContext, Name));
Devang Patel2db49d72010-05-07 18:11:54 +00001194 Elts.push_back(F);
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001195 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
Devang Patel2db49d72010-05-07 18:11:54 +00001196 Elts.push_back(Ty);
Benjamin Kramer28b4afc2010-09-21 16:00:03 +00001197 Elts.append(Addr, Addr+NumAddr);
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001198
Benjamin Kramer28b4afc2010-09-21 16:00:03 +00001199 return DIVariable(MDNode::get(VMContext, Elts.data(), Elts.size()));
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001200}
1201
1202
Chris Lattnera45664f2008-11-10 02:56:27 +00001203/// CreateBlock - This creates a descriptor for a lexical block with the
Owen Anderson99035272009-07-07 17:12:53 +00001204/// specified parent VMContext.
Devang Patel3d821aa2010-02-16 21:39:34 +00001205DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context,
Stuart Hastings0db42712010-07-19 23:56:30 +00001206 DIFile F, unsigned LineNo,
1207 unsigned Col) {
1208 // Defeat MDNode uniqing for lexical blocks.
1209 static unsigned int unique_id = 0;
Devang Patele4b27562009-08-28 23:24:31 +00001210 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001211 GetTagConstant(dwarf::DW_TAG_lexical_block),
Devang Patel2db49d72010-05-07 18:11:54 +00001212 Context,
Devang Patel3d821aa2010-02-16 21:39:34 +00001213 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Stuart Hastings0db42712010-07-19 23:56:30 +00001214 ConstantInt::get(Type::getInt32Ty(VMContext), Col),
1215 F,
1216 ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
Chris Lattnera45664f2008-11-10 02:56:27 +00001217 };
Stuart Hastings0db42712010-07-19 23:56:30 +00001218 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 6));
Chris Lattnera45664f2008-11-10 02:56:27 +00001219}
1220
Devang Patel6404e4e2009-12-15 19:16:48 +00001221/// CreateNameSpace - This creates new descriptor for a namespace
1222/// with the specified parent context.
1223DINameSpace DIFactory::CreateNameSpace(DIDescriptor Context, StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +00001224 DIFile F,
Devang Patel6404e4e2009-12-15 19:16:48 +00001225 unsigned LineNo) {
1226 Value *Elts[] = {
1227 GetTagConstant(dwarf::DW_TAG_namespace),
Devang Patel2db49d72010-05-07 18:11:54 +00001228 Context,
Devang Patel6404e4e2009-12-15 19:16:48 +00001229 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +00001230 F,
Devang Patel6404e4e2009-12-15 19:16:48 +00001231 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1232 };
1233 return DINameSpace(MDNode::get(VMContext, &Elts[0], 5));
1234}
1235
Devang Patelf98d8fe2009-09-01 01:14:15 +00001236/// CreateLocation - Creates a debug info location.
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001237DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
Daniel Dunbara279bc32009-09-20 02:20:51 +00001238 DIScope S, DILocation OrigLoc) {
Devang Patelf98d8fe2009-09-01 01:14:15 +00001239 Value *Elts[] = {
1240 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1241 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001242 S,
1243 OrigLoc,
Devang Patelf98d8fe2009-09-01 01:14:15 +00001244 };
1245 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1246}
1247
Chris Lattnera45664f2008-11-10 02:56:27 +00001248//===----------------------------------------------------------------------===//
1249// DIFactory: Routines for inserting code into a function
1250//===----------------------------------------------------------------------===//
1251
Chris Lattnera45664f2008-11-10 02:56:27 +00001252/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Devang Patel6daf99b2009-11-10 22:05:35 +00001253Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001254 Instruction *InsertBefore) {
Victor Hernandez4cf292a2010-01-26 02:07:38 +00001255 assert(Storage && "no storage passed to dbg.declare");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001256 assert(D.Verify() && "empty DIVariable passed to dbg.declare");
Chris Lattnera45664f2008-11-10 02:56:27 +00001257 if (!DeclareFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +00001258 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1259
Victor Hernandez756462b2010-01-18 20:42:09 +00001260 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
Devang Patel2db49d72010-05-07 18:11:54 +00001261 D };
Devang Patel6daf99b2009-11-10 22:05:35 +00001262 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
Mike Stumpe4250392009-10-01 22:08:58 +00001263}
1264
1265/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Devang Patel6daf99b2009-11-10 22:05:35 +00001266Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001267 BasicBlock *InsertAtEnd) {
Victor Hernandez4cf292a2010-01-26 02:07:38 +00001268 assert(Storage && "no storage passed to dbg.declare");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001269 assert(D.Verify() && "invalid DIVariable passed to dbg.declare");
Mike Stumpe4250392009-10-01 22:08:58 +00001270 if (!DeclareFn)
1271 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1272
Victor Hernandez756462b2010-01-18 20:42:09 +00001273 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
Devang Patel2db49d72010-05-07 18:11:54 +00001274 D };
Devang Patel27a53de2010-01-29 18:30:57 +00001275
1276 // If this block already has a terminator then insert this intrinsic
1277 // before the terminator.
Jim Grosbache62b6902010-07-21 21:36:25 +00001278 if (TerminatorInst *T = InsertAtEnd->getTerminator())
Devang Patel27a53de2010-01-29 18:30:57 +00001279 return CallInst::Create(DeclareFn, Args, Args+2, "", T);
1280 else
1281 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);}
Torok Edwin620f2802008-12-16 09:07:36 +00001282
Victor Hernandezc59b3352009-12-07 21:54:43 +00001283/// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001284Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
Victor Hernandezc59b3352009-12-07 21:54:43 +00001285 DIVariable D,
1286 Instruction *InsertBefore) {
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001287 assert(V && "no value passed to dbg.value");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001288 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001289 if (!ValueFn)
1290 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1291
Victor Hernandezc8b7cd02010-01-20 05:44:11 +00001292 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001293 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
Devang Patel2db49d72010-05-07 18:11:54 +00001294 D };
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001295 return CallInst::Create(ValueFn, Args, Args+3, "", InsertBefore);
1296}
1297
Victor Hernandezc59b3352009-12-07 21:54:43 +00001298/// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001299Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
Victor Hernandezc59b3352009-12-07 21:54:43 +00001300 DIVariable D,
1301 BasicBlock *InsertAtEnd) {
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001302 assert(V && "no value passed to dbg.value");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001303 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001304 if (!ValueFn)
1305 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1306
Jim Grosbache62b6902010-07-21 21:36:25 +00001307 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001308 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
Devang Patel2db49d72010-05-07 18:11:54 +00001309 D };
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001310 return CallInst::Create(ValueFn, Args, Args+3, "", InsertAtEnd);
1311}
Devang Patele4b27562009-08-28 23:24:31 +00001312
Devang Patel1a7ca032010-09-28 18:08:20 +00001313// RecordType - Record DIType in a module such that it is not lost even if
1314// it is not referenced through debug info anchors.
1315void DIFactory::RecordType(DIType T) {
1316 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.ty");
1317 NMD->addOperand(T);
1318}
1319
1320
Devang Pateld2f79a12009-07-28 19:55:13 +00001321//===----------------------------------------------------------------------===//
Devang Patel98c65172009-07-30 18:25:15 +00001322// DebugInfoFinder implementations.
Devang Pateld2f79a12009-07-28 19:55:13 +00001323//===----------------------------------------------------------------------===//
1324
Devang Patel98c65172009-07-30 18:25:15 +00001325/// processModule - Process entire module and collect debug info.
1326void DebugInfoFinder::processModule(Module &M) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001327 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1328 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
1329 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
1330 ++BI) {
Devang Patel01c5ff62010-05-04 01:05:02 +00001331 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
Devang Patelb4d31302009-07-31 18:18:52 +00001332 processDeclare(DDI);
Jim Grosbache62b6902010-07-21 21:36:25 +00001333
Chris Lattner28a9bf62010-04-02 20:44:29 +00001334 DebugLoc Loc = BI->getDebugLoc();
1335 if (Loc.isUnknown())
1336 continue;
Jim Grosbache62b6902010-07-21 21:36:25 +00001337
Chris Lattner28a9bf62010-04-02 20:44:29 +00001338 LLVMContext &Ctx = BI->getContext();
1339 DIDescriptor Scope(Loc.getScope(Ctx));
Jim Grosbache62b6902010-07-21 21:36:25 +00001340
Chris Lattner28a9bf62010-04-02 20:44:29 +00001341 if (Scope.isCompileUnit())
Devang Patel2db49d72010-05-07 18:11:54 +00001342 addCompileUnit(DICompileUnit(Scope));
Chris Lattner28a9bf62010-04-02 20:44:29 +00001343 else if (Scope.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001344 processSubprogram(DISubprogram(Scope));
Chris Lattner28a9bf62010-04-02 20:44:29 +00001345 else if (Scope.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001346 processLexicalBlock(DILexicalBlock(Scope));
Jim Grosbache62b6902010-07-21 21:36:25 +00001347
Chris Lattner28a9bf62010-04-02 20:44:29 +00001348 if (MDNode *IA = Loc.getInlinedAt(Ctx))
1349 processLocation(DILocation(IA));
Devang Pateld2f79a12009-07-28 19:55:13 +00001350 }
Devang Patele4b27562009-08-28 23:24:31 +00001351
Devang Patelfd5fdc32010-06-28 05:53:08 +00001352 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
1353 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1354 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
1355 if (addGlobalVariable(DIG)) {
1356 addCompileUnit(DIG.getCompileUnit());
1357 processType(DIG.getType());
1358 }
Devang Pateld2f79a12009-07-28 19:55:13 +00001359 }
1360 }
Devang Patelfd5fdc32010-06-28 05:53:08 +00001361
1362 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
1363 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
1364 processSubprogram(DISubprogram(NMD->getOperand(i)));
Devang Pateld2f79a12009-07-28 19:55:13 +00001365}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001366
Devang Patel6daf99b2009-11-10 22:05:35 +00001367/// processLocation - Process DILocation.
1368void DebugInfoFinder::processLocation(DILocation Loc) {
Devang Patel3c91b052010-03-08 20:52:55 +00001369 if (!Loc.Verify()) return;
Devang Patel2db49d72010-05-07 18:11:54 +00001370 DIDescriptor S(Loc.getScope());
Devang Patel6daf99b2009-11-10 22:05:35 +00001371 if (S.isCompileUnit())
Devang Patel2db49d72010-05-07 18:11:54 +00001372 addCompileUnit(DICompileUnit(S));
Devang Patel6daf99b2009-11-10 22:05:35 +00001373 else if (S.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001374 processSubprogram(DISubprogram(S));
Devang Patel6daf99b2009-11-10 22:05:35 +00001375 else if (S.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001376 processLexicalBlock(DILexicalBlock(S));
Devang Patel6daf99b2009-11-10 22:05:35 +00001377 processLocation(Loc.getOrigLocation());
1378}
1379
Devang Patel98c65172009-07-30 18:25:15 +00001380/// processType - Process DIType.
1381void DebugInfoFinder::processType(DIType DT) {
Devang Patel72bcdb62009-08-10 22:09:58 +00001382 if (!addType(DT))
Devang Pateld2f79a12009-07-28 19:55:13 +00001383 return;
1384
1385 addCompileUnit(DT.getCompileUnit());
Devang Patel6ceea332009-08-31 18:49:10 +00001386 if (DT.isCompositeType()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001387 DICompositeType DCT(DT);
Devang Patel98c65172009-07-30 18:25:15 +00001388 processType(DCT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001389 DIArray DA = DCT.getTypeArray();
Devang Patel3c91b052010-03-08 20:52:55 +00001390 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1391 DIDescriptor D = DA.getElement(i);
1392 if (D.isType())
Devang Patel2db49d72010-05-07 18:11:54 +00001393 processType(DIType(D));
Devang Patel3c91b052010-03-08 20:52:55 +00001394 else if (D.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001395 processSubprogram(DISubprogram(D));
Devang Patel3c91b052010-03-08 20:52:55 +00001396 }
Devang Patel6ceea332009-08-31 18:49:10 +00001397 } else if (DT.isDerivedType()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001398 DIDerivedType DDT(DT);
Devang Patel3c91b052010-03-08 20:52:55 +00001399 processType(DDT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001400 }
1401}
1402
Devang Patelbeab41b2009-10-07 22:04:08 +00001403/// processLexicalBlock
1404void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
Devang Patelbeab41b2009-10-07 22:04:08 +00001405 DIScope Context = LB.getContext();
1406 if (Context.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001407 return processLexicalBlock(DILexicalBlock(Context));
Devang Patelbeab41b2009-10-07 22:04:08 +00001408 else
Devang Patel2db49d72010-05-07 18:11:54 +00001409 return processSubprogram(DISubprogram(Context));
Devang Patelbeab41b2009-10-07 22:04:08 +00001410}
1411
Devang Patel98c65172009-07-30 18:25:15 +00001412/// processSubprogram - Process DISubprogram.
1413void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001414 if (!addSubprogram(SP))
1415 return;
1416 addCompileUnit(SP.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +00001417 processType(SP.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +00001418}
1419
Devang Patelb4d31302009-07-31 18:18:52 +00001420/// processDeclare - Process DbgDeclareInst.
1421void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
Devang Patel3c91b052010-03-08 20:52:55 +00001422 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1423 if (!N) return;
1424
1425 DIDescriptor DV(N);
1426 if (!DV.isVariable())
Devang Patelb4d31302009-07-31 18:18:52 +00001427 return;
1428
Devang Patel2db49d72010-05-07 18:11:54 +00001429 if (!NodesSeen.insert(DV))
Devang Patelb4d31302009-07-31 18:18:52 +00001430 return;
1431
Devang Patel3c91b052010-03-08 20:52:55 +00001432 addCompileUnit(DIVariable(N).getCompileUnit());
1433 processType(DIVariable(N).getType());
Devang Patelb4d31302009-07-31 18:18:52 +00001434}
1435
Devang Patel72bcdb62009-08-10 22:09:58 +00001436/// addType - Add type into Tys.
1437bool DebugInfoFinder::addType(DIType DT) {
Devang Patel3c91b052010-03-08 20:52:55 +00001438 if (!DT.isValid())
Devang Patel72bcdb62009-08-10 22:09:58 +00001439 return false;
1440
Devang Patel2db49d72010-05-07 18:11:54 +00001441 if (!NodesSeen.insert(DT))
Devang Patel72bcdb62009-08-10 22:09:58 +00001442 return false;
1443
Devang Patel2db49d72010-05-07 18:11:54 +00001444 TYs.push_back(DT);
Devang Patel72bcdb62009-08-10 22:09:58 +00001445 return true;
1446}
1447
Devang Pateld2f79a12009-07-28 19:55:13 +00001448/// addCompileUnit - Add compile unit into CUs.
Devang Patel98c65172009-07-30 18:25:15 +00001449bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Patel3c91b052010-03-08 20:52:55 +00001450 if (!CU.Verify())
Devang Pateld2f79a12009-07-28 19:55:13 +00001451 return false;
1452
Devang Patel2db49d72010-05-07 18:11:54 +00001453 if (!NodesSeen.insert(CU))
Devang Pateld2f79a12009-07-28 19:55:13 +00001454 return false;
1455
Devang Patel2db49d72010-05-07 18:11:54 +00001456 CUs.push_back(CU);
Devang Pateld2f79a12009-07-28 19:55:13 +00001457 return true;
1458}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001459
Devang Pateld2f79a12009-07-28 19:55:13 +00001460/// addGlobalVariable - Add global variable into GVs.
Devang Patel98c65172009-07-30 18:25:15 +00001461bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Patel2db49d72010-05-07 18:11:54 +00001462 if (!DIDescriptor(DIG).isGlobalVariable())
Devang Pateld2f79a12009-07-28 19:55:13 +00001463 return false;
1464
Devang Patel2db49d72010-05-07 18:11:54 +00001465 if (!NodesSeen.insert(DIG))
Devang Pateld2f79a12009-07-28 19:55:13 +00001466 return false;
1467
Devang Patel2db49d72010-05-07 18:11:54 +00001468 GVs.push_back(DIG);
Devang Pateld2f79a12009-07-28 19:55:13 +00001469 return true;
1470}
1471
1472// addSubprogram - Add subprgoram into SPs.
Devang Patel98c65172009-07-30 18:25:15 +00001473bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Patel2db49d72010-05-07 18:11:54 +00001474 if (!DIDescriptor(SP).isSubprogram())
Devang Pateld2f79a12009-07-28 19:55:13 +00001475 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001476
Devang Patel2db49d72010-05-07 18:11:54 +00001477 if (!NodesSeen.insert(SP))
Devang Pateld2f79a12009-07-28 19:55:13 +00001478 return false;
1479
Devang Patel2db49d72010-05-07 18:11:54 +00001480 SPs.push_back(SP);
Devang Pateld2f79a12009-07-28 19:55:13 +00001481 return true;
1482}
1483
Victor Hernandez756462b2010-01-18 20:42:09 +00001484/// Find the debug info descriptor corresponding to this global variable.
1485static Value *findDbgGlobalDeclare(GlobalVariable *V) {
Chris Lattner099b7792009-12-29 09:22:47 +00001486 const Module *M = V->getParent();
1487 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1488 if (!NMD)
Torok Edwinff7d0e92009-03-10 13:41:26 +00001489 return 0;
Chris Lattner099b7792009-12-29 09:22:47 +00001490
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001491 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman872814a2010-07-21 18:54:18 +00001492 DIDescriptor DIG(cast<MDNode>(NMD->getOperand(i)));
Devang Patel3c91b052010-03-08 20:52:55 +00001493 if (!DIG.isGlobalVariable())
Chris Lattner099b7792009-12-29 09:22:47 +00001494 continue;
Devang Patel2db49d72010-05-07 18:11:54 +00001495 if (DIGlobalVariable(DIG).getGlobal() == V)
1496 return DIG;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001497 }
Chris Lattner099b7792009-12-29 09:22:47 +00001498 return 0;
1499}
Torok Edwinff7d0e92009-03-10 13:41:26 +00001500
Chris Lattner099b7792009-12-29 09:22:47 +00001501/// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
1502/// It looks through pointer casts too.
Victor Hernandez756462b2010-01-18 20:42:09 +00001503static const DbgDeclareInst *findDbgDeclare(const Value *V) {
Victor Hernandez3a328652010-01-15 19:04:09 +00001504 V = V->stripPointerCasts();
Jim Grosbache62b6902010-07-21 21:36:25 +00001505
Victor Hernandez3a328652010-01-15 19:04:09 +00001506 if (!isa<Instruction>(V) && !isa<Argument>(V))
Torok Edwin620f2802008-12-16 09:07:36 +00001507 return 0;
Jim Grosbache62b6902010-07-21 21:36:25 +00001508
Victor Hernandez3a328652010-01-15 19:04:09 +00001509 const Function *F = NULL;
1510 if (const Instruction *I = dyn_cast<Instruction>(V))
1511 F = I->getParent()->getParent();
1512 else if (const Argument *A = dyn_cast<Argument>(V))
1513 F = A->getParent();
Jim Grosbache62b6902010-07-21 21:36:25 +00001514
Victor Hernandez3a328652010-01-15 19:04:09 +00001515 for (Function::const_iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
1516 for (BasicBlock::const_iterator BI = (*FI).begin(), BE = (*FI).end();
1517 BI != BE; ++BI)
1518 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1519 if (DDI->getAddress() == V)
1520 return DDI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001521
Chris Lattner099b7792009-12-29 09:22:47 +00001522 return 0;
1523}
Bill Wendlingdc817b62009-05-14 18:26:15 +00001524
Chris Lattner099b7792009-12-29 09:22:47 +00001525bool llvm::getLocationInfo(const Value *V, std::string &DisplayName,
1526 std::string &Type, unsigned &LineNo,
1527 std::string &File, std::string &Dir) {
1528 DICompileUnit Unit;
1529 DIType TypeD;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001530
Chris Lattner099b7792009-12-29 09:22:47 +00001531 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1532 Value *DIGV = findDbgGlobalDeclare(GV);
1533 if (!DIGV) return false;
1534 DIGlobalVariable Var(cast<MDNode>(DIGV));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001535
Chris Lattner099b7792009-12-29 09:22:47 +00001536 StringRef D = Var.getDisplayName();
Devang Patel65dbc902009-11-25 17:36:49 +00001537 if (!D.empty())
Chris Lattner099b7792009-12-29 09:22:47 +00001538 DisplayName = D;
1539 LineNo = Var.getLineNumber();
1540 Unit = Var.getCompileUnit();
1541 TypeD = Var.getType();
1542 } else {
1543 const DbgDeclareInst *DDI = findDbgDeclare(V);
1544 if (!DDI) return false;
1545 DIVariable Var(cast<MDNode>(DDI->getVariable()));
1546
1547 StringRef D = Var.getName();
1548 if (!D.empty())
1549 DisplayName = D;
1550 LineNo = Var.getLineNumber();
1551 Unit = Var.getCompileUnit();
1552 TypeD = Var.getType();
Torok Edwinff7d0e92009-03-10 13:41:26 +00001553 }
Devang Patel13e16b62009-06-26 01:49:18 +00001554
Chris Lattner099b7792009-12-29 09:22:47 +00001555 StringRef T = TypeD.getName();
1556 if (!T.empty())
1557 Type = T;
1558 StringRef F = Unit.getFilename();
1559 if (!F.empty())
1560 File = F;
1561 StringRef D = Unit.getDirectory();
1562 if (!D.empty())
1563 Dir = D;
1564 return true;
1565}
Devang Patel9e529c32009-07-02 01:15:24 +00001566
Chris Lattner099b7792009-12-29 09:22:47 +00001567/// getDISubprogram - Find subprogram that is enclosing this scope.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001568DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
Chris Lattner099b7792009-12-29 09:22:47 +00001569 DIDescriptor D(Scope);
Chris Lattner099b7792009-12-29 09:22:47 +00001570 if (D.isSubprogram())
1571 return DISubprogram(Scope);
Jim Grosbache62b6902010-07-21 21:36:25 +00001572
Chris Lattner099b7792009-12-29 09:22:47 +00001573 if (D.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001574 return getDISubprogram(DILexicalBlock(Scope).getContext());
Jim Grosbache62b6902010-07-21 21:36:25 +00001575
Chris Lattner099b7792009-12-29 09:22:47 +00001576 return DISubprogram();
1577}
Devang Patel193f7202009-11-24 01:14:22 +00001578
Chris Lattner099b7792009-12-29 09:22:47 +00001579/// getDICompositeType - Find underlying composite type.
1580DICompositeType llvm::getDICompositeType(DIType T) {
Chris Lattner099b7792009-12-29 09:22:47 +00001581 if (T.isCompositeType())
Devang Patel2db49d72010-05-07 18:11:54 +00001582 return DICompositeType(T);
Jim Grosbache62b6902010-07-21 21:36:25 +00001583
Chris Lattner099b7792009-12-29 09:22:47 +00001584 if (T.isDerivedType())
Devang Patel2db49d72010-05-07 18:11:54 +00001585 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
Jim Grosbache62b6902010-07-21 21:36:25 +00001586
Chris Lattner099b7792009-12-29 09:22:47 +00001587 return DICompositeType();
Torok Edwin620f2802008-12-16 09:07:36 +00001588}