blob: c38002842917909d3fc8d50064a5290e0abb82d6 [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
Devang Patel0c4720c2010-08-23 18:25:56 +0000306/// Verify - Verify that a basic type descriptor is well formed.
307bool DIBasicType::Verify() const {
308 return isBasicType();
309}
310
311/// Verify - Verify that a derived type descriptor is well formed.
312bool DIDerivedType::Verify() const {
313 return isDerivedType();
314}
315
Devang Patelb79b5352009-01-19 23:21:49 +0000316/// Verify - Verify that a composite type descriptor is well formed.
317bool DICompositeType::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000318 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000319 return false;
Devang Patel3c91b052010-03-08 20:52:55 +0000320 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000321 return false;
322
323 DICompileUnit CU = getCompileUnit();
Devang Patel3c91b052010-03-08 20:52:55 +0000324 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000325 return false;
326 return true;
327}
328
329/// Verify - Verify that a subprogram descriptor is well formed.
330bool DISubprogram::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000331 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000332 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000333
Devang Patel3c91b052010-03-08 20:52:55 +0000334 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000335 return false;
336
337 DICompileUnit CU = getCompileUnit();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000338 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000339 return false;
340
341 DICompositeType Ty = getType();
Devang Patel3c91b052010-03-08 20:52:55 +0000342 if (!Ty.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000343 return false;
344 return true;
345}
346
347/// Verify - Verify that a global variable descriptor is well formed.
348bool DIGlobalVariable::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000349 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000350 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000351
Devang Patel65dbc902009-11-25 17:36:49 +0000352 if (getDisplayName().empty())
Devang Patel84c73e92009-11-06 17:58:12 +0000353 return false;
354
Devang Patel3c91b052010-03-08 20:52:55 +0000355 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000356 return false;
357
358 DICompileUnit CU = getCompileUnit();
Devang Patel3c91b052010-03-08 20:52:55 +0000359 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000360 return false;
361
362 DIType Ty = getType();
363 if (!Ty.Verify())
364 return false;
365
Devang Patel27398962010-08-09 21:39:24 +0000366 if (!getGlobal() && !getConstant())
Devang Patelb79b5352009-01-19 23:21:49 +0000367 return false;
368
369 return true;
370}
371
372/// Verify - Verify that a variable descriptor is well formed.
373bool DIVariable::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000374 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000375 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000376
Devang Patel3c91b052010-03-08 20:52:55 +0000377 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000378 return false;
379
Devang Patel62077af2010-05-07 21:42:24 +0000380 if (!getCompileUnit().Verify())
381 return false;
382
Devang Patelb79b5352009-01-19 23:21:49 +0000383 DIType Ty = getType();
384 if (!Ty.Verify())
385 return false;
386
Devang Patelb79b5352009-01-19 23:21:49 +0000387 return true;
388}
389
Devang Patel3c91b052010-03-08 20:52:55 +0000390/// Verify - Verify that a location descriptor is well formed.
391bool DILocation::Verify() const {
392 if (!DbgNode)
393 return false;
Jim Grosbache62b6902010-07-21 21:36:25 +0000394
Devang Patel3c91b052010-03-08 20:52:55 +0000395 return DbgNode->getNumOperands() == 4;
396}
397
Devang Patel47e22652010-05-07 23:04:32 +0000398/// Verify - Verify that a namespace descriptor is well formed.
399bool DINameSpace::Verify() const {
400 if (!DbgNode)
401 return false;
402 if (getName().empty())
403 return false;
404 if (!getCompileUnit().Verify())
405 return false;
406 return true;
407}
408
Devang Patel36375ee2009-02-17 21:23:59 +0000409/// getOriginalTypeSize - If this type is derived from a base type then
410/// return base type size.
411uint64_t DIDerivedType::getOriginalTypeSize() const {
Devang Patel61ecbd12009-11-04 23:48:00 +0000412 unsigned Tag = getTag();
413 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
414 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
415 Tag == dwarf::DW_TAG_restrict_type) {
416 DIType BaseType = getTypeDerivedFrom();
Jim Grosbache62b6902010-07-21 21:36:25 +0000417 // If this type is not derived from any type then take conservative
Devang Patel5ebfa2d2009-11-06 18:24:05 +0000418 // approach.
Devang Patel3c91b052010-03-08 20:52:55 +0000419 if (!BaseType.isValid())
Devang Patel5ebfa2d2009-11-06 18:24:05 +0000420 return getSizeInBits();
Devang Patel61ecbd12009-11-04 23:48:00 +0000421 if (BaseType.isDerivedType())
Devang Patel2db49d72010-05-07 18:11:54 +0000422 return DIDerivedType(BaseType).getOriginalTypeSize();
Devang Patel61ecbd12009-11-04 23:48:00 +0000423 else
424 return BaseType.getSizeInBits();
425 }
Jim Grosbache62b6902010-07-21 21:36:25 +0000426
Devang Patel61ecbd12009-11-04 23:48:00 +0000427 return getSizeInBits();
Devang Patel36375ee2009-02-17 21:23:59 +0000428}
Devang Patelb79b5352009-01-19 23:21:49 +0000429
Jim Grosbache62b6902010-07-21 21:36:25 +0000430/// isInlinedFnArgument - Return true if this variable provides debugging
Devang Patel719f6a92010-04-29 20:40:36 +0000431/// information for an inlined function arguments.
432bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
433 assert(CurFn && "Invalid function");
434 if (!getContext().isSubprogram())
435 return false;
Jim Grosbache62b6902010-07-21 21:36:25 +0000436 // This variable is not inlined function argument if its scope
Devang Patel719f6a92010-04-29 20:40:36 +0000437 // does not describe current function.
Devang Patel2db49d72010-05-07 18:11:54 +0000438 return !(DISubprogram(getContext()).describes(CurFn));
Devang Patel719f6a92010-04-29 20:40:36 +0000439}
440
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000441/// describes - Return true if this subprogram provides debugging
442/// information for the function F.
443bool DISubprogram::describes(const Function *F) {
Chris Lattner099b7792009-12-29 09:22:47 +0000444 assert(F && "Invalid function");
Devang Patelffd33cd2010-06-16 06:42:02 +0000445 if (F == getFunction())
446 return true;
Devang Patel65dbc902009-11-25 17:36:49 +0000447 StringRef Name = getLinkageName();
448 if (Name.empty())
Devang Patel5ccdd102009-09-29 18:40:58 +0000449 Name = getName();
Devang Patel65dbc902009-11-25 17:36:49 +0000450 if (F->getName() == Name)
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000451 return true;
452 return false;
453}
454
Jim Grosbache62b6902010-07-21 21:36:25 +0000455unsigned DISubprogram::isOptimized() const {
Devang Patelccff8122010-04-30 19:38:23 +0000456 assert (DbgNode && "Invalid subprogram descriptor!");
457 if (DbgNode->getNumOperands() == 16)
458 return getUnsignedField(15);
459 return 0;
460}
461
Devang Patel65dbc902009-11-25 17:36:49 +0000462StringRef DIScope::getFilename() const {
Devang Patel77bf2952010-03-08 22:02:50 +0000463 if (!DbgNode)
464 return StringRef();
Jim Grosbache62b6902010-07-21 21:36:25 +0000465 if (isLexicalBlock())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000466 return DILexicalBlock(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000467 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000468 return DISubprogram(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000469 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000470 return DICompileUnit(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000471 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000472 return DINameSpace(DbgNode).getFilename();
Devang Patel77bf2952010-03-08 22:02:50 +0000473 if (isType())
474 return DIType(DbgNode).getFilename();
Devang Patel7aa81892010-03-08 22:27:22 +0000475 if (isFile())
476 return DIFile(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000477 assert(0 && "Invalid DIScope!");
Devang Patel65dbc902009-11-25 17:36:49 +0000478 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000479}
480
Devang Patel65dbc902009-11-25 17:36:49 +0000481StringRef DIScope::getDirectory() const {
Devang Patel77bf2952010-03-08 22:02:50 +0000482 if (!DbgNode)
483 return StringRef();
Jim Grosbache62b6902010-07-21 21:36:25 +0000484 if (isLexicalBlock())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000485 return DILexicalBlock(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000486 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000487 return DISubprogram(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000488 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000489 return DICompileUnit(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000490 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000491 return DINameSpace(DbgNode).getDirectory();
Devang Patel77bf2952010-03-08 22:02:50 +0000492 if (isType())
493 return DIType(DbgNode).getDirectory();
Devang Patel7aa81892010-03-08 22:27:22 +0000494 if (isFile())
495 return DIFile(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000496 assert(0 && "Invalid DIScope!");
Devang Patel65dbc902009-11-25 17:36:49 +0000497 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000498}
499
Chris Lattnera45664f2008-11-10 02:56:27 +0000500//===----------------------------------------------------------------------===//
Devang Patel7136a652009-07-01 22:10:23 +0000501// DIDescriptor: dump routines for all descriptors.
502//===----------------------------------------------------------------------===//
503
504
Dan Gohman50404362010-05-07 15:30:29 +0000505/// print - Print descriptor.
506void DIDescriptor::print(raw_ostream &OS) const {
507 OS << "[" << dwarf::TagString(getTag()) << "] ";
508 OS.write_hex((intptr_t) &*DbgNode) << ']';
Devang Patel7136a652009-07-01 22:10:23 +0000509}
510
Dan Gohman50404362010-05-07 15:30:29 +0000511/// print - Print compile unit.
512void DICompileUnit::print(raw_ostream &OS) const {
Devang Patel7136a652009-07-01 22:10:23 +0000513 if (getLanguage())
Dan Gohman50404362010-05-07 15:30:29 +0000514 OS << " [" << dwarf::LanguageString(getLanguage()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000515
Dan Gohmanfaa19c32010-05-10 20:07:44 +0000516 OS << " [" << getDirectory() << "/" << getFilename() << "]";
Devang Patel7136a652009-07-01 22:10:23 +0000517}
518
Dan Gohman50404362010-05-07 15:30:29 +0000519/// print - Print type.
520void DIType::print(raw_ostream &OS) const {
Devang Patel3c91b052010-03-08 20:52:55 +0000521 if (!DbgNode) return;
Devang Patel7136a652009-07-01 22:10:23 +0000522
Devang Patel65dbc902009-11-25 17:36:49 +0000523 StringRef Res = getName();
524 if (!Res.empty())
Dan Gohman50404362010-05-07 15:30:29 +0000525 OS << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000526
527 unsigned Tag = getTag();
Dan Gohman50404362010-05-07 15:30:29 +0000528 OS << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000529
530 // TODO : Print context
Dan Gohmanc014d092010-05-07 16:17:22 +0000531 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000532 OS << " ["
Dan Gohman9a7063e2010-05-07 16:39:27 +0000533 << "line " << getLineNumber() << ", "
534 << getSizeInBits() << " bits, "
535 << getAlignInBits() << " bit alignment, "
536 << getOffsetInBits() << " bit offset"
Chris Lattnera81d29b2009-08-23 07:33:14 +0000537 << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000538
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000539 if (isPrivate())
Dan Gohman50404362010-05-07 15:30:29 +0000540 OS << " [private] ";
Devang Patel7136a652009-07-01 22:10:23 +0000541 else if (isProtected())
Dan Gohman50404362010-05-07 15:30:29 +0000542 OS << " [protected] ";
Devang Patel7136a652009-07-01 22:10:23 +0000543
544 if (isForwardDecl())
Dan Gohman50404362010-05-07 15:30:29 +0000545 OS << " [fwd] ";
Devang Patel7136a652009-07-01 22:10:23 +0000546
Devang Patel6ceea332009-08-31 18:49:10 +0000547 if (isBasicType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000548 DIBasicType(DbgNode).print(OS);
Devang Patel6ceea332009-08-31 18:49:10 +0000549 else if (isDerivedType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000550 DIDerivedType(DbgNode).print(OS);
Devang Patel6ceea332009-08-31 18:49:10 +0000551 else if (isCompositeType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000552 DICompositeType(DbgNode).print(OS);
Devang Patel7136a652009-07-01 22:10:23 +0000553 else {
Dan Gohman50404362010-05-07 15:30:29 +0000554 OS << "Invalid DIType\n";
Devang Patel7136a652009-07-01 22:10:23 +0000555 return;
556 }
557
Dan Gohman50404362010-05-07 15:30:29 +0000558 OS << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000559}
560
Dan Gohman50404362010-05-07 15:30:29 +0000561/// print - Print basic type.
562void DIBasicType::print(raw_ostream &OS) const {
563 OS << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000564}
565
Dan Gohman50404362010-05-07 15:30:29 +0000566/// print - Print derived type.
567void DIDerivedType::print(raw_ostream &OS) const {
Dan Gohmanc014d092010-05-07 16:17:22 +0000568 OS << "\n\t Derived From: "; getTypeDerivedFrom().print(OS);
Devang Patel7136a652009-07-01 22:10:23 +0000569}
570
Dan Gohman50404362010-05-07 15:30:29 +0000571/// print - Print composite type.
572void DICompositeType::print(raw_ostream &OS) const {
Devang Patel7136a652009-07-01 22:10:23 +0000573 DIArray A = getTypeArray();
Dan Gohman50404362010-05-07 15:30:29 +0000574 OS << " [" << A.getNumElements() << " elements]";
Devang Patel7136a652009-07-01 22:10:23 +0000575}
576
Dan Gohman50404362010-05-07 15:30:29 +0000577/// print - Print subprogram.
578void DISubprogram::print(raw_ostream &OS) const {
Devang Patel65dbc902009-11-25 17:36:49 +0000579 StringRef Res = getName();
580 if (!Res.empty())
Dan Gohman50404362010-05-07 15:30:29 +0000581 OS << " [" << Res << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000582
583 unsigned Tag = getTag();
Dan Gohman50404362010-05-07 15:30:29 +0000584 OS << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000585
586 // TODO : Print context
Dan Gohmanc014d092010-05-07 16:17:22 +0000587 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000588 OS << " [" << getLineNumber() << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000589
590 if (isLocalToUnit())
Dan Gohman50404362010-05-07 15:30:29 +0000591 OS << " [local] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000592
593 if (isDefinition())
Dan Gohman50404362010-05-07 15:30:29 +0000594 OS << " [def] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000595
Dan Gohman50404362010-05-07 15:30:29 +0000596 OS << "\n";
597}
598
599/// print - Print global variable.
600void DIGlobalVariable::print(raw_ostream &OS) const {
601 OS << " [";
Devang Patela49d8772010-05-07 23:19:07 +0000602 StringRef Res = getName();
603 if (!Res.empty())
604 OS << " [" << Res << "] ";
605
606 unsigned Tag = getTag();
607 OS << " [" << dwarf::TagString(Tag) << "] ";
608
609 // TODO : Print context
610 getCompileUnit().print(OS);
611 OS << " [" << getLineNumber() << "] ";
612
613 if (isLocalToUnit())
614 OS << " [local] ";
615
616 if (isDefinition())
617 OS << " [def] ";
618
619 if (isGlobalVariable())
620 DIGlobalVariable(DbgNode).print(OS);
621 OS << "]\n";
Dan Gohman50404362010-05-07 15:30:29 +0000622}
623
624/// print - Print variable.
625void DIVariable::print(raw_ostream &OS) const {
626 StringRef Res = getName();
627 if (!Res.empty())
628 OS << " [" << Res << "] ";
629
Dan Gohmanc014d092010-05-07 16:17:22 +0000630 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000631 OS << " [" << getLineNumber() << "] ";
Dan Gohmanc014d092010-05-07 16:17:22 +0000632 getType().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000633 OS << "\n";
634
635 // FIXME: Dump complex addresses
636}
637
638/// dump - Print descriptor to dbgs() with a newline.
639void DIDescriptor::dump() const {
640 print(dbgs()); dbgs() << '\n';
641}
642
643/// dump - Print compile unit to dbgs() with a newline.
644void DICompileUnit::dump() const {
645 print(dbgs()); dbgs() << '\n';
646}
647
648/// dump - Print type to dbgs() with a newline.
649void DIType::dump() const {
650 print(dbgs()); dbgs() << '\n';
651}
652
653/// dump - Print basic type to dbgs() with a newline.
654void DIBasicType::dump() const {
655 print(dbgs()); dbgs() << '\n';
656}
657
658/// dump - Print derived type to dbgs() with a newline.
659void DIDerivedType::dump() const {
660 print(dbgs()); dbgs() << '\n';
661}
662
663/// dump - Print composite type to dbgs() with a newline.
664void DICompositeType::dump() const {
665 print(dbgs()); dbgs() << '\n';
666}
667
Dan Gohman50404362010-05-07 15:30:29 +0000668/// dump - Print subprogram to dbgs() with a newline.
669void DISubprogram::dump() const {
670 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000671}
672
673/// dump - Print global variable.
674void DIGlobalVariable::dump() const {
Dan Gohman50404362010-05-07 15:30:29 +0000675 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000676}
677
678/// dump - Print variable.
679void DIVariable::dump() const {
Dan Gohman50404362010-05-07 15:30:29 +0000680 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000681}
682
683//===----------------------------------------------------------------------===//
Chris Lattnera45664f2008-11-10 02:56:27 +0000684// DIFactory: Basic Helpers
685//===----------------------------------------------------------------------===//
686
Bill Wendlingdc817b62009-05-14 18:26:15 +0000687DIFactory::DIFactory(Module &m)
Victor Hernandez06203122010-01-23 00:03:28 +0000688 : M(m), VMContext(M.getContext()), DeclareFn(0), ValueFn(0) {}
Chris Lattner497a7a82008-11-10 04:10:34 +0000689
Chris Lattnera45664f2008-11-10 02:56:27 +0000690Constant *DIFactory::GetTagConstant(unsigned TAG) {
Devang Patel6906ba52009-01-20 19:22:03 +0000691 assert((TAG & LLVMDebugVersionMask) == 0 &&
Chris Lattnera45664f2008-11-10 02:56:27 +0000692 "Tag too large for debug encoding!");
Owen Anderson1d0be152009-08-13 21:58:54 +0000693 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
Chris Lattnera45664f2008-11-10 02:56:27 +0000694}
695
Chris Lattnera45664f2008-11-10 02:56:27 +0000696//===----------------------------------------------------------------------===//
697// DIFactory: Primary Constructors
698//===----------------------------------------------------------------------===//
699
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000700/// GetOrCreateArray - Create an descriptor for an array of descriptors.
Chris Lattnera45664f2008-11-10 02:56:27 +0000701/// This implicitly uniques the arrays created.
702DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
Devang Patele4b27562009-08-28 23:24:31 +0000703 SmallVector<Value*, 16> Elts;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000704
Devang Patele4b27562009-08-28 23:24:31 +0000705 if (NumTys == 0)
706 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)));
707 else
708 for (unsigned i = 0; i != NumTys; ++i)
Devang Patel2db49d72010-05-07 18:11:54 +0000709 Elts.push_back(Tys[i]);
Devang Patel82459882009-08-26 05:01:18 +0000710
Devang Patele4b27562009-08-28 23:24:31 +0000711 return DIArray(MDNode::get(VMContext,Elts.data(), Elts.size()));
Chris Lattnera45664f2008-11-10 02:56:27 +0000712}
713
714/// GetOrCreateSubrange - Create a descriptor for a value range. This
715/// implicitly uniques the values returned.
716DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
Devang Patele4b27562009-08-28 23:24:31 +0000717 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000718 GetTagConstant(dwarf::DW_TAG_subrange_type),
Owen Anderson1d0be152009-08-13 21:58:54 +0000719 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
720 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
Chris Lattnera45664f2008-11-10 02:56:27 +0000721 };
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000722
Devang Patele4b27562009-08-28 23:24:31 +0000723 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000724}
725
726
727
728/// CreateCompileUnit - Create a new descriptor for the specified compile
729/// unit. Note that this does not unique compile units within the module.
730DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
Devang Patel65dbc902009-11-25 17:36:49 +0000731 StringRef Filename,
732 StringRef Directory,
733 StringRef Producer,
Devang Pateldd9db662009-01-30 18:20:31 +0000734 bool isMain,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000735 bool isOptimized,
Devang Patel65dbc902009-11-25 17:36:49 +0000736 StringRef Flags,
Devang Patel13319ce2009-02-17 22:43:44 +0000737 unsigned RunTimeVer) {
Devang Patele4b27562009-08-28 23:24:31 +0000738 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000739 GetTagConstant(dwarf::DW_TAG_compile_unit),
Devang Patele4b27562009-08-28 23:24:31 +0000740 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Owen Anderson1d0be152009-08-13 21:58:54 +0000741 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
Devang Patele4b27562009-08-28 23:24:31 +0000742 MDString::get(VMContext, Filename),
743 MDString::get(VMContext, Directory),
744 MDString::get(VMContext, Producer),
Owen Anderson1d0be152009-08-13 21:58:54 +0000745 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
746 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
Devang Patele4b27562009-08-28 23:24:31 +0000747 MDString::get(VMContext, Flags),
Owen Anderson1d0be152009-08-13 21:58:54 +0000748 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
Chris Lattnera45664f2008-11-10 02:56:27 +0000749 };
Devang Patele4b27562009-08-28 23:24:31 +0000750
751 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000752}
753
Devang Patel7aa81892010-03-08 22:27:22 +0000754/// CreateFile - Create a new descriptor for the specified file.
755DIFile DIFactory::CreateFile(StringRef Filename,
756 StringRef Directory,
757 DICompileUnit CU) {
758 Value *Elts[] = {
759 GetTagConstant(dwarf::DW_TAG_file_type),
760 MDString::get(VMContext, Filename),
761 MDString::get(VMContext, Directory),
Devang Patel2db49d72010-05-07 18:11:54 +0000762 CU
Devang Patel7aa81892010-03-08 22:27:22 +0000763 };
764
765 return DIFile(MDNode::get(VMContext, &Elts[0], 4));
766}
767
Chris Lattnera45664f2008-11-10 02:56:27 +0000768/// CreateEnumerator - Create a single enumerator value.
Devang Patel65dbc902009-11-25 17:36:49 +0000769DIEnumerator DIFactory::CreateEnumerator(StringRef Name, uint64_t Val){
Devang Patele4b27562009-08-28 23:24:31 +0000770 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000771 GetTagConstant(dwarf::DW_TAG_enumerator),
Devang Patele4b27562009-08-28 23:24:31 +0000772 MDString::get(VMContext, Name),
Owen Anderson1d0be152009-08-13 21:58:54 +0000773 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
Chris Lattnera45664f2008-11-10 02:56:27 +0000774 };
Devang Patele4b27562009-08-28 23:24:31 +0000775 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000776}
777
778
779/// CreateBasicType - Create a basic type like int, float, etc.
780DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000781 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000782 DIFile F,
Chris Lattnera45664f2008-11-10 02:56:27 +0000783 unsigned LineNumber,
784 uint64_t SizeInBits,
785 uint64_t AlignInBits,
786 uint64_t OffsetInBits, unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000787 unsigned Encoding) {
Devang Patele4b27562009-08-28 23:24:31 +0000788 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000789 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patel2db49d72010-05-07 18:11:54 +0000790 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000791 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000792 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000793 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
794 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
795 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
796 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
797 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
798 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
Chris Lattnera45664f2008-11-10 02:56:27 +0000799 };
Devang Patele4b27562009-08-28 23:24:31 +0000800 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000801}
802
Devang Patelac16d442009-10-26 16:54:35 +0000803
804/// CreateBasicType - Create a basic type like int, float, etc.
805DIBasicType DIFactory::CreateBasicTypeEx(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000806 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000807 DIFile F,
Devang Patelac16d442009-10-26 16:54:35 +0000808 unsigned LineNumber,
809 Constant *SizeInBits,
810 Constant *AlignInBits,
811 Constant *OffsetInBits, unsigned Flags,
812 unsigned Encoding) {
813 Value *Elts[] = {
814 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patel2db49d72010-05-07 18:11:54 +0000815 Context,
Devang Patelac16d442009-10-26 16:54:35 +0000816 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000817 F,
Devang Patelac16d442009-10-26 16:54:35 +0000818 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
819 SizeInBits,
820 AlignInBits,
821 OffsetInBits,
822 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
823 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
824 };
825 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
826}
827
Devang Patelb4645642010-02-06 01:02:37 +0000828/// CreateArtificialType - Create a new DIType with "artificial" flag set.
829DIType DIFactory::CreateArtificialType(DIType Ty) {
830 if (Ty.isArtificial())
831 return Ty;
832
833 SmallVector<Value *, 9> Elts;
Devang Patel2db49d72010-05-07 18:11:54 +0000834 MDNode *N = Ty;
Devang Patelb4645642010-02-06 01:02:37 +0000835 assert (N && "Unexpected input DIType!");
836 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
837 if (Value *V = N->getOperand(i))
838 Elts.push_back(V);
839 else
840 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
841 }
842
843 unsigned CurFlags = Ty.getFlags();
844 CurFlags = CurFlags | DIType::FlagArtificial;
845
846 // Flags are stored at this slot.
847 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
848
849 return DIType(MDNode::get(VMContext, Elts.data(), Elts.size()));
850}
Devang Patelac16d442009-10-26 16:54:35 +0000851
Chris Lattnera45664f2008-11-10 02:56:27 +0000852/// CreateDerivedType - Create a derived type like const qualified type,
853/// pointer, typedef, etc.
854DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
855 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000856 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000857 DIFile F,
Chris Lattnera45664f2008-11-10 02:56:27 +0000858 unsigned LineNumber,
859 uint64_t SizeInBits,
860 uint64_t AlignInBits,
861 uint64_t OffsetInBits,
862 unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000863 DIType DerivedFrom) {
Devang Patele4b27562009-08-28 23:24:31 +0000864 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000865 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000866 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000867 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000868 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000869 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
870 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
871 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
872 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
873 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000874 DerivedFrom,
Chris Lattnera45664f2008-11-10 02:56:27 +0000875 };
Devang Patele4b27562009-08-28 23:24:31 +0000876 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000877}
878
Devang Patelac16d442009-10-26 16:54:35 +0000879
880/// CreateDerivedType - Create a derived type like const qualified type,
881/// pointer, typedef, etc.
882DIDerivedType DIFactory::CreateDerivedTypeEx(unsigned Tag,
883 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000884 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000885 DIFile F,
Devang Patelac16d442009-10-26 16:54:35 +0000886 unsigned LineNumber,
887 Constant *SizeInBits,
888 Constant *AlignInBits,
889 Constant *OffsetInBits,
890 unsigned Flags,
891 DIType DerivedFrom) {
892 Value *Elts[] = {
893 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000894 Context,
Devang Patelac16d442009-10-26 16:54:35 +0000895 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000896 F,
Devang Patelac16d442009-10-26 16:54:35 +0000897 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
898 SizeInBits,
899 AlignInBits,
900 OffsetInBits,
901 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000902 DerivedFrom,
Devang Patelac16d442009-10-26 16:54:35 +0000903 };
904 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
905}
906
907
Chris Lattnera45664f2008-11-10 02:56:27 +0000908/// CreateCompositeType - Create a composite type like array, struct, etc.
909DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
910 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000911 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000912 DIFile F,
Chris Lattnera45664f2008-11-10 02:56:27 +0000913 unsigned LineNumber,
914 uint64_t SizeInBits,
915 uint64_t AlignInBits,
916 uint64_t OffsetInBits,
917 unsigned Flags,
918 DIType DerivedFrom,
Devang Patel13319ce2009-02-17 22:43:44 +0000919 DIArray Elements,
Devang Patel0fd7f9d2010-01-26 21:14:59 +0000920 unsigned RuntimeLang,
921 MDNode *ContainingType) {
Owen Andersone277fed2009-07-07 16:31:25 +0000922
Devang Patele4b27562009-08-28 23:24:31 +0000923 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000924 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000925 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000926 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000927 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000928 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
929 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
930 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
931 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
932 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000933 DerivedFrom,
934 Elements,
Devang Patel0fd7f9d2010-01-26 21:14:59 +0000935 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
936 ContainingType
Chris Lattnera45664f2008-11-10 02:56:27 +0000937 };
Devang Patele7e5a0f2010-08-10 20:01:20 +0000938
939 MDNode *Node = MDNode::get(VMContext, &Elts[0], 13);
940 // Create a named metadata so that we do not lose this enum info.
941 if (Tag == dwarf::DW_TAG_enumeration_type) {
942 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.enum");
943 NMD->addOperand(Node);
944 }
945 return DICompositeType(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +0000946}
947
948
Dan Gohman489b29b2010-08-20 22:02:26 +0000949/// CreateTemporaryType - Create a temporary forward-declared type.
Dan Gohmana3833f12010-08-20 22:39:47 +0000950DIType DIFactory::CreateTemporaryType() {
Dan Gohman489b29b2010-08-20 22:02:26 +0000951 // Give the temporary MDNode a tag. It doesn't matter what tag we
952 // use here as long as DIType accepts it.
953 Value *Elts[] = {
954 GetTagConstant(DW_TAG_base_type)
955 };
956 MDNode *Node = MDNode::getTemporary(VMContext, Elts, array_lengthof(Elts));
957 return DIType(Node);
958}
959
960
Devang Patelac16d442009-10-26 16:54:35 +0000961/// CreateCompositeType - Create a composite type like array, struct, etc.
962DICompositeType DIFactory::CreateCompositeTypeEx(unsigned Tag,
963 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000964 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000965 DIFile F,
Devang Patelac16d442009-10-26 16:54:35 +0000966 unsigned LineNumber,
967 Constant *SizeInBits,
968 Constant *AlignInBits,
969 Constant *OffsetInBits,
970 unsigned Flags,
971 DIType DerivedFrom,
972 DIArray Elements,
Devang Patel6bf058c2010-08-10 20:22:49 +0000973 unsigned RuntimeLang,
974 MDNode *ContainingType) {
Devang Patelac16d442009-10-26 16:54:35 +0000975 Value *Elts[] = {
976 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000977 Context,
Devang Patelac16d442009-10-26 16:54:35 +0000978 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000979 F,
Devang Patelac16d442009-10-26 16:54:35 +0000980 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
981 SizeInBits,
982 AlignInBits,
983 OffsetInBits,
984 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000985 DerivedFrom,
986 Elements,
Devang Patel6bf058c2010-08-10 20:22:49 +0000987 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
988 ContainingType
Devang Patelac16d442009-10-26 16:54:35 +0000989 };
Devang Patel6bf058c2010-08-10 20:22:49 +0000990 MDNode *Node = MDNode::get(VMContext, &Elts[0], 13);
Devang Patele7e5a0f2010-08-10 20:01:20 +0000991 // Create a named metadata so that we do not lose this enum info.
992 if (Tag == dwarf::DW_TAG_enumeration_type) {
993 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.enum");
994 NMD->addOperand(Node);
995 }
996 return DICompositeType(Node);
Devang Patelac16d442009-10-26 16:54:35 +0000997}
998
999
Chris Lattnera45664f2008-11-10 02:56:27 +00001000/// CreateSubprogram - Create a new descriptor for the specified subprogram.
1001/// See comments in DISubprogram for descriptions of these fields. This
1002/// method does not unique the generated descriptors.
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001003DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +00001004 StringRef Name,
1005 StringRef DisplayName,
1006 StringRef LinkageName,
Devang Patel4b945502010-03-09 00:44:10 +00001007 DIFile F,
Devang Patel2e369932010-01-23 00:26:28 +00001008 unsigned LineNo, DIType Ty,
Chris Lattnera45664f2008-11-10 02:56:27 +00001009 bool isLocalToUnit,
Devang Patel5d11eb02009-12-03 19:11:07 +00001010 bool isDefinition,
1011 unsigned VK, unsigned VIndex,
Devang Patel4e0d19d2010-02-03 19:57:19 +00001012 DIType ContainingType,
Devang Patelccff8122010-04-30 19:38:23 +00001013 bool isArtificial,
Stuart Hastings215aa152010-06-11 20:08:44 +00001014 bool isOptimized,
1015 Function *Fn) {
Devang Patel854967e2008-12-17 22:39:29 +00001016
Devang Patele4b27562009-08-28 23:24:31 +00001017 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001018 GetTagConstant(dwarf::DW_TAG_subprogram),
Devang Patele4b27562009-08-28 23:24:31 +00001019 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Devang Patel2db49d72010-05-07 18:11:54 +00001020 Context,
Devang Patele4b27562009-08-28 23:24:31 +00001021 MDString::get(VMContext, Name),
1022 MDString::get(VMContext, DisplayName),
1023 MDString::get(VMContext, LinkageName),
Devang Patel2db49d72010-05-07 18:11:54 +00001024 F,
Owen Anderson1d0be152009-08-13 21:58:54 +00001025 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001026 Ty,
Owen Anderson1d0be152009-08-13 21:58:54 +00001027 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
Devang Patel5d11eb02009-12-03 19:11:07 +00001028 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1029 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
1030 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
Devang Patel2db49d72010-05-07 18:11:54 +00001031 ContainingType,
Devang Patelccff8122010-04-30 19:38:23 +00001032 ConstantInt::get(Type::getInt1Ty(VMContext), isArtificial),
Stuart Hastings215aa152010-06-11 20:08:44 +00001033 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
1034 Fn
Chris Lattnera45664f2008-11-10 02:56:27 +00001035 };
Devang Patelfd5fdc32010-06-28 05:53:08 +00001036 MDNode *Node = MDNode::get(VMContext, &Elts[0], 17);
1037
1038 // Create a named metadata so that we do not lose this mdnode.
1039 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
1040 NMD->addOperand(Node);
1041 return DISubprogram(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +00001042}
1043
Devang Patele3a18de2009-12-01 23:09:02 +00001044/// CreateSubprogramDefinition - Create new subprogram descriptor for the
Jim Grosbache62b6902010-07-21 21:36:25 +00001045/// given declaration.
1046DISubprogram DIFactory::CreateSubprogramDefinition(DISubprogram &SPDeclaration){
Devang Patele3a18de2009-12-01 23:09:02 +00001047 if (SPDeclaration.isDefinition())
Devang Patel2db49d72010-05-07 18:11:54 +00001048 return DISubprogram(SPDeclaration);
Devang Patele3a18de2009-12-01 23:09:02 +00001049
Devang Patel2db49d72010-05-07 18:11:54 +00001050 MDNode *DeclNode = SPDeclaration;
Devang Patele3a18de2009-12-01 23:09:02 +00001051 Value *Elts[] = {
1052 GetTagConstant(dwarf::DW_TAG_subprogram),
1053 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001054 DeclNode->getOperand(2), // Context
1055 DeclNode->getOperand(3), // Name
1056 DeclNode->getOperand(4), // DisplayName
1057 DeclNode->getOperand(5), // LinkageName
1058 DeclNode->getOperand(6), // CompileUnit
1059 DeclNode->getOperand(7), // LineNo
1060 DeclNode->getOperand(8), // Type
1061 DeclNode->getOperand(9), // isLocalToUnit
Stuart Hastings6d56b9f2010-06-05 00:39:29 +00001062 ConstantInt::get(Type::getInt1Ty(VMContext), true),
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001063 DeclNode->getOperand(11), // Virtuality
1064 DeclNode->getOperand(12), // VIndex
Devang Patel4e0d19d2010-02-03 19:57:19 +00001065 DeclNode->getOperand(13), // Containting Type
Devang Patelccff8122010-04-30 19:38:23 +00001066 DeclNode->getOperand(14), // isArtificial
Devang Patelacc6efa2010-06-27 21:04:31 +00001067 DeclNode->getOperand(15), // isOptimized
1068 SPDeclaration.getFunction()
Devang Patele3a18de2009-12-01 23:09:02 +00001069 };
Devang Patelfd5fdc32010-06-28 05:53:08 +00001070 MDNode *Node =MDNode::get(VMContext, &Elts[0], 16);
1071
1072 // Create a named metadata so that we do not lose this mdnode.
1073 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
1074 NMD->addOperand(Node);
1075 return DISubprogram(Node);
Devang Patele3a18de2009-12-01 23:09:02 +00001076}
1077
Chris Lattnera45664f2008-11-10 02:56:27 +00001078/// CreateGlobalVariable - Create a new descriptor for the specified global.
1079DIGlobalVariable
Devang Patel65dbc902009-11-25 17:36:49 +00001080DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
1081 StringRef DisplayName,
1082 StringRef LinkageName,
Devang Patel4b945502010-03-09 00:44:10 +00001083 DIFile F,
Devang Patel2e369932010-01-23 00:26:28 +00001084 unsigned LineNo, DIType Ty,bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +00001085 bool isDefinition, llvm::GlobalVariable *Val) {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001086 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001087 GetTagConstant(dwarf::DW_TAG_variable),
Devang Patele4b27562009-08-28 23:24:31 +00001088 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Devang Patel2db49d72010-05-07 18:11:54 +00001089 Context,
Devang Patele4b27562009-08-28 23:24:31 +00001090 MDString::get(VMContext, Name),
1091 MDString::get(VMContext, DisplayName),
1092 MDString::get(VMContext, LinkageName),
Devang Patel2db49d72010-05-07 18:11:54 +00001093 F,
Owen Anderson1d0be152009-08-13 21:58:54 +00001094 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001095 Ty,
Owen Anderson1d0be152009-08-13 21:58:54 +00001096 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1097 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
Devang Patele4b27562009-08-28 23:24:31 +00001098 Val
Chris Lattnera45664f2008-11-10 02:56:27 +00001099 };
Devang Patele4b27562009-08-28 23:24:31 +00001100
1101 Value *const *Vs = &Elts[0];
1102 MDNode *Node = MDNode::get(VMContext,Vs, 12);
1103
1104 // Create a named metadata so that we do not lose this mdnode.
1105 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001106 NMD->addOperand(Node);
Devang Patele4b27562009-08-28 23:24:31 +00001107
1108 return DIGlobalVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +00001109}
1110
Devang Patel27398962010-08-09 21:39:24 +00001111/// CreateGlobalVariable - Create a new descriptor for the specified constant.
1112DIGlobalVariable
1113DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
1114 StringRef DisplayName,
1115 StringRef LinkageName,
1116 DIFile F,
1117 unsigned LineNo, DIType Ty,bool isLocalToUnit,
1118 bool isDefinition, llvm::Constant *Val) {
1119 Value *Elts[] = {
Devang Patelebd53742010-08-11 23:17:54 +00001120 GetTagConstant(dwarf::DW_TAG_variable),
Devang Patel27398962010-08-09 21:39:24 +00001121 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
1122 Context,
1123 MDString::get(VMContext, Name),
1124 MDString::get(VMContext, DisplayName),
1125 MDString::get(VMContext, LinkageName),
1126 F,
1127 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1128 Ty,
1129 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1130 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1131 Val
1132 };
1133
1134 Value *const *Vs = &Elts[0];
1135 MDNode *Node = MDNode::get(VMContext,Vs, 12);
1136
1137 // Create a named metadata so that we do not lose this mdnode.
1138 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
1139 NMD->addOperand(Node);
1140
1141 return DIGlobalVariable(Node);
1142}
Chris Lattnera45664f2008-11-10 02:56:27 +00001143
1144/// CreateVariable - Create a new descriptor for the specified variable.
1145DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +00001146 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +00001147 DIFile F,
1148 unsigned LineNo,
Devang Patel6ed0ce32010-05-20 20:35:24 +00001149 DIType Ty, bool AlwaysPreserve) {
Devang Patele4b27562009-08-28 23:24:31 +00001150 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001151 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +00001152 Context,
Devang Patele4b27562009-08-28 23:24:31 +00001153 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +00001154 F,
Owen Anderson1d0be152009-08-13 21:58:54 +00001155 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001156 Ty,
Chris Lattnera45664f2008-11-10 02:56:27 +00001157 };
Devang Patel98e1cac2010-05-14 21:01:35 +00001158 MDNode *Node = MDNode::get(VMContext, &Elts[0], 6);
Devang Patel6ed0ce32010-05-20 20:35:24 +00001159 if (AlwaysPreserve) {
1160 // The optimizer may remove local variable. If there is an interest
1161 // to preserve variable info in such situation then stash it in a
1162 // named mdnode.
Devang Patel2f7d5292010-06-16 00:53:55 +00001163 DISubprogram Fn(getDISubprogram(Context));
Devang Patel10de3bb2010-06-21 18:36:58 +00001164 StringRef FName = "fn";
1165 if (Fn.getFunction())
1166 FName = Fn.getFunction()->getName();
Devang Patel10de3bb2010-06-21 18:36:58 +00001167 char One = '\1';
1168 if (FName.startswith(StringRef(&One, 1)))
1169 FName = FName.substr(1);
Dan Gohman17aa92c2010-07-21 23:38:33 +00001170
1171 SmallString<32> Out;
1172 NamedMDNode *FnLocals =
1173 M.getOrInsertNamedMetadata(Twine("llvm.dbg.lv.", FName).toStringRef(Out));
Devang Patel2f7d5292010-06-16 00:53:55 +00001174 FnLocals->addOperand(Node);
Devang Patel98e1cac2010-05-14 21:01:35 +00001175 }
1176 return DIVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +00001177}
1178
1179
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001180/// CreateComplexVariable - Create a new descriptor for the specified variable
1181/// which has a complex address expression for its address.
1182DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
1183 const std::string &Name,
Devang Patel4b945502010-03-09 00:44:10 +00001184 DIFile F,
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001185 unsigned LineNo,
Jim Grosbache62b6902010-07-21 21:36:25 +00001186 DIType Ty,
Devang Patel2e369932010-01-23 00:26:28 +00001187 SmallVector<Value *, 9> &addr) {
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001188 SmallVector<Value *, 9> Elts;
1189 Elts.push_back(GetTagConstant(Tag));
Devang Patel2db49d72010-05-07 18:11:54 +00001190 Elts.push_back(Context);
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001191 Elts.push_back(MDString::get(VMContext, Name));
Devang Patel2db49d72010-05-07 18:11:54 +00001192 Elts.push_back(F);
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001193 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
Devang Patel2db49d72010-05-07 18:11:54 +00001194 Elts.push_back(Ty);
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001195 Elts.insert(Elts.end(), addr.begin(), addr.end());
1196
1197 return DIVariable(MDNode::get(VMContext, &Elts[0], 6+addr.size()));
1198}
1199
1200
Chris Lattnera45664f2008-11-10 02:56:27 +00001201/// CreateBlock - This creates a descriptor for a lexical block with the
Owen Anderson99035272009-07-07 17:12:53 +00001202/// specified parent VMContext.
Devang Patel3d821aa2010-02-16 21:39:34 +00001203DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context,
Stuart Hastings0db42712010-07-19 23:56:30 +00001204 DIFile F, unsigned LineNo,
1205 unsigned Col) {
1206 // Defeat MDNode uniqing for lexical blocks.
1207 static unsigned int unique_id = 0;
Devang Patele4b27562009-08-28 23:24:31 +00001208 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001209 GetTagConstant(dwarf::DW_TAG_lexical_block),
Devang Patel2db49d72010-05-07 18:11:54 +00001210 Context,
Devang Patel3d821aa2010-02-16 21:39:34 +00001211 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Stuart Hastings0db42712010-07-19 23:56:30 +00001212 ConstantInt::get(Type::getInt32Ty(VMContext), Col),
1213 F,
1214 ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
Chris Lattnera45664f2008-11-10 02:56:27 +00001215 };
Stuart Hastings0db42712010-07-19 23:56:30 +00001216 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 6));
Chris Lattnera45664f2008-11-10 02:56:27 +00001217}
1218
Devang Patel6404e4e2009-12-15 19:16:48 +00001219/// CreateNameSpace - This creates new descriptor for a namespace
1220/// with the specified parent context.
1221DINameSpace DIFactory::CreateNameSpace(DIDescriptor Context, StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +00001222 DIFile F,
Devang Patel6404e4e2009-12-15 19:16:48 +00001223 unsigned LineNo) {
1224 Value *Elts[] = {
1225 GetTagConstant(dwarf::DW_TAG_namespace),
Devang Patel2db49d72010-05-07 18:11:54 +00001226 Context,
Devang Patel6404e4e2009-12-15 19:16:48 +00001227 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +00001228 F,
Devang Patel6404e4e2009-12-15 19:16:48 +00001229 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1230 };
1231 return DINameSpace(MDNode::get(VMContext, &Elts[0], 5));
1232}
1233
Devang Patelf98d8fe2009-09-01 01:14:15 +00001234/// CreateLocation - Creates a debug info location.
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001235DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
Daniel Dunbara279bc32009-09-20 02:20:51 +00001236 DIScope S, DILocation OrigLoc) {
Devang Patelf98d8fe2009-09-01 01:14:15 +00001237 Value *Elts[] = {
1238 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1239 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001240 S,
1241 OrigLoc,
Devang Patelf98d8fe2009-09-01 01:14:15 +00001242 };
1243 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1244}
1245
Chris Lattnera45664f2008-11-10 02:56:27 +00001246//===----------------------------------------------------------------------===//
1247// DIFactory: Routines for inserting code into a function
1248//===----------------------------------------------------------------------===//
1249
Chris Lattnera45664f2008-11-10 02:56:27 +00001250/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Devang Patel6daf99b2009-11-10 22:05:35 +00001251Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001252 Instruction *InsertBefore) {
Victor Hernandez4cf292a2010-01-26 02:07:38 +00001253 assert(Storage && "no storage passed to dbg.declare");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001254 assert(D.Verify() && "empty DIVariable passed to dbg.declare");
Chris Lattnera45664f2008-11-10 02:56:27 +00001255 if (!DeclareFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +00001256 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1257
Victor Hernandez756462b2010-01-18 20:42:09 +00001258 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
Devang Patel2db49d72010-05-07 18:11:54 +00001259 D };
Devang Patel6daf99b2009-11-10 22:05:35 +00001260 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
Mike Stumpe4250392009-10-01 22:08:58 +00001261}
1262
1263/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Devang Patel6daf99b2009-11-10 22:05:35 +00001264Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001265 BasicBlock *InsertAtEnd) {
Victor Hernandez4cf292a2010-01-26 02:07:38 +00001266 assert(Storage && "no storage passed to dbg.declare");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001267 assert(D.Verify() && "invalid DIVariable passed to dbg.declare");
Mike Stumpe4250392009-10-01 22:08:58 +00001268 if (!DeclareFn)
1269 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1270
Victor Hernandez756462b2010-01-18 20:42:09 +00001271 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
Devang Patel2db49d72010-05-07 18:11:54 +00001272 D };
Devang Patel27a53de2010-01-29 18:30:57 +00001273
1274 // If this block already has a terminator then insert this intrinsic
1275 // before the terminator.
Jim Grosbache62b6902010-07-21 21:36:25 +00001276 if (TerminatorInst *T = InsertAtEnd->getTerminator())
Devang Patel27a53de2010-01-29 18:30:57 +00001277 return CallInst::Create(DeclareFn, Args, Args+2, "", T);
1278 else
1279 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);}
Torok Edwin620f2802008-12-16 09:07:36 +00001280
Victor Hernandezc59b3352009-12-07 21:54:43 +00001281/// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001282Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
Victor Hernandezc59b3352009-12-07 21:54:43 +00001283 DIVariable D,
1284 Instruction *InsertBefore) {
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001285 assert(V && "no value passed to dbg.value");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001286 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001287 if (!ValueFn)
1288 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1289
Victor Hernandezc8b7cd02010-01-20 05:44:11 +00001290 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001291 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
Devang Patel2db49d72010-05-07 18:11:54 +00001292 D };
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001293 return CallInst::Create(ValueFn, Args, Args+3, "", InsertBefore);
1294}
1295
Victor Hernandezc59b3352009-12-07 21:54:43 +00001296/// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001297Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
Victor Hernandezc59b3352009-12-07 21:54:43 +00001298 DIVariable D,
1299 BasicBlock *InsertAtEnd) {
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001300 assert(V && "no value passed to dbg.value");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001301 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001302 if (!ValueFn)
1303 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1304
Jim Grosbache62b6902010-07-21 21:36:25 +00001305 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001306 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
Devang Patel2db49d72010-05-07 18:11:54 +00001307 D };
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001308 return CallInst::Create(ValueFn, Args, Args+3, "", InsertAtEnd);
1309}
Devang Patele4b27562009-08-28 23:24:31 +00001310
Devang Pateld2f79a12009-07-28 19:55:13 +00001311//===----------------------------------------------------------------------===//
Devang Patel98c65172009-07-30 18:25:15 +00001312// DebugInfoFinder implementations.
Devang Pateld2f79a12009-07-28 19:55:13 +00001313//===----------------------------------------------------------------------===//
1314
Devang Patel98c65172009-07-30 18:25:15 +00001315/// processModule - Process entire module and collect debug info.
1316void DebugInfoFinder::processModule(Module &M) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001317 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1318 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
1319 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
1320 ++BI) {
Devang Patel01c5ff62010-05-04 01:05:02 +00001321 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
Devang Patelb4d31302009-07-31 18:18:52 +00001322 processDeclare(DDI);
Jim Grosbache62b6902010-07-21 21:36:25 +00001323
Chris Lattner28a9bf62010-04-02 20:44:29 +00001324 DebugLoc Loc = BI->getDebugLoc();
1325 if (Loc.isUnknown())
1326 continue;
Jim Grosbache62b6902010-07-21 21:36:25 +00001327
Chris Lattner28a9bf62010-04-02 20:44:29 +00001328 LLVMContext &Ctx = BI->getContext();
1329 DIDescriptor Scope(Loc.getScope(Ctx));
Jim Grosbache62b6902010-07-21 21:36:25 +00001330
Chris Lattner28a9bf62010-04-02 20:44:29 +00001331 if (Scope.isCompileUnit())
Devang Patel2db49d72010-05-07 18:11:54 +00001332 addCompileUnit(DICompileUnit(Scope));
Chris Lattner28a9bf62010-04-02 20:44:29 +00001333 else if (Scope.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001334 processSubprogram(DISubprogram(Scope));
Chris Lattner28a9bf62010-04-02 20:44:29 +00001335 else if (Scope.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001336 processLexicalBlock(DILexicalBlock(Scope));
Jim Grosbache62b6902010-07-21 21:36:25 +00001337
Chris Lattner28a9bf62010-04-02 20:44:29 +00001338 if (MDNode *IA = Loc.getInlinedAt(Ctx))
1339 processLocation(DILocation(IA));
Devang Pateld2f79a12009-07-28 19:55:13 +00001340 }
Devang Patele4b27562009-08-28 23:24:31 +00001341
Devang Patelfd5fdc32010-06-28 05:53:08 +00001342 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
1343 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1344 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
1345 if (addGlobalVariable(DIG)) {
1346 addCompileUnit(DIG.getCompileUnit());
1347 processType(DIG.getType());
1348 }
Devang Pateld2f79a12009-07-28 19:55:13 +00001349 }
1350 }
Devang Patelfd5fdc32010-06-28 05:53:08 +00001351
1352 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
1353 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
1354 processSubprogram(DISubprogram(NMD->getOperand(i)));
Devang Pateld2f79a12009-07-28 19:55:13 +00001355}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001356
Devang Patel6daf99b2009-11-10 22:05:35 +00001357/// processLocation - Process DILocation.
1358void DebugInfoFinder::processLocation(DILocation Loc) {
Devang Patel3c91b052010-03-08 20:52:55 +00001359 if (!Loc.Verify()) return;
Devang Patel2db49d72010-05-07 18:11:54 +00001360 DIDescriptor S(Loc.getScope());
Devang Patel6daf99b2009-11-10 22:05:35 +00001361 if (S.isCompileUnit())
Devang Patel2db49d72010-05-07 18:11:54 +00001362 addCompileUnit(DICompileUnit(S));
Devang Patel6daf99b2009-11-10 22:05:35 +00001363 else if (S.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001364 processSubprogram(DISubprogram(S));
Devang Patel6daf99b2009-11-10 22:05:35 +00001365 else if (S.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001366 processLexicalBlock(DILexicalBlock(S));
Devang Patel6daf99b2009-11-10 22:05:35 +00001367 processLocation(Loc.getOrigLocation());
1368}
1369
Devang Patel98c65172009-07-30 18:25:15 +00001370/// processType - Process DIType.
1371void DebugInfoFinder::processType(DIType DT) {
Devang Patel72bcdb62009-08-10 22:09:58 +00001372 if (!addType(DT))
Devang Pateld2f79a12009-07-28 19:55:13 +00001373 return;
1374
1375 addCompileUnit(DT.getCompileUnit());
Devang Patel6ceea332009-08-31 18:49:10 +00001376 if (DT.isCompositeType()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001377 DICompositeType DCT(DT);
Devang Patel98c65172009-07-30 18:25:15 +00001378 processType(DCT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001379 DIArray DA = DCT.getTypeArray();
Devang Patel3c91b052010-03-08 20:52:55 +00001380 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1381 DIDescriptor D = DA.getElement(i);
1382 if (D.isType())
Devang Patel2db49d72010-05-07 18:11:54 +00001383 processType(DIType(D));
Devang Patel3c91b052010-03-08 20:52:55 +00001384 else if (D.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001385 processSubprogram(DISubprogram(D));
Devang Patel3c91b052010-03-08 20:52:55 +00001386 }
Devang Patel6ceea332009-08-31 18:49:10 +00001387 } else if (DT.isDerivedType()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001388 DIDerivedType DDT(DT);
Devang Patel3c91b052010-03-08 20:52:55 +00001389 processType(DDT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001390 }
1391}
1392
Devang Patelbeab41b2009-10-07 22:04:08 +00001393/// processLexicalBlock
1394void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
Devang Patelbeab41b2009-10-07 22:04:08 +00001395 DIScope Context = LB.getContext();
1396 if (Context.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001397 return processLexicalBlock(DILexicalBlock(Context));
Devang Patelbeab41b2009-10-07 22:04:08 +00001398 else
Devang Patel2db49d72010-05-07 18:11:54 +00001399 return processSubprogram(DISubprogram(Context));
Devang Patelbeab41b2009-10-07 22:04:08 +00001400}
1401
Devang Patel98c65172009-07-30 18:25:15 +00001402/// processSubprogram - Process DISubprogram.
1403void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001404 if (!addSubprogram(SP))
1405 return;
1406 addCompileUnit(SP.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +00001407 processType(SP.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +00001408}
1409
Devang Patelb4d31302009-07-31 18:18:52 +00001410/// processDeclare - Process DbgDeclareInst.
1411void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
Devang Patel3c91b052010-03-08 20:52:55 +00001412 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1413 if (!N) return;
1414
1415 DIDescriptor DV(N);
1416 if (!DV.isVariable())
Devang Patelb4d31302009-07-31 18:18:52 +00001417 return;
1418
Devang Patel2db49d72010-05-07 18:11:54 +00001419 if (!NodesSeen.insert(DV))
Devang Patelb4d31302009-07-31 18:18:52 +00001420 return;
1421
Devang Patel3c91b052010-03-08 20:52:55 +00001422 addCompileUnit(DIVariable(N).getCompileUnit());
1423 processType(DIVariable(N).getType());
Devang Patelb4d31302009-07-31 18:18:52 +00001424}
1425
Devang Patel72bcdb62009-08-10 22:09:58 +00001426/// addType - Add type into Tys.
1427bool DebugInfoFinder::addType(DIType DT) {
Devang Patel3c91b052010-03-08 20:52:55 +00001428 if (!DT.isValid())
Devang Patel72bcdb62009-08-10 22:09:58 +00001429 return false;
1430
Devang Patel2db49d72010-05-07 18:11:54 +00001431 if (!NodesSeen.insert(DT))
Devang Patel72bcdb62009-08-10 22:09:58 +00001432 return false;
1433
Devang Patel2db49d72010-05-07 18:11:54 +00001434 TYs.push_back(DT);
Devang Patel72bcdb62009-08-10 22:09:58 +00001435 return true;
1436}
1437
Devang Pateld2f79a12009-07-28 19:55:13 +00001438/// addCompileUnit - Add compile unit into CUs.
Devang Patel98c65172009-07-30 18:25:15 +00001439bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Patel3c91b052010-03-08 20:52:55 +00001440 if (!CU.Verify())
Devang Pateld2f79a12009-07-28 19:55:13 +00001441 return false;
1442
Devang Patel2db49d72010-05-07 18:11:54 +00001443 if (!NodesSeen.insert(CU))
Devang Pateld2f79a12009-07-28 19:55:13 +00001444 return false;
1445
Devang Patel2db49d72010-05-07 18:11:54 +00001446 CUs.push_back(CU);
Devang Pateld2f79a12009-07-28 19:55:13 +00001447 return true;
1448}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001449
Devang Pateld2f79a12009-07-28 19:55:13 +00001450/// addGlobalVariable - Add global variable into GVs.
Devang Patel98c65172009-07-30 18:25:15 +00001451bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Patel2db49d72010-05-07 18:11:54 +00001452 if (!DIDescriptor(DIG).isGlobalVariable())
Devang Pateld2f79a12009-07-28 19:55:13 +00001453 return false;
1454
Devang Patel2db49d72010-05-07 18:11:54 +00001455 if (!NodesSeen.insert(DIG))
Devang Pateld2f79a12009-07-28 19:55:13 +00001456 return false;
1457
Devang Patel2db49d72010-05-07 18:11:54 +00001458 GVs.push_back(DIG);
Devang Pateld2f79a12009-07-28 19:55:13 +00001459 return true;
1460}
1461
1462// addSubprogram - Add subprgoram into SPs.
Devang Patel98c65172009-07-30 18:25:15 +00001463bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Patel2db49d72010-05-07 18:11:54 +00001464 if (!DIDescriptor(SP).isSubprogram())
Devang Pateld2f79a12009-07-28 19:55:13 +00001465 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001466
Devang Patel2db49d72010-05-07 18:11:54 +00001467 if (!NodesSeen.insert(SP))
Devang Pateld2f79a12009-07-28 19:55:13 +00001468 return false;
1469
Devang Patel2db49d72010-05-07 18:11:54 +00001470 SPs.push_back(SP);
Devang Pateld2f79a12009-07-28 19:55:13 +00001471 return true;
1472}
1473
Victor Hernandez756462b2010-01-18 20:42:09 +00001474/// Find the debug info descriptor corresponding to this global variable.
1475static Value *findDbgGlobalDeclare(GlobalVariable *V) {
Chris Lattner099b7792009-12-29 09:22:47 +00001476 const Module *M = V->getParent();
1477 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1478 if (!NMD)
Torok Edwinff7d0e92009-03-10 13:41:26 +00001479 return 0;
Chris Lattner099b7792009-12-29 09:22:47 +00001480
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001481 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman872814a2010-07-21 18:54:18 +00001482 DIDescriptor DIG(cast<MDNode>(NMD->getOperand(i)));
Devang Patel3c91b052010-03-08 20:52:55 +00001483 if (!DIG.isGlobalVariable())
Chris Lattner099b7792009-12-29 09:22:47 +00001484 continue;
Devang Patel2db49d72010-05-07 18:11:54 +00001485 if (DIGlobalVariable(DIG).getGlobal() == V)
1486 return DIG;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001487 }
Chris Lattner099b7792009-12-29 09:22:47 +00001488 return 0;
1489}
Torok Edwinff7d0e92009-03-10 13:41:26 +00001490
Chris Lattner099b7792009-12-29 09:22:47 +00001491/// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
1492/// It looks through pointer casts too.
Victor Hernandez756462b2010-01-18 20:42:09 +00001493static const DbgDeclareInst *findDbgDeclare(const Value *V) {
Victor Hernandez3a328652010-01-15 19:04:09 +00001494 V = V->stripPointerCasts();
Jim Grosbache62b6902010-07-21 21:36:25 +00001495
Victor Hernandez3a328652010-01-15 19:04:09 +00001496 if (!isa<Instruction>(V) && !isa<Argument>(V))
Torok Edwin620f2802008-12-16 09:07:36 +00001497 return 0;
Jim Grosbache62b6902010-07-21 21:36:25 +00001498
Victor Hernandez3a328652010-01-15 19:04:09 +00001499 const Function *F = NULL;
1500 if (const Instruction *I = dyn_cast<Instruction>(V))
1501 F = I->getParent()->getParent();
1502 else if (const Argument *A = dyn_cast<Argument>(V))
1503 F = A->getParent();
Jim Grosbache62b6902010-07-21 21:36:25 +00001504
Victor Hernandez3a328652010-01-15 19:04:09 +00001505 for (Function::const_iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
1506 for (BasicBlock::const_iterator BI = (*FI).begin(), BE = (*FI).end();
1507 BI != BE; ++BI)
1508 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1509 if (DDI->getAddress() == V)
1510 return DDI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001511
Chris Lattner099b7792009-12-29 09:22:47 +00001512 return 0;
1513}
Bill Wendlingdc817b62009-05-14 18:26:15 +00001514
Chris Lattner099b7792009-12-29 09:22:47 +00001515bool llvm::getLocationInfo(const Value *V, std::string &DisplayName,
1516 std::string &Type, unsigned &LineNo,
1517 std::string &File, std::string &Dir) {
1518 DICompileUnit Unit;
1519 DIType TypeD;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001520
Chris Lattner099b7792009-12-29 09:22:47 +00001521 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1522 Value *DIGV = findDbgGlobalDeclare(GV);
1523 if (!DIGV) return false;
1524 DIGlobalVariable Var(cast<MDNode>(DIGV));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001525
Chris Lattner099b7792009-12-29 09:22:47 +00001526 StringRef D = Var.getDisplayName();
Devang Patel65dbc902009-11-25 17:36:49 +00001527 if (!D.empty())
Chris Lattner099b7792009-12-29 09:22:47 +00001528 DisplayName = D;
1529 LineNo = Var.getLineNumber();
1530 Unit = Var.getCompileUnit();
1531 TypeD = Var.getType();
1532 } else {
1533 const DbgDeclareInst *DDI = findDbgDeclare(V);
1534 if (!DDI) return false;
1535 DIVariable Var(cast<MDNode>(DDI->getVariable()));
1536
1537 StringRef D = Var.getName();
1538 if (!D.empty())
1539 DisplayName = D;
1540 LineNo = Var.getLineNumber();
1541 Unit = Var.getCompileUnit();
1542 TypeD = Var.getType();
Torok Edwinff7d0e92009-03-10 13:41:26 +00001543 }
Devang Patel13e16b62009-06-26 01:49:18 +00001544
Chris Lattner099b7792009-12-29 09:22:47 +00001545 StringRef T = TypeD.getName();
1546 if (!T.empty())
1547 Type = T;
1548 StringRef F = Unit.getFilename();
1549 if (!F.empty())
1550 File = F;
1551 StringRef D = Unit.getDirectory();
1552 if (!D.empty())
1553 Dir = D;
1554 return true;
1555}
Devang Patel9e529c32009-07-02 01:15:24 +00001556
Chris Lattner099b7792009-12-29 09:22:47 +00001557/// getDISubprogram - Find subprogram that is enclosing this scope.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001558DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
Chris Lattner099b7792009-12-29 09:22:47 +00001559 DIDescriptor D(Scope);
Chris Lattner099b7792009-12-29 09:22:47 +00001560 if (D.isSubprogram())
1561 return DISubprogram(Scope);
Jim Grosbache62b6902010-07-21 21:36:25 +00001562
Chris Lattner099b7792009-12-29 09:22:47 +00001563 if (D.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001564 return getDISubprogram(DILexicalBlock(Scope).getContext());
Jim Grosbache62b6902010-07-21 21:36:25 +00001565
Chris Lattner099b7792009-12-29 09:22:47 +00001566 return DISubprogram();
1567}
Devang Patel193f7202009-11-24 01:14:22 +00001568
Chris Lattner099b7792009-12-29 09:22:47 +00001569/// getDICompositeType - Find underlying composite type.
1570DICompositeType llvm::getDICompositeType(DIType T) {
Chris Lattner099b7792009-12-29 09:22:47 +00001571 if (T.isCompositeType())
Devang Patel2db49d72010-05-07 18:11:54 +00001572 return DICompositeType(T);
Jim Grosbache62b6902010-07-21 21:36:25 +00001573
Chris Lattner099b7792009-12-29 09:22:47 +00001574 if (T.isDerivedType())
Devang Patel2db49d72010-05-07 18:11:54 +00001575 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
Jim Grosbache62b6902010-07-21 21:36:25 +00001576
Chris Lattner099b7792009-12-29 09:22:47 +00001577 return DICompositeType();
Torok Edwin620f2802008-12-16 09:07:36 +00001578}