blob: 1aaa60ebd268c110301e696d54d94c2388f163d2 [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 {
Devang Patel3cf763d2010-09-29 23:07:21 +0000112 if (getVersion() <= llvm::LLVMDebugVersion8)
113 return DbgNode->getNumOperands()-6;
114 return DbgNode->getNumOperands()-7;
Chris Lattnerf0908a32009-12-31 03:02:08 +0000115}
116
117
Chris Lattnera45664f2008-11-10 02:56:27 +0000118//===----------------------------------------------------------------------===//
Devang Patel6ceea332009-08-31 18:49:10 +0000119// Predicates
Chris Lattnera45664f2008-11-10 02:56:27 +0000120//===----------------------------------------------------------------------===//
121
Devang Patel6ceea332009-08-31 18:49:10 +0000122/// isBasicType - Return true if the specified tag is legal for
123/// DIBasicType.
124bool DIDescriptor::isBasicType() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000125 return DbgNode && getTag() == dwarf::DW_TAG_base_type;
Torok Edwinb07fbd92008-12-13 08:25:29 +0000126}
Chris Lattnera45664f2008-11-10 02:56:27 +0000127
Devang Patel6ceea332009-08-31 18:49:10 +0000128/// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
129bool DIDescriptor::isDerivedType() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000130 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000131 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000132 case dwarf::DW_TAG_typedef:
133 case dwarf::DW_TAG_pointer_type:
134 case dwarf::DW_TAG_reference_type:
135 case dwarf::DW_TAG_const_type:
136 case dwarf::DW_TAG_volatile_type:
137 case dwarf::DW_TAG_restrict_type:
138 case dwarf::DW_TAG_member:
139 case dwarf::DW_TAG_inheritance:
Devang Patel49d96382010-08-23 23:16:25 +0000140 case dwarf::DW_TAG_friend:
Chris Lattnera45664f2008-11-10 02:56:27 +0000141 return true;
142 default:
Devang Patele4b27562009-08-28 23:24:31 +0000143 // CompositeTypes are currently modelled as DerivedTypes.
Devang Patel6ceea332009-08-31 18:49:10 +0000144 return isCompositeType();
Chris Lattnera45664f2008-11-10 02:56:27 +0000145 }
146}
147
Chris Lattnera45664f2008-11-10 02:56:27 +0000148/// isCompositeType - Return true if the specified tag is legal for
149/// DICompositeType.
Devang Patel6ceea332009-08-31 18:49:10 +0000150bool DIDescriptor::isCompositeType() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000151 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000152 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000153 case dwarf::DW_TAG_array_type:
154 case dwarf::DW_TAG_structure_type:
155 case dwarf::DW_TAG_union_type:
156 case dwarf::DW_TAG_enumeration_type:
157 case dwarf::DW_TAG_vector_type:
158 case dwarf::DW_TAG_subroutine_type:
Devang Patel25cb0d72009-03-25 03:52:06 +0000159 case dwarf::DW_TAG_class_type:
Chris Lattnera45664f2008-11-10 02:56:27 +0000160 return true;
161 default:
162 return false;
163 }
164}
165
Chris Lattnera45664f2008-11-10 02:56:27 +0000166/// isVariable - Return true if the specified tag is legal for DIVariable.
Devang Patel6ceea332009-08-31 18:49:10 +0000167bool DIDescriptor::isVariable() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000168 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000169 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000170 case dwarf::DW_TAG_auto_variable:
171 case dwarf::DW_TAG_arg_variable:
172 case dwarf::DW_TAG_return_variable:
173 return true;
174 default:
175 return false;
176 }
177}
178
Devang Patelecbeb1a2009-09-30 22:34:41 +0000179/// isType - Return true if the specified tag is legal for DIType.
180bool DIDescriptor::isType() const {
181 return isBasicType() || isCompositeType() || isDerivedType();
182}
183
Devang Patel6ceea332009-08-31 18:49:10 +0000184/// isSubprogram - Return true if the specified tag is legal for
185/// DISubprogram.
186bool DIDescriptor::isSubprogram() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000187 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
Devang Patel6ceea332009-08-31 18:49:10 +0000188}
189
190/// isGlobalVariable - Return true if the specified tag is legal for
191/// DIGlobalVariable.
192bool DIDescriptor::isGlobalVariable() const {
Devang Patel29368072010-08-10 07:11:13 +0000193 return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
194 getTag() == dwarf::DW_TAG_constant);
Devang Patel6ceea332009-08-31 18:49:10 +0000195}
196
Devang Patelecbeb1a2009-09-30 22:34:41 +0000197/// isGlobal - Return true if the specified tag is legal for DIGlobal.
198bool DIDescriptor::isGlobal() const {
199 return isGlobalVariable();
200}
201
Devang Pateld6747df2010-10-06 20:50:40 +0000202/// isUnspecifiedParmeter - Return true if the specified tab is
203/// DW_TAG_unspecified_parameters.
204bool DIDescriptor::isUnspecifiedParameter() const {
205 return DbgNode && getTag() == dwarf::DW_TAG_unspecified_parameters;
206}
207
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000208/// isScope - Return true if the specified tag is one of the scope
Devang Patel43d98b32009-08-31 20:44:45 +0000209/// related tag.
210bool DIDescriptor::isScope() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000211 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000212 switch (getTag()) {
213 case dwarf::DW_TAG_compile_unit:
214 case dwarf::DW_TAG_lexical_block:
215 case dwarf::DW_TAG_subprogram:
216 case dwarf::DW_TAG_namespace:
217 return true;
218 default:
219 break;
Devang Patel43d98b32009-08-31 20:44:45 +0000220 }
221 return false;
222}
Devang Patel6ceea332009-08-31 18:49:10 +0000223
Devang Patel7e2cb112011-02-02 21:38:25 +0000224/// isTemplateTypeParameter - Return true if the specified tag is
225/// DW_TAG_template_type_parameter.
226bool DIDescriptor::isTemplateTypeParameter() const {
227 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
228}
229
Devang Patelc9f322d2009-08-31 21:34:44 +0000230/// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
231bool DIDescriptor::isCompileUnit() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000232 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
Devang Patelc9f322d2009-08-31 21:34:44 +0000233}
234
Devang Patel7aa81892010-03-08 22:27:22 +0000235/// isFile - Return true if the specified tag is DW_TAG_file_type.
236bool DIDescriptor::isFile() const {
237 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
238}
239
Devang Patel6404e4e2009-12-15 19:16:48 +0000240/// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
241bool DIDescriptor::isNameSpace() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000242 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
Devang Patel6404e4e2009-12-15 19:16:48 +0000243}
244
Devang Patel5e005d82009-08-31 22:00:15 +0000245/// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
246bool DIDescriptor::isLexicalBlock() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000247 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block;
Devang Patel5e005d82009-08-31 22:00:15 +0000248}
249
Devang Patelecbeb1a2009-09-30 22:34:41 +0000250/// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
251bool DIDescriptor::isSubrange() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000252 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
Devang Patelecbeb1a2009-09-30 22:34:41 +0000253}
254
255/// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
256bool DIDescriptor::isEnumerator() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000257 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
Devang Patelecbeb1a2009-09-30 22:34:41 +0000258}
259
Devang Patel6ceea332009-08-31 18:49:10 +0000260//===----------------------------------------------------------------------===//
261// Simple Descriptor Constructors and other Methods
262//===----------------------------------------------------------------------===//
263
Devang Patele9f8f5e2010-05-07 20:54:48 +0000264DIType::DIType(const MDNode *N) : DIScope(N) {
Devang Patel6ceea332009-08-31 18:49:10 +0000265 if (!N) return;
266 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
267 DbgNode = 0;
268 }
269}
270
Devang Patel68afdc32009-01-05 18:33:01 +0000271unsigned DIArray::getNumElements() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000272 if (!DbgNode)
273 return 0;
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000274 return DbgNode->getNumOperands();
Devang Patel68afdc32009-01-05 18:33:01 +0000275}
Chris Lattnera45664f2008-11-10 02:56:27 +0000276
Devang Patelc4999d72009-07-22 18:23:44 +0000277/// replaceAllUsesWith - Replace all uses of debug info referenced by
Dan Gohman872814a2010-07-21 18:54:18 +0000278/// this descriptor.
Dan Gohman489b29b2010-08-20 22:02:26 +0000279void DIType::replaceAllUsesWith(DIDescriptor &D) {
Devang Patel3c91b052010-03-08 20:52:55 +0000280 if (!DbgNode)
Devang Patelc4999d72009-07-22 18:23:44 +0000281 return;
282
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000283 // Since we use a TrackingVH for the node, its easy for clients to manufacture
284 // legitimate situations where they want to replaceAllUsesWith() on something
285 // which, due to uniquing, has merged with the source. We shield clients from
286 // this detail by allowing a value to be replaced with replaceAllUsesWith()
287 // itself.
Devang Pateled66bf52010-05-07 18:19:32 +0000288 if (DbgNode != D) {
Devang Patele9f8f5e2010-05-07 20:54:48 +0000289 MDNode *Node = const_cast<MDNode*>(DbgNode);
290 const MDNode *DN = D;
291 const Value *V = cast_or_null<Value>(DN);
292 Node->replaceAllUsesWith(const_cast<Value*>(V));
Dan Gohman489b29b2010-08-20 22:02:26 +0000293 MDNode::deleteTemporary(Node);
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000294 }
Devang Patelc4999d72009-07-22 18:23:44 +0000295}
296
Devang Patel0a2551d2010-12-08 20:18:20 +0000297/// replaceAllUsesWith - Replace all uses of debug info referenced by
298/// this descriptor.
299void DIType::replaceAllUsesWith(MDNode *D) {
300 if (!DbgNode)
301 return;
302
303 // Since we use a TrackingVH for the node, its easy for clients to manufacture
304 // legitimate situations where they want to replaceAllUsesWith() on something
305 // which, due to uniquing, has merged with the source. We shield clients from
306 // this detail by allowing a value to be replaced with replaceAllUsesWith()
307 // itself.
308 if (DbgNode != D) {
309 MDNode *Node = const_cast<MDNode*>(DbgNode);
310 const MDNode *DN = D;
311 const Value *V = cast_or_null<Value>(DN);
312 Node->replaceAllUsesWith(const_cast<Value*>(V));
313 MDNode::deleteTemporary(Node);
314 }
315}
316
Devang Patelb79b5352009-01-19 23:21:49 +0000317/// Verify - Verify that a compile unit is well formed.
318bool DICompileUnit::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000319 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000320 return false;
Devang Patel65dbc902009-11-25 17:36:49 +0000321 StringRef N = getFilename();
322 if (N.empty())
Bill Wendling0582ae92009-03-13 04:39:26 +0000323 return false;
Devang Patelb79b5352009-01-19 23:21:49 +0000324 // It is possible that directory and produce string is empty.
Bill Wendling0582ae92009-03-13 04:39:26 +0000325 return true;
Devang Patelb79b5352009-01-19 23:21:49 +0000326}
327
328/// Verify - Verify that a type descriptor is well formed.
329bool DIType::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000330 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000331 return false;
Devang Patel3c91b052010-03-08 20:52:55 +0000332 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000333 return false;
Devang Patelb71bbf92010-11-02 20:41:13 +0000334 unsigned Tag = getTag();
335 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
336 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
Devang Patelfe58f952010-12-07 23:25:47 +0000337 Tag != dwarf::DW_TAG_reference_type && Tag != dwarf::DW_TAG_restrict_type
Devang Patel43c249c2010-12-08 01:50:15 +0000338 && Tag != dwarf::DW_TAG_vector_type && Tag != dwarf::DW_TAG_array_type
339 && Tag != dwarf::DW_TAG_enumeration_type
Devang Patelfe58f952010-12-07 23:25:47 +0000340 && getFilename().empty())
Devang Patelb79b5352009-01-19 23:21:49 +0000341 return false;
342 return true;
343}
344
Devang Patel0c4720c2010-08-23 18:25:56 +0000345/// Verify - Verify that a basic type descriptor is well formed.
346bool DIBasicType::Verify() const {
347 return isBasicType();
348}
349
350/// Verify - Verify that a derived type descriptor is well formed.
351bool DIDerivedType::Verify() const {
352 return isDerivedType();
353}
354
Devang Patelb79b5352009-01-19 23:21:49 +0000355/// Verify - Verify that a composite type descriptor is well formed.
356bool DICompositeType::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000357 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000358 return false;
Devang Patel3c91b052010-03-08 20:52:55 +0000359 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000360 return false;
361
362 DICompileUnit CU = getCompileUnit();
Devang Patel3c91b052010-03-08 20:52:55 +0000363 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000364 return false;
365 return true;
366}
367
368/// Verify - Verify that a subprogram descriptor is well formed.
369bool DISubprogram::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000370 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000371 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000372
Devang Patel3c91b052010-03-08 20:52:55 +0000373 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000374 return false;
375
376 DICompileUnit CU = getCompileUnit();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000377 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000378 return false;
379
380 DICompositeType Ty = getType();
Devang Patel3c91b052010-03-08 20:52:55 +0000381 if (!Ty.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000382 return false;
383 return true;
384}
385
386/// Verify - Verify that a global variable descriptor is well formed.
387bool DIGlobalVariable::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000388 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000389 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000390
Devang Patel65dbc902009-11-25 17:36:49 +0000391 if (getDisplayName().empty())
Devang Patel84c73e92009-11-06 17:58:12 +0000392 return false;
393
Devang Patel3c91b052010-03-08 20:52:55 +0000394 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000395 return false;
396
397 DICompileUnit CU = getCompileUnit();
Devang Patel3c91b052010-03-08 20:52:55 +0000398 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000399 return false;
400
401 DIType Ty = getType();
402 if (!Ty.Verify())
403 return false;
404
Devang Patel27398962010-08-09 21:39:24 +0000405 if (!getGlobal() && !getConstant())
Devang Patelb79b5352009-01-19 23:21:49 +0000406 return false;
407
408 return true;
409}
410
411/// Verify - Verify that a variable descriptor is well formed.
412bool DIVariable::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000413 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000414 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000415
Devang Patel3c91b052010-03-08 20:52:55 +0000416 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000417 return false;
418
Devang Patel62077af2010-05-07 21:42:24 +0000419 if (!getCompileUnit().Verify())
420 return false;
421
Devang Patelb79b5352009-01-19 23:21:49 +0000422 DIType Ty = getType();
423 if (!Ty.Verify())
424 return false;
425
Devang Patelb79b5352009-01-19 23:21:49 +0000426 return true;
427}
428
Devang Patel3c91b052010-03-08 20:52:55 +0000429/// Verify - Verify that a location descriptor is well formed.
430bool DILocation::Verify() const {
431 if (!DbgNode)
432 return false;
Jim Grosbache62b6902010-07-21 21:36:25 +0000433
Devang Patel3c91b052010-03-08 20:52:55 +0000434 return DbgNode->getNumOperands() == 4;
435}
436
Devang Patel47e22652010-05-07 23:04:32 +0000437/// Verify - Verify that a namespace descriptor is well formed.
438bool DINameSpace::Verify() const {
439 if (!DbgNode)
440 return false;
441 if (getName().empty())
442 return false;
443 if (!getCompileUnit().Verify())
444 return false;
445 return true;
446}
447
Devang Patel36375ee2009-02-17 21:23:59 +0000448/// getOriginalTypeSize - If this type is derived from a base type then
449/// return base type size.
450uint64_t DIDerivedType::getOriginalTypeSize() const {
Devang Patel61ecbd12009-11-04 23:48:00 +0000451 unsigned Tag = getTag();
452 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
453 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
454 Tag == dwarf::DW_TAG_restrict_type) {
455 DIType BaseType = getTypeDerivedFrom();
Jim Grosbache62b6902010-07-21 21:36:25 +0000456 // If this type is not derived from any type then take conservative
Devang Patel5ebfa2d2009-11-06 18:24:05 +0000457 // approach.
Devang Patel3c91b052010-03-08 20:52:55 +0000458 if (!BaseType.isValid())
Devang Patel5ebfa2d2009-11-06 18:24:05 +0000459 return getSizeInBits();
Devang Patel61ecbd12009-11-04 23:48:00 +0000460 if (BaseType.isDerivedType())
Devang Patel2db49d72010-05-07 18:11:54 +0000461 return DIDerivedType(BaseType).getOriginalTypeSize();
Devang Patel61ecbd12009-11-04 23:48:00 +0000462 else
463 return BaseType.getSizeInBits();
464 }
Jim Grosbache62b6902010-07-21 21:36:25 +0000465
Devang Patel61ecbd12009-11-04 23:48:00 +0000466 return getSizeInBits();
Devang Patel36375ee2009-02-17 21:23:59 +0000467}
Devang Patelb79b5352009-01-19 23:21:49 +0000468
Jim Grosbache62b6902010-07-21 21:36:25 +0000469/// isInlinedFnArgument - Return true if this variable provides debugging
Devang Patel719f6a92010-04-29 20:40:36 +0000470/// information for an inlined function arguments.
471bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
472 assert(CurFn && "Invalid function");
473 if (!getContext().isSubprogram())
474 return false;
Jim Grosbache62b6902010-07-21 21:36:25 +0000475 // This variable is not inlined function argument if its scope
Devang Patel719f6a92010-04-29 20:40:36 +0000476 // does not describe current function.
Devang Patel2db49d72010-05-07 18:11:54 +0000477 return !(DISubprogram(getContext()).describes(CurFn));
Devang Patel719f6a92010-04-29 20:40:36 +0000478}
479
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000480/// describes - Return true if this subprogram provides debugging
481/// information for the function F.
482bool DISubprogram::describes(const Function *F) {
Chris Lattner099b7792009-12-29 09:22:47 +0000483 assert(F && "Invalid function");
Devang Patelffd33cd2010-06-16 06:42:02 +0000484 if (F == getFunction())
485 return true;
Devang Patel65dbc902009-11-25 17:36:49 +0000486 StringRef Name = getLinkageName();
487 if (Name.empty())
Devang Patel5ccdd102009-09-29 18:40:58 +0000488 Name = getName();
Devang Patel65dbc902009-11-25 17:36:49 +0000489 if (F->getName() == Name)
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000490 return true;
491 return false;
492}
493
Jim Grosbache62b6902010-07-21 21:36:25 +0000494unsigned DISubprogram::isOptimized() const {
Devang Patelccff8122010-04-30 19:38:23 +0000495 assert (DbgNode && "Invalid subprogram descriptor!");
496 if (DbgNode->getNumOperands() == 16)
497 return getUnsignedField(15);
498 return 0;
499}
500
Devang Patel65dbc902009-11-25 17:36:49 +0000501StringRef DIScope::getFilename() const {
Devang Patel77bf2952010-03-08 22:02:50 +0000502 if (!DbgNode)
503 return StringRef();
Jim Grosbache62b6902010-07-21 21:36:25 +0000504 if (isLexicalBlock())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000505 return DILexicalBlock(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000506 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000507 return DISubprogram(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000508 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000509 return DICompileUnit(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000510 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000511 return DINameSpace(DbgNode).getFilename();
Devang Patel77bf2952010-03-08 22:02:50 +0000512 if (isType())
513 return DIType(DbgNode).getFilename();
Devang Patel7aa81892010-03-08 22:27:22 +0000514 if (isFile())
515 return DIFile(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000516 assert(0 && "Invalid DIScope!");
Devang Patel65dbc902009-11-25 17:36:49 +0000517 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000518}
519
Devang Patel65dbc902009-11-25 17:36:49 +0000520StringRef DIScope::getDirectory() const {
Devang Patel77bf2952010-03-08 22:02:50 +0000521 if (!DbgNode)
522 return StringRef();
Jim Grosbache62b6902010-07-21 21:36:25 +0000523 if (isLexicalBlock())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000524 return DILexicalBlock(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000525 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000526 return DISubprogram(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000527 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000528 return DICompileUnit(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000529 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000530 return DINameSpace(DbgNode).getDirectory();
Devang Patel77bf2952010-03-08 22:02:50 +0000531 if (isType())
532 return DIType(DbgNode).getDirectory();
Devang Patel7aa81892010-03-08 22:27:22 +0000533 if (isFile())
534 return DIFile(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000535 assert(0 && "Invalid DIScope!");
Devang Patel65dbc902009-11-25 17:36:49 +0000536 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000537}
538
Chris Lattnera45664f2008-11-10 02:56:27 +0000539//===----------------------------------------------------------------------===//
Devang Patel7136a652009-07-01 22:10:23 +0000540// DIDescriptor: dump routines for all descriptors.
541//===----------------------------------------------------------------------===//
542
543
Dan Gohman50404362010-05-07 15:30:29 +0000544/// print - Print descriptor.
545void DIDescriptor::print(raw_ostream &OS) const {
546 OS << "[" << dwarf::TagString(getTag()) << "] ";
547 OS.write_hex((intptr_t) &*DbgNode) << ']';
Devang Patel7136a652009-07-01 22:10:23 +0000548}
549
Dan Gohman50404362010-05-07 15:30:29 +0000550/// print - Print compile unit.
551void DICompileUnit::print(raw_ostream &OS) const {
Devang Patel7136a652009-07-01 22:10:23 +0000552 if (getLanguage())
Dan Gohman50404362010-05-07 15:30:29 +0000553 OS << " [" << dwarf::LanguageString(getLanguage()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000554
Dan Gohmanfaa19c32010-05-10 20:07:44 +0000555 OS << " [" << getDirectory() << "/" << getFilename() << "]";
Devang Patel7136a652009-07-01 22:10:23 +0000556}
557
Dan Gohman50404362010-05-07 15:30:29 +0000558/// print - Print type.
559void DIType::print(raw_ostream &OS) const {
Devang Patel3c91b052010-03-08 20:52:55 +0000560 if (!DbgNode) return;
Devang Patel7136a652009-07-01 22:10:23 +0000561
Devang Patel65dbc902009-11-25 17:36:49 +0000562 StringRef Res = getName();
563 if (!Res.empty())
Dan Gohman50404362010-05-07 15:30:29 +0000564 OS << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000565
566 unsigned Tag = getTag();
Dan Gohman50404362010-05-07 15:30:29 +0000567 OS << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000568
569 // TODO : Print context
Dan Gohmanc014d092010-05-07 16:17:22 +0000570 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000571 OS << " ["
Dan Gohman9a7063e2010-05-07 16:39:27 +0000572 << "line " << getLineNumber() << ", "
573 << getSizeInBits() << " bits, "
574 << getAlignInBits() << " bit alignment, "
575 << getOffsetInBits() << " bit offset"
Chris Lattnera81d29b2009-08-23 07:33:14 +0000576 << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000577
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000578 if (isPrivate())
Dan Gohman50404362010-05-07 15:30:29 +0000579 OS << " [private] ";
Devang Patel7136a652009-07-01 22:10:23 +0000580 else if (isProtected())
Dan Gohman50404362010-05-07 15:30:29 +0000581 OS << " [protected] ";
Devang Patel7136a652009-07-01 22:10:23 +0000582
583 if (isForwardDecl())
Dan Gohman50404362010-05-07 15:30:29 +0000584 OS << " [fwd] ";
Devang Patel7136a652009-07-01 22:10:23 +0000585
Devang Patel6ceea332009-08-31 18:49:10 +0000586 if (isBasicType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000587 DIBasicType(DbgNode).print(OS);
Devang Patel6ceea332009-08-31 18:49:10 +0000588 else if (isDerivedType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000589 DIDerivedType(DbgNode).print(OS);
Devang Patel6ceea332009-08-31 18:49:10 +0000590 else if (isCompositeType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000591 DICompositeType(DbgNode).print(OS);
Devang Patel7136a652009-07-01 22:10:23 +0000592 else {
Dan Gohman50404362010-05-07 15:30:29 +0000593 OS << "Invalid DIType\n";
Devang Patel7136a652009-07-01 22:10:23 +0000594 return;
595 }
596
Dan Gohman50404362010-05-07 15:30:29 +0000597 OS << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000598}
599
Dan Gohman50404362010-05-07 15:30:29 +0000600/// print - Print basic type.
601void DIBasicType::print(raw_ostream &OS) const {
602 OS << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000603}
604
Dan Gohman50404362010-05-07 15:30:29 +0000605/// print - Print derived type.
606void DIDerivedType::print(raw_ostream &OS) const {
Dan Gohmanc014d092010-05-07 16:17:22 +0000607 OS << "\n\t Derived From: "; getTypeDerivedFrom().print(OS);
Devang Patel7136a652009-07-01 22:10:23 +0000608}
609
Dan Gohman50404362010-05-07 15:30:29 +0000610/// print - Print composite type.
611void DICompositeType::print(raw_ostream &OS) const {
Devang Patel7136a652009-07-01 22:10:23 +0000612 DIArray A = getTypeArray();
Dan Gohman50404362010-05-07 15:30:29 +0000613 OS << " [" << A.getNumElements() << " elements]";
Devang Patel7136a652009-07-01 22:10:23 +0000614}
615
Dan Gohman50404362010-05-07 15:30:29 +0000616/// print - Print subprogram.
617void DISubprogram::print(raw_ostream &OS) const {
Devang Patel65dbc902009-11-25 17:36:49 +0000618 StringRef Res = getName();
619 if (!Res.empty())
Dan Gohman50404362010-05-07 15:30:29 +0000620 OS << " [" << Res << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000621
622 unsigned Tag = getTag();
Dan Gohman50404362010-05-07 15:30:29 +0000623 OS << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000624
625 // TODO : Print context
Dan Gohmanc014d092010-05-07 16:17:22 +0000626 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000627 OS << " [" << getLineNumber() << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000628
629 if (isLocalToUnit())
Dan Gohman50404362010-05-07 15:30:29 +0000630 OS << " [local] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000631
632 if (isDefinition())
Dan Gohman50404362010-05-07 15:30:29 +0000633 OS << " [def] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000634
Dan Gohman50404362010-05-07 15:30:29 +0000635 OS << "\n";
636}
637
638/// print - Print global variable.
639void DIGlobalVariable::print(raw_ostream &OS) const {
640 OS << " [";
Devang Patela49d8772010-05-07 23:19:07 +0000641 StringRef Res = getName();
642 if (!Res.empty())
643 OS << " [" << Res << "] ";
644
645 unsigned Tag = getTag();
646 OS << " [" << dwarf::TagString(Tag) << "] ";
647
648 // TODO : Print context
649 getCompileUnit().print(OS);
650 OS << " [" << getLineNumber() << "] ";
651
652 if (isLocalToUnit())
653 OS << " [local] ";
654
655 if (isDefinition())
656 OS << " [def] ";
657
658 if (isGlobalVariable())
659 DIGlobalVariable(DbgNode).print(OS);
660 OS << "]\n";
Dan Gohman50404362010-05-07 15:30:29 +0000661}
662
663/// print - Print variable.
664void DIVariable::print(raw_ostream &OS) const {
665 StringRef Res = getName();
666 if (!Res.empty())
667 OS << " [" << Res << "] ";
668
Dan Gohmanc014d092010-05-07 16:17:22 +0000669 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000670 OS << " [" << getLineNumber() << "] ";
Dan Gohmanc014d092010-05-07 16:17:22 +0000671 getType().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000672 OS << "\n";
673
674 // FIXME: Dump complex addresses
675}
676
677/// dump - Print descriptor to dbgs() with a newline.
678void DIDescriptor::dump() const {
679 print(dbgs()); dbgs() << '\n';
680}
681
682/// dump - Print compile unit to dbgs() with a newline.
683void DICompileUnit::dump() const {
684 print(dbgs()); dbgs() << '\n';
685}
686
687/// dump - Print type to dbgs() with a newline.
688void DIType::dump() const {
689 print(dbgs()); dbgs() << '\n';
690}
691
692/// dump - Print basic type to dbgs() with a newline.
693void DIBasicType::dump() const {
694 print(dbgs()); dbgs() << '\n';
695}
696
697/// dump - Print derived type to dbgs() with a newline.
698void DIDerivedType::dump() const {
699 print(dbgs()); dbgs() << '\n';
700}
701
702/// dump - Print composite type to dbgs() with a newline.
703void DICompositeType::dump() const {
704 print(dbgs()); dbgs() << '\n';
705}
706
Dan Gohman50404362010-05-07 15:30:29 +0000707/// dump - Print subprogram to dbgs() with a newline.
708void DISubprogram::dump() const {
709 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000710}
711
712/// dump - Print global variable.
713void DIGlobalVariable::dump() const {
Dan Gohman50404362010-05-07 15:30:29 +0000714 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000715}
716
717/// dump - Print variable.
718void DIVariable::dump() const {
Dan Gohman50404362010-05-07 15:30:29 +0000719 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000720}
721
722//===----------------------------------------------------------------------===//
Chris Lattnera45664f2008-11-10 02:56:27 +0000723// DIFactory: Basic Helpers
724//===----------------------------------------------------------------------===//
725
Bill Wendlingdc817b62009-05-14 18:26:15 +0000726DIFactory::DIFactory(Module &m)
Victor Hernandez06203122010-01-23 00:03:28 +0000727 : M(m), VMContext(M.getContext()), DeclareFn(0), ValueFn(0) {}
Chris Lattner497a7a82008-11-10 04:10:34 +0000728
Chris Lattnera45664f2008-11-10 02:56:27 +0000729Constant *DIFactory::GetTagConstant(unsigned TAG) {
Devang Patel6906ba52009-01-20 19:22:03 +0000730 assert((TAG & LLVMDebugVersionMask) == 0 &&
Chris Lattnera45664f2008-11-10 02:56:27 +0000731 "Tag too large for debug encoding!");
Owen Anderson1d0be152009-08-13 21:58:54 +0000732 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
Chris Lattnera45664f2008-11-10 02:56:27 +0000733}
734
Chris Lattnera45664f2008-11-10 02:56:27 +0000735//===----------------------------------------------------------------------===//
736// DIFactory: Primary Constructors
737//===----------------------------------------------------------------------===//
738
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000739/// GetOrCreateArray - Create an descriptor for an array of descriptors.
Chris Lattnera45664f2008-11-10 02:56:27 +0000740/// This implicitly uniques the arrays created.
741DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
Benjamin Kramera53557e2010-09-21 16:41:29 +0000742 if (NumTys == 0) {
743 Value *Null = llvm::Constant::getNullValue(Type::getInt32Ty(VMContext));
744 return DIArray(MDNode::get(VMContext, &Null, 1));
745 }
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000746
Benjamin Kramera53557e2010-09-21 16:41:29 +0000747 SmallVector<Value *, 16> Elts(Tys, Tys+NumTys);
748 return DIArray(MDNode::get(VMContext, Elts.data(), Elts.size()));
Chris Lattnera45664f2008-11-10 02:56:27 +0000749}
750
751/// GetOrCreateSubrange - Create a descriptor for a value range. This
752/// implicitly uniques the values returned.
753DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
Devang Patele4b27562009-08-28 23:24:31 +0000754 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000755 GetTagConstant(dwarf::DW_TAG_subrange_type),
Owen Anderson1d0be152009-08-13 21:58:54 +0000756 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
757 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
Chris Lattnera45664f2008-11-10 02:56:27 +0000758 };
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000759
Devang Patele4b27562009-08-28 23:24:31 +0000760 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000761}
762
Devang Pateld6747df2010-10-06 20:50:40 +0000763/// CreateUnspecifiedParameter - Create unspeicified type descriptor
764/// for the subroutine type.
765DIDescriptor DIFactory::CreateUnspecifiedParameter() {
766 Value *Elts[] = {
767 GetTagConstant(dwarf::DW_TAG_unspecified_parameters)
768 };
769 return DIDescriptor(MDNode::get(VMContext, &Elts[0], 1));
770}
Chris Lattnera45664f2008-11-10 02:56:27 +0000771
772/// CreateCompileUnit - Create a new descriptor for the specified compile
773/// unit. Note that this does not unique compile units within the module.
774DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
Devang Patel65dbc902009-11-25 17:36:49 +0000775 StringRef Filename,
776 StringRef Directory,
777 StringRef Producer,
Devang Pateldd9db662009-01-30 18:20:31 +0000778 bool isMain,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000779 bool isOptimized,
Devang Patel65dbc902009-11-25 17:36:49 +0000780 StringRef Flags,
Devang Patel13319ce2009-02-17 22:43:44 +0000781 unsigned RunTimeVer) {
Devang Patele4b27562009-08-28 23:24:31 +0000782 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000783 GetTagConstant(dwarf::DW_TAG_compile_unit),
Devang Patele4b27562009-08-28 23:24:31 +0000784 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Owen Anderson1d0be152009-08-13 21:58:54 +0000785 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
Devang Patele4b27562009-08-28 23:24:31 +0000786 MDString::get(VMContext, Filename),
787 MDString::get(VMContext, Directory),
788 MDString::get(VMContext, Producer),
Owen Anderson1d0be152009-08-13 21:58:54 +0000789 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
790 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
Devang Patele4b27562009-08-28 23:24:31 +0000791 MDString::get(VMContext, Flags),
Owen Anderson1d0be152009-08-13 21:58:54 +0000792 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
Chris Lattnera45664f2008-11-10 02:56:27 +0000793 };
Devang Patele4b27562009-08-28 23:24:31 +0000794
795 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000796}
797
Devang Patel7aa81892010-03-08 22:27:22 +0000798/// CreateFile - Create a new descriptor for the specified file.
799DIFile DIFactory::CreateFile(StringRef Filename,
800 StringRef Directory,
801 DICompileUnit CU) {
802 Value *Elts[] = {
803 GetTagConstant(dwarf::DW_TAG_file_type),
804 MDString::get(VMContext, Filename),
805 MDString::get(VMContext, Directory),
Devang Patel2db49d72010-05-07 18:11:54 +0000806 CU
Devang Patel7aa81892010-03-08 22:27:22 +0000807 };
808
809 return DIFile(MDNode::get(VMContext, &Elts[0], 4));
810}
811
Chris Lattnera45664f2008-11-10 02:56:27 +0000812/// CreateEnumerator - Create a single enumerator value.
Devang Patel65dbc902009-11-25 17:36:49 +0000813DIEnumerator DIFactory::CreateEnumerator(StringRef Name, uint64_t Val){
Devang Patele4b27562009-08-28 23:24:31 +0000814 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000815 GetTagConstant(dwarf::DW_TAG_enumerator),
Devang Patele4b27562009-08-28 23:24:31 +0000816 MDString::get(VMContext, Name),
Owen Anderson1d0be152009-08-13 21:58:54 +0000817 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
Chris Lattnera45664f2008-11-10 02:56:27 +0000818 };
Devang Patele4b27562009-08-28 23:24:31 +0000819 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000820}
821
822
823/// CreateBasicType - Create a basic type like int, float, etc.
824DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000825 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000826 DIFile F,
Chris Lattnera45664f2008-11-10 02:56:27 +0000827 unsigned LineNumber,
828 uint64_t SizeInBits,
829 uint64_t AlignInBits,
830 uint64_t OffsetInBits, unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000831 unsigned Encoding) {
Devang Patele4b27562009-08-28 23:24:31 +0000832 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000833 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patel2db49d72010-05-07 18:11:54 +0000834 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000835 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000836 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000837 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
838 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
839 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
840 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
841 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
842 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
Chris Lattnera45664f2008-11-10 02:56:27 +0000843 };
Devang Patele4b27562009-08-28 23:24:31 +0000844 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000845}
846
Devang Patelac16d442009-10-26 16:54:35 +0000847
848/// CreateBasicType - Create a basic type like int, float, etc.
849DIBasicType DIFactory::CreateBasicTypeEx(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000850 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000851 DIFile F,
Devang Patelac16d442009-10-26 16:54:35 +0000852 unsigned LineNumber,
853 Constant *SizeInBits,
854 Constant *AlignInBits,
855 Constant *OffsetInBits, unsigned Flags,
856 unsigned Encoding) {
857 Value *Elts[] = {
858 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patel2db49d72010-05-07 18:11:54 +0000859 Context,
Devang Patelac16d442009-10-26 16:54:35 +0000860 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000861 F,
Devang Patelac16d442009-10-26 16:54:35 +0000862 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
863 SizeInBits,
864 AlignInBits,
865 OffsetInBits,
866 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
867 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
868 };
869 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
870}
871
Devang Patelb4645642010-02-06 01:02:37 +0000872/// CreateArtificialType - Create a new DIType with "artificial" flag set.
873DIType DIFactory::CreateArtificialType(DIType Ty) {
874 if (Ty.isArtificial())
875 return Ty;
876
877 SmallVector<Value *, 9> Elts;
Devang Patel2db49d72010-05-07 18:11:54 +0000878 MDNode *N = Ty;
Devang Patelb4645642010-02-06 01:02:37 +0000879 assert (N && "Unexpected input DIType!");
880 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
881 if (Value *V = N->getOperand(i))
882 Elts.push_back(V);
883 else
884 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
885 }
886
887 unsigned CurFlags = Ty.getFlags();
888 CurFlags = CurFlags | DIType::FlagArtificial;
889
890 // Flags are stored at this slot.
891 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
892
893 return DIType(MDNode::get(VMContext, Elts.data(), Elts.size()));
894}
Devang Patelac16d442009-10-26 16:54:35 +0000895
Chris Lattnera45664f2008-11-10 02:56:27 +0000896/// CreateDerivedType - Create a derived type like const qualified type,
897/// pointer, typedef, etc.
898DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
899 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000900 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000901 DIFile F,
Chris Lattnera45664f2008-11-10 02:56:27 +0000902 unsigned LineNumber,
903 uint64_t SizeInBits,
904 uint64_t AlignInBits,
905 uint64_t OffsetInBits,
906 unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000907 DIType DerivedFrom) {
Devang Patele4b27562009-08-28 23:24:31 +0000908 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000909 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000910 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000911 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000912 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000913 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
914 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
915 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
916 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
917 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000918 DerivedFrom,
Chris Lattnera45664f2008-11-10 02:56:27 +0000919 };
Devang Patele4b27562009-08-28 23:24:31 +0000920 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000921}
922
Devang Patelac16d442009-10-26 16:54:35 +0000923
924/// CreateDerivedType - Create a derived type like const qualified type,
925/// pointer, typedef, etc.
926DIDerivedType DIFactory::CreateDerivedTypeEx(unsigned Tag,
927 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000928 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000929 DIFile F,
Devang Patelac16d442009-10-26 16:54:35 +0000930 unsigned LineNumber,
931 Constant *SizeInBits,
932 Constant *AlignInBits,
933 Constant *OffsetInBits,
934 unsigned Flags,
935 DIType DerivedFrom) {
936 Value *Elts[] = {
937 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000938 Context,
Devang Patelac16d442009-10-26 16:54:35 +0000939 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000940 F,
Devang Patelac16d442009-10-26 16:54:35 +0000941 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
942 SizeInBits,
943 AlignInBits,
944 OffsetInBits,
945 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000946 DerivedFrom,
Devang Patelac16d442009-10-26 16:54:35 +0000947 };
948 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
949}
950
951
Chris Lattnera45664f2008-11-10 02:56:27 +0000952/// CreateCompositeType - Create a composite type like array, struct, etc.
953DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
954 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000955 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000956 DIFile F,
Chris Lattnera45664f2008-11-10 02:56:27 +0000957 unsigned LineNumber,
958 uint64_t SizeInBits,
959 uint64_t AlignInBits,
960 uint64_t OffsetInBits,
961 unsigned Flags,
962 DIType DerivedFrom,
Devang Patel13319ce2009-02-17 22:43:44 +0000963 DIArray Elements,
Devang Patel0fd7f9d2010-01-26 21:14:59 +0000964 unsigned RuntimeLang,
965 MDNode *ContainingType) {
Owen Andersone277fed2009-07-07 16:31:25 +0000966
Devang Patele4b27562009-08-28 23:24:31 +0000967 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000968 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000969 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000970 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000971 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000972 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
973 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
974 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
975 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
976 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000977 DerivedFrom,
978 Elements,
Devang Patel0fd7f9d2010-01-26 21:14:59 +0000979 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
980 ContainingType
Chris Lattnera45664f2008-11-10 02:56:27 +0000981 };
Devang Patele7e5a0f2010-08-10 20:01:20 +0000982
983 MDNode *Node = MDNode::get(VMContext, &Elts[0], 13);
984 // Create a named metadata so that we do not lose this enum info.
985 if (Tag == dwarf::DW_TAG_enumeration_type) {
986 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.enum");
987 NMD->addOperand(Node);
988 }
989 return DICompositeType(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +0000990}
991
Dan Gohman489b29b2010-08-20 22:02:26 +0000992/// CreateTemporaryType - Create a temporary forward-declared type.
Dan Gohmana3833f12010-08-20 22:39:47 +0000993DIType DIFactory::CreateTemporaryType() {
Dan Gohman489b29b2010-08-20 22:02:26 +0000994 // Give the temporary MDNode a tag. It doesn't matter what tag we
995 // use here as long as DIType accepts it.
996 Value *Elts[] = {
997 GetTagConstant(DW_TAG_base_type)
998 };
999 MDNode *Node = MDNode::getTemporary(VMContext, Elts, array_lengthof(Elts));
1000 return DIType(Node);
1001}
1002
Devang Patelfe58f952010-12-07 23:25:47 +00001003/// CreateTemporaryType - Create a temporary forward-declared type.
1004DIType DIFactory::CreateTemporaryType(DIFile F) {
1005 // Give the temporary MDNode a tag. It doesn't matter what tag we
1006 // use here as long as DIType accepts it.
1007 Value *Elts[] = {
1008 GetTagConstant(DW_TAG_base_type),
1009 F.getCompileUnit(),
1010 NULL,
1011 F
1012 };
1013 MDNode *Node = MDNode::getTemporary(VMContext, Elts, array_lengthof(Elts));
1014 return DIType(Node);
1015}
Dan Gohman489b29b2010-08-20 22:02:26 +00001016
Devang Patelac16d442009-10-26 16:54:35 +00001017/// CreateCompositeType - Create a composite type like array, struct, etc.
1018DICompositeType DIFactory::CreateCompositeTypeEx(unsigned Tag,
1019 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +00001020 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +00001021 DIFile F,
Devang Patelac16d442009-10-26 16:54:35 +00001022 unsigned LineNumber,
1023 Constant *SizeInBits,
1024 Constant *AlignInBits,
1025 Constant *OffsetInBits,
1026 unsigned Flags,
1027 DIType DerivedFrom,
1028 DIArray Elements,
Devang Patel6bf058c2010-08-10 20:22:49 +00001029 unsigned RuntimeLang,
1030 MDNode *ContainingType) {
Devang Patelac16d442009-10-26 16:54:35 +00001031 Value *Elts[] = {
1032 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +00001033 Context,
Devang Patelac16d442009-10-26 16:54:35 +00001034 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +00001035 F,
Devang Patelac16d442009-10-26 16:54:35 +00001036 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
1037 SizeInBits,
1038 AlignInBits,
1039 OffsetInBits,
1040 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +00001041 DerivedFrom,
1042 Elements,
Devang Patel6bf058c2010-08-10 20:22:49 +00001043 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
1044 ContainingType
Devang Patelac16d442009-10-26 16:54:35 +00001045 };
Devang Patel6bf058c2010-08-10 20:22:49 +00001046 MDNode *Node = MDNode::get(VMContext, &Elts[0], 13);
Devang Patele7e5a0f2010-08-10 20:01:20 +00001047 // Create a named metadata so that we do not lose this enum info.
1048 if (Tag == dwarf::DW_TAG_enumeration_type) {
1049 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.enum");
1050 NMD->addOperand(Node);
1051 }
1052 return DICompositeType(Node);
Devang Patelac16d442009-10-26 16:54:35 +00001053}
1054
1055
Chris Lattnera45664f2008-11-10 02:56:27 +00001056/// CreateSubprogram - Create a new descriptor for the specified subprogram.
1057/// See comments in DISubprogram for descriptions of these fields. This
1058/// method does not unique the generated descriptors.
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001059DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +00001060 StringRef Name,
1061 StringRef DisplayName,
1062 StringRef LinkageName,
Devang Patel4b945502010-03-09 00:44:10 +00001063 DIFile F,
Devang Patel2e369932010-01-23 00:26:28 +00001064 unsigned LineNo, DIType Ty,
Chris Lattnera45664f2008-11-10 02:56:27 +00001065 bool isLocalToUnit,
Devang Patel5d11eb02009-12-03 19:11:07 +00001066 bool isDefinition,
1067 unsigned VK, unsigned VIndex,
Devang Patel4e0d19d2010-02-03 19:57:19 +00001068 DIType ContainingType,
Devang Patel9dd2b472010-09-29 21:04:46 +00001069 unsigned Flags,
Stuart Hastings215aa152010-06-11 20:08:44 +00001070 bool isOptimized,
1071 Function *Fn) {
Devang Patel854967e2008-12-17 22:39:29 +00001072
Devang Patele4b27562009-08-28 23:24:31 +00001073 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001074 GetTagConstant(dwarf::DW_TAG_subprogram),
Devang Patele4b27562009-08-28 23:24:31 +00001075 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Devang Patel2db49d72010-05-07 18:11:54 +00001076 Context,
Devang Patele4b27562009-08-28 23:24:31 +00001077 MDString::get(VMContext, Name),
1078 MDString::get(VMContext, DisplayName),
1079 MDString::get(VMContext, LinkageName),
Devang Patel2db49d72010-05-07 18:11:54 +00001080 F,
Owen Anderson1d0be152009-08-13 21:58:54 +00001081 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001082 Ty,
Owen Anderson1d0be152009-08-13 21:58:54 +00001083 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
Devang Patel5d11eb02009-12-03 19:11:07 +00001084 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1085 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
1086 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
Devang Patel2db49d72010-05-07 18:11:54 +00001087 ContainingType,
Devang Patel9dd2b472010-09-29 21:04:46 +00001088 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Stuart Hastings215aa152010-06-11 20:08:44 +00001089 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
1090 Fn
Chris Lattnera45664f2008-11-10 02:56:27 +00001091 };
Devang Patelfd5fdc32010-06-28 05:53:08 +00001092 MDNode *Node = MDNode::get(VMContext, &Elts[0], 17);
1093
1094 // Create a named metadata so that we do not lose this mdnode.
1095 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
1096 NMD->addOperand(Node);
1097 return DISubprogram(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +00001098}
1099
Devang Patele3a18de2009-12-01 23:09:02 +00001100/// CreateSubprogramDefinition - Create new subprogram descriptor for the
Jim Grosbache62b6902010-07-21 21:36:25 +00001101/// given declaration.
1102DISubprogram DIFactory::CreateSubprogramDefinition(DISubprogram &SPDeclaration){
Devang Patele3a18de2009-12-01 23:09:02 +00001103 if (SPDeclaration.isDefinition())
Devang Patel2db49d72010-05-07 18:11:54 +00001104 return DISubprogram(SPDeclaration);
Devang Patele3a18de2009-12-01 23:09:02 +00001105
Devang Patel2db49d72010-05-07 18:11:54 +00001106 MDNode *DeclNode = SPDeclaration;
Devang Patele3a18de2009-12-01 23:09:02 +00001107 Value *Elts[] = {
1108 GetTagConstant(dwarf::DW_TAG_subprogram),
1109 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001110 DeclNode->getOperand(2), // Context
1111 DeclNode->getOperand(3), // Name
1112 DeclNode->getOperand(4), // DisplayName
1113 DeclNode->getOperand(5), // LinkageName
1114 DeclNode->getOperand(6), // CompileUnit
1115 DeclNode->getOperand(7), // LineNo
1116 DeclNode->getOperand(8), // Type
1117 DeclNode->getOperand(9), // isLocalToUnit
Stuart Hastings6d56b9f2010-06-05 00:39:29 +00001118 ConstantInt::get(Type::getInt1Ty(VMContext), true),
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001119 DeclNode->getOperand(11), // Virtuality
1120 DeclNode->getOperand(12), // VIndex
Devang Patel4e0d19d2010-02-03 19:57:19 +00001121 DeclNode->getOperand(13), // Containting Type
Devang Patel9dd2b472010-09-29 21:04:46 +00001122 DeclNode->getOperand(14), // Flags
Devang Patelacc6efa2010-06-27 21:04:31 +00001123 DeclNode->getOperand(15), // isOptimized
1124 SPDeclaration.getFunction()
Devang Patele3a18de2009-12-01 23:09:02 +00001125 };
Devang Patelfd5fdc32010-06-28 05:53:08 +00001126 MDNode *Node =MDNode::get(VMContext, &Elts[0], 16);
1127
1128 // Create a named metadata so that we do not lose this mdnode.
1129 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
1130 NMD->addOperand(Node);
1131 return DISubprogram(Node);
Devang Patele3a18de2009-12-01 23:09:02 +00001132}
1133
Chris Lattnera45664f2008-11-10 02:56:27 +00001134/// CreateGlobalVariable - Create a new descriptor for the specified global.
1135DIGlobalVariable
Devang Patel65dbc902009-11-25 17:36:49 +00001136DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
1137 StringRef DisplayName,
1138 StringRef LinkageName,
Devang Patel4b945502010-03-09 00:44:10 +00001139 DIFile F,
Devang Patel2e369932010-01-23 00:26:28 +00001140 unsigned LineNo, DIType Ty,bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +00001141 bool isDefinition, llvm::GlobalVariable *Val) {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001142 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001143 GetTagConstant(dwarf::DW_TAG_variable),
Devang Patele4b27562009-08-28 23:24:31 +00001144 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Devang Patel2db49d72010-05-07 18:11:54 +00001145 Context,
Devang Patele4b27562009-08-28 23:24:31 +00001146 MDString::get(VMContext, Name),
1147 MDString::get(VMContext, DisplayName),
1148 MDString::get(VMContext, LinkageName),
Devang Patel2db49d72010-05-07 18:11:54 +00001149 F,
Owen Anderson1d0be152009-08-13 21:58:54 +00001150 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001151 Ty,
Owen Anderson1d0be152009-08-13 21:58:54 +00001152 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1153 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
Devang Patele4b27562009-08-28 23:24:31 +00001154 Val
Chris Lattnera45664f2008-11-10 02:56:27 +00001155 };
Devang Patele4b27562009-08-28 23:24:31 +00001156
1157 Value *const *Vs = &Elts[0];
1158 MDNode *Node = MDNode::get(VMContext,Vs, 12);
1159
1160 // Create a named metadata so that we do not lose this mdnode.
1161 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001162 NMD->addOperand(Node);
Devang Patele4b27562009-08-28 23:24:31 +00001163
1164 return DIGlobalVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +00001165}
1166
Devang Patel27398962010-08-09 21:39:24 +00001167/// CreateGlobalVariable - Create a new descriptor for the specified constant.
1168DIGlobalVariable
1169DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
1170 StringRef DisplayName,
1171 StringRef LinkageName,
1172 DIFile F,
1173 unsigned LineNo, DIType Ty,bool isLocalToUnit,
1174 bool isDefinition, llvm::Constant *Val) {
1175 Value *Elts[] = {
Devang Patelebd53742010-08-11 23:17:54 +00001176 GetTagConstant(dwarf::DW_TAG_variable),
Devang Patel27398962010-08-09 21:39:24 +00001177 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
1178 Context,
1179 MDString::get(VMContext, Name),
1180 MDString::get(VMContext, DisplayName),
1181 MDString::get(VMContext, LinkageName),
1182 F,
1183 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1184 Ty,
1185 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1186 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1187 Val
1188 };
1189
1190 Value *const *Vs = &Elts[0];
1191 MDNode *Node = MDNode::get(VMContext,Vs, 12);
1192
1193 // Create a named metadata so that we do not lose this mdnode.
1194 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
1195 NMD->addOperand(Node);
1196
1197 return DIGlobalVariable(Node);
1198}
Chris Lattnera45664f2008-11-10 02:56:27 +00001199
Devang Patel62367042010-11-10 22:19:21 +00001200/// fixupObjcLikeName - Replace contains special characters used
1201/// in a typical Objective-C names with '.' in a given string.
1202static void fixupObjcLikeName(std::string &Str) {
1203 for (size_t i = 0, e = Str.size(); i < e; ++i) {
1204 char C = Str[i];
Jakob Stoklund Olesen5b3f7792010-12-03 23:40:45 +00001205 if (C == '[' || C == ']' || C == ' ' || C == ':' || C == '+' ||
1206 C == '(' || C == ')')
Devang Patel62367042010-11-10 22:19:21 +00001207 Str[i] = '.';
1208 }
1209}
1210
1211/// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
1212/// to hold function specific information.
1213NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, StringRef FuncName) {
1214 SmallString<32> Out;
1215 if (FuncName.find('[') == StringRef::npos)
1216 return M.getOrInsertNamedMetadata(Twine("llvm.dbg.lv.", FuncName)
1217 .toStringRef(Out));
1218 std::string Name = FuncName;
1219 fixupObjcLikeName(Name);
1220 return M.getOrInsertNamedMetadata(Twine("llvm.dbg.lv.", Name)
1221 .toStringRef(Out));
1222}
1223
1224/// getFnSpecificMDNode - Return a NameMDNode, if available, that is
1225/// suitable to hold function specific information.
1226NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, StringRef FuncName) {
1227 if (FuncName.find('[') == StringRef::npos)
1228 return M.getNamedMetadata(Twine("llvm.dbg.lv.", FuncName));
1229 std::string Name = FuncName;
1230 fixupObjcLikeName(Name);
1231 return M.getNamedMetadata(Twine("llvm.dbg.lv.", Name));
1232}
1233
Chris Lattnera45664f2008-11-10 02:56:27 +00001234/// CreateVariable - Create a new descriptor for the specified variable.
1235DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +00001236 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +00001237 DIFile F,
1238 unsigned LineNo,
Devang Patel3cf763d2010-09-29 23:07:21 +00001239 DIType Ty, bool AlwaysPreserve,
1240 unsigned Flags) {
Devang Patele4b27562009-08-28 23:24:31 +00001241 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001242 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +00001243 Context,
Devang Patele4b27562009-08-28 23:24:31 +00001244 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +00001245 F,
Owen Anderson1d0be152009-08-13 21:58:54 +00001246 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001247 Ty,
Devang Patel3cf763d2010-09-29 23:07:21 +00001248 ConstantInt::get(Type::getInt32Ty(VMContext), Flags)
Chris Lattnera45664f2008-11-10 02:56:27 +00001249 };
Devang Patel3cf763d2010-09-29 23:07:21 +00001250 MDNode *Node = MDNode::get(VMContext, &Elts[0], 7);
Devang Patel6ed0ce32010-05-20 20:35:24 +00001251 if (AlwaysPreserve) {
1252 // The optimizer may remove local variable. If there is an interest
1253 // to preserve variable info in such situation then stash it in a
1254 // named mdnode.
Devang Patel2f7d5292010-06-16 00:53:55 +00001255 DISubprogram Fn(getDISubprogram(Context));
Devang Patel10de3bb2010-06-21 18:36:58 +00001256 StringRef FName = "fn";
1257 if (Fn.getFunction())
1258 FName = Fn.getFunction()->getName();
Devang Patel10de3bb2010-06-21 18:36:58 +00001259 char One = '\1';
1260 if (FName.startswith(StringRef(&One, 1)))
1261 FName = FName.substr(1);
Dan Gohman17aa92c2010-07-21 23:38:33 +00001262
Devang Patel62367042010-11-10 22:19:21 +00001263
1264 NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, FName);
Devang Patel2f7d5292010-06-16 00:53:55 +00001265 FnLocals->addOperand(Node);
Devang Patel98e1cac2010-05-14 21:01:35 +00001266 }
1267 return DIVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +00001268}
1269
1270
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001271/// CreateComplexVariable - Create a new descriptor for the specified variable
1272/// which has a complex address expression for its address.
1273DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
Benjamin Kramer28b4afc2010-09-21 16:00:03 +00001274 StringRef Name, DIFile F,
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001275 unsigned LineNo,
Benjamin Kramer28b4afc2010-09-21 16:00:03 +00001276 DIType Ty, Value *const *Addr,
1277 unsigned NumAddr) {
1278 SmallVector<Value *, 15> Elts;
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001279 Elts.push_back(GetTagConstant(Tag));
Devang Patel2db49d72010-05-07 18:11:54 +00001280 Elts.push_back(Context);
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001281 Elts.push_back(MDString::get(VMContext, Name));
Devang Patel2db49d72010-05-07 18:11:54 +00001282 Elts.push_back(F);
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001283 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
Devang Patel2db49d72010-05-07 18:11:54 +00001284 Elts.push_back(Ty);
Benjamin Kramer28b4afc2010-09-21 16:00:03 +00001285 Elts.append(Addr, Addr+NumAddr);
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001286
Benjamin Kramer28b4afc2010-09-21 16:00:03 +00001287 return DIVariable(MDNode::get(VMContext, Elts.data(), Elts.size()));
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001288}
1289
1290
Chris Lattnera45664f2008-11-10 02:56:27 +00001291/// CreateBlock - This creates a descriptor for a lexical block with the
Owen Anderson99035272009-07-07 17:12:53 +00001292/// specified parent VMContext.
Devang Patel3d821aa2010-02-16 21:39:34 +00001293DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context,
Stuart Hastings0db42712010-07-19 23:56:30 +00001294 DIFile F, unsigned LineNo,
1295 unsigned Col) {
1296 // Defeat MDNode uniqing for lexical blocks.
1297 static unsigned int unique_id = 0;
Devang Patele4b27562009-08-28 23:24:31 +00001298 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001299 GetTagConstant(dwarf::DW_TAG_lexical_block),
Devang Patel2db49d72010-05-07 18:11:54 +00001300 Context,
Devang Patel3d821aa2010-02-16 21:39:34 +00001301 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Stuart Hastings0db42712010-07-19 23:56:30 +00001302 ConstantInt::get(Type::getInt32Ty(VMContext), Col),
1303 F,
1304 ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
Chris Lattnera45664f2008-11-10 02:56:27 +00001305 };
Stuart Hastings0db42712010-07-19 23:56:30 +00001306 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 6));
Chris Lattnera45664f2008-11-10 02:56:27 +00001307}
1308
Devang Patel6404e4e2009-12-15 19:16:48 +00001309/// CreateNameSpace - This creates new descriptor for a namespace
1310/// with the specified parent context.
1311DINameSpace DIFactory::CreateNameSpace(DIDescriptor Context, StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +00001312 DIFile F,
Devang Patel6404e4e2009-12-15 19:16:48 +00001313 unsigned LineNo) {
1314 Value *Elts[] = {
1315 GetTagConstant(dwarf::DW_TAG_namespace),
Devang Patel2db49d72010-05-07 18:11:54 +00001316 Context,
Devang Patel6404e4e2009-12-15 19:16:48 +00001317 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +00001318 F,
Devang Patel6404e4e2009-12-15 19:16:48 +00001319 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1320 };
1321 return DINameSpace(MDNode::get(VMContext, &Elts[0], 5));
1322}
1323
Devang Patelf98d8fe2009-09-01 01:14:15 +00001324/// CreateLocation - Creates a debug info location.
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001325DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
Daniel Dunbara279bc32009-09-20 02:20:51 +00001326 DIScope S, DILocation OrigLoc) {
Devang Patelf98d8fe2009-09-01 01:14:15 +00001327 Value *Elts[] = {
1328 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1329 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001330 S,
1331 OrigLoc,
Devang Patelf98d8fe2009-09-01 01:14:15 +00001332 };
1333 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1334}
1335
Chris Lattnera45664f2008-11-10 02:56:27 +00001336//===----------------------------------------------------------------------===//
1337// DIFactory: Routines for inserting code into a function
1338//===----------------------------------------------------------------------===//
1339
Chris Lattnera45664f2008-11-10 02:56:27 +00001340/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Devang Patel6daf99b2009-11-10 22:05:35 +00001341Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001342 Instruction *InsertBefore) {
Victor Hernandez4cf292a2010-01-26 02:07:38 +00001343 assert(Storage && "no storage passed to dbg.declare");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001344 assert(D.Verify() && "empty DIVariable passed to dbg.declare");
Chris Lattnera45664f2008-11-10 02:56:27 +00001345 if (!DeclareFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +00001346 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1347
Victor Hernandez756462b2010-01-18 20:42:09 +00001348 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
Devang Patel2db49d72010-05-07 18:11:54 +00001349 D };
Devang Patel6daf99b2009-11-10 22:05:35 +00001350 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
Mike Stumpe4250392009-10-01 22:08:58 +00001351}
1352
1353/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Devang Patel6daf99b2009-11-10 22:05:35 +00001354Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001355 BasicBlock *InsertAtEnd) {
Victor Hernandez4cf292a2010-01-26 02:07:38 +00001356 assert(Storage && "no storage passed to dbg.declare");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001357 assert(D.Verify() && "invalid DIVariable passed to dbg.declare");
Mike Stumpe4250392009-10-01 22:08:58 +00001358 if (!DeclareFn)
1359 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1360
Victor Hernandez756462b2010-01-18 20:42:09 +00001361 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
Devang Patel2db49d72010-05-07 18:11:54 +00001362 D };
Devang Patel27a53de2010-01-29 18:30:57 +00001363
1364 // If this block already has a terminator then insert this intrinsic
1365 // before the terminator.
Jim Grosbache62b6902010-07-21 21:36:25 +00001366 if (TerminatorInst *T = InsertAtEnd->getTerminator())
Devang Patel27a53de2010-01-29 18:30:57 +00001367 return CallInst::Create(DeclareFn, Args, Args+2, "", T);
1368 else
1369 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);}
Torok Edwin620f2802008-12-16 09:07:36 +00001370
Victor Hernandezc59b3352009-12-07 21:54:43 +00001371/// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001372Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
Victor Hernandezc59b3352009-12-07 21:54:43 +00001373 DIVariable D,
1374 Instruction *InsertBefore) {
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001375 assert(V && "no value passed to dbg.value");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001376 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001377 if (!ValueFn)
1378 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1379
Victor Hernandezc8b7cd02010-01-20 05:44:11 +00001380 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001381 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
Devang Patel2db49d72010-05-07 18:11:54 +00001382 D };
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001383 return CallInst::Create(ValueFn, Args, Args+3, "", InsertBefore);
1384}
1385
Victor Hernandezc59b3352009-12-07 21:54:43 +00001386/// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001387Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
Victor Hernandezc59b3352009-12-07 21:54:43 +00001388 DIVariable D,
1389 BasicBlock *InsertAtEnd) {
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001390 assert(V && "no value passed to dbg.value");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001391 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001392 if (!ValueFn)
1393 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1394
Jim Grosbache62b6902010-07-21 21:36:25 +00001395 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001396 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
Devang Patel2db49d72010-05-07 18:11:54 +00001397 D };
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001398 return CallInst::Create(ValueFn, Args, Args+3, "", InsertAtEnd);
1399}
Devang Patele4b27562009-08-28 23:24:31 +00001400
Devang Patel1a7ca032010-09-28 18:08:20 +00001401// RecordType - Record DIType in a module such that it is not lost even if
1402// it is not referenced through debug info anchors.
1403void DIFactory::RecordType(DIType T) {
1404 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.ty");
1405 NMD->addOperand(T);
1406}
1407
1408
Devang Pateld2f79a12009-07-28 19:55:13 +00001409//===----------------------------------------------------------------------===//
Devang Patel98c65172009-07-30 18:25:15 +00001410// DebugInfoFinder implementations.
Devang Pateld2f79a12009-07-28 19:55:13 +00001411//===----------------------------------------------------------------------===//
1412
Devang Patel98c65172009-07-30 18:25:15 +00001413/// processModule - Process entire module and collect debug info.
1414void DebugInfoFinder::processModule(Module &M) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001415 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1416 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
1417 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
1418 ++BI) {
Devang Patel01c5ff62010-05-04 01:05:02 +00001419 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
Devang Patelb4d31302009-07-31 18:18:52 +00001420 processDeclare(DDI);
Jim Grosbache62b6902010-07-21 21:36:25 +00001421
Chris Lattner28a9bf62010-04-02 20:44:29 +00001422 DebugLoc Loc = BI->getDebugLoc();
1423 if (Loc.isUnknown())
1424 continue;
Jim Grosbache62b6902010-07-21 21:36:25 +00001425
Chris Lattner28a9bf62010-04-02 20:44:29 +00001426 LLVMContext &Ctx = BI->getContext();
1427 DIDescriptor Scope(Loc.getScope(Ctx));
Jim Grosbache62b6902010-07-21 21:36:25 +00001428
Chris Lattner28a9bf62010-04-02 20:44:29 +00001429 if (Scope.isCompileUnit())
Devang Patel2db49d72010-05-07 18:11:54 +00001430 addCompileUnit(DICompileUnit(Scope));
Chris Lattner28a9bf62010-04-02 20:44:29 +00001431 else if (Scope.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001432 processSubprogram(DISubprogram(Scope));
Chris Lattner28a9bf62010-04-02 20:44:29 +00001433 else if (Scope.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001434 processLexicalBlock(DILexicalBlock(Scope));
Jim Grosbache62b6902010-07-21 21:36:25 +00001435
Chris Lattner28a9bf62010-04-02 20:44:29 +00001436 if (MDNode *IA = Loc.getInlinedAt(Ctx))
1437 processLocation(DILocation(IA));
Devang Pateld2f79a12009-07-28 19:55:13 +00001438 }
Devang Patele4b27562009-08-28 23:24:31 +00001439
Devang Patelfd5fdc32010-06-28 05:53:08 +00001440 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
1441 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1442 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
1443 if (addGlobalVariable(DIG)) {
1444 addCompileUnit(DIG.getCompileUnit());
1445 processType(DIG.getType());
1446 }
Devang Pateld2f79a12009-07-28 19:55:13 +00001447 }
1448 }
Devang Patelfd5fdc32010-06-28 05:53:08 +00001449
1450 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
1451 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
1452 processSubprogram(DISubprogram(NMD->getOperand(i)));
Devang Pateld2f79a12009-07-28 19:55:13 +00001453}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001454
Devang Patel6daf99b2009-11-10 22:05:35 +00001455/// processLocation - Process DILocation.
1456void DebugInfoFinder::processLocation(DILocation Loc) {
Devang Patel3c91b052010-03-08 20:52:55 +00001457 if (!Loc.Verify()) return;
Devang Patel2db49d72010-05-07 18:11:54 +00001458 DIDescriptor S(Loc.getScope());
Devang Patel6daf99b2009-11-10 22:05:35 +00001459 if (S.isCompileUnit())
Devang Patel2db49d72010-05-07 18:11:54 +00001460 addCompileUnit(DICompileUnit(S));
Devang Patel6daf99b2009-11-10 22:05:35 +00001461 else if (S.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001462 processSubprogram(DISubprogram(S));
Devang Patel6daf99b2009-11-10 22:05:35 +00001463 else if (S.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001464 processLexicalBlock(DILexicalBlock(S));
Devang Patel6daf99b2009-11-10 22:05:35 +00001465 processLocation(Loc.getOrigLocation());
1466}
1467
Devang Patel98c65172009-07-30 18:25:15 +00001468/// processType - Process DIType.
1469void DebugInfoFinder::processType(DIType DT) {
Devang Patel72bcdb62009-08-10 22:09:58 +00001470 if (!addType(DT))
Devang Pateld2f79a12009-07-28 19:55:13 +00001471 return;
1472
1473 addCompileUnit(DT.getCompileUnit());
Devang Patel6ceea332009-08-31 18:49:10 +00001474 if (DT.isCompositeType()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001475 DICompositeType DCT(DT);
Devang Patel98c65172009-07-30 18:25:15 +00001476 processType(DCT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001477 DIArray DA = DCT.getTypeArray();
Devang Patel3c91b052010-03-08 20:52:55 +00001478 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1479 DIDescriptor D = DA.getElement(i);
1480 if (D.isType())
Devang Patel2db49d72010-05-07 18:11:54 +00001481 processType(DIType(D));
Devang Patel3c91b052010-03-08 20:52:55 +00001482 else if (D.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001483 processSubprogram(DISubprogram(D));
Devang Patel3c91b052010-03-08 20:52:55 +00001484 }
Devang Patel6ceea332009-08-31 18:49:10 +00001485 } else if (DT.isDerivedType()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001486 DIDerivedType DDT(DT);
Devang Patel3c91b052010-03-08 20:52:55 +00001487 processType(DDT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001488 }
1489}
1490
Devang Patelbeab41b2009-10-07 22:04:08 +00001491/// processLexicalBlock
1492void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
Devang Patelbeab41b2009-10-07 22:04:08 +00001493 DIScope Context = LB.getContext();
1494 if (Context.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001495 return processLexicalBlock(DILexicalBlock(Context));
Devang Patelbeab41b2009-10-07 22:04:08 +00001496 else
Devang Patel2db49d72010-05-07 18:11:54 +00001497 return processSubprogram(DISubprogram(Context));
Devang Patelbeab41b2009-10-07 22:04:08 +00001498}
1499
Devang Patel98c65172009-07-30 18:25:15 +00001500/// processSubprogram - Process DISubprogram.
1501void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001502 if (!addSubprogram(SP))
1503 return;
1504 addCompileUnit(SP.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +00001505 processType(SP.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +00001506}
1507
Devang Patelb4d31302009-07-31 18:18:52 +00001508/// processDeclare - Process DbgDeclareInst.
1509void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
Devang Patel3c91b052010-03-08 20:52:55 +00001510 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1511 if (!N) return;
1512
1513 DIDescriptor DV(N);
1514 if (!DV.isVariable())
Devang Patelb4d31302009-07-31 18:18:52 +00001515 return;
1516
Devang Patel2db49d72010-05-07 18:11:54 +00001517 if (!NodesSeen.insert(DV))
Devang Patelb4d31302009-07-31 18:18:52 +00001518 return;
1519
Devang Patel3c91b052010-03-08 20:52:55 +00001520 addCompileUnit(DIVariable(N).getCompileUnit());
1521 processType(DIVariable(N).getType());
Devang Patelb4d31302009-07-31 18:18:52 +00001522}
1523
Devang Patel72bcdb62009-08-10 22:09:58 +00001524/// addType - Add type into Tys.
1525bool DebugInfoFinder::addType(DIType DT) {
Devang Patel3c91b052010-03-08 20:52:55 +00001526 if (!DT.isValid())
Devang Patel72bcdb62009-08-10 22:09:58 +00001527 return false;
1528
Devang Patel2db49d72010-05-07 18:11:54 +00001529 if (!NodesSeen.insert(DT))
Devang Patel72bcdb62009-08-10 22:09:58 +00001530 return false;
1531
Devang Patel2db49d72010-05-07 18:11:54 +00001532 TYs.push_back(DT);
Devang Patel72bcdb62009-08-10 22:09:58 +00001533 return true;
1534}
1535
Devang Pateld2f79a12009-07-28 19:55:13 +00001536/// addCompileUnit - Add compile unit into CUs.
Devang Patel98c65172009-07-30 18:25:15 +00001537bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Patel3c91b052010-03-08 20:52:55 +00001538 if (!CU.Verify())
Devang Pateld2f79a12009-07-28 19:55:13 +00001539 return false;
1540
Devang Patel2db49d72010-05-07 18:11:54 +00001541 if (!NodesSeen.insert(CU))
Devang Pateld2f79a12009-07-28 19:55:13 +00001542 return false;
1543
Devang Patel2db49d72010-05-07 18:11:54 +00001544 CUs.push_back(CU);
Devang Pateld2f79a12009-07-28 19:55:13 +00001545 return true;
1546}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001547
Devang Pateld2f79a12009-07-28 19:55:13 +00001548/// addGlobalVariable - Add global variable into GVs.
Devang Patel98c65172009-07-30 18:25:15 +00001549bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Patel2db49d72010-05-07 18:11:54 +00001550 if (!DIDescriptor(DIG).isGlobalVariable())
Devang Pateld2f79a12009-07-28 19:55:13 +00001551 return false;
1552
Devang Patel2db49d72010-05-07 18:11:54 +00001553 if (!NodesSeen.insert(DIG))
Devang Pateld2f79a12009-07-28 19:55:13 +00001554 return false;
1555
Devang Patel2db49d72010-05-07 18:11:54 +00001556 GVs.push_back(DIG);
Devang Pateld2f79a12009-07-28 19:55:13 +00001557 return true;
1558}
1559
1560// addSubprogram - Add subprgoram into SPs.
Devang Patel98c65172009-07-30 18:25:15 +00001561bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Patel2db49d72010-05-07 18:11:54 +00001562 if (!DIDescriptor(SP).isSubprogram())
Devang Pateld2f79a12009-07-28 19:55:13 +00001563 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001564
Devang Patel2db49d72010-05-07 18:11:54 +00001565 if (!NodesSeen.insert(SP))
Devang Pateld2f79a12009-07-28 19:55:13 +00001566 return false;
1567
Devang Patel2db49d72010-05-07 18:11:54 +00001568 SPs.push_back(SP);
Devang Pateld2f79a12009-07-28 19:55:13 +00001569 return true;
1570}
1571
Victor Hernandez756462b2010-01-18 20:42:09 +00001572/// Find the debug info descriptor corresponding to this global variable.
1573static Value *findDbgGlobalDeclare(GlobalVariable *V) {
Chris Lattner099b7792009-12-29 09:22:47 +00001574 const Module *M = V->getParent();
1575 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1576 if (!NMD)
Torok Edwinff7d0e92009-03-10 13:41:26 +00001577 return 0;
Chris Lattner099b7792009-12-29 09:22:47 +00001578
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001579 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman872814a2010-07-21 18:54:18 +00001580 DIDescriptor DIG(cast<MDNode>(NMD->getOperand(i)));
Devang Patel3c91b052010-03-08 20:52:55 +00001581 if (!DIG.isGlobalVariable())
Chris Lattner099b7792009-12-29 09:22:47 +00001582 continue;
Devang Patel2db49d72010-05-07 18:11:54 +00001583 if (DIGlobalVariable(DIG).getGlobal() == V)
1584 return DIG;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001585 }
Chris Lattner099b7792009-12-29 09:22:47 +00001586 return 0;
1587}
Torok Edwinff7d0e92009-03-10 13:41:26 +00001588
Chris Lattner099b7792009-12-29 09:22:47 +00001589/// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
1590/// It looks through pointer casts too.
Victor Hernandez756462b2010-01-18 20:42:09 +00001591static const DbgDeclareInst *findDbgDeclare(const Value *V) {
Victor Hernandez3a328652010-01-15 19:04:09 +00001592 V = V->stripPointerCasts();
Jim Grosbache62b6902010-07-21 21:36:25 +00001593
Victor Hernandez3a328652010-01-15 19:04:09 +00001594 if (!isa<Instruction>(V) && !isa<Argument>(V))
Torok Edwin620f2802008-12-16 09:07:36 +00001595 return 0;
Jim Grosbache62b6902010-07-21 21:36:25 +00001596
Victor Hernandez3a328652010-01-15 19:04:09 +00001597 const Function *F = NULL;
1598 if (const Instruction *I = dyn_cast<Instruction>(V))
1599 F = I->getParent()->getParent();
1600 else if (const Argument *A = dyn_cast<Argument>(V))
1601 F = A->getParent();
Jim Grosbache62b6902010-07-21 21:36:25 +00001602
Victor Hernandez3a328652010-01-15 19:04:09 +00001603 for (Function::const_iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
1604 for (BasicBlock::const_iterator BI = (*FI).begin(), BE = (*FI).end();
1605 BI != BE; ++BI)
1606 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1607 if (DDI->getAddress() == V)
1608 return DDI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001609
Chris Lattner099b7792009-12-29 09:22:47 +00001610 return 0;
1611}
Bill Wendlingdc817b62009-05-14 18:26:15 +00001612
Chris Lattner099b7792009-12-29 09:22:47 +00001613bool llvm::getLocationInfo(const Value *V, std::string &DisplayName,
1614 std::string &Type, unsigned &LineNo,
1615 std::string &File, std::string &Dir) {
1616 DICompileUnit Unit;
1617 DIType TypeD;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001618
Chris Lattner099b7792009-12-29 09:22:47 +00001619 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1620 Value *DIGV = findDbgGlobalDeclare(GV);
1621 if (!DIGV) return false;
1622 DIGlobalVariable Var(cast<MDNode>(DIGV));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001623
Chris Lattner099b7792009-12-29 09:22:47 +00001624 StringRef D = Var.getDisplayName();
Devang Patel65dbc902009-11-25 17:36:49 +00001625 if (!D.empty())
Chris Lattner099b7792009-12-29 09:22:47 +00001626 DisplayName = D;
1627 LineNo = Var.getLineNumber();
1628 Unit = Var.getCompileUnit();
1629 TypeD = Var.getType();
1630 } else {
1631 const DbgDeclareInst *DDI = findDbgDeclare(V);
1632 if (!DDI) return false;
1633 DIVariable Var(cast<MDNode>(DDI->getVariable()));
1634
1635 StringRef D = Var.getName();
1636 if (!D.empty())
1637 DisplayName = D;
1638 LineNo = Var.getLineNumber();
1639 Unit = Var.getCompileUnit();
1640 TypeD = Var.getType();
Torok Edwinff7d0e92009-03-10 13:41:26 +00001641 }
Devang Patel13e16b62009-06-26 01:49:18 +00001642
Chris Lattner099b7792009-12-29 09:22:47 +00001643 StringRef T = TypeD.getName();
1644 if (!T.empty())
1645 Type = T;
1646 StringRef F = Unit.getFilename();
1647 if (!F.empty())
1648 File = F;
1649 StringRef D = Unit.getDirectory();
1650 if (!D.empty())
1651 Dir = D;
1652 return true;
1653}
Devang Patel9e529c32009-07-02 01:15:24 +00001654
Chris Lattner099b7792009-12-29 09:22:47 +00001655/// getDISubprogram - Find subprogram that is enclosing this scope.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001656DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
Chris Lattner099b7792009-12-29 09:22:47 +00001657 DIDescriptor D(Scope);
Chris Lattner099b7792009-12-29 09:22:47 +00001658 if (D.isSubprogram())
1659 return DISubprogram(Scope);
Jim Grosbache62b6902010-07-21 21:36:25 +00001660
Chris Lattner099b7792009-12-29 09:22:47 +00001661 if (D.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001662 return getDISubprogram(DILexicalBlock(Scope).getContext());
Jim Grosbache62b6902010-07-21 21:36:25 +00001663
Chris Lattner099b7792009-12-29 09:22:47 +00001664 return DISubprogram();
1665}
Devang Patel193f7202009-11-24 01:14:22 +00001666
Chris Lattner099b7792009-12-29 09:22:47 +00001667/// getDICompositeType - Find underlying composite type.
1668DICompositeType llvm::getDICompositeType(DIType T) {
Chris Lattner099b7792009-12-29 09:22:47 +00001669 if (T.isCompositeType())
Devang Patel2db49d72010-05-07 18:11:54 +00001670 return DICompositeType(T);
Jim Grosbache62b6902010-07-21 21:36:25 +00001671
Chris Lattner099b7792009-12-29 09:22:47 +00001672 if (T.isDerivedType())
Devang Patel2db49d72010-05-07 18:11:54 +00001673 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
Jim Grosbache62b6902010-07-21 21:36:25 +00001674
Chris Lattner099b7792009-12-29 09:22:47 +00001675 return DICompositeType();
Torok Edwin620f2802008-12-16 09:07:36 +00001676}