blob: f968d0619518edfeea72191314099ee4e24c1b1d [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 &&
Devang Patelfe58f952010-12-07 23:25:47 +0000311 Tag != dwarf::DW_TAG_reference_type && Tag != dwarf::DW_TAG_restrict_type
Devang Patel43c249c2010-12-08 01:50:15 +0000312 && Tag != dwarf::DW_TAG_vector_type && Tag != dwarf::DW_TAG_array_type
313 && Tag != dwarf::DW_TAG_enumeration_type
Devang Patelfe58f952010-12-07 23:25:47 +0000314 && getFilename().empty())
Devang Patelb79b5352009-01-19 23:21:49 +0000315 return false;
316 return true;
317}
318
Devang Patel0c4720c2010-08-23 18:25:56 +0000319/// Verify - Verify that a basic type descriptor is well formed.
320bool DIBasicType::Verify() const {
321 return isBasicType();
322}
323
324/// Verify - Verify that a derived type descriptor is well formed.
325bool DIDerivedType::Verify() const {
326 return isDerivedType();
327}
328
Devang Patelb79b5352009-01-19 23:21:49 +0000329/// Verify - Verify that a composite type descriptor is well formed.
330bool DICompositeType::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000331 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000332 return false;
Devang Patel3c91b052010-03-08 20:52:55 +0000333 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000334 return false;
335
336 DICompileUnit CU = getCompileUnit();
Devang Patel3c91b052010-03-08 20:52:55 +0000337 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000338 return false;
339 return true;
340}
341
342/// Verify - Verify that a subprogram descriptor is well formed.
343bool DISubprogram::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000344 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000345 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000346
Devang Patel3c91b052010-03-08 20:52:55 +0000347 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000348 return false;
349
350 DICompileUnit CU = getCompileUnit();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000351 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000352 return false;
353
354 DICompositeType Ty = getType();
Devang Patel3c91b052010-03-08 20:52:55 +0000355 if (!Ty.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000356 return false;
357 return true;
358}
359
360/// Verify - Verify that a global variable descriptor is well formed.
361bool DIGlobalVariable::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000362 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000363 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000364
Devang Patel65dbc902009-11-25 17:36:49 +0000365 if (getDisplayName().empty())
Devang Patel84c73e92009-11-06 17:58:12 +0000366 return false;
367
Devang Patel3c91b052010-03-08 20:52:55 +0000368 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000369 return false;
370
371 DICompileUnit CU = getCompileUnit();
Devang Patel3c91b052010-03-08 20:52:55 +0000372 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000373 return false;
374
375 DIType Ty = getType();
376 if (!Ty.Verify())
377 return false;
378
Devang Patel27398962010-08-09 21:39:24 +0000379 if (!getGlobal() && !getConstant())
Devang Patelb79b5352009-01-19 23:21:49 +0000380 return false;
381
382 return true;
383}
384
385/// Verify - Verify that a variable descriptor is well formed.
386bool DIVariable::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000387 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000388 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000389
Devang Patel3c91b052010-03-08 20:52:55 +0000390 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000391 return false;
392
Devang Patel62077af2010-05-07 21:42:24 +0000393 if (!getCompileUnit().Verify())
394 return false;
395
Devang Patelb79b5352009-01-19 23:21:49 +0000396 DIType Ty = getType();
397 if (!Ty.Verify())
398 return false;
399
Devang Patelb79b5352009-01-19 23:21:49 +0000400 return true;
401}
402
Devang Patel3c91b052010-03-08 20:52:55 +0000403/// Verify - Verify that a location descriptor is well formed.
404bool DILocation::Verify() const {
405 if (!DbgNode)
406 return false;
Jim Grosbache62b6902010-07-21 21:36:25 +0000407
Devang Patel3c91b052010-03-08 20:52:55 +0000408 return DbgNode->getNumOperands() == 4;
409}
410
Devang Patel47e22652010-05-07 23:04:32 +0000411/// Verify - Verify that a namespace descriptor is well formed.
412bool DINameSpace::Verify() const {
413 if (!DbgNode)
414 return false;
415 if (getName().empty())
416 return false;
417 if (!getCompileUnit().Verify())
418 return false;
419 return true;
420}
421
Devang Patel36375ee2009-02-17 21:23:59 +0000422/// getOriginalTypeSize - If this type is derived from a base type then
423/// return base type size.
424uint64_t DIDerivedType::getOriginalTypeSize() const {
Devang Patel61ecbd12009-11-04 23:48:00 +0000425 unsigned Tag = getTag();
426 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
427 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
428 Tag == dwarf::DW_TAG_restrict_type) {
429 DIType BaseType = getTypeDerivedFrom();
Jim Grosbache62b6902010-07-21 21:36:25 +0000430 // If this type is not derived from any type then take conservative
Devang Patel5ebfa2d2009-11-06 18:24:05 +0000431 // approach.
Devang Patel3c91b052010-03-08 20:52:55 +0000432 if (!BaseType.isValid())
Devang Patel5ebfa2d2009-11-06 18:24:05 +0000433 return getSizeInBits();
Devang Patel61ecbd12009-11-04 23:48:00 +0000434 if (BaseType.isDerivedType())
Devang Patel2db49d72010-05-07 18:11:54 +0000435 return DIDerivedType(BaseType).getOriginalTypeSize();
Devang Patel61ecbd12009-11-04 23:48:00 +0000436 else
437 return BaseType.getSizeInBits();
438 }
Jim Grosbache62b6902010-07-21 21:36:25 +0000439
Devang Patel61ecbd12009-11-04 23:48:00 +0000440 return getSizeInBits();
Devang Patel36375ee2009-02-17 21:23:59 +0000441}
Devang Patelb79b5352009-01-19 23:21:49 +0000442
Jim Grosbache62b6902010-07-21 21:36:25 +0000443/// isInlinedFnArgument - Return true if this variable provides debugging
Devang Patel719f6a92010-04-29 20:40:36 +0000444/// information for an inlined function arguments.
445bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
446 assert(CurFn && "Invalid function");
447 if (!getContext().isSubprogram())
448 return false;
Jim Grosbache62b6902010-07-21 21:36:25 +0000449 // This variable is not inlined function argument if its scope
Devang Patel719f6a92010-04-29 20:40:36 +0000450 // does not describe current function.
Devang Patel2db49d72010-05-07 18:11:54 +0000451 return !(DISubprogram(getContext()).describes(CurFn));
Devang Patel719f6a92010-04-29 20:40:36 +0000452}
453
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000454/// describes - Return true if this subprogram provides debugging
455/// information for the function F.
456bool DISubprogram::describes(const Function *F) {
Chris Lattner099b7792009-12-29 09:22:47 +0000457 assert(F && "Invalid function");
Devang Patelffd33cd2010-06-16 06:42:02 +0000458 if (F == getFunction())
459 return true;
Devang Patel65dbc902009-11-25 17:36:49 +0000460 StringRef Name = getLinkageName();
461 if (Name.empty())
Devang Patel5ccdd102009-09-29 18:40:58 +0000462 Name = getName();
Devang Patel65dbc902009-11-25 17:36:49 +0000463 if (F->getName() == Name)
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000464 return true;
465 return false;
466}
467
Jim Grosbache62b6902010-07-21 21:36:25 +0000468unsigned DISubprogram::isOptimized() const {
Devang Patelccff8122010-04-30 19:38:23 +0000469 assert (DbgNode && "Invalid subprogram descriptor!");
470 if (DbgNode->getNumOperands() == 16)
471 return getUnsignedField(15);
472 return 0;
473}
474
Devang Patel65dbc902009-11-25 17:36:49 +0000475StringRef DIScope::getFilename() const {
Devang Patel77bf2952010-03-08 22:02:50 +0000476 if (!DbgNode)
477 return StringRef();
Jim Grosbache62b6902010-07-21 21:36:25 +0000478 if (isLexicalBlock())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000479 return DILexicalBlock(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000480 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000481 return DISubprogram(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000482 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000483 return DICompileUnit(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000484 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000485 return DINameSpace(DbgNode).getFilename();
Devang Patel77bf2952010-03-08 22:02:50 +0000486 if (isType())
487 return DIType(DbgNode).getFilename();
Devang Patel7aa81892010-03-08 22:27:22 +0000488 if (isFile())
489 return DIFile(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000490 assert(0 && "Invalid DIScope!");
Devang Patel65dbc902009-11-25 17:36:49 +0000491 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000492}
493
Devang Patel65dbc902009-11-25 17:36:49 +0000494StringRef DIScope::getDirectory() const {
Devang Patel77bf2952010-03-08 22:02:50 +0000495 if (!DbgNode)
496 return StringRef();
Jim Grosbache62b6902010-07-21 21:36:25 +0000497 if (isLexicalBlock())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000498 return DILexicalBlock(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000499 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000500 return DISubprogram(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000501 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000502 return DICompileUnit(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000503 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000504 return DINameSpace(DbgNode).getDirectory();
Devang Patel77bf2952010-03-08 22:02:50 +0000505 if (isType())
506 return DIType(DbgNode).getDirectory();
Devang Patel7aa81892010-03-08 22:27:22 +0000507 if (isFile())
508 return DIFile(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000509 assert(0 && "Invalid DIScope!");
Devang Patel65dbc902009-11-25 17:36:49 +0000510 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000511}
512
Chris Lattnera45664f2008-11-10 02:56:27 +0000513//===----------------------------------------------------------------------===//
Devang Patel7136a652009-07-01 22:10:23 +0000514// DIDescriptor: dump routines for all descriptors.
515//===----------------------------------------------------------------------===//
516
517
Dan Gohman50404362010-05-07 15:30:29 +0000518/// print - Print descriptor.
519void DIDescriptor::print(raw_ostream &OS) const {
520 OS << "[" << dwarf::TagString(getTag()) << "] ";
521 OS.write_hex((intptr_t) &*DbgNode) << ']';
Devang Patel7136a652009-07-01 22:10:23 +0000522}
523
Dan Gohman50404362010-05-07 15:30:29 +0000524/// print - Print compile unit.
525void DICompileUnit::print(raw_ostream &OS) const {
Devang Patel7136a652009-07-01 22:10:23 +0000526 if (getLanguage())
Dan Gohman50404362010-05-07 15:30:29 +0000527 OS << " [" << dwarf::LanguageString(getLanguage()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000528
Dan Gohmanfaa19c32010-05-10 20:07:44 +0000529 OS << " [" << getDirectory() << "/" << getFilename() << "]";
Devang Patel7136a652009-07-01 22:10:23 +0000530}
531
Dan Gohman50404362010-05-07 15:30:29 +0000532/// print - Print type.
533void DIType::print(raw_ostream &OS) const {
Devang Patel3c91b052010-03-08 20:52:55 +0000534 if (!DbgNode) return;
Devang Patel7136a652009-07-01 22:10:23 +0000535
Devang Patel65dbc902009-11-25 17:36:49 +0000536 StringRef Res = getName();
537 if (!Res.empty())
Dan Gohman50404362010-05-07 15:30:29 +0000538 OS << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000539
540 unsigned Tag = getTag();
Dan Gohman50404362010-05-07 15:30:29 +0000541 OS << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000542
543 // TODO : Print context
Dan Gohmanc014d092010-05-07 16:17:22 +0000544 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000545 OS << " ["
Dan Gohman9a7063e2010-05-07 16:39:27 +0000546 << "line " << getLineNumber() << ", "
547 << getSizeInBits() << " bits, "
548 << getAlignInBits() << " bit alignment, "
549 << getOffsetInBits() << " bit offset"
Chris Lattnera81d29b2009-08-23 07:33:14 +0000550 << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000551
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000552 if (isPrivate())
Dan Gohman50404362010-05-07 15:30:29 +0000553 OS << " [private] ";
Devang Patel7136a652009-07-01 22:10:23 +0000554 else if (isProtected())
Dan Gohman50404362010-05-07 15:30:29 +0000555 OS << " [protected] ";
Devang Patel7136a652009-07-01 22:10:23 +0000556
557 if (isForwardDecl())
Dan Gohman50404362010-05-07 15:30:29 +0000558 OS << " [fwd] ";
Devang Patel7136a652009-07-01 22:10:23 +0000559
Devang Patel6ceea332009-08-31 18:49:10 +0000560 if (isBasicType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000561 DIBasicType(DbgNode).print(OS);
Devang Patel6ceea332009-08-31 18:49:10 +0000562 else if (isDerivedType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000563 DIDerivedType(DbgNode).print(OS);
Devang Patel6ceea332009-08-31 18:49:10 +0000564 else if (isCompositeType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000565 DICompositeType(DbgNode).print(OS);
Devang Patel7136a652009-07-01 22:10:23 +0000566 else {
Dan Gohman50404362010-05-07 15:30:29 +0000567 OS << "Invalid DIType\n";
Devang Patel7136a652009-07-01 22:10:23 +0000568 return;
569 }
570
Dan Gohman50404362010-05-07 15:30:29 +0000571 OS << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000572}
573
Dan Gohman50404362010-05-07 15:30:29 +0000574/// print - Print basic type.
575void DIBasicType::print(raw_ostream &OS) const {
576 OS << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000577}
578
Dan Gohman50404362010-05-07 15:30:29 +0000579/// print - Print derived type.
580void DIDerivedType::print(raw_ostream &OS) const {
Dan Gohmanc014d092010-05-07 16:17:22 +0000581 OS << "\n\t Derived From: "; getTypeDerivedFrom().print(OS);
Devang Patel7136a652009-07-01 22:10:23 +0000582}
583
Dan Gohman50404362010-05-07 15:30:29 +0000584/// print - Print composite type.
585void DICompositeType::print(raw_ostream &OS) const {
Devang Patel7136a652009-07-01 22:10:23 +0000586 DIArray A = getTypeArray();
Dan Gohman50404362010-05-07 15:30:29 +0000587 OS << " [" << A.getNumElements() << " elements]";
Devang Patel7136a652009-07-01 22:10:23 +0000588}
589
Dan Gohman50404362010-05-07 15:30:29 +0000590/// print - Print subprogram.
591void DISubprogram::print(raw_ostream &OS) const {
Devang Patel65dbc902009-11-25 17:36:49 +0000592 StringRef Res = getName();
593 if (!Res.empty())
Dan Gohman50404362010-05-07 15:30:29 +0000594 OS << " [" << Res << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000595
596 unsigned Tag = getTag();
Dan Gohman50404362010-05-07 15:30:29 +0000597 OS << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000598
599 // TODO : Print context
Dan Gohmanc014d092010-05-07 16:17:22 +0000600 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000601 OS << " [" << getLineNumber() << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000602
603 if (isLocalToUnit())
Dan Gohman50404362010-05-07 15:30:29 +0000604 OS << " [local] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000605
606 if (isDefinition())
Dan Gohman50404362010-05-07 15:30:29 +0000607 OS << " [def] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000608
Dan Gohman50404362010-05-07 15:30:29 +0000609 OS << "\n";
610}
611
612/// print - Print global variable.
613void DIGlobalVariable::print(raw_ostream &OS) const {
614 OS << " [";
Devang Patela49d8772010-05-07 23:19:07 +0000615 StringRef Res = getName();
616 if (!Res.empty())
617 OS << " [" << Res << "] ";
618
619 unsigned Tag = getTag();
620 OS << " [" << dwarf::TagString(Tag) << "] ";
621
622 // TODO : Print context
623 getCompileUnit().print(OS);
624 OS << " [" << getLineNumber() << "] ";
625
626 if (isLocalToUnit())
627 OS << " [local] ";
628
629 if (isDefinition())
630 OS << " [def] ";
631
632 if (isGlobalVariable())
633 DIGlobalVariable(DbgNode).print(OS);
634 OS << "]\n";
Dan Gohman50404362010-05-07 15:30:29 +0000635}
636
637/// print - Print variable.
638void DIVariable::print(raw_ostream &OS) const {
639 StringRef Res = getName();
640 if (!Res.empty())
641 OS << " [" << Res << "] ";
642
Dan Gohmanc014d092010-05-07 16:17:22 +0000643 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000644 OS << " [" << getLineNumber() << "] ";
Dan Gohmanc014d092010-05-07 16:17:22 +0000645 getType().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000646 OS << "\n";
647
648 // FIXME: Dump complex addresses
649}
650
651/// dump - Print descriptor to dbgs() with a newline.
652void DIDescriptor::dump() const {
653 print(dbgs()); dbgs() << '\n';
654}
655
656/// dump - Print compile unit to dbgs() with a newline.
657void DICompileUnit::dump() const {
658 print(dbgs()); dbgs() << '\n';
659}
660
661/// dump - Print type to dbgs() with a newline.
662void DIType::dump() const {
663 print(dbgs()); dbgs() << '\n';
664}
665
666/// dump - Print basic type to dbgs() with a newline.
667void DIBasicType::dump() const {
668 print(dbgs()); dbgs() << '\n';
669}
670
671/// dump - Print derived type to dbgs() with a newline.
672void DIDerivedType::dump() const {
673 print(dbgs()); dbgs() << '\n';
674}
675
676/// dump - Print composite type to dbgs() with a newline.
677void DICompositeType::dump() const {
678 print(dbgs()); dbgs() << '\n';
679}
680
Dan Gohman50404362010-05-07 15:30:29 +0000681/// dump - Print subprogram to dbgs() with a newline.
682void DISubprogram::dump() const {
683 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000684}
685
686/// dump - Print global variable.
687void DIGlobalVariable::dump() const {
Dan Gohman50404362010-05-07 15:30:29 +0000688 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000689}
690
691/// dump - Print variable.
692void DIVariable::dump() const {
Dan Gohman50404362010-05-07 15:30:29 +0000693 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000694}
695
696//===----------------------------------------------------------------------===//
Chris Lattnera45664f2008-11-10 02:56:27 +0000697// DIFactory: Basic Helpers
698//===----------------------------------------------------------------------===//
699
Bill Wendlingdc817b62009-05-14 18:26:15 +0000700DIFactory::DIFactory(Module &m)
Victor Hernandez06203122010-01-23 00:03:28 +0000701 : M(m), VMContext(M.getContext()), DeclareFn(0), ValueFn(0) {}
Chris Lattner497a7a82008-11-10 04:10:34 +0000702
Chris Lattnera45664f2008-11-10 02:56:27 +0000703Constant *DIFactory::GetTagConstant(unsigned TAG) {
Devang Patel6906ba52009-01-20 19:22:03 +0000704 assert((TAG & LLVMDebugVersionMask) == 0 &&
Chris Lattnera45664f2008-11-10 02:56:27 +0000705 "Tag too large for debug encoding!");
Owen Anderson1d0be152009-08-13 21:58:54 +0000706 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
Chris Lattnera45664f2008-11-10 02:56:27 +0000707}
708
Chris Lattnera45664f2008-11-10 02:56:27 +0000709//===----------------------------------------------------------------------===//
710// DIFactory: Primary Constructors
711//===----------------------------------------------------------------------===//
712
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000713/// GetOrCreateArray - Create an descriptor for an array of descriptors.
Chris Lattnera45664f2008-11-10 02:56:27 +0000714/// This implicitly uniques the arrays created.
715DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
Benjamin Kramera53557e2010-09-21 16:41:29 +0000716 if (NumTys == 0) {
717 Value *Null = llvm::Constant::getNullValue(Type::getInt32Ty(VMContext));
718 return DIArray(MDNode::get(VMContext, &Null, 1));
719 }
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000720
Benjamin Kramera53557e2010-09-21 16:41:29 +0000721 SmallVector<Value *, 16> Elts(Tys, Tys+NumTys);
722 return DIArray(MDNode::get(VMContext, Elts.data(), Elts.size()));
Chris Lattnera45664f2008-11-10 02:56:27 +0000723}
724
725/// GetOrCreateSubrange - Create a descriptor for a value range. This
726/// implicitly uniques the values returned.
727DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
Devang Patele4b27562009-08-28 23:24:31 +0000728 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000729 GetTagConstant(dwarf::DW_TAG_subrange_type),
Owen Anderson1d0be152009-08-13 21:58:54 +0000730 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
731 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
Chris Lattnera45664f2008-11-10 02:56:27 +0000732 };
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000733
Devang Patele4b27562009-08-28 23:24:31 +0000734 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000735}
736
Devang Pateld6747df2010-10-06 20:50:40 +0000737/// CreateUnspecifiedParameter - Create unspeicified type descriptor
738/// for the subroutine type.
739DIDescriptor DIFactory::CreateUnspecifiedParameter() {
740 Value *Elts[] = {
741 GetTagConstant(dwarf::DW_TAG_unspecified_parameters)
742 };
743 return DIDescriptor(MDNode::get(VMContext, &Elts[0], 1));
744}
Chris Lattnera45664f2008-11-10 02:56:27 +0000745
746/// CreateCompileUnit - Create a new descriptor for the specified compile
747/// unit. Note that this does not unique compile units within the module.
748DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
Devang Patel65dbc902009-11-25 17:36:49 +0000749 StringRef Filename,
750 StringRef Directory,
751 StringRef Producer,
Devang Pateldd9db662009-01-30 18:20:31 +0000752 bool isMain,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000753 bool isOptimized,
Devang Patel65dbc902009-11-25 17:36:49 +0000754 StringRef Flags,
Devang Patel13319ce2009-02-17 22:43:44 +0000755 unsigned RunTimeVer) {
Devang Patele4b27562009-08-28 23:24:31 +0000756 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000757 GetTagConstant(dwarf::DW_TAG_compile_unit),
Devang Patele4b27562009-08-28 23:24:31 +0000758 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Owen Anderson1d0be152009-08-13 21:58:54 +0000759 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
Devang Patele4b27562009-08-28 23:24:31 +0000760 MDString::get(VMContext, Filename),
761 MDString::get(VMContext, Directory),
762 MDString::get(VMContext, Producer),
Owen Anderson1d0be152009-08-13 21:58:54 +0000763 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
764 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
Devang Patele4b27562009-08-28 23:24:31 +0000765 MDString::get(VMContext, Flags),
Owen Anderson1d0be152009-08-13 21:58:54 +0000766 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
Chris Lattnera45664f2008-11-10 02:56:27 +0000767 };
Devang Patele4b27562009-08-28 23:24:31 +0000768
769 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000770}
771
Devang Patel7aa81892010-03-08 22:27:22 +0000772/// CreateFile - Create a new descriptor for the specified file.
773DIFile DIFactory::CreateFile(StringRef Filename,
774 StringRef Directory,
775 DICompileUnit CU) {
776 Value *Elts[] = {
777 GetTagConstant(dwarf::DW_TAG_file_type),
778 MDString::get(VMContext, Filename),
779 MDString::get(VMContext, Directory),
Devang Patel2db49d72010-05-07 18:11:54 +0000780 CU
Devang Patel7aa81892010-03-08 22:27:22 +0000781 };
782
783 return DIFile(MDNode::get(VMContext, &Elts[0], 4));
784}
785
Chris Lattnera45664f2008-11-10 02:56:27 +0000786/// CreateEnumerator - Create a single enumerator value.
Devang Patel65dbc902009-11-25 17:36:49 +0000787DIEnumerator DIFactory::CreateEnumerator(StringRef Name, uint64_t Val){
Devang Patele4b27562009-08-28 23:24:31 +0000788 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000789 GetTagConstant(dwarf::DW_TAG_enumerator),
Devang Patele4b27562009-08-28 23:24:31 +0000790 MDString::get(VMContext, Name),
Owen Anderson1d0be152009-08-13 21:58:54 +0000791 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
Chris Lattnera45664f2008-11-10 02:56:27 +0000792 };
Devang Patele4b27562009-08-28 23:24:31 +0000793 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000794}
795
796
797/// CreateBasicType - Create a basic type like int, float, etc.
798DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000799 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000800 DIFile F,
Chris Lattnera45664f2008-11-10 02:56:27 +0000801 unsigned LineNumber,
802 uint64_t SizeInBits,
803 uint64_t AlignInBits,
804 uint64_t OffsetInBits, unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000805 unsigned Encoding) {
Devang Patele4b27562009-08-28 23:24:31 +0000806 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000807 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patel2db49d72010-05-07 18:11:54 +0000808 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000809 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000810 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000811 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
812 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
813 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
814 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
815 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
816 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
Chris Lattnera45664f2008-11-10 02:56:27 +0000817 };
Devang Patele4b27562009-08-28 23:24:31 +0000818 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000819}
820
Devang Patelac16d442009-10-26 16:54:35 +0000821
822/// CreateBasicType - Create a basic type like int, float, etc.
823DIBasicType DIFactory::CreateBasicTypeEx(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000824 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000825 DIFile F,
Devang Patelac16d442009-10-26 16:54:35 +0000826 unsigned LineNumber,
827 Constant *SizeInBits,
828 Constant *AlignInBits,
829 Constant *OffsetInBits, unsigned Flags,
830 unsigned Encoding) {
831 Value *Elts[] = {
832 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patel2db49d72010-05-07 18:11:54 +0000833 Context,
Devang Patelac16d442009-10-26 16:54:35 +0000834 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000835 F,
Devang Patelac16d442009-10-26 16:54:35 +0000836 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
837 SizeInBits,
838 AlignInBits,
839 OffsetInBits,
840 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
841 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
842 };
843 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
844}
845
Devang Patelb4645642010-02-06 01:02:37 +0000846/// CreateArtificialType - Create a new DIType with "artificial" flag set.
847DIType DIFactory::CreateArtificialType(DIType Ty) {
848 if (Ty.isArtificial())
849 return Ty;
850
851 SmallVector<Value *, 9> Elts;
Devang Patel2db49d72010-05-07 18:11:54 +0000852 MDNode *N = Ty;
Devang Patelb4645642010-02-06 01:02:37 +0000853 assert (N && "Unexpected input DIType!");
854 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
855 if (Value *V = N->getOperand(i))
856 Elts.push_back(V);
857 else
858 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
859 }
860
861 unsigned CurFlags = Ty.getFlags();
862 CurFlags = CurFlags | DIType::FlagArtificial;
863
864 // Flags are stored at this slot.
865 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
866
867 return DIType(MDNode::get(VMContext, Elts.data(), Elts.size()));
868}
Devang Patelac16d442009-10-26 16:54:35 +0000869
Chris Lattnera45664f2008-11-10 02:56:27 +0000870/// CreateDerivedType - Create a derived type like const qualified type,
871/// pointer, typedef, etc.
872DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
873 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000874 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000875 DIFile F,
Chris Lattnera45664f2008-11-10 02:56:27 +0000876 unsigned LineNumber,
877 uint64_t SizeInBits,
878 uint64_t AlignInBits,
879 uint64_t OffsetInBits,
880 unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000881 DIType DerivedFrom) {
Devang Patele4b27562009-08-28 23:24:31 +0000882 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000883 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000884 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000885 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000886 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000887 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
888 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
889 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
890 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
891 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000892 DerivedFrom,
Chris Lattnera45664f2008-11-10 02:56:27 +0000893 };
Devang Patele4b27562009-08-28 23:24:31 +0000894 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000895}
896
Devang Patelac16d442009-10-26 16:54:35 +0000897
898/// CreateDerivedType - Create a derived type like const qualified type,
899/// pointer, typedef, etc.
900DIDerivedType DIFactory::CreateDerivedTypeEx(unsigned Tag,
901 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000902 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000903 DIFile F,
Devang Patelac16d442009-10-26 16:54:35 +0000904 unsigned LineNumber,
905 Constant *SizeInBits,
906 Constant *AlignInBits,
907 Constant *OffsetInBits,
908 unsigned Flags,
909 DIType DerivedFrom) {
910 Value *Elts[] = {
911 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000912 Context,
Devang Patelac16d442009-10-26 16:54:35 +0000913 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000914 F,
Devang Patelac16d442009-10-26 16:54:35 +0000915 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
916 SizeInBits,
917 AlignInBits,
918 OffsetInBits,
919 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000920 DerivedFrom,
Devang Patelac16d442009-10-26 16:54:35 +0000921 };
922 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
923}
924
925
Chris Lattnera45664f2008-11-10 02:56:27 +0000926/// CreateCompositeType - Create a composite type like array, struct, etc.
927DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
928 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000929 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000930 DIFile F,
Chris Lattnera45664f2008-11-10 02:56:27 +0000931 unsigned LineNumber,
932 uint64_t SizeInBits,
933 uint64_t AlignInBits,
934 uint64_t OffsetInBits,
935 unsigned Flags,
936 DIType DerivedFrom,
Devang Patel13319ce2009-02-17 22:43:44 +0000937 DIArray Elements,
Devang Patel0fd7f9d2010-01-26 21:14:59 +0000938 unsigned RuntimeLang,
939 MDNode *ContainingType) {
Owen Andersone277fed2009-07-07 16:31:25 +0000940
Devang Patele4b27562009-08-28 23:24:31 +0000941 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000942 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000943 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000944 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000945 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000946 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
947 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
948 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
949 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
950 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000951 DerivedFrom,
952 Elements,
Devang Patel0fd7f9d2010-01-26 21:14:59 +0000953 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
954 ContainingType
Chris Lattnera45664f2008-11-10 02:56:27 +0000955 };
Devang Patele7e5a0f2010-08-10 20:01:20 +0000956
957 MDNode *Node = MDNode::get(VMContext, &Elts[0], 13);
958 // Create a named metadata so that we do not lose this enum info.
959 if (Tag == dwarf::DW_TAG_enumeration_type) {
960 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.enum");
961 NMD->addOperand(Node);
962 }
963 return DICompositeType(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +0000964}
965
Dan Gohman489b29b2010-08-20 22:02:26 +0000966/// CreateTemporaryType - Create a temporary forward-declared type.
Dan Gohmana3833f12010-08-20 22:39:47 +0000967DIType DIFactory::CreateTemporaryType() {
Dan Gohman489b29b2010-08-20 22:02:26 +0000968 // Give the temporary MDNode a tag. It doesn't matter what tag we
969 // use here as long as DIType accepts it.
970 Value *Elts[] = {
971 GetTagConstant(DW_TAG_base_type)
972 };
973 MDNode *Node = MDNode::getTemporary(VMContext, Elts, array_lengthof(Elts));
974 return DIType(Node);
975}
976
Devang Patelfe58f952010-12-07 23:25:47 +0000977/// CreateTemporaryType - Create a temporary forward-declared type.
978DIType DIFactory::CreateTemporaryType(DIFile F) {
979 // Give the temporary MDNode a tag. It doesn't matter what tag we
980 // use here as long as DIType accepts it.
981 Value *Elts[] = {
982 GetTagConstant(DW_TAG_base_type),
983 F.getCompileUnit(),
984 NULL,
985 F
986 };
987 MDNode *Node = MDNode::getTemporary(VMContext, Elts, array_lengthof(Elts));
988 return DIType(Node);
989}
Dan Gohman489b29b2010-08-20 22:02:26 +0000990
Devang Patelac16d442009-10-26 16:54:35 +0000991/// CreateCompositeType - Create a composite type like array, struct, etc.
992DICompositeType DIFactory::CreateCompositeTypeEx(unsigned Tag,
993 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000994 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000995 DIFile F,
Devang Patelac16d442009-10-26 16:54:35 +0000996 unsigned LineNumber,
997 Constant *SizeInBits,
998 Constant *AlignInBits,
999 Constant *OffsetInBits,
1000 unsigned Flags,
1001 DIType DerivedFrom,
1002 DIArray Elements,
Devang Patel6bf058c2010-08-10 20:22:49 +00001003 unsigned RuntimeLang,
1004 MDNode *ContainingType) {
Devang Patelac16d442009-10-26 16:54:35 +00001005 Value *Elts[] = {
1006 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +00001007 Context,
Devang Patelac16d442009-10-26 16:54:35 +00001008 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +00001009 F,
Devang Patelac16d442009-10-26 16:54:35 +00001010 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
1011 SizeInBits,
1012 AlignInBits,
1013 OffsetInBits,
1014 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +00001015 DerivedFrom,
1016 Elements,
Devang Patel6bf058c2010-08-10 20:22:49 +00001017 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
1018 ContainingType
Devang Patelac16d442009-10-26 16:54:35 +00001019 };
Devang Patel6bf058c2010-08-10 20:22:49 +00001020 MDNode *Node = MDNode::get(VMContext, &Elts[0], 13);
Devang Patele7e5a0f2010-08-10 20:01:20 +00001021 // Create a named metadata so that we do not lose this enum info.
1022 if (Tag == dwarf::DW_TAG_enumeration_type) {
1023 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.enum");
1024 NMD->addOperand(Node);
1025 }
1026 return DICompositeType(Node);
Devang Patelac16d442009-10-26 16:54:35 +00001027}
1028
1029
Chris Lattnera45664f2008-11-10 02:56:27 +00001030/// CreateSubprogram - Create a new descriptor for the specified subprogram.
1031/// See comments in DISubprogram for descriptions of these fields. This
1032/// method does not unique the generated descriptors.
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001033DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +00001034 StringRef Name,
1035 StringRef DisplayName,
1036 StringRef LinkageName,
Devang Patel4b945502010-03-09 00:44:10 +00001037 DIFile F,
Devang Patel2e369932010-01-23 00:26:28 +00001038 unsigned LineNo, DIType Ty,
Chris Lattnera45664f2008-11-10 02:56:27 +00001039 bool isLocalToUnit,
Devang Patel5d11eb02009-12-03 19:11:07 +00001040 bool isDefinition,
1041 unsigned VK, unsigned VIndex,
Devang Patel4e0d19d2010-02-03 19:57:19 +00001042 DIType ContainingType,
Devang Patel9dd2b472010-09-29 21:04:46 +00001043 unsigned Flags,
Stuart Hastings215aa152010-06-11 20:08:44 +00001044 bool isOptimized,
1045 Function *Fn) {
Devang Patel854967e2008-12-17 22:39:29 +00001046
Devang Patele4b27562009-08-28 23:24:31 +00001047 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001048 GetTagConstant(dwarf::DW_TAG_subprogram),
Devang Patele4b27562009-08-28 23:24:31 +00001049 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Devang Patel2db49d72010-05-07 18:11:54 +00001050 Context,
Devang Patele4b27562009-08-28 23:24:31 +00001051 MDString::get(VMContext, Name),
1052 MDString::get(VMContext, DisplayName),
1053 MDString::get(VMContext, LinkageName),
Devang Patel2db49d72010-05-07 18:11:54 +00001054 F,
Owen Anderson1d0be152009-08-13 21:58:54 +00001055 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001056 Ty,
Owen Anderson1d0be152009-08-13 21:58:54 +00001057 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
Devang Patel5d11eb02009-12-03 19:11:07 +00001058 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1059 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
1060 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
Devang Patel2db49d72010-05-07 18:11:54 +00001061 ContainingType,
Devang Patel9dd2b472010-09-29 21:04:46 +00001062 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Stuart Hastings215aa152010-06-11 20:08:44 +00001063 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
1064 Fn
Chris Lattnera45664f2008-11-10 02:56:27 +00001065 };
Devang Patelfd5fdc32010-06-28 05:53:08 +00001066 MDNode *Node = MDNode::get(VMContext, &Elts[0], 17);
1067
1068 // Create a named metadata so that we do not lose this mdnode.
1069 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
1070 NMD->addOperand(Node);
1071 return DISubprogram(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +00001072}
1073
Devang Patele3a18de2009-12-01 23:09:02 +00001074/// CreateSubprogramDefinition - Create new subprogram descriptor for the
Jim Grosbache62b6902010-07-21 21:36:25 +00001075/// given declaration.
1076DISubprogram DIFactory::CreateSubprogramDefinition(DISubprogram &SPDeclaration){
Devang Patele3a18de2009-12-01 23:09:02 +00001077 if (SPDeclaration.isDefinition())
Devang Patel2db49d72010-05-07 18:11:54 +00001078 return DISubprogram(SPDeclaration);
Devang Patele3a18de2009-12-01 23:09:02 +00001079
Devang Patel2db49d72010-05-07 18:11:54 +00001080 MDNode *DeclNode = SPDeclaration;
Devang Patele3a18de2009-12-01 23:09:02 +00001081 Value *Elts[] = {
1082 GetTagConstant(dwarf::DW_TAG_subprogram),
1083 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001084 DeclNode->getOperand(2), // Context
1085 DeclNode->getOperand(3), // Name
1086 DeclNode->getOperand(4), // DisplayName
1087 DeclNode->getOperand(5), // LinkageName
1088 DeclNode->getOperand(6), // CompileUnit
1089 DeclNode->getOperand(7), // LineNo
1090 DeclNode->getOperand(8), // Type
1091 DeclNode->getOperand(9), // isLocalToUnit
Stuart Hastings6d56b9f2010-06-05 00:39:29 +00001092 ConstantInt::get(Type::getInt1Ty(VMContext), true),
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001093 DeclNode->getOperand(11), // Virtuality
1094 DeclNode->getOperand(12), // VIndex
Devang Patel4e0d19d2010-02-03 19:57:19 +00001095 DeclNode->getOperand(13), // Containting Type
Devang Patel9dd2b472010-09-29 21:04:46 +00001096 DeclNode->getOperand(14), // Flags
Devang Patelacc6efa2010-06-27 21:04:31 +00001097 DeclNode->getOperand(15), // isOptimized
1098 SPDeclaration.getFunction()
Devang Patele3a18de2009-12-01 23:09:02 +00001099 };
Devang Patelfd5fdc32010-06-28 05:53:08 +00001100 MDNode *Node =MDNode::get(VMContext, &Elts[0], 16);
1101
1102 // Create a named metadata so that we do not lose this mdnode.
1103 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
1104 NMD->addOperand(Node);
1105 return DISubprogram(Node);
Devang Patele3a18de2009-12-01 23:09:02 +00001106}
1107
Chris Lattnera45664f2008-11-10 02:56:27 +00001108/// CreateGlobalVariable - Create a new descriptor for the specified global.
1109DIGlobalVariable
Devang Patel65dbc902009-11-25 17:36:49 +00001110DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
1111 StringRef DisplayName,
1112 StringRef LinkageName,
Devang Patel4b945502010-03-09 00:44:10 +00001113 DIFile F,
Devang Patel2e369932010-01-23 00:26:28 +00001114 unsigned LineNo, DIType Ty,bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +00001115 bool isDefinition, llvm::GlobalVariable *Val) {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001116 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001117 GetTagConstant(dwarf::DW_TAG_variable),
Devang Patele4b27562009-08-28 23:24:31 +00001118 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Devang Patel2db49d72010-05-07 18:11:54 +00001119 Context,
Devang Patele4b27562009-08-28 23:24:31 +00001120 MDString::get(VMContext, Name),
1121 MDString::get(VMContext, DisplayName),
1122 MDString::get(VMContext, LinkageName),
Devang Patel2db49d72010-05-07 18:11:54 +00001123 F,
Owen Anderson1d0be152009-08-13 21:58:54 +00001124 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001125 Ty,
Owen Anderson1d0be152009-08-13 21:58:54 +00001126 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1127 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
Devang Patele4b27562009-08-28 23:24:31 +00001128 Val
Chris Lattnera45664f2008-11-10 02:56:27 +00001129 };
Devang Patele4b27562009-08-28 23:24:31 +00001130
1131 Value *const *Vs = &Elts[0];
1132 MDNode *Node = MDNode::get(VMContext,Vs, 12);
1133
1134 // Create a named metadata so that we do not lose this mdnode.
1135 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001136 NMD->addOperand(Node);
Devang Patele4b27562009-08-28 23:24:31 +00001137
1138 return DIGlobalVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +00001139}
1140
Devang Patel27398962010-08-09 21:39:24 +00001141/// CreateGlobalVariable - Create a new descriptor for the specified constant.
1142DIGlobalVariable
1143DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
1144 StringRef DisplayName,
1145 StringRef LinkageName,
1146 DIFile F,
1147 unsigned LineNo, DIType Ty,bool isLocalToUnit,
1148 bool isDefinition, llvm::Constant *Val) {
1149 Value *Elts[] = {
Devang Patelebd53742010-08-11 23:17:54 +00001150 GetTagConstant(dwarf::DW_TAG_variable),
Devang Patel27398962010-08-09 21:39:24 +00001151 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
1152 Context,
1153 MDString::get(VMContext, Name),
1154 MDString::get(VMContext, DisplayName),
1155 MDString::get(VMContext, LinkageName),
1156 F,
1157 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1158 Ty,
1159 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1160 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1161 Val
1162 };
1163
1164 Value *const *Vs = &Elts[0];
1165 MDNode *Node = MDNode::get(VMContext,Vs, 12);
1166
1167 // Create a named metadata so that we do not lose this mdnode.
1168 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
1169 NMD->addOperand(Node);
1170
1171 return DIGlobalVariable(Node);
1172}
Chris Lattnera45664f2008-11-10 02:56:27 +00001173
Devang Patel62367042010-11-10 22:19:21 +00001174/// fixupObjcLikeName - Replace contains special characters used
1175/// in a typical Objective-C names with '.' in a given string.
1176static void fixupObjcLikeName(std::string &Str) {
1177 for (size_t i = 0, e = Str.size(); i < e; ++i) {
1178 char C = Str[i];
Jakob Stoklund Olesen5b3f7792010-12-03 23:40:45 +00001179 if (C == '[' || C == ']' || C == ' ' || C == ':' || C == '+' ||
1180 C == '(' || C == ')')
Devang Patel62367042010-11-10 22:19:21 +00001181 Str[i] = '.';
1182 }
1183}
1184
1185/// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
1186/// to hold function specific information.
1187NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, StringRef FuncName) {
1188 SmallString<32> Out;
1189 if (FuncName.find('[') == StringRef::npos)
1190 return M.getOrInsertNamedMetadata(Twine("llvm.dbg.lv.", FuncName)
1191 .toStringRef(Out));
1192 std::string Name = FuncName;
1193 fixupObjcLikeName(Name);
1194 return M.getOrInsertNamedMetadata(Twine("llvm.dbg.lv.", Name)
1195 .toStringRef(Out));
1196}
1197
1198/// getFnSpecificMDNode - Return a NameMDNode, if available, that is
1199/// suitable to hold function specific information.
1200NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, StringRef FuncName) {
1201 if (FuncName.find('[') == StringRef::npos)
1202 return M.getNamedMetadata(Twine("llvm.dbg.lv.", FuncName));
1203 std::string Name = FuncName;
1204 fixupObjcLikeName(Name);
1205 return M.getNamedMetadata(Twine("llvm.dbg.lv.", Name));
1206}
1207
Chris Lattnera45664f2008-11-10 02:56:27 +00001208/// CreateVariable - Create a new descriptor for the specified variable.
1209DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +00001210 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +00001211 DIFile F,
1212 unsigned LineNo,
Devang Patel3cf763d2010-09-29 23:07:21 +00001213 DIType Ty, bool AlwaysPreserve,
1214 unsigned Flags) {
Devang Patele4b27562009-08-28 23:24:31 +00001215 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001216 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +00001217 Context,
Devang Patele4b27562009-08-28 23:24:31 +00001218 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +00001219 F,
Owen Anderson1d0be152009-08-13 21:58:54 +00001220 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001221 Ty,
Devang Patel3cf763d2010-09-29 23:07:21 +00001222 ConstantInt::get(Type::getInt32Ty(VMContext), Flags)
Chris Lattnera45664f2008-11-10 02:56:27 +00001223 };
Devang Patel3cf763d2010-09-29 23:07:21 +00001224 MDNode *Node = MDNode::get(VMContext, &Elts[0], 7);
Devang Patel6ed0ce32010-05-20 20:35:24 +00001225 if (AlwaysPreserve) {
1226 // The optimizer may remove local variable. If there is an interest
1227 // to preserve variable info in such situation then stash it in a
1228 // named mdnode.
Devang Patel2f7d5292010-06-16 00:53:55 +00001229 DISubprogram Fn(getDISubprogram(Context));
Devang Patel10de3bb2010-06-21 18:36:58 +00001230 StringRef FName = "fn";
1231 if (Fn.getFunction())
1232 FName = Fn.getFunction()->getName();
Devang Patel10de3bb2010-06-21 18:36:58 +00001233 char One = '\1';
1234 if (FName.startswith(StringRef(&One, 1)))
1235 FName = FName.substr(1);
Dan Gohman17aa92c2010-07-21 23:38:33 +00001236
Devang Patel62367042010-11-10 22:19:21 +00001237
1238 NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, FName);
Devang Patel2f7d5292010-06-16 00:53:55 +00001239 FnLocals->addOperand(Node);
Devang Patel98e1cac2010-05-14 21:01:35 +00001240 }
1241 return DIVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +00001242}
1243
1244
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001245/// CreateComplexVariable - Create a new descriptor for the specified variable
1246/// which has a complex address expression for its address.
1247DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
Benjamin Kramer28b4afc2010-09-21 16:00:03 +00001248 StringRef Name, DIFile F,
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001249 unsigned LineNo,
Benjamin Kramer28b4afc2010-09-21 16:00:03 +00001250 DIType Ty, Value *const *Addr,
1251 unsigned NumAddr) {
1252 SmallVector<Value *, 15> Elts;
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001253 Elts.push_back(GetTagConstant(Tag));
Devang Patel2db49d72010-05-07 18:11:54 +00001254 Elts.push_back(Context);
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001255 Elts.push_back(MDString::get(VMContext, Name));
Devang Patel2db49d72010-05-07 18:11:54 +00001256 Elts.push_back(F);
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001257 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
Devang Patel2db49d72010-05-07 18:11:54 +00001258 Elts.push_back(Ty);
Benjamin Kramer28b4afc2010-09-21 16:00:03 +00001259 Elts.append(Addr, Addr+NumAddr);
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001260
Benjamin Kramer28b4afc2010-09-21 16:00:03 +00001261 return DIVariable(MDNode::get(VMContext, Elts.data(), Elts.size()));
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001262}
1263
1264
Chris Lattnera45664f2008-11-10 02:56:27 +00001265/// CreateBlock - This creates a descriptor for a lexical block with the
Owen Anderson99035272009-07-07 17:12:53 +00001266/// specified parent VMContext.
Devang Patel3d821aa2010-02-16 21:39:34 +00001267DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context,
Stuart Hastings0db42712010-07-19 23:56:30 +00001268 DIFile F, unsigned LineNo,
1269 unsigned Col) {
1270 // Defeat MDNode uniqing for lexical blocks.
1271 static unsigned int unique_id = 0;
Devang Patele4b27562009-08-28 23:24:31 +00001272 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001273 GetTagConstant(dwarf::DW_TAG_lexical_block),
Devang Patel2db49d72010-05-07 18:11:54 +00001274 Context,
Devang Patel3d821aa2010-02-16 21:39:34 +00001275 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Stuart Hastings0db42712010-07-19 23:56:30 +00001276 ConstantInt::get(Type::getInt32Ty(VMContext), Col),
1277 F,
1278 ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
Chris Lattnera45664f2008-11-10 02:56:27 +00001279 };
Stuart Hastings0db42712010-07-19 23:56:30 +00001280 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 6));
Chris Lattnera45664f2008-11-10 02:56:27 +00001281}
1282
Devang Patel6404e4e2009-12-15 19:16:48 +00001283/// CreateNameSpace - This creates new descriptor for a namespace
1284/// with the specified parent context.
1285DINameSpace DIFactory::CreateNameSpace(DIDescriptor Context, StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +00001286 DIFile F,
Devang Patel6404e4e2009-12-15 19:16:48 +00001287 unsigned LineNo) {
1288 Value *Elts[] = {
1289 GetTagConstant(dwarf::DW_TAG_namespace),
Devang Patel2db49d72010-05-07 18:11:54 +00001290 Context,
Devang Patel6404e4e2009-12-15 19:16:48 +00001291 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +00001292 F,
Devang Patel6404e4e2009-12-15 19:16:48 +00001293 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1294 };
1295 return DINameSpace(MDNode::get(VMContext, &Elts[0], 5));
1296}
1297
Devang Patelf98d8fe2009-09-01 01:14:15 +00001298/// CreateLocation - Creates a debug info location.
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001299DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
Daniel Dunbara279bc32009-09-20 02:20:51 +00001300 DIScope S, DILocation OrigLoc) {
Devang Patelf98d8fe2009-09-01 01:14:15 +00001301 Value *Elts[] = {
1302 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1303 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001304 S,
1305 OrigLoc,
Devang Patelf98d8fe2009-09-01 01:14:15 +00001306 };
1307 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1308}
1309
Chris Lattnera45664f2008-11-10 02:56:27 +00001310//===----------------------------------------------------------------------===//
1311// DIFactory: Routines for inserting code into a function
1312//===----------------------------------------------------------------------===//
1313
Chris Lattnera45664f2008-11-10 02:56:27 +00001314/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Devang Patel6daf99b2009-11-10 22:05:35 +00001315Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001316 Instruction *InsertBefore) {
Victor Hernandez4cf292a2010-01-26 02:07:38 +00001317 assert(Storage && "no storage passed to dbg.declare");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001318 assert(D.Verify() && "empty DIVariable passed to dbg.declare");
Chris Lattnera45664f2008-11-10 02:56:27 +00001319 if (!DeclareFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +00001320 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1321
Victor Hernandez756462b2010-01-18 20:42:09 +00001322 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
Devang Patel2db49d72010-05-07 18:11:54 +00001323 D };
Devang Patel6daf99b2009-11-10 22:05:35 +00001324 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
Mike Stumpe4250392009-10-01 22:08:58 +00001325}
1326
1327/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Devang Patel6daf99b2009-11-10 22:05:35 +00001328Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001329 BasicBlock *InsertAtEnd) {
Victor Hernandez4cf292a2010-01-26 02:07:38 +00001330 assert(Storage && "no storage passed to dbg.declare");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001331 assert(D.Verify() && "invalid DIVariable passed to dbg.declare");
Mike Stumpe4250392009-10-01 22:08:58 +00001332 if (!DeclareFn)
1333 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1334
Victor Hernandez756462b2010-01-18 20:42:09 +00001335 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
Devang Patel2db49d72010-05-07 18:11:54 +00001336 D };
Devang Patel27a53de2010-01-29 18:30:57 +00001337
1338 // If this block already has a terminator then insert this intrinsic
1339 // before the terminator.
Jim Grosbache62b6902010-07-21 21:36:25 +00001340 if (TerminatorInst *T = InsertAtEnd->getTerminator())
Devang Patel27a53de2010-01-29 18:30:57 +00001341 return CallInst::Create(DeclareFn, Args, Args+2, "", T);
1342 else
1343 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);}
Torok Edwin620f2802008-12-16 09:07:36 +00001344
Victor Hernandezc59b3352009-12-07 21:54:43 +00001345/// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001346Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
Victor Hernandezc59b3352009-12-07 21:54:43 +00001347 DIVariable D,
1348 Instruction *InsertBefore) {
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001349 assert(V && "no value passed to dbg.value");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001350 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001351 if (!ValueFn)
1352 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1353
Victor Hernandezc8b7cd02010-01-20 05:44:11 +00001354 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001355 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
Devang Patel2db49d72010-05-07 18:11:54 +00001356 D };
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001357 return CallInst::Create(ValueFn, Args, Args+3, "", InsertBefore);
1358}
1359
Victor Hernandezc59b3352009-12-07 21:54:43 +00001360/// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001361Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
Victor Hernandezc59b3352009-12-07 21:54:43 +00001362 DIVariable D,
1363 BasicBlock *InsertAtEnd) {
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001364 assert(V && "no value passed to dbg.value");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001365 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001366 if (!ValueFn)
1367 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1368
Jim Grosbache62b6902010-07-21 21:36:25 +00001369 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001370 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
Devang Patel2db49d72010-05-07 18:11:54 +00001371 D };
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001372 return CallInst::Create(ValueFn, Args, Args+3, "", InsertAtEnd);
1373}
Devang Patele4b27562009-08-28 23:24:31 +00001374
Devang Patel1a7ca032010-09-28 18:08:20 +00001375// RecordType - Record DIType in a module such that it is not lost even if
1376// it is not referenced through debug info anchors.
1377void DIFactory::RecordType(DIType T) {
1378 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.ty");
1379 NMD->addOperand(T);
1380}
1381
1382
Devang Pateld2f79a12009-07-28 19:55:13 +00001383//===----------------------------------------------------------------------===//
Devang Patel98c65172009-07-30 18:25:15 +00001384// DebugInfoFinder implementations.
Devang Pateld2f79a12009-07-28 19:55:13 +00001385//===----------------------------------------------------------------------===//
1386
Devang Patel98c65172009-07-30 18:25:15 +00001387/// processModule - Process entire module and collect debug info.
1388void DebugInfoFinder::processModule(Module &M) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001389 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1390 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
1391 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
1392 ++BI) {
Devang Patel01c5ff62010-05-04 01:05:02 +00001393 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
Devang Patelb4d31302009-07-31 18:18:52 +00001394 processDeclare(DDI);
Jim Grosbache62b6902010-07-21 21:36:25 +00001395
Chris Lattner28a9bf62010-04-02 20:44:29 +00001396 DebugLoc Loc = BI->getDebugLoc();
1397 if (Loc.isUnknown())
1398 continue;
Jim Grosbache62b6902010-07-21 21:36:25 +00001399
Chris Lattner28a9bf62010-04-02 20:44:29 +00001400 LLVMContext &Ctx = BI->getContext();
1401 DIDescriptor Scope(Loc.getScope(Ctx));
Jim Grosbache62b6902010-07-21 21:36:25 +00001402
Chris Lattner28a9bf62010-04-02 20:44:29 +00001403 if (Scope.isCompileUnit())
Devang Patel2db49d72010-05-07 18:11:54 +00001404 addCompileUnit(DICompileUnit(Scope));
Chris Lattner28a9bf62010-04-02 20:44:29 +00001405 else if (Scope.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001406 processSubprogram(DISubprogram(Scope));
Chris Lattner28a9bf62010-04-02 20:44:29 +00001407 else if (Scope.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001408 processLexicalBlock(DILexicalBlock(Scope));
Jim Grosbache62b6902010-07-21 21:36:25 +00001409
Chris Lattner28a9bf62010-04-02 20:44:29 +00001410 if (MDNode *IA = Loc.getInlinedAt(Ctx))
1411 processLocation(DILocation(IA));
Devang Pateld2f79a12009-07-28 19:55:13 +00001412 }
Devang Patele4b27562009-08-28 23:24:31 +00001413
Devang Patelfd5fdc32010-06-28 05:53:08 +00001414 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
1415 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1416 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
1417 if (addGlobalVariable(DIG)) {
1418 addCompileUnit(DIG.getCompileUnit());
1419 processType(DIG.getType());
1420 }
Devang Pateld2f79a12009-07-28 19:55:13 +00001421 }
1422 }
Devang Patelfd5fdc32010-06-28 05:53:08 +00001423
1424 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
1425 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
1426 processSubprogram(DISubprogram(NMD->getOperand(i)));
Devang Pateld2f79a12009-07-28 19:55:13 +00001427}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001428
Devang Patel6daf99b2009-11-10 22:05:35 +00001429/// processLocation - Process DILocation.
1430void DebugInfoFinder::processLocation(DILocation Loc) {
Devang Patel3c91b052010-03-08 20:52:55 +00001431 if (!Loc.Verify()) return;
Devang Patel2db49d72010-05-07 18:11:54 +00001432 DIDescriptor S(Loc.getScope());
Devang Patel6daf99b2009-11-10 22:05:35 +00001433 if (S.isCompileUnit())
Devang Patel2db49d72010-05-07 18:11:54 +00001434 addCompileUnit(DICompileUnit(S));
Devang Patel6daf99b2009-11-10 22:05:35 +00001435 else if (S.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001436 processSubprogram(DISubprogram(S));
Devang Patel6daf99b2009-11-10 22:05:35 +00001437 else if (S.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001438 processLexicalBlock(DILexicalBlock(S));
Devang Patel6daf99b2009-11-10 22:05:35 +00001439 processLocation(Loc.getOrigLocation());
1440}
1441
Devang Patel98c65172009-07-30 18:25:15 +00001442/// processType - Process DIType.
1443void DebugInfoFinder::processType(DIType DT) {
Devang Patel72bcdb62009-08-10 22:09:58 +00001444 if (!addType(DT))
Devang Pateld2f79a12009-07-28 19:55:13 +00001445 return;
1446
1447 addCompileUnit(DT.getCompileUnit());
Devang Patel6ceea332009-08-31 18:49:10 +00001448 if (DT.isCompositeType()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001449 DICompositeType DCT(DT);
Devang Patel98c65172009-07-30 18:25:15 +00001450 processType(DCT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001451 DIArray DA = DCT.getTypeArray();
Devang Patel3c91b052010-03-08 20:52:55 +00001452 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1453 DIDescriptor D = DA.getElement(i);
1454 if (D.isType())
Devang Patel2db49d72010-05-07 18:11:54 +00001455 processType(DIType(D));
Devang Patel3c91b052010-03-08 20:52:55 +00001456 else if (D.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001457 processSubprogram(DISubprogram(D));
Devang Patel3c91b052010-03-08 20:52:55 +00001458 }
Devang Patel6ceea332009-08-31 18:49:10 +00001459 } else if (DT.isDerivedType()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001460 DIDerivedType DDT(DT);
Devang Patel3c91b052010-03-08 20:52:55 +00001461 processType(DDT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001462 }
1463}
1464
Devang Patelbeab41b2009-10-07 22:04:08 +00001465/// processLexicalBlock
1466void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
Devang Patelbeab41b2009-10-07 22:04:08 +00001467 DIScope Context = LB.getContext();
1468 if (Context.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001469 return processLexicalBlock(DILexicalBlock(Context));
Devang Patelbeab41b2009-10-07 22:04:08 +00001470 else
Devang Patel2db49d72010-05-07 18:11:54 +00001471 return processSubprogram(DISubprogram(Context));
Devang Patelbeab41b2009-10-07 22:04:08 +00001472}
1473
Devang Patel98c65172009-07-30 18:25:15 +00001474/// processSubprogram - Process DISubprogram.
1475void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001476 if (!addSubprogram(SP))
1477 return;
1478 addCompileUnit(SP.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +00001479 processType(SP.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +00001480}
1481
Devang Patelb4d31302009-07-31 18:18:52 +00001482/// processDeclare - Process DbgDeclareInst.
1483void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
Devang Patel3c91b052010-03-08 20:52:55 +00001484 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1485 if (!N) return;
1486
1487 DIDescriptor DV(N);
1488 if (!DV.isVariable())
Devang Patelb4d31302009-07-31 18:18:52 +00001489 return;
1490
Devang Patel2db49d72010-05-07 18:11:54 +00001491 if (!NodesSeen.insert(DV))
Devang Patelb4d31302009-07-31 18:18:52 +00001492 return;
1493
Devang Patel3c91b052010-03-08 20:52:55 +00001494 addCompileUnit(DIVariable(N).getCompileUnit());
1495 processType(DIVariable(N).getType());
Devang Patelb4d31302009-07-31 18:18:52 +00001496}
1497
Devang Patel72bcdb62009-08-10 22:09:58 +00001498/// addType - Add type into Tys.
1499bool DebugInfoFinder::addType(DIType DT) {
Devang Patel3c91b052010-03-08 20:52:55 +00001500 if (!DT.isValid())
Devang Patel72bcdb62009-08-10 22:09:58 +00001501 return false;
1502
Devang Patel2db49d72010-05-07 18:11:54 +00001503 if (!NodesSeen.insert(DT))
Devang Patel72bcdb62009-08-10 22:09:58 +00001504 return false;
1505
Devang Patel2db49d72010-05-07 18:11:54 +00001506 TYs.push_back(DT);
Devang Patel72bcdb62009-08-10 22:09:58 +00001507 return true;
1508}
1509
Devang Pateld2f79a12009-07-28 19:55:13 +00001510/// addCompileUnit - Add compile unit into CUs.
Devang Patel98c65172009-07-30 18:25:15 +00001511bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Patel3c91b052010-03-08 20:52:55 +00001512 if (!CU.Verify())
Devang Pateld2f79a12009-07-28 19:55:13 +00001513 return false;
1514
Devang Patel2db49d72010-05-07 18:11:54 +00001515 if (!NodesSeen.insert(CU))
Devang Pateld2f79a12009-07-28 19:55:13 +00001516 return false;
1517
Devang Patel2db49d72010-05-07 18:11:54 +00001518 CUs.push_back(CU);
Devang Pateld2f79a12009-07-28 19:55:13 +00001519 return true;
1520}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001521
Devang Pateld2f79a12009-07-28 19:55:13 +00001522/// addGlobalVariable - Add global variable into GVs.
Devang Patel98c65172009-07-30 18:25:15 +00001523bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Patel2db49d72010-05-07 18:11:54 +00001524 if (!DIDescriptor(DIG).isGlobalVariable())
Devang Pateld2f79a12009-07-28 19:55:13 +00001525 return false;
1526
Devang Patel2db49d72010-05-07 18:11:54 +00001527 if (!NodesSeen.insert(DIG))
Devang Pateld2f79a12009-07-28 19:55:13 +00001528 return false;
1529
Devang Patel2db49d72010-05-07 18:11:54 +00001530 GVs.push_back(DIG);
Devang Pateld2f79a12009-07-28 19:55:13 +00001531 return true;
1532}
1533
1534// addSubprogram - Add subprgoram into SPs.
Devang Patel98c65172009-07-30 18:25:15 +00001535bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Patel2db49d72010-05-07 18:11:54 +00001536 if (!DIDescriptor(SP).isSubprogram())
Devang Pateld2f79a12009-07-28 19:55:13 +00001537 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001538
Devang Patel2db49d72010-05-07 18:11:54 +00001539 if (!NodesSeen.insert(SP))
Devang Pateld2f79a12009-07-28 19:55:13 +00001540 return false;
1541
Devang Patel2db49d72010-05-07 18:11:54 +00001542 SPs.push_back(SP);
Devang Pateld2f79a12009-07-28 19:55:13 +00001543 return true;
1544}
1545
Victor Hernandez756462b2010-01-18 20:42:09 +00001546/// Find the debug info descriptor corresponding to this global variable.
1547static Value *findDbgGlobalDeclare(GlobalVariable *V) {
Chris Lattner099b7792009-12-29 09:22:47 +00001548 const Module *M = V->getParent();
1549 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1550 if (!NMD)
Torok Edwinff7d0e92009-03-10 13:41:26 +00001551 return 0;
Chris Lattner099b7792009-12-29 09:22:47 +00001552
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001553 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman872814a2010-07-21 18:54:18 +00001554 DIDescriptor DIG(cast<MDNode>(NMD->getOperand(i)));
Devang Patel3c91b052010-03-08 20:52:55 +00001555 if (!DIG.isGlobalVariable())
Chris Lattner099b7792009-12-29 09:22:47 +00001556 continue;
Devang Patel2db49d72010-05-07 18:11:54 +00001557 if (DIGlobalVariable(DIG).getGlobal() == V)
1558 return DIG;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001559 }
Chris Lattner099b7792009-12-29 09:22:47 +00001560 return 0;
1561}
Torok Edwinff7d0e92009-03-10 13:41:26 +00001562
Chris Lattner099b7792009-12-29 09:22:47 +00001563/// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
1564/// It looks through pointer casts too.
Victor Hernandez756462b2010-01-18 20:42:09 +00001565static const DbgDeclareInst *findDbgDeclare(const Value *V) {
Victor Hernandez3a328652010-01-15 19:04:09 +00001566 V = V->stripPointerCasts();
Jim Grosbache62b6902010-07-21 21:36:25 +00001567
Victor Hernandez3a328652010-01-15 19:04:09 +00001568 if (!isa<Instruction>(V) && !isa<Argument>(V))
Torok Edwin620f2802008-12-16 09:07:36 +00001569 return 0;
Jim Grosbache62b6902010-07-21 21:36:25 +00001570
Victor Hernandez3a328652010-01-15 19:04:09 +00001571 const Function *F = NULL;
1572 if (const Instruction *I = dyn_cast<Instruction>(V))
1573 F = I->getParent()->getParent();
1574 else if (const Argument *A = dyn_cast<Argument>(V))
1575 F = A->getParent();
Jim Grosbache62b6902010-07-21 21:36:25 +00001576
Victor Hernandez3a328652010-01-15 19:04:09 +00001577 for (Function::const_iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
1578 for (BasicBlock::const_iterator BI = (*FI).begin(), BE = (*FI).end();
1579 BI != BE; ++BI)
1580 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1581 if (DDI->getAddress() == V)
1582 return DDI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001583
Chris Lattner099b7792009-12-29 09:22:47 +00001584 return 0;
1585}
Bill Wendlingdc817b62009-05-14 18:26:15 +00001586
Chris Lattner099b7792009-12-29 09:22:47 +00001587bool llvm::getLocationInfo(const Value *V, std::string &DisplayName,
1588 std::string &Type, unsigned &LineNo,
1589 std::string &File, std::string &Dir) {
1590 DICompileUnit Unit;
1591 DIType TypeD;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001592
Chris Lattner099b7792009-12-29 09:22:47 +00001593 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1594 Value *DIGV = findDbgGlobalDeclare(GV);
1595 if (!DIGV) return false;
1596 DIGlobalVariable Var(cast<MDNode>(DIGV));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001597
Chris Lattner099b7792009-12-29 09:22:47 +00001598 StringRef D = Var.getDisplayName();
Devang Patel65dbc902009-11-25 17:36:49 +00001599 if (!D.empty())
Chris Lattner099b7792009-12-29 09:22:47 +00001600 DisplayName = D;
1601 LineNo = Var.getLineNumber();
1602 Unit = Var.getCompileUnit();
1603 TypeD = Var.getType();
1604 } else {
1605 const DbgDeclareInst *DDI = findDbgDeclare(V);
1606 if (!DDI) return false;
1607 DIVariable Var(cast<MDNode>(DDI->getVariable()));
1608
1609 StringRef D = Var.getName();
1610 if (!D.empty())
1611 DisplayName = D;
1612 LineNo = Var.getLineNumber();
1613 Unit = Var.getCompileUnit();
1614 TypeD = Var.getType();
Torok Edwinff7d0e92009-03-10 13:41:26 +00001615 }
Devang Patel13e16b62009-06-26 01:49:18 +00001616
Chris Lattner099b7792009-12-29 09:22:47 +00001617 StringRef T = TypeD.getName();
1618 if (!T.empty())
1619 Type = T;
1620 StringRef F = Unit.getFilename();
1621 if (!F.empty())
1622 File = F;
1623 StringRef D = Unit.getDirectory();
1624 if (!D.empty())
1625 Dir = D;
1626 return true;
1627}
Devang Patel9e529c32009-07-02 01:15:24 +00001628
Chris Lattner099b7792009-12-29 09:22:47 +00001629/// getDISubprogram - Find subprogram that is enclosing this scope.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001630DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
Chris Lattner099b7792009-12-29 09:22:47 +00001631 DIDescriptor D(Scope);
Chris Lattner099b7792009-12-29 09:22:47 +00001632 if (D.isSubprogram())
1633 return DISubprogram(Scope);
Jim Grosbache62b6902010-07-21 21:36:25 +00001634
Chris Lattner099b7792009-12-29 09:22:47 +00001635 if (D.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001636 return getDISubprogram(DILexicalBlock(Scope).getContext());
Jim Grosbache62b6902010-07-21 21:36:25 +00001637
Chris Lattner099b7792009-12-29 09:22:47 +00001638 return DISubprogram();
1639}
Devang Patel193f7202009-11-24 01:14:22 +00001640
Chris Lattner099b7792009-12-29 09:22:47 +00001641/// getDICompositeType - Find underlying composite type.
1642DICompositeType llvm::getDICompositeType(DIType T) {
Chris Lattner099b7792009-12-29 09:22:47 +00001643 if (T.isCompositeType())
Devang Patel2db49d72010-05-07 18:11:54 +00001644 return DICompositeType(T);
Jim Grosbache62b6902010-07-21 21:36:25 +00001645
Chris Lattner099b7792009-12-29 09:22:47 +00001646 if (T.isDerivedType())
Devang Patel2db49d72010-05-07 18:11:54 +00001647 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
Jim Grosbache62b6902010-07-21 21:36:25 +00001648
Chris Lattner099b7792009-12-29 09:22:47 +00001649 return DICompositeType();
Torok Edwin620f2802008-12-16 09:07:36 +00001650}