blob: 0bd7904060a18836d71470e1750b426a8927a112 [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 {
112 return DbgNode->getNumOperands()-6;
113}
114
115
Chris Lattnera45664f2008-11-10 02:56:27 +0000116//===----------------------------------------------------------------------===//
Devang Patel6ceea332009-08-31 18:49:10 +0000117// Predicates
Chris Lattnera45664f2008-11-10 02:56:27 +0000118//===----------------------------------------------------------------------===//
119
Devang Patel6ceea332009-08-31 18:49:10 +0000120/// isBasicType - Return true if the specified tag is legal for
121/// DIBasicType.
122bool DIDescriptor::isBasicType() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000123 return DbgNode && getTag() == dwarf::DW_TAG_base_type;
Torok Edwinb07fbd92008-12-13 08:25:29 +0000124}
Chris Lattnera45664f2008-11-10 02:56:27 +0000125
Devang Patel6ceea332009-08-31 18:49:10 +0000126/// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
127bool DIDescriptor::isDerivedType() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000128 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000129 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000130 case dwarf::DW_TAG_typedef:
131 case dwarf::DW_TAG_pointer_type:
132 case dwarf::DW_TAG_reference_type:
133 case dwarf::DW_TAG_const_type:
134 case dwarf::DW_TAG_volatile_type:
135 case dwarf::DW_TAG_restrict_type:
136 case dwarf::DW_TAG_member:
137 case dwarf::DW_TAG_inheritance:
138 return true;
139 default:
Devang Patele4b27562009-08-28 23:24:31 +0000140 // CompositeTypes are currently modelled as DerivedTypes.
Devang Patel6ceea332009-08-31 18:49:10 +0000141 return isCompositeType();
Chris Lattnera45664f2008-11-10 02:56:27 +0000142 }
143}
144
Chris Lattnera45664f2008-11-10 02:56:27 +0000145/// isCompositeType - Return true if the specified tag is legal for
146/// DICompositeType.
Devang Patel6ceea332009-08-31 18:49:10 +0000147bool DIDescriptor::isCompositeType() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000148 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000149 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000150 case dwarf::DW_TAG_array_type:
151 case dwarf::DW_TAG_structure_type:
152 case dwarf::DW_TAG_union_type:
153 case dwarf::DW_TAG_enumeration_type:
154 case dwarf::DW_TAG_vector_type:
155 case dwarf::DW_TAG_subroutine_type:
Devang Patel25cb0d72009-03-25 03:52:06 +0000156 case dwarf::DW_TAG_class_type:
Chris Lattnera45664f2008-11-10 02:56:27 +0000157 return true;
158 default:
159 return false;
160 }
161}
162
Chris Lattnera45664f2008-11-10 02:56:27 +0000163/// isVariable - Return true if the specified tag is legal for DIVariable.
Devang Patel6ceea332009-08-31 18:49:10 +0000164bool DIDescriptor::isVariable() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000165 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000166 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000167 case dwarf::DW_TAG_auto_variable:
168 case dwarf::DW_TAG_arg_variable:
169 case dwarf::DW_TAG_return_variable:
170 return true;
171 default:
172 return false;
173 }
174}
175
Devang Patelecbeb1a2009-09-30 22:34:41 +0000176/// isType - Return true if the specified tag is legal for DIType.
177bool DIDescriptor::isType() const {
178 return isBasicType() || isCompositeType() || isDerivedType();
179}
180
Devang Patel6ceea332009-08-31 18:49:10 +0000181/// isSubprogram - Return true if the specified tag is legal for
182/// DISubprogram.
183bool DIDescriptor::isSubprogram() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000184 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
Devang Patel6ceea332009-08-31 18:49:10 +0000185}
186
187/// isGlobalVariable - Return true if the specified tag is legal for
188/// DIGlobalVariable.
189bool DIDescriptor::isGlobalVariable() const {
Devang Patel29368072010-08-10 07:11:13 +0000190 return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
191 getTag() == dwarf::DW_TAG_constant);
Devang Patel6ceea332009-08-31 18:49:10 +0000192}
193
Devang Patelecbeb1a2009-09-30 22:34:41 +0000194/// isGlobal - Return true if the specified tag is legal for DIGlobal.
195bool DIDescriptor::isGlobal() const {
196 return isGlobalVariable();
197}
198
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000199/// isScope - Return true if the specified tag is one of the scope
Devang Patel43d98b32009-08-31 20:44:45 +0000200/// related tag.
201bool DIDescriptor::isScope() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000202 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000203 switch (getTag()) {
204 case dwarf::DW_TAG_compile_unit:
205 case dwarf::DW_TAG_lexical_block:
206 case dwarf::DW_TAG_subprogram:
207 case dwarf::DW_TAG_namespace:
208 return true;
209 default:
210 break;
Devang Patel43d98b32009-08-31 20:44:45 +0000211 }
212 return false;
213}
Devang Patel6ceea332009-08-31 18:49:10 +0000214
Devang Patelc9f322d2009-08-31 21:34:44 +0000215/// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
216bool DIDescriptor::isCompileUnit() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000217 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
Devang Patelc9f322d2009-08-31 21:34:44 +0000218}
219
Devang Patel7aa81892010-03-08 22:27:22 +0000220/// isFile - Return true if the specified tag is DW_TAG_file_type.
221bool DIDescriptor::isFile() const {
222 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
223}
224
Devang Patel6404e4e2009-12-15 19:16:48 +0000225/// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
226bool DIDescriptor::isNameSpace() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000227 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
Devang Patel6404e4e2009-12-15 19:16:48 +0000228}
229
Devang Patel5e005d82009-08-31 22:00:15 +0000230/// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
231bool DIDescriptor::isLexicalBlock() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000232 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block;
Devang Patel5e005d82009-08-31 22:00:15 +0000233}
234
Devang Patelecbeb1a2009-09-30 22:34:41 +0000235/// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
236bool DIDescriptor::isSubrange() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000237 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
Devang Patelecbeb1a2009-09-30 22:34:41 +0000238}
239
240/// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
241bool DIDescriptor::isEnumerator() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000242 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
Devang Patelecbeb1a2009-09-30 22:34:41 +0000243}
244
Devang Patel6ceea332009-08-31 18:49:10 +0000245//===----------------------------------------------------------------------===//
246// Simple Descriptor Constructors and other Methods
247//===----------------------------------------------------------------------===//
248
Devang Patele9f8f5e2010-05-07 20:54:48 +0000249DIType::DIType(const MDNode *N) : DIScope(N) {
Devang Patel6ceea332009-08-31 18:49:10 +0000250 if (!N) return;
251 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
252 DbgNode = 0;
253 }
254}
255
Devang Patel68afdc32009-01-05 18:33:01 +0000256unsigned DIArray::getNumElements() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000257 if (!DbgNode)
258 return 0;
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000259 return DbgNode->getNumOperands();
Devang Patel68afdc32009-01-05 18:33:01 +0000260}
Chris Lattnera45664f2008-11-10 02:56:27 +0000261
Devang Patelc4999d72009-07-22 18:23:44 +0000262/// replaceAllUsesWith - Replace all uses of debug info referenced by
Dan Gohman872814a2010-07-21 18:54:18 +0000263/// this descriptor.
Dan Gohman489b29b2010-08-20 22:02:26 +0000264void DIType::replaceAllUsesWith(DIDescriptor &D) {
Devang Patel3c91b052010-03-08 20:52:55 +0000265 if (!DbgNode)
Devang Patelc4999d72009-07-22 18:23:44 +0000266 return;
267
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000268 // Since we use a TrackingVH for the node, its easy for clients to manufacture
269 // legitimate situations where they want to replaceAllUsesWith() on something
270 // which, due to uniquing, has merged with the source. We shield clients from
271 // this detail by allowing a value to be replaced with replaceAllUsesWith()
272 // itself.
Devang Pateled66bf52010-05-07 18:19:32 +0000273 if (DbgNode != D) {
Devang Patele9f8f5e2010-05-07 20:54:48 +0000274 MDNode *Node = const_cast<MDNode*>(DbgNode);
275 const MDNode *DN = D;
276 const Value *V = cast_or_null<Value>(DN);
277 Node->replaceAllUsesWith(const_cast<Value*>(V));
Dan Gohman489b29b2010-08-20 22:02:26 +0000278 MDNode::deleteTemporary(Node);
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000279 }
Devang Patelc4999d72009-07-22 18:23:44 +0000280}
281
Devang Patelb79b5352009-01-19 23:21:49 +0000282/// Verify - Verify that a compile unit is well formed.
283bool DICompileUnit::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000284 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000285 return false;
Devang Patel65dbc902009-11-25 17:36:49 +0000286 StringRef N = getFilename();
287 if (N.empty())
Bill Wendling0582ae92009-03-13 04:39:26 +0000288 return false;
Devang Patelb79b5352009-01-19 23:21:49 +0000289 // It is possible that directory and produce string is empty.
Bill Wendling0582ae92009-03-13 04:39:26 +0000290 return true;
Devang Patelb79b5352009-01-19 23:21:49 +0000291}
292
293/// Verify - Verify that a type descriptor is well formed.
294bool DIType::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000295 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000296 return false;
Devang Patel3c91b052010-03-08 20:52:55 +0000297 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000298 return false;
299
300 DICompileUnit CU = getCompileUnit();
Devang Patel3c91b052010-03-08 20:52:55 +0000301 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000302 return false;
303 return true;
304}
305
306/// Verify - Verify that a composite type descriptor is well formed.
307bool DICompositeType::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000308 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000309 return false;
Devang Patel3c91b052010-03-08 20:52:55 +0000310 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000311 return false;
312
313 DICompileUnit CU = getCompileUnit();
Devang Patel3c91b052010-03-08 20:52:55 +0000314 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000315 return false;
316 return true;
317}
318
319/// Verify - Verify that a subprogram descriptor is well formed.
320bool DISubprogram::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000321 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000322 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000323
Devang Patel3c91b052010-03-08 20:52:55 +0000324 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000325 return false;
326
327 DICompileUnit CU = getCompileUnit();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000328 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000329 return false;
330
331 DICompositeType Ty = getType();
Devang Patel3c91b052010-03-08 20:52:55 +0000332 if (!Ty.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000333 return false;
334 return true;
335}
336
337/// Verify - Verify that a global variable descriptor is well formed.
338bool DIGlobalVariable::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000339 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000340 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000341
Devang Patel65dbc902009-11-25 17:36:49 +0000342 if (getDisplayName().empty())
Devang Patel84c73e92009-11-06 17:58:12 +0000343 return false;
344
Devang Patel3c91b052010-03-08 20:52:55 +0000345 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000346 return false;
347
348 DICompileUnit CU = getCompileUnit();
Devang Patel3c91b052010-03-08 20:52:55 +0000349 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000350 return false;
351
352 DIType Ty = getType();
353 if (!Ty.Verify())
354 return false;
355
Devang Patel27398962010-08-09 21:39:24 +0000356 if (!getGlobal() && !getConstant())
Devang Patelb79b5352009-01-19 23:21:49 +0000357 return false;
358
359 return true;
360}
361
362/// Verify - Verify that a variable descriptor is well formed.
363bool DIVariable::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000364 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000365 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000366
Devang Patel3c91b052010-03-08 20:52:55 +0000367 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000368 return false;
369
Devang Patel62077af2010-05-07 21:42:24 +0000370 if (!getCompileUnit().Verify())
371 return false;
372
Devang Patelb79b5352009-01-19 23:21:49 +0000373 DIType Ty = getType();
374 if (!Ty.Verify())
375 return false;
376
Devang Patelb79b5352009-01-19 23:21:49 +0000377 return true;
378}
379
Devang Patel3c91b052010-03-08 20:52:55 +0000380/// Verify - Verify that a location descriptor is well formed.
381bool DILocation::Verify() const {
382 if (!DbgNode)
383 return false;
Jim Grosbache62b6902010-07-21 21:36:25 +0000384
Devang Patel3c91b052010-03-08 20:52:55 +0000385 return DbgNode->getNumOperands() == 4;
386}
387
Devang Patel47e22652010-05-07 23:04:32 +0000388/// Verify - Verify that a namespace descriptor is well formed.
389bool DINameSpace::Verify() const {
390 if (!DbgNode)
391 return false;
392 if (getName().empty())
393 return false;
394 if (!getCompileUnit().Verify())
395 return false;
396 return true;
397}
398
Devang Patel36375ee2009-02-17 21:23:59 +0000399/// getOriginalTypeSize - If this type is derived from a base type then
400/// return base type size.
401uint64_t DIDerivedType::getOriginalTypeSize() const {
Devang Patel61ecbd12009-11-04 23:48:00 +0000402 unsigned Tag = getTag();
403 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
404 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
405 Tag == dwarf::DW_TAG_restrict_type) {
406 DIType BaseType = getTypeDerivedFrom();
Jim Grosbache62b6902010-07-21 21:36:25 +0000407 // If this type is not derived from any type then take conservative
Devang Patel5ebfa2d2009-11-06 18:24:05 +0000408 // approach.
Devang Patel3c91b052010-03-08 20:52:55 +0000409 if (!BaseType.isValid())
Devang Patel5ebfa2d2009-11-06 18:24:05 +0000410 return getSizeInBits();
Devang Patel61ecbd12009-11-04 23:48:00 +0000411 if (BaseType.isDerivedType())
Devang Patel2db49d72010-05-07 18:11:54 +0000412 return DIDerivedType(BaseType).getOriginalTypeSize();
Devang Patel61ecbd12009-11-04 23:48:00 +0000413 else
414 return BaseType.getSizeInBits();
415 }
Jim Grosbache62b6902010-07-21 21:36:25 +0000416
Devang Patel61ecbd12009-11-04 23:48:00 +0000417 return getSizeInBits();
Devang Patel36375ee2009-02-17 21:23:59 +0000418}
Devang Patelb79b5352009-01-19 23:21:49 +0000419
Jim Grosbache62b6902010-07-21 21:36:25 +0000420/// isInlinedFnArgument - Return true if this variable provides debugging
Devang Patel719f6a92010-04-29 20:40:36 +0000421/// information for an inlined function arguments.
422bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
423 assert(CurFn && "Invalid function");
424 if (!getContext().isSubprogram())
425 return false;
Jim Grosbache62b6902010-07-21 21:36:25 +0000426 // This variable is not inlined function argument if its scope
Devang Patel719f6a92010-04-29 20:40:36 +0000427 // does not describe current function.
Devang Patel2db49d72010-05-07 18:11:54 +0000428 return !(DISubprogram(getContext()).describes(CurFn));
Devang Patel719f6a92010-04-29 20:40:36 +0000429}
430
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000431/// describes - Return true if this subprogram provides debugging
432/// information for the function F.
433bool DISubprogram::describes(const Function *F) {
Chris Lattner099b7792009-12-29 09:22:47 +0000434 assert(F && "Invalid function");
Devang Patelffd33cd2010-06-16 06:42:02 +0000435 if (F == getFunction())
436 return true;
Devang Patel65dbc902009-11-25 17:36:49 +0000437 StringRef Name = getLinkageName();
438 if (Name.empty())
Devang Patel5ccdd102009-09-29 18:40:58 +0000439 Name = getName();
Devang Patel65dbc902009-11-25 17:36:49 +0000440 if (F->getName() == Name)
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000441 return true;
442 return false;
443}
444
Jim Grosbache62b6902010-07-21 21:36:25 +0000445unsigned DISubprogram::isOptimized() const {
Devang Patelccff8122010-04-30 19:38:23 +0000446 assert (DbgNode && "Invalid subprogram descriptor!");
447 if (DbgNode->getNumOperands() == 16)
448 return getUnsignedField(15);
449 return 0;
450}
451
Devang Patel65dbc902009-11-25 17:36:49 +0000452StringRef DIScope::getFilename() const {
Devang Patel77bf2952010-03-08 22:02:50 +0000453 if (!DbgNode)
454 return StringRef();
Jim Grosbache62b6902010-07-21 21:36:25 +0000455 if (isLexicalBlock())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000456 return DILexicalBlock(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000457 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000458 return DISubprogram(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000459 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000460 return DICompileUnit(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000461 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000462 return DINameSpace(DbgNode).getFilename();
Devang Patel77bf2952010-03-08 22:02:50 +0000463 if (isType())
464 return DIType(DbgNode).getFilename();
Devang Patel7aa81892010-03-08 22:27:22 +0000465 if (isFile())
466 return DIFile(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000467 assert(0 && "Invalid DIScope!");
Devang Patel65dbc902009-11-25 17:36:49 +0000468 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000469}
470
Devang Patel65dbc902009-11-25 17:36:49 +0000471StringRef DIScope::getDirectory() 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).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000476 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000477 return DISubprogram(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000478 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000479 return DICompileUnit(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000480 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000481 return DINameSpace(DbgNode).getDirectory();
Devang Patel77bf2952010-03-08 22:02:50 +0000482 if (isType())
483 return DIType(DbgNode).getDirectory();
Devang Patel7aa81892010-03-08 22:27:22 +0000484 if (isFile())
485 return DIFile(DbgNode).getDirectory();
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
Chris Lattnera45664f2008-11-10 02:56:27 +0000490//===----------------------------------------------------------------------===//
Devang Patel7136a652009-07-01 22:10:23 +0000491// DIDescriptor: dump routines for all descriptors.
492//===----------------------------------------------------------------------===//
493
494
Dan Gohman50404362010-05-07 15:30:29 +0000495/// print - Print descriptor.
496void DIDescriptor::print(raw_ostream &OS) const {
497 OS << "[" << dwarf::TagString(getTag()) << "] ";
498 OS.write_hex((intptr_t) &*DbgNode) << ']';
Devang Patel7136a652009-07-01 22:10:23 +0000499}
500
Dan Gohman50404362010-05-07 15:30:29 +0000501/// print - Print compile unit.
502void DICompileUnit::print(raw_ostream &OS) const {
Devang Patel7136a652009-07-01 22:10:23 +0000503 if (getLanguage())
Dan Gohman50404362010-05-07 15:30:29 +0000504 OS << " [" << dwarf::LanguageString(getLanguage()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000505
Dan Gohmanfaa19c32010-05-10 20:07:44 +0000506 OS << " [" << getDirectory() << "/" << getFilename() << "]";
Devang Patel7136a652009-07-01 22:10:23 +0000507}
508
Dan Gohman50404362010-05-07 15:30:29 +0000509/// print - Print type.
510void DIType::print(raw_ostream &OS) const {
Devang Patel3c91b052010-03-08 20:52:55 +0000511 if (!DbgNode) return;
Devang Patel7136a652009-07-01 22:10:23 +0000512
Devang Patel65dbc902009-11-25 17:36:49 +0000513 StringRef Res = getName();
514 if (!Res.empty())
Dan Gohman50404362010-05-07 15:30:29 +0000515 OS << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000516
517 unsigned Tag = getTag();
Dan Gohman50404362010-05-07 15:30:29 +0000518 OS << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000519
520 // TODO : Print context
Dan Gohmanc014d092010-05-07 16:17:22 +0000521 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000522 OS << " ["
Dan Gohman9a7063e2010-05-07 16:39:27 +0000523 << "line " << getLineNumber() << ", "
524 << getSizeInBits() << " bits, "
525 << getAlignInBits() << " bit alignment, "
526 << getOffsetInBits() << " bit offset"
Chris Lattnera81d29b2009-08-23 07:33:14 +0000527 << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000528
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000529 if (isPrivate())
Dan Gohman50404362010-05-07 15:30:29 +0000530 OS << " [private] ";
Devang Patel7136a652009-07-01 22:10:23 +0000531 else if (isProtected())
Dan Gohman50404362010-05-07 15:30:29 +0000532 OS << " [protected] ";
Devang Patel7136a652009-07-01 22:10:23 +0000533
534 if (isForwardDecl())
Dan Gohman50404362010-05-07 15:30:29 +0000535 OS << " [fwd] ";
Devang Patel7136a652009-07-01 22:10:23 +0000536
Devang Patel6ceea332009-08-31 18:49:10 +0000537 if (isBasicType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000538 DIBasicType(DbgNode).print(OS);
Devang Patel6ceea332009-08-31 18:49:10 +0000539 else if (isDerivedType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000540 DIDerivedType(DbgNode).print(OS);
Devang Patel6ceea332009-08-31 18:49:10 +0000541 else if (isCompositeType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000542 DICompositeType(DbgNode).print(OS);
Devang Patel7136a652009-07-01 22:10:23 +0000543 else {
Dan Gohman50404362010-05-07 15:30:29 +0000544 OS << "Invalid DIType\n";
Devang Patel7136a652009-07-01 22:10:23 +0000545 return;
546 }
547
Dan Gohman50404362010-05-07 15:30:29 +0000548 OS << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000549}
550
Dan Gohman50404362010-05-07 15:30:29 +0000551/// print - Print basic type.
552void DIBasicType::print(raw_ostream &OS) const {
553 OS << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000554}
555
Dan Gohman50404362010-05-07 15:30:29 +0000556/// print - Print derived type.
557void DIDerivedType::print(raw_ostream &OS) const {
Dan Gohmanc014d092010-05-07 16:17:22 +0000558 OS << "\n\t Derived From: "; getTypeDerivedFrom().print(OS);
Devang Patel7136a652009-07-01 22:10:23 +0000559}
560
Dan Gohman50404362010-05-07 15:30:29 +0000561/// print - Print composite type.
562void DICompositeType::print(raw_ostream &OS) const {
Devang Patel7136a652009-07-01 22:10:23 +0000563 DIArray A = getTypeArray();
Dan Gohman50404362010-05-07 15:30:29 +0000564 OS << " [" << A.getNumElements() << " elements]";
Devang Patel7136a652009-07-01 22:10:23 +0000565}
566
Dan Gohman50404362010-05-07 15:30:29 +0000567/// print - Print subprogram.
568void DISubprogram::print(raw_ostream &OS) const {
Devang Patel65dbc902009-11-25 17:36:49 +0000569 StringRef Res = getName();
570 if (!Res.empty())
Dan Gohman50404362010-05-07 15:30:29 +0000571 OS << " [" << Res << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000572
573 unsigned Tag = getTag();
Dan Gohman50404362010-05-07 15:30:29 +0000574 OS << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000575
576 // TODO : Print context
Dan Gohmanc014d092010-05-07 16:17:22 +0000577 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000578 OS << " [" << getLineNumber() << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000579
580 if (isLocalToUnit())
Dan Gohman50404362010-05-07 15:30:29 +0000581 OS << " [local] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000582
583 if (isDefinition())
Dan Gohman50404362010-05-07 15:30:29 +0000584 OS << " [def] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000585
Dan Gohman50404362010-05-07 15:30:29 +0000586 OS << "\n";
587}
588
589/// print - Print global variable.
590void DIGlobalVariable::print(raw_ostream &OS) const {
591 OS << " [";
Devang Patela49d8772010-05-07 23:19:07 +0000592 StringRef Res = getName();
593 if (!Res.empty())
594 OS << " [" << Res << "] ";
595
596 unsigned Tag = getTag();
597 OS << " [" << dwarf::TagString(Tag) << "] ";
598
599 // TODO : Print context
600 getCompileUnit().print(OS);
601 OS << " [" << getLineNumber() << "] ";
602
603 if (isLocalToUnit())
604 OS << " [local] ";
605
606 if (isDefinition())
607 OS << " [def] ";
608
609 if (isGlobalVariable())
610 DIGlobalVariable(DbgNode).print(OS);
611 OS << "]\n";
Dan Gohman50404362010-05-07 15:30:29 +0000612}
613
614/// print - Print variable.
615void DIVariable::print(raw_ostream &OS) const {
616 StringRef Res = getName();
617 if (!Res.empty())
618 OS << " [" << Res << "] ";
619
Dan Gohmanc014d092010-05-07 16:17:22 +0000620 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000621 OS << " [" << getLineNumber() << "] ";
Dan Gohmanc014d092010-05-07 16:17:22 +0000622 getType().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000623 OS << "\n";
624
625 // FIXME: Dump complex addresses
626}
627
628/// dump - Print descriptor to dbgs() with a newline.
629void DIDescriptor::dump() const {
630 print(dbgs()); dbgs() << '\n';
631}
632
633/// dump - Print compile unit to dbgs() with a newline.
634void DICompileUnit::dump() const {
635 print(dbgs()); dbgs() << '\n';
636}
637
638/// dump - Print type to dbgs() with a newline.
639void DIType::dump() const {
640 print(dbgs()); dbgs() << '\n';
641}
642
643/// dump - Print basic type to dbgs() with a newline.
644void DIBasicType::dump() const {
645 print(dbgs()); dbgs() << '\n';
646}
647
648/// dump - Print derived type to dbgs() with a newline.
649void DIDerivedType::dump() const {
650 print(dbgs()); dbgs() << '\n';
651}
652
653/// dump - Print composite type to dbgs() with a newline.
654void DICompositeType::dump() const {
655 print(dbgs()); dbgs() << '\n';
656}
657
Dan Gohman50404362010-05-07 15:30:29 +0000658/// dump - Print subprogram to dbgs() with a newline.
659void DISubprogram::dump() const {
660 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000661}
662
663/// dump - Print global variable.
664void DIGlobalVariable::dump() const {
Dan Gohman50404362010-05-07 15:30:29 +0000665 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000666}
667
668/// dump - Print variable.
669void DIVariable::dump() const {
Dan Gohman50404362010-05-07 15:30:29 +0000670 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000671}
672
673//===----------------------------------------------------------------------===//
Chris Lattnera45664f2008-11-10 02:56:27 +0000674// DIFactory: Basic Helpers
675//===----------------------------------------------------------------------===//
676
Bill Wendlingdc817b62009-05-14 18:26:15 +0000677DIFactory::DIFactory(Module &m)
Victor Hernandez06203122010-01-23 00:03:28 +0000678 : M(m), VMContext(M.getContext()), DeclareFn(0), ValueFn(0) {}
Chris Lattner497a7a82008-11-10 04:10:34 +0000679
Chris Lattnera45664f2008-11-10 02:56:27 +0000680Constant *DIFactory::GetTagConstant(unsigned TAG) {
Devang Patel6906ba52009-01-20 19:22:03 +0000681 assert((TAG & LLVMDebugVersionMask) == 0 &&
Chris Lattnera45664f2008-11-10 02:56:27 +0000682 "Tag too large for debug encoding!");
Owen Anderson1d0be152009-08-13 21:58:54 +0000683 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
Chris Lattnera45664f2008-11-10 02:56:27 +0000684}
685
Chris Lattnera45664f2008-11-10 02:56:27 +0000686//===----------------------------------------------------------------------===//
687// DIFactory: Primary Constructors
688//===----------------------------------------------------------------------===//
689
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000690/// GetOrCreateArray - Create an descriptor for an array of descriptors.
Chris Lattnera45664f2008-11-10 02:56:27 +0000691/// This implicitly uniques the arrays created.
692DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
Devang Patele4b27562009-08-28 23:24:31 +0000693 SmallVector<Value*, 16> Elts;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000694
Devang Patele4b27562009-08-28 23:24:31 +0000695 if (NumTys == 0)
696 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)));
697 else
698 for (unsigned i = 0; i != NumTys; ++i)
Devang Patel2db49d72010-05-07 18:11:54 +0000699 Elts.push_back(Tys[i]);
Devang Patel82459882009-08-26 05:01:18 +0000700
Devang Patele4b27562009-08-28 23:24:31 +0000701 return DIArray(MDNode::get(VMContext,Elts.data(), Elts.size()));
Chris Lattnera45664f2008-11-10 02:56:27 +0000702}
703
704/// GetOrCreateSubrange - Create a descriptor for a value range. This
705/// implicitly uniques the values returned.
706DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
Devang Patele4b27562009-08-28 23:24:31 +0000707 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000708 GetTagConstant(dwarf::DW_TAG_subrange_type),
Owen Anderson1d0be152009-08-13 21:58:54 +0000709 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
710 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
Chris Lattnera45664f2008-11-10 02:56:27 +0000711 };
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000712
Devang Patele4b27562009-08-28 23:24:31 +0000713 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000714}
715
716
717
718/// CreateCompileUnit - Create a new descriptor for the specified compile
719/// unit. Note that this does not unique compile units within the module.
720DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
Devang Patel65dbc902009-11-25 17:36:49 +0000721 StringRef Filename,
722 StringRef Directory,
723 StringRef Producer,
Devang Pateldd9db662009-01-30 18:20:31 +0000724 bool isMain,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000725 bool isOptimized,
Devang Patel65dbc902009-11-25 17:36:49 +0000726 StringRef Flags,
Devang Patel13319ce2009-02-17 22:43:44 +0000727 unsigned RunTimeVer) {
Devang Patele4b27562009-08-28 23:24:31 +0000728 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000729 GetTagConstant(dwarf::DW_TAG_compile_unit),
Devang Patele4b27562009-08-28 23:24:31 +0000730 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Owen Anderson1d0be152009-08-13 21:58:54 +0000731 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
Devang Patele4b27562009-08-28 23:24:31 +0000732 MDString::get(VMContext, Filename),
733 MDString::get(VMContext, Directory),
734 MDString::get(VMContext, Producer),
Owen Anderson1d0be152009-08-13 21:58:54 +0000735 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
736 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
Devang Patele4b27562009-08-28 23:24:31 +0000737 MDString::get(VMContext, Flags),
Owen Anderson1d0be152009-08-13 21:58:54 +0000738 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
Chris Lattnera45664f2008-11-10 02:56:27 +0000739 };
Devang Patele4b27562009-08-28 23:24:31 +0000740
741 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000742}
743
Devang Patel7aa81892010-03-08 22:27:22 +0000744/// CreateFile - Create a new descriptor for the specified file.
745DIFile DIFactory::CreateFile(StringRef Filename,
746 StringRef Directory,
747 DICompileUnit CU) {
748 Value *Elts[] = {
749 GetTagConstant(dwarf::DW_TAG_file_type),
750 MDString::get(VMContext, Filename),
751 MDString::get(VMContext, Directory),
Devang Patel2db49d72010-05-07 18:11:54 +0000752 CU
Devang Patel7aa81892010-03-08 22:27:22 +0000753 };
754
755 return DIFile(MDNode::get(VMContext, &Elts[0], 4));
756}
757
Chris Lattnera45664f2008-11-10 02:56:27 +0000758/// CreateEnumerator - Create a single enumerator value.
Devang Patel65dbc902009-11-25 17:36:49 +0000759DIEnumerator DIFactory::CreateEnumerator(StringRef Name, uint64_t Val){
Devang Patele4b27562009-08-28 23:24:31 +0000760 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000761 GetTagConstant(dwarf::DW_TAG_enumerator),
Devang Patele4b27562009-08-28 23:24:31 +0000762 MDString::get(VMContext, Name),
Owen Anderson1d0be152009-08-13 21:58:54 +0000763 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
Chris Lattnera45664f2008-11-10 02:56:27 +0000764 };
Devang Patele4b27562009-08-28 23:24:31 +0000765 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000766}
767
768
769/// CreateBasicType - Create a basic type like int, float, etc.
770DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000771 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000772 DIFile F,
Chris Lattnera45664f2008-11-10 02:56:27 +0000773 unsigned LineNumber,
774 uint64_t SizeInBits,
775 uint64_t AlignInBits,
776 uint64_t OffsetInBits, unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000777 unsigned Encoding) {
Devang Patele4b27562009-08-28 23:24:31 +0000778 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000779 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patel2db49d72010-05-07 18:11:54 +0000780 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000781 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000782 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000783 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
784 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
785 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
786 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
787 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
788 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
Chris Lattnera45664f2008-11-10 02:56:27 +0000789 };
Devang Patele4b27562009-08-28 23:24:31 +0000790 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000791}
792
Devang Patelac16d442009-10-26 16:54:35 +0000793
794/// CreateBasicType - Create a basic type like int, float, etc.
795DIBasicType DIFactory::CreateBasicTypeEx(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000796 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000797 DIFile F,
Devang Patelac16d442009-10-26 16:54:35 +0000798 unsigned LineNumber,
799 Constant *SizeInBits,
800 Constant *AlignInBits,
801 Constant *OffsetInBits, unsigned Flags,
802 unsigned Encoding) {
803 Value *Elts[] = {
804 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patel2db49d72010-05-07 18:11:54 +0000805 Context,
Devang Patelac16d442009-10-26 16:54:35 +0000806 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000807 F,
Devang Patelac16d442009-10-26 16:54:35 +0000808 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
809 SizeInBits,
810 AlignInBits,
811 OffsetInBits,
812 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
813 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
814 };
815 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
816}
817
Devang Patelb4645642010-02-06 01:02:37 +0000818/// CreateArtificialType - Create a new DIType with "artificial" flag set.
819DIType DIFactory::CreateArtificialType(DIType Ty) {
820 if (Ty.isArtificial())
821 return Ty;
822
823 SmallVector<Value *, 9> Elts;
Devang Patel2db49d72010-05-07 18:11:54 +0000824 MDNode *N = Ty;
Devang Patelb4645642010-02-06 01:02:37 +0000825 assert (N && "Unexpected input DIType!");
826 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
827 if (Value *V = N->getOperand(i))
828 Elts.push_back(V);
829 else
830 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
831 }
832
833 unsigned CurFlags = Ty.getFlags();
834 CurFlags = CurFlags | DIType::FlagArtificial;
835
836 // Flags are stored at this slot.
837 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
838
839 return DIType(MDNode::get(VMContext, Elts.data(), Elts.size()));
840}
Devang Patelac16d442009-10-26 16:54:35 +0000841
Chris Lattnera45664f2008-11-10 02:56:27 +0000842/// CreateDerivedType - Create a derived type like const qualified type,
843/// pointer, typedef, etc.
844DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
845 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000846 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000847 DIFile F,
Chris Lattnera45664f2008-11-10 02:56:27 +0000848 unsigned LineNumber,
849 uint64_t SizeInBits,
850 uint64_t AlignInBits,
851 uint64_t OffsetInBits,
852 unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000853 DIType DerivedFrom) {
Devang Patele4b27562009-08-28 23:24:31 +0000854 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000855 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000856 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000857 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000858 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000859 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
860 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
861 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
862 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
863 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000864 DerivedFrom,
Chris Lattnera45664f2008-11-10 02:56:27 +0000865 };
Devang Patele4b27562009-08-28 23:24:31 +0000866 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000867}
868
Devang Patelac16d442009-10-26 16:54:35 +0000869
870/// CreateDerivedType - Create a derived type like const qualified type,
871/// pointer, typedef, etc.
872DIDerivedType DIFactory::CreateDerivedTypeEx(unsigned Tag,
873 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000874 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000875 DIFile F,
Devang Patelac16d442009-10-26 16:54:35 +0000876 unsigned LineNumber,
877 Constant *SizeInBits,
878 Constant *AlignInBits,
879 Constant *OffsetInBits,
880 unsigned Flags,
881 DIType DerivedFrom) {
882 Value *Elts[] = {
883 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000884 Context,
Devang Patelac16d442009-10-26 16:54:35 +0000885 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000886 F,
Devang Patelac16d442009-10-26 16:54:35 +0000887 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
888 SizeInBits,
889 AlignInBits,
890 OffsetInBits,
891 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000892 DerivedFrom,
Devang Patelac16d442009-10-26 16:54:35 +0000893 };
894 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
895}
896
897
Chris Lattnera45664f2008-11-10 02:56:27 +0000898/// CreateCompositeType - Create a composite type like array, struct, etc.
899DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
900 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000901 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000902 DIFile F,
Chris Lattnera45664f2008-11-10 02:56:27 +0000903 unsigned LineNumber,
904 uint64_t SizeInBits,
905 uint64_t AlignInBits,
906 uint64_t OffsetInBits,
907 unsigned Flags,
908 DIType DerivedFrom,
Devang Patel13319ce2009-02-17 22:43:44 +0000909 DIArray Elements,
Devang Patel0fd7f9d2010-01-26 21:14:59 +0000910 unsigned RuntimeLang,
911 MDNode *ContainingType) {
Owen Andersone277fed2009-07-07 16:31:25 +0000912
Devang Patele4b27562009-08-28 23:24:31 +0000913 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000914 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000915 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000916 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000917 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000918 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
919 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
920 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
921 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
922 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000923 DerivedFrom,
924 Elements,
Devang Patel0fd7f9d2010-01-26 21:14:59 +0000925 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
926 ContainingType
Chris Lattnera45664f2008-11-10 02:56:27 +0000927 };
Devang Patele7e5a0f2010-08-10 20:01:20 +0000928
929 MDNode *Node = MDNode::get(VMContext, &Elts[0], 13);
930 // Create a named metadata so that we do not lose this enum info.
931 if (Tag == dwarf::DW_TAG_enumeration_type) {
932 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.enum");
933 NMD->addOperand(Node);
934 }
935 return DICompositeType(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +0000936}
937
938
Dan Gohman489b29b2010-08-20 22:02:26 +0000939/// CreateTemporaryType - Create a temporary forward-declared type.
Dan Gohmana3833f12010-08-20 22:39:47 +0000940DIType DIFactory::CreateTemporaryType() {
Dan Gohman489b29b2010-08-20 22:02:26 +0000941 // Give the temporary MDNode a tag. It doesn't matter what tag we
942 // use here as long as DIType accepts it.
943 Value *Elts[] = {
944 GetTagConstant(DW_TAG_base_type)
945 };
946 MDNode *Node = MDNode::getTemporary(VMContext, Elts, array_lengthof(Elts));
947 return DIType(Node);
948}
949
950
Devang Patelac16d442009-10-26 16:54:35 +0000951/// CreateCompositeType - Create a composite type like array, struct, etc.
952DICompositeType DIFactory::CreateCompositeTypeEx(unsigned Tag,
953 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000954 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000955 DIFile F,
Devang Patelac16d442009-10-26 16:54:35 +0000956 unsigned LineNumber,
957 Constant *SizeInBits,
958 Constant *AlignInBits,
959 Constant *OffsetInBits,
960 unsigned Flags,
961 DIType DerivedFrom,
962 DIArray Elements,
Devang Patel6bf058c2010-08-10 20:22:49 +0000963 unsigned RuntimeLang,
964 MDNode *ContainingType) {
Devang Patelac16d442009-10-26 16:54:35 +0000965 Value *Elts[] = {
966 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000967 Context,
Devang Patelac16d442009-10-26 16:54:35 +0000968 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000969 F,
Devang Patelac16d442009-10-26 16:54:35 +0000970 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
971 SizeInBits,
972 AlignInBits,
973 OffsetInBits,
974 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000975 DerivedFrom,
976 Elements,
Devang Patel6bf058c2010-08-10 20:22:49 +0000977 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
978 ContainingType
Devang Patelac16d442009-10-26 16:54:35 +0000979 };
Devang Patel6bf058c2010-08-10 20:22:49 +0000980 MDNode *Node = MDNode::get(VMContext, &Elts[0], 13);
Devang Patele7e5a0f2010-08-10 20:01:20 +0000981 // Create a named metadata so that we do not lose this enum info.
982 if (Tag == dwarf::DW_TAG_enumeration_type) {
983 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.enum");
984 NMD->addOperand(Node);
985 }
986 return DICompositeType(Node);
Devang Patelac16d442009-10-26 16:54:35 +0000987}
988
989
Chris Lattnera45664f2008-11-10 02:56:27 +0000990/// CreateSubprogram - Create a new descriptor for the specified subprogram.
991/// See comments in DISubprogram for descriptions of these fields. This
992/// method does not unique the generated descriptors.
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000993DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000994 StringRef Name,
995 StringRef DisplayName,
996 StringRef LinkageName,
Devang Patel4b945502010-03-09 00:44:10 +0000997 DIFile F,
Devang Patel2e369932010-01-23 00:26:28 +0000998 unsigned LineNo, DIType Ty,
Chris Lattnera45664f2008-11-10 02:56:27 +0000999 bool isLocalToUnit,
Devang Patel5d11eb02009-12-03 19:11:07 +00001000 bool isDefinition,
1001 unsigned VK, unsigned VIndex,
Devang Patel4e0d19d2010-02-03 19:57:19 +00001002 DIType ContainingType,
Devang Patelccff8122010-04-30 19:38:23 +00001003 bool isArtificial,
Stuart Hastings215aa152010-06-11 20:08:44 +00001004 bool isOptimized,
1005 Function *Fn) {
Devang Patel854967e2008-12-17 22:39:29 +00001006
Devang Patele4b27562009-08-28 23:24:31 +00001007 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001008 GetTagConstant(dwarf::DW_TAG_subprogram),
Devang Patele4b27562009-08-28 23:24:31 +00001009 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Devang Patel2db49d72010-05-07 18:11:54 +00001010 Context,
Devang Patele4b27562009-08-28 23:24:31 +00001011 MDString::get(VMContext, Name),
1012 MDString::get(VMContext, DisplayName),
1013 MDString::get(VMContext, LinkageName),
Devang Patel2db49d72010-05-07 18:11:54 +00001014 F,
Owen Anderson1d0be152009-08-13 21:58:54 +00001015 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001016 Ty,
Owen Anderson1d0be152009-08-13 21:58:54 +00001017 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
Devang Patel5d11eb02009-12-03 19:11:07 +00001018 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1019 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
1020 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
Devang Patel2db49d72010-05-07 18:11:54 +00001021 ContainingType,
Devang Patelccff8122010-04-30 19:38:23 +00001022 ConstantInt::get(Type::getInt1Ty(VMContext), isArtificial),
Stuart Hastings215aa152010-06-11 20:08:44 +00001023 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
1024 Fn
Chris Lattnera45664f2008-11-10 02:56:27 +00001025 };
Devang Patelfd5fdc32010-06-28 05:53:08 +00001026 MDNode *Node = MDNode::get(VMContext, &Elts[0], 17);
1027
1028 // Create a named metadata so that we do not lose this mdnode.
1029 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
1030 NMD->addOperand(Node);
1031 return DISubprogram(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +00001032}
1033
Devang Patele3a18de2009-12-01 23:09:02 +00001034/// CreateSubprogramDefinition - Create new subprogram descriptor for the
Jim Grosbache62b6902010-07-21 21:36:25 +00001035/// given declaration.
1036DISubprogram DIFactory::CreateSubprogramDefinition(DISubprogram &SPDeclaration){
Devang Patele3a18de2009-12-01 23:09:02 +00001037 if (SPDeclaration.isDefinition())
Devang Patel2db49d72010-05-07 18:11:54 +00001038 return DISubprogram(SPDeclaration);
Devang Patele3a18de2009-12-01 23:09:02 +00001039
Devang Patel2db49d72010-05-07 18:11:54 +00001040 MDNode *DeclNode = SPDeclaration;
Devang Patele3a18de2009-12-01 23:09:02 +00001041 Value *Elts[] = {
1042 GetTagConstant(dwarf::DW_TAG_subprogram),
1043 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001044 DeclNode->getOperand(2), // Context
1045 DeclNode->getOperand(3), // Name
1046 DeclNode->getOperand(4), // DisplayName
1047 DeclNode->getOperand(5), // LinkageName
1048 DeclNode->getOperand(6), // CompileUnit
1049 DeclNode->getOperand(7), // LineNo
1050 DeclNode->getOperand(8), // Type
1051 DeclNode->getOperand(9), // isLocalToUnit
Stuart Hastings6d56b9f2010-06-05 00:39:29 +00001052 ConstantInt::get(Type::getInt1Ty(VMContext), true),
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001053 DeclNode->getOperand(11), // Virtuality
1054 DeclNode->getOperand(12), // VIndex
Devang Patel4e0d19d2010-02-03 19:57:19 +00001055 DeclNode->getOperand(13), // Containting Type
Devang Patelccff8122010-04-30 19:38:23 +00001056 DeclNode->getOperand(14), // isArtificial
Devang Patelacc6efa2010-06-27 21:04:31 +00001057 DeclNode->getOperand(15), // isOptimized
1058 SPDeclaration.getFunction()
Devang Patele3a18de2009-12-01 23:09:02 +00001059 };
Devang Patelfd5fdc32010-06-28 05:53:08 +00001060 MDNode *Node =MDNode::get(VMContext, &Elts[0], 16);
1061
1062 // Create a named metadata so that we do not lose this mdnode.
1063 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
1064 NMD->addOperand(Node);
1065 return DISubprogram(Node);
Devang Patele3a18de2009-12-01 23:09:02 +00001066}
1067
Chris Lattnera45664f2008-11-10 02:56:27 +00001068/// CreateGlobalVariable - Create a new descriptor for the specified global.
1069DIGlobalVariable
Devang Patel65dbc902009-11-25 17:36:49 +00001070DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
1071 StringRef DisplayName,
1072 StringRef LinkageName,
Devang Patel4b945502010-03-09 00:44:10 +00001073 DIFile F,
Devang Patel2e369932010-01-23 00:26:28 +00001074 unsigned LineNo, DIType Ty,bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +00001075 bool isDefinition, llvm::GlobalVariable *Val) {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001076 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001077 GetTagConstant(dwarf::DW_TAG_variable),
Devang Patele4b27562009-08-28 23:24:31 +00001078 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Devang Patel2db49d72010-05-07 18:11:54 +00001079 Context,
Devang Patele4b27562009-08-28 23:24:31 +00001080 MDString::get(VMContext, Name),
1081 MDString::get(VMContext, DisplayName),
1082 MDString::get(VMContext, LinkageName),
Devang Patel2db49d72010-05-07 18:11:54 +00001083 F,
Owen Anderson1d0be152009-08-13 21:58:54 +00001084 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001085 Ty,
Owen Anderson1d0be152009-08-13 21:58:54 +00001086 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1087 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
Devang Patele4b27562009-08-28 23:24:31 +00001088 Val
Chris Lattnera45664f2008-11-10 02:56:27 +00001089 };
Devang Patele4b27562009-08-28 23:24:31 +00001090
1091 Value *const *Vs = &Elts[0];
1092 MDNode *Node = MDNode::get(VMContext,Vs, 12);
1093
1094 // Create a named metadata so that we do not lose this mdnode.
1095 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001096 NMD->addOperand(Node);
Devang Patele4b27562009-08-28 23:24:31 +00001097
1098 return DIGlobalVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +00001099}
1100
Devang Patel27398962010-08-09 21:39:24 +00001101/// CreateGlobalVariable - Create a new descriptor for the specified constant.
1102DIGlobalVariable
1103DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
1104 StringRef DisplayName,
1105 StringRef LinkageName,
1106 DIFile F,
1107 unsigned LineNo, DIType Ty,bool isLocalToUnit,
1108 bool isDefinition, llvm::Constant *Val) {
1109 Value *Elts[] = {
Devang Patelebd53742010-08-11 23:17:54 +00001110 GetTagConstant(dwarf::DW_TAG_variable),
Devang Patel27398962010-08-09 21:39:24 +00001111 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
1112 Context,
1113 MDString::get(VMContext, Name),
1114 MDString::get(VMContext, DisplayName),
1115 MDString::get(VMContext, LinkageName),
1116 F,
1117 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1118 Ty,
1119 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1120 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1121 Val
1122 };
1123
1124 Value *const *Vs = &Elts[0];
1125 MDNode *Node = MDNode::get(VMContext,Vs, 12);
1126
1127 // Create a named metadata so that we do not lose this mdnode.
1128 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
1129 NMD->addOperand(Node);
1130
1131 return DIGlobalVariable(Node);
1132}
Chris Lattnera45664f2008-11-10 02:56:27 +00001133
1134/// CreateVariable - Create a new descriptor for the specified variable.
1135DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +00001136 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +00001137 DIFile F,
1138 unsigned LineNo,
Devang Patel6ed0ce32010-05-20 20:35:24 +00001139 DIType Ty, bool AlwaysPreserve) {
Devang Patele4b27562009-08-28 23:24:31 +00001140 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001141 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +00001142 Context,
Devang Patele4b27562009-08-28 23:24:31 +00001143 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +00001144 F,
Owen Anderson1d0be152009-08-13 21:58:54 +00001145 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001146 Ty,
Chris Lattnera45664f2008-11-10 02:56:27 +00001147 };
Devang Patel98e1cac2010-05-14 21:01:35 +00001148 MDNode *Node = MDNode::get(VMContext, &Elts[0], 6);
Devang Patel6ed0ce32010-05-20 20:35:24 +00001149 if (AlwaysPreserve) {
1150 // The optimizer may remove local variable. If there is an interest
1151 // to preserve variable info in such situation then stash it in a
1152 // named mdnode.
Devang Patel2f7d5292010-06-16 00:53:55 +00001153 DISubprogram Fn(getDISubprogram(Context));
Devang Patel10de3bb2010-06-21 18:36:58 +00001154 StringRef FName = "fn";
1155 if (Fn.getFunction())
1156 FName = Fn.getFunction()->getName();
Devang Patel10de3bb2010-06-21 18:36:58 +00001157 char One = '\1';
1158 if (FName.startswith(StringRef(&One, 1)))
1159 FName = FName.substr(1);
Dan Gohman17aa92c2010-07-21 23:38:33 +00001160
1161 SmallString<32> Out;
1162 NamedMDNode *FnLocals =
1163 M.getOrInsertNamedMetadata(Twine("llvm.dbg.lv.", FName).toStringRef(Out));
Devang Patel2f7d5292010-06-16 00:53:55 +00001164 FnLocals->addOperand(Node);
Devang Patel98e1cac2010-05-14 21:01:35 +00001165 }
1166 return DIVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +00001167}
1168
1169
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001170/// CreateComplexVariable - Create a new descriptor for the specified variable
1171/// which has a complex address expression for its address.
1172DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
1173 const std::string &Name,
Devang Patel4b945502010-03-09 00:44:10 +00001174 DIFile F,
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001175 unsigned LineNo,
Jim Grosbache62b6902010-07-21 21:36:25 +00001176 DIType Ty,
Devang Patel2e369932010-01-23 00:26:28 +00001177 SmallVector<Value *, 9> &addr) {
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001178 SmallVector<Value *, 9> Elts;
1179 Elts.push_back(GetTagConstant(Tag));
Devang Patel2db49d72010-05-07 18:11:54 +00001180 Elts.push_back(Context);
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001181 Elts.push_back(MDString::get(VMContext, Name));
Devang Patel2db49d72010-05-07 18:11:54 +00001182 Elts.push_back(F);
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001183 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
Devang Patel2db49d72010-05-07 18:11:54 +00001184 Elts.push_back(Ty);
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001185 Elts.insert(Elts.end(), addr.begin(), addr.end());
1186
1187 return DIVariable(MDNode::get(VMContext, &Elts[0], 6+addr.size()));
1188}
1189
1190
Chris Lattnera45664f2008-11-10 02:56:27 +00001191/// CreateBlock - This creates a descriptor for a lexical block with the
Owen Anderson99035272009-07-07 17:12:53 +00001192/// specified parent VMContext.
Devang Patel3d821aa2010-02-16 21:39:34 +00001193DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context,
Stuart Hastings0db42712010-07-19 23:56:30 +00001194 DIFile F, unsigned LineNo,
1195 unsigned Col) {
1196 // Defeat MDNode uniqing for lexical blocks.
1197 static unsigned int unique_id = 0;
Devang Patele4b27562009-08-28 23:24:31 +00001198 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001199 GetTagConstant(dwarf::DW_TAG_lexical_block),
Devang Patel2db49d72010-05-07 18:11:54 +00001200 Context,
Devang Patel3d821aa2010-02-16 21:39:34 +00001201 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Stuart Hastings0db42712010-07-19 23:56:30 +00001202 ConstantInt::get(Type::getInt32Ty(VMContext), Col),
1203 F,
1204 ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
Chris Lattnera45664f2008-11-10 02:56:27 +00001205 };
Stuart Hastings0db42712010-07-19 23:56:30 +00001206 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 6));
Chris Lattnera45664f2008-11-10 02:56:27 +00001207}
1208
Devang Patel6404e4e2009-12-15 19:16:48 +00001209/// CreateNameSpace - This creates new descriptor for a namespace
1210/// with the specified parent context.
1211DINameSpace DIFactory::CreateNameSpace(DIDescriptor Context, StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +00001212 DIFile F,
Devang Patel6404e4e2009-12-15 19:16:48 +00001213 unsigned LineNo) {
1214 Value *Elts[] = {
1215 GetTagConstant(dwarf::DW_TAG_namespace),
Devang Patel2db49d72010-05-07 18:11:54 +00001216 Context,
Devang Patel6404e4e2009-12-15 19:16:48 +00001217 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +00001218 F,
Devang Patel6404e4e2009-12-15 19:16:48 +00001219 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1220 };
1221 return DINameSpace(MDNode::get(VMContext, &Elts[0], 5));
1222}
1223
Devang Patelf98d8fe2009-09-01 01:14:15 +00001224/// CreateLocation - Creates a debug info location.
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001225DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
Daniel Dunbara279bc32009-09-20 02:20:51 +00001226 DIScope S, DILocation OrigLoc) {
Devang Patelf98d8fe2009-09-01 01:14:15 +00001227 Value *Elts[] = {
1228 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1229 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001230 S,
1231 OrigLoc,
Devang Patelf98d8fe2009-09-01 01:14:15 +00001232 };
1233 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1234}
1235
Chris Lattnera45664f2008-11-10 02:56:27 +00001236//===----------------------------------------------------------------------===//
1237// DIFactory: Routines for inserting code into a function
1238//===----------------------------------------------------------------------===//
1239
Chris Lattnera45664f2008-11-10 02:56:27 +00001240/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Devang Patel6daf99b2009-11-10 22:05:35 +00001241Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001242 Instruction *InsertBefore) {
Victor Hernandez4cf292a2010-01-26 02:07:38 +00001243 assert(Storage && "no storage passed to dbg.declare");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001244 assert(D.Verify() && "empty DIVariable passed to dbg.declare");
Chris Lattnera45664f2008-11-10 02:56:27 +00001245 if (!DeclareFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +00001246 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1247
Victor Hernandez756462b2010-01-18 20:42:09 +00001248 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
Devang Patel2db49d72010-05-07 18:11:54 +00001249 D };
Devang Patel6daf99b2009-11-10 22:05:35 +00001250 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
Mike Stumpe4250392009-10-01 22:08:58 +00001251}
1252
1253/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Devang Patel6daf99b2009-11-10 22:05:35 +00001254Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001255 BasicBlock *InsertAtEnd) {
Victor Hernandez4cf292a2010-01-26 02:07:38 +00001256 assert(Storage && "no storage passed to dbg.declare");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001257 assert(D.Verify() && "invalid DIVariable passed to dbg.declare");
Mike Stumpe4250392009-10-01 22:08:58 +00001258 if (!DeclareFn)
1259 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1260
Victor Hernandez756462b2010-01-18 20:42:09 +00001261 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
Devang Patel2db49d72010-05-07 18:11:54 +00001262 D };
Devang Patel27a53de2010-01-29 18:30:57 +00001263
1264 // If this block already has a terminator then insert this intrinsic
1265 // before the terminator.
Jim Grosbache62b6902010-07-21 21:36:25 +00001266 if (TerminatorInst *T = InsertAtEnd->getTerminator())
Devang Patel27a53de2010-01-29 18:30:57 +00001267 return CallInst::Create(DeclareFn, Args, Args+2, "", T);
1268 else
1269 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);}
Torok Edwin620f2802008-12-16 09:07:36 +00001270
Victor Hernandezc59b3352009-12-07 21:54:43 +00001271/// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001272Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
Victor Hernandezc59b3352009-12-07 21:54:43 +00001273 DIVariable D,
1274 Instruction *InsertBefore) {
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001275 assert(V && "no value passed to dbg.value");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001276 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001277 if (!ValueFn)
1278 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1279
Victor Hernandezc8b7cd02010-01-20 05:44:11 +00001280 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001281 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
Devang Patel2db49d72010-05-07 18:11:54 +00001282 D };
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001283 return CallInst::Create(ValueFn, Args, Args+3, "", InsertBefore);
1284}
1285
Victor Hernandezc59b3352009-12-07 21:54:43 +00001286/// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001287Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
Victor Hernandezc59b3352009-12-07 21:54:43 +00001288 DIVariable D,
1289 BasicBlock *InsertAtEnd) {
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001290 assert(V && "no value passed to dbg.value");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001291 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001292 if (!ValueFn)
1293 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1294
Jim Grosbache62b6902010-07-21 21:36:25 +00001295 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001296 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
Devang Patel2db49d72010-05-07 18:11:54 +00001297 D };
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001298 return CallInst::Create(ValueFn, Args, Args+3, "", InsertAtEnd);
1299}
Devang Patele4b27562009-08-28 23:24:31 +00001300
Devang Pateld2f79a12009-07-28 19:55:13 +00001301//===----------------------------------------------------------------------===//
Devang Patel98c65172009-07-30 18:25:15 +00001302// DebugInfoFinder implementations.
Devang Pateld2f79a12009-07-28 19:55:13 +00001303//===----------------------------------------------------------------------===//
1304
Devang Patel98c65172009-07-30 18:25:15 +00001305/// processModule - Process entire module and collect debug info.
1306void DebugInfoFinder::processModule(Module &M) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001307 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1308 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
1309 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
1310 ++BI) {
Devang Patel01c5ff62010-05-04 01:05:02 +00001311 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
Devang Patelb4d31302009-07-31 18:18:52 +00001312 processDeclare(DDI);
Jim Grosbache62b6902010-07-21 21:36:25 +00001313
Chris Lattner28a9bf62010-04-02 20:44:29 +00001314 DebugLoc Loc = BI->getDebugLoc();
1315 if (Loc.isUnknown())
1316 continue;
Jim Grosbache62b6902010-07-21 21:36:25 +00001317
Chris Lattner28a9bf62010-04-02 20:44:29 +00001318 LLVMContext &Ctx = BI->getContext();
1319 DIDescriptor Scope(Loc.getScope(Ctx));
Jim Grosbache62b6902010-07-21 21:36:25 +00001320
Chris Lattner28a9bf62010-04-02 20:44:29 +00001321 if (Scope.isCompileUnit())
Devang Patel2db49d72010-05-07 18:11:54 +00001322 addCompileUnit(DICompileUnit(Scope));
Chris Lattner28a9bf62010-04-02 20:44:29 +00001323 else if (Scope.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001324 processSubprogram(DISubprogram(Scope));
Chris Lattner28a9bf62010-04-02 20:44:29 +00001325 else if (Scope.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001326 processLexicalBlock(DILexicalBlock(Scope));
Jim Grosbache62b6902010-07-21 21:36:25 +00001327
Chris Lattner28a9bf62010-04-02 20:44:29 +00001328 if (MDNode *IA = Loc.getInlinedAt(Ctx))
1329 processLocation(DILocation(IA));
Devang Pateld2f79a12009-07-28 19:55:13 +00001330 }
Devang Patele4b27562009-08-28 23:24:31 +00001331
Devang Patelfd5fdc32010-06-28 05:53:08 +00001332 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
1333 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1334 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
1335 if (addGlobalVariable(DIG)) {
1336 addCompileUnit(DIG.getCompileUnit());
1337 processType(DIG.getType());
1338 }
Devang Pateld2f79a12009-07-28 19:55:13 +00001339 }
1340 }
Devang Patelfd5fdc32010-06-28 05:53:08 +00001341
1342 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
1343 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
1344 processSubprogram(DISubprogram(NMD->getOperand(i)));
Devang Pateld2f79a12009-07-28 19:55:13 +00001345}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001346
Devang Patel6daf99b2009-11-10 22:05:35 +00001347/// processLocation - Process DILocation.
1348void DebugInfoFinder::processLocation(DILocation Loc) {
Devang Patel3c91b052010-03-08 20:52:55 +00001349 if (!Loc.Verify()) return;
Devang Patel2db49d72010-05-07 18:11:54 +00001350 DIDescriptor S(Loc.getScope());
Devang Patel6daf99b2009-11-10 22:05:35 +00001351 if (S.isCompileUnit())
Devang Patel2db49d72010-05-07 18:11:54 +00001352 addCompileUnit(DICompileUnit(S));
Devang Patel6daf99b2009-11-10 22:05:35 +00001353 else if (S.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001354 processSubprogram(DISubprogram(S));
Devang Patel6daf99b2009-11-10 22:05:35 +00001355 else if (S.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001356 processLexicalBlock(DILexicalBlock(S));
Devang Patel6daf99b2009-11-10 22:05:35 +00001357 processLocation(Loc.getOrigLocation());
1358}
1359
Devang Patel98c65172009-07-30 18:25:15 +00001360/// processType - Process DIType.
1361void DebugInfoFinder::processType(DIType DT) {
Devang Patel72bcdb62009-08-10 22:09:58 +00001362 if (!addType(DT))
Devang Pateld2f79a12009-07-28 19:55:13 +00001363 return;
1364
1365 addCompileUnit(DT.getCompileUnit());
Devang Patel6ceea332009-08-31 18:49:10 +00001366 if (DT.isCompositeType()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001367 DICompositeType DCT(DT);
Devang Patel98c65172009-07-30 18:25:15 +00001368 processType(DCT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001369 DIArray DA = DCT.getTypeArray();
Devang Patel3c91b052010-03-08 20:52:55 +00001370 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1371 DIDescriptor D = DA.getElement(i);
1372 if (D.isType())
Devang Patel2db49d72010-05-07 18:11:54 +00001373 processType(DIType(D));
Devang Patel3c91b052010-03-08 20:52:55 +00001374 else if (D.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001375 processSubprogram(DISubprogram(D));
Devang Patel3c91b052010-03-08 20:52:55 +00001376 }
Devang Patel6ceea332009-08-31 18:49:10 +00001377 } else if (DT.isDerivedType()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001378 DIDerivedType DDT(DT);
Devang Patel3c91b052010-03-08 20:52:55 +00001379 processType(DDT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001380 }
1381}
1382
Devang Patelbeab41b2009-10-07 22:04:08 +00001383/// processLexicalBlock
1384void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
Devang Patelbeab41b2009-10-07 22:04:08 +00001385 DIScope Context = LB.getContext();
1386 if (Context.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001387 return processLexicalBlock(DILexicalBlock(Context));
Devang Patelbeab41b2009-10-07 22:04:08 +00001388 else
Devang Patel2db49d72010-05-07 18:11:54 +00001389 return processSubprogram(DISubprogram(Context));
Devang Patelbeab41b2009-10-07 22:04:08 +00001390}
1391
Devang Patel98c65172009-07-30 18:25:15 +00001392/// processSubprogram - Process DISubprogram.
1393void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001394 if (!addSubprogram(SP))
1395 return;
1396 addCompileUnit(SP.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +00001397 processType(SP.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +00001398}
1399
Devang Patelb4d31302009-07-31 18:18:52 +00001400/// processDeclare - Process DbgDeclareInst.
1401void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
Devang Patel3c91b052010-03-08 20:52:55 +00001402 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1403 if (!N) return;
1404
1405 DIDescriptor DV(N);
1406 if (!DV.isVariable())
Devang Patelb4d31302009-07-31 18:18:52 +00001407 return;
1408
Devang Patel2db49d72010-05-07 18:11:54 +00001409 if (!NodesSeen.insert(DV))
Devang Patelb4d31302009-07-31 18:18:52 +00001410 return;
1411
Devang Patel3c91b052010-03-08 20:52:55 +00001412 addCompileUnit(DIVariable(N).getCompileUnit());
1413 processType(DIVariable(N).getType());
Devang Patelb4d31302009-07-31 18:18:52 +00001414}
1415
Devang Patel72bcdb62009-08-10 22:09:58 +00001416/// addType - Add type into Tys.
1417bool DebugInfoFinder::addType(DIType DT) {
Devang Patel3c91b052010-03-08 20:52:55 +00001418 if (!DT.isValid())
Devang Patel72bcdb62009-08-10 22:09:58 +00001419 return false;
1420
Devang Patel2db49d72010-05-07 18:11:54 +00001421 if (!NodesSeen.insert(DT))
Devang Patel72bcdb62009-08-10 22:09:58 +00001422 return false;
1423
Devang Patel2db49d72010-05-07 18:11:54 +00001424 TYs.push_back(DT);
Devang Patel72bcdb62009-08-10 22:09:58 +00001425 return true;
1426}
1427
Devang Pateld2f79a12009-07-28 19:55:13 +00001428/// addCompileUnit - Add compile unit into CUs.
Devang Patel98c65172009-07-30 18:25:15 +00001429bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Patel3c91b052010-03-08 20:52:55 +00001430 if (!CU.Verify())
Devang Pateld2f79a12009-07-28 19:55:13 +00001431 return false;
1432
Devang Patel2db49d72010-05-07 18:11:54 +00001433 if (!NodesSeen.insert(CU))
Devang Pateld2f79a12009-07-28 19:55:13 +00001434 return false;
1435
Devang Patel2db49d72010-05-07 18:11:54 +00001436 CUs.push_back(CU);
Devang Pateld2f79a12009-07-28 19:55:13 +00001437 return true;
1438}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001439
Devang Pateld2f79a12009-07-28 19:55:13 +00001440/// addGlobalVariable - Add global variable into GVs.
Devang Patel98c65172009-07-30 18:25:15 +00001441bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Patel2db49d72010-05-07 18:11:54 +00001442 if (!DIDescriptor(DIG).isGlobalVariable())
Devang Pateld2f79a12009-07-28 19:55:13 +00001443 return false;
1444
Devang Patel2db49d72010-05-07 18:11:54 +00001445 if (!NodesSeen.insert(DIG))
Devang Pateld2f79a12009-07-28 19:55:13 +00001446 return false;
1447
Devang Patel2db49d72010-05-07 18:11:54 +00001448 GVs.push_back(DIG);
Devang Pateld2f79a12009-07-28 19:55:13 +00001449 return true;
1450}
1451
1452// addSubprogram - Add subprgoram into SPs.
Devang Patel98c65172009-07-30 18:25:15 +00001453bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Patel2db49d72010-05-07 18:11:54 +00001454 if (!DIDescriptor(SP).isSubprogram())
Devang Pateld2f79a12009-07-28 19:55:13 +00001455 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001456
Devang Patel2db49d72010-05-07 18:11:54 +00001457 if (!NodesSeen.insert(SP))
Devang Pateld2f79a12009-07-28 19:55:13 +00001458 return false;
1459
Devang Patel2db49d72010-05-07 18:11:54 +00001460 SPs.push_back(SP);
Devang Pateld2f79a12009-07-28 19:55:13 +00001461 return true;
1462}
1463
Victor Hernandez756462b2010-01-18 20:42:09 +00001464/// Find the debug info descriptor corresponding to this global variable.
1465static Value *findDbgGlobalDeclare(GlobalVariable *V) {
Chris Lattner099b7792009-12-29 09:22:47 +00001466 const Module *M = V->getParent();
1467 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1468 if (!NMD)
Torok Edwinff7d0e92009-03-10 13:41:26 +00001469 return 0;
Chris Lattner099b7792009-12-29 09:22:47 +00001470
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001471 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman872814a2010-07-21 18:54:18 +00001472 DIDescriptor DIG(cast<MDNode>(NMD->getOperand(i)));
Devang Patel3c91b052010-03-08 20:52:55 +00001473 if (!DIG.isGlobalVariable())
Chris Lattner099b7792009-12-29 09:22:47 +00001474 continue;
Devang Patel2db49d72010-05-07 18:11:54 +00001475 if (DIGlobalVariable(DIG).getGlobal() == V)
1476 return DIG;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001477 }
Chris Lattner099b7792009-12-29 09:22:47 +00001478 return 0;
1479}
Torok Edwinff7d0e92009-03-10 13:41:26 +00001480
Chris Lattner099b7792009-12-29 09:22:47 +00001481/// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
1482/// It looks through pointer casts too.
Victor Hernandez756462b2010-01-18 20:42:09 +00001483static const DbgDeclareInst *findDbgDeclare(const Value *V) {
Victor Hernandez3a328652010-01-15 19:04:09 +00001484 V = V->stripPointerCasts();
Jim Grosbache62b6902010-07-21 21:36:25 +00001485
Victor Hernandez3a328652010-01-15 19:04:09 +00001486 if (!isa<Instruction>(V) && !isa<Argument>(V))
Torok Edwin620f2802008-12-16 09:07:36 +00001487 return 0;
Jim Grosbache62b6902010-07-21 21:36:25 +00001488
Victor Hernandez3a328652010-01-15 19:04:09 +00001489 const Function *F = NULL;
1490 if (const Instruction *I = dyn_cast<Instruction>(V))
1491 F = I->getParent()->getParent();
1492 else if (const Argument *A = dyn_cast<Argument>(V))
1493 F = A->getParent();
Jim Grosbache62b6902010-07-21 21:36:25 +00001494
Victor Hernandez3a328652010-01-15 19:04:09 +00001495 for (Function::const_iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
1496 for (BasicBlock::const_iterator BI = (*FI).begin(), BE = (*FI).end();
1497 BI != BE; ++BI)
1498 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1499 if (DDI->getAddress() == V)
1500 return DDI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001501
Chris Lattner099b7792009-12-29 09:22:47 +00001502 return 0;
1503}
Bill Wendlingdc817b62009-05-14 18:26:15 +00001504
Chris Lattner099b7792009-12-29 09:22:47 +00001505bool llvm::getLocationInfo(const Value *V, std::string &DisplayName,
1506 std::string &Type, unsigned &LineNo,
1507 std::string &File, std::string &Dir) {
1508 DICompileUnit Unit;
1509 DIType TypeD;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001510
Chris Lattner099b7792009-12-29 09:22:47 +00001511 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1512 Value *DIGV = findDbgGlobalDeclare(GV);
1513 if (!DIGV) return false;
1514 DIGlobalVariable Var(cast<MDNode>(DIGV));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001515
Chris Lattner099b7792009-12-29 09:22:47 +00001516 StringRef D = Var.getDisplayName();
Devang Patel65dbc902009-11-25 17:36:49 +00001517 if (!D.empty())
Chris Lattner099b7792009-12-29 09:22:47 +00001518 DisplayName = D;
1519 LineNo = Var.getLineNumber();
1520 Unit = Var.getCompileUnit();
1521 TypeD = Var.getType();
1522 } else {
1523 const DbgDeclareInst *DDI = findDbgDeclare(V);
1524 if (!DDI) return false;
1525 DIVariable Var(cast<MDNode>(DDI->getVariable()));
1526
1527 StringRef D = Var.getName();
1528 if (!D.empty())
1529 DisplayName = D;
1530 LineNo = Var.getLineNumber();
1531 Unit = Var.getCompileUnit();
1532 TypeD = Var.getType();
Torok Edwinff7d0e92009-03-10 13:41:26 +00001533 }
Devang Patel13e16b62009-06-26 01:49:18 +00001534
Chris Lattner099b7792009-12-29 09:22:47 +00001535 StringRef T = TypeD.getName();
1536 if (!T.empty())
1537 Type = T;
1538 StringRef F = Unit.getFilename();
1539 if (!F.empty())
1540 File = F;
1541 StringRef D = Unit.getDirectory();
1542 if (!D.empty())
1543 Dir = D;
1544 return true;
1545}
Devang Patel9e529c32009-07-02 01:15:24 +00001546
Chris Lattner099b7792009-12-29 09:22:47 +00001547/// getDISubprogram - Find subprogram that is enclosing this scope.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001548DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
Chris Lattner099b7792009-12-29 09:22:47 +00001549 DIDescriptor D(Scope);
Chris Lattner099b7792009-12-29 09:22:47 +00001550 if (D.isSubprogram())
1551 return DISubprogram(Scope);
Jim Grosbache62b6902010-07-21 21:36:25 +00001552
Chris Lattner099b7792009-12-29 09:22:47 +00001553 if (D.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001554 return getDISubprogram(DILexicalBlock(Scope).getContext());
Jim Grosbache62b6902010-07-21 21:36:25 +00001555
Chris Lattner099b7792009-12-29 09:22:47 +00001556 return DISubprogram();
1557}
Devang Patel193f7202009-11-24 01:14:22 +00001558
Chris Lattner099b7792009-12-29 09:22:47 +00001559/// getDICompositeType - Find underlying composite type.
1560DICompositeType llvm::getDICompositeType(DIType T) {
Chris Lattner099b7792009-12-29 09:22:47 +00001561 if (T.isCompositeType())
Devang Patel2db49d72010-05-07 18:11:54 +00001562 return DICompositeType(T);
Jim Grosbache62b6902010-07-21 21:36:25 +00001563
Chris Lattner099b7792009-12-29 09:22:47 +00001564 if (T.isDerivedType())
Devang Patel2db49d72010-05-07 18:11:54 +00001565 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
Jim Grosbache62b6902010-07-21 21:36:25 +00001566
Chris Lattner099b7792009-12-29 09:22:47 +00001567 return DICompositeType();
Torok Edwin620f2802008-12-16 09:07:36 +00001568}