blob: 41d803c699d5f055c16f48278b4209259a702b42 [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"
Owen Anderson99035272009-07-07 17:12:53 +000021#include "llvm/LLVMContext.h"
Chris Lattnera45664f2008-11-10 02:56:27 +000022#include "llvm/Module.h"
23#include "llvm/Analysis/ValueTracking.h"
Devang Patele4b27562009-08-28 23:24:31 +000024#include "llvm/ADT/SmallPtrSet.h"
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000025#include "llvm/Support/Dwarf.h"
Devang Patel9e529c32009-07-02 01:15:24 +000026#include "llvm/Support/DebugLoc.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
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000035/// ValidDebugInfo - Return true if V represents valid debug info value.
Devang Patele4b27562009-08-28 23:24:31 +000036/// FIXME : Add DIDescriptor.isValid()
37bool DIDescriptor::ValidDebugInfo(MDNode *N, CodeGenOpt::Level OptLevel) {
38 if (!N)
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000039 return false;
40
Devang Patele4b27562009-08-28 23:24:31 +000041 DIDescriptor DI(N);
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000042
43 // Check current version. Allow Version6 for now.
44 unsigned Version = DI.getVersion();
45 if (Version != LLVMDebugVersion && Version != LLVMDebugVersion6)
46 return false;
47
48 unsigned Tag = DI.getTag();
49 switch (Tag) {
50 case DW_TAG_variable:
Devang Patele4b27562009-08-28 23:24:31 +000051 assert(DIVariable(N).Verify() && "Invalid DebugInfo value");
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000052 break;
53 case DW_TAG_compile_unit:
Devang Patele4b27562009-08-28 23:24:31 +000054 assert(DICompileUnit(N).Verify() && "Invalid DebugInfo value");
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000055 break;
56 case DW_TAG_subprogram:
Devang Patele4b27562009-08-28 23:24:31 +000057 assert(DISubprogram(N).Verify() && "Invalid DebugInfo value");
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000058 break;
59 case DW_TAG_lexical_block:
Bill Wendlingdc817b62009-05-14 18:26:15 +000060 // FIXME: This interfers with the quality of generated code during
61 // optimization.
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000062 if (OptLevel != CodeGenOpt::None)
63 return false;
Bill Wendlingdc817b62009-05-14 18:26:15 +000064 // FALLTHROUGH
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000065 default:
66 break;
67 }
68
69 return true;
70}
71
Devang Patele4b27562009-08-28 23:24:31 +000072DIDescriptor::DIDescriptor(MDNode *N, unsigned RequiredTag) {
73 DbgNode = N;
Daniel Dunbarf612ff62009-09-19 20:40:05 +000074
Bill Wendlingdc817b62009-05-14 18:26:15 +000075 // If this is non-null, check to see if the Tag matches. If not, set to null.
Devang Patele4b27562009-08-28 23:24:31 +000076 if (N && getTag() != RequiredTag) {
77 DbgNode = 0;
78 }
Chris Lattnera45664f2008-11-10 02:56:27 +000079}
80
Devang Patel65dbc902009-11-25 17:36:49 +000081StringRef
Devang Patel5ccdd102009-09-29 18:40:58 +000082DIDescriptor::getStringField(unsigned Elt) const {
Devang Patele4b27562009-08-28 23:24:31 +000083 if (DbgNode == 0)
Devang Patel65dbc902009-11-25 17:36:49 +000084 return StringRef();
Chris Lattnera45664f2008-11-10 02:56:27 +000085
Daniel Dunbarf612ff62009-09-19 20:40:05 +000086 if (Elt < DbgNode->getNumElements())
Devang Patel65dbc902009-11-25 17:36:49 +000087 if (MDString *MDS = dyn_cast_or_null<MDString>(DbgNode->getElement(Elt)))
88 return MDS->getString();
Daniel Dunbarf612ff62009-09-19 20:40:05 +000089
Devang Patel65dbc902009-11-25 17:36:49 +000090 return StringRef();
Chris Lattnera45664f2008-11-10 02:56:27 +000091}
92
93uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +000094 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +000095 return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +000096
Devang Patele4b27562009-08-28 23:24:31 +000097 if (Elt < DbgNode->getNumElements())
98 if (ConstantInt *CI = dyn_cast<ConstantInt>(DbgNode->getElement(Elt)))
99 return CI->getZExtValue();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000100
Chris Lattnera45664f2008-11-10 02:56:27 +0000101 return 0;
102}
103
Chris Lattnera45664f2008-11-10 02:56:27 +0000104DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000105 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +0000106 return DIDescriptor();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000107
Devang Patele4b27562009-08-28 23:24:31 +0000108 if (Elt < DbgNode->getNumElements() && DbgNode->getElement(Elt))
109 return DIDescriptor(dyn_cast<MDNode>(DbgNode->getElement(Elt)));
110
111 return DIDescriptor();
Chris Lattnera45664f2008-11-10 02:56:27 +0000112}
113
114GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000115 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +0000116 return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000117
Devang Patele4b27562009-08-28 23:24:31 +0000118 if (Elt < DbgNode->getNumElements())
Bill Wendling26c6cf42009-10-08 20:52:51 +0000119 return dyn_cast_or_null<GlobalVariable>(DbgNode->getElement(Elt));
Devang Patele4b27562009-08-28 23:24:31 +0000120 return 0;
Chris Lattnera45664f2008-11-10 02:56:27 +0000121}
122
Chris Lattnera45664f2008-11-10 02:56:27 +0000123//===----------------------------------------------------------------------===//
Devang Patel6ceea332009-08-31 18:49:10 +0000124// Predicates
Chris Lattnera45664f2008-11-10 02:56:27 +0000125//===----------------------------------------------------------------------===//
126
Devang Patel6ceea332009-08-31 18:49:10 +0000127/// isBasicType - Return true if the specified tag is legal for
128/// DIBasicType.
129bool DIDescriptor::isBasicType() const {
Devang Patel5a685092009-08-31 20:27:49 +0000130 assert (!isNull() && "Invalid descriptor!");
Devang Patel6ceea332009-08-31 18:49:10 +0000131 unsigned Tag = getTag();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000132
Devang Patel6ceea332009-08-31 18:49:10 +0000133 return Tag == dwarf::DW_TAG_base_type;
Torok Edwinb07fbd92008-12-13 08:25:29 +0000134}
Chris Lattnera45664f2008-11-10 02:56:27 +0000135
Devang Patel6ceea332009-08-31 18:49:10 +0000136/// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
137bool DIDescriptor::isDerivedType() const {
Devang Patel5a685092009-08-31 20:27:49 +0000138 assert (!isNull() && "Invalid descriptor!");
Devang Patel6ceea332009-08-31 18:49:10 +0000139 unsigned Tag = getTag();
140
Chris Lattnera45664f2008-11-10 02:56:27 +0000141 switch (Tag) {
142 case dwarf::DW_TAG_typedef:
143 case dwarf::DW_TAG_pointer_type:
144 case dwarf::DW_TAG_reference_type:
145 case dwarf::DW_TAG_const_type:
146 case dwarf::DW_TAG_volatile_type:
147 case dwarf::DW_TAG_restrict_type:
148 case dwarf::DW_TAG_member:
149 case dwarf::DW_TAG_inheritance:
150 return true;
151 default:
Devang Patele4b27562009-08-28 23:24:31 +0000152 // CompositeTypes are currently modelled as DerivedTypes.
Devang Patel6ceea332009-08-31 18:49:10 +0000153 return isCompositeType();
Chris Lattnera45664f2008-11-10 02:56:27 +0000154 }
155}
156
Chris Lattnera45664f2008-11-10 02:56:27 +0000157/// isCompositeType - Return true if the specified tag is legal for
158/// DICompositeType.
Devang Patel6ceea332009-08-31 18:49:10 +0000159bool DIDescriptor::isCompositeType() const {
Devang Patel5a685092009-08-31 20:27:49 +0000160 assert (!isNull() && "Invalid descriptor!");
Devang Patel6ceea332009-08-31 18:49:10 +0000161 unsigned Tag = getTag();
162
163 switch (Tag) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000164 case dwarf::DW_TAG_array_type:
165 case dwarf::DW_TAG_structure_type:
166 case dwarf::DW_TAG_union_type:
167 case dwarf::DW_TAG_enumeration_type:
168 case dwarf::DW_TAG_vector_type:
169 case dwarf::DW_TAG_subroutine_type:
Devang Patel25cb0d72009-03-25 03:52:06 +0000170 case dwarf::DW_TAG_class_type:
Chris Lattnera45664f2008-11-10 02:56:27 +0000171 return true;
172 default:
173 return false;
174 }
175}
176
Chris Lattnera45664f2008-11-10 02:56:27 +0000177/// isVariable - Return true if the specified tag is legal for DIVariable.
Devang Patel6ceea332009-08-31 18:49:10 +0000178bool DIDescriptor::isVariable() const {
Devang Patel5a685092009-08-31 20:27:49 +0000179 assert (!isNull() && "Invalid descriptor!");
Devang Patel6ceea332009-08-31 18:49:10 +0000180 unsigned Tag = getTag();
181
Chris Lattnera45664f2008-11-10 02:56:27 +0000182 switch (Tag) {
183 case dwarf::DW_TAG_auto_variable:
184 case dwarf::DW_TAG_arg_variable:
185 case dwarf::DW_TAG_return_variable:
186 return true;
187 default:
188 return false;
189 }
190}
191
Devang Patelecbeb1a2009-09-30 22:34:41 +0000192/// isType - Return true if the specified tag is legal for DIType.
193bool DIDescriptor::isType() const {
194 return isBasicType() || isCompositeType() || isDerivedType();
195}
196
Devang Patel6ceea332009-08-31 18:49:10 +0000197/// isSubprogram - Return true if the specified tag is legal for
198/// DISubprogram.
199bool DIDescriptor::isSubprogram() const {
Devang Patel5a685092009-08-31 20:27:49 +0000200 assert (!isNull() && "Invalid descriptor!");
Devang Patel6ceea332009-08-31 18:49:10 +0000201 unsigned Tag = getTag();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000202
Devang Patel6ceea332009-08-31 18:49:10 +0000203 return Tag == dwarf::DW_TAG_subprogram;
204}
205
206/// isGlobalVariable - Return true if the specified tag is legal for
207/// DIGlobalVariable.
208bool DIDescriptor::isGlobalVariable() const {
Devang Patel5a685092009-08-31 20:27:49 +0000209 assert (!isNull() && "Invalid descriptor!");
Devang Patel6ceea332009-08-31 18:49:10 +0000210 unsigned Tag = getTag();
211
212 return Tag == dwarf::DW_TAG_variable;
213}
214
Devang Patelecbeb1a2009-09-30 22:34:41 +0000215/// isGlobal - Return true if the specified tag is legal for DIGlobal.
216bool DIDescriptor::isGlobal() const {
217 return isGlobalVariable();
218}
219
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000220/// isScope - Return true if the specified tag is one of the scope
Devang Patel43d98b32009-08-31 20:44:45 +0000221/// related tag.
222bool DIDescriptor::isScope() const {
223 assert (!isNull() && "Invalid descriptor!");
224 unsigned Tag = getTag();
225
226 switch (Tag) {
227 case dwarf::DW_TAG_compile_unit:
228 case dwarf::DW_TAG_lexical_block:
229 case dwarf::DW_TAG_subprogram:
230 return true;
231 default:
232 break;
233 }
234 return false;
235}
Devang Patel6ceea332009-08-31 18:49:10 +0000236
Devang Patelc9f322d2009-08-31 21:34:44 +0000237/// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
238bool DIDescriptor::isCompileUnit() const {
239 assert (!isNull() && "Invalid descriptor!");
240 unsigned Tag = getTag();
241
242 return Tag == dwarf::DW_TAG_compile_unit;
243}
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 {
247 assert (!isNull() && "Invalid descriptor!");
248 unsigned Tag = getTag();
249
250 return Tag == dwarf::DW_TAG_lexical_block;
251}
252
Devang Patelecbeb1a2009-09-30 22:34:41 +0000253/// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
254bool DIDescriptor::isSubrange() const {
255 assert (!isNull() && "Invalid descriptor!");
256 unsigned Tag = getTag();
257
258 return Tag == dwarf::DW_TAG_subrange_type;
259}
260
261/// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
262bool DIDescriptor::isEnumerator() const {
263 assert (!isNull() && "Invalid descriptor!");
264 unsigned Tag = getTag();
265
266 return Tag == dwarf::DW_TAG_enumerator;
267}
268
Devang Patel6ceea332009-08-31 18:49:10 +0000269//===----------------------------------------------------------------------===//
270// Simple Descriptor Constructors and other Methods
271//===----------------------------------------------------------------------===//
272
273DIType::DIType(MDNode *N) : DIDescriptor(N) {
274 if (!N) return;
275 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
276 DbgNode = 0;
277 }
278}
279
Devang Patel68afdc32009-01-05 18:33:01 +0000280unsigned DIArray::getNumElements() const {
Devang Patele4b27562009-08-28 23:24:31 +0000281 assert (DbgNode && "Invalid DIArray");
282 return DbgNode->getNumElements();
Devang Patel68afdc32009-01-05 18:33:01 +0000283}
Chris Lattnera45664f2008-11-10 02:56:27 +0000284
Devang Patelc4999d72009-07-22 18:23:44 +0000285/// replaceAllUsesWith - Replace all uses of debug info referenced by
286/// this descriptor. After this completes, the current debug info value
287/// is erased.
288void DIDerivedType::replaceAllUsesWith(DIDescriptor &D) {
289 if (isNull())
290 return;
291
Devang Patel6930f4f2009-07-22 18:56:16 +0000292 assert (!D.isNull() && "Can not replace with null");
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000293
294 // Since we use a TrackingVH for the node, its easy for clients to manufacture
295 // legitimate situations where they want to replaceAllUsesWith() on something
296 // which, due to uniquing, has merged with the source. We shield clients from
297 // this detail by allowing a value to be replaced with replaceAllUsesWith()
298 // itself.
299 if (getNode() != D.getNode()) {
300 MDNode *Node = DbgNode;
301 Node->replaceAllUsesWith(D.getNode());
302 delete Node;
303 }
Devang Patelc4999d72009-07-22 18:23:44 +0000304}
305
Devang Patelb79b5352009-01-19 23:21:49 +0000306/// Verify - Verify that a compile unit is well formed.
307bool DICompileUnit::Verify() const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000308 if (isNull())
Devang Patelb79b5352009-01-19 23:21:49 +0000309 return false;
Devang Patel65dbc902009-11-25 17:36:49 +0000310 StringRef N = getFilename();
311 if (N.empty())
Bill Wendling0582ae92009-03-13 04:39:26 +0000312 return false;
Devang Patelb79b5352009-01-19 23:21:49 +0000313 // It is possible that directory and produce string is empty.
Bill Wendling0582ae92009-03-13 04:39:26 +0000314 return true;
Devang Patelb79b5352009-01-19 23:21:49 +0000315}
316
317/// Verify - Verify that a type descriptor is well formed.
318bool DIType::Verify() const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000319 if (isNull())
Devang Patelb79b5352009-01-19 23:21:49 +0000320 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000321 if (getContext().isNull())
Devang Patelb79b5352009-01-19 23:21:49 +0000322 return false;
323
324 DICompileUnit CU = getCompileUnit();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000325 if (!CU.isNull() && !CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000326 return false;
327 return true;
328}
329
330/// Verify - Verify that a composite type descriptor is well formed.
331bool DICompositeType::Verify() const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000332 if (isNull())
Devang Patelb79b5352009-01-19 23:21:49 +0000333 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000334 if (getContext().isNull())
Devang Patelb79b5352009-01-19 23:21:49 +0000335 return false;
336
337 DICompileUnit CU = getCompileUnit();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000338 if (!CU.isNull() && !CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000339 return false;
340 return true;
341}
342
343/// Verify - Verify that a subprogram descriptor is well formed.
344bool DISubprogram::Verify() const {
345 if (isNull())
346 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000347
Devang Patelb79b5352009-01-19 23:21:49 +0000348 if (getContext().isNull())
349 return false;
350
351 DICompileUnit CU = getCompileUnit();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000352 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000353 return false;
354
355 DICompositeType Ty = getType();
356 if (!Ty.isNull() && !Ty.Verify())
357 return false;
358 return true;
359}
360
361/// Verify - Verify that a global variable descriptor is well formed.
362bool DIGlobalVariable::Verify() const {
363 if (isNull())
364 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000365
Devang Patel65dbc902009-11-25 17:36:49 +0000366 if (getDisplayName().empty())
Devang Patel84c73e92009-11-06 17:58:12 +0000367 return false;
368
Devang Patelb79b5352009-01-19 23:21:49 +0000369 if (getContext().isNull())
370 return false;
371
372 DICompileUnit CU = getCompileUnit();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000373 if (!CU.isNull() && !CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000374 return false;
375
376 DIType Ty = getType();
377 if (!Ty.Verify())
378 return false;
379
380 if (!getGlobal())
381 return false;
382
383 return true;
384}
385
386/// Verify - Verify that a variable descriptor is well formed.
387bool DIVariable::Verify() const {
388 if (isNull())
389 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000390
Devang Patelb79b5352009-01-19 23:21:49 +0000391 if (getContext().isNull())
392 return false;
393
394 DIType Ty = getType();
395 if (!Ty.Verify())
396 return false;
397
Devang Patelb79b5352009-01-19 23:21:49 +0000398 return true;
399}
400
Devang Patel36375ee2009-02-17 21:23:59 +0000401/// getOriginalTypeSize - If this type is derived from a base type then
402/// return base type size.
403uint64_t DIDerivedType::getOriginalTypeSize() const {
Devang Patel61ecbd12009-11-04 23:48:00 +0000404 unsigned Tag = getTag();
405 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
406 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
407 Tag == dwarf::DW_TAG_restrict_type) {
408 DIType BaseType = getTypeDerivedFrom();
Devang Patel5ebfa2d2009-11-06 18:24:05 +0000409 // If this type is not derived from any type then take conservative
410 // approach.
411 if (BaseType.isNull())
412 return getSizeInBits();
Devang Patel61ecbd12009-11-04 23:48:00 +0000413 if (BaseType.isDerivedType())
414 return DIDerivedType(BaseType.getNode()).getOriginalTypeSize();
415 else
416 return BaseType.getSizeInBits();
417 }
418
419 return getSizeInBits();
Devang Patel36375ee2009-02-17 21:23:59 +0000420}
Devang Patelb79b5352009-01-19 23:21:49 +0000421
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000422/// describes - Return true if this subprogram provides debugging
423/// information for the function F.
424bool DISubprogram::describes(const Function *F) {
425 assert (F && "Invalid function");
Devang Patel65dbc902009-11-25 17:36:49 +0000426 StringRef Name = getLinkageName();
427 if (Name.empty())
Devang Patel5ccdd102009-09-29 18:40:58 +0000428 Name = getName();
Devang Patel65dbc902009-11-25 17:36:49 +0000429 if (F->getName() == Name)
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000430 return true;
431 return false;
432}
433
Devang Patel65dbc902009-11-25 17:36:49 +0000434StringRef DIScope::getFilename() const {
Devang Patelecbeb1a2009-09-30 22:34:41 +0000435 if (isLexicalBlock())
436 return DILexicalBlock(DbgNode).getFilename();
437 else if (isSubprogram())
438 return DISubprogram(DbgNode).getFilename();
439 else if (isCompileUnit())
440 return DICompileUnit(DbgNode).getFilename();
441 else
442 assert (0 && "Invalid DIScope!");
Devang Patel65dbc902009-11-25 17:36:49 +0000443 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000444}
445
Devang Patel65dbc902009-11-25 17:36:49 +0000446StringRef DIScope::getDirectory() const {
Devang Patelecbeb1a2009-09-30 22:34:41 +0000447 if (isLexicalBlock())
448 return DILexicalBlock(DbgNode).getDirectory();
449 else if (isSubprogram())
450 return DISubprogram(DbgNode).getDirectory();
451 else if (isCompileUnit())
452 return DICompileUnit(DbgNode).getDirectory();
453 else
454 assert (0 && "Invalid DIScope!");
Devang Patel65dbc902009-11-25 17:36:49 +0000455 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000456}
457
Chris Lattnera45664f2008-11-10 02:56:27 +0000458//===----------------------------------------------------------------------===//
Devang Patel7136a652009-07-01 22:10:23 +0000459// DIDescriptor: dump routines for all descriptors.
460//===----------------------------------------------------------------------===//
461
462
463/// dump - Print descriptor.
464void DIDescriptor::dump() const {
Devang Patele4b27562009-08-28 23:24:31 +0000465 errs() << "[" << dwarf::TagString(getTag()) << "] ";
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000466 errs().write_hex((intptr_t) &*DbgNode) << ']';
Devang Patel7136a652009-07-01 22:10:23 +0000467}
468
469/// dump - Print compile unit.
470void DICompileUnit::dump() const {
471 if (getLanguage())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000472 errs() << " [" << dwarf::LanguageString(getLanguage()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000473
Devang Patel5ccdd102009-09-29 18:40:58 +0000474 errs() << " [" << getDirectory() << "/" << getFilename() << " ]";
Devang Patel7136a652009-07-01 22:10:23 +0000475}
476
477/// dump - Print type.
478void DIType::dump() const {
479 if (isNull()) return;
480
Devang Patel65dbc902009-11-25 17:36:49 +0000481 StringRef Res = getName();
482 if (!Res.empty())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000483 errs() << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000484
485 unsigned Tag = getTag();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000486 errs() << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000487
488 // TODO : Print context
489 getCompileUnit().dump();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000490 errs() << " ["
491 << getLineNumber() << ", "
Chris Lattnera81d29b2009-08-23 07:33:14 +0000492 << getSizeInBits() << ", "
493 << getAlignInBits() << ", "
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000494 << getOffsetInBits()
Chris Lattnera81d29b2009-08-23 07:33:14 +0000495 << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000496
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000497 if (isPrivate())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000498 errs() << " [private] ";
Devang Patel7136a652009-07-01 22:10:23 +0000499 else if (isProtected())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000500 errs() << " [protected] ";
Devang Patel7136a652009-07-01 22:10:23 +0000501
502 if (isForwardDecl())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000503 errs() << " [fwd] ";
Devang Patel7136a652009-07-01 22:10:23 +0000504
Devang Patel6ceea332009-08-31 18:49:10 +0000505 if (isBasicType())
Devang Patele4b27562009-08-28 23:24:31 +0000506 DIBasicType(DbgNode).dump();
Devang Patel6ceea332009-08-31 18:49:10 +0000507 else if (isDerivedType())
Devang Patele4b27562009-08-28 23:24:31 +0000508 DIDerivedType(DbgNode).dump();
Devang Patel6ceea332009-08-31 18:49:10 +0000509 else if (isCompositeType())
Devang Patele4b27562009-08-28 23:24:31 +0000510 DICompositeType(DbgNode).dump();
Devang Patel7136a652009-07-01 22:10:23 +0000511 else {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000512 errs() << "Invalid DIType\n";
Devang Patel7136a652009-07-01 22:10:23 +0000513 return;
514 }
515
Chris Lattnera81d29b2009-08-23 07:33:14 +0000516 errs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000517}
518
519/// dump - Print basic type.
520void DIBasicType::dump() const {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000521 errs() << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000522}
523
524/// dump - Print derived type.
525void DIDerivedType::dump() const {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000526 errs() << "\n\t Derived From: "; getTypeDerivedFrom().dump();
Devang Patel7136a652009-07-01 22:10:23 +0000527}
528
529/// dump - Print composite type.
530void DICompositeType::dump() const {
531 DIArray A = getTypeArray();
532 if (A.isNull())
533 return;
Chris Lattnera81d29b2009-08-23 07:33:14 +0000534 errs() << " [" << A.getNumElements() << " elements]";
Devang Patel7136a652009-07-01 22:10:23 +0000535}
536
537/// dump - Print global.
538void DIGlobal::dump() const {
Devang Patel65dbc902009-11-25 17:36:49 +0000539 StringRef Res = getName();
540 if (!Res.empty())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000541 errs() << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000542
543 unsigned Tag = getTag();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000544 errs() << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000545
546 // TODO : Print context
547 getCompileUnit().dump();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000548 errs() << " [" << getLineNumber() << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000549
550 if (isLocalToUnit())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000551 errs() << " [local] ";
Devang Patel7136a652009-07-01 22:10:23 +0000552
553 if (isDefinition())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000554 errs() << " [def] ";
Devang Patel7136a652009-07-01 22:10:23 +0000555
Devang Patel6ceea332009-08-31 18:49:10 +0000556 if (isGlobalVariable())
Devang Patele4b27562009-08-28 23:24:31 +0000557 DIGlobalVariable(DbgNode).dump();
Devang Patel7136a652009-07-01 22:10:23 +0000558
Chris Lattnera81d29b2009-08-23 07:33:14 +0000559 errs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000560}
561
562/// dump - Print subprogram.
563void DISubprogram::dump() const {
Devang Patel65dbc902009-11-25 17:36:49 +0000564 StringRef Res = getName();
565 if (!Res.empty())
Devang Patel82dfc0c2009-08-31 22:47:13 +0000566 errs() << " [" << Res << "] ";
567
568 unsigned Tag = getTag();
569 errs() << " [" << dwarf::TagString(Tag) << "] ";
570
571 // TODO : Print context
572 getCompileUnit().dump();
573 errs() << " [" << getLineNumber() << "] ";
574
575 if (isLocalToUnit())
576 errs() << " [local] ";
577
578 if (isDefinition())
579 errs() << " [def] ";
580
581 errs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000582}
583
584/// dump - Print global variable.
585void DIGlobalVariable::dump() const {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000586 errs() << " [";
587 getGlobal()->dump();
588 errs() << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000589}
590
591/// dump - Print variable.
592void DIVariable::dump() const {
Devang Patel65dbc902009-11-25 17:36:49 +0000593 StringRef Res = getName();
594 if (!Res.empty())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000595 errs() << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000596
597 getCompileUnit().dump();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000598 errs() << " [" << getLineNumber() << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000599 getType().dump();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000600 errs() << "\n";
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000601
602 // FIXME: Dump complex addresses
Devang Patel7136a652009-07-01 22:10:23 +0000603}
604
605//===----------------------------------------------------------------------===//
Chris Lattnera45664f2008-11-10 02:56:27 +0000606// DIFactory: Basic Helpers
607//===----------------------------------------------------------------------===//
608
Bill Wendlingdc817b62009-05-14 18:26:15 +0000609DIFactory::DIFactory(Module &m)
Devang Patel427ef4e2009-11-17 22:39:08 +0000610 : M(m), VMContext(M.getContext()), DeclareFn(0) {
Devang Patel3ddf7042009-11-13 02:27:33 +0000611 EmptyStructPtr = PointerType::getUnqual(StructType::get(VMContext));
612}
Chris Lattner497a7a82008-11-10 04:10:34 +0000613
Chris Lattnera45664f2008-11-10 02:56:27 +0000614Constant *DIFactory::GetTagConstant(unsigned TAG) {
Devang Patel6906ba52009-01-20 19:22:03 +0000615 assert((TAG & LLVMDebugVersionMask) == 0 &&
Chris Lattnera45664f2008-11-10 02:56:27 +0000616 "Tag too large for debug encoding!");
Owen Anderson1d0be152009-08-13 21:58:54 +0000617 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
Chris Lattnera45664f2008-11-10 02:56:27 +0000618}
619
Chris Lattnera45664f2008-11-10 02:56:27 +0000620//===----------------------------------------------------------------------===//
621// DIFactory: Primary Constructors
622//===----------------------------------------------------------------------===//
623
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000624/// GetOrCreateArray - Create an descriptor for an array of descriptors.
Chris Lattnera45664f2008-11-10 02:56:27 +0000625/// This implicitly uniques the arrays created.
626DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
Devang Patele4b27562009-08-28 23:24:31 +0000627 SmallVector<Value*, 16> Elts;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000628
Devang Patele4b27562009-08-28 23:24:31 +0000629 if (NumTys == 0)
630 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)));
631 else
632 for (unsigned i = 0; i != NumTys; ++i)
633 Elts.push_back(Tys[i].getNode());
Devang Patel82459882009-08-26 05:01:18 +0000634
Devang Patele4b27562009-08-28 23:24:31 +0000635 return DIArray(MDNode::get(VMContext,Elts.data(), Elts.size()));
Chris Lattnera45664f2008-11-10 02:56:27 +0000636}
637
638/// GetOrCreateSubrange - Create a descriptor for a value range. This
639/// implicitly uniques the values returned.
640DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
Devang Patele4b27562009-08-28 23:24:31 +0000641 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000642 GetTagConstant(dwarf::DW_TAG_subrange_type),
Owen Anderson1d0be152009-08-13 21:58:54 +0000643 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
644 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
Chris Lattnera45664f2008-11-10 02:56:27 +0000645 };
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000646
Devang Patele4b27562009-08-28 23:24:31 +0000647 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000648}
649
650
651
652/// CreateCompileUnit - Create a new descriptor for the specified compile
653/// unit. Note that this does not unique compile units within the module.
654DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
Devang Patel65dbc902009-11-25 17:36:49 +0000655 StringRef Filename,
656 StringRef Directory,
657 StringRef Producer,
Devang Pateldd9db662009-01-30 18:20:31 +0000658 bool isMain,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000659 bool isOptimized,
Devang Patel65dbc902009-11-25 17:36:49 +0000660 StringRef Flags,
Devang Patel13319ce2009-02-17 22:43:44 +0000661 unsigned RunTimeVer) {
Devang Patele4b27562009-08-28 23:24:31 +0000662 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000663 GetTagConstant(dwarf::DW_TAG_compile_unit),
Devang Patele4b27562009-08-28 23:24:31 +0000664 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Owen Anderson1d0be152009-08-13 21:58:54 +0000665 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
Devang Patele4b27562009-08-28 23:24:31 +0000666 MDString::get(VMContext, Filename),
667 MDString::get(VMContext, Directory),
668 MDString::get(VMContext, Producer),
Owen Anderson1d0be152009-08-13 21:58:54 +0000669 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
670 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
Devang Patele4b27562009-08-28 23:24:31 +0000671 MDString::get(VMContext, Flags),
Owen Anderson1d0be152009-08-13 21:58:54 +0000672 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
Chris Lattnera45664f2008-11-10 02:56:27 +0000673 };
Devang Patele4b27562009-08-28 23:24:31 +0000674
675 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000676}
677
678/// CreateEnumerator - Create a single enumerator value.
Devang Patel65dbc902009-11-25 17:36:49 +0000679DIEnumerator DIFactory::CreateEnumerator(StringRef Name, uint64_t Val){
Devang Patele4b27562009-08-28 23:24:31 +0000680 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000681 GetTagConstant(dwarf::DW_TAG_enumerator),
Devang Patele4b27562009-08-28 23:24:31 +0000682 MDString::get(VMContext, Name),
Owen Anderson1d0be152009-08-13 21:58:54 +0000683 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
Chris Lattnera45664f2008-11-10 02:56:27 +0000684 };
Devang Patele4b27562009-08-28 23:24:31 +0000685 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000686}
687
688
689/// CreateBasicType - Create a basic type like int, float, etc.
690DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000691 StringRef Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000692 DICompileUnit CompileUnit,
693 unsigned LineNumber,
694 uint64_t SizeInBits,
695 uint64_t AlignInBits,
696 uint64_t OffsetInBits, unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000697 unsigned Encoding) {
Devang Patele4b27562009-08-28 23:24:31 +0000698 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000699 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patele4b27562009-08-28 23:24:31 +0000700 Context.getNode(),
701 MDString::get(VMContext, Name),
702 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000703 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
704 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
705 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
706 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
707 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
708 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
Chris Lattnera45664f2008-11-10 02:56:27 +0000709 };
Devang Patele4b27562009-08-28 23:24:31 +0000710 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000711}
712
Devang Patelac16d442009-10-26 16:54:35 +0000713
714/// CreateBasicType - Create a basic type like int, float, etc.
715DIBasicType DIFactory::CreateBasicTypeEx(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000716 StringRef Name,
Devang Patelac16d442009-10-26 16:54:35 +0000717 DICompileUnit CompileUnit,
718 unsigned LineNumber,
719 Constant *SizeInBits,
720 Constant *AlignInBits,
721 Constant *OffsetInBits, unsigned Flags,
722 unsigned Encoding) {
723 Value *Elts[] = {
724 GetTagConstant(dwarf::DW_TAG_base_type),
725 Context.getNode(),
726 MDString::get(VMContext, Name),
727 CompileUnit.getNode(),
728 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
729 SizeInBits,
730 AlignInBits,
731 OffsetInBits,
732 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
733 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
734 };
735 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
736}
737
738
Chris Lattnera45664f2008-11-10 02:56:27 +0000739/// CreateDerivedType - Create a derived type like const qualified type,
740/// pointer, typedef, etc.
741DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
742 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000743 StringRef Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000744 DICompileUnit CompileUnit,
745 unsigned LineNumber,
746 uint64_t SizeInBits,
747 uint64_t AlignInBits,
748 uint64_t OffsetInBits,
749 unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000750 DIType DerivedFrom) {
Devang Patele4b27562009-08-28 23:24:31 +0000751 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000752 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000753 Context.getNode(),
754 MDString::get(VMContext, Name),
755 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000756 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
757 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
758 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
759 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
760 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patele4b27562009-08-28 23:24:31 +0000761 DerivedFrom.getNode(),
Chris Lattnera45664f2008-11-10 02:56:27 +0000762 };
Devang Patele4b27562009-08-28 23:24:31 +0000763 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000764}
765
Devang Patelac16d442009-10-26 16:54:35 +0000766
767/// CreateDerivedType - Create a derived type like const qualified type,
768/// pointer, typedef, etc.
769DIDerivedType DIFactory::CreateDerivedTypeEx(unsigned Tag,
770 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000771 StringRef Name,
Devang Patelac16d442009-10-26 16:54:35 +0000772 DICompileUnit CompileUnit,
773 unsigned LineNumber,
774 Constant *SizeInBits,
775 Constant *AlignInBits,
776 Constant *OffsetInBits,
777 unsigned Flags,
778 DIType DerivedFrom) {
779 Value *Elts[] = {
780 GetTagConstant(Tag),
781 Context.getNode(),
782 MDString::get(VMContext, Name),
783 CompileUnit.getNode(),
784 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
785 SizeInBits,
786 AlignInBits,
787 OffsetInBits,
788 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
789 DerivedFrom.getNode(),
790 };
791 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
792}
793
794
Chris Lattnera45664f2008-11-10 02:56:27 +0000795/// CreateCompositeType - Create a composite type like array, struct, etc.
796DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
797 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000798 StringRef Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000799 DICompileUnit CompileUnit,
800 unsigned LineNumber,
801 uint64_t SizeInBits,
802 uint64_t AlignInBits,
803 uint64_t OffsetInBits,
804 unsigned Flags,
805 DIType DerivedFrom,
Devang Patel13319ce2009-02-17 22:43:44 +0000806 DIArray Elements,
807 unsigned RuntimeLang) {
Owen Andersone277fed2009-07-07 16:31:25 +0000808
Devang Patele4b27562009-08-28 23:24:31 +0000809 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000810 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000811 Context.getNode(),
812 MDString::get(VMContext, Name),
813 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000814 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
815 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
816 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
817 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
818 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patele4b27562009-08-28 23:24:31 +0000819 DerivedFrom.getNode(),
820 Elements.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000821 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
Chris Lattnera45664f2008-11-10 02:56:27 +0000822 };
Devang Patele4b27562009-08-28 23:24:31 +0000823 return DICompositeType(MDNode::get(VMContext, &Elts[0], 12));
Chris Lattnera45664f2008-11-10 02:56:27 +0000824}
825
826
Devang Patelac16d442009-10-26 16:54:35 +0000827/// CreateCompositeType - Create a composite type like array, struct, etc.
828DICompositeType DIFactory::CreateCompositeTypeEx(unsigned Tag,
829 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000830 StringRef Name,
Devang Patelac16d442009-10-26 16:54:35 +0000831 DICompileUnit CompileUnit,
832 unsigned LineNumber,
833 Constant *SizeInBits,
834 Constant *AlignInBits,
835 Constant *OffsetInBits,
836 unsigned Flags,
837 DIType DerivedFrom,
838 DIArray Elements,
839 unsigned RuntimeLang) {
840
841 Value *Elts[] = {
842 GetTagConstant(Tag),
843 Context.getNode(),
844 MDString::get(VMContext, Name),
845 CompileUnit.getNode(),
846 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
847 SizeInBits,
848 AlignInBits,
849 OffsetInBits,
850 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
851 DerivedFrom.getNode(),
852 Elements.getNode(),
853 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
854 };
855 return DICompositeType(MDNode::get(VMContext, &Elts[0], 12));
856}
857
858
Chris Lattnera45664f2008-11-10 02:56:27 +0000859/// CreateSubprogram - Create a new descriptor for the specified subprogram.
860/// See comments in DISubprogram for descriptions of these fields. This
861/// method does not unique the generated descriptors.
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000862DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000863 StringRef Name,
864 StringRef DisplayName,
865 StringRef LinkageName,
Chris Lattnera45664f2008-11-10 02:56:27 +0000866 DICompileUnit CompileUnit,
867 unsigned LineNo, DIType Type,
868 bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000869 bool isDefinition) {
Devang Patel854967e2008-12-17 22:39:29 +0000870
Devang Patele4b27562009-08-28 23:24:31 +0000871 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000872 GetTagConstant(dwarf::DW_TAG_subprogram),
Devang Patele4b27562009-08-28 23:24:31 +0000873 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
874 Context.getNode(),
875 MDString::get(VMContext, Name),
876 MDString::get(VMContext, DisplayName),
877 MDString::get(VMContext, LinkageName),
878 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000879 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000880 Type.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000881 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
882 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition)
Chris Lattnera45664f2008-11-10 02:56:27 +0000883 };
Devang Patele4b27562009-08-28 23:24:31 +0000884 return DISubprogram(MDNode::get(VMContext, &Elts[0], 11));
Chris Lattnera45664f2008-11-10 02:56:27 +0000885}
886
887/// CreateGlobalVariable - Create a new descriptor for the specified global.
888DIGlobalVariable
Devang Patel65dbc902009-11-25 17:36:49 +0000889DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
890 StringRef DisplayName,
891 StringRef LinkageName,
Chris Lattnera45664f2008-11-10 02:56:27 +0000892 DICompileUnit CompileUnit,
893 unsigned LineNo, DIType Type,bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000894 bool isDefinition, llvm::GlobalVariable *Val) {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000895 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000896 GetTagConstant(dwarf::DW_TAG_variable),
Devang Patele4b27562009-08-28 23:24:31 +0000897 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
898 Context.getNode(),
899 MDString::get(VMContext, Name),
900 MDString::get(VMContext, DisplayName),
901 MDString::get(VMContext, LinkageName),
902 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000903 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000904 Type.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000905 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
906 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
Devang Patele4b27562009-08-28 23:24:31 +0000907 Val
Chris Lattnera45664f2008-11-10 02:56:27 +0000908 };
Devang Patele4b27562009-08-28 23:24:31 +0000909
910 Value *const *Vs = &Elts[0];
911 MDNode *Node = MDNode::get(VMContext,Vs, 12);
912
913 // Create a named metadata so that we do not lose this mdnode.
914 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
915 NMD->addElement(Node);
916
917 return DIGlobalVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +0000918}
919
920
921/// CreateVariable - Create a new descriptor for the specified variable.
922DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000923 StringRef Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000924 DICompileUnit CompileUnit, unsigned LineNo,
Devang Pateldd9db662009-01-30 18:20:31 +0000925 DIType Type) {
Devang Patele4b27562009-08-28 23:24:31 +0000926 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000927 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000928 Context.getNode(),
929 MDString::get(VMContext, Name),
930 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000931 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000932 Type.getNode(),
Chris Lattnera45664f2008-11-10 02:56:27 +0000933 };
Devang Patele4b27562009-08-28 23:24:31 +0000934 return DIVariable(MDNode::get(VMContext, &Elts[0], 6));
Chris Lattnera45664f2008-11-10 02:56:27 +0000935}
936
937
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000938/// CreateComplexVariable - Create a new descriptor for the specified variable
939/// which has a complex address expression for its address.
940DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
941 const std::string &Name,
942 DICompileUnit CompileUnit,
943 unsigned LineNo,
944 DIType Type, SmallVector<Value *, 9> &addr) {
945 SmallVector<Value *, 9> Elts;
946 Elts.push_back(GetTagConstant(Tag));
947 Elts.push_back(Context.getNode());
948 Elts.push_back(MDString::get(VMContext, Name));
949 Elts.push_back(CompileUnit.getNode());
950 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
951 Elts.push_back(Type.getNode());
952 Elts.insert(Elts.end(), addr.begin(), addr.end());
953
954 return DIVariable(MDNode::get(VMContext, &Elts[0], 6+addr.size()));
955}
956
957
Chris Lattnera45664f2008-11-10 02:56:27 +0000958/// CreateBlock - This creates a descriptor for a lexical block with the
Owen Anderson99035272009-07-07 17:12:53 +0000959/// specified parent VMContext.
Devang Patel5e005d82009-08-31 22:00:15 +0000960DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context) {
Devang Patele4b27562009-08-28 23:24:31 +0000961 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000962 GetTagConstant(dwarf::DW_TAG_lexical_block),
Devang Patele4b27562009-08-28 23:24:31 +0000963 Context.getNode()
Chris Lattnera45664f2008-11-10 02:56:27 +0000964 };
Devang Patel5e005d82009-08-31 22:00:15 +0000965 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 2));
Chris Lattnera45664f2008-11-10 02:56:27 +0000966}
967
Devang Patelf98d8fe2009-09-01 01:14:15 +0000968/// CreateLocation - Creates a debug info location.
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000969DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
Daniel Dunbara279bc32009-09-20 02:20:51 +0000970 DIScope S, DILocation OrigLoc) {
Devang Patelf98d8fe2009-09-01 01:14:15 +0000971 Value *Elts[] = {
972 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
973 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
974 S.getNode(),
975 OrigLoc.getNode(),
976 };
977 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
978}
979
Devang Patele54a5e82009-11-23 19:11:20 +0000980/// CreateLocation - Creates a debug info location.
981DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
982 DIScope S, MDNode *OrigLoc) {
983 Value *Elts[] = {
984 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
985 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
986 S.getNode(),
987 OrigLoc
988 };
989 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
990}
Chris Lattnera45664f2008-11-10 02:56:27 +0000991
992//===----------------------------------------------------------------------===//
993// DIFactory: Routines for inserting code into a function
994//===----------------------------------------------------------------------===//
995
Chris Lattnera45664f2008-11-10 02:56:27 +0000996/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Devang Patel6daf99b2009-11-10 22:05:35 +0000997Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
Devang Patel3ddf7042009-11-13 02:27:33 +0000998 Instruction *InsertBefore) {
999 // Cast the storage to a {}* for the call to llvm.dbg.declare.
1000 Storage = new BitCastInst(Storage, EmptyStructPtr, "", InsertBefore);
1001
Chris Lattnera45664f2008-11-10 02:56:27 +00001002 if (!DeclareFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +00001003 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1004
Devang Patele4b27562009-08-28 23:24:31 +00001005 Value *Args[] = { Storage, D.getNode() };
Devang Patel6daf99b2009-11-10 22:05:35 +00001006 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
Mike Stumpe4250392009-10-01 22:08:58 +00001007}
1008
1009/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Devang Patel6daf99b2009-11-10 22:05:35 +00001010Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
Devang Patel3ddf7042009-11-13 02:27:33 +00001011 BasicBlock *InsertAtEnd) {
1012 // Cast the storage to a {}* for the call to llvm.dbg.declare.
1013 Storage = new BitCastInst(Storage, EmptyStructPtr, "", InsertAtEnd);
1014
Mike Stumpe4250392009-10-01 22:08:58 +00001015 if (!DeclareFn)
1016 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1017
1018 Value *Args[] = { Storage, D.getNode() };
Devang Patel6daf99b2009-11-10 22:05:35 +00001019 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);
Chris Lattnera45664f2008-11-10 02:56:27 +00001020}
Torok Edwin620f2802008-12-16 09:07:36 +00001021
Devang Patele4b27562009-08-28 23:24:31 +00001022
Devang Pateld2f79a12009-07-28 19:55:13 +00001023//===----------------------------------------------------------------------===//
Devang Patel98c65172009-07-30 18:25:15 +00001024// DebugInfoFinder implementations.
Devang Pateld2f79a12009-07-28 19:55:13 +00001025//===----------------------------------------------------------------------===//
1026
Devang Patel98c65172009-07-30 18:25:15 +00001027/// processModule - Process entire module and collect debug info.
1028void DebugInfoFinder::processModule(Module &M) {
Devang Patele4b27562009-08-28 23:24:31 +00001029
Devang Patelbeab41b2009-10-07 22:04:08 +00001030 MetadataContext &TheMetadata = M.getContext().getMetadata();
1031 unsigned MDDbgKind = TheMetadata.getMDKind("dbg");
Devang Patel70d75ca2009-11-12 19:02:56 +00001032
Devang Pateld2f79a12009-07-28 19:55:13 +00001033 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1034 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
1035 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
1036 ++BI) {
Devang Patel70d75ca2009-11-12 19:02:56 +00001037 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
Devang Patelb4d31302009-07-31 18:18:52 +00001038 processDeclare(DDI);
Devang Patel6daf99b2009-11-10 22:05:35 +00001039 else if (MDDbgKind)
1040 if (MDNode *L = TheMetadata.getMD(MDDbgKind, BI))
1041 processLocation(DILocation(L));
Devang Pateld2f79a12009-07-28 19:55:13 +00001042 }
Devang Patele4b27562009-08-28 23:24:31 +00001043
1044 NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv");
1045 if (!NMD)
1046 return;
1047
1048 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
1049 DIGlobalVariable DIG(cast<MDNode>(NMD->getElement(i)));
Devang Pateld2f79a12009-07-28 19:55:13 +00001050 if (addGlobalVariable(DIG)) {
1051 addCompileUnit(DIG.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +00001052 processType(DIG.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +00001053 }
1054 }
1055}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001056
Devang Patel6daf99b2009-11-10 22:05:35 +00001057/// processLocation - Process DILocation.
1058void DebugInfoFinder::processLocation(DILocation Loc) {
1059 if (Loc.isNull()) return;
1060 DIScope S(Loc.getScope().getNode());
1061 if (S.isNull()) return;
1062 if (S.isCompileUnit())
1063 addCompileUnit(DICompileUnit(S.getNode()));
1064 else if (S.isSubprogram())
1065 processSubprogram(DISubprogram(S.getNode()));
1066 else if (S.isLexicalBlock())
1067 processLexicalBlock(DILexicalBlock(S.getNode()));
1068 processLocation(Loc.getOrigLocation());
1069}
1070
Devang Patel98c65172009-07-30 18:25:15 +00001071/// processType - Process DIType.
1072void DebugInfoFinder::processType(DIType DT) {
Devang Patel72bcdb62009-08-10 22:09:58 +00001073 if (!addType(DT))
Devang Pateld2f79a12009-07-28 19:55:13 +00001074 return;
1075
1076 addCompileUnit(DT.getCompileUnit());
Devang Patel6ceea332009-08-31 18:49:10 +00001077 if (DT.isCompositeType()) {
Devang Patele4b27562009-08-28 23:24:31 +00001078 DICompositeType DCT(DT.getNode());
Devang Patel98c65172009-07-30 18:25:15 +00001079 processType(DCT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001080 DIArray DA = DCT.getTypeArray();
1081 if (!DA.isNull())
1082 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1083 DIDescriptor D = DA.getElement(i);
Devang Patele4b27562009-08-28 23:24:31 +00001084 DIType TypeE = DIType(D.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001085 if (!TypeE.isNull())
Devang Patel98c65172009-07-30 18:25:15 +00001086 processType(TypeE);
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001087 else
Devang Patele4b27562009-08-28 23:24:31 +00001088 processSubprogram(DISubprogram(D.getNode()));
Devang Pateld2f79a12009-07-28 19:55:13 +00001089 }
Devang Patel6ceea332009-08-31 18:49:10 +00001090 } else if (DT.isDerivedType()) {
Devang Patele4b27562009-08-28 23:24:31 +00001091 DIDerivedType DDT(DT.getNode());
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001092 if (!DDT.isNull())
Devang Patel98c65172009-07-30 18:25:15 +00001093 processType(DDT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001094 }
1095}
1096
Devang Patelbeab41b2009-10-07 22:04:08 +00001097/// processLexicalBlock
1098void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
1099 if (LB.isNull())
1100 return;
1101 DIScope Context = LB.getContext();
1102 if (Context.isLexicalBlock())
1103 return processLexicalBlock(DILexicalBlock(Context.getNode()));
1104 else
1105 return processSubprogram(DISubprogram(Context.getNode()));
1106}
1107
Devang Patel98c65172009-07-30 18:25:15 +00001108/// processSubprogram - Process DISubprogram.
1109void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Patele802f1c2009-07-30 17:30:23 +00001110 if (SP.isNull())
1111 return;
Devang Pateld2f79a12009-07-28 19:55:13 +00001112 if (!addSubprogram(SP))
1113 return;
1114 addCompileUnit(SP.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +00001115 processType(SP.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +00001116}
1117
Devang Patelb4d31302009-07-31 18:18:52 +00001118/// processDeclare - Process DbgDeclareInst.
1119void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
Devang Patele4b27562009-08-28 23:24:31 +00001120 DIVariable DV(cast<MDNode>(DDI->getVariable()));
Devang Patelb4d31302009-07-31 18:18:52 +00001121 if (DV.isNull())
1122 return;
1123
Devang Patele4b27562009-08-28 23:24:31 +00001124 if (!NodesSeen.insert(DV.getNode()))
Devang Patelb4d31302009-07-31 18:18:52 +00001125 return;
1126
1127 addCompileUnit(DV.getCompileUnit());
1128 processType(DV.getType());
1129}
1130
Devang Patel72bcdb62009-08-10 22:09:58 +00001131/// addType - Add type into Tys.
1132bool DebugInfoFinder::addType(DIType DT) {
1133 if (DT.isNull())
1134 return false;
1135
Devang Patele4b27562009-08-28 23:24:31 +00001136 if (!NodesSeen.insert(DT.getNode()))
Devang Patel72bcdb62009-08-10 22:09:58 +00001137 return false;
1138
Devang Patele4b27562009-08-28 23:24:31 +00001139 TYs.push_back(DT.getNode());
Devang Patel72bcdb62009-08-10 22:09:58 +00001140 return true;
1141}
1142
Devang Pateld2f79a12009-07-28 19:55:13 +00001143/// addCompileUnit - Add compile unit into CUs.
Devang Patel98c65172009-07-30 18:25:15 +00001144bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001145 if (CU.isNull())
1146 return false;
1147
Devang Patele4b27562009-08-28 23:24:31 +00001148 if (!NodesSeen.insert(CU.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001149 return false;
1150
Devang Patele4b27562009-08-28 23:24:31 +00001151 CUs.push_back(CU.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001152 return true;
1153}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001154
Devang Pateld2f79a12009-07-28 19:55:13 +00001155/// addGlobalVariable - Add global variable into GVs.
Devang Patel98c65172009-07-30 18:25:15 +00001156bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001157 if (DIG.isNull())
1158 return false;
1159
Devang Patele4b27562009-08-28 23:24:31 +00001160 if (!NodesSeen.insert(DIG.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001161 return false;
1162
Devang Patele4b27562009-08-28 23:24:31 +00001163 GVs.push_back(DIG.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001164 return true;
1165}
1166
1167// addSubprogram - Add subprgoram into SPs.
Devang Patel98c65172009-07-30 18:25:15 +00001168bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001169 if (SP.isNull())
1170 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001171
Devang Patele4b27562009-08-28 23:24:31 +00001172 if (!NodesSeen.insert(SP.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001173 return false;
1174
Devang Patele4b27562009-08-28 23:24:31 +00001175 SPs.push_back(SP.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001176 return true;
1177}
1178
Torok Edwin620f2802008-12-16 09:07:36 +00001179namespace llvm {
Bill Wendlingdc817b62009-05-14 18:26:15 +00001180 /// findStopPoint - Find the stoppoint coressponding to this instruction, that
1181 /// is the stoppoint that dominates this instruction.
1182 const DbgStopPointInst *findStopPoint(const Instruction *Inst) {
Torok Edwin620f2802008-12-16 09:07:36 +00001183 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(Inst))
1184 return DSI;
1185
1186 const BasicBlock *BB = Inst->getParent();
1187 BasicBlock::const_iterator I = Inst, B;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001188 while (BB) {
Torok Edwin620f2802008-12-16 09:07:36 +00001189 B = BB->begin();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001190
Torok Edwin620f2802008-12-16 09:07:36 +00001191 // A BB consisting only of a terminator can't have a stoppoint.
Bill Wendlingdc817b62009-05-14 18:26:15 +00001192 while (I != B) {
1193 --I;
1194 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
1195 return DSI;
Torok Edwin620f2802008-12-16 09:07:36 +00001196 }
Bill Wendlingdc817b62009-05-14 18:26:15 +00001197
1198 // This BB didn't have a stoppoint: if there is only one predecessor, look
1199 // for a stoppoint there. We could use getIDom(), but that would require
1200 // dominator info.
Torok Edwin620f2802008-12-16 09:07:36 +00001201 BB = I->getParent()->getUniquePredecessor();
1202 if (BB)
1203 I = BB->getTerminator();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001204 }
1205
Torok Edwin620f2802008-12-16 09:07:36 +00001206 return 0;
1207 }
1208
Bill Wendlingdc817b62009-05-14 18:26:15 +00001209 /// findBBStopPoint - Find the stoppoint corresponding to first real
1210 /// (non-debug intrinsic) instruction in this Basic Block, and return the
1211 /// stoppoint for it.
1212 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB) {
1213 for(BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001214 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
1215 return DSI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001216
1217 // Fallback to looking for stoppoint of unique predecessor. Useful if this
1218 // BB contains no stoppoints, but unique predecessor does.
Torok Edwin620f2802008-12-16 09:07:36 +00001219 BB = BB->getUniquePredecessor();
1220 if (BB)
1221 return findStopPoint(BB->getTerminator());
Bill Wendlingdc817b62009-05-14 18:26:15 +00001222
Torok Edwin620f2802008-12-16 09:07:36 +00001223 return 0;
1224 }
1225
Bill Wendlingdc817b62009-05-14 18:26:15 +00001226 Value *findDbgGlobalDeclare(GlobalVariable *V) {
Torok Edwinff7d0e92009-03-10 13:41:26 +00001227 const Module *M = V->getParent();
Devang Patele4b27562009-08-28 23:24:31 +00001228 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1229 if (!NMD)
1230 return 0;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001231
Devang Patele4b27562009-08-28 23:24:31 +00001232 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
1233 DIGlobalVariable DIG(cast_or_null<MDNode>(NMD->getElement(i)));
1234 if (DIG.isNull())
1235 continue;
1236 if (DIG.getGlobal() == V)
1237 return DIG.getNode();
Torok Edwinff7d0e92009-03-10 13:41:26 +00001238 }
Torok Edwinff7d0e92009-03-10 13:41:26 +00001239 return 0;
1240 }
1241
Bill Wendlingdc817b62009-05-14 18:26:15 +00001242 /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
Torok Edwin620f2802008-12-16 09:07:36 +00001243 /// It looks through pointer casts too.
Bill Wendlingdc817b62009-05-14 18:26:15 +00001244 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts) {
Torok Edwin620f2802008-12-16 09:07:36 +00001245 if (stripCasts) {
1246 V = V->stripPointerCasts();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001247
Torok Edwin620f2802008-12-16 09:07:36 +00001248 // Look for the bitcast.
1249 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001250 I != E; ++I)
Devang Patel94dfaec2009-10-29 18:20:34 +00001251 if (isa<BitCastInst>(I)) {
1252 const DbgDeclareInst *DDI = findDbgDeclare(*I, false);
1253 if (DDI) return DDI;
1254 }
Torok Edwin620f2802008-12-16 09:07:36 +00001255 return 0;
1256 }
1257
Bill Wendlingdc817b62009-05-14 18:26:15 +00001258 // Find llvm.dbg.declare among uses of the instruction.
Torok Edwin620f2802008-12-16 09:07:36 +00001259 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001260 I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001261 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I))
1262 return DDI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001263
Torok Edwin620f2802008-12-16 09:07:36 +00001264 return 0;
1265 }
Torok Edwinff7d0e92009-03-10 13:41:26 +00001266
Devang Patel5ccdd102009-09-29 18:40:58 +00001267bool getLocationInfo(const Value *V, std::string &DisplayName,
1268 std::string &Type, unsigned &LineNo, std::string &File,
Bill Wendlingdc817b62009-05-14 18:26:15 +00001269 std::string &Dir) {
Torok Edwinff7d0e92009-03-10 13:41:26 +00001270 DICompileUnit Unit;
1271 DIType TypeD;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001272
Torok Edwinff7d0e92009-03-10 13:41:26 +00001273 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1274 Value *DIGV = findDbgGlobalDeclare(GV);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001275 if (!DIGV) return false;
Devang Patele4b27562009-08-28 23:24:31 +00001276 DIGlobalVariable Var(cast<MDNode>(DIGV));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001277
Devang Patel65dbc902009-11-25 17:36:49 +00001278 StringRef D = Var.getDisplayName();
1279 if (!D.empty())
Devang Patel5ccdd102009-09-29 18:40:58 +00001280 DisplayName = D;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001281 LineNo = Var.getLineNumber();
1282 Unit = Var.getCompileUnit();
1283 TypeD = Var.getType();
1284 } else {
1285 const DbgDeclareInst *DDI = findDbgDeclare(V);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001286 if (!DDI) return false;
Devang Patele4b27562009-08-28 23:24:31 +00001287 DIVariable Var(cast<MDNode>(DDI->getVariable()));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001288
Devang Patel65dbc902009-11-25 17:36:49 +00001289 StringRef D = Var.getName();
1290 if (!D.empty())
Devang Patel5ccdd102009-09-29 18:40:58 +00001291 DisplayName = D;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001292 LineNo = Var.getLineNumber();
1293 Unit = Var.getCompileUnit();
1294 TypeD = Var.getType();
1295 }
Bill Wendlingdc817b62009-05-14 18:26:15 +00001296
Devang Patel65dbc902009-11-25 17:36:49 +00001297 StringRef T = TypeD.getName();
1298 if (!T.empty())
Devang Patel5ccdd102009-09-29 18:40:58 +00001299 Type = T;
Devang Patel65dbc902009-11-25 17:36:49 +00001300 StringRef F = Unit.getFilename();
1301 if (!F.empty())
Devang Patel5ccdd102009-09-29 18:40:58 +00001302 File = F;
Devang Patel65dbc902009-11-25 17:36:49 +00001303 StringRef D = Unit.getDirectory();
1304 if (!D.empty())
Devang Patel5ccdd102009-09-29 18:40:58 +00001305 Dir = D;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001306 return true;
1307 }
Devang Patel13e16b62009-06-26 01:49:18 +00001308
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001309 /// isValidDebugInfoIntrinsic - Return true if SPI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001310 /// info intrinsic.
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001311 bool isValidDebugInfoIntrinsic(DbgStopPointInst &SPI,
Devang Patel9e529c32009-07-02 01:15:24 +00001312 CodeGenOpt::Level OptLev) {
1313 return DIDescriptor::ValidDebugInfo(SPI.getContext(), OptLev);
1314 }
1315
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001316 /// isValidDebugInfoIntrinsic - Return true if FSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001317 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001318 bool isValidDebugInfoIntrinsic(DbgFuncStartInst &FSI,
1319 CodeGenOpt::Level OptLev) {
1320 return DIDescriptor::ValidDebugInfo(FSI.getSubprogram(), OptLev);
1321 }
1322
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001323 /// isValidDebugInfoIntrinsic - Return true if RSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001324 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001325 bool isValidDebugInfoIntrinsic(DbgRegionStartInst &RSI,
1326 CodeGenOpt::Level OptLev) {
1327 return DIDescriptor::ValidDebugInfo(RSI.getContext(), OptLev);
1328 }
1329
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001330 /// isValidDebugInfoIntrinsic - Return true if REI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001331 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001332 bool isValidDebugInfoIntrinsic(DbgRegionEndInst &REI,
1333 CodeGenOpt::Level OptLev) {
1334 return DIDescriptor::ValidDebugInfo(REI.getContext(), OptLev);
1335 }
1336
1337
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001338 /// isValidDebugInfoIntrinsic - Return true if DI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001339 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001340 bool isValidDebugInfoIntrinsic(DbgDeclareInst &DI,
1341 CodeGenOpt::Level OptLev) {
1342 return DIDescriptor::ValidDebugInfo(DI.getVariable(), OptLev);
1343 }
1344
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001345 /// ExtractDebugLocation - Extract debug location information
Devang Patel9e529c32009-07-02 01:15:24 +00001346 /// from llvm.dbg.stoppoint intrinsic.
1347 DebugLoc ExtractDebugLocation(DbgStopPointInst &SPI,
Devang Patel9e529c32009-07-02 01:15:24 +00001348 DebugLocTracker &DebugLocInfo) {
1349 DebugLoc DL;
1350 Value *Context = SPI.getContext();
Devang Patel9e529c32009-07-02 01:15:24 +00001351
1352 // If this location is already tracked then use it.
Devang Patela1434042009-10-01 01:15:28 +00001353 DebugLocTuple Tuple(cast<MDNode>(Context), NULL, SPI.getLine(),
Devang Patel9e529c32009-07-02 01:15:24 +00001354 SPI.getColumn());
1355 DenseMap<DebugLocTuple, unsigned>::iterator II
1356 = DebugLocInfo.DebugIdMap.find(Tuple);
1357 if (II != DebugLocInfo.DebugIdMap.end())
1358 return DebugLoc::get(II->second);
1359
1360 // Add a new location entry.
1361 unsigned Id = DebugLocInfo.DebugLocations.size();
1362 DebugLocInfo.DebugLocations.push_back(Tuple);
1363 DebugLocInfo.DebugIdMap[Tuple] = Id;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001364
Devang Patel9e529c32009-07-02 01:15:24 +00001365 return DebugLoc::get(Id);
1366 }
1367
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001368 /// ExtractDebugLocation - Extract debug location information
Devang Patel1b75f442009-09-16 18:20:05 +00001369 /// from DILocation.
1370 DebugLoc ExtractDebugLocation(DILocation &Loc,
1371 DebugLocTracker &DebugLocInfo) {
1372 DebugLoc DL;
1373 MDNode *Context = Loc.getScope().getNode();
Devang Patela1434042009-10-01 01:15:28 +00001374 MDNode *InlinedLoc = NULL;
1375 if (!Loc.getOrigLocation().isNull())
1376 InlinedLoc = Loc.getOrigLocation().getNode();
Devang Patel1b75f442009-09-16 18:20:05 +00001377 // If this location is already tracked then use it.
Devang Patela1434042009-10-01 01:15:28 +00001378 DebugLocTuple Tuple(Context, InlinedLoc, Loc.getLineNumber(),
Daniel Dunbara279bc32009-09-20 02:20:51 +00001379 Loc.getColumnNumber());
Devang Patel1b75f442009-09-16 18:20:05 +00001380 DenseMap<DebugLocTuple, unsigned>::iterator II
1381 = DebugLocInfo.DebugIdMap.find(Tuple);
1382 if (II != DebugLocInfo.DebugIdMap.end())
1383 return DebugLoc::get(II->second);
1384
1385 // Add a new location entry.
1386 unsigned Id = DebugLocInfo.DebugLocations.size();
1387 DebugLocInfo.DebugLocations.push_back(Tuple);
1388 DebugLocInfo.DebugIdMap[Tuple] = Id;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001389
Devang Patel1b75f442009-09-16 18:20:05 +00001390 return DebugLoc::get(Id);
1391 }
1392
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001393 /// ExtractDebugLocation - Extract debug location information
Devang Patel9e529c32009-07-02 01:15:24 +00001394 /// from llvm.dbg.func_start intrinsic.
1395 DebugLoc ExtractDebugLocation(DbgFuncStartInst &FSI,
Devang Patel9e529c32009-07-02 01:15:24 +00001396 DebugLocTracker &DebugLocInfo) {
1397 DebugLoc DL;
1398 Value *SP = FSI.getSubprogram();
Devang Patel9e529c32009-07-02 01:15:24 +00001399
Devang Patele4b27562009-08-28 23:24:31 +00001400 DISubprogram Subprogram(cast<MDNode>(SP));
Devang Patel9e529c32009-07-02 01:15:24 +00001401 unsigned Line = Subprogram.getLineNumber();
1402 DICompileUnit CU(Subprogram.getCompileUnit());
1403
1404 // If this location is already tracked then use it.
Devang Patela1434042009-10-01 01:15:28 +00001405 DebugLocTuple Tuple(CU.getNode(), NULL, Line, /* Column */ 0);
Devang Patel9e529c32009-07-02 01:15:24 +00001406 DenseMap<DebugLocTuple, unsigned>::iterator II
1407 = DebugLocInfo.DebugIdMap.find(Tuple);
1408 if (II != DebugLocInfo.DebugIdMap.end())
1409 return DebugLoc::get(II->second);
1410
1411 // Add a new location entry.
1412 unsigned Id = DebugLocInfo.DebugLocations.size();
1413 DebugLocInfo.DebugLocations.push_back(Tuple);
1414 DebugLocInfo.DebugIdMap[Tuple] = Id;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001415
Devang Patel9e529c32009-07-02 01:15:24 +00001416 return DebugLoc::get(Id);
1417 }
Devang Patel193f7202009-11-24 01:14:22 +00001418
1419 /// getDISubprogram - Find subprogram that is enclosing this scope.
1420 DISubprogram getDISubprogram(MDNode *Scope) {
1421 DIDescriptor D(Scope);
1422 if (D.isNull())
1423 return DISubprogram();
1424
1425 if (D.isCompileUnit())
1426 return DISubprogram();
1427
1428 if (D.isSubprogram())
1429 return DISubprogram(Scope);
1430
1431 if (D.isLexicalBlock())
1432 return getDISubprogram(DILexicalBlock(Scope).getContext().getNode());
1433
1434 return DISubprogram();
1435 }
1436
1437 /// getDICompositeType - Find underlying composite type.
1438 DICompositeType getDICompositeType(DIType T) {
1439 if (T.isNull())
1440 return DICompositeType();
1441
1442 if (T.isCompositeType())
1443 return DICompositeType(T.getNode());
1444
1445 if (T.isDerivedType())
1446 return getDICompositeType(DIDerivedType(T.getNode()).getTypeDerivedFrom());
1447
1448 return DICompositeType();
1449 }
Torok Edwin620f2802008-12-16 09:07:36 +00001450}