blob: 3d038159749f26dff3dea076328aab57456e6e87 [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"
David Greene0eb5b662009-12-23 19:45:49 +000025#include "llvm/Support/Debug.h"
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000026#include "llvm/Support/Dwarf.h"
Chris Lattnera81d29b2009-08-23 07:33:14 +000027#include "llvm/Support/raw_ostream.h"
Chris Lattnera45664f2008-11-10 02:56:27 +000028using namespace llvm;
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000029using namespace llvm::dwarf;
Chris Lattnera45664f2008-11-10 02:56:27 +000030
31//===----------------------------------------------------------------------===//
32// DIDescriptor
33//===----------------------------------------------------------------------===//
34
Devang Patel5b164b52010-08-02 22:51:46 +000035DIDescriptor::DIDescriptor(const DIFile F) : DbgNode(F.DbgNode) {
36}
37
38DIDescriptor::DIDescriptor(const DISubprogram F) : DbgNode(F.DbgNode) {
39}
40
41DIDescriptor::DIDescriptor(const DILexicalBlock F) : DbgNode(F.DbgNode) {
42}
43
44DIDescriptor::DIDescriptor(const DIVariable F) : DbgNode(F.DbgNode) {
45}
46
47DIDescriptor::DIDescriptor(const DIType F) : DbgNode(F.DbgNode) {
48}
49
Jim Grosbache62b6902010-07-21 21:36:25 +000050StringRef
Devang Patel5ccdd102009-09-29 18:40:58 +000051DIDescriptor::getStringField(unsigned Elt) const {
Devang Patele4b27562009-08-28 23:24:31 +000052 if (DbgNode == 0)
Devang Patel65dbc902009-11-25 17:36:49 +000053 return StringRef();
Chris Lattnera45664f2008-11-10 02:56:27 +000054
Chris Lattner5d0cacd2009-12-31 01:22:29 +000055 if (Elt < DbgNode->getNumOperands())
56 if (MDString *MDS = dyn_cast_or_null<MDString>(DbgNode->getOperand(Elt)))
Devang Patel65dbc902009-11-25 17:36:49 +000057 return MDS->getString();
Daniel Dunbarf612ff62009-09-19 20:40:05 +000058
Devang Patel65dbc902009-11-25 17:36:49 +000059 return StringRef();
Chris Lattnera45664f2008-11-10 02:56:27 +000060}
61
62uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +000063 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +000064 return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +000065
Chris Lattner5d0cacd2009-12-31 01:22:29 +000066 if (Elt < DbgNode->getNumOperands())
67 if (ConstantInt *CI = dyn_cast<ConstantInt>(DbgNode->getOperand(Elt)))
Devang Patele4b27562009-08-28 23:24:31 +000068 return CI->getZExtValue();
Daniel Dunbarf612ff62009-09-19 20:40:05 +000069
Chris Lattnera45664f2008-11-10 02:56:27 +000070 return 0;
71}
72
Chris Lattnera45664f2008-11-10 02:56:27 +000073DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +000074 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +000075 return DIDescriptor();
Bill Wendlingdc817b62009-05-14 18:26:15 +000076
Chris Lattner7a2f3e02010-03-31 05:53:47 +000077 if (Elt < DbgNode->getNumOperands())
Jim Grosbache62b6902010-07-21 21:36:25 +000078 return
79 DIDescriptor(dyn_cast_or_null<const MDNode>(DbgNode->getOperand(Elt)));
Devang Patele4b27562009-08-28 23:24:31 +000080 return DIDescriptor();
Chris Lattnera45664f2008-11-10 02:56:27 +000081}
82
83GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +000084 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +000085 return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +000086
Chris Lattner5d0cacd2009-12-31 01:22:29 +000087 if (Elt < DbgNode->getNumOperands())
88 return dyn_cast_or_null<GlobalVariable>(DbgNode->getOperand(Elt));
Devang Patele4b27562009-08-28 23:24:31 +000089 return 0;
Chris Lattnera45664f2008-11-10 02:56:27 +000090}
91
Devang Patel27398962010-08-09 21:39:24 +000092Constant *DIDescriptor::getConstantField(unsigned Elt) const {
93 if (DbgNode == 0)
94 return 0;
95
96 if (Elt < DbgNode->getNumOperands())
97 return dyn_cast_or_null<Constant>(DbgNode->getOperand(Elt));
98 return 0;
99}
100
Stuart Hastings215aa152010-06-11 20:08:44 +0000101Function *DIDescriptor::getFunctionField(unsigned Elt) const {
102 if (DbgNode == 0)
103 return 0;
104
105 if (Elt < DbgNode->getNumOperands())
106 return dyn_cast_or_null<Function>(DbgNode->getOperand(Elt));
107 return 0;
108}
109
Chris Lattnerf0908a32009-12-31 03:02:08 +0000110unsigned DIVariable::getNumAddrElements() const {
111 return DbgNode->getNumOperands()-6;
112}
113
114
Chris Lattnera45664f2008-11-10 02:56:27 +0000115//===----------------------------------------------------------------------===//
Devang Patel6ceea332009-08-31 18:49:10 +0000116// Predicates
Chris Lattnera45664f2008-11-10 02:56:27 +0000117//===----------------------------------------------------------------------===//
118
Devang Patel6ceea332009-08-31 18:49:10 +0000119/// isBasicType - Return true if the specified tag is legal for
120/// DIBasicType.
121bool DIDescriptor::isBasicType() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000122 return DbgNode && getTag() == dwarf::DW_TAG_base_type;
Torok Edwinb07fbd92008-12-13 08:25:29 +0000123}
Chris Lattnera45664f2008-11-10 02:56:27 +0000124
Devang Patel6ceea332009-08-31 18:49:10 +0000125/// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
126bool DIDescriptor::isDerivedType() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000127 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000128 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000129 case dwarf::DW_TAG_typedef:
130 case dwarf::DW_TAG_pointer_type:
131 case dwarf::DW_TAG_reference_type:
132 case dwarf::DW_TAG_const_type:
133 case dwarf::DW_TAG_volatile_type:
134 case dwarf::DW_TAG_restrict_type:
135 case dwarf::DW_TAG_member:
136 case dwarf::DW_TAG_inheritance:
137 return true;
138 default:
Devang Patele4b27562009-08-28 23:24:31 +0000139 // CompositeTypes are currently modelled as DerivedTypes.
Devang Patel6ceea332009-08-31 18:49:10 +0000140 return isCompositeType();
Chris Lattnera45664f2008-11-10 02:56:27 +0000141 }
142}
143
Chris Lattnera45664f2008-11-10 02:56:27 +0000144/// isCompositeType - Return true if the specified tag is legal for
145/// DICompositeType.
Devang Patel6ceea332009-08-31 18:49:10 +0000146bool DIDescriptor::isCompositeType() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000147 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000148 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000149 case dwarf::DW_TAG_array_type:
150 case dwarf::DW_TAG_structure_type:
151 case dwarf::DW_TAG_union_type:
152 case dwarf::DW_TAG_enumeration_type:
153 case dwarf::DW_TAG_vector_type:
154 case dwarf::DW_TAG_subroutine_type:
Devang Patel25cb0d72009-03-25 03:52:06 +0000155 case dwarf::DW_TAG_class_type:
Chris Lattnera45664f2008-11-10 02:56:27 +0000156 return true;
157 default:
158 return false;
159 }
160}
161
Chris Lattnera45664f2008-11-10 02:56:27 +0000162/// isVariable - Return true if the specified tag is legal for DIVariable.
Devang Patel6ceea332009-08-31 18:49:10 +0000163bool DIDescriptor::isVariable() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000164 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000165 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000166 case dwarf::DW_TAG_auto_variable:
167 case dwarf::DW_TAG_arg_variable:
168 case dwarf::DW_TAG_return_variable:
169 return true;
170 default:
171 return false;
172 }
173}
174
Devang Patelecbeb1a2009-09-30 22:34:41 +0000175/// isType - Return true if the specified tag is legal for DIType.
176bool DIDescriptor::isType() const {
177 return isBasicType() || isCompositeType() || isDerivedType();
178}
179
Devang Patel6ceea332009-08-31 18:49:10 +0000180/// isSubprogram - Return true if the specified tag is legal for
181/// DISubprogram.
182bool DIDescriptor::isSubprogram() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000183 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
Devang Patel6ceea332009-08-31 18:49:10 +0000184}
185
186/// isGlobalVariable - Return true if the specified tag is legal for
187/// DIGlobalVariable.
188bool DIDescriptor::isGlobalVariable() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000189 return DbgNode && getTag() == dwarf::DW_TAG_variable;
Devang Patel6ceea332009-08-31 18:49:10 +0000190}
191
Devang Patelecbeb1a2009-09-30 22:34:41 +0000192/// isGlobal - Return true if the specified tag is legal for DIGlobal.
193bool DIDescriptor::isGlobal() const {
194 return isGlobalVariable();
195}
196
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000197/// isScope - Return true if the specified tag is one of the scope
Devang Patel43d98b32009-08-31 20:44:45 +0000198/// related tag.
199bool DIDescriptor::isScope() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000200 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000201 switch (getTag()) {
202 case dwarf::DW_TAG_compile_unit:
203 case dwarf::DW_TAG_lexical_block:
204 case dwarf::DW_TAG_subprogram:
205 case dwarf::DW_TAG_namespace:
206 return true;
207 default:
208 break;
Devang Patel43d98b32009-08-31 20:44:45 +0000209 }
210 return false;
211}
Devang Patel6ceea332009-08-31 18:49:10 +0000212
Devang Patelc9f322d2009-08-31 21:34:44 +0000213/// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
214bool DIDescriptor::isCompileUnit() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000215 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
Devang Patelc9f322d2009-08-31 21:34:44 +0000216}
217
Devang Patel7aa81892010-03-08 22:27:22 +0000218/// isFile - Return true if the specified tag is DW_TAG_file_type.
219bool DIDescriptor::isFile() const {
220 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
221}
222
Devang Patel6404e4e2009-12-15 19:16:48 +0000223/// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
224bool DIDescriptor::isNameSpace() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000225 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
Devang Patel6404e4e2009-12-15 19:16:48 +0000226}
227
Devang Patel5e005d82009-08-31 22:00:15 +0000228/// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
229bool DIDescriptor::isLexicalBlock() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000230 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block;
Devang Patel5e005d82009-08-31 22:00:15 +0000231}
232
Devang Patelecbeb1a2009-09-30 22:34:41 +0000233/// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
234bool DIDescriptor::isSubrange() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000235 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
Devang Patelecbeb1a2009-09-30 22:34:41 +0000236}
237
238/// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
239bool DIDescriptor::isEnumerator() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000240 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
Devang Patelecbeb1a2009-09-30 22:34:41 +0000241}
242
Devang Patel6ceea332009-08-31 18:49:10 +0000243//===----------------------------------------------------------------------===//
244// Simple Descriptor Constructors and other Methods
245//===----------------------------------------------------------------------===//
246
Devang Patele9f8f5e2010-05-07 20:54:48 +0000247DIType::DIType(const MDNode *N) : DIScope(N) {
Devang Patel6ceea332009-08-31 18:49:10 +0000248 if (!N) return;
249 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
250 DbgNode = 0;
251 }
252}
253
Devang Patel68afdc32009-01-05 18:33:01 +0000254unsigned DIArray::getNumElements() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000255 if (!DbgNode)
256 return 0;
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000257 return DbgNode->getNumOperands();
Devang Patel68afdc32009-01-05 18:33:01 +0000258}
Chris Lattnera45664f2008-11-10 02:56:27 +0000259
Devang Patelc4999d72009-07-22 18:23:44 +0000260/// replaceAllUsesWith - Replace all uses of debug info referenced by
Dan Gohman872814a2010-07-21 18:54:18 +0000261/// this descriptor.
Devang Patelc4999d72009-07-22 18:23:44 +0000262void DIDerivedType::replaceAllUsesWith(DIDescriptor &D) {
Devang Patel3c91b052010-03-08 20:52:55 +0000263 if (!DbgNode)
Devang Patelc4999d72009-07-22 18:23:44 +0000264 return;
265
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000266 // Since we use a TrackingVH for the node, its easy for clients to manufacture
267 // legitimate situations where they want to replaceAllUsesWith() on something
268 // which, due to uniquing, has merged with the source. We shield clients from
269 // this detail by allowing a value to be replaced with replaceAllUsesWith()
270 // itself.
Devang Pateled66bf52010-05-07 18:19:32 +0000271 if (DbgNode != D) {
Devang Patele9f8f5e2010-05-07 20:54:48 +0000272 MDNode *Node = const_cast<MDNode*>(DbgNode);
273 const MDNode *DN = D;
274 const Value *V = cast_or_null<Value>(DN);
275 Node->replaceAllUsesWith(const_cast<Value*>(V));
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000276 }
Devang Patelc4999d72009-07-22 18:23:44 +0000277}
278
Devang Patelb79b5352009-01-19 23:21:49 +0000279/// Verify - Verify that a compile unit is well formed.
280bool DICompileUnit::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000281 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000282 return false;
Devang Patel65dbc902009-11-25 17:36:49 +0000283 StringRef N = getFilename();
284 if (N.empty())
Bill Wendling0582ae92009-03-13 04:39:26 +0000285 return false;
Devang Patelb79b5352009-01-19 23:21:49 +0000286 // It is possible that directory and produce string is empty.
Bill Wendling0582ae92009-03-13 04:39:26 +0000287 return true;
Devang Patelb79b5352009-01-19 23:21:49 +0000288}
289
290/// Verify - Verify that a type descriptor is well formed.
291bool DIType::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000292 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000293 return false;
Devang Patel3c91b052010-03-08 20:52:55 +0000294 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000295 return false;
296
297 DICompileUnit CU = getCompileUnit();
Devang Patel3c91b052010-03-08 20:52:55 +0000298 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000299 return false;
300 return true;
301}
302
303/// Verify - Verify that a composite type descriptor is well formed.
304bool DICompositeType::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000305 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000306 return false;
Devang Patel3c91b052010-03-08 20:52:55 +0000307 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000308 return false;
309
310 DICompileUnit CU = getCompileUnit();
Devang Patel3c91b052010-03-08 20:52:55 +0000311 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000312 return false;
313 return true;
314}
315
316/// Verify - Verify that a subprogram descriptor is well formed.
317bool DISubprogram::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000318 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000319 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000320
Devang Patel3c91b052010-03-08 20:52:55 +0000321 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000322 return false;
323
324 DICompileUnit CU = getCompileUnit();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000325 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000326 return false;
327
328 DICompositeType Ty = getType();
Devang Patel3c91b052010-03-08 20:52:55 +0000329 if (!Ty.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000330 return false;
331 return true;
332}
333
334/// Verify - Verify that a global variable descriptor is well formed.
335bool DIGlobalVariable::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000336 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000337 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000338
Devang Patel65dbc902009-11-25 17:36:49 +0000339 if (getDisplayName().empty())
Devang Patel84c73e92009-11-06 17:58:12 +0000340 return false;
341
Devang Patel3c91b052010-03-08 20:52:55 +0000342 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000343 return false;
344
345 DICompileUnit CU = getCompileUnit();
Devang Patel3c91b052010-03-08 20:52:55 +0000346 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000347 return false;
348
349 DIType Ty = getType();
350 if (!Ty.Verify())
351 return false;
352
Devang Patel27398962010-08-09 21:39:24 +0000353 if (!getGlobal() && !getConstant())
Devang Patelb79b5352009-01-19 23:21:49 +0000354 return false;
355
356 return true;
357}
358
359/// Verify - Verify that a variable descriptor is well formed.
360bool DIVariable::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000361 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000362 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000363
Devang Patel3c91b052010-03-08 20:52:55 +0000364 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000365 return false;
366
Devang Patel62077af2010-05-07 21:42:24 +0000367 if (!getCompileUnit().Verify())
368 return false;
369
Devang Patelb79b5352009-01-19 23:21:49 +0000370 DIType Ty = getType();
371 if (!Ty.Verify())
372 return false;
373
Devang Patelb79b5352009-01-19 23:21:49 +0000374 return true;
375}
376
Devang Patel3c91b052010-03-08 20:52:55 +0000377/// Verify - Verify that a location descriptor is well formed.
378bool DILocation::Verify() const {
379 if (!DbgNode)
380 return false;
Jim Grosbache62b6902010-07-21 21:36:25 +0000381
Devang Patel3c91b052010-03-08 20:52:55 +0000382 return DbgNode->getNumOperands() == 4;
383}
384
Devang Patel47e22652010-05-07 23:04:32 +0000385/// Verify - Verify that a namespace descriptor is well formed.
386bool DINameSpace::Verify() const {
387 if (!DbgNode)
388 return false;
389 if (getName().empty())
390 return false;
391 if (!getCompileUnit().Verify())
392 return false;
393 return true;
394}
395
Devang Patel36375ee2009-02-17 21:23:59 +0000396/// getOriginalTypeSize - If this type is derived from a base type then
397/// return base type size.
398uint64_t DIDerivedType::getOriginalTypeSize() const {
Devang Patel61ecbd12009-11-04 23:48:00 +0000399 unsigned Tag = getTag();
400 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
401 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
402 Tag == dwarf::DW_TAG_restrict_type) {
403 DIType BaseType = getTypeDerivedFrom();
Jim Grosbache62b6902010-07-21 21:36:25 +0000404 // If this type is not derived from any type then take conservative
Devang Patel5ebfa2d2009-11-06 18:24:05 +0000405 // approach.
Devang Patel3c91b052010-03-08 20:52:55 +0000406 if (!BaseType.isValid())
Devang Patel5ebfa2d2009-11-06 18:24:05 +0000407 return getSizeInBits();
Devang Patel61ecbd12009-11-04 23:48:00 +0000408 if (BaseType.isDerivedType())
Devang Patel2db49d72010-05-07 18:11:54 +0000409 return DIDerivedType(BaseType).getOriginalTypeSize();
Devang Patel61ecbd12009-11-04 23:48:00 +0000410 else
411 return BaseType.getSizeInBits();
412 }
Jim Grosbache62b6902010-07-21 21:36:25 +0000413
Devang Patel61ecbd12009-11-04 23:48:00 +0000414 return getSizeInBits();
Devang Patel36375ee2009-02-17 21:23:59 +0000415}
Devang Patelb79b5352009-01-19 23:21:49 +0000416
Jim Grosbache62b6902010-07-21 21:36:25 +0000417/// isInlinedFnArgument - Return true if this variable provides debugging
Devang Patel719f6a92010-04-29 20:40:36 +0000418/// information for an inlined function arguments.
419bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
420 assert(CurFn && "Invalid function");
421 if (!getContext().isSubprogram())
422 return false;
Jim Grosbache62b6902010-07-21 21:36:25 +0000423 // This variable is not inlined function argument if its scope
Devang Patel719f6a92010-04-29 20:40:36 +0000424 // does not describe current function.
Devang Patel2db49d72010-05-07 18:11:54 +0000425 return !(DISubprogram(getContext()).describes(CurFn));
Devang Patel719f6a92010-04-29 20:40:36 +0000426}
427
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000428/// describes - Return true if this subprogram provides debugging
429/// information for the function F.
430bool DISubprogram::describes(const Function *F) {
Chris Lattner099b7792009-12-29 09:22:47 +0000431 assert(F && "Invalid function");
Devang Patelffd33cd2010-06-16 06:42:02 +0000432 if (F == getFunction())
433 return true;
Devang Patel65dbc902009-11-25 17:36:49 +0000434 StringRef Name = getLinkageName();
435 if (Name.empty())
Devang Patel5ccdd102009-09-29 18:40:58 +0000436 Name = getName();
Devang Patel65dbc902009-11-25 17:36:49 +0000437 if (F->getName() == Name)
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000438 return true;
439 return false;
440}
441
Jim Grosbache62b6902010-07-21 21:36:25 +0000442unsigned DISubprogram::isOptimized() const {
Devang Patelccff8122010-04-30 19:38:23 +0000443 assert (DbgNode && "Invalid subprogram descriptor!");
444 if (DbgNode->getNumOperands() == 16)
445 return getUnsignedField(15);
446 return 0;
447}
448
Devang Patel65dbc902009-11-25 17:36:49 +0000449StringRef DIScope::getFilename() const {
Devang Patel77bf2952010-03-08 22:02:50 +0000450 if (!DbgNode)
451 return StringRef();
Jim Grosbache62b6902010-07-21 21:36:25 +0000452 if (isLexicalBlock())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000453 return DILexicalBlock(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000454 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000455 return DISubprogram(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000456 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000457 return DICompileUnit(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000458 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000459 return DINameSpace(DbgNode).getFilename();
Devang Patel77bf2952010-03-08 22:02:50 +0000460 if (isType())
461 return DIType(DbgNode).getFilename();
Devang Patel7aa81892010-03-08 22:27:22 +0000462 if (isFile())
463 return DIFile(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000464 assert(0 && "Invalid DIScope!");
Devang Patel65dbc902009-11-25 17:36:49 +0000465 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000466}
467
Devang Patel65dbc902009-11-25 17:36:49 +0000468StringRef DIScope::getDirectory() const {
Devang Patel77bf2952010-03-08 22:02:50 +0000469 if (!DbgNode)
470 return StringRef();
Jim Grosbache62b6902010-07-21 21:36:25 +0000471 if (isLexicalBlock())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000472 return DILexicalBlock(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000473 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000474 return DISubprogram(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000475 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000476 return DICompileUnit(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000477 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000478 return DINameSpace(DbgNode).getDirectory();
Devang Patel77bf2952010-03-08 22:02:50 +0000479 if (isType())
480 return DIType(DbgNode).getDirectory();
Devang Patel7aa81892010-03-08 22:27:22 +0000481 if (isFile())
482 return DIFile(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000483 assert(0 && "Invalid DIScope!");
Devang Patel65dbc902009-11-25 17:36:49 +0000484 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000485}
486
Chris Lattnera45664f2008-11-10 02:56:27 +0000487//===----------------------------------------------------------------------===//
Devang Patel7136a652009-07-01 22:10:23 +0000488// DIDescriptor: dump routines for all descriptors.
489//===----------------------------------------------------------------------===//
490
491
Dan Gohman50404362010-05-07 15:30:29 +0000492/// print - Print descriptor.
493void DIDescriptor::print(raw_ostream &OS) const {
494 OS << "[" << dwarf::TagString(getTag()) << "] ";
495 OS.write_hex((intptr_t) &*DbgNode) << ']';
Devang Patel7136a652009-07-01 22:10:23 +0000496}
497
Dan Gohman50404362010-05-07 15:30:29 +0000498/// print - Print compile unit.
499void DICompileUnit::print(raw_ostream &OS) const {
Devang Patel7136a652009-07-01 22:10:23 +0000500 if (getLanguage())
Dan Gohman50404362010-05-07 15:30:29 +0000501 OS << " [" << dwarf::LanguageString(getLanguage()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000502
Dan Gohmanfaa19c32010-05-10 20:07:44 +0000503 OS << " [" << getDirectory() << "/" << getFilename() << "]";
Devang Patel7136a652009-07-01 22:10:23 +0000504}
505
Dan Gohman50404362010-05-07 15:30:29 +0000506/// print - Print type.
507void DIType::print(raw_ostream &OS) const {
Devang Patel3c91b052010-03-08 20:52:55 +0000508 if (!DbgNode) return;
Devang Patel7136a652009-07-01 22:10:23 +0000509
Devang Patel65dbc902009-11-25 17:36:49 +0000510 StringRef Res = getName();
511 if (!Res.empty())
Dan Gohman50404362010-05-07 15:30:29 +0000512 OS << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000513
514 unsigned Tag = getTag();
Dan Gohman50404362010-05-07 15:30:29 +0000515 OS << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000516
517 // TODO : Print context
Dan Gohmanc014d092010-05-07 16:17:22 +0000518 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000519 OS << " ["
Dan Gohman9a7063e2010-05-07 16:39:27 +0000520 << "line " << getLineNumber() << ", "
521 << getSizeInBits() << " bits, "
522 << getAlignInBits() << " bit alignment, "
523 << getOffsetInBits() << " bit offset"
Chris Lattnera81d29b2009-08-23 07:33:14 +0000524 << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000525
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000526 if (isPrivate())
Dan Gohman50404362010-05-07 15:30:29 +0000527 OS << " [private] ";
Devang Patel7136a652009-07-01 22:10:23 +0000528 else if (isProtected())
Dan Gohman50404362010-05-07 15:30:29 +0000529 OS << " [protected] ";
Devang Patel7136a652009-07-01 22:10:23 +0000530
531 if (isForwardDecl())
Dan Gohman50404362010-05-07 15:30:29 +0000532 OS << " [fwd] ";
Devang Patel7136a652009-07-01 22:10:23 +0000533
Devang Patel6ceea332009-08-31 18:49:10 +0000534 if (isBasicType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000535 DIBasicType(DbgNode).print(OS);
Devang Patel6ceea332009-08-31 18:49:10 +0000536 else if (isDerivedType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000537 DIDerivedType(DbgNode).print(OS);
Devang Patel6ceea332009-08-31 18:49:10 +0000538 else if (isCompositeType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000539 DICompositeType(DbgNode).print(OS);
Devang Patel7136a652009-07-01 22:10:23 +0000540 else {
Dan Gohman50404362010-05-07 15:30:29 +0000541 OS << "Invalid DIType\n";
Devang Patel7136a652009-07-01 22:10:23 +0000542 return;
543 }
544
Dan Gohman50404362010-05-07 15:30:29 +0000545 OS << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000546}
547
Dan Gohman50404362010-05-07 15:30:29 +0000548/// print - Print basic type.
549void DIBasicType::print(raw_ostream &OS) const {
550 OS << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000551}
552
Dan Gohman50404362010-05-07 15:30:29 +0000553/// print - Print derived type.
554void DIDerivedType::print(raw_ostream &OS) const {
Dan Gohmanc014d092010-05-07 16:17:22 +0000555 OS << "\n\t Derived From: "; getTypeDerivedFrom().print(OS);
Devang Patel7136a652009-07-01 22:10:23 +0000556}
557
Dan Gohman50404362010-05-07 15:30:29 +0000558/// print - Print composite type.
559void DICompositeType::print(raw_ostream &OS) const {
Devang Patel7136a652009-07-01 22:10:23 +0000560 DIArray A = getTypeArray();
Dan Gohman50404362010-05-07 15:30:29 +0000561 OS << " [" << A.getNumElements() << " elements]";
Devang Patel7136a652009-07-01 22:10:23 +0000562}
563
Dan Gohman50404362010-05-07 15:30:29 +0000564/// print - Print subprogram.
565void DISubprogram::print(raw_ostream &OS) const {
Devang Patel65dbc902009-11-25 17:36:49 +0000566 StringRef Res = getName();
567 if (!Res.empty())
Dan Gohman50404362010-05-07 15:30:29 +0000568 OS << " [" << Res << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000569
570 unsigned Tag = getTag();
Dan Gohman50404362010-05-07 15:30:29 +0000571 OS << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000572
573 // TODO : Print context
Dan Gohmanc014d092010-05-07 16:17:22 +0000574 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000575 OS << " [" << getLineNumber() << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000576
577 if (isLocalToUnit())
Dan Gohman50404362010-05-07 15:30:29 +0000578 OS << " [local] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000579
580 if (isDefinition())
Dan Gohman50404362010-05-07 15:30:29 +0000581 OS << " [def] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000582
Dan Gohman50404362010-05-07 15:30:29 +0000583 OS << "\n";
584}
585
586/// print - Print global variable.
587void DIGlobalVariable::print(raw_ostream &OS) const {
588 OS << " [";
Devang Patela49d8772010-05-07 23:19:07 +0000589 StringRef Res = getName();
590 if (!Res.empty())
591 OS << " [" << Res << "] ";
592
593 unsigned Tag = getTag();
594 OS << " [" << dwarf::TagString(Tag) << "] ";
595
596 // TODO : Print context
597 getCompileUnit().print(OS);
598 OS << " [" << getLineNumber() << "] ";
599
600 if (isLocalToUnit())
601 OS << " [local] ";
602
603 if (isDefinition())
604 OS << " [def] ";
605
606 if (isGlobalVariable())
607 DIGlobalVariable(DbgNode).print(OS);
608 OS << "]\n";
Dan Gohman50404362010-05-07 15:30:29 +0000609}
610
611/// print - Print variable.
612void DIVariable::print(raw_ostream &OS) const {
613 StringRef Res = getName();
614 if (!Res.empty())
615 OS << " [" << Res << "] ";
616
Dan Gohmanc014d092010-05-07 16:17:22 +0000617 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000618 OS << " [" << getLineNumber() << "] ";
Dan Gohmanc014d092010-05-07 16:17:22 +0000619 getType().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000620 OS << "\n";
621
622 // FIXME: Dump complex addresses
623}
624
625/// dump - Print descriptor to dbgs() with a newline.
626void DIDescriptor::dump() const {
627 print(dbgs()); dbgs() << '\n';
628}
629
630/// dump - Print compile unit to dbgs() with a newline.
631void DICompileUnit::dump() const {
632 print(dbgs()); dbgs() << '\n';
633}
634
635/// dump - Print type to dbgs() with a newline.
636void DIType::dump() const {
637 print(dbgs()); dbgs() << '\n';
638}
639
640/// dump - Print basic type to dbgs() with a newline.
641void DIBasicType::dump() const {
642 print(dbgs()); dbgs() << '\n';
643}
644
645/// dump - Print derived type to dbgs() with a newline.
646void DIDerivedType::dump() const {
647 print(dbgs()); dbgs() << '\n';
648}
649
650/// dump - Print composite type to dbgs() with a newline.
651void DICompositeType::dump() const {
652 print(dbgs()); dbgs() << '\n';
653}
654
Dan Gohman50404362010-05-07 15:30:29 +0000655/// dump - Print subprogram to dbgs() with a newline.
656void DISubprogram::dump() const {
657 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000658}
659
660/// dump - Print global variable.
661void DIGlobalVariable::dump() const {
Dan Gohman50404362010-05-07 15:30:29 +0000662 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000663}
664
665/// dump - Print variable.
666void DIVariable::dump() const {
Dan Gohman50404362010-05-07 15:30:29 +0000667 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000668}
669
670//===----------------------------------------------------------------------===//
Chris Lattnera45664f2008-11-10 02:56:27 +0000671// DIFactory: Basic Helpers
672//===----------------------------------------------------------------------===//
673
Bill Wendlingdc817b62009-05-14 18:26:15 +0000674DIFactory::DIFactory(Module &m)
Victor Hernandez06203122010-01-23 00:03:28 +0000675 : M(m), VMContext(M.getContext()), DeclareFn(0), ValueFn(0) {}
Chris Lattner497a7a82008-11-10 04:10:34 +0000676
Chris Lattnera45664f2008-11-10 02:56:27 +0000677Constant *DIFactory::GetTagConstant(unsigned TAG) {
Devang Patel6906ba52009-01-20 19:22:03 +0000678 assert((TAG & LLVMDebugVersionMask) == 0 &&
Chris Lattnera45664f2008-11-10 02:56:27 +0000679 "Tag too large for debug encoding!");
Owen Anderson1d0be152009-08-13 21:58:54 +0000680 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
Chris Lattnera45664f2008-11-10 02:56:27 +0000681}
682
Chris Lattnera45664f2008-11-10 02:56:27 +0000683//===----------------------------------------------------------------------===//
684// DIFactory: Primary Constructors
685//===----------------------------------------------------------------------===//
686
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000687/// GetOrCreateArray - Create an descriptor for an array of descriptors.
Chris Lattnera45664f2008-11-10 02:56:27 +0000688/// This implicitly uniques the arrays created.
689DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
Devang Patele4b27562009-08-28 23:24:31 +0000690 SmallVector<Value*, 16> Elts;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000691
Devang Patele4b27562009-08-28 23:24:31 +0000692 if (NumTys == 0)
693 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)));
694 else
695 for (unsigned i = 0; i != NumTys; ++i)
Devang Patel2db49d72010-05-07 18:11:54 +0000696 Elts.push_back(Tys[i]);
Devang Patel82459882009-08-26 05:01:18 +0000697
Devang Patele4b27562009-08-28 23:24:31 +0000698 return DIArray(MDNode::get(VMContext,Elts.data(), Elts.size()));
Chris Lattnera45664f2008-11-10 02:56:27 +0000699}
700
701/// GetOrCreateSubrange - Create a descriptor for a value range. This
702/// implicitly uniques the values returned.
703DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
Devang Patele4b27562009-08-28 23:24:31 +0000704 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000705 GetTagConstant(dwarf::DW_TAG_subrange_type),
Owen Anderson1d0be152009-08-13 21:58:54 +0000706 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
707 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
Chris Lattnera45664f2008-11-10 02:56:27 +0000708 };
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000709
Devang Patele4b27562009-08-28 23:24:31 +0000710 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000711}
712
713
714
715/// CreateCompileUnit - Create a new descriptor for the specified compile
716/// unit. Note that this does not unique compile units within the module.
717DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
Devang Patel65dbc902009-11-25 17:36:49 +0000718 StringRef Filename,
719 StringRef Directory,
720 StringRef Producer,
Devang Pateldd9db662009-01-30 18:20:31 +0000721 bool isMain,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000722 bool isOptimized,
Devang Patel65dbc902009-11-25 17:36:49 +0000723 StringRef Flags,
Devang Patel13319ce2009-02-17 22:43:44 +0000724 unsigned RunTimeVer) {
Devang Patele4b27562009-08-28 23:24:31 +0000725 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000726 GetTagConstant(dwarf::DW_TAG_compile_unit),
Devang Patele4b27562009-08-28 23:24:31 +0000727 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Owen Anderson1d0be152009-08-13 21:58:54 +0000728 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
Devang Patele4b27562009-08-28 23:24:31 +0000729 MDString::get(VMContext, Filename),
730 MDString::get(VMContext, Directory),
731 MDString::get(VMContext, Producer),
Owen Anderson1d0be152009-08-13 21:58:54 +0000732 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
733 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
Devang Patele4b27562009-08-28 23:24:31 +0000734 MDString::get(VMContext, Flags),
Owen Anderson1d0be152009-08-13 21:58:54 +0000735 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
Chris Lattnera45664f2008-11-10 02:56:27 +0000736 };
Devang Patele4b27562009-08-28 23:24:31 +0000737
738 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000739}
740
Devang Patel7aa81892010-03-08 22:27:22 +0000741/// CreateFile - Create a new descriptor for the specified file.
742DIFile DIFactory::CreateFile(StringRef Filename,
743 StringRef Directory,
744 DICompileUnit CU) {
745 Value *Elts[] = {
746 GetTagConstant(dwarf::DW_TAG_file_type),
747 MDString::get(VMContext, Filename),
748 MDString::get(VMContext, Directory),
Devang Patel2db49d72010-05-07 18:11:54 +0000749 CU
Devang Patel7aa81892010-03-08 22:27:22 +0000750 };
751
752 return DIFile(MDNode::get(VMContext, &Elts[0], 4));
753}
754
Chris Lattnera45664f2008-11-10 02:56:27 +0000755/// CreateEnumerator - Create a single enumerator value.
Devang Patel65dbc902009-11-25 17:36:49 +0000756DIEnumerator DIFactory::CreateEnumerator(StringRef Name, uint64_t Val){
Devang Patele4b27562009-08-28 23:24:31 +0000757 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000758 GetTagConstant(dwarf::DW_TAG_enumerator),
Devang Patele4b27562009-08-28 23:24:31 +0000759 MDString::get(VMContext, Name),
Owen Anderson1d0be152009-08-13 21:58:54 +0000760 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
Chris Lattnera45664f2008-11-10 02:56:27 +0000761 };
Devang Patele4b27562009-08-28 23:24:31 +0000762 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000763}
764
765
766/// CreateBasicType - Create a basic type like int, float, etc.
767DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000768 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000769 DIFile F,
Chris Lattnera45664f2008-11-10 02:56:27 +0000770 unsigned LineNumber,
771 uint64_t SizeInBits,
772 uint64_t AlignInBits,
773 uint64_t OffsetInBits, unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000774 unsigned Encoding) {
Devang Patele4b27562009-08-28 23:24:31 +0000775 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000776 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patel2db49d72010-05-07 18:11:54 +0000777 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000778 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000779 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000780 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
781 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
782 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
783 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
784 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
785 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
Chris Lattnera45664f2008-11-10 02:56:27 +0000786 };
Devang Patele4b27562009-08-28 23:24:31 +0000787 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000788}
789
Devang Patelac16d442009-10-26 16:54:35 +0000790
791/// CreateBasicType - Create a basic type like int, float, etc.
792DIBasicType DIFactory::CreateBasicTypeEx(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000793 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000794 DIFile F,
Devang Patelac16d442009-10-26 16:54:35 +0000795 unsigned LineNumber,
796 Constant *SizeInBits,
797 Constant *AlignInBits,
798 Constant *OffsetInBits, unsigned Flags,
799 unsigned Encoding) {
800 Value *Elts[] = {
801 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patel2db49d72010-05-07 18:11:54 +0000802 Context,
Devang Patelac16d442009-10-26 16:54:35 +0000803 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000804 F,
Devang Patelac16d442009-10-26 16:54:35 +0000805 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
806 SizeInBits,
807 AlignInBits,
808 OffsetInBits,
809 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
810 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
811 };
812 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
813}
814
Devang Patelb4645642010-02-06 01:02:37 +0000815/// CreateArtificialType - Create a new DIType with "artificial" flag set.
816DIType DIFactory::CreateArtificialType(DIType Ty) {
817 if (Ty.isArtificial())
818 return Ty;
819
820 SmallVector<Value *, 9> Elts;
Devang Patel2db49d72010-05-07 18:11:54 +0000821 MDNode *N = Ty;
Devang Patelb4645642010-02-06 01:02:37 +0000822 assert (N && "Unexpected input DIType!");
823 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
824 if (Value *V = N->getOperand(i))
825 Elts.push_back(V);
826 else
827 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
828 }
829
830 unsigned CurFlags = Ty.getFlags();
831 CurFlags = CurFlags | DIType::FlagArtificial;
832
833 // Flags are stored at this slot.
834 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
835
836 return DIType(MDNode::get(VMContext, Elts.data(), Elts.size()));
837}
Devang Patelac16d442009-10-26 16:54:35 +0000838
Chris Lattnera45664f2008-11-10 02:56:27 +0000839/// CreateDerivedType - Create a derived type like const qualified type,
840/// pointer, typedef, etc.
841DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
842 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000843 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000844 DIFile F,
Chris Lattnera45664f2008-11-10 02:56:27 +0000845 unsigned LineNumber,
846 uint64_t SizeInBits,
847 uint64_t AlignInBits,
848 uint64_t OffsetInBits,
849 unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000850 DIType DerivedFrom) {
Devang Patele4b27562009-08-28 23:24:31 +0000851 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000852 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000853 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000854 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000855 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000856 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
857 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
858 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
859 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
860 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000861 DerivedFrom,
Chris Lattnera45664f2008-11-10 02:56:27 +0000862 };
Devang Patele4b27562009-08-28 23:24:31 +0000863 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000864}
865
Devang Patelac16d442009-10-26 16:54:35 +0000866
867/// CreateDerivedType - Create a derived type like const qualified type,
868/// pointer, typedef, etc.
869DIDerivedType DIFactory::CreateDerivedTypeEx(unsigned Tag,
870 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000871 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000872 DIFile F,
Devang Patelac16d442009-10-26 16:54:35 +0000873 unsigned LineNumber,
874 Constant *SizeInBits,
875 Constant *AlignInBits,
876 Constant *OffsetInBits,
877 unsigned Flags,
878 DIType DerivedFrom) {
879 Value *Elts[] = {
880 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000881 Context,
Devang Patelac16d442009-10-26 16:54:35 +0000882 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000883 F,
Devang Patelac16d442009-10-26 16:54:35 +0000884 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
885 SizeInBits,
886 AlignInBits,
887 OffsetInBits,
888 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000889 DerivedFrom,
Devang Patelac16d442009-10-26 16:54:35 +0000890 };
891 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
892}
893
894
Chris Lattnera45664f2008-11-10 02:56:27 +0000895/// CreateCompositeType - Create a composite type like array, struct, etc.
896DICompositeType DIFactory::CreateCompositeType(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,
Chris Lattnera45664f2008-11-10 02:56:27 +0000900 unsigned LineNumber,
901 uint64_t SizeInBits,
902 uint64_t AlignInBits,
903 uint64_t OffsetInBits,
904 unsigned Flags,
905 DIType DerivedFrom,
Devang Patel13319ce2009-02-17 22:43:44 +0000906 DIArray Elements,
Devang Patel0fd7f9d2010-01-26 21:14:59 +0000907 unsigned RuntimeLang,
908 MDNode *ContainingType) {
Owen Andersone277fed2009-07-07 16:31:25 +0000909
Devang Patele4b27562009-08-28 23:24:31 +0000910 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000911 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000912 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000913 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000914 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000915 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
916 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
917 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
918 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
919 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000920 DerivedFrom,
921 Elements,
Devang Patel0fd7f9d2010-01-26 21:14:59 +0000922 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
923 ContainingType
Chris Lattnera45664f2008-11-10 02:56:27 +0000924 };
Devang Patel0fd7f9d2010-01-26 21:14:59 +0000925 return DICompositeType(MDNode::get(VMContext, &Elts[0], 13));
Chris Lattnera45664f2008-11-10 02:56:27 +0000926}
927
928
Devang Patelac16d442009-10-26 16:54:35 +0000929/// CreateCompositeType - Create a composite type like array, struct, etc.
930DICompositeType DIFactory::CreateCompositeTypeEx(unsigned Tag,
931 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000932 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000933 DIFile F,
Devang Patelac16d442009-10-26 16:54:35 +0000934 unsigned LineNumber,
935 Constant *SizeInBits,
936 Constant *AlignInBits,
937 Constant *OffsetInBits,
938 unsigned Flags,
939 DIType DerivedFrom,
940 DIArray Elements,
941 unsigned RuntimeLang) {
942
943 Value *Elts[] = {
944 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000945 Context,
Devang Patelac16d442009-10-26 16:54:35 +0000946 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000947 F,
Devang Patelac16d442009-10-26 16:54:35 +0000948 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
949 SizeInBits,
950 AlignInBits,
951 OffsetInBits,
952 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000953 DerivedFrom,
954 Elements,
Devang Patelac16d442009-10-26 16:54:35 +0000955 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
956 };
957 return DICompositeType(MDNode::get(VMContext, &Elts[0], 12));
958}
959
960
Chris Lattnera45664f2008-11-10 02:56:27 +0000961/// CreateSubprogram - Create a new descriptor for the specified subprogram.
962/// See comments in DISubprogram for descriptions of these fields. This
963/// method does not unique the generated descriptors.
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000964DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000965 StringRef Name,
966 StringRef DisplayName,
967 StringRef LinkageName,
Devang Patel4b945502010-03-09 00:44:10 +0000968 DIFile F,
Devang Patel2e369932010-01-23 00:26:28 +0000969 unsigned LineNo, DIType Ty,
Chris Lattnera45664f2008-11-10 02:56:27 +0000970 bool isLocalToUnit,
Devang Patel5d11eb02009-12-03 19:11:07 +0000971 bool isDefinition,
972 unsigned VK, unsigned VIndex,
Devang Patel4e0d19d2010-02-03 19:57:19 +0000973 DIType ContainingType,
Devang Patelccff8122010-04-30 19:38:23 +0000974 bool isArtificial,
Stuart Hastings215aa152010-06-11 20:08:44 +0000975 bool isOptimized,
976 Function *Fn) {
Devang Patel854967e2008-12-17 22:39:29 +0000977
Devang Patele4b27562009-08-28 23:24:31 +0000978 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000979 GetTagConstant(dwarf::DW_TAG_subprogram),
Devang Patele4b27562009-08-28 23:24:31 +0000980 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Devang Patel2db49d72010-05-07 18:11:54 +0000981 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000982 MDString::get(VMContext, Name),
983 MDString::get(VMContext, DisplayName),
984 MDString::get(VMContext, LinkageName),
Devang Patel2db49d72010-05-07 18:11:54 +0000985 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000986 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2db49d72010-05-07 18:11:54 +0000987 Ty,
Owen Anderson1d0be152009-08-13 21:58:54 +0000988 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
Devang Patel5d11eb02009-12-03 19:11:07 +0000989 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
990 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
991 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
Devang Patel2db49d72010-05-07 18:11:54 +0000992 ContainingType,
Devang Patelccff8122010-04-30 19:38:23 +0000993 ConstantInt::get(Type::getInt1Ty(VMContext), isArtificial),
Stuart Hastings215aa152010-06-11 20:08:44 +0000994 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
995 Fn
Chris Lattnera45664f2008-11-10 02:56:27 +0000996 };
Devang Patelfd5fdc32010-06-28 05:53:08 +0000997 MDNode *Node = MDNode::get(VMContext, &Elts[0], 17);
998
999 // Create a named metadata so that we do not lose this mdnode.
1000 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
1001 NMD->addOperand(Node);
1002 return DISubprogram(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +00001003}
1004
Devang Patele3a18de2009-12-01 23:09:02 +00001005/// CreateSubprogramDefinition - Create new subprogram descriptor for the
Jim Grosbache62b6902010-07-21 21:36:25 +00001006/// given declaration.
1007DISubprogram DIFactory::CreateSubprogramDefinition(DISubprogram &SPDeclaration){
Devang Patele3a18de2009-12-01 23:09:02 +00001008 if (SPDeclaration.isDefinition())
Devang Patel2db49d72010-05-07 18:11:54 +00001009 return DISubprogram(SPDeclaration);
Devang Patele3a18de2009-12-01 23:09:02 +00001010
Devang Patel2db49d72010-05-07 18:11:54 +00001011 MDNode *DeclNode = SPDeclaration;
Devang Patele3a18de2009-12-01 23:09:02 +00001012 Value *Elts[] = {
1013 GetTagConstant(dwarf::DW_TAG_subprogram),
1014 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001015 DeclNode->getOperand(2), // Context
1016 DeclNode->getOperand(3), // Name
1017 DeclNode->getOperand(4), // DisplayName
1018 DeclNode->getOperand(5), // LinkageName
1019 DeclNode->getOperand(6), // CompileUnit
1020 DeclNode->getOperand(7), // LineNo
1021 DeclNode->getOperand(8), // Type
1022 DeclNode->getOperand(9), // isLocalToUnit
Stuart Hastings6d56b9f2010-06-05 00:39:29 +00001023 ConstantInt::get(Type::getInt1Ty(VMContext), true),
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001024 DeclNode->getOperand(11), // Virtuality
1025 DeclNode->getOperand(12), // VIndex
Devang Patel4e0d19d2010-02-03 19:57:19 +00001026 DeclNode->getOperand(13), // Containting Type
Devang Patelccff8122010-04-30 19:38:23 +00001027 DeclNode->getOperand(14), // isArtificial
Devang Patelacc6efa2010-06-27 21:04:31 +00001028 DeclNode->getOperand(15), // isOptimized
1029 SPDeclaration.getFunction()
Devang Patele3a18de2009-12-01 23:09:02 +00001030 };
Devang Patelfd5fdc32010-06-28 05:53:08 +00001031 MDNode *Node =MDNode::get(VMContext, &Elts[0], 16);
1032
1033 // Create a named metadata so that we do not lose this mdnode.
1034 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
1035 NMD->addOperand(Node);
1036 return DISubprogram(Node);
Devang Patele3a18de2009-12-01 23:09:02 +00001037}
1038
Chris Lattnera45664f2008-11-10 02:56:27 +00001039/// CreateGlobalVariable - Create a new descriptor for the specified global.
1040DIGlobalVariable
Devang Patel65dbc902009-11-25 17:36:49 +00001041DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
1042 StringRef DisplayName,
1043 StringRef LinkageName,
Devang Patel4b945502010-03-09 00:44:10 +00001044 DIFile F,
Devang Patel2e369932010-01-23 00:26:28 +00001045 unsigned LineNo, DIType Ty,bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +00001046 bool isDefinition, llvm::GlobalVariable *Val) {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001047 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001048 GetTagConstant(dwarf::DW_TAG_variable),
Devang Patele4b27562009-08-28 23:24:31 +00001049 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Devang Patel2db49d72010-05-07 18:11:54 +00001050 Context,
Devang Patele4b27562009-08-28 23:24:31 +00001051 MDString::get(VMContext, Name),
1052 MDString::get(VMContext, DisplayName),
1053 MDString::get(VMContext, LinkageName),
Devang Patel2db49d72010-05-07 18:11:54 +00001054 F,
Owen Anderson1d0be152009-08-13 21:58:54 +00001055 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001056 Ty,
Owen Anderson1d0be152009-08-13 21:58:54 +00001057 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1058 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
Devang Patele4b27562009-08-28 23:24:31 +00001059 Val
Chris Lattnera45664f2008-11-10 02:56:27 +00001060 };
Devang Patele4b27562009-08-28 23:24:31 +00001061
1062 Value *const *Vs = &Elts[0];
1063 MDNode *Node = MDNode::get(VMContext,Vs, 12);
1064
1065 // Create a named metadata so that we do not lose this mdnode.
1066 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001067 NMD->addOperand(Node);
Devang Patele4b27562009-08-28 23:24:31 +00001068
1069 return DIGlobalVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +00001070}
1071
Devang Patel27398962010-08-09 21:39:24 +00001072/// CreateGlobalVariable - Create a new descriptor for the specified constant.
1073DIGlobalVariable
1074DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
1075 StringRef DisplayName,
1076 StringRef LinkageName,
1077 DIFile F,
1078 unsigned LineNo, DIType Ty,bool isLocalToUnit,
1079 bool isDefinition, llvm::Constant *Val) {
1080 Value *Elts[] = {
1081 GetTagConstant(dwarf::DW_TAG_variable),
1082 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
1083 Context,
1084 MDString::get(VMContext, Name),
1085 MDString::get(VMContext, DisplayName),
1086 MDString::get(VMContext, LinkageName),
1087 F,
1088 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1089 Ty,
1090 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1091 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1092 Val
1093 };
1094
1095 Value *const *Vs = &Elts[0];
1096 MDNode *Node = MDNode::get(VMContext,Vs, 12);
1097
1098 // Create a named metadata so that we do not lose this mdnode.
1099 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
1100 NMD->addOperand(Node);
1101
1102 return DIGlobalVariable(Node);
1103}
Chris Lattnera45664f2008-11-10 02:56:27 +00001104
1105/// CreateVariable - Create a new descriptor for the specified variable.
1106DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +00001107 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +00001108 DIFile F,
1109 unsigned LineNo,
Devang Patel6ed0ce32010-05-20 20:35:24 +00001110 DIType Ty, bool AlwaysPreserve) {
Devang Patele4b27562009-08-28 23:24:31 +00001111 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001112 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +00001113 Context,
Devang Patele4b27562009-08-28 23:24:31 +00001114 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +00001115 F,
Owen Anderson1d0be152009-08-13 21:58:54 +00001116 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001117 Ty,
Chris Lattnera45664f2008-11-10 02:56:27 +00001118 };
Devang Patel98e1cac2010-05-14 21:01:35 +00001119 MDNode *Node = MDNode::get(VMContext, &Elts[0], 6);
Devang Patel6ed0ce32010-05-20 20:35:24 +00001120 if (AlwaysPreserve) {
1121 // The optimizer may remove local variable. If there is an interest
1122 // to preserve variable info in such situation then stash it in a
1123 // named mdnode.
Devang Patel2f7d5292010-06-16 00:53:55 +00001124 DISubprogram Fn(getDISubprogram(Context));
Devang Patel10de3bb2010-06-21 18:36:58 +00001125 StringRef FName = "fn";
1126 if (Fn.getFunction())
1127 FName = Fn.getFunction()->getName();
Devang Patel10de3bb2010-06-21 18:36:58 +00001128 char One = '\1';
1129 if (FName.startswith(StringRef(&One, 1)))
1130 FName = FName.substr(1);
Dan Gohman17aa92c2010-07-21 23:38:33 +00001131
1132 SmallString<32> Out;
1133 NamedMDNode *FnLocals =
1134 M.getOrInsertNamedMetadata(Twine("llvm.dbg.lv.", FName).toStringRef(Out));
Devang Patel2f7d5292010-06-16 00:53:55 +00001135 FnLocals->addOperand(Node);
Devang Patel98e1cac2010-05-14 21:01:35 +00001136 }
1137 return DIVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +00001138}
1139
1140
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001141/// CreateComplexVariable - Create a new descriptor for the specified variable
1142/// which has a complex address expression for its address.
1143DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
1144 const std::string &Name,
Devang Patel4b945502010-03-09 00:44:10 +00001145 DIFile F,
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001146 unsigned LineNo,
Jim Grosbache62b6902010-07-21 21:36:25 +00001147 DIType Ty,
Devang Patel2e369932010-01-23 00:26:28 +00001148 SmallVector<Value *, 9> &addr) {
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001149 SmallVector<Value *, 9> Elts;
1150 Elts.push_back(GetTagConstant(Tag));
Devang Patel2db49d72010-05-07 18:11:54 +00001151 Elts.push_back(Context);
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001152 Elts.push_back(MDString::get(VMContext, Name));
Devang Patel2db49d72010-05-07 18:11:54 +00001153 Elts.push_back(F);
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001154 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
Devang Patel2db49d72010-05-07 18:11:54 +00001155 Elts.push_back(Ty);
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001156 Elts.insert(Elts.end(), addr.begin(), addr.end());
1157
1158 return DIVariable(MDNode::get(VMContext, &Elts[0], 6+addr.size()));
1159}
1160
1161
Chris Lattnera45664f2008-11-10 02:56:27 +00001162/// CreateBlock - This creates a descriptor for a lexical block with the
Owen Anderson99035272009-07-07 17:12:53 +00001163/// specified parent VMContext.
Devang Patel3d821aa2010-02-16 21:39:34 +00001164DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context,
Stuart Hastings0db42712010-07-19 23:56:30 +00001165 DIFile F, unsigned LineNo,
1166 unsigned Col) {
1167 // Defeat MDNode uniqing for lexical blocks.
1168 static unsigned int unique_id = 0;
Devang Patele4b27562009-08-28 23:24:31 +00001169 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001170 GetTagConstant(dwarf::DW_TAG_lexical_block),
Devang Patel2db49d72010-05-07 18:11:54 +00001171 Context,
Devang Patel3d821aa2010-02-16 21:39:34 +00001172 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Stuart Hastings0db42712010-07-19 23:56:30 +00001173 ConstantInt::get(Type::getInt32Ty(VMContext), Col),
1174 F,
1175 ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
Chris Lattnera45664f2008-11-10 02:56:27 +00001176 };
Stuart Hastings0db42712010-07-19 23:56:30 +00001177 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 6));
Chris Lattnera45664f2008-11-10 02:56:27 +00001178}
1179
Devang Patel6404e4e2009-12-15 19:16:48 +00001180/// CreateNameSpace - This creates new descriptor for a namespace
1181/// with the specified parent context.
1182DINameSpace DIFactory::CreateNameSpace(DIDescriptor Context, StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +00001183 DIFile F,
Devang Patel6404e4e2009-12-15 19:16:48 +00001184 unsigned LineNo) {
1185 Value *Elts[] = {
1186 GetTagConstant(dwarf::DW_TAG_namespace),
Devang Patel2db49d72010-05-07 18:11:54 +00001187 Context,
Devang Patel6404e4e2009-12-15 19:16:48 +00001188 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +00001189 F,
Devang Patel6404e4e2009-12-15 19:16:48 +00001190 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1191 };
1192 return DINameSpace(MDNode::get(VMContext, &Elts[0], 5));
1193}
1194
Devang Patelf98d8fe2009-09-01 01:14:15 +00001195/// CreateLocation - Creates a debug info location.
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001196DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
Daniel Dunbara279bc32009-09-20 02:20:51 +00001197 DIScope S, DILocation OrigLoc) {
Devang Patelf98d8fe2009-09-01 01:14:15 +00001198 Value *Elts[] = {
1199 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1200 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001201 S,
1202 OrigLoc,
Devang Patelf98d8fe2009-09-01 01:14:15 +00001203 };
1204 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1205}
1206
Chris Lattnera45664f2008-11-10 02:56:27 +00001207//===----------------------------------------------------------------------===//
1208// DIFactory: Routines for inserting code into a function
1209//===----------------------------------------------------------------------===//
1210
Chris Lattnera45664f2008-11-10 02:56:27 +00001211/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Devang Patel6daf99b2009-11-10 22:05:35 +00001212Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001213 Instruction *InsertBefore) {
Victor Hernandez4cf292a2010-01-26 02:07:38 +00001214 assert(Storage && "no storage passed to dbg.declare");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001215 assert(D.Verify() && "empty DIVariable passed to dbg.declare");
Chris Lattnera45664f2008-11-10 02:56:27 +00001216 if (!DeclareFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +00001217 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1218
Victor Hernandez756462b2010-01-18 20:42:09 +00001219 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
Devang Patel2db49d72010-05-07 18:11:54 +00001220 D };
Devang Patel6daf99b2009-11-10 22:05:35 +00001221 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
Mike Stumpe4250392009-10-01 22:08:58 +00001222}
1223
1224/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Devang Patel6daf99b2009-11-10 22:05:35 +00001225Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001226 BasicBlock *InsertAtEnd) {
Victor Hernandez4cf292a2010-01-26 02:07:38 +00001227 assert(Storage && "no storage passed to dbg.declare");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001228 assert(D.Verify() && "invalid DIVariable passed to dbg.declare");
Mike Stumpe4250392009-10-01 22:08:58 +00001229 if (!DeclareFn)
1230 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1231
Victor Hernandez756462b2010-01-18 20:42:09 +00001232 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
Devang Patel2db49d72010-05-07 18:11:54 +00001233 D };
Devang Patel27a53de2010-01-29 18:30:57 +00001234
1235 // If this block already has a terminator then insert this intrinsic
1236 // before the terminator.
Jim Grosbache62b6902010-07-21 21:36:25 +00001237 if (TerminatorInst *T = InsertAtEnd->getTerminator())
Devang Patel27a53de2010-01-29 18:30:57 +00001238 return CallInst::Create(DeclareFn, Args, Args+2, "", T);
1239 else
1240 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);}
Torok Edwin620f2802008-12-16 09:07:36 +00001241
Victor Hernandezc59b3352009-12-07 21:54:43 +00001242/// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001243Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
Victor Hernandezc59b3352009-12-07 21:54:43 +00001244 DIVariable D,
1245 Instruction *InsertBefore) {
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001246 assert(V && "no value passed to dbg.value");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001247 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001248 if (!ValueFn)
1249 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1250
Victor Hernandezc8b7cd02010-01-20 05:44:11 +00001251 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001252 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
Devang Patel2db49d72010-05-07 18:11:54 +00001253 D };
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001254 return CallInst::Create(ValueFn, Args, Args+3, "", InsertBefore);
1255}
1256
Victor Hernandezc59b3352009-12-07 21:54:43 +00001257/// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001258Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
Victor Hernandezc59b3352009-12-07 21:54:43 +00001259 DIVariable D,
1260 BasicBlock *InsertAtEnd) {
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001261 assert(V && "no value passed to dbg.value");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001262 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001263 if (!ValueFn)
1264 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1265
Jim Grosbache62b6902010-07-21 21:36:25 +00001266 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001267 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
Devang Patel2db49d72010-05-07 18:11:54 +00001268 D };
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001269 return CallInst::Create(ValueFn, Args, Args+3, "", InsertAtEnd);
1270}
Devang Patele4b27562009-08-28 23:24:31 +00001271
Devang Pateld2f79a12009-07-28 19:55:13 +00001272//===----------------------------------------------------------------------===//
Devang Patel98c65172009-07-30 18:25:15 +00001273// DebugInfoFinder implementations.
Devang Pateld2f79a12009-07-28 19:55:13 +00001274//===----------------------------------------------------------------------===//
1275
Devang Patel98c65172009-07-30 18:25:15 +00001276/// processModule - Process entire module and collect debug info.
1277void DebugInfoFinder::processModule(Module &M) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001278 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1279 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
1280 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
1281 ++BI) {
Devang Patel01c5ff62010-05-04 01:05:02 +00001282 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
Devang Patelb4d31302009-07-31 18:18:52 +00001283 processDeclare(DDI);
Jim Grosbache62b6902010-07-21 21:36:25 +00001284
Chris Lattner28a9bf62010-04-02 20:44:29 +00001285 DebugLoc Loc = BI->getDebugLoc();
1286 if (Loc.isUnknown())
1287 continue;
Jim Grosbache62b6902010-07-21 21:36:25 +00001288
Chris Lattner28a9bf62010-04-02 20:44:29 +00001289 LLVMContext &Ctx = BI->getContext();
1290 DIDescriptor Scope(Loc.getScope(Ctx));
Jim Grosbache62b6902010-07-21 21:36:25 +00001291
Chris Lattner28a9bf62010-04-02 20:44:29 +00001292 if (Scope.isCompileUnit())
Devang Patel2db49d72010-05-07 18:11:54 +00001293 addCompileUnit(DICompileUnit(Scope));
Chris Lattner28a9bf62010-04-02 20:44:29 +00001294 else if (Scope.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001295 processSubprogram(DISubprogram(Scope));
Chris Lattner28a9bf62010-04-02 20:44:29 +00001296 else if (Scope.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001297 processLexicalBlock(DILexicalBlock(Scope));
Jim Grosbache62b6902010-07-21 21:36:25 +00001298
Chris Lattner28a9bf62010-04-02 20:44:29 +00001299 if (MDNode *IA = Loc.getInlinedAt(Ctx))
1300 processLocation(DILocation(IA));
Devang Pateld2f79a12009-07-28 19:55:13 +00001301 }
Devang Patele4b27562009-08-28 23:24:31 +00001302
Devang Patelfd5fdc32010-06-28 05:53:08 +00001303 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
1304 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1305 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
1306 if (addGlobalVariable(DIG)) {
1307 addCompileUnit(DIG.getCompileUnit());
1308 processType(DIG.getType());
1309 }
Devang Pateld2f79a12009-07-28 19:55:13 +00001310 }
1311 }
Devang Patelfd5fdc32010-06-28 05:53:08 +00001312
1313 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
1314 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
1315 processSubprogram(DISubprogram(NMD->getOperand(i)));
Devang Pateld2f79a12009-07-28 19:55:13 +00001316}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001317
Devang Patel6daf99b2009-11-10 22:05:35 +00001318/// processLocation - Process DILocation.
1319void DebugInfoFinder::processLocation(DILocation Loc) {
Devang Patel3c91b052010-03-08 20:52:55 +00001320 if (!Loc.Verify()) return;
Devang Patel2db49d72010-05-07 18:11:54 +00001321 DIDescriptor S(Loc.getScope());
Devang Patel6daf99b2009-11-10 22:05:35 +00001322 if (S.isCompileUnit())
Devang Patel2db49d72010-05-07 18:11:54 +00001323 addCompileUnit(DICompileUnit(S));
Devang Patel6daf99b2009-11-10 22:05:35 +00001324 else if (S.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001325 processSubprogram(DISubprogram(S));
Devang Patel6daf99b2009-11-10 22:05:35 +00001326 else if (S.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001327 processLexicalBlock(DILexicalBlock(S));
Devang Patel6daf99b2009-11-10 22:05:35 +00001328 processLocation(Loc.getOrigLocation());
1329}
1330
Devang Patel98c65172009-07-30 18:25:15 +00001331/// processType - Process DIType.
1332void DebugInfoFinder::processType(DIType DT) {
Devang Patel72bcdb62009-08-10 22:09:58 +00001333 if (!addType(DT))
Devang Pateld2f79a12009-07-28 19:55:13 +00001334 return;
1335
1336 addCompileUnit(DT.getCompileUnit());
Devang Patel6ceea332009-08-31 18:49:10 +00001337 if (DT.isCompositeType()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001338 DICompositeType DCT(DT);
Devang Patel98c65172009-07-30 18:25:15 +00001339 processType(DCT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001340 DIArray DA = DCT.getTypeArray();
Devang Patel3c91b052010-03-08 20:52:55 +00001341 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1342 DIDescriptor D = DA.getElement(i);
1343 if (D.isType())
Devang Patel2db49d72010-05-07 18:11:54 +00001344 processType(DIType(D));
Devang Patel3c91b052010-03-08 20:52:55 +00001345 else if (D.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001346 processSubprogram(DISubprogram(D));
Devang Patel3c91b052010-03-08 20:52:55 +00001347 }
Devang Patel6ceea332009-08-31 18:49:10 +00001348 } else if (DT.isDerivedType()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001349 DIDerivedType DDT(DT);
Devang Patel3c91b052010-03-08 20:52:55 +00001350 processType(DDT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001351 }
1352}
1353
Devang Patelbeab41b2009-10-07 22:04:08 +00001354/// processLexicalBlock
1355void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
Devang Patelbeab41b2009-10-07 22:04:08 +00001356 DIScope Context = LB.getContext();
1357 if (Context.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001358 return processLexicalBlock(DILexicalBlock(Context));
Devang Patelbeab41b2009-10-07 22:04:08 +00001359 else
Devang Patel2db49d72010-05-07 18:11:54 +00001360 return processSubprogram(DISubprogram(Context));
Devang Patelbeab41b2009-10-07 22:04:08 +00001361}
1362
Devang Patel98c65172009-07-30 18:25:15 +00001363/// processSubprogram - Process DISubprogram.
1364void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001365 if (!addSubprogram(SP))
1366 return;
1367 addCompileUnit(SP.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +00001368 processType(SP.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +00001369}
1370
Devang Patelb4d31302009-07-31 18:18:52 +00001371/// processDeclare - Process DbgDeclareInst.
1372void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
Devang Patel3c91b052010-03-08 20:52:55 +00001373 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1374 if (!N) return;
1375
1376 DIDescriptor DV(N);
1377 if (!DV.isVariable())
Devang Patelb4d31302009-07-31 18:18:52 +00001378 return;
1379
Devang Patel2db49d72010-05-07 18:11:54 +00001380 if (!NodesSeen.insert(DV))
Devang Patelb4d31302009-07-31 18:18:52 +00001381 return;
1382
Devang Patel3c91b052010-03-08 20:52:55 +00001383 addCompileUnit(DIVariable(N).getCompileUnit());
1384 processType(DIVariable(N).getType());
Devang Patelb4d31302009-07-31 18:18:52 +00001385}
1386
Devang Patel72bcdb62009-08-10 22:09:58 +00001387/// addType - Add type into Tys.
1388bool DebugInfoFinder::addType(DIType DT) {
Devang Patel3c91b052010-03-08 20:52:55 +00001389 if (!DT.isValid())
Devang Patel72bcdb62009-08-10 22:09:58 +00001390 return false;
1391
Devang Patel2db49d72010-05-07 18:11:54 +00001392 if (!NodesSeen.insert(DT))
Devang Patel72bcdb62009-08-10 22:09:58 +00001393 return false;
1394
Devang Patel2db49d72010-05-07 18:11:54 +00001395 TYs.push_back(DT);
Devang Patel72bcdb62009-08-10 22:09:58 +00001396 return true;
1397}
1398
Devang Pateld2f79a12009-07-28 19:55:13 +00001399/// addCompileUnit - Add compile unit into CUs.
Devang Patel98c65172009-07-30 18:25:15 +00001400bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Patel3c91b052010-03-08 20:52:55 +00001401 if (!CU.Verify())
Devang Pateld2f79a12009-07-28 19:55:13 +00001402 return false;
1403
Devang Patel2db49d72010-05-07 18:11:54 +00001404 if (!NodesSeen.insert(CU))
Devang Pateld2f79a12009-07-28 19:55:13 +00001405 return false;
1406
Devang Patel2db49d72010-05-07 18:11:54 +00001407 CUs.push_back(CU);
Devang Pateld2f79a12009-07-28 19:55:13 +00001408 return true;
1409}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001410
Devang Pateld2f79a12009-07-28 19:55:13 +00001411/// addGlobalVariable - Add global variable into GVs.
Devang Patel98c65172009-07-30 18:25:15 +00001412bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Patel2db49d72010-05-07 18:11:54 +00001413 if (!DIDescriptor(DIG).isGlobalVariable())
Devang Pateld2f79a12009-07-28 19:55:13 +00001414 return false;
1415
Devang Patel2db49d72010-05-07 18:11:54 +00001416 if (!NodesSeen.insert(DIG))
Devang Pateld2f79a12009-07-28 19:55:13 +00001417 return false;
1418
Devang Patel2db49d72010-05-07 18:11:54 +00001419 GVs.push_back(DIG);
Devang Pateld2f79a12009-07-28 19:55:13 +00001420 return true;
1421}
1422
1423// addSubprogram - Add subprgoram into SPs.
Devang Patel98c65172009-07-30 18:25:15 +00001424bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Patel2db49d72010-05-07 18:11:54 +00001425 if (!DIDescriptor(SP).isSubprogram())
Devang Pateld2f79a12009-07-28 19:55:13 +00001426 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001427
Devang Patel2db49d72010-05-07 18:11:54 +00001428 if (!NodesSeen.insert(SP))
Devang Pateld2f79a12009-07-28 19:55:13 +00001429 return false;
1430
Devang Patel2db49d72010-05-07 18:11:54 +00001431 SPs.push_back(SP);
Devang Pateld2f79a12009-07-28 19:55:13 +00001432 return true;
1433}
1434
Victor Hernandez756462b2010-01-18 20:42:09 +00001435/// Find the debug info descriptor corresponding to this global variable.
1436static Value *findDbgGlobalDeclare(GlobalVariable *V) {
Chris Lattner099b7792009-12-29 09:22:47 +00001437 const Module *M = V->getParent();
1438 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1439 if (!NMD)
Torok Edwinff7d0e92009-03-10 13:41:26 +00001440 return 0;
Chris Lattner099b7792009-12-29 09:22:47 +00001441
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001442 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman872814a2010-07-21 18:54:18 +00001443 DIDescriptor DIG(cast<MDNode>(NMD->getOperand(i)));
Devang Patel3c91b052010-03-08 20:52:55 +00001444 if (!DIG.isGlobalVariable())
Chris Lattner099b7792009-12-29 09:22:47 +00001445 continue;
Devang Patel2db49d72010-05-07 18:11:54 +00001446 if (DIGlobalVariable(DIG).getGlobal() == V)
1447 return DIG;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001448 }
Chris Lattner099b7792009-12-29 09:22:47 +00001449 return 0;
1450}
Torok Edwinff7d0e92009-03-10 13:41:26 +00001451
Chris Lattner099b7792009-12-29 09:22:47 +00001452/// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
1453/// It looks through pointer casts too.
Victor Hernandez756462b2010-01-18 20:42:09 +00001454static const DbgDeclareInst *findDbgDeclare(const Value *V) {
Victor Hernandez3a328652010-01-15 19:04:09 +00001455 V = V->stripPointerCasts();
Jim Grosbache62b6902010-07-21 21:36:25 +00001456
Victor Hernandez3a328652010-01-15 19:04:09 +00001457 if (!isa<Instruction>(V) && !isa<Argument>(V))
Torok Edwin620f2802008-12-16 09:07:36 +00001458 return 0;
Jim Grosbache62b6902010-07-21 21:36:25 +00001459
Victor Hernandez3a328652010-01-15 19:04:09 +00001460 const Function *F = NULL;
1461 if (const Instruction *I = dyn_cast<Instruction>(V))
1462 F = I->getParent()->getParent();
1463 else if (const Argument *A = dyn_cast<Argument>(V))
1464 F = A->getParent();
Jim Grosbache62b6902010-07-21 21:36:25 +00001465
Victor Hernandez3a328652010-01-15 19:04:09 +00001466 for (Function::const_iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
1467 for (BasicBlock::const_iterator BI = (*FI).begin(), BE = (*FI).end();
1468 BI != BE; ++BI)
1469 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1470 if (DDI->getAddress() == V)
1471 return DDI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001472
Chris Lattner099b7792009-12-29 09:22:47 +00001473 return 0;
1474}
Bill Wendlingdc817b62009-05-14 18:26:15 +00001475
Chris Lattner099b7792009-12-29 09:22:47 +00001476bool llvm::getLocationInfo(const Value *V, std::string &DisplayName,
1477 std::string &Type, unsigned &LineNo,
1478 std::string &File, std::string &Dir) {
1479 DICompileUnit Unit;
1480 DIType TypeD;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001481
Chris Lattner099b7792009-12-29 09:22:47 +00001482 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1483 Value *DIGV = findDbgGlobalDeclare(GV);
1484 if (!DIGV) return false;
1485 DIGlobalVariable Var(cast<MDNode>(DIGV));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001486
Chris Lattner099b7792009-12-29 09:22:47 +00001487 StringRef D = Var.getDisplayName();
Devang Patel65dbc902009-11-25 17:36:49 +00001488 if (!D.empty())
Chris Lattner099b7792009-12-29 09:22:47 +00001489 DisplayName = D;
1490 LineNo = Var.getLineNumber();
1491 Unit = Var.getCompileUnit();
1492 TypeD = Var.getType();
1493 } else {
1494 const DbgDeclareInst *DDI = findDbgDeclare(V);
1495 if (!DDI) return false;
1496 DIVariable Var(cast<MDNode>(DDI->getVariable()));
1497
1498 StringRef D = Var.getName();
1499 if (!D.empty())
1500 DisplayName = D;
1501 LineNo = Var.getLineNumber();
1502 Unit = Var.getCompileUnit();
1503 TypeD = Var.getType();
Torok Edwinff7d0e92009-03-10 13:41:26 +00001504 }
Devang Patel13e16b62009-06-26 01:49:18 +00001505
Chris Lattner099b7792009-12-29 09:22:47 +00001506 StringRef T = TypeD.getName();
1507 if (!T.empty())
1508 Type = T;
1509 StringRef F = Unit.getFilename();
1510 if (!F.empty())
1511 File = F;
1512 StringRef D = Unit.getDirectory();
1513 if (!D.empty())
1514 Dir = D;
1515 return true;
1516}
Devang Patel9e529c32009-07-02 01:15:24 +00001517
Chris Lattner099b7792009-12-29 09:22:47 +00001518/// getDISubprogram - Find subprogram that is enclosing this scope.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001519DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
Chris Lattner099b7792009-12-29 09:22:47 +00001520 DIDescriptor D(Scope);
Chris Lattner099b7792009-12-29 09:22:47 +00001521 if (D.isSubprogram())
1522 return DISubprogram(Scope);
Jim Grosbache62b6902010-07-21 21:36:25 +00001523
Chris Lattner099b7792009-12-29 09:22:47 +00001524 if (D.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001525 return getDISubprogram(DILexicalBlock(Scope).getContext());
Jim Grosbache62b6902010-07-21 21:36:25 +00001526
Chris Lattner099b7792009-12-29 09:22:47 +00001527 return DISubprogram();
1528}
Devang Patel193f7202009-11-24 01:14:22 +00001529
Chris Lattner099b7792009-12-29 09:22:47 +00001530/// getDICompositeType - Find underlying composite type.
1531DICompositeType llvm::getDICompositeType(DIType T) {
Chris Lattner099b7792009-12-29 09:22:47 +00001532 if (T.isCompositeType())
Devang Patel2db49d72010-05-07 18:11:54 +00001533 return DICompositeType(T);
Jim Grosbache62b6902010-07-21 21:36:25 +00001534
Chris Lattner099b7792009-12-29 09:22:47 +00001535 if (T.isDerivedType())
Devang Patel2db49d72010-05-07 18:11:54 +00001536 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
Jim Grosbache62b6902010-07-21 21:36:25 +00001537
Chris Lattner099b7792009-12-29 09:22:47 +00001538 return DICompositeType();
Torok Edwin620f2802008-12-16 09:07:36 +00001539}