blob: 472669c770a90d19d10c93cb3cd42d248abde1bb [file] [log] [blame]
Chris Lattnera45664f2008-11-10 02:56:27 +00001//===--- DebugInfo.cpp - Debug Information Helper Classes -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the helper classes used to build and interpret debug
11// information in LLVM IR form.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Analysis/DebugInfo.h"
16#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Intrinsics.h"
Torok Edwin620f2802008-12-16 09:07:36 +000019#include "llvm/IntrinsicInst.h"
Chris Lattnera45664f2008-11-10 02:56:27 +000020#include "llvm/Instructions.h"
21#include "llvm/Module.h"
22#include "llvm/Analysis/ValueTracking.h"
Devang Patele4b27562009-08-28 23:24:31 +000023#include "llvm/ADT/SmallPtrSet.h"
Dan Gohman17aa92c2010-07-21 23:38:33 +000024#include "llvm/ADT/SmallString.h"
David Greene0eb5b662009-12-23 19:45:49 +000025#include "llvm/Support/Debug.h"
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000026#include "llvm/Support/Dwarf.h"
Chris Lattnera81d29b2009-08-23 07:33:14 +000027#include "llvm/Support/raw_ostream.h"
Chris Lattnera45664f2008-11-10 02:56:27 +000028using namespace llvm;
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000029using namespace llvm::dwarf;
Chris Lattnera45664f2008-11-10 02:56:27 +000030
31//===----------------------------------------------------------------------===//
32// DIDescriptor
33//===----------------------------------------------------------------------===//
34
Jim Grosbache62b6902010-07-21 21:36:25 +000035StringRef
Devang Patel5ccdd102009-09-29 18:40:58 +000036DIDescriptor::getStringField(unsigned Elt) const {
Devang Patele4b27562009-08-28 23:24:31 +000037 if (DbgNode == 0)
Devang Patel65dbc902009-11-25 17:36:49 +000038 return StringRef();
Chris Lattnera45664f2008-11-10 02:56:27 +000039
Chris Lattner5d0cacd2009-12-31 01:22:29 +000040 if (Elt < DbgNode->getNumOperands())
41 if (MDString *MDS = dyn_cast_or_null<MDString>(DbgNode->getOperand(Elt)))
Devang Patel65dbc902009-11-25 17:36:49 +000042 return MDS->getString();
Daniel Dunbarf612ff62009-09-19 20:40:05 +000043
Devang Patel65dbc902009-11-25 17:36:49 +000044 return StringRef();
Chris Lattnera45664f2008-11-10 02:56:27 +000045}
46
47uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +000048 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +000049 return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +000050
Chris Lattner5d0cacd2009-12-31 01:22:29 +000051 if (Elt < DbgNode->getNumOperands())
52 if (ConstantInt *CI = dyn_cast<ConstantInt>(DbgNode->getOperand(Elt)))
Devang Patele4b27562009-08-28 23:24:31 +000053 return CI->getZExtValue();
Daniel Dunbarf612ff62009-09-19 20:40:05 +000054
Chris Lattnera45664f2008-11-10 02:56:27 +000055 return 0;
56}
57
Chris Lattnera45664f2008-11-10 02:56:27 +000058DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +000059 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +000060 return DIDescriptor();
Bill Wendlingdc817b62009-05-14 18:26:15 +000061
Chris Lattner7a2f3e02010-03-31 05:53:47 +000062 if (Elt < DbgNode->getNumOperands())
Jim Grosbache62b6902010-07-21 21:36:25 +000063 return
64 DIDescriptor(dyn_cast_or_null<const MDNode>(DbgNode->getOperand(Elt)));
Devang Patele4b27562009-08-28 23:24:31 +000065 return DIDescriptor();
Chris Lattnera45664f2008-11-10 02:56:27 +000066}
67
68GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +000069 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +000070 return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +000071
Chris Lattner5d0cacd2009-12-31 01:22:29 +000072 if (Elt < DbgNode->getNumOperands())
73 return dyn_cast_or_null<GlobalVariable>(DbgNode->getOperand(Elt));
Devang Patele4b27562009-08-28 23:24:31 +000074 return 0;
Chris Lattnera45664f2008-11-10 02:56:27 +000075}
76
Stuart Hastings215aa152010-06-11 20:08:44 +000077Function *DIDescriptor::getFunctionField(unsigned Elt) const {
78 if (DbgNode == 0)
79 return 0;
80
81 if (Elt < DbgNode->getNumOperands())
82 return dyn_cast_or_null<Function>(DbgNode->getOperand(Elt));
83 return 0;
84}
85
Chris Lattnerf0908a32009-12-31 03:02:08 +000086unsigned DIVariable::getNumAddrElements() const {
87 return DbgNode->getNumOperands()-6;
88}
89
90
Chris Lattnera45664f2008-11-10 02:56:27 +000091//===----------------------------------------------------------------------===//
Devang Patel6ceea332009-08-31 18:49:10 +000092// Predicates
Chris Lattnera45664f2008-11-10 02:56:27 +000093//===----------------------------------------------------------------------===//
94
Devang Patel6ceea332009-08-31 18:49:10 +000095/// isBasicType - Return true if the specified tag is legal for
96/// DIBasicType.
97bool DIDescriptor::isBasicType() const {
Devang Patel3c91b052010-03-08 20:52:55 +000098 return DbgNode && getTag() == dwarf::DW_TAG_base_type;
Torok Edwinb07fbd92008-12-13 08:25:29 +000099}
Chris Lattnera45664f2008-11-10 02:56:27 +0000100
Devang Patel6ceea332009-08-31 18:49:10 +0000101/// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
102bool DIDescriptor::isDerivedType() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000103 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000104 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000105 case dwarf::DW_TAG_typedef:
106 case dwarf::DW_TAG_pointer_type:
107 case dwarf::DW_TAG_reference_type:
108 case dwarf::DW_TAG_const_type:
109 case dwarf::DW_TAG_volatile_type:
110 case dwarf::DW_TAG_restrict_type:
111 case dwarf::DW_TAG_member:
112 case dwarf::DW_TAG_inheritance:
113 return true;
114 default:
Devang Patele4b27562009-08-28 23:24:31 +0000115 // CompositeTypes are currently modelled as DerivedTypes.
Devang Patel6ceea332009-08-31 18:49:10 +0000116 return isCompositeType();
Chris Lattnera45664f2008-11-10 02:56:27 +0000117 }
118}
119
Chris Lattnera45664f2008-11-10 02:56:27 +0000120/// isCompositeType - Return true if the specified tag is legal for
121/// DICompositeType.
Devang Patel6ceea332009-08-31 18:49:10 +0000122bool DIDescriptor::isCompositeType() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000123 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000124 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000125 case dwarf::DW_TAG_array_type:
126 case dwarf::DW_TAG_structure_type:
127 case dwarf::DW_TAG_union_type:
128 case dwarf::DW_TAG_enumeration_type:
129 case dwarf::DW_TAG_vector_type:
130 case dwarf::DW_TAG_subroutine_type:
Devang Patel25cb0d72009-03-25 03:52:06 +0000131 case dwarf::DW_TAG_class_type:
Chris Lattnera45664f2008-11-10 02:56:27 +0000132 return true;
133 default:
134 return false;
135 }
136}
137
Chris Lattnera45664f2008-11-10 02:56:27 +0000138/// isVariable - Return true if the specified tag is legal for DIVariable.
Devang Patel6ceea332009-08-31 18:49:10 +0000139bool DIDescriptor::isVariable() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000140 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000141 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000142 case dwarf::DW_TAG_auto_variable:
143 case dwarf::DW_TAG_arg_variable:
144 case dwarf::DW_TAG_return_variable:
145 return true;
146 default:
147 return false;
148 }
149}
150
Devang Patelecbeb1a2009-09-30 22:34:41 +0000151/// isType - Return true if the specified tag is legal for DIType.
152bool DIDescriptor::isType() const {
153 return isBasicType() || isCompositeType() || isDerivedType();
154}
155
Devang Patel6ceea332009-08-31 18:49:10 +0000156/// isSubprogram - Return true if the specified tag is legal for
157/// DISubprogram.
158bool DIDescriptor::isSubprogram() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000159 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
Devang Patel6ceea332009-08-31 18:49:10 +0000160}
161
162/// isGlobalVariable - Return true if the specified tag is legal for
163/// DIGlobalVariable.
164bool DIDescriptor::isGlobalVariable() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000165 return DbgNode && getTag() == dwarf::DW_TAG_variable;
Devang Patel6ceea332009-08-31 18:49:10 +0000166}
167
Devang Patelecbeb1a2009-09-30 22:34:41 +0000168/// isGlobal - Return true if the specified tag is legal for DIGlobal.
169bool DIDescriptor::isGlobal() const {
170 return isGlobalVariable();
171}
172
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000173/// isScope - Return true if the specified tag is one of the scope
Devang Patel43d98b32009-08-31 20:44:45 +0000174/// related tag.
175bool DIDescriptor::isScope() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000176 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000177 switch (getTag()) {
178 case dwarf::DW_TAG_compile_unit:
179 case dwarf::DW_TAG_lexical_block:
180 case dwarf::DW_TAG_subprogram:
181 case dwarf::DW_TAG_namespace:
182 return true;
183 default:
184 break;
Devang Patel43d98b32009-08-31 20:44:45 +0000185 }
186 return false;
187}
Devang Patel6ceea332009-08-31 18:49:10 +0000188
Devang Patelc9f322d2009-08-31 21:34:44 +0000189/// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
190bool DIDescriptor::isCompileUnit() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000191 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
Devang Patelc9f322d2009-08-31 21:34:44 +0000192}
193
Devang Patel7aa81892010-03-08 22:27:22 +0000194/// isFile - Return true if the specified tag is DW_TAG_file_type.
195bool DIDescriptor::isFile() const {
196 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
197}
198
Devang Patel6404e4e2009-12-15 19:16:48 +0000199/// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
200bool DIDescriptor::isNameSpace() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000201 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
Devang Patel6404e4e2009-12-15 19:16:48 +0000202}
203
Devang Patel5e005d82009-08-31 22:00:15 +0000204/// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
205bool DIDescriptor::isLexicalBlock() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000206 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block;
Devang Patel5e005d82009-08-31 22:00:15 +0000207}
208
Devang Patelecbeb1a2009-09-30 22:34:41 +0000209/// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
210bool DIDescriptor::isSubrange() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000211 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
Devang Patelecbeb1a2009-09-30 22:34:41 +0000212}
213
214/// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
215bool DIDescriptor::isEnumerator() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000216 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
Devang Patelecbeb1a2009-09-30 22:34:41 +0000217}
218
Devang Patel6ceea332009-08-31 18:49:10 +0000219//===----------------------------------------------------------------------===//
220// Simple Descriptor Constructors and other Methods
221//===----------------------------------------------------------------------===//
222
Devang Patele9f8f5e2010-05-07 20:54:48 +0000223DIType::DIType(const MDNode *N) : DIScope(N) {
Devang Patel6ceea332009-08-31 18:49:10 +0000224 if (!N) return;
225 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
226 DbgNode = 0;
227 }
228}
229
Devang Patel68afdc32009-01-05 18:33:01 +0000230unsigned DIArray::getNumElements() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000231 if (!DbgNode)
232 return 0;
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000233 return DbgNode->getNumOperands();
Devang Patel68afdc32009-01-05 18:33:01 +0000234}
Chris Lattnera45664f2008-11-10 02:56:27 +0000235
Devang Patelc4999d72009-07-22 18:23:44 +0000236/// replaceAllUsesWith - Replace all uses of debug info referenced by
Dan Gohman872814a2010-07-21 18:54:18 +0000237/// this descriptor.
Devang Patelc4999d72009-07-22 18:23:44 +0000238void DIDerivedType::replaceAllUsesWith(DIDescriptor &D) {
Devang Patel3c91b052010-03-08 20:52:55 +0000239 if (!DbgNode)
Devang Patelc4999d72009-07-22 18:23:44 +0000240 return;
241
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000242 // Since we use a TrackingVH for the node, its easy for clients to manufacture
243 // legitimate situations where they want to replaceAllUsesWith() on something
244 // which, due to uniquing, has merged with the source. We shield clients from
245 // this detail by allowing a value to be replaced with replaceAllUsesWith()
246 // itself.
Devang Pateled66bf52010-05-07 18:19:32 +0000247 if (DbgNode != D) {
Devang Patele9f8f5e2010-05-07 20:54:48 +0000248 MDNode *Node = const_cast<MDNode*>(DbgNode);
249 const MDNode *DN = D;
250 const Value *V = cast_or_null<Value>(DN);
251 Node->replaceAllUsesWith(const_cast<Value*>(V));
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000252 }
Devang Patelc4999d72009-07-22 18:23:44 +0000253}
254
Devang Patelb79b5352009-01-19 23:21:49 +0000255/// Verify - Verify that a compile unit is well formed.
256bool DICompileUnit::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000257 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000258 return false;
Devang Patel65dbc902009-11-25 17:36:49 +0000259 StringRef N = getFilename();
260 if (N.empty())
Bill Wendling0582ae92009-03-13 04:39:26 +0000261 return false;
Devang Patelb79b5352009-01-19 23:21:49 +0000262 // It is possible that directory and produce string is empty.
Bill Wendling0582ae92009-03-13 04:39:26 +0000263 return true;
Devang Patelb79b5352009-01-19 23:21:49 +0000264}
265
266/// Verify - Verify that a type descriptor is well formed.
267bool DIType::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000268 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000269 return false;
Devang Patel3c91b052010-03-08 20:52:55 +0000270 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000271 return false;
272
273 DICompileUnit CU = getCompileUnit();
Devang Patel3c91b052010-03-08 20:52:55 +0000274 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000275 return false;
276 return true;
277}
278
279/// Verify - Verify that a composite type descriptor is well formed.
280bool DICompositeType::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000281 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000282 return false;
Devang Patel3c91b052010-03-08 20:52:55 +0000283 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000284 return false;
285
286 DICompileUnit CU = getCompileUnit();
Devang Patel3c91b052010-03-08 20:52:55 +0000287 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000288 return false;
289 return true;
290}
291
292/// Verify - Verify that a subprogram descriptor is well formed.
293bool DISubprogram::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000294 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000295 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000296
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();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000301 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000302 return false;
303
304 DICompositeType Ty = getType();
Devang Patel3c91b052010-03-08 20:52:55 +0000305 if (!Ty.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000306 return false;
307 return true;
308}
309
310/// Verify - Verify that a global variable descriptor is well formed.
311bool DIGlobalVariable::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000312 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000313 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000314
Devang Patel65dbc902009-11-25 17:36:49 +0000315 if (getDisplayName().empty())
Devang Patel84c73e92009-11-06 17:58:12 +0000316 return false;
317
Devang Patel3c91b052010-03-08 20:52:55 +0000318 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000319 return false;
320
321 DICompileUnit CU = getCompileUnit();
Devang Patel3c91b052010-03-08 20:52:55 +0000322 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000323 return false;
324
325 DIType Ty = getType();
326 if (!Ty.Verify())
327 return false;
328
329 if (!getGlobal())
330 return false;
331
332 return true;
333}
334
335/// Verify - Verify that a variable descriptor is well formed.
336bool DIVariable::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000337 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000338 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000339
Devang Patel3c91b052010-03-08 20:52:55 +0000340 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000341 return false;
342
Devang Patel62077af2010-05-07 21:42:24 +0000343 if (!getCompileUnit().Verify())
344 return false;
345
Devang Patelb79b5352009-01-19 23:21:49 +0000346 DIType Ty = getType();
347 if (!Ty.Verify())
348 return false;
349
Devang Patelb79b5352009-01-19 23:21:49 +0000350 return true;
351}
352
Devang Patel3c91b052010-03-08 20:52:55 +0000353/// Verify - Verify that a location descriptor is well formed.
354bool DILocation::Verify() const {
355 if (!DbgNode)
356 return false;
Jim Grosbache62b6902010-07-21 21:36:25 +0000357
Devang Patel3c91b052010-03-08 20:52:55 +0000358 return DbgNode->getNumOperands() == 4;
359}
360
Devang Patel47e22652010-05-07 23:04:32 +0000361/// Verify - Verify that a namespace descriptor is well formed.
362bool DINameSpace::Verify() const {
363 if (!DbgNode)
364 return false;
365 if (getName().empty())
366 return false;
367 if (!getCompileUnit().Verify())
368 return false;
369 return true;
370}
371
Devang Patel36375ee2009-02-17 21:23:59 +0000372/// getOriginalTypeSize - If this type is derived from a base type then
373/// return base type size.
374uint64_t DIDerivedType::getOriginalTypeSize() const {
Devang Patel61ecbd12009-11-04 23:48:00 +0000375 unsigned Tag = getTag();
376 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
377 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
378 Tag == dwarf::DW_TAG_restrict_type) {
379 DIType BaseType = getTypeDerivedFrom();
Jim Grosbache62b6902010-07-21 21:36:25 +0000380 // If this type is not derived from any type then take conservative
Devang Patel5ebfa2d2009-11-06 18:24:05 +0000381 // approach.
Devang Patel3c91b052010-03-08 20:52:55 +0000382 if (!BaseType.isValid())
Devang Patel5ebfa2d2009-11-06 18:24:05 +0000383 return getSizeInBits();
Devang Patel61ecbd12009-11-04 23:48:00 +0000384 if (BaseType.isDerivedType())
Devang Patel2db49d72010-05-07 18:11:54 +0000385 return DIDerivedType(BaseType).getOriginalTypeSize();
Devang Patel61ecbd12009-11-04 23:48:00 +0000386 else
387 return BaseType.getSizeInBits();
388 }
Jim Grosbache62b6902010-07-21 21:36:25 +0000389
Devang Patel61ecbd12009-11-04 23:48:00 +0000390 return getSizeInBits();
Devang Patel36375ee2009-02-17 21:23:59 +0000391}
Devang Patelb79b5352009-01-19 23:21:49 +0000392
Jim Grosbache62b6902010-07-21 21:36:25 +0000393/// isInlinedFnArgument - Return true if this variable provides debugging
Devang Patel719f6a92010-04-29 20:40:36 +0000394/// information for an inlined function arguments.
395bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
396 assert(CurFn && "Invalid function");
397 if (!getContext().isSubprogram())
398 return false;
Jim Grosbache62b6902010-07-21 21:36:25 +0000399 // This variable is not inlined function argument if its scope
Devang Patel719f6a92010-04-29 20:40:36 +0000400 // does not describe current function.
Devang Patel2db49d72010-05-07 18:11:54 +0000401 return !(DISubprogram(getContext()).describes(CurFn));
Devang Patel719f6a92010-04-29 20:40:36 +0000402}
403
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000404/// describes - Return true if this subprogram provides debugging
405/// information for the function F.
406bool DISubprogram::describes(const Function *F) {
Chris Lattner099b7792009-12-29 09:22:47 +0000407 assert(F && "Invalid function");
Devang Patelffd33cd2010-06-16 06:42:02 +0000408 if (F == getFunction())
409 return true;
Devang Patel65dbc902009-11-25 17:36:49 +0000410 StringRef Name = getLinkageName();
411 if (Name.empty())
Devang Patel5ccdd102009-09-29 18:40:58 +0000412 Name = getName();
Devang Patel65dbc902009-11-25 17:36:49 +0000413 if (F->getName() == Name)
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000414 return true;
415 return false;
416}
417
Jim Grosbache62b6902010-07-21 21:36:25 +0000418unsigned DISubprogram::isOptimized() const {
Devang Patelccff8122010-04-30 19:38:23 +0000419 assert (DbgNode && "Invalid subprogram descriptor!");
420 if (DbgNode->getNumOperands() == 16)
421 return getUnsignedField(15);
422 return 0;
423}
424
Devang Patel65dbc902009-11-25 17:36:49 +0000425StringRef DIScope::getFilename() const {
Devang Patel77bf2952010-03-08 22:02:50 +0000426 if (!DbgNode)
427 return StringRef();
Jim Grosbache62b6902010-07-21 21:36:25 +0000428 if (isLexicalBlock())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000429 return DILexicalBlock(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000430 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000431 return DISubprogram(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000432 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000433 return DICompileUnit(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000434 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000435 return DINameSpace(DbgNode).getFilename();
Devang Patel77bf2952010-03-08 22:02:50 +0000436 if (isType())
437 return DIType(DbgNode).getFilename();
Devang Patel7aa81892010-03-08 22:27:22 +0000438 if (isFile())
439 return DIFile(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000440 assert(0 && "Invalid DIScope!");
Devang Patel65dbc902009-11-25 17:36:49 +0000441 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000442}
443
Devang Patel65dbc902009-11-25 17:36:49 +0000444StringRef DIScope::getDirectory() const {
Devang Patel77bf2952010-03-08 22:02:50 +0000445 if (!DbgNode)
446 return StringRef();
Jim Grosbache62b6902010-07-21 21:36:25 +0000447 if (isLexicalBlock())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000448 return DILexicalBlock(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000449 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000450 return DISubprogram(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000451 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000452 return DICompileUnit(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000453 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000454 return DINameSpace(DbgNode).getDirectory();
Devang Patel77bf2952010-03-08 22:02:50 +0000455 if (isType())
456 return DIType(DbgNode).getDirectory();
Devang Patel7aa81892010-03-08 22:27:22 +0000457 if (isFile())
458 return DIFile(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000459 assert(0 && "Invalid DIScope!");
Devang Patel65dbc902009-11-25 17:36:49 +0000460 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000461}
462
Chris Lattnera45664f2008-11-10 02:56:27 +0000463//===----------------------------------------------------------------------===//
Devang Patel7136a652009-07-01 22:10:23 +0000464// DIDescriptor: dump routines for all descriptors.
465//===----------------------------------------------------------------------===//
466
467
Dan Gohman50404362010-05-07 15:30:29 +0000468/// print - Print descriptor.
469void DIDescriptor::print(raw_ostream &OS) const {
470 OS << "[" << dwarf::TagString(getTag()) << "] ";
471 OS.write_hex((intptr_t) &*DbgNode) << ']';
Devang Patel7136a652009-07-01 22:10:23 +0000472}
473
Dan Gohman50404362010-05-07 15:30:29 +0000474/// print - Print compile unit.
475void DICompileUnit::print(raw_ostream &OS) const {
Devang Patel7136a652009-07-01 22:10:23 +0000476 if (getLanguage())
Dan Gohman50404362010-05-07 15:30:29 +0000477 OS << " [" << dwarf::LanguageString(getLanguage()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000478
Dan Gohmanfaa19c32010-05-10 20:07:44 +0000479 OS << " [" << getDirectory() << "/" << getFilename() << "]";
Devang Patel7136a652009-07-01 22:10:23 +0000480}
481
Dan Gohman50404362010-05-07 15:30:29 +0000482/// print - Print type.
483void DIType::print(raw_ostream &OS) const {
Devang Patel3c91b052010-03-08 20:52:55 +0000484 if (!DbgNode) return;
Devang Patel7136a652009-07-01 22:10:23 +0000485
Devang Patel65dbc902009-11-25 17:36:49 +0000486 StringRef Res = getName();
487 if (!Res.empty())
Dan Gohman50404362010-05-07 15:30:29 +0000488 OS << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000489
490 unsigned Tag = getTag();
Dan Gohman50404362010-05-07 15:30:29 +0000491 OS << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000492
493 // TODO : Print context
Dan Gohmanc014d092010-05-07 16:17:22 +0000494 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000495 OS << " ["
Dan Gohman9a7063e2010-05-07 16:39:27 +0000496 << "line " << getLineNumber() << ", "
497 << getSizeInBits() << " bits, "
498 << getAlignInBits() << " bit alignment, "
499 << getOffsetInBits() << " bit offset"
Chris Lattnera81d29b2009-08-23 07:33:14 +0000500 << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000501
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000502 if (isPrivate())
Dan Gohman50404362010-05-07 15:30:29 +0000503 OS << " [private] ";
Devang Patel7136a652009-07-01 22:10:23 +0000504 else if (isProtected())
Dan Gohman50404362010-05-07 15:30:29 +0000505 OS << " [protected] ";
Devang Patel7136a652009-07-01 22:10:23 +0000506
507 if (isForwardDecl())
Dan Gohman50404362010-05-07 15:30:29 +0000508 OS << " [fwd] ";
Devang Patel7136a652009-07-01 22:10:23 +0000509
Devang Patel6ceea332009-08-31 18:49:10 +0000510 if (isBasicType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000511 DIBasicType(DbgNode).print(OS);
Devang Patel6ceea332009-08-31 18:49:10 +0000512 else if (isDerivedType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000513 DIDerivedType(DbgNode).print(OS);
Devang Patel6ceea332009-08-31 18:49:10 +0000514 else if (isCompositeType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000515 DICompositeType(DbgNode).print(OS);
Devang Patel7136a652009-07-01 22:10:23 +0000516 else {
Dan Gohman50404362010-05-07 15:30:29 +0000517 OS << "Invalid DIType\n";
Devang Patel7136a652009-07-01 22:10:23 +0000518 return;
519 }
520
Dan Gohman50404362010-05-07 15:30:29 +0000521 OS << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000522}
523
Dan Gohman50404362010-05-07 15:30:29 +0000524/// print - Print basic type.
525void DIBasicType::print(raw_ostream &OS) const {
526 OS << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000527}
528
Dan Gohman50404362010-05-07 15:30:29 +0000529/// print - Print derived type.
530void DIDerivedType::print(raw_ostream &OS) const {
Dan Gohmanc014d092010-05-07 16:17:22 +0000531 OS << "\n\t Derived From: "; getTypeDerivedFrom().print(OS);
Devang Patel7136a652009-07-01 22:10:23 +0000532}
533
Dan Gohman50404362010-05-07 15:30:29 +0000534/// print - Print composite type.
535void DICompositeType::print(raw_ostream &OS) const {
Devang Patel7136a652009-07-01 22:10:23 +0000536 DIArray A = getTypeArray();
Dan Gohman50404362010-05-07 15:30:29 +0000537 OS << " [" << A.getNumElements() << " elements]";
Devang Patel7136a652009-07-01 22:10:23 +0000538}
539
Dan Gohman50404362010-05-07 15:30:29 +0000540/// print - Print subprogram.
541void DISubprogram::print(raw_ostream &OS) const {
Devang Patel65dbc902009-11-25 17:36:49 +0000542 StringRef Res = getName();
543 if (!Res.empty())
Dan Gohman50404362010-05-07 15:30:29 +0000544 OS << " [" << Res << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000545
546 unsigned Tag = getTag();
Dan Gohman50404362010-05-07 15:30:29 +0000547 OS << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000548
549 // TODO : Print context
Dan Gohmanc014d092010-05-07 16:17:22 +0000550 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000551 OS << " [" << getLineNumber() << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000552
553 if (isLocalToUnit())
Dan Gohman50404362010-05-07 15:30:29 +0000554 OS << " [local] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000555
556 if (isDefinition())
Dan Gohman50404362010-05-07 15:30:29 +0000557 OS << " [def] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000558
Dan Gohman50404362010-05-07 15:30:29 +0000559 OS << "\n";
560}
561
562/// print - Print global variable.
563void DIGlobalVariable::print(raw_ostream &OS) const {
564 OS << " [";
Devang Patela49d8772010-05-07 23:19:07 +0000565 StringRef Res = getName();
566 if (!Res.empty())
567 OS << " [" << Res << "] ";
568
569 unsigned Tag = getTag();
570 OS << " [" << dwarf::TagString(Tag) << "] ";
571
572 // TODO : Print context
573 getCompileUnit().print(OS);
574 OS << " [" << getLineNumber() << "] ";
575
576 if (isLocalToUnit())
577 OS << " [local] ";
578
579 if (isDefinition())
580 OS << " [def] ";
581
582 if (isGlobalVariable())
583 DIGlobalVariable(DbgNode).print(OS);
584 OS << "]\n";
Dan Gohman50404362010-05-07 15:30:29 +0000585}
586
587/// print - Print variable.
588void DIVariable::print(raw_ostream &OS) const {
589 StringRef Res = getName();
590 if (!Res.empty())
591 OS << " [" << Res << "] ";
592
Dan Gohmanc014d092010-05-07 16:17:22 +0000593 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000594 OS << " [" << getLineNumber() << "] ";
Dan Gohmanc014d092010-05-07 16:17:22 +0000595 getType().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000596 OS << "\n";
597
598 // FIXME: Dump complex addresses
599}
600
601/// dump - Print descriptor to dbgs() with a newline.
602void DIDescriptor::dump() const {
603 print(dbgs()); dbgs() << '\n';
604}
605
606/// dump - Print compile unit to dbgs() with a newline.
607void DICompileUnit::dump() const {
608 print(dbgs()); dbgs() << '\n';
609}
610
611/// dump - Print type to dbgs() with a newline.
612void DIType::dump() const {
613 print(dbgs()); dbgs() << '\n';
614}
615
616/// dump - Print basic type to dbgs() with a newline.
617void DIBasicType::dump() const {
618 print(dbgs()); dbgs() << '\n';
619}
620
621/// dump - Print derived type to dbgs() with a newline.
622void DIDerivedType::dump() const {
623 print(dbgs()); dbgs() << '\n';
624}
625
626/// dump - Print composite type to dbgs() with a newline.
627void DICompositeType::dump() const {
628 print(dbgs()); dbgs() << '\n';
629}
630
Dan Gohman50404362010-05-07 15:30:29 +0000631/// dump - Print subprogram to dbgs() with a newline.
632void DISubprogram::dump() const {
633 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000634}
635
636/// dump - Print global variable.
637void DIGlobalVariable::dump() const {
Dan Gohman50404362010-05-07 15:30:29 +0000638 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000639}
640
641/// dump - Print variable.
642void DIVariable::dump() const {
Dan Gohman50404362010-05-07 15:30:29 +0000643 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000644}
645
646//===----------------------------------------------------------------------===//
Chris Lattnera45664f2008-11-10 02:56:27 +0000647// DIFactory: Basic Helpers
648//===----------------------------------------------------------------------===//
649
Bill Wendlingdc817b62009-05-14 18:26:15 +0000650DIFactory::DIFactory(Module &m)
Victor Hernandez06203122010-01-23 00:03:28 +0000651 : M(m), VMContext(M.getContext()), DeclareFn(0), ValueFn(0) {}
Chris Lattner497a7a82008-11-10 04:10:34 +0000652
Chris Lattnera45664f2008-11-10 02:56:27 +0000653Constant *DIFactory::GetTagConstant(unsigned TAG) {
Devang Patel6906ba52009-01-20 19:22:03 +0000654 assert((TAG & LLVMDebugVersionMask) == 0 &&
Chris Lattnera45664f2008-11-10 02:56:27 +0000655 "Tag too large for debug encoding!");
Owen Anderson1d0be152009-08-13 21:58:54 +0000656 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
Chris Lattnera45664f2008-11-10 02:56:27 +0000657}
658
Chris Lattnera45664f2008-11-10 02:56:27 +0000659//===----------------------------------------------------------------------===//
660// DIFactory: Primary Constructors
661//===----------------------------------------------------------------------===//
662
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000663/// GetOrCreateArray - Create an descriptor for an array of descriptors.
Chris Lattnera45664f2008-11-10 02:56:27 +0000664/// This implicitly uniques the arrays created.
665DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
Devang Patele4b27562009-08-28 23:24:31 +0000666 SmallVector<Value*, 16> Elts;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000667
Devang Patele4b27562009-08-28 23:24:31 +0000668 if (NumTys == 0)
669 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)));
670 else
671 for (unsigned i = 0; i != NumTys; ++i)
Devang Patel2db49d72010-05-07 18:11:54 +0000672 Elts.push_back(Tys[i]);
Devang Patel82459882009-08-26 05:01:18 +0000673
Devang Patele4b27562009-08-28 23:24:31 +0000674 return DIArray(MDNode::get(VMContext,Elts.data(), Elts.size()));
Chris Lattnera45664f2008-11-10 02:56:27 +0000675}
676
677/// GetOrCreateSubrange - Create a descriptor for a value range. This
678/// implicitly uniques the values returned.
679DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
Devang Patele4b27562009-08-28 23:24:31 +0000680 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000681 GetTagConstant(dwarf::DW_TAG_subrange_type),
Owen Anderson1d0be152009-08-13 21:58:54 +0000682 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
683 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
Chris Lattnera45664f2008-11-10 02:56:27 +0000684 };
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000685
Devang Patele4b27562009-08-28 23:24:31 +0000686 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000687}
688
689
690
691/// CreateCompileUnit - Create a new descriptor for the specified compile
692/// unit. Note that this does not unique compile units within the module.
693DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
Devang Patel65dbc902009-11-25 17:36:49 +0000694 StringRef Filename,
695 StringRef Directory,
696 StringRef Producer,
Devang Pateldd9db662009-01-30 18:20:31 +0000697 bool isMain,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000698 bool isOptimized,
Devang Patel65dbc902009-11-25 17:36:49 +0000699 StringRef Flags,
Devang Patel13319ce2009-02-17 22:43:44 +0000700 unsigned RunTimeVer) {
Devang Patele4b27562009-08-28 23:24:31 +0000701 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000702 GetTagConstant(dwarf::DW_TAG_compile_unit),
Devang Patele4b27562009-08-28 23:24:31 +0000703 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Owen Anderson1d0be152009-08-13 21:58:54 +0000704 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
Devang Patele4b27562009-08-28 23:24:31 +0000705 MDString::get(VMContext, Filename),
706 MDString::get(VMContext, Directory),
707 MDString::get(VMContext, Producer),
Owen Anderson1d0be152009-08-13 21:58:54 +0000708 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
709 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
Devang Patele4b27562009-08-28 23:24:31 +0000710 MDString::get(VMContext, Flags),
Owen Anderson1d0be152009-08-13 21:58:54 +0000711 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
Chris Lattnera45664f2008-11-10 02:56:27 +0000712 };
Devang Patele4b27562009-08-28 23:24:31 +0000713
714 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000715}
716
Devang Patel7aa81892010-03-08 22:27:22 +0000717/// CreateFile - Create a new descriptor for the specified file.
718DIFile DIFactory::CreateFile(StringRef Filename,
719 StringRef Directory,
720 DICompileUnit CU) {
721 Value *Elts[] = {
722 GetTagConstant(dwarf::DW_TAG_file_type),
723 MDString::get(VMContext, Filename),
724 MDString::get(VMContext, Directory),
Devang Patel2db49d72010-05-07 18:11:54 +0000725 CU
Devang Patel7aa81892010-03-08 22:27:22 +0000726 };
727
728 return DIFile(MDNode::get(VMContext, &Elts[0], 4));
729}
730
Chris Lattnera45664f2008-11-10 02:56:27 +0000731/// CreateEnumerator - Create a single enumerator value.
Devang Patel65dbc902009-11-25 17:36:49 +0000732DIEnumerator DIFactory::CreateEnumerator(StringRef Name, uint64_t Val){
Devang Patele4b27562009-08-28 23:24:31 +0000733 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000734 GetTagConstant(dwarf::DW_TAG_enumerator),
Devang Patele4b27562009-08-28 23:24:31 +0000735 MDString::get(VMContext, Name),
Owen Anderson1d0be152009-08-13 21:58:54 +0000736 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
Chris Lattnera45664f2008-11-10 02:56:27 +0000737 };
Devang Patele4b27562009-08-28 23:24:31 +0000738 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000739}
740
741
742/// CreateBasicType - Create a basic type like int, float, etc.
743DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000744 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000745 DIFile F,
Chris Lattnera45664f2008-11-10 02:56:27 +0000746 unsigned LineNumber,
747 uint64_t SizeInBits,
748 uint64_t AlignInBits,
749 uint64_t OffsetInBits, unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000750 unsigned Encoding) {
Devang Patele4b27562009-08-28 23:24:31 +0000751 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000752 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patel2db49d72010-05-07 18:11:54 +0000753 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000754 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000755 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000756 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
757 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
758 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
759 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
760 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
761 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
Chris Lattnera45664f2008-11-10 02:56:27 +0000762 };
Devang Patele4b27562009-08-28 23:24:31 +0000763 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000764}
765
Devang Patelac16d442009-10-26 16:54:35 +0000766
767/// CreateBasicType - Create a basic type like int, float, etc.
768DIBasicType DIFactory::CreateBasicTypeEx(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000769 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000770 DIFile F,
Devang Patelac16d442009-10-26 16:54:35 +0000771 unsigned LineNumber,
772 Constant *SizeInBits,
773 Constant *AlignInBits,
774 Constant *OffsetInBits, unsigned Flags,
775 unsigned Encoding) {
776 Value *Elts[] = {
777 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patel2db49d72010-05-07 18:11:54 +0000778 Context,
Devang Patelac16d442009-10-26 16:54:35 +0000779 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000780 F,
Devang Patelac16d442009-10-26 16:54:35 +0000781 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
782 SizeInBits,
783 AlignInBits,
784 OffsetInBits,
785 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
786 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
787 };
788 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
789}
790
Devang Patelb4645642010-02-06 01:02:37 +0000791/// CreateArtificialType - Create a new DIType with "artificial" flag set.
792DIType DIFactory::CreateArtificialType(DIType Ty) {
793 if (Ty.isArtificial())
794 return Ty;
795
796 SmallVector<Value *, 9> Elts;
Devang Patel2db49d72010-05-07 18:11:54 +0000797 MDNode *N = Ty;
Devang Patelb4645642010-02-06 01:02:37 +0000798 assert (N && "Unexpected input DIType!");
799 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
800 if (Value *V = N->getOperand(i))
801 Elts.push_back(V);
802 else
803 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
804 }
805
806 unsigned CurFlags = Ty.getFlags();
807 CurFlags = CurFlags | DIType::FlagArtificial;
808
809 // Flags are stored at this slot.
810 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
811
812 return DIType(MDNode::get(VMContext, Elts.data(), Elts.size()));
813}
Devang Patelac16d442009-10-26 16:54:35 +0000814
Chris Lattnera45664f2008-11-10 02:56:27 +0000815/// CreateDerivedType - Create a derived type like const qualified type,
816/// pointer, typedef, etc.
817DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
818 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000819 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000820 DIFile F,
Chris Lattnera45664f2008-11-10 02:56:27 +0000821 unsigned LineNumber,
822 uint64_t SizeInBits,
823 uint64_t AlignInBits,
824 uint64_t OffsetInBits,
825 unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000826 DIType DerivedFrom) {
Devang Patele4b27562009-08-28 23:24:31 +0000827 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000828 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000829 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000830 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000831 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000832 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
833 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
834 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
835 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
836 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000837 DerivedFrom,
Chris Lattnera45664f2008-11-10 02:56:27 +0000838 };
Devang Patele4b27562009-08-28 23:24:31 +0000839 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000840}
841
Devang Patelac16d442009-10-26 16:54:35 +0000842
843/// CreateDerivedType - Create a derived type like const qualified type,
844/// pointer, typedef, etc.
845DIDerivedType DIFactory::CreateDerivedTypeEx(unsigned Tag,
846 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000847 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000848 DIFile F,
Devang Patelac16d442009-10-26 16:54:35 +0000849 unsigned LineNumber,
850 Constant *SizeInBits,
851 Constant *AlignInBits,
852 Constant *OffsetInBits,
853 unsigned Flags,
854 DIType DerivedFrom) {
855 Value *Elts[] = {
856 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000857 Context,
Devang Patelac16d442009-10-26 16:54:35 +0000858 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000859 F,
Devang Patelac16d442009-10-26 16:54:35 +0000860 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
861 SizeInBits,
862 AlignInBits,
863 OffsetInBits,
864 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000865 DerivedFrom,
Devang Patelac16d442009-10-26 16:54:35 +0000866 };
867 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
868}
869
870
Chris Lattnera45664f2008-11-10 02:56:27 +0000871/// CreateCompositeType - Create a composite type like array, struct, etc.
872DICompositeType DIFactory::CreateCompositeType(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,
Chris Lattnera45664f2008-11-10 02:56:27 +0000876 unsigned LineNumber,
877 uint64_t SizeInBits,
878 uint64_t AlignInBits,
879 uint64_t OffsetInBits,
880 unsigned Flags,
881 DIType DerivedFrom,
Devang Patel13319ce2009-02-17 22:43:44 +0000882 DIArray Elements,
Devang Patel0fd7f9d2010-01-26 21:14:59 +0000883 unsigned RuntimeLang,
884 MDNode *ContainingType) {
Owen Andersone277fed2009-07-07 16:31:25 +0000885
Devang Patele4b27562009-08-28 23:24:31 +0000886 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000887 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000888 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000889 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000890 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000891 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
892 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
893 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
894 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
895 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000896 DerivedFrom,
897 Elements,
Devang Patel0fd7f9d2010-01-26 21:14:59 +0000898 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
899 ContainingType
Chris Lattnera45664f2008-11-10 02:56:27 +0000900 };
Devang Patel0fd7f9d2010-01-26 21:14:59 +0000901 return DICompositeType(MDNode::get(VMContext, &Elts[0], 13));
Chris Lattnera45664f2008-11-10 02:56:27 +0000902}
903
904
Devang Patelac16d442009-10-26 16:54:35 +0000905/// CreateCompositeType - Create a composite type like array, struct, etc.
906DICompositeType DIFactory::CreateCompositeTypeEx(unsigned Tag,
907 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000908 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000909 DIFile F,
Devang Patelac16d442009-10-26 16:54:35 +0000910 unsigned LineNumber,
911 Constant *SizeInBits,
912 Constant *AlignInBits,
913 Constant *OffsetInBits,
914 unsigned Flags,
915 DIType DerivedFrom,
916 DIArray Elements,
917 unsigned RuntimeLang) {
918
919 Value *Elts[] = {
920 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000921 Context,
Devang Patelac16d442009-10-26 16:54:35 +0000922 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000923 F,
Devang Patelac16d442009-10-26 16:54:35 +0000924 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
925 SizeInBits,
926 AlignInBits,
927 OffsetInBits,
928 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000929 DerivedFrom,
930 Elements,
Devang Patelac16d442009-10-26 16:54:35 +0000931 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
932 };
933 return DICompositeType(MDNode::get(VMContext, &Elts[0], 12));
934}
935
936
Chris Lattnera45664f2008-11-10 02:56:27 +0000937/// CreateSubprogram - Create a new descriptor for the specified subprogram.
938/// See comments in DISubprogram for descriptions of these fields. This
939/// method does not unique the generated descriptors.
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000940DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000941 StringRef Name,
942 StringRef DisplayName,
943 StringRef LinkageName,
Devang Patel4b945502010-03-09 00:44:10 +0000944 DIFile F,
Devang Patel2e369932010-01-23 00:26:28 +0000945 unsigned LineNo, DIType Ty,
Chris Lattnera45664f2008-11-10 02:56:27 +0000946 bool isLocalToUnit,
Devang Patel5d11eb02009-12-03 19:11:07 +0000947 bool isDefinition,
948 unsigned VK, unsigned VIndex,
Devang Patel4e0d19d2010-02-03 19:57:19 +0000949 DIType ContainingType,
Devang Patelccff8122010-04-30 19:38:23 +0000950 bool isArtificial,
Stuart Hastings215aa152010-06-11 20:08:44 +0000951 bool isOptimized,
952 Function *Fn) {
Devang Patel854967e2008-12-17 22:39:29 +0000953
Devang Patele4b27562009-08-28 23:24:31 +0000954 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000955 GetTagConstant(dwarf::DW_TAG_subprogram),
Devang Patele4b27562009-08-28 23:24:31 +0000956 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Devang Patel2db49d72010-05-07 18:11:54 +0000957 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000958 MDString::get(VMContext, Name),
959 MDString::get(VMContext, DisplayName),
960 MDString::get(VMContext, LinkageName),
Devang Patel2db49d72010-05-07 18:11:54 +0000961 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000962 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2db49d72010-05-07 18:11:54 +0000963 Ty,
Owen Anderson1d0be152009-08-13 21:58:54 +0000964 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
Devang Patel5d11eb02009-12-03 19:11:07 +0000965 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
966 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
967 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
Devang Patel2db49d72010-05-07 18:11:54 +0000968 ContainingType,
Devang Patelccff8122010-04-30 19:38:23 +0000969 ConstantInt::get(Type::getInt1Ty(VMContext), isArtificial),
Stuart Hastings215aa152010-06-11 20:08:44 +0000970 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
971 Fn
Chris Lattnera45664f2008-11-10 02:56:27 +0000972 };
Devang Patelfd5fdc32010-06-28 05:53:08 +0000973 MDNode *Node = MDNode::get(VMContext, &Elts[0], 17);
974
975 // Create a named metadata so that we do not lose this mdnode.
976 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
977 NMD->addOperand(Node);
978 return DISubprogram(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +0000979}
980
Devang Patele3a18de2009-12-01 23:09:02 +0000981/// CreateSubprogramDefinition - Create new subprogram descriptor for the
Jim Grosbache62b6902010-07-21 21:36:25 +0000982/// given declaration.
983DISubprogram DIFactory::CreateSubprogramDefinition(DISubprogram &SPDeclaration){
Devang Patele3a18de2009-12-01 23:09:02 +0000984 if (SPDeclaration.isDefinition())
Devang Patel2db49d72010-05-07 18:11:54 +0000985 return DISubprogram(SPDeclaration);
Devang Patele3a18de2009-12-01 23:09:02 +0000986
Devang Patel2db49d72010-05-07 18:11:54 +0000987 MDNode *DeclNode = SPDeclaration;
Devang Patele3a18de2009-12-01 23:09:02 +0000988 Value *Elts[] = {
989 GetTagConstant(dwarf::DW_TAG_subprogram),
990 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000991 DeclNode->getOperand(2), // Context
992 DeclNode->getOperand(3), // Name
993 DeclNode->getOperand(4), // DisplayName
994 DeclNode->getOperand(5), // LinkageName
995 DeclNode->getOperand(6), // CompileUnit
996 DeclNode->getOperand(7), // LineNo
997 DeclNode->getOperand(8), // Type
998 DeclNode->getOperand(9), // isLocalToUnit
Stuart Hastings6d56b9f2010-06-05 00:39:29 +0000999 ConstantInt::get(Type::getInt1Ty(VMContext), true),
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001000 DeclNode->getOperand(11), // Virtuality
1001 DeclNode->getOperand(12), // VIndex
Devang Patel4e0d19d2010-02-03 19:57:19 +00001002 DeclNode->getOperand(13), // Containting Type
Devang Patelccff8122010-04-30 19:38:23 +00001003 DeclNode->getOperand(14), // isArtificial
Devang Patelacc6efa2010-06-27 21:04:31 +00001004 DeclNode->getOperand(15), // isOptimized
1005 SPDeclaration.getFunction()
Devang Patele3a18de2009-12-01 23:09:02 +00001006 };
Devang Patelfd5fdc32010-06-28 05:53:08 +00001007 MDNode *Node =MDNode::get(VMContext, &Elts[0], 16);
1008
1009 // Create a named metadata so that we do not lose this mdnode.
1010 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
1011 NMD->addOperand(Node);
1012 return DISubprogram(Node);
Devang Patele3a18de2009-12-01 23:09:02 +00001013}
1014
Chris Lattnera45664f2008-11-10 02:56:27 +00001015/// CreateGlobalVariable - Create a new descriptor for the specified global.
1016DIGlobalVariable
Devang Patel65dbc902009-11-25 17:36:49 +00001017DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
1018 StringRef DisplayName,
1019 StringRef LinkageName,
Devang Patel4b945502010-03-09 00:44:10 +00001020 DIFile F,
Devang Patel2e369932010-01-23 00:26:28 +00001021 unsigned LineNo, DIType Ty,bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +00001022 bool isDefinition, llvm::GlobalVariable *Val) {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001023 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001024 GetTagConstant(dwarf::DW_TAG_variable),
Devang Patele4b27562009-08-28 23:24:31 +00001025 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Devang Patel2db49d72010-05-07 18:11:54 +00001026 Context,
Devang Patele4b27562009-08-28 23:24:31 +00001027 MDString::get(VMContext, Name),
1028 MDString::get(VMContext, DisplayName),
1029 MDString::get(VMContext, LinkageName),
Devang Patel2db49d72010-05-07 18:11:54 +00001030 F,
Owen Anderson1d0be152009-08-13 21:58:54 +00001031 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001032 Ty,
Owen Anderson1d0be152009-08-13 21:58:54 +00001033 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1034 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
Devang Patele4b27562009-08-28 23:24:31 +00001035 Val
Chris Lattnera45664f2008-11-10 02:56:27 +00001036 };
Devang Patele4b27562009-08-28 23:24:31 +00001037
1038 Value *const *Vs = &Elts[0];
1039 MDNode *Node = MDNode::get(VMContext,Vs, 12);
1040
1041 // Create a named metadata so that we do not lose this mdnode.
1042 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001043 NMD->addOperand(Node);
Devang Patele4b27562009-08-28 23:24:31 +00001044
1045 return DIGlobalVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +00001046}
1047
1048
1049/// CreateVariable - Create a new descriptor for the specified variable.
1050DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +00001051 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +00001052 DIFile F,
1053 unsigned LineNo,
Devang Patel6ed0ce32010-05-20 20:35:24 +00001054 DIType Ty, bool AlwaysPreserve) {
Devang Patele4b27562009-08-28 23:24:31 +00001055 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001056 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +00001057 Context,
Devang Patele4b27562009-08-28 23:24:31 +00001058 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +00001059 F,
Owen Anderson1d0be152009-08-13 21:58:54 +00001060 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001061 Ty,
Chris Lattnera45664f2008-11-10 02:56:27 +00001062 };
Devang Patel98e1cac2010-05-14 21:01:35 +00001063 MDNode *Node = MDNode::get(VMContext, &Elts[0], 6);
Devang Patel6ed0ce32010-05-20 20:35:24 +00001064 if (AlwaysPreserve) {
1065 // The optimizer may remove local variable. If there is an interest
1066 // to preserve variable info in such situation then stash it in a
1067 // named mdnode.
Devang Patel2f7d5292010-06-16 00:53:55 +00001068 DISubprogram Fn(getDISubprogram(Context));
Devang Patel10de3bb2010-06-21 18:36:58 +00001069 StringRef FName = "fn";
1070 if (Fn.getFunction())
1071 FName = Fn.getFunction()->getName();
Devang Patel10de3bb2010-06-21 18:36:58 +00001072 char One = '\1';
1073 if (FName.startswith(StringRef(&One, 1)))
1074 FName = FName.substr(1);
Dan Gohman17aa92c2010-07-21 23:38:33 +00001075
1076 SmallString<32> Out;
1077 NamedMDNode *FnLocals =
1078 M.getOrInsertNamedMetadata(Twine("llvm.dbg.lv.", FName).toStringRef(Out));
Devang Patel2f7d5292010-06-16 00:53:55 +00001079 FnLocals->addOperand(Node);
Devang Patel98e1cac2010-05-14 21:01:35 +00001080 }
1081 return DIVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +00001082}
1083
1084
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001085/// CreateComplexVariable - Create a new descriptor for the specified variable
1086/// which has a complex address expression for its address.
1087DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
1088 const std::string &Name,
Devang Patel4b945502010-03-09 00:44:10 +00001089 DIFile F,
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001090 unsigned LineNo,
Jim Grosbache62b6902010-07-21 21:36:25 +00001091 DIType Ty,
Devang Patel2e369932010-01-23 00:26:28 +00001092 SmallVector<Value *, 9> &addr) {
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001093 SmallVector<Value *, 9> Elts;
1094 Elts.push_back(GetTagConstant(Tag));
Devang Patel2db49d72010-05-07 18:11:54 +00001095 Elts.push_back(Context);
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001096 Elts.push_back(MDString::get(VMContext, Name));
Devang Patel2db49d72010-05-07 18:11:54 +00001097 Elts.push_back(F);
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001098 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
Devang Patel2db49d72010-05-07 18:11:54 +00001099 Elts.push_back(Ty);
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001100 Elts.insert(Elts.end(), addr.begin(), addr.end());
1101
1102 return DIVariable(MDNode::get(VMContext, &Elts[0], 6+addr.size()));
1103}
1104
1105
Chris Lattnera45664f2008-11-10 02:56:27 +00001106/// CreateBlock - This creates a descriptor for a lexical block with the
Owen Anderson99035272009-07-07 17:12:53 +00001107/// specified parent VMContext.
Devang Patel3d821aa2010-02-16 21:39:34 +00001108DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context,
Stuart Hastings0db42712010-07-19 23:56:30 +00001109 DIFile F, unsigned LineNo,
1110 unsigned Col) {
1111 // Defeat MDNode uniqing for lexical blocks.
1112 static unsigned int unique_id = 0;
Devang Patele4b27562009-08-28 23:24:31 +00001113 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001114 GetTagConstant(dwarf::DW_TAG_lexical_block),
Devang Patel2db49d72010-05-07 18:11:54 +00001115 Context,
Devang Patel3d821aa2010-02-16 21:39:34 +00001116 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Stuart Hastings0db42712010-07-19 23:56:30 +00001117 ConstantInt::get(Type::getInt32Ty(VMContext), Col),
1118 F,
1119 ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
Chris Lattnera45664f2008-11-10 02:56:27 +00001120 };
Stuart Hastings0db42712010-07-19 23:56:30 +00001121 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 6));
Chris Lattnera45664f2008-11-10 02:56:27 +00001122}
1123
Devang Patel6404e4e2009-12-15 19:16:48 +00001124/// CreateNameSpace - This creates new descriptor for a namespace
1125/// with the specified parent context.
1126DINameSpace DIFactory::CreateNameSpace(DIDescriptor Context, StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +00001127 DIFile F,
Devang Patel6404e4e2009-12-15 19:16:48 +00001128 unsigned LineNo) {
1129 Value *Elts[] = {
1130 GetTagConstant(dwarf::DW_TAG_namespace),
Devang Patel2db49d72010-05-07 18:11:54 +00001131 Context,
Devang Patel6404e4e2009-12-15 19:16:48 +00001132 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +00001133 F,
Devang Patel6404e4e2009-12-15 19:16:48 +00001134 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1135 };
1136 return DINameSpace(MDNode::get(VMContext, &Elts[0], 5));
1137}
1138
Devang Patelf98d8fe2009-09-01 01:14:15 +00001139/// CreateLocation - Creates a debug info location.
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001140DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
Daniel Dunbara279bc32009-09-20 02:20:51 +00001141 DIScope S, DILocation OrigLoc) {
Devang Patelf98d8fe2009-09-01 01:14:15 +00001142 Value *Elts[] = {
1143 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1144 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001145 S,
1146 OrigLoc,
Devang Patelf98d8fe2009-09-01 01:14:15 +00001147 };
1148 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1149}
1150
Chris Lattnera45664f2008-11-10 02:56:27 +00001151//===----------------------------------------------------------------------===//
1152// DIFactory: Routines for inserting code into a function
1153//===----------------------------------------------------------------------===//
1154
Chris Lattnera45664f2008-11-10 02:56:27 +00001155/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Devang Patel6daf99b2009-11-10 22:05:35 +00001156Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001157 Instruction *InsertBefore) {
Victor Hernandez4cf292a2010-01-26 02:07:38 +00001158 assert(Storage && "no storage passed to dbg.declare");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001159 assert(D.Verify() && "empty DIVariable passed to dbg.declare");
Chris Lattnera45664f2008-11-10 02:56:27 +00001160 if (!DeclareFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +00001161 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1162
Victor Hernandez756462b2010-01-18 20:42:09 +00001163 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
Devang Patel2db49d72010-05-07 18:11:54 +00001164 D };
Devang Patel6daf99b2009-11-10 22:05:35 +00001165 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
Mike Stumpe4250392009-10-01 22:08:58 +00001166}
1167
1168/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Devang Patel6daf99b2009-11-10 22:05:35 +00001169Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001170 BasicBlock *InsertAtEnd) {
Victor Hernandez4cf292a2010-01-26 02:07:38 +00001171 assert(Storage && "no storage passed to dbg.declare");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001172 assert(D.Verify() && "invalid DIVariable passed to dbg.declare");
Mike Stumpe4250392009-10-01 22:08:58 +00001173 if (!DeclareFn)
1174 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1175
Victor Hernandez756462b2010-01-18 20:42:09 +00001176 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
Devang Patel2db49d72010-05-07 18:11:54 +00001177 D };
Devang Patel27a53de2010-01-29 18:30:57 +00001178
1179 // If this block already has a terminator then insert this intrinsic
1180 // before the terminator.
Jim Grosbache62b6902010-07-21 21:36:25 +00001181 if (TerminatorInst *T = InsertAtEnd->getTerminator())
Devang Patel27a53de2010-01-29 18:30:57 +00001182 return CallInst::Create(DeclareFn, Args, Args+2, "", T);
1183 else
1184 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);}
Torok Edwin620f2802008-12-16 09:07:36 +00001185
Victor Hernandezc59b3352009-12-07 21:54:43 +00001186/// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001187Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
Victor Hernandezc59b3352009-12-07 21:54:43 +00001188 DIVariable D,
1189 Instruction *InsertBefore) {
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001190 assert(V && "no value passed to dbg.value");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001191 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001192 if (!ValueFn)
1193 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1194
Victor Hernandezc8b7cd02010-01-20 05:44:11 +00001195 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001196 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
Devang Patel2db49d72010-05-07 18:11:54 +00001197 D };
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001198 return CallInst::Create(ValueFn, Args, Args+3, "", InsertBefore);
1199}
1200
Victor Hernandezc59b3352009-12-07 21:54:43 +00001201/// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001202Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
Victor Hernandezc59b3352009-12-07 21:54:43 +00001203 DIVariable D,
1204 BasicBlock *InsertAtEnd) {
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001205 assert(V && "no value passed to dbg.value");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001206 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001207 if (!ValueFn)
1208 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1209
Jim Grosbache62b6902010-07-21 21:36:25 +00001210 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001211 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
Devang Patel2db49d72010-05-07 18:11:54 +00001212 D };
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001213 return CallInst::Create(ValueFn, Args, Args+3, "", InsertAtEnd);
1214}
Devang Patele4b27562009-08-28 23:24:31 +00001215
Devang Pateld2f79a12009-07-28 19:55:13 +00001216//===----------------------------------------------------------------------===//
Devang Patel98c65172009-07-30 18:25:15 +00001217// DebugInfoFinder implementations.
Devang Pateld2f79a12009-07-28 19:55:13 +00001218//===----------------------------------------------------------------------===//
1219
Devang Patel98c65172009-07-30 18:25:15 +00001220/// processModule - Process entire module and collect debug info.
1221void DebugInfoFinder::processModule(Module &M) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001222 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1223 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
1224 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
1225 ++BI) {
Devang Patel01c5ff62010-05-04 01:05:02 +00001226 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
Devang Patelb4d31302009-07-31 18:18:52 +00001227 processDeclare(DDI);
Jim Grosbache62b6902010-07-21 21:36:25 +00001228
Chris Lattner28a9bf62010-04-02 20:44:29 +00001229 DebugLoc Loc = BI->getDebugLoc();
1230 if (Loc.isUnknown())
1231 continue;
Jim Grosbache62b6902010-07-21 21:36:25 +00001232
Chris Lattner28a9bf62010-04-02 20:44:29 +00001233 LLVMContext &Ctx = BI->getContext();
1234 DIDescriptor Scope(Loc.getScope(Ctx));
Jim Grosbache62b6902010-07-21 21:36:25 +00001235
Chris Lattner28a9bf62010-04-02 20:44:29 +00001236 if (Scope.isCompileUnit())
Devang Patel2db49d72010-05-07 18:11:54 +00001237 addCompileUnit(DICompileUnit(Scope));
Chris Lattner28a9bf62010-04-02 20:44:29 +00001238 else if (Scope.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001239 processSubprogram(DISubprogram(Scope));
Chris Lattner28a9bf62010-04-02 20:44:29 +00001240 else if (Scope.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001241 processLexicalBlock(DILexicalBlock(Scope));
Jim Grosbache62b6902010-07-21 21:36:25 +00001242
Chris Lattner28a9bf62010-04-02 20:44:29 +00001243 if (MDNode *IA = Loc.getInlinedAt(Ctx))
1244 processLocation(DILocation(IA));
Devang Pateld2f79a12009-07-28 19:55:13 +00001245 }
Devang Patele4b27562009-08-28 23:24:31 +00001246
Devang Patelfd5fdc32010-06-28 05:53:08 +00001247 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
1248 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1249 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
1250 if (addGlobalVariable(DIG)) {
1251 addCompileUnit(DIG.getCompileUnit());
1252 processType(DIG.getType());
1253 }
Devang Pateld2f79a12009-07-28 19:55:13 +00001254 }
1255 }
Devang Patelfd5fdc32010-06-28 05:53:08 +00001256
1257 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
1258 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
1259 processSubprogram(DISubprogram(NMD->getOperand(i)));
Devang Pateld2f79a12009-07-28 19:55:13 +00001260}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001261
Devang Patel6daf99b2009-11-10 22:05:35 +00001262/// processLocation - Process DILocation.
1263void DebugInfoFinder::processLocation(DILocation Loc) {
Devang Patel3c91b052010-03-08 20:52:55 +00001264 if (!Loc.Verify()) return;
Devang Patel2db49d72010-05-07 18:11:54 +00001265 DIDescriptor S(Loc.getScope());
Devang Patel6daf99b2009-11-10 22:05:35 +00001266 if (S.isCompileUnit())
Devang Patel2db49d72010-05-07 18:11:54 +00001267 addCompileUnit(DICompileUnit(S));
Devang Patel6daf99b2009-11-10 22:05:35 +00001268 else if (S.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001269 processSubprogram(DISubprogram(S));
Devang Patel6daf99b2009-11-10 22:05:35 +00001270 else if (S.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001271 processLexicalBlock(DILexicalBlock(S));
Devang Patel6daf99b2009-11-10 22:05:35 +00001272 processLocation(Loc.getOrigLocation());
1273}
1274
Devang Patel98c65172009-07-30 18:25:15 +00001275/// processType - Process DIType.
1276void DebugInfoFinder::processType(DIType DT) {
Devang Patel72bcdb62009-08-10 22:09:58 +00001277 if (!addType(DT))
Devang Pateld2f79a12009-07-28 19:55:13 +00001278 return;
1279
1280 addCompileUnit(DT.getCompileUnit());
Devang Patel6ceea332009-08-31 18:49:10 +00001281 if (DT.isCompositeType()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001282 DICompositeType DCT(DT);
Devang Patel98c65172009-07-30 18:25:15 +00001283 processType(DCT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001284 DIArray DA = DCT.getTypeArray();
Devang Patel3c91b052010-03-08 20:52:55 +00001285 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1286 DIDescriptor D = DA.getElement(i);
1287 if (D.isType())
Devang Patel2db49d72010-05-07 18:11:54 +00001288 processType(DIType(D));
Devang Patel3c91b052010-03-08 20:52:55 +00001289 else if (D.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001290 processSubprogram(DISubprogram(D));
Devang Patel3c91b052010-03-08 20:52:55 +00001291 }
Devang Patel6ceea332009-08-31 18:49:10 +00001292 } else if (DT.isDerivedType()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001293 DIDerivedType DDT(DT);
Devang Patel3c91b052010-03-08 20:52:55 +00001294 processType(DDT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001295 }
1296}
1297
Devang Patelbeab41b2009-10-07 22:04:08 +00001298/// processLexicalBlock
1299void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
Devang Patelbeab41b2009-10-07 22:04:08 +00001300 DIScope Context = LB.getContext();
1301 if (Context.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001302 return processLexicalBlock(DILexicalBlock(Context));
Devang Patelbeab41b2009-10-07 22:04:08 +00001303 else
Devang Patel2db49d72010-05-07 18:11:54 +00001304 return processSubprogram(DISubprogram(Context));
Devang Patelbeab41b2009-10-07 22:04:08 +00001305}
1306
Devang Patel98c65172009-07-30 18:25:15 +00001307/// processSubprogram - Process DISubprogram.
1308void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001309 if (!addSubprogram(SP))
1310 return;
1311 addCompileUnit(SP.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +00001312 processType(SP.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +00001313}
1314
Devang Patelb4d31302009-07-31 18:18:52 +00001315/// processDeclare - Process DbgDeclareInst.
1316void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
Devang Patel3c91b052010-03-08 20:52:55 +00001317 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1318 if (!N) return;
1319
1320 DIDescriptor DV(N);
1321 if (!DV.isVariable())
Devang Patelb4d31302009-07-31 18:18:52 +00001322 return;
1323
Devang Patel2db49d72010-05-07 18:11:54 +00001324 if (!NodesSeen.insert(DV))
Devang Patelb4d31302009-07-31 18:18:52 +00001325 return;
1326
Devang Patel3c91b052010-03-08 20:52:55 +00001327 addCompileUnit(DIVariable(N).getCompileUnit());
1328 processType(DIVariable(N).getType());
Devang Patelb4d31302009-07-31 18:18:52 +00001329}
1330
Devang Patel72bcdb62009-08-10 22:09:58 +00001331/// addType - Add type into Tys.
1332bool DebugInfoFinder::addType(DIType DT) {
Devang Patel3c91b052010-03-08 20:52:55 +00001333 if (!DT.isValid())
Devang Patel72bcdb62009-08-10 22:09:58 +00001334 return false;
1335
Devang Patel2db49d72010-05-07 18:11:54 +00001336 if (!NodesSeen.insert(DT))
Devang Patel72bcdb62009-08-10 22:09:58 +00001337 return false;
1338
Devang Patel2db49d72010-05-07 18:11:54 +00001339 TYs.push_back(DT);
Devang Patel72bcdb62009-08-10 22:09:58 +00001340 return true;
1341}
1342
Devang Pateld2f79a12009-07-28 19:55:13 +00001343/// addCompileUnit - Add compile unit into CUs.
Devang Patel98c65172009-07-30 18:25:15 +00001344bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Patel3c91b052010-03-08 20:52:55 +00001345 if (!CU.Verify())
Devang Pateld2f79a12009-07-28 19:55:13 +00001346 return false;
1347
Devang Patel2db49d72010-05-07 18:11:54 +00001348 if (!NodesSeen.insert(CU))
Devang Pateld2f79a12009-07-28 19:55:13 +00001349 return false;
1350
Devang Patel2db49d72010-05-07 18:11:54 +00001351 CUs.push_back(CU);
Devang Pateld2f79a12009-07-28 19:55:13 +00001352 return true;
1353}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001354
Devang Pateld2f79a12009-07-28 19:55:13 +00001355/// addGlobalVariable - Add global variable into GVs.
Devang Patel98c65172009-07-30 18:25:15 +00001356bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Patel2db49d72010-05-07 18:11:54 +00001357 if (!DIDescriptor(DIG).isGlobalVariable())
Devang Pateld2f79a12009-07-28 19:55:13 +00001358 return false;
1359
Devang Patel2db49d72010-05-07 18:11:54 +00001360 if (!NodesSeen.insert(DIG))
Devang Pateld2f79a12009-07-28 19:55:13 +00001361 return false;
1362
Devang Patel2db49d72010-05-07 18:11:54 +00001363 GVs.push_back(DIG);
Devang Pateld2f79a12009-07-28 19:55:13 +00001364 return true;
1365}
1366
1367// addSubprogram - Add subprgoram into SPs.
Devang Patel98c65172009-07-30 18:25:15 +00001368bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Patel2db49d72010-05-07 18:11:54 +00001369 if (!DIDescriptor(SP).isSubprogram())
Devang Pateld2f79a12009-07-28 19:55:13 +00001370 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001371
Devang Patel2db49d72010-05-07 18:11:54 +00001372 if (!NodesSeen.insert(SP))
Devang Pateld2f79a12009-07-28 19:55:13 +00001373 return false;
1374
Devang Patel2db49d72010-05-07 18:11:54 +00001375 SPs.push_back(SP);
Devang Pateld2f79a12009-07-28 19:55:13 +00001376 return true;
1377}
1378
Victor Hernandez756462b2010-01-18 20:42:09 +00001379/// Find the debug info descriptor corresponding to this global variable.
1380static Value *findDbgGlobalDeclare(GlobalVariable *V) {
Chris Lattner099b7792009-12-29 09:22:47 +00001381 const Module *M = V->getParent();
1382 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1383 if (!NMD)
Torok Edwinff7d0e92009-03-10 13:41:26 +00001384 return 0;
Chris Lattner099b7792009-12-29 09:22:47 +00001385
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001386 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman872814a2010-07-21 18:54:18 +00001387 DIDescriptor DIG(cast<MDNode>(NMD->getOperand(i)));
Devang Patel3c91b052010-03-08 20:52:55 +00001388 if (!DIG.isGlobalVariable())
Chris Lattner099b7792009-12-29 09:22:47 +00001389 continue;
Devang Patel2db49d72010-05-07 18:11:54 +00001390 if (DIGlobalVariable(DIG).getGlobal() == V)
1391 return DIG;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001392 }
Chris Lattner099b7792009-12-29 09:22:47 +00001393 return 0;
1394}
Torok Edwinff7d0e92009-03-10 13:41:26 +00001395
Chris Lattner099b7792009-12-29 09:22:47 +00001396/// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
1397/// It looks through pointer casts too.
Victor Hernandez756462b2010-01-18 20:42:09 +00001398static const DbgDeclareInst *findDbgDeclare(const Value *V) {
Victor Hernandez3a328652010-01-15 19:04:09 +00001399 V = V->stripPointerCasts();
Jim Grosbache62b6902010-07-21 21:36:25 +00001400
Victor Hernandez3a328652010-01-15 19:04:09 +00001401 if (!isa<Instruction>(V) && !isa<Argument>(V))
Torok Edwin620f2802008-12-16 09:07:36 +00001402 return 0;
Jim Grosbache62b6902010-07-21 21:36:25 +00001403
Victor Hernandez3a328652010-01-15 19:04:09 +00001404 const Function *F = NULL;
1405 if (const Instruction *I = dyn_cast<Instruction>(V))
1406 F = I->getParent()->getParent();
1407 else if (const Argument *A = dyn_cast<Argument>(V))
1408 F = A->getParent();
Jim Grosbache62b6902010-07-21 21:36:25 +00001409
Victor Hernandez3a328652010-01-15 19:04:09 +00001410 for (Function::const_iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
1411 for (BasicBlock::const_iterator BI = (*FI).begin(), BE = (*FI).end();
1412 BI != BE; ++BI)
1413 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1414 if (DDI->getAddress() == V)
1415 return DDI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001416
Chris Lattner099b7792009-12-29 09:22:47 +00001417 return 0;
1418}
Bill Wendlingdc817b62009-05-14 18:26:15 +00001419
Chris Lattner099b7792009-12-29 09:22:47 +00001420bool llvm::getLocationInfo(const Value *V, std::string &DisplayName,
1421 std::string &Type, unsigned &LineNo,
1422 std::string &File, std::string &Dir) {
1423 DICompileUnit Unit;
1424 DIType TypeD;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001425
Chris Lattner099b7792009-12-29 09:22:47 +00001426 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1427 Value *DIGV = findDbgGlobalDeclare(GV);
1428 if (!DIGV) return false;
1429 DIGlobalVariable Var(cast<MDNode>(DIGV));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001430
Chris Lattner099b7792009-12-29 09:22:47 +00001431 StringRef D = Var.getDisplayName();
Devang Patel65dbc902009-11-25 17:36:49 +00001432 if (!D.empty())
Chris Lattner099b7792009-12-29 09:22:47 +00001433 DisplayName = D;
1434 LineNo = Var.getLineNumber();
1435 Unit = Var.getCompileUnit();
1436 TypeD = Var.getType();
1437 } else {
1438 const DbgDeclareInst *DDI = findDbgDeclare(V);
1439 if (!DDI) return false;
1440 DIVariable Var(cast<MDNode>(DDI->getVariable()));
1441
1442 StringRef D = Var.getName();
1443 if (!D.empty())
1444 DisplayName = D;
1445 LineNo = Var.getLineNumber();
1446 Unit = Var.getCompileUnit();
1447 TypeD = Var.getType();
Torok Edwinff7d0e92009-03-10 13:41:26 +00001448 }
Devang Patel13e16b62009-06-26 01:49:18 +00001449
Chris Lattner099b7792009-12-29 09:22:47 +00001450 StringRef T = TypeD.getName();
1451 if (!T.empty())
1452 Type = T;
1453 StringRef F = Unit.getFilename();
1454 if (!F.empty())
1455 File = F;
1456 StringRef D = Unit.getDirectory();
1457 if (!D.empty())
1458 Dir = D;
1459 return true;
1460}
Devang Patel9e529c32009-07-02 01:15:24 +00001461
Chris Lattner099b7792009-12-29 09:22:47 +00001462/// getDISubprogram - Find subprogram that is enclosing this scope.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001463DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
Chris Lattner099b7792009-12-29 09:22:47 +00001464 DIDescriptor D(Scope);
Chris Lattner099b7792009-12-29 09:22:47 +00001465 if (D.isSubprogram())
1466 return DISubprogram(Scope);
Jim Grosbache62b6902010-07-21 21:36:25 +00001467
Chris Lattner099b7792009-12-29 09:22:47 +00001468 if (D.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001469 return getDISubprogram(DILexicalBlock(Scope).getContext());
Jim Grosbache62b6902010-07-21 21:36:25 +00001470
Chris Lattner099b7792009-12-29 09:22:47 +00001471 return DISubprogram();
1472}
Devang Patel193f7202009-11-24 01:14:22 +00001473
Chris Lattner099b7792009-12-29 09:22:47 +00001474/// getDICompositeType - Find underlying composite type.
1475DICompositeType llvm::getDICompositeType(DIType T) {
Chris Lattner099b7792009-12-29 09:22:47 +00001476 if (T.isCompositeType())
Devang Patel2db49d72010-05-07 18:11:54 +00001477 return DICompositeType(T);
Jim Grosbache62b6902010-07-21 21:36:25 +00001478
Chris Lattner099b7792009-12-29 09:22:47 +00001479 if (T.isDerivedType())
Devang Patel2db49d72010-05-07 18:11:54 +00001480 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
Jim Grosbache62b6902010-07-21 21:36:25 +00001481
Chris Lattner099b7792009-12-29 09:22:47 +00001482 return DICompositeType();
Torok Edwin620f2802008-12-16 09:07:36 +00001483}