blob: 9e802c64b0e41a4b6f68460b2c2511b172062699 [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 Patel0a2551d2010-12-08 20:18:20 +0000291/// replaceAllUsesWith - Replace all uses of debug info referenced by
292/// this descriptor.
293void DIType::replaceAllUsesWith(MDNode *D) {
294 if (!DbgNode)
295 return;
296
297 // Since we use a TrackingVH for the node, its easy for clients to manufacture
298 // legitimate situations where they want to replaceAllUsesWith() on something
299 // which, due to uniquing, has merged with the source. We shield clients from
300 // this detail by allowing a value to be replaced with replaceAllUsesWith()
301 // itself.
302 if (DbgNode != D) {
303 MDNode *Node = const_cast<MDNode*>(DbgNode);
304 const MDNode *DN = D;
305 const Value *V = cast_or_null<Value>(DN);
306 Node->replaceAllUsesWith(const_cast<Value*>(V));
307 MDNode::deleteTemporary(Node);
308 }
309}
310
Devang Patelb79b5352009-01-19 23:21:49 +0000311/// Verify - Verify that a compile unit is well formed.
312bool DICompileUnit::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000313 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000314 return false;
Devang Patel65dbc902009-11-25 17:36:49 +0000315 StringRef N = getFilename();
316 if (N.empty())
Bill Wendling0582ae92009-03-13 04:39:26 +0000317 return false;
Devang Patelb79b5352009-01-19 23:21:49 +0000318 // It is possible that directory and produce string is empty.
Bill Wendling0582ae92009-03-13 04:39:26 +0000319 return true;
Devang Patelb79b5352009-01-19 23:21:49 +0000320}
321
322/// Verify - Verify that a type descriptor is well formed.
323bool DIType::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000324 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000325 return false;
Devang Patel3c91b052010-03-08 20:52:55 +0000326 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000327 return false;
Devang Patelb71bbf92010-11-02 20:41:13 +0000328 unsigned Tag = getTag();
329 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
330 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
Devang Patelfe58f952010-12-07 23:25:47 +0000331 Tag != dwarf::DW_TAG_reference_type && Tag != dwarf::DW_TAG_restrict_type
Devang Patel43c249c2010-12-08 01:50:15 +0000332 && Tag != dwarf::DW_TAG_vector_type && Tag != dwarf::DW_TAG_array_type
333 && Tag != dwarf::DW_TAG_enumeration_type
Devang Patelfe58f952010-12-07 23:25:47 +0000334 && getFilename().empty())
Devang Patelb79b5352009-01-19 23:21:49 +0000335 return false;
336 return true;
337}
338
Devang Patel0c4720c2010-08-23 18:25:56 +0000339/// Verify - Verify that a basic type descriptor is well formed.
340bool DIBasicType::Verify() const {
341 return isBasicType();
342}
343
344/// Verify - Verify that a derived type descriptor is well formed.
345bool DIDerivedType::Verify() const {
346 return isDerivedType();
347}
348
Devang Patelb79b5352009-01-19 23:21:49 +0000349/// Verify - Verify that a composite type descriptor is well formed.
350bool DICompositeType::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000351 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000352 return false;
Devang Patel3c91b052010-03-08 20:52:55 +0000353 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000354 return false;
355
356 DICompileUnit CU = getCompileUnit();
Devang Patel3c91b052010-03-08 20:52:55 +0000357 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000358 return false;
359 return true;
360}
361
362/// Verify - Verify that a subprogram descriptor is well formed.
363bool DISubprogram::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000364 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000365 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000366
Devang Patel3c91b052010-03-08 20:52:55 +0000367 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000368 return false;
369
370 DICompileUnit CU = getCompileUnit();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000371 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000372 return false;
373
374 DICompositeType Ty = getType();
Devang Patel3c91b052010-03-08 20:52:55 +0000375 if (!Ty.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000376 return false;
377 return true;
378}
379
380/// Verify - Verify that a global variable descriptor is well formed.
381bool DIGlobalVariable::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000382 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000383 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000384
Devang Patel65dbc902009-11-25 17:36:49 +0000385 if (getDisplayName().empty())
Devang Patel84c73e92009-11-06 17:58:12 +0000386 return false;
387
Devang Patel3c91b052010-03-08 20:52:55 +0000388 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000389 return false;
390
391 DICompileUnit CU = getCompileUnit();
Devang Patel3c91b052010-03-08 20:52:55 +0000392 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000393 return false;
394
395 DIType Ty = getType();
396 if (!Ty.Verify())
397 return false;
398
Devang Patel27398962010-08-09 21:39:24 +0000399 if (!getGlobal() && !getConstant())
Devang Patelb79b5352009-01-19 23:21:49 +0000400 return false;
401
402 return true;
403}
404
405/// Verify - Verify that a variable descriptor is well formed.
406bool DIVariable::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000407 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000408 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000409
Devang Patel3c91b052010-03-08 20:52:55 +0000410 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000411 return false;
412
Devang Patel62077af2010-05-07 21:42:24 +0000413 if (!getCompileUnit().Verify())
414 return false;
415
Devang Patelb79b5352009-01-19 23:21:49 +0000416 DIType Ty = getType();
417 if (!Ty.Verify())
418 return false;
419
Devang Patelb79b5352009-01-19 23:21:49 +0000420 return true;
421}
422
Devang Patel3c91b052010-03-08 20:52:55 +0000423/// Verify - Verify that a location descriptor is well formed.
424bool DILocation::Verify() const {
425 if (!DbgNode)
426 return false;
Jim Grosbache62b6902010-07-21 21:36:25 +0000427
Devang Patel3c91b052010-03-08 20:52:55 +0000428 return DbgNode->getNumOperands() == 4;
429}
430
Devang Patel47e22652010-05-07 23:04:32 +0000431/// Verify - Verify that a namespace descriptor is well formed.
432bool DINameSpace::Verify() const {
433 if (!DbgNode)
434 return false;
435 if (getName().empty())
436 return false;
437 if (!getCompileUnit().Verify())
438 return false;
439 return true;
440}
441
Devang Patel36375ee2009-02-17 21:23:59 +0000442/// getOriginalTypeSize - If this type is derived from a base type then
443/// return base type size.
444uint64_t DIDerivedType::getOriginalTypeSize() const {
Devang Patel61ecbd12009-11-04 23:48:00 +0000445 unsigned Tag = getTag();
446 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
447 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
448 Tag == dwarf::DW_TAG_restrict_type) {
449 DIType BaseType = getTypeDerivedFrom();
Jim Grosbache62b6902010-07-21 21:36:25 +0000450 // If this type is not derived from any type then take conservative
Devang Patel5ebfa2d2009-11-06 18:24:05 +0000451 // approach.
Devang Patel3c91b052010-03-08 20:52:55 +0000452 if (!BaseType.isValid())
Devang Patel5ebfa2d2009-11-06 18:24:05 +0000453 return getSizeInBits();
Devang Patel61ecbd12009-11-04 23:48:00 +0000454 if (BaseType.isDerivedType())
Devang Patel2db49d72010-05-07 18:11:54 +0000455 return DIDerivedType(BaseType).getOriginalTypeSize();
Devang Patel61ecbd12009-11-04 23:48:00 +0000456 else
457 return BaseType.getSizeInBits();
458 }
Jim Grosbache62b6902010-07-21 21:36:25 +0000459
Devang Patel61ecbd12009-11-04 23:48:00 +0000460 return getSizeInBits();
Devang Patel36375ee2009-02-17 21:23:59 +0000461}
Devang Patelb79b5352009-01-19 23:21:49 +0000462
Jim Grosbache62b6902010-07-21 21:36:25 +0000463/// isInlinedFnArgument - Return true if this variable provides debugging
Devang Patel719f6a92010-04-29 20:40:36 +0000464/// information for an inlined function arguments.
465bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
466 assert(CurFn && "Invalid function");
467 if (!getContext().isSubprogram())
468 return false;
Jim Grosbache62b6902010-07-21 21:36:25 +0000469 // This variable is not inlined function argument if its scope
Devang Patel719f6a92010-04-29 20:40:36 +0000470 // does not describe current function.
Devang Patel2db49d72010-05-07 18:11:54 +0000471 return !(DISubprogram(getContext()).describes(CurFn));
Devang Patel719f6a92010-04-29 20:40:36 +0000472}
473
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000474/// describes - Return true if this subprogram provides debugging
475/// information for the function F.
476bool DISubprogram::describes(const Function *F) {
Chris Lattner099b7792009-12-29 09:22:47 +0000477 assert(F && "Invalid function");
Devang Patelffd33cd2010-06-16 06:42:02 +0000478 if (F == getFunction())
479 return true;
Devang Patel65dbc902009-11-25 17:36:49 +0000480 StringRef Name = getLinkageName();
481 if (Name.empty())
Devang Patel5ccdd102009-09-29 18:40:58 +0000482 Name = getName();
Devang Patel65dbc902009-11-25 17:36:49 +0000483 if (F->getName() == Name)
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000484 return true;
485 return false;
486}
487
Jim Grosbache62b6902010-07-21 21:36:25 +0000488unsigned DISubprogram::isOptimized() const {
Devang Patelccff8122010-04-30 19:38:23 +0000489 assert (DbgNode && "Invalid subprogram descriptor!");
490 if (DbgNode->getNumOperands() == 16)
491 return getUnsignedField(15);
492 return 0;
493}
494
Devang Patel65dbc902009-11-25 17:36:49 +0000495StringRef DIScope::getFilename() const {
Devang Patel77bf2952010-03-08 22:02:50 +0000496 if (!DbgNode)
497 return StringRef();
Jim Grosbache62b6902010-07-21 21:36:25 +0000498 if (isLexicalBlock())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000499 return DILexicalBlock(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000500 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000501 return DISubprogram(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000502 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000503 return DICompileUnit(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000504 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000505 return DINameSpace(DbgNode).getFilename();
Devang Patel77bf2952010-03-08 22:02:50 +0000506 if (isType())
507 return DIType(DbgNode).getFilename();
Devang Patel7aa81892010-03-08 22:27:22 +0000508 if (isFile())
509 return DIFile(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000510 assert(0 && "Invalid DIScope!");
Devang Patel65dbc902009-11-25 17:36:49 +0000511 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000512}
513
Devang Patel65dbc902009-11-25 17:36:49 +0000514StringRef DIScope::getDirectory() const {
Devang Patel77bf2952010-03-08 22:02:50 +0000515 if (!DbgNode)
516 return StringRef();
Jim Grosbache62b6902010-07-21 21:36:25 +0000517 if (isLexicalBlock())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000518 return DILexicalBlock(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000519 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000520 return DISubprogram(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000521 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000522 return DICompileUnit(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000523 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000524 return DINameSpace(DbgNode).getDirectory();
Devang Patel77bf2952010-03-08 22:02:50 +0000525 if (isType())
526 return DIType(DbgNode).getDirectory();
Devang Patel7aa81892010-03-08 22:27:22 +0000527 if (isFile())
528 return DIFile(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000529 assert(0 && "Invalid DIScope!");
Devang Patel65dbc902009-11-25 17:36:49 +0000530 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000531}
532
Chris Lattnera45664f2008-11-10 02:56:27 +0000533//===----------------------------------------------------------------------===//
Devang Patel7136a652009-07-01 22:10:23 +0000534// DIDescriptor: dump routines for all descriptors.
535//===----------------------------------------------------------------------===//
536
537
Dan Gohman50404362010-05-07 15:30:29 +0000538/// print - Print descriptor.
539void DIDescriptor::print(raw_ostream &OS) const {
540 OS << "[" << dwarf::TagString(getTag()) << "] ";
541 OS.write_hex((intptr_t) &*DbgNode) << ']';
Devang Patel7136a652009-07-01 22:10:23 +0000542}
543
Dan Gohman50404362010-05-07 15:30:29 +0000544/// print - Print compile unit.
545void DICompileUnit::print(raw_ostream &OS) const {
Devang Patel7136a652009-07-01 22:10:23 +0000546 if (getLanguage())
Dan Gohman50404362010-05-07 15:30:29 +0000547 OS << " [" << dwarf::LanguageString(getLanguage()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000548
Dan Gohmanfaa19c32010-05-10 20:07:44 +0000549 OS << " [" << getDirectory() << "/" << getFilename() << "]";
Devang Patel7136a652009-07-01 22:10:23 +0000550}
551
Dan Gohman50404362010-05-07 15:30:29 +0000552/// print - Print type.
553void DIType::print(raw_ostream &OS) const {
Devang Patel3c91b052010-03-08 20:52:55 +0000554 if (!DbgNode) return;
Devang Patel7136a652009-07-01 22:10:23 +0000555
Devang Patel65dbc902009-11-25 17:36:49 +0000556 StringRef Res = getName();
557 if (!Res.empty())
Dan Gohman50404362010-05-07 15:30:29 +0000558 OS << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000559
560 unsigned Tag = getTag();
Dan Gohman50404362010-05-07 15:30:29 +0000561 OS << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000562
563 // TODO : Print context
Dan Gohmanc014d092010-05-07 16:17:22 +0000564 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000565 OS << " ["
Dan Gohman9a7063e2010-05-07 16:39:27 +0000566 << "line " << getLineNumber() << ", "
567 << getSizeInBits() << " bits, "
568 << getAlignInBits() << " bit alignment, "
569 << getOffsetInBits() << " bit offset"
Chris Lattnera81d29b2009-08-23 07:33:14 +0000570 << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000571
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000572 if (isPrivate())
Dan Gohman50404362010-05-07 15:30:29 +0000573 OS << " [private] ";
Devang Patel7136a652009-07-01 22:10:23 +0000574 else if (isProtected())
Dan Gohman50404362010-05-07 15:30:29 +0000575 OS << " [protected] ";
Devang Patel7136a652009-07-01 22:10:23 +0000576
577 if (isForwardDecl())
Dan Gohman50404362010-05-07 15:30:29 +0000578 OS << " [fwd] ";
Devang Patel7136a652009-07-01 22:10:23 +0000579
Devang Patel6ceea332009-08-31 18:49:10 +0000580 if (isBasicType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000581 DIBasicType(DbgNode).print(OS);
Devang Patel6ceea332009-08-31 18:49:10 +0000582 else if (isDerivedType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000583 DIDerivedType(DbgNode).print(OS);
Devang Patel6ceea332009-08-31 18:49:10 +0000584 else if (isCompositeType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000585 DICompositeType(DbgNode).print(OS);
Devang Patel7136a652009-07-01 22:10:23 +0000586 else {
Dan Gohman50404362010-05-07 15:30:29 +0000587 OS << "Invalid DIType\n";
Devang Patel7136a652009-07-01 22:10:23 +0000588 return;
589 }
590
Dan Gohman50404362010-05-07 15:30:29 +0000591 OS << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000592}
593
Dan Gohman50404362010-05-07 15:30:29 +0000594/// print - Print basic type.
595void DIBasicType::print(raw_ostream &OS) const {
596 OS << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000597}
598
Dan Gohman50404362010-05-07 15:30:29 +0000599/// print - Print derived type.
600void DIDerivedType::print(raw_ostream &OS) const {
Dan Gohmanc014d092010-05-07 16:17:22 +0000601 OS << "\n\t Derived From: "; getTypeDerivedFrom().print(OS);
Devang Patel7136a652009-07-01 22:10:23 +0000602}
603
Dan Gohman50404362010-05-07 15:30:29 +0000604/// print - Print composite type.
605void DICompositeType::print(raw_ostream &OS) const {
Devang Patel7136a652009-07-01 22:10:23 +0000606 DIArray A = getTypeArray();
Dan Gohman50404362010-05-07 15:30:29 +0000607 OS << " [" << A.getNumElements() << " elements]";
Devang Patel7136a652009-07-01 22:10:23 +0000608}
609
Dan Gohman50404362010-05-07 15:30:29 +0000610/// print - Print subprogram.
611void DISubprogram::print(raw_ostream &OS) const {
Devang Patel65dbc902009-11-25 17:36:49 +0000612 StringRef Res = getName();
613 if (!Res.empty())
Dan Gohman50404362010-05-07 15:30:29 +0000614 OS << " [" << Res << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000615
616 unsigned Tag = getTag();
Dan Gohman50404362010-05-07 15:30:29 +0000617 OS << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000618
619 // TODO : Print context
Dan Gohmanc014d092010-05-07 16:17:22 +0000620 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000621 OS << " [" << getLineNumber() << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000622
623 if (isLocalToUnit())
Dan Gohman50404362010-05-07 15:30:29 +0000624 OS << " [local] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000625
626 if (isDefinition())
Dan Gohman50404362010-05-07 15:30:29 +0000627 OS << " [def] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000628
Dan Gohman50404362010-05-07 15:30:29 +0000629 OS << "\n";
630}
631
632/// print - Print global variable.
633void DIGlobalVariable::print(raw_ostream &OS) const {
634 OS << " [";
Devang Patela49d8772010-05-07 23:19:07 +0000635 StringRef Res = getName();
636 if (!Res.empty())
637 OS << " [" << Res << "] ";
638
639 unsigned Tag = getTag();
640 OS << " [" << dwarf::TagString(Tag) << "] ";
641
642 // TODO : Print context
643 getCompileUnit().print(OS);
644 OS << " [" << getLineNumber() << "] ";
645
646 if (isLocalToUnit())
647 OS << " [local] ";
648
649 if (isDefinition())
650 OS << " [def] ";
651
652 if (isGlobalVariable())
653 DIGlobalVariable(DbgNode).print(OS);
654 OS << "]\n";
Dan Gohman50404362010-05-07 15:30:29 +0000655}
656
657/// print - Print variable.
658void DIVariable::print(raw_ostream &OS) const {
659 StringRef Res = getName();
660 if (!Res.empty())
661 OS << " [" << Res << "] ";
662
Dan Gohmanc014d092010-05-07 16:17:22 +0000663 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000664 OS << " [" << getLineNumber() << "] ";
Dan Gohmanc014d092010-05-07 16:17:22 +0000665 getType().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000666 OS << "\n";
667
668 // FIXME: Dump complex addresses
669}
670
671/// dump - Print descriptor to dbgs() with a newline.
672void DIDescriptor::dump() const {
673 print(dbgs()); dbgs() << '\n';
674}
675
676/// dump - Print compile unit to dbgs() with a newline.
677void DICompileUnit::dump() const {
678 print(dbgs()); dbgs() << '\n';
679}
680
681/// dump - Print type to dbgs() with a newline.
682void DIType::dump() const {
683 print(dbgs()); dbgs() << '\n';
684}
685
686/// dump - Print basic type to dbgs() with a newline.
687void DIBasicType::dump() const {
688 print(dbgs()); dbgs() << '\n';
689}
690
691/// dump - Print derived type to dbgs() with a newline.
692void DIDerivedType::dump() const {
693 print(dbgs()); dbgs() << '\n';
694}
695
696/// dump - Print composite type to dbgs() with a newline.
697void DICompositeType::dump() const {
698 print(dbgs()); dbgs() << '\n';
699}
700
Dan Gohman50404362010-05-07 15:30:29 +0000701/// dump - Print subprogram to dbgs() with a newline.
702void DISubprogram::dump() const {
703 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000704}
705
706/// dump - Print global variable.
707void DIGlobalVariable::dump() const {
Dan Gohman50404362010-05-07 15:30:29 +0000708 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000709}
710
711/// dump - Print variable.
712void DIVariable::dump() const {
Dan Gohman50404362010-05-07 15:30:29 +0000713 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000714}
715
716//===----------------------------------------------------------------------===//
Chris Lattnera45664f2008-11-10 02:56:27 +0000717// DIFactory: Basic Helpers
718//===----------------------------------------------------------------------===//
719
Bill Wendlingdc817b62009-05-14 18:26:15 +0000720DIFactory::DIFactory(Module &m)
Victor Hernandez06203122010-01-23 00:03:28 +0000721 : M(m), VMContext(M.getContext()), DeclareFn(0), ValueFn(0) {}
Chris Lattner497a7a82008-11-10 04:10:34 +0000722
Chris Lattnera45664f2008-11-10 02:56:27 +0000723Constant *DIFactory::GetTagConstant(unsigned TAG) {
Devang Patel6906ba52009-01-20 19:22:03 +0000724 assert((TAG & LLVMDebugVersionMask) == 0 &&
Chris Lattnera45664f2008-11-10 02:56:27 +0000725 "Tag too large for debug encoding!");
Owen Anderson1d0be152009-08-13 21:58:54 +0000726 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
Chris Lattnera45664f2008-11-10 02:56:27 +0000727}
728
Chris Lattnera45664f2008-11-10 02:56:27 +0000729//===----------------------------------------------------------------------===//
730// DIFactory: Primary Constructors
731//===----------------------------------------------------------------------===//
732
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000733/// GetOrCreateArray - Create an descriptor for an array of descriptors.
Chris Lattnera45664f2008-11-10 02:56:27 +0000734/// This implicitly uniques the arrays created.
735DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
Benjamin Kramera53557e2010-09-21 16:41:29 +0000736 if (NumTys == 0) {
737 Value *Null = llvm::Constant::getNullValue(Type::getInt32Ty(VMContext));
738 return DIArray(MDNode::get(VMContext, &Null, 1));
739 }
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000740
Benjamin Kramera53557e2010-09-21 16:41:29 +0000741 SmallVector<Value *, 16> Elts(Tys, Tys+NumTys);
742 return DIArray(MDNode::get(VMContext, Elts.data(), Elts.size()));
Chris Lattnera45664f2008-11-10 02:56:27 +0000743}
744
745/// GetOrCreateSubrange - Create a descriptor for a value range. This
746/// implicitly uniques the values returned.
747DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
Devang Patele4b27562009-08-28 23:24:31 +0000748 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000749 GetTagConstant(dwarf::DW_TAG_subrange_type),
Owen Anderson1d0be152009-08-13 21:58:54 +0000750 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
751 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
Chris Lattnera45664f2008-11-10 02:56:27 +0000752 };
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000753
Devang Patele4b27562009-08-28 23:24:31 +0000754 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000755}
756
Devang Pateld6747df2010-10-06 20:50:40 +0000757/// CreateUnspecifiedParameter - Create unspeicified type descriptor
758/// for the subroutine type.
759DIDescriptor DIFactory::CreateUnspecifiedParameter() {
760 Value *Elts[] = {
761 GetTagConstant(dwarf::DW_TAG_unspecified_parameters)
762 };
763 return DIDescriptor(MDNode::get(VMContext, &Elts[0], 1));
764}
Chris Lattnera45664f2008-11-10 02:56:27 +0000765
766/// CreateCompileUnit - Create a new descriptor for the specified compile
767/// unit. Note that this does not unique compile units within the module.
768DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
Devang Patel65dbc902009-11-25 17:36:49 +0000769 StringRef Filename,
770 StringRef Directory,
771 StringRef Producer,
Devang Pateldd9db662009-01-30 18:20:31 +0000772 bool isMain,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000773 bool isOptimized,
Devang Patel65dbc902009-11-25 17:36:49 +0000774 StringRef Flags,
Devang Patel13319ce2009-02-17 22:43:44 +0000775 unsigned RunTimeVer) {
Devang Patele4b27562009-08-28 23:24:31 +0000776 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000777 GetTagConstant(dwarf::DW_TAG_compile_unit),
Devang Patele4b27562009-08-28 23:24:31 +0000778 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Owen Anderson1d0be152009-08-13 21:58:54 +0000779 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
Devang Patele4b27562009-08-28 23:24:31 +0000780 MDString::get(VMContext, Filename),
781 MDString::get(VMContext, Directory),
782 MDString::get(VMContext, Producer),
Owen Anderson1d0be152009-08-13 21:58:54 +0000783 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
784 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
Devang Patele4b27562009-08-28 23:24:31 +0000785 MDString::get(VMContext, Flags),
Owen Anderson1d0be152009-08-13 21:58:54 +0000786 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
Chris Lattnera45664f2008-11-10 02:56:27 +0000787 };
Devang Patele4b27562009-08-28 23:24:31 +0000788
789 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000790}
791
Devang Patel7aa81892010-03-08 22:27:22 +0000792/// CreateFile - Create a new descriptor for the specified file.
793DIFile DIFactory::CreateFile(StringRef Filename,
794 StringRef Directory,
795 DICompileUnit CU) {
796 Value *Elts[] = {
797 GetTagConstant(dwarf::DW_TAG_file_type),
798 MDString::get(VMContext, Filename),
799 MDString::get(VMContext, Directory),
Devang Patel2db49d72010-05-07 18:11:54 +0000800 CU
Devang Patel7aa81892010-03-08 22:27:22 +0000801 };
802
803 return DIFile(MDNode::get(VMContext, &Elts[0], 4));
804}
805
Chris Lattnera45664f2008-11-10 02:56:27 +0000806/// CreateEnumerator - Create a single enumerator value.
Devang Patel65dbc902009-11-25 17:36:49 +0000807DIEnumerator DIFactory::CreateEnumerator(StringRef Name, uint64_t Val){
Devang Patele4b27562009-08-28 23:24:31 +0000808 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000809 GetTagConstant(dwarf::DW_TAG_enumerator),
Devang Patele4b27562009-08-28 23:24:31 +0000810 MDString::get(VMContext, Name),
Owen Anderson1d0be152009-08-13 21:58:54 +0000811 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
Chris Lattnera45664f2008-11-10 02:56:27 +0000812 };
Devang Patele4b27562009-08-28 23:24:31 +0000813 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000814}
815
816
817/// CreateBasicType - Create a basic type like int, float, etc.
818DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000819 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000820 DIFile F,
Chris Lattnera45664f2008-11-10 02:56:27 +0000821 unsigned LineNumber,
822 uint64_t SizeInBits,
823 uint64_t AlignInBits,
824 uint64_t OffsetInBits, unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000825 unsigned Encoding) {
Devang Patele4b27562009-08-28 23:24:31 +0000826 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000827 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patel2db49d72010-05-07 18:11:54 +0000828 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000829 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000830 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000831 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
832 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
833 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
834 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
835 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
836 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
Chris Lattnera45664f2008-11-10 02:56:27 +0000837 };
Devang Patele4b27562009-08-28 23:24:31 +0000838 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000839}
840
Devang Patelac16d442009-10-26 16:54:35 +0000841
842/// CreateBasicType - Create a basic type like int, float, etc.
843DIBasicType DIFactory::CreateBasicTypeEx(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000844 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000845 DIFile F,
Devang Patelac16d442009-10-26 16:54:35 +0000846 unsigned LineNumber,
847 Constant *SizeInBits,
848 Constant *AlignInBits,
849 Constant *OffsetInBits, unsigned Flags,
850 unsigned Encoding) {
851 Value *Elts[] = {
852 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patel2db49d72010-05-07 18:11:54 +0000853 Context,
Devang Patelac16d442009-10-26 16:54:35 +0000854 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000855 F,
Devang Patelac16d442009-10-26 16:54:35 +0000856 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
857 SizeInBits,
858 AlignInBits,
859 OffsetInBits,
860 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
861 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
862 };
863 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
864}
865
Devang Patelb4645642010-02-06 01:02:37 +0000866/// CreateArtificialType - Create a new DIType with "artificial" flag set.
867DIType DIFactory::CreateArtificialType(DIType Ty) {
868 if (Ty.isArtificial())
869 return Ty;
870
871 SmallVector<Value *, 9> Elts;
Devang Patel2db49d72010-05-07 18:11:54 +0000872 MDNode *N = Ty;
Devang Patelb4645642010-02-06 01:02:37 +0000873 assert (N && "Unexpected input DIType!");
874 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
875 if (Value *V = N->getOperand(i))
876 Elts.push_back(V);
877 else
878 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
879 }
880
881 unsigned CurFlags = Ty.getFlags();
882 CurFlags = CurFlags | DIType::FlagArtificial;
883
884 // Flags are stored at this slot.
885 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
886
887 return DIType(MDNode::get(VMContext, Elts.data(), Elts.size()));
888}
Devang Patelac16d442009-10-26 16:54:35 +0000889
Chris Lattnera45664f2008-11-10 02:56:27 +0000890/// CreateDerivedType - Create a derived type like const qualified type,
891/// pointer, typedef, etc.
892DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
893 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000894 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000895 DIFile F,
Chris Lattnera45664f2008-11-10 02:56:27 +0000896 unsigned LineNumber,
897 uint64_t SizeInBits,
898 uint64_t AlignInBits,
899 uint64_t OffsetInBits,
900 unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000901 DIType DerivedFrom) {
Devang Patele4b27562009-08-28 23:24:31 +0000902 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000903 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000904 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000905 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000906 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000907 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
908 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
909 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
910 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
911 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000912 DerivedFrom,
Chris Lattnera45664f2008-11-10 02:56:27 +0000913 };
Devang Patele4b27562009-08-28 23:24:31 +0000914 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000915}
916
Devang Patelac16d442009-10-26 16:54:35 +0000917
918/// CreateDerivedType - Create a derived type like const qualified type,
919/// pointer, typedef, etc.
920DIDerivedType DIFactory::CreateDerivedTypeEx(unsigned Tag,
921 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000922 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000923 DIFile F,
Devang Patelac16d442009-10-26 16:54:35 +0000924 unsigned LineNumber,
925 Constant *SizeInBits,
926 Constant *AlignInBits,
927 Constant *OffsetInBits,
928 unsigned Flags,
929 DIType DerivedFrom) {
930 Value *Elts[] = {
931 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000932 Context,
Devang Patelac16d442009-10-26 16:54:35 +0000933 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000934 F,
Devang Patelac16d442009-10-26 16:54:35 +0000935 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
936 SizeInBits,
937 AlignInBits,
938 OffsetInBits,
939 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000940 DerivedFrom,
Devang Patelac16d442009-10-26 16:54:35 +0000941 };
942 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
943}
944
945
Chris Lattnera45664f2008-11-10 02:56:27 +0000946/// CreateCompositeType - Create a composite type like array, struct, etc.
947DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
948 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000949 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000950 DIFile F,
Chris Lattnera45664f2008-11-10 02:56:27 +0000951 unsigned LineNumber,
952 uint64_t SizeInBits,
953 uint64_t AlignInBits,
954 uint64_t OffsetInBits,
955 unsigned Flags,
956 DIType DerivedFrom,
Devang Patel13319ce2009-02-17 22:43:44 +0000957 DIArray Elements,
Devang Patel0fd7f9d2010-01-26 21:14:59 +0000958 unsigned RuntimeLang,
959 MDNode *ContainingType) {
Owen Andersone277fed2009-07-07 16:31:25 +0000960
Devang Patele4b27562009-08-28 23:24:31 +0000961 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000962 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000963 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000964 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000965 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000966 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
967 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
968 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
969 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
970 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000971 DerivedFrom,
972 Elements,
Devang Patel0fd7f9d2010-01-26 21:14:59 +0000973 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
974 ContainingType
Chris Lattnera45664f2008-11-10 02:56:27 +0000975 };
Devang Patele7e5a0f2010-08-10 20:01:20 +0000976
977 MDNode *Node = MDNode::get(VMContext, &Elts[0], 13);
978 // Create a named metadata so that we do not lose this enum info.
979 if (Tag == dwarf::DW_TAG_enumeration_type) {
980 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.enum");
981 NMD->addOperand(Node);
982 }
983 return DICompositeType(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +0000984}
985
Dan Gohman489b29b2010-08-20 22:02:26 +0000986/// CreateTemporaryType - Create a temporary forward-declared type.
Dan Gohmana3833f12010-08-20 22:39:47 +0000987DIType DIFactory::CreateTemporaryType() {
Dan Gohman489b29b2010-08-20 22:02:26 +0000988 // Give the temporary MDNode a tag. It doesn't matter what tag we
989 // use here as long as DIType accepts it.
990 Value *Elts[] = {
991 GetTagConstant(DW_TAG_base_type)
992 };
993 MDNode *Node = MDNode::getTemporary(VMContext, Elts, array_lengthof(Elts));
994 return DIType(Node);
995}
996
Devang Patelfe58f952010-12-07 23:25:47 +0000997/// CreateTemporaryType - Create a temporary forward-declared type.
998DIType DIFactory::CreateTemporaryType(DIFile F) {
999 // Give the temporary MDNode a tag. It doesn't matter what tag we
1000 // use here as long as DIType accepts it.
1001 Value *Elts[] = {
1002 GetTagConstant(DW_TAG_base_type),
1003 F.getCompileUnit(),
1004 NULL,
1005 F
1006 };
1007 MDNode *Node = MDNode::getTemporary(VMContext, Elts, array_lengthof(Elts));
1008 return DIType(Node);
1009}
Dan Gohman489b29b2010-08-20 22:02:26 +00001010
Devang Patelac16d442009-10-26 16:54:35 +00001011/// CreateCompositeType - Create a composite type like array, struct, etc.
1012DICompositeType DIFactory::CreateCompositeTypeEx(unsigned Tag,
1013 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +00001014 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +00001015 DIFile F,
Devang Patelac16d442009-10-26 16:54:35 +00001016 unsigned LineNumber,
1017 Constant *SizeInBits,
1018 Constant *AlignInBits,
1019 Constant *OffsetInBits,
1020 unsigned Flags,
1021 DIType DerivedFrom,
1022 DIArray Elements,
Devang Patel6bf058c2010-08-10 20:22:49 +00001023 unsigned RuntimeLang,
1024 MDNode *ContainingType) {
Devang Patelac16d442009-10-26 16:54:35 +00001025 Value *Elts[] = {
1026 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +00001027 Context,
Devang Patelac16d442009-10-26 16:54:35 +00001028 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +00001029 F,
Devang Patelac16d442009-10-26 16:54:35 +00001030 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
1031 SizeInBits,
1032 AlignInBits,
1033 OffsetInBits,
1034 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +00001035 DerivedFrom,
1036 Elements,
Devang Patel6bf058c2010-08-10 20:22:49 +00001037 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
1038 ContainingType
Devang Patelac16d442009-10-26 16:54:35 +00001039 };
Devang Patel6bf058c2010-08-10 20:22:49 +00001040 MDNode *Node = MDNode::get(VMContext, &Elts[0], 13);
Devang Patele7e5a0f2010-08-10 20:01:20 +00001041 // Create a named metadata so that we do not lose this enum info.
1042 if (Tag == dwarf::DW_TAG_enumeration_type) {
1043 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.enum");
1044 NMD->addOperand(Node);
1045 }
1046 return DICompositeType(Node);
Devang Patelac16d442009-10-26 16:54:35 +00001047}
1048
1049
Chris Lattnera45664f2008-11-10 02:56:27 +00001050/// CreateSubprogram - Create a new descriptor for the specified subprogram.
1051/// See comments in DISubprogram for descriptions of these fields. This
1052/// method does not unique the generated descriptors.
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001053DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +00001054 StringRef Name,
1055 StringRef DisplayName,
1056 StringRef LinkageName,
Devang Patel4b945502010-03-09 00:44:10 +00001057 DIFile F,
Devang Patel2e369932010-01-23 00:26:28 +00001058 unsigned LineNo, DIType Ty,
Chris Lattnera45664f2008-11-10 02:56:27 +00001059 bool isLocalToUnit,
Devang Patel5d11eb02009-12-03 19:11:07 +00001060 bool isDefinition,
1061 unsigned VK, unsigned VIndex,
Devang Patel4e0d19d2010-02-03 19:57:19 +00001062 DIType ContainingType,
Devang Patel9dd2b472010-09-29 21:04:46 +00001063 unsigned Flags,
Stuart Hastings215aa152010-06-11 20:08:44 +00001064 bool isOptimized,
1065 Function *Fn) {
Devang Patel854967e2008-12-17 22:39:29 +00001066
Devang Patele4b27562009-08-28 23:24:31 +00001067 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001068 GetTagConstant(dwarf::DW_TAG_subprogram),
Devang Patele4b27562009-08-28 23:24:31 +00001069 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Devang Patel2db49d72010-05-07 18:11:54 +00001070 Context,
Devang Patele4b27562009-08-28 23:24:31 +00001071 MDString::get(VMContext, Name),
1072 MDString::get(VMContext, DisplayName),
1073 MDString::get(VMContext, LinkageName),
Devang Patel2db49d72010-05-07 18:11:54 +00001074 F,
Owen Anderson1d0be152009-08-13 21:58:54 +00001075 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001076 Ty,
Owen Anderson1d0be152009-08-13 21:58:54 +00001077 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
Devang Patel5d11eb02009-12-03 19:11:07 +00001078 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1079 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
1080 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
Devang Patel2db49d72010-05-07 18:11:54 +00001081 ContainingType,
Devang Patel9dd2b472010-09-29 21:04:46 +00001082 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Stuart Hastings215aa152010-06-11 20:08:44 +00001083 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
1084 Fn
Chris Lattnera45664f2008-11-10 02:56:27 +00001085 };
Devang Patelfd5fdc32010-06-28 05:53:08 +00001086 MDNode *Node = MDNode::get(VMContext, &Elts[0], 17);
1087
1088 // Create a named metadata so that we do not lose this mdnode.
1089 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
1090 NMD->addOperand(Node);
1091 return DISubprogram(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +00001092}
1093
Devang Patele3a18de2009-12-01 23:09:02 +00001094/// CreateSubprogramDefinition - Create new subprogram descriptor for the
Jim Grosbache62b6902010-07-21 21:36:25 +00001095/// given declaration.
1096DISubprogram DIFactory::CreateSubprogramDefinition(DISubprogram &SPDeclaration){
Devang Patele3a18de2009-12-01 23:09:02 +00001097 if (SPDeclaration.isDefinition())
Devang Patel2db49d72010-05-07 18:11:54 +00001098 return DISubprogram(SPDeclaration);
Devang Patele3a18de2009-12-01 23:09:02 +00001099
Devang Patel2db49d72010-05-07 18:11:54 +00001100 MDNode *DeclNode = SPDeclaration;
Devang Patele3a18de2009-12-01 23:09:02 +00001101 Value *Elts[] = {
1102 GetTagConstant(dwarf::DW_TAG_subprogram),
1103 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001104 DeclNode->getOperand(2), // Context
1105 DeclNode->getOperand(3), // Name
1106 DeclNode->getOperand(4), // DisplayName
1107 DeclNode->getOperand(5), // LinkageName
1108 DeclNode->getOperand(6), // CompileUnit
1109 DeclNode->getOperand(7), // LineNo
1110 DeclNode->getOperand(8), // Type
1111 DeclNode->getOperand(9), // isLocalToUnit
Stuart Hastings6d56b9f2010-06-05 00:39:29 +00001112 ConstantInt::get(Type::getInt1Ty(VMContext), true),
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001113 DeclNode->getOperand(11), // Virtuality
1114 DeclNode->getOperand(12), // VIndex
Devang Patel4e0d19d2010-02-03 19:57:19 +00001115 DeclNode->getOperand(13), // Containting Type
Devang Patel9dd2b472010-09-29 21:04:46 +00001116 DeclNode->getOperand(14), // Flags
Devang Patelacc6efa2010-06-27 21:04:31 +00001117 DeclNode->getOperand(15), // isOptimized
1118 SPDeclaration.getFunction()
Devang Patele3a18de2009-12-01 23:09:02 +00001119 };
Devang Patelfd5fdc32010-06-28 05:53:08 +00001120 MDNode *Node =MDNode::get(VMContext, &Elts[0], 16);
1121
1122 // Create a named metadata so that we do not lose this mdnode.
1123 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
1124 NMD->addOperand(Node);
1125 return DISubprogram(Node);
Devang Patele3a18de2009-12-01 23:09:02 +00001126}
1127
Chris Lattnera45664f2008-11-10 02:56:27 +00001128/// CreateGlobalVariable - Create a new descriptor for the specified global.
1129DIGlobalVariable
Devang Patel65dbc902009-11-25 17:36:49 +00001130DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
1131 StringRef DisplayName,
1132 StringRef LinkageName,
Devang Patel4b945502010-03-09 00:44:10 +00001133 DIFile F,
Devang Patel2e369932010-01-23 00:26:28 +00001134 unsigned LineNo, DIType Ty,bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +00001135 bool isDefinition, llvm::GlobalVariable *Val) {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001136 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001137 GetTagConstant(dwarf::DW_TAG_variable),
Devang Patele4b27562009-08-28 23:24:31 +00001138 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Devang Patel2db49d72010-05-07 18:11:54 +00001139 Context,
Devang Patele4b27562009-08-28 23:24:31 +00001140 MDString::get(VMContext, Name),
1141 MDString::get(VMContext, DisplayName),
1142 MDString::get(VMContext, LinkageName),
Devang Patel2db49d72010-05-07 18:11:54 +00001143 F,
Owen Anderson1d0be152009-08-13 21:58:54 +00001144 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001145 Ty,
Owen Anderson1d0be152009-08-13 21:58:54 +00001146 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1147 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
Devang Patele4b27562009-08-28 23:24:31 +00001148 Val
Chris Lattnera45664f2008-11-10 02:56:27 +00001149 };
Devang Patele4b27562009-08-28 23:24:31 +00001150
1151 Value *const *Vs = &Elts[0];
1152 MDNode *Node = MDNode::get(VMContext,Vs, 12);
1153
1154 // Create a named metadata so that we do not lose this mdnode.
1155 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001156 NMD->addOperand(Node);
Devang Patele4b27562009-08-28 23:24:31 +00001157
1158 return DIGlobalVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +00001159}
1160
Devang Patel27398962010-08-09 21:39:24 +00001161/// CreateGlobalVariable - Create a new descriptor for the specified constant.
1162DIGlobalVariable
1163DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
1164 StringRef DisplayName,
1165 StringRef LinkageName,
1166 DIFile F,
1167 unsigned LineNo, DIType Ty,bool isLocalToUnit,
1168 bool isDefinition, llvm::Constant *Val) {
1169 Value *Elts[] = {
Devang Patelebd53742010-08-11 23:17:54 +00001170 GetTagConstant(dwarf::DW_TAG_variable),
Devang Patel27398962010-08-09 21:39:24 +00001171 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
1172 Context,
1173 MDString::get(VMContext, Name),
1174 MDString::get(VMContext, DisplayName),
1175 MDString::get(VMContext, LinkageName),
1176 F,
1177 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1178 Ty,
1179 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1180 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1181 Val
1182 };
1183
1184 Value *const *Vs = &Elts[0];
1185 MDNode *Node = MDNode::get(VMContext,Vs, 12);
1186
1187 // Create a named metadata so that we do not lose this mdnode.
1188 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
1189 NMD->addOperand(Node);
1190
1191 return DIGlobalVariable(Node);
1192}
Chris Lattnera45664f2008-11-10 02:56:27 +00001193
Devang Patel62367042010-11-10 22:19:21 +00001194/// fixupObjcLikeName - Replace contains special characters used
1195/// in a typical Objective-C names with '.' in a given string.
1196static void fixupObjcLikeName(std::string &Str) {
1197 for (size_t i = 0, e = Str.size(); i < e; ++i) {
1198 char C = Str[i];
Jakob Stoklund Olesen5b3f7792010-12-03 23:40:45 +00001199 if (C == '[' || C == ']' || C == ' ' || C == ':' || C == '+' ||
1200 C == '(' || C == ')')
Devang Patel62367042010-11-10 22:19:21 +00001201 Str[i] = '.';
1202 }
1203}
1204
1205/// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
1206/// to hold function specific information.
1207NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, StringRef FuncName) {
1208 SmallString<32> Out;
1209 if (FuncName.find('[') == StringRef::npos)
1210 return M.getOrInsertNamedMetadata(Twine("llvm.dbg.lv.", FuncName)
1211 .toStringRef(Out));
1212 std::string Name = FuncName;
1213 fixupObjcLikeName(Name);
1214 return M.getOrInsertNamedMetadata(Twine("llvm.dbg.lv.", Name)
1215 .toStringRef(Out));
1216}
1217
1218/// getFnSpecificMDNode - Return a NameMDNode, if available, that is
1219/// suitable to hold function specific information.
1220NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, StringRef FuncName) {
1221 if (FuncName.find('[') == StringRef::npos)
1222 return M.getNamedMetadata(Twine("llvm.dbg.lv.", FuncName));
1223 std::string Name = FuncName;
1224 fixupObjcLikeName(Name);
1225 return M.getNamedMetadata(Twine("llvm.dbg.lv.", Name));
1226}
1227
Chris Lattnera45664f2008-11-10 02:56:27 +00001228/// CreateVariable - Create a new descriptor for the specified variable.
1229DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +00001230 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +00001231 DIFile F,
1232 unsigned LineNo,
Devang Patel3cf763d2010-09-29 23:07:21 +00001233 DIType Ty, bool AlwaysPreserve,
1234 unsigned Flags) {
Devang Patele4b27562009-08-28 23:24:31 +00001235 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001236 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +00001237 Context,
Devang Patele4b27562009-08-28 23:24:31 +00001238 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +00001239 F,
Owen Anderson1d0be152009-08-13 21:58:54 +00001240 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001241 Ty,
Devang Patel3cf763d2010-09-29 23:07:21 +00001242 ConstantInt::get(Type::getInt32Ty(VMContext), Flags)
Chris Lattnera45664f2008-11-10 02:56:27 +00001243 };
Devang Patel3cf763d2010-09-29 23:07:21 +00001244 MDNode *Node = MDNode::get(VMContext, &Elts[0], 7);
Devang Patel6ed0ce32010-05-20 20:35:24 +00001245 if (AlwaysPreserve) {
1246 // The optimizer may remove local variable. If there is an interest
1247 // to preserve variable info in such situation then stash it in a
1248 // named mdnode.
Devang Patel2f7d5292010-06-16 00:53:55 +00001249 DISubprogram Fn(getDISubprogram(Context));
Devang Patel10de3bb2010-06-21 18:36:58 +00001250 StringRef FName = "fn";
1251 if (Fn.getFunction())
1252 FName = Fn.getFunction()->getName();
Devang Patel10de3bb2010-06-21 18:36:58 +00001253 char One = '\1';
1254 if (FName.startswith(StringRef(&One, 1)))
1255 FName = FName.substr(1);
Dan Gohman17aa92c2010-07-21 23:38:33 +00001256
Devang Patel62367042010-11-10 22:19:21 +00001257
1258 NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, FName);
Devang Patel2f7d5292010-06-16 00:53:55 +00001259 FnLocals->addOperand(Node);
Devang Patel98e1cac2010-05-14 21:01:35 +00001260 }
1261 return DIVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +00001262}
1263
1264
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001265/// CreateComplexVariable - Create a new descriptor for the specified variable
1266/// which has a complex address expression for its address.
1267DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
Benjamin Kramer28b4afc2010-09-21 16:00:03 +00001268 StringRef Name, DIFile F,
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001269 unsigned LineNo,
Benjamin Kramer28b4afc2010-09-21 16:00:03 +00001270 DIType Ty, Value *const *Addr,
1271 unsigned NumAddr) {
1272 SmallVector<Value *, 15> Elts;
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001273 Elts.push_back(GetTagConstant(Tag));
Devang Patel2db49d72010-05-07 18:11:54 +00001274 Elts.push_back(Context);
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001275 Elts.push_back(MDString::get(VMContext, Name));
Devang Patel2db49d72010-05-07 18:11:54 +00001276 Elts.push_back(F);
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001277 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
Devang Patel2db49d72010-05-07 18:11:54 +00001278 Elts.push_back(Ty);
Benjamin Kramer28b4afc2010-09-21 16:00:03 +00001279 Elts.append(Addr, Addr+NumAddr);
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001280
Benjamin Kramer28b4afc2010-09-21 16:00:03 +00001281 return DIVariable(MDNode::get(VMContext, Elts.data(), Elts.size()));
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001282}
1283
1284
Chris Lattnera45664f2008-11-10 02:56:27 +00001285/// CreateBlock - This creates a descriptor for a lexical block with the
Owen Anderson99035272009-07-07 17:12:53 +00001286/// specified parent VMContext.
Devang Patel3d821aa2010-02-16 21:39:34 +00001287DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context,
Stuart Hastings0db42712010-07-19 23:56:30 +00001288 DIFile F, unsigned LineNo,
1289 unsigned Col) {
1290 // Defeat MDNode uniqing for lexical blocks.
1291 static unsigned int unique_id = 0;
Devang Patele4b27562009-08-28 23:24:31 +00001292 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001293 GetTagConstant(dwarf::DW_TAG_lexical_block),
Devang Patel2db49d72010-05-07 18:11:54 +00001294 Context,
Devang Patel3d821aa2010-02-16 21:39:34 +00001295 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Stuart Hastings0db42712010-07-19 23:56:30 +00001296 ConstantInt::get(Type::getInt32Ty(VMContext), Col),
1297 F,
1298 ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
Chris Lattnera45664f2008-11-10 02:56:27 +00001299 };
Stuart Hastings0db42712010-07-19 23:56:30 +00001300 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 6));
Chris Lattnera45664f2008-11-10 02:56:27 +00001301}
1302
Devang Patel6404e4e2009-12-15 19:16:48 +00001303/// CreateNameSpace - This creates new descriptor for a namespace
1304/// with the specified parent context.
1305DINameSpace DIFactory::CreateNameSpace(DIDescriptor Context, StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +00001306 DIFile F,
Devang Patel6404e4e2009-12-15 19:16:48 +00001307 unsigned LineNo) {
1308 Value *Elts[] = {
1309 GetTagConstant(dwarf::DW_TAG_namespace),
Devang Patel2db49d72010-05-07 18:11:54 +00001310 Context,
Devang Patel6404e4e2009-12-15 19:16:48 +00001311 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +00001312 F,
Devang Patel6404e4e2009-12-15 19:16:48 +00001313 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1314 };
1315 return DINameSpace(MDNode::get(VMContext, &Elts[0], 5));
1316}
1317
Devang Patelf98d8fe2009-09-01 01:14:15 +00001318/// CreateLocation - Creates a debug info location.
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001319DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
Daniel Dunbara279bc32009-09-20 02:20:51 +00001320 DIScope S, DILocation OrigLoc) {
Devang Patelf98d8fe2009-09-01 01:14:15 +00001321 Value *Elts[] = {
1322 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1323 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001324 S,
1325 OrigLoc,
Devang Patelf98d8fe2009-09-01 01:14:15 +00001326 };
1327 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1328}
1329
Chris Lattnera45664f2008-11-10 02:56:27 +00001330//===----------------------------------------------------------------------===//
1331// DIFactory: Routines for inserting code into a function
1332//===----------------------------------------------------------------------===//
1333
Chris Lattnera45664f2008-11-10 02:56:27 +00001334/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Devang Patel6daf99b2009-11-10 22:05:35 +00001335Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001336 Instruction *InsertBefore) {
Victor Hernandez4cf292a2010-01-26 02:07:38 +00001337 assert(Storage && "no storage passed to dbg.declare");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001338 assert(D.Verify() && "empty DIVariable passed to dbg.declare");
Chris Lattnera45664f2008-11-10 02:56:27 +00001339 if (!DeclareFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +00001340 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1341
Victor Hernandez756462b2010-01-18 20:42:09 +00001342 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
Devang Patel2db49d72010-05-07 18:11:54 +00001343 D };
Devang Patel6daf99b2009-11-10 22:05:35 +00001344 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
Mike Stumpe4250392009-10-01 22:08:58 +00001345}
1346
1347/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Devang Patel6daf99b2009-11-10 22:05:35 +00001348Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001349 BasicBlock *InsertAtEnd) {
Victor Hernandez4cf292a2010-01-26 02:07:38 +00001350 assert(Storage && "no storage passed to dbg.declare");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001351 assert(D.Verify() && "invalid DIVariable passed to dbg.declare");
Mike Stumpe4250392009-10-01 22:08:58 +00001352 if (!DeclareFn)
1353 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1354
Victor Hernandez756462b2010-01-18 20:42:09 +00001355 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
Devang Patel2db49d72010-05-07 18:11:54 +00001356 D };
Devang Patel27a53de2010-01-29 18:30:57 +00001357
1358 // If this block already has a terminator then insert this intrinsic
1359 // before the terminator.
Jim Grosbache62b6902010-07-21 21:36:25 +00001360 if (TerminatorInst *T = InsertAtEnd->getTerminator())
Devang Patel27a53de2010-01-29 18:30:57 +00001361 return CallInst::Create(DeclareFn, Args, Args+2, "", T);
1362 else
1363 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);}
Torok Edwin620f2802008-12-16 09:07:36 +00001364
Victor Hernandezc59b3352009-12-07 21:54:43 +00001365/// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001366Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
Victor Hernandezc59b3352009-12-07 21:54:43 +00001367 DIVariable D,
1368 Instruction *InsertBefore) {
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001369 assert(V && "no value passed to dbg.value");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001370 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001371 if (!ValueFn)
1372 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1373
Victor Hernandezc8b7cd02010-01-20 05:44:11 +00001374 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001375 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
Devang Patel2db49d72010-05-07 18:11:54 +00001376 D };
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001377 return CallInst::Create(ValueFn, Args, Args+3, "", InsertBefore);
1378}
1379
Victor Hernandezc59b3352009-12-07 21:54:43 +00001380/// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001381Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
Victor Hernandezc59b3352009-12-07 21:54:43 +00001382 DIVariable D,
1383 BasicBlock *InsertAtEnd) {
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001384 assert(V && "no value passed to dbg.value");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001385 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001386 if (!ValueFn)
1387 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1388
Jim Grosbache62b6902010-07-21 21:36:25 +00001389 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001390 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
Devang Patel2db49d72010-05-07 18:11:54 +00001391 D };
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001392 return CallInst::Create(ValueFn, Args, Args+3, "", InsertAtEnd);
1393}
Devang Patele4b27562009-08-28 23:24:31 +00001394
Devang Patel1a7ca032010-09-28 18:08:20 +00001395// RecordType - Record DIType in a module such that it is not lost even if
1396// it is not referenced through debug info anchors.
1397void DIFactory::RecordType(DIType T) {
1398 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.ty");
1399 NMD->addOperand(T);
1400}
1401
1402
Devang Pateld2f79a12009-07-28 19:55:13 +00001403//===----------------------------------------------------------------------===//
Devang Patel98c65172009-07-30 18:25:15 +00001404// DebugInfoFinder implementations.
Devang Pateld2f79a12009-07-28 19:55:13 +00001405//===----------------------------------------------------------------------===//
1406
Devang Patel98c65172009-07-30 18:25:15 +00001407/// processModule - Process entire module and collect debug info.
1408void DebugInfoFinder::processModule(Module &M) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001409 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1410 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
1411 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
1412 ++BI) {
Devang Patel01c5ff62010-05-04 01:05:02 +00001413 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
Devang Patelb4d31302009-07-31 18:18:52 +00001414 processDeclare(DDI);
Jim Grosbache62b6902010-07-21 21:36:25 +00001415
Chris Lattner28a9bf62010-04-02 20:44:29 +00001416 DebugLoc Loc = BI->getDebugLoc();
1417 if (Loc.isUnknown())
1418 continue;
Jim Grosbache62b6902010-07-21 21:36:25 +00001419
Chris Lattner28a9bf62010-04-02 20:44:29 +00001420 LLVMContext &Ctx = BI->getContext();
1421 DIDescriptor Scope(Loc.getScope(Ctx));
Jim Grosbache62b6902010-07-21 21:36:25 +00001422
Chris Lattner28a9bf62010-04-02 20:44:29 +00001423 if (Scope.isCompileUnit())
Devang Patel2db49d72010-05-07 18:11:54 +00001424 addCompileUnit(DICompileUnit(Scope));
Chris Lattner28a9bf62010-04-02 20:44:29 +00001425 else if (Scope.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001426 processSubprogram(DISubprogram(Scope));
Chris Lattner28a9bf62010-04-02 20:44:29 +00001427 else if (Scope.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001428 processLexicalBlock(DILexicalBlock(Scope));
Jim Grosbache62b6902010-07-21 21:36:25 +00001429
Chris Lattner28a9bf62010-04-02 20:44:29 +00001430 if (MDNode *IA = Loc.getInlinedAt(Ctx))
1431 processLocation(DILocation(IA));
Devang Pateld2f79a12009-07-28 19:55:13 +00001432 }
Devang Patele4b27562009-08-28 23:24:31 +00001433
Devang Patelfd5fdc32010-06-28 05:53:08 +00001434 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
1435 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1436 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
1437 if (addGlobalVariable(DIG)) {
1438 addCompileUnit(DIG.getCompileUnit());
1439 processType(DIG.getType());
1440 }
Devang Pateld2f79a12009-07-28 19:55:13 +00001441 }
1442 }
Devang Patelfd5fdc32010-06-28 05:53:08 +00001443
1444 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
1445 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
1446 processSubprogram(DISubprogram(NMD->getOperand(i)));
Devang Pateld2f79a12009-07-28 19:55:13 +00001447}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001448
Devang Patel6daf99b2009-11-10 22:05:35 +00001449/// processLocation - Process DILocation.
1450void DebugInfoFinder::processLocation(DILocation Loc) {
Devang Patel3c91b052010-03-08 20:52:55 +00001451 if (!Loc.Verify()) return;
Devang Patel2db49d72010-05-07 18:11:54 +00001452 DIDescriptor S(Loc.getScope());
Devang Patel6daf99b2009-11-10 22:05:35 +00001453 if (S.isCompileUnit())
Devang Patel2db49d72010-05-07 18:11:54 +00001454 addCompileUnit(DICompileUnit(S));
Devang Patel6daf99b2009-11-10 22:05:35 +00001455 else if (S.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001456 processSubprogram(DISubprogram(S));
Devang Patel6daf99b2009-11-10 22:05:35 +00001457 else if (S.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001458 processLexicalBlock(DILexicalBlock(S));
Devang Patel6daf99b2009-11-10 22:05:35 +00001459 processLocation(Loc.getOrigLocation());
1460}
1461
Devang Patel98c65172009-07-30 18:25:15 +00001462/// processType - Process DIType.
1463void DebugInfoFinder::processType(DIType DT) {
Devang Patel72bcdb62009-08-10 22:09:58 +00001464 if (!addType(DT))
Devang Pateld2f79a12009-07-28 19:55:13 +00001465 return;
1466
1467 addCompileUnit(DT.getCompileUnit());
Devang Patel6ceea332009-08-31 18:49:10 +00001468 if (DT.isCompositeType()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001469 DICompositeType DCT(DT);
Devang Patel98c65172009-07-30 18:25:15 +00001470 processType(DCT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001471 DIArray DA = DCT.getTypeArray();
Devang Patel3c91b052010-03-08 20:52:55 +00001472 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1473 DIDescriptor D = DA.getElement(i);
1474 if (D.isType())
Devang Patel2db49d72010-05-07 18:11:54 +00001475 processType(DIType(D));
Devang Patel3c91b052010-03-08 20:52:55 +00001476 else if (D.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001477 processSubprogram(DISubprogram(D));
Devang Patel3c91b052010-03-08 20:52:55 +00001478 }
Devang Patel6ceea332009-08-31 18:49:10 +00001479 } else if (DT.isDerivedType()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001480 DIDerivedType DDT(DT);
Devang Patel3c91b052010-03-08 20:52:55 +00001481 processType(DDT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001482 }
1483}
1484
Devang Patelbeab41b2009-10-07 22:04:08 +00001485/// processLexicalBlock
1486void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
Devang Patelbeab41b2009-10-07 22:04:08 +00001487 DIScope Context = LB.getContext();
1488 if (Context.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001489 return processLexicalBlock(DILexicalBlock(Context));
Devang Patelbeab41b2009-10-07 22:04:08 +00001490 else
Devang Patel2db49d72010-05-07 18:11:54 +00001491 return processSubprogram(DISubprogram(Context));
Devang Patelbeab41b2009-10-07 22:04:08 +00001492}
1493
Devang Patel98c65172009-07-30 18:25:15 +00001494/// processSubprogram - Process DISubprogram.
1495void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001496 if (!addSubprogram(SP))
1497 return;
1498 addCompileUnit(SP.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +00001499 processType(SP.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +00001500}
1501
Devang Patelb4d31302009-07-31 18:18:52 +00001502/// processDeclare - Process DbgDeclareInst.
1503void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
Devang Patel3c91b052010-03-08 20:52:55 +00001504 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1505 if (!N) return;
1506
1507 DIDescriptor DV(N);
1508 if (!DV.isVariable())
Devang Patelb4d31302009-07-31 18:18:52 +00001509 return;
1510
Devang Patel2db49d72010-05-07 18:11:54 +00001511 if (!NodesSeen.insert(DV))
Devang Patelb4d31302009-07-31 18:18:52 +00001512 return;
1513
Devang Patel3c91b052010-03-08 20:52:55 +00001514 addCompileUnit(DIVariable(N).getCompileUnit());
1515 processType(DIVariable(N).getType());
Devang Patelb4d31302009-07-31 18:18:52 +00001516}
1517
Devang Patel72bcdb62009-08-10 22:09:58 +00001518/// addType - Add type into Tys.
1519bool DebugInfoFinder::addType(DIType DT) {
Devang Patel3c91b052010-03-08 20:52:55 +00001520 if (!DT.isValid())
Devang Patel72bcdb62009-08-10 22:09:58 +00001521 return false;
1522
Devang Patel2db49d72010-05-07 18:11:54 +00001523 if (!NodesSeen.insert(DT))
Devang Patel72bcdb62009-08-10 22:09:58 +00001524 return false;
1525
Devang Patel2db49d72010-05-07 18:11:54 +00001526 TYs.push_back(DT);
Devang Patel72bcdb62009-08-10 22:09:58 +00001527 return true;
1528}
1529
Devang Pateld2f79a12009-07-28 19:55:13 +00001530/// addCompileUnit - Add compile unit into CUs.
Devang Patel98c65172009-07-30 18:25:15 +00001531bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Patel3c91b052010-03-08 20:52:55 +00001532 if (!CU.Verify())
Devang Pateld2f79a12009-07-28 19:55:13 +00001533 return false;
1534
Devang Patel2db49d72010-05-07 18:11:54 +00001535 if (!NodesSeen.insert(CU))
Devang Pateld2f79a12009-07-28 19:55:13 +00001536 return false;
1537
Devang Patel2db49d72010-05-07 18:11:54 +00001538 CUs.push_back(CU);
Devang Pateld2f79a12009-07-28 19:55:13 +00001539 return true;
1540}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001541
Devang Pateld2f79a12009-07-28 19:55:13 +00001542/// addGlobalVariable - Add global variable into GVs.
Devang Patel98c65172009-07-30 18:25:15 +00001543bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Patel2db49d72010-05-07 18:11:54 +00001544 if (!DIDescriptor(DIG).isGlobalVariable())
Devang Pateld2f79a12009-07-28 19:55:13 +00001545 return false;
1546
Devang Patel2db49d72010-05-07 18:11:54 +00001547 if (!NodesSeen.insert(DIG))
Devang Pateld2f79a12009-07-28 19:55:13 +00001548 return false;
1549
Devang Patel2db49d72010-05-07 18:11:54 +00001550 GVs.push_back(DIG);
Devang Pateld2f79a12009-07-28 19:55:13 +00001551 return true;
1552}
1553
1554// addSubprogram - Add subprgoram into SPs.
Devang Patel98c65172009-07-30 18:25:15 +00001555bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Patel2db49d72010-05-07 18:11:54 +00001556 if (!DIDescriptor(SP).isSubprogram())
Devang Pateld2f79a12009-07-28 19:55:13 +00001557 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001558
Devang Patel2db49d72010-05-07 18:11:54 +00001559 if (!NodesSeen.insert(SP))
Devang Pateld2f79a12009-07-28 19:55:13 +00001560 return false;
1561
Devang Patel2db49d72010-05-07 18:11:54 +00001562 SPs.push_back(SP);
Devang Pateld2f79a12009-07-28 19:55:13 +00001563 return true;
1564}
1565
Victor Hernandez756462b2010-01-18 20:42:09 +00001566/// Find the debug info descriptor corresponding to this global variable.
1567static Value *findDbgGlobalDeclare(GlobalVariable *V) {
Chris Lattner099b7792009-12-29 09:22:47 +00001568 const Module *M = V->getParent();
1569 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1570 if (!NMD)
Torok Edwinff7d0e92009-03-10 13:41:26 +00001571 return 0;
Chris Lattner099b7792009-12-29 09:22:47 +00001572
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001573 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman872814a2010-07-21 18:54:18 +00001574 DIDescriptor DIG(cast<MDNode>(NMD->getOperand(i)));
Devang Patel3c91b052010-03-08 20:52:55 +00001575 if (!DIG.isGlobalVariable())
Chris Lattner099b7792009-12-29 09:22:47 +00001576 continue;
Devang Patel2db49d72010-05-07 18:11:54 +00001577 if (DIGlobalVariable(DIG).getGlobal() == V)
1578 return DIG;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001579 }
Chris Lattner099b7792009-12-29 09:22:47 +00001580 return 0;
1581}
Torok Edwinff7d0e92009-03-10 13:41:26 +00001582
Chris Lattner099b7792009-12-29 09:22:47 +00001583/// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
1584/// It looks through pointer casts too.
Victor Hernandez756462b2010-01-18 20:42:09 +00001585static const DbgDeclareInst *findDbgDeclare(const Value *V) {
Victor Hernandez3a328652010-01-15 19:04:09 +00001586 V = V->stripPointerCasts();
Jim Grosbache62b6902010-07-21 21:36:25 +00001587
Victor Hernandez3a328652010-01-15 19:04:09 +00001588 if (!isa<Instruction>(V) && !isa<Argument>(V))
Torok Edwin620f2802008-12-16 09:07:36 +00001589 return 0;
Jim Grosbache62b6902010-07-21 21:36:25 +00001590
Victor Hernandez3a328652010-01-15 19:04:09 +00001591 const Function *F = NULL;
1592 if (const Instruction *I = dyn_cast<Instruction>(V))
1593 F = I->getParent()->getParent();
1594 else if (const Argument *A = dyn_cast<Argument>(V))
1595 F = A->getParent();
Jim Grosbache62b6902010-07-21 21:36:25 +00001596
Victor Hernandez3a328652010-01-15 19:04:09 +00001597 for (Function::const_iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
1598 for (BasicBlock::const_iterator BI = (*FI).begin(), BE = (*FI).end();
1599 BI != BE; ++BI)
1600 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1601 if (DDI->getAddress() == V)
1602 return DDI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001603
Chris Lattner099b7792009-12-29 09:22:47 +00001604 return 0;
1605}
Bill Wendlingdc817b62009-05-14 18:26:15 +00001606
Chris Lattner099b7792009-12-29 09:22:47 +00001607bool llvm::getLocationInfo(const Value *V, std::string &DisplayName,
1608 std::string &Type, unsigned &LineNo,
1609 std::string &File, std::string &Dir) {
1610 DICompileUnit Unit;
1611 DIType TypeD;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001612
Chris Lattner099b7792009-12-29 09:22:47 +00001613 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1614 Value *DIGV = findDbgGlobalDeclare(GV);
1615 if (!DIGV) return false;
1616 DIGlobalVariable Var(cast<MDNode>(DIGV));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001617
Chris Lattner099b7792009-12-29 09:22:47 +00001618 StringRef D = Var.getDisplayName();
Devang Patel65dbc902009-11-25 17:36:49 +00001619 if (!D.empty())
Chris Lattner099b7792009-12-29 09:22:47 +00001620 DisplayName = D;
1621 LineNo = Var.getLineNumber();
1622 Unit = Var.getCompileUnit();
1623 TypeD = Var.getType();
1624 } else {
1625 const DbgDeclareInst *DDI = findDbgDeclare(V);
1626 if (!DDI) return false;
1627 DIVariable Var(cast<MDNode>(DDI->getVariable()));
1628
1629 StringRef D = Var.getName();
1630 if (!D.empty())
1631 DisplayName = D;
1632 LineNo = Var.getLineNumber();
1633 Unit = Var.getCompileUnit();
1634 TypeD = Var.getType();
Torok Edwinff7d0e92009-03-10 13:41:26 +00001635 }
Devang Patel13e16b62009-06-26 01:49:18 +00001636
Chris Lattner099b7792009-12-29 09:22:47 +00001637 StringRef T = TypeD.getName();
1638 if (!T.empty())
1639 Type = T;
1640 StringRef F = Unit.getFilename();
1641 if (!F.empty())
1642 File = F;
1643 StringRef D = Unit.getDirectory();
1644 if (!D.empty())
1645 Dir = D;
1646 return true;
1647}
Devang Patel9e529c32009-07-02 01:15:24 +00001648
Chris Lattner099b7792009-12-29 09:22:47 +00001649/// getDISubprogram - Find subprogram that is enclosing this scope.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001650DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
Chris Lattner099b7792009-12-29 09:22:47 +00001651 DIDescriptor D(Scope);
Chris Lattner099b7792009-12-29 09:22:47 +00001652 if (D.isSubprogram())
1653 return DISubprogram(Scope);
Jim Grosbache62b6902010-07-21 21:36:25 +00001654
Chris Lattner099b7792009-12-29 09:22:47 +00001655 if (D.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001656 return getDISubprogram(DILexicalBlock(Scope).getContext());
Jim Grosbache62b6902010-07-21 21:36:25 +00001657
Chris Lattner099b7792009-12-29 09:22:47 +00001658 return DISubprogram();
1659}
Devang Patel193f7202009-11-24 01:14:22 +00001660
Chris Lattner099b7792009-12-29 09:22:47 +00001661/// getDICompositeType - Find underlying composite type.
1662DICompositeType llvm::getDICompositeType(DIType T) {
Chris Lattner099b7792009-12-29 09:22:47 +00001663 if (T.isCompositeType())
Devang Patel2db49d72010-05-07 18:11:54 +00001664 return DICompositeType(T);
Jim Grosbache62b6902010-07-21 21:36:25 +00001665
Chris Lattner099b7792009-12-29 09:22:47 +00001666 if (T.isDerivedType())
Devang Patel2db49d72010-05-07 18:11:54 +00001667 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
Jim Grosbache62b6902010-07-21 21:36:25 +00001668
Chris Lattner099b7792009-12-29 09:22:47 +00001669 return DICompositeType();
Torok Edwin620f2802008-12-16 09:07:36 +00001670}