blob: aacbc11e4d7524b8385b7c41a4aa9bff568c64ec [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
Devang Pateld6747df2010-10-06 20:50:40 +0000202/// isUnspecifiedParmeter - Return true if the specified tab is
203/// DW_TAG_unspecified_parameters.
204bool DIDescriptor::isUnspecifiedParameter() const {
205 return DbgNode && getTag() == dwarf::DW_TAG_unspecified_parameters;
206}
207
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000208/// isScope - Return true if the specified tag is one of the scope
Devang Patel43d98b32009-08-31 20:44:45 +0000209/// related tag.
210bool DIDescriptor::isScope() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000211 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000212 switch (getTag()) {
213 case dwarf::DW_TAG_compile_unit:
214 case dwarf::DW_TAG_lexical_block:
215 case dwarf::DW_TAG_subprogram:
216 case dwarf::DW_TAG_namespace:
217 return true;
218 default:
219 break;
Devang Patel43d98b32009-08-31 20:44:45 +0000220 }
221 return false;
222}
Devang Patel6ceea332009-08-31 18:49:10 +0000223
Devang Patelc9f322d2009-08-31 21:34:44 +0000224/// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
225bool DIDescriptor::isCompileUnit() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000226 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
Devang Patelc9f322d2009-08-31 21:34:44 +0000227}
228
Devang Patel7aa81892010-03-08 22:27:22 +0000229/// isFile - Return true if the specified tag is DW_TAG_file_type.
230bool DIDescriptor::isFile() const {
231 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
232}
233
Devang Patel6404e4e2009-12-15 19:16:48 +0000234/// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
235bool DIDescriptor::isNameSpace() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000236 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
Devang Patel6404e4e2009-12-15 19:16:48 +0000237}
238
Devang Patel5e005d82009-08-31 22:00:15 +0000239/// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
240bool DIDescriptor::isLexicalBlock() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000241 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block;
Devang Patel5e005d82009-08-31 22:00:15 +0000242}
243
Devang Patelecbeb1a2009-09-30 22:34:41 +0000244/// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
245bool DIDescriptor::isSubrange() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000246 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
Devang Patelecbeb1a2009-09-30 22:34:41 +0000247}
248
249/// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
250bool DIDescriptor::isEnumerator() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000251 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
Devang Patelecbeb1a2009-09-30 22:34:41 +0000252}
253
Devang Patel6ceea332009-08-31 18:49:10 +0000254//===----------------------------------------------------------------------===//
255// Simple Descriptor Constructors and other Methods
256//===----------------------------------------------------------------------===//
257
Devang Patele9f8f5e2010-05-07 20:54:48 +0000258DIType::DIType(const MDNode *N) : DIScope(N) {
Devang Patel6ceea332009-08-31 18:49:10 +0000259 if (!N) return;
260 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
261 DbgNode = 0;
262 }
263}
264
Devang Patel68afdc32009-01-05 18:33:01 +0000265unsigned DIArray::getNumElements() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000266 if (!DbgNode)
267 return 0;
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000268 return DbgNode->getNumOperands();
Devang Patel68afdc32009-01-05 18:33:01 +0000269}
Chris Lattnera45664f2008-11-10 02:56:27 +0000270
Devang Patelc4999d72009-07-22 18:23:44 +0000271/// replaceAllUsesWith - Replace all uses of debug info referenced by
Dan Gohman872814a2010-07-21 18:54:18 +0000272/// this descriptor.
Dan Gohman489b29b2010-08-20 22:02:26 +0000273void DIType::replaceAllUsesWith(DIDescriptor &D) {
Devang Patel3c91b052010-03-08 20:52:55 +0000274 if (!DbgNode)
Devang Patelc4999d72009-07-22 18:23:44 +0000275 return;
276
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000277 // Since we use a TrackingVH for the node, its easy for clients to manufacture
278 // legitimate situations where they want to replaceAllUsesWith() on something
279 // which, due to uniquing, has merged with the source. We shield clients from
280 // this detail by allowing a value to be replaced with replaceAllUsesWith()
281 // itself.
Devang Pateled66bf52010-05-07 18:19:32 +0000282 if (DbgNode != D) {
Devang Patele9f8f5e2010-05-07 20:54:48 +0000283 MDNode *Node = const_cast<MDNode*>(DbgNode);
284 const MDNode *DN = D;
285 const Value *V = cast_or_null<Value>(DN);
286 Node->replaceAllUsesWith(const_cast<Value*>(V));
Dan Gohman489b29b2010-08-20 22:02:26 +0000287 MDNode::deleteTemporary(Node);
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000288 }
Devang Patelc4999d72009-07-22 18:23:44 +0000289}
290
Devang Patelb79b5352009-01-19 23:21:49 +0000291/// Verify - Verify that a compile unit is well formed.
292bool DICompileUnit::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000293 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000294 return false;
Devang Patel65dbc902009-11-25 17:36:49 +0000295 StringRef N = getFilename();
296 if (N.empty())
Bill Wendling0582ae92009-03-13 04:39:26 +0000297 return false;
Devang Patelb79b5352009-01-19 23:21:49 +0000298 // It is possible that directory and produce string is empty.
Bill Wendling0582ae92009-03-13 04:39:26 +0000299 return true;
Devang Patelb79b5352009-01-19 23:21:49 +0000300}
301
302/// Verify - Verify that a type descriptor is well formed.
303bool DIType::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000304 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000305 return false;
Devang Patel3c91b052010-03-08 20:52:55 +0000306 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000307 return false;
Devang Patelb71bbf92010-11-02 20:41:13 +0000308 unsigned Tag = getTag();
309 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
310 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
311 Tag != dwarf::DW_TAG_restrict_type && getFilename().empty())
Devang Patelb79b5352009-01-19 23:21:49 +0000312 return false;
313 return true;
314}
315
Devang Patel0c4720c2010-08-23 18:25:56 +0000316/// Verify - Verify that a basic type descriptor is well formed.
317bool DIBasicType::Verify() const {
318 return isBasicType();
319}
320
321/// Verify - Verify that a derived type descriptor is well formed.
322bool DIDerivedType::Verify() const {
323 return isDerivedType();
324}
325
Devang Patelb79b5352009-01-19 23:21:49 +0000326/// Verify - Verify that a composite type descriptor is well formed.
327bool DICompositeType::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000328 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000329 return false;
Devang Patel3c91b052010-03-08 20:52:55 +0000330 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000331 return false;
332
333 DICompileUnit CU = getCompileUnit();
Devang Patel3c91b052010-03-08 20:52:55 +0000334 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000335 return false;
336 return true;
337}
338
339/// Verify - Verify that a subprogram descriptor is well formed.
340bool DISubprogram::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000341 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000342 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000343
Devang Patel3c91b052010-03-08 20:52:55 +0000344 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000345 return false;
346
347 DICompileUnit CU = getCompileUnit();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000348 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000349 return false;
350
351 DICompositeType Ty = getType();
Devang Patel3c91b052010-03-08 20:52:55 +0000352 if (!Ty.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000353 return false;
354 return true;
355}
356
357/// Verify - Verify that a global variable descriptor is well formed.
358bool DIGlobalVariable::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000359 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000360 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000361
Devang Patel65dbc902009-11-25 17:36:49 +0000362 if (getDisplayName().empty())
Devang Patel84c73e92009-11-06 17:58:12 +0000363 return false;
364
Devang Patel3c91b052010-03-08 20:52:55 +0000365 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000366 return false;
367
368 DICompileUnit CU = getCompileUnit();
Devang Patel3c91b052010-03-08 20:52:55 +0000369 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000370 return false;
371
372 DIType Ty = getType();
373 if (!Ty.Verify())
374 return false;
375
Devang Patel27398962010-08-09 21:39:24 +0000376 if (!getGlobal() && !getConstant())
Devang Patelb79b5352009-01-19 23:21:49 +0000377 return false;
378
379 return true;
380}
381
382/// Verify - Verify that a variable descriptor is well formed.
383bool DIVariable::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000384 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000385 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000386
Devang Patel3c91b052010-03-08 20:52:55 +0000387 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000388 return false;
389
Devang Patel62077af2010-05-07 21:42:24 +0000390 if (!getCompileUnit().Verify())
391 return false;
392
Devang Patelb79b5352009-01-19 23:21:49 +0000393 DIType Ty = getType();
394 if (!Ty.Verify())
395 return false;
396
Devang Patelb79b5352009-01-19 23:21:49 +0000397 return true;
398}
399
Devang Patel3c91b052010-03-08 20:52:55 +0000400/// Verify - Verify that a location descriptor is well formed.
401bool DILocation::Verify() const {
402 if (!DbgNode)
403 return false;
Jim Grosbache62b6902010-07-21 21:36:25 +0000404
Devang Patel3c91b052010-03-08 20:52:55 +0000405 return DbgNode->getNumOperands() == 4;
406}
407
Devang Patel47e22652010-05-07 23:04:32 +0000408/// Verify - Verify that a namespace descriptor is well formed.
409bool DINameSpace::Verify() const {
410 if (!DbgNode)
411 return false;
412 if (getName().empty())
413 return false;
414 if (!getCompileUnit().Verify())
415 return false;
416 return true;
417}
418
Devang Patel36375ee2009-02-17 21:23:59 +0000419/// getOriginalTypeSize - If this type is derived from a base type then
420/// return base type size.
421uint64_t DIDerivedType::getOriginalTypeSize() const {
Devang Patel61ecbd12009-11-04 23:48:00 +0000422 unsigned Tag = getTag();
423 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
424 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
425 Tag == dwarf::DW_TAG_restrict_type) {
426 DIType BaseType = getTypeDerivedFrom();
Jim Grosbache62b6902010-07-21 21:36:25 +0000427 // If this type is not derived from any type then take conservative
Devang Patel5ebfa2d2009-11-06 18:24:05 +0000428 // approach.
Devang Patel3c91b052010-03-08 20:52:55 +0000429 if (!BaseType.isValid())
Devang Patel5ebfa2d2009-11-06 18:24:05 +0000430 return getSizeInBits();
Devang Patel61ecbd12009-11-04 23:48:00 +0000431 if (BaseType.isDerivedType())
Devang Patel2db49d72010-05-07 18:11:54 +0000432 return DIDerivedType(BaseType).getOriginalTypeSize();
Devang Patel61ecbd12009-11-04 23:48:00 +0000433 else
434 return BaseType.getSizeInBits();
435 }
Jim Grosbache62b6902010-07-21 21:36:25 +0000436
Devang Patel61ecbd12009-11-04 23:48:00 +0000437 return getSizeInBits();
Devang Patel36375ee2009-02-17 21:23:59 +0000438}
Devang Patelb79b5352009-01-19 23:21:49 +0000439
Jim Grosbache62b6902010-07-21 21:36:25 +0000440/// isInlinedFnArgument - Return true if this variable provides debugging
Devang Patel719f6a92010-04-29 20:40:36 +0000441/// information for an inlined function arguments.
442bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
443 assert(CurFn && "Invalid function");
444 if (!getContext().isSubprogram())
445 return false;
Jim Grosbache62b6902010-07-21 21:36:25 +0000446 // This variable is not inlined function argument if its scope
Devang Patel719f6a92010-04-29 20:40:36 +0000447 // does not describe current function.
Devang Patel2db49d72010-05-07 18:11:54 +0000448 return !(DISubprogram(getContext()).describes(CurFn));
Devang Patel719f6a92010-04-29 20:40:36 +0000449}
450
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000451/// describes - Return true if this subprogram provides debugging
452/// information for the function F.
453bool DISubprogram::describes(const Function *F) {
Chris Lattner099b7792009-12-29 09:22:47 +0000454 assert(F && "Invalid function");
Devang Patelffd33cd2010-06-16 06:42:02 +0000455 if (F == getFunction())
456 return true;
Devang Patel65dbc902009-11-25 17:36:49 +0000457 StringRef Name = getLinkageName();
458 if (Name.empty())
Devang Patel5ccdd102009-09-29 18:40:58 +0000459 Name = getName();
Devang Patel65dbc902009-11-25 17:36:49 +0000460 if (F->getName() == Name)
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000461 return true;
462 return false;
463}
464
Jim Grosbache62b6902010-07-21 21:36:25 +0000465unsigned DISubprogram::isOptimized() const {
Devang Patelccff8122010-04-30 19:38:23 +0000466 assert (DbgNode && "Invalid subprogram descriptor!");
467 if (DbgNode->getNumOperands() == 16)
468 return getUnsignedField(15);
469 return 0;
470}
471
Devang Patel65dbc902009-11-25 17:36:49 +0000472StringRef DIScope::getFilename() const {
Devang Patel77bf2952010-03-08 22:02:50 +0000473 if (!DbgNode)
474 return StringRef();
Jim Grosbache62b6902010-07-21 21:36:25 +0000475 if (isLexicalBlock())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000476 return DILexicalBlock(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000477 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000478 return DISubprogram(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000479 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000480 return DICompileUnit(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000481 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000482 return DINameSpace(DbgNode).getFilename();
Devang Patel77bf2952010-03-08 22:02:50 +0000483 if (isType())
484 return DIType(DbgNode).getFilename();
Devang Patel7aa81892010-03-08 22:27:22 +0000485 if (isFile())
486 return DIFile(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000487 assert(0 && "Invalid DIScope!");
Devang Patel65dbc902009-11-25 17:36:49 +0000488 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000489}
490
Devang Patel65dbc902009-11-25 17:36:49 +0000491StringRef DIScope::getDirectory() const {
Devang Patel77bf2952010-03-08 22:02:50 +0000492 if (!DbgNode)
493 return StringRef();
Jim Grosbache62b6902010-07-21 21:36:25 +0000494 if (isLexicalBlock())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000495 return DILexicalBlock(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000496 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000497 return DISubprogram(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000498 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000499 return DICompileUnit(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000500 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000501 return DINameSpace(DbgNode).getDirectory();
Devang Patel77bf2952010-03-08 22:02:50 +0000502 if (isType())
503 return DIType(DbgNode).getDirectory();
Devang Patel7aa81892010-03-08 22:27:22 +0000504 if (isFile())
505 return DIFile(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000506 assert(0 && "Invalid DIScope!");
Devang Patel65dbc902009-11-25 17:36:49 +0000507 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000508}
509
Chris Lattnera45664f2008-11-10 02:56:27 +0000510//===----------------------------------------------------------------------===//
Devang Patel7136a652009-07-01 22:10:23 +0000511// DIDescriptor: dump routines for all descriptors.
512//===----------------------------------------------------------------------===//
513
514
Dan Gohman50404362010-05-07 15:30:29 +0000515/// print - Print descriptor.
516void DIDescriptor::print(raw_ostream &OS) const {
517 OS << "[" << dwarf::TagString(getTag()) << "] ";
518 OS.write_hex((intptr_t) &*DbgNode) << ']';
Devang Patel7136a652009-07-01 22:10:23 +0000519}
520
Dan Gohman50404362010-05-07 15:30:29 +0000521/// print - Print compile unit.
522void DICompileUnit::print(raw_ostream &OS) const {
Devang Patel7136a652009-07-01 22:10:23 +0000523 if (getLanguage())
Dan Gohman50404362010-05-07 15:30:29 +0000524 OS << " [" << dwarf::LanguageString(getLanguage()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000525
Dan Gohmanfaa19c32010-05-10 20:07:44 +0000526 OS << " [" << getDirectory() << "/" << getFilename() << "]";
Devang Patel7136a652009-07-01 22:10:23 +0000527}
528
Dan Gohman50404362010-05-07 15:30:29 +0000529/// print - Print type.
530void DIType::print(raw_ostream &OS) const {
Devang Patel3c91b052010-03-08 20:52:55 +0000531 if (!DbgNode) return;
Devang Patel7136a652009-07-01 22:10:23 +0000532
Devang Patel65dbc902009-11-25 17:36:49 +0000533 StringRef Res = getName();
534 if (!Res.empty())
Dan Gohman50404362010-05-07 15:30:29 +0000535 OS << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000536
537 unsigned Tag = getTag();
Dan Gohman50404362010-05-07 15:30:29 +0000538 OS << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000539
540 // TODO : Print context
Dan Gohmanc014d092010-05-07 16:17:22 +0000541 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000542 OS << " ["
Dan Gohman9a7063e2010-05-07 16:39:27 +0000543 << "line " << getLineNumber() << ", "
544 << getSizeInBits() << " bits, "
545 << getAlignInBits() << " bit alignment, "
546 << getOffsetInBits() << " bit offset"
Chris Lattnera81d29b2009-08-23 07:33:14 +0000547 << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000548
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000549 if (isPrivate())
Dan Gohman50404362010-05-07 15:30:29 +0000550 OS << " [private] ";
Devang Patel7136a652009-07-01 22:10:23 +0000551 else if (isProtected())
Dan Gohman50404362010-05-07 15:30:29 +0000552 OS << " [protected] ";
Devang Patel7136a652009-07-01 22:10:23 +0000553
554 if (isForwardDecl())
Dan Gohman50404362010-05-07 15:30:29 +0000555 OS << " [fwd] ";
Devang Patel7136a652009-07-01 22:10:23 +0000556
Devang Patel6ceea332009-08-31 18:49:10 +0000557 if (isBasicType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000558 DIBasicType(DbgNode).print(OS);
Devang Patel6ceea332009-08-31 18:49:10 +0000559 else if (isDerivedType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000560 DIDerivedType(DbgNode).print(OS);
Devang Patel6ceea332009-08-31 18:49:10 +0000561 else if (isCompositeType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000562 DICompositeType(DbgNode).print(OS);
Devang Patel7136a652009-07-01 22:10:23 +0000563 else {
Dan Gohman50404362010-05-07 15:30:29 +0000564 OS << "Invalid DIType\n";
Devang Patel7136a652009-07-01 22:10:23 +0000565 return;
566 }
567
Dan Gohman50404362010-05-07 15:30:29 +0000568 OS << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000569}
570
Dan Gohman50404362010-05-07 15:30:29 +0000571/// print - Print basic type.
572void DIBasicType::print(raw_ostream &OS) const {
573 OS << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000574}
575
Dan Gohman50404362010-05-07 15:30:29 +0000576/// print - Print derived type.
577void DIDerivedType::print(raw_ostream &OS) const {
Dan Gohmanc014d092010-05-07 16:17:22 +0000578 OS << "\n\t Derived From: "; getTypeDerivedFrom().print(OS);
Devang Patel7136a652009-07-01 22:10:23 +0000579}
580
Dan Gohman50404362010-05-07 15:30:29 +0000581/// print - Print composite type.
582void DICompositeType::print(raw_ostream &OS) const {
Devang Patel7136a652009-07-01 22:10:23 +0000583 DIArray A = getTypeArray();
Dan Gohman50404362010-05-07 15:30:29 +0000584 OS << " [" << A.getNumElements() << " elements]";
Devang Patel7136a652009-07-01 22:10:23 +0000585}
586
Dan Gohman50404362010-05-07 15:30:29 +0000587/// print - Print subprogram.
588void DISubprogram::print(raw_ostream &OS) const {
Devang Patel65dbc902009-11-25 17:36:49 +0000589 StringRef Res = getName();
590 if (!Res.empty())
Dan Gohman50404362010-05-07 15:30:29 +0000591 OS << " [" << Res << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000592
593 unsigned Tag = getTag();
Dan Gohman50404362010-05-07 15:30:29 +0000594 OS << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000595
596 // TODO : Print context
Dan Gohmanc014d092010-05-07 16:17:22 +0000597 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000598 OS << " [" << getLineNumber() << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000599
600 if (isLocalToUnit())
Dan Gohman50404362010-05-07 15:30:29 +0000601 OS << " [local] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000602
603 if (isDefinition())
Dan Gohman50404362010-05-07 15:30:29 +0000604 OS << " [def] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000605
Dan Gohman50404362010-05-07 15:30:29 +0000606 OS << "\n";
607}
608
609/// print - Print global variable.
610void DIGlobalVariable::print(raw_ostream &OS) const {
611 OS << " [";
Devang Patela49d8772010-05-07 23:19:07 +0000612 StringRef Res = getName();
613 if (!Res.empty())
614 OS << " [" << Res << "] ";
615
616 unsigned Tag = getTag();
617 OS << " [" << dwarf::TagString(Tag) << "] ";
618
619 // TODO : Print context
620 getCompileUnit().print(OS);
621 OS << " [" << getLineNumber() << "] ";
622
623 if (isLocalToUnit())
624 OS << " [local] ";
625
626 if (isDefinition())
627 OS << " [def] ";
628
629 if (isGlobalVariable())
630 DIGlobalVariable(DbgNode).print(OS);
631 OS << "]\n";
Dan Gohman50404362010-05-07 15:30:29 +0000632}
633
634/// print - Print variable.
635void DIVariable::print(raw_ostream &OS) const {
636 StringRef Res = getName();
637 if (!Res.empty())
638 OS << " [" << Res << "] ";
639
Dan Gohmanc014d092010-05-07 16:17:22 +0000640 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000641 OS << " [" << getLineNumber() << "] ";
Dan Gohmanc014d092010-05-07 16:17:22 +0000642 getType().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000643 OS << "\n";
644
645 // FIXME: Dump complex addresses
646}
647
648/// dump - Print descriptor to dbgs() with a newline.
649void DIDescriptor::dump() const {
650 print(dbgs()); dbgs() << '\n';
651}
652
653/// dump - Print compile unit to dbgs() with a newline.
654void DICompileUnit::dump() const {
655 print(dbgs()); dbgs() << '\n';
656}
657
658/// dump - Print type to dbgs() with a newline.
659void DIType::dump() const {
660 print(dbgs()); dbgs() << '\n';
661}
662
663/// dump - Print basic type to dbgs() with a newline.
664void DIBasicType::dump() const {
665 print(dbgs()); dbgs() << '\n';
666}
667
668/// dump - Print derived type to dbgs() with a newline.
669void DIDerivedType::dump() const {
670 print(dbgs()); dbgs() << '\n';
671}
672
673/// dump - Print composite type to dbgs() with a newline.
674void DICompositeType::dump() const {
675 print(dbgs()); dbgs() << '\n';
676}
677
Dan Gohman50404362010-05-07 15:30:29 +0000678/// dump - Print subprogram to dbgs() with a newline.
679void DISubprogram::dump() const {
680 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000681}
682
683/// dump - Print global variable.
684void DIGlobalVariable::dump() const {
Dan Gohman50404362010-05-07 15:30:29 +0000685 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000686}
687
688/// dump - Print variable.
689void DIVariable::dump() const {
Dan Gohman50404362010-05-07 15:30:29 +0000690 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000691}
692
693//===----------------------------------------------------------------------===//
Chris Lattnera45664f2008-11-10 02:56:27 +0000694// DIFactory: Basic Helpers
695//===----------------------------------------------------------------------===//
696
Bill Wendlingdc817b62009-05-14 18:26:15 +0000697DIFactory::DIFactory(Module &m)
Victor Hernandez06203122010-01-23 00:03:28 +0000698 : M(m), VMContext(M.getContext()), DeclareFn(0), ValueFn(0) {}
Chris Lattner497a7a82008-11-10 04:10:34 +0000699
Chris Lattnera45664f2008-11-10 02:56:27 +0000700Constant *DIFactory::GetTagConstant(unsigned TAG) {
Devang Patel6906ba52009-01-20 19:22:03 +0000701 assert((TAG & LLVMDebugVersionMask) == 0 &&
Chris Lattnera45664f2008-11-10 02:56:27 +0000702 "Tag too large for debug encoding!");
Owen Anderson1d0be152009-08-13 21:58:54 +0000703 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
Chris Lattnera45664f2008-11-10 02:56:27 +0000704}
705
Chris Lattnera45664f2008-11-10 02:56:27 +0000706//===----------------------------------------------------------------------===//
707// DIFactory: Primary Constructors
708//===----------------------------------------------------------------------===//
709
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000710/// GetOrCreateArray - Create an descriptor for an array of descriptors.
Chris Lattnera45664f2008-11-10 02:56:27 +0000711/// This implicitly uniques the arrays created.
712DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
Benjamin Kramera53557e2010-09-21 16:41:29 +0000713 if (NumTys == 0) {
714 Value *Null = llvm::Constant::getNullValue(Type::getInt32Ty(VMContext));
715 return DIArray(MDNode::get(VMContext, &Null, 1));
716 }
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000717
Benjamin Kramera53557e2010-09-21 16:41:29 +0000718 SmallVector<Value *, 16> Elts(Tys, Tys+NumTys);
719 return DIArray(MDNode::get(VMContext, Elts.data(), Elts.size()));
Chris Lattnera45664f2008-11-10 02:56:27 +0000720}
721
722/// GetOrCreateSubrange - Create a descriptor for a value range. This
723/// implicitly uniques the values returned.
724DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
Devang Patele4b27562009-08-28 23:24:31 +0000725 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000726 GetTagConstant(dwarf::DW_TAG_subrange_type),
Owen Anderson1d0be152009-08-13 21:58:54 +0000727 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
728 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
Chris Lattnera45664f2008-11-10 02:56:27 +0000729 };
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000730
Devang Patele4b27562009-08-28 23:24:31 +0000731 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000732}
733
Devang Pateld6747df2010-10-06 20:50:40 +0000734/// CreateUnspecifiedParameter - Create unspeicified type descriptor
735/// for the subroutine type.
736DIDescriptor DIFactory::CreateUnspecifiedParameter() {
737 Value *Elts[] = {
738 GetTagConstant(dwarf::DW_TAG_unspecified_parameters)
739 };
740 return DIDescriptor(MDNode::get(VMContext, &Elts[0], 1));
741}
Chris Lattnera45664f2008-11-10 02:56:27 +0000742
743/// CreateCompileUnit - Create a new descriptor for the specified compile
744/// unit. Note that this does not unique compile units within the module.
745DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
Devang Patel65dbc902009-11-25 17:36:49 +0000746 StringRef Filename,
747 StringRef Directory,
748 StringRef Producer,
Devang Pateldd9db662009-01-30 18:20:31 +0000749 bool isMain,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000750 bool isOptimized,
Devang Patel65dbc902009-11-25 17:36:49 +0000751 StringRef Flags,
Devang Patel13319ce2009-02-17 22:43:44 +0000752 unsigned RunTimeVer) {
Devang Patele4b27562009-08-28 23:24:31 +0000753 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000754 GetTagConstant(dwarf::DW_TAG_compile_unit),
Devang Patele4b27562009-08-28 23:24:31 +0000755 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Owen Anderson1d0be152009-08-13 21:58:54 +0000756 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
Devang Patele4b27562009-08-28 23:24:31 +0000757 MDString::get(VMContext, Filename),
758 MDString::get(VMContext, Directory),
759 MDString::get(VMContext, Producer),
Owen Anderson1d0be152009-08-13 21:58:54 +0000760 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
761 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
Devang Patele4b27562009-08-28 23:24:31 +0000762 MDString::get(VMContext, Flags),
Owen Anderson1d0be152009-08-13 21:58:54 +0000763 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
Chris Lattnera45664f2008-11-10 02:56:27 +0000764 };
Devang Patele4b27562009-08-28 23:24:31 +0000765
766 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000767}
768
Devang Patel7aa81892010-03-08 22:27:22 +0000769/// CreateFile - Create a new descriptor for the specified file.
770DIFile DIFactory::CreateFile(StringRef Filename,
771 StringRef Directory,
772 DICompileUnit CU) {
773 Value *Elts[] = {
774 GetTagConstant(dwarf::DW_TAG_file_type),
775 MDString::get(VMContext, Filename),
776 MDString::get(VMContext, Directory),
Devang Patel2db49d72010-05-07 18:11:54 +0000777 CU
Devang Patel7aa81892010-03-08 22:27:22 +0000778 };
779
780 return DIFile(MDNode::get(VMContext, &Elts[0], 4));
781}
782
Chris Lattnera45664f2008-11-10 02:56:27 +0000783/// CreateEnumerator - Create a single enumerator value.
Devang Patel65dbc902009-11-25 17:36:49 +0000784DIEnumerator DIFactory::CreateEnumerator(StringRef Name, uint64_t Val){
Devang Patele4b27562009-08-28 23:24:31 +0000785 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000786 GetTagConstant(dwarf::DW_TAG_enumerator),
Devang Patele4b27562009-08-28 23:24:31 +0000787 MDString::get(VMContext, Name),
Owen Anderson1d0be152009-08-13 21:58:54 +0000788 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
Chris Lattnera45664f2008-11-10 02:56:27 +0000789 };
Devang Patele4b27562009-08-28 23:24:31 +0000790 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000791}
792
793
794/// CreateBasicType - Create a basic type like int, float, etc.
795DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000796 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000797 DIFile F,
Chris Lattnera45664f2008-11-10 02:56:27 +0000798 unsigned LineNumber,
799 uint64_t SizeInBits,
800 uint64_t AlignInBits,
801 uint64_t OffsetInBits, unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000802 unsigned Encoding) {
Devang Patele4b27562009-08-28 23:24:31 +0000803 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000804 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patel2db49d72010-05-07 18:11:54 +0000805 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000806 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000807 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000808 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
809 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
810 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
811 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
812 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
813 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
Chris Lattnera45664f2008-11-10 02:56:27 +0000814 };
Devang Patele4b27562009-08-28 23:24:31 +0000815 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000816}
817
Devang Patelac16d442009-10-26 16:54:35 +0000818
819/// CreateBasicType - Create a basic type like int, float, etc.
820DIBasicType DIFactory::CreateBasicTypeEx(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000821 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000822 DIFile F,
Devang Patelac16d442009-10-26 16:54:35 +0000823 unsigned LineNumber,
824 Constant *SizeInBits,
825 Constant *AlignInBits,
826 Constant *OffsetInBits, unsigned Flags,
827 unsigned Encoding) {
828 Value *Elts[] = {
829 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patel2db49d72010-05-07 18:11:54 +0000830 Context,
Devang Patelac16d442009-10-26 16:54:35 +0000831 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000832 F,
Devang Patelac16d442009-10-26 16:54:35 +0000833 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
834 SizeInBits,
835 AlignInBits,
836 OffsetInBits,
837 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
838 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
839 };
840 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
841}
842
Devang Patelb4645642010-02-06 01:02:37 +0000843/// CreateArtificialType - Create a new DIType with "artificial" flag set.
844DIType DIFactory::CreateArtificialType(DIType Ty) {
845 if (Ty.isArtificial())
846 return Ty;
847
848 SmallVector<Value *, 9> Elts;
Devang Patel2db49d72010-05-07 18:11:54 +0000849 MDNode *N = Ty;
Devang Patelb4645642010-02-06 01:02:37 +0000850 assert (N && "Unexpected input DIType!");
851 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
852 if (Value *V = N->getOperand(i))
853 Elts.push_back(V);
854 else
855 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
856 }
857
858 unsigned CurFlags = Ty.getFlags();
859 CurFlags = CurFlags | DIType::FlagArtificial;
860
861 // Flags are stored at this slot.
862 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
863
864 return DIType(MDNode::get(VMContext, Elts.data(), Elts.size()));
865}
Devang Patelac16d442009-10-26 16:54:35 +0000866
Chris Lattnera45664f2008-11-10 02:56:27 +0000867/// CreateDerivedType - Create a derived type like const qualified type,
868/// pointer, typedef, etc.
869DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
870 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000871 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000872 DIFile F,
Chris Lattnera45664f2008-11-10 02:56:27 +0000873 unsigned LineNumber,
874 uint64_t SizeInBits,
875 uint64_t AlignInBits,
876 uint64_t OffsetInBits,
877 unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000878 DIType DerivedFrom) {
Devang Patele4b27562009-08-28 23:24:31 +0000879 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000880 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000881 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000882 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000883 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000884 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
885 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
886 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
887 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
888 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000889 DerivedFrom,
Chris Lattnera45664f2008-11-10 02:56:27 +0000890 };
Devang Patele4b27562009-08-28 23:24:31 +0000891 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000892}
893
Devang Patelac16d442009-10-26 16:54:35 +0000894
895/// CreateDerivedType - Create a derived type like const qualified type,
896/// pointer, typedef, etc.
897DIDerivedType DIFactory::CreateDerivedTypeEx(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,
Devang Patelac16d442009-10-26 16:54:35 +0000901 unsigned LineNumber,
902 Constant *SizeInBits,
903 Constant *AlignInBits,
904 Constant *OffsetInBits,
905 unsigned Flags,
906 DIType DerivedFrom) {
907 Value *Elts[] = {
908 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000909 Context,
Devang Patelac16d442009-10-26 16:54:35 +0000910 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000911 F,
Devang Patelac16d442009-10-26 16:54:35 +0000912 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
913 SizeInBits,
914 AlignInBits,
915 OffsetInBits,
916 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000917 DerivedFrom,
Devang Patelac16d442009-10-26 16:54:35 +0000918 };
919 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
920}
921
922
Chris Lattnera45664f2008-11-10 02:56:27 +0000923/// CreateCompositeType - Create a composite type like array, struct, etc.
924DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
925 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000926 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000927 DIFile F,
Chris Lattnera45664f2008-11-10 02:56:27 +0000928 unsigned LineNumber,
929 uint64_t SizeInBits,
930 uint64_t AlignInBits,
931 uint64_t OffsetInBits,
932 unsigned Flags,
933 DIType DerivedFrom,
Devang Patel13319ce2009-02-17 22:43:44 +0000934 DIArray Elements,
Devang Patel0fd7f9d2010-01-26 21:14:59 +0000935 unsigned RuntimeLang,
936 MDNode *ContainingType) {
Owen Andersone277fed2009-07-07 16:31:25 +0000937
Devang Patele4b27562009-08-28 23:24:31 +0000938 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000939 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000940 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000941 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000942 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000943 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
944 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
945 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
946 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
947 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000948 DerivedFrom,
949 Elements,
Devang Patel0fd7f9d2010-01-26 21:14:59 +0000950 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
951 ContainingType
Chris Lattnera45664f2008-11-10 02:56:27 +0000952 };
Devang Patele7e5a0f2010-08-10 20:01:20 +0000953
954 MDNode *Node = MDNode::get(VMContext, &Elts[0], 13);
955 // Create a named metadata so that we do not lose this enum info.
956 if (Tag == dwarf::DW_TAG_enumeration_type) {
957 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.enum");
958 NMD->addOperand(Node);
959 }
960 return DICompositeType(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +0000961}
962
963
Dan Gohman489b29b2010-08-20 22:02:26 +0000964/// CreateTemporaryType - Create a temporary forward-declared type.
Dan Gohmana3833f12010-08-20 22:39:47 +0000965DIType DIFactory::CreateTemporaryType() {
Dan Gohman489b29b2010-08-20 22:02:26 +0000966 // Give the temporary MDNode a tag. It doesn't matter what tag we
967 // use here as long as DIType accepts it.
968 Value *Elts[] = {
969 GetTagConstant(DW_TAG_base_type)
970 };
971 MDNode *Node = MDNode::getTemporary(VMContext, Elts, array_lengthof(Elts));
972 return DIType(Node);
973}
974
975
Devang Patelac16d442009-10-26 16:54:35 +0000976/// CreateCompositeType - Create a composite type like array, struct, etc.
977DICompositeType DIFactory::CreateCompositeTypeEx(unsigned Tag,
978 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000979 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000980 DIFile F,
Devang Patelac16d442009-10-26 16:54:35 +0000981 unsigned LineNumber,
982 Constant *SizeInBits,
983 Constant *AlignInBits,
984 Constant *OffsetInBits,
985 unsigned Flags,
986 DIType DerivedFrom,
987 DIArray Elements,
Devang Patel6bf058c2010-08-10 20:22:49 +0000988 unsigned RuntimeLang,
989 MDNode *ContainingType) {
Devang Patelac16d442009-10-26 16:54:35 +0000990 Value *Elts[] = {
991 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000992 Context,
Devang Patelac16d442009-10-26 16:54:35 +0000993 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000994 F,
Devang Patelac16d442009-10-26 16:54:35 +0000995 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
996 SizeInBits,
997 AlignInBits,
998 OffsetInBits,
999 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +00001000 DerivedFrom,
1001 Elements,
Devang Patel6bf058c2010-08-10 20:22:49 +00001002 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
1003 ContainingType
Devang Patelac16d442009-10-26 16:54:35 +00001004 };
Devang Patel6bf058c2010-08-10 20:22:49 +00001005 MDNode *Node = MDNode::get(VMContext, &Elts[0], 13);
Devang Patele7e5a0f2010-08-10 20:01:20 +00001006 // Create a named metadata so that we do not lose this enum info.
1007 if (Tag == dwarf::DW_TAG_enumeration_type) {
1008 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.enum");
1009 NMD->addOperand(Node);
1010 }
1011 return DICompositeType(Node);
Devang Patelac16d442009-10-26 16:54:35 +00001012}
1013
1014
Chris Lattnera45664f2008-11-10 02:56:27 +00001015/// CreateSubprogram - Create a new descriptor for the specified subprogram.
1016/// See comments in DISubprogram for descriptions of these fields. This
1017/// method does not unique the generated descriptors.
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001018DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +00001019 StringRef Name,
1020 StringRef DisplayName,
1021 StringRef LinkageName,
Devang Patel4b945502010-03-09 00:44:10 +00001022 DIFile F,
Devang Patel2e369932010-01-23 00:26:28 +00001023 unsigned LineNo, DIType Ty,
Chris Lattnera45664f2008-11-10 02:56:27 +00001024 bool isLocalToUnit,
Devang Patel5d11eb02009-12-03 19:11:07 +00001025 bool isDefinition,
1026 unsigned VK, unsigned VIndex,
Devang Patel4e0d19d2010-02-03 19:57:19 +00001027 DIType ContainingType,
Devang Patel9dd2b472010-09-29 21:04:46 +00001028 unsigned Flags,
Stuart Hastings215aa152010-06-11 20:08:44 +00001029 bool isOptimized,
1030 Function *Fn) {
Devang Patel854967e2008-12-17 22:39:29 +00001031
Devang Patele4b27562009-08-28 23:24:31 +00001032 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001033 GetTagConstant(dwarf::DW_TAG_subprogram),
Devang Patele4b27562009-08-28 23:24:31 +00001034 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Devang Patel2db49d72010-05-07 18:11:54 +00001035 Context,
Devang Patele4b27562009-08-28 23:24:31 +00001036 MDString::get(VMContext, Name),
1037 MDString::get(VMContext, DisplayName),
1038 MDString::get(VMContext, LinkageName),
Devang Patel2db49d72010-05-07 18:11:54 +00001039 F,
Owen Anderson1d0be152009-08-13 21:58:54 +00001040 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001041 Ty,
Owen Anderson1d0be152009-08-13 21:58:54 +00001042 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
Devang Patel5d11eb02009-12-03 19:11:07 +00001043 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1044 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
1045 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
Devang Patel2db49d72010-05-07 18:11:54 +00001046 ContainingType,
Devang Patel9dd2b472010-09-29 21:04:46 +00001047 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Stuart Hastings215aa152010-06-11 20:08:44 +00001048 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
1049 Fn
Chris Lattnera45664f2008-11-10 02:56:27 +00001050 };
Devang Patelfd5fdc32010-06-28 05:53:08 +00001051 MDNode *Node = MDNode::get(VMContext, &Elts[0], 17);
1052
1053 // Create a named metadata so that we do not lose this mdnode.
1054 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
1055 NMD->addOperand(Node);
1056 return DISubprogram(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +00001057}
1058
Devang Patele3a18de2009-12-01 23:09:02 +00001059/// CreateSubprogramDefinition - Create new subprogram descriptor for the
Jim Grosbache62b6902010-07-21 21:36:25 +00001060/// given declaration.
1061DISubprogram DIFactory::CreateSubprogramDefinition(DISubprogram &SPDeclaration){
Devang Patele3a18de2009-12-01 23:09:02 +00001062 if (SPDeclaration.isDefinition())
Devang Patel2db49d72010-05-07 18:11:54 +00001063 return DISubprogram(SPDeclaration);
Devang Patele3a18de2009-12-01 23:09:02 +00001064
Devang Patel2db49d72010-05-07 18:11:54 +00001065 MDNode *DeclNode = SPDeclaration;
Devang Patele3a18de2009-12-01 23:09:02 +00001066 Value *Elts[] = {
1067 GetTagConstant(dwarf::DW_TAG_subprogram),
1068 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001069 DeclNode->getOperand(2), // Context
1070 DeclNode->getOperand(3), // Name
1071 DeclNode->getOperand(4), // DisplayName
1072 DeclNode->getOperand(5), // LinkageName
1073 DeclNode->getOperand(6), // CompileUnit
1074 DeclNode->getOperand(7), // LineNo
1075 DeclNode->getOperand(8), // Type
1076 DeclNode->getOperand(9), // isLocalToUnit
Stuart Hastings6d56b9f2010-06-05 00:39:29 +00001077 ConstantInt::get(Type::getInt1Ty(VMContext), true),
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001078 DeclNode->getOperand(11), // Virtuality
1079 DeclNode->getOperand(12), // VIndex
Devang Patel4e0d19d2010-02-03 19:57:19 +00001080 DeclNode->getOperand(13), // Containting Type
Devang Patel9dd2b472010-09-29 21:04:46 +00001081 DeclNode->getOperand(14), // Flags
Devang Patelacc6efa2010-06-27 21:04:31 +00001082 DeclNode->getOperand(15), // isOptimized
1083 SPDeclaration.getFunction()
Devang Patele3a18de2009-12-01 23:09:02 +00001084 };
Devang Patelfd5fdc32010-06-28 05:53:08 +00001085 MDNode *Node =MDNode::get(VMContext, &Elts[0], 16);
1086
1087 // Create a named metadata so that we do not lose this mdnode.
1088 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
1089 NMD->addOperand(Node);
1090 return DISubprogram(Node);
Devang Patele3a18de2009-12-01 23:09:02 +00001091}
1092
Chris Lattnera45664f2008-11-10 02:56:27 +00001093/// CreateGlobalVariable - Create a new descriptor for the specified global.
1094DIGlobalVariable
Devang Patel65dbc902009-11-25 17:36:49 +00001095DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
1096 StringRef DisplayName,
1097 StringRef LinkageName,
Devang Patel4b945502010-03-09 00:44:10 +00001098 DIFile F,
Devang Patel2e369932010-01-23 00:26:28 +00001099 unsigned LineNo, DIType Ty,bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +00001100 bool isDefinition, llvm::GlobalVariable *Val) {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001101 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001102 GetTagConstant(dwarf::DW_TAG_variable),
Devang Patele4b27562009-08-28 23:24:31 +00001103 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Devang Patel2db49d72010-05-07 18:11:54 +00001104 Context,
Devang Patele4b27562009-08-28 23:24:31 +00001105 MDString::get(VMContext, Name),
1106 MDString::get(VMContext, DisplayName),
1107 MDString::get(VMContext, LinkageName),
Devang Patel2db49d72010-05-07 18:11:54 +00001108 F,
Owen Anderson1d0be152009-08-13 21:58:54 +00001109 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001110 Ty,
Owen Anderson1d0be152009-08-13 21:58:54 +00001111 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1112 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
Devang Patele4b27562009-08-28 23:24:31 +00001113 Val
Chris Lattnera45664f2008-11-10 02:56:27 +00001114 };
Devang Patele4b27562009-08-28 23:24:31 +00001115
1116 Value *const *Vs = &Elts[0];
1117 MDNode *Node = MDNode::get(VMContext,Vs, 12);
1118
1119 // Create a named metadata so that we do not lose this mdnode.
1120 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001121 NMD->addOperand(Node);
Devang Patele4b27562009-08-28 23:24:31 +00001122
1123 return DIGlobalVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +00001124}
1125
Devang Patel27398962010-08-09 21:39:24 +00001126/// CreateGlobalVariable - Create a new descriptor for the specified constant.
1127DIGlobalVariable
1128DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
1129 StringRef DisplayName,
1130 StringRef LinkageName,
1131 DIFile F,
1132 unsigned LineNo, DIType Ty,bool isLocalToUnit,
1133 bool isDefinition, llvm::Constant *Val) {
1134 Value *Elts[] = {
Devang Patelebd53742010-08-11 23:17:54 +00001135 GetTagConstant(dwarf::DW_TAG_variable),
Devang Patel27398962010-08-09 21:39:24 +00001136 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
1137 Context,
1138 MDString::get(VMContext, Name),
1139 MDString::get(VMContext, DisplayName),
1140 MDString::get(VMContext, LinkageName),
1141 F,
1142 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1143 Ty,
1144 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1145 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1146 Val
1147 };
1148
1149 Value *const *Vs = &Elts[0];
1150 MDNode *Node = MDNode::get(VMContext,Vs, 12);
1151
1152 // Create a named metadata so that we do not lose this mdnode.
1153 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
1154 NMD->addOperand(Node);
1155
1156 return DIGlobalVariable(Node);
1157}
Chris Lattnera45664f2008-11-10 02:56:27 +00001158
Devang Patel62367042010-11-10 22:19:21 +00001159/// fixupObjcLikeName - Replace contains special characters used
1160/// in a typical Objective-C names with '.' in a given string.
1161static void fixupObjcLikeName(std::string &Str) {
1162 for (size_t i = 0, e = Str.size(); i < e; ++i) {
1163 char C = Str[i];
Devang Patelddb85ac2010-12-03 23:29:30 +00001164 if (C == '[' || C == ']' || C == ' ' || C == ':' || C == '+')
Devang Patel62367042010-11-10 22:19:21 +00001165 Str[i] = '.';
1166 }
1167}
1168
1169/// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
1170/// to hold function specific information.
1171NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, StringRef FuncName) {
1172 SmallString<32> Out;
1173 if (FuncName.find('[') == StringRef::npos)
1174 return M.getOrInsertNamedMetadata(Twine("llvm.dbg.lv.", FuncName)
1175 .toStringRef(Out));
1176 std::string Name = FuncName;
1177 fixupObjcLikeName(Name);
1178 return M.getOrInsertNamedMetadata(Twine("llvm.dbg.lv.", Name)
1179 .toStringRef(Out));
1180}
1181
1182/// getFnSpecificMDNode - Return a NameMDNode, if available, that is
1183/// suitable to hold function specific information.
1184NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, StringRef FuncName) {
1185 if (FuncName.find('[') == StringRef::npos)
1186 return M.getNamedMetadata(Twine("llvm.dbg.lv.", FuncName));
1187 std::string Name = FuncName;
1188 fixupObjcLikeName(Name);
1189 return M.getNamedMetadata(Twine("llvm.dbg.lv.", Name));
1190}
1191
Chris Lattnera45664f2008-11-10 02:56:27 +00001192/// CreateVariable - Create a new descriptor for the specified variable.
1193DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +00001194 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +00001195 DIFile F,
1196 unsigned LineNo,
Devang Patel3cf763d2010-09-29 23:07:21 +00001197 DIType Ty, bool AlwaysPreserve,
1198 unsigned Flags) {
Devang Patele4b27562009-08-28 23:24:31 +00001199 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001200 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +00001201 Context,
Devang Patele4b27562009-08-28 23:24:31 +00001202 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +00001203 F,
Owen Anderson1d0be152009-08-13 21:58:54 +00001204 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001205 Ty,
Devang Patel3cf763d2010-09-29 23:07:21 +00001206 ConstantInt::get(Type::getInt32Ty(VMContext), Flags)
Chris Lattnera45664f2008-11-10 02:56:27 +00001207 };
Devang Patel3cf763d2010-09-29 23:07:21 +00001208 MDNode *Node = MDNode::get(VMContext, &Elts[0], 7);
Devang Patel6ed0ce32010-05-20 20:35:24 +00001209 if (AlwaysPreserve) {
1210 // The optimizer may remove local variable. If there is an interest
1211 // to preserve variable info in such situation then stash it in a
1212 // named mdnode.
Devang Patel2f7d5292010-06-16 00:53:55 +00001213 DISubprogram Fn(getDISubprogram(Context));
Devang Patel10de3bb2010-06-21 18:36:58 +00001214 StringRef FName = "fn";
1215 if (Fn.getFunction())
1216 FName = Fn.getFunction()->getName();
Devang Patel10de3bb2010-06-21 18:36:58 +00001217 char One = '\1';
1218 if (FName.startswith(StringRef(&One, 1)))
1219 FName = FName.substr(1);
Dan Gohman17aa92c2010-07-21 23:38:33 +00001220
Devang Patel62367042010-11-10 22:19:21 +00001221
1222 NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, FName);
Devang Patel2f7d5292010-06-16 00:53:55 +00001223 FnLocals->addOperand(Node);
Devang Patel98e1cac2010-05-14 21:01:35 +00001224 }
1225 return DIVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +00001226}
1227
1228
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001229/// CreateComplexVariable - Create a new descriptor for the specified variable
1230/// which has a complex address expression for its address.
1231DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
Benjamin Kramer28b4afc2010-09-21 16:00:03 +00001232 StringRef Name, DIFile F,
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001233 unsigned LineNo,
Benjamin Kramer28b4afc2010-09-21 16:00:03 +00001234 DIType Ty, Value *const *Addr,
1235 unsigned NumAddr) {
1236 SmallVector<Value *, 15> Elts;
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001237 Elts.push_back(GetTagConstant(Tag));
Devang Patel2db49d72010-05-07 18:11:54 +00001238 Elts.push_back(Context);
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001239 Elts.push_back(MDString::get(VMContext, Name));
Devang Patel2db49d72010-05-07 18:11:54 +00001240 Elts.push_back(F);
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001241 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
Devang Patel2db49d72010-05-07 18:11:54 +00001242 Elts.push_back(Ty);
Benjamin Kramer28b4afc2010-09-21 16:00:03 +00001243 Elts.append(Addr, Addr+NumAddr);
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001244
Benjamin Kramer28b4afc2010-09-21 16:00:03 +00001245 return DIVariable(MDNode::get(VMContext, Elts.data(), Elts.size()));
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001246}
1247
1248
Chris Lattnera45664f2008-11-10 02:56:27 +00001249/// CreateBlock - This creates a descriptor for a lexical block with the
Owen Anderson99035272009-07-07 17:12:53 +00001250/// specified parent VMContext.
Devang Patel3d821aa2010-02-16 21:39:34 +00001251DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context,
Stuart Hastings0db42712010-07-19 23:56:30 +00001252 DIFile F, unsigned LineNo,
1253 unsigned Col) {
1254 // Defeat MDNode uniqing for lexical blocks.
1255 static unsigned int unique_id = 0;
Devang Patele4b27562009-08-28 23:24:31 +00001256 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001257 GetTagConstant(dwarf::DW_TAG_lexical_block),
Devang Patel2db49d72010-05-07 18:11:54 +00001258 Context,
Devang Patel3d821aa2010-02-16 21:39:34 +00001259 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Stuart Hastings0db42712010-07-19 23:56:30 +00001260 ConstantInt::get(Type::getInt32Ty(VMContext), Col),
1261 F,
1262 ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
Chris Lattnera45664f2008-11-10 02:56:27 +00001263 };
Stuart Hastings0db42712010-07-19 23:56:30 +00001264 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 6));
Chris Lattnera45664f2008-11-10 02:56:27 +00001265}
1266
Devang Patel6404e4e2009-12-15 19:16:48 +00001267/// CreateNameSpace - This creates new descriptor for a namespace
1268/// with the specified parent context.
1269DINameSpace DIFactory::CreateNameSpace(DIDescriptor Context, StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +00001270 DIFile F,
Devang Patel6404e4e2009-12-15 19:16:48 +00001271 unsigned LineNo) {
1272 Value *Elts[] = {
1273 GetTagConstant(dwarf::DW_TAG_namespace),
Devang Patel2db49d72010-05-07 18:11:54 +00001274 Context,
Devang Patel6404e4e2009-12-15 19:16:48 +00001275 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +00001276 F,
Devang Patel6404e4e2009-12-15 19:16:48 +00001277 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1278 };
1279 return DINameSpace(MDNode::get(VMContext, &Elts[0], 5));
1280}
1281
Devang Patelf98d8fe2009-09-01 01:14:15 +00001282/// CreateLocation - Creates a debug info location.
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001283DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
Daniel Dunbara279bc32009-09-20 02:20:51 +00001284 DIScope S, DILocation OrigLoc) {
Devang Patelf98d8fe2009-09-01 01:14:15 +00001285 Value *Elts[] = {
1286 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1287 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001288 S,
1289 OrigLoc,
Devang Patelf98d8fe2009-09-01 01:14:15 +00001290 };
1291 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1292}
1293
Chris Lattnera45664f2008-11-10 02:56:27 +00001294//===----------------------------------------------------------------------===//
1295// DIFactory: Routines for inserting code into a function
1296//===----------------------------------------------------------------------===//
1297
Chris Lattnera45664f2008-11-10 02:56:27 +00001298/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Devang Patel6daf99b2009-11-10 22:05:35 +00001299Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001300 Instruction *InsertBefore) {
Victor Hernandez4cf292a2010-01-26 02:07:38 +00001301 assert(Storage && "no storage passed to dbg.declare");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001302 assert(D.Verify() && "empty DIVariable passed to dbg.declare");
Chris Lattnera45664f2008-11-10 02:56:27 +00001303 if (!DeclareFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +00001304 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1305
Victor Hernandez756462b2010-01-18 20:42:09 +00001306 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
Devang Patel2db49d72010-05-07 18:11:54 +00001307 D };
Devang Patel6daf99b2009-11-10 22:05:35 +00001308 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
Mike Stumpe4250392009-10-01 22:08:58 +00001309}
1310
1311/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Devang Patel6daf99b2009-11-10 22:05:35 +00001312Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001313 BasicBlock *InsertAtEnd) {
Victor Hernandez4cf292a2010-01-26 02:07:38 +00001314 assert(Storage && "no storage passed to dbg.declare");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001315 assert(D.Verify() && "invalid DIVariable passed to dbg.declare");
Mike Stumpe4250392009-10-01 22:08:58 +00001316 if (!DeclareFn)
1317 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1318
Victor Hernandez756462b2010-01-18 20:42:09 +00001319 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
Devang Patel2db49d72010-05-07 18:11:54 +00001320 D };
Devang Patel27a53de2010-01-29 18:30:57 +00001321
1322 // If this block already has a terminator then insert this intrinsic
1323 // before the terminator.
Jim Grosbache62b6902010-07-21 21:36:25 +00001324 if (TerminatorInst *T = InsertAtEnd->getTerminator())
Devang Patel27a53de2010-01-29 18:30:57 +00001325 return CallInst::Create(DeclareFn, Args, Args+2, "", T);
1326 else
1327 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);}
Torok Edwin620f2802008-12-16 09:07:36 +00001328
Victor Hernandezc59b3352009-12-07 21:54:43 +00001329/// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001330Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
Victor Hernandezc59b3352009-12-07 21:54:43 +00001331 DIVariable D,
1332 Instruction *InsertBefore) {
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001333 assert(V && "no value passed to dbg.value");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001334 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001335 if (!ValueFn)
1336 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1337
Victor Hernandezc8b7cd02010-01-20 05:44:11 +00001338 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001339 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
Devang Patel2db49d72010-05-07 18:11:54 +00001340 D };
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001341 return CallInst::Create(ValueFn, Args, Args+3, "", InsertBefore);
1342}
1343
Victor Hernandezc59b3352009-12-07 21:54:43 +00001344/// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001345Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
Victor Hernandezc59b3352009-12-07 21:54:43 +00001346 DIVariable D,
1347 BasicBlock *InsertAtEnd) {
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001348 assert(V && "no value passed to dbg.value");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001349 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001350 if (!ValueFn)
1351 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1352
Jim Grosbache62b6902010-07-21 21:36:25 +00001353 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001354 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
Devang Patel2db49d72010-05-07 18:11:54 +00001355 D };
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001356 return CallInst::Create(ValueFn, Args, Args+3, "", InsertAtEnd);
1357}
Devang Patele4b27562009-08-28 23:24:31 +00001358
Devang Patel1a7ca032010-09-28 18:08:20 +00001359// RecordType - Record DIType in a module such that it is not lost even if
1360// it is not referenced through debug info anchors.
1361void DIFactory::RecordType(DIType T) {
1362 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.ty");
1363 NMD->addOperand(T);
1364}
1365
1366
Devang Pateld2f79a12009-07-28 19:55:13 +00001367//===----------------------------------------------------------------------===//
Devang Patel98c65172009-07-30 18:25:15 +00001368// DebugInfoFinder implementations.
Devang Pateld2f79a12009-07-28 19:55:13 +00001369//===----------------------------------------------------------------------===//
1370
Devang Patel98c65172009-07-30 18:25:15 +00001371/// processModule - Process entire module and collect debug info.
1372void DebugInfoFinder::processModule(Module &M) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001373 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1374 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
1375 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
1376 ++BI) {
Devang Patel01c5ff62010-05-04 01:05:02 +00001377 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
Devang Patelb4d31302009-07-31 18:18:52 +00001378 processDeclare(DDI);
Jim Grosbache62b6902010-07-21 21:36:25 +00001379
Chris Lattner28a9bf62010-04-02 20:44:29 +00001380 DebugLoc Loc = BI->getDebugLoc();
1381 if (Loc.isUnknown())
1382 continue;
Jim Grosbache62b6902010-07-21 21:36:25 +00001383
Chris Lattner28a9bf62010-04-02 20:44:29 +00001384 LLVMContext &Ctx = BI->getContext();
1385 DIDescriptor Scope(Loc.getScope(Ctx));
Jim Grosbache62b6902010-07-21 21:36:25 +00001386
Chris Lattner28a9bf62010-04-02 20:44:29 +00001387 if (Scope.isCompileUnit())
Devang Patel2db49d72010-05-07 18:11:54 +00001388 addCompileUnit(DICompileUnit(Scope));
Chris Lattner28a9bf62010-04-02 20:44:29 +00001389 else if (Scope.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001390 processSubprogram(DISubprogram(Scope));
Chris Lattner28a9bf62010-04-02 20:44:29 +00001391 else if (Scope.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001392 processLexicalBlock(DILexicalBlock(Scope));
Jim Grosbache62b6902010-07-21 21:36:25 +00001393
Chris Lattner28a9bf62010-04-02 20:44:29 +00001394 if (MDNode *IA = Loc.getInlinedAt(Ctx))
1395 processLocation(DILocation(IA));
Devang Pateld2f79a12009-07-28 19:55:13 +00001396 }
Devang Patele4b27562009-08-28 23:24:31 +00001397
Devang Patelfd5fdc32010-06-28 05:53:08 +00001398 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
1399 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1400 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
1401 if (addGlobalVariable(DIG)) {
1402 addCompileUnit(DIG.getCompileUnit());
1403 processType(DIG.getType());
1404 }
Devang Pateld2f79a12009-07-28 19:55:13 +00001405 }
1406 }
Devang Patelfd5fdc32010-06-28 05:53:08 +00001407
1408 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
1409 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
1410 processSubprogram(DISubprogram(NMD->getOperand(i)));
Devang Pateld2f79a12009-07-28 19:55:13 +00001411}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001412
Devang Patel6daf99b2009-11-10 22:05:35 +00001413/// processLocation - Process DILocation.
1414void DebugInfoFinder::processLocation(DILocation Loc) {
Devang Patel3c91b052010-03-08 20:52:55 +00001415 if (!Loc.Verify()) return;
Devang Patel2db49d72010-05-07 18:11:54 +00001416 DIDescriptor S(Loc.getScope());
Devang Patel6daf99b2009-11-10 22:05:35 +00001417 if (S.isCompileUnit())
Devang Patel2db49d72010-05-07 18:11:54 +00001418 addCompileUnit(DICompileUnit(S));
Devang Patel6daf99b2009-11-10 22:05:35 +00001419 else if (S.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001420 processSubprogram(DISubprogram(S));
Devang Patel6daf99b2009-11-10 22:05:35 +00001421 else if (S.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001422 processLexicalBlock(DILexicalBlock(S));
Devang Patel6daf99b2009-11-10 22:05:35 +00001423 processLocation(Loc.getOrigLocation());
1424}
1425
Devang Patel98c65172009-07-30 18:25:15 +00001426/// processType - Process DIType.
1427void DebugInfoFinder::processType(DIType DT) {
Devang Patel72bcdb62009-08-10 22:09:58 +00001428 if (!addType(DT))
Devang Pateld2f79a12009-07-28 19:55:13 +00001429 return;
1430
1431 addCompileUnit(DT.getCompileUnit());
Devang Patel6ceea332009-08-31 18:49:10 +00001432 if (DT.isCompositeType()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001433 DICompositeType DCT(DT);
Devang Patel98c65172009-07-30 18:25:15 +00001434 processType(DCT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001435 DIArray DA = DCT.getTypeArray();
Devang Patel3c91b052010-03-08 20:52:55 +00001436 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1437 DIDescriptor D = DA.getElement(i);
1438 if (D.isType())
Devang Patel2db49d72010-05-07 18:11:54 +00001439 processType(DIType(D));
Devang Patel3c91b052010-03-08 20:52:55 +00001440 else if (D.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001441 processSubprogram(DISubprogram(D));
Devang Patel3c91b052010-03-08 20:52:55 +00001442 }
Devang Patel6ceea332009-08-31 18:49:10 +00001443 } else if (DT.isDerivedType()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001444 DIDerivedType DDT(DT);
Devang Patel3c91b052010-03-08 20:52:55 +00001445 processType(DDT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001446 }
1447}
1448
Devang Patelbeab41b2009-10-07 22:04:08 +00001449/// processLexicalBlock
1450void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
Devang Patelbeab41b2009-10-07 22:04:08 +00001451 DIScope Context = LB.getContext();
1452 if (Context.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001453 return processLexicalBlock(DILexicalBlock(Context));
Devang Patelbeab41b2009-10-07 22:04:08 +00001454 else
Devang Patel2db49d72010-05-07 18:11:54 +00001455 return processSubprogram(DISubprogram(Context));
Devang Patelbeab41b2009-10-07 22:04:08 +00001456}
1457
Devang Patel98c65172009-07-30 18:25:15 +00001458/// processSubprogram - Process DISubprogram.
1459void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001460 if (!addSubprogram(SP))
1461 return;
1462 addCompileUnit(SP.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +00001463 processType(SP.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +00001464}
1465
Devang Patelb4d31302009-07-31 18:18:52 +00001466/// processDeclare - Process DbgDeclareInst.
1467void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
Devang Patel3c91b052010-03-08 20:52:55 +00001468 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1469 if (!N) return;
1470
1471 DIDescriptor DV(N);
1472 if (!DV.isVariable())
Devang Patelb4d31302009-07-31 18:18:52 +00001473 return;
1474
Devang Patel2db49d72010-05-07 18:11:54 +00001475 if (!NodesSeen.insert(DV))
Devang Patelb4d31302009-07-31 18:18:52 +00001476 return;
1477
Devang Patel3c91b052010-03-08 20:52:55 +00001478 addCompileUnit(DIVariable(N).getCompileUnit());
1479 processType(DIVariable(N).getType());
Devang Patelb4d31302009-07-31 18:18:52 +00001480}
1481
Devang Patel72bcdb62009-08-10 22:09:58 +00001482/// addType - Add type into Tys.
1483bool DebugInfoFinder::addType(DIType DT) {
Devang Patel3c91b052010-03-08 20:52:55 +00001484 if (!DT.isValid())
Devang Patel72bcdb62009-08-10 22:09:58 +00001485 return false;
1486
Devang Patel2db49d72010-05-07 18:11:54 +00001487 if (!NodesSeen.insert(DT))
Devang Patel72bcdb62009-08-10 22:09:58 +00001488 return false;
1489
Devang Patel2db49d72010-05-07 18:11:54 +00001490 TYs.push_back(DT);
Devang Patel72bcdb62009-08-10 22:09:58 +00001491 return true;
1492}
1493
Devang Pateld2f79a12009-07-28 19:55:13 +00001494/// addCompileUnit - Add compile unit into CUs.
Devang Patel98c65172009-07-30 18:25:15 +00001495bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Patel3c91b052010-03-08 20:52:55 +00001496 if (!CU.Verify())
Devang Pateld2f79a12009-07-28 19:55:13 +00001497 return false;
1498
Devang Patel2db49d72010-05-07 18:11:54 +00001499 if (!NodesSeen.insert(CU))
Devang Pateld2f79a12009-07-28 19:55:13 +00001500 return false;
1501
Devang Patel2db49d72010-05-07 18:11:54 +00001502 CUs.push_back(CU);
Devang Pateld2f79a12009-07-28 19:55:13 +00001503 return true;
1504}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001505
Devang Pateld2f79a12009-07-28 19:55:13 +00001506/// addGlobalVariable - Add global variable into GVs.
Devang Patel98c65172009-07-30 18:25:15 +00001507bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Patel2db49d72010-05-07 18:11:54 +00001508 if (!DIDescriptor(DIG).isGlobalVariable())
Devang Pateld2f79a12009-07-28 19:55:13 +00001509 return false;
1510
Devang Patel2db49d72010-05-07 18:11:54 +00001511 if (!NodesSeen.insert(DIG))
Devang Pateld2f79a12009-07-28 19:55:13 +00001512 return false;
1513
Devang Patel2db49d72010-05-07 18:11:54 +00001514 GVs.push_back(DIG);
Devang Pateld2f79a12009-07-28 19:55:13 +00001515 return true;
1516}
1517
1518// addSubprogram - Add subprgoram into SPs.
Devang Patel98c65172009-07-30 18:25:15 +00001519bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Patel2db49d72010-05-07 18:11:54 +00001520 if (!DIDescriptor(SP).isSubprogram())
Devang Pateld2f79a12009-07-28 19:55:13 +00001521 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001522
Devang Patel2db49d72010-05-07 18:11:54 +00001523 if (!NodesSeen.insert(SP))
Devang Pateld2f79a12009-07-28 19:55:13 +00001524 return false;
1525
Devang Patel2db49d72010-05-07 18:11:54 +00001526 SPs.push_back(SP);
Devang Pateld2f79a12009-07-28 19:55:13 +00001527 return true;
1528}
1529
Victor Hernandez756462b2010-01-18 20:42:09 +00001530/// Find the debug info descriptor corresponding to this global variable.
1531static Value *findDbgGlobalDeclare(GlobalVariable *V) {
Chris Lattner099b7792009-12-29 09:22:47 +00001532 const Module *M = V->getParent();
1533 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1534 if (!NMD)
Torok Edwinff7d0e92009-03-10 13:41:26 +00001535 return 0;
Chris Lattner099b7792009-12-29 09:22:47 +00001536
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001537 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman872814a2010-07-21 18:54:18 +00001538 DIDescriptor DIG(cast<MDNode>(NMD->getOperand(i)));
Devang Patel3c91b052010-03-08 20:52:55 +00001539 if (!DIG.isGlobalVariable())
Chris Lattner099b7792009-12-29 09:22:47 +00001540 continue;
Devang Patel2db49d72010-05-07 18:11:54 +00001541 if (DIGlobalVariable(DIG).getGlobal() == V)
1542 return DIG;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001543 }
Chris Lattner099b7792009-12-29 09:22:47 +00001544 return 0;
1545}
Torok Edwinff7d0e92009-03-10 13:41:26 +00001546
Chris Lattner099b7792009-12-29 09:22:47 +00001547/// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
1548/// It looks through pointer casts too.
Victor Hernandez756462b2010-01-18 20:42:09 +00001549static const DbgDeclareInst *findDbgDeclare(const Value *V) {
Victor Hernandez3a328652010-01-15 19:04:09 +00001550 V = V->stripPointerCasts();
Jim Grosbache62b6902010-07-21 21:36:25 +00001551
Victor Hernandez3a328652010-01-15 19:04:09 +00001552 if (!isa<Instruction>(V) && !isa<Argument>(V))
Torok Edwin620f2802008-12-16 09:07:36 +00001553 return 0;
Jim Grosbache62b6902010-07-21 21:36:25 +00001554
Victor Hernandez3a328652010-01-15 19:04:09 +00001555 const Function *F = NULL;
1556 if (const Instruction *I = dyn_cast<Instruction>(V))
1557 F = I->getParent()->getParent();
1558 else if (const Argument *A = dyn_cast<Argument>(V))
1559 F = A->getParent();
Jim Grosbache62b6902010-07-21 21:36:25 +00001560
Victor Hernandez3a328652010-01-15 19:04:09 +00001561 for (Function::const_iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
1562 for (BasicBlock::const_iterator BI = (*FI).begin(), BE = (*FI).end();
1563 BI != BE; ++BI)
1564 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1565 if (DDI->getAddress() == V)
1566 return DDI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001567
Chris Lattner099b7792009-12-29 09:22:47 +00001568 return 0;
1569}
Bill Wendlingdc817b62009-05-14 18:26:15 +00001570
Chris Lattner099b7792009-12-29 09:22:47 +00001571bool llvm::getLocationInfo(const Value *V, std::string &DisplayName,
1572 std::string &Type, unsigned &LineNo,
1573 std::string &File, std::string &Dir) {
1574 DICompileUnit Unit;
1575 DIType TypeD;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001576
Chris Lattner099b7792009-12-29 09:22:47 +00001577 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1578 Value *DIGV = findDbgGlobalDeclare(GV);
1579 if (!DIGV) return false;
1580 DIGlobalVariable Var(cast<MDNode>(DIGV));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001581
Chris Lattner099b7792009-12-29 09:22:47 +00001582 StringRef D = Var.getDisplayName();
Devang Patel65dbc902009-11-25 17:36:49 +00001583 if (!D.empty())
Chris Lattner099b7792009-12-29 09:22:47 +00001584 DisplayName = D;
1585 LineNo = Var.getLineNumber();
1586 Unit = Var.getCompileUnit();
1587 TypeD = Var.getType();
1588 } else {
1589 const DbgDeclareInst *DDI = findDbgDeclare(V);
1590 if (!DDI) return false;
1591 DIVariable Var(cast<MDNode>(DDI->getVariable()));
1592
1593 StringRef D = Var.getName();
1594 if (!D.empty())
1595 DisplayName = D;
1596 LineNo = Var.getLineNumber();
1597 Unit = Var.getCompileUnit();
1598 TypeD = Var.getType();
Torok Edwinff7d0e92009-03-10 13:41:26 +00001599 }
Devang Patel13e16b62009-06-26 01:49:18 +00001600
Chris Lattner099b7792009-12-29 09:22:47 +00001601 StringRef T = TypeD.getName();
1602 if (!T.empty())
1603 Type = T;
1604 StringRef F = Unit.getFilename();
1605 if (!F.empty())
1606 File = F;
1607 StringRef D = Unit.getDirectory();
1608 if (!D.empty())
1609 Dir = D;
1610 return true;
1611}
Devang Patel9e529c32009-07-02 01:15:24 +00001612
Chris Lattner099b7792009-12-29 09:22:47 +00001613/// getDISubprogram - Find subprogram that is enclosing this scope.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001614DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
Chris Lattner099b7792009-12-29 09:22:47 +00001615 DIDescriptor D(Scope);
Chris Lattner099b7792009-12-29 09:22:47 +00001616 if (D.isSubprogram())
1617 return DISubprogram(Scope);
Jim Grosbache62b6902010-07-21 21:36:25 +00001618
Chris Lattner099b7792009-12-29 09:22:47 +00001619 if (D.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001620 return getDISubprogram(DILexicalBlock(Scope).getContext());
Jim Grosbache62b6902010-07-21 21:36:25 +00001621
Chris Lattner099b7792009-12-29 09:22:47 +00001622 return DISubprogram();
1623}
Devang Patel193f7202009-11-24 01:14:22 +00001624
Chris Lattner099b7792009-12-29 09:22:47 +00001625/// getDICompositeType - Find underlying composite type.
1626DICompositeType llvm::getDICompositeType(DIType T) {
Chris Lattner099b7792009-12-29 09:22:47 +00001627 if (T.isCompositeType())
Devang Patel2db49d72010-05-07 18:11:54 +00001628 return DICompositeType(T);
Jim Grosbache62b6902010-07-21 21:36:25 +00001629
Chris Lattner099b7792009-12-29 09:22:47 +00001630 if (T.isDerivedType())
Devang Patel2db49d72010-05-07 18:11:54 +00001631 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
Jim Grosbache62b6902010-07-21 21:36:25 +00001632
Chris Lattner099b7792009-12-29 09:22:47 +00001633 return DICompositeType();
Torok Edwin620f2802008-12-16 09:07:36 +00001634}