blob: fec269e2205063b5946125007ab7294c9a08a495 [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:
Devang Patel49d96382010-08-23 23:16:25 +0000138 case dwarf::DW_TAG_friend:
Chris Lattnera45664f2008-11-10 02:56:27 +0000139 return true;
140 default:
Devang Patele4b27562009-08-28 23:24:31 +0000141 // CompositeTypes are currently modelled as DerivedTypes.
Devang Patel6ceea332009-08-31 18:49:10 +0000142 return isCompositeType();
Chris Lattnera45664f2008-11-10 02:56:27 +0000143 }
144}
145
Chris Lattnera45664f2008-11-10 02:56:27 +0000146/// isCompositeType - Return true if the specified tag is legal for
147/// DICompositeType.
Devang Patel6ceea332009-08-31 18:49:10 +0000148bool DIDescriptor::isCompositeType() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000149 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000150 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000151 case dwarf::DW_TAG_array_type:
152 case dwarf::DW_TAG_structure_type:
153 case dwarf::DW_TAG_union_type:
154 case dwarf::DW_TAG_enumeration_type:
155 case dwarf::DW_TAG_vector_type:
156 case dwarf::DW_TAG_subroutine_type:
Devang Patel25cb0d72009-03-25 03:52:06 +0000157 case dwarf::DW_TAG_class_type:
Chris Lattnera45664f2008-11-10 02:56:27 +0000158 return true;
159 default:
160 return false;
161 }
162}
163
Chris Lattnera45664f2008-11-10 02:56:27 +0000164/// isVariable - Return true if the specified tag is legal for DIVariable.
Devang Patel6ceea332009-08-31 18:49:10 +0000165bool DIDescriptor::isVariable() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000166 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000167 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000168 case dwarf::DW_TAG_auto_variable:
169 case dwarf::DW_TAG_arg_variable:
170 case dwarf::DW_TAG_return_variable:
171 return true;
172 default:
173 return false;
174 }
175}
176
Devang Patelecbeb1a2009-09-30 22:34:41 +0000177/// isType - Return true if the specified tag is legal for DIType.
178bool DIDescriptor::isType() const {
179 return isBasicType() || isCompositeType() || isDerivedType();
180}
181
Devang Patel6ceea332009-08-31 18:49:10 +0000182/// isSubprogram - Return true if the specified tag is legal for
183/// DISubprogram.
184bool DIDescriptor::isSubprogram() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000185 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
Devang Patel6ceea332009-08-31 18:49:10 +0000186}
187
188/// isGlobalVariable - Return true if the specified tag is legal for
189/// DIGlobalVariable.
190bool DIDescriptor::isGlobalVariable() const {
Devang Patel29368072010-08-10 07:11:13 +0000191 return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
192 getTag() == dwarf::DW_TAG_constant);
Devang Patel6ceea332009-08-31 18:49:10 +0000193}
194
Devang Patelecbeb1a2009-09-30 22:34:41 +0000195/// isGlobal - Return true if the specified tag is legal for DIGlobal.
196bool DIDescriptor::isGlobal() const {
197 return isGlobalVariable();
198}
199
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000200/// isScope - Return true if the specified tag is one of the scope
Devang Patel43d98b32009-08-31 20:44:45 +0000201/// related tag.
202bool DIDescriptor::isScope() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000203 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000204 switch (getTag()) {
205 case dwarf::DW_TAG_compile_unit:
206 case dwarf::DW_TAG_lexical_block:
207 case dwarf::DW_TAG_subprogram:
208 case dwarf::DW_TAG_namespace:
209 return true;
210 default:
211 break;
Devang Patel43d98b32009-08-31 20:44:45 +0000212 }
213 return false;
214}
Devang Patel6ceea332009-08-31 18:49:10 +0000215
Devang Patelc9f322d2009-08-31 21:34:44 +0000216/// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
217bool DIDescriptor::isCompileUnit() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000218 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
Devang Patelc9f322d2009-08-31 21:34:44 +0000219}
220
Devang Patel7aa81892010-03-08 22:27:22 +0000221/// isFile - Return true if the specified tag is DW_TAG_file_type.
222bool DIDescriptor::isFile() const {
223 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
224}
225
Devang Patel6404e4e2009-12-15 19:16:48 +0000226/// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
227bool DIDescriptor::isNameSpace() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000228 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
Devang Patel6404e4e2009-12-15 19:16:48 +0000229}
230
Devang Patel5e005d82009-08-31 22:00:15 +0000231/// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
232bool DIDescriptor::isLexicalBlock() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000233 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block;
Devang Patel5e005d82009-08-31 22:00:15 +0000234}
235
Devang Patelecbeb1a2009-09-30 22:34:41 +0000236/// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
237bool DIDescriptor::isSubrange() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000238 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
Devang Patelecbeb1a2009-09-30 22:34:41 +0000239}
240
241/// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
242bool DIDescriptor::isEnumerator() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000243 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
Devang Patelecbeb1a2009-09-30 22:34:41 +0000244}
245
Devang Patel6ceea332009-08-31 18:49:10 +0000246//===----------------------------------------------------------------------===//
247// Simple Descriptor Constructors and other Methods
248//===----------------------------------------------------------------------===//
249
Devang Patele9f8f5e2010-05-07 20:54:48 +0000250DIType::DIType(const MDNode *N) : DIScope(N) {
Devang Patel6ceea332009-08-31 18:49:10 +0000251 if (!N) return;
252 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
253 DbgNode = 0;
254 }
255}
256
Devang Patel68afdc32009-01-05 18:33:01 +0000257unsigned DIArray::getNumElements() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000258 if (!DbgNode)
259 return 0;
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000260 return DbgNode->getNumOperands();
Devang Patel68afdc32009-01-05 18:33:01 +0000261}
Chris Lattnera45664f2008-11-10 02:56:27 +0000262
Devang Patelc4999d72009-07-22 18:23:44 +0000263/// replaceAllUsesWith - Replace all uses of debug info referenced by
Dan Gohman872814a2010-07-21 18:54:18 +0000264/// this descriptor.
Dan Gohman489b29b2010-08-20 22:02:26 +0000265void DIType::replaceAllUsesWith(DIDescriptor &D) {
Devang Patel3c91b052010-03-08 20:52:55 +0000266 if (!DbgNode)
Devang Patelc4999d72009-07-22 18:23:44 +0000267 return;
268
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000269 // Since we use a TrackingVH for the node, its easy for clients to manufacture
270 // legitimate situations where they want to replaceAllUsesWith() on something
271 // which, due to uniquing, has merged with the source. We shield clients from
272 // this detail by allowing a value to be replaced with replaceAllUsesWith()
273 // itself.
Devang Pateled66bf52010-05-07 18:19:32 +0000274 if (DbgNode != D) {
Devang Patele9f8f5e2010-05-07 20:54:48 +0000275 MDNode *Node = const_cast<MDNode*>(DbgNode);
276 const MDNode *DN = D;
277 const Value *V = cast_or_null<Value>(DN);
278 Node->replaceAllUsesWith(const_cast<Value*>(V));
Dan Gohman489b29b2010-08-20 22:02:26 +0000279 MDNode::deleteTemporary(Node);
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000280 }
Devang Patelc4999d72009-07-22 18:23:44 +0000281}
282
Devang Patelb79b5352009-01-19 23:21:49 +0000283/// Verify - Verify that a compile unit is well formed.
284bool DICompileUnit::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000285 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000286 return false;
Devang Patel65dbc902009-11-25 17:36:49 +0000287 StringRef N = getFilename();
288 if (N.empty())
Bill Wendling0582ae92009-03-13 04:39:26 +0000289 return false;
Devang Patelb79b5352009-01-19 23:21:49 +0000290 // It is possible that directory and produce string is empty.
Bill Wendling0582ae92009-03-13 04:39:26 +0000291 return true;
Devang Patelb79b5352009-01-19 23:21:49 +0000292}
293
294/// Verify - Verify that a type descriptor is well formed.
295bool DIType::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000296 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000297 return false;
Devang Patel3c91b052010-03-08 20:52:55 +0000298 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000299 return false;
300
301 DICompileUnit CU = getCompileUnit();
Devang Patel3c91b052010-03-08 20:52:55 +0000302 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000303 return false;
304 return true;
305}
306
Devang Patel0c4720c2010-08-23 18:25:56 +0000307/// Verify - Verify that a basic type descriptor is well formed.
308bool DIBasicType::Verify() const {
309 return isBasicType();
310}
311
312/// Verify - Verify that a derived type descriptor is well formed.
313bool DIDerivedType::Verify() const {
314 return isDerivedType();
315}
316
Devang Patelb79b5352009-01-19 23:21:49 +0000317/// Verify - Verify that a composite type descriptor is well formed.
318bool DICompositeType::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000319 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000320 return false;
Devang Patel3c91b052010-03-08 20:52:55 +0000321 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000322 return false;
323
324 DICompileUnit CU = getCompileUnit();
Devang Patel3c91b052010-03-08 20:52:55 +0000325 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000326 return false;
327 return true;
328}
329
330/// Verify - Verify that a subprogram descriptor is well formed.
331bool DISubprogram::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000332 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000333 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000334
Devang Patel3c91b052010-03-08 20:52:55 +0000335 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000336 return false;
337
338 DICompileUnit CU = getCompileUnit();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000339 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000340 return false;
341
342 DICompositeType Ty = getType();
Devang Patel3c91b052010-03-08 20:52:55 +0000343 if (!Ty.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000344 return false;
345 return true;
346}
347
348/// Verify - Verify that a global variable descriptor is well formed.
349bool DIGlobalVariable::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000350 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000351 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000352
Devang Patel65dbc902009-11-25 17:36:49 +0000353 if (getDisplayName().empty())
Devang Patel84c73e92009-11-06 17:58:12 +0000354 return false;
355
Devang Patel3c91b052010-03-08 20:52:55 +0000356 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000357 return false;
358
359 DICompileUnit CU = getCompileUnit();
Devang Patel3c91b052010-03-08 20:52:55 +0000360 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000361 return false;
362
363 DIType Ty = getType();
364 if (!Ty.Verify())
365 return false;
366
Devang Patel27398962010-08-09 21:39:24 +0000367 if (!getGlobal() && !getConstant())
Devang Patelb79b5352009-01-19 23:21:49 +0000368 return false;
369
370 return true;
371}
372
373/// Verify - Verify that a variable descriptor is well formed.
374bool DIVariable::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000375 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000376 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000377
Devang Patel3c91b052010-03-08 20:52:55 +0000378 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000379 return false;
380
Devang Patel62077af2010-05-07 21:42:24 +0000381 if (!getCompileUnit().Verify())
382 return false;
383
Devang Patelb79b5352009-01-19 23:21:49 +0000384 DIType Ty = getType();
385 if (!Ty.Verify())
386 return false;
387
Devang Patelb79b5352009-01-19 23:21:49 +0000388 return true;
389}
390
Devang Patel3c91b052010-03-08 20:52:55 +0000391/// Verify - Verify that a location descriptor is well formed.
392bool DILocation::Verify() const {
393 if (!DbgNode)
394 return false;
Jim Grosbache62b6902010-07-21 21:36:25 +0000395
Devang Patel3c91b052010-03-08 20:52:55 +0000396 return DbgNode->getNumOperands() == 4;
397}
398
Devang Patel47e22652010-05-07 23:04:32 +0000399/// Verify - Verify that a namespace descriptor is well formed.
400bool DINameSpace::Verify() const {
401 if (!DbgNode)
402 return false;
403 if (getName().empty())
404 return false;
405 if (!getCompileUnit().Verify())
406 return false;
407 return true;
408}
409
Devang Patel36375ee2009-02-17 21:23:59 +0000410/// getOriginalTypeSize - If this type is derived from a base type then
411/// return base type size.
412uint64_t DIDerivedType::getOriginalTypeSize() const {
Devang Patel61ecbd12009-11-04 23:48:00 +0000413 unsigned Tag = getTag();
414 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
415 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
416 Tag == dwarf::DW_TAG_restrict_type) {
417 DIType BaseType = getTypeDerivedFrom();
Jim Grosbache62b6902010-07-21 21:36:25 +0000418 // If this type is not derived from any type then take conservative
Devang Patel5ebfa2d2009-11-06 18:24:05 +0000419 // approach.
Devang Patel3c91b052010-03-08 20:52:55 +0000420 if (!BaseType.isValid())
Devang Patel5ebfa2d2009-11-06 18:24:05 +0000421 return getSizeInBits();
Devang Patel61ecbd12009-11-04 23:48:00 +0000422 if (BaseType.isDerivedType())
Devang Patel2db49d72010-05-07 18:11:54 +0000423 return DIDerivedType(BaseType).getOriginalTypeSize();
Devang Patel61ecbd12009-11-04 23:48:00 +0000424 else
425 return BaseType.getSizeInBits();
426 }
Jim Grosbache62b6902010-07-21 21:36:25 +0000427
Devang Patel61ecbd12009-11-04 23:48:00 +0000428 return getSizeInBits();
Devang Patel36375ee2009-02-17 21:23:59 +0000429}
Devang Patelb79b5352009-01-19 23:21:49 +0000430
Jim Grosbache62b6902010-07-21 21:36:25 +0000431/// isInlinedFnArgument - Return true if this variable provides debugging
Devang Patel719f6a92010-04-29 20:40:36 +0000432/// information for an inlined function arguments.
433bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
434 assert(CurFn && "Invalid function");
435 if (!getContext().isSubprogram())
436 return false;
Jim Grosbache62b6902010-07-21 21:36:25 +0000437 // This variable is not inlined function argument if its scope
Devang Patel719f6a92010-04-29 20:40:36 +0000438 // does not describe current function.
Devang Patel2db49d72010-05-07 18:11:54 +0000439 return !(DISubprogram(getContext()).describes(CurFn));
Devang Patel719f6a92010-04-29 20:40:36 +0000440}
441
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000442/// describes - Return true if this subprogram provides debugging
443/// information for the function F.
444bool DISubprogram::describes(const Function *F) {
Chris Lattner099b7792009-12-29 09:22:47 +0000445 assert(F && "Invalid function");
Devang Patelffd33cd2010-06-16 06:42:02 +0000446 if (F == getFunction())
447 return true;
Devang Patel65dbc902009-11-25 17:36:49 +0000448 StringRef Name = getLinkageName();
449 if (Name.empty())
Devang Patel5ccdd102009-09-29 18:40:58 +0000450 Name = getName();
Devang Patel65dbc902009-11-25 17:36:49 +0000451 if (F->getName() == Name)
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000452 return true;
453 return false;
454}
455
Jim Grosbache62b6902010-07-21 21:36:25 +0000456unsigned DISubprogram::isOptimized() const {
Devang Patelccff8122010-04-30 19:38:23 +0000457 assert (DbgNode && "Invalid subprogram descriptor!");
458 if (DbgNode->getNumOperands() == 16)
459 return getUnsignedField(15);
460 return 0;
461}
462
Devang Patel65dbc902009-11-25 17:36:49 +0000463StringRef DIScope::getFilename() const {
Devang Patel77bf2952010-03-08 22:02:50 +0000464 if (!DbgNode)
465 return StringRef();
Jim Grosbache62b6902010-07-21 21:36:25 +0000466 if (isLexicalBlock())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000467 return DILexicalBlock(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000468 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000469 return DISubprogram(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000470 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000471 return DICompileUnit(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000472 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000473 return DINameSpace(DbgNode).getFilename();
Devang Patel77bf2952010-03-08 22:02:50 +0000474 if (isType())
475 return DIType(DbgNode).getFilename();
Devang Patel7aa81892010-03-08 22:27:22 +0000476 if (isFile())
477 return DIFile(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000478 assert(0 && "Invalid DIScope!");
Devang Patel65dbc902009-11-25 17:36:49 +0000479 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000480}
481
Devang Patel65dbc902009-11-25 17:36:49 +0000482StringRef DIScope::getDirectory() const {
Devang Patel77bf2952010-03-08 22:02:50 +0000483 if (!DbgNode)
484 return StringRef();
Jim Grosbache62b6902010-07-21 21:36:25 +0000485 if (isLexicalBlock())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000486 return DILexicalBlock(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000487 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000488 return DISubprogram(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000489 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000490 return DICompileUnit(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000491 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000492 return DINameSpace(DbgNode).getDirectory();
Devang Patel77bf2952010-03-08 22:02:50 +0000493 if (isType())
494 return DIType(DbgNode).getDirectory();
Devang Patel7aa81892010-03-08 22:27:22 +0000495 if (isFile())
496 return DIFile(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000497 assert(0 && "Invalid DIScope!");
Devang Patel65dbc902009-11-25 17:36:49 +0000498 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000499}
500
Chris Lattnera45664f2008-11-10 02:56:27 +0000501//===----------------------------------------------------------------------===//
Devang Patel7136a652009-07-01 22:10:23 +0000502// DIDescriptor: dump routines for all descriptors.
503//===----------------------------------------------------------------------===//
504
505
Dan Gohman50404362010-05-07 15:30:29 +0000506/// print - Print descriptor.
507void DIDescriptor::print(raw_ostream &OS) const {
508 OS << "[" << dwarf::TagString(getTag()) << "] ";
509 OS.write_hex((intptr_t) &*DbgNode) << ']';
Devang Patel7136a652009-07-01 22:10:23 +0000510}
511
Dan Gohman50404362010-05-07 15:30:29 +0000512/// print - Print compile unit.
513void DICompileUnit::print(raw_ostream &OS) const {
Devang Patel7136a652009-07-01 22:10:23 +0000514 if (getLanguage())
Dan Gohman50404362010-05-07 15:30:29 +0000515 OS << " [" << dwarf::LanguageString(getLanguage()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000516
Dan Gohmanfaa19c32010-05-10 20:07:44 +0000517 OS << " [" << getDirectory() << "/" << getFilename() << "]";
Devang Patel7136a652009-07-01 22:10:23 +0000518}
519
Dan Gohman50404362010-05-07 15:30:29 +0000520/// print - Print type.
521void DIType::print(raw_ostream &OS) const {
Devang Patel3c91b052010-03-08 20:52:55 +0000522 if (!DbgNode) return;
Devang Patel7136a652009-07-01 22:10:23 +0000523
Devang Patel65dbc902009-11-25 17:36:49 +0000524 StringRef Res = getName();
525 if (!Res.empty())
Dan Gohman50404362010-05-07 15:30:29 +0000526 OS << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000527
528 unsigned Tag = getTag();
Dan Gohman50404362010-05-07 15:30:29 +0000529 OS << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000530
531 // TODO : Print context
Dan Gohmanc014d092010-05-07 16:17:22 +0000532 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000533 OS << " ["
Dan Gohman9a7063e2010-05-07 16:39:27 +0000534 << "line " << getLineNumber() << ", "
535 << getSizeInBits() << " bits, "
536 << getAlignInBits() << " bit alignment, "
537 << getOffsetInBits() << " bit offset"
Chris Lattnera81d29b2009-08-23 07:33:14 +0000538 << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000539
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000540 if (isPrivate())
Dan Gohman50404362010-05-07 15:30:29 +0000541 OS << " [private] ";
Devang Patel7136a652009-07-01 22:10:23 +0000542 else if (isProtected())
Dan Gohman50404362010-05-07 15:30:29 +0000543 OS << " [protected] ";
Devang Patel7136a652009-07-01 22:10:23 +0000544
545 if (isForwardDecl())
Dan Gohman50404362010-05-07 15:30:29 +0000546 OS << " [fwd] ";
Devang Patel7136a652009-07-01 22:10:23 +0000547
Devang Patel6ceea332009-08-31 18:49:10 +0000548 if (isBasicType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000549 DIBasicType(DbgNode).print(OS);
Devang Patel6ceea332009-08-31 18:49:10 +0000550 else if (isDerivedType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000551 DIDerivedType(DbgNode).print(OS);
Devang Patel6ceea332009-08-31 18:49:10 +0000552 else if (isCompositeType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000553 DICompositeType(DbgNode).print(OS);
Devang Patel7136a652009-07-01 22:10:23 +0000554 else {
Dan Gohman50404362010-05-07 15:30:29 +0000555 OS << "Invalid DIType\n";
Devang Patel7136a652009-07-01 22:10:23 +0000556 return;
557 }
558
Dan Gohman50404362010-05-07 15:30:29 +0000559 OS << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000560}
561
Dan Gohman50404362010-05-07 15:30:29 +0000562/// print - Print basic type.
563void DIBasicType::print(raw_ostream &OS) const {
564 OS << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000565}
566
Dan Gohman50404362010-05-07 15:30:29 +0000567/// print - Print derived type.
568void DIDerivedType::print(raw_ostream &OS) const {
Dan Gohmanc014d092010-05-07 16:17:22 +0000569 OS << "\n\t Derived From: "; getTypeDerivedFrom().print(OS);
Devang Patel7136a652009-07-01 22:10:23 +0000570}
571
Dan Gohman50404362010-05-07 15:30:29 +0000572/// print - Print composite type.
573void DICompositeType::print(raw_ostream &OS) const {
Devang Patel7136a652009-07-01 22:10:23 +0000574 DIArray A = getTypeArray();
Dan Gohman50404362010-05-07 15:30:29 +0000575 OS << " [" << A.getNumElements() << " elements]";
Devang Patel7136a652009-07-01 22:10:23 +0000576}
577
Dan Gohman50404362010-05-07 15:30:29 +0000578/// print - Print subprogram.
579void DISubprogram::print(raw_ostream &OS) const {
Devang Patel65dbc902009-11-25 17:36:49 +0000580 StringRef Res = getName();
581 if (!Res.empty())
Dan Gohman50404362010-05-07 15:30:29 +0000582 OS << " [" << Res << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000583
584 unsigned Tag = getTag();
Dan Gohman50404362010-05-07 15:30:29 +0000585 OS << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000586
587 // TODO : Print context
Dan Gohmanc014d092010-05-07 16:17:22 +0000588 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000589 OS << " [" << getLineNumber() << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000590
591 if (isLocalToUnit())
Dan Gohman50404362010-05-07 15:30:29 +0000592 OS << " [local] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000593
594 if (isDefinition())
Dan Gohman50404362010-05-07 15:30:29 +0000595 OS << " [def] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000596
Dan Gohman50404362010-05-07 15:30:29 +0000597 OS << "\n";
598}
599
600/// print - Print global variable.
601void DIGlobalVariable::print(raw_ostream &OS) const {
602 OS << " [";
Devang Patela49d8772010-05-07 23:19:07 +0000603 StringRef Res = getName();
604 if (!Res.empty())
605 OS << " [" << Res << "] ";
606
607 unsigned Tag = getTag();
608 OS << " [" << dwarf::TagString(Tag) << "] ";
609
610 // TODO : Print context
611 getCompileUnit().print(OS);
612 OS << " [" << getLineNumber() << "] ";
613
614 if (isLocalToUnit())
615 OS << " [local] ";
616
617 if (isDefinition())
618 OS << " [def] ";
619
620 if (isGlobalVariable())
621 DIGlobalVariable(DbgNode).print(OS);
622 OS << "]\n";
Dan Gohman50404362010-05-07 15:30:29 +0000623}
624
625/// print - Print variable.
626void DIVariable::print(raw_ostream &OS) const {
627 StringRef Res = getName();
628 if (!Res.empty())
629 OS << " [" << Res << "] ";
630
Dan Gohmanc014d092010-05-07 16:17:22 +0000631 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000632 OS << " [" << getLineNumber() << "] ";
Dan Gohmanc014d092010-05-07 16:17:22 +0000633 getType().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000634 OS << "\n";
635
636 // FIXME: Dump complex addresses
637}
638
639/// dump - Print descriptor to dbgs() with a newline.
640void DIDescriptor::dump() const {
641 print(dbgs()); dbgs() << '\n';
642}
643
644/// dump - Print compile unit to dbgs() with a newline.
645void DICompileUnit::dump() const {
646 print(dbgs()); dbgs() << '\n';
647}
648
649/// dump - Print type to dbgs() with a newline.
650void DIType::dump() const {
651 print(dbgs()); dbgs() << '\n';
652}
653
654/// dump - Print basic type to dbgs() with a newline.
655void DIBasicType::dump() const {
656 print(dbgs()); dbgs() << '\n';
657}
658
659/// dump - Print derived type to dbgs() with a newline.
660void DIDerivedType::dump() const {
661 print(dbgs()); dbgs() << '\n';
662}
663
664/// dump - Print composite type to dbgs() with a newline.
665void DICompositeType::dump() const {
666 print(dbgs()); dbgs() << '\n';
667}
668
Dan Gohman50404362010-05-07 15:30:29 +0000669/// dump - Print subprogram to dbgs() with a newline.
670void DISubprogram::dump() const {
671 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000672}
673
674/// dump - Print global variable.
675void DIGlobalVariable::dump() const {
Dan Gohman50404362010-05-07 15:30:29 +0000676 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000677}
678
679/// dump - Print variable.
680void DIVariable::dump() const {
Dan Gohman50404362010-05-07 15:30:29 +0000681 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000682}
683
684//===----------------------------------------------------------------------===//
Chris Lattnera45664f2008-11-10 02:56:27 +0000685// DIFactory: Basic Helpers
686//===----------------------------------------------------------------------===//
687
Bill Wendlingdc817b62009-05-14 18:26:15 +0000688DIFactory::DIFactory(Module &m)
Victor Hernandez06203122010-01-23 00:03:28 +0000689 : M(m), VMContext(M.getContext()), DeclareFn(0), ValueFn(0) {}
Chris Lattner497a7a82008-11-10 04:10:34 +0000690
Chris Lattnera45664f2008-11-10 02:56:27 +0000691Constant *DIFactory::GetTagConstant(unsigned TAG) {
Devang Patel6906ba52009-01-20 19:22:03 +0000692 assert((TAG & LLVMDebugVersionMask) == 0 &&
Chris Lattnera45664f2008-11-10 02:56:27 +0000693 "Tag too large for debug encoding!");
Owen Anderson1d0be152009-08-13 21:58:54 +0000694 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
Chris Lattnera45664f2008-11-10 02:56:27 +0000695}
696
Chris Lattnera45664f2008-11-10 02:56:27 +0000697//===----------------------------------------------------------------------===//
698// DIFactory: Primary Constructors
699//===----------------------------------------------------------------------===//
700
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000701/// GetOrCreateArray - Create an descriptor for an array of descriptors.
Chris Lattnera45664f2008-11-10 02:56:27 +0000702/// This implicitly uniques the arrays created.
703DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
Benjamin Kramera53557e2010-09-21 16:41:29 +0000704 if (NumTys == 0) {
705 Value *Null = llvm::Constant::getNullValue(Type::getInt32Ty(VMContext));
706 return DIArray(MDNode::get(VMContext, &Null, 1));
707 }
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000708
Benjamin Kramera53557e2010-09-21 16:41:29 +0000709 SmallVector<Value *, 16> Elts(Tys, Tys+NumTys);
710 return DIArray(MDNode::get(VMContext, Elts.data(), Elts.size()));
Chris Lattnera45664f2008-11-10 02:56:27 +0000711}
712
713/// GetOrCreateSubrange - Create a descriptor for a value range. This
714/// implicitly uniques the values returned.
715DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
Devang Patele4b27562009-08-28 23:24:31 +0000716 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000717 GetTagConstant(dwarf::DW_TAG_subrange_type),
Owen Anderson1d0be152009-08-13 21:58:54 +0000718 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
719 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
Chris Lattnera45664f2008-11-10 02:56:27 +0000720 };
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000721
Devang Patele4b27562009-08-28 23:24:31 +0000722 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000723}
724
725
726
727/// CreateCompileUnit - Create a new descriptor for the specified compile
728/// unit. Note that this does not unique compile units within the module.
729DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
Devang Patel65dbc902009-11-25 17:36:49 +0000730 StringRef Filename,
731 StringRef Directory,
732 StringRef Producer,
Devang Pateldd9db662009-01-30 18:20:31 +0000733 bool isMain,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000734 bool isOptimized,
Devang Patel65dbc902009-11-25 17:36:49 +0000735 StringRef Flags,
Devang Patel13319ce2009-02-17 22:43:44 +0000736 unsigned RunTimeVer) {
Devang Patele4b27562009-08-28 23:24:31 +0000737 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000738 GetTagConstant(dwarf::DW_TAG_compile_unit),
Devang Patele4b27562009-08-28 23:24:31 +0000739 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Owen Anderson1d0be152009-08-13 21:58:54 +0000740 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
Devang Patele4b27562009-08-28 23:24:31 +0000741 MDString::get(VMContext, Filename),
742 MDString::get(VMContext, Directory),
743 MDString::get(VMContext, Producer),
Owen Anderson1d0be152009-08-13 21:58:54 +0000744 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
745 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
Devang Patele4b27562009-08-28 23:24:31 +0000746 MDString::get(VMContext, Flags),
Owen Anderson1d0be152009-08-13 21:58:54 +0000747 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
Chris Lattnera45664f2008-11-10 02:56:27 +0000748 };
Devang Patele4b27562009-08-28 23:24:31 +0000749
750 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000751}
752
Devang Patel7aa81892010-03-08 22:27:22 +0000753/// CreateFile - Create a new descriptor for the specified file.
754DIFile DIFactory::CreateFile(StringRef Filename,
755 StringRef Directory,
756 DICompileUnit CU) {
757 Value *Elts[] = {
758 GetTagConstant(dwarf::DW_TAG_file_type),
759 MDString::get(VMContext, Filename),
760 MDString::get(VMContext, Directory),
Devang Patel2db49d72010-05-07 18:11:54 +0000761 CU
Devang Patel7aa81892010-03-08 22:27:22 +0000762 };
763
764 return DIFile(MDNode::get(VMContext, &Elts[0], 4));
765}
766
Chris Lattnera45664f2008-11-10 02:56:27 +0000767/// CreateEnumerator - Create a single enumerator value.
Devang Patel65dbc902009-11-25 17:36:49 +0000768DIEnumerator DIFactory::CreateEnumerator(StringRef Name, uint64_t Val){
Devang Patele4b27562009-08-28 23:24:31 +0000769 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000770 GetTagConstant(dwarf::DW_TAG_enumerator),
Devang Patele4b27562009-08-28 23:24:31 +0000771 MDString::get(VMContext, Name),
Owen Anderson1d0be152009-08-13 21:58:54 +0000772 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
Chris Lattnera45664f2008-11-10 02:56:27 +0000773 };
Devang Patele4b27562009-08-28 23:24:31 +0000774 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000775}
776
777
778/// CreateBasicType - Create a basic type like int, float, etc.
779DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000780 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000781 DIFile F,
Chris Lattnera45664f2008-11-10 02:56:27 +0000782 unsigned LineNumber,
783 uint64_t SizeInBits,
784 uint64_t AlignInBits,
785 uint64_t OffsetInBits, unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000786 unsigned Encoding) {
Devang Patele4b27562009-08-28 23:24:31 +0000787 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000788 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patel2db49d72010-05-07 18:11:54 +0000789 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000790 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000791 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000792 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
793 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
794 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
795 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
796 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
797 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
Chris Lattnera45664f2008-11-10 02:56:27 +0000798 };
Devang Patele4b27562009-08-28 23:24:31 +0000799 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000800}
801
Devang Patelac16d442009-10-26 16:54:35 +0000802
803/// CreateBasicType - Create a basic type like int, float, etc.
804DIBasicType DIFactory::CreateBasicTypeEx(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000805 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000806 DIFile F,
Devang Patelac16d442009-10-26 16:54:35 +0000807 unsigned LineNumber,
808 Constant *SizeInBits,
809 Constant *AlignInBits,
810 Constant *OffsetInBits, unsigned Flags,
811 unsigned Encoding) {
812 Value *Elts[] = {
813 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patel2db49d72010-05-07 18:11:54 +0000814 Context,
Devang Patelac16d442009-10-26 16:54:35 +0000815 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000816 F,
Devang Patelac16d442009-10-26 16:54:35 +0000817 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
818 SizeInBits,
819 AlignInBits,
820 OffsetInBits,
821 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
822 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
823 };
824 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
825}
826
Devang Patelb4645642010-02-06 01:02:37 +0000827/// CreateArtificialType - Create a new DIType with "artificial" flag set.
828DIType DIFactory::CreateArtificialType(DIType Ty) {
829 if (Ty.isArtificial())
830 return Ty;
831
832 SmallVector<Value *, 9> Elts;
Devang Patel2db49d72010-05-07 18:11:54 +0000833 MDNode *N = Ty;
Devang Patelb4645642010-02-06 01:02:37 +0000834 assert (N && "Unexpected input DIType!");
835 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
836 if (Value *V = N->getOperand(i))
837 Elts.push_back(V);
838 else
839 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
840 }
841
842 unsigned CurFlags = Ty.getFlags();
843 CurFlags = CurFlags | DIType::FlagArtificial;
844
845 // Flags are stored at this slot.
846 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
847
848 return DIType(MDNode::get(VMContext, Elts.data(), Elts.size()));
849}
Devang Patelac16d442009-10-26 16:54:35 +0000850
Chris Lattnera45664f2008-11-10 02:56:27 +0000851/// CreateDerivedType - Create a derived type like const qualified type,
852/// pointer, typedef, etc.
853DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
854 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000855 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000856 DIFile F,
Chris Lattnera45664f2008-11-10 02:56:27 +0000857 unsigned LineNumber,
858 uint64_t SizeInBits,
859 uint64_t AlignInBits,
860 uint64_t OffsetInBits,
861 unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000862 DIType DerivedFrom) {
Devang Patele4b27562009-08-28 23:24:31 +0000863 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000864 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000865 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000866 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000867 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000868 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
869 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
870 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
871 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
872 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000873 DerivedFrom,
Chris Lattnera45664f2008-11-10 02:56:27 +0000874 };
Devang Patele4b27562009-08-28 23:24:31 +0000875 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000876}
877
Devang Patelac16d442009-10-26 16:54:35 +0000878
879/// CreateDerivedType - Create a derived type like const qualified type,
880/// pointer, typedef, etc.
881DIDerivedType DIFactory::CreateDerivedTypeEx(unsigned Tag,
882 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000883 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000884 DIFile F,
Devang Patelac16d442009-10-26 16:54:35 +0000885 unsigned LineNumber,
886 Constant *SizeInBits,
887 Constant *AlignInBits,
888 Constant *OffsetInBits,
889 unsigned Flags,
890 DIType DerivedFrom) {
891 Value *Elts[] = {
892 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000893 Context,
Devang Patelac16d442009-10-26 16:54:35 +0000894 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000895 F,
Devang Patelac16d442009-10-26 16:54:35 +0000896 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
897 SizeInBits,
898 AlignInBits,
899 OffsetInBits,
900 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000901 DerivedFrom,
Devang Patelac16d442009-10-26 16:54:35 +0000902 };
903 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
904}
905
906
Chris Lattnera45664f2008-11-10 02:56:27 +0000907/// CreateCompositeType - Create a composite type like array, struct, etc.
908DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
909 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000910 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000911 DIFile F,
Chris Lattnera45664f2008-11-10 02:56:27 +0000912 unsigned LineNumber,
913 uint64_t SizeInBits,
914 uint64_t AlignInBits,
915 uint64_t OffsetInBits,
916 unsigned Flags,
917 DIType DerivedFrom,
Devang Patel13319ce2009-02-17 22:43:44 +0000918 DIArray Elements,
Devang Patel0fd7f9d2010-01-26 21:14:59 +0000919 unsigned RuntimeLang,
920 MDNode *ContainingType) {
Owen Andersone277fed2009-07-07 16:31:25 +0000921
Devang Patele4b27562009-08-28 23:24:31 +0000922 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000923 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000924 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000925 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000926 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000927 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
928 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
929 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
930 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
931 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000932 DerivedFrom,
933 Elements,
Devang Patel0fd7f9d2010-01-26 21:14:59 +0000934 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
935 ContainingType
Chris Lattnera45664f2008-11-10 02:56:27 +0000936 };
Devang Patele7e5a0f2010-08-10 20:01:20 +0000937
938 MDNode *Node = MDNode::get(VMContext, &Elts[0], 13);
939 // Create a named metadata so that we do not lose this enum info.
940 if (Tag == dwarf::DW_TAG_enumeration_type) {
941 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.enum");
942 NMD->addOperand(Node);
943 }
944 return DICompositeType(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +0000945}
946
947
Dan Gohman489b29b2010-08-20 22:02:26 +0000948/// CreateTemporaryType - Create a temporary forward-declared type.
Dan Gohmana3833f12010-08-20 22:39:47 +0000949DIType DIFactory::CreateTemporaryType() {
Dan Gohman489b29b2010-08-20 22:02:26 +0000950 // Give the temporary MDNode a tag. It doesn't matter what tag we
951 // use here as long as DIType accepts it.
952 Value *Elts[] = {
953 GetTagConstant(DW_TAG_base_type)
954 };
955 MDNode *Node = MDNode::getTemporary(VMContext, Elts, array_lengthof(Elts));
956 return DIType(Node);
957}
958
959
Devang Patelac16d442009-10-26 16:54:35 +0000960/// CreateCompositeType - Create a composite type like array, struct, etc.
961DICompositeType DIFactory::CreateCompositeTypeEx(unsigned Tag,
962 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000963 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000964 DIFile F,
Devang Patelac16d442009-10-26 16:54:35 +0000965 unsigned LineNumber,
966 Constant *SizeInBits,
967 Constant *AlignInBits,
968 Constant *OffsetInBits,
969 unsigned Flags,
970 DIType DerivedFrom,
971 DIArray Elements,
Devang Patel6bf058c2010-08-10 20:22:49 +0000972 unsigned RuntimeLang,
973 MDNode *ContainingType) {
Devang Patelac16d442009-10-26 16:54:35 +0000974 Value *Elts[] = {
975 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000976 Context,
Devang Patelac16d442009-10-26 16:54:35 +0000977 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000978 F,
Devang Patelac16d442009-10-26 16:54:35 +0000979 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
980 SizeInBits,
981 AlignInBits,
982 OffsetInBits,
983 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000984 DerivedFrom,
985 Elements,
Devang Patel6bf058c2010-08-10 20:22:49 +0000986 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
987 ContainingType
Devang Patelac16d442009-10-26 16:54:35 +0000988 };
Devang Patel6bf058c2010-08-10 20:22:49 +0000989 MDNode *Node = MDNode::get(VMContext, &Elts[0], 13);
Devang Patele7e5a0f2010-08-10 20:01:20 +0000990 // Create a named metadata so that we do not lose this enum info.
991 if (Tag == dwarf::DW_TAG_enumeration_type) {
992 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.enum");
993 NMD->addOperand(Node);
994 }
995 return DICompositeType(Node);
Devang Patelac16d442009-10-26 16:54:35 +0000996}
997
998
Chris Lattnera45664f2008-11-10 02:56:27 +0000999/// CreateSubprogram - Create a new descriptor for the specified subprogram.
1000/// See comments in DISubprogram for descriptions of these fields. This
1001/// method does not unique the generated descriptors.
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001002DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +00001003 StringRef Name,
1004 StringRef DisplayName,
1005 StringRef LinkageName,
Devang Patel4b945502010-03-09 00:44:10 +00001006 DIFile F,
Devang Patel2e369932010-01-23 00:26:28 +00001007 unsigned LineNo, DIType Ty,
Chris Lattnera45664f2008-11-10 02:56:27 +00001008 bool isLocalToUnit,
Devang Patel5d11eb02009-12-03 19:11:07 +00001009 bool isDefinition,
1010 unsigned VK, unsigned VIndex,
Devang Patel4e0d19d2010-02-03 19:57:19 +00001011 DIType ContainingType,
Devang Patelccff8122010-04-30 19:38:23 +00001012 bool isArtificial,
Stuart Hastings215aa152010-06-11 20:08:44 +00001013 bool isOptimized,
1014 Function *Fn) {
Devang Patel854967e2008-12-17 22:39:29 +00001015
Devang Patele4b27562009-08-28 23:24:31 +00001016 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001017 GetTagConstant(dwarf::DW_TAG_subprogram),
Devang Patele4b27562009-08-28 23:24:31 +00001018 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Devang Patel2db49d72010-05-07 18:11:54 +00001019 Context,
Devang Patele4b27562009-08-28 23:24:31 +00001020 MDString::get(VMContext, Name),
1021 MDString::get(VMContext, DisplayName),
1022 MDString::get(VMContext, LinkageName),
Devang Patel2db49d72010-05-07 18:11:54 +00001023 F,
Owen Anderson1d0be152009-08-13 21:58:54 +00001024 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001025 Ty,
Owen Anderson1d0be152009-08-13 21:58:54 +00001026 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
Devang Patel5d11eb02009-12-03 19:11:07 +00001027 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1028 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
1029 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
Devang Patel2db49d72010-05-07 18:11:54 +00001030 ContainingType,
Devang Patelccff8122010-04-30 19:38:23 +00001031 ConstantInt::get(Type::getInt1Ty(VMContext), isArtificial),
Stuart Hastings215aa152010-06-11 20:08:44 +00001032 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
1033 Fn
Chris Lattnera45664f2008-11-10 02:56:27 +00001034 };
Devang Patelfd5fdc32010-06-28 05:53:08 +00001035 MDNode *Node = MDNode::get(VMContext, &Elts[0], 17);
1036
1037 // Create a named metadata so that we do not lose this mdnode.
1038 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
1039 NMD->addOperand(Node);
1040 return DISubprogram(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +00001041}
1042
Devang Patele3a18de2009-12-01 23:09:02 +00001043/// CreateSubprogramDefinition - Create new subprogram descriptor for the
Jim Grosbache62b6902010-07-21 21:36:25 +00001044/// given declaration.
1045DISubprogram DIFactory::CreateSubprogramDefinition(DISubprogram &SPDeclaration){
Devang Patele3a18de2009-12-01 23:09:02 +00001046 if (SPDeclaration.isDefinition())
Devang Patel2db49d72010-05-07 18:11:54 +00001047 return DISubprogram(SPDeclaration);
Devang Patele3a18de2009-12-01 23:09:02 +00001048
Devang Patel2db49d72010-05-07 18:11:54 +00001049 MDNode *DeclNode = SPDeclaration;
Devang Patele3a18de2009-12-01 23:09:02 +00001050 Value *Elts[] = {
1051 GetTagConstant(dwarf::DW_TAG_subprogram),
1052 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001053 DeclNode->getOperand(2), // Context
1054 DeclNode->getOperand(3), // Name
1055 DeclNode->getOperand(4), // DisplayName
1056 DeclNode->getOperand(5), // LinkageName
1057 DeclNode->getOperand(6), // CompileUnit
1058 DeclNode->getOperand(7), // LineNo
1059 DeclNode->getOperand(8), // Type
1060 DeclNode->getOperand(9), // isLocalToUnit
Stuart Hastings6d56b9f2010-06-05 00:39:29 +00001061 ConstantInt::get(Type::getInt1Ty(VMContext), true),
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001062 DeclNode->getOperand(11), // Virtuality
1063 DeclNode->getOperand(12), // VIndex
Devang Patel4e0d19d2010-02-03 19:57:19 +00001064 DeclNode->getOperand(13), // Containting Type
Devang Patelccff8122010-04-30 19:38:23 +00001065 DeclNode->getOperand(14), // isArtificial
Devang Patelacc6efa2010-06-27 21:04:31 +00001066 DeclNode->getOperand(15), // isOptimized
1067 SPDeclaration.getFunction()
Devang Patele3a18de2009-12-01 23:09:02 +00001068 };
Devang Patelfd5fdc32010-06-28 05:53:08 +00001069 MDNode *Node =MDNode::get(VMContext, &Elts[0], 16);
1070
1071 // Create a named metadata so that we do not lose this mdnode.
1072 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
1073 NMD->addOperand(Node);
1074 return DISubprogram(Node);
Devang Patele3a18de2009-12-01 23:09:02 +00001075}
1076
Chris Lattnera45664f2008-11-10 02:56:27 +00001077/// CreateGlobalVariable - Create a new descriptor for the specified global.
1078DIGlobalVariable
Devang Patel65dbc902009-11-25 17:36:49 +00001079DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
1080 StringRef DisplayName,
1081 StringRef LinkageName,
Devang Patel4b945502010-03-09 00:44:10 +00001082 DIFile F,
Devang Patel2e369932010-01-23 00:26:28 +00001083 unsigned LineNo, DIType Ty,bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +00001084 bool isDefinition, llvm::GlobalVariable *Val) {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001085 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001086 GetTagConstant(dwarf::DW_TAG_variable),
Devang Patele4b27562009-08-28 23:24:31 +00001087 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Devang Patel2db49d72010-05-07 18:11:54 +00001088 Context,
Devang Patele4b27562009-08-28 23:24:31 +00001089 MDString::get(VMContext, Name),
1090 MDString::get(VMContext, DisplayName),
1091 MDString::get(VMContext, LinkageName),
Devang Patel2db49d72010-05-07 18:11:54 +00001092 F,
Owen Anderson1d0be152009-08-13 21:58:54 +00001093 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001094 Ty,
Owen Anderson1d0be152009-08-13 21:58:54 +00001095 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1096 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
Devang Patele4b27562009-08-28 23:24:31 +00001097 Val
Chris Lattnera45664f2008-11-10 02:56:27 +00001098 };
Devang Patele4b27562009-08-28 23:24:31 +00001099
1100 Value *const *Vs = &Elts[0];
1101 MDNode *Node = MDNode::get(VMContext,Vs, 12);
1102
1103 // Create a named metadata so that we do not lose this mdnode.
1104 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001105 NMD->addOperand(Node);
Devang Patele4b27562009-08-28 23:24:31 +00001106
1107 return DIGlobalVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +00001108}
1109
Devang Patel27398962010-08-09 21:39:24 +00001110/// CreateGlobalVariable - Create a new descriptor for the specified constant.
1111DIGlobalVariable
1112DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
1113 StringRef DisplayName,
1114 StringRef LinkageName,
1115 DIFile F,
1116 unsigned LineNo, DIType Ty,bool isLocalToUnit,
1117 bool isDefinition, llvm::Constant *Val) {
1118 Value *Elts[] = {
Devang Patelebd53742010-08-11 23:17:54 +00001119 GetTagConstant(dwarf::DW_TAG_variable),
Devang Patel27398962010-08-09 21:39:24 +00001120 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
1121 Context,
1122 MDString::get(VMContext, Name),
1123 MDString::get(VMContext, DisplayName),
1124 MDString::get(VMContext, LinkageName),
1125 F,
1126 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1127 Ty,
1128 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1129 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1130 Val
1131 };
1132
1133 Value *const *Vs = &Elts[0];
1134 MDNode *Node = MDNode::get(VMContext,Vs, 12);
1135
1136 // Create a named metadata so that we do not lose this mdnode.
1137 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
1138 NMD->addOperand(Node);
1139
1140 return DIGlobalVariable(Node);
1141}
Chris Lattnera45664f2008-11-10 02:56:27 +00001142
1143/// CreateVariable - Create a new descriptor for the specified variable.
1144DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +00001145 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +00001146 DIFile F,
1147 unsigned LineNo,
Devang Patel6ed0ce32010-05-20 20:35:24 +00001148 DIType Ty, bool AlwaysPreserve) {
Devang Patele4b27562009-08-28 23:24:31 +00001149 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001150 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +00001151 Context,
Devang Patele4b27562009-08-28 23:24:31 +00001152 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +00001153 F,
Owen Anderson1d0be152009-08-13 21:58:54 +00001154 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001155 Ty,
Chris Lattnera45664f2008-11-10 02:56:27 +00001156 };
Devang Patel98e1cac2010-05-14 21:01:35 +00001157 MDNode *Node = MDNode::get(VMContext, &Elts[0], 6);
Devang Patel6ed0ce32010-05-20 20:35:24 +00001158 if (AlwaysPreserve) {
1159 // The optimizer may remove local variable. If there is an interest
1160 // to preserve variable info in such situation then stash it in a
1161 // named mdnode.
Devang Patel2f7d5292010-06-16 00:53:55 +00001162 DISubprogram Fn(getDISubprogram(Context));
Devang Patel10de3bb2010-06-21 18:36:58 +00001163 StringRef FName = "fn";
1164 if (Fn.getFunction())
1165 FName = Fn.getFunction()->getName();
Devang Patel10de3bb2010-06-21 18:36:58 +00001166 char One = '\1';
1167 if (FName.startswith(StringRef(&One, 1)))
1168 FName = FName.substr(1);
Dan Gohman17aa92c2010-07-21 23:38:33 +00001169
1170 SmallString<32> Out;
1171 NamedMDNode *FnLocals =
1172 M.getOrInsertNamedMetadata(Twine("llvm.dbg.lv.", FName).toStringRef(Out));
Devang Patel2f7d5292010-06-16 00:53:55 +00001173 FnLocals->addOperand(Node);
Devang Patel98e1cac2010-05-14 21:01:35 +00001174 }
1175 return DIVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +00001176}
1177
1178
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001179/// CreateComplexVariable - Create a new descriptor for the specified variable
1180/// which has a complex address expression for its address.
1181DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
Benjamin Kramer28b4afc2010-09-21 16:00:03 +00001182 StringRef Name, DIFile F,
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001183 unsigned LineNo,
Benjamin Kramer28b4afc2010-09-21 16:00:03 +00001184 DIType Ty, Value *const *Addr,
1185 unsigned NumAddr) {
1186 SmallVector<Value *, 15> Elts;
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001187 Elts.push_back(GetTagConstant(Tag));
Devang Patel2db49d72010-05-07 18:11:54 +00001188 Elts.push_back(Context);
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001189 Elts.push_back(MDString::get(VMContext, Name));
Devang Patel2db49d72010-05-07 18:11:54 +00001190 Elts.push_back(F);
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001191 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
Devang Patel2db49d72010-05-07 18:11:54 +00001192 Elts.push_back(Ty);
Benjamin Kramer28b4afc2010-09-21 16:00:03 +00001193 Elts.append(Addr, Addr+NumAddr);
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001194
Benjamin Kramer28b4afc2010-09-21 16:00:03 +00001195 return DIVariable(MDNode::get(VMContext, Elts.data(), Elts.size()));
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001196}
1197
1198
Chris Lattnera45664f2008-11-10 02:56:27 +00001199/// CreateBlock - This creates a descriptor for a lexical block with the
Owen Anderson99035272009-07-07 17:12:53 +00001200/// specified parent VMContext.
Devang Patel3d821aa2010-02-16 21:39:34 +00001201DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context,
Stuart Hastings0db42712010-07-19 23:56:30 +00001202 DIFile F, unsigned LineNo,
1203 unsigned Col) {
1204 // Defeat MDNode uniqing for lexical blocks.
1205 static unsigned int unique_id = 0;
Devang Patele4b27562009-08-28 23:24:31 +00001206 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001207 GetTagConstant(dwarf::DW_TAG_lexical_block),
Devang Patel2db49d72010-05-07 18:11:54 +00001208 Context,
Devang Patel3d821aa2010-02-16 21:39:34 +00001209 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Stuart Hastings0db42712010-07-19 23:56:30 +00001210 ConstantInt::get(Type::getInt32Ty(VMContext), Col),
1211 F,
1212 ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
Chris Lattnera45664f2008-11-10 02:56:27 +00001213 };
Stuart Hastings0db42712010-07-19 23:56:30 +00001214 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 6));
Chris Lattnera45664f2008-11-10 02:56:27 +00001215}
1216
Devang Patel6404e4e2009-12-15 19:16:48 +00001217/// CreateNameSpace - This creates new descriptor for a namespace
1218/// with the specified parent context.
1219DINameSpace DIFactory::CreateNameSpace(DIDescriptor Context, StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +00001220 DIFile F,
Devang Patel6404e4e2009-12-15 19:16:48 +00001221 unsigned LineNo) {
1222 Value *Elts[] = {
1223 GetTagConstant(dwarf::DW_TAG_namespace),
Devang Patel2db49d72010-05-07 18:11:54 +00001224 Context,
Devang Patel6404e4e2009-12-15 19:16:48 +00001225 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +00001226 F,
Devang Patel6404e4e2009-12-15 19:16:48 +00001227 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1228 };
1229 return DINameSpace(MDNode::get(VMContext, &Elts[0], 5));
1230}
1231
Devang Patelf98d8fe2009-09-01 01:14:15 +00001232/// CreateLocation - Creates a debug info location.
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001233DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
Daniel Dunbara279bc32009-09-20 02:20:51 +00001234 DIScope S, DILocation OrigLoc) {
Devang Patelf98d8fe2009-09-01 01:14:15 +00001235 Value *Elts[] = {
1236 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1237 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001238 S,
1239 OrigLoc,
Devang Patelf98d8fe2009-09-01 01:14:15 +00001240 };
1241 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1242}
1243
Chris Lattnera45664f2008-11-10 02:56:27 +00001244//===----------------------------------------------------------------------===//
1245// DIFactory: Routines for inserting code into a function
1246//===----------------------------------------------------------------------===//
1247
Chris Lattnera45664f2008-11-10 02:56:27 +00001248/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Devang Patel6daf99b2009-11-10 22:05:35 +00001249Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001250 Instruction *InsertBefore) {
Victor Hernandez4cf292a2010-01-26 02:07:38 +00001251 assert(Storage && "no storage passed to dbg.declare");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001252 assert(D.Verify() && "empty DIVariable passed to dbg.declare");
Chris Lattnera45664f2008-11-10 02:56:27 +00001253 if (!DeclareFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +00001254 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1255
Victor Hernandez756462b2010-01-18 20:42:09 +00001256 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
Devang Patel2db49d72010-05-07 18:11:54 +00001257 D };
Devang Patel6daf99b2009-11-10 22:05:35 +00001258 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
Mike Stumpe4250392009-10-01 22:08:58 +00001259}
1260
1261/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Devang Patel6daf99b2009-11-10 22:05:35 +00001262Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001263 BasicBlock *InsertAtEnd) {
Victor Hernandez4cf292a2010-01-26 02:07:38 +00001264 assert(Storage && "no storage passed to dbg.declare");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001265 assert(D.Verify() && "invalid DIVariable passed to dbg.declare");
Mike Stumpe4250392009-10-01 22:08:58 +00001266 if (!DeclareFn)
1267 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1268
Victor Hernandez756462b2010-01-18 20:42:09 +00001269 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
Devang Patel2db49d72010-05-07 18:11:54 +00001270 D };
Devang Patel27a53de2010-01-29 18:30:57 +00001271
1272 // If this block already has a terminator then insert this intrinsic
1273 // before the terminator.
Jim Grosbache62b6902010-07-21 21:36:25 +00001274 if (TerminatorInst *T = InsertAtEnd->getTerminator())
Devang Patel27a53de2010-01-29 18:30:57 +00001275 return CallInst::Create(DeclareFn, Args, Args+2, "", T);
1276 else
1277 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);}
Torok Edwin620f2802008-12-16 09:07:36 +00001278
Victor Hernandezc59b3352009-12-07 21:54:43 +00001279/// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001280Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
Victor Hernandezc59b3352009-12-07 21:54:43 +00001281 DIVariable D,
1282 Instruction *InsertBefore) {
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001283 assert(V && "no value passed to dbg.value");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001284 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001285 if (!ValueFn)
1286 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1287
Victor Hernandezc8b7cd02010-01-20 05:44:11 +00001288 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001289 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
Devang Patel2db49d72010-05-07 18:11:54 +00001290 D };
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001291 return CallInst::Create(ValueFn, Args, Args+3, "", InsertBefore);
1292}
1293
Victor Hernandezc59b3352009-12-07 21:54:43 +00001294/// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001295Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
Victor Hernandezc59b3352009-12-07 21:54:43 +00001296 DIVariable D,
1297 BasicBlock *InsertAtEnd) {
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001298 assert(V && "no value passed to dbg.value");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001299 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001300 if (!ValueFn)
1301 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1302
Jim Grosbache62b6902010-07-21 21:36:25 +00001303 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001304 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
Devang Patel2db49d72010-05-07 18:11:54 +00001305 D };
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001306 return CallInst::Create(ValueFn, Args, Args+3, "", InsertAtEnd);
1307}
Devang Patele4b27562009-08-28 23:24:31 +00001308
Devang Pateld2f79a12009-07-28 19:55:13 +00001309//===----------------------------------------------------------------------===//
Devang Patel98c65172009-07-30 18:25:15 +00001310// DebugInfoFinder implementations.
Devang Pateld2f79a12009-07-28 19:55:13 +00001311//===----------------------------------------------------------------------===//
1312
Devang Patel98c65172009-07-30 18:25:15 +00001313/// processModule - Process entire module and collect debug info.
1314void DebugInfoFinder::processModule(Module &M) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001315 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1316 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
1317 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
1318 ++BI) {
Devang Patel01c5ff62010-05-04 01:05:02 +00001319 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
Devang Patelb4d31302009-07-31 18:18:52 +00001320 processDeclare(DDI);
Jim Grosbache62b6902010-07-21 21:36:25 +00001321
Chris Lattner28a9bf62010-04-02 20:44:29 +00001322 DebugLoc Loc = BI->getDebugLoc();
1323 if (Loc.isUnknown())
1324 continue;
Jim Grosbache62b6902010-07-21 21:36:25 +00001325
Chris Lattner28a9bf62010-04-02 20:44:29 +00001326 LLVMContext &Ctx = BI->getContext();
1327 DIDescriptor Scope(Loc.getScope(Ctx));
Jim Grosbache62b6902010-07-21 21:36:25 +00001328
Chris Lattner28a9bf62010-04-02 20:44:29 +00001329 if (Scope.isCompileUnit())
Devang Patel2db49d72010-05-07 18:11:54 +00001330 addCompileUnit(DICompileUnit(Scope));
Chris Lattner28a9bf62010-04-02 20:44:29 +00001331 else if (Scope.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001332 processSubprogram(DISubprogram(Scope));
Chris Lattner28a9bf62010-04-02 20:44:29 +00001333 else if (Scope.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001334 processLexicalBlock(DILexicalBlock(Scope));
Jim Grosbache62b6902010-07-21 21:36:25 +00001335
Chris Lattner28a9bf62010-04-02 20:44:29 +00001336 if (MDNode *IA = Loc.getInlinedAt(Ctx))
1337 processLocation(DILocation(IA));
Devang Pateld2f79a12009-07-28 19:55:13 +00001338 }
Devang Patele4b27562009-08-28 23:24:31 +00001339
Devang Patelfd5fdc32010-06-28 05:53:08 +00001340 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
1341 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1342 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
1343 if (addGlobalVariable(DIG)) {
1344 addCompileUnit(DIG.getCompileUnit());
1345 processType(DIG.getType());
1346 }
Devang Pateld2f79a12009-07-28 19:55:13 +00001347 }
1348 }
Devang Patelfd5fdc32010-06-28 05:53:08 +00001349
1350 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
1351 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
1352 processSubprogram(DISubprogram(NMD->getOperand(i)));
Devang Pateld2f79a12009-07-28 19:55:13 +00001353}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001354
Devang Patel6daf99b2009-11-10 22:05:35 +00001355/// processLocation - Process DILocation.
1356void DebugInfoFinder::processLocation(DILocation Loc) {
Devang Patel3c91b052010-03-08 20:52:55 +00001357 if (!Loc.Verify()) return;
Devang Patel2db49d72010-05-07 18:11:54 +00001358 DIDescriptor S(Loc.getScope());
Devang Patel6daf99b2009-11-10 22:05:35 +00001359 if (S.isCompileUnit())
Devang Patel2db49d72010-05-07 18:11:54 +00001360 addCompileUnit(DICompileUnit(S));
Devang Patel6daf99b2009-11-10 22:05:35 +00001361 else if (S.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001362 processSubprogram(DISubprogram(S));
Devang Patel6daf99b2009-11-10 22:05:35 +00001363 else if (S.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001364 processLexicalBlock(DILexicalBlock(S));
Devang Patel6daf99b2009-11-10 22:05:35 +00001365 processLocation(Loc.getOrigLocation());
1366}
1367
Devang Patel98c65172009-07-30 18:25:15 +00001368/// processType - Process DIType.
1369void DebugInfoFinder::processType(DIType DT) {
Devang Patel72bcdb62009-08-10 22:09:58 +00001370 if (!addType(DT))
Devang Pateld2f79a12009-07-28 19:55:13 +00001371 return;
1372
1373 addCompileUnit(DT.getCompileUnit());
Devang Patel6ceea332009-08-31 18:49:10 +00001374 if (DT.isCompositeType()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001375 DICompositeType DCT(DT);
Devang Patel98c65172009-07-30 18:25:15 +00001376 processType(DCT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001377 DIArray DA = DCT.getTypeArray();
Devang Patel3c91b052010-03-08 20:52:55 +00001378 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1379 DIDescriptor D = DA.getElement(i);
1380 if (D.isType())
Devang Patel2db49d72010-05-07 18:11:54 +00001381 processType(DIType(D));
Devang Patel3c91b052010-03-08 20:52:55 +00001382 else if (D.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001383 processSubprogram(DISubprogram(D));
Devang Patel3c91b052010-03-08 20:52:55 +00001384 }
Devang Patel6ceea332009-08-31 18:49:10 +00001385 } else if (DT.isDerivedType()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001386 DIDerivedType DDT(DT);
Devang Patel3c91b052010-03-08 20:52:55 +00001387 processType(DDT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001388 }
1389}
1390
Devang Patelbeab41b2009-10-07 22:04:08 +00001391/// processLexicalBlock
1392void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
Devang Patelbeab41b2009-10-07 22:04:08 +00001393 DIScope Context = LB.getContext();
1394 if (Context.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001395 return processLexicalBlock(DILexicalBlock(Context));
Devang Patelbeab41b2009-10-07 22:04:08 +00001396 else
Devang Patel2db49d72010-05-07 18:11:54 +00001397 return processSubprogram(DISubprogram(Context));
Devang Patelbeab41b2009-10-07 22:04:08 +00001398}
1399
Devang Patel98c65172009-07-30 18:25:15 +00001400/// processSubprogram - Process DISubprogram.
1401void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001402 if (!addSubprogram(SP))
1403 return;
1404 addCompileUnit(SP.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +00001405 processType(SP.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +00001406}
1407
Devang Patelb4d31302009-07-31 18:18:52 +00001408/// processDeclare - Process DbgDeclareInst.
1409void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
Devang Patel3c91b052010-03-08 20:52:55 +00001410 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1411 if (!N) return;
1412
1413 DIDescriptor DV(N);
1414 if (!DV.isVariable())
Devang Patelb4d31302009-07-31 18:18:52 +00001415 return;
1416
Devang Patel2db49d72010-05-07 18:11:54 +00001417 if (!NodesSeen.insert(DV))
Devang Patelb4d31302009-07-31 18:18:52 +00001418 return;
1419
Devang Patel3c91b052010-03-08 20:52:55 +00001420 addCompileUnit(DIVariable(N).getCompileUnit());
1421 processType(DIVariable(N).getType());
Devang Patelb4d31302009-07-31 18:18:52 +00001422}
1423
Devang Patel72bcdb62009-08-10 22:09:58 +00001424/// addType - Add type into Tys.
1425bool DebugInfoFinder::addType(DIType DT) {
Devang Patel3c91b052010-03-08 20:52:55 +00001426 if (!DT.isValid())
Devang Patel72bcdb62009-08-10 22:09:58 +00001427 return false;
1428
Devang Patel2db49d72010-05-07 18:11:54 +00001429 if (!NodesSeen.insert(DT))
Devang Patel72bcdb62009-08-10 22:09:58 +00001430 return false;
1431
Devang Patel2db49d72010-05-07 18:11:54 +00001432 TYs.push_back(DT);
Devang Patel72bcdb62009-08-10 22:09:58 +00001433 return true;
1434}
1435
Devang Pateld2f79a12009-07-28 19:55:13 +00001436/// addCompileUnit - Add compile unit into CUs.
Devang Patel98c65172009-07-30 18:25:15 +00001437bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Patel3c91b052010-03-08 20:52:55 +00001438 if (!CU.Verify())
Devang Pateld2f79a12009-07-28 19:55:13 +00001439 return false;
1440
Devang Patel2db49d72010-05-07 18:11:54 +00001441 if (!NodesSeen.insert(CU))
Devang Pateld2f79a12009-07-28 19:55:13 +00001442 return false;
1443
Devang Patel2db49d72010-05-07 18:11:54 +00001444 CUs.push_back(CU);
Devang Pateld2f79a12009-07-28 19:55:13 +00001445 return true;
1446}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001447
Devang Pateld2f79a12009-07-28 19:55:13 +00001448/// addGlobalVariable - Add global variable into GVs.
Devang Patel98c65172009-07-30 18:25:15 +00001449bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Patel2db49d72010-05-07 18:11:54 +00001450 if (!DIDescriptor(DIG).isGlobalVariable())
Devang Pateld2f79a12009-07-28 19:55:13 +00001451 return false;
1452
Devang Patel2db49d72010-05-07 18:11:54 +00001453 if (!NodesSeen.insert(DIG))
Devang Pateld2f79a12009-07-28 19:55:13 +00001454 return false;
1455
Devang Patel2db49d72010-05-07 18:11:54 +00001456 GVs.push_back(DIG);
Devang Pateld2f79a12009-07-28 19:55:13 +00001457 return true;
1458}
1459
1460// addSubprogram - Add subprgoram into SPs.
Devang Patel98c65172009-07-30 18:25:15 +00001461bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Patel2db49d72010-05-07 18:11:54 +00001462 if (!DIDescriptor(SP).isSubprogram())
Devang Pateld2f79a12009-07-28 19:55:13 +00001463 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001464
Devang Patel2db49d72010-05-07 18:11:54 +00001465 if (!NodesSeen.insert(SP))
Devang Pateld2f79a12009-07-28 19:55:13 +00001466 return false;
1467
Devang Patel2db49d72010-05-07 18:11:54 +00001468 SPs.push_back(SP);
Devang Pateld2f79a12009-07-28 19:55:13 +00001469 return true;
1470}
1471
Victor Hernandez756462b2010-01-18 20:42:09 +00001472/// Find the debug info descriptor corresponding to this global variable.
1473static Value *findDbgGlobalDeclare(GlobalVariable *V) {
Chris Lattner099b7792009-12-29 09:22:47 +00001474 const Module *M = V->getParent();
1475 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1476 if (!NMD)
Torok Edwinff7d0e92009-03-10 13:41:26 +00001477 return 0;
Chris Lattner099b7792009-12-29 09:22:47 +00001478
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001479 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman872814a2010-07-21 18:54:18 +00001480 DIDescriptor DIG(cast<MDNode>(NMD->getOperand(i)));
Devang Patel3c91b052010-03-08 20:52:55 +00001481 if (!DIG.isGlobalVariable())
Chris Lattner099b7792009-12-29 09:22:47 +00001482 continue;
Devang Patel2db49d72010-05-07 18:11:54 +00001483 if (DIGlobalVariable(DIG).getGlobal() == V)
1484 return DIG;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001485 }
Chris Lattner099b7792009-12-29 09:22:47 +00001486 return 0;
1487}
Torok Edwinff7d0e92009-03-10 13:41:26 +00001488
Chris Lattner099b7792009-12-29 09:22:47 +00001489/// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
1490/// It looks through pointer casts too.
Victor Hernandez756462b2010-01-18 20:42:09 +00001491static const DbgDeclareInst *findDbgDeclare(const Value *V) {
Victor Hernandez3a328652010-01-15 19:04:09 +00001492 V = V->stripPointerCasts();
Jim Grosbache62b6902010-07-21 21:36:25 +00001493
Victor Hernandez3a328652010-01-15 19:04:09 +00001494 if (!isa<Instruction>(V) && !isa<Argument>(V))
Torok Edwin620f2802008-12-16 09:07:36 +00001495 return 0;
Jim Grosbache62b6902010-07-21 21:36:25 +00001496
Victor Hernandez3a328652010-01-15 19:04:09 +00001497 const Function *F = NULL;
1498 if (const Instruction *I = dyn_cast<Instruction>(V))
1499 F = I->getParent()->getParent();
1500 else if (const Argument *A = dyn_cast<Argument>(V))
1501 F = A->getParent();
Jim Grosbache62b6902010-07-21 21:36:25 +00001502
Victor Hernandez3a328652010-01-15 19:04:09 +00001503 for (Function::const_iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
1504 for (BasicBlock::const_iterator BI = (*FI).begin(), BE = (*FI).end();
1505 BI != BE; ++BI)
1506 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1507 if (DDI->getAddress() == V)
1508 return DDI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001509
Chris Lattner099b7792009-12-29 09:22:47 +00001510 return 0;
1511}
Bill Wendlingdc817b62009-05-14 18:26:15 +00001512
Chris Lattner099b7792009-12-29 09:22:47 +00001513bool llvm::getLocationInfo(const Value *V, std::string &DisplayName,
1514 std::string &Type, unsigned &LineNo,
1515 std::string &File, std::string &Dir) {
1516 DICompileUnit Unit;
1517 DIType TypeD;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001518
Chris Lattner099b7792009-12-29 09:22:47 +00001519 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1520 Value *DIGV = findDbgGlobalDeclare(GV);
1521 if (!DIGV) return false;
1522 DIGlobalVariable Var(cast<MDNode>(DIGV));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001523
Chris Lattner099b7792009-12-29 09:22:47 +00001524 StringRef D = Var.getDisplayName();
Devang Patel65dbc902009-11-25 17:36:49 +00001525 if (!D.empty())
Chris Lattner099b7792009-12-29 09:22:47 +00001526 DisplayName = D;
1527 LineNo = Var.getLineNumber();
1528 Unit = Var.getCompileUnit();
1529 TypeD = Var.getType();
1530 } else {
1531 const DbgDeclareInst *DDI = findDbgDeclare(V);
1532 if (!DDI) return false;
1533 DIVariable Var(cast<MDNode>(DDI->getVariable()));
1534
1535 StringRef D = Var.getName();
1536 if (!D.empty())
1537 DisplayName = D;
1538 LineNo = Var.getLineNumber();
1539 Unit = Var.getCompileUnit();
1540 TypeD = Var.getType();
Torok Edwinff7d0e92009-03-10 13:41:26 +00001541 }
Devang Patel13e16b62009-06-26 01:49:18 +00001542
Chris Lattner099b7792009-12-29 09:22:47 +00001543 StringRef T = TypeD.getName();
1544 if (!T.empty())
1545 Type = T;
1546 StringRef F = Unit.getFilename();
1547 if (!F.empty())
1548 File = F;
1549 StringRef D = Unit.getDirectory();
1550 if (!D.empty())
1551 Dir = D;
1552 return true;
1553}
Devang Patel9e529c32009-07-02 01:15:24 +00001554
Chris Lattner099b7792009-12-29 09:22:47 +00001555/// getDISubprogram - Find subprogram that is enclosing this scope.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001556DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
Chris Lattner099b7792009-12-29 09:22:47 +00001557 DIDescriptor D(Scope);
Chris Lattner099b7792009-12-29 09:22:47 +00001558 if (D.isSubprogram())
1559 return DISubprogram(Scope);
Jim Grosbache62b6902010-07-21 21:36:25 +00001560
Chris Lattner099b7792009-12-29 09:22:47 +00001561 if (D.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001562 return getDISubprogram(DILexicalBlock(Scope).getContext());
Jim Grosbache62b6902010-07-21 21:36:25 +00001563
Chris Lattner099b7792009-12-29 09:22:47 +00001564 return DISubprogram();
1565}
Devang Patel193f7202009-11-24 01:14:22 +00001566
Chris Lattner099b7792009-12-29 09:22:47 +00001567/// getDICompositeType - Find underlying composite type.
1568DICompositeType llvm::getDICompositeType(DIType T) {
Chris Lattner099b7792009-12-29 09:22:47 +00001569 if (T.isCompositeType())
Devang Patel2db49d72010-05-07 18:11:54 +00001570 return DICompositeType(T);
Jim Grosbache62b6902010-07-21 21:36:25 +00001571
Chris Lattner099b7792009-12-29 09:22:47 +00001572 if (T.isDerivedType())
Devang Patel2db49d72010-05-07 18:11:54 +00001573 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
Jim Grosbache62b6902010-07-21 21:36:25 +00001574
Chris Lattner099b7792009-12-29 09:22:47 +00001575 return DICompositeType();
Torok Edwin620f2802008-12-16 09:07:36 +00001576}