blob: 77e0ece04b9f3d48393163cd77ce219a53a607a2 [file] [log] [blame]
Chris Lattnera45664f2008-11-10 02:56:27 +00001//===--- DebugInfo.cpp - Debug Information Helper Classes -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the helper classes used to build and interpret debug
11// information in LLVM IR form.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Analysis/DebugInfo.h"
16#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Intrinsics.h"
Torok Edwin620f2802008-12-16 09:07:36 +000019#include "llvm/IntrinsicInst.h"
Chris Lattnera45664f2008-11-10 02:56:27 +000020#include "llvm/Instructions.h"
21#include "llvm/Module.h"
22#include "llvm/Analysis/ValueTracking.h"
Devang Patele4b27562009-08-28 23:24:31 +000023#include "llvm/ADT/SmallPtrSet.h"
Dan Gohman17aa92c2010-07-21 23:38:33 +000024#include "llvm/ADT/SmallString.h"
David Greene0eb5b662009-12-23 19:45:49 +000025#include "llvm/Support/Debug.h"
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000026#include "llvm/Support/Dwarf.h"
Chris Lattnera81d29b2009-08-23 07:33:14 +000027#include "llvm/Support/raw_ostream.h"
Chris Lattnera45664f2008-11-10 02:56:27 +000028using namespace llvm;
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000029using namespace llvm::dwarf;
Chris Lattnera45664f2008-11-10 02:56:27 +000030
31//===----------------------------------------------------------------------===//
32// DIDescriptor
33//===----------------------------------------------------------------------===//
34
Devang Patel5b164b52010-08-02 22:51:46 +000035DIDescriptor::DIDescriptor(const DIFile F) : DbgNode(F.DbgNode) {
36}
37
38DIDescriptor::DIDescriptor(const DISubprogram F) : DbgNode(F.DbgNode) {
39}
40
41DIDescriptor::DIDescriptor(const DILexicalBlock F) : DbgNode(F.DbgNode) {
42}
43
44DIDescriptor::DIDescriptor(const DIVariable F) : DbgNode(F.DbgNode) {
45}
46
47DIDescriptor::DIDescriptor(const DIType F) : DbgNode(F.DbgNode) {
48}
49
Jim Grosbache62b6902010-07-21 21:36:25 +000050StringRef
Devang Patel5ccdd102009-09-29 18:40:58 +000051DIDescriptor::getStringField(unsigned Elt) const {
Devang Patele4b27562009-08-28 23:24:31 +000052 if (DbgNode == 0)
Devang Patel65dbc902009-11-25 17:36:49 +000053 return StringRef();
Chris Lattnera45664f2008-11-10 02:56:27 +000054
Chris Lattner5d0cacd2009-12-31 01:22:29 +000055 if (Elt < DbgNode->getNumOperands())
56 if (MDString *MDS = dyn_cast_or_null<MDString>(DbgNode->getOperand(Elt)))
Devang Patel65dbc902009-11-25 17:36:49 +000057 return MDS->getString();
Daniel Dunbarf612ff62009-09-19 20:40:05 +000058
Devang Patel65dbc902009-11-25 17:36:49 +000059 return StringRef();
Chris Lattnera45664f2008-11-10 02:56:27 +000060}
61
62uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +000063 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +000064 return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +000065
Chris Lattner5d0cacd2009-12-31 01:22:29 +000066 if (Elt < DbgNode->getNumOperands())
67 if (ConstantInt *CI = dyn_cast<ConstantInt>(DbgNode->getOperand(Elt)))
Devang Patele4b27562009-08-28 23:24:31 +000068 return CI->getZExtValue();
Daniel Dunbarf612ff62009-09-19 20:40:05 +000069
Chris Lattnera45664f2008-11-10 02:56:27 +000070 return 0;
71}
72
Chris Lattnera45664f2008-11-10 02:56:27 +000073DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +000074 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +000075 return DIDescriptor();
Bill Wendlingdc817b62009-05-14 18:26:15 +000076
Chris Lattner7a2f3e02010-03-31 05:53:47 +000077 if (Elt < DbgNode->getNumOperands())
Jim Grosbache62b6902010-07-21 21:36:25 +000078 return
79 DIDescriptor(dyn_cast_or_null<const MDNode>(DbgNode->getOperand(Elt)));
Devang Patele4b27562009-08-28 23:24:31 +000080 return DIDescriptor();
Chris Lattnera45664f2008-11-10 02:56:27 +000081}
82
83GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +000084 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +000085 return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +000086
Chris Lattner5d0cacd2009-12-31 01:22:29 +000087 if (Elt < DbgNode->getNumOperands())
88 return dyn_cast_or_null<GlobalVariable>(DbgNode->getOperand(Elt));
Devang Patele4b27562009-08-28 23:24:31 +000089 return 0;
Chris Lattnera45664f2008-11-10 02:56:27 +000090}
91
Stuart Hastings215aa152010-06-11 20:08:44 +000092Function *DIDescriptor::getFunctionField(unsigned Elt) const {
93 if (DbgNode == 0)
94 return 0;
95
96 if (Elt < DbgNode->getNumOperands())
97 return dyn_cast_or_null<Function>(DbgNode->getOperand(Elt));
98 return 0;
99}
100
Chris Lattnerf0908a32009-12-31 03:02:08 +0000101unsigned DIVariable::getNumAddrElements() const {
102 return DbgNode->getNumOperands()-6;
103}
104
105
Chris Lattnera45664f2008-11-10 02:56:27 +0000106//===----------------------------------------------------------------------===//
Devang Patel6ceea332009-08-31 18:49:10 +0000107// Predicates
Chris Lattnera45664f2008-11-10 02:56:27 +0000108//===----------------------------------------------------------------------===//
109
Devang Patel6ceea332009-08-31 18:49:10 +0000110/// isBasicType - Return true if the specified tag is legal for
111/// DIBasicType.
112bool DIDescriptor::isBasicType() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000113 return DbgNode && getTag() == dwarf::DW_TAG_base_type;
Torok Edwinb07fbd92008-12-13 08:25:29 +0000114}
Chris Lattnera45664f2008-11-10 02:56:27 +0000115
Devang Patel6ceea332009-08-31 18:49:10 +0000116/// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
117bool DIDescriptor::isDerivedType() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000118 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000119 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000120 case dwarf::DW_TAG_typedef:
121 case dwarf::DW_TAG_pointer_type:
122 case dwarf::DW_TAG_reference_type:
123 case dwarf::DW_TAG_const_type:
124 case dwarf::DW_TAG_volatile_type:
125 case dwarf::DW_TAG_restrict_type:
126 case dwarf::DW_TAG_member:
127 case dwarf::DW_TAG_inheritance:
128 return true;
129 default:
Devang Patele4b27562009-08-28 23:24:31 +0000130 // CompositeTypes are currently modelled as DerivedTypes.
Devang Patel6ceea332009-08-31 18:49:10 +0000131 return isCompositeType();
Chris Lattnera45664f2008-11-10 02:56:27 +0000132 }
133}
134
Chris Lattnera45664f2008-11-10 02:56:27 +0000135/// isCompositeType - Return true if the specified tag is legal for
136/// DICompositeType.
Devang Patel6ceea332009-08-31 18:49:10 +0000137bool DIDescriptor::isCompositeType() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000138 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000139 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000140 case dwarf::DW_TAG_array_type:
141 case dwarf::DW_TAG_structure_type:
142 case dwarf::DW_TAG_union_type:
143 case dwarf::DW_TAG_enumeration_type:
144 case dwarf::DW_TAG_vector_type:
145 case dwarf::DW_TAG_subroutine_type:
Devang Patel25cb0d72009-03-25 03:52:06 +0000146 case dwarf::DW_TAG_class_type:
Chris Lattnera45664f2008-11-10 02:56:27 +0000147 return true;
148 default:
149 return false;
150 }
151}
152
Chris Lattnera45664f2008-11-10 02:56:27 +0000153/// isVariable - Return true if the specified tag is legal for DIVariable.
Devang Patel6ceea332009-08-31 18:49:10 +0000154bool DIDescriptor::isVariable() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000155 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000156 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000157 case dwarf::DW_TAG_auto_variable:
158 case dwarf::DW_TAG_arg_variable:
159 case dwarf::DW_TAG_return_variable:
160 return true;
161 default:
162 return false;
163 }
164}
165
Devang Patelecbeb1a2009-09-30 22:34:41 +0000166/// isType - Return true if the specified tag is legal for DIType.
167bool DIDescriptor::isType() const {
168 return isBasicType() || isCompositeType() || isDerivedType();
169}
170
Devang Patel6ceea332009-08-31 18:49:10 +0000171/// isSubprogram - Return true if the specified tag is legal for
172/// DISubprogram.
173bool DIDescriptor::isSubprogram() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000174 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
Devang Patel6ceea332009-08-31 18:49:10 +0000175}
176
177/// isGlobalVariable - Return true if the specified tag is legal for
178/// DIGlobalVariable.
179bool DIDescriptor::isGlobalVariable() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000180 return DbgNode && getTag() == dwarf::DW_TAG_variable;
Devang Patel6ceea332009-08-31 18:49:10 +0000181}
182
Devang Patelecbeb1a2009-09-30 22:34:41 +0000183/// isGlobal - Return true if the specified tag is legal for DIGlobal.
184bool DIDescriptor::isGlobal() const {
185 return isGlobalVariable();
186}
187
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000188/// isScope - Return true if the specified tag is one of the scope
Devang Patel43d98b32009-08-31 20:44:45 +0000189/// related tag.
190bool DIDescriptor::isScope() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000191 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000192 switch (getTag()) {
193 case dwarf::DW_TAG_compile_unit:
194 case dwarf::DW_TAG_lexical_block:
195 case dwarf::DW_TAG_subprogram:
196 case dwarf::DW_TAG_namespace:
197 return true;
198 default:
199 break;
Devang Patel43d98b32009-08-31 20:44:45 +0000200 }
201 return false;
202}
Devang Patel6ceea332009-08-31 18:49:10 +0000203
Devang Patelc9f322d2009-08-31 21:34:44 +0000204/// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
205bool DIDescriptor::isCompileUnit() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000206 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
Devang Patelc9f322d2009-08-31 21:34:44 +0000207}
208
Devang Patel7aa81892010-03-08 22:27:22 +0000209/// isFile - Return true if the specified tag is DW_TAG_file_type.
210bool DIDescriptor::isFile() const {
211 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
212}
213
Devang Patel6404e4e2009-12-15 19:16:48 +0000214/// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
215bool DIDescriptor::isNameSpace() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000216 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
Devang Patel6404e4e2009-12-15 19:16:48 +0000217}
218
Devang Patel5e005d82009-08-31 22:00:15 +0000219/// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
220bool DIDescriptor::isLexicalBlock() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000221 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block;
Devang Patel5e005d82009-08-31 22:00:15 +0000222}
223
Devang Patelecbeb1a2009-09-30 22:34:41 +0000224/// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
225bool DIDescriptor::isSubrange() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000226 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
Devang Patelecbeb1a2009-09-30 22:34:41 +0000227}
228
229/// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
230bool DIDescriptor::isEnumerator() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000231 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
Devang Patelecbeb1a2009-09-30 22:34:41 +0000232}
233
Devang Patel6ceea332009-08-31 18:49:10 +0000234//===----------------------------------------------------------------------===//
235// Simple Descriptor Constructors and other Methods
236//===----------------------------------------------------------------------===//
237
Devang Patele9f8f5e2010-05-07 20:54:48 +0000238DIType::DIType(const MDNode *N) : DIScope(N) {
Devang Patel6ceea332009-08-31 18:49:10 +0000239 if (!N) return;
240 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
241 DbgNode = 0;
242 }
243}
244
Devang Patel68afdc32009-01-05 18:33:01 +0000245unsigned DIArray::getNumElements() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000246 if (!DbgNode)
247 return 0;
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000248 return DbgNode->getNumOperands();
Devang Patel68afdc32009-01-05 18:33:01 +0000249}
Chris Lattnera45664f2008-11-10 02:56:27 +0000250
Devang Patelc4999d72009-07-22 18:23:44 +0000251/// replaceAllUsesWith - Replace all uses of debug info referenced by
Dan Gohman872814a2010-07-21 18:54:18 +0000252/// this descriptor.
Devang Patelc4999d72009-07-22 18:23:44 +0000253void DIDerivedType::replaceAllUsesWith(DIDescriptor &D) {
Devang Patel3c91b052010-03-08 20:52:55 +0000254 if (!DbgNode)
Devang Patelc4999d72009-07-22 18:23:44 +0000255 return;
256
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000257 // Since we use a TrackingVH for the node, its easy for clients to manufacture
258 // legitimate situations where they want to replaceAllUsesWith() on something
259 // which, due to uniquing, has merged with the source. We shield clients from
260 // this detail by allowing a value to be replaced with replaceAllUsesWith()
261 // itself.
Devang Pateled66bf52010-05-07 18:19:32 +0000262 if (DbgNode != D) {
Devang Patele9f8f5e2010-05-07 20:54:48 +0000263 MDNode *Node = const_cast<MDNode*>(DbgNode);
264 const MDNode *DN = D;
265 const Value *V = cast_or_null<Value>(DN);
266 Node->replaceAllUsesWith(const_cast<Value*>(V));
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000267 }
Devang Patelc4999d72009-07-22 18:23:44 +0000268}
269
Devang Patelb79b5352009-01-19 23:21:49 +0000270/// Verify - Verify that a compile unit is well formed.
271bool DICompileUnit::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000272 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000273 return false;
Devang Patel65dbc902009-11-25 17:36:49 +0000274 StringRef N = getFilename();
275 if (N.empty())
Bill Wendling0582ae92009-03-13 04:39:26 +0000276 return false;
Devang Patelb79b5352009-01-19 23:21:49 +0000277 // It is possible that directory and produce string is empty.
Bill Wendling0582ae92009-03-13 04:39:26 +0000278 return true;
Devang Patelb79b5352009-01-19 23:21:49 +0000279}
280
281/// Verify - Verify that a type descriptor is well formed.
282bool DIType::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000283 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000284 return false;
Devang Patel3c91b052010-03-08 20:52:55 +0000285 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000286 return false;
287
288 DICompileUnit CU = getCompileUnit();
Devang Patel3c91b052010-03-08 20:52:55 +0000289 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000290 return false;
291 return true;
292}
293
294/// Verify - Verify that a composite type descriptor is well formed.
295bool DICompositeType::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
307/// Verify - Verify that a subprogram descriptor is well formed.
308bool DISubprogram::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000309 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000310 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000311
Devang Patel3c91b052010-03-08 20:52:55 +0000312 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000313 return false;
314
315 DICompileUnit CU = getCompileUnit();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000316 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000317 return false;
318
319 DICompositeType Ty = getType();
Devang Patel3c91b052010-03-08 20:52:55 +0000320 if (!Ty.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000321 return false;
322 return true;
323}
324
325/// Verify - Verify that a global variable descriptor is well formed.
326bool DIGlobalVariable::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000327 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000328 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000329
Devang Patel65dbc902009-11-25 17:36:49 +0000330 if (getDisplayName().empty())
Devang Patel84c73e92009-11-06 17:58:12 +0000331 return false;
332
Devang Patel3c91b052010-03-08 20:52:55 +0000333 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000334 return false;
335
336 DICompileUnit CU = getCompileUnit();
Devang Patel3c91b052010-03-08 20:52:55 +0000337 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000338 return false;
339
340 DIType Ty = getType();
341 if (!Ty.Verify())
342 return false;
343
344 if (!getGlobal())
345 return false;
346
347 return true;
348}
349
350/// Verify - Verify that a variable descriptor is well formed.
351bool DIVariable::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000352 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000353 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000354
Devang Patel3c91b052010-03-08 20:52:55 +0000355 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000356 return false;
357
Devang Patel62077af2010-05-07 21:42:24 +0000358 if (!getCompileUnit().Verify())
359 return false;
360
Devang Patelb79b5352009-01-19 23:21:49 +0000361 DIType Ty = getType();
362 if (!Ty.Verify())
363 return false;
364
Devang Patelb79b5352009-01-19 23:21:49 +0000365 return true;
366}
367
Devang Patel3c91b052010-03-08 20:52:55 +0000368/// Verify - Verify that a location descriptor is well formed.
369bool DILocation::Verify() const {
370 if (!DbgNode)
371 return false;
Jim Grosbache62b6902010-07-21 21:36:25 +0000372
Devang Patel3c91b052010-03-08 20:52:55 +0000373 return DbgNode->getNumOperands() == 4;
374}
375
Devang Patel47e22652010-05-07 23:04:32 +0000376/// Verify - Verify that a namespace descriptor is well formed.
377bool DINameSpace::Verify() const {
378 if (!DbgNode)
379 return false;
380 if (getName().empty())
381 return false;
382 if (!getCompileUnit().Verify())
383 return false;
384 return true;
385}
386
Devang Patel36375ee2009-02-17 21:23:59 +0000387/// getOriginalTypeSize - If this type is derived from a base type then
388/// return base type size.
389uint64_t DIDerivedType::getOriginalTypeSize() const {
Devang Patel61ecbd12009-11-04 23:48:00 +0000390 unsigned Tag = getTag();
391 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
392 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
393 Tag == dwarf::DW_TAG_restrict_type) {
394 DIType BaseType = getTypeDerivedFrom();
Jim Grosbache62b6902010-07-21 21:36:25 +0000395 // If this type is not derived from any type then take conservative
Devang Patel5ebfa2d2009-11-06 18:24:05 +0000396 // approach.
Devang Patel3c91b052010-03-08 20:52:55 +0000397 if (!BaseType.isValid())
Devang Patel5ebfa2d2009-11-06 18:24:05 +0000398 return getSizeInBits();
Devang Patel61ecbd12009-11-04 23:48:00 +0000399 if (BaseType.isDerivedType())
Devang Patel2db49d72010-05-07 18:11:54 +0000400 return DIDerivedType(BaseType).getOriginalTypeSize();
Devang Patel61ecbd12009-11-04 23:48:00 +0000401 else
402 return BaseType.getSizeInBits();
403 }
Jim Grosbache62b6902010-07-21 21:36:25 +0000404
Devang Patel61ecbd12009-11-04 23:48:00 +0000405 return getSizeInBits();
Devang Patel36375ee2009-02-17 21:23:59 +0000406}
Devang Patelb79b5352009-01-19 23:21:49 +0000407
Jim Grosbache62b6902010-07-21 21:36:25 +0000408/// isInlinedFnArgument - Return true if this variable provides debugging
Devang Patel719f6a92010-04-29 20:40:36 +0000409/// information for an inlined function arguments.
410bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
411 assert(CurFn && "Invalid function");
412 if (!getContext().isSubprogram())
413 return false;
Jim Grosbache62b6902010-07-21 21:36:25 +0000414 // This variable is not inlined function argument if its scope
Devang Patel719f6a92010-04-29 20:40:36 +0000415 // does not describe current function.
Devang Patel2db49d72010-05-07 18:11:54 +0000416 return !(DISubprogram(getContext()).describes(CurFn));
Devang Patel719f6a92010-04-29 20:40:36 +0000417}
418
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000419/// describes - Return true if this subprogram provides debugging
420/// information for the function F.
421bool DISubprogram::describes(const Function *F) {
Chris Lattner099b7792009-12-29 09:22:47 +0000422 assert(F && "Invalid function");
Devang Patelffd33cd2010-06-16 06:42:02 +0000423 if (F == getFunction())
424 return true;
Devang Patel65dbc902009-11-25 17:36:49 +0000425 StringRef Name = getLinkageName();
426 if (Name.empty())
Devang Patel5ccdd102009-09-29 18:40:58 +0000427 Name = getName();
Devang Patel65dbc902009-11-25 17:36:49 +0000428 if (F->getName() == Name)
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000429 return true;
430 return false;
431}
432
Jim Grosbache62b6902010-07-21 21:36:25 +0000433unsigned DISubprogram::isOptimized() const {
Devang Patelccff8122010-04-30 19:38:23 +0000434 assert (DbgNode && "Invalid subprogram descriptor!");
435 if (DbgNode->getNumOperands() == 16)
436 return getUnsignedField(15);
437 return 0;
438}
439
Devang Patel65dbc902009-11-25 17:36:49 +0000440StringRef DIScope::getFilename() const {
Devang Patel77bf2952010-03-08 22:02:50 +0000441 if (!DbgNode)
442 return StringRef();
Jim Grosbache62b6902010-07-21 21:36:25 +0000443 if (isLexicalBlock())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000444 return DILexicalBlock(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000445 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000446 return DISubprogram(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000447 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000448 return DICompileUnit(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000449 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000450 return DINameSpace(DbgNode).getFilename();
Devang Patel77bf2952010-03-08 22:02:50 +0000451 if (isType())
452 return DIType(DbgNode).getFilename();
Devang Patel7aa81892010-03-08 22:27:22 +0000453 if (isFile())
454 return DIFile(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000455 assert(0 && "Invalid DIScope!");
Devang Patel65dbc902009-11-25 17:36:49 +0000456 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000457}
458
Devang Patel65dbc902009-11-25 17:36:49 +0000459StringRef DIScope::getDirectory() const {
Devang Patel77bf2952010-03-08 22:02:50 +0000460 if (!DbgNode)
461 return StringRef();
Jim Grosbache62b6902010-07-21 21:36:25 +0000462 if (isLexicalBlock())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000463 return DILexicalBlock(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000464 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000465 return DISubprogram(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000466 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000467 return DICompileUnit(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000468 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000469 return DINameSpace(DbgNode).getDirectory();
Devang Patel77bf2952010-03-08 22:02:50 +0000470 if (isType())
471 return DIType(DbgNode).getDirectory();
Devang Patel7aa81892010-03-08 22:27:22 +0000472 if (isFile())
473 return DIFile(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000474 assert(0 && "Invalid DIScope!");
Devang Patel65dbc902009-11-25 17:36:49 +0000475 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000476}
477
Chris Lattnera45664f2008-11-10 02:56:27 +0000478//===----------------------------------------------------------------------===//
Devang Patel7136a652009-07-01 22:10:23 +0000479// DIDescriptor: dump routines for all descriptors.
480//===----------------------------------------------------------------------===//
481
482
Dan Gohman50404362010-05-07 15:30:29 +0000483/// print - Print descriptor.
484void DIDescriptor::print(raw_ostream &OS) const {
485 OS << "[" << dwarf::TagString(getTag()) << "] ";
486 OS.write_hex((intptr_t) &*DbgNode) << ']';
Devang Patel7136a652009-07-01 22:10:23 +0000487}
488
Dan Gohman50404362010-05-07 15:30:29 +0000489/// print - Print compile unit.
490void DICompileUnit::print(raw_ostream &OS) const {
Devang Patel7136a652009-07-01 22:10:23 +0000491 if (getLanguage())
Dan Gohman50404362010-05-07 15:30:29 +0000492 OS << " [" << dwarf::LanguageString(getLanguage()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000493
Dan Gohmanfaa19c32010-05-10 20:07:44 +0000494 OS << " [" << getDirectory() << "/" << getFilename() << "]";
Devang Patel7136a652009-07-01 22:10:23 +0000495}
496
Dan Gohman50404362010-05-07 15:30:29 +0000497/// print - Print type.
498void DIType::print(raw_ostream &OS) const {
Devang Patel3c91b052010-03-08 20:52:55 +0000499 if (!DbgNode) return;
Devang Patel7136a652009-07-01 22:10:23 +0000500
Devang Patel65dbc902009-11-25 17:36:49 +0000501 StringRef Res = getName();
502 if (!Res.empty())
Dan Gohman50404362010-05-07 15:30:29 +0000503 OS << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000504
505 unsigned Tag = getTag();
Dan Gohman50404362010-05-07 15:30:29 +0000506 OS << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000507
508 // TODO : Print context
Dan Gohmanc014d092010-05-07 16:17:22 +0000509 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000510 OS << " ["
Dan Gohman9a7063e2010-05-07 16:39:27 +0000511 << "line " << getLineNumber() << ", "
512 << getSizeInBits() << " bits, "
513 << getAlignInBits() << " bit alignment, "
514 << getOffsetInBits() << " bit offset"
Chris Lattnera81d29b2009-08-23 07:33:14 +0000515 << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000516
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000517 if (isPrivate())
Dan Gohman50404362010-05-07 15:30:29 +0000518 OS << " [private] ";
Devang Patel7136a652009-07-01 22:10:23 +0000519 else if (isProtected())
Dan Gohman50404362010-05-07 15:30:29 +0000520 OS << " [protected] ";
Devang Patel7136a652009-07-01 22:10:23 +0000521
522 if (isForwardDecl())
Dan Gohman50404362010-05-07 15:30:29 +0000523 OS << " [fwd] ";
Devang Patel7136a652009-07-01 22:10:23 +0000524
Devang Patel6ceea332009-08-31 18:49:10 +0000525 if (isBasicType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000526 DIBasicType(DbgNode).print(OS);
Devang Patel6ceea332009-08-31 18:49:10 +0000527 else if (isDerivedType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000528 DIDerivedType(DbgNode).print(OS);
Devang Patel6ceea332009-08-31 18:49:10 +0000529 else if (isCompositeType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000530 DICompositeType(DbgNode).print(OS);
Devang Patel7136a652009-07-01 22:10:23 +0000531 else {
Dan Gohman50404362010-05-07 15:30:29 +0000532 OS << "Invalid DIType\n";
Devang Patel7136a652009-07-01 22:10:23 +0000533 return;
534 }
535
Dan Gohman50404362010-05-07 15:30:29 +0000536 OS << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000537}
538
Dan Gohman50404362010-05-07 15:30:29 +0000539/// print - Print basic type.
540void DIBasicType::print(raw_ostream &OS) const {
541 OS << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000542}
543
Dan Gohman50404362010-05-07 15:30:29 +0000544/// print - Print derived type.
545void DIDerivedType::print(raw_ostream &OS) const {
Dan Gohmanc014d092010-05-07 16:17:22 +0000546 OS << "\n\t Derived From: "; getTypeDerivedFrom().print(OS);
Devang Patel7136a652009-07-01 22:10:23 +0000547}
548
Dan Gohman50404362010-05-07 15:30:29 +0000549/// print - Print composite type.
550void DICompositeType::print(raw_ostream &OS) const {
Devang Patel7136a652009-07-01 22:10:23 +0000551 DIArray A = getTypeArray();
Dan Gohman50404362010-05-07 15:30:29 +0000552 OS << " [" << A.getNumElements() << " elements]";
Devang Patel7136a652009-07-01 22:10:23 +0000553}
554
Dan Gohman50404362010-05-07 15:30:29 +0000555/// print - Print subprogram.
556void DISubprogram::print(raw_ostream &OS) const {
Devang Patel65dbc902009-11-25 17:36:49 +0000557 StringRef Res = getName();
558 if (!Res.empty())
Dan Gohman50404362010-05-07 15:30:29 +0000559 OS << " [" << Res << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000560
561 unsigned Tag = getTag();
Dan Gohman50404362010-05-07 15:30:29 +0000562 OS << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000563
564 // TODO : Print context
Dan Gohmanc014d092010-05-07 16:17:22 +0000565 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000566 OS << " [" << getLineNumber() << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000567
568 if (isLocalToUnit())
Dan Gohman50404362010-05-07 15:30:29 +0000569 OS << " [local] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000570
571 if (isDefinition())
Dan Gohman50404362010-05-07 15:30:29 +0000572 OS << " [def] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000573
Dan Gohman50404362010-05-07 15:30:29 +0000574 OS << "\n";
575}
576
577/// print - Print global variable.
578void DIGlobalVariable::print(raw_ostream &OS) const {
579 OS << " [";
Devang Patela49d8772010-05-07 23:19:07 +0000580 StringRef Res = getName();
581 if (!Res.empty())
582 OS << " [" << Res << "] ";
583
584 unsigned Tag = getTag();
585 OS << " [" << dwarf::TagString(Tag) << "] ";
586
587 // TODO : Print context
588 getCompileUnit().print(OS);
589 OS << " [" << getLineNumber() << "] ";
590
591 if (isLocalToUnit())
592 OS << " [local] ";
593
594 if (isDefinition())
595 OS << " [def] ";
596
597 if (isGlobalVariable())
598 DIGlobalVariable(DbgNode).print(OS);
599 OS << "]\n";
Dan Gohman50404362010-05-07 15:30:29 +0000600}
601
602/// print - Print variable.
603void DIVariable::print(raw_ostream &OS) const {
604 StringRef Res = getName();
605 if (!Res.empty())
606 OS << " [" << Res << "] ";
607
Dan Gohmanc014d092010-05-07 16:17:22 +0000608 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000609 OS << " [" << getLineNumber() << "] ";
Dan Gohmanc014d092010-05-07 16:17:22 +0000610 getType().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000611 OS << "\n";
612
613 // FIXME: Dump complex addresses
614}
615
616/// dump - Print descriptor to dbgs() with a newline.
617void DIDescriptor::dump() const {
618 print(dbgs()); dbgs() << '\n';
619}
620
621/// dump - Print compile unit to dbgs() with a newline.
622void DICompileUnit::dump() const {
623 print(dbgs()); dbgs() << '\n';
624}
625
626/// dump - Print type to dbgs() with a newline.
627void DIType::dump() const {
628 print(dbgs()); dbgs() << '\n';
629}
630
631/// dump - Print basic type to dbgs() with a newline.
632void DIBasicType::dump() const {
633 print(dbgs()); dbgs() << '\n';
634}
635
636/// dump - Print derived type to dbgs() with a newline.
637void DIDerivedType::dump() const {
638 print(dbgs()); dbgs() << '\n';
639}
640
641/// dump - Print composite type to dbgs() with a newline.
642void DICompositeType::dump() const {
643 print(dbgs()); dbgs() << '\n';
644}
645
Dan Gohman50404362010-05-07 15:30:29 +0000646/// dump - Print subprogram to dbgs() with a newline.
647void DISubprogram::dump() const {
648 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000649}
650
651/// dump - Print global variable.
652void DIGlobalVariable::dump() const {
Dan Gohman50404362010-05-07 15:30:29 +0000653 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000654}
655
656/// dump - Print variable.
657void DIVariable::dump() const {
Dan Gohman50404362010-05-07 15:30:29 +0000658 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000659}
660
661//===----------------------------------------------------------------------===//
Chris Lattnera45664f2008-11-10 02:56:27 +0000662// DIFactory: Basic Helpers
663//===----------------------------------------------------------------------===//
664
Bill Wendlingdc817b62009-05-14 18:26:15 +0000665DIFactory::DIFactory(Module &m)
Victor Hernandez06203122010-01-23 00:03:28 +0000666 : M(m), VMContext(M.getContext()), DeclareFn(0), ValueFn(0) {}
Chris Lattner497a7a82008-11-10 04:10:34 +0000667
Chris Lattnera45664f2008-11-10 02:56:27 +0000668Constant *DIFactory::GetTagConstant(unsigned TAG) {
Devang Patel6906ba52009-01-20 19:22:03 +0000669 assert((TAG & LLVMDebugVersionMask) == 0 &&
Chris Lattnera45664f2008-11-10 02:56:27 +0000670 "Tag too large for debug encoding!");
Owen Anderson1d0be152009-08-13 21:58:54 +0000671 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
Chris Lattnera45664f2008-11-10 02:56:27 +0000672}
673
Chris Lattnera45664f2008-11-10 02:56:27 +0000674//===----------------------------------------------------------------------===//
675// DIFactory: Primary Constructors
676//===----------------------------------------------------------------------===//
677
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000678/// GetOrCreateArray - Create an descriptor for an array of descriptors.
Chris Lattnera45664f2008-11-10 02:56:27 +0000679/// This implicitly uniques the arrays created.
680DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
Devang Patele4b27562009-08-28 23:24:31 +0000681 SmallVector<Value*, 16> Elts;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000682
Devang Patele4b27562009-08-28 23:24:31 +0000683 if (NumTys == 0)
684 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)));
685 else
686 for (unsigned i = 0; i != NumTys; ++i)
Devang Patel2db49d72010-05-07 18:11:54 +0000687 Elts.push_back(Tys[i]);
Devang Patel82459882009-08-26 05:01:18 +0000688
Devang Patele4b27562009-08-28 23:24:31 +0000689 return DIArray(MDNode::get(VMContext,Elts.data(), Elts.size()));
Chris Lattnera45664f2008-11-10 02:56:27 +0000690}
691
692/// GetOrCreateSubrange - Create a descriptor for a value range. This
693/// implicitly uniques the values returned.
694DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
Devang Patele4b27562009-08-28 23:24:31 +0000695 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000696 GetTagConstant(dwarf::DW_TAG_subrange_type),
Owen Anderson1d0be152009-08-13 21:58:54 +0000697 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
698 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
Chris Lattnera45664f2008-11-10 02:56:27 +0000699 };
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000700
Devang Patele4b27562009-08-28 23:24:31 +0000701 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000702}
703
704
705
706/// CreateCompileUnit - Create a new descriptor for the specified compile
707/// unit. Note that this does not unique compile units within the module.
708DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
Devang Patel65dbc902009-11-25 17:36:49 +0000709 StringRef Filename,
710 StringRef Directory,
711 StringRef Producer,
Devang Pateldd9db662009-01-30 18:20:31 +0000712 bool isMain,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000713 bool isOptimized,
Devang Patel65dbc902009-11-25 17:36:49 +0000714 StringRef Flags,
Devang Patel13319ce2009-02-17 22:43:44 +0000715 unsigned RunTimeVer) {
Devang Patele4b27562009-08-28 23:24:31 +0000716 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000717 GetTagConstant(dwarf::DW_TAG_compile_unit),
Devang Patele4b27562009-08-28 23:24:31 +0000718 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Owen Anderson1d0be152009-08-13 21:58:54 +0000719 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
Devang Patele4b27562009-08-28 23:24:31 +0000720 MDString::get(VMContext, Filename),
721 MDString::get(VMContext, Directory),
722 MDString::get(VMContext, Producer),
Owen Anderson1d0be152009-08-13 21:58:54 +0000723 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
724 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
Devang Patele4b27562009-08-28 23:24:31 +0000725 MDString::get(VMContext, Flags),
Owen Anderson1d0be152009-08-13 21:58:54 +0000726 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
Chris Lattnera45664f2008-11-10 02:56:27 +0000727 };
Devang Patele4b27562009-08-28 23:24:31 +0000728
729 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000730}
731
Devang Patel7aa81892010-03-08 22:27:22 +0000732/// CreateFile - Create a new descriptor for the specified file.
733DIFile DIFactory::CreateFile(StringRef Filename,
734 StringRef Directory,
735 DICompileUnit CU) {
736 Value *Elts[] = {
737 GetTagConstant(dwarf::DW_TAG_file_type),
738 MDString::get(VMContext, Filename),
739 MDString::get(VMContext, Directory),
Devang Patel2db49d72010-05-07 18:11:54 +0000740 CU
Devang Patel7aa81892010-03-08 22:27:22 +0000741 };
742
743 return DIFile(MDNode::get(VMContext, &Elts[0], 4));
744}
745
Chris Lattnera45664f2008-11-10 02:56:27 +0000746/// CreateEnumerator - Create a single enumerator value.
Devang Patel65dbc902009-11-25 17:36:49 +0000747DIEnumerator DIFactory::CreateEnumerator(StringRef Name, uint64_t Val){
Devang Patele4b27562009-08-28 23:24:31 +0000748 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000749 GetTagConstant(dwarf::DW_TAG_enumerator),
Devang Patele4b27562009-08-28 23:24:31 +0000750 MDString::get(VMContext, Name),
Owen Anderson1d0be152009-08-13 21:58:54 +0000751 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
Chris Lattnera45664f2008-11-10 02:56:27 +0000752 };
Devang Patele4b27562009-08-28 23:24:31 +0000753 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000754}
755
756
757/// CreateBasicType - Create a basic type like int, float, etc.
758DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000759 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000760 DIFile F,
Chris Lattnera45664f2008-11-10 02:56:27 +0000761 unsigned LineNumber,
762 uint64_t SizeInBits,
763 uint64_t AlignInBits,
764 uint64_t OffsetInBits, unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000765 unsigned Encoding) {
Devang Patele4b27562009-08-28 23:24:31 +0000766 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000767 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patel2db49d72010-05-07 18:11:54 +0000768 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000769 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000770 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000771 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
772 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
773 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
774 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
775 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
776 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
Chris Lattnera45664f2008-11-10 02:56:27 +0000777 };
Devang Patele4b27562009-08-28 23:24:31 +0000778 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000779}
780
Devang Patelac16d442009-10-26 16:54:35 +0000781
782/// CreateBasicType - Create a basic type like int, float, etc.
783DIBasicType DIFactory::CreateBasicTypeEx(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000784 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000785 DIFile F,
Devang Patelac16d442009-10-26 16:54:35 +0000786 unsigned LineNumber,
787 Constant *SizeInBits,
788 Constant *AlignInBits,
789 Constant *OffsetInBits, unsigned Flags,
790 unsigned Encoding) {
791 Value *Elts[] = {
792 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patel2db49d72010-05-07 18:11:54 +0000793 Context,
Devang Patelac16d442009-10-26 16:54:35 +0000794 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000795 F,
Devang Patelac16d442009-10-26 16:54:35 +0000796 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
797 SizeInBits,
798 AlignInBits,
799 OffsetInBits,
800 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
801 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
802 };
803 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
804}
805
Devang Patelb4645642010-02-06 01:02:37 +0000806/// CreateArtificialType - Create a new DIType with "artificial" flag set.
807DIType DIFactory::CreateArtificialType(DIType Ty) {
808 if (Ty.isArtificial())
809 return Ty;
810
811 SmallVector<Value *, 9> Elts;
Devang Patel2db49d72010-05-07 18:11:54 +0000812 MDNode *N = Ty;
Devang Patelb4645642010-02-06 01:02:37 +0000813 assert (N && "Unexpected input DIType!");
814 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
815 if (Value *V = N->getOperand(i))
816 Elts.push_back(V);
817 else
818 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
819 }
820
821 unsigned CurFlags = Ty.getFlags();
822 CurFlags = CurFlags | DIType::FlagArtificial;
823
824 // Flags are stored at this slot.
825 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
826
827 return DIType(MDNode::get(VMContext, Elts.data(), Elts.size()));
828}
Devang Patelac16d442009-10-26 16:54:35 +0000829
Chris Lattnera45664f2008-11-10 02:56:27 +0000830/// CreateDerivedType - Create a derived type like const qualified type,
831/// pointer, typedef, etc.
832DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
833 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000834 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000835 DIFile F,
Chris Lattnera45664f2008-11-10 02:56:27 +0000836 unsigned LineNumber,
837 uint64_t SizeInBits,
838 uint64_t AlignInBits,
839 uint64_t OffsetInBits,
840 unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000841 DIType DerivedFrom) {
Devang Patele4b27562009-08-28 23:24:31 +0000842 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000843 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000844 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000845 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000846 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000847 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
848 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
849 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
850 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
851 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000852 DerivedFrom,
Chris Lattnera45664f2008-11-10 02:56:27 +0000853 };
Devang Patele4b27562009-08-28 23:24:31 +0000854 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000855}
856
Devang Patelac16d442009-10-26 16:54:35 +0000857
858/// CreateDerivedType - Create a derived type like const qualified type,
859/// pointer, typedef, etc.
860DIDerivedType DIFactory::CreateDerivedTypeEx(unsigned Tag,
861 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000862 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000863 DIFile F,
Devang Patelac16d442009-10-26 16:54:35 +0000864 unsigned LineNumber,
865 Constant *SizeInBits,
866 Constant *AlignInBits,
867 Constant *OffsetInBits,
868 unsigned Flags,
869 DIType DerivedFrom) {
870 Value *Elts[] = {
871 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000872 Context,
Devang Patelac16d442009-10-26 16:54:35 +0000873 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000874 F,
Devang Patelac16d442009-10-26 16:54:35 +0000875 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
876 SizeInBits,
877 AlignInBits,
878 OffsetInBits,
879 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000880 DerivedFrom,
Devang Patelac16d442009-10-26 16:54:35 +0000881 };
882 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
883}
884
885
Chris Lattnera45664f2008-11-10 02:56:27 +0000886/// CreateCompositeType - Create a composite type like array, struct, etc.
887DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
888 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000889 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000890 DIFile F,
Chris Lattnera45664f2008-11-10 02:56:27 +0000891 unsigned LineNumber,
892 uint64_t SizeInBits,
893 uint64_t AlignInBits,
894 uint64_t OffsetInBits,
895 unsigned Flags,
896 DIType DerivedFrom,
Devang Patel13319ce2009-02-17 22:43:44 +0000897 DIArray Elements,
Devang Patel0fd7f9d2010-01-26 21:14:59 +0000898 unsigned RuntimeLang,
899 MDNode *ContainingType) {
Owen Andersone277fed2009-07-07 16:31:25 +0000900
Devang Patele4b27562009-08-28 23:24:31 +0000901 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000902 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000903 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000904 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000905 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000906 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
907 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
908 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
909 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
910 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000911 DerivedFrom,
912 Elements,
Devang Patel0fd7f9d2010-01-26 21:14:59 +0000913 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
914 ContainingType
Chris Lattnera45664f2008-11-10 02:56:27 +0000915 };
Devang Patel0fd7f9d2010-01-26 21:14:59 +0000916 return DICompositeType(MDNode::get(VMContext, &Elts[0], 13));
Chris Lattnera45664f2008-11-10 02:56:27 +0000917}
918
919
Devang Patelac16d442009-10-26 16:54:35 +0000920/// CreateCompositeType - Create a composite type like array, struct, etc.
921DICompositeType DIFactory::CreateCompositeTypeEx(unsigned Tag,
922 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000923 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000924 DIFile F,
Devang Patelac16d442009-10-26 16:54:35 +0000925 unsigned LineNumber,
926 Constant *SizeInBits,
927 Constant *AlignInBits,
928 Constant *OffsetInBits,
929 unsigned Flags,
930 DIType DerivedFrom,
931 DIArray Elements,
932 unsigned RuntimeLang) {
933
934 Value *Elts[] = {
935 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000936 Context,
Devang Patelac16d442009-10-26 16:54:35 +0000937 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000938 F,
Devang Patelac16d442009-10-26 16:54:35 +0000939 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
940 SizeInBits,
941 AlignInBits,
942 OffsetInBits,
943 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000944 DerivedFrom,
945 Elements,
Devang Patelac16d442009-10-26 16:54:35 +0000946 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
947 };
948 return DICompositeType(MDNode::get(VMContext, &Elts[0], 12));
949}
950
951
Chris Lattnera45664f2008-11-10 02:56:27 +0000952/// CreateSubprogram - Create a new descriptor for the specified subprogram.
953/// See comments in DISubprogram for descriptions of these fields. This
954/// method does not unique the generated descriptors.
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000955DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000956 StringRef Name,
957 StringRef DisplayName,
958 StringRef LinkageName,
Devang Patel4b945502010-03-09 00:44:10 +0000959 DIFile F,
Devang Patel2e369932010-01-23 00:26:28 +0000960 unsigned LineNo, DIType Ty,
Chris Lattnera45664f2008-11-10 02:56:27 +0000961 bool isLocalToUnit,
Devang Patel5d11eb02009-12-03 19:11:07 +0000962 bool isDefinition,
963 unsigned VK, unsigned VIndex,
Devang Patel4e0d19d2010-02-03 19:57:19 +0000964 DIType ContainingType,
Devang Patelccff8122010-04-30 19:38:23 +0000965 bool isArtificial,
Stuart Hastings215aa152010-06-11 20:08:44 +0000966 bool isOptimized,
967 Function *Fn) {
Devang Patel854967e2008-12-17 22:39:29 +0000968
Devang Patele4b27562009-08-28 23:24:31 +0000969 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000970 GetTagConstant(dwarf::DW_TAG_subprogram),
Devang Patele4b27562009-08-28 23:24:31 +0000971 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Devang Patel2db49d72010-05-07 18:11:54 +0000972 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000973 MDString::get(VMContext, Name),
974 MDString::get(VMContext, DisplayName),
975 MDString::get(VMContext, LinkageName),
Devang Patel2db49d72010-05-07 18:11:54 +0000976 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000977 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2db49d72010-05-07 18:11:54 +0000978 Ty,
Owen Anderson1d0be152009-08-13 21:58:54 +0000979 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
Devang Patel5d11eb02009-12-03 19:11:07 +0000980 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
981 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
982 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
Devang Patel2db49d72010-05-07 18:11:54 +0000983 ContainingType,
Devang Patelccff8122010-04-30 19:38:23 +0000984 ConstantInt::get(Type::getInt1Ty(VMContext), isArtificial),
Stuart Hastings215aa152010-06-11 20:08:44 +0000985 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
986 Fn
Chris Lattnera45664f2008-11-10 02:56:27 +0000987 };
Devang Patelfd5fdc32010-06-28 05:53:08 +0000988 MDNode *Node = MDNode::get(VMContext, &Elts[0], 17);
989
990 // Create a named metadata so that we do not lose this mdnode.
991 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
992 NMD->addOperand(Node);
993 return DISubprogram(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +0000994}
995
Devang Patele3a18de2009-12-01 23:09:02 +0000996/// CreateSubprogramDefinition - Create new subprogram descriptor for the
Jim Grosbache62b6902010-07-21 21:36:25 +0000997/// given declaration.
998DISubprogram DIFactory::CreateSubprogramDefinition(DISubprogram &SPDeclaration){
Devang Patele3a18de2009-12-01 23:09:02 +0000999 if (SPDeclaration.isDefinition())
Devang Patel2db49d72010-05-07 18:11:54 +00001000 return DISubprogram(SPDeclaration);
Devang Patele3a18de2009-12-01 23:09:02 +00001001
Devang Patel2db49d72010-05-07 18:11:54 +00001002 MDNode *DeclNode = SPDeclaration;
Devang Patele3a18de2009-12-01 23:09:02 +00001003 Value *Elts[] = {
1004 GetTagConstant(dwarf::DW_TAG_subprogram),
1005 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001006 DeclNode->getOperand(2), // Context
1007 DeclNode->getOperand(3), // Name
1008 DeclNode->getOperand(4), // DisplayName
1009 DeclNode->getOperand(5), // LinkageName
1010 DeclNode->getOperand(6), // CompileUnit
1011 DeclNode->getOperand(7), // LineNo
1012 DeclNode->getOperand(8), // Type
1013 DeclNode->getOperand(9), // isLocalToUnit
Stuart Hastings6d56b9f2010-06-05 00:39:29 +00001014 ConstantInt::get(Type::getInt1Ty(VMContext), true),
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001015 DeclNode->getOperand(11), // Virtuality
1016 DeclNode->getOperand(12), // VIndex
Devang Patel4e0d19d2010-02-03 19:57:19 +00001017 DeclNode->getOperand(13), // Containting Type
Devang Patelccff8122010-04-30 19:38:23 +00001018 DeclNode->getOperand(14), // isArtificial
Devang Patelacc6efa2010-06-27 21:04:31 +00001019 DeclNode->getOperand(15), // isOptimized
1020 SPDeclaration.getFunction()
Devang Patele3a18de2009-12-01 23:09:02 +00001021 };
Devang Patelfd5fdc32010-06-28 05:53:08 +00001022 MDNode *Node =MDNode::get(VMContext, &Elts[0], 16);
1023
1024 // Create a named metadata so that we do not lose this mdnode.
1025 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
1026 NMD->addOperand(Node);
1027 return DISubprogram(Node);
Devang Patele3a18de2009-12-01 23:09:02 +00001028}
1029
Chris Lattnera45664f2008-11-10 02:56:27 +00001030/// CreateGlobalVariable - Create a new descriptor for the specified global.
1031DIGlobalVariable
Devang Patel65dbc902009-11-25 17:36:49 +00001032DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
1033 StringRef DisplayName,
1034 StringRef LinkageName,
Devang Patel4b945502010-03-09 00:44:10 +00001035 DIFile F,
Devang Patel2e369932010-01-23 00:26:28 +00001036 unsigned LineNo, DIType Ty,bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +00001037 bool isDefinition, llvm::GlobalVariable *Val) {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001038 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001039 GetTagConstant(dwarf::DW_TAG_variable),
Devang Patele4b27562009-08-28 23:24:31 +00001040 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Devang Patel2db49d72010-05-07 18:11:54 +00001041 Context,
Devang Patele4b27562009-08-28 23:24:31 +00001042 MDString::get(VMContext, Name),
1043 MDString::get(VMContext, DisplayName),
1044 MDString::get(VMContext, LinkageName),
Devang Patel2db49d72010-05-07 18:11:54 +00001045 F,
Owen Anderson1d0be152009-08-13 21:58:54 +00001046 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001047 Ty,
Owen Anderson1d0be152009-08-13 21:58:54 +00001048 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1049 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
Devang Patele4b27562009-08-28 23:24:31 +00001050 Val
Chris Lattnera45664f2008-11-10 02:56:27 +00001051 };
Devang Patele4b27562009-08-28 23:24:31 +00001052
1053 Value *const *Vs = &Elts[0];
1054 MDNode *Node = MDNode::get(VMContext,Vs, 12);
1055
1056 // Create a named metadata so that we do not lose this mdnode.
1057 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001058 NMD->addOperand(Node);
Devang Patele4b27562009-08-28 23:24:31 +00001059
1060 return DIGlobalVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +00001061}
1062
1063
1064/// CreateVariable - Create a new descriptor for the specified variable.
1065DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +00001066 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +00001067 DIFile F,
1068 unsigned LineNo,
Devang Patel6ed0ce32010-05-20 20:35:24 +00001069 DIType Ty, bool AlwaysPreserve) {
Devang Patele4b27562009-08-28 23:24:31 +00001070 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001071 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +00001072 Context,
Devang Patele4b27562009-08-28 23:24:31 +00001073 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +00001074 F,
Owen Anderson1d0be152009-08-13 21:58:54 +00001075 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001076 Ty,
Chris Lattnera45664f2008-11-10 02:56:27 +00001077 };
Devang Patel98e1cac2010-05-14 21:01:35 +00001078 MDNode *Node = MDNode::get(VMContext, &Elts[0], 6);
Devang Patel6ed0ce32010-05-20 20:35:24 +00001079 if (AlwaysPreserve) {
1080 // The optimizer may remove local variable. If there is an interest
1081 // to preserve variable info in such situation then stash it in a
1082 // named mdnode.
Devang Patel2f7d5292010-06-16 00:53:55 +00001083 DISubprogram Fn(getDISubprogram(Context));
Devang Patel10de3bb2010-06-21 18:36:58 +00001084 StringRef FName = "fn";
1085 if (Fn.getFunction())
1086 FName = Fn.getFunction()->getName();
Devang Patel10de3bb2010-06-21 18:36:58 +00001087 char One = '\1';
1088 if (FName.startswith(StringRef(&One, 1)))
1089 FName = FName.substr(1);
Dan Gohman17aa92c2010-07-21 23:38:33 +00001090
1091 SmallString<32> Out;
1092 NamedMDNode *FnLocals =
1093 M.getOrInsertNamedMetadata(Twine("llvm.dbg.lv.", FName).toStringRef(Out));
Devang Patel2f7d5292010-06-16 00:53:55 +00001094 FnLocals->addOperand(Node);
Devang Patel98e1cac2010-05-14 21:01:35 +00001095 }
1096 return DIVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +00001097}
1098
1099
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001100/// CreateComplexVariable - Create a new descriptor for the specified variable
1101/// which has a complex address expression for its address.
1102DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
1103 const std::string &Name,
Devang Patel4b945502010-03-09 00:44:10 +00001104 DIFile F,
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001105 unsigned LineNo,
Jim Grosbache62b6902010-07-21 21:36:25 +00001106 DIType Ty,
Devang Patel2e369932010-01-23 00:26:28 +00001107 SmallVector<Value *, 9> &addr) {
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001108 SmallVector<Value *, 9> Elts;
1109 Elts.push_back(GetTagConstant(Tag));
Devang Patel2db49d72010-05-07 18:11:54 +00001110 Elts.push_back(Context);
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001111 Elts.push_back(MDString::get(VMContext, Name));
Devang Patel2db49d72010-05-07 18:11:54 +00001112 Elts.push_back(F);
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001113 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
Devang Patel2db49d72010-05-07 18:11:54 +00001114 Elts.push_back(Ty);
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001115 Elts.insert(Elts.end(), addr.begin(), addr.end());
1116
1117 return DIVariable(MDNode::get(VMContext, &Elts[0], 6+addr.size()));
1118}
1119
1120
Chris Lattnera45664f2008-11-10 02:56:27 +00001121/// CreateBlock - This creates a descriptor for a lexical block with the
Owen Anderson99035272009-07-07 17:12:53 +00001122/// specified parent VMContext.
Devang Patel3d821aa2010-02-16 21:39:34 +00001123DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context,
Stuart Hastings0db42712010-07-19 23:56:30 +00001124 DIFile F, unsigned LineNo,
1125 unsigned Col) {
1126 // Defeat MDNode uniqing for lexical blocks.
1127 static unsigned int unique_id = 0;
Devang Patele4b27562009-08-28 23:24:31 +00001128 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001129 GetTagConstant(dwarf::DW_TAG_lexical_block),
Devang Patel2db49d72010-05-07 18:11:54 +00001130 Context,
Devang Patel3d821aa2010-02-16 21:39:34 +00001131 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Stuart Hastings0db42712010-07-19 23:56:30 +00001132 ConstantInt::get(Type::getInt32Ty(VMContext), Col),
1133 F,
1134 ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
Chris Lattnera45664f2008-11-10 02:56:27 +00001135 };
Stuart Hastings0db42712010-07-19 23:56:30 +00001136 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 6));
Chris Lattnera45664f2008-11-10 02:56:27 +00001137}
1138
Devang Patel6404e4e2009-12-15 19:16:48 +00001139/// CreateNameSpace - This creates new descriptor for a namespace
1140/// with the specified parent context.
1141DINameSpace DIFactory::CreateNameSpace(DIDescriptor Context, StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +00001142 DIFile F,
Devang Patel6404e4e2009-12-15 19:16:48 +00001143 unsigned LineNo) {
1144 Value *Elts[] = {
1145 GetTagConstant(dwarf::DW_TAG_namespace),
Devang Patel2db49d72010-05-07 18:11:54 +00001146 Context,
Devang Patel6404e4e2009-12-15 19:16:48 +00001147 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +00001148 F,
Devang Patel6404e4e2009-12-15 19:16:48 +00001149 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1150 };
1151 return DINameSpace(MDNode::get(VMContext, &Elts[0], 5));
1152}
1153
Devang Patelf98d8fe2009-09-01 01:14:15 +00001154/// CreateLocation - Creates a debug info location.
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001155DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
Daniel Dunbara279bc32009-09-20 02:20:51 +00001156 DIScope S, DILocation OrigLoc) {
Devang Patelf98d8fe2009-09-01 01:14:15 +00001157 Value *Elts[] = {
1158 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1159 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001160 S,
1161 OrigLoc,
Devang Patelf98d8fe2009-09-01 01:14:15 +00001162 };
1163 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1164}
1165
Chris Lattnera45664f2008-11-10 02:56:27 +00001166//===----------------------------------------------------------------------===//
1167// DIFactory: Routines for inserting code into a function
1168//===----------------------------------------------------------------------===//
1169
Chris Lattnera45664f2008-11-10 02:56:27 +00001170/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Devang Patel6daf99b2009-11-10 22:05:35 +00001171Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001172 Instruction *InsertBefore) {
Victor Hernandez4cf292a2010-01-26 02:07:38 +00001173 assert(Storage && "no storage passed to dbg.declare");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001174 assert(D.Verify() && "empty DIVariable passed to dbg.declare");
Chris Lattnera45664f2008-11-10 02:56:27 +00001175 if (!DeclareFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +00001176 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1177
Victor Hernandez756462b2010-01-18 20:42:09 +00001178 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
Devang Patel2db49d72010-05-07 18:11:54 +00001179 D };
Devang Patel6daf99b2009-11-10 22:05:35 +00001180 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
Mike Stumpe4250392009-10-01 22:08:58 +00001181}
1182
1183/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Devang Patel6daf99b2009-11-10 22:05:35 +00001184Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001185 BasicBlock *InsertAtEnd) {
Victor Hernandez4cf292a2010-01-26 02:07:38 +00001186 assert(Storage && "no storage passed to dbg.declare");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001187 assert(D.Verify() && "invalid DIVariable passed to dbg.declare");
Mike Stumpe4250392009-10-01 22:08:58 +00001188 if (!DeclareFn)
1189 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1190
Victor Hernandez756462b2010-01-18 20:42:09 +00001191 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
Devang Patel2db49d72010-05-07 18:11:54 +00001192 D };
Devang Patel27a53de2010-01-29 18:30:57 +00001193
1194 // If this block already has a terminator then insert this intrinsic
1195 // before the terminator.
Jim Grosbache62b6902010-07-21 21:36:25 +00001196 if (TerminatorInst *T = InsertAtEnd->getTerminator())
Devang Patel27a53de2010-01-29 18:30:57 +00001197 return CallInst::Create(DeclareFn, Args, Args+2, "", T);
1198 else
1199 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);}
Torok Edwin620f2802008-12-16 09:07:36 +00001200
Victor Hernandezc59b3352009-12-07 21:54:43 +00001201/// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001202Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
Victor Hernandezc59b3352009-12-07 21:54:43 +00001203 DIVariable D,
1204 Instruction *InsertBefore) {
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001205 assert(V && "no value passed to dbg.value");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001206 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001207 if (!ValueFn)
1208 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1209
Victor Hernandezc8b7cd02010-01-20 05:44:11 +00001210 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001211 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
Devang Patel2db49d72010-05-07 18:11:54 +00001212 D };
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001213 return CallInst::Create(ValueFn, Args, Args+3, "", InsertBefore);
1214}
1215
Victor Hernandezc59b3352009-12-07 21:54:43 +00001216/// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001217Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
Victor Hernandezc59b3352009-12-07 21:54:43 +00001218 DIVariable D,
1219 BasicBlock *InsertAtEnd) {
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001220 assert(V && "no value passed to dbg.value");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001221 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001222 if (!ValueFn)
1223 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1224
Jim Grosbache62b6902010-07-21 21:36:25 +00001225 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001226 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
Devang Patel2db49d72010-05-07 18:11:54 +00001227 D };
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001228 return CallInst::Create(ValueFn, Args, Args+3, "", InsertAtEnd);
1229}
Devang Patele4b27562009-08-28 23:24:31 +00001230
Devang Pateld2f79a12009-07-28 19:55:13 +00001231//===----------------------------------------------------------------------===//
Devang Patel98c65172009-07-30 18:25:15 +00001232// DebugInfoFinder implementations.
Devang Pateld2f79a12009-07-28 19:55:13 +00001233//===----------------------------------------------------------------------===//
1234
Devang Patel98c65172009-07-30 18:25:15 +00001235/// processModule - Process entire module and collect debug info.
1236void DebugInfoFinder::processModule(Module &M) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001237 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1238 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
1239 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
1240 ++BI) {
Devang Patel01c5ff62010-05-04 01:05:02 +00001241 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
Devang Patelb4d31302009-07-31 18:18:52 +00001242 processDeclare(DDI);
Jim Grosbache62b6902010-07-21 21:36:25 +00001243
Chris Lattner28a9bf62010-04-02 20:44:29 +00001244 DebugLoc Loc = BI->getDebugLoc();
1245 if (Loc.isUnknown())
1246 continue;
Jim Grosbache62b6902010-07-21 21:36:25 +00001247
Chris Lattner28a9bf62010-04-02 20:44:29 +00001248 LLVMContext &Ctx = BI->getContext();
1249 DIDescriptor Scope(Loc.getScope(Ctx));
Jim Grosbache62b6902010-07-21 21:36:25 +00001250
Chris Lattner28a9bf62010-04-02 20:44:29 +00001251 if (Scope.isCompileUnit())
Devang Patel2db49d72010-05-07 18:11:54 +00001252 addCompileUnit(DICompileUnit(Scope));
Chris Lattner28a9bf62010-04-02 20:44:29 +00001253 else if (Scope.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001254 processSubprogram(DISubprogram(Scope));
Chris Lattner28a9bf62010-04-02 20:44:29 +00001255 else if (Scope.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001256 processLexicalBlock(DILexicalBlock(Scope));
Jim Grosbache62b6902010-07-21 21:36:25 +00001257
Chris Lattner28a9bf62010-04-02 20:44:29 +00001258 if (MDNode *IA = Loc.getInlinedAt(Ctx))
1259 processLocation(DILocation(IA));
Devang Pateld2f79a12009-07-28 19:55:13 +00001260 }
Devang Patele4b27562009-08-28 23:24:31 +00001261
Devang Patelfd5fdc32010-06-28 05:53:08 +00001262 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
1263 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1264 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
1265 if (addGlobalVariable(DIG)) {
1266 addCompileUnit(DIG.getCompileUnit());
1267 processType(DIG.getType());
1268 }
Devang Pateld2f79a12009-07-28 19:55:13 +00001269 }
1270 }
Devang Patelfd5fdc32010-06-28 05:53:08 +00001271
1272 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
1273 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
1274 processSubprogram(DISubprogram(NMD->getOperand(i)));
Devang Pateld2f79a12009-07-28 19:55:13 +00001275}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001276
Devang Patel6daf99b2009-11-10 22:05:35 +00001277/// processLocation - Process DILocation.
1278void DebugInfoFinder::processLocation(DILocation Loc) {
Devang Patel3c91b052010-03-08 20:52:55 +00001279 if (!Loc.Verify()) return;
Devang Patel2db49d72010-05-07 18:11:54 +00001280 DIDescriptor S(Loc.getScope());
Devang Patel6daf99b2009-11-10 22:05:35 +00001281 if (S.isCompileUnit())
Devang Patel2db49d72010-05-07 18:11:54 +00001282 addCompileUnit(DICompileUnit(S));
Devang Patel6daf99b2009-11-10 22:05:35 +00001283 else if (S.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001284 processSubprogram(DISubprogram(S));
Devang Patel6daf99b2009-11-10 22:05:35 +00001285 else if (S.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001286 processLexicalBlock(DILexicalBlock(S));
Devang Patel6daf99b2009-11-10 22:05:35 +00001287 processLocation(Loc.getOrigLocation());
1288}
1289
Devang Patel98c65172009-07-30 18:25:15 +00001290/// processType - Process DIType.
1291void DebugInfoFinder::processType(DIType DT) {
Devang Patel72bcdb62009-08-10 22:09:58 +00001292 if (!addType(DT))
Devang Pateld2f79a12009-07-28 19:55:13 +00001293 return;
1294
1295 addCompileUnit(DT.getCompileUnit());
Devang Patel6ceea332009-08-31 18:49:10 +00001296 if (DT.isCompositeType()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001297 DICompositeType DCT(DT);
Devang Patel98c65172009-07-30 18:25:15 +00001298 processType(DCT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001299 DIArray DA = DCT.getTypeArray();
Devang Patel3c91b052010-03-08 20:52:55 +00001300 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1301 DIDescriptor D = DA.getElement(i);
1302 if (D.isType())
Devang Patel2db49d72010-05-07 18:11:54 +00001303 processType(DIType(D));
Devang Patel3c91b052010-03-08 20:52:55 +00001304 else if (D.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001305 processSubprogram(DISubprogram(D));
Devang Patel3c91b052010-03-08 20:52:55 +00001306 }
Devang Patel6ceea332009-08-31 18:49:10 +00001307 } else if (DT.isDerivedType()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001308 DIDerivedType DDT(DT);
Devang Patel3c91b052010-03-08 20:52:55 +00001309 processType(DDT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001310 }
1311}
1312
Devang Patelbeab41b2009-10-07 22:04:08 +00001313/// processLexicalBlock
1314void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
Devang Patelbeab41b2009-10-07 22:04:08 +00001315 DIScope Context = LB.getContext();
1316 if (Context.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001317 return processLexicalBlock(DILexicalBlock(Context));
Devang Patelbeab41b2009-10-07 22:04:08 +00001318 else
Devang Patel2db49d72010-05-07 18:11:54 +00001319 return processSubprogram(DISubprogram(Context));
Devang Patelbeab41b2009-10-07 22:04:08 +00001320}
1321
Devang Patel98c65172009-07-30 18:25:15 +00001322/// processSubprogram - Process DISubprogram.
1323void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001324 if (!addSubprogram(SP))
1325 return;
1326 addCompileUnit(SP.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +00001327 processType(SP.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +00001328}
1329
Devang Patelb4d31302009-07-31 18:18:52 +00001330/// processDeclare - Process DbgDeclareInst.
1331void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
Devang Patel3c91b052010-03-08 20:52:55 +00001332 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1333 if (!N) return;
1334
1335 DIDescriptor DV(N);
1336 if (!DV.isVariable())
Devang Patelb4d31302009-07-31 18:18:52 +00001337 return;
1338
Devang Patel2db49d72010-05-07 18:11:54 +00001339 if (!NodesSeen.insert(DV))
Devang Patelb4d31302009-07-31 18:18:52 +00001340 return;
1341
Devang Patel3c91b052010-03-08 20:52:55 +00001342 addCompileUnit(DIVariable(N).getCompileUnit());
1343 processType(DIVariable(N).getType());
Devang Patelb4d31302009-07-31 18:18:52 +00001344}
1345
Devang Patel72bcdb62009-08-10 22:09:58 +00001346/// addType - Add type into Tys.
1347bool DebugInfoFinder::addType(DIType DT) {
Devang Patel3c91b052010-03-08 20:52:55 +00001348 if (!DT.isValid())
Devang Patel72bcdb62009-08-10 22:09:58 +00001349 return false;
1350
Devang Patel2db49d72010-05-07 18:11:54 +00001351 if (!NodesSeen.insert(DT))
Devang Patel72bcdb62009-08-10 22:09:58 +00001352 return false;
1353
Devang Patel2db49d72010-05-07 18:11:54 +00001354 TYs.push_back(DT);
Devang Patel72bcdb62009-08-10 22:09:58 +00001355 return true;
1356}
1357
Devang Pateld2f79a12009-07-28 19:55:13 +00001358/// addCompileUnit - Add compile unit into CUs.
Devang Patel98c65172009-07-30 18:25:15 +00001359bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Patel3c91b052010-03-08 20:52:55 +00001360 if (!CU.Verify())
Devang Pateld2f79a12009-07-28 19:55:13 +00001361 return false;
1362
Devang Patel2db49d72010-05-07 18:11:54 +00001363 if (!NodesSeen.insert(CU))
Devang Pateld2f79a12009-07-28 19:55:13 +00001364 return false;
1365
Devang Patel2db49d72010-05-07 18:11:54 +00001366 CUs.push_back(CU);
Devang Pateld2f79a12009-07-28 19:55:13 +00001367 return true;
1368}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001369
Devang Pateld2f79a12009-07-28 19:55:13 +00001370/// addGlobalVariable - Add global variable into GVs.
Devang Patel98c65172009-07-30 18:25:15 +00001371bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Patel2db49d72010-05-07 18:11:54 +00001372 if (!DIDescriptor(DIG).isGlobalVariable())
Devang Pateld2f79a12009-07-28 19:55:13 +00001373 return false;
1374
Devang Patel2db49d72010-05-07 18:11:54 +00001375 if (!NodesSeen.insert(DIG))
Devang Pateld2f79a12009-07-28 19:55:13 +00001376 return false;
1377
Devang Patel2db49d72010-05-07 18:11:54 +00001378 GVs.push_back(DIG);
Devang Pateld2f79a12009-07-28 19:55:13 +00001379 return true;
1380}
1381
1382// addSubprogram - Add subprgoram into SPs.
Devang Patel98c65172009-07-30 18:25:15 +00001383bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Patel2db49d72010-05-07 18:11:54 +00001384 if (!DIDescriptor(SP).isSubprogram())
Devang Pateld2f79a12009-07-28 19:55:13 +00001385 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001386
Devang Patel2db49d72010-05-07 18:11:54 +00001387 if (!NodesSeen.insert(SP))
Devang Pateld2f79a12009-07-28 19:55:13 +00001388 return false;
1389
Devang Patel2db49d72010-05-07 18:11:54 +00001390 SPs.push_back(SP);
Devang Pateld2f79a12009-07-28 19:55:13 +00001391 return true;
1392}
1393
Victor Hernandez756462b2010-01-18 20:42:09 +00001394/// Find the debug info descriptor corresponding to this global variable.
1395static Value *findDbgGlobalDeclare(GlobalVariable *V) {
Chris Lattner099b7792009-12-29 09:22:47 +00001396 const Module *M = V->getParent();
1397 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1398 if (!NMD)
Torok Edwinff7d0e92009-03-10 13:41:26 +00001399 return 0;
Chris Lattner099b7792009-12-29 09:22:47 +00001400
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001401 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman872814a2010-07-21 18:54:18 +00001402 DIDescriptor DIG(cast<MDNode>(NMD->getOperand(i)));
Devang Patel3c91b052010-03-08 20:52:55 +00001403 if (!DIG.isGlobalVariable())
Chris Lattner099b7792009-12-29 09:22:47 +00001404 continue;
Devang Patel2db49d72010-05-07 18:11:54 +00001405 if (DIGlobalVariable(DIG).getGlobal() == V)
1406 return DIG;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001407 }
Chris Lattner099b7792009-12-29 09:22:47 +00001408 return 0;
1409}
Torok Edwinff7d0e92009-03-10 13:41:26 +00001410
Chris Lattner099b7792009-12-29 09:22:47 +00001411/// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
1412/// It looks through pointer casts too.
Victor Hernandez756462b2010-01-18 20:42:09 +00001413static const DbgDeclareInst *findDbgDeclare(const Value *V) {
Victor Hernandez3a328652010-01-15 19:04:09 +00001414 V = V->stripPointerCasts();
Jim Grosbache62b6902010-07-21 21:36:25 +00001415
Victor Hernandez3a328652010-01-15 19:04:09 +00001416 if (!isa<Instruction>(V) && !isa<Argument>(V))
Torok Edwin620f2802008-12-16 09:07:36 +00001417 return 0;
Jim Grosbache62b6902010-07-21 21:36:25 +00001418
Victor Hernandez3a328652010-01-15 19:04:09 +00001419 const Function *F = NULL;
1420 if (const Instruction *I = dyn_cast<Instruction>(V))
1421 F = I->getParent()->getParent();
1422 else if (const Argument *A = dyn_cast<Argument>(V))
1423 F = A->getParent();
Jim Grosbache62b6902010-07-21 21:36:25 +00001424
Victor Hernandez3a328652010-01-15 19:04:09 +00001425 for (Function::const_iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
1426 for (BasicBlock::const_iterator BI = (*FI).begin(), BE = (*FI).end();
1427 BI != BE; ++BI)
1428 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1429 if (DDI->getAddress() == V)
1430 return DDI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001431
Chris Lattner099b7792009-12-29 09:22:47 +00001432 return 0;
1433}
Bill Wendlingdc817b62009-05-14 18:26:15 +00001434
Chris Lattner099b7792009-12-29 09:22:47 +00001435bool llvm::getLocationInfo(const Value *V, std::string &DisplayName,
1436 std::string &Type, unsigned &LineNo,
1437 std::string &File, std::string &Dir) {
1438 DICompileUnit Unit;
1439 DIType TypeD;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001440
Chris Lattner099b7792009-12-29 09:22:47 +00001441 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1442 Value *DIGV = findDbgGlobalDeclare(GV);
1443 if (!DIGV) return false;
1444 DIGlobalVariable Var(cast<MDNode>(DIGV));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001445
Chris Lattner099b7792009-12-29 09:22:47 +00001446 StringRef D = Var.getDisplayName();
Devang Patel65dbc902009-11-25 17:36:49 +00001447 if (!D.empty())
Chris Lattner099b7792009-12-29 09:22:47 +00001448 DisplayName = D;
1449 LineNo = Var.getLineNumber();
1450 Unit = Var.getCompileUnit();
1451 TypeD = Var.getType();
1452 } else {
1453 const DbgDeclareInst *DDI = findDbgDeclare(V);
1454 if (!DDI) return false;
1455 DIVariable Var(cast<MDNode>(DDI->getVariable()));
1456
1457 StringRef D = Var.getName();
1458 if (!D.empty())
1459 DisplayName = D;
1460 LineNo = Var.getLineNumber();
1461 Unit = Var.getCompileUnit();
1462 TypeD = Var.getType();
Torok Edwinff7d0e92009-03-10 13:41:26 +00001463 }
Devang Patel13e16b62009-06-26 01:49:18 +00001464
Chris Lattner099b7792009-12-29 09:22:47 +00001465 StringRef T = TypeD.getName();
1466 if (!T.empty())
1467 Type = T;
1468 StringRef F = Unit.getFilename();
1469 if (!F.empty())
1470 File = F;
1471 StringRef D = Unit.getDirectory();
1472 if (!D.empty())
1473 Dir = D;
1474 return true;
1475}
Devang Patel9e529c32009-07-02 01:15:24 +00001476
Chris Lattner099b7792009-12-29 09:22:47 +00001477/// getDISubprogram - Find subprogram that is enclosing this scope.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001478DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
Chris Lattner099b7792009-12-29 09:22:47 +00001479 DIDescriptor D(Scope);
Chris Lattner099b7792009-12-29 09:22:47 +00001480 if (D.isSubprogram())
1481 return DISubprogram(Scope);
Jim Grosbache62b6902010-07-21 21:36:25 +00001482
Chris Lattner099b7792009-12-29 09:22:47 +00001483 if (D.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001484 return getDISubprogram(DILexicalBlock(Scope).getContext());
Jim Grosbache62b6902010-07-21 21:36:25 +00001485
Chris Lattner099b7792009-12-29 09:22:47 +00001486 return DISubprogram();
1487}
Devang Patel193f7202009-11-24 01:14:22 +00001488
Chris Lattner099b7792009-12-29 09:22:47 +00001489/// getDICompositeType - Find underlying composite type.
1490DICompositeType llvm::getDICompositeType(DIType T) {
Chris Lattner099b7792009-12-29 09:22:47 +00001491 if (T.isCompositeType())
Devang Patel2db49d72010-05-07 18:11:54 +00001492 return DICompositeType(T);
Jim Grosbache62b6902010-07-21 21:36:25 +00001493
Chris Lattner099b7792009-12-29 09:22:47 +00001494 if (T.isDerivedType())
Devang Patel2db49d72010-05-07 18:11:54 +00001495 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
Jim Grosbache62b6902010-07-21 21:36:25 +00001496
Chris Lattner099b7792009-12-29 09:22:47 +00001497 return DICompositeType();
Torok Edwin620f2802008-12-16 09:07:36 +00001498}