blob: 1b732a272926fd1d9ea3024eb264e757fa5efbe5 [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
312 && getFilename().empty())
Devang Patelb79b5352009-01-19 23:21:49 +0000313 return false;
314 return true;
315}
316
Devang Patel0c4720c2010-08-23 18:25:56 +0000317/// Verify - Verify that a basic type descriptor is well formed.
318bool DIBasicType::Verify() const {
319 return isBasicType();
320}
321
322/// Verify - Verify that a derived type descriptor is well formed.
323bool DIDerivedType::Verify() const {
324 return isDerivedType();
325}
326
Devang Patelb79b5352009-01-19 23:21:49 +0000327/// Verify - Verify that a composite type descriptor is well formed.
328bool DICompositeType::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000329 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000330 return false;
Devang Patel3c91b052010-03-08 20:52:55 +0000331 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000332 return false;
333
334 DICompileUnit CU = getCompileUnit();
Devang Patel3c91b052010-03-08 20:52:55 +0000335 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000336 return false;
337 return true;
338}
339
340/// Verify - Verify that a subprogram descriptor is well formed.
341bool DISubprogram::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000342 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000343 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000344
Devang Patel3c91b052010-03-08 20:52:55 +0000345 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000346 return false;
347
348 DICompileUnit CU = getCompileUnit();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000349 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000350 return false;
351
352 DICompositeType Ty = getType();
Devang Patel3c91b052010-03-08 20:52:55 +0000353 if (!Ty.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000354 return false;
355 return true;
356}
357
358/// Verify - Verify that a global variable descriptor is well formed.
359bool DIGlobalVariable::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000360 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000361 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000362
Devang Patel65dbc902009-11-25 17:36:49 +0000363 if (getDisplayName().empty())
Devang Patel84c73e92009-11-06 17:58:12 +0000364 return false;
365
Devang Patel3c91b052010-03-08 20:52:55 +0000366 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000367 return false;
368
369 DICompileUnit CU = getCompileUnit();
Devang Patel3c91b052010-03-08 20:52:55 +0000370 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000371 return false;
372
373 DIType Ty = getType();
374 if (!Ty.Verify())
375 return false;
376
Devang Patel27398962010-08-09 21:39:24 +0000377 if (!getGlobal() && !getConstant())
Devang Patelb79b5352009-01-19 23:21:49 +0000378 return false;
379
380 return true;
381}
382
383/// Verify - Verify that a variable descriptor is well formed.
384bool DIVariable::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000385 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000386 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000387
Devang Patel3c91b052010-03-08 20:52:55 +0000388 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000389 return false;
390
Devang Patel62077af2010-05-07 21:42:24 +0000391 if (!getCompileUnit().Verify())
392 return false;
393
Devang Patelb79b5352009-01-19 23:21:49 +0000394 DIType Ty = getType();
395 if (!Ty.Verify())
396 return false;
397
Devang Patelb79b5352009-01-19 23:21:49 +0000398 return true;
399}
400
Devang Patel3c91b052010-03-08 20:52:55 +0000401/// Verify - Verify that a location descriptor is well formed.
402bool DILocation::Verify() const {
403 if (!DbgNode)
404 return false;
Jim Grosbache62b6902010-07-21 21:36:25 +0000405
Devang Patel3c91b052010-03-08 20:52:55 +0000406 return DbgNode->getNumOperands() == 4;
407}
408
Devang Patel47e22652010-05-07 23:04:32 +0000409/// Verify - Verify that a namespace descriptor is well formed.
410bool DINameSpace::Verify() const {
411 if (!DbgNode)
412 return false;
413 if (getName().empty())
414 return false;
415 if (!getCompileUnit().Verify())
416 return false;
417 return true;
418}
419
Devang Patel36375ee2009-02-17 21:23:59 +0000420/// getOriginalTypeSize - If this type is derived from a base type then
421/// return base type size.
422uint64_t DIDerivedType::getOriginalTypeSize() const {
Devang Patel61ecbd12009-11-04 23:48:00 +0000423 unsigned Tag = getTag();
424 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
425 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
426 Tag == dwarf::DW_TAG_restrict_type) {
427 DIType BaseType = getTypeDerivedFrom();
Jim Grosbache62b6902010-07-21 21:36:25 +0000428 // If this type is not derived from any type then take conservative
Devang Patel5ebfa2d2009-11-06 18:24:05 +0000429 // approach.
Devang Patel3c91b052010-03-08 20:52:55 +0000430 if (!BaseType.isValid())
Devang Patel5ebfa2d2009-11-06 18:24:05 +0000431 return getSizeInBits();
Devang Patel61ecbd12009-11-04 23:48:00 +0000432 if (BaseType.isDerivedType())
Devang Patel2db49d72010-05-07 18:11:54 +0000433 return DIDerivedType(BaseType).getOriginalTypeSize();
Devang Patel61ecbd12009-11-04 23:48:00 +0000434 else
435 return BaseType.getSizeInBits();
436 }
Jim Grosbache62b6902010-07-21 21:36:25 +0000437
Devang Patel61ecbd12009-11-04 23:48:00 +0000438 return getSizeInBits();
Devang Patel36375ee2009-02-17 21:23:59 +0000439}
Devang Patelb79b5352009-01-19 23:21:49 +0000440
Jim Grosbache62b6902010-07-21 21:36:25 +0000441/// isInlinedFnArgument - Return true if this variable provides debugging
Devang Patel719f6a92010-04-29 20:40:36 +0000442/// information for an inlined function arguments.
443bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
444 assert(CurFn && "Invalid function");
445 if (!getContext().isSubprogram())
446 return false;
Jim Grosbache62b6902010-07-21 21:36:25 +0000447 // This variable is not inlined function argument if its scope
Devang Patel719f6a92010-04-29 20:40:36 +0000448 // does not describe current function.
Devang Patel2db49d72010-05-07 18:11:54 +0000449 return !(DISubprogram(getContext()).describes(CurFn));
Devang Patel719f6a92010-04-29 20:40:36 +0000450}
451
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000452/// describes - Return true if this subprogram provides debugging
453/// information for the function F.
454bool DISubprogram::describes(const Function *F) {
Chris Lattner099b7792009-12-29 09:22:47 +0000455 assert(F && "Invalid function");
Devang Patelffd33cd2010-06-16 06:42:02 +0000456 if (F == getFunction())
457 return true;
Devang Patel65dbc902009-11-25 17:36:49 +0000458 StringRef Name = getLinkageName();
459 if (Name.empty())
Devang Patel5ccdd102009-09-29 18:40:58 +0000460 Name = getName();
Devang Patel65dbc902009-11-25 17:36:49 +0000461 if (F->getName() == Name)
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000462 return true;
463 return false;
464}
465
Jim Grosbache62b6902010-07-21 21:36:25 +0000466unsigned DISubprogram::isOptimized() const {
Devang Patelccff8122010-04-30 19:38:23 +0000467 assert (DbgNode && "Invalid subprogram descriptor!");
468 if (DbgNode->getNumOperands() == 16)
469 return getUnsignedField(15);
470 return 0;
471}
472
Devang Patel65dbc902009-11-25 17:36:49 +0000473StringRef DIScope::getFilename() const {
Devang Patel77bf2952010-03-08 22:02:50 +0000474 if (!DbgNode)
475 return StringRef();
Jim Grosbache62b6902010-07-21 21:36:25 +0000476 if (isLexicalBlock())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000477 return DILexicalBlock(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000478 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000479 return DISubprogram(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000480 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000481 return DICompileUnit(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000482 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000483 return DINameSpace(DbgNode).getFilename();
Devang Patel77bf2952010-03-08 22:02:50 +0000484 if (isType())
485 return DIType(DbgNode).getFilename();
Devang Patel7aa81892010-03-08 22:27:22 +0000486 if (isFile())
487 return DIFile(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000488 assert(0 && "Invalid DIScope!");
Devang Patel65dbc902009-11-25 17:36:49 +0000489 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000490}
491
Devang Patel65dbc902009-11-25 17:36:49 +0000492StringRef DIScope::getDirectory() const {
Devang Patel77bf2952010-03-08 22:02:50 +0000493 if (!DbgNode)
494 return StringRef();
Jim Grosbache62b6902010-07-21 21:36:25 +0000495 if (isLexicalBlock())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000496 return DILexicalBlock(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000497 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000498 return DISubprogram(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000499 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000500 return DICompileUnit(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000501 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000502 return DINameSpace(DbgNode).getDirectory();
Devang Patel77bf2952010-03-08 22:02:50 +0000503 if (isType())
504 return DIType(DbgNode).getDirectory();
Devang Patel7aa81892010-03-08 22:27:22 +0000505 if (isFile())
506 return DIFile(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000507 assert(0 && "Invalid DIScope!");
Devang Patel65dbc902009-11-25 17:36:49 +0000508 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000509}
510
Chris Lattnera45664f2008-11-10 02:56:27 +0000511//===----------------------------------------------------------------------===//
Devang Patel7136a652009-07-01 22:10:23 +0000512// DIDescriptor: dump routines for all descriptors.
513//===----------------------------------------------------------------------===//
514
515
Dan Gohman50404362010-05-07 15:30:29 +0000516/// print - Print descriptor.
517void DIDescriptor::print(raw_ostream &OS) const {
518 OS << "[" << dwarf::TagString(getTag()) << "] ";
519 OS.write_hex((intptr_t) &*DbgNode) << ']';
Devang Patel7136a652009-07-01 22:10:23 +0000520}
521
Dan Gohman50404362010-05-07 15:30:29 +0000522/// print - Print compile unit.
523void DICompileUnit::print(raw_ostream &OS) const {
Devang Patel7136a652009-07-01 22:10:23 +0000524 if (getLanguage())
Dan Gohman50404362010-05-07 15:30:29 +0000525 OS << " [" << dwarf::LanguageString(getLanguage()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000526
Dan Gohmanfaa19c32010-05-10 20:07:44 +0000527 OS << " [" << getDirectory() << "/" << getFilename() << "]";
Devang Patel7136a652009-07-01 22:10:23 +0000528}
529
Dan Gohman50404362010-05-07 15:30:29 +0000530/// print - Print type.
531void DIType::print(raw_ostream &OS) const {
Devang Patel3c91b052010-03-08 20:52:55 +0000532 if (!DbgNode) return;
Devang Patel7136a652009-07-01 22:10:23 +0000533
Devang Patel65dbc902009-11-25 17:36:49 +0000534 StringRef Res = getName();
535 if (!Res.empty())
Dan Gohman50404362010-05-07 15:30:29 +0000536 OS << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000537
538 unsigned Tag = getTag();
Dan Gohman50404362010-05-07 15:30:29 +0000539 OS << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000540
541 // TODO : Print context
Dan Gohmanc014d092010-05-07 16:17:22 +0000542 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000543 OS << " ["
Dan Gohman9a7063e2010-05-07 16:39:27 +0000544 << "line " << getLineNumber() << ", "
545 << getSizeInBits() << " bits, "
546 << getAlignInBits() << " bit alignment, "
547 << getOffsetInBits() << " bit offset"
Chris Lattnera81d29b2009-08-23 07:33:14 +0000548 << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000549
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000550 if (isPrivate())
Dan Gohman50404362010-05-07 15:30:29 +0000551 OS << " [private] ";
Devang Patel7136a652009-07-01 22:10:23 +0000552 else if (isProtected())
Dan Gohman50404362010-05-07 15:30:29 +0000553 OS << " [protected] ";
Devang Patel7136a652009-07-01 22:10:23 +0000554
555 if (isForwardDecl())
Dan Gohman50404362010-05-07 15:30:29 +0000556 OS << " [fwd] ";
Devang Patel7136a652009-07-01 22:10:23 +0000557
Devang Patel6ceea332009-08-31 18:49:10 +0000558 if (isBasicType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000559 DIBasicType(DbgNode).print(OS);
Devang Patel6ceea332009-08-31 18:49:10 +0000560 else if (isDerivedType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000561 DIDerivedType(DbgNode).print(OS);
Devang Patel6ceea332009-08-31 18:49:10 +0000562 else if (isCompositeType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000563 DICompositeType(DbgNode).print(OS);
Devang Patel7136a652009-07-01 22:10:23 +0000564 else {
Dan Gohman50404362010-05-07 15:30:29 +0000565 OS << "Invalid DIType\n";
Devang Patel7136a652009-07-01 22:10:23 +0000566 return;
567 }
568
Dan Gohman50404362010-05-07 15:30:29 +0000569 OS << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000570}
571
Dan Gohman50404362010-05-07 15:30:29 +0000572/// print - Print basic type.
573void DIBasicType::print(raw_ostream &OS) const {
574 OS << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000575}
576
Dan Gohman50404362010-05-07 15:30:29 +0000577/// print - Print derived type.
578void DIDerivedType::print(raw_ostream &OS) const {
Dan Gohmanc014d092010-05-07 16:17:22 +0000579 OS << "\n\t Derived From: "; getTypeDerivedFrom().print(OS);
Devang Patel7136a652009-07-01 22:10:23 +0000580}
581
Dan Gohman50404362010-05-07 15:30:29 +0000582/// print - Print composite type.
583void DICompositeType::print(raw_ostream &OS) const {
Devang Patel7136a652009-07-01 22:10:23 +0000584 DIArray A = getTypeArray();
Dan Gohman50404362010-05-07 15:30:29 +0000585 OS << " [" << A.getNumElements() << " elements]";
Devang Patel7136a652009-07-01 22:10:23 +0000586}
587
Dan Gohman50404362010-05-07 15:30:29 +0000588/// print - Print subprogram.
589void DISubprogram::print(raw_ostream &OS) const {
Devang Patel65dbc902009-11-25 17:36:49 +0000590 StringRef Res = getName();
591 if (!Res.empty())
Dan Gohman50404362010-05-07 15:30:29 +0000592 OS << " [" << Res << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000593
594 unsigned Tag = getTag();
Dan Gohman50404362010-05-07 15:30:29 +0000595 OS << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000596
597 // TODO : Print context
Dan Gohmanc014d092010-05-07 16:17:22 +0000598 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000599 OS << " [" << getLineNumber() << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000600
601 if (isLocalToUnit())
Dan Gohman50404362010-05-07 15:30:29 +0000602 OS << " [local] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000603
604 if (isDefinition())
Dan Gohman50404362010-05-07 15:30:29 +0000605 OS << " [def] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000606
Dan Gohman50404362010-05-07 15:30:29 +0000607 OS << "\n";
608}
609
610/// print - Print global variable.
611void DIGlobalVariable::print(raw_ostream &OS) const {
612 OS << " [";
Devang Patela49d8772010-05-07 23:19:07 +0000613 StringRef Res = getName();
614 if (!Res.empty())
615 OS << " [" << Res << "] ";
616
617 unsigned Tag = getTag();
618 OS << " [" << dwarf::TagString(Tag) << "] ";
619
620 // TODO : Print context
621 getCompileUnit().print(OS);
622 OS << " [" << getLineNumber() << "] ";
623
624 if (isLocalToUnit())
625 OS << " [local] ";
626
627 if (isDefinition())
628 OS << " [def] ";
629
630 if (isGlobalVariable())
631 DIGlobalVariable(DbgNode).print(OS);
632 OS << "]\n";
Dan Gohman50404362010-05-07 15:30:29 +0000633}
634
635/// print - Print variable.
636void DIVariable::print(raw_ostream &OS) const {
637 StringRef Res = getName();
638 if (!Res.empty())
639 OS << " [" << Res << "] ";
640
Dan Gohmanc014d092010-05-07 16:17:22 +0000641 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000642 OS << " [" << getLineNumber() << "] ";
Dan Gohmanc014d092010-05-07 16:17:22 +0000643 getType().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000644 OS << "\n";
645
646 // FIXME: Dump complex addresses
647}
648
649/// dump - Print descriptor to dbgs() with a newline.
650void DIDescriptor::dump() const {
651 print(dbgs()); dbgs() << '\n';
652}
653
654/// dump - Print compile unit to dbgs() with a newline.
655void DICompileUnit::dump() const {
656 print(dbgs()); dbgs() << '\n';
657}
658
659/// dump - Print type to dbgs() with a newline.
660void DIType::dump() const {
661 print(dbgs()); dbgs() << '\n';
662}
663
664/// dump - Print basic type to dbgs() with a newline.
665void DIBasicType::dump() const {
666 print(dbgs()); dbgs() << '\n';
667}
668
669/// dump - Print derived type to dbgs() with a newline.
670void DIDerivedType::dump() const {
671 print(dbgs()); dbgs() << '\n';
672}
673
674/// dump - Print composite type to dbgs() with a newline.
675void DICompositeType::dump() const {
676 print(dbgs()); dbgs() << '\n';
677}
678
Dan Gohman50404362010-05-07 15:30:29 +0000679/// dump - Print subprogram to dbgs() with a newline.
680void DISubprogram::dump() const {
681 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000682}
683
684/// dump - Print global variable.
685void DIGlobalVariable::dump() const {
Dan Gohman50404362010-05-07 15:30:29 +0000686 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000687}
688
689/// dump - Print variable.
690void DIVariable::dump() const {
Dan Gohman50404362010-05-07 15:30:29 +0000691 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000692}
693
694//===----------------------------------------------------------------------===//
Chris Lattnera45664f2008-11-10 02:56:27 +0000695// DIFactory: Basic Helpers
696//===----------------------------------------------------------------------===//
697
Bill Wendlingdc817b62009-05-14 18:26:15 +0000698DIFactory::DIFactory(Module &m)
Victor Hernandez06203122010-01-23 00:03:28 +0000699 : M(m), VMContext(M.getContext()), DeclareFn(0), ValueFn(0) {}
Chris Lattner497a7a82008-11-10 04:10:34 +0000700
Chris Lattnera45664f2008-11-10 02:56:27 +0000701Constant *DIFactory::GetTagConstant(unsigned TAG) {
Devang Patel6906ba52009-01-20 19:22:03 +0000702 assert((TAG & LLVMDebugVersionMask) == 0 &&
Chris Lattnera45664f2008-11-10 02:56:27 +0000703 "Tag too large for debug encoding!");
Owen Anderson1d0be152009-08-13 21:58:54 +0000704 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
Chris Lattnera45664f2008-11-10 02:56:27 +0000705}
706
Chris Lattnera45664f2008-11-10 02:56:27 +0000707//===----------------------------------------------------------------------===//
708// DIFactory: Primary Constructors
709//===----------------------------------------------------------------------===//
710
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000711/// GetOrCreateArray - Create an descriptor for an array of descriptors.
Chris Lattnera45664f2008-11-10 02:56:27 +0000712/// This implicitly uniques the arrays created.
713DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
Benjamin Kramera53557e2010-09-21 16:41:29 +0000714 if (NumTys == 0) {
715 Value *Null = llvm::Constant::getNullValue(Type::getInt32Ty(VMContext));
716 return DIArray(MDNode::get(VMContext, &Null, 1));
717 }
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000718
Benjamin Kramera53557e2010-09-21 16:41:29 +0000719 SmallVector<Value *, 16> Elts(Tys, Tys+NumTys);
720 return DIArray(MDNode::get(VMContext, Elts.data(), Elts.size()));
Chris Lattnera45664f2008-11-10 02:56:27 +0000721}
722
723/// GetOrCreateSubrange - Create a descriptor for a value range. This
724/// implicitly uniques the values returned.
725DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
Devang Patele4b27562009-08-28 23:24:31 +0000726 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000727 GetTagConstant(dwarf::DW_TAG_subrange_type),
Owen Anderson1d0be152009-08-13 21:58:54 +0000728 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
729 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
Chris Lattnera45664f2008-11-10 02:56:27 +0000730 };
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000731
Devang Patele4b27562009-08-28 23:24:31 +0000732 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000733}
734
Devang Pateld6747df2010-10-06 20:50:40 +0000735/// CreateUnspecifiedParameter - Create unspeicified type descriptor
736/// for the subroutine type.
737DIDescriptor DIFactory::CreateUnspecifiedParameter() {
738 Value *Elts[] = {
739 GetTagConstant(dwarf::DW_TAG_unspecified_parameters)
740 };
741 return DIDescriptor(MDNode::get(VMContext, &Elts[0], 1));
742}
Chris Lattnera45664f2008-11-10 02:56:27 +0000743
744/// CreateCompileUnit - Create a new descriptor for the specified compile
745/// unit. Note that this does not unique compile units within the module.
746DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
Devang Patel65dbc902009-11-25 17:36:49 +0000747 StringRef Filename,
748 StringRef Directory,
749 StringRef Producer,
Devang Pateldd9db662009-01-30 18:20:31 +0000750 bool isMain,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000751 bool isOptimized,
Devang Patel65dbc902009-11-25 17:36:49 +0000752 StringRef Flags,
Devang Patel13319ce2009-02-17 22:43:44 +0000753 unsigned RunTimeVer) {
Devang Patele4b27562009-08-28 23:24:31 +0000754 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000755 GetTagConstant(dwarf::DW_TAG_compile_unit),
Devang Patele4b27562009-08-28 23:24:31 +0000756 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Owen Anderson1d0be152009-08-13 21:58:54 +0000757 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
Devang Patele4b27562009-08-28 23:24:31 +0000758 MDString::get(VMContext, Filename),
759 MDString::get(VMContext, Directory),
760 MDString::get(VMContext, Producer),
Owen Anderson1d0be152009-08-13 21:58:54 +0000761 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
762 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
Devang Patele4b27562009-08-28 23:24:31 +0000763 MDString::get(VMContext, Flags),
Owen Anderson1d0be152009-08-13 21:58:54 +0000764 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
Chris Lattnera45664f2008-11-10 02:56:27 +0000765 };
Devang Patele4b27562009-08-28 23:24:31 +0000766
767 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000768}
769
Devang Patel7aa81892010-03-08 22:27:22 +0000770/// CreateFile - Create a new descriptor for the specified file.
771DIFile DIFactory::CreateFile(StringRef Filename,
772 StringRef Directory,
773 DICompileUnit CU) {
774 Value *Elts[] = {
775 GetTagConstant(dwarf::DW_TAG_file_type),
776 MDString::get(VMContext, Filename),
777 MDString::get(VMContext, Directory),
Devang Patel2db49d72010-05-07 18:11:54 +0000778 CU
Devang Patel7aa81892010-03-08 22:27:22 +0000779 };
780
781 return DIFile(MDNode::get(VMContext, &Elts[0], 4));
782}
783
Chris Lattnera45664f2008-11-10 02:56:27 +0000784/// CreateEnumerator - Create a single enumerator value.
Devang Patel65dbc902009-11-25 17:36:49 +0000785DIEnumerator DIFactory::CreateEnumerator(StringRef Name, uint64_t Val){
Devang Patele4b27562009-08-28 23:24:31 +0000786 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000787 GetTagConstant(dwarf::DW_TAG_enumerator),
Devang Patele4b27562009-08-28 23:24:31 +0000788 MDString::get(VMContext, Name),
Owen Anderson1d0be152009-08-13 21:58:54 +0000789 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
Chris Lattnera45664f2008-11-10 02:56:27 +0000790 };
Devang Patele4b27562009-08-28 23:24:31 +0000791 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000792}
793
794
795/// CreateBasicType - Create a basic type like int, float, etc.
796DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000797 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000798 DIFile F,
Chris Lattnera45664f2008-11-10 02:56:27 +0000799 unsigned LineNumber,
800 uint64_t SizeInBits,
801 uint64_t AlignInBits,
802 uint64_t OffsetInBits, unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000803 unsigned Encoding) {
Devang Patele4b27562009-08-28 23:24:31 +0000804 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000805 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patel2db49d72010-05-07 18:11:54 +0000806 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000807 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000808 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000809 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
810 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
811 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
812 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
813 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
814 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
Chris Lattnera45664f2008-11-10 02:56:27 +0000815 };
Devang Patele4b27562009-08-28 23:24:31 +0000816 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000817}
818
Devang Patelac16d442009-10-26 16:54:35 +0000819
820/// CreateBasicType - Create a basic type like int, float, etc.
821DIBasicType DIFactory::CreateBasicTypeEx(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000822 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000823 DIFile F,
Devang Patelac16d442009-10-26 16:54:35 +0000824 unsigned LineNumber,
825 Constant *SizeInBits,
826 Constant *AlignInBits,
827 Constant *OffsetInBits, unsigned Flags,
828 unsigned Encoding) {
829 Value *Elts[] = {
830 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patel2db49d72010-05-07 18:11:54 +0000831 Context,
Devang Patelac16d442009-10-26 16:54:35 +0000832 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000833 F,
Devang Patelac16d442009-10-26 16:54:35 +0000834 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
835 SizeInBits,
836 AlignInBits,
837 OffsetInBits,
838 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
839 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
840 };
841 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
842}
843
Devang Patelb4645642010-02-06 01:02:37 +0000844/// CreateArtificialType - Create a new DIType with "artificial" flag set.
845DIType DIFactory::CreateArtificialType(DIType Ty) {
846 if (Ty.isArtificial())
847 return Ty;
848
849 SmallVector<Value *, 9> Elts;
Devang Patel2db49d72010-05-07 18:11:54 +0000850 MDNode *N = Ty;
Devang Patelb4645642010-02-06 01:02:37 +0000851 assert (N && "Unexpected input DIType!");
852 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
853 if (Value *V = N->getOperand(i))
854 Elts.push_back(V);
855 else
856 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
857 }
858
859 unsigned CurFlags = Ty.getFlags();
860 CurFlags = CurFlags | DIType::FlagArtificial;
861
862 // Flags are stored at this slot.
863 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
864
865 return DIType(MDNode::get(VMContext, Elts.data(), Elts.size()));
866}
Devang Patelac16d442009-10-26 16:54:35 +0000867
Chris Lattnera45664f2008-11-10 02:56:27 +0000868/// CreateDerivedType - Create a derived type like const qualified type,
869/// pointer, typedef, etc.
870DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
871 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000872 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000873 DIFile F,
Chris Lattnera45664f2008-11-10 02:56:27 +0000874 unsigned LineNumber,
875 uint64_t SizeInBits,
876 uint64_t AlignInBits,
877 uint64_t OffsetInBits,
878 unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000879 DIType DerivedFrom) {
Devang Patele4b27562009-08-28 23:24:31 +0000880 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000881 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000882 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000883 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000884 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000885 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
886 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
887 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
888 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
889 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000890 DerivedFrom,
Chris Lattnera45664f2008-11-10 02:56:27 +0000891 };
Devang Patele4b27562009-08-28 23:24:31 +0000892 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000893}
894
Devang Patelac16d442009-10-26 16:54:35 +0000895
896/// CreateDerivedType - Create a derived type like const qualified type,
897/// pointer, typedef, etc.
898DIDerivedType DIFactory::CreateDerivedTypeEx(unsigned Tag,
899 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000900 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000901 DIFile F,
Devang Patelac16d442009-10-26 16:54:35 +0000902 unsigned LineNumber,
903 Constant *SizeInBits,
904 Constant *AlignInBits,
905 Constant *OffsetInBits,
906 unsigned Flags,
907 DIType DerivedFrom) {
908 Value *Elts[] = {
909 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000910 Context,
Devang Patelac16d442009-10-26 16:54:35 +0000911 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000912 F,
Devang Patelac16d442009-10-26 16:54:35 +0000913 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
914 SizeInBits,
915 AlignInBits,
916 OffsetInBits,
917 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000918 DerivedFrom,
Devang Patelac16d442009-10-26 16:54:35 +0000919 };
920 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
921}
922
923
Chris Lattnera45664f2008-11-10 02:56:27 +0000924/// CreateCompositeType - Create a composite type like array, struct, etc.
925DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
926 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000927 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000928 DIFile F,
Chris Lattnera45664f2008-11-10 02:56:27 +0000929 unsigned LineNumber,
930 uint64_t SizeInBits,
931 uint64_t AlignInBits,
932 uint64_t OffsetInBits,
933 unsigned Flags,
934 DIType DerivedFrom,
Devang Patel13319ce2009-02-17 22:43:44 +0000935 DIArray Elements,
Devang Patel0fd7f9d2010-01-26 21:14:59 +0000936 unsigned RuntimeLang,
937 MDNode *ContainingType) {
Owen Andersone277fed2009-07-07 16:31:25 +0000938
Devang Patele4b27562009-08-28 23:24:31 +0000939 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000940 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000941 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000942 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000943 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000944 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
945 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
946 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
947 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
948 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000949 DerivedFrom,
950 Elements,
Devang Patel0fd7f9d2010-01-26 21:14:59 +0000951 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
952 ContainingType
Chris Lattnera45664f2008-11-10 02:56:27 +0000953 };
Devang Patele7e5a0f2010-08-10 20:01:20 +0000954
955 MDNode *Node = MDNode::get(VMContext, &Elts[0], 13);
956 // Create a named metadata so that we do not lose this enum info.
957 if (Tag == dwarf::DW_TAG_enumeration_type) {
958 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.enum");
959 NMD->addOperand(Node);
960 }
961 return DICompositeType(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +0000962}
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
Devang Patelfe58f952010-12-07 23:25:47 +0000975/// CreateTemporaryType - Create a temporary forward-declared type.
976DIType DIFactory::CreateTemporaryType(DIFile F) {
977 // Give the temporary MDNode a tag. It doesn't matter what tag we
978 // use here as long as DIType accepts it.
979 Value *Elts[] = {
980 GetTagConstant(DW_TAG_base_type),
981 F.getCompileUnit(),
982 NULL,
983 F
984 };
985 MDNode *Node = MDNode::getTemporary(VMContext, Elts, array_lengthof(Elts));
986 return DIType(Node);
987}
Dan Gohman489b29b2010-08-20 22:02:26 +0000988
Devang Patelac16d442009-10-26 16:54:35 +0000989/// CreateCompositeType - Create a composite type like array, struct, etc.
990DICompositeType DIFactory::CreateCompositeTypeEx(unsigned Tag,
991 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000992 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000993 DIFile F,
Devang Patelac16d442009-10-26 16:54:35 +0000994 unsigned LineNumber,
995 Constant *SizeInBits,
996 Constant *AlignInBits,
997 Constant *OffsetInBits,
998 unsigned Flags,
999 DIType DerivedFrom,
1000 DIArray Elements,
Devang Patel6bf058c2010-08-10 20:22:49 +00001001 unsigned RuntimeLang,
1002 MDNode *ContainingType) {
Devang Patelac16d442009-10-26 16:54:35 +00001003 Value *Elts[] = {
1004 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +00001005 Context,
Devang Patelac16d442009-10-26 16:54:35 +00001006 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +00001007 F,
Devang Patelac16d442009-10-26 16:54:35 +00001008 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
1009 SizeInBits,
1010 AlignInBits,
1011 OffsetInBits,
1012 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +00001013 DerivedFrom,
1014 Elements,
Devang Patel6bf058c2010-08-10 20:22:49 +00001015 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
1016 ContainingType
Devang Patelac16d442009-10-26 16:54:35 +00001017 };
Devang Patel6bf058c2010-08-10 20:22:49 +00001018 MDNode *Node = MDNode::get(VMContext, &Elts[0], 13);
Devang Patele7e5a0f2010-08-10 20:01:20 +00001019 // Create a named metadata so that we do not lose this enum info.
1020 if (Tag == dwarf::DW_TAG_enumeration_type) {
1021 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.enum");
1022 NMD->addOperand(Node);
1023 }
1024 return DICompositeType(Node);
Devang Patelac16d442009-10-26 16:54:35 +00001025}
1026
1027
Chris Lattnera45664f2008-11-10 02:56:27 +00001028/// CreateSubprogram - Create a new descriptor for the specified subprogram.
1029/// See comments in DISubprogram for descriptions of these fields. This
1030/// method does not unique the generated descriptors.
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001031DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +00001032 StringRef Name,
1033 StringRef DisplayName,
1034 StringRef LinkageName,
Devang Patel4b945502010-03-09 00:44:10 +00001035 DIFile F,
Devang Patel2e369932010-01-23 00:26:28 +00001036 unsigned LineNo, DIType Ty,
Chris Lattnera45664f2008-11-10 02:56:27 +00001037 bool isLocalToUnit,
Devang Patel5d11eb02009-12-03 19:11:07 +00001038 bool isDefinition,
1039 unsigned VK, unsigned VIndex,
Devang Patel4e0d19d2010-02-03 19:57:19 +00001040 DIType ContainingType,
Devang Patel9dd2b472010-09-29 21:04:46 +00001041 unsigned Flags,
Stuart Hastings215aa152010-06-11 20:08:44 +00001042 bool isOptimized,
1043 Function *Fn) {
Devang Patel854967e2008-12-17 22:39:29 +00001044
Devang Patele4b27562009-08-28 23:24:31 +00001045 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001046 GetTagConstant(dwarf::DW_TAG_subprogram),
Devang Patele4b27562009-08-28 23:24:31 +00001047 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Devang Patel2db49d72010-05-07 18:11:54 +00001048 Context,
Devang Patele4b27562009-08-28 23:24:31 +00001049 MDString::get(VMContext, Name),
1050 MDString::get(VMContext, DisplayName),
1051 MDString::get(VMContext, LinkageName),
Devang Patel2db49d72010-05-07 18:11:54 +00001052 F,
Owen Anderson1d0be152009-08-13 21:58:54 +00001053 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001054 Ty,
Owen Anderson1d0be152009-08-13 21:58:54 +00001055 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
Devang Patel5d11eb02009-12-03 19:11:07 +00001056 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1057 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
1058 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
Devang Patel2db49d72010-05-07 18:11:54 +00001059 ContainingType,
Devang Patel9dd2b472010-09-29 21:04:46 +00001060 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Stuart Hastings215aa152010-06-11 20:08:44 +00001061 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
1062 Fn
Chris Lattnera45664f2008-11-10 02:56:27 +00001063 };
Devang Patelfd5fdc32010-06-28 05:53:08 +00001064 MDNode *Node = MDNode::get(VMContext, &Elts[0], 17);
1065
1066 // Create a named metadata so that we do not lose this mdnode.
1067 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
1068 NMD->addOperand(Node);
1069 return DISubprogram(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +00001070}
1071
Devang Patele3a18de2009-12-01 23:09:02 +00001072/// CreateSubprogramDefinition - Create new subprogram descriptor for the
Jim Grosbache62b6902010-07-21 21:36:25 +00001073/// given declaration.
1074DISubprogram DIFactory::CreateSubprogramDefinition(DISubprogram &SPDeclaration){
Devang Patele3a18de2009-12-01 23:09:02 +00001075 if (SPDeclaration.isDefinition())
Devang Patel2db49d72010-05-07 18:11:54 +00001076 return DISubprogram(SPDeclaration);
Devang Patele3a18de2009-12-01 23:09:02 +00001077
Devang Patel2db49d72010-05-07 18:11:54 +00001078 MDNode *DeclNode = SPDeclaration;
Devang Patele3a18de2009-12-01 23:09:02 +00001079 Value *Elts[] = {
1080 GetTagConstant(dwarf::DW_TAG_subprogram),
1081 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001082 DeclNode->getOperand(2), // Context
1083 DeclNode->getOperand(3), // Name
1084 DeclNode->getOperand(4), // DisplayName
1085 DeclNode->getOperand(5), // LinkageName
1086 DeclNode->getOperand(6), // CompileUnit
1087 DeclNode->getOperand(7), // LineNo
1088 DeclNode->getOperand(8), // Type
1089 DeclNode->getOperand(9), // isLocalToUnit
Stuart Hastings6d56b9f2010-06-05 00:39:29 +00001090 ConstantInt::get(Type::getInt1Ty(VMContext), true),
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001091 DeclNode->getOperand(11), // Virtuality
1092 DeclNode->getOperand(12), // VIndex
Devang Patel4e0d19d2010-02-03 19:57:19 +00001093 DeclNode->getOperand(13), // Containting Type
Devang Patel9dd2b472010-09-29 21:04:46 +00001094 DeclNode->getOperand(14), // Flags
Devang Patelacc6efa2010-06-27 21:04:31 +00001095 DeclNode->getOperand(15), // isOptimized
1096 SPDeclaration.getFunction()
Devang Patele3a18de2009-12-01 23:09:02 +00001097 };
Devang Patelfd5fdc32010-06-28 05:53:08 +00001098 MDNode *Node =MDNode::get(VMContext, &Elts[0], 16);
1099
1100 // Create a named metadata so that we do not lose this mdnode.
1101 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
1102 NMD->addOperand(Node);
1103 return DISubprogram(Node);
Devang Patele3a18de2009-12-01 23:09:02 +00001104}
1105
Chris Lattnera45664f2008-11-10 02:56:27 +00001106/// CreateGlobalVariable - Create a new descriptor for the specified global.
1107DIGlobalVariable
Devang Patel65dbc902009-11-25 17:36:49 +00001108DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
1109 StringRef DisplayName,
1110 StringRef LinkageName,
Devang Patel4b945502010-03-09 00:44:10 +00001111 DIFile F,
Devang Patel2e369932010-01-23 00:26:28 +00001112 unsigned LineNo, DIType Ty,bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +00001113 bool isDefinition, llvm::GlobalVariable *Val) {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001114 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001115 GetTagConstant(dwarf::DW_TAG_variable),
Devang Patele4b27562009-08-28 23:24:31 +00001116 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Devang Patel2db49d72010-05-07 18:11:54 +00001117 Context,
Devang Patele4b27562009-08-28 23:24:31 +00001118 MDString::get(VMContext, Name),
1119 MDString::get(VMContext, DisplayName),
1120 MDString::get(VMContext, LinkageName),
Devang Patel2db49d72010-05-07 18:11:54 +00001121 F,
Owen Anderson1d0be152009-08-13 21:58:54 +00001122 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001123 Ty,
Owen Anderson1d0be152009-08-13 21:58:54 +00001124 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1125 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
Devang Patele4b27562009-08-28 23:24:31 +00001126 Val
Chris Lattnera45664f2008-11-10 02:56:27 +00001127 };
Devang Patele4b27562009-08-28 23:24:31 +00001128
1129 Value *const *Vs = &Elts[0];
1130 MDNode *Node = MDNode::get(VMContext,Vs, 12);
1131
1132 // Create a named metadata so that we do not lose this mdnode.
1133 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001134 NMD->addOperand(Node);
Devang Patele4b27562009-08-28 23:24:31 +00001135
1136 return DIGlobalVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +00001137}
1138
Devang Patel27398962010-08-09 21:39:24 +00001139/// CreateGlobalVariable - Create a new descriptor for the specified constant.
1140DIGlobalVariable
1141DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
1142 StringRef DisplayName,
1143 StringRef LinkageName,
1144 DIFile F,
1145 unsigned LineNo, DIType Ty,bool isLocalToUnit,
1146 bool isDefinition, llvm::Constant *Val) {
1147 Value *Elts[] = {
Devang Patelebd53742010-08-11 23:17:54 +00001148 GetTagConstant(dwarf::DW_TAG_variable),
Devang Patel27398962010-08-09 21:39:24 +00001149 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
1150 Context,
1151 MDString::get(VMContext, Name),
1152 MDString::get(VMContext, DisplayName),
1153 MDString::get(VMContext, LinkageName),
1154 F,
1155 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1156 Ty,
1157 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1158 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1159 Val
1160 };
1161
1162 Value *const *Vs = &Elts[0];
1163 MDNode *Node = MDNode::get(VMContext,Vs, 12);
1164
1165 // Create a named metadata so that we do not lose this mdnode.
1166 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
1167 NMD->addOperand(Node);
1168
1169 return DIGlobalVariable(Node);
1170}
Chris Lattnera45664f2008-11-10 02:56:27 +00001171
Devang Patel62367042010-11-10 22:19:21 +00001172/// fixupObjcLikeName - Replace contains special characters used
1173/// in a typical Objective-C names with '.' in a given string.
1174static void fixupObjcLikeName(std::string &Str) {
1175 for (size_t i = 0, e = Str.size(); i < e; ++i) {
1176 char C = Str[i];
Jakob Stoklund Olesen5b3f7792010-12-03 23:40:45 +00001177 if (C == '[' || C == ']' || C == ' ' || C == ':' || C == '+' ||
1178 C == '(' || C == ')')
Devang Patel62367042010-11-10 22:19:21 +00001179 Str[i] = '.';
1180 }
1181}
1182
1183/// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
1184/// to hold function specific information.
1185NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, StringRef FuncName) {
1186 SmallString<32> Out;
1187 if (FuncName.find('[') == StringRef::npos)
1188 return M.getOrInsertNamedMetadata(Twine("llvm.dbg.lv.", FuncName)
1189 .toStringRef(Out));
1190 std::string Name = FuncName;
1191 fixupObjcLikeName(Name);
1192 return M.getOrInsertNamedMetadata(Twine("llvm.dbg.lv.", Name)
1193 .toStringRef(Out));
1194}
1195
1196/// getFnSpecificMDNode - Return a NameMDNode, if available, that is
1197/// suitable to hold function specific information.
1198NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, StringRef FuncName) {
1199 if (FuncName.find('[') == StringRef::npos)
1200 return M.getNamedMetadata(Twine("llvm.dbg.lv.", FuncName));
1201 std::string Name = FuncName;
1202 fixupObjcLikeName(Name);
1203 return M.getNamedMetadata(Twine("llvm.dbg.lv.", Name));
1204}
1205
Chris Lattnera45664f2008-11-10 02:56:27 +00001206/// CreateVariable - Create a new descriptor for the specified variable.
1207DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +00001208 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +00001209 DIFile F,
1210 unsigned LineNo,
Devang Patel3cf763d2010-09-29 23:07:21 +00001211 DIType Ty, bool AlwaysPreserve,
1212 unsigned Flags) {
Devang Patele4b27562009-08-28 23:24:31 +00001213 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001214 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +00001215 Context,
Devang Patele4b27562009-08-28 23:24:31 +00001216 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +00001217 F,
Owen Anderson1d0be152009-08-13 21:58:54 +00001218 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001219 Ty,
Devang Patel3cf763d2010-09-29 23:07:21 +00001220 ConstantInt::get(Type::getInt32Ty(VMContext), Flags)
Chris Lattnera45664f2008-11-10 02:56:27 +00001221 };
Devang Patel3cf763d2010-09-29 23:07:21 +00001222 MDNode *Node = MDNode::get(VMContext, &Elts[0], 7);
Devang Patel6ed0ce32010-05-20 20:35:24 +00001223 if (AlwaysPreserve) {
1224 // The optimizer may remove local variable. If there is an interest
1225 // to preserve variable info in such situation then stash it in a
1226 // named mdnode.
Devang Patel2f7d5292010-06-16 00:53:55 +00001227 DISubprogram Fn(getDISubprogram(Context));
Devang Patel10de3bb2010-06-21 18:36:58 +00001228 StringRef FName = "fn";
1229 if (Fn.getFunction())
1230 FName = Fn.getFunction()->getName();
Devang Patel10de3bb2010-06-21 18:36:58 +00001231 char One = '\1';
1232 if (FName.startswith(StringRef(&One, 1)))
1233 FName = FName.substr(1);
Dan Gohman17aa92c2010-07-21 23:38:33 +00001234
Devang Patel62367042010-11-10 22:19:21 +00001235
1236 NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, FName);
Devang Patel2f7d5292010-06-16 00:53:55 +00001237 FnLocals->addOperand(Node);
Devang Patel98e1cac2010-05-14 21:01:35 +00001238 }
1239 return DIVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +00001240}
1241
1242
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001243/// CreateComplexVariable - Create a new descriptor for the specified variable
1244/// which has a complex address expression for its address.
1245DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
Benjamin Kramer28b4afc2010-09-21 16:00:03 +00001246 StringRef Name, DIFile F,
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001247 unsigned LineNo,
Benjamin Kramer28b4afc2010-09-21 16:00:03 +00001248 DIType Ty, Value *const *Addr,
1249 unsigned NumAddr) {
1250 SmallVector<Value *, 15> Elts;
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001251 Elts.push_back(GetTagConstant(Tag));
Devang Patel2db49d72010-05-07 18:11:54 +00001252 Elts.push_back(Context);
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001253 Elts.push_back(MDString::get(VMContext, Name));
Devang Patel2db49d72010-05-07 18:11:54 +00001254 Elts.push_back(F);
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001255 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
Devang Patel2db49d72010-05-07 18:11:54 +00001256 Elts.push_back(Ty);
Benjamin Kramer28b4afc2010-09-21 16:00:03 +00001257 Elts.append(Addr, Addr+NumAddr);
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001258
Benjamin Kramer28b4afc2010-09-21 16:00:03 +00001259 return DIVariable(MDNode::get(VMContext, Elts.data(), Elts.size()));
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001260}
1261
1262
Chris Lattnera45664f2008-11-10 02:56:27 +00001263/// CreateBlock - This creates a descriptor for a lexical block with the
Owen Anderson99035272009-07-07 17:12:53 +00001264/// specified parent VMContext.
Devang Patel3d821aa2010-02-16 21:39:34 +00001265DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context,
Stuart Hastings0db42712010-07-19 23:56:30 +00001266 DIFile F, unsigned LineNo,
1267 unsigned Col) {
1268 // Defeat MDNode uniqing for lexical blocks.
1269 static unsigned int unique_id = 0;
Devang Patele4b27562009-08-28 23:24:31 +00001270 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001271 GetTagConstant(dwarf::DW_TAG_lexical_block),
Devang Patel2db49d72010-05-07 18:11:54 +00001272 Context,
Devang Patel3d821aa2010-02-16 21:39:34 +00001273 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Stuart Hastings0db42712010-07-19 23:56:30 +00001274 ConstantInt::get(Type::getInt32Ty(VMContext), Col),
1275 F,
1276 ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
Chris Lattnera45664f2008-11-10 02:56:27 +00001277 };
Stuart Hastings0db42712010-07-19 23:56:30 +00001278 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 6));
Chris Lattnera45664f2008-11-10 02:56:27 +00001279}
1280
Devang Patel6404e4e2009-12-15 19:16:48 +00001281/// CreateNameSpace - This creates new descriptor for a namespace
1282/// with the specified parent context.
1283DINameSpace DIFactory::CreateNameSpace(DIDescriptor Context, StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +00001284 DIFile F,
Devang Patel6404e4e2009-12-15 19:16:48 +00001285 unsigned LineNo) {
1286 Value *Elts[] = {
1287 GetTagConstant(dwarf::DW_TAG_namespace),
Devang Patel2db49d72010-05-07 18:11:54 +00001288 Context,
Devang Patel6404e4e2009-12-15 19:16:48 +00001289 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +00001290 F,
Devang Patel6404e4e2009-12-15 19:16:48 +00001291 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1292 };
1293 return DINameSpace(MDNode::get(VMContext, &Elts[0], 5));
1294}
1295
Devang Patelf98d8fe2009-09-01 01:14:15 +00001296/// CreateLocation - Creates a debug info location.
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001297DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
Daniel Dunbara279bc32009-09-20 02:20:51 +00001298 DIScope S, DILocation OrigLoc) {
Devang Patelf98d8fe2009-09-01 01:14:15 +00001299 Value *Elts[] = {
1300 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1301 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001302 S,
1303 OrigLoc,
Devang Patelf98d8fe2009-09-01 01:14:15 +00001304 };
1305 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1306}
1307
Chris Lattnera45664f2008-11-10 02:56:27 +00001308//===----------------------------------------------------------------------===//
1309// DIFactory: Routines for inserting code into a function
1310//===----------------------------------------------------------------------===//
1311
Chris Lattnera45664f2008-11-10 02:56:27 +00001312/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Devang Patel6daf99b2009-11-10 22:05:35 +00001313Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001314 Instruction *InsertBefore) {
Victor Hernandez4cf292a2010-01-26 02:07:38 +00001315 assert(Storage && "no storage passed to dbg.declare");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001316 assert(D.Verify() && "empty DIVariable passed to dbg.declare");
Chris Lattnera45664f2008-11-10 02:56:27 +00001317 if (!DeclareFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +00001318 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1319
Victor Hernandez756462b2010-01-18 20:42:09 +00001320 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
Devang Patel2db49d72010-05-07 18:11:54 +00001321 D };
Devang Patel6daf99b2009-11-10 22:05:35 +00001322 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
Mike Stumpe4250392009-10-01 22:08:58 +00001323}
1324
1325/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Devang Patel6daf99b2009-11-10 22:05:35 +00001326Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001327 BasicBlock *InsertAtEnd) {
Victor Hernandez4cf292a2010-01-26 02:07:38 +00001328 assert(Storage && "no storage passed to dbg.declare");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001329 assert(D.Verify() && "invalid DIVariable passed to dbg.declare");
Mike Stumpe4250392009-10-01 22:08:58 +00001330 if (!DeclareFn)
1331 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1332
Victor Hernandez756462b2010-01-18 20:42:09 +00001333 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
Devang Patel2db49d72010-05-07 18:11:54 +00001334 D };
Devang Patel27a53de2010-01-29 18:30:57 +00001335
1336 // If this block already has a terminator then insert this intrinsic
1337 // before the terminator.
Jim Grosbache62b6902010-07-21 21:36:25 +00001338 if (TerminatorInst *T = InsertAtEnd->getTerminator())
Devang Patel27a53de2010-01-29 18:30:57 +00001339 return CallInst::Create(DeclareFn, Args, Args+2, "", T);
1340 else
1341 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);}
Torok Edwin620f2802008-12-16 09:07:36 +00001342
Victor Hernandezc59b3352009-12-07 21:54:43 +00001343/// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001344Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
Victor Hernandezc59b3352009-12-07 21:54:43 +00001345 DIVariable D,
1346 Instruction *InsertBefore) {
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001347 assert(V && "no value passed to dbg.value");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001348 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001349 if (!ValueFn)
1350 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1351
Victor Hernandezc8b7cd02010-01-20 05:44:11 +00001352 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001353 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
Devang Patel2db49d72010-05-07 18:11:54 +00001354 D };
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001355 return CallInst::Create(ValueFn, Args, Args+3, "", InsertBefore);
1356}
1357
Victor Hernandezc59b3352009-12-07 21:54:43 +00001358/// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001359Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
Victor Hernandezc59b3352009-12-07 21:54:43 +00001360 DIVariable D,
1361 BasicBlock *InsertAtEnd) {
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001362 assert(V && "no value passed to dbg.value");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001363 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001364 if (!ValueFn)
1365 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1366
Jim Grosbache62b6902010-07-21 21:36:25 +00001367 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001368 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
Devang Patel2db49d72010-05-07 18:11:54 +00001369 D };
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001370 return CallInst::Create(ValueFn, Args, Args+3, "", InsertAtEnd);
1371}
Devang Patele4b27562009-08-28 23:24:31 +00001372
Devang Patel1a7ca032010-09-28 18:08:20 +00001373// RecordType - Record DIType in a module such that it is not lost even if
1374// it is not referenced through debug info anchors.
1375void DIFactory::RecordType(DIType T) {
1376 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.ty");
1377 NMD->addOperand(T);
1378}
1379
1380
Devang Pateld2f79a12009-07-28 19:55:13 +00001381//===----------------------------------------------------------------------===//
Devang Patel98c65172009-07-30 18:25:15 +00001382// DebugInfoFinder implementations.
Devang Pateld2f79a12009-07-28 19:55:13 +00001383//===----------------------------------------------------------------------===//
1384
Devang Patel98c65172009-07-30 18:25:15 +00001385/// processModule - Process entire module and collect debug info.
1386void DebugInfoFinder::processModule(Module &M) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001387 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1388 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
1389 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
1390 ++BI) {
Devang Patel01c5ff62010-05-04 01:05:02 +00001391 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
Devang Patelb4d31302009-07-31 18:18:52 +00001392 processDeclare(DDI);
Jim Grosbache62b6902010-07-21 21:36:25 +00001393
Chris Lattner28a9bf62010-04-02 20:44:29 +00001394 DebugLoc Loc = BI->getDebugLoc();
1395 if (Loc.isUnknown())
1396 continue;
Jim Grosbache62b6902010-07-21 21:36:25 +00001397
Chris Lattner28a9bf62010-04-02 20:44:29 +00001398 LLVMContext &Ctx = BI->getContext();
1399 DIDescriptor Scope(Loc.getScope(Ctx));
Jim Grosbache62b6902010-07-21 21:36:25 +00001400
Chris Lattner28a9bf62010-04-02 20:44:29 +00001401 if (Scope.isCompileUnit())
Devang Patel2db49d72010-05-07 18:11:54 +00001402 addCompileUnit(DICompileUnit(Scope));
Chris Lattner28a9bf62010-04-02 20:44:29 +00001403 else if (Scope.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001404 processSubprogram(DISubprogram(Scope));
Chris Lattner28a9bf62010-04-02 20:44:29 +00001405 else if (Scope.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001406 processLexicalBlock(DILexicalBlock(Scope));
Jim Grosbache62b6902010-07-21 21:36:25 +00001407
Chris Lattner28a9bf62010-04-02 20:44:29 +00001408 if (MDNode *IA = Loc.getInlinedAt(Ctx))
1409 processLocation(DILocation(IA));
Devang Pateld2f79a12009-07-28 19:55:13 +00001410 }
Devang Patele4b27562009-08-28 23:24:31 +00001411
Devang Patelfd5fdc32010-06-28 05:53:08 +00001412 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
1413 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1414 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
1415 if (addGlobalVariable(DIG)) {
1416 addCompileUnit(DIG.getCompileUnit());
1417 processType(DIG.getType());
1418 }
Devang Pateld2f79a12009-07-28 19:55:13 +00001419 }
1420 }
Devang Patelfd5fdc32010-06-28 05:53:08 +00001421
1422 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
1423 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
1424 processSubprogram(DISubprogram(NMD->getOperand(i)));
Devang Pateld2f79a12009-07-28 19:55:13 +00001425}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001426
Devang Patel6daf99b2009-11-10 22:05:35 +00001427/// processLocation - Process DILocation.
1428void DebugInfoFinder::processLocation(DILocation Loc) {
Devang Patel3c91b052010-03-08 20:52:55 +00001429 if (!Loc.Verify()) return;
Devang Patel2db49d72010-05-07 18:11:54 +00001430 DIDescriptor S(Loc.getScope());
Devang Patel6daf99b2009-11-10 22:05:35 +00001431 if (S.isCompileUnit())
Devang Patel2db49d72010-05-07 18:11:54 +00001432 addCompileUnit(DICompileUnit(S));
Devang Patel6daf99b2009-11-10 22:05:35 +00001433 else if (S.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001434 processSubprogram(DISubprogram(S));
Devang Patel6daf99b2009-11-10 22:05:35 +00001435 else if (S.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001436 processLexicalBlock(DILexicalBlock(S));
Devang Patel6daf99b2009-11-10 22:05:35 +00001437 processLocation(Loc.getOrigLocation());
1438}
1439
Devang Patel98c65172009-07-30 18:25:15 +00001440/// processType - Process DIType.
1441void DebugInfoFinder::processType(DIType DT) {
Devang Patel72bcdb62009-08-10 22:09:58 +00001442 if (!addType(DT))
Devang Pateld2f79a12009-07-28 19:55:13 +00001443 return;
1444
1445 addCompileUnit(DT.getCompileUnit());
Devang Patel6ceea332009-08-31 18:49:10 +00001446 if (DT.isCompositeType()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001447 DICompositeType DCT(DT);
Devang Patel98c65172009-07-30 18:25:15 +00001448 processType(DCT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001449 DIArray DA = DCT.getTypeArray();
Devang Patel3c91b052010-03-08 20:52:55 +00001450 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1451 DIDescriptor D = DA.getElement(i);
1452 if (D.isType())
Devang Patel2db49d72010-05-07 18:11:54 +00001453 processType(DIType(D));
Devang Patel3c91b052010-03-08 20:52:55 +00001454 else if (D.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001455 processSubprogram(DISubprogram(D));
Devang Patel3c91b052010-03-08 20:52:55 +00001456 }
Devang Patel6ceea332009-08-31 18:49:10 +00001457 } else if (DT.isDerivedType()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001458 DIDerivedType DDT(DT);
Devang Patel3c91b052010-03-08 20:52:55 +00001459 processType(DDT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001460 }
1461}
1462
Devang Patelbeab41b2009-10-07 22:04:08 +00001463/// processLexicalBlock
1464void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
Devang Patelbeab41b2009-10-07 22:04:08 +00001465 DIScope Context = LB.getContext();
1466 if (Context.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001467 return processLexicalBlock(DILexicalBlock(Context));
Devang Patelbeab41b2009-10-07 22:04:08 +00001468 else
Devang Patel2db49d72010-05-07 18:11:54 +00001469 return processSubprogram(DISubprogram(Context));
Devang Patelbeab41b2009-10-07 22:04:08 +00001470}
1471
Devang Patel98c65172009-07-30 18:25:15 +00001472/// processSubprogram - Process DISubprogram.
1473void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001474 if (!addSubprogram(SP))
1475 return;
1476 addCompileUnit(SP.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +00001477 processType(SP.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +00001478}
1479
Devang Patelb4d31302009-07-31 18:18:52 +00001480/// processDeclare - Process DbgDeclareInst.
1481void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
Devang Patel3c91b052010-03-08 20:52:55 +00001482 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1483 if (!N) return;
1484
1485 DIDescriptor DV(N);
1486 if (!DV.isVariable())
Devang Patelb4d31302009-07-31 18:18:52 +00001487 return;
1488
Devang Patel2db49d72010-05-07 18:11:54 +00001489 if (!NodesSeen.insert(DV))
Devang Patelb4d31302009-07-31 18:18:52 +00001490 return;
1491
Devang Patel3c91b052010-03-08 20:52:55 +00001492 addCompileUnit(DIVariable(N).getCompileUnit());
1493 processType(DIVariable(N).getType());
Devang Patelb4d31302009-07-31 18:18:52 +00001494}
1495
Devang Patel72bcdb62009-08-10 22:09:58 +00001496/// addType - Add type into Tys.
1497bool DebugInfoFinder::addType(DIType DT) {
Devang Patel3c91b052010-03-08 20:52:55 +00001498 if (!DT.isValid())
Devang Patel72bcdb62009-08-10 22:09:58 +00001499 return false;
1500
Devang Patel2db49d72010-05-07 18:11:54 +00001501 if (!NodesSeen.insert(DT))
Devang Patel72bcdb62009-08-10 22:09:58 +00001502 return false;
1503
Devang Patel2db49d72010-05-07 18:11:54 +00001504 TYs.push_back(DT);
Devang Patel72bcdb62009-08-10 22:09:58 +00001505 return true;
1506}
1507
Devang Pateld2f79a12009-07-28 19:55:13 +00001508/// addCompileUnit - Add compile unit into CUs.
Devang Patel98c65172009-07-30 18:25:15 +00001509bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Patel3c91b052010-03-08 20:52:55 +00001510 if (!CU.Verify())
Devang Pateld2f79a12009-07-28 19:55:13 +00001511 return false;
1512
Devang Patel2db49d72010-05-07 18:11:54 +00001513 if (!NodesSeen.insert(CU))
Devang Pateld2f79a12009-07-28 19:55:13 +00001514 return false;
1515
Devang Patel2db49d72010-05-07 18:11:54 +00001516 CUs.push_back(CU);
Devang Pateld2f79a12009-07-28 19:55:13 +00001517 return true;
1518}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001519
Devang Pateld2f79a12009-07-28 19:55:13 +00001520/// addGlobalVariable - Add global variable into GVs.
Devang Patel98c65172009-07-30 18:25:15 +00001521bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Patel2db49d72010-05-07 18:11:54 +00001522 if (!DIDescriptor(DIG).isGlobalVariable())
Devang Pateld2f79a12009-07-28 19:55:13 +00001523 return false;
1524
Devang Patel2db49d72010-05-07 18:11:54 +00001525 if (!NodesSeen.insert(DIG))
Devang Pateld2f79a12009-07-28 19:55:13 +00001526 return false;
1527
Devang Patel2db49d72010-05-07 18:11:54 +00001528 GVs.push_back(DIG);
Devang Pateld2f79a12009-07-28 19:55:13 +00001529 return true;
1530}
1531
1532// addSubprogram - Add subprgoram into SPs.
Devang Patel98c65172009-07-30 18:25:15 +00001533bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Patel2db49d72010-05-07 18:11:54 +00001534 if (!DIDescriptor(SP).isSubprogram())
Devang Pateld2f79a12009-07-28 19:55:13 +00001535 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001536
Devang Patel2db49d72010-05-07 18:11:54 +00001537 if (!NodesSeen.insert(SP))
Devang Pateld2f79a12009-07-28 19:55:13 +00001538 return false;
1539
Devang Patel2db49d72010-05-07 18:11:54 +00001540 SPs.push_back(SP);
Devang Pateld2f79a12009-07-28 19:55:13 +00001541 return true;
1542}
1543
Victor Hernandez756462b2010-01-18 20:42:09 +00001544/// Find the debug info descriptor corresponding to this global variable.
1545static Value *findDbgGlobalDeclare(GlobalVariable *V) {
Chris Lattner099b7792009-12-29 09:22:47 +00001546 const Module *M = V->getParent();
1547 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1548 if (!NMD)
Torok Edwinff7d0e92009-03-10 13:41:26 +00001549 return 0;
Chris Lattner099b7792009-12-29 09:22:47 +00001550
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001551 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman872814a2010-07-21 18:54:18 +00001552 DIDescriptor DIG(cast<MDNode>(NMD->getOperand(i)));
Devang Patel3c91b052010-03-08 20:52:55 +00001553 if (!DIG.isGlobalVariable())
Chris Lattner099b7792009-12-29 09:22:47 +00001554 continue;
Devang Patel2db49d72010-05-07 18:11:54 +00001555 if (DIGlobalVariable(DIG).getGlobal() == V)
1556 return DIG;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001557 }
Chris Lattner099b7792009-12-29 09:22:47 +00001558 return 0;
1559}
Torok Edwinff7d0e92009-03-10 13:41:26 +00001560
Chris Lattner099b7792009-12-29 09:22:47 +00001561/// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
1562/// It looks through pointer casts too.
Victor Hernandez756462b2010-01-18 20:42:09 +00001563static const DbgDeclareInst *findDbgDeclare(const Value *V) {
Victor Hernandez3a328652010-01-15 19:04:09 +00001564 V = V->stripPointerCasts();
Jim Grosbache62b6902010-07-21 21:36:25 +00001565
Victor Hernandez3a328652010-01-15 19:04:09 +00001566 if (!isa<Instruction>(V) && !isa<Argument>(V))
Torok Edwin620f2802008-12-16 09:07:36 +00001567 return 0;
Jim Grosbache62b6902010-07-21 21:36:25 +00001568
Victor Hernandez3a328652010-01-15 19:04:09 +00001569 const Function *F = NULL;
1570 if (const Instruction *I = dyn_cast<Instruction>(V))
1571 F = I->getParent()->getParent();
1572 else if (const Argument *A = dyn_cast<Argument>(V))
1573 F = A->getParent();
Jim Grosbache62b6902010-07-21 21:36:25 +00001574
Victor Hernandez3a328652010-01-15 19:04:09 +00001575 for (Function::const_iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
1576 for (BasicBlock::const_iterator BI = (*FI).begin(), BE = (*FI).end();
1577 BI != BE; ++BI)
1578 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1579 if (DDI->getAddress() == V)
1580 return DDI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001581
Chris Lattner099b7792009-12-29 09:22:47 +00001582 return 0;
1583}
Bill Wendlingdc817b62009-05-14 18:26:15 +00001584
Chris Lattner099b7792009-12-29 09:22:47 +00001585bool llvm::getLocationInfo(const Value *V, std::string &DisplayName,
1586 std::string &Type, unsigned &LineNo,
1587 std::string &File, std::string &Dir) {
1588 DICompileUnit Unit;
1589 DIType TypeD;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001590
Chris Lattner099b7792009-12-29 09:22:47 +00001591 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1592 Value *DIGV = findDbgGlobalDeclare(GV);
1593 if (!DIGV) return false;
1594 DIGlobalVariable Var(cast<MDNode>(DIGV));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001595
Chris Lattner099b7792009-12-29 09:22:47 +00001596 StringRef D = Var.getDisplayName();
Devang Patel65dbc902009-11-25 17:36:49 +00001597 if (!D.empty())
Chris Lattner099b7792009-12-29 09:22:47 +00001598 DisplayName = D;
1599 LineNo = Var.getLineNumber();
1600 Unit = Var.getCompileUnit();
1601 TypeD = Var.getType();
1602 } else {
1603 const DbgDeclareInst *DDI = findDbgDeclare(V);
1604 if (!DDI) return false;
1605 DIVariable Var(cast<MDNode>(DDI->getVariable()));
1606
1607 StringRef D = Var.getName();
1608 if (!D.empty())
1609 DisplayName = D;
1610 LineNo = Var.getLineNumber();
1611 Unit = Var.getCompileUnit();
1612 TypeD = Var.getType();
Torok Edwinff7d0e92009-03-10 13:41:26 +00001613 }
Devang Patel13e16b62009-06-26 01:49:18 +00001614
Chris Lattner099b7792009-12-29 09:22:47 +00001615 StringRef T = TypeD.getName();
1616 if (!T.empty())
1617 Type = T;
1618 StringRef F = Unit.getFilename();
1619 if (!F.empty())
1620 File = F;
1621 StringRef D = Unit.getDirectory();
1622 if (!D.empty())
1623 Dir = D;
1624 return true;
1625}
Devang Patel9e529c32009-07-02 01:15:24 +00001626
Chris Lattner099b7792009-12-29 09:22:47 +00001627/// getDISubprogram - Find subprogram that is enclosing this scope.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001628DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
Chris Lattner099b7792009-12-29 09:22:47 +00001629 DIDescriptor D(Scope);
Chris Lattner099b7792009-12-29 09:22:47 +00001630 if (D.isSubprogram())
1631 return DISubprogram(Scope);
Jim Grosbache62b6902010-07-21 21:36:25 +00001632
Chris Lattner099b7792009-12-29 09:22:47 +00001633 if (D.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001634 return getDISubprogram(DILexicalBlock(Scope).getContext());
Jim Grosbache62b6902010-07-21 21:36:25 +00001635
Chris Lattner099b7792009-12-29 09:22:47 +00001636 return DISubprogram();
1637}
Devang Patel193f7202009-11-24 01:14:22 +00001638
Chris Lattner099b7792009-12-29 09:22:47 +00001639/// getDICompositeType - Find underlying composite type.
1640DICompositeType llvm::getDICompositeType(DIType T) {
Chris Lattner099b7792009-12-29 09:22:47 +00001641 if (T.isCompositeType())
Devang Patel2db49d72010-05-07 18:11:54 +00001642 return DICompositeType(T);
Jim Grosbache62b6902010-07-21 21:36:25 +00001643
Chris Lattner099b7792009-12-29 09:22:47 +00001644 if (T.isDerivedType())
Devang Patel2db49d72010-05-07 18:11:54 +00001645 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
Jim Grosbache62b6902010-07-21 21:36:25 +00001646
Chris Lattner099b7792009-12-29 09:22:47 +00001647 return DICompositeType();
Torok Edwin620f2802008-12-16 09:07:36 +00001648}