blob: 3e3b8023024b490a9733245f33258f58ea93bac9 [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;
Chris Lattnera45664f2008-11-10 02:56:27 +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
Bill Wendling0582ae92009-03-13 04:39:26 +000081const std::string &
82DIDescriptor::getStringField(unsigned Elt, std::string &Result) const {
Devang Patele4b27562009-08-28 23:24:31 +000083 Result.clear();
84 if (DbgNode == 0)
Bill Wendling0582ae92009-03-13 04:39:26 +000085 return Result;
Chris Lattnera45664f2008-11-10 02:56:27 +000086
Devang Patele4b27562009-08-28 23:24:31 +000087 if (Elt < DbgNode->getNumElements())
88 if (MDString *MDS = dyn_cast_or_null<MDString>(DbgNode->getElement(Elt))) {
89 Result.assign(MDS->begin(), MDS->begin() + MDS->length());
90 return Result;
91 }
92
Bill Wendling0582ae92009-03-13 04:39:26 +000093 return Result;
Chris Lattnera45664f2008-11-10 02:56:27 +000094}
95
96uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
Devang Patele4b27562009-08-28 23:24:31 +000097 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +000098 return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +000099
Devang Patele4b27562009-08-28 23:24:31 +0000100 if (Elt < DbgNode->getNumElements())
101 if (ConstantInt *CI = dyn_cast<ConstantInt>(DbgNode->getElement(Elt)))
102 return CI->getZExtValue();
103
Chris Lattnera45664f2008-11-10 02:56:27 +0000104 return 0;
105}
106
Chris Lattnera45664f2008-11-10 02:56:27 +0000107DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
Devang Patele4b27562009-08-28 23:24:31 +0000108 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +0000109 return DIDescriptor();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000110
Devang Patele4b27562009-08-28 23:24:31 +0000111 if (Elt < DbgNode->getNumElements() && DbgNode->getElement(Elt))
112 return DIDescriptor(dyn_cast<MDNode>(DbgNode->getElement(Elt)));
113
114 return DIDescriptor();
Chris Lattnera45664f2008-11-10 02:56:27 +0000115}
116
117GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
Devang Patele4b27562009-08-28 23:24:31 +0000118 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +0000119 return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000120
Devang Patele4b27562009-08-28 23:24:31 +0000121 if (Elt < DbgNode->getNumElements())
122 return dyn_cast<GlobalVariable>(DbgNode->getElement(Elt));
123 return 0;
Chris Lattnera45664f2008-11-10 02:56:27 +0000124}
125
Chris Lattnera45664f2008-11-10 02:56:27 +0000126//===----------------------------------------------------------------------===//
Devang Patel6ceea332009-08-31 18:49:10 +0000127// Predicates
Chris Lattnera45664f2008-11-10 02:56:27 +0000128//===----------------------------------------------------------------------===//
129
Devang Patel6ceea332009-08-31 18:49:10 +0000130/// isBasicType - Return true if the specified tag is legal for
131/// DIBasicType.
132bool DIDescriptor::isBasicType() const {
Devang Patel5a685092009-08-31 20:27:49 +0000133 assert (!isNull() && "Invalid descriptor!");
Devang Patel6ceea332009-08-31 18:49:10 +0000134 unsigned Tag = getTag();
135
136 return Tag == dwarf::DW_TAG_base_type;
Torok Edwinb07fbd92008-12-13 08:25:29 +0000137}
Chris Lattnera45664f2008-11-10 02:56:27 +0000138
Devang Patel6ceea332009-08-31 18:49:10 +0000139/// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
140bool DIDescriptor::isDerivedType() const {
Devang Patel5a685092009-08-31 20:27:49 +0000141 assert (!isNull() && "Invalid descriptor!");
Devang Patel6ceea332009-08-31 18:49:10 +0000142 unsigned Tag = getTag();
143
Chris Lattnera45664f2008-11-10 02:56:27 +0000144 switch (Tag) {
145 case dwarf::DW_TAG_typedef:
146 case dwarf::DW_TAG_pointer_type:
147 case dwarf::DW_TAG_reference_type:
148 case dwarf::DW_TAG_const_type:
149 case dwarf::DW_TAG_volatile_type:
150 case dwarf::DW_TAG_restrict_type:
151 case dwarf::DW_TAG_member:
152 case dwarf::DW_TAG_inheritance:
153 return true;
154 default:
Devang Patele4b27562009-08-28 23:24:31 +0000155 // CompositeTypes are currently modelled as DerivedTypes.
Devang Patel6ceea332009-08-31 18:49:10 +0000156 return isCompositeType();
Chris Lattnera45664f2008-11-10 02:56:27 +0000157 }
158}
159
Chris Lattnera45664f2008-11-10 02:56:27 +0000160/// isCompositeType - Return true if the specified tag is legal for
161/// DICompositeType.
Devang Patel6ceea332009-08-31 18:49:10 +0000162bool DIDescriptor::isCompositeType() const {
Devang Patel5a685092009-08-31 20:27:49 +0000163 assert (!isNull() && "Invalid descriptor!");
Devang Patel6ceea332009-08-31 18:49:10 +0000164 unsigned Tag = getTag();
165
166 switch (Tag) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000167 case dwarf::DW_TAG_array_type:
168 case dwarf::DW_TAG_structure_type:
169 case dwarf::DW_TAG_union_type:
170 case dwarf::DW_TAG_enumeration_type:
171 case dwarf::DW_TAG_vector_type:
172 case dwarf::DW_TAG_subroutine_type:
Devang Patel25cb0d72009-03-25 03:52:06 +0000173 case dwarf::DW_TAG_class_type:
Chris Lattnera45664f2008-11-10 02:56:27 +0000174 return true;
175 default:
176 return false;
177 }
178}
179
Chris Lattnera45664f2008-11-10 02:56:27 +0000180/// isVariable - Return true if the specified tag is legal for DIVariable.
Devang Patel6ceea332009-08-31 18:49:10 +0000181bool DIDescriptor::isVariable() const {
Devang Patel5a685092009-08-31 20:27:49 +0000182 assert (!isNull() && "Invalid descriptor!");
Devang Patel6ceea332009-08-31 18:49:10 +0000183 unsigned Tag = getTag();
184
Chris Lattnera45664f2008-11-10 02:56:27 +0000185 switch (Tag) {
186 case dwarf::DW_TAG_auto_variable:
187 case dwarf::DW_TAG_arg_variable:
188 case dwarf::DW_TAG_return_variable:
189 return true;
190 default:
191 return false;
192 }
193}
194
Devang Patel6ceea332009-08-31 18:49:10 +0000195/// isSubprogram - Return true if the specified tag is legal for
196/// DISubprogram.
197bool DIDescriptor::isSubprogram() const {
Devang Patel5a685092009-08-31 20:27:49 +0000198 assert (!isNull() && "Invalid descriptor!");
Devang Patel6ceea332009-08-31 18:49:10 +0000199 unsigned Tag = getTag();
200
201 return Tag == dwarf::DW_TAG_subprogram;
202}
203
204/// isGlobalVariable - Return true if the specified tag is legal for
205/// DIGlobalVariable.
206bool DIDescriptor::isGlobalVariable() const {
Devang Patel5a685092009-08-31 20:27:49 +0000207 assert (!isNull() && "Invalid descriptor!");
Devang Patel6ceea332009-08-31 18:49:10 +0000208 unsigned Tag = getTag();
209
210 return Tag == dwarf::DW_TAG_variable;
211}
212
Devang Patel43d98b32009-08-31 20:44:45 +0000213/// isScope - Return true if the specified tag is one of the scope
214/// related tag.
215bool DIDescriptor::isScope() const {
216 assert (!isNull() && "Invalid descriptor!");
217 unsigned Tag = getTag();
218
219 switch (Tag) {
220 case dwarf::DW_TAG_compile_unit:
221 case dwarf::DW_TAG_lexical_block:
222 case dwarf::DW_TAG_subprogram:
223 return true;
224 default:
225 break;
226 }
227 return false;
228}
Devang Patel6ceea332009-08-31 18:49:10 +0000229
Devang Patelc9f322d2009-08-31 21:34:44 +0000230/// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
231bool DIDescriptor::isCompileUnit() const {
232 assert (!isNull() && "Invalid descriptor!");
233 unsigned Tag = getTag();
234
235 return Tag == dwarf::DW_TAG_compile_unit;
236}
237
Devang Patel6ceea332009-08-31 18:49:10 +0000238//===----------------------------------------------------------------------===//
239// Simple Descriptor Constructors and other Methods
240//===----------------------------------------------------------------------===//
241
242DIType::DIType(MDNode *N) : DIDescriptor(N) {
243 if (!N) return;
244 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
245 DbgNode = 0;
246 }
247}
248
Devang Patel68afdc32009-01-05 18:33:01 +0000249unsigned DIArray::getNumElements() const {
Devang Patele4b27562009-08-28 23:24:31 +0000250 assert (DbgNode && "Invalid DIArray");
251 return DbgNode->getNumElements();
Devang Patel68afdc32009-01-05 18:33:01 +0000252}
Chris Lattnera45664f2008-11-10 02:56:27 +0000253
Devang Patelc4999d72009-07-22 18:23:44 +0000254/// replaceAllUsesWith - Replace all uses of debug info referenced by
255/// this descriptor. After this completes, the current debug info value
256/// is erased.
257void DIDerivedType::replaceAllUsesWith(DIDescriptor &D) {
258 if (isNull())
259 return;
260
Devang Patel6930f4f2009-07-22 18:56:16 +0000261 assert (!D.isNull() && "Can not replace with null");
Devang Patele4b27562009-08-28 23:24:31 +0000262 DbgNode->replaceAllUsesWith(D.getNode());
263 delete DbgNode;
Devang Patelc4999d72009-07-22 18:23:44 +0000264}
265
Devang Patelb79b5352009-01-19 23:21:49 +0000266/// Verify - Verify that a compile unit is well formed.
267bool DICompileUnit::Verify() const {
268 if (isNull())
269 return false;
Bill Wendling0582ae92009-03-13 04:39:26 +0000270 std::string Res;
271 if (getFilename(Res).empty())
272 return false;
Devang Patelb79b5352009-01-19 23:21:49 +0000273 // It is possible that directory and produce string is empty.
Bill Wendling0582ae92009-03-13 04:39:26 +0000274 return true;
Devang Patelb79b5352009-01-19 23:21:49 +0000275}
276
277/// Verify - Verify that a type descriptor is well formed.
278bool DIType::Verify() const {
279 if (isNull())
280 return false;
281 if (getContext().isNull())
282 return false;
283
284 DICompileUnit CU = getCompileUnit();
285 if (!CU.isNull() && !CU.Verify())
286 return false;
287 return true;
288}
289
290/// Verify - Verify that a composite type descriptor is well formed.
291bool DICompositeType::Verify() const {
292 if (isNull())
293 return false;
294 if (getContext().isNull())
295 return false;
296
297 DICompileUnit CU = getCompileUnit();
298 if (!CU.isNull() && !CU.Verify())
299 return false;
300 return true;
301}
302
303/// Verify - Verify that a subprogram descriptor is well formed.
304bool DISubprogram::Verify() const {
305 if (isNull())
306 return false;
307
308 if (getContext().isNull())
309 return false;
310
311 DICompileUnit CU = getCompileUnit();
312 if (!CU.Verify())
313 return false;
314
315 DICompositeType Ty = getType();
316 if (!Ty.isNull() && !Ty.Verify())
317 return false;
318 return true;
319}
320
321/// Verify - Verify that a global variable descriptor is well formed.
322bool DIGlobalVariable::Verify() const {
323 if (isNull())
324 return false;
325
326 if (getContext().isNull())
327 return false;
328
329 DICompileUnit CU = getCompileUnit();
Chris Lattnere3f6cea2009-05-05 04:55:56 +0000330 if (!CU.isNull() && !CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000331 return false;
332
333 DIType Ty = getType();
334 if (!Ty.Verify())
335 return false;
336
337 if (!getGlobal())
338 return false;
339
340 return true;
341}
342
343/// Verify - Verify that a variable descriptor is well formed.
344bool DIVariable::Verify() const {
345 if (isNull())
346 return false;
347
348 if (getContext().isNull())
349 return false;
350
351 DIType Ty = getType();
352 if (!Ty.Verify())
353 return false;
354
Devang Patelb79b5352009-01-19 23:21:49 +0000355 return true;
356}
357
Devang Patel36375ee2009-02-17 21:23:59 +0000358/// getOriginalTypeSize - If this type is derived from a base type then
359/// return base type size.
360uint64_t DIDerivedType::getOriginalTypeSize() const {
361 if (getTag() != dwarf::DW_TAG_member)
362 return getSizeInBits();
363 DIType BT = getTypeDerivedFrom();
364 if (BT.getTag() != dwarf::DW_TAG_base_type)
365 return getSizeInBits();
366 return BT.getSizeInBits();
367}
Devang Patelb79b5352009-01-19 23:21:49 +0000368
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000369/// describes - Return true if this subprogram provides debugging
370/// information for the function F.
371bool DISubprogram::describes(const Function *F) {
372 assert (F && "Invalid function");
373 std::string Name;
374 getLinkageName(Name);
375 if (Name.empty())
376 getName(Name);
Daniel Dunbar460f6562009-07-26 09:48:23 +0000377 if (F->getName() == Name)
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000378 return true;
379 return false;
380}
381
Chris Lattnera45664f2008-11-10 02:56:27 +0000382//===----------------------------------------------------------------------===//
Devang Patel7136a652009-07-01 22:10:23 +0000383// DIDescriptor: dump routines for all descriptors.
384//===----------------------------------------------------------------------===//
385
386
387/// dump - Print descriptor.
388void DIDescriptor::dump() const {
Devang Patele4b27562009-08-28 23:24:31 +0000389 errs() << "[" << dwarf::TagString(getTag()) << "] ";
390 errs().write_hex((intptr_t)DbgNode) << ']';
Devang Patel7136a652009-07-01 22:10:23 +0000391}
392
393/// dump - Print compile unit.
394void DICompileUnit::dump() const {
395 if (getLanguage())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000396 errs() << " [" << dwarf::LanguageString(getLanguage()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000397
398 std::string Res1, Res2;
Chris Lattnera81d29b2009-08-23 07:33:14 +0000399 errs() << " [" << getDirectory(Res1) << "/" << getFilename(Res2) << " ]";
Devang Patel7136a652009-07-01 22:10:23 +0000400}
401
402/// dump - Print type.
403void DIType::dump() const {
404 if (isNull()) return;
405
406 std::string Res;
407 if (!getName(Res).empty())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000408 errs() << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000409
410 unsigned Tag = getTag();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000411 errs() << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000412
413 // TODO : Print context
414 getCompileUnit().dump();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000415 errs() << " ["
416 << getLineNumber() << ", "
417 << getSizeInBits() << ", "
418 << getAlignInBits() << ", "
419 << getOffsetInBits()
420 << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000421
422 if (isPrivate())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000423 errs() << " [private] ";
Devang Patel7136a652009-07-01 22:10:23 +0000424 else if (isProtected())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000425 errs() << " [protected] ";
Devang Patel7136a652009-07-01 22:10:23 +0000426
427 if (isForwardDecl())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000428 errs() << " [fwd] ";
Devang Patel7136a652009-07-01 22:10:23 +0000429
Devang Patel6ceea332009-08-31 18:49:10 +0000430 if (isBasicType())
Devang Patele4b27562009-08-28 23:24:31 +0000431 DIBasicType(DbgNode).dump();
Devang Patel6ceea332009-08-31 18:49:10 +0000432 else if (isDerivedType())
Devang Patele4b27562009-08-28 23:24:31 +0000433 DIDerivedType(DbgNode).dump();
Devang Patel6ceea332009-08-31 18:49:10 +0000434 else if (isCompositeType())
Devang Patele4b27562009-08-28 23:24:31 +0000435 DICompositeType(DbgNode).dump();
Devang Patel7136a652009-07-01 22:10:23 +0000436 else {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000437 errs() << "Invalid DIType\n";
Devang Patel7136a652009-07-01 22:10:23 +0000438 return;
439 }
440
Chris Lattnera81d29b2009-08-23 07:33:14 +0000441 errs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000442}
443
444/// dump - Print basic type.
445void DIBasicType::dump() const {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000446 errs() << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000447}
448
449/// dump - Print derived type.
450void DIDerivedType::dump() const {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000451 errs() << "\n\t Derived From: "; getTypeDerivedFrom().dump();
Devang Patel7136a652009-07-01 22:10:23 +0000452}
453
454/// dump - Print composite type.
455void DICompositeType::dump() const {
456 DIArray A = getTypeArray();
457 if (A.isNull())
458 return;
Chris Lattnera81d29b2009-08-23 07:33:14 +0000459 errs() << " [" << A.getNumElements() << " elements]";
Devang Patel7136a652009-07-01 22:10:23 +0000460}
461
462/// dump - Print global.
463void DIGlobal::dump() const {
464 std::string Res;
465 if (!getName(Res).empty())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000466 errs() << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000467
468 unsigned Tag = getTag();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000469 errs() << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000470
471 // TODO : Print context
472 getCompileUnit().dump();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000473 errs() << " [" << getLineNumber() << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000474
475 if (isLocalToUnit())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000476 errs() << " [local] ";
Devang Patel7136a652009-07-01 22:10:23 +0000477
478 if (isDefinition())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000479 errs() << " [def] ";
Devang Patel7136a652009-07-01 22:10:23 +0000480
Devang Patel6ceea332009-08-31 18:49:10 +0000481 if (isGlobalVariable())
Devang Patele4b27562009-08-28 23:24:31 +0000482 DIGlobalVariable(DbgNode).dump();
Devang Patel7136a652009-07-01 22:10:23 +0000483
Chris Lattnera81d29b2009-08-23 07:33:14 +0000484 errs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000485}
486
487/// dump - Print subprogram.
488void DISubprogram::dump() const {
489 DIGlobal::dump();
490}
491
492/// dump - Print global variable.
493void DIGlobalVariable::dump() const {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000494 errs() << " [";
495 getGlobal()->dump();
496 errs() << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000497}
498
499/// dump - Print variable.
500void DIVariable::dump() const {
501 std::string Res;
502 if (!getName(Res).empty())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000503 errs() << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000504
505 getCompileUnit().dump();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000506 errs() << " [" << getLineNumber() << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000507 getType().dump();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000508 errs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000509}
510
511//===----------------------------------------------------------------------===//
Chris Lattnera45664f2008-11-10 02:56:27 +0000512// DIFactory: Basic Helpers
513//===----------------------------------------------------------------------===//
514
Bill Wendlingdc817b62009-05-14 18:26:15 +0000515DIFactory::DIFactory(Module &m)
Owen Anderson99035272009-07-07 17:12:53 +0000516 : M(m), VMContext(M.getContext()), StopPointFn(0), FuncStartFn(0),
517 RegionStartFn(0), RegionEndFn(0),
Bill Wendlingdc817b62009-05-14 18:26:15 +0000518 DeclareFn(0) {
Owen Andersond7f2a6c2009-08-05 23:16:16 +0000519 EmptyStructPtr = PointerType::getUnqual(StructType::get(VMContext));
Chris Lattner497a7a82008-11-10 04:10:34 +0000520}
521
Chris Lattnera45664f2008-11-10 02:56:27 +0000522Constant *DIFactory::GetTagConstant(unsigned TAG) {
Devang Patel6906ba52009-01-20 19:22:03 +0000523 assert((TAG & LLVMDebugVersionMask) == 0 &&
Chris Lattnera45664f2008-11-10 02:56:27 +0000524 "Tag too large for debug encoding!");
Owen Anderson1d0be152009-08-13 21:58:54 +0000525 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
Chris Lattnera45664f2008-11-10 02:56:27 +0000526}
527
Chris Lattnera45664f2008-11-10 02:56:27 +0000528//===----------------------------------------------------------------------===//
529// DIFactory: Primary Constructors
530//===----------------------------------------------------------------------===//
531
Chris Lattnera45664f2008-11-10 02:56:27 +0000532/// GetOrCreateArray - Create an descriptor for an array of descriptors.
533/// This implicitly uniques the arrays created.
534DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
Devang Patele4b27562009-08-28 23:24:31 +0000535 SmallVector<Value*, 16> Elts;
Chris Lattnera45664f2008-11-10 02:56:27 +0000536
Devang Patele4b27562009-08-28 23:24:31 +0000537 if (NumTys == 0)
538 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)));
539 else
540 for (unsigned i = 0; i != NumTys; ++i)
541 Elts.push_back(Tys[i].getNode());
Devang Patel82459882009-08-26 05:01:18 +0000542
Devang Patele4b27562009-08-28 23:24:31 +0000543 return DIArray(MDNode::get(VMContext,Elts.data(), Elts.size()));
Chris Lattnera45664f2008-11-10 02:56:27 +0000544}
545
546/// GetOrCreateSubrange - Create a descriptor for a value range. This
547/// implicitly uniques the values returned.
548DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
Devang Patele4b27562009-08-28 23:24:31 +0000549 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000550 GetTagConstant(dwarf::DW_TAG_subrange_type),
Owen Anderson1d0be152009-08-13 21:58:54 +0000551 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
552 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
Chris Lattnera45664f2008-11-10 02:56:27 +0000553 };
554
Devang Patele4b27562009-08-28 23:24:31 +0000555 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000556}
557
558
559
560/// CreateCompileUnit - Create a new descriptor for the specified compile
561/// unit. Note that this does not unique compile units within the module.
562DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
563 const std::string &Filename,
564 const std::string &Directory,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000565 const std::string &Producer,
Devang Pateldd9db662009-01-30 18:20:31 +0000566 bool isMain,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000567 bool isOptimized,
Devang Patel13319ce2009-02-17 22:43:44 +0000568 const char *Flags,
569 unsigned RunTimeVer) {
Devang Patele4b27562009-08-28 23:24:31 +0000570 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000571 GetTagConstant(dwarf::DW_TAG_compile_unit),
Devang Patele4b27562009-08-28 23:24:31 +0000572 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Owen Anderson1d0be152009-08-13 21:58:54 +0000573 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
Devang Patele4b27562009-08-28 23:24:31 +0000574 MDString::get(VMContext, Filename),
575 MDString::get(VMContext, Directory),
576 MDString::get(VMContext, Producer),
Owen Anderson1d0be152009-08-13 21:58:54 +0000577 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
578 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
Devang Patele4b27562009-08-28 23:24:31 +0000579 MDString::get(VMContext, Flags),
Owen Anderson1d0be152009-08-13 21:58:54 +0000580 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
Chris Lattnera45664f2008-11-10 02:56:27 +0000581 };
Devang Patele4b27562009-08-28 23:24:31 +0000582
583 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000584}
585
586/// CreateEnumerator - Create a single enumerator value.
587DIEnumerator DIFactory::CreateEnumerator(const std::string &Name, uint64_t Val){
Devang Patele4b27562009-08-28 23:24:31 +0000588 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000589 GetTagConstant(dwarf::DW_TAG_enumerator),
Devang Patele4b27562009-08-28 23:24:31 +0000590 MDString::get(VMContext, Name),
Owen Anderson1d0be152009-08-13 21:58:54 +0000591 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
Chris Lattnera45664f2008-11-10 02:56:27 +0000592 };
Devang Patele4b27562009-08-28 23:24:31 +0000593 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000594}
595
596
597/// CreateBasicType - Create a basic type like int, float, etc.
598DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
Bill Wendling0582ae92009-03-13 04:39:26 +0000599 const std::string &Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000600 DICompileUnit CompileUnit,
601 unsigned LineNumber,
602 uint64_t SizeInBits,
603 uint64_t AlignInBits,
604 uint64_t OffsetInBits, unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000605 unsigned Encoding) {
Devang Patele4b27562009-08-28 23:24:31 +0000606 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000607 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patele4b27562009-08-28 23:24:31 +0000608 Context.getNode(),
609 MDString::get(VMContext, Name),
610 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000611 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
612 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
613 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
614 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
615 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
616 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
Chris Lattnera45664f2008-11-10 02:56:27 +0000617 };
Devang Patele4b27562009-08-28 23:24:31 +0000618 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000619}
620
621/// CreateDerivedType - Create a derived type like const qualified type,
622/// pointer, typedef, etc.
623DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
624 DIDescriptor Context,
625 const std::string &Name,
626 DICompileUnit CompileUnit,
627 unsigned LineNumber,
628 uint64_t SizeInBits,
629 uint64_t AlignInBits,
630 uint64_t OffsetInBits,
631 unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000632 DIType DerivedFrom) {
Devang Patele4b27562009-08-28 23:24:31 +0000633 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000634 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000635 Context.getNode(),
636 MDString::get(VMContext, Name),
637 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000638 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
639 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
640 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
641 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
642 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patele4b27562009-08-28 23:24:31 +0000643 DerivedFrom.getNode(),
Chris Lattnera45664f2008-11-10 02:56:27 +0000644 };
Devang Patele4b27562009-08-28 23:24:31 +0000645 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000646}
647
648/// CreateCompositeType - Create a composite type like array, struct, etc.
649DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
650 DIDescriptor Context,
651 const std::string &Name,
652 DICompileUnit CompileUnit,
653 unsigned LineNumber,
654 uint64_t SizeInBits,
655 uint64_t AlignInBits,
656 uint64_t OffsetInBits,
657 unsigned Flags,
658 DIType DerivedFrom,
Devang Patel13319ce2009-02-17 22:43:44 +0000659 DIArray Elements,
660 unsigned RuntimeLang) {
Owen Andersone277fed2009-07-07 16:31:25 +0000661
Devang Patele4b27562009-08-28 23:24:31 +0000662 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000663 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000664 Context.getNode(),
665 MDString::get(VMContext, Name),
666 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000667 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
668 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
669 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
670 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
671 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patele4b27562009-08-28 23:24:31 +0000672 DerivedFrom.getNode(),
673 Elements.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000674 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
Chris Lattnera45664f2008-11-10 02:56:27 +0000675 };
Devang Patele4b27562009-08-28 23:24:31 +0000676 return DICompositeType(MDNode::get(VMContext, &Elts[0], 12));
Chris Lattnera45664f2008-11-10 02:56:27 +0000677}
678
679
680/// CreateSubprogram - Create a new descriptor for the specified subprogram.
681/// See comments in DISubprogram for descriptions of these fields. This
682/// method does not unique the generated descriptors.
683DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
684 const std::string &Name,
685 const std::string &DisplayName,
686 const std::string &LinkageName,
687 DICompileUnit CompileUnit,
688 unsigned LineNo, DIType Type,
689 bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000690 bool isDefinition) {
Devang Patel854967e2008-12-17 22:39:29 +0000691
Devang Patele4b27562009-08-28 23:24:31 +0000692 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000693 GetTagConstant(dwarf::DW_TAG_subprogram),
Devang Patele4b27562009-08-28 23:24:31 +0000694 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
695 Context.getNode(),
696 MDString::get(VMContext, Name),
697 MDString::get(VMContext, DisplayName),
698 MDString::get(VMContext, LinkageName),
699 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000700 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000701 Type.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000702 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
703 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition)
Chris Lattnera45664f2008-11-10 02:56:27 +0000704 };
Devang Patele4b27562009-08-28 23:24:31 +0000705 return DISubprogram(MDNode::get(VMContext, &Elts[0], 11));
Chris Lattnera45664f2008-11-10 02:56:27 +0000706}
707
708/// CreateGlobalVariable - Create a new descriptor for the specified global.
709DIGlobalVariable
710DIFactory::CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
711 const std::string &DisplayName,
712 const std::string &LinkageName,
713 DICompileUnit CompileUnit,
714 unsigned LineNo, DIType Type,bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000715 bool isDefinition, llvm::GlobalVariable *Val) {
Devang Patele4b27562009-08-28 23:24:31 +0000716 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000717 GetTagConstant(dwarf::DW_TAG_variable),
Devang Patele4b27562009-08-28 23:24:31 +0000718 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
719 Context.getNode(),
720 MDString::get(VMContext, Name),
721 MDString::get(VMContext, DisplayName),
722 MDString::get(VMContext, LinkageName),
723 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000724 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000725 Type.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000726 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
727 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
Devang Patele4b27562009-08-28 23:24:31 +0000728 Val
Chris Lattnera45664f2008-11-10 02:56:27 +0000729 };
Devang Patele4b27562009-08-28 23:24:31 +0000730
731 Value *const *Vs = &Elts[0];
732 MDNode *Node = MDNode::get(VMContext,Vs, 12);
733
734 // Create a named metadata so that we do not lose this mdnode.
735 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
736 NMD->addElement(Node);
737
738 return DIGlobalVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +0000739}
740
741
742/// CreateVariable - Create a new descriptor for the specified variable.
743DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
744 const std::string &Name,
745 DICompileUnit CompileUnit, unsigned LineNo,
Devang Pateldd9db662009-01-30 18:20:31 +0000746 DIType Type) {
Devang Patele4b27562009-08-28 23:24:31 +0000747 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000748 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000749 Context.getNode(),
750 MDString::get(VMContext, Name),
751 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000752 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000753 Type.getNode(),
Chris Lattnera45664f2008-11-10 02:56:27 +0000754 };
Devang Patele4b27562009-08-28 23:24:31 +0000755 return DIVariable(MDNode::get(VMContext, &Elts[0], 6));
Chris Lattnera45664f2008-11-10 02:56:27 +0000756}
757
758
759/// CreateBlock - This creates a descriptor for a lexical block with the
Owen Anderson99035272009-07-07 17:12:53 +0000760/// specified parent VMContext.
Chris Lattnera45664f2008-11-10 02:56:27 +0000761DIBlock DIFactory::CreateBlock(DIDescriptor Context) {
Devang Patele4b27562009-08-28 23:24:31 +0000762 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000763 GetTagConstant(dwarf::DW_TAG_lexical_block),
Devang Patele4b27562009-08-28 23:24:31 +0000764 Context.getNode()
Chris Lattnera45664f2008-11-10 02:56:27 +0000765 };
Devang Patele4b27562009-08-28 23:24:31 +0000766 return DIBlock(MDNode::get(VMContext, &Elts[0], 2));
Chris Lattnera45664f2008-11-10 02:56:27 +0000767}
768
769
770//===----------------------------------------------------------------------===//
771// DIFactory: Routines for inserting code into a function
772//===----------------------------------------------------------------------===//
773
774/// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
775/// inserting it at the end of the specified basic block.
776void DIFactory::InsertStopPoint(DICompileUnit CU, unsigned LineNo,
777 unsigned ColNo, BasicBlock *BB) {
778
779 // Lazily construct llvm.dbg.stoppoint function.
780 if (!StopPointFn)
781 StopPointFn = llvm::Intrinsic::getDeclaration(&M,
782 llvm::Intrinsic::dbg_stoppoint);
783
784 // Invoke llvm.dbg.stoppoint
785 Value *Args[] = {
Owen Anderson1d0be152009-08-13 21:58:54 +0000786 ConstantInt::get(llvm::Type::getInt32Ty(VMContext), LineNo),
787 ConstantInt::get(llvm::Type::getInt32Ty(VMContext), ColNo),
Devang Patele4b27562009-08-28 23:24:31 +0000788 CU.getNode()
Chris Lattnera45664f2008-11-10 02:56:27 +0000789 };
790 CallInst::Create(StopPointFn, Args, Args+3, "", BB);
791}
792
793/// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
794/// mark the start of the specified subprogram.
795void DIFactory::InsertSubprogramStart(DISubprogram SP, BasicBlock *BB) {
796 // Lazily construct llvm.dbg.func.start.
797 if (!FuncStartFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000798 FuncStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_func_start);
Chris Lattnera45664f2008-11-10 02:56:27 +0000799
800 // Call llvm.dbg.func.start which also implicitly sets a stoppoint.
Devang Patele4b27562009-08-28 23:24:31 +0000801 CallInst::Create(FuncStartFn, SP.getNode(), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000802}
803
804/// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
805/// mark the start of a region for the specified scoping descriptor.
806void DIFactory::InsertRegionStart(DIDescriptor D, BasicBlock *BB) {
807 // Lazily construct llvm.dbg.region.start function.
808 if (!RegionStartFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000809 RegionStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_start);
810
Chris Lattnera45664f2008-11-10 02:56:27 +0000811 // Call llvm.dbg.func.start.
Devang Patele4b27562009-08-28 23:24:31 +0000812 CallInst::Create(RegionStartFn, D.getNode(), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000813}
814
Chris Lattnera45664f2008-11-10 02:56:27 +0000815/// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
816/// mark the end of a region for the specified scoping descriptor.
817void DIFactory::InsertRegionEnd(DIDescriptor D, BasicBlock *BB) {
818 // Lazily construct llvm.dbg.region.end function.
819 if (!RegionEndFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000820 RegionEndFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_end);
821
822 // Call llvm.dbg.region.end.
Devang Patele4b27562009-08-28 23:24:31 +0000823 CallInst::Create(RegionEndFn, D.getNode(), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000824}
825
826/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Bill Wendlingdc817b62009-05-14 18:26:15 +0000827void DIFactory::InsertDeclare(Value *Storage, DIVariable D, BasicBlock *BB) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000828 // Cast the storage to a {}* for the call to llvm.dbg.declare.
Bill Wendlingdc817b62009-05-14 18:26:15 +0000829 Storage = new BitCastInst(Storage, EmptyStructPtr, "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000830
831 if (!DeclareFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000832 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
833
Devang Patele4b27562009-08-28 23:24:31 +0000834 Value *Args[] = { Storage, D.getNode() };
Chris Lattnera45664f2008-11-10 02:56:27 +0000835 CallInst::Create(DeclareFn, Args, Args+2, "", BB);
836}
Torok Edwin620f2802008-12-16 09:07:36 +0000837
Devang Patele4b27562009-08-28 23:24:31 +0000838
Devang Pateld2f79a12009-07-28 19:55:13 +0000839//===----------------------------------------------------------------------===//
Devang Patel98c65172009-07-30 18:25:15 +0000840// DebugInfoFinder implementations.
Devang Pateld2f79a12009-07-28 19:55:13 +0000841//===----------------------------------------------------------------------===//
842
Devang Patel98c65172009-07-30 18:25:15 +0000843/// processModule - Process entire module and collect debug info.
844void DebugInfoFinder::processModule(Module &M) {
Devang Patele4b27562009-08-28 23:24:31 +0000845
846
Devang Pateld2f79a12009-07-28 19:55:13 +0000847 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
848 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
849 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
850 ++BI) {
851 if (DbgStopPointInst *SPI = dyn_cast<DbgStopPointInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +0000852 processStopPoint(SPI);
Devang Pateld2f79a12009-07-28 19:55:13 +0000853 else if (DbgFuncStartInst *FSI = dyn_cast<DbgFuncStartInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +0000854 processFuncStart(FSI);
Devang Patele802f1c2009-07-30 17:30:23 +0000855 else if (DbgRegionStartInst *DRS = dyn_cast<DbgRegionStartInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +0000856 processRegionStart(DRS);
Devang Patele802f1c2009-07-30 17:30:23 +0000857 else if (DbgRegionEndInst *DRE = dyn_cast<DbgRegionEndInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +0000858 processRegionEnd(DRE);
Devang Patelb4d31302009-07-31 18:18:52 +0000859 else if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
860 processDeclare(DDI);
Devang Pateld2f79a12009-07-28 19:55:13 +0000861 }
Devang Patele4b27562009-08-28 23:24:31 +0000862
863 NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv");
864 if (!NMD)
865 return;
866
867 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
868 DIGlobalVariable DIG(cast<MDNode>(NMD->getElement(i)));
Devang Pateld2f79a12009-07-28 19:55:13 +0000869 if (addGlobalVariable(DIG)) {
870 addCompileUnit(DIG.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +0000871 processType(DIG.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +0000872 }
873 }
874}
875
Devang Patel98c65172009-07-30 18:25:15 +0000876/// processType - Process DIType.
877void DebugInfoFinder::processType(DIType DT) {
Devang Patel72bcdb62009-08-10 22:09:58 +0000878 if (!addType(DT))
Devang Pateld2f79a12009-07-28 19:55:13 +0000879 return;
880
881 addCompileUnit(DT.getCompileUnit());
Devang Patel6ceea332009-08-31 18:49:10 +0000882 if (DT.isCompositeType()) {
Devang Patele4b27562009-08-28 23:24:31 +0000883 DICompositeType DCT(DT.getNode());
Devang Patel98c65172009-07-30 18:25:15 +0000884 processType(DCT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +0000885 DIArray DA = DCT.getTypeArray();
886 if (!DA.isNull())
887 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
888 DIDescriptor D = DA.getElement(i);
Devang Patele4b27562009-08-28 23:24:31 +0000889 DIType TypeE = DIType(D.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +0000890 if (!TypeE.isNull())
Devang Patel98c65172009-07-30 18:25:15 +0000891 processType(TypeE);
Devang Pateld2f79a12009-07-28 19:55:13 +0000892 else
Devang Patele4b27562009-08-28 23:24:31 +0000893 processSubprogram(DISubprogram(D.getNode()));
Devang Pateld2f79a12009-07-28 19:55:13 +0000894 }
Devang Patel6ceea332009-08-31 18:49:10 +0000895 } else if (DT.isDerivedType()) {
Devang Patele4b27562009-08-28 23:24:31 +0000896 DIDerivedType DDT(DT.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +0000897 if (!DDT.isNull())
Devang Patel98c65172009-07-30 18:25:15 +0000898 processType(DDT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +0000899 }
900}
901
Devang Patel98c65172009-07-30 18:25:15 +0000902/// processSubprogram - Process DISubprogram.
903void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Patele802f1c2009-07-30 17:30:23 +0000904 if (SP.isNull())
905 return;
Devang Pateld2f79a12009-07-28 19:55:13 +0000906 if (!addSubprogram(SP))
907 return;
908 addCompileUnit(SP.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +0000909 processType(SP.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +0000910}
911
Devang Patel98c65172009-07-30 18:25:15 +0000912/// processStopPoint - Process DbgStopPointInst.
913void DebugInfoFinder::processStopPoint(DbgStopPointInst *SPI) {
Devang Patele4b27562009-08-28 23:24:31 +0000914 MDNode *Context = dyn_cast<MDNode>(SPI->getContext());
Devang Pateld2f79a12009-07-28 19:55:13 +0000915 addCompileUnit(DICompileUnit(Context));
916}
917
Devang Patel98c65172009-07-30 18:25:15 +0000918/// processFuncStart - Process DbgFuncStartInst.
919void DebugInfoFinder::processFuncStart(DbgFuncStartInst *FSI) {
Devang Patele4b27562009-08-28 23:24:31 +0000920 MDNode *SP = dyn_cast<MDNode>(FSI->getSubprogram());
Devang Patel98c65172009-07-30 18:25:15 +0000921 processSubprogram(DISubprogram(SP));
Devang Pateld2f79a12009-07-28 19:55:13 +0000922}
923
Devang Patel98c65172009-07-30 18:25:15 +0000924/// processRegionStart - Process DbgRegionStart.
925void DebugInfoFinder::processRegionStart(DbgRegionStartInst *DRS) {
Devang Patele4b27562009-08-28 23:24:31 +0000926 MDNode *SP = dyn_cast<MDNode>(DRS->getContext());
Devang Patel98c65172009-07-30 18:25:15 +0000927 processSubprogram(DISubprogram(SP));
Devang Patele802f1c2009-07-30 17:30:23 +0000928}
929
Devang Patel98c65172009-07-30 18:25:15 +0000930/// processRegionEnd - Process DbgRegionEnd.
931void DebugInfoFinder::processRegionEnd(DbgRegionEndInst *DRE) {
Devang Patele4b27562009-08-28 23:24:31 +0000932 MDNode *SP = dyn_cast<MDNode>(DRE->getContext());
Devang Patel98c65172009-07-30 18:25:15 +0000933 processSubprogram(DISubprogram(SP));
Devang Patele802f1c2009-07-30 17:30:23 +0000934}
935
Devang Patelb4d31302009-07-31 18:18:52 +0000936/// processDeclare - Process DbgDeclareInst.
937void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
Devang Patele4b27562009-08-28 23:24:31 +0000938 DIVariable DV(cast<MDNode>(DDI->getVariable()));
Devang Patelb4d31302009-07-31 18:18:52 +0000939 if (DV.isNull())
940 return;
941
Devang Patele4b27562009-08-28 23:24:31 +0000942 if (!NodesSeen.insert(DV.getNode()))
Devang Patelb4d31302009-07-31 18:18:52 +0000943 return;
944
945 addCompileUnit(DV.getCompileUnit());
946 processType(DV.getType());
947}
948
Devang Patel72bcdb62009-08-10 22:09:58 +0000949/// addType - Add type into Tys.
950bool DebugInfoFinder::addType(DIType DT) {
951 if (DT.isNull())
952 return false;
953
Devang Patele4b27562009-08-28 23:24:31 +0000954 if (!NodesSeen.insert(DT.getNode()))
Devang Patel72bcdb62009-08-10 22:09:58 +0000955 return false;
956
Devang Patele4b27562009-08-28 23:24:31 +0000957 TYs.push_back(DT.getNode());
Devang Patel72bcdb62009-08-10 22:09:58 +0000958 return true;
959}
960
Devang Pateld2f79a12009-07-28 19:55:13 +0000961/// addCompileUnit - Add compile unit into CUs.
Devang Patel98c65172009-07-30 18:25:15 +0000962bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Pateld2f79a12009-07-28 19:55:13 +0000963 if (CU.isNull())
964 return false;
965
Devang Patele4b27562009-08-28 23:24:31 +0000966 if (!NodesSeen.insert(CU.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +0000967 return false;
968
Devang Patele4b27562009-08-28 23:24:31 +0000969 CUs.push_back(CU.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +0000970 return true;
971}
972
973/// addGlobalVariable - Add global variable into GVs.
Devang Patel98c65172009-07-30 18:25:15 +0000974bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Pateld2f79a12009-07-28 19:55:13 +0000975 if (DIG.isNull())
976 return false;
977
Devang Patele4b27562009-08-28 23:24:31 +0000978 if (!NodesSeen.insert(DIG.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +0000979 return false;
980
Devang Patele4b27562009-08-28 23:24:31 +0000981 GVs.push_back(DIG.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +0000982 return true;
983}
984
985// addSubprogram - Add subprgoram into SPs.
Devang Patel98c65172009-07-30 18:25:15 +0000986bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Pateld2f79a12009-07-28 19:55:13 +0000987 if (SP.isNull())
988 return false;
989
Devang Patele4b27562009-08-28 23:24:31 +0000990 if (!NodesSeen.insert(SP.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +0000991 return false;
992
Devang Patele4b27562009-08-28 23:24:31 +0000993 SPs.push_back(SP.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +0000994 return true;
995}
996
Torok Edwin620f2802008-12-16 09:07:36 +0000997namespace llvm {
Bill Wendlingdc817b62009-05-14 18:26:15 +0000998 /// findStopPoint - Find the stoppoint coressponding to this instruction, that
999 /// is the stoppoint that dominates this instruction.
1000 const DbgStopPointInst *findStopPoint(const Instruction *Inst) {
Torok Edwin620f2802008-12-16 09:07:36 +00001001 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(Inst))
1002 return DSI;
1003
1004 const BasicBlock *BB = Inst->getParent();
1005 BasicBlock::const_iterator I = Inst, B;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001006 while (BB) {
Torok Edwin620f2802008-12-16 09:07:36 +00001007 B = BB->begin();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001008
Torok Edwin620f2802008-12-16 09:07:36 +00001009 // A BB consisting only of a terminator can't have a stoppoint.
Bill Wendlingdc817b62009-05-14 18:26:15 +00001010 while (I != B) {
1011 --I;
1012 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
1013 return DSI;
Torok Edwin620f2802008-12-16 09:07:36 +00001014 }
Bill Wendlingdc817b62009-05-14 18:26:15 +00001015
1016 // This BB didn't have a stoppoint: if there is only one predecessor, look
1017 // for a stoppoint there. We could use getIDom(), but that would require
1018 // dominator info.
Torok Edwin620f2802008-12-16 09:07:36 +00001019 BB = I->getParent()->getUniquePredecessor();
1020 if (BB)
1021 I = BB->getTerminator();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001022 }
1023
Torok Edwin620f2802008-12-16 09:07:36 +00001024 return 0;
1025 }
1026
Bill Wendlingdc817b62009-05-14 18:26:15 +00001027 /// findBBStopPoint - Find the stoppoint corresponding to first real
1028 /// (non-debug intrinsic) instruction in this Basic Block, and return the
1029 /// stoppoint for it.
1030 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB) {
1031 for(BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001032 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
1033 return DSI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001034
1035 // Fallback to looking for stoppoint of unique predecessor. Useful if this
1036 // BB contains no stoppoints, but unique predecessor does.
Torok Edwin620f2802008-12-16 09:07:36 +00001037 BB = BB->getUniquePredecessor();
1038 if (BB)
1039 return findStopPoint(BB->getTerminator());
Bill Wendlingdc817b62009-05-14 18:26:15 +00001040
Torok Edwin620f2802008-12-16 09:07:36 +00001041 return 0;
1042 }
1043
Bill Wendlingdc817b62009-05-14 18:26:15 +00001044 Value *findDbgGlobalDeclare(GlobalVariable *V) {
Torok Edwinff7d0e92009-03-10 13:41:26 +00001045 const Module *M = V->getParent();
Devang Patele4b27562009-08-28 23:24:31 +00001046 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1047 if (!NMD)
1048 return 0;
Owen Anderson99035272009-07-07 17:12:53 +00001049
Devang Patele4b27562009-08-28 23:24:31 +00001050 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
1051 DIGlobalVariable DIG(cast_or_null<MDNode>(NMD->getElement(i)));
1052 if (DIG.isNull())
1053 continue;
1054 if (DIG.getGlobal() == V)
1055 return DIG.getNode();
Torok Edwinff7d0e92009-03-10 13:41:26 +00001056 }
Torok Edwinff7d0e92009-03-10 13:41:26 +00001057 return 0;
1058 }
1059
Bill Wendlingdc817b62009-05-14 18:26:15 +00001060 /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
Torok Edwin620f2802008-12-16 09:07:36 +00001061 /// It looks through pointer casts too.
Bill Wendlingdc817b62009-05-14 18:26:15 +00001062 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts) {
Torok Edwin620f2802008-12-16 09:07:36 +00001063 if (stripCasts) {
1064 V = V->stripPointerCasts();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001065
Torok Edwin620f2802008-12-16 09:07:36 +00001066 // Look for the bitcast.
1067 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001068 I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001069 if (isa<BitCastInst>(I))
1070 return findDbgDeclare(*I, false);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001071
Torok Edwin620f2802008-12-16 09:07:36 +00001072 return 0;
1073 }
1074
Bill Wendlingdc817b62009-05-14 18:26:15 +00001075 // Find llvm.dbg.declare among uses of the instruction.
Torok Edwin620f2802008-12-16 09:07:36 +00001076 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001077 I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001078 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I))
1079 return DDI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001080
Torok Edwin620f2802008-12-16 09:07:36 +00001081 return 0;
1082 }
Torok Edwinff7d0e92009-03-10 13:41:26 +00001083
Bill Wendlingdc817b62009-05-14 18:26:15 +00001084 bool getLocationInfo(const Value *V, std::string &DisplayName,
1085 std::string &Type, unsigned &LineNo, std::string &File,
1086 std::string &Dir) {
Torok Edwinff7d0e92009-03-10 13:41:26 +00001087 DICompileUnit Unit;
1088 DIType TypeD;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001089
Torok Edwinff7d0e92009-03-10 13:41:26 +00001090 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1091 Value *DIGV = findDbgGlobalDeclare(GV);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001092 if (!DIGV) return false;
Devang Patele4b27562009-08-28 23:24:31 +00001093 DIGlobalVariable Var(cast<MDNode>(DIGV));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001094
Bill Wendling0582ae92009-03-13 04:39:26 +00001095 Var.getDisplayName(DisplayName);
Torok Edwinff7d0e92009-03-10 13:41:26 +00001096 LineNo = Var.getLineNumber();
1097 Unit = Var.getCompileUnit();
1098 TypeD = Var.getType();
1099 } else {
1100 const DbgDeclareInst *DDI = findDbgDeclare(V);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001101 if (!DDI) return false;
Devang Patele4b27562009-08-28 23:24:31 +00001102 DIVariable Var(cast<MDNode>(DDI->getVariable()));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001103
Bill Wendling0582ae92009-03-13 04:39:26 +00001104 Var.getName(DisplayName);
Torok Edwinff7d0e92009-03-10 13:41:26 +00001105 LineNo = Var.getLineNumber();
1106 Unit = Var.getCompileUnit();
1107 TypeD = Var.getType();
1108 }
Bill Wendlingdc817b62009-05-14 18:26:15 +00001109
Bill Wendling0582ae92009-03-13 04:39:26 +00001110 TypeD.getName(Type);
1111 Unit.getFilename(File);
1112 Unit.getDirectory(Dir);
Torok Edwinff7d0e92009-03-10 13:41:26 +00001113 return true;
1114 }
Devang Patel13e16b62009-06-26 01:49:18 +00001115
Devang Patel9e529c32009-07-02 01:15:24 +00001116 /// isValidDebugInfoIntrinsic - Return true if SPI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001117 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001118 bool isValidDebugInfoIntrinsic(DbgStopPointInst &SPI,
1119 CodeGenOpt::Level OptLev) {
1120 return DIDescriptor::ValidDebugInfo(SPI.getContext(), OptLev);
1121 }
1122
1123 /// isValidDebugInfoIntrinsic - Return true if FSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001124 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001125 bool isValidDebugInfoIntrinsic(DbgFuncStartInst &FSI,
1126 CodeGenOpt::Level OptLev) {
1127 return DIDescriptor::ValidDebugInfo(FSI.getSubprogram(), OptLev);
1128 }
1129
1130 /// isValidDebugInfoIntrinsic - Return true if RSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001131 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001132 bool isValidDebugInfoIntrinsic(DbgRegionStartInst &RSI,
1133 CodeGenOpt::Level OptLev) {
1134 return DIDescriptor::ValidDebugInfo(RSI.getContext(), OptLev);
1135 }
1136
1137 /// isValidDebugInfoIntrinsic - Return true if REI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001138 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001139 bool isValidDebugInfoIntrinsic(DbgRegionEndInst &REI,
1140 CodeGenOpt::Level OptLev) {
1141 return DIDescriptor::ValidDebugInfo(REI.getContext(), OptLev);
1142 }
1143
1144
1145 /// isValidDebugInfoIntrinsic - Return true if DI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001146 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001147 bool isValidDebugInfoIntrinsic(DbgDeclareInst &DI,
1148 CodeGenOpt::Level OptLev) {
1149 return DIDescriptor::ValidDebugInfo(DI.getVariable(), OptLev);
1150 }
1151
1152 /// ExtractDebugLocation - Extract debug location information
1153 /// from llvm.dbg.stoppoint intrinsic.
1154 DebugLoc ExtractDebugLocation(DbgStopPointInst &SPI,
Devang Patel9e529c32009-07-02 01:15:24 +00001155 DebugLocTracker &DebugLocInfo) {
1156 DebugLoc DL;
1157 Value *Context = SPI.getContext();
Devang Patel9e529c32009-07-02 01:15:24 +00001158
1159 // If this location is already tracked then use it.
Devang Patele4b27562009-08-28 23:24:31 +00001160 DebugLocTuple Tuple(cast<MDNode>(Context), SPI.getLine(),
Devang Patel9e529c32009-07-02 01:15:24 +00001161 SPI.getColumn());
1162 DenseMap<DebugLocTuple, unsigned>::iterator II
1163 = DebugLocInfo.DebugIdMap.find(Tuple);
1164 if (II != DebugLocInfo.DebugIdMap.end())
1165 return DebugLoc::get(II->second);
1166
1167 // Add a new location entry.
1168 unsigned Id = DebugLocInfo.DebugLocations.size();
1169 DebugLocInfo.DebugLocations.push_back(Tuple);
1170 DebugLocInfo.DebugIdMap[Tuple] = Id;
1171
1172 return DebugLoc::get(Id);
1173 }
1174
1175 /// ExtractDebugLocation - Extract debug location information
1176 /// from llvm.dbg.func_start intrinsic.
1177 DebugLoc ExtractDebugLocation(DbgFuncStartInst &FSI,
Devang Patel9e529c32009-07-02 01:15:24 +00001178 DebugLocTracker &DebugLocInfo) {
1179 DebugLoc DL;
1180 Value *SP = FSI.getSubprogram();
Devang Patel9e529c32009-07-02 01:15:24 +00001181
Devang Patele4b27562009-08-28 23:24:31 +00001182 DISubprogram Subprogram(cast<MDNode>(SP));
Devang Patel9e529c32009-07-02 01:15:24 +00001183 unsigned Line = Subprogram.getLineNumber();
1184 DICompileUnit CU(Subprogram.getCompileUnit());
1185
1186 // If this location is already tracked then use it.
Devang Patele4b27562009-08-28 23:24:31 +00001187 DebugLocTuple Tuple(CU.getNode(), Line, /* Column */ 0);
Devang Patel9e529c32009-07-02 01:15:24 +00001188 DenseMap<DebugLocTuple, unsigned>::iterator II
1189 = DebugLocInfo.DebugIdMap.find(Tuple);
1190 if (II != DebugLocInfo.DebugIdMap.end())
1191 return DebugLoc::get(II->second);
1192
1193 // Add a new location entry.
1194 unsigned Id = DebugLocInfo.DebugLocations.size();
1195 DebugLocInfo.DebugLocations.push_back(Tuple);
1196 DebugLocInfo.DebugIdMap[Tuple] = Id;
1197
1198 return DebugLoc::get(Id);
1199 }
1200
1201 /// isInlinedFnStart - Return true if FSI is starting an inlined function.
1202 bool isInlinedFnStart(DbgFuncStartInst &FSI, const Function *CurrentFn) {
Devang Patele4b27562009-08-28 23:24:31 +00001203 DISubprogram Subprogram(cast<MDNode>(FSI.getSubprogram()));
Devang Patel9e529c32009-07-02 01:15:24 +00001204 if (Subprogram.describes(CurrentFn))
1205 return false;
1206
1207 return true;
1208 }
1209
1210 /// isInlinedFnEnd - Return true if REI is ending an inlined function.
1211 bool isInlinedFnEnd(DbgRegionEndInst &REI, const Function *CurrentFn) {
Devang Patele4b27562009-08-28 23:24:31 +00001212 DISubprogram Subprogram(cast<MDNode>(REI.getContext()));
Devang Patel9e529c32009-07-02 01:15:24 +00001213 if (Subprogram.isNull() || Subprogram.describes(CurrentFn))
1214 return false;
1215
1216 return true;
1217 }
Torok Edwin620f2802008-12-16 09:07:36 +00001218}