blob: 3dd659833e0fbafc4ab44b485cd1dcbf4d46a394 [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;
308
309 DICompileUnit CU = getCompileUnit();
Devang Patel3c91b052010-03-08 20:52:55 +0000310 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000311 return false;
312 return true;
313}
314
Devang Patel0c4720c2010-08-23 18:25:56 +0000315/// Verify - Verify that a basic type descriptor is well formed.
316bool DIBasicType::Verify() const {
317 return isBasicType();
318}
319
320/// Verify - Verify that a derived type descriptor is well formed.
321bool DIDerivedType::Verify() const {
322 return isDerivedType();
323}
324
Devang Patelb79b5352009-01-19 23:21:49 +0000325/// Verify - Verify that a composite type descriptor is well formed.
326bool DICompositeType::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000327 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000328 return false;
Devang Patel3c91b052010-03-08 20:52:55 +0000329 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000330 return false;
331
332 DICompileUnit CU = getCompileUnit();
Devang Patel3c91b052010-03-08 20:52:55 +0000333 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000334 return false;
335 return true;
336}
337
338/// Verify - Verify that a subprogram descriptor is well formed.
339bool DISubprogram::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000340 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000341 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000342
Devang Patel3c91b052010-03-08 20:52:55 +0000343 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000344 return false;
345
346 DICompileUnit CU = getCompileUnit();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000347 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000348 return false;
349
350 DICompositeType Ty = getType();
Devang Patel3c91b052010-03-08 20:52:55 +0000351 if (!Ty.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000352 return false;
353 return true;
354}
355
356/// Verify - Verify that a global variable descriptor is well formed.
357bool DIGlobalVariable::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000358 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000359 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000360
Devang Patel65dbc902009-11-25 17:36:49 +0000361 if (getDisplayName().empty())
Devang Patel84c73e92009-11-06 17:58:12 +0000362 return false;
363
Devang Patel3c91b052010-03-08 20:52:55 +0000364 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000365 return false;
366
367 DICompileUnit CU = getCompileUnit();
Devang Patel3c91b052010-03-08 20:52:55 +0000368 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000369 return false;
370
371 DIType Ty = getType();
372 if (!Ty.Verify())
373 return false;
374
Devang Patel27398962010-08-09 21:39:24 +0000375 if (!getGlobal() && !getConstant())
Devang Patelb79b5352009-01-19 23:21:49 +0000376 return false;
377
378 return true;
379}
380
381/// Verify - Verify that a variable descriptor is well formed.
382bool DIVariable::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000383 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000384 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000385
Devang Patel3c91b052010-03-08 20:52:55 +0000386 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000387 return false;
388
Devang Patel62077af2010-05-07 21:42:24 +0000389 if (!getCompileUnit().Verify())
390 return false;
391
Devang Patelb79b5352009-01-19 23:21:49 +0000392 DIType Ty = getType();
393 if (!Ty.Verify())
394 return false;
395
Devang Patelb79b5352009-01-19 23:21:49 +0000396 return true;
397}
398
Devang Patel3c91b052010-03-08 20:52:55 +0000399/// Verify - Verify that a location descriptor is well formed.
400bool DILocation::Verify() const {
401 if (!DbgNode)
402 return false;
Jim Grosbache62b6902010-07-21 21:36:25 +0000403
Devang Patel3c91b052010-03-08 20:52:55 +0000404 return DbgNode->getNumOperands() == 4;
405}
406
Devang Patel47e22652010-05-07 23:04:32 +0000407/// Verify - Verify that a namespace descriptor is well formed.
408bool DINameSpace::Verify() const {
409 if (!DbgNode)
410 return false;
411 if (getName().empty())
412 return false;
413 if (!getCompileUnit().Verify())
414 return false;
415 return true;
416}
417
Devang Patel36375ee2009-02-17 21:23:59 +0000418/// getOriginalTypeSize - If this type is derived from a base type then
419/// return base type size.
420uint64_t DIDerivedType::getOriginalTypeSize() const {
Devang Patel61ecbd12009-11-04 23:48:00 +0000421 unsigned Tag = getTag();
422 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
423 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
424 Tag == dwarf::DW_TAG_restrict_type) {
425 DIType BaseType = getTypeDerivedFrom();
Jim Grosbache62b6902010-07-21 21:36:25 +0000426 // If this type is not derived from any type then take conservative
Devang Patel5ebfa2d2009-11-06 18:24:05 +0000427 // approach.
Devang Patel3c91b052010-03-08 20:52:55 +0000428 if (!BaseType.isValid())
Devang Patel5ebfa2d2009-11-06 18:24:05 +0000429 return getSizeInBits();
Devang Patel61ecbd12009-11-04 23:48:00 +0000430 if (BaseType.isDerivedType())
Devang Patel2db49d72010-05-07 18:11:54 +0000431 return DIDerivedType(BaseType).getOriginalTypeSize();
Devang Patel61ecbd12009-11-04 23:48:00 +0000432 else
433 return BaseType.getSizeInBits();
434 }
Jim Grosbache62b6902010-07-21 21:36:25 +0000435
Devang Patel61ecbd12009-11-04 23:48:00 +0000436 return getSizeInBits();
Devang Patel36375ee2009-02-17 21:23:59 +0000437}
Devang Patelb79b5352009-01-19 23:21:49 +0000438
Jim Grosbache62b6902010-07-21 21:36:25 +0000439/// isInlinedFnArgument - Return true if this variable provides debugging
Devang Patel719f6a92010-04-29 20:40:36 +0000440/// information for an inlined function arguments.
441bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
442 assert(CurFn && "Invalid function");
443 if (!getContext().isSubprogram())
444 return false;
Jim Grosbache62b6902010-07-21 21:36:25 +0000445 // This variable is not inlined function argument if its scope
Devang Patel719f6a92010-04-29 20:40:36 +0000446 // does not describe current function.
Devang Patel2db49d72010-05-07 18:11:54 +0000447 return !(DISubprogram(getContext()).describes(CurFn));
Devang Patel719f6a92010-04-29 20:40:36 +0000448}
449
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000450/// describes - Return true if this subprogram provides debugging
451/// information for the function F.
452bool DISubprogram::describes(const Function *F) {
Chris Lattner099b7792009-12-29 09:22:47 +0000453 assert(F && "Invalid function");
Devang Patelffd33cd2010-06-16 06:42:02 +0000454 if (F == getFunction())
455 return true;
Devang Patel65dbc902009-11-25 17:36:49 +0000456 StringRef Name = getLinkageName();
457 if (Name.empty())
Devang Patel5ccdd102009-09-29 18:40:58 +0000458 Name = getName();
Devang Patel65dbc902009-11-25 17:36:49 +0000459 if (F->getName() == Name)
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000460 return true;
461 return false;
462}
463
Jim Grosbache62b6902010-07-21 21:36:25 +0000464unsigned DISubprogram::isOptimized() const {
Devang Patelccff8122010-04-30 19:38:23 +0000465 assert (DbgNode && "Invalid subprogram descriptor!");
466 if (DbgNode->getNumOperands() == 16)
467 return getUnsignedField(15);
468 return 0;
469}
470
Devang Patel65dbc902009-11-25 17:36:49 +0000471StringRef DIScope::getFilename() const {
Devang Patel77bf2952010-03-08 22:02:50 +0000472 if (!DbgNode)
473 return StringRef();
Jim Grosbache62b6902010-07-21 21:36:25 +0000474 if (isLexicalBlock())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000475 return DILexicalBlock(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000476 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000477 return DISubprogram(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000478 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000479 return DICompileUnit(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000480 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000481 return DINameSpace(DbgNode).getFilename();
Devang Patel77bf2952010-03-08 22:02:50 +0000482 if (isType())
483 return DIType(DbgNode).getFilename();
Devang Patel7aa81892010-03-08 22:27:22 +0000484 if (isFile())
485 return DIFile(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000486 assert(0 && "Invalid DIScope!");
Devang Patel65dbc902009-11-25 17:36:49 +0000487 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000488}
489
Devang Patel65dbc902009-11-25 17:36:49 +0000490StringRef DIScope::getDirectory() const {
Devang Patel77bf2952010-03-08 22:02:50 +0000491 if (!DbgNode)
492 return StringRef();
Jim Grosbache62b6902010-07-21 21:36:25 +0000493 if (isLexicalBlock())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000494 return DILexicalBlock(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000495 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000496 return DISubprogram(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000497 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000498 return DICompileUnit(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000499 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000500 return DINameSpace(DbgNode).getDirectory();
Devang Patel77bf2952010-03-08 22:02:50 +0000501 if (isType())
502 return DIType(DbgNode).getDirectory();
Devang Patel7aa81892010-03-08 22:27:22 +0000503 if (isFile())
504 return DIFile(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000505 assert(0 && "Invalid DIScope!");
Devang Patel65dbc902009-11-25 17:36:49 +0000506 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000507}
508
Chris Lattnera45664f2008-11-10 02:56:27 +0000509//===----------------------------------------------------------------------===//
Devang Patel7136a652009-07-01 22:10:23 +0000510// DIDescriptor: dump routines for all descriptors.
511//===----------------------------------------------------------------------===//
512
513
Dan Gohman50404362010-05-07 15:30:29 +0000514/// print - Print descriptor.
515void DIDescriptor::print(raw_ostream &OS) const {
516 OS << "[" << dwarf::TagString(getTag()) << "] ";
517 OS.write_hex((intptr_t) &*DbgNode) << ']';
Devang Patel7136a652009-07-01 22:10:23 +0000518}
519
Dan Gohman50404362010-05-07 15:30:29 +0000520/// print - Print compile unit.
521void DICompileUnit::print(raw_ostream &OS) const {
Devang Patel7136a652009-07-01 22:10:23 +0000522 if (getLanguage())
Dan Gohman50404362010-05-07 15:30:29 +0000523 OS << " [" << dwarf::LanguageString(getLanguage()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000524
Dan Gohmanfaa19c32010-05-10 20:07:44 +0000525 OS << " [" << getDirectory() << "/" << getFilename() << "]";
Devang Patel7136a652009-07-01 22:10:23 +0000526}
527
Dan Gohman50404362010-05-07 15:30:29 +0000528/// print - Print type.
529void DIType::print(raw_ostream &OS) const {
Devang Patel3c91b052010-03-08 20:52:55 +0000530 if (!DbgNode) return;
Devang Patel7136a652009-07-01 22:10:23 +0000531
Devang Patel65dbc902009-11-25 17:36:49 +0000532 StringRef Res = getName();
533 if (!Res.empty())
Dan Gohman50404362010-05-07 15:30:29 +0000534 OS << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000535
536 unsigned Tag = getTag();
Dan Gohman50404362010-05-07 15:30:29 +0000537 OS << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000538
539 // TODO : Print context
Dan Gohmanc014d092010-05-07 16:17:22 +0000540 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000541 OS << " ["
Dan Gohman9a7063e2010-05-07 16:39:27 +0000542 << "line " << getLineNumber() << ", "
543 << getSizeInBits() << " bits, "
544 << getAlignInBits() << " bit alignment, "
545 << getOffsetInBits() << " bit offset"
Chris Lattnera81d29b2009-08-23 07:33:14 +0000546 << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000547
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000548 if (isPrivate())
Dan Gohman50404362010-05-07 15:30:29 +0000549 OS << " [private] ";
Devang Patel7136a652009-07-01 22:10:23 +0000550 else if (isProtected())
Dan Gohman50404362010-05-07 15:30:29 +0000551 OS << " [protected] ";
Devang Patel7136a652009-07-01 22:10:23 +0000552
553 if (isForwardDecl())
Dan Gohman50404362010-05-07 15:30:29 +0000554 OS << " [fwd] ";
Devang Patel7136a652009-07-01 22:10:23 +0000555
Devang Patel6ceea332009-08-31 18:49:10 +0000556 if (isBasicType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000557 DIBasicType(DbgNode).print(OS);
Devang Patel6ceea332009-08-31 18:49:10 +0000558 else if (isDerivedType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000559 DIDerivedType(DbgNode).print(OS);
Devang Patel6ceea332009-08-31 18:49:10 +0000560 else if (isCompositeType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000561 DICompositeType(DbgNode).print(OS);
Devang Patel7136a652009-07-01 22:10:23 +0000562 else {
Dan Gohman50404362010-05-07 15:30:29 +0000563 OS << "Invalid DIType\n";
Devang Patel7136a652009-07-01 22:10:23 +0000564 return;
565 }
566
Dan Gohman50404362010-05-07 15:30:29 +0000567 OS << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000568}
569
Dan Gohman50404362010-05-07 15:30:29 +0000570/// print - Print basic type.
571void DIBasicType::print(raw_ostream &OS) const {
572 OS << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000573}
574
Dan Gohman50404362010-05-07 15:30:29 +0000575/// print - Print derived type.
576void DIDerivedType::print(raw_ostream &OS) const {
Dan Gohmanc014d092010-05-07 16:17:22 +0000577 OS << "\n\t Derived From: "; getTypeDerivedFrom().print(OS);
Devang Patel7136a652009-07-01 22:10:23 +0000578}
579
Dan Gohman50404362010-05-07 15:30:29 +0000580/// print - Print composite type.
581void DICompositeType::print(raw_ostream &OS) const {
Devang Patel7136a652009-07-01 22:10:23 +0000582 DIArray A = getTypeArray();
Dan Gohman50404362010-05-07 15:30:29 +0000583 OS << " [" << A.getNumElements() << " elements]";
Devang Patel7136a652009-07-01 22:10:23 +0000584}
585
Dan Gohman50404362010-05-07 15:30:29 +0000586/// print - Print subprogram.
587void DISubprogram::print(raw_ostream &OS) const {
Devang Patel65dbc902009-11-25 17:36:49 +0000588 StringRef Res = getName();
589 if (!Res.empty())
Dan Gohman50404362010-05-07 15:30:29 +0000590 OS << " [" << Res << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000591
592 unsigned Tag = getTag();
Dan Gohman50404362010-05-07 15:30:29 +0000593 OS << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000594
595 // TODO : Print context
Dan Gohmanc014d092010-05-07 16:17:22 +0000596 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000597 OS << " [" << getLineNumber() << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000598
599 if (isLocalToUnit())
Dan Gohman50404362010-05-07 15:30:29 +0000600 OS << " [local] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000601
602 if (isDefinition())
Dan Gohman50404362010-05-07 15:30:29 +0000603 OS << " [def] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000604
Dan Gohman50404362010-05-07 15:30:29 +0000605 OS << "\n";
606}
607
608/// print - Print global variable.
609void DIGlobalVariable::print(raw_ostream &OS) const {
610 OS << " [";
Devang Patela49d8772010-05-07 23:19:07 +0000611 StringRef Res = getName();
612 if (!Res.empty())
613 OS << " [" << Res << "] ";
614
615 unsigned Tag = getTag();
616 OS << " [" << dwarf::TagString(Tag) << "] ";
617
618 // TODO : Print context
619 getCompileUnit().print(OS);
620 OS << " [" << getLineNumber() << "] ";
621
622 if (isLocalToUnit())
623 OS << " [local] ";
624
625 if (isDefinition())
626 OS << " [def] ";
627
628 if (isGlobalVariable())
629 DIGlobalVariable(DbgNode).print(OS);
630 OS << "]\n";
Dan Gohman50404362010-05-07 15:30:29 +0000631}
632
633/// print - Print variable.
634void DIVariable::print(raw_ostream &OS) const {
635 StringRef Res = getName();
636 if (!Res.empty())
637 OS << " [" << Res << "] ";
638
Dan Gohmanc014d092010-05-07 16:17:22 +0000639 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000640 OS << " [" << getLineNumber() << "] ";
Dan Gohmanc014d092010-05-07 16:17:22 +0000641 getType().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000642 OS << "\n";
643
644 // FIXME: Dump complex addresses
645}
646
647/// dump - Print descriptor to dbgs() with a newline.
648void DIDescriptor::dump() const {
649 print(dbgs()); dbgs() << '\n';
650}
651
652/// dump - Print compile unit to dbgs() with a newline.
653void DICompileUnit::dump() const {
654 print(dbgs()); dbgs() << '\n';
655}
656
657/// dump - Print type to dbgs() with a newline.
658void DIType::dump() const {
659 print(dbgs()); dbgs() << '\n';
660}
661
662/// dump - Print basic type to dbgs() with a newline.
663void DIBasicType::dump() const {
664 print(dbgs()); dbgs() << '\n';
665}
666
667/// dump - Print derived type to dbgs() with a newline.
668void DIDerivedType::dump() const {
669 print(dbgs()); dbgs() << '\n';
670}
671
672/// dump - Print composite type to dbgs() with a newline.
673void DICompositeType::dump() const {
674 print(dbgs()); dbgs() << '\n';
675}
676
Dan Gohman50404362010-05-07 15:30:29 +0000677/// dump - Print subprogram to dbgs() with a newline.
678void DISubprogram::dump() const {
679 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000680}
681
682/// dump - Print global variable.
683void DIGlobalVariable::dump() const {
Dan Gohman50404362010-05-07 15:30:29 +0000684 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000685}
686
687/// dump - Print variable.
688void DIVariable::dump() const {
Dan Gohman50404362010-05-07 15:30:29 +0000689 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000690}
691
692//===----------------------------------------------------------------------===//
Chris Lattnera45664f2008-11-10 02:56:27 +0000693// DIFactory: Basic Helpers
694//===----------------------------------------------------------------------===//
695
Bill Wendlingdc817b62009-05-14 18:26:15 +0000696DIFactory::DIFactory(Module &m)
Victor Hernandez06203122010-01-23 00:03:28 +0000697 : M(m), VMContext(M.getContext()), DeclareFn(0), ValueFn(0) {}
Chris Lattner497a7a82008-11-10 04:10:34 +0000698
Chris Lattnera45664f2008-11-10 02:56:27 +0000699Constant *DIFactory::GetTagConstant(unsigned TAG) {
Devang Patel6906ba52009-01-20 19:22:03 +0000700 assert((TAG & LLVMDebugVersionMask) == 0 &&
Chris Lattnera45664f2008-11-10 02:56:27 +0000701 "Tag too large for debug encoding!");
Owen Anderson1d0be152009-08-13 21:58:54 +0000702 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
Chris Lattnera45664f2008-11-10 02:56:27 +0000703}
704
Chris Lattnera45664f2008-11-10 02:56:27 +0000705//===----------------------------------------------------------------------===//
706// DIFactory: Primary Constructors
707//===----------------------------------------------------------------------===//
708
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000709/// GetOrCreateArray - Create an descriptor for an array of descriptors.
Chris Lattnera45664f2008-11-10 02:56:27 +0000710/// This implicitly uniques the arrays created.
711DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
Benjamin Kramera53557e2010-09-21 16:41:29 +0000712 if (NumTys == 0) {
713 Value *Null = llvm::Constant::getNullValue(Type::getInt32Ty(VMContext));
714 return DIArray(MDNode::get(VMContext, &Null, 1));
715 }
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000716
Benjamin Kramera53557e2010-09-21 16:41:29 +0000717 SmallVector<Value *, 16> Elts(Tys, Tys+NumTys);
718 return DIArray(MDNode::get(VMContext, Elts.data(), Elts.size()));
Chris Lattnera45664f2008-11-10 02:56:27 +0000719}
720
721/// GetOrCreateSubrange - Create a descriptor for a value range. This
722/// implicitly uniques the values returned.
723DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
Devang Patele4b27562009-08-28 23:24:31 +0000724 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000725 GetTagConstant(dwarf::DW_TAG_subrange_type),
Owen Anderson1d0be152009-08-13 21:58:54 +0000726 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
727 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
Chris Lattnera45664f2008-11-10 02:56:27 +0000728 };
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000729
Devang Patele4b27562009-08-28 23:24:31 +0000730 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000731}
732
Devang Pateld6747df2010-10-06 20:50:40 +0000733/// CreateUnspecifiedParameter - Create unspeicified type descriptor
734/// for the subroutine type.
735DIDescriptor DIFactory::CreateUnspecifiedParameter() {
736 Value *Elts[] = {
737 GetTagConstant(dwarf::DW_TAG_unspecified_parameters)
738 };
739 return DIDescriptor(MDNode::get(VMContext, &Elts[0], 1));
740}
Chris Lattnera45664f2008-11-10 02:56:27 +0000741
742/// CreateCompileUnit - Create a new descriptor for the specified compile
743/// unit. Note that this does not unique compile units within the module.
744DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
Devang Patel65dbc902009-11-25 17:36:49 +0000745 StringRef Filename,
746 StringRef Directory,
747 StringRef Producer,
Devang Pateldd9db662009-01-30 18:20:31 +0000748 bool isMain,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000749 bool isOptimized,
Devang Patel65dbc902009-11-25 17:36:49 +0000750 StringRef Flags,
Devang Patel13319ce2009-02-17 22:43:44 +0000751 unsigned RunTimeVer) {
Devang Patele4b27562009-08-28 23:24:31 +0000752 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000753 GetTagConstant(dwarf::DW_TAG_compile_unit),
Devang Patele4b27562009-08-28 23:24:31 +0000754 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Owen Anderson1d0be152009-08-13 21:58:54 +0000755 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
Devang Patele4b27562009-08-28 23:24:31 +0000756 MDString::get(VMContext, Filename),
757 MDString::get(VMContext, Directory),
758 MDString::get(VMContext, Producer),
Owen Anderson1d0be152009-08-13 21:58:54 +0000759 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
760 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
Devang Patele4b27562009-08-28 23:24:31 +0000761 MDString::get(VMContext, Flags),
Owen Anderson1d0be152009-08-13 21:58:54 +0000762 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
Chris Lattnera45664f2008-11-10 02:56:27 +0000763 };
Devang Patele4b27562009-08-28 23:24:31 +0000764
765 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000766}
767
Devang Patel7aa81892010-03-08 22:27:22 +0000768/// CreateFile - Create a new descriptor for the specified file.
769DIFile DIFactory::CreateFile(StringRef Filename,
770 StringRef Directory,
771 DICompileUnit CU) {
772 Value *Elts[] = {
773 GetTagConstant(dwarf::DW_TAG_file_type),
774 MDString::get(VMContext, Filename),
775 MDString::get(VMContext, Directory),
Devang Patel2db49d72010-05-07 18:11:54 +0000776 CU
Devang Patel7aa81892010-03-08 22:27:22 +0000777 };
778
779 return DIFile(MDNode::get(VMContext, &Elts[0], 4));
780}
781
Chris Lattnera45664f2008-11-10 02:56:27 +0000782/// CreateEnumerator - Create a single enumerator value.
Devang Patel65dbc902009-11-25 17:36:49 +0000783DIEnumerator DIFactory::CreateEnumerator(StringRef Name, uint64_t Val){
Devang Patele4b27562009-08-28 23:24:31 +0000784 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000785 GetTagConstant(dwarf::DW_TAG_enumerator),
Devang Patele4b27562009-08-28 23:24:31 +0000786 MDString::get(VMContext, Name),
Owen Anderson1d0be152009-08-13 21:58:54 +0000787 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
Chris Lattnera45664f2008-11-10 02:56:27 +0000788 };
Devang Patele4b27562009-08-28 23:24:31 +0000789 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000790}
791
792
793/// CreateBasicType - Create a basic type like int, float, etc.
794DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000795 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000796 DIFile F,
Chris Lattnera45664f2008-11-10 02:56:27 +0000797 unsigned LineNumber,
798 uint64_t SizeInBits,
799 uint64_t AlignInBits,
800 uint64_t OffsetInBits, unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000801 unsigned Encoding) {
Devang Patele4b27562009-08-28 23:24:31 +0000802 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000803 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patel2db49d72010-05-07 18:11:54 +0000804 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000805 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000806 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000807 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
808 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
809 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
810 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
811 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
812 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
Chris Lattnera45664f2008-11-10 02:56:27 +0000813 };
Devang Patele4b27562009-08-28 23:24:31 +0000814 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000815}
816
Devang Patelac16d442009-10-26 16:54:35 +0000817
818/// CreateBasicType - Create a basic type like int, float, etc.
819DIBasicType DIFactory::CreateBasicTypeEx(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000820 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000821 DIFile F,
Devang Patelac16d442009-10-26 16:54:35 +0000822 unsigned LineNumber,
823 Constant *SizeInBits,
824 Constant *AlignInBits,
825 Constant *OffsetInBits, unsigned Flags,
826 unsigned Encoding) {
827 Value *Elts[] = {
828 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patel2db49d72010-05-07 18:11:54 +0000829 Context,
Devang Patelac16d442009-10-26 16:54:35 +0000830 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000831 F,
Devang Patelac16d442009-10-26 16:54:35 +0000832 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
833 SizeInBits,
834 AlignInBits,
835 OffsetInBits,
836 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
837 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
838 };
839 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
840}
841
Devang Patelb4645642010-02-06 01:02:37 +0000842/// CreateArtificialType - Create a new DIType with "artificial" flag set.
843DIType DIFactory::CreateArtificialType(DIType Ty) {
844 if (Ty.isArtificial())
845 return Ty;
846
847 SmallVector<Value *, 9> Elts;
Devang Patel2db49d72010-05-07 18:11:54 +0000848 MDNode *N = Ty;
Devang Patelb4645642010-02-06 01:02:37 +0000849 assert (N && "Unexpected input DIType!");
850 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
851 if (Value *V = N->getOperand(i))
852 Elts.push_back(V);
853 else
854 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
855 }
856
857 unsigned CurFlags = Ty.getFlags();
858 CurFlags = CurFlags | DIType::FlagArtificial;
859
860 // Flags are stored at this slot.
861 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
862
863 return DIType(MDNode::get(VMContext, Elts.data(), Elts.size()));
864}
Devang Patelac16d442009-10-26 16:54:35 +0000865
Chris Lattnera45664f2008-11-10 02:56:27 +0000866/// CreateDerivedType - Create a derived type like const qualified type,
867/// pointer, typedef, etc.
868DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
869 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000870 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000871 DIFile F,
Chris Lattnera45664f2008-11-10 02:56:27 +0000872 unsigned LineNumber,
873 uint64_t SizeInBits,
874 uint64_t AlignInBits,
875 uint64_t OffsetInBits,
876 unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000877 DIType DerivedFrom) {
Devang Patele4b27562009-08-28 23:24:31 +0000878 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000879 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000880 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000881 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000882 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000883 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
884 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
885 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
886 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
887 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000888 DerivedFrom,
Chris Lattnera45664f2008-11-10 02:56:27 +0000889 };
Devang Patele4b27562009-08-28 23:24:31 +0000890 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000891}
892
Devang Patelac16d442009-10-26 16:54:35 +0000893
894/// CreateDerivedType - Create a derived type like const qualified type,
895/// pointer, typedef, etc.
896DIDerivedType DIFactory::CreateDerivedTypeEx(unsigned Tag,
897 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000898 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000899 DIFile F,
Devang Patelac16d442009-10-26 16:54:35 +0000900 unsigned LineNumber,
901 Constant *SizeInBits,
902 Constant *AlignInBits,
903 Constant *OffsetInBits,
904 unsigned Flags,
905 DIType DerivedFrom) {
906 Value *Elts[] = {
907 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000908 Context,
Devang Patelac16d442009-10-26 16:54:35 +0000909 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000910 F,
Devang Patelac16d442009-10-26 16:54:35 +0000911 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
912 SizeInBits,
913 AlignInBits,
914 OffsetInBits,
915 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000916 DerivedFrom,
Devang Patelac16d442009-10-26 16:54:35 +0000917 };
918 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
919}
920
921
Chris Lattnera45664f2008-11-10 02:56:27 +0000922/// CreateCompositeType - Create a composite type like array, struct, etc.
923DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
924 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000925 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000926 DIFile F,
Chris Lattnera45664f2008-11-10 02:56:27 +0000927 unsigned LineNumber,
928 uint64_t SizeInBits,
929 uint64_t AlignInBits,
930 uint64_t OffsetInBits,
931 unsigned Flags,
932 DIType DerivedFrom,
Devang Patel13319ce2009-02-17 22:43:44 +0000933 DIArray Elements,
Devang Patel0fd7f9d2010-01-26 21:14:59 +0000934 unsigned RuntimeLang,
935 MDNode *ContainingType) {
Owen Andersone277fed2009-07-07 16:31:25 +0000936
Devang Patele4b27562009-08-28 23:24:31 +0000937 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000938 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000939 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000940 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000941 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000942 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
943 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
944 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
945 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
946 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000947 DerivedFrom,
948 Elements,
Devang Patel0fd7f9d2010-01-26 21:14:59 +0000949 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
950 ContainingType
Chris Lattnera45664f2008-11-10 02:56:27 +0000951 };
Devang Patele7e5a0f2010-08-10 20:01:20 +0000952
953 MDNode *Node = MDNode::get(VMContext, &Elts[0], 13);
954 // Create a named metadata so that we do not lose this enum info.
955 if (Tag == dwarf::DW_TAG_enumeration_type) {
956 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.enum");
957 NMD->addOperand(Node);
958 }
959 return DICompositeType(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +0000960}
961
962
Dan Gohman489b29b2010-08-20 22:02:26 +0000963/// CreateTemporaryType - Create a temporary forward-declared type.
Dan Gohmana3833f12010-08-20 22:39:47 +0000964DIType DIFactory::CreateTemporaryType() {
Dan Gohman489b29b2010-08-20 22:02:26 +0000965 // Give the temporary MDNode a tag. It doesn't matter what tag we
966 // use here as long as DIType accepts it.
967 Value *Elts[] = {
968 GetTagConstant(DW_TAG_base_type)
969 };
970 MDNode *Node = MDNode::getTemporary(VMContext, Elts, array_lengthof(Elts));
971 return DIType(Node);
972}
973
974
Devang Patelac16d442009-10-26 16:54:35 +0000975/// CreateCompositeType - Create a composite type like array, struct, etc.
976DICompositeType DIFactory::CreateCompositeTypeEx(unsigned Tag,
977 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000978 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000979 DIFile F,
Devang Patelac16d442009-10-26 16:54:35 +0000980 unsigned LineNumber,
981 Constant *SizeInBits,
982 Constant *AlignInBits,
983 Constant *OffsetInBits,
984 unsigned Flags,
985 DIType DerivedFrom,
986 DIArray Elements,
Devang Patel6bf058c2010-08-10 20:22:49 +0000987 unsigned RuntimeLang,
988 MDNode *ContainingType) {
Devang Patelac16d442009-10-26 16:54:35 +0000989 Value *Elts[] = {
990 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000991 Context,
Devang Patelac16d442009-10-26 16:54:35 +0000992 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000993 F,
Devang Patelac16d442009-10-26 16:54:35 +0000994 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
995 SizeInBits,
996 AlignInBits,
997 OffsetInBits,
998 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000999 DerivedFrom,
1000 Elements,
Devang Patel6bf058c2010-08-10 20:22:49 +00001001 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
1002 ContainingType
Devang Patelac16d442009-10-26 16:54:35 +00001003 };
Devang Patel6bf058c2010-08-10 20:22:49 +00001004 MDNode *Node = MDNode::get(VMContext, &Elts[0], 13);
Devang Patele7e5a0f2010-08-10 20:01:20 +00001005 // Create a named metadata so that we do not lose this enum info.
1006 if (Tag == dwarf::DW_TAG_enumeration_type) {
1007 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.enum");
1008 NMD->addOperand(Node);
1009 }
1010 return DICompositeType(Node);
Devang Patelac16d442009-10-26 16:54:35 +00001011}
1012
1013
Chris Lattnera45664f2008-11-10 02:56:27 +00001014/// CreateSubprogram - Create a new descriptor for the specified subprogram.
1015/// See comments in DISubprogram for descriptions of these fields. This
1016/// method does not unique the generated descriptors.
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001017DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +00001018 StringRef Name,
1019 StringRef DisplayName,
1020 StringRef LinkageName,
Devang Patel4b945502010-03-09 00:44:10 +00001021 DIFile F,
Devang Patel2e369932010-01-23 00:26:28 +00001022 unsigned LineNo, DIType Ty,
Chris Lattnera45664f2008-11-10 02:56:27 +00001023 bool isLocalToUnit,
Devang Patel5d11eb02009-12-03 19:11:07 +00001024 bool isDefinition,
1025 unsigned VK, unsigned VIndex,
Devang Patel4e0d19d2010-02-03 19:57:19 +00001026 DIType ContainingType,
Devang Patel9dd2b472010-09-29 21:04:46 +00001027 unsigned Flags,
Stuart Hastings215aa152010-06-11 20:08:44 +00001028 bool isOptimized,
1029 Function *Fn) {
Devang Patel854967e2008-12-17 22:39:29 +00001030
Devang Patele4b27562009-08-28 23:24:31 +00001031 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001032 GetTagConstant(dwarf::DW_TAG_subprogram),
Devang Patele4b27562009-08-28 23:24:31 +00001033 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Devang Patel2db49d72010-05-07 18:11:54 +00001034 Context,
Devang Patele4b27562009-08-28 23:24:31 +00001035 MDString::get(VMContext, Name),
1036 MDString::get(VMContext, DisplayName),
1037 MDString::get(VMContext, LinkageName),
Devang Patel2db49d72010-05-07 18:11:54 +00001038 F,
Owen Anderson1d0be152009-08-13 21:58:54 +00001039 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001040 Ty,
Owen Anderson1d0be152009-08-13 21:58:54 +00001041 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
Devang Patel5d11eb02009-12-03 19:11:07 +00001042 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1043 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
1044 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
Devang Patel2db49d72010-05-07 18:11:54 +00001045 ContainingType,
Devang Patel9dd2b472010-09-29 21:04:46 +00001046 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Stuart Hastings215aa152010-06-11 20:08:44 +00001047 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
1048 Fn
Chris Lattnera45664f2008-11-10 02:56:27 +00001049 };
Devang Patelfd5fdc32010-06-28 05:53:08 +00001050 MDNode *Node = MDNode::get(VMContext, &Elts[0], 17);
1051
1052 // Create a named metadata so that we do not lose this mdnode.
1053 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
1054 NMD->addOperand(Node);
1055 return DISubprogram(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +00001056}
1057
Devang Patele3a18de2009-12-01 23:09:02 +00001058/// CreateSubprogramDefinition - Create new subprogram descriptor for the
Jim Grosbache62b6902010-07-21 21:36:25 +00001059/// given declaration.
1060DISubprogram DIFactory::CreateSubprogramDefinition(DISubprogram &SPDeclaration){
Devang Patele3a18de2009-12-01 23:09:02 +00001061 if (SPDeclaration.isDefinition())
Devang Patel2db49d72010-05-07 18:11:54 +00001062 return DISubprogram(SPDeclaration);
Devang Patele3a18de2009-12-01 23:09:02 +00001063
Devang Patel2db49d72010-05-07 18:11:54 +00001064 MDNode *DeclNode = SPDeclaration;
Devang Patele3a18de2009-12-01 23:09:02 +00001065 Value *Elts[] = {
1066 GetTagConstant(dwarf::DW_TAG_subprogram),
1067 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001068 DeclNode->getOperand(2), // Context
1069 DeclNode->getOperand(3), // Name
1070 DeclNode->getOperand(4), // DisplayName
1071 DeclNode->getOperand(5), // LinkageName
1072 DeclNode->getOperand(6), // CompileUnit
1073 DeclNode->getOperand(7), // LineNo
1074 DeclNode->getOperand(8), // Type
1075 DeclNode->getOperand(9), // isLocalToUnit
Stuart Hastings6d56b9f2010-06-05 00:39:29 +00001076 ConstantInt::get(Type::getInt1Ty(VMContext), true),
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001077 DeclNode->getOperand(11), // Virtuality
1078 DeclNode->getOperand(12), // VIndex
Devang Patel4e0d19d2010-02-03 19:57:19 +00001079 DeclNode->getOperand(13), // Containting Type
Devang Patel9dd2b472010-09-29 21:04:46 +00001080 DeclNode->getOperand(14), // Flags
Devang Patelacc6efa2010-06-27 21:04:31 +00001081 DeclNode->getOperand(15), // isOptimized
1082 SPDeclaration.getFunction()
Devang Patele3a18de2009-12-01 23:09:02 +00001083 };
Devang Patelfd5fdc32010-06-28 05:53:08 +00001084 MDNode *Node =MDNode::get(VMContext, &Elts[0], 16);
1085
1086 // Create a named metadata so that we do not lose this mdnode.
1087 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
1088 NMD->addOperand(Node);
1089 return DISubprogram(Node);
Devang Patele3a18de2009-12-01 23:09:02 +00001090}
1091
Chris Lattnera45664f2008-11-10 02:56:27 +00001092/// CreateGlobalVariable - Create a new descriptor for the specified global.
1093DIGlobalVariable
Devang Patel65dbc902009-11-25 17:36:49 +00001094DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
1095 StringRef DisplayName,
1096 StringRef LinkageName,
Devang Patel4b945502010-03-09 00:44:10 +00001097 DIFile F,
Devang Patel2e369932010-01-23 00:26:28 +00001098 unsigned LineNo, DIType Ty,bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +00001099 bool isDefinition, llvm::GlobalVariable *Val) {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001100 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001101 GetTagConstant(dwarf::DW_TAG_variable),
Devang Patele4b27562009-08-28 23:24:31 +00001102 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Devang Patel2db49d72010-05-07 18:11:54 +00001103 Context,
Devang Patele4b27562009-08-28 23:24:31 +00001104 MDString::get(VMContext, Name),
1105 MDString::get(VMContext, DisplayName),
1106 MDString::get(VMContext, LinkageName),
Devang Patel2db49d72010-05-07 18:11:54 +00001107 F,
Owen Anderson1d0be152009-08-13 21:58:54 +00001108 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001109 Ty,
Owen Anderson1d0be152009-08-13 21:58:54 +00001110 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1111 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
Devang Patele4b27562009-08-28 23:24:31 +00001112 Val
Chris Lattnera45664f2008-11-10 02:56:27 +00001113 };
Devang Patele4b27562009-08-28 23:24:31 +00001114
1115 Value *const *Vs = &Elts[0];
1116 MDNode *Node = MDNode::get(VMContext,Vs, 12);
1117
1118 // Create a named metadata so that we do not lose this mdnode.
1119 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001120 NMD->addOperand(Node);
Devang Patele4b27562009-08-28 23:24:31 +00001121
1122 return DIGlobalVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +00001123}
1124
Devang Patel27398962010-08-09 21:39:24 +00001125/// CreateGlobalVariable - Create a new descriptor for the specified constant.
1126DIGlobalVariable
1127DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
1128 StringRef DisplayName,
1129 StringRef LinkageName,
1130 DIFile F,
1131 unsigned LineNo, DIType Ty,bool isLocalToUnit,
1132 bool isDefinition, llvm::Constant *Val) {
1133 Value *Elts[] = {
Devang Patelebd53742010-08-11 23:17:54 +00001134 GetTagConstant(dwarf::DW_TAG_variable),
Devang Patel27398962010-08-09 21:39:24 +00001135 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
1136 Context,
1137 MDString::get(VMContext, Name),
1138 MDString::get(VMContext, DisplayName),
1139 MDString::get(VMContext, LinkageName),
1140 F,
1141 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1142 Ty,
1143 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1144 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1145 Val
1146 };
1147
1148 Value *const *Vs = &Elts[0];
1149 MDNode *Node = MDNode::get(VMContext,Vs, 12);
1150
1151 // Create a named metadata so that we do not lose this mdnode.
1152 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
1153 NMD->addOperand(Node);
1154
1155 return DIGlobalVariable(Node);
1156}
Chris Lattnera45664f2008-11-10 02:56:27 +00001157
1158/// CreateVariable - Create a new descriptor for the specified variable.
1159DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +00001160 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +00001161 DIFile F,
1162 unsigned LineNo,
Devang Patel3cf763d2010-09-29 23:07:21 +00001163 DIType Ty, bool AlwaysPreserve,
1164 unsigned Flags) {
Devang Patele4b27562009-08-28 23:24:31 +00001165 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001166 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +00001167 Context,
Devang Patele4b27562009-08-28 23:24:31 +00001168 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +00001169 F,
Owen Anderson1d0be152009-08-13 21:58:54 +00001170 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001171 Ty,
Devang Patel3cf763d2010-09-29 23:07:21 +00001172 ConstantInt::get(Type::getInt32Ty(VMContext), Flags)
Chris Lattnera45664f2008-11-10 02:56:27 +00001173 };
Devang Patel3cf763d2010-09-29 23:07:21 +00001174 MDNode *Node = MDNode::get(VMContext, &Elts[0], 7);
Devang Patel6ed0ce32010-05-20 20:35:24 +00001175 if (AlwaysPreserve) {
1176 // The optimizer may remove local variable. If there is an interest
1177 // to preserve variable info in such situation then stash it in a
1178 // named mdnode.
Devang Patel2f7d5292010-06-16 00:53:55 +00001179 DISubprogram Fn(getDISubprogram(Context));
Devang Patel10de3bb2010-06-21 18:36:58 +00001180 StringRef FName = "fn";
1181 if (Fn.getFunction())
1182 FName = Fn.getFunction()->getName();
Devang Patel10de3bb2010-06-21 18:36:58 +00001183 char One = '\1';
1184 if (FName.startswith(StringRef(&One, 1)))
1185 FName = FName.substr(1);
Dan Gohman17aa92c2010-07-21 23:38:33 +00001186
1187 SmallString<32> Out;
1188 NamedMDNode *FnLocals =
1189 M.getOrInsertNamedMetadata(Twine("llvm.dbg.lv.", FName).toStringRef(Out));
Devang Patel2f7d5292010-06-16 00:53:55 +00001190 FnLocals->addOperand(Node);
Devang Patel98e1cac2010-05-14 21:01:35 +00001191 }
1192 return DIVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +00001193}
1194
1195
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001196/// CreateComplexVariable - Create a new descriptor for the specified variable
1197/// which has a complex address expression for its address.
1198DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
Benjamin Kramer28b4afc2010-09-21 16:00:03 +00001199 StringRef Name, DIFile F,
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001200 unsigned LineNo,
Benjamin Kramer28b4afc2010-09-21 16:00:03 +00001201 DIType Ty, Value *const *Addr,
1202 unsigned NumAddr) {
1203 SmallVector<Value *, 15> Elts;
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001204 Elts.push_back(GetTagConstant(Tag));
Devang Patel2db49d72010-05-07 18:11:54 +00001205 Elts.push_back(Context);
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001206 Elts.push_back(MDString::get(VMContext, Name));
Devang Patel2db49d72010-05-07 18:11:54 +00001207 Elts.push_back(F);
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001208 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
Devang Patel2db49d72010-05-07 18:11:54 +00001209 Elts.push_back(Ty);
Benjamin Kramer28b4afc2010-09-21 16:00:03 +00001210 Elts.append(Addr, Addr+NumAddr);
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001211
Benjamin Kramer28b4afc2010-09-21 16:00:03 +00001212 return DIVariable(MDNode::get(VMContext, Elts.data(), Elts.size()));
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001213}
1214
1215
Chris Lattnera45664f2008-11-10 02:56:27 +00001216/// CreateBlock - This creates a descriptor for a lexical block with the
Owen Anderson99035272009-07-07 17:12:53 +00001217/// specified parent VMContext.
Devang Patel3d821aa2010-02-16 21:39:34 +00001218DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context,
Stuart Hastings0db42712010-07-19 23:56:30 +00001219 DIFile F, unsigned LineNo,
1220 unsigned Col) {
1221 // Defeat MDNode uniqing for lexical blocks.
1222 static unsigned int unique_id = 0;
Devang Patele4b27562009-08-28 23:24:31 +00001223 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001224 GetTagConstant(dwarf::DW_TAG_lexical_block),
Devang Patel2db49d72010-05-07 18:11:54 +00001225 Context,
Devang Patel3d821aa2010-02-16 21:39:34 +00001226 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Stuart Hastings0db42712010-07-19 23:56:30 +00001227 ConstantInt::get(Type::getInt32Ty(VMContext), Col),
1228 F,
1229 ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
Chris Lattnera45664f2008-11-10 02:56:27 +00001230 };
Stuart Hastings0db42712010-07-19 23:56:30 +00001231 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 6));
Chris Lattnera45664f2008-11-10 02:56:27 +00001232}
1233
Devang Patel6404e4e2009-12-15 19:16:48 +00001234/// CreateNameSpace - This creates new descriptor for a namespace
1235/// with the specified parent context.
1236DINameSpace DIFactory::CreateNameSpace(DIDescriptor Context, StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +00001237 DIFile F,
Devang Patel6404e4e2009-12-15 19:16:48 +00001238 unsigned LineNo) {
1239 Value *Elts[] = {
1240 GetTagConstant(dwarf::DW_TAG_namespace),
Devang Patel2db49d72010-05-07 18:11:54 +00001241 Context,
Devang Patel6404e4e2009-12-15 19:16:48 +00001242 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +00001243 F,
Devang Patel6404e4e2009-12-15 19:16:48 +00001244 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1245 };
1246 return DINameSpace(MDNode::get(VMContext, &Elts[0], 5));
1247}
1248
Devang Patelf98d8fe2009-09-01 01:14:15 +00001249/// CreateLocation - Creates a debug info location.
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001250DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
Daniel Dunbara279bc32009-09-20 02:20:51 +00001251 DIScope S, DILocation OrigLoc) {
Devang Patelf98d8fe2009-09-01 01:14:15 +00001252 Value *Elts[] = {
1253 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1254 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001255 S,
1256 OrigLoc,
Devang Patelf98d8fe2009-09-01 01:14:15 +00001257 };
1258 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1259}
1260
Chris Lattnera45664f2008-11-10 02:56:27 +00001261//===----------------------------------------------------------------------===//
1262// DIFactory: Routines for inserting code into a function
1263//===----------------------------------------------------------------------===//
1264
Chris Lattnera45664f2008-11-10 02:56:27 +00001265/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Devang Patel6daf99b2009-11-10 22:05:35 +00001266Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001267 Instruction *InsertBefore) {
Victor Hernandez4cf292a2010-01-26 02:07:38 +00001268 assert(Storage && "no storage passed to dbg.declare");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001269 assert(D.Verify() && "empty DIVariable passed to dbg.declare");
Chris Lattnera45664f2008-11-10 02:56:27 +00001270 if (!DeclareFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +00001271 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1272
Victor Hernandez756462b2010-01-18 20:42:09 +00001273 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
Devang Patel2db49d72010-05-07 18:11:54 +00001274 D };
Devang Patel6daf99b2009-11-10 22:05:35 +00001275 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
Mike Stumpe4250392009-10-01 22:08:58 +00001276}
1277
1278/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Devang Patel6daf99b2009-11-10 22:05:35 +00001279Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001280 BasicBlock *InsertAtEnd) {
Victor Hernandez4cf292a2010-01-26 02:07:38 +00001281 assert(Storage && "no storage passed to dbg.declare");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001282 assert(D.Verify() && "invalid DIVariable passed to dbg.declare");
Mike Stumpe4250392009-10-01 22:08:58 +00001283 if (!DeclareFn)
1284 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1285
Victor Hernandez756462b2010-01-18 20:42:09 +00001286 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
Devang Patel2db49d72010-05-07 18:11:54 +00001287 D };
Devang Patel27a53de2010-01-29 18:30:57 +00001288
1289 // If this block already has a terminator then insert this intrinsic
1290 // before the terminator.
Jim Grosbache62b6902010-07-21 21:36:25 +00001291 if (TerminatorInst *T = InsertAtEnd->getTerminator())
Devang Patel27a53de2010-01-29 18:30:57 +00001292 return CallInst::Create(DeclareFn, Args, Args+2, "", T);
1293 else
1294 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);}
Torok Edwin620f2802008-12-16 09:07:36 +00001295
Victor Hernandezc59b3352009-12-07 21:54:43 +00001296/// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001297Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
Victor Hernandezc59b3352009-12-07 21:54:43 +00001298 DIVariable D,
1299 Instruction *InsertBefore) {
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001300 assert(V && "no value passed to dbg.value");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001301 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001302 if (!ValueFn)
1303 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1304
Victor Hernandezc8b7cd02010-01-20 05:44:11 +00001305 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001306 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
Devang Patel2db49d72010-05-07 18:11:54 +00001307 D };
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001308 return CallInst::Create(ValueFn, Args, Args+3, "", InsertBefore);
1309}
1310
Victor Hernandezc59b3352009-12-07 21:54:43 +00001311/// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001312Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
Victor Hernandezc59b3352009-12-07 21:54:43 +00001313 DIVariable D,
1314 BasicBlock *InsertAtEnd) {
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001315 assert(V && "no value passed to dbg.value");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001316 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001317 if (!ValueFn)
1318 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1319
Jim Grosbache62b6902010-07-21 21:36:25 +00001320 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001321 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
Devang Patel2db49d72010-05-07 18:11:54 +00001322 D };
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001323 return CallInst::Create(ValueFn, Args, Args+3, "", InsertAtEnd);
1324}
Devang Patele4b27562009-08-28 23:24:31 +00001325
Devang Patel1a7ca032010-09-28 18:08:20 +00001326// RecordType - Record DIType in a module such that it is not lost even if
1327// it is not referenced through debug info anchors.
1328void DIFactory::RecordType(DIType T) {
1329 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.ty");
1330 NMD->addOperand(T);
1331}
1332
1333
Devang Pateld2f79a12009-07-28 19:55:13 +00001334//===----------------------------------------------------------------------===//
Devang Patel98c65172009-07-30 18:25:15 +00001335// DebugInfoFinder implementations.
Devang Pateld2f79a12009-07-28 19:55:13 +00001336//===----------------------------------------------------------------------===//
1337
Devang Patel98c65172009-07-30 18:25:15 +00001338/// processModule - Process entire module and collect debug info.
1339void DebugInfoFinder::processModule(Module &M) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001340 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1341 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
1342 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
1343 ++BI) {
Devang Patel01c5ff62010-05-04 01:05:02 +00001344 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
Devang Patelb4d31302009-07-31 18:18:52 +00001345 processDeclare(DDI);
Jim Grosbache62b6902010-07-21 21:36:25 +00001346
Chris Lattner28a9bf62010-04-02 20:44:29 +00001347 DebugLoc Loc = BI->getDebugLoc();
1348 if (Loc.isUnknown())
1349 continue;
Jim Grosbache62b6902010-07-21 21:36:25 +00001350
Chris Lattner28a9bf62010-04-02 20:44:29 +00001351 LLVMContext &Ctx = BI->getContext();
1352 DIDescriptor Scope(Loc.getScope(Ctx));
Jim Grosbache62b6902010-07-21 21:36:25 +00001353
Chris Lattner28a9bf62010-04-02 20:44:29 +00001354 if (Scope.isCompileUnit())
Devang Patel2db49d72010-05-07 18:11:54 +00001355 addCompileUnit(DICompileUnit(Scope));
Chris Lattner28a9bf62010-04-02 20:44:29 +00001356 else if (Scope.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001357 processSubprogram(DISubprogram(Scope));
Chris Lattner28a9bf62010-04-02 20:44:29 +00001358 else if (Scope.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001359 processLexicalBlock(DILexicalBlock(Scope));
Jim Grosbache62b6902010-07-21 21:36:25 +00001360
Chris Lattner28a9bf62010-04-02 20:44:29 +00001361 if (MDNode *IA = Loc.getInlinedAt(Ctx))
1362 processLocation(DILocation(IA));
Devang Pateld2f79a12009-07-28 19:55:13 +00001363 }
Devang Patele4b27562009-08-28 23:24:31 +00001364
Devang Patelfd5fdc32010-06-28 05:53:08 +00001365 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
1366 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1367 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
1368 if (addGlobalVariable(DIG)) {
1369 addCompileUnit(DIG.getCompileUnit());
1370 processType(DIG.getType());
1371 }
Devang Pateld2f79a12009-07-28 19:55:13 +00001372 }
1373 }
Devang Patelfd5fdc32010-06-28 05:53:08 +00001374
1375 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
1376 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
1377 processSubprogram(DISubprogram(NMD->getOperand(i)));
Devang Pateld2f79a12009-07-28 19:55:13 +00001378}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001379
Devang Patel6daf99b2009-11-10 22:05:35 +00001380/// processLocation - Process DILocation.
1381void DebugInfoFinder::processLocation(DILocation Loc) {
Devang Patel3c91b052010-03-08 20:52:55 +00001382 if (!Loc.Verify()) return;
Devang Patel2db49d72010-05-07 18:11:54 +00001383 DIDescriptor S(Loc.getScope());
Devang Patel6daf99b2009-11-10 22:05:35 +00001384 if (S.isCompileUnit())
Devang Patel2db49d72010-05-07 18:11:54 +00001385 addCompileUnit(DICompileUnit(S));
Devang Patel6daf99b2009-11-10 22:05:35 +00001386 else if (S.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001387 processSubprogram(DISubprogram(S));
Devang Patel6daf99b2009-11-10 22:05:35 +00001388 else if (S.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001389 processLexicalBlock(DILexicalBlock(S));
Devang Patel6daf99b2009-11-10 22:05:35 +00001390 processLocation(Loc.getOrigLocation());
1391}
1392
Devang Patel98c65172009-07-30 18:25:15 +00001393/// processType - Process DIType.
1394void DebugInfoFinder::processType(DIType DT) {
Devang Patel72bcdb62009-08-10 22:09:58 +00001395 if (!addType(DT))
Devang Pateld2f79a12009-07-28 19:55:13 +00001396 return;
1397
1398 addCompileUnit(DT.getCompileUnit());
Devang Patel6ceea332009-08-31 18:49:10 +00001399 if (DT.isCompositeType()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001400 DICompositeType DCT(DT);
Devang Patel98c65172009-07-30 18:25:15 +00001401 processType(DCT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001402 DIArray DA = DCT.getTypeArray();
Devang Patel3c91b052010-03-08 20:52:55 +00001403 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1404 DIDescriptor D = DA.getElement(i);
1405 if (D.isType())
Devang Patel2db49d72010-05-07 18:11:54 +00001406 processType(DIType(D));
Devang Patel3c91b052010-03-08 20:52:55 +00001407 else if (D.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001408 processSubprogram(DISubprogram(D));
Devang Patel3c91b052010-03-08 20:52:55 +00001409 }
Devang Patel6ceea332009-08-31 18:49:10 +00001410 } else if (DT.isDerivedType()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001411 DIDerivedType DDT(DT);
Devang Patel3c91b052010-03-08 20:52:55 +00001412 processType(DDT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001413 }
1414}
1415
Devang Patelbeab41b2009-10-07 22:04:08 +00001416/// processLexicalBlock
1417void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
Devang Patelbeab41b2009-10-07 22:04:08 +00001418 DIScope Context = LB.getContext();
1419 if (Context.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001420 return processLexicalBlock(DILexicalBlock(Context));
Devang Patelbeab41b2009-10-07 22:04:08 +00001421 else
Devang Patel2db49d72010-05-07 18:11:54 +00001422 return processSubprogram(DISubprogram(Context));
Devang Patelbeab41b2009-10-07 22:04:08 +00001423}
1424
Devang Patel98c65172009-07-30 18:25:15 +00001425/// processSubprogram - Process DISubprogram.
1426void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001427 if (!addSubprogram(SP))
1428 return;
1429 addCompileUnit(SP.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +00001430 processType(SP.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +00001431}
1432
Devang Patelb4d31302009-07-31 18:18:52 +00001433/// processDeclare - Process DbgDeclareInst.
1434void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
Devang Patel3c91b052010-03-08 20:52:55 +00001435 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1436 if (!N) return;
1437
1438 DIDescriptor DV(N);
1439 if (!DV.isVariable())
Devang Patelb4d31302009-07-31 18:18:52 +00001440 return;
1441
Devang Patel2db49d72010-05-07 18:11:54 +00001442 if (!NodesSeen.insert(DV))
Devang Patelb4d31302009-07-31 18:18:52 +00001443 return;
1444
Devang Patel3c91b052010-03-08 20:52:55 +00001445 addCompileUnit(DIVariable(N).getCompileUnit());
1446 processType(DIVariable(N).getType());
Devang Patelb4d31302009-07-31 18:18:52 +00001447}
1448
Devang Patel72bcdb62009-08-10 22:09:58 +00001449/// addType - Add type into Tys.
1450bool DebugInfoFinder::addType(DIType DT) {
Devang Patel3c91b052010-03-08 20:52:55 +00001451 if (!DT.isValid())
Devang Patel72bcdb62009-08-10 22:09:58 +00001452 return false;
1453
Devang Patel2db49d72010-05-07 18:11:54 +00001454 if (!NodesSeen.insert(DT))
Devang Patel72bcdb62009-08-10 22:09:58 +00001455 return false;
1456
Devang Patel2db49d72010-05-07 18:11:54 +00001457 TYs.push_back(DT);
Devang Patel72bcdb62009-08-10 22:09:58 +00001458 return true;
1459}
1460
Devang Pateld2f79a12009-07-28 19:55:13 +00001461/// addCompileUnit - Add compile unit into CUs.
Devang Patel98c65172009-07-30 18:25:15 +00001462bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Patel3c91b052010-03-08 20:52:55 +00001463 if (!CU.Verify())
Devang Pateld2f79a12009-07-28 19:55:13 +00001464 return false;
1465
Devang Patel2db49d72010-05-07 18:11:54 +00001466 if (!NodesSeen.insert(CU))
Devang Pateld2f79a12009-07-28 19:55:13 +00001467 return false;
1468
Devang Patel2db49d72010-05-07 18:11:54 +00001469 CUs.push_back(CU);
Devang Pateld2f79a12009-07-28 19:55:13 +00001470 return true;
1471}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001472
Devang Pateld2f79a12009-07-28 19:55:13 +00001473/// addGlobalVariable - Add global variable into GVs.
Devang Patel98c65172009-07-30 18:25:15 +00001474bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Patel2db49d72010-05-07 18:11:54 +00001475 if (!DIDescriptor(DIG).isGlobalVariable())
Devang Pateld2f79a12009-07-28 19:55:13 +00001476 return false;
1477
Devang Patel2db49d72010-05-07 18:11:54 +00001478 if (!NodesSeen.insert(DIG))
Devang Pateld2f79a12009-07-28 19:55:13 +00001479 return false;
1480
Devang Patel2db49d72010-05-07 18:11:54 +00001481 GVs.push_back(DIG);
Devang Pateld2f79a12009-07-28 19:55:13 +00001482 return true;
1483}
1484
1485// addSubprogram - Add subprgoram into SPs.
Devang Patel98c65172009-07-30 18:25:15 +00001486bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Patel2db49d72010-05-07 18:11:54 +00001487 if (!DIDescriptor(SP).isSubprogram())
Devang Pateld2f79a12009-07-28 19:55:13 +00001488 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001489
Devang Patel2db49d72010-05-07 18:11:54 +00001490 if (!NodesSeen.insert(SP))
Devang Pateld2f79a12009-07-28 19:55:13 +00001491 return false;
1492
Devang Patel2db49d72010-05-07 18:11:54 +00001493 SPs.push_back(SP);
Devang Pateld2f79a12009-07-28 19:55:13 +00001494 return true;
1495}
1496
Victor Hernandez756462b2010-01-18 20:42:09 +00001497/// Find the debug info descriptor corresponding to this global variable.
1498static Value *findDbgGlobalDeclare(GlobalVariable *V) {
Chris Lattner099b7792009-12-29 09:22:47 +00001499 const Module *M = V->getParent();
1500 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1501 if (!NMD)
Torok Edwinff7d0e92009-03-10 13:41:26 +00001502 return 0;
Chris Lattner099b7792009-12-29 09:22:47 +00001503
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001504 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman872814a2010-07-21 18:54:18 +00001505 DIDescriptor DIG(cast<MDNode>(NMD->getOperand(i)));
Devang Patel3c91b052010-03-08 20:52:55 +00001506 if (!DIG.isGlobalVariable())
Chris Lattner099b7792009-12-29 09:22:47 +00001507 continue;
Devang Patel2db49d72010-05-07 18:11:54 +00001508 if (DIGlobalVariable(DIG).getGlobal() == V)
1509 return DIG;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001510 }
Chris Lattner099b7792009-12-29 09:22:47 +00001511 return 0;
1512}
Torok Edwinff7d0e92009-03-10 13:41:26 +00001513
Chris Lattner099b7792009-12-29 09:22:47 +00001514/// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
1515/// It looks through pointer casts too.
Victor Hernandez756462b2010-01-18 20:42:09 +00001516static const DbgDeclareInst *findDbgDeclare(const Value *V) {
Victor Hernandez3a328652010-01-15 19:04:09 +00001517 V = V->stripPointerCasts();
Jim Grosbache62b6902010-07-21 21:36:25 +00001518
Victor Hernandez3a328652010-01-15 19:04:09 +00001519 if (!isa<Instruction>(V) && !isa<Argument>(V))
Torok Edwin620f2802008-12-16 09:07:36 +00001520 return 0;
Jim Grosbache62b6902010-07-21 21:36:25 +00001521
Victor Hernandez3a328652010-01-15 19:04:09 +00001522 const Function *F = NULL;
1523 if (const Instruction *I = dyn_cast<Instruction>(V))
1524 F = I->getParent()->getParent();
1525 else if (const Argument *A = dyn_cast<Argument>(V))
1526 F = A->getParent();
Jim Grosbache62b6902010-07-21 21:36:25 +00001527
Victor Hernandez3a328652010-01-15 19:04:09 +00001528 for (Function::const_iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
1529 for (BasicBlock::const_iterator BI = (*FI).begin(), BE = (*FI).end();
1530 BI != BE; ++BI)
1531 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1532 if (DDI->getAddress() == V)
1533 return DDI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001534
Chris Lattner099b7792009-12-29 09:22:47 +00001535 return 0;
1536}
Bill Wendlingdc817b62009-05-14 18:26:15 +00001537
Chris Lattner099b7792009-12-29 09:22:47 +00001538bool llvm::getLocationInfo(const Value *V, std::string &DisplayName,
1539 std::string &Type, unsigned &LineNo,
1540 std::string &File, std::string &Dir) {
1541 DICompileUnit Unit;
1542 DIType TypeD;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001543
Chris Lattner099b7792009-12-29 09:22:47 +00001544 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1545 Value *DIGV = findDbgGlobalDeclare(GV);
1546 if (!DIGV) return false;
1547 DIGlobalVariable Var(cast<MDNode>(DIGV));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001548
Chris Lattner099b7792009-12-29 09:22:47 +00001549 StringRef D = Var.getDisplayName();
Devang Patel65dbc902009-11-25 17:36:49 +00001550 if (!D.empty())
Chris Lattner099b7792009-12-29 09:22:47 +00001551 DisplayName = D;
1552 LineNo = Var.getLineNumber();
1553 Unit = Var.getCompileUnit();
1554 TypeD = Var.getType();
1555 } else {
1556 const DbgDeclareInst *DDI = findDbgDeclare(V);
1557 if (!DDI) return false;
1558 DIVariable Var(cast<MDNode>(DDI->getVariable()));
1559
1560 StringRef D = Var.getName();
1561 if (!D.empty())
1562 DisplayName = D;
1563 LineNo = Var.getLineNumber();
1564 Unit = Var.getCompileUnit();
1565 TypeD = Var.getType();
Torok Edwinff7d0e92009-03-10 13:41:26 +00001566 }
Devang Patel13e16b62009-06-26 01:49:18 +00001567
Chris Lattner099b7792009-12-29 09:22:47 +00001568 StringRef T = TypeD.getName();
1569 if (!T.empty())
1570 Type = T;
1571 StringRef F = Unit.getFilename();
1572 if (!F.empty())
1573 File = F;
1574 StringRef D = Unit.getDirectory();
1575 if (!D.empty())
1576 Dir = D;
1577 return true;
1578}
Devang Patel9e529c32009-07-02 01:15:24 +00001579
Chris Lattner099b7792009-12-29 09:22:47 +00001580/// getDISubprogram - Find subprogram that is enclosing this scope.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001581DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
Chris Lattner099b7792009-12-29 09:22:47 +00001582 DIDescriptor D(Scope);
Chris Lattner099b7792009-12-29 09:22:47 +00001583 if (D.isSubprogram())
1584 return DISubprogram(Scope);
Jim Grosbache62b6902010-07-21 21:36:25 +00001585
Chris Lattner099b7792009-12-29 09:22:47 +00001586 if (D.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001587 return getDISubprogram(DILexicalBlock(Scope).getContext());
Jim Grosbache62b6902010-07-21 21:36:25 +00001588
Chris Lattner099b7792009-12-29 09:22:47 +00001589 return DISubprogram();
1590}
Devang Patel193f7202009-11-24 01:14:22 +00001591
Chris Lattner099b7792009-12-29 09:22:47 +00001592/// getDICompositeType - Find underlying composite type.
1593DICompositeType llvm::getDICompositeType(DIType T) {
Chris Lattner099b7792009-12-29 09:22:47 +00001594 if (T.isCompositeType())
Devang Patel2db49d72010-05-07 18:11:54 +00001595 return DICompositeType(T);
Jim Grosbache62b6902010-07-21 21:36:25 +00001596
Chris Lattner099b7792009-12-29 09:22:47 +00001597 if (T.isDerivedType())
Devang Patel2db49d72010-05-07 18:11:54 +00001598 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
Jim Grosbache62b6902010-07-21 21:36:25 +00001599
Chris Lattner099b7792009-12-29 09:22:47 +00001600 return DICompositeType();
Torok Edwin620f2802008-12-16 09:07:36 +00001601}